Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / net / sched / act_nat.c
blob98c6a4b2f5238c2b46113df9bd8276068ccff2bc
1 /*
2 * Stateless NAT actions
4 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
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)
9 * any later version.
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>
24 #include <net/icmp.h>
25 #include <net/ip.h>
26 #include <net/netlink.h>
27 #include <net/tc_act/tc_nat.h>
28 #include <net/tcp.h>
29 #include <net/udp.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];
44 struct tc_nat *parm;
45 int ret = 0, err;
46 struct tcf_nat *p;
48 if (nla == NULL)
49 return -EINVAL;
51 err = nla_parse_nested(tb, TCA_NAT_MAX, nla, nat_policy, NULL);
52 if (err < 0)
53 return err;
55 if (tb[TCA_NAT_PARMS] == NULL)
56 return -EINVAL;
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);
62 if (ret)
63 return ret;
64 ret = ACT_P_CREATED;
65 } else {
66 if (bind)
67 return 0;
68 tcf_idr_release(*a, bind);
69 if (!ovr)
70 return -EEXIST;
72 p = to_tcf_nat(*a);
74 spin_lock_bh(&p->tcf_lock);
75 p->old_addr = parm->old_addr;
76 p->new_addr = parm->new_addr;
77 p->mask = parm->mask;
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);
86 return ret;
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);
93 struct iphdr *iph;
94 __be32 old_addr;
95 __be32 new_addr;
96 __be32 mask;
97 __be32 addr;
98 int egress;
99 int action;
100 int ihl;
101 int noff;
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;
108 mask = p->mask;
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))
117 goto drop;
119 noff = skb_network_offset(skb);
120 if (!pskb_may_pull(skb, sizeof(*iph) + noff))
121 goto drop;
123 iph = ip_hdr(skb);
125 if (egress)
126 addr = iph->saddr;
127 else
128 addr = iph->daddr;
130 if (!((old_addr ^ addr) & mask)) {
131 if (skb_try_make_writable(skb, sizeof(*iph) + noff))
132 goto drop;
134 new_addr &= mask;
135 new_addr |= addr & ~mask;
137 /* Rewrite IP header */
138 iph = ip_hdr(skb);
139 if (egress)
140 iph->saddr = new_addr;
141 else
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) {
147 goto out;
150 ihl = iph->ihl * 4;
152 /* It would be nice to share code with stateful NAT. */
153 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
154 case IPPROTO_TCP:
156 struct tcphdr *tcph;
158 if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
159 skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
160 goto drop;
162 tcph = (void *)(skb_network_header(skb) + ihl);
163 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
164 true);
165 break;
167 case IPPROTO_UDP:
169 struct udphdr *udph;
171 if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
172 skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
173 goto drop;
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,
178 new_addr, true);
179 if (!udph->check)
180 udph->check = CSUM_MANGLED_0;
182 break;
184 case IPPROTO_ICMP:
186 struct icmphdr *icmph;
188 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
189 goto drop;
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))
196 break;
198 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
199 noff))
200 goto drop;
202 icmph = (void *)(skb_network_header(skb) + ihl);
203 iph = (void *)(icmph + 1);
204 if (egress)
205 addr = iph->daddr;
206 else
207 addr = iph->saddr;
209 if ((old_addr ^ addr) & mask)
210 break;
212 if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
213 sizeof(*iph) + noff))
214 goto drop;
216 icmph = (void *)(skb_network_header(skb) + ihl);
217 iph = (void *)(icmph + 1);
219 new_addr &= mask;
220 new_addr |= addr & ~mask;
222 /* XXX Fix up the inner checksums. */
223 if (egress)
224 iph->daddr = new_addr;
225 else
226 iph->saddr = new_addr;
228 inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
229 false);
230 break;
232 default:
233 break;
236 out:
237 return action;
239 drop:
240 spin_lock(&p->tcf_lock);
241 p->tcf_qstats.drops++;
242 spin_unlock(&p->tcf_lock);
243 return TC_ACT_SHOT;
246 static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
247 int bind, int ref)
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,
254 .mask = p->mask,
255 .flags = p->flags,
257 .index = p->tcf_index,
258 .action = p->tcf_action,
259 .refcnt = p->tcf_refcnt - ref,
260 .bindcnt = p->tcf_bindcnt - bind,
262 struct tcf_t t;
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;
271 return skb->len;
273 nla_put_failure:
274 nlmsg_trim(skb, b);
275 return -1;
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 = {
295 .kind = "nat",
296 .type = TCA_ACT_NAT,
297 .owner = THIS_MODULE,
298 .act = tcf_nat,
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 list_head *net_list)
315 tc_action_net_exit(net_list, nat_net_id);
318 static struct pernet_operations nat_net_ops = {
319 .init = nat_init_net,
320 .exit_batch = nat_exit_net,
321 .id = &nat_net_id,
322 .size = sizeof(struct tc_action_net),
325 MODULE_DESCRIPTION("Stateless NAT actions");
326 MODULE_LICENSE("GPL");
328 static int __init nat_init_module(void)
330 return tcf_register_action(&act_nat_ops, &nat_net_ops);
333 static void __exit nat_cleanup_module(void)
335 tcf_unregister_action(&act_nat_ops, &nat_net_ops);
338 module_init(nat_init_module);
339 module_exit(nat_cleanup_module);