~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/netfilter/nf_nat_l3proto_ipv6.c

Version: ~ [ linux-5.13-rc2 ] ~ [ linux-5.12.4 ] ~ [ linux-5.11.21 ] ~ [ linux-5.10.37 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.119 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.190 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.232 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.268 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.268 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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>
 18 #include <net/ip6_checksum.h>
 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 
103         /* must reload, offset might have changed */
104         ipv6h = (void *)skb->data + iphdroff;
105 
106 manip_addr:
107         if (maniptype == NF_NAT_MANIP_SRC)
108                 ipv6h->saddr = target->src.u3.in6;
109         else
110                 ipv6h->daddr = target->dst.u3.in6;
111 
112         return true;
113 }
114 
115 static void nf_nat_ipv6_csum_update(struct sk_buff *skb,
116                                     unsigned int iphdroff, __sum16 *check,
117                                     const struct nf_conntrack_tuple *t,
118                                     enum nf_nat_manip_type maniptype)
119 {
120         const struct ipv6hdr *ipv6h = (struct ipv6hdr *)(skb->data + iphdroff);
121         const struct in6_addr *oldip, *newip;
122 
123         if (maniptype == NF_NAT_MANIP_SRC) {
124                 oldip = &ipv6h->saddr;
125                 newip = &t->src.u3.in6;
126         } else {
127                 oldip = &ipv6h->daddr;
128                 newip = &t->dst.u3.in6;
129         }
130         inet_proto_csum_replace16(check, skb, oldip->s6_addr32,
131                                   newip->s6_addr32, true);
132 }
133 
134 static void nf_nat_ipv6_csum_recalc(struct sk_buff *skb,
135                                     u8 proto, void *data, __sum16 *check,
136                                     int datalen, int oldlen)
137 {
138         if (skb->ip_summed != CHECKSUM_PARTIAL) {
139                 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
140 
141                 skb->ip_summed = CHECKSUM_PARTIAL;
142                 skb->csum_start = skb_headroom(skb) + 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                 inet_proto_csum_replace2(check, skb,
149                                          htons(oldlen), htons(datalen), true);
150 }
151 
152 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
153 static int nf_nat_ipv6_nlattr_to_range(struct nlattr *tb[],
154                                        struct nf_nat_range *range)
155 {
156         if (tb[CTA_NAT_V6_MINIP]) {
157                 nla_memcpy(&range->min_addr.ip6, tb[CTA_NAT_V6_MINIP],
158                            sizeof(struct in6_addr));
159                 range->flags |= NF_NAT_RANGE_MAP_IPS;
160         }
161 
162         if (tb[CTA_NAT_V6_MAXIP])
163                 nla_memcpy(&range->max_addr.ip6, tb[CTA_NAT_V6_MAXIP],
164                            sizeof(struct in6_addr));
165         else
166                 range->max_addr = range->min_addr;
167 
168         return 0;
169 }
170 #endif
171 
172 static const struct nf_nat_l3proto nf_nat_l3proto_ipv6 = {
173         .l3proto                = NFPROTO_IPV6,
174         .secure_port            = nf_nat_ipv6_secure_port,
175         .in_range               = nf_nat_ipv6_in_range,
176         .manip_pkt              = nf_nat_ipv6_manip_pkt,
177         .csum_update            = nf_nat_ipv6_csum_update,
178         .csum_recalc            = nf_nat_ipv6_csum_recalc,
179 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
180         .nlattr_to_range        = nf_nat_ipv6_nlattr_to_range,
181 #endif
182 #ifdef CONFIG_XFRM
183         .decode_session = nf_nat_ipv6_decode_session,
184 #endif
185 };
186 
187 int nf_nat_icmpv6_reply_translation(struct sk_buff *skb,
188                                     struct nf_conn *ct,
189                                     enum ip_conntrack_info ctinfo,
190                                     unsigned int hooknum,
191                                     unsigned int hdrlen)
192 {
193         struct {
194                 struct icmp6hdr icmp6;
195                 struct ipv6hdr  ip6;
196         } *inside;
197         enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
198         enum nf_nat_manip_type manip = HOOK2MANIP(hooknum);
199         const struct nf_nat_l4proto *l4proto;
200         struct nf_conntrack_tuple target;
201         unsigned long statusbit;
202 
203         WARN_ON(ctinfo != IP_CT_RELATED && ctinfo != IP_CT_RELATED_REPLY);
204 
205         if (!skb_make_writable(skb, hdrlen + sizeof(*inside)))
206                 return 0;
207         if (nf_ip6_checksum(skb, hooknum, hdrlen, IPPROTO_ICMPV6))
208                 return 0;
209 
210         inside = (void *)skb->data + hdrlen;
211         if (inside->icmp6.icmp6_type == NDISC_REDIRECT) {
212                 if ((ct->status & IPS_NAT_DONE_MASK) != IPS_NAT_DONE_MASK)
213                         return 0;
214                 if (ct->status & IPS_NAT_MASK)
215                         return 0;
216         }
217 
218         if (manip == NF_NAT_MANIP_SRC)
219                 statusbit = IPS_SRC_NAT;
220         else
221                 statusbit = IPS_DST_NAT;
222 
223         /* Invert if this is reply direction */
224         if (dir == IP_CT_DIR_REPLY)
225                 statusbit ^= IPS_NAT_MASK;
226 
227         if (!(ct->status & statusbit))
228                 return 1;
229 
230         l4proto = __nf_nat_l4proto_find(NFPROTO_IPV6, inside->ip6.nexthdr);
231         if (!nf_nat_ipv6_manip_pkt(skb, hdrlen + sizeof(inside->icmp6),
232                                    l4proto, &ct->tuplehash[!dir].tuple, !manip))
233                 return 0;
234 
235         if (skb->ip_summed != CHECKSUM_PARTIAL) {
236                 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
237                 inside = (void *)skb->data + hdrlen;
238                 inside->icmp6.icmp6_cksum = 0;
239                 inside->icmp6.icmp6_cksum =
240                         csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
241                                         skb->len - hdrlen, IPPROTO_ICMPV6,
242                                         skb_checksum(skb, hdrlen,
243                                                      skb->len - hdrlen, 0));
244         }
245 
246         nf_ct_invert_tuplepr(&target, &ct->tuplehash[!dir].tuple);
247         l4proto = __nf_nat_l4proto_find(NFPROTO_IPV6, IPPROTO_ICMPV6);
248         if (!nf_nat_ipv6_manip_pkt(skb, 0, l4proto, &target, manip))
249                 return 0;
250 
251         return 1;
252 }
253 EXPORT_SYMBOL_GPL(nf_nat_icmpv6_reply_translation);
254 
255 unsigned int
256 nf_nat_ipv6_fn(void *priv, struct sk_buff *skb,
257                const struct nf_hook_state *state,
258                unsigned int (*do_chain)(void *priv,
259                                         struct sk_buff *skb,
260                                         const struct nf_hook_state *state,
261                                         struct nf_conn *ct))
262 {
263         struct nf_conn *ct;
264         enum ip_conntrack_info ctinfo;
265         struct nf_conn_nat *nat;
266         enum nf_nat_manip_type maniptype = HOOK2MANIP(state->hook);
267         __be16 frag_off;
268         int hdrlen;
269         u8 nexthdr;
270 
271         ct = nf_ct_get(skb, &ctinfo);
272         /* Can't track?  It's not due to stress, or conntrack would
273          * have dropped it.  Hence it's the user's responsibilty to
274          * packet filter it out, or implement conntrack/NAT for that
275          * protocol. 8) --RR
276          */
277         if (!ct)
278                 return NF_ACCEPT;
279 
280         nat = nfct_nat(ct);
281 
282         switch (ctinfo) {
283         case IP_CT_RELATED:
284         case IP_CT_RELATED_REPLY:
285                 nexthdr = ipv6_hdr(skb)->nexthdr;
286                 hdrlen = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
287                                           &nexthdr, &frag_off);
288 
289                 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
290                         if (!nf_nat_icmpv6_reply_translation(skb, ct, ctinfo,
291                                                              state->hook,
292                                                              hdrlen))
293                                 return NF_DROP;
294                         else
295                                 return NF_ACCEPT;
296                 }
297                 /* Only ICMPs can be IP_CT_IS_REPLY: */
298                 /* fall through */
299         case IP_CT_NEW:
300                 /* Seen it before?  This can happen for loopback, retrans,
301                  * or local packets.
302                  */
303                 if (!nf_nat_initialized(ct, maniptype)) {
304                         unsigned int ret;
305 
306                         ret = do_chain(priv, skb, state, ct);
307                         if (ret != NF_ACCEPT)
308                                 return ret;
309 
310                         if (nf_nat_initialized(ct, HOOK2MANIP(state->hook)))
311                                 break;
312 
313                         ret = nf_nat_alloc_null_binding(ct, state->hook);
314                         if (ret != NF_ACCEPT)
315                                 return ret;
316                 } else {
317                         pr_debug("Already setup manip %s for ct %p\n",
318                                  maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
319                                  ct);
320                         if (nf_nat_oif_changed(state->hook, ctinfo, nat, state->out))
321                                 goto oif_changed;
322                 }
323                 break;
324 
325         default:
326                 /* ESTABLISHED */
327                 WARN_ON(ctinfo != IP_CT_ESTABLISHED &&
328                         ctinfo != IP_CT_ESTABLISHED_REPLY);
329                 if (nf_nat_oif_changed(state->hook, ctinfo, nat, state->out))
330                         goto oif_changed;
331         }
332 
333         return nf_nat_packet(ct, ctinfo, state->hook, skb);
334 
335 oif_changed:
336         nf_ct_kill_acct(ct, ctinfo, skb);
337         return NF_DROP;
338 }
339 EXPORT_SYMBOL_GPL(nf_nat_ipv6_fn);
340 
341 unsigned int
342 nf_nat_ipv6_in(void *priv, struct sk_buff *skb,
343                const struct nf_hook_state *state,
344                unsigned int (*do_chain)(void *priv,
345                                         struct sk_buff *skb,
346                                         const struct nf_hook_state *state,
347                                         struct nf_conn *ct))
348 {
349         unsigned int ret;
350         struct in6_addr daddr = ipv6_hdr(skb)->daddr;
351 
352         ret = nf_nat_ipv6_fn(priv, skb, state, do_chain);
353         if (ret != NF_DROP && ret != NF_STOLEN &&
354             ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr))
355                 skb_dst_drop(skb);
356 
357         return ret;
358 }
359 EXPORT_SYMBOL_GPL(nf_nat_ipv6_in);
360 
361 unsigned int
362 nf_nat_ipv6_out(void *priv, struct sk_buff *skb,
363                 const struct nf_hook_state *state,
364                 unsigned int (*do_chain)(void *priv,
365                                          struct sk_buff *skb,
366                                          const struct nf_hook_state *state,
367                                          struct nf_conn *ct))
368 {
369 #ifdef CONFIG_XFRM
370         const struct nf_conn *ct;
371         enum ip_conntrack_info ctinfo;
372         int err;
373 #endif
374         unsigned int ret;
375 
376         /* root is playing with raw sockets. */
377         if (skb->len < sizeof(struct ipv6hdr))
378                 return NF_ACCEPT;
379 
380         ret = nf_nat_ipv6_fn(priv, skb, state, do_chain);
381 #ifdef CONFIG_XFRM
382         if (ret != NF_DROP && ret != NF_STOLEN &&
383             !(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
384             (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
385                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
386 
387                 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3,
388                                       &ct->tuplehash[!dir].tuple.dst.u3) ||
389                     (ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 &&
390                      ct->tuplehash[dir].tuple.src.u.all !=
391                      ct->tuplehash[!dir].tuple.dst.u.all)) {
392                         err = nf_xfrm_me_harder(state->net, skb, AF_INET6);
393                         if (err < 0)
394                                 ret = NF_DROP_ERR(err);
395                 }
396         }
397 #endif
398         return ret;
399 }
400 EXPORT_SYMBOL_GPL(nf_nat_ipv6_out);
401 
402 unsigned int
403 nf_nat_ipv6_local_fn(void *priv, struct sk_buff *skb,
404                      const struct nf_hook_state *state,
405                      unsigned int (*do_chain)(void *priv,
406                                               struct sk_buff *skb,
407                                               const struct nf_hook_state *state,
408                                               struct nf_conn *ct))
409 {
410         const struct nf_conn *ct;
411         enum ip_conntrack_info ctinfo;
412         unsigned int ret;
413         int err;
414 
415         /* root is playing with raw sockets. */
416         if (skb->len < sizeof(struct ipv6hdr))
417                 return NF_ACCEPT;
418 
419         ret = nf_nat_ipv6_fn(priv, skb, state, do_chain);
420         if (ret != NF_DROP && ret != NF_STOLEN &&
421             (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
422                 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
423 
424                 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3,
425                                       &ct->tuplehash[!dir].tuple.src.u3)) {
426                         err = ip6_route_me_harder(state->net, skb);
427                         if (err < 0)
428                                 ret = NF_DROP_ERR(err);
429                 }
430 #ifdef CONFIG_XFRM
431                 else if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
432                          ct->tuplehash[dir].tuple.dst.protonum != IPPROTO_ICMPV6 &&
433                          ct->tuplehash[dir].tuple.dst.u.all !=
434                          ct->tuplehash[!dir].tuple.src.u.all) {
435                         err = nf_xfrm_me_harder(state->net, skb, AF_INET6);
436                         if (err < 0)
437                                 ret = NF_DROP_ERR(err);
438                 }
439 #endif
440         }
441         return ret;
442 }
443 EXPORT_SYMBOL_GPL(nf_nat_ipv6_local_fn);
444 
445 static int __init nf_nat_l3proto_ipv6_init(void)
446 {
447         int err;
448 
449         err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
450         if (err < 0)
451                 goto err1;
452         err = nf_nat_l3proto_register(&nf_nat_l3proto_ipv6);
453         if (err < 0)
454                 goto err2;
455         return err;
456 
457 err2:
458         nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
459 err1:
460         return err;
461 }
462 
463 static void __exit nf_nat_l3proto_ipv6_exit(void)
464 {
465         nf_nat_l3proto_unregister(&nf_nat_l3proto_ipv6);
466         nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_icmpv6);
467 }
468 
469 MODULE_LICENSE("GPL");
470 MODULE_ALIAS("nf-nat-" __stringify(AF_INET6));
471 
472 module_init(nf_nat_l3proto_ipv6_init);
473 module_exit(nf_nat_l3proto_ipv6_exit);
474 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp