11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * PF_INET6 socket protocol family 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Adapted from linux/net/ipv4/af_inet.c 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Fixes: 111da177e4SLinus Torvalds * piggy, Karl Knutson : Socket protocol table 121da177e4SLinus Torvalds * Hideaki YOSHIFUJI : sin6_scope_id support 131da177e4SLinus Torvalds * Arnaldo Melo : check proc_net_create return, cleanups 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 161da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 171da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 181da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 21f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <linux/module.h> 244fc268d2SRandy Dunlap #include <linux/capability.h> 251da177e4SLinus Torvalds #include <linux/errno.h> 261da177e4SLinus Torvalds #include <linux/types.h> 271da177e4SLinus Torvalds #include <linux/socket.h> 281da177e4SLinus Torvalds #include <linux/in.h> 291da177e4SLinus Torvalds #include <linux/kernel.h> 301da177e4SLinus Torvalds #include <linux/timer.h> 311da177e4SLinus Torvalds #include <linux/string.h> 321da177e4SLinus Torvalds #include <linux/sockios.h> 331da177e4SLinus Torvalds #include <linux/net.h> 341da177e4SLinus Torvalds #include <linux/fcntl.h> 351da177e4SLinus Torvalds #include <linux/mm.h> 361da177e4SLinus Torvalds #include <linux/interrupt.h> 371da177e4SLinus Torvalds #include <linux/proc_fs.h> 381da177e4SLinus Torvalds #include <linux/stat.h> 391da177e4SLinus Torvalds #include <linux/init.h> 405a0e3ad6STejun Heo #include <linux/slab.h> 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #include <linux/inet.h> 431da177e4SLinus Torvalds #include <linux/netdevice.h> 441da177e4SLinus Torvalds #include <linux/icmpv6.h> 452cc7d573SHarald Welte #include <linux/netfilter_ipv6.h> 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #include <net/ip.h> 481da177e4SLinus Torvalds #include <net/ipv6.h> 491da177e4SLinus Torvalds #include <net/udp.h> 50ba4e58ecSGerrit Renker #include <net/udplite.h> 511da177e4SLinus Torvalds #include <net/tcp.h> 526d0bfe22SLorenzo Colitti #include <net/ping.h> 531da177e4SLinus Torvalds #include <net/protocol.h> 541da177e4SLinus Torvalds #include <net/inet_common.h> 551668e010SKOVACS Krisztian #include <net/route.h> 561da177e4SLinus Torvalds #include <net/transp_v6.h> 571da177e4SLinus Torvalds #include <net/ip6_route.h> 581da177e4SLinus Torvalds #include <net/addrconf.h> 59f564f45cSCong Wang #include <net/ndisc.h> 601da177e4SLinus Torvalds #ifdef CONFIG_IPV6_TUNNEL 611da177e4SLinus Torvalds #include <net/ip6_tunnel.h> 621da177e4SLinus Torvalds #endif 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds #include <asm/uaccess.h> 657bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds MODULE_AUTHOR("Cast of dozens"); 681da177e4SLinus Torvalds MODULE_DESCRIPTION("IPv6 protocol stack for Linux"); 691da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 701da177e4SLinus Torvalds 71f1267347SPavel Emelyanov /* The inetsw6 table contains everything that inet6_create needs to 721da177e4SLinus Torvalds * build a new socket. 731da177e4SLinus Torvalds */ 741da177e4SLinus Torvalds static struct list_head inetsw6[SOCK_MAX]; 751da177e4SLinus Torvalds static DEFINE_SPINLOCK(inetsw6_lock); 761da177e4SLinus Torvalds 7756d417b1SBrian Haley struct ipv6_params ipv6_defaults = { 7856d417b1SBrian Haley .disable_ipv6 = 0, 7956d417b1SBrian Haley .autoconf = 1, 8056d417b1SBrian Haley }; 8156d417b1SBrian Haley 82647c0c70SEldad Zack static int disable_ipv6_mod; 8356d417b1SBrian Haley 8456d417b1SBrian Haley module_param_named(disable, disable_ipv6_mod, int, 0444); 8556d417b1SBrian Haley MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional"); 8656d417b1SBrian Haley 8756d417b1SBrian Haley module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444); 8856d417b1SBrian Haley MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces"); 8956d417b1SBrian Haley 9056d417b1SBrian Haley module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444); 9156d417b1SBrian Haley MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces"); 92fe7ca2e1SBrian Haley 931da177e4SLinus Torvalds static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk) 941da177e4SLinus Torvalds { 951da177e4SLinus Torvalds const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo); 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds 1003f378b68SEric Paris static int inet6_create(struct net *net, struct socket *sock, int protocol, 1013f378b68SEric Paris int kern) 1021da177e4SLinus Torvalds { 1031da177e4SLinus Torvalds struct inet_sock *inet; 1041da177e4SLinus Torvalds struct ipv6_pinfo *np; 1051da177e4SLinus Torvalds struct sock *sk; 1061da177e4SLinus Torvalds struct inet_protosw *answer; 1071da177e4SLinus Torvalds struct proto *answer_prot; 1081da177e4SLinus Torvalds unsigned char answer_flags; 109af1afe86SYOSHIFUJI Hideaki int try_loading_module = 0; 110af1afe86SYOSHIFUJI Hideaki int err; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /* Look for the requested type/protocol pair. */ 113af1afe86SYOSHIFUJI Hideaki lookup_protocol: 114af1afe86SYOSHIFUJI Hideaki err = -ESOCKTNOSUPPORT; 1151da177e4SLinus Torvalds rcu_read_lock(); 116696adfe8SPaul E. McKenney list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) { 1171da177e4SLinus Torvalds 118696adfe8SPaul E. McKenney err = 0; 1191da177e4SLinus Torvalds /* Check the non-wild match. */ 1201da177e4SLinus Torvalds if (protocol == answer->protocol) { 1211da177e4SLinus Torvalds if (protocol != IPPROTO_IP) 1221da177e4SLinus Torvalds break; 1231da177e4SLinus Torvalds } else { 1241da177e4SLinus Torvalds /* Check for the two wild cases. */ 1251da177e4SLinus Torvalds if (IPPROTO_IP == protocol) { 1261da177e4SLinus Torvalds protocol = answer->protocol; 1271da177e4SLinus Torvalds break; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds if (IPPROTO_IP == answer->protocol) 1301da177e4SLinus Torvalds break; 1311da177e4SLinus Torvalds } 132af1afe86SYOSHIFUJI Hideaki err = -EPROTONOSUPPORT; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 135696adfe8SPaul E. McKenney if (err) { 136af1afe86SYOSHIFUJI Hideaki if (try_loading_module < 2) { 137af1afe86SYOSHIFUJI Hideaki rcu_read_unlock(); 138af1afe86SYOSHIFUJI Hideaki /* 139af1afe86SYOSHIFUJI Hideaki * Be more specific, e.g. net-pf-10-proto-132-type-1 140af1afe86SYOSHIFUJI Hideaki * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM) 141af1afe86SYOSHIFUJI Hideaki */ 142af1afe86SYOSHIFUJI Hideaki if (++try_loading_module == 1) 143af1afe86SYOSHIFUJI Hideaki request_module("net-pf-%d-proto-%d-type-%d", 144af1afe86SYOSHIFUJI Hideaki PF_INET6, protocol, sock->type); 145af1afe86SYOSHIFUJI Hideaki /* 146af1afe86SYOSHIFUJI Hideaki * Fall back to generic, e.g. net-pf-10-proto-132 147af1afe86SYOSHIFUJI Hideaki * (net-pf-PF_INET6-proto-IPPROTO_SCTP) 148af1afe86SYOSHIFUJI Hideaki */ 149af1afe86SYOSHIFUJI Hideaki else 150af1afe86SYOSHIFUJI Hideaki request_module("net-pf-%d-proto-%d", 151af1afe86SYOSHIFUJI Hideaki PF_INET6, protocol); 152af1afe86SYOSHIFUJI Hideaki goto lookup_protocol; 153af1afe86SYOSHIFUJI Hideaki } else 1541da177e4SLinus Torvalds goto out_rcu_unlock; 155af1afe86SYOSHIFUJI Hideaki } 156af1afe86SYOSHIFUJI Hideaki 157af1afe86SYOSHIFUJI Hideaki err = -EPERM; 158af31f412SEric W. Biederman if (sock->type == SOCK_RAW && !kern && 159af31f412SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_RAW)) 1601da177e4SLinus Torvalds goto out_rcu_unlock; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds sock->ops = answer->ops; 1631da177e4SLinus Torvalds answer_prot = answer->prot; 1641da177e4SLinus Torvalds answer_flags = answer->flags; 1651da177e4SLinus Torvalds rcu_read_unlock(); 1661da177e4SLinus Torvalds 16763159f29SIan Morris WARN_ON(!answer_prot->slab); 1681da177e4SLinus Torvalds 169af1afe86SYOSHIFUJI Hideaki err = -ENOBUFS; 17011aa9c28SEric W. Biederman sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot, kern); 17163159f29SIan Morris if (!sk) 1721da177e4SLinus Torvalds goto out; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds sock_init_data(sock, sk); 1751da177e4SLinus Torvalds 176af1afe86SYOSHIFUJI Hideaki err = 0; 1771da177e4SLinus Torvalds if (INET_PROTOSW_REUSE & answer_flags) 1784a17fd52SPavel Emelyanov sk->sk_reuse = SK_CAN_REUSE; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds inet = inet_sk(sk); 181469de9b9SPaul Moore inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds if (SOCK_RAW == sock->type) { 184c720c7e8SEric Dumazet inet->inet_num = protocol; 1851da177e4SLinus Torvalds if (IPPROTO_RAW == protocol) 1861da177e4SLinus Torvalds inet->hdrincl = 1; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 189e6848976SArnaldo Carvalho de Melo sk->sk_destruct = inet_sock_destruct; 1901da177e4SLinus Torvalds sk->sk_family = PF_INET6; 1911da177e4SLinus Torvalds sk->sk_protocol = protocol; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds sk->sk_backlog_rcv = answer->prot->backlog_rcv; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk); 1961da177e4SLinus Torvalds np->hop_limit = -1; 197f935aa9eSDavid S. Miller np->mcast_hops = IPV6_DEFAULT_MCASTHOPS; 1981da177e4SLinus Torvalds np->mc_loop = 1; 1991da177e4SLinus Torvalds np->pmtudisc = IPV6_PMTUDISC_WANT; 2009fe516baSEric Dumazet sk->sk_ipv6only = net->ipv6.sysctl.bindv6only; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds /* Init the ipv4 part of the socket since we can have sockets 2031da177e4SLinus Torvalds * using v6 API for ipv4. 2041da177e4SLinus Torvalds */ 2051da177e4SLinus Torvalds inet->uc_ttl = -1; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds inet->mc_loop = 1; 2081da177e4SLinus Torvalds inet->mc_ttl = 1; 2091da177e4SLinus Torvalds inet->mc_index = 0; 2101da177e4SLinus Torvalds inet->mc_list = NULL; 2114c507d28SJiri Benc inet->rcv_tos = 0; 2121da177e4SLinus Torvalds 213974eda11SHannes Frederic Sowa if (net->ipv4.sysctl_ip_no_pmtu_disc) 2141da177e4SLinus Torvalds inet->pmtudisc = IP_PMTUDISC_DONT; 2151da177e4SLinus Torvalds else 2161da177e4SLinus Torvalds inet->pmtudisc = IP_PMTUDISC_WANT; 217e6848976SArnaldo Carvalho de Melo /* 218e6848976SArnaldo Carvalho de Melo * Increment only the relevant sk_prot->socks debug field, this changes 219e6848976SArnaldo Carvalho de Melo * the previous behaviour of incrementing both the equivalent to 220e6848976SArnaldo Carvalho de Melo * answer->prot->socks (inet6_sock_nr) and inet_sock_nr. 221e6848976SArnaldo Carvalho de Melo * 222e6848976SArnaldo Carvalho de Melo * This allows better debug granularity as we'll know exactly how many 223e6848976SArnaldo Carvalho de Melo * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6 224e6848976SArnaldo Carvalho de Melo * transport protocol socks. -acme 225e6848976SArnaldo Carvalho de Melo */ 226e6848976SArnaldo Carvalho de Melo sk_refcnt_debug_inc(sk); 2271da177e4SLinus Torvalds 228c720c7e8SEric Dumazet if (inet->inet_num) { 2291da177e4SLinus Torvalds /* It assumes that any protocol which allows 2301da177e4SLinus Torvalds * the user to assign a number at socket 2311da177e4SLinus Torvalds * creation time automatically shares. 2321da177e4SLinus Torvalds */ 233c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 2341da177e4SLinus Torvalds sk->sk_prot->hash(sk); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds if (sk->sk_prot->init) { 237af1afe86SYOSHIFUJI Hideaki err = sk->sk_prot->init(sk); 238af1afe86SYOSHIFUJI Hideaki if (err) { 2391da177e4SLinus Torvalds sk_common_release(sk); 2401da177e4SLinus Torvalds goto out; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds out: 244af1afe86SYOSHIFUJI Hideaki return err; 2451da177e4SLinus Torvalds out_rcu_unlock: 2461da177e4SLinus Torvalds rcu_read_unlock(); 2471da177e4SLinus Torvalds goto out; 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds /* bind for INET6 API */ 2521da177e4SLinus Torvalds int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 2531da177e4SLinus Torvalds { 2541da177e4SLinus Torvalds struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr; 2551da177e4SLinus Torvalds struct sock *sk = sock->sk; 2561da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2571da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 2583b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 259fd683222SAl Viro __be32 v4addr = 0; 2601da177e4SLinus Torvalds unsigned short snum; 2611da177e4SLinus Torvalds int addr_type = 0; 2621da177e4SLinus Torvalds int err = 0; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds /* If the socket has its own bind function then use it. */ 2651da177e4SLinus Torvalds if (sk->sk_prot->bind) 2661da177e4SLinus Torvalds return sk->sk_prot->bind(sk, uaddr, addr_len); 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds if (addr_len < SIN6_LEN_RFC2133) 2691da177e4SLinus Torvalds return -EINVAL; 2705a079c30SMarcus Meissner 2715a079c30SMarcus Meissner if (addr->sin6_family != AF_INET6) 272c349a528SMarcus Meissner return -EAFNOSUPPORT; 2735a079c30SMarcus Meissner 2741da177e4SLinus Torvalds addr_type = ipv6_addr_type(&addr->sin6_addr); 2751da177e4SLinus Torvalds if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM) 2761da177e4SLinus Torvalds return -EINVAL; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds snum = ntohs(addr->sin6_port); 2793594698aSEric W. Biederman if (snum && snum < PROT_SOCK && !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) 2801da177e4SLinus Torvalds return -EACCES; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds lock_sock(sk); 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds /* Check these errors (active socket, double bind). */ 285c720c7e8SEric Dumazet if (sk->sk_state != TCP_CLOSE || inet->inet_num) { 2861da177e4SLinus Torvalds err = -EINVAL; 2871da177e4SLinus Torvalds goto out; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds /* Check if the address belongs to the host. */ 2911da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_MAPPED) { 29263d9950bSVlad Yasevich int chk_addr_ret; 29363d9950bSVlad Yasevich 294783ed5a7SVlad Yasevich /* Binding to v4-mapped address on a v6-only socket 295783ed5a7SVlad Yasevich * makes no sense 296783ed5a7SVlad Yasevich */ 2979fe516baSEric Dumazet if (sk->sk_ipv6only) { 298783ed5a7SVlad Yasevich err = -EINVAL; 299783ed5a7SVlad Yasevich goto out; 300783ed5a7SVlad Yasevich } 30163d9950bSVlad Yasevich 302a34f0b31SUwe Kleine-König /* Reproduce AF_INET checks to make the bindings consistent */ 3031da177e4SLinus Torvalds v4addr = addr->sin6_addr.s6_addr32[3]; 30463d9950bSVlad Yasevich chk_addr_ret = inet_addr_type(net, v4addr); 30549a60158SVincent Bernat if (!net->ipv4.sysctl_ip_nonlocal_bind && 30663d9950bSVlad Yasevich !(inet->freebind || inet->transparent) && 30763d9950bSVlad Yasevich v4addr != htonl(INADDR_ANY) && 30863d9950bSVlad Yasevich chk_addr_ret != RTN_LOCAL && 30963d9950bSVlad Yasevich chk_addr_ret != RTN_MULTICAST && 310ca6982b8SBruno Prémont chk_addr_ret != RTN_BROADCAST) { 311ca6982b8SBruno Prémont err = -EADDRNOTAVAIL; 3121da177e4SLinus Torvalds goto out; 313ca6982b8SBruno Prémont } 3141da177e4SLinus Torvalds } else { 3151da177e4SLinus Torvalds if (addr_type != IPV6_ADDR_ANY) { 3161da177e4SLinus Torvalds struct net_device *dev = NULL; 3171da177e4SLinus Torvalds 31816ba5e8eSEric Dumazet rcu_read_lock(); 319842df073SHannes Frederic Sowa if (__ipv6_addr_needs_scope_id(addr_type)) { 3201da177e4SLinus Torvalds if (addr_len >= sizeof(struct sockaddr_in6) && 3211da177e4SLinus Torvalds addr->sin6_scope_id) { 3221da177e4SLinus Torvalds /* Override any existing binding, if another one 3231da177e4SLinus Torvalds * is supplied by user. 3241da177e4SLinus Torvalds */ 3251da177e4SLinus Torvalds sk->sk_bound_dev_if = addr->sin6_scope_id; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds /* Binding to link-local address requires an interface */ 3291da177e4SLinus Torvalds if (!sk->sk_bound_dev_if) { 3301da177e4SLinus Torvalds err = -EINVAL; 33116ba5e8eSEric Dumazet goto out_unlock; 3321da177e4SLinus Torvalds } 33316ba5e8eSEric Dumazet dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if); 3341da177e4SLinus Torvalds if (!dev) { 3351da177e4SLinus Torvalds err = -ENODEV; 33616ba5e8eSEric Dumazet goto out_unlock; 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds /* ipv4 addr of the socket is invalid. Only the 3411da177e4SLinus Torvalds * unspecified and mapped address have a v4 equivalent. 3421da177e4SLinus Torvalds */ 3431da177e4SLinus Torvalds v4addr = LOOPBACK4_IPV6; 3441da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST)) { 34535a256feSTom Herbert if (!net->ipv6.sysctl.ip_nonlocal_bind && 34635a256feSTom Herbert !(inet->freebind || inet->transparent) && 3470a513f6aSBalazs Scheidler !ipv6_chk_addr(net, &addr->sin6_addr, 348bfeade08SDaniel Lezcano dev, 0)) { 3491da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 35016ba5e8eSEric Dumazet goto out_unlock; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds } 35316ba5e8eSEric Dumazet rcu_read_unlock(); 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 357c720c7e8SEric Dumazet inet->inet_rcv_saddr = v4addr; 358c720c7e8SEric Dumazet inet->inet_saddr = v4addr; 3591da177e4SLinus Torvalds 360efe4208fSEric Dumazet sk->sk_v6_rcv_saddr = addr->sin6_addr; 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST)) 3634e3fd7a0SAlexey Dobriyan np->saddr = addr->sin6_addr; 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds /* Make sure we are allowed to bind here. */ 36690c337daSEric Dumazet if ((snum || !inet->bind_address_no_port) && 36790c337daSEric Dumazet sk->sk_prot->get_port(sk, snum)) { 3681da177e4SLinus Torvalds inet_reset_saddr(sk); 3691da177e4SLinus Torvalds err = -EADDRINUSE; 3701da177e4SLinus Torvalds goto out; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3730f8d3c7aSVlad Yasevich if (addr_type != IPV6_ADDR_ANY) { 3741da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDADDR_LOCK; 3750f8d3c7aSVlad Yasevich if (addr_type != IPV6_ADDR_MAPPED) 3769fe516baSEric Dumazet sk->sk_ipv6only = 1; 3770f8d3c7aSVlad Yasevich } 3781da177e4SLinus Torvalds if (snum) 3791da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 380c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 381c720c7e8SEric Dumazet inet->inet_dport = 0; 382c720c7e8SEric Dumazet inet->inet_daddr = 0; 3831da177e4SLinus Torvalds out: 3841da177e4SLinus Torvalds release_sock(sk); 3851da177e4SLinus Torvalds return err; 38616ba5e8eSEric Dumazet out_unlock: 38716ba5e8eSEric Dumazet rcu_read_unlock(); 38816ba5e8eSEric Dumazet goto out; 3891da177e4SLinus Torvalds } 3907159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_bind); 3917159039aSYOSHIFUJI Hideaki 3921da177e4SLinus Torvalds int inet6_release(struct socket *sock) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds struct sock *sk = sock->sk; 3951da177e4SLinus Torvalds 39663159f29SIan Morris if (!sk) 3971da177e4SLinus Torvalds return -EINVAL; 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds /* Free mc lists */ 4001da177e4SLinus Torvalds ipv6_sock_mc_close(sk); 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds /* Free ac lists */ 4031da177e4SLinus Torvalds ipv6_sock_ac_close(sk); 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds return inet_release(sock); 4061da177e4SLinus Torvalds } 4077159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release); 4087159039aSYOSHIFUJI Hideaki 4097d06b2e0SBrian Haley void inet6_destroy_sock(struct sock *sk) 4101da177e4SLinus Torvalds { 4111da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4121da177e4SLinus Torvalds struct sk_buff *skb; 4131da177e4SLinus Torvalds struct ipv6_txoptions *opt; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds /* Release rx options */ 4161da177e4SLinus Torvalds 417647c0c70SEldad Zack skb = xchg(&np->pktoptions, NULL); 41853b24b8fSIan Morris if (skb) 4191da177e4SLinus Torvalds kfree_skb(skb); 4201da177e4SLinus Torvalds 421647c0c70SEldad Zack skb = xchg(&np->rxpmtu, NULL); 42253b24b8fSIan Morris if (skb) 4234b340ae2SBrian Haley kfree_skb(skb); 4244b340ae2SBrian Haley 4251da177e4SLinus Torvalds /* Free flowlabels */ 4261da177e4SLinus Torvalds fl6_free_socklist(sk); 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds /* Free tx options */ 4291da177e4SLinus Torvalds 430647c0c70SEldad Zack opt = xchg(&np->opt, NULL); 43153b24b8fSIan Morris if (opt) 4321da177e4SLinus Torvalds sock_kfree_s(sk, opt, opt->tot_len); 4331da177e4SLinus Torvalds } 4343cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_destroy_sock); 4353cf3dc6cSArnaldo Carvalho de Melo 4361da177e4SLinus Torvalds /* 4371da177e4SLinus Torvalds * This does both peername and sockname. 4381da177e4SLinus Torvalds */ 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr, 4411da177e4SLinus Torvalds int *uaddr_len, int peer) 4421da177e4SLinus Torvalds { 4431da177e4SLinus Torvalds struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr; 4441da177e4SLinus Torvalds struct sock *sk = sock->sk; 4451da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 4461da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds sin->sin6_family = AF_INET6; 4491da177e4SLinus Torvalds sin->sin6_flowinfo = 0; 4501da177e4SLinus Torvalds sin->sin6_scope_id = 0; 4511da177e4SLinus Torvalds if (peer) { 452c720c7e8SEric Dumazet if (!inet->inet_dport) 4531da177e4SLinus Torvalds return -ENOTCONN; 4541da177e4SLinus Torvalds if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && 4551da177e4SLinus Torvalds peer == 1) 4561da177e4SLinus Torvalds return -ENOTCONN; 457c720c7e8SEric Dumazet sin->sin6_port = inet->inet_dport; 458efe4208fSEric Dumazet sin->sin6_addr = sk->sk_v6_daddr; 4591da177e4SLinus Torvalds if (np->sndflow) 4601da177e4SLinus Torvalds sin->sin6_flowinfo = np->flow_label; 4611da177e4SLinus Torvalds } else { 462efe4208fSEric Dumazet if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) 4634e3fd7a0SAlexey Dobriyan sin->sin6_addr = np->saddr; 4641da177e4SLinus Torvalds else 465efe4208fSEric Dumazet sin->sin6_addr = sk->sk_v6_rcv_saddr; 4661da177e4SLinus Torvalds 467c720c7e8SEric Dumazet sin->sin6_port = inet->inet_sport; 4681da177e4SLinus Torvalds } 469842df073SHannes Frederic Sowa sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr, 470842df073SHannes Frederic Sowa sk->sk_bound_dev_if); 4711da177e4SLinus Torvalds *uaddr_len = sizeof(*sin); 472a02cec21SEric Dumazet return 0; 4731da177e4SLinus Torvalds } 4747159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname); 4757159039aSYOSHIFUJI Hideaki 4761da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 4771da177e4SLinus Torvalds { 4781da177e4SLinus Torvalds struct sock *sk = sock->sk; 4793b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 4801da177e4SLinus Torvalds 481647c0c70SEldad Zack switch (cmd) { 4821da177e4SLinus Torvalds case SIOCGSTAMP: 4831da177e4SLinus Torvalds return sock_get_timestamp(sk, (struct timeval __user *)arg); 4841da177e4SLinus Torvalds 485ae40eb1eSEric Dumazet case SIOCGSTAMPNS: 486ae40eb1eSEric Dumazet return sock_get_timestampns(sk, (struct timespec __user *)arg); 487ae40eb1eSEric Dumazet 4881da177e4SLinus Torvalds case SIOCADDRT: 4891da177e4SLinus Torvalds case SIOCDELRT: 4901da177e4SLinus Torvalds 491a02cec21SEric Dumazet return ipv6_route_ioctl(net, cmd, (void __user *)arg); 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds case SIOCSIFADDR: 494af284937SDaniel Lezcano return addrconf_add_ifaddr(net, (void __user *) arg); 4951da177e4SLinus Torvalds case SIOCDIFADDR: 496af284937SDaniel Lezcano return addrconf_del_ifaddr(net, (void __user *) arg); 4971da177e4SLinus Torvalds case SIOCSIFDSTADDR: 498af284937SDaniel Lezcano return addrconf_set_dstaddr(net, (void __user *) arg); 4991da177e4SLinus Torvalds default: 500b5e5fa5eSChristoph Hellwig if (!sk->sk_prot->ioctl) 501b5e5fa5eSChristoph Hellwig return -ENOIOCTLCMD; 502b5e5fa5eSChristoph Hellwig return sk->sk_prot->ioctl(sk, cmd, arg); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds /*NOTREACHED*/ 505a02cec21SEric Dumazet return 0; 5061da177e4SLinus Torvalds } 5077159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl); 5087159039aSYOSHIFUJI Hideaki 50990ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = { 5101da177e4SLinus Torvalds .family = PF_INET6, 5111da177e4SLinus Torvalds .owner = THIS_MODULE, 5121da177e4SLinus Torvalds .release = inet6_release, 5131da177e4SLinus Torvalds .bind = inet6_bind, 5141da177e4SLinus Torvalds .connect = inet_stream_connect, /* ok */ 5151da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */ 5161da177e4SLinus Torvalds .accept = inet_accept, /* ok */ 5171da177e4SLinus Torvalds .getname = inet6_getname, 5181da177e4SLinus Torvalds .poll = tcp_poll, /* ok */ 5191da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */ 5201da177e4SLinus Torvalds .listen = inet_listen, /* ok */ 5211da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */ 5221da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */ 5231da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */ 5247ba42910SChangli Gao .sendmsg = inet_sendmsg, /* ok */ 5257ba42910SChangli Gao .recvmsg = inet_recvmsg, /* ok */ 526543d9cfeSArnaldo Carvalho de Melo .mmap = sock_no_mmap, 5277ba42910SChangli Gao .sendpage = inet_sendpage, 528a0974dd3SJens Axboe .splice_read = tcp_splice_read, 5293fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 5303fdadf7dSDmitry Mishin .compat_setsockopt = compat_sock_common_setsockopt, 5313fdadf7dSDmitry Mishin .compat_getsockopt = compat_sock_common_getsockopt, 5323fdadf7dSDmitry Mishin #endif 5331da177e4SLinus Torvalds }; 5341da177e4SLinus Torvalds 53590ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = { 5361da177e4SLinus Torvalds .family = PF_INET6, 5371da177e4SLinus Torvalds .owner = THIS_MODULE, 5381da177e4SLinus Torvalds .release = inet6_release, 5391da177e4SLinus Torvalds .bind = inet6_bind, 5401da177e4SLinus Torvalds .connect = inet_dgram_connect, /* ok */ 5411da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */ 5421da177e4SLinus Torvalds .accept = sock_no_accept, /* a do nothing */ 5431da177e4SLinus Torvalds .getname = inet6_getname, 5441da177e4SLinus Torvalds .poll = udp_poll, /* ok */ 5451da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */ 5461da177e4SLinus Torvalds .listen = sock_no_listen, /* ok */ 5471da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */ 5481da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */ 5491da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */ 5501da177e4SLinus Torvalds .sendmsg = inet_sendmsg, /* ok */ 5517ba42910SChangli Gao .recvmsg = inet_recvmsg, /* ok */ 5521da177e4SLinus Torvalds .mmap = sock_no_mmap, 5531da177e4SLinus Torvalds .sendpage = sock_no_sendpage, 554543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 555543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_sock_common_setsockopt, 556543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_sock_common_getsockopt, 557543d9cfeSArnaldo Carvalho de Melo #endif 5581da177e4SLinus Torvalds }; 5591da177e4SLinus Torvalds 560ec1b4cf7SStephen Hemminger static const struct net_proto_family inet6_family_ops = { 5611da177e4SLinus Torvalds .family = PF_INET6, 5621da177e4SLinus Torvalds .create = inet6_create, 5631da177e4SLinus Torvalds .owner = THIS_MODULE, 5641da177e4SLinus Torvalds }; 5651da177e4SLinus Torvalds 56687c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p) 5671da177e4SLinus Torvalds { 5681da177e4SLinus Torvalds struct list_head *lh; 5691da177e4SLinus Torvalds struct inet_protosw *answer; 5701da177e4SLinus Torvalds struct list_head *last_perm; 57187c3efbfSDaniel Lezcano int protocol = p->protocol; 57287c3efbfSDaniel Lezcano int ret; 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock); 5751da177e4SLinus Torvalds 57687c3efbfSDaniel Lezcano ret = -EINVAL; 5771da177e4SLinus Torvalds if (p->type >= SOCK_MAX) 5781da177e4SLinus Torvalds goto out_illegal; 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds /* If we are trying to override a permanent protocol, bail. */ 5811da177e4SLinus Torvalds answer = NULL; 58287c3efbfSDaniel Lezcano ret = -EPERM; 5831da177e4SLinus Torvalds last_perm = &inetsw6[p->type]; 5841da177e4SLinus Torvalds list_for_each(lh, &inetsw6[p->type]) { 5851da177e4SLinus Torvalds answer = list_entry(lh, struct inet_protosw, list); 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds /* Check only the non-wild match. */ 5881da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & answer->flags) { 5891da177e4SLinus Torvalds if (protocol == answer->protocol) 5901da177e4SLinus Torvalds break; 5911da177e4SLinus Torvalds last_perm = lh; 5921da177e4SLinus Torvalds } 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds answer = NULL; 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds if (answer) 5971da177e4SLinus Torvalds goto out_permanent; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds /* Add the new entry after the last permanent entry if any, so that 6001da177e4SLinus Torvalds * the new entry does not override a permanent entry when matched with 6011da177e4SLinus Torvalds * a wild-card protocol. But it is allowed to override any existing 6021da177e4SLinus Torvalds * non-permanent entry. This means that when we remove this entry, the 6031da177e4SLinus Torvalds * system automatically returns to the old behavior. 6041da177e4SLinus Torvalds */ 6051da177e4SLinus Torvalds list_add_rcu(&p->list, last_perm); 60687c3efbfSDaniel Lezcano ret = 0; 6071da177e4SLinus Torvalds out: 6081da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock); 60987c3efbfSDaniel Lezcano return ret; 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds out_permanent: 612f3213831SJoe Perches pr_err("Attempt to override permanent protocol %d\n", protocol); 6131da177e4SLinus Torvalds goto out; 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds out_illegal: 616f3213831SJoe Perches pr_err("Ignoring attempt to register invalid socket type %d\n", 6171da177e4SLinus Torvalds p->type); 6181da177e4SLinus Torvalds goto out; 6191da177e4SLinus Torvalds } 6207159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw); 6217159039aSYOSHIFUJI Hideaki 6221da177e4SLinus Torvalds void 6231da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p) 6241da177e4SLinus Torvalds { 6251da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & p->flags) { 626f3213831SJoe Perches pr_err("Attempt to unregister permanent protocol %d\n", 6271da177e4SLinus Torvalds p->protocol); 6281da177e4SLinus Torvalds } else { 6291da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock); 6301da177e4SLinus Torvalds list_del_rcu(&p->list); 6311da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock); 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds synchronize_net(); 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds } 6367159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw); 6377159039aSYOSHIFUJI Hideaki 638b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk) 639b9750ce1SArnaldo Carvalho de Melo { 640b9750ce1SArnaldo Carvalho de Melo struct ipv6_pinfo *np = inet6_sk(sk); 64168d0c6d3SDavid S. Miller struct dst_entry *dst; 642b9750ce1SArnaldo Carvalho de Melo 643b9750ce1SArnaldo Carvalho de Melo dst = __sk_dst_check(sk, np->dst_cookie); 644b9750ce1SArnaldo Carvalho de Melo 64563159f29SIan Morris if (!dst) { 646b9750ce1SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 64720c59de2SArnaud Ebalard struct in6_addr *final_p, final; 6484c9483b2SDavid S. Miller struct flowi6 fl6; 649b9750ce1SArnaldo Carvalho de Melo 6504c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 6514c9483b2SDavid S. Miller fl6.flowi6_proto = sk->sk_protocol; 652efe4208fSEric Dumazet fl6.daddr = sk->sk_v6_daddr; 6534e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 6544c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 6554c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 6564c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark; 6571958b856SDavid S. Miller fl6.fl6_dport = inet->inet_dport; 6581958b856SDavid S. Miller fl6.fl6_sport = inet->inet_sport; 6594c9483b2SDavid S. Miller security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); 660b9750ce1SArnaldo Carvalho de Melo 6614c9483b2SDavid S. Miller final_p = fl6_update_dst(&fl6, np->opt, &final); 662b9750ce1SArnaldo Carvalho de Melo 6630e0d44abSSteffen Klassert dst = ip6_dst_lookup_flow(sk, &fl6, final_p); 66468d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 665b9750ce1SArnaldo Carvalho de Melo sk->sk_route_caps = 0; 66668d0c6d3SDavid S. Miller sk->sk_err_soft = -PTR_ERR(dst); 66768d0c6d3SDavid S. Miller return PTR_ERR(dst); 668b9750ce1SArnaldo Carvalho de Melo } 669b9750ce1SArnaldo Carvalho de Melo 6708e1ef0a9SYOSHIFUJI Hideaki __ip6_dst_store(sk, dst, NULL, NULL); 671b9750ce1SArnaldo Carvalho de Melo } 672b9750ce1SArnaldo Carvalho de Melo 673b9750ce1SArnaldo Carvalho de Melo return 0; 674b9750ce1SArnaldo Carvalho de Melo } 675b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header); 676b9750ce1SArnaldo Carvalho de Melo 677a224772dSEric Dumazet bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb, 678a224772dSEric Dumazet const struct inet6_skb_parm *opt) 679399c07deSArnaldo Carvalho de Melo { 68092113bfdSEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk); 681399c07deSArnaldo Carvalho de Melo 682399c07deSArnaldo Carvalho de Melo if (np->rxopt.all) { 6838b58a398SFlorian Westphal if (((opt->flags & IP6SKB_HOPBYHOP) && 6848b58a398SFlorian Westphal (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) || 685ac1eabcaSFlorent Fourcot (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) && 686399c07deSArnaldo Carvalho de Melo np->rxopt.bits.rxflow) || 687399c07deSArnaldo Carvalho de Melo (opt->srcrt && (np->rxopt.bits.srcrt || 688399c07deSArnaldo Carvalho de Melo np->rxopt.bits.osrcrt)) || 689399c07deSArnaldo Carvalho de Melo ((opt->dst1 || opt->dst0) && 690399c07deSArnaldo Carvalho de Melo (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts))) 69192113bfdSEric Dumazet return true; 692399c07deSArnaldo Carvalho de Melo } 69392113bfdSEric Dumazet return false; 694399c07deSArnaldo Carvalho de Melo } 695399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted); 696399c07deSArnaldo Carvalho de Melo 6977546dd97SStephen Hemminger static struct packet_type ipv6_packet_type __read_mostly = { 69809640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_IPV6), 699662397fdSYOSHIFUJI Hideaki .func = ipv6_rcv, 70022061d80SVlad Yasevich }; 70122061d80SVlad Yasevich 702662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void) 703662397fdSYOSHIFUJI Hideaki { 704662397fdSYOSHIFUJI Hideaki dev_add_pack(&ipv6_packet_type); 705662397fdSYOSHIFUJI Hideaki return 0; 706662397fdSYOSHIFUJI Hideaki } 707662397fdSYOSHIFUJI Hideaki 708662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void) 709662397fdSYOSHIFUJI Hideaki { 710662397fdSYOSHIFUJI Hideaki dev_remove_pack(&ipv6_packet_type); 711662397fdSYOSHIFUJI Hideaki } 712662397fdSYOSHIFUJI Hideaki 713e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net) 714e43291cbSDenis V. Lunev { 715827da44cSJohn Stultz int i; 716827da44cSJohn Stultz 717698365faSWANG Cong net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib); 718698365faSWANG Cong if (!net->mib.udp_stats_in6) 7190c7ed677SDenis V. Lunev return -ENOMEM; 720698365faSWANG Cong net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib); 721698365faSWANG Cong if (!net->mib.udplite_stats_in6) 722be713a44SDenis V. Lunev goto err_udplite_mib; 723698365faSWANG Cong net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib); 724698365faSWANG Cong if (!net->mib.ipv6_statistics) 7259261e537SDenis V. Lunev goto err_ip_mib; 726827da44cSJohn Stultz 727827da44cSJohn Stultz for_each_possible_cpu(i) { 728827da44cSJohn Stultz struct ipstats_mib *af_inet6_stats; 729698365faSWANG Cong af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i); 730827da44cSJohn Stultz u64_stats_init(&af_inet6_stats->syncp); 731827da44cSJohn Stultz } 732827da44cSJohn Stultz 733827da44cSJohn Stultz 734698365faSWANG Cong net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib); 735698365faSWANG Cong if (!net->mib.icmpv6_statistics) 7369261e537SDenis V. Lunev goto err_icmp_mib; 7372a24444fSEric Dumazet net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib), 7382a24444fSEric Dumazet GFP_KERNEL); 7392a24444fSEric Dumazet if (!net->mib.icmpv6msg_statistics) 7409261e537SDenis V. Lunev goto err_icmpmsg_mib; 741e43291cbSDenis V. Lunev return 0; 742be713a44SDenis V. Lunev 7439261e537SDenis V. Lunev err_icmpmsg_mib: 744698365faSWANG Cong free_percpu(net->mib.icmpv6_statistics); 7459261e537SDenis V. Lunev err_icmp_mib: 746698365faSWANG Cong free_percpu(net->mib.ipv6_statistics); 7479261e537SDenis V. Lunev err_ip_mib: 748698365faSWANG Cong free_percpu(net->mib.udplite_stats_in6); 749be713a44SDenis V. Lunev err_udplite_mib: 750698365faSWANG Cong free_percpu(net->mib.udp_stats_in6); 751be713a44SDenis V. Lunev return -ENOMEM; 752e43291cbSDenis V. Lunev } 753e43291cbSDenis V. Lunev 7542c8c1e72SAlexey Dobriyan static void ipv6_cleanup_mibs(struct net *net) 755e43291cbSDenis V. Lunev { 756698365faSWANG Cong free_percpu(net->mib.udp_stats_in6); 757698365faSWANG Cong free_percpu(net->mib.udplite_stats_in6); 758698365faSWANG Cong free_percpu(net->mib.ipv6_statistics); 759698365faSWANG Cong free_percpu(net->mib.icmpv6_statistics); 7602a24444fSEric Dumazet kfree(net->mib.icmpv6msg_statistics); 761e43291cbSDenis V. Lunev } 762e43291cbSDenis V. Lunev 763e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net) 76481c1c178SDaniel Lezcano { 7650c96d8c5SDaniel Lezcano int err = 0; 7660c96d8c5SDaniel Lezcano 76799bc9c4eSDaniel Lezcano net->ipv6.sysctl.bindv6only = 0; 76841a76906SDaniel Lezcano net->ipv6.sysctl.icmpv6_time = 1*HZ; 7696444f72bSFlorent Fourcot net->ipv6.sysctl.flowlabel_consistency = 1; 770cb1ce2efSTom Herbert net->ipv6.sysctl.auto_flowlabels = 0; 7711855b7c3SHannes Frederic Sowa net->ipv6.sysctl.idgen_retries = 3; 7721855b7c3SHannes Frederic Sowa net->ipv6.sysctl.idgen_delay = 1 * HZ; 77382a584b7STom Herbert net->ipv6.sysctl.flowlabel_state_ranges = 1; 774812918c4SHannes Frederic Sowa atomic_set(&net->ipv6.fib6_sernum, 1); 775e71e0349SDaniel Lezcano 776e43291cbSDenis V. Lunev err = ipv6_init_mibs(net); 777e43291cbSDenis V. Lunev if (err) 778e43291cbSDenis V. Lunev return err; 7790c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS 7800c96d8c5SDaniel Lezcano err = udp6_proc_init(net); 7810c96d8c5SDaniel Lezcano if (err) 7820c96d8c5SDaniel Lezcano goto out; 7836f8b13bcSDaniel Lezcano err = tcp6_proc_init(net); 7846f8b13bcSDaniel Lezcano if (err) 7856f8b13bcSDaniel Lezcano goto proc_tcp6_fail; 7866ab57e7eSDaniel Lezcano err = ac6_proc_init(net); 7876ab57e7eSDaniel Lezcano if (err) 7886ab57e7eSDaniel Lezcano goto proc_ac6_fail; 7890c96d8c5SDaniel Lezcano #endif 7900c96d8c5SDaniel Lezcano return err; 7916f8b13bcSDaniel Lezcano 7926f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS 7936ab57e7eSDaniel Lezcano proc_ac6_fail: 7946ab57e7eSDaniel Lezcano tcp6_proc_exit(net); 7956f8b13bcSDaniel Lezcano proc_tcp6_fail: 7966f8b13bcSDaniel Lezcano udp6_proc_exit(net); 797e43291cbSDenis V. Lunev out: 798e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net); 799e43291cbSDenis V. Lunev return err; 8006f8b13bcSDaniel Lezcano #endif 80181c1c178SDaniel Lezcano } 80281c1c178SDaniel Lezcano 8032c8c1e72SAlexey Dobriyan static void __net_exit inet6_net_exit(struct net *net) 80481c1c178SDaniel Lezcano { 8050c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS 8060c96d8c5SDaniel Lezcano udp6_proc_exit(net); 8076f8b13bcSDaniel Lezcano tcp6_proc_exit(net); 8086ab57e7eSDaniel Lezcano ac6_proc_exit(net); 8090c96d8c5SDaniel Lezcano #endif 810e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net); 81181c1c178SDaniel Lezcano } 81281c1c178SDaniel Lezcano 81381c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = { 81481c1c178SDaniel Lezcano .init = inet6_net_init, 81581c1c178SDaniel Lezcano .exit = inet6_net_exit, 81681c1c178SDaniel Lezcano }; 81781c1c178SDaniel Lezcano 8185f81bd2eSCong Wang static const struct ipv6_stub ipv6_stub_impl = { 8195f81bd2eSCong Wang .ipv6_sock_mc_join = ipv6_sock_mc_join, 8205f81bd2eSCong Wang .ipv6_sock_mc_drop = ipv6_sock_mc_drop, 8215f81bd2eSCong Wang .ipv6_dst_lookup = ip6_dst_lookup, 8225f81bd2eSCong Wang .udpv6_encap_enable = udpv6_encap_enable, 823f564f45cSCong Wang .ndisc_send_na = ndisc_send_na, 824e15a00aaSCong Wang .nd_tbl = &nd_tbl, 8255f81bd2eSCong Wang }; 8265f81bd2eSCong Wang 8271da177e4SLinus Torvalds static int __init inet6_init(void) 8281da177e4SLinus Torvalds { 8291da177e4SLinus Torvalds struct list_head *r; 830fe7ca2e1SBrian Haley int err = 0; 8311da177e4SLinus Torvalds 832b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct inet6_skb_parm)); 833ef047f5eSYOSHIFUJI Hideaki 834fe7ca2e1SBrian Haley /* Register the socket-side information for inet6_create. */ 835fe7ca2e1SBrian Haley for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r) 836fe7ca2e1SBrian Haley INIT_LIST_HEAD(r); 837fe7ca2e1SBrian Haley 83856d417b1SBrian Haley if (disable_ipv6_mod) { 839f3213831SJoe Perches pr_info("Loaded, but administratively disabled, reboot required to enable\n"); 840fe7ca2e1SBrian Haley goto out; 841fe7ca2e1SBrian Haley } 842fe7ca2e1SBrian Haley 8431da177e4SLinus Torvalds err = proto_register(&tcpv6_prot, 1); 8441da177e4SLinus Torvalds if (err) 8451da177e4SLinus Torvalds goto out; 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds err = proto_register(&udpv6_prot, 1); 8481da177e4SLinus Torvalds if (err) 8491da177e4SLinus Torvalds goto out_unregister_tcp_proto; 8501da177e4SLinus Torvalds 851ba4e58ecSGerrit Renker err = proto_register(&udplitev6_prot, 1); 8521da177e4SLinus Torvalds if (err) 8531da177e4SLinus Torvalds goto out_unregister_udp_proto; 8541da177e4SLinus Torvalds 855ba4e58ecSGerrit Renker err = proto_register(&rawv6_prot, 1); 856ba4e58ecSGerrit Renker if (err) 857ba4e58ecSGerrit Renker goto out_unregister_udplite_proto; 858ba4e58ecSGerrit Renker 8596d0bfe22SLorenzo Colitti err = proto_register(&pingv6_prot, 1); 8606d0bfe22SLorenzo Colitti if (err) 8616d0bfe22SLorenzo Colitti goto out_unregister_ping_proto; 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds /* We MUST register RAW sockets before we create the ICMP6, 8641da177e4SLinus Torvalds * IGMP6, or NDISC control sockets. 8651da177e4SLinus Torvalds */ 8667f4e4868SDaniel Lezcano err = rawv6_init(); 8677f4e4868SDaniel Lezcano if (err) 8687f4e4868SDaniel Lezcano goto out_unregister_raw_proto; 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds /* Register the family here so that the init calls below will 8711da177e4SLinus Torvalds * be able to create sockets. (?? is this dangerous ??) 8721da177e4SLinus Torvalds */ 8738eb55910SDavid S. Miller err = sock_register(&inet6_family_ops); 8748eb55910SDavid S. Miller if (err) 8757f4e4868SDaniel Lezcano goto out_sock_register_fail; 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds /* 8781da177e4SLinus Torvalds * ipngwg API draft makes clear that the correct semantics 8791da177e4SLinus Torvalds * for TCP and UDP is to consider one TCP and UDP instance 88025985edcSLucas De Marchi * in a host available by both INET and INET6 APIs and 8811da177e4SLinus Torvalds * able to communicate via both network protocols. 8821da177e4SLinus Torvalds */ 8831da177e4SLinus Torvalds 88481c1c178SDaniel Lezcano err = register_pernet_subsys(&inet6_net_ops); 88581c1c178SDaniel Lezcano if (err) 88681c1c178SDaniel Lezcano goto register_pernet_fail; 8879b0f976fSDenis V. Lunev err = icmpv6_init(); 8881da177e4SLinus Torvalds if (err) 8891da177e4SLinus Torvalds goto icmp_fail; 890623d1a1aSWang Chen err = ip6_mr_init(); 891623d1a1aSWang Chen if (err) 892623d1a1aSWang Chen goto ipmr_fail; 8939b0f976fSDenis V. Lunev err = ndisc_init(); 8941da177e4SLinus Torvalds if (err) 8951da177e4SLinus Torvalds goto ndisc_fail; 8969b0f976fSDenis V. Lunev err = igmp6_init(); 8971da177e4SLinus Torvalds if (err) 8981da177e4SLinus Torvalds goto igmp_fail; 8995f81bd2eSCong Wang 9005f81bd2eSCong Wang ipv6_stub = &ipv6_stub_impl; 9015f81bd2eSCong Wang 9022cc7d573SHarald Welte err = ipv6_netfilter_init(); 9032cc7d573SHarald Welte if (err) 9042cc7d573SHarald Welte goto netfilter_fail; 9051da177e4SLinus Torvalds /* Create /proc/foo6 entries. */ 9061da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 9071da177e4SLinus Torvalds err = -ENOMEM; 9081da177e4SLinus Torvalds if (raw6_proc_init()) 9091da177e4SLinus Torvalds goto proc_raw6_fail; 910ba4e58ecSGerrit Renker if (udplite6_proc_init()) 911ba4e58ecSGerrit Renker goto proc_udplite6_fail; 9121da177e4SLinus Torvalds if (ipv6_misc_proc_init()) 9131da177e4SLinus Torvalds goto proc_misc6_fail; 9141da177e4SLinus Torvalds if (if6_proc_init()) 9151da177e4SLinus Torvalds goto proc_if6_fail; 9161da177e4SLinus Torvalds #endif 917e2fddf5eSDaniel Lezcano err = ip6_route_init(); 918e2fddf5eSDaniel Lezcano if (err) 919e2fddf5eSDaniel Lezcano goto ip6_route_fail; 9202c861cc6SMichal Kubeček err = ndisc_late_init(); 9212c861cc6SMichal Kubeček if (err) 9222c861cc6SMichal Kubeček goto ndisc_late_fail; 9230a3e78acSDaniel Lezcano err = ip6_flowlabel_init(); 9240a3e78acSDaniel Lezcano if (err) 9250a3e78acSDaniel Lezcano goto ip6_flowlabel_fail; 9261da177e4SLinus Torvalds err = addrconf_init(); 9271da177e4SLinus Torvalds if (err) 9281da177e4SLinus Torvalds goto addrconf_fail; 9291da177e4SLinus Torvalds 9301da177e4SLinus Torvalds /* Init v6 extension headers. */ 931248b238dSDaniel Lezcano err = ipv6_exthdrs_init(); 932248b238dSDaniel Lezcano if (err) 933248b238dSDaniel Lezcano goto ipv6_exthdrs_fail; 934248b238dSDaniel Lezcano 935853cbbaaSDaniel Lezcano err = ipv6_frag_init(); 936853cbbaaSDaniel Lezcano if (err) 937853cbbaaSDaniel Lezcano goto ipv6_frag_fail; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds /* Init v6 transport protocols. */ 9407f4e4868SDaniel Lezcano err = udpv6_init(); 9417f4e4868SDaniel Lezcano if (err) 9427f4e4868SDaniel Lezcano goto udpv6_fail; 943e2ed4052SHerbert Xu 9447f4e4868SDaniel Lezcano err = udplitev6_init(); 9457f4e4868SDaniel Lezcano if (err) 9467f4e4868SDaniel Lezcano goto udplitev6_fail; 9477f4e4868SDaniel Lezcano 9487f4e4868SDaniel Lezcano err = tcpv6_init(); 9497f4e4868SDaniel Lezcano if (err) 9507f4e4868SDaniel Lezcano goto tcpv6_fail; 9517f4e4868SDaniel Lezcano 9527f4e4868SDaniel Lezcano err = ipv6_packet_init(); 9537f4e4868SDaniel Lezcano if (err) 9547f4e4868SDaniel Lezcano goto ipv6_packet_fail; 95594911fe3SBenjamin Thery 9566d0bfe22SLorenzo Colitti err = pingv6_init(); 9576d0bfe22SLorenzo Colitti if (err) 9586d0bfe22SLorenzo Colitti goto pingv6_fail; 9596d0bfe22SLorenzo Colitti 96094911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL 96194911fe3SBenjamin Thery err = ipv6_sysctl_register(); 96294911fe3SBenjamin Thery if (err) 96394911fe3SBenjamin Thery goto sysctl_fail; 96494911fe3SBenjamin Thery #endif 9651da177e4SLinus Torvalds out: 9661da177e4SLinus Torvalds return err; 9671da177e4SLinus Torvalds 96894911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL 96994911fe3SBenjamin Thery sysctl_fail: 970eca42aafSVlad Yasevich pingv6_exit(); 97194911fe3SBenjamin Thery #endif 9726d0bfe22SLorenzo Colitti pingv6_fail: 973eca42aafSVlad Yasevich ipv6_packet_cleanup(); 9747f4e4868SDaniel Lezcano ipv6_packet_fail: 9757f4e4868SDaniel Lezcano tcpv6_exit(); 9767f4e4868SDaniel Lezcano tcpv6_fail: 9777f4e4868SDaniel Lezcano udplitev6_exit(); 9787f4e4868SDaniel Lezcano udplitev6_fail: 9797f4e4868SDaniel Lezcano udpv6_exit(); 9807f4e4868SDaniel Lezcano udpv6_fail: 9817f4e4868SDaniel Lezcano ipv6_frag_exit(); 982853cbbaaSDaniel Lezcano ipv6_frag_fail: 983853cbbaaSDaniel Lezcano ipv6_exthdrs_exit(); 984248b238dSDaniel Lezcano ipv6_exthdrs_fail: 985248b238dSDaniel Lezcano addrconf_cleanup(); 9861da177e4SLinus Torvalds addrconf_fail: 9871da177e4SLinus Torvalds ip6_flowlabel_cleanup(); 9880a3e78acSDaniel Lezcano ip6_flowlabel_fail: 9892c861cc6SMichal Kubeček ndisc_late_cleanup(); 9902c861cc6SMichal Kubeček ndisc_late_fail: 9911da177e4SLinus Torvalds ip6_route_cleanup(); 992e2fddf5eSDaniel Lezcano ip6_route_fail: 9931da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 9941da177e4SLinus Torvalds if6_proc_exit(); 9951da177e4SLinus Torvalds proc_if6_fail: 9961da177e4SLinus Torvalds ipv6_misc_proc_exit(); 9971da177e4SLinus Torvalds proc_misc6_fail: 998ba4e58ecSGerrit Renker udplite6_proc_exit(); 999ba4e58ecSGerrit Renker proc_udplite6_fail: 10001da177e4SLinus Torvalds raw6_proc_exit(); 10011da177e4SLinus Torvalds proc_raw6_fail: 10021da177e4SLinus Torvalds #endif 10032cc7d573SHarald Welte ipv6_netfilter_fini(); 10042cc7d573SHarald Welte netfilter_fail: 10051da177e4SLinus Torvalds igmp6_cleanup(); 10061da177e4SLinus Torvalds igmp_fail: 10071da177e4SLinus Torvalds ndisc_cleanup(); 10081da177e4SLinus Torvalds ndisc_fail: 1009623d1a1aSWang Chen ip6_mr_cleanup(); 1010623d1a1aSWang Chen ipmr_fail: 10111da177e4SLinus Torvalds icmpv6_cleanup(); 10121da177e4SLinus Torvalds icmp_fail: 101381c1c178SDaniel Lezcano unregister_pernet_subsys(&inet6_net_ops); 101481c1c178SDaniel Lezcano register_pernet_fail: 10158eb55910SDavid S. Miller sock_unregister(PF_INET6); 1016e2fddf5eSDaniel Lezcano rtnl_unregister_all(PF_INET6); 10177f4e4868SDaniel Lezcano out_sock_register_fail: 10187f4e4868SDaniel Lezcano rawv6_exit(); 10196d0bfe22SLorenzo Colitti out_unregister_ping_proto: 10206d0bfe22SLorenzo Colitti proto_unregister(&pingv6_prot); 10211da177e4SLinus Torvalds out_unregister_raw_proto: 10221da177e4SLinus Torvalds proto_unregister(&rawv6_prot); 1023ba4e58ecSGerrit Renker out_unregister_udplite_proto: 1024ba4e58ecSGerrit Renker proto_unregister(&udplitev6_prot); 10251da177e4SLinus Torvalds out_unregister_udp_proto: 10261da177e4SLinus Torvalds proto_unregister(&udpv6_prot); 10271da177e4SLinus Torvalds out_unregister_tcp_proto: 10281da177e4SLinus Torvalds proto_unregister(&tcpv6_prot); 10291da177e4SLinus Torvalds goto out; 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds module_init(inet6_init); 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6); 1034