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