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)) { 345f74024d9SMaciej Żenczykowski if (!(inet->freebind || inet->transparent) && 3460a513f6aSBalazs Scheidler !ipv6_chk_addr(net, &addr->sin6_addr, 347bfeade08SDaniel Lezcano dev, 0)) { 3481da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 34916ba5e8eSEric Dumazet goto out_unlock; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds } 35216ba5e8eSEric Dumazet rcu_read_unlock(); 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds 356c720c7e8SEric Dumazet inet->inet_rcv_saddr = v4addr; 357c720c7e8SEric Dumazet inet->inet_saddr = v4addr; 3581da177e4SLinus Torvalds 359efe4208fSEric Dumazet sk->sk_v6_rcv_saddr = addr->sin6_addr; 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST)) 3624e3fd7a0SAlexey Dobriyan np->saddr = addr->sin6_addr; 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds /* Make sure we are allowed to bind here. */ 3651da177e4SLinus Torvalds if (sk->sk_prot->get_port(sk, snum)) { 3661da177e4SLinus Torvalds inet_reset_saddr(sk); 3671da177e4SLinus Torvalds err = -EADDRINUSE; 3681da177e4SLinus Torvalds goto out; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 3710f8d3c7aSVlad Yasevich if (addr_type != IPV6_ADDR_ANY) { 3721da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDADDR_LOCK; 3730f8d3c7aSVlad Yasevich if (addr_type != IPV6_ADDR_MAPPED) 3749fe516baSEric Dumazet sk->sk_ipv6only = 1; 3750f8d3c7aSVlad Yasevich } 3761da177e4SLinus Torvalds if (snum) 3771da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 378c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 379c720c7e8SEric Dumazet inet->inet_dport = 0; 380c720c7e8SEric Dumazet inet->inet_daddr = 0; 3811da177e4SLinus Torvalds out: 3821da177e4SLinus Torvalds release_sock(sk); 3831da177e4SLinus Torvalds return err; 38416ba5e8eSEric Dumazet out_unlock: 38516ba5e8eSEric Dumazet rcu_read_unlock(); 38616ba5e8eSEric Dumazet goto out; 3871da177e4SLinus Torvalds } 3887159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_bind); 3897159039aSYOSHIFUJI Hideaki 3901da177e4SLinus Torvalds int inet6_release(struct socket *sock) 3911da177e4SLinus Torvalds { 3921da177e4SLinus Torvalds struct sock *sk = sock->sk; 3931da177e4SLinus Torvalds 39463159f29SIan Morris if (!sk) 3951da177e4SLinus Torvalds return -EINVAL; 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds /* Free mc lists */ 3981da177e4SLinus Torvalds ipv6_sock_mc_close(sk); 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds /* Free ac lists */ 4011da177e4SLinus Torvalds ipv6_sock_ac_close(sk); 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds return inet_release(sock); 4041da177e4SLinus Torvalds } 4057159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release); 4067159039aSYOSHIFUJI Hideaki 4077d06b2e0SBrian Haley void inet6_destroy_sock(struct sock *sk) 4081da177e4SLinus Torvalds { 4091da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4101da177e4SLinus Torvalds struct sk_buff *skb; 4111da177e4SLinus Torvalds struct ipv6_txoptions *opt; 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds /* Release rx options */ 4141da177e4SLinus Torvalds 415647c0c70SEldad Zack skb = xchg(&np->pktoptions, NULL); 41653b24b8fSIan Morris if (skb) 4171da177e4SLinus Torvalds kfree_skb(skb); 4181da177e4SLinus Torvalds 419647c0c70SEldad Zack skb = xchg(&np->rxpmtu, NULL); 42053b24b8fSIan Morris if (skb) 4214b340ae2SBrian Haley kfree_skb(skb); 4224b340ae2SBrian Haley 4231da177e4SLinus Torvalds /* Free flowlabels */ 4241da177e4SLinus Torvalds fl6_free_socklist(sk); 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds /* Free tx options */ 4271da177e4SLinus Torvalds 428647c0c70SEldad Zack opt = xchg(&np->opt, NULL); 42953b24b8fSIan Morris if (opt) 4301da177e4SLinus Torvalds sock_kfree_s(sk, opt, opt->tot_len); 4311da177e4SLinus Torvalds } 4323cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_destroy_sock); 4333cf3dc6cSArnaldo Carvalho de Melo 4341da177e4SLinus Torvalds /* 4351da177e4SLinus Torvalds * This does both peername and sockname. 4361da177e4SLinus Torvalds */ 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr, 4391da177e4SLinus Torvalds int *uaddr_len, int peer) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr; 4421da177e4SLinus Torvalds struct sock *sk = sock->sk; 4431da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 4441da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds sin->sin6_family = AF_INET6; 4471da177e4SLinus Torvalds sin->sin6_flowinfo = 0; 4481da177e4SLinus Torvalds sin->sin6_scope_id = 0; 4491da177e4SLinus Torvalds if (peer) { 450c720c7e8SEric Dumazet if (!inet->inet_dport) 4511da177e4SLinus Torvalds return -ENOTCONN; 4521da177e4SLinus Torvalds if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && 4531da177e4SLinus Torvalds peer == 1) 4541da177e4SLinus Torvalds return -ENOTCONN; 455c720c7e8SEric Dumazet sin->sin6_port = inet->inet_dport; 456efe4208fSEric Dumazet sin->sin6_addr = sk->sk_v6_daddr; 4571da177e4SLinus Torvalds if (np->sndflow) 4581da177e4SLinus Torvalds sin->sin6_flowinfo = np->flow_label; 4591da177e4SLinus Torvalds } else { 460efe4208fSEric Dumazet if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) 4614e3fd7a0SAlexey Dobriyan sin->sin6_addr = np->saddr; 4621da177e4SLinus Torvalds else 463efe4208fSEric Dumazet sin->sin6_addr = sk->sk_v6_rcv_saddr; 4641da177e4SLinus Torvalds 465c720c7e8SEric Dumazet sin->sin6_port = inet->inet_sport; 4661da177e4SLinus Torvalds } 467842df073SHannes Frederic Sowa sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr, 468842df073SHannes Frederic Sowa sk->sk_bound_dev_if); 4691da177e4SLinus Torvalds *uaddr_len = sizeof(*sin); 470a02cec21SEric Dumazet return 0; 4711da177e4SLinus Torvalds } 4727159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname); 4737159039aSYOSHIFUJI Hideaki 4741da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 4751da177e4SLinus Torvalds { 4761da177e4SLinus Torvalds struct sock *sk = sock->sk; 4773b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 4781da177e4SLinus Torvalds 479647c0c70SEldad Zack switch (cmd) { 4801da177e4SLinus Torvalds case SIOCGSTAMP: 4811da177e4SLinus Torvalds return sock_get_timestamp(sk, (struct timeval __user *)arg); 4821da177e4SLinus Torvalds 483ae40eb1eSEric Dumazet case SIOCGSTAMPNS: 484ae40eb1eSEric Dumazet return sock_get_timestampns(sk, (struct timespec __user *)arg); 485ae40eb1eSEric Dumazet 4861da177e4SLinus Torvalds case SIOCADDRT: 4871da177e4SLinus Torvalds case SIOCDELRT: 4881da177e4SLinus Torvalds 489a02cec21SEric Dumazet return ipv6_route_ioctl(net, cmd, (void __user *)arg); 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds case SIOCSIFADDR: 492af284937SDaniel Lezcano return addrconf_add_ifaddr(net, (void __user *) arg); 4931da177e4SLinus Torvalds case SIOCDIFADDR: 494af284937SDaniel Lezcano return addrconf_del_ifaddr(net, (void __user *) arg); 4951da177e4SLinus Torvalds case SIOCSIFDSTADDR: 496af284937SDaniel Lezcano return addrconf_set_dstaddr(net, (void __user *) arg); 4971da177e4SLinus Torvalds default: 498b5e5fa5eSChristoph Hellwig if (!sk->sk_prot->ioctl) 499b5e5fa5eSChristoph Hellwig return -ENOIOCTLCMD; 500b5e5fa5eSChristoph Hellwig return sk->sk_prot->ioctl(sk, cmd, arg); 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds /*NOTREACHED*/ 503a02cec21SEric Dumazet return 0; 5041da177e4SLinus Torvalds } 5057159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl); 5067159039aSYOSHIFUJI Hideaki 50790ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = { 5081da177e4SLinus Torvalds .family = PF_INET6, 5091da177e4SLinus Torvalds .owner = THIS_MODULE, 5101da177e4SLinus Torvalds .release = inet6_release, 5111da177e4SLinus Torvalds .bind = inet6_bind, 5121da177e4SLinus Torvalds .connect = inet_stream_connect, /* ok */ 5131da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */ 5141da177e4SLinus Torvalds .accept = inet_accept, /* ok */ 5151da177e4SLinus Torvalds .getname = inet6_getname, 5161da177e4SLinus Torvalds .poll = tcp_poll, /* ok */ 5171da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */ 5181da177e4SLinus Torvalds .listen = inet_listen, /* ok */ 5191da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */ 5201da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */ 5211da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */ 5227ba42910SChangli Gao .sendmsg = inet_sendmsg, /* ok */ 5237ba42910SChangli Gao .recvmsg = inet_recvmsg, /* ok */ 524543d9cfeSArnaldo Carvalho de Melo .mmap = sock_no_mmap, 5257ba42910SChangli Gao .sendpage = inet_sendpage, 526a0974dd3SJens Axboe .splice_read = tcp_splice_read, 5273fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 5283fdadf7dSDmitry Mishin .compat_setsockopt = compat_sock_common_setsockopt, 5293fdadf7dSDmitry Mishin .compat_getsockopt = compat_sock_common_getsockopt, 5303fdadf7dSDmitry Mishin #endif 5311da177e4SLinus Torvalds }; 5321da177e4SLinus Torvalds 53390ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = { 5341da177e4SLinus Torvalds .family = PF_INET6, 5351da177e4SLinus Torvalds .owner = THIS_MODULE, 5361da177e4SLinus Torvalds .release = inet6_release, 5371da177e4SLinus Torvalds .bind = inet6_bind, 5381da177e4SLinus Torvalds .connect = inet_dgram_connect, /* ok */ 5391da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */ 5401da177e4SLinus Torvalds .accept = sock_no_accept, /* a do nothing */ 5411da177e4SLinus Torvalds .getname = inet6_getname, 5421da177e4SLinus Torvalds .poll = udp_poll, /* ok */ 5431da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */ 5441da177e4SLinus Torvalds .listen = sock_no_listen, /* ok */ 5451da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */ 5461da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */ 5471da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */ 5481da177e4SLinus Torvalds .sendmsg = inet_sendmsg, /* ok */ 5497ba42910SChangli Gao .recvmsg = inet_recvmsg, /* ok */ 5501da177e4SLinus Torvalds .mmap = sock_no_mmap, 5511da177e4SLinus Torvalds .sendpage = sock_no_sendpage, 552543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 553543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_sock_common_setsockopt, 554543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_sock_common_getsockopt, 555543d9cfeSArnaldo Carvalho de Melo #endif 5561da177e4SLinus Torvalds }; 5571da177e4SLinus Torvalds 558ec1b4cf7SStephen Hemminger static const struct net_proto_family inet6_family_ops = { 5591da177e4SLinus Torvalds .family = PF_INET6, 5601da177e4SLinus Torvalds .create = inet6_create, 5611da177e4SLinus Torvalds .owner = THIS_MODULE, 5621da177e4SLinus Torvalds }; 5631da177e4SLinus Torvalds 56487c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p) 5651da177e4SLinus Torvalds { 5661da177e4SLinus Torvalds struct list_head *lh; 5671da177e4SLinus Torvalds struct inet_protosw *answer; 5681da177e4SLinus Torvalds struct list_head *last_perm; 56987c3efbfSDaniel Lezcano int protocol = p->protocol; 57087c3efbfSDaniel Lezcano int ret; 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock); 5731da177e4SLinus Torvalds 57487c3efbfSDaniel Lezcano ret = -EINVAL; 5751da177e4SLinus Torvalds if (p->type >= SOCK_MAX) 5761da177e4SLinus Torvalds goto out_illegal; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds /* If we are trying to override a permanent protocol, bail. */ 5791da177e4SLinus Torvalds answer = NULL; 58087c3efbfSDaniel Lezcano ret = -EPERM; 5811da177e4SLinus Torvalds last_perm = &inetsw6[p->type]; 5821da177e4SLinus Torvalds list_for_each(lh, &inetsw6[p->type]) { 5831da177e4SLinus Torvalds answer = list_entry(lh, struct inet_protosw, list); 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds /* Check only the non-wild match. */ 5861da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & answer->flags) { 5871da177e4SLinus Torvalds if (protocol == answer->protocol) 5881da177e4SLinus Torvalds break; 5891da177e4SLinus Torvalds last_perm = lh; 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds answer = NULL; 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds if (answer) 5951da177e4SLinus Torvalds goto out_permanent; 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds /* Add the new entry after the last permanent entry if any, so that 5981da177e4SLinus Torvalds * the new entry does not override a permanent entry when matched with 5991da177e4SLinus Torvalds * a wild-card protocol. But it is allowed to override any existing 6001da177e4SLinus Torvalds * non-permanent entry. This means that when we remove this entry, the 6011da177e4SLinus Torvalds * system automatically returns to the old behavior. 6021da177e4SLinus Torvalds */ 6031da177e4SLinus Torvalds list_add_rcu(&p->list, last_perm); 60487c3efbfSDaniel Lezcano ret = 0; 6051da177e4SLinus Torvalds out: 6061da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock); 60787c3efbfSDaniel Lezcano return ret; 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds out_permanent: 610f3213831SJoe Perches pr_err("Attempt to override permanent protocol %d\n", protocol); 6111da177e4SLinus Torvalds goto out; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds out_illegal: 614f3213831SJoe Perches pr_err("Ignoring attempt to register invalid socket type %d\n", 6151da177e4SLinus Torvalds p->type); 6161da177e4SLinus Torvalds goto out; 6171da177e4SLinus Torvalds } 6187159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw); 6197159039aSYOSHIFUJI Hideaki 6201da177e4SLinus Torvalds void 6211da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p) 6221da177e4SLinus Torvalds { 6231da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & p->flags) { 624f3213831SJoe Perches pr_err("Attempt to unregister permanent protocol %d\n", 6251da177e4SLinus Torvalds p->protocol); 6261da177e4SLinus Torvalds } else { 6271da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock); 6281da177e4SLinus Torvalds list_del_rcu(&p->list); 6291da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock); 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds synchronize_net(); 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds } 6347159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw); 6357159039aSYOSHIFUJI Hideaki 636b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk) 637b9750ce1SArnaldo Carvalho de Melo { 638b9750ce1SArnaldo Carvalho de Melo struct ipv6_pinfo *np = inet6_sk(sk); 63968d0c6d3SDavid S. Miller struct dst_entry *dst; 640b9750ce1SArnaldo Carvalho de Melo 641b9750ce1SArnaldo Carvalho de Melo dst = __sk_dst_check(sk, np->dst_cookie); 642b9750ce1SArnaldo Carvalho de Melo 64363159f29SIan Morris if (!dst) { 644b9750ce1SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 64520c59de2SArnaud Ebalard struct in6_addr *final_p, final; 6464c9483b2SDavid S. Miller struct flowi6 fl6; 647b9750ce1SArnaldo Carvalho de Melo 6484c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 6494c9483b2SDavid S. Miller fl6.flowi6_proto = sk->sk_protocol; 650efe4208fSEric Dumazet fl6.daddr = sk->sk_v6_daddr; 6514e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 6524c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 6534c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 6544c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark; 6551958b856SDavid S. Miller fl6.fl6_dport = inet->inet_dport; 6561958b856SDavid S. Miller fl6.fl6_sport = inet->inet_sport; 6574c9483b2SDavid S. Miller security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); 658b9750ce1SArnaldo Carvalho de Melo 6594c9483b2SDavid S. Miller final_p = fl6_update_dst(&fl6, np->opt, &final); 660b9750ce1SArnaldo Carvalho de Melo 6610e0d44abSSteffen Klassert dst = ip6_dst_lookup_flow(sk, &fl6, final_p); 66268d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 663b9750ce1SArnaldo Carvalho de Melo sk->sk_route_caps = 0; 66468d0c6d3SDavid S. Miller sk->sk_err_soft = -PTR_ERR(dst); 66568d0c6d3SDavid S. Miller return PTR_ERR(dst); 666b9750ce1SArnaldo Carvalho de Melo } 667b9750ce1SArnaldo Carvalho de Melo 6688e1ef0a9SYOSHIFUJI Hideaki __ip6_dst_store(sk, dst, NULL, NULL); 669b9750ce1SArnaldo Carvalho de Melo } 670b9750ce1SArnaldo Carvalho de Melo 671b9750ce1SArnaldo Carvalho de Melo return 0; 672b9750ce1SArnaldo Carvalho de Melo } 673b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header); 674b9750ce1SArnaldo Carvalho de Melo 675a224772dSEric Dumazet bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb, 676a224772dSEric Dumazet const struct inet6_skb_parm *opt) 677399c07deSArnaldo Carvalho de Melo { 67892113bfdSEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk); 679399c07deSArnaldo Carvalho de Melo 680399c07deSArnaldo Carvalho de Melo if (np->rxopt.all) { 681399c07deSArnaldo Carvalho de Melo if ((opt->hop && (np->rxopt.bits.hopopts || 682399c07deSArnaldo Carvalho de Melo np->rxopt.bits.ohopopts)) || 683ac1eabcaSFlorent Fourcot (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) && 684399c07deSArnaldo Carvalho de Melo np->rxopt.bits.rxflow) || 685399c07deSArnaldo Carvalho de Melo (opt->srcrt && (np->rxopt.bits.srcrt || 686399c07deSArnaldo Carvalho de Melo np->rxopt.bits.osrcrt)) || 687399c07deSArnaldo Carvalho de Melo ((opt->dst1 || opt->dst0) && 688399c07deSArnaldo Carvalho de Melo (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts))) 68992113bfdSEric Dumazet return true; 690399c07deSArnaldo Carvalho de Melo } 69192113bfdSEric Dumazet return false; 692399c07deSArnaldo Carvalho de Melo } 693399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted); 694399c07deSArnaldo Carvalho de Melo 6957546dd97SStephen Hemminger static struct packet_type ipv6_packet_type __read_mostly = { 69609640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_IPV6), 697662397fdSYOSHIFUJI Hideaki .func = ipv6_rcv, 69822061d80SVlad Yasevich }; 69922061d80SVlad Yasevich 700662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void) 701662397fdSYOSHIFUJI Hideaki { 702662397fdSYOSHIFUJI Hideaki dev_add_pack(&ipv6_packet_type); 703662397fdSYOSHIFUJI Hideaki return 0; 704662397fdSYOSHIFUJI Hideaki } 705662397fdSYOSHIFUJI Hideaki 706662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void) 707662397fdSYOSHIFUJI Hideaki { 708662397fdSYOSHIFUJI Hideaki dev_remove_pack(&ipv6_packet_type); 709662397fdSYOSHIFUJI Hideaki } 710662397fdSYOSHIFUJI Hideaki 711e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net) 712e43291cbSDenis V. Lunev { 713827da44cSJohn Stultz int i; 714827da44cSJohn Stultz 715698365faSWANG Cong net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib); 716698365faSWANG Cong if (!net->mib.udp_stats_in6) 7170c7ed677SDenis V. Lunev return -ENOMEM; 718698365faSWANG Cong net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib); 719698365faSWANG Cong if (!net->mib.udplite_stats_in6) 720be713a44SDenis V. Lunev goto err_udplite_mib; 721698365faSWANG Cong net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib); 722698365faSWANG Cong if (!net->mib.ipv6_statistics) 7239261e537SDenis V. Lunev goto err_ip_mib; 724827da44cSJohn Stultz 725827da44cSJohn Stultz for_each_possible_cpu(i) { 726827da44cSJohn Stultz struct ipstats_mib *af_inet6_stats; 727698365faSWANG Cong af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i); 728827da44cSJohn Stultz u64_stats_init(&af_inet6_stats->syncp); 729827da44cSJohn Stultz } 730827da44cSJohn Stultz 731827da44cSJohn Stultz 732698365faSWANG Cong net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib); 733698365faSWANG Cong if (!net->mib.icmpv6_statistics) 7349261e537SDenis V. Lunev goto err_icmp_mib; 7352a24444fSEric Dumazet net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib), 7362a24444fSEric Dumazet GFP_KERNEL); 7372a24444fSEric Dumazet if (!net->mib.icmpv6msg_statistics) 7389261e537SDenis V. Lunev goto err_icmpmsg_mib; 739e43291cbSDenis V. Lunev return 0; 740be713a44SDenis V. Lunev 7419261e537SDenis V. Lunev err_icmpmsg_mib: 742698365faSWANG Cong free_percpu(net->mib.icmpv6_statistics); 7439261e537SDenis V. Lunev err_icmp_mib: 744698365faSWANG Cong free_percpu(net->mib.ipv6_statistics); 7459261e537SDenis V. Lunev err_ip_mib: 746698365faSWANG Cong free_percpu(net->mib.udplite_stats_in6); 747be713a44SDenis V. Lunev err_udplite_mib: 748698365faSWANG Cong free_percpu(net->mib.udp_stats_in6); 749be713a44SDenis V. Lunev return -ENOMEM; 750e43291cbSDenis V. Lunev } 751e43291cbSDenis V. Lunev 7522c8c1e72SAlexey Dobriyan static void ipv6_cleanup_mibs(struct net *net) 753e43291cbSDenis V. Lunev { 754698365faSWANG Cong free_percpu(net->mib.udp_stats_in6); 755698365faSWANG Cong free_percpu(net->mib.udplite_stats_in6); 756698365faSWANG Cong free_percpu(net->mib.ipv6_statistics); 757698365faSWANG Cong free_percpu(net->mib.icmpv6_statistics); 7582a24444fSEric Dumazet kfree(net->mib.icmpv6msg_statistics); 759e43291cbSDenis V. Lunev } 760e43291cbSDenis V. Lunev 761e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net) 76281c1c178SDaniel Lezcano { 7630c96d8c5SDaniel Lezcano int err = 0; 7640c96d8c5SDaniel Lezcano 76599bc9c4eSDaniel Lezcano net->ipv6.sysctl.bindv6only = 0; 76641a76906SDaniel Lezcano net->ipv6.sysctl.icmpv6_time = 1*HZ; 7676444f72bSFlorent Fourcot net->ipv6.sysctl.flowlabel_consistency = 1; 768cb1ce2efSTom Herbert net->ipv6.sysctl.auto_flowlabels = 0; 7691855b7c3SHannes Frederic Sowa net->ipv6.sysctl.idgen_retries = 3; 7701855b7c3SHannes Frederic Sowa net->ipv6.sysctl.idgen_delay = 1 * HZ; 77182a584b7STom Herbert net->ipv6.sysctl.flowlabel_state_ranges = 1; 772812918c4SHannes Frederic Sowa atomic_set(&net->ipv6.fib6_sernum, 1); 773e71e0349SDaniel Lezcano 774e43291cbSDenis V. Lunev err = ipv6_init_mibs(net); 775e43291cbSDenis V. Lunev if (err) 776e43291cbSDenis V. Lunev return err; 7770c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS 7780c96d8c5SDaniel Lezcano err = udp6_proc_init(net); 7790c96d8c5SDaniel Lezcano if (err) 7800c96d8c5SDaniel Lezcano goto out; 7816f8b13bcSDaniel Lezcano err = tcp6_proc_init(net); 7826f8b13bcSDaniel Lezcano if (err) 7836f8b13bcSDaniel Lezcano goto proc_tcp6_fail; 7846ab57e7eSDaniel Lezcano err = ac6_proc_init(net); 7856ab57e7eSDaniel Lezcano if (err) 7866ab57e7eSDaniel Lezcano goto proc_ac6_fail; 7870c96d8c5SDaniel Lezcano #endif 7880c96d8c5SDaniel Lezcano return err; 7896f8b13bcSDaniel Lezcano 7906f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS 7916ab57e7eSDaniel Lezcano proc_ac6_fail: 7926ab57e7eSDaniel Lezcano tcp6_proc_exit(net); 7936f8b13bcSDaniel Lezcano proc_tcp6_fail: 7946f8b13bcSDaniel Lezcano udp6_proc_exit(net); 795e43291cbSDenis V. Lunev out: 796e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net); 797e43291cbSDenis V. Lunev return err; 7986f8b13bcSDaniel Lezcano #endif 79981c1c178SDaniel Lezcano } 80081c1c178SDaniel Lezcano 8012c8c1e72SAlexey Dobriyan static void __net_exit inet6_net_exit(struct net *net) 80281c1c178SDaniel Lezcano { 8030c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS 8040c96d8c5SDaniel Lezcano udp6_proc_exit(net); 8056f8b13bcSDaniel Lezcano tcp6_proc_exit(net); 8066ab57e7eSDaniel Lezcano ac6_proc_exit(net); 8070c96d8c5SDaniel Lezcano #endif 808e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net); 80981c1c178SDaniel Lezcano } 81081c1c178SDaniel Lezcano 81181c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = { 81281c1c178SDaniel Lezcano .init = inet6_net_init, 81381c1c178SDaniel Lezcano .exit = inet6_net_exit, 81481c1c178SDaniel Lezcano }; 81581c1c178SDaniel Lezcano 8165f81bd2eSCong Wang static const struct ipv6_stub ipv6_stub_impl = { 8175f81bd2eSCong Wang .ipv6_sock_mc_join = ipv6_sock_mc_join, 8185f81bd2eSCong Wang .ipv6_sock_mc_drop = ipv6_sock_mc_drop, 8195f81bd2eSCong Wang .ipv6_dst_lookup = ip6_dst_lookup, 8205f81bd2eSCong Wang .udpv6_encap_enable = udpv6_encap_enable, 821f564f45cSCong Wang .ndisc_send_na = ndisc_send_na, 822e15a00aaSCong Wang .nd_tbl = &nd_tbl, 8235f81bd2eSCong Wang }; 8245f81bd2eSCong Wang 8251da177e4SLinus Torvalds static int __init inet6_init(void) 8261da177e4SLinus Torvalds { 8271da177e4SLinus Torvalds struct list_head *r; 828fe7ca2e1SBrian Haley int err = 0; 8291da177e4SLinus Torvalds 830b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct inet6_skb_parm)); 831ef047f5eSYOSHIFUJI Hideaki 832fe7ca2e1SBrian Haley /* Register the socket-side information for inet6_create. */ 833fe7ca2e1SBrian Haley for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r) 834fe7ca2e1SBrian Haley INIT_LIST_HEAD(r); 835fe7ca2e1SBrian Haley 83656d417b1SBrian Haley if (disable_ipv6_mod) { 837f3213831SJoe Perches pr_info("Loaded, but administratively disabled, reboot required to enable\n"); 838fe7ca2e1SBrian Haley goto out; 839fe7ca2e1SBrian Haley } 840fe7ca2e1SBrian Haley 8411da177e4SLinus Torvalds err = proto_register(&tcpv6_prot, 1); 8421da177e4SLinus Torvalds if (err) 8431da177e4SLinus Torvalds goto out; 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds err = proto_register(&udpv6_prot, 1); 8461da177e4SLinus Torvalds if (err) 8471da177e4SLinus Torvalds goto out_unregister_tcp_proto; 8481da177e4SLinus Torvalds 849ba4e58ecSGerrit Renker err = proto_register(&udplitev6_prot, 1); 8501da177e4SLinus Torvalds if (err) 8511da177e4SLinus Torvalds goto out_unregister_udp_proto; 8521da177e4SLinus Torvalds 853ba4e58ecSGerrit Renker err = proto_register(&rawv6_prot, 1); 854ba4e58ecSGerrit Renker if (err) 855ba4e58ecSGerrit Renker goto out_unregister_udplite_proto; 856ba4e58ecSGerrit Renker 8576d0bfe22SLorenzo Colitti err = proto_register(&pingv6_prot, 1); 8586d0bfe22SLorenzo Colitti if (err) 8596d0bfe22SLorenzo Colitti goto out_unregister_ping_proto; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds /* We MUST register RAW sockets before we create the ICMP6, 8621da177e4SLinus Torvalds * IGMP6, or NDISC control sockets. 8631da177e4SLinus Torvalds */ 8647f4e4868SDaniel Lezcano err = rawv6_init(); 8657f4e4868SDaniel Lezcano if (err) 8667f4e4868SDaniel Lezcano goto out_unregister_raw_proto; 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds /* Register the family here so that the init calls below will 8691da177e4SLinus Torvalds * be able to create sockets. (?? is this dangerous ??) 8701da177e4SLinus Torvalds */ 8718eb55910SDavid S. Miller err = sock_register(&inet6_family_ops); 8728eb55910SDavid S. Miller if (err) 8737f4e4868SDaniel Lezcano goto out_sock_register_fail; 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds /* 8761da177e4SLinus Torvalds * ipngwg API draft makes clear that the correct semantics 8771da177e4SLinus Torvalds * for TCP and UDP is to consider one TCP and UDP instance 87825985edcSLucas De Marchi * in a host available by both INET and INET6 APIs and 8791da177e4SLinus Torvalds * able to communicate via both network protocols. 8801da177e4SLinus Torvalds */ 8811da177e4SLinus Torvalds 88281c1c178SDaniel Lezcano err = register_pernet_subsys(&inet6_net_ops); 88381c1c178SDaniel Lezcano if (err) 88481c1c178SDaniel Lezcano goto register_pernet_fail; 8859b0f976fSDenis V. Lunev err = icmpv6_init(); 8861da177e4SLinus Torvalds if (err) 8871da177e4SLinus Torvalds goto icmp_fail; 888623d1a1aSWang Chen err = ip6_mr_init(); 889623d1a1aSWang Chen if (err) 890623d1a1aSWang Chen goto ipmr_fail; 8919b0f976fSDenis V. Lunev err = ndisc_init(); 8921da177e4SLinus Torvalds if (err) 8931da177e4SLinus Torvalds goto ndisc_fail; 8949b0f976fSDenis V. Lunev err = igmp6_init(); 8951da177e4SLinus Torvalds if (err) 8961da177e4SLinus Torvalds goto igmp_fail; 8975f81bd2eSCong Wang 8985f81bd2eSCong Wang ipv6_stub = &ipv6_stub_impl; 8995f81bd2eSCong Wang 9002cc7d573SHarald Welte err = ipv6_netfilter_init(); 9012cc7d573SHarald Welte if (err) 9022cc7d573SHarald Welte goto netfilter_fail; 9031da177e4SLinus Torvalds /* Create /proc/foo6 entries. */ 9041da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 9051da177e4SLinus Torvalds err = -ENOMEM; 9061da177e4SLinus Torvalds if (raw6_proc_init()) 9071da177e4SLinus Torvalds goto proc_raw6_fail; 908ba4e58ecSGerrit Renker if (udplite6_proc_init()) 909ba4e58ecSGerrit Renker goto proc_udplite6_fail; 9101da177e4SLinus Torvalds if (ipv6_misc_proc_init()) 9111da177e4SLinus Torvalds goto proc_misc6_fail; 9121da177e4SLinus Torvalds if (if6_proc_init()) 9131da177e4SLinus Torvalds goto proc_if6_fail; 9141da177e4SLinus Torvalds #endif 915e2fddf5eSDaniel Lezcano err = ip6_route_init(); 916e2fddf5eSDaniel Lezcano if (err) 917e2fddf5eSDaniel Lezcano goto ip6_route_fail; 9182c861cc6SMichal Kubeček err = ndisc_late_init(); 9192c861cc6SMichal Kubeček if (err) 9202c861cc6SMichal Kubeček goto ndisc_late_fail; 9210a3e78acSDaniel Lezcano err = ip6_flowlabel_init(); 9220a3e78acSDaniel Lezcano if (err) 9230a3e78acSDaniel Lezcano goto ip6_flowlabel_fail; 9241da177e4SLinus Torvalds err = addrconf_init(); 9251da177e4SLinus Torvalds if (err) 9261da177e4SLinus Torvalds goto addrconf_fail; 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds /* Init v6 extension headers. */ 929248b238dSDaniel Lezcano err = ipv6_exthdrs_init(); 930248b238dSDaniel Lezcano if (err) 931248b238dSDaniel Lezcano goto ipv6_exthdrs_fail; 932248b238dSDaniel Lezcano 933853cbbaaSDaniel Lezcano err = ipv6_frag_init(); 934853cbbaaSDaniel Lezcano if (err) 935853cbbaaSDaniel Lezcano goto ipv6_frag_fail; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds /* Init v6 transport protocols. */ 9387f4e4868SDaniel Lezcano err = udpv6_init(); 9397f4e4868SDaniel Lezcano if (err) 9407f4e4868SDaniel Lezcano goto udpv6_fail; 941e2ed4052SHerbert Xu 9427f4e4868SDaniel Lezcano err = udplitev6_init(); 9437f4e4868SDaniel Lezcano if (err) 9447f4e4868SDaniel Lezcano goto udplitev6_fail; 9457f4e4868SDaniel Lezcano 9467f4e4868SDaniel Lezcano err = tcpv6_init(); 9477f4e4868SDaniel Lezcano if (err) 9487f4e4868SDaniel Lezcano goto tcpv6_fail; 9497f4e4868SDaniel Lezcano 9507f4e4868SDaniel Lezcano err = ipv6_packet_init(); 9517f4e4868SDaniel Lezcano if (err) 9527f4e4868SDaniel Lezcano goto ipv6_packet_fail; 95394911fe3SBenjamin Thery 9546d0bfe22SLorenzo Colitti err = pingv6_init(); 9556d0bfe22SLorenzo Colitti if (err) 9566d0bfe22SLorenzo Colitti goto pingv6_fail; 9576d0bfe22SLorenzo Colitti 95894911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL 95994911fe3SBenjamin Thery err = ipv6_sysctl_register(); 96094911fe3SBenjamin Thery if (err) 96194911fe3SBenjamin Thery goto sysctl_fail; 96294911fe3SBenjamin Thery #endif 9631da177e4SLinus Torvalds out: 9641da177e4SLinus Torvalds return err; 9651da177e4SLinus Torvalds 96694911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL 96794911fe3SBenjamin Thery sysctl_fail: 968eca42aafSVlad Yasevich pingv6_exit(); 96994911fe3SBenjamin Thery #endif 9706d0bfe22SLorenzo Colitti pingv6_fail: 971eca42aafSVlad Yasevich ipv6_packet_cleanup(); 9727f4e4868SDaniel Lezcano ipv6_packet_fail: 9737f4e4868SDaniel Lezcano tcpv6_exit(); 9747f4e4868SDaniel Lezcano tcpv6_fail: 9757f4e4868SDaniel Lezcano udplitev6_exit(); 9767f4e4868SDaniel Lezcano udplitev6_fail: 9777f4e4868SDaniel Lezcano udpv6_exit(); 9787f4e4868SDaniel Lezcano udpv6_fail: 9797f4e4868SDaniel Lezcano ipv6_frag_exit(); 980853cbbaaSDaniel Lezcano ipv6_frag_fail: 981853cbbaaSDaniel Lezcano ipv6_exthdrs_exit(); 982248b238dSDaniel Lezcano ipv6_exthdrs_fail: 983248b238dSDaniel Lezcano addrconf_cleanup(); 9841da177e4SLinus Torvalds addrconf_fail: 9851da177e4SLinus Torvalds ip6_flowlabel_cleanup(); 9860a3e78acSDaniel Lezcano ip6_flowlabel_fail: 9872c861cc6SMichal Kubeček ndisc_late_cleanup(); 9882c861cc6SMichal Kubeček ndisc_late_fail: 9891da177e4SLinus Torvalds ip6_route_cleanup(); 990e2fddf5eSDaniel Lezcano ip6_route_fail: 9911da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 9921da177e4SLinus Torvalds if6_proc_exit(); 9931da177e4SLinus Torvalds proc_if6_fail: 9941da177e4SLinus Torvalds ipv6_misc_proc_exit(); 9951da177e4SLinus Torvalds proc_misc6_fail: 996ba4e58ecSGerrit Renker udplite6_proc_exit(); 997ba4e58ecSGerrit Renker proc_udplite6_fail: 9981da177e4SLinus Torvalds raw6_proc_exit(); 9991da177e4SLinus Torvalds proc_raw6_fail: 10001da177e4SLinus Torvalds #endif 10012cc7d573SHarald Welte ipv6_netfilter_fini(); 10022cc7d573SHarald Welte netfilter_fail: 10031da177e4SLinus Torvalds igmp6_cleanup(); 10041da177e4SLinus Torvalds igmp_fail: 10051da177e4SLinus Torvalds ndisc_cleanup(); 10061da177e4SLinus Torvalds ndisc_fail: 1007623d1a1aSWang Chen ip6_mr_cleanup(); 1008623d1a1aSWang Chen ipmr_fail: 10091da177e4SLinus Torvalds icmpv6_cleanup(); 10101da177e4SLinus Torvalds icmp_fail: 101181c1c178SDaniel Lezcano unregister_pernet_subsys(&inet6_net_ops); 101281c1c178SDaniel Lezcano register_pernet_fail: 10138eb55910SDavid S. Miller sock_unregister(PF_INET6); 1014e2fddf5eSDaniel Lezcano rtnl_unregister_all(PF_INET6); 10157f4e4868SDaniel Lezcano out_sock_register_fail: 10167f4e4868SDaniel Lezcano rawv6_exit(); 10176d0bfe22SLorenzo Colitti out_unregister_ping_proto: 10186d0bfe22SLorenzo Colitti proto_unregister(&pingv6_prot); 10191da177e4SLinus Torvalds out_unregister_raw_proto: 10201da177e4SLinus Torvalds proto_unregister(&rawv6_prot); 1021ba4e58ecSGerrit Renker out_unregister_udplite_proto: 1022ba4e58ecSGerrit Renker proto_unregister(&udplitev6_prot); 10231da177e4SLinus Torvalds out_unregister_udp_proto: 10241da177e4SLinus Torvalds proto_unregister(&udpv6_prot); 10251da177e4SLinus Torvalds out_unregister_tcp_proto: 10261da177e4SLinus Torvalds proto_unregister(&tcpv6_prot); 10271da177e4SLinus Torvalds goto out; 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds module_init(inet6_init); 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6); 1032