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/if_arp.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: if_arp.c,v 1.94 2003/09/24 06:52:47 itojun Exp $       */
    2 
    3 /*-
    4  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Public Access Networks Corporation ("Panix").  It was developed under
    9  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. All advertising materials mentioning features or use of this software
   20  *    must display the following acknowledgement:
   21  *      This product includes software developed by the NetBSD
   22  *      Foundation, Inc. and its contributors.
   23  * 4. Neither the name of The NetBSD Foundation nor the names of its
   24  *    contributors may be used to endorse or promote products derived
   25  *    from this software without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   37  * POSSIBILITY OF SUCH DAMAGE.
   38  */
   39 
   40 /*
   41  * Copyright (c) 1982, 1986, 1988, 1993
   42  *      The Regents of the University of California.  All rights reserved.
   43  *
   44  * Redistribution and use in source and binary forms, with or without
   45  * modification, are permitted provided that the following conditions
   46  * are met:
   47  * 1. Redistributions of source code must retain the above copyright
   48  *    notice, this list of conditions and the following disclaimer.
   49  * 2. Redistributions in binary form must reproduce the above copyright
   50  *    notice, this list of conditions and the following disclaimer in the
   51  *    documentation and/or other materials provided with the distribution.
   52  * 3. Neither the name of the University nor the names of its contributors
   53  *    may be used to endorse or promote products derived from this software
   54  *    without specific prior written permission.
   55  *
   56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   66  * SUCH DAMAGE.
   67  *
   68  *      @(#)if_ether.c  8.2 (Berkeley) 9/26/94
   69  */
   70 
   71 /*
   72  * Ethernet address resolution protocol.
   73  * TODO:
   74  *      add "inuse/lock" bit (or ref. count) along with valid bit
   75  */
   76 
   77 #include <sys/cdefs.h>
   78 __KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.94 2003/09/24 06:52:47 itojun Exp $");
   79 
   80 #include "opt_ddb.h"
   81 #include "opt_inet.h"
   82 
   83 #ifdef INET
   84 
   85 #include "bridge.h"
   86 
   87 #include <sys/param.h>
   88 #include <sys/systm.h>
   89 #include <sys/callout.h>
   90 #include <sys/malloc.h>
   91 #include <sys/mbuf.h>
   92 #include <sys/socket.h>
   93 #include <sys/time.h>
   94 #include <sys/kernel.h>
   95 #include <sys/errno.h>
   96 #include <sys/ioctl.h>
   97 #include <sys/syslog.h>
   98 #include <sys/proc.h>
   99 #include <sys/protosw.h>
  100 #include <sys/domain.h>
  101 
  102 #include <net/ethertypes.h>
  103 #include <net/if.h>
  104 #include <net/if_dl.h>
  105 #include <net/if_token.h>
  106 #include <net/if_types.h>
  107 #include <net/route.h>
  108 
  109 #include <netinet/in.h>
  110 #include <netinet/in_systm.h>
  111 #include <netinet/in_var.h>
  112 #include <netinet/ip.h>
  113 #include <netinet/if_inarp.h>
  114 
  115 #include "loop.h"
  116 #include "arc.h"
  117 #if NARC > 0
  118 #include <net/if_arc.h>
  119 #endif
  120 #include "fddi.h"
  121 #if NFDDI > 0
  122 #include <net/if_fddi.h>
  123 #endif
  124 #include "token.h"
  125 
  126 #define SIN(s) ((struct sockaddr_in *)s)
  127 #define SDL(s) ((struct sockaddr_dl *)s)
  128 #define SRP(s) ((struct sockaddr_inarp *)s)
  129 
  130 /*
  131  * ARP trailer negotiation.  Trailer protocol is not IP specific,
  132  * but ARP request/response use IP addresses.
  133  */
  134 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
  135 
  136 /* timer values */
  137 int     arpt_prune = (5*60*1);  /* walk list every 5 minutes */
  138 int     arpt_keep = (20*60);    /* once resolved, good for 20 more minutes */
  139 int     arpt_down = 20;         /* once declared down, don't send for 20 secs */
  140 #define rt_expire rt_rmx.rmx_expire
  141 
  142 extern  struct domain arpdomain;
  143 
  144 static  void arprequest __P((struct ifnet *,
  145             struct in_addr *, struct in_addr *, u_int8_t *));
  146 static  void arptfree __P((struct llinfo_arp *));
  147 static  void arptimer __P((void *));
  148 static  struct llinfo_arp *arplookup __P((struct mbuf *, struct in_addr *,
  149                                           int, int));
  150 static  void in_arpinput __P((struct mbuf *));
  151 
  152 #if NLOOP > 0
  153 extern  struct ifnet loif[NLOOP];
  154 #endif
  155 LIST_HEAD(, llinfo_arp) llinfo_arp;
  156 struct  ifqueue arpintrq = {0, 0, 0, 50};
  157 int     arp_inuse, arp_allocated, arp_intimer;
  158 int     arp_maxtries = 5;
  159 int     useloopback = 1;        /* use loopback interface for local traffic */
  160 int     arpinit_done = 0;
  161 
  162 struct  arpstat arpstat;
  163 struct  callout arptimer_ch;
  164 
  165 
  166 /* revarp state */
  167 static struct   in_addr myip, srv_ip;
  168 static int      myip_initialized = 0;
  169 static int      revarp_in_progress = 0;
  170 static struct   ifnet *myip_ifp = NULL;
  171 
  172 #ifdef DDB
  173 static void db_print_sa __P((struct sockaddr *));
  174 static void db_print_ifa __P((struct ifaddr *));
  175 static void db_print_llinfo __P((caddr_t));
  176 static int db_show_radix_node __P((struct radix_node *, void *));
  177 #endif
  178 
  179 /*
  180  * this should be elsewhere.
  181  */
  182 
  183 static char *
  184 lla_snprintf __P((u_int8_t *, int));
  185 
  186 static char *
  187 lla_snprintf(adrp, len)
  188         u_int8_t *adrp;
  189         int len;
  190 {
  191 #define NUMBUFS 3
  192         static char buf[NUMBUFS][16*3];
  193         static int bnum = 0;
  194         static const char hexdigits[] = {
  195             '','1','2','3','4','5','6','7',
  196             '8','9','a','b','c','d','e','f'
  197         };
  198 
  199         int i;
  200         char *p;
  201 
  202         p = buf[bnum];
  203 
  204         *p++ = hexdigits[(*adrp)>>4];
  205         *p++ = hexdigits[(*adrp++)&0xf];
  206 
  207         for (i=1; i<len && i<16; i++) {
  208                 *p++ = ':';
  209                 *p++ = hexdigits[(*adrp)>>4];
  210                 *p++ = hexdigits[(*adrp++)&0xf];
  211         }
  212 
  213         *p = 0;
  214         p = buf[bnum];
  215         bnum = (bnum + 1) % NUMBUFS;
  216         return p;
  217 }
  218 
  219 struct protosw arpsw[] = {
  220         { 0, 0, 0, 0,
  221           0, 0, 0, 0,
  222           0,
  223           0, 0, 0, arp_drain,
  224         }
  225 };
  226 
  227 
  228 struct domain arpdomain =
  229 {       PF_ARP,  "arp", 0, 0, 0,
  230         arpsw, &arpsw[sizeof(arpsw)/sizeof(arpsw[0])]
  231 };
  232 
  233 /*
  234  * ARP table locking.
  235  *
  236  * to prevent lossage vs. the arp_drain routine (which may be called at
  237  * any time, including in a device driver context), we do two things:
  238  *
  239  * 1) manipulation of la->la_hold is done at splnet() (for all of
  240  * about two instructions).
  241  *
  242  * 2) manipulation of the arp table's linked list is done under the
  243  * protection of the ARP_LOCK; if arp_drain() or arptimer is called
  244  * while the arp table is locked, we punt and try again later.
  245  */
  246 
  247 static int      arp_locked;
  248 static __inline int arp_lock_try __P((int));
  249 static __inline void arp_unlock __P((void));
  250 
  251 static __inline int
  252 arp_lock_try(int recurse)
  253 {
  254         int s;
  255 
  256         /*
  257          * Use splvm() -- we're blocking things that would cause
  258          * mbuf allocation.
  259          */
  260         s = splvm();
  261         if (!recurse && arp_locked) {
  262                 splx(s);
  263                 return (0);
  264         }
  265         arp_locked++;
  266         splx(s);
  267         return (1);
  268 }
  269 
  270 static __inline void
  271 arp_unlock()
  272 {
  273         int s;
  274 
  275         s = splvm();
  276         arp_locked--;
  277         splx(s);
  278 }
  279 
  280 #ifdef DIAGNOSTIC
  281 #define ARP_LOCK(recurse)                                               \
  282 do {                                                                    \
  283         if (arp_lock_try(recurse) == 0) {                               \
  284                 printf("%s:%d: arp already locked\n", __FILE__, __LINE__); \
  285                 panic("arp_lock");                                      \
  286         }                                                               \
  287 } while (/*CONSTCOND*/ 0)
  288 #define ARP_LOCK_CHECK()                                                \
  289 do {                                                                    \
  290         if (arp_locked == 0) {                                          \
  291                 printf("%s:%d: arp lock not held\n", __FILE__, __LINE__); \
  292                 panic("arp lock check");                                \
  293         }                                                               \
  294 } while (/*CONSTCOND*/ 0)
  295 #else
  296 #define ARP_LOCK(x)             (void) arp_lock_try(x)
  297 #define ARP_LOCK_CHECK()        /* nothing */
  298 #endif
  299 
  300 #define ARP_UNLOCK()            arp_unlock()
  301 
  302 /*
  303  * ARP protocol drain routine.  Called when memory is in short supply.
  304  * Called at splvm();
  305  */
  306 
  307 void
  308 arp_drain()
  309 {
  310         struct llinfo_arp *la, *nla;
  311         int count = 0;
  312         struct mbuf *mold;
  313 
  314         if (arp_lock_try(0) == 0) {
  315                 printf("arp_drain: locked; punting\n");
  316                 return;
  317         }
  318 
  319         for (la = LIST_FIRST(&llinfo_arp); la != 0; la = nla) {
  320                 nla = LIST_NEXT(la, la_list);
  321 
  322                 mold = la->la_hold;
  323                 la->la_hold = 0;
  324 
  325                 if (mold) {
  326                         m_freem(mold);
  327                         count++;
  328                 }
  329         }
  330         ARP_UNLOCK();
  331         arpstat.as_dfrdropped += count;
  332 }
  333 
  334 
  335 /*
  336  * Timeout routine.  Age arp_tab entries periodically.
  337  */
  338 /* ARGSUSED */
  339 static void
  340 arptimer(arg)
  341         void *arg;
  342 {
  343         int s;
  344         struct llinfo_arp *la, *nla;
  345 
  346         s = splsoftnet();
  347 
  348         if (arp_lock_try(0) == 0) {
  349                 /* get it later.. */
  350                 splx(s);
  351                 return;
  352         }
  353 
  354         callout_reset(&arptimer_ch, arpt_prune * hz, arptimer, NULL);
  355         for (la = LIST_FIRST(&llinfo_arp); la != 0; la = nla) {
  356                 struct rtentry *rt = la->la_rt;
  357 
  358                 nla = LIST_NEXT(la, la_list);
  359                 if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
  360                         arptfree(la); /* timer has expired; clear */
  361         }
  362 
  363         ARP_UNLOCK();
  364 
  365         splx(s);
  366 }
  367 
  368 /*
  369  * Parallel to llc_rtrequest.
  370  */
  371 void
  372 arp_rtrequest(req, rt, info)
  373         int req;
  374         struct rtentry *rt;
  375         struct rt_addrinfo *info;
  376 {
  377         struct sockaddr *gate = rt->rt_gateway;
  378         struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
  379         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
  380         size_t allocsize;
  381         struct mbuf *mold;
  382         int s;
  383         struct in_ifaddr *ia;
  384         struct ifaddr *ifa;
  385 
  386         if (!arpinit_done) {
  387                 arpinit_done = 1;
  388                 /*
  389                  * We generate expiration times from time.tv_sec
  390                  * so avoid accidently creating permanent routes.
  391                  */
  392                 if (time.tv_sec == 0) {
  393                         time.tv_sec++;
  394                 }
  395                 callout_init(&arptimer_ch);
  396                 callout_reset(&arptimer_ch, hz, arptimer, NULL);
  397         }
  398 
  399         if ((rt->rt_flags & RTF_GATEWAY) != 0) {
  400                 if (req != RTM_ADD)
  401                         return;
  402 
  403                 /*
  404                  * linklayers with particular link MTU limitation.
  405                  */
  406                 switch(rt->rt_ifp->if_type) {
  407 #if NFDDI > 0
  408                 case IFT_FDDI:
  409                         if (rt->rt_ifp->if_mtu > FDDIIPMTU)
  410                                 rt->rt_rmx.rmx_mtu = FDDIIPMTU;
  411                         break;
  412 #endif
  413 #if NARC > 0
  414                 case IFT_ARCNET:
  415                     {
  416                         int arcipifmtu;
  417 
  418                         if (rt->rt_ifp->if_flags & IFF_LINK0)
  419                                 arcipifmtu = arc_ipmtu;
  420                         else
  421                                 arcipifmtu = ARCMTU;
  422                         if (rt->rt_ifp->if_mtu > arcipifmtu)
  423                                 rt->rt_rmx.rmx_mtu = arcipifmtu;
  424                         break;
  425                     }
  426 #endif
  427                 }
  428                 return;
  429         }
  430 
  431         ARP_LOCK(1);            /* we may already be locked here. */
  432 
  433         switch (req) {
  434 
  435         case RTM_ADD:
  436                 /*
  437                  * XXX: If this is a manually added route to interface
  438                  * such as older version of routed or gated might provide,
  439                  * restore cloning bit.
  440                  */
  441                 if ((rt->rt_flags & RTF_HOST) == 0 &&
  442                     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
  443                         rt->rt_flags |= RTF_CLONING;
  444                 if (rt->rt_flags & RTF_CLONING) {
  445                         /*
  446                          * Case 1: This route should come from a route to iface.
  447                          */
  448                         rt_setgate(rt, rt_key(rt),
  449                                         (struct sockaddr *)&null_sdl);
  450                         gate = rt->rt_gateway;
  451                         SDL(gate)->sdl_type = rt->rt_ifp->if_type;
  452                         SDL(gate)->sdl_index = rt->rt_ifp->if_index;
  453                         /*
  454                          * Give this route an expiration time, even though
  455                          * it's a "permanent" route, so that routes cloned
  456                          * from it do not need their expiration time set.
  457                          */
  458                         rt->rt_expire = time.tv_sec;
  459                         /*
  460                          * linklayers with particular link MTU limitation.
  461                          */
  462                         switch (rt->rt_ifp->if_type) {
  463 #if NFDDI > 0
  464                         case IFT_FDDI:
  465                                 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
  466                                     (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
  467                                      (rt->rt_rmx.rmx_mtu == 0 &&
  468                                       rt->rt_ifp->if_mtu > FDDIIPMTU)))
  469                                         rt->rt_rmx.rmx_mtu = FDDIIPMTU;
  470                                 break;
  471 #endif
  472 #if NARC > 0
  473                         case IFT_ARCNET:
  474                             {
  475                                 int arcipifmtu;
  476                                 if (rt->rt_ifp->if_flags & IFF_LINK0)
  477                                         arcipifmtu = arc_ipmtu;
  478                                 else
  479                                         arcipifmtu = ARCMTU;
  480 
  481                                 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
  482                                     (rt->rt_rmx.rmx_mtu > arcipifmtu ||
  483                                      (rt->rt_rmx.rmx_mtu == 0 &&
  484                                       rt->rt_ifp->if_mtu > arcipifmtu)))
  485                                         rt->rt_rmx.rmx_mtu = arcipifmtu;
  486                                 break;
  487                             }
  488 #endif
  489                         }
  490                         break;
  491                 }
  492                 /* Announce a new entry if requested. */
  493                 if (rt->rt_flags & RTF_ANNOUNCE)
  494                         arprequest(rt->rt_ifp,
  495                             &SIN(rt_key(rt))->sin_addr,
  496                             &SIN(rt_key(rt))->sin_addr,
  497                             (u_char *)LLADDR(SDL(gate)));
  498                 /*FALLTHROUGH*/
  499         case RTM_RESOLVE:
  500                 if (gate->sa_family != AF_LINK ||
  501                     gate->sa_len < sizeof(null_sdl)) {
  502                         log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
  503                         break;
  504                 }
  505                 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
  506                 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
  507                 if (la != 0)
  508                         break; /* This happens on a route change */
  509                 /*
  510                  * Case 2:  This route may come from cloning, or a manual route
  511                  * add with a LL address.
  512                  */
  513                 switch (SDL(gate)->sdl_type) {
  514 #if NTOKEN > 0
  515                 case IFT_ISO88025:
  516                         allocsize = sizeof(*la) + sizeof(struct token_rif);
  517                         break;
  518 #endif /* NTOKEN > 0 */
  519                 default:
  520                         allocsize = sizeof(*la);
  521                 }
  522                 R_Malloc(la, struct llinfo_arp *, allocsize);
  523                 rt->rt_llinfo = (caddr_t)la;
  524                 if (la == 0) {
  525                         log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
  526                         break;
  527                 }
  528                 arp_inuse++, arp_allocated++;
  529                 Bzero(la, allocsize);
  530                 la->la_rt = rt;
  531                 rt->rt_flags |= RTF_LLINFO;
  532                 LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
  533 
  534                 INADDR_TO_IA(SIN(rt_key(rt))->sin_addr, ia);
  535                 while (ia && ia->ia_ifp != rt->rt_ifp)
  536                         NEXT_IA_WITH_SAME_ADDR(ia);
  537                 if (ia) {
  538                         /*
  539                          * This test used to be
  540                          *      if (loif.if_flags & IFF_UP)
  541                          * It allowed local traffic to be forced through
  542                          * the hardware by configuring the loopback down.
  543                          * However, it causes problems during network
  544                          * configuration for boards that can't receive
  545                          * packets they send.  It is now necessary to clear
  546                          * "useloopback" and remove the route to force
  547                          * traffic out to the hardware.
  548                          *
  549                          * In 4.4BSD, the above "if" statement checked
  550                          * rt->rt_ifa against rt_key(rt).  It was changed
  551                          * to the current form so that we can provide a
  552                          * better support for multiple IPv4 addresses on a
  553                          * interface.
  554                          */
  555                         rt->rt_expire = 0;
  556                         Bcopy(LLADDR(rt->rt_ifp->if_sadl),
  557                             LLADDR(SDL(gate)),
  558                             SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
  559 #if NLOOP > 0
  560                         if (useloopback)
  561                                 rt->rt_ifp = &loif[0];
  562 #endif
  563                         /*
  564                          * make sure to set rt->rt_ifa to the interface
  565                          * address we are using, otherwise we will have trouble
  566                          * with source address selection.
  567                          */
  568                         ifa = &ia->ia_ifa;
  569                         if (ifa != rt->rt_ifa) {
  570                                 IFAFREE(rt->rt_ifa);
  571                                 IFAREF(ifa);
  572                                 rt->rt_ifa = ifa;
  573                         }
  574                 }
  575                 break;
  576 
  577         case RTM_DELETE:
  578                 if (la == 0)
  579                         break;
  580                 arp_inuse--;
  581                 LIST_REMOVE(la, la_list);
  582                 rt->rt_llinfo = 0;
  583                 rt->rt_flags &= ~RTF_LLINFO;
  584 
  585                 s = splnet();
  586                 mold = la->la_hold;
  587                 la->la_hold = 0;
  588                 splx(s);
  589 
  590                 if (mold)
  591                         m_freem(mold);
  592 
  593                 Free((caddr_t)la);
  594         }
  595         ARP_UNLOCK();
  596 }
  597 
  598 /*
  599  * Broadcast an ARP request. Caller specifies:
  600  *      - arp header source ip address
  601  *      - arp header target ip address
  602  *      - arp header source ethernet address
  603  */
  604 static void
  605 arprequest(ifp, sip, tip, enaddr)
  606         struct ifnet *ifp;
  607         struct in_addr *sip, *tip;
  608         u_int8_t *enaddr;
  609 {
  610         struct mbuf *m;
  611         struct arphdr *ah;
  612         struct sockaddr sa;
  613 
  614         if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
  615                 return;
  616         MCLAIM(m, &arpdomain.dom_mowner);
  617         switch (ifp->if_type) {
  618         case IFT_IEEE1394:
  619                 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
  620                     ifp->if_addrlen;
  621                 break;
  622         default:
  623                 m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
  624                     2 * ifp->if_addrlen;
  625                 break;
  626         }
  627         m->m_pkthdr.len = m->m_len;
  628         MH_ALIGN(m, m->m_len);
  629         ah = mtod(m, struct arphdr *);
  630         bzero((caddr_t)ah, m->m_len);
  631         switch (ifp->if_type) {
  632         case IFT_IEEE1394:      /* RFC2734 */
  633                 /* fill it now for ar_tpa computation */
  634                 ah->ar_hrd = htons(ARPHRD_IEEE1394);
  635                 break;
  636         default:
  637                 /* ifp->if_output will fill ar_hrd */
  638                 break;
  639         }
  640         ah->ar_pro = htons(ETHERTYPE_IP);
  641         ah->ar_hln = ifp->if_addrlen;           /* hardware address length */
  642         ah->ar_pln = sizeof(struct in_addr);    /* protocol address length */
  643         ah->ar_op = htons(ARPOP_REQUEST);
  644         bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
  645         bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
  646         bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
  647         sa.sa_family = AF_ARP;
  648         sa.sa_len = 2;
  649         m->m_flags |= M_BCAST;
  650         arpstat.as_sndtotal++;
  651         arpstat.as_sndrequest++;
  652         (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
  653 }
  654 
  655 /*
  656  * Resolve an IP address into an ethernet address.  If success,
  657  * desten is filled in.  If there is no entry in arptab,
  658  * set one up and broadcast a request for the IP address.
  659  * Hold onto this mbuf and resend it once the address
  660  * is finally resolved.  A return value of 1 indicates
  661  * that desten has been filled in and the packet should be sent
  662  * normally; a 0 return indicates that the packet has been
  663  * taken over here, either now or for later transmission.
  664  */
  665 int
  666 arpresolve(ifp, rt, m, dst, desten)
  667         struct ifnet *ifp;
  668         struct rtentry *rt;
  669         struct mbuf *m;
  670         struct sockaddr *dst;
  671         u_char *desten;
  672 {
  673         struct llinfo_arp *la;
  674         struct sockaddr_dl *sdl;
  675         struct mbuf *mold;
  676         int s;
  677 
  678         if (rt)
  679                 la = (struct llinfo_arp *)rt->rt_llinfo;
  680         else {
  681                 if ((la = arplookup(m, &SIN(dst)->sin_addr, 1, 0)) != NULL)
  682                         rt = la->la_rt;
  683         }
  684         if (la == 0 || rt == 0) {
  685                 arpstat.as_allocfail++;
  686                 log(LOG_DEBUG,
  687                     "arpresolve: can't allocate llinfo on %s for %s\n",
  688                     ifp->if_xname, in_fmtaddr(SIN(dst)->sin_addr));
  689                 m_freem(m);
  690                 return (0);
  691         }
  692         sdl = SDL(rt->rt_gateway);
  693         /*
  694          * Check the address family and length is valid, the address
  695          * is resolved; otherwise, try to resolve.
  696          */
  697         if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
  698             sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
  699                 bcopy(LLADDR(sdl), desten,
  700                     min(sdl->sdl_alen, ifp->if_addrlen));
  701                 return 1;
  702         }
  703         /*
  704          * There is an arptab entry, but no ethernet address
  705          * response yet.  Replace the held mbuf with this
  706          * latest one.
  707          */
  708 
  709         arpstat.as_dfrtotal++;
  710         s = splnet();
  711         mold = la->la_hold;
  712         la->la_hold = m;
  713         splx(s);
  714 
  715         if (mold) {
  716                 arpstat.as_dfrdropped++;
  717                 m_freem(mold);
  718         }
  719 
  720         /*
  721          * Re-send the ARP request when appropriate.
  722          */
  723 #ifdef  DIAGNOSTIC
  724         if (rt->rt_expire == 0) {
  725                 /* This should never happen. (Should it? -gwr) */
  726                 printf("arpresolve: unresolved and rt_expire == 0\n");
  727                 /* Set expiration time to now (expired). */
  728                 rt->rt_expire = time.tv_sec;
  729         }
  730 #endif
  731         if (rt->rt_expire) {
  732                 rt->rt_flags &= ~RTF_REJECT;
  733                 if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
  734                         rt->rt_expire = time.tv_sec;
  735                         if (la->la_asked++ < arp_maxtries)
  736                                 arprequest(ifp,
  737                                     &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
  738                                     &SIN(dst)->sin_addr,
  739                                     LLADDR(ifp->if_sadl));
  740                         else {
  741                                 rt->rt_flags |= RTF_REJECT;
  742                                 rt->rt_expire += arpt_down;
  743                                 la->la_asked = 0;
  744                         }
  745                 }
  746         }
  747         return (0);
  748 }
  749 
  750 /*
  751  * Common length and type checks are done here,
  752  * then the protocol-specific routine is called.
  753  */
  754 void
  755 arpintr()
  756 {
  757         struct mbuf *m;
  758         struct arphdr *ar;
  759         int s;
  760         int arplen;
  761 
  762         while (arpintrq.ifq_head) {
  763                 s = splnet();
  764                 IF_DEQUEUE(&arpintrq, m);
  765                 splx(s);
  766                 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
  767                         panic("arpintr");
  768 
  769                 MCLAIM(m, &arpdomain.dom_mowner);
  770                 arpstat.as_rcvtotal++;
  771 
  772                 /*
  773                  * First, make sure we have at least struct arphdr.
  774                  */
  775                 if (m->m_len < sizeof(struct arphdr) ||
  776                     (ar = mtod(m, struct arphdr *)) == NULL)
  777                         goto badlen;
  778 
  779                 switch (m->m_pkthdr.rcvif->if_type) {
  780                 case IFT_IEEE1394:
  781                         arplen = sizeof(struct arphdr) +
  782                             ar->ar_hln + 2 * ar->ar_pln;
  783                         break;
  784                 default:
  785                         arplen = sizeof(struct arphdr) +
  786                             2 * ar->ar_hln + 2 * ar->ar_pln;
  787                         break;
  788                 }
  789 
  790                 if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
  791                     m->m_len >= arplen)
  792                         switch (ntohs(ar->ar_pro)) {
  793                         case ETHERTYPE_IP:
  794                         case ETHERTYPE_IPTRAILERS:
  795                                 in_arpinput(m);
  796                                 continue;
  797                         default:
  798                                 arpstat.as_rcvbadproto++;
  799                         }
  800                 else {
  801 badlen:
  802                         arpstat.as_rcvbadlen++;
  803                 }
  804                 m_freem(m);
  805         }
  806 }
  807 
  808 /*
  809  * ARP for Internet protocols on 10 Mb/s Ethernet.
  810  * Algorithm is that given in RFC 826.
  811  * In addition, a sanity check is performed on the sender
  812  * protocol address, to catch impersonators.
  813  * We no longer handle negotiations for use of trailer protocol:
  814  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
  815  * along with IP replies if we wanted trailers sent to us,
  816  * and also sent them in response to IP replies.
  817  * This allowed either end to announce the desire to receive
  818  * trailer packets.
  819  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
  820  * but formerly didn't normally send requests.
  821  */
  822 static void
  823 in_arpinput(m)
  824         struct mbuf *m;
  825 {
  826         struct arphdr *ah;
  827         struct ifnet *ifp = m->m_pkthdr.rcvif;
  828         struct llinfo_arp *la = 0;
  829         struct rtentry  *rt;
  830         struct in_ifaddr *ia;
  831 #if NBRIDGE > 0
  832         struct in_ifaddr *bridge_ia = NULL;
  833 #endif
  834         struct sockaddr_dl *sdl;
  835         struct sockaddr sa;
  836         struct in_addr isaddr, itaddr, myaddr;
  837         int op;
  838         struct mbuf *mold;
  839         int s;
  840 
  841         ah = mtod(m, struct arphdr *);
  842         op = ntohs(ah->ar_op);
  843 
  844         /*
  845          * Fix up ah->ar_hrd if necessary, before using ar_tha() or
  846          * ar_tpa().
  847          */
  848         switch (ifp->if_type) {
  849         case IFT_IEEE1394:
  850                 if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
  851                         ;
  852                 else {
  853                         /* XXX this is to make sure we compute ar_tha right */
  854                         /* XXX check ar_hrd more strictly? */
  855                         ah->ar_hrd = htons(ARPHRD_IEEE1394);
  856                 }
  857                 break;
  858         default:
  859                 /* XXX check ar_hrd? */
  860                 break;
  861         }
  862 
  863         bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr));
  864         bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr));
  865 
  866         if (m->m_flags & (M_BCAST|M_MCAST))
  867                 arpstat.as_rcvmcast++;
  868 
  869         /*
  870          * If the target IP address is zero, ignore the packet.
  871          * This prevents the code below from tring to answer
  872          * when we are using IP address zero (booting).
  873          */
  874         if (in_nullhost(itaddr)) {
  875                 arpstat.as_rcvzerotpa++;
  876                 goto out;
  877         }
  878 
  879         /*
  880          * If the source IP address is zero, this is most likely a
  881          * confused host trying to use IP address zero. (Windoze?)
  882          * XXX: Should we bother trying to reply to these?
  883          */
  884         if (in_nullhost(isaddr)) {
  885                 arpstat.as_rcvzerospa++;
  886                 goto out;
  887         }
  888 
  889         /*
  890          * Search for a matching interface address
  891          * or any address on the interface to use
  892          * as a dummy address in the rest of this function
  893          */
  894         INADDR_TO_IA(itaddr, ia);
  895         while (ia != NULL) {
  896                 if (ia->ia_ifp == m->m_pkthdr.rcvif)
  897                         break;
  898 
  899 #if NBRIDGE > 0
  900                 /*
  901                  * If the interface we received the packet on
  902                  * is part of a bridge, check to see if we need
  903                  * to "bridge" the packet to ourselves at this
  904                  * layer.  Note we still prefer a perfect match,
  905                  * but allow this weaker match if necessary.
  906                  */
  907                 if (m->m_pkthdr.rcvif->if_bridge != NULL &&
  908                     m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge)
  909                         bridge_ia = ia;
  910 #endif /* NBRIDGE > 0 */
  911 
  912                 NEXT_IA_WITH_SAME_ADDR(ia);
  913         }
  914 
  915 #if NBRIDGE > 0
  916         if (ia == NULL && bridge_ia != NULL) {
  917                 ia = bridge_ia;
  918                 ifp = bridge_ia->ia_ifp;
  919         }
  920 #endif
  921 
  922         if (ia == NULL) {
  923                 INADDR_TO_IA(isaddr, ia);
  924                 while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
  925                         NEXT_IA_WITH_SAME_ADDR(ia);
  926 
  927                 if (ia == NULL) {
  928                         IFP_TO_IA(ifp, ia);
  929                         if (ia == NULL) {
  930                                 arpstat.as_rcvnoint++;
  931                                 goto out;
  932                         }
  933                 }
  934         }
  935 
  936         myaddr = ia->ia_addr.sin_addr;
  937 
  938         /* XXX checks for bridge case? */
  939         if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
  940             ifp->if_addrlen)) {
  941                 arpstat.as_rcvlocalsha++;
  942                 goto out;       /* it's from me, ignore it. */
  943         }
  944 
  945         /* XXX checks for bridge case? */
  946         if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)ifp->if_broadcastaddr,
  947             ifp->if_addrlen)) {
  948                 arpstat.as_rcvbcastsha++;
  949                 log(LOG_ERR,
  950                     "%s: arp: link address is broadcast for IP address %s!\n",
  951                     ifp->if_xname, in_fmtaddr(isaddr));
  952                 goto out;
  953         }
  954 
  955         if (in_hosteq(isaddr, myaddr)) {
  956                 arpstat.as_rcvlocalspa++;
  957                 log(LOG_ERR,
  958                    "duplicate IP address %s sent from link address %s\n",
  959                    in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
  960                 itaddr = myaddr;
  961                 goto reply;
  962         }
  963         la = arplookup(m, &isaddr, in_hosteq(itaddr, myaddr), 0);
  964         if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
  965                 if (sdl->sdl_alen &&
  966                     bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
  967                         if (rt->rt_flags & RTF_STATIC) {
  968                                 arpstat.as_rcvoverperm++;
  969                                 log(LOG_INFO,
  970                                     "%s tried to overwrite permanent arp info"
  971                                     " for %s\n",
  972                                     lla_snprintf(ar_sha(ah), ah->ar_hln),
  973                                     in_fmtaddr(isaddr));
  974                                 goto out;
  975                         } else if (rt->rt_ifp != ifp) {
  976                                 arpstat.as_rcvoverint++;
  977                                 log(LOG_INFO,
  978                                     "%s on %s tried to overwrite "
  979                                     "arp info for %s on %s\n",
  980                                     lla_snprintf(ar_sha(ah), ah->ar_hln),
  981                                     ifp->if_xname, in_fmtaddr(isaddr),
  982                                     rt->rt_ifp->if_xname);
  983                                     goto out;
  984                         } else {
  985                                 arpstat.as_rcvover++;
  986                                 log(LOG_INFO,
  987                                     "arp info overwritten for %s by %s\n",
  988                                     in_fmtaddr(isaddr),
  989                                     lla_snprintf(ar_sha(ah), ah->ar_hln));
  990                         }
  991                 }
  992                 /*
  993                  * sanity check for the address length.
  994                  * XXX this does not work for protocols with variable address
  995                  * length. -is
  996                  */
  997                 if (sdl->sdl_alen &&
  998                     sdl->sdl_alen != ah->ar_hln) {
  999                         arpstat.as_rcvlenchg++;
 1000                         log(LOG_WARNING,
 1001                             "arp from %s: new addr len %d, was %d",
 1002                             in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
 1003                 }
 1004                 if (ifp->if_addrlen != ah->ar_hln) {
 1005                         arpstat.as_rcvbadlen++;
 1006                         log(LOG_WARNING,
 1007                             "arp from %s: addr len: new %d, i/f %d (ignored)",
 1008                             in_fmtaddr(isaddr), ah->ar_hln,
 1009                             ifp->if_addrlen);
 1010                         goto reply;
 1011                 }
 1012 #if NTOKEN > 0
 1013                 /*
 1014                  * XXX uses m_data and assumes the complete answer including
 1015                  * XXX token-ring headers is in the same buf
 1016                  */
 1017                 if (ifp->if_type == IFT_ISO88025) {
 1018                         struct token_header *trh;
 1019 
 1020                         trh = (struct token_header *)M_TRHSTART(m);
 1021                         if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
 1022                                 struct token_rif        *rif;
 1023                                 size_t  riflen;
 1024 
 1025                                 rif = TOKEN_RIF(trh);
 1026                                 riflen = (ntohs(rif->tr_rcf) &
 1027                                     TOKEN_RCF_LEN_MASK) >> 8;
 1028 
 1029                                 if (riflen > 2 &&
 1030                                     riflen < sizeof(struct token_rif) &&
 1031                                     (riflen & 1) == 0) {
 1032                                         rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
 1033                                         rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
 1034                                         bcopy(rif, TOKEN_RIF(la), riflen);
 1035                                 }
 1036                         }
 1037                 }
 1038 #endif /* NTOKEN > 0 */
 1039                 bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
 1040                     sdl->sdl_alen = ah->ar_hln);
 1041                 if (rt->rt_expire)
 1042                         rt->rt_expire = time.tv_sec + arpt_keep;
 1043                 rt->rt_flags &= ~RTF_REJECT;
 1044                 la->la_asked = 0;
 1045 
 1046                 s = splnet();
 1047                 mold = la->la_hold;
 1048                 la->la_hold = 0;
 1049                 splx(s);
 1050 
 1051                 if (mold) {
 1052                         arpstat.as_dfrsent++;
 1053                         (*ifp->if_output)(ifp, mold, rt_key(rt), rt);
 1054                 }
 1055         }
 1056 reply:
 1057         if (op != ARPOP_REQUEST) {
 1058                 if (op == ARPOP_REPLY)
 1059                         arpstat.as_rcvreply++;
 1060         out:
 1061                 m_freem(m);
 1062                 return;
 1063         }
 1064         arpstat.as_rcvrequest++;
 1065         if (in_hosteq(itaddr, myaddr)) {
 1066                 /* I am the target */
 1067                 if (ar_tha(ah))
 1068                         bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah),
 1069                             ah->ar_hln);
 1070                 bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
 1071         } else {
 1072                 la = arplookup(m, &itaddr, 0, SIN_PROXY);
 1073                 if (la == 0)
 1074                         goto out;
 1075                 rt = la->la_rt;
 1076                 if (ar_tha(ah))
 1077                         bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah),
 1078                             ah->ar_hln);
 1079                 sdl = SDL(rt->rt_gateway);
 1080                 bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
 1081         }
 1082 
 1083         bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
 1084         bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
 1085         ah->ar_op = htons(ARPOP_REPLY);
 1086         ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
 1087         switch (ifp->if_type) {
 1088         case IFT_IEEE1394:
 1089                 /*
 1090                  * ieee1394 arp reply is broadcast
 1091                  */
 1092                 m->m_flags &= ~M_MCAST;
 1093                 m->m_flags |= M_BCAST;
 1094                 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
 1095                 break;
 1096 
 1097         default:
 1098                 m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
 1099                 m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
 1100                 break;
 1101         }
 1102         m->m_pkthdr.len = m->m_len;
 1103         sa.sa_family = AF_ARP;
 1104         sa.sa_len = 2;
 1105         arpstat.as_sndtotal++;
 1106         arpstat.as_sndreply++;
 1107         (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
 1108         return;
 1109 }
 1110 
 1111 /*
 1112  * Free an arp entry.
 1113  */
 1114 static void
 1115 arptfree(la)
 1116         struct llinfo_arp *la;
 1117 {
 1118         struct rtentry *rt = la->la_rt;
 1119         struct sockaddr_dl *sdl;
 1120 
 1121         ARP_LOCK_CHECK();
 1122 
 1123         if (rt == 0)
 1124                 panic("arptfree");
 1125         if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
 1126             sdl->sdl_family == AF_LINK) {
 1127                 sdl->sdl_alen = 0;
 1128                 la->la_asked = 0;
 1129                 rt->rt_flags &= ~RTF_REJECT;
 1130                 return;
 1131         }
 1132         rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
 1133             0, (struct rtentry **)0);
 1134 }
 1135 
 1136 /*
 1137  * Lookup or enter a new address in arptab.
 1138  */
 1139 static struct llinfo_arp *
 1140 arplookup(m, addr, create, proxy)
 1141         struct mbuf *m;
 1142         struct in_addr *addr;
 1143         int create, proxy;
 1144 {
 1145         struct arphdr *ah;
 1146         struct ifnet *ifp = m->m_pkthdr.rcvif;
 1147         struct rtentry *rt;
 1148         static struct sockaddr_inarp sin;
 1149         const char *why = 0;
 1150 
 1151         ah = mtod(m, struct arphdr *);
 1152         sin.sin_len = sizeof(sin);
 1153         sin.sin_family = AF_INET;
 1154         sin.sin_addr = *addr;
 1155         sin.sin_other = proxy ? SIN_PROXY : 0;
 1156         rt = rtalloc1(sintosa(&sin), create);
 1157         if (rt == 0)
 1158                 return (0);
 1159         rt->rt_refcnt--;
 1160 
 1161         if (rt->rt_flags & RTF_GATEWAY)
 1162                 why = "host is not on local network";
 1163         else if ((rt->rt_flags & RTF_LLINFO) == 0) {
 1164                 arpstat.as_allocfail++;
 1165                 why = "could not allocate llinfo";
 1166         } else if (rt->rt_gateway->sa_family != AF_LINK)
 1167                 why = "gateway route is not ours";
 1168         else
 1169                 return ((struct llinfo_arp *)rt->rt_llinfo);
 1170 
 1171         if (create) {
 1172                 log(LOG_DEBUG, "arplookup: unable to enter address"
 1173                     " for %s@%s on %s (%s)\n",
 1174                     in_fmtaddr(*addr), lla_snprintf(ar_sha(ah), ah->ar_hln),
 1175                     (ifp) ? ifp->if_xname : 0, why);
 1176                 if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_CLONED) != 0) {
 1177                         rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt),
 1178                             rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
 1179                 }
 1180         }
 1181         return (0);
 1182 }
 1183 
 1184 int
 1185 arpioctl(cmd, data)
 1186         u_long cmd;
 1187         caddr_t data;
 1188 {
 1189 
 1190         return (EOPNOTSUPP);
 1191 }
 1192 
 1193 void
 1194 arp_ifinit(ifp, ifa)
 1195         struct ifnet *ifp;
 1196         struct ifaddr *ifa;
 1197 {
 1198         struct in_addr *ip;
 1199 
 1200         /*
 1201          * Warn the user if another station has this IP address,
 1202          * but only if the interface IP address is not zero.
 1203          */
 1204         ip = &IA_SIN(ifa)->sin_addr;
 1205         if (!in_nullhost(*ip))
 1206                 arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
 1207 
 1208         ifa->ifa_rtrequest = arp_rtrequest;
 1209         ifa->ifa_flags |= RTF_CLONING;
 1210 }
 1211 
 1212 /*
 1213  * Called from 10 Mb/s Ethernet interrupt handlers
 1214  * when ether packet type ETHERTYPE_REVARP
 1215  * is received.  Common length and type checks are done here,
 1216  * then the protocol-specific routine is called.
 1217  */
 1218 void
 1219 revarpinput(m)
 1220         struct mbuf *m;
 1221 {
 1222         struct arphdr *ar;
 1223 
 1224         if (m->m_len < sizeof(struct arphdr))
 1225                 goto out;
 1226         ar = mtod(m, struct arphdr *);
 1227 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
 1228         if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
 1229                 goto out;
 1230 #endif
 1231         if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
 1232                 goto out;
 1233         switch (ntohs(ar->ar_pro)) {
 1234         case ETHERTYPE_IP:
 1235         case ETHERTYPE_IPTRAILERS:
 1236                 in_revarpinput(m);
 1237                 return;
 1238 
 1239         default:
 1240                 break;
 1241         }
 1242 out:
 1243         m_freem(m);
 1244 }
 1245 
 1246 /*
 1247  * RARP for Internet protocols on 10 Mb/s Ethernet.
 1248  * Algorithm is that given in RFC 903.
 1249  * We are only using for bootstrap purposes to get an ip address for one of
 1250  * our interfaces.  Thus we support no user-interface.
 1251  *
 1252  * Since the contents of the RARP reply are specific to the interface that
 1253  * sent the request, this code must ensure that they are properly associated.
 1254  *
 1255  * Note: also supports ARP via RARP packets, per the RFC.
 1256  */
 1257 void
 1258 in_revarpinput(m)
 1259         struct mbuf *m;
 1260 {
 1261         struct ifnet *ifp;
 1262         struct arphdr *ah;
 1263         int op;
 1264 
 1265         ah = mtod(m, struct arphdr *);
 1266         op = ntohs(ah->ar_op);
 1267 
 1268         switch (m->m_pkthdr.rcvif->if_type) {
 1269         case IFT_IEEE1394:
 1270                 /* ARP without target hardware address is not supported */
 1271                 goto out;
 1272         default:
 1273                 break;
 1274         }
 1275 
 1276         switch (op) {
 1277         case ARPOP_REQUEST:
 1278         case ARPOP_REPLY:       /* per RFC */
 1279                 in_arpinput(m);
 1280                 return;
 1281         case ARPOP_REVREPLY:
 1282                 break;
 1283         case ARPOP_REVREQUEST:  /* handled by rarpd(8) */
 1284         default:
 1285                 goto out;
 1286         }
 1287         if (!revarp_in_progress)
 1288                 goto out;
 1289         ifp = m->m_pkthdr.rcvif;
 1290         if (ifp != myip_ifp) /* !same interface */
 1291                 goto out;
 1292         if (myip_initialized)
 1293                 goto wake;
 1294         if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
 1295                 goto out;
 1296         bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
 1297         bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
 1298         myip_initialized = 1;
 1299 wake:   /* Do wakeup every time in case it was missed. */
 1300         wakeup((caddr_t)&myip);
 1301 
 1302 out:
 1303         m_freem(m);
 1304 }
 1305 
 1306 /*
 1307  * Send a RARP request for the ip address of the specified interface.
 1308  * The request should be RFC 903-compliant.
 1309  */
 1310 void
 1311 revarprequest(ifp)
 1312         struct ifnet *ifp;
 1313 {
 1314         struct sockaddr sa;
 1315         struct mbuf *m;
 1316         struct arphdr *ah;
 1317 
 1318         if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
 1319                 return;
 1320         MCLAIM(m, &arpdomain.dom_mowner);
 1321         m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
 1322             2*ifp->if_addrlen;
 1323         m->m_pkthdr.len = m->m_len;
 1324         MH_ALIGN(m, m->m_len);
 1325         ah = mtod(m, struct arphdr *);
 1326         bzero((caddr_t)ah, m->m_len);
 1327         ah->ar_pro = htons(ETHERTYPE_IP);
 1328         ah->ar_hln = ifp->if_addrlen;           /* hardware address length */
 1329         ah->ar_pln = sizeof(struct in_addr);    /* protocol address length */
 1330         ah->ar_op = htons(ARPOP_REVREQUEST);
 1331 
 1332         bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
 1333         bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
 1334 
 1335         sa.sa_family = AF_ARP;
 1336         sa.sa_len = 2;
 1337         m->m_flags |= M_BCAST;
 1338         (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
 1339 
 1340 }
 1341 
 1342 /*
 1343  * RARP for the ip address of the specified interface, but also
 1344  * save the ip address of the server that sent the answer.
 1345  * Timeout if no response is received.
 1346  */
 1347 int
 1348 revarpwhoarewe(ifp, serv_in, clnt_in)
 1349         struct ifnet *ifp;
 1350         struct in_addr *serv_in;
 1351         struct in_addr *clnt_in;
 1352 {
 1353         int result, count = 20;
 1354 
 1355         myip_initialized = 0;
 1356         myip_ifp = ifp;
 1357 
 1358         revarp_in_progress = 1;
 1359         while (count--) {
 1360                 revarprequest(ifp);
 1361                 result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
 1362                 if (result != EWOULDBLOCK)
 1363                         break;
 1364         }
 1365         revarp_in_progress = 0;
 1366 
 1367         if (!myip_initialized)
 1368                 return ENETUNREACH;
 1369 
 1370         bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
 1371         bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
 1372         return 0;
 1373 }
 1374 
 1375 
 1376 
 1377 #ifdef DDB
 1378 
 1379 #include <machine/db_machdep.h>
 1380 #include <ddb/db_interface.h>
 1381 #include <ddb/db_output.h>
 1382 static void
 1383 db_print_sa(sa)
 1384         struct sockaddr *sa;
 1385 {
 1386         int len;
 1387         u_char *p;
 1388 
 1389         if (sa == 0) {
 1390                 db_printf("[NULL]");
 1391                 return;
 1392         }
 1393 
 1394         p = (u_char*)sa;
 1395         len = sa->sa_len;
 1396         db_printf("[");
 1397         while (len > 0) {
 1398                 db_printf("%d", *p);
 1399                 p++; len--;
 1400                 if (len) db_printf(",");
 1401         }
 1402         db_printf("]\n");
 1403 }
 1404 static void
 1405 db_print_ifa(ifa)
 1406         struct ifaddr *ifa;
 1407 {
 1408         if (ifa == 0)
 1409                 return;
 1410         db_printf("  ifa_addr=");
 1411         db_print_sa(ifa->ifa_addr);
 1412         db_printf("  ifa_dsta=");
 1413         db_print_sa(ifa->ifa_dstaddr);
 1414         db_printf("  ifa_mask=");
 1415         db_print_sa(ifa->ifa_netmask);
 1416         db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
 1417                           ifa->ifa_flags,
 1418                           ifa->ifa_refcnt,
 1419                           ifa->ifa_metric);
 1420 }
 1421 static void
 1422 db_print_llinfo(li)
 1423         caddr_t li;
 1424 {
 1425         struct llinfo_arp *la;
 1426 
 1427         if (li == 0)
 1428                 return;
 1429         la = (struct llinfo_arp *)li;
 1430         db_printf("  la_rt=%p la_hold=%p, la_asked=0x%lx\n",
 1431                           la->la_rt, la->la_hold, la->la_asked);
 1432 }
 1433 /*
 1434  * Function to pass to rn_walktree().
 1435  * Return non-zero error to abort walk.
 1436  */
 1437 static int
 1438 db_show_radix_node(rn, w)
 1439         struct radix_node *rn;
 1440         void *w;
 1441 {
 1442         struct rtentry *rt = (struct rtentry *)rn;
 1443 
 1444         db_printf("rtentry=%p", rt);
 1445 
 1446         db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
 1447                           rt->rt_flags, rt->rt_refcnt,
 1448                           rt->rt_use, rt->rt_expire);
 1449 
 1450         db_printf(" key="); db_print_sa(rt_key(rt));
 1451         db_printf(" mask="); db_print_sa(rt_mask(rt));
 1452         db_printf(" gw="); db_print_sa(rt->rt_gateway);
 1453 
 1454         db_printf(" ifp=%p ", rt->rt_ifp);
 1455         if (rt->rt_ifp)
 1456                 db_printf("(%s)", rt->rt_ifp->if_xname);
 1457         else
 1458                 db_printf("(NULL)");
 1459 
 1460         db_printf(" ifa=%p\n", rt->rt_ifa);
 1461         db_print_ifa(rt->rt_ifa);
 1462 
 1463         db_printf(" genmask="); db_print_sa(rt->rt_genmask);
 1464 
 1465         db_printf(" gwroute=%p llinfo=%p\n",
 1466                           rt->rt_gwroute, rt->rt_llinfo);
 1467         db_print_llinfo(rt->rt_llinfo);
 1468 
 1469         return (0);
 1470 }
 1471 /*
 1472  * Function to print all the route trees.
 1473  * Use this from ddb:  "show arptab"
 1474  */
 1475 void
 1476 db_show_arptab(addr, have_addr, count, modif)
 1477         db_expr_t       addr;
 1478         int             have_addr;
 1479         db_expr_t       count;
 1480         char *          modif;
 1481 {
 1482         struct radix_node_head *rnh;
 1483         rnh = rt_tables[AF_INET];
 1484         db_printf("Route tree for AF_INET\n");
 1485         if (rnh == NULL) {
 1486                 db_printf(" (not initialized)\n");
 1487                 return;
 1488         }
 1489         rn_walktree(rnh, db_show_radix_node, NULL);
 1490         return;
 1491 }
 1492 #endif
 1493 #endif /* INET */

Cache object: d6e9af05f89e79921a34383601f318c1


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