s390/mm: fix page table upgrade vs 2ndary address mode accesses
[linux/fpc-iii.git] / net / sched / act_nat.c
blobd1b47a1b145c40f0acd98ca0ab30f9ecce4f364a
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,
41 bool rtnl_held, struct netlink_ext_ack *extack)
43 struct tc_action_net *tn = net_generic(net, nat_net_id);
44 struct nlattr *tb[TCA_NAT_MAX + 1];
45 struct tc_nat *parm;
46 int ret = 0, err;
47 struct tcf_nat *p;
48 u32 index;
50 if (nla == NULL)
51 return -EINVAL;
53 err = nla_parse_nested(tb, TCA_NAT_MAX, nla, nat_policy, NULL);
54 if (err < 0)
55 return err;
57 if (tb[TCA_NAT_PARMS] == NULL)
58 return -EINVAL;
59 parm = nla_data(tb[TCA_NAT_PARMS]);
60 index = parm->index;
61 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;
69 ret = ACT_P_CREATED;
70 } else if (err > 0) {
71 if (bind)
72 return 0;
73 if (!ovr) {
74 tcf_idr_release(*a, bind);
75 return -EEXIST;
77 } else {
78 return err;
80 p = to_tcf_nat(*a);
82 spin_lock_bh(&p->tcf_lock);
83 p->old_addr = parm->old_addr;
84 p->new_addr = parm->new_addr;
85 p->mask = parm->mask;
86 p->flags = parm->flags;
88 p->tcf_action = parm->action;
89 spin_unlock_bh(&p->tcf_lock);
91 if (ret == ACT_P_CREATED)
92 tcf_idr_insert(tn, *a);
94 return ret;
97 static int tcf_nat_act(struct sk_buff *skb, const struct tc_action *a,
98 struct tcf_result *res)
100 struct tcf_nat *p = to_tcf_nat(a);
101 struct iphdr *iph;
102 __be32 old_addr;
103 __be32 new_addr;
104 __be32 mask;
105 __be32 addr;
106 int egress;
107 int action;
108 int ihl;
109 int noff;
111 spin_lock(&p->tcf_lock);
113 tcf_lastuse_update(&p->tcf_tm);
114 old_addr = p->old_addr;
115 new_addr = p->new_addr;
116 mask = p->mask;
117 egress = p->flags & TCA_NAT_FLAG_EGRESS;
118 action = p->tcf_action;
120 bstats_update(&p->tcf_bstats, skb);
122 spin_unlock(&p->tcf_lock);
124 if (unlikely(action == TC_ACT_SHOT))
125 goto drop;
127 noff = skb_network_offset(skb);
128 if (!pskb_may_pull(skb, sizeof(*iph) + noff))
129 goto drop;
131 iph = ip_hdr(skb);
133 if (egress)
134 addr = iph->saddr;
135 else
136 addr = iph->daddr;
138 if (!((old_addr ^ addr) & mask)) {
139 if (skb_try_make_writable(skb, sizeof(*iph) + noff))
140 goto drop;
142 new_addr &= mask;
143 new_addr |= addr & ~mask;
145 /* Rewrite IP header */
146 iph = ip_hdr(skb);
147 if (egress)
148 iph->saddr = new_addr;
149 else
150 iph->daddr = new_addr;
152 csum_replace4(&iph->check, addr, new_addr);
153 } else if ((iph->frag_off & htons(IP_OFFSET)) ||
154 iph->protocol != IPPROTO_ICMP) {
155 goto out;
158 ihl = iph->ihl * 4;
160 /* It would be nice to share code with stateful NAT. */
161 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
162 case IPPROTO_TCP:
164 struct tcphdr *tcph;
166 if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
167 skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
168 goto drop;
170 tcph = (void *)(skb_network_header(skb) + ihl);
171 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
172 true);
173 break;
175 case IPPROTO_UDP:
177 struct udphdr *udph;
179 if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
180 skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
181 goto drop;
183 udph = (void *)(skb_network_header(skb) + ihl);
184 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
185 inet_proto_csum_replace4(&udph->check, skb, addr,
186 new_addr, true);
187 if (!udph->check)
188 udph->check = CSUM_MANGLED_0;
190 break;
192 case IPPROTO_ICMP:
194 struct icmphdr *icmph;
196 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
197 goto drop;
199 icmph = (void *)(skb_network_header(skb) + ihl);
201 if ((icmph->type != ICMP_DEST_UNREACH) &&
202 (icmph->type != ICMP_TIME_EXCEEDED) &&
203 (icmph->type != ICMP_PARAMETERPROB))
204 break;
206 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
207 noff))
208 goto drop;
210 icmph = (void *)(skb_network_header(skb) + ihl);
211 iph = (void *)(icmph + 1);
212 if (egress)
213 addr = iph->daddr;
214 else
215 addr = iph->saddr;
217 if ((old_addr ^ addr) & mask)
218 break;
220 if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
221 sizeof(*iph) + noff))
222 goto drop;
224 icmph = (void *)(skb_network_header(skb) + ihl);
225 iph = (void *)(icmph + 1);
227 new_addr &= mask;
228 new_addr |= addr & ~mask;
230 /* XXX Fix up the inner checksums. */
231 if (egress)
232 iph->daddr = new_addr;
233 else
234 iph->saddr = new_addr;
236 inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
237 false);
238 break;
240 default:
241 break;
244 out:
245 return action;
247 drop:
248 spin_lock(&p->tcf_lock);
249 p->tcf_qstats.drops++;
250 spin_unlock(&p->tcf_lock);
251 return TC_ACT_SHOT;
254 static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
255 int bind, int ref)
257 unsigned char *b = skb_tail_pointer(skb);
258 struct tcf_nat *p = to_tcf_nat(a);
259 struct tc_nat opt = {
260 .old_addr = p->old_addr,
261 .new_addr = p->new_addr,
262 .mask = p->mask,
263 .flags = p->flags,
265 .index = p->tcf_index,
266 .action = p->tcf_action,
267 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
268 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
270 struct tcf_t t;
272 if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
273 goto nla_put_failure;
275 tcf_tm_dump(&t, &p->tcf_tm);
276 if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
277 goto nla_put_failure;
279 return skb->len;
281 nla_put_failure:
282 nlmsg_trim(skb, b);
283 return -1;
286 static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
287 struct netlink_callback *cb, int type,
288 const struct tc_action_ops *ops,
289 struct netlink_ext_ack *extack)
291 struct tc_action_net *tn = net_generic(net, nat_net_id);
293 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
296 static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index,
297 struct netlink_ext_ack *extack)
299 struct tc_action_net *tn = net_generic(net, nat_net_id);
301 return tcf_idr_search(tn, a, index);
304 static struct tc_action_ops act_nat_ops = {
305 .kind = "nat",
306 .type = TCA_ACT_NAT,
307 .owner = THIS_MODULE,
308 .act = tcf_nat_act,
309 .dump = tcf_nat_dump,
310 .init = tcf_nat_init,
311 .walk = tcf_nat_walker,
312 .lookup = tcf_nat_search,
313 .size = sizeof(struct tcf_nat),
316 static __net_init int nat_init_net(struct net *net)
318 struct tc_action_net *tn = net_generic(net, nat_net_id);
320 return tc_action_net_init(net, tn, &act_nat_ops);
323 static void __net_exit nat_exit_net(struct list_head *net_list)
325 tc_action_net_exit(net_list, nat_net_id);
328 static struct pernet_operations nat_net_ops = {
329 .init = nat_init_net,
330 .exit_batch = nat_exit_net,
331 .id = &nat_net_id,
332 .size = sizeof(struct tc_action_net),
335 MODULE_DESCRIPTION("Stateless NAT actions");
336 MODULE_LICENSE("GPL");
338 static int __init nat_init_module(void)
340 return tcf_register_action(&act_nat_ops, &nat_net_ops);
343 static void __exit nat_cleanup_module(void)
345 tcf_unregister_action(&act_nat_ops, &nat_net_ops);
348 module_init(nat_init_module);
349 module_exit(nat_cleanup_module);