gro: Disable frag0 optimization on IPv6 ext headers
[pandora-kernel.git] / net / ipv6 / af_inet6.c
1 /*
2  *      PF_INET6 socket protocol family
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>
7  *
8  *      Adapted from linux/net/ipv4/af_inet.c
9  *
10  *      Fixes:
11  *      piggy, Karl Knutson     :       Socket protocol table
12  *      Hideaki YOSHIFUJI       :       sin6_scope_id support
13  *      Arnaldo Melo            :       check proc_net_create return, cleanups
14  *
15  *      This program is free software; you can redistribute it and/or
16  *      modify it under the terms of the GNU General Public License
17  *      as published by the Free Software Foundation; either version
18  *      2 of the License, or (at your option) any later version.
19  */
20
21
22 #include <linux/module.h>
23 #include <linux/capability.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/in.h>
28 #include <linux/kernel.h>
29 #include <linux/timer.h>
30 #include <linux/string.h>
31 #include <linux/sockios.h>
32 #include <linux/net.h>
33 #include <linux/fcntl.h>
34 #include <linux/mm.h>
35 #include <linux/interrupt.h>
36 #include <linux/proc_fs.h>
37 #include <linux/stat.h>
38 #include <linux/init.h>
39 #include <linux/slab.h>
40
41 #include <linux/inet.h>
42 #include <linux/netdevice.h>
43 #include <linux/icmpv6.h>
44 #include <linux/netfilter_ipv6.h>
45
46 #include <net/ip.h>
47 #include <net/ipv6.h>
48 #include <net/udp.h>
49 #include <net/udplite.h>
50 #include <net/tcp.h>
51 #include <net/ipip.h>
52 #include <net/protocol.h>
53 #include <net/inet_common.h>
54 #include <net/route.h>
55 #include <net/transp_v6.h>
56 #include <net/ip6_route.h>
57 #include <net/addrconf.h>
58 #ifdef CONFIG_IPV6_TUNNEL
59 #include <net/ip6_tunnel.h>
60 #endif
61
62 #include <asm/uaccess.h>
63 #include <asm/system.h>
64 #include <linux/mroute6.h>
65
66 MODULE_AUTHOR("Cast of dozens");
67 MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
68 MODULE_LICENSE("GPL");
69
70 /* The inetsw6 table contains everything that inet6_create needs to
71  * build a new socket.
72  */
73 static struct list_head inetsw6[SOCK_MAX];
74 static DEFINE_SPINLOCK(inetsw6_lock);
75
76 struct ipv6_params ipv6_defaults = {
77         .disable_ipv6 = 0,
78         .autoconf = 1,
79 };
80
81 static int disable_ipv6_mod = 0;
82
83 module_param_named(disable, disable_ipv6_mod, int, 0444);
84 MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
85
86 module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
87 MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
88
89 module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
90 MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
91
92 static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
93 {
94         const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
95
96         return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
97 }
98
99 static int inet6_create(struct net *net, struct socket *sock, int protocol,
100                         int kern)
101 {
102         struct inet_sock *inet;
103         struct ipv6_pinfo *np;
104         struct sock *sk;
105         struct inet_protosw *answer;
106         struct proto *answer_prot;
107         unsigned char answer_flags;
108         char answer_no_check;
109         int try_loading_module = 0;
110         int err;
111
112         if (protocol < 0 || protocol >= IPPROTO_MAX)
113                 return -EINVAL;
114
115         if (sock->type != SOCK_RAW &&
116             sock->type != SOCK_DGRAM &&
117             !inet_ehash_secret)
118                 build_ehash_secret();
119
120         /* Look for the requested type/protocol pair. */
121 lookup_protocol:
122         err = -ESOCKTNOSUPPORT;
123         rcu_read_lock();
124         list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
125
126                 err = 0;
127                 /* Check the non-wild match. */
128                 if (protocol == answer->protocol) {
129                         if (protocol != IPPROTO_IP)
130                                 break;
131                 } else {
132                         /* Check for the two wild cases. */
133                         if (IPPROTO_IP == protocol) {
134                                 protocol = answer->protocol;
135                                 break;
136                         }
137                         if (IPPROTO_IP == answer->protocol)
138                                 break;
139                 }
140                 err = -EPROTONOSUPPORT;
141         }
142
143         if (err) {
144                 if (try_loading_module < 2) {
145                         rcu_read_unlock();
146                         /*
147                          * Be more specific, e.g. net-pf-10-proto-132-type-1
148                          * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
149                          */
150                         if (++try_loading_module == 1)
151                                 request_module("net-pf-%d-proto-%d-type-%d",
152                                                 PF_INET6, protocol, sock->type);
153                         /*
154                          * Fall back to generic, e.g. net-pf-10-proto-132
155                          * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
156                          */
157                         else
158                                 request_module("net-pf-%d-proto-%d",
159                                                 PF_INET6, protocol);
160                         goto lookup_protocol;
161                 } else
162                         goto out_rcu_unlock;
163         }
164
165         err = -EPERM;
166         if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
167                 goto out_rcu_unlock;
168
169         sock->ops = answer->ops;
170         answer_prot = answer->prot;
171         answer_no_check = answer->no_check;
172         answer_flags = answer->flags;
173         rcu_read_unlock();
174
175         WARN_ON(answer_prot->slab == NULL);
176
177         err = -ENOBUFS;
178         sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
179         if (sk == NULL)
180                 goto out;
181
182         sock_init_data(sock, sk);
183
184         err = 0;
185         sk->sk_no_check = answer_no_check;
186         if (INET_PROTOSW_REUSE & answer_flags)
187                 sk->sk_reuse = 1;
188
189         inet = inet_sk(sk);
190         inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
191
192         if (SOCK_RAW == sock->type) {
193                 inet->inet_num = protocol;
194                 if (IPPROTO_RAW == protocol)
195                         inet->hdrincl = 1;
196         }
197
198         sk->sk_destruct         = inet_sock_destruct;
199         sk->sk_family           = PF_INET6;
200         sk->sk_protocol         = protocol;
201
202         sk->sk_backlog_rcv      = answer->prot->backlog_rcv;
203
204         inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
205         np->hop_limit   = -1;
206         np->mcast_hops  = IPV6_DEFAULT_MCASTHOPS;
207         np->mc_loop     = 1;
208         np->pmtudisc    = IPV6_PMTUDISC_WANT;
209         np->ipv6only    = net->ipv6.sysctl.bindv6only;
210
211         /* Init the ipv4 part of the socket since we can have sockets
212          * using v6 API for ipv4.
213          */
214         inet->uc_ttl    = -1;
215
216         inet->mc_loop   = 1;
217         inet->mc_ttl    = 1;
218         inet->mc_index  = 0;
219         inet->mc_list   = NULL;
220
221         if (ipv4_config.no_pmtu_disc)
222                 inet->pmtudisc = IP_PMTUDISC_DONT;
223         else
224                 inet->pmtudisc = IP_PMTUDISC_WANT;
225         /*
226          * Increment only the relevant sk_prot->socks debug field, this changes
227          * the previous behaviour of incrementing both the equivalent to
228          * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
229          *
230          * This allows better debug granularity as we'll know exactly how many
231          * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
232          * transport protocol socks. -acme
233          */
234         sk_refcnt_debug_inc(sk);
235
236         if (inet->inet_num) {
237                 /* It assumes that any protocol which allows
238                  * the user to assign a number at socket
239                  * creation time automatically shares.
240                  */
241                 inet->inet_sport = htons(inet->inet_num);
242                 sk->sk_prot->hash(sk);
243         }
244         if (sk->sk_prot->init) {
245                 err = sk->sk_prot->init(sk);
246                 if (err) {
247                         sk_common_release(sk);
248                         goto out;
249                 }
250         }
251 out:
252         return err;
253 out_rcu_unlock:
254         rcu_read_unlock();
255         goto out;
256 }
257
258
259 /* bind for INET6 API */
260 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
261 {
262         struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
263         struct sock *sk = sock->sk;
264         struct inet_sock *inet = inet_sk(sk);
265         struct ipv6_pinfo *np = inet6_sk(sk);
266         struct net *net = sock_net(sk);
267         __be32 v4addr = 0;
268         unsigned short snum;
269         int addr_type = 0;
270         int err = 0;
271
272         /* If the socket has its own bind function then use it. */
273         if (sk->sk_prot->bind)
274                 return sk->sk_prot->bind(sk, uaddr, addr_len);
275
276         if (addr_len < SIN6_LEN_RFC2133)
277                 return -EINVAL;
278
279         if (addr->sin6_family != AF_INET6)
280                 return -EAFNOSUPPORT;
281
282         addr_type = ipv6_addr_type(&addr->sin6_addr);
283         if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
284                 return -EINVAL;
285
286         snum = ntohs(addr->sin6_port);
287         if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
288                 return -EACCES;
289
290         lock_sock(sk);
291
292         /* Check these errors (active socket, double bind). */
293         if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
294                 err = -EINVAL;
295                 goto out;
296         }
297
298         /* Check if the address belongs to the host. */
299         if (addr_type == IPV6_ADDR_MAPPED) {
300                 int chk_addr_ret;
301
302                 /* Binding to v4-mapped address on a v6-only socket
303                  * makes no sense
304                  */
305                 if (np->ipv6only) {
306                         err = -EINVAL;
307                         goto out;
308                 }
309
310                 /* Reproduce AF_INET checks to make the bindings consistent */
311                 v4addr = addr->sin6_addr.s6_addr32[3];
312                 chk_addr_ret = inet_addr_type(net, v4addr);
313                 if (!sysctl_ip_nonlocal_bind &&
314                     !(inet->freebind || inet->transparent) &&
315                     v4addr != htonl(INADDR_ANY) &&
316                     chk_addr_ret != RTN_LOCAL &&
317                     chk_addr_ret != RTN_MULTICAST &&
318                     chk_addr_ret != RTN_BROADCAST) {
319                         err = -EADDRNOTAVAIL;
320                         goto out;
321                 }
322         } else {
323                 if (addr_type != IPV6_ADDR_ANY) {
324                         struct net_device *dev = NULL;
325
326                         rcu_read_lock();
327                         if (addr_type & IPV6_ADDR_LINKLOCAL) {
328                                 if (addr_len >= sizeof(struct sockaddr_in6) &&
329                                     addr->sin6_scope_id) {
330                                         /* Override any existing binding, if another one
331                                          * is supplied by user.
332                                          */
333                                         sk->sk_bound_dev_if = addr->sin6_scope_id;
334                                 }
335
336                                 /* Binding to link-local address requires an interface */
337                                 if (!sk->sk_bound_dev_if) {
338                                         err = -EINVAL;
339                                         goto out_unlock;
340                                 }
341                                 dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
342                                 if (!dev) {
343                                         err = -ENODEV;
344                                         goto out_unlock;
345                                 }
346                         }
347
348                         /* ipv4 addr of the socket is invalid.  Only the
349                          * unspecified and mapped address have a v4 equivalent.
350                          */
351                         v4addr = LOOPBACK4_IPV6;
352                         if (!(addr_type & IPV6_ADDR_MULTICAST)) {
353                                 if (!inet->transparent &&
354                                     !ipv6_chk_addr(net, &addr->sin6_addr,
355                                                    dev, 0)) {
356                                         err = -EADDRNOTAVAIL;
357                                         goto out_unlock;
358                                 }
359                         }
360                         rcu_read_unlock();
361                 }
362         }
363
364         inet->inet_rcv_saddr = v4addr;
365         inet->inet_saddr = v4addr;
366
367         ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
368
369         if (!(addr_type & IPV6_ADDR_MULTICAST))
370                 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
371
372         /* Make sure we are allowed to bind here. */
373         if (sk->sk_prot->get_port(sk, snum)) {
374                 inet_reset_saddr(sk);
375                 err = -EADDRINUSE;
376                 goto out;
377         }
378
379         if (addr_type != IPV6_ADDR_ANY) {
380                 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
381                 if (addr_type != IPV6_ADDR_MAPPED)
382                         np->ipv6only = 1;
383         }
384         if (snum)
385                 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
386         inet->inet_sport = htons(inet->inet_num);
387         inet->inet_dport = 0;
388         inet->inet_daddr = 0;
389 out:
390         release_sock(sk);
391         return err;
392 out_unlock:
393         rcu_read_unlock();
394         goto out;
395 }
396
397 EXPORT_SYMBOL(inet6_bind);
398
399 int inet6_release(struct socket *sock)
400 {
401         struct sock *sk = sock->sk;
402
403         if (sk == NULL)
404                 return -EINVAL;
405
406         /* Free mc lists */
407         ipv6_sock_mc_close(sk);
408
409         /* Free ac lists */
410         ipv6_sock_ac_close(sk);
411
412         return inet_release(sock);
413 }
414
415 EXPORT_SYMBOL(inet6_release);
416
417 void inet6_destroy_sock(struct sock *sk)
418 {
419         struct ipv6_pinfo *np = inet6_sk(sk);
420         struct sk_buff *skb;
421         struct ipv6_txoptions *opt;
422
423         /* Release rx options */
424
425         if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
426                 kfree_skb(skb);
427
428         if ((skb = xchg(&np->rxpmtu, NULL)) != NULL)
429                 kfree_skb(skb);
430
431         /* Free flowlabels */
432         fl6_free_socklist(sk);
433
434         /* Free tx options */
435
436         opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL);
437         if (opt) {
438                 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
439                 txopt_put(opt);
440         }
441 }
442
443 EXPORT_SYMBOL_GPL(inet6_destroy_sock);
444
445 /*
446  *      This does both peername and sockname.
447  */
448
449 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
450                  int *uaddr_len, int peer)
451 {
452         struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
453         struct sock *sk = sock->sk;
454         struct inet_sock *inet = inet_sk(sk);
455         struct ipv6_pinfo *np = inet6_sk(sk);
456
457         sin->sin6_family = AF_INET6;
458         sin->sin6_flowinfo = 0;
459         sin->sin6_scope_id = 0;
460         if (peer) {
461                 if (!inet->inet_dport)
462                         return -ENOTCONN;
463                 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
464                     peer == 1)
465                         return -ENOTCONN;
466                 sin->sin6_port = inet->inet_dport;
467                 ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
468                 if (np->sndflow)
469                         sin->sin6_flowinfo = np->flow_label;
470         } else {
471                 if (ipv6_addr_any(&np->rcv_saddr))
472                         ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
473                 else
474                         ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
475
476                 sin->sin6_port = inet->inet_sport;
477         }
478         if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
479                 sin->sin6_scope_id = sk->sk_bound_dev_if;
480         *uaddr_len = sizeof(*sin);
481         return 0;
482 }
483
484 EXPORT_SYMBOL(inet6_getname);
485
486 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
487 {
488         struct sock *sk = sock->sk;
489         struct net *net = sock_net(sk);
490
491         switch(cmd)
492         {
493         case SIOCGSTAMP:
494                 return sock_get_timestamp(sk, (struct timeval __user *)arg);
495
496         case SIOCGSTAMPNS:
497                 return sock_get_timestampns(sk, (struct timespec __user *)arg);
498
499         case SIOCADDRT:
500         case SIOCDELRT:
501
502                 return ipv6_route_ioctl(net, cmd, (void __user *)arg);
503
504         case SIOCSIFADDR:
505                 return addrconf_add_ifaddr(net, (void __user *) arg);
506         case SIOCDIFADDR:
507                 return addrconf_del_ifaddr(net, (void __user *) arg);
508         case SIOCSIFDSTADDR:
509                 return addrconf_set_dstaddr(net, (void __user *) arg);
510         default:
511                 if (!sk->sk_prot->ioctl)
512                         return -ENOIOCTLCMD;
513                 return sk->sk_prot->ioctl(sk, cmd, arg);
514         }
515         /*NOTREACHED*/
516         return 0;
517 }
518
519 EXPORT_SYMBOL(inet6_ioctl);
520
521 const struct proto_ops inet6_stream_ops = {
522         .family            = PF_INET6,
523         .owner             = THIS_MODULE,
524         .release           = inet6_release,
525         .bind              = inet6_bind,
526         .connect           = inet_stream_connect,       /* ok           */
527         .socketpair        = sock_no_socketpair,        /* a do nothing */
528         .accept            = inet_accept,               /* ok           */
529         .getname           = inet6_getname,
530         .poll              = tcp_poll,                  /* ok           */
531         .ioctl             = inet6_ioctl,               /* must change  */
532         .listen            = inet_listen,               /* ok           */
533         .shutdown          = inet_shutdown,             /* ok           */
534         .setsockopt        = sock_common_setsockopt,    /* ok           */
535         .getsockopt        = sock_common_getsockopt,    /* ok           */
536         .sendmsg           = inet_sendmsg,              /* ok           */
537         .recvmsg           = inet_recvmsg,              /* ok           */
538         .mmap              = sock_no_mmap,
539         .sendpage          = inet_sendpage,
540         .splice_read       = tcp_splice_read,
541 #ifdef CONFIG_COMPAT
542         .compat_setsockopt = compat_sock_common_setsockopt,
543         .compat_getsockopt = compat_sock_common_getsockopt,
544 #endif
545 };
546
547 const struct proto_ops inet6_dgram_ops = {
548         .family            = PF_INET6,
549         .owner             = THIS_MODULE,
550         .release           = inet6_release,
551         .bind              = inet6_bind,
552         .connect           = inet_dgram_connect,        /* ok           */
553         .socketpair        = sock_no_socketpair,        /* a do nothing */
554         .accept            = sock_no_accept,            /* a do nothing */
555         .getname           = inet6_getname,
556         .poll              = udp_poll,                  /* ok           */
557         .ioctl             = inet6_ioctl,               /* must change  */
558         .listen            = sock_no_listen,            /* ok           */
559         .shutdown          = inet_shutdown,             /* ok           */
560         .setsockopt        = sock_common_setsockopt,    /* ok           */
561         .getsockopt        = sock_common_getsockopt,    /* ok           */
562         .sendmsg           = inet_sendmsg,              /* ok           */
563         .recvmsg           = inet_recvmsg,              /* ok           */
564         .mmap              = sock_no_mmap,
565         .sendpage          = sock_no_sendpage,
566 #ifdef CONFIG_COMPAT
567         .compat_setsockopt = compat_sock_common_setsockopt,
568         .compat_getsockopt = compat_sock_common_getsockopt,
569 #endif
570 };
571
572 static const struct net_proto_family inet6_family_ops = {
573         .family = PF_INET6,
574         .create = inet6_create,
575         .owner  = THIS_MODULE,
576 };
577
578 int inet6_register_protosw(struct inet_protosw *p)
579 {
580         struct list_head *lh;
581         struct inet_protosw *answer;
582         struct list_head *last_perm;
583         int protocol = p->protocol;
584         int ret;
585
586         spin_lock_bh(&inetsw6_lock);
587
588         ret = -EINVAL;
589         if (p->type >= SOCK_MAX)
590                 goto out_illegal;
591
592         /* If we are trying to override a permanent protocol, bail. */
593         answer = NULL;
594         ret = -EPERM;
595         last_perm = &inetsw6[p->type];
596         list_for_each(lh, &inetsw6[p->type]) {
597                 answer = list_entry(lh, struct inet_protosw, list);
598
599                 /* Check only the non-wild match. */
600                 if (INET_PROTOSW_PERMANENT & answer->flags) {
601                         if (protocol == answer->protocol)
602                                 break;
603                         last_perm = lh;
604                 }
605
606                 answer = NULL;
607         }
608         if (answer)
609                 goto out_permanent;
610
611         /* Add the new entry after the last permanent entry if any, so that
612          * the new entry does not override a permanent entry when matched with
613          * a wild-card protocol. But it is allowed to override any existing
614          * non-permanent entry.  This means that when we remove this entry, the
615          * system automatically returns to the old behavior.
616          */
617         list_add_rcu(&p->list, last_perm);
618         ret = 0;
619 out:
620         spin_unlock_bh(&inetsw6_lock);
621         return ret;
622
623 out_permanent:
624         printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
625                protocol);
626         goto out;
627
628 out_illegal:
629         printk(KERN_ERR
630                "Ignoring attempt to register invalid socket type %d.\n",
631                p->type);
632         goto out;
633 }
634
635 EXPORT_SYMBOL(inet6_register_protosw);
636
637 void
638 inet6_unregister_protosw(struct inet_protosw *p)
639 {
640         if (INET_PROTOSW_PERMANENT & p->flags) {
641                 printk(KERN_ERR
642                        "Attempt to unregister permanent protocol %d.\n",
643                        p->protocol);
644         } else {
645                 spin_lock_bh(&inetsw6_lock);
646                 list_del_rcu(&p->list);
647                 spin_unlock_bh(&inetsw6_lock);
648
649                 synchronize_net();
650         }
651 }
652
653 EXPORT_SYMBOL(inet6_unregister_protosw);
654
655 int inet6_sk_rebuild_header(struct sock *sk)
656 {
657         struct ipv6_pinfo *np = inet6_sk(sk);
658         struct dst_entry *dst;
659
660         dst = __sk_dst_check(sk, np->dst_cookie);
661
662         if (dst == NULL) {
663                 struct inet_sock *inet = inet_sk(sk);
664                 struct in6_addr *final_p, final;
665                 struct flowi6 fl6;
666
667                 memset(&fl6, 0, sizeof(fl6));
668                 fl6.flowi6_proto = sk->sk_protocol;
669                 ipv6_addr_copy(&fl6.daddr, &np->daddr);
670                 ipv6_addr_copy(&fl6.saddr, &np->saddr);
671                 fl6.flowlabel = np->flow_label;
672                 fl6.flowi6_oif = sk->sk_bound_dev_if;
673                 fl6.flowi6_mark = sk->sk_mark;
674                 fl6.fl6_dport = inet->inet_dport;
675                 fl6.fl6_sport = inet->inet_sport;
676                 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
677
678                 rcu_read_lock();
679                 final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt),
680                                          &final);
681                 rcu_read_unlock();
682
683                 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);
684                 if (IS_ERR(dst)) {
685                         sk->sk_route_caps = 0;
686                         sk->sk_err_soft = -PTR_ERR(dst);
687                         return PTR_ERR(dst);
688                 }
689
690                 __ip6_dst_store(sk, dst, NULL, NULL);
691         }
692
693         return 0;
694 }
695
696 EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
697
698 int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
699 {
700         struct ipv6_pinfo *np = inet6_sk(sk);
701         struct inet6_skb_parm *opt = IP6CB(skb);
702
703         if (np->rxopt.all) {
704                 if ((opt->hop && (np->rxopt.bits.hopopts ||
705                                   np->rxopt.bits.ohopopts)) ||
706                     ((IPV6_FLOWINFO_MASK &
707                       *(__be32 *)skb_network_header(skb)) &&
708                      np->rxopt.bits.rxflow) ||
709                     (opt->srcrt && (np->rxopt.bits.srcrt ||
710                      np->rxopt.bits.osrcrt)) ||
711                     ((opt->dst1 || opt->dst0) &&
712                      (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
713                         return 1;
714         }
715         return 0;
716 }
717
718 EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
719
720 static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
721 {
722         const struct inet6_protocol *ops = NULL;
723
724         for (;;) {
725                 struct ipv6_opt_hdr *opth;
726                 int len;
727
728                 if (proto != NEXTHDR_HOP) {
729                         ops = rcu_dereference(inet6_protos[proto]);
730
731                         if (unlikely(!ops))
732                                 break;
733
734                         if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
735                                 break;
736                 }
737
738                 if (unlikely(!pskb_may_pull(skb, 8)))
739                         break;
740
741                 opth = (void *)skb->data;
742                 len = ipv6_optlen(opth);
743
744                 if (unlikely(!pskb_may_pull(skb, len)))
745                         break;
746
747                 proto = opth->nexthdr;
748                 __skb_pull(skb, len);
749         }
750
751         return proto;
752 }
753
754 static int ipv6_gso_send_check(struct sk_buff *skb)
755 {
756         const struct ipv6hdr *ipv6h;
757         const struct inet6_protocol *ops;
758         int err = -EINVAL;
759
760         if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
761                 goto out;
762
763         ipv6h = ipv6_hdr(skb);
764         __skb_pull(skb, sizeof(*ipv6h));
765         err = -EPROTONOSUPPORT;
766
767         rcu_read_lock();
768         ops = rcu_dereference(inet6_protos[
769                 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
770
771         if (likely(ops && ops->gso_send_check)) {
772                 skb_reset_transport_header(skb);
773                 err = ops->gso_send_check(skb);
774         }
775         rcu_read_unlock();
776
777 out:
778         return err;
779 }
780
781 static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, u32 features)
782 {
783         struct sk_buff *segs = ERR_PTR(-EINVAL);
784         struct ipv6hdr *ipv6h;
785         const struct inet6_protocol *ops;
786         int proto;
787         struct frag_hdr *fptr;
788         unsigned int unfrag_ip6hlen;
789         u8 *prevhdr;
790         int offset = 0;
791
792         if (!(features & NETIF_F_V6_CSUM))
793                 features &= ~NETIF_F_SG;
794
795         if (unlikely(skb_shinfo(skb)->gso_type &
796                      ~(SKB_GSO_UDP |
797                        SKB_GSO_DODGY |
798                        SKB_GSO_TCP_ECN |
799                        SKB_GSO_TCPV6 |
800                        0)))
801                 goto out;
802
803         if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
804                 goto out;
805
806         ipv6h = ipv6_hdr(skb);
807         __skb_pull(skb, sizeof(*ipv6h));
808         segs = ERR_PTR(-EPROTONOSUPPORT);
809
810         proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
811         rcu_read_lock();
812         ops = rcu_dereference(inet6_protos[proto]);
813         if (likely(ops && ops->gso_segment)) {
814                 skb_reset_transport_header(skb);
815                 segs = ops->gso_segment(skb, features);
816         }
817         rcu_read_unlock();
818
819         if (IS_ERR(segs))
820                 goto out;
821
822         for (skb = segs; skb; skb = skb->next) {
823                 ipv6h = ipv6_hdr(skb);
824                 ipv6h->payload_len = htons(skb->len - skb->mac_len -
825                                            sizeof(*ipv6h));
826                 if (proto == IPPROTO_UDP) {
827                         unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
828                         fptr = (struct frag_hdr *)(skb_network_header(skb) +
829                                 unfrag_ip6hlen);
830                         fptr->frag_off = htons(offset);
831                         if (skb->next != NULL)
832                                 fptr->frag_off |= htons(IP6_MF);
833                         offset += (ntohs(ipv6h->payload_len) -
834                                    sizeof(struct frag_hdr));
835                 }
836         }
837
838 out:
839         return segs;
840 }
841
842 struct ipv6_gro_cb {
843         struct napi_gro_cb napi;
844         int proto;
845 };
846
847 #define IPV6_GRO_CB(skb) ((struct ipv6_gro_cb *)(skb)->cb)
848
849 static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
850                                          struct sk_buff *skb)
851 {
852         const struct inet6_protocol *ops;
853         struct sk_buff **pp = NULL;
854         struct sk_buff *p;
855         struct ipv6hdr *iph;
856         unsigned int nlen;
857         unsigned int hlen;
858         unsigned int off;
859         int flush = 1;
860         int proto;
861         __wsum csum;
862
863         off = skb_gro_offset(skb);
864         hlen = off + sizeof(*iph);
865         iph = skb_gro_header_fast(skb, off);
866         if (skb_gro_header_hard(skb, hlen)) {
867                 iph = skb_gro_header_slow(skb, hlen, off);
868                 if (unlikely(!iph))
869                         goto out;
870         }
871
872         skb_gro_pull(skb, sizeof(*iph));
873         skb_set_transport_header(skb, skb_gro_offset(skb));
874
875         flush += ntohs(iph->payload_len) != skb_gro_len(skb);
876
877         rcu_read_lock();
878         proto = iph->nexthdr;
879         ops = rcu_dereference(inet6_protos[proto]);
880         if (!ops || !ops->gro_receive) {
881                 __pskb_pull(skb, skb_gro_offset(skb));
882                 skb_gro_frag0_invalidate(skb);
883                 proto = ipv6_gso_pull_exthdrs(skb, proto);
884                 skb_gro_pull(skb, -skb_transport_offset(skb));
885                 skb_reset_transport_header(skb);
886                 __skb_push(skb, skb_gro_offset(skb));
887
888                 ops = rcu_dereference(inet6_protos[proto]);
889                 if (!ops || !ops->gro_receive)
890                         goto out_unlock;
891
892                 iph = ipv6_hdr(skb);
893         }
894
895         IPV6_GRO_CB(skb)->proto = proto;
896
897         flush--;
898         nlen = skb_network_header_len(skb);
899
900         for (p = *head; p; p = p->next) {
901                 struct ipv6hdr *iph2;
902
903                 if (!NAPI_GRO_CB(p)->same_flow)
904                         continue;
905
906                 iph2 = ipv6_hdr(p);
907
908                 /* All fields must match except length. */
909                 if (nlen != skb_network_header_len(p) ||
910                     memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
911                     memcmp(&iph->nexthdr, &iph2->nexthdr,
912                            nlen - offsetof(struct ipv6hdr, nexthdr))) {
913                         NAPI_GRO_CB(p)->same_flow = 0;
914                         continue;
915                 }
916
917                 NAPI_GRO_CB(p)->flush |= flush;
918         }
919
920         NAPI_GRO_CB(skb)->flush |= flush;
921
922         csum = skb->csum;
923         skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
924
925         pp = ops->gro_receive(head, skb);
926
927         skb->csum = csum;
928
929 out_unlock:
930         rcu_read_unlock();
931
932 out:
933         NAPI_GRO_CB(skb)->flush |= flush;
934
935         return pp;
936 }
937
938 static int ipv6_gro_complete(struct sk_buff *skb)
939 {
940         const struct inet6_protocol *ops;
941         struct ipv6hdr *iph = ipv6_hdr(skb);
942         int err = -ENOSYS;
943
944         iph->payload_len = htons(skb->len - skb_network_offset(skb) -
945                                  sizeof(*iph));
946
947         rcu_read_lock();
948         ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
949         if (WARN_ON(!ops || !ops->gro_complete))
950                 goto out_unlock;
951
952         err = ops->gro_complete(skb);
953
954 out_unlock:
955         rcu_read_unlock();
956
957         return err;
958 }
959
960 static struct packet_type ipv6_packet_type __read_mostly = {
961         .type = cpu_to_be16(ETH_P_IPV6),
962         .func = ipv6_rcv,
963         .gso_send_check = ipv6_gso_send_check,
964         .gso_segment = ipv6_gso_segment,
965         .gro_receive = ipv6_gro_receive,
966         .gro_complete = ipv6_gro_complete,
967 };
968
969 static int __init ipv6_packet_init(void)
970 {
971         dev_add_pack(&ipv6_packet_type);
972         return 0;
973 }
974
975 static void ipv6_packet_cleanup(void)
976 {
977         dev_remove_pack(&ipv6_packet_type);
978 }
979
980 static int __net_init ipv6_init_mibs(struct net *net)
981 {
982         if (snmp_mib_init((void __percpu **)net->mib.udp_stats_in6,
983                           sizeof(struct udp_mib),
984                           __alignof__(struct udp_mib)) < 0)
985                 return -ENOMEM;
986         if (snmp_mib_init((void __percpu **)net->mib.udplite_stats_in6,
987                           sizeof(struct udp_mib),
988                           __alignof__(struct udp_mib)) < 0)
989                 goto err_udplite_mib;
990         if (snmp_mib_init((void __percpu **)net->mib.ipv6_statistics,
991                           sizeof(struct ipstats_mib),
992                           __alignof__(struct ipstats_mib)) < 0)
993                 goto err_ip_mib;
994         if (snmp_mib_init((void __percpu **)net->mib.icmpv6_statistics,
995                           sizeof(struct icmpv6_mib),
996                           __alignof__(struct icmpv6_mib)) < 0)
997                 goto err_icmp_mib;
998         if (snmp_mib_init((void __percpu **)net->mib.icmpv6msg_statistics,
999                           sizeof(struct icmpv6msg_mib),
1000                           __alignof__(struct icmpv6msg_mib)) < 0)
1001                 goto err_icmpmsg_mib;
1002         return 0;
1003
1004 err_icmpmsg_mib:
1005         snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
1006 err_icmp_mib:
1007         snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
1008 err_ip_mib:
1009         snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
1010 err_udplite_mib:
1011         snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
1012         return -ENOMEM;
1013 }
1014
1015 static void ipv6_cleanup_mibs(struct net *net)
1016 {
1017         snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
1018         snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
1019         snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
1020         snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
1021         snmp_mib_free((void __percpu **)net->mib.icmpv6msg_statistics);
1022 }
1023
1024 static int __net_init inet6_net_init(struct net *net)
1025 {
1026         int err = 0;
1027
1028         net->ipv6.sysctl.bindv6only = 0;
1029         net->ipv6.sysctl.icmpv6_time = 1*HZ;
1030
1031         err = ipv6_init_mibs(net);
1032         if (err)
1033                 return err;
1034 #ifdef CONFIG_PROC_FS
1035         err = udp6_proc_init(net);
1036         if (err)
1037                 goto out;
1038         err = tcp6_proc_init(net);
1039         if (err)
1040                 goto proc_tcp6_fail;
1041         err = ac6_proc_init(net);
1042         if (err)
1043                 goto proc_ac6_fail;
1044 #endif
1045         return err;
1046
1047 #ifdef CONFIG_PROC_FS
1048 proc_ac6_fail:
1049         tcp6_proc_exit(net);
1050 proc_tcp6_fail:
1051         udp6_proc_exit(net);
1052 out:
1053         ipv6_cleanup_mibs(net);
1054         return err;
1055 #endif
1056 }
1057
1058 static void __net_exit inet6_net_exit(struct net *net)
1059 {
1060 #ifdef CONFIG_PROC_FS
1061         udp6_proc_exit(net);
1062         tcp6_proc_exit(net);
1063         ac6_proc_exit(net);
1064 #endif
1065         ipv6_cleanup_mibs(net);
1066 }
1067
1068 static struct pernet_operations inet6_net_ops = {
1069         .init = inet6_net_init,
1070         .exit = inet6_net_exit,
1071 };
1072
1073 static int __init inet6_init(void)
1074 {
1075         struct sk_buff *dummy_skb;
1076         struct list_head *r;
1077         int err = 0;
1078
1079         BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
1080
1081         /* Register the socket-side information for inet6_create.  */
1082         for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1083                 INIT_LIST_HEAD(r);
1084
1085         if (disable_ipv6_mod) {
1086                 printk(KERN_INFO
1087                        "IPv6: Loaded, but administratively disabled, "
1088                        "reboot required to enable\n");
1089                 goto out;
1090         }
1091
1092         err = proto_register(&tcpv6_prot, 1);
1093         if (err)
1094                 goto out;
1095
1096         err = proto_register(&udpv6_prot, 1);
1097         if (err)
1098                 goto out_unregister_tcp_proto;
1099
1100         err = proto_register(&udplitev6_prot, 1);
1101         if (err)
1102                 goto out_unregister_udp_proto;
1103
1104         err = proto_register(&rawv6_prot, 1);
1105         if (err)
1106                 goto out_unregister_udplite_proto;
1107
1108
1109         /* We MUST register RAW sockets before we create the ICMP6,
1110          * IGMP6, or NDISC control sockets.
1111          */
1112         err = rawv6_init();
1113         if (err)
1114                 goto out_unregister_raw_proto;
1115
1116         /* Register the family here so that the init calls below will
1117          * be able to create sockets. (?? is this dangerous ??)
1118          */
1119         err = sock_register(&inet6_family_ops);
1120         if (err)
1121                 goto out_sock_register_fail;
1122
1123 #ifdef CONFIG_SYSCTL
1124         err = ipv6_static_sysctl_register();
1125         if (err)
1126                 goto static_sysctl_fail;
1127 #endif
1128         /*
1129          *      ipngwg API draft makes clear that the correct semantics
1130          *      for TCP and UDP is to consider one TCP and UDP instance
1131          *      in a host available by both INET and INET6 APIs and
1132          *      able to communicate via both network protocols.
1133          */
1134
1135         err = register_pernet_subsys(&inet6_net_ops);
1136         if (err)
1137                 goto register_pernet_fail;
1138         err = icmpv6_init();
1139         if (err)
1140                 goto icmp_fail;
1141         err = ip6_mr_init();
1142         if (err)
1143                 goto ipmr_fail;
1144         err = ndisc_init();
1145         if (err)
1146                 goto ndisc_fail;
1147         err = igmp6_init();
1148         if (err)
1149                 goto igmp_fail;
1150         err = ipv6_netfilter_init();
1151         if (err)
1152                 goto netfilter_fail;
1153         /* Create /proc/foo6 entries. */
1154 #ifdef CONFIG_PROC_FS
1155         err = -ENOMEM;
1156         if (raw6_proc_init())
1157                 goto proc_raw6_fail;
1158         if (udplite6_proc_init())
1159                 goto proc_udplite6_fail;
1160         if (ipv6_misc_proc_init())
1161                 goto proc_misc6_fail;
1162         if (if6_proc_init())
1163                 goto proc_if6_fail;
1164 #endif
1165         err = ip6_route_init();
1166         if (err)
1167                 goto ip6_route_fail;
1168         err = ip6_flowlabel_init();
1169         if (err)
1170                 goto ip6_flowlabel_fail;
1171         err = addrconf_init();
1172         if (err)
1173                 goto addrconf_fail;
1174
1175         /* Init v6 extension headers. */
1176         err = ipv6_exthdrs_init();
1177         if (err)
1178                 goto ipv6_exthdrs_fail;
1179
1180         err = ipv6_frag_init();
1181         if (err)
1182                 goto ipv6_frag_fail;
1183
1184         /* Init v6 transport protocols. */
1185         err = udpv6_init();
1186         if (err)
1187                 goto udpv6_fail;
1188
1189         err = udplitev6_init();
1190         if (err)
1191                 goto udplitev6_fail;
1192
1193         err = tcpv6_init();
1194         if (err)
1195                 goto tcpv6_fail;
1196
1197         err = ipv6_packet_init();
1198         if (err)
1199                 goto ipv6_packet_fail;
1200
1201 #ifdef CONFIG_SYSCTL
1202         err = ipv6_sysctl_register();
1203         if (err)
1204                 goto sysctl_fail;
1205 #endif
1206 out:
1207         return err;
1208
1209 #ifdef CONFIG_SYSCTL
1210 sysctl_fail:
1211         ipv6_packet_cleanup();
1212 #endif
1213 ipv6_packet_fail:
1214         tcpv6_exit();
1215 tcpv6_fail:
1216         udplitev6_exit();
1217 udplitev6_fail:
1218         udpv6_exit();
1219 udpv6_fail:
1220         ipv6_frag_exit();
1221 ipv6_frag_fail:
1222         ipv6_exthdrs_exit();
1223 ipv6_exthdrs_fail:
1224         addrconf_cleanup();
1225 addrconf_fail:
1226         ip6_flowlabel_cleanup();
1227 ip6_flowlabel_fail:
1228         ip6_route_cleanup();
1229 ip6_route_fail:
1230 #ifdef CONFIG_PROC_FS
1231         if6_proc_exit();
1232 proc_if6_fail:
1233         ipv6_misc_proc_exit();
1234 proc_misc6_fail:
1235         udplite6_proc_exit();
1236 proc_udplite6_fail:
1237         raw6_proc_exit();
1238 proc_raw6_fail:
1239 #endif
1240         ipv6_netfilter_fini();
1241 netfilter_fail:
1242         igmp6_cleanup();
1243 igmp_fail:
1244         ndisc_cleanup();
1245 ndisc_fail:
1246         ip6_mr_cleanup();
1247 ipmr_fail:
1248         icmpv6_cleanup();
1249 icmp_fail:
1250         unregister_pernet_subsys(&inet6_net_ops);
1251 register_pernet_fail:
1252 #ifdef CONFIG_SYSCTL
1253         ipv6_static_sysctl_unregister();
1254 static_sysctl_fail:
1255 #endif
1256         sock_unregister(PF_INET6);
1257         rtnl_unregister_all(PF_INET6);
1258 out_sock_register_fail:
1259         rawv6_exit();
1260 out_unregister_raw_proto:
1261         proto_unregister(&rawv6_prot);
1262 out_unregister_udplite_proto:
1263         proto_unregister(&udplitev6_prot);
1264 out_unregister_udp_proto:
1265         proto_unregister(&udpv6_prot);
1266 out_unregister_tcp_proto:
1267         proto_unregister(&tcpv6_prot);
1268         goto out;
1269 }
1270 module_init(inet6_init);
1271
1272 static void __exit inet6_exit(void)
1273 {
1274         if (disable_ipv6_mod)
1275                 return;
1276
1277         /* First of all disallow new sockets creation. */
1278         sock_unregister(PF_INET6);
1279         /* Disallow any further netlink messages */
1280         rtnl_unregister_all(PF_INET6);
1281
1282 #ifdef CONFIG_SYSCTL
1283         ipv6_sysctl_unregister();
1284 #endif
1285         udpv6_exit();
1286         udplitev6_exit();
1287         tcpv6_exit();
1288
1289         /* Cleanup code parts. */
1290         ipv6_packet_cleanup();
1291         ipv6_frag_exit();
1292         ipv6_exthdrs_exit();
1293         addrconf_cleanup();
1294         ip6_flowlabel_cleanup();
1295         ip6_route_cleanup();
1296 #ifdef CONFIG_PROC_FS
1297
1298         /* Cleanup code parts. */
1299         if6_proc_exit();
1300         ipv6_misc_proc_exit();
1301         udplite6_proc_exit();
1302         raw6_proc_exit();
1303 #endif
1304         ipv6_netfilter_fini();
1305         igmp6_cleanup();
1306         ndisc_cleanup();
1307         ip6_mr_cleanup();
1308         icmpv6_cleanup();
1309         rawv6_exit();
1310
1311         unregister_pernet_subsys(&inet6_net_ops);
1312 #ifdef CONFIG_SYSCTL
1313         ipv6_static_sysctl_unregister();
1314 #endif
1315         proto_unregister(&rawv6_prot);
1316         proto_unregister(&udplitev6_prot);
1317         proto_unregister(&udpv6_prot);
1318         proto_unregister(&tcpv6_prot);
1319
1320         rcu_barrier(); /* Wait for completion of call_rcu()'s */
1321 }
1322 module_exit(inet6_exit);
1323
1324 MODULE_ALIAS_NETPROTO(PF_INET6);