blob: aa4882929fd05227f476cc223c3f89089e43aa84 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * PF_INET6 socket protocol family
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Adapted from linux/net/ipv4/af_inet.c
10 *
Ian Morris67ba4152014-08-24 21:53:10 +010011 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * piggy, Karl Knutson : Socket protocol table
Ian Morris67ba4152014-08-24 21:53:10 +010013 * Hideaki YOSHIFUJI : sin6_scope_id support
14 * Arnaldo Melo : check proc_net_create return, cleanups
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 */
16
Joe Perchesf3213832012-05-15 14:11:53 +000017#define pr_fmt(fmt) "IPv6: " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080020#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/errno.h>
22#include <linux/types.h>
23#include <linux/socket.h>
24#include <linux/in.h>
25#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/timer.h>
27#include <linux/string.h>
28#include <linux/sockios.h>
29#include <linux/net.h>
30#include <linux/fcntl.h>
31#include <linux/mm.h>
32#include <linux/interrupt.h>
33#include <linux/proc_fs.h>
34#include <linux/stat.h>
35#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <linux/inet.h>
39#include <linux/netdevice.h>
40#include <linux/icmpv6.h>
Harald Welte2cc7d572005-08-09 19:42:34 -070041#include <linux/netfilter_ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <net/ip.h>
44#include <net/ipv6.h>
45#include <net/udp.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080046#include <net/udplite.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <net/tcp.h>
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +000048#include <net/ping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/protocol.h>
50#include <net/inet_common.h>
KOVACS Krisztian1668e012008-10-01 07:33:10 -070051#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/transp_v6.h>
53#include <net/ip6_route.h>
54#include <net/addrconf.h>
David Ahern3616d082019-03-22 06:06:09 -070055#include <net/ipv6_stubs.h>
Cong Wangf564f452013-08-31 13:44:36 +080056#include <net/ndisc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifdef CONFIG_IPV6_TUNNEL
58#include <net/ip6_tunnel.h>
59#endif
Huw Daviescb72d382016-06-27 15:02:46 -040060#include <net/calipso.h>
David Lebrun915d7e52016-11-08 14:57:40 +010061#include <net/seg6.h>
Alexander Aringa7a29f92020-03-27 18:00:22 -040062#include <net/rpl.h>
Sabrina Dubroca0146dca2020-04-27 17:59:34 +020063#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080065#include <linux/uaccess.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090066#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Tom Herberta6024562016-04-05 08:22:51 -070068#include "ip6_offload.h"
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070MODULE_AUTHOR("Cast of dozens");
71MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
72MODULE_LICENSE("GPL");
73
Pavel Emelyanovf1267342007-11-23 21:28:44 +080074/* The inetsw6 table contains everything that inet6_create needs to
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * build a new socket.
76 */
77static struct list_head inetsw6[SOCK_MAX];
78static DEFINE_SPINLOCK(inetsw6_lock);
79
Brian Haley56d417b2009-06-01 03:07:33 -070080struct ipv6_params ipv6_defaults = {
81 .disable_ipv6 = 0,
82 .autoconf = 1,
83};
84
Eldad Zack647c0c72012-05-05 10:13:53 +000085static int disable_ipv6_mod;
Brian Haley56d417b2009-06-01 03:07:33 -070086
87module_param_named(disable, disable_ipv6_mod, int, 0444);
88MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
89
90module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
91MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
92
93module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
94MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
Brian Haleyfe7ca2e2009-03-04 03:18:11 -080095
David Aherne4348632016-06-09 10:21:00 -070096bool ipv6_mod_enabled(void)
97{
98 return disable_ipv6_mod == 0;
99}
100EXPORT_SYMBOL_GPL(ipv6_mod_enabled);
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
103{
104 const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
105
106 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
107}
108
Eric Paris3f378b62009-11-05 22:18:14 -0800109static int inet6_create(struct net *net, struct socket *sock, int protocol,
110 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct inet_sock *inet;
113 struct ipv6_pinfo *np;
114 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 struct inet_protosw *answer;
116 struct proto *answer_prot;
117 unsigned char answer_flags;
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800118 int try_loading_module = 0;
119 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Hannes Frederic Sowa79462ad02015-12-14 22:03:39 +0100121 if (protocol < 0 || protocol >= IPPROTO_MAX)
122 return -EINVAL;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* Look for the requested type/protocol pair. */
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800125lookup_protocol:
126 err = -ESOCKTNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 rcu_read_lock();
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700128 list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700130 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 /* Check the non-wild match. */
132 if (protocol == answer->protocol) {
133 if (protocol != IPPROTO_IP)
134 break;
135 } else {
136 /* Check for the two wild cases. */
137 if (IPPROTO_IP == protocol) {
138 protocol = answer->protocol;
139 break;
140 }
141 if (IPPROTO_IP == answer->protocol)
142 break;
143 }
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800144 err = -EPROTONOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700147 if (err) {
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800148 if (try_loading_module < 2) {
149 rcu_read_unlock();
150 /*
151 * Be more specific, e.g. net-pf-10-proto-132-type-1
152 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
153 */
154 if (++try_loading_module == 1)
155 request_module("net-pf-%d-proto-%d-type-%d",
156 PF_INET6, protocol, sock->type);
157 /*
158 * Fall back to generic, e.g. net-pf-10-proto-132
159 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
160 */
161 else
162 request_module("net-pf-%d-proto-%d",
163 PF_INET6, protocol);
164 goto lookup_protocol;
165 } else
166 goto out_rcu_unlock;
167 }
168
169 err = -EPERM;
Eric W. Biedermanaf31f412012-11-16 03:03:06 +0000170 if (sock->type == SOCK_RAW && !kern &&
171 !ns_capable(net->user_ns, CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 goto out_rcu_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 sock->ops = answer->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 answer_prot = answer->prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 answer_flags = answer->flags;
177 rcu_read_unlock();
178
Ian Morris63159f22015-03-29 14:00:04 +0100179 WARN_ON(!answer_prot->slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800181 err = -ENOBUFS;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500182 sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot, kern);
Ian Morris63159f22015-03-29 14:00:04 +0100183 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 goto out;
185
186 sock_init_data(sock, sk);
187
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800188 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (INET_PROTOSW_REUSE & answer_flags)
Pavel Emelyanov4a17fd52012-04-19 03:39:36 +0000190 sk->sk_reuse = SK_CAN_REUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 inet = inet_sk(sk);
Paul Moore469de9b2007-01-09 14:37:06 -0800193 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (SOCK_RAW == sock->type) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000196 inet->inet_num = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (IPPROTO_RAW == protocol)
198 inet->hdrincl = 1;
199 }
200
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700201 sk->sk_destruct = inet_sock_destruct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 sk->sk_family = PF_INET6;
203 sk->sk_protocol = protocol;
204
205 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
206
207 inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
208 np->hop_limit = -1;
David S. Millerf935aa92010-05-03 23:42:27 -0700209 np->mcast_hops = IPV6_DEFAULT_MCASTHOPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 np->mc_loop = 1;
Andre Naujoks15033f02018-09-10 10:27:15 +0200211 np->mc_all = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 np->pmtudisc = IPV6_PMTUDISC_WANT;
Eric Dumazeta346abe2019-07-01 06:39:36 -0700213 np->repflow = net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_ESTABLISHED;
Eric Dumazet9fe516b2014-06-27 08:36:16 -0700214 sk->sk_ipv6only = net->ipv6.sysctl.bindv6only;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* Init the ipv4 part of the socket since we can have sockets
217 * using v6 API for ipv4.
218 */
219 inet->uc_ttl = -1;
220
221 inet->mc_loop = 1;
222 inet->mc_ttl = 1;
223 inet->mc_index = 0;
224 inet->mc_list = NULL;
Jiri Benc4c507d22012-02-09 09:35:49 +0000225 inet->rcv_tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Hannes Frederic Sowa974eda12013-12-14 05:13:38 +0100227 if (net->ipv4.sysctl_ip_no_pmtu_disc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 inet->pmtudisc = IP_PMTUDISC_DONT;
229 else
230 inet->pmtudisc = IP_PMTUDISC_WANT;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900231 /*
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700232 * Increment only the relevant sk_prot->socks debug field, this changes
233 * the previous behaviour of incrementing both the equivalent to
234 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
235 *
236 * This allows better debug granularity as we'll know exactly how many
237 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
238 * transport protocol socks. -acme
239 */
240 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000242 if (inet->inet_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* It assumes that any protocol which allows
244 * the user to assign a number at socket
245 * creation time automatically shares.
246 */
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000247 inet->inet_sport = htons(inet->inet_num);
Craig Gallek086c6532016-02-10 11:50:35 -0500248 err = sk->sk_prot->hash(sk);
249 if (err) {
250 sk_common_release(sk);
251 goto out;
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254 if (sk->sk_prot->init) {
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800255 err = sk->sk_prot->init(sk);
256 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 sk_common_release(sk);
258 goto out;
259 }
260 }
David Ahern610236582016-12-01 08:48:04 -0800261
262 if (!kern) {
263 err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
264 if (err) {
265 sk_common_release(sk);
266 goto out;
267 }
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269out:
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800270 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271out_rcu_unlock:
272 rcu_read_unlock();
273 goto out;
274}
275
David Ahern032234d2018-04-17 10:00:39 -0700276static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
277 bool force_bind_address_no_port, bool with_lock)
Andrey Ignatov3679d582018-03-30 15:08:04 -0700278{
279 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
280 struct inet_sock *inet = inet_sk(sk);
281 struct ipv6_pinfo *np = inet6_sk(sk);
282 struct net *net = sock_net(sk);
283 __be32 v4addr = 0;
284 unsigned short snum;
285 bool saved_ipv6only;
286 int addr_type = 0;
287 int err = 0;
288
Marcus Meissner5a079c32011-06-06 06:00:07 +0000289 if (addr->sin6_family != AF_INET6)
Marcus Meissnerc349a522011-07-04 01:30:29 +0000290 return -EAFNOSUPPORT;
Marcus Meissner5a079c32011-06-06 06:00:07 +0000291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 addr_type = ipv6_addr_type(&addr->sin6_addr);
Andrey Ignatov3679d582018-03-30 15:08:04 -0700293 if ((addr_type & IPV6_ADDR_MULTICAST) && sk->sk_type == SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return -EINVAL;
295
296 snum = ntohs(addr->sin6_port);
Maciej Żenczykowski82f31eb2019-11-25 15:37:04 -0800297 if (snum && inet_port_requires_bind_service(net, snum) &&
Krister Johansen4548b682017-01-20 17:49:11 -0800298 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return -EACCES;
300
Andrey Ignatov3679d582018-03-30 15:08:04 -0700301 if (with_lock)
302 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* Check these errors (active socket, double bind). */
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000305 if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 err = -EINVAL;
307 goto out;
308 }
309
310 /* Check if the address belongs to the host. */
311 if (addr_type == IPV6_ADDR_MAPPED) {
David Ahernec90ad32019-01-04 16:58:15 -0800312 struct net_device *dev = NULL;
Vlad Yasevich63d99502009-03-24 16:24:50 +0000313 int chk_addr_ret;
314
Vlad Yasevich783ed5a2009-03-24 16:24:48 +0000315 /* Binding to v4-mapped address on a v6-only socket
316 * makes no sense
317 */
Eric Dumazet9fe516b2014-06-27 08:36:16 -0700318 if (sk->sk_ipv6only) {
Vlad Yasevich783ed5a2009-03-24 16:24:48 +0000319 err = -EINVAL;
320 goto out;
321 }
Vlad Yasevich63d99502009-03-24 16:24:50 +0000322
David Ahernd4a7e9b2019-01-05 07:35:04 -0800323 rcu_read_lock();
David Ahernec90ad32019-01-04 16:58:15 -0800324 if (sk->sk_bound_dev_if) {
325 dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
326 if (!dev) {
327 err = -ENODEV;
David Ahernd4a7e9b2019-01-05 07:35:04 -0800328 goto out_unlock;
David Ahernec90ad32019-01-04 16:58:15 -0800329 }
330 }
331
Uwe Kleine-Königa34f0b32010-12-10 14:55:42 +0100332 /* Reproduce AF_INET checks to make the bindings consistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 v4addr = addr->sin6_addr.s6_addr32[3];
David Ahernec90ad32019-01-04 16:58:15 -0800334 chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr);
David Ahernd4a7e9b2019-01-05 07:35:04 -0800335 rcu_read_unlock();
336
Vincent Bernat83ba4642018-07-31 21:18:11 +0200337 if (!inet_can_nonlocal_bind(net, inet) &&
Vlad Yasevich63d99502009-03-24 16:24:50 +0000338 v4addr != htonl(INADDR_ANY) &&
339 chk_addr_ret != RTN_LOCAL &&
340 chk_addr_ret != RTN_MULTICAST &&
Bruno Prémontca6982b2009-08-23 19:06:28 -0700341 chk_addr_ret != RTN_BROADCAST) {
342 err = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto out;
Bruno Prémontca6982b2009-08-23 19:06:28 -0700344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 } else {
346 if (addr_type != IPV6_ADDR_ANY) {
347 struct net_device *dev = NULL;
348
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100349 rcu_read_lock();
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000350 if (__ipv6_addr_needs_scope_id(addr_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (addr_len >= sizeof(struct sockaddr_in6) &&
352 addr->sin6_scope_id) {
353 /* Override any existing binding, if another one
354 * is supplied by user.
355 */
356 sk->sk_bound_dev_if = addr->sin6_scope_id;
357 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* Binding to link-local address requires an interface */
360 if (!sk->sk_bound_dev_if) {
361 err = -EINVAL;
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100362 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
David Ahernc5ee0662019-01-02 18:57:09 -0800364 }
365
366 if (sk->sk_bound_dev_if) {
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100367 dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (!dev) {
369 err = -ENODEV;
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100370 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 }
373
374 /* ipv4 addr of the socket is invalid. Only the
375 * unspecified and mapped address have a v4 equivalent.
376 */
377 v4addr = LOOPBACK4_IPV6;
378 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
Vincent Bernat83ba4642018-07-31 21:18:11 +0200379 if (!ipv6_can_nonlocal_bind(net, inet) &&
Balazs Scheidler0a513f62010-10-21 16:10:03 +0200380 !ipv6_chk_addr(net, &addr->sin6_addr,
Daniel Lezcanobfeade02008-01-10 22:43:18 -0800381 dev, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 err = -EADDRNOTAVAIL;
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100383 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385 }
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100386 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 }
389
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000390 inet->inet_rcv_saddr = v4addr;
391 inet->inet_saddr = v4addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Eric Dumazetefe42082013-10-03 15:42:29 -0700393 sk->sk_v6_rcv_saddr = addr->sin6_addr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!(addr_type & IPV6_ADDR_MULTICAST))
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000396 np->saddr = addr->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Martin KaFai Lau7ece54a2018-01-24 23:15:27 -0800398 saved_ipv6only = sk->sk_ipv6only;
399 if (addr_type != IPV6_ADDR_ANY && addr_type != IPV6_ADDR_MAPPED)
400 sk->sk_ipv6only = 1;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /* Make sure we are allowed to bind here. */
Andrey Ignatovaac3fc32018-03-30 15:08:07 -0700403 if (snum || !(inet->bind_address_no_port ||
404 force_bind_address_no_port)) {
405 if (sk->sk_prot->get_port(sk, snum)) {
406 sk->sk_ipv6only = saved_ipv6only;
407 inet_reset_saddr(sk);
408 err = -EADDRINUSE;
409 goto out;
410 }
411 err = BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk);
412 if (err) {
413 sk->sk_ipv6only = saved_ipv6only;
414 inet_reset_saddr(sk);
415 goto out;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Martin KaFai Lau7ece54a2018-01-24 23:15:27 -0800419 if (addr_type != IPV6_ADDR_ANY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
421 if (snum)
422 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000423 inet->inet_sport = htons(inet->inet_num);
424 inet->inet_dport = 0;
425 inet->inet_daddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426out:
Andrey Ignatov3679d582018-03-30 15:08:04 -0700427 if (with_lock)
428 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return err;
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100430out_unlock:
431 rcu_read_unlock();
432 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900434
David Ahern032234d2018-04-17 10:00:39 -0700435/* bind for INET6 API */
436int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
437{
438 struct sock *sk = sock->sk;
439 int err = 0;
440
441 /* If the socket has its own bind function then use it. */
442 if (sk->sk_prot->bind)
443 return sk->sk_prot->bind(sk, uaddr, addr_len);
444
445 if (addr_len < SIN6_LEN_RFC2133)
446 return -EINVAL;
447
448 /* BPF prog is run before any checks are done so that if the prog
449 * changes context in a wrong way it will be caught.
450 */
451 err = BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr);
452 if (err)
453 return err;
454
455 return __inet6_bind(sk, uaddr, addr_len, false, true);
456}
457EXPORT_SYMBOL(inet6_bind);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459int inet6_release(struct socket *sock)
460{
461 struct sock *sk = sock->sk;
462
Ian Morris63159f22015-03-29 14:00:04 +0100463 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return -EINVAL;
465
466 /* Free mc lists */
467 ipv6_sock_mc_close(sk);
468
469 /* Free ac lists */
470 ipv6_sock_ac_close(sk);
471
472 return inet_release(sock);
473}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900474EXPORT_SYMBOL(inet6_release);
475
Brian Haley7d06b2e2008-06-14 17:04:49 -0700476void inet6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct ipv6_pinfo *np = inet6_sk(sk);
479 struct sk_buff *skb;
480 struct ipv6_txoptions *opt;
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* Release rx options */
483
Eldad Zack647c0c72012-05-05 10:13:53 +0000484 skb = xchg(&np->pktoptions, NULL);
zhong jiangf2a2f212018-09-20 17:37:46 +0800485 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Eldad Zack647c0c72012-05-05 10:13:53 +0000487 skb = xchg(&np->rxpmtu, NULL);
zhong jiangf2a2f212018-09-20 17:37:46 +0800488 kfree_skb(skb);
Brian Haley4b340ae2010-04-23 11:26:09 +0000489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /* Free flowlabels */
491 fl6_free_socklist(sk);
492
493 /* Free tx options */
494
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800495 opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL);
496 if (opt) {
497 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
498 txopt_put(opt);
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800501EXPORT_SYMBOL_GPL(inet6_destroy_sock);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/*
504 * This does both peername and sockname.
505 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100508 int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Eldad Zack647c0c72012-05-05 10:13:53 +0000510 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct sock *sk = sock->sk;
512 struct inet_sock *inet = inet_sk(sk);
513 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 sin->sin6_family = AF_INET6;
516 sin->sin6_flowinfo = 0;
517 sin->sin6_scope_id = 0;
518 if (peer) {
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000519 if (!inet->inet_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return -ENOTCONN;
521 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
522 peer == 1)
523 return -ENOTCONN;
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000524 sin->sin6_port = inet->inet_dport;
Eric Dumazetefe42082013-10-03 15:42:29 -0700525 sin->sin6_addr = sk->sk_v6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (np->sndflow)
527 sin->sin6_flowinfo = np->flow_label;
528 } else {
Eric Dumazetefe42082013-10-03 15:42:29 -0700529 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000530 sin->sin6_addr = np->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 else
Eric Dumazetefe42082013-10-03 15:42:29 -0700532 sin->sin6_addr = sk->sk_v6_rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000534 sin->sin6_port = inet->inet_sport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000536 sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
537 sk->sk_bound_dev_if);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100538 return sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900540EXPORT_SYMBOL(inet6_getname);
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
543{
544 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900545 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Eldad Zack647c0c72012-05-05 10:13:53 +0000547 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 case SIOCADDRT:
549 case SIOCDELRT:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900550
Eric Dumazeta02cec22010-09-22 20:43:57 +0000551 return ipv6_route_ioctl(net, cmd, (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 case SIOCSIFADDR:
Daniel Lezcanoaf284932008-03-05 10:46:57 -0800554 return addrconf_add_ifaddr(net, (void __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 case SIOCDIFADDR:
Daniel Lezcanoaf284932008-03-05 10:46:57 -0800556 return addrconf_del_ifaddr(net, (void __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 case SIOCSIFDSTADDR:
Daniel Lezcanoaf284932008-03-05 10:46:57 -0800558 return addrconf_set_dstaddr(net, (void __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -0800560 if (!sk->sk_prot->ioctl)
561 return -ENOIOCTLCMD;
562 return sk->sk_prot->ioctl(sk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 /*NOTREACHED*/
Eric Dumazeta02cec22010-09-22 20:43:57 +0000565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900567EXPORT_SYMBOL(inet6_ioctl);
568
Paolo Abeni164c51f2019-07-03 16:06:55 +0200569INDIRECT_CALLABLE_DECLARE(int udpv6_sendmsg(struct sock *, struct msghdr *,
570 size_t));
Paolo Abeni68ab5d12019-07-03 16:06:53 +0200571int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
572{
573 struct sock *sk = sock->sk;
574
575 if (unlikely(inet_send_prepare(sk)))
576 return -EAGAIN;
577
Paolo Abeni164c51f2019-07-03 16:06:55 +0200578 return INDIRECT_CALL_2(sk->sk_prot->sendmsg, tcp_sendmsg, udpv6_sendmsg,
579 sk, msg, size);
Paolo Abeni68ab5d12019-07-03 16:06:53 +0200580}
581
Paolo Abeni164c51f2019-07-03 16:06:55 +0200582INDIRECT_CALLABLE_DECLARE(int udpv6_recvmsg(struct sock *, struct msghdr *,
583 size_t, int, int, int *));
Paolo Abeni68ab5d12019-07-03 16:06:53 +0200584int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
585 int flags)
586{
587 struct sock *sk = sock->sk;
588 int addr_len = 0;
589 int err;
590
591 if (likely(!(flags & MSG_ERRQUEUE)))
592 sock_rps_record_flow(sk);
593
Paolo Abeni164c51f2019-07-03 16:06:55 +0200594 err = INDIRECT_CALL_2(sk->sk_prot->recvmsg, tcp_recvmsg, udpv6_recvmsg,
595 sk, msg, size, flags & MSG_DONTWAIT,
596 flags & ~MSG_DONTWAIT, &addr_len);
Paolo Abeni68ab5d12019-07-03 16:06:53 +0200597 if (err >= 0)
598 msg->msg_namelen = addr_len;
599 return err;
600}
601
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800602const struct proto_ops inet6_stream_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800603 .family = PF_INET6,
604 .owner = THIS_MODULE,
605 .release = inet6_release,
606 .bind = inet6_bind,
607 .connect = inet_stream_connect, /* ok */
608 .socketpair = sock_no_socketpair, /* a do nothing */
609 .accept = inet_accept, /* ok */
610 .getname = inet6_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700611 .poll = tcp_poll, /* ok */
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800612 .ioctl = inet6_ioctl, /* must change */
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +0200613 .gettstamp = sock_gettstamp,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800614 .listen = inet_listen, /* ok */
615 .shutdown = inet_shutdown, /* ok */
616 .setsockopt = sock_common_setsockopt, /* ok */
617 .getsockopt = sock_common_getsockopt, /* ok */
Paolo Abeni68ab5d12019-07-03 16:06:53 +0200618 .sendmsg = inet6_sendmsg, /* retpoline's sake */
619 .recvmsg = inet6_recvmsg, /* retpoline's sake */
Eric Dumazet05255b82018-04-27 08:58:08 -0700620#ifdef CONFIG_MMU
Eric Dumazet93ab6cc2018-04-16 10:33:38 -0700621 .mmap = tcp_mmap,
Eric Dumazet05255b82018-04-27 08:58:08 -0700622#endif
Changli Gao7ba42912010-07-10 20:41:55 +0000623 .sendpage = inet_sendpage,
John Fastabend45f91bd2017-08-15 22:31:10 -0700624 .sendmsg_locked = tcp_sendmsg_locked,
625 .sendpage_locked = tcp_sendpage_locked,
Jens Axboea0974dd2007-11-06 23:31:58 -0800626 .splice_read = tcp_splice_read,
Tom Herbert32035582016-08-28 14:43:18 -0700627 .read_sock = tcp_read_sock,
628 .peek_len = tcp_peek_len,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800629#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800630 .compat_setsockopt = compat_sock_common_setsockopt,
631 .compat_getsockopt = compat_sock_common_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800632#endif
Eric Dumazetd1361842018-04-16 10:33:35 -0700633 .set_rcvlowat = tcp_set_rcvlowat,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634};
635
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800636const struct proto_ops inet6_dgram_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800637 .family = PF_INET6,
638 .owner = THIS_MODULE,
639 .release = inet6_release,
640 .bind = inet6_bind,
641 .connect = inet_dgram_connect, /* ok */
642 .socketpair = sock_no_socketpair, /* a do nothing */
643 .accept = sock_no_accept, /* a do nothing */
644 .getname = inet6_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700645 .poll = udp_poll, /* ok */
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800646 .ioctl = inet6_ioctl, /* must change */
Arnd Bergmannc7cbdbf2019-04-17 22:51:48 +0200647 .gettstamp = sock_gettstamp,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800648 .listen = sock_no_listen, /* ok */
649 .shutdown = inet_shutdown, /* ok */
650 .setsockopt = sock_common_setsockopt, /* ok */
651 .getsockopt = sock_common_getsockopt, /* ok */
Paolo Abeni68ab5d12019-07-03 16:06:53 +0200652 .sendmsg = inet6_sendmsg, /* retpoline's sake */
653 .recvmsg = inet6_recvmsg, /* retpoline's sake */
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800654 .mmap = sock_no_mmap,
655 .sendpage = sock_no_sendpage,
samanthakumar627d2d62016-04-05 12:41:16 -0400656 .set_peek_off = sk_set_peek_off,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800657#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800658 .compat_setsockopt = compat_sock_common_setsockopt,
659 .compat_getsockopt = compat_sock_common_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800660#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661};
662
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000663static const struct net_proto_family inet6_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 .family = PF_INET6,
665 .create = inet6_create,
666 .owner = THIS_MODULE,
667};
668
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800669int inet6_register_protosw(struct inet_protosw *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 struct list_head *lh;
672 struct inet_protosw *answer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct list_head *last_perm;
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800674 int protocol = p->protocol;
675 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 spin_lock_bh(&inetsw6_lock);
678
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800679 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (p->type >= SOCK_MAX)
681 goto out_illegal;
682
683 /* If we are trying to override a permanent protocol, bail. */
684 answer = NULL;
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800685 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 last_perm = &inetsw6[p->type];
687 list_for_each(lh, &inetsw6[p->type]) {
688 answer = list_entry(lh, struct inet_protosw, list);
689
690 /* Check only the non-wild match. */
691 if (INET_PROTOSW_PERMANENT & answer->flags) {
692 if (protocol == answer->protocol)
693 break;
694 last_perm = lh;
695 }
696
697 answer = NULL;
698 }
699 if (answer)
700 goto out_permanent;
701
702 /* Add the new entry after the last permanent entry if any, so that
703 * the new entry does not override a permanent entry when matched with
704 * a wild-card protocol. But it is allowed to override any existing
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900705 * non-permanent entry. This means that when we remove this entry, the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * system automatically returns to the old behavior.
707 */
708 list_add_rcu(&p->list, last_perm);
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800709 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710out:
711 spin_unlock_bh(&inetsw6_lock);
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800712 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714out_permanent:
Joe Perchesf3213832012-05-15 14:11:53 +0000715 pr_err("Attempt to override permanent protocol %d\n", protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 goto out;
717
718out_illegal:
Joe Perchesf3213832012-05-15 14:11:53 +0000719 pr_err("Ignoring attempt to register invalid socket type %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 p->type);
721 goto out;
722}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900723EXPORT_SYMBOL(inet6_register_protosw);
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725void
726inet6_unregister_protosw(struct inet_protosw *p)
727{
728 if (INET_PROTOSW_PERMANENT & p->flags) {
Joe Perchesf3213832012-05-15 14:11:53 +0000729 pr_err("Attempt to unregister permanent protocol %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 p->protocol);
731 } else {
732 spin_lock_bh(&inetsw6_lock);
733 list_del_rcu(&p->list);
734 spin_unlock_bh(&inetsw6_lock);
735
736 synchronize_net();
737 }
738}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900739EXPORT_SYMBOL(inet6_unregister_protosw);
740
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800741int inet6_sk_rebuild_header(struct sock *sk)
742{
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800743 struct ipv6_pinfo *np = inet6_sk(sk);
David S. Miller68d0c6d2011-03-01 13:19:07 -0800744 struct dst_entry *dst;
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800745
746 dst = __sk_dst_check(sk, np->dst_cookie);
747
Ian Morris63159f22015-03-29 14:00:04 +0100748 if (!dst) {
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800749 struct inet_sock *inet = inet_sk(sk);
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000750 struct in6_addr *final_p, final;
David S. Miller4c9483b2011-03-12 16:22:43 -0500751 struct flowi6 fl6;
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800752
David S. Miller4c9483b2011-03-12 16:22:43 -0500753 memset(&fl6, 0, sizeof(fl6));
754 fl6.flowi6_proto = sk->sk_protocol;
Eric Dumazetefe42082013-10-03 15:42:29 -0700755 fl6.daddr = sk->sk_v6_daddr;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000756 fl6.saddr = np->saddr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500757 fl6.flowlabel = np->flow_label;
758 fl6.flowi6_oif = sk->sk_bound_dev_if;
759 fl6.flowi6_mark = sk->sk_mark;
David S. Miller1958b852011-03-12 16:36:19 -0500760 fl6.fl6_dport = inet->inet_dport;
761 fl6.fl6_sport = inet->inet_sport;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900762 fl6.flowi6_uid = sk->sk_uid;
David S. Miller4c9483b2011-03-12 16:22:43 -0500763 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800764
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800765 rcu_read_lock();
766 final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt),
767 &final);
768 rcu_read_unlock();
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800769
Sabrina Dubrocac4e85f72019-12-04 15:35:52 +0100770 dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
David S. Miller68d0c6d2011-03-01 13:19:07 -0800771 if (IS_ERR(dst)) {
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800772 sk->sk_route_caps = 0;
David S. Miller68d0c6d2011-03-01 13:19:07 -0800773 sk->sk_err_soft = -PTR_ERR(dst);
774 return PTR_ERR(dst);
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800775 }
776
Eric Dumazet6bd4f352015-12-02 21:53:57 -0800777 ip6_dst_store(sk, dst, NULL, NULL);
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800778 }
779
780 return 0;
781}
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800782EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
783
Eric Dumazeta2247722014-09-27 09:50:56 -0700784bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
785 const struct inet6_skb_parm *opt)
Arnaldo Carvalho de Melo399c07d2005-12-13 23:24:28 -0800786{
Eric Dumazet92113bf2012-05-18 08:14:11 +0200787 const struct ipv6_pinfo *np = inet6_sk(sk);
Arnaldo Carvalho de Melo399c07d2005-12-13 23:24:28 -0800788
789 if (np->rxopt.all) {
Florian Westphal8b58a392015-07-08 23:32:12 +0200790 if (((opt->flags & IP6SKB_HOPBYHOP) &&
791 (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) ||
Florent Fourcotac1eabc2013-12-08 15:47:01 +0100792 (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) &&
Arnaldo Carvalho de Melo399c07d2005-12-13 23:24:28 -0800793 np->rxopt.bits.rxflow) ||
794 (opt->srcrt && (np->rxopt.bits.srcrt ||
795 np->rxopt.bits.osrcrt)) ||
796 ((opt->dst1 || opt->dst0) &&
797 (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
Eric Dumazet92113bf2012-05-18 08:14:11 +0200798 return true;
Arnaldo Carvalho de Melo399c07d2005-12-13 23:24:28 -0800799 }
Eric Dumazet92113bf2012-05-18 08:14:11 +0200800 return false;
Arnaldo Carvalho de Melo399c07d2005-12-13 23:24:28 -0800801}
Arnaldo Carvalho de Melo399c07d2005-12-13 23:24:28 -0800802EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
803
Stephen Hemminger7546dd92009-03-09 08:18:29 +0000804static struct packet_type ipv6_packet_type __read_mostly = {
Harvey Harrison09640e632009-02-01 00:45:17 -0800805 .type = cpu_to_be16(ETH_P_IPV6),
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900806 .func = ipv6_rcv,
Edward Creed8269e22018-07-05 15:49:42 +0100807 .list_func = ipv6_list_rcv,
Vlad Yasevich22061d82012-11-15 08:49:11 +0000808};
809
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900810static int __init ipv6_packet_init(void)
811{
812 dev_add_pack(&ipv6_packet_type);
813 return 0;
814}
815
816static void ipv6_packet_cleanup(void)
817{
818 dev_remove_pack(&ipv6_packet_type);
819}
820
Denis V. Luneve43291c2008-10-07 14:48:53 -0700821static int __net_init ipv6_init_mibs(struct net *net)
822{
John Stultz827da442013-10-07 15:51:58 -0700823 int i;
824
WANG Cong698365f2014-05-05 15:55:55 -0700825 net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib);
826 if (!net->mib.udp_stats_in6)
Denis V. Lunev0c7ed672008-10-07 14:49:36 -0700827 return -ENOMEM;
WANG Cong698365f2014-05-05 15:55:55 -0700828 net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib);
829 if (!net->mib.udplite_stats_in6)
Denis V. Lunevbe713a42008-10-07 14:50:06 -0700830 goto err_udplite_mib;
WANG Cong698365f2014-05-05 15:55:55 -0700831 net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib);
832 if (!net->mib.ipv6_statistics)
Denis V. Lunev9261e532008-10-08 10:36:03 -0700833 goto err_ip_mib;
John Stultz827da442013-10-07 15:51:58 -0700834
835 for_each_possible_cpu(i) {
836 struct ipstats_mib *af_inet6_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700837 af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i);
John Stultz827da442013-10-07 15:51:58 -0700838 u64_stats_init(&af_inet6_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700839 }
840
841
WANG Cong698365f2014-05-05 15:55:55 -0700842 net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib);
843 if (!net->mib.icmpv6_statistics)
Denis V. Lunev9261e532008-10-08 10:36:03 -0700844 goto err_icmp_mib;
Eric Dumazet2a244442011-11-13 01:24:04 +0000845 net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
846 GFP_KERNEL);
847 if (!net->mib.icmpv6msg_statistics)
Denis V. Lunev9261e532008-10-08 10:36:03 -0700848 goto err_icmpmsg_mib;
Denis V. Luneve43291c2008-10-07 14:48:53 -0700849 return 0;
Denis V. Lunevbe713a42008-10-07 14:50:06 -0700850
Denis V. Lunev9261e532008-10-08 10:36:03 -0700851err_icmpmsg_mib:
WANG Cong698365f2014-05-05 15:55:55 -0700852 free_percpu(net->mib.icmpv6_statistics);
Denis V. Lunev9261e532008-10-08 10:36:03 -0700853err_icmp_mib:
WANG Cong698365f2014-05-05 15:55:55 -0700854 free_percpu(net->mib.ipv6_statistics);
Denis V. Lunev9261e532008-10-08 10:36:03 -0700855err_ip_mib:
WANG Cong698365f2014-05-05 15:55:55 -0700856 free_percpu(net->mib.udplite_stats_in6);
Denis V. Lunevbe713a42008-10-07 14:50:06 -0700857err_udplite_mib:
WANG Cong698365f2014-05-05 15:55:55 -0700858 free_percpu(net->mib.udp_stats_in6);
Denis V. Lunevbe713a42008-10-07 14:50:06 -0700859 return -ENOMEM;
Denis V. Luneve43291c2008-10-07 14:48:53 -0700860}
861
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000862static void ipv6_cleanup_mibs(struct net *net)
Denis V. Luneve43291c2008-10-07 14:48:53 -0700863{
WANG Cong698365f2014-05-05 15:55:55 -0700864 free_percpu(net->mib.udp_stats_in6);
865 free_percpu(net->mib.udplite_stats_in6);
866 free_percpu(net->mib.ipv6_statistics);
867 free_percpu(net->mib.icmpv6_statistics);
Eric Dumazet2a244442011-11-13 01:24:04 +0000868 kfree(net->mib.icmpv6msg_statistics);
Denis V. Luneve43291c2008-10-07 14:48:53 -0700869}
870
Alexey Dobriyane7dc8492008-10-13 18:54:07 -0700871static int __net_init inet6_net_init(struct net *net)
Daniel Lezcano81c1c172008-01-10 02:48:33 -0800872{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -0700873 int err = 0;
874
Daniel Lezcano99bc9c42008-01-10 02:54:53 -0800875 net->ipv6.sysctl.bindv6only = 0;
Daniel Lezcano41a76902008-01-10 03:02:40 -0800876 net->ipv6.sysctl.icmpv6_time = 1*HZ;
Virgile Jarrye6f86b02018-08-10 17:48:15 +0200877 net->ipv6.sysctl.icmpv6_echo_ignore_all = 0;
Stephen Suryaputra03f1ecc2019-03-19 12:37:12 -0400878 net->ipv6.sysctl.icmpv6_echo_ignore_multicast = 0;
Stephen Suryaputra0b03a5c2019-03-20 10:29:27 -0400879 net->ipv6.sysctl.icmpv6_echo_ignore_anycast = 0;
Stephen Suryaputra0bc19982019-04-17 16:35:49 -0400880
881 /* By default, rate limit error messages.
882 * Except for pmtu discovery, it would break it.
883 * proc_do_large_bitmap needs pointer to the bitmap.
884 */
885 bitmap_set(net->ipv6.sysctl.icmpv6_ratemask, 0, ICMPV6_ERRMSG_MAX + 1);
886 bitmap_clear(net->ipv6.sysctl.icmpv6_ratemask, ICMPV6_PKT_TOOBIG, 1);
887 net->ipv6.sysctl.icmpv6_ratemask_ptr = net->ipv6.sysctl.icmpv6_ratemask;
888
Florent Fourcot6444f722014-01-17 17:15:05 +0100889 net->ipv6.sysctl.flowlabel_consistency = 1;
Tom Herbert42240902015-07-31 16:52:12 -0700890 net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +0100891 net->ipv6.sysctl.idgen_retries = 3;
892 net->ipv6.sysctl.idgen_delay = 1 * HZ;
Tom Herbertbe268492015-07-31 16:52:13 -0700893 net->ipv6.sysctl.flowlabel_state_ranges = 0;
Tom Herbert47d3d7a2017-10-30 14:16:00 -0700894 net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT;
895 net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT;
896 net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN;
897 net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN;
Hannes Frederic Sowa812918c2014-10-06 19:58:37 +0200898 atomic_set(&net->ipv6.fib6_sernum, 1);
Daniel Lezcanoe71e0342008-01-10 02:56:03 -0800899
Denis V. Luneve43291c2008-10-07 14:48:53 -0700900 err = ipv6_init_mibs(net);
901 if (err)
902 return err;
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -0700903#ifdef CONFIG_PROC_FS
904 err = udp6_proc_init(net);
905 if (err)
906 goto out;
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -0700907 err = tcp6_proc_init(net);
908 if (err)
909 goto proc_tcp6_fail;
Daniel Lezcano6ab57e72008-03-26 16:52:32 -0700910 err = ac6_proc_init(net);
911 if (err)
912 goto proc_ac6_fail;
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -0700913#endif
914 return err;
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -0700915
916#ifdef CONFIG_PROC_FS
Daniel Lezcano6ab57e72008-03-26 16:52:32 -0700917proc_ac6_fail:
918 tcp6_proc_exit(net);
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -0700919proc_tcp6_fail:
920 udp6_proc_exit(net);
Denis V. Luneve43291c2008-10-07 14:48:53 -0700921out:
922 ipv6_cleanup_mibs(net);
923 return err;
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -0700924#endif
Daniel Lezcano81c1c172008-01-10 02:48:33 -0800925}
926
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +0000927static void __net_exit inet6_net_exit(struct net *net)
Daniel Lezcano81c1c172008-01-10 02:48:33 -0800928{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -0700929#ifdef CONFIG_PROC_FS
930 udp6_proc_exit(net);
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -0700931 tcp6_proc_exit(net);
Daniel Lezcano6ab57e72008-03-26 16:52:32 -0700932 ac6_proc_exit(net);
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -0700933#endif
Denis V. Luneve43291c2008-10-07 14:48:53 -0700934 ipv6_cleanup_mibs(net);
Daniel Lezcano81c1c172008-01-10 02:48:33 -0800935}
936
937static struct pernet_operations inet6_net_ops = {
938 .init = inet6_net_init,
939 .exit = inet6_net_exit,
940};
941
Peter Oskolkov9b0a6a92019-02-13 11:53:38 -0800942static int ipv6_route_input(struct sk_buff *skb)
943{
944 ip6_route_input(skb);
945 return skb_dst(skb)->error;
946}
947
Cong Wang5f81bd22013-08-31 13:44:30 +0800948static const struct ipv6_stub ipv6_stub_impl = {
949 .ipv6_sock_mc_join = ipv6_sock_mc_join,
950 .ipv6_sock_mc_drop = ipv6_sock_mc_drop,
Sabrina Dubroca6c8991f2019-12-04 15:35:53 +0100951 .ipv6_dst_lookup_flow = ip6_dst_lookup_flow,
Peter Oskolkov9b0a6a92019-02-13 11:53:38 -0800952 .ipv6_route_input = ipv6_route_input,
David Ahern65a20222018-05-09 20:34:25 -0700953 .fib6_get_table = fib6_get_table,
954 .fib6_table_lookup = fib6_table_lookup,
955 .fib6_lookup = fib6_lookup,
David Ahernb1d40992019-04-16 14:35:59 -0700956 .fib6_select_path = fib6_select_path,
David Ahern901731b2018-05-21 09:08:14 -0700957 .ip6_mtu_from_fib6 = ip6_mtu_from_fib6,
David Ahern1aefd3d2019-04-05 16:30:24 -0700958 .fib6_nh_init = fib6_nh_init,
959 .fib6_nh_release = fib6_nh_release,
David Aherncdaa16a2019-05-22 12:04:40 -0700960 .fib6_update_sernum = fib6_update_sernum_stub,
David Ahern19a3b7e2019-05-22 12:04:41 -0700961 .fib6_rt_update = fib6_rt_update,
David Ahern68a9b132019-05-22 12:04:39 -0700962 .ip6_del_rt = ip6_del_rt,
Cong Wang5f81bd22013-08-31 13:44:30 +0800963 .udpv6_encap_enable = udpv6_encap_enable,
Cong Wangf564f452013-08-31 13:44:36 +0800964 .ndisc_send_na = ndisc_send_na,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200965#if IS_ENABLED(CONFIG_XFRM)
Florian Westphal3e50ddd2020-05-04 10:06:06 +0200966 .xfrm6_local_rxpmtu = xfrm6_local_rxpmtu,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200967 .xfrm6_udp_encap_rcv = xfrm6_udp_encap_rcv,
Sabrina Dubroca26333c32020-04-27 17:59:35 +0200968 .xfrm6_rcv_encap = xfrm6_rcv_encap,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200969#endif
Cong Wange15a00a2013-08-31 13:44:34 +0800970 .nd_tbl = &nd_tbl,
Cong Wang5f81bd22013-08-31 13:44:30 +0800971};
972
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700973static const struct ipv6_bpf_stub ipv6_bpf_stub_impl = {
974 .inet6_bind = __inet6_bind,
Joe Stringer8a615c62018-10-15 10:27:45 -0700975 .udp6_lib_lookup = __udp6_lib_lookup,
Andrey Ignatovd74bad42018-03-30 15:08:05 -0700976};
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978static int __init inet6_init(void)
979{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900980 struct list_head *r;
Brian Haleyfe7ca2e2009-03-04 03:18:11 -0800981 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Eyal Birgerb4772ef2015-03-01 14:58:29 +0200983 sock_skb_cb_check_size(sizeof(struct inet6_skb_parm));
YOSHIFUJI Hideakief047f52006-09-01 00:29:06 -0700984
Brian Haleyfe7ca2e2009-03-04 03:18:11 -0800985 /* Register the socket-side information for inet6_create. */
Eldad Zack647c0c72012-05-05 10:13:53 +0000986 for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
Brian Haleyfe7ca2e2009-03-04 03:18:11 -0800987 INIT_LIST_HEAD(r);
988
Brian Haley56d417b2009-06-01 03:07:33 -0700989 if (disable_ipv6_mod) {
Joe Perchesf3213832012-05-15 14:11:53 +0000990 pr_info("Loaded, but administratively disabled, reboot required to enable\n");
Brian Haleyfe7ca2e2009-03-04 03:18:11 -0800991 goto out;
992 }
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 err = proto_register(&tcpv6_prot, 1);
995 if (err)
996 goto out;
997
998 err = proto_register(&udpv6_prot, 1);
999 if (err)
1000 goto out_unregister_tcp_proto;
1001
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001002 err = proto_register(&udplitev6_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (err)
1004 goto out_unregister_udp_proto;
1005
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001006 err = proto_register(&rawv6_prot, 1);
1007 if (err)
1008 goto out_unregister_udplite_proto;
1009
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +00001010 err = proto_register(&pingv6_prot, 1);
1011 if (err)
Sabrina Dubrocaa03dc36b2018-08-28 13:40:52 +02001012 goto out_unregister_raw_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /* We MUST register RAW sockets before we create the ICMP6,
1015 * IGMP6, or NDISC control sockets.
1016 */
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001017 err = rawv6_init();
1018 if (err)
Sabrina Dubrocaa03dc36b2018-08-28 13:40:52 +02001019 goto out_unregister_ping_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 /* Register the family here so that the init calls below will
1022 * be able to create sockets. (?? is this dangerous ??)
1023 */
David S. Miller8eb55912005-11-11 15:05:47 -08001024 err = sock_register(&inet6_family_ops);
1025 if (err)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001026 goto out_sock_register_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /*
1029 * ipngwg API draft makes clear that the correct semantics
1030 * for TCP and UDP is to consider one TCP and UDP instance
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001031 * in a host available by both INET and INET6 APIs and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 * able to communicate via both network protocols.
1033 */
1034
Daniel Lezcano81c1c172008-01-10 02:48:33 -08001035 err = register_pernet_subsys(&inet6_net_ops);
1036 if (err)
1037 goto register_pernet_fail;
Wang Chen623d1a12008-07-03 12:13:30 +08001038 err = ip6_mr_init();
1039 if (err)
1040 goto ipmr_fail;
WANG Cong15e66802017-03-05 12:34:53 -08001041 err = icmpv6_init();
1042 if (err)
1043 goto icmp_fail;
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08001044 err = ndisc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (err)
1046 goto ndisc_fail;
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08001047 err = igmp6_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (err)
1049 goto igmp_fail;
Cong Wang5f81bd22013-08-31 13:44:30 +08001050
Harald Welte2cc7d572005-08-09 19:42:34 -07001051 err = ipv6_netfilter_init();
1052 if (err)
1053 goto netfilter_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* Create /proc/foo6 entries. */
1055#ifdef CONFIG_PROC_FS
1056 err = -ENOMEM;
1057 if (raw6_proc_init())
1058 goto proc_raw6_fail;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001059 if (udplite6_proc_init())
1060 goto proc_udplite6_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (ipv6_misc_proc_init())
1062 goto proc_misc6_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (if6_proc_init())
1064 goto proc_if6_fail;
1065#endif
Daniel Lezcanoe2fddf52007-12-07 00:44:29 -08001066 err = ip6_route_init();
1067 if (err)
1068 goto ip6_route_fail;
Michal Kubeček2c861cc2013-09-09 21:45:04 +02001069 err = ndisc_late_init();
1070 if (err)
1071 goto ndisc_late_fail;
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -08001072 err = ip6_flowlabel_init();
1073 if (err)
1074 goto ip6_flowlabel_fail;
Jeff Barnhill2384d022018-11-02 20:23:57 +00001075 err = ipv6_anycast_init();
1076 if (err)
1077 goto ipv6_anycast_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 err = addrconf_init();
1079 if (err)
1080 goto addrconf_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /* Init v6 extension headers. */
Daniel Lezcano248b2382007-12-11 02:23:54 -08001083 err = ipv6_exthdrs_init();
1084 if (err)
1085 goto ipv6_exthdrs_fail;
1086
Daniel Lezcano853cbba2007-12-11 02:24:29 -08001087 err = ipv6_frag_init();
1088 if (err)
1089 goto ipv6_frag_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 /* Init v6 transport protocols. */
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001092 err = udpv6_init();
1093 if (err)
1094 goto udpv6_fail;
Herbert Xue2ed4052005-07-05 14:41:20 -07001095
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001096 err = udplitev6_init();
1097 if (err)
1098 goto udplitev6_fail;
1099
Tom Herberta6024562016-04-05 08:22:51 -07001100 err = udpv6_offload_init();
1101 if (err)
1102 goto udpv6_offload_fail;
1103
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001104 err = tcpv6_init();
1105 if (err)
1106 goto tcpv6_fail;
1107
1108 err = ipv6_packet_init();
1109 if (err)
1110 goto ipv6_packet_fail;
Benjamin Thery94911fe2008-03-05 10:45:36 -08001111
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +00001112 err = pingv6_init();
1113 if (err)
1114 goto pingv6_fail;
1115
Huw Daviescb72d382016-06-27 15:02:46 -04001116 err = calipso_init();
1117 if (err)
1118 goto calipso_fail;
1119
David Lebrun915d7e52016-11-08 14:57:40 +01001120 err = seg6_init();
1121 if (err)
1122 goto seg6_fail;
1123
Alexander Aringa7a29f92020-03-27 18:00:22 -04001124 err = rpl_init();
1125 if (err)
1126 goto rpl_fail;
1127
Vlad Yasevich382ed722017-03-28 14:49:16 -04001128 err = igmp6_late_init();
1129 if (err)
1130 goto igmp6_late_err;
1131
Benjamin Thery94911fe2008-03-05 10:45:36 -08001132#ifdef CONFIG_SYSCTL
1133 err = ipv6_sysctl_register();
1134 if (err)
1135 goto sysctl_fail;
1136#endif
Paolo Abenib7d6df52017-04-24 14:18:28 +02001137
1138 /* ensure that ipv6 stubs are visible only after ipv6 is ready */
1139 wmb();
1140 ipv6_stub = &ipv6_stub_impl;
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001141 ipv6_bpf_stub = &ipv6_bpf_stub_impl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142out:
1143 return err;
1144
Benjamin Thery94911fe2008-03-05 10:45:36 -08001145#ifdef CONFIG_SYSCTL
1146sysctl_fail:
Vlad Yasevich382ed722017-03-28 14:49:16 -04001147 igmp6_late_cleanup();
Benjamin Thery94911fe2008-03-05 10:45:36 -08001148#endif
Vlad Yasevich382ed722017-03-28 14:49:16 -04001149igmp6_late_err:
Alexander Aringa7a29f92020-03-27 18:00:22 -04001150 rpl_exit();
1151rpl_fail:
Vlad Yasevich382ed722017-03-28 14:49:16 -04001152 seg6_exit();
David Lebrun915d7e52016-11-08 14:57:40 +01001153seg6_fail:
1154 calipso_exit();
Huw Daviescb72d382016-06-27 15:02:46 -04001155calipso_fail:
1156 pingv6_exit();
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +00001157pingv6_fail:
Vlad Yasevicheca42aa2013-11-16 15:17:24 -05001158 ipv6_packet_cleanup();
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001159ipv6_packet_fail:
1160 tcpv6_exit();
1161tcpv6_fail:
Tom Herberta6024562016-04-05 08:22:51 -07001162 udpv6_offload_exit();
1163udpv6_offload_fail:
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001164 udplitev6_exit();
1165udplitev6_fail:
1166 udpv6_exit();
1167udpv6_fail:
1168 ipv6_frag_exit();
Daniel Lezcano853cbba2007-12-11 02:24:29 -08001169ipv6_frag_fail:
1170 ipv6_exthdrs_exit();
Daniel Lezcano248b2382007-12-11 02:23:54 -08001171ipv6_exthdrs_fail:
1172 addrconf_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173addrconf_fail:
Jeff Barnhill2384d022018-11-02 20:23:57 +00001174 ipv6_anycast_cleanup();
1175ipv6_anycast_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 ip6_flowlabel_cleanup();
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -08001177ip6_flowlabel_fail:
Michal Kubeček2c861cc2013-09-09 21:45:04 +02001178 ndisc_late_cleanup();
1179ndisc_late_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 ip6_route_cleanup();
Daniel Lezcanoe2fddf52007-12-07 00:44:29 -08001181ip6_route_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182#ifdef CONFIG_PROC_FS
1183 if6_proc_exit();
1184proc_if6_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 ipv6_misc_proc_exit();
1186proc_misc6_fail:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001187 udplite6_proc_exit();
1188proc_udplite6_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 raw6_proc_exit();
1190proc_raw6_fail:
1191#endif
Harald Welte2cc7d572005-08-09 19:42:34 -07001192 ipv6_netfilter_fini();
1193netfilter_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 igmp6_cleanup();
1195igmp_fail:
1196 ndisc_cleanup();
1197ndisc_fail:
WANG Cong15e66802017-03-05 12:34:53 -08001198 icmpv6_cleanup();
Sabrina Dubrocaafe49de2018-08-28 13:40:51 +02001199icmp_fail:
1200 ip6_mr_cleanup();
1201ipmr_fail:
1202 unregister_pernet_subsys(&inet6_net_ops);
Daniel Lezcano81c1c172008-01-10 02:48:33 -08001203register_pernet_fail:
David S. Miller8eb55912005-11-11 15:05:47 -08001204 sock_unregister(PF_INET6);
Daniel Lezcanoe2fddf52007-12-07 00:44:29 -08001205 rtnl_unregister_all(PF_INET6);
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001206out_sock_register_fail:
1207 rawv6_exit();
Lorenzo Colitti6d0bfe22013-05-22 20:17:31 +00001208out_unregister_ping_proto:
1209 proto_unregister(&pingv6_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210out_unregister_raw_proto:
1211 proto_unregister(&rawv6_prot);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001212out_unregister_udplite_proto:
1213 proto_unregister(&udplitev6_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214out_unregister_udp_proto:
1215 proto_unregister(&udpv6_prot);
1216out_unregister_tcp_proto:
1217 proto_unregister(&tcpv6_prot);
1218 goto out;
1219}
1220module_init(inet6_init);
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222MODULE_ALIAS_NETPROTO(PF_INET6);