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.1.1.1 2005/04/11 02:51:13 jack 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 raw6_get_info(char *, char **, off_t, int);
80 extern int tcp6_get_info(char *, char **, off_t, int);
81 extern int udp6_get_info(char *, char **, off_t, int);
82 extern int afinet6_get_info(char *, char **, off_t, int);
83 extern int afinet6_get_snmp(char *, char **, off_t, int);
87 extern void ipv6_sysctl_register(void);
88 extern void ipv6_sysctl_unregister(void);
91 int sysctl_ipv6_bindv6only;
93 #ifdef INET_REFCNT_DEBUG
94 atomic_t inet6_sock_nr;
97 /* The inetsw table contains everything that inet_create needs to
100 struct list_head inetsw6[SOCK_MAX];
102 static void inet6_sock_destruct(struct sock *sk)
104 inet_sock_destruct(sk);
106 #ifdef INET_REFCNT_DEBUG
107 atomic_dec(&inet6_sock_nr);
112 static int inet6_create(struct socket *sock, int protocol)
116 struct inet_protosw *answer;
118 sk = sk_alloc(PF_INET6, GFP_KERNEL, 1);
122 /* Look for the requested type/protocol pair. */
124 br_read_lock_bh(BR_NETPROTO_LOCK);
125 list_for_each(p, &inetsw6[sock->type]) {
126 answer = list_entry(p, struct inet_protosw, list);
128 /* Check the non-wild match. */
129 if (protocol == answer->protocol) {
130 if (protocol != IPPROTO_IP)
133 /* Check for the two wild cases. */
134 if (IPPROTO_IP == protocol) {
135 protocol = answer->protocol;
138 if (IPPROTO_IP == answer->protocol)
143 br_read_unlock_bh(BR_NETPROTO_LOCK);
146 goto free_and_badtype;
147 if (answer->capability > 0 && !capable(answer->capability))
148 goto free_and_badperm;
150 goto free_and_noproto;
152 sock->ops = answer->ops;
153 sock_init_data(sock, sk);
155 sk->prot = answer->prot;
156 sk->no_check = answer->no_check;
157 if (INET_PROTOSW_REUSE & answer->flags)
160 if (SOCK_RAW == sock->type) {
162 if (IPPROTO_RAW == protocol)
163 sk->protinfo.af_inet.hdrincl = 1;
166 sk->destruct = inet6_sock_destruct;
168 sk->family = PF_INET6;
169 sk->protocol = protocol;
171 sk->backlog_rcv = answer->prot->backlog_rcv;
173 sk->net_pinfo.af_inet6.hop_limit = -1;
174 sk->net_pinfo.af_inet6.mcast_hops = -1;
175 sk->net_pinfo.af_inet6.mc_loop = 1;
176 sk->net_pinfo.af_inet6.pmtudisc = IPV6_PMTUDISC_WANT;
178 sk->net_pinfo.af_inet6.ipv6only = sysctl_ipv6_bindv6only;
180 /* Init the ipv4 part of the socket since we can have sockets
181 * using v6 API for ipv4.
183 sk->protinfo.af_inet.ttl = 64;
185 sk->protinfo.af_inet.mc_loop = 1;
186 sk->protinfo.af_inet.mc_ttl = 1;
187 sk->protinfo.af_inet.mc_index = 0;
188 sk->protinfo.af_inet.mc_list = NULL;
190 if (ipv4_config.no_pmtu_disc)
191 sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_DONT;
193 sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_WANT;
196 #ifdef INET_REFCNT_DEBUG
197 atomic_inc(&inet6_sock_nr);
198 atomic_inc(&inet_sock_nr);
203 /* It assumes that any protocol which allows
204 * the user to assign a number at socket
205 * creation time automatically shares.
207 sk->sport = ntohs(sk->num);
210 if (sk->prot->init) {
211 int err = sk->prot->init(sk);
214 inet_sock_release(sk);
222 return -ESOCKTNOSUPPORT;
228 return -EPROTONOSUPPORT;
234 /* bind for INET6 API */
235 static int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
237 struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
238 struct sock *sk = sock->sk;
243 /* If the socket has its own bind function then use it. */
245 return sk->prot->bind(sk, uaddr, addr_len);
247 if (addr_len < SIN6_LEN_RFC2133)
249 addr_type = ipv6_addr_type(&addr->sin6_addr);
250 if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
253 /* Check if the address belongs to the host. */
254 if (addr_type == IPV6_ADDR_MAPPED) {
255 v4addr = addr->sin6_addr.s6_addr32[3];
256 if (inet_addr_type(v4addr) != RTN_LOCAL)
257 return -EADDRNOTAVAIL;
259 if (addr_type != IPV6_ADDR_ANY) {
260 /* ipv4 addr of the socket is invalid. Only the
261 * unpecified and mapped address have a v4 equivalent.
263 v4addr = LOOPBACK4_IPV6;
264 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
265 if (!ipv6_chk_addr(&addr->sin6_addr, NULL))
266 return -EADDRNOTAVAIL;
271 snum = ntohs(addr->sin6_port);
272 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
277 /* Check these errors (active socket, double bind). */
278 if ((sk->state != TCP_CLOSE) ||
284 if (addr_type & IPV6_ADDR_LINKLOCAL) {
285 if (addr_len >= sizeof(struct sockaddr_in6) &&
286 addr->sin6_scope_id) {
287 /* Override any existing binding, if another one
288 * is supplied by user.
290 sk->bound_dev_if = addr->sin6_scope_id;
293 /* Binding to link-local address requires an interface */
294 if (sk->bound_dev_if == 0) {
300 sk->rcv_saddr = v4addr;
303 ipv6_addr_copy(&sk->net_pinfo.af_inet6.rcv_saddr, &addr->sin6_addr);
305 if (!(addr_type & IPV6_ADDR_MULTICAST))
306 ipv6_addr_copy(&sk->net_pinfo.af_inet6.saddr, &addr->sin6_addr);
308 /* Make sure we are allowed to bind here. */
309 if (sk->prot->get_port(sk, snum) != 0) {
312 memset(&sk->net_pinfo.af_inet6.rcv_saddr, 0, sizeof(struct in6_addr));
313 memset(&sk->net_pinfo.af_inet6.saddr, 0, sizeof(struct in6_addr));
319 if (addr_type != IPV6_ADDR_ANY)
320 sk->userlocks |= SOCK_BINDADDR_LOCK;
322 sk->userlocks |= SOCK_BINDPORT_LOCK;
323 sk->sport = ntohs(sk->num);
331 static int inet6_release(struct socket *sock)
333 struct sock *sk = sock->sk;
339 ipv6_sock_mc_close(sk);
341 return inet_release(sock);
344 int inet6_destroy_sock(struct sock *sk)
347 struct ipv6_txoptions *opt;
350 * Release destination entry
355 /* Release rx options */
357 if ((skb = xchg(&sk->net_pinfo.af_inet6.pktoptions, NULL)) != NULL)
360 /* Free flowlabels */
361 fl6_free_socklist(sk);
363 /* Free tx options */
365 if ((opt = xchg(&sk->net_pinfo.af_inet6.opt, NULL)) != NULL)
366 sock_kfree_s(sk, opt, opt->tot_len);
372 * This does both peername and sockname.
375 static int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
376 int *uaddr_len, int peer)
378 struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
379 struct sock *sk = sock->sk;
381 sin->sin6_family = AF_INET6;
382 sin->sin6_flowinfo = 0;
383 sin->sin6_scope_id = 0;
387 if (((1<<sk->state)&(TCPF_CLOSE|TCPF_SYN_SENT)) && peer == 1)
389 sin->sin6_port = sk->dport;
390 memcpy(&sin->sin6_addr, &sk->net_pinfo.af_inet6.daddr,
391 sizeof(struct in6_addr));
392 if (sk->net_pinfo.af_inet6.sndflow)
393 sin->sin6_flowinfo = sk->net_pinfo.af_inet6.flow_label;
395 if (ipv6_addr_type(&sk->net_pinfo.af_inet6.rcv_saddr) == IPV6_ADDR_ANY)
396 memcpy(&sin->sin6_addr,
397 &sk->net_pinfo.af_inet6.saddr,
398 sizeof(struct in6_addr));
400 memcpy(&sin->sin6_addr,
401 &sk->net_pinfo.af_inet6.rcv_saddr,
402 sizeof(struct in6_addr));
404 sin->sin6_port = sk->sport;
406 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
407 sin->sin6_scope_id = sk->bound_dev_if;
408 *uaddr_len = sizeof(*sin);
412 static int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
414 struct sock *sk = sock->sk;
422 if (get_user(pid, (int *) arg))
424 /* see sock_no_fcntl */
425 if (current->pid != pid && current->pgrp != -pid &&
426 !capable(CAP_NET_ADMIN))
432 return put_user(sk->proc,(int *)arg);
434 if(sk->stamp.tv_sec==0)
436 err = copy_to_user((void *)arg, &sk->stamp,
437 sizeof(struct timeval));
445 return(ipv6_route_ioctl(cmd,(void *)arg));
448 return addrconf_add_ifaddr((void *) arg);
450 return addrconf_del_ifaddr((void *) arg);
452 return addrconf_set_dstaddr((void *) arg);
454 if ((cmd >= SIOCDEVPRIVATE) &&
455 (cmd <= (SIOCDEVPRIVATE + 15)))
456 return(dev_ioctl(cmd,(void *) arg));
458 if(sk->prot->ioctl==0 || (err=sk->prot->ioctl(sk, cmd, arg))==-ENOIOCTLCMD)
459 return(dev_ioctl(cmd,(void *) arg));
466 struct proto_ops inet6_stream_ops = {
469 release: inet6_release,
471 connect: inet_stream_connect, /* ok */
472 socketpair: sock_no_socketpair, /* a do nothing */
473 accept: inet_accept, /* ok */
474 getname: inet6_getname,
475 poll: tcp_poll, /* ok */
476 ioctl: inet6_ioctl, /* must change */
477 listen: inet_listen, /* ok */
478 shutdown: inet_shutdown, /* ok */
479 setsockopt: inet_setsockopt, /* ok */
480 getsockopt: inet_getsockopt, /* ok */
481 sendmsg: inet_sendmsg, /* ok */
482 recvmsg: inet_recvmsg, /* ok */
484 sendpage: tcp_sendpage
487 struct proto_ops inet6_dgram_ops = {
490 release: inet6_release,
492 connect: inet_dgram_connect, /* ok */
493 socketpair: sock_no_socketpair, /* a do nothing */
494 accept: sock_no_accept, /* a do nothing */
495 getname: inet6_getname,
496 poll: datagram_poll, /* ok */
497 ioctl: inet6_ioctl, /* must change */
498 listen: sock_no_listen, /* ok */
499 shutdown: inet_shutdown, /* ok */
500 setsockopt: inet_setsockopt, /* ok */
501 getsockopt: inet_getsockopt, /* ok */
502 sendmsg: inet_sendmsg, /* ok */
503 recvmsg: inet_recvmsg, /* ok */
505 sendpage: sock_no_sendpage,
508 struct net_proto_family inet6_family_ops = {
514 int ipv6_unload(void)
516 if (!unloadable) return 1;
517 /* We keep internally 3 raw sockets */
518 return atomic_read(&(__this_module.uc.usecount)) - 3;
522 #if defined(MODULE) && defined(CONFIG_SYSCTL)
523 extern void ipv6_sysctl_register(void);
524 extern void ipv6_sysctl_unregister(void);
527 static struct inet_protosw rawv6_protosw = {
529 protocol: IPPROTO_IP, /* wild card */
531 ops: &inet6_dgram_ops,
532 capability: CAP_NET_RAW,
533 no_check: UDP_CSUM_DEFAULT,
534 flags: INET_PROTOSW_REUSE,
537 #define INETSW6_ARRAY_LEN (sizeof(inetsw6_array) / sizeof(struct inet_protosw))
540 inet6_register_protosw(struct inet_protosw *p)
542 struct list_head *lh;
543 struct inet_protosw *answer;
544 int protocol = p->protocol;
546 br_write_lock_bh(BR_NETPROTO_LOCK);
548 if (p->type > SOCK_MAX)
551 /* If we are trying to override a permanent protocol, bail. */
553 list_for_each(lh, &inetsw6[p->type]) {
554 answer = list_entry(lh, struct inet_protosw, list);
556 /* Check only the non-wild match. */
557 if (protocol == answer->protocol &&
558 (INET_PROTOSW_PERMANENT & answer->flags))
566 /* Add to the BEGINNING so that we override any existing
567 * entry. This means that when we remove this entry, the
568 * system automatically returns to the old behavior.
570 list_add(&p->list, &inetsw6[p->type]);
572 br_write_unlock_bh(BR_NETPROTO_LOCK);
576 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
582 "Ignoring attempt to register illegal socket type %d.\n",
588 inet6_unregister_protosw(struct inet_protosw *p)
590 inet_unregister_protosw(p);
593 static int __init inet6_init(void)
595 struct sk_buff *dummy_skb;
600 if (!mod_member_present(&__this_module, can_unload))
603 __this_module.can_unload = &ipv6_unload;
606 printk(KERN_INFO "IPv6 v0.8 for NET4.0\n");
608 if (sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb))
610 printk(KERN_CRIT "inet6_proto_init: size fault\n");
614 /* Register the socket-side information for inet6_create. */
615 for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
618 /* We MUST register RAW sockets before we create the ICMP6,
619 * IGMP6, or NDISC control sockets.
621 inet6_register_protosw(&rawv6_protosw);
624 * ipngwg API draft makes clear that the correct semantics
625 * for TCP and UDP is to consider one TCP and UDP instance
626 * in a host availiable by both INET and INET6 APIs and
627 * able to communicate via both network protocols.
630 #if defined(MODULE) && defined(CONFIG_SYSCTL)
631 ipv6_sysctl_register();
633 err = icmpv6_init(&inet6_family_ops);
636 err = ndisc_init(&inet6_family_ops);
639 err = igmp6_init(&inet6_family_ops);
642 /* Create /proc/foo6 entries. */
643 #ifdef CONFIG_PROC_FS
645 if (!proc_net_create("raw6", 0, raw6_get_info))
647 if (!proc_net_create("tcp6", 0, tcp6_get_info))
649 if (!proc_net_create("udp6", 0, udp6_get_info))
651 if (!proc_net_create("sockstat6", 0, afinet6_get_info))
652 goto proc_sockstat6_fail;
653 if (!proc_net_create("snmp6", 0, afinet6_get_snmp))
654 goto proc_snmp6_fail;
656 ipv6_netdev_notif_init();
659 ip6_flowlabel_init();
663 /* Init v6 transport protocols. */
667 /* Now the userspace is allowed to create INET6 sockets. */
668 (void) sock_register(&inet6_family_ops);
672 #ifdef CONFIG_PROC_FS
674 proc_net_remove("sockstat6");
676 proc_net_remove("udp6");
678 proc_net_remove("tcp6");
680 proc_net_remove("raw6");
689 #if defined(MODULE) && defined(CONFIG_SYSCTL)
690 ipv6_sysctl_unregister();
694 module_init(inet6_init);
698 static void inet6_exit(void)
700 /* First of all disallow new sockets creation. */
701 sock_unregister(PF_INET6);
702 #ifdef CONFIG_PROC_FS
703 proc_net_remove("raw6");
704 proc_net_remove("tcp6");
705 proc_net_remove("udp6");
706 proc_net_remove("sockstat6");
707 proc_net_remove("snmp6");
709 /* Cleanup code parts. */
711 ipv6_netdev_notif_cleanup();
712 ip6_flowlabel_cleanup();
715 ipv6_packet_cleanup();
720 ipv6_sysctl_unregister();
723 module_exit(inet6_exit);
725 MODULE_LICENSE("GPL");