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/sched.h>
32 #include <linux/timer.h>
33 #include <linux/string.h>
34 #include <linux/sockios.h>
35 #include <linux/net.h>
36 #include <linux/fcntl.h>
38 #include <linux/interrupt.h>
39 #include <linux/proc_fs.h>
40 #include <linux/stat.h>
41 #include <linux/init.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
45 #include <linux/icmpv6.h>
46 #include <linux/smp_lock.h>
53 #include <net/protocol.h>
54 #include <net/inet_common.h>
55 #include <net/transp_v6.h>
56 #include <net/ip6_route.h>
57 #include <net/addrconf.h>
58 #ifdef CONFIG_IPV6_TUNNEL
59 #include <net/ip6_tunnel.h>
62 #include <asm/uaccess.h>
63 #include <asm/system.h>
65 MODULE_AUTHOR("Cast of dozens");
66 MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
67 MODULE_LICENSE("GPL");
69 /* IPv6 procfs goodies... */
72 extern int raw6_proc_init(void);
73 extern void raw6_proc_exit(void);
74 extern int tcp6_proc_init(void);
75 extern void tcp6_proc_exit(void);
76 extern int udp6_proc_init(void);
77 extern void udp6_proc_exit(void);
78 extern int ipv6_misc_proc_init(void);
79 extern void ipv6_misc_proc_exit(void);
80 extern int ac6_proc_init(void);
81 extern void ac6_proc_exit(void);
82 extern int if6_proc_init(void);
83 extern void if6_proc_exit(void);
86 int sysctl_ipv6_bindv6only
;
88 #ifdef INET_REFCNT_DEBUG
89 atomic_t inet6_sock_nr
;
90 EXPORT_SYMBOL(inet6_sock_nr
);
93 /* The inetsw table contains everything that inet_create needs to
96 static struct list_head inetsw6
[SOCK_MAX
];
97 static DEFINE_SPINLOCK(inetsw6_lock
);
99 static void inet6_sock_destruct(struct sock
*sk
)
101 inet_sock_destruct(sk
);
103 #ifdef INET_REFCNT_DEBUG
104 atomic_dec(&inet6_sock_nr
);
108 static __inline__
struct ipv6_pinfo
*inet6_sk_generic(struct sock
*sk
)
110 const int offset
= sk
->sk_prot
->obj_size
- sizeof(struct ipv6_pinfo
);
112 return (struct ipv6_pinfo
*)(((u8
*)sk
) + offset
);
115 static int inet6_create(struct socket
*sock
, int protocol
)
117 struct inet_sock
*inet
;
118 struct ipv6_pinfo
*np
;
121 struct inet_protosw
*answer
;
122 struct proto
*answer_prot
;
123 unsigned char answer_flags
;
124 char answer_no_check
;
127 /* Look for the requested type/protocol pair. */
130 list_for_each_rcu(p
, &inetsw6
[sock
->type
]) {
131 answer
= list_entry(p
, struct inet_protosw
, list
);
133 /* Check the non-wild match. */
134 if (protocol
== answer
->protocol
) {
135 if (protocol
!= IPPROTO_IP
)
138 /* Check for the two wild cases. */
139 if (IPPROTO_IP
== protocol
) {
140 protocol
= answer
->protocol
;
143 if (IPPROTO_IP
== answer
->protocol
)
149 rc
= -ESOCKTNOSUPPORT
;
153 if (answer
->capability
> 0 && !capable(answer
->capability
))
155 rc
= -EPROTONOSUPPORT
;
159 sock
->ops
= answer
->ops
;
161 answer_prot
= answer
->prot
;
162 answer_no_check
= answer
->no_check
;
163 answer_flags
= answer
->flags
;
166 BUG_TRAP(answer_prot
->slab
!= NULL
);
169 sk
= sk_alloc(PF_INET6
, GFP_KERNEL
, answer_prot
, 1);
173 sock_init_data(sock
, sk
);
176 sk
->sk_no_check
= answer_no_check
;
177 if (INET_PROTOSW_REUSE
& answer_flags
)
182 if (SOCK_RAW
== sock
->type
) {
183 inet
->num
= protocol
;
184 if (IPPROTO_RAW
== protocol
)
188 sk
->sk_destruct
= inet6_sock_destruct
;
189 sk
->sk_family
= PF_INET6
;
190 sk
->sk_protocol
= protocol
;
192 sk
->sk_backlog_rcv
= answer
->prot
->backlog_rcv
;
194 inet_sk(sk
)->pinet6
= np
= inet6_sk_generic(sk
);
198 np
->pmtudisc
= IPV6_PMTUDISC_WANT
;
199 np
->ipv6only
= sysctl_ipv6_bindv6only
;
201 /* Init the ipv4 part of the socket since we can have sockets
202 * using v6 API for ipv4.
209 inet
->mc_list
= NULL
;
211 if (ipv4_config
.no_pmtu_disc
)
212 inet
->pmtudisc
= IP_PMTUDISC_DONT
;
214 inet
->pmtudisc
= IP_PMTUDISC_WANT
;
217 #ifdef INET_REFCNT_DEBUG
218 atomic_inc(&inet6_sock_nr
);
219 atomic_inc(&inet_sock_nr
);
222 /* It assumes that any protocol which allows
223 * the user to assign a number at socket
224 * creation time automatically shares.
226 inet
->sport
= ntohs(inet
->num
);
227 sk
->sk_prot
->hash(sk
);
229 if (sk
->sk_prot
->init
) {
230 rc
= sk
->sk_prot
->init(sk
);
232 sk_common_release(sk
);
244 /* bind for INET6 API */
245 int inet6_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
247 struct sockaddr_in6
*addr
=(struct sockaddr_in6
*)uaddr
;
248 struct sock
*sk
= sock
->sk
;
249 struct inet_sock
*inet
= inet_sk(sk
);
250 struct ipv6_pinfo
*np
= inet6_sk(sk
);
256 /* If the socket has its own bind function then use it. */
257 if (sk
->sk_prot
->bind
)
258 return sk
->sk_prot
->bind(sk
, uaddr
, addr_len
);
260 if (addr_len
< SIN6_LEN_RFC2133
)
262 addr_type
= ipv6_addr_type(&addr
->sin6_addr
);
263 if ((addr_type
& IPV6_ADDR_MULTICAST
) && sock
->type
== SOCK_STREAM
)
266 snum
= ntohs(addr
->sin6_port
);
267 if (snum
&& snum
< PROT_SOCK
&& !capable(CAP_NET_BIND_SERVICE
))
272 /* Check these errors (active socket, double bind). */
273 if (sk
->sk_state
!= TCP_CLOSE
|| inet
->num
) {
278 /* Check if the address belongs to the host. */
279 if (addr_type
== IPV6_ADDR_MAPPED
) {
280 v4addr
= addr
->sin6_addr
.s6_addr32
[3];
281 if (inet_addr_type(v4addr
) != RTN_LOCAL
) {
282 err
= -EADDRNOTAVAIL
;
286 if (addr_type
!= IPV6_ADDR_ANY
) {
287 struct net_device
*dev
= NULL
;
289 if (addr_type
& IPV6_ADDR_LINKLOCAL
) {
290 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
291 addr
->sin6_scope_id
) {
292 /* Override any existing binding, if another one
293 * is supplied by user.
295 sk
->sk_bound_dev_if
= addr
->sin6_scope_id
;
298 /* Binding to link-local address requires an interface */
299 if (!sk
->sk_bound_dev_if
) {
303 dev
= dev_get_by_index(sk
->sk_bound_dev_if
);
310 /* ipv4 addr of the socket is invalid. Only the
311 * unspecified and mapped address have a v4 equivalent.
313 v4addr
= LOOPBACK4_IPV6
;
314 if (!(addr_type
& IPV6_ADDR_MULTICAST
)) {
315 if (!ipv6_chk_addr(&addr
->sin6_addr
, dev
, 0)) {
318 err
= -EADDRNOTAVAIL
;
327 inet
->rcv_saddr
= v4addr
;
328 inet
->saddr
= v4addr
;
330 ipv6_addr_copy(&np
->rcv_saddr
, &addr
->sin6_addr
);
332 if (!(addr_type
& IPV6_ADDR_MULTICAST
))
333 ipv6_addr_copy(&np
->saddr
, &addr
->sin6_addr
);
335 /* Make sure we are allowed to bind here. */
336 if (sk
->sk_prot
->get_port(sk
, snum
)) {
337 inet_reset_saddr(sk
);
342 if (addr_type
!= IPV6_ADDR_ANY
)
343 sk
->sk_userlocks
|= SOCK_BINDADDR_LOCK
;
345 sk
->sk_userlocks
|= SOCK_BINDPORT_LOCK
;
346 inet
->sport
= ntohs(inet
->num
);
354 int inet6_release(struct socket
*sock
)
356 struct sock
*sk
= sock
->sk
;
362 ipv6_sock_mc_close(sk
);
365 ipv6_sock_ac_close(sk
);
367 return inet_release(sock
);
370 int inet6_destroy_sock(struct sock
*sk
)
372 struct ipv6_pinfo
*np
= inet6_sk(sk
);
374 struct ipv6_txoptions
*opt
;
377 * Release destination entry
382 /* Release rx options */
384 if ((skb
= xchg(&np
->pktoptions
, NULL
)) != NULL
)
387 /* Free flowlabels */
388 fl6_free_socklist(sk
);
390 /* Free tx options */
392 if ((opt
= xchg(&np
->opt
, NULL
)) != NULL
)
393 sock_kfree_s(sk
, opt
, opt
->tot_len
);
399 * This does both peername and sockname.
402 int inet6_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
403 int *uaddr_len
, int peer
)
405 struct sockaddr_in6
*sin
=(struct sockaddr_in6
*)uaddr
;
406 struct sock
*sk
= sock
->sk
;
407 struct inet_sock
*inet
= inet_sk(sk
);
408 struct ipv6_pinfo
*np
= inet6_sk(sk
);
410 sin
->sin6_family
= AF_INET6
;
411 sin
->sin6_flowinfo
= 0;
412 sin
->sin6_scope_id
= 0;
416 if (((1 << sk
->sk_state
) & (TCPF_CLOSE
| TCPF_SYN_SENT
)) &&
419 sin
->sin6_port
= inet
->dport
;
420 ipv6_addr_copy(&sin
->sin6_addr
, &np
->daddr
);
422 sin
->sin6_flowinfo
= np
->flow_label
;
424 if (ipv6_addr_any(&np
->rcv_saddr
))
425 ipv6_addr_copy(&sin
->sin6_addr
, &np
->saddr
);
427 ipv6_addr_copy(&sin
->sin6_addr
, &np
->rcv_saddr
);
429 sin
->sin6_port
= inet
->sport
;
431 if (ipv6_addr_type(&sin
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
432 sin
->sin6_scope_id
= sk
->sk_bound_dev_if
;
433 *uaddr_len
= sizeof(*sin
);
437 int inet6_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
439 struct sock
*sk
= sock
->sk
;
445 return sock_get_timestamp(sk
, (struct timeval __user
*)arg
);
450 return(ipv6_route_ioctl(cmd
,(void __user
*)arg
));
453 return addrconf_add_ifaddr((void __user
*) arg
);
455 return addrconf_del_ifaddr((void __user
*) arg
);
457 return addrconf_set_dstaddr((void __user
*) arg
);
459 if (!sk
->sk_prot
->ioctl
||
460 (err
= sk
->sk_prot
->ioctl(sk
, cmd
, arg
)) == -ENOIOCTLCMD
)
461 return(dev_ioctl(cmd
,(void __user
*) arg
));
468 struct proto_ops inet6_stream_ops
= {
470 .owner
= THIS_MODULE
,
471 .release
= inet6_release
,
473 .connect
= inet_stream_connect
, /* ok */
474 .socketpair
= sock_no_socketpair
, /* a do nothing */
475 .accept
= inet_accept
, /* ok */
476 .getname
= inet6_getname
,
477 .poll
= tcp_poll
, /* ok */
478 .ioctl
= inet6_ioctl
, /* must change */
479 .listen
= inet_listen
, /* ok */
480 .shutdown
= inet_shutdown
, /* ok */
481 .setsockopt
= sock_common_setsockopt
, /* ok */
482 .getsockopt
= sock_common_getsockopt
, /* ok */
483 .sendmsg
= inet_sendmsg
, /* ok */
484 .recvmsg
= sock_common_recvmsg
, /* ok */
485 .mmap
= sock_no_mmap
,
486 .sendpage
= tcp_sendpage
489 struct proto_ops inet6_dgram_ops
= {
491 .owner
= THIS_MODULE
,
492 .release
= inet6_release
,
494 .connect
= inet_dgram_connect
, /* ok */
495 .socketpair
= sock_no_socketpair
, /* a do nothing */
496 .accept
= sock_no_accept
, /* a do nothing */
497 .getname
= inet6_getname
,
498 .poll
= udp_poll
, /* ok */
499 .ioctl
= inet6_ioctl
, /* must change */
500 .listen
= sock_no_listen
, /* ok */
501 .shutdown
= inet_shutdown
, /* ok */
502 .setsockopt
= sock_common_setsockopt
, /* ok */
503 .getsockopt
= sock_common_getsockopt
, /* ok */
504 .sendmsg
= inet_sendmsg
, /* ok */
505 .recvmsg
= sock_common_recvmsg
, /* ok */
506 .mmap
= sock_no_mmap
,
507 .sendpage
= sock_no_sendpage
,
510 static struct net_proto_family inet6_family_ops
= {
512 .create
= inet6_create
,
513 .owner
= THIS_MODULE
,
517 extern void ipv6_sysctl_register(void);
518 extern void ipv6_sysctl_unregister(void);
521 /* Same as inet6_dgram_ops, sans udp_poll. */
522 static struct proto_ops inet6_sockraw_ops
= {
524 .owner
= THIS_MODULE
,
525 .release
= inet6_release
,
527 .connect
= inet_dgram_connect
, /* ok */
528 .socketpair
= sock_no_socketpair
, /* a do nothing */
529 .accept
= sock_no_accept
, /* a do nothing */
530 .getname
= inet6_getname
,
531 .poll
= datagram_poll
, /* ok */
532 .ioctl
= inet6_ioctl
, /* must change */
533 .listen
= sock_no_listen
, /* ok */
534 .shutdown
= inet_shutdown
, /* ok */
535 .setsockopt
= sock_common_setsockopt
, /* ok */
536 .getsockopt
= sock_common_getsockopt
, /* ok */
537 .sendmsg
= inet_sendmsg
, /* ok */
538 .recvmsg
= sock_common_recvmsg
, /* ok */
539 .mmap
= sock_no_mmap
,
540 .sendpage
= sock_no_sendpage
,
543 static struct inet_protosw rawv6_protosw
= {
545 .protocol
= IPPROTO_IP
, /* wild card */
547 .ops
= &inet6_sockraw_ops
,
548 .capability
= CAP_NET_RAW
,
549 .no_check
= UDP_CSUM_DEFAULT
,
550 .flags
= INET_PROTOSW_REUSE
,
554 inet6_register_protosw(struct inet_protosw
*p
)
556 struct list_head
*lh
;
557 struct inet_protosw
*answer
;
558 int protocol
= p
->protocol
;
559 struct list_head
*last_perm
;
561 spin_lock_bh(&inetsw6_lock
);
563 if (p
->type
>= SOCK_MAX
)
566 /* If we are trying to override a permanent protocol, bail. */
568 last_perm
= &inetsw6
[p
->type
];
569 list_for_each(lh
, &inetsw6
[p
->type
]) {
570 answer
= list_entry(lh
, struct inet_protosw
, list
);
572 /* Check only the non-wild match. */
573 if (INET_PROTOSW_PERMANENT
& answer
->flags
) {
574 if (protocol
== answer
->protocol
)
584 /* Add the new entry after the last permanent entry if any, so that
585 * the new entry does not override a permanent entry when matched with
586 * a wild-card protocol. But it is allowed to override any existing
587 * non-permanent entry. This means that when we remove this entry, the
588 * system automatically returns to the old behavior.
590 list_add_rcu(&p
->list
, last_perm
);
592 spin_unlock_bh(&inetsw6_lock
);
596 printk(KERN_ERR
"Attempt to override permanent protocol %d.\n",
602 "Ignoring attempt to register invalid socket type %d.\n",
608 inet6_unregister_protosw(struct inet_protosw
*p
)
610 if (INET_PROTOSW_PERMANENT
& p
->flags
) {
612 "Attempt to unregister permanent protocol %d.\n",
615 spin_lock_bh(&inetsw6_lock
);
616 list_del_rcu(&p
->list
);
617 spin_unlock_bh(&inetsw6_lock
);
624 snmp6_mib_init(void *ptr
[2], size_t mibsize
, size_t mibalign
)
629 ptr
[0] = __alloc_percpu(mibsize
, mibalign
);
633 ptr
[1] = __alloc_percpu(mibsize
, mibalign
);
647 snmp6_mib_free(void *ptr
[2])
655 ptr
[0] = ptr
[1] = NULL
;
658 static int __init
init_ipv6_mibs(void)
660 if (snmp6_mib_init((void **)ipv6_statistics
, sizeof (struct ipstats_mib
),
661 __alignof__(struct ipstats_mib
)) < 0)
663 if (snmp6_mib_init((void **)icmpv6_statistics
, sizeof (struct icmpv6_mib
),
664 __alignof__(struct icmpv6_mib
)) < 0)
666 if (snmp6_mib_init((void **)udp_stats_in6
, sizeof (struct udp_mib
),
667 __alignof__(struct udp_mib
)) < 0)
672 snmp6_mib_free((void **)icmpv6_statistics
);
674 snmp6_mib_free((void **)ipv6_statistics
);
680 static void cleanup_ipv6_mibs(void)
682 snmp6_mib_free((void **)ipv6_statistics
);
683 snmp6_mib_free((void **)icmpv6_statistics
);
684 snmp6_mib_free((void **)udp_stats_in6
);
687 extern int ipv6_misc_proc_init(void);
689 static int __init
inet6_init(void)
691 struct sk_buff
*dummy_skb
;
696 #if 0 /* FIXME --RR */
697 if (!mod_member_present(&__this_module
, can_unload
))
700 __this_module
.can_unload
= &ipv6_unload
;
704 if (sizeof(struct inet6_skb_parm
) > sizeof(dummy_skb
->cb
)) {
705 printk(KERN_CRIT
"inet6_proto_init: size fault\n");
709 err
= proto_register(&tcpv6_prot
, 1);
713 err
= proto_register(&udpv6_prot
, 1);
715 goto out_unregister_tcp_proto
;
717 err
= proto_register(&rawv6_prot
, 1);
719 goto out_unregister_udp_proto
;
722 /* Register the socket-side information for inet6_create. */
723 for(r
= &inetsw6
[0]; r
< &inetsw6
[SOCK_MAX
]; ++r
)
726 /* We MUST register RAW sockets before we create the ICMP6,
727 * IGMP6, or NDISC control sockets.
729 inet6_register_protosw(&rawv6_protosw
);
731 /* Register the family here so that the init calls below will
732 * be able to create sockets. (?? is this dangerous ??)
734 (void) sock_register(&inet6_family_ops
);
736 /* Initialise ipv6 mibs */
737 err
= init_ipv6_mibs();
739 goto out_unregister_raw_proto
;
742 * ipngwg API draft makes clear that the correct semantics
743 * for TCP and UDP is to consider one TCP and UDP instance
744 * in a host availiable by both INET and INET6 APIs and
745 * able to communicate via both network protocols.
749 ipv6_sysctl_register();
751 err
= icmpv6_init(&inet6_family_ops
);
754 err
= ndisc_init(&inet6_family_ops
);
757 err
= igmp6_init(&inet6_family_ops
);
760 /* Create /proc/foo6 entries. */
761 #ifdef CONFIG_PROC_FS
763 if (raw6_proc_init())
765 if (tcp6_proc_init())
767 if (udp6_proc_init())
769 if (ipv6_misc_proc_init())
770 goto proc_misc6_fail
;
773 goto proc_anycast6_fail
;
779 ip6_flowlabel_init();
780 err
= addrconf_init();
785 /* Init v6 extension headers. */
791 /* Init v6 transport protocols. */
799 ip6_flowlabel_cleanup();
801 ipv6_packet_cleanup();
802 #ifdef CONFIG_PROC_FS
807 ipv6_misc_proc_exit();
823 ipv6_sysctl_unregister();
826 out_unregister_raw_proto
:
827 proto_unregister(&rawv6_prot
);
828 out_unregister_udp_proto
:
829 proto_unregister(&udpv6_prot
);
830 out_unregister_tcp_proto
:
831 proto_unregister(&tcpv6_prot
);
834 module_init(inet6_init
);
836 static void __exit
inet6_exit(void)
838 /* First of all disallow new sockets creation. */
839 sock_unregister(PF_INET6
);
840 #ifdef CONFIG_PROC_FS
843 ipv6_misc_proc_exit();
848 /* Cleanup code parts. */
850 ip6_flowlabel_cleanup();
853 ipv6_packet_cleanup();
858 ipv6_sysctl_unregister();
861 proto_unregister(&rawv6_prot
);
862 proto_unregister(&udpv6_prot
);
863 proto_unregister(&tcpv6_prot
);
865 module_exit(inet6_exit
);
867 MODULE_ALIAS_NETPROTO(PF_INET6
);