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