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