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
22 #include <linux/module.h>
23 #include <linux/capability.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/in.h>
28 #include <linux/kernel.h>
29 #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>
43 #include <linux/netfilter_ipv6.h>
44
45 #include <net/ip.h>
46 #include <net/ipv6.h>
47 #include <net/udp.h>
48 #include <net/udplite.h>
49 #include <net/tcp.h>
50 #include <net/ipip.h>
51 #include <net/protocol.h>
52 #include <net/inet_common.h>
53 #include <net/route.h>
54 #include <net/transp_v6.h>
55 #include <net/ip6_route.h>
56 #include <net/addrconf.h>
57 #ifdef CONFIG_IPV6_TUNNEL
58 #include <net/ip6_tunnel.h>
59 #endif
60
61 #include <asm/uaccess.h>
62 #include <asm/system.h>
63 #include <linux/mroute6.h>
64
65 #ifdef CONFIG_ANDROID_PARANOID_NETWORK
66 #include <linux/android_aid.h>
67
current_has_network(void)68 static inline int current_has_network(void)
69 {
70 return in_egroup_p(AID_INET) || capable(CAP_NET_RAW);
71 }
72 #else
current_has_network(void)73 static inline int current_has_network(void)
74 {
75 return 1;
76 }
77 #endif
78
79 MODULE_AUTHOR("Cast of dozens");
80 MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
81 MODULE_LICENSE("GPL");
82
83 /* The inetsw6 table contains everything that inet6_create needs to
84 * build a new socket.
85 */
86 static struct list_head inetsw6[SOCK_MAX];
87 static DEFINE_SPINLOCK(inetsw6_lock);
88
89 static int disable_ipv6 = 0;
90 module_param_named(disable, disable_ipv6, int, 0);
91 MODULE_PARM_DESC(disable, "Disable IPv6 such that it is non-functional");
92
inet6_sk_generic(struct sock * sk)93 static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk)
94 {
95 const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo);
96
97 return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
98 }
99
inet6_create(struct net * net,struct socket * sock,int protocol)100 static int inet6_create(struct net *net, struct socket *sock, int protocol)
101 {
102 struct inet_sock *inet;
103 struct ipv6_pinfo *np;
104 struct sock *sk;
105 struct inet_protosw *answer;
106 struct proto *answer_prot;
107 unsigned char answer_flags;
108 char answer_no_check;
109 int try_loading_module = 0;
110 int err;
111
112 if (!current_has_network())
113 return -EACCES;
114
115 if (sock->type != SOCK_RAW &&
116 sock->type != SOCK_DGRAM &&
117 !inet_ehash_secret)
118 build_ehash_secret();
119
120 /* Look for the requested type/protocol pair. */
121 lookup_protocol:
122 err = -ESOCKTNOSUPPORT;
123 rcu_read_lock();
124 list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
125
126 err = 0;
127 /* Check the non-wild match. */
128 if (protocol == answer->protocol) {
129 if (protocol != IPPROTO_IP)
130 break;
131 } else {
132 /* Check for the two wild cases. */
133 if (IPPROTO_IP == protocol) {
134 protocol = answer->protocol;
135 break;
136 }
137 if (IPPROTO_IP == answer->protocol)
138 break;
139 }
140 err = -EPROTONOSUPPORT;
141 }
142
143 if (err) {
144 if (try_loading_module < 2) {
145 rcu_read_unlock();
146 /*
147 * Be more specific, e.g. net-pf-10-proto-132-type-1
148 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM)
149 */
150 if (++try_loading_module == 1)
151 request_module("net-pf-%d-proto-%d-type-%d",
152 PF_INET6, protocol, sock->type);
153 /*
154 * Fall back to generic, e.g. net-pf-10-proto-132
155 * (net-pf-PF_INET6-proto-IPPROTO_SCTP)
156 */
157 else
158 request_module("net-pf-%d-proto-%d",
159 PF_INET6, protocol);
160 goto lookup_protocol;
161 } else
162 goto out_rcu_unlock;
163 }
164
165 err = -EPERM;
166 if (answer->capability > 0 && !capable(answer->capability))
167 goto out_rcu_unlock;
168
169 sock->ops = answer->ops;
170 answer_prot = answer->prot;
171 answer_no_check = answer->no_check;
172 answer_flags = answer->flags;
173 rcu_read_unlock();
174
175 WARN_ON(answer_prot->slab == NULL);
176
177 err = -ENOBUFS;
178 sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
179 if (sk == NULL)
180 goto out;
181
182 sock_init_data(sock, sk);
183
184 err = 0;
185 sk->sk_no_check = answer_no_check;
186 if (INET_PROTOSW_REUSE & answer_flags)
187 sk->sk_reuse = 1;
188
189 inet = inet_sk(sk);
190 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
191
192 if (SOCK_RAW == sock->type) {
193 inet->num = protocol;
194 if (IPPROTO_RAW == protocol)
195 inet->hdrincl = 1;
196 }
197
198 sk->sk_destruct = inet_sock_destruct;
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;
206 np->mcast_hops = -1;
207 np->mc_loop = 1;
208 np->pmtudisc = IPV6_PMTUDISC_WANT;
209 np->ipv6only = net->ipv6.sysctl.bindv6only;
210
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;
220
221 if (ipv4_config.no_pmtu_disc)
222 inet->pmtudisc = IP_PMTUDISC_DONT;
223 else
224 inet->pmtudisc = IP_PMTUDISC_WANT;
225 /*
226 * Increment only the relevant sk_prot->socks debug field, this changes
227 * the previous behaviour of incrementing both the equivalent to
228 * answer->prot->socks (inet6_sock_nr) and inet_sock_nr.
229 *
230 * This allows better debug granularity as we'll know exactly how many
231 * UDPv6, TCPv6, etc socks were allocated, not the sum of all IPv6
232 * transport protocol socks. -acme
233 */
234 sk_refcnt_debug_inc(sk);
235
236 if (inet->num) {
237 /* It assumes that any protocol which allows
238 * the user to assign a number at socket
239 * creation time automatically shares.
240 */
241 inet->sport = htons(inet->num);
242 sk->sk_prot->hash(sk);
243 }
244 if (sk->sk_prot->init) {
245 err = sk->sk_prot->init(sk);
246 if (err) {
247 sk_common_release(sk);
248 goto out;
249 }
250 }
251 out:
252 return err;
253 out_rcu_unlock:
254 rcu_read_unlock();
255 goto out;
256 }
257
258
259 /* bind for INET6 API */
inet6_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)260 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
261 {
262 struct sockaddr_in6 *addr=(struct sockaddr_in6 *)uaddr;
263 struct sock *sk = sock->sk;
264 struct inet_sock *inet = inet_sk(sk);
265 struct ipv6_pinfo *np = inet6_sk(sk);
266 struct net *net = sock_net(sk);
267 __be32 v4addr = 0;
268 unsigned short snum;
269 int addr_type = 0;
270 int err = 0;
271
272 /* If the socket has its own bind function then use it. */
273 if (sk->sk_prot->bind)
274 return sk->sk_prot->bind(sk, uaddr, addr_len);
275
276 if (addr_len < SIN6_LEN_RFC2133)
277 return -EINVAL;
278 addr_type = ipv6_addr_type(&addr->sin6_addr);
279 if ((addr_type & IPV6_ADDR_MULTICAST) && sock->type == SOCK_STREAM)
280 return -EINVAL;
281
282 snum = ntohs(addr->sin6_port);
283 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
284 return -EACCES;
285
286 lock_sock(sk);
287
288 /* Check these errors (active socket, double bind). */
289 if (sk->sk_state != TCP_CLOSE || inet->num) {
290 err = -EINVAL;
291 goto out;
292 }
293
294 /* Check if the address belongs to the host. */
295 if (addr_type == IPV6_ADDR_MAPPED) {
296 v4addr = addr->sin6_addr.s6_addr32[3];
297 if (inet_addr_type(net, v4addr) != RTN_LOCAL) {
298 err = -EADDRNOTAVAIL;
299 goto out;
300 }
301 } else {
302 if (addr_type != IPV6_ADDR_ANY) {
303 struct net_device *dev = NULL;
304
305 if (addr_type & IPV6_ADDR_LINKLOCAL) {
306 if (addr_len >= sizeof(struct sockaddr_in6) &&
307 addr->sin6_scope_id) {
308 /* Override any existing binding, if another one
309 * is supplied by user.
310 */
311 sk->sk_bound_dev_if = addr->sin6_scope_id;
312 }
313
314 /* Binding to link-local address requires an interface */
315 if (!sk->sk_bound_dev_if) {
316 err = -EINVAL;
317 goto out;
318 }
319 dev = dev_get_by_index(net, sk->sk_bound_dev_if);
320 if (!dev) {
321 err = -ENODEV;
322 goto out;
323 }
324 }
325
326 /* ipv4 addr of the socket is invalid. Only the
327 * unspecified and mapped address have a v4 equivalent.
328 */
329 v4addr = LOOPBACK4_IPV6;
330 if (!(addr_type & IPV6_ADDR_MULTICAST)) {
331 if (!ipv6_chk_addr(net, &addr->sin6_addr,
332 dev, 0)) {
333 if (dev)
334 dev_put(dev);
335 err = -EADDRNOTAVAIL;
336 goto out;
337 }
338 }
339 if (dev)
340 dev_put(dev);
341 }
342 }
343
344 inet->rcv_saddr = v4addr;
345 inet->saddr = v4addr;
346
347 ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
348
349 if (!(addr_type & IPV6_ADDR_MULTICAST))
350 ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
351
352 /* Make sure we are allowed to bind here. */
353 if (sk->sk_prot->get_port(sk, snum)) {
354 inet_reset_saddr(sk);
355 err = -EADDRINUSE;
356 goto out;
357 }
358
359 if (addr_type != IPV6_ADDR_ANY)
360 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
361 if (snum)
362 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
363 inet->sport = htons(inet->num);
364 inet->dport = 0;
365 inet->daddr = 0;
366 out:
367 release_sock(sk);
368 return err;
369 }
370
371 EXPORT_SYMBOL(inet6_bind);
372
inet6_release(struct socket * sock)373 int inet6_release(struct socket *sock)
374 {
375 struct sock *sk = sock->sk;
376
377 if (sk == NULL)
378 return -EINVAL;
379
380 /* Free mc lists */
381 ipv6_sock_mc_close(sk);
382
383 /* Free ac lists */
384 ipv6_sock_ac_close(sk);
385
386 return inet_release(sock);
387 }
388
389 EXPORT_SYMBOL(inet6_release);
390
inet6_destroy_sock(struct sock * sk)391 void inet6_destroy_sock(struct sock *sk)
392 {
393 struct ipv6_pinfo *np = inet6_sk(sk);
394 struct sk_buff *skb;
395 struct ipv6_txoptions *opt;
396
397 /* Release rx options */
398
399 if ((skb = xchg(&np->pktoptions, NULL)) != NULL)
400 kfree_skb(skb);
401
402 /* Free flowlabels */
403 fl6_free_socklist(sk);
404
405 /* Free tx options */
406
407 if ((opt = xchg(&np->opt, NULL)) != NULL)
408 sock_kfree_s(sk, opt, opt->tot_len);
409 }
410
411 EXPORT_SYMBOL_GPL(inet6_destroy_sock);
412
413 /*
414 * This does both peername and sockname.
415 */
416
inet6_getname(struct socket * sock,struct sockaddr * uaddr,int * uaddr_len,int peer)417 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
418 int *uaddr_len, int peer)
419 {
420 struct sockaddr_in6 *sin=(struct sockaddr_in6 *)uaddr;
421 struct sock *sk = sock->sk;
422 struct inet_sock *inet = inet_sk(sk);
423 struct ipv6_pinfo *np = inet6_sk(sk);
424
425 sin->sin6_family = AF_INET6;
426 sin->sin6_flowinfo = 0;
427 sin->sin6_scope_id = 0;
428 if (peer) {
429 if (!inet->dport)
430 return -ENOTCONN;
431 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
432 peer == 1)
433 return -ENOTCONN;
434 sin->sin6_port = inet->dport;
435 ipv6_addr_copy(&sin->sin6_addr, &np->daddr);
436 if (np->sndflow)
437 sin->sin6_flowinfo = np->flow_label;
438 } else {
439 if (ipv6_addr_any(&np->rcv_saddr))
440 ipv6_addr_copy(&sin->sin6_addr, &np->saddr);
441 else
442 ipv6_addr_copy(&sin->sin6_addr, &np->rcv_saddr);
443
444 sin->sin6_port = inet->sport;
445 }
446 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
447 sin->sin6_scope_id = sk->sk_bound_dev_if;
448 *uaddr_len = sizeof(*sin);
449 return(0);
450 }
451
452 EXPORT_SYMBOL(inet6_getname);
453
inet6_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)454 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
455 {
456 struct sock *sk = sock->sk;
457 struct net *net = sock_net(sk);
458
459 switch(cmd)
460 {
461 case SIOCGSTAMP:
462 return sock_get_timestamp(sk, (struct timeval __user *)arg);
463
464 case SIOCGSTAMPNS:
465 return sock_get_timestampns(sk, (struct timespec __user *)arg);
466
467 case SIOCADDRT:
468 case SIOCDELRT:
469
470 return(ipv6_route_ioctl(net, cmd, (void __user *)arg));
471
472 case SIOCSIFADDR:
473 return addrconf_add_ifaddr(net, (void __user *) arg);
474 case SIOCDIFADDR:
475 return addrconf_del_ifaddr(net, (void __user *) arg);
476 case SIOCSIFDSTADDR:
477 return addrconf_set_dstaddr(net, (void __user *) arg);
478 default:
479 if (!sk->sk_prot->ioctl)
480 return -ENOIOCTLCMD;
481 return sk->sk_prot->ioctl(sk, cmd, arg);
482 }
483 /*NOTREACHED*/
484 return(0);
485 }
486
487 EXPORT_SYMBOL(inet6_ioctl);
488
489 const struct proto_ops inet6_stream_ops = {
490 .family = PF_INET6,
491 .owner = THIS_MODULE,
492 .release = inet6_release,
493 .bind = inet6_bind,
494 .connect = inet_stream_connect, /* ok */
495 .socketpair = sock_no_socketpair, /* a do nothing */
496 .accept = inet_accept, /* ok */
497 .getname = inet6_getname,
498 .poll = tcp_poll, /* ok */
499 .ioctl = inet6_ioctl, /* must change */
500 .listen = inet_listen, /* ok */
501 .shutdown = inet_shutdown, /* ok */
502 .setsockopt = sock_common_setsockopt, /* ok */
503 .getsockopt = sock_common_getsockopt, /* ok */
504 .sendmsg = tcp_sendmsg, /* ok */
505 .recvmsg = sock_common_recvmsg, /* ok */
506 .mmap = sock_no_mmap,
507 .sendpage = tcp_sendpage,
508 .splice_read = tcp_splice_read,
509 #ifdef CONFIG_COMPAT
510 .compat_setsockopt = compat_sock_common_setsockopt,
511 .compat_getsockopt = compat_sock_common_getsockopt,
512 #endif
513 };
514
515 const struct proto_ops inet6_dgram_ops = {
516 .family = PF_INET6,
517 .owner = THIS_MODULE,
518 .release = inet6_release,
519 .bind = inet6_bind,
520 .connect = inet_dgram_connect, /* ok */
521 .socketpair = sock_no_socketpair, /* a do nothing */
522 .accept = sock_no_accept, /* a do nothing */
523 .getname = inet6_getname,
524 .poll = udp_poll, /* ok */
525 .ioctl = inet6_ioctl, /* must change */
526 .listen = sock_no_listen, /* ok */
527 .shutdown = inet_shutdown, /* ok */
528 .setsockopt = sock_common_setsockopt, /* ok */
529 .getsockopt = sock_common_getsockopt, /* ok */
530 .sendmsg = inet_sendmsg, /* ok */
531 .recvmsg = sock_common_recvmsg, /* ok */
532 .mmap = sock_no_mmap,
533 .sendpage = sock_no_sendpage,
534 #ifdef CONFIG_COMPAT
535 .compat_setsockopt = compat_sock_common_setsockopt,
536 .compat_getsockopt = compat_sock_common_getsockopt,
537 #endif
538 };
539
540 static struct net_proto_family inet6_family_ops = {
541 .family = PF_INET6,
542 .create = inet6_create,
543 .owner = THIS_MODULE,
544 };
545
inet6_register_protosw(struct inet_protosw * p)546 int inet6_register_protosw(struct inet_protosw *p)
547 {
548 struct list_head *lh;
549 struct inet_protosw *answer;
550 struct list_head *last_perm;
551 int protocol = p->protocol;
552 int ret;
553
554 spin_lock_bh(&inetsw6_lock);
555
556 ret = -EINVAL;
557 if (p->type >= SOCK_MAX)
558 goto out_illegal;
559
560 /* If we are trying to override a permanent protocol, bail. */
561 answer = NULL;
562 ret = -EPERM;
563 last_perm = &inetsw6[p->type];
564 list_for_each(lh, &inetsw6[p->type]) {
565 answer = list_entry(lh, struct inet_protosw, list);
566
567 /* Check only the non-wild match. */
568 if (INET_PROTOSW_PERMANENT & answer->flags) {
569 if (protocol == answer->protocol)
570 break;
571 last_perm = lh;
572 }
573
574 answer = NULL;
575 }
576 if (answer)
577 goto out_permanent;
578
579 /* Add the new entry after the last permanent entry if any, so that
580 * the new entry does not override a permanent entry when matched with
581 * a wild-card protocol. But it is allowed to override any existing
582 * non-permanent entry. This means that when we remove this entry, the
583 * system automatically returns to the old behavior.
584 */
585 list_add_rcu(&p->list, last_perm);
586 ret = 0;
587 out:
588 spin_unlock_bh(&inetsw6_lock);
589 return ret;
590
591 out_permanent:
592 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
593 protocol);
594 goto out;
595
596 out_illegal:
597 printk(KERN_ERR
598 "Ignoring attempt to register invalid socket type %d.\n",
599 p->type);
600 goto out;
601 }
602
603 EXPORT_SYMBOL(inet6_register_protosw);
604
605 void
inet6_unregister_protosw(struct inet_protosw * p)606 inet6_unregister_protosw(struct inet_protosw *p)
607 {
608 if (INET_PROTOSW_PERMANENT & p->flags) {
609 printk(KERN_ERR
610 "Attempt to unregister permanent protocol %d.\n",
611 p->protocol);
612 } else {
613 spin_lock_bh(&inetsw6_lock);
614 list_del_rcu(&p->list);
615 spin_unlock_bh(&inetsw6_lock);
616
617 synchronize_net();
618 }
619 }
620
621 EXPORT_SYMBOL(inet6_unregister_protosw);
622
inet6_sk_rebuild_header(struct sock * sk)623 int inet6_sk_rebuild_header(struct sock *sk)
624 {
625 int err;
626 struct dst_entry *dst;
627 struct ipv6_pinfo *np = inet6_sk(sk);
628
629 dst = __sk_dst_check(sk, np->dst_cookie);
630
631 if (dst == NULL) {
632 struct inet_sock *inet = inet_sk(sk);
633 struct in6_addr *final_p = NULL, final;
634 struct flowi fl;
635
636 memset(&fl, 0, sizeof(fl));
637 fl.proto = sk->sk_protocol;
638 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
639 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
640 fl.fl6_flowlabel = np->flow_label;
641 fl.oif = sk->sk_bound_dev_if;
642 fl.fl_ip_dport = inet->dport;
643 fl.fl_ip_sport = inet->sport;
644 security_sk_classify_flow(sk, &fl);
645
646 if (np->opt && np->opt->srcrt) {
647 struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
648 ipv6_addr_copy(&final, &fl.fl6_dst);
649 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
650 final_p = &final;
651 }
652
653 err = ip6_dst_lookup(sk, &dst, &fl);
654 if (err) {
655 sk->sk_route_caps = 0;
656 return err;
657 }
658 if (final_p)
659 ipv6_addr_copy(&fl.fl6_dst, final_p);
660
661 if ((err = xfrm_lookup(sock_net(sk), &dst, &fl, sk, 0)) < 0) {
662 sk->sk_err_soft = -err;
663 return err;
664 }
665
666 __ip6_dst_store(sk, dst, NULL, NULL);
667 }
668
669 return 0;
670 }
671
672 EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
673
ipv6_opt_accepted(struct sock * sk,struct sk_buff * skb)674 int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
675 {
676 struct ipv6_pinfo *np = inet6_sk(sk);
677 struct inet6_skb_parm *opt = IP6CB(skb);
678
679 if (np->rxopt.all) {
680 if ((opt->hop && (np->rxopt.bits.hopopts ||
681 np->rxopt.bits.ohopopts)) ||
682 ((IPV6_FLOWINFO_MASK &
683 *(__be32 *)skb_network_header(skb)) &&
684 np->rxopt.bits.rxflow) ||
685 (opt->srcrt && (np->rxopt.bits.srcrt ||
686 np->rxopt.bits.osrcrt)) ||
687 ((opt->dst1 || opt->dst0) &&
688 (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts)))
689 return 1;
690 }
691 return 0;
692 }
693
694 EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
695
ipv6_gso_pull_exthdrs(struct sk_buff * skb,int proto)696 static int ipv6_gso_pull_exthdrs(struct sk_buff *skb, int proto)
697 {
698 struct inet6_protocol *ops = NULL;
699
700 for (;;) {
701 struct ipv6_opt_hdr *opth;
702 int len;
703
704 if (proto != NEXTHDR_HOP) {
705 ops = rcu_dereference(inet6_protos[proto]);
706
707 if (unlikely(!ops))
708 break;
709
710 if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
711 break;
712 }
713
714 if (unlikely(!pskb_may_pull(skb, 8)))
715 break;
716
717 opth = (void *)skb->data;
718 len = ipv6_optlen(opth);
719
720 if (unlikely(!pskb_may_pull(skb, len)))
721 break;
722
723 proto = opth->nexthdr;
724 __skb_pull(skb, len);
725 }
726
727 return proto;
728 }
729
ipv6_gso_send_check(struct sk_buff * skb)730 static int ipv6_gso_send_check(struct sk_buff *skb)
731 {
732 struct ipv6hdr *ipv6h;
733 struct inet6_protocol *ops;
734 int err = -EINVAL;
735
736 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
737 goto out;
738
739 ipv6h = ipv6_hdr(skb);
740 __skb_pull(skb, sizeof(*ipv6h));
741 err = -EPROTONOSUPPORT;
742
743 rcu_read_lock();
744 ops = rcu_dereference(inet6_protos[
745 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
746
747 if (likely(ops && ops->gso_send_check)) {
748 skb_reset_transport_header(skb);
749 err = ops->gso_send_check(skb);
750 }
751 rcu_read_unlock();
752
753 out:
754 return err;
755 }
756
ipv6_gso_segment(struct sk_buff * skb,int features)757 static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
758 {
759 struct sk_buff *segs = ERR_PTR(-EINVAL);
760 struct ipv6hdr *ipv6h;
761 struct inet6_protocol *ops;
762
763 if (!(features & NETIF_F_V6_CSUM))
764 features &= ~NETIF_F_SG;
765
766 if (unlikely(skb_shinfo(skb)->gso_type &
767 ~(SKB_GSO_UDP |
768 SKB_GSO_DODGY |
769 SKB_GSO_TCP_ECN |
770 SKB_GSO_TCPV6 |
771 0)))
772 goto out;
773
774 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
775 goto out;
776
777 ipv6h = ipv6_hdr(skb);
778 __skb_pull(skb, sizeof(*ipv6h));
779 segs = ERR_PTR(-EPROTONOSUPPORT);
780
781 rcu_read_lock();
782 ops = rcu_dereference(inet6_protos[
783 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
784
785 if (likely(ops && ops->gso_segment)) {
786 skb_reset_transport_header(skb);
787 segs = ops->gso_segment(skb, features);
788 }
789 rcu_read_unlock();
790
791 if (unlikely(IS_ERR(segs)))
792 goto out;
793
794 for (skb = segs; skb; skb = skb->next) {
795 ipv6h = ipv6_hdr(skb);
796 ipv6h->payload_len = htons(skb->len - skb->mac_len -
797 sizeof(*ipv6h));
798 }
799
800 out:
801 return segs;
802 }
803
804 struct ipv6_gro_cb {
805 struct napi_gro_cb napi;
806 int proto;
807 };
808
809 #define IPV6_GRO_CB(skb) ((struct ipv6_gro_cb *)(skb)->cb)
810
ipv6_gro_receive(struct sk_buff ** head,struct sk_buff * skb)811 static struct sk_buff **ipv6_gro_receive(struct sk_buff **head,
812 struct sk_buff *skb)
813 {
814 struct inet6_protocol *ops;
815 struct sk_buff **pp = NULL;
816 struct sk_buff *p;
817 struct ipv6hdr *iph;
818 unsigned int nlen;
819 int flush = 1;
820 int proto;
821 __wsum csum;
822
823 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
824 goto out;
825
826 iph = ipv6_hdr(skb);
827 __skb_pull(skb, sizeof(*iph));
828
829 flush += ntohs(iph->payload_len) != skb->len;
830
831 rcu_read_lock();
832 proto = ipv6_gso_pull_exthdrs(skb, iph->nexthdr);
833 iph = ipv6_hdr(skb);
834 IPV6_GRO_CB(skb)->proto = proto;
835 ops = rcu_dereference(inet6_protos[proto]);
836 if (!ops || !ops->gro_receive)
837 goto out_unlock;
838
839 flush--;
840 skb_reset_transport_header(skb);
841 nlen = skb_network_header_len(skb);
842
843 for (p = *head; p; p = p->next) {
844 struct ipv6hdr *iph2;
845
846 if (!NAPI_GRO_CB(p)->same_flow)
847 continue;
848
849 iph2 = ipv6_hdr(p);
850
851 /* All fields must match except length. */
852 if (nlen != skb_network_header_len(p) ||
853 memcmp(iph, iph2, offsetof(struct ipv6hdr, payload_len)) ||
854 memcmp(&iph->nexthdr, &iph2->nexthdr,
855 nlen - offsetof(struct ipv6hdr, nexthdr))) {
856 NAPI_GRO_CB(p)->same_flow = 0;
857 continue;
858 }
859
860 NAPI_GRO_CB(p)->flush |= flush;
861 }
862
863 NAPI_GRO_CB(skb)->flush |= flush;
864
865 csum = skb->csum;
866 skb_postpull_rcsum(skb, iph, skb_network_header_len(skb));
867
868 pp = ops->gro_receive(head, skb);
869
870 skb->csum = csum;
871
872 out_unlock:
873 rcu_read_unlock();
874
875 out:
876 NAPI_GRO_CB(skb)->flush |= flush;
877
878 return pp;
879 }
880
ipv6_gro_complete(struct sk_buff * skb)881 static int ipv6_gro_complete(struct sk_buff *skb)
882 {
883 struct inet6_protocol *ops;
884 struct ipv6hdr *iph = ipv6_hdr(skb);
885 int err = -ENOSYS;
886
887 iph->payload_len = htons(skb->len - skb_network_offset(skb) -
888 sizeof(*iph));
889
890 rcu_read_lock();
891 ops = rcu_dereference(inet6_protos[IPV6_GRO_CB(skb)->proto]);
892 if (WARN_ON(!ops || !ops->gro_complete))
893 goto out_unlock;
894
895 err = ops->gro_complete(skb);
896
897 out_unlock:
898 rcu_read_unlock();
899
900 return err;
901 }
902
903 static struct packet_type ipv6_packet_type = {
904 .type = __constant_htons(ETH_P_IPV6),
905 .func = ipv6_rcv,
906 .gso_send_check = ipv6_gso_send_check,
907 .gso_segment = ipv6_gso_segment,
908 .gro_receive = ipv6_gro_receive,
909 .gro_complete = ipv6_gro_complete,
910 };
911
ipv6_packet_init(void)912 static int __init ipv6_packet_init(void)
913 {
914 dev_add_pack(&ipv6_packet_type);
915 return 0;
916 }
917
ipv6_packet_cleanup(void)918 static void ipv6_packet_cleanup(void)
919 {
920 dev_remove_pack(&ipv6_packet_type);
921 }
922
ipv6_init_mibs(struct net * net)923 static int __net_init ipv6_init_mibs(struct net *net)
924 {
925 if (snmp_mib_init((void **)net->mib.udp_stats_in6,
926 sizeof (struct udp_mib)) < 0)
927 return -ENOMEM;
928 if (snmp_mib_init((void **)net->mib.udplite_stats_in6,
929 sizeof (struct udp_mib)) < 0)
930 goto err_udplite_mib;
931 if (snmp_mib_init((void **)net->mib.ipv6_statistics,
932 sizeof(struct ipstats_mib)) < 0)
933 goto err_ip_mib;
934 if (snmp_mib_init((void **)net->mib.icmpv6_statistics,
935 sizeof(struct icmpv6_mib)) < 0)
936 goto err_icmp_mib;
937 if (snmp_mib_init((void **)net->mib.icmpv6msg_statistics,
938 sizeof(struct icmpv6msg_mib)) < 0)
939 goto err_icmpmsg_mib;
940 return 0;
941
942 err_icmpmsg_mib:
943 snmp_mib_free((void **)net->mib.icmpv6_statistics);
944 err_icmp_mib:
945 snmp_mib_free((void **)net->mib.ipv6_statistics);
946 err_ip_mib:
947 snmp_mib_free((void **)net->mib.udplite_stats_in6);
948 err_udplite_mib:
949 snmp_mib_free((void **)net->mib.udp_stats_in6);
950 return -ENOMEM;
951 }
952
ipv6_cleanup_mibs(struct net * net)953 static void __net_exit ipv6_cleanup_mibs(struct net *net)
954 {
955 snmp_mib_free((void **)net->mib.udp_stats_in6);
956 snmp_mib_free((void **)net->mib.udplite_stats_in6);
957 snmp_mib_free((void **)net->mib.ipv6_statistics);
958 snmp_mib_free((void **)net->mib.icmpv6_statistics);
959 snmp_mib_free((void **)net->mib.icmpv6msg_statistics);
960 }
961
inet6_net_init(struct net * net)962 static int __net_init inet6_net_init(struct net *net)
963 {
964 int err = 0;
965
966 net->ipv6.sysctl.bindv6only = 0;
967 net->ipv6.sysctl.icmpv6_time = 1*HZ;
968
969 err = ipv6_init_mibs(net);
970 if (err)
971 return err;
972 #ifdef CONFIG_PROC_FS
973 err = udp6_proc_init(net);
974 if (err)
975 goto out;
976 err = tcp6_proc_init(net);
977 if (err)
978 goto proc_tcp6_fail;
979 err = ac6_proc_init(net);
980 if (err)
981 goto proc_ac6_fail;
982 #endif
983 return err;
984
985 #ifdef CONFIG_PROC_FS
986 proc_ac6_fail:
987 tcp6_proc_exit(net);
988 proc_tcp6_fail:
989 udp6_proc_exit(net);
990 out:
991 ipv6_cleanup_mibs(net);
992 return err;
993 #endif
994 }
995
inet6_net_exit(struct net * net)996 static void inet6_net_exit(struct net *net)
997 {
998 #ifdef CONFIG_PROC_FS
999 udp6_proc_exit(net);
1000 tcp6_proc_exit(net);
1001 ac6_proc_exit(net);
1002 #endif
1003 ipv6_cleanup_mibs(net);
1004 }
1005
1006 static struct pernet_operations inet6_net_ops = {
1007 .init = inet6_net_init,
1008 .exit = inet6_net_exit,
1009 };
1010
inet6_init(void)1011 static int __init inet6_init(void)
1012 {
1013 struct sk_buff *dummy_skb;
1014 struct list_head *r;
1015 int err = 0;
1016
1017 BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > sizeof(dummy_skb->cb));
1018
1019 /* Register the socket-side information for inet6_create. */
1020 for(r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
1021 INIT_LIST_HEAD(r);
1022
1023 if (disable_ipv6) {
1024 printk(KERN_INFO
1025 "IPv6: Loaded, but administratively disabled, "
1026 "reboot required to enable\n");
1027 goto out;
1028 }
1029
1030 err = proto_register(&tcpv6_prot, 1);
1031 if (err)
1032 goto out;
1033
1034 err = proto_register(&udpv6_prot, 1);
1035 if (err)
1036 goto out_unregister_tcp_proto;
1037
1038 err = proto_register(&udplitev6_prot, 1);
1039 if (err)
1040 goto out_unregister_udp_proto;
1041
1042 err = proto_register(&rawv6_prot, 1);
1043 if (err)
1044 goto out_unregister_udplite_proto;
1045
1046
1047 /* We MUST register RAW sockets before we create the ICMP6,
1048 * IGMP6, or NDISC control sockets.
1049 */
1050 err = rawv6_init();
1051 if (err)
1052 goto out_unregister_raw_proto;
1053
1054 /* Register the family here so that the init calls below will
1055 * be able to create sockets. (?? is this dangerous ??)
1056 */
1057 err = sock_register(&inet6_family_ops);
1058 if (err)
1059 goto out_sock_register_fail;
1060
1061 #ifdef CONFIG_SYSCTL
1062 err = ipv6_static_sysctl_register();
1063 if (err)
1064 goto static_sysctl_fail;
1065 #endif
1066 /*
1067 * ipngwg API draft makes clear that the correct semantics
1068 * for TCP and UDP is to consider one TCP and UDP instance
1069 * in a host availiable by both INET and INET6 APIs and
1070 * able to communicate via both network protocols.
1071 */
1072
1073 err = register_pernet_subsys(&inet6_net_ops);
1074 if (err)
1075 goto register_pernet_fail;
1076 err = icmpv6_init();
1077 if (err)
1078 goto icmp_fail;
1079 err = ip6_mr_init();
1080 if (err)
1081 goto ipmr_fail;
1082 err = ndisc_init();
1083 if (err)
1084 goto ndisc_fail;
1085 err = igmp6_init();
1086 if (err)
1087 goto igmp_fail;
1088 err = ipv6_netfilter_init();
1089 if (err)
1090 goto netfilter_fail;
1091 /* Create /proc/foo6 entries. */
1092 #ifdef CONFIG_PROC_FS
1093 err = -ENOMEM;
1094 if (raw6_proc_init())
1095 goto proc_raw6_fail;
1096 if (udplite6_proc_init())
1097 goto proc_udplite6_fail;
1098 if (ipv6_misc_proc_init())
1099 goto proc_misc6_fail;
1100 if (if6_proc_init())
1101 goto proc_if6_fail;
1102 #endif
1103 err = ip6_route_init();
1104 if (err)
1105 goto ip6_route_fail;
1106 err = ip6_flowlabel_init();
1107 if (err)
1108 goto ip6_flowlabel_fail;
1109 err = addrconf_init();
1110 if (err)
1111 goto addrconf_fail;
1112
1113 /* Init v6 extension headers. */
1114 err = ipv6_exthdrs_init();
1115 if (err)
1116 goto ipv6_exthdrs_fail;
1117
1118 err = ipv6_frag_init();
1119 if (err)
1120 goto ipv6_frag_fail;
1121
1122 /* Init v6 transport protocols. */
1123 err = udpv6_init();
1124 if (err)
1125 goto udpv6_fail;
1126
1127 err = udplitev6_init();
1128 if (err)
1129 goto udplitev6_fail;
1130
1131 err = tcpv6_init();
1132 if (err)
1133 goto tcpv6_fail;
1134
1135 err = ipv6_packet_init();
1136 if (err)
1137 goto ipv6_packet_fail;
1138
1139 #ifdef CONFIG_SYSCTL
1140 err = ipv6_sysctl_register();
1141 if (err)
1142 goto sysctl_fail;
1143 #endif
1144 out:
1145 return err;
1146
1147 #ifdef CONFIG_SYSCTL
1148 sysctl_fail:
1149 ipv6_packet_cleanup();
1150 #endif
1151 ipv6_packet_fail:
1152 tcpv6_exit();
1153 tcpv6_fail:
1154 udplitev6_exit();
1155 udplitev6_fail:
1156 udpv6_exit();
1157 udpv6_fail:
1158 ipv6_frag_exit();
1159 ipv6_frag_fail:
1160 ipv6_exthdrs_exit();
1161 ipv6_exthdrs_fail:
1162 addrconf_cleanup();
1163 addrconf_fail:
1164 ip6_flowlabel_cleanup();
1165 ip6_flowlabel_fail:
1166 ip6_route_cleanup();
1167 ip6_route_fail:
1168 #ifdef CONFIG_PROC_FS
1169 if6_proc_exit();
1170 proc_if6_fail:
1171 ipv6_misc_proc_exit();
1172 proc_misc6_fail:
1173 udplite6_proc_exit();
1174 proc_udplite6_fail:
1175 raw6_proc_exit();
1176 proc_raw6_fail:
1177 #endif
1178 ipv6_netfilter_fini();
1179 netfilter_fail:
1180 igmp6_cleanup();
1181 igmp_fail:
1182 ndisc_cleanup();
1183 ndisc_fail:
1184 ip6_mr_cleanup();
1185 ipmr_fail:
1186 icmpv6_cleanup();
1187 icmp_fail:
1188 unregister_pernet_subsys(&inet6_net_ops);
1189 register_pernet_fail:
1190 #ifdef CONFIG_SYSCTL
1191 ipv6_static_sysctl_unregister();
1192 static_sysctl_fail:
1193 #endif
1194 sock_unregister(PF_INET6);
1195 rtnl_unregister_all(PF_INET6);
1196 out_sock_register_fail:
1197 rawv6_exit();
1198 out_unregister_raw_proto:
1199 proto_unregister(&rawv6_prot);
1200 out_unregister_udplite_proto:
1201 proto_unregister(&udplitev6_prot);
1202 out_unregister_udp_proto:
1203 proto_unregister(&udpv6_prot);
1204 out_unregister_tcp_proto:
1205 proto_unregister(&tcpv6_prot);
1206 goto out;
1207 }
1208 module_init(inet6_init);
1209
inet6_exit(void)1210 static void __exit inet6_exit(void)
1211 {
1212 if (disable_ipv6)
1213 return;
1214
1215 /* First of all disallow new sockets creation. */
1216 sock_unregister(PF_INET6);
1217 /* Disallow any further netlink messages */
1218 rtnl_unregister_all(PF_INET6);
1219
1220 #ifdef CONFIG_SYSCTL
1221 ipv6_sysctl_unregister();
1222 #endif
1223 udpv6_exit();
1224 udplitev6_exit();
1225 tcpv6_exit();
1226
1227 /* Cleanup code parts. */
1228 ipv6_packet_cleanup();
1229 ipv6_frag_exit();
1230 ipv6_exthdrs_exit();
1231 addrconf_cleanup();
1232 ip6_flowlabel_cleanup();
1233 ip6_route_cleanup();
1234 #ifdef CONFIG_PROC_FS
1235
1236 /* Cleanup code parts. */
1237 if6_proc_exit();
1238 ipv6_misc_proc_exit();
1239 udplite6_proc_exit();
1240 raw6_proc_exit();
1241 #endif
1242 ipv6_netfilter_fini();
1243 igmp6_cleanup();
1244 ndisc_cleanup();
1245 ip6_mr_cleanup();
1246 icmpv6_cleanup();
1247 rawv6_exit();
1248
1249 unregister_pernet_subsys(&inet6_net_ops);
1250 #ifdef CONFIG_SYSCTL
1251 ipv6_static_sysctl_unregister();
1252 #endif
1253 proto_unregister(&rawv6_prot);
1254 proto_unregister(&udplitev6_prot);
1255 proto_unregister(&udpv6_prot);
1256 proto_unregister(&tcpv6_prot);
1257 }
1258 module_exit(inet6_exit);
1259
1260 MODULE_ALIAS_NETPROTO(PF_INET6);
1261