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> 48885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h> 49885f1aa4SPoul-Henning Kamp #include <sys/malloc.h> 504458ac71SBruce Evans #include <sys/socket.h> 51885f1aa4SPoul-Henning Kamp #include <sys/syslog.h> 52df8bae1dSRodney W. Grimes 53df8bae1dSRodney W. Grimes #include <net/if.h> 54df8bae1dSRodney W. Grimes #include <net/if_dl.h> 55722012ccSJulian Elischer #include <net/if_types.h> 56df8bae1dSRodney W. Grimes #include <net/route.h> 57748e0b0aSGarrett Wollman #include <net/netisr.h> 58b149dd6cSLarry Lile #include <net/if_llc.h> 59c8f8e9c1SJulian Elischer #include <net/ethernet.h> 60df8bae1dSRodney W. Grimes 61df8bae1dSRodney W. Grimes #include <netinet/in.h> 62df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 63df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 64df8bae1dSRodney W. Grimes 65322dcb8dSMax Khon #include <net/if_arc.h> 66fda82fc2SJulian Elischer #include <net/iso88025.h> 67fda82fc2SJulian Elischer 68a9771948SGleb Smirnoff #ifdef DEV_CARP 69a9771948SGleb Smirnoff #include <netinet/ip_carp.h> 70a9771948SGleb Smirnoff #endif 71a9771948SGleb Smirnoff 72aed55708SRobert Watson #include <security/mac/mac_framework.h> 73aed55708SRobert Watson 74df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s) 75df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s) 76df8bae1dSRodney W. Grimes 77ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether); 78602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 79df8bae1dSRodney W. Grimes 80df8bae1dSRodney W. Grimes /* timer values */ 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, max_age, CTLFLAG_RW, 84bd2b686fSEd Maste &arpt_keep, 0, "ARP entry lifetime in seconds"); 85885f1aa4SPoul-Henning Kamp 86df8bae1dSRodney W. Grimes #define rt_expire rt_rmx.rmx_expire 87df8bae1dSRodney W. Grimes 8866800f57SGarrett Wollman struct llinfo_arp { 891daaa65dSGleb Smirnoff struct callout la_timer; 9066800f57SGarrett Wollman struct rtentry *la_rt; 9166800f57SGarrett Wollman struct mbuf *la_hold; /* last packet until resolved/timeout */ 92022695f8SOrion Hodson u_short la_preempt; /* countdown for pre-expiry arps */ 93e1ff74c5SGleb Smirnoff u_short la_asked; /* # requests sent */ 9466800f57SGarrett Wollman }; 9566800f57SGarrett Wollman 961cafed39SJonathan Lemon static struct ifqueue arpintrq; 97d1dd20beSSam Leffler static int arp_allocated; 98df8bae1dSRodney W. Grimes 99885f1aa4SPoul-Henning Kamp static int arp_maxtries = 5; 100602d513cSGarrett Wollman static int useloopback = 1; /* use loopback interface for local traffic */ 101885f1aa4SPoul-Henning Kamp static int arp_proxyall = 0; 102602d513cSGarrett Wollman 103602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 104bd2b686fSEd Maste &arp_maxtries, 0, "ARP resolution attempts before returning error"); 105602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 106bd2b686fSEd Maste &useloopback, 0, "Use the loopback interface for local traffic"); 107602d513cSGarrett Wollman SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 108bd2b686fSEd Maste &arp_proxyall, 0, "Enable proxy ARP for all suitable requests"); 109885f1aa4SPoul-Henning Kamp 1104d77a549SAlfred Perlstein static void arp_init(void); 1114d77a549SAlfred Perlstein static void arp_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 1124d77a549SAlfred Perlstein static void arprequest(struct ifnet *, 1134d77a549SAlfred Perlstein struct in_addr *, struct in_addr *, u_char *); 1141cafed39SJonathan Lemon static void arpintr(struct mbuf *); 1154d77a549SAlfred Perlstein static void arptimer(void *); 1161ed7bf1eSGleb Smirnoff static struct rtentry 1174d77a549SAlfred Perlstein *arplookup(u_long, int, int); 1181d5e9e22SEivind Eklund #ifdef INET 1194d77a549SAlfred Perlstein static void in_arpinput(struct mbuf *); 1201d5e9e22SEivind Eklund #endif 12128e82295SGarrett Wollman 122df8bae1dSRodney W. Grimes /* 1231daaa65dSGleb Smirnoff * Timeout routine. 124df8bae1dSRodney W. Grimes */ 125df8bae1dSRodney W. Grimes static void 1261daaa65dSGleb Smirnoff arptimer(void *arg) 127df8bae1dSRodney W. Grimes { 1281daaa65dSGleb Smirnoff struct rtentry *rt = (struct rtentry *)arg; 129df8bae1dSRodney W. Grimes 1301daaa65dSGleb Smirnoff RT_LOCK_ASSERT(rt); 1311ed7bf1eSGleb Smirnoff /* 1321daaa65dSGleb Smirnoff * The lock is needed to close a theoretical race 1331daaa65dSGleb Smirnoff * between spontaneous expiry and intentional removal. 1341daaa65dSGleb Smirnoff * We still got an extra reference on rtentry, so can 1351daaa65dSGleb Smirnoff * safely pass pointers to its contents. 1361ed7bf1eSGleb Smirnoff */ 1371ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 138d1dd20beSSam Leffler 1391daaa65dSGleb Smirnoff rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt), 0, NULL); 140df8bae1dSRodney W. Grimes } 141df8bae1dSRodney W. Grimes 142df8bae1dSRodney W. Grimes /* 143df8bae1dSRodney W. Grimes * Parallel to llc_rtrequest. 144df8bae1dSRodney W. Grimes */ 145b2774d00SGarrett Wollman static void 146f2565d68SRobert Watson arp_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info) 147df8bae1dSRodney W. Grimes { 148e952fa39SMatthew N. Dodd struct sockaddr *gate; 149e952fa39SMatthew N. Dodd struct llinfo_arp *la; 150df8bae1dSRodney W. Grimes static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 151067a8babSMax Laier struct in_ifaddr *ia; 152067a8babSMax Laier struct ifaddr *ifa; 153df8bae1dSRodney W. Grimes 154d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 155d1dd20beSSam Leffler 156df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_GATEWAY) 157df8bae1dSRodney W. Grimes return; 158d1dd20beSSam Leffler gate = rt->rt_gateway; 159d1dd20beSSam Leffler la = (struct llinfo_arp *)rt->rt_llinfo; 160df8bae1dSRodney W. Grimes switch (req) { 161df8bae1dSRodney W. Grimes 162df8bae1dSRodney W. Grimes case RTM_ADD: 163df8bae1dSRodney W. Grimes /* 164df8bae1dSRodney W. Grimes * XXX: If this is a manually added route to interface 165df8bae1dSRodney W. Grimes * such as older version of routed or gated might provide, 166df8bae1dSRodney W. Grimes * restore cloning bit. 167df8bae1dSRodney W. Grimes */ 168df8bae1dSRodney W. Grimes if ((rt->rt_flags & RTF_HOST) == 0 && 169d6fa5d28SBruce M Simpson rt_mask(rt) != NULL && 170df8bae1dSRodney W. Grimes SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 171df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_CLONING; 172df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_CLONING) { 173df8bae1dSRodney W. Grimes /* 174df8bae1dSRodney W. Grimes * Case 1: This route should come from a route to iface. 175df8bae1dSRodney W. Grimes */ 176df8bae1dSRodney W. Grimes rt_setgate(rt, rt_key(rt), 177df8bae1dSRodney W. Grimes (struct sockaddr *)&null_sdl); 178df8bae1dSRodney W. Grimes gate = rt->rt_gateway; 179df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 180df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 181fe53256dSAndre Oppermann rt->rt_expire = time_uptime; 182df8bae1dSRodney W. Grimes break; 183df8bae1dSRodney W. Grimes } 184df8bae1dSRodney W. Grimes /* Announce a new entry if requested. */ 185df8bae1dSRodney W. Grimes if (rt->rt_flags & RTF_ANNOUNCE) 186322dcb8dSMax Khon arprequest(rt->rt_ifp, 187ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 188ed7509acSJulian Elischer &SIN(rt_key(rt))->sin_addr, 189df8bae1dSRodney W. Grimes (u_char *)LLADDR(SDL(gate))); 190df8bae1dSRodney W. Grimes /*FALLTHROUGH*/ 191df8bae1dSRodney W. Grimes case RTM_RESOLVE: 192df8bae1dSRodney W. Grimes if (gate->sa_family != AF_LINK || 193df8bae1dSRodney W. Grimes gate->sa_len < sizeof(null_sdl)) { 194d1dd20beSSam Leffler log(LOG_DEBUG, "%s: bad gateway %s%s\n", __func__, 195beb2ced8SBruce M Simpson inet_ntoa(SIN(rt_key(rt))->sin_addr), 196beb2ced8SBruce M Simpson (gate->sa_family != AF_LINK) ? 197c3b52d64SBruce M Simpson " (!AF_LINK)": ""); 198df8bae1dSRodney W. Grimes break; 199df8bae1dSRodney W. Grimes } 200df8bae1dSRodney W. Grimes SDL(gate)->sdl_type = rt->rt_ifp->if_type; 201df8bae1dSRodney W. Grimes SDL(gate)->sdl_index = rt->rt_ifp->if_index; 202df8bae1dSRodney W. Grimes if (la != 0) 203df8bae1dSRodney W. Grimes break; /* This happens on a route change */ 204df8bae1dSRodney W. Grimes /* 205df8bae1dSRodney W. Grimes * Case 2: This route may come from cloning, or a manual route 206df8bae1dSRodney W. Grimes * add with a LL address. 207df8bae1dSRodney W. Grimes */ 208d1dd20beSSam Leffler R_Zalloc(la, struct llinfo_arp *, sizeof(*la)); 209df8bae1dSRodney W. Grimes rt->rt_llinfo = (caddr_t)la; 210df8bae1dSRodney W. Grimes if (la == 0) { 211d1dd20beSSam Leffler log(LOG_DEBUG, "%s: malloc failed\n", __func__); 212df8bae1dSRodney W. Grimes break; 213df8bae1dSRodney W. Grimes } 214d1dd20beSSam Leffler arp_allocated++; 2151ed7bf1eSGleb Smirnoff /* 2161ed7bf1eSGleb Smirnoff * We are storing a route entry outside of radix tree. So, 2171ed7bf1eSGleb Smirnoff * it can be found and accessed by other means than radix 2181ed7bf1eSGleb Smirnoff * lookup. The routing code assumes that any rtentry detached 2191ed7bf1eSGleb Smirnoff * from radix can be destroyed safely. To prevent this, we 2201ed7bf1eSGleb Smirnoff * add an additional reference. 2211ed7bf1eSGleb Smirnoff */ 2221ed7bf1eSGleb Smirnoff RT_ADDREF(rt); 223df8bae1dSRodney W. Grimes la->la_rt = rt; 224df8bae1dSRodney W. Grimes rt->rt_flags |= RTF_LLINFO; 2251daaa65dSGleb Smirnoff callout_init_mtx(&la->la_timer, &rt->rt_mtx, 2261daaa65dSGleb Smirnoff CALLOUT_RETURNUNLOCKED); 227cbb0b46aSGarrett Wollman 2281d5e9e22SEivind Eklund #ifdef INET 229cbb0b46aSGarrett Wollman /* 230cbb0b46aSGarrett Wollman * This keeps the multicast addresses from showing up 231cbb0b46aSGarrett Wollman * in `arp -a' listings as unresolved. It's not actually 232cbb0b46aSGarrett Wollman * functional. Then the same for broadcast. 233cbb0b46aSGarrett Wollman */ 234c448c89cSJonathan Lemon if (IN_MULTICAST(ntohl(SIN(rt_key(rt))->sin_addr.s_addr)) && 235c448c89cSJonathan Lemon rt->rt_ifp->if_type != IFT_ARCNET) { 236cbb0b46aSGarrett Wollman ETHER_MAP_IP_MULTICAST(&SIN(rt_key(rt))->sin_addr, 237cbb0b46aSGarrett Wollman LLADDR(SDL(gate))); 238cbb0b46aSGarrett Wollman SDL(gate)->sdl_alen = 6; 23951a109a1SPeter Wemm rt->rt_expire = 0; 240cbb0b46aSGarrett Wollman } 241cbb0b46aSGarrett Wollman if (in_broadcast(SIN(rt_key(rt))->sin_addr, rt->rt_ifp)) { 242322dcb8dSMax Khon memcpy(LLADDR(SDL(gate)), rt->rt_ifp->if_broadcastaddr, 243322dcb8dSMax Khon rt->rt_ifp->if_addrlen); 244322dcb8dSMax Khon SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen; 24551a109a1SPeter Wemm rt->rt_expire = 0; 246cbb0b46aSGarrett Wollman } 2471d5e9e22SEivind Eklund #endif 248cbb0b46aSGarrett Wollman 249067a8babSMax Laier TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 250067a8babSMax Laier if (ia->ia_ifp == rt->rt_ifp && 251067a8babSMax Laier SIN(rt_key(rt))->sin_addr.s_addr == 252067a8babSMax Laier (IA_SIN(ia))->sin_addr.s_addr) 253067a8babSMax Laier break; 254067a8babSMax Laier } 255067a8babSMax Laier if (ia) { 256df8bae1dSRodney W. Grimes /* 257df8bae1dSRodney W. Grimes * This test used to be 258df8bae1dSRodney W. Grimes * if (loif.if_flags & IFF_UP) 259df8bae1dSRodney W. Grimes * It allowed local traffic to be forced 260df8bae1dSRodney W. Grimes * through the hardware by configuring the loopback down. 261df8bae1dSRodney W. Grimes * However, it causes problems during network configuration 262df8bae1dSRodney W. Grimes * for boards that can't receive packets they send. 263df8bae1dSRodney W. Grimes * It is now necessary to clear "useloopback" and remove 264df8bae1dSRodney W. Grimes * the route to force traffic out to the hardware. 265df8bae1dSRodney W. Grimes */ 266df8bae1dSRodney W. Grimes rt->rt_expire = 0; 267ac912b2dSLuigi Rizzo bcopy(IF_LLADDR(rt->rt_ifp), LLADDR(SDL(gate)), 268322dcb8dSMax Khon SDL(gate)->sdl_alen = rt->rt_ifp->if_addrlen); 269402865f6SJohn-Mark Gurney if (useloopback) { 270f5fea3ddSPaul Traina rt->rt_ifp = loif; 271402865f6SJohn-Mark Gurney rt->rt_rmx.rmx_mtu = loif->if_mtu; 272402865f6SJohn-Mark Gurney } 273df8bae1dSRodney W. Grimes 274067a8babSMax Laier /* 275067a8babSMax Laier * make sure to set rt->rt_ifa to the interface 276067a8babSMax Laier * address we are using, otherwise we will have trouble 277067a8babSMax Laier * with source address selection. 278067a8babSMax Laier */ 279067a8babSMax Laier ifa = &ia->ia_ifa; 280067a8babSMax Laier if (ifa != rt->rt_ifa) { 281067a8babSMax Laier IFAFREE(rt->rt_ifa); 282067a8babSMax Laier IFAREF(ifa); 283067a8babSMax Laier rt->rt_ifa = ifa; 284067a8babSMax Laier } 285df8bae1dSRodney W. Grimes } 286df8bae1dSRodney W. Grimes break; 287df8bae1dSRodney W. Grimes 288df8bae1dSRodney W. Grimes case RTM_DELETE: 2891daaa65dSGleb Smirnoff if (la == NULL) /* XXX: at least CARP does this. */ 290df8bae1dSRodney W. Grimes break; 2911daaa65dSGleb Smirnoff callout_stop(&la->la_timer); 2921daaa65dSGleb Smirnoff rt->rt_llinfo = NULL; 293df8bae1dSRodney W. Grimes rt->rt_flags &= ~RTF_LLINFO; 2941daaa65dSGleb Smirnoff RT_REMREF(rt); 295df8bae1dSRodney W. Grimes if (la->la_hold) 296df8bae1dSRodney W. Grimes m_freem(la->la_hold); 297df8bae1dSRodney W. Grimes Free((caddr_t)la); 298df8bae1dSRodney W. Grimes } 299df8bae1dSRodney W. Grimes } 300df8bae1dSRodney W. Grimes 301df8bae1dSRodney W. Grimes /* 302df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies: 303df8bae1dSRodney W. Grimes * - arp header source ip address 304df8bae1dSRodney W. Grimes * - arp header target ip address 305df8bae1dSRodney W. Grimes * - arp header source ethernet address 306df8bae1dSRodney W. Grimes */ 307df8bae1dSRodney W. Grimes static void 308f2565d68SRobert Watson arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, 309f2565d68SRobert Watson u_char *enaddr) 310df8bae1dSRodney W. Grimes { 311e952fa39SMatthew N. Dodd struct mbuf *m; 312e952fa39SMatthew N. Dodd struct arphdr *ah; 313df8bae1dSRodney W. Grimes struct sockaddr sa; 314df8bae1dSRodney W. Grimes 315a163d034SWarner Losh if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 316df8bae1dSRodney W. Grimes return; 31764bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 31864bf80ceSMatthew N. Dodd 2*ifp->if_data.ifi_addrlen; 31964bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 32064bf80ceSMatthew N. Dodd MH_ALIGN(m, m->m_len); 32164bf80ceSMatthew N. Dodd ah = mtod(m, struct arphdr *); 32264bf80ceSMatthew N. Dodd bzero((caddr_t)ah, m->m_len); 32319527d3eSRobert Watson #ifdef MAC 32419527d3eSRobert Watson mac_create_mbuf_linklayer(ifp, m); 32519527d3eSRobert Watson #endif 326322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); 327322dcb8dSMax Khon ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 328322dcb8dSMax Khon ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 329322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REQUEST); 33064bf80ceSMatthew N. Dodd bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 33164bf80ceSMatthew N. Dodd bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 33264bf80ceSMatthew N. Dodd bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 33364bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 33464bf80ceSMatthew N. Dodd sa.sa_len = 2; 33564bf80ceSMatthew N. Dodd m->m_flags |= M_BCAST; 336322dcb8dSMax Khon (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 33764bf80ceSMatthew N. Dodd 33864bf80ceSMatthew N. Dodd return; 339df8bae1dSRodney W. Grimes } 340df8bae1dSRodney W. Grimes 341df8bae1dSRodney W. Grimes /* 342cd46a114SLuigi Rizzo * Resolve an IP address into an ethernet address. 343cd46a114SLuigi Rizzo * On input: 344cd46a114SLuigi Rizzo * ifp is the interface we use 345cd46a114SLuigi Rizzo * dst is the next hop, 346cd46a114SLuigi Rizzo * rt0 is the route to the final destination (possibly useless) 347cd46a114SLuigi Rizzo * m is the mbuf 348cd46a114SLuigi Rizzo * desten is where we want the address. 349cd46a114SLuigi Rizzo * 350cd46a114SLuigi Rizzo * On success, desten is filled in and the function returns 0; 351cd46a114SLuigi Rizzo * If the packet must be held pending resolution, we return EWOULDBLOCK 352cd46a114SLuigi Rizzo * On other errors, we return the corresponding error code. 353df8bae1dSRodney W. Grimes */ 354df8bae1dSRodney W. Grimes int 355cd46a114SLuigi Rizzo arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 356f7c5baa1SLuigi Rizzo struct sockaddr *dst, u_char *desten) 357df8bae1dSRodney W. Grimes { 3581ed7bf1eSGleb Smirnoff struct llinfo_arp *la = NULL; 3591ed7bf1eSGleb Smirnoff struct rtentry *rt = NULL; 360df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 361cd46a114SLuigi Rizzo int error; 362df8bae1dSRodney W. Grimes 363df8bae1dSRodney W. Grimes if (m->m_flags & M_BCAST) { /* broadcast */ 364322dcb8dSMax Khon (void)memcpy(desten, ifp->if_broadcastaddr, ifp->if_addrlen); 365cd46a114SLuigi Rizzo return (0); 366df8bae1dSRodney W. Grimes } 367322dcb8dSMax Khon if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {/* multicast */ 368df8bae1dSRodney W. Grimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 369cd46a114SLuigi Rizzo return (0); 370df8bae1dSRodney W. Grimes } 3711ed7bf1eSGleb Smirnoff 3721ed7bf1eSGleb Smirnoff if (rt0 != NULL) { 3731ed7bf1eSGleb Smirnoff error = rt_check(&rt, &rt0, dst); 3741ed7bf1eSGleb Smirnoff if (error) { 3751ed7bf1eSGleb Smirnoff m_freem(m); 3761ed7bf1eSGleb Smirnoff return error; 377df8bae1dSRodney W. Grimes } 3781ed7bf1eSGleb Smirnoff la = (struct llinfo_arp *)rt->rt_llinfo; 3791ed7bf1eSGleb Smirnoff if (la == NULL) 3801ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 3811ed7bf1eSGleb Smirnoff } 3821ed7bf1eSGleb Smirnoff if (la == NULL) { 3831ed7bf1eSGleb Smirnoff /* 3841ed7bf1eSGleb Smirnoff * We enter this block in case if rt0 was NULL, 3851ed7bf1eSGleb Smirnoff * or if rt found by rt_check() didn't have llinfo. 3861ed7bf1eSGleb Smirnoff */ 3871ed7bf1eSGleb Smirnoff rt = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0); 3881ed7bf1eSGleb Smirnoff if (rt == NULL) { 3891ed7bf1eSGleb Smirnoff log(LOG_DEBUG, 3901ed7bf1eSGleb Smirnoff "arpresolve: can't allocate route for %s\n", 3911ed7bf1eSGleb Smirnoff inet_ntoa(SIN(dst)->sin_addr)); 392df8bae1dSRodney W. Grimes m_freem(m); 393cd46a114SLuigi Rizzo return (EINVAL); /* XXX */ 394df8bae1dSRodney W. Grimes } 3951ed7bf1eSGleb Smirnoff la = (struct llinfo_arp *)rt->rt_llinfo; 3961ed7bf1eSGleb Smirnoff if (la == NULL) { 3971ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 3981ed7bf1eSGleb Smirnoff log(LOG_DEBUG, 3991ed7bf1eSGleb Smirnoff "arpresolve: can't allocate llinfo for %s\n", 4001ed7bf1eSGleb Smirnoff inet_ntoa(SIN(dst)->sin_addr)); 4011ed7bf1eSGleb Smirnoff m_freem(m); 4021ed7bf1eSGleb Smirnoff return (EINVAL); /* XXX */ 4031ed7bf1eSGleb Smirnoff } 4041ed7bf1eSGleb Smirnoff } 405df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 406df8bae1dSRodney W. Grimes /* 407df8bae1dSRodney W. Grimes * Check the address family and length is valid, the address 408df8bae1dSRodney W. Grimes * is resolved; otherwise, try to resolve. 409df8bae1dSRodney W. Grimes */ 410fe53256dSAndre Oppermann if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) && 411df8bae1dSRodney W. Grimes sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { 412a20e2538SGleb Smirnoff 413a20e2538SGleb Smirnoff bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 414a20e2538SGleb Smirnoff 415f0f3379eSOrion Hodson /* 416f0f3379eSOrion Hodson * If entry has an expiry time and it is approaching, 417e1ff74c5SGleb Smirnoff * send an ARP request. 418f0f3379eSOrion Hodson */ 419f0f3379eSOrion Hodson if ((rt->rt_expire != 0) && 420fe53256dSAndre Oppermann (time_uptime + la->la_preempt > rt->rt_expire)) { 421a20e2538SGleb Smirnoff struct in_addr sin = 422a20e2538SGleb Smirnoff SIN(rt->rt_ifa->ifa_addr)->sin_addr; 423a20e2538SGleb Smirnoff 424022695f8SOrion Hodson la->la_preempt--; 425a20e2538SGleb Smirnoff RT_UNLOCK(rt); 426a20e2538SGleb Smirnoff arprequest(ifp, &sin, &SIN(dst)->sin_addr, 427a20e2538SGleb Smirnoff IF_LLADDR(ifp)); 428a20e2538SGleb Smirnoff return (0); 429f0f3379eSOrion Hodson } 430f0f3379eSOrion Hodson 4311ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 432cd46a114SLuigi Rizzo return (0); 433df8bae1dSRodney W. Grimes } 434df8bae1dSRodney W. Grimes /* 435deb62e28SRuslan Ermilov * If ARP is disabled or static on this interface, stop. 43608aadfbbSJonathan Lemon * XXX 43708aadfbbSJonathan Lemon * Probably should not allocate empty llinfo struct if we are 43808aadfbbSJonathan Lemon * not going to be sending out an arp request. 43908aadfbbSJonathan Lemon */ 440deb62e28SRuslan Ermilov if (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) { 4411ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 44247891de1SRuslan Ermilov m_freem(m); 443cd46a114SLuigi Rizzo return (EINVAL); 44447891de1SRuslan Ermilov } 44508aadfbbSJonathan Lemon /* 446df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address 447df8bae1dSRodney W. Grimes * response yet. Replace the held mbuf with this 448df8bae1dSRodney W. Grimes * latest one. 449df8bae1dSRodney W. Grimes */ 450df8bae1dSRodney W. Grimes if (la->la_hold) 451df8bae1dSRodney W. Grimes m_freem(la->la_hold); 452df8bae1dSRodney W. Grimes la->la_hold = m; 453e1ff74c5SGleb Smirnoff 454e1ff74c5SGleb Smirnoff KASSERT(rt->rt_expire > 0, ("sending ARP request for static entry")); 455e1ff74c5SGleb Smirnoff 456e1ff74c5SGleb Smirnoff /* 457e1ff74c5SGleb Smirnoff * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 458e1ff74c5SGleb Smirnoff * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 459e1ff74c5SGleb Smirnoff * if we have already sent arp_maxtries ARP requests. Retransmit the 460e1ff74c5SGleb Smirnoff * ARP request, but not faster than one request per second. 461e1ff74c5SGleb Smirnoff */ 462e1ff74c5SGleb Smirnoff if (la->la_asked < arp_maxtries) 463e1ff74c5SGleb Smirnoff error = EWOULDBLOCK; /* First request. */ 464e1ff74c5SGleb Smirnoff else 465e1ff74c5SGleb Smirnoff error = (rt == rt0) ? EHOSTDOWN : EHOSTUNREACH; 466e1ff74c5SGleb Smirnoff 46795ebcabeSMaxim Konovalov if (la->la_asked == 0 || rt->rt_expire != time_uptime) { 468a20e2538SGleb Smirnoff struct in_addr sin = 469a20e2538SGleb Smirnoff SIN(rt->rt_ifa->ifa_addr)->sin_addr; 470a20e2538SGleb Smirnoff 471e1ff74c5SGleb Smirnoff rt->rt_expire = time_uptime; 4721daaa65dSGleb Smirnoff callout_reset(&la->la_timer, hz, arptimer, rt); 47395ebcabeSMaxim Konovalov la->la_asked++; 474a20e2538SGleb Smirnoff RT_UNLOCK(rt); 475e1ff74c5SGleb Smirnoff 476a20e2538SGleb Smirnoff arprequest(ifp, &sin, &SIN(dst)->sin_addr, 477322dcb8dSMax Khon IF_LLADDR(ifp)); 478e1ff74c5SGleb Smirnoff } else 4791ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 480e1ff74c5SGleb Smirnoff 481e1ff74c5SGleb Smirnoff return (error); 482df8bae1dSRodney W. Grimes } 483df8bae1dSRodney W. Grimes 484df8bae1dSRodney W. Grimes /* 485df8bae1dSRodney W. Grimes * Common length and type checks are done here, 486df8bae1dSRodney W. Grimes * then the protocol-specific routine is called. 487df8bae1dSRodney W. Grimes */ 488885f1aa4SPoul-Henning Kamp static void 4891cafed39SJonathan Lemon arpintr(struct mbuf *m) 490df8bae1dSRodney W. Grimes { 4911cafed39SJonathan Lemon struct arphdr *ar; 492df8bae1dSRodney W. Grimes 49376ec7b2fSRobert Watson if (m->m_len < sizeof(struct arphdr) && 49484365e2bSMatthew Dillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 495e44d6283SJoerg Wunsch log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 4961cafed39SJonathan Lemon return; 49776ec7b2fSRobert Watson } 49876ec7b2fSRobert Watson ar = mtod(m, struct arphdr *); 49976ec7b2fSRobert Watson 5001cafed39SJonathan Lemon if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 5011cafed39SJonathan Lemon ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 502b8b33234SDoug Rabson ntohs(ar->ar_hrd) != ARPHRD_ARCNET && 503b8b33234SDoug Rabson ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) { 5041cafed39SJonathan Lemon log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 50576ec7b2fSRobert Watson (unsigned char *)&ar->ar_hrd, ""); 50676ec7b2fSRobert Watson m_freem(m); 5071cafed39SJonathan Lemon return; 50876ec7b2fSRobert Watson } 50976ec7b2fSRobert Watson 51031175791SRuslan Ermilov if (m->m_len < arphdr_len(ar)) { 51178e2d2bdSRuslan Ermilov if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 512f72d9d83SJoerg Wunsch log(LOG_ERR, "arp: runt packet\n"); 51376ec7b2fSRobert Watson m_freem(m); 5141cafed39SJonathan Lemon return; 51576ec7b2fSRobert Watson } 51678e2d2bdSRuslan Ermilov ar = mtod(m, struct arphdr *); 51778e2d2bdSRuslan Ermilov } 518df8bae1dSRodney W. Grimes 519df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) { 5201d5e9e22SEivind Eklund #ifdef INET 521df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 522df8bae1dSRodney W. Grimes in_arpinput(m); 5231cafed39SJonathan Lemon return; 5241d5e9e22SEivind Eklund #endif 525df8bae1dSRodney W. Grimes } 526df8bae1dSRodney W. Grimes m_freem(m); 527df8bae1dSRodney W. Grimes } 528df8bae1dSRodney W. Grimes 5291d5e9e22SEivind Eklund #ifdef INET 530df8bae1dSRodney W. Grimes /* 531df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet. 532df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826. 533df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender 534df8bae1dSRodney W. Grimes * protocol address, to catch impersonators. 535df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol: 536df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 537df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us, 538df8bae1dSRodney W. Grimes * and also sent them in response to IP replies. 539df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive 540df8bae1dSRodney W. Grimes * trailer packets. 541df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 542df8bae1dSRodney W. Grimes * but formerly didn't normally send requests. 543df8bae1dSRodney W. Grimes */ 5443269187dSAlfred Perlstein static int log_arp_wrong_iface = 1; 545e3d123d6SAlfred Perlstein static int log_arp_movements = 1; 54639393906SGleb Smirnoff static int log_arp_permanent_modify = 1; 5473269187dSAlfred Perlstein 5483269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 5493269187dSAlfred Perlstein &log_arp_wrong_iface, 0, 5503269187dSAlfred Perlstein "log arp packets arriving on the wrong interface"); 551e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 552e3d123d6SAlfred Perlstein &log_arp_movements, 0, 55375ce3221SAlfred Perlstein "log arp replies from MACs different than the one in the cache"); 55439393906SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, 55539393906SGleb Smirnoff &log_arp_permanent_modify, 0, 55639393906SGleb Smirnoff "log arp replies from MACs different than the one in the permanent arp entry"); 557e3d123d6SAlfred Perlstein 5583269187dSAlfred Perlstein 559df8bae1dSRodney W. Grimes static void 560f2565d68SRobert Watson in_arpinput(struct mbuf *m) 561df8bae1dSRodney W. Grimes { 562e952fa39SMatthew N. Dodd struct arphdr *ah; 563e952fa39SMatthew N. Dodd struct ifnet *ifp = m->m_pkthdr.rcvif; 5641ed7bf1eSGleb Smirnoff struct llinfo_arp *la; 565e952fa39SMatthew N. Dodd struct rtentry *rt; 566ca925d9cSJonathan Lemon struct ifaddr *ifa; 567ca925d9cSJonathan Lemon struct in_ifaddr *ia; 568df8bae1dSRodney W. Grimes struct sockaddr_dl *sdl; 569df8bae1dSRodney W. Grimes struct sockaddr sa; 570df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr; 5711ed7bf1eSGleb Smirnoff struct mbuf *hold; 572a9771948SGleb Smirnoff u_int8_t *enaddr = NULL; 573b149dd6cSLarry Lile int op, rif_len; 574322dcb8dSMax Khon int req_len; 5758f867517SAndrew Thompson int bridged = 0; 576422a115aSGleb Smirnoff #ifdef DEV_CARP 5772ef4a436SGleb Smirnoff int carp_match = 0; 578422a115aSGleb Smirnoff #endif 579df8bae1dSRodney W. Grimes 58074948aa6SAndrew Thompson if (ifp->if_bridge) 5818f867517SAndrew Thompson bridged = 1; 5828f867517SAndrew Thompson 583322dcb8dSMax Khon req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 584322dcb8dSMax Khon if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 5854cbc8ad1SYaroslav Tykhiy log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 5864cbc8ad1SYaroslav Tykhiy return; 5874cbc8ad1SYaroslav Tykhiy } 5884cbc8ad1SYaroslav Tykhiy 589322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 590322dcb8dSMax Khon op = ntohs(ah->ar_op); 591322dcb8dSMax Khon (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 592322dcb8dSMax Khon (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 59332439868SGleb Smirnoff 594ca925d9cSJonathan Lemon /* 595ca925d9cSJonathan Lemon * For a bridge, we want to check the address irrespective 596ca925d9cSJonathan Lemon * of the receive interface. (This will change slightly 597ca925d9cSJonathan Lemon * when we have clusters of interfaces). 598a9771948SGleb Smirnoff * If the interface does not match, but the recieving interface 599a9771948SGleb Smirnoff * is part of carp, we call carp_iamatch to see if this is a 600a9771948SGleb Smirnoff * request for the virtual host ip. 601a9771948SGleb Smirnoff * XXX: This is really ugly! 602ca925d9cSJonathan Lemon */ 6032ef4a436SGleb Smirnoff LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 6047f2d8767SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge != NULL) || 605235073f4SAndrew Thompson (ia->ia_ifp == ifp)) && 6062ef4a436SGleb Smirnoff itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 607ca925d9cSJonathan Lemon goto match; 6082ef4a436SGleb Smirnoff #ifdef DEV_CARP 6092ef4a436SGleb Smirnoff if (ifp->if_carp != NULL && 6102ef4a436SGleb Smirnoff carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) && 6112ef4a436SGleb Smirnoff itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 6122ef4a436SGleb Smirnoff carp_match = 1; 6132ef4a436SGleb Smirnoff goto match; 6142ef4a436SGleb Smirnoff } 6152ef4a436SGleb Smirnoff #endif 6162ef4a436SGleb Smirnoff } 617ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 6187f2d8767SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge != NULL) || 619235073f4SAndrew Thompson (ia->ia_ifp == ifp)) && 620ca925d9cSJonathan Lemon isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) 621ca925d9cSJonathan Lemon goto match; 622ca925d9cSJonathan Lemon /* 623d8b84d9eSJonathan Lemon * No match, use the first inet address on the receive interface 624ca925d9cSJonathan Lemon * as a dummy address for the rest of the function. 625ca925d9cSJonathan Lemon */ 626d8b84d9eSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 6274b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_INET) { 628ec691a10SJonathan Lemon ia = ifatoia(ifa); 629ec691a10SJonathan Lemon goto match; 630ec691a10SJonathan Lemon } 631ec691a10SJonathan Lemon /* 632ec691a10SJonathan Lemon * If bridging, fall back to using any inet address. 633ec691a10SJonathan Lemon */ 6348f867517SAndrew Thompson if (!bridged || (ia = TAILQ_FIRST(&in_ifaddrhead)) == NULL) 635b2a8ac7cSLuigi Rizzo goto drop; 636ca925d9cSJonathan Lemon match: 637a9771948SGleb Smirnoff if (!enaddr) 638a9771948SGleb Smirnoff enaddr = (u_int8_t *)IF_LLADDR(ifp); 639ca925d9cSJonathan Lemon myaddr = ia->ia_addr.sin_addr; 640a9771948SGleb Smirnoff if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 641b2a8ac7cSLuigi Rizzo goto drop; /* it's from me, ignore it. */ 642322dcb8dSMax Khon if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 643df8bae1dSRodney W. Grimes log(LOG_ERR, 644322dcb8dSMax Khon "arp: link address is broadcast for IP address %s!\n", 645ef0cdf33SGarrett Wollman inet_ntoa(isaddr)); 646b2a8ac7cSLuigi Rizzo goto drop; 647df8bae1dSRodney W. Grimes } 64800fcf9d1SRobert Watson /* 64900fcf9d1SRobert Watson * Warn if another host is using the same IP address, but only if the 65000fcf9d1SRobert Watson * IP address isn't 0.0.0.0, which is used for DHCP only, in which 65100fcf9d1SRobert Watson * case we suppress the warning to avoid false positive complaints of 65200fcf9d1SRobert Watson * potential misconfiguration. 65300fcf9d1SRobert Watson */ 654f69453caSAndrew Thompson if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 655df8bae1dSRodney W. Grimes log(LOG_ERR, 656322dcb8dSMax Khon "arp: %*D is using my IP address %s!\n", 657322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 658322dcb8dSMax Khon inet_ntoa(isaddr)); 659df8bae1dSRodney W. Grimes itaddr = myaddr; 660df8bae1dSRodney W. Grimes goto reply; 661df8bae1dSRodney W. Grimes } 662deb62e28SRuslan Ermilov if (ifp->if_flags & IFF_STATICARP) 663deb62e28SRuslan Ermilov goto reply; 6641ed7bf1eSGleb Smirnoff rt = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0); 6651ed7bf1eSGleb Smirnoff if (rt != NULL) { 6661ed7bf1eSGleb Smirnoff la = (struct llinfo_arp *)rt->rt_llinfo; 6671ed7bf1eSGleb Smirnoff if (la == NULL) { 6681ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 6691ed7bf1eSGleb Smirnoff goto reply; 6701ed7bf1eSGleb Smirnoff } 6711ed7bf1eSGleb Smirnoff } else 6721ed7bf1eSGleb Smirnoff goto reply; 6731ed7bf1eSGleb Smirnoff 6741ed7bf1eSGleb Smirnoff /* The following is not an error when doing bridging. */ 6758f867517SAndrew Thompson if (!bridged && rt->rt_ifp != ifp 676422a115aSGleb Smirnoff #ifdef DEV_CARP 677422a115aSGleb Smirnoff && (ifp->if_type != IFT_CARP || !carp_match) 678422a115aSGleb Smirnoff #endif 679422a115aSGleb Smirnoff ) { 6803269187dSAlfred Perlstein if (log_arp_wrong_iface) 6819bf40edeSBrooks Davis log(LOG_ERR, "arp: %s is on %s but got reply from %*D on %s\n", 682dd9b6ddeSBill Fenner inet_ntoa(isaddr), 6839bf40edeSBrooks Davis rt->rt_ifp->if_xname, 684322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 6859bf40edeSBrooks Davis ifp->if_xname); 6861ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 687dd9b6ddeSBill Fenner goto reply; 688dd9b6ddeSBill Fenner } 6891ed7bf1eSGleb Smirnoff sdl = SDL(rt->rt_gateway); 690df8bae1dSRodney W. Grimes if (sdl->sdl_alen && 691322dcb8dSMax Khon bcmp(ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) { 692e3d123d6SAlfred Perlstein if (rt->rt_expire) { 693e3d123d6SAlfred Perlstein if (log_arp_movements) 6949bf40edeSBrooks Davis log(LOG_INFO, "arp: %s moved from %*D to %*D on %s\n", 695322dcb8dSMax Khon inet_ntoa(isaddr), 696322dcb8dSMax Khon ifp->if_addrlen, (u_char *)LLADDR(sdl), ":", 697322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 6989bf40edeSBrooks Davis ifp->if_xname); 699e3d123d6SAlfred Perlstein } else { 70039393906SGleb Smirnoff RT_UNLOCK(rt); 70139393906SGleb Smirnoff if (log_arp_permanent_modify) 70239393906SGleb Smirnoff log(LOG_ERR, "arp: %*D attempts to modify " 70339393906SGleb Smirnoff "permanent entry for %s on %s\n", 704322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7059bf40edeSBrooks Davis inet_ntoa(isaddr), ifp->if_xname); 706dd9b6ddeSBill Fenner goto reply; 707dd9b6ddeSBill Fenner } 708dfd5dee1SPeter Wemm } 709322dcb8dSMax Khon /* 710322dcb8dSMax Khon * sanity check for the address length. 711322dcb8dSMax Khon * XXX this does not work for protocols with variable address 712322dcb8dSMax Khon * length. -is 713322dcb8dSMax Khon */ 714322dcb8dSMax Khon if (sdl->sdl_alen && 715322dcb8dSMax Khon sdl->sdl_alen != ah->ar_hln) { 716322dcb8dSMax Khon log(LOG_WARNING, 717322dcb8dSMax Khon "arp from %*D: new addr len %d, was %d", 718322dcb8dSMax Khon ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 719322dcb8dSMax Khon ah->ar_hln, sdl->sdl_alen); 720322dcb8dSMax Khon } 721322dcb8dSMax Khon if (ifp->if_addrlen != ah->ar_hln) { 722322dcb8dSMax Khon log(LOG_WARNING, 723322dcb8dSMax Khon "arp from %*D: addr len: new %d, i/f %d (ignored)", 724322dcb8dSMax Khon ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 725322dcb8dSMax Khon ah->ar_hln, ifp->if_addrlen); 7261ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 727322dcb8dSMax Khon goto reply; 728322dcb8dSMax Khon } 729322dcb8dSMax Khon (void)memcpy(LLADDR(sdl), ar_sha(ah), 730322dcb8dSMax Khon sdl->sdl_alen = ah->ar_hln); 731243c4c6dSEivind Eklund /* 732243c4c6dSEivind Eklund * If we receive an arp from a token-ring station over 733243c4c6dSEivind Eklund * a token-ring nic then try to save the source 734243c4c6dSEivind Eklund * routing info. 735243c4c6dSEivind Eklund */ 736322dcb8dSMax Khon if (ifp->if_type == IFT_ISO88025) { 737f7a679b2SGleb Smirnoff struct iso88025_header *th = NULL; 738f7a679b2SGleb Smirnoff struct iso88025_sockaddr_dl_data *trld; 739f7a679b2SGleb Smirnoff 740fda82fc2SJulian Elischer th = (struct iso88025_header *)m->m_pkthdr.header; 74142fdfc12SKelly Yancey trld = SDL_ISO88025(sdl); 742b149dd6cSLarry Lile rif_len = TR_RCF_RIFLEN(th->rcf); 743b149dd6cSLarry Lile if ((th->iso88025_shost[0] & TR_RII) && 744b149dd6cSLarry Lile (rif_len > 2)) { 74542fdfc12SKelly Yancey trld->trld_rcf = th->rcf; 74642fdfc12SKelly Yancey trld->trld_rcf ^= htons(TR_RCF_DIR); 74742fdfc12SKelly Yancey memcpy(trld->trld_route, th->rd, rif_len - 2); 74842fdfc12SKelly Yancey trld->trld_rcf &= ~htons(TR_RCF_BCST_MASK); 749f9083fdbSLarry Lile /* 750f9083fdbSLarry Lile * Set up source routing information for 751f9083fdbSLarry Lile * reply packet (XXX) 752f9083fdbSLarry Lile */ 753b149dd6cSLarry Lile m->m_data -= rif_len; 754b149dd6cSLarry Lile m->m_len += rif_len; 755b149dd6cSLarry Lile m->m_pkthdr.len += rif_len; 756fda82fc2SJulian Elischer } else { 757b149dd6cSLarry Lile th->iso88025_shost[0] &= ~TR_RII; 758c3a2190cSKelly Yancey trld->trld_rcf = 0; 759fcf11853SLarry Lile } 760fda82fc2SJulian Elischer m->m_data -= 8; 761fda82fc2SJulian Elischer m->m_len += 8; 762fcf11853SLarry Lile m->m_pkthdr.len += 8; 76342fdfc12SKelly Yancey th->rcf = trld->trld_rcf; 764fda82fc2SJulian Elischer } 7651daaa65dSGleb Smirnoff if (rt->rt_expire) { 766fe53256dSAndre Oppermann rt->rt_expire = time_uptime + arpt_keep; 7671daaa65dSGleb Smirnoff callout_reset(&la->la_timer, hz * arpt_keep, arptimer, rt); 7681daaa65dSGleb Smirnoff } 769022695f8SOrion Hodson la->la_asked = 0; 770022695f8SOrion Hodson la->la_preempt = arp_maxtries; 7711ed7bf1eSGleb Smirnoff hold = la->la_hold; 7721ed7bf1eSGleb Smirnoff la->la_hold = NULL; 7731ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 7741ed7bf1eSGleb Smirnoff if (hold != NULL) 7751ed7bf1eSGleb Smirnoff (*ifp->if_output)(ifp, hold, rt_key(rt), rt); 7761ed7bf1eSGleb Smirnoff 777df8bae1dSRodney W. Grimes reply: 778b2a8ac7cSLuigi Rizzo if (op != ARPOP_REQUEST) 779b2a8ac7cSLuigi Rizzo goto drop; 780df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) { 781df8bae1dSRodney W. Grimes /* I am the target */ 782322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 783a9771948SGleb Smirnoff (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 784df8bae1dSRodney W. Grimes } else { 7851ed7bf1eSGleb Smirnoff rt = arplookup(itaddr.s_addr, 0, SIN_PROXY); 7861ed7bf1eSGleb Smirnoff if (rt == NULL) { 78728e82295SGarrett Wollman struct sockaddr_in sin; 78828e82295SGarrett Wollman 789b2a8ac7cSLuigi Rizzo if (!arp_proxyall) 790b2a8ac7cSLuigi Rizzo goto drop; 79128e82295SGarrett Wollman 79228e82295SGarrett Wollman bzero(&sin, sizeof sin); 79328e82295SGarrett Wollman sin.sin_family = AF_INET; 79428e82295SGarrett Wollman sin.sin_len = sizeof sin; 79528e82295SGarrett Wollman sin.sin_addr = itaddr; 79628e82295SGarrett Wollman 79731246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 798b2a8ac7cSLuigi Rizzo if (!rt) 799b2a8ac7cSLuigi Rizzo goto drop; 80028e82295SGarrett Wollman /* 80128e82295SGarrett Wollman * Don't send proxies for nodes on the same interface 80228e82295SGarrett Wollman * as this one came out of, or we'll get into a fight 80328e82295SGarrett Wollman * over who claims what Ether address. 80428e82295SGarrett Wollman */ 805322dcb8dSMax Khon if (rt->rt_ifp == ifp) { 80628e82295SGarrett Wollman rtfree(rt); 807b2a8ac7cSLuigi Rizzo goto drop; 80828e82295SGarrett Wollman } 809322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 810a9771948SGleb Smirnoff (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 81128e82295SGarrett Wollman rtfree(rt); 812cc728227SDavid Malone 813cc728227SDavid Malone /* 814cc728227SDavid Malone * Also check that the node which sent the ARP packet 815cc728227SDavid Malone * is on the the interface we expect it to be on. This 816cc728227SDavid Malone * avoids ARP chaos if an interface is connected to the 817cc728227SDavid Malone * wrong network. 818cc728227SDavid Malone */ 819cc728227SDavid Malone sin.sin_addr = isaddr; 820cc728227SDavid Malone 821cc728227SDavid Malone rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL); 822b2a8ac7cSLuigi Rizzo if (!rt) 823b2a8ac7cSLuigi Rizzo goto drop; 824322dcb8dSMax Khon if (rt->rt_ifp != ifp) { 825cc728227SDavid Malone log(LOG_INFO, "arp_proxy: ignoring request" 8269bf40edeSBrooks Davis " from %s via %s, expecting %s\n", 8279bf40edeSBrooks Davis inet_ntoa(isaddr), ifp->if_xname, 8289bf40edeSBrooks Davis rt->rt_ifp->if_xname); 829cc728227SDavid Malone rtfree(rt); 830b2a8ac7cSLuigi Rizzo goto drop; 831cc728227SDavid Malone } 832cc728227SDavid Malone rtfree(rt); 833cc728227SDavid Malone 834ac234f93SGarrett Wollman #ifdef DEBUG_PROXY 835ac234f93SGarrett Wollman printf("arp: proxying for %s\n", 836ef0cdf33SGarrett Wollman inet_ntoa(itaddr)); 837ac234f93SGarrett Wollman #endif 83828e82295SGarrett Wollman } else { 839510b360fSGleb Smirnoff /* 840510b360fSGleb Smirnoff * Return proxied ARP replies only on the interface 8415feebeebSAndrew Thompson * or bridge cluster where this network resides. 8425feebeebSAndrew Thompson * Otherwise we may conflict with the host we are 8435feebeebSAndrew Thompson * proxying for. 844510b360fSGleb Smirnoff */ 8455feebeebSAndrew Thompson if (rt->rt_ifp != ifp && 8465feebeebSAndrew Thompson (rt->rt_ifp->if_bridge != ifp->if_bridge || 8475feebeebSAndrew Thompson ifp->if_bridge == NULL)) { 848510b360fSGleb Smirnoff RT_UNLOCK(rt); 849510b360fSGleb Smirnoff goto drop; 850510b360fSGleb Smirnoff } 851df8bae1dSRodney W. Grimes sdl = SDL(rt->rt_gateway); 8521ed7bf1eSGleb Smirnoff (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 853322dcb8dSMax Khon (void)memcpy(ar_sha(ah), LLADDR(sdl), ah->ar_hln); 8541ed7bf1eSGleb Smirnoff RT_UNLOCK(rt); 85528e82295SGarrett Wollman } 856df8bae1dSRodney W. Grimes } 857df8bae1dSRodney W. Grimes 858d0558157SBruce M Simpson if (itaddr.s_addr == myaddr.s_addr && 859d0558157SBruce M Simpson IN_LINKLOCAL(ntohl(itaddr.s_addr))) { 860d0558157SBruce M Simpson /* RFC 3927 link-local IPv4; always reply by broadcast. */ 861d0558157SBruce M Simpson #ifdef DEBUG_LINKLOCAL 862d0558157SBruce M Simpson printf("arp: sending reply for link-local addr %s\n", 863d0558157SBruce M Simpson inet_ntoa(itaddr)); 864d0558157SBruce M Simpson #endif 865d0558157SBruce M Simpson m->m_flags |= M_BCAST; 866d0558157SBruce M Simpson m->m_flags &= ~M_MCAST; 867d0558157SBruce M Simpson } else { 868d0558157SBruce M Simpson /* default behaviour; never reply by broadcast. */ 869d0558157SBruce M Simpson m->m_flags &= ~(M_BCAST|M_MCAST); 870d0558157SBruce M Simpson } 871322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 872322dcb8dSMax Khon (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 873322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REPLY); 874322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 87564bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 87664bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 87764bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 87864bf80ceSMatthew N. Dodd sa.sa_len = 2; 879322dcb8dSMax Khon (*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0); 880df8bae1dSRodney W. Grimes return; 881b2a8ac7cSLuigi Rizzo 882b2a8ac7cSLuigi Rizzo drop: 883b2a8ac7cSLuigi Rizzo m_freem(m); 884df8bae1dSRodney W. Grimes } 8851d5e9e22SEivind Eklund #endif 886df8bae1dSRodney W. Grimes 887df8bae1dSRodney W. Grimes /* 888df8bae1dSRodney W. Grimes * Lookup or enter a new address in arptab. 889df8bae1dSRodney W. Grimes */ 8901ed7bf1eSGleb Smirnoff static struct rtentry * 891f2565d68SRobert Watson arplookup(u_long addr, int create, int proxy) 892df8bae1dSRodney W. Grimes { 893e952fa39SMatthew N. Dodd struct rtentry *rt; 894d1dd20beSSam Leffler struct sockaddr_inarp sin; 895ac234f93SGarrett Wollman const char *why = 0; 896df8bae1dSRodney W. Grimes 897d1dd20beSSam Leffler bzero(&sin, sizeof(sin)); 898d1dd20beSSam Leffler sin.sin_len = sizeof(sin); 899d1dd20beSSam Leffler sin.sin_family = AF_INET; 900df8bae1dSRodney W. Grimes sin.sin_addr.s_addr = addr; 901d1dd20beSSam Leffler if (proxy) 902d1dd20beSSam Leffler sin.sin_other = SIN_PROXY; 90331246bc2SGarrett Wollman rt = rtalloc1((struct sockaddr *)&sin, create, 0UL); 904df8bae1dSRodney W. Grimes if (rt == 0) 905df8bae1dSRodney W. Grimes return (0); 906ac234f93SGarrett Wollman 907ac234f93SGarrett Wollman if (rt->rt_flags & RTF_GATEWAY) 908ac234f93SGarrett Wollman why = "host is not on local network"; 909ac234f93SGarrett Wollman else if ((rt->rt_flags & RTF_LLINFO) == 0) 910ac234f93SGarrett Wollman why = "could not allocate llinfo"; 911ac234f93SGarrett Wollman else if (rt->rt_gateway->sa_family != AF_LINK) 912ac234f93SGarrett Wollman why = "gateway route is not ours"; 913ac234f93SGarrett Wollman 914fedf1d01SBruce M Simpson if (why) { 915d1dd20beSSam Leffler #define ISDYNCLONE(_rt) \ 916d1dd20beSSam Leffler (((_rt)->rt_flags & (RTF_STATIC | RTF_WASCLONED)) == RTF_WASCLONED) 917d1dd20beSSam Leffler if (create) 918ac234f93SGarrett Wollman log(LOG_DEBUG, "arplookup %s failed: %s\n", 919ef0cdf33SGarrett Wollman inet_ntoa(sin.sin_addr), why); 920b75bead1SBruce M Simpson /* 921b75bead1SBruce M Simpson * If there are no references to this Layer 2 route, 922b75bead1SBruce M Simpson * and it is a cloned route, and not static, and 923b75bead1SBruce M Simpson * arplookup() is creating the route, then purge 924b75bead1SBruce M Simpson * it from the routing table as it is probably bogus. 925b75bead1SBruce M Simpson */ 9269c63e9dbSSam Leffler if (rt->rt_refcnt == 1 && ISDYNCLONE(rt)) 9279c63e9dbSSam Leffler rtexpunge(rt); 9289c63e9dbSSam Leffler RTFREE_LOCKED(rt); 929fedf1d01SBruce M Simpson return (0); 930d1dd20beSSam Leffler #undef ISDYNCLONE 931d1dd20beSSam Leffler } else { 9327138d65cSSam Leffler RT_REMREF(rt); 9331ed7bf1eSGleb Smirnoff return (rt); 934df8bae1dSRodney W. Grimes } 935d1dd20beSSam Leffler } 936df8bae1dSRodney W. Grimes 937dd2e4102SGarrett Wollman void 938f2565d68SRobert Watson arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 939dd2e4102SGarrett Wollman { 940dd570d4dSTor Egge if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 941322dcb8dSMax Khon arprequest(ifp, &IA_SIN(ifa)->sin_addr, 942322dcb8dSMax Khon &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 943dd2e4102SGarrett Wollman ifa->ifa_rtrequest = arp_rtrequest; 944dd2e4102SGarrett Wollman ifa->ifa_flags |= RTF_CLONING; 945dd2e4102SGarrett Wollman } 946df5e1987SJonathan Lemon 947a9771948SGleb Smirnoff void 948f2565d68SRobert Watson arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 949a9771948SGleb Smirnoff { 950a9771948SGleb Smirnoff if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 951a9771948SGleb Smirnoff arprequest(ifp, &IA_SIN(ifa)->sin_addr, 952a9771948SGleb Smirnoff &IA_SIN(ifa)->sin_addr, enaddr); 953a9771948SGleb Smirnoff ifa->ifa_rtrequest = arp_rtrequest; 954a9771948SGleb Smirnoff ifa->ifa_flags |= RTF_CLONING; 955a9771948SGleb Smirnoff } 956a9771948SGleb Smirnoff 957df5e1987SJonathan Lemon static void 958df5e1987SJonathan Lemon arp_init(void) 959df5e1987SJonathan Lemon { 960df5e1987SJonathan Lemon 961df5e1987SJonathan Lemon arpintrq.ifq_maxlen = 50; 9626008862bSJohn Baldwin mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF); 9637902224cSSam Leffler netisr_register(NETISR_ARP, arpintr, &arpintrq, NETISR_MPSAFE); 964df5e1987SJonathan Lemon } 965df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 966