blob: 22ebbb9728397439f6db10be793e7bf7a356202b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PF_INET6 socket protocol family
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Adapted from linux/net/ipv4/af_inet.c
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Fixes:
11 * piggy, Karl Knutson : Socket protocol table
12 * Hideaki YOSHIFUJI : sin6_scope_id support
13 * Arnaldo Melo : check proc_net_create return, cleanups
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
20
21
22#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080023#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/in.h>
28#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/timer.h>
30#include <linux/string.h>
31#include <linux/sockios.h>
32#include <linux/net.h>
33#include <linux/fcntl.h>
34#include <linux/mm.h>
35#include <linux/interrupt.h>
36#include <linux/proc_fs.h>
37#include <linux/stat.h>
38#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <linux/inet.h>
42#include <linux/netdevice.h>
43#include <linux/icmpv6.h>
Harald Welte2cc7d572005-08-09 19:42:34 -070044#include <linux/netfilter_ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <net/ip.h>
47#include <net/ipv6.h>
48#include <net/udp.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080049#include <net/udplite.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <net/tcp.h>
51#include <net/ipip.h>
Lorenzo Colittif7048202013-01-16 22:09:49 +000052#include <net/ping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <net/protocol.h>
54#include <net/inet_common.h>
KOVACS Krisztian1668e012008-10-01 07:33:10 -070055#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/transp_v6.h>
57#include <net/ip6_route.h>
58#include <net/addrconf.h>
59#ifdef CONFIG_IPV6_TUNNEL
60#include <net/ip6_tunnel.h>
61#endif
62
63#include <asm/uaccess.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090064#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Robert Love20386922008-10-15 15:35:44 -040066#ifdef CONFIG_ANDROID_PARANOID_NETWORK
67#include <linux/android_aid.h>
Chia-chi Yehc543f112009-06-30 11:23:04 +080068
69static inline int current_has_network(void)
70{
71 return in_egroup_p(AID_INET) || capable(CAP_NET_RAW);
72}
73#else
74static inline int current_has_network(void)
75{
76 return 1;
77}
Robert Love20386922008-10-15 15:35:44 -040078#endif
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080MODULE_AUTHOR("Cast of dozens");
81MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
82MODULE_LICENSE("GPL");
83
Pavel Emelyanovf1267342007-11-23 21:28:44 +080084/* The inetsw6 table contains everything that inet6_create needs to
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * build a new socket.
86 */
87static struct list_head inetsw6[SOCK_MAX];
88static DEFINE_SPINLOCK(inetsw6_lock);
89
Brian Haley56d417b2009-06-01 03:07:33 -070090struct ipv6_params ipv6_defaults = {
91 .disable_ipv6 = 0,
92 .autoconf = 1,
93};
94
95static int disable_ipv6_mod = 0;
96
97module_param_named(disable, disable_ipv6_mod, int, 0444);
98MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
99
100module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
101MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
102
103module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
104MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
Brian Haleyfe7ca2e2009-03-04 03:18:11 -0800105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
107{
108 const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
109
110 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
111}
112
Eric Paris3f378b62009-11-05 22:18:14 -0800113static int inet6_create(struct net *net, struct socket *sock, int protocol,
114 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct inet_sock *inet;
117 struct ipv6_pinfo *np;
118 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct inet_protosw *answer;
120 struct proto *answer_prot;
121 unsigned char answer_flags;
122 char answer_no_check;
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800123 int try_loading_module = 0;
124 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Robert Love20386922008-10-15 15:35:44 -0400126 if (!current_has_network())
127 return -EACCES;
128
David S. Millerb3da2cf2007-03-23 11:40:27 -0700129 if (sock->type != SOCK_RAW &&
130 sock->type != SOCK_DGRAM &&
131 !inet_ehash_secret)
132 build_ehash_secret();
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* Look for the requested type/protocol pair. */
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800135lookup_protocol:
136 err = -ESOCKTNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 rcu_read_lock();
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700138 list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700140 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* Check the non-wild match. */
142 if (protocol == answer->protocol) {
143 if (protocol != IPPROTO_IP)
144 break;
145 } else {
146 /* Check for the two wild cases. */
147 if (IPPROTO_IP == protocol) {
148 protocol = answer->protocol;
149 break;
150 }
151 if (IPPROTO_IP == answer->protocol)
152 break;
153 }
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800154 err = -EPROTONOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156
Paul E. McKenney696adfe2008-07-25 01:45:34 -0700157 if (err) {
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800158 if (try_loading_module < 2) {
159 rcu_read_unlock();
160 /*
161 * Be more specific, e.g. net-pf-10-proto-132-type-1
162 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
163 */
164 if (++try_loading_module == 1)
165 request_module("net-pf-%d-proto-%d-type-%d",
166 PF_INET6, protocol, sock->type);
167 /*
168 * Fall back to generic, e.g. net-pf-10-proto-132
169 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
170 */
171 else
172 request_module("net-pf-%d-proto-%d",
173 PF_INET6, protocol);
174 goto lookup_protocol;
175 } else
176 goto out_rcu_unlock;
177 }
178
179 err = -EPERM;
Chia-chi Yehc543f112009-06-30 11:23:04 +0800180 if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto out_rcu_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 sock->ops = answer->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 answer_prot = answer->prot;
185 answer_no_check = answer->no_check;
186 answer_flags = answer->flags;
187 rcu_read_unlock();
188
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700189 WARN_ON(answer_prot->slab == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800191 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700192 sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (sk == NULL)
194 goto out;
195
196 sock_init_data(sock, sk);
197
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800198 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 sk->sk_no_check = answer_no_check;
200 if (INET_PROTOSW_REUSE & answer_flags)
201 sk->sk_reuse = 1;
202
203 inet = inet_sk(sk);
Paul Moore469de9b2007-01-09 14:37:06 -0800204 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (SOCK_RAW == sock->type) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000207 inet->inet_num = protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (IPPROTO_RAW == protocol)
209 inet->hdrincl = 1;
210 }
211
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700212 sk->sk_destruct = inet_sock_destruct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 sk->sk_family = PF_INET6;
214 sk->sk_protocol = protocol;
215
216 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
217
218 inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
219 np->hop_limit = -1;
David S. Millerf935aa92010-05-03 23:42:27 -0700220 np->mcast_hops = IPV6_DEFAULT_MCASTHOPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 np->mc_loop = 1;
222 np->pmtudisc = IPV6_PMTUDISC_WANT;
Pavel Emelyanov2e761e02008-06-09 15:53:30 -0700223 np->ipv6only = net->ipv6.sysctl.bindv6only;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* Init the ipv4 part of the socket since we can have sockets
226 * using v6 API for ipv4.
227 */
228 inet->uc_ttl = -1;
229
230 inet->mc_loop = 1;
231 inet->mc_ttl = 1;
232 inet->mc_index = 0;
233 inet->mc_list = NULL;
Jiri Benc4c507d22012-02-09 09:35:49 +0000234 inet->rcv_tos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (ipv4_config.no_pmtu_disc)
237 inet->pmtudisc = IP_PMTUDISC_DONT;
238 else
239 inet->pmtudisc = IP_PMTUDISC_WANT;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900240 /*
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700241 * Increment only the relevant sk_prot->socks debug field, this changes
242 * the previous behaviour of incrementing both the equivalent to
243 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
244 *
245 * This allows better debug granularity as we'll know exactly how many
246 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
247 * transport protocol socks. -acme
248 */
249 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000251 if (inet->inet_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* It assumes that any protocol which allows
253 * the user to assign a number at socket
254 * creation time automatically shares.
255 */
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000256 inet->inet_sport = htons(inet->inet_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 sk->sk_prot->hash(sk);
258 }
259 if (sk->sk_prot->init) {
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800260 err = sk->sk_prot->init(sk);
261 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 sk_common_release(sk);
263 goto out;
264 }
265 }
266out:
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800267 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268out_rcu_unlock:
269 rcu_read_unlock();
270 goto out;
271}
272
273
274/* bind for INET6 API */
275int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
276{
277 struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
278 struct sock *sk = sock->sk;
279 struct inet_sock *inet = inet_sk(sk);
280 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900281 struct net *net = sock_net(sk);
Al Virofd683222006-09-26 22:17:51 -0700282 __be32 v4addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 unsigned short snum;
284 int addr_type = 0;
285 int err = 0;
286
287 /* If the socket has its own bind function then use it. */
288 if (sk->sk_prot->bind)
289 return sk->sk_prot->bind(sk, uaddr, addr_len);
290
291 if (addr_len < SIN6_LEN_RFC2133)
292 return -EINVAL;
Marcus Meissner5a079c32011-06-06 06:00:07 +0000293
294 if (addr->sin6_family != AF_INET6)
Marcus Meissnerc349a522011-07-04 01:30:29 +0000295 return -EAFNOSUPPORT;
Marcus Meissner5a079c32011-06-06 06:00:07 +0000296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 addr_type = ipv6_addr_type(&addr->sin6_addr);
298 if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
299 return -EINVAL;
300
301 snum = ntohs(addr->sin6_port);
302 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
303 return -EACCES;
304
305 lock_sock(sk);
306
307 /* Check these errors (active socket, double bind). */
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000308 if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 err = -EINVAL;
310 goto out;
311 }
312
313 /* Check if the address belongs to the host. */
314 if (addr_type == IPV6_ADDR_MAPPED) {
Vlad Yasevich63d99502009-03-24 16:24:50 +0000315 int chk_addr_ret;
316
Vlad Yasevich783ed5a2009-03-24 16:24:48 +0000317 /* Binding to v4-mapped address on a v6-only socket
318 * makes no sense
319 */
320 if (np->ipv6only) {
321 err = -EINVAL;
322 goto out;
323 }
Vlad Yasevich63d99502009-03-24 16:24:50 +0000324
Uwe Kleine-Königa34f0b32010-12-10 14:55:42 +0100325 /* Reproduce AF_INET checks to make the bindings consistent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 v4addr = addr->sin6_addr.s6_addr32[3];
Vlad Yasevich63d99502009-03-24 16:24:50 +0000327 chk_addr_ret = inet_addr_type(net, v4addr);
328 if (!sysctl_ip_nonlocal_bind &&
329 !(inet->freebind || inet->transparent) &&
330 v4addr != htonl(INADDR_ANY) &&
331 chk_addr_ret != RTN_LOCAL &&
332 chk_addr_ret != RTN_MULTICAST &&
Bruno Prémontca6982b2009-08-23 19:06:28 -0700333 chk_addr_ret != RTN_BROADCAST) {
334 err = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 goto out;
Bruno Prémontca6982b2009-08-23 19:06:28 -0700336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 } else {
338 if (addr_type != IPV6_ADDR_ANY) {
339 struct net_device *dev = NULL;
340
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100341 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (addr_type & IPV6_ADDR_LINKLOCAL) {
343 if (addr_len >= sizeof(struct sockaddr_in6) &&
344 addr->sin6_scope_id) {
345 /* Override any existing binding, if another one
346 * is supplied by user.
347 */
348 sk->sk_bound_dev_if = addr->sin6_scope_id;
349 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /* Binding to link-local address requires an interface */
352 if (!sk->sk_bound_dev_if) {
353 err = -EINVAL;
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100354 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100356 dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!dev) {
358 err = -ENODEV;
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100359 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 }
362
363 /* ipv4 addr of the socket is invalid. Only the
364 * unspecified and mapped address have a v4 equivalent.
365 */
366 v4addr = LOOPBACK4_IPV6;
367 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
Maciej Żenczykowskif74024d2011-11-07 14:57:21 +0000368 if (!(inet->freebind || inet->transparent) &&
Balazs Scheidler0a513f6a2010-10-21 16:10:03 +0200369 !ipv6_chk_addr(net, &addr->sin6_addr,
Daniel Lezcanobfeade02008-01-10 22:43:18 -0800370 dev, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 err = -EADDRNOTAVAIL;
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100372 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374 }
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100375 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377 }
378
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000379 inet->inet_rcv_saddr = v4addr;
380 inet->inet_saddr = v4addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000382 np->rcv_saddr = addr->sin6_addr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!(addr_type & IPV6_ADDR_MULTICAST))
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000385 np->saddr = addr->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* Make sure we are allowed to bind here. */
388 if (sk->sk_prot->get_port(sk, snum)) {
389 inet_reset_saddr(sk);
390 err = -EADDRINUSE;
391 goto out;
392 }
393
Vlad Yasevich0f8d3c72009-03-24 16:24:49 +0000394 if (addr_type != IPV6_ADDR_ANY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
Vlad Yasevich0f8d3c72009-03-24 16:24:49 +0000396 if (addr_type != IPV6_ADDR_MAPPED)
397 np->ipv6only = 1;
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (snum)
400 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000401 inet->inet_sport = htons(inet->inet_num);
402 inet->inet_dport = 0;
403 inet->inet_daddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404out:
405 release_sock(sk);
406 return err;
Eric Dumazet16ba5e82009-11-02 12:10:39 +0100407out_unlock:
408 rcu_read_unlock();
409 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900412EXPORT_SYMBOL(inet6_bind);
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414int inet6_release(struct socket *sock)
415{
416 struct sock *sk = sock->sk;
417
418 if (sk == NULL)
419 return -EINVAL;
420
421 /* Free mc lists */
422 ipv6_sock_mc_close(sk);
423
424 /* Free ac lists */
425 ipv6_sock_ac_close(sk);
426
427 return inet_release(sock);
428}
429
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900430EXPORT_SYMBOL(inet6_release);
431
Brian Haley7d06b2e2008-06-14 17:04:49 -0700432void inet6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct ipv6_pinfo *np = inet6_sk(sk);
435 struct sk_buff *skb;
436 struct ipv6_txoptions *opt;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /* Release rx options */
439
440 if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
441 kfree_skb(skb);
442
Brian Haley4b340ae2010-04-23 11:26:09 +0000443 if ((skb = xchg(&np->rxpmtu, NULL)) != NULL)
444 kfree_skb(skb);
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /* Free flowlabels */
447 fl6_free_socklist(sk);
448
449 /* Free tx options */
450
451 if ((opt = xchg(&np->opt, NULL)) != NULL)
452 sock_kfree_s(sk, opt, opt->tot_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800455EXPORT_SYMBOL_GPL(inet6_destroy_sock);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * This does both peername and sockname.
459 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
462 int *uaddr_len, int peer)
463{
464 struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
465 struct sock *sk = sock->sk;
466 struct inet_sock *inet = inet_sk(sk);
467 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 sin->sin6_family = AF_INET6;
470 sin->sin6_flowinfo = 0;
471 sin->sin6_scope_id = 0;
472 if (peer) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000473 if (!inet->inet_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return -ENOTCONN;
475 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
476 peer == 1)
477 return -ENOTCONN;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000478 sin->sin6_port = inet->inet_dport;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000479 sin->sin6_addr = np->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (np->sndflow)
481 sin->sin6_flowinfo = np->flow_label;
482 } else {
483 if (ipv6_addr_any(&np->rcv_saddr))
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000484 sin->sin6_addr = np->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 else
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000486 sin->sin6_addr = np->rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000488 sin->sin6_port = inet->inet_sport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
491 sin->sin6_scope_id = sk->sk_bound_dev_if;
492 *uaddr_len = sizeof(*sin);
Eric Dumazeta02cec22010-09-22 20:43:57 +0000493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900496EXPORT_SYMBOL(inet6_getname);
497
Robert Love2365d052008-05-12 17:08:29 -0400498int inet6_killaddr_ioctl(struct net *net, void __user *arg) {
499 struct in6_ifreq ireq;
500 struct sockaddr_in6 sin6;
501
502 if (!capable(CAP_NET_ADMIN))
503 return -EACCES;
504
505 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
506 return -EFAULT;
507
508 sin6.sin6_family = AF_INET6;
509 sin6.sin6_addr = ireq.ifr6_addr;
510 return tcp_nuke_addr(net, (struct sockaddr *) &sin6);
511}
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
514{
515 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900516 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900518 switch(cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 {
520 case SIOCGSTAMP:
521 return sock_get_timestamp(sk, (struct timeval __user *)arg);
522
Eric Dumazetae40eb12007-03-18 17:33:16 -0700523 case SIOCGSTAMPNS:
524 return sock_get_timestampns(sk, (struct timespec __user *)arg);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 case SIOCADDRT:
527 case SIOCDELRT:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900528
Eric Dumazeta02cec22010-09-22 20:43:57 +0000529 return ipv6_route_ioctl(net, cmd, (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 case SIOCSIFADDR:
Daniel Lezcanoaf284932008-03-05 10:46:57 -0800532 return addrconf_add_ifaddr(net, (void __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 case SIOCDIFADDR:
Daniel Lezcanoaf284932008-03-05 10:46:57 -0800534 return addrconf_del_ifaddr(net, (void __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 case SIOCSIFDSTADDR:
Daniel Lezcanoaf284932008-03-05 10:46:57 -0800536 return addrconf_set_dstaddr(net, (void __user *) arg);
Robert Love2365d052008-05-12 17:08:29 -0400537 case SIOCKILLADDR:
538 return inet6_killaddr_ioctl(net, (void __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -0800540 if (!sk->sk_prot->ioctl)
541 return -ENOIOCTLCMD;
542 return sk->sk_prot->ioctl(sk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544 /*NOTREACHED*/
Eric Dumazeta02cec22010-09-22 20:43:57 +0000545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900548EXPORT_SYMBOL(inet6_ioctl);
549
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800550const struct proto_ops inet6_stream_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800551 .family = PF_INET6,
552 .owner = THIS_MODULE,
553 .release = inet6_release,
554 .bind = inet6_bind,
555 .connect = inet_stream_connect, /* ok */
556 .socketpair = sock_no_socketpair, /* a do nothing */
557 .accept = inet_accept, /* ok */
558 .getname = inet6_getname,
559 .poll = tcp_poll, /* ok */
560 .ioctl = inet6_ioctl, /* must change */
561 .listen = inet_listen, /* ok */
562 .shutdown = inet_shutdown, /* ok */
563 .setsockopt = sock_common_setsockopt, /* ok */
564 .getsockopt = sock_common_getsockopt, /* ok */
Changli Gao7ba42912010-07-10 20:41:55 +0000565 .sendmsg = inet_sendmsg, /* ok */
566 .recvmsg = inet_recvmsg, /* ok */
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800567 .mmap = sock_no_mmap,
Changli Gao7ba42912010-07-10 20:41:55 +0000568 .sendpage = inet_sendpage,
Jens Axboea0974dd2007-11-06 23:31:58 -0800569 .splice_read = tcp_splice_read,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800570#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800571 .compat_setsockopt = compat_sock_common_setsockopt,
572 .compat_getsockopt = compat_sock_common_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800573#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574};
575
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800576const struct proto_ops inet6_dgram_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800577 .family = PF_INET6,
578 .owner = THIS_MODULE,
579 .release = inet6_release,
580 .bind = inet6_bind,
581 .connect = inet_dgram_connect, /* ok */
582 .socketpair = sock_no_socketpair, /* a do nothing */
583 .accept = sock_no_accept, /* a do nothing */
584 .getname = inet6_getname,
585 .poll = udp_poll, /* ok */
586 .ioctl = inet6_ioctl, /* must change */
587 .listen = sock_no_listen, /* ok */
588 .shutdown = inet_shutdown, /* ok */
589 .setsockopt = sock_common_setsockopt, /* ok */
590 .getsockopt = sock_common_getsockopt, /* ok */
591 .sendmsg = inet_sendmsg, /* ok */
Changli Gao7ba42912010-07-10 20:41:55 +0000592 .recvmsg = inet_recvmsg, /* ok */
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800593 .mmap = sock_no_mmap,
594 .sendpage = sock_no_sendpage,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800595#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800596 .compat_setsockopt = compat_sock_common_setsockopt,
597 .compat_getsockopt = compat_sock_common_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800598#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599};
600
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000601static const struct net_proto_family inet6_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 .family = PF_INET6,
603 .create = inet6_create,
604 .owner = THIS_MODULE,
605};
606
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800607int inet6_register_protosw(struct inet_protosw *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct list_head *lh;
610 struct inet_protosw *answer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct list_head *last_perm;
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800612 int protocol = p->protocol;
613 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 spin_lock_bh(&inetsw6_lock);
616
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800617 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (p->type >= SOCK_MAX)
619 goto out_illegal;
620
621 /* If we are trying to override a permanent protocol, bail. */
622 answer = NULL;
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800623 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 last_perm = &inetsw6[p->type];
625 list_for_each(lh, &inetsw6[p->type]) {
626 answer = list_entry(lh, struct inet_protosw, list);
627
628 /* Check only the non-wild match. */
629 if (INET_PROTOSW_PERMANENT & answer->flags) {
630 if (protocol == answer->protocol)
631 break;
632 last_perm = lh;
633 }
634
635 answer = NULL;
636 }
637 if (answer)
638 goto out_permanent;
639
640 /* Add the new entry after the last permanent entry if any, so that
641 * the new entry does not override a permanent entry when matched with
642 * a wild-card protocol. But it is allowed to override any existing
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900643 * non-permanent entry. This means that when we remove this entry, the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * system automatically returns to the old behavior.
645 */
646 list_add_rcu(&p->list, last_perm);
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800647 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648out:
649 spin_unlock_bh(&inetsw6_lock);
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652out_permanent:
653 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
654 protocol);
655 goto out;
656
657out_illegal:
658 printk(KERN_ERR
659 "Ignoring attempt to register invalid socket type %d.\n",
660 p->type);
661 goto out;
662}
663
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900664EXPORT_SYMBOL(inet6_register_protosw);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666void
667inet6_unregister_protosw(struct inet_protosw *p)
668{
669 if (INET_PROTOSW_PERMANENT & p->flags) {
670 printk(KERN_ERR
671 "Attempt to unregister permanent protocol %d.\n",
672 p->protocol);
673 } else {
674 spin_lock_bh(&inetsw6_lock);
675 list_del_rcu(&p->list);
676 spin_unlock_bh(&inetsw6_lock);
677
678 synchronize_net();
679 }
680}
681
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900682EXPORT_SYMBOL(inet6_unregister_protosw);
683
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800684int inet6_sk_rebuild_header(struct sock *sk)
685{
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800686 struct ipv6_pinfo *np = inet6_sk(sk);
David S. Miller68d0c6d2011-03-01 13:19:07 -0800687 struct dst_entry *dst;
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800688
689 dst = __sk_dst_check(sk, np->dst_cookie);
690
691 if (dst == NULL) {
692 struct inet_sock *inet = inet_sk(sk);
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000693 struct in6_addr *final_p, final;
David S. Miller4c9483b2011-03-12 16:22:43 -0500694 struct flowi6 fl6;
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800695
David S. Miller4c9483b2011-03-12 16:22:43 -0500696 memset(&fl6, 0, sizeof(fl6));
697 fl6.flowi6_proto = sk->sk_protocol;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000698 fl6.daddr = np->daddr;
699 fl6.saddr = np->saddr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500700 fl6.flowlabel = np->flow_label;
701 fl6.flowi6_oif = sk->sk_bound_dev_if;
702 fl6.flowi6_mark = sk->sk_mark;
David S. Miller1958b852011-03-12 16:36:19 -0500703 fl6.fl6_dport = inet->inet_dport;
704 fl6.fl6_sport = inet->inet_sport;
David S. Miller4c9483b2011-03-12 16:22:43 -0500705 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800706
David S. Miller4c9483b2011-03-12 16:22:43 -0500707 final_p = fl6_update_dst(&fl6, np->opt, &final);
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800708
David S. Miller4c9483b2011-03-12 16:22:43 -0500709 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);
David S. Miller68d0c6d2011-03-01 13:19:07 -0800710 if (IS_ERR(dst)) {
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800711 sk->sk_route_caps = 0;
David S. Miller68d0c6d2011-03-01 13:19:07 -0800712 sk->sk_err_soft = -PTR_ERR(dst);
713 return PTR_ERR(dst);
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800714 }
715
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700716 __ip6_dst_store(sk, dst, NULL, NULL);
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800717 }
718
719 return 0;
720}
721
722EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
723
Arnaldo Carvalho de Melo399c07d2005-12-13 23:24:28 -0800724int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
725{
726 struct ipv6_pinfo *np = inet6_sk(sk);
727 struct inet6_skb_parm *opt = IP6CB(skb);
728
729 if (np->rxopt.all) {
730 if ((opt->hop && (np->rxopt.bits.hopopts ||
731 np->rxopt.bits.ohopopts)) ||
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700732 ((IPV6_FLOWINFO_MASK &
733 *(__be32 *)skb_network_header(skb)) &&
Arnaldo Carvalho de Melo399c07d2005-12-13 23:24:28 -0800734 np->rxopt.bits.rxflow) ||
735 (opt->srcrt && (np->rxopt.bits.srcrt ||
736 np->rxopt.bits.osrcrt)) ||
737 ((opt->dst1 || opt->dst0) &&
738 (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
739 return 1;
740 }
741 return 0;
742}
743
744EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
745
Herbert Xu787e9202009-01-08 10:40:57 -0800746static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900747{
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000748 const struct inet6_protocol *ops = NULL;
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900749
750 for (;;) {
751 struct ipv6_opt_hdr *opth;
752 int len;
753
754 if (proto != NEXTHDR_HOP) {
755 ops = rcu_dereference(inet6_protos[proto]);
756
757 if (unlikely(!ops))
758 break;
759
760 if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
761 break;
762 }
763
764 if (unlikely(!pskb_may_pull(skb, 8)))
765 break;
766
767 opth = (void *)skb->data;
768 len = ipv6_optlen(opth);
769
770 if (unlikely(!pskb_may_pull(skb, len)))
771 break;
772
773 proto = opth->nexthdr;
774 __skb_pull(skb, len);
775 }
776
Herbert Xu787e9202009-01-08 10:40:57 -0800777 return proto;
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900778}
779
780static int ipv6_gso_send_check(struct sk_buff *skb)
781{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000782 const struct ipv6hdr *ipv6h;
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000783 const struct inet6_protocol *ops;
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900784 int err = -EINVAL;
785
786 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
787 goto out;
788
789 ipv6h = ipv6_hdr(skb);
790 __skb_pull(skb, sizeof(*ipv6h));
791 err = -EPROTONOSUPPORT;
792
793 rcu_read_lock();
Herbert Xu787e9202009-01-08 10:40:57 -0800794 ops = rcu_dereference(inet6_protos[
795 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
796
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900797 if (likely(ops && ops->gso_send_check)) {
798 skb_reset_transport_header(skb);
799 err = ops->gso_send_check(skb);
800 }
801 rcu_read_unlock();
802
803out:
804 return err;
805}
806
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000807static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
808 netdev_features_t features)
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900809{
810 struct sk_buff *segs = ERR_PTR(-EINVAL);
811 struct ipv6hdr *ipv6h;
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000812 const struct inet6_protocol *ops;
Sridhar Samudralaba735422009-07-09 08:10:04 +0000813 int proto;
814 struct frag_hdr *fptr;
815 unsigned int unfrag_ip6hlen;
816 u8 *prevhdr;
817 int offset = 0;
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900818
819 if (!(features & NETIF_F_V6_CSUM))
820 features &= ~NETIF_F_SG;
821
822 if (unlikely(skb_shinfo(skb)->gso_type &
823 ~(SKB_GSO_UDP |
824 SKB_GSO_DODGY |
825 SKB_GSO_TCP_ECN |
826 SKB_GSO_TCPV6 |
827 0)))
828 goto out;
829
830 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
831 goto out;
832
833 ipv6h = ipv6_hdr(skb);
834 __skb_pull(skb, sizeof(*ipv6h));
835 segs = ERR_PTR(-EPROTONOSUPPORT);
836
Sridhar Samudralaba735422009-07-09 08:10:04 +0000837 proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900838 rcu_read_lock();
Sridhar Samudralaba735422009-07-09 08:10:04 +0000839 ops = rcu_dereference(inet6_protos[proto]);
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900840 if (likely(ops && ops->gso_segment)) {
841 skb_reset_transport_header(skb);
842 segs = ops->gso_segment(skb, features);
843 }
844 rcu_read_unlock();
845
Tobias Klauser376d9402010-12-09 04:37:48 +0000846 if (IS_ERR(segs))
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900847 goto out;
848
849 for (skb = segs; skb; skb = skb->next) {
850 ipv6h = ipv6_hdr(skb);
851 ipv6h->payload_len = htons(skb->len - skb->mac_len -
852 sizeof(*ipv6h));
Sridhar Samudralaba735422009-07-09 08:10:04 +0000853 if (proto == IPPROTO_UDP) {
854 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
855 fptr = (struct frag_hdr *)(skb_network_header(skb) +
856 unfrag_ip6hlen);
857 fptr->frag_off = htons(offset);
858 if (skb->next != NULL)
859 fptr->frag_off |= htons(IP6_MF);
860 offset += (ntohs(ipv6h->payload_len) -
861 sizeof(struct frag_hdr));
862 }
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900863 }
864
865out:
866 return segs;
867}
868
Herbert Xu787e9202009-01-08 10:40:57 -0800869struct ipv6_gro_cb {
870 struct napi_gro_cb napi;
871 int proto;
872};
873
874#define IPV6_GRO_CB(skb) ((struct ipv6_gro_cb *)(skb)->cb)
875
876static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
877 struct sk_buff *skb)
878{
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000879 const struct inet6_protocol *ops;
Herbert Xu787e9202009-01-08 10:40:57 -0800880 struct sk_buff **pp = NULL;
881 struct sk_buff *p;
882 struct ipv6hdr *iph;
883 unsigned int nlen;
Herbert Xua5b1cf22009-05-26 18:50:28 +0000884 unsigned int hlen;
885 unsigned int off;
Herbert Xu787e9202009-01-08 10:40:57 -0800886 int flush = 1;
887 int proto;
Herbert Xuebad18e2009-01-17 19:46:16 +0000888 __wsum csum;
Herbert Xu787e9202009-01-08 10:40:57 -0800889
Herbert Xua5b1cf22009-05-26 18:50:28 +0000890 off = skb_gro_offset(skb);
891 hlen = off + sizeof(*iph);
892 iph = skb_gro_header_fast(skb, off);
893 if (skb_gro_header_hard(skb, hlen)) {
894 iph = skb_gro_header_slow(skb, hlen, off);
895 if (unlikely(!iph))
896 goto out;
897 }
Herbert Xu787e9202009-01-08 10:40:57 -0800898
Herbert Xu86911732009-01-29 14:19:50 +0000899 skb_gro_pull(skb, sizeof(*iph));
900 skb_set_transport_header(skb, skb_gro_offset(skb));
Herbert Xu787e9202009-01-08 10:40:57 -0800901
Herbert Xu86911732009-01-29 14:19:50 +0000902 flush += ntohs(iph->payload_len) != skb_gro_len(skb);
Herbert Xu787e9202009-01-08 10:40:57 -0800903
904 rcu_read_lock();
Herbert Xu86911732009-01-29 14:19:50 +0000905 proto = iph->nexthdr;
Herbert Xu787e9202009-01-08 10:40:57 -0800906 ops = rcu_dereference(inet6_protos[proto]);
Herbert Xu86911732009-01-29 14:19:50 +0000907 if (!ops || !ops->gro_receive) {
908 __pskb_pull(skb, skb_gro_offset(skb));
909 proto = ipv6_gso_pull_exthdrs(skb, proto);
910 skb_gro_pull(skb, -skb_transport_offset(skb));
911 skb_reset_transport_header(skb);
912 __skb_push(skb, skb_gro_offset(skb));
913
Yan, Zhengcdaf5572011-10-08 22:34:35 +0000914 ops = rcu_dereference(inet6_protos[proto]);
Herbert Xu86911732009-01-29 14:19:50 +0000915 if (!ops || !ops->gro_receive)
916 goto out_unlock;
917
918 iph = ipv6_hdr(skb);
919 }
920
921 IPV6_GRO_CB(skb)->proto = proto;
Herbert Xu787e9202009-01-08 10:40:57 -0800922
923 flush--;
Herbert Xu787e9202009-01-08 10:40:57 -0800924 nlen = skb_network_header_len(skb);
925
926 for (p = *head; p; p = p->next) {
927 struct ipv6hdr *iph2;
928
929 if (!NAPI_GRO_CB(p)->same_flow)
930 continue;
931
932 iph2 = ipv6_hdr(p);
933
934 /* All fields must match except length. */
935 if (nlen != skb_network_header_len(p) ||
936 memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
937 memcmp(&iph->nexthdr, &iph2->nexthdr,
938 nlen - offsetof(struct ipv6hdr, nexthdr))) {
939 NAPI_GRO_CB(p)->same_flow = 0;
940 continue;
941 }
942
943 NAPI_GRO_CB(p)->flush |= flush;
944 }
945
946 NAPI_GRO_CB(skb)->flush |= flush;
947
Herbert Xuebad18e2009-01-17 19:46:16 +0000948 csum = skb->csum;
949 skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
950
Herbert Xu787e9202009-01-08 10:40:57 -0800951 pp = ops->gro_receive(head, skb);
952
Herbert Xuebad18e2009-01-17 19:46:16 +0000953 skb->csum = csum;
954
Herbert Xu787e9202009-01-08 10:40:57 -0800955out_unlock:
956 rcu_read_unlock();
957
958out:
959 NAPI_GRO_CB(skb)->flush |= flush;
960
961 return pp;
962}
963
964static int ipv6_gro_complete(struct sk_buff *skb)
965{
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000966 const struct inet6_protocol *ops;
Herbert Xu787e9202009-01-08 10:40:57 -0800967 struct ipv6hdr *iph = ipv6_hdr(skb);
968 int err = -ENOSYS;
969
970 iph->payload_len = htons(skb->len - skb_network_offset(skb) -
971 sizeof(*iph));
972
973 rcu_read_lock();
974 ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
975 if (WARN_ON(!ops || !ops->gro_complete))
976 goto out_unlock;
977
978 err = ops->gro_complete(skb);
979
980out_unlock:
981 rcu_read_unlock();
982
983 return err;
984}
985
Stephen Hemminger7546dd92009-03-09 08:18:29 +0000986static struct packet_type ipv6_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -0800987 .type = cpu_to_be16(ETH_P_IPV6),
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900988 .func = ipv6_rcv,
989 .gso_send_check = ipv6_gso_send_check,
990 .gso_segment = ipv6_gso_segment,
Herbert Xu787e9202009-01-08 10:40:57 -0800991 .gro_receive = ipv6_gro_receive,
992 .gro_complete = ipv6_gro_complete,
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900993};
994
995static int __init ipv6_packet_init(void)
996{
997 dev_add_pack(&ipv6_packet_type);
998 return 0;
999}
1000
1001static void ipv6_packet_cleanup(void)
1002{
1003 dev_remove_pack(&ipv6_packet_type);
1004}
1005
Denis V. Luneve43291c2008-10-07 14:48:53 -07001006static int __net_init ipv6_init_mibs(struct net *net)
1007{
Tejun Heo7d720c32010-02-16 15:20:26 +00001008 if (snmp_mib_init((void __percpu **)net->mib.udp_stats_in6,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001009 sizeof(struct udp_mib),
1010 __alignof__(struct udp_mib)) < 0)
Denis V. Lunev0c7ed672008-10-07 14:49:36 -07001011 return -ENOMEM;
Tejun Heo7d720c32010-02-16 15:20:26 +00001012 if (snmp_mib_init((void __percpu **)net->mib.udplite_stats_in6,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001013 sizeof(struct udp_mib),
1014 __alignof__(struct udp_mib)) < 0)
Denis V. Lunevbe713a42008-10-07 14:50:06 -07001015 goto err_udplite_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001016 if (snmp_mib_init((void __percpu **)net->mib.ipv6_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001017 sizeof(struct ipstats_mib),
1018 __alignof__(struct ipstats_mib)) < 0)
Denis V. Lunev9261e532008-10-08 10:36:03 -07001019 goto err_ip_mib;
Tejun Heo7d720c32010-02-16 15:20:26 +00001020 if (snmp_mib_init((void __percpu **)net->mib.icmpv6_statistics,
Eric Dumazet1823e4c82010-06-22 20:58:41 +00001021 sizeof(struct icmpv6_mib),
1022 __alignof__(struct icmpv6_mib)) < 0)
Denis V. Lunev9261e532008-10-08 10:36:03 -07001023 goto err_icmp_mib;
Eric Dumazet2a244442011-11-13 01:24:04 +00001024 net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
1025 GFP_KERNEL);
1026 if (!net->mib.icmpv6msg_statistics)
Denis V. Lunev9261e532008-10-08 10:36:03 -07001027 goto err_icmpmsg_mib;
Denis V. Luneve43291c2008-10-07 14:48:53 -07001028 return 0;
Denis V. Lunevbe713a42008-10-07 14:50:06 -07001029
Denis V. Lunev9261e532008-10-08 10:36:03 -07001030err_icmpmsg_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001031 snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
Denis V. Lunev9261e532008-10-08 10:36:03 -07001032err_icmp_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001033 snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
Denis V. Lunev9261e532008-10-08 10:36:03 -07001034err_ip_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001035 snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
Denis V. Lunevbe713a42008-10-07 14:50:06 -07001036err_udplite_mib:
Tejun Heo7d720c32010-02-16 15:20:26 +00001037 snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
Denis V. Lunevbe713a42008-10-07 14:50:06 -07001038 return -ENOMEM;
Denis V. Luneve43291c2008-10-07 14:48:53 -07001039}
1040
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001041static void ipv6_cleanup_mibs(struct net *net)
Denis V. Luneve43291c2008-10-07 14:48:53 -07001042{
Tejun Heo7d720c32010-02-16 15:20:26 +00001043 snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
1044 snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
1045 snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
1046 snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
Eric Dumazet2a244442011-11-13 01:24:04 +00001047 kfree(net->mib.icmpv6msg_statistics);
Denis V. Luneve43291c2008-10-07 14:48:53 -07001048}
1049
Alexey Dobriyane7dc8492008-10-13 18:54:07 -07001050static int __net_init inet6_net_init(struct net *net)
Daniel Lezcano81c1c172008-01-10 02:48:33 -08001051{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001052 int err = 0;
1053
Daniel Lezcano99bc9c42008-01-10 02:54:53 -08001054 net->ipv6.sysctl.bindv6only = 0;
Daniel Lezcano41a76902008-01-10 03:02:40 -08001055 net->ipv6.sysctl.icmpv6_time = 1*HZ;
Daniel Lezcanoe71e0342008-01-10 02:56:03 -08001056
Denis V. Luneve43291c2008-10-07 14:48:53 -07001057 err = ipv6_init_mibs(net);
1058 if (err)
1059 return err;
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001060#ifdef CONFIG_PROC_FS
1061 err = udp6_proc_init(net);
1062 if (err)
1063 goto out;
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07001064 err = tcp6_proc_init(net);
1065 if (err)
1066 goto proc_tcp6_fail;
Daniel Lezcano6ab57e72008-03-26 16:52:32 -07001067 err = ac6_proc_init(net);
1068 if (err)
1069 goto proc_ac6_fail;
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001070#endif
1071 return err;
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07001072
1073#ifdef CONFIG_PROC_FS
Daniel Lezcano6ab57e72008-03-26 16:52:32 -07001074proc_ac6_fail:
1075 tcp6_proc_exit(net);
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07001076proc_tcp6_fail:
1077 udp6_proc_exit(net);
Denis V. Luneve43291c2008-10-07 14:48:53 -07001078out:
1079 ipv6_cleanup_mibs(net);
1080 return err;
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07001081#endif
Daniel Lezcano81c1c172008-01-10 02:48:33 -08001082}
1083
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001084static void __net_exit inet6_net_exit(struct net *net)
Daniel Lezcano81c1c172008-01-10 02:48:33 -08001085{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001086#ifdef CONFIG_PROC_FS
1087 udp6_proc_exit(net);
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07001088 tcp6_proc_exit(net);
Daniel Lezcano6ab57e72008-03-26 16:52:32 -07001089 ac6_proc_exit(net);
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -07001090#endif
Denis V. Luneve43291c2008-10-07 14:48:53 -07001091 ipv6_cleanup_mibs(net);
Daniel Lezcano81c1c172008-01-10 02:48:33 -08001092}
1093
1094static struct pernet_operations inet6_net_ops = {
1095 .init = inet6_net_init,
1096 .exit = inet6_net_exit,
1097};
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099static int __init inet6_init(void)
1100{
1101 struct sk_buff *dummy_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001102 struct list_head *r;
Brian Haleyfe7ca2e2009-03-04 03:18:11 -08001103 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
YOSHIFUJI Hideakief047f52006-09-01 00:29:06 -07001105 BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
1106
Brian Haleyfe7ca2e2009-03-04 03:18:11 -08001107 /* Register the socket-side information for inet6_create. */
1108 for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1109 INIT_LIST_HEAD(r);
1110
Brian Haley56d417b2009-06-01 03:07:33 -07001111 if (disable_ipv6_mod) {
Brian Haleyfe7ca2e2009-03-04 03:18:11 -08001112 printk(KERN_INFO
1113 "IPv6: Loaded, but administratively disabled, "
1114 "reboot required to enable\n");
1115 goto out;
1116 }
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 err = proto_register(&tcpv6_prot, 1);
1119 if (err)
1120 goto out;
1121
1122 err = proto_register(&udpv6_prot, 1);
1123 if (err)
1124 goto out_unregister_tcp_proto;
1125
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001126 err = proto_register(&udplitev6_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (err)
1128 goto out_unregister_udp_proto;
1129
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001130 err = proto_register(&rawv6_prot, 1);
1131 if (err)
1132 goto out_unregister_udplite_proto;
1133
Lorenzo Colittif7048202013-01-16 22:09:49 +00001134 err = proto_register(&pingv6_prot, 1);
1135 if (err)
1136 goto out_unregister_ping_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 /* We MUST register RAW sockets before we create the ICMP6,
1139 * IGMP6, or NDISC control sockets.
1140 */
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001141 err = rawv6_init();
1142 if (err)
1143 goto out_unregister_raw_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 /* Register the family here so that the init calls below will
1146 * be able to create sockets. (?? is this dangerous ??)
1147 */
David S. Miller8eb55912005-11-11 15:05:47 -08001148 err = sock_register(&inet6_family_ops);
1149 if (err)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001150 goto out_sock_register_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Al Viroeeb61f72008-07-27 08:59:33 +01001152#ifdef CONFIG_SYSCTL
1153 err = ipv6_static_sysctl_register();
1154 if (err)
1155 goto static_sysctl_fail;
1156#endif
Glauber Costa3dc43e32011-12-11 21:47:05 +00001157 tcpv6_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /*
1160 * ipngwg API draft makes clear that the correct semantics
1161 * for TCP and UDP is to consider one TCP and UDP instance
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001162 * in a host available by both INET and INET6 APIs and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * able to communicate via both network protocols.
1164 */
1165
Daniel Lezcano81c1c172008-01-10 02:48:33 -08001166 err = register_pernet_subsys(&inet6_net_ops);
1167 if (err)
1168 goto register_pernet_fail;
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08001169 err = icmpv6_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (err)
1171 goto icmp_fail;
Wang Chen623d1a12008-07-03 12:13:30 +08001172 err = ip6_mr_init();
1173 if (err)
1174 goto ipmr_fail;
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08001175 err = ndisc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (err)
1177 goto ndisc_fail;
Denis V. Lunev9b0f9762008-02-29 11:13:15 -08001178 err = igmp6_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (err)
1180 goto igmp_fail;
Harald Welte2cc7d572005-08-09 19:42:34 -07001181 err = ipv6_netfilter_init();
1182 if (err)
1183 goto netfilter_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* Create /proc/foo6 entries. */
1185#ifdef CONFIG_PROC_FS
1186 err = -ENOMEM;
1187 if (raw6_proc_init())
1188 goto proc_raw6_fail;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001189 if (udplite6_proc_init())
1190 goto proc_udplite6_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (ipv6_misc_proc_init())
1192 goto proc_misc6_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (if6_proc_init())
1194 goto proc_if6_fail;
1195#endif
Daniel Lezcanoe2fddf52007-12-07 00:44:29 -08001196 err = ip6_route_init();
1197 if (err)
1198 goto ip6_route_fail;
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -08001199 err = ip6_flowlabel_init();
1200 if (err)
1201 goto ip6_flowlabel_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 err = addrconf_init();
1203 if (err)
1204 goto addrconf_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 /* Init v6 extension headers. */
Daniel Lezcano248b2382007-12-11 02:23:54 -08001207 err = ipv6_exthdrs_init();
1208 if (err)
1209 goto ipv6_exthdrs_fail;
1210
Daniel Lezcano853cbba2007-12-11 02:24:29 -08001211 err = ipv6_frag_init();
1212 if (err)
1213 goto ipv6_frag_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 /* Init v6 transport protocols. */
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001216 err = udpv6_init();
1217 if (err)
1218 goto udpv6_fail;
Herbert Xue2ed4052005-07-05 14:41:20 -07001219
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001220 err = udplitev6_init();
1221 if (err)
1222 goto udplitev6_fail;
1223
1224 err = tcpv6_init();
1225 if (err)
1226 goto tcpv6_fail;
1227
1228 err = ipv6_packet_init();
1229 if (err)
1230 goto ipv6_packet_fail;
Benjamin Thery94911fe2008-03-05 10:45:36 -08001231
Lorenzo Colittif7048202013-01-16 22:09:49 +00001232 err = pingv6_init();
1233 if (err)
1234 goto pingv6_fail;
1235
Benjamin Thery94911fe2008-03-05 10:45:36 -08001236#ifdef CONFIG_SYSCTL
1237 err = ipv6_sysctl_register();
1238 if (err)
1239 goto sysctl_fail;
1240#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241out:
1242 return err;
1243
Benjamin Thery94911fe2008-03-05 10:45:36 -08001244#ifdef CONFIG_SYSCTL
1245sysctl_fail:
1246 ipv6_packet_cleanup();
1247#endif
Lorenzo Colittif7048202013-01-16 22:09:49 +00001248pingv6_fail:
1249 pingv6_exit();
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001250ipv6_packet_fail:
1251 tcpv6_exit();
1252tcpv6_fail:
1253 udplitev6_exit();
1254udplitev6_fail:
1255 udpv6_exit();
1256udpv6_fail:
1257 ipv6_frag_exit();
Daniel Lezcano853cbba2007-12-11 02:24:29 -08001258ipv6_frag_fail:
1259 ipv6_exthdrs_exit();
Daniel Lezcano248b2382007-12-11 02:23:54 -08001260ipv6_exthdrs_fail:
1261 addrconf_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262addrconf_fail:
1263 ip6_flowlabel_cleanup();
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -08001264ip6_flowlabel_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 ip6_route_cleanup();
Daniel Lezcanoe2fddf52007-12-07 00:44:29 -08001266ip6_route_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267#ifdef CONFIG_PROC_FS
1268 if6_proc_exit();
1269proc_if6_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 ipv6_misc_proc_exit();
1271proc_misc6_fail:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001272 udplite6_proc_exit();
1273proc_udplite6_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 raw6_proc_exit();
1275proc_raw6_fail:
1276#endif
Harald Welte2cc7d572005-08-09 19:42:34 -07001277 ipv6_netfilter_fini();
1278netfilter_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 igmp6_cleanup();
1280igmp_fail:
1281 ndisc_cleanup();
1282ndisc_fail:
Wang Chen623d1a12008-07-03 12:13:30 +08001283 ip6_mr_cleanup();
1284ipmr_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 icmpv6_cleanup();
1286icmp_fail:
Daniel Lezcano81c1c172008-01-10 02:48:33 -08001287 unregister_pernet_subsys(&inet6_net_ops);
1288register_pernet_fail:
Al Viroeeb61f72008-07-27 08:59:33 +01001289#ifdef CONFIG_SYSCTL
1290 ipv6_static_sysctl_unregister();
1291static_sysctl_fail:
1292#endif
David S. Miller8eb55912005-11-11 15:05:47 -08001293 sock_unregister(PF_INET6);
Daniel Lezcanoe2fddf52007-12-07 00:44:29 -08001294 rtnl_unregister_all(PF_INET6);
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001295out_sock_register_fail:
1296 rawv6_exit();
Lorenzo Colittif7048202013-01-16 22:09:49 +00001297out_unregister_ping_proto:
1298 proto_unregister(&pingv6_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299out_unregister_raw_proto:
1300 proto_unregister(&rawv6_prot);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001301out_unregister_udplite_proto:
1302 proto_unregister(&udplitev6_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303out_unregister_udp_proto:
1304 proto_unregister(&udpv6_prot);
1305out_unregister_tcp_proto:
1306 proto_unregister(&tcpv6_prot);
1307 goto out;
1308}
1309module_init(inet6_init);
1310
1311static void __exit inet6_exit(void)
1312{
Brian Haley56d417b2009-06-01 03:07:33 -07001313 if (disable_ipv6_mod)
John Dykstraff8cf9a2009-03-11 09:22:51 -07001314 return;
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /* First of all disallow new sockets creation. */
1317 sock_unregister(PF_INET6);
Thomas Grafc127ea22007-03-22 11:58:32 -07001318 /* Disallow any further netlink messages */
1319 rtnl_unregister_all(PF_INET6);
Joe Jinca17c232007-02-20 01:30:15 -08001320
Benjamin Thery94911fe2008-03-05 10:45:36 -08001321#ifdef CONFIG_SYSCTL
1322 ipv6_sysctl_unregister();
1323#endif
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001324 udpv6_exit();
1325 udplitev6_exit();
1326 tcpv6_exit();
1327
Joe Jinca17c232007-02-20 01:30:15 -08001328 /* Cleanup code parts. */
1329 ipv6_packet_cleanup();
Daniel Lezcano853cbba2007-12-11 02:24:29 -08001330 ipv6_frag_exit();
Daniel Lezcano248b2382007-12-11 02:23:54 -08001331 ipv6_exthdrs_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 addrconf_cleanup();
Joe Jinca17c232007-02-20 01:30:15 -08001333 ip6_flowlabel_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 ip6_route_cleanup();
Joe Jinca17c232007-02-20 01:30:15 -08001335#ifdef CONFIG_PROC_FS
1336
1337 /* Cleanup code parts. */
1338 if6_proc_exit();
Joe Jinca17c232007-02-20 01:30:15 -08001339 ipv6_misc_proc_exit();
1340 udplite6_proc_exit();
Joe Jinca17c232007-02-20 01:30:15 -08001341 raw6_proc_exit();
1342#endif
Harald Welte2cc7d572005-08-09 19:42:34 -07001343 ipv6_netfilter_fini();
Joe Jinca17c232007-02-20 01:30:15 -08001344 igmp6_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 ndisc_cleanup();
Wang Chen623d1a12008-07-03 12:13:30 +08001346 ip6_mr_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 icmpv6_cleanup();
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001348 rawv6_exit();
Benjamin Thery94911fe2008-03-05 10:45:36 -08001349
Daniel Lezcano81c1c172008-01-10 02:48:33 -08001350 unregister_pernet_subsys(&inet6_net_ops);
Al Viroeeb61f72008-07-27 08:59:33 +01001351#ifdef CONFIG_SYSCTL
1352 ipv6_static_sysctl_unregister();
1353#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 proto_unregister(&rawv6_prot);
Joe Jinca17c232007-02-20 01:30:15 -08001355 proto_unregister(&udplitev6_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 proto_unregister(&udpv6_prot);
1357 proto_unregister(&tcpv6_prot);
Jesper Dangaard Brouer1f2ccd02009-06-26 10:46:03 +00001358
1359 rcu_barrier(); /* Wait for completion of call_rcu()'s */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361module_exit(inet6_exit);
1362
1363MODULE_ALIAS_NETPROTO(PF_INET6);