1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 30c3aac50fSPeter Wemm * $FreeBSD$ 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 33df8bae1dSRodney W. Grimes /* 34df8bae1dSRodney W. Grimes * Ethernet address resolution protocol. 35df8bae1dSRodney W. Grimes * TODO: 36df8bae1dSRodney W. Grimes * add "inuse/lock" bit (or ref. count) along with valid bit 37df8bae1dSRodney W. Grimes */ 38df8bae1dSRodney W. Grimes 391d5e9e22SEivind Eklund #include "opt_inet.h" 4019527d3eSRobert Watson #include "opt_mac.h" 41a9771948SGleb Smirnoff #include "opt_carp.h" 421d5e9e22SEivind Eklund 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44df8bae1dSRodney W. Grimes #include <sys/kernel.h> 45ce02431fSDoug Rabson #include <sys/queue.h> 46885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h> 47885f1aa4SPoul-Henning Kamp #include <sys/systm.h> 4819527d3eSRobert Watson #include <sys/mac.h> 49885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h> 50885f1aa4SPoul-Henning Kamp #include <sys/malloc.h> 514458ac71SBruce Evans #include <sys/socket.h> 52885f1aa4SPoul-Henning Kamp #include <sys/syslog.h> 53df8bae1dSRodney W. Grimes 54df8bae1dSRodney W. Grimes #include <net/if.h> 55df8bae1dSRodney W. Grimes #include <net/if_dl.h> 56722012ccSJulian Elischer #include <net/if_types.h> 57df8bae1dSRodney W. Grimes #include <net/route.h> 58748e0b0aSGarrett Wollman #include <net/netisr.h> 59b149dd6cSLarry Lile #include <net/if_llc.h> 60c8f8e9c1SJulian Elischer #include <net/ethernet.h> 61df8bae1dSRodney W. Grimes 62df8bae1dSRodney W. Grimes #include <netinet/in.h> 63df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 64df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 65df8bae1dSRodney W. Grimes 66322dcb8dSMax Khon #include <net/if_arc.h> 67fda82fc2SJulian Elischer #include <net/iso88025.h> 68fda82fc2SJulian Elischer 69a9771948SGleb Smirnoff #ifdef DEV_CARP 70a9771948SGleb Smirnoff #include <netinet/ip_carp.h> 71a9771948SGleb Smirnoff #endif 72a9771948SGleb Smirnoff 73df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s) 74df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s) 75df8bae1dSRodney W. Grimes 76ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether); 77602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes /* timer values */ 80602d513cSGarrett Wollman static int arpt_prune = (5*60*1); /* walk list every 5 minutes */ 81602d513cSGarrett Wollman static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 82885f1aa4SPoul-Henning Kamp 83602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, prune_intvl, CTLFLAG_RW, 84602d513cSGarrett Wollman &arpt_prune, 0, ""); 85602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 86602d513cSGarrett Wollman &arpt_keep, 0, ""); 87885f1aa4SPoul-Henning Kamp 88df8bae1dSRodney W. Grimes #define rt_expire rt_rmx.rmx_expire 89df8bae1dSRodney W. Grimes 9066800f57SGarrett Wollman struct llinfo_arp { 91e3975643SJake Burkholder LIST_ENTRY(llinfo_arp) la_le; 9266800f57SGarrett Wollman struct rtentry *la_rt; 9366800f57SGarrett Wollman struct mbuf *la_hold; /* last packet until resolved/timeout */ 94022695f8SOrion Hodson u_short la_preempt; /* countdown for pre-expiry arps */ 95e1ff74c5SGleb Smirnoff u_short la_asked; /* # requests sent */ 9666800f57SGarrett Wollman }; 9766800f57SGarrett Wollman 98e3975643SJake Burkholder static LIST_HEAD(, llinfo_arp) llinfo_arp; 99df8bae1dSRodney W. Grimes 1001cafed39SJonathan Lemon static struct ifqueue arpintrq; 101d1dd20beSSam Leffler static int arp_allocated; 102df8bae1dSRodney W. Grimes 103885f1aa4SPoul-Henning Kamp static int arp_maxtries = 5; 104602d513cSGarrett Wollman static int useloopback = 1; /* use loopback interface for local traffic */ 105885f1aa4SPoul-Henning Kamp static int arp_proxyall = 0; 106d1dd20beSSam Leffler static struct callout arp_callout; 107602d513cSGarrett Wollman 108602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 109602d513cSGarrett Wollman &arp_maxtries, 0, ""); 110602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 111602d513cSGarrett Wollman &useloopback, 0, ""); 112602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 113602d513cSGarrett Wollman &arp_proxyall, 0, ""); 114885f1aa4SPoul-Henning Kamp 1154d77a549SAlfred Perlstein static void arp_init(void); 1164d77a549SAlfred Perlstein static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 1174d77a549SAlfred Perlstein static void arprequest(struct ifnet *, 1184d77a549SAlfred Perlstein struct in_addr *, struct in_addr *, u_char *); 1191cafed39SJonathan Lemon static void arpintr(struct mbuf *); 1204d77a549SAlfred Perlstein static void arptimer(void *); 1211ed7bf1eSGleb Smirnoff static struct rtentry 1224d77a549SAlfred Perlstein *arplookup(u_long, int, int); 1231d5e9e22SEivind Eklund #ifdef INET 1244d77a549SAlfred Perlstein static void in_arpinput(struct mbuf *); 1251d5e9e22SEivind Eklund #endif 12628e82295SGarrett Wollman 127df8bae1dSRodney W. Grimes /* 128df8bae1dSRodney W. Grimes * Timeout routine. Age arp_tab entries periodically. 129df8bae1dSRodney W. Grimes */ 130df8bae1dSRodney W. Grimes /* ARGSUSED */ 131df8bae1dSRodney W. Grimes static void 1321ed7bf1eSGleb Smirnoff arptimer(void * __unused unused) 133df8bae1dSRodney W. Grimes { 134c996428cSJeffrey Hsu struct llinfo_arp *la, *ola; 135df8bae1dSRodney W. Grimes 13693f79889SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rt_tables[AF_INET]); 1371ed7bf1eSGleb Smirnoff LIST_FOREACH_SAFE(la, &llinfo_arp, la_le, ola) { 138c996428cSJeffrey Hsu struct rtentry *rt = la->la_rt; 1391ed7bf1eSGleb Smirnoff 1401ed7bf1eSGleb Smirnoff RT_LOCK(rt); 141fe53256dSAndre Oppermann if (rt->rt_expire && rt->rt_expire <= time_uptime) { 1421ed7bf1eSGleb Smirnoff struct sockaddr_dl *sdl = SDL(rt->rt_gateway); 1431ed7bf1eSGleb Smirnoff 1441ed7bf1eSGleb Smirnoff KASSERT(sdl->sdl_family == AF_LINK, ("sdl_family %d", 1451ed7bf1eSGleb Smirnoff sdl->sdl_family)); 1461ed7bf1eSGleb Smirnoff if (rt->rt_refcnt > 1) { 1471ed7bf1eSGleb Smirnoff sdl->sdl_alen = 0; 1481ed7bf1eSGleb Smirnoff la->la_preempt = la->la_asked = 0; 1491ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 1501ed7bf1eSGleb Smirnoff continue; 1511ed7bf1eSGleb Smirnoff } 1521ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 1531ed7bf1eSGleb Smirnoff /* 1541ed7bf1eSGleb Smirnoff * XXX: LIST_REMOVE() is deep inside rtrequest(). 1551ed7bf1eSGleb Smirnoff */ 1561ed7bf1eSGleb Smirnoff rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, 1571ed7bf1eSGleb Smirnoff NULL); 1581ed7bf1eSGleb Smirnoff continue; 1591ed7bf1eSGleb Smirnoff } 1601ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 161df8bae1dSRodney W. Grimes } 16293f79889SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rt_tables[AF_INET]); 163d1dd20beSSam Leffler 164d1dd20beSSam Leffler callout_reset(&arp_callout, arpt_prune * hz, arptimer, NULL); 165df8bae1dSRodney W. Grimes } 166df8bae1dSRodney W. Grimes 167df8bae1dSRodney W. Grimes /* 168df8bae1dSRodney W. Grimes * Parallel to llc_rtrequest. 169df8bae1dSRodney W. Grimes */ 170b2774d00SGarrett Wollman static void 1718071913dSRuslan Ermilov arp_rtrequest(req, rt, info) 172df8bae1dSRodney W. Grimes int req; 173e952fa39SMatthew N. Dodd struct rtentry *rt; 1748071913dSRuslan Ermilov struct rt_addrinfo *info; 175df8bae1dSRodney W. Grimes { 176e952fa39SMatthew N. Dodd struct sockaddr *gate; 177e952fa39SMatthew N. Dodd struct llinfo_arp *la; 178df8bae1dSRodney W. Grimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 179067a8babSMax Laier struct in_ifaddr *ia; 180067a8babSMax Laier struct ifaddr *ifa; 181df8bae1dSRodney W. Grimes 182d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 183d1dd20beSSam Leffler 184df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 185df8bae1dSRodney W. Grimes return; 186d1dd20beSSam Leffler gate = rt->rt_gateway; 187d1dd20beSSam Leffler la = (struct llinfo_arp *)rt->rt_llinfo; 188df8bae1dSRodney W. Grimes switch (req) { 189df8bae1dSRodney W. Grimes 190df8bae1dSRodney W. Grimes case RTM_ADD: 191df8bae1dSRodney W. Grimes /* 192df8bae1dSRodney W. Grimes * XXX: If this is a manually added route to interface 193df8bae1dSRodney W. Grimes * such as older version of routed or gated might provide, 194df8bae1dSRodney W. Grimes * restore cloning bit. 195df8bae1dSRodney W. Grimes */ 196df8bae1dSRodney W. Grimes if ((rt->rt_flags & RTF_HOST) == 0 && 197d6fa5d28SBruce M Simpson rt_mask(rt) != NULL && 198df8bae1dSRodney W. Grimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 199df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_CLONING; 200df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_CLONING) { 201df8bae1dSRodney W. Grimes /* 202df8bae1dSRodney W. Grimes * Case 1: This route should come from a route to iface. 203df8bae1dSRodney W. Grimes */ 204df8bae1dSRodney W. Grimes rt_setgate(rt, rt_key(rt), 205df8bae1dSRodney W. Grimes (struct sockaddr *)&null_sdl); 206df8bae1dSRodney W. Grimes gate = rt->rt_gateway; 207df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 208df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 209fe53256dSAndre Oppermann rt->rt_expire = time_uptime; 210df8bae1dSRodney W. Grimes break; 211df8bae1dSRodney W. Grimes } 212df8bae1dSRodney W. Grimes /* Announce a new entry if requested. */ 213df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_ANNOUNCE) 214322dcb8dSMax Khon arprequest(rt->rt_ifp, 215ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 216ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 217df8bae1dSRodney W. Grimes (u_char *)LLADDR(SDL(gate))); 218df8bae1dSRodney W. Grimes /*FALLTHROUGH*/ 219df8bae1dSRodney W. Grimes case RTM_RESOLVE: 220df8bae1dSRodney W. Grimes if (gate->sa_family != AF_LINK || 221df8bae1dSRodney W. Grimes gate->sa_len < sizeof(null_sdl)) { 222d1dd20beSSam Leffler log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__, 223beb2ced8SBruce M Simpson inet_ntoa(SIN(rt_key(rt))->sin_addr), 224beb2ced8SBruce M Simpson (gate->sa_family != AF_LINK) ? 225c3b52d64SBruce M Simpson " (!AF_LINK)": ""); 226df8bae1dSRodney W. Grimes break; 227df8bae1dSRodney W. Grimes } 228df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 229df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 230df8bae1dSRodney W. Grimes if (la != 0) 231df8bae1dSRodney W. Grimes break; /* This happens on a route change */ 232df8bae1dSRodney W. Grimes /* 233df8bae1dSRodney W. Grimes * Case 2: This route may come from cloning, or a manual route 234df8bae1dSRodney W. Grimes * add with a LL address. 235df8bae1dSRodney W. Grimes */ 236d1dd20beSSam Leffler R_Zalloc(la, struct llinfo_arp *, sizeof(*la)); 237df8bae1dSRodney W. Grimes rt->rt_llinfo = (caddr_t)la; 238df8bae1dSRodney W. Grimes if (la == 0) { 239d1dd20beSSam Leffler log(LOG_DEBUG, "%s: malloc failed\n", __func__); 240df8bae1dSRodney W. Grimes break; 241df8bae1dSRodney W. Grimes } 242d1dd20beSSam Leffler arp_allocated++; 2431ed7bf1eSGleb Smirnoff /* 2441ed7bf1eSGleb Smirnoff * We are storing a route entry outside of radix tree. So, 2451ed7bf1eSGleb Smirnoff * it can be found and accessed by other means than radix 2461ed7bf1eSGleb Smirnoff * lookup. The routing code assumes that any rtentry detached 2471ed7bf1eSGleb Smirnoff * from radix can be destroyed safely. To prevent this, we 2481ed7bf1eSGleb Smirnoff * add an additional reference. 2491ed7bf1eSGleb Smirnoff */ 2501ed7bf1eSGleb Smirnoff RT_ADDREF(rt); 251df8bae1dSRodney W. Grimes la->la_rt = rt; 252df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_LLINFO; 25393f79889SJeffrey Hsu RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]); 25466800f57SGarrett Wollman LIST_INSERT_HEAD(&llinfo_arp, la, la_le); 255cbb0b46aSGarrett Wollman 2561d5e9e22SEivind Eklund #ifdef INET 257cbb0b46aSGarrett Wollman /* 258cbb0b46aSGarrett Wollman * This keeps the multicast addresses from showing up 259cbb0b46aSGarrett Wollman * in `arp -a' listings as unresolved. It's not actually 260cbb0b46aSGarrett Wollman * functional. Then the same for broadcast. 261cbb0b46aSGarrett Wollman */ 262c448c89cSJonathan Lemon if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) && 263c448c89cSJonathan Lemon rt->rt_ifp->if_type != IFT_ARCNET) { 264cbb0b46aSGarrett Wollman ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 265cbb0b46aSGarrett Wollman LLADDR(SDL(gate))); 266cbb0b46aSGarrett Wollman SDL(gate)->sdl_alen = 6; 26751a109a1SPeter Wemm rt->rt_expire = 0; 268cbb0b46aSGarrett Wollman } 269cbb0b46aSGarrett Wollman if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 270322dcb8dSMax Khon memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 271322dcb8dSMax Khon rt->rt_ifp->if_addrlen); 272322dcb8dSMax Khon SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 27351a109a1SPeter Wemm rt->rt_expire = 0; 274cbb0b46aSGarrett Wollman } 2751d5e9e22SEivind Eklund #endif 276cbb0b46aSGarrett Wollman 277067a8babSMax Laier TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 278067a8babSMax Laier if (ia->ia_ifp == rt->rt_ifp && 279067a8babSMax Laier SIN(rt_key(rt))->sin_addr.s_addr == 280067a8babSMax Laier (IA_SIN(ia))->sin_addr.s_addr) 281067a8babSMax Laier break; 282067a8babSMax Laier } 283067a8babSMax Laier if (ia) { 284df8bae1dSRodney W. Grimes /* 285df8bae1dSRodney W. Grimes * This test used to be 286df8bae1dSRodney W. Grimes * if (loif.if_flags & IFF_UP) 287df8bae1dSRodney W. Grimes * It allowed local traffic to be forced 288df8bae1dSRodney W. Grimes * through the hardware by configuring the loopback down. 289df8bae1dSRodney W. Grimes * However, it causes problems during network configuration 290df8bae1dSRodney W. Grimes * for boards that can't receive packets they send. 291df8bae1dSRodney W. Grimes * It is now necessary to clear "useloopback" and remove 292df8bae1dSRodney W. Grimes * the route to force traffic out to the hardware. 293df8bae1dSRodney W. Grimes */ 294df8bae1dSRodney W. Grimes rt->rt_expire = 0; 295ac912b2dSLuigi Rizzo bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 296322dcb8dSMax Khon SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 297df8bae1dSRodney W. Grimes if (useloopback) 298f5fea3ddSPaul Traina rt->rt_ifp = loif; 299df8bae1dSRodney W. Grimes 300067a8babSMax Laier /* 301067a8babSMax Laier * make sure to set rt->rt_ifa to the interface 302067a8babSMax Laier * address we are using, otherwise we will have trouble 303067a8babSMax Laier * with source address selection. 304067a8babSMax Laier */ 305067a8babSMax Laier ifa = &ia->ia_ifa; 306067a8babSMax Laier if (ifa != rt->rt_ifa) { 307067a8babSMax Laier IFAFREE(rt->rt_ifa); 308067a8babSMax Laier IFAREF(ifa); 309067a8babSMax Laier rt->rt_ifa = ifa; 310067a8babSMax Laier } 311df8bae1dSRodney W. Grimes } 312df8bae1dSRodney W. Grimes break; 313df8bae1dSRodney W. Grimes 314df8bae1dSRodney W. Grimes case RTM_DELETE: 315df8bae1dSRodney W. Grimes if (la == 0) 316df8bae1dSRodney W. Grimes break; 31793f79889SJeffrey Hsu RADIX_NODE_HEAD_LOCK_ASSERT(rt_tables[AF_INET]); 31866800f57SGarrett Wollman LIST_REMOVE(la, la_le); 3191ed7bf1eSGleb Smirnoff RT_REMREF(rt); 320df8bae1dSRodney W. Grimes rt->rt_llinfo = 0; 321df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_LLINFO; 322df8bae1dSRodney W. Grimes if (la->la_hold) 323df8bae1dSRodney W. Grimes m_freem(la->la_hold); 324df8bae1dSRodney W. Grimes Free((caddr_t)la); 325df8bae1dSRodney W. Grimes } 326df8bae1dSRodney W. Grimes } 327df8bae1dSRodney W. Grimes 328df8bae1dSRodney W. Grimes /* 329df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies: 330df8bae1dSRodney W. Grimes * - arp header source ip address 331df8bae1dSRodney W. Grimes * - arp header target ip address 332df8bae1dSRodney W. Grimes * - arp header source ethernet address 333df8bae1dSRodney W. Grimes */ 334df8bae1dSRodney W. Grimes static void 335322dcb8dSMax Khon arprequest(ifp, sip, tip, enaddr) 336e952fa39SMatthew N. Dodd struct ifnet *ifp; 337e952fa39SMatthew N. Dodd struct in_addr *sip, *tip; 338e952fa39SMatthew N. Dodd u_char *enaddr; 339df8bae1dSRodney W. Grimes { 340e952fa39SMatthew N. Dodd struct mbuf *m; 341e952fa39SMatthew N. Dodd struct arphdr *ah; 342df8bae1dSRodney W. Grimes struct sockaddr sa; 343df8bae1dSRodney W. Grimes 344a163d034SWarner Losh if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 345df8bae1dSRodney W. Grimes return; 34664bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 34764bf80ceSMatthew N. Dodd 2*ifp->if_data.ifi_addrlen; 34864bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 34964bf80ceSMatthew N. Dodd MH_ALIGN(m, m->m_len); 35064bf80ceSMatthew N. Dodd ah = mtod(m, struct arphdr *); 35164bf80ceSMatthew N. Dodd bzero((caddr_t)ah, m->m_len); 35219527d3eSRobert Watson #ifdef MAC 35319527d3eSRobert Watson mac_create_mbuf_linklayer(ifp, m); 35419527d3eSRobert Watson #endif 355322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); 356322dcb8dSMax Khon ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 357322dcb8dSMax Khon ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 358322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REQUEST); 35964bf80ceSMatthew N. Dodd bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 36064bf80ceSMatthew N. Dodd bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 36164bf80ceSMatthew N. Dodd bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 36264bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 36364bf80ceSMatthew N. Dodd sa.sa_len = 2; 36464bf80ceSMatthew N. Dodd m->m_flags |= M_BCAST; 365322dcb8dSMax Khon (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 36664bf80ceSMatthew N. Dodd 36764bf80ceSMatthew N. Dodd return; 368df8bae1dSRodney W. Grimes } 369df8bae1dSRodney W. Grimes 370df8bae1dSRodney W. Grimes /* 371cd46a114SLuigi Rizzo * Resolve an IP address into an ethernet address. 372cd46a114SLuigi Rizzo * On input: 373cd46a114SLuigi Rizzo * ifp is the interface we use 374cd46a114SLuigi Rizzo * dst is the next hop, 375cd46a114SLuigi Rizzo * rt0 is the route to the final destination (possibly useless) 376cd46a114SLuigi Rizzo * m is the mbuf 377cd46a114SLuigi Rizzo * desten is where we want the address. 378cd46a114SLuigi Rizzo * 379cd46a114SLuigi Rizzo * On success, desten is filled in and the function returns 0; 380cd46a114SLuigi Rizzo * If the packet must be held pending resolution, we return EWOULDBLOCK 381cd46a114SLuigi Rizzo * On other errors, we return the corresponding error code. 382df8bae1dSRodney W. Grimes */ 383df8bae1dSRodney W. Grimes int 384cd46a114SLuigi Rizzo arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 385f7c5baa1SLuigi Rizzo struct sockaddr *dst, u_char *desten) 386df8bae1dSRodney W. Grimes { 3871ed7bf1eSGleb Smirnoff struct llinfo_arp *la = NULL; 3881ed7bf1eSGleb Smirnoff struct rtentry *rt = NULL; 389df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 390cd46a114SLuigi Rizzo int error; 391df8bae1dSRodney W. Grimes 392df8bae1dSRodney W. Grimes if (m->m_flags & M_BCAST) { /* broadcast */ 393322dcb8dSMax Khon (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); 394cd46a114SLuigi Rizzo return (0); 395df8bae1dSRodney W. Grimes } 396322dcb8dSMax Khon if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */ 397df8bae1dSRodney W. Grimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 398cd46a114SLuigi Rizzo return (0); 399df8bae1dSRodney W. Grimes } 4001ed7bf1eSGleb Smirnoff 4011ed7bf1eSGleb Smirnoff if (rt0 != NULL) { 4021ed7bf1eSGleb Smirnoff error = rt_check(&rt, &rt0, dst); 4031ed7bf1eSGleb Smirnoff if (error) { 4041ed7bf1eSGleb Smirnoff m_freem(m); 4051ed7bf1eSGleb Smirnoff return error; 406df8bae1dSRodney W. Grimes } 4071ed7bf1eSGleb Smirnoff la = (struct llinfo_arp *)rt->rt_llinfo; 4081ed7bf1eSGleb Smirnoff if (la == NULL) 4091ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 4101ed7bf1eSGleb Smirnoff } 4111ed7bf1eSGleb Smirnoff if (la == NULL) { 4121ed7bf1eSGleb Smirnoff /* 4131ed7bf1eSGleb Smirnoff * We enter this block in case if rt0 was NULL, 4141ed7bf1eSGleb Smirnoff * or if rt found by rt_check() didn't have llinfo. 4151ed7bf1eSGleb Smirnoff */ 4161ed7bf1eSGleb Smirnoff rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 4171ed7bf1eSGleb Smirnoff if (rt == NULL) { 4181ed7bf1eSGleb Smirnoff log(LOG_DEBUG, 4191ed7bf1eSGleb Smirnoff "arpresolve: can't allocate route for %s\n", 4201ed7bf1eSGleb Smirnoff inet_ntoa(SIN(dst)->sin_addr)); 421df8bae1dSRodney W. Grimes m_freem(m); 422cd46a114SLuigi Rizzo return (EINVAL); /* XXX */ 423df8bae1dSRodney W. Grimes } 4241ed7bf1eSGleb Smirnoff la = (struct llinfo_arp *)rt->rt_llinfo; 4251ed7bf1eSGleb Smirnoff if (la == NULL) { 4261ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 4271ed7bf1eSGleb Smirnoff log(LOG_DEBUG, 4281ed7bf1eSGleb Smirnoff "arpresolve: can't allocate llinfo for %s\n", 4291ed7bf1eSGleb Smirnoff inet_ntoa(SIN(dst)->sin_addr)); 4301ed7bf1eSGleb Smirnoff m_freem(m); 4311ed7bf1eSGleb Smirnoff return (EINVAL); /* XXX */ 4321ed7bf1eSGleb Smirnoff } 4331ed7bf1eSGleb Smirnoff } 434df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 435df8bae1dSRodney W. Grimes /* 436df8bae1dSRodney W. Grimes * Check the address family and length is valid, the address 437df8bae1dSRodney W. Grimes * is resolved; otherwise, try to resolve. 438df8bae1dSRodney W. Grimes */ 439fe53256dSAndre Oppermann if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) && 440df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 441a20e2538SGleb Smirnoff 442a20e2538SGleb Smirnoff bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 443a20e2538SGleb Smirnoff 444f0f3379eSOrion Hodson /* 445f0f3379eSOrion Hodson * If entry has an expiry time and it is approaching, 446e1ff74c5SGleb Smirnoff * send an ARP request. 447f0f3379eSOrion Hodson */ 448f0f3379eSOrion Hodson if ((rt->rt_expire != 0) && 449fe53256dSAndre Oppermann (time_uptime + la->la_preempt > rt->rt_expire)) { 450a20e2538SGleb Smirnoff struct in_addr sin = 451a20e2538SGleb Smirnoff SIN(rt->rt_ifa->ifa_addr)->sin_addr; 452a20e2538SGleb Smirnoff 453022695f8SOrion Hodson la->la_preempt--; 454a20e2538SGleb Smirnoff RT_UNLOCK(rt); 455a20e2538SGleb Smirnoff arprequest(ifp, &sin, &SIN(dst)->sin_addr, 456a20e2538SGleb Smirnoff IF_LLADDR(ifp)); 457a20e2538SGleb Smirnoff return (0); 458f0f3379eSOrion Hodson } 459f0f3379eSOrion Hodson 4601ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 461cd46a114SLuigi Rizzo return (0); 462df8bae1dSRodney W. Grimes } 463df8bae1dSRodney W. Grimes /* 464deb62e28SRuslan Ermilov * If ARP is disabled or static on this interface, stop. 46508aadfbbSJonathan Lemon * XXX 46608aadfbbSJonathan Lemon * Probably should not allocate empty llinfo struct if we are 46708aadfbbSJonathan Lemon * not going to be sending out an arp request. 46808aadfbbSJonathan Lemon */ 469deb62e28SRuslan Ermilov if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { 4701ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 47147891de1SRuslan Ermilov m_freem(m); 472cd46a114SLuigi Rizzo return (EINVAL); 47347891de1SRuslan Ermilov } 47408aadfbbSJonathan Lemon /* 475df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address 476df8bae1dSRodney W. Grimes * response yet. Replace the held mbuf with this 477df8bae1dSRodney W. Grimes * latest one. 478df8bae1dSRodney W. Grimes */ 479df8bae1dSRodney W. Grimes if (la->la_hold) 480df8bae1dSRodney W. Grimes m_freem(la->la_hold); 481df8bae1dSRodney W. Grimes la->la_hold = m; 482e1ff74c5SGleb Smirnoff 483e1ff74c5SGleb Smirnoff KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry")); 484e1ff74c5SGleb Smirnoff 485e1ff74c5SGleb Smirnoff /* 486e1ff74c5SGleb Smirnoff * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 487e1ff74c5SGleb Smirnoff * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 488e1ff74c5SGleb Smirnoff * if we have already sent arp_maxtries ARP requests. Retransmit the 489e1ff74c5SGleb Smirnoff * ARP request, but not faster than one request per second. 490e1ff74c5SGleb Smirnoff */ 491e1ff74c5SGleb Smirnoff if (la->la_asked < arp_maxtries) 492e1ff74c5SGleb Smirnoff error = EWOULDBLOCK; /* First request. */ 493e1ff74c5SGleb Smirnoff else 494e1ff74c5SGleb Smirnoff error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH; 495e1ff74c5SGleb Smirnoff 496e1ff74c5SGleb Smirnoff if (la->la_asked++ == 0 || rt->rt_expire != time_uptime) { 497a20e2538SGleb Smirnoff struct in_addr sin = 498a20e2538SGleb Smirnoff SIN(rt->rt_ifa->ifa_addr)->sin_addr; 499a20e2538SGleb Smirnoff 500e1ff74c5SGleb Smirnoff rt->rt_expire = time_uptime; 501a20e2538SGleb Smirnoff RT_UNLOCK(rt); 502e1ff74c5SGleb Smirnoff 503a20e2538SGleb Smirnoff arprequest(ifp, &sin, &SIN(dst)->sin_addr, 504322dcb8dSMax Khon IF_LLADDR(ifp)); 505e1ff74c5SGleb Smirnoff } else 5061ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 507e1ff74c5SGleb Smirnoff 508e1ff74c5SGleb Smirnoff return (error); 509df8bae1dSRodney W. Grimes } 510df8bae1dSRodney W. Grimes 511df8bae1dSRodney W. Grimes /* 512df8bae1dSRodney W. Grimes * Common length and type checks are done here, 513df8bae1dSRodney W. Grimes * then the protocol-specific routine is called. 514df8bae1dSRodney W. Grimes */ 515885f1aa4SPoul-Henning Kamp static void 5161cafed39SJonathan Lemon arpintr(struct mbuf *m) 517df8bae1dSRodney W. Grimes { 5181cafed39SJonathan Lemon struct arphdr *ar; 519df8bae1dSRodney W. Grimes 52076ec7b2fSRobert Watson if (m->m_len < sizeof(struct arphdr) && 52184365e2bSMatthew Dillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 522e44d6283SJoerg Wunsch log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 5231cafed39SJonathan Lemon return; 52476ec7b2fSRobert Watson } 52576ec7b2fSRobert Watson ar = mtod(m, struct arphdr *); 52676ec7b2fSRobert Watson 5271cafed39SJonathan Lemon if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 5281cafed39SJonathan Lemon ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 529b8b33234SDoug Rabson ntohs(ar->ar_hrd) != ARPHRD_ARCNET && 530b8b33234SDoug Rabson ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) { 5311cafed39SJonathan Lemon log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 53276ec7b2fSRobert Watson (unsigned char *)&ar->ar_hrd, ""); 53376ec7b2fSRobert Watson m_freem(m); 5341cafed39SJonathan Lemon return; 53576ec7b2fSRobert Watson } 53676ec7b2fSRobert Watson 53731175791SRuslan Ermilov if (m->m_len < arphdr_len(ar)) { 53878e2d2bdSRuslan Ermilov if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 539f72d9d83SJoerg Wunsch log(LOG_ERR, "arp: runt packet\n"); 54076ec7b2fSRobert Watson m_freem(m); 5411cafed39SJonathan Lemon return; 54276ec7b2fSRobert Watson } 54378e2d2bdSRuslan Ermilov ar = mtod(m, struct arphdr *); 54478e2d2bdSRuslan Ermilov } 545df8bae1dSRodney W. Grimes 546df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) { 5471d5e9e22SEivind Eklund #ifdef INET 548df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 549df8bae1dSRodney W. Grimes in_arpinput(m); 5501cafed39SJonathan Lemon return; 5511d5e9e22SEivind Eklund #endif 552df8bae1dSRodney W. Grimes } 553df8bae1dSRodney W. Grimes m_freem(m); 554df8bae1dSRodney W. Grimes } 555df8bae1dSRodney W. Grimes 5561d5e9e22SEivind Eklund #ifdef INET 557df8bae1dSRodney W. Grimes /* 558df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet. 559df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826. 560df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender 561df8bae1dSRodney W. Grimes * protocol address, to catch impersonators. 562df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol: 563df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 564df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us, 565df8bae1dSRodney W. Grimes * and also sent them in response to IP replies. 566df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive 567df8bae1dSRodney W. Grimes * trailer packets. 568df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 569df8bae1dSRodney W. Grimes * but formerly didn't normally send requests. 570df8bae1dSRodney W. Grimes */ 5713269187dSAlfred Perlstein static int log_arp_wrong_iface = 1; 572e3d123d6SAlfred Perlstein static int log_arp_movements = 1; 5733269187dSAlfred Perlstein 5743269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 5753269187dSAlfred Perlstein &log_arp_wrong_iface, 0, 5763269187dSAlfred Perlstein "log arp packets arriving on the wrong interface"); 577e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 578e3d123d6SAlfred Perlstein &log_arp_movements, 0, 57975ce3221SAlfred Perlstein "log arp replies from MACs different than the one in the cache"); 580e3d123d6SAlfred Perlstein 5813269187dSAlfred Perlstein 582df8bae1dSRodney W. Grimes static void 583df8bae1dSRodney W. Grimes in_arpinput(m) 584df8bae1dSRodney W. Grimes struct mbuf *m; 585df8bae1dSRodney W. Grimes { 586e952fa39SMatthew N. Dodd struct arphdr *ah; 587e952fa39SMatthew N. Dodd struct ifnet *ifp = m->m_pkthdr.rcvif; 588fda82fc2SJulian Elischer struct iso88025_header *th = (struct iso88025_header *)0; 58942fdfc12SKelly Yancey struct iso88025_sockaddr_dl_data *trld; 5901ed7bf1eSGleb Smirnoff struct llinfo_arp *la; 591e952fa39SMatthew N. Dodd struct rtentry *rt; 592ca925d9cSJonathan Lemon struct ifaddr *ifa; 593ca925d9cSJonathan Lemon struct in_ifaddr *ia; 594df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 595df8bae1dSRodney W. Grimes struct sockaddr sa; 596df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr; 5971ed7bf1eSGleb Smirnoff struct mbuf *hold; 598a9771948SGleb Smirnoff u_int8_t *enaddr = NULL; 599b149dd6cSLarry Lile int op, rif_len; 600322dcb8dSMax Khon int req_len; 6018f867517SAndrew Thompson int bridged = 0; 602422a115aSGleb Smirnoff #ifdef DEV_CARP 6032ef4a436SGleb Smirnoff int carp_match = 0; 604422a115aSGleb Smirnoff #endif 605df8bae1dSRodney W. Grimes 606b6de9e91SMax Laier if (ifp->if_bridge) 6078f867517SAndrew Thompson bridged = 1; 6088f867517SAndrew Thompson 609322dcb8dSMax Khon req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 610322dcb8dSMax Khon if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 6114cbc8ad1SYaroslav Tykhiy log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 6124cbc8ad1SYaroslav Tykhiy return; 6134cbc8ad1SYaroslav Tykhiy } 6144cbc8ad1SYaroslav Tykhiy 615322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 616322dcb8dSMax Khon op = ntohs(ah->ar_op); 617322dcb8dSMax Khon (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 618322dcb8dSMax Khon (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 61932439868SGleb Smirnoff 620ca925d9cSJonathan Lemon /* 621ca925d9cSJonathan Lemon * For a bridge, we want to check the address irrespective 622ca925d9cSJonathan Lemon * of the receive interface. (This will change slightly 623ca925d9cSJonathan Lemon * when we have clusters of interfaces). 624a9771948SGleb Smirnoff * If the interface does not match, but the recieving interface 625a9771948SGleb Smirnoff * is part of carp, we call carp_iamatch to see if this is a 626a9771948SGleb Smirnoff * request for the virtual host ip. 627a9771948SGleb Smirnoff * XXX: This is really ugly! 628ca925d9cSJonathan Lemon */ 6292ef4a436SGleb Smirnoff LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 6308f867517SAndrew Thompson if ((bridged || (ia->ia_ifp == ifp)) && 6312ef4a436SGleb Smirnoff itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 632ca925d9cSJonathan Lemon goto match; 6332ef4a436SGleb Smirnoff #ifdef DEV_CARP 6342ef4a436SGleb Smirnoff if (ifp->if_carp != NULL && 6352ef4a436SGleb Smirnoff carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) && 6362ef4a436SGleb Smirnoff itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 6372ef4a436SGleb Smirnoff carp_match = 1; 6382ef4a436SGleb Smirnoff goto match; 6392ef4a436SGleb Smirnoff } 6402ef4a436SGleb Smirnoff #endif 6412ef4a436SGleb Smirnoff } 642ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 6438f867517SAndrew Thompson if ((bridged || (ia->ia_ifp == ifp)) && 644ca925d9cSJonathan Lemon isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 645ca925d9cSJonathan Lemon goto match; 646ca925d9cSJonathan Lemon /* 647d8b84d9eSJonathan Lemon * No match, use the first inet address on the receive interface 648ca925d9cSJonathan Lemon * as a dummy address for the rest of the function. 649ca925d9cSJonathan Lemon */ 650d8b84d9eSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 651ec691a10SJonathan Lemon if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 652ec691a10SJonathan Lemon ia = ifatoia(ifa); 653ec691a10SJonathan Lemon goto match; 654ec691a10SJonathan Lemon } 655ec691a10SJonathan Lemon /* 656ec691a10SJonathan Lemon * If bridging, fall back to using any inet address. 657ec691a10SJonathan Lemon */ 6588f867517SAndrew Thompson if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) 659b2a8ac7cSLuigi Rizzo goto drop; 660ca925d9cSJonathan Lemon match: 661a9771948SGleb Smirnoff if (!enaddr) 662a9771948SGleb Smirnoff enaddr = (u_int8_t *)IF_LLADDR(ifp); 663ca925d9cSJonathan Lemon myaddr = ia->ia_addr.sin_addr; 664a9771948SGleb Smirnoff if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 665b2a8ac7cSLuigi Rizzo goto drop; /* it's from me, ignore it. */ 666322dcb8dSMax Khon if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 667df8bae1dSRodney W. Grimes log(LOG_ERR, 668322dcb8dSMax Khon "arp: link address is broadcast for IP address %s!\n", 669ef0cdf33SGarrett Wollman inet_ntoa(isaddr)); 670b2a8ac7cSLuigi Rizzo goto drop; 671df8bae1dSRodney W. Grimes } 67200fcf9d1SRobert Watson /* 67300fcf9d1SRobert Watson * Warn if another host is using the same IP address, but only if the 67400fcf9d1SRobert Watson * IP address isn't 0.0.0.0, which is used for DHCP only, in which 67500fcf9d1SRobert Watson * case we suppress the warning to avoid false positive complaints of 67600fcf9d1SRobert Watson * potential misconfiguration. 67700fcf9d1SRobert Watson */ 678f69453caSAndrew Thompson if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 679df8bae1dSRodney W. Grimes log(LOG_ERR, 680322dcb8dSMax Khon "arp: %*D is using my IP address %s!\n", 681322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 682322dcb8dSMax Khon inet_ntoa(isaddr)); 683df8bae1dSRodney W. Grimes itaddr = myaddr; 684df8bae1dSRodney W. Grimes goto reply; 685df8bae1dSRodney W. Grimes } 686deb62e28SRuslan Ermilov if (ifp->if_flags & IFF_STATICARP) 687deb62e28SRuslan Ermilov goto reply; 6881ed7bf1eSGleb Smirnoff rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 6891ed7bf1eSGleb Smirnoff if (rt != NULL) { 6901ed7bf1eSGleb Smirnoff la = (struct llinfo_arp *)rt->rt_llinfo; 6911ed7bf1eSGleb Smirnoff if (la == NULL) { 6921ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 6931ed7bf1eSGleb Smirnoff goto reply; 6941ed7bf1eSGleb Smirnoff } 6951ed7bf1eSGleb Smirnoff } else 6961ed7bf1eSGleb Smirnoff goto reply; 6971ed7bf1eSGleb Smirnoff 6981ed7bf1eSGleb Smirnoff /* The following is not an error when doing bridging. */ 6998f867517SAndrew Thompson if (!bridged && rt->rt_ifp != ifp 700422a115aSGleb Smirnoff #ifdef DEV_CARP 701422a115aSGleb Smirnoff && (ifp->if_type != IFT_CARP || !carp_match) 702422a115aSGleb Smirnoff #endif 703422a115aSGleb Smirnoff ) { 7043269187dSAlfred Perlstein if (log_arp_wrong_iface) 7059bf40edeSBrooks Davis log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n", 706dd9b6ddeSBill Fenner inet_ntoa(isaddr), 7079bf40edeSBrooks Davis rt->rt_ifp->if_xname, 708322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7099bf40edeSBrooks Davis ifp->if_xname); 7101ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 711dd9b6ddeSBill Fenner goto reply; 712dd9b6ddeSBill Fenner } 7131ed7bf1eSGleb Smirnoff sdl = SDL(rt->rt_gateway); 714df8bae1dSRodney W. Grimes if (sdl->sdl_alen && 715322dcb8dSMax Khon bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 716e3d123d6SAlfred Perlstein if (rt->rt_expire) { 717e3d123d6SAlfred Perlstein if (log_arp_movements) 7189bf40edeSBrooks Davis log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n", 719322dcb8dSMax Khon inet_ntoa(isaddr), 720322dcb8dSMax Khon ifp->if_addrlen, (u_char *)LLADDR(sdl), ":", 721322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7229bf40edeSBrooks Davis ifp->if_xname); 723e3d123d6SAlfred Perlstein } else { 724dd9b6ddeSBill Fenner log(LOG_ERR, 7259bf40edeSBrooks Davis "arp: %*D attempts to modify permanent entry for %s on %s\n", 726322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7279bf40edeSBrooks Davis inet_ntoa(isaddr), ifp->if_xname); 7281ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 729dd9b6ddeSBill Fenner goto reply; 730dd9b6ddeSBill Fenner } 731dfd5dee1SPeter Wemm } 732322dcb8dSMax Khon /* 733322dcb8dSMax Khon * sanity check for the address length. 734322dcb8dSMax Khon * XXX this does not work for protocols with variable address 735322dcb8dSMax Khon * length. -is 736322dcb8dSMax Khon */ 737322dcb8dSMax Khon if (sdl->sdl_alen && 738322dcb8dSMax Khon sdl->sdl_alen != ah->ar_hln) { 739322dcb8dSMax Khon log(LOG_WARNING, 740322dcb8dSMax Khon "arp from %*D: new addr len %d, was %d", 741322dcb8dSMax Khon ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 742322dcb8dSMax Khon ah->ar_hln, sdl->sdl_alen); 743322dcb8dSMax Khon } 744322dcb8dSMax Khon if (ifp->if_addrlen != ah->ar_hln) { 745322dcb8dSMax Khon log(LOG_WARNING, 746322dcb8dSMax Khon "arp from %*D: addr len: new %d, i/f %d (ignored)", 747322dcb8dSMax Khon ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 748322dcb8dSMax Khon ah->ar_hln, ifp->if_addrlen); 7491ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 750322dcb8dSMax Khon goto reply; 751322dcb8dSMax Khon } 752322dcb8dSMax Khon (void)memcpy(LLADDR(sdl), ar_sha(ah), 753322dcb8dSMax Khon sdl->sdl_alen = ah->ar_hln); 754243c4c6dSEivind Eklund /* 755243c4c6dSEivind Eklund * If we receive an arp from a token-ring station over 756243c4c6dSEivind Eklund * a token-ring nic then try to save the source 757243c4c6dSEivind Eklund * routing info. 758243c4c6dSEivind Eklund */ 759322dcb8dSMax Khon if (ifp->if_type == IFT_ISO88025) { 760fda82fc2SJulian Elischer th = (struct iso88025_header *)m->m_pkthdr.header; 76142fdfc12SKelly Yancey trld = SDL_ISO88025(sdl); 762b149dd6cSLarry Lile rif_len = TR_RCF_RIFLEN(th->rcf); 763b149dd6cSLarry Lile if ((th->iso88025_shost[0] & TR_RII) && 764b149dd6cSLarry Lile (rif_len > 2)) { 76542fdfc12SKelly Yancey trld->trld_rcf = th->rcf; 76642fdfc12SKelly Yancey trld->trld_rcf ^= htons(TR_RCF_DIR); 76742fdfc12SKelly Yancey memcpy(trld->trld_route, th->rd, rif_len - 2); 76842fdfc12SKelly Yancey trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); 769f9083fdbSLarry Lile /* 770f9083fdbSLarry Lile * Set up source routing information for 771f9083fdbSLarry Lile * reply packet (XXX) 772f9083fdbSLarry Lile */ 773b149dd6cSLarry Lile m->m_data -= rif_len; 774b149dd6cSLarry Lile m->m_len += rif_len; 775b149dd6cSLarry Lile m->m_pkthdr.len += rif_len; 776fda82fc2SJulian Elischer } else { 777b149dd6cSLarry Lile th->iso88025_shost[0] &= ~TR_RII; 778c3a2190cSKelly Yancey trld->trld_rcf = 0; 779fcf11853SLarry Lile } 780fda82fc2SJulian Elischer m->m_data -= 8; 781fda82fc2SJulian Elischer m->m_len += 8; 782fcf11853SLarry Lile m->m_pkthdr.len += 8; 78342fdfc12SKelly Yancey th->rcf = trld->trld_rcf; 784fda82fc2SJulian Elischer } 785df8bae1dSRodney W. Grimes if (rt->rt_expire) 786fe53256dSAndre Oppermann rt->rt_expire = time_uptime + arpt_keep; 787022695f8SOrion Hodson la->la_asked = 0; 788022695f8SOrion Hodson la->la_preempt = arp_maxtries; 7891ed7bf1eSGleb Smirnoff hold = la->la_hold; 7901ed7bf1eSGleb Smirnoff la->la_hold = NULL; 7911ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 7921ed7bf1eSGleb Smirnoff if (hold != NULL) 7931ed7bf1eSGleb Smirnoff (*ifp->if_output)(ifp, hold, rt_key(rt), rt); 7941ed7bf1eSGleb Smirnoff 795df8bae1dSRodney W. Grimes reply: 796b2a8ac7cSLuigi Rizzo if (op != ARPOP_REQUEST) 797b2a8ac7cSLuigi Rizzo goto drop; 798df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) { 799df8bae1dSRodney W. Grimes /* I am the target */ 800322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 801a9771948SGleb Smirnoff (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 802df8bae1dSRodney W. Grimes } else { 8031ed7bf1eSGleb Smirnoff rt = arplookup(itaddr.s_addr, 0, SIN_PROXY); 8041ed7bf1eSGleb Smirnoff if (rt == NULL) { 80528e82295SGarrett Wollman struct sockaddr_in sin; 80628e82295SGarrett Wollman 807b2a8ac7cSLuigi Rizzo if (!arp_proxyall) 808b2a8ac7cSLuigi Rizzo goto drop; 80928e82295SGarrett Wollman 81028e82295SGarrett Wollman bzero(&sin, sizeof sin); 81128e82295SGarrett Wollman sin.sin_family = AF_INET; 81228e82295SGarrett Wollman sin.sin_len = sizeof sin; 81328e82295SGarrett Wollman sin.sin_addr = itaddr; 81428e82295SGarrett Wollman 81531246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 816b2a8ac7cSLuigi Rizzo if (!rt) 817b2a8ac7cSLuigi Rizzo goto drop; 81828e82295SGarrett Wollman /* 81928e82295SGarrett Wollman * Don't send proxies for nodes on the same interface 82028e82295SGarrett Wollman * as this one came out of, or we'll get into a fight 82128e82295SGarrett Wollman * over who claims what Ether address. 82228e82295SGarrett Wollman */ 823322dcb8dSMax Khon if (rt->rt_ifp == ifp) { 82428e82295SGarrett Wollman rtfree(rt); 825b2a8ac7cSLuigi Rizzo goto drop; 82628e82295SGarrett Wollman } 827322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 828a9771948SGleb Smirnoff (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 82928e82295SGarrett Wollman rtfree(rt); 830cc728227SDavid Malone 831cc728227SDavid Malone /* 832cc728227SDavid Malone * Also check that the node which sent the ARP packet 833cc728227SDavid Malone * is on the the interface we expect it to be on. This 834cc728227SDavid Malone * avoids ARP chaos if an interface is connected to the 835cc728227SDavid Malone * wrong network. 836cc728227SDavid Malone */ 837cc728227SDavid Malone sin.sin_addr = isaddr; 838cc728227SDavid Malone 839cc728227SDavid Malone rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 840b2a8ac7cSLuigi Rizzo if (!rt) 841b2a8ac7cSLuigi Rizzo goto drop; 842322dcb8dSMax Khon if (rt->rt_ifp != ifp) { 843cc728227SDavid Malone log(LOG_INFO, "arp_proxy: ignoring request" 8449bf40edeSBrooks Davis " from %s via %s, expecting %s\n", 8459bf40edeSBrooks Davis inet_ntoa(isaddr), ifp->if_xname, 8469bf40edeSBrooks Davis rt->rt_ifp->if_xname); 847cc728227SDavid Malone rtfree(rt); 848b2a8ac7cSLuigi Rizzo goto drop; 849cc728227SDavid Malone } 850cc728227SDavid Malone rtfree(rt); 851cc728227SDavid Malone 852ac234f93SGarrett Wollman #ifdef DEBUG_PROXY 853ac234f93SGarrett Wollman printf("arp: proxying for %s\n", 854ef0cdf33SGarrett Wollman inet_ntoa(itaddr)); 855ac234f93SGarrett Wollman #endif 85628e82295SGarrett Wollman } else { 857510b360fSGleb Smirnoff /* 858510b360fSGleb Smirnoff * Return proxied ARP replies only on the interface 859510b360fSGleb Smirnoff * where this network resides. Otherwise we may 860510b360fSGleb Smirnoff * conflict with the host we are proxying for. 861510b360fSGleb Smirnoff */ 862510b360fSGleb Smirnoff if (rt->rt_ifp != ifp) { 863510b360fSGleb Smirnoff RT_UNLOCK(rt); 864510b360fSGleb Smirnoff goto drop; 865510b360fSGleb Smirnoff } 866df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 8671ed7bf1eSGleb Smirnoff (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 868322dcb8dSMax Khon (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 8691ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 87028e82295SGarrett Wollman } 871df8bae1dSRodney W. Grimes } 872df8bae1dSRodney W. Grimes 873322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 874322dcb8dSMax Khon (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 875322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REPLY); 876322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 87764bf80ceSMatthew N. Dodd m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */ 87864bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 87964bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 88064bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 88164bf80ceSMatthew N. Dodd sa.sa_len = 2; 882322dcb8dSMax Khon (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 883df8bae1dSRodney W. Grimes return; 884b2a8ac7cSLuigi Rizzo 885b2a8ac7cSLuigi Rizzo drop: 886b2a8ac7cSLuigi Rizzo m_freem(m); 887df8bae1dSRodney W. Grimes } 8881d5e9e22SEivind Eklund #endif 889df8bae1dSRodney W. Grimes 890df8bae1dSRodney W. Grimes /* 891df8bae1dSRodney W. Grimes * Lookup or enter a new address in arptab. 892df8bae1dSRodney W. Grimes */ 8931ed7bf1eSGleb Smirnoff static struct rtentry * 894df8bae1dSRodney W. Grimes arplookup(addr, create, proxy) 895df8bae1dSRodney W. Grimes u_long addr; 896df8bae1dSRodney W. Grimes int create, proxy; 897df8bae1dSRodney W. Grimes { 898e952fa39SMatthew N. Dodd struct rtentry *rt; 899d1dd20beSSam Leffler struct sockaddr_inarp sin; 900ac234f93SGarrett Wollman const char *why = 0; 901df8bae1dSRodney W. Grimes 902d1dd20beSSam Leffler bzero(&sin, sizeof(sin)); 903d1dd20beSSam Leffler sin.sin_len = sizeof(sin); 904d1dd20beSSam Leffler sin.sin_family = AF_INET; 905df8bae1dSRodney W. Grimes sin.sin_addr.s_addr = addr; 906d1dd20beSSam Leffler if (proxy) 907d1dd20beSSam Leffler sin.sin_other = SIN_PROXY; 90831246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 909df8bae1dSRodney W. Grimes if (rt == 0) 910df8bae1dSRodney W. Grimes return (0); 911ac234f93SGarrett Wollman 912ac234f93SGarrett Wollman if (rt->rt_flags & RTF_GATEWAY) 913ac234f93SGarrett Wollman why = "host is not on local network"; 914ac234f93SGarrett Wollman else if ((rt->rt_flags & RTF_LLINFO) == 0) 915ac234f93SGarrett Wollman why = "could not allocate llinfo"; 916ac234f93SGarrett Wollman else if (rt->rt_gateway->sa_family != AF_LINK) 917ac234f93SGarrett Wollman why = "gateway route is not ours"; 918ac234f93SGarrett Wollman 919fedf1d01SBruce M Simpson if (why) { 920d1dd20beSSam Leffler #define ISDYNCLONE(_rt) \ 921d1dd20beSSam Leffler (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED) 922d1dd20beSSam Leffler if (create) 923ac234f93SGarrett Wollman log(LOG_DEBUG, "arplookup %s failed: %s\n", 924ef0cdf33SGarrett Wollman inet_ntoa(sin.sin_addr), why); 925b75bead1SBruce M Simpson /* 926b75bead1SBruce M Simpson * If there are no references to this Layer 2 route, 927b75bead1SBruce M Simpson * and it is a cloned route, and not static, and 928b75bead1SBruce M Simpson * arplookup() is creating the route, then purge 929b75bead1SBruce M Simpson * it from the routing table as it is probably bogus. 930b75bead1SBruce M Simpson */ 9319c63e9dbSSam Leffler if (rt->rt_refcnt == 1 && ISDYNCLONE(rt)) 9329c63e9dbSSam Leffler rtexpunge(rt); 9339c63e9dbSSam Leffler RTFREE_LOCKED(rt); 934fedf1d01SBruce M Simpson return (0); 935d1dd20beSSam Leffler #undef ISDYNCLONE 936d1dd20beSSam Leffler } else { 9377138d65cSSam Leffler RT_REMREF(rt); 9381ed7bf1eSGleb Smirnoff return (rt); 939df8bae1dSRodney W. Grimes } 940d1dd20beSSam Leffler } 941df8bae1dSRodney W. Grimes 942dd2e4102SGarrett Wollman void 943322dcb8dSMax Khon arp_ifinit(ifp, ifa) 944322dcb8dSMax Khon struct ifnet *ifp; 945dd2e4102SGarrett Wollman struct ifaddr *ifa; 946dd2e4102SGarrett Wollman { 947dd570d4dSTor Egge if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 948322dcb8dSMax Khon arprequest(ifp, &IA_SIN(ifa)->sin_addr, 949322dcb8dSMax Khon &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 950dd2e4102SGarrett Wollman ifa->ifa_rtrequest = arp_rtrequest; 951dd2e4102SGarrett Wollman ifa->ifa_flags |= RTF_CLONING; 952dd2e4102SGarrett Wollman } 953df5e1987SJonathan Lemon 954a9771948SGleb Smirnoff void 955a9771948SGleb Smirnoff arp_ifinit2(ifp, ifa, enaddr) 956a9771948SGleb Smirnoff struct ifnet *ifp; 957a9771948SGleb Smirnoff struct ifaddr *ifa; 958a9771948SGleb Smirnoff u_char *enaddr; 959a9771948SGleb Smirnoff { 960a9771948SGleb Smirnoff if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 961a9771948SGleb Smirnoff arprequest(ifp, &IA_SIN(ifa)->sin_addr, 962a9771948SGleb Smirnoff &IA_SIN(ifa)->sin_addr, enaddr); 963a9771948SGleb Smirnoff ifa->ifa_rtrequest = arp_rtrequest; 964a9771948SGleb Smirnoff ifa->ifa_flags |= RTF_CLONING; 965a9771948SGleb Smirnoff } 966a9771948SGleb Smirnoff 967df5e1987SJonathan Lemon static void 968df5e1987SJonathan Lemon arp_init(void) 969df5e1987SJonathan Lemon { 970df5e1987SJonathan Lemon 971df5e1987SJonathan Lemon arpintrq.ifq_maxlen = 50; 9726008862bSJohn Baldwin mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF); 973532cf61bSPeter Wemm LIST_INIT(&llinfo_arp); 974d1dd20beSSam Leffler callout_init(&arp_callout, CALLOUT_MPSAFE); 9757902224cSSam Leffler netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE); 976cfff63f1SLuigi Rizzo callout_reset(&arp_callout, hz, arptimer, NULL); 977df5e1987SJonathan Lemon } 978df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 979