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

Cache object: fa08aea08cac43fda98a9d5ca41b2114


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