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/netinet/in.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 /*-
    2  * Copyright (c) 1982, 1986, 1991, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  * Copyright (C) 2001 WIDE Project.  All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 4. Neither the name of the University nor the names of its contributors
   15  *    may be used to endorse or promote products derived from this software
   16  *    without specific prior written permission.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   28  * SUCH DAMAGE.
   29  *
   30  *      @(#)in.c        8.4 (Berkeley) 1/9/95
   31  * $FreeBSD: releng/6.0/sys/netinet/in.c 151380 2005-10-16 17:12:18Z rwatson $
   32  */
   33 
   34 #include "opt_carp.h"
   35 
   36 #include <sys/param.h>
   37 #include <sys/systm.h>
   38 #include <sys/sockio.h>
   39 #include <sys/malloc.h>
   40 #include <sys/socket.h>
   41 #include <sys/kernel.h>
   42 #include <sys/sysctl.h>
   43 
   44 #include <net/if.h>
   45 #include <net/if_types.h>
   46 #include <net/route.h>
   47 
   48 #include <netinet/in.h>
   49 #include <netinet/in_var.h>
   50 #include <netinet/in_pcb.h>
   51 
   52 #include <netinet/igmp_var.h>
   53 
   54 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
   55 
   56 static int in_mask2len(struct in_addr *);
   57 static void in_len2mask(struct in_addr *, int);
   58 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
   59         struct ifnet *, struct thread *);
   60 
   61 static int      in_addprefix(struct in_ifaddr *, int);
   62 static int      in_scrubprefix(struct in_ifaddr *);
   63 static void     in_socktrim(struct sockaddr_in *);
   64 static int      in_ifinit(struct ifnet *,
   65             struct in_ifaddr *, struct sockaddr_in *, int);
   66 
   67 static int subnetsarelocal = 0;
   68 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
   69         &subnetsarelocal, 0, "Treat all subnets as directly connected");
   70 static int sameprefixcarponly = 0;
   71 SYSCTL_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
   72         &sameprefixcarponly, 0,
   73         "Refuse to create same prefixes on different interfaces");
   74 
   75 /*
   76  * The IPv4 multicast list (in_multihead and associated structures) are
   77  * protected by the global in_multi_mtx.  See in_var.h for more details.  For
   78  * now, in_multi_mtx is marked as recursible due to IGMP's calling back into
   79  * ip_output() to send IGMP packets while holding the lock; this probably is
   80  * not quite desirable.
   81  */
   82 struct in_multihead in_multihead; /* XXX BSS initialization */
   83 struct mtx in_multi_mtx;
   84 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF | MTX_RECURSE);
   85 
   86 extern struct inpcbinfo ripcbinfo;
   87 extern struct inpcbinfo udbinfo;
   88 
   89 /*
   90  * Return 1 if an internet address is for a ``local'' host
   91  * (one to which we have a connection).  If subnetsarelocal
   92  * is true, this includes other subnets of the local net.
   93  * Otherwise, it includes only the directly-connected (sub)nets.
   94  */
   95 int
   96 in_localaddr(in)
   97         struct in_addr in;
   98 {
   99         register u_long i = ntohl(in.s_addr);
  100         register struct in_ifaddr *ia;
  101 
  102         if (subnetsarelocal) {
  103                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
  104                         if ((i & ia->ia_netmask) == ia->ia_net)
  105                                 return (1);
  106         } else {
  107                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
  108                         if ((i & ia->ia_subnetmask) == ia->ia_subnet)
  109                                 return (1);
  110         }
  111         return (0);
  112 }
  113 
  114 /*
  115  * Return 1 if an internet address is for the local host and configured
  116  * on one of its interfaces.
  117  */
  118 int
  119 in_localip(in)
  120         struct in_addr in;
  121 {
  122         struct in_ifaddr *ia;
  123 
  124         LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
  125                 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
  126                         return 1;
  127         }
  128         return 0;
  129 }
  130 
  131 /*
  132  * Determine whether an IP address is in a reserved set of addresses
  133  * that may not be forwarded, or whether datagrams to that destination
  134  * may be forwarded.
  135  */
  136 int
  137 in_canforward(in)
  138         struct in_addr in;
  139 {
  140         register u_long i = ntohl(in.s_addr);
  141         register u_long net;
  142 
  143         if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
  144                 return (0);
  145         if (IN_CLASSA(i)) {
  146                 net = i & IN_CLASSA_NET;
  147                 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
  148                         return (0);
  149         }
  150         return (1);
  151 }
  152 
  153 /*
  154  * Trim a mask in a sockaddr
  155  */
  156 static void
  157 in_socktrim(ap)
  158 struct sockaddr_in *ap;
  159 {
  160     register char *cplim = (char *) &ap->sin_addr;
  161     register char *cp = (char *) (&ap->sin_addr + 1);
  162 
  163     ap->sin_len = 0;
  164     while (--cp >= cplim)
  165         if (*cp) {
  166             (ap)->sin_len = cp - (char *) (ap) + 1;
  167             break;
  168         }
  169 }
  170 
  171 static int
  172 in_mask2len(mask)
  173         struct in_addr *mask;
  174 {
  175         int x, y;
  176         u_char *p;
  177 
  178         p = (u_char *)mask;
  179         for (x = 0; x < sizeof(*mask); x++) {
  180                 if (p[x] != 0xff)
  181                         break;
  182         }
  183         y = 0;
  184         if (x < sizeof(*mask)) {
  185                 for (y = 0; y < 8; y++) {
  186                         if ((p[x] & (0x80 >> y)) == 0)
  187                                 break;
  188                 }
  189         }
  190         return x * 8 + y;
  191 }
  192 
  193 static void
  194 in_len2mask(mask, len)
  195         struct in_addr *mask;
  196         int len;
  197 {
  198         int i;
  199         u_char *p;
  200 
  201         p = (u_char *)mask;
  202         bzero(mask, sizeof(*mask));
  203         for (i = 0; i < len / 8; i++)
  204                 p[i] = 0xff;
  205         if (len % 8)
  206                 p[i] = (0xff00 >> (len % 8)) & 0xff;
  207 }
  208 
  209 /*
  210  * Generic internet control operations (ioctl's).
  211  * Ifp is 0 if not an interface-specific ioctl.
  212  */
  213 /* ARGSUSED */
  214 int
  215 in_control(so, cmd, data, ifp, td)
  216         struct socket *so;
  217         u_long cmd;
  218         caddr_t data;
  219         register struct ifnet *ifp;
  220         struct thread *td;
  221 {
  222         register struct ifreq *ifr = (struct ifreq *)data;
  223         register struct in_ifaddr *ia = 0, *iap;
  224         register struct ifaddr *ifa;
  225         struct in_addr dst;
  226         struct in_ifaddr *oia;
  227         struct in_aliasreq *ifra = (struct in_aliasreq *)data;
  228         struct sockaddr_in oldaddr;
  229         int error, hostIsNew, iaIsNew, maskIsNew, s;
  230 
  231         iaIsNew = 0;
  232 
  233         switch (cmd) {
  234         case SIOCALIFADDR:
  235         case SIOCDLIFADDR:
  236                 if (td && (error = suser(td)) != 0)
  237                         return error;
  238                 /*fall through*/
  239         case SIOCGLIFADDR:
  240                 if (!ifp)
  241                         return EINVAL;
  242                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
  243         }
  244 
  245         /*
  246          * Find address for this interface, if it exists.
  247          *
  248          * If an alias address was specified, find that one instead of
  249          * the first one on the interface, if possible.
  250          */
  251         if (ifp) {
  252                 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
  253                 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
  254                         if (iap->ia_ifp == ifp &&
  255                             iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
  256                                 ia = iap;
  257                                 break;
  258                         }
  259                 if (ia == NULL)
  260                         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  261                                 iap = ifatoia(ifa);
  262                                 if (iap->ia_addr.sin_family == AF_INET) {
  263                                         ia = iap;
  264                                         break;
  265                                 }
  266                         }
  267         }
  268 
  269         switch (cmd) {
  270 
  271         case SIOCAIFADDR:
  272         case SIOCDIFADDR:
  273                 if (ifp == 0)
  274                         return (EADDRNOTAVAIL);
  275                 if (ifra->ifra_addr.sin_family == AF_INET) {
  276                         for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
  277                                 if (ia->ia_ifp == ifp  &&
  278                                     ia->ia_addr.sin_addr.s_addr ==
  279                                     ifra->ifra_addr.sin_addr.s_addr)
  280                                         break;
  281                         }
  282                         if ((ifp->if_flags & IFF_POINTOPOINT)
  283                             && (cmd == SIOCAIFADDR)
  284                             && (ifra->ifra_dstaddr.sin_addr.s_addr
  285                                 == INADDR_ANY)) {
  286                                 return EDESTADDRREQ;
  287                         }
  288                 }
  289                 if (cmd == SIOCDIFADDR && ia == 0)
  290                         return (EADDRNOTAVAIL);
  291                 /* FALLTHROUGH */
  292         case SIOCSIFADDR:
  293         case SIOCSIFNETMASK:
  294         case SIOCSIFDSTADDR:
  295                 if (td && (error = suser(td)) != 0)
  296                         return error;
  297 
  298                 if (ifp == 0)
  299                         return (EADDRNOTAVAIL);
  300                 if (ia == (struct in_ifaddr *)0) {
  301                         ia = (struct in_ifaddr *)
  302                                 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
  303                         if (ia == (struct in_ifaddr *)NULL)
  304                                 return (ENOBUFS);
  305                         /*
  306                          * Protect from ipintr() traversing address list
  307                          * while we're modifying it.
  308                          */
  309                         s = splnet();
  310                         TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
  311 
  312                         ifa = &ia->ia_ifa;
  313                         IFA_LOCK_INIT(ifa);
  314                         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
  315                         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
  316                         ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
  317                         ifa->ifa_refcnt = 1;
  318                         TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
  319 
  320                         ia->ia_sockmask.sin_len = 8;
  321                         ia->ia_sockmask.sin_family = AF_INET;
  322                         if (ifp->if_flags & IFF_BROADCAST) {
  323                                 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
  324                                 ia->ia_broadaddr.sin_family = AF_INET;
  325                         }
  326                         ia->ia_ifp = ifp;
  327                         splx(s);
  328                         iaIsNew = 1;
  329                 }
  330                 break;
  331 
  332         case SIOCSIFBRDADDR:
  333                 if (td && (error = suser(td)) != 0)
  334                         return error;
  335                 /* FALLTHROUGH */
  336 
  337         case SIOCGIFADDR:
  338         case SIOCGIFNETMASK:
  339         case SIOCGIFDSTADDR:
  340         case SIOCGIFBRDADDR:
  341                 if (ia == (struct in_ifaddr *)0)
  342                         return (EADDRNOTAVAIL);
  343                 break;
  344         }
  345         switch (cmd) {
  346 
  347         case SIOCGIFADDR:
  348                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
  349                 return (0);
  350 
  351         case SIOCGIFBRDADDR:
  352                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
  353                         return (EINVAL);
  354                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
  355                 return (0);
  356 
  357         case SIOCGIFDSTADDR:
  358                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
  359                         return (EINVAL);
  360                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
  361                 return (0);
  362 
  363         case SIOCGIFNETMASK:
  364                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
  365                 return (0);
  366 
  367         case SIOCSIFDSTADDR:
  368                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
  369                         return (EINVAL);
  370                 oldaddr = ia->ia_dstaddr;
  371                 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
  372                 if (ifp->if_ioctl) {
  373                         IFF_LOCKGIANT(ifp);
  374                         error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
  375                             (caddr_t)ia);
  376                         IFF_UNLOCKGIANT(ifp);
  377                         if (error) {
  378                                 ia->ia_dstaddr = oldaddr;
  379                                 return (error);
  380                         }
  381                 }
  382                 if (ia->ia_flags & IFA_ROUTE) {
  383                         ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
  384                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
  385                         ia->ia_ifa.ifa_dstaddr =
  386                                         (struct sockaddr *)&ia->ia_dstaddr;
  387                         rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
  388                 }
  389                 return (0);
  390 
  391         case SIOCSIFBRDADDR:
  392                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
  393                         return (EINVAL);
  394                 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
  395                 return (0);
  396 
  397         case SIOCSIFADDR:
  398                 error = in_ifinit(ifp, ia,
  399                     (struct sockaddr_in *) &ifr->ifr_addr, 1);
  400                 if (error != 0 && iaIsNew)
  401                         break;
  402                 if (error == 0)
  403                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  404                 return (0);
  405 
  406         case SIOCSIFNETMASK:
  407                 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
  408                 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
  409                 return (0);
  410 
  411         case SIOCAIFADDR:
  412                 maskIsNew = 0;
  413                 hostIsNew = 1;
  414                 error = 0;
  415                 if (ia->ia_addr.sin_family == AF_INET) {
  416                         if (ifra->ifra_addr.sin_len == 0) {
  417                                 ifra->ifra_addr = ia->ia_addr;
  418                                 hostIsNew = 0;
  419                         } else if (ifra->ifra_addr.sin_addr.s_addr ==
  420                                                ia->ia_addr.sin_addr.s_addr)
  421                                 hostIsNew = 0;
  422                 }
  423                 if (ifra->ifra_mask.sin_len) {
  424                         in_ifscrub(ifp, ia);
  425                         ia->ia_sockmask = ifra->ifra_mask;
  426                         ia->ia_sockmask.sin_family = AF_INET;
  427                         ia->ia_subnetmask =
  428                              ntohl(ia->ia_sockmask.sin_addr.s_addr);
  429                         maskIsNew = 1;
  430                 }
  431                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
  432                     (ifra->ifra_dstaddr.sin_family == AF_INET)) {
  433                         in_ifscrub(ifp, ia);
  434                         ia->ia_dstaddr = ifra->ifra_dstaddr;
  435                         maskIsNew  = 1; /* We lie; but the effect's the same */
  436                 }
  437                 if (ifra->ifra_addr.sin_family == AF_INET &&
  438                     (hostIsNew || maskIsNew))
  439                         error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
  440                 if (error != 0 && iaIsNew)
  441                         break;
  442 
  443                 if ((ifp->if_flags & IFF_BROADCAST) &&
  444                     (ifra->ifra_broadaddr.sin_family == AF_INET))
  445                         ia->ia_broadaddr = ifra->ifra_broadaddr;
  446                 if (error == 0)
  447                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  448                 return (error);
  449 
  450         case SIOCDIFADDR:
  451                 /*
  452                  * in_ifscrub kills the interface route.
  453                  */
  454                 in_ifscrub(ifp, ia);
  455                 /*
  456                  * in_ifadown gets rid of all the rest of
  457                  * the routes.  This is not quite the right
  458                  * thing to do, but at least if we are running
  459                  * a routing process they will come back.
  460                  */
  461                 in_ifadown(&ia->ia_ifa, 1);
  462                 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
  463                 error = 0;
  464                 break;
  465 
  466         default:
  467                 if (ifp == 0 || ifp->if_ioctl == 0)
  468                         return (EOPNOTSUPP);
  469                 IFF_LOCKGIANT(ifp);
  470                 error = (*ifp->if_ioctl)(ifp, cmd, data);
  471                 IFF_UNLOCKGIANT(ifp);
  472                 return (error);
  473         }
  474 
  475         /*
  476          * Protect from ipintr() traversing address list while we're modifying
  477          * it.
  478          */
  479         s = splnet();
  480         TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
  481         TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
  482         LIST_REMOVE(ia, ia_hash);
  483         IFAFREE(&ia->ia_ifa);
  484         splx(s);
  485 
  486         return (error);
  487 }
  488 
  489 /*
  490  * SIOC[GAD]LIFADDR.
  491  *      SIOCGLIFADDR: get first address. (?!?)
  492  *      SIOCGLIFADDR with IFLR_PREFIX:
  493  *              get first address that matches the specified prefix.
  494  *      SIOCALIFADDR: add the specified address.
  495  *      SIOCALIFADDR with IFLR_PREFIX:
  496  *              EINVAL since we can't deduce hostid part of the address.
  497  *      SIOCDLIFADDR: delete the specified address.
  498  *      SIOCDLIFADDR with IFLR_PREFIX:
  499  *              delete the first address that matches the specified prefix.
  500  * return values:
  501  *      EINVAL on invalid parameters
  502  *      EADDRNOTAVAIL on prefix match failed/specified address not found
  503  *      other values may be returned from in_ioctl()
  504  */
  505 static int
  506 in_lifaddr_ioctl(so, cmd, data, ifp, td)
  507         struct socket *so;
  508         u_long cmd;
  509         caddr_t data;
  510         struct ifnet *ifp;
  511         struct thread *td;
  512 {
  513         struct if_laddrreq *iflr = (struct if_laddrreq *)data;
  514         struct ifaddr *ifa;
  515 
  516         /* sanity checks */
  517         if (!data || !ifp) {
  518                 panic("invalid argument to in_lifaddr_ioctl");
  519                 /*NOTRECHED*/
  520         }
  521 
  522         switch (cmd) {
  523         case SIOCGLIFADDR:
  524                 /* address must be specified on GET with IFLR_PREFIX */
  525                 if ((iflr->flags & IFLR_PREFIX) == 0)
  526                         break;
  527                 /*FALLTHROUGH*/
  528         case SIOCALIFADDR:
  529         case SIOCDLIFADDR:
  530                 /* address must be specified on ADD and DELETE */
  531                 if (iflr->addr.ss_family != AF_INET)
  532                         return EINVAL;
  533                 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
  534                         return EINVAL;
  535                 /* XXX need improvement */
  536                 if (iflr->dstaddr.ss_family
  537                  && iflr->dstaddr.ss_family != AF_INET)
  538                         return EINVAL;
  539                 if (iflr->dstaddr.ss_family
  540                  && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
  541                         return EINVAL;
  542                 break;
  543         default: /*shouldn't happen*/
  544                 return EOPNOTSUPP;
  545         }
  546         if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
  547                 return EINVAL;
  548 
  549         switch (cmd) {
  550         case SIOCALIFADDR:
  551             {
  552                 struct in_aliasreq ifra;
  553 
  554                 if (iflr->flags & IFLR_PREFIX)
  555                         return EINVAL;
  556 
  557                 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
  558                 bzero(&ifra, sizeof(ifra));
  559                 bcopy(iflr->iflr_name, ifra.ifra_name,
  560                         sizeof(ifra.ifra_name));
  561 
  562                 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
  563 
  564                 if (iflr->dstaddr.ss_family) {  /*XXX*/
  565                         bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
  566                                 iflr->dstaddr.ss_len);
  567                 }
  568 
  569                 ifra.ifra_mask.sin_family = AF_INET;
  570                 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
  571                 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
  572 
  573                 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
  574             }
  575         case SIOCGLIFADDR:
  576         case SIOCDLIFADDR:
  577             {
  578                 struct in_ifaddr *ia;
  579                 struct in_addr mask, candidate, match;
  580                 struct sockaddr_in *sin;
  581                 int cmp;
  582 
  583                 bzero(&mask, sizeof(mask));
  584                 if (iflr->flags & IFLR_PREFIX) {
  585                         /* lookup a prefix rather than address. */
  586                         in_len2mask(&mask, iflr->prefixlen);
  587 
  588                         sin = (struct sockaddr_in *)&iflr->addr;
  589                         match.s_addr = sin->sin_addr.s_addr;
  590                         match.s_addr &= mask.s_addr;
  591 
  592                         /* if you set extra bits, that's wrong */
  593                         if (match.s_addr != sin->sin_addr.s_addr)
  594                                 return EINVAL;
  595 
  596                         cmp = 1;
  597                 } else {
  598                         if (cmd == SIOCGLIFADDR) {
  599                                 /* on getting an address, take the 1st match */
  600                                 cmp = 0;        /*XXX*/
  601                         } else {
  602                                 /* on deleting an address, do exact match */
  603                                 in_len2mask(&mask, 32);
  604                                 sin = (struct sockaddr_in *)&iflr->addr;
  605                                 match.s_addr = sin->sin_addr.s_addr;
  606 
  607                                 cmp = 1;
  608                         }
  609                 }
  610 
  611                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  612                         if (ifa->ifa_addr->sa_family != AF_INET6)
  613                                 continue;
  614                         if (!cmp)
  615                                 break;
  616                         candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
  617                         candidate.s_addr &= mask.s_addr;
  618                         if (candidate.s_addr == match.s_addr)
  619                                 break;
  620                 }
  621                 if (!ifa)
  622                         return EADDRNOTAVAIL;
  623                 ia = (struct in_ifaddr *)ifa;
  624 
  625                 if (cmd == SIOCGLIFADDR) {
  626                         /* fill in the if_laddrreq structure */
  627                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
  628 
  629                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
  630                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
  631                                         ia->ia_dstaddr.sin_len);
  632                         } else
  633                                 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
  634 
  635                         iflr->prefixlen =
  636                                 in_mask2len(&ia->ia_sockmask.sin_addr);
  637 
  638                         iflr->flags = 0;        /*XXX*/
  639 
  640                         return 0;
  641                 } else {
  642                         struct in_aliasreq ifra;
  643 
  644                         /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
  645                         bzero(&ifra, sizeof(ifra));
  646                         bcopy(iflr->iflr_name, ifra.ifra_name,
  647                                 sizeof(ifra.ifra_name));
  648 
  649                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
  650                                 ia->ia_addr.sin_len);
  651                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
  652                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
  653                                         ia->ia_dstaddr.sin_len);
  654                         }
  655                         bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
  656                                 ia->ia_sockmask.sin_len);
  657 
  658                         return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
  659                                           ifp, td);
  660                 }
  661             }
  662         }
  663 
  664         return EOPNOTSUPP;      /*just for safety*/
  665 }
  666 
  667 /*
  668  * Delete any existing route for an interface.
  669  */
  670 void
  671 in_ifscrub(ifp, ia)
  672         register struct ifnet *ifp;
  673         register struct in_ifaddr *ia;
  674 {
  675         in_scrubprefix(ia);
  676 }
  677 
  678 /*
  679  * Initialize an interface's internet address
  680  * and routing table entry.
  681  */
  682 static int
  683 in_ifinit(ifp, ia, sin, scrub)
  684         register struct ifnet *ifp;
  685         register struct in_ifaddr *ia;
  686         struct sockaddr_in *sin;
  687         int scrub;
  688 {
  689         register u_long i = ntohl(sin->sin_addr.s_addr);
  690         struct sockaddr_in oldaddr;
  691         int s = splimp(), flags = RTF_UP, error = 0;
  692 
  693         oldaddr = ia->ia_addr;
  694         if (oldaddr.sin_family == AF_INET)
  695                 LIST_REMOVE(ia, ia_hash);
  696         ia->ia_addr = *sin;
  697         if (ia->ia_addr.sin_family == AF_INET)
  698                 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
  699                     ia, ia_hash);
  700         /*
  701          * Give the interface a chance to initialize
  702          * if this is its first address,
  703          * and to validate the address if necessary.
  704          */
  705         if (ifp->if_ioctl) {
  706                 IFF_LOCKGIANT(ifp);
  707                 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
  708                 IFF_UNLOCKGIANT(ifp);
  709                 if (error) {
  710                         splx(s);
  711                         /* LIST_REMOVE(ia, ia_hash) is done in in_control */
  712                         ia->ia_addr = oldaddr;
  713                         if (ia->ia_addr.sin_family == AF_INET)
  714                                 LIST_INSERT_HEAD(INADDR_HASH(
  715                                     ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
  716                         return (error);
  717                 }
  718         }
  719         splx(s);
  720         if (scrub) {
  721                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
  722                 in_ifscrub(ifp, ia);
  723                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
  724         }
  725         if (IN_CLASSA(i))
  726                 ia->ia_netmask = IN_CLASSA_NET;
  727         else if (IN_CLASSB(i))
  728                 ia->ia_netmask = IN_CLASSB_NET;
  729         else
  730                 ia->ia_netmask = IN_CLASSC_NET;
  731         /*
  732          * The subnet mask usually includes at least the standard network part,
  733          * but may may be smaller in the case of supernetting.
  734          * If it is set, we believe it.
  735          */
  736         if (ia->ia_subnetmask == 0) {
  737                 ia->ia_subnetmask = ia->ia_netmask;
  738                 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
  739         } else
  740                 ia->ia_netmask &= ia->ia_subnetmask;
  741         ia->ia_net = i & ia->ia_netmask;
  742         ia->ia_subnet = i & ia->ia_subnetmask;
  743         in_socktrim(&ia->ia_sockmask);
  744 #ifdef DEV_CARP
  745         /*
  746          * XXX: carp(4) does not have interface route
  747          */
  748         if (ifp->if_type == IFT_CARP)
  749                 return (0);
  750 #endif
  751         /*
  752          * Add route for the network.
  753          */
  754         ia->ia_ifa.ifa_metric = ifp->if_metric;
  755         if (ifp->if_flags & IFF_BROADCAST) {
  756                 ia->ia_broadaddr.sin_addr.s_addr =
  757                         htonl(ia->ia_subnet | ~ia->ia_subnetmask);
  758                 ia->ia_netbroadcast.s_addr =
  759                         htonl(ia->ia_net | ~ ia->ia_netmask);
  760         } else if (ifp->if_flags & IFF_LOOPBACK) {
  761                 ia->ia_dstaddr = ia->ia_addr;
  762                 flags |= RTF_HOST;
  763         } else if (ifp->if_flags & IFF_POINTOPOINT) {
  764                 if (ia->ia_dstaddr.sin_family != AF_INET)
  765                         return (0);
  766                 flags |= RTF_HOST;
  767         }
  768         if ((error = in_addprefix(ia, flags)) != 0)
  769                 return (error);
  770 
  771         /*
  772          * If the interface supports multicast, join the "all hosts"
  773          * multicast group on that interface.
  774          */
  775         if (ifp->if_flags & IFF_MULTICAST) {
  776                 struct in_addr addr;
  777 
  778                 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
  779                 in_addmulti(&addr, ifp);
  780         }
  781         return (error);
  782 }
  783 
  784 #define rtinitflags(x) \
  785         ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
  786             ? RTF_HOST : 0)
  787 /*
  788  * Check if we have a route for the given prefix already or add a one
  789  * accordingly.
  790  */
  791 static int
  792 in_addprefix(target, flags)
  793         struct in_ifaddr *target;
  794         int flags;
  795 {
  796         struct in_ifaddr *ia;
  797         struct in_addr prefix, mask, p;
  798         int error;
  799 
  800         if ((flags & RTF_HOST) != 0)
  801                 prefix = target->ia_dstaddr.sin_addr;
  802         else {
  803                 prefix = target->ia_addr.sin_addr;
  804                 mask = target->ia_sockmask.sin_addr;
  805                 prefix.s_addr &= mask.s_addr;
  806         }
  807 
  808         TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
  809                 if (rtinitflags(ia))
  810                         p = ia->ia_dstaddr.sin_addr;
  811                 else {
  812                         p = ia->ia_addr.sin_addr;
  813                         p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
  814                 }
  815 
  816                 if (prefix.s_addr != p.s_addr)
  817                         continue;
  818 
  819                 /*
  820                  * If we got a matching prefix route inserted by other
  821                  * interface address, we are done here.
  822                  */
  823                 if (ia->ia_flags & IFA_ROUTE) {
  824                         if (sameprefixcarponly &&
  825                             target->ia_ifp->if_type != IFT_CARP &&
  826                             ia->ia_ifp->if_type != IFT_CARP)
  827                                 return (EEXIST);
  828                         else
  829                                 return (0);
  830                 }
  831         }
  832 
  833         /*
  834          * No-one seem to have this prefix route, so we try to insert it.
  835          */
  836         error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
  837         if (!error)
  838                 target->ia_flags |= IFA_ROUTE;
  839         return error;
  840 }
  841 
  842 /*
  843  * If there is no other address in the system that can serve a route to the
  844  * same prefix, remove the route.  Hand over the route to the new address
  845  * otherwise.
  846  */
  847 static int
  848 in_scrubprefix(target)
  849         struct in_ifaddr *target;
  850 {
  851         struct in_ifaddr *ia;
  852         struct in_addr prefix, mask, p;
  853         int error;
  854 
  855         if ((target->ia_flags & IFA_ROUTE) == 0)
  856                 return 0;
  857 
  858         if (rtinitflags(target))
  859                 prefix = target->ia_dstaddr.sin_addr;
  860         else {
  861                 prefix = target->ia_addr.sin_addr;
  862                 mask = target->ia_sockmask.sin_addr;
  863                 prefix.s_addr &= mask.s_addr;
  864         }
  865 
  866         TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
  867                 if (rtinitflags(ia))
  868                         p = ia->ia_dstaddr.sin_addr;
  869                 else {
  870                         p = ia->ia_addr.sin_addr;
  871                         p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
  872                 }
  873 
  874                 if (prefix.s_addr != p.s_addr)
  875                         continue;
  876 
  877                 /*
  878                  * If we got a matching prefix address, move IFA_ROUTE and
  879                  * the route itself to it.  Make sure that routing daemons
  880                  * get a heads-up.
  881                  *
  882                  * XXX: a special case for carp(4) interface
  883                  */
  884                 if ((ia->ia_flags & IFA_ROUTE) == 0
  885 #ifdef DEV_CARP
  886                     && (ia->ia_ifp->if_type != IFT_CARP)
  887 #endif
  888                                                         ) {
  889                         rtinit(&(target->ia_ifa), (int)RTM_DELETE,
  890                             rtinitflags(target));
  891                         target->ia_flags &= ~IFA_ROUTE;
  892 
  893                         error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
  894                             rtinitflags(ia) | RTF_UP);
  895                         if (error == 0)
  896                                 ia->ia_flags |= IFA_ROUTE;
  897                         return error;
  898                 }
  899         }
  900 
  901         /*
  902          * As no-one seem to have this prefix, we can remove the route.
  903          */
  904         rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
  905         target->ia_flags &= ~IFA_ROUTE;
  906         return 0;
  907 }
  908 
  909 #undef rtinitflags
  910 
  911 /*
  912  * Return 1 if the address might be a local broadcast address.
  913  */
  914 int
  915 in_broadcast(in, ifp)
  916         struct in_addr in;
  917         struct ifnet *ifp;
  918 {
  919         register struct ifaddr *ifa;
  920         u_long t;
  921 
  922         if (in.s_addr == INADDR_BROADCAST ||
  923             in.s_addr == INADDR_ANY)
  924                 return 1;
  925         if ((ifp->if_flags & IFF_BROADCAST) == 0)
  926                 return 0;
  927         t = ntohl(in.s_addr);
  928         /*
  929          * Look through the list of addresses for a match
  930          * with a broadcast address.
  931          */
  932 #define ia ((struct in_ifaddr *)ifa)
  933         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
  934                 if (ifa->ifa_addr->sa_family == AF_INET &&
  935                     (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
  936                      in.s_addr == ia->ia_netbroadcast.s_addr ||
  937                      /*
  938                       * Check for old-style (host 0) broadcast.
  939                       */
  940                      t == ia->ia_subnet || t == ia->ia_net) &&
  941                      /*
  942                       * Check for an all one subnetmask. These
  943                       * only exist when an interface gets a secondary
  944                       * address.
  945                       */
  946                      ia->ia_subnetmask != (u_long)0xffffffff)
  947                             return 1;
  948         return (0);
  949 #undef ia
  950 }
  951 /*
  952  * Add an address to the list of IP multicast addresses for a given interface.
  953  */
  954 struct in_multi *
  955 in_addmulti(ap, ifp)
  956         register struct in_addr *ap;
  957         register struct ifnet *ifp;
  958 {
  959         register struct in_multi *inm;
  960         int error;
  961         struct sockaddr_in sin;
  962         struct ifmultiaddr *ifma;
  963 
  964         IFF_LOCKGIANT(ifp);
  965         IN_MULTI_LOCK();
  966         /*
  967          * Call generic routine to add membership or increment
  968          * refcount.  It wants addresses in the form of a sockaddr,
  969          * so we build one here (being careful to zero the unused bytes).
  970          */
  971         bzero(&sin, sizeof sin);
  972         sin.sin_family = AF_INET;
  973         sin.sin_len = sizeof sin;
  974         sin.sin_addr = *ap;
  975         error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
  976         if (error) {
  977                 IN_MULTI_UNLOCK();
  978                 IFF_UNLOCKGIANT(ifp);
  979                 return 0;
  980         }
  981 
  982         /*
  983          * If ifma->ifma_protospec is null, then if_addmulti() created
  984          * a new record.  Otherwise, we are done.
  985          */
  986         if (ifma->ifma_protospec != NULL) {
  987                 IN_MULTI_UNLOCK();
  988                 IFF_UNLOCKGIANT(ifp);
  989                 return ifma->ifma_protospec;
  990         }
  991 
  992         inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
  993             M_NOWAIT | M_ZERO);
  994         if (inm == NULL) {
  995                 IN_MULTI_UNLOCK();
  996                 IFF_UNLOCKGIANT(ifp);
  997                 return (NULL);
  998         }
  999 
 1000         inm->inm_addr = *ap;
 1001         inm->inm_ifp = ifp;
 1002         inm->inm_ifma = ifma;
 1003         ifma->ifma_protospec = inm;
 1004         LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
 1005 
 1006         /*
 1007          * Let IGMP know that we have joined a new IP multicast group.
 1008          */
 1009         igmp_joingroup(inm);
 1010         IN_MULTI_UNLOCK();
 1011         IFF_UNLOCKGIANT(ifp);
 1012         return (inm);
 1013 }
 1014 
 1015 /*
 1016  * Delete a multicast address record.
 1017  */
 1018 void
 1019 in_delmulti(inm)
 1020         register struct in_multi *inm;
 1021 {
 1022         struct ifmultiaddr *ifma;
 1023         struct in_multi my_inm;
 1024         struct ifnet *ifp;
 1025 
 1026         ifp = inm->inm_ifp;
 1027         IFF_LOCKGIANT(ifp);
 1028         IN_MULTI_LOCK();
 1029         ifma = inm->inm_ifma;
 1030         my_inm.inm_ifp = NULL ; /* don't send the leave msg */
 1031         if (ifma->ifma_refcount == 1) {
 1032                 /*
 1033                  * No remaining claims to this record; let IGMP know that
 1034                  * we are leaving the multicast group.
 1035                  * But do it after the if_delmulti() which might reset
 1036                  * the interface and nuke the packet.
 1037                  */
 1038                 my_inm = *inm ;
 1039                 ifma->ifma_protospec = NULL;
 1040                 LIST_REMOVE(inm, inm_link);
 1041                 free(inm, M_IPMADDR);
 1042         }
 1043         /* XXX - should be separate API for when we have an ifma? */
 1044         if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
 1045         if (my_inm.inm_ifp != NULL)
 1046                 igmp_leavegroup(&my_inm);
 1047         IN_MULTI_UNLOCK();
 1048         IFF_UNLOCKGIANT(ifp);
 1049 }
 1050 
 1051 /*
 1052  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
 1053  */
 1054 void
 1055 in_ifdetach(ifp)
 1056         struct ifnet *ifp;
 1057 {
 1058 
 1059         in_pcbpurgeif0(&ripcbinfo, ifp);
 1060         in_pcbpurgeif0(&udbinfo, ifp);
 1061 }

Cache object: f1ee4e2694d9a61ce87cae259d9f40f8


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