Commit | Line | Data |
---|---|---|
58a317f1 PM |
1 | /* |
2 | * Copyright (c) 2011 Patrick McHardy <kaber@trash.net> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * Development of IPv6 NAT funded by Astaro. | |
9 | */ | |
10 | #include <linux/types.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/skbuff.h> | |
13 | #include <linux/ipv6.h> | |
14 | #include <linux/netfilter.h> | |
15 | #include <linux/netfilter_ipv6.h> | |
16 | #include <net/secure_seq.h> | |
17 | #include <net/checksum.h> | |
2c969322 | 18 | #include <net/ip6_checksum.h> |
58a317f1 PM |
19 | #include <net/ip6_route.h> |
20 | #include <net/ipv6.h> | |
21 | ||
22 | #include <net/netfilter/nf_conntrack_core.h> | |
23 | #include <net/netfilter/nf_conntrack.h> | |
24 | #include <net/netfilter/nf_nat_core.h> | |
25 | #include <net/netfilter/nf_nat_l3proto.h> | |
26 | #include <net/netfilter/nf_nat_l4proto.h> | |
27 | ||
28 | static const struct nf_nat_l3proto nf_nat_l3proto_ipv6; | |
29 | ||
30 | #ifdef CONFIG_XFRM | |
31 | static void nf_nat_ipv6_decode_session(struct sk_buff *skb, | |
32 | const struct nf_conn *ct, | |
33 | enum ip_conntrack_dir dir, | |
34 | unsigned long statusbit, | |
35 | struct flowi *fl) | |
36 | { | |
37 | const struct nf_conntrack_tuple *t = &ct->tuplehash[dir].tuple; | |
38 | struct flowi6 *fl6 = &fl->u.ip6; | |
39 | ||
40 | if (ct->status & statusbit) { | |
41 | fl6->daddr = t->dst.u3.in6; | |
42 | if (t->dst.protonum == IPPROTO_TCP || | |
43 | t->dst.protonum == IPPROTO_UDP || | |
44 | t->dst.protonum == IPPROTO_UDPLITE || | |
45 | t->dst.protonum == IPPROTO_DCCP || | |
46 | t->dst.protonum == IPPROTO_SCTP) | |
47 | fl6->fl6_dport = t->dst.u.all; | |
48 | } | |
49 | ||
50 | statusbit ^= IPS_NAT_MASK; | |
51 | ||
52 | if (ct->status & statusbit) { | |
53 | fl6->saddr = t->src.u3.in6; | |
54 | if (t->dst.protonum == IPPROTO_TCP || | |
55 | t->dst.protonum == IPPROTO_UDP || | |
56 | t->dst.protonum == IPPROTO_UDPLITE || | |
57 | t->dst.protonum == IPPROTO_DCCP || | |
58 | t->dst.protonum == IPPROTO_SCTP) | |
59 | fl6->fl6_sport = t->src.u.all; | |
60 | } | |
61 | } | |
62 | #endif | |
63 | ||
64 | static bool nf_nat_ipv6_in_range(const struct nf_conntrack_tuple *t, | |
65 | const struct nf_nat_range *range) | |
66 | { | |
67 | return ipv6_addr_cmp(&t->src.u3.in6, &range->min_addr.in6) >= 0 && | |
68 | ipv6_addr_cmp(&t->src.u3.in6, &range->max_addr.in6) <= 0; | |
69 | } | |
70 | ||
71 | static u32 nf_nat_ipv6_secure_port(const struct nf_conntrack_tuple *t, | |
72 | __be16 dport) | |
73 | { | |
74 | return secure_ipv6_port_ephemeral(t->src.u3.ip6, t->dst.u3.ip6, dport); | |
75 | } | |
76 | ||
77 | static bool nf_nat_ipv6_manip_pkt(struct sk_buff *skb, | |
78 | unsigned int iphdroff, | |
79 | const struct nf_nat_l4proto *l4proto, | |
80 | const struct nf_conntrack_tuple *target, | |
81 | enum nf_nat_manip_type maniptype) | |
82 | { | |
83 | struct ipv6hdr *ipv6h; | |
84 | __be16 frag_off; | |
85 | int hdroff; | |
86 | u8 nexthdr; | |
87 | ||
88 | if (!skb_make_writable(skb, iphdroff + sizeof(*ipv6h))) | |
89 | return false; | |
90 | ||
91 | ipv6h = (void *)skb->data + iphdroff; | |
92 | nexthdr = ipv6h->nexthdr; | |
93 | hdroff = ipv6_skip_exthdr(skb, iphdroff + sizeof(*ipv6h), | |
94 | &nexthdr, &frag_off); | |
95 | if (hdroff < 0) | |
96 | goto manip_addr; | |
97 | ||
98 | if ((frag_off & htons(~0x7)) == 0 && | |
99 | !l4proto->manip_pkt(skb, &nf_nat_l3proto_ipv6, iphdroff, hdroff, | |
100 | target, maniptype)) | |
101 | return false; | |
102 | manip_addr: | |
103 | if (maniptype == NF_NAT_MANIP_SRC) | |
104 | ipv6h->saddr = target->src.u3.in6; | |
105 | else | |
106 | ipv6h->daddr = target->dst.u3.in6; | |
107 | ||
108 | return true; | |
109 | } | |
110 | ||
111 | static void nf_nat_ipv6_csum_update(struct sk_buff *skb, | |
112 | unsigned int iphdroff, __sum16 *check, | |
113 | const struct nf_conntrack_tuple *t, | |
114 | enum nf_nat_manip_type maniptype) | |
115 | { | |
116 | const struct ipv6hdr *ipv6h = (struct ipv6hdr *)(skb->data + iphdroff); | |
117 | const struct in6_addr *oldip, *newip; | |
118 | ||
119 | if (maniptype == NF_NAT_MANIP_SRC) { | |
120 | oldip = &ipv6h->saddr; | |
121 | newip = &t->src.u3.in6; | |
122 | } else { | |
123 | oldip = &ipv6h->daddr; | |
124 | newip = &t->dst.u3.in6; | |
125 | } | |
126 | inet_proto_csum_replace16(check, skb, oldip->s6_addr32, | |
127 | newip->s6_addr32, 1); | |
128 | } | |
129 | ||
130 | static void nf_nat_ipv6_csum_recalc(struct sk_buff *skb, | |
131 | u8 proto, void *data, __sum16 *check, | |
132 | int datalen, int oldlen) | |
133 | { | |
134 | const struct ipv6hdr *ipv6h = ipv6_hdr(skb); | |
135 | struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); | |
136 | ||
137 | if (skb->ip_summed != CHECKSUM_PARTIAL) { | |
138 | if (!(rt->rt6i_flags & RTF_LOCAL) && | |
139 | (!skb->dev || skb->dev->features & NETIF_F_V6_CSUM)) { | |
140 | skb->ip_summed = CHECKSUM_PARTIAL; | |
141 | skb->csum_start = skb_headroom(skb) + | |
142 | skb_network_offset(skb) + | |
143 | (data - (void *)skb->data); | |
144 | skb->csum_offset = (void *)check - data; | |
145 | *check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, | |
146 | datalen, proto, 0); | |
147 | } else { | |
148 | *check = 0; | |
149 | *check = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, | |
150 | datalen, proto, | |
151 | csum_partial(data, datalen, | |
152 | 0)); | |
153 | if (proto == IPPROTO_UDP && !*check) | |
154 | *check = CSUM_MANGLED_0; | |
155 | } | |
156 | } else | |
157 | inet_proto_csum_replace2(check, skb, | |
158 | htons(oldlen), htons(datalen), 1); | |
159 | } | |
160 | ||
24de3d37 | 161 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
58a317f1 PM |
162 | static int nf_nat_ipv6_nlattr_to_range(struct nlattr *tb[], |
163 | struct nf_nat_range *range) | |
164 | { | |
165 | if (tb[CTA_NAT_V6_MINIP]) { | |
166 | nla_memcpy(&range->min_addr.ip6, tb[CTA_NAT_V6_MINIP], | |
167 | sizeof(struct in6_addr)); | |
168 | range->flags |= NF_NAT_RANGE_MAP_IPS; | |
169 | } | |
170 | ||
171 | if (tb[CTA_NAT_V6_MAXIP]) | |
172 | nla_memcpy(&range->max_addr.ip6, tb[CTA_NAT_V6_MAXIP], | |
173 | sizeof(struct in6_addr)); | |
174 | else | |
175 | range->max_addr = range->min_addr; | |
176 | ||
177 | return 0; | |
178 | } | |
24de3d37 | 179 | #endif |
58a317f1 PM |
180 | |
181 | static const struct nf_nat_l3proto nf_nat_l3proto_ipv6 = { | |
182 | .l3proto = NFPROTO_IPV6, | |
183 | .secure_port = nf_nat_ipv6_secure_port, | |
184 | .in_range = nf_nat_ipv6_in_range, | |
185 | .manip_pkt = nf_nat_ipv6_manip_pkt, | |
186 | .csum_update = nf_nat_ipv6_csum_update, | |
187 | .csum_recalc = nf_nat_ipv6_csum_recalc, | |
24de3d37 | 188 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
58a317f1 | 189 | .nlattr_to_range = nf_nat_ipv6_nlattr_to_range, |
24de3d37 | 190 | #endif |
58a317f1 PM |
191 | #ifdef CONFIG_XFRM |
192 | .decode_session = nf_nat_ipv6_decode_session, | |
193 | #endif | |
194 | }; | |
195 | ||
196 | int nf_nat_icmpv6_reply_translation(struct sk_buff *skb, | |
197 | struct nf_conn *ct, | |
198 | enum ip_conntrack_info ctinfo, | |
199 | unsigned int hooknum, | |
200 | unsigned int hdrlen) | |
201 | { | |
202 | struct { | |
203 | struct icmp6hdr icmp6; | |
204 | struct ipv6hdr ip6; | |
205 | } *inside; | |
206 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); | |
207 | enum nf_nat_manip_type manip = HOOK2MANIP(hooknum); | |
208 | const struct nf_nat_l4proto *l4proto; | |
209 | struct nf_conntrack_tuple target; | |
210 | unsigned long statusbit; | |
211 | ||
212 | NF_CT_ASSERT(ctinfo == IP_CT_RELATED || ctinfo == IP_CT_RELATED_REPLY); | |
213 | ||
214 | if (!skb_make_writable(skb, hdrlen + sizeof(*inside))) | |
215 | return 0; | |
216 | if (nf_ip6_checksum(skb, hooknum, hdrlen, IPPROTO_ICMPV6)) | |
217 | return 0; | |
218 | ||
219 | inside = (void *)skb->data + hdrlen; | |
220 | if (inside->icmp6.icmp6_type == NDISC_REDIRECT) { | |
221 | if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK) | |
222 | return 0; | |
223 | if (ct->status & IPS_NAT_MASK) | |
224 | return 0; | |
225 | } | |
226 | ||
227 | if (manip == NF_NAT_MANIP_SRC) | |
228 | statusbit = IPS_SRC_NAT; | |
229 | else | |
230 | statusbit = IPS_DST_NAT; | |
231 | ||
232 | /* Invert if this is reply direction */ | |
233 | if (dir == IP_CT_DIR_REPLY) | |
234 | statusbit ^= IPS_NAT_MASK; | |
235 | ||
236 | if (!(ct->status & statusbit)) | |
237 | return 1; | |
238 | ||
239 | l4proto = __nf_nat_l4proto_find(NFPROTO_IPV6, inside->ip6.nexthdr); | |
240 | if (!nf_nat_ipv6_manip_pkt(skb, hdrlen + sizeof(inside->icmp6), | |
241 | l4proto, &ct->tuplehash[!dir].tuple, !manip)) | |
242 | return 0; | |
243 | ||
244 | if (skb->ip_summed != CHECKSUM_PARTIAL) { | |
245 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); | |
246 | inside = (void *)skb->data + hdrlen; | |
247 | inside->icmp6.icmp6_cksum = 0; | |
248 | inside->icmp6.icmp6_cksum = | |
249 | csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, | |
250 | skb->len - hdrlen, IPPROTO_ICMPV6, | |
251 | csum_partial(&inside->icmp6, | |
252 | skb->len - hdrlen, 0)); | |
253 | } | |
254 | ||
255 | nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple); | |
256 | l4proto = __nf_nat_l4proto_find(NFPROTO_IPV6, IPPROTO_ICMPV6); | |
257 | if (!nf_nat_ipv6_manip_pkt(skb, 0, l4proto, &target, manip)) | |
258 | return 0; | |
259 | ||
260 | return 1; | |
261 | } | |
262 | EXPORT_SYMBOL_GPL(nf_nat_icmpv6_reply_translation); | |
263 | ||
2a5538e9 PNA |
264 | unsigned int |
265 | nf_nat_ipv6_fn(const struct nf_hook_ops *ops, struct sk_buff *skb, | |
266 | const struct net_device *in, const struct net_device *out, | |
267 | unsigned int (*do_chain)(const struct nf_hook_ops *ops, | |
268 | struct sk_buff *skb, | |
269 | const struct net_device *in, | |
270 | const struct net_device *out, | |
271 | struct nf_conn *ct)) | |
272 | { | |
273 | struct nf_conn *ct; | |
274 | enum ip_conntrack_info ctinfo; | |
275 | struct nf_conn_nat *nat; | |
276 | enum nf_nat_manip_type maniptype = HOOK2MANIP(ops->hooknum); | |
277 | __be16 frag_off; | |
278 | int hdrlen; | |
279 | u8 nexthdr; | |
280 | ||
281 | ct = nf_ct_get(skb, &ctinfo); | |
282 | /* Can't track? It's not due to stress, or conntrack would | |
283 | * have dropped it. Hence it's the user's responsibilty to | |
284 | * packet filter it out, or implement conntrack/NAT for that | |
285 | * protocol. 8) --RR | |
286 | */ | |
287 | if (!ct) | |
288 | return NF_ACCEPT; | |
289 | ||
290 | /* Don't try to NAT if this packet is not conntracked */ | |
291 | if (nf_ct_is_untracked(ct)) | |
292 | return NF_ACCEPT; | |
293 | ||
294 | nat = nf_ct_nat_ext_add(ct); | |
295 | if (nat == NULL) | |
296 | return NF_ACCEPT; | |
297 | ||
298 | switch (ctinfo) { | |
299 | case IP_CT_RELATED: | |
300 | case IP_CT_RELATED_REPLY: | |
301 | nexthdr = ipv6_hdr(skb)->nexthdr; | |
302 | hdrlen = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), | |
303 | &nexthdr, &frag_off); | |
304 | ||
305 | if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) { | |
306 | if (!nf_nat_icmpv6_reply_translation(skb, ct, ctinfo, | |
307 | ops->hooknum, | |
308 | hdrlen)) | |
309 | return NF_DROP; | |
310 | else | |
311 | return NF_ACCEPT; | |
312 | } | |
313 | /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */ | |
314 | case IP_CT_NEW: | |
315 | /* Seen it before? This can happen for loopback, retrans, | |
316 | * or local packets. | |
317 | */ | |
318 | if (!nf_nat_initialized(ct, maniptype)) { | |
319 | unsigned int ret; | |
320 | ||
321 | ret = do_chain(ops, skb, in, out, ct); | |
322 | if (ret != NF_ACCEPT) | |
323 | return ret; | |
324 | ||
325 | if (nf_nat_initialized(ct, HOOK2MANIP(ops->hooknum))) | |
326 | break; | |
327 | ||
328 | ret = nf_nat_alloc_null_binding(ct, ops->hooknum); | |
329 | if (ret != NF_ACCEPT) | |
330 | return ret; | |
331 | } else { | |
332 | pr_debug("Already setup manip %s for ct %p\n", | |
333 | maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST", | |
334 | ct); | |
335 | if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out)) | |
336 | goto oif_changed; | |
337 | } | |
338 | break; | |
339 | ||
340 | default: | |
341 | /* ESTABLISHED */ | |
342 | NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED || | |
343 | ctinfo == IP_CT_ESTABLISHED_REPLY); | |
344 | if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out)) | |
345 | goto oif_changed; | |
346 | } | |
347 | ||
348 | return nf_nat_packet(ct, ctinfo, ops->hooknum, skb); | |
349 | ||
350 | oif_changed: | |
351 | nf_ct_kill_acct(ct, ctinfo, skb); | |
352 | return NF_DROP; | |
353 | } | |
354 | EXPORT_SYMBOL_GPL(nf_nat_ipv6_fn); | |
355 | ||
356 | unsigned int | |
357 | nf_nat_ipv6_in(const struct nf_hook_ops *ops, struct sk_buff *skb, | |
358 | const struct net_device *in, const struct net_device *out, | |
359 | unsigned int (*do_chain)(const struct nf_hook_ops *ops, | |
360 | struct sk_buff *skb, | |
361 | const struct net_device *in, | |
362 | const struct net_device *out, | |
363 | struct nf_conn *ct)) | |
364 | { | |
365 | unsigned int ret; | |
366 | struct in6_addr daddr = ipv6_hdr(skb)->daddr; | |
367 | ||
368 | ret = nf_nat_ipv6_fn(ops, skb, in, out, do_chain); | |
369 | if (ret != NF_DROP && ret != NF_STOLEN && | |
370 | ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr)) | |
371 | skb_dst_drop(skb); | |
372 | ||
373 | return ret; | |
374 | } | |
375 | EXPORT_SYMBOL_GPL(nf_nat_ipv6_in); | |
376 | ||
377 | unsigned int | |
378 | nf_nat_ipv6_out(const struct nf_hook_ops *ops, struct sk_buff *skb, | |
379 | const struct net_device *in, const struct net_device *out, | |
380 | unsigned int (*do_chain)(const struct nf_hook_ops *ops, | |
381 | struct sk_buff *skb, | |
382 | const struct net_device *in, | |
383 | const struct net_device *out, | |
384 | struct nf_conn *ct)) | |
385 | { | |
386 | #ifdef CONFIG_XFRM | |
387 | const struct nf_conn *ct; | |
388 | enum ip_conntrack_info ctinfo; | |
389 | int err; | |
390 | #endif | |
391 | unsigned int ret; | |
392 | ||
393 | /* root is playing with raw sockets. */ | |
394 | if (skb->len < sizeof(struct ipv6hdr)) | |
395 | return NF_ACCEPT; | |
396 | ||
397 | ret = nf_nat_ipv6_fn(ops, skb, in, out, do_chain); | |
398 | #ifdef CONFIG_XFRM | |
399 | if (ret != NF_DROP && ret != NF_STOLEN && | |
400 | !(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) && | |
401 | (ct = nf_ct_get(skb, &ctinfo)) != NULL) { | |
402 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); | |
403 | ||
404 | if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3, | |
405 | &ct->tuplehash[!dir].tuple.dst.u3) || | |
406 | (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 && | |
407 | ct->tuplehash[dir].tuple.src.u.all != | |
408 | ct->tuplehash[!dir].tuple.dst.u.all)) { | |
409 | err = nf_xfrm_me_harder(skb, AF_INET6); | |
410 | if (err < 0) | |
411 | ret = NF_DROP_ERR(err); | |
412 | } | |
413 | } | |
414 | #endif | |
415 | return ret; | |
416 | } | |
417 | EXPORT_SYMBOL_GPL(nf_nat_ipv6_out); | |
418 | ||
419 | unsigned int | |
420 | nf_nat_ipv6_local_fn(const struct nf_hook_ops *ops, struct sk_buff *skb, | |
421 | const struct net_device *in, const struct net_device *out, | |
422 | unsigned int (*do_chain)(const struct nf_hook_ops *ops, | |
423 | struct sk_buff *skb, | |
424 | const struct net_device *in, | |
425 | const struct net_device *out, | |
426 | struct nf_conn *ct)) | |
427 | { | |
428 | const struct nf_conn *ct; | |
429 | enum ip_conntrack_info ctinfo; | |
430 | unsigned int ret; | |
431 | int err; | |
432 | ||
433 | /* root is playing with raw sockets. */ | |
434 | if (skb->len < sizeof(struct ipv6hdr)) | |
435 | return NF_ACCEPT; | |
436 | ||
437 | ret = nf_nat_ipv6_fn(ops, skb, in, out, do_chain); | |
438 | if (ret != NF_DROP && ret != NF_STOLEN && | |
439 | (ct = nf_ct_get(skb, &ctinfo)) != NULL) { | |
440 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); | |
441 | ||
442 | if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3, | |
443 | &ct->tuplehash[!dir].tuple.src.u3)) { | |
444 | err = ip6_route_me_harder(skb); | |
445 | if (err < 0) | |
446 | ret = NF_DROP_ERR(err); | |
447 | } | |
448 | #ifdef CONFIG_XFRM | |
449 | else if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) && | |
450 | ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 && | |
451 | ct->tuplehash[dir].tuple.dst.u.all != | |
452 | ct->tuplehash[!dir].tuple.src.u.all) { | |
453 | err = nf_xfrm_me_harder(skb, AF_INET6); | |
454 | if (err < 0) | |
455 | ret = NF_DROP_ERR(err); | |
456 | } | |
457 | #endif | |
458 | } | |
459 | return ret; | |
460 | } | |
461 | EXPORT_SYMBOL_GPL(nf_nat_ipv6_local_fn); | |
462 | ||
58a317f1 PM |
463 | static int __init nf_nat_l3proto_ipv6_init(void) |
464 | { | |
465 | int err; | |
466 | ||
467 | err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6); | |
468 | if (err < 0) | |
469 | goto err1; | |
470 | err = nf_nat_l3proto_register(&nf_nat_l3proto_ipv6); | |
471 | if (err < 0) | |
472 | goto err2; | |
473 | return err; | |
474 | ||
475 | err2: | |
476 | nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6); | |
477 | err1: | |
478 | return err; | |
479 | } | |
480 | ||
481 | static void __exit nf_nat_l3proto_ipv6_exit(void) | |
482 | { | |
483 | nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv6); | |
484 | nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6); | |
485 | } | |
486 | ||
487 | MODULE_LICENSE("GPL"); | |
488 | MODULE_ALIAS("nf-nat-" __stringify(AF_INET6)); | |
489 | ||
490 | module_init(nf_nat_l3proto_ipv6_init); | |
491 | module_exit(nf_nat_l3proto_ipv6_exit); |