netfilter: x_tables: check for bogus target offset
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv6 / af_inet6.c
CommitLineData
1da177e4
LT
1/*
2 * PF_INET6 socket protocol family
1ab1457c 3 * Linux INET6 implementation
1da177e4
LT
4 *
5 * Authors:
1ab1457c 6 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4
LT
7 *
8 * Adapted from linux/net/ipv4/af_inet.c
9 *
1da177e4
LT
10 * 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
f3213831 21#define pr_fmt(fmt) "IPv6: " fmt
1da177e4
LT
22
23#include <linux/module.h>
4fc268d2 24#include <linux/capability.h>
1da177e4
LT
25#include <linux/errno.h>
26#include <linux/types.h>
27#include <linux/socket.h>
28#include <linux/in.h>
29#include <linux/kernel.h>
1da177e4
LT
30#include <linux/timer.h>
31#include <linux/string.h>
32#include <linux/sockios.h>
33#include <linux/net.h>
34#include <linux/fcntl.h>
35#include <linux/mm.h>
36#include <linux/interrupt.h>
37#include <linux/proc_fs.h>
38#include <linux/stat.h>
39#include <linux/init.h>
5a0e3ad6 40#include <linux/slab.h>
1da177e4
LT
41
42#include <linux/inet.h>
43#include <linux/netdevice.h>
44#include <linux/icmpv6.h>
2cc7d573 45#include <linux/netfilter_ipv6.h>
1da177e4
LT
46
47#include <net/ip.h>
48#include <net/ipv6.h>
49#include <net/udp.h>
ba4e58ec 50#include <net/udplite.h>
1da177e4 51#include <net/tcp.h>
1da177e4
LT
52#include <net/protocol.h>
53#include <net/inet_common.h>
1668e010 54#include <net/route.h>
1da177e4
LT
55#include <net/transp_v6.h>
56#include <net/ip6_route.h>
57#include <net/addrconf.h>
58#ifdef CONFIG_IPV6_TUNNEL
59#include <net/ip6_tunnel.h>
60#endif
61
62#include <asm/uaccess.h>
7bc570c8 63#include <linux/mroute6.h>
1da177e4
LT
64
65MODULE_AUTHOR("Cast of dozens");
66MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
67MODULE_LICENSE("GPL");
68
f1267347 69/* The inetsw6 table contains everything that inet6_create needs to
1da177e4
LT
70 * build a new socket.
71 */
72static struct list_head inetsw6[SOCK_MAX];
73static DEFINE_SPINLOCK(inetsw6_lock);
74
56d417b1
BH
75struct ipv6_params ipv6_defaults = {
76 .disable_ipv6 = 0,
77 .autoconf = 1,
78};
79
647c0c70 80static int disable_ipv6_mod;
56d417b1
BH
81
82module_param_named(disable, disable_ipv6_mod, int, 0444);
83MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
84
85module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
86MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
87
88module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
89MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
fe7ca2e1 90
1da177e4
LT
91static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
92{
93 const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
94
95 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
96}
97
3f378b68
EP
98static int inet6_create(struct net *net, struct socket *sock, int protocol,
99 int kern)
1da177e4
LT
100{
101 struct inet_sock *inet;
102 struct ipv6_pinfo *np;
103 struct sock *sk;
1da177e4
LT
104 struct inet_protosw *answer;
105 struct proto *answer_prot;
106 unsigned char answer_flags;
107 char answer_no_check;
af1afe86
YH
108 int try_loading_module = 0;
109 int err;
1da177e4 110
b3da2cf3
DM
111 if (sock->type != SOCK_RAW &&
112 sock->type != SOCK_DGRAM &&
113 !inet_ehash_secret)
114 build_ehash_secret();
115
0b15bc29
HFS
116 if (protocol < 0 || protocol >= IPPROTO_MAX)
117 return -EINVAL;
118
1da177e4 119 /* Look for the requested type/protocol pair. */
af1afe86
YH
120lookup_protocol:
121 err = -ESOCKTNOSUPPORT;
1da177e4 122 rcu_read_lock();
696adfe8 123 list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
1da177e4 124
696adfe8 125 err = 0;
1da177e4
LT
126 /* Check the non-wild match. */
127 if (protocol == answer->protocol) {
128 if (protocol != IPPROTO_IP)
129 break;
130 } else {
131 /* Check for the two wild cases. */
132 if (IPPROTO_IP == protocol) {
133 protocol = answer->protocol;
134 break;
135 }
136 if (IPPROTO_IP == answer->protocol)
137 break;
138 }
af1afe86 139 err = -EPROTONOSUPPORT;
1da177e4
LT
140 }
141
696adfe8 142 if (err) {
af1afe86
YH
143 if (try_loading_module < 2) {
144 rcu_read_unlock();
145 /*
146 * Be more specific, e.g. net-pf-10-proto-132-type-1
147 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
148 */
149 if (++try_loading_module == 1)
150 request_module("net-pf-%d-proto-%d-type-%d",
151 PF_INET6, protocol, sock->type);
152 /*
153 * Fall back to generic, e.g. net-pf-10-proto-132
154 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
155 */
156 else
157 request_module("net-pf-%d-proto-%d",
158 PF_INET6, protocol);
159 goto lookup_protocol;
160 } else
161 goto out_rcu_unlock;
162 }
163
164 err = -EPERM;
af31f412
EB
165 if (sock->type == SOCK_RAW && !kern &&
166 !ns_capable(net->user_ns, CAP_NET_RAW))
1da177e4 167 goto out_rcu_unlock;
1da177e4
LT
168
169 sock->ops = answer->ops;
1da177e4
LT
170 answer_prot = answer->prot;
171 answer_no_check = answer->no_check;
172 answer_flags = answer->flags;
173 rcu_read_unlock();
174
547b792c 175 WARN_ON(answer_prot->slab == NULL);
1da177e4 176
af1afe86 177 err = -ENOBUFS;
6257ff21 178 sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
1da177e4
LT
179 if (sk == NULL)
180 goto out;
181
182 sock_init_data(sock, sk);
183
af1afe86 184 err = 0;
1da177e4
LT
185 sk->sk_no_check = answer_no_check;
186 if (INET_PROTOSW_REUSE & answer_flags)
4a17fd52 187 sk->sk_reuse = SK_CAN_REUSE;
1da177e4
LT
188
189 inet = inet_sk(sk);
469de9b9 190 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
1da177e4
LT
191
192 if (SOCK_RAW == sock->type) {
c720c7e8 193 inet->inet_num = protocol;
1da177e4
LT
194 if (IPPROTO_RAW == protocol)
195 inet->hdrincl = 1;
196 }
197
e6848976 198 sk->sk_destruct = inet_sock_destruct;
1da177e4
LT
199 sk->sk_family = PF_INET6;
200 sk->sk_protocol = protocol;
201
202 sk->sk_backlog_rcv = answer->prot->backlog_rcv;
203
204 inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk);
205 np->hop_limit = -1;
f935aa9e 206 np->mcast_hops = IPV6_DEFAULT_MCASTHOPS;
1da177e4
LT
207 np->mc_loop = 1;
208 np->pmtudisc = IPV6_PMTUDISC_WANT;
2e761e05 209 np->ipv6only = net->ipv6.sysctl.bindv6only;
1ab1457c 210
1da177e4
LT
211 /* Init the ipv4 part of the socket since we can have sockets
212 * using v6 API for ipv4.
213 */
214 inet->uc_ttl = -1;
215
216 inet->mc_loop = 1;
217 inet->mc_ttl = 1;
218 inet->mc_index = 0;
219 inet->mc_list = NULL;
4c507d28 220 inet->rcv_tos = 0;
1da177e4
LT
221
222 if (ipv4_config.no_pmtu_disc)
223 inet->pmtudisc = IP_PMTUDISC_DONT;
224 else
225 inet->pmtudisc = IP_PMTUDISC_WANT;
1ab1457c 226 /*
e6848976
ACM
227 * Increment only the relevant sk_prot->socks debug field, this changes
228 * the previous behaviour of incrementing both the equivalent to
229 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
230 *
231 * This allows better debug granularity as we'll know exactly how many
232 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
233 * transport protocol socks. -acme
234 */
235 sk_refcnt_debug_inc(sk);
1da177e4 236
c720c7e8 237 if (inet->inet_num) {
1da177e4
LT
238 /* It assumes that any protocol which allows
239 * the user to assign a number at socket
240 * creation time automatically shares.
241 */
c720c7e8 242 inet->inet_sport = htons(inet->inet_num);
1da177e4
LT
243 sk->sk_prot->hash(sk);
244 }
245 if (sk->sk_prot->init) {
af1afe86
YH
246 err = sk->sk_prot->init(sk);
247 if (err) {
1da177e4
LT
248 sk_common_release(sk);
249 goto out;
250 }
251 }
252out:
af1afe86 253 return err;
1da177e4
LT
254out_rcu_unlock:
255 rcu_read_unlock();
256 goto out;
257}
258
259
260/* bind for INET6 API */
261int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
262{
647c0c70 263 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
1da177e4
LT
264 struct sock *sk = sock->sk;
265 struct inet_sock *inet = inet_sk(sk);
266 struct ipv6_pinfo *np = inet6_sk(sk);
3b1e0a65 267 struct net *net = sock_net(sk);
fd683222 268 __be32 v4addr = 0;
1da177e4
LT
269 unsigned short snum;
270 int addr_type = 0;
271 int err = 0;
272
273 /* If the socket has its own bind function then use it. */
274 if (sk->sk_prot->bind)
275 return sk->sk_prot->bind(sk, uaddr, addr_len);
276
277 if (addr_len < SIN6_LEN_RFC2133)
278 return -EINVAL;
5a079c30
MM
279
280 if (addr->sin6_family != AF_INET6)
c349a528 281 return -EAFNOSUPPORT;
5a079c30 282
1da177e4
LT
283 addr_type = ipv6_addr_type(&addr->sin6_addr);
284 if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
285 return -EINVAL;
286
287 snum = ntohs(addr->sin6_port);
3594698a 288 if (snum && snum < PROT_SOCK && !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
1da177e4
LT
289 return -EACCES;
290
291 lock_sock(sk);
292
293 /* Check these errors (active socket, double bind). */
c720c7e8 294 if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
1da177e4
LT
295 err = -EINVAL;
296 goto out;
297 }
298
299 /* Check if the address belongs to the host. */
300 if (addr_type == IPV6_ADDR_MAPPED) {
63d9950b
VY
301 int chk_addr_ret;
302
783ed5a7
VY
303 /* Binding to v4-mapped address on a v6-only socket
304 * makes no sense
305 */
306 if (np->ipv6only) {
307 err = -EINVAL;
308 goto out;
309 }
63d9950b 310
a34f0b31 311 /* Reproduce AF_INET checks to make the bindings consistent */
1da177e4 312 v4addr = addr->sin6_addr.s6_addr32[3];
63d9950b
VY
313 chk_addr_ret = inet_addr_type(net, v4addr);
314 if (!sysctl_ip_nonlocal_bind &&
315 !(inet->freebind || inet->transparent) &&
316 v4addr != htonl(INADDR_ANY) &&
317 chk_addr_ret != RTN_LOCAL &&
318 chk_addr_ret != RTN_MULTICAST &&
ca6982b8
BP
319 chk_addr_ret != RTN_BROADCAST) {
320 err = -EADDRNOTAVAIL;
1da177e4 321 goto out;
ca6982b8 322 }
1da177e4
LT
323 } else {
324 if (addr_type != IPV6_ADDR_ANY) {
325 struct net_device *dev = NULL;
326
16ba5e8e 327 rcu_read_lock();
842df073 328 if (__ipv6_addr_needs_scope_id(addr_type)) {
1da177e4
LT
329 if (addr_len >= sizeof(struct sockaddr_in6) &&
330 addr->sin6_scope_id) {
331 /* Override any existing binding, if another one
332 * is supplied by user.
333 */
334 sk->sk_bound_dev_if = addr->sin6_scope_id;
335 }
1ab1457c 336
1da177e4
LT
337 /* Binding to link-local address requires an interface */
338 if (!sk->sk_bound_dev_if) {
339 err = -EINVAL;
16ba5e8e 340 goto out_unlock;
1da177e4 341 }
16ba5e8e 342 dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
1da177e4
LT
343 if (!dev) {
344 err = -ENODEV;
16ba5e8e 345 goto out_unlock;
1da177e4
LT
346 }
347 }
348
349 /* ipv4 addr of the socket is invalid. Only the
350 * unspecified and mapped address have a v4 equivalent.
351 */
352 v4addr = LOOPBACK4_IPV6;
353 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
f74024d9 354 if (!(inet->freebind || inet->transparent) &&
0a513f6a 355 !ipv6_chk_addr(net, &addr->sin6_addr,
bfeade08 356 dev, 0)) {
1da177e4 357 err = -EADDRNOTAVAIL;
16ba5e8e 358 goto out_unlock;
1da177e4
LT
359 }
360 }
16ba5e8e 361 rcu_read_unlock();
1da177e4
LT
362 }
363 }
364
c720c7e8
ED
365 inet->inet_rcv_saddr = v4addr;
366 inet->inet_saddr = v4addr;
1da177e4 367
4e3fd7a0 368 np->rcv_saddr = addr->sin6_addr;
1ab1457c 369
1da177e4 370 if (!(addr_type & IPV6_ADDR_MULTICAST))
4e3fd7a0 371 np->saddr = addr->sin6_addr;
1da177e4
LT
372
373 /* Make sure we are allowed to bind here. */
374 if (sk->sk_prot->get_port(sk, snum)) {
375 inet_reset_saddr(sk);
376 err = -EADDRINUSE;
377 goto out;
378 }
379
0f8d3c7a 380 if (addr_type != IPV6_ADDR_ANY) {
1da177e4 381 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
0f8d3c7a
VY
382 if (addr_type != IPV6_ADDR_MAPPED)
383 np->ipv6only = 1;
384 }
1da177e4
LT
385 if (snum)
386 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
c720c7e8
ED
387 inet->inet_sport = htons(inet->inet_num);
388 inet->inet_dport = 0;
389 inet->inet_daddr = 0;
1da177e4
LT
390out:
391 release_sock(sk);
392 return err;
16ba5e8e
ED
393out_unlock:
394 rcu_read_unlock();
395 goto out;
1da177e4 396}
7159039a
YH
397EXPORT_SYMBOL(inet6_bind);
398
1da177e4
LT
399int inet6_release(struct socket *sock)
400{
401 struct sock *sk = sock->sk;
402
403 if (sk == NULL)
404 return -EINVAL;
405
406 /* Free mc lists */
407 ipv6_sock_mc_close(sk);
408
409 /* Free ac lists */
410 ipv6_sock_ac_close(sk);
411
412 return inet_release(sock);
413}
7159039a
YH
414EXPORT_SYMBOL(inet6_release);
415
7d06b2e0 416void inet6_destroy_sock(struct sock *sk)
1da177e4
LT
417{
418 struct ipv6_pinfo *np = inet6_sk(sk);
419 struct sk_buff *skb;
420 struct ipv6_txoptions *opt;
421
1da177e4
LT
422 /* Release rx options */
423
647c0c70
EZ
424 skb = xchg(&np->pktoptions, NULL);
425 if (skb != NULL)
1da177e4 426 kfree_skb(skb);
4b340ae2 427
647c0c70
EZ
428 skb = xchg(&np->rxpmtu, NULL);
429 if (skb != NULL)
4b340ae2 430 kfree_skb(skb);
1da177e4
LT
431
432 /* Free flowlabels */
433 fl6_free_socklist(sk);
434
435 /* Free tx options */
436
647c0c70
EZ
437 opt = xchg(&np->opt, NULL);
438 if (opt != NULL)
1da177e4 439 sock_kfree_s(sk, opt, opt->tot_len);
1da177e4 440}
3cf3dc6c
ACM
441EXPORT_SYMBOL_GPL(inet6_destroy_sock);
442
1da177e4
LT
443/*
444 * This does both peername and sockname.
445 */
1ab1457c 446
1da177e4
LT
447int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
448 int *uaddr_len, int peer)
449{
647c0c70 450 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
1da177e4
LT
451 struct sock *sk = sock->sk;
452 struct inet_sock *inet = inet_sk(sk);
453 struct ipv6_pinfo *np = inet6_sk(sk);
1ab1457c 454
1da177e4
LT
455 sin->sin6_family = AF_INET6;
456 sin->sin6_flowinfo = 0;
457 sin->sin6_scope_id = 0;
458 if (peer) {
c720c7e8 459 if (!inet->inet_dport)
1da177e4
LT
460 return -ENOTCONN;
461 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
462 peer == 1)
463 return -ENOTCONN;
c720c7e8 464 sin->sin6_port = inet->inet_dport;
4e3fd7a0 465 sin->sin6_addr = np->daddr;
1da177e4
LT
466 if (np->sndflow)
467 sin->sin6_flowinfo = np->flow_label;
468 } else {
469 if (ipv6_addr_any(&np->rcv_saddr))
4e3fd7a0 470 sin->sin6_addr = np->saddr;
1da177e4 471 else
4e3fd7a0 472 sin->sin6_addr = np->rcv_saddr;
1da177e4 473
c720c7e8 474 sin->sin6_port = inet->inet_sport;
1da177e4 475 }
842df073
HFS
476 sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
477 sk->sk_bound_dev_if);
1da177e4 478 *uaddr_len = sizeof(*sin);
a02cec21 479 return 0;
1da177e4 480}
7159039a
YH
481EXPORT_SYMBOL(inet6_getname);
482
1da177e4
LT
483int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
484{
485 struct sock *sk = sock->sk;
3b1e0a65 486 struct net *net = sock_net(sk);
1da177e4 487
647c0c70 488 switch (cmd) {
1da177e4
LT
489 case SIOCGSTAMP:
490 return sock_get_timestamp(sk, (struct timeval __user *)arg);
491
ae40eb1e
ED
492 case SIOCGSTAMPNS:
493 return sock_get_timestampns(sk, (struct timespec __user *)arg);
494
1da177e4
LT
495 case SIOCADDRT:
496 case SIOCDELRT:
1ab1457c 497
a02cec21 498 return ipv6_route_ioctl(net, cmd, (void __user *)arg);
1da177e4
LT
499
500 case SIOCSIFADDR:
af284937 501 return addrconf_add_ifaddr(net, (void __user *) arg);
1da177e4 502 case SIOCDIFADDR:
af284937 503 return addrconf_del_ifaddr(net, (void __user *) arg);
1da177e4 504 case SIOCSIFDSTADDR:
af284937 505 return addrconf_set_dstaddr(net, (void __user *) arg);
1da177e4 506 default:
b5e5fa5e
CH
507 if (!sk->sk_prot->ioctl)
508 return -ENOIOCTLCMD;
509 return sk->sk_prot->ioctl(sk, cmd, arg);
1da177e4
LT
510 }
511 /*NOTREACHED*/
a02cec21 512 return 0;
1da177e4 513}
7159039a
YH
514EXPORT_SYMBOL(inet6_ioctl);
515
90ddc4f0 516const struct proto_ops inet6_stream_ops = {
543d9cfe
ACM
517 .family = PF_INET6,
518 .owner = THIS_MODULE,
519 .release = inet6_release,
520 .bind = inet6_bind,
521 .connect = inet_stream_connect, /* ok */
522 .socketpair = sock_no_socketpair, /* a do nothing */
523 .accept = inet_accept, /* ok */
524 .getname = inet6_getname,
525 .poll = tcp_poll, /* ok */
526 .ioctl = inet6_ioctl, /* must change */
527 .listen = inet_listen, /* ok */
528 .shutdown = inet_shutdown, /* ok */
529 .setsockopt = sock_common_setsockopt, /* ok */
530 .getsockopt = sock_common_getsockopt, /* ok */
7ba42910
CG
531 .sendmsg = inet_sendmsg, /* ok */
532 .recvmsg = inet_recvmsg, /* ok */
543d9cfe 533 .mmap = sock_no_mmap,
7ba42910 534 .sendpage = inet_sendpage,
a0974dd3 535 .splice_read = tcp_splice_read,
3fdadf7d 536#ifdef CONFIG_COMPAT
543d9cfe
ACM
537 .compat_setsockopt = compat_sock_common_setsockopt,
538 .compat_getsockopt = compat_sock_common_getsockopt,
3fdadf7d 539#endif
1da177e4
LT
540};
541
90ddc4f0 542const struct proto_ops inet6_dgram_ops = {
543d9cfe
ACM
543 .family = PF_INET6,
544 .owner = THIS_MODULE,
545 .release = inet6_release,
546 .bind = inet6_bind,
547 .connect = inet_dgram_connect, /* ok */
548 .socketpair = sock_no_socketpair, /* a do nothing */
549 .accept = sock_no_accept, /* a do nothing */
550 .getname = inet6_getname,
551 .poll = udp_poll, /* ok */
552 .ioctl = inet6_ioctl, /* must change */
553 .listen = sock_no_listen, /* ok */
554 .shutdown = inet_shutdown, /* ok */
555 .setsockopt = sock_common_setsockopt, /* ok */
556 .getsockopt = sock_common_getsockopt, /* ok */
557 .sendmsg = inet_sendmsg, /* ok */
7ba42910 558 .recvmsg = inet_recvmsg, /* ok */
543d9cfe
ACM
559 .mmap = sock_no_mmap,
560 .sendpage = sock_no_sendpage,
3fdadf7d 561#ifdef CONFIG_COMPAT
543d9cfe
ACM
562 .compat_setsockopt = compat_sock_common_setsockopt,
563 .compat_getsockopt = compat_sock_common_getsockopt,
3fdadf7d 564#endif
1da177e4
LT
565};
566
ec1b4cf7 567static const struct net_proto_family inet6_family_ops = {
1da177e4
LT
568 .family = PF_INET6,
569 .create = inet6_create,
570 .owner = THIS_MODULE,
571};
572
87c3efbf 573int inet6_register_protosw(struct inet_protosw *p)
1da177e4
LT
574{
575 struct list_head *lh;
576 struct inet_protosw *answer;
1da177e4 577 struct list_head *last_perm;
87c3efbf
DL
578 int protocol = p->protocol;
579 int ret;
1da177e4
LT
580
581 spin_lock_bh(&inetsw6_lock);
582
87c3efbf 583 ret = -EINVAL;
1da177e4
LT
584 if (p->type >= SOCK_MAX)
585 goto out_illegal;
586
587 /* If we are trying to override a permanent protocol, bail. */
588 answer = NULL;
87c3efbf 589 ret = -EPERM;
1da177e4
LT
590 last_perm = &inetsw6[p->type];
591 list_for_each(lh, &inetsw6[p->type]) {
592 answer = list_entry(lh, struct inet_protosw, list);
593
594 /* Check only the non-wild match. */
595 if (INET_PROTOSW_PERMANENT & answer->flags) {
596 if (protocol == answer->protocol)
597 break;
598 last_perm = lh;
599 }
600
601 answer = NULL;
602 }
603 if (answer)
604 goto out_permanent;
605
606 /* Add the new entry after the last permanent entry if any, so that
607 * the new entry does not override a permanent entry when matched with
608 * a wild-card protocol. But it is allowed to override any existing
1ab1457c 609 * non-permanent entry. This means that when we remove this entry, the
1da177e4
LT
610 * system automatically returns to the old behavior.
611 */
612 list_add_rcu(&p->list, last_perm);
87c3efbf 613 ret = 0;
1da177e4
LT
614out:
615 spin_unlock_bh(&inetsw6_lock);
87c3efbf 616 return ret;
1da177e4
LT
617
618out_permanent:
f3213831 619 pr_err("Attempt to override permanent protocol %d\n", protocol);
1da177e4
LT
620 goto out;
621
622out_illegal:
f3213831 623 pr_err("Ignoring attempt to register invalid socket type %d\n",
1da177e4
LT
624 p->type);
625 goto out;
626}
7159039a
YH
627EXPORT_SYMBOL(inet6_register_protosw);
628
1da177e4
LT
629void
630inet6_unregister_protosw(struct inet_protosw *p)
631{
632 if (INET_PROTOSW_PERMANENT & p->flags) {
f3213831 633 pr_err("Attempt to unregister permanent protocol %d\n",
1da177e4
LT
634 p->protocol);
635 } else {
636 spin_lock_bh(&inetsw6_lock);
637 list_del_rcu(&p->list);
638 spin_unlock_bh(&inetsw6_lock);
639
640 synchronize_net();
641 }
642}
7159039a
YH
643EXPORT_SYMBOL(inet6_unregister_protosw);
644
b9750ce1
ACM
645int inet6_sk_rebuild_header(struct sock *sk)
646{
b9750ce1 647 struct ipv6_pinfo *np = inet6_sk(sk);
68d0c6d3 648 struct dst_entry *dst;
b9750ce1
ACM
649
650 dst = __sk_dst_check(sk, np->dst_cookie);
651
652 if (dst == NULL) {
653 struct inet_sock *inet = inet_sk(sk);
20c59de2 654 struct in6_addr *final_p, final;
4c9483b2
DM
655 struct flowi6 fl6;
656
657 memset(&fl6, 0, sizeof(fl6));
658 fl6.flowi6_proto = sk->sk_protocol;
4e3fd7a0
AD
659 fl6.daddr = np->daddr;
660 fl6.saddr = np->saddr;
4c9483b2
DM
661 fl6.flowlabel = np->flow_label;
662 fl6.flowi6_oif = sk->sk_bound_dev_if;
663 fl6.flowi6_mark = sk->sk_mark;
1958b856
DM
664 fl6.fl6_dport = inet->inet_dport;
665 fl6.fl6_sport = inet->inet_sport;
4c9483b2
DM
666 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
667
668 final_p = fl6_update_dst(&fl6, np->opt, &final);
669
670 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);
68d0c6d3 671 if (IS_ERR(dst)) {
b9750ce1 672 sk->sk_route_caps = 0;
68d0c6d3
DM
673 sk->sk_err_soft = -PTR_ERR(dst);
674 return PTR_ERR(dst);
b9750ce1
ACM
675 }
676
8e1ef0a9 677 __ip6_dst_store(sk, dst, NULL, NULL);
b9750ce1
ACM
678 }
679
680 return 0;
681}
b9750ce1
ACM
682EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
683
92113bfd 684bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb)
399c07de 685{
92113bfd
ED
686 const struct ipv6_pinfo *np = inet6_sk(sk);
687 const struct inet6_skb_parm *opt = IP6CB(skb);
399c07de
ACM
688
689 if (np->rxopt.all) {
690 if ((opt->hop && (np->rxopt.bits.hopopts ||
691 np->rxopt.bits.ohopopts)) ||
d56f90a7
ACM
692 ((IPV6_FLOWINFO_MASK &
693 *(__be32 *)skb_network_header(skb)) &&
399c07de
ACM
694 np->rxopt.bits.rxflow) ||
695 (opt->srcrt && (np->rxopt.bits.srcrt ||
696 np->rxopt.bits.osrcrt)) ||
697 ((opt->dst1 || opt->dst0) &&
698 (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
92113bfd 699 return true;
399c07de 700 }
92113bfd 701 return false;
399c07de 702}
399c07de
ACM
703EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
704
7546dd97 705static struct packet_type ipv6_packet_type __read_mostly = {
09640e63 706 .type = cpu_to_be16(ETH_P_IPV6),
662397fd 707 .func = ipv6_rcv,
22061d80
VY
708};
709
662397fd
YH
710static int __init ipv6_packet_init(void)
711{
712 dev_add_pack(&ipv6_packet_type);
713 return 0;
714}
715
716static void ipv6_packet_cleanup(void)
717{
718 dev_remove_pack(&ipv6_packet_type);
719}
720
e43291cb
DL
721static int __net_init ipv6_init_mibs(struct net *net)
722{
7d720c3e 723 if (snmp_mib_init((void __percpu **)net->mib.udp_stats_in6,
1823e4c8
ED
724 sizeof(struct udp_mib),
725 __alignof__(struct udp_mib)) < 0)
0c7ed677 726 return -ENOMEM;
7d720c3e 727 if (snmp_mib_init((void __percpu **)net->mib.udplite_stats_in6,
1823e4c8
ED
728 sizeof(struct udp_mib),
729 __alignof__(struct udp_mib)) < 0)
be713a44 730 goto err_udplite_mib;
7d720c3e 731 if (snmp_mib_init((void __percpu **)net->mib.ipv6_statistics,
1823e4c8
ED
732 sizeof(struct ipstats_mib),
733 __alignof__(struct ipstats_mib)) < 0)
9261e537 734 goto err_ip_mib;
7d720c3e 735 if (snmp_mib_init((void __percpu **)net->mib.icmpv6_statistics,
1823e4c8
ED
736 sizeof(struct icmpv6_mib),
737 __alignof__(struct icmpv6_mib)) < 0)
9261e537 738 goto err_icmp_mib;
2a24444f
ED
739 net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
740 GFP_KERNEL);
741 if (!net->mib.icmpv6msg_statistics)
9261e537 742 goto err_icmpmsg_mib;
e43291cb 743 return 0;
be713a44 744
9261e537 745err_icmpmsg_mib:
7d720c3e 746 snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
9261e537 747err_icmp_mib:
7d720c3e 748 snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
9261e537 749err_ip_mib:
7d720c3e 750 snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
be713a44 751err_udplite_mib:
7d720c3e 752 snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
be713a44 753 return -ENOMEM;
e43291cb
DL
754}
755
2c8c1e72 756static void ipv6_cleanup_mibs(struct net *net)
e43291cb 757{
7d720c3e
TH
758 snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
759 snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
760 snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
761 snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
2a24444f 762 kfree(net->mib.icmpv6msg_statistics);
e43291cb
DL
763}
764
e7dc8494 765static int __net_init inet6_net_init(struct net *net)
81c1c178 766{
0c96d8c5
DL
767 int err = 0;
768
99bc9c4e 769 net->ipv6.sysctl.bindv6only = 0;
41a76906 770 net->ipv6.sysctl.icmpv6_time = 1*HZ;
e71e0349 771
e43291cb
DL
772 err = ipv6_init_mibs(net);
773 if (err)
774 return err;
0c96d8c5
DL
775#ifdef CONFIG_PROC_FS
776 err = udp6_proc_init(net);
777 if (err)
778 goto out;
6f8b13bc
DL
779 err = tcp6_proc_init(net);
780 if (err)
781 goto proc_tcp6_fail;
6ab57e7e
DL
782 err = ac6_proc_init(net);
783 if (err)
784 goto proc_ac6_fail;
0c96d8c5
DL
785#endif
786 return err;
6f8b13bc
DL
787
788#ifdef CONFIG_PROC_FS
6ab57e7e
DL
789proc_ac6_fail:
790 tcp6_proc_exit(net);
6f8b13bc
DL
791proc_tcp6_fail:
792 udp6_proc_exit(net);
e43291cb
DL
793out:
794 ipv6_cleanup_mibs(net);
795 return err;
6f8b13bc 796#endif
81c1c178
DL
797}
798
2c8c1e72 799static void __net_exit inet6_net_exit(struct net *net)
81c1c178 800{
0c96d8c5
DL
801#ifdef CONFIG_PROC_FS
802 udp6_proc_exit(net);
6f8b13bc 803 tcp6_proc_exit(net);
6ab57e7e 804 ac6_proc_exit(net);
0c96d8c5 805#endif
e43291cb 806 ipv6_cleanup_mibs(net);
81c1c178
DL
807}
808
809static struct pernet_operations inet6_net_ops = {
810 .init = inet6_net_init,
811 .exit = inet6_net_exit,
812};
813
1da177e4
LT
814static int __init inet6_init(void)
815{
1ab1457c 816 struct list_head *r;
fe7ca2e1 817 int err = 0;
1da177e4 818
ba96bcbc 819 BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > FIELD_SIZEOF(struct sk_buff, cb));
ef047f5e 820
fe7ca2e1 821 /* Register the socket-side information for inet6_create. */
647c0c70 822 for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
fe7ca2e1
BH
823 INIT_LIST_HEAD(r);
824
56d417b1 825 if (disable_ipv6_mod) {
f3213831 826 pr_info("Loaded, but administratively disabled, reboot required to enable\n");
fe7ca2e1
BH
827 goto out;
828 }
829
1da177e4
LT
830 err = proto_register(&tcpv6_prot, 1);
831 if (err)
832 goto out;
833
834 err = proto_register(&udpv6_prot, 1);
835 if (err)
836 goto out_unregister_tcp_proto;
837
ba4e58ec 838 err = proto_register(&udplitev6_prot, 1);
1da177e4
LT
839 if (err)
840 goto out_unregister_udp_proto;
841
ba4e58ec
GR
842 err = proto_register(&rawv6_prot, 1);
843 if (err)
844 goto out_unregister_udplite_proto;
845
1da177e4 846
1da177e4
LT
847 /* We MUST register RAW sockets before we create the ICMP6,
848 * IGMP6, or NDISC control sockets.
849 */
7f4e4868
DL
850 err = rawv6_init();
851 if (err)
852 goto out_unregister_raw_proto;
1da177e4
LT
853
854 /* Register the family here so that the init calls below will
855 * be able to create sockets. (?? is this dangerous ??)
856 */
8eb55910
DM
857 err = sock_register(&inet6_family_ops);
858 if (err)
7f4e4868 859 goto out_sock_register_fail;
1da177e4 860
3dc43e3e
GC
861 tcpv6_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;
862
1da177e4
LT
863 /*
864 * ipngwg API draft makes clear that the correct semantics
865 * for TCP and UDP is to consider one TCP and UDP instance
25985edc 866 * in a host available by both INET and INET6 APIs and
1da177e4
LT
867 * able to communicate via both network protocols.
868 */
869
81c1c178
DL
870 err = register_pernet_subsys(&inet6_net_ops);
871 if (err)
872 goto register_pernet_fail;
9b0f976f 873 err = icmpv6_init();
1da177e4
LT
874 if (err)
875 goto icmp_fail;
623d1a1a
WC
876 err = ip6_mr_init();
877 if (err)
878 goto ipmr_fail;
9b0f976f 879 err = ndisc_init();
1da177e4
LT
880 if (err)
881 goto ndisc_fail;
9b0f976f 882 err = igmp6_init();
1da177e4
LT
883 if (err)
884 goto igmp_fail;
2cc7d573
HW
885 err = ipv6_netfilter_init();
886 if (err)
887 goto netfilter_fail;
1da177e4
LT
888 /* Create /proc/foo6 entries. */
889#ifdef CONFIG_PROC_FS
890 err = -ENOMEM;
891 if (raw6_proc_init())
892 goto proc_raw6_fail;
ba4e58ec
GR
893 if (udplite6_proc_init())
894 goto proc_udplite6_fail;
1da177e4
LT
895 if (ipv6_misc_proc_init())
896 goto proc_misc6_fail;
1da177e4
LT
897 if (if6_proc_init())
898 goto proc_if6_fail;
899#endif
e2fddf5e
DL
900 err = ip6_route_init();
901 if (err)
902 goto ip6_route_fail;
0a3e78ac
DL
903 err = ip6_flowlabel_init();
904 if (err)
905 goto ip6_flowlabel_fail;
1da177e4
LT
906 err = addrconf_init();
907 if (err)
908 goto addrconf_fail;
1da177e4
LT
909
910 /* Init v6 extension headers. */
248b238d
DL
911 err = ipv6_exthdrs_init();
912 if (err)
913 goto ipv6_exthdrs_fail;
914
853cbbaa
DL
915 err = ipv6_frag_init();
916 if (err)
917 goto ipv6_frag_fail;
1da177e4
LT
918
919 /* Init v6 transport protocols. */
7f4e4868
DL
920 err = udpv6_init();
921 if (err)
922 goto udpv6_fail;
e2ed4052 923
7f4e4868
DL
924 err = udplitev6_init();
925 if (err)
926 goto udplitev6_fail;
927
928 err = tcpv6_init();
929 if (err)
930 goto tcpv6_fail;
931
932 err = ipv6_packet_init();
933 if (err)
934 goto ipv6_packet_fail;
94911fe3
BT
935
936#ifdef CONFIG_SYSCTL
937 err = ipv6_sysctl_register();
938 if (err)
939 goto sysctl_fail;
940#endif
1da177e4
LT
941out:
942 return err;
943
94911fe3
BT
944#ifdef CONFIG_SYSCTL
945sysctl_fail:
946 ipv6_packet_cleanup();
947#endif
7f4e4868
DL
948ipv6_packet_fail:
949 tcpv6_exit();
950tcpv6_fail:
951 udplitev6_exit();
952udplitev6_fail:
953 udpv6_exit();
954udpv6_fail:
955 ipv6_frag_exit();
853cbbaa
DL
956ipv6_frag_fail:
957 ipv6_exthdrs_exit();
248b238d
DL
958ipv6_exthdrs_fail:
959 addrconf_cleanup();
1da177e4
LT
960addrconf_fail:
961 ip6_flowlabel_cleanup();
0a3e78ac 962ip6_flowlabel_fail:
1da177e4 963 ip6_route_cleanup();
e2fddf5e 964ip6_route_fail:
1da177e4
LT
965#ifdef CONFIG_PROC_FS
966 if6_proc_exit();
967proc_if6_fail:
1da177e4
LT
968 ipv6_misc_proc_exit();
969proc_misc6_fail:
ba4e58ec
GR
970 udplite6_proc_exit();
971proc_udplite6_fail:
1da177e4
LT
972 raw6_proc_exit();
973proc_raw6_fail:
974#endif
2cc7d573
HW
975 ipv6_netfilter_fini();
976netfilter_fail:
1da177e4
LT
977 igmp6_cleanup();
978igmp_fail:
979 ndisc_cleanup();
980ndisc_fail:
623d1a1a
WC
981 ip6_mr_cleanup();
982ipmr_fail:
1da177e4
LT
983 icmpv6_cleanup();
984icmp_fail:
81c1c178
DL
985 unregister_pernet_subsys(&inet6_net_ops);
986register_pernet_fail:
8eb55910 987 sock_unregister(PF_INET6);
e2fddf5e 988 rtnl_unregister_all(PF_INET6);
7f4e4868
DL
989out_sock_register_fail:
990 rawv6_exit();
1da177e4
LT
991out_unregister_raw_proto:
992 proto_unregister(&rawv6_prot);
ba4e58ec
GR
993out_unregister_udplite_proto:
994 proto_unregister(&udplitev6_prot);
1da177e4
LT
995out_unregister_udp_proto:
996 proto_unregister(&udpv6_prot);
997out_unregister_tcp_proto:
998 proto_unregister(&tcpv6_prot);
999 goto out;
1000}
1001module_init(inet6_init);
1002
1003static void __exit inet6_exit(void)
1004{
56d417b1 1005 if (disable_ipv6_mod)
ff8cf9a9
JD
1006 return;
1007
1da177e4
LT
1008 /* First of all disallow new sockets creation. */
1009 sock_unregister(PF_INET6);
c127ea2c
TG
1010 /* Disallow any further netlink messages */
1011 rtnl_unregister_all(PF_INET6);
ca17c233 1012
7f4e4868
DL
1013 udpv6_exit();
1014 udplitev6_exit();
1015 tcpv6_exit();
1016
ca17c233
JJ
1017 /* Cleanup code parts. */
1018 ipv6_packet_cleanup();
853cbbaa 1019 ipv6_frag_exit();
248b238d 1020 ipv6_exthdrs_exit();
ca17c233
JJ
1021 addrconf_cleanup();
1022 ip6_flowlabel_cleanup();
1023 ip6_route_cleanup();
1da177e4 1024#ifdef CONFIG_PROC_FS
ca17c233
JJ
1025
1026 /* Cleanup code parts. */
1da177e4 1027 if6_proc_exit();
1ab1457c 1028 ipv6_misc_proc_exit();
1ab1457c 1029 udplite6_proc_exit();
1ab1457c 1030 raw6_proc_exit();
2c8d7ca0 1031#endif
2cc7d573 1032 ipv6_netfilter_fini();
ca17c233 1033 igmp6_cleanup();
1da177e4 1034 ndisc_cleanup();
623d1a1a 1035 ip6_mr_cleanup();
1da177e4 1036 icmpv6_cleanup();
7f4e4868 1037 rawv6_exit();
94911fe3 1038
81c1c178 1039 unregister_pernet_subsys(&inet6_net_ops);
1da177e4 1040 proto_unregister(&rawv6_prot);
ca17c233 1041 proto_unregister(&udplitev6_prot);
1da177e4
LT
1042 proto_unregister(&udpv6_prot);
1043 proto_unregister(&tcpv6_prot);
1f2ccd00
JDB
1044
1045 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1da177e4
LT
1046}
1047module_exit(inet6_exit);
1048
1049MODULE_ALIAS_NETPROTO(PF_INET6);