dm snapshot: fix header corruption race on invalidation
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / af_inet6.c
blob55e315a5f0af7392cfb43154059eeae47e8c6694
1 /*
2 * PF_INET6 socket protocol family
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Adapted from linux/net/ipv4/af_inet.c
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
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.
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>
40 #include <linux/inet.h>
41 #include <linux/netdevice.h>
42 #include <linux/icmpv6.h>
43 #include <linux/netfilter_ipv6.h>
45 #include <net/ip.h>
46 #include <net/ipv6.h>
47 #include <net/udp.h>
48 #include <net/udplite.h>
49 #include <net/tcp.h>
50 #include <net/ipip.h>
51 #include <net/protocol.h>
52 #include <net/inet_common.h>
53 #include <net/route.h>
54 #include <net/transp_v6.h>
55 #include <net/ip6_route.h>
56 #include <net/addrconf.h>
57 #ifdef CONFIG_IPV6_TUNNEL
58 #include <net/ip6_tunnel.h>
59 #endif
61 #include <asm/uaccess.h>
62 #include <asm/system.h>
63 #include <linux/mroute6.h>
65 MODULE_AUTHOR("Cast of dozens");
66 MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
67 MODULE_LICENSE("GPL");
69 /* The inetsw6 table contains everything that inet6_create needs to
70 * build a new socket.
72 static struct list_head inetsw6[SOCK_MAX];
73 static DEFINE_SPINLOCK(inetsw6_lock);
75 static int disable_ipv6 = 0;
76 module_param_named(disable, disable_ipv6, int, 0);
77 MODULE_PARM_DESC(disable, "Disable IPv6 such that it is non-functional");
79 static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
81 const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
83 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
86 static int inet6_create(struct net *net, struct socket *sock, int protocol)
88 struct inet_sock *inet;
89 struct ipv6_pinfo *np;
90 struct sock *sk;
91 struct inet_protosw *answer;
92 struct proto *answer_prot;
93 unsigned char answer_flags;
94 char answer_no_check;
95 int try_loading_module = 0;
96 int err;
98 if (sock->type != SOCK_RAW &&
99 sock->type != SOCK_DGRAM &&
100 !inet_ehash_secret)
101 build_ehash_secret();
103 /* Look for the requested type/protocol pair. */
104 lookup_protocol:
105 err = -ESOCKTNOSUPPORT;
106 rcu_read_lock();
107 list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
109 err = 0;
110 /* Check the non-wild match. */
111 if (protocol == answer->protocol) {
112 if (protocol != IPPROTO_IP)
113 break;
114 } else {
115 /* Check for the two wild cases. */
116 if (IPPROTO_IP == protocol) {
117 protocol = answer->protocol;
118 break;
120 if (IPPROTO_IP == answer->protocol)
121 break;
123 err = -EPROTONOSUPPORT;
126 if (err) {
127 if (try_loading_module < 2) {
128 rcu_read_unlock();
130 * Be more specific, e.g. net-pf-10-proto-132-type-1
131 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
133 if (++try_loading_module == 1)
134 request_module("net-pf-%d-proto-%d-type-%d",
135 PF_INET6, protocol, sock->type);
137 * Fall back to generic, e.g. net-pf-10-proto-132
138 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
140 else
141 request_module("net-pf-%d-proto-%d",
142 PF_INET6, protocol);
143 goto lookup_protocol;
144 } else
145 goto out_rcu_unlock;
148 err = -EPERM;
149 if (answer->capability > 0 && !capable(answer->capability))
150 goto out_rcu_unlock;
152 sock->ops = answer->ops;
153 answer_prot = answer->prot;
154 answer_no_check = answer->no_check;
155 answer_flags = answer->flags;
156 rcu_read_unlock();
158 WARN_ON(answer_prot->slab == NULL);
160 err = -ENOBUFS;
161 sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
162 if (sk == NULL)
163 goto out;
165 sock_init_data(sock, sk);
167 err = 0;
168 sk->sk_no_check = answer_no_check;
169 if (INET_PROTOSW_REUSE & answer_flags)
170 sk->sk_reuse = 1;
172 inet = inet_sk(sk);
173 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
175 if (SOCK_RAW == sock->type) {
176 inet->num = protocol;
177 if (IPPROTO_RAW == protocol)
178 inet->hdrincl = 1;
181 sk->sk_destruct = inet_sock_destruct;
182 sk->sk_family = PF_INET6;
183 sk->sk_protocol = protocol;
185 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
187 inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
188 np->hop_limit = -1;
189 np->mcast_hops = -1;
190 np->mc_loop = 1;
191 np->pmtudisc = IPV6_PMTUDISC_WANT;
192 np->ipv6only = net->ipv6.sysctl.bindv6only;
194 /* Init the ipv4 part of the socket since we can have sockets
195 * using v6 API for ipv4.
197 inet->uc_ttl = -1;
199 inet->mc_loop = 1;
200 inet->mc_ttl = 1;
201 inet->mc_index = 0;
202 inet->mc_list = NULL;
204 if (ipv4_config.no_pmtu_disc)
205 inet->pmtudisc = IP_PMTUDISC_DONT;
206 else
207 inet->pmtudisc = IP_PMTUDISC_WANT;
209 * Increment only the relevant sk_prot->socks debug field, this changes
210 * the previous behaviour of incrementing both the equivalent to
211 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
213 * This allows better debug granularity as we'll know exactly how many
214 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
215 * transport protocol socks. -acme
217 sk_refcnt_debug_inc(sk);
219 if (inet->num) {
220 /* It assumes that any protocol which allows
221 * the user to assign a number at socket
222 * creation time automatically shares.
224 inet->sport = htons(inet->num);
225 sk->sk_prot->hash(sk);
227 if (sk->sk_prot->init) {
228 err = sk->sk_prot->init(sk);
229 if (err) {
230 sk_common_release(sk);
231 goto out;
234 out:
235 return err;
236 out_rcu_unlock:
237 rcu_read_unlock();
238 goto out;
242 /* bind for INET6 API */
243 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
245 struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
246 struct sock *sk = sock->sk;
247 struct inet_sock *inet = inet_sk(sk);
248 struct ipv6_pinfo *np = inet6_sk(sk);
249 struct net *net = sock_net(sk);
250 __be32 v4addr = 0;
251 unsigned short snum;
252 int addr_type = 0;
253 int err = 0;
255 /* If the socket has its own bind function then use it. */
256 if (sk->sk_prot->bind)
257 return sk->sk_prot->bind(sk, uaddr, addr_len);
259 if (addr_len < SIN6_LEN_RFC2133)
260 return -EINVAL;
261 addr_type = ipv6_addr_type(&addr->sin6_addr);
262 if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
263 return -EINVAL;
265 snum = ntohs(addr->sin6_port);
266 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
267 return -EACCES;
269 lock_sock(sk);
271 /* Check these errors (active socket, double bind). */
272 if (sk->sk_state != TCP_CLOSE || inet->num) {
273 err = -EINVAL;
274 goto out;
277 /* Check if the address belongs to the host. */
278 if (addr_type == IPV6_ADDR_MAPPED) {
279 int chk_addr_ret;
281 /* Binding to v4-mapped address on a v6-only socket
282 * makes no sense
284 if (np->ipv6only) {
285 err = -EINVAL;
286 goto out;
289 /* Reproduce AF_INET checks to make the bindings consitant */
290 v4addr = addr->sin6_addr.s6_addr32[3];
291 chk_addr_ret = inet_addr_type(net, v4addr);
292 if (!sysctl_ip_nonlocal_bind &&
293 !(inet->freebind || inet->transparent) &&
294 v4addr != htonl(INADDR_ANY) &&
295 chk_addr_ret != RTN_LOCAL &&
296 chk_addr_ret != RTN_MULTICAST &&
297 chk_addr_ret != RTN_BROADCAST) {
298 err = -EADDRNOTAVAIL;
299 goto out;
301 } else {
302 if (addr_type != IPV6_ADDR_ANY) {
303 struct net_device *dev = NULL;
305 if (addr_type & IPV6_ADDR_LINKLOCAL) {
306 if (addr_len >= sizeof(struct sockaddr_in6) &&
307 addr->sin6_scope_id) {
308 /* Override any existing binding, if another one
309 * is supplied by user.
311 sk->sk_bound_dev_if = addr->sin6_scope_id;
314 /* Binding to link-local address requires an interface */
315 if (!sk->sk_bound_dev_if) {
316 err = -EINVAL;
317 goto out;
319 dev = dev_get_by_index(net, sk->sk_bound_dev_if);
320 if (!dev) {
321 err = -ENODEV;
322 goto out;
326 /* ipv4 addr of the socket is invalid. Only the
327 * unspecified and mapped address have a v4 equivalent.
329 v4addr = LOOPBACK4_IPV6;
330 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
331 if (!ipv6_chk_addr(net, &addr->sin6_addr,
332 dev, 0)) {
333 if (dev)
334 dev_put(dev);
335 err = -EADDRNOTAVAIL;
336 goto out;
339 if (dev)
340 dev_put(dev);
344 inet->rcv_saddr = v4addr;
345 inet->saddr = v4addr;
347 ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
349 if (!(addr_type & IPV6_ADDR_MULTICAST))
350 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
352 /* Make sure we are allowed to bind here. */
353 if (sk->sk_prot->get_port(sk, snum)) {
354 inet_reset_saddr(sk);
355 err = -EADDRINUSE;
356 goto out;
359 if (addr_type != IPV6_ADDR_ANY) {
360 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
361 if (addr_type != IPV6_ADDR_MAPPED)
362 np->ipv6only = 1;
364 if (snum)
365 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
366 inet->sport = htons(inet->num);
367 inet->dport = 0;
368 inet->daddr = 0;
369 out:
370 release_sock(sk);
371 return err;
374 EXPORT_SYMBOL(inet6_bind);
376 int inet6_release(struct socket *sock)
378 struct sock *sk = sock->sk;
380 if (sk == NULL)
381 return -EINVAL;
383 /* Free mc lists */
384 ipv6_sock_mc_close(sk);
386 /* Free ac lists */
387 ipv6_sock_ac_close(sk);
389 return inet_release(sock);
392 EXPORT_SYMBOL(inet6_release);
394 void inet6_destroy_sock(struct sock *sk)
396 struct ipv6_pinfo *np = inet6_sk(sk);
397 struct sk_buff *skb;
398 struct ipv6_txoptions *opt;
400 /* Release rx options */
402 if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
403 kfree_skb(skb);
405 /* Free flowlabels */
406 fl6_free_socklist(sk);
408 /* Free tx options */
410 if ((opt = xchg(&np->opt, NULL)) != NULL)
411 sock_kfree_s(sk, opt, opt->tot_len);
414 EXPORT_SYMBOL_GPL(inet6_destroy_sock);
417 * This does both peername and sockname.
420 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
421 int *uaddr_len, int peer)
423 struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
424 struct sock *sk = sock->sk;
425 struct inet_sock *inet = inet_sk(sk);
426 struct ipv6_pinfo *np = inet6_sk(sk);
428 sin->sin6_family = AF_INET6;
429 sin->sin6_flowinfo = 0;
430 sin->sin6_scope_id = 0;
431 if (peer) {
432 if (!inet->dport)
433 return -ENOTCONN;
434 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
435 peer == 1)
436 return -ENOTCONN;
437 sin->sin6_port = inet->dport;
438 ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
439 if (np->sndflow)
440 sin->sin6_flowinfo = np->flow_label;
441 } else {
442 if (ipv6_addr_any(&np->rcv_saddr))
443 ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
444 else
445 ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
447 sin->sin6_port = inet->sport;
449 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
450 sin->sin6_scope_id = sk->sk_bound_dev_if;
451 *uaddr_len = sizeof(*sin);
452 return(0);
455 EXPORT_SYMBOL(inet6_getname);
457 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
459 struct sock *sk = sock->sk;
460 struct net *net = sock_net(sk);
462 switch(cmd)
464 case SIOCGSTAMP:
465 return sock_get_timestamp(sk, (struct timeval __user *)arg);
467 case SIOCGSTAMPNS:
468 return sock_get_timestampns(sk, (struct timespec __user *)arg);
470 case SIOCADDRT:
471 case SIOCDELRT:
473 return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
475 case SIOCSIFADDR:
476 return addrconf_add_ifaddr(net, (void __user *) arg);
477 case SIOCDIFADDR:
478 return addrconf_del_ifaddr(net, (void __user *) arg);
479 case SIOCSIFDSTADDR:
480 return addrconf_set_dstaddr(net, (void __user *) arg);
481 default:
482 if (!sk->sk_prot->ioctl)
483 return -ENOIOCTLCMD;
484 return sk->sk_prot->ioctl(sk, cmd, arg);
486 /*NOTREACHED*/
487 return(0);
490 EXPORT_SYMBOL(inet6_ioctl);
492 const struct proto_ops inet6_stream_ops = {
493 .family = PF_INET6,
494 .owner = THIS_MODULE,
495 .release = inet6_release,
496 .bind = inet6_bind,
497 .connect = inet_stream_connect, /* ok */
498 .socketpair = sock_no_socketpair, /* a do nothing */
499 .accept = inet_accept, /* ok */
500 .getname = inet6_getname,
501 .poll = tcp_poll, /* ok */
502 .ioctl = inet6_ioctl, /* must change */
503 .listen = inet_listen, /* ok */
504 .shutdown = inet_shutdown, /* ok */
505 .setsockopt = sock_common_setsockopt, /* ok */
506 .getsockopt = sock_common_getsockopt, /* ok */
507 .sendmsg = tcp_sendmsg, /* ok */
508 .recvmsg = sock_common_recvmsg, /* ok */
509 .mmap = sock_no_mmap,
510 .sendpage = tcp_sendpage,
511 .splice_read = tcp_splice_read,
512 #ifdef CONFIG_COMPAT
513 .compat_setsockopt = compat_sock_common_setsockopt,
514 .compat_getsockopt = compat_sock_common_getsockopt,
515 #endif
518 const struct proto_ops inet6_dgram_ops = {
519 .family = PF_INET6,
520 .owner = THIS_MODULE,
521 .release = inet6_release,
522 .bind = inet6_bind,
523 .connect = inet_dgram_connect, /* ok */
524 .socketpair = sock_no_socketpair, /* a do nothing */
525 .accept = sock_no_accept, /* a do nothing */
526 .getname = inet6_getname,
527 .poll = udp_poll, /* ok */
528 .ioctl = inet6_ioctl, /* must change */
529 .listen = sock_no_listen, /* ok */
530 .shutdown = inet_shutdown, /* ok */
531 .setsockopt = sock_common_setsockopt, /* ok */
532 .getsockopt = sock_common_getsockopt, /* ok */
533 .sendmsg = inet_sendmsg, /* ok */
534 .recvmsg = sock_common_recvmsg, /* ok */
535 .mmap = sock_no_mmap,
536 .sendpage = sock_no_sendpage,
537 #ifdef CONFIG_COMPAT
538 .compat_setsockopt = compat_sock_common_setsockopt,
539 .compat_getsockopt = compat_sock_common_getsockopt,
540 #endif
543 static struct net_proto_family inet6_family_ops = {
544 .family = PF_INET6,
545 .create = inet6_create,
546 .owner = THIS_MODULE,
549 int inet6_register_protosw(struct inet_protosw *p)
551 struct list_head *lh;
552 struct inet_protosw *answer;
553 struct list_head *last_perm;
554 int protocol = p->protocol;
555 int ret;
557 spin_lock_bh(&inetsw6_lock);
559 ret = -EINVAL;
560 if (p->type >= SOCK_MAX)
561 goto out_illegal;
563 /* If we are trying to override a permanent protocol, bail. */
564 answer = NULL;
565 ret = -EPERM;
566 last_perm = &inetsw6[p->type];
567 list_for_each(lh, &inetsw6[p->type]) {
568 answer = list_entry(lh, struct inet_protosw, list);
570 /* Check only the non-wild match. */
571 if (INET_PROTOSW_PERMANENT & answer->flags) {
572 if (protocol == answer->protocol)
573 break;
574 last_perm = lh;
577 answer = NULL;
579 if (answer)
580 goto out_permanent;
582 /* Add the new entry after the last permanent entry if any, so that
583 * the new entry does not override a permanent entry when matched with
584 * a wild-card protocol. But it is allowed to override any existing
585 * non-permanent entry. This means that when we remove this entry, the
586 * system automatically returns to the old behavior.
588 list_add_rcu(&p->list, last_perm);
589 ret = 0;
590 out:
591 spin_unlock_bh(&inetsw6_lock);
592 return ret;
594 out_permanent:
595 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
596 protocol);
597 goto out;
599 out_illegal:
600 printk(KERN_ERR
601 "Ignoring attempt to register invalid socket type %d.\n",
602 p->type);
603 goto out;
606 EXPORT_SYMBOL(inet6_register_protosw);
608 void
609 inet6_unregister_protosw(struct inet_protosw *p)
611 if (INET_PROTOSW_PERMANENT & p->flags) {
612 printk(KERN_ERR
613 "Attempt to unregister permanent protocol %d.\n",
614 p->protocol);
615 } else {
616 spin_lock_bh(&inetsw6_lock);
617 list_del_rcu(&p->list);
618 spin_unlock_bh(&inetsw6_lock);
620 synchronize_net();
624 EXPORT_SYMBOL(inet6_unregister_protosw);
626 int inet6_sk_rebuild_header(struct sock *sk)
628 int err;
629 struct dst_entry *dst;
630 struct ipv6_pinfo *np = inet6_sk(sk);
632 dst = __sk_dst_check(sk, np->dst_cookie);
634 if (dst == NULL) {
635 struct inet_sock *inet = inet_sk(sk);
636 struct in6_addr *final_p = NULL, final;
637 struct flowi fl;
639 memset(&fl, 0, sizeof(fl));
640 fl.proto = sk->sk_protocol;
641 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
642 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
643 fl.fl6_flowlabel = np->flow_label;
644 fl.oif = sk->sk_bound_dev_if;
645 fl.fl_ip_dport = inet->dport;
646 fl.fl_ip_sport = inet->sport;
647 security_sk_classify_flow(sk, &fl);
649 if (np->opt && np->opt->srcrt) {
650 struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
651 ipv6_addr_copy(&final, &fl.fl6_dst);
652 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
653 final_p = &final;
656 err = ip6_dst_lookup(sk, &dst, &fl);
657 if (err) {
658 sk->sk_route_caps = 0;
659 return err;
661 if (final_p)
662 ipv6_addr_copy(&fl.fl6_dst, final_p);
664 if ((err = xfrm_lookup(sock_net(sk), &dst, &fl, sk, 0)) < 0) {
665 sk->sk_err_soft = -err;
666 return err;
669 __ip6_dst_store(sk, dst, NULL, NULL);
672 return 0;
675 EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
677 int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
679 struct ipv6_pinfo *np = inet6_sk(sk);
680 struct inet6_skb_parm *opt = IP6CB(skb);
682 if (np->rxopt.all) {
683 if ((opt->hop && (np->rxopt.bits.hopopts ||
684 np->rxopt.bits.ohopopts)) ||
685 ((IPV6_FLOWINFO_MASK &
686 *(__be32 *)skb_network_header(skb)) &&
687 np->rxopt.bits.rxflow) ||
688 (opt->srcrt && (np->rxopt.bits.srcrt ||
689 np->rxopt.bits.osrcrt)) ||
690 ((opt->dst1 || opt->dst0) &&
691 (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
692 return 1;
694 return 0;
697 EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
699 static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
701 struct inet6_protocol *ops = NULL;
703 for (;;) {
704 struct ipv6_opt_hdr *opth;
705 int len;
707 if (proto != NEXTHDR_HOP) {
708 ops = rcu_dereference(inet6_protos[proto]);
710 if (unlikely(!ops))
711 break;
713 if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
714 break;
717 if (unlikely(!pskb_may_pull(skb, 8)))
718 break;
720 opth = (void *)skb->data;
721 len = ipv6_optlen(opth);
723 if (unlikely(!pskb_may_pull(skb, len)))
724 break;
726 proto = opth->nexthdr;
727 __skb_pull(skb, len);
730 return proto;
733 static int ipv6_gso_send_check(struct sk_buff *skb)
735 struct ipv6hdr *ipv6h;
736 struct inet6_protocol *ops;
737 int err = -EINVAL;
739 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
740 goto out;
742 ipv6h = ipv6_hdr(skb);
743 __skb_pull(skb, sizeof(*ipv6h));
744 err = -EPROTONOSUPPORT;
746 rcu_read_lock();
747 ops = rcu_dereference(inet6_protos[
748 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
750 if (likely(ops && ops->gso_send_check)) {
751 skb_reset_transport_header(skb);
752 err = ops->gso_send_check(skb);
754 rcu_read_unlock();
756 out:
757 return err;
760 static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
762 struct sk_buff *segs = ERR_PTR(-EINVAL);
763 struct ipv6hdr *ipv6h;
764 struct inet6_protocol *ops;
766 if (!(features & NETIF_F_V6_CSUM))
767 features &= ~NETIF_F_SG;
769 if (unlikely(skb_shinfo(skb)->gso_type &
770 ~(SKB_GSO_UDP |
771 SKB_GSO_DODGY |
772 SKB_GSO_TCP_ECN |
773 SKB_GSO_TCPV6 |
774 0)))
775 goto out;
777 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
778 goto out;
780 ipv6h = ipv6_hdr(skb);
781 __skb_pull(skb, sizeof(*ipv6h));
782 segs = ERR_PTR(-EPROTONOSUPPORT);
784 rcu_read_lock();
785 ops = rcu_dereference(inet6_protos[
786 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
788 if (likely(ops && ops->gso_segment)) {
789 skb_reset_transport_header(skb);
790 segs = ops->gso_segment(skb, features);
792 rcu_read_unlock();
794 if (unlikely(IS_ERR(segs)))
795 goto out;
797 for (skb = segs; skb; skb = skb->next) {
798 ipv6h = ipv6_hdr(skb);
799 ipv6h->payload_len = htons(skb->len - skb->mac_len -
800 sizeof(*ipv6h));
803 out:
804 return segs;
807 struct ipv6_gro_cb {
808 struct napi_gro_cb napi;
809 int proto;
812 #define IPV6_GRO_CB(skb) ((struct ipv6_gro_cb *)(skb)->cb)
814 static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
815 struct sk_buff *skb)
817 struct inet6_protocol *ops;
818 struct sk_buff **pp = NULL;
819 struct sk_buff *p;
820 struct ipv6hdr *iph;
821 unsigned int nlen;
822 int flush = 1;
823 int proto;
824 __wsum csum;
826 iph = skb_gro_header(skb, sizeof(*iph));
827 if (unlikely(!iph))
828 goto out;
830 skb_gro_pull(skb, sizeof(*iph));
831 skb_set_transport_header(skb, skb_gro_offset(skb));
833 flush += ntohs(iph->payload_len) != skb_gro_len(skb);
835 rcu_read_lock();
836 proto = iph->nexthdr;
837 ops = rcu_dereference(inet6_protos[proto]);
838 if (!ops || !ops->gro_receive) {
839 __pskb_pull(skb, skb_gro_offset(skb));
840 proto = ipv6_gso_pull_exthdrs(skb, proto);
841 skb_gro_pull(skb, -skb_transport_offset(skb));
842 skb_reset_transport_header(skb);
843 __skb_push(skb, skb_gro_offset(skb));
845 if (!ops || !ops->gro_receive)
846 goto out_unlock;
848 iph = ipv6_hdr(skb);
851 IPV6_GRO_CB(skb)->proto = proto;
853 flush--;
854 nlen = skb_network_header_len(skb);
856 for (p = *head; p; p = p->next) {
857 struct ipv6hdr *iph2;
859 if (!NAPI_GRO_CB(p)->same_flow)
860 continue;
862 iph2 = ipv6_hdr(p);
864 /* All fields must match except length. */
865 if (nlen != skb_network_header_len(p) ||
866 memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
867 memcmp(&iph->nexthdr, &iph2->nexthdr,
868 nlen - offsetof(struct ipv6hdr, nexthdr))) {
869 NAPI_GRO_CB(p)->same_flow = 0;
870 continue;
873 NAPI_GRO_CB(p)->flush |= flush;
876 NAPI_GRO_CB(skb)->flush |= flush;
878 csum = skb->csum;
879 skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
881 pp = ops->gro_receive(head, skb);
883 skb->csum = csum;
885 out_unlock:
886 rcu_read_unlock();
888 out:
889 NAPI_GRO_CB(skb)->flush |= flush;
891 return pp;
894 static int ipv6_gro_complete(struct sk_buff *skb)
896 struct inet6_protocol *ops;
897 struct ipv6hdr *iph = ipv6_hdr(skb);
898 int err = -ENOSYS;
900 iph->payload_len = htons(skb->len - skb_network_offset(skb) -
901 sizeof(*iph));
903 rcu_read_lock();
904 ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
905 if (WARN_ON(!ops || !ops->gro_complete))
906 goto out_unlock;
908 err = ops->gro_complete(skb);
910 out_unlock:
911 rcu_read_unlock();
913 return err;
916 static struct packet_type ipv6_packet_type __read_mostly = {
917 .type = cpu_to_be16(ETH_P_IPV6),
918 .func = ipv6_rcv,
919 .gso_send_check = ipv6_gso_send_check,
920 .gso_segment = ipv6_gso_segment,
921 .gro_receive = ipv6_gro_receive,
922 .gro_complete = ipv6_gro_complete,
925 static int __init ipv6_packet_init(void)
927 dev_add_pack(&ipv6_packet_type);
928 return 0;
931 static void ipv6_packet_cleanup(void)
933 dev_remove_pack(&ipv6_packet_type);
936 static int __net_init ipv6_init_mibs(struct net *net)
938 if (snmp_mib_init((void **)net->mib.udp_stats_in6,
939 sizeof (struct udp_mib)) < 0)
940 return -ENOMEM;
941 if (snmp_mib_init((void **)net->mib.udplite_stats_in6,
942 sizeof (struct udp_mib)) < 0)
943 goto err_udplite_mib;
944 if (snmp_mib_init((void **)net->mib.ipv6_statistics,
945 sizeof(struct ipstats_mib)) < 0)
946 goto err_ip_mib;
947 if (snmp_mib_init((void **)net->mib.icmpv6_statistics,
948 sizeof(struct icmpv6_mib)) < 0)
949 goto err_icmp_mib;
950 if (snmp_mib_init((void **)net->mib.icmpv6msg_statistics,
951 sizeof(struct icmpv6msg_mib)) < 0)
952 goto err_icmpmsg_mib;
953 return 0;
955 err_icmpmsg_mib:
956 snmp_mib_free((void **)net->mib.icmpv6_statistics);
957 err_icmp_mib:
958 snmp_mib_free((void **)net->mib.ipv6_statistics);
959 err_ip_mib:
960 snmp_mib_free((void **)net->mib.udplite_stats_in6);
961 err_udplite_mib:
962 snmp_mib_free((void **)net->mib.udp_stats_in6);
963 return -ENOMEM;
966 static void __net_exit ipv6_cleanup_mibs(struct net *net)
968 snmp_mib_free((void **)net->mib.udp_stats_in6);
969 snmp_mib_free((void **)net->mib.udplite_stats_in6);
970 snmp_mib_free((void **)net->mib.ipv6_statistics);
971 snmp_mib_free((void **)net->mib.icmpv6_statistics);
972 snmp_mib_free((void **)net->mib.icmpv6msg_statistics);
975 static int __net_init inet6_net_init(struct net *net)
977 int err = 0;
979 net->ipv6.sysctl.bindv6only = 0;
980 net->ipv6.sysctl.icmpv6_time = 1*HZ;
982 err = ipv6_init_mibs(net);
983 if (err)
984 return err;
985 #ifdef CONFIG_PROC_FS
986 err = udp6_proc_init(net);
987 if (err)
988 goto out;
989 err = tcp6_proc_init(net);
990 if (err)
991 goto proc_tcp6_fail;
992 err = ac6_proc_init(net);
993 if (err)
994 goto proc_ac6_fail;
995 #endif
996 return err;
998 #ifdef CONFIG_PROC_FS
999 proc_ac6_fail:
1000 tcp6_proc_exit(net);
1001 proc_tcp6_fail:
1002 udp6_proc_exit(net);
1003 out:
1004 ipv6_cleanup_mibs(net);
1005 return err;
1006 #endif
1009 static void inet6_net_exit(struct net *net)
1011 #ifdef CONFIG_PROC_FS
1012 udp6_proc_exit(net);
1013 tcp6_proc_exit(net);
1014 ac6_proc_exit(net);
1015 #endif
1016 ipv6_cleanup_mibs(net);
1019 static struct pernet_operations inet6_net_ops = {
1020 .init = inet6_net_init,
1021 .exit = inet6_net_exit,
1024 static int __init inet6_init(void)
1026 struct sk_buff *dummy_skb;
1027 struct list_head *r;
1028 int err = 0;
1030 BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
1032 /* Register the socket-side information for inet6_create. */
1033 for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1034 INIT_LIST_HEAD(r);
1036 if (disable_ipv6) {
1037 printk(KERN_INFO
1038 "IPv6: Loaded, but administratively disabled, "
1039 "reboot required to enable\n");
1040 goto out;
1043 err = proto_register(&tcpv6_prot, 1);
1044 if (err)
1045 goto out;
1047 err = proto_register(&udpv6_prot, 1);
1048 if (err)
1049 goto out_unregister_tcp_proto;
1051 err = proto_register(&udplitev6_prot, 1);
1052 if (err)
1053 goto out_unregister_udp_proto;
1055 err = proto_register(&rawv6_prot, 1);
1056 if (err)
1057 goto out_unregister_udplite_proto;
1060 /* We MUST register RAW sockets before we create the ICMP6,
1061 * IGMP6, or NDISC control sockets.
1063 err = rawv6_init();
1064 if (err)
1065 goto out_unregister_raw_proto;
1067 /* Register the family here so that the init calls below will
1068 * be able to create sockets. (?? is this dangerous ??)
1070 err = sock_register(&inet6_family_ops);
1071 if (err)
1072 goto out_sock_register_fail;
1074 #ifdef CONFIG_SYSCTL
1075 err = ipv6_static_sysctl_register();
1076 if (err)
1077 goto static_sysctl_fail;
1078 #endif
1080 * ipngwg API draft makes clear that the correct semantics
1081 * for TCP and UDP is to consider one TCP and UDP instance
1082 * in a host availiable by both INET and INET6 APIs and
1083 * able to communicate via both network protocols.
1086 err = register_pernet_subsys(&inet6_net_ops);
1087 if (err)
1088 goto register_pernet_fail;
1089 err = icmpv6_init();
1090 if (err)
1091 goto icmp_fail;
1092 err = ip6_mr_init();
1093 if (err)
1094 goto ipmr_fail;
1095 err = ndisc_init();
1096 if (err)
1097 goto ndisc_fail;
1098 err = igmp6_init();
1099 if (err)
1100 goto igmp_fail;
1101 err = ipv6_netfilter_init();
1102 if (err)
1103 goto netfilter_fail;
1104 /* Create /proc/foo6 entries. */
1105 #ifdef CONFIG_PROC_FS
1106 err = -ENOMEM;
1107 if (raw6_proc_init())
1108 goto proc_raw6_fail;
1109 if (udplite6_proc_init())
1110 goto proc_udplite6_fail;
1111 if (ipv6_misc_proc_init())
1112 goto proc_misc6_fail;
1113 if (if6_proc_init())
1114 goto proc_if6_fail;
1115 #endif
1116 err = ip6_route_init();
1117 if (err)
1118 goto ip6_route_fail;
1119 err = ip6_flowlabel_init();
1120 if (err)
1121 goto ip6_flowlabel_fail;
1122 err = addrconf_init();
1123 if (err)
1124 goto addrconf_fail;
1126 /* Init v6 extension headers. */
1127 err = ipv6_exthdrs_init();
1128 if (err)
1129 goto ipv6_exthdrs_fail;
1131 err = ipv6_frag_init();
1132 if (err)
1133 goto ipv6_frag_fail;
1135 /* Init v6 transport protocols. */
1136 err = udpv6_init();
1137 if (err)
1138 goto udpv6_fail;
1140 err = udplitev6_init();
1141 if (err)
1142 goto udplitev6_fail;
1144 err = tcpv6_init();
1145 if (err)
1146 goto tcpv6_fail;
1148 err = ipv6_packet_init();
1149 if (err)
1150 goto ipv6_packet_fail;
1152 #ifdef CONFIG_SYSCTL
1153 err = ipv6_sysctl_register();
1154 if (err)
1155 goto sysctl_fail;
1156 #endif
1157 out:
1158 return err;
1160 #ifdef CONFIG_SYSCTL
1161 sysctl_fail:
1162 ipv6_packet_cleanup();
1163 #endif
1164 ipv6_packet_fail:
1165 tcpv6_exit();
1166 tcpv6_fail:
1167 udplitev6_exit();
1168 udplitev6_fail:
1169 udpv6_exit();
1170 udpv6_fail:
1171 ipv6_frag_exit();
1172 ipv6_frag_fail:
1173 ipv6_exthdrs_exit();
1174 ipv6_exthdrs_fail:
1175 addrconf_cleanup();
1176 addrconf_fail:
1177 ip6_flowlabel_cleanup();
1178 ip6_flowlabel_fail:
1179 ip6_route_cleanup();
1180 ip6_route_fail:
1181 #ifdef CONFIG_PROC_FS
1182 if6_proc_exit();
1183 proc_if6_fail:
1184 ipv6_misc_proc_exit();
1185 proc_misc6_fail:
1186 udplite6_proc_exit();
1187 proc_udplite6_fail:
1188 raw6_proc_exit();
1189 proc_raw6_fail:
1190 #endif
1191 ipv6_netfilter_fini();
1192 netfilter_fail:
1193 igmp6_cleanup();
1194 igmp_fail:
1195 ndisc_cleanup();
1196 ndisc_fail:
1197 ip6_mr_cleanup();
1198 ipmr_fail:
1199 icmpv6_cleanup();
1200 icmp_fail:
1201 unregister_pernet_subsys(&inet6_net_ops);
1202 register_pernet_fail:
1203 #ifdef CONFIG_SYSCTL
1204 ipv6_static_sysctl_unregister();
1205 static_sysctl_fail:
1206 #endif
1207 sock_unregister(PF_INET6);
1208 rtnl_unregister_all(PF_INET6);
1209 out_sock_register_fail:
1210 rawv6_exit();
1211 out_unregister_raw_proto:
1212 proto_unregister(&rawv6_prot);
1213 out_unregister_udplite_proto:
1214 proto_unregister(&udplitev6_prot);
1215 out_unregister_udp_proto:
1216 proto_unregister(&udpv6_prot);
1217 out_unregister_tcp_proto:
1218 proto_unregister(&tcpv6_prot);
1219 goto out;
1221 module_init(inet6_init);
1223 static void __exit inet6_exit(void)
1225 if (disable_ipv6)
1226 return;
1228 /* First of all disallow new sockets creation. */
1229 sock_unregister(PF_INET6);
1230 /* Disallow any further netlink messages */
1231 rtnl_unregister_all(PF_INET6);
1233 #ifdef CONFIG_SYSCTL
1234 ipv6_sysctl_unregister();
1235 #endif
1236 udpv6_exit();
1237 udplitev6_exit();
1238 tcpv6_exit();
1240 /* Cleanup code parts. */
1241 ipv6_packet_cleanup();
1242 ipv6_frag_exit();
1243 ipv6_exthdrs_exit();
1244 addrconf_cleanup();
1245 ip6_flowlabel_cleanup();
1246 ip6_route_cleanup();
1247 #ifdef CONFIG_PROC_FS
1249 /* Cleanup code parts. */
1250 if6_proc_exit();
1251 ipv6_misc_proc_exit();
1252 udplite6_proc_exit();
1253 raw6_proc_exit();
1254 #endif
1255 ipv6_netfilter_fini();
1256 igmp6_cleanup();
1257 ndisc_cleanup();
1258 ip6_mr_cleanup();
1259 icmpv6_cleanup();
1260 rawv6_exit();
1262 unregister_pernet_subsys(&inet6_net_ops);
1263 #ifdef CONFIG_SYSCTL
1264 ipv6_static_sysctl_unregister();
1265 #endif
1266 proto_unregister(&rawv6_prot);
1267 proto_unregister(&udplitev6_prot);
1268 proto_unregister(&udpv6_prot);
1269 proto_unregister(&tcpv6_prot);
1271 module_exit(inet6_exit);
1273 MODULE_ALIAS_NETPROTO(PF_INET6);