gro: Enter slow-path if there is no tailroom
[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                 proto = ipv6_gso_pull_exthdrs(skb, proto);
883                 skb_gro_pull(skb, -skb_transport_offset(skb));
884                 skb_reset_transport_header(skb);
885                 __skb_push(skb, skb_gro_offset(skb));
886
887                 ops = rcu_dereference(inet6_protos[proto]);
888                 if (!ops || !ops->gro_receive)
889                         goto out_unlock;
890
891                 iph = ipv6_hdr(skb);
892         }
893
894         IPV6_GRO_CB(skb)->proto = proto;
895
896         flush--;
897         nlen = skb_network_header_len(skb);
898
899         for (p = *head; p; p = p->next) {
900                 struct ipv6hdr *iph2;
901
902                 if (!NAPI_GRO_CB(p)->same_flow)
903                         continue;
904
905                 iph2 = ipv6_hdr(p);
906
907                 /* All fields must match except length. */
908                 if (nlen != skb_network_header_len(p) ||
909                     memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
910                     memcmp(&iph->nexthdr, &iph2->nexthdr,
911                            nlen - offsetof(struct ipv6hdr, nexthdr))) {
912                         NAPI_GRO_CB(p)->same_flow = 0;
913                         continue;
914                 }
915
916                 NAPI_GRO_CB(p)->flush |= flush;
917         }
918
919         NAPI_GRO_CB(skb)->flush |= flush;
920
921         csum = skb->csum;
922         skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
923
924         pp = ops->gro_receive(head, skb);
925
926         skb->csum = csum;
927
928 out_unlock:
929         rcu_read_unlock();
930
931 out:
932         NAPI_GRO_CB(skb)->flush |= flush;
933
934         return pp;
935 }
936
937 static int ipv6_gro_complete(struct sk_buff *skb)
938 {
939         const struct inet6_protocol *ops;
940         struct ipv6hdr *iph = ipv6_hdr(skb);
941         int err = -ENOSYS;
942
943         iph->payload_len = htons(skb->len - skb_network_offset(skb) -
944                                  sizeof(*iph));
945
946         rcu_read_lock();
947         ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
948         if (WARN_ON(!ops || !ops->gro_complete))
949                 goto out_unlock;
950
951         err = ops->gro_complete(skb);
952
953 out_unlock:
954         rcu_read_unlock();
955
956         return err;
957 }
958
959 static struct packet_type ipv6_packet_type __read_mostly = {
960         .type = cpu_to_be16(ETH_P_IPV6),
961         .func = ipv6_rcv,
962         .gso_send_check = ipv6_gso_send_check,
963         .gso_segment = ipv6_gso_segment,
964         .gro_receive = ipv6_gro_receive,
965         .gro_complete = ipv6_gro_complete,
966 };
967
968 static int __init ipv6_packet_init(void)
969 {
970         dev_add_pack(&ipv6_packet_type);
971         return 0;
972 }
973
974 static void ipv6_packet_cleanup(void)
975 {
976         dev_remove_pack(&ipv6_packet_type);
977 }
978
979 static int __net_init ipv6_init_mibs(struct net *net)
980 {
981         if (snmp_mib_init((void __percpu **)net->mib.udp_stats_in6,
982                           sizeof(struct udp_mib),
983                           __alignof__(struct udp_mib)) < 0)
984                 return -ENOMEM;
985         if (snmp_mib_init((void __percpu **)net->mib.udplite_stats_in6,
986                           sizeof(struct udp_mib),
987                           __alignof__(struct udp_mib)) < 0)
988                 goto err_udplite_mib;
989         if (snmp_mib_init((void __percpu **)net->mib.ipv6_statistics,
990                           sizeof(struct ipstats_mib),
991                           __alignof__(struct ipstats_mib)) < 0)
992                 goto err_ip_mib;
993         if (snmp_mib_init((void __percpu **)net->mib.icmpv6_statistics,
994                           sizeof(struct icmpv6_mib),
995                           __alignof__(struct icmpv6_mib)) < 0)
996                 goto err_icmp_mib;
997         if (snmp_mib_init((void __percpu **)net->mib.icmpv6msg_statistics,
998                           sizeof(struct icmpv6msg_mib),
999                           __alignof__(struct icmpv6msg_mib)) < 0)
1000                 goto err_icmpmsg_mib;
1001         return 0;
1002
1003 err_icmpmsg_mib:
1004         snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
1005 err_icmp_mib:
1006         snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
1007 err_ip_mib:
1008         snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
1009 err_udplite_mib:
1010         snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
1011         return -ENOMEM;
1012 }
1013
1014 static void ipv6_cleanup_mibs(struct net *net)
1015 {
1016         snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
1017         snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
1018         snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
1019         snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
1020         snmp_mib_free((void __percpu **)net->mib.icmpv6msg_statistics);
1021 }
1022
1023 static int __net_init inet6_net_init(struct net *net)
1024 {
1025         int err = 0;
1026
1027         net->ipv6.sysctl.bindv6only = 0;
1028         net->ipv6.sysctl.icmpv6_time = 1*HZ;
1029
1030         err = ipv6_init_mibs(net);
1031         if (err)
1032                 return err;
1033 #ifdef CONFIG_PROC_FS
1034         err = udp6_proc_init(net);
1035         if (err)
1036                 goto out;
1037         err = tcp6_proc_init(net);
1038         if (err)
1039                 goto proc_tcp6_fail;
1040         err = ac6_proc_init(net);
1041         if (err)
1042                 goto proc_ac6_fail;
1043 #endif
1044         return err;
1045
1046 #ifdef CONFIG_PROC_FS
1047 proc_ac6_fail:
1048         tcp6_proc_exit(net);
1049 proc_tcp6_fail:
1050         udp6_proc_exit(net);
1051 out:
1052         ipv6_cleanup_mibs(net);
1053         return err;
1054 #endif
1055 }
1056
1057 static void __net_exit inet6_net_exit(struct net *net)
1058 {
1059 #ifdef CONFIG_PROC_FS
1060         udp6_proc_exit(net);
1061         tcp6_proc_exit(net);
1062         ac6_proc_exit(net);
1063 #endif
1064         ipv6_cleanup_mibs(net);
1065 }
1066
1067 static struct pernet_operations inet6_net_ops = {
1068         .init = inet6_net_init,
1069         .exit = inet6_net_exit,
1070 };
1071
1072 static int __init inet6_init(void)
1073 {
1074         struct sk_buff *dummy_skb;
1075         struct list_head *r;
1076         int err = 0;
1077
1078         BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
1079
1080         /* Register the socket-side information for inet6_create.  */
1081         for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1082                 INIT_LIST_HEAD(r);
1083
1084         if (disable_ipv6_mod) {
1085                 printk(KERN_INFO
1086                        "IPv6: Loaded, but administratively disabled, "
1087                        "reboot required to enable\n");
1088                 goto out;
1089         }
1090
1091         err = proto_register(&tcpv6_prot, 1);
1092         if (err)
1093                 goto out;
1094
1095         err = proto_register(&udpv6_prot, 1);
1096         if (err)
1097                 goto out_unregister_tcp_proto;
1098
1099         err = proto_register(&udplitev6_prot, 1);
1100         if (err)
1101                 goto out_unregister_udp_proto;
1102
1103         err = proto_register(&rawv6_prot, 1);
1104         if (err)
1105                 goto out_unregister_udplite_proto;
1106
1107
1108         /* We MUST register RAW sockets before we create the ICMP6,
1109          * IGMP6, or NDISC control sockets.
1110          */
1111         err = rawv6_init();
1112         if (err)
1113                 goto out_unregister_raw_proto;
1114
1115         /* Register the family here so that the init calls below will
1116          * be able to create sockets. (?? is this dangerous ??)
1117          */
1118         err = sock_register(&inet6_family_ops);
1119         if (err)
1120                 goto out_sock_register_fail;
1121
1122 #ifdef CONFIG_SYSCTL
1123         err = ipv6_static_sysctl_register();
1124         if (err)
1125                 goto static_sysctl_fail;
1126 #endif
1127         /*
1128          *      ipngwg API draft makes clear that the correct semantics
1129          *      for TCP and UDP is to consider one TCP and UDP instance
1130          *      in a host available by both INET and INET6 APIs and
1131          *      able to communicate via both network protocols.
1132          */
1133
1134         err = register_pernet_subsys(&inet6_net_ops);
1135         if (err)
1136                 goto register_pernet_fail;
1137         err = icmpv6_init();
1138         if (err)
1139                 goto icmp_fail;
1140         err = ip6_mr_init();
1141         if (err)
1142                 goto ipmr_fail;
1143         err = ndisc_init();
1144         if (err)
1145                 goto ndisc_fail;
1146         err = igmp6_init();
1147         if (err)
1148                 goto igmp_fail;
1149         err = ipv6_netfilter_init();
1150         if (err)
1151                 goto netfilter_fail;
1152         /* Create /proc/foo6 entries. */
1153 #ifdef CONFIG_PROC_FS
1154         err = -ENOMEM;
1155         if (raw6_proc_init())
1156                 goto proc_raw6_fail;
1157         if (udplite6_proc_init())
1158                 goto proc_udplite6_fail;
1159         if (ipv6_misc_proc_init())
1160                 goto proc_misc6_fail;
1161         if (if6_proc_init())
1162                 goto proc_if6_fail;
1163 #endif
1164         err = ip6_route_init();
1165         if (err)
1166                 goto ip6_route_fail;
1167         err = ip6_flowlabel_init();
1168         if (err)
1169                 goto ip6_flowlabel_fail;
1170         err = addrconf_init();
1171         if (err)
1172                 goto addrconf_fail;
1173
1174         /* Init v6 extension headers. */
1175         err = ipv6_exthdrs_init();
1176         if (err)
1177                 goto ipv6_exthdrs_fail;
1178
1179         err = ipv6_frag_init();
1180         if (err)
1181                 goto ipv6_frag_fail;
1182
1183         /* Init v6 transport protocols. */
1184         err = udpv6_init();
1185         if (err)
1186                 goto udpv6_fail;
1187
1188         err = udplitev6_init();
1189         if (err)
1190                 goto udplitev6_fail;
1191
1192         err = tcpv6_init();
1193         if (err)
1194                 goto tcpv6_fail;
1195
1196         err = ipv6_packet_init();
1197         if (err)
1198                 goto ipv6_packet_fail;
1199
1200 #ifdef CONFIG_SYSCTL
1201         err = ipv6_sysctl_register();
1202         if (err)
1203                 goto sysctl_fail;
1204 #endif
1205 out:
1206         return err;
1207
1208 #ifdef CONFIG_SYSCTL
1209 sysctl_fail:
1210         ipv6_packet_cleanup();
1211 #endif
1212 ipv6_packet_fail:
1213         tcpv6_exit();
1214 tcpv6_fail:
1215         udplitev6_exit();
1216 udplitev6_fail:
1217         udpv6_exit();
1218 udpv6_fail:
1219         ipv6_frag_exit();
1220 ipv6_frag_fail:
1221         ipv6_exthdrs_exit();
1222 ipv6_exthdrs_fail:
1223         addrconf_cleanup();
1224 addrconf_fail:
1225         ip6_flowlabel_cleanup();
1226 ip6_flowlabel_fail:
1227         ip6_route_cleanup();
1228 ip6_route_fail:
1229 #ifdef CONFIG_PROC_FS
1230         if6_proc_exit();
1231 proc_if6_fail:
1232         ipv6_misc_proc_exit();
1233 proc_misc6_fail:
1234         udplite6_proc_exit();
1235 proc_udplite6_fail:
1236         raw6_proc_exit();
1237 proc_raw6_fail:
1238 #endif
1239         ipv6_netfilter_fini();
1240 netfilter_fail:
1241         igmp6_cleanup();
1242 igmp_fail:
1243         ndisc_cleanup();
1244 ndisc_fail:
1245         ip6_mr_cleanup();
1246 ipmr_fail:
1247         icmpv6_cleanup();
1248 icmp_fail:
1249         unregister_pernet_subsys(&inet6_net_ops);
1250 register_pernet_fail:
1251 #ifdef CONFIG_SYSCTL
1252         ipv6_static_sysctl_unregister();
1253 static_sysctl_fail:
1254 #endif
1255         sock_unregister(PF_INET6);
1256         rtnl_unregister_all(PF_INET6);
1257 out_sock_register_fail:
1258         rawv6_exit();
1259 out_unregister_raw_proto:
1260         proto_unregister(&rawv6_prot);
1261 out_unregister_udplite_proto:
1262         proto_unregister(&udplitev6_prot);
1263 out_unregister_udp_proto:
1264         proto_unregister(&udpv6_prot);
1265 out_unregister_tcp_proto:
1266         proto_unregister(&tcpv6_prot);
1267         goto out;
1268 }
1269 module_init(inet6_init);
1270
1271 static void __exit inet6_exit(void)
1272 {
1273         if (disable_ipv6_mod)
1274                 return;
1275
1276         /* First of all disallow new sockets creation. */
1277         sock_unregister(PF_INET6);
1278         /* Disallow any further netlink messages */
1279         rtnl_unregister_all(PF_INET6);
1280
1281 #ifdef CONFIG_SYSCTL
1282         ipv6_sysctl_unregister();
1283 #endif
1284         udpv6_exit();
1285         udplitev6_exit();
1286         tcpv6_exit();
1287
1288         /* Cleanup code parts. */
1289         ipv6_packet_cleanup();
1290         ipv6_frag_exit();
1291         ipv6_exthdrs_exit();
1292         addrconf_cleanup();
1293         ip6_flowlabel_cleanup();
1294         ip6_route_cleanup();
1295 #ifdef CONFIG_PROC_FS
1296
1297         /* Cleanup code parts. */
1298         if6_proc_exit();
1299         ipv6_misc_proc_exit();
1300         udplite6_proc_exit();
1301         raw6_proc_exit();
1302 #endif
1303         ipv6_netfilter_fini();
1304         igmp6_cleanup();
1305         ndisc_cleanup();
1306         ip6_mr_cleanup();
1307         icmpv6_cleanup();
1308         rawv6_exit();
1309
1310         unregister_pernet_subsys(&inet6_net_ops);
1311 #ifdef CONFIG_SYSCTL
1312         ipv6_static_sysctl_unregister();
1313 #endif
1314         proto_unregister(&rawv6_prot);
1315         proto_unregister(&udplitev6_prot);
1316         proto_unregister(&udpv6_prot);
1317         proto_unregister(&tcpv6_prot);
1318
1319         rcu_barrier(); /* Wait for completion of call_rcu()'s */
1320 }
1321 module_exit(inet6_exit);
1322
1323 MODULE_ALIAS_NETPROTO(PF_INET6);