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. */ 36590c337daSEric Dumazet if ((snum || !inet->bind_address_no_port) && 36690c337daSEric Dumazet sk->sk_prot->get_port(sk, snum)) { 3671da177e4SLinus Torvalds inet_reset_saddr(sk); 3681da177e4SLinus Torvalds err = -EADDRINUSE; 3691da177e4SLinus Torvalds goto out; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3720f8d3c7aSVlad Yasevich if (addr_type != IPV6_ADDR_ANY) { 3731da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDADDR_LOCK; 3740f8d3c7aSVlad Yasevich if (addr_type != IPV6_ADDR_MAPPED) 3759fe516baSEric Dumazet sk->sk_ipv6only = 1; 3760f8d3c7aSVlad Yasevich } 3771da177e4SLinus Torvalds if (snum) 3781da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 379c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 380c720c7e8SEric Dumazet inet->inet_dport = 0; 381c720c7e8SEric Dumazet inet->inet_daddr = 0; 3821da177e4SLinus Torvalds out: 3831da177e4SLinus Torvalds release_sock(sk); 3841da177e4SLinus Torvalds return err; 38516ba5e8eSEric Dumazet out_unlock: 38616ba5e8eSEric Dumazet rcu_read_unlock(); 38716ba5e8eSEric Dumazet goto out; 3881da177e4SLinus Torvalds } 3897159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_bind); 3907159039aSYOSHIFUJI Hideaki 3911da177e4SLinus Torvalds int inet6_release(struct socket *sock) 3921da177e4SLinus Torvalds { 3931da177e4SLinus Torvalds struct sock *sk = sock->sk; 3941da177e4SLinus Torvalds 39563159f29SIan Morris if (!sk) 3961da177e4SLinus Torvalds return -EINVAL; 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds /* Free mc lists */ 3991da177e4SLinus Torvalds ipv6_sock_mc_close(sk); 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds /* Free ac lists */ 4021da177e4SLinus Torvalds ipv6_sock_ac_close(sk); 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds return inet_release(sock); 4051da177e4SLinus Torvalds } 4067159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release); 4077159039aSYOSHIFUJI Hideaki 4087d06b2e0SBrian Haley void inet6_destroy_sock(struct sock *sk) 4091da177e4SLinus Torvalds { 4101da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4111da177e4SLinus Torvalds struct sk_buff *skb; 4121da177e4SLinus Torvalds struct ipv6_txoptions *opt; 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds /* Release rx options */ 4151da177e4SLinus Torvalds 416647c0c70SEldad Zack skb = xchg(&np->pktoptions, NULL); 41753b24b8fSIan Morris if (skb) 4181da177e4SLinus Torvalds kfree_skb(skb); 4191da177e4SLinus Torvalds 420647c0c70SEldad Zack skb = xchg(&np->rxpmtu, NULL); 42153b24b8fSIan Morris if (skb) 4224b340ae2SBrian Haley kfree_skb(skb); 4234b340ae2SBrian Haley 4241da177e4SLinus Torvalds /* Free flowlabels */ 4251da177e4SLinus Torvalds fl6_free_socklist(sk); 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds /* Free tx options */ 4281da177e4SLinus Torvalds 429647c0c70SEldad Zack opt = xchg(&np->opt, NULL); 43053b24b8fSIan Morris if (opt) 4311da177e4SLinus Torvalds sock_kfree_s(sk, opt, opt->tot_len); 4321da177e4SLinus Torvalds } 4333cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_destroy_sock); 4343cf3dc6cSArnaldo Carvalho de Melo 4351da177e4SLinus Torvalds /* 4361da177e4SLinus Torvalds * This does both peername and sockname. 4371da177e4SLinus Torvalds */ 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr, 4401da177e4SLinus Torvalds int *uaddr_len, int peer) 4411da177e4SLinus Torvalds { 4421da177e4SLinus Torvalds struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr; 4431da177e4SLinus Torvalds struct sock *sk = sock->sk; 4441da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 4451da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds sin->sin6_family = AF_INET6; 4481da177e4SLinus Torvalds sin->sin6_flowinfo = 0; 4491da177e4SLinus Torvalds sin->sin6_scope_id = 0; 4501da177e4SLinus Torvalds if (peer) { 451c720c7e8SEric Dumazet if (!inet->inet_dport) 4521da177e4SLinus Torvalds return -ENOTCONN; 4531da177e4SLinus Torvalds if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && 4541da177e4SLinus Torvalds peer == 1) 4551da177e4SLinus Torvalds return -ENOTCONN; 456c720c7e8SEric Dumazet sin->sin6_port = inet->inet_dport; 457efe4208fSEric Dumazet sin->sin6_addr = sk->sk_v6_daddr; 4581da177e4SLinus Torvalds if (np->sndflow) 4591da177e4SLinus Torvalds sin->sin6_flowinfo = np->flow_label; 4601da177e4SLinus Torvalds } else { 461efe4208fSEric Dumazet if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) 4624e3fd7a0SAlexey Dobriyan sin->sin6_addr = np->saddr; 4631da177e4SLinus Torvalds else 464efe4208fSEric Dumazet sin->sin6_addr = sk->sk_v6_rcv_saddr; 4651da177e4SLinus Torvalds 466c720c7e8SEric Dumazet sin->sin6_port = inet->inet_sport; 4671da177e4SLinus Torvalds } 468842df073SHannes Frederic Sowa sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr, 469842df073SHannes Frederic Sowa sk->sk_bound_dev_if); 4701da177e4SLinus Torvalds *uaddr_len = sizeof(*sin); 471a02cec21SEric Dumazet return 0; 4721da177e4SLinus Torvalds } 4737159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname); 4747159039aSYOSHIFUJI Hideaki 4751da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 4761da177e4SLinus Torvalds { 4771da177e4SLinus Torvalds struct sock *sk = sock->sk; 4783b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 4791da177e4SLinus Torvalds 480647c0c70SEldad Zack switch (cmd) { 4811da177e4SLinus Torvalds case SIOCGSTAMP: 4821da177e4SLinus Torvalds return sock_get_timestamp(sk, (struct timeval __user *)arg); 4831da177e4SLinus Torvalds 484ae40eb1eSEric Dumazet case SIOCGSTAMPNS: 485ae40eb1eSEric Dumazet return sock_get_timestampns(sk, (struct timespec __user *)arg); 486ae40eb1eSEric Dumazet 4871da177e4SLinus Torvalds case SIOCADDRT: 4881da177e4SLinus Torvalds case SIOCDELRT: 4891da177e4SLinus Torvalds 490a02cec21SEric Dumazet return ipv6_route_ioctl(net, cmd, (void __user *)arg); 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds case SIOCSIFADDR: 493af284937SDaniel Lezcano return addrconf_add_ifaddr(net, (void __user *) arg); 4941da177e4SLinus Torvalds case SIOCDIFADDR: 495af284937SDaniel Lezcano return addrconf_del_ifaddr(net, (void __user *) arg); 4961da177e4SLinus Torvalds case SIOCSIFDSTADDR: 497af284937SDaniel Lezcano return addrconf_set_dstaddr(net, (void __user *) arg); 4981da177e4SLinus Torvalds default: 499b5e5fa5eSChristoph Hellwig if (!sk->sk_prot->ioctl) 500b5e5fa5eSChristoph Hellwig return -ENOIOCTLCMD; 501b5e5fa5eSChristoph Hellwig return sk->sk_prot->ioctl(sk, cmd, arg); 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds /*NOTREACHED*/ 504a02cec21SEric Dumazet return 0; 5051da177e4SLinus Torvalds } 5067159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl); 5077159039aSYOSHIFUJI Hideaki 50890ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = { 5091da177e4SLinus Torvalds .family = PF_INET6, 5101da177e4SLinus Torvalds .owner = THIS_MODULE, 5111da177e4SLinus Torvalds .release = inet6_release, 5121da177e4SLinus Torvalds .bind = inet6_bind, 5131da177e4SLinus Torvalds .connect = inet_stream_connect, /* ok */ 5141da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */ 5151da177e4SLinus Torvalds .accept = inet_accept, /* ok */ 5161da177e4SLinus Torvalds .getname = inet6_getname, 5171da177e4SLinus Torvalds .poll = tcp_poll, /* ok */ 5181da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */ 5191da177e4SLinus Torvalds .listen = inet_listen, /* ok */ 5201da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */ 5211da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */ 5221da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */ 5237ba42910SChangli Gao .sendmsg = inet_sendmsg, /* ok */ 5247ba42910SChangli Gao .recvmsg = inet_recvmsg, /* ok */ 525543d9cfeSArnaldo Carvalho de Melo .mmap = sock_no_mmap, 5267ba42910SChangli Gao .sendpage = inet_sendpage, 527a0974dd3SJens Axboe .splice_read = tcp_splice_read, 5283fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 5293fdadf7dSDmitry Mishin .compat_setsockopt = compat_sock_common_setsockopt, 5303fdadf7dSDmitry Mishin .compat_getsockopt = compat_sock_common_getsockopt, 5313fdadf7dSDmitry Mishin #endif 5321da177e4SLinus Torvalds }; 5331da177e4SLinus Torvalds 53490ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = { 5351da177e4SLinus Torvalds .family = PF_INET6, 5361da177e4SLinus Torvalds .owner = THIS_MODULE, 5371da177e4SLinus Torvalds .release = inet6_release, 5381da177e4SLinus Torvalds .bind = inet6_bind, 5391da177e4SLinus Torvalds .connect = inet_dgram_connect, /* ok */ 5401da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */ 5411da177e4SLinus Torvalds .accept = sock_no_accept, /* a do nothing */ 5421da177e4SLinus Torvalds .getname = inet6_getname, 5431da177e4SLinus Torvalds .poll = udp_poll, /* ok */ 5441da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */ 5451da177e4SLinus Torvalds .listen = sock_no_listen, /* ok */ 5461da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */ 5471da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */ 5481da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */ 5491da177e4SLinus Torvalds .sendmsg = inet_sendmsg, /* ok */ 5507ba42910SChangli Gao .recvmsg = inet_recvmsg, /* ok */ 5511da177e4SLinus Torvalds .mmap = sock_no_mmap, 5521da177e4SLinus Torvalds .sendpage = sock_no_sendpage, 553543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 554543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_sock_common_setsockopt, 555543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_sock_common_getsockopt, 556543d9cfeSArnaldo Carvalho de Melo #endif 5571da177e4SLinus Torvalds }; 5581da177e4SLinus Torvalds 559ec1b4cf7SStephen Hemminger static const struct net_proto_family inet6_family_ops = { 5601da177e4SLinus Torvalds .family = PF_INET6, 5611da177e4SLinus Torvalds .create = inet6_create, 5621da177e4SLinus Torvalds .owner = THIS_MODULE, 5631da177e4SLinus Torvalds }; 5641da177e4SLinus Torvalds 56587c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p) 5661da177e4SLinus Torvalds { 5671da177e4SLinus Torvalds struct list_head *lh; 5681da177e4SLinus Torvalds struct inet_protosw *answer; 5691da177e4SLinus Torvalds struct list_head *last_perm; 57087c3efbfSDaniel Lezcano int protocol = p->protocol; 57187c3efbfSDaniel Lezcano int ret; 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock); 5741da177e4SLinus Torvalds 57587c3efbfSDaniel Lezcano ret = -EINVAL; 5761da177e4SLinus Torvalds if (p->type >= SOCK_MAX) 5771da177e4SLinus Torvalds goto out_illegal; 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds /* If we are trying to override a permanent protocol, bail. */ 5801da177e4SLinus Torvalds answer = NULL; 58187c3efbfSDaniel Lezcano ret = -EPERM; 5821da177e4SLinus Torvalds last_perm = &inetsw6[p->type]; 5831da177e4SLinus Torvalds list_for_each(lh, &inetsw6[p->type]) { 5841da177e4SLinus Torvalds answer = list_entry(lh, struct inet_protosw, list); 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds /* Check only the non-wild match. */ 5871da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & answer->flags) { 5881da177e4SLinus Torvalds if (protocol == answer->protocol) 5891da177e4SLinus Torvalds break; 5901da177e4SLinus Torvalds last_perm = lh; 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds answer = NULL; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds if (answer) 5961da177e4SLinus Torvalds goto out_permanent; 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds /* Add the new entry after the last permanent entry if any, so that 5991da177e4SLinus Torvalds * the new entry does not override a permanent entry when matched with 6001da177e4SLinus Torvalds * a wild-card protocol. But it is allowed to override any existing 6011da177e4SLinus Torvalds * non-permanent entry. This means that when we remove this entry, the 6021da177e4SLinus Torvalds * system automatically returns to the old behavior. 6031da177e4SLinus Torvalds */ 6041da177e4SLinus Torvalds list_add_rcu(&p->list, last_perm); 60587c3efbfSDaniel Lezcano ret = 0; 6061da177e4SLinus Torvalds out: 6071da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock); 60887c3efbfSDaniel Lezcano return ret; 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds out_permanent: 611f3213831SJoe Perches pr_err("Attempt to override permanent protocol %d\n", protocol); 6121da177e4SLinus Torvalds goto out; 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds out_illegal: 615f3213831SJoe Perches pr_err("Ignoring attempt to register invalid socket type %d\n", 6161da177e4SLinus Torvalds p->type); 6171da177e4SLinus Torvalds goto out; 6181da177e4SLinus Torvalds } 6197159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw); 6207159039aSYOSHIFUJI Hideaki 6211da177e4SLinus Torvalds void 6221da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p) 6231da177e4SLinus Torvalds { 6241da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & p->flags) { 625f3213831SJoe Perches pr_err("Attempt to unregister permanent protocol %d\n", 6261da177e4SLinus Torvalds p->protocol); 6271da177e4SLinus Torvalds } else { 6281da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock); 6291da177e4SLinus Torvalds list_del_rcu(&p->list); 6301da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock); 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds synchronize_net(); 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds } 6357159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw); 6367159039aSYOSHIFUJI Hideaki 637b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk) 638b9750ce1SArnaldo Carvalho de Melo { 639b9750ce1SArnaldo Carvalho de Melo struct ipv6_pinfo *np = inet6_sk(sk); 64068d0c6d3SDavid S. Miller struct dst_entry *dst; 641b9750ce1SArnaldo Carvalho de Melo 642b9750ce1SArnaldo Carvalho de Melo dst = __sk_dst_check(sk, np->dst_cookie); 643b9750ce1SArnaldo Carvalho de Melo 64463159f29SIan Morris if (!dst) { 645b9750ce1SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 64620c59de2SArnaud Ebalard struct in6_addr *final_p, final; 6474c9483b2SDavid S. Miller struct flowi6 fl6; 648b9750ce1SArnaldo Carvalho de Melo 6494c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 6504c9483b2SDavid S. Miller fl6.flowi6_proto = sk->sk_protocol; 651efe4208fSEric Dumazet fl6.daddr = sk->sk_v6_daddr; 6524e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 6534c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 6544c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 6554c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark; 6561958b856SDavid S. Miller fl6.fl6_dport = inet->inet_dport; 6571958b856SDavid S. Miller fl6.fl6_sport = inet->inet_sport; 6584c9483b2SDavid S. Miller security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); 659b9750ce1SArnaldo Carvalho de Melo 6604c9483b2SDavid S. Miller final_p = fl6_update_dst(&fl6, np->opt, &final); 661b9750ce1SArnaldo Carvalho de Melo 6620e0d44abSSteffen Klassert dst = ip6_dst_lookup_flow(sk, &fl6, final_p); 66368d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 664b9750ce1SArnaldo Carvalho de Melo sk->sk_route_caps = 0; 66568d0c6d3SDavid S. Miller sk->sk_err_soft = -PTR_ERR(dst); 66668d0c6d3SDavid S. Miller return PTR_ERR(dst); 667b9750ce1SArnaldo Carvalho de Melo } 668b9750ce1SArnaldo Carvalho de Melo 6698e1ef0a9SYOSHIFUJI Hideaki __ip6_dst_store(sk, dst, NULL, NULL); 670b9750ce1SArnaldo Carvalho de Melo } 671b9750ce1SArnaldo Carvalho de Melo 672b9750ce1SArnaldo Carvalho de Melo return 0; 673b9750ce1SArnaldo Carvalho de Melo } 674b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header); 675b9750ce1SArnaldo Carvalho de Melo 676a224772dSEric Dumazet bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb, 677a224772dSEric Dumazet const struct inet6_skb_parm *opt) 678399c07deSArnaldo Carvalho de Melo { 67992113bfdSEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk); 680399c07deSArnaldo Carvalho de Melo 681399c07deSArnaldo Carvalho de Melo if (np->rxopt.all) { 682399c07deSArnaldo Carvalho de Melo if ((opt->hop && (np->rxopt.bits.hopopts || 683399c07deSArnaldo Carvalho de Melo np->rxopt.bits.ohopopts)) || 684ac1eabcaSFlorent Fourcot (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) && 685399c07deSArnaldo Carvalho de Melo np->rxopt.bits.rxflow) || 686399c07deSArnaldo Carvalho de Melo (opt->srcrt && (np->rxopt.bits.srcrt || 687399c07deSArnaldo Carvalho de Melo np->rxopt.bits.osrcrt)) || 688399c07deSArnaldo Carvalho de Melo ((opt->dst1 || opt->dst0) && 689399c07deSArnaldo Carvalho de Melo (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts))) 69092113bfdSEric Dumazet return true; 691399c07deSArnaldo Carvalho de Melo } 69292113bfdSEric Dumazet return false; 693399c07deSArnaldo Carvalho de Melo } 694399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted); 695399c07deSArnaldo Carvalho de Melo 6967546dd97SStephen Hemminger static struct packet_type ipv6_packet_type __read_mostly = { 69709640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_IPV6), 698662397fdSYOSHIFUJI Hideaki .func = ipv6_rcv, 69922061d80SVlad Yasevich }; 70022061d80SVlad Yasevich 701662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void) 702662397fdSYOSHIFUJI Hideaki { 703662397fdSYOSHIFUJI Hideaki dev_add_pack(&ipv6_packet_type); 704662397fdSYOSHIFUJI Hideaki return 0; 705662397fdSYOSHIFUJI Hideaki } 706662397fdSYOSHIFUJI Hideaki 707662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void) 708662397fdSYOSHIFUJI Hideaki { 709662397fdSYOSHIFUJI Hideaki dev_remove_pack(&ipv6_packet_type); 710662397fdSYOSHIFUJI Hideaki } 711662397fdSYOSHIFUJI Hideaki 712e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net) 713e43291cbSDenis V. Lunev { 714827da44cSJohn Stultz int i; 715827da44cSJohn Stultz 716698365faSWANG Cong net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib); 717698365faSWANG Cong if (!net->mib.udp_stats_in6) 7180c7ed677SDenis V. Lunev return -ENOMEM; 719698365faSWANG Cong net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib); 720698365faSWANG Cong if (!net->mib.udplite_stats_in6) 721be713a44SDenis V. Lunev goto err_udplite_mib; 722698365faSWANG Cong net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib); 723698365faSWANG Cong if (!net->mib.ipv6_statistics) 7249261e537SDenis V. Lunev goto err_ip_mib; 725827da44cSJohn Stultz 726827da44cSJohn Stultz for_each_possible_cpu(i) { 727827da44cSJohn Stultz struct ipstats_mib *af_inet6_stats; 728698365faSWANG Cong af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i); 729827da44cSJohn Stultz u64_stats_init(&af_inet6_stats->syncp); 730827da44cSJohn Stultz } 731827da44cSJohn Stultz 732827da44cSJohn Stultz 733698365faSWANG Cong net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib); 734698365faSWANG Cong if (!net->mib.icmpv6_statistics) 7359261e537SDenis V. Lunev goto err_icmp_mib; 7362a24444fSEric Dumazet net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib), 7372a24444fSEric Dumazet GFP_KERNEL); 7382a24444fSEric Dumazet if (!net->mib.icmpv6msg_statistics) 7399261e537SDenis V. Lunev goto err_icmpmsg_mib; 740e43291cbSDenis V. Lunev return 0; 741be713a44SDenis V. Lunev 7429261e537SDenis V. Lunev err_icmpmsg_mib: 743698365faSWANG Cong free_percpu(net->mib.icmpv6_statistics); 7449261e537SDenis V. Lunev err_icmp_mib: 745698365faSWANG Cong free_percpu(net->mib.ipv6_statistics); 7469261e537SDenis V. Lunev err_ip_mib: 747698365faSWANG Cong free_percpu(net->mib.udplite_stats_in6); 748be713a44SDenis V. Lunev err_udplite_mib: 749698365faSWANG Cong free_percpu(net->mib.udp_stats_in6); 750be713a44SDenis V. Lunev return -ENOMEM; 751e43291cbSDenis V. Lunev } 752e43291cbSDenis V. Lunev 7532c8c1e72SAlexey Dobriyan static void ipv6_cleanup_mibs(struct net *net) 754e43291cbSDenis V. Lunev { 755698365faSWANG Cong free_percpu(net->mib.udp_stats_in6); 756698365faSWANG Cong free_percpu(net->mib.udplite_stats_in6); 757698365faSWANG Cong free_percpu(net->mib.ipv6_statistics); 758698365faSWANG Cong free_percpu(net->mib.icmpv6_statistics); 7592a24444fSEric Dumazet kfree(net->mib.icmpv6msg_statistics); 760e43291cbSDenis V. Lunev } 761e43291cbSDenis V. Lunev 762e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net) 76381c1c178SDaniel Lezcano { 7640c96d8c5SDaniel Lezcano int err = 0; 7650c96d8c5SDaniel Lezcano 76699bc9c4eSDaniel Lezcano net->ipv6.sysctl.bindv6only = 0; 76741a76906SDaniel Lezcano net->ipv6.sysctl.icmpv6_time = 1*HZ; 7686444f72bSFlorent Fourcot net->ipv6.sysctl.flowlabel_consistency = 1; 769cb1ce2efSTom Herbert net->ipv6.sysctl.auto_flowlabels = 0; 7701855b7c3SHannes Frederic Sowa net->ipv6.sysctl.idgen_retries = 3; 7711855b7c3SHannes Frederic Sowa net->ipv6.sysctl.idgen_delay = 1 * HZ; 77282a584b7STom Herbert net->ipv6.sysctl.flowlabel_state_ranges = 1; 773812918c4SHannes Frederic Sowa atomic_set(&net->ipv6.fib6_sernum, 1); 774e71e0349SDaniel Lezcano 775e43291cbSDenis V. Lunev err = ipv6_init_mibs(net); 776e43291cbSDenis V. Lunev if (err) 777e43291cbSDenis V. Lunev return err; 7780c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS 7790c96d8c5SDaniel Lezcano err = udp6_proc_init(net); 7800c96d8c5SDaniel Lezcano if (err) 7810c96d8c5SDaniel Lezcano goto out; 7826f8b13bcSDaniel Lezcano err = tcp6_proc_init(net); 7836f8b13bcSDaniel Lezcano if (err) 7846f8b13bcSDaniel Lezcano goto proc_tcp6_fail; 7856ab57e7eSDaniel Lezcano err = ac6_proc_init(net); 7866ab57e7eSDaniel Lezcano if (err) 7876ab57e7eSDaniel Lezcano goto proc_ac6_fail; 7880c96d8c5SDaniel Lezcano #endif 7890c96d8c5SDaniel Lezcano return err; 7906f8b13bcSDaniel Lezcano 7916f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS 7926ab57e7eSDaniel Lezcano proc_ac6_fail: 7936ab57e7eSDaniel Lezcano tcp6_proc_exit(net); 7946f8b13bcSDaniel Lezcano proc_tcp6_fail: 7956f8b13bcSDaniel Lezcano udp6_proc_exit(net); 796e43291cbSDenis V. Lunev out: 797e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net); 798e43291cbSDenis V. Lunev return err; 7996f8b13bcSDaniel Lezcano #endif 80081c1c178SDaniel Lezcano } 80181c1c178SDaniel Lezcano 8022c8c1e72SAlexey Dobriyan static void __net_exit inet6_net_exit(struct net *net) 80381c1c178SDaniel Lezcano { 8040c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS 8050c96d8c5SDaniel Lezcano udp6_proc_exit(net); 8066f8b13bcSDaniel Lezcano tcp6_proc_exit(net); 8076ab57e7eSDaniel Lezcano ac6_proc_exit(net); 8080c96d8c5SDaniel Lezcano #endif 809e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net); 81081c1c178SDaniel Lezcano } 81181c1c178SDaniel Lezcano 81281c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = { 81381c1c178SDaniel Lezcano .init = inet6_net_init, 81481c1c178SDaniel Lezcano .exit = inet6_net_exit, 81581c1c178SDaniel Lezcano }; 81681c1c178SDaniel Lezcano 8175f81bd2eSCong Wang static const struct ipv6_stub ipv6_stub_impl = { 8185f81bd2eSCong Wang .ipv6_sock_mc_join = ipv6_sock_mc_join, 8195f81bd2eSCong Wang .ipv6_sock_mc_drop = ipv6_sock_mc_drop, 8205f81bd2eSCong Wang .ipv6_dst_lookup = ip6_dst_lookup, 8215f81bd2eSCong Wang .udpv6_encap_enable = udpv6_encap_enable, 822f564f45cSCong Wang .ndisc_send_na = ndisc_send_na, 823e15a00aaSCong Wang .nd_tbl = &nd_tbl, 8245f81bd2eSCong Wang }; 8255f81bd2eSCong Wang 8261da177e4SLinus Torvalds static int __init inet6_init(void) 8271da177e4SLinus Torvalds { 8281da177e4SLinus Torvalds struct list_head *r; 829fe7ca2e1SBrian Haley int err = 0; 8301da177e4SLinus Torvalds 831b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct inet6_skb_parm)); 832ef047f5eSYOSHIFUJI Hideaki 833fe7ca2e1SBrian Haley /* Register the socket-side information for inet6_create. */ 834fe7ca2e1SBrian Haley for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r) 835fe7ca2e1SBrian Haley INIT_LIST_HEAD(r); 836fe7ca2e1SBrian Haley 83756d417b1SBrian Haley if (disable_ipv6_mod) { 838f3213831SJoe Perches pr_info("Loaded, but administratively disabled, reboot required to enable\n"); 839fe7ca2e1SBrian Haley goto out; 840fe7ca2e1SBrian Haley } 841fe7ca2e1SBrian Haley 8421da177e4SLinus Torvalds err = proto_register(&tcpv6_prot, 1); 8431da177e4SLinus Torvalds if (err) 8441da177e4SLinus Torvalds goto out; 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds err = proto_register(&udpv6_prot, 1); 8471da177e4SLinus Torvalds if (err) 8481da177e4SLinus Torvalds goto out_unregister_tcp_proto; 8491da177e4SLinus Torvalds 850ba4e58ecSGerrit Renker err = proto_register(&udplitev6_prot, 1); 8511da177e4SLinus Torvalds if (err) 8521da177e4SLinus Torvalds goto out_unregister_udp_proto; 8531da177e4SLinus Torvalds 854ba4e58ecSGerrit Renker err = proto_register(&rawv6_prot, 1); 855ba4e58ecSGerrit Renker if (err) 856ba4e58ecSGerrit Renker goto out_unregister_udplite_proto; 857ba4e58ecSGerrit Renker 8586d0bfe22SLorenzo Colitti err = proto_register(&pingv6_prot, 1); 8596d0bfe22SLorenzo Colitti if (err) 8606d0bfe22SLorenzo Colitti goto out_unregister_ping_proto; 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds /* We MUST register RAW sockets before we create the ICMP6, 8631da177e4SLinus Torvalds * IGMP6, or NDISC control sockets. 8641da177e4SLinus Torvalds */ 8657f4e4868SDaniel Lezcano err = rawv6_init(); 8667f4e4868SDaniel Lezcano if (err) 8677f4e4868SDaniel Lezcano goto out_unregister_raw_proto; 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds /* Register the family here so that the init calls below will 8701da177e4SLinus Torvalds * be able to create sockets. (?? is this dangerous ??) 8711da177e4SLinus Torvalds */ 8728eb55910SDavid S. Miller err = sock_register(&inet6_family_ops); 8738eb55910SDavid S. Miller if (err) 8747f4e4868SDaniel Lezcano goto out_sock_register_fail; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds /* 8771da177e4SLinus Torvalds * ipngwg API draft makes clear that the correct semantics 8781da177e4SLinus Torvalds * for TCP and UDP is to consider one TCP and UDP instance 87925985edcSLucas De Marchi * in a host available by both INET and INET6 APIs and 8801da177e4SLinus Torvalds * able to communicate via both network protocols. 8811da177e4SLinus Torvalds */ 8821da177e4SLinus Torvalds 88381c1c178SDaniel Lezcano err = register_pernet_subsys(&inet6_net_ops); 88481c1c178SDaniel Lezcano if (err) 88581c1c178SDaniel Lezcano goto register_pernet_fail; 8869b0f976fSDenis V. Lunev err = icmpv6_init(); 8871da177e4SLinus Torvalds if (err) 8881da177e4SLinus Torvalds goto icmp_fail; 889623d1a1aSWang Chen err = ip6_mr_init(); 890623d1a1aSWang Chen if (err) 891623d1a1aSWang Chen goto ipmr_fail; 8929b0f976fSDenis V. Lunev err = ndisc_init(); 8931da177e4SLinus Torvalds if (err) 8941da177e4SLinus Torvalds goto ndisc_fail; 8959b0f976fSDenis V. Lunev err = igmp6_init(); 8961da177e4SLinus Torvalds if (err) 8971da177e4SLinus Torvalds goto igmp_fail; 8985f81bd2eSCong Wang 8995f81bd2eSCong Wang ipv6_stub = &ipv6_stub_impl; 9005f81bd2eSCong Wang 9012cc7d573SHarald Welte err = ipv6_netfilter_init(); 9022cc7d573SHarald Welte if (err) 9032cc7d573SHarald Welte goto netfilter_fail; 9041da177e4SLinus Torvalds /* Create /proc/foo6 entries. */ 9051da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 9061da177e4SLinus Torvalds err = -ENOMEM; 9071da177e4SLinus Torvalds if (raw6_proc_init()) 9081da177e4SLinus Torvalds goto proc_raw6_fail; 909ba4e58ecSGerrit Renker if (udplite6_proc_init()) 910ba4e58ecSGerrit Renker goto proc_udplite6_fail; 9111da177e4SLinus Torvalds if (ipv6_misc_proc_init()) 9121da177e4SLinus Torvalds goto proc_misc6_fail; 9131da177e4SLinus Torvalds if (if6_proc_init()) 9141da177e4SLinus Torvalds goto proc_if6_fail; 9151da177e4SLinus Torvalds #endif 916e2fddf5eSDaniel Lezcano err = ip6_route_init(); 917e2fddf5eSDaniel Lezcano if (err) 918e2fddf5eSDaniel Lezcano goto ip6_route_fail; 9192c861cc6SMichal Kubeček err = ndisc_late_init(); 9202c861cc6SMichal Kubeček if (err) 9212c861cc6SMichal Kubeček goto ndisc_late_fail; 9220a3e78acSDaniel Lezcano err = ip6_flowlabel_init(); 9230a3e78acSDaniel Lezcano if (err) 9240a3e78acSDaniel Lezcano goto ip6_flowlabel_fail; 9251da177e4SLinus Torvalds err = addrconf_init(); 9261da177e4SLinus Torvalds if (err) 9271da177e4SLinus Torvalds goto addrconf_fail; 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds /* Init v6 extension headers. */ 930248b238dSDaniel Lezcano err = ipv6_exthdrs_init(); 931248b238dSDaniel Lezcano if (err) 932248b238dSDaniel Lezcano goto ipv6_exthdrs_fail; 933248b238dSDaniel Lezcano 934853cbbaaSDaniel Lezcano err = ipv6_frag_init(); 935853cbbaaSDaniel Lezcano if (err) 936853cbbaaSDaniel Lezcano goto ipv6_frag_fail; 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds /* Init v6 transport protocols. */ 9397f4e4868SDaniel Lezcano err = udpv6_init(); 9407f4e4868SDaniel Lezcano if (err) 9417f4e4868SDaniel Lezcano goto udpv6_fail; 942e2ed4052SHerbert Xu 9437f4e4868SDaniel Lezcano err = udplitev6_init(); 9447f4e4868SDaniel Lezcano if (err) 9457f4e4868SDaniel Lezcano goto udplitev6_fail; 9467f4e4868SDaniel Lezcano 9477f4e4868SDaniel Lezcano err = tcpv6_init(); 9487f4e4868SDaniel Lezcano if (err) 9497f4e4868SDaniel Lezcano goto tcpv6_fail; 9507f4e4868SDaniel Lezcano 9517f4e4868SDaniel Lezcano err = ipv6_packet_init(); 9527f4e4868SDaniel Lezcano if (err) 9537f4e4868SDaniel Lezcano goto ipv6_packet_fail; 95494911fe3SBenjamin Thery 9556d0bfe22SLorenzo Colitti err = pingv6_init(); 9566d0bfe22SLorenzo Colitti if (err) 9576d0bfe22SLorenzo Colitti goto pingv6_fail; 9586d0bfe22SLorenzo Colitti 95994911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL 96094911fe3SBenjamin Thery err = ipv6_sysctl_register(); 96194911fe3SBenjamin Thery if (err) 96294911fe3SBenjamin Thery goto sysctl_fail; 96394911fe3SBenjamin Thery #endif 9641da177e4SLinus Torvalds out: 9651da177e4SLinus Torvalds return err; 9661da177e4SLinus Torvalds 96794911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL 96894911fe3SBenjamin Thery sysctl_fail: 969eca42aafSVlad Yasevich pingv6_exit(); 97094911fe3SBenjamin Thery #endif 9716d0bfe22SLorenzo Colitti pingv6_fail: 972eca42aafSVlad Yasevich ipv6_packet_cleanup(); 9737f4e4868SDaniel Lezcano ipv6_packet_fail: 9747f4e4868SDaniel Lezcano tcpv6_exit(); 9757f4e4868SDaniel Lezcano tcpv6_fail: 9767f4e4868SDaniel Lezcano udplitev6_exit(); 9777f4e4868SDaniel Lezcano udplitev6_fail: 9787f4e4868SDaniel Lezcano udpv6_exit(); 9797f4e4868SDaniel Lezcano udpv6_fail: 9807f4e4868SDaniel Lezcano ipv6_frag_exit(); 981853cbbaaSDaniel Lezcano ipv6_frag_fail: 982853cbbaaSDaniel Lezcano ipv6_exthdrs_exit(); 983248b238dSDaniel Lezcano ipv6_exthdrs_fail: 984248b238dSDaniel Lezcano addrconf_cleanup(); 9851da177e4SLinus Torvalds addrconf_fail: 9861da177e4SLinus Torvalds ip6_flowlabel_cleanup(); 9870a3e78acSDaniel Lezcano ip6_flowlabel_fail: 9882c861cc6SMichal Kubeček ndisc_late_cleanup(); 9892c861cc6SMichal Kubeček ndisc_late_fail: 9901da177e4SLinus Torvalds ip6_route_cleanup(); 991e2fddf5eSDaniel Lezcano ip6_route_fail: 9921da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 9931da177e4SLinus Torvalds if6_proc_exit(); 9941da177e4SLinus Torvalds proc_if6_fail: 9951da177e4SLinus Torvalds ipv6_misc_proc_exit(); 9961da177e4SLinus Torvalds proc_misc6_fail: 997ba4e58ecSGerrit Renker udplite6_proc_exit(); 998ba4e58ecSGerrit Renker proc_udplite6_fail: 9991da177e4SLinus Torvalds raw6_proc_exit(); 10001da177e4SLinus Torvalds proc_raw6_fail: 10011da177e4SLinus Torvalds #endif 10022cc7d573SHarald Welte ipv6_netfilter_fini(); 10032cc7d573SHarald Welte netfilter_fail: 10041da177e4SLinus Torvalds igmp6_cleanup(); 10051da177e4SLinus Torvalds igmp_fail: 10061da177e4SLinus Torvalds ndisc_cleanup(); 10071da177e4SLinus Torvalds ndisc_fail: 1008623d1a1aSWang Chen ip6_mr_cleanup(); 1009623d1a1aSWang Chen ipmr_fail: 10101da177e4SLinus Torvalds icmpv6_cleanup(); 10111da177e4SLinus Torvalds icmp_fail: 101281c1c178SDaniel Lezcano unregister_pernet_subsys(&inet6_net_ops); 101381c1c178SDaniel Lezcano register_pernet_fail: 10148eb55910SDavid S. Miller sock_unregister(PF_INET6); 1015e2fddf5eSDaniel Lezcano rtnl_unregister_all(PF_INET6); 10167f4e4868SDaniel Lezcano out_sock_register_fail: 10177f4e4868SDaniel Lezcano rawv6_exit(); 10186d0bfe22SLorenzo Colitti out_unregister_ping_proto: 10196d0bfe22SLorenzo Colitti proto_unregister(&pingv6_prot); 10201da177e4SLinus Torvalds out_unregister_raw_proto: 10211da177e4SLinus Torvalds proto_unregister(&rawv6_prot); 1022ba4e58ecSGerrit Renker out_unregister_udplite_proto: 1023ba4e58ecSGerrit Renker proto_unregister(&udplitev6_prot); 10241da177e4SLinus Torvalds out_unregister_udp_proto: 10251da177e4SLinus Torvalds proto_unregister(&udpv6_prot); 10261da177e4SLinus Torvalds out_unregister_tcp_proto: 10271da177e4SLinus Torvalds proto_unregister(&tcpv6_prot); 10281da177e4SLinus Torvalds goto out; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds module_init(inet6_init); 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6); 1033