act_nat.c 7.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
H
Herbert Xu 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Stateless NAT actions
 *
 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
 */

#include <linux/errno.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/rtnetlink.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/tc_act/tc_nat.h>
#include <net/act_api.h>
20
#include <net/pkt_cls.h>
H
Herbert Xu 已提交
21 22 23 24 25 26 27 28
#include <net/icmp.h>
#include <net/ip.h>
#include <net/netlink.h>
#include <net/tc_act/tc_nat.h>
#include <net/tcp.h>
#include <net/udp.h>


29
static unsigned int nat_net_id;
30
static struct tc_action_ops act_nat_ops;
31

32 33 34 35
static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
	[TCA_NAT_PARMS]	= { .len = sizeof(struct tc_nat) },
};

36
static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
37
			struct tc_action **a, int ovr, int bind,
38 39
			bool rtnl_held,	struct tcf_proto *tp,
			struct netlink_ext_ack *extack)
H
Herbert Xu 已提交
40
{
41
	struct tc_action_net *tn = net_generic(net, nat_net_id);
42
	struct nlattr *tb[TCA_NAT_MAX + 1];
43
	struct tcf_chain *goto_ch = NULL;
H
Herbert Xu 已提交
44
	struct tc_nat *parm;
45
	int ret = 0, err;
H
Herbert Xu 已提交
46
	struct tcf_nat *p;
47
	u32 index;
H
Herbert Xu 已提交
48

49
	if (nla == NULL)
H
Herbert Xu 已提交
50 51
		return -EINVAL;

52 53
	err = nla_parse_nested_deprecated(tb, TCA_NAT_MAX, nla, nat_policy,
					  NULL);
54 55 56
	if (err < 0)
		return err;

57
	if (tb[TCA_NAT_PARMS] == NULL)
H
Herbert Xu 已提交
58
		return -EINVAL;
59
	parm = nla_data(tb[TCA_NAT_PARMS]);
60 61
	index = parm->index;
	err = tcf_idr_check_alloc(tn, &index, a, bind);
62
	if (!err) {
63
		ret = tcf_idr_create(tn, index, est, a,
64
				     &act_nat_ops, bind, false);
65
		if (ret) {
66
			tcf_idr_cleanup(tn, index);
67
			return ret;
68
		}
H
Herbert Xu 已提交
69
		ret = ACT_P_CREATED;
70
	} else if (err > 0) {
71 72
		if (bind)
			return 0;
73 74
		if (!ovr) {
			tcf_idr_release(*a, bind);
H
Herbert Xu 已提交
75
			return -EEXIST;
76
		}
77 78
	} else {
		return err;
H
Herbert Xu 已提交
79
	}
80 81 82
	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
	if (err < 0)
		goto release_idr;
83
	p = to_tcf_nat(*a);
H
Herbert Xu 已提交
84 85 86 87 88 89 90

	spin_lock_bh(&p->tcf_lock);
	p->old_addr = parm->old_addr;
	p->new_addr = parm->new_addr;
	p->mask = parm->mask;
	p->flags = parm->flags;

91
	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
H
Herbert Xu 已提交
92
	spin_unlock_bh(&p->tcf_lock);
93 94
	if (goto_ch)
		tcf_chain_put_by_act(goto_ch);
H
Herbert Xu 已提交
95 96

	if (ret == ACT_P_CREATED)
97
		tcf_idr_insert(tn, *a);
H
Herbert Xu 已提交
98 99

	return ret;
100 101 102
release_idr:
	tcf_idr_release(*a, bind);
	return err;
H
Herbert Xu 已提交
103 104
}

105 106
static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a,
		       struct tcf_result *res)
H
Herbert Xu 已提交
107
{
108
	struct tcf_nat *p = to_tcf_nat(a);
H
Herbert Xu 已提交
109 110 111 112 113 114 115 116
	struct iphdr *iph;
	__be32 old_addr;
	__be32 new_addr;
	__be32 mask;
	__be32 addr;
	int egress;
	int action;
	int ihl;
C
Changli Gao 已提交
117
	int noff;
H
Herbert Xu 已提交
118 119 120

	spin_lock(&p->tcf_lock);

121
	tcf_lastuse_update(&p->tcf_tm);
H
Herbert Xu 已提交
122 123 124 125 126 127
	old_addr = p->old_addr;
	new_addr = p->new_addr;
	mask = p->mask;
	egress = p->flags & TCA_NAT_FLAG_EGRESS;
	action = p->tcf_action;

128
	bstats_update(&p->tcf_bstats, skb);
H
Herbert Xu 已提交
129 130 131 132 133 134

	spin_unlock(&p->tcf_lock);

	if (unlikely(action == TC_ACT_SHOT))
		goto drop;

C
Changli Gao 已提交
135 136
	noff = skb_network_offset(skb);
	if (!pskb_may_pull(skb, sizeof(*iph) + noff))
H
Herbert Xu 已提交
137 138 139 140 141 142 143 144 145 146
		goto drop;

	iph = ip_hdr(skb);

	if (egress)
		addr = iph->saddr;
	else
		addr = iph->daddr;

	if (!((old_addr ^ addr) & mask)) {
147
		if (skb_try_make_writable(skb, sizeof(*iph) + noff))
H
Herbert Xu 已提交
148 149 150 151 152 153 154 155 156 157 158 159
			goto drop;

		new_addr &= mask;
		new_addr |= addr & ~mask;

		/* Rewrite IP header */
		iph = ip_hdr(skb);
		if (egress)
			iph->saddr = new_addr;
		else
			iph->daddr = new_addr;

160
		csum_replace4(&iph->check, addr, new_addr);
161 162 163
	} else if ((iph->frag_off & htons(IP_OFFSET)) ||
		   iph->protocol != IPPROTO_ICMP) {
		goto out;
H
Herbert Xu 已提交
164 165 166 167 168 169 170 171 172 173
	}

	ihl = iph->ihl * 4;

	/* It would be nice to share code with stateful NAT. */
	switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
	case IPPROTO_TCP:
	{
		struct tcphdr *tcph;

C
Changli Gao 已提交
174
		if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
175
		    skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
H
Herbert Xu 已提交
176 177 178
			goto drop;

		tcph = (void *)(skb_network_header(skb) + ihl);
179 180
		inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
					 true);
H
Herbert Xu 已提交
181 182 183 184 185 186
		break;
	}
	case IPPROTO_UDP:
	{
		struct udphdr *udph;

C
Changli Gao 已提交
187
		if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
188
		    skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
H
Herbert Xu 已提交
189 190 191 192
			goto drop;

		udph = (void *)(skb_network_header(skb) + ihl);
		if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
193
			inet_proto_csum_replace4(&udph->check, skb, addr,
194
						 new_addr, true);
H
Herbert Xu 已提交
195 196 197 198 199 200 201 202 203
			if (!udph->check)
				udph->check = CSUM_MANGLED_0;
		}
		break;
	}
	case IPPROTO_ICMP:
	{
		struct icmphdr *icmph;

C
Changli Gao 已提交
204
		if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
H
Herbert Xu 已提交
205 206 207 208 209 210 211 212 213
			goto drop;

		icmph = (void *)(skb_network_header(skb) + ihl);

		if ((icmph->type != ICMP_DEST_UNREACH) &&
		    (icmph->type != ICMP_TIME_EXCEEDED) &&
		    (icmph->type != ICMP_PARAMETERPROB))
			break;

C
Changli Gao 已提交
214 215
		if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
					noff))
216 217
			goto drop;

C
Changli Gao 已提交
218
		icmph = (void *)(skb_network_header(skb) + ihl);
H
Herbert Xu 已提交
219 220 221 222 223 224 225 226 227
		iph = (void *)(icmph + 1);
		if (egress)
			addr = iph->daddr;
		else
			addr = iph->saddr;

		if ((old_addr ^ addr) & mask)
			break;

228 229
		if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
					  sizeof(*iph) + noff))
H
Herbert Xu 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243
			goto drop;

		icmph = (void *)(skb_network_header(skb) + ihl);
		iph = (void *)(icmph + 1);

		new_addr &= mask;
		new_addr |= addr & ~mask;

		/* XXX Fix up the inner checksums. */
		if (egress)
			iph->daddr = new_addr;
		else
			iph->saddr = new_addr;

244
		inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
245
					 false);
H
Herbert Xu 已提交
246 247 248 249 250 251
		break;
	}
	default:
		break;
	}

252
out:
H
Herbert Xu 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265
	return action;

drop:
	spin_lock(&p->tcf_lock);
	p->tcf_qstats.drops++;
	spin_unlock(&p->tcf_lock);
	return TC_ACT_SHOT;
}

static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
			int bind, int ref)
{
	unsigned char *b = skb_tail_pointer(skb);
266
	struct tcf_nat *p = to_tcf_nat(a);
267 268
	struct tc_nat opt = {
		.index    = p->tcf_index,
269 270
		.refcnt   = refcount_read(&p->tcf_refcnt) - ref,
		.bindcnt  = atomic_read(&p->tcf_bindcnt) - bind,
271
	};
H
Herbert Xu 已提交
272 273
	struct tcf_t t;

274 275 276 277 278 279 280
	spin_lock_bh(&p->tcf_lock);
	opt.old_addr = p->old_addr;
	opt.new_addr = p->new_addr;
	opt.mask = p->mask;
	opt.flags = p->flags;
	opt.action = p->tcf_action;

281 282
	if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
		goto nla_put_failure;
283 284

	tcf_tm_dump(&t, &p->tcf_tm);
285
	if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
286
		goto nla_put_failure;
287
	spin_unlock_bh(&p->tcf_lock);
H
Herbert Xu 已提交
288 289 290

	return skb->len;

291
nla_put_failure:
292
	spin_unlock_bh(&p->tcf_lock);
H
Herbert Xu 已提交
293 294 295 296
	nlmsg_trim(skb, b);
	return -1;
}

297 298
static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
			  struct netlink_callback *cb, int type,
299 300
			  const struct tc_action_ops *ops,
			  struct netlink_ext_ack *extack)
301 302 303
{
	struct tc_action_net *tn = net_generic(net, nat_net_id);

304
	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
305 306
}

307
static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index)
308 309 310
{
	struct tc_action_net *tn = net_generic(net, nat_net_id);

311
	return tcf_idr_search(tn, a, index);
312 313
}

H
Herbert Xu 已提交
314 315
static struct tc_action_ops act_nat_ops = {
	.kind		=	"nat",
316
	.id		=	TCA_ID_NAT,
H
Herbert Xu 已提交
317
	.owner		=	THIS_MODULE,
318
	.act		=	tcf_nat_act,
H
Herbert Xu 已提交
319 320
	.dump		=	tcf_nat_dump,
	.init		=	tcf_nat_init,
321 322
	.walk		=	tcf_nat_walker,
	.lookup		=	tcf_nat_search,
323
	.size		=	sizeof(struct tcf_nat),
324 325 326 327 328 329
};

static __net_init int nat_init_net(struct net *net)
{
	struct tc_action_net *tn = net_generic(net, nat_net_id);

330
	return tc_action_net_init(tn, &act_nat_ops);
331 332
}

333
static void __net_exit nat_exit_net(struct list_head *net_list)
334
{
335
	tc_action_net_exit(net_list, nat_net_id);
336 337 338 339
}

static struct pernet_operations nat_net_ops = {
	.init = nat_init_net,
340
	.exit_batch = nat_exit_net,
341 342
	.id   = &nat_net_id,
	.size = sizeof(struct tc_action_net),
H
Herbert Xu 已提交
343 344 345 346 347 348 349
};

MODULE_DESCRIPTION("Stateless NAT actions");
MODULE_LICENSE("GPL");

static int __init nat_init_module(void)
{
350
	return tcf_register_action(&act_nat_ops, &nat_net_ops);
H
Herbert Xu 已提交
351 352 353 354
}

static void __exit nat_cleanup_module(void)
{
355
	tcf_unregister_action(&act_nat_ops, &nat_net_ops);
H
Herbert Xu 已提交
356 357 358 359
}

module_init(nat_init_module);
module_exit(nat_cleanup_module);