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>
43 #include <linux/version.h>
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <linux/icmpv6.h>
48 #include <linux/smp_lock.h>
55 #include <net/protocol.h>
56 #include <net/inet_common.h>
57 #include <net/transp_v6.h>
58 #include <net/ip6_route.h>
59 #include <net/addrconf.h>
60 #if CONFIG_IPV6_TUNNEL
61 #include <net/ip6_tunnel.h>
64 #include <asm/uaccess.h>
65 #include <asm/system.h>
68 static int unloadable = 0; /* XX: Turn to one when all is ok within the
69 module for allowing unload */
70 MODULE_PARM(unloadable, "i");
73 MODULE_AUTHOR("Cast of dozens");
74 MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
75 MODULE_LICENSE("GPL");
77 /* IPv6 procfs goodies... */
80 extern int raw6_proc_init(void);
81 extern void raw6_proc_exit(void);
82 extern int tcp6_proc_init(void);
83 extern void tcp6_proc_exit(void);
84 extern int udp6_proc_init(void);
85 extern void udp6_proc_exit(void);
86 extern int ipv6_misc_proc_init(void);
87 extern void ipv6_misc_proc_exit(void);
88 extern int ac6_proc_init(void);
89 extern void ac6_proc_exit(void);
90 extern int if6_proc_init(void);
91 extern void if6_proc_exit(void);
95 extern void ipv6_sysctl_register(void);
96 extern void ipv6_sysctl_unregister(void);
99 int sysctl_ipv6_bindv6only;
101 #ifdef INET_REFCNT_DEBUG
102 atomic_t inet6_sock_nr;
105 /* Per protocol sock slabcache */
106 kmem_cache_t *tcp6_sk_cachep;
107 kmem_cache_t *udp6_sk_cachep;
108 kmem_cache_t *raw6_sk_cachep;
110 /* The inetsw table contains everything that inet_create needs to
111 * build a new socket.
113 static struct list_head inetsw6[SOCK_MAX];
114 static spinlock_t inetsw6_lock = SPIN_LOCK_UNLOCKED;
116 static void inet6_sock_destruct(struct sock *sk)
118 inet_sock_destruct(sk);
120 #ifdef INET_REFCNT_DEBUG
121 atomic_dec(&inet6_sock_nr);
125 static __inline__ kmem_cache_t *inet6_sk_slab(int protocol)
127 kmem_cache_t* rc = tcp6_sk_cachep;
129 if (protocol == IPPROTO_UDP)
131 else if (protocol == IPPROTO_RAW)
136 static __inline__ int inet6_sk_size(int protocol)
138 int rc = sizeof(struct tcp6_sock);
140 if (protocol == IPPROTO_UDP)
141 rc = sizeof(struct udp6_sock);
142 else if (protocol == IPPROTO_RAW)
143 rc = sizeof(struct raw6_sock);
147 static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
149 struct ipv6_pinfo *rc = (&((struct tcp6_sock *)sk)->inet6);
151 if (sk->sk_protocol == IPPROTO_UDP)
152 rc = (&((struct udp6_sock *)sk)->inet6);
153 else if (sk->sk_protocol == IPPROTO_RAW)
154 rc = (&((struct raw6_sock *)sk)->inet6);
158 static int inet6_create(struct socket *sock, int protocol)
160 struct inet_opt *inet;
161 struct ipv6_pinfo *np;
163 struct tcp6_sock* tcp6sk;
165 struct inet_protosw *answer;
167 sk = sk_alloc(PF_INET6, GFP_KERNEL, inet6_sk_size(protocol),
168 inet6_sk_slab(protocol));
172 /* Look for the requested type/protocol pair. */
175 list_for_each_rcu(p, &inetsw6[sock->type]) {
176 answer = list_entry(p, struct inet_protosw, list);
178 /* Check the non-wild match. */
179 if (protocol == answer->protocol) {
180 if (protocol != IPPROTO_IP)
183 /* Check for the two wild cases. */
184 if (IPPROTO_IP == protocol) {
185 protocol = answer->protocol;
188 if (IPPROTO_IP == answer->protocol)
195 goto free_and_badtype;
196 if (answer->capability > 0 && !capable(answer->capability))
197 goto free_and_badperm;
199 goto free_and_noproto;
201 sock->ops = answer->ops;
202 sock_init_data(sock, sk);
203 sk_set_owner(sk, THIS_MODULE);
205 sk->sk_prot = answer->prot;
206 sk->sk_no_check = answer->no_check;
207 if (INET_PROTOSW_REUSE & answer->flags)
213 if (SOCK_RAW == sock->type) {
214 inet->num = protocol;
215 if (IPPROTO_RAW == protocol)
219 sk->sk_destruct = inet6_sock_destruct;
221 sk->sk_family = PF_INET6;
222 sk->sk_protocol = protocol;
224 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
226 tcp6sk = (struct tcp6_sock *)sk;
227 tcp6sk->pinet6 = np = inet6_sk_generic(sk);
231 np->pmtudisc = IPV6_PMTUDISC_WANT;
232 np->ipv6only = sysctl_ipv6_bindv6only;
234 /* Init the ipv4 part of the socket since we can have sockets
235 * using v6 API for ipv4.
242 inet->mc_list = NULL;
244 if (ipv4_config.no_pmtu_disc)
245 inet->pmtudisc = IP_PMTUDISC_DONT;
247 inet->pmtudisc = IP_PMTUDISC_WANT;
250 #ifdef INET_REFCNT_DEBUG
251 atomic_inc(&inet6_sock_nr);
252 atomic_inc(&inet_sock_nr);
255 /* It assumes that any protocol which allows
256 * the user to assign a number at socket
257 * creation time automatically shares.
259 inet->sport = ntohs(inet->num);
260 sk->sk_prot->hash(sk);
262 if (sk->sk_prot->init) {
263 int err = sk->sk_prot->init(sk);
265 inet_sock_release(sk);
274 return -ESOCKTNOSUPPORT;
282 return -EPROTONOSUPPORT;
288 /* bind for INET6 API */
289 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
291 struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
292 struct sock *sk = sock->sk;
293 struct inet_opt *inet = inet_sk(sk);
294 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 /* Check if the address belongs to the host. */
310 if (addr_type == IPV6_ADDR_MAPPED) {
311 v4addr = addr->sin6_addr.s6_addr32[3];
312 if (inet_addr_type(v4addr) != RTN_LOCAL)
313 return -EADDRNOTAVAIL;
315 if (addr_type != IPV6_ADDR_ANY) {
316 /* ipv4 addr of the socket is invalid. Only the
317 * unspecified and mapped address have a v4 equivalent.
319 v4addr = LOOPBACK4_IPV6;
320 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
321 if (!ipv6_chk_addr(&addr->sin6_addr, NULL))
322 return -EADDRNOTAVAIL;
327 snum = ntohs(addr->sin6_port);
328 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
333 /* Check these errors (active socket, double bind). */
334 if (sk->sk_state != TCP_CLOSE || inet->num) {
339 if (addr_type & IPV6_ADDR_LINKLOCAL) {
340 if (addr_len >= sizeof(struct sockaddr_in6) &&
341 addr->sin6_scope_id) {
342 /* Override any existing binding, if another one
343 * is supplied by user.
345 sk->sk_bound_dev_if = addr->sin6_scope_id;
348 /* Binding to link-local address requires an interface */
349 if (!sk->sk_bound_dev_if) {
355 inet->rcv_saddr = v4addr;
356 inet->saddr = v4addr;
358 ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
360 if (!(addr_type & IPV6_ADDR_MULTICAST))
361 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
363 /* Make sure we are allowed to bind here. */
364 if (sk->sk_prot->get_port(sk, snum)) {
365 inet_reset_saddr(sk);
370 if (addr_type != IPV6_ADDR_ANY)
371 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
373 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
374 inet->sport = ntohs(inet->num);
382 int inet6_release(struct socket *sock)
384 struct sock *sk = sock->sk;
390 ipv6_sock_mc_close(sk);
393 ipv6_sock_ac_close(sk);
395 return inet_release(sock);
398 int inet6_destroy_sock(struct sock *sk)
400 struct ipv6_pinfo *np = inet6_sk(sk);
402 struct ipv6_txoptions *opt;
405 * Release destination entry
410 /* Release rx options */
412 if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
415 /* Free flowlabels */
416 fl6_free_socklist(sk);
418 /* Free tx options */
420 if ((opt = xchg(&np->opt, NULL)) != NULL)
421 sock_kfree_s(sk, opt, opt->tot_len);
427 * This does both peername and sockname.
430 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
431 int *uaddr_len, int peer)
433 struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
434 struct sock *sk = sock->sk;
435 struct inet_opt *inet = inet_sk(sk);
436 struct ipv6_pinfo *np = inet6_sk(sk);
438 sin->sin6_family = AF_INET6;
439 sin->sin6_flowinfo = 0;
440 sin->sin6_scope_id = 0;
444 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
447 sin->sin6_port = inet->dport;
448 ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
450 sin->sin6_flowinfo = np->flow_label;
452 if (ipv6_addr_type(&np->rcv_saddr) == IPV6_ADDR_ANY)
453 ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
455 ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
457 sin->sin6_port = inet->sport;
459 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
460 sin->sin6_scope_id = sk->sk_bound_dev_if;
461 *uaddr_len = sizeof(*sin);
465 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
467 struct sock *sk = sock->sk;
473 if (!sk->sk_stamp.tv_sec)
475 err = copy_to_user((void *)arg, &sk->sk_stamp,
476 sizeof(struct timeval));
484 return(ipv6_route_ioctl(cmd,(void *)arg));
487 return addrconf_add_ifaddr((void *) arg);
489 return addrconf_del_ifaddr((void *) arg);
491 return addrconf_set_dstaddr((void *) arg);
493 if (!sk->sk_prot->ioctl ||
494 (err = sk->sk_prot->ioctl(sk, cmd, arg)) == -ENOIOCTLCMD)
495 return(dev_ioctl(cmd,(void *) arg));
502 struct proto_ops inet6_stream_ops = {
504 .owner = THIS_MODULE,
505 .release = inet6_release,
507 .connect = inet_stream_connect, /* ok */
508 .socketpair = sock_no_socketpair, /* a do nothing */
509 .accept = inet_accept, /* ok */
510 .getname = inet6_getname,
511 .poll = tcp_poll, /* ok */
512 .ioctl = inet6_ioctl, /* must change */
513 .listen = inet_listen, /* ok */
514 .shutdown = inet_shutdown, /* ok */
515 .setsockopt = inet_setsockopt, /* ok */
516 .getsockopt = inet_getsockopt, /* ok */
517 .sendmsg = inet_sendmsg, /* ok */
518 .recvmsg = inet_recvmsg, /* ok */
519 .mmap = sock_no_mmap,
520 .sendpage = tcp_sendpage
523 struct proto_ops inet6_dgram_ops = {
525 .owner = THIS_MODULE,
526 .release = inet6_release,
528 .connect = inet_dgram_connect, /* ok */
529 .socketpair = sock_no_socketpair, /* a do nothing */
530 .accept = sock_no_accept, /* a do nothing */
531 .getname = inet6_getname,
532 .poll = datagram_poll, /* ok */
533 .ioctl = inet6_ioctl, /* must change */
534 .listen = sock_no_listen, /* ok */
535 .shutdown = inet_shutdown, /* ok */
536 .setsockopt = inet_setsockopt, /* ok */
537 .getsockopt = inet_getsockopt, /* ok */
538 .sendmsg = inet_sendmsg, /* ok */
539 .recvmsg = inet_recvmsg, /* ok */
540 .mmap = sock_no_mmap,
541 .sendpage = sock_no_sendpage,
544 struct net_proto_family inet6_family_ops = {
546 .create = inet6_create,
547 .owner = THIS_MODULE,
551 #if 0 /* FIXME --RR */
552 int ipv6_unload(void)
554 if (!unloadable) return 1;
555 /* We keep internally 3 raw sockets */
556 return atomic_read(&(__this_module.uc.usecount)) - 3;
561 #if defined(MODULE) && defined(CONFIG_SYSCTL)
562 extern void ipv6_sysctl_register(void);
563 extern void ipv6_sysctl_unregister(void);
566 static struct inet_protosw rawv6_protosw = {
568 .protocol = IPPROTO_IP, /* wild card */
570 .ops = &inet6_dgram_ops,
571 .capability = CAP_NET_RAW,
572 .no_check = UDP_CSUM_DEFAULT,
573 .flags = INET_PROTOSW_REUSE,
576 #define INETSW6_ARRAY_LEN (sizeof(inetsw6_array) / sizeof(struct inet_protosw))
579 inet6_register_protosw(struct inet_protosw *p)
581 struct list_head *lh;
582 struct inet_protosw *answer;
583 int protocol = p->protocol;
584 struct list_head *last_perm;
586 spin_lock_bh(&inetsw6_lock);
588 if (p->type > SOCK_MAX)
591 /* If we are trying to override a permanent protocol, bail. */
593 last_perm = &inetsw6[p->type];
594 list_for_each(lh, &inetsw6[p->type]) {
595 answer = list_entry(lh, struct inet_protosw, list);
597 /* Check only the non-wild match. */
598 if (INET_PROTOSW_PERMANENT & answer->flags) {
599 if (protocol == answer->protocol)
609 /* Add the new entry after the last permanent entry if any, so that
610 * the new entry does not override a permanent entry when matched with
611 * a wild-card protocol. But it is allowed to override any existing
612 * non-permanent entry. This means that when we remove this entry, the
613 * system automatically returns to the old behavior.
615 list_add_rcu(&p->list, last_perm);
617 spin_unlock_bh(&inetsw6_lock);
621 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
627 "Ignoring attempt to register illegal socket type %d.\n",
633 inet6_unregister_protosw(struct inet_protosw *p)
635 if (INET_PROTOSW_PERMANENT & p->flags) {
637 "Attempt to unregister permanent protocol %d.\n",
640 spin_lock_bh(&inetsw6_lock);
641 list_del_rcu(&p->list);
642 spin_unlock_bh(&inetsw6_lock);
649 snmp6_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
654 ptr[0] = __alloc_percpu(mibsize, mibalign);
658 ptr[1] = __alloc_percpu(mibsize, mibalign);
672 snmp6_mib_free(void *ptr[2])
678 ptr[0] = ptr[1] = NULL;
681 static int __init init_ipv6_mibs(void)
683 if (snmp6_mib_init((void **)ipv6_statistics, sizeof (struct ipv6_mib),
684 __alignof__(struct ipv6_mib)) < 0)
686 if (snmp6_mib_init((void **)icmpv6_statistics, sizeof (struct icmpv6_mib),
687 __alignof__(struct ipv6_mib)) < 0)
689 if (snmp6_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib),
690 __alignof__(struct ipv6_mib)) < 0)
695 snmp6_mib_free((void **)icmpv6_statistics);
697 snmp6_mib_free((void **)ipv6_statistics);
703 static void cleanup_ipv6_mibs(void)
705 snmp6_mib_free((void **)ipv6_statistics);
706 snmp6_mib_free((void **)icmpv6_statistics);
707 snmp6_mib_free((void **)udp_stats_in6);
710 extern int ipv6_misc_proc_init(void);
712 static int __init inet6_init(void)
714 struct sk_buff *dummy_skb;
719 #if 0 /* FIXME --RR */
720 if (!mod_member_present(&__this_module, can_unload))
723 __this_module.can_unload = &ipv6_unload;
727 printk(KERN_INFO "IPv6 v0.8 for NET4.0\n");
729 if (sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb))
731 printk(KERN_CRIT "inet6_proto_init: size fault\n");
734 /* allocate our sock slab caches */
735 tcp6_sk_cachep = kmem_cache_create("tcp6_sock",
736 sizeof(struct tcp6_sock), 0,
737 SLAB_HWCACHE_ALIGN, 0, 0);
738 udp6_sk_cachep = kmem_cache_create("udp6_sock",
739 sizeof(struct udp6_sock), 0,
740 SLAB_HWCACHE_ALIGN, 0, 0);
741 raw6_sk_cachep = kmem_cache_create("raw6_sock",
742 sizeof(struct raw6_sock), 0,
743 SLAB_HWCACHE_ALIGN, 0, 0);
744 if (!tcp6_sk_cachep || !udp6_sk_cachep || !raw6_sk_cachep)
745 printk(KERN_CRIT "%s: Can't create protocol sock SLAB "
746 "caches!\n", __FUNCTION__);
748 /* Register the socket-side information for inet6_create. */
749 for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
752 /* We MUST register RAW sockets before we create the ICMP6,
753 * IGMP6, or NDISC control sockets.
755 inet6_register_protosw(&rawv6_protosw);
757 /* Register the family here so that the init calls below will
758 * be able to create sockets. (?? is this dangerous ??)
760 (void) sock_register(&inet6_family_ops);
762 /* Initialise ipv6 mibs */
763 err = init_ipv6_mibs();
768 * ipngwg API draft makes clear that the correct semantics
769 * for TCP and UDP is to consider one TCP and UDP instance
770 * in a host availiable by both INET and INET6 APIs and
771 * able to communicate via both network protocols.
774 #if defined(MODULE) && defined(CONFIG_SYSCTL)
775 ipv6_sysctl_register();
777 err = icmpv6_init(&inet6_family_ops);
780 err = ndisc_init(&inet6_family_ops);
783 #ifdef CONFIG_IPV6_TUNNEL
784 err = ip6_tunnel_init();
786 goto ip6_tunnel_fail;
788 err = igmp6_init(&inet6_family_ops);
791 /* Create /proc/foo6 entries. */
792 #ifdef CONFIG_PROC_FS
794 if (raw6_proc_init())
796 if (tcp6_proc_init())
798 if (udp6_proc_init())
800 if (ipv6_misc_proc_init())
801 goto proc_misc6_fail;
804 goto proc_anycast6_fail;
808 ipv6_netdev_notif_init();
811 ip6_flowlabel_init();
815 /* Init v6 extension headers. */
821 /* Init v6 transport protocols. */
827 #ifdef CONFIG_PROC_FS
831 ipv6_misc_proc_exit();
842 #ifdef CONFIG_IPV6_TUNNEL
843 ip6_tunnel_cleanup();
850 #if defined(MODULE) && defined(CONFIG_SYSCTL)
851 ipv6_sysctl_unregister();
857 module_init(inet6_init);
861 static void inet6_exit(void)
863 /* First of all disallow new sockets creation. */
864 sock_unregister(PF_INET6);
865 #ifdef CONFIG_PROC_FS
868 ipv6_misc_proc_exit();
873 /* Cleanup code parts. */
875 ipv6_netdev_notif_cleanup();
876 ip6_flowlabel_cleanup();
879 ipv6_packet_cleanup();
881 #ifdef CONFIG_IPV6_TUNNEL
882 ip6_tunnel_cleanup();
887 ipv6_sysctl_unregister();
890 kmem_cache_destroy(tcp6_sk_cachep);
891 kmem_cache_destroy(udp6_sk_cachep);
892 kmem_cache_destroy(raw6_sk_cachep);
894 module_exit(inet6_exit);