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 /* Per protocol sock slabcache */
94 kmem_cache_t *tcp6_sk_cachep;
95 kmem_cache_t *udp6_sk_cachep;
96 kmem_cache_t *raw6_sk_cachep;
98 /* The inetsw table contains everything that inet_create needs to
101 static struct list_head inetsw6[SOCK_MAX];
102 static spinlock_t inetsw6_lock = SPIN_LOCK_UNLOCKED;
104 static void inet6_sock_destruct(struct sock *sk)
106 inet_sock_destruct(sk);
108 #ifdef INET_REFCNT_DEBUG
109 atomic_dec(&inet6_sock_nr);
113 static __inline__ kmem_cache_t *inet6_sk_slab(int protocol)
115 kmem_cache_t* rc = tcp6_sk_cachep;
117 if (protocol == IPPROTO_UDP)
119 else if (protocol == IPPROTO_RAW)
124 static __inline__ int inet6_sk_size(int protocol)
126 int rc = sizeof(struct tcp6_sock);
128 if (protocol == IPPROTO_UDP)
129 rc = sizeof(struct udp6_sock);
130 else if (protocol == IPPROTO_RAW)
131 rc = sizeof(struct raw6_sock);
135 static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
137 struct ipv6_pinfo *rc = (&((struct tcp6_sock *)sk)->inet6);
139 if (sk->sk_protocol == IPPROTO_UDP)
140 rc = (&((struct udp6_sock *)sk)->inet6);
141 else if (sk->sk_protocol == IPPROTO_RAW)
142 rc = (&((struct raw6_sock *)sk)->inet6);
146 static int inet6_create(struct socket *sock, int protocol)
148 struct inet_opt *inet;
149 struct ipv6_pinfo *np;
151 struct tcp6_sock* tcp6sk;
153 struct inet_protosw *answer;
155 sk = sk_alloc(PF_INET6, GFP_KERNEL, inet6_sk_size(protocol),
156 inet6_sk_slab(protocol));
160 /* Look for the requested type/protocol pair. */
163 list_for_each_rcu(p, &inetsw6[sock->type]) {
164 answer = list_entry(p, struct inet_protosw, list);
166 /* Check the non-wild match. */
167 if (protocol == answer->protocol) {
168 if (protocol != IPPROTO_IP)
171 /* Check for the two wild cases. */
172 if (IPPROTO_IP == protocol) {
173 protocol = answer->protocol;
176 if (IPPROTO_IP == answer->protocol)
183 goto free_and_badtype;
184 if (answer->capability > 0 && !capable(answer->capability))
185 goto free_and_badperm;
187 goto free_and_noproto;
189 sock->ops = answer->ops;
190 sock_init_data(sock, sk);
191 sk_set_owner(sk, THIS_MODULE);
193 sk->sk_prot = answer->prot;
194 sk->sk_no_check = answer->no_check;
195 if (INET_PROTOSW_REUSE & answer->flags)
201 if (SOCK_RAW == sock->type) {
202 inet->num = protocol;
203 if (IPPROTO_RAW == protocol)
207 sk->sk_destruct = inet6_sock_destruct;
209 sk->sk_family = PF_INET6;
210 sk->sk_protocol = protocol;
212 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
214 tcp6sk = (struct tcp6_sock *)sk;
215 tcp6sk->pinet6 = np = inet6_sk_generic(sk);
219 np->pmtudisc = IPV6_PMTUDISC_WANT;
220 np->ipv6only = sysctl_ipv6_bindv6only;
222 /* Init the ipv4 part of the socket since we can have sockets
223 * using v6 API for ipv4.
230 inet->mc_list = NULL;
232 if (ipv4_config.no_pmtu_disc)
233 inet->pmtudisc = IP_PMTUDISC_DONT;
235 inet->pmtudisc = IP_PMTUDISC_WANT;
238 #ifdef INET_REFCNT_DEBUG
239 atomic_inc(&inet6_sock_nr);
240 atomic_inc(&inet_sock_nr);
243 /* It assumes that any protocol which allows
244 * the user to assign a number at socket
245 * creation time automatically shares.
247 inet->sport = ntohs(inet->num);
248 sk->sk_prot->hash(sk);
250 if (sk->sk_prot->init) {
251 int err = sk->sk_prot->init(sk);
253 inet_sock_release(sk);
262 return -ESOCKTNOSUPPORT;
270 return -EPROTONOSUPPORT;
276 /* bind for INET6 API */
277 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
279 struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
280 struct sock *sk = sock->sk;
281 struct inet_opt *inet = inet_sk(sk);
282 struct ipv6_pinfo *np = inet6_sk(sk);
288 /* If the socket has its own bind function then use it. */
289 if (sk->sk_prot->bind)
290 return sk->sk_prot->bind(sk, uaddr, addr_len);
292 if (addr_len < SIN6_LEN_RFC2133)
294 addr_type = ipv6_addr_type(&addr->sin6_addr);
295 if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
298 snum = ntohs(addr->sin6_port);
299 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
304 /* Check these errors (active socket, double bind). */
305 if (sk->sk_state != TCP_CLOSE || inet->num) {
310 /* Check if the address belongs to the host. */
311 if (addr_type == IPV6_ADDR_MAPPED) {
312 v4addr = addr->sin6_addr.s6_addr32[3];
313 if (inet_addr_type(v4addr) != RTN_LOCAL) {
314 err = -EADDRNOTAVAIL;
318 if (addr_type != IPV6_ADDR_ANY) {
319 struct net_device *dev = NULL;
321 if (addr_type & IPV6_ADDR_LINKLOCAL) {
322 if (addr_len >= sizeof(struct sockaddr_in6) &&
323 addr->sin6_scope_id) {
324 /* Override any existing binding, if another one
325 * is supplied by user.
327 sk->sk_bound_dev_if = addr->sin6_scope_id;
330 /* Binding to link-local address requires an interface */
331 if (!sk->sk_bound_dev_if) {
335 dev = dev_get_by_index(sk->sk_bound_dev_if);
342 /* ipv4 addr of the socket is invalid. Only the
343 * unspecified and mapped address have a v4 equivalent.
345 v4addr = LOOPBACK4_IPV6;
346 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
347 if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) {
350 err = -EADDRNOTAVAIL;
359 inet->rcv_saddr = v4addr;
360 inet->saddr = v4addr;
362 ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
364 if (!(addr_type & IPV6_ADDR_MULTICAST))
365 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
367 /* Make sure we are allowed to bind here. */
368 if (sk->sk_prot->get_port(sk, snum)) {
369 inet_reset_saddr(sk);
374 if (addr_type != IPV6_ADDR_ANY)
375 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
377 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
378 inet->sport = ntohs(inet->num);
386 int inet6_release(struct socket *sock)
388 struct sock *sk = sock->sk;
394 ipv6_sock_mc_close(sk);
397 ipv6_sock_ac_close(sk);
399 return inet_release(sock);
402 int inet6_destroy_sock(struct sock *sk)
404 struct ipv6_pinfo *np = inet6_sk(sk);
406 struct ipv6_txoptions *opt;
409 * Release destination entry
414 /* Release rx options */
416 if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
419 /* Free flowlabels */
420 fl6_free_socklist(sk);
422 /* Free tx options */
424 if ((opt = xchg(&np->opt, NULL)) != NULL)
425 sock_kfree_s(sk, opt, opt->tot_len);
431 * This does both peername and sockname.
434 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
435 int *uaddr_len, int peer)
437 struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
438 struct sock *sk = sock->sk;
439 struct inet_opt *inet = inet_sk(sk);
440 struct ipv6_pinfo *np = inet6_sk(sk);
442 sin->sin6_family = AF_INET6;
443 sin->sin6_flowinfo = 0;
444 sin->sin6_scope_id = 0;
448 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
451 sin->sin6_port = inet->dport;
452 ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
454 sin->sin6_flowinfo = np->flow_label;
456 if (ipv6_addr_any(&np->rcv_saddr))
457 ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
459 ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
461 sin->sin6_port = inet->sport;
463 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
464 sin->sin6_scope_id = sk->sk_bound_dev_if;
465 *uaddr_len = sizeof(*sin);
469 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
471 struct sock *sk = sock->sk;
477 return sock_get_timestamp(sk, (struct timeval *)arg);
482 return(ipv6_route_ioctl(cmd,(void *)arg));
485 return addrconf_add_ifaddr((void *) arg);
487 return addrconf_del_ifaddr((void *) arg);
489 return addrconf_set_dstaddr((void *) arg);
491 if (!sk->sk_prot->ioctl ||
492 (err = sk->sk_prot->ioctl(sk, cmd, arg)) == -ENOIOCTLCMD)
493 return(dev_ioctl(cmd,(void *) arg));
500 struct proto_ops inet6_stream_ops = {
502 .owner = THIS_MODULE,
503 .release = inet6_release,
505 .connect = inet_stream_connect, /* ok */
506 .socketpair = sock_no_socketpair, /* a do nothing */
507 .accept = inet_accept, /* ok */
508 .getname = inet6_getname,
509 .poll = tcp_poll, /* ok */
510 .ioctl = inet6_ioctl, /* must change */
511 .listen = inet_listen, /* ok */
512 .shutdown = inet_shutdown, /* ok */
513 .setsockopt = inet_setsockopt, /* ok */
514 .getsockopt = inet_getsockopt, /* ok */
515 .sendmsg = inet_sendmsg, /* ok */
516 .recvmsg = inet_recvmsg, /* ok */
517 .mmap = sock_no_mmap,
518 .sendpage = tcp_sendpage
521 struct proto_ops inet6_dgram_ops = {
523 .owner = THIS_MODULE,
524 .release = inet6_release,
526 .connect = inet_dgram_connect, /* ok */
527 .socketpair = sock_no_socketpair, /* a do nothing */
528 .accept = sock_no_accept, /* a do nothing */
529 .getname = inet6_getname,
530 .poll = datagram_poll, /* ok */
531 .ioctl = inet6_ioctl, /* must change */
532 .listen = sock_no_listen, /* ok */
533 .shutdown = inet_shutdown, /* ok */
534 .setsockopt = inet_setsockopt, /* ok */
535 .getsockopt = inet_getsockopt, /* ok */
536 .sendmsg = inet_sendmsg, /* ok */
537 .recvmsg = inet_recvmsg, /* ok */
538 .mmap = sock_no_mmap,
539 .sendpage = sock_no_sendpage,
542 static struct net_proto_family inet6_family_ops = {
544 .create = inet6_create,
545 .owner = THIS_MODULE,
549 extern void ipv6_sysctl_register(void);
550 extern void ipv6_sysctl_unregister(void);
553 static struct inet_protosw rawv6_protosw = {
555 .protocol = IPPROTO_IP, /* wild card */
557 .ops = &inet6_dgram_ops,
558 .capability = CAP_NET_RAW,
559 .no_check = UDP_CSUM_DEFAULT,
560 .flags = INET_PROTOSW_REUSE,
563 #define INETSW6_ARRAY_LEN (sizeof(inetsw6_array) / sizeof(struct inet_protosw))
566 inet6_register_protosw(struct inet_protosw *p)
568 struct list_head *lh;
569 struct inet_protosw *answer;
570 int protocol = p->protocol;
571 struct list_head *last_perm;
573 spin_lock_bh(&inetsw6_lock);
575 if (p->type > SOCK_MAX)
578 /* If we are trying to override a permanent protocol, bail. */
580 last_perm = &inetsw6[p->type];
581 list_for_each(lh, &inetsw6[p->type]) {
582 answer = list_entry(lh, struct inet_protosw, list);
584 /* Check only the non-wild match. */
585 if (INET_PROTOSW_PERMANENT & answer->flags) {
586 if (protocol == answer->protocol)
596 /* Add the new entry after the last permanent entry if any, so that
597 * the new entry does not override a permanent entry when matched with
598 * a wild-card protocol. But it is allowed to override any existing
599 * non-permanent entry. This means that when we remove this entry, the
600 * system automatically returns to the old behavior.
602 list_add_rcu(&p->list, last_perm);
604 spin_unlock_bh(&inetsw6_lock);
608 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
614 "Ignoring attempt to register invalid socket type %d.\n",
620 inet6_unregister_protosw(struct inet_protosw *p)
622 if (INET_PROTOSW_PERMANENT & p->flags) {
624 "Attempt to unregister permanent protocol %d.\n",
627 spin_lock_bh(&inetsw6_lock);
628 list_del_rcu(&p->list);
629 spin_unlock_bh(&inetsw6_lock);
636 snmp6_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
641 ptr[0] = __alloc_percpu(mibsize, mibalign);
645 ptr[1] = __alloc_percpu(mibsize, mibalign);
659 snmp6_mib_free(void *ptr[2])
665 ptr[0] = ptr[1] = NULL;
668 static int __init init_ipv6_mibs(void)
670 if (snmp6_mib_init((void **)ipv6_statistics, sizeof (struct ipv6_mib),
671 __alignof__(struct ipv6_mib)) < 0)
673 if (snmp6_mib_init((void **)icmpv6_statistics, sizeof (struct icmpv6_mib),
674 __alignof__(struct ipv6_mib)) < 0)
676 if (snmp6_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib),
677 __alignof__(struct ipv6_mib)) < 0)
682 snmp6_mib_free((void **)icmpv6_statistics);
684 snmp6_mib_free((void **)ipv6_statistics);
690 static void cleanup_ipv6_mibs(void)
692 snmp6_mib_free((void **)ipv6_statistics);
693 snmp6_mib_free((void **)icmpv6_statistics);
694 snmp6_mib_free((void **)udp_stats_in6);
697 extern int ipv6_misc_proc_init(void);
699 static int __init inet6_init(void)
701 struct sk_buff *dummy_skb;
706 #if 0 /* FIXME --RR */
707 if (!mod_member_present(&__this_module, can_unload))
710 __this_module.can_unload = &ipv6_unload;
714 if (sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb))
716 printk(KERN_CRIT "inet6_proto_init: size fault\n");
719 /* allocate our sock slab caches */
720 tcp6_sk_cachep = kmem_cache_create("tcp6_sock",
721 sizeof(struct tcp6_sock), 0,
722 SLAB_HWCACHE_ALIGN, 0, 0);
723 udp6_sk_cachep = kmem_cache_create("udp6_sock",
724 sizeof(struct udp6_sock), 0,
725 SLAB_HWCACHE_ALIGN, 0, 0);
726 raw6_sk_cachep = kmem_cache_create("raw6_sock",
727 sizeof(struct raw6_sock), 0,
728 SLAB_HWCACHE_ALIGN, 0, 0);
729 if (!tcp6_sk_cachep || !udp6_sk_cachep || !raw6_sk_cachep)
730 printk(KERN_CRIT "%s: Can't create protocol sock SLAB "
731 "caches!\n", __FUNCTION__);
733 /* Register the socket-side information for inet6_create. */
734 for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
737 /* We MUST register RAW sockets before we create the ICMP6,
738 * IGMP6, or NDISC control sockets.
740 inet6_register_protosw(&rawv6_protosw);
742 /* Register the family here so that the init calls below will
743 * be able to create sockets. (?? is this dangerous ??)
745 (void) sock_register(&inet6_family_ops);
747 /* Initialise ipv6 mibs */
748 err = init_ipv6_mibs();
753 * ipngwg API draft makes clear that the correct semantics
754 * for TCP and UDP is to consider one TCP and UDP instance
755 * in a host availiable by both INET and INET6 APIs and
756 * able to communicate via both network protocols.
760 ipv6_sysctl_register();
762 err = icmpv6_init(&inet6_family_ops);
765 err = ndisc_init(&inet6_family_ops);
768 err = igmp6_init(&inet6_family_ops);
771 /* Create /proc/foo6 entries. */
772 #ifdef CONFIG_PROC_FS
774 if (raw6_proc_init())
776 if (tcp6_proc_init())
778 if (udp6_proc_init())
780 if (ipv6_misc_proc_init())
781 goto proc_misc6_fail;
784 goto proc_anycast6_fail;
790 ip6_flowlabel_init();
794 /* Init v6 extension headers. */
800 /* Init v6 transport protocols. */
806 #ifdef CONFIG_PROC_FS
810 ipv6_misc_proc_exit();
826 ipv6_sysctl_unregister();
832 module_init(inet6_init);
834 static void __exit inet6_exit(void)
836 /* First of all disallow new sockets creation. */
837 sock_unregister(PF_INET6);
838 #ifdef CONFIG_PROC_FS
841 ipv6_misc_proc_exit();
846 /* Cleanup code parts. */
848 ip6_flowlabel_cleanup();
851 ipv6_packet_cleanup();
856 ipv6_sysctl_unregister();
859 kmem_cache_destroy(tcp6_sk_cachep);
860 kmem_cache_destroy(udp6_sk_cachep);
861 kmem_cache_destroy(raw6_sk_cachep);
863 module_exit(inet6_exit);
865 MODULE_ALIAS_NETPROTO(PF_INET6);