1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Stateless NAT actions
5 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
8 #include <linux/errno.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/netfilter.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/skbuff.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/string.h>
18 #include <linux/tc_act/tc_nat.h>
19 #include <net/act_api.h>
20 #include <net/pkt_cls.h>
23 #include <net/netlink.h>
24 #include <net/tc_act/tc_nat.h>
29 static struct tc_action_ops act_nat_ops;
31 static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
32 [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) },
35 static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
36 struct tc_action **a, struct tcf_proto *tp,
37 u32 flags, struct netlink_ext_ack *extack)
39 struct tc_action_net *tn = net_generic(net, act_nat_ops.net_id);
40 bool bind = flags & TCA_ACT_FLAGS_BIND;
41 struct nlattr *tb[TCA_NAT_MAX + 1];
42 struct tcf_chain *goto_ch = NULL;
51 err = nla_parse_nested_deprecated(tb, TCA_NAT_MAX, nla, nat_policy,
56 if (tb[TCA_NAT_PARMS] == NULL)
58 parm = nla_data(tb[TCA_NAT_PARMS]);
60 err = tcf_idr_check_alloc(tn, &index, a, bind);
62 ret = tcf_idr_create(tn, index, est, a,
63 &act_nat_ops, bind, false, flags);
65 tcf_idr_cleanup(tn, index);
72 if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
73 tcf_idr_release(*a, bind);
79 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
84 spin_lock_bh(&p->tcf_lock);
85 p->old_addr = parm->old_addr;
86 p->new_addr = parm->new_addr;
88 p->flags = parm->flags;
90 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
91 spin_unlock_bh(&p->tcf_lock);
93 tcf_chain_put_by_act(goto_ch);
97 tcf_idr_release(*a, bind);
101 static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a,
102 struct tcf_result *res)
104 struct tcf_nat *p = to_tcf_nat(a);
115 spin_lock(&p->tcf_lock);
117 tcf_lastuse_update(&p->tcf_tm);
118 old_addr = p->old_addr;
119 new_addr = p->new_addr;
121 egress = p->flags & TCA_NAT_FLAG_EGRESS;
122 action = p->tcf_action;
124 bstats_update(&p->tcf_bstats, skb);
126 spin_unlock(&p->tcf_lock);
128 if (unlikely(action == TC_ACT_SHOT))
131 noff = skb_network_offset(skb);
132 if (!pskb_may_pull(skb, sizeof(*iph) + noff))
142 if (!((old_addr ^ addr) & mask)) {
143 if (skb_try_make_writable(skb, sizeof(*iph) + noff))
147 new_addr |= addr & ~mask;
149 /* Rewrite IP header */
152 iph->saddr = new_addr;
154 iph->daddr = new_addr;
156 csum_replace4(&iph->check, addr, new_addr);
157 } else if ((iph->frag_off & htons(IP_OFFSET)) ||
158 iph->protocol != IPPROTO_ICMP) {
164 /* It would be nice to share code with stateful NAT. */
165 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
170 if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
171 skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
174 tcph = (void *)(skb_network_header(skb) + ihl);
175 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
183 if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
184 skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
187 udph = (void *)(skb_network_header(skb) + ihl);
188 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
189 inet_proto_csum_replace4(&udph->check, skb, addr,
192 udph->check = CSUM_MANGLED_0;
198 struct icmphdr *icmph;
200 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
203 icmph = (void *)(skb_network_header(skb) + ihl);
205 if (!icmp_is_err(icmph->type))
208 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
212 icmph = (void *)(skb_network_header(skb) + ihl);
213 iph = (void *)(icmph + 1);
219 if ((old_addr ^ addr) & mask)
222 if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
223 sizeof(*iph) + noff))
226 icmph = (void *)(skb_network_header(skb) + ihl);
227 iph = (void *)(icmph + 1);
230 new_addr |= addr & ~mask;
232 /* XXX Fix up the inner checksums. */
234 iph->daddr = new_addr;
236 iph->saddr = new_addr;
238 inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
250 spin_lock(&p->tcf_lock);
251 p->tcf_qstats.drops++;
252 spin_unlock(&p->tcf_lock);
256 static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
259 unsigned char *b = skb_tail_pointer(skb);
260 struct tcf_nat *p = to_tcf_nat(a);
261 struct tc_nat opt = {
262 .index = p->tcf_index,
263 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
264 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
268 spin_lock_bh(&p->tcf_lock);
269 opt.old_addr = p->old_addr;
270 opt.new_addr = p->new_addr;
272 opt.flags = p->flags;
273 opt.action = p->tcf_action;
275 if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
276 goto nla_put_failure;
278 tcf_tm_dump(&t, &p->tcf_tm);
279 if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
280 goto nla_put_failure;
281 spin_unlock_bh(&p->tcf_lock);
286 spin_unlock_bh(&p->tcf_lock);
291 static struct tc_action_ops act_nat_ops = {
294 .owner = THIS_MODULE,
296 .dump = tcf_nat_dump,
297 .init = tcf_nat_init,
298 .size = sizeof(struct tcf_nat),
301 static __net_init int nat_init_net(struct net *net)
303 struct tc_action_net *tn = net_generic(net, act_nat_ops.net_id);
305 return tc_action_net_init(net, tn, &act_nat_ops);
308 static void __net_exit nat_exit_net(struct list_head *net_list)
310 tc_action_net_exit(net_list, act_nat_ops.net_id);
313 static struct pernet_operations nat_net_ops = {
314 .init = nat_init_net,
315 .exit_batch = nat_exit_net,
316 .id = &act_nat_ops.net_id,
317 .size = sizeof(struct tc_action_net),
320 MODULE_DESCRIPTION("Stateless NAT actions");
321 MODULE_LICENSE("GPL");
323 static int __init nat_init_module(void)
325 return tcf_register_action(&act_nat_ops, &nat_net_ops);
328 static void __exit nat_cleanup_module(void)
330 tcf_unregister_action(&act_nat_ops, &nat_net_ops);
333 module_init(nat_init_module);
334 module_exit(nat_cleanup_module);