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_ether.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, 1988, 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  *      @(#)if_ether.c  8.1 (Berkeley) 6/10/93
   30  * $FreeBSD: releng/6.1/sys/netinet/if_ether.c 156989 2006-03-22 07:48:31Z glebius $
   31  */
   32 
   33 /*
   34  * Ethernet address resolution protocol.
   35  * TODO:
   36  *      add "inuse/lock" bit (or ref. count) along with valid bit
   37  */
   38 
   39 #include "opt_inet.h"
   40 #include "opt_bdg.h"
   41 #include "opt_mac.h"
   42 #include "opt_carp.h"
   43 
   44 #include <sys/param.h>
   45 #include <sys/kernel.h>
   46 #include <sys/queue.h>
   47 #include <sys/sysctl.h>
   48 #include <sys/systm.h>
   49 #include <sys/mac.h>
   50 #include <sys/mbuf.h>
   51 #include <sys/malloc.h>
   52 #include <sys/socket.h>
   53 #include <sys/syslog.h>
   54 
   55 #include <net/if.h>
   56 #include <net/if_dl.h>
   57 #include <net/if_types.h>
   58 #include <net/route.h>
   59 #include <net/netisr.h>
   60 #include <net/if_llc.h>
   61 #include <net/ethernet.h>
   62 #include <net/bridge.h>
   63 
   64 #include <netinet/in.h>
   65 #include <netinet/in_var.h>
   66 #include <netinet/if_ether.h>
   67 
   68 #include <net/if_arc.h>
   69 #include <net/iso88025.h>
   70 
   71 #ifdef DEV_CARP
   72 #include <netinet/ip_carp.h>
   73 #endif
   74 
   75 #define SIN(s) ((struct sockaddr_in *)s)
   76 #define SDL(s) ((struct sockaddr_dl *)s)
   77 
   78 SYSCTL_DECL(_net_link_ether);
   79 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
   80 
   81 /* timer values */
   82 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
   83 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
   84 
   85 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
   86            &arpt_prune, 0, "");
   87 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 
   88            &arpt_keep, 0, "");
   89 
   90 #define rt_expire rt_rmx.rmx_expire
   91 
   92 struct llinfo_arp {
   93         LIST_ENTRY(llinfo_arp) la_le;
   94         struct  rtentry *la_rt;
   95         struct  mbuf *la_hold;  /* last packet until resolved/timeout */
   96         u_short la_preempt;     /* countdown for pre-expiry arps */
   97         u_short la_asked;       /* # requests sent */
   98 };
   99 
  100 static  LIST_HEAD(, llinfo_arp) llinfo_arp;
  101 
  102 static struct   ifqueue arpintrq;
  103 static int      arp_allocated;
  104 
  105 static int      arp_maxtries = 5;
  106 static int      useloopback = 1; /* use loopback interface for local traffic */
  107 static int      arp_proxyall = 0;
  108 static struct callout arp_callout;
  109 
  110 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
  111            &arp_maxtries, 0, "");
  112 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
  113            &useloopback, 0, "");
  114 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
  115            &arp_proxyall, 0, "");
  116 
  117 static void     arp_init(void);
  118 static void     arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
  119 static void     arprequest(struct ifnet *,
  120                         struct in_addr *, struct in_addr *, u_char *);
  121 static void     arpintr(struct mbuf *);
  122 static void     arptimer(void *);
  123 static struct rtentry
  124                 *arplookup(u_long, int, int);
  125 #ifdef INET
  126 static void     in_arpinput(struct mbuf *);
  127 #endif
  128 
  129 /*
  130  * Timeout routine.  Age arp_tab entries periodically.
  131  */
  132 /* ARGSUSED */
  133 static void
  134 arptimer(void * __unused unused)
  135 {
  136         struct llinfo_arp *la, *ola;
  137 
  138         RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]);
  139         LIST_FOREACH_SAFE(la, &llinfo_arp, la_le, ola) {
  140                 struct rtentry *rt = la->la_rt;
  141 
  142                 RT_LOCK(rt);
  143                 if (rt->rt_expire && rt->rt_expire <= time_second) {
  144                         struct sockaddr_dl *sdl = SDL(rt->rt_gateway);
  145 
  146                         KASSERT(sdl->sdl_family == AF_LINK, ("sdl_family %d",
  147                             sdl->sdl_family));
  148                         if (rt->rt_refcnt > 1) {
  149                                 sdl->sdl_alen = 0;
  150                                 la->la_preempt = la->la_asked = 0;
  151                                 RT_UNLOCK(rt);
  152                                 continue;
  153                         }
  154                         RT_UNLOCK(rt);
  155                         /*
  156                          * XXX: LIST_REMOVE() is deep inside rtrequest().
  157                          */
  158                         rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0,
  159                             NULL);
  160                         continue;
  161                 }
  162                 RT_UNLOCK(rt);
  163         }
  164         RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]);
  165 
  166         callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL);
  167 }
  168 
  169 /*
  170  * Parallel to llc_rtrequest.
  171  */
  172 static void
  173 arp_rtrequest(req, rt, info)
  174         int req;
  175         struct rtentry *rt;
  176         struct rt_addrinfo *info;
  177 {
  178         struct sockaddr *gate;
  179         struct llinfo_arp *la;
  180         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
  181         struct in_ifaddr *ia;
  182         struct ifaddr *ifa;
  183 
  184         RT_LOCK_ASSERT(rt);
  185 
  186         if (rt->rt_flags & RTF_GATEWAY)
  187                 return;
  188         gate = rt->rt_gateway;
  189         la = (struct llinfo_arp *)rt->rt_llinfo;
  190         switch (req) {
  191 
  192         case RTM_ADD:
  193                 /*
  194                  * XXX: If this is a manually added route to interface
  195                  * such as older version of routed or gated might provide,
  196                  * restore cloning bit.
  197                  */
  198                 if ((rt->rt_flags & RTF_HOST) == 0 &&
  199                     rt_mask(rt) != NULL &&
  200                     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
  201                         rt->rt_flags |= RTF_CLONING;
  202                 if (rt->rt_flags & RTF_CLONING) {
  203                         /*
  204                          * Case 1: This route should come from a route to iface.
  205                          */
  206                         rt_setgate(rt, rt_key(rt),
  207                                         (struct sockaddr *)&null_sdl);
  208                         gate = rt->rt_gateway;
  209                         SDL(gate)->sdl_type = rt->rt_ifp->if_type;
  210                         SDL(gate)->sdl_index = rt->rt_ifp->if_index;
  211                         rt->rt_expire = time_second;
  212                         break;
  213                 }
  214                 /* Announce a new entry if requested. */
  215                 if (rt->rt_flags & RTF_ANNOUNCE)
  216                         arprequest(rt->rt_ifp,
  217                             &SIN(rt_key(rt))->sin_addr,
  218                             &SIN(rt_key(rt))->sin_addr,
  219                             (u_char *)LLADDR(SDL(gate)));
  220                 /*FALLTHROUGH*/
  221         case RTM_RESOLVE:
  222                 if (gate->sa_family != AF_LINK ||
  223                     gate->sa_len < sizeof(null_sdl)) {
  224                         log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__,
  225                             inet_ntoa(SIN(rt_key(rt))->sin_addr),
  226                             (gate->sa_family != AF_LINK) ?
  227                             " (!AF_LINK)": "");
  228                         break;
  229                 }
  230                 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
  231                 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
  232                 if (la != 0)
  233                         break; /* This happens on a route change */
  234                 /*
  235                  * Case 2:  This route may come from cloning, or a manual route
  236                  * add with a LL address.
  237                  */
  238                 R_Zalloc(la, struct llinfo_arp *, sizeof(*la));
  239                 rt->rt_llinfo = (caddr_t)la;
  240                 if (la == 0) {
  241                         log(LOG_DEBUG, "%s: malloc failed\n", __func__);
  242                         break;
  243                 }
  244                 arp_allocated++;
  245                 /*
  246                  * We are storing a route entry outside of radix tree. So,
  247                  * it can be found and accessed by other means than radix
  248                  * lookup. The routing code assumes that any rtentry detached
  249                  * from radix can be destroyed safely. To prevent this, we
  250                  * add an additional reference.
  251                  */
  252                 RT_ADDREF(rt);
  253                 la->la_rt = rt;
  254                 rt->rt_flags |= RTF_LLINFO;
  255                 RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
  256                 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
  257 
  258 #ifdef INET
  259                 /*
  260                  * This keeps the multicast addresses from showing up
  261                  * in `arp -a' listings as unresolved.  It's not actually
  262                  * functional.  Then the same for broadcast.
  263                  */
  264                 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) &&
  265                     rt->rt_ifp->if_type != IFT_ARCNET) {
  266                         ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
  267                                                LLADDR(SDL(gate)));
  268                         SDL(gate)->sdl_alen = 6;
  269                         rt->rt_expire = 0;
  270                 }
  271                 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
  272                         memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr,
  273                                rt->rt_ifp->if_addrlen);
  274                         SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen;
  275                         rt->rt_expire = 0;
  276                 }
  277 #endif
  278 
  279                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
  280                         if (ia->ia_ifp == rt->rt_ifp &&
  281                             SIN(rt_key(rt))->sin_addr.s_addr ==
  282                             (IA_SIN(ia))->sin_addr.s_addr)
  283                                 break;
  284                 }
  285                 if (ia) {
  286                     /*
  287                      * This test used to be
  288                      *  if (loif.if_flags & IFF_UP)
  289                      * It allowed local traffic to be forced
  290                      * through the hardware by configuring the loopback down.
  291                      * However, it causes problems during network configuration
  292                      * for boards that can't receive packets they send.
  293                      * It is now necessary to clear "useloopback" and remove
  294                      * the route to force traffic out to the hardware.
  295                      */
  296                         rt->rt_expire = 0;
  297                         bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)),
  298                               SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen);
  299                         if (useloopback)
  300                                 rt->rt_ifp = loif;
  301 
  302                     /*
  303                      * make sure to set rt->rt_ifa to the interface
  304                      * address we are using, otherwise we will have trouble
  305                      * with source address selection.
  306                      */
  307                         ifa = &ia->ia_ifa;
  308                         if (ifa != rt->rt_ifa) {
  309                                 IFAFREE(rt->rt_ifa);
  310                                 IFAREF(ifa);
  311                                 rt->rt_ifa = ifa;
  312                         }
  313                 }
  314                 break;
  315 
  316         case RTM_DELETE:
  317                 if (la == 0)
  318                         break;
  319                 RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]);
  320                 LIST_REMOVE(la, la_le);
  321                 RT_REMREF(rt);
  322                 rt->rt_llinfo = 0;
  323                 rt->rt_flags &= ~RTF_LLINFO;
  324                 if (la->la_hold)
  325                         m_freem(la->la_hold);
  326                 Free((caddr_t)la);
  327         }
  328 }
  329 
  330 /*
  331  * Broadcast an ARP request. Caller specifies:
  332  *      - arp header source ip address
  333  *      - arp header target ip address
  334  *      - arp header source ethernet address
  335  */
  336 static void
  337 arprequest(ifp, sip, tip, enaddr)
  338         struct ifnet *ifp;
  339         struct in_addr *sip, *tip;
  340         u_char *enaddr;
  341 {
  342         struct mbuf *m;
  343         struct arphdr *ah;
  344         struct sockaddr sa;
  345 
  346         if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
  347                 return;
  348         m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
  349                 2*ifp->if_data.ifi_addrlen;
  350         m->m_pkthdr.len = m->m_len;
  351         MH_ALIGN(m, m->m_len);
  352         ah = mtod(m, struct arphdr *);
  353         bzero((caddr_t)ah, m->m_len);
  354 #ifdef MAC
  355         mac_create_mbuf_linklayer(ifp, m);
  356 #endif
  357         ah->ar_pro = htons(ETHERTYPE_IP);
  358         ah->ar_hln = ifp->if_addrlen;           /* hardware address length */
  359         ah->ar_pln = sizeof(struct in_addr);    /* protocol address length */
  360         ah->ar_op = htons(ARPOP_REQUEST);
  361         bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
  362         bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
  363         bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
  364         sa.sa_family = AF_ARP;
  365         sa.sa_len = 2;
  366         m->m_flags |= M_BCAST;
  367         (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
  368 
  369         return;
  370 }
  371 
  372 /*
  373  * Resolve an IP address into an ethernet address.
  374  * On input:
  375  *    ifp is the interface we use
  376  *    dst is the next hop,
  377  *    rt0 is the route to the final destination (possibly useless)
  378  *    m is the mbuf
  379  *    desten is where we want the address.
  380  *
  381  * On success, desten is filled in and the function returns 0;
  382  * If the packet must be held pending resolution, we return EWOULDBLOCK
  383  * On other errors, we return the corresponding error code.
  384  */
  385 int
  386 arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
  387         struct sockaddr *dst, u_char *desten)
  388 {
  389         struct llinfo_arp *la = NULL;
  390         struct rtentry *rt = NULL;
  391         struct sockaddr_dl *sdl;
  392         int error;
  393 
  394         if (m->m_flags & M_BCAST) {     /* broadcast */
  395                 (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen);
  396                 return (0);
  397         }
  398         if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */
  399                 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
  400                 return (0);
  401         }
  402 
  403         if (rt0 != NULL) {
  404                 error = rt_check(&rt, &rt0, dst);
  405                 if (error) {
  406                         m_freem(m);
  407                         return error;
  408                 }
  409                 la = (struct llinfo_arp *)rt->rt_llinfo;
  410                 if (la == NULL)
  411                         RT_UNLOCK(rt);
  412         }
  413         if (la == NULL) {
  414                 /*
  415                  * We enter this block in case if rt0 was NULL,
  416                  * or if rt found by rt_check() didn't have llinfo.
  417                  */
  418                 rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
  419                 if (rt == NULL) {
  420                         log(LOG_DEBUG,
  421                             "arpresolve: can't allocate route for %s\n",
  422                             inet_ntoa(SIN(dst)->sin_addr));
  423                         m_freem(m);
  424                         return (EINVAL); /* XXX */
  425                 }
  426                 la = (struct llinfo_arp *)rt->rt_llinfo;
  427                 if (la == NULL) {
  428                         RT_UNLOCK(rt);
  429                         log(LOG_DEBUG,
  430                             "arpresolve: can't allocate llinfo for %s\n",
  431                             inet_ntoa(SIN(dst)->sin_addr));
  432                         m_freem(m);
  433                         return (EINVAL); /* XXX */
  434                 }
  435         }
  436         sdl = SDL(rt->rt_gateway);
  437         /*
  438          * Check the address family and length is valid, the address
  439          * is resolved; otherwise, try to resolve.
  440          */
  441         if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
  442             sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
  443 
  444                 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
  445 
  446                 /*
  447                  * If entry has an expiry time and it is approaching,
  448                  * send an ARP request.
  449                  */
  450                 if ((rt->rt_expire != 0) &&
  451                     (time_second + la->la_preempt > rt->rt_expire)) {
  452                         struct in_addr sin = 
  453                             SIN(rt->rt_ifa->ifa_addr)->sin_addr;
  454 
  455                         la->la_preempt--;
  456                         RT_UNLOCK(rt);
  457                         arprequest(ifp, &sin, &SIN(dst)->sin_addr,
  458                             IF_LLADDR(ifp));
  459                         return (0);
  460                 } 
  461 
  462                 RT_UNLOCK(rt);
  463                 return (0);
  464         }
  465         /*
  466          * If ARP is disabled or static on this interface, stop.
  467          * XXX
  468          * Probably should not allocate empty llinfo struct if we are
  469          * not going to be sending out an arp request.
  470          */
  471         if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) {
  472                 RT_UNLOCK(rt);
  473                 m_freem(m);
  474                 return (EINVAL);
  475         }
  476         /*
  477          * There is an arptab entry, but no ethernet address
  478          * response yet.  Replace the held mbuf with this
  479          * latest one.
  480          */
  481         if (la->la_hold)
  482                 m_freem(la->la_hold);
  483         la->la_hold = m;
  484 
  485         KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry"));
  486 
  487         /*
  488          * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
  489          * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
  490          * if we have already sent arp_maxtries ARP requests. Retransmit the
  491          * ARP request, but not faster than one request per second.
  492          */
  493         if (la->la_asked < arp_maxtries)
  494                 error = EWOULDBLOCK;    /* First request. */
  495         else
  496                 error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH;
  497 
  498         if (la->la_asked++ == 0 || rt->rt_expire != time_second) {
  499                 struct in_addr sin =
  500                     SIN(rt->rt_ifa->ifa_addr)->sin_addr;
  501 
  502                 rt->rt_expire = time_second;
  503                 RT_UNLOCK(rt);
  504 
  505                 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
  506                     IF_LLADDR(ifp));
  507         } else
  508                 RT_UNLOCK(rt);
  509 
  510         return (error);
  511 }
  512 
  513 /*
  514  * Common length and type checks are done here,
  515  * then the protocol-specific routine is called.
  516  */
  517 static void
  518 arpintr(struct mbuf *m)
  519 {
  520         struct arphdr *ar;
  521 
  522         if (m->m_len < sizeof(struct arphdr) &&
  523             ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
  524                 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
  525                 return;
  526         }
  527         ar = mtod(m, struct arphdr *);
  528 
  529         if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
  530             ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
  531             ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
  532             ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
  533                 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
  534                     (unsigned char *)&ar->ar_hrd, "");
  535                 m_freem(m);
  536                 return;
  537         }
  538 
  539         if (m->m_len < arphdr_len(ar)) {
  540                 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
  541                         log(LOG_ERR, "arp: runt packet\n");
  542                         m_freem(m);
  543                         return;
  544                 }
  545                 ar = mtod(m, struct arphdr *);
  546         }
  547 
  548         switch (ntohs(ar->ar_pro)) {
  549 #ifdef INET
  550         case ETHERTYPE_IP:
  551                 in_arpinput(m);
  552                 return;
  553 #endif
  554         }
  555         m_freem(m);
  556 }
  557 
  558 #ifdef INET
  559 /*
  560  * ARP for Internet protocols on 10 Mb/s Ethernet.
  561  * Algorithm is that given in RFC 826.
  562  * In addition, a sanity check is performed on the sender
  563  * protocol address, to catch impersonators.
  564  * We no longer handle negotiations for use of trailer protocol:
  565  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
  566  * along with IP replies if we wanted trailers sent to us,
  567  * and also sent them in response to IP replies.
  568  * This allowed either end to announce the desire to receive
  569  * trailer packets.
  570  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
  571  * but formerly didn't normally send requests.
  572  */
  573 static int log_arp_wrong_iface = 1;
  574 static int log_arp_movements = 1;
  575 static int log_arp_permanent_modify = 1;
  576 
  577 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
  578         &log_arp_wrong_iface, 0,
  579         "log arp packets arriving on the wrong interface");
  580 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
  581         &log_arp_movements, 0,
  582         "log arp replies from MACs different than the one in the cache");
  583 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
  584         &log_arp_permanent_modify, 0,
  585         "log arp replies from MACs different than the one in the permanent arp entry");
  586 
  587 
  588 static void
  589 in_arpinput(m)
  590         struct mbuf *m;
  591 {
  592         struct arphdr *ah;
  593         struct ifnet *ifp = m->m_pkthdr.rcvif;
  594         struct iso88025_header *th = (struct iso88025_header *)0;
  595         struct iso88025_sockaddr_dl_data *trld;
  596         struct llinfo_arp *la;
  597         struct rtentry *rt;
  598         struct ifaddr *ifa;
  599         struct in_ifaddr *ia;
  600         struct sockaddr_dl *sdl;
  601         struct sockaddr sa;
  602         struct in_addr isaddr, itaddr, myaddr;
  603         struct mbuf *hold;
  604         u_int8_t *enaddr = NULL;
  605         int op, rif_len;
  606         int req_len;
  607         int bridged = 0;
  608 #ifdef DEV_CARP
  609         int carp_match = 0;
  610 #endif
  611 
  612         if (do_bridge || ifp->if_bridge)
  613                 bridged = 1;
  614 
  615         req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
  616         if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
  617                 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
  618                 return;
  619         }
  620 
  621         ah = mtod(m, struct arphdr *);
  622         op = ntohs(ah->ar_op);
  623         (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
  624         (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
  625 
  626         /*
  627          * For a bridge, we want to check the address irrespective
  628          * of the receive interface. (This will change slightly
  629          * when we have clusters of interfaces).
  630          * If the interface does not match, but the recieving interface
  631          * is part of carp, we call carp_iamatch to see if this is a
  632          * request for the virtual host ip.
  633          * XXX: This is really ugly!
  634          */
  635         LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
  636                 if (((bridged && ia->ia_ifp->if_bridge != NULL) || do_bridge ||
  637                     (ia->ia_ifp == ifp)) &&
  638                     itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
  639                         goto match;
  640 #ifdef DEV_CARP
  641                 if (ifp->if_carp != NULL &&
  642                     carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
  643                     itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
  644                         carp_match = 1;
  645                         goto match;
  646                 }
  647 #endif
  648         }
  649         LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
  650                 if (((bridged && ia->ia_ifp->if_bridge != NULL) || do_bridge ||
  651                     (ia->ia_ifp == ifp)) &&
  652                     isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
  653                         goto match;
  654         /*
  655          * No match, use the first inet address on the receive interface
  656          * as a dummy address for the rest of the function.
  657          */
  658         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
  659                 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
  660                         ia = ifatoia(ifa);
  661                         goto match;
  662                 }
  663         /*
  664          * If bridging, fall back to using any inet address.
  665          */
  666         if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
  667                 goto drop;
  668 match:
  669         if (!enaddr)
  670                 enaddr = (u_int8_t *)IF_LLADDR(ifp);
  671         myaddr = ia->ia_addr.sin_addr;
  672         if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
  673                 goto drop;      /* it's from me, ignore it. */
  674         if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
  675                 log(LOG_ERR,
  676                     "arp: link address is broadcast for IP address %s!\n",
  677                     inet_ntoa(isaddr));
  678                 goto drop;
  679         }
  680         /*
  681          * Warn if another host is using the same IP address, but only if the
  682          * IP address isn't 0.0.0.0, which is used for DHCP only, in which
  683          * case we suppress the warning to avoid false positive complaints of
  684          * potential misconfiguration.
  685          */
  686         if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
  687                 log(LOG_ERR,
  688                    "arp: %*D is using my IP address %s!\n",
  689                    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
  690                    inet_ntoa(isaddr));
  691                 itaddr = myaddr;
  692                 goto reply;
  693         }
  694         if (ifp->if_flags & IFF_STATICARP)
  695                 goto reply;
  696         rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
  697         if (rt != NULL) {
  698                 la = (struct llinfo_arp *)rt->rt_llinfo;
  699                 if (la == NULL) {
  700                         RT_UNLOCK(rt);
  701                         goto reply;
  702                 }
  703         } else
  704                 goto reply;
  705 
  706         /* The following is not an error when doing bridging. */
  707         if (!bridged && rt->rt_ifp != ifp
  708 #ifdef DEV_CARP
  709             && (ifp->if_type != IFT_CARP || !carp_match)
  710 #endif
  711                                                         ) {
  712                 if (log_arp_wrong_iface)
  713                         log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
  714                             inet_ntoa(isaddr),
  715                             rt->rt_ifp->if_xname,
  716                             ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
  717                             ifp->if_xname);
  718                 RT_UNLOCK(rt);
  719                 goto reply;
  720         }
  721         sdl = SDL(rt->rt_gateway);
  722         if (sdl->sdl_alen &&
  723             bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
  724                 if (rt->rt_expire) {
  725                     if (log_arp_movements)
  726                         log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
  727                             inet_ntoa(isaddr),
  728                             ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
  729                             ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
  730                             ifp->if_xname);
  731                 } else {
  732                         RT_UNLOCK(rt);
  733                         if (log_arp_permanent_modify)
  734                                 log(LOG_ERR, "arp: %*D attempts to modify "
  735                                     "permanent entry for %s on %s\n",
  736                                     ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
  737                                     inet_ntoa(isaddr), ifp->if_xname);
  738                         goto reply;
  739                 }
  740         }
  741         /*
  742          * sanity check for the address length.
  743          * XXX this does not work for protocols with variable address
  744          * length. -is
  745          */
  746         if (sdl->sdl_alen &&
  747             sdl->sdl_alen != ah->ar_hln) {
  748                 log(LOG_WARNING,
  749                     "arp from %*D: new addr len %d, was %d",
  750                     ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
  751                     ah->ar_hln, sdl->sdl_alen);
  752         }
  753         if (ifp->if_addrlen != ah->ar_hln) {
  754                 log(LOG_WARNING,
  755                     "arp from %*D: addr len: new %d, i/f %d (ignored)",
  756                     ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
  757                     ah->ar_hln, ifp->if_addrlen);
  758                 RT_UNLOCK(rt);
  759                 goto reply;
  760         }
  761         (void)memcpy(LLADDR(sdl), ar_sha(ah),
  762             sdl->sdl_alen = ah->ar_hln);
  763         /*
  764          * If we receive an arp from a token-ring station over
  765          * a token-ring nic then try to save the source
  766          * routing info.
  767          */
  768         if (ifp->if_type == IFT_ISO88025) {
  769                 th = (struct iso88025_header *)m->m_pkthdr.header;
  770                 trld = SDL_ISO88025(sdl);
  771                 rif_len = TR_RCF_RIFLEN(th->rcf);
  772                 if ((th->iso88025_shost[0] & TR_RII) &&
  773                     (rif_len > 2)) {
  774                         trld->trld_rcf = th->rcf;
  775                         trld->trld_rcf ^= htons(TR_RCF_DIR);
  776                         memcpy(trld->trld_route, th->rd, rif_len - 2);
  777                         trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
  778                         /*
  779                          * Set up source routing information for
  780                          * reply packet (XXX)
  781                          */
  782                         m->m_data -= rif_len;
  783                         m->m_len  += rif_len;
  784                         m->m_pkthdr.len += rif_len;
  785                 } else {
  786                         th->iso88025_shost[0] &= ~TR_RII;
  787                         trld->trld_rcf = 0;
  788                 }
  789                 m->m_data -= 8;
  790                 m->m_len  += 8;
  791                 m->m_pkthdr.len += 8;
  792                 th->rcf = trld->trld_rcf;
  793         }
  794         if (rt->rt_expire)
  795                 rt->rt_expire = time_second + arpt_keep;
  796         la->la_asked = 0;
  797         la->la_preempt = arp_maxtries;
  798         hold = la->la_hold;
  799         la->la_hold = NULL;
  800         RT_UNLOCK(rt);
  801         if (hold != NULL)
  802                 (*ifp->if_output)(ifp, hold, rt_key(rt), rt);
  803 
  804 reply:
  805         if (op != ARPOP_REQUEST)
  806                 goto drop;
  807         if (itaddr.s_addr == myaddr.s_addr) {
  808                 /* I am the target */
  809                 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
  810                 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
  811         } else {
  812                 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
  813                 if (rt == NULL) {
  814                         struct sockaddr_in sin;
  815 
  816                         if (!arp_proxyall)
  817                                 goto drop;
  818 
  819                         bzero(&sin, sizeof sin);
  820                         sin.sin_family = AF_INET;
  821                         sin.sin_len = sizeof sin;
  822                         sin.sin_addr = itaddr;
  823 
  824                         rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
  825                         if (!rt)
  826                                 goto drop;
  827                         /*
  828                          * Don't send proxies for nodes on the same interface
  829                          * as this one came out of, or we'll get into a fight
  830                          * over who claims what Ether address.
  831                          */
  832                         if (rt->rt_ifp == ifp) {
  833                                 rtfree(rt);
  834                                 goto drop;
  835                         }
  836                         (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
  837                         (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
  838                         rtfree(rt);
  839 
  840                         /*
  841                          * Also check that the node which sent the ARP packet
  842                          * is on the the interface we expect it to be on. This
  843                          * avoids ARP chaos if an interface is connected to the
  844                          * wrong network.
  845                          */
  846                         sin.sin_addr = isaddr;
  847 
  848                         rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
  849                         if (!rt)
  850                                 goto drop;
  851                         if (rt->rt_ifp != ifp) {
  852                                 log(LOG_INFO, "arp_proxy: ignoring request"
  853                                     " from %s via %s, expecting %s\n",
  854                                     inet_ntoa(isaddr), ifp->if_xname,
  855                                     rt->rt_ifp->if_xname);
  856                                 rtfree(rt);
  857                                 goto drop;
  858                         }
  859                         rtfree(rt);
  860 
  861 #ifdef DEBUG_PROXY
  862                         printf("arp: proxying for %s\n",
  863                                inet_ntoa(itaddr));
  864 #endif
  865                 } else {
  866                         /*
  867                          * Return proxied ARP replies only on the interface
  868                          * where this network resides. Otherwise we may
  869                          * conflict with the host we are proxying for.
  870                          */
  871                         if (rt->rt_ifp != ifp) {
  872                                 RT_UNLOCK(rt);
  873                                 goto drop;
  874                         }
  875                         sdl = SDL(rt->rt_gateway);
  876                         (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
  877                         (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
  878                         RT_UNLOCK(rt);
  879                 }
  880         }
  881 
  882         (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
  883         (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
  884         ah->ar_op = htons(ARPOP_REPLY);
  885         ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
  886         m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
  887         m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);   
  888         m->m_pkthdr.len = m->m_len;   
  889         sa.sa_family = AF_ARP;
  890         sa.sa_len = 2;
  891         (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
  892         return;
  893 
  894 drop:
  895         m_freem(m);
  896 }
  897 #endif
  898 
  899 /*
  900  * Lookup or enter a new address in arptab.
  901  */
  902 static struct rtentry *
  903 arplookup(addr, create, proxy)
  904         u_long addr;
  905         int create, proxy;
  906 {
  907         struct rtentry *rt;
  908         struct sockaddr_inarp sin;
  909         const char *why = 0;
  910 
  911         bzero(&sin, sizeof(sin));
  912         sin.sin_len = sizeof(sin);
  913         sin.sin_family = AF_INET;
  914         sin.sin_addr.s_addr = addr;
  915         if (proxy)
  916                 sin.sin_other = SIN_PROXY;
  917         rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
  918         if (rt == 0)
  919                 return (0);
  920 
  921         if (rt->rt_flags & RTF_GATEWAY)
  922                 why = "host is not on local network";
  923         else if ((rt->rt_flags & RTF_LLINFO) == 0)
  924                 why = "could not allocate llinfo";
  925         else if (rt->rt_gateway->sa_family != AF_LINK)
  926                 why = "gateway route is not ours";
  927 
  928         if (why) {
  929 #define ISDYNCLONE(_rt) \
  930         (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
  931                 if (create)
  932                         log(LOG_DEBUG, "arplookup %s failed: %s\n",
  933                             inet_ntoa(sin.sin_addr), why);
  934                 /*
  935                  * If there are no references to this Layer 2 route,
  936                  * and it is a cloned route, and not static, and
  937                  * arplookup() is creating the route, then purge
  938                  * it from the routing table as it is probably bogus.
  939                  */
  940                 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
  941                         rtexpunge(rt);
  942                 RTFREE_LOCKED(rt);
  943                 return (0);
  944 #undef ISDYNCLONE
  945         } else {
  946                 RT_REMREF(rt);
  947                 return (rt);
  948         }
  949 }
  950 
  951 void
  952 arp_ifinit(ifp, ifa)
  953         struct ifnet *ifp;
  954         struct ifaddr *ifa;
  955 {
  956         if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
  957                 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
  958                                 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
  959         ifa->ifa_rtrequest = arp_rtrequest;
  960         ifa->ifa_flags |= RTF_CLONING;
  961 }
  962 
  963 void
  964 arp_ifinit2(ifp, ifa, enaddr)
  965         struct ifnet *ifp;
  966         struct ifaddr *ifa;
  967         u_char *enaddr;
  968 {
  969         if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
  970                 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
  971                                 &IA_SIN(ifa)->sin_addr, enaddr);
  972         ifa->ifa_rtrequest = arp_rtrequest;
  973         ifa->ifa_flags |= RTF_CLONING;
  974 }
  975 
  976 static void
  977 arp_init(void)
  978 {
  979 
  980         arpintrq.ifq_maxlen = 50;
  981         mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
  982         LIST_INIT(&llinfo_arp);
  983         callout_init(&arp_callout, CALLOUT_MPSAFE);
  984         netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
  985         callout_reset(&arp_callout, hz, arptimer, NULL);
  986 }
  987 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);

Cache object: 71e16cfe00dee9bc83db9c03dafa5878


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