12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * PF_INET6 socket protocol family 41da177e4SLinus Torvalds * Linux INET6 implementation 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: 71da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Adapted from linux/net/ipv4/af_inet.c 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Fixes: 121da177e4SLinus Torvalds * piggy, Karl Knutson : Socket protocol table 131da177e4SLinus Torvalds * Hideaki YOSHIFUJI : sin6_scope_id support 141da177e4SLinus Torvalds * Arnaldo Melo : check proc_net_create return, cleanups 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 17f3213831SJoe Perches #define pr_fmt(fmt) "IPv6: " fmt 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <linux/module.h> 204fc268d2SRandy Dunlap #include <linux/capability.h> 211da177e4SLinus Torvalds #include <linux/errno.h> 221da177e4SLinus Torvalds #include <linux/types.h> 231da177e4SLinus Torvalds #include <linux/socket.h> 241da177e4SLinus Torvalds #include <linux/in.h> 251da177e4SLinus Torvalds #include <linux/kernel.h> 261da177e4SLinus Torvalds #include <linux/timer.h> 271da177e4SLinus Torvalds #include <linux/string.h> 281da177e4SLinus Torvalds #include <linux/sockios.h> 291da177e4SLinus Torvalds #include <linux/net.h> 301da177e4SLinus Torvalds #include <linux/fcntl.h> 311da177e4SLinus Torvalds #include <linux/mm.h> 321da177e4SLinus Torvalds #include <linux/interrupt.h> 331da177e4SLinus Torvalds #include <linux/proc_fs.h> 341da177e4SLinus Torvalds #include <linux/stat.h> 351da177e4SLinus Torvalds #include <linux/init.h> 365a0e3ad6STejun Heo #include <linux/slab.h> 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include <linux/inet.h> 391da177e4SLinus Torvalds #include <linux/netdevice.h> 401da177e4SLinus Torvalds #include <linux/icmpv6.h> 412cc7d573SHarald Welte #include <linux/netfilter_ipv6.h> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #include <net/ip.h> 441da177e4SLinus Torvalds #include <net/ipv6.h> 451da177e4SLinus Torvalds #include <net/udp.h> 46ba4e58ecSGerrit Renker #include <net/udplite.h> 471da177e4SLinus Torvalds #include <net/tcp.h> 486d0bfe22SLorenzo Colitti #include <net/ping.h> 491da177e4SLinus Torvalds #include <net/protocol.h> 501da177e4SLinus Torvalds #include <net/inet_common.h> 511668e010SKOVACS Krisztian #include <net/route.h> 521da177e4SLinus Torvalds #include <net/transp_v6.h> 531da177e4SLinus Torvalds #include <net/ip6_route.h> 541da177e4SLinus Torvalds #include <net/addrconf.h> 553616d08bSDavid Ahern #include <net/ipv6_stubs.h> 56f564f45cSCong Wang #include <net/ndisc.h> 571da177e4SLinus Torvalds #ifdef CONFIG_IPV6_TUNNEL 581da177e4SLinus Torvalds #include <net/ip6_tunnel.h> 591da177e4SLinus Torvalds #endif 60cb72d382SHuw Davies #include <net/calipso.h> 61915d7e5eSDavid Lebrun #include <net/seg6.h> 62a7a29f9cSAlexander Aring #include <net/rpl.h> 633986912fSChristoph Hellwig #include <net/compat.h> 640146dca7SSabrina Dubroca #include <net/xfrm.h> 659ee11f0fSJustin Iurman #include <net/ioam6.h> 660daf07e5SEric Dumazet #include <net/rawv6.h> 671da177e4SLinus Torvalds 687c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 697bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h> 701da177e4SLinus Torvalds 71a6024562STom Herbert #include "ip6_offload.h" 72a6024562STom Herbert 731da177e4SLinus Torvalds MODULE_AUTHOR("Cast of dozens"); 741da177e4SLinus Torvalds MODULE_DESCRIPTION("IPv6 protocol stack for Linux"); 751da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 761da177e4SLinus Torvalds 77f1267347SPavel Emelyanov /* The inetsw6 table contains everything that inet6_create needs to 781da177e4SLinus Torvalds * build a new socket. 791da177e4SLinus Torvalds */ 801da177e4SLinus Torvalds static struct list_head inetsw6[SOCK_MAX]; 811da177e4SLinus Torvalds static DEFINE_SPINLOCK(inetsw6_lock); 821da177e4SLinus Torvalds 8356d417b1SBrian Haley struct ipv6_params ipv6_defaults = { 8456d417b1SBrian Haley .disable_ipv6 = 0, 8556d417b1SBrian Haley .autoconf = 1, 8656d417b1SBrian Haley }; 8756d417b1SBrian Haley 88647c0c70SEldad Zack static int disable_ipv6_mod; 8956d417b1SBrian Haley 9056d417b1SBrian Haley module_param_named(disable, disable_ipv6_mod, int, 0444); 9156d417b1SBrian Haley MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional"); 9256d417b1SBrian Haley 9356d417b1SBrian Haley module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444); 9456d417b1SBrian Haley MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces"); 9556d417b1SBrian Haley 9656d417b1SBrian Haley module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444); 9756d417b1SBrian Haley MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces"); 98fe7ca2e1SBrian Haley 99e4348637SDavid Ahern bool ipv6_mod_enabled(void) 100e4348637SDavid Ahern { 101e4348637SDavid Ahern return disable_ipv6_mod == 0; 102e4348637SDavid Ahern } 103e4348637SDavid Ahern EXPORT_SYMBOL_GPL(ipv6_mod_enabled); 104e4348637SDavid Ahern 105f5f80e32SEric Dumazet static struct ipv6_pinfo *inet6_sk_generic(struct sock *sk) 1061da177e4SLinus Torvalds { 107f5f80e32SEric Dumazet const int offset = sk->sk_prot->ipv6_pinfo_offset; 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 112d38afeecSKuniyuki Iwashima void inet6_sock_destruct(struct sock *sk) 113d38afeecSKuniyuki Iwashima { 114d38afeecSKuniyuki Iwashima inet6_cleanup_sock(sk); 115d38afeecSKuniyuki Iwashima inet_sock_destruct(sk); 116d38afeecSKuniyuki Iwashima } 1171651951eSKuniyuki Iwashima EXPORT_SYMBOL_GPL(inet6_sock_destruct); 118d38afeecSKuniyuki Iwashima 1193f378b68SEric Paris static int inet6_create(struct net *net, struct socket *sock, int protocol, 1203f378b68SEric Paris int kern) 1211da177e4SLinus Torvalds { 1221da177e4SLinus Torvalds struct inet_sock *inet; 1231da177e4SLinus Torvalds struct ipv6_pinfo *np; 1241da177e4SLinus Torvalds struct sock *sk; 1251da177e4SLinus Torvalds struct inet_protosw *answer; 1261da177e4SLinus Torvalds struct proto *answer_prot; 1271da177e4SLinus Torvalds unsigned char answer_flags; 128af1afe86SYOSHIFUJI Hideaki int try_loading_module = 0; 129af1afe86SYOSHIFUJI Hideaki int err; 1301da177e4SLinus Torvalds 13179462ad0SHannes Frederic Sowa if (protocol < 0 || protocol >= IPPROTO_MAX) 13279462ad0SHannes Frederic Sowa return -EINVAL; 13379462ad0SHannes Frederic Sowa 1341da177e4SLinus Torvalds /* Look for the requested type/protocol pair. */ 135af1afe86SYOSHIFUJI Hideaki lookup_protocol: 136af1afe86SYOSHIFUJI Hideaki err = -ESOCKTNOSUPPORT; 1371da177e4SLinus Torvalds rcu_read_lock(); 138696adfe8SPaul E. McKenney list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) { 1391da177e4SLinus Torvalds 140696adfe8SPaul E. McKenney err = 0; 1411da177e4SLinus Torvalds /* Check the non-wild match. */ 1421da177e4SLinus Torvalds if (protocol == answer->protocol) { 1431da177e4SLinus Torvalds if (protocol != IPPROTO_IP) 1441da177e4SLinus Torvalds break; 1451da177e4SLinus Torvalds } else { 1461da177e4SLinus Torvalds /* Check for the two wild cases. */ 1471da177e4SLinus Torvalds if (IPPROTO_IP == protocol) { 1481da177e4SLinus Torvalds protocol = answer->protocol; 1491da177e4SLinus Torvalds break; 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds if (IPPROTO_IP == answer->protocol) 1521da177e4SLinus Torvalds break; 1531da177e4SLinus Torvalds } 154af1afe86SYOSHIFUJI Hideaki err = -EPROTONOSUPPORT; 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 157696adfe8SPaul E. McKenney if (err) { 158af1afe86SYOSHIFUJI Hideaki if (try_loading_module < 2) { 159af1afe86SYOSHIFUJI Hideaki rcu_read_unlock(); 160af1afe86SYOSHIFUJI Hideaki /* 161af1afe86SYOSHIFUJI Hideaki * Be more specific, e.g. net-pf-10-proto-132-type-1 162af1afe86SYOSHIFUJI Hideaki * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM) 163af1afe86SYOSHIFUJI Hideaki */ 164af1afe86SYOSHIFUJI Hideaki if (++try_loading_module == 1) 165af1afe86SYOSHIFUJI Hideaki request_module("net-pf-%d-proto-%d-type-%d", 166af1afe86SYOSHIFUJI Hideaki PF_INET6, protocol, sock->type); 167af1afe86SYOSHIFUJI Hideaki /* 168af1afe86SYOSHIFUJI Hideaki * Fall back to generic, e.g. net-pf-10-proto-132 169af1afe86SYOSHIFUJI Hideaki * (net-pf-PF_INET6-proto-IPPROTO_SCTP) 170af1afe86SYOSHIFUJI Hideaki */ 171af1afe86SYOSHIFUJI Hideaki else 172af1afe86SYOSHIFUJI Hideaki request_module("net-pf-%d-proto-%d", 173af1afe86SYOSHIFUJI Hideaki PF_INET6, protocol); 174af1afe86SYOSHIFUJI Hideaki goto lookup_protocol; 175af1afe86SYOSHIFUJI Hideaki } else 1761da177e4SLinus Torvalds goto out_rcu_unlock; 177af1afe86SYOSHIFUJI Hideaki } 178af1afe86SYOSHIFUJI Hideaki 179af1afe86SYOSHIFUJI Hideaki err = -EPERM; 180af31f412SEric W. Biederman if (sock->type == SOCK_RAW && !kern && 181af31f412SEric W. Biederman !ns_capable(net->user_ns, CAP_NET_RAW)) 1821da177e4SLinus Torvalds goto out_rcu_unlock; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds sock->ops = answer->ops; 1851da177e4SLinus Torvalds answer_prot = answer->prot; 1861da177e4SLinus Torvalds answer_flags = answer->flags; 1871da177e4SLinus Torvalds rcu_read_unlock(); 1881da177e4SLinus Torvalds 18963159f29SIan Morris WARN_ON(!answer_prot->slab); 1901da177e4SLinus Torvalds 191af1afe86SYOSHIFUJI Hideaki err = -ENOBUFS; 19211aa9c28SEric W. Biederman sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot, kern); 19363159f29SIan Morris if (!sk) 1941da177e4SLinus Torvalds goto out; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds sock_init_data(sock, sk); 1971da177e4SLinus Torvalds 198af1afe86SYOSHIFUJI Hideaki err = 0; 1991da177e4SLinus Torvalds if (INET_PROTOSW_REUSE & answer_flags) 2004a17fd52SPavel Emelyanov sk->sk_reuse = SK_CAN_REUSE; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds inet = inet_sk(sk); 203b1c0356aSEric Dumazet inet_assign_bit(IS_ICSK, sk, INET_PROTOSW_ICSK & answer_flags); 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds if (SOCK_RAW == sock->type) { 206c720c7e8SEric Dumazet inet->inet_num = protocol; 2071da177e4SLinus Torvalds if (IPPROTO_RAW == protocol) 208cafbe182SEric Dumazet inet_set_bit(HDRINCL, sk); 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 211d38afeecSKuniyuki Iwashima sk->sk_destruct = inet6_sock_destruct; 2121da177e4SLinus Torvalds sk->sk_family = PF_INET6; 2131da177e4SLinus Torvalds sk->sk_protocol = protocol; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds sk->sk_backlog_rcv = answer->prot->backlog_rcv; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk); 2181da177e4SLinus Torvalds np->hop_limit = -1; 219f935aa9eSDavid S. Miller np->mcast_hops = IPV6_DEFAULT_MCASTHOPS; 2201da177e4SLinus Torvalds np->mc_loop = 1; 22115033f04SAndre Naujoks np->mc_all = 1; 2221da177e4SLinus Torvalds np->pmtudisc = IPV6_PMTUDISC_WANT; 223a346abe0SEric Dumazet np->repflow = net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ESTABLISHED; 2249fe516baSEric Dumazet sk->sk_ipv6only = net->ipv6.sysctl.bindv6only; 225c11204c7SKevin Yang sk->sk_txrehash = READ_ONCE(net->core.sysctl_txrehash); 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds /* Init the ipv4 part of the socket since we can have sockets 2281da177e4SLinus Torvalds * using v6 API for ipv4. 2291da177e4SLinus Torvalds */ 2301da177e4SLinus Torvalds inet->uc_ttl = -1; 2311da177e4SLinus Torvalds 232b09bde5cSEric Dumazet inet_set_bit(MC_LOOP, sk); 2331da177e4SLinus Torvalds inet->mc_ttl = 1; 2341da177e4SLinus Torvalds inet->mc_index = 0; 23588e2ca30STaehee Yoo RCU_INIT_POINTER(inet->mc_list, NULL); 2364c507d28SJiri Benc inet->rcv_tos = 0; 2371da177e4SLinus Torvalds 2380968d2a4SKuniyuki Iwashima if (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc)) 2391da177e4SLinus Torvalds inet->pmtudisc = IP_PMTUDISC_DONT; 2401da177e4SLinus Torvalds else 2411da177e4SLinus Torvalds inet->pmtudisc = IP_PMTUDISC_WANT; 2421da177e4SLinus Torvalds 243c720c7e8SEric Dumazet if (inet->inet_num) { 2441da177e4SLinus Torvalds /* It assumes that any protocol which allows 2451da177e4SLinus Torvalds * the user to assign a number at socket 2461da177e4SLinus Torvalds * creation time automatically shares. 2471da177e4SLinus Torvalds */ 248c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 249086c653fSCraig Gallek err = sk->sk_prot->hash(sk); 250086c653fSCraig Gallek if (err) { 251086c653fSCraig Gallek sk_common_release(sk); 252086c653fSCraig Gallek goto out; 253086c653fSCraig Gallek } 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds if (sk->sk_prot->init) { 256af1afe86SYOSHIFUJI Hideaki err = sk->sk_prot->init(sk); 257af1afe86SYOSHIFUJI Hideaki if (err) { 2581da177e4SLinus Torvalds sk_common_release(sk); 2591da177e4SLinus Torvalds goto out; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds } 26261023658SDavid Ahern 26361023658SDavid Ahern if (!kern) { 26461023658SDavid Ahern err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk); 26561023658SDavid Ahern if (err) { 26661023658SDavid Ahern sk_common_release(sk); 26761023658SDavid Ahern goto out; 26861023658SDavid Ahern } 26961023658SDavid Ahern } 2701da177e4SLinus Torvalds out: 271af1afe86SYOSHIFUJI Hideaki return err; 2721da177e4SLinus Torvalds out_rcu_unlock: 2731da177e4SLinus Torvalds rcu_read_unlock(); 2741da177e4SLinus Torvalds goto out; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 277032234d8SDavid Ahern static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len, 278cb0721c7SStanislav Fomichev u32 flags) 2793679d585SAndrey Ignatov { 2803679d585SAndrey Ignatov struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr; 2813679d585SAndrey Ignatov struct inet_sock *inet = inet_sk(sk); 2823679d585SAndrey Ignatov struct ipv6_pinfo *np = inet6_sk(sk); 2833679d585SAndrey Ignatov struct net *net = sock_net(sk); 2843679d585SAndrey Ignatov __be32 v4addr = 0; 2853679d585SAndrey Ignatov unsigned short snum; 2863679d585SAndrey Ignatov bool saved_ipv6only; 2873679d585SAndrey Ignatov int addr_type = 0; 2883679d585SAndrey Ignatov int err = 0; 2893679d585SAndrey Ignatov 2905a079c30SMarcus Meissner if (addr->sin6_family != AF_INET6) 291c349a528SMarcus Meissner return -EAFNOSUPPORT; 2925a079c30SMarcus Meissner 2931da177e4SLinus Torvalds addr_type = ipv6_addr_type(&addr->sin6_addr); 2943679d585SAndrey Ignatov if ((addr_type & IPV6_ADDR_MULTICAST) && sk->sk_type == SOCK_STREAM) 2951da177e4SLinus Torvalds return -EINVAL; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds snum = ntohs(addr->sin6_port); 29877241217SStanislav Fomichev if (!(flags & BIND_NO_CAP_NET_BIND_SERVICE) && 29977241217SStanislav Fomichev snum && inet_port_requires_bind_service(net, snum) && 3004548b683SKrister Johansen !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) 3011da177e4SLinus Torvalds return -EACCES; 3021da177e4SLinus Torvalds 303cb0721c7SStanislav Fomichev if (flags & BIND_WITH_LOCK) 3041da177e4SLinus Torvalds lock_sock(sk); 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds /* Check these errors (active socket, double bind). */ 307c720c7e8SEric Dumazet if (sk->sk_state != TCP_CLOSE || inet->inet_num) { 3081da177e4SLinus Torvalds err = -EINVAL; 3091da177e4SLinus Torvalds goto out; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds /* Check if the address belongs to the host. */ 3131da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_MAPPED) { 314ec90ad33SDavid Ahern struct net_device *dev = NULL; 31563d9950bSVlad Yasevich int chk_addr_ret; 31663d9950bSVlad Yasevich 317783ed5a7SVlad Yasevich /* Binding to v4-mapped address on a v6-only socket 318783ed5a7SVlad Yasevich * makes no sense 319783ed5a7SVlad Yasevich */ 32081ee0eb6SKuniyuki Iwashima if (ipv6_only_sock(sk)) { 321783ed5a7SVlad Yasevich err = -EINVAL; 322783ed5a7SVlad Yasevich goto out; 323783ed5a7SVlad Yasevich } 32463d9950bSVlad Yasevich 325d4a7e9bbSDavid Ahern rcu_read_lock(); 326ec90ad33SDavid Ahern if (sk->sk_bound_dev_if) { 327ec90ad33SDavid Ahern dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if); 328ec90ad33SDavid Ahern if (!dev) { 329ec90ad33SDavid Ahern err = -ENODEV; 330d4a7e9bbSDavid Ahern goto out_unlock; 331ec90ad33SDavid Ahern } 332ec90ad33SDavid Ahern } 333ec90ad33SDavid Ahern 334a34f0b31SUwe Kleine-König /* Reproduce AF_INET checks to make the bindings consistent */ 3351da177e4SLinus Torvalds v4addr = addr->sin6_addr.s6_addr32[3]; 336ec90ad33SDavid Ahern chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr); 337d4a7e9bbSDavid Ahern rcu_read_unlock(); 338d4a7e9bbSDavid Ahern 3398ff978b8SRiccardo Paolo Bestetti if (!inet_addr_valid_or_nonlocal(net, inet, v4addr, 3408ff978b8SRiccardo Paolo Bestetti chk_addr_ret)) { 341ca6982b8SBruno Prémont err = -EADDRNOTAVAIL; 3421da177e4SLinus Torvalds goto out; 343ca6982b8SBruno Prémont } 3441da177e4SLinus Torvalds } else { 3451da177e4SLinus Torvalds if (addr_type != IPV6_ADDR_ANY) { 3461da177e4SLinus Torvalds struct net_device *dev = NULL; 3471da177e4SLinus Torvalds 34816ba5e8eSEric Dumazet rcu_read_lock(); 349842df073SHannes Frederic Sowa if (__ipv6_addr_needs_scope_id(addr_type)) { 3501da177e4SLinus Torvalds if (addr_len >= sizeof(struct sockaddr_in6) && 3511da177e4SLinus Torvalds addr->sin6_scope_id) { 3521da177e4SLinus Torvalds /* Override any existing binding, if another one 3531da177e4SLinus Torvalds * is supplied by user. 3541da177e4SLinus Torvalds */ 3551da177e4SLinus Torvalds sk->sk_bound_dev_if = addr->sin6_scope_id; 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds /* Binding to link-local address requires an interface */ 3591da177e4SLinus Torvalds if (!sk->sk_bound_dev_if) { 3601da177e4SLinus Torvalds err = -EINVAL; 36116ba5e8eSEric Dumazet goto out_unlock; 3621da177e4SLinus Torvalds } 363c5ee0663SDavid Ahern } 364c5ee0663SDavid Ahern 365c5ee0663SDavid Ahern if (sk->sk_bound_dev_if) { 36616ba5e8eSEric Dumazet dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if); 3671da177e4SLinus Torvalds if (!dev) { 3681da177e4SLinus Torvalds err = -ENODEV; 36916ba5e8eSEric Dumazet goto out_unlock; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds /* ipv4 addr of the socket is invalid. Only the 3741da177e4SLinus Torvalds * unspecified and mapped address have a v4 equivalent. 3751da177e4SLinus Torvalds */ 3761da177e4SLinus Torvalds v4addr = LOOPBACK4_IPV6; 3771da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST)) { 37883ba4645SVincent Bernat if (!ipv6_can_nonlocal_bind(net, inet) && 3790a513f6aSBalazs Scheidler !ipv6_chk_addr(net, &addr->sin6_addr, 380bfeade08SDaniel Lezcano dev, 0)) { 3811da177e4SLinus Torvalds err = -EADDRNOTAVAIL; 38216ba5e8eSEric Dumazet goto out_unlock; 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds } 38516ba5e8eSEric Dumazet rcu_read_unlock(); 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 389c720c7e8SEric Dumazet inet->inet_rcv_saddr = v4addr; 390c720c7e8SEric Dumazet inet->inet_saddr = v4addr; 3911da177e4SLinus Torvalds 392efe4208fSEric Dumazet sk->sk_v6_rcv_saddr = addr->sin6_addr; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST)) 3954e3fd7a0SAlexey Dobriyan np->saddr = addr->sin6_addr; 3961da177e4SLinus Torvalds 3977ece54a6SMartin KaFai Lau saved_ipv6only = sk->sk_ipv6only; 3987ece54a6SMartin KaFai Lau if (addr_type != IPV6_ADDR_ANY && addr_type != IPV6_ADDR_MAPPED) 3997ece54a6SMartin KaFai Lau sk->sk_ipv6only = 1; 4007ece54a6SMartin KaFai Lau 4011da177e4SLinus Torvalds /* Make sure we are allowed to bind here. */ 402*ca571e2eSEric Dumazet if (snum || !(inet_test_bit(BIND_ADDRESS_NO_PORT, sk) || 403cb0721c7SStanislav Fomichev (flags & BIND_FORCE_ADDRESS_NO_PORT))) { 4047a7160edSKuniyuki Iwashima err = sk->sk_prot->get_port(sk, snum); 4057a7160edSKuniyuki Iwashima if (err) { 4067ece54a6SMartin KaFai Lau sk->sk_ipv6only = saved_ipv6only; 4071da177e4SLinus Torvalds inet_reset_saddr(sk); 4081da177e4SLinus Torvalds goto out; 4091da177e4SLinus Torvalds } 4108086fbafSStanislav Fomichev if (!(flags & BIND_FROM_BPF)) { 411aac3fc32SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk); 412aac3fc32SAndrey Ignatov if (err) { 413aac3fc32SAndrey Ignatov sk->sk_ipv6only = saved_ipv6only; 414aac3fc32SAndrey Ignatov inet_reset_saddr(sk); 41591a760b2SMenglong Dong if (sk->sk_prot->put_port) 41691a760b2SMenglong Dong sk->sk_prot->put_port(sk); 417aac3fc32SAndrey Ignatov goto out; 418aac3fc32SAndrey Ignatov } 419aac3fc32SAndrey Ignatov } 4208086fbafSStanislav Fomichev } 4211da177e4SLinus Torvalds 4227ece54a6SMartin KaFai Lau if (addr_type != IPV6_ADDR_ANY) 4231da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDADDR_LOCK; 4241da177e4SLinus Torvalds if (snum) 4251da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 426c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num); 427c720c7e8SEric Dumazet inet->inet_dport = 0; 428c720c7e8SEric Dumazet inet->inet_daddr = 0; 4291da177e4SLinus Torvalds out: 430cb0721c7SStanislav Fomichev if (flags & BIND_WITH_LOCK) 4311da177e4SLinus Torvalds release_sock(sk); 4321da177e4SLinus Torvalds return err; 43316ba5e8eSEric Dumazet out_unlock: 43416ba5e8eSEric Dumazet rcu_read_unlock(); 43516ba5e8eSEric Dumazet goto out; 4361da177e4SLinus Torvalds } 4377159039aSYOSHIFUJI Hideaki 438e6d360ffSPaolo Abeni int inet6_bind_sk(struct sock *sk, struct sockaddr *uaddr, int addr_len) 439032234d8SDavid Ahern { 44077241217SStanislav Fomichev u32 flags = BIND_WITH_LOCK; 441086d4905SEric Dumazet const struct proto *prot; 442032234d8SDavid Ahern int err = 0; 443032234d8SDavid Ahern 444086d4905SEric Dumazet /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 445086d4905SEric Dumazet prot = READ_ONCE(sk->sk_prot); 446032234d8SDavid Ahern /* If the socket has its own bind function then use it. */ 447086d4905SEric Dumazet if (prot->bind) 448086d4905SEric Dumazet return prot->bind(sk, uaddr, addr_len); 449032234d8SDavid Ahern 450032234d8SDavid Ahern if (addr_len < SIN6_LEN_RFC2133) 451032234d8SDavid Ahern return -EINVAL; 452032234d8SDavid Ahern 453032234d8SDavid Ahern /* BPF prog is run before any checks are done so that if the prog 454032234d8SDavid Ahern * changes context in a wrong way it will be caught. 455032234d8SDavid Ahern */ 45677241217SStanislav Fomichev err = BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr, 4576fc88c35SDave Marchevsky CGROUP_INET6_BIND, &flags); 458032234d8SDavid Ahern if (err) 459032234d8SDavid Ahern return err; 460032234d8SDavid Ahern 46177241217SStanislav Fomichev return __inet6_bind(sk, uaddr, addr_len, flags); 462032234d8SDavid Ahern } 463e6d360ffSPaolo Abeni 464e6d360ffSPaolo Abeni /* bind for INET6 API */ 465e6d360ffSPaolo Abeni int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 466e6d360ffSPaolo Abeni { 467e6d360ffSPaolo Abeni return inet6_bind_sk(sock->sk, uaddr, addr_len); 468e6d360ffSPaolo Abeni } 469032234d8SDavid Ahern EXPORT_SYMBOL(inet6_bind); 470032234d8SDavid Ahern 4711da177e4SLinus Torvalds int inet6_release(struct socket *sock) 4721da177e4SLinus Torvalds { 4731da177e4SLinus Torvalds struct sock *sk = sock->sk; 4741da177e4SLinus Torvalds 47563159f29SIan Morris if (!sk) 4761da177e4SLinus Torvalds return -EINVAL; 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds /* Free mc lists */ 4791da177e4SLinus Torvalds ipv6_sock_mc_close(sk); 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds /* Free ac lists */ 4821da177e4SLinus Torvalds ipv6_sock_ac_close(sk); 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds return inet_release(sock); 4851da177e4SLinus Torvalds } 4867159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release); 4877159039aSYOSHIFUJI Hideaki 4881f8c4eebSKuniyuki Iwashima void inet6_cleanup_sock(struct sock *sk) 4891da177e4SLinus Torvalds { 4901da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4911da177e4SLinus Torvalds struct sk_buff *skb; 4921da177e4SLinus Torvalds struct ipv6_txoptions *opt; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds /* Release rx options */ 4951da177e4SLinus Torvalds 496647c0c70SEldad Zack skb = xchg(&np->pktoptions, NULL); 4971da177e4SLinus Torvalds kfree_skb(skb); 4981da177e4SLinus Torvalds 499647c0c70SEldad Zack skb = xchg(&np->rxpmtu, NULL); 5004b340ae2SBrian Haley kfree_skb(skb); 5014b340ae2SBrian Haley 5021da177e4SLinus Torvalds /* Free flowlabels */ 5031da177e4SLinus Torvalds fl6_free_socklist(sk); 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds /* Free tx options */ 5061da177e4SLinus Torvalds 50745f6fad8SEric Dumazet opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL); 50845f6fad8SEric Dumazet if (opt) { 50945f6fad8SEric Dumazet atomic_sub(opt->tot_len, &sk->sk_omem_alloc); 51045f6fad8SEric Dumazet txopt_put(opt); 51145f6fad8SEric Dumazet } 5121da177e4SLinus Torvalds } 51321985f43SKuniyuki Iwashima EXPORT_SYMBOL_GPL(inet6_cleanup_sock); 51421985f43SKuniyuki Iwashima 5151da177e4SLinus Torvalds /* 5161da177e4SLinus Torvalds * This does both peername and sockname. 5171da177e4SLinus Torvalds */ 5181da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr, 5199b2c45d4SDenys Vlasenko int peer) 5201da177e4SLinus Torvalds { 5211da177e4SLinus Torvalds struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr; 5221da177e4SLinus Torvalds struct sock *sk = sock->sk; 5231da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 5241da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds sin->sin6_family = AF_INET6; 5271da177e4SLinus Torvalds sin->sin6_flowinfo = 0; 5281da177e4SLinus Torvalds sin->sin6_scope_id = 0; 5299dfc685eSEric Dumazet lock_sock(sk); 5301da177e4SLinus Torvalds if (peer) { 5319dfc685eSEric Dumazet if (!inet->inet_dport || 5329dfc685eSEric Dumazet (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && 5339dfc685eSEric Dumazet peer == 1)) { 5349dfc685eSEric Dumazet release_sock(sk); 5351da177e4SLinus Torvalds return -ENOTCONN; 5369dfc685eSEric Dumazet } 537c720c7e8SEric Dumazet sin->sin6_port = inet->inet_dport; 538efe4208fSEric Dumazet sin->sin6_addr = sk->sk_v6_daddr; 5391da177e4SLinus Torvalds if (np->sndflow) 5401da177e4SLinus Torvalds sin->sin6_flowinfo = np->flow_label; 5419dfc685eSEric Dumazet BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, 5429dfc685eSEric Dumazet CGROUP_INET6_GETPEERNAME); 5431da177e4SLinus Torvalds } else { 544efe4208fSEric Dumazet if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) 5454e3fd7a0SAlexey Dobriyan sin->sin6_addr = np->saddr; 5461da177e4SLinus Torvalds else 547efe4208fSEric Dumazet sin->sin6_addr = sk->sk_v6_rcv_saddr; 548c720c7e8SEric Dumazet sin->sin6_port = inet->inet_sport; 5499dfc685eSEric Dumazet BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, 5509dfc685eSEric Dumazet CGROUP_INET6_GETSOCKNAME); 551a9ed15daSStanislav Fomichev } 552842df073SHannes Frederic Sowa sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr, 553842df073SHannes Frederic Sowa sk->sk_bound_dev_if); 5549dfc685eSEric Dumazet release_sock(sk); 5559b2c45d4SDenys Vlasenko return sizeof(*sin); 5561da177e4SLinus Torvalds } 5577159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname); 5587159039aSYOSHIFUJI Hideaki 5591da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 5601da177e4SLinus Torvalds { 5617c1552daSChristoph Hellwig void __user *argp = (void __user *)arg; 5621da177e4SLinus Torvalds struct sock *sk = sock->sk; 5633b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 564086d4905SEric Dumazet const struct proto *prot; 5651da177e4SLinus Torvalds 566647c0c70SEldad Zack switch (cmd) { 5671da177e4SLinus Torvalds case SIOCADDRT: 5687c1552daSChristoph Hellwig case SIOCDELRT: { 5697c1552daSChristoph Hellwig struct in6_rtmsg rtmsg; 5701da177e4SLinus Torvalds 5717c1552daSChristoph Hellwig if (copy_from_user(&rtmsg, argp, sizeof(rtmsg))) 5727c1552daSChristoph Hellwig return -EFAULT; 5737c1552daSChristoph Hellwig return ipv6_route_ioctl(net, cmd, &rtmsg); 5747c1552daSChristoph Hellwig } 5751da177e4SLinus Torvalds case SIOCSIFADDR: 5767c1552daSChristoph Hellwig return addrconf_add_ifaddr(net, argp); 5771da177e4SLinus Torvalds case SIOCDIFADDR: 5787c1552daSChristoph Hellwig return addrconf_del_ifaddr(net, argp); 5791da177e4SLinus Torvalds case SIOCSIFDSTADDR: 5807c1552daSChristoph Hellwig return addrconf_set_dstaddr(net, argp); 5811da177e4SLinus Torvalds default: 582086d4905SEric Dumazet /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 583086d4905SEric Dumazet prot = READ_ONCE(sk->sk_prot); 584086d4905SEric Dumazet if (!prot->ioctl) 585b5e5fa5eSChristoph Hellwig return -ENOIOCTLCMD; 586e1d001faSBreno Leitao return sk_ioctl(sk, cmd, (void __user *)arg); 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds /*NOTREACHED*/ 589a02cec21SEric Dumazet return 0; 5901da177e4SLinus Torvalds } 5917159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl); 5927159039aSYOSHIFUJI Hideaki 5933986912fSChristoph Hellwig #ifdef CONFIG_COMPAT 5943986912fSChristoph Hellwig struct compat_in6_rtmsg { 5953986912fSChristoph Hellwig struct in6_addr rtmsg_dst; 5963986912fSChristoph Hellwig struct in6_addr rtmsg_src; 5973986912fSChristoph Hellwig struct in6_addr rtmsg_gateway; 5983986912fSChristoph Hellwig u32 rtmsg_type; 5993986912fSChristoph Hellwig u16 rtmsg_dst_len; 6003986912fSChristoph Hellwig u16 rtmsg_src_len; 6013986912fSChristoph Hellwig u32 rtmsg_metric; 6023986912fSChristoph Hellwig u32 rtmsg_info; 6033986912fSChristoph Hellwig u32 rtmsg_flags; 6043986912fSChristoph Hellwig s32 rtmsg_ifindex; 6053986912fSChristoph Hellwig }; 6063986912fSChristoph Hellwig 6073986912fSChristoph Hellwig static int inet6_compat_routing_ioctl(struct sock *sk, unsigned int cmd, 6083986912fSChristoph Hellwig struct compat_in6_rtmsg __user *ur) 6093986912fSChristoph Hellwig { 6103986912fSChristoph Hellwig struct in6_rtmsg rt; 6113986912fSChristoph Hellwig 6123986912fSChristoph Hellwig if (copy_from_user(&rt.rtmsg_dst, &ur->rtmsg_dst, 6133986912fSChristoph Hellwig 3 * sizeof(struct in6_addr)) || 6143986912fSChristoph Hellwig get_user(rt.rtmsg_type, &ur->rtmsg_type) || 6153986912fSChristoph Hellwig get_user(rt.rtmsg_dst_len, &ur->rtmsg_dst_len) || 6163986912fSChristoph Hellwig get_user(rt.rtmsg_src_len, &ur->rtmsg_src_len) || 6173986912fSChristoph Hellwig get_user(rt.rtmsg_metric, &ur->rtmsg_metric) || 6183986912fSChristoph Hellwig get_user(rt.rtmsg_info, &ur->rtmsg_info) || 6193986912fSChristoph Hellwig get_user(rt.rtmsg_flags, &ur->rtmsg_flags) || 6203986912fSChristoph Hellwig get_user(rt.rtmsg_ifindex, &ur->rtmsg_ifindex)) 6213986912fSChristoph Hellwig return -EFAULT; 6223986912fSChristoph Hellwig 6233986912fSChristoph Hellwig 6243986912fSChristoph Hellwig return ipv6_route_ioctl(sock_net(sk), cmd, &rt); 6253986912fSChristoph Hellwig } 6263986912fSChristoph Hellwig 6273986912fSChristoph Hellwig int inet6_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 6283986912fSChristoph Hellwig { 6293986912fSChristoph Hellwig void __user *argp = compat_ptr(arg); 6303986912fSChristoph Hellwig struct sock *sk = sock->sk; 6313986912fSChristoph Hellwig 6323986912fSChristoph Hellwig switch (cmd) { 6333986912fSChristoph Hellwig case SIOCADDRT: 6343986912fSChristoph Hellwig case SIOCDELRT: 6353986912fSChristoph Hellwig return inet6_compat_routing_ioctl(sk, cmd, argp); 6363986912fSChristoph Hellwig default: 6373986912fSChristoph Hellwig return -ENOIOCTLCMD; 6383986912fSChristoph Hellwig } 6393986912fSChristoph Hellwig } 6403986912fSChristoph Hellwig EXPORT_SYMBOL_GPL(inet6_compat_ioctl); 6413986912fSChristoph Hellwig #endif /* CONFIG_COMPAT */ 6423986912fSChristoph Hellwig 643164c51feSPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udpv6_sendmsg(struct sock *, struct msghdr *, 644164c51feSPaolo Abeni size_t)); 64568ab5d14SPaolo Abeni int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) 64668ab5d14SPaolo Abeni { 64768ab5d14SPaolo Abeni struct sock *sk = sock->sk; 648086d4905SEric Dumazet const struct proto *prot; 64968ab5d14SPaolo Abeni 65068ab5d14SPaolo Abeni if (unlikely(inet_send_prepare(sk))) 65168ab5d14SPaolo Abeni return -EAGAIN; 65268ab5d14SPaolo Abeni 653086d4905SEric Dumazet /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 654086d4905SEric Dumazet prot = READ_ONCE(sk->sk_prot); 655086d4905SEric Dumazet return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg, 656164c51feSPaolo Abeni sk, msg, size); 65768ab5d14SPaolo Abeni } 65868ab5d14SPaolo Abeni 659164c51feSPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udpv6_recvmsg(struct sock *, struct msghdr *, 660ec095263SOliver Hartkopp size_t, int, int *)); 66168ab5d14SPaolo Abeni int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 66268ab5d14SPaolo Abeni int flags) 66368ab5d14SPaolo Abeni { 66468ab5d14SPaolo Abeni struct sock *sk = sock->sk; 665086d4905SEric Dumazet const struct proto *prot; 66668ab5d14SPaolo Abeni int addr_len = 0; 66768ab5d14SPaolo Abeni int err; 66868ab5d14SPaolo Abeni 66968ab5d14SPaolo Abeni if (likely(!(flags & MSG_ERRQUEUE))) 67068ab5d14SPaolo Abeni sock_rps_record_flow(sk); 67168ab5d14SPaolo Abeni 672086d4905SEric Dumazet /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 673086d4905SEric Dumazet prot = READ_ONCE(sk->sk_prot); 674086d4905SEric Dumazet err = INDIRECT_CALL_2(prot->recvmsg, tcp_recvmsg, udpv6_recvmsg, 675ec095263SOliver Hartkopp sk, msg, size, flags, &addr_len); 67668ab5d14SPaolo Abeni if (err >= 0) 67768ab5d14SPaolo Abeni msg->msg_namelen = addr_len; 67868ab5d14SPaolo Abeni return err; 67968ab5d14SPaolo Abeni } 68068ab5d14SPaolo Abeni 68190ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = { 6821da177e4SLinus Torvalds .family = PF_INET6, 6831da177e4SLinus Torvalds .owner = THIS_MODULE, 6841da177e4SLinus Torvalds .release = inet6_release, 6851da177e4SLinus Torvalds .bind = inet6_bind, 6861da177e4SLinus Torvalds .connect = inet_stream_connect, /* ok */ 6871da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */ 6881da177e4SLinus Torvalds .accept = inet_accept, /* ok */ 6891da177e4SLinus Torvalds .getname = inet6_getname, 690a11e1d43SLinus Torvalds .poll = tcp_poll, /* ok */ 6911da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */ 692c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp, 6931da177e4SLinus Torvalds .listen = inet_listen, /* ok */ 6941da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */ 6951da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */ 6961da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */ 69768ab5d14SPaolo Abeni .sendmsg = inet6_sendmsg, /* retpoline's sake */ 69868ab5d14SPaolo Abeni .recvmsg = inet6_recvmsg, /* retpoline's sake */ 69905255b82SEric Dumazet #ifdef CONFIG_MMU 70093ab6cc6SEric Dumazet .mmap = tcp_mmap, 70105255b82SEric Dumazet #endif 7021d7e4538SDavid Howells .splice_eof = inet_splice_eof, 70345f91bdcSJohn Fastabend .sendmsg_locked = tcp_sendmsg_locked, 704a0974dd3SJens Axboe .splice_read = tcp_splice_read, 70532035585STom Herbert .read_sock = tcp_read_sock, 706965b57b4SCong Wang .read_skb = tcp_read_skb, 70732035585STom Herbert .peek_len = tcp_peek_len, 7083fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 7093986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 7103fdadf7dSDmitry Mishin #endif 711d1361840SEric Dumazet .set_rcvlowat = tcp_set_rcvlowat, 7121da177e4SLinus Torvalds }; 7131da177e4SLinus Torvalds 71490ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = { 7151da177e4SLinus Torvalds .family = PF_INET6, 7161da177e4SLinus Torvalds .owner = THIS_MODULE, 7171da177e4SLinus Torvalds .release = inet6_release, 7181da177e4SLinus Torvalds .bind = inet6_bind, 7191da177e4SLinus Torvalds .connect = inet_dgram_connect, /* ok */ 7201da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */ 7211da177e4SLinus Torvalds .accept = sock_no_accept, /* a do nothing */ 7221da177e4SLinus Torvalds .getname = inet6_getname, 723a11e1d43SLinus Torvalds .poll = udp_poll, /* ok */ 7241da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */ 725c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp, 7261da177e4SLinus Torvalds .listen = sock_no_listen, /* ok */ 7271da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */ 7281da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */ 7291da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */ 73068ab5d14SPaolo Abeni .sendmsg = inet6_sendmsg, /* retpoline's sake */ 73168ab5d14SPaolo Abeni .recvmsg = inet6_recvmsg, /* retpoline's sake */ 732965b57b4SCong Wang .read_skb = udp_read_skb, 7331da177e4SLinus Torvalds .mmap = sock_no_mmap, 734627d2d6bSsamanthakumar .set_peek_off = sk_set_peek_off, 735543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 7363986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 737543d9cfeSArnaldo Carvalho de Melo #endif 7381da177e4SLinus Torvalds }; 7391da177e4SLinus Torvalds 740ec1b4cf7SStephen Hemminger static const struct net_proto_family inet6_family_ops = { 7411da177e4SLinus Torvalds .family = PF_INET6, 7421da177e4SLinus Torvalds .create = inet6_create, 7431da177e4SLinus Torvalds .owner = THIS_MODULE, 7441da177e4SLinus Torvalds }; 7451da177e4SLinus Torvalds 74687c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p) 7471da177e4SLinus Torvalds { 7481da177e4SLinus Torvalds struct list_head *lh; 7491da177e4SLinus Torvalds struct inet_protosw *answer; 7501da177e4SLinus Torvalds struct list_head *last_perm; 75187c3efbfSDaniel Lezcano int protocol = p->protocol; 75287c3efbfSDaniel Lezcano int ret; 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock); 7551da177e4SLinus Torvalds 75687c3efbfSDaniel Lezcano ret = -EINVAL; 7571da177e4SLinus Torvalds if (p->type >= SOCK_MAX) 7581da177e4SLinus Torvalds goto out_illegal; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds /* If we are trying to override a permanent protocol, bail. */ 7611da177e4SLinus Torvalds answer = NULL; 76287c3efbfSDaniel Lezcano ret = -EPERM; 7631da177e4SLinus Torvalds last_perm = &inetsw6[p->type]; 7641da177e4SLinus Torvalds list_for_each(lh, &inetsw6[p->type]) { 7651da177e4SLinus Torvalds answer = list_entry(lh, struct inet_protosw, list); 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds /* Check only the non-wild match. */ 7681da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & answer->flags) { 7691da177e4SLinus Torvalds if (protocol == answer->protocol) 7701da177e4SLinus Torvalds break; 7711da177e4SLinus Torvalds last_perm = lh; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds answer = NULL; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds if (answer) 7771da177e4SLinus Torvalds goto out_permanent; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds /* Add the new entry after the last permanent entry if any, so that 7801da177e4SLinus Torvalds * the new entry does not override a permanent entry when matched with 7811da177e4SLinus Torvalds * a wild-card protocol. But it is allowed to override any existing 7821da177e4SLinus Torvalds * non-permanent entry. This means that when we remove this entry, the 7831da177e4SLinus Torvalds * system automatically returns to the old behavior. 7841da177e4SLinus Torvalds */ 7851da177e4SLinus Torvalds list_add_rcu(&p->list, last_perm); 78687c3efbfSDaniel Lezcano ret = 0; 7871da177e4SLinus Torvalds out: 7881da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock); 78987c3efbfSDaniel Lezcano return ret; 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds out_permanent: 792f3213831SJoe Perches pr_err("Attempt to override permanent protocol %d\n", protocol); 7931da177e4SLinus Torvalds goto out; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds out_illegal: 796f3213831SJoe Perches pr_err("Ignoring attempt to register invalid socket type %d\n", 7971da177e4SLinus Torvalds p->type); 7981da177e4SLinus Torvalds goto out; 7991da177e4SLinus Torvalds } 8007159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw); 8017159039aSYOSHIFUJI Hideaki 8021da177e4SLinus Torvalds void 8031da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p) 8041da177e4SLinus Torvalds { 8051da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & p->flags) { 806f3213831SJoe Perches pr_err("Attempt to unregister permanent protocol %d\n", 8071da177e4SLinus Torvalds p->protocol); 8081da177e4SLinus Torvalds } else { 8091da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock); 8101da177e4SLinus Torvalds list_del_rcu(&p->list); 8111da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock); 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds synchronize_net(); 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds } 8167159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw); 8177159039aSYOSHIFUJI Hideaki 818b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk) 819b9750ce1SArnaldo Carvalho de Melo { 820b9750ce1SArnaldo Carvalho de Melo struct ipv6_pinfo *np = inet6_sk(sk); 82168d0c6d3SDavid S. Miller struct dst_entry *dst; 822b9750ce1SArnaldo Carvalho de Melo 823b9750ce1SArnaldo Carvalho de Melo dst = __sk_dst_check(sk, np->dst_cookie); 824b9750ce1SArnaldo Carvalho de Melo 82563159f29SIan Morris if (!dst) { 826b9750ce1SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 82720c59de2SArnaud Ebalard struct in6_addr *final_p, final; 8284c9483b2SDavid S. Miller struct flowi6 fl6; 829b9750ce1SArnaldo Carvalho de Melo 8304c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 8314c9483b2SDavid S. Miller fl6.flowi6_proto = sk->sk_protocol; 832efe4208fSEric Dumazet fl6.daddr = sk->sk_v6_daddr; 8334e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 8344c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 8354c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 8364c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark; 8371958b856SDavid S. Miller fl6.fl6_dport = inet->inet_dport; 8381958b856SDavid S. Miller fl6.fl6_sport = inet->inet_sport; 839e2d118a1SLorenzo Colitti fl6.flowi6_uid = sk->sk_uid; 8403df98d79SPaul Moore security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6)); 841b9750ce1SArnaldo Carvalho de Melo 84245f6fad8SEric Dumazet rcu_read_lock(); 84345f6fad8SEric Dumazet final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt), 84445f6fad8SEric Dumazet &final); 84545f6fad8SEric Dumazet rcu_read_unlock(); 846b9750ce1SArnaldo Carvalho de Melo 847c4e85f73SSabrina Dubroca dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p); 84868d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 849b9750ce1SArnaldo Carvalho de Melo sk->sk_route_caps = 0; 8502f2d9972SEric Dumazet WRITE_ONCE(sk->sk_err_soft, -PTR_ERR(dst)); 85168d0c6d3SDavid S. Miller return PTR_ERR(dst); 852b9750ce1SArnaldo Carvalho de Melo } 853b9750ce1SArnaldo Carvalho de Melo 8546bd4f355SEric Dumazet ip6_dst_store(sk, dst, NULL, NULL); 855b9750ce1SArnaldo Carvalho de Melo } 856b9750ce1SArnaldo Carvalho de Melo 857b9750ce1SArnaldo Carvalho de Melo return 0; 858b9750ce1SArnaldo Carvalho de Melo } 859b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header); 860b9750ce1SArnaldo Carvalho de Melo 861a224772dSEric Dumazet bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb, 862a224772dSEric Dumazet const struct inet6_skb_parm *opt) 863399c07deSArnaldo Carvalho de Melo { 86492113bfdSEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk); 865399c07deSArnaldo Carvalho de Melo 866399c07deSArnaldo Carvalho de Melo if (np->rxopt.all) { 8678b58a398SFlorian Westphal if (((opt->flags & IP6SKB_HOPBYHOP) && 8688b58a398SFlorian Westphal (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) || 869ac1eabcaSFlorent Fourcot (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) && 870399c07deSArnaldo Carvalho de Melo np->rxopt.bits.rxflow) || 871399c07deSArnaldo Carvalho de Melo (opt->srcrt && (np->rxopt.bits.srcrt || 872399c07deSArnaldo Carvalho de Melo np->rxopt.bits.osrcrt)) || 873399c07deSArnaldo Carvalho de Melo ((opt->dst1 || opt->dst0) && 874399c07deSArnaldo Carvalho de Melo (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts))) 87592113bfdSEric Dumazet return true; 876399c07deSArnaldo Carvalho de Melo } 87792113bfdSEric Dumazet return false; 878399c07deSArnaldo Carvalho de Melo } 879399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted); 880399c07deSArnaldo Carvalho de Melo 8817546dd97SStephen Hemminger static struct packet_type ipv6_packet_type __read_mostly = { 88209640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_IPV6), 883662397fdSYOSHIFUJI Hideaki .func = ipv6_rcv, 884d8269e2cSEdward Cree .list_func = ipv6_list_rcv, 88522061d80SVlad Yasevich }; 88622061d80SVlad Yasevich 887662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void) 888662397fdSYOSHIFUJI Hideaki { 889662397fdSYOSHIFUJI Hideaki dev_add_pack(&ipv6_packet_type); 890662397fdSYOSHIFUJI Hideaki return 0; 891662397fdSYOSHIFUJI Hideaki } 892662397fdSYOSHIFUJI Hideaki 893662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void) 894662397fdSYOSHIFUJI Hideaki { 895662397fdSYOSHIFUJI Hideaki dev_remove_pack(&ipv6_packet_type); 896662397fdSYOSHIFUJI Hideaki } 897662397fdSYOSHIFUJI Hideaki 898e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net) 899e43291cbSDenis V. Lunev { 900827da44cSJohn Stultz int i; 901827da44cSJohn Stultz 902698365faSWANG Cong net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib); 903698365faSWANG Cong if (!net->mib.udp_stats_in6) 9040c7ed677SDenis V. Lunev return -ENOMEM; 905698365faSWANG Cong net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib); 906698365faSWANG Cong if (!net->mib.udplite_stats_in6) 907be713a44SDenis V. Lunev goto err_udplite_mib; 908698365faSWANG Cong net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib); 909698365faSWANG Cong if (!net->mib.ipv6_statistics) 9109261e537SDenis V. Lunev goto err_ip_mib; 911827da44cSJohn Stultz 912827da44cSJohn Stultz for_each_possible_cpu(i) { 913827da44cSJohn Stultz struct ipstats_mib *af_inet6_stats; 914698365faSWANG Cong af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i); 915827da44cSJohn Stultz u64_stats_init(&af_inet6_stats->syncp); 916827da44cSJohn Stultz } 917827da44cSJohn Stultz 918827da44cSJohn Stultz 919698365faSWANG Cong net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib); 920698365faSWANG Cong if (!net->mib.icmpv6_statistics) 9219261e537SDenis V. Lunev goto err_icmp_mib; 9222a24444fSEric Dumazet net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib), 9232a24444fSEric Dumazet GFP_KERNEL); 9242a24444fSEric Dumazet if (!net->mib.icmpv6msg_statistics) 9259261e537SDenis V. Lunev goto err_icmpmsg_mib; 926e43291cbSDenis V. Lunev return 0; 927be713a44SDenis V. Lunev 9289261e537SDenis V. Lunev err_icmpmsg_mib: 929698365faSWANG Cong free_percpu(net->mib.icmpv6_statistics); 9309261e537SDenis V. Lunev err_icmp_mib: 931698365faSWANG Cong free_percpu(net->mib.ipv6_statistics); 9329261e537SDenis V. Lunev err_ip_mib: 933698365faSWANG Cong free_percpu(net->mib.udplite_stats_in6); 934be713a44SDenis V. Lunev err_udplite_mib: 935698365faSWANG Cong free_percpu(net->mib.udp_stats_in6); 936be713a44SDenis V. Lunev return -ENOMEM; 937e43291cbSDenis V. Lunev } 938e43291cbSDenis V. Lunev 9392c8c1e72SAlexey Dobriyan static void ipv6_cleanup_mibs(struct net *net) 940e43291cbSDenis V. Lunev { 941698365faSWANG Cong free_percpu(net->mib.udp_stats_in6); 942698365faSWANG Cong free_percpu(net->mib.udplite_stats_in6); 943698365faSWANG Cong free_percpu(net->mib.ipv6_statistics); 944698365faSWANG Cong free_percpu(net->mib.icmpv6_statistics); 9452a24444fSEric Dumazet kfree(net->mib.icmpv6msg_statistics); 946e43291cbSDenis V. Lunev } 947e43291cbSDenis V. Lunev 948e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net) 94981c1c178SDaniel Lezcano { 9500c96d8c5SDaniel Lezcano int err = 0; 9510c96d8c5SDaniel Lezcano 95299bc9c4eSDaniel Lezcano net->ipv6.sysctl.bindv6only = 0; 95341a76906SDaniel Lezcano net->ipv6.sysctl.icmpv6_time = 1*HZ; 954e6f86b0fSVirgile Jarry net->ipv6.sysctl.icmpv6_echo_ignore_all = 0; 95503f1ecccSStephen Suryaputra net->ipv6.sysctl.icmpv6_echo_ignore_multicast = 0; 9560b03a5caSStephen Suryaputra net->ipv6.sysctl.icmpv6_echo_ignore_anycast = 0; 9577ab75456SMahesh Bandewar net->ipv6.sysctl.icmpv6_error_anycast_as_unicast = 0; 9580bc19985SStephen Suryaputra 9590bc19985SStephen Suryaputra /* By default, rate limit error messages. 9600bc19985SStephen Suryaputra * Except for pmtu discovery, it would break it. 9610bc19985SStephen Suryaputra * proc_do_large_bitmap needs pointer to the bitmap. 9620bc19985SStephen Suryaputra */ 9630bc19985SStephen Suryaputra bitmap_set(net->ipv6.sysctl.icmpv6_ratemask, 0, ICMPV6_ERRMSG_MAX + 1); 9640bc19985SStephen Suryaputra bitmap_clear(net->ipv6.sysctl.icmpv6_ratemask, ICMPV6_PKT_TOOBIG, 1); 9650bc19985SStephen Suryaputra net->ipv6.sysctl.icmpv6_ratemask_ptr = net->ipv6.sysctl.icmpv6_ratemask; 9660bc19985SStephen Suryaputra 9676444f72bSFlorent Fourcot net->ipv6.sysctl.flowlabel_consistency = 1; 96842240901STom Herbert net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS; 9691855b7c3SHannes Frederic Sowa net->ipv6.sysctl.idgen_retries = 3; 9701855b7c3SHannes Frederic Sowa net->ipv6.sysctl.idgen_delay = 1 * HZ; 971be26849bSTom Herbert net->ipv6.sysctl.flowlabel_state_ranges = 0; 97247d3d7acSTom Herbert net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT; 97347d3d7acSTom Herbert net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT; 97447d3d7acSTom Herbert net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN; 97547d3d7acSTom Herbert net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN; 976907eea48SAmit Cohen net->ipv6.sysctl.fib_notify_on_flag_change = 0; 977812918c4SHannes Frederic Sowa atomic_set(&net->ipv6.fib6_sernum, 1); 978e71e0349SDaniel Lezcano 9799ee11f0fSJustin Iurman net->ipv6.sysctl.ioam6_id = IOAM6_DEFAULT_ID; 9809ee11f0fSJustin Iurman net->ipv6.sysctl.ioam6_id_wide = IOAM6_DEFAULT_ID_WIDE; 9819ee11f0fSJustin Iurman 982e43291cbSDenis V. Lunev err = ipv6_init_mibs(net); 983e43291cbSDenis V. Lunev if (err) 984e43291cbSDenis V. Lunev return err; 9850c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS 9860c96d8c5SDaniel Lezcano err = udp6_proc_init(net); 9870c96d8c5SDaniel Lezcano if (err) 9880c96d8c5SDaniel Lezcano goto out; 9896f8b13bcSDaniel Lezcano err = tcp6_proc_init(net); 9906f8b13bcSDaniel Lezcano if (err) 9916f8b13bcSDaniel Lezcano goto proc_tcp6_fail; 9926ab57e7eSDaniel Lezcano err = ac6_proc_init(net); 9936ab57e7eSDaniel Lezcano if (err) 9946ab57e7eSDaniel Lezcano goto proc_ac6_fail; 9950c96d8c5SDaniel Lezcano #endif 9960c96d8c5SDaniel Lezcano return err; 9976f8b13bcSDaniel Lezcano 9986f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS 9996ab57e7eSDaniel Lezcano proc_ac6_fail: 10006ab57e7eSDaniel Lezcano tcp6_proc_exit(net); 10016f8b13bcSDaniel Lezcano proc_tcp6_fail: 10026f8b13bcSDaniel Lezcano udp6_proc_exit(net); 1003e43291cbSDenis V. Lunev out: 1004e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net); 1005e43291cbSDenis V. Lunev return err; 10066f8b13bcSDaniel Lezcano #endif 100781c1c178SDaniel Lezcano } 100881c1c178SDaniel Lezcano 10092c8c1e72SAlexey Dobriyan static void __net_exit inet6_net_exit(struct net *net) 101081c1c178SDaniel Lezcano { 10110c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS 10120c96d8c5SDaniel Lezcano udp6_proc_exit(net); 10136f8b13bcSDaniel Lezcano tcp6_proc_exit(net); 10146ab57e7eSDaniel Lezcano ac6_proc_exit(net); 10150c96d8c5SDaniel Lezcano #endif 1016e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net); 101781c1c178SDaniel Lezcano } 101881c1c178SDaniel Lezcano 101981c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = { 102081c1c178SDaniel Lezcano .init = inet6_net_init, 102181c1c178SDaniel Lezcano .exit = inet6_net_exit, 102281c1c178SDaniel Lezcano }; 102381c1c178SDaniel Lezcano 10249b0a6a9dSPeter Oskolkov static int ipv6_route_input(struct sk_buff *skb) 10259b0a6a9dSPeter Oskolkov { 10269b0a6a9dSPeter Oskolkov ip6_route_input(skb); 10279b0a6a9dSPeter Oskolkov return skb_dst(skb)->error; 10289b0a6a9dSPeter Oskolkov } 10299b0a6a9dSPeter Oskolkov 10305f81bd2eSCong Wang static const struct ipv6_stub ipv6_stub_impl = { 10315f81bd2eSCong Wang .ipv6_sock_mc_join = ipv6_sock_mc_join, 10325f81bd2eSCong Wang .ipv6_sock_mc_drop = ipv6_sock_mc_drop, 10336c8991f4SSabrina Dubroca .ipv6_dst_lookup_flow = ip6_dst_lookup_flow, 10349b0a6a9dSPeter Oskolkov .ipv6_route_input = ipv6_route_input, 103565a2022eSDavid Ahern .fib6_get_table = fib6_get_table, 103665a2022eSDavid Ahern .fib6_table_lookup = fib6_table_lookup, 103765a2022eSDavid Ahern .fib6_lookup = fib6_lookup, 1038b1d40991SDavid Ahern .fib6_select_path = fib6_select_path, 1039901731b8SDavid Ahern .ip6_mtu_from_fib6 = ip6_mtu_from_fib6, 10401aefd3deSDavid Ahern .fib6_nh_init = fib6_nh_init, 10411aefd3deSDavid Ahern .fib6_nh_release = fib6_nh_release, 10428837cbbfSNikolay Aleksandrov .fib6_nh_release_dsts = fib6_nh_release_dsts, 1043cdaa16a4SDavid Ahern .fib6_update_sernum = fib6_update_sernum_stub, 104419a3b7eeSDavid Ahern .fib6_rt_update = fib6_rt_update, 104568a9b13dSDavid Ahern .ip6_del_rt = ip6_del_rt, 10465f81bd2eSCong Wang .udpv6_encap_enable = udpv6_encap_enable, 1047f564f45cSCong Wang .ndisc_send_na = ndisc_send_na, 10480146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_XFRM) 10493e50ddd8SFlorian Westphal .xfrm6_local_rxpmtu = xfrm6_local_rxpmtu, 10500146dca7SSabrina Dubroca .xfrm6_udp_encap_rcv = xfrm6_udp_encap_rcv, 105126333c37SSabrina Dubroca .xfrm6_rcv_encap = xfrm6_rcv_encap, 10520146dca7SSabrina Dubroca #endif 1053e15a00aaSCong Wang .nd_tbl = &nd_tbl, 10541d97898bSwenxu .ipv6_fragment = ip6_fragment, 1055504a4011SAndreas Roeseler .ipv6_dev_find = ipv6_dev_find, 10565f81bd2eSCong Wang }; 10575f81bd2eSCong Wang 1058d74bad4eSAndrey Ignatov static const struct ipv6_bpf_stub ipv6_bpf_stub_impl = { 1059d74bad4eSAndrey Ignatov .inet6_bind = __inet6_bind, 10608a615c6bSJoe Stringer .udp6_lib_lookup = __udp6_lib_lookup, 106175b64b68SMartin KaFai Lau .ipv6_setsockopt = do_ipv6_setsockopt, 106238566ec0SMartin KaFai Lau .ipv6_getsockopt = do_ipv6_getsockopt, 1063d74bad4eSAndrey Ignatov }; 1064d74bad4eSAndrey Ignatov 10651da177e4SLinus Torvalds static int __init inet6_init(void) 10661da177e4SLinus Torvalds { 10671da177e4SLinus Torvalds struct list_head *r; 1068fe7ca2e1SBrian Haley int err = 0; 10691da177e4SLinus Torvalds 1070b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct inet6_skb_parm)); 1071ef047f5eSYOSHIFUJI Hideaki 1072fe7ca2e1SBrian Haley /* Register the socket-side information for inet6_create. */ 1073fe7ca2e1SBrian Haley for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r) 1074fe7ca2e1SBrian Haley INIT_LIST_HEAD(r); 1075fe7ca2e1SBrian Haley 107676dd0728SIdo Schimmel raw_hashinfo_init(&raw_v6_hashinfo); 107776dd0728SIdo Schimmel 107856d417b1SBrian Haley if (disable_ipv6_mod) { 1079f3213831SJoe Perches pr_info("Loaded, but administratively disabled, reboot required to enable\n"); 1080fe7ca2e1SBrian Haley goto out; 1081fe7ca2e1SBrian Haley } 1082fe7ca2e1SBrian Haley 10831da177e4SLinus Torvalds err = proto_register(&tcpv6_prot, 1); 10841da177e4SLinus Torvalds if (err) 10851da177e4SLinus Torvalds goto out; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds err = proto_register(&udpv6_prot, 1); 10881da177e4SLinus Torvalds if (err) 10891da177e4SLinus Torvalds goto out_unregister_tcp_proto; 10901da177e4SLinus Torvalds 1091ba4e58ecSGerrit Renker err = proto_register(&udplitev6_prot, 1); 10921da177e4SLinus Torvalds if (err) 10931da177e4SLinus Torvalds goto out_unregister_udp_proto; 10941da177e4SLinus Torvalds 1095ba4e58ecSGerrit Renker err = proto_register(&rawv6_prot, 1); 1096ba4e58ecSGerrit Renker if (err) 1097ba4e58ecSGerrit Renker goto out_unregister_udplite_proto; 1098ba4e58ecSGerrit Renker 10996d0bfe22SLorenzo Colitti err = proto_register(&pingv6_prot, 1); 11006d0bfe22SLorenzo Colitti if (err) 1101a03dc36bSSabrina Dubroca goto out_unregister_raw_proto; 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds /* We MUST register RAW sockets before we create the ICMP6, 11041da177e4SLinus Torvalds * IGMP6, or NDISC control sockets. 11051da177e4SLinus Torvalds */ 11067f4e4868SDaniel Lezcano err = rawv6_init(); 11077f4e4868SDaniel Lezcano if (err) 1108a03dc36bSSabrina Dubroca goto out_unregister_ping_proto; 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds /* Register the family here so that the init calls below will 11111da177e4SLinus Torvalds * be able to create sockets. (?? is this dangerous ??) 11121da177e4SLinus Torvalds */ 11138eb55910SDavid S. Miller err = sock_register(&inet6_family_ops); 11148eb55910SDavid S. Miller if (err) 11157f4e4868SDaniel Lezcano goto out_sock_register_fail; 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds /* 11181da177e4SLinus Torvalds * ipngwg API draft makes clear that the correct semantics 11191da177e4SLinus Torvalds * for TCP and UDP is to consider one TCP and UDP instance 112025985edcSLucas De Marchi * in a host available by both INET and INET6 APIs and 11211da177e4SLinus Torvalds * able to communicate via both network protocols. 11221da177e4SLinus Torvalds */ 11231da177e4SLinus Torvalds 112481c1c178SDaniel Lezcano err = register_pernet_subsys(&inet6_net_ops); 112581c1c178SDaniel Lezcano if (err) 112681c1c178SDaniel Lezcano goto register_pernet_fail; 1127623d1a1aSWang Chen err = ip6_mr_init(); 1128623d1a1aSWang Chen if (err) 1129623d1a1aSWang Chen goto ipmr_fail; 113015e66807SWANG Cong err = icmpv6_init(); 113115e66807SWANG Cong if (err) 113215e66807SWANG Cong goto icmp_fail; 11339b0f976fSDenis V. Lunev err = ndisc_init(); 11341da177e4SLinus Torvalds if (err) 11351da177e4SLinus Torvalds goto ndisc_fail; 11369b0f976fSDenis V. Lunev err = igmp6_init(); 11371da177e4SLinus Torvalds if (err) 11381da177e4SLinus Torvalds goto igmp_fail; 11395f81bd2eSCong Wang 11402cc7d573SHarald Welte err = ipv6_netfilter_init(); 11412cc7d573SHarald Welte if (err) 11422cc7d573SHarald Welte goto netfilter_fail; 11431da177e4SLinus Torvalds /* Create /proc/foo6 entries. */ 11441da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 11451da177e4SLinus Torvalds err = -ENOMEM; 11461da177e4SLinus Torvalds if (raw6_proc_init()) 11471da177e4SLinus Torvalds goto proc_raw6_fail; 1148ba4e58ecSGerrit Renker if (udplite6_proc_init()) 1149ba4e58ecSGerrit Renker goto proc_udplite6_fail; 11501da177e4SLinus Torvalds if (ipv6_misc_proc_init()) 11511da177e4SLinus Torvalds goto proc_misc6_fail; 11521da177e4SLinus Torvalds if (if6_proc_init()) 11531da177e4SLinus Torvalds goto proc_if6_fail; 11541da177e4SLinus Torvalds #endif 1155e2fddf5eSDaniel Lezcano err = ip6_route_init(); 1156e2fddf5eSDaniel Lezcano if (err) 1157e2fddf5eSDaniel Lezcano goto ip6_route_fail; 11582c861cc6SMichal Kubeček err = ndisc_late_init(); 11592c861cc6SMichal Kubeček if (err) 11602c861cc6SMichal Kubeček goto ndisc_late_fail; 11610a3e78acSDaniel Lezcano err = ip6_flowlabel_init(); 11620a3e78acSDaniel Lezcano if (err) 11630a3e78acSDaniel Lezcano goto ip6_flowlabel_fail; 11642384d025SJeff Barnhill err = ipv6_anycast_init(); 11652384d025SJeff Barnhill if (err) 11662384d025SJeff Barnhill goto ipv6_anycast_fail; 11671da177e4SLinus Torvalds err = addrconf_init(); 11681da177e4SLinus Torvalds if (err) 11691da177e4SLinus Torvalds goto addrconf_fail; 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds /* Init v6 extension headers. */ 1172248b238dSDaniel Lezcano err = ipv6_exthdrs_init(); 1173248b238dSDaniel Lezcano if (err) 1174248b238dSDaniel Lezcano goto ipv6_exthdrs_fail; 1175248b238dSDaniel Lezcano 1176853cbbaaSDaniel Lezcano err = ipv6_frag_init(); 1177853cbbaaSDaniel Lezcano if (err) 1178853cbbaaSDaniel Lezcano goto ipv6_frag_fail; 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds /* Init v6 transport protocols. */ 11817f4e4868SDaniel Lezcano err = udpv6_init(); 11827f4e4868SDaniel Lezcano if (err) 11837f4e4868SDaniel Lezcano goto udpv6_fail; 1184e2ed4052SHerbert Xu 11857f4e4868SDaniel Lezcano err = udplitev6_init(); 11867f4e4868SDaniel Lezcano if (err) 11877f4e4868SDaniel Lezcano goto udplitev6_fail; 11887f4e4868SDaniel Lezcano 1189a6024562STom Herbert err = udpv6_offload_init(); 1190a6024562STom Herbert if (err) 1191a6024562STom Herbert goto udpv6_offload_fail; 1192a6024562STom Herbert 11937f4e4868SDaniel Lezcano err = tcpv6_init(); 11947f4e4868SDaniel Lezcano if (err) 11957f4e4868SDaniel Lezcano goto tcpv6_fail; 11967f4e4868SDaniel Lezcano 11977f4e4868SDaniel Lezcano err = ipv6_packet_init(); 11987f4e4868SDaniel Lezcano if (err) 11997f4e4868SDaniel Lezcano goto ipv6_packet_fail; 120094911fe3SBenjamin Thery 12016d0bfe22SLorenzo Colitti err = pingv6_init(); 12026d0bfe22SLorenzo Colitti if (err) 12036d0bfe22SLorenzo Colitti goto pingv6_fail; 12046d0bfe22SLorenzo Colitti 1205cb72d382SHuw Davies err = calipso_init(); 1206cb72d382SHuw Davies if (err) 1207cb72d382SHuw Davies goto calipso_fail; 1208cb72d382SHuw Davies 1209915d7e5eSDavid Lebrun err = seg6_init(); 1210915d7e5eSDavid Lebrun if (err) 1211915d7e5eSDavid Lebrun goto seg6_fail; 1212915d7e5eSDavid Lebrun 1213a7a29f9cSAlexander Aring err = rpl_init(); 1214a7a29f9cSAlexander Aring if (err) 1215a7a29f9cSAlexander Aring goto rpl_fail; 1216a7a29f9cSAlexander Aring 12179ee11f0fSJustin Iurman err = ioam6_init(); 12189ee11f0fSJustin Iurman if (err) 12199ee11f0fSJustin Iurman goto ioam6_fail; 12209ee11f0fSJustin Iurman 1221382ed724SVlad Yasevich err = igmp6_late_init(); 1222382ed724SVlad Yasevich if (err) 1223382ed724SVlad Yasevich goto igmp6_late_err; 1224382ed724SVlad Yasevich 122594911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL 122694911fe3SBenjamin Thery err = ipv6_sysctl_register(); 122794911fe3SBenjamin Thery if (err) 122894911fe3SBenjamin Thery goto sysctl_fail; 122994911fe3SBenjamin Thery #endif 1230b7d6df57SPaolo Abeni 1231b7d6df57SPaolo Abeni /* ensure that ipv6 stubs are visible only after ipv6 is ready */ 1232b7d6df57SPaolo Abeni wmb(); 1233b7d6df57SPaolo Abeni ipv6_stub = &ipv6_stub_impl; 1234d74bad4eSAndrey Ignatov ipv6_bpf_stub = &ipv6_bpf_stub_impl; 12351da177e4SLinus Torvalds out: 12361da177e4SLinus Torvalds return err; 12371da177e4SLinus Torvalds 123894911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL 123994911fe3SBenjamin Thery sysctl_fail: 1240382ed724SVlad Yasevich igmp6_late_cleanup(); 124194911fe3SBenjamin Thery #endif 1242382ed724SVlad Yasevich igmp6_late_err: 12439ee11f0fSJustin Iurman ioam6_exit(); 12449ee11f0fSJustin Iurman ioam6_fail: 1245a7a29f9cSAlexander Aring rpl_exit(); 1246a7a29f9cSAlexander Aring rpl_fail: 1247382ed724SVlad Yasevich seg6_exit(); 1248915d7e5eSDavid Lebrun seg6_fail: 1249915d7e5eSDavid Lebrun calipso_exit(); 1250cb72d382SHuw Davies calipso_fail: 1251cb72d382SHuw Davies pingv6_exit(); 12526d0bfe22SLorenzo Colitti pingv6_fail: 1253eca42aafSVlad Yasevich ipv6_packet_cleanup(); 12547f4e4868SDaniel Lezcano ipv6_packet_fail: 12557f4e4868SDaniel Lezcano tcpv6_exit(); 12567f4e4868SDaniel Lezcano tcpv6_fail: 1257a6024562STom Herbert udpv6_offload_exit(); 1258a6024562STom Herbert udpv6_offload_fail: 12597f4e4868SDaniel Lezcano udplitev6_exit(); 12607f4e4868SDaniel Lezcano udplitev6_fail: 12617f4e4868SDaniel Lezcano udpv6_exit(); 12627f4e4868SDaniel Lezcano udpv6_fail: 12637f4e4868SDaniel Lezcano ipv6_frag_exit(); 1264853cbbaaSDaniel Lezcano ipv6_frag_fail: 1265853cbbaaSDaniel Lezcano ipv6_exthdrs_exit(); 1266248b238dSDaniel Lezcano ipv6_exthdrs_fail: 1267248b238dSDaniel Lezcano addrconf_cleanup(); 12681da177e4SLinus Torvalds addrconf_fail: 12692384d025SJeff Barnhill ipv6_anycast_cleanup(); 12702384d025SJeff Barnhill ipv6_anycast_fail: 12711da177e4SLinus Torvalds ip6_flowlabel_cleanup(); 12720a3e78acSDaniel Lezcano ip6_flowlabel_fail: 12732c861cc6SMichal Kubeček ndisc_late_cleanup(); 12742c861cc6SMichal Kubeček ndisc_late_fail: 12751da177e4SLinus Torvalds ip6_route_cleanup(); 1276e2fddf5eSDaniel Lezcano ip6_route_fail: 12771da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 12781da177e4SLinus Torvalds if6_proc_exit(); 12791da177e4SLinus Torvalds proc_if6_fail: 12801da177e4SLinus Torvalds ipv6_misc_proc_exit(); 12811da177e4SLinus Torvalds proc_misc6_fail: 1282ba4e58ecSGerrit Renker udplite6_proc_exit(); 1283ba4e58ecSGerrit Renker proc_udplite6_fail: 12841da177e4SLinus Torvalds raw6_proc_exit(); 12851da177e4SLinus Torvalds proc_raw6_fail: 12861da177e4SLinus Torvalds #endif 12872cc7d573SHarald Welte ipv6_netfilter_fini(); 12882cc7d573SHarald Welte netfilter_fail: 12891da177e4SLinus Torvalds igmp6_cleanup(); 12901da177e4SLinus Torvalds igmp_fail: 12911da177e4SLinus Torvalds ndisc_cleanup(); 12921da177e4SLinus Torvalds ndisc_fail: 129315e66807SWANG Cong icmpv6_cleanup(); 1294afe49de4SSabrina Dubroca icmp_fail: 1295afe49de4SSabrina Dubroca ip6_mr_cleanup(); 1296afe49de4SSabrina Dubroca ipmr_fail: 1297afe49de4SSabrina Dubroca unregister_pernet_subsys(&inet6_net_ops); 129881c1c178SDaniel Lezcano register_pernet_fail: 12998eb55910SDavid S. Miller sock_unregister(PF_INET6); 1300e2fddf5eSDaniel Lezcano rtnl_unregister_all(PF_INET6); 13017f4e4868SDaniel Lezcano out_sock_register_fail: 13027f4e4868SDaniel Lezcano rawv6_exit(); 13036d0bfe22SLorenzo Colitti out_unregister_ping_proto: 13046d0bfe22SLorenzo Colitti proto_unregister(&pingv6_prot); 13051da177e4SLinus Torvalds out_unregister_raw_proto: 13061da177e4SLinus Torvalds proto_unregister(&rawv6_prot); 1307ba4e58ecSGerrit Renker out_unregister_udplite_proto: 1308ba4e58ecSGerrit Renker proto_unregister(&udplitev6_prot); 13091da177e4SLinus Torvalds out_unregister_udp_proto: 13101da177e4SLinus Torvalds proto_unregister(&udpv6_prot); 13111da177e4SLinus Torvalds out_unregister_tcp_proto: 13121da177e4SLinus Torvalds proto_unregister(&tcpv6_prot); 13131da177e4SLinus Torvalds goto out; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds module_init(inet6_init); 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6); 1318