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 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 32df8bae1dSRodney W. Grimes /* 33df8bae1dSRodney W. Grimes * Ethernet address resolution protocol. 34df8bae1dSRodney W. Grimes * TODO: 35df8bae1dSRodney W. Grimes * add "inuse/lock" bit (or ref. count) along with valid bit 36df8bae1dSRodney W. Grimes */ 37df8bae1dSRodney W. Grimes 384b421e2dSMike Silbersack #include <sys/cdefs.h> 394b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 404b421e2dSMike Silbersack 411d5e9e22SEivind Eklund #include "opt_inet.h" 4219527d3eSRobert Watson #include "opt_mac.h" 43a9771948SGleb Smirnoff #include "opt_carp.h" 441d5e9e22SEivind Eklund 45df8bae1dSRodney W. Grimes #include <sys/param.h> 46df8bae1dSRodney W. Grimes #include <sys/kernel.h> 47ce02431fSDoug Rabson #include <sys/queue.h> 48885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h> 49885f1aa4SPoul-Henning Kamp #include <sys/systm.h> 50885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h> 51885f1aa4SPoul-Henning Kamp #include <sys/malloc.h> 52de34ad3fSJulian Elischer #include <sys/proc.h> 534458ac71SBruce Evans #include <sys/socket.h> 54885f1aa4SPoul-Henning Kamp #include <sys/syslog.h> 55603724d3SBjoern A. Zeeb #include <sys/vimage.h> 56df8bae1dSRodney W. Grimes 57df8bae1dSRodney W. Grimes #include <net/if.h> 58df8bae1dSRodney W. Grimes #include <net/if_dl.h> 59722012ccSJulian Elischer #include <net/if_types.h> 60df8bae1dSRodney W. Grimes #include <net/route.h> 61748e0b0aSGarrett Wollman #include <net/netisr.h> 62b149dd6cSLarry Lile #include <net/if_llc.h> 63c8f8e9c1SJulian Elischer #include <net/ethernet.h> 64df8bae1dSRodney W. Grimes 65df8bae1dSRodney W. Grimes #include <netinet/in.h> 66df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 67df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 68df8bae1dSRodney W. Grimes 69322dcb8dSMax Khon #include <net/if_arc.h> 70fda82fc2SJulian Elischer #include <net/iso88025.h> 71fda82fc2SJulian Elischer 72a9771948SGleb Smirnoff #ifdef DEV_CARP 73a9771948SGleb Smirnoff #include <netinet/ip_carp.h> 74a9771948SGleb Smirnoff #endif 75a9771948SGleb Smirnoff 76aed55708SRobert Watson #include <security/mac/mac_framework.h> 77aed55708SRobert Watson 78df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s) 79df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s) 80df8bae1dSRodney W. Grimes 81ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether); 82602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 83df8bae1dSRodney W. Grimes 84df8bae1dSRodney W. Grimes /* timer values */ 85602d513cSGarrett Wollman static int arpt_keep = (20*60); /* once resolved, good for 20 more minutes */ 86885f1aa4SPoul-Henning Kamp 87602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 88bd2b686fSEd Maste &arpt_keep, 0, "ARP entry lifetime in seconds"); 89885f1aa4SPoul-Henning Kamp 90df8bae1dSRodney W. Grimes #define rt_expire rt_rmx.rmx_expire 91df8bae1dSRodney W. Grimes 9266800f57SGarrett Wollman struct llinfo_arp { 931daaa65dSGleb Smirnoff struct callout la_timer; 9466800f57SGarrett Wollman struct rtentry *la_rt; 9566800f57SGarrett Wollman struct mbuf *la_hold; /* last packet until resolved/timeout */ 96022695f8SOrion Hodson u_short la_preempt; /* countdown for pre-expiry arps */ 97e1ff74c5SGleb Smirnoff u_short la_asked; /* # requests sent */ 9866800f57SGarrett Wollman }; 9966800f57SGarrett Wollman 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; 106602d513cSGarrett Wollman 1078b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, maxtries, 1088b615593SMarko Zec CTLFLAG_RW, arp_maxtries, 0, 1098b615593SMarko Zec "ARP resolution attempts before returning error"); 1108b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, useloopback, 1118b615593SMarko Zec CTLFLAG_RW, useloopback, 0, 1128b615593SMarko Zec "Use the loopback interface for local traffic"); 1138b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, proxyall, 1148b615593SMarko Zec CTLFLAG_RW, arp_proxyall, 0, 1158b615593SMarko Zec "Enable proxy ARP for all suitable requests"); 116885f1aa4SPoul-Henning Kamp 1174d77a549SAlfred Perlstein static void arp_init(void); 1184d77a549SAlfred Perlstein static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 1194d77a549SAlfred Perlstein static void arprequest(struct ifnet *, 1204d77a549SAlfred Perlstein struct in_addr *, struct in_addr *, u_char *); 1211cafed39SJonathan Lemon static void arpintr(struct mbuf *); 1224d77a549SAlfred Perlstein static void arptimer(void *); 1231ed7bf1eSGleb Smirnoff static struct rtentry 1248b07e49aSJulian Elischer *arplookup(u_long, int, int, int); 1251d5e9e22SEivind Eklund #ifdef INET 1264d77a549SAlfred Perlstein static void in_arpinput(struct mbuf *); 1271d5e9e22SEivind Eklund #endif 12828e82295SGarrett Wollman 129df8bae1dSRodney W. Grimes /* 1301daaa65dSGleb Smirnoff * Timeout routine. 131df8bae1dSRodney W. Grimes */ 132df8bae1dSRodney W. Grimes static void 1331daaa65dSGleb Smirnoff arptimer(void *arg) 134df8bae1dSRodney W. Grimes { 1351daaa65dSGleb Smirnoff struct rtentry *rt = (struct rtentry *)arg; 136df8bae1dSRodney W. Grimes 1371daaa65dSGleb Smirnoff RT_LOCK_ASSERT(rt); 1381ed7bf1eSGleb Smirnoff /* 1391daaa65dSGleb Smirnoff * The lock is needed to close a theoretical race 1401daaa65dSGleb Smirnoff * between spontaneous expiry and intentional removal. 1411daaa65dSGleb Smirnoff * We still got an extra reference on rtentry, so can 1421daaa65dSGleb Smirnoff * safely pass pointers to its contents. 1431ed7bf1eSGleb Smirnoff */ 1441ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 145d1dd20beSSam Leffler 1468b07e49aSJulian Elischer in_rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL, 1478b07e49aSJulian Elischer rt->rt_fibnum); 148df8bae1dSRodney W. Grimes } 149df8bae1dSRodney W. Grimes 150df8bae1dSRodney W. Grimes /* 151df8bae1dSRodney W. Grimes * Parallel to llc_rtrequest. 152df8bae1dSRodney W. Grimes */ 153b2774d00SGarrett Wollman static void 154f2565d68SRobert Watson arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info) 155df8bae1dSRodney W. Grimes { 1568b615593SMarko Zec INIT_VNET_NET(curvnet); 1578b615593SMarko Zec INIT_VNET_INET(curvnet); 158e952fa39SMatthew N. Dodd struct sockaddr *gate; 159e952fa39SMatthew N. Dodd struct llinfo_arp *la; 160df8bae1dSRodney W. Grimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 161067a8babSMax Laier struct in_ifaddr *ia; 162067a8babSMax Laier struct ifaddr *ifa; 163df8bae1dSRodney W. Grimes 164d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 165d1dd20beSSam Leffler 166df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 167df8bae1dSRodney W. Grimes return; 168d1dd20beSSam Leffler gate = rt->rt_gateway; 169d1dd20beSSam Leffler la = (struct llinfo_arp *)rt->rt_llinfo; 170df8bae1dSRodney W. Grimes switch (req) { 171df8bae1dSRodney W. Grimes 172df8bae1dSRodney W. Grimes case RTM_ADD: 173df8bae1dSRodney W. Grimes /* 174df8bae1dSRodney W. Grimes * XXX: If this is a manually added route to interface 175df8bae1dSRodney W. Grimes * such as older version of routed or gated might provide, 176df8bae1dSRodney W. Grimes * restore cloning bit. 177df8bae1dSRodney W. Grimes */ 178df8bae1dSRodney W. Grimes if ((rt->rt_flags & RTF_HOST) == 0 && 179d6fa5d28SBruce M Simpson rt_mask(rt) != NULL && 180df8bae1dSRodney W. Grimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 181df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_CLONING; 182df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_CLONING) { 183df8bae1dSRodney W. Grimes /* 184df8bae1dSRodney W. Grimes * Case 1: This route should come from a route to iface. 185df8bae1dSRodney W. Grimes */ 186df8bae1dSRodney W. Grimes rt_setgate(rt, rt_key(rt), 187df8bae1dSRodney W. Grimes (struct sockaddr *)&null_sdl); 188df8bae1dSRodney W. Grimes gate = rt->rt_gateway; 189df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 190df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 191fe53256dSAndre Oppermann rt->rt_expire = time_uptime; 192df8bae1dSRodney W. Grimes break; 193df8bae1dSRodney W. Grimes } 194df8bae1dSRodney W. Grimes /* Announce a new entry if requested. */ 195df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_ANNOUNCE) 196322dcb8dSMax Khon arprequest(rt->rt_ifp, 197ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 198ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 199df8bae1dSRodney W. Grimes (u_char *)LLADDR(SDL(gate))); 200df8bae1dSRodney W. Grimes /*FALLTHROUGH*/ 201df8bae1dSRodney W. Grimes case RTM_RESOLVE: 202df8bae1dSRodney W. Grimes if (gate->sa_family != AF_LINK || 203df8bae1dSRodney W. Grimes gate->sa_len < sizeof(null_sdl)) { 204d1dd20beSSam Leffler log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__, 205beb2ced8SBruce M Simpson inet_ntoa(SIN(rt_key(rt))->sin_addr), 206beb2ced8SBruce M Simpson (gate->sa_family != AF_LINK) ? 207c3b52d64SBruce M Simpson " (!AF_LINK)": ""); 208df8bae1dSRodney W. Grimes break; 209df8bae1dSRodney W. Grimes } 210df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 211df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 212df8bae1dSRodney W. Grimes if (la != 0) 213df8bae1dSRodney W. Grimes break; /* This happens on a route change */ 214df8bae1dSRodney W. Grimes /* 215df8bae1dSRodney W. Grimes * Case 2: This route may come from cloning, or a manual route 216df8bae1dSRodney W. Grimes * add with a LL address. 217df8bae1dSRodney W. Grimes */ 218d1dd20beSSam Leffler R_Zalloc(la, struct llinfo_arp *, sizeof(*la)); 219df8bae1dSRodney W. Grimes rt->rt_llinfo = (caddr_t)la; 220df8bae1dSRodney W. Grimes if (la == 0) { 221d1dd20beSSam Leffler log(LOG_DEBUG, "%s: malloc failed\n", __func__); 222df8bae1dSRodney W. Grimes break; 223df8bae1dSRodney W. Grimes } 224d1dd20beSSam Leffler arp_allocated++; 2251ed7bf1eSGleb Smirnoff /* 2261ed7bf1eSGleb Smirnoff * We are storing a route entry outside of radix tree. So, 2271ed7bf1eSGleb Smirnoff * it can be found and accessed by other means than radix 2281ed7bf1eSGleb Smirnoff * lookup. The routing code assumes that any rtentry detached 2291ed7bf1eSGleb Smirnoff * from radix can be destroyed safely. To prevent this, we 2301ed7bf1eSGleb Smirnoff * add an additional reference. 2311ed7bf1eSGleb Smirnoff */ 2321ed7bf1eSGleb Smirnoff RT_ADDREF(rt); 233df8bae1dSRodney W. Grimes la->la_rt = rt; 234df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_LLINFO; 2351daaa65dSGleb Smirnoff callout_init_mtx(&la->la_timer, &rt->rt_mtx, 2361daaa65dSGleb Smirnoff CALLOUT_RETURNUNLOCKED); 237cbb0b46aSGarrett Wollman 2381d5e9e22SEivind Eklund #ifdef INET 239cbb0b46aSGarrett Wollman /* 240cbb0b46aSGarrett Wollman * This keeps the multicast addresses from showing up 241cbb0b46aSGarrett Wollman * in `arp -a' listings as unresolved. It's not actually 242cbb0b46aSGarrett Wollman * functional. Then the same for broadcast. 243cbb0b46aSGarrett Wollman */ 244c448c89cSJonathan Lemon if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) && 245c448c89cSJonathan Lemon rt->rt_ifp->if_type != IFT_ARCNET) { 246cbb0b46aSGarrett Wollman ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 247cbb0b46aSGarrett Wollman LLADDR(SDL(gate))); 248cbb0b46aSGarrett Wollman SDL(gate)->sdl_alen = 6; 24951a109a1SPeter Wemm rt->rt_expire = 0; 250cbb0b46aSGarrett Wollman } 251cbb0b46aSGarrett Wollman if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 252322dcb8dSMax Khon memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 253322dcb8dSMax Khon rt->rt_ifp->if_addrlen); 254322dcb8dSMax Khon SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 25551a109a1SPeter Wemm rt->rt_expire = 0; 256cbb0b46aSGarrett Wollman } 2571d5e9e22SEivind Eklund #endif 258cbb0b46aSGarrett Wollman 259603724d3SBjoern A. Zeeb TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 260067a8babSMax Laier if (ia->ia_ifp == rt->rt_ifp && 261067a8babSMax Laier SIN(rt_key(rt))->sin_addr.s_addr == 262067a8babSMax Laier (IA_SIN(ia))->sin_addr.s_addr) 263067a8babSMax Laier break; 264067a8babSMax Laier } 265067a8babSMax Laier if (ia) { 266df8bae1dSRodney W. Grimes /* 267df8bae1dSRodney W. Grimes * This test used to be 268df8bae1dSRodney W. Grimes * if (loif.if_flags & IFF_UP) 269df8bae1dSRodney W. Grimes * It allowed local traffic to be forced 270df8bae1dSRodney W. Grimes * through the hardware by configuring the loopback down. 271df8bae1dSRodney W. Grimes * However, it causes problems during network configuration 272df8bae1dSRodney W. Grimes * for boards that can't receive packets they send. 273df8bae1dSRodney W. Grimes * It is now necessary to clear "useloopback" and remove 274df8bae1dSRodney W. Grimes * the route to force traffic out to the hardware. 275df8bae1dSRodney W. Grimes */ 276df8bae1dSRodney W. Grimes rt->rt_expire = 0; 277ac912b2dSLuigi Rizzo bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 278322dcb8dSMax Khon SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 279603724d3SBjoern A. Zeeb if (V_useloopback) { 280603724d3SBjoern A. Zeeb rt->rt_ifp = V_loif; 281603724d3SBjoern A. Zeeb rt->rt_rmx.rmx_mtu = V_loif->if_mtu; 282402865f6SJohn-Mark Gurney } 283df8bae1dSRodney W. Grimes 284067a8babSMax Laier /* 285067a8babSMax Laier * make sure to set rt->rt_ifa to the interface 286067a8babSMax Laier * address we are using, otherwise we will have trouble 287067a8babSMax Laier * with source address selection. 288067a8babSMax Laier */ 289067a8babSMax Laier ifa = &ia->ia_ifa; 290067a8babSMax Laier if (ifa != rt->rt_ifa) { 291067a8babSMax Laier IFAFREE(rt->rt_ifa); 292067a8babSMax Laier IFAREF(ifa); 293067a8babSMax Laier rt->rt_ifa = ifa; 294067a8babSMax Laier } 295df8bae1dSRodney W. Grimes } 296df8bae1dSRodney W. Grimes break; 297df8bae1dSRodney W. Grimes 298df8bae1dSRodney W. Grimes case RTM_DELETE: 2991daaa65dSGleb Smirnoff if (la == NULL) /* XXX: at least CARP does this. */ 300df8bae1dSRodney W. Grimes break; 3011daaa65dSGleb Smirnoff callout_stop(&la->la_timer); 3021daaa65dSGleb Smirnoff rt->rt_llinfo = NULL; 303df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_LLINFO; 3041daaa65dSGleb Smirnoff RT_REMREF(rt); 305df8bae1dSRodney W. Grimes if (la->la_hold) 306df8bae1dSRodney W. Grimes m_freem(la->la_hold); 307df8bae1dSRodney W. Grimes Free((caddr_t)la); 308df8bae1dSRodney W. Grimes } 309df8bae1dSRodney W. Grimes } 310df8bae1dSRodney W. Grimes 311df8bae1dSRodney W. Grimes /* 312df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies: 313df8bae1dSRodney W. Grimes * - arp header source ip address 314df8bae1dSRodney W. Grimes * - arp header target ip address 315df8bae1dSRodney W. Grimes * - arp header source ethernet address 316df8bae1dSRodney W. Grimes */ 317df8bae1dSRodney W. Grimes static void 318f2565d68SRobert Watson arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, 319f2565d68SRobert Watson u_char *enaddr) 320df8bae1dSRodney W. Grimes { 321e952fa39SMatthew N. Dodd struct mbuf *m; 322e952fa39SMatthew N. Dodd struct arphdr *ah; 323df8bae1dSRodney W. Grimes struct sockaddr sa; 324df8bae1dSRodney W. Grimes 325a163d034SWarner Losh if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 326df8bae1dSRodney W. Grimes return; 32764bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 32864bf80ceSMatthew N. Dodd 2*ifp->if_data.ifi_addrlen; 32964bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 33064bf80ceSMatthew N. Dodd MH_ALIGN(m, m->m_len); 33164bf80ceSMatthew N. Dodd ah = mtod(m, struct arphdr *); 33264bf80ceSMatthew N. Dodd bzero((caddr_t)ah, m->m_len); 33319527d3eSRobert Watson #ifdef MAC 334b9b0dac3SRobert Watson mac_netinet_arp_send(ifp, m); 33519527d3eSRobert Watson #endif 336322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); 337322dcb8dSMax Khon ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 338322dcb8dSMax Khon ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 339322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REQUEST); 34064bf80ceSMatthew N. Dodd bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 34164bf80ceSMatthew N. Dodd bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 34264bf80ceSMatthew N. Dodd bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 34364bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 34464bf80ceSMatthew N. Dodd sa.sa_len = 2; 34564bf80ceSMatthew N. Dodd m->m_flags |= M_BCAST; 346322dcb8dSMax Khon (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 34764bf80ceSMatthew N. Dodd 34864bf80ceSMatthew N. Dodd return; 349df8bae1dSRodney W. Grimes } 350df8bae1dSRodney W. Grimes 351df8bae1dSRodney W. Grimes /* 352cd46a114SLuigi Rizzo * Resolve an IP address into an ethernet address. 353cd46a114SLuigi Rizzo * On input: 354cd46a114SLuigi Rizzo * ifp is the interface we use 355cd46a114SLuigi Rizzo * rt0 is the route to the final destination (possibly useless) 356b6ae6984SJulian Elischer * m is the mbuf. May be NULL if we don't have a packet. 357b6ae6984SJulian Elischer * dst is the next hop, 358cd46a114SLuigi Rizzo * desten is where we want the address. 359cd46a114SLuigi Rizzo * 360cd46a114SLuigi Rizzo * On success, desten is filled in and the function returns 0; 361cd46a114SLuigi Rizzo * If the packet must be held pending resolution, we return EWOULDBLOCK 362cd46a114SLuigi Rizzo * On other errors, we return the corresponding error code. 363b6ae6984SJulian Elischer * Note that m_freem() handles NULL. 364df8bae1dSRodney W. Grimes */ 365df8bae1dSRodney W. Grimes int 366cd46a114SLuigi Rizzo arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 367f7c5baa1SLuigi Rizzo struct sockaddr *dst, u_char *desten) 368df8bae1dSRodney W. Grimes { 3698b615593SMarko Zec INIT_VNET_INET(ifp->if_vnet); 3701ed7bf1eSGleb Smirnoff struct llinfo_arp *la = NULL; 3711ed7bf1eSGleb Smirnoff struct rtentry *rt = NULL; 372df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 373cd46a114SLuigi Rizzo int error; 37493fcb5a2SJulian Elischer int fibnum = -1; 375df8bae1dSRodney W. Grimes 376b6ae6984SJulian Elischer if (m) { 377b6ae6984SJulian Elischer if (m->m_flags & M_BCAST) { 378b6ae6984SJulian Elischer /* broadcast */ 379b6ae6984SJulian Elischer (void)memcpy(desten, 380b6ae6984SJulian Elischer ifp->if_broadcastaddr, ifp->if_addrlen); 381cd46a114SLuigi Rizzo return (0); 382df8bae1dSRodney W. Grimes } 383b6ae6984SJulian Elischer if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) { 384b6ae6984SJulian Elischer /* multicast */ 385df8bae1dSRodney W. Grimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 386cd46a114SLuigi Rizzo return (0); 387df8bae1dSRodney W. Grimes } 3888b07e49aSJulian Elischer fibnum = M_GETFIB(m); 389b6ae6984SJulian Elischer } 3901ed7bf1eSGleb Smirnoff 3911ed7bf1eSGleb Smirnoff if (rt0 != NULL) { 3928b07e49aSJulian Elischer /* Look for a cached arp (ll) entry. */ 3938b07e49aSJulian Elischer if (m == NULL) 3948b07e49aSJulian Elischer fibnum = rt0->rt_fibnum; 39593fcb5a2SJulian Elischer error = rt_check(&rt, &rt0, dst); 3961ed7bf1eSGleb Smirnoff if (error) { 3971ed7bf1eSGleb Smirnoff m_freem(m); 3981ed7bf1eSGleb Smirnoff return error; 399df8bae1dSRodney W. Grimes } 4001ed7bf1eSGleb Smirnoff la = (struct llinfo_arp *)rt->rt_llinfo; 4011ed7bf1eSGleb Smirnoff if (la == NULL) 4021ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 4031ed7bf1eSGleb Smirnoff } 40493fcb5a2SJulian Elischer 40593fcb5a2SJulian Elischer /* 40693fcb5a2SJulian Elischer * If we had no mbuf and no route, then hope the caller 40793fcb5a2SJulian Elischer * has a fib in mind because we are running out of ideas. 40893fcb5a2SJulian Elischer * I think this should not happen in current code. 40993fcb5a2SJulian Elischer * (kmacy would know). 41093fcb5a2SJulian Elischer */ 41193fcb5a2SJulian Elischer if (fibnum == -1) 41293fcb5a2SJulian Elischer fibnum = curthread->td_proc->p_fibnum; /* last gasp */ 41393fcb5a2SJulian Elischer 4141ed7bf1eSGleb Smirnoff if (la == NULL) { 4151ed7bf1eSGleb Smirnoff /* 4168b07e49aSJulian Elischer * We enter this block if rt0 was NULL, 41793fcb5a2SJulian Elischer * or if rt found by rt_check() didn't have llinfo. 41893fcb5a2SJulian Elischer * we should get a cloned route, which since it should 41993fcb5a2SJulian Elischer * come from the local interface should have a ll entry. 42057a5a46eSGiorgos Keramidas * It may be incomplete but that's ok. 4211ed7bf1eSGleb Smirnoff */ 4228b07e49aSJulian Elischer rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0, fibnum); 4231ed7bf1eSGleb Smirnoff if (rt == NULL) { 4241ed7bf1eSGleb Smirnoff log(LOG_DEBUG, 4251ed7bf1eSGleb Smirnoff "arpresolve: can't allocate route for %s\n", 4261ed7bf1eSGleb Smirnoff inet_ntoa(SIN(dst)->sin_addr)); 427df8bae1dSRodney W. Grimes m_freem(m); 428cd46a114SLuigi Rizzo return (EINVAL); /* XXX */ 429df8bae1dSRodney W. Grimes } 4301ed7bf1eSGleb Smirnoff la = (struct llinfo_arp *)rt->rt_llinfo; 4311ed7bf1eSGleb Smirnoff if (la == NULL) { 4321ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 4331ed7bf1eSGleb Smirnoff log(LOG_DEBUG, 4341ed7bf1eSGleb Smirnoff "arpresolve: can't allocate llinfo for %s\n", 4351ed7bf1eSGleb Smirnoff inet_ntoa(SIN(dst)->sin_addr)); 4361ed7bf1eSGleb Smirnoff m_freem(m); 4371ed7bf1eSGleb Smirnoff return (EINVAL); /* XXX */ 4381ed7bf1eSGleb Smirnoff } 4391ed7bf1eSGleb Smirnoff } 440df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 441df8bae1dSRodney W. Grimes /* 442df8bae1dSRodney W. Grimes * Check the address family and length is valid, the address 443df8bae1dSRodney W. Grimes * is resolved; otherwise, try to resolve. 444df8bae1dSRodney W. Grimes */ 445fe53256dSAndre Oppermann if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) && 446df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 447a20e2538SGleb Smirnoff 448a20e2538SGleb Smirnoff bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 449a20e2538SGleb Smirnoff 450f0f3379eSOrion Hodson /* 451f0f3379eSOrion Hodson * If entry has an expiry time and it is approaching, 452e1ff74c5SGleb Smirnoff * send an ARP request. 453f0f3379eSOrion Hodson */ 454f0f3379eSOrion Hodson if ((rt->rt_expire != 0) && 455fe53256dSAndre Oppermann (time_uptime + la->la_preempt > rt->rt_expire)) { 456a20e2538SGleb Smirnoff struct in_addr sin = 457a20e2538SGleb Smirnoff SIN(rt->rt_ifa->ifa_addr)->sin_addr; 458a20e2538SGleb Smirnoff 459022695f8SOrion Hodson la->la_preempt--; 460a20e2538SGleb Smirnoff RT_UNLOCK(rt); 461a20e2538SGleb Smirnoff arprequest(ifp, &sin, &SIN(dst)->sin_addr, 462a20e2538SGleb Smirnoff IF_LLADDR(ifp)); 463a20e2538SGleb Smirnoff return (0); 464f0f3379eSOrion Hodson } 465f0f3379eSOrion Hodson 4661ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 467cd46a114SLuigi Rizzo return (0); 468df8bae1dSRodney W. Grimes } 469df8bae1dSRodney W. Grimes /* 470deb62e28SRuslan Ermilov * If ARP is disabled or static on this interface, stop. 47108aadfbbSJonathan Lemon * XXX 47208aadfbbSJonathan Lemon * Probably should not allocate empty llinfo struct if we are 47308aadfbbSJonathan Lemon * not going to be sending out an arp request. 47408aadfbbSJonathan Lemon */ 475deb62e28SRuslan Ermilov if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { 4761ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 47747891de1SRuslan Ermilov m_freem(m); 478cd46a114SLuigi Rizzo return (EINVAL); 47947891de1SRuslan Ermilov } 48008aadfbbSJonathan Lemon /* 481df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address 482df8bae1dSRodney W. Grimes * response yet. Replace the held mbuf with this 483df8bae1dSRodney W. Grimes * latest one. 484df8bae1dSRodney W. Grimes */ 485b6ae6984SJulian Elischer if (m) { 486df8bae1dSRodney W. Grimes if (la->la_hold) 487df8bae1dSRodney W. Grimes m_freem(la->la_hold); 488df8bae1dSRodney W. Grimes la->la_hold = m; 48958505389SKip Macy } 490e1ff74c5SGleb Smirnoff KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry")); 491e1ff74c5SGleb Smirnoff 492e1ff74c5SGleb Smirnoff /* 493e1ff74c5SGleb Smirnoff * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 494e1ff74c5SGleb Smirnoff * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 495e1ff74c5SGleb Smirnoff * if we have already sent arp_maxtries ARP requests. Retransmit the 496e1ff74c5SGleb Smirnoff * ARP request, but not faster than one request per second. 497e1ff74c5SGleb Smirnoff */ 498603724d3SBjoern A. Zeeb if (la->la_asked < V_arp_maxtries) 499e1ff74c5SGleb Smirnoff error = EWOULDBLOCK; /* First request. */ 500e1ff74c5SGleb Smirnoff else 501e1ff74c5SGleb Smirnoff error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH; 502e1ff74c5SGleb Smirnoff 50395ebcabeSMaxim Konovalov if (la->la_asked == 0 || rt->rt_expire != time_uptime) { 504a20e2538SGleb Smirnoff struct in_addr sin = 505a20e2538SGleb Smirnoff SIN(rt->rt_ifa->ifa_addr)->sin_addr; 506a20e2538SGleb Smirnoff 507e1ff74c5SGleb Smirnoff rt->rt_expire = time_uptime; 5081daaa65dSGleb Smirnoff callout_reset(&la->la_timer, hz, arptimer, rt); 50995ebcabeSMaxim Konovalov la->la_asked++; 510a20e2538SGleb Smirnoff RT_UNLOCK(rt); 511e1ff74c5SGleb Smirnoff 512a20e2538SGleb Smirnoff arprequest(ifp, &sin, &SIN(dst)->sin_addr, 513322dcb8dSMax Khon IF_LLADDR(ifp)); 514e1ff74c5SGleb Smirnoff } else 5151ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 516e1ff74c5SGleb Smirnoff 517e1ff74c5SGleb Smirnoff return (error); 518df8bae1dSRodney W. Grimes } 519df8bae1dSRodney W. Grimes 520df8bae1dSRodney W. Grimes /* 521df8bae1dSRodney W. Grimes * Common length and type checks are done here, 522df8bae1dSRodney W. Grimes * then the protocol-specific routine is called. 523df8bae1dSRodney W. Grimes */ 524885f1aa4SPoul-Henning Kamp static void 5251cafed39SJonathan Lemon arpintr(struct mbuf *m) 526df8bae1dSRodney W. Grimes { 5271cafed39SJonathan Lemon struct arphdr *ar; 528df8bae1dSRodney W. Grimes 52976ec7b2fSRobert Watson if (m->m_len < sizeof(struct arphdr) && 53084365e2bSMatthew Dillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 531e44d6283SJoerg Wunsch log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 5321cafed39SJonathan Lemon return; 53376ec7b2fSRobert Watson } 53476ec7b2fSRobert Watson ar = mtod(m, struct arphdr *); 53576ec7b2fSRobert Watson 5361cafed39SJonathan Lemon if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 5371cafed39SJonathan Lemon ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 538b8b33234SDoug Rabson ntohs(ar->ar_hrd) != ARPHRD_ARCNET && 539b8b33234SDoug Rabson ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) { 5401cafed39SJonathan Lemon log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 54176ec7b2fSRobert Watson (unsigned char *)&ar->ar_hrd, ""); 54276ec7b2fSRobert Watson m_freem(m); 5431cafed39SJonathan Lemon return; 54476ec7b2fSRobert Watson } 54576ec7b2fSRobert Watson 54631175791SRuslan Ermilov if (m->m_len < arphdr_len(ar)) { 54778e2d2bdSRuslan Ermilov if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 548f72d9d83SJoerg Wunsch log(LOG_ERR, "arp: runt packet\n"); 54976ec7b2fSRobert Watson m_freem(m); 5501cafed39SJonathan Lemon return; 55176ec7b2fSRobert Watson } 55278e2d2bdSRuslan Ermilov ar = mtod(m, struct arphdr *); 55378e2d2bdSRuslan Ermilov } 554df8bae1dSRodney W. Grimes 555df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) { 5561d5e9e22SEivind Eklund #ifdef INET 557df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 558df8bae1dSRodney W. Grimes in_arpinput(m); 5591cafed39SJonathan Lemon return; 5601d5e9e22SEivind Eklund #endif 561df8bae1dSRodney W. Grimes } 562df8bae1dSRodney W. Grimes m_freem(m); 563df8bae1dSRodney W. Grimes } 564df8bae1dSRodney W. Grimes 5651d5e9e22SEivind Eklund #ifdef INET 566df8bae1dSRodney W. Grimes /* 567df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet. 568df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826. 569df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender 570df8bae1dSRodney W. Grimes * protocol address, to catch impersonators. 571df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol: 572df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 573df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us, 574df8bae1dSRodney W. Grimes * and also sent them in response to IP replies. 575df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive 576df8bae1dSRodney W. Grimes * trailer packets. 577df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 578df8bae1dSRodney W. Grimes * but formerly didn't normally send requests. 579df8bae1dSRodney W. Grimes */ 5803269187dSAlfred Perlstein static int log_arp_wrong_iface = 1; 581e3d123d6SAlfred Perlstein static int log_arp_movements = 1; 58239393906SGleb Smirnoff static int log_arp_permanent_modify = 1; 5833269187dSAlfred Perlstein 5843269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 5853269187dSAlfred Perlstein &log_arp_wrong_iface, 0, 5863269187dSAlfred Perlstein "log arp packets arriving on the wrong interface"); 587e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 588e3d123d6SAlfred Perlstein &log_arp_movements, 0, 58975ce3221SAlfred Perlstein "log arp replies from MACs different than the one in the cache"); 59039393906SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, 59139393906SGleb Smirnoff &log_arp_permanent_modify, 0, 59239393906SGleb Smirnoff "log arp replies from MACs different than the one in the permanent arp entry"); 593e3d123d6SAlfred Perlstein 5943269187dSAlfred Perlstein 595df8bae1dSRodney W. Grimes static void 596f2565d68SRobert Watson in_arpinput(struct mbuf *m) 597df8bae1dSRodney W. Grimes { 598e952fa39SMatthew N. Dodd struct arphdr *ah; 599e952fa39SMatthew N. Dodd struct ifnet *ifp = m->m_pkthdr.rcvif; 6001ed7bf1eSGleb Smirnoff struct llinfo_arp *la; 601e952fa39SMatthew N. Dodd struct rtentry *rt; 602ca925d9cSJonathan Lemon struct ifaddr *ifa; 603ca925d9cSJonathan Lemon struct in_ifaddr *ia; 604df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 605df8bae1dSRodney W. Grimes struct sockaddr sa; 606df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr; 6071ed7bf1eSGleb Smirnoff struct mbuf *hold; 608a9771948SGleb Smirnoff u_int8_t *enaddr = NULL; 609b149dd6cSLarry Lile int op, rif_len; 610322dcb8dSMax Khon int req_len; 61180b11ee4SPhilip Paeps int bridged = 0, is_bridge = 0; 6128b07e49aSJulian Elischer u_int fibnum; 6138b07e49aSJulian Elischer u_int goodfib = 0; 6148b07e49aSJulian Elischer int firstpass = 1; 615422a115aSGleb Smirnoff #ifdef DEV_CARP 6162ef4a436SGleb Smirnoff int carp_match = 0; 617422a115aSGleb Smirnoff #endif 6188e7e854cSKip Macy struct sockaddr_in sin; 6198e7e854cSKip Macy sin.sin_len = sizeof(struct sockaddr_in); 6208e7e854cSKip Macy sin.sin_family = AF_INET; 62129910a5aSKip Macy sin.sin_addr.s_addr = 0; 6228b615593SMarko Zec INIT_VNET_INET(ifp->if_vnet); 623df8bae1dSRodney W. Grimes 62474948aa6SAndrew Thompson if (ifp->if_bridge) 6258f867517SAndrew Thompson bridged = 1; 62680b11ee4SPhilip Paeps if (ifp->if_type == IFT_BRIDGE) 62780b11ee4SPhilip Paeps is_bridge = 1; 6288f867517SAndrew Thompson 629322dcb8dSMax Khon req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 630322dcb8dSMax Khon if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 6314cbc8ad1SYaroslav Tykhiy log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 6324cbc8ad1SYaroslav Tykhiy return; 6334cbc8ad1SYaroslav Tykhiy } 6344cbc8ad1SYaroslav Tykhiy 635322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 636322dcb8dSMax Khon op = ntohs(ah->ar_op); 637322dcb8dSMax Khon (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 638322dcb8dSMax Khon (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 63932439868SGleb Smirnoff 640ca925d9cSJonathan Lemon /* 641ca925d9cSJonathan Lemon * For a bridge, we want to check the address irrespective 642ca925d9cSJonathan Lemon * of the receive interface. (This will change slightly 643ca925d9cSJonathan Lemon * when we have clusters of interfaces). 644a9771948SGleb Smirnoff * If the interface does not match, but the recieving interface 645a9771948SGleb Smirnoff * is part of carp, we call carp_iamatch to see if this is a 646a9771948SGleb Smirnoff * request for the virtual host ip. 647a9771948SGleb Smirnoff * XXX: This is really ugly! 648ca925d9cSJonathan Lemon */ 6492ef4a436SGleb Smirnoff LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 6507f2d8767SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge != NULL) || 651235073f4SAndrew Thompson (ia->ia_ifp == ifp)) && 6522ef4a436SGleb Smirnoff itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 653ca925d9cSJonathan Lemon goto match; 6542ef4a436SGleb Smirnoff #ifdef DEV_CARP 6552ef4a436SGleb Smirnoff if (ifp->if_carp != NULL && 6562ef4a436SGleb Smirnoff carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) && 6572ef4a436SGleb Smirnoff itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 6582ef4a436SGleb Smirnoff carp_match = 1; 6592ef4a436SGleb Smirnoff goto match; 6602ef4a436SGleb Smirnoff } 6612ef4a436SGleb Smirnoff #endif 6622ef4a436SGleb Smirnoff } 663ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 6647f2d8767SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge != NULL) || 665235073f4SAndrew Thompson (ia->ia_ifp == ifp)) && 666ca925d9cSJonathan Lemon isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 667ca925d9cSJonathan Lemon goto match; 66880b11ee4SPhilip Paeps 66980b11ee4SPhilip Paeps #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \ 67080b11ee4SPhilip Paeps (ia->ia_ifp->if_bridge == ifp->if_softc && \ 67180b11ee4SPhilip Paeps !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \ 67280b11ee4SPhilip Paeps addr == ia->ia_addr.sin_addr.s_addr) 67380b11ee4SPhilip Paeps /* 67480b11ee4SPhilip Paeps * Check the case when bridge shares its MAC address with 67580b11ee4SPhilip Paeps * some of its children, so packets are claimed by bridge 67680b11ee4SPhilip Paeps * itself (bridge_input() does it first), but they are really 67780b11ee4SPhilip Paeps * meant to be destined to the bridge member. 67880b11ee4SPhilip Paeps */ 67980b11ee4SPhilip Paeps if (is_bridge) { 68080b11ee4SPhilip Paeps LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 68180b11ee4SPhilip Paeps if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) { 68280b11ee4SPhilip Paeps ifp = ia->ia_ifp; 68380b11ee4SPhilip Paeps goto match; 68480b11ee4SPhilip Paeps } 68580b11ee4SPhilip Paeps } 68680b11ee4SPhilip Paeps } 68780b11ee4SPhilip Paeps #undef BDG_MEMBER_MATCHES_ARP 68880b11ee4SPhilip Paeps 689ca925d9cSJonathan Lemon /* 690d8b84d9eSJonathan Lemon * No match, use the first inet address on the receive interface 691ca925d9cSJonathan Lemon * as a dummy address for the rest of the function. 692ca925d9cSJonathan Lemon */ 693d8b84d9eSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 6944b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_INET) { 695ec691a10SJonathan Lemon ia = ifatoia(ifa); 696ec691a10SJonathan Lemon goto match; 697ec691a10SJonathan Lemon } 698ec691a10SJonathan Lemon /* 699ec691a10SJonathan Lemon * If bridging, fall back to using any inet address. 700ec691a10SJonathan Lemon */ 701603724d3SBjoern A. Zeeb if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) 702b2a8ac7cSLuigi Rizzo goto drop; 703ca925d9cSJonathan Lemon match: 704a9771948SGleb Smirnoff if (!enaddr) 705a9771948SGleb Smirnoff enaddr = (u_int8_t *)IF_LLADDR(ifp); 706ca925d9cSJonathan Lemon myaddr = ia->ia_addr.sin_addr; 707a9771948SGleb Smirnoff if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 708b2a8ac7cSLuigi Rizzo goto drop; /* it's from me, ignore it. */ 709322dcb8dSMax Khon if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 710df8bae1dSRodney W. Grimes log(LOG_ERR, 711322dcb8dSMax Khon "arp: link address is broadcast for IP address %s!\n", 712ef0cdf33SGarrett Wollman inet_ntoa(isaddr)); 713b2a8ac7cSLuigi Rizzo goto drop; 714df8bae1dSRodney W. Grimes } 71500fcf9d1SRobert Watson /* 71600fcf9d1SRobert Watson * Warn if another host is using the same IP address, but only if the 71700fcf9d1SRobert Watson * IP address isn't 0.0.0.0, which is used for DHCP only, in which 71800fcf9d1SRobert Watson * case we suppress the warning to avoid false positive complaints of 71900fcf9d1SRobert Watson * potential misconfiguration. 72000fcf9d1SRobert Watson */ 721f69453caSAndrew Thompson if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 722df8bae1dSRodney W. Grimes log(LOG_ERR, 7233affb6fbSYaroslav Tykhiy "arp: %*D is using my IP address %s on %s!\n", 724322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7253affb6fbSYaroslav Tykhiy inet_ntoa(isaddr), ifp->if_xname); 726df8bae1dSRodney W. Grimes itaddr = myaddr; 727df8bae1dSRodney W. Grimes goto reply; 728df8bae1dSRodney W. Grimes } 729deb62e28SRuslan Ermilov if (ifp->if_flags & IFF_STATICARP) 730deb62e28SRuslan Ermilov goto reply; 7318b07e49aSJulian Elischer /* 73222b55ba9SJulian Elischer * We look for any FIB that has this address to find 7338b07e49aSJulian Elischer * the interface etc. 7348b07e49aSJulian Elischer * For sanity checks that are FIB independent we abort the loop. 7358b07e49aSJulian Elischer */ 7368b07e49aSJulian Elischer for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 7378b07e49aSJulian Elischer rt = arplookup(isaddr.s_addr, 7388b07e49aSJulian Elischer itaddr.s_addr == myaddr.s_addr, 0, fibnum); 7398b07e49aSJulian Elischer if (rt == NULL) 7408b07e49aSJulian Elischer continue; 7418b07e49aSJulian Elischer 7428b07e49aSJulian Elischer sdl = SDL(rt->rt_gateway); 7438b07e49aSJulian Elischer /* Only call this once */ 7448b07e49aSJulian Elischer if (firstpass) { 745b3e761e5SKip Macy sin.sin_addr.s_addr = isaddr.s_addr; 74629910a5aSKip Macy EVENTHANDLER_INVOKE(route_arp_update_event, rt, 74729910a5aSKip Macy ar_sha(ah), (struct sockaddr *)&sin); 7488b07e49aSJulian Elischer } 749b3e761e5SKip Macy 7501ed7bf1eSGleb Smirnoff la = (struct llinfo_arp *)rt->rt_llinfo; 7511ed7bf1eSGleb Smirnoff if (la == NULL) { 7521ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 7538b07e49aSJulian Elischer continue; 7541ed7bf1eSGleb Smirnoff } 7551ed7bf1eSGleb Smirnoff 7568b07e49aSJulian Elischer if (firstpass) { 7571ed7bf1eSGleb Smirnoff /* The following is not an error when doing bridging. */ 7588f867517SAndrew Thompson if (!bridged && rt->rt_ifp != ifp 759422a115aSGleb Smirnoff #ifdef DEV_CARP 760422a115aSGleb Smirnoff && (ifp->if_type != IFT_CARP || !carp_match) 761422a115aSGleb Smirnoff #endif 762422a115aSGleb Smirnoff ) { 7633269187dSAlfred Perlstein if (log_arp_wrong_iface) 7648b07e49aSJulian Elischer log(LOG_ERR, "arp: %s is on %s " 7658b07e49aSJulian Elischer "but got reply from %*D " 7668b07e49aSJulian Elischer "on %s\n", 767dd9b6ddeSBill Fenner inet_ntoa(isaddr), 7689bf40edeSBrooks Davis rt->rt_ifp->if_xname, 7698b07e49aSJulian Elischer ifp->if_addrlen, 7708b07e49aSJulian Elischer (u_char *)ar_sha(ah), ":", 7719bf40edeSBrooks Davis ifp->if_xname); 7721ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 7738b07e49aSJulian Elischer break; 774dd9b6ddeSBill Fenner } 775df8bae1dSRodney W. Grimes if (sdl->sdl_alen && 776322dcb8dSMax Khon bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 777e3d123d6SAlfred Perlstein if (rt->rt_expire) { 778e3d123d6SAlfred Perlstein if (log_arp_movements) 7798b07e49aSJulian Elischer log(LOG_INFO, 7808b07e49aSJulian Elischer "arp: %s moved from %*D to %*D " 7818b07e49aSJulian Elischer "on %s\n", 782322dcb8dSMax Khon inet_ntoa(isaddr), 7838b07e49aSJulian Elischer ifp->if_addrlen, 7848b07e49aSJulian Elischer (u_char *)LLADDR(sdl), ":", 7858b07e49aSJulian Elischer ifp->if_addrlen, 7868b07e49aSJulian Elischer (u_char *)ar_sha(ah), ":", 7879bf40edeSBrooks Davis ifp->if_xname); 788e3d123d6SAlfred Perlstein } else { 78939393906SGleb Smirnoff RT_UNLOCK(rt); 79039393906SGleb Smirnoff if (log_arp_permanent_modify) 7918b07e49aSJulian Elischer log(LOG_ERR, 7928b07e49aSJulian Elischer "arp: %*D attempts to " 7938b07e49aSJulian Elischer "modify permanent entry " 7948b07e49aSJulian Elischer "for %s on %s\n", 7958b07e49aSJulian Elischer ifp->if_addrlen, 7968b07e49aSJulian Elischer (u_char *)ar_sha(ah), ":", 7978b07e49aSJulian Elischer inet_ntoa(isaddr), 7988b07e49aSJulian Elischer ifp->if_xname); 7998b07e49aSJulian Elischer break; 800dd9b6ddeSBill Fenner } 801dfd5dee1SPeter Wemm } 802322dcb8dSMax Khon /* 803322dcb8dSMax Khon * sanity check for the address length. 8048b07e49aSJulian Elischer * XXX this does not work for protocols 8058b07e49aSJulian Elischer * with variable address length. -is 806322dcb8dSMax Khon */ 807322dcb8dSMax Khon if (sdl->sdl_alen && 808322dcb8dSMax Khon sdl->sdl_alen != ah->ar_hln) { 809322dcb8dSMax Khon log(LOG_WARNING, 810322dcb8dSMax Khon "arp from %*D: new addr len %d, was %d", 8118b07e49aSJulian Elischer ifp->if_addrlen, (u_char *) ar_sha(ah), 8128b07e49aSJulian Elischer ":", ah->ar_hln, sdl->sdl_alen); 813322dcb8dSMax Khon } 814322dcb8dSMax Khon if (ifp->if_addrlen != ah->ar_hln) { 815322dcb8dSMax Khon log(LOG_WARNING, 8168b07e49aSJulian Elischer "arp from %*D: addr len: " 8178b07e49aSJulian Elischer "new %d, i/f %d (ignored)", 8188b07e49aSJulian Elischer ifp->if_addrlen, (u_char *) ar_sha(ah), 8198b07e49aSJulian Elischer ":", ah->ar_hln, ifp->if_addrlen); 8201ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 8218b07e49aSJulian Elischer break; 822322dcb8dSMax Khon } 8238b07e49aSJulian Elischer firstpass = 0; 8248b07e49aSJulian Elischer goodfib = fibnum; 8258b07e49aSJulian Elischer } 8268b07e49aSJulian Elischer 8278b07e49aSJulian Elischer /* Copy in the information received. */ 828322dcb8dSMax Khon (void)memcpy(LLADDR(sdl), ar_sha(ah), 829322dcb8dSMax Khon sdl->sdl_alen = ah->ar_hln); 830243c4c6dSEivind Eklund /* 831243c4c6dSEivind Eklund * If we receive an arp from a token-ring station over 8328b07e49aSJulian Elischer * a token-ring nic then try to save the source routing info. 8338b07e49aSJulian Elischer * XXXMRT Only minimal Token Ring support for MRT. 8348b07e49aSJulian Elischer * Only do this on the first pass as if modifies the mbuf. 835243c4c6dSEivind Eklund */ 836322dcb8dSMax Khon if (ifp->if_type == IFT_ISO88025) { 837f7a679b2SGleb Smirnoff struct iso88025_header *th = NULL; 838f7a679b2SGleb Smirnoff struct iso88025_sockaddr_dl_data *trld; 839f7a679b2SGleb Smirnoff 8408b07e49aSJulian Elischer /* force the fib loop to end after this pass */ 8418b07e49aSJulian Elischer fibnum = rt_numfibs - 1; 8428b07e49aSJulian Elischer 843fda82fc2SJulian Elischer th = (struct iso88025_header *)m->m_pkthdr.header; 84442fdfc12SKelly Yancey trld = SDL_ISO88025(sdl); 845b149dd6cSLarry Lile rif_len = TR_RCF_RIFLEN(th->rcf); 846b149dd6cSLarry Lile if ((th->iso88025_shost[0] & TR_RII) && 847b149dd6cSLarry Lile (rif_len > 2)) { 84842fdfc12SKelly Yancey trld->trld_rcf = th->rcf; 84942fdfc12SKelly Yancey trld->trld_rcf ^= htons(TR_RCF_DIR); 85042fdfc12SKelly Yancey memcpy(trld->trld_route, th->rd, rif_len - 2); 85142fdfc12SKelly Yancey trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); 852f9083fdbSLarry Lile /* 853f9083fdbSLarry Lile * Set up source routing information for 854f9083fdbSLarry Lile * reply packet (XXX) 855f9083fdbSLarry Lile */ 856b149dd6cSLarry Lile m->m_data -= rif_len; 857b149dd6cSLarry Lile m->m_len += rif_len; 858b149dd6cSLarry Lile m->m_pkthdr.len += rif_len; 859fda82fc2SJulian Elischer } else { 860b149dd6cSLarry Lile th->iso88025_shost[0] &= ~TR_RII; 861c3a2190cSKelly Yancey trld->trld_rcf = 0; 862fcf11853SLarry Lile } 863fda82fc2SJulian Elischer m->m_data -= 8; 864fda82fc2SJulian Elischer m->m_len += 8; 865fcf11853SLarry Lile m->m_pkthdr.len += 8; 86642fdfc12SKelly Yancey th->rcf = trld->trld_rcf; 867fda82fc2SJulian Elischer } 8688b07e49aSJulian Elischer 8691daaa65dSGleb Smirnoff if (rt->rt_expire) { 870603724d3SBjoern A. Zeeb rt->rt_expire = time_uptime + V_arpt_keep; 871603724d3SBjoern A. Zeeb callout_reset(&la->la_timer, hz * V_arpt_keep, 8728b07e49aSJulian Elischer arptimer, rt); 8731daaa65dSGleb Smirnoff } 874022695f8SOrion Hodson la->la_asked = 0; 875603724d3SBjoern A. Zeeb la->la_preempt = V_arp_maxtries; 8761ed7bf1eSGleb Smirnoff hold = la->la_hold; 8771ed7bf1eSGleb Smirnoff la->la_hold = NULL; 8781ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 8791ed7bf1eSGleb Smirnoff if (hold != NULL) 8801ed7bf1eSGleb Smirnoff (*ifp->if_output)(ifp, hold, rt_key(rt), rt); 8818b07e49aSJulian Elischer } /* end of FIB loop */ 882df8bae1dSRodney W. Grimes reply: 8838b07e49aSJulian Elischer 8848b07e49aSJulian Elischer /* 8858b07e49aSJulian Elischer * Decide if we have to respond to something. 8868b07e49aSJulian Elischer */ 887b2a8ac7cSLuigi Rizzo if (op != ARPOP_REQUEST) 888b2a8ac7cSLuigi Rizzo goto drop; 889df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) { 8908b07e49aSJulian Elischer /* Shortcut.. the receiving interface is the target. */ 891322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 892a9771948SGleb Smirnoff (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 893df8bae1dSRodney W. Grimes } else { 8948b07e49aSJulian Elischer /* It's not asking for our address. But it still may 8958b07e49aSJulian Elischer * be something we should answer. 8968b07e49aSJulian Elischer * 8978b07e49aSJulian Elischer * XXX MRT 8988b07e49aSJulian Elischer * We assume that link level info is independent of 8998b07e49aSJulian Elischer * the table used and so we use whichever we can and don't 9008b07e49aSJulian Elischer * have a better option. 9018b07e49aSJulian Elischer */ 9028b07e49aSJulian Elischer /* Have we been asked to proxy for the target. */ 9038b07e49aSJulian Elischer rt = arplookup(itaddr.s_addr, 0, SIN_PROXY, goodfib); 9041ed7bf1eSGleb Smirnoff if (rt == NULL) { 9058b07e49aSJulian Elischer /* Nope, only intersted now if proxying everything. */ 90628e82295SGarrett Wollman struct sockaddr_in sin; 90728e82295SGarrett Wollman 908603724d3SBjoern A. Zeeb if (!V_arp_proxyall) 909b2a8ac7cSLuigi Rizzo goto drop; 91028e82295SGarrett Wollman 91128e82295SGarrett Wollman bzero(&sin, sizeof sin); 91228e82295SGarrett Wollman sin.sin_family = AF_INET; 91328e82295SGarrett Wollman sin.sin_len = sizeof sin; 91428e82295SGarrett Wollman sin.sin_addr = itaddr; 91528e82295SGarrett Wollman 9168b07e49aSJulian Elischer /* XXX MRT use table 0 for arp reply */ 9178b07e49aSJulian Elischer rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 918b2a8ac7cSLuigi Rizzo if (!rt) 919b2a8ac7cSLuigi Rizzo goto drop; 92028e82295SGarrett Wollman /* 92128e82295SGarrett Wollman * Don't send proxies for nodes on the same interface 92228e82295SGarrett Wollman * as this one came out of, or we'll get into a fight 92328e82295SGarrett Wollman * over who claims what Ether address. 92428e82295SGarrett Wollman */ 925322dcb8dSMax Khon if (rt->rt_ifp == ifp) { 92628e82295SGarrett Wollman rtfree(rt); 927b2a8ac7cSLuigi Rizzo goto drop; 92828e82295SGarrett Wollman } 929322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 930a9771948SGleb Smirnoff (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 93128e82295SGarrett Wollman rtfree(rt); 932cc728227SDavid Malone 933cc728227SDavid Malone /* 934cc728227SDavid Malone * Also check that the node which sent the ARP packet 935cc728227SDavid Malone * is on the the interface we expect it to be on. This 936cc728227SDavid Malone * avoids ARP chaos if an interface is connected to the 937cc728227SDavid Malone * wrong network. 938cc728227SDavid Malone */ 939cc728227SDavid Malone sin.sin_addr = isaddr; 940cc728227SDavid Malone 9418b07e49aSJulian Elischer /* XXX MRT use table 0 for arp checks */ 9428b07e49aSJulian Elischer rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 943b2a8ac7cSLuigi Rizzo if (!rt) 944b2a8ac7cSLuigi Rizzo goto drop; 945322dcb8dSMax Khon if (rt->rt_ifp != ifp) { 946cc728227SDavid Malone log(LOG_INFO, "arp_proxy: ignoring request" 9479bf40edeSBrooks Davis " from %s via %s, expecting %s\n", 9489bf40edeSBrooks Davis inet_ntoa(isaddr), ifp->if_xname, 9499bf40edeSBrooks Davis rt->rt_ifp->if_xname); 950cc728227SDavid Malone rtfree(rt); 951b2a8ac7cSLuigi Rizzo goto drop; 952cc728227SDavid Malone } 953cc728227SDavid Malone rtfree(rt); 954cc728227SDavid Malone 955ac234f93SGarrett Wollman #ifdef DEBUG_PROXY 956ac234f93SGarrett Wollman printf("arp: proxying for %s\n", 957ef0cdf33SGarrett Wollman inet_ntoa(itaddr)); 958ac234f93SGarrett Wollman #endif 95928e82295SGarrett Wollman } else { 960510b360fSGleb Smirnoff /* 961510b360fSGleb Smirnoff * Return proxied ARP replies only on the interface 9625feebeebSAndrew Thompson * or bridge cluster where this network resides. 9635feebeebSAndrew Thompson * Otherwise we may conflict with the host we are 9645feebeebSAndrew Thompson * proxying for. 965510b360fSGleb Smirnoff */ 9665feebeebSAndrew Thompson if (rt->rt_ifp != ifp && 9675feebeebSAndrew Thompson (rt->rt_ifp->if_bridge != ifp->if_bridge || 9685feebeebSAndrew Thompson ifp->if_bridge == NULL)) { 969510b360fSGleb Smirnoff RT_UNLOCK(rt); 970510b360fSGleb Smirnoff goto drop; 971510b360fSGleb Smirnoff } 972df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 9731ed7bf1eSGleb Smirnoff (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 974322dcb8dSMax Khon (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 9751ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 97628e82295SGarrett Wollman } 977df8bae1dSRodney W. Grimes } 978df8bae1dSRodney W. Grimes 979d0558157SBruce M Simpson if (itaddr.s_addr == myaddr.s_addr && 980d0558157SBruce M Simpson IN_LINKLOCAL(ntohl(itaddr.s_addr))) { 981d0558157SBruce M Simpson /* RFC 3927 link-local IPv4; always reply by broadcast. */ 982d0558157SBruce M Simpson #ifdef DEBUG_LINKLOCAL 983d0558157SBruce M Simpson printf("arp: sending reply for link-local addr %s\n", 984d0558157SBruce M Simpson inet_ntoa(itaddr)); 985d0558157SBruce M Simpson #endif 986d0558157SBruce M Simpson m->m_flags |= M_BCAST; 987d0558157SBruce M Simpson m->m_flags &= ~M_MCAST; 988d0558157SBruce M Simpson } else { 989d0558157SBruce M Simpson /* default behaviour; never reply by broadcast. */ 990d0558157SBruce M Simpson m->m_flags &= ~(M_BCAST|M_MCAST); 991d0558157SBruce M Simpson } 992322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 993322dcb8dSMax Khon (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 994322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REPLY); 995322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 99664bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 99764bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 99864bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 99964bf80ceSMatthew N. Dodd sa.sa_len = 2; 1000322dcb8dSMax Khon (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 1001df8bae1dSRodney W. Grimes return; 1002b2a8ac7cSLuigi Rizzo 1003b2a8ac7cSLuigi Rizzo drop: 1004b2a8ac7cSLuigi Rizzo m_freem(m); 1005df8bae1dSRodney W. Grimes } 10061d5e9e22SEivind Eklund #endif 1007df8bae1dSRodney W. Grimes 1008df8bae1dSRodney W. Grimes /* 1009df8bae1dSRodney W. Grimes * Lookup or enter a new address in arptab. 1010df8bae1dSRodney W. Grimes */ 10111ed7bf1eSGleb Smirnoff static struct rtentry * 10128b07e49aSJulian Elischer arplookup(u_long addr, int create, int proxy, int fibnum) 1013df8bae1dSRodney W. Grimes { 1014e952fa39SMatthew N. Dodd struct rtentry *rt; 1015d1dd20beSSam Leffler struct sockaddr_inarp sin; 1016ac234f93SGarrett Wollman const char *why = 0; 1017df8bae1dSRodney W. Grimes 1018d1dd20beSSam Leffler bzero(&sin, sizeof(sin)); 1019d1dd20beSSam Leffler sin.sin_len = sizeof(sin); 1020d1dd20beSSam Leffler sin.sin_family = AF_INET; 1021df8bae1dSRodney W. Grimes sin.sin_addr.s_addr = addr; 1022d1dd20beSSam Leffler if (proxy) 1023d1dd20beSSam Leffler sin.sin_other = SIN_PROXY; 10248b07e49aSJulian Elischer rt = in_rtalloc1((struct sockaddr *)&sin, create, 0UL, fibnum); 1025df8bae1dSRodney W. Grimes if (rt == 0) 1026df8bae1dSRodney W. Grimes return (0); 1027ac234f93SGarrett Wollman 1028ac234f93SGarrett Wollman if (rt->rt_flags & RTF_GATEWAY) 1029ac234f93SGarrett Wollman why = "host is not on local network"; 1030ac234f93SGarrett Wollman else if ((rt->rt_flags & RTF_LLINFO) == 0) 1031ac234f93SGarrett Wollman why = "could not allocate llinfo"; 1032ac234f93SGarrett Wollman else if (rt->rt_gateway->sa_family != AF_LINK) 1033ac234f93SGarrett Wollman why = "gateway route is not ours"; 1034ac234f93SGarrett Wollman 1035fedf1d01SBruce M Simpson if (why) { 1036d1dd20beSSam Leffler #define ISDYNCLONE(_rt) \ 1037d1dd20beSSam Leffler (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED) 1038d1dd20beSSam Leffler if (create) 1039ac234f93SGarrett Wollman log(LOG_DEBUG, "arplookup %s failed: %s\n", 1040ef0cdf33SGarrett Wollman inet_ntoa(sin.sin_addr), why); 1041b75bead1SBruce M Simpson /* 1042b75bead1SBruce M Simpson * If there are no references to this Layer 2 route, 1043b75bead1SBruce M Simpson * and it is a cloned route, and not static, and 1044b75bead1SBruce M Simpson * arplookup() is creating the route, then purge 1045b75bead1SBruce M Simpson * it from the routing table as it is probably bogus. 1046b75bead1SBruce M Simpson */ 10479c63e9dbSSam Leffler if (rt->rt_refcnt == 1 && ISDYNCLONE(rt)) 10489c63e9dbSSam Leffler rtexpunge(rt); 10499c63e9dbSSam Leffler RTFREE_LOCKED(rt); 1050fedf1d01SBruce M Simpson return (0); 1051d1dd20beSSam Leffler #undef ISDYNCLONE 1052d1dd20beSSam Leffler } else { 10537138d65cSSam Leffler RT_REMREF(rt); 10541ed7bf1eSGleb Smirnoff return (rt); 1055df8bae1dSRodney W. Grimes } 1056d1dd20beSSam Leffler } 1057df8bae1dSRodney W. Grimes 1058dd2e4102SGarrett Wollman void 1059f2565d68SRobert Watson arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 1060dd2e4102SGarrett Wollman { 1061dd570d4dSTor Egge if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 1062322dcb8dSMax Khon arprequest(ifp, &IA_SIN(ifa)->sin_addr, 1063322dcb8dSMax Khon &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 1064dd2e4102SGarrett Wollman ifa->ifa_rtrequest = arp_rtrequest; 1065dd2e4102SGarrett Wollman ifa->ifa_flags |= RTF_CLONING; 1066dd2e4102SGarrett Wollman } 1067df5e1987SJonathan Lemon 1068a9771948SGleb Smirnoff void 1069f2565d68SRobert Watson arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 1070a9771948SGleb Smirnoff { 1071a9771948SGleb Smirnoff if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 1072a9771948SGleb Smirnoff arprequest(ifp, &IA_SIN(ifa)->sin_addr, 1073a9771948SGleb Smirnoff &IA_SIN(ifa)->sin_addr, enaddr); 1074a9771948SGleb Smirnoff ifa->ifa_rtrequest = arp_rtrequest; 1075a9771948SGleb Smirnoff ifa->ifa_flags |= RTF_CLONING; 1076a9771948SGleb Smirnoff } 1077a9771948SGleb Smirnoff 1078df5e1987SJonathan Lemon static void 1079df5e1987SJonathan Lemon arp_init(void) 1080df5e1987SJonathan Lemon { 1081df5e1987SJonathan Lemon 1082df5e1987SJonathan Lemon arpintrq.ifq_maxlen = 50; 10836008862bSJohn Baldwin mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF); 108459dd72d0SRobert Watson netisr_register(NETISR_ARP, arpintr, &arpintrq, 0); 1085df5e1987SJonathan Lemon } 1086df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 1087