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-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 31d7f95a7de9dd5b0de6566567c2af8f


[ 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.