2 * PF_INET6 socket protocol family
3 * Linux INET6 implementation
6 * Pedro Roque <pedro_m@yahoo.com>
8 * Adapted from linux/net/ipv4/af_inet.c
10 * $Id: af_inet6.c,v 1.65 2001/10/02 02:22:36 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/brlock.h>
49 #include <linux/smp_lock.h>
56 #include <net/protocol.h>
57 #include <net/inet_common.h>
58 #include <net/transp_v6.h>
59 #include <net/ip6_route.h>
60 #include <net/addrconf.h>
62 #include <asm/uaccess.h>
63 #include <asm/system.h>
66 static int unloadable = 0; /* XX: Turn to one when all is ok within the
67 module for allowing unload */
70 #if defined(MODULE) && LINUX_VERSION_CODE > 0x20115
71 MODULE_AUTHOR("Cast of dozens");
72 MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
73 MODULE_PARM(unloadable, "i");
76 /* IPv6 procfs goodies... */
79 extern int anycast6_get_info(char *, char **, off_t, int);
80 extern int raw6_get_info(char *, char **, off_t, int);
81 extern int tcp6_get_info(char *, char **, off_t, int);
82 extern int udp6_get_info(char *, char **, off_t, int);
83 extern int afinet6_get_info(char *, char **, off_t, int);
84 extern int afinet6_get_snmp(char *, char **, off_t, int);
88 extern void ipv6_sysctl_register(void);
89 extern void ipv6_sysctl_unregister(void);
92 int sysctl_ipv6_bindv6only;
94 #ifdef INET_REFCNT_DEBUG
95 atomic_t inet6_sock_nr;
98 /* The inetsw table contains everything that inet_create needs to
101 struct list_head inetsw6[SOCK_MAX];
103 static void inet6_sock_destruct(struct sock *sk)
105 inet_sock_destruct(sk);
107 #ifdef INET_REFCNT_DEBUG
108 atomic_dec(&inet6_sock_nr);
113 static int inet6_create(struct socket *sock, int protocol)
117 struct inet_protosw *answer;
119 sk = sk_alloc(PF_INET6, GFP_KERNEL, 1);
123 /* Look for the requested type/protocol pair. */
125 br_read_lock_bh(BR_NETPROTO_LOCK);
126 list_for_each(p, &inetsw6[sock->type]) {
127 answer = list_entry(p, struct inet_protosw, list);
129 /* Check the non-wild match. */
130 if (protocol == answer->protocol) {
131 if (protocol != IPPROTO_IP)
134 /* Check for the two wild cases. */
135 if (IPPROTO_IP == protocol) {
136 protocol = answer->protocol;
139 if (IPPROTO_IP == answer->protocol)
144 br_read_unlock_bh(BR_NETPROTO_LOCK);
147 goto free_and_badtype;
148 if (answer->capability > 0 && !capable(answer->capability))
149 goto free_and_badperm;
151 goto free_and_noproto;
153 sock->ops = answer->ops;
154 sock_init_data(sock, sk);
156 sk->prot = answer->prot;
157 sk->no_check = answer->no_check;
158 if (INET_PROTOSW_REUSE & answer->flags)
161 if (SOCK_RAW == sock->type) {
163 if (IPPROTO_RAW == protocol)
164 sk->protinfo.af_inet.hdrincl = 1;
167 sk->destruct = inet6_sock_destruct;
169 sk->family = PF_INET6;
170 sk->protocol = protocol;
172 sk->backlog_rcv = answer->prot->backlog_rcv;
174 sk->net_pinfo.af_inet6.hop_limit = -1;
175 sk->net_pinfo.af_inet6.mcast_hops = -1;
176 sk->net_pinfo.af_inet6.mc_loop = 1;
177 sk->net_pinfo.af_inet6.pmtudisc = IPV6_PMTUDISC_WANT;
179 sk->net_pinfo.af_inet6.ipv6only = sysctl_ipv6_bindv6only;
181 /* Init the ipv4 part of the socket since we can have sockets
182 * using v6 API for ipv4.
184 sk->protinfo.af_inet.ttl = 64;
186 sk->protinfo.af_inet.mc_loop = 1;
187 sk->protinfo.af_inet.mc_ttl = 1;
188 sk->protinfo.af_inet.mc_index = 0;
189 sk->protinfo.af_inet.mc_list = NULL;
191 if (ipv4_config.no_pmtu_disc)
192 sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_DONT;
194 sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_WANT;
197 #ifdef INET_REFCNT_DEBUG
198 atomic_inc(&inet6_sock_nr);
199 atomic_inc(&inet_sock_nr);
204 /* It assumes that any protocol which allows
205 * the user to assign a number at socket
206 * creation time automatically shares.
208 sk->sport = ntohs(sk->num);
211 if (sk->prot->init) {
212 int err = sk->prot->init(sk);
215 inet_sock_release(sk);
223 return -ESOCKTNOSUPPORT;
229 return -EPROTONOSUPPORT;
235 /* bind for INET6 API */
236 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
238 struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
239 struct sock *sk = sock->sk;
244 /* If the socket has its own bind function then use it. */
246 return sk->prot->bind(sk, uaddr, addr_len);
248 if (addr_len < SIN6_LEN_RFC2133)
250 addr_type = ipv6_addr_type(&addr->sin6_addr);
251 if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
254 /* Check if the address belongs to the host. */
255 if (addr_type == IPV6_ADDR_MAPPED) {
256 v4addr = addr->sin6_addr.s6_addr32[3];
257 if (inet_addr_type(v4addr) != RTN_LOCAL)
258 return -EADDRNOTAVAIL;
260 if (addr_type != IPV6_ADDR_ANY) {
261 /* ipv4 addr of the socket is invalid. Only the
262 * unspecified and mapped address have a v4 equivalent.
264 v4addr = LOOPBACK4_IPV6;
265 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
266 if (!ipv6_chk_addr(&addr->sin6_addr, NULL))
267 return -EADDRNOTAVAIL;
272 snum = ntohs(addr->sin6_port);
273 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
278 /* Check these errors (active socket, double bind). */
279 if ((sk->state != TCP_CLOSE) ||
285 if (addr_type & IPV6_ADDR_LINKLOCAL) {
286 if (addr_len >= sizeof(struct sockaddr_in6) &&
287 addr->sin6_scope_id) {
288 /* Override any existing binding, if another one
289 * is supplied by user.
291 sk->bound_dev_if = addr->sin6_scope_id;
294 /* Binding to link-local address requires an interface */
295 if (sk->bound_dev_if == 0) {
301 sk->rcv_saddr = v4addr;
304 ipv6_addr_copy(&sk->net_pinfo.af_inet6.rcv_saddr, &addr->sin6_addr);
306 if (!(addr_type & IPV6_ADDR_MULTICAST))
307 ipv6_addr_copy(&sk->net_pinfo.af_inet6.saddr, &addr->sin6_addr);
309 /* Make sure we are allowed to bind here. */
310 if (sk->prot->get_port(sk, snum) != 0) {
313 memset(&sk->net_pinfo.af_inet6.rcv_saddr, 0, sizeof(struct in6_addr));
314 memset(&sk->net_pinfo.af_inet6.saddr, 0, sizeof(struct in6_addr));
320 if (addr_type != IPV6_ADDR_ANY)
321 sk->userlocks |= SOCK_BINDADDR_LOCK;
323 sk->userlocks |= SOCK_BINDPORT_LOCK;
324 sk->sport = ntohs(sk->num);
332 int inet6_release(struct socket *sock)
334 struct sock *sk = sock->sk;
340 ipv6_sock_mc_close(sk);
343 ipv6_sock_ac_close(sk);
345 return inet_release(sock);
348 int inet6_destroy_sock(struct sock *sk)
351 struct ipv6_txoptions *opt;
354 * Release destination entry
359 /* Release rx options */
361 if ((skb = xchg(&sk->net_pinfo.af_inet6.pktoptions, NULL)) != NULL)
364 /* Free flowlabels */
365 fl6_free_socklist(sk);
367 /* Free tx options */
369 if ((opt = xchg(&sk->net_pinfo.af_inet6.opt, NULL)) != NULL)
370 sock_kfree_s(sk, opt, opt->tot_len);
376 * This does both peername and sockname.
379 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
380 int *uaddr_len, int peer)
382 struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
383 struct sock *sk = sock->sk;
385 sin->sin6_family = AF_INET6;
386 sin->sin6_flowinfo = 0;
387 sin->sin6_scope_id = 0;
391 if (((1<<sk->state)&(TCPF_CLOSE|TCPF_SYN_SENT)) && peer == 1)
393 sin->sin6_port = sk->dport;
394 memcpy(&sin->sin6_addr, &sk->net_pinfo.af_inet6.daddr,
395 sizeof(struct in6_addr));
396 if (sk->net_pinfo.af_inet6.sndflow)
397 sin->sin6_flowinfo = sk->net_pinfo.af_inet6.flow_label;
399 if (ipv6_addr_type(&sk->net_pinfo.af_inet6.rcv_saddr) == IPV6_ADDR_ANY)
400 memcpy(&sin->sin6_addr,
401 &sk->net_pinfo.af_inet6.saddr,
402 sizeof(struct in6_addr));
404 memcpy(&sin->sin6_addr,
405 &sk->net_pinfo.af_inet6.rcv_saddr,
406 sizeof(struct in6_addr));
408 sin->sin6_port = sk->sport;
410 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
411 sin->sin6_scope_id = sk->bound_dev_if;
412 *uaddr_len = sizeof(*sin);
416 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
418 struct sock *sk = sock->sk;
426 if (get_user(pid, (int *) arg))
428 /* see sock_no_fcntl */
429 if (current->pid != pid && current->pgrp != -pid &&
430 !capable(CAP_NET_ADMIN))
436 return put_user(sk->proc,(int *)arg);
438 if(sk->stamp.tv_sec==0)
440 err = copy_to_user((void *)arg, &sk->stamp,
441 sizeof(struct timeval));
449 return(ipv6_route_ioctl(cmd,(void *)arg));
452 return addrconf_add_ifaddr((void *) arg);
454 return addrconf_del_ifaddr((void *) arg);
456 return addrconf_set_dstaddr((void *) arg);
458 if ((cmd >= SIOCDEVPRIVATE) &&
459 (cmd <= (SIOCDEVPRIVATE + 15)))
460 return(dev_ioctl(cmd,(void *) arg));
462 if(sk->prot->ioctl==0 || (err=sk->prot->ioctl(sk, cmd, arg))==-ENOIOCTLCMD)
463 return(dev_ioctl(cmd,(void *) arg));
470 struct proto_ops inet6_stream_ops = {
473 release: inet6_release,
475 connect: inet_stream_connect, /* ok */
476 socketpair: sock_no_socketpair, /* a do nothing */
477 accept: inet_accept, /* ok */
478 getname: inet6_getname,
479 poll: tcp_poll, /* ok */
480 ioctl: inet6_ioctl, /* must change */
481 listen: inet_listen, /* ok */
482 shutdown: inet_shutdown, /* ok */
483 setsockopt: inet_setsockopt, /* ok */
484 getsockopt: inet_getsockopt, /* ok */
485 sendmsg: inet_sendmsg, /* ok */
486 recvmsg: inet_recvmsg, /* ok */
488 sendpage: tcp_sendpage
491 struct proto_ops inet6_dgram_ops = {
494 release: inet6_release,
496 connect: inet_dgram_connect, /* ok */
497 socketpair: sock_no_socketpair, /* a do nothing */
498 accept: sock_no_accept, /* a do nothing */
499 getname: inet6_getname,
500 poll: udp_poll, /* ok */
501 ioctl: inet6_ioctl, /* must change */
502 listen: sock_no_listen, /* ok */
503 shutdown: inet_shutdown, /* ok */
504 setsockopt: inet_setsockopt, /* ok */
505 getsockopt: inet_getsockopt, /* ok */
506 sendmsg: inet_sendmsg, /* ok */
507 recvmsg: inet_recvmsg, /* ok */
509 sendpage: sock_no_sendpage,
512 struct proto_ops inet6_sockraw_ops = {
515 release: inet6_release,
517 connect: inet_dgram_connect, /* ok */
518 socketpair: sock_no_socketpair, /* a do nothing */
519 accept: sock_no_accept, /* a do nothing */
520 getname: inet6_getname,
521 poll: datagram_poll, /* ok */
522 ioctl: inet6_ioctl, /* must change */
523 listen: sock_no_listen, /* ok */
524 shutdown: inet_shutdown, /* ok */
525 setsockopt: inet_setsockopt, /* ok */
526 getsockopt: inet_getsockopt, /* ok */
527 sendmsg: inet_sendmsg, /* ok */
528 recvmsg: inet_recvmsg, /* ok */
530 sendpage: sock_no_sendpage,
533 struct net_proto_family inet6_family_ops = {
539 int ipv6_unload(void)
541 if (!unloadable) return 1;
542 /* We keep internally 3 raw sockets */
543 return atomic_read(&(__this_module.uc.usecount)) - 3;
547 #if defined(MODULE) && defined(CONFIG_SYSCTL)
548 extern void ipv6_sysctl_register(void);
549 extern void ipv6_sysctl_unregister(void);
552 static struct inet_protosw rawv6_protosw = {
554 protocol: IPPROTO_IP, /* wild card */
556 ops: &inet6_sockraw_ops,
557 capability: CAP_NET_RAW,
558 no_check: UDP_CSUM_DEFAULT,
559 flags: INET_PROTOSW_REUSE,
562 #define INETSW6_ARRAY_LEN (sizeof(inetsw6_array) / sizeof(struct inet_protosw))
565 inet6_register_protosw(struct inet_protosw *p)
567 struct list_head *lh;
568 struct inet_protosw *answer;
569 int protocol = p->protocol;
570 struct list_head *last_perm;
572 br_write_lock_bh(BR_NETPROTO_LOCK);
574 if (p->type >= SOCK_MAX)
577 /* If we are trying to override a permanent protocol, bail. */
579 last_perm = &inetsw6[p->type];
580 list_for_each(lh, &inetsw6[p->type]) {
581 answer = list_entry(lh, struct inet_protosw, list);
583 /* Check only the non-wild match. */
584 if (INET_PROTOSW_PERMANENT & answer->flags) {
585 if (protocol == answer->protocol)
595 /* Add the new entry after the last permanent entry if any, so that
596 * the new entry does not override a permanent entry when matched with
597 * a wild-card protocol. But it is allowed to override any existing
598 * non-permanent entry. This means that when we remove this entry, the
599 * system automatically returns to the old behavior.
601 list_add(&p->list, last_perm);
603 br_write_unlock_bh(BR_NETPROTO_LOCK);
607 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
613 "Ignoring attempt to register illegal socket type %d.\n",
619 inet6_unregister_protosw(struct inet_protosw *p)
621 inet_unregister_protosw(p);
624 static int __init inet6_init(void)
626 struct sk_buff *dummy_skb;
631 if (!mod_member_present(&__this_module, can_unload))
634 __this_module.can_unload = &ipv6_unload;
637 printk(KERN_INFO "IPv6 v0.8 for NET4.0\n");
639 if (sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb))
641 printk(KERN_CRIT "inet6_proto_init: size fault\n");
645 /* Register the socket-side information for inet6_create. */
646 for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
649 /* We MUST register RAW sockets before we create the ICMP6,
650 * IGMP6, or NDISC control sockets.
652 inet6_register_protosw(&rawv6_protosw);
655 * ipngwg API draft makes clear that the correct semantics
656 * for TCP and UDP is to consider one TCP and UDP instance
657 * in a host available by both INET and INET6 APIs and
658 * able to communicate via both network protocols.
661 #if defined(MODULE) && defined(CONFIG_SYSCTL)
662 ipv6_sysctl_register();
664 err = icmpv6_init(&inet6_family_ops);
667 err = ndisc_init(&inet6_family_ops);
670 err = igmp6_init(&inet6_family_ops);
673 /* Create /proc/foo6 entries. */
674 #ifdef CONFIG_PROC_FS
676 if (!proc_net_create("raw6", 0, raw6_get_info))
678 if (!proc_net_create("tcp6", 0, tcp6_get_info))
680 if (!proc_net_create("udp6", 0, udp6_get_info))
682 if (!proc_net_create("sockstat6", 0, afinet6_get_info))
683 goto proc_sockstat6_fail;
684 if (!proc_net_create("snmp6", 0, afinet6_get_snmp))
685 goto proc_snmp6_fail;
686 if (!proc_net_create("anycast6", 0, anycast6_get_info))
687 goto proc_anycast6_fail;
689 ipv6_netdev_notif_init();
692 ip6_flowlabel_init();
697 /* Init v6 transport protocols. */
701 /* Now the userspace is allowed to create INET6 sockets. */
702 (void) sock_register(&inet6_family_ops);
706 #ifdef CONFIG_PROC_FS
708 proc_net_remove("anycast6");
710 proc_net_remove("sockstat6");
712 proc_net_remove("udp6");
714 proc_net_remove("tcp6");
716 proc_net_remove("raw6");
725 #if defined(MODULE) && defined(CONFIG_SYSCTL)
726 ipv6_sysctl_unregister();
730 module_init(inet6_init);
734 static void inet6_exit(void)
736 /* First of all disallow new sockets creation. */
737 sock_unregister(PF_INET6);
738 #ifdef CONFIG_PROC_FS
739 proc_net_remove("raw6");
740 proc_net_remove("tcp6");
741 proc_net_remove("udp6");
742 proc_net_remove("sockstat6");
743 proc_net_remove("snmp6");
744 proc_net_remove("anycast6");
746 /* Cleanup code parts. */
748 ipv6_netdev_notif_cleanup();
749 ip6_flowlabel_cleanup();
752 ipv6_packet_cleanup();
757 ipv6_sysctl_unregister();
760 module_exit(inet6_exit);
762 MODULE_LICENSE("GPL");