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 105*f5f80e32SEric Dumazet static struct ipv6_pinfo *inet6_sk_generic(struct sock *sk) 1061da177e4SLinus Torvalds { 107*f5f80e32SEric 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); 203469de9b9SPaul Moore inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0; 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds if (SOCK_RAW == sock->type) { 206c720c7e8SEric Dumazet inet->inet_num = protocol; 2071da177e4SLinus Torvalds if (IPPROTO_RAW == protocol) 2081da177e4SLinus Torvalds inet->hdrincl = 1; 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 2321da177e4SLinus Torvalds inet->mc_loop = 1; 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. */ 402aac3fc32SAndrey Ignatov if (snum || !(inet->bind_address_no_port || 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 438032234d8SDavid Ahern /* bind for INET6 API */ 439032234d8SDavid Ahern int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 440032234d8SDavid Ahern { 441032234d8SDavid Ahern struct sock *sk = sock->sk; 44277241217SStanislav Fomichev u32 flags = BIND_WITH_LOCK; 443086d4905SEric Dumazet const struct proto *prot; 444032234d8SDavid Ahern int err = 0; 445032234d8SDavid Ahern 446086d4905SEric Dumazet /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 447086d4905SEric Dumazet prot = READ_ONCE(sk->sk_prot); 448032234d8SDavid Ahern /* If the socket has its own bind function then use it. */ 449086d4905SEric Dumazet if (prot->bind) 450086d4905SEric Dumazet return prot->bind(sk, uaddr, addr_len); 451032234d8SDavid Ahern 452032234d8SDavid Ahern if (addr_len < SIN6_LEN_RFC2133) 453032234d8SDavid Ahern return -EINVAL; 454032234d8SDavid Ahern 455032234d8SDavid Ahern /* BPF prog is run before any checks are done so that if the prog 456032234d8SDavid Ahern * changes context in a wrong way it will be caught. 457032234d8SDavid Ahern */ 45877241217SStanislav Fomichev err = BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr, 4596fc88c35SDave Marchevsky CGROUP_INET6_BIND, &flags); 460032234d8SDavid Ahern if (err) 461032234d8SDavid Ahern return err; 462032234d8SDavid Ahern 46377241217SStanislav Fomichev return __inet6_bind(sk, uaddr, addr_len, flags); 464032234d8SDavid Ahern } 465032234d8SDavid Ahern EXPORT_SYMBOL(inet6_bind); 466032234d8SDavid Ahern 4671da177e4SLinus Torvalds int inet6_release(struct socket *sock) 4681da177e4SLinus Torvalds { 4691da177e4SLinus Torvalds struct sock *sk = sock->sk; 4701da177e4SLinus Torvalds 47163159f29SIan Morris if (!sk) 4721da177e4SLinus Torvalds return -EINVAL; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds /* Free mc lists */ 4751da177e4SLinus Torvalds ipv6_sock_mc_close(sk); 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds /* Free ac lists */ 4781da177e4SLinus Torvalds ipv6_sock_ac_close(sk); 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds return inet_release(sock); 4811da177e4SLinus Torvalds } 4827159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release); 4837159039aSYOSHIFUJI Hideaki 4841f8c4eebSKuniyuki Iwashima void inet6_cleanup_sock(struct sock *sk) 4851da177e4SLinus Torvalds { 4861da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 4871da177e4SLinus Torvalds struct sk_buff *skb; 4881da177e4SLinus Torvalds struct ipv6_txoptions *opt; 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds /* Release rx options */ 4911da177e4SLinus Torvalds 492647c0c70SEldad Zack skb = xchg(&np->pktoptions, NULL); 4931da177e4SLinus Torvalds kfree_skb(skb); 4941da177e4SLinus Torvalds 495647c0c70SEldad Zack skb = xchg(&np->rxpmtu, NULL); 4964b340ae2SBrian Haley kfree_skb(skb); 4974b340ae2SBrian Haley 4981da177e4SLinus Torvalds /* Free flowlabels */ 4991da177e4SLinus Torvalds fl6_free_socklist(sk); 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds /* Free tx options */ 5021da177e4SLinus Torvalds 50345f6fad8SEric Dumazet opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL); 50445f6fad8SEric Dumazet if (opt) { 50545f6fad8SEric Dumazet atomic_sub(opt->tot_len, &sk->sk_omem_alloc); 50645f6fad8SEric Dumazet txopt_put(opt); 50745f6fad8SEric Dumazet } 5081da177e4SLinus Torvalds } 50921985f43SKuniyuki Iwashima EXPORT_SYMBOL_GPL(inet6_cleanup_sock); 51021985f43SKuniyuki Iwashima 5111da177e4SLinus Torvalds /* 5121da177e4SLinus Torvalds * This does both peername and sockname. 5131da177e4SLinus Torvalds */ 5141da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr, 5159b2c45d4SDenys Vlasenko int peer) 5161da177e4SLinus Torvalds { 5171da177e4SLinus Torvalds struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr; 5181da177e4SLinus Torvalds struct sock *sk = sock->sk; 5191da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 5201da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk); 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds sin->sin6_family = AF_INET6; 5231da177e4SLinus Torvalds sin->sin6_flowinfo = 0; 5241da177e4SLinus Torvalds sin->sin6_scope_id = 0; 5259dfc685eSEric Dumazet lock_sock(sk); 5261da177e4SLinus Torvalds if (peer) { 5279dfc685eSEric Dumazet if (!inet->inet_dport || 5289dfc685eSEric Dumazet (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && 5299dfc685eSEric Dumazet peer == 1)) { 5309dfc685eSEric Dumazet release_sock(sk); 5311da177e4SLinus Torvalds return -ENOTCONN; 5329dfc685eSEric Dumazet } 533c720c7e8SEric Dumazet sin->sin6_port = inet->inet_dport; 534efe4208fSEric Dumazet sin->sin6_addr = sk->sk_v6_daddr; 5351da177e4SLinus Torvalds if (np->sndflow) 5361da177e4SLinus Torvalds sin->sin6_flowinfo = np->flow_label; 5379dfc685eSEric Dumazet BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, 5389dfc685eSEric Dumazet CGROUP_INET6_GETPEERNAME); 5391da177e4SLinus Torvalds } else { 540efe4208fSEric Dumazet if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) 5414e3fd7a0SAlexey Dobriyan sin->sin6_addr = np->saddr; 5421da177e4SLinus Torvalds else 543efe4208fSEric Dumazet sin->sin6_addr = sk->sk_v6_rcv_saddr; 544c720c7e8SEric Dumazet sin->sin6_port = inet->inet_sport; 5459dfc685eSEric Dumazet BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, 5469dfc685eSEric Dumazet CGROUP_INET6_GETSOCKNAME); 547a9ed15daSStanislav Fomichev } 548842df073SHannes Frederic Sowa sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr, 549842df073SHannes Frederic Sowa sk->sk_bound_dev_if); 5509dfc685eSEric Dumazet release_sock(sk); 5519b2c45d4SDenys Vlasenko return sizeof(*sin); 5521da177e4SLinus Torvalds } 5537159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname); 5547159039aSYOSHIFUJI Hideaki 5551da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 5561da177e4SLinus Torvalds { 5577c1552daSChristoph Hellwig void __user *argp = (void __user *)arg; 5581da177e4SLinus Torvalds struct sock *sk = sock->sk; 5593b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk); 560086d4905SEric Dumazet const struct proto *prot; 5611da177e4SLinus Torvalds 562647c0c70SEldad Zack switch (cmd) { 5631da177e4SLinus Torvalds case SIOCADDRT: 5647c1552daSChristoph Hellwig case SIOCDELRT: { 5657c1552daSChristoph Hellwig struct in6_rtmsg rtmsg; 5661da177e4SLinus Torvalds 5677c1552daSChristoph Hellwig if (copy_from_user(&rtmsg, argp, sizeof(rtmsg))) 5687c1552daSChristoph Hellwig return -EFAULT; 5697c1552daSChristoph Hellwig return ipv6_route_ioctl(net, cmd, &rtmsg); 5707c1552daSChristoph Hellwig } 5711da177e4SLinus Torvalds case SIOCSIFADDR: 5727c1552daSChristoph Hellwig return addrconf_add_ifaddr(net, argp); 5731da177e4SLinus Torvalds case SIOCDIFADDR: 5747c1552daSChristoph Hellwig return addrconf_del_ifaddr(net, argp); 5751da177e4SLinus Torvalds case SIOCSIFDSTADDR: 5767c1552daSChristoph Hellwig return addrconf_set_dstaddr(net, argp); 5771da177e4SLinus Torvalds default: 578086d4905SEric Dumazet /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 579086d4905SEric Dumazet prot = READ_ONCE(sk->sk_prot); 580086d4905SEric Dumazet if (!prot->ioctl) 581b5e5fa5eSChristoph Hellwig return -ENOIOCTLCMD; 582e1d001faSBreno Leitao return sk_ioctl(sk, cmd, (void __user *)arg); 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds /*NOTREACHED*/ 585a02cec21SEric Dumazet return 0; 5861da177e4SLinus Torvalds } 5877159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl); 5887159039aSYOSHIFUJI Hideaki 5893986912fSChristoph Hellwig #ifdef CONFIG_COMPAT 5903986912fSChristoph Hellwig struct compat_in6_rtmsg { 5913986912fSChristoph Hellwig struct in6_addr rtmsg_dst; 5923986912fSChristoph Hellwig struct in6_addr rtmsg_src; 5933986912fSChristoph Hellwig struct in6_addr rtmsg_gateway; 5943986912fSChristoph Hellwig u32 rtmsg_type; 5953986912fSChristoph Hellwig u16 rtmsg_dst_len; 5963986912fSChristoph Hellwig u16 rtmsg_src_len; 5973986912fSChristoph Hellwig u32 rtmsg_metric; 5983986912fSChristoph Hellwig u32 rtmsg_info; 5993986912fSChristoph Hellwig u32 rtmsg_flags; 6003986912fSChristoph Hellwig s32 rtmsg_ifindex; 6013986912fSChristoph Hellwig }; 6023986912fSChristoph Hellwig 6033986912fSChristoph Hellwig static int inet6_compat_routing_ioctl(struct sock *sk, unsigned int cmd, 6043986912fSChristoph Hellwig struct compat_in6_rtmsg __user *ur) 6053986912fSChristoph Hellwig { 6063986912fSChristoph Hellwig struct in6_rtmsg rt; 6073986912fSChristoph Hellwig 6083986912fSChristoph Hellwig if (copy_from_user(&rt.rtmsg_dst, &ur->rtmsg_dst, 6093986912fSChristoph Hellwig 3 * sizeof(struct in6_addr)) || 6103986912fSChristoph Hellwig get_user(rt.rtmsg_type, &ur->rtmsg_type) || 6113986912fSChristoph Hellwig get_user(rt.rtmsg_dst_len, &ur->rtmsg_dst_len) || 6123986912fSChristoph Hellwig get_user(rt.rtmsg_src_len, &ur->rtmsg_src_len) || 6133986912fSChristoph Hellwig get_user(rt.rtmsg_metric, &ur->rtmsg_metric) || 6143986912fSChristoph Hellwig get_user(rt.rtmsg_info, &ur->rtmsg_info) || 6153986912fSChristoph Hellwig get_user(rt.rtmsg_flags, &ur->rtmsg_flags) || 6163986912fSChristoph Hellwig get_user(rt.rtmsg_ifindex, &ur->rtmsg_ifindex)) 6173986912fSChristoph Hellwig return -EFAULT; 6183986912fSChristoph Hellwig 6193986912fSChristoph Hellwig 6203986912fSChristoph Hellwig return ipv6_route_ioctl(sock_net(sk), cmd, &rt); 6213986912fSChristoph Hellwig } 6223986912fSChristoph Hellwig 6233986912fSChristoph Hellwig int inet6_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 6243986912fSChristoph Hellwig { 6253986912fSChristoph Hellwig void __user *argp = compat_ptr(arg); 6263986912fSChristoph Hellwig struct sock *sk = sock->sk; 6273986912fSChristoph Hellwig 6283986912fSChristoph Hellwig switch (cmd) { 6293986912fSChristoph Hellwig case SIOCADDRT: 6303986912fSChristoph Hellwig case SIOCDELRT: 6313986912fSChristoph Hellwig return inet6_compat_routing_ioctl(sk, cmd, argp); 6323986912fSChristoph Hellwig default: 6333986912fSChristoph Hellwig return -ENOIOCTLCMD; 6343986912fSChristoph Hellwig } 6353986912fSChristoph Hellwig } 6363986912fSChristoph Hellwig EXPORT_SYMBOL_GPL(inet6_compat_ioctl); 6373986912fSChristoph Hellwig #endif /* CONFIG_COMPAT */ 6383986912fSChristoph Hellwig 639164c51feSPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udpv6_sendmsg(struct sock *, struct msghdr *, 640164c51feSPaolo Abeni size_t)); 64168ab5d14SPaolo Abeni int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) 64268ab5d14SPaolo Abeni { 64368ab5d14SPaolo Abeni struct sock *sk = sock->sk; 644086d4905SEric Dumazet const struct proto *prot; 64568ab5d14SPaolo Abeni 64668ab5d14SPaolo Abeni if (unlikely(inet_send_prepare(sk))) 64768ab5d14SPaolo Abeni return -EAGAIN; 64868ab5d14SPaolo Abeni 649086d4905SEric Dumazet /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 650086d4905SEric Dumazet prot = READ_ONCE(sk->sk_prot); 651086d4905SEric Dumazet return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg, 652164c51feSPaolo Abeni sk, msg, size); 65368ab5d14SPaolo Abeni } 65468ab5d14SPaolo Abeni 655164c51feSPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udpv6_recvmsg(struct sock *, struct msghdr *, 656ec095263SOliver Hartkopp size_t, int, int *)); 65768ab5d14SPaolo Abeni int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 65868ab5d14SPaolo Abeni int flags) 65968ab5d14SPaolo Abeni { 66068ab5d14SPaolo Abeni struct sock *sk = sock->sk; 661086d4905SEric Dumazet const struct proto *prot; 66268ab5d14SPaolo Abeni int addr_len = 0; 66368ab5d14SPaolo Abeni int err; 66468ab5d14SPaolo Abeni 66568ab5d14SPaolo Abeni if (likely(!(flags & MSG_ERRQUEUE))) 66668ab5d14SPaolo Abeni sock_rps_record_flow(sk); 66768ab5d14SPaolo Abeni 668086d4905SEric Dumazet /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 669086d4905SEric Dumazet prot = READ_ONCE(sk->sk_prot); 670086d4905SEric Dumazet err = INDIRECT_CALL_2(prot->recvmsg, tcp_recvmsg, udpv6_recvmsg, 671ec095263SOliver Hartkopp sk, msg, size, flags, &addr_len); 67268ab5d14SPaolo Abeni if (err >= 0) 67368ab5d14SPaolo Abeni msg->msg_namelen = addr_len; 67468ab5d14SPaolo Abeni return err; 67568ab5d14SPaolo Abeni } 67668ab5d14SPaolo Abeni 67790ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = { 6781da177e4SLinus Torvalds .family = PF_INET6, 6791da177e4SLinus Torvalds .owner = THIS_MODULE, 6801da177e4SLinus Torvalds .release = inet6_release, 6811da177e4SLinus Torvalds .bind = inet6_bind, 6821da177e4SLinus Torvalds .connect = inet_stream_connect, /* ok */ 6831da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */ 6841da177e4SLinus Torvalds .accept = inet_accept, /* ok */ 6851da177e4SLinus Torvalds .getname = inet6_getname, 686a11e1d43SLinus Torvalds .poll = tcp_poll, /* ok */ 6871da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */ 688c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp, 6891da177e4SLinus Torvalds .listen = inet_listen, /* ok */ 6901da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */ 6911da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */ 6921da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */ 69368ab5d14SPaolo Abeni .sendmsg = inet6_sendmsg, /* retpoline's sake */ 69468ab5d14SPaolo Abeni .recvmsg = inet6_recvmsg, /* retpoline's sake */ 69505255b82SEric Dumazet #ifdef CONFIG_MMU 69693ab6cc6SEric Dumazet .mmap = tcp_mmap, 69705255b82SEric Dumazet #endif 6981d7e4538SDavid Howells .splice_eof = inet_splice_eof, 69945f91bdcSJohn Fastabend .sendmsg_locked = tcp_sendmsg_locked, 700a0974dd3SJens Axboe .splice_read = tcp_splice_read, 70132035585STom Herbert .read_sock = tcp_read_sock, 702965b57b4SCong Wang .read_skb = tcp_read_skb, 70332035585STom Herbert .peek_len = tcp_peek_len, 7043fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT 7053986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 7063fdadf7dSDmitry Mishin #endif 707d1361840SEric Dumazet .set_rcvlowat = tcp_set_rcvlowat, 7081da177e4SLinus Torvalds }; 7091da177e4SLinus Torvalds 71090ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = { 7111da177e4SLinus Torvalds .family = PF_INET6, 7121da177e4SLinus Torvalds .owner = THIS_MODULE, 7131da177e4SLinus Torvalds .release = inet6_release, 7141da177e4SLinus Torvalds .bind = inet6_bind, 7151da177e4SLinus Torvalds .connect = inet_dgram_connect, /* ok */ 7161da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */ 7171da177e4SLinus Torvalds .accept = sock_no_accept, /* a do nothing */ 7181da177e4SLinus Torvalds .getname = inet6_getname, 719a11e1d43SLinus Torvalds .poll = udp_poll, /* ok */ 7201da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */ 721c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp, 7221da177e4SLinus Torvalds .listen = sock_no_listen, /* ok */ 7231da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */ 7241da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */ 7251da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */ 72668ab5d14SPaolo Abeni .sendmsg = inet6_sendmsg, /* retpoline's sake */ 72768ab5d14SPaolo Abeni .recvmsg = inet6_recvmsg, /* retpoline's sake */ 728965b57b4SCong Wang .read_skb = udp_read_skb, 7291da177e4SLinus Torvalds .mmap = sock_no_mmap, 730627d2d6bSsamanthakumar .set_peek_off = sk_set_peek_off, 731543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT 7323986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl, 733543d9cfeSArnaldo Carvalho de Melo #endif 7341da177e4SLinus Torvalds }; 7351da177e4SLinus Torvalds 736ec1b4cf7SStephen Hemminger static const struct net_proto_family inet6_family_ops = { 7371da177e4SLinus Torvalds .family = PF_INET6, 7381da177e4SLinus Torvalds .create = inet6_create, 7391da177e4SLinus Torvalds .owner = THIS_MODULE, 7401da177e4SLinus Torvalds }; 7411da177e4SLinus Torvalds 74287c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p) 7431da177e4SLinus Torvalds { 7441da177e4SLinus Torvalds struct list_head *lh; 7451da177e4SLinus Torvalds struct inet_protosw *answer; 7461da177e4SLinus Torvalds struct list_head *last_perm; 74787c3efbfSDaniel Lezcano int protocol = p->protocol; 74887c3efbfSDaniel Lezcano int ret; 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock); 7511da177e4SLinus Torvalds 75287c3efbfSDaniel Lezcano ret = -EINVAL; 7531da177e4SLinus Torvalds if (p->type >= SOCK_MAX) 7541da177e4SLinus Torvalds goto out_illegal; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds /* If we are trying to override a permanent protocol, bail. */ 7571da177e4SLinus Torvalds answer = NULL; 75887c3efbfSDaniel Lezcano ret = -EPERM; 7591da177e4SLinus Torvalds last_perm = &inetsw6[p->type]; 7601da177e4SLinus Torvalds list_for_each(lh, &inetsw6[p->type]) { 7611da177e4SLinus Torvalds answer = list_entry(lh, struct inet_protosw, list); 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /* Check only the non-wild match. */ 7641da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & answer->flags) { 7651da177e4SLinus Torvalds if (protocol == answer->protocol) 7661da177e4SLinus Torvalds break; 7671da177e4SLinus Torvalds last_perm = lh; 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds answer = NULL; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds if (answer) 7731da177e4SLinus Torvalds goto out_permanent; 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds /* Add the new entry after the last permanent entry if any, so that 7761da177e4SLinus Torvalds * the new entry does not override a permanent entry when matched with 7771da177e4SLinus Torvalds * a wild-card protocol. But it is allowed to override any existing 7781da177e4SLinus Torvalds * non-permanent entry. This means that when we remove this entry, the 7791da177e4SLinus Torvalds * system automatically returns to the old behavior. 7801da177e4SLinus Torvalds */ 7811da177e4SLinus Torvalds list_add_rcu(&p->list, last_perm); 78287c3efbfSDaniel Lezcano ret = 0; 7831da177e4SLinus Torvalds out: 7841da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock); 78587c3efbfSDaniel Lezcano return ret; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds out_permanent: 788f3213831SJoe Perches pr_err("Attempt to override permanent protocol %d\n", protocol); 7891da177e4SLinus Torvalds goto out; 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds out_illegal: 792f3213831SJoe Perches pr_err("Ignoring attempt to register invalid socket type %d\n", 7931da177e4SLinus Torvalds p->type); 7941da177e4SLinus Torvalds goto out; 7951da177e4SLinus Torvalds } 7967159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw); 7977159039aSYOSHIFUJI Hideaki 7981da177e4SLinus Torvalds void 7991da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p) 8001da177e4SLinus Torvalds { 8011da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & p->flags) { 802f3213831SJoe Perches pr_err("Attempt to unregister permanent protocol %d\n", 8031da177e4SLinus Torvalds p->protocol); 8041da177e4SLinus Torvalds } else { 8051da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock); 8061da177e4SLinus Torvalds list_del_rcu(&p->list); 8071da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock); 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds synchronize_net(); 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds } 8127159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw); 8137159039aSYOSHIFUJI Hideaki 814b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk) 815b9750ce1SArnaldo Carvalho de Melo { 816b9750ce1SArnaldo Carvalho de Melo struct ipv6_pinfo *np = inet6_sk(sk); 81768d0c6d3SDavid S. Miller struct dst_entry *dst; 818b9750ce1SArnaldo Carvalho de Melo 819b9750ce1SArnaldo Carvalho de Melo dst = __sk_dst_check(sk, np->dst_cookie); 820b9750ce1SArnaldo Carvalho de Melo 82163159f29SIan Morris if (!dst) { 822b9750ce1SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk); 82320c59de2SArnaud Ebalard struct in6_addr *final_p, final; 8244c9483b2SDavid S. Miller struct flowi6 fl6; 825b9750ce1SArnaldo Carvalho de Melo 8264c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6)); 8274c9483b2SDavid S. Miller fl6.flowi6_proto = sk->sk_protocol; 828efe4208fSEric Dumazet fl6.daddr = sk->sk_v6_daddr; 8294e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr; 8304c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label; 8314c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if; 8324c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark; 8331958b856SDavid S. Miller fl6.fl6_dport = inet->inet_dport; 8341958b856SDavid S. Miller fl6.fl6_sport = inet->inet_sport; 835e2d118a1SLorenzo Colitti fl6.flowi6_uid = sk->sk_uid; 8363df98d79SPaul Moore security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6)); 837b9750ce1SArnaldo Carvalho de Melo 83845f6fad8SEric Dumazet rcu_read_lock(); 83945f6fad8SEric Dumazet final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt), 84045f6fad8SEric Dumazet &final); 84145f6fad8SEric Dumazet rcu_read_unlock(); 842b9750ce1SArnaldo Carvalho de Melo 843c4e85f73SSabrina Dubroca dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p); 84468d0c6d3SDavid S. Miller if (IS_ERR(dst)) { 845b9750ce1SArnaldo Carvalho de Melo sk->sk_route_caps = 0; 8462f2d9972SEric Dumazet WRITE_ONCE(sk->sk_err_soft, -PTR_ERR(dst)); 84768d0c6d3SDavid S. Miller return PTR_ERR(dst); 848b9750ce1SArnaldo Carvalho de Melo } 849b9750ce1SArnaldo Carvalho de Melo 8506bd4f355SEric Dumazet ip6_dst_store(sk, dst, NULL, NULL); 851b9750ce1SArnaldo Carvalho de Melo } 852b9750ce1SArnaldo Carvalho de Melo 853b9750ce1SArnaldo Carvalho de Melo return 0; 854b9750ce1SArnaldo Carvalho de Melo } 855b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header); 856b9750ce1SArnaldo Carvalho de Melo 857a224772dSEric Dumazet bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb, 858a224772dSEric Dumazet const struct inet6_skb_parm *opt) 859399c07deSArnaldo Carvalho de Melo { 86092113bfdSEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk); 861399c07deSArnaldo Carvalho de Melo 862399c07deSArnaldo Carvalho de Melo if (np->rxopt.all) { 8638b58a398SFlorian Westphal if (((opt->flags & IP6SKB_HOPBYHOP) && 8648b58a398SFlorian Westphal (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) || 865ac1eabcaSFlorent Fourcot (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) && 866399c07deSArnaldo Carvalho de Melo np->rxopt.bits.rxflow) || 867399c07deSArnaldo Carvalho de Melo (opt->srcrt && (np->rxopt.bits.srcrt || 868399c07deSArnaldo Carvalho de Melo np->rxopt.bits.osrcrt)) || 869399c07deSArnaldo Carvalho de Melo ((opt->dst1 || opt->dst0) && 870399c07deSArnaldo Carvalho de Melo (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts))) 87192113bfdSEric Dumazet return true; 872399c07deSArnaldo Carvalho de Melo } 87392113bfdSEric Dumazet return false; 874399c07deSArnaldo Carvalho de Melo } 875399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted); 876399c07deSArnaldo Carvalho de Melo 8777546dd97SStephen Hemminger static struct packet_type ipv6_packet_type __read_mostly = { 87809640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_IPV6), 879662397fdSYOSHIFUJI Hideaki .func = ipv6_rcv, 880d8269e2cSEdward Cree .list_func = ipv6_list_rcv, 88122061d80SVlad Yasevich }; 88222061d80SVlad Yasevich 883662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void) 884662397fdSYOSHIFUJI Hideaki { 885662397fdSYOSHIFUJI Hideaki dev_add_pack(&ipv6_packet_type); 886662397fdSYOSHIFUJI Hideaki return 0; 887662397fdSYOSHIFUJI Hideaki } 888662397fdSYOSHIFUJI Hideaki 889662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void) 890662397fdSYOSHIFUJI Hideaki { 891662397fdSYOSHIFUJI Hideaki dev_remove_pack(&ipv6_packet_type); 892662397fdSYOSHIFUJI Hideaki } 893662397fdSYOSHIFUJI Hideaki 894e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net) 895e43291cbSDenis V. Lunev { 896827da44cSJohn Stultz int i; 897827da44cSJohn Stultz 898698365faSWANG Cong net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib); 899698365faSWANG Cong if (!net->mib.udp_stats_in6) 9000c7ed677SDenis V. Lunev return -ENOMEM; 901698365faSWANG Cong net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib); 902698365faSWANG Cong if (!net->mib.udplite_stats_in6) 903be713a44SDenis V. Lunev goto err_udplite_mib; 904698365faSWANG Cong net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib); 905698365faSWANG Cong if (!net->mib.ipv6_statistics) 9069261e537SDenis V. Lunev goto err_ip_mib; 907827da44cSJohn Stultz 908827da44cSJohn Stultz for_each_possible_cpu(i) { 909827da44cSJohn Stultz struct ipstats_mib *af_inet6_stats; 910698365faSWANG Cong af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i); 911827da44cSJohn Stultz u64_stats_init(&af_inet6_stats->syncp); 912827da44cSJohn Stultz } 913827da44cSJohn Stultz 914827da44cSJohn Stultz 915698365faSWANG Cong net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib); 916698365faSWANG Cong if (!net->mib.icmpv6_statistics) 9179261e537SDenis V. Lunev goto err_icmp_mib; 9182a24444fSEric Dumazet net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib), 9192a24444fSEric Dumazet GFP_KERNEL); 9202a24444fSEric Dumazet if (!net->mib.icmpv6msg_statistics) 9219261e537SDenis V. Lunev goto err_icmpmsg_mib; 922e43291cbSDenis V. Lunev return 0; 923be713a44SDenis V. Lunev 9249261e537SDenis V. Lunev err_icmpmsg_mib: 925698365faSWANG Cong free_percpu(net->mib.icmpv6_statistics); 9269261e537SDenis V. Lunev err_icmp_mib: 927698365faSWANG Cong free_percpu(net->mib.ipv6_statistics); 9289261e537SDenis V. Lunev err_ip_mib: 929698365faSWANG Cong free_percpu(net->mib.udplite_stats_in6); 930be713a44SDenis V. Lunev err_udplite_mib: 931698365faSWANG Cong free_percpu(net->mib.udp_stats_in6); 932be713a44SDenis V. Lunev return -ENOMEM; 933e43291cbSDenis V. Lunev } 934e43291cbSDenis V. Lunev 9352c8c1e72SAlexey Dobriyan static void ipv6_cleanup_mibs(struct net *net) 936e43291cbSDenis V. Lunev { 937698365faSWANG Cong free_percpu(net->mib.udp_stats_in6); 938698365faSWANG Cong free_percpu(net->mib.udplite_stats_in6); 939698365faSWANG Cong free_percpu(net->mib.ipv6_statistics); 940698365faSWANG Cong free_percpu(net->mib.icmpv6_statistics); 9412a24444fSEric Dumazet kfree(net->mib.icmpv6msg_statistics); 942e43291cbSDenis V. Lunev } 943e43291cbSDenis V. Lunev 944e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net) 94581c1c178SDaniel Lezcano { 9460c96d8c5SDaniel Lezcano int err = 0; 9470c96d8c5SDaniel Lezcano 94899bc9c4eSDaniel Lezcano net->ipv6.sysctl.bindv6only = 0; 94941a76906SDaniel Lezcano net->ipv6.sysctl.icmpv6_time = 1*HZ; 950e6f86b0fSVirgile Jarry net->ipv6.sysctl.icmpv6_echo_ignore_all = 0; 95103f1ecccSStephen Suryaputra net->ipv6.sysctl.icmpv6_echo_ignore_multicast = 0; 9520b03a5caSStephen Suryaputra net->ipv6.sysctl.icmpv6_echo_ignore_anycast = 0; 9537ab75456SMahesh Bandewar net->ipv6.sysctl.icmpv6_error_anycast_as_unicast = 0; 9540bc19985SStephen Suryaputra 9550bc19985SStephen Suryaputra /* By default, rate limit error messages. 9560bc19985SStephen Suryaputra * Except for pmtu discovery, it would break it. 9570bc19985SStephen Suryaputra * proc_do_large_bitmap needs pointer to the bitmap. 9580bc19985SStephen Suryaputra */ 9590bc19985SStephen Suryaputra bitmap_set(net->ipv6.sysctl.icmpv6_ratemask, 0, ICMPV6_ERRMSG_MAX + 1); 9600bc19985SStephen Suryaputra bitmap_clear(net->ipv6.sysctl.icmpv6_ratemask, ICMPV6_PKT_TOOBIG, 1); 9610bc19985SStephen Suryaputra net->ipv6.sysctl.icmpv6_ratemask_ptr = net->ipv6.sysctl.icmpv6_ratemask; 9620bc19985SStephen Suryaputra 9636444f72bSFlorent Fourcot net->ipv6.sysctl.flowlabel_consistency = 1; 96442240901STom Herbert net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS; 9651855b7c3SHannes Frederic Sowa net->ipv6.sysctl.idgen_retries = 3; 9661855b7c3SHannes Frederic Sowa net->ipv6.sysctl.idgen_delay = 1 * HZ; 967be26849bSTom Herbert net->ipv6.sysctl.flowlabel_state_ranges = 0; 96847d3d7acSTom Herbert net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT; 96947d3d7acSTom Herbert net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT; 97047d3d7acSTom Herbert net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN; 97147d3d7acSTom Herbert net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN; 972907eea48SAmit Cohen net->ipv6.sysctl.fib_notify_on_flag_change = 0; 973812918c4SHannes Frederic Sowa atomic_set(&net->ipv6.fib6_sernum, 1); 974e71e0349SDaniel Lezcano 9759ee11f0fSJustin Iurman net->ipv6.sysctl.ioam6_id = IOAM6_DEFAULT_ID; 9769ee11f0fSJustin Iurman net->ipv6.sysctl.ioam6_id_wide = IOAM6_DEFAULT_ID_WIDE; 9779ee11f0fSJustin Iurman 978e43291cbSDenis V. Lunev err = ipv6_init_mibs(net); 979e43291cbSDenis V. Lunev if (err) 980e43291cbSDenis V. Lunev return err; 9810c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS 9820c96d8c5SDaniel Lezcano err = udp6_proc_init(net); 9830c96d8c5SDaniel Lezcano if (err) 9840c96d8c5SDaniel Lezcano goto out; 9856f8b13bcSDaniel Lezcano err = tcp6_proc_init(net); 9866f8b13bcSDaniel Lezcano if (err) 9876f8b13bcSDaniel Lezcano goto proc_tcp6_fail; 9886ab57e7eSDaniel Lezcano err = ac6_proc_init(net); 9896ab57e7eSDaniel Lezcano if (err) 9906ab57e7eSDaniel Lezcano goto proc_ac6_fail; 9910c96d8c5SDaniel Lezcano #endif 9920c96d8c5SDaniel Lezcano return err; 9936f8b13bcSDaniel Lezcano 9946f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS 9956ab57e7eSDaniel Lezcano proc_ac6_fail: 9966ab57e7eSDaniel Lezcano tcp6_proc_exit(net); 9976f8b13bcSDaniel Lezcano proc_tcp6_fail: 9986f8b13bcSDaniel Lezcano udp6_proc_exit(net); 999e43291cbSDenis V. Lunev out: 1000e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net); 1001e43291cbSDenis V. Lunev return err; 10026f8b13bcSDaniel Lezcano #endif 100381c1c178SDaniel Lezcano } 100481c1c178SDaniel Lezcano 10052c8c1e72SAlexey Dobriyan static void __net_exit inet6_net_exit(struct net *net) 100681c1c178SDaniel Lezcano { 10070c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS 10080c96d8c5SDaniel Lezcano udp6_proc_exit(net); 10096f8b13bcSDaniel Lezcano tcp6_proc_exit(net); 10106ab57e7eSDaniel Lezcano ac6_proc_exit(net); 10110c96d8c5SDaniel Lezcano #endif 1012e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net); 101381c1c178SDaniel Lezcano } 101481c1c178SDaniel Lezcano 101581c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = { 101681c1c178SDaniel Lezcano .init = inet6_net_init, 101781c1c178SDaniel Lezcano .exit = inet6_net_exit, 101881c1c178SDaniel Lezcano }; 101981c1c178SDaniel Lezcano 10209b0a6a9dSPeter Oskolkov static int ipv6_route_input(struct sk_buff *skb) 10219b0a6a9dSPeter Oskolkov { 10229b0a6a9dSPeter Oskolkov ip6_route_input(skb); 10239b0a6a9dSPeter Oskolkov return skb_dst(skb)->error; 10249b0a6a9dSPeter Oskolkov } 10259b0a6a9dSPeter Oskolkov 10265f81bd2eSCong Wang static const struct ipv6_stub ipv6_stub_impl = { 10275f81bd2eSCong Wang .ipv6_sock_mc_join = ipv6_sock_mc_join, 10285f81bd2eSCong Wang .ipv6_sock_mc_drop = ipv6_sock_mc_drop, 10296c8991f4SSabrina Dubroca .ipv6_dst_lookup_flow = ip6_dst_lookup_flow, 10309b0a6a9dSPeter Oskolkov .ipv6_route_input = ipv6_route_input, 103165a2022eSDavid Ahern .fib6_get_table = fib6_get_table, 103265a2022eSDavid Ahern .fib6_table_lookup = fib6_table_lookup, 103365a2022eSDavid Ahern .fib6_lookup = fib6_lookup, 1034b1d40991SDavid Ahern .fib6_select_path = fib6_select_path, 1035901731b8SDavid Ahern .ip6_mtu_from_fib6 = ip6_mtu_from_fib6, 10361aefd3deSDavid Ahern .fib6_nh_init = fib6_nh_init, 10371aefd3deSDavid Ahern .fib6_nh_release = fib6_nh_release, 10388837cbbfSNikolay Aleksandrov .fib6_nh_release_dsts = fib6_nh_release_dsts, 1039cdaa16a4SDavid Ahern .fib6_update_sernum = fib6_update_sernum_stub, 104019a3b7eeSDavid Ahern .fib6_rt_update = fib6_rt_update, 104168a9b13dSDavid Ahern .ip6_del_rt = ip6_del_rt, 10425f81bd2eSCong Wang .udpv6_encap_enable = udpv6_encap_enable, 1043f564f45cSCong Wang .ndisc_send_na = ndisc_send_na, 10440146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_XFRM) 10453e50ddd8SFlorian Westphal .xfrm6_local_rxpmtu = xfrm6_local_rxpmtu, 10460146dca7SSabrina Dubroca .xfrm6_udp_encap_rcv = xfrm6_udp_encap_rcv, 104726333c37SSabrina Dubroca .xfrm6_rcv_encap = xfrm6_rcv_encap, 10480146dca7SSabrina Dubroca #endif 1049e15a00aaSCong Wang .nd_tbl = &nd_tbl, 10501d97898bSwenxu .ipv6_fragment = ip6_fragment, 1051504a4011SAndreas Roeseler .ipv6_dev_find = ipv6_dev_find, 10525f81bd2eSCong Wang }; 10535f81bd2eSCong Wang 1054d74bad4eSAndrey Ignatov static const struct ipv6_bpf_stub ipv6_bpf_stub_impl = { 1055d74bad4eSAndrey Ignatov .inet6_bind = __inet6_bind, 10568a615c6bSJoe Stringer .udp6_lib_lookup = __udp6_lib_lookup, 105775b64b68SMartin KaFai Lau .ipv6_setsockopt = do_ipv6_setsockopt, 105838566ec0SMartin KaFai Lau .ipv6_getsockopt = do_ipv6_getsockopt, 1059d74bad4eSAndrey Ignatov }; 1060d74bad4eSAndrey Ignatov 10611da177e4SLinus Torvalds static int __init inet6_init(void) 10621da177e4SLinus Torvalds { 10631da177e4SLinus Torvalds struct list_head *r; 1064fe7ca2e1SBrian Haley int err = 0; 10651da177e4SLinus Torvalds 1066b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct inet6_skb_parm)); 1067ef047f5eSYOSHIFUJI Hideaki 1068fe7ca2e1SBrian Haley /* Register the socket-side information for inet6_create. */ 1069fe7ca2e1SBrian Haley for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r) 1070fe7ca2e1SBrian Haley INIT_LIST_HEAD(r); 1071fe7ca2e1SBrian Haley 107276dd0728SIdo Schimmel raw_hashinfo_init(&raw_v6_hashinfo); 107376dd0728SIdo Schimmel 107456d417b1SBrian Haley if (disable_ipv6_mod) { 1075f3213831SJoe Perches pr_info("Loaded, but administratively disabled, reboot required to enable\n"); 1076fe7ca2e1SBrian Haley goto out; 1077fe7ca2e1SBrian Haley } 1078fe7ca2e1SBrian Haley 10791da177e4SLinus Torvalds err = proto_register(&tcpv6_prot, 1); 10801da177e4SLinus Torvalds if (err) 10811da177e4SLinus Torvalds goto out; 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds err = proto_register(&udpv6_prot, 1); 10841da177e4SLinus Torvalds if (err) 10851da177e4SLinus Torvalds goto out_unregister_tcp_proto; 10861da177e4SLinus Torvalds 1087ba4e58ecSGerrit Renker err = proto_register(&udplitev6_prot, 1); 10881da177e4SLinus Torvalds if (err) 10891da177e4SLinus Torvalds goto out_unregister_udp_proto; 10901da177e4SLinus Torvalds 1091ba4e58ecSGerrit Renker err = proto_register(&rawv6_prot, 1); 1092ba4e58ecSGerrit Renker if (err) 1093ba4e58ecSGerrit Renker goto out_unregister_udplite_proto; 1094ba4e58ecSGerrit Renker 10956d0bfe22SLorenzo Colitti err = proto_register(&pingv6_prot, 1); 10966d0bfe22SLorenzo Colitti if (err) 1097a03dc36bSSabrina Dubroca goto out_unregister_raw_proto; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds /* We MUST register RAW sockets before we create the ICMP6, 11001da177e4SLinus Torvalds * IGMP6, or NDISC control sockets. 11011da177e4SLinus Torvalds */ 11027f4e4868SDaniel Lezcano err = rawv6_init(); 11037f4e4868SDaniel Lezcano if (err) 1104a03dc36bSSabrina Dubroca goto out_unregister_ping_proto; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds /* Register the family here so that the init calls below will 11071da177e4SLinus Torvalds * be able to create sockets. (?? is this dangerous ??) 11081da177e4SLinus Torvalds */ 11098eb55910SDavid S. Miller err = sock_register(&inet6_family_ops); 11108eb55910SDavid S. Miller if (err) 11117f4e4868SDaniel Lezcano goto out_sock_register_fail; 11121da177e4SLinus Torvalds 11131da177e4SLinus Torvalds /* 11141da177e4SLinus Torvalds * ipngwg API draft makes clear that the correct semantics 11151da177e4SLinus Torvalds * for TCP and UDP is to consider one TCP and UDP instance 111625985edcSLucas De Marchi * in a host available by both INET and INET6 APIs and 11171da177e4SLinus Torvalds * able to communicate via both network protocols. 11181da177e4SLinus Torvalds */ 11191da177e4SLinus Torvalds 112081c1c178SDaniel Lezcano err = register_pernet_subsys(&inet6_net_ops); 112181c1c178SDaniel Lezcano if (err) 112281c1c178SDaniel Lezcano goto register_pernet_fail; 1123623d1a1aSWang Chen err = ip6_mr_init(); 1124623d1a1aSWang Chen if (err) 1125623d1a1aSWang Chen goto ipmr_fail; 112615e66807SWANG Cong err = icmpv6_init(); 112715e66807SWANG Cong if (err) 112815e66807SWANG Cong goto icmp_fail; 11299b0f976fSDenis V. Lunev err = ndisc_init(); 11301da177e4SLinus Torvalds if (err) 11311da177e4SLinus Torvalds goto ndisc_fail; 11329b0f976fSDenis V. Lunev err = igmp6_init(); 11331da177e4SLinus Torvalds if (err) 11341da177e4SLinus Torvalds goto igmp_fail; 11355f81bd2eSCong Wang 11362cc7d573SHarald Welte err = ipv6_netfilter_init(); 11372cc7d573SHarald Welte if (err) 11382cc7d573SHarald Welte goto netfilter_fail; 11391da177e4SLinus Torvalds /* Create /proc/foo6 entries. */ 11401da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 11411da177e4SLinus Torvalds err = -ENOMEM; 11421da177e4SLinus Torvalds if (raw6_proc_init()) 11431da177e4SLinus Torvalds goto proc_raw6_fail; 1144ba4e58ecSGerrit Renker if (udplite6_proc_init()) 1145ba4e58ecSGerrit Renker goto proc_udplite6_fail; 11461da177e4SLinus Torvalds if (ipv6_misc_proc_init()) 11471da177e4SLinus Torvalds goto proc_misc6_fail; 11481da177e4SLinus Torvalds if (if6_proc_init()) 11491da177e4SLinus Torvalds goto proc_if6_fail; 11501da177e4SLinus Torvalds #endif 1151e2fddf5eSDaniel Lezcano err = ip6_route_init(); 1152e2fddf5eSDaniel Lezcano if (err) 1153e2fddf5eSDaniel Lezcano goto ip6_route_fail; 11542c861cc6SMichal Kubeček err = ndisc_late_init(); 11552c861cc6SMichal Kubeček if (err) 11562c861cc6SMichal Kubeček goto ndisc_late_fail; 11570a3e78acSDaniel Lezcano err = ip6_flowlabel_init(); 11580a3e78acSDaniel Lezcano if (err) 11590a3e78acSDaniel Lezcano goto ip6_flowlabel_fail; 11602384d025SJeff Barnhill err = ipv6_anycast_init(); 11612384d025SJeff Barnhill if (err) 11622384d025SJeff Barnhill goto ipv6_anycast_fail; 11631da177e4SLinus Torvalds err = addrconf_init(); 11641da177e4SLinus Torvalds if (err) 11651da177e4SLinus Torvalds goto addrconf_fail; 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds /* Init v6 extension headers. */ 1168248b238dSDaniel Lezcano err = ipv6_exthdrs_init(); 1169248b238dSDaniel Lezcano if (err) 1170248b238dSDaniel Lezcano goto ipv6_exthdrs_fail; 1171248b238dSDaniel Lezcano 1172853cbbaaSDaniel Lezcano err = ipv6_frag_init(); 1173853cbbaaSDaniel Lezcano if (err) 1174853cbbaaSDaniel Lezcano goto ipv6_frag_fail; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds /* Init v6 transport protocols. */ 11777f4e4868SDaniel Lezcano err = udpv6_init(); 11787f4e4868SDaniel Lezcano if (err) 11797f4e4868SDaniel Lezcano goto udpv6_fail; 1180e2ed4052SHerbert Xu 11817f4e4868SDaniel Lezcano err = udplitev6_init(); 11827f4e4868SDaniel Lezcano if (err) 11837f4e4868SDaniel Lezcano goto udplitev6_fail; 11847f4e4868SDaniel Lezcano 1185a6024562STom Herbert err = udpv6_offload_init(); 1186a6024562STom Herbert if (err) 1187a6024562STom Herbert goto udpv6_offload_fail; 1188a6024562STom Herbert 11897f4e4868SDaniel Lezcano err = tcpv6_init(); 11907f4e4868SDaniel Lezcano if (err) 11917f4e4868SDaniel Lezcano goto tcpv6_fail; 11927f4e4868SDaniel Lezcano 11937f4e4868SDaniel Lezcano err = ipv6_packet_init(); 11947f4e4868SDaniel Lezcano if (err) 11957f4e4868SDaniel Lezcano goto ipv6_packet_fail; 119694911fe3SBenjamin Thery 11976d0bfe22SLorenzo Colitti err = pingv6_init(); 11986d0bfe22SLorenzo Colitti if (err) 11996d0bfe22SLorenzo Colitti goto pingv6_fail; 12006d0bfe22SLorenzo Colitti 1201cb72d382SHuw Davies err = calipso_init(); 1202cb72d382SHuw Davies if (err) 1203cb72d382SHuw Davies goto calipso_fail; 1204cb72d382SHuw Davies 1205915d7e5eSDavid Lebrun err = seg6_init(); 1206915d7e5eSDavid Lebrun if (err) 1207915d7e5eSDavid Lebrun goto seg6_fail; 1208915d7e5eSDavid Lebrun 1209a7a29f9cSAlexander Aring err = rpl_init(); 1210a7a29f9cSAlexander Aring if (err) 1211a7a29f9cSAlexander Aring goto rpl_fail; 1212a7a29f9cSAlexander Aring 12139ee11f0fSJustin Iurman err = ioam6_init(); 12149ee11f0fSJustin Iurman if (err) 12159ee11f0fSJustin Iurman goto ioam6_fail; 12169ee11f0fSJustin Iurman 1217382ed724SVlad Yasevich err = igmp6_late_init(); 1218382ed724SVlad Yasevich if (err) 1219382ed724SVlad Yasevich goto igmp6_late_err; 1220382ed724SVlad Yasevich 122194911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL 122294911fe3SBenjamin Thery err = ipv6_sysctl_register(); 122394911fe3SBenjamin Thery if (err) 122494911fe3SBenjamin Thery goto sysctl_fail; 122594911fe3SBenjamin Thery #endif 1226b7d6df57SPaolo Abeni 1227b7d6df57SPaolo Abeni /* ensure that ipv6 stubs are visible only after ipv6 is ready */ 1228b7d6df57SPaolo Abeni wmb(); 1229b7d6df57SPaolo Abeni ipv6_stub = &ipv6_stub_impl; 1230d74bad4eSAndrey Ignatov ipv6_bpf_stub = &ipv6_bpf_stub_impl; 12311da177e4SLinus Torvalds out: 12321da177e4SLinus Torvalds return err; 12331da177e4SLinus Torvalds 123494911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL 123594911fe3SBenjamin Thery sysctl_fail: 1236382ed724SVlad Yasevich igmp6_late_cleanup(); 123794911fe3SBenjamin Thery #endif 1238382ed724SVlad Yasevich igmp6_late_err: 12399ee11f0fSJustin Iurman ioam6_exit(); 12409ee11f0fSJustin Iurman ioam6_fail: 1241a7a29f9cSAlexander Aring rpl_exit(); 1242a7a29f9cSAlexander Aring rpl_fail: 1243382ed724SVlad Yasevich seg6_exit(); 1244915d7e5eSDavid Lebrun seg6_fail: 1245915d7e5eSDavid Lebrun calipso_exit(); 1246cb72d382SHuw Davies calipso_fail: 1247cb72d382SHuw Davies pingv6_exit(); 12486d0bfe22SLorenzo Colitti pingv6_fail: 1249eca42aafSVlad Yasevich ipv6_packet_cleanup(); 12507f4e4868SDaniel Lezcano ipv6_packet_fail: 12517f4e4868SDaniel Lezcano tcpv6_exit(); 12527f4e4868SDaniel Lezcano tcpv6_fail: 1253a6024562STom Herbert udpv6_offload_exit(); 1254a6024562STom Herbert udpv6_offload_fail: 12557f4e4868SDaniel Lezcano udplitev6_exit(); 12567f4e4868SDaniel Lezcano udplitev6_fail: 12577f4e4868SDaniel Lezcano udpv6_exit(); 12587f4e4868SDaniel Lezcano udpv6_fail: 12597f4e4868SDaniel Lezcano ipv6_frag_exit(); 1260853cbbaaSDaniel Lezcano ipv6_frag_fail: 1261853cbbaaSDaniel Lezcano ipv6_exthdrs_exit(); 1262248b238dSDaniel Lezcano ipv6_exthdrs_fail: 1263248b238dSDaniel Lezcano addrconf_cleanup(); 12641da177e4SLinus Torvalds addrconf_fail: 12652384d025SJeff Barnhill ipv6_anycast_cleanup(); 12662384d025SJeff Barnhill ipv6_anycast_fail: 12671da177e4SLinus Torvalds ip6_flowlabel_cleanup(); 12680a3e78acSDaniel Lezcano ip6_flowlabel_fail: 12692c861cc6SMichal Kubeček ndisc_late_cleanup(); 12702c861cc6SMichal Kubeček ndisc_late_fail: 12711da177e4SLinus Torvalds ip6_route_cleanup(); 1272e2fddf5eSDaniel Lezcano ip6_route_fail: 12731da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 12741da177e4SLinus Torvalds if6_proc_exit(); 12751da177e4SLinus Torvalds proc_if6_fail: 12761da177e4SLinus Torvalds ipv6_misc_proc_exit(); 12771da177e4SLinus Torvalds proc_misc6_fail: 1278ba4e58ecSGerrit Renker udplite6_proc_exit(); 1279ba4e58ecSGerrit Renker proc_udplite6_fail: 12801da177e4SLinus Torvalds raw6_proc_exit(); 12811da177e4SLinus Torvalds proc_raw6_fail: 12821da177e4SLinus Torvalds #endif 12832cc7d573SHarald Welte ipv6_netfilter_fini(); 12842cc7d573SHarald Welte netfilter_fail: 12851da177e4SLinus Torvalds igmp6_cleanup(); 12861da177e4SLinus Torvalds igmp_fail: 12871da177e4SLinus Torvalds ndisc_cleanup(); 12881da177e4SLinus Torvalds ndisc_fail: 128915e66807SWANG Cong icmpv6_cleanup(); 1290afe49de4SSabrina Dubroca icmp_fail: 1291afe49de4SSabrina Dubroca ip6_mr_cleanup(); 1292afe49de4SSabrina Dubroca ipmr_fail: 1293afe49de4SSabrina Dubroca unregister_pernet_subsys(&inet6_net_ops); 129481c1c178SDaniel Lezcano register_pernet_fail: 12958eb55910SDavid S. Miller sock_unregister(PF_INET6); 1296e2fddf5eSDaniel Lezcano rtnl_unregister_all(PF_INET6); 12977f4e4868SDaniel Lezcano out_sock_register_fail: 12987f4e4868SDaniel Lezcano rawv6_exit(); 12996d0bfe22SLorenzo Colitti out_unregister_ping_proto: 13006d0bfe22SLorenzo Colitti proto_unregister(&pingv6_prot); 13011da177e4SLinus Torvalds out_unregister_raw_proto: 13021da177e4SLinus Torvalds proto_unregister(&rawv6_prot); 1303ba4e58ecSGerrit Renker out_unregister_udplite_proto: 1304ba4e58ecSGerrit Renker proto_unregister(&udplitev6_prot); 13051da177e4SLinus Torvalds out_unregister_udp_proto: 13061da177e4SLinus Torvalds proto_unregister(&udpv6_prot); 13071da177e4SLinus Torvalds out_unregister_tcp_proto: 13081da177e4SLinus Torvalds proto_unregister(&tcpv6_prot); 13091da177e4SLinus Torvalds goto out; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds module_init(inet6_init); 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6); 1314