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_src.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 /*      $NetBSD: in6_src.c,v 1.18 2003/12/10 11:46:33 itojun Exp $      */
    2 /*      $KAME: in6_src.c,v 1.36 2001/02/06 04:08:17 itojun 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  * Copyright (c) 1982, 1986, 1991, 1993
   35  *      The Regents of the University of California.  All rights reserved.
   36  *
   37  * Redistribution and use in source and binary forms, with or without
   38  * modification, are permitted provided that the following conditions
   39  * are met:
   40  * 1. Redistributions of source code must retain the above copyright
   41  *    notice, this list of conditions and the following disclaimer.
   42  * 2. Redistributions in binary form must reproduce the above copyright
   43  *    notice, this list of conditions and the following disclaimer in the
   44  *    documentation and/or other materials provided with the distribution.
   45  * 3. Neither the name of the University nor the names of its contributors
   46  *    may be used to endorse or promote products derived from this software
   47  *    without specific prior written permission.
   48  *
   49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   59  * SUCH DAMAGE.
   60  *
   61  *      @(#)in_pcb.c    8.2 (Berkeley) 1/4/94
   62  */
   63 
   64 #include <sys/cdefs.h>
   65 __KERNEL_RCSID(0, "$NetBSD: in6_src.c,v 1.18 2003/12/10 11:46:33 itojun Exp $");
   66 
   67 #include "opt_inet.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/protosw.h>
   74 #include <sys/socket.h>
   75 #include <sys/socketvar.h>
   76 #include <sys/ioctl.h>
   77 #include <sys/errno.h>
   78 #include <sys/time.h>
   79 #include <sys/proc.h>
   80 
   81 #include <net/if.h>
   82 #include <net/route.h>
   83 
   84 #include <netinet/in.h>
   85 #include <netinet/in_var.h>
   86 #include <netinet/in_systm.h>
   87 #include <netinet/ip.h>
   88 #include <netinet/in_pcb.h>
   89 #include <netinet6/in6_var.h>
   90 #include <netinet/ip6.h>
   91 #include <netinet6/in6_pcb.h>
   92 #include <netinet6/ip6_var.h>
   93 #include <netinet6/nd6.h>
   94 #ifdef ENABLE_DEFAULT_SCOPE
   95 #include <netinet6/scope6_var.h>
   96 #endif
   97 
   98 #include <net/net_osdep.h>
   99 
  100 #include "loop.h"
  101 extern struct ifnet loif[NLOOP];
  102 
  103 /*
  104  * Return an IPv6 address, which is the most appropriate for a given
  105  * destination and user specified options.
  106  * If necessary, this function lookups the routing table and returns
  107  * an entry to the caller for later use.
  108  */
  109 struct in6_addr *
  110 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
  111         struct sockaddr_in6 *dstsock;
  112         struct ip6_pktopts *opts;
  113         struct ip6_moptions *mopts;
  114         struct route_in6 *ro;
  115         struct in6_addr *laddr;
  116         int *errorp;
  117 {
  118         struct in6_addr *dst;
  119         struct in6_ifaddr *ia6 = 0;
  120         struct in6_pktinfo *pi = NULL;
  121 
  122         dst = &dstsock->sin6_addr;
  123         *errorp = 0;
  124 
  125         /*
  126          * If the source address is explicitly specified by the caller,
  127          * use it.
  128          */
  129         if (opts && (pi = opts->ip6po_pktinfo) &&
  130             !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
  131                 return (&pi->ipi6_addr);
  132 
  133         /*
  134          * If the source address is not specified but the socket(if any)
  135          * is already bound, use the bound address.
  136          */
  137         if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
  138                 return (laddr);
  139 
  140         /*
  141          * If the caller doesn't specify the source address but
  142          * the outgoing interface, use an address associated with
  143          * the interface.
  144          */
  145         if (pi && pi->ipi6_ifindex) {
  146                 /* XXX boundary check is assumed to be already done. */
  147                 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
  148                                        dst);
  149                 if (ia6 == 0) {
  150                         *errorp = EADDRNOTAVAIL;
  151                         return (0);
  152                 }
  153                 return (&satosin6(&ia6->ia_addr)->sin6_addr);
  154         }
  155 
  156         /*
  157          * If the destination address is a link-local unicast address or
  158          * a multicast address, and if the outgoing interface is specified
  159          * by the sin6_scope_id filed, use an address associated with the
  160          * interface.
  161          * XXX: We're now trying to define more specific semantics of
  162          *      sin6_scope_id field, so this part will be rewritten in
  163          *      the near future.
  164          */
  165         if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
  166             dstsock->sin6_scope_id) {
  167                 /*
  168                  * I'm not sure if boundary check for scope_id is done
  169                  * somewhere...
  170                  */
  171                 if (dstsock->sin6_scope_id < 0 ||
  172                     if_indexlim <= dstsock->sin6_scope_id ||
  173                     !ifindex2ifnet[dstsock->sin6_scope_id]) {
  174                         *errorp = ENXIO; /* XXX: better error? */
  175                         return (0);
  176                 }
  177                 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
  178                                        dst);
  179                 if (ia6 == 0) {
  180                         *errorp = EADDRNOTAVAIL;
  181                         return (0);
  182                 }
  183                 return (&satosin6(&ia6->ia_addr)->sin6_addr);
  184         }
  185 
  186         /*
  187          * If the destination address is a multicast address and
  188          * the outgoing interface for the address is specified
  189          * by the caller, use an address associated with the interface.
  190          * There is a sanity check here; if the destination has node-local
  191          * scope, the outgoing interfacde should be a loopback address.
  192          * Even if the outgoing interface is not specified, we also
  193          * choose a loopback interface as the outgoing interface.
  194          */
  195         if (IN6_IS_ADDR_MULTICAST(dst)) {
  196                 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
  197 
  198                 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
  199                         ifp = &loif[0];
  200                 }
  201 
  202                 if (ifp) {
  203                         ia6 = in6_ifawithscope(ifp, dst);
  204                         if (ia6 == 0) {
  205                                 *errorp = EADDRNOTAVAIL;
  206                                 return (0);
  207                         }
  208                         return (&satosin6(&ia6->ia_addr)->sin6_addr);
  209                 }
  210         }
  211 
  212         /*
  213          * If the next hop address for the packet is specified
  214          * by caller, use an address associated with the route
  215          * to the next hop.
  216          */
  217         {
  218                 struct sockaddr_in6 *sin6_next;
  219                 struct rtentry *rt;
  220 
  221                 if (opts && opts->ip6po_nexthop) {
  222                         sin6_next = satosin6(opts->ip6po_nexthop);
  223                         rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
  224                         if (rt) {
  225                                 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
  226                                 if (ia6 == 0)
  227                                         ia6 = ifatoia6(rt->rt_ifa);
  228                         }
  229                         if (ia6 == 0) {
  230                                 *errorp = EADDRNOTAVAIL;
  231                                 return (0);
  232                         }
  233                         return (&satosin6(&ia6->ia_addr)->sin6_addr);
  234                 }
  235         }
  236 
  237         /*
  238          * If route is known or can be allocated now,
  239          * our src addr is taken from the i/f, else punt.
  240          * Note that we should check the address family of the
  241          * cached destination, in case of sharing the cache with IPv4.
  242          */
  243         if (ro) {
  244                 if (ro->ro_rt &&
  245                     (ro->ro_dst.sin6_family != AF_INET6 ||
  246                      !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst))) {
  247                         RTFREE(ro->ro_rt);
  248                         ro->ro_rt = (struct rtentry *)0;
  249                 }
  250                 if (ro->ro_rt == (struct rtentry *)0 ||
  251                     ro->ro_rt->rt_ifp == (struct ifnet *)0) {
  252                         struct sockaddr_in6 *sa6;
  253 
  254                         /* No route yet, so try to acquire one */
  255                         bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
  256                         sa6 = (struct sockaddr_in6 *)&ro->ro_dst;
  257                         sa6->sin6_family = AF_INET6;
  258                         sa6->sin6_len = sizeof(struct sockaddr_in6);
  259                         sa6->sin6_addr = *dst;
  260                         sa6->sin6_scope_id = dstsock->sin6_scope_id;
  261                         if (IN6_IS_ADDR_MULTICAST(dst)) {
  262                                 ro->ro_rt = rtalloc1(&((struct route *)ro)
  263                                                      ->ro_dst, 0);
  264                         } else {
  265                                 rtalloc((struct route *)ro);
  266                         }
  267                 }
  268 
  269                 /*
  270                  * in_pcbconnect() checks out IFF_LOOPBACK to skip using
  271                  * the address. But we don't know why it does so.
  272                  * It is necessary to ensure the scope even for lo0
  273                  * so doesn't check out IFF_LOOPBACK.
  274                  */
  275 
  276                 if (ro->ro_rt) {
  277                         ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
  278                         if (ia6 == 0) /* xxx scope error ?*/
  279                                 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
  280                 }
  281 #if 0
  282                 /*
  283                  * xxx The followings are necessary? (kazu)
  284                  * I don't think so.
  285                  * It's for SO_DONTROUTE option in IPv4.(jinmei)
  286                  */
  287                 if (ia6 == 0) {
  288                         struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
  289 
  290                         sin6->sin6_addr = *dst;
  291 
  292                         ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
  293                         if (ia6 == 0)
  294                                 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
  295                         if (ia6 == 0)
  296                                 return (0);
  297                         return (&satosin6(&ia6->ia_addr)->sin6_addr);
  298                 }
  299 #endif /* 0 */
  300                 if (ia6 == 0) {
  301                         *errorp = EHOSTUNREACH; /* no route */
  302                         return (0);
  303                 }
  304                 return (&satosin6(&ia6->ia_addr)->sin6_addr);
  305         }
  306 
  307         *errorp = EADDRNOTAVAIL;
  308         return (0);
  309 }
  310 
  311 /*
  312  * Default hop limit selection. The precedence is as follows:
  313  * 1. Hoplimit value specified via ioctl.
  314  * 2. (If the outgoing interface is detected) the current
  315  *     hop limit of the interface specified by router advertisement.
  316  * 3. The system default hoplimit.
  317 */
  318 int
  319 in6_selecthlim(in6p, ifp)
  320         struct in6pcb *in6p;
  321         struct ifnet *ifp;
  322 {
  323         if (in6p && in6p->in6p_af != AF_INET6)
  324                 return (-1);
  325 
  326         if (in6p && in6p->in6p_hops >= 0)
  327                 return (in6p->in6p_hops);
  328         else if (ifp)
  329                 return (ND_IFINFO(ifp)->chlim);
  330         else
  331                 return (ip6_defhlim);
  332 }
  333 
  334 /*
  335  * Find an empty port and set it to the specified PCB.
  336  */
  337 int
  338 in6_pcbsetport(laddr, in6p, p)
  339         struct in6_addr *laddr;
  340         struct in6pcb *in6p;
  341         struct proc *p;
  342 {
  343         struct socket *so = in6p->in6p_socket;
  344         struct inpcbtable *table = in6p->in6p_table;
  345         int cnt;
  346         u_int16_t min, max;
  347         u_int16_t lport, *lastport;
  348         int wild = 0;
  349         void *t;
  350 
  351         if (in6p->in6p_af != AF_INET6)
  352                 return (EINVAL);
  353 
  354         /* XXX: this is redundant when called from in6_pcbbind */
  355         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
  356            ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
  357             (so->so_options & SO_ACCEPTCONN) == 0))
  358                 wild = 1;
  359 
  360         if (in6p->in6p_flags & IN6P_LOWPORT) {
  361 #ifndef IPNOPRIVPORTS
  362                 if (p == 0 || (suser(p->p_ucred, &p->p_acflag) != 0))
  363                         return (EACCES);
  364 #endif
  365                 min = ip6_lowportmin;
  366                 max = ip6_lowportmax;
  367                 lastport = &table->inpt_lastlow;
  368         } else {
  369                 min = ip6_anonportmin;
  370                 max = ip6_anonportmax;
  371                 lastport = &table->inpt_lastport;
  372         }
  373         if (min > max) {        /* sanity check */
  374                 u_int16_t swp;
  375 
  376                 swp = min;
  377                 min = max;
  378                 max = swp;
  379         }
  380 
  381         lport = *lastport - 1;
  382         for (cnt = max - min + 1; cnt; cnt--, lport--) {
  383                 if (lport < min || lport > max)
  384                         lport = max;
  385 #ifdef INET
  386                 if (IN6_IS_ADDR_V4MAPPED(laddr)) {
  387                         t = in_pcblookup_port(table,
  388                             *(struct in_addr *)&in6p->in6p_laddr.s6_addr32[3],
  389                             lport, wild);
  390                 } else
  391 #endif
  392                 {
  393                         t = in6_pcblookup_port(table, laddr, lport, wild);
  394                 }
  395                 if (t == 0)
  396                         goto found;
  397         }
  398 
  399         return (EAGAIN);
  400 
  401 found:
  402         in6p->in6p_flags |= IN6P_ANONPORT;
  403         *lastport = lport;
  404         in6p->in6p_lport = htons(lport);
  405         in6_pcbstate(in6p, IN6P_BOUND);
  406         return (0);             /* success */
  407 }
  408 
  409 /*
  410  * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
  411  * If the address scope of is link-local, embed the interface index in the
  412  * address.  The routine determines our precedence
  413  * between advanced API scope/interface specification and basic API
  414  * specification.
  415  *
  416  * this function should be nuked in the future, when we get rid of
  417  * embedded scopeid thing.
  418  *
  419  * XXX actually, it is over-specification to return ifp against sin6_scope_id.
  420  * there can be multiple interfaces that belong to a particular scope zone
  421  * (in specification, we have 1:N mapping between a scope zone and interfaces).
  422  * we may want to change the function to return something other than ifp.
  423  */
  424 int
  425 in6_embedscope(in6, sin6, in6p, ifpp)
  426         struct in6_addr *in6;
  427         const struct sockaddr_in6 *sin6;
  428         struct in6pcb *in6p;
  429         struct ifnet **ifpp;
  430 {
  431         struct ifnet *ifp = NULL;
  432         u_int32_t scopeid;
  433 
  434         *in6 = sin6->sin6_addr;
  435         scopeid = sin6->sin6_scope_id;
  436         if (ifpp)
  437                 *ifpp = NULL;
  438 
  439         /*
  440          * don't try to read sin6->sin6_addr beyond here, since the caller may
  441          * ask us to overwrite existing sockaddr_in6
  442          */
  443 
  444 #ifdef ENABLE_DEFAULT_SCOPE
  445         if (scopeid == 0)
  446                 scopeid = scope6_addr2default(in6);
  447 #endif
  448 
  449         if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
  450                 struct in6_pktinfo *pi;
  451 
  452                 /*
  453                  * KAME assumption: link id == interface id
  454                  */
  455 
  456                 if (in6p && in6p->in6p_outputopts &&
  457                     (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
  458                     pi->ipi6_ifindex) {
  459                         ifp = ifindex2ifnet[pi->ipi6_ifindex];
  460                         in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
  461                 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
  462                            in6p->in6p_moptions &&
  463                            in6p->in6p_moptions->im6o_multicast_ifp) {
  464                         ifp = in6p->in6p_moptions->im6o_multicast_ifp;
  465                         in6->s6_addr16[1] = htons(ifp->if_index);
  466                 } else if (scopeid) {
  467                         /* boundary check */
  468                         if (scopeid < 0 || if_indexlim <= scopeid ||
  469                             !ifindex2ifnet[scopeid])
  470                                 return ENXIO;  /* XXX EINVAL? */
  471                         ifp = ifindex2ifnet[scopeid];
  472                         /* XXX assignment to 16bit from 32bit variable */
  473                         in6->s6_addr16[1] = htons(scopeid & 0xffff);
  474                 }
  475 
  476                 if (ifpp)
  477                         *ifpp = ifp;
  478         }
  479 
  480         return 0;
  481 }
  482 
  483 /*
  484  * generate standard sockaddr_in6 from embedded form.
  485  * touches sin6_addr and sin6_scope_id only.
  486  *
  487  * this function should be nuked in the future, when we get rid of
  488  * embedded scopeid thing.
  489  */
  490 int
  491 in6_recoverscope(sin6, in6, ifp)
  492         struct sockaddr_in6 *sin6;
  493         const struct in6_addr *in6;
  494         struct ifnet *ifp;
  495 {
  496         u_int32_t scopeid;
  497 
  498         sin6->sin6_addr = *in6;
  499 
  500         /*
  501          * don't try to read *in6 beyond here, since the caller may
  502          * ask us to overwrite existing sockaddr_in6
  503          */
  504 
  505         sin6->sin6_scope_id = 0;
  506         if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
  507                 /*
  508                  * KAME assumption: link id == interface id
  509                  */
  510                 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
  511                 if (scopeid) {
  512                         /* sanity check */
  513                         if (scopeid < 0 || if_indexlim <= scopeid ||
  514                             !ifindex2ifnet[scopeid])
  515                                 return ENXIO;
  516                         if (ifp && ifp->if_index != scopeid)
  517                                 return ENXIO;
  518                         sin6->sin6_addr.s6_addr16[1] = 0;
  519                         sin6->sin6_scope_id = scopeid;
  520                 }
  521         }
  522 
  523         return 0;
  524 }
  525 
  526 /*
  527  * just clear the embedded scope identifer.
  528  */
  529 void
  530 in6_clearscope(addr)
  531         struct in6_addr *addr;
  532 {
  533         if (IN6_IS_SCOPE_LINKLOCAL(addr))
  534                 addr->s6_addr16[1] = 0;
  535 }

Cache object: 22cfd10218d3d6edc640274afebbb2c0


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