blob: 3d828bc4b1cf97cbfcbd1df9e76c5b357b37a9c4 [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>
39
40#include <linux/inet.h>
41#include <linux/netdevice.h>
42#include <linux/icmpv6.h>
Harald Welte2cc7d572005-08-09 19:42:34 -070043#include <linux/netfilter_ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <net/ip.h>
46#include <net/ipv6.h>
47#include <net/udp.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080048#include <net/udplite.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/tcp.h>
50#include <net/ipip.h>
51#include <net/protocol.h>
52#include <net/inet_common.h>
53#include <net/transp_v6.h>
54#include <net/ip6_route.h>
55#include <net/addrconf.h>
56#ifdef CONFIG_IPV6_TUNNEL
57#include <net/ip6_tunnel.h>
58#endif
59
60#include <asm/uaccess.h>
61#include <asm/system.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090062#include <linux/mroute6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64MODULE_AUTHOR("Cast of dozens");
65MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
66MODULE_LICENSE("GPL");
67
Pavel Emelyanovf1267342007-11-23 21:28:44 +080068/* The inetsw6 table contains everything that inet6_create needs to
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * build a new socket.
70 */
71static struct list_head inetsw6[SOCK_MAX];
72static DEFINE_SPINLOCK(inetsw6_lock);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
75{
76 const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
77
78 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
79}
80
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -070081static int inet6_create(struct net *net, struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 struct inet_sock *inet;
84 struct ipv6_pinfo *np;
85 struct sock *sk;
86 struct list_head *p;
87 struct inet_protosw *answer;
88 struct proto *answer_prot;
89 unsigned char answer_flags;
90 char answer_no_check;
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -080091 int try_loading_module = 0;
92 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
David S. Millerb3da2cf2007-03-23 11:40:27 -070094 if (sock->type != SOCK_RAW &&
95 sock->type != SOCK_DGRAM &&
96 !inet_ehash_secret)
97 build_ehash_secret();
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 /* Look for the requested type/protocol pair. */
100 answer = NULL;
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800101lookup_protocol:
102 err = -ESOCKTNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 rcu_read_lock();
104 list_for_each_rcu(p, &inetsw6[sock->type]) {
105 answer = list_entry(p, struct inet_protosw, list);
106
107 /* Check the non-wild match. */
108 if (protocol == answer->protocol) {
109 if (protocol != IPPROTO_IP)
110 break;
111 } else {
112 /* Check for the two wild cases. */
113 if (IPPROTO_IP == protocol) {
114 protocol = answer->protocol;
115 break;
116 }
117 if (IPPROTO_IP == answer->protocol)
118 break;
119 }
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800120 err = -EPROTONOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 answer = NULL;
122 }
123
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800124 if (!answer) {
125 if (try_loading_module < 2) {
126 rcu_read_unlock();
127 /*
128 * Be more specific, e.g. net-pf-10-proto-132-type-1
129 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
130 */
131 if (++try_loading_module == 1)
132 request_module("net-pf-%d-proto-%d-type-%d",
133 PF_INET6, protocol, sock->type);
134 /*
135 * Fall back to generic, e.g. net-pf-10-proto-132
136 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
137 */
138 else
139 request_module("net-pf-%d-proto-%d",
140 PF_INET6, protocol);
141 goto lookup_protocol;
142 } else
143 goto out_rcu_unlock;
144 }
145
146 err = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (answer->capability > 0 && !capable(answer->capability))
148 goto out_rcu_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 sock->ops = answer->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 answer_prot = answer->prot;
152 answer_no_check = answer->no_check;
153 answer_flags = answer->flags;
154 rcu_read_unlock();
155
156 BUG_TRAP(answer_prot->slab != NULL);
157
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800158 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700159 sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (sk == NULL)
161 goto out;
162
163 sock_init_data(sock, sk);
164
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800165 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 sk->sk_no_check = answer_no_check;
167 if (INET_PROTOSW_REUSE & answer_flags)
168 sk->sk_reuse = 1;
169
170 inet = inet_sk(sk);
Paul Moore469de9b2007-01-09 14:37:06 -0800171 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 if (SOCK_RAW == sock->type) {
174 inet->num = protocol;
175 if (IPPROTO_RAW == protocol)
176 inet->hdrincl = 1;
177 }
178
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700179 sk->sk_destruct = inet_sock_destruct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 sk->sk_family = PF_INET6;
181 sk->sk_protocol = protocol;
182
183 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
184
185 inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
186 np->hop_limit = -1;
187 np->mcast_hops = -1;
188 np->mc_loop = 1;
189 np->pmtudisc = IPV6_PMTUDISC_WANT;
Pavel Emelyanov2e761e02008-06-09 15:53:30 -0700190 np->ipv6only = net->ipv6.sysctl.bindv6only;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /* Init the ipv4 part of the socket since we can have sockets
193 * using v6 API for ipv4.
194 */
195 inet->uc_ttl = -1;
196
197 inet->mc_loop = 1;
198 inet->mc_ttl = 1;
199 inet->mc_index = 0;
200 inet->mc_list = NULL;
201
202 if (ipv4_config.no_pmtu_disc)
203 inet->pmtudisc = IP_PMTUDISC_DONT;
204 else
205 inet->pmtudisc = IP_PMTUDISC_WANT;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900206 /*
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700207 * Increment only the relevant sk_prot->socks debug field, this changes
208 * the previous behaviour of incrementing both the equivalent to
209 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
210 *
211 * This allows better debug granularity as we'll know exactly how many
212 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
213 * transport protocol socks. -acme
214 */
215 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (inet->num) {
218 /* It assumes that any protocol which allows
219 * the user to assign a number at socket
220 * creation time automatically shares.
221 */
Al Viroe69a4ad2006-11-14 20:56:00 -0800222 inet->sport = htons(inet->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 sk->sk_prot->hash(sk);
224 }
225 if (sk->sk_prot->init) {
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800226 err = sk->sk_prot->init(sk);
227 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 sk_common_release(sk);
229 goto out;
230 }
231 }
232out:
YOSHIFUJI Hideakiaf1afe82005-12-02 20:56:57 -0800233 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234out_rcu_unlock:
235 rcu_read_unlock();
236 goto out;
237}
238
239
240/* bind for INET6 API */
241int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
242{
243 struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
244 struct sock *sk = sock->sk;
245 struct inet_sock *inet = inet_sk(sk);
246 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900247 struct net *net = sock_net(sk);
Al Virofd683222006-09-26 22:17:51 -0700248 __be32 v4addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 unsigned short snum;
250 int addr_type = 0;
251 int err = 0;
252
253 /* If the socket has its own bind function then use it. */
254 if (sk->sk_prot->bind)
255 return sk->sk_prot->bind(sk, uaddr, addr_len);
256
257 if (addr_len < SIN6_LEN_RFC2133)
258 return -EINVAL;
259 addr_type = ipv6_addr_type(&addr->sin6_addr);
260 if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
261 return -EINVAL;
262
263 snum = ntohs(addr->sin6_port);
264 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
265 return -EACCES;
266
267 lock_sock(sk);
268
269 /* Check these errors (active socket, double bind). */
270 if (sk->sk_state != TCP_CLOSE || inet->num) {
271 err = -EINVAL;
272 goto out;
273 }
274
275 /* Check if the address belongs to the host. */
276 if (addr_type == IPV6_ADDR_MAPPED) {
277 v4addr = addr->sin6_addr.s6_addr32[3];
Benjamin Thery075de932008-03-05 10:45:59 -0800278 if (inet_addr_type(net, v4addr) != RTN_LOCAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 err = -EADDRNOTAVAIL;
280 goto out;
281 }
282 } else {
283 if (addr_type != IPV6_ADDR_ANY) {
284 struct net_device *dev = NULL;
285
286 if (addr_type & IPV6_ADDR_LINKLOCAL) {
287 if (addr_len >= sizeof(struct sockaddr_in6) &&
288 addr->sin6_scope_id) {
289 /* Override any existing binding, if another one
290 * is supplied by user.
291 */
292 sk->sk_bound_dev_if = addr->sin6_scope_id;
293 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* Binding to link-local address requires an interface */
296 if (!sk->sk_bound_dev_if) {
297 err = -EINVAL;
298 goto out;
299 }
Benjamin Thery075de932008-03-05 10:45:59 -0800300 dev = dev_get_by_index(net, sk->sk_bound_dev_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (!dev) {
302 err = -ENODEV;
303 goto out;
304 }
305 }
306
307 /* ipv4 addr of the socket is invalid. Only the
308 * unspecified and mapped address have a v4 equivalent.
309 */
310 v4addr = LOOPBACK4_IPV6;
311 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
Benjamin Thery075de932008-03-05 10:45:59 -0800312 if (!ipv6_chk_addr(net, &addr->sin6_addr,
Daniel Lezcanobfeade02008-01-10 22:43:18 -0800313 dev, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (dev)
315 dev_put(dev);
316 err = -EADDRNOTAVAIL;
317 goto out;
318 }
319 }
320 if (dev)
321 dev_put(dev);
322 }
323 }
324
325 inet->rcv_saddr = v4addr;
326 inet->saddr = v4addr;
327
328 ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (!(addr_type & IPV6_ADDR_MULTICAST))
331 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
332
333 /* Make sure we are allowed to bind here. */
334 if (sk->sk_prot->get_port(sk, snum)) {
335 inet_reset_saddr(sk);
336 err = -EADDRINUSE;
337 goto out;
338 }
339
340 if (addr_type != IPV6_ADDR_ANY)
341 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
342 if (snum)
343 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
Al Viroe69a4ad2006-11-14 20:56:00 -0800344 inet->sport = htons(inet->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 inet->dport = 0;
346 inet->daddr = 0;
347out:
348 release_sock(sk);
349 return err;
350}
351
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900352EXPORT_SYMBOL(inet6_bind);
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354int inet6_release(struct socket *sock)
355{
356 struct sock *sk = sock->sk;
357
358 if (sk == NULL)
359 return -EINVAL;
360
361 /* Free mc lists */
362 ipv6_sock_mc_close(sk);
363
364 /* Free ac lists */
365 ipv6_sock_ac_close(sk);
366
367 return inet_release(sock);
368}
369
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900370EXPORT_SYMBOL(inet6_release);
371
Brian Haley7d06b2e2008-06-14 17:04:49 -0700372void inet6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct ipv6_pinfo *np = inet6_sk(sk);
375 struct sk_buff *skb;
376 struct ipv6_txoptions *opt;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Release rx options */
379
380 if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
381 kfree_skb(skb);
382
383 /* Free flowlabels */
384 fl6_free_socklist(sk);
385
386 /* Free tx options */
387
388 if ((opt = xchg(&np->opt, NULL)) != NULL)
389 sock_kfree_s(sk, opt, opt->tot_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -0800392EXPORT_SYMBOL_GPL(inet6_destroy_sock);
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/*
395 * This does both peername and sockname.
396 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
399 int *uaddr_len, int peer)
400{
401 struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
402 struct sock *sk = sock->sk;
403 struct inet_sock *inet = inet_sk(sk);
404 struct ipv6_pinfo *np = inet6_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 sin->sin6_family = AF_INET6;
407 sin->sin6_flowinfo = 0;
408 sin->sin6_scope_id = 0;
409 if (peer) {
410 if (!inet->dport)
411 return -ENOTCONN;
412 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
413 peer == 1)
414 return -ENOTCONN;
415 sin->sin6_port = inet->dport;
416 ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
417 if (np->sndflow)
418 sin->sin6_flowinfo = np->flow_label;
419 } else {
420 if (ipv6_addr_any(&np->rcv_saddr))
421 ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
422 else
423 ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
424
425 sin->sin6_port = inet->sport;
426 }
427 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
428 sin->sin6_scope_id = sk->sk_bound_dev_if;
429 *uaddr_len = sizeof(*sin);
430 return(0);
431}
432
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900433EXPORT_SYMBOL(inet6_getname);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
436{
437 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900438 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900440 switch(cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 {
442 case SIOCGSTAMP:
443 return sock_get_timestamp(sk, (struct timeval __user *)arg);
444
Eric Dumazetae40eb12007-03-18 17:33:16 -0700445 case SIOCGSTAMPNS:
446 return sock_get_timestampns(sk, (struct timespec __user *)arg);
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 case SIOCADDRT:
449 case SIOCDELRT:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900450
Daniel Lezcano55786892008-03-04 13:47:47 -0800451 return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 case SIOCSIFADDR:
Daniel Lezcanoaf284932008-03-05 10:46:57 -0800454 return addrconf_add_ifaddr(net, (void __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 case SIOCDIFADDR:
Daniel Lezcanoaf284932008-03-05 10:46:57 -0800456 return addrconf_del_ifaddr(net, (void __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 case SIOCSIFDSTADDR:
Daniel Lezcanoaf284932008-03-05 10:46:57 -0800458 return addrconf_set_dstaddr(net, (void __user *) arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -0800460 if (!sk->sk_prot->ioctl)
461 return -ENOIOCTLCMD;
462 return sk->sk_prot->ioctl(sk, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464 /*NOTREACHED*/
465 return(0);
466}
467
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900468EXPORT_SYMBOL(inet6_ioctl);
469
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800470const struct proto_ops inet6_stream_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800471 .family = PF_INET6,
472 .owner = THIS_MODULE,
473 .release = inet6_release,
474 .bind = inet6_bind,
475 .connect = inet_stream_connect, /* ok */
476 .socketpair = sock_no_socketpair, /* a do nothing */
477 .accept = inet_accept, /* ok */
478 .getname = inet6_getname,
479 .poll = tcp_poll, /* ok */
480 .ioctl = inet6_ioctl, /* must change */
481 .listen = inet_listen, /* ok */
482 .shutdown = inet_shutdown, /* ok */
483 .setsockopt = sock_common_setsockopt, /* ok */
484 .getsockopt = sock_common_getsockopt, /* ok */
David S. Miller3516ffb2007-08-02 19:23:56 -0700485 .sendmsg = tcp_sendmsg, /* ok */
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800486 .recvmsg = sock_common_recvmsg, /* ok */
487 .mmap = sock_no_mmap,
488 .sendpage = tcp_sendpage,
Jens Axboea0974dd2007-11-06 23:31:58 -0800489 .splice_read = tcp_splice_read,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800490#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800491 .compat_setsockopt = compat_sock_common_setsockopt,
492 .compat_getsockopt = compat_sock_common_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800493#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494};
495
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800496const struct proto_ops inet6_dgram_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800497 .family = PF_INET6,
498 .owner = THIS_MODULE,
499 .release = inet6_release,
500 .bind = inet6_bind,
501 .connect = inet_dgram_connect, /* ok */
502 .socketpair = sock_no_socketpair, /* a do nothing */
503 .accept = sock_no_accept, /* a do nothing */
504 .getname = inet6_getname,
505 .poll = udp_poll, /* ok */
506 .ioctl = inet6_ioctl, /* must change */
507 .listen = sock_no_listen, /* ok */
508 .shutdown = inet_shutdown, /* ok */
509 .setsockopt = sock_common_setsockopt, /* ok */
510 .getsockopt = sock_common_getsockopt, /* ok */
511 .sendmsg = inet_sendmsg, /* ok */
512 .recvmsg = sock_common_recvmsg, /* ok */
513 .mmap = sock_no_mmap,
514 .sendpage = sock_no_sendpage,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800515#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800516 .compat_setsockopt = compat_sock_common_setsockopt,
517 .compat_getsockopt = compat_sock_common_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800518#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519};
520
521static struct net_proto_family inet6_family_ops = {
522 .family = PF_INET6,
523 .create = inet6_create,
524 .owner = THIS_MODULE,
525};
526
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800527int inet6_register_protosw(struct inet_protosw *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct list_head *lh;
530 struct inet_protosw *answer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct list_head *last_perm;
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800532 int protocol = p->protocol;
533 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 spin_lock_bh(&inetsw6_lock);
536
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800537 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (p->type >= SOCK_MAX)
539 goto out_illegal;
540
541 /* If we are trying to override a permanent protocol, bail. */
542 answer = NULL;
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800543 ret = -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 last_perm = &inetsw6[p->type];
545 list_for_each(lh, &inetsw6[p->type]) {
546 answer = list_entry(lh, struct inet_protosw, list);
547
548 /* Check only the non-wild match. */
549 if (INET_PROTOSW_PERMANENT & answer->flags) {
550 if (protocol == answer->protocol)
551 break;
552 last_perm = lh;
553 }
554
555 answer = NULL;
556 }
557 if (answer)
558 goto out_permanent;
559
560 /* Add the new entry after the last permanent entry if any, so that
561 * the new entry does not override a permanent entry when matched with
562 * a wild-card protocol. But it is allowed to override any existing
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900563 * non-permanent entry. This means that when we remove this entry, the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 * system automatically returns to the old behavior.
565 */
566 list_add_rcu(&p->list, last_perm);
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800567 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568out:
569 spin_unlock_bh(&inetsw6_lock);
Daniel Lezcano87c3efb2007-12-11 02:25:01 -0800570 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572out_permanent:
573 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
574 protocol);
575 goto out;
576
577out_illegal:
578 printk(KERN_ERR
579 "Ignoring attempt to register invalid socket type %d.\n",
580 p->type);
581 goto out;
582}
583
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900584EXPORT_SYMBOL(inet6_register_protosw);
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586void
587inet6_unregister_protosw(struct inet_protosw *p)
588{
589 if (INET_PROTOSW_PERMANENT & p->flags) {
590 printk(KERN_ERR
591 "Attempt to unregister permanent protocol %d.\n",
592 p->protocol);
593 } else {
594 spin_lock_bh(&inetsw6_lock);
595 list_del_rcu(&p->list);
596 spin_unlock_bh(&inetsw6_lock);
597
598 synchronize_net();
599 }
600}
601
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900602EXPORT_SYMBOL(inet6_unregister_protosw);
603
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800604int inet6_sk_rebuild_header(struct sock *sk)
605{
606 int err;
607 struct dst_entry *dst;
608 struct ipv6_pinfo *np = inet6_sk(sk);
609
610 dst = __sk_dst_check(sk, np->dst_cookie);
611
612 if (dst == NULL) {
613 struct inet_sock *inet = inet_sk(sk);
614 struct in6_addr *final_p = NULL, final;
615 struct flowi fl;
616
617 memset(&fl, 0, sizeof(fl));
618 fl.proto = sk->sk_protocol;
619 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
620 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
621 fl.fl6_flowlabel = np->flow_label;
622 fl.oif = sk->sk_bound_dev_if;
623 fl.fl_ip_dport = inet->dport;
624 fl.fl_ip_sport = inet->sport;
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700625 security_sk_classify_flow(sk, &fl);
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800626
627 if (np->opt && np->opt->srcrt) {
628 struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
629 ipv6_addr_copy(&final, &fl.fl6_dst);
630 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
631 final_p = &final;
632 }
633
634 err = ip6_dst_lookup(sk, &dst, &fl);
635 if (err) {
636 sk->sk_route_caps = 0;
637 return err;
638 }
639 if (final_p)
640 ipv6_addr_copy(&fl.fl6_dst, final_p);
641
642 if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
643 sk->sk_err_soft = -err;
644 return err;
645 }
646
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700647 __ip6_dst_store(sk, dst, NULL, NULL);
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800648 }
649
650 return 0;
651}
652
653EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
654
Arnaldo Carvalho de Melo399c07d2005-12-13 23:24:28 -0800655int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
656{
657 struct ipv6_pinfo *np = inet6_sk(sk);
658 struct inet6_skb_parm *opt = IP6CB(skb);
659
660 if (np->rxopt.all) {
661 if ((opt->hop && (np->rxopt.bits.hopopts ||
662 np->rxopt.bits.ohopopts)) ||
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700663 ((IPV6_FLOWINFO_MASK &
664 *(__be32 *)skb_network_header(skb)) &&
Arnaldo Carvalho de Melo399c07d2005-12-13 23:24:28 -0800665 np->rxopt.bits.rxflow) ||
666 (opt->srcrt && (np->rxopt.bits.srcrt ||
667 np->rxopt.bits.osrcrt)) ||
668 ((opt->dst1 || opt->dst0) &&
669 (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
670 return 1;
671 }
672 return 0;
673}
674
675EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
676
YOSHIFUJI Hideaki662397f2008-02-27 23:14:03 +0900677static struct inet6_protocol *ipv6_gso_pull_exthdrs(struct sk_buff *skb,
678 int proto)
679{
680 struct inet6_protocol *ops = NULL;
681
682 for (;;) {
683 struct ipv6_opt_hdr *opth;
684 int len;
685
686 if (proto != NEXTHDR_HOP) {
687 ops = rcu_dereference(inet6_protos[proto]);
688
689 if (unlikely(!ops))
690 break;
691
692 if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
693 break;
694 }
695
696 if (unlikely(!pskb_may_pull(skb, 8)))
697 break;
698
699 opth = (void *)skb->data;
700 len = ipv6_optlen(opth);
701
702 if (unlikely(!pskb_may_pull(skb, len)))
703 break;
704
705 proto = opth->nexthdr;
706 __skb_pull(skb, len);
707 }
708
709 return ops;
710}
711
712static int ipv6_gso_send_check(struct sk_buff *skb)
713{
714 struct ipv6hdr *ipv6h;
715 struct inet6_protocol *ops;
716 int err = -EINVAL;
717
718 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
719 goto out;
720
721 ipv6h = ipv6_hdr(skb);
722 __skb_pull(skb, sizeof(*ipv6h));
723 err = -EPROTONOSUPPORT;
724
725 rcu_read_lock();
726 ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
727 if (likely(ops && ops->gso_send_check)) {
728 skb_reset_transport_header(skb);
729 err = ops->gso_send_check(skb);
730 }
731 rcu_read_unlock();
732
733out:
734 return err;
735}
736
737static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
738{
739 struct sk_buff *segs = ERR_PTR(-EINVAL);
740 struct ipv6hdr *ipv6h;
741 struct inet6_protocol *ops;
742
743 if (!(features & NETIF_F_V6_CSUM))
744 features &= ~NETIF_F_SG;
745
746 if (unlikely(skb_shinfo(skb)->gso_type &
747 ~(SKB_GSO_UDP |
748 SKB_GSO_DODGY |
749 SKB_GSO_TCP_ECN |
750 SKB_GSO_TCPV6 |
751 0)))
752 goto out;
753
754 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
755 goto out;
756
757 ipv6h = ipv6_hdr(skb);
758 __skb_pull(skb, sizeof(*ipv6h));
759 segs = ERR_PTR(-EPROTONOSUPPORT);
760
761 rcu_read_lock();
762 ops = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
763 if (likely(ops && ops->gso_segment)) {
764 skb_reset_transport_header(skb);
765 segs = ops->gso_segment(skb, features);
766 }
767 rcu_read_unlock();
768
769 if (unlikely(IS_ERR(segs)))
770 goto out;
771
772 for (skb = segs; skb; skb = skb->next) {
773 ipv6h = ipv6_hdr(skb);
774 ipv6h->payload_len = htons(skb->len - skb->mac_len -
775 sizeof(*ipv6h));
776 }
777
778out:
779 return segs;
780}
781
782static struct packet_type ipv6_packet_type = {
783 .type = __constant_htons(ETH_P_IPV6),
784 .func = ipv6_rcv,
785 .gso_send_check = ipv6_gso_send_check,
786 .gso_segment = ipv6_gso_segment,
787};
788
789static int __init ipv6_packet_init(void)
790{
791 dev_add_pack(&ipv6_packet_type);
792 return 0;
793}
794
795static void ipv6_packet_cleanup(void)
796{
797 dev_remove_pack(&ipv6_packet_type);
798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800static int __init init_ipv6_mibs(void)
801{
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800802 if (snmp_mib_init((void **)ipv6_statistics,
803 sizeof(struct ipstats_mib)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 goto err_ip_mib;
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800805 if (snmp_mib_init((void **)icmpv6_statistics,
806 sizeof(struct icmpv6_mib)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 goto err_icmp_mib;
David L Stevens14878f72007-09-16 16:52:35 -0700808 if (snmp_mib_init((void **)icmpv6msg_statistics,
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800809 sizeof(struct icmpv6msg_mib)) < 0)
David L Stevens14878f72007-09-16 16:52:35 -0700810 goto err_icmpmsg_mib;
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800811 if (snmp_mib_init((void **)udp_stats_in6, sizeof (struct udp_mib)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto err_udp_mib;
YOSHIFUJI Hideakic69bce22008-01-23 22:31:45 -0800813 if (snmp_mib_init((void **)udplite_stats_in6,
814 sizeof (struct udp_mib)) < 0)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800815 goto err_udplite_mib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return 0;
817
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800818err_udplite_mib:
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700819 snmp_mib_free((void **)udp_stats_in6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820err_udp_mib:
David L Stevens14878f72007-09-16 16:52:35 -0700821 snmp_mib_free((void **)icmpv6msg_statistics);
822err_icmpmsg_mib:
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700823 snmp_mib_free((void **)icmpv6_statistics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824err_icmp_mib:
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700825 snmp_mib_free((void **)ipv6_statistics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826err_ip_mib:
827 return -ENOMEM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831static void cleanup_ipv6_mibs(void)
832{
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700833 snmp_mib_free((void **)ipv6_statistics);
834 snmp_mib_free((void **)icmpv6_statistics);
Pavel Emelyanovdc8a82a2007-10-17 19:30:40 -0700835 snmp_mib_free((void **)icmpv6msg_statistics);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700836 snmp_mib_free((void **)udp_stats_in6);
837 snmp_mib_free((void **)udplite_stats_in6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
Daniel Lezcano81c1c172008-01-10 02:48:33 -0800840static int inet6_net_init(struct net *net)
841{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -0700842 int err = 0;
843
Daniel Lezcano99bc9c42008-01-10 02:54:53 -0800844 net->ipv6.sysctl.bindv6only = 0;
Daniel Lezcano49905092008-01-10 03:01:01 -0800845 net->ipv6.sysctl.flush_delay = 0;
846 net->ipv6.sysctl.ip6_rt_max_size = 4096;
847 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
848 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
849 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
850 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
851 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
852 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
Daniel Lezcano41a76902008-01-10 03:02:40 -0800853 net->ipv6.sysctl.icmpv6_time = 1*HZ;
Daniel Lezcanoe71e0342008-01-10 02:56:03 -0800854
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -0700855#ifdef CONFIG_PROC_FS
856 err = udp6_proc_init(net);
857 if (err)
858 goto out;
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -0700859 err = tcp6_proc_init(net);
860 if (err)
861 goto proc_tcp6_fail;
Daniel Lezcano6ab57e72008-03-26 16:52:32 -0700862 err = ac6_proc_init(net);
863 if (err)
864 goto proc_ac6_fail;
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -0700865out:
866#endif
867 return err;
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -0700868
869#ifdef CONFIG_PROC_FS
Daniel Lezcano6ab57e72008-03-26 16:52:32 -0700870proc_ac6_fail:
871 tcp6_proc_exit(net);
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -0700872proc_tcp6_fail:
873 udp6_proc_exit(net);
874 goto out;
875#endif
Daniel Lezcano81c1c172008-01-10 02:48:33 -0800876}
877
878static void inet6_net_exit(struct net *net)
879{
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -0700880#ifdef CONFIG_PROC_FS
881 udp6_proc_exit(net);
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -0700882 tcp6_proc_exit(net);
Daniel Lezcano6ab57e72008-03-26 16:52:32 -0700883 ac6_proc_exit(net);
Daniel Lezcano0c96d8c2008-03-21 04:14:17 -0700884#endif
Daniel Lezcano81c1c172008-01-10 02:48:33 -0800885}
886
887static struct pernet_operations inet6_net_ops = {
888 .init = inet6_net_init,
889 .exit = inet6_net_exit,
890};
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892static int __init inet6_init(void)
893{
894 struct sk_buff *dummy_skb;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900895 struct list_head *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int err;
897
YOSHIFUJI Hideakief047f52006-09-01 00:29:06 -0700898 BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 err = proto_register(&tcpv6_prot, 1);
901 if (err)
902 goto out;
903
904 err = proto_register(&udpv6_prot, 1);
905 if (err)
906 goto out_unregister_tcp_proto;
907
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800908 err = proto_register(&udplitev6_prot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (err)
910 goto out_unregister_udp_proto;
911
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800912 err = proto_register(&rawv6_prot, 1);
913 if (err)
914 goto out_unregister_udplite_proto;
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 /* Register the socket-side information for inet6_create. */
918 for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
919 INIT_LIST_HEAD(r);
920
921 /* We MUST register RAW sockets before we create the ICMP6,
922 * IGMP6, or NDISC control sockets.
923 */
Daniel Lezcano7f4e4862007-12-11 02:25:35 -0800924 err = rawv6_init();
925 if (err)
926 goto out_unregister_raw_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 /* Register the family here so that the init calls below will
929 * be able to create sockets. (?? is this dangerous ??)
930 */
David S. Miller8eb55912005-11-11 15:05:47 -0800931 err = sock_register(&inet6_family_ops);
932 if (err)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -0800933 goto out_sock_register_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /* Initialise ipv6 mibs */
936 err = init_ipv6_mibs();
937 if (err)
David S. Miller8eb55912005-11-11 15:05:47 -0800938 goto out_unregister_sock;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /*
941 * ipngwg API draft makes clear that the correct semantics
942 * for TCP and UDP is to consider one TCP and UDP instance
943 * in a host availiable by both INET and INET6 APIs and
944 * able to communicate via both network protocols.
945 */
946
Daniel Lezcano81c1c172008-01-10 02:48:33 -0800947 err = register_pernet_subsys(&inet6_net_ops);
948 if (err)
949 goto register_pernet_fail;
Denis V. Lunev9b0f9762008-02-29 11:13:15 -0800950 err = icmpv6_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (err)
952 goto icmp_fail;
Wang Chen623d1a12008-07-03 12:13:30 +0800953 err = ip6_mr_init();
954 if (err)
955 goto ipmr_fail;
Denis V. Lunev9b0f9762008-02-29 11:13:15 -0800956 err = ndisc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (err)
958 goto ndisc_fail;
Denis V. Lunev9b0f9762008-02-29 11:13:15 -0800959 err = igmp6_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (err)
961 goto igmp_fail;
Harald Welte2cc7d572005-08-09 19:42:34 -0700962 err = ipv6_netfilter_init();
963 if (err)
964 goto netfilter_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* Create /proc/foo6 entries. */
966#ifdef CONFIG_PROC_FS
967 err = -ENOMEM;
968 if (raw6_proc_init())
969 goto proc_raw6_fail;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800970 if (udplite6_proc_init())
971 goto proc_udplite6_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (ipv6_misc_proc_init())
973 goto proc_misc6_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (if6_proc_init())
975 goto proc_if6_fail;
976#endif
Daniel Lezcanoe2fddf52007-12-07 00:44:29 -0800977 err = ip6_route_init();
978 if (err)
979 goto ip6_route_fail;
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -0800980 err = ip6_flowlabel_init();
981 if (err)
982 goto ip6_flowlabel_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 err = addrconf_init();
984 if (err)
985 goto addrconf_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* Init v6 extension headers. */
Daniel Lezcano248b2382007-12-11 02:23:54 -0800988 err = ipv6_exthdrs_init();
989 if (err)
990 goto ipv6_exthdrs_fail;
991
Daniel Lezcano853cbba2007-12-11 02:24:29 -0800992 err = ipv6_frag_init();
993 if (err)
994 goto ipv6_frag_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /* Init v6 transport protocols. */
Daniel Lezcano7f4e4862007-12-11 02:25:35 -0800997 err = udpv6_init();
998 if (err)
999 goto udpv6_fail;
Herbert Xue2ed4052005-07-05 14:41:20 -07001000
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001001 err = udplitev6_init();
1002 if (err)
1003 goto udplitev6_fail;
1004
1005 err = tcpv6_init();
1006 if (err)
1007 goto tcpv6_fail;
1008
1009 err = ipv6_packet_init();
1010 if (err)
1011 goto ipv6_packet_fail;
Benjamin Thery94911fe2008-03-05 10:45:36 -08001012
1013#ifdef CONFIG_SYSCTL
1014 err = ipv6_sysctl_register();
1015 if (err)
1016 goto sysctl_fail;
1017#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018out:
1019 return err;
1020
Benjamin Thery94911fe2008-03-05 10:45:36 -08001021#ifdef CONFIG_SYSCTL
1022sysctl_fail:
1023 ipv6_packet_cleanup();
1024#endif
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001025ipv6_packet_fail:
1026 tcpv6_exit();
1027tcpv6_fail:
1028 udplitev6_exit();
1029udplitev6_fail:
1030 udpv6_exit();
1031udpv6_fail:
1032 ipv6_frag_exit();
Daniel Lezcano853cbba2007-12-11 02:24:29 -08001033ipv6_frag_fail:
1034 ipv6_exthdrs_exit();
Daniel Lezcano248b2382007-12-11 02:23:54 -08001035ipv6_exthdrs_fail:
1036 addrconf_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037addrconf_fail:
1038 ip6_flowlabel_cleanup();
Daniel Lezcano0a3e78a2007-12-11 02:23:18 -08001039ip6_flowlabel_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 ip6_route_cleanup();
Daniel Lezcanoe2fddf52007-12-07 00:44:29 -08001041ip6_route_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042#ifdef CONFIG_PROC_FS
1043 if6_proc_exit();
1044proc_if6_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 ipv6_misc_proc_exit();
1046proc_misc6_fail:
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001047 udplite6_proc_exit();
1048proc_udplite6_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 raw6_proc_exit();
1050proc_raw6_fail:
1051#endif
Harald Welte2cc7d572005-08-09 19:42:34 -07001052 ipv6_netfilter_fini();
1053netfilter_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 igmp6_cleanup();
1055igmp_fail:
1056 ndisc_cleanup();
1057ndisc_fail:
Wang Chen623d1a12008-07-03 12:13:30 +08001058 ip6_mr_cleanup();
1059ipmr_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 icmpv6_cleanup();
1061icmp_fail:
Daniel Lezcano81c1c172008-01-10 02:48:33 -08001062 unregister_pernet_subsys(&inet6_net_ops);
1063register_pernet_fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 cleanup_ipv6_mibs();
David S. Miller8eb55912005-11-11 15:05:47 -08001065out_unregister_sock:
1066 sock_unregister(PF_INET6);
Daniel Lezcanoe2fddf52007-12-07 00:44:29 -08001067 rtnl_unregister_all(PF_INET6);
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001068out_sock_register_fail:
1069 rawv6_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070out_unregister_raw_proto:
1071 proto_unregister(&rawv6_prot);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001072out_unregister_udplite_proto:
1073 proto_unregister(&udplitev6_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074out_unregister_udp_proto:
1075 proto_unregister(&udpv6_prot);
1076out_unregister_tcp_proto:
1077 proto_unregister(&tcpv6_prot);
1078 goto out;
1079}
1080module_init(inet6_init);
1081
1082static void __exit inet6_exit(void)
1083{
1084 /* First of all disallow new sockets creation. */
1085 sock_unregister(PF_INET6);
Thomas Grafc127ea22007-03-22 11:58:32 -07001086 /* Disallow any further netlink messages */
1087 rtnl_unregister_all(PF_INET6);
Joe Jinca17c232007-02-20 01:30:15 -08001088
Benjamin Thery94911fe2008-03-05 10:45:36 -08001089#ifdef CONFIG_SYSCTL
1090 ipv6_sysctl_unregister();
1091#endif
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001092 udpv6_exit();
1093 udplitev6_exit();
1094 tcpv6_exit();
1095
Joe Jinca17c232007-02-20 01:30:15 -08001096 /* Cleanup code parts. */
1097 ipv6_packet_cleanup();
Daniel Lezcano853cbba2007-12-11 02:24:29 -08001098 ipv6_frag_exit();
Daniel Lezcano248b2382007-12-11 02:23:54 -08001099 ipv6_exthdrs_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 addrconf_cleanup();
Joe Jinca17c232007-02-20 01:30:15 -08001101 ip6_flowlabel_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 ip6_route_cleanup();
Joe Jinca17c232007-02-20 01:30:15 -08001103#ifdef CONFIG_PROC_FS
1104
1105 /* Cleanup code parts. */
1106 if6_proc_exit();
Joe Jinca17c232007-02-20 01:30:15 -08001107 ipv6_misc_proc_exit();
1108 udplite6_proc_exit();
Joe Jinca17c232007-02-20 01:30:15 -08001109 raw6_proc_exit();
1110#endif
Harald Welte2cc7d572005-08-09 19:42:34 -07001111 ipv6_netfilter_fini();
Joe Jinca17c232007-02-20 01:30:15 -08001112 igmp6_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 ndisc_cleanup();
Wang Chen623d1a12008-07-03 12:13:30 +08001114 ip6_mr_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 icmpv6_cleanup();
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001116 rawv6_exit();
Benjamin Thery94911fe2008-03-05 10:45:36 -08001117
Daniel Lezcano81c1c172008-01-10 02:48:33 -08001118 unregister_pernet_subsys(&inet6_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 cleanup_ipv6_mibs();
1120 proto_unregister(&rawv6_prot);
Joe Jinca17c232007-02-20 01:30:15 -08001121 proto_unregister(&udplitev6_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 proto_unregister(&udpv6_prot);
1123 proto_unregister(&tcpv6_prot);
1124}
1125module_exit(inet6_exit);
1126
1127MODULE_ALIAS_NETPROTO(PF_INET6);