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

Cache object: 5718153cbf3aabb77e193db794e20a84


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