The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/in6_pcb.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. Neither the name of the project nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
   30  */
   31 
   32 /*-
   33  * Copyright (c) 1982, 1986, 1991, 1993
   34  *      The Regents of the University of California.  All rights reserved.
   35  *
   36  * Redistribution and use in source and binary forms, with or without
   37  * modification, are permitted provided that the following conditions
   38  * are met:
   39  * 1. Redistributions of source code must retain the above copyright
   40  *    notice, this list of conditions and the following disclaimer.
   41  * 2. Redistributions in binary form must reproduce the above copyright
   42  *    notice, this list of conditions and the following disclaimer in the
   43  *    documentation and/or other materials provided with the distribution.
   44  * 4. Neither the name of the University nor the names of its contributors
   45  *    may be used to endorse or promote products derived from this software
   46  *    without specific prior written permission.
   47  *
   48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   58  * SUCH DAMAGE.
   59  *
   60  *      @(#)in_pcb.c    8.2 (Berkeley) 1/4/94
   61  */
   62 
   63 #include <sys/cdefs.h>
   64 __FBSDID("$FreeBSD: releng/6.4/sys/netinet6/in6_pcb.c 182649 2008-09-01 22:57:56Z obrien $");
   65 
   66 #include "opt_inet.h"
   67 #include "opt_inet6.h"
   68 #include "opt_ipsec.h"
   69 #include "opt_mac.h"
   70 
   71 #include <sys/param.h>
   72 #include <sys/systm.h>
   73 #include <sys/mac.h>
   74 #include <sys/malloc.h>
   75 #include <sys/mbuf.h>
   76 #include <sys/domain.h>
   77 #include <sys/protosw.h>
   78 #include <sys/socket.h>
   79 #include <sys/socketvar.h>
   80 #include <sys/sockio.h>
   81 #include <sys/errno.h>
   82 #include <sys/time.h>
   83 #include <sys/proc.h>
   84 #include <sys/jail.h>
   85 
   86 #include <vm/uma.h>
   87 
   88 #include <net/if.h>
   89 #include <net/if_types.h>
   90 #include <net/route.h>
   91 
   92 #include <netinet/in.h>
   93 #include <netinet/in_var.h>
   94 #include <netinet/in_systm.h>
   95 #include <netinet/tcp_var.h>
   96 #include <netinet/ip6.h>
   97 #include <netinet/ip_var.h>
   98 #include <netinet6/ip6_var.h>
   99 #include <netinet6/nd6.h>
  100 #include <netinet/in_pcb.h>
  101 #include <netinet6/in6_pcb.h>
  102 #include <netinet6/scope6_var.h>
  103 
  104 #ifdef IPSEC
  105 #include <netinet6/ipsec.h>
  106 #ifdef INET6
  107 #include <netinet6/ipsec6.h>
  108 #endif
  109 #include <netinet6/ah.h>
  110 #ifdef INET6
  111 #include <netinet6/ah6.h>
  112 #endif
  113 #include <netkey/key.h>
  114 #endif /* IPSEC */
  115 
  116 #ifdef FAST_IPSEC
  117 #include <netipsec/ipsec.h>
  118 #include <netipsec/ipsec6.h>
  119 #include <netipsec/key.h>
  120 #endif /* FAST_IPSEC */
  121 
  122 struct  in6_addr zeroin6_addr;
  123 
  124 int
  125 in6_pcbbind(inp, nam, cred)
  126         register struct inpcb *inp;
  127         struct sockaddr *nam;
  128         struct ucred *cred;
  129 {
  130         struct socket *so = inp->inp_socket;
  131         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
  132         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
  133         u_short lport = 0;
  134         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
  135 
  136         INP_INFO_WLOCK_ASSERT(pcbinfo);
  137         INP_LOCK_ASSERT(inp);
  138 
  139         if (!in6_ifaddr) /* XXX broken! */
  140                 return (EADDRNOTAVAIL);
  141         if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
  142                 return (EINVAL);
  143         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
  144                 wild = 1;
  145         if (nam) {
  146                 int error;
  147 
  148                 sin6 = (struct sockaddr_in6 *)nam;
  149                 if (nam->sa_len != sizeof(*sin6))
  150                         return (EINVAL);
  151                 /*
  152                  * family check.
  153                  */
  154                 if (nam->sa_family != AF_INET6)
  155                         return (EAFNOSUPPORT);
  156 
  157                 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
  158                         return(error);
  159 
  160                 lport = sin6->sin6_port;
  161                 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
  162                         /*
  163                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
  164                          * allow compepte duplication of binding if
  165                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
  166                          * and a multicast address is bound on both
  167                          * new and duplicated sockets.
  168                          */
  169                         if (so->so_options & SO_REUSEADDR)
  170                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
  171                 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  172                         struct ifaddr *ia = NULL;
  173 
  174                         sin6->sin6_port = 0;            /* yech... */
  175                         if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
  176                                 return (EADDRNOTAVAIL);
  177 
  178                         /*
  179                          * XXX: bind to an anycast address might accidentally
  180                          * cause sending a packet with anycast source address.
  181                          * We should allow to bind to a deprecated address, since
  182                          * the application dares to use it.
  183                          */
  184                         if (ia &&
  185                             ((struct in6_ifaddr *)ia)->ia6_flags &
  186                             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
  187                                 return (EADDRNOTAVAIL);
  188                         }
  189                 }
  190                 if (lport) {
  191                         struct inpcb *t;
  192 
  193                         /* GROSS */
  194                         if (ntohs(lport) <= ipport_reservedhigh &&
  195                             ntohs(lport) >= ipport_reservedlow &&
  196                             suser_cred(cred, SUSER_ALLOWJAIL))
  197                                 return (EACCES);
  198                         if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
  199                             suser_cred(so->so_cred, SUSER_ALLOWJAIL) != 0) {
  200                                 t = in6_pcblookup_local(pcbinfo,
  201                                     &sin6->sin6_addr, lport,
  202                                     INPLOOKUP_WILDCARD);
  203                                 if (t &&
  204                                     ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
  205                                     (so->so_type != SOCK_STREAM ||
  206                                      IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
  207                                     (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
  208                                      !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
  209                                      (t->inp_socket->so_options & SO_REUSEPORT) 
  210                                       == 0) && (so->so_cred->cr_uid !=
  211                                      t->inp_socket->so_cred->cr_uid))
  212                                         return (EADDRINUSE);
  213                                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
  214                                     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  215                                         struct sockaddr_in sin;
  216 
  217                                         in6_sin6_2_sin(&sin, sin6);
  218                                         t = in_pcblookup_local(pcbinfo,
  219                                                 sin.sin_addr, lport,
  220                                                 INPLOOKUP_WILDCARD);
  221                                         if (t &&
  222                                             ((t->inp_vflag &
  223                                               INP_TIMEWAIT) == 0) &&
  224                                             (so->so_type != SOCK_STREAM ||
  225                                              ntohl(t->inp_faddr.s_addr) ==
  226                                               INADDR_ANY) &&
  227                                             (so->so_cred->cr_uid !=
  228                                              t->inp_socket->so_cred->cr_uid))
  229                                                 return (EADDRINUSE);
  230                                 }
  231                         }
  232                         t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
  233                                                 lport, wild);
  234                         if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
  235                             intotw(t)->tw_so_options : 
  236                             t->inp_socket->so_options)) == 0)
  237                                 return (EADDRINUSE);
  238                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
  239                             IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
  240                                 struct sockaddr_in sin;
  241 
  242                                 in6_sin6_2_sin(&sin, sin6);
  243                                 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
  244                                                        lport, wild);
  245                                 if (t && t->inp_vflag & INP_TIMEWAIT) {
  246                                         if ((reuseport & 
  247                                             intotw(t)->tw_so_options) == 0 &&
  248                                             (ntohl(t->inp_laddr.s_addr) !=
  249                                              INADDR_ANY || ((inp->inp_vflag & 
  250                                              INP_IPV6PROTO) == 
  251                                              (t->inp_vflag & INP_IPV6PROTO))))
  252                                                 return (EADDRINUSE);
  253                                 }
  254                                 else if (t && 
  255                                     (reuseport & t->inp_socket->so_options) 
  256                                     == 0 && (ntohl(t->inp_laddr.s_addr) != 
  257                                     INADDR_ANY || INP_SOCKAF(so) ==
  258                                      INP_SOCKAF(t->inp_socket)))
  259                                         return (EADDRINUSE);
  260                         }
  261                 }
  262                 inp->in6p_laddr = sin6->sin6_addr;
  263         }
  264         if (lport == 0) {
  265                 int e;
  266                 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
  267                         return (e);
  268         }
  269         else {
  270                 inp->inp_lport = lport;
  271                 if (in_pcbinshash(inp) != 0) {
  272                         inp->in6p_laddr = in6addr_any;
  273                         inp->inp_lport = 0;
  274                         return (EAGAIN);
  275                 }
  276         }
  277         return (0);
  278 }
  279 
  280 /*
  281  *   Transform old in6_pcbconnect() into an inner subroutine for new
  282  *   in6_pcbconnect(): Do some validity-checking on the remote
  283  *   address (in mbuf 'nam') and then determine local host address
  284  *   (i.e., which interface) to use to access that remote host.
  285  *
  286  *   This preserves definition of in6_pcbconnect(), while supporting a
  287  *   slightly different version for T/TCP.  (This is more than
  288  *   a bit of a kludge, but cleaning up the internal interfaces would
  289  *   have forced minor changes in every protocol).
  290  */
  291 
  292 int
  293 in6_pcbladdr(inp, nam, plocal_addr6)
  294         register struct inpcb *inp;
  295         struct sockaddr *nam;
  296         struct in6_addr **plocal_addr6;
  297 {
  298         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
  299         int error = 0;
  300         struct ifnet *ifp = NULL;
  301         int scope_ambiguous = 0;
  302 
  303         if (nam->sa_len != sizeof (*sin6))
  304                 return (EINVAL);
  305         if (sin6->sin6_family != AF_INET6)
  306                 return (EAFNOSUPPORT);
  307         if (sin6->sin6_port == 0)
  308                 return (EADDRNOTAVAIL);
  309 
  310         if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
  311                 scope_ambiguous = 1;
  312         if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
  313                 return(error);
  314 
  315         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  316         INP_LOCK_ASSERT(inp);
  317 
  318         if (in6_ifaddr) {
  319                 /*
  320                  * If the destination address is UNSPECIFIED addr,
  321                  * use the loopback addr, e.g ::1.
  322                  */
  323                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
  324                         sin6->sin6_addr = in6addr_loopback;
  325         }
  326 
  327         /*
  328          * XXX: in6_selectsrc might replace the bound local address
  329          * with the address specified by setsockopt(IPV6_PKTINFO).
  330          * Is it the intended behavior?
  331          */
  332         *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
  333                                       inp->in6p_moptions, NULL,
  334                                       &inp->in6p_laddr, &ifp, &error);
  335         if (ifp && scope_ambiguous &&
  336             (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
  337                 return(error);
  338         }
  339 
  340         if (*plocal_addr6 == 0) {
  341                 if (error == 0)
  342                         error = EADDRNOTAVAIL;
  343                 return (error);
  344         }
  345         /*
  346          * Don't do pcblookup call here; return interface in
  347          * plocal_addr6
  348          * and exit to caller, that will do the lookup.
  349          */
  350 
  351         return (0);
  352 }
  353 
  354 /*
  355  * Outer subroutine:
  356  * Connect from a socket to a specified address.
  357  * Both address and port must be specified in argument sin.
  358  * If don't have a local address for this socket yet,
  359  * then pick one.
  360  */
  361 int
  362 in6_pcbconnect(inp, nam, cred)
  363         register struct inpcb *inp;
  364         struct sockaddr *nam;
  365         struct ucred *cred;
  366 {
  367         struct in6_addr *addr6;
  368         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
  369         int error;
  370 
  371         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  372         INP_LOCK_ASSERT(inp);
  373 
  374         /*
  375          * Call inner routine, to assign local interface address.
  376          * in6_pcbladdr() may automatically fill in sin6_scope_id.
  377          */
  378         if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
  379                 return (error);
  380 
  381         if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
  382                                sin6->sin6_port,
  383                               IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
  384                               ? addr6 : &inp->in6p_laddr,
  385                               inp->inp_lport, 0, NULL) != NULL) {
  386                 return (EADDRINUSE);
  387         }
  388         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
  389                 if (inp->inp_lport == 0) {
  390                         error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
  391                         if (error)
  392                                 return (error);
  393                 }
  394                 inp->in6p_laddr = *addr6;
  395         }
  396         inp->in6p_faddr = sin6->sin6_addr;
  397         inp->inp_fport = sin6->sin6_port;
  398         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
  399         inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
  400         if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
  401                 inp->in6p_flowinfo |=
  402                     (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
  403 
  404         in_pcbrehash(inp);
  405 #ifdef IPSEC
  406         if (inp->inp_socket->so_type == SOCK_STREAM)
  407                 ipsec_pcbconn(inp->inp_sp);
  408 #endif
  409         return (0);
  410 }
  411 
  412 void
  413 in6_pcbdisconnect(inp)
  414         struct inpcb *inp;
  415 {
  416 
  417         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  418         INP_LOCK_ASSERT(inp);
  419 
  420         bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
  421         inp->inp_fport = 0;
  422         /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
  423         inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
  424         in_pcbrehash(inp);
  425 #ifdef IPSEC
  426         ipsec_pcbdisconn(inp->inp_sp);
  427 #endif
  428         if (inp->inp_socket->so_state & SS_NOFDREF)
  429                 in6_pcbdetach(inp);
  430 }
  431 
  432 void
  433 in6_pcbdetach(inp)
  434         struct inpcb *inp;
  435 {
  436         struct socket *so = inp->inp_socket;
  437         struct inpcbinfo *ipi = inp->inp_pcbinfo;
  438 
  439         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  440         INP_LOCK_ASSERT(inp);
  441 
  442 #if defined(IPSEC) || defined(FAST_IPSEC)
  443         if (inp->in6p_sp != NULL)
  444                 ipsec6_delete_pcbpolicy(inp);
  445 #endif /* IPSEC */
  446         inp->inp_gencnt = ++ipi->ipi_gencnt;
  447         in_pcbremlists(inp);
  448 
  449         if (so) {
  450                 ACCEPT_LOCK();
  451                 SOCK_LOCK(so);
  452                 so->so_pcb = NULL;
  453                 sotryfree(so);
  454         }
  455 
  456         ip6_freepcbopts(inp->in6p_outputopts);
  457         ip6_freemoptions(inp->in6p_moptions);
  458         /* Check and free IPv4 related resources in case of mapped addr */
  459         if (inp->inp_options)
  460                 (void)m_free(inp->inp_options);
  461         ip_freemoptions(inp->inp_moptions);
  462         inp->inp_vflag = 0;
  463 #ifdef MAC
  464         mac_destroy_inpcb(inp);
  465 #endif
  466         INP_UNLOCK(inp);
  467         uma_zfree(ipi->ipi_zone, inp);
  468 }
  469 
  470 struct sockaddr *
  471 in6_sockaddr(port, addr_p)
  472         in_port_t port;
  473         struct in6_addr *addr_p;
  474 {
  475         struct sockaddr_in6 *sin6;
  476 
  477         MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
  478         bzero(sin6, sizeof *sin6);
  479         sin6->sin6_family = AF_INET6;
  480         sin6->sin6_len = sizeof(*sin6);
  481         sin6->sin6_port = port;
  482         sin6->sin6_addr = *addr_p;
  483         (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
  484 
  485         return (struct sockaddr *)sin6;
  486 }
  487 
  488 struct sockaddr *
  489 in6_v4mapsin6_sockaddr(port, addr_p)
  490         in_port_t port;
  491         struct in_addr *addr_p;
  492 {
  493         struct sockaddr_in sin;
  494         struct sockaddr_in6 *sin6_p;
  495 
  496         bzero(&sin, sizeof sin);
  497         sin.sin_family = AF_INET;
  498         sin.sin_len = sizeof(sin);
  499         sin.sin_port = port;
  500         sin.sin_addr = *addr_p;
  501 
  502         MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
  503                 M_WAITOK);
  504         in6_sin_2_v4mapsin6(&sin, sin6_p);
  505 
  506         return (struct sockaddr *)sin6_p;
  507 }
  508 
  509 /*
  510  * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
  511  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
  512  * in struct pr_usrreqs, so that protocols can just reference then directly
  513  * without the need for a wrapper function.  The socket must have a valid
  514  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
  515  * except through a kernel programming error, so it is acceptable to panic
  516  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
  517  * because there actually /is/ a programming error somewhere... XXX)
  518  */
  519 int
  520 in6_setsockaddr(so, nam)
  521         struct socket *so;
  522         struct sockaddr **nam;
  523 {
  524         int s;
  525         register struct inpcb *inp;
  526         struct in6_addr addr;
  527         in_port_t port;
  528 
  529         s = splnet();
  530         inp = sotoinpcb(so);
  531         if (!inp) {
  532                 splx(s);
  533                 return EINVAL;
  534         }
  535         port = inp->inp_lport;
  536         addr = inp->in6p_laddr;
  537         splx(s);
  538 
  539         *nam = in6_sockaddr(port, &addr);
  540         return 0;
  541 }
  542 
  543 int
  544 in6_setpeeraddr(so, nam)
  545         struct socket *so;
  546         struct sockaddr **nam;
  547 {
  548         int s;
  549         struct inpcb *inp;
  550         struct in6_addr addr;
  551         in_port_t port;
  552 
  553         s = splnet();
  554         inp = sotoinpcb(so);
  555         if (!inp) {
  556                 splx(s);
  557                 return EINVAL;
  558         }
  559         port = inp->inp_fport;
  560         addr = inp->in6p_faddr;
  561         splx(s);
  562 
  563         *nam = in6_sockaddr(port, &addr);
  564         return 0;
  565 }
  566 
  567 int
  568 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
  569 {
  570         struct  inpcb *inp = sotoinpcb(so);
  571         int     error;
  572 
  573         if (inp == NULL)
  574                 return EINVAL;
  575         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
  576                 error = in_setsockaddr(so, nam, &tcbinfo);
  577                 if (error == 0)
  578                         in6_sin_2_v4mapsin6_in_sock(nam);
  579         } else {
  580                 /* scope issues will be handled in in6_setsockaddr(). */
  581                 error = in6_setsockaddr(so, nam);
  582         }
  583 
  584         return error;
  585 }
  586 
  587 int
  588 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
  589 {
  590         struct  inpcb *inp = sotoinpcb(so);
  591         int     error;
  592 
  593         if (inp == NULL)
  594                 return EINVAL;
  595         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
  596                 error = in_setpeeraddr(so, nam, &tcbinfo);
  597                 if (error == 0)
  598                         in6_sin_2_v4mapsin6_in_sock(nam);
  599         } else
  600         /* scope issues will be handled in in6_setpeeraddr(). */
  601         error = in6_setpeeraddr(so, nam);
  602 
  603         return error;
  604 }
  605 
  606 /*
  607  * Pass some notification to all connections of a protocol
  608  * associated with address dst.  The local address and/or port numbers
  609  * may be specified to limit the search.  The "usual action" will be
  610  * taken, depending on the ctlinput cmd.  The caller must filter any
  611  * cmds that are uninteresting (e.g., no error in the map).
  612  * Call the protocol specific routine (if any) to report
  613  * any errors for each matching socket.
  614  *
  615  * Must be called at splnet.
  616  */
  617 void
  618 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
  619     u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
  620     int cmd, void *cmdarg, struct inpcb *(*notify)(struct inpcb *, int))
  621 {
  622         struct inpcbhead *head;
  623         struct inpcb *inp, *ninp;
  624         struct sockaddr_in6 sa6_src, *sa6_dst;
  625         u_short fport = fport_arg, lport = lport_arg;
  626         u_int32_t flowinfo;
  627         int errno, s;
  628 
  629         if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
  630                 return;
  631 
  632         sa6_dst = (struct sockaddr_in6 *)dst;
  633         if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
  634                 return;
  635 
  636         /*
  637          * note that src can be NULL when we get notify by local fragmentation.
  638          */
  639         sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
  640         flowinfo = sa6_src.sin6_flowinfo;
  641 
  642         /*
  643          * Redirects go to all references to the destination,
  644          * and use in6_rtchange to invalidate the route cache.
  645          * Dead host indications: also use in6_rtchange to invalidate
  646          * the cache, and deliver the error to all the sockets.
  647          * Otherwise, if we have knowledge of the local port and address,
  648          * deliver only to that socket.
  649          */
  650         if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
  651                 fport = 0;
  652                 lport = 0;
  653                 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
  654 
  655                 if (cmd != PRC_HOSTDEAD)
  656                         notify = in6_rtchange;
  657         }
  658         errno = inet6ctlerrmap[cmd];
  659         s = splnet();
  660         head = pcbinfo->listhead;
  661         INP_INFO_WLOCK(pcbinfo);
  662         for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
  663                 INP_LOCK(inp);
  664                 ninp = LIST_NEXT(inp, inp_list);
  665 
  666                 if ((inp->inp_vflag & INP_IPV6) == 0) {
  667                         INP_UNLOCK(inp);
  668                         continue;
  669                 }
  670 
  671                 /*
  672                  * If the error designates a new path MTU for a destination
  673                  * and the application (associated with this socket) wanted to
  674                  * know the value, notify. Note that we notify for all
  675                  * disconnected sockets if the corresponding application
  676                  * wanted. This is because some UDP applications keep sending
  677                  * sockets disconnected.
  678                  * XXX: should we avoid to notify the value to TCP sockets?
  679                  */
  680                 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
  681                     (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
  682                      IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
  683                         ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
  684                                         (u_int32_t *)cmdarg);
  685                 }
  686 
  687                 /*
  688                  * Detect if we should notify the error. If no source and
  689                  * destination ports are specifed, but non-zero flowinfo and
  690                  * local address match, notify the error. This is the case
  691                  * when the error is delivered with an encrypted buffer
  692                  * by ESP. Otherwise, just compare addresses and ports
  693                  * as usual.
  694                  */
  695                 if (lport == 0 && fport == 0 && flowinfo &&
  696                     inp->inp_socket != NULL &&
  697                     flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
  698                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
  699                         goto do_notify;
  700                 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
  701                                              &sa6_dst->sin6_addr) ||
  702                          inp->inp_socket == 0 ||
  703                          (lport && inp->inp_lport != lport) ||
  704                          (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
  705                           !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
  706                                               &sa6_src.sin6_addr)) ||
  707                          (fport && inp->inp_fport != fport)) {
  708                         INP_UNLOCK(inp);
  709                         continue;
  710                 }
  711 
  712           do_notify:
  713                 if (notify) {
  714                         if ((*notify)(inp, errno))
  715                                 INP_UNLOCK(inp);
  716                 } else
  717                         INP_UNLOCK(inp);
  718         }
  719         INP_INFO_WUNLOCK(pcbinfo);
  720         splx(s);
  721 }
  722 
  723 /*
  724  * Lookup a PCB based on the local address and port.
  725  */
  726 struct inpcb *
  727 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
  728         struct inpcbinfo *pcbinfo;
  729         struct in6_addr *laddr;
  730         u_int lport_arg;
  731         int wild_okay;
  732 {
  733         register struct inpcb *inp;
  734         int matchwild = 3, wildcard;
  735         u_short lport = lport_arg;
  736 
  737         if (!wild_okay) {
  738                 struct inpcbhead *head;
  739                 /*
  740                  * Look for an unconnected (wildcard foreign addr) PCB that
  741                  * matches the local address and port we're looking for.
  742                  */
  743                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
  744                                                       pcbinfo->hashmask)];
  745                 LIST_FOREACH(inp, head, inp_hash) {
  746                         if ((inp->inp_vflag & INP_IPV6) == 0)
  747                                 continue;
  748                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
  749                             IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
  750                             inp->inp_lport == lport) {
  751                                 /*
  752                                  * Found.
  753                                  */
  754                                 return (inp);
  755                         }
  756                 }
  757                 /*
  758                  * Not found.
  759                  */
  760                 return (NULL);
  761         } else {
  762                 struct inpcbporthead *porthash;
  763                 struct inpcbport *phd;
  764                 struct inpcb *match = NULL;
  765                 /*
  766                  * Best fit PCB lookup.
  767                  *
  768                  * First see if this local port is in use by looking on the
  769                  * port hash list.
  770                  */
  771                 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
  772                     pcbinfo->porthashmask)];
  773                 LIST_FOREACH(phd, porthash, phd_hash) {
  774                         if (phd->phd_port == lport)
  775                                 break;
  776                 }
  777                 if (phd != NULL) {
  778                         /*
  779                          * Port is in use by one or more PCBs. Look for best
  780                          * fit.
  781                          */
  782                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
  783                                 wildcard = 0;
  784                                 if ((inp->inp_vflag & INP_IPV6) == 0)
  785                                         continue;
  786                                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
  787                                         wildcard++;
  788                                 if (!IN6_IS_ADDR_UNSPECIFIED(
  789                                         &inp->in6p_laddr)) {
  790                                         if (IN6_IS_ADDR_UNSPECIFIED(laddr))
  791                                                 wildcard++;
  792                                         else if (!IN6_ARE_ADDR_EQUAL(
  793                                                 &inp->in6p_laddr, laddr))
  794                                                 continue;
  795                                 } else {
  796                                         if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
  797                                                 wildcard++;
  798                                 }
  799                                 if (wildcard < matchwild) {
  800                                         match = inp;
  801                                         matchwild = wildcard;
  802                                         if (matchwild == 0) {
  803                                                 break;
  804                                         }
  805                                 }
  806                         }
  807                 }
  808                 return (match);
  809         }
  810 }
  811 
  812 void
  813 in6_pcbpurgeif0(head, ifp)
  814         struct in6pcb *head;
  815         struct ifnet *ifp;
  816 {
  817         struct in6pcb *in6p;
  818         struct ip6_moptions *im6o;
  819         struct in6_multi_mship *imm, *nimm;
  820 
  821         for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
  822                 im6o = in6p->in6p_moptions;
  823                 if ((in6p->inp_vflag & INP_IPV6) &&
  824                     im6o) {
  825                         /*
  826                          * Unselect the outgoing interface if it is being
  827                          * detached.
  828                          */
  829                         if (im6o->im6o_multicast_ifp == ifp)
  830                                 im6o->im6o_multicast_ifp = NULL;
  831 
  832                         /*
  833                          * Drop multicast group membership if we joined
  834                          * through the interface being detached.
  835                          * XXX controversial - is it really legal for kernel
  836                          * to force this?
  837                          */
  838                         for (imm = im6o->im6o_memberships.lh_first;
  839                              imm != NULL; imm = nimm) {
  840                                 nimm = imm->i6mm_chain.le_next;
  841                                 if (imm->i6mm_maddr->in6m_ifp == ifp) {
  842                                         LIST_REMOVE(imm, i6mm_chain);
  843                                         in6_delmulti(imm->i6mm_maddr);
  844                                         free(imm, M_IP6MADDR);
  845                                 }
  846                         }
  847                 }
  848         }
  849 }
  850 
  851 /*
  852  * Check for alternatives when higher level complains
  853  * about service problems.  For now, invalidate cached
  854  * routing information.  If the route was created dynamically
  855  * (by a redirect), time to try a default gateway again.
  856  */
  857 void
  858 in6_losing(in6p)
  859         struct inpcb *in6p;
  860 {
  861         /*
  862          * We don't store route pointers in the routing table anymore
  863          */
  864         return;
  865 }
  866 
  867 /*
  868  * After a routing change, flush old routing
  869  * and allocate a (hopefully) better one.
  870  */
  871 struct inpcb *
  872 in6_rtchange(inp, errno)
  873         struct inpcb *inp;
  874         int errno;
  875 {
  876         /*
  877          * We don't store route pointers in the routing table anymore
  878          */
  879         return inp;
  880 }
  881 
  882 /*
  883  * Lookup PCB in hash list.
  884  */
  885 struct inpcb *
  886 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
  887         struct inpcbinfo *pcbinfo;
  888         struct in6_addr *faddr, *laddr;
  889         u_int fport_arg, lport_arg;
  890         int wildcard;
  891         struct ifnet *ifp;
  892 {
  893         struct inpcbhead *head;
  894         register struct inpcb *inp;
  895         u_short fport = fport_arg, lport = lport_arg;
  896         int faith;
  897 
  898         if (faithprefix_p != NULL)
  899                 faith = (*faithprefix_p)(laddr);
  900         else
  901                 faith = 0;
  902 
  903         /*
  904          * First look for an exact match.
  905          */
  906         head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
  907                                               lport, fport,
  908                                               pcbinfo->hashmask)];
  909         LIST_FOREACH(inp, head, inp_hash) {
  910                 if ((inp->inp_vflag & INP_IPV6) == 0)
  911                         continue;
  912                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
  913                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
  914                     inp->inp_fport == fport &&
  915                     inp->inp_lport == lport) {
  916                         /*
  917                          * Found.
  918                          */
  919                         return (inp);
  920                 }
  921         }
  922         if (wildcard) {
  923                 struct inpcb *local_wild = NULL;
  924 
  925                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
  926                                                       pcbinfo->hashmask)];
  927                 LIST_FOREACH(inp, head, inp_hash) {
  928                         if ((inp->inp_vflag & INP_IPV6) == 0)
  929                                 continue;
  930                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
  931                             inp->inp_lport == lport) {
  932                                 if (faith && (inp->inp_flags & INP_FAITH) == 0)
  933                                         continue;
  934                                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
  935                                                        laddr))
  936                                         return (inp);
  937                                 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
  938                                         local_wild = inp;
  939                         }
  940                 }
  941                 return (local_wild);
  942         }
  943 
  944         /*
  945          * Not found.
  946          */
  947         return (NULL);
  948 }
  949 
  950 void
  951 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
  952 {
  953         struct ip6_hdr *ip;
  954 
  955         ip = mtod(m, struct ip6_hdr *);
  956         bzero(sin6, sizeof(*sin6));
  957         sin6->sin6_len = sizeof(*sin6);
  958         sin6->sin6_family = AF_INET6;
  959         sin6->sin6_addr = ip->ip6_src;
  960 
  961         (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
  962 
  963         return;
  964 }

Cache object: 0e7864503fd0f0b58cf151de56cfe727


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.