2 * PF_INET6 socket protocol family
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Adapted from linux/net/ipv4/af_inet.c
10 * $Id: af_inet6.c,v 1.66 2002/02/01 22:01:04 davem Exp $
13 * piggy, Karl Knutson : Socket protocol table
14 * Hideaki YOSHIFUJI : sin6_scope_id support
15 * Arnaldo Melo : check proc_net_create return, cleanups
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
24 #include <linux/module.h>
25 #include <linux/config.h>
26 #include <linux/errno.h>
27 #include <linux/types.h>
28 #include <linux/socket.h>
30 #include <linux/kernel.h>
31 #include <linux/major.h>
32 #include <linux/sched.h>
33 #include <linux/timer.h>
34 #include <linux/string.h>
35 #include <linux/sockios.h>
36 #include <linux/net.h>
37 #include <linux/fcntl.h>
39 #include <linux/interrupt.h>
40 #include <linux/proc_fs.h>
41 #include <linux/stat.h>
42 #include <linux/init.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/icmpv6.h>
47 #include <linux/smp_lock.h>
54 #include <net/protocol.h>
55 #include <net/inet_common.h>
56 #include <net/transp_v6.h>
57 #include <net/ip6_route.h>
58 #include <net/addrconf.h>
59 #ifdef CONFIG_IPV6_TUNNEL
60 #include <net/ip6_tunnel.h>
63 #include <asm/uaccess.h>
64 #include <asm/system.h>
66 MODULE_AUTHOR("Cast of dozens");
67 MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
68 MODULE_LICENSE("GPL");
70 /* IPv6 procfs goodies... */
73 extern int raw6_proc_init(void);
74 extern void raw6_proc_exit(void);
75 extern int tcp6_proc_init(void);
76 extern void tcp6_proc_exit(void);
77 extern int udp6_proc_init(void);
78 extern void udp6_proc_exit(void);
79 extern int ipv6_misc_proc_init(void);
80 extern void ipv6_misc_proc_exit(void);
81 extern int ac6_proc_init(void);
82 extern void ac6_proc_exit(void);
83 extern int if6_proc_init(void);
84 extern void if6_proc_exit(void);
87 int sysctl_ipv6_bindv6only;
89 #ifdef INET_REFCNT_DEBUG
90 atomic_t inet6_sock_nr;
93 /* The inetsw table contains everything that inet_create needs to
96 static struct list_head inetsw6[SOCK_MAX];
97 static DEFINE_SPINLOCK(inetsw6_lock);
99 static void inet6_sock_destruct(struct sock *sk)
101 inet_sock_destruct(sk);
103 #ifdef INET_REFCNT_DEBUG
104 atomic_dec(&inet6_sock_nr);
108 static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
110 const int offset = sk->sk_prot->slab_obj_size - sizeof(struct ipv6_pinfo);
112 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
115 static int inet6_create(struct socket *sock, int protocol)
117 struct inet_sock *inet;
118 struct ipv6_pinfo *np;
121 struct inet_protosw *answer;
122 struct proto *answer_prot;
123 unsigned char answer_flags;
124 char answer_no_check;
127 /* Look for the requested type/protocol pair. */
130 list_for_each_rcu(p, &inetsw6[sock->type]) {
131 answer = list_entry(p, struct inet_protosw, list);
133 /* Check the non-wild match. */
134 if (protocol == answer->protocol) {
135 if (protocol != IPPROTO_IP)
138 /* Check for the two wild cases. */
139 if (IPPROTO_IP == protocol) {
140 protocol = answer->protocol;
143 if (IPPROTO_IP == answer->protocol)
149 rc = -ESOCKTNOSUPPORT;
153 if (answer->capability > 0 && !capable(answer->capability))
155 rc = -EPROTONOSUPPORT;
159 sock->ops = answer->ops;
161 answer_prot = answer->prot;
162 answer_no_check = answer->no_check;
163 answer_flags = answer->flags;
166 BUG_TRAP(answer_prot->slab != NULL);
169 sk = sk_alloc(PF_INET6, GFP_KERNEL,
170 answer_prot->slab_obj_size,
175 sock_init_data(sock, sk);
176 sk->sk_prot = answer_prot;
177 sk_set_owner(sk, sk->sk_prot->owner);
180 sk->sk_no_check = answer_no_check;
181 if (INET_PROTOSW_REUSE & answer_flags)
186 if (SOCK_RAW == sock->type) {
187 inet->num = protocol;
188 if (IPPROTO_RAW == protocol)
192 sk->sk_destruct = inet6_sock_destruct;
193 sk->sk_family = PF_INET6;
194 sk->sk_protocol = protocol;
196 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
198 inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
202 np->pmtudisc = IPV6_PMTUDISC_WANT;
203 np->ipv6only = sysctl_ipv6_bindv6only;
205 /* Init the ipv4 part of the socket since we can have sockets
206 * using v6 API for ipv4.
213 inet->mc_list = NULL;
215 if (ipv4_config.no_pmtu_disc)
216 inet->pmtudisc = IP_PMTUDISC_DONT;
218 inet->pmtudisc = IP_PMTUDISC_WANT;
221 #ifdef INET_REFCNT_DEBUG
222 atomic_inc(&inet6_sock_nr);
223 atomic_inc(&inet_sock_nr);
226 /* It assumes that any protocol which allows
227 * the user to assign a number at socket
228 * creation time automatically shares.
230 inet->sport = ntohs(inet->num);
231 sk->sk_prot->hash(sk);
233 if (sk->sk_prot->init) {
234 rc = sk->sk_prot->init(sk);
236 sk_common_release(sk);
248 /* bind for INET6 API */
249 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
251 struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
252 struct sock *sk = sock->sk;
253 struct inet_sock *inet = inet_sk(sk);
254 struct ipv6_pinfo *np = inet6_sk(sk);
260 /* If the socket has its own bind function then use it. */
261 if (sk->sk_prot->bind)
262 return sk->sk_prot->bind(sk, uaddr, addr_len);
264 if (addr_len < SIN6_LEN_RFC2133)
266 addr_type = ipv6_addr_type(&addr->sin6_addr);
267 if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
270 snum = ntohs(addr->sin6_port);
271 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
276 /* Check these errors (active socket, double bind). */
277 if (sk->sk_state != TCP_CLOSE || inet->num) {
282 /* Check if the address belongs to the host. */
283 if (addr_type == IPV6_ADDR_MAPPED) {
284 v4addr = addr->sin6_addr.s6_addr32[3];
285 if (inet_addr_type(v4addr) != RTN_LOCAL) {
286 err = -EADDRNOTAVAIL;
290 if (addr_type != IPV6_ADDR_ANY) {
291 struct net_device *dev = NULL;
293 if (addr_type & IPV6_ADDR_LINKLOCAL) {
294 if (addr_len >= sizeof(struct sockaddr_in6) &&
295 addr->sin6_scope_id) {
296 /* Override any existing binding, if another one
297 * is supplied by user.
299 sk->sk_bound_dev_if = addr->sin6_scope_id;
302 /* Binding to link-local address requires an interface */
303 if (!sk->sk_bound_dev_if) {
307 dev = dev_get_by_index(sk->sk_bound_dev_if);
314 /* ipv4 addr of the socket is invalid. Only the
315 * unspecified and mapped address have a v4 equivalent.
317 v4addr = LOOPBACK4_IPV6;
318 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
319 if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) {
322 err = -EADDRNOTAVAIL;
331 inet->rcv_saddr = v4addr;
332 inet->saddr = v4addr;
334 ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
336 if (!(addr_type & IPV6_ADDR_MULTICAST))
337 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
339 /* Make sure we are allowed to bind here. */
340 if (sk->sk_prot->get_port(sk, snum)) {
341 inet_reset_saddr(sk);
346 if (addr_type != IPV6_ADDR_ANY)
347 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
349 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
350 inet->sport = ntohs(inet->num);
358 int inet6_release(struct socket *sock)
360 struct sock *sk = sock->sk;
366 ipv6_sock_mc_close(sk);
369 ipv6_sock_ac_close(sk);
371 return inet_release(sock);
374 int inet6_destroy_sock(struct sock *sk)
376 struct ipv6_pinfo *np = inet6_sk(sk);
378 struct ipv6_txoptions *opt;
381 * Release destination entry
386 /* Release rx options */
388 if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
391 /* Free flowlabels */
392 fl6_free_socklist(sk);
394 /* Free tx options */
396 if ((opt = xchg(&np->opt, NULL)) != NULL)
397 sock_kfree_s(sk, opt, opt->tot_len);
403 * This does both peername and sockname.
406 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
407 int *uaddr_len, int peer)
409 struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
410 struct sock *sk = sock->sk;
411 struct inet_sock *inet = inet_sk(sk);
412 struct ipv6_pinfo *np = inet6_sk(sk);
414 sin->sin6_family = AF_INET6;
415 sin->sin6_flowinfo = 0;
416 sin->sin6_scope_id = 0;
420 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
423 sin->sin6_port = inet->dport;
424 ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
426 sin->sin6_flowinfo = np->flow_label;
428 if (ipv6_addr_any(&np->rcv_saddr))
429 ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
431 ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
433 sin->sin6_port = inet->sport;
435 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
436 sin->sin6_scope_id = sk->sk_bound_dev_if;
437 *uaddr_len = sizeof(*sin);
441 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
443 struct sock *sk = sock->sk;
449 return sock_get_timestamp(sk, (struct timeval __user *)arg);
454 return(ipv6_route_ioctl(cmd,(void __user *)arg));
457 return addrconf_add_ifaddr((void __user *) arg);
459 return addrconf_del_ifaddr((void __user *) arg);
461 return addrconf_set_dstaddr((void __user *) arg);
463 if (!sk->sk_prot->ioctl ||
464 (err = sk->sk_prot->ioctl(sk, cmd, arg)) == -ENOIOCTLCMD)
465 return(dev_ioctl(cmd,(void __user *) arg));
472 struct proto_ops inet6_stream_ops = {
474 .owner = THIS_MODULE,
475 .release = inet6_release,
477 .connect = inet_stream_connect, /* ok */
478 .socketpair = sock_no_socketpair, /* a do nothing */
479 .accept = inet_accept, /* ok */
480 .getname = inet6_getname,
481 .poll = tcp_poll, /* ok */
482 .ioctl = inet6_ioctl, /* must change */
483 .listen = inet_listen, /* ok */
484 .shutdown = inet_shutdown, /* ok */
485 .setsockopt = sock_common_setsockopt, /* ok */
486 .getsockopt = sock_common_getsockopt, /* ok */
487 .sendmsg = inet_sendmsg, /* ok */
488 .recvmsg = sock_common_recvmsg, /* ok */
489 .mmap = sock_no_mmap,
490 .sendpage = tcp_sendpage
493 struct proto_ops inet6_dgram_ops = {
495 .owner = THIS_MODULE,
496 .release = inet6_release,
498 .connect = inet_dgram_connect, /* ok */
499 .socketpair = sock_no_socketpair, /* a do nothing */
500 .accept = sock_no_accept, /* a do nothing */
501 .getname = inet6_getname,
502 .poll = udp_poll, /* ok */
503 .ioctl = inet6_ioctl, /* must change */
504 .listen = sock_no_listen, /* ok */
505 .shutdown = inet_shutdown, /* ok */
506 .setsockopt = sock_common_setsockopt, /* ok */
507 .getsockopt = sock_common_getsockopt, /* ok */
508 .sendmsg = inet_sendmsg, /* ok */
509 .recvmsg = sock_common_recvmsg, /* ok */
510 .mmap = sock_no_mmap,
511 .sendpage = sock_no_sendpage,
514 static struct net_proto_family inet6_family_ops = {
516 .create = inet6_create,
517 .owner = THIS_MODULE,
521 extern void ipv6_sysctl_register(void);
522 extern void ipv6_sysctl_unregister(void);
525 /* Same as inet6_dgram_ops, sans udp_poll. */
526 static struct proto_ops inet6_sockraw_ops = {
528 .owner = THIS_MODULE,
529 .release = inet6_release,
531 .connect = inet_dgram_connect, /* ok */
532 .socketpair = sock_no_socketpair, /* a do nothing */
533 .accept = sock_no_accept, /* a do nothing */
534 .getname = inet6_getname,
535 .poll = datagram_poll, /* ok */
536 .ioctl = inet6_ioctl, /* must change */
537 .listen = sock_no_listen, /* ok */
538 .shutdown = inet_shutdown, /* ok */
539 .setsockopt = sock_common_setsockopt, /* ok */
540 .getsockopt = sock_common_getsockopt, /* ok */
541 .sendmsg = inet_sendmsg, /* ok */
542 .recvmsg = sock_common_recvmsg, /* ok */
543 .mmap = sock_no_mmap,
544 .sendpage = sock_no_sendpage,
547 static struct inet_protosw rawv6_protosw = {
549 .protocol = IPPROTO_IP, /* wild card */
551 .ops = &inet6_sockraw_ops,
552 .capability = CAP_NET_RAW,
553 .no_check = UDP_CSUM_DEFAULT,
554 .flags = INET_PROTOSW_REUSE,
558 inet6_register_protosw(struct inet_protosw *p)
560 struct list_head *lh;
561 struct inet_protosw *answer;
562 int protocol = p->protocol;
563 struct list_head *last_perm;
565 spin_lock_bh(&inetsw6_lock);
567 if (p->type >= SOCK_MAX)
570 /* If we are trying to override a permanent protocol, bail. */
572 last_perm = &inetsw6[p->type];
573 list_for_each(lh, &inetsw6[p->type]) {
574 answer = list_entry(lh, struct inet_protosw, list);
576 /* Check only the non-wild match. */
577 if (INET_PROTOSW_PERMANENT & answer->flags) {
578 if (protocol == answer->protocol)
588 /* Add the new entry after the last permanent entry if any, so that
589 * the new entry does not override a permanent entry when matched with
590 * a wild-card protocol. But it is allowed to override any existing
591 * non-permanent entry. This means that when we remove this entry, the
592 * system automatically returns to the old behavior.
594 list_add_rcu(&p->list, last_perm);
596 spin_unlock_bh(&inetsw6_lock);
600 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
606 "Ignoring attempt to register invalid socket type %d.\n",
612 inet6_unregister_protosw(struct inet_protosw *p)
614 if (INET_PROTOSW_PERMANENT & p->flags) {
616 "Attempt to unregister permanent protocol %d.\n",
619 spin_lock_bh(&inetsw6_lock);
620 list_del_rcu(&p->list);
621 spin_unlock_bh(&inetsw6_lock);
628 snmp6_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
633 ptr[0] = __alloc_percpu(mibsize, mibalign);
637 ptr[1] = __alloc_percpu(mibsize, mibalign);
651 snmp6_mib_free(void *ptr[2])
659 ptr[0] = ptr[1] = NULL;
662 static int __init init_ipv6_mibs(void)
664 if (snmp6_mib_init((void **)ipv6_statistics, sizeof (struct ipstats_mib),
665 __alignof__(struct ipstats_mib)) < 0)
667 if (snmp6_mib_init((void **)icmpv6_statistics, sizeof (struct icmpv6_mib),
668 __alignof__(struct icmpv6_mib)) < 0)
670 if (snmp6_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib),
671 __alignof__(struct udp_mib)) < 0)
676 snmp6_mib_free((void **)icmpv6_statistics);
678 snmp6_mib_free((void **)ipv6_statistics);
684 static void cleanup_ipv6_mibs(void)
686 snmp6_mib_free((void **)ipv6_statistics);
687 snmp6_mib_free((void **)icmpv6_statistics);
688 snmp6_mib_free((void **)udp_stats_in6);
691 extern int ipv6_misc_proc_init(void);
693 static int __init inet6_init(void)
695 struct sk_buff *dummy_skb;
700 #if 0 /* FIXME --RR */
701 if (!mod_member_present(&__this_module, can_unload))
704 __this_module.can_unload = &ipv6_unload;
708 if (sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb)) {
709 printk(KERN_CRIT "inet6_proto_init: size fault\n");
713 err = sk_alloc_slab(&tcpv6_prot, "tcpv6_sock");
715 sk_alloc_slab_error(&tcpv6_prot);
718 err = sk_alloc_slab(&udpv6_prot, "udpv6_sock");
720 sk_alloc_slab_error(&udpv6_prot);
721 goto out_tcp_free_slab;
723 err = sk_alloc_slab(&rawv6_prot, "rawv6_sock");
725 sk_alloc_slab_error(&rawv6_prot);
726 goto out_udp_free_slab;
729 /* Register the socket-side information for inet6_create. */
730 for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
733 /* We MUST register RAW sockets before we create the ICMP6,
734 * IGMP6, or NDISC control sockets.
736 inet6_register_protosw(&rawv6_protosw);
738 /* Register the family here so that the init calls below will
739 * be able to create sockets. (?? is this dangerous ??)
741 (void) sock_register(&inet6_family_ops);
743 /* Initialise ipv6 mibs */
744 err = init_ipv6_mibs();
746 goto out_raw_free_slab;
749 * ipngwg API draft makes clear that the correct semantics
750 * for TCP and UDP is to consider one TCP and UDP instance
751 * in a host availiable by both INET and INET6 APIs and
752 * able to communicate via both network protocols.
756 ipv6_sysctl_register();
758 err = icmpv6_init(&inet6_family_ops);
761 err = ndisc_init(&inet6_family_ops);
764 err = igmp6_init(&inet6_family_ops);
767 /* Create /proc/foo6 entries. */
768 #ifdef CONFIG_PROC_FS
770 if (raw6_proc_init())
772 if (tcp6_proc_init())
774 if (udp6_proc_init())
776 if (ipv6_misc_proc_init())
777 goto proc_misc6_fail;
780 goto proc_anycast6_fail;
786 ip6_flowlabel_init();
790 /* Init v6 extension headers. */
796 /* Init v6 transport protocols. */
803 #ifdef CONFIG_PROC_FS
807 ipv6_misc_proc_exit();
823 ipv6_sysctl_unregister();
827 sk_free_slab(&rawv6_prot);
829 sk_free_slab(&udpv6_prot);
831 sk_free_slab(&tcpv6_prot);
834 module_init(inet6_init);
836 static void __exit inet6_exit(void)
838 /* First of all disallow new sockets creation. */
839 sock_unregister(PF_INET6);
840 #ifdef CONFIG_PROC_FS
843 ipv6_misc_proc_exit();
848 /* Cleanup code parts. */
850 ip6_flowlabel_cleanup();
853 ipv6_packet_cleanup();
858 ipv6_sysctl_unregister();
861 sk_free_slab(&rawv6_prot);
862 sk_free_slab(&udpv6_prot);
863 sk_free_slab(&tcpv6_prot);
865 module_exit(inet6_exit);
867 MODULE_ALIAS_NETPROTO(PF_INET6);