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/sysctl.h>
   49 #include <sys/systm.h>
   50 #include <sys/mbuf.h>
   51 #include <sys/malloc.h>
   52 #include <sys/socket.h>
   53 #include <sys/syslog.h>
   54 
   55 #include <net/if.h>
   56 #include <net/if_dl.h>
   57 #include <net/if_types.h>
   58 #include <net/route.h>
   59 #include <net/netisr.h>
   60 
   61 #include <netinet/in.h>
   62 #include <netinet/in_var.h>
   63 #include <netinet/if_ether.h>
   64 
   65 #include <net/iso88025.h>
   66 
   67 #define SIN(s) ((struct sockaddr_in *)s)
   68 #define SDL(s) ((struct sockaddr_dl *)s)
   69 
   70 SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
   71 
   72 /* timer values */
   73 static int arpt_prune = (5*60*1); /* walk list every 5 minutes */
   74 static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
   75 static int arpt_down = 20;      /* once declared down, don't send for 20 sec */
   76 
   77 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW,
   78            &arpt_prune, 0, "");
   79 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 
   80            &arpt_keep, 0, "");
   81 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, host_down_time, CTLFLAG_RW,
   82            &arpt_down, 0, "");
   83 
   84 #define rt_expire rt_rmx.rmx_expire
   85 
   86 struct llinfo_arp {
   87         LIST_ENTRY(llinfo_arp) la_le;
   88         struct  rtentry *la_rt;
   89         struct  mbuf *la_hold;          /* last packet until resolved/timeout */
   90         long    la_asked;               /* last time we QUERIED for this addr */
   91 #define la_timer la_rt->rt_rmx.rmx_expire /* deletion time in seconds */
   92 };
   93 
   94 static  LIST_HEAD(, llinfo_arp) llinfo_arp;
   95 
   96 struct  ifqueue arpintrq = {0, 0, 0, 50};
   97 static int      arp_inuse, arp_allocated;
   98 
   99 static int      arp_maxtries = 5;
  100 static int      useloopback = 1; /* use loopback interface for local traffic */
  101 static int      arp_proxyall = 0;
  102 
  103 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
  104            &arp_maxtries, 0, "");
  105 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
  106            &useloopback, 0, "");
  107 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
  108            &arp_proxyall, 0, "");
  109 
  110 static void     arp_rtrequest __P((int, struct rtentry *, struct sockaddr *));
  111 static void     arprequest __P((struct arpcom *,
  112                         struct in_addr *, struct in_addr *, u_char *));
  113 static void     arpintr __P((void));
  114 static void     arptfree __P((struct llinfo_arp *));
  115 static void     arptimer __P((void *));
  116 static struct llinfo_arp
  117                 *arplookup __P((u_long, int, int));
  118 #ifdef INET
  119 static void     in_arpinput __P((struct mbuf *));
  120 #endif
  121 
  122 /*
  123  * Timeout routine.  Age arp_tab entries periodically.
  124  */
  125 /* ARGSUSED */
  126 static void
  127 arptimer(ignored_arg)
  128         void *ignored_arg;
  129 {
  130         int s = splnet();
  131         register struct llinfo_arp *la = llinfo_arp.lh_first;
  132         struct llinfo_arp *ola;
  133 
  134         timeout(arptimer, (caddr_t)0, arpt_prune * hz);
  135         while ((ola = la) != 0) {
  136                 register struct rtentry *rt = la->la_rt;
  137                 la = la->la_le.le_next;
  138                 if (rt->rt_expire && rt->rt_expire <= time_second)
  139                         arptfree(ola); /* timer has expired, clear */
  140         }
  141         splx(s);
  142 }
  143 
  144 /*
  145  * Parallel to llc_rtrequest.
  146  */
  147 static void
  148 arp_rtrequest(req, rt, sa)
  149         int req;
  150         register struct rtentry *rt;
  151         struct sockaddr *sa;
  152 {
  153         register struct sockaddr *gate = rt->rt_gateway;
  154         register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
  155         static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
  156         static int arpinit_done;
  157 
  158         if (!arpinit_done) {
  159                 arpinit_done = 1;
  160                 LIST_INIT(&llinfo_arp);
  161                 timeout(arptimer, (caddr_t)0, hz);
  162         }
  163         if (rt->rt_flags & RTF_GATEWAY)
  164                 return;
  165         switch (req) {
  166 
  167         case RTM_ADD:
  168                 /*
  169                  * XXX: If this is a manually added route to interface
  170                  * such as older version of routed or gated might provide,
  171                  * restore cloning bit.
  172                  */
  173                 if ((rt->rt_flags & RTF_HOST) == 0 &&
  174                     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
  175                         rt->rt_flags |= RTF_CLONING;
  176                 if (rt->rt_flags & RTF_CLONING) {
  177                         /*
  178                          * Case 1: This route should come from a route to iface.
  179                          */
  180                         rt_setgate(rt, rt_key(rt),
  181                                         (struct sockaddr *)&null_sdl);
  182                         gate = rt->rt_gateway;
  183                         SDL(gate)->sdl_type = rt->rt_ifp->if_type;
  184                         SDL(gate)->sdl_index = rt->rt_ifp->if_index;
  185                         rt->rt_expire = time_second;
  186                         break;
  187                 }
  188                 /* Announce a new entry if requested. */
  189                 if (rt->rt_flags & RTF_ANNOUNCE)
  190                         arprequest((struct arpcom *)rt->rt_ifp,
  191                             &SIN(rt_key(rt))->sin_addr,
  192                             &SIN(rt_key(rt))->sin_addr,
  193                             (u_char *)LLADDR(SDL(gate)));
  194                 /*FALLTHROUGH*/
  195         case RTM_RESOLVE:
  196                 if (gate->sa_family != AF_LINK ||
  197                     gate->sa_len < sizeof(null_sdl)) {
  198                         log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
  199                         break;
  200                 }
  201                 SDL(gate)->sdl_type = rt->rt_ifp->if_type;
  202                 SDL(gate)->sdl_index = rt->rt_ifp->if_index;
  203                 if (la != 0)
  204                         break; /* This happens on a route change */
  205                 /*
  206                  * Case 2:  This route may come from cloning, or a manual route
  207                  * add with a LL address.
  208                  */
  209                 R_Malloc(la, struct llinfo_arp *, sizeof(*la));
  210                 rt->rt_llinfo = (caddr_t)la;
  211                 if (la == 0) {
  212                         log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
  213                         break;
  214                 }
  215                 arp_inuse++, arp_allocated++;
  216                 Bzero(la, sizeof(*la));
  217                 la->la_rt = rt;
  218                 rt->rt_flags |= RTF_LLINFO;
  219                 LIST_INSERT_HEAD(&llinfo_arp, la, la_le);
  220 
  221 #ifdef INET
  222                 /*
  223                  * This keeps the multicast addresses from showing up
  224                  * in `arp -a' listings as unresolved.  It's not actually
  225                  * functional.  Then the same for broadcast.
  226                  */
  227                 if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr))) {
  228                         ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr,
  229                                                LLADDR(SDL(gate)));
  230                         SDL(gate)->sdl_alen = 6;
  231                         rt->rt_expire = 0;
  232                 }
  233                 if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) {
  234                         memcpy(LLADDR(SDL(gate)), etherbroadcastaddr, 6);
  235                         SDL(gate)->sdl_alen = 6;
  236                         rt->rt_expire = 0;
  237                 }
  238 #endif
  239 
  240                 if (SIN(rt_key(rt))->sin_addr.s_addr ==
  241                     (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
  242                     /*
  243                      * This test used to be
  244                      *  if (loif.if_flags & IFF_UP)
  245                      * It allowed local traffic to be forced
  246                      * through the hardware by configuring the loopback down.
  247                      * However, it causes problems during network configuration
  248                      * for boards that can't receive packets they send.
  249                      * It is now necessary to clear "useloopback" and remove
  250                      * the route to force traffic out to the hardware.
  251                      */
  252                         rt->rt_expire = 0;
  253                         Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
  254                                 LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
  255                         if (useloopback)
  256                                 rt->rt_ifp = loif;
  257 
  258                 }
  259                 break;
  260 
  261         case RTM_DELETE:
  262                 if (la == 0)
  263                         break;
  264                 arp_inuse--;
  265                 LIST_REMOVE(la, la_le);
  266                 rt->rt_llinfo = 0;
  267                 rt->rt_flags &= ~RTF_LLINFO;
  268                 if (la->la_hold)
  269                         m_freem(la->la_hold);
  270                 Free((caddr_t)la);
  271         }
  272 }
  273 
  274 /*
  275  * Broadcast an ARP request. Caller specifies:
  276  *      - arp header source ip address
  277  *      - arp header target ip address
  278  *      - arp header source ethernet address
  279  */
  280 static void
  281 arprequest(ac, sip, tip, enaddr)
  282         register struct arpcom *ac;
  283         register struct in_addr *sip, *tip;
  284         register u_char *enaddr;
  285 {
  286         register struct mbuf *m;
  287         register struct ether_header *eh;
  288         register struct ether_arp *ea;
  289         struct sockaddr sa;
  290 
  291         if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
  292                 return;
  293         m->m_pkthdr.rcvif = (struct ifnet *)0;
  294         switch (ac->ac_if.if_type) {
  295         case IFT_ISO88025:
  296                 m->m_len = sizeof(*ea) + 10;
  297                 m->m_pkthdr.len = sizeof(*ea) + 10;
  298                 MH_ALIGN(m, sizeof(*ea) + 10);
  299                 (void)memcpy(mtod(m, caddr_t),
  300                     "\x82\x40\xaa\xaa\x03\x00\x00\x00\x08\x06", 10);
  301                 (void)memcpy(sa.sa_data, etherbroadcastaddr, 6);
  302                 (void)memcpy(sa.sa_data + 6, enaddr, 6);
  303                 sa.sa_data[6] |= 0x80;
  304                 sa.sa_data[12] = 0x10;
  305                 sa.sa_data[13] = 0x40;
  306                 ea = (struct ether_arp *)(mtod(m, char *) + 10);
  307                 bzero((caddr_t)ea, sizeof (*ea));
  308                 ea->arp_hrd = htons(ARPHRD_IEEE802);
  309                 break;
  310         case IFT_FDDI:
  311         case IFT_ETHER:
  312                 /*
  313                  * This may not be correct for types not explicitly
  314                  * listed, but this is our best guess
  315                  */
  316         default:
  317                 m->m_len = sizeof(*ea);
  318                 m->m_pkthdr.len = sizeof(*ea);
  319                 MH_ALIGN(m, sizeof(*ea));
  320                 ea = mtod(m, struct ether_arp *);
  321                 eh = (struct ether_header *)sa.sa_data;
  322                 bzero((caddr_t)ea, sizeof (*ea));
  323                 /* if_output will not swap */
  324                 eh->ether_type = htons(ETHERTYPE_ARP);
  325                 (void)memcpy(eh->ether_dhost, etherbroadcastaddr,
  326                     sizeof(eh->ether_dhost));
  327                 ea->arp_hrd = htons(ARPHRD_ETHER);
  328                 break;
  329         }
  330         ea->arp_pro = htons(ETHERTYPE_IP);
  331         ea->arp_hln = sizeof(ea->arp_sha);      /* hardware address length */
  332         ea->arp_pln = sizeof(ea->arp_spa);      /* protocol address length */
  333         ea->arp_op = htons(ARPOP_REQUEST);
  334         (void)memcpy(ea->arp_sha, enaddr, sizeof(ea->arp_sha));
  335         (void)memcpy(ea->arp_spa, sip, sizeof(ea->arp_spa));
  336         (void)memcpy(ea->arp_tpa, tip, sizeof(ea->arp_tpa));
  337         sa.sa_family = AF_UNSPEC;
  338         sa.sa_len = sizeof(sa);
  339         (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
  340 }
  341 
  342 /*
  343  * Resolve an IP address into an ethernet address.  If success,
  344  * desten is filled in.  If there is no entry in arptab,
  345  * set one up and broadcast a request for the IP address.
  346  * Hold onto this mbuf and resend it once the address
  347  * is finally resolved.  A return value of 1 indicates
  348  * that desten has been filled in and the packet should be sent
  349  * normally; a 0 return indicates that the packet has been
  350  * taken over here, either now or for later transmission.
  351  */
  352 int
  353 arpresolve(ac, rt, m, dst, desten, rt0)
  354         register struct arpcom *ac;
  355         register struct rtentry *rt;
  356         struct mbuf *m;
  357         register struct sockaddr *dst;
  358         register u_char *desten;
  359         struct rtentry *rt0;
  360 {
  361         register struct llinfo_arp *la = 0;
  362         struct sockaddr_dl *sdl;
  363 
  364         if (m->m_flags & M_BCAST) {     /* broadcast */
  365                 (void)memcpy(desten, etherbroadcastaddr, sizeof(etherbroadcastaddr));
  366                 return (1);
  367         }
  368         if (m->m_flags & M_MCAST) {     /* multicast */
  369                 ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
  370                 return(1);
  371         }
  372         if (rt)
  373                 la = (struct llinfo_arp *)rt->rt_llinfo;
  374         if (la == 0) {
  375                 la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0);
  376                 if (la)
  377                         rt = la->la_rt;
  378         }
  379         if (la == 0 || rt == 0) {
  380                 log(LOG_DEBUG, "arpresolve: can't allocate llinfo for %s%s%s\n",
  381                         inet_ntoa(SIN(dst)->sin_addr), la ? "la" : "",
  382                                 rt ? "rt" : "");
  383                 m_freem(m);
  384                 return (0);
  385         }
  386         sdl = SDL(rt->rt_gateway);
  387         /*
  388          * Check the address family and length is valid, the address
  389          * is resolved; otherwise, try to resolve.
  390          */
  391         if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
  392             sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
  393                 bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
  394                 return 1;
  395         }
  396         /*
  397          * There is an arptab entry, but no ethernet address
  398          * response yet.  Replace the held mbuf with this
  399          * latest one.
  400          */
  401         if (la->la_hold)
  402                 m_freem(la->la_hold);
  403         la->la_hold = m;
  404         if (rt->rt_expire) {
  405                 rt->rt_flags &= ~RTF_REJECT;
  406                 if (la->la_asked == 0 || rt->rt_expire != time_second) {
  407                         rt->rt_expire = time_second;
  408                         if (la->la_asked++ < arp_maxtries)
  409                             arprequest(ac,
  410                                 &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
  411                                 &SIN(dst)->sin_addr, ac->ac_enaddr);
  412                         else {
  413                                 rt->rt_flags |= RTF_REJECT;
  414                                 rt->rt_expire += arpt_down;
  415                                 la->la_asked = 0;
  416                         }
  417 
  418                 }
  419         }
  420         return (0);
  421 }
  422 
  423 /*
  424  * Common length and type checks are done here,
  425  * then the protocol-specific routine is called.
  426  */
  427 static void
  428 arpintr()
  429 {
  430         register struct mbuf *m;
  431         register struct arphdr *ar;
  432         int s;
  433 
  434         while (arpintrq.ifq_head) {
  435                 s = splimp();
  436                 IF_DEQUEUE(&arpintrq, m);
  437                 splx(s);
  438                 if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
  439                         panic("arpintr");
  440                 if (m->m_len >= sizeof(struct arphdr) &&
  441                     (ar = mtod(m, struct arphdr *)) &&
  442                     (ntohs(ar->ar_hrd) == ARPHRD_ETHER ||
  443                      ntohs(ar->ar_hrd) == ARPHRD_IEEE802) &&
  444                     m->m_len >=
  445                       sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
  446 
  447                             switch (ntohs(ar->ar_pro)) {
  448 
  449 #ifdef INET
  450                             case ETHERTYPE_IP:
  451                                     in_arpinput(m);
  452                                     continue;
  453 #endif
  454                             }
  455                 m_freem(m);
  456         }
  457 }
  458 
  459 NETISR_SET(NETISR_ARP, arpintr);
  460 
  461 
  462 #ifdef INET
  463 /*
  464  * ARP for Internet protocols on 10 Mb/s Ethernet.
  465  * Algorithm is that given in RFC 826.
  466  * In addition, a sanity check is performed on the sender
  467  * protocol address, to catch impersonators.
  468  * We no longer handle negotiations for use of trailer protocol:
  469  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
  470  * along with IP replies if we wanted trailers sent to us,
  471  * and also sent them in response to IP replies.
  472  * This allowed either end to announce the desire to receive
  473  * trailer packets.
  474  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
  475  * but formerly didn't normally send requests.
  476  */
  477 static void
  478 in_arpinput(m)
  479         struct mbuf *m;
  480 {
  481         register struct ether_arp *ea;
  482         register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
  483         struct ether_header *eh;
  484         struct iso88025_header *th = (struct iso88025_header *)0;
  485         register struct llinfo_arp *la = 0;
  486         register struct rtentry *rt;
  487         struct in_ifaddr *ia, *maybe_ia = 0;
  488         struct sockaddr_dl *sdl;
  489         struct sockaddr sa;
  490         struct in_addr isaddr, itaddr, myaddr;
  491         int op;
  492 
  493         ea = mtod(m, struct ether_arp *);
  494         op = ntohs(ea->arp_op);
  495         (void)memcpy(&isaddr, ea->arp_spa, sizeof (isaddr));
  496         (void)memcpy(&itaddr, ea->arp_tpa, sizeof (itaddr));
  497         for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next)
  498 #ifdef BRIDGE
  499                 /*
  500                  * For a bridge, we want to check the address irrespective
  501                  * of the receive interface. (This will change slightly
  502                  * when we have clusters of interfaces).
  503                  */
  504                 {
  505 #else
  506                 if (ia->ia_ifp == &ac->ac_if) {
  507 #endif
  508                         maybe_ia = ia;
  509                         if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
  510                              (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
  511                                 break;
  512                 }
  513         if (maybe_ia == 0) {
  514                 m_freem(m);
  515                 return;
  516         }
  517         myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
  518         if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
  519             sizeof (ea->arp_sha))) {
  520                 m_freem(m);     /* it's from me, ignore it. */
  521                 return;
  522         }
  523         if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
  524             sizeof (ea->arp_sha))) {
  525                 log(LOG_ERR,
  526                     "arp: ether address is broadcast for IP address %s!\n",
  527                     inet_ntoa(isaddr));
  528                 m_freem(m);
  529                 return;
  530         }
  531         if (isaddr.s_addr == myaddr.s_addr) {
  532                 log(LOG_ERR,
  533                    "arp: %6D is using my IP address %s!\n",
  534                    ea->arp_sha, ":", inet_ntoa(isaddr));
  535                 itaddr = myaddr;
  536                 goto reply;
  537         }
  538         la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
  539         if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
  540 #ifndef BRIDGE /* the following is not an error when doing bridging */
  541                 if (rt->rt_ifp != &ac->ac_if) {
  542                         log(LOG_ERR, "arp: %s is on %s%d but got reply from %6D on %s%d\n",
  543                             inet_ntoa(isaddr),
  544                             rt->rt_ifp->if_name, rt->rt_ifp->if_unit,
  545                             ea->arp_sha, ":",
  546                             ac->ac_if.if_name, ac->ac_if.if_unit);
  547                         goto reply;
  548                 }
  549 #endif
  550                 if (sdl->sdl_alen &&
  551                     bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
  552                         if (rt->rt_expire)
  553                             log(LOG_INFO, "arp: %s moved from %6D to %6D on %s%d\n",
  554                                 inet_ntoa(isaddr), (u_char *)LLADDR(sdl), ":",
  555                                 ea->arp_sha, ":",
  556                                 ac->ac_if.if_name, ac->ac_if.if_unit);
  557                         else {
  558                             log(LOG_ERR,
  559                                 "arp: %6D attempts to modify permanent entry for %s on %s%d\n",
  560                                 ea->arp_sha, ":", inet_ntoa(isaddr),
  561                                 ac->ac_if.if_name, ac->ac_if.if_unit);
  562                             goto reply;
  563                         }
  564                 (void)memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha));
  565                 sdl->sdl_alen = sizeof(ea->arp_sha);
  566                 sdl->sdl_rcf = NULL;
  567                 /*
  568                  * If we receive an arp from a token-ring station over
  569                  * a token-ring nic then try to save the source
  570                  * routing info.
  571                  */
  572                 if (ac->ac_if.if_type == IFT_ISO88025) {
  573                         th = (struct iso88025_header *)m->m_pkthdr.header;
  574                         if ((th->iso88025_shost[0] & 0x80) &&
  575                             ((th->rcf & 0x001f) > 2)) {
  576                                 sdl->sdl_rcf = (th->rcf & 0x8000) ?
  577                                     (th->rcf & 0x7fff) :
  578                                     (th->rcf | 0x8000);
  579                                 memcpy(sdl->sdl_route, th->rseg,
  580                                     (th->rcf & 0x001f)  - 2);
  581                                 sdl->sdl_rcf = sdl->sdl_rcf & 0xff1f;
  582                                 /*
  583                                  * Set up source routing information for
  584                                  * reply packet (XXX)
  585                                  */
  586                                 m->m_data -= (th->rcf & 0x001f);
  587                                 m->m_len  += (th->rcf & 0x001f);
  588                                 m->m_pkthdr.len += (th->rcf & 0x001f);
  589                         } else {
  590                                 th->iso88025_shost[0] &= 0x7f;
  591                         }
  592                         m->m_data -= 8;
  593                         m->m_len  += 8;
  594                         m->m_pkthdr.len += 8;
  595                         th->rcf = sdl->sdl_rcf;
  596                 } else {
  597                         sdl->sdl_rcf = NULL;
  598                 }
  599                 if (rt->rt_expire)
  600                         rt->rt_expire = time_second + arpt_keep;
  601                 rt->rt_flags &= ~RTF_REJECT;
  602                 la->la_asked = 0;
  603                 if (la->la_hold) {
  604                         (*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
  605                                 rt_key(rt), rt);
  606                         la->la_hold = 0;
  607                 }
  608         }
  609 reply:
  610         if (op != ARPOP_REQUEST) {
  611                 m_freem(m);
  612                 return;
  613         }
  614         if (itaddr.s_addr == myaddr.s_addr) {
  615                 /* I am the target */
  616                 (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
  617                 (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
  618         } else {
  619                 la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
  620                 if (la == NULL) {
  621                         struct sockaddr_in sin;
  622 
  623                         if (!arp_proxyall) {
  624                                 m_freem(m);
  625                                 return;
  626                         }
  627 
  628                         bzero(&sin, sizeof sin);
  629                         sin.sin_family = AF_INET;
  630                         sin.sin_len = sizeof sin;
  631                         sin.sin_addr = itaddr;
  632 
  633                         rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
  634                         if (!rt) {
  635                                 m_freem(m);
  636                                 return;
  637                         }
  638                         /*
  639                          * Don't send proxies for nodes on the same interface
  640                          * as this one came out of, or we'll get into a fight
  641                          * over who claims what Ether address.
  642                          */
  643                         if (rt->rt_ifp == &ac->ac_if) {
  644                                 rtfree(rt);
  645                                 m_freem(m);
  646                                 return;
  647                         }
  648                         (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
  649                         (void)memcpy(ea->arp_sha, ac->ac_enaddr, sizeof(ea->arp_sha));
  650                         rtfree(rt);
  651 #ifdef DEBUG_PROXY
  652                         printf("arp: proxying for %s\n",
  653                                inet_ntoa(itaddr));
  654 #endif
  655                 } else {
  656                         rt = la->la_rt;
  657                         (void)memcpy(ea->arp_tha, ea->arp_sha, sizeof(ea->arp_sha));
  658                         sdl = SDL(rt->rt_gateway);
  659                         (void)memcpy(ea->arp_sha, LLADDR(sdl), sizeof(ea->arp_sha));
  660                 }
  661         }
  662 
  663         (void)memcpy(ea->arp_tpa, ea->arp_spa, sizeof(ea->arp_spa));
  664         (void)memcpy(ea->arp_spa, &itaddr, sizeof(ea->arp_spa));
  665         ea->arp_op = htons(ARPOP_REPLY);
  666         ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
  667         switch (ac->ac_if.if_type) {
  668         case IFT_ISO88025:
  669                 /* Re-arrange the source/dest address */
  670                 memcpy(th->iso88025_dhost, th->iso88025_shost,
  671                     sizeof(th->iso88025_dhost));
  672                 memcpy(th->iso88025_shost, ac->ac_enaddr,
  673                     sizeof(th->iso88025_shost));
  674                 /* Set the source routing bit if neccesary */
  675                 if (th->iso88025_dhost[0] & 0x80) {
  676                         th->iso88025_dhost[0] &= 0x7f;
  677                         if ((th->rcf & 0x001f) - 2)
  678                                 th->iso88025_shost[0] |= 0x80;
  679                 }
  680                 /* Copy the addresses, ac and fc into sa_data */
  681                 memcpy(sa.sa_data, th->iso88025_dhost,
  682                     sizeof(th->iso88025_dhost) * 2);
  683                 sa.sa_data[(sizeof(th->iso88025_dhost) * 2)] = 0x10;
  684                 sa.sa_data[(sizeof(th->iso88025_dhost) * 2) + 1] = 0x40;
  685                 break;
  686         case IFT_ETHER:
  687         case IFT_FDDI:
  688         /*
  689          * May not be correct for types not explictly
  690          * listed, but it is our best guess.
  691          */
  692         default:
  693                 eh = (struct ether_header *)sa.sa_data;
  694                 (void)memcpy(eh->ether_dhost, ea->arp_tha,
  695                     sizeof(eh->ether_dhost));
  696                 eh->ether_type = htons(ETHERTYPE_ARP);
  697                 break;
  698         }
  699         sa.sa_family = AF_UNSPEC;
  700         sa.sa_len = sizeof(sa);
  701         (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
  702         return;
  703 }
  704 #endif
  705 
  706 /*
  707  * Free an arp entry.
  708  */
  709 static void
  710 arptfree(la)
  711         register struct llinfo_arp *la;
  712 {
  713         register struct rtentry *rt = la->la_rt;
  714         register struct sockaddr_dl *sdl;
  715         if (rt == 0)
  716                 panic("arptfree");
  717         if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
  718             sdl->sdl_family == AF_LINK) {
  719                 sdl->sdl_alen = 0;
  720                 la->la_asked = 0;
  721                 rt->rt_flags &= ~RTF_REJECT;
  722                 return;
  723         }
  724         rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
  725                         0, (struct rtentry **)0);
  726 }
  727 /*
  728  * Lookup or enter a new address in arptab.
  729  */
  730 static struct llinfo_arp *
  731 arplookup(addr, create, proxy)
  732         u_long addr;
  733         int create, proxy;
  734 {
  735         register struct rtentry *rt;
  736         static struct sockaddr_inarp sin = {sizeof(sin), AF_INET };
  737         const char *why = 0;
  738 
  739         sin.sin_addr.s_addr = addr;
  740         sin.sin_other = proxy ? SIN_PROXY : 0;
  741         rt = rtalloc1((struct sockaddr *)&sin, create, 0UL);
  742         if (rt == 0)
  743                 return (0);
  744         rt->rt_refcnt--;
  745 
  746         if (rt->rt_flags & RTF_GATEWAY)
  747                 why = "host is not on local network";
  748         else if ((rt->rt_flags & RTF_LLINFO) == 0)
  749                 why = "could not allocate llinfo";
  750         else if (rt->rt_gateway->sa_family != AF_LINK)
  751                 why = "gateway route is not ours";
  752 
  753         if (why && create) {
  754                 log(LOG_DEBUG, "arplookup %s failed: %s\n",
  755                     inet_ntoa(sin.sin_addr), why);
  756                 return 0;
  757         } else if (why) {
  758                 return 0;
  759         }
  760         return ((struct llinfo_arp *)rt->rt_llinfo);
  761 }
  762 
  763 void
  764 arp_ifinit(ac, ifa)
  765         struct arpcom *ac;
  766         struct ifaddr *ifa;
  767 {
  768         if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
  769                 arprequest(ac, &IA_SIN(ifa)->sin_addr,
  770                                &IA_SIN(ifa)->sin_addr, ac->ac_enaddr);
  771         ifa->ifa_rtrequest = arp_rtrequest;
  772         ifa->ifa_flags |= RTF_CLONING;
  773 }

Cache object: e4bb716117b1d79597e81d6b52cd457f


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