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

Cache object: 22a6f376fe5dacd4b8714e92b3f85f7b


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