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;
91 EXPORT_SYMBOL(inet6_sock_nr);
94 /* The inetsw table contains everything that inet_create needs to
97 static struct list_head inetsw6[SOCK_MAX];
98 static DEFINE_SPINLOCK(inetsw6_lock);
100 static void inet6_sock_destruct(struct sock *sk)
102 inet_sock_destruct(sk);
104 #ifdef INET_REFCNT_DEBUG
105 atomic_dec(&inet6_sock_nr);
109 static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
111 const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
113 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
116 static int inet6_create(struct socket *sock, int protocol)
118 struct inet_sock *inet;
119 struct ipv6_pinfo *np;
122 struct inet_protosw *answer;
123 struct proto *answer_prot;
124 unsigned char answer_flags;
125 char answer_no_check;
128 /* Look for the requested type/protocol pair. */
131 list_for_each_rcu(p, &inetsw6[sock->type]) {
132 answer = list_entry(p, struct inet_protosw, list);
134 /* Check the non-wild match. */
135 if (protocol == answer->protocol) {
136 if (protocol != IPPROTO_IP)
139 /* Check for the two wild cases. */
140 if (IPPROTO_IP == protocol) {
141 protocol = answer->protocol;
144 if (IPPROTO_IP == answer->protocol)
150 rc = -ESOCKTNOSUPPORT;
154 if (answer->capability > 0 && !capable(answer->capability))
156 rc = -EPROTONOSUPPORT;
160 sock->ops = answer->ops;
162 answer_prot = answer->prot;
163 answer_no_check = answer->no_check;
164 answer_flags = answer->flags;
167 BUG_TRAP(answer_prot->slab != NULL);
170 sk = sk_alloc(PF_INET6, GFP_KERNEL, answer_prot, 1);
174 sock_init_data(sock, sk);
177 sk->sk_no_check = answer_no_check;
178 if (INET_PROTOSW_REUSE & answer_flags)
183 if (SOCK_RAW == sock->type) {
184 inet->num = protocol;
185 if (IPPROTO_RAW == protocol)
189 sk->sk_destruct = inet6_sock_destruct;
190 sk->sk_family = PF_INET6;
191 sk->sk_protocol = protocol;
193 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
195 inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
199 np->pmtudisc = IPV6_PMTUDISC_WANT;
200 np->ipv6only = sysctl_ipv6_bindv6only;
202 /* Init the ipv4 part of the socket since we can have sockets
203 * using v6 API for ipv4.
210 inet->mc_list = NULL;
212 if (ipv4_config.no_pmtu_disc)
213 inet->pmtudisc = IP_PMTUDISC_DONT;
215 inet->pmtudisc = IP_PMTUDISC_WANT;
218 #ifdef INET_REFCNT_DEBUG
219 atomic_inc(&inet6_sock_nr);
220 atomic_inc(&inet_sock_nr);
223 /* It assumes that any protocol which allows
224 * the user to assign a number at socket
225 * creation time automatically shares.
227 inet->sport = ntohs(inet->num);
228 sk->sk_prot->hash(sk);
230 if (sk->sk_prot->init) {
231 rc = sk->sk_prot->init(sk);
233 sk_common_release(sk);
245 /* bind for INET6 API */
246 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
248 struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
249 struct sock *sk = sock->sk;
250 struct inet_sock *inet = inet_sk(sk);
251 struct ipv6_pinfo *np = inet6_sk(sk);
257 /* If the socket has its own bind function then use it. */
258 if (sk->sk_prot->bind)
259 return sk->sk_prot->bind(sk, uaddr, addr_len);
261 if (addr_len < SIN6_LEN_RFC2133)
263 addr_type = ipv6_addr_type(&addr->sin6_addr);
264 if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
267 snum = ntohs(addr->sin6_port);
268 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
273 /* Check these errors (active socket, double bind). */
274 if (sk->sk_state != TCP_CLOSE || inet->num) {
279 /* Check if the address belongs to the host. */
280 if (addr_type == IPV6_ADDR_MAPPED) {
281 v4addr = addr->sin6_addr.s6_addr32[3];
282 if (inet_addr_type(v4addr) != RTN_LOCAL) {
283 err = -EADDRNOTAVAIL;
287 if (addr_type != IPV6_ADDR_ANY) {
288 struct net_device *dev = NULL;
290 if (addr_type & IPV6_ADDR_LINKLOCAL) {
291 if (addr_len >= sizeof(struct sockaddr_in6) &&
292 addr->sin6_scope_id) {
293 /* Override any existing binding, if another one
294 * is supplied by user.
296 sk->sk_bound_dev_if = addr->sin6_scope_id;
299 /* Binding to link-local address requires an interface */
300 if (!sk->sk_bound_dev_if) {
304 dev = dev_get_by_index(sk->sk_bound_dev_if);
311 /* ipv4 addr of the socket is invalid. Only the
312 * unspecified and mapped address have a v4 equivalent.
314 v4addr = LOOPBACK4_IPV6;
315 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
316 if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) {
319 err = -EADDRNOTAVAIL;
328 inet->rcv_saddr = v4addr;
329 inet->saddr = v4addr;
331 ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
333 if (!(addr_type & IPV6_ADDR_MULTICAST))
334 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
336 /* Make sure we are allowed to bind here. */
337 if (sk->sk_prot->get_port(sk, snum)) {
338 inet_reset_saddr(sk);
343 if (addr_type != IPV6_ADDR_ANY)
344 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
346 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
347 inet->sport = ntohs(inet->num);
355 int inet6_release(struct socket *sock)
357 struct sock *sk = sock->sk;
363 ipv6_sock_mc_close(sk);
366 ipv6_sock_ac_close(sk);
368 return inet_release(sock);
371 int inet6_destroy_sock(struct sock *sk)
373 struct ipv6_pinfo *np = inet6_sk(sk);
375 struct ipv6_txoptions *opt;
378 * Release destination entry
383 /* Release rx options */
385 if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
388 /* Free flowlabels */
389 fl6_free_socklist(sk);
391 /* Free tx options */
393 if ((opt = xchg(&np->opt, NULL)) != NULL)
394 sock_kfree_s(sk, opt, opt->tot_len);
400 * This does both peername and sockname.
403 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
404 int *uaddr_len, int peer)
406 struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
407 struct sock *sk = sock->sk;
408 struct inet_sock *inet = inet_sk(sk);
409 struct ipv6_pinfo *np = inet6_sk(sk);
411 sin->sin6_family = AF_INET6;
412 sin->sin6_flowinfo = 0;
413 sin->sin6_scope_id = 0;
417 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
420 sin->sin6_port = inet->dport;
421 ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
423 sin->sin6_flowinfo = np->flow_label;
425 if (ipv6_addr_any(&np->rcv_saddr))
426 ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
428 ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
430 sin->sin6_port = inet->sport;
432 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
433 sin->sin6_scope_id = sk->sk_bound_dev_if;
434 *uaddr_len = sizeof(*sin);
438 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
440 struct sock *sk = sock->sk;
446 return sock_get_timestamp(sk, (struct timeval __user *)arg);
451 return(ipv6_route_ioctl(cmd,(void __user *)arg));
454 return addrconf_add_ifaddr((void __user *) arg);
456 return addrconf_del_ifaddr((void __user *) arg);
458 return addrconf_set_dstaddr((void __user *) arg);
460 if (!sk->sk_prot->ioctl ||
461 (err = sk->sk_prot->ioctl(sk, cmd, arg)) == -ENOIOCTLCMD)
462 return(dev_ioctl(cmd,(void __user *) arg));
469 struct proto_ops inet6_stream_ops = {
471 .owner = THIS_MODULE,
472 .release = inet6_release,
474 .connect = inet_stream_connect, /* ok */
475 .socketpair = sock_no_socketpair, /* a do nothing */
476 .accept = inet_accept, /* ok */
477 .getname = inet6_getname,
478 .poll = tcp_poll, /* ok */
479 .ioctl = inet6_ioctl, /* must change */
480 .listen = inet_listen, /* ok */
481 .shutdown = inet_shutdown, /* ok */
482 .setsockopt = sock_common_setsockopt, /* ok */
483 .getsockopt = sock_common_getsockopt, /* ok */
484 .sendmsg = inet_sendmsg, /* ok */
485 .recvmsg = sock_common_recvmsg, /* ok */
486 .mmap = sock_no_mmap,
487 .sendpage = tcp_sendpage
490 struct proto_ops inet6_dgram_ops = {
492 .owner = THIS_MODULE,
493 .release = inet6_release,
495 .connect = inet_dgram_connect, /* ok */
496 .socketpair = sock_no_socketpair, /* a do nothing */
497 .accept = sock_no_accept, /* a do nothing */
498 .getname = inet6_getname,
499 .poll = udp_poll, /* ok */
500 .ioctl = inet6_ioctl, /* must change */
501 .listen = sock_no_listen, /* ok */
502 .shutdown = inet_shutdown, /* ok */
503 .setsockopt = sock_common_setsockopt, /* ok */
504 .getsockopt = sock_common_getsockopt, /* ok */
505 .sendmsg = inet_sendmsg, /* ok */
506 .recvmsg = sock_common_recvmsg, /* ok */
507 .mmap = sock_no_mmap,
508 .sendpage = sock_no_sendpage,
511 static struct net_proto_family inet6_family_ops = {
513 .create = inet6_create,
514 .owner = THIS_MODULE,
518 extern void ipv6_sysctl_register(void);
519 extern void ipv6_sysctl_unregister(void);
522 /* Same as inet6_dgram_ops, sans udp_poll. */
523 static struct proto_ops inet6_sockraw_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 = sock_common_setsockopt, /* ok */
537 .getsockopt = sock_common_getsockopt, /* ok */
538 .sendmsg = inet_sendmsg, /* ok */
539 .recvmsg = sock_common_recvmsg, /* ok */
540 .mmap = sock_no_mmap,
541 .sendpage = sock_no_sendpage,
544 static struct inet_protosw rawv6_protosw = {
546 .protocol = IPPROTO_IP, /* wild card */
548 .ops = &inet6_sockraw_ops,
549 .capability = CAP_NET_RAW,
550 .no_check = UDP_CSUM_DEFAULT,
551 .flags = INET_PROTOSW_REUSE,
555 inet6_register_protosw(struct inet_protosw *p)
557 struct list_head *lh;
558 struct inet_protosw *answer;
559 int protocol = p->protocol;
560 struct list_head *last_perm;
562 spin_lock_bh(&inetsw6_lock);
564 if (p->type >= SOCK_MAX)
567 /* If we are trying to override a permanent protocol, bail. */
569 last_perm = &inetsw6[p->type];
570 list_for_each(lh, &inetsw6[p->type]) {
571 answer = list_entry(lh, struct inet_protosw, list);
573 /* Check only the non-wild match. */
574 if (INET_PROTOSW_PERMANENT & answer->flags) {
575 if (protocol == answer->protocol)
585 /* Add the new entry after the last permanent entry if any, so that
586 * the new entry does not override a permanent entry when matched with
587 * a wild-card protocol. But it is allowed to override any existing
588 * non-permanent entry. This means that when we remove this entry, the
589 * system automatically returns to the old behavior.
591 list_add_rcu(&p->list, last_perm);
593 spin_unlock_bh(&inetsw6_lock);
597 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
603 "Ignoring attempt to register invalid socket type %d.\n",
609 inet6_unregister_protosw(struct inet_protosw *p)
611 if (INET_PROTOSW_PERMANENT & p->flags) {
613 "Attempt to unregister permanent protocol %d.\n",
616 spin_lock_bh(&inetsw6_lock);
617 list_del_rcu(&p->list);
618 spin_unlock_bh(&inetsw6_lock);
625 snmp6_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
630 ptr[0] = __alloc_percpu(mibsize, mibalign);
634 ptr[1] = __alloc_percpu(mibsize, mibalign);
648 snmp6_mib_free(void *ptr[2])
656 ptr[0] = ptr[1] = NULL;
659 static int __init init_ipv6_mibs(void)
661 if (snmp6_mib_init((void **)ipv6_statistics, sizeof (struct ipstats_mib),
662 __alignof__(struct ipstats_mib)) < 0)
664 if (snmp6_mib_init((void **)icmpv6_statistics, sizeof (struct icmpv6_mib),
665 __alignof__(struct icmpv6_mib)) < 0)
667 if (snmp6_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib),
668 __alignof__(struct udp_mib)) < 0)
673 snmp6_mib_free((void **)icmpv6_statistics);
675 snmp6_mib_free((void **)ipv6_statistics);
681 static void cleanup_ipv6_mibs(void)
683 snmp6_mib_free((void **)ipv6_statistics);
684 snmp6_mib_free((void **)icmpv6_statistics);
685 snmp6_mib_free((void **)udp_stats_in6);
688 extern int ipv6_misc_proc_init(void);
690 static int __init inet6_init(void)
692 struct sk_buff *dummy_skb;
697 #if 0 /* FIXME --RR */
698 if (!mod_member_present(&__this_module, can_unload))
701 __this_module.can_unload = &ipv6_unload;
705 if (sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb)) {
706 printk(KERN_CRIT "inet6_proto_init: size fault\n");
710 err = proto_register(&tcpv6_prot, 1);
714 err = proto_register(&udpv6_prot, 1);
716 goto out_unregister_tcp_proto;
718 err = proto_register(&rawv6_prot, 1);
720 goto out_unregister_udp_proto;
723 /* Register the socket-side information for inet6_create. */
724 for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
727 /* We MUST register RAW sockets before we create the ICMP6,
728 * IGMP6, or NDISC control sockets.
730 inet6_register_protosw(&rawv6_protosw);
732 /* Register the family here so that the init calls below will
733 * be able to create sockets. (?? is this dangerous ??)
735 (void) sock_register(&inet6_family_ops);
737 /* Initialise ipv6 mibs */
738 err = init_ipv6_mibs();
740 goto out_unregister_raw_proto;
743 * ipngwg API draft makes clear that the correct semantics
744 * for TCP and UDP is to consider one TCP and UDP instance
745 * in a host availiable by both INET and INET6 APIs and
746 * able to communicate via both network protocols.
750 ipv6_sysctl_register();
752 err = icmpv6_init(&inet6_family_ops);
755 err = ndisc_init(&inet6_family_ops);
758 err = igmp6_init(&inet6_family_ops);
761 /* Create /proc/foo6 entries. */
762 #ifdef CONFIG_PROC_FS
764 if (raw6_proc_init())
766 if (tcp6_proc_init())
768 if (udp6_proc_init())
770 if (ipv6_misc_proc_init())
771 goto proc_misc6_fail;
774 goto proc_anycast6_fail;
780 ip6_flowlabel_init();
781 err = addrconf_init();
786 /* Init v6 extension headers. */
792 /* Init v6 transport protocols. */
800 ip6_flowlabel_cleanup();
802 ipv6_packet_cleanup();
803 #ifdef CONFIG_PROC_FS
808 ipv6_misc_proc_exit();
824 ipv6_sysctl_unregister();
827 out_unregister_raw_proto:
828 proto_unregister(&rawv6_prot);
829 out_unregister_udp_proto:
830 proto_unregister(&udpv6_prot);
831 out_unregister_tcp_proto:
832 proto_unregister(&tcpv6_prot);
835 module_init(inet6_init);
837 static void __exit inet6_exit(void)
839 /* First of all disallow new sockets creation. */
840 sock_unregister(PF_INET6);
841 #ifdef CONFIG_PROC_FS
844 ipv6_misc_proc_exit();
849 /* Cleanup code parts. */
851 ip6_flowlabel_cleanup();
854 ipv6_packet_cleanup();
859 ipv6_sysctl_unregister();
862 proto_unregister(&rawv6_prot);
863 proto_unregister(&udpv6_prot);
864 proto_unregister(&tcpv6_prot);
866 module_exit(inet6_exit);
868 MODULE_ALIAS_NETPROTO(PF_INET6);