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

Cache object: 6e1f046bff5e3f832622a329cb6fe86d


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