2 * Stateless NAT actions
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
12 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/netfilter.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/skbuff.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/string.h>
22 #include <linux/tc_act/tc_nat.h>
23 #include <net/act_api.h>
26 #include <net/netlink.h>
27 #include <net/tc_act/tc_nat.h>
32 static unsigned int nat_net_id;
33 static struct tc_action_ops act_nat_ops;
35 static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
36 [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) },
39 static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
40 struct tc_action **a, int ovr, int bind)
42 struct tc_action_net *tn = net_generic(net, nat_net_id);
43 struct nlattr *tb[TCA_NAT_MAX + 1];
51 err = nla_parse_nested(tb, TCA_NAT_MAX, nla, nat_policy, NULL);
55 if (tb[TCA_NAT_PARMS] == NULL)
57 parm = nla_data(tb[TCA_NAT_PARMS]);
59 if (!tcf_idr_check(tn, parm->index, a, bind)) {
60 ret = tcf_idr_create(tn, parm->index, est, a,
61 &act_nat_ops, bind, false);
68 tcf_idr_release(*a, bind);
74 spin_lock_bh(&p->tcf_lock);
75 p->old_addr = parm->old_addr;
76 p->new_addr = parm->new_addr;
78 p->flags = parm->flags;
80 p->tcf_action = parm->action;
81 spin_unlock_bh(&p->tcf_lock);
83 if (ret == ACT_P_CREATED)
84 tcf_idr_insert(tn, *a);
89 static int tcf_nat(struct sk_buff *skb, const struct tc_action *a,
90 struct tcf_result *res)
92 struct tcf_nat *p = to_tcf_nat(a);
103 spin_lock(&p->tcf_lock);
105 tcf_lastuse_update(&p->tcf_tm);
106 old_addr = p->old_addr;
107 new_addr = p->new_addr;
109 egress = p->flags & TCA_NAT_FLAG_EGRESS;
110 action = p->tcf_action;
112 bstats_update(&p->tcf_bstats, skb);
114 spin_unlock(&p->tcf_lock);
116 if (unlikely(action == TC_ACT_SHOT))
119 noff = skb_network_offset(skb);
120 if (!pskb_may_pull(skb, sizeof(*iph) + noff))
130 if (!((old_addr ^ addr) & mask)) {
131 if (skb_try_make_writable(skb, sizeof(*iph) + noff))
135 new_addr |= addr & ~mask;
137 /* Rewrite IP header */
140 iph->saddr = new_addr;
142 iph->daddr = new_addr;
144 csum_replace4(&iph->check, addr, new_addr);
145 } else if ((iph->frag_off & htons(IP_OFFSET)) ||
146 iph->protocol != IPPROTO_ICMP) {
152 /* It would be nice to share code with stateful NAT. */
153 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
158 if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
159 skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
162 tcph = (void *)(skb_network_header(skb) + ihl);
163 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
171 if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
172 skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
175 udph = (void *)(skb_network_header(skb) + ihl);
176 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
177 inet_proto_csum_replace4(&udph->check, skb, addr,
180 udph->check = CSUM_MANGLED_0;
186 struct icmphdr *icmph;
188 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
191 icmph = (void *)(skb_network_header(skb) + ihl);
193 if ((icmph->type != ICMP_DEST_UNREACH) &&
194 (icmph->type != ICMP_TIME_EXCEEDED) &&
195 (icmph->type != ICMP_PARAMETERPROB))
198 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
202 icmph = (void *)(skb_network_header(skb) + ihl);
203 iph = (void *)(icmph + 1);
209 if ((old_addr ^ addr) & mask)
212 if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
213 sizeof(*iph) + noff))
216 icmph = (void *)(skb_network_header(skb) + ihl);
217 iph = (void *)(icmph + 1);
220 new_addr |= addr & ~mask;
222 /* XXX Fix up the inner checksums. */
224 iph->daddr = new_addr;
226 iph->saddr = new_addr;
228 inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
240 spin_lock(&p->tcf_lock);
241 p->tcf_qstats.drops++;
242 spin_unlock(&p->tcf_lock);
246 static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
249 unsigned char *b = skb_tail_pointer(skb);
250 struct tcf_nat *p = to_tcf_nat(a);
251 struct tc_nat opt = {
252 .old_addr = p->old_addr,
253 .new_addr = p->new_addr,
257 .index = p->tcf_index,
258 .action = p->tcf_action,
259 .refcnt = p->tcf_refcnt - ref,
260 .bindcnt = p->tcf_bindcnt - bind,
264 if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
265 goto nla_put_failure;
267 tcf_tm_dump(&t, &p->tcf_tm);
268 if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
269 goto nla_put_failure;
278 static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
279 struct netlink_callback *cb, int type,
280 const struct tc_action_ops *ops)
282 struct tc_action_net *tn = net_generic(net, nat_net_id);
284 return tcf_generic_walker(tn, skb, cb, type, ops);
287 static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index)
289 struct tc_action_net *tn = net_generic(net, nat_net_id);
291 return tcf_idr_search(tn, a, index);
294 static struct tc_action_ops act_nat_ops = {
297 .owner = THIS_MODULE,
299 .dump = tcf_nat_dump,
300 .init = tcf_nat_init,
301 .walk = tcf_nat_walker,
302 .lookup = tcf_nat_search,
303 .size = sizeof(struct tcf_nat),
306 static __net_init int nat_init_net(struct net *net)
308 struct tc_action_net *tn = net_generic(net, nat_net_id);
310 return tc_action_net_init(tn, &act_nat_ops);
313 static void __net_exit nat_exit_net(struct net *net)
315 struct tc_action_net *tn = net_generic(net, nat_net_id);
317 tc_action_net_exit(tn);
320 static struct pernet_operations nat_net_ops = {
321 .init = nat_init_net,
322 .exit = nat_exit_net,
324 .size = sizeof(struct tc_action_net),
327 MODULE_DESCRIPTION("Stateless NAT actions");
328 MODULE_LICENSE("GPL");
330 static int __init nat_init_module(void)
332 return tcf_register_action(&act_nat_ops, &nat_net_ops);
335 static void __exit nat_cleanup_module(void)
337 tcf_unregister_action(&act_nat_ops, &nat_net_ops);
340 module_init(nat_init_module);
341 module_exit(nat_cleanup_module);