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