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" 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> 50de34ad3fSJulian Elischer #include <sys/proc.h> 514458ac71SBruce Evans #include <sys/socket.h> 52885f1aa4SPoul-Henning Kamp #include <sys/syslog.h> 53df8bae1dSRodney W. Grimes 54df8bae1dSRodney W. Grimes #include <net/if.h> 55df8bae1dSRodney W. Grimes #include <net/if_dl.h> 56722012ccSJulian Elischer #include <net/if_types.h> 57748e0b0aSGarrett Wollman #include <net/netisr.h> 58b149dd6cSLarry Lile #include <net/if_llc.h> 59c8f8e9c1SJulian Elischer #include <net/ethernet.h> 605736e6fbSBjoern A. Zeeb #include <net/route.h> 61530c0060SRobert Watson #include <net/vnet.h> 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes #include <netinet/in.h> 64df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 656e6b3f7cSQing Li #include <net/if_llatbl.h> 66df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 679963e8a5SWill Andrews #if defined(INET) || defined(INET6) 689963e8a5SWill Andrews #include <netinet/ip_carp.h> 699963e8a5SWill Andrews #endif 70df8bae1dSRodney W. Grimes 71322dcb8dSMax Khon #include <net/if_arc.h> 72fda82fc2SJulian Elischer #include <net/iso88025.h> 73fda82fc2SJulian Elischer 74aed55708SRobert Watson #include <security/mac/mac_framework.h> 75aed55708SRobert Watson 76df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s) 77df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s) 78df8bae1dSRodney W. Grimes 79ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether); 80602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 8154fc657dSGeorge V. Neville-Neil SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, ""); 82df8bae1dSRodney W. Grimes 83df8bae1dSRodney W. Grimes /* timer values */ 843e288e62SDimitry Andric static VNET_DEFINE(int, arpt_keep) = (20*60); /* once resolved, good for 20 85eddfbb76SRobert Watson * minutes */ 863e288e62SDimitry Andric static VNET_DEFINE(int, arp_maxtries) = 5; 8782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, useloopback) = 1; /* use loopback interface for 8882cea7e6SBjoern A. Zeeb * local traffic */ 893e288e62SDimitry Andric static VNET_DEFINE(int, arp_proxyall) = 0; 903e288e62SDimitry Andric static VNET_DEFINE(int, arpt_down) = 20; /* keep incomplete entries for 9193704ac5SQing Li * 20 seconds */ 92e162ea60SGeorge V. Neville-Neil VNET_DEFINE(struct arpstat, arpstat); /* ARP statistics, see if_arp.h */ 93e162ea60SGeorge V. Neville-Neil 943e288e62SDimitry Andric static VNET_DEFINE(int, arp_maxhold) = 1; 95885f1aa4SPoul-Henning Kamp 961e77c105SRobert Watson #define V_arpt_keep VNET(arpt_keep) 9793704ac5SQing Li #define V_arpt_down VNET(arpt_down) 981e77c105SRobert Watson #define V_arp_maxtries VNET(arp_maxtries) 991e77c105SRobert Watson #define V_arp_proxyall VNET(arp_proxyall) 10054fc657dSGeorge V. Neville-Neil #define V_arpstat VNET(arpstat) 101e162ea60SGeorge V. Neville-Neil #define V_arp_maxhold VNET(arp_maxhold) 102885f1aa4SPoul-Henning Kamp 103eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 104eddfbb76SRobert Watson &VNET_NAME(arpt_keep), 0, 105eddfbb76SRobert Watson "ARP entry lifetime in seconds"); 106eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 107eddfbb76SRobert Watson &VNET_NAME(arp_maxtries), 0, 1088b615593SMarko Zec "ARP resolution attempts before returning error"); 109eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 110eddfbb76SRobert Watson &VNET_NAME(useloopback), 0, 1118b615593SMarko Zec "Use the loopback interface for local traffic"); 112eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 113eddfbb76SRobert Watson &VNET_NAME(arp_proxyall), 0, 1148b615593SMarko Zec "Enable proxy ARP for all suitable requests"); 115e162ea60SGeorge V. Neville-Neil SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, wait, CTLFLAG_RW, 116e162ea60SGeorge V. Neville-Neil &VNET_NAME(arpt_down), 0, 117e162ea60SGeorge V. Neville-Neil "Incomplete ARP entry lifetime in seconds"); 11854fc657dSGeorge V. Neville-Neil SYSCTL_VNET_STRUCT(_net_link_ether_arp, OID_AUTO, stats, CTLFLAG_RW, 11954fc657dSGeorge V. Neville-Neil &VNET_NAME(arpstat), arpstat, 12054fc657dSGeorge V. Neville-Neil "ARP statistics (struct arpstat, net/if_arp.h)"); 121e162ea60SGeorge V. Neville-Neil SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, maxhold, CTLFLAG_RW, 122e162ea60SGeorge V. Neville-Neil &VNET_NAME(arp_maxhold), 0, 123e162ea60SGeorge V. Neville-Neil "Number of packets to hold per ARP entry"); 124885f1aa4SPoul-Henning Kamp 1254d77a549SAlfred Perlstein static void arp_init(void); 1266e6b3f7cSQing Li void arprequest(struct ifnet *, 1274d77a549SAlfred Perlstein struct in_addr *, struct in_addr *, u_char *); 1281cafed39SJonathan Lemon static void arpintr(struct mbuf *); 1294d77a549SAlfred Perlstein static void arptimer(void *); 1301d5e9e22SEivind Eklund #ifdef INET 1314d77a549SAlfred Perlstein static void in_arpinput(struct mbuf *); 1321d5e9e22SEivind Eklund #endif 13328e82295SGarrett Wollman 134d4b5cae4SRobert Watson static const struct netisr_handler arp_nh = { 135d4b5cae4SRobert Watson .nh_name = "arp", 136d4b5cae4SRobert Watson .nh_handler = arpintr, 137d4b5cae4SRobert Watson .nh_proto = NETISR_ARP, 138d4b5cae4SRobert Watson .nh_policy = NETISR_POLICY_SOURCE, 139d4b5cae4SRobert Watson }; 140d4b5cae4SRobert Watson 1416e6b3f7cSQing Li #ifdef AF_INET 1426e6b3f7cSQing Li void arp_ifscrub(struct ifnet *ifp, uint32_t addr); 1436e6b3f7cSQing Li 144df8bae1dSRodney W. Grimes /* 1456e6b3f7cSQing Li * called by in_ifscrub to remove entry from the table when 1466e6b3f7cSQing Li * the interface goes away 1476e6b3f7cSQing Li */ 1486e6b3f7cSQing Li void 1496e6b3f7cSQing Li arp_ifscrub(struct ifnet *ifp, uint32_t addr) 1506e6b3f7cSQing Li { 1516e6b3f7cSQing Li struct sockaddr_in addr4; 1526e6b3f7cSQing Li 1536e6b3f7cSQing Li bzero((void *)&addr4, sizeof(addr4)); 1546e6b3f7cSQing Li addr4.sin_len = sizeof(addr4); 1556e6b3f7cSQing Li addr4.sin_family = AF_INET; 1566e6b3f7cSQing Li addr4.sin_addr.s_addr = addr; 1576e6b3f7cSQing Li IF_AFDATA_LOCK(ifp); 1586e6b3f7cSQing Li lla_lookup(LLTABLE(ifp), (LLE_DELETE | LLE_IFADDR), 1596e6b3f7cSQing Li (struct sockaddr *)&addr4); 1606e6b3f7cSQing Li IF_AFDATA_UNLOCK(ifp); 1616e6b3f7cSQing Li } 1626e6b3f7cSQing Li #endif 1636e6b3f7cSQing Li 1646e6b3f7cSQing Li /* 1656e6b3f7cSQing Li * Timeout routine. Age arp_tab entries periodically. 166df8bae1dSRodney W. Grimes */ 167df8bae1dSRodney W. Grimes static void 1681daaa65dSGleb Smirnoff arptimer(void *arg) 169df8bae1dSRodney W. Grimes { 1706e6b3f7cSQing Li struct ifnet *ifp; 17119291ab3SBjoern A. Zeeb struct llentry *lle; 172e162ea60SGeorge V. Neville-Neil int pkts_dropped; 173df8bae1dSRodney W. Grimes 17419291ab3SBjoern A. Zeeb KASSERT(arg != NULL, ("%s: arg NULL", __func__)); 17519291ab3SBjoern A. Zeeb lle = (struct llentry *)arg; 1766e6b3f7cSQing Li ifp = lle->lle_tbl->llt_ifp; 17754fc657dSGeorge V. Neville-Neil CURVNET_SET(ifp->if_vnet); 1786e6b3f7cSQing Li IF_AFDATA_LOCK(ifp); 1796e6b3f7cSQing Li LLE_WLOCK(lle); 180ee8a75d3SQing Li if (lle->la_flags & LLE_STATIC) 181ee8a75d3SQing Li LLE_WUNLOCK(lle); 182ee8a75d3SQing Li else { 183ee8a75d3SQing Li if (!callout_pending(&lle->la_timer) && 184ee8a75d3SQing Li callout_active(&lle->la_timer)) { 185becba438SBjoern A. Zeeb callout_stop(&lle->la_timer); 186becba438SBjoern A. Zeeb LLE_REMREF(lle); 187e162ea60SGeorge V. Neville-Neil pkts_dropped = llentry_free(lle); 188e162ea60SGeorge V. Neville-Neil ARPSTAT_ADD(dropped, pkts_dropped); 18954fc657dSGeorge V. Neville-Neil ARPSTAT_INC(timeouts); 19033b31db6SJohn Baldwin } else { 1916cb2b4e7SQing Li #ifdef DIAGNOSTIC 192fc023235SQing Li struct sockaddr *l3addr = L3_ADDR(lle); 193ee8a75d3SQing Li log(LOG_INFO, 194ee8a75d3SQing Li "arptimer issue: %p, IPv4 address: \"%s\"\n", lle, 195ee8a75d3SQing Li inet_ntoa( 196ee8a75d3SQing Li ((const struct sockaddr_in *)l3addr)->sin_addr)); 197fc023235SQing Li #endif 19833b31db6SJohn Baldwin LLE_WUNLOCK(lle); 19933b31db6SJohn Baldwin } 200ee8a75d3SQing Li } 2016e6b3f7cSQing Li IF_AFDATA_UNLOCK(ifp); 20254fc657dSGeorge V. Neville-Neil CURVNET_RESTORE(); 203df8bae1dSRodney W. Grimes } 204df8bae1dSRodney W. Grimes 205df8bae1dSRodney W. Grimes /* 206df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies: 207df8bae1dSRodney W. Grimes * - arp header source ip address 208df8bae1dSRodney W. Grimes * - arp header target ip address 209df8bae1dSRodney W. Grimes * - arp header source ethernet address 210df8bae1dSRodney W. Grimes */ 2116e6b3f7cSQing Li void 212f2565d68SRobert Watson arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr *tip, 213f2565d68SRobert Watson u_char *enaddr) 214df8bae1dSRodney W. Grimes { 215e952fa39SMatthew N. Dodd struct mbuf *m; 216e952fa39SMatthew N. Dodd struct arphdr *ah; 217df8bae1dSRodney W. Grimes struct sockaddr sa; 218df8bae1dSRodney W. Grimes 2196e6b3f7cSQing Li if (sip == NULL) { 2206e6b3f7cSQing Li /* XXX don't believe this can happen (or explain why) */ 2216e6b3f7cSQing Li /* 2226e6b3f7cSQing Li * The caller did not supply a source address, try to find 2236e6b3f7cSQing Li * a compatible one among those assigned to this interface. 2246e6b3f7cSQing Li */ 2256e6b3f7cSQing Li struct ifaddr *ifa; 2266e6b3f7cSQing Li 2276e6b3f7cSQing Li TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2286e6b3f7cSQing Li if (!ifa->ifa_addr || 2296e6b3f7cSQing Li ifa->ifa_addr->sa_family != AF_INET) 2306e6b3f7cSQing Li continue; 2316e6b3f7cSQing Li sip = &SIN(ifa->ifa_addr)->sin_addr; 2326e6b3f7cSQing Li if (0 == ((sip->s_addr ^ tip->s_addr) & 2336e6b3f7cSQing Li SIN(ifa->ifa_netmask)->sin_addr.s_addr) ) 2346e6b3f7cSQing Li break; /* found it. */ 2356e6b3f7cSQing Li } 2366e6b3f7cSQing Li if (sip == NULL) { 2376e6b3f7cSQing Li printf("%s: cannot find matching address\n", __func__); 2386e6b3f7cSQing Li return; 2396e6b3f7cSQing Li } 2406e6b3f7cSQing Li } 2416e6b3f7cSQing Li 242a163d034SWarner Losh if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) 243df8bae1dSRodney W. Grimes return; 24464bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 24564bf80ceSMatthew N. Dodd 2*ifp->if_data.ifi_addrlen; 24664bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 24764bf80ceSMatthew N. Dodd MH_ALIGN(m, m->m_len); 24864bf80ceSMatthew N. Dodd ah = mtod(m, struct arphdr *); 24964bf80ceSMatthew N. Dodd bzero((caddr_t)ah, m->m_len); 25019527d3eSRobert Watson #ifdef MAC 251b9b0dac3SRobert Watson mac_netinet_arp_send(ifp, m); 25219527d3eSRobert Watson #endif 253322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); 254322dcb8dSMax Khon ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 255322dcb8dSMax Khon ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 256322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REQUEST); 25764bf80ceSMatthew N. Dodd bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln); 25864bf80ceSMatthew N. Dodd bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln); 25964bf80ceSMatthew N. Dodd bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln); 26064bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 26164bf80ceSMatthew N. Dodd sa.sa_len = 2; 26264bf80ceSMatthew N. Dodd m->m_flags |= M_BCAST; 263279aa3d4SKip Macy (*ifp->if_output)(ifp, m, &sa, NULL); 26454fc657dSGeorge V. Neville-Neil ARPSTAT_INC(txrequests); 265df8bae1dSRodney W. Grimes } 266df8bae1dSRodney W. Grimes 267df8bae1dSRodney W. Grimes /* 268cd46a114SLuigi Rizzo * Resolve an IP address into an ethernet address. 269cd46a114SLuigi Rizzo * On input: 270cd46a114SLuigi Rizzo * ifp is the interface we use 271cd46a114SLuigi Rizzo * rt0 is the route to the final destination (possibly useless) 272b6ae6984SJulian Elischer * m is the mbuf. May be NULL if we don't have a packet. 273b6ae6984SJulian Elischer * dst is the next hop, 274cd46a114SLuigi Rizzo * desten is where we want the address. 275cd46a114SLuigi Rizzo * 276cd46a114SLuigi Rizzo * On success, desten is filled in and the function returns 0; 277cd46a114SLuigi Rizzo * If the packet must be held pending resolution, we return EWOULDBLOCK 278cd46a114SLuigi Rizzo * On other errors, we return the corresponding error code. 279b6ae6984SJulian Elischer * Note that m_freem() handles NULL. 280df8bae1dSRodney W. Grimes */ 281df8bae1dSRodney W. Grimes int 282cd46a114SLuigi Rizzo arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 2836e6b3f7cSQing Li struct sockaddr *dst, u_char *desten, struct llentry **lle) 284df8bae1dSRodney W. Grimes { 2856e6b3f7cSQing Li struct llentry *la = 0; 28600a46b31SKip Macy u_int flags = 0; 287e162ea60SGeorge V. Neville-Neil struct mbuf *curr = NULL; 288e162ea60SGeorge V. Neville-Neil struct mbuf *next = NULL; 2896e6b3f7cSQing Li int error, renew; 290df8bae1dSRodney W. Grimes 2916e6b3f7cSQing Li *lle = NULL; 2926e6b3f7cSQing Li if (m != NULL) { 293b6ae6984SJulian Elischer if (m->m_flags & M_BCAST) { 294b6ae6984SJulian Elischer /* broadcast */ 295b6ae6984SJulian Elischer (void)memcpy(desten, 296b6ae6984SJulian Elischer ifp->if_broadcastaddr, ifp->if_addrlen); 297cd46a114SLuigi Rizzo return (0); 298df8bae1dSRodney W. Grimes } 299b6ae6984SJulian Elischer if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) { 300b6ae6984SJulian Elischer /* multicast */ 301df8bae1dSRodney W. Grimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 302cd46a114SLuigi Rizzo return (0); 303df8bae1dSRodney W. Grimes } 304b6ae6984SJulian Elischer } 3056e6b3f7cSQing Li retry: 30600a46b31SKip Macy IF_AFDATA_RLOCK(ifp); 3076e6b3f7cSQing Li la = lla_lookup(LLTABLE(ifp), flags, dst); 30800a46b31SKip Macy IF_AFDATA_RUNLOCK(ifp); 30900a46b31SKip Macy if ((la == NULL) && ((flags & LLE_EXCLUSIVE) == 0) 31000a46b31SKip Macy && ((ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0)) { 31100a46b31SKip Macy flags |= (LLE_CREATE | LLE_EXCLUSIVE); 31200a46b31SKip Macy IF_AFDATA_WLOCK(ifp); 31300a46b31SKip Macy la = lla_lookup(LLTABLE(ifp), flags, dst); 31400a46b31SKip Macy IF_AFDATA_WUNLOCK(ifp); 31500a46b31SKip Macy } 3161ed7bf1eSGleb Smirnoff if (la == NULL) { 3176e6b3f7cSQing Li if (flags & LLE_CREATE) 3181ed7bf1eSGleb Smirnoff log(LOG_DEBUG, 3191ed7bf1eSGleb Smirnoff "arpresolve: can't allocate llinfo for %s\n", 3201ed7bf1eSGleb Smirnoff inet_ntoa(SIN(dst)->sin_addr)); 3211ed7bf1eSGleb Smirnoff m_freem(m); 3226e6b3f7cSQing Li return (EINVAL); 3231ed7bf1eSGleb Smirnoff } 324a20e2538SGleb Smirnoff 3256e6b3f7cSQing Li if ((la->la_flags & LLE_VALID) && 326a98c06f1SGleb Smirnoff ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) { 3276e6b3f7cSQing Li bcopy(&la->ll_addr, desten, ifp->if_addrlen); 328f0f3379eSOrion Hodson /* 329f0f3379eSOrion Hodson * If entry has an expiry time and it is approaching, 3306e6b3f7cSQing Li * see if we need to send an ARP request within this 3316e6b3f7cSQing Li * arpt_down interval. 332f0f3379eSOrion Hodson */ 3336e6b3f7cSQing Li if (!(la->la_flags & LLE_STATIC) && 334a98c06f1SGleb Smirnoff time_uptime + la->la_preempt > la->la_expire) { 3356e6b3f7cSQing Li arprequest(ifp, NULL, 3366e6b3f7cSQing Li &SIN(dst)->sin_addr, IF_LLADDR(ifp)); 337a20e2538SGleb Smirnoff 338022695f8SOrion Hodson la->la_preempt--; 339f0f3379eSOrion Hodson } 340f0f3379eSOrion Hodson 3416e6b3f7cSQing Li *lle = la; 3426e6b3f7cSQing Li error = 0; 3436e6b3f7cSQing Li goto done; 344df8bae1dSRodney W. Grimes } 3456e6b3f7cSQing Li 3466e6b3f7cSQing Li if (la->la_flags & LLE_STATIC) { /* should not happen! */ 3476e6b3f7cSQing Li log(LOG_DEBUG, "arpresolve: ouch, empty static llinfo for %s\n", 3486e6b3f7cSQing Li inet_ntoa(SIN(dst)->sin_addr)); 34947891de1SRuslan Ermilov m_freem(m); 3506e6b3f7cSQing Li error = EINVAL; 3516e6b3f7cSQing Li goto done; 3526e6b3f7cSQing Li } 3536e6b3f7cSQing Li 354a98c06f1SGleb Smirnoff renew = (la->la_asked == 0 || la->la_expire != time_uptime); 3556e6b3f7cSQing Li if ((renew || m != NULL) && (flags & LLE_EXCLUSIVE) == 0) { 3566e6b3f7cSQing Li flags |= LLE_EXCLUSIVE; 3576e6b3f7cSQing Li LLE_RUNLOCK(la); 3586e6b3f7cSQing Li goto retry; 35947891de1SRuslan Ermilov } 36008aadfbbSJonathan Lemon /* 361df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address 362e162ea60SGeorge V. Neville-Neil * response yet. Add the mbuf to the list, dropping 363e162ea60SGeorge V. Neville-Neil * the oldest packet if we have exceeded the system 364e162ea60SGeorge V. Neville-Neil * setting. 365df8bae1dSRodney W. Grimes */ 3666e6b3f7cSQing Li if (m != NULL) { 367e162ea60SGeorge V. Neville-Neil if (la->la_numheld >= V_arp_maxhold) { 36854fc657dSGeorge V. Neville-Neil if (la->la_hold != NULL) { 369e162ea60SGeorge V. Neville-Neil next = la->la_hold->m_nextpkt; 370df8bae1dSRodney W. Grimes m_freem(la->la_hold); 371e162ea60SGeorge V. Neville-Neil la->la_hold = next; 372e162ea60SGeorge V. Neville-Neil la->la_numheld--; 37354fc657dSGeorge V. Neville-Neil ARPSTAT_INC(dropped); 37454fc657dSGeorge V. Neville-Neil } 375e162ea60SGeorge V. Neville-Neil } 376e162ea60SGeorge V. Neville-Neil if (la->la_hold != NULL) { 377e162ea60SGeorge V. Neville-Neil curr = la->la_hold; 378e162ea60SGeorge V. Neville-Neil while (curr->m_nextpkt != NULL) 379e162ea60SGeorge V. Neville-Neil curr = curr->m_nextpkt; 380e162ea60SGeorge V. Neville-Neil curr->m_nextpkt = m; 381e162ea60SGeorge V. Neville-Neil } else 382df8bae1dSRodney W. Grimes la->la_hold = m; 383e162ea60SGeorge V. Neville-Neil la->la_numheld++; 3846e6b3f7cSQing Li if (renew == 0 && (flags & LLE_EXCLUSIVE)) { 3856e6b3f7cSQing Li flags &= ~LLE_EXCLUSIVE; 3866e6b3f7cSQing Li LLE_DOWNGRADE(la); 38758505389SKip Macy } 388e1ff74c5SGleb Smirnoff 3896e6b3f7cSQing Li } 390e1ff74c5SGleb Smirnoff /* 391e1ff74c5SGleb Smirnoff * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 392e1ff74c5SGleb Smirnoff * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 393e1ff74c5SGleb Smirnoff * if we have already sent arp_maxtries ARP requests. Retransmit the 394e1ff74c5SGleb Smirnoff * ARP request, but not faster than one request per second. 395e1ff74c5SGleb Smirnoff */ 396603724d3SBjoern A. Zeeb if (la->la_asked < V_arp_maxtries) 397e1ff74c5SGleb Smirnoff error = EWOULDBLOCK; /* First request. */ 398e1ff74c5SGleb Smirnoff else 39956714599SNavdeep Parhar error = rt0 != NULL && (rt0->rt_flags & RTF_GATEWAY) ? 40056714599SNavdeep Parhar EHOSTUNREACH : EHOSTDOWN; 401e1ff74c5SGleb Smirnoff 4026e6b3f7cSQing Li if (renew) { 403becba438SBjoern A. Zeeb int canceled; 404becba438SBjoern A. Zeeb 4056e6b3f7cSQing Li LLE_ADDREF(la); 406a98c06f1SGleb Smirnoff la->la_expire = time_uptime; 407becba438SBjoern A. Zeeb canceled = callout_reset(&la->la_timer, hz * V_arpt_down, 408becba438SBjoern A. Zeeb arptimer, la); 409becba438SBjoern A. Zeeb if (canceled) 410becba438SBjoern A. Zeeb LLE_REMREF(la); 41195ebcabeSMaxim Konovalov la->la_asked++; 4126e6b3f7cSQing Li LLE_WUNLOCK(la); 4136e6b3f7cSQing Li arprequest(ifp, NULL, &SIN(dst)->sin_addr, 414322dcb8dSMax Khon IF_LLADDR(ifp)); 4156e6b3f7cSQing Li return (error); 4166e6b3f7cSQing Li } 4176e6b3f7cSQing Li done: 4186e6b3f7cSQing Li if (flags & LLE_EXCLUSIVE) 4196e6b3f7cSQing Li LLE_WUNLOCK(la); 4206e6b3f7cSQing Li else 4216e6b3f7cSQing Li LLE_RUNLOCK(la); 422e1ff74c5SGleb Smirnoff return (error); 423df8bae1dSRodney W. Grimes } 424df8bae1dSRodney W. Grimes 425df8bae1dSRodney W. Grimes /* 426df8bae1dSRodney W. Grimes * Common length and type checks are done here, 427df8bae1dSRodney W. Grimes * then the protocol-specific routine is called. 428df8bae1dSRodney W. Grimes */ 429885f1aa4SPoul-Henning Kamp static void 4301cafed39SJonathan Lemon arpintr(struct mbuf *m) 431df8bae1dSRodney W. Grimes { 4321cafed39SJonathan Lemon struct arphdr *ar; 433df8bae1dSRodney W. Grimes 43476ec7b2fSRobert Watson if (m->m_len < sizeof(struct arphdr) && 43584365e2bSMatthew Dillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 436e44d6283SJoerg Wunsch log(LOG_ERR, "arp: runt packet -- m_pullup failed\n"); 4371cafed39SJonathan Lemon return; 43876ec7b2fSRobert Watson } 43976ec7b2fSRobert Watson ar = mtod(m, struct arphdr *); 44076ec7b2fSRobert Watson 4411cafed39SJonathan Lemon if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 4421cafed39SJonathan Lemon ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 443b8b33234SDoug Rabson ntohs(ar->ar_hrd) != ARPHRD_ARCNET && 444b8b33234SDoug Rabson ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) { 4451cafed39SJonathan Lemon log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n", 44676ec7b2fSRobert Watson (unsigned char *)&ar->ar_hrd, ""); 44776ec7b2fSRobert Watson m_freem(m); 4481cafed39SJonathan Lemon return; 44976ec7b2fSRobert Watson } 45076ec7b2fSRobert Watson 45131175791SRuslan Ermilov if (m->m_len < arphdr_len(ar)) { 45278e2d2bdSRuslan Ermilov if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 453f72d9d83SJoerg Wunsch log(LOG_ERR, "arp: runt packet\n"); 45476ec7b2fSRobert Watson m_freem(m); 4551cafed39SJonathan Lemon return; 45676ec7b2fSRobert Watson } 45778e2d2bdSRuslan Ermilov ar = mtod(m, struct arphdr *); 45878e2d2bdSRuslan Ermilov } 459df8bae1dSRodney W. Grimes 46054fc657dSGeorge V. Neville-Neil ARPSTAT_INC(received); 461df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) { 4621d5e9e22SEivind Eklund #ifdef INET 463df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 464df8bae1dSRodney W. Grimes in_arpinput(m); 4651cafed39SJonathan Lemon return; 4661d5e9e22SEivind Eklund #endif 467df8bae1dSRodney W. Grimes } 468df8bae1dSRodney W. Grimes m_freem(m); 469df8bae1dSRodney W. Grimes } 470df8bae1dSRodney W. Grimes 4711d5e9e22SEivind Eklund #ifdef INET 472df8bae1dSRodney W. Grimes /* 473df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet. 474df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826. 475df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender 476df8bae1dSRodney W. Grimes * protocol address, to catch impersonators. 477df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol: 478df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 479df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us, 480df8bae1dSRodney W. Grimes * and also sent them in response to IP replies. 481df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive 482df8bae1dSRodney W. Grimes * trailer packets. 483df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 484df8bae1dSRodney W. Grimes * but formerly didn't normally send requests. 485df8bae1dSRodney W. Grimes */ 4863269187dSAlfred Perlstein static int log_arp_wrong_iface = 1; 487e3d123d6SAlfred Perlstein static int log_arp_movements = 1; 48839393906SGleb Smirnoff static int log_arp_permanent_modify = 1; 4893269187dSAlfred Perlstein 4903269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 4913269187dSAlfred Perlstein &log_arp_wrong_iface, 0, 4923269187dSAlfred Perlstein "log arp packets arriving on the wrong interface"); 493e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 494e3d123d6SAlfred Perlstein &log_arp_movements, 0, 49575ce3221SAlfred Perlstein "log arp replies from MACs different than the one in the cache"); 49639393906SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, 49739393906SGleb Smirnoff &log_arp_permanent_modify, 0, 49839393906SGleb Smirnoff "log arp replies from MACs different than the one in the permanent arp entry"); 499e3d123d6SAlfred Perlstein 5003269187dSAlfred Perlstein 501df8bae1dSRodney W. Grimes static void 502f2565d68SRobert Watson in_arpinput(struct mbuf *m) 503df8bae1dSRodney W. Grimes { 504e952fa39SMatthew N. Dodd struct arphdr *ah; 505e952fa39SMatthew N. Dodd struct ifnet *ifp = m->m_pkthdr.rcvif; 5066e6b3f7cSQing Li struct llentry *la = NULL; 507e952fa39SMatthew N. Dodd struct rtentry *rt; 508ca925d9cSJonathan Lemon struct ifaddr *ifa; 509ca925d9cSJonathan Lemon struct in_ifaddr *ia; 510df8bae1dSRodney W. Grimes struct sockaddr sa; 511df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr; 512a9771948SGleb Smirnoff u_int8_t *enaddr = NULL; 5136e6b3f7cSQing Li int op, flags; 514322dcb8dSMax Khon int req_len; 51580b11ee4SPhilip Paeps int bridged = 0, is_bridge = 0; 5162ef4a436SGleb Smirnoff int carp_match = 0; 5178e7e854cSKip Macy struct sockaddr_in sin; 5188e7e854cSKip Macy sin.sin_len = sizeof(struct sockaddr_in); 5198e7e854cSKip Macy sin.sin_family = AF_INET; 52029910a5aSKip Macy sin.sin_addr.s_addr = 0; 521df8bae1dSRodney W. Grimes 52274948aa6SAndrew Thompson if (ifp->if_bridge) 5238f867517SAndrew Thompson bridged = 1; 52480b11ee4SPhilip Paeps if (ifp->if_type == IFT_BRIDGE) 52580b11ee4SPhilip Paeps is_bridge = 1; 5268f867517SAndrew Thompson 527322dcb8dSMax Khon req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 528322dcb8dSMax Khon if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 5294cbc8ad1SYaroslav Tykhiy log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n"); 5304cbc8ad1SYaroslav Tykhiy return; 5314cbc8ad1SYaroslav Tykhiy } 5324cbc8ad1SYaroslav Tykhiy 533322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 53409d3f895SGeorge V. Neville-Neil /* 53509d3f895SGeorge V. Neville-Neil * ARP is only for IPv4 so we can reject packets with 53609d3f895SGeorge V. Neville-Neil * a protocol length not equal to an IPv4 address. 53709d3f895SGeorge V. Neville-Neil */ 53809d3f895SGeorge V. Neville-Neil if (ah->ar_pln != sizeof(struct in_addr)) { 5399844b029SChristian S.J. Peron log(LOG_ERR, "in_arp: requested protocol length != %zu\n", 54009d3f895SGeorge V. Neville-Neil sizeof(struct in_addr)); 54109d3f895SGeorge V. Neville-Neil return; 54209d3f895SGeorge V. Neville-Neil } 54309d3f895SGeorge V. Neville-Neil 54409d3f895SGeorge V. Neville-Neil if (ETHER_IS_MULTICAST(ar_sha(ah))) { 54509d3f895SGeorge V. Neville-Neil log(LOG_ERR, "in_arp: source hardware address is multicast."); 54609d3f895SGeorge V. Neville-Neil return; 54709d3f895SGeorge V. Neville-Neil } 54809d3f895SGeorge V. Neville-Neil 549322dcb8dSMax Khon op = ntohs(ah->ar_op); 550322dcb8dSMax Khon (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 551322dcb8dSMax Khon (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 55232439868SGleb Smirnoff 55354fc657dSGeorge V. Neville-Neil if (op == ARPOP_REPLY) 55454fc657dSGeorge V. Neville-Neil ARPSTAT_INC(rxreplies); 55554fc657dSGeorge V. Neville-Neil 556ca925d9cSJonathan Lemon /* 557ca925d9cSJonathan Lemon * For a bridge, we want to check the address irrespective 558ca925d9cSJonathan Lemon * of the receive interface. (This will change slightly 559ca925d9cSJonathan Lemon * when we have clusters of interfaces). 560a9771948SGleb Smirnoff * If the interface does not match, but the recieving interface 561a9771948SGleb Smirnoff * is part of carp, we call carp_iamatch to see if this is a 562a9771948SGleb Smirnoff * request for the virtual host ip. 563a9771948SGleb Smirnoff * XXX: This is really ugly! 564ca925d9cSJonathan Lemon */ 5652d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 5662ef4a436SGleb Smirnoff LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 56796561547SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) || 5686e6b3f7cSQing Li ia->ia_ifp == ifp) && 56909d54778SRobert Watson itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 57009d54778SRobert Watson ifa_ref(&ia->ia_ifa); 5712d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 572ca925d9cSJonathan Lemon goto match; 57309d54778SRobert Watson } 5742ef4a436SGleb Smirnoff if (ifp->if_carp != NULL && 57554bfbd51SWill Andrews (*carp_iamatch_p)(ifp, ia, &isaddr, &enaddr) && 5762ef4a436SGleb Smirnoff itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 5772ef4a436SGleb Smirnoff carp_match = 1; 57809d54778SRobert Watson ifa_ref(&ia->ia_ifa); 5792d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 5802ef4a436SGleb Smirnoff goto match; 5812ef4a436SGleb Smirnoff } 5822ef4a436SGleb Smirnoff } 583ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 58496561547SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) || 5856e6b3f7cSQing Li ia->ia_ifp == ifp) && 58609d54778SRobert Watson isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 58709d54778SRobert Watson ifa_ref(&ia->ia_ifa); 5882d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 589ca925d9cSJonathan Lemon goto match; 59009d54778SRobert Watson } 59180b11ee4SPhilip Paeps 59280b11ee4SPhilip Paeps #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \ 59380b11ee4SPhilip Paeps (ia->ia_ifp->if_bridge == ifp->if_softc && \ 59480b11ee4SPhilip Paeps !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \ 59580b11ee4SPhilip Paeps addr == ia->ia_addr.sin_addr.s_addr) 59680b11ee4SPhilip Paeps /* 59780b11ee4SPhilip Paeps * Check the case when bridge shares its MAC address with 59880b11ee4SPhilip Paeps * some of its children, so packets are claimed by bridge 59980b11ee4SPhilip Paeps * itself (bridge_input() does it first), but they are really 60080b11ee4SPhilip Paeps * meant to be destined to the bridge member. 60180b11ee4SPhilip Paeps */ 60280b11ee4SPhilip Paeps if (is_bridge) { 60380b11ee4SPhilip Paeps LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 60480b11ee4SPhilip Paeps if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) { 60509d54778SRobert Watson ifa_ref(&ia->ia_ifa); 60680b11ee4SPhilip Paeps ifp = ia->ia_ifp; 6072d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 60880b11ee4SPhilip Paeps goto match; 60980b11ee4SPhilip Paeps } 61080b11ee4SPhilip Paeps } 61180b11ee4SPhilip Paeps } 61280b11ee4SPhilip Paeps #undef BDG_MEMBER_MATCHES_ARP 6132d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 61480b11ee4SPhilip Paeps 615ca925d9cSJonathan Lemon /* 616d8b84d9eSJonathan Lemon * No match, use the first inet address on the receive interface 617ca925d9cSJonathan Lemon * as a dummy address for the rest of the function. 618ca925d9cSJonathan Lemon */ 61909d54778SRobert Watson IF_ADDR_LOCK(ifp); 620d8b84d9eSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 6214b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_INET) { 622ec691a10SJonathan Lemon ia = ifatoia(ifa); 62309d54778SRobert Watson ifa_ref(ifa); 624f8574c7aSRobert Watson IF_ADDR_UNLOCK(ifp); 625ec691a10SJonathan Lemon goto match; 626ec691a10SJonathan Lemon } 62709d54778SRobert Watson IF_ADDR_UNLOCK(ifp); 62809d54778SRobert Watson 629ec691a10SJonathan Lemon /* 630ec691a10SJonathan Lemon * If bridging, fall back to using any inet address. 631ec691a10SJonathan Lemon */ 6322d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 6332d9cfabaSRobert Watson if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) { 6342d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 635b2a8ac7cSLuigi Rizzo goto drop; 6362d9cfabaSRobert Watson } 63709d54778SRobert Watson ifa_ref(&ia->ia_ifa); 6382d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 639ca925d9cSJonathan Lemon match: 640a9771948SGleb Smirnoff if (!enaddr) 641a9771948SGleb Smirnoff enaddr = (u_int8_t *)IF_LLADDR(ifp); 642ca925d9cSJonathan Lemon myaddr = ia->ia_addr.sin_addr; 64309d54778SRobert Watson ifa_free(&ia->ia_ifa); 644a9771948SGleb Smirnoff if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 645b2a8ac7cSLuigi Rizzo goto drop; /* it's from me, ignore it. */ 646322dcb8dSMax Khon if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 647df8bae1dSRodney W. Grimes log(LOG_ERR, 648322dcb8dSMax Khon "arp: link address is broadcast for IP address %s!\n", 649ef0cdf33SGarrett Wollman inet_ntoa(isaddr)); 650b2a8ac7cSLuigi Rizzo goto drop; 651df8bae1dSRodney W. Grimes } 65200fcf9d1SRobert Watson /* 65300fcf9d1SRobert Watson * Warn if another host is using the same IP address, but only if the 65400fcf9d1SRobert Watson * IP address isn't 0.0.0.0, which is used for DHCP only, in which 65500fcf9d1SRobert Watson * case we suppress the warning to avoid false positive complaints of 65600fcf9d1SRobert Watson * potential misconfiguration. 65700fcf9d1SRobert Watson */ 658f69453caSAndrew Thompson if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) { 659df8bae1dSRodney W. Grimes log(LOG_ERR, 6603affb6fbSYaroslav Tykhiy "arp: %*D is using my IP address %s on %s!\n", 661322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 6623affb6fbSYaroslav Tykhiy inet_ntoa(isaddr), ifp->if_xname); 663df8bae1dSRodney W. Grimes itaddr = myaddr; 66454fc657dSGeorge V. Neville-Neil ARPSTAT_INC(dupips); 665df8bae1dSRodney W. Grimes goto reply; 666df8bae1dSRodney W. Grimes } 667deb62e28SRuslan Ermilov if (ifp->if_flags & IFF_STATICARP) 668deb62e28SRuslan Ermilov goto reply; 6698b07e49aSJulian Elischer 6706e6b3f7cSQing Li bzero(&sin, sizeof(sin)); 6716e6b3f7cSQing Li sin.sin_len = sizeof(struct sockaddr_in); 6726e6b3f7cSQing Li sin.sin_family = AF_INET; 6736e6b3f7cSQing Li sin.sin_addr = isaddr; 6746e6b3f7cSQing Li flags = (itaddr.s_addr == myaddr.s_addr) ? LLE_CREATE : 0; 6756e6b3f7cSQing Li flags |= LLE_EXCLUSIVE; 6766e6b3f7cSQing Li IF_AFDATA_LOCK(ifp); 6776e6b3f7cSQing Li la = lla_lookup(LLTABLE(ifp), flags, (struct sockaddr *)&sin); 6786e6b3f7cSQing Li IF_AFDATA_UNLOCK(ifp); 6796e6b3f7cSQing Li if (la != NULL) { 6806e6b3f7cSQing Li /* the following is not an error when doing bridging */ 68154bfbd51SWill Andrews if (!bridged && la->lle_tbl->llt_ifp != ifp && !carp_match) { 6823269187dSAlfred Perlstein if (log_arp_wrong_iface) 6838b07e49aSJulian Elischer log(LOG_ERR, "arp: %s is on %s " 6846e6b3f7cSQing Li "but got reply from %*D on %s\n", 685dd9b6ddeSBill Fenner inet_ntoa(isaddr), 6866e6b3f7cSQing Li la->lle_tbl->llt_ifp->if_xname, 6876e6b3f7cSQing Li ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 6889bf40edeSBrooks Davis ifp->if_xname); 689cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 6906e6b3f7cSQing Li goto reply; 691dd9b6ddeSBill Fenner } 6926e6b3f7cSQing Li if ((la->la_flags & LLE_VALID) && 6936e6b3f7cSQing Li bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) { 6946e6b3f7cSQing Li if (la->la_flags & LLE_STATIC) { 695cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 6968b07e49aSJulian Elischer log(LOG_ERR, 6976e6b3f7cSQing Li "arp: %*D attempts to modify permanent " 6986e6b3f7cSQing Li "entry for %s on %s\n", 6996e6b3f7cSQing Li ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7006e6b3f7cSQing Li inet_ntoa(isaddr), ifp->if_xname); 7016e6b3f7cSQing Li goto reply; 7026e6b3f7cSQing Li } 7036e6b3f7cSQing Li if (log_arp_movements) { 7046e6b3f7cSQing Li log(LOG_INFO, "arp: %s moved from %*D " 7056e6b3f7cSQing Li "to %*D on %s\n", 7068b07e49aSJulian Elischer inet_ntoa(isaddr), 7076e6b3f7cSQing Li ifp->if_addrlen, 7086e6b3f7cSQing Li (u_char *)&la->ll_addr, ":", 7096e6b3f7cSQing Li ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7108b07e49aSJulian Elischer ifp->if_xname); 711dd9b6ddeSBill Fenner } 712dfd5dee1SPeter Wemm } 7136e6b3f7cSQing Li 714322dcb8dSMax Khon if (ifp->if_addrlen != ah->ar_hln) { 715cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 716322dcb8dSMax Khon log(LOG_WARNING, 7176e6b3f7cSQing Li "arp from %*D: addr len: new %d, i/f %d (ignored)", 7186e6b3f7cSQing Li ifp->if_addrlen, (u_char *) ar_sha(ah), ":", 7196e6b3f7cSQing Li ah->ar_hln, ifp->if_addrlen); 72009d3f895SGeorge V. Neville-Neil goto drop; 721322dcb8dSMax Khon } 7226e6b3f7cSQing Li (void)memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen); 7236e6b3f7cSQing Li la->la_flags |= LLE_VALID; 7248b07e49aSJulian Elischer 7259a311445SNavdeep Parhar EVENTHANDLER_INVOKE(arp_update_event, la); 7269a311445SNavdeep Parhar 7276e6b3f7cSQing Li if (!(la->la_flags & LLE_STATIC)) { 728becba438SBjoern A. Zeeb int canceled; 729becba438SBjoern A. Zeeb 730becba438SBjoern A. Zeeb LLE_ADDREF(la); 731a98c06f1SGleb Smirnoff la->la_expire = time_uptime + V_arpt_keep; 732becba438SBjoern A. Zeeb canceled = callout_reset(&la->la_timer, 733becba438SBjoern A. Zeeb hz * V_arpt_keep, arptimer, la); 734becba438SBjoern A. Zeeb if (canceled) 735becba438SBjoern A. Zeeb LLE_REMREF(la); 7361daaa65dSGleb Smirnoff } 737022695f8SOrion Hodson la->la_asked = 0; 738603724d3SBjoern A. Zeeb la->la_preempt = V_arp_maxtries; 739e162ea60SGeorge V. Neville-Neil /* 740e162ea60SGeorge V. Neville-Neil * The packets are all freed within the call to the output 741e162ea60SGeorge V. Neville-Neil * routine. 742e162ea60SGeorge V. Neville-Neil * 743e162ea60SGeorge V. Neville-Neil * NB: The lock MUST be released before the call to the 744e162ea60SGeorge V. Neville-Neil * output routine. 745e162ea60SGeorge V. Neville-Neil */ 746e162ea60SGeorge V. Neville-Neil if (la->la_hold != NULL) { 747e162ea60SGeorge V. Neville-Neil struct mbuf *m_hold, *m_hold_next; 748e162ea60SGeorge V. Neville-Neil 74990fdff07SGeorge V. Neville-Neil m_hold = la->la_hold; 75090fdff07SGeorge V. Neville-Neil la->la_hold = NULL; 75190fdff07SGeorge V. Neville-Neil la->la_numheld = 0; 7526e6b3f7cSQing Li memcpy(&sa, L3_ADDR(la), sizeof(sa)); 753cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 754ede99017SGeorge V. Neville-Neil for (; m_hold != NULL; m_hold = m_hold_next) { 755e162ea60SGeorge V. Neville-Neil m_hold_next = m_hold->m_nextpkt; 756e162ea60SGeorge V. Neville-Neil m_hold->m_nextpkt = NULL; 757e162ea60SGeorge V. Neville-Neil (*ifp->if_output)(ifp, m_hold, &sa, NULL); 7586e6b3f7cSQing Li } 759e162ea60SGeorge V. Neville-Neil } else 760e162ea60SGeorge V. Neville-Neil LLE_WUNLOCK(la); 761e162ea60SGeorge V. Neville-Neil } /* end of FIB loop */ 7626e6b3f7cSQing Li reply: 763b2a8ac7cSLuigi Rizzo if (op != ARPOP_REQUEST) 764b2a8ac7cSLuigi Rizzo goto drop; 76554fc657dSGeorge V. Neville-Neil ARPSTAT_INC(rxrequests); 7666e6b3f7cSQing Li 767df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) { 7688b07e49aSJulian Elischer /* Shortcut.. the receiving interface is the target. */ 769322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 770a9771948SGleb Smirnoff (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 771df8bae1dSRodney W. Grimes } else { 772897d75c9SQing Li struct llentry *lle = NULL; 773897d75c9SQing Li 774cd29a779SQing Li sin.sin_addr = itaddr; 775cd29a779SQing Li IF_AFDATA_LOCK(ifp); 776cd29a779SQing Li lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin); 777cd29a779SQing Li IF_AFDATA_UNLOCK(ifp); 778cd29a779SQing Li 779cd29a779SQing Li if ((lle != NULL) && (lle->la_flags & LLE_PUB)) { 780cd29a779SQing Li (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 781cd29a779SQing Li (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln); 782cd29a779SQing Li LLE_RUNLOCK(lle); 783cd29a779SQing Li } else { 784cd29a779SQing Li 785cd29a779SQing Li if (lle != NULL) 786cd29a779SQing Li LLE_RUNLOCK(lle); 787cd29a779SQing Li 788603724d3SBjoern A. Zeeb if (!V_arp_proxyall) 789b2a8ac7cSLuigi Rizzo goto drop; 79028e82295SGarrett Wollman 79128e82295SGarrett Wollman sin.sin_addr = itaddr; 7928b07e49aSJulian Elischer /* XXX MRT use table 0 for arp reply */ 7938b07e49aSJulian Elischer rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 794b2a8ac7cSLuigi Rizzo if (!rt) 795b2a8ac7cSLuigi Rizzo goto drop; 796897d75c9SQing Li 79728e82295SGarrett Wollman /* 79828e82295SGarrett Wollman * Don't send proxies for nodes on the same interface 79928e82295SGarrett Wollman * as this one came out of, or we'll get into a fight 80028e82295SGarrett Wollman * over who claims what Ether address. 80128e82295SGarrett Wollman */ 802897d75c9SQing Li if (!rt->rt_ifp || rt->rt_ifp == ifp) { 8034e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 804b2a8ac7cSLuigi Rizzo goto drop; 80528e82295SGarrett Wollman } 8064e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 807cc728227SDavid Malone 808897d75c9SQing Li (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 809cd29a779SQing Li (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 810897d75c9SQing Li 811cc728227SDavid Malone /* 812cc728227SDavid Malone * Also check that the node which sent the ARP packet 813*6bccea7cSRebecca Cran * is on the interface we expect it to be on. This 814cc728227SDavid Malone * avoids ARP chaos if an interface is connected to the 815cc728227SDavid Malone * wrong network. 816cc728227SDavid Malone */ 817cc728227SDavid Malone sin.sin_addr = isaddr; 818cc728227SDavid Malone 8198b07e49aSJulian Elischer /* XXX MRT use table 0 for arp checks */ 8208b07e49aSJulian Elischer rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 821b2a8ac7cSLuigi Rizzo if (!rt) 822b2a8ac7cSLuigi Rizzo goto drop; 823322dcb8dSMax Khon if (rt->rt_ifp != ifp) { 824cc728227SDavid Malone log(LOG_INFO, "arp_proxy: ignoring request" 8259bf40edeSBrooks Davis " from %s via %s, expecting %s\n", 8269bf40edeSBrooks Davis inet_ntoa(isaddr), ifp->if_xname, 8279bf40edeSBrooks Davis rt->rt_ifp->if_xname); 8284e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 829b2a8ac7cSLuigi Rizzo goto drop; 830cc728227SDavid Malone } 8314e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 832cc728227SDavid Malone 833ac234f93SGarrett Wollman #ifdef DEBUG_PROXY 834ac234f93SGarrett Wollman printf("arp: proxying for %s\n", 835ef0cdf33SGarrett Wollman inet_ntoa(itaddr)); 836ac234f93SGarrett Wollman #endif 837df8bae1dSRodney W. Grimes } 838cd29a779SQing Li } 839df8bae1dSRodney W. Grimes 840d0558157SBruce M Simpson if (itaddr.s_addr == myaddr.s_addr && 841d0558157SBruce M Simpson IN_LINKLOCAL(ntohl(itaddr.s_addr))) { 842d0558157SBruce M Simpson /* RFC 3927 link-local IPv4; always reply by broadcast. */ 843d0558157SBruce M Simpson #ifdef DEBUG_LINKLOCAL 844d0558157SBruce M Simpson printf("arp: sending reply for link-local addr %s\n", 845d0558157SBruce M Simpson inet_ntoa(itaddr)); 846d0558157SBruce M Simpson #endif 847d0558157SBruce M Simpson m->m_flags |= M_BCAST; 848d0558157SBruce M Simpson m->m_flags &= ~M_MCAST; 849d0558157SBruce M Simpson } else { 850d0558157SBruce M Simpson /* default behaviour; never reply by broadcast. */ 851d0558157SBruce M Simpson m->m_flags &= ~(M_BCAST|M_MCAST); 852d0558157SBruce M Simpson } 853322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 854322dcb8dSMax Khon (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 855322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REPLY); 856322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 85764bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 85864bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 85964bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 86064bf80ceSMatthew N. Dodd sa.sa_len = 2; 861279aa3d4SKip Macy (*ifp->if_output)(ifp, m, &sa, NULL); 86254fc657dSGeorge V. Neville-Neil ARPSTAT_INC(txreplies); 863df8bae1dSRodney W. Grimes return; 864b2a8ac7cSLuigi Rizzo 865b2a8ac7cSLuigi Rizzo drop: 866b2a8ac7cSLuigi Rizzo m_freem(m); 867df8bae1dSRodney W. Grimes } 8681d5e9e22SEivind Eklund #endif 869df8bae1dSRodney W. Grimes 870dd2e4102SGarrett Wollman void 871f2565d68SRobert Watson arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 872dd2e4102SGarrett Wollman { 8736e6b3f7cSQing Li struct llentry *lle; 8746e6b3f7cSQing Li 875ce9122fdSQing Li if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) { 876322dcb8dSMax Khon arprequest(ifp, &IA_SIN(ifa)->sin_addr, 877322dcb8dSMax Khon &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 8786e6b3f7cSQing Li /* 8796e6b3f7cSQing Li * interface address is considered static entry 8806e6b3f7cSQing Li * because the output of the arp utility shows 8816e6b3f7cSQing Li * that L2 entry as permanent 8826e6b3f7cSQing Li */ 8836e6b3f7cSQing Li IF_AFDATA_LOCK(ifp); 8846e6b3f7cSQing Li lle = lla_lookup(LLTABLE(ifp), (LLE_CREATE | LLE_IFADDR | LLE_STATIC), 8856e6b3f7cSQing Li (struct sockaddr *)IA_SIN(ifa)); 8866e6b3f7cSQing Li IF_AFDATA_UNLOCK(ifp); 8876e6b3f7cSQing Li if (lle == NULL) 8886e6b3f7cSQing Li log(LOG_INFO, "arp_ifinit: cannot create arp " 8896e6b3f7cSQing Li "entry for interface address\n"); 89086cd829dSKip Macy else 8916e6b3f7cSQing Li LLE_RUNLOCK(lle); 892ce9122fdSQing Li } 8936e6b3f7cSQing Li ifa->ifa_rtrequest = NULL; 894dd2e4102SGarrett Wollman } 895df5e1987SJonathan Lemon 896a9771948SGleb Smirnoff void 897f2565d68SRobert Watson arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 898a9771948SGleb Smirnoff { 899a9771948SGleb Smirnoff if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 900a9771948SGleb Smirnoff arprequest(ifp, &IA_SIN(ifa)->sin_addr, 901a9771948SGleb Smirnoff &IA_SIN(ifa)->sin_addr, enaddr); 9026e6b3f7cSQing Li ifa->ifa_rtrequest = NULL; 903a9771948SGleb Smirnoff } 904a9771948SGleb Smirnoff 9051ed81b73SMarko Zec static void 9061ed81b73SMarko Zec arp_init(void) 9071ed81b73SMarko Zec { 9081ed81b73SMarko Zec 909d4b5cae4SRobert Watson netisr_register(&arp_nh); 910df5e1987SJonathan Lemon } 911df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 912