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 #if CONFIG_IPV6_TUNNEL
60 #include <net/ip6_tunnel.h>
63 #include <asm/uaccess.h>
64 #include <asm/system.h>
67 static int unloadable; /* XX: Turn to one when all is ok within the
68 module for allowing unload */
69 MODULE_PARM(unloadable, "i");
72 MODULE_AUTHOR("Cast of dozens");
73 MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
74 MODULE_LICENSE("GPL");
76 /* IPv6 procfs goodies... */
79 extern int raw6_proc_init(void);
80 extern void raw6_proc_exit(void);
81 extern int tcp6_proc_init(void);
82 extern void tcp6_proc_exit(void);
83 extern int udp6_proc_init(void);
84 extern void udp6_proc_exit(void);
85 extern int ipv6_misc_proc_init(void);
86 extern void ipv6_misc_proc_exit(void);
87 extern int ac6_proc_init(void);
88 extern void ac6_proc_exit(void);
89 extern int if6_proc_init(void);
90 extern void if6_proc_exit(void);
94 extern void ipv6_sysctl_register(void);
95 extern void ipv6_sysctl_unregister(void);
98 int sysctl_ipv6_bindv6only;
100 #ifdef INET_REFCNT_DEBUG
101 atomic_t inet6_sock_nr;
104 /* Per protocol sock slabcache */
105 kmem_cache_t *tcp6_sk_cachep;
106 kmem_cache_t *udp6_sk_cachep;
107 kmem_cache_t *raw6_sk_cachep;
109 /* The inetsw table contains everything that inet_create needs to
110 * build a new socket.
112 static struct list_head inetsw6[SOCK_MAX];
113 static spinlock_t inetsw6_lock = SPIN_LOCK_UNLOCKED;
115 static void inet6_sock_destruct(struct sock *sk)
117 inet_sock_destruct(sk);
119 #ifdef INET_REFCNT_DEBUG
120 atomic_dec(&inet6_sock_nr);
124 static __inline__ kmem_cache_t *inet6_sk_slab(int protocol)
126 kmem_cache_t* rc = tcp6_sk_cachep;
128 if (protocol == IPPROTO_UDP)
130 else if (protocol == IPPROTO_RAW)
135 static __inline__ int inet6_sk_size(int protocol)
137 int rc = sizeof(struct tcp6_sock);
139 if (protocol == IPPROTO_UDP)
140 rc = sizeof(struct udp6_sock);
141 else if (protocol == IPPROTO_RAW)
142 rc = sizeof(struct raw6_sock);
146 static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
148 struct ipv6_pinfo *rc = (&((struct tcp6_sock *)sk)->inet6);
150 if (sk->sk_protocol == IPPROTO_UDP)
151 rc = (&((struct udp6_sock *)sk)->inet6);
152 else if (sk->sk_protocol == IPPROTO_RAW)
153 rc = (&((struct raw6_sock *)sk)->inet6);
157 static int inet6_create(struct socket *sock, int protocol)
159 struct inet_opt *inet;
160 struct ipv6_pinfo *np;
162 struct tcp6_sock* tcp6sk;
164 struct inet_protosw *answer;
166 sk = sk_alloc(PF_INET6, GFP_KERNEL, inet6_sk_size(protocol),
167 inet6_sk_slab(protocol));
171 /* Look for the requested type/protocol pair. */
174 list_for_each_rcu(p, &inetsw6[sock->type]) {
175 answer = list_entry(p, struct inet_protosw, list);
177 /* Check the non-wild match. */
178 if (protocol == answer->protocol) {
179 if (protocol != IPPROTO_IP)
182 /* Check for the two wild cases. */
183 if (IPPROTO_IP == protocol) {
184 protocol = answer->protocol;
187 if (IPPROTO_IP == answer->protocol)
194 goto free_and_badtype;
195 if (answer->capability > 0 && !capable(answer->capability))
196 goto free_and_badperm;
198 goto free_and_noproto;
200 sock->ops = answer->ops;
201 sock_init_data(sock, sk);
202 sk_set_owner(sk, THIS_MODULE);
204 sk->sk_prot = answer->prot;
205 sk->sk_no_check = answer->no_check;
206 if (INET_PROTOSW_REUSE & answer->flags)
212 if (SOCK_RAW == sock->type) {
213 inet->num = protocol;
214 if (IPPROTO_RAW == protocol)
218 sk->sk_destruct = inet6_sock_destruct;
220 sk->sk_family = PF_INET6;
221 sk->sk_protocol = protocol;
223 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
225 tcp6sk = (struct tcp6_sock *)sk;
226 tcp6sk->pinet6 = np = inet6_sk_generic(sk);
230 np->pmtudisc = IPV6_PMTUDISC_WANT;
231 np->ipv6only = sysctl_ipv6_bindv6only;
233 /* Init the ipv4 part of the socket since we can have sockets
234 * using v6 API for ipv4.
241 inet->mc_list = NULL;
243 if (ipv4_config.no_pmtu_disc)
244 inet->pmtudisc = IP_PMTUDISC_DONT;
246 inet->pmtudisc = IP_PMTUDISC_WANT;
249 #ifdef INET_REFCNT_DEBUG
250 atomic_inc(&inet6_sock_nr);
251 atomic_inc(&inet_sock_nr);
254 /* It assumes that any protocol which allows
255 * the user to assign a number at socket
256 * creation time automatically shares.
258 inet->sport = ntohs(inet->num);
259 sk->sk_prot->hash(sk);
261 if (sk->sk_prot->init) {
262 int err = sk->sk_prot->init(sk);
264 inet_sock_release(sk);
273 return -ESOCKTNOSUPPORT;
281 return -EPROTONOSUPPORT;
287 /* bind for INET6 API */
288 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
290 struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
291 struct sock *sk = sock->sk;
292 struct inet_opt *inet = inet_sk(sk);
293 struct ipv6_pinfo *np = inet6_sk(sk);
299 /* If the socket has its own bind function then use it. */
300 if (sk->sk_prot->bind)
301 return sk->sk_prot->bind(sk, uaddr, addr_len);
303 if (addr_len < SIN6_LEN_RFC2133)
305 addr_type = ipv6_addr_type(&addr->sin6_addr);
306 if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
309 snum = ntohs(addr->sin6_port);
310 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
315 /* Check these errors (active socket, double bind). */
316 if (sk->sk_state != TCP_CLOSE || inet->num) {
321 /* Check if the address belongs to the host. */
322 if (addr_type == IPV6_ADDR_MAPPED) {
323 v4addr = addr->sin6_addr.s6_addr32[3];
324 if (inet_addr_type(v4addr) != RTN_LOCAL) {
325 err = -EADDRNOTAVAIL;
329 if (addr_type != IPV6_ADDR_ANY) {
330 struct net_device *dev = NULL;
332 if (addr_type & IPV6_ADDR_LINKLOCAL) {
333 if (addr_len >= sizeof(struct sockaddr_in6) &&
334 addr->sin6_scope_id) {
335 /* Override any existing binding, if another one
336 * is supplied by user.
338 sk->sk_bound_dev_if = addr->sin6_scope_id;
341 /* Binding to link-local address requires an interface */
342 if (!sk->sk_bound_dev_if) {
346 dev = dev_get_by_index(sk->sk_bound_dev_if);
353 /* ipv4 addr of the socket is invalid. Only the
354 * unspecified and mapped address have a v4 equivalent.
356 v4addr = LOOPBACK4_IPV6;
357 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
358 if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) {
361 err = -EADDRNOTAVAIL;
370 inet->rcv_saddr = v4addr;
371 inet->saddr = v4addr;
373 ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
375 if (!(addr_type & IPV6_ADDR_MULTICAST))
376 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
378 /* Make sure we are allowed to bind here. */
379 if (sk->sk_prot->get_port(sk, snum)) {
380 inet_reset_saddr(sk);
385 if (addr_type != IPV6_ADDR_ANY)
386 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
388 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
389 inet->sport = ntohs(inet->num);
397 int inet6_release(struct socket *sock)
399 struct sock *sk = sock->sk;
405 ipv6_sock_mc_close(sk);
408 ipv6_sock_ac_close(sk);
410 return inet_release(sock);
413 int inet6_destroy_sock(struct sock *sk)
415 struct ipv6_pinfo *np = inet6_sk(sk);
417 struct ipv6_txoptions *opt;
420 * Release destination entry
425 /* Release rx options */
427 if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
430 /* Free flowlabels */
431 fl6_free_socklist(sk);
433 /* Free tx options */
435 if ((opt = xchg(&np->opt, NULL)) != NULL)
436 sock_kfree_s(sk, opt, opt->tot_len);
442 * This does both peername and sockname.
445 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
446 int *uaddr_len, int peer)
448 struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
449 struct sock *sk = sock->sk;
450 struct inet_opt *inet = inet_sk(sk);
451 struct ipv6_pinfo *np = inet6_sk(sk);
453 sin->sin6_family = AF_INET6;
454 sin->sin6_flowinfo = 0;
455 sin->sin6_scope_id = 0;
459 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
462 sin->sin6_port = inet->dport;
463 ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
465 sin->sin6_flowinfo = np->flow_label;
467 if (ipv6_addr_any(&np->rcv_saddr))
468 ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
470 ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
472 sin->sin6_port = inet->sport;
474 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
475 sin->sin6_scope_id = sk->sk_bound_dev_if;
476 *uaddr_len = sizeof(*sin);
480 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
482 struct sock *sk = sock->sk;
488 if (!sk->sk_stamp.tv_sec)
490 err = copy_to_user((void *)arg, &sk->sk_stamp,
491 sizeof(struct timeval));
499 return(ipv6_route_ioctl(cmd,(void *)arg));
502 return addrconf_add_ifaddr((void *) arg);
504 return addrconf_del_ifaddr((void *) arg);
506 return addrconf_set_dstaddr((void *) arg);
508 if (!sk->sk_prot->ioctl ||
509 (err = sk->sk_prot->ioctl(sk, cmd, arg)) == -ENOIOCTLCMD)
510 return(dev_ioctl(cmd,(void *) arg));
517 struct proto_ops inet6_stream_ops = {
519 .owner = THIS_MODULE,
520 .release = inet6_release,
522 .connect = inet_stream_connect, /* ok */
523 .socketpair = sock_no_socketpair, /* a do nothing */
524 .accept = inet_accept, /* ok */
525 .getname = inet6_getname,
526 .poll = tcp_poll, /* ok */
527 .ioctl = inet6_ioctl, /* must change */
528 .listen = inet_listen, /* ok */
529 .shutdown = inet_shutdown, /* ok */
530 .setsockopt = inet_setsockopt, /* ok */
531 .getsockopt = inet_getsockopt, /* ok */
532 .sendmsg = inet_sendmsg, /* ok */
533 .recvmsg = inet_recvmsg, /* ok */
534 .mmap = sock_no_mmap,
535 .sendpage = tcp_sendpage
538 struct proto_ops inet6_dgram_ops = {
540 .owner = THIS_MODULE,
541 .release = inet6_release,
543 .connect = inet_dgram_connect, /* ok */
544 .socketpair = sock_no_socketpair, /* a do nothing */
545 .accept = sock_no_accept, /* a do nothing */
546 .getname = inet6_getname,
547 .poll = datagram_poll, /* ok */
548 .ioctl = inet6_ioctl, /* must change */
549 .listen = sock_no_listen, /* ok */
550 .shutdown = inet_shutdown, /* ok */
551 .setsockopt = inet_setsockopt, /* ok */
552 .getsockopt = inet_getsockopt, /* ok */
553 .sendmsg = inet_sendmsg, /* ok */
554 .recvmsg = inet_recvmsg, /* ok */
555 .mmap = sock_no_mmap,
556 .sendpage = sock_no_sendpage,
559 struct net_proto_family inet6_family_ops = {
561 .create = inet6_create,
562 .owner = THIS_MODULE,
566 #if 0 /* FIXME --RR */
567 int ipv6_unload(void)
569 if (!unloadable) return 1;
570 /* We keep internally 3 raw sockets */
571 return atomic_read(&(__this_module.uc.usecount)) - 3;
576 #if defined(MODULE) && defined(CONFIG_SYSCTL)
577 extern void ipv6_sysctl_register(void);
578 extern void ipv6_sysctl_unregister(void);
581 static struct inet_protosw rawv6_protosw = {
583 .protocol = IPPROTO_IP, /* wild card */
585 .ops = &inet6_dgram_ops,
586 .capability = CAP_NET_RAW,
587 .no_check = UDP_CSUM_DEFAULT,
588 .flags = INET_PROTOSW_REUSE,
591 #define INETSW6_ARRAY_LEN (sizeof(inetsw6_array) / sizeof(struct inet_protosw))
594 inet6_register_protosw(struct inet_protosw *p)
596 struct list_head *lh;
597 struct inet_protosw *answer;
598 int protocol = p->protocol;
599 struct list_head *last_perm;
601 spin_lock_bh(&inetsw6_lock);
603 if (p->type > SOCK_MAX)
606 /* If we are trying to override a permanent protocol, bail. */
608 last_perm = &inetsw6[p->type];
609 list_for_each(lh, &inetsw6[p->type]) {
610 answer = list_entry(lh, struct inet_protosw, list);
612 /* Check only the non-wild match. */
613 if (INET_PROTOSW_PERMANENT & answer->flags) {
614 if (protocol == answer->protocol)
624 /* Add the new entry after the last permanent entry if any, so that
625 * the new entry does not override a permanent entry when matched with
626 * a wild-card protocol. But it is allowed to override any existing
627 * non-permanent entry. This means that when we remove this entry, the
628 * system automatically returns to the old behavior.
630 list_add_rcu(&p->list, last_perm);
632 spin_unlock_bh(&inetsw6_lock);
636 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
642 "Ignoring attempt to register invalid socket type %d.\n",
648 inet6_unregister_protosw(struct inet_protosw *p)
650 if (INET_PROTOSW_PERMANENT & p->flags) {
652 "Attempt to unregister permanent protocol %d.\n",
655 spin_lock_bh(&inetsw6_lock);
656 list_del_rcu(&p->list);
657 spin_unlock_bh(&inetsw6_lock);
664 snmp6_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
669 ptr[0] = __alloc_percpu(mibsize, mibalign);
673 ptr[1] = __alloc_percpu(mibsize, mibalign);
687 snmp6_mib_free(void *ptr[2])
693 ptr[0] = ptr[1] = NULL;
696 static int __init init_ipv6_mibs(void)
698 if (snmp6_mib_init((void **)ipv6_statistics, sizeof (struct ipv6_mib),
699 __alignof__(struct ipv6_mib)) < 0)
701 if (snmp6_mib_init((void **)icmpv6_statistics, sizeof (struct icmpv6_mib),
702 __alignof__(struct ipv6_mib)) < 0)
704 if (snmp6_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib),
705 __alignof__(struct ipv6_mib)) < 0)
710 snmp6_mib_free((void **)icmpv6_statistics);
712 snmp6_mib_free((void **)ipv6_statistics);
718 static void cleanup_ipv6_mibs(void)
720 snmp6_mib_free((void **)ipv6_statistics);
721 snmp6_mib_free((void **)icmpv6_statistics);
722 snmp6_mib_free((void **)udp_stats_in6);
725 extern int ipv6_misc_proc_init(void);
727 static int __init inet6_init(void)
729 struct sk_buff *dummy_skb;
734 #if 0 /* FIXME --RR */
735 if (!mod_member_present(&__this_module, can_unload))
738 __this_module.can_unload = &ipv6_unload;
742 if (sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb))
744 printk(KERN_CRIT "inet6_proto_init: size fault\n");
747 /* allocate our sock slab caches */
748 tcp6_sk_cachep = kmem_cache_create("tcp6_sock",
749 sizeof(struct tcp6_sock), 0,
750 SLAB_HWCACHE_ALIGN, 0, 0);
751 udp6_sk_cachep = kmem_cache_create("udp6_sock",
752 sizeof(struct udp6_sock), 0,
753 SLAB_HWCACHE_ALIGN, 0, 0);
754 raw6_sk_cachep = kmem_cache_create("raw6_sock",
755 sizeof(struct raw6_sock), 0,
756 SLAB_HWCACHE_ALIGN, 0, 0);
757 if (!tcp6_sk_cachep || !udp6_sk_cachep || !raw6_sk_cachep)
758 printk(KERN_CRIT "%s: Can't create protocol sock SLAB "
759 "caches!\n", __FUNCTION__);
761 /* Register the socket-side information for inet6_create. */
762 for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
765 /* We MUST register RAW sockets before we create the ICMP6,
766 * IGMP6, or NDISC control sockets.
768 inet6_register_protosw(&rawv6_protosw);
770 /* Register the family here so that the init calls below will
771 * be able to create sockets. (?? is this dangerous ??)
773 (void) sock_register(&inet6_family_ops);
775 /* Initialise ipv6 mibs */
776 err = init_ipv6_mibs();
781 * ipngwg API draft makes clear that the correct semantics
782 * for TCP and UDP is to consider one TCP and UDP instance
783 * in a host availiable by both INET and INET6 APIs and
784 * able to communicate via both network protocols.
787 #if defined(MODULE) && defined(CONFIG_SYSCTL)
788 ipv6_sysctl_register();
790 err = icmpv6_init(&inet6_family_ops);
793 err = ndisc_init(&inet6_family_ops);
796 #ifdef CONFIG_IPV6_TUNNEL
797 err = ip6_tunnel_init();
799 goto ip6_tunnel_fail;
801 err = igmp6_init(&inet6_family_ops);
804 /* Create /proc/foo6 entries. */
805 #ifdef CONFIG_PROC_FS
807 if (raw6_proc_init())
809 if (tcp6_proc_init())
811 if (udp6_proc_init())
813 if (ipv6_misc_proc_init())
814 goto proc_misc6_fail;
817 goto proc_anycast6_fail;
823 ip6_flowlabel_init();
827 /* Init v6 extension headers. */
833 /* Init v6 transport protocols. */
839 #ifdef CONFIG_PROC_FS
843 ipv6_misc_proc_exit();
854 #ifdef CONFIG_IPV6_TUNNEL
855 ip6_tunnel_cleanup();
862 #if defined(MODULE) && defined(CONFIG_SYSCTL)
863 ipv6_sysctl_unregister();
869 module_init(inet6_init);
873 static void inet6_exit(void)
875 /* First of all disallow new sockets creation. */
876 sock_unregister(PF_INET6);
877 #ifdef CONFIG_PROC_FS
880 ipv6_misc_proc_exit();
885 /* Cleanup code parts. */
887 ip6_flowlabel_cleanup();
890 ipv6_packet_cleanup();
892 #ifdef CONFIG_IPV6_TUNNEL
893 ip6_tunnel_cleanup();
898 ipv6_sysctl_unregister();
901 kmem_cache_destroy(tcp6_sk_cachep);
902 kmem_cache_destroy(udp6_sk_cachep);
903 kmem_cache_destroy(raw6_sk_cachep);
905 module_exit(inet6_exit);
908 MODULE_ALIAS_NETPROTO(PF_INET6);