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> 45cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h> 46ce02431fSDoug Rabson #include <sys/queue.h> 47885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h> 48885f1aa4SPoul-Henning Kamp #include <sys/systm.h> 49885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h> 50885f1aa4SPoul-Henning Kamp #include <sys/malloc.h> 51de34ad3fSJulian Elischer #include <sys/proc.h> 52cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h> 534458ac71SBruce Evans #include <sys/socket.h> 54885f1aa4SPoul-Henning Kamp #include <sys/syslog.h> 55df8bae1dSRodney W. Grimes 56df8bae1dSRodney W. Grimes #include <net/if.h> 5776039bc8SGleb Smirnoff #include <net/if_var.h> 58df8bae1dSRodney W. Grimes #include <net/if_dl.h> 59722012ccSJulian Elischer #include <net/if_types.h> 60748e0b0aSGarrett Wollman #include <net/netisr.h> 61b149dd6cSLarry Lile #include <net/if_llc.h> 62c8f8e9c1SJulian Elischer #include <net/ethernet.h> 635736e6fbSBjoern A. Zeeb #include <net/route.h> 64530c0060SRobert Watson #include <net/vnet.h> 65df8bae1dSRodney W. Grimes 66df8bae1dSRodney W. Grimes #include <netinet/in.h> 67df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 686e6b3f7cSQing Li #include <net/if_llatbl.h> 69df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 7083e521ecSBjoern A. Zeeb #ifdef INET 719963e8a5SWill Andrews #include <netinet/ip_carp.h> 729963e8a5SWill Andrews #endif 73df8bae1dSRodney W. Grimes 74322dcb8dSMax Khon #include <net/if_arc.h> 75fda82fc2SJulian Elischer #include <net/iso88025.h> 76fda82fc2SJulian Elischer 77aed55708SRobert Watson #include <security/mac/mac_framework.h> 78aed55708SRobert Watson 7947e8d432SGleb Smirnoff #define SIN(s) ((const struct sockaddr_in *)(s)) 80df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s) 81df8bae1dSRodney W. Grimes 82ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether); 836472ac3dSEd Schouten static SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 846472ac3dSEd Schouten static SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, ""); 85df8bae1dSRodney W. Grimes 86df8bae1dSRodney W. Grimes /* timer values */ 873e288e62SDimitry Andric static VNET_DEFINE(int, arpt_keep) = (20*60); /* once resolved, good for 20 88eddfbb76SRobert Watson * minutes */ 893e288e62SDimitry Andric static VNET_DEFINE(int, arp_maxtries) = 5; 903e288e62SDimitry Andric static VNET_DEFINE(int, arp_proxyall) = 0; 913e288e62SDimitry Andric static VNET_DEFINE(int, arpt_down) = 20; /* keep incomplete entries for 9293704ac5SQing Li * 20 seconds */ 935b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct arpstat, arpstat); /* ARP statistics, see if_arp.h */ 945b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(arpstat); 955b7cb97cSAndrey V. Elsukov 965b7cb97cSAndrey V. Elsukov #ifdef VIMAGE 975b7cb97cSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(arpstat); 985b7cb97cSAndrey V. Elsukov #endif /* VIMAGE */ 99e162ea60SGeorge V. Neville-Neil 1003e288e62SDimitry Andric static VNET_DEFINE(int, arp_maxhold) = 1; 101885f1aa4SPoul-Henning Kamp 1021e77c105SRobert Watson #define V_arpt_keep VNET(arpt_keep) 10393704ac5SQing Li #define V_arpt_down VNET(arpt_down) 1041e77c105SRobert Watson #define V_arp_maxtries VNET(arp_maxtries) 1051e77c105SRobert Watson #define V_arp_proxyall VNET(arp_proxyall) 106e162ea60SGeorge V. Neville-Neil #define V_arp_maxhold VNET(arp_maxhold) 107885f1aa4SPoul-Henning Kamp 1086df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_VNET | CTLFLAG_RW, 109eddfbb76SRobert Watson &VNET_NAME(arpt_keep), 0, 110eddfbb76SRobert Watson "ARP entry lifetime in seconds"); 1116df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_VNET | CTLFLAG_RW, 112eddfbb76SRobert Watson &VNET_NAME(arp_maxtries), 0, 1138b615593SMarko Zec "ARP resolution attempts before returning error"); 1146df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_VNET | CTLFLAG_RW, 115eddfbb76SRobert Watson &VNET_NAME(arp_proxyall), 0, 1168b615593SMarko Zec "Enable proxy ARP for all suitable requests"); 1176df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, wait, CTLFLAG_VNET | CTLFLAG_RW, 118e162ea60SGeorge V. Neville-Neil &VNET_NAME(arpt_down), 0, 119e162ea60SGeorge V. Neville-Neil "Incomplete ARP entry lifetime in seconds"); 1205b7cb97cSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_link_ether_arp, OID_AUTO, stats, struct arpstat, 1215b7cb97cSAndrey V. Elsukov arpstat, "ARP statistics (struct arpstat, net/if_arp.h)"); 1226df8a710SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxhold, CTLFLAG_VNET | CTLFLAG_RW, 123e162ea60SGeorge V. Neville-Neil &VNET_NAME(arp_maxhold), 0, 124e162ea60SGeorge V. Neville-Neil "Number of packets to hold per ARP entry"); 125885f1aa4SPoul-Henning Kamp 1264d77a549SAlfred Perlstein static void arp_init(void); 1271cafed39SJonathan Lemon static void arpintr(struct mbuf *); 1284d77a549SAlfred Perlstein static void arptimer(void *); 1291d5e9e22SEivind Eklund #ifdef INET 1304d77a549SAlfred Perlstein static void in_arpinput(struct mbuf *); 1311d5e9e22SEivind Eklund #endif 13228e82295SGarrett Wollman 133d4b5cae4SRobert Watson static const struct netisr_handler arp_nh = { 134d4b5cae4SRobert Watson .nh_name = "arp", 135d4b5cae4SRobert Watson .nh_handler = arpintr, 136d4b5cae4SRobert Watson .nh_proto = NETISR_ARP, 137d4b5cae4SRobert Watson .nh_policy = NETISR_POLICY_SOURCE, 138d4b5cae4SRobert Watson }; 139d4b5cae4SRobert Watson 1406e6b3f7cSQing Li #ifdef AF_INET 141df8bae1dSRodney W. Grimes /* 142237bf7f7SGleb Smirnoff * called by in_scrubprefix() to remove entry from the table when 1436e6b3f7cSQing Li * the interface goes away 1446e6b3f7cSQing Li */ 1456e6b3f7cSQing Li void 1466e6b3f7cSQing Li arp_ifscrub(struct ifnet *ifp, uint32_t addr) 1476e6b3f7cSQing Li { 1486e6b3f7cSQing Li struct sockaddr_in addr4; 1496e6b3f7cSQing Li 1506e6b3f7cSQing Li bzero((void *)&addr4, sizeof(addr4)); 1516e6b3f7cSQing Li addr4.sin_len = sizeof(addr4); 1526e6b3f7cSQing Li addr4.sin_family = AF_INET; 1536e6b3f7cSQing Li addr4.sin_addr.s_addr = addr; 1541b5aa92cSAndrey V. Elsukov IF_AFDATA_WLOCK(ifp); 155b4b1367aSAlexander V. Chernikov lla_delete(LLTABLE(ifp), LLE_IFADDR, (struct sockaddr *)&addr4); 1561b5aa92cSAndrey V. Elsukov IF_AFDATA_WUNLOCK(ifp); 1576e6b3f7cSQing Li } 1586e6b3f7cSQing Li #endif 1596e6b3f7cSQing Li 1606e6b3f7cSQing Li /* 1616e6b3f7cSQing Li * Timeout routine. Age arp_tab entries periodically. 162df8bae1dSRodney W. Grimes */ 163df8bae1dSRodney W. Grimes static void 1641daaa65dSGleb Smirnoff arptimer(void *arg) 165df8bae1dSRodney W. Grimes { 166ea537929SGleb Smirnoff struct llentry *lle = (struct llentry *)arg; 1676e6b3f7cSQing Li struct ifnet *ifp; 168df8bae1dSRodney W. Grimes 169ea537929SGleb Smirnoff if (lle->la_flags & LLE_STATIC) { 1702575fbb8SRandall Stewart return; 1712575fbb8SRandall Stewart } 1722575fbb8SRandall Stewart LLE_WLOCK(lle); 173*0447c136SAlexander V. Chernikov if (callout_pending(&lle->lle_timer)) { 1742575fbb8SRandall Stewart /* 1752575fbb8SRandall Stewart * Here we are a bit odd here in the treatment of 1762575fbb8SRandall Stewart * active/pending. If the pending bit is set, it got 1772575fbb8SRandall Stewart * rescheduled before I ran. The active 1782575fbb8SRandall Stewart * bit we ignore, since if it was stopped 1792575fbb8SRandall Stewart * in ll_tablefree() and was currently running 1802575fbb8SRandall Stewart * it would have return 0 so the code would 1812575fbb8SRandall Stewart * not have deleted it since the callout could 1822575fbb8SRandall Stewart * not be stopped so we want to go through 1832575fbb8SRandall Stewart * with the delete here now. If the callout 1842575fbb8SRandall Stewart * was restarted, the pending bit will be back on and 1852575fbb8SRandall Stewart * we just want to bail since the callout_reset would 1862575fbb8SRandall Stewart * return 1 and our reference would have been removed 1872575fbb8SRandall Stewart * by arpresolve() below. 1882575fbb8SRandall Stewart */ 189ea537929SGleb Smirnoff LLE_WUNLOCK(lle); 190ea537929SGleb Smirnoff return; 191ea537929SGleb Smirnoff } 1926e6b3f7cSQing Li ifp = lle->lle_tbl->llt_ifp; 19354fc657dSGeorge V. Neville-Neil CURVNET_SET(ifp->if_vnet); 19409fe6320SNavdeep Parhar 195e364d8c4SNavdeep Parhar if ((lle->la_flags & LLE_DELETED) == 0) { 19609fe6320SNavdeep Parhar int evt; 19709fe6320SNavdeep Parhar 19809fe6320SNavdeep Parhar if (lle->la_flags & LLE_VALID) 19909fe6320SNavdeep Parhar evt = LLENTRY_EXPIRED; 20009fe6320SNavdeep Parhar else 20109fe6320SNavdeep Parhar evt = LLENTRY_TIMEDOUT; 20209fe6320SNavdeep Parhar EVENTHANDLER_INVOKE(lle_event, lle, evt); 20309fe6320SNavdeep Parhar } 20409fe6320SNavdeep Parhar 205*0447c136SAlexander V. Chernikov callout_stop(&lle->lle_timer); 206ea537929SGleb Smirnoff 207ea537929SGleb Smirnoff /* XXX: LOR avoidance. We still have ref on lle. */ 208ea537929SGleb Smirnoff LLE_WUNLOCK(lle); 209ea537929SGleb Smirnoff IF_AFDATA_LOCK(ifp); 210ea537929SGleb Smirnoff LLE_WLOCK(lle); 211ea537929SGleb Smirnoff 212b1ec2940SGleb Smirnoff /* Guard against race with other llentry_free(). */ 213b1ec2940SGleb Smirnoff if (lle->la_flags & LLE_LINKED) { 214b1ec2940SGleb Smirnoff size_t pkts_dropped; 215b1ec2940SGleb Smirnoff 216ea537929SGleb Smirnoff LLE_REMREF(lle); 217e162ea60SGeorge V. Neville-Neil pkts_dropped = llentry_free(lle); 218e162ea60SGeorge V. Neville-Neil ARPSTAT_ADD(dropped, pkts_dropped); 219b1ec2940SGleb Smirnoff } else 220b1ec2940SGleb Smirnoff LLE_FREE_LOCKED(lle); 221b1ec2940SGleb Smirnoff 222b1ec2940SGleb Smirnoff IF_AFDATA_UNLOCK(ifp); 223b1ec2940SGleb Smirnoff 22454fc657dSGeorge V. Neville-Neil ARPSTAT_INC(timeouts); 225b1ec2940SGleb Smirnoff 22654fc657dSGeorge V. Neville-Neil CURVNET_RESTORE(); 227df8bae1dSRodney W. Grimes } 228df8bae1dSRodney W. Grimes 229df8bae1dSRodney W. Grimes /* 230df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies: 231df8bae1dSRodney W. Grimes * - arp header source ip address 232df8bae1dSRodney W. Grimes * - arp header target ip address 233df8bae1dSRodney W. Grimes * - arp header source ethernet address 234df8bae1dSRodney W. Grimes */ 2356e6b3f7cSQing Li void 23647e8d432SGleb Smirnoff arprequest(struct ifnet *ifp, const struct in_addr *sip, 23747e8d432SGleb Smirnoff const struct in_addr *tip, u_char *enaddr) 238df8bae1dSRodney W. Grimes { 239e952fa39SMatthew N. Dodd struct mbuf *m; 240e952fa39SMatthew N. Dodd struct arphdr *ah; 241df8bae1dSRodney W. Grimes struct sockaddr sa; 242aa24ae3cSGleb Smirnoff u_char *carpaddr = NULL; 243df8bae1dSRodney W. Grimes 2446e6b3f7cSQing Li if (sip == NULL) { 2456e6b3f7cSQing Li /* 2466e6b3f7cSQing Li * The caller did not supply a source address, try to find 2476e6b3f7cSQing Li * a compatible one among those assigned to this interface. 2486e6b3f7cSQing Li */ 2496e6b3f7cSQing Li struct ifaddr *ifa; 2506e6b3f7cSQing Li 251aa24ae3cSGleb Smirnoff IF_ADDR_RLOCK(ifp); 2526e6b3f7cSQing Li TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 253aa24ae3cSGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_INET) 2546e6b3f7cSQing Li continue; 255aa24ae3cSGleb Smirnoff 256aa24ae3cSGleb Smirnoff if (ifa->ifa_carp) { 257aa24ae3cSGleb Smirnoff if ((*carp_iamatch_p)(ifa, &carpaddr) == 0) 258aa24ae3cSGleb Smirnoff continue; 259aa24ae3cSGleb Smirnoff sip = &IA_SIN(ifa)->sin_addr; 260aa24ae3cSGleb Smirnoff } else { 261aa24ae3cSGleb Smirnoff carpaddr = NULL; 262aa24ae3cSGleb Smirnoff sip = &IA_SIN(ifa)->sin_addr; 263aa24ae3cSGleb Smirnoff } 264aa24ae3cSGleb Smirnoff 2656e6b3f7cSQing Li if (0 == ((sip->s_addr ^ tip->s_addr) & 266aa24ae3cSGleb Smirnoff IA_MASKSIN(ifa)->sin_addr.s_addr)) 2676e6b3f7cSQing Li break; /* found it. */ 2686e6b3f7cSQing Li } 269aa24ae3cSGleb Smirnoff IF_ADDR_RUNLOCK(ifp); 2706e6b3f7cSQing Li if (sip == NULL) { 2716e6b3f7cSQing Li printf("%s: cannot find matching address\n", __func__); 2726e6b3f7cSQing Li return; 2736e6b3f7cSQing Li } 2746e6b3f7cSQing Li } 275aa24ae3cSGleb Smirnoff if (enaddr == NULL) 276aa24ae3cSGleb Smirnoff enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp); 2776e6b3f7cSQing Li 278eb1b1807SGleb Smirnoff if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL) 279df8bae1dSRodney W. Grimes return; 28064bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) + 281546451a2SGleb Smirnoff 2 * ifp->if_addrlen; 28264bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 283ed6a66caSRobert Watson M_ALIGN(m, m->m_len); 28464bf80ceSMatthew N. Dodd ah = mtod(m, struct arphdr *); 28564bf80ceSMatthew N. Dodd bzero((caddr_t)ah, m->m_len); 28619527d3eSRobert Watson #ifdef MAC 287b9b0dac3SRobert Watson mac_netinet_arp_send(ifp, m); 28819527d3eSRobert Watson #endif 289322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); 290322dcb8dSMax Khon ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 291322dcb8dSMax Khon ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 292322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REQUEST); 29347e8d432SGleb Smirnoff bcopy(enaddr, ar_sha(ah), ah->ar_hln); 29447e8d432SGleb Smirnoff bcopy(sip, ar_spa(ah), ah->ar_pln); 29547e8d432SGleb Smirnoff bcopy(tip, ar_tpa(ah), ah->ar_pln); 29664bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 29764bf80ceSMatthew N. Dodd sa.sa_len = 2; 29864bf80ceSMatthew N. Dodd m->m_flags |= M_BCAST; 29986bd0491SAndre Oppermann m_clrprotoflags(m); /* Avoid confusing lower layers. */ 300279aa3d4SKip Macy (*ifp->if_output)(ifp, m, &sa, NULL); 30154fc657dSGeorge V. Neville-Neil ARPSTAT_INC(txrequests); 302df8bae1dSRodney W. Grimes } 303df8bae1dSRodney W. Grimes 304df8bae1dSRodney W. Grimes /* 305cd46a114SLuigi Rizzo * Resolve an IP address into an ethernet address. 306cd46a114SLuigi Rizzo * On input: 307cd46a114SLuigi Rizzo * ifp is the interface we use 30874860d4fSAlexander V. Chernikov * is_gw != if @dst represents gateway to some destination 309b6ae6984SJulian Elischer * m is the mbuf. May be NULL if we don't have a packet. 310b6ae6984SJulian Elischer * dst is the next hop, 311cd46a114SLuigi Rizzo * desten is where we want the address. 31274860d4fSAlexander V. Chernikov * flags returns lle entry flags. 313cd46a114SLuigi Rizzo * 31474860d4fSAlexander V. Chernikov * On success, desten and flags are filled in and the function returns 0; 315cd46a114SLuigi Rizzo * If the packet must be held pending resolution, we return EWOULDBLOCK 316cd46a114SLuigi Rizzo * On other errors, we return the corresponding error code. 317b6ae6984SJulian Elischer * Note that m_freem() handles NULL. 318df8bae1dSRodney W. Grimes */ 319df8bae1dSRodney W. Grimes int 32074860d4fSAlexander V. Chernikov arpresolve(struct ifnet *ifp, int is_gw, struct mbuf *m, 32174860d4fSAlexander V. Chernikov const struct sockaddr *dst, u_char *desten, uint32_t *pflags) 322df8bae1dSRodney W. Grimes { 3236e6b3f7cSQing Li struct llentry *la = 0; 32400a46b31SKip Macy u_int flags = 0; 325e162ea60SGeorge V. Neville-Neil struct mbuf *curr = NULL; 326e162ea60SGeorge V. Neville-Neil struct mbuf *next = NULL; 327b4b1367aSAlexander V. Chernikov int create, error, renew; 328df8bae1dSRodney W. Grimes 32974860d4fSAlexander V. Chernikov if (pflags != NULL) 33074860d4fSAlexander V. Chernikov *pflags = 0; 33174860d4fSAlexander V. Chernikov 332b4b1367aSAlexander V. Chernikov create = 0; 3336e6b3f7cSQing Li if (m != NULL) { 334b6ae6984SJulian Elischer if (m->m_flags & M_BCAST) { 335b6ae6984SJulian Elischer /* broadcast */ 336b6ae6984SJulian Elischer (void)memcpy(desten, 337b6ae6984SJulian Elischer ifp->if_broadcastaddr, ifp->if_addrlen); 338cd46a114SLuigi Rizzo return (0); 339df8bae1dSRodney W. Grimes } 340d63e657cSAlexander V. Chernikov if (m->m_flags & M_MCAST) { 341b6ae6984SJulian Elischer /* multicast */ 342df8bae1dSRodney W. Grimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 343cd46a114SLuigi Rizzo return (0); 344df8bae1dSRodney W. Grimes } 345b6ae6984SJulian Elischer } 3466e6b3f7cSQing Li retry: 34700a46b31SKip Macy IF_AFDATA_RLOCK(ifp); 3486e6b3f7cSQing Li la = lla_lookup(LLTABLE(ifp), flags, dst); 34900a46b31SKip Macy IF_AFDATA_RUNLOCK(ifp); 35000a46b31SKip Macy if ((la == NULL) && ((flags & LLE_EXCLUSIVE) == 0) 35100a46b31SKip Macy && ((ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0)) { 352b4b1367aSAlexander V. Chernikov create = 1; 353b4b1367aSAlexander V. Chernikov flags |= LLE_EXCLUSIVE; 35400a46b31SKip Macy IF_AFDATA_WLOCK(ifp); 355b4b1367aSAlexander V. Chernikov la = lla_create(LLTABLE(ifp), flags, dst); 35600a46b31SKip Macy IF_AFDATA_WUNLOCK(ifp); 35700a46b31SKip Macy } 3581ed7bf1eSGleb Smirnoff if (la == NULL) { 359b4b1367aSAlexander V. Chernikov if (create != 0) 3601ed7bf1eSGleb Smirnoff log(LOG_DEBUG, 361743c072aSAlan Somers "arpresolve: can't allocate llinfo for %s on %s\n", 362743c072aSAlan Somers inet_ntoa(SIN(dst)->sin_addr), ifp->if_xname); 3631ed7bf1eSGleb Smirnoff m_freem(m); 3646e6b3f7cSQing Li return (EINVAL); 3651ed7bf1eSGleb Smirnoff } 366a20e2538SGleb Smirnoff 3676e6b3f7cSQing Li if ((la->la_flags & LLE_VALID) && 368a98c06f1SGleb Smirnoff ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) { 3696e6b3f7cSQing Li bcopy(&la->ll_addr, desten, ifp->if_addrlen); 370f0516b3cSErmal Luçi renew = 0; 371f0f3379eSOrion Hodson /* 372f0f3379eSOrion Hodson * If entry has an expiry time and it is approaching, 3736e6b3f7cSQing Li * see if we need to send an ARP request within this 3746e6b3f7cSQing Li * arpt_down interval. 375f0f3379eSOrion Hodson */ 3766e6b3f7cSQing Li if (!(la->la_flags & LLE_STATIC) && 377a98c06f1SGleb Smirnoff time_uptime + la->la_preempt > la->la_expire) { 378f0516b3cSErmal Luçi renew = 1; 379022695f8SOrion Hodson la->la_preempt--; 380f0f3379eSOrion Hodson } 381f0f3379eSOrion Hodson 38274860d4fSAlexander V. Chernikov if (pflags != NULL) 38374860d4fSAlexander V. Chernikov *pflags = la->la_flags; 384f0516b3cSErmal Luçi 385f0516b3cSErmal Luçi if (flags & LLE_EXCLUSIVE) 386f0516b3cSErmal Luçi LLE_WUNLOCK(la); 387f0516b3cSErmal Luçi else 388f0516b3cSErmal Luçi LLE_RUNLOCK(la); 389f0516b3cSErmal Luçi 390f0516b3cSErmal Luçi if (renew == 1) 391f0516b3cSErmal Luçi arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL); 392f0516b3cSErmal Luçi 393f0516b3cSErmal Luçi return (0); 394df8bae1dSRodney W. Grimes } 3956e6b3f7cSQing Li 3966e6b3f7cSQing Li if (la->la_flags & LLE_STATIC) { /* should not happen! */ 3976e6b3f7cSQing Li log(LOG_DEBUG, "arpresolve: ouch, empty static llinfo for %s\n", 3986e6b3f7cSQing Li inet_ntoa(SIN(dst)->sin_addr)); 39947891de1SRuslan Ermilov m_freem(m); 4006e6b3f7cSQing Li error = EINVAL; 4016e6b3f7cSQing Li goto done; 4026e6b3f7cSQing Li } 4036e6b3f7cSQing Li 404a98c06f1SGleb Smirnoff renew = (la->la_asked == 0 || la->la_expire != time_uptime); 4056e6b3f7cSQing Li if ((renew || m != NULL) && (flags & LLE_EXCLUSIVE) == 0) { 4066e6b3f7cSQing Li flags |= LLE_EXCLUSIVE; 4076e6b3f7cSQing Li LLE_RUNLOCK(la); 4086e6b3f7cSQing Li goto retry; 40947891de1SRuslan Ermilov } 41008aadfbbSJonathan Lemon /* 411df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address 412e162ea60SGeorge V. Neville-Neil * response yet. Add the mbuf to the list, dropping 413e162ea60SGeorge V. Neville-Neil * the oldest packet if we have exceeded the system 414e162ea60SGeorge V. Neville-Neil * setting. 415df8bae1dSRodney W. Grimes */ 4166e6b3f7cSQing Li if (m != NULL) { 417e162ea60SGeorge V. Neville-Neil if (la->la_numheld >= V_arp_maxhold) { 41854fc657dSGeorge V. Neville-Neil if (la->la_hold != NULL) { 419e162ea60SGeorge V. Neville-Neil next = la->la_hold->m_nextpkt; 420df8bae1dSRodney W. Grimes m_freem(la->la_hold); 421e162ea60SGeorge V. Neville-Neil la->la_hold = next; 422e162ea60SGeorge V. Neville-Neil la->la_numheld--; 42354fc657dSGeorge V. Neville-Neil ARPSTAT_INC(dropped); 42454fc657dSGeorge V. Neville-Neil } 425e162ea60SGeorge V. Neville-Neil } 426e162ea60SGeorge V. Neville-Neil if (la->la_hold != NULL) { 427e162ea60SGeorge V. Neville-Neil curr = la->la_hold; 428e162ea60SGeorge V. Neville-Neil while (curr->m_nextpkt != NULL) 429e162ea60SGeorge V. Neville-Neil curr = curr->m_nextpkt; 430e162ea60SGeorge V. Neville-Neil curr->m_nextpkt = m; 431e162ea60SGeorge V. Neville-Neil } else 432df8bae1dSRodney W. Grimes la->la_hold = m; 433e162ea60SGeorge V. Neville-Neil la->la_numheld++; 4346e6b3f7cSQing Li if (renew == 0 && (flags & LLE_EXCLUSIVE)) { 4356e6b3f7cSQing Li flags &= ~LLE_EXCLUSIVE; 4366e6b3f7cSQing Li LLE_DOWNGRADE(la); 43758505389SKip Macy } 438e1ff74c5SGleb Smirnoff 4396e6b3f7cSQing Li } 440e1ff74c5SGleb Smirnoff /* 441e1ff74c5SGleb Smirnoff * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 442e1ff74c5SGleb Smirnoff * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 443e1ff74c5SGleb Smirnoff * if we have already sent arp_maxtries ARP requests. Retransmit the 444e1ff74c5SGleb Smirnoff * ARP request, but not faster than one request per second. 445e1ff74c5SGleb Smirnoff */ 446603724d3SBjoern A. Zeeb if (la->la_asked < V_arp_maxtries) 447e1ff74c5SGleb Smirnoff error = EWOULDBLOCK; /* First request. */ 448e1ff74c5SGleb Smirnoff else 44974860d4fSAlexander V. Chernikov error = is_gw != 0 ? EHOSTUNREACH : EHOSTDOWN; 450e1ff74c5SGleb Smirnoff 4516e6b3f7cSQing Li if (renew) { 452becba438SBjoern A. Zeeb int canceled; 453becba438SBjoern A. Zeeb 4546e6b3f7cSQing Li LLE_ADDREF(la); 455a98c06f1SGleb Smirnoff la->la_expire = time_uptime; 456*0447c136SAlexander V. Chernikov canceled = callout_reset(&la->lle_timer, hz * V_arpt_down, 457becba438SBjoern A. Zeeb arptimer, la); 458becba438SBjoern A. Zeeb if (canceled) 459becba438SBjoern A. Zeeb LLE_REMREF(la); 46095ebcabeSMaxim Konovalov la->la_asked++; 4616e6b3f7cSQing Li LLE_WUNLOCK(la); 462aa24ae3cSGleb Smirnoff arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL); 4636e6b3f7cSQing Li return (error); 4646e6b3f7cSQing Li } 4656e6b3f7cSQing Li done: 4666e6b3f7cSQing Li if (flags & LLE_EXCLUSIVE) 4676e6b3f7cSQing Li LLE_WUNLOCK(la); 4686e6b3f7cSQing Li else 4696e6b3f7cSQing Li LLE_RUNLOCK(la); 470e1ff74c5SGleb Smirnoff return (error); 471df8bae1dSRodney W. Grimes } 472df8bae1dSRodney W. Grimes 473df8bae1dSRodney W. Grimes /* 474df8bae1dSRodney W. Grimes * Common length and type checks are done here, 475df8bae1dSRodney W. Grimes * then the protocol-specific routine is called. 476df8bae1dSRodney W. Grimes */ 477885f1aa4SPoul-Henning Kamp static void 4781cafed39SJonathan Lemon arpintr(struct mbuf *m) 479df8bae1dSRodney W. Grimes { 4801cafed39SJonathan Lemon struct arphdr *ar; 481df8bae1dSRodney W. Grimes 48276ec7b2fSRobert Watson if (m->m_len < sizeof(struct arphdr) && 48384365e2bSMatthew Dillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 484c9168718SGleb Smirnoff log(LOG_NOTICE, "arp: runt packet -- m_pullup failed\n"); 4851cafed39SJonathan Lemon return; 48676ec7b2fSRobert Watson } 48776ec7b2fSRobert Watson ar = mtod(m, struct arphdr *); 48876ec7b2fSRobert Watson 4891cafed39SJonathan Lemon if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 4901cafed39SJonathan Lemon ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 491b8b33234SDoug Rabson ntohs(ar->ar_hrd) != ARPHRD_ARCNET && 492e4cd31ddSJeff Roberson ntohs(ar->ar_hrd) != ARPHRD_IEEE1394 && 493e4cd31ddSJeff Roberson ntohs(ar->ar_hrd) != ARPHRD_INFINIBAND) { 49461905171SGleb Smirnoff log(LOG_NOTICE, "arp: unknown hardware address format (0x%2D)" 49561905171SGleb Smirnoff " (from %*D to %*D)\n", (unsigned char *)&ar->ar_hrd, "", 49661905171SGleb Smirnoff ETHER_ADDR_LEN, (u_char *)ar_sha(ar), ":", 49761905171SGleb Smirnoff ETHER_ADDR_LEN, (u_char *)ar_tha(ar), ":"); 49876ec7b2fSRobert Watson m_freem(m); 4991cafed39SJonathan Lemon return; 50076ec7b2fSRobert Watson } 50176ec7b2fSRobert Watson 50231175791SRuslan Ermilov if (m->m_len < arphdr_len(ar)) { 50378e2d2bdSRuslan Ermilov if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 504c9168718SGleb Smirnoff log(LOG_NOTICE, "arp: runt packet\n"); 50576ec7b2fSRobert Watson m_freem(m); 5061cafed39SJonathan Lemon return; 50776ec7b2fSRobert Watson } 50878e2d2bdSRuslan Ermilov ar = mtod(m, struct arphdr *); 50978e2d2bdSRuslan Ermilov } 510df8bae1dSRodney W. Grimes 51154fc657dSGeorge V. Neville-Neil ARPSTAT_INC(received); 512df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) { 5131d5e9e22SEivind Eklund #ifdef INET 514df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 515df8bae1dSRodney W. Grimes in_arpinput(m); 5161cafed39SJonathan Lemon return; 5171d5e9e22SEivind Eklund #endif 518df8bae1dSRodney W. Grimes } 519df8bae1dSRodney W. Grimes m_freem(m); 520df8bae1dSRodney W. Grimes } 521df8bae1dSRodney W. Grimes 5221d5e9e22SEivind Eklund #ifdef INET 523df8bae1dSRodney W. Grimes /* 524df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet. 525df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826. 526df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender 527df8bae1dSRodney W. Grimes * protocol address, to catch impersonators. 528df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol: 529df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 530df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us, 531df8bae1dSRodney W. Grimes * and also sent them in response to IP replies. 532df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive 533df8bae1dSRodney W. Grimes * trailer packets. 534df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 535df8bae1dSRodney W. Grimes * but formerly didn't normally send requests. 536df8bae1dSRodney W. Grimes */ 5373269187dSAlfred Perlstein static int log_arp_wrong_iface = 1; 538e3d123d6SAlfred Perlstein static int log_arp_movements = 1; 53939393906SGleb Smirnoff static int log_arp_permanent_modify = 1; 540478df1d5SGleb Smirnoff static int allow_multicast = 0; 5415d81d095SGleb Smirnoff static struct timeval arp_lastlog; 5425d81d095SGleb Smirnoff static int arp_curpps; 5435d81d095SGleb Smirnoff static int arp_maxpps = 1; 5443269187dSAlfred Perlstein 5453269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 5463269187dSAlfred Perlstein &log_arp_wrong_iface, 0, 5473269187dSAlfred Perlstein "log arp packets arriving on the wrong interface"); 548e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 549e3d123d6SAlfred Perlstein &log_arp_movements, 0, 55075ce3221SAlfred Perlstein "log arp replies from MACs different than the one in the cache"); 55139393906SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, 55239393906SGleb Smirnoff &log_arp_permanent_modify, 0, 55339393906SGleb Smirnoff "log arp replies from MACs different than the one in the permanent arp entry"); 554478df1d5SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW, 555478df1d5SGleb Smirnoff &allow_multicast, 0, "accept multicast addresses"); 5565d81d095SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_log_per_second, 5575d81d095SGleb Smirnoff CTLFLAG_RW, &arp_maxpps, 0, 5585d81d095SGleb Smirnoff "Maximum number of remotely triggered ARP messages that can be " 5595d81d095SGleb Smirnoff "logged per second"); 5605d81d095SGleb Smirnoff 5615d81d095SGleb Smirnoff #define ARP_LOG(pri, ...) do { \ 5625d81d095SGleb Smirnoff if (ppsratecheck(&arp_lastlog, &arp_curpps, arp_maxpps)) \ 5635d81d095SGleb Smirnoff log((pri), "arp: " __VA_ARGS__); \ 5645d81d095SGleb Smirnoff } while (0) 5653269187dSAlfred Perlstein 566df8bae1dSRodney W. Grimes static void 567f2565d68SRobert Watson in_arpinput(struct mbuf *m) 568df8bae1dSRodney W. Grimes { 569cc0a3c8cSAndrey V. Elsukov struct rm_priotracker in_ifa_tracker; 570e952fa39SMatthew N. Dodd struct arphdr *ah; 571e952fa39SMatthew N. Dodd struct ifnet *ifp = m->m_pkthdr.rcvif; 5726e6b3f7cSQing Li struct llentry *la = NULL; 573e952fa39SMatthew N. Dodd struct rtentry *rt; 574ca925d9cSJonathan Lemon struct ifaddr *ifa; 575ca925d9cSJonathan Lemon struct in_ifaddr *ia; 576df8bae1dSRodney W. Grimes struct sockaddr sa; 577df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr; 578a9771948SGleb Smirnoff u_int8_t *enaddr = NULL; 5796e6b3f7cSQing Li int op, flags; 580322dcb8dSMax Khon int req_len; 58180b11ee4SPhilip Paeps int bridged = 0, is_bridge = 0; 582b4b1367aSAlexander V. Chernikov int carped, create; 5838e7e854cSKip Macy struct sockaddr_in sin; 5848e7e854cSKip Macy sin.sin_len = sizeof(struct sockaddr_in); 5858e7e854cSKip Macy sin.sin_family = AF_INET; 58629910a5aSKip Macy sin.sin_addr.s_addr = 0; 587df8bae1dSRodney W. Grimes 58874948aa6SAndrew Thompson if (ifp->if_bridge) 5898f867517SAndrew Thompson bridged = 1; 59080b11ee4SPhilip Paeps if (ifp->if_type == IFT_BRIDGE) 59180b11ee4SPhilip Paeps is_bridge = 1; 5928f867517SAndrew Thompson 593322dcb8dSMax Khon req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 594322dcb8dSMax Khon if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 5955d81d095SGleb Smirnoff ARP_LOG(LOG_NOTICE, "runt packet -- m_pullup failed\n"); 5964cbc8ad1SYaroslav Tykhiy return; 5974cbc8ad1SYaroslav Tykhiy } 5984cbc8ad1SYaroslav Tykhiy 599322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 60009d3f895SGeorge V. Neville-Neil /* 60109d3f895SGeorge V. Neville-Neil * ARP is only for IPv4 so we can reject packets with 60209d3f895SGeorge V. Neville-Neil * a protocol length not equal to an IPv4 address. 60309d3f895SGeorge V. Neville-Neil */ 60409d3f895SGeorge V. Neville-Neil if (ah->ar_pln != sizeof(struct in_addr)) { 6055d81d095SGleb Smirnoff ARP_LOG(LOG_NOTICE, "requested protocol length != %zu\n", 60609d3f895SGeorge V. Neville-Neil sizeof(struct in_addr)); 607414676baSGleb Smirnoff goto drop; 60809d3f895SGeorge V. Neville-Neil } 60909d3f895SGeorge V. Neville-Neil 610478df1d5SGleb Smirnoff if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) { 6115d81d095SGleb Smirnoff ARP_LOG(LOG_NOTICE, "%*D is multicast\n", 612c9168718SGleb Smirnoff ifp->if_addrlen, (u_char *)ar_sha(ah), ":"); 613414676baSGleb Smirnoff goto drop; 61409d3f895SGeorge V. Neville-Neil } 61509d3f895SGeorge V. Neville-Neil 616322dcb8dSMax Khon op = ntohs(ah->ar_op); 617322dcb8dSMax Khon (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 618322dcb8dSMax Khon (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 61932439868SGleb Smirnoff 62054fc657dSGeorge V. Neville-Neil if (op == ARPOP_REPLY) 62154fc657dSGeorge V. Neville-Neil ARPSTAT_INC(rxreplies); 62254fc657dSGeorge V. Neville-Neil 623ca925d9cSJonathan Lemon /* 624ca925d9cSJonathan Lemon * For a bridge, we want to check the address irrespective 625ca925d9cSJonathan Lemon * of the receive interface. (This will change slightly 626ca925d9cSJonathan Lemon * when we have clusters of interfaces). 627ca925d9cSJonathan Lemon */ 628cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 6292ef4a436SGleb Smirnoff LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 63096561547SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) || 6316e6b3f7cSQing Li ia->ia_ifp == ifp) && 63208b68b0eSGleb Smirnoff itaddr.s_addr == ia->ia_addr.sin_addr.s_addr && 63308b68b0eSGleb Smirnoff (ia->ia_ifa.ifa_carp == NULL || 63408b68b0eSGleb Smirnoff (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) { 63509d54778SRobert Watson ifa_ref(&ia->ia_ifa); 636cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 6372ef4a436SGleb Smirnoff goto match; 6382ef4a436SGleb Smirnoff } 6392ef4a436SGleb Smirnoff } 640ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 64196561547SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) || 6426e6b3f7cSQing Li ia->ia_ifp == ifp) && 64309d54778SRobert Watson isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 64409d54778SRobert Watson ifa_ref(&ia->ia_ifa); 645cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 646ca925d9cSJonathan Lemon goto match; 64709d54778SRobert Watson } 64880b11ee4SPhilip Paeps 64980b11ee4SPhilip Paeps #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \ 65080b11ee4SPhilip Paeps (ia->ia_ifp->if_bridge == ifp->if_softc && \ 65180b11ee4SPhilip Paeps !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \ 65280b11ee4SPhilip Paeps addr == ia->ia_addr.sin_addr.s_addr) 65380b11ee4SPhilip Paeps /* 65480b11ee4SPhilip Paeps * Check the case when bridge shares its MAC address with 65580b11ee4SPhilip Paeps * some of its children, so packets are claimed by bridge 65680b11ee4SPhilip Paeps * itself (bridge_input() does it first), but they are really 65780b11ee4SPhilip Paeps * meant to be destined to the bridge member. 65880b11ee4SPhilip Paeps */ 65980b11ee4SPhilip Paeps if (is_bridge) { 66080b11ee4SPhilip Paeps LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 66180b11ee4SPhilip Paeps if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) { 66209d54778SRobert Watson ifa_ref(&ia->ia_ifa); 66380b11ee4SPhilip Paeps ifp = ia->ia_ifp; 664cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 66580b11ee4SPhilip Paeps goto match; 66680b11ee4SPhilip Paeps } 66780b11ee4SPhilip Paeps } 66880b11ee4SPhilip Paeps } 66980b11ee4SPhilip Paeps #undef BDG_MEMBER_MATCHES_ARP 670cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 67180b11ee4SPhilip Paeps 672ca925d9cSJonathan Lemon /* 673d8b84d9eSJonathan Lemon * No match, use the first inet address on the receive interface 674ca925d9cSJonathan Lemon * as a dummy address for the rest of the function. 675ca925d9cSJonathan Lemon */ 676137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 677d8b84d9eSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 6789de96e89SGleb Smirnoff if (ifa->ifa_addr->sa_family == AF_INET && 6799de96e89SGleb Smirnoff (ifa->ifa_carp == NULL || 6809de96e89SGleb Smirnoff (*carp_iamatch_p)(ifa, &enaddr))) { 681ec691a10SJonathan Lemon ia = ifatoia(ifa); 68209d54778SRobert Watson ifa_ref(ifa); 683137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 684ec691a10SJonathan Lemon goto match; 685ec691a10SJonathan Lemon } 686137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 68709d54778SRobert Watson 688ec691a10SJonathan Lemon /* 689ec691a10SJonathan Lemon * If bridging, fall back to using any inet address. 690ec691a10SJonathan Lemon */ 691cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RLOCK(&in_ifa_tracker); 6922d9cfabaSRobert Watson if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) { 693cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 694b2a8ac7cSLuigi Rizzo goto drop; 6952d9cfabaSRobert Watson } 69609d54778SRobert Watson ifa_ref(&ia->ia_ifa); 697cc0a3c8cSAndrey V. Elsukov IN_IFADDR_RUNLOCK(&in_ifa_tracker); 698ca925d9cSJonathan Lemon match: 699a9771948SGleb Smirnoff if (!enaddr) 700a9771948SGleb Smirnoff enaddr = (u_int8_t *)IF_LLADDR(ifp); 70108b68b0eSGleb Smirnoff carped = (ia->ia_ifa.ifa_carp != NULL); 702ca925d9cSJonathan Lemon myaddr = ia->ia_addr.sin_addr; 70309d54778SRobert Watson ifa_free(&ia->ia_ifa); 704a9771948SGleb Smirnoff if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 705b2a8ac7cSLuigi Rizzo goto drop; /* it's from me, ignore it. */ 706322dcb8dSMax Khon if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 7075d81d095SGleb Smirnoff ARP_LOG(LOG_NOTICE, "link address is broadcast for IP address " 7085d81d095SGleb Smirnoff "%s!\n", inet_ntoa(isaddr)); 709b2a8ac7cSLuigi Rizzo goto drop; 710df8bae1dSRodney W. Grimes } 71100fcf9d1SRobert Watson /* 71200fcf9d1SRobert Watson * Warn if another host is using the same IP address, but only if the 71300fcf9d1SRobert Watson * IP address isn't 0.0.0.0, which is used for DHCP only, in which 71400fcf9d1SRobert Watson * case we suppress the warning to avoid false positive complaints of 71500fcf9d1SRobert Watson * potential misconfiguration. 71600fcf9d1SRobert Watson */ 71708b68b0eSGleb Smirnoff if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr && 71808b68b0eSGleb Smirnoff myaddr.s_addr != 0) { 7195d81d095SGleb Smirnoff ARP_LOG(LOG_ERR, "%*D is using my IP address %s on %s!\n", 720322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7213affb6fbSYaroslav Tykhiy inet_ntoa(isaddr), ifp->if_xname); 722df8bae1dSRodney W. Grimes itaddr = myaddr; 72354fc657dSGeorge V. Neville-Neil ARPSTAT_INC(dupips); 724df8bae1dSRodney W. Grimes goto reply; 725df8bae1dSRodney W. Grimes } 726deb62e28SRuslan Ermilov if (ifp->if_flags & IFF_STATICARP) 727deb62e28SRuslan Ermilov goto reply; 7288b07e49aSJulian Elischer 7296e6b3f7cSQing Li bzero(&sin, sizeof(sin)); 7306e6b3f7cSQing Li sin.sin_len = sizeof(struct sockaddr_in); 7316e6b3f7cSQing Li sin.sin_family = AF_INET; 7326e6b3f7cSQing Li sin.sin_addr = isaddr; 733b4b1367aSAlexander V. Chernikov create = (itaddr.s_addr == myaddr.s_addr) ? 1 : 0; 734b4b1367aSAlexander V. Chernikov flags = LLE_EXCLUSIVE; 7356e6b3f7cSQing Li IF_AFDATA_LOCK(ifp); 736b4b1367aSAlexander V. Chernikov if (create != 0) 737b4b1367aSAlexander V. Chernikov la = lla_create(LLTABLE(ifp), 0, (struct sockaddr *)&sin); 738b4b1367aSAlexander V. Chernikov else 7396e6b3f7cSQing Li la = lla_lookup(LLTABLE(ifp), flags, (struct sockaddr *)&sin); 7406e6b3f7cSQing Li IF_AFDATA_UNLOCK(ifp); 7416e6b3f7cSQing Li if (la != NULL) { 7426e6b3f7cSQing Li /* the following is not an error when doing bridging */ 74308b68b0eSGleb Smirnoff if (!bridged && la->lle_tbl->llt_ifp != ifp) { 7443269187dSAlfred Perlstein if (log_arp_wrong_iface) 7455d81d095SGleb Smirnoff ARP_LOG(LOG_WARNING, "%s is on %s " 7466e6b3f7cSQing Li "but got reply from %*D on %s\n", 747dd9b6ddeSBill Fenner inet_ntoa(isaddr), 7486e6b3f7cSQing Li la->lle_tbl->llt_ifp->if_xname, 7496e6b3f7cSQing Li ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7509bf40edeSBrooks Davis ifp->if_xname); 751cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 7526e6b3f7cSQing Li goto reply; 753dd9b6ddeSBill Fenner } 7546e6b3f7cSQing Li if ((la->la_flags & LLE_VALID) && 7556e6b3f7cSQing Li bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) { 7566e6b3f7cSQing Li if (la->la_flags & LLE_STATIC) { 757cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 7584659e09dSAndrey V. Elsukov if (log_arp_permanent_modify) 7595d81d095SGleb Smirnoff ARP_LOG(LOG_ERR, 7605d81d095SGleb Smirnoff "%*D attempts to modify " 7614659e09dSAndrey V. Elsukov "permanent entry for %s on %s\n", 7624659e09dSAndrey V. Elsukov ifp->if_addrlen, 7634659e09dSAndrey V. Elsukov (u_char *)ar_sha(ah), ":", 7646e6b3f7cSQing Li inet_ntoa(isaddr), ifp->if_xname); 7656e6b3f7cSQing Li goto reply; 7666e6b3f7cSQing Li } 7676e6b3f7cSQing Li if (log_arp_movements) { 7685d81d095SGleb Smirnoff ARP_LOG(LOG_INFO, "%s moved from %*D " 7696e6b3f7cSQing Li "to %*D on %s\n", 7708b07e49aSJulian Elischer inet_ntoa(isaddr), 7716e6b3f7cSQing Li ifp->if_addrlen, 7726e6b3f7cSQing Li (u_char *)&la->ll_addr, ":", 7736e6b3f7cSQing Li ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7748b07e49aSJulian Elischer ifp->if_xname); 775dd9b6ddeSBill Fenner } 776dfd5dee1SPeter Wemm } 7776e6b3f7cSQing Li 778322dcb8dSMax Khon if (ifp->if_addrlen != ah->ar_hln) { 779cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 7805d81d095SGleb Smirnoff ARP_LOG(LOG_WARNING, "from %*D: addr len: new %d, " 781c9168718SGleb Smirnoff "i/f %d (ignored)\n", ifp->if_addrlen, 782c9168718SGleb Smirnoff (u_char *) ar_sha(ah), ":", ah->ar_hln, 783c9168718SGleb Smirnoff ifp->if_addrlen); 78409d3f895SGeorge V. Neville-Neil goto drop; 785322dcb8dSMax Khon } 7866e6b3f7cSQing Li (void)memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen); 7876e6b3f7cSQing Li la->la_flags |= LLE_VALID; 7888b07e49aSJulian Elischer 78909fe6320SNavdeep Parhar EVENTHANDLER_INVOKE(lle_event, la, LLENTRY_RESOLVED); 7909a311445SNavdeep Parhar 7916e6b3f7cSQing Li if (!(la->la_flags & LLE_STATIC)) { 792becba438SBjoern A. Zeeb int canceled; 793becba438SBjoern A. Zeeb 794becba438SBjoern A. Zeeb LLE_ADDREF(la); 795a98c06f1SGleb Smirnoff la->la_expire = time_uptime + V_arpt_keep; 796*0447c136SAlexander V. Chernikov canceled = callout_reset(&la->lle_timer, 797becba438SBjoern A. Zeeb hz * V_arpt_keep, arptimer, la); 798becba438SBjoern A. Zeeb if (canceled) 799becba438SBjoern A. Zeeb LLE_REMREF(la); 8001daaa65dSGleb Smirnoff } 801022695f8SOrion Hodson la->la_asked = 0; 802603724d3SBjoern A. Zeeb la->la_preempt = V_arp_maxtries; 803e162ea60SGeorge V. Neville-Neil /* 804e162ea60SGeorge V. Neville-Neil * The packets are all freed within the call to the output 805e162ea60SGeorge V. Neville-Neil * routine. 806e162ea60SGeorge V. Neville-Neil * 807e162ea60SGeorge V. Neville-Neil * NB: The lock MUST be released before the call to the 808e162ea60SGeorge V. Neville-Neil * output routine. 809e162ea60SGeorge V. Neville-Neil */ 810e162ea60SGeorge V. Neville-Neil if (la->la_hold != NULL) { 811e162ea60SGeorge V. Neville-Neil struct mbuf *m_hold, *m_hold_next; 812e162ea60SGeorge V. Neville-Neil 81390fdff07SGeorge V. Neville-Neil m_hold = la->la_hold; 81490fdff07SGeorge V. Neville-Neil la->la_hold = NULL; 81590fdff07SGeorge V. Neville-Neil la->la_numheld = 0; 816314294deSAlexander V. Chernikov lltable_fill_sa_entry(la, (struct sockaddr *)&sa); 817cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 818ede99017SGeorge V. Neville-Neil for (; m_hold != NULL; m_hold = m_hold_next) { 819e162ea60SGeorge V. Neville-Neil m_hold_next = m_hold->m_nextpkt; 820e162ea60SGeorge V. Neville-Neil m_hold->m_nextpkt = NULL; 82186bd0491SAndre Oppermann /* Avoid confusing lower layers. */ 82286bd0491SAndre Oppermann m_clrprotoflags(m_hold); 823e162ea60SGeorge V. Neville-Neil (*ifp->if_output)(ifp, m_hold, &sa, NULL); 8246e6b3f7cSQing Li } 825e162ea60SGeorge V. Neville-Neil } else 826e162ea60SGeorge V. Neville-Neil LLE_WUNLOCK(la); 827f4048639SBjoern A. Zeeb } 8286e6b3f7cSQing Li reply: 829b2a8ac7cSLuigi Rizzo if (op != ARPOP_REQUEST) 830b2a8ac7cSLuigi Rizzo goto drop; 83154fc657dSGeorge V. Neville-Neil ARPSTAT_INC(rxrequests); 8326e6b3f7cSQing Li 833df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) { 8348b07e49aSJulian Elischer /* Shortcut.. the receiving interface is the target. */ 835322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 836a9771948SGleb Smirnoff (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 837df8bae1dSRodney W. Grimes } else { 838897d75c9SQing Li struct llentry *lle = NULL; 839897d75c9SQing Li 840cd29a779SQing Li sin.sin_addr = itaddr; 841ea0c3776SAndrey V. Elsukov IF_AFDATA_RLOCK(ifp); 842cd29a779SQing Li lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin); 843ea0c3776SAndrey V. Elsukov IF_AFDATA_RUNLOCK(ifp); 844cd29a779SQing Li 845cd29a779SQing Li if ((lle != NULL) && (lle->la_flags & LLE_PUB)) { 846cd29a779SQing Li (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 847cd29a779SQing Li (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln); 848cd29a779SQing Li LLE_RUNLOCK(lle); 849cd29a779SQing Li } else { 850cd29a779SQing Li 851cd29a779SQing Li if (lle != NULL) 852cd29a779SQing Li LLE_RUNLOCK(lle); 853cd29a779SQing Li 854603724d3SBjoern A. Zeeb if (!V_arp_proxyall) 855b2a8ac7cSLuigi Rizzo goto drop; 85628e82295SGarrett Wollman 85728e82295SGarrett Wollman sin.sin_addr = itaddr; 8588b07e49aSJulian Elischer /* XXX MRT use table 0 for arp reply */ 8598b07e49aSJulian Elischer rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 860b2a8ac7cSLuigi Rizzo if (!rt) 861b2a8ac7cSLuigi Rizzo goto drop; 862897d75c9SQing Li 86328e82295SGarrett Wollman /* 86428e82295SGarrett Wollman * Don't send proxies for nodes on the same interface 86528e82295SGarrett Wollman * as this one came out of, or we'll get into a fight 86628e82295SGarrett Wollman * over who claims what Ether address. 86728e82295SGarrett Wollman */ 868897d75c9SQing Li if (!rt->rt_ifp || rt->rt_ifp == ifp) { 8694e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 870b2a8ac7cSLuigi Rizzo goto drop; 87128e82295SGarrett Wollman } 8724e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 873cc728227SDavid Malone 874897d75c9SQing Li (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 875cd29a779SQing Li (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 876897d75c9SQing Li 877cc728227SDavid Malone /* 878cc728227SDavid Malone * Also check that the node which sent the ARP packet 8796bccea7cSRebecca Cran * is on the interface we expect it to be on. This 880cc728227SDavid Malone * avoids ARP chaos if an interface is connected to the 881cc728227SDavid Malone * wrong network. 882cc728227SDavid Malone */ 883cc728227SDavid Malone sin.sin_addr = isaddr; 884cc728227SDavid Malone 8858b07e49aSJulian Elischer /* XXX MRT use table 0 for arp checks */ 8868b07e49aSJulian Elischer rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 887b2a8ac7cSLuigi Rizzo if (!rt) 888b2a8ac7cSLuigi Rizzo goto drop; 889322dcb8dSMax Khon if (rt->rt_ifp != ifp) { 8905d81d095SGleb Smirnoff ARP_LOG(LOG_INFO, "proxy: ignoring request" 8919bf40edeSBrooks Davis " from %s via %s, expecting %s\n", 8929bf40edeSBrooks Davis inet_ntoa(isaddr), ifp->if_xname, 8939bf40edeSBrooks Davis rt->rt_ifp->if_xname); 8944e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 895b2a8ac7cSLuigi Rizzo goto drop; 896cc728227SDavid Malone } 8974e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 898cc728227SDavid Malone 899ac234f93SGarrett Wollman #ifdef DEBUG_PROXY 900ea50c13eSGleb Smirnoff printf("arp: proxying for %s\n", inet_ntoa(itaddr)); 901ac234f93SGarrett Wollman #endif 902df8bae1dSRodney W. Grimes } 903cd29a779SQing Li } 904df8bae1dSRodney W. Grimes 905d0558157SBruce M Simpson if (itaddr.s_addr == myaddr.s_addr && 906d0558157SBruce M Simpson IN_LINKLOCAL(ntohl(itaddr.s_addr))) { 907d0558157SBruce M Simpson /* RFC 3927 link-local IPv4; always reply by broadcast. */ 908d0558157SBruce M Simpson #ifdef DEBUG_LINKLOCAL 909d0558157SBruce M Simpson printf("arp: sending reply for link-local addr %s\n", 910d0558157SBruce M Simpson inet_ntoa(itaddr)); 911d0558157SBruce M Simpson #endif 912d0558157SBruce M Simpson m->m_flags |= M_BCAST; 913d0558157SBruce M Simpson m->m_flags &= ~M_MCAST; 914d0558157SBruce M Simpson } else { 915d0558157SBruce M Simpson /* default behaviour; never reply by broadcast. */ 916d0558157SBruce M Simpson m->m_flags &= ~(M_BCAST|M_MCAST); 917d0558157SBruce M Simpson } 918322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 919322dcb8dSMax Khon (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 920322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REPLY); 921322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 92264bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 92364bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 9242303570fSAndrey V. Elsukov m->m_pkthdr.rcvif = NULL; 92564bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 92664bf80ceSMatthew N. Dodd sa.sa_len = 2; 92786bd0491SAndre Oppermann m_clrprotoflags(m); /* Avoid confusing lower layers. */ 928279aa3d4SKip Macy (*ifp->if_output)(ifp, m, &sa, NULL); 92954fc657dSGeorge V. Neville-Neil ARPSTAT_INC(txreplies); 930df8bae1dSRodney W. Grimes return; 931b2a8ac7cSLuigi Rizzo 932b2a8ac7cSLuigi Rizzo drop: 933b2a8ac7cSLuigi Rizzo m_freem(m); 934df8bae1dSRodney W. Grimes } 9351d5e9e22SEivind Eklund #endif 936df8bae1dSRodney W. Grimes 937dd2e4102SGarrett Wollman void 938f2565d68SRobert Watson arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 939dd2e4102SGarrett Wollman { 9406e6b3f7cSQing Li struct llentry *lle; 9416e6b3f7cSQing Li 94208b68b0eSGleb Smirnoff if (ifa->ifa_carp != NULL) 94308b68b0eSGleb Smirnoff return; 94408b68b0eSGleb Smirnoff 945ce9122fdSQing Li if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) { 946322dcb8dSMax Khon arprequest(ifp, &IA_SIN(ifa)->sin_addr, 947322dcb8dSMax Khon &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 9486e6b3f7cSQing Li /* 9496e6b3f7cSQing Li * interface address is considered static entry 9506e6b3f7cSQing Li * because the output of the arp utility shows 9516e6b3f7cSQing Li * that L2 entry as permanent 9526e6b3f7cSQing Li */ 9536e6b3f7cSQing Li IF_AFDATA_LOCK(ifp); 954b4b1367aSAlexander V. Chernikov lle = lla_create(LLTABLE(ifp), LLE_IFADDR | LLE_STATIC, 9556e6b3f7cSQing Li (struct sockaddr *)IA_SIN(ifa)); 9566e6b3f7cSQing Li IF_AFDATA_UNLOCK(ifp); 9576e6b3f7cSQing Li if (lle == NULL) 9586e6b3f7cSQing Li log(LOG_INFO, "arp_ifinit: cannot create arp " 9596e6b3f7cSQing Li "entry for interface address\n"); 96086cd829dSKip Macy else 961b4b1367aSAlexander V. Chernikov LLE_WUNLOCK(lle); 962ce9122fdSQing Li } 9636e6b3f7cSQing Li ifa->ifa_rtrequest = NULL; 964dd2e4102SGarrett Wollman } 965df5e1987SJonathan Lemon 966a9771948SGleb Smirnoff void 967f2565d68SRobert Watson arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 968a9771948SGleb Smirnoff { 969a9771948SGleb Smirnoff if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 970a9771948SGleb Smirnoff arprequest(ifp, &IA_SIN(ifa)->sin_addr, 971a9771948SGleb Smirnoff &IA_SIN(ifa)->sin_addr, enaddr); 9726e6b3f7cSQing Li ifa->ifa_rtrequest = NULL; 973a9771948SGleb Smirnoff } 974a9771948SGleb Smirnoff 9751ed81b73SMarko Zec static void 9761ed81b73SMarko Zec arp_init(void) 9771ed81b73SMarko Zec { 9781ed81b73SMarko Zec 979d4b5cae4SRobert Watson netisr_register(&arp_nh); 980df5e1987SJonathan Lemon } 981df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 982