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

Cache object: 4b5d2cab94f4a3a10c0c68ea3cc71c32


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