~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/af_inet6.c

Version: ~ [ linux-5.9 ] ~ [ linux-5.8.14 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.70 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.150 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.200 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.238 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.238 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/protocol.h>
 53 #include <net/inet_common.h>
 54 #include <net/route.h>
 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>
 63 #include <linux/mroute6.h>
 64 
 65 MODULE_AUTHOR("Cast of dozens");
 66 MODULE_DESCRIPTION("IPv6 protocol stack for Linux");
 67 MODULE_LICENSE("GPL");
 68 
 69 /* The inetsw6 table contains everything that inet6_create needs to
 70  * build a new socket.
 71  */
 72 static struct list_head inetsw6[SOCK_MAX];
 73 static DEFINE_SPINLOCK(inetsw6_lock);
 74 
 75 struct ipv6_params ipv6_defaults = {
 76         .disable_ipv6 = 0,
 77         .autoconf = 1,
 78 };
 79 
 80 static int disable_ipv6_mod;
 81 
 82 module_param_named(disable, disable_ipv6_mod, int, 0444);
 83 MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional");
 84 
 85 module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444);
 86 MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces");
 87 
 88 module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444);
 89 MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces");
 90 
 91 static __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 
 98 static int inet6_create(struct net *net, struct socket *sock, int protocol,
 99                         int kern)
100 {
101         struct inet_sock *inet;
102         struct ipv6_pinfo *np;
103         struct sock *sk;
104         struct inet_protosw *answer;
105         struct proto *answer_prot;
106         unsigned char answer_flags;
107         char answer_no_check;
108         int try_loading_module = 0;
109         int err;
110 
111         if (sock->type != SOCK_RAW &&
112             sock->type != SOCK_DGRAM &&
113             !inet_ehash_secret)
114                 build_ehash_secret();
115 
116         if (protocol < 0 || protocol >= IPPROTO_MAX)
117                 return -EINVAL;
118 
119         /* Look for the requested type/protocol pair. */
120 lookup_protocol:
121         err = -ESOCKTNOSUPPORT;
122         rcu_read_lock();
123         list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) {
124 
125                 err = 0;
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                 }
139                 err = -EPROTONOSUPPORT;
140         }
141 
142         if (err) {
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;
165         if (sock->type == SOCK_RAW && !kern &&
166             !ns_capable(net->user_ns, CAP_NET_RAW))
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 = SK_CAN_REUSE;
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->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  = IPV6_DEFAULT_MCASTHOPS;
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         inet->rcv_tos   = 0;
221 
222         if (ipv4_config.no_pmtu_disc)
223                 inet->pmtudisc = IP_PMTUDISC_DONT;
224         else
225                 inet->pmtudisc = IP_PMTUDISC_WANT;
226         /*
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);
236 
237         if (inet->inet_num) {
238                 /* It assumes that any protocol which allows
239                  * the user to assign a number at socket
240                  * creation time automatically shares.
241                  */
242                 inet->inet_sport = htons(inet->inet_num);
243                 sk->sk_prot->hash(sk);
244         }
245         if (sk->sk_prot->init) {
246                 err = sk->sk_prot->init(sk);
247                 if (err) {
248                         sk_common_release(sk);
249                         goto out;
250                 }
251         }
252 out:
253         return err;
254 out_rcu_unlock:
255         rcu_read_unlock();
256         goto out;
257 }
258 
259 
260 /* bind for INET6 API */
261 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
262 {
263         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr;
264         struct sock *sk = sock->sk;
265         struct inet_sock *inet = inet_sk(sk);
266         struct ipv6_pinfo *np = inet6_sk(sk);
267         struct net *net = sock_net(sk);
268         __be32 v4addr = 0;
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;
279 
280         if (addr->sin6_family != AF_INET6)
281                 return -EAFNOSUPPORT;
282 
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);
288         if (snum && snum < PROT_SOCK && !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
289                 return -EACCES;
290 
291         lock_sock(sk);
292 
293         /* Check these errors (active socket, double bind). */
294         if (sk->sk_state != TCP_CLOSE || inet->inet_num) {
295                 err = -EINVAL;
296                 goto out;
297         }
298 
299         /* Check if the address belongs to the host. */
300         if (addr_type == IPV6_ADDR_MAPPED) {
301                 int chk_addr_ret;
302 
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                 }
310 
311                 /* Reproduce AF_INET checks to make the bindings consistent */
312                 v4addr = addr->sin6_addr.s6_addr32[3];
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 &&
319                     chk_addr_ret != RTN_BROADCAST) {
320                         err = -EADDRNOTAVAIL;
321                         goto out;
322                 }
323         } else {
324                 if (addr_type != IPV6_ADDR_ANY) {
325                         struct net_device *dev = NULL;
326 
327                         rcu_read_lock();
328                         if (__ipv6_addr_needs_scope_id(addr_type)) {
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                                 }
336 
337                                 /* Binding to link-local address requires an interface */
338                                 if (!sk->sk_bound_dev_if) {
339                                         err = -EINVAL;
340                                         goto out_unlock;
341                                 }
342                                 dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
343                                 if (!dev) {
344                                         err = -ENODEV;
345                                         goto out_unlock;
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)) {
354                                 if (!(inet->freebind || inet->transparent) &&
355                                     !ipv6_chk_addr(net, &addr->sin6_addr,
356                                                    dev, 0)) {
357                                         err = -EADDRNOTAVAIL;
358                                         goto out_unlock;
359                                 }
360                         }
361                         rcu_read_unlock();
362                 }
363         }
364 
365         inet->inet_rcv_saddr = v4addr;
366         inet->inet_saddr = v4addr;
367 
368         np->rcv_saddr = addr->sin6_addr;
369 
370         if (!(addr_type & IPV6_ADDR_MULTICAST))
371                 np->saddr = addr->sin6_addr;
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 
380         if (addr_type != IPV6_ADDR_ANY) {
381                 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
382                 if (addr_type != IPV6_ADDR_MAPPED)
383                         np->ipv6only = 1;
384         }
385         if (snum)
386                 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
387         inet->inet_sport = htons(inet->inet_num);
388         inet->inet_dport = 0;
389         inet->inet_daddr = 0;
390 out:
391         release_sock(sk);
392         return err;
393 out_unlock:
394         rcu_read_unlock();
395         goto out;
396 }
397 EXPORT_SYMBOL(inet6_bind);
398 
399 int 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 }
414 EXPORT_SYMBOL(inet6_release);
415 
416 void inet6_destroy_sock(struct sock *sk)
417 {
418         struct ipv6_pinfo *np = inet6_sk(sk);
419         struct sk_buff *skb;
420         struct ipv6_txoptions *opt;
421 
422         /* Release rx options */
423 
424         skb = xchg(&np->pktoptions, NULL);
425         if (skb != NULL)
426                 kfree_skb(skb);
427 
428         skb = xchg(&np->rxpmtu, NULL);
429         if (skb != NULL)
430                 kfree_skb(skb);
431 
432         /* Free flowlabels */
433         fl6_free_socklist(sk);
434 
435         /* Free tx options */
436 
437         opt = xchg(&np->opt, NULL);
438         if (opt != NULL)
439                 sock_kfree_s(sk, opt, opt->tot_len);
440 }
441 EXPORT_SYMBOL_GPL(inet6_destroy_sock);
442 
443 /*
444  *      This does both peername and sockname.
445  */
446 
447 int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
448                  int *uaddr_len, int peer)
449 {
450         struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr;
451         struct sock *sk = sock->sk;
452         struct inet_sock *inet = inet_sk(sk);
453         struct ipv6_pinfo *np = inet6_sk(sk);
454 
455         sin->sin6_family = AF_INET6;
456         sin->sin6_flowinfo = 0;
457         sin->sin6_scope_id = 0;
458         if (peer) {
459                 if (!inet->inet_dport)
460                         return -ENOTCONN;
461                 if (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
462                     peer == 1)
463                         return -ENOTCONN;
464                 sin->sin6_port = inet->inet_dport;
465                 sin->sin6_addr = np->daddr;
466                 if (np->sndflow)
467                         sin->sin6_flowinfo = np->flow_label;
468         } else {
469                 if (ipv6_addr_any(&np->rcv_saddr))
470                         sin->sin6_addr = np->saddr;
471                 else
472                         sin->sin6_addr = np->rcv_saddr;
473 
474                 sin->sin6_port = inet->inet_sport;
475         }
476         sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr,
477                                                  sk->sk_bound_dev_if);
478         *uaddr_len = sizeof(*sin);
479         return 0;
480 }
481 EXPORT_SYMBOL(inet6_getname);
482 
483 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
484 {
485         struct sock *sk = sock->sk;
486         struct net *net = sock_net(sk);
487 
488         switch (cmd) {
489         case SIOCGSTAMP:
490                 return sock_get_timestamp(sk, (struct timeval __user *)arg);
491 
492         case SIOCGSTAMPNS:
493                 return sock_get_timestampns(sk, (struct timespec __user *)arg);
494 
495         case SIOCADDRT:
496         case SIOCDELRT:
497 
498                 return ipv6_route_ioctl(net, cmd, (void __user *)arg);
499 
500         case SIOCSIFADDR:
501                 return addrconf_add_ifaddr(net, (void __user *) arg);
502         case SIOCDIFADDR:
503                 return addrconf_del_ifaddr(net, (void __user *) arg);
504         case SIOCSIFDSTADDR:
505                 return addrconf_set_dstaddr(net, (void __user *) arg);
506         default:
507                 if (!sk->sk_prot->ioctl)
508                         return -ENOIOCTLCMD;
509                 return sk->sk_prot->ioctl(sk, cmd, arg);
510         }
511         /*NOTREACHED*/
512         return 0;
513 }
514 EXPORT_SYMBOL(inet6_ioctl);
515 
516 const struct proto_ops inet6_stream_ops = {
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           */
531         .sendmsg           = inet_sendmsg,              /* ok           */
532         .recvmsg           = inet_recvmsg,              /* ok           */
533         .mmap              = sock_no_mmap,
534         .sendpage          = inet_sendpage,
535         .splice_read       = tcp_splice_read,
536 #ifdef CONFIG_COMPAT
537         .compat_setsockopt = compat_sock_common_setsockopt,
538         .compat_getsockopt = compat_sock_common_getsockopt,
539 #endif
540 };
541 
542 const struct proto_ops inet6_dgram_ops = {
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           */
558         .recvmsg           = inet_recvmsg,              /* ok           */
559         .mmap              = sock_no_mmap,
560         .sendpage          = sock_no_sendpage,
561 #ifdef CONFIG_COMPAT
562         .compat_setsockopt = compat_sock_common_setsockopt,
563         .compat_getsockopt = compat_sock_common_getsockopt,
564 #endif
565 };
566 
567 static const struct net_proto_family inet6_family_ops = {
568         .family = PF_INET6,
569         .create = inet6_create,
570         .owner  = THIS_MODULE,
571 };
572 
573 int inet6_register_protosw(struct inet_protosw *p)
574 {
575         struct list_head *lh;
576         struct inet_protosw *answer;
577         struct list_head *last_perm;
578         int protocol = p->protocol;
579         int ret;
580 
581         spin_lock_bh(&inetsw6_lock);
582 
583         ret = -EINVAL;
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;
589         ret = -EPERM;
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
609          * non-permanent entry.  This means that when we remove this entry, the
610          * system automatically returns to the old behavior.
611          */
612         list_add_rcu(&p->list, last_perm);
613         ret = 0;
614 out:
615         spin_unlock_bh(&inetsw6_lock);
616         return ret;
617 
618 out_permanent:
619         pr_err("Attempt to override permanent protocol %d\n", protocol);
620         goto out;
621 
622 out_illegal:
623         pr_err("Ignoring attempt to register invalid socket type %d\n",
624                p->type);
625         goto out;
626 }
627 EXPORT_SYMBOL(inet6_register_protosw);
628 
629 void
630 inet6_unregister_protosw(struct inet_protosw *p)
631 {
632         if (INET_PROTOSW_PERMANENT & p->flags) {
633                 pr_err("Attempt to unregister permanent protocol %d\n",
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 }
643 EXPORT_SYMBOL(inet6_unregister_protosw);
644 
645 int inet6_sk_rebuild_header(struct sock *sk)
646 {
647         struct ipv6_pinfo *np = inet6_sk(sk);
648         struct dst_entry *dst;
649 
650         dst = __sk_dst_check(sk, np->dst_cookie);
651 
652         if (dst == NULL) {
653                 struct inet_sock *inet = inet_sk(sk);
654                 struct in6_addr *final_p, final;
655                 struct flowi6 fl6;
656 
657                 memset(&fl6, 0, sizeof(fl6));
658                 fl6.flowi6_proto = sk->sk_protocol;
659                 fl6.daddr = np->daddr;
660                 fl6.saddr = np->saddr;
661                 fl6.flowlabel = np->flow_label;
662                 fl6.flowi6_oif = sk->sk_bound_dev_if;
663                 fl6.flowi6_mark = sk->sk_mark;
664                 fl6.fl6_dport = inet->inet_dport;
665                 fl6.fl6_sport = inet->inet_sport;
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);
671                 if (IS_ERR(dst)) {
672                         sk->sk_route_caps = 0;
673                         sk->sk_err_soft = -PTR_ERR(dst);
674                         return PTR_ERR(dst);
675                 }
676 
677                 __ip6_dst_store(sk, dst, NULL, NULL);
678         }
679 
680         return 0;
681 }
682 EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header);
683 
684 bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb)
685 {
686         const struct ipv6_pinfo *np = inet6_sk(sk);
687         const struct inet6_skb_parm *opt = IP6CB(skb);
688 
689         if (np->rxopt.all) {
690                 if ((opt->hop && (np->rxopt.bits.hopopts ||
691                                   np->rxopt.bits.ohopopts)) ||
692                     ((IPV6_FLOWINFO_MASK &
693                       *(__be32 *)skb_network_header(skb)) &&
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)))
699                         return true;
700         }
701         return false;
702 }
703 EXPORT_SYMBOL_GPL(ipv6_opt_accepted);
704 
705 static struct packet_type ipv6_packet_type __read_mostly = {
706         .type = cpu_to_be16(ETH_P_IPV6),
707         .func = ipv6_rcv,
708 };
709 
710 static int __init ipv6_packet_init(void)
711 {
712         dev_add_pack(&ipv6_packet_type);
713         return 0;
714 }
715 
716 static void ipv6_packet_cleanup(void)
717 {
718         dev_remove_pack(&ipv6_packet_type);
719 }
720 
721 static int __net_init ipv6_init_mibs(struct net *net)
722 {
723         if (snmp_mib_init((void __percpu **)net->mib.udp_stats_in6,
724                           sizeof(struct udp_mib),
725                           __alignof__(struct udp_mib)) < 0)
726                 return -ENOMEM;
727         if (snmp_mib_init((void __percpu **)net->mib.udplite_stats_in6,
728                           sizeof(struct udp_mib),
729                           __alignof__(struct udp_mib)) < 0)
730                 goto err_udplite_mib;
731         if (snmp_mib_init((void __percpu **)net->mib.ipv6_statistics,
732                           sizeof(struct ipstats_mib),
733                           __alignof__(struct ipstats_mib)) < 0)
734                 goto err_ip_mib;
735         if (snmp_mib_init((void __percpu **)net->mib.icmpv6_statistics,
736                           sizeof(struct icmpv6_mib),
737                           __alignof__(struct icmpv6_mib)) < 0)
738                 goto err_icmp_mib;
739         net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib),
740                                                 GFP_KERNEL);
741         if (!net->mib.icmpv6msg_statistics)
742                 goto err_icmpmsg_mib;
743         return 0;
744 
745 err_icmpmsg_mib:
746         snmp_mib_free((void __percpu **)net->mib.icmpv6_statistics);
747 err_icmp_mib:
748         snmp_mib_free((void __percpu **)net->mib.ipv6_statistics);
749 err_ip_mib:
750         snmp_mib_free((void __percpu **)net->mib.udplite_stats_in6);
751 err_udplite_mib:
752         snmp_mib_free((void __percpu **)net->mib.udp_stats_in6);
753         return -ENOMEM;
754 }
755 
756 static void ipv6_cleanup_mibs(struct net *net)
757 {
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);
762         kfree(net->mib.icmpv6msg_statistics);
763 }
764 
765 static int __net_init inet6_net_init(struct net *net)
766 {
767         int err = 0;
768 
769         net->ipv6.sysctl.bindv6only = 0;
770         net->ipv6.sysctl.icmpv6_time = 1*HZ;
771 
772         err = ipv6_init_mibs(net);
773         if (err)
774                 return err;
775 #ifdef CONFIG_PROC_FS
776         err = udp6_proc_init(net);
777         if (err)
778                 goto out;
779         err = tcp6_proc_init(net);
780         if (err)
781                 goto proc_tcp6_fail;
782         err = ac6_proc_init(net);
783         if (err)
784                 goto proc_ac6_fail;
785 #endif
786         return err;
787 
788 #ifdef CONFIG_PROC_FS
789 proc_ac6_fail:
790         tcp6_proc_exit(net);
791 proc_tcp6_fail:
792         udp6_proc_exit(net);
793 out:
794         ipv6_cleanup_mibs(net);
795         return err;
796 #endif
797 }
798 
799 static void __net_exit inet6_net_exit(struct net *net)
800 {
801 #ifdef CONFIG_PROC_FS
802         udp6_proc_exit(net);
803         tcp6_proc_exit(net);
804         ac6_proc_exit(net);
805 #endif
806         ipv6_cleanup_mibs(net);
807 }
808 
809 static struct pernet_operations inet6_net_ops = {
810         .init = inet6_net_init,
811         .exit = inet6_net_exit,
812 };
813 
814 static int __init inet6_init(void)
815 {
816         struct list_head *r;
817         int err = 0;
818 
819         BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > FIELD_SIZEOF(struct sk_buff, cb));
820 
821         /* Register the socket-side information for inet6_create.  */
822         for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
823                 INIT_LIST_HEAD(r);
824 
825         if (disable_ipv6_mod) {
826                 pr_info("Loaded, but administratively disabled, reboot required to enable\n");
827                 goto out;
828         }
829 
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 
838         err = proto_register(&udplitev6_prot, 1);
839         if (err)
840                 goto out_unregister_udp_proto;
841 
842         err = proto_register(&rawv6_prot, 1);
843         if (err)
844                 goto out_unregister_udplite_proto;
845 
846 
847         /* We MUST register RAW sockets before we create the ICMP6,
848          * IGMP6, or NDISC control sockets.
849          */
850         err = rawv6_init();
851         if (err)
852                 goto out_unregister_raw_proto;
853 
854         /* Register the family here so that the init calls below will
855          * be able to create sockets. (?? is this dangerous ??)
856          */
857         err = sock_register(&inet6_family_ops);
858         if (err)
859                 goto out_sock_register_fail;
860 
861         tcpv6_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;
862 
863         /*
864          *      ipngwg API draft makes clear that the correct semantics
865          *      for TCP and UDP is to consider one TCP and UDP instance
866          *      in a host available by both INET and INET6 APIs and
867          *      able to communicate via both network protocols.
868          */
869 
870         err = register_pernet_subsys(&inet6_net_ops);
871         if (err)
872                 goto register_pernet_fail;
873         err = icmpv6_init();
874         if (err)
875                 goto icmp_fail;
876         err = ip6_mr_init();
877         if (err)
878                 goto ipmr_fail;
879         err = ndisc_init();
880         if (err)
881                 goto ndisc_fail;
882         err = igmp6_init();
883         if (err)
884                 goto igmp_fail;
885         err = ipv6_netfilter_init();
886         if (err)
887                 goto netfilter_fail;
888         /* Create /proc/foo6 entries. */
889 #ifdef CONFIG_PROC_FS
890         err = -ENOMEM;
891         if (raw6_proc_init())
892                 goto proc_raw6_fail;
893         if (udplite6_proc_init())
894                 goto proc_udplite6_fail;
895         if (ipv6_misc_proc_init())
896                 goto proc_misc6_fail;
897         if (if6_proc_init())
898                 goto proc_if6_fail;
899 #endif
900         err = ip6_route_init();
901         if (err)
902                 goto ip6_route_fail;
903         err = ndisc_late_init();
904         if (err)
905                 goto ndisc_late_fail;
906         err = ip6_flowlabel_init();
907         if (err)
908                 goto ip6_flowlabel_fail;
909         err = addrconf_init();
910         if (err)
911                 goto addrconf_fail;
912 
913         /* Init v6 extension headers. */
914         err = ipv6_exthdrs_init();
915         if (err)
916                 goto ipv6_exthdrs_fail;
917 
918         err = ipv6_frag_init();
919         if (err)
920                 goto ipv6_frag_fail;
921 
922         /* Init v6 transport protocols. */
923         err = udpv6_init();
924         if (err)
925                 goto udpv6_fail;
926 
927         err = udplitev6_init();
928         if (err)
929                 goto udplitev6_fail;
930 
931         err = tcpv6_init();
932         if (err)
933                 goto tcpv6_fail;
934 
935         err = ipv6_packet_init();
936         if (err)
937                 goto ipv6_packet_fail;
938 
939 #ifdef CONFIG_SYSCTL
940         err = ipv6_sysctl_register();
941         if (err)
942                 goto sysctl_fail;
943 #endif
944 out:
945         return err;
946 
947 #ifdef CONFIG_SYSCTL
948 sysctl_fail:
949         ipv6_packet_cleanup();
950 #endif
951 ipv6_packet_fail:
952         tcpv6_exit();
953 tcpv6_fail:
954         udplitev6_exit();
955 udplitev6_fail:
956         udpv6_exit();
957 udpv6_fail:
958         ipv6_frag_exit();
959 ipv6_frag_fail:
960         ipv6_exthdrs_exit();
961 ipv6_exthdrs_fail:
962         addrconf_cleanup();
963 addrconf_fail:
964         ip6_flowlabel_cleanup();
965 ip6_flowlabel_fail:
966         ndisc_late_cleanup();
967 ndisc_late_fail:
968         ip6_route_cleanup();
969 ip6_route_fail:
970 #ifdef CONFIG_PROC_FS
971         if6_proc_exit();
972 proc_if6_fail:
973         ipv6_misc_proc_exit();
974 proc_misc6_fail:
975         udplite6_proc_exit();
976 proc_udplite6_fail:
977         raw6_proc_exit();
978 proc_raw6_fail:
979 #endif
980         ipv6_netfilter_fini();
981 netfilter_fail:
982         igmp6_cleanup();
983 igmp_fail:
984         ndisc_cleanup();
985 ndisc_fail:
986         ip6_mr_cleanup();
987 ipmr_fail:
988         icmpv6_cleanup();
989 icmp_fail:
990         unregister_pernet_subsys(&inet6_net_ops);
991 register_pernet_fail:
992         sock_unregister(PF_INET6);
993         rtnl_unregister_all(PF_INET6);
994 out_sock_register_fail:
995         rawv6_exit();
996 out_unregister_raw_proto:
997         proto_unregister(&rawv6_prot);
998 out_unregister_udplite_proto:
999         proto_unregister(&udplitev6_prot);
1000 out_unregister_udp_proto:
1001         proto_unregister(&udpv6_prot);
1002 out_unregister_tcp_proto:
1003         proto_unregister(&tcpv6_prot);
1004         goto out;
1005 }
1006 module_init(inet6_init);
1007 
1008 static void __exit inet6_exit(void)
1009 {
1010         if (disable_ipv6_mod)
1011                 return;
1012 
1013         /* First of all disallow new sockets creation. */
1014         sock_unregister(PF_INET6);
1015         /* Disallow any further netlink messages */
1016         rtnl_unregister_all(PF_INET6);
1017 
1018         udpv6_exit();
1019         udplitev6_exit();
1020         tcpv6_exit();
1021 
1022         /* Cleanup code parts. */
1023         ipv6_packet_cleanup();
1024         ipv6_frag_exit();
1025         ipv6_exthdrs_exit();
1026         addrconf_cleanup();
1027         ip6_flowlabel_cleanup();
1028         ndisc_late_cleanup();
1029         ip6_route_cleanup();
1030 #ifdef CONFIG_PROC_FS
1031 
1032         /* Cleanup code parts. */
1033         if6_proc_exit();
1034         ipv6_misc_proc_exit();
1035         udplite6_proc_exit();
1036         raw6_proc_exit();
1037 #endif
1038         ipv6_netfilter_fini();
1039         igmp6_cleanup();
1040         ndisc_cleanup();
1041         ip6_mr_cleanup();
1042         icmpv6_cleanup();
1043         rawv6_exit();
1044 
1045         unregister_pernet_subsys(&inet6_net_ops);
1046         proto_unregister(&rawv6_prot);
1047         proto_unregister(&udplitev6_prot);
1048         proto_unregister(&udpv6_prot);
1049         proto_unregister(&tcpv6_prot);
1050 
1051         rcu_barrier(); /* Wait for completion of call_rcu()'s */
1052 }
1053 module_exit(inet6_exit);
1054 
1055 MODULE_ALIAS_NETPROTO(PF_INET6);
1056 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp