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; 1091da177e4SLinus Torvalds char answer_no_check; 110af1afe86SYOSHIFUJI Hideaki int try_loading_module = 0; 111af1afe86SYOSHIFUJI Hideaki int err; 1121da177e4SLinus Torvalds 113b3da2cf3SDavid S. Miller if (sock->type != SOCK_RAW && 114b3da2cf3SDavid S. Miller sock->type != SOCK_DGRAM && 115b3da2cf3SDavid S. Miller !inet_ehash_secret) 116b3da2cf3SDavid S. Miller build_ehash_secret(); 117b3da2cf3SDavid S. Miller 1181da177e4SLinus Torvalds /* Look for the requested type/protocol pair. */ 119af1afe86SYOSHIFUJI Hideaki lookup_protocol: 120af1afe86SYOSHIFUJI Hideaki err = -ESOCKTNOSUPPORT; 1211da177e4SLinus Torvalds rcu_read_lock(); 122696adfe8SPaul E. McKenney list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) { 1231da177e4SLinus Torvalds 124696adfe8SPaul E. McKenney err = 0; 1251da177e4SLinus Torvalds /* Check the non-wild match. */ 1261da177e4SLinus Torvalds if (protocol == answer->protocol) { 1271da177e4SLinus Torvalds if (protocol != IPPROTO_IP) 1281da177e4SLinus Torvalds break; 1291da177e4SLinus Torvalds } else { 1301da177e4SLinus Torvalds /* Check for the two wild cases. */ 1311da177e4SLinus Torvalds if (IPPROTO_IP == protocol) { 1321da177e4SLinus Torvalds protocol = answer->protocol; 1331da177e4SLinus Torvalds break; 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds if (IPPROTO_IP == answer->protocol) 1361da177e4SLinus Torvalds break; 1371da177e4SLinus Torvalds } 138af1afe86SYOSHIFUJI Hideaki err = -EPROTONOSUPPORT; 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 141696adfe8SPaul E. McKenney if (err) { 142af1afe86SYOSHIFUJI Hideaki if (try_loading_module < 2) { 143af1afe86SYOSHIFUJI Hideaki rcu_read_unlock(); 144af1afe86SYOSHIFUJI Hideaki /* 145af1afe86SYOSHIFUJI Hideaki * Be more specific, e.g. net-pf-10-proto-132-type-1 146af1afe86SYOSHIFUJI Hideaki * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM) 147af1afe86SYOSHIFUJI Hideaki */ 148af1afe86SYOSHIFUJI Hideaki if (++try_loading_module == 1) 149af1afe86SYOSHIFUJI Hideaki request_module("net-pf-%d-proto-%d-type-%d", 150af1afe86SYOSHIFUJI Hideaki PF_INET6, protocol, sock->type); 151af1afe86SYOSHIFUJI Hideaki /* 152af1afe86SYOSHIFUJI Hideaki * Fall back to generic, e.g. net-pf-10-proto-132 153af1afe86SYOSHIFUJI Hideaki * (net-pf-PF_INET6-proto-IPPROTO_SCTP) 154af1afe86SYOSHIFUJI Hideaki */ 155af1afe86SYOSHIFUJI Hideaki else 156af1afe86SYOSHIFUJI Hideaki request_module("net-pf-%d-proto-%d", 157af1afe86SYOSHIFUJI Hideaki PF_INET6, protocol); 158af1afe86SYOSHIFUJI Hideaki goto lookup_protocol; 159af1afe86SYOSHIFUJI Hideaki } else 1601da177e4SLinus Torvalds goto out_rcu_unlock; 161af1afe86SYOSHIFUJI Hideaki } 162af1afe86SYOSHIFUJI Hideaki 163af1afe86SYOSHIFUJI Hideaki err = -EPERM; 164af31f412SEric W. Biederman if (sock->type == SOCK_RAW && !kern && 165af31f412SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_RAW)) 1661da177e4SLinus Torvalds goto out_rcu_unlock; 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds sock->ops = answer->ops; 1691da177e4SLinus Torvalds answer_prot = answer->prot; 1701da177e4SLinus Torvalds answer_no_check = answer->no_check; 1711da177e4SLinus Torvalds answer_flags = answer->flags; 1721da177e4SLinus Torvalds rcu_read_unlock(); 1731da177e4SLinus Torvalds 174547b792cSIlpo Järvinen WARN_ON(answer_prot->slab == NULL); 1751da177e4SLinus Torvalds 176af1afe86SYOSHIFUJI Hideaki err = -ENOBUFS; 1776257ff21SPavel Emelyanov sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot); 1781da177e4SLinus Torvalds if (sk == NULL) 1791da177e4SLinus Torvalds goto out; 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds sock_init_data(sock, sk); 1821da177e4SLinus Torvalds 183af1afe86SYOSHIFUJI Hideaki err = 0; 1841da177e4SLinus Torvalds sk->sk_no_check = answer_no_check; 1851da177e4SLinus Torvalds if (INET_PROTOSW_REUSE & answer_flags) 1864a17fd52SPavel Emelyanov sk->sk_reuse = SK_CAN_REUSE; 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds inet = inet_sk(sk); 189469de9b9SPaul Moore inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0; 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds if (SOCK_RAW == sock->type) { 192c720c7e8SEric Dumazet inet->inet_num = protocol; 1931da177e4SLinus Torvalds if (IPPROTO_RAW == protocol) 1941da177e4SLinus Torvalds inet->hdrincl = 1; 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 197e6848976SArnaldo Carvalho de Melo sk->sk_destruct = inet_sock_destruct; 1981da177e4SLinus Torvalds sk->sk_family = PF_INET6; 1991da177e4SLinus Torvalds sk->sk_protocol = protocol; 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds sk->sk_backlog_rcv = answer->prot->backlog_rcv; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk); 2041da177e4SLinus Torvalds np->hop_limit = -1; 205f935aa9eSDavid S. Miller np->mcast_hops = IPV6_DEFAULT_MCASTHOPS; 2061da177e4SLinus Torvalds np->mc_loop = 1; 2071da177e4SLinus Torvalds np->pmtudisc = IPV6_PMTUDISC_WANT; 2082e761e05SPavel Emelyanov np->ipv6only = net->ipv6.sysctl.bindv6only; 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds /* Init the ipv4 part of the socket since we can have sockets 2111da177e4SLinus Torvalds * using v6 API for ipv4. 2121da177e4SLinus Torvalds */ 2131da177e4SLinus Torvalds inet->uc_ttl = -1; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds inet->mc_loop = 1; 2161da177e4SLinus Torvalds inet->mc_ttl = 1; 2171da177e4SLinus Torvalds inet->mc_index = 0; 2181da177e4SLinus Torvalds inet->mc_list = NULL; 2194c507d28SJiri Benc inet->rcv_tos = 0; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds if (ipv4_config.no_pmtu_disc) 2221da177e4SLinus Torvalds inet->pmtudisc = IP_PMTUDISC_DONT; 2231da177e4SLinus Torvalds else 2241da177e4SLinus Torvalds inet->pmtudisc = IP_PMTUDISC_WANT; 225e6848976SArnaldo Carvalho de Melo /* 226e6848976SArnaldo Carvalho de Melo * Increment only the relevant sk_prot->socks debug field, this changes 227e6848976SArnaldo Carvalho de Melo * the previous behaviour of incrementing both the equivalent to 228e6848976SArnaldo Carvalho de Melo * answer->prot->socks (inet6_sock_nr) and inet_sock_nr. 229e6848976SArnaldo Carvalho de Melo * 230e6848976SArnaldo Carvalho de Melo * This allows better debug granularity as we'll know exactly how many 231e6848976SArnaldo Carvalho de Melo * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6 232e6848976SArnaldo Carvalho de Melo * transport protocol socks. -acme 233e6848976SArnaldo Carvalho de Melo */ 234e6848976SArnaldo Carvalho de Melo sk_refcnt_debug_inc(sk); 2351da177e4SLinus Torvalds 236c720c7e8SEric Dumazet if (inet->inet_num) { 2371da177e4SLinus Torvalds /* It assumes that any protocol which allows 2381da177e4SLinus Torvalds * the user to assign a number at socket 2391da177e4SLinus Torvalds * creation time automatically shares. 2401da177e4SLinus Torvalds */ 241c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 2421da177e4SLinus Torvalds sk->sk_prot->hash(sk); 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds if (sk->sk_prot->init) { 245af1afe86SYOSHIFUJI Hideaki err = sk->sk_prot->init(sk); 246af1afe86SYOSHIFUJI Hideaki if (err) { 2471da177e4SLinus Torvalds sk_common_release(sk); 2481da177e4SLinus Torvalds goto out; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds out: 252af1afe86SYOSHIFUJI Hideaki return err; 2531da177e4SLinus Torvalds out_rcu_unlock: 2541da177e4SLinus Torvalds rcu_read_unlock(); 2551da177e4SLinus Torvalds goto out; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds /* bind for INET6 API */ 2601da177e4SLinus Torvalds int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr; 2631da177e4SLinus Torvalds struct sock *sk = sock->sk; 2641da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 2651da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 2663b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 267fd683222SAl Viro __be32 v4addr = 0; 2681da177e4SLinus Torvalds unsigned short snum; 2691da177e4SLinus Torvalds int addr_type = 0; 2701da177e4SLinus Torvalds int err = 0; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds /* If the socket has its own bind function then use it. */ 2731da177e4SLinus Torvalds if (sk->sk_prot->bind) 2741da177e4SLinus Torvalds return sk->sk_prot->bind(sk, uaddr, addr_len); 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds if (addr_len < SIN6_LEN_RFC2133) 2771da177e4SLinus Torvalds return -EINVAL; 2785a079c30SMarcus Meissner 2795a079c30SMarcus Meissner if (addr->sin6_family != AF_INET6) 280c349a528SMarcus Meissner return -EAFNOSUPPORT; 2815a079c30SMarcus Meissner 2821da177e4SLinus Torvalds addr_type = ipv6_addr_type(&addr->sin6_addr); 2831da177e4SLinus Torvalds if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM) 2841da177e4SLinus Torvalds return -EINVAL; 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds snum = ntohs(addr->sin6_port); 2873594698aSEric W. Biederman if (snum && snum < PROT_SOCK && !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) 2881da177e4SLinus Torvalds return -EACCES; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds lock_sock(sk); 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds /* Check these errors (active socket, double bind). */ 293c720c7e8SEric Dumazet if (sk->sk_state != TCP_CLOSE || inet->inet_num) { 2941da177e4SLinus Torvalds err = -EINVAL; 2951da177e4SLinus Torvalds goto out; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds /* Check if the address belongs to the host. */ 2991da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_MAPPED) { 30063d9950bSVlad Yasevich int chk_addr_ret; 30163d9950bSVlad Yasevich 302783ed5a7SVlad Yasevich /* Binding to v4-mapped address on a v6-only socket 303783ed5a7SVlad Yasevich * makes no sense 304783ed5a7SVlad Yasevich */ 305783ed5a7SVlad Yasevich if (np->ipv6only) { 306783ed5a7SVlad Yasevich err = -EINVAL; 307783ed5a7SVlad Yasevich goto out; 308783ed5a7SVlad Yasevich } 30963d9950bSVlad Yasevich 310a34f0b31SUwe Kleine-König /* Reproduce AF_INET checks to make the bindings consistent */ 3111da177e4SLinus Torvalds v4addr = addr->sin6_addr.s6_addr32[3]; 31263d9950bSVlad Yasevich chk_addr_ret = inet_addr_type(net, v4addr); 31363d9950bSVlad Yasevich if (!sysctl_ip_nonlocal_bind && 31463d9950bSVlad Yasevich !(inet->freebind || inet->transparent) && 31563d9950bSVlad Yasevich v4addr != htonl(INADDR_ANY) && 31663d9950bSVlad Yasevich chk_addr_ret != RTN_LOCAL && 31763d9950bSVlad Yasevich chk_addr_ret != RTN_MULTICAST && 318ca6982b8SBruno Prémont chk_addr_ret != RTN_BROADCAST) { 319ca6982b8SBruno Prémont err = -EADDRNOTAVAIL; 3201da177e4SLinus Torvalds goto out; 321ca6982b8SBruno Prémont } 3221da177e4SLinus Torvalds } else { 3231da177e4SLinus Torvalds if (addr_type != IPV6_ADDR_ANY) { 3241da177e4SLinus Torvalds struct net_device *dev = NULL; 3251da177e4SLinus Torvalds 32616ba5e8eSEric Dumazet rcu_read_lock(); 327842df073SHannes Frederic Sowa if (__ipv6_addr_needs_scope_id(addr_type)) { 3281da177e4SLinus Torvalds if (addr_len >= sizeof(struct sockaddr_in6) && 3291da177e4SLinus Torvalds addr->sin6_scope_id) { 3301da177e4SLinus Torvalds /* Override any existing binding, if another one 3311da177e4SLinus Torvalds * is supplied by user. 3321da177e4SLinus Torvalds */ 3331da177e4SLinus Torvalds sk->sk_bound_dev_if = addr->sin6_scope_id; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds /* Binding to link-local address requires an interface */ 3371da177e4SLinus Torvalds if (!sk->sk_bound_dev_if) { 3381da177e4SLinus Torvalds err = -EINVAL; 33916ba5e8eSEric Dumazet goto out_unlock; 3401da177e4SLinus Torvalds } 34116ba5e8eSEric Dumazet dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if); 3421da177e4SLinus Torvalds if (!dev) { 3431da177e4SLinus Torvalds err = -ENODEV; 34416ba5e8eSEric Dumazet goto out_unlock; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds /* ipv4 addr of the socket is invalid. Only the 3491da177e4SLinus Torvalds * unspecified and mapped address have a v4 equivalent. 3501da177e4SLinus Torvalds */ 3511da177e4SLinus Torvalds v4addr = LOOPBACK4_IPV6; 3521da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST)) { 353f74024d9SMaciej Żenczykowski if (!(inet->freebind || inet->transparent) && 3540a513f6aSBalazs Scheidler !ipv6_chk_addr(net, &addr->sin6_addr, 355bfeade08SDaniel Lezcano dev, 0)) { 3561da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 35716ba5e8eSEric Dumazet goto out_unlock; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds } 36016ba5e8eSEric Dumazet rcu_read_unlock(); 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 364c720c7e8SEric Dumazet inet->inet_rcv_saddr = v4addr; 365c720c7e8SEric Dumazet inet->inet_saddr = v4addr; 3661da177e4SLinus Torvalds 3674e3fd7a0SAlexey Dobriyan np->rcv_saddr = addr->sin6_addr; 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST)) 3704e3fd7a0SAlexey Dobriyan np->saddr = addr->sin6_addr; 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds /* Make sure we are allowed to bind here. */ 3731da177e4SLinus Torvalds if (sk->sk_prot->get_port(sk, snum)) { 3741da177e4SLinus Torvalds inet_reset_saddr(sk); 3751da177e4SLinus Torvalds err = -EADDRINUSE; 3761da177e4SLinus Torvalds goto out; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3790f8d3c7aSVlad Yasevich if (addr_type != IPV6_ADDR_ANY) { 3801da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDADDR_LOCK; 3810f8d3c7aSVlad Yasevich if (addr_type != IPV6_ADDR_MAPPED) 3820f8d3c7aSVlad Yasevich np->ipv6only = 1; 3830f8d3c7aSVlad Yasevich } 3841da177e4SLinus Torvalds if (snum) 3851da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 386c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 387c720c7e8SEric Dumazet inet->inet_dport = 0; 388c720c7e8SEric Dumazet inet->inet_daddr = 0; 3891da177e4SLinus Torvalds out: 3901da177e4SLinus Torvalds release_sock(sk); 3911da177e4SLinus Torvalds return err; 39216ba5e8eSEric Dumazet out_unlock: 39316ba5e8eSEric Dumazet rcu_read_unlock(); 39416ba5e8eSEric Dumazet goto out; 3951da177e4SLinus Torvalds } 3967159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_bind); 3977159039aSYOSHIFUJI Hideaki 3981da177e4SLinus Torvalds int inet6_release(struct socket *sock) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds struct sock *sk = sock->sk; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds if (sk == NULL) 4031da177e4SLinus Torvalds return -EINVAL; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /* Free mc lists */ 4061da177e4SLinus Torvalds ipv6_sock_mc_close(sk); 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /* Free ac lists */ 4091da177e4SLinus Torvalds ipv6_sock_ac_close(sk); 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds return inet_release(sock); 4121da177e4SLinus Torvalds } 4137159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release); 4147159039aSYOSHIFUJI Hideaki 4157d06b2e0SBrian Haley void inet6_destroy_sock(struct sock *sk) 4161da177e4SLinus Torvalds { 4171da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4181da177e4SLinus Torvalds struct sk_buff *skb; 4191da177e4SLinus Torvalds struct ipv6_txoptions *opt; 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds /* Release rx options */ 4221da177e4SLinus Torvalds 423647c0c70SEldad Zack skb = xchg(&np->pktoptions, NULL); 424647c0c70SEldad Zack if (skb != NULL) 4251da177e4SLinus Torvalds kfree_skb(skb); 4261da177e4SLinus Torvalds 427647c0c70SEldad Zack skb = xchg(&np->rxpmtu, NULL); 428647c0c70SEldad Zack if (skb != NULL) 4294b340ae2SBrian Haley kfree_skb(skb); 4304b340ae2SBrian Haley 4311da177e4SLinus Torvalds /* Free flowlabels */ 4321da177e4SLinus Torvalds fl6_free_socklist(sk); 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /* Free tx options */ 4351da177e4SLinus Torvalds 436647c0c70SEldad Zack opt = xchg(&np->opt, NULL); 437647c0c70SEldad Zack if (opt != NULL) 4381da177e4SLinus Torvalds sock_kfree_s(sk, opt, opt->tot_len); 4391da177e4SLinus Torvalds } 4403cf3dc6cSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_destroy_sock); 4413cf3dc6cSArnaldo Carvalho de Melo 4421da177e4SLinus Torvalds /* 4431da177e4SLinus Torvalds * This does both peername and sockname. 4441da177e4SLinus Torvalds */ 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr, 4471da177e4SLinus Torvalds int *uaddr_len, int peer) 4481da177e4SLinus Torvalds { 4491da177e4SLinus Torvalds struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr; 4501da177e4SLinus Torvalds struct sock *sk = sock->sk; 4511da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 4521da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds sin->sin6_family = AF_INET6; 4551da177e4SLinus Torvalds sin->sin6_flowinfo = 0; 4561da177e4SLinus Torvalds sin->sin6_scope_id = 0; 4571da177e4SLinus Torvalds if (peer) { 458c720c7e8SEric Dumazet if (!inet->inet_dport) 4591da177e4SLinus Torvalds return -ENOTCONN; 4601da177e4SLinus Torvalds if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && 4611da177e4SLinus Torvalds peer == 1) 4621da177e4SLinus Torvalds return -ENOTCONN; 463c720c7e8SEric Dumazet sin->sin6_port = inet->inet_dport; 4644e3fd7a0SAlexey Dobriyan sin->sin6_addr = np->daddr; 4651da177e4SLinus Torvalds if (np->sndflow) 4661da177e4SLinus Torvalds sin->sin6_flowinfo = np->flow_label; 4671da177e4SLinus Torvalds } else { 4681da177e4SLinus Torvalds if (ipv6_addr_any(&np->rcv_saddr)) 4694e3fd7a0SAlexey Dobriyan sin->sin6_addr = np->saddr; 4701da177e4SLinus Torvalds else 4714e3fd7a0SAlexey Dobriyan sin->sin6_addr = np->rcv_saddr; 4721da177e4SLinus Torvalds 473c720c7e8SEric Dumazet sin->sin6_port = inet->inet_sport; 4741da177e4SLinus Torvalds } 475842df073SHannes Frederic Sowa sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr, 476842df073SHannes Frederic Sowa sk->sk_bound_dev_if); 4771da177e4SLinus Torvalds *uaddr_len = sizeof(*sin); 478a02cec21SEric Dumazet return 0; 4791da177e4SLinus Torvalds } 4807159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname); 4817159039aSYOSHIFUJI Hideaki 4821da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 4831da177e4SLinus Torvalds { 4841da177e4SLinus Torvalds struct sock *sk = sock->sk; 4853b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 4861da177e4SLinus Torvalds 487647c0c70SEldad Zack switch (cmd) { 4881da177e4SLinus Torvalds case SIOCGSTAMP: 4891da177e4SLinus Torvalds return sock_get_timestamp(sk, (struct timeval __user *)arg); 4901da177e4SLinus Torvalds 491ae40eb1eSEric Dumazet case SIOCGSTAMPNS: 492ae40eb1eSEric Dumazet return sock_get_timestampns(sk, (struct timespec __user *)arg); 493ae40eb1eSEric Dumazet 4941da177e4SLinus Torvalds case SIOCADDRT: 4951da177e4SLinus Torvalds case SIOCDELRT: 4961da177e4SLinus Torvalds 497a02cec21SEric Dumazet return ipv6_route_ioctl(net, cmd, (void __user *)arg); 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds case SIOCSIFADDR: 500af284937SDaniel Lezcano return addrconf_add_ifaddr(net, (void __user *) arg); 5011da177e4SLinus Torvalds case SIOCDIFADDR: 502af284937SDaniel Lezcano return addrconf_del_ifaddr(net, (void __user *) arg); 5031da177e4SLinus Torvalds case SIOCSIFDSTADDR: 504af284937SDaniel Lezcano return addrconf_set_dstaddr(net, (void __user *) arg); 5051da177e4SLinus Torvalds default: 506b5e5fa5eSChristoph Hellwig if (!sk->sk_prot->ioctl) 507b5e5fa5eSChristoph Hellwig return -ENOIOCTLCMD; 508b5e5fa5eSChristoph Hellwig return sk->sk_prot->ioctl(sk, cmd, arg); 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds /*NOTREACHED*/ 511a02cec21SEric Dumazet return 0; 5121da177e4SLinus Torvalds } 5137159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl); 5147159039aSYOSHIFUJI Hideaki 51590ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = { 5161da177e4SLinus Torvalds .family = PF_INET6, 5171da177e4SLinus Torvalds .owner = THIS_MODULE, 5181da177e4SLinus Torvalds .release = inet6_release, 5191da177e4SLinus Torvalds .bind = inet6_bind, 5201da177e4SLinus Torvalds .connect = inet_stream_connect, /* ok */ 5211da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */ 5221da177e4SLinus Torvalds .accept = inet_accept, /* ok */ 5231da177e4SLinus Torvalds .getname = inet6_getname, 5241da177e4SLinus Torvalds .poll = tcp_poll, /* ok */ 5251da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */ 5261da177e4SLinus Torvalds .listen = inet_listen, /* ok */ 5271da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */ 5281da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */ 5291da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */ 5307ba42910SChangli Gao .sendmsg = inet_sendmsg, /* ok */ 5317ba42910SChangli Gao .recvmsg = inet_recvmsg, /* ok */ 532543d9cfeSArnaldo Carvalho de Melo .mmap = sock_no_mmap, 5337ba42910SChangli Gao .sendpage = inet_sendpage, 534a0974dd3SJens Axboe .splice_read = tcp_splice_read, 5353fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 5363fdadf7dSDmitry Mishin .compat_setsockopt = compat_sock_common_setsockopt, 5373fdadf7dSDmitry Mishin .compat_getsockopt = compat_sock_common_getsockopt, 5383fdadf7dSDmitry Mishin #endif 5391da177e4SLinus Torvalds }; 5401da177e4SLinus Torvalds 54190ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = { 5421da177e4SLinus Torvalds .family = PF_INET6, 5431da177e4SLinus Torvalds .owner = THIS_MODULE, 5441da177e4SLinus Torvalds .release = inet6_release, 5451da177e4SLinus Torvalds .bind = inet6_bind, 5461da177e4SLinus Torvalds .connect = inet_dgram_connect, /* ok */ 5471da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */ 5481da177e4SLinus Torvalds .accept = sock_no_accept, /* a do nothing */ 5491da177e4SLinus Torvalds .getname = inet6_getname, 5501da177e4SLinus Torvalds .poll = udp_poll, /* ok */ 5511da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */ 5521da177e4SLinus Torvalds .listen = sock_no_listen, /* ok */ 5531da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */ 5541da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */ 5551da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */ 5561da177e4SLinus Torvalds .sendmsg = inet_sendmsg, /* ok */ 5577ba42910SChangli Gao .recvmsg = inet_recvmsg, /* ok */ 5581da177e4SLinus Torvalds .mmap = sock_no_mmap, 5591da177e4SLinus Torvalds .sendpage = sock_no_sendpage, 560543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 561543d9cfeSArnaldo Carvalho de Melo .compat_setsockopt = compat_sock_common_setsockopt, 562543d9cfeSArnaldo Carvalho de Melo .compat_getsockopt = compat_sock_common_getsockopt, 563543d9cfeSArnaldo Carvalho de Melo #endif 5641da177e4SLinus Torvalds }; 5651da177e4SLinus Torvalds 566ec1b4cf7SStephen Hemminger static const struct net_proto_family inet6_family_ops = { 5671da177e4SLinus Torvalds .family = PF_INET6, 5681da177e4SLinus Torvalds .create = inet6_create, 5691da177e4SLinus Torvalds .owner = THIS_MODULE, 5701da177e4SLinus Torvalds }; 5711da177e4SLinus Torvalds 57287c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p) 5731da177e4SLinus Torvalds { 5741da177e4SLinus Torvalds struct list_head *lh; 5751da177e4SLinus Torvalds struct inet_protosw *answer; 5761da177e4SLinus Torvalds struct list_head *last_perm; 57787c3efbfSDaniel Lezcano int protocol = p->protocol; 57887c3efbfSDaniel Lezcano int ret; 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock); 5811da177e4SLinus Torvalds 58287c3efbfSDaniel Lezcano ret = -EINVAL; 5831da177e4SLinus Torvalds if (p->type >= SOCK_MAX) 5841da177e4SLinus Torvalds goto out_illegal; 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds /* If we are trying to override a permanent protocol, bail. */ 5871da177e4SLinus Torvalds answer = NULL; 58887c3efbfSDaniel Lezcano ret = -EPERM; 5891da177e4SLinus Torvalds last_perm = &inetsw6[p->type]; 5901da177e4SLinus Torvalds list_for_each(lh, &inetsw6[p->type]) { 5911da177e4SLinus Torvalds answer = list_entry(lh, struct inet_protosw, list); 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds /* Check only the non-wild match. */ 5941da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & answer->flags) { 5951da177e4SLinus Torvalds if (protocol == answer->protocol) 5961da177e4SLinus Torvalds break; 5971da177e4SLinus Torvalds last_perm = lh; 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds answer = NULL; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds if (answer) 6031da177e4SLinus Torvalds goto out_permanent; 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds /* Add the new entry after the last permanent entry if any, so that 6061da177e4SLinus Torvalds * the new entry does not override a permanent entry when matched with 6071da177e4SLinus Torvalds * a wild-card protocol. But it is allowed to override any existing 6081da177e4SLinus Torvalds * non-permanent entry. This means that when we remove this entry, the 6091da177e4SLinus Torvalds * system automatically returns to the old behavior. 6101da177e4SLinus Torvalds */ 6111da177e4SLinus Torvalds list_add_rcu(&p->list, last_perm); 61287c3efbfSDaniel Lezcano ret = 0; 6131da177e4SLinus Torvalds out: 6141da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock); 61587c3efbfSDaniel Lezcano return ret; 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds out_permanent: 618f3213831SJoe Perches pr_err("Attempt to override permanent protocol %d\n", protocol); 6191da177e4SLinus Torvalds goto out; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds out_illegal: 622f3213831SJoe Perches pr_err("Ignoring attempt to register invalid socket type %d\n", 6231da177e4SLinus Torvalds p->type); 6241da177e4SLinus Torvalds goto out; 6251da177e4SLinus Torvalds } 6267159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw); 6277159039aSYOSHIFUJI Hideaki 6281da177e4SLinus Torvalds void 6291da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p) 6301da177e4SLinus Torvalds { 6311da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & p->flags) { 632f3213831SJoe Perches pr_err("Attempt to unregister permanent protocol %d\n", 6331da177e4SLinus Torvalds p->protocol); 6341da177e4SLinus Torvalds } else { 6351da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock); 6361da177e4SLinus Torvalds list_del_rcu(&p->list); 6371da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock); 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds synchronize_net(); 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds } 6427159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw); 6437159039aSYOSHIFUJI Hideaki 644b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk) 645b9750ce1SArnaldo Carvalho de Melo { 646b9750ce1SArnaldo Carvalho de Melo struct ipv6_pinfo *np = inet6_sk(sk); 64768d0c6d3SDavid S. Miller struct dst_entry *dst; 648b9750ce1SArnaldo Carvalho de Melo 649b9750ce1SArnaldo Carvalho de Melo dst = __sk_dst_check(sk, np->dst_cookie); 650b9750ce1SArnaldo Carvalho de Melo 651b9750ce1SArnaldo Carvalho de Melo if (dst == NULL) { 652b9750ce1SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 65320c59de2SArnaud Ebalard struct in6_addr *final_p, final; 6544c9483b2SDavid S. Miller struct flowi6 fl6; 655b9750ce1SArnaldo Carvalho de Melo 6564c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 6574c9483b2SDavid S. Miller fl6.flowi6_proto = sk->sk_protocol; 6584e3fd7a0SAlexey Dobriyan fl6.daddr = np->daddr; 6594e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 6604c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 6614c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 6624c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark; 6631958b856SDavid S. Miller fl6.fl6_dport = inet->inet_dport; 6641958b856SDavid S. Miller fl6.fl6_sport = inet->inet_sport; 6654c9483b2SDavid S. Miller security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); 666b9750ce1SArnaldo Carvalho de Melo 6674c9483b2SDavid S. Miller final_p = fl6_update_dst(&fl6, np->opt, &final); 668b9750ce1SArnaldo Carvalho de Melo 6694c9483b2SDavid S. Miller dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false); 67068d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 671b9750ce1SArnaldo Carvalho de Melo sk->sk_route_caps = 0; 67268d0c6d3SDavid S. Miller sk->sk_err_soft = -PTR_ERR(dst); 67368d0c6d3SDavid S. Miller return PTR_ERR(dst); 674b9750ce1SArnaldo Carvalho de Melo } 675b9750ce1SArnaldo Carvalho de Melo 6768e1ef0a9SYOSHIFUJI Hideaki __ip6_dst_store(sk, dst, NULL, NULL); 677b9750ce1SArnaldo Carvalho de Melo } 678b9750ce1SArnaldo Carvalho de Melo 679b9750ce1SArnaldo Carvalho de Melo return 0; 680b9750ce1SArnaldo Carvalho de Melo } 681b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header); 682b9750ce1SArnaldo Carvalho de Melo 68392113bfdSEric Dumazet bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb) 684399c07deSArnaldo Carvalho de Melo { 68592113bfdSEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk); 68692113bfdSEric Dumazet const struct inet6_skb_parm *opt = IP6CB(skb); 687399c07deSArnaldo Carvalho de Melo 688399c07deSArnaldo Carvalho de Melo if (np->rxopt.all) { 689399c07deSArnaldo Carvalho de Melo if ((opt->hop && (np->rxopt.bits.hopopts || 690399c07deSArnaldo Carvalho de Melo np->rxopt.bits.ohopopts)) || 691d56f90a7SArnaldo Carvalho de Melo ((IPV6_FLOWINFO_MASK & 692d56f90a7SArnaldo Carvalho de Melo *(__be32 *)skb_network_header(skb)) && 693399c07deSArnaldo Carvalho de Melo np->rxopt.bits.rxflow) || 694399c07deSArnaldo Carvalho de Melo (opt->srcrt && (np->rxopt.bits.srcrt || 695399c07deSArnaldo Carvalho de Melo np->rxopt.bits.osrcrt)) || 696399c07deSArnaldo Carvalho de Melo ((opt->dst1 || opt->dst0) && 697399c07deSArnaldo Carvalho de Melo (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts))) 69892113bfdSEric Dumazet return true; 699399c07deSArnaldo Carvalho de Melo } 70092113bfdSEric Dumazet return false; 701399c07deSArnaldo Carvalho de Melo } 702399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted); 703399c07deSArnaldo Carvalho de Melo 7047546dd97SStephen Hemminger static struct packet_type ipv6_packet_type __read_mostly = { 70509640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_IPV6), 706662397fdSYOSHIFUJI Hideaki .func = ipv6_rcv, 70722061d80SVlad Yasevich }; 70822061d80SVlad Yasevich 709662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void) 710662397fdSYOSHIFUJI Hideaki { 711662397fdSYOSHIFUJI Hideaki dev_add_pack(&ipv6_packet_type); 712662397fdSYOSHIFUJI Hideaki return 0; 713662397fdSYOSHIFUJI Hideaki } 714662397fdSYOSHIFUJI Hideaki 715662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void) 716662397fdSYOSHIFUJI Hideaki { 717662397fdSYOSHIFUJI Hideaki dev_remove_pack(&ipv6_packet_type); 718662397fdSYOSHIFUJI Hideaki } 719662397fdSYOSHIFUJI Hideaki 720e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net) 721e43291cbSDenis V. Lunev { 7227d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)net->mib.udp_stats_in6, 7231823e4c8SEric Dumazet sizeof(struct udp_mib), 7241823e4c8SEric Dumazet __alignof__(struct udp_mib)) < 0) 7250c7ed677SDenis V. Lunev return -ENOMEM; 7267d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)net->mib.udplite_stats_in6, 7271823e4c8SEric Dumazet sizeof(struct udp_mib), 7281823e4c8SEric Dumazet __alignof__(struct udp_mib)) < 0) 729be713a44SDenis V. Lunev goto err_udplite_mib; 7307d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)net->mib.ipv6_statistics, 7311823e4c8SEric Dumazet sizeof(struct ipstats_mib), 7321823e4c8SEric Dumazet __alignof__(struct ipstats_mib)) < 0) 7339261e537SDenis V. Lunev goto err_ip_mib; 7347d720c3eSTejun Heo if (snmp_mib_init((void __percpu **)net->mib.icmpv6_statistics, 7351823e4c8SEric Dumazet sizeof(struct icmpv6_mib), 7361823e4c8SEric Dumazet __alignof__(struct icmpv6_mib)) < 0) 7379261e537SDenis V. Lunev goto err_icmp_mib; 7382a24444fSEric Dumazet net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib), 7392a24444fSEric Dumazet GFP_KERNEL); 7402a24444fSEric Dumazet if (!net->mib.icmpv6msg_statistics) 7419261e537SDenis V. Lunev goto err_icmpmsg_mib; 742e43291cbSDenis V. Lunev return 0; 743be713a44SDenis V. Lunev 7449261e537SDenis V. Lunev err_icmpmsg_mib: 7457d720c3eSTejun Heo snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics); 7469261e537SDenis V. Lunev err_icmp_mib: 7477d720c3eSTejun Heo snmp_mib_free((void __percpu **)net->mib.ipv6_statistics); 7489261e537SDenis V. Lunev err_ip_mib: 7497d720c3eSTejun Heo snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6); 750be713a44SDenis V. Lunev err_udplite_mib: 7517d720c3eSTejun Heo snmp_mib_free((void __percpu **)net->mib.udp_stats_in6); 752be713a44SDenis V. Lunev return -ENOMEM; 753e43291cbSDenis V. Lunev } 754e43291cbSDenis V. Lunev 7552c8c1e72SAlexey Dobriyan static void ipv6_cleanup_mibs(struct net *net) 756e43291cbSDenis V. Lunev { 7577d720c3eSTejun Heo snmp_mib_free((void __percpu **)net->mib.udp_stats_in6); 7587d720c3eSTejun Heo snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6); 7597d720c3eSTejun Heo snmp_mib_free((void __percpu **)net->mib.ipv6_statistics); 7607d720c3eSTejun Heo snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics); 7612a24444fSEric Dumazet kfree(net->mib.icmpv6msg_statistics); 762e43291cbSDenis V. Lunev } 763e43291cbSDenis V. Lunev 764e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net) 76581c1c178SDaniel Lezcano { 7660c96d8c5SDaniel Lezcano int err = 0; 7670c96d8c5SDaniel Lezcano 76899bc9c4eSDaniel Lezcano net->ipv6.sysctl.bindv6only = 0; 76941a76906SDaniel Lezcano net->ipv6.sysctl.icmpv6_time = 1*HZ; 770ca4c3fc2Sfan.du atomic_set(&net->ipv6.rt_genid, 0); 771e71e0349SDaniel Lezcano 772e43291cbSDenis V. Lunev err = ipv6_init_mibs(net); 773e43291cbSDenis V. Lunev if (err) 774e43291cbSDenis V. Lunev return err; 7750c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS 7760c96d8c5SDaniel Lezcano err = udp6_proc_init(net); 7770c96d8c5SDaniel Lezcano if (err) 7780c96d8c5SDaniel Lezcano goto out; 7796f8b13bcSDaniel Lezcano err = tcp6_proc_init(net); 7806f8b13bcSDaniel Lezcano if (err) 7816f8b13bcSDaniel Lezcano goto proc_tcp6_fail; 7826ab57e7eSDaniel Lezcano err = ac6_proc_init(net); 7836ab57e7eSDaniel Lezcano if (err) 7846ab57e7eSDaniel Lezcano goto proc_ac6_fail; 7850c96d8c5SDaniel Lezcano #endif 7860c96d8c5SDaniel Lezcano return err; 7876f8b13bcSDaniel Lezcano 7886f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS 7896ab57e7eSDaniel Lezcano proc_ac6_fail: 7906ab57e7eSDaniel Lezcano tcp6_proc_exit(net); 7916f8b13bcSDaniel Lezcano proc_tcp6_fail: 7926f8b13bcSDaniel Lezcano udp6_proc_exit(net); 793e43291cbSDenis V. Lunev out: 794e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net); 795e43291cbSDenis V. Lunev return err; 7966f8b13bcSDaniel Lezcano #endif 79781c1c178SDaniel Lezcano } 79881c1c178SDaniel Lezcano 7992c8c1e72SAlexey Dobriyan static void __net_exit inet6_net_exit(struct net *net) 80081c1c178SDaniel Lezcano { 8010c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS 8020c96d8c5SDaniel Lezcano udp6_proc_exit(net); 8036f8b13bcSDaniel Lezcano tcp6_proc_exit(net); 8046ab57e7eSDaniel Lezcano ac6_proc_exit(net); 8050c96d8c5SDaniel Lezcano #endif 806e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net); 80781c1c178SDaniel Lezcano } 80881c1c178SDaniel Lezcano 80981c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = { 81081c1c178SDaniel Lezcano .init = inet6_net_init, 81181c1c178SDaniel Lezcano .exit = inet6_net_exit, 81281c1c178SDaniel Lezcano }; 81381c1c178SDaniel Lezcano 8145f81bd2eSCong Wang static const struct ipv6_stub ipv6_stub_impl = { 8155f81bd2eSCong Wang .ipv6_sock_mc_join = ipv6_sock_mc_join, 8165f81bd2eSCong Wang .ipv6_sock_mc_drop = ipv6_sock_mc_drop, 8175f81bd2eSCong Wang .ipv6_dst_lookup = ip6_dst_lookup, 8185f81bd2eSCong Wang .udpv6_encap_enable = udpv6_encap_enable, 819f564f45cSCong Wang .ndisc_send_na = ndisc_send_na, 820e15a00aaSCong Wang .nd_tbl = &nd_tbl, 8215f81bd2eSCong Wang }; 8225f81bd2eSCong Wang 8231da177e4SLinus Torvalds static int __init inet6_init(void) 8241da177e4SLinus Torvalds { 8251da177e4SLinus Torvalds struct list_head *r; 826fe7ca2e1SBrian Haley int err = 0; 8271da177e4SLinus Torvalds 828ba96bcbcSYOSHIFUJI Hideaki / 吉藤英明 BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > FIELD_SIZEOF(struct sk_buff, cb)); 829ef047f5eSYOSHIFUJI Hideaki 830fe7ca2e1SBrian Haley /* Register the socket-side information for inet6_create. */ 831fe7ca2e1SBrian Haley for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r) 832fe7ca2e1SBrian Haley INIT_LIST_HEAD(r); 833fe7ca2e1SBrian Haley 83456d417b1SBrian Haley if (disable_ipv6_mod) { 835f3213831SJoe Perches pr_info("Loaded, but administratively disabled, reboot required to enable\n"); 836fe7ca2e1SBrian Haley goto out; 837fe7ca2e1SBrian Haley } 838fe7ca2e1SBrian Haley 8391da177e4SLinus Torvalds err = proto_register(&tcpv6_prot, 1); 8401da177e4SLinus Torvalds if (err) 8411da177e4SLinus Torvalds goto out; 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds err = proto_register(&udpv6_prot, 1); 8441da177e4SLinus Torvalds if (err) 8451da177e4SLinus Torvalds goto out_unregister_tcp_proto; 8461da177e4SLinus Torvalds 847ba4e58ecSGerrit Renker err = proto_register(&udplitev6_prot, 1); 8481da177e4SLinus Torvalds if (err) 8491da177e4SLinus Torvalds goto out_unregister_udp_proto; 8501da177e4SLinus Torvalds 851ba4e58ecSGerrit Renker err = proto_register(&rawv6_prot, 1); 852ba4e58ecSGerrit Renker if (err) 853ba4e58ecSGerrit Renker goto out_unregister_udplite_proto; 854ba4e58ecSGerrit Renker 8556d0bfe22SLorenzo Colitti err = proto_register(&pingv6_prot, 1); 8566d0bfe22SLorenzo Colitti if (err) 8576d0bfe22SLorenzo Colitti goto out_unregister_ping_proto; 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /* We MUST register RAW sockets before we create the ICMP6, 8601da177e4SLinus Torvalds * IGMP6, or NDISC control sockets. 8611da177e4SLinus Torvalds */ 8627f4e4868SDaniel Lezcano err = rawv6_init(); 8637f4e4868SDaniel Lezcano if (err) 8647f4e4868SDaniel Lezcano goto out_unregister_raw_proto; 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds /* Register the family here so that the init calls below will 8671da177e4SLinus Torvalds * be able to create sockets. (?? is this dangerous ??) 8681da177e4SLinus Torvalds */ 8698eb55910SDavid S. Miller err = sock_register(&inet6_family_ops); 8708eb55910SDavid S. Miller if (err) 8717f4e4868SDaniel Lezcano goto out_sock_register_fail; 8721da177e4SLinus Torvalds 8733dc43e3eSGlauber Costa tcpv6_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem; 8743dc43e3eSGlauber Costa 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; 9180a3e78acSDaniel Lezcano err = ip6_flowlabel_init(); 9190a3e78acSDaniel Lezcano if (err) 9200a3e78acSDaniel Lezcano goto ip6_flowlabel_fail; 9211da177e4SLinus Torvalds err = addrconf_init(); 9221da177e4SLinus Torvalds if (err) 9231da177e4SLinus Torvalds goto addrconf_fail; 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds /* Init v6 extension headers. */ 926248b238dSDaniel Lezcano err = ipv6_exthdrs_init(); 927248b238dSDaniel Lezcano if (err) 928248b238dSDaniel Lezcano goto ipv6_exthdrs_fail; 929248b238dSDaniel Lezcano 930853cbbaaSDaniel Lezcano err = ipv6_frag_init(); 931853cbbaaSDaniel Lezcano if (err) 932853cbbaaSDaniel Lezcano goto ipv6_frag_fail; 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds /* Init v6 transport protocols. */ 9357f4e4868SDaniel Lezcano err = udpv6_init(); 9367f4e4868SDaniel Lezcano if (err) 9377f4e4868SDaniel Lezcano goto udpv6_fail; 938e2ed4052SHerbert Xu 9397f4e4868SDaniel Lezcano err = udplitev6_init(); 9407f4e4868SDaniel Lezcano if (err) 9417f4e4868SDaniel Lezcano goto udplitev6_fail; 9427f4e4868SDaniel Lezcano 9437f4e4868SDaniel Lezcano err = tcpv6_init(); 9447f4e4868SDaniel Lezcano if (err) 9457f4e4868SDaniel Lezcano goto tcpv6_fail; 9467f4e4868SDaniel Lezcano 9477f4e4868SDaniel Lezcano err = ipv6_packet_init(); 9487f4e4868SDaniel Lezcano if (err) 9497f4e4868SDaniel Lezcano goto ipv6_packet_fail; 95094911fe3SBenjamin Thery 9516d0bfe22SLorenzo Colitti err = pingv6_init(); 9526d0bfe22SLorenzo Colitti if (err) 9536d0bfe22SLorenzo Colitti goto pingv6_fail; 9546d0bfe22SLorenzo Colitti 95594911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL 95694911fe3SBenjamin Thery err = ipv6_sysctl_register(); 95794911fe3SBenjamin Thery if (err) 95894911fe3SBenjamin Thery goto sysctl_fail; 95994911fe3SBenjamin Thery #endif 9601da177e4SLinus Torvalds out: 9611da177e4SLinus Torvalds return err; 9621da177e4SLinus Torvalds 96394911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL 96494911fe3SBenjamin Thery sysctl_fail: 96594911fe3SBenjamin Thery ipv6_packet_cleanup(); 96694911fe3SBenjamin Thery #endif 9676d0bfe22SLorenzo Colitti pingv6_fail: 9686d0bfe22SLorenzo Colitti pingv6_exit(); 9697f4e4868SDaniel Lezcano ipv6_packet_fail: 9707f4e4868SDaniel Lezcano tcpv6_exit(); 9717f4e4868SDaniel Lezcano tcpv6_fail: 9727f4e4868SDaniel Lezcano udplitev6_exit(); 9737f4e4868SDaniel Lezcano udplitev6_fail: 9747f4e4868SDaniel Lezcano udpv6_exit(); 9757f4e4868SDaniel Lezcano udpv6_fail: 9767f4e4868SDaniel Lezcano ipv6_frag_exit(); 977853cbbaaSDaniel Lezcano ipv6_frag_fail: 978853cbbaaSDaniel Lezcano ipv6_exthdrs_exit(); 979248b238dSDaniel Lezcano ipv6_exthdrs_fail: 980248b238dSDaniel Lezcano addrconf_cleanup(); 9811da177e4SLinus Torvalds addrconf_fail: 9821da177e4SLinus Torvalds ip6_flowlabel_cleanup(); 9830a3e78acSDaniel Lezcano ip6_flowlabel_fail: 9841da177e4SLinus Torvalds ip6_route_cleanup(); 985e2fddf5eSDaniel Lezcano ip6_route_fail: 9861da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 9871da177e4SLinus Torvalds if6_proc_exit(); 9881da177e4SLinus Torvalds proc_if6_fail: 9891da177e4SLinus Torvalds ipv6_misc_proc_exit(); 9901da177e4SLinus Torvalds proc_misc6_fail: 991ba4e58ecSGerrit Renker udplite6_proc_exit(); 992ba4e58ecSGerrit Renker proc_udplite6_fail: 9931da177e4SLinus Torvalds raw6_proc_exit(); 9941da177e4SLinus Torvalds proc_raw6_fail: 9951da177e4SLinus Torvalds #endif 9962cc7d573SHarald Welte ipv6_netfilter_fini(); 9972cc7d573SHarald Welte netfilter_fail: 9981da177e4SLinus Torvalds igmp6_cleanup(); 9991da177e4SLinus Torvalds igmp_fail: 10001da177e4SLinus Torvalds ndisc_cleanup(); 10011da177e4SLinus Torvalds ndisc_fail: 1002623d1a1aSWang Chen ip6_mr_cleanup(); 1003623d1a1aSWang Chen ipmr_fail: 10041da177e4SLinus Torvalds icmpv6_cleanup(); 10051da177e4SLinus Torvalds icmp_fail: 100681c1c178SDaniel Lezcano unregister_pernet_subsys(&inet6_net_ops); 100781c1c178SDaniel Lezcano register_pernet_fail: 10088eb55910SDavid S. Miller sock_unregister(PF_INET6); 1009e2fddf5eSDaniel Lezcano rtnl_unregister_all(PF_INET6); 10107f4e4868SDaniel Lezcano out_sock_register_fail: 10117f4e4868SDaniel Lezcano rawv6_exit(); 10126d0bfe22SLorenzo Colitti out_unregister_ping_proto: 10136d0bfe22SLorenzo Colitti proto_unregister(&pingv6_prot); 10141da177e4SLinus Torvalds out_unregister_raw_proto: 10151da177e4SLinus Torvalds proto_unregister(&rawv6_prot); 1016ba4e58ecSGerrit Renker out_unregister_udplite_proto: 1017ba4e58ecSGerrit Renker proto_unregister(&udplitev6_prot); 10181da177e4SLinus Torvalds out_unregister_udp_proto: 10191da177e4SLinus Torvalds proto_unregister(&udpv6_prot); 10201da177e4SLinus Torvalds out_unregister_tcp_proto: 10211da177e4SLinus Torvalds proto_unregister(&tcpv6_prot); 10221da177e4SLinus Torvalds goto out; 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds module_init(inet6_init); 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds static void __exit inet6_exit(void) 10271da177e4SLinus Torvalds { 102856d417b1SBrian Haley if (disable_ipv6_mod) 1029ff8cf9a9SJohn Dykstra return; 1030ff8cf9a9SJohn Dykstra 10311da177e4SLinus Torvalds /* First of all disallow new sockets creation. */ 10321da177e4SLinus Torvalds sock_unregister(PF_INET6); 1033c127ea2cSThomas Graf /* Disallow any further netlink messages */ 1034c127ea2cSThomas Graf rtnl_unregister_all(PF_INET6); 1035ca17c233SJoe Jin 10367f4e4868SDaniel Lezcano udpv6_exit(); 10377f4e4868SDaniel Lezcano udplitev6_exit(); 10387f4e4868SDaniel Lezcano tcpv6_exit(); 10397f4e4868SDaniel Lezcano 1040ca17c233SJoe Jin /* Cleanup code parts. */ 1041ca17c233SJoe Jin ipv6_packet_cleanup(); 1042853cbbaaSDaniel Lezcano ipv6_frag_exit(); 1043248b238dSDaniel Lezcano ipv6_exthdrs_exit(); 10441da177e4SLinus Torvalds addrconf_cleanup(); 1045ca17c233SJoe Jin ip6_flowlabel_cleanup(); 10461da177e4SLinus Torvalds ip6_route_cleanup(); 1047ca17c233SJoe Jin #ifdef CONFIG_PROC_FS 1048ca17c233SJoe Jin 1049ca17c233SJoe Jin /* Cleanup code parts. */ 1050ca17c233SJoe Jin if6_proc_exit(); 1051ca17c233SJoe Jin ipv6_misc_proc_exit(); 1052ca17c233SJoe Jin udplite6_proc_exit(); 1053ca17c233SJoe Jin raw6_proc_exit(); 1054ca17c233SJoe Jin #endif 10552cc7d573SHarald Welte ipv6_netfilter_fini(); 10565f81bd2eSCong Wang ipv6_stub = NULL; 1057ca17c233SJoe Jin igmp6_cleanup(); 10581da177e4SLinus Torvalds ndisc_cleanup(); 1059623d1a1aSWang Chen ip6_mr_cleanup(); 10601da177e4SLinus Torvalds icmpv6_cleanup(); 10617f4e4868SDaniel Lezcano rawv6_exit(); 106294911fe3SBenjamin Thery 106381c1c178SDaniel Lezcano unregister_pernet_subsys(&inet6_net_ops); 10641da177e4SLinus Torvalds proto_unregister(&rawv6_prot); 1065ca17c233SJoe Jin proto_unregister(&udplitev6_prot); 10661da177e4SLinus Torvalds proto_unregister(&udpv6_prot); 10671da177e4SLinus Torvalds proto_unregister(&tcpv6_prot); 10681f2ccd00SJesper Dangaard Brouer 10691f2ccd00SJesper Dangaard Brouer rcu_barrier(); /* Wait for completion of call_rcu()'s */ 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds module_exit(inet6_exit); 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6); 1074