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.3/sys/netinet/if_ether.c 167510 2007-03-13 21:59:20Z bms $
   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                 la->la_asked++;
  504                 RT_UNLOCK(rt);
  505 
  506                 arprequest(ifp, &sin, &SIN(dst)->sin_addr,
  507                     IF_LLADDR(ifp));
  508         } else
  509                 RT_UNLOCK(rt);
  510 
  511         return (error);
  512 }
  513 
  514 /*
  515  * Common length and type checks are done here,
  516  * then the protocol-specific routine is called.
  517  */
  518 static void
  519 arpintr(struct mbuf *m)
  520 {
  521         struct arphdr *ar;
  522 
  523         if (m->m_len < sizeof(struct arphdr) &&
  524             ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
  525                 log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
  526                 return;
  527         }
  528         ar = mtod(m, struct arphdr *);
  529 
  530         if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
  531             ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
  532             ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
  533             ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
  534                 log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
  535                     (unsigned char *)&ar->ar_hrd, "");
  536                 m_freem(m);
  537                 return;
  538         }
  539 
  540         if (m->m_len < arphdr_len(ar)) {
  541                 if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
  542                         log(LOG_ERR, "arp: runt packet\n");
  543                         m_freem(m);
  544                         return;
  545                 }
  546                 ar = mtod(m, struct arphdr *);
  547         }
  548 
  549         switch (ntohs(ar->ar_pro)) {
  550 #ifdef INET
  551         case ETHERTYPE_IP:
  552                 in_arpinput(m);
  553                 return;
  554 #endif
  555         }
  556         m_freem(m);
  557 }
  558 
  559 #ifdef INET
  560 /*
  561  * ARP for Internet protocols on 10 Mb/s Ethernet.
  562  * Algorithm is that given in RFC 826.
  563  * In addition, a sanity check is performed on the sender
  564  * protocol address, to catch impersonators.
  565  * We no longer handle negotiations for use of trailer protocol:
  566  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
  567  * along with IP replies if we wanted trailers sent to us,
  568  * and also sent them in response to IP replies.
  569  * This allowed either end to announce the desire to receive
  570  * trailer packets.
  571  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
  572  * but formerly didn't normally send requests.
  573  */
  574 static int log_arp_wrong_iface = 1;
  575 static int log_arp_movements = 1;
  576 static int log_arp_permanent_modify = 1;
  577 
  578 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
  579         &log_arp_wrong_iface, 0,
  580         "log arp packets arriving on the wrong interface");
  581 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
  582         &log_arp_movements, 0,
  583         "log arp replies from MACs different than the one in the cache");
  584 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
  585         &log_arp_permanent_modify, 0,
  586         "log arp replies from MACs different than the one in the permanent arp entry");
  587 
  588 
  589 static void
  590 in_arpinput(m)
  591         struct mbuf *m;
  592 {
  593         struct arphdr *ah;
  594         struct ifnet *ifp = m->m_pkthdr.rcvif;
  595         struct iso88025_header *th = (struct iso88025_header *)0;
  596         struct iso88025_sockaddr_dl_data *trld;
  597         struct llinfo_arp *la;
  598         struct rtentry *rt;
  599         struct ifaddr *ifa;
  600         struct in_ifaddr *ia;
  601         struct sockaddr_dl *sdl;
  602         struct sockaddr sa;
  603         struct in_addr isaddr, itaddr, myaddr;
  604         struct mbuf *hold;
  605         u_int8_t *enaddr = NULL;
  606         int op, rif_len;
  607         int req_len;
  608         int bridged = 0;
  609 #ifdef DEV_CARP
  610         int carp_match = 0;
  611 #endif
  612 
  613         if (do_bridge || ifp->if_bridge)
  614                 bridged = 1;
  615 
  616         req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
  617         if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
  618                 log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
  619                 return;
  620         }
  621 
  622         ah = mtod(m, struct arphdr *);
  623         op = ntohs(ah->ar_op);
  624         (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
  625         (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
  626 
  627         /*
  628          * For a bridge, we want to check the address irrespective
  629          * of the receive interface. (This will change slightly
  630          * when we have clusters of interfaces).
  631          * If the interface does not match, but the recieving interface
  632          * is part of carp, we call carp_iamatch to see if this is a
  633          * request for the virtual host ip.
  634          * XXX: This is really ugly!
  635          */
  636         LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
  637                 if (((bridged && ia->ia_ifp->if_bridge != NULL) || do_bridge ||
  638                     (ia->ia_ifp == ifp)) &&
  639                     itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
  640                         goto match;
  641 #ifdef DEV_CARP
  642                 if (ifp->if_carp != NULL &&
  643                     carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
  644                     itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
  645                         carp_match = 1;
  646                         goto match;
  647                 }
  648 #endif
  649         }
  650         LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
  651                 if (((bridged && ia->ia_ifp->if_bridge != NULL) || do_bridge ||
  652                     (ia->ia_ifp == ifp)) &&
  653                     isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
  654                         goto match;
  655         /*
  656          * No match, use the first inet address on the receive interface
  657          * as a dummy address for the rest of the function.
  658          */
  659         TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
  660                 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
  661                         ia = ifatoia(ifa);
  662                         goto match;
  663                 }
  664         /*
  665          * If bridging, fall back to using any inet address.
  666          */
  667         if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL)
  668                 goto drop;
  669 match:
  670         if (!enaddr)
  671                 enaddr = (u_int8_t *)IF_LLADDR(ifp);
  672         myaddr = ia->ia_addr.sin_addr;
  673         if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
  674                 goto drop;      /* it's from me, ignore it. */
  675         if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
  676                 log(LOG_ERR,
  677                     "arp: link address is broadcast for IP address %s!\n",
  678                     inet_ntoa(isaddr));
  679                 goto drop;
  680         }
  681         /*
  682          * Warn if another host is using the same IP address, but only if the
  683          * IP address isn't 0.0.0.0, which is used for DHCP only, in which
  684          * case we suppress the warning to avoid false positive complaints of
  685          * potential misconfiguration.
  686          */
  687         if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
  688                 log(LOG_ERR,
  689                    "arp: %*D is using my IP address %s!\n",
  690                    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
  691                    inet_ntoa(isaddr));
  692                 itaddr = myaddr;
  693                 goto reply;
  694         }
  695         if (ifp->if_flags & IFF_STATICARP)
  696                 goto reply;
  697         rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
  698         if (rt != NULL) {
  699                 la = (struct llinfo_arp *)rt->rt_llinfo;
  700                 if (la == NULL) {
  701                         RT_UNLOCK(rt);
  702                         goto reply;
  703                 }
  704         } else
  705                 goto reply;
  706 
  707         /* The following is not an error when doing bridging. */
  708         if (!bridged && rt->rt_ifp != ifp
  709 #ifdef DEV_CARP
  710             && (ifp->if_type != IFT_CARP || !carp_match)
  711 #endif
  712                                                         ) {
  713                 if (log_arp_wrong_iface)
  714                         log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n",
  715                             inet_ntoa(isaddr),
  716                             rt->rt_ifp->if_xname,
  717                             ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
  718                             ifp->if_xname);
  719                 RT_UNLOCK(rt);
  720                 goto reply;
  721         }
  722         sdl = SDL(rt->rt_gateway);
  723         if (sdl->sdl_alen &&
  724             bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
  725                 if (rt->rt_expire) {
  726                     if (log_arp_movements)
  727                         log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n",
  728                             inet_ntoa(isaddr),
  729                             ifp->if_addrlen, (u_char *)LLADDR(sdl), ":",
  730                             ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
  731                             ifp->if_xname);
  732                 } else {
  733                         RT_UNLOCK(rt);
  734                         if (log_arp_permanent_modify)
  735                                 log(LOG_ERR, "arp: %*D attempts to modify "
  736                                     "permanent entry for %s on %s\n",
  737                                     ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
  738                                     inet_ntoa(isaddr), ifp->if_xname);
  739                         goto reply;
  740                 }
  741         }
  742         /*
  743          * sanity check for the address length.
  744          * XXX this does not work for protocols with variable address
  745          * length. -is
  746          */
  747         if (sdl->sdl_alen &&
  748             sdl->sdl_alen != ah->ar_hln) {
  749                 log(LOG_WARNING,
  750                     "arp from %*D: new addr len %d, was %d",
  751                     ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
  752                     ah->ar_hln, sdl->sdl_alen);
  753         }
  754         if (ifp->if_addrlen != ah->ar_hln) {
  755                 log(LOG_WARNING,
  756                     "arp from %*D: addr len: new %d, i/f %d (ignored)",
  757                     ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
  758                     ah->ar_hln, ifp->if_addrlen);
  759                 RT_UNLOCK(rt);
  760                 goto reply;
  761         }
  762         (void)memcpy(LLADDR(sdl), ar_sha(ah),
  763             sdl->sdl_alen = ah->ar_hln);
  764         /*
  765          * If we receive an arp from a token-ring station over
  766          * a token-ring nic then try to save the source
  767          * routing info.
  768          */
  769         if (ifp->if_type == IFT_ISO88025) {
  770                 th = (struct iso88025_header *)m->m_pkthdr.header;
  771                 trld = SDL_ISO88025(sdl);
  772                 rif_len = TR_RCF_RIFLEN(th->rcf);
  773                 if ((th->iso88025_shost[0] & TR_RII) &&
  774                     (rif_len > 2)) {
  775                         trld->trld_rcf = th->rcf;
  776                         trld->trld_rcf ^= htons(TR_RCF_DIR);
  777                         memcpy(trld->trld_route, th->rd, rif_len - 2);
  778                         trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK);
  779                         /*
  780                          * Set up source routing information for
  781                          * reply packet (XXX)
  782                          */
  783                         m->m_data -= rif_len;
  784                         m->m_len  += rif_len;
  785                         m->m_pkthdr.len += rif_len;
  786                 } else {
  787                         th->iso88025_shost[0] &= ~TR_RII;
  788                         trld->trld_rcf = 0;
  789                 }
  790                 m->m_data -= 8;
  791                 m->m_len  += 8;
  792                 m->m_pkthdr.len += 8;
  793                 th->rcf = trld->trld_rcf;
  794         }
  795         if (rt->rt_expire)
  796                 rt->rt_expire = time_second + arpt_keep;
  797         la->la_asked = 0;
  798         la->la_preempt = arp_maxtries;
  799         hold = la->la_hold;
  800         la->la_hold = NULL;
  801         RT_UNLOCK(rt);
  802         if (hold != NULL)
  803                 (*ifp->if_output)(ifp, hold, rt_key(rt), rt);
  804 
  805 reply:
  806         if (op != ARPOP_REQUEST)
  807                 goto drop;
  808         if (itaddr.s_addr == myaddr.s_addr) {
  809                 /* I am the target */
  810                 (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
  811                 (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
  812         } else {
  813                 rt = arplookup(itaddr.s_addr, 0, SIN_PROXY);
  814                 if (rt == NULL) {
  815                         struct sockaddr_in sin;
  816 
  817                         if (!arp_proxyall)
  818                                 goto drop;
  819 
  820                         bzero(&sin, sizeof sin);
  821                         sin.sin_family = AF_INET;
  822                         sin.sin_len = sizeof sin;
  823                         sin.sin_addr = itaddr;
  824 
  825                         rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
  826                         if (!rt)
  827                                 goto drop;
  828                         /*
  829                          * Don't send proxies for nodes on the same interface
  830                          * as this one came out of, or we'll get into a fight
  831                          * over who claims what Ether address.
  832                          */
  833                         if (rt->rt_ifp == ifp) {
  834                                 rtfree(rt);
  835                                 goto drop;
  836                         }
  837                         (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
  838                         (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
  839                         rtfree(rt);
  840 
  841                         /*
  842                          * Also check that the node which sent the ARP packet
  843                          * is on the the interface we expect it to be on. This
  844                          * avoids ARP chaos if an interface is connected to the
  845                          * wrong network.
  846                          */
  847                         sin.sin_addr = isaddr;
  848 
  849                         rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
  850                         if (!rt)
  851                                 goto drop;
  852                         if (rt->rt_ifp != ifp) {
  853                                 log(LOG_INFO, "arp_proxy: ignoring request"
  854                                     " from %s via %s, expecting %s\n",
  855                                     inet_ntoa(isaddr), ifp->if_xname,
  856                                     rt->rt_ifp->if_xname);
  857                                 rtfree(rt);
  858                                 goto drop;
  859                         }
  860                         rtfree(rt);
  861 
  862 #ifdef DEBUG_PROXY
  863                         printf("arp: proxying for %s\n",
  864                                inet_ntoa(itaddr));
  865 #endif
  866                 } else {
  867                         /*
  868                          * Return proxied ARP replies only on the interface
  869                          * or bridge cluster where this network resides.
  870                          * Otherwise we may conflict with the host we are
  871                          * proxying for.
  872                          */
  873                         if (rt->rt_ifp != ifp &&
  874                                 (rt->rt_ifp->if_bridge != ifp->if_bridge ||
  875                                 ifp->if_bridge == NULL)) {
  876                                 RT_UNLOCK(rt);
  877                                 goto drop;
  878                         }
  879                         sdl = SDL(rt->rt_gateway);
  880                         (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
  881                         (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln);
  882                         RT_UNLOCK(rt);
  883                 }
  884         }
  885 
  886         if (itaddr.s_addr == myaddr.s_addr &&
  887             IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
  888                 /* RFC 3927 link-local IPv4; always reply by broadcast. */
  889 #ifdef DEBUG_LINKLOCAL
  890                 printf("arp: sending reply for link-local addr %s\n",
  891                     inet_ntoa(itaddr));
  892 #endif
  893                 m->m_flags |= M_BCAST;
  894                 m->m_flags &= ~M_MCAST;
  895         } else {
  896                 /* default behaviour; never reply by broadcast. */
  897                 m->m_flags &= ~(M_BCAST|M_MCAST);
  898         }
  899         (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
  900         (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
  901         ah->ar_op = htons(ARPOP_REPLY);
  902         ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
  903         m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);   
  904         m->m_pkthdr.len = m->m_len;   
  905         sa.sa_family = AF_ARP;
  906         sa.sa_len = 2;
  907         (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
  908         return;
  909 
  910 drop:
  911         m_freem(m);
  912 }
  913 #endif
  914 
  915 /*
  916  * Lookup or enter a new address in arptab.
  917  */
  918 static struct rtentry *
  919 arplookup(addr, create, proxy)
  920         u_long addr;
  921         int create, proxy;
  922 {
  923         struct rtentry *rt;
  924         struct sockaddr_inarp sin;
  925         const char *why = 0;
  926 
  927         bzero(&sin, sizeof(sin));
  928         sin.sin_len = sizeof(sin);
  929         sin.sin_family = AF_INET;
  930         sin.sin_addr.s_addr = addr;
  931         if (proxy)
  932                 sin.sin_other = SIN_PROXY;
  933         rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
  934         if (rt == 0)
  935                 return (0);
  936 
  937         if (rt->rt_flags & RTF_GATEWAY)
  938                 why = "host is not on local network";
  939         else if ((rt->rt_flags & RTF_LLINFO) == 0)
  940                 why = "could not allocate llinfo";
  941         else if (rt->rt_gateway->sa_family != AF_LINK)
  942                 why = "gateway route is not ours";
  943 
  944         if (why) {
  945 #define ISDYNCLONE(_rt) \
  946         (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED)
  947                 if (create)
  948                         log(LOG_DEBUG, "arplookup %s failed: %s\n",
  949                             inet_ntoa(sin.sin_addr), why);
  950                 /*
  951                  * If there are no references to this Layer 2 route,
  952                  * and it is a cloned route, and not static, and
  953                  * arplookup() is creating the route, then purge
  954                  * it from the routing table as it is probably bogus.
  955                  */
  956                 if (rt->rt_refcnt == 1 && ISDYNCLONE(rt))
  957                         rtexpunge(rt);
  958                 RTFREE_LOCKED(rt);
  959                 return (0);
  960 #undef ISDYNCLONE
  961         } else {
  962                 RT_REMREF(rt);
  963                 return (rt);
  964         }
  965 }
  966 
  967 void
  968 arp_ifinit(ifp, ifa)
  969         struct ifnet *ifp;
  970         struct ifaddr *ifa;
  971 {
  972         if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
  973                 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
  974                                 &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
  975         ifa->ifa_rtrequest = arp_rtrequest;
  976         ifa->ifa_flags |= RTF_CLONING;
  977 }
  978 
  979 void
  980 arp_ifinit2(ifp, ifa, enaddr)
  981         struct ifnet *ifp;
  982         struct ifaddr *ifa;
  983         u_char *enaddr;
  984 {
  985         if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
  986                 arprequest(ifp, &IA_SIN(ifa)->sin_addr,
  987                                 &IA_SIN(ifa)->sin_addr, enaddr);
  988         ifa->ifa_rtrequest = arp_rtrequest;
  989         ifa->ifa_flags |= RTF_CLONING;
  990 }
  991 
  992 static void
  993 arp_init(void)
  994 {
  995 
  996         arpintrq.ifq_maxlen = 50;
  997         mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
  998         LIST_INIT(&llinfo_arp);
  999         callout_init(&arp_callout, CALLOUT_MPSAFE);
 1000         netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE);
 1001         callout_reset(&arp_callout, hz, arptimer, NULL);
 1002 }
 1003 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);

Cache object: 8dd2bf6fa9fe60aa6b08cd6ec5e29a49


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