]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/sched/act_nat.c
Merge branch 'topic/error' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[mirror_ubuntu-zesty-kernel.git] / net / sched / act_nat.c
CommitLineData
b4219952
HX
1/*
2 * Stateless NAT actions
3 *
4 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
5 *
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.
10 */
11
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>
30
31
32#define NAT_TAB_MASK 15
b4219952 33
ddf97ccd 34static int nat_net_id;
a85a970a 35static struct tc_action_ops act_nat_ops;
ddf97ccd 36
53b2bf3f
PM
37static const struct nla_policy nat_policy[TCA_NAT_MAX + 1] = {
38 [TCA_NAT_PARMS] = { .len = sizeof(struct tc_nat) },
39};
40
c1b52739 41static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
a85a970a 42 struct tc_action **a, int ovr, int bind)
b4219952 43{
ddf97ccd 44 struct tc_action_net *tn = net_generic(net, nat_net_id);
7ba699c6 45 struct nlattr *tb[TCA_NAT_MAX + 1];
b4219952 46 struct tc_nat *parm;
cee63723 47 int ret = 0, err;
b4219952 48 struct tcf_nat *p;
b4219952 49
cee63723 50 if (nla == NULL)
b4219952
HX
51 return -EINVAL;
52
53b2bf3f 53 err = nla_parse_nested(tb, TCA_NAT_MAX, nla, nat_policy);
cee63723
PM
54 if (err < 0)
55 return err;
56
53b2bf3f 57 if (tb[TCA_NAT_PARMS] == NULL)
b4219952 58 return -EINVAL;
7ba699c6 59 parm = nla_data(tb[TCA_NAT_PARMS]);
b4219952 60
ddf97ccd
WC
61 if (!tcf_hash_check(tn, parm->index, a, bind)) {
62 ret = tcf_hash_create(tn, parm->index, est, a,
a85a970a 63 &act_nat_ops, bind, false);
86062033
WC
64 if (ret)
65 return ret;
b4219952
HX
66 ret = ACT_P_CREATED;
67 } else {
1a29321e
JHS
68 if (bind)
69 return 0;
a85a970a 70 tcf_hash_release(*a, bind);
1a29321e 71 if (!ovr)
b4219952 72 return -EEXIST;
b4219952 73 }
a85a970a 74 p = to_tcf_nat(*a);
b4219952
HX
75
76 spin_lock_bh(&p->tcf_lock);
77 p->old_addr = parm->old_addr;
78 p->new_addr = parm->new_addr;
79 p->mask = parm->mask;
80 p->flags = parm->flags;
81
82 p->tcf_action = parm->action;
83 spin_unlock_bh(&p->tcf_lock);
84
85 if (ret == ACT_P_CREATED)
a85a970a 86 tcf_hash_insert(tn, *a);
b4219952
HX
87
88 return ret;
89}
90
dc7f9f6e 91static int tcf_nat(struct sk_buff *skb, const struct tc_action *a,
b4219952
HX
92 struct tcf_result *res)
93{
a85a970a 94 struct tcf_nat *p = to_tcf_nat(a);
b4219952
HX
95 struct iphdr *iph;
96 __be32 old_addr;
97 __be32 new_addr;
98 __be32 mask;
99 __be32 addr;
100 int egress;
101 int action;
102 int ihl;
36d12690 103 int noff;
b4219952
HX
104
105 spin_lock(&p->tcf_lock);
106
9c4a4e48 107 tcf_lastuse_update(&p->tcf_tm);
b4219952
HX
108 old_addr = p->old_addr;
109 new_addr = p->new_addr;
110 mask = p->mask;
111 egress = p->flags & TCA_NAT_FLAG_EGRESS;
112 action = p->tcf_action;
113
bfe0d029 114 bstats_update(&p->tcf_bstats, skb);
b4219952
HX
115
116 spin_unlock(&p->tcf_lock);
117
118 if (unlikely(action == TC_ACT_SHOT))
119 goto drop;
120
36d12690
CG
121 noff = skb_network_offset(skb);
122 if (!pskb_may_pull(skb, sizeof(*iph) + noff))
b4219952
HX
123 goto drop;
124
125 iph = ip_hdr(skb);
126
127 if (egress)
128 addr = iph->saddr;
129 else
130 addr = iph->daddr;
131
132 if (!((old_addr ^ addr) & mask)) {
3697649f 133 if (skb_try_make_writable(skb, sizeof(*iph) + noff))
b4219952
HX
134 goto drop;
135
136 new_addr &= mask;
137 new_addr |= addr & ~mask;
138
139 /* Rewrite IP header */
140 iph = ip_hdr(skb);
141 if (egress)
142 iph->saddr = new_addr;
143 else
144 iph->daddr = new_addr;
145
be0ea7d5 146 csum_replace4(&iph->check, addr, new_addr);
33c29dde
CG
147 } else if ((iph->frag_off & htons(IP_OFFSET)) ||
148 iph->protocol != IPPROTO_ICMP) {
149 goto out;
b4219952
HX
150 }
151
152 ihl = iph->ihl * 4;
153
154 /* It would be nice to share code with stateful NAT. */
155 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
156 case IPPROTO_TCP:
157 {
158 struct tcphdr *tcph;
159
36d12690 160 if (!pskb_may_pull(skb, ihl + sizeof(*tcph) + noff) ||
3697649f 161 skb_try_make_writable(skb, ihl + sizeof(*tcph) + noff))
b4219952
HX
162 goto drop;
163
164 tcph = (void *)(skb_network_header(skb) + ihl);
4b048d6d
TH
165 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr,
166 true);
b4219952
HX
167 break;
168 }
169 case IPPROTO_UDP:
170 {
171 struct udphdr *udph;
172
36d12690 173 if (!pskb_may_pull(skb, ihl + sizeof(*udph) + noff) ||
3697649f 174 skb_try_make_writable(skb, ihl + sizeof(*udph) + noff))
b4219952
HX
175 goto drop;
176
177 udph = (void *)(skb_network_header(skb) + ihl);
178 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
be0ea7d5 179 inet_proto_csum_replace4(&udph->check, skb, addr,
4b048d6d 180 new_addr, true);
b4219952
HX
181 if (!udph->check)
182 udph->check = CSUM_MANGLED_0;
183 }
184 break;
185 }
186 case IPPROTO_ICMP:
187 {
188 struct icmphdr *icmph;
189
36d12690 190 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + noff))
b4219952
HX
191 goto drop;
192
193 icmph = (void *)(skb_network_header(skb) + ihl);
194
195 if ((icmph->type != ICMP_DEST_UNREACH) &&
196 (icmph->type != ICMP_TIME_EXCEEDED) &&
197 (icmph->type != ICMP_PARAMETERPROB))
198 break;
199
36d12690
CG
200 if (!pskb_may_pull(skb, ihl + sizeof(*icmph) + sizeof(*iph) +
201 noff))
70c2efa5
CG
202 goto drop;
203
072d79a3 204 icmph = (void *)(skb_network_header(skb) + ihl);
b4219952
HX
205 iph = (void *)(icmph + 1);
206 if (egress)
207 addr = iph->daddr;
208 else
209 addr = iph->saddr;
210
211 if ((old_addr ^ addr) & mask)
212 break;
213
3697649f
DB
214 if (skb_try_make_writable(skb, ihl + sizeof(*icmph) +
215 sizeof(*iph) + noff))
b4219952
HX
216 goto drop;
217
218 icmph = (void *)(skb_network_header(skb) + ihl);
219 iph = (void *)(icmph + 1);
220
221 new_addr &= mask;
222 new_addr |= addr & ~mask;
223
224 /* XXX Fix up the inner checksums. */
225 if (egress)
226 iph->daddr = new_addr;
227 else
228 iph->saddr = new_addr;
229
be0ea7d5 230 inet_proto_csum_replace4(&icmph->checksum, skb, addr, new_addr,
4b048d6d 231 false);
b4219952
HX
232 break;
233 }
234 default:
235 break;
236 }
237
33c29dde 238out:
b4219952
HX
239 return action;
240
241drop:
242 spin_lock(&p->tcf_lock);
243 p->tcf_qstats.drops++;
244 spin_unlock(&p->tcf_lock);
245 return TC_ACT_SHOT;
246}
247
248static int tcf_nat_dump(struct sk_buff *skb, struct tc_action *a,
249 int bind, int ref)
250{
251 unsigned char *b = skb_tail_pointer(skb);
a85a970a 252 struct tcf_nat *p = to_tcf_nat(a);
1c40be12
ED
253 struct tc_nat opt = {
254 .old_addr = p->old_addr,
255 .new_addr = p->new_addr,
256 .mask = p->mask,
257 .flags = p->flags,
258
259 .index = p->tcf_index,
260 .action = p->tcf_action,
261 .refcnt = p->tcf_refcnt - ref,
262 .bindcnt = p->tcf_bindcnt - bind,
263 };
b4219952 264 struct tcf_t t;
b4219952 265
1b34ec43
DM
266 if (nla_put(skb, TCA_NAT_PARMS, sizeof(opt), &opt))
267 goto nla_put_failure;
48d8ee16
JHS
268
269 tcf_tm_dump(&t, &p->tcf_tm);
9854518e 270 if (nla_put_64bit(skb, TCA_NAT_TM, sizeof(t), &t, TCA_NAT_PAD))
1b34ec43 271 goto nla_put_failure;
b4219952 272
b4219952
HX
273 return skb->len;
274
7ba699c6 275nla_put_failure:
b4219952 276 nlmsg_trim(skb, b);
b4219952
HX
277 return -1;
278}
279
ddf97ccd
WC
280static int tcf_nat_walker(struct net *net, struct sk_buff *skb,
281 struct netlink_callback *cb, int type,
a85a970a 282 const struct tc_action_ops *ops)
ddf97ccd
WC
283{
284 struct tc_action_net *tn = net_generic(net, nat_net_id);
285
a85a970a 286 return tcf_generic_walker(tn, skb, cb, type, ops);
ddf97ccd
WC
287}
288
a85a970a 289static int tcf_nat_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccd
WC
290{
291 struct tc_action_net *tn = net_generic(net, nat_net_id);
292
293 return tcf_hash_search(tn, a, index);
294}
295
b4219952
HX
296static struct tc_action_ops act_nat_ops = {
297 .kind = "nat",
b4219952 298 .type = TCA_ACT_NAT,
b4219952
HX
299 .owner = THIS_MODULE,
300 .act = tcf_nat,
301 .dump = tcf_nat_dump,
b4219952 302 .init = tcf_nat_init,
ddf97ccd
WC
303 .walk = tcf_nat_walker,
304 .lookup = tcf_nat_search,
a85a970a 305 .size = sizeof(struct tcf_nat),
ddf97ccd
WC
306};
307
308static __net_init int nat_init_net(struct net *net)
309{
310 struct tc_action_net *tn = net_generic(net, nat_net_id);
311
312 return tc_action_net_init(tn, &act_nat_ops, NAT_TAB_MASK);
313}
314
315static void __net_exit nat_exit_net(struct net *net)
316{
317 struct tc_action_net *tn = net_generic(net, nat_net_id);
318
319 tc_action_net_exit(tn);
320}
321
322static struct pernet_operations nat_net_ops = {
323 .init = nat_init_net,
324 .exit = nat_exit_net,
325 .id = &nat_net_id,
326 .size = sizeof(struct tc_action_net),
b4219952
HX
327};
328
329MODULE_DESCRIPTION("Stateless NAT actions");
330MODULE_LICENSE("GPL");
331
332static int __init nat_init_module(void)
333{
ddf97ccd 334 return tcf_register_action(&act_nat_ops, &nat_net_ops);
b4219952
HX
335}
336
337static void __exit nat_cleanup_module(void)
338{
ddf97ccd 339 tcf_unregister_action(&act_nat_ops, &nat_net_ops);
b4219952
HX
340}
341
342module_init(nat_init_module);
343module_exit(nat_cleanup_module);