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> 5576039bc8SGleb Smirnoff #include <net/if_var.h> 56df8bae1dSRodney W. Grimes #include <net/if_dl.h> 57722012ccSJulian Elischer #include <net/if_types.h> 58748e0b0aSGarrett Wollman #include <net/netisr.h> 59b149dd6cSLarry Lile #include <net/if_llc.h> 60c8f8e9c1SJulian Elischer #include <net/ethernet.h> 615736e6fbSBjoern A. Zeeb #include <net/route.h> 62530c0060SRobert Watson #include <net/vnet.h> 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes #include <netinet/in.h> 65df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 666e6b3f7cSQing Li #include <net/if_llatbl.h> 67df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 6883e521ecSBjoern A. Zeeb #ifdef INET 699963e8a5SWill Andrews #include <netinet/ip_carp.h> 709963e8a5SWill Andrews #endif 71df8bae1dSRodney W. Grimes 72322dcb8dSMax Khon #include <net/if_arc.h> 73fda82fc2SJulian Elischer #include <net/iso88025.h> 74fda82fc2SJulian Elischer 75aed55708SRobert Watson #include <security/mac/mac_framework.h> 76aed55708SRobert Watson 7747e8d432SGleb Smirnoff #define SIN(s) ((const struct sockaddr_in *)(s)) 78df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s) 79df8bae1dSRodney W. Grimes 80ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether); 816472ac3dSEd Schouten static SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 826472ac3dSEd Schouten static SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, ""); 83df8bae1dSRodney W. Grimes 84df8bae1dSRodney W. Grimes /* timer values */ 853e288e62SDimitry Andric static VNET_DEFINE(int, arpt_keep) = (20*60); /* once resolved, good for 20 86eddfbb76SRobert Watson * minutes */ 873e288e62SDimitry Andric static VNET_DEFINE(int, arp_maxtries) = 5; 883e288e62SDimitry Andric static VNET_DEFINE(int, arp_proxyall) = 0; 893e288e62SDimitry Andric static VNET_DEFINE(int, arpt_down) = 20; /* keep incomplete entries for 9093704ac5SQing Li * 20 seconds */ 915b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct arpstat, arpstat); /* ARP statistics, see if_arp.h */ 925b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(arpstat); 935b7cb97cSAndrey V. Elsukov 945b7cb97cSAndrey V. Elsukov #ifdef VIMAGE 955b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(arpstat); 965b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */ 97e162ea60SGeorge V. Neville-Neil 983e288e62SDimitry Andric static VNET_DEFINE(int, arp_maxhold) = 1; 99885f1aa4SPoul-Henning Kamp 1001e77c105SRobert Watson #define V_arpt_keep VNET(arpt_keep) 10193704ac5SQing Li #define V_arpt_down VNET(arpt_down) 1021e77c105SRobert Watson #define V_arp_maxtries VNET(arp_maxtries) 1031e77c105SRobert Watson #define V_arp_proxyall VNET(arp_proxyall) 104e162ea60SGeorge V. Neville-Neil #define V_arp_maxhold VNET(arp_maxhold) 105885f1aa4SPoul-Henning Kamp 1066df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_VNET | CTLFLAG_RW, 107eddfbb76SRobert Watson &VNET_NAME(arpt_keep), 0, 108eddfbb76SRobert Watson "ARP entry lifetime in seconds"); 1096df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_VNET | CTLFLAG_RW, 110eddfbb76SRobert Watson &VNET_NAME(arp_maxtries), 0, 1118b615593SMarko Zec "ARP resolution attempts before returning error"); 1126df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_VNET | CTLFLAG_RW, 113eddfbb76SRobert Watson &VNET_NAME(arp_proxyall), 0, 1148b615593SMarko Zec "Enable proxy ARP for all suitable requests"); 1156df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, wait, CTLFLAG_VNET | CTLFLAG_RW, 116e162ea60SGeorge V. Neville-Neil &VNET_NAME(arpt_down), 0, 117e162ea60SGeorge V. Neville-Neil "Incomplete ARP entry lifetime in seconds"); 1185b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_link_ether_arp, OID_AUTO, stats, struct arpstat, 1195b7cb97cSAndrey V. Elsukov arpstat, "ARP statistics (struct arpstat, net/if_arp.h)"); 1206df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxhold, CTLFLAG_VNET | CTLFLAG_RW, 121e162ea60SGeorge V. Neville-Neil &VNET_NAME(arp_maxhold), 0, 122e162ea60SGeorge V. Neville-Neil "Number of packets to hold per ARP entry"); 123885f1aa4SPoul-Henning Kamp 1244d77a549SAlfred Perlstein static void arp_init(void); 1251cafed39SJonathan Lemon static void arpintr(struct mbuf *); 1264d77a549SAlfred Perlstein static void arptimer(void *); 1271d5e9e22SEivind Eklund #ifdef INET 1284d77a549SAlfred Perlstein static void in_arpinput(struct mbuf *); 1291d5e9e22SEivind Eklund #endif 13028e82295SGarrett Wollman 131d4b5cae4SRobert Watson static const struct netisr_handler arp_nh = { 132d4b5cae4SRobert Watson .nh_name = "arp", 133d4b5cae4SRobert Watson .nh_handler = arpintr, 134d4b5cae4SRobert Watson .nh_proto = NETISR_ARP, 135d4b5cae4SRobert Watson .nh_policy = NETISR_POLICY_SOURCE, 136d4b5cae4SRobert Watson }; 137d4b5cae4SRobert Watson 1386e6b3f7cSQing Li #ifdef AF_INET 139df8bae1dSRodney W. Grimes /* 140237bf7f7SGleb Smirnoff * called by in_scrubprefix() to remove entry from the table when 1416e6b3f7cSQing Li * the interface goes away 1426e6b3f7cSQing Li */ 1436e6b3f7cSQing Li void 1446e6b3f7cSQing Li arp_ifscrub(struct ifnet *ifp, uint32_t addr) 1456e6b3f7cSQing Li { 1466e6b3f7cSQing Li struct sockaddr_in addr4; 1476e6b3f7cSQing Li 1486e6b3f7cSQing Li bzero((void *)&addr4, sizeof(addr4)); 1496e6b3f7cSQing Li addr4.sin_len = sizeof(addr4); 1506e6b3f7cSQing Li addr4.sin_family = AF_INET; 1516e6b3f7cSQing Li addr4.sin_addr.s_addr = addr; 152ea0c3776SAndrey V. Elsukov IF_AFDATA_RLOCK(ifp); 1536e6b3f7cSQing Li lla_lookup(LLTABLE(ifp), (LLE_DELETE | LLE_IFADDR), 1546e6b3f7cSQing Li (struct sockaddr *)&addr4); 155ea0c3776SAndrey V. Elsukov IF_AFDATA_RUNLOCK(ifp); 1566e6b3f7cSQing Li } 1576e6b3f7cSQing Li #endif 1586e6b3f7cSQing Li 1596e6b3f7cSQing Li /* 1606e6b3f7cSQing Li * Timeout routine. Age arp_tab entries periodically. 161df8bae1dSRodney W. Grimes */ 162df8bae1dSRodney W. Grimes static void 1631daaa65dSGleb Smirnoff arptimer(void *arg) 164df8bae1dSRodney W. Grimes { 165ea537929SGleb Smirnoff struct llentry *lle = (struct llentry *)arg; 1666e6b3f7cSQing Li struct ifnet *ifp; 167df8bae1dSRodney W. Grimes 168ea537929SGleb Smirnoff if (lle->la_flags & LLE_STATIC) { 169*2575fbb8SRandall Stewart return; 170*2575fbb8SRandall Stewart } 171*2575fbb8SRandall Stewart LLE_WLOCK(lle); 172*2575fbb8SRandall Stewart if (callout_pending(&lle->la_timer)) { 173*2575fbb8SRandall Stewart /* 174*2575fbb8SRandall Stewart * Here we are a bit odd here in the treatment of 175*2575fbb8SRandall Stewart * active/pending. If the pending bit is set, it got 176*2575fbb8SRandall Stewart * rescheduled before I ran. The active 177*2575fbb8SRandall Stewart * bit we ignore, since if it was stopped 178*2575fbb8SRandall Stewart * in ll_tablefree() and was currently running 179*2575fbb8SRandall Stewart * it would have return 0 so the code would 180*2575fbb8SRandall Stewart * not have deleted it since the callout could 181*2575fbb8SRandall Stewart * not be stopped so we want to go through 182*2575fbb8SRandall Stewart * with the delete here now. If the callout 183*2575fbb8SRandall Stewart * was restarted, the pending bit will be back on and 184*2575fbb8SRandall Stewart * we just want to bail since the callout_reset would 185*2575fbb8SRandall Stewart * return 1 and our reference would have been removed 186*2575fbb8SRandall Stewart * by arpresolve() below. 187*2575fbb8SRandall Stewart */ 188ea537929SGleb Smirnoff LLE_WUNLOCK(lle); 189ea537929SGleb Smirnoff return; 190ea537929SGleb Smirnoff } 1916e6b3f7cSQing Li ifp = lle->lle_tbl->llt_ifp; 19254fc657dSGeorge V. Neville-Neil CURVNET_SET(ifp->if_vnet); 19309fe6320SNavdeep Parhar 194e364d8c4SNavdeep Parhar if ((lle->la_flags & LLE_DELETED) == 0) { 19509fe6320SNavdeep Parhar int evt; 19609fe6320SNavdeep Parhar 19709fe6320SNavdeep Parhar if (lle->la_flags & LLE_VALID) 19809fe6320SNavdeep Parhar evt = LLENTRY_EXPIRED; 19909fe6320SNavdeep Parhar else 20009fe6320SNavdeep Parhar evt = LLENTRY_TIMEDOUT; 20109fe6320SNavdeep Parhar EVENTHANDLER_INVOKE(lle_event, lle, evt); 20209fe6320SNavdeep Parhar } 20309fe6320SNavdeep Parhar 204ea537929SGleb Smirnoff callout_stop(&lle->la_timer); 205ea537929SGleb Smirnoff 206ea537929SGleb Smirnoff /* XXX: LOR avoidance. We still have ref on lle. */ 207ea537929SGleb Smirnoff LLE_WUNLOCK(lle); 208ea537929SGleb Smirnoff IF_AFDATA_LOCK(ifp); 209ea537929SGleb Smirnoff LLE_WLOCK(lle); 210ea537929SGleb Smirnoff 211b1ec2940SGleb Smirnoff /* Guard against race with other llentry_free(). */ 212b1ec2940SGleb Smirnoff if (lle->la_flags & LLE_LINKED) { 213b1ec2940SGleb Smirnoff size_t pkts_dropped; 214b1ec2940SGleb Smirnoff 215ea537929SGleb Smirnoff LLE_REMREF(lle); 216e162ea60SGeorge V. Neville-Neil pkts_dropped = llentry_free(lle); 217e162ea60SGeorge V. Neville-Neil ARPSTAT_ADD(dropped, pkts_dropped); 218b1ec2940SGleb Smirnoff } else 219b1ec2940SGleb Smirnoff LLE_FREE_LOCKED(lle); 220b1ec2940SGleb Smirnoff 221b1ec2940SGleb Smirnoff IF_AFDATA_UNLOCK(ifp); 222b1ec2940SGleb Smirnoff 22354fc657dSGeorge V. Neville-Neil ARPSTAT_INC(timeouts); 224b1ec2940SGleb Smirnoff 22554fc657dSGeorge V. Neville-Neil CURVNET_RESTORE(); 226df8bae1dSRodney W. Grimes } 227df8bae1dSRodney W. Grimes 228df8bae1dSRodney W. Grimes /* 229df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies: 230df8bae1dSRodney W. Grimes * - arp header source ip address 231df8bae1dSRodney W. Grimes * - arp header target ip address 232df8bae1dSRodney W. Grimes * - arp header source ethernet address 233df8bae1dSRodney W. Grimes */ 2346e6b3f7cSQing Li void 23547e8d432SGleb Smirnoff arprequest(struct ifnet *ifp, const struct in_addr *sip, 23647e8d432SGleb Smirnoff const struct in_addr *tip, u_char *enaddr) 237df8bae1dSRodney W. Grimes { 238e952fa39SMatthew N. Dodd struct mbuf *m; 239e952fa39SMatthew N. Dodd struct arphdr *ah; 240df8bae1dSRodney W. Grimes struct sockaddr sa; 241aa24ae3cSGleb Smirnoff u_char *carpaddr = NULL; 242df8bae1dSRodney W. Grimes 2436e6b3f7cSQing Li if (sip == NULL) { 2446e6b3f7cSQing Li /* 2456e6b3f7cSQing Li * The caller did not supply a source address, try to find 2466e6b3f7cSQing Li * a compatible one among those assigned to this interface. 2476e6b3f7cSQing Li */ 2486e6b3f7cSQing Li struct ifaddr *ifa; 2496e6b3f7cSQing Li 250aa24ae3cSGleb Smirnoff IF_ADDR_RLOCK(ifp); 2516e6b3f7cSQing Li TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 252aa24ae3cSGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_INET) 2536e6b3f7cSQing Li continue; 254aa24ae3cSGleb Smirnoff 255aa24ae3cSGleb Smirnoff if (ifa->ifa_carp) { 256aa24ae3cSGleb Smirnoff if ((*carp_iamatch_p)(ifa, &carpaddr) == 0) 257aa24ae3cSGleb Smirnoff continue; 258aa24ae3cSGleb Smirnoff sip = &IA_SIN(ifa)->sin_addr; 259aa24ae3cSGleb Smirnoff } else { 260aa24ae3cSGleb Smirnoff carpaddr = NULL; 261aa24ae3cSGleb Smirnoff sip = &IA_SIN(ifa)->sin_addr; 262aa24ae3cSGleb Smirnoff } 263aa24ae3cSGleb Smirnoff 2646e6b3f7cSQing Li if (0 == ((sip->s_addr ^ tip->s_addr) & 265aa24ae3cSGleb Smirnoff IA_MASKSIN(ifa)->sin_addr.s_addr)) 2666e6b3f7cSQing Li break; /* found it. */ 2676e6b3f7cSQing Li } 268aa24ae3cSGleb Smirnoff IF_ADDR_RUNLOCK(ifp); 2696e6b3f7cSQing Li if (sip == NULL) { 2706e6b3f7cSQing Li printf("%s: cannot find matching address\n", __func__); 2716e6b3f7cSQing Li return; 2726e6b3f7cSQing Li } 2736e6b3f7cSQing Li } 274aa24ae3cSGleb Smirnoff if (enaddr == NULL) 275aa24ae3cSGleb Smirnoff enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp); 2766e6b3f7cSQing Li 277eb1b1807SGleb Smirnoff if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL) 278df8bae1dSRodney W. Grimes return; 27964bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) + 280546451a2SGleb Smirnoff 2 * ifp->if_addrlen; 28164bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 282ed6a66caSRobert Watson M_ALIGN(m, m->m_len); 28364bf80ceSMatthew N. Dodd ah = mtod(m, struct arphdr *); 28464bf80ceSMatthew N. Dodd bzero((caddr_t)ah, m->m_len); 28519527d3eSRobert Watson #ifdef MAC 286b9b0dac3SRobert Watson mac_netinet_arp_send(ifp, m); 28719527d3eSRobert Watson #endif 288322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); 289322dcb8dSMax Khon ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 290322dcb8dSMax Khon ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 291322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REQUEST); 29247e8d432SGleb Smirnoff bcopy(enaddr, ar_sha(ah), ah->ar_hln); 29347e8d432SGleb Smirnoff bcopy(sip, ar_spa(ah), ah->ar_pln); 29447e8d432SGleb Smirnoff bcopy(tip, ar_tpa(ah), ah->ar_pln); 29564bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 29664bf80ceSMatthew N. Dodd sa.sa_len = 2; 29764bf80ceSMatthew N. Dodd m->m_flags |= M_BCAST; 29886bd0491SAndre Oppermann m_clrprotoflags(m); /* Avoid confusing lower layers. */ 299279aa3d4SKip Macy (*ifp->if_output)(ifp, m, &sa, NULL); 30054fc657dSGeorge V. Neville-Neil ARPSTAT_INC(txrequests); 301df8bae1dSRodney W. Grimes } 302df8bae1dSRodney W. Grimes 303df8bae1dSRodney W. Grimes /* 304cd46a114SLuigi Rizzo * Resolve an IP address into an ethernet address. 305cd46a114SLuigi Rizzo * On input: 306cd46a114SLuigi Rizzo * ifp is the interface we use 30774860d4fSAlexander V. Chernikov * is_gw != if @dst represents gateway to some destination 308b6ae6984SJulian Elischer * m is the mbuf. May be NULL if we don't have a packet. 309b6ae6984SJulian Elischer * dst is the next hop, 310cd46a114SLuigi Rizzo * desten is where we want the address. 31174860d4fSAlexander V. Chernikov * flags returns lle entry flags. 312cd46a114SLuigi Rizzo * 31374860d4fSAlexander V. Chernikov * On success, desten and flags are filled in and the function returns 0; 314cd46a114SLuigi Rizzo * If the packet must be held pending resolution, we return EWOULDBLOCK 315cd46a114SLuigi Rizzo * On other errors, we return the corresponding error code. 316b6ae6984SJulian Elischer * Note that m_freem() handles NULL. 317df8bae1dSRodney W. Grimes */ 318df8bae1dSRodney W. Grimes int 31974860d4fSAlexander V. Chernikov arpresolve(struct ifnet *ifp, int is_gw, struct mbuf *m, 32074860d4fSAlexander V. Chernikov const struct sockaddr *dst, u_char *desten, uint32_t *pflags) 321df8bae1dSRodney W. Grimes { 3226e6b3f7cSQing Li struct llentry *la = 0; 32300a46b31SKip Macy u_int flags = 0; 324e162ea60SGeorge V. Neville-Neil struct mbuf *curr = NULL; 325e162ea60SGeorge V. Neville-Neil struct mbuf *next = NULL; 3266e6b3f7cSQing Li int error, renew; 327df8bae1dSRodney W. Grimes 32874860d4fSAlexander V. Chernikov if (pflags != NULL) 32974860d4fSAlexander V. Chernikov *pflags = 0; 33074860d4fSAlexander V. Chernikov 3316e6b3f7cSQing Li if (m != NULL) { 332b6ae6984SJulian Elischer if (m->m_flags & M_BCAST) { 333b6ae6984SJulian Elischer /* broadcast */ 334b6ae6984SJulian Elischer (void)memcpy(desten, 335b6ae6984SJulian Elischer ifp->if_broadcastaddr, ifp->if_addrlen); 336cd46a114SLuigi Rizzo return (0); 337df8bae1dSRodney W. Grimes } 338d63e657cSAlexander V. Chernikov if (m->m_flags & M_MCAST) { 339b6ae6984SJulian Elischer /* multicast */ 340df8bae1dSRodney W. Grimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 341cd46a114SLuigi Rizzo return (0); 342df8bae1dSRodney W. Grimes } 343b6ae6984SJulian Elischer } 3446e6b3f7cSQing Li retry: 34500a46b31SKip Macy IF_AFDATA_RLOCK(ifp); 3466e6b3f7cSQing Li la = lla_lookup(LLTABLE(ifp), flags, dst); 34700a46b31SKip Macy IF_AFDATA_RUNLOCK(ifp); 34800a46b31SKip Macy if ((la == NULL) && ((flags & LLE_EXCLUSIVE) == 0) 34900a46b31SKip Macy && ((ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0)) { 35000a46b31SKip Macy flags |= (LLE_CREATE | LLE_EXCLUSIVE); 35100a46b31SKip Macy IF_AFDATA_WLOCK(ifp); 35200a46b31SKip Macy la = lla_lookup(LLTABLE(ifp), flags, dst); 35300a46b31SKip Macy IF_AFDATA_WUNLOCK(ifp); 35400a46b31SKip Macy } 3551ed7bf1eSGleb Smirnoff if (la == NULL) { 3566e6b3f7cSQing Li if (flags & LLE_CREATE) 3571ed7bf1eSGleb Smirnoff log(LOG_DEBUG, 358743c072aSAlan Somers "arpresolve: can't allocate llinfo for %s on %s\n", 359743c072aSAlan Somers inet_ntoa(SIN(dst)->sin_addr), ifp->if_xname); 3601ed7bf1eSGleb Smirnoff m_freem(m); 3616e6b3f7cSQing Li return (EINVAL); 3621ed7bf1eSGleb Smirnoff } 363a20e2538SGleb Smirnoff 3646e6b3f7cSQing Li if ((la->la_flags & LLE_VALID) && 365a98c06f1SGleb Smirnoff ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) { 3666e6b3f7cSQing Li bcopy(&la->ll_addr, desten, ifp->if_addrlen); 367f0f3379eSOrion Hodson /* 368f0f3379eSOrion Hodson * If entry has an expiry time and it is approaching, 3696e6b3f7cSQing Li * see if we need to send an ARP request within this 3706e6b3f7cSQing Li * arpt_down interval. 371f0f3379eSOrion Hodson */ 3726e6b3f7cSQing Li if (!(la->la_flags & LLE_STATIC) && 373a98c06f1SGleb Smirnoff time_uptime + la->la_preempt > la->la_expire) { 374aa24ae3cSGleb Smirnoff arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL); 375022695f8SOrion Hodson la->la_preempt--; 376f0f3379eSOrion Hodson } 377f0f3379eSOrion Hodson 37874860d4fSAlexander V. Chernikov if (pflags != NULL) 37974860d4fSAlexander V. Chernikov *pflags = la->la_flags; 3806e6b3f7cSQing Li error = 0; 3816e6b3f7cSQing Li goto done; 382df8bae1dSRodney W. Grimes } 3836e6b3f7cSQing Li 3846e6b3f7cSQing Li if (la->la_flags & LLE_STATIC) { /* should not happen! */ 3856e6b3f7cSQing Li log(LOG_DEBUG, "arpresolve: ouch, empty static llinfo for %s\n", 3866e6b3f7cSQing Li inet_ntoa(SIN(dst)->sin_addr)); 38747891de1SRuslan Ermilov m_freem(m); 3886e6b3f7cSQing Li error = EINVAL; 3896e6b3f7cSQing Li goto done; 3906e6b3f7cSQing Li } 3916e6b3f7cSQing Li 392a98c06f1SGleb Smirnoff renew = (la->la_asked == 0 || la->la_expire != time_uptime); 3936e6b3f7cSQing Li if ((renew || m != NULL) && (flags & LLE_EXCLUSIVE) == 0) { 3946e6b3f7cSQing Li flags |= LLE_EXCLUSIVE; 3956e6b3f7cSQing Li LLE_RUNLOCK(la); 3966e6b3f7cSQing Li goto retry; 39747891de1SRuslan Ermilov } 39808aadfbbSJonathan Lemon /* 399df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address 400e162ea60SGeorge V. Neville-Neil * response yet. Add the mbuf to the list, dropping 401e162ea60SGeorge V. Neville-Neil * the oldest packet if we have exceeded the system 402e162ea60SGeorge V. Neville-Neil * setting. 403df8bae1dSRodney W. Grimes */ 4046e6b3f7cSQing Li if (m != NULL) { 405e162ea60SGeorge V. Neville-Neil if (la->la_numheld >= V_arp_maxhold) { 40654fc657dSGeorge V. Neville-Neil if (la->la_hold != NULL) { 407e162ea60SGeorge V. Neville-Neil next = la->la_hold->m_nextpkt; 408df8bae1dSRodney W. Grimes m_freem(la->la_hold); 409e162ea60SGeorge V. Neville-Neil la->la_hold = next; 410e162ea60SGeorge V. Neville-Neil la->la_numheld--; 41154fc657dSGeorge V. Neville-Neil ARPSTAT_INC(dropped); 41254fc657dSGeorge V. Neville-Neil } 413e162ea60SGeorge V. Neville-Neil } 414e162ea60SGeorge V. Neville-Neil if (la->la_hold != NULL) { 415e162ea60SGeorge V. Neville-Neil curr = la->la_hold; 416e162ea60SGeorge V. Neville-Neil while (curr->m_nextpkt != NULL) 417e162ea60SGeorge V. Neville-Neil curr = curr->m_nextpkt; 418e162ea60SGeorge V. Neville-Neil curr->m_nextpkt = m; 419e162ea60SGeorge V. Neville-Neil } else 420df8bae1dSRodney W. Grimes la->la_hold = m; 421e162ea60SGeorge V. Neville-Neil la->la_numheld++; 4226e6b3f7cSQing Li if (renew == 0 && (flags & LLE_EXCLUSIVE)) { 4236e6b3f7cSQing Li flags &= ~LLE_EXCLUSIVE; 4246e6b3f7cSQing Li LLE_DOWNGRADE(la); 42558505389SKip Macy } 426e1ff74c5SGleb Smirnoff 4276e6b3f7cSQing Li } 428e1ff74c5SGleb Smirnoff /* 429e1ff74c5SGleb Smirnoff * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 430e1ff74c5SGleb Smirnoff * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 431e1ff74c5SGleb Smirnoff * if we have already sent arp_maxtries ARP requests. Retransmit the 432e1ff74c5SGleb Smirnoff * ARP request, but not faster than one request per second. 433e1ff74c5SGleb Smirnoff */ 434603724d3SBjoern A. Zeeb if (la->la_asked < V_arp_maxtries) 435e1ff74c5SGleb Smirnoff error = EWOULDBLOCK; /* First request. */ 436e1ff74c5SGleb Smirnoff else 43774860d4fSAlexander V. Chernikov error = is_gw != 0 ? EHOSTUNREACH : EHOSTDOWN; 438e1ff74c5SGleb Smirnoff 4396e6b3f7cSQing Li if (renew) { 440becba438SBjoern A. Zeeb int canceled; 441becba438SBjoern A. Zeeb 4426e6b3f7cSQing Li LLE_ADDREF(la); 443a98c06f1SGleb Smirnoff la->la_expire = time_uptime; 444becba438SBjoern A. Zeeb canceled = callout_reset(&la->la_timer, hz * V_arpt_down, 445becba438SBjoern A. Zeeb arptimer, la); 446becba438SBjoern A. Zeeb if (canceled) 447becba438SBjoern A. Zeeb LLE_REMREF(la); 44895ebcabeSMaxim Konovalov la->la_asked++; 4496e6b3f7cSQing Li LLE_WUNLOCK(la); 450aa24ae3cSGleb Smirnoff arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL); 4516e6b3f7cSQing Li return (error); 4526e6b3f7cSQing Li } 4536e6b3f7cSQing Li done: 4546e6b3f7cSQing Li if (flags & LLE_EXCLUSIVE) 4556e6b3f7cSQing Li LLE_WUNLOCK(la); 4566e6b3f7cSQing Li else 4576e6b3f7cSQing Li LLE_RUNLOCK(la); 458e1ff74c5SGleb Smirnoff return (error); 459df8bae1dSRodney W. Grimes } 460df8bae1dSRodney W. Grimes 461df8bae1dSRodney W. Grimes /* 462df8bae1dSRodney W. Grimes * Common length and type checks are done here, 463df8bae1dSRodney W. Grimes * then the protocol-specific routine is called. 464df8bae1dSRodney W. Grimes */ 465885f1aa4SPoul-Henning Kamp static void 4661cafed39SJonathan Lemon arpintr(struct mbuf *m) 467df8bae1dSRodney W. Grimes { 4681cafed39SJonathan Lemon struct arphdr *ar; 469df8bae1dSRodney W. Grimes 47076ec7b2fSRobert Watson if (m->m_len < sizeof(struct arphdr) && 47184365e2bSMatthew Dillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 472c9168718SGleb Smirnoff log(LOG_NOTICE, "arp: runt packet -- m_pullup failed\n"); 4731cafed39SJonathan Lemon return; 47476ec7b2fSRobert Watson } 47576ec7b2fSRobert Watson ar = mtod(m, struct arphdr *); 47676ec7b2fSRobert Watson 4771cafed39SJonathan Lemon if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 4781cafed39SJonathan Lemon ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 479b8b33234SDoug Rabson ntohs(ar->ar_hrd) != ARPHRD_ARCNET && 480e4cd31ddSJeff Roberson ntohs(ar->ar_hrd) != ARPHRD_IEEE1394 && 481e4cd31ddSJeff Roberson ntohs(ar->ar_hrd) != ARPHRD_INFINIBAND) { 48261905171SGleb Smirnoff log(LOG_NOTICE, "arp: unknown hardware address format (0x%2D)" 48361905171SGleb Smirnoff " (from %*D to %*D)\n", (unsigned char *)&ar->ar_hrd, "", 48461905171SGleb Smirnoff ETHER_ADDR_LEN, (u_char *)ar_sha(ar), ":", 48561905171SGleb Smirnoff ETHER_ADDR_LEN, (u_char *)ar_tha(ar), ":"); 48676ec7b2fSRobert Watson m_freem(m); 4871cafed39SJonathan Lemon return; 48876ec7b2fSRobert Watson } 48976ec7b2fSRobert Watson 49031175791SRuslan Ermilov if (m->m_len < arphdr_len(ar)) { 49178e2d2bdSRuslan Ermilov if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 492c9168718SGleb Smirnoff log(LOG_NOTICE, "arp: runt packet\n"); 49376ec7b2fSRobert Watson m_freem(m); 4941cafed39SJonathan Lemon return; 49576ec7b2fSRobert Watson } 49678e2d2bdSRuslan Ermilov ar = mtod(m, struct arphdr *); 49778e2d2bdSRuslan Ermilov } 498df8bae1dSRodney W. Grimes 49954fc657dSGeorge V. Neville-Neil ARPSTAT_INC(received); 500df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) { 5011d5e9e22SEivind Eklund #ifdef INET 502df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 503df8bae1dSRodney W. Grimes in_arpinput(m); 5041cafed39SJonathan Lemon return; 5051d5e9e22SEivind Eklund #endif 506df8bae1dSRodney W. Grimes } 507df8bae1dSRodney W. Grimes m_freem(m); 508df8bae1dSRodney W. Grimes } 509df8bae1dSRodney W. Grimes 5101d5e9e22SEivind Eklund #ifdef INET 511df8bae1dSRodney W. Grimes /* 512df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet. 513df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826. 514df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender 515df8bae1dSRodney W. Grimes * protocol address, to catch impersonators. 516df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol: 517df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 518df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us, 519df8bae1dSRodney W. Grimes * and also sent them in response to IP replies. 520df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive 521df8bae1dSRodney W. Grimes * trailer packets. 522df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 523df8bae1dSRodney W. Grimes * but formerly didn't normally send requests. 524df8bae1dSRodney W. Grimes */ 5253269187dSAlfred Perlstein static int log_arp_wrong_iface = 1; 526e3d123d6SAlfred Perlstein static int log_arp_movements = 1; 52739393906SGleb Smirnoff static int log_arp_permanent_modify = 1; 528478df1d5SGleb Smirnoff static int allow_multicast = 0; 5295d81d095SGleb Smirnoff static struct timeval arp_lastlog; 5305d81d095SGleb Smirnoff static int arp_curpps; 5315d81d095SGleb Smirnoff static int arp_maxpps = 1; 5323269187dSAlfred Perlstein 5333269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 5343269187dSAlfred Perlstein &log_arp_wrong_iface, 0, 5353269187dSAlfred Perlstein "log arp packets arriving on the wrong interface"); 536e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 537e3d123d6SAlfred Perlstein &log_arp_movements, 0, 53875ce3221SAlfred Perlstein "log arp replies from MACs different than the one in the cache"); 53939393906SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, 54039393906SGleb Smirnoff &log_arp_permanent_modify, 0, 54139393906SGleb Smirnoff "log arp replies from MACs different than the one in the permanent arp entry"); 542478df1d5SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW, 543478df1d5SGleb Smirnoff &allow_multicast, 0, "accept multicast addresses"); 5445d81d095SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_log_per_second, 5455d81d095SGleb Smirnoff CTLFLAG_RW, &arp_maxpps, 0, 5465d81d095SGleb Smirnoff "Maximum number of remotely triggered ARP messages that can be " 5475d81d095SGleb Smirnoff "logged per second"); 5485d81d095SGleb Smirnoff 5495d81d095SGleb Smirnoff #define ARP_LOG(pri, ...) do { \ 5505d81d095SGleb Smirnoff if (ppsratecheck(&arp_lastlog, &arp_curpps, arp_maxpps)) \ 5515d81d095SGleb Smirnoff log((pri), "arp: " __VA_ARGS__); \ 5525d81d095SGleb Smirnoff } while (0) 5533269187dSAlfred Perlstein 554df8bae1dSRodney W. Grimes static void 555f2565d68SRobert Watson in_arpinput(struct mbuf *m) 556df8bae1dSRodney W. Grimes { 557e952fa39SMatthew N. Dodd struct arphdr *ah; 558e952fa39SMatthew N. Dodd struct ifnet *ifp = m->m_pkthdr.rcvif; 5596e6b3f7cSQing Li struct llentry *la = NULL; 560e952fa39SMatthew N. Dodd struct rtentry *rt; 561ca925d9cSJonathan Lemon struct ifaddr *ifa; 562ca925d9cSJonathan Lemon struct in_ifaddr *ia; 563df8bae1dSRodney W. Grimes struct sockaddr sa; 564df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr; 565a9771948SGleb Smirnoff u_int8_t *enaddr = NULL; 5666e6b3f7cSQing Li int op, flags; 567322dcb8dSMax Khon int req_len; 56880b11ee4SPhilip Paeps int bridged = 0, is_bridge = 0; 56908b68b0eSGleb Smirnoff int carped; 5708e7e854cSKip Macy struct sockaddr_in sin; 5718e7e854cSKip Macy sin.sin_len = sizeof(struct sockaddr_in); 5728e7e854cSKip Macy sin.sin_family = AF_INET; 57329910a5aSKip Macy sin.sin_addr.s_addr = 0; 574df8bae1dSRodney W. Grimes 57574948aa6SAndrew Thompson if (ifp->if_bridge) 5768f867517SAndrew Thompson bridged = 1; 57780b11ee4SPhilip Paeps if (ifp->if_type == IFT_BRIDGE) 57880b11ee4SPhilip Paeps is_bridge = 1; 5798f867517SAndrew Thompson 580322dcb8dSMax Khon req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 581322dcb8dSMax Khon if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 5825d81d095SGleb Smirnoff ARP_LOG(LOG_NOTICE, "runt packet -- m_pullup failed\n"); 5834cbc8ad1SYaroslav Tykhiy return; 5844cbc8ad1SYaroslav Tykhiy } 5854cbc8ad1SYaroslav Tykhiy 586322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 58709d3f895SGeorge V. Neville-Neil /* 58809d3f895SGeorge V. Neville-Neil * ARP is only for IPv4 so we can reject packets with 58909d3f895SGeorge V. Neville-Neil * a protocol length not equal to an IPv4 address. 59009d3f895SGeorge V. Neville-Neil */ 59109d3f895SGeorge V. Neville-Neil if (ah->ar_pln != sizeof(struct in_addr)) { 5925d81d095SGleb Smirnoff ARP_LOG(LOG_NOTICE, "requested protocol length != %zu\n", 59309d3f895SGeorge V. Neville-Neil sizeof(struct in_addr)); 594414676baSGleb Smirnoff goto drop; 59509d3f895SGeorge V. Neville-Neil } 59609d3f895SGeorge V. Neville-Neil 597478df1d5SGleb Smirnoff if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) { 5985d81d095SGleb Smirnoff ARP_LOG(LOG_NOTICE, "%*D is multicast\n", 599c9168718SGleb Smirnoff ifp->if_addrlen, (u_char *)ar_sha(ah), ":"); 600414676baSGleb Smirnoff goto drop; 60109d3f895SGeorge V. Neville-Neil } 60209d3f895SGeorge V. Neville-Neil 603322dcb8dSMax Khon op = ntohs(ah->ar_op); 604322dcb8dSMax Khon (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 605322dcb8dSMax Khon (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 60632439868SGleb Smirnoff 60754fc657dSGeorge V. Neville-Neil if (op == ARPOP_REPLY) 60854fc657dSGeorge V. Neville-Neil ARPSTAT_INC(rxreplies); 60954fc657dSGeorge V. Neville-Neil 610ca925d9cSJonathan Lemon /* 611ca925d9cSJonathan Lemon * For a bridge, we want to check the address irrespective 612ca925d9cSJonathan Lemon * of the receive interface. (This will change slightly 613ca925d9cSJonathan Lemon * when we have clusters of interfaces). 614ca925d9cSJonathan Lemon */ 6152d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 6162ef4a436SGleb Smirnoff LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 61796561547SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) || 6186e6b3f7cSQing Li ia->ia_ifp == ifp) && 61908b68b0eSGleb Smirnoff itaddr.s_addr == ia->ia_addr.sin_addr.s_addr && 62008b68b0eSGleb Smirnoff (ia->ia_ifa.ifa_carp == NULL || 62108b68b0eSGleb Smirnoff (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) { 62209d54778SRobert Watson ifa_ref(&ia->ia_ifa); 6232d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 6242ef4a436SGleb Smirnoff goto match; 6252ef4a436SGleb Smirnoff } 6262ef4a436SGleb Smirnoff } 627ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 62896561547SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) || 6296e6b3f7cSQing Li ia->ia_ifp == ifp) && 63009d54778SRobert Watson isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 63109d54778SRobert Watson ifa_ref(&ia->ia_ifa); 6322d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 633ca925d9cSJonathan Lemon goto match; 63409d54778SRobert Watson } 63580b11ee4SPhilip Paeps 63680b11ee4SPhilip Paeps #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \ 63780b11ee4SPhilip Paeps (ia->ia_ifp->if_bridge == ifp->if_softc && \ 63880b11ee4SPhilip Paeps !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \ 63980b11ee4SPhilip Paeps addr == ia->ia_addr.sin_addr.s_addr) 64080b11ee4SPhilip Paeps /* 64180b11ee4SPhilip Paeps * Check the case when bridge shares its MAC address with 64280b11ee4SPhilip Paeps * some of its children, so packets are claimed by bridge 64380b11ee4SPhilip Paeps * itself (bridge_input() does it first), but they are really 64480b11ee4SPhilip Paeps * meant to be destined to the bridge member. 64580b11ee4SPhilip Paeps */ 64680b11ee4SPhilip Paeps if (is_bridge) { 64780b11ee4SPhilip Paeps LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 64880b11ee4SPhilip Paeps if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) { 64909d54778SRobert Watson ifa_ref(&ia->ia_ifa); 65080b11ee4SPhilip Paeps ifp = ia->ia_ifp; 6512d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 65280b11ee4SPhilip Paeps goto match; 65380b11ee4SPhilip Paeps } 65480b11ee4SPhilip Paeps } 65580b11ee4SPhilip Paeps } 65680b11ee4SPhilip Paeps #undef BDG_MEMBER_MATCHES_ARP 6572d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 65880b11ee4SPhilip Paeps 659ca925d9cSJonathan Lemon /* 660d8b84d9eSJonathan Lemon * No match, use the first inet address on the receive interface 661ca925d9cSJonathan Lemon * as a dummy address for the rest of the function. 662ca925d9cSJonathan Lemon */ 663137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 664d8b84d9eSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 6659de96e89SGleb Smirnoff if (ifa->ifa_addr->sa_family == AF_INET && 6669de96e89SGleb Smirnoff (ifa->ifa_carp == NULL || 6679de96e89SGleb Smirnoff (*carp_iamatch_p)(ifa, &enaddr))) { 668ec691a10SJonathan Lemon ia = ifatoia(ifa); 66909d54778SRobert Watson ifa_ref(ifa); 670137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 671ec691a10SJonathan Lemon goto match; 672ec691a10SJonathan Lemon } 673137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 67409d54778SRobert Watson 675ec691a10SJonathan Lemon /* 676ec691a10SJonathan Lemon * If bridging, fall back to using any inet address. 677ec691a10SJonathan Lemon */ 6782d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 6792d9cfabaSRobert Watson if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) { 6802d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 681b2a8ac7cSLuigi Rizzo goto drop; 6822d9cfabaSRobert Watson } 68309d54778SRobert Watson ifa_ref(&ia->ia_ifa); 6842d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 685ca925d9cSJonathan Lemon match: 686a9771948SGleb Smirnoff if (!enaddr) 687a9771948SGleb Smirnoff enaddr = (u_int8_t *)IF_LLADDR(ifp); 68808b68b0eSGleb Smirnoff carped = (ia->ia_ifa.ifa_carp != NULL); 689ca925d9cSJonathan Lemon myaddr = ia->ia_addr.sin_addr; 69009d54778SRobert Watson ifa_free(&ia->ia_ifa); 691a9771948SGleb Smirnoff if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 692b2a8ac7cSLuigi Rizzo goto drop; /* it's from me, ignore it. */ 693322dcb8dSMax Khon if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 6945d81d095SGleb Smirnoff ARP_LOG(LOG_NOTICE, "link address is broadcast for IP address " 6955d81d095SGleb Smirnoff "%s!\n", inet_ntoa(isaddr)); 696b2a8ac7cSLuigi Rizzo goto drop; 697df8bae1dSRodney W. Grimes } 69800fcf9d1SRobert Watson /* 69900fcf9d1SRobert Watson * Warn if another host is using the same IP address, but only if the 70000fcf9d1SRobert Watson * IP address isn't 0.0.0.0, which is used for DHCP only, in which 70100fcf9d1SRobert Watson * case we suppress the warning to avoid false positive complaints of 70200fcf9d1SRobert Watson * potential misconfiguration. 70300fcf9d1SRobert Watson */ 70408b68b0eSGleb Smirnoff if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr && 70508b68b0eSGleb Smirnoff myaddr.s_addr != 0) { 7065d81d095SGleb Smirnoff ARP_LOG(LOG_ERR, "%*D is using my IP address %s on %s!\n", 707322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7083affb6fbSYaroslav Tykhiy inet_ntoa(isaddr), ifp->if_xname); 709df8bae1dSRodney W. Grimes itaddr = myaddr; 71054fc657dSGeorge V. Neville-Neil ARPSTAT_INC(dupips); 711df8bae1dSRodney W. Grimes goto reply; 712df8bae1dSRodney W. Grimes } 713deb62e28SRuslan Ermilov if (ifp->if_flags & IFF_STATICARP) 714deb62e28SRuslan Ermilov goto reply; 7158b07e49aSJulian Elischer 7166e6b3f7cSQing Li bzero(&sin, sizeof(sin)); 7176e6b3f7cSQing Li sin.sin_len = sizeof(struct sockaddr_in); 7186e6b3f7cSQing Li sin.sin_family = AF_INET; 7196e6b3f7cSQing Li sin.sin_addr = isaddr; 7206e6b3f7cSQing Li flags = (itaddr.s_addr == myaddr.s_addr) ? LLE_CREATE : 0; 7216e6b3f7cSQing Li flags |= LLE_EXCLUSIVE; 7226e6b3f7cSQing Li IF_AFDATA_LOCK(ifp); 7236e6b3f7cSQing Li la = lla_lookup(LLTABLE(ifp), flags, (struct sockaddr *)&sin); 7246e6b3f7cSQing Li IF_AFDATA_UNLOCK(ifp); 7256e6b3f7cSQing Li if (la != NULL) { 7266e6b3f7cSQing Li /* the following is not an error when doing bridging */ 72708b68b0eSGleb Smirnoff if (!bridged && la->lle_tbl->llt_ifp != ifp) { 7283269187dSAlfred Perlstein if (log_arp_wrong_iface) 7295d81d095SGleb Smirnoff ARP_LOG(LOG_WARNING, "%s is on %s " 7306e6b3f7cSQing Li "but got reply from %*D on %s\n", 731dd9b6ddeSBill Fenner inet_ntoa(isaddr), 7326e6b3f7cSQing Li la->lle_tbl->llt_ifp->if_xname, 7336e6b3f7cSQing Li ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7349bf40edeSBrooks Davis ifp->if_xname); 735cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 7366e6b3f7cSQing Li goto reply; 737dd9b6ddeSBill Fenner } 7386e6b3f7cSQing Li if ((la->la_flags & LLE_VALID) && 7396e6b3f7cSQing Li bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) { 7406e6b3f7cSQing Li if (la->la_flags & LLE_STATIC) { 741cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 7424659e09dSAndrey V. Elsukov if (log_arp_permanent_modify) 7435d81d095SGleb Smirnoff ARP_LOG(LOG_ERR, 7445d81d095SGleb Smirnoff "%*D attempts to modify " 7454659e09dSAndrey V. Elsukov "permanent entry for %s on %s\n", 7464659e09dSAndrey V. Elsukov ifp->if_addrlen, 7474659e09dSAndrey V. Elsukov (u_char *)ar_sha(ah), ":", 7486e6b3f7cSQing Li inet_ntoa(isaddr), ifp->if_xname); 7496e6b3f7cSQing Li goto reply; 7506e6b3f7cSQing Li } 7516e6b3f7cSQing Li if (log_arp_movements) { 7525d81d095SGleb Smirnoff ARP_LOG(LOG_INFO, "%s moved from %*D " 7536e6b3f7cSQing Li "to %*D on %s\n", 7548b07e49aSJulian Elischer inet_ntoa(isaddr), 7556e6b3f7cSQing Li ifp->if_addrlen, 7566e6b3f7cSQing Li (u_char *)&la->ll_addr, ":", 7576e6b3f7cSQing Li ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7588b07e49aSJulian Elischer ifp->if_xname); 759dd9b6ddeSBill Fenner } 760dfd5dee1SPeter Wemm } 7616e6b3f7cSQing Li 762322dcb8dSMax Khon if (ifp->if_addrlen != ah->ar_hln) { 763cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 7645d81d095SGleb Smirnoff ARP_LOG(LOG_WARNING, "from %*D: addr len: new %d, " 765c9168718SGleb Smirnoff "i/f %d (ignored)\n", ifp->if_addrlen, 766c9168718SGleb Smirnoff (u_char *) ar_sha(ah), ":", ah->ar_hln, 767c9168718SGleb Smirnoff ifp->if_addrlen); 76809d3f895SGeorge V. Neville-Neil goto drop; 769322dcb8dSMax Khon } 7706e6b3f7cSQing Li (void)memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen); 7716e6b3f7cSQing Li la->la_flags |= LLE_VALID; 7728b07e49aSJulian Elischer 77309fe6320SNavdeep Parhar EVENTHANDLER_INVOKE(lle_event, la, LLENTRY_RESOLVED); 7749a311445SNavdeep Parhar 7756e6b3f7cSQing Li if (!(la->la_flags & LLE_STATIC)) { 776becba438SBjoern A. Zeeb int canceled; 777becba438SBjoern A. Zeeb 778becba438SBjoern A. Zeeb LLE_ADDREF(la); 779a98c06f1SGleb Smirnoff la->la_expire = time_uptime + V_arpt_keep; 780becba438SBjoern A. Zeeb canceled = callout_reset(&la->la_timer, 781becba438SBjoern A. Zeeb hz * V_arpt_keep, arptimer, la); 782becba438SBjoern A. Zeeb if (canceled) 783becba438SBjoern A. Zeeb LLE_REMREF(la); 7841daaa65dSGleb Smirnoff } 785022695f8SOrion Hodson la->la_asked = 0; 786603724d3SBjoern A. Zeeb la->la_preempt = V_arp_maxtries; 787e162ea60SGeorge V. Neville-Neil /* 788e162ea60SGeorge V. Neville-Neil * The packets are all freed within the call to the output 789e162ea60SGeorge V. Neville-Neil * routine. 790e162ea60SGeorge V. Neville-Neil * 791e162ea60SGeorge V. Neville-Neil * NB: The lock MUST be released before the call to the 792e162ea60SGeorge V. Neville-Neil * output routine. 793e162ea60SGeorge V. Neville-Neil */ 794e162ea60SGeorge V. Neville-Neil if (la->la_hold != NULL) { 795e162ea60SGeorge V. Neville-Neil struct mbuf *m_hold, *m_hold_next; 796e162ea60SGeorge V. Neville-Neil 79790fdff07SGeorge V. Neville-Neil m_hold = la->la_hold; 79890fdff07SGeorge V. Neville-Neil la->la_hold = NULL; 79990fdff07SGeorge V. Neville-Neil la->la_numheld = 0; 8006e6b3f7cSQing Li memcpy(&sa, L3_ADDR(la), sizeof(sa)); 801cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 802ede99017SGeorge V. Neville-Neil for (; m_hold != NULL; m_hold = m_hold_next) { 803e162ea60SGeorge V. Neville-Neil m_hold_next = m_hold->m_nextpkt; 804e162ea60SGeorge V. Neville-Neil m_hold->m_nextpkt = NULL; 80586bd0491SAndre Oppermann /* Avoid confusing lower layers. */ 80686bd0491SAndre Oppermann m_clrprotoflags(m_hold); 807e162ea60SGeorge V. Neville-Neil (*ifp->if_output)(ifp, m_hold, &sa, NULL); 8086e6b3f7cSQing Li } 809e162ea60SGeorge V. Neville-Neil } else 810e162ea60SGeorge V. Neville-Neil LLE_WUNLOCK(la); 811f4048639SBjoern A. Zeeb } 8126e6b3f7cSQing Li reply: 813b2a8ac7cSLuigi Rizzo if (op != ARPOP_REQUEST) 814b2a8ac7cSLuigi Rizzo goto drop; 81554fc657dSGeorge V. Neville-Neil ARPSTAT_INC(rxrequests); 8166e6b3f7cSQing Li 817df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) { 8188b07e49aSJulian Elischer /* Shortcut.. the receiving interface is the target. */ 819322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 820a9771948SGleb Smirnoff (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 821df8bae1dSRodney W. Grimes } else { 822897d75c9SQing Li struct llentry *lle = NULL; 823897d75c9SQing Li 824cd29a779SQing Li sin.sin_addr = itaddr; 825ea0c3776SAndrey V. Elsukov IF_AFDATA_RLOCK(ifp); 826cd29a779SQing Li lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin); 827ea0c3776SAndrey V. Elsukov IF_AFDATA_RUNLOCK(ifp); 828cd29a779SQing Li 829cd29a779SQing Li if ((lle != NULL) && (lle->la_flags & LLE_PUB)) { 830cd29a779SQing Li (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 831cd29a779SQing Li (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln); 832cd29a779SQing Li LLE_RUNLOCK(lle); 833cd29a779SQing Li } else { 834cd29a779SQing Li 835cd29a779SQing Li if (lle != NULL) 836cd29a779SQing Li LLE_RUNLOCK(lle); 837cd29a779SQing Li 838603724d3SBjoern A. Zeeb if (!V_arp_proxyall) 839b2a8ac7cSLuigi Rizzo goto drop; 84028e82295SGarrett Wollman 84128e82295SGarrett Wollman sin.sin_addr = itaddr; 8428b07e49aSJulian Elischer /* XXX MRT use table 0 for arp reply */ 8438b07e49aSJulian Elischer rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 844b2a8ac7cSLuigi Rizzo if (!rt) 845b2a8ac7cSLuigi Rizzo goto drop; 846897d75c9SQing Li 84728e82295SGarrett Wollman /* 84828e82295SGarrett Wollman * Don't send proxies for nodes on the same interface 84928e82295SGarrett Wollman * as this one came out of, or we'll get into a fight 85028e82295SGarrett Wollman * over who claims what Ether address. 85128e82295SGarrett Wollman */ 852897d75c9SQing Li if (!rt->rt_ifp || rt->rt_ifp == ifp) { 8534e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 854b2a8ac7cSLuigi Rizzo goto drop; 85528e82295SGarrett Wollman } 8564e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 857cc728227SDavid Malone 858897d75c9SQing Li (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 859cd29a779SQing Li (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 860897d75c9SQing Li 861cc728227SDavid Malone /* 862cc728227SDavid Malone * Also check that the node which sent the ARP packet 8636bccea7cSRebecca Cran * is on the interface we expect it to be on. This 864cc728227SDavid Malone * avoids ARP chaos if an interface is connected to the 865cc728227SDavid Malone * wrong network. 866cc728227SDavid Malone */ 867cc728227SDavid Malone sin.sin_addr = isaddr; 868cc728227SDavid Malone 8698b07e49aSJulian Elischer /* XXX MRT use table 0 for arp checks */ 8708b07e49aSJulian Elischer rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 871b2a8ac7cSLuigi Rizzo if (!rt) 872b2a8ac7cSLuigi Rizzo goto drop; 873322dcb8dSMax Khon if (rt->rt_ifp != ifp) { 8745d81d095SGleb Smirnoff ARP_LOG(LOG_INFO, "proxy: ignoring request" 8759bf40edeSBrooks Davis " from %s via %s, expecting %s\n", 8769bf40edeSBrooks Davis inet_ntoa(isaddr), ifp->if_xname, 8779bf40edeSBrooks Davis rt->rt_ifp->if_xname); 8784e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 879b2a8ac7cSLuigi Rizzo goto drop; 880cc728227SDavid Malone } 8814e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 882cc728227SDavid Malone 883ac234f93SGarrett Wollman #ifdef DEBUG_PROXY 884ea50c13eSGleb Smirnoff printf("arp: proxying for %s\n", inet_ntoa(itaddr)); 885ac234f93SGarrett Wollman #endif 886df8bae1dSRodney W. Grimes } 887cd29a779SQing Li } 888df8bae1dSRodney W. Grimes 889d0558157SBruce M Simpson if (itaddr.s_addr == myaddr.s_addr && 890d0558157SBruce M Simpson IN_LINKLOCAL(ntohl(itaddr.s_addr))) { 891d0558157SBruce M Simpson /* RFC 3927 link-local IPv4; always reply by broadcast. */ 892d0558157SBruce M Simpson #ifdef DEBUG_LINKLOCAL 893d0558157SBruce M Simpson printf("arp: sending reply for link-local addr %s\n", 894d0558157SBruce M Simpson inet_ntoa(itaddr)); 895d0558157SBruce M Simpson #endif 896d0558157SBruce M Simpson m->m_flags |= M_BCAST; 897d0558157SBruce M Simpson m->m_flags &= ~M_MCAST; 898d0558157SBruce M Simpson } else { 899d0558157SBruce M Simpson /* default behaviour; never reply by broadcast. */ 900d0558157SBruce M Simpson m->m_flags &= ~(M_BCAST|M_MCAST); 901d0558157SBruce M Simpson } 902322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 903322dcb8dSMax Khon (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 904322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REPLY); 905322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 90664bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 90764bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 9082303570fSAndrey V. Elsukov m->m_pkthdr.rcvif = NULL; 90964bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 91064bf80ceSMatthew N. Dodd sa.sa_len = 2; 91186bd0491SAndre Oppermann m_clrprotoflags(m); /* Avoid confusing lower layers. */ 912279aa3d4SKip Macy (*ifp->if_output)(ifp, m, &sa, NULL); 91354fc657dSGeorge V. Neville-Neil ARPSTAT_INC(txreplies); 914df8bae1dSRodney W. Grimes return; 915b2a8ac7cSLuigi Rizzo 916b2a8ac7cSLuigi Rizzo drop: 917b2a8ac7cSLuigi Rizzo m_freem(m); 918df8bae1dSRodney W. Grimes } 9191d5e9e22SEivind Eklund #endif 920df8bae1dSRodney W. Grimes 921dd2e4102SGarrett Wollman void 922f2565d68SRobert Watson arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 923dd2e4102SGarrett Wollman { 9246e6b3f7cSQing Li struct llentry *lle; 9256e6b3f7cSQing Li 92608b68b0eSGleb Smirnoff if (ifa->ifa_carp != NULL) 92708b68b0eSGleb Smirnoff return; 92808b68b0eSGleb Smirnoff 929ce9122fdSQing Li if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) { 930322dcb8dSMax Khon arprequest(ifp, &IA_SIN(ifa)->sin_addr, 931322dcb8dSMax Khon &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 9326e6b3f7cSQing Li /* 9336e6b3f7cSQing Li * interface address is considered static entry 9346e6b3f7cSQing Li * because the output of the arp utility shows 9356e6b3f7cSQing Li * that L2 entry as permanent 9366e6b3f7cSQing Li */ 9376e6b3f7cSQing Li IF_AFDATA_LOCK(ifp); 9386e6b3f7cSQing Li lle = lla_lookup(LLTABLE(ifp), (LLE_CREATE | LLE_IFADDR | LLE_STATIC), 9396e6b3f7cSQing Li (struct sockaddr *)IA_SIN(ifa)); 9406e6b3f7cSQing Li IF_AFDATA_UNLOCK(ifp); 9416e6b3f7cSQing Li if (lle == NULL) 9426e6b3f7cSQing Li log(LOG_INFO, "arp_ifinit: cannot create arp " 9436e6b3f7cSQing Li "entry for interface address\n"); 94486cd829dSKip Macy else 9456e6b3f7cSQing Li LLE_RUNLOCK(lle); 946ce9122fdSQing Li } 9476e6b3f7cSQing Li ifa->ifa_rtrequest = NULL; 948dd2e4102SGarrett Wollman } 949df5e1987SJonathan Lemon 950a9771948SGleb Smirnoff void 951f2565d68SRobert Watson arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 952a9771948SGleb Smirnoff { 953a9771948SGleb Smirnoff if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 954a9771948SGleb Smirnoff arprequest(ifp, &IA_SIN(ifa)->sin_addr, 955a9771948SGleb Smirnoff &IA_SIN(ifa)->sin_addr, enaddr); 9566e6b3f7cSQing Li ifa->ifa_rtrequest = NULL; 957a9771948SGleb Smirnoff } 958a9771948SGleb Smirnoff 9591ed81b73SMarko Zec static void 9601ed81b73SMarko Zec arp_init(void) 9611ed81b73SMarko Zec { 9621ed81b73SMarko Zec 963d4b5cae4SRobert Watson netisr_register(&arp_nh); 964df5e1987SJonathan Lemon } 965df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 966