]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
Merge remote-tracking branch 'net-next/master'
[karo-tx-linux.git] / net / ipv6 / netfilter / nf_conntrack_l3proto_ipv6.c
1 /*
2  * Copyright (C)2004 USAGI/WIDE Project
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  * Author:
9  *      Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
10  */
11
12 #include <linux/types.h>
13 #include <linux/ipv6.h>
14 #include <linux/in6.h>
15 #include <linux/netfilter.h>
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/icmp.h>
19 #include <net/ipv6.h>
20 #include <net/inet_frag.h>
21
22 #include <linux/netfilter_bridge.h>
23 #include <linux/netfilter_ipv6.h>
24 #include <linux/netfilter_ipv6/ip6_tables.h>
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_helper.h>
27 #include <net/netfilter/nf_conntrack_l4proto.h>
28 #include <net/netfilter/nf_conntrack_l3proto.h>
29 #include <net/netfilter/nf_conntrack_core.h>
30 #include <net/netfilter/nf_conntrack_zones.h>
31 #include <net/netfilter/nf_conntrack_seqadj.h>
32 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
33 #include <net/netfilter/nf_nat_helper.h>
34 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
35 #include <net/netfilter/nf_log.h>
36
37 static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
38                               struct nf_conntrack_tuple *tuple)
39 {
40         const u_int32_t *ap;
41         u_int32_t _addrs[8];
42
43         ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
44                                 sizeof(_addrs), _addrs);
45         if (ap == NULL)
46                 return false;
47
48         memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
49         memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
50
51         return true;
52 }
53
54 static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
55                               const struct nf_conntrack_tuple *orig)
56 {
57         memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
58         memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
59
60         return true;
61 }
62
63 static int ipv6_print_tuple(struct seq_file *s,
64                             const struct nf_conntrack_tuple *tuple)
65 {
66         return seq_printf(s, "src=%pI6 dst=%pI6 ",
67                           tuple->src.u3.ip6, tuple->dst.u3.ip6);
68 }
69
70 static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
71                             unsigned int *dataoff, u_int8_t *protonum)
72 {
73         unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
74         __be16 frag_off;
75         int protoff;
76         u8 nexthdr;
77
78         if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
79                           &nexthdr, sizeof(nexthdr)) != 0) {
80                 pr_debug("ip6_conntrack_core: can't get nexthdr\n");
81                 return -NF_ACCEPT;
82         }
83         protoff = ipv6_skip_exthdr(skb, extoff, &nexthdr, &frag_off);
84         /*
85          * (protoff == skb->len) means the packet has not data, just
86          * IPv6 and possibly extensions headers, but it is tracked anyway
87          */
88         if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
89                 pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
90                 return -NF_ACCEPT;
91         }
92
93         *dataoff = protoff;
94         *protonum = nexthdr;
95         return NF_ACCEPT;
96 }
97
98 static unsigned int ipv6_helper(const struct nf_hook_ops *ops,
99                                 struct sk_buff *skb,
100                                 const struct net_device *in,
101                                 const struct net_device *out,
102                                 int (*okfn)(struct sk_buff *))
103 {
104         struct nf_conn *ct;
105         const struct nf_conn_help *help;
106         const struct nf_conntrack_helper *helper;
107         enum ip_conntrack_info ctinfo;
108         __be16 frag_off;
109         int protoff;
110         u8 nexthdr;
111
112         /* This is where we call the helper: as the packet goes out. */
113         ct = nf_ct_get(skb, &ctinfo);
114         if (!ct || ctinfo == IP_CT_RELATED_REPLY)
115                 return NF_ACCEPT;
116
117         help = nfct_help(ct);
118         if (!help)
119                 return NF_ACCEPT;
120         /* rcu_read_lock()ed by nf_hook_slow */
121         helper = rcu_dereference(help->helper);
122         if (!helper)
123                 return NF_ACCEPT;
124
125         nexthdr = ipv6_hdr(skb)->nexthdr;
126         protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
127                                    &frag_off);
128         if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
129                 pr_debug("proto header not found\n");
130                 return NF_ACCEPT;
131         }
132
133         return helper->help(skb, protoff, ct, ctinfo);
134 }
135
136 static unsigned int ipv6_confirm(const struct nf_hook_ops *ops,
137                                  struct sk_buff *skb,
138                                  const struct net_device *in,
139                                  const struct net_device *out,
140                                  int (*okfn)(struct sk_buff *))
141 {
142         struct nf_conn *ct;
143         enum ip_conntrack_info ctinfo;
144         unsigned char pnum = ipv6_hdr(skb)->nexthdr;
145         int protoff;
146         __be16 frag_off;
147
148         ct = nf_ct_get(skb, &ctinfo);
149         if (!ct || ctinfo == IP_CT_RELATED_REPLY)
150                 goto out;
151
152         protoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &pnum,
153                                    &frag_off);
154         if (protoff < 0 || (frag_off & htons(~0x7)) != 0) {
155                 pr_debug("proto header not found\n");
156                 goto out;
157         }
158
159         /* adjust seqs for loopback traffic only in outgoing direction */
160         if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
161             !nf_is_loopback_packet(skb)) {
162                 if (!nf_ct_seq_adjust(skb, ct, ctinfo, protoff)) {
163                         NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop);
164                         return NF_DROP;
165                 }
166         }
167 out:
168         /* We've seen it coming out the other side: confirm it */
169         return nf_conntrack_confirm(skb);
170 }
171
172 static unsigned int __ipv6_conntrack_in(struct net *net,
173                                         unsigned int hooknum,
174                                         struct sk_buff *skb,
175                                         const struct net_device *in,
176                                         const struct net_device *out,
177                                         int (*okfn)(struct sk_buff *))
178 {
179         struct sk_buff *reasm = skb->nfct_reasm;
180         const struct nf_conn_help *help;
181         struct nf_conn *ct;
182         enum ip_conntrack_info ctinfo;
183
184         /* This packet is fragmented and has reassembled packet. */
185         if (reasm) {
186                 /* Reassembled packet isn't parsed yet ? */
187                 if (!reasm->nfct) {
188                         unsigned int ret;
189
190                         ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
191                         if (ret != NF_ACCEPT)
192                                 return ret;
193                 }
194
195                 /* Conntrack helpers need the entire reassembled packet in the
196                  * POST_ROUTING hook. In case of unconfirmed connections NAT
197                  * might reassign a helper, so the entire packet is also
198                  * required.
199                  */
200                 ct = nf_ct_get(reasm, &ctinfo);
201                 if (ct != NULL && !nf_ct_is_untracked(ct)) {
202                         help = nfct_help(ct);
203                         if ((help && help->helper) || !nf_ct_is_confirmed(ct)) {
204                                 nf_conntrack_get_reasm(reasm);
205                                 NF_HOOK_THRESH(NFPROTO_IPV6, hooknum, reasm,
206                                                (struct net_device *)in,
207                                                (struct net_device *)out,
208                                                okfn, NF_IP6_PRI_CONNTRACK + 1);
209                                 return NF_DROP_ERR(-ECANCELED);
210                         }
211                 }
212
213                 nf_conntrack_get(reasm->nfct);
214                 skb->nfct = reasm->nfct;
215                 skb->nfctinfo = reasm->nfctinfo;
216                 return NF_ACCEPT;
217         }
218
219         return nf_conntrack_in(net, PF_INET6, hooknum, skb);
220 }
221
222 static unsigned int ipv6_conntrack_in(const struct nf_hook_ops *ops,
223                                       struct sk_buff *skb,
224                                       const struct net_device *in,
225                                       const struct net_device *out,
226                                       int (*okfn)(struct sk_buff *))
227 {
228         return __ipv6_conntrack_in(dev_net(in), ops->hooknum, skb, in, out,
229                                    okfn);
230 }
231
232 static unsigned int ipv6_conntrack_local(const struct nf_hook_ops *ops,
233                                          struct sk_buff *skb,
234                                          const struct net_device *in,
235                                          const struct net_device *out,
236                                          int (*okfn)(struct sk_buff *))
237 {
238         /* root is playing with raw sockets. */
239         if (skb->len < sizeof(struct ipv6hdr)) {
240                 net_notice_ratelimited("ipv6_conntrack_local: packet too short\n");
241                 return NF_ACCEPT;
242         }
243         return __ipv6_conntrack_in(dev_net(out), ops->hooknum, skb, in, out,
244                                    okfn);
245 }
246
247 static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
248         {
249                 .hook           = ipv6_conntrack_in,
250                 .owner          = THIS_MODULE,
251                 .pf             = NFPROTO_IPV6,
252                 .hooknum        = NF_INET_PRE_ROUTING,
253                 .priority       = NF_IP6_PRI_CONNTRACK,
254         },
255         {
256                 .hook           = ipv6_conntrack_local,
257                 .owner          = THIS_MODULE,
258                 .pf             = NFPROTO_IPV6,
259                 .hooknum        = NF_INET_LOCAL_OUT,
260                 .priority       = NF_IP6_PRI_CONNTRACK,
261         },
262         {
263                 .hook           = ipv6_helper,
264                 .owner          = THIS_MODULE,
265                 .pf             = NFPROTO_IPV6,
266                 .hooknum        = NF_INET_POST_ROUTING,
267                 .priority       = NF_IP6_PRI_CONNTRACK_HELPER,
268         },
269         {
270                 .hook           = ipv6_confirm,
271                 .owner          = THIS_MODULE,
272                 .pf             = NFPROTO_IPV6,
273                 .hooknum        = NF_INET_POST_ROUTING,
274                 .priority       = NF_IP6_PRI_LAST,
275         },
276         {
277                 .hook           = ipv6_helper,
278                 .owner          = THIS_MODULE,
279                 .pf             = NFPROTO_IPV6,
280                 .hooknum        = NF_INET_LOCAL_IN,
281                 .priority       = NF_IP6_PRI_CONNTRACK_HELPER,
282         },
283         {
284                 .hook           = ipv6_confirm,
285                 .owner          = THIS_MODULE,
286                 .pf             = NFPROTO_IPV6,
287                 .hooknum        = NF_INET_LOCAL_IN,
288                 .priority       = NF_IP6_PRI_LAST-1,
289         },
290 };
291
292 static int
293 ipv6_getorigdst(struct sock *sk, int optval, void __user *user, int *len)
294 {
295         const struct inet_sock *inet = inet_sk(sk);
296         const struct ipv6_pinfo *inet6 = inet6_sk(sk);
297         const struct nf_conntrack_tuple_hash *h;
298         struct sockaddr_in6 sin6;
299         struct nf_conntrack_tuple tuple = { .src.l3num = NFPROTO_IPV6 };
300         struct nf_conn *ct;
301
302         tuple.src.u3.in6 = sk->sk_v6_rcv_saddr;
303         tuple.src.u.tcp.port = inet->inet_sport;
304         tuple.dst.u3.in6 = sk->sk_v6_daddr;
305         tuple.dst.u.tcp.port = inet->inet_dport;
306         tuple.dst.protonum = sk->sk_protocol;
307
308         if (sk->sk_protocol != IPPROTO_TCP && sk->sk_protocol != IPPROTO_SCTP)
309                 return -ENOPROTOOPT;
310
311         if (*len < 0 || (unsigned int) *len < sizeof(sin6))
312                 return -EINVAL;
313
314         h = nf_conntrack_find_get(sock_net(sk), NF_CT_DEFAULT_ZONE, &tuple);
315         if (!h) {
316                 pr_debug("IP6T_SO_ORIGINAL_DST: Can't find %pI6c/%u-%pI6c/%u.\n",
317                          &tuple.src.u3.ip6, ntohs(tuple.src.u.tcp.port),
318                          &tuple.dst.u3.ip6, ntohs(tuple.dst.u.tcp.port));
319                 return -ENOENT;
320         }
321
322         ct = nf_ct_tuplehash_to_ctrack(h);
323
324         sin6.sin6_family = AF_INET6;
325         sin6.sin6_port = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.tcp.port;
326         sin6.sin6_flowinfo = inet6->flow_label & IPV6_FLOWINFO_MASK;
327         memcpy(&sin6.sin6_addr,
328                 &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.in6,
329                                         sizeof(sin6.sin6_addr));
330
331         nf_ct_put(ct);
332         sin6.sin6_scope_id = ipv6_iface_scope_id(&sin6.sin6_addr,
333                                                  sk->sk_bound_dev_if);
334         return copy_to_user(user, &sin6, sizeof(sin6)) ? -EFAULT : 0;
335 }
336
337 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
338
339 #include <linux/netfilter/nfnetlink.h>
340 #include <linux/netfilter/nfnetlink_conntrack.h>
341
342 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
343                                 const struct nf_conntrack_tuple *tuple)
344 {
345         if (nla_put(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
346                     &tuple->src.u3.ip6) ||
347             nla_put(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
348                     &tuple->dst.u3.ip6))
349                 goto nla_put_failure;
350         return 0;
351
352 nla_put_failure:
353         return -1;
354 }
355
356 static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
357         [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
358         [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
359 };
360
361 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
362                                 struct nf_conntrack_tuple *t)
363 {
364         if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
365                 return -EINVAL;
366
367         memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
368                sizeof(u_int32_t) * 4);
369         memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
370                sizeof(u_int32_t) * 4);
371
372         return 0;
373 }
374
375 static int ipv6_nlattr_tuple_size(void)
376 {
377         return nla_policy_len(ipv6_nla_policy, CTA_IP_MAX + 1);
378 }
379 #endif
380
381 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
382         .l3proto                = PF_INET6,
383         .name                   = "ipv6",
384         .pkt_to_tuple           = ipv6_pkt_to_tuple,
385         .invert_tuple           = ipv6_invert_tuple,
386         .print_tuple            = ipv6_print_tuple,
387         .get_l4proto            = ipv6_get_l4proto,
388 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
389         .tuple_to_nlattr        = ipv6_tuple_to_nlattr,
390         .nlattr_tuple_size      = ipv6_nlattr_tuple_size,
391         .nlattr_to_tuple        = ipv6_nlattr_to_tuple,
392         .nla_policy             = ipv6_nla_policy,
393 #endif
394         .me                     = THIS_MODULE,
395 };
396
397 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
398 MODULE_LICENSE("GPL");
399 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
400
401 static struct nf_sockopt_ops so_getorigdst6 = {
402         .pf             = NFPROTO_IPV6,
403         .get_optmin     = IP6T_SO_ORIGINAL_DST,
404         .get_optmax     = IP6T_SO_ORIGINAL_DST + 1,
405         .get            = ipv6_getorigdst,
406         .owner          = THIS_MODULE,
407 };
408
409 static int ipv6_net_init(struct net *net)
410 {
411         int ret = 0;
412
413         ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_tcp6);
414         if (ret < 0) {
415                 pr_err("nf_conntrack_tcp6: pernet registration failed\n");
416                 goto out;
417         }
418         ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udp6);
419         if (ret < 0) {
420                 pr_err("nf_conntrack_udp6: pernet registration failed\n");
421                 goto cleanup_tcp6;
422         }
423         ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_icmpv6);
424         if (ret < 0) {
425                 pr_err("nf_conntrack_icmp6: pernet registration failed\n");
426                 goto cleanup_udp6;
427         }
428         ret = nf_ct_l3proto_pernet_register(net, &nf_conntrack_l3proto_ipv6);
429         if (ret < 0) {
430                 pr_err("nf_conntrack_ipv6: pernet registration failed.\n");
431                 goto cleanup_icmpv6;
432         }
433         return 0;
434  cleanup_icmpv6:
435         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmpv6);
436  cleanup_udp6:
437         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp6);
438  cleanup_tcp6:
439         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp6);
440  out:
441         return ret;
442 }
443
444 static void ipv6_net_exit(struct net *net)
445 {
446         nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv6);
447         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmpv6);
448         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp6);
449         nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp6);
450 }
451
452 static struct pernet_operations ipv6_net_ops = {
453         .init = ipv6_net_init,
454         .exit = ipv6_net_exit,
455 };
456
457 static int __init nf_conntrack_l3proto_ipv6_init(void)
458 {
459         int ret = 0;
460
461         need_conntrack();
462         nf_defrag_ipv6_enable();
463
464         ret = nf_register_sockopt(&so_getorigdst6);
465         if (ret < 0) {
466                 pr_err("Unable to register netfilter socket option\n");
467                 return ret;
468         }
469
470         ret = register_pernet_subsys(&ipv6_net_ops);
471         if (ret < 0)
472                 goto cleanup_sockopt;
473
474         ret = nf_register_hooks(ipv6_conntrack_ops,
475                                 ARRAY_SIZE(ipv6_conntrack_ops));
476         if (ret < 0) {
477                 pr_err("nf_conntrack_ipv6: can't register pre-routing defrag "
478                        "hook.\n");
479                 goto cleanup_pernet;
480         }
481
482         ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_tcp6);
483         if (ret < 0) {
484                 pr_err("nf_conntrack_ipv6: can't register tcp6 proto.\n");
485                 goto cleanup_hooks;
486         }
487
488         ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udp6);
489         if (ret < 0) {
490                 pr_err("nf_conntrack_ipv6: can't register udp6 proto.\n");
491                 goto cleanup_tcp6;
492         }
493
494         ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_icmpv6);
495         if (ret < 0) {
496                 pr_err("nf_conntrack_ipv6: can't register icmpv6 proto.\n");
497                 goto cleanup_udp6;
498         }
499
500         ret = nf_ct_l3proto_register(&nf_conntrack_l3proto_ipv6);
501         if (ret < 0) {
502                 pr_err("nf_conntrack_ipv6: can't register ipv6 proto.\n");
503                 goto cleanup_icmpv6;
504         }
505         return ret;
506
507  cleanup_icmpv6:
508         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
509  cleanup_udp6:
510         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp6);
511  cleanup_tcp6:
512         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
513  cleanup_hooks:
514         nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
515  cleanup_pernet:
516         unregister_pernet_subsys(&ipv6_net_ops);
517  cleanup_sockopt:
518         nf_unregister_sockopt(&so_getorigdst6);
519         return ret;
520 }
521
522 static void __exit nf_conntrack_l3proto_ipv6_fini(void)
523 {
524         synchronize_net();
525         nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
526         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
527         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp6);
528         nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
529         nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
530         unregister_pernet_subsys(&ipv6_net_ops);
531         nf_unregister_sockopt(&so_getorigdst6);
532 }
533
534 module_init(nf_conntrack_l3proto_ipv6_init);
535 module_exit(nf_conntrack_l3proto_ipv6_fini);