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
ipv6_mod_enabled(void)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
inet6_sk_generic(struct sock * sk)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
inet6_sock_destruct(struct sock * sk)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
inet6_create(struct net * net,struct socket * sock,int protocol,int kern)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
202050855e3SZhengchao Shao if (INET_PROTOSW_ICSK & answer_flags)
203050855e3SZhengchao Shao inet_init_csk_locks(sk);
204050855e3SZhengchao Shao
2051da177e4SLinus Torvalds inet = inet_sk(sk);
206b1c0356aSEric Dumazet inet_assign_bit(IS_ICSK, sk, INET_PROTOSW_ICSK & answer_flags);
2071da177e4SLinus Torvalds
2081da177e4SLinus Torvalds if (SOCK_RAW == sock->type) {
209c720c7e8SEric Dumazet inet->inet_num = protocol;
2101da177e4SLinus Torvalds if (IPPROTO_RAW == protocol)
211cafbe182SEric Dumazet inet_set_bit(HDRINCL, sk);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds
214d38afeecSKuniyuki Iwashima sk->sk_destruct = inet6_sock_destruct;
2151da177e4SLinus Torvalds sk->sk_family = PF_INET6;
2161da177e4SLinus Torvalds sk->sk_protocol = protocol;
2171da177e4SLinus Torvalds
2181da177e4SLinus Torvalds sk->sk_backlog_rcv = answer->prot->backlog_rcv;
2191da177e4SLinus Torvalds
2201da177e4SLinus Torvalds inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
2211da177e4SLinus Torvalds np->hop_limit = -1;
222f935aa9eSDavid S. Miller np->mcast_hops = IPV6_DEFAULT_MCASTHOPS;
2231da177e4SLinus Torvalds np->mc_loop = 1;
22415033f04SAndre Naujoks np->mc_all = 1;
2251da177e4SLinus Torvalds np->pmtudisc = IPV6_PMTUDISC_WANT;
226a346abe0SEric Dumazet np->repflow = net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ESTABLISHED;
2279fe516baSEric Dumazet sk->sk_ipv6only = net->ipv6.sysctl.bindv6only;
228c11204c7SKevin Yang sk->sk_txrehash = READ_ONCE(net->core.sysctl_txrehash);
2291da177e4SLinus Torvalds
2301da177e4SLinus Torvalds /* Init the ipv4 part of the socket since we can have sockets
2311da177e4SLinus Torvalds * using v6 API for ipv4.
2321da177e4SLinus Torvalds */
2331da177e4SLinus Torvalds inet->uc_ttl = -1;
2341da177e4SLinus Torvalds
235b09bde5cSEric Dumazet inet_set_bit(MC_LOOP, sk);
2361da177e4SLinus Torvalds inet->mc_ttl = 1;
2371da177e4SLinus Torvalds inet->mc_index = 0;
23888e2ca30STaehee Yoo RCU_INIT_POINTER(inet->mc_list, NULL);
2394c507d28SJiri Benc inet->rcv_tos = 0;
2401da177e4SLinus Torvalds
2410968d2a4SKuniyuki Iwashima if (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc))
2421da177e4SLinus Torvalds inet->pmtudisc = IP_PMTUDISC_DONT;
2431da177e4SLinus Torvalds else
2441da177e4SLinus Torvalds inet->pmtudisc = IP_PMTUDISC_WANT;
2451da177e4SLinus Torvalds
246c720c7e8SEric Dumazet if (inet->inet_num) {
2471da177e4SLinus Torvalds /* It assumes that any protocol which allows
2481da177e4SLinus Torvalds * the user to assign a number at socket
2491da177e4SLinus Torvalds * creation time automatically shares.
2501da177e4SLinus Torvalds */
251c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num);
252086c653fSCraig Gallek err = sk->sk_prot->hash(sk);
253086c653fSCraig Gallek if (err) {
254086c653fSCraig Gallek sk_common_release(sk);
255086c653fSCraig Gallek goto out;
256086c653fSCraig Gallek }
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds if (sk->sk_prot->init) {
259af1afe86SYOSHIFUJI Hideaki err = sk->sk_prot->init(sk);
260af1afe86SYOSHIFUJI Hideaki if (err) {
2611da177e4SLinus Torvalds sk_common_release(sk);
2621da177e4SLinus Torvalds goto out;
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds }
26561023658SDavid Ahern
26661023658SDavid Ahern if (!kern) {
26761023658SDavid Ahern err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
26861023658SDavid Ahern if (err) {
26961023658SDavid Ahern sk_common_release(sk);
27061023658SDavid Ahern goto out;
27161023658SDavid Ahern }
27261023658SDavid Ahern }
2731da177e4SLinus Torvalds out:
274af1afe86SYOSHIFUJI Hideaki return err;
2751da177e4SLinus Torvalds out_rcu_unlock:
2761da177e4SLinus Torvalds rcu_read_unlock();
2771da177e4SLinus Torvalds goto out;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds
__inet6_bind(struct sock * sk,struct sockaddr * uaddr,int addr_len,u32 flags)280032234d8SDavid Ahern static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
281cb0721c7SStanislav Fomichev u32 flags)
2823679d585SAndrey Ignatov {
2833679d585SAndrey Ignatov struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
2843679d585SAndrey Ignatov struct inet_sock *inet = inet_sk(sk);
2853679d585SAndrey Ignatov struct ipv6_pinfo *np = inet6_sk(sk);
2863679d585SAndrey Ignatov struct net *net = sock_net(sk);
2873679d585SAndrey Ignatov __be32 v4addr = 0;
2883679d585SAndrey Ignatov unsigned short snum;
2893679d585SAndrey Ignatov bool saved_ipv6only;
2903679d585SAndrey Ignatov int addr_type = 0;
2913679d585SAndrey Ignatov int err = 0;
2923679d585SAndrey Ignatov
2935a079c30SMarcus Meissner if (addr->sin6_family != AF_INET6)
294c349a528SMarcus Meissner return -EAFNOSUPPORT;
2955a079c30SMarcus Meissner
2961da177e4SLinus Torvalds addr_type = ipv6_addr_type(&addr->sin6_addr);
2973679d585SAndrey Ignatov if ((addr_type & IPV6_ADDR_MULTICAST) && sk->sk_type == SOCK_STREAM)
2981da177e4SLinus Torvalds return -EINVAL;
2991da177e4SLinus Torvalds
3001da177e4SLinus Torvalds snum = ntohs(addr->sin6_port);
30177241217SStanislav Fomichev if (!(flags & BIND_NO_CAP_NET_BIND_SERVICE) &&
30277241217SStanislav Fomichev snum && inet_port_requires_bind_service(net, snum) &&
3034548b683SKrister Johansen !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
3041da177e4SLinus Torvalds return -EACCES;
3051da177e4SLinus Torvalds
306cb0721c7SStanislav Fomichev if (flags & BIND_WITH_LOCK)
3071da177e4SLinus Torvalds lock_sock(sk);
3081da177e4SLinus Torvalds
3091da177e4SLinus Torvalds /* Check these errors (active socket, double bind). */
310c720c7e8SEric Dumazet if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
3111da177e4SLinus Torvalds err = -EINVAL;
3121da177e4SLinus Torvalds goto out;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds
3151da177e4SLinus Torvalds /* Check if the address belongs to the host. */
3161da177e4SLinus Torvalds if (addr_type == IPV6_ADDR_MAPPED) {
317ec90ad33SDavid Ahern struct net_device *dev = NULL;
31863d9950bSVlad Yasevich int chk_addr_ret;
31963d9950bSVlad Yasevich
320783ed5a7SVlad Yasevich /* Binding to v4-mapped address on a v6-only socket
321783ed5a7SVlad Yasevich * makes no sense
322783ed5a7SVlad Yasevich */
32381ee0eb6SKuniyuki Iwashima if (ipv6_only_sock(sk)) {
324783ed5a7SVlad Yasevich err = -EINVAL;
325783ed5a7SVlad Yasevich goto out;
326783ed5a7SVlad Yasevich }
32763d9950bSVlad Yasevich
328d4a7e9bbSDavid Ahern rcu_read_lock();
329ec90ad33SDavid Ahern if (sk->sk_bound_dev_if) {
330ec90ad33SDavid Ahern dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
331ec90ad33SDavid Ahern if (!dev) {
332ec90ad33SDavid Ahern err = -ENODEV;
333d4a7e9bbSDavid Ahern goto out_unlock;
334ec90ad33SDavid Ahern }
335ec90ad33SDavid Ahern }
336ec90ad33SDavid Ahern
337a34f0b31SUwe Kleine-König /* Reproduce AF_INET checks to make the bindings consistent */
3381da177e4SLinus Torvalds v4addr = addr->sin6_addr.s6_addr32[3];
339ec90ad33SDavid Ahern chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr);
340d4a7e9bbSDavid Ahern rcu_read_unlock();
341d4a7e9bbSDavid Ahern
3428ff978b8SRiccardo Paolo Bestetti if (!inet_addr_valid_or_nonlocal(net, inet, v4addr,
3438ff978b8SRiccardo Paolo Bestetti chk_addr_ret)) {
344ca6982b8SBruno Prémont err = -EADDRNOTAVAIL;
3451da177e4SLinus Torvalds goto out;
346ca6982b8SBruno Prémont }
3471da177e4SLinus Torvalds } else {
3481da177e4SLinus Torvalds if (addr_type != IPV6_ADDR_ANY) {
3491da177e4SLinus Torvalds struct net_device *dev = NULL;
3501da177e4SLinus Torvalds
35116ba5e8eSEric Dumazet rcu_read_lock();
352842df073SHannes Frederic Sowa if (__ipv6_addr_needs_scope_id(addr_type)) {
3531da177e4SLinus Torvalds if (addr_len >= sizeof(struct sockaddr_in6) &&
3541da177e4SLinus Torvalds addr->sin6_scope_id) {
3551da177e4SLinus Torvalds /* Override any existing binding, if another one
3561da177e4SLinus Torvalds * is supplied by user.
3571da177e4SLinus Torvalds */
3581da177e4SLinus Torvalds sk->sk_bound_dev_if = addr->sin6_scope_id;
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds
3611da177e4SLinus Torvalds /* Binding to link-local address requires an interface */
3621da177e4SLinus Torvalds if (!sk->sk_bound_dev_if) {
3631da177e4SLinus Torvalds err = -EINVAL;
36416ba5e8eSEric Dumazet goto out_unlock;
3651da177e4SLinus Torvalds }
366c5ee0663SDavid Ahern }
367c5ee0663SDavid Ahern
368c5ee0663SDavid Ahern if (sk->sk_bound_dev_if) {
36916ba5e8eSEric Dumazet dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
3701da177e4SLinus Torvalds if (!dev) {
3711da177e4SLinus Torvalds err = -ENODEV;
37216ba5e8eSEric Dumazet goto out_unlock;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds
3761da177e4SLinus Torvalds /* ipv4 addr of the socket is invalid. Only the
3771da177e4SLinus Torvalds * unspecified and mapped address have a v4 equivalent.
3781da177e4SLinus Torvalds */
3791da177e4SLinus Torvalds v4addr = LOOPBACK4_IPV6;
3801da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST)) {
38183ba4645SVincent Bernat if (!ipv6_can_nonlocal_bind(net, inet) &&
3820a513f6aSBalazs Scheidler !ipv6_chk_addr(net, &addr->sin6_addr,
383bfeade08SDaniel Lezcano dev, 0)) {
3841da177e4SLinus Torvalds err = -EADDRNOTAVAIL;
38516ba5e8eSEric Dumazet goto out_unlock;
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds }
38816ba5e8eSEric Dumazet rcu_read_unlock();
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds
392c720c7e8SEric Dumazet inet->inet_rcv_saddr = v4addr;
393c720c7e8SEric Dumazet inet->inet_saddr = v4addr;
3941da177e4SLinus Torvalds
395efe4208fSEric Dumazet sk->sk_v6_rcv_saddr = addr->sin6_addr;
3961da177e4SLinus Torvalds
3971da177e4SLinus Torvalds if (!(addr_type & IPV6_ADDR_MULTICAST))
3984e3fd7a0SAlexey Dobriyan np->saddr = addr->sin6_addr;
3991da177e4SLinus Torvalds
4007ece54a6SMartin KaFai Lau saved_ipv6only = sk->sk_ipv6only;
4017ece54a6SMartin KaFai Lau if (addr_type != IPV6_ADDR_ANY && addr_type != IPV6_ADDR_MAPPED)
4027ece54a6SMartin KaFai Lau sk->sk_ipv6only = 1;
4037ece54a6SMartin KaFai Lau
4041da177e4SLinus Torvalds /* Make sure we are allowed to bind here. */
405ca571e2eSEric Dumazet if (snum || !(inet_test_bit(BIND_ADDRESS_NO_PORT, sk) ||
406cb0721c7SStanislav Fomichev (flags & BIND_FORCE_ADDRESS_NO_PORT))) {
4077a7160edSKuniyuki Iwashima err = sk->sk_prot->get_port(sk, snum);
4087a7160edSKuniyuki Iwashima if (err) {
4097ece54a6SMartin KaFai Lau sk->sk_ipv6only = saved_ipv6only;
4101da177e4SLinus Torvalds inet_reset_saddr(sk);
4111da177e4SLinus Torvalds goto out;
4121da177e4SLinus Torvalds }
4138086fbafSStanislav Fomichev if (!(flags & BIND_FROM_BPF)) {
414aac3fc32SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk);
415aac3fc32SAndrey Ignatov if (err) {
416aac3fc32SAndrey Ignatov sk->sk_ipv6only = saved_ipv6only;
417aac3fc32SAndrey Ignatov inet_reset_saddr(sk);
41891a760b2SMenglong Dong if (sk->sk_prot->put_port)
41991a760b2SMenglong Dong sk->sk_prot->put_port(sk);
420aac3fc32SAndrey Ignatov goto out;
421aac3fc32SAndrey Ignatov }
422aac3fc32SAndrey Ignatov }
4238086fbafSStanislav Fomichev }
4241da177e4SLinus Torvalds
4257ece54a6SMartin KaFai Lau if (addr_type != IPV6_ADDR_ANY)
4261da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
4271da177e4SLinus Torvalds if (snum)
4281da177e4SLinus Torvalds sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
429c720c7e8SEric Dumazet inet->inet_sport = htons(inet->inet_num);
430c720c7e8SEric Dumazet inet->inet_dport = 0;
431c720c7e8SEric Dumazet inet->inet_daddr = 0;
4321da177e4SLinus Torvalds out:
433cb0721c7SStanislav Fomichev if (flags & BIND_WITH_LOCK)
4341da177e4SLinus Torvalds release_sock(sk);
4351da177e4SLinus Torvalds return err;
43616ba5e8eSEric Dumazet out_unlock:
43716ba5e8eSEric Dumazet rcu_read_unlock();
43816ba5e8eSEric Dumazet goto out;
4391da177e4SLinus Torvalds }
4407159039aSYOSHIFUJI Hideaki
inet6_bind_sk(struct sock * sk,struct sockaddr * uaddr,int addr_len)441e6d360ffSPaolo Abeni int inet6_bind_sk(struct sock *sk, struct sockaddr *uaddr, int addr_len)
442032234d8SDavid Ahern {
44377241217SStanislav Fomichev u32 flags = BIND_WITH_LOCK;
444086d4905SEric Dumazet const struct proto *prot;
445032234d8SDavid Ahern int err = 0;
446032234d8SDavid Ahern
447086d4905SEric Dumazet /* IPV6_ADDRFORM can change sk->sk_prot under us. */
448086d4905SEric Dumazet prot = READ_ONCE(sk->sk_prot);
449032234d8SDavid Ahern /* If the socket has its own bind function then use it. */
450086d4905SEric Dumazet if (prot->bind)
451086d4905SEric Dumazet return prot->bind(sk, uaddr, addr_len);
452032234d8SDavid Ahern
453032234d8SDavid Ahern if (addr_len < SIN6_LEN_RFC2133)
454032234d8SDavid Ahern return -EINVAL;
455032234d8SDavid Ahern
456032234d8SDavid Ahern /* BPF prog is run before any checks are done so that if the prog
457032234d8SDavid Ahern * changes context in a wrong way it will be caught.
458032234d8SDavid Ahern */
4596d71331eSDaan De Meyer err = BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr, &addr_len,
4606fc88c35SDave Marchevsky CGROUP_INET6_BIND, &flags);
461032234d8SDavid Ahern if (err)
462032234d8SDavid Ahern return err;
463032234d8SDavid Ahern
46477241217SStanislav Fomichev return __inet6_bind(sk, uaddr, addr_len, flags);
465032234d8SDavid Ahern }
466e6d360ffSPaolo Abeni
467e6d360ffSPaolo Abeni /* bind for INET6 API */
inet6_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)468e6d360ffSPaolo Abeni int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
469e6d360ffSPaolo Abeni {
470e6d360ffSPaolo Abeni return inet6_bind_sk(sock->sk, uaddr, addr_len);
471e6d360ffSPaolo Abeni }
472032234d8SDavid Ahern EXPORT_SYMBOL(inet6_bind);
473032234d8SDavid Ahern
inet6_release(struct socket * sock)4741da177e4SLinus Torvalds int inet6_release(struct socket *sock)
4751da177e4SLinus Torvalds {
4761da177e4SLinus Torvalds struct sock *sk = sock->sk;
4771da177e4SLinus Torvalds
47863159f29SIan Morris if (!sk)
4791da177e4SLinus Torvalds return -EINVAL;
4801da177e4SLinus Torvalds
4811da177e4SLinus Torvalds /* Free mc lists */
4821da177e4SLinus Torvalds ipv6_sock_mc_close(sk);
4831da177e4SLinus Torvalds
4841da177e4SLinus Torvalds /* Free ac lists */
4851da177e4SLinus Torvalds ipv6_sock_ac_close(sk);
4861da177e4SLinus Torvalds
4871da177e4SLinus Torvalds return inet_release(sock);
4881da177e4SLinus Torvalds }
4897159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_release);
4907159039aSYOSHIFUJI Hideaki
inet6_cleanup_sock(struct sock * sk)4911f8c4eebSKuniyuki Iwashima void inet6_cleanup_sock(struct sock *sk)
4921da177e4SLinus Torvalds {
4931da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk);
4941da177e4SLinus Torvalds struct sk_buff *skb;
4951da177e4SLinus Torvalds struct ipv6_txoptions *opt;
4961da177e4SLinus Torvalds
4971da177e4SLinus Torvalds /* Release rx options */
4981da177e4SLinus Torvalds
499647c0c70SEldad Zack skb = xchg(&np->pktoptions, NULL);
5001da177e4SLinus Torvalds kfree_skb(skb);
5011da177e4SLinus Torvalds
502647c0c70SEldad Zack skb = xchg(&np->rxpmtu, NULL);
5034b340ae2SBrian Haley kfree_skb(skb);
5044b340ae2SBrian Haley
5051da177e4SLinus Torvalds /* Free flowlabels */
5061da177e4SLinus Torvalds fl6_free_socklist(sk);
5071da177e4SLinus Torvalds
5081da177e4SLinus Torvalds /* Free tx options */
5091da177e4SLinus Torvalds
51045f6fad8SEric Dumazet opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL);
51145f6fad8SEric Dumazet if (opt) {
51245f6fad8SEric Dumazet atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
51345f6fad8SEric Dumazet txopt_put(opt);
51445f6fad8SEric Dumazet }
5151da177e4SLinus Torvalds }
51621985f43SKuniyuki Iwashima EXPORT_SYMBOL_GPL(inet6_cleanup_sock);
51721985f43SKuniyuki Iwashima
5181da177e4SLinus Torvalds /*
5191da177e4SLinus Torvalds * This does both peername and sockname.
5201da177e4SLinus Torvalds */
inet6_getname(struct socket * sock,struct sockaddr * uaddr,int peer)5211da177e4SLinus Torvalds int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
5229b2c45d4SDenys Vlasenko int peer)
5231da177e4SLinus Torvalds {
5241da177e4SLinus Torvalds struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
5256d71331eSDaan De Meyer int sin_addr_len = sizeof(*sin);
5261da177e4SLinus Torvalds struct sock *sk = sock->sk;
5271da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk);
5281da177e4SLinus Torvalds struct ipv6_pinfo *np = inet6_sk(sk);
5291da177e4SLinus Torvalds
5301da177e4SLinus Torvalds sin->sin6_family = AF_INET6;
5311da177e4SLinus Torvalds sin->sin6_flowinfo = 0;
5321da177e4SLinus Torvalds sin->sin6_scope_id = 0;
5339dfc685eSEric Dumazet lock_sock(sk);
5341da177e4SLinus Torvalds if (peer) {
5359dfc685eSEric Dumazet if (!inet->inet_dport ||
5369dfc685eSEric Dumazet (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
5379dfc685eSEric Dumazet peer == 1)) {
5389dfc685eSEric Dumazet release_sock(sk);
5391da177e4SLinus Torvalds return -ENOTCONN;
5409dfc685eSEric Dumazet }
541c720c7e8SEric Dumazet sin->sin6_port = inet->inet_dport;
542efe4208fSEric Dumazet sin->sin6_addr = sk->sk_v6_daddr;
5431da177e4SLinus Torvalds if (np->sndflow)
5441da177e4SLinus Torvalds sin->sin6_flowinfo = np->flow_label;
5456d71331eSDaan De Meyer BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, &sin_addr_len,
5469dfc685eSEric Dumazet CGROUP_INET6_GETPEERNAME);
5471da177e4SLinus Torvalds } else {
548efe4208fSEric Dumazet if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
5494e3fd7a0SAlexey Dobriyan sin->sin6_addr = np->saddr;
5501da177e4SLinus Torvalds else
551efe4208fSEric Dumazet sin->sin6_addr = sk->sk_v6_rcv_saddr;
552c720c7e8SEric Dumazet sin->sin6_port = inet->inet_sport;
5536d71331eSDaan De Meyer BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, &sin_addr_len,
5549dfc685eSEric Dumazet CGROUP_INET6_GETSOCKNAME);
555a9ed15daSStanislav Fomichev }
556842df073SHannes Frederic Sowa sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
557842df073SHannes Frederic Sowa sk->sk_bound_dev_if);
5589dfc685eSEric Dumazet release_sock(sk);
5596d71331eSDaan De Meyer return sin_addr_len;
5601da177e4SLinus Torvalds }
5617159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_getname);
5627159039aSYOSHIFUJI Hideaki
inet6_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)5631da177e4SLinus Torvalds int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
5641da177e4SLinus Torvalds {
5657c1552daSChristoph Hellwig void __user *argp = (void __user *)arg;
5661da177e4SLinus Torvalds struct sock *sk = sock->sk;
5673b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(sk);
568086d4905SEric Dumazet const struct proto *prot;
5691da177e4SLinus Torvalds
570647c0c70SEldad Zack switch (cmd) {
5711da177e4SLinus Torvalds case SIOCADDRT:
5727c1552daSChristoph Hellwig case SIOCDELRT: {
5737c1552daSChristoph Hellwig struct in6_rtmsg rtmsg;
5741da177e4SLinus Torvalds
5757c1552daSChristoph Hellwig if (copy_from_user(&rtmsg, argp, sizeof(rtmsg)))
5767c1552daSChristoph Hellwig return -EFAULT;
5777c1552daSChristoph Hellwig return ipv6_route_ioctl(net, cmd, &rtmsg);
5787c1552daSChristoph Hellwig }
5791da177e4SLinus Torvalds case SIOCSIFADDR:
5807c1552daSChristoph Hellwig return addrconf_add_ifaddr(net, argp);
5811da177e4SLinus Torvalds case SIOCDIFADDR:
5827c1552daSChristoph Hellwig return addrconf_del_ifaddr(net, argp);
5831da177e4SLinus Torvalds case SIOCSIFDSTADDR:
5847c1552daSChristoph Hellwig return addrconf_set_dstaddr(net, argp);
5851da177e4SLinus Torvalds default:
586086d4905SEric Dumazet /* IPV6_ADDRFORM can change sk->sk_prot under us. */
587086d4905SEric Dumazet prot = READ_ONCE(sk->sk_prot);
588086d4905SEric Dumazet if (!prot->ioctl)
589b5e5fa5eSChristoph Hellwig return -ENOIOCTLCMD;
590e1d001faSBreno Leitao return sk_ioctl(sk, cmd, (void __user *)arg);
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds /*NOTREACHED*/
593a02cec21SEric Dumazet return 0;
5941da177e4SLinus Torvalds }
5957159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_ioctl);
5967159039aSYOSHIFUJI Hideaki
5973986912fSChristoph Hellwig #ifdef CONFIG_COMPAT
5983986912fSChristoph Hellwig struct compat_in6_rtmsg {
5993986912fSChristoph Hellwig struct in6_addr rtmsg_dst;
6003986912fSChristoph Hellwig struct in6_addr rtmsg_src;
6013986912fSChristoph Hellwig struct in6_addr rtmsg_gateway;
6023986912fSChristoph Hellwig u32 rtmsg_type;
6033986912fSChristoph Hellwig u16 rtmsg_dst_len;
6043986912fSChristoph Hellwig u16 rtmsg_src_len;
6053986912fSChristoph Hellwig u32 rtmsg_metric;
6063986912fSChristoph Hellwig u32 rtmsg_info;
6073986912fSChristoph Hellwig u32 rtmsg_flags;
6083986912fSChristoph Hellwig s32 rtmsg_ifindex;
6093986912fSChristoph Hellwig };
6103986912fSChristoph Hellwig
inet6_compat_routing_ioctl(struct sock * sk,unsigned int cmd,struct compat_in6_rtmsg __user * ur)6113986912fSChristoph Hellwig static int inet6_compat_routing_ioctl(struct sock *sk, unsigned int cmd,
6123986912fSChristoph Hellwig struct compat_in6_rtmsg __user *ur)
6133986912fSChristoph Hellwig {
6143986912fSChristoph Hellwig struct in6_rtmsg rt;
6153986912fSChristoph Hellwig
6163986912fSChristoph Hellwig if (copy_from_user(&rt.rtmsg_dst, &ur->rtmsg_dst,
6173986912fSChristoph Hellwig 3 * sizeof(struct in6_addr)) ||
6183986912fSChristoph Hellwig get_user(rt.rtmsg_type, &ur->rtmsg_type) ||
6193986912fSChristoph Hellwig get_user(rt.rtmsg_dst_len, &ur->rtmsg_dst_len) ||
6203986912fSChristoph Hellwig get_user(rt.rtmsg_src_len, &ur->rtmsg_src_len) ||
6213986912fSChristoph Hellwig get_user(rt.rtmsg_metric, &ur->rtmsg_metric) ||
6223986912fSChristoph Hellwig get_user(rt.rtmsg_info, &ur->rtmsg_info) ||
6233986912fSChristoph Hellwig get_user(rt.rtmsg_flags, &ur->rtmsg_flags) ||
6243986912fSChristoph Hellwig get_user(rt.rtmsg_ifindex, &ur->rtmsg_ifindex))
6253986912fSChristoph Hellwig return -EFAULT;
6263986912fSChristoph Hellwig
6273986912fSChristoph Hellwig
6283986912fSChristoph Hellwig return ipv6_route_ioctl(sock_net(sk), cmd, &rt);
6293986912fSChristoph Hellwig }
6303986912fSChristoph Hellwig
inet6_compat_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)6313986912fSChristoph Hellwig int inet6_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
6323986912fSChristoph Hellwig {
6333986912fSChristoph Hellwig void __user *argp = compat_ptr(arg);
6343986912fSChristoph Hellwig struct sock *sk = sock->sk;
6353986912fSChristoph Hellwig
6363986912fSChristoph Hellwig switch (cmd) {
6373986912fSChristoph Hellwig case SIOCADDRT:
6383986912fSChristoph Hellwig case SIOCDELRT:
6393986912fSChristoph Hellwig return inet6_compat_routing_ioctl(sk, cmd, argp);
6403986912fSChristoph Hellwig default:
6413986912fSChristoph Hellwig return -ENOIOCTLCMD;
6423986912fSChristoph Hellwig }
6433986912fSChristoph Hellwig }
6443986912fSChristoph Hellwig EXPORT_SYMBOL_GPL(inet6_compat_ioctl);
6453986912fSChristoph Hellwig #endif /* CONFIG_COMPAT */
6463986912fSChristoph Hellwig
647164c51feSPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udpv6_sendmsg(struct sock *, struct msghdr *,
648164c51feSPaolo Abeni size_t));
inet6_sendmsg(struct socket * sock,struct msghdr * msg,size_t size)64968ab5d14SPaolo Abeni int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
65068ab5d14SPaolo Abeni {
65168ab5d14SPaolo Abeni struct sock *sk = sock->sk;
652086d4905SEric Dumazet const struct proto *prot;
65368ab5d14SPaolo Abeni
65468ab5d14SPaolo Abeni if (unlikely(inet_send_prepare(sk)))
65568ab5d14SPaolo Abeni return -EAGAIN;
65668ab5d14SPaolo Abeni
657086d4905SEric Dumazet /* IPV6_ADDRFORM can change sk->sk_prot under us. */
658086d4905SEric Dumazet prot = READ_ONCE(sk->sk_prot);
659086d4905SEric Dumazet return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg,
660164c51feSPaolo Abeni sk, msg, size);
66168ab5d14SPaolo Abeni }
66268ab5d14SPaolo Abeni
663164c51feSPaolo Abeni INDIRECT_CALLABLE_DECLARE(int udpv6_recvmsg(struct sock *, struct msghdr *,
664ec095263SOliver Hartkopp size_t, int, int *));
inet6_recvmsg(struct socket * sock,struct msghdr * msg,size_t size,int flags)66568ab5d14SPaolo Abeni int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
66668ab5d14SPaolo Abeni int flags)
66768ab5d14SPaolo Abeni {
66868ab5d14SPaolo Abeni struct sock *sk = sock->sk;
669086d4905SEric Dumazet const struct proto *prot;
67068ab5d14SPaolo Abeni int addr_len = 0;
67168ab5d14SPaolo Abeni int err;
67268ab5d14SPaolo Abeni
67368ab5d14SPaolo Abeni if (likely(!(flags & MSG_ERRQUEUE)))
67468ab5d14SPaolo Abeni sock_rps_record_flow(sk);
67568ab5d14SPaolo Abeni
676086d4905SEric Dumazet /* IPV6_ADDRFORM can change sk->sk_prot under us. */
677086d4905SEric Dumazet prot = READ_ONCE(sk->sk_prot);
678086d4905SEric Dumazet err = INDIRECT_CALL_2(prot->recvmsg, tcp_recvmsg, udpv6_recvmsg,
679ec095263SOliver Hartkopp sk, msg, size, flags, &addr_len);
68068ab5d14SPaolo Abeni if (err >= 0)
68168ab5d14SPaolo Abeni msg->msg_namelen = addr_len;
68268ab5d14SPaolo Abeni return err;
68368ab5d14SPaolo Abeni }
68468ab5d14SPaolo Abeni
68590ddc4f0SEric Dumazet const struct proto_ops inet6_stream_ops = {
6861da177e4SLinus Torvalds .family = PF_INET6,
6871da177e4SLinus Torvalds .owner = THIS_MODULE,
6881da177e4SLinus Torvalds .release = inet6_release,
6891da177e4SLinus Torvalds .bind = inet6_bind,
6901da177e4SLinus Torvalds .connect = inet_stream_connect, /* ok */
6911da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */
6921da177e4SLinus Torvalds .accept = inet_accept, /* ok */
6931da177e4SLinus Torvalds .getname = inet6_getname,
694a11e1d43SLinus Torvalds .poll = tcp_poll, /* ok */
6951da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */
696c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp,
6971da177e4SLinus Torvalds .listen = inet_listen, /* ok */
6981da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */
6991da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */
7001da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */
70168ab5d14SPaolo Abeni .sendmsg = inet6_sendmsg, /* retpoline's sake */
70268ab5d14SPaolo Abeni .recvmsg = inet6_recvmsg, /* retpoline's sake */
70305255b82SEric Dumazet #ifdef CONFIG_MMU
70493ab6cc6SEric Dumazet .mmap = tcp_mmap,
70505255b82SEric Dumazet #endif
7061d7e4538SDavid Howells .splice_eof = inet_splice_eof,
70745f91bdcSJohn Fastabend .sendmsg_locked = tcp_sendmsg_locked,
708a0974dd3SJens Axboe .splice_read = tcp_splice_read,
70932035585STom Herbert .read_sock = tcp_read_sock,
710965b57b4SCong Wang .read_skb = tcp_read_skb,
71132035585STom Herbert .peek_len = tcp_peek_len,
7123fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
7133986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl,
7143fdadf7dSDmitry Mishin #endif
715d1361840SEric Dumazet .set_rcvlowat = tcp_set_rcvlowat,
7161da177e4SLinus Torvalds };
7171da177e4SLinus Torvalds
71890ddc4f0SEric Dumazet const struct proto_ops inet6_dgram_ops = {
7191da177e4SLinus Torvalds .family = PF_INET6,
7201da177e4SLinus Torvalds .owner = THIS_MODULE,
7211da177e4SLinus Torvalds .release = inet6_release,
7221da177e4SLinus Torvalds .bind = inet6_bind,
7231da177e4SLinus Torvalds .connect = inet_dgram_connect, /* ok */
7241da177e4SLinus Torvalds .socketpair = sock_no_socketpair, /* a do nothing */
7251da177e4SLinus Torvalds .accept = sock_no_accept, /* a do nothing */
7261da177e4SLinus Torvalds .getname = inet6_getname,
727a11e1d43SLinus Torvalds .poll = udp_poll, /* ok */
7281da177e4SLinus Torvalds .ioctl = inet6_ioctl, /* must change */
729c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp,
7301da177e4SLinus Torvalds .listen = sock_no_listen, /* ok */
7311da177e4SLinus Torvalds .shutdown = inet_shutdown, /* ok */
7321da177e4SLinus Torvalds .setsockopt = sock_common_setsockopt, /* ok */
7331da177e4SLinus Torvalds .getsockopt = sock_common_getsockopt, /* ok */
73468ab5d14SPaolo Abeni .sendmsg = inet6_sendmsg, /* retpoline's sake */
73568ab5d14SPaolo Abeni .recvmsg = inet6_recvmsg, /* retpoline's sake */
736965b57b4SCong Wang .read_skb = udp_read_skb,
7371da177e4SLinus Torvalds .mmap = sock_no_mmap,
738627d2d6bSsamanthakumar .set_peek_off = sk_set_peek_off,
739543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
7403986912fSChristoph Hellwig .compat_ioctl = inet6_compat_ioctl,
741543d9cfeSArnaldo Carvalho de Melo #endif
7421da177e4SLinus Torvalds };
7431da177e4SLinus Torvalds
744ec1b4cf7SStephen Hemminger static const struct net_proto_family inet6_family_ops = {
7451da177e4SLinus Torvalds .family = PF_INET6,
7461da177e4SLinus Torvalds .create = inet6_create,
7471da177e4SLinus Torvalds .owner = THIS_MODULE,
7481da177e4SLinus Torvalds };
7491da177e4SLinus Torvalds
inet6_register_protosw(struct inet_protosw * p)75087c3efbfSDaniel Lezcano int inet6_register_protosw(struct inet_protosw *p)
7511da177e4SLinus Torvalds {
7521da177e4SLinus Torvalds struct list_head *lh;
7531da177e4SLinus Torvalds struct inet_protosw *answer;
7541da177e4SLinus Torvalds struct list_head *last_perm;
75587c3efbfSDaniel Lezcano int protocol = p->protocol;
75687c3efbfSDaniel Lezcano int ret;
7571da177e4SLinus Torvalds
7581da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock);
7591da177e4SLinus Torvalds
76087c3efbfSDaniel Lezcano ret = -EINVAL;
7611da177e4SLinus Torvalds if (p->type >= SOCK_MAX)
7621da177e4SLinus Torvalds goto out_illegal;
7631da177e4SLinus Torvalds
7641da177e4SLinus Torvalds /* If we are trying to override a permanent protocol, bail. */
7651da177e4SLinus Torvalds answer = NULL;
76687c3efbfSDaniel Lezcano ret = -EPERM;
7671da177e4SLinus Torvalds last_perm = &inetsw6[p->type];
7681da177e4SLinus Torvalds list_for_each(lh, &inetsw6[p->type]) {
7691da177e4SLinus Torvalds answer = list_entry(lh, struct inet_protosw, list);
7701da177e4SLinus Torvalds
7711da177e4SLinus Torvalds /* Check only the non-wild match. */
7721da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & answer->flags) {
7731da177e4SLinus Torvalds if (protocol == answer->protocol)
7741da177e4SLinus Torvalds break;
7751da177e4SLinus Torvalds last_perm = lh;
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds
7781da177e4SLinus Torvalds answer = NULL;
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds if (answer)
7811da177e4SLinus Torvalds goto out_permanent;
7821da177e4SLinus Torvalds
7831da177e4SLinus Torvalds /* Add the new entry after the last permanent entry if any, so that
7841da177e4SLinus Torvalds * the new entry does not override a permanent entry when matched with
7851da177e4SLinus Torvalds * a wild-card protocol. But it is allowed to override any existing
7861da177e4SLinus Torvalds * non-permanent entry. This means that when we remove this entry, the
7871da177e4SLinus Torvalds * system automatically returns to the old behavior.
7881da177e4SLinus Torvalds */
7891da177e4SLinus Torvalds list_add_rcu(&p->list, last_perm);
79087c3efbfSDaniel Lezcano ret = 0;
7911da177e4SLinus Torvalds out:
7921da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock);
79387c3efbfSDaniel Lezcano return ret;
7941da177e4SLinus Torvalds
7951da177e4SLinus Torvalds out_permanent:
796f3213831SJoe Perches pr_err("Attempt to override permanent protocol %d\n", protocol);
7971da177e4SLinus Torvalds goto out;
7981da177e4SLinus Torvalds
7991da177e4SLinus Torvalds out_illegal:
800f3213831SJoe Perches pr_err("Ignoring attempt to register invalid socket type %d\n",
8011da177e4SLinus Torvalds p->type);
8021da177e4SLinus Torvalds goto out;
8031da177e4SLinus Torvalds }
8047159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_register_protosw);
8057159039aSYOSHIFUJI Hideaki
8061da177e4SLinus Torvalds void
inet6_unregister_protosw(struct inet_protosw * p)8071da177e4SLinus Torvalds inet6_unregister_protosw(struct inet_protosw *p)
8081da177e4SLinus Torvalds {
8091da177e4SLinus Torvalds if (INET_PROTOSW_PERMANENT & p->flags) {
810f3213831SJoe Perches pr_err("Attempt to unregister permanent protocol %d\n",
8111da177e4SLinus Torvalds p->protocol);
8121da177e4SLinus Torvalds } else {
8131da177e4SLinus Torvalds spin_lock_bh(&inetsw6_lock);
8141da177e4SLinus Torvalds list_del_rcu(&p->list);
8151da177e4SLinus Torvalds spin_unlock_bh(&inetsw6_lock);
8161da177e4SLinus Torvalds
8171da177e4SLinus Torvalds synchronize_net();
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds }
8207159039aSYOSHIFUJI Hideaki EXPORT_SYMBOL(inet6_unregister_protosw);
8217159039aSYOSHIFUJI Hideaki
inet6_sk_rebuild_header(struct sock * sk)822b9750ce1SArnaldo Carvalho de Melo int inet6_sk_rebuild_header(struct sock *sk)
823b9750ce1SArnaldo Carvalho de Melo {
824b9750ce1SArnaldo Carvalho de Melo struct ipv6_pinfo *np = inet6_sk(sk);
82568d0c6d3SDavid S. Miller struct dst_entry *dst;
826b9750ce1SArnaldo Carvalho de Melo
827b9750ce1SArnaldo Carvalho de Melo dst = __sk_dst_check(sk, np->dst_cookie);
828b9750ce1SArnaldo Carvalho de Melo
82963159f29SIan Morris if (!dst) {
830b9750ce1SArnaldo Carvalho de Melo struct inet_sock *inet = inet_sk(sk);
83120c59de2SArnaud Ebalard struct in6_addr *final_p, final;
8324c9483b2SDavid S. Miller struct flowi6 fl6;
833b9750ce1SArnaldo Carvalho de Melo
8344c9483b2SDavid S. Miller memset(&fl6, 0, sizeof(fl6));
8354c9483b2SDavid S. Miller fl6.flowi6_proto = sk->sk_protocol;
836efe4208fSEric Dumazet fl6.daddr = sk->sk_v6_daddr;
8374e3fd7a0SAlexey Dobriyan fl6.saddr = np->saddr;
8384c9483b2SDavid S. Miller fl6.flowlabel = np->flow_label;
8394c9483b2SDavid S. Miller fl6.flowi6_oif = sk->sk_bound_dev_if;
8404c9483b2SDavid S. Miller fl6.flowi6_mark = sk->sk_mark;
8411958b856SDavid S. Miller fl6.fl6_dport = inet->inet_dport;
8421958b856SDavid S. Miller fl6.fl6_sport = inet->inet_sport;
843e2d118a1SLorenzo Colitti fl6.flowi6_uid = sk->sk_uid;
8443df98d79SPaul Moore security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
845b9750ce1SArnaldo Carvalho de Melo
84645f6fad8SEric Dumazet rcu_read_lock();
84745f6fad8SEric Dumazet final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt),
84845f6fad8SEric Dumazet &final);
84945f6fad8SEric Dumazet rcu_read_unlock();
850b9750ce1SArnaldo Carvalho de Melo
851c4e85f73SSabrina Dubroca dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
85268d0c6d3SDavid S. Miller if (IS_ERR(dst)) {
853b9750ce1SArnaldo Carvalho de Melo sk->sk_route_caps = 0;
8542f2d9972SEric Dumazet WRITE_ONCE(sk->sk_err_soft, -PTR_ERR(dst));
85568d0c6d3SDavid S. Miller return PTR_ERR(dst);
856b9750ce1SArnaldo Carvalho de Melo }
857b9750ce1SArnaldo Carvalho de Melo
8586bd4f355SEric Dumazet ip6_dst_store(sk, dst, NULL, NULL);
859b9750ce1SArnaldo Carvalho de Melo }
860b9750ce1SArnaldo Carvalho de Melo
861b9750ce1SArnaldo Carvalho de Melo return 0;
862b9750ce1SArnaldo Carvalho de Melo }
863b9750ce1SArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
864b9750ce1SArnaldo Carvalho de Melo
ipv6_opt_accepted(const struct sock * sk,const struct sk_buff * skb,const struct inet6_skb_parm * opt)865a224772dSEric Dumazet bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
866a224772dSEric Dumazet const struct inet6_skb_parm *opt)
867399c07deSArnaldo Carvalho de Melo {
86892113bfdSEric Dumazet const struct ipv6_pinfo *np = inet6_sk(sk);
869399c07deSArnaldo Carvalho de Melo
870399c07deSArnaldo Carvalho de Melo if (np->rxopt.all) {
8718b58a398SFlorian Westphal if (((opt->flags & IP6SKB_HOPBYHOP) &&
8728b58a398SFlorian Westphal (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) ||
873ac1eabcaSFlorent Fourcot (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) &&
874399c07deSArnaldo Carvalho de Melo np->rxopt.bits.rxflow) ||
875399c07deSArnaldo Carvalho de Melo (opt->srcrt && (np->rxopt.bits.srcrt ||
876399c07deSArnaldo Carvalho de Melo np->rxopt.bits.osrcrt)) ||
877399c07deSArnaldo Carvalho de Melo ((opt->dst1 || opt->dst0) &&
878399c07deSArnaldo Carvalho de Melo (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
87992113bfdSEric Dumazet return true;
880399c07deSArnaldo Carvalho de Melo }
88192113bfdSEric Dumazet return false;
882399c07deSArnaldo Carvalho de Melo }
883399c07deSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
884399c07deSArnaldo Carvalho de Melo
8857546dd97SStephen Hemminger static struct packet_type ipv6_packet_type __read_mostly = {
88609640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_IPV6),
887662397fdSYOSHIFUJI Hideaki .func = ipv6_rcv,
888d8269e2cSEdward Cree .list_func = ipv6_list_rcv,
88922061d80SVlad Yasevich };
89022061d80SVlad Yasevich
ipv6_packet_init(void)891662397fdSYOSHIFUJI Hideaki static int __init ipv6_packet_init(void)
892662397fdSYOSHIFUJI Hideaki {
893662397fdSYOSHIFUJI Hideaki dev_add_pack(&ipv6_packet_type);
894662397fdSYOSHIFUJI Hideaki return 0;
895662397fdSYOSHIFUJI Hideaki }
896662397fdSYOSHIFUJI Hideaki
ipv6_packet_cleanup(void)897662397fdSYOSHIFUJI Hideaki static void ipv6_packet_cleanup(void)
898662397fdSYOSHIFUJI Hideaki {
899662397fdSYOSHIFUJI Hideaki dev_remove_pack(&ipv6_packet_type);
900662397fdSYOSHIFUJI Hideaki }
901662397fdSYOSHIFUJI Hideaki
ipv6_init_mibs(struct net * net)902e43291cbSDenis V. Lunev static int __net_init ipv6_init_mibs(struct net *net)
903e43291cbSDenis V. Lunev {
904827da44cSJohn Stultz int i;
905827da44cSJohn Stultz
906698365faSWANG Cong net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib);
907698365faSWANG Cong if (!net->mib.udp_stats_in6)
9080c7ed677SDenis V. Lunev return -ENOMEM;
909698365faSWANG Cong net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib);
910698365faSWANG Cong if (!net->mib.udplite_stats_in6)
911be713a44SDenis V. Lunev goto err_udplite_mib;
912698365faSWANG Cong net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib);
913698365faSWANG Cong if (!net->mib.ipv6_statistics)
9149261e537SDenis V. Lunev goto err_ip_mib;
915827da44cSJohn Stultz
916827da44cSJohn Stultz for_each_possible_cpu(i) {
917827da44cSJohn Stultz struct ipstats_mib *af_inet6_stats;
918698365faSWANG Cong af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i);
919827da44cSJohn Stultz u64_stats_init(&af_inet6_stats->syncp);
920827da44cSJohn Stultz }
921827da44cSJohn Stultz
922827da44cSJohn Stultz
923698365faSWANG Cong net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib);
924698365faSWANG Cong if (!net->mib.icmpv6_statistics)
9259261e537SDenis V. Lunev goto err_icmp_mib;
9262a24444fSEric Dumazet net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
9272a24444fSEric Dumazet GFP_KERNEL);
9282a24444fSEric Dumazet if (!net->mib.icmpv6msg_statistics)
9299261e537SDenis V. Lunev goto err_icmpmsg_mib;
930e43291cbSDenis V. Lunev return 0;
931be713a44SDenis V. Lunev
9329261e537SDenis V. Lunev err_icmpmsg_mib:
933698365faSWANG Cong free_percpu(net->mib.icmpv6_statistics);
9349261e537SDenis V. Lunev err_icmp_mib:
935698365faSWANG Cong free_percpu(net->mib.ipv6_statistics);
9369261e537SDenis V. Lunev err_ip_mib:
937698365faSWANG Cong free_percpu(net->mib.udplite_stats_in6);
938be713a44SDenis V. Lunev err_udplite_mib:
939698365faSWANG Cong free_percpu(net->mib.udp_stats_in6);
940be713a44SDenis V. Lunev return -ENOMEM;
941e43291cbSDenis V. Lunev }
942e43291cbSDenis V. Lunev
ipv6_cleanup_mibs(struct net * net)9432c8c1e72SAlexey Dobriyan static void ipv6_cleanup_mibs(struct net *net)
944e43291cbSDenis V. Lunev {
945698365faSWANG Cong free_percpu(net->mib.udp_stats_in6);
946698365faSWANG Cong free_percpu(net->mib.udplite_stats_in6);
947698365faSWANG Cong free_percpu(net->mib.ipv6_statistics);
948698365faSWANG Cong free_percpu(net->mib.icmpv6_statistics);
9492a24444fSEric Dumazet kfree(net->mib.icmpv6msg_statistics);
950e43291cbSDenis V. Lunev }
951e43291cbSDenis V. Lunev
inet6_net_init(struct net * net)952e7dc8494SAlexey Dobriyan static int __net_init inet6_net_init(struct net *net)
95381c1c178SDaniel Lezcano {
9540c96d8c5SDaniel Lezcano int err = 0;
9550c96d8c5SDaniel Lezcano
95699bc9c4eSDaniel Lezcano net->ipv6.sysctl.bindv6only = 0;
95741a76906SDaniel Lezcano net->ipv6.sysctl.icmpv6_time = 1*HZ;
958e6f86b0fSVirgile Jarry net->ipv6.sysctl.icmpv6_echo_ignore_all = 0;
95903f1ecccSStephen Suryaputra net->ipv6.sysctl.icmpv6_echo_ignore_multicast = 0;
9600b03a5caSStephen Suryaputra net->ipv6.sysctl.icmpv6_echo_ignore_anycast = 0;
9617ab75456SMahesh Bandewar net->ipv6.sysctl.icmpv6_error_anycast_as_unicast = 0;
9620bc19985SStephen Suryaputra
9630bc19985SStephen Suryaputra /* By default, rate limit error messages.
9640bc19985SStephen Suryaputra * Except for pmtu discovery, it would break it.
9650bc19985SStephen Suryaputra * proc_do_large_bitmap needs pointer to the bitmap.
9660bc19985SStephen Suryaputra */
9670bc19985SStephen Suryaputra bitmap_set(net->ipv6.sysctl.icmpv6_ratemask, 0, ICMPV6_ERRMSG_MAX + 1);
9680bc19985SStephen Suryaputra bitmap_clear(net->ipv6.sysctl.icmpv6_ratemask, ICMPV6_PKT_TOOBIG, 1);
9690bc19985SStephen Suryaputra net->ipv6.sysctl.icmpv6_ratemask_ptr = net->ipv6.sysctl.icmpv6_ratemask;
9700bc19985SStephen Suryaputra
9716444f72bSFlorent Fourcot net->ipv6.sysctl.flowlabel_consistency = 1;
97242240901STom Herbert net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS;
9731855b7c3SHannes Frederic Sowa net->ipv6.sysctl.idgen_retries = 3;
9741855b7c3SHannes Frederic Sowa net->ipv6.sysctl.idgen_delay = 1 * HZ;
975be26849bSTom Herbert net->ipv6.sysctl.flowlabel_state_ranges = 0;
97647d3d7acSTom Herbert net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT;
97747d3d7acSTom Herbert net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT;
97847d3d7acSTom Herbert net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN;
97947d3d7acSTom Herbert net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN;
980907eea48SAmit Cohen net->ipv6.sysctl.fib_notify_on_flag_change = 0;
981812918c4SHannes Frederic Sowa atomic_set(&net->ipv6.fib6_sernum, 1);
982e71e0349SDaniel Lezcano
9839ee11f0fSJustin Iurman net->ipv6.sysctl.ioam6_id = IOAM6_DEFAULT_ID;
9849ee11f0fSJustin Iurman net->ipv6.sysctl.ioam6_id_wide = IOAM6_DEFAULT_ID_WIDE;
9859ee11f0fSJustin Iurman
986e43291cbSDenis V. Lunev err = ipv6_init_mibs(net);
987e43291cbSDenis V. Lunev if (err)
988e43291cbSDenis V. Lunev return err;
9890c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
9900c96d8c5SDaniel Lezcano err = udp6_proc_init(net);
9910c96d8c5SDaniel Lezcano if (err)
9920c96d8c5SDaniel Lezcano goto out;
9936f8b13bcSDaniel Lezcano err = tcp6_proc_init(net);
9946f8b13bcSDaniel Lezcano if (err)
9956f8b13bcSDaniel Lezcano goto proc_tcp6_fail;
9966ab57e7eSDaniel Lezcano err = ac6_proc_init(net);
9976ab57e7eSDaniel Lezcano if (err)
9986ab57e7eSDaniel Lezcano goto proc_ac6_fail;
9990c96d8c5SDaniel Lezcano #endif
10000c96d8c5SDaniel Lezcano return err;
10016f8b13bcSDaniel Lezcano
10026f8b13bcSDaniel Lezcano #ifdef CONFIG_PROC_FS
10036ab57e7eSDaniel Lezcano proc_ac6_fail:
10046ab57e7eSDaniel Lezcano tcp6_proc_exit(net);
10056f8b13bcSDaniel Lezcano proc_tcp6_fail:
10066f8b13bcSDaniel Lezcano udp6_proc_exit(net);
1007e43291cbSDenis V. Lunev out:
1008e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net);
1009e43291cbSDenis V. Lunev return err;
10106f8b13bcSDaniel Lezcano #endif
101181c1c178SDaniel Lezcano }
101281c1c178SDaniel Lezcano
inet6_net_exit(struct net * net)10132c8c1e72SAlexey Dobriyan static void __net_exit inet6_net_exit(struct net *net)
101481c1c178SDaniel Lezcano {
10150c96d8c5SDaniel Lezcano #ifdef CONFIG_PROC_FS
10160c96d8c5SDaniel Lezcano udp6_proc_exit(net);
10176f8b13bcSDaniel Lezcano tcp6_proc_exit(net);
10186ab57e7eSDaniel Lezcano ac6_proc_exit(net);
10190c96d8c5SDaniel Lezcano #endif
1020e43291cbSDenis V. Lunev ipv6_cleanup_mibs(net);
102181c1c178SDaniel Lezcano }
102281c1c178SDaniel Lezcano
102381c1c178SDaniel Lezcano static struct pernet_operations inet6_net_ops = {
102481c1c178SDaniel Lezcano .init = inet6_net_init,
102581c1c178SDaniel Lezcano .exit = inet6_net_exit,
102681c1c178SDaniel Lezcano };
102781c1c178SDaniel Lezcano
ipv6_route_input(struct sk_buff * skb)10289b0a6a9dSPeter Oskolkov static int ipv6_route_input(struct sk_buff *skb)
10299b0a6a9dSPeter Oskolkov {
10309b0a6a9dSPeter Oskolkov ip6_route_input(skb);
10319b0a6a9dSPeter Oskolkov return skb_dst(skb)->error;
10329b0a6a9dSPeter Oskolkov }
10339b0a6a9dSPeter Oskolkov
10345f81bd2eSCong Wang static const struct ipv6_stub ipv6_stub_impl = {
10355f81bd2eSCong Wang .ipv6_sock_mc_join = ipv6_sock_mc_join,
10365f81bd2eSCong Wang .ipv6_sock_mc_drop = ipv6_sock_mc_drop,
10376c8991f4SSabrina Dubroca .ipv6_dst_lookup_flow = ip6_dst_lookup_flow,
10389b0a6a9dSPeter Oskolkov .ipv6_route_input = ipv6_route_input,
103965a2022eSDavid Ahern .fib6_get_table = fib6_get_table,
104065a2022eSDavid Ahern .fib6_table_lookup = fib6_table_lookup,
104165a2022eSDavid Ahern .fib6_lookup = fib6_lookup,
1042b1d40991SDavid Ahern .fib6_select_path = fib6_select_path,
1043901731b8SDavid Ahern .ip6_mtu_from_fib6 = ip6_mtu_from_fib6,
10441aefd3deSDavid Ahern .fib6_nh_init = fib6_nh_init,
10451aefd3deSDavid Ahern .fib6_nh_release = fib6_nh_release,
10468837cbbfSNikolay Aleksandrov .fib6_nh_release_dsts = fib6_nh_release_dsts,
1047cdaa16a4SDavid Ahern .fib6_update_sernum = fib6_update_sernum_stub,
104819a3b7eeSDavid Ahern .fib6_rt_update = fib6_rt_update,
104968a9b13dSDavid Ahern .ip6_del_rt = ip6_del_rt,
10505f81bd2eSCong Wang .udpv6_encap_enable = udpv6_encap_enable,
1051f564f45cSCong Wang .ndisc_send_na = ndisc_send_na,
10520146dca7SSabrina Dubroca #if IS_ENABLED(CONFIG_XFRM)
10533e50ddd8SFlorian Westphal .xfrm6_local_rxpmtu = xfrm6_local_rxpmtu,
10540146dca7SSabrina Dubroca .xfrm6_udp_encap_rcv = xfrm6_udp_encap_rcv,
105526333c37SSabrina Dubroca .xfrm6_rcv_encap = xfrm6_rcv_encap,
10560146dca7SSabrina Dubroca #endif
1057e15a00aaSCong Wang .nd_tbl = &nd_tbl,
10581d97898bSwenxu .ipv6_fragment = ip6_fragment,
1059504a4011SAndreas Roeseler .ipv6_dev_find = ipv6_dev_find,
10605f81bd2eSCong Wang };
10615f81bd2eSCong Wang
1062d74bad4eSAndrey Ignatov static const struct ipv6_bpf_stub ipv6_bpf_stub_impl = {
1063d74bad4eSAndrey Ignatov .inet6_bind = __inet6_bind,
10648a615c6bSJoe Stringer .udp6_lib_lookup = __udp6_lib_lookup,
106575b64b68SMartin KaFai Lau .ipv6_setsockopt = do_ipv6_setsockopt,
106638566ec0SMartin KaFai Lau .ipv6_getsockopt = do_ipv6_getsockopt,
1067*b96f500dSMartynas Pumputis .ipv6_dev_get_saddr = ipv6_dev_get_saddr,
1068d74bad4eSAndrey Ignatov };
1069d74bad4eSAndrey Ignatov
inet6_init(void)10701da177e4SLinus Torvalds static int __init inet6_init(void)
10711da177e4SLinus Torvalds {
10721da177e4SLinus Torvalds struct list_head *r;
1073fe7ca2e1SBrian Haley int err = 0;
10741da177e4SLinus Torvalds
1075b4772ef8SEyal Birger sock_skb_cb_check_size(sizeof(struct inet6_skb_parm));
1076ef047f5eSYOSHIFUJI Hideaki
1077fe7ca2e1SBrian Haley /* Register the socket-side information for inet6_create. */
1078fe7ca2e1SBrian Haley for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1079fe7ca2e1SBrian Haley INIT_LIST_HEAD(r);
1080fe7ca2e1SBrian Haley
108176dd0728SIdo Schimmel raw_hashinfo_init(&raw_v6_hashinfo);
108276dd0728SIdo Schimmel
108356d417b1SBrian Haley if (disable_ipv6_mod) {
1084f3213831SJoe Perches pr_info("Loaded, but administratively disabled, reboot required to enable\n");
1085fe7ca2e1SBrian Haley goto out;
1086fe7ca2e1SBrian Haley }
1087fe7ca2e1SBrian Haley
10881da177e4SLinus Torvalds err = proto_register(&tcpv6_prot, 1);
10891da177e4SLinus Torvalds if (err)
10901da177e4SLinus Torvalds goto out;
10911da177e4SLinus Torvalds
10921da177e4SLinus Torvalds err = proto_register(&udpv6_prot, 1);
10931da177e4SLinus Torvalds if (err)
10941da177e4SLinus Torvalds goto out_unregister_tcp_proto;
10951da177e4SLinus Torvalds
1096ba4e58ecSGerrit Renker err = proto_register(&udplitev6_prot, 1);
10971da177e4SLinus Torvalds if (err)
10981da177e4SLinus Torvalds goto out_unregister_udp_proto;
10991da177e4SLinus Torvalds
1100ba4e58ecSGerrit Renker err = proto_register(&rawv6_prot, 1);
1101ba4e58ecSGerrit Renker if (err)
1102ba4e58ecSGerrit Renker goto out_unregister_udplite_proto;
1103ba4e58ecSGerrit Renker
11046d0bfe22SLorenzo Colitti err = proto_register(&pingv6_prot, 1);
11056d0bfe22SLorenzo Colitti if (err)
1106a03dc36bSSabrina Dubroca goto out_unregister_raw_proto;
11071da177e4SLinus Torvalds
11081da177e4SLinus Torvalds /* We MUST register RAW sockets before we create the ICMP6,
11091da177e4SLinus Torvalds * IGMP6, or NDISC control sockets.
11101da177e4SLinus Torvalds */
11117f4e4868SDaniel Lezcano err = rawv6_init();
11127f4e4868SDaniel Lezcano if (err)
1113a03dc36bSSabrina Dubroca goto out_unregister_ping_proto;
11141da177e4SLinus Torvalds
11151da177e4SLinus Torvalds /* Register the family here so that the init calls below will
11161da177e4SLinus Torvalds * be able to create sockets. (?? is this dangerous ??)
11171da177e4SLinus Torvalds */
11188eb55910SDavid S. Miller err = sock_register(&inet6_family_ops);
11198eb55910SDavid S. Miller if (err)
11207f4e4868SDaniel Lezcano goto out_sock_register_fail;
11211da177e4SLinus Torvalds
11221da177e4SLinus Torvalds /*
11231da177e4SLinus Torvalds * ipngwg API draft makes clear that the correct semantics
11241da177e4SLinus Torvalds * for TCP and UDP is to consider one TCP and UDP instance
112525985edcSLucas De Marchi * in a host available by both INET and INET6 APIs and
11261da177e4SLinus Torvalds * able to communicate via both network protocols.
11271da177e4SLinus Torvalds */
11281da177e4SLinus Torvalds
112981c1c178SDaniel Lezcano err = register_pernet_subsys(&inet6_net_ops);
113081c1c178SDaniel Lezcano if (err)
113181c1c178SDaniel Lezcano goto register_pernet_fail;
1132623d1a1aSWang Chen err = ip6_mr_init();
1133623d1a1aSWang Chen if (err)
1134623d1a1aSWang Chen goto ipmr_fail;
113515e66807SWANG Cong err = icmpv6_init();
113615e66807SWANG Cong if (err)
113715e66807SWANG Cong goto icmp_fail;
11389b0f976fSDenis V. Lunev err = ndisc_init();
11391da177e4SLinus Torvalds if (err)
11401da177e4SLinus Torvalds goto ndisc_fail;
11419b0f976fSDenis V. Lunev err = igmp6_init();
11421da177e4SLinus Torvalds if (err)
11431da177e4SLinus Torvalds goto igmp_fail;
11445f81bd2eSCong Wang
11452cc7d573SHarald Welte err = ipv6_netfilter_init();
11462cc7d573SHarald Welte if (err)
11472cc7d573SHarald Welte goto netfilter_fail;
11481da177e4SLinus Torvalds /* Create /proc/foo6 entries. */
11491da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
11501da177e4SLinus Torvalds err = -ENOMEM;
11511da177e4SLinus Torvalds if (raw6_proc_init())
11521da177e4SLinus Torvalds goto proc_raw6_fail;
1153ba4e58ecSGerrit Renker if (udplite6_proc_init())
1154ba4e58ecSGerrit Renker goto proc_udplite6_fail;
11551da177e4SLinus Torvalds if (ipv6_misc_proc_init())
11561da177e4SLinus Torvalds goto proc_misc6_fail;
11571da177e4SLinus Torvalds if (if6_proc_init())
11581da177e4SLinus Torvalds goto proc_if6_fail;
11591da177e4SLinus Torvalds #endif
1160e2fddf5eSDaniel Lezcano err = ip6_route_init();
1161e2fddf5eSDaniel Lezcano if (err)
1162e2fddf5eSDaniel Lezcano goto ip6_route_fail;
11632c861cc6SMichal Kubeček err = ndisc_late_init();
11642c861cc6SMichal Kubeček if (err)
11652c861cc6SMichal Kubeček goto ndisc_late_fail;
11660a3e78acSDaniel Lezcano err = ip6_flowlabel_init();
11670a3e78acSDaniel Lezcano if (err)
11680a3e78acSDaniel Lezcano goto ip6_flowlabel_fail;
11692384d025SJeff Barnhill err = ipv6_anycast_init();
11702384d025SJeff Barnhill if (err)
11712384d025SJeff Barnhill goto ipv6_anycast_fail;
11721da177e4SLinus Torvalds err = addrconf_init();
11731da177e4SLinus Torvalds if (err)
11741da177e4SLinus Torvalds goto addrconf_fail;
11751da177e4SLinus Torvalds
11761da177e4SLinus Torvalds /* Init v6 extension headers. */
1177248b238dSDaniel Lezcano err = ipv6_exthdrs_init();
1178248b238dSDaniel Lezcano if (err)
1179248b238dSDaniel Lezcano goto ipv6_exthdrs_fail;
1180248b238dSDaniel Lezcano
1181853cbbaaSDaniel Lezcano err = ipv6_frag_init();
1182853cbbaaSDaniel Lezcano if (err)
1183853cbbaaSDaniel Lezcano goto ipv6_frag_fail;
11841da177e4SLinus Torvalds
11851da177e4SLinus Torvalds /* Init v6 transport protocols. */
11867f4e4868SDaniel Lezcano err = udpv6_init();
11877f4e4868SDaniel Lezcano if (err)
11887f4e4868SDaniel Lezcano goto udpv6_fail;
1189e2ed4052SHerbert Xu
11907f4e4868SDaniel Lezcano err = udplitev6_init();
11917f4e4868SDaniel Lezcano if (err)
11927f4e4868SDaniel Lezcano goto udplitev6_fail;
11937f4e4868SDaniel Lezcano
1194a6024562STom Herbert err = udpv6_offload_init();
1195a6024562STom Herbert if (err)
1196a6024562STom Herbert goto udpv6_offload_fail;
1197a6024562STom Herbert
11987f4e4868SDaniel Lezcano err = tcpv6_init();
11997f4e4868SDaniel Lezcano if (err)
12007f4e4868SDaniel Lezcano goto tcpv6_fail;
12017f4e4868SDaniel Lezcano
12027f4e4868SDaniel Lezcano err = ipv6_packet_init();
12037f4e4868SDaniel Lezcano if (err)
12047f4e4868SDaniel Lezcano goto ipv6_packet_fail;
120594911fe3SBenjamin Thery
12066d0bfe22SLorenzo Colitti err = pingv6_init();
12076d0bfe22SLorenzo Colitti if (err)
12086d0bfe22SLorenzo Colitti goto pingv6_fail;
12096d0bfe22SLorenzo Colitti
1210cb72d382SHuw Davies err = calipso_init();
1211cb72d382SHuw Davies if (err)
1212cb72d382SHuw Davies goto calipso_fail;
1213cb72d382SHuw Davies
1214915d7e5eSDavid Lebrun err = seg6_init();
1215915d7e5eSDavid Lebrun if (err)
1216915d7e5eSDavid Lebrun goto seg6_fail;
1217915d7e5eSDavid Lebrun
1218a7a29f9cSAlexander Aring err = rpl_init();
1219a7a29f9cSAlexander Aring if (err)
1220a7a29f9cSAlexander Aring goto rpl_fail;
1221a7a29f9cSAlexander Aring
12229ee11f0fSJustin Iurman err = ioam6_init();
12239ee11f0fSJustin Iurman if (err)
12249ee11f0fSJustin Iurman goto ioam6_fail;
12259ee11f0fSJustin Iurman
1226382ed724SVlad Yasevich err = igmp6_late_init();
1227382ed724SVlad Yasevich if (err)
1228382ed724SVlad Yasevich goto igmp6_late_err;
1229382ed724SVlad Yasevich
123094911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
123194911fe3SBenjamin Thery err = ipv6_sysctl_register();
123294911fe3SBenjamin Thery if (err)
123394911fe3SBenjamin Thery goto sysctl_fail;
123494911fe3SBenjamin Thery #endif
1235b7d6df57SPaolo Abeni
1236b7d6df57SPaolo Abeni /* ensure that ipv6 stubs are visible only after ipv6 is ready */
1237b7d6df57SPaolo Abeni wmb();
1238b7d6df57SPaolo Abeni ipv6_stub = &ipv6_stub_impl;
1239d74bad4eSAndrey Ignatov ipv6_bpf_stub = &ipv6_bpf_stub_impl;
12401da177e4SLinus Torvalds out:
12411da177e4SLinus Torvalds return err;
12421da177e4SLinus Torvalds
124394911fe3SBenjamin Thery #ifdef CONFIG_SYSCTL
124494911fe3SBenjamin Thery sysctl_fail:
1245382ed724SVlad Yasevich igmp6_late_cleanup();
124694911fe3SBenjamin Thery #endif
1247382ed724SVlad Yasevich igmp6_late_err:
12489ee11f0fSJustin Iurman ioam6_exit();
12499ee11f0fSJustin Iurman ioam6_fail:
1250a7a29f9cSAlexander Aring rpl_exit();
1251a7a29f9cSAlexander Aring rpl_fail:
1252382ed724SVlad Yasevich seg6_exit();
1253915d7e5eSDavid Lebrun seg6_fail:
1254915d7e5eSDavid Lebrun calipso_exit();
1255cb72d382SHuw Davies calipso_fail:
1256cb72d382SHuw Davies pingv6_exit();
12576d0bfe22SLorenzo Colitti pingv6_fail:
1258eca42aafSVlad Yasevich ipv6_packet_cleanup();
12597f4e4868SDaniel Lezcano ipv6_packet_fail:
12607f4e4868SDaniel Lezcano tcpv6_exit();
12617f4e4868SDaniel Lezcano tcpv6_fail:
1262a6024562STom Herbert udpv6_offload_exit();
1263a6024562STom Herbert udpv6_offload_fail:
12647f4e4868SDaniel Lezcano udplitev6_exit();
12657f4e4868SDaniel Lezcano udplitev6_fail:
12667f4e4868SDaniel Lezcano udpv6_exit();
12677f4e4868SDaniel Lezcano udpv6_fail:
12687f4e4868SDaniel Lezcano ipv6_frag_exit();
1269853cbbaaSDaniel Lezcano ipv6_frag_fail:
1270853cbbaaSDaniel Lezcano ipv6_exthdrs_exit();
1271248b238dSDaniel Lezcano ipv6_exthdrs_fail:
1272248b238dSDaniel Lezcano addrconf_cleanup();
12731da177e4SLinus Torvalds addrconf_fail:
12742384d025SJeff Barnhill ipv6_anycast_cleanup();
12752384d025SJeff Barnhill ipv6_anycast_fail:
12761da177e4SLinus Torvalds ip6_flowlabel_cleanup();
12770a3e78acSDaniel Lezcano ip6_flowlabel_fail:
12782c861cc6SMichal Kubeček ndisc_late_cleanup();
12792c861cc6SMichal Kubeček ndisc_late_fail:
12801da177e4SLinus Torvalds ip6_route_cleanup();
1281e2fddf5eSDaniel Lezcano ip6_route_fail:
12821da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12831da177e4SLinus Torvalds if6_proc_exit();
12841da177e4SLinus Torvalds proc_if6_fail:
12851da177e4SLinus Torvalds ipv6_misc_proc_exit();
12861da177e4SLinus Torvalds proc_misc6_fail:
1287ba4e58ecSGerrit Renker udplite6_proc_exit();
1288ba4e58ecSGerrit Renker proc_udplite6_fail:
12891da177e4SLinus Torvalds raw6_proc_exit();
12901da177e4SLinus Torvalds proc_raw6_fail:
12911da177e4SLinus Torvalds #endif
12922cc7d573SHarald Welte ipv6_netfilter_fini();
12932cc7d573SHarald Welte netfilter_fail:
12941da177e4SLinus Torvalds igmp6_cleanup();
12951da177e4SLinus Torvalds igmp_fail:
12961da177e4SLinus Torvalds ndisc_cleanup();
12971da177e4SLinus Torvalds ndisc_fail:
129815e66807SWANG Cong icmpv6_cleanup();
1299afe49de4SSabrina Dubroca icmp_fail:
1300afe49de4SSabrina Dubroca ip6_mr_cleanup();
1301afe49de4SSabrina Dubroca ipmr_fail:
1302afe49de4SSabrina Dubroca unregister_pernet_subsys(&inet6_net_ops);
130381c1c178SDaniel Lezcano register_pernet_fail:
13048eb55910SDavid S. Miller sock_unregister(PF_INET6);
1305e2fddf5eSDaniel Lezcano rtnl_unregister_all(PF_INET6);
13067f4e4868SDaniel Lezcano out_sock_register_fail:
13077f4e4868SDaniel Lezcano rawv6_exit();
13086d0bfe22SLorenzo Colitti out_unregister_ping_proto:
13096d0bfe22SLorenzo Colitti proto_unregister(&pingv6_prot);
13101da177e4SLinus Torvalds out_unregister_raw_proto:
13111da177e4SLinus Torvalds proto_unregister(&rawv6_prot);
1312ba4e58ecSGerrit Renker out_unregister_udplite_proto:
1313ba4e58ecSGerrit Renker proto_unregister(&udplitev6_prot);
13141da177e4SLinus Torvalds out_unregister_udp_proto:
13151da177e4SLinus Torvalds proto_unregister(&udpv6_prot);
13161da177e4SLinus Torvalds out_unregister_tcp_proto:
13171da177e4SLinus Torvalds proto_unregister(&tcpv6_prot);
13181da177e4SLinus Torvalds goto out;
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds module_init(inet6_init);
13211da177e4SLinus Torvalds
13221da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_INET6);
1323