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

Cache object: 5400cc97188abcb043ad204ed4b9e5c1


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