1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)if_ether.c 8.1 (Berkeley) 6/10/93 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 32df8bae1dSRodney W. Grimes /* 33df8bae1dSRodney W. Grimes * Ethernet address resolution protocol. 34df8bae1dSRodney W. Grimes * TODO: 35df8bae1dSRodney W. Grimes * add "inuse/lock" bit (or ref. count) along with valid bit 36df8bae1dSRodney W. Grimes */ 37df8bae1dSRodney W. Grimes 384b421e2dSMike Silbersack #include <sys/cdefs.h> 394b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 404b421e2dSMike Silbersack 411d5e9e22SEivind Eklund #include "opt_inet.h" 421d5e9e22SEivind Eklund 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44df8bae1dSRodney W. Grimes #include <sys/kernel.h> 45ce02431fSDoug Rabson #include <sys/queue.h> 46885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h> 47885f1aa4SPoul-Henning Kamp #include <sys/systm.h> 48885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h> 49885f1aa4SPoul-Henning Kamp #include <sys/malloc.h> 50de34ad3fSJulian Elischer #include <sys/proc.h> 514458ac71SBruce Evans #include <sys/socket.h> 52885f1aa4SPoul-Henning Kamp #include <sys/syslog.h> 53df8bae1dSRodney W. Grimes 54df8bae1dSRodney W. Grimes #include <net/if.h> 55df8bae1dSRodney W. Grimes #include <net/if_dl.h> 56722012ccSJulian Elischer #include <net/if_types.h> 57748e0b0aSGarrett Wollman #include <net/netisr.h> 58b149dd6cSLarry Lile #include <net/if_llc.h> 59c8f8e9c1SJulian Elischer #include <net/ethernet.h> 605736e6fbSBjoern A. Zeeb #include <net/route.h> 61530c0060SRobert Watson #include <net/vnet.h> 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes #include <netinet/in.h> 64df8bae1dSRodney W. Grimes #include <netinet/in_var.h> 656e6b3f7cSQing Li #include <net/if_llatbl.h> 66df8bae1dSRodney W. Grimes #include <netinet/if_ether.h> 6783e521ecSBjoern A. Zeeb #ifdef INET 689963e8a5SWill Andrews #include <netinet/ip_carp.h> 699963e8a5SWill Andrews #endif 70df8bae1dSRodney W. Grimes 71322dcb8dSMax Khon #include <net/if_arc.h> 72fda82fc2SJulian Elischer #include <net/iso88025.h> 73fda82fc2SJulian Elischer 74aed55708SRobert Watson #include <security/mac/mac_framework.h> 75aed55708SRobert Watson 76*47e8d432SGleb Smirnoff #define SIN(s) ((const struct sockaddr_in *)(s)) 77df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s) 78df8bae1dSRodney W. Grimes 79ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether); 806472ac3dSEd Schouten static SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, ""); 816472ac3dSEd Schouten static SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, ""); 82df8bae1dSRodney W. Grimes 83df8bae1dSRodney W. Grimes /* timer values */ 843e288e62SDimitry Andric static VNET_DEFINE(int, arpt_keep) = (20*60); /* once resolved, good for 20 85eddfbb76SRobert Watson * minutes */ 863e288e62SDimitry Andric static VNET_DEFINE(int, arp_maxtries) = 5; 8782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, useloopback) = 1; /* use loopback interface for 8882cea7e6SBjoern A. Zeeb * local traffic */ 893e288e62SDimitry Andric static VNET_DEFINE(int, arp_proxyall) = 0; 903e288e62SDimitry Andric static VNET_DEFINE(int, arpt_down) = 20; /* keep incomplete entries for 9193704ac5SQing Li * 20 seconds */ 92e162ea60SGeorge V. Neville-Neil VNET_DEFINE(struct arpstat, arpstat); /* ARP statistics, see if_arp.h */ 93e162ea60SGeorge V. Neville-Neil 943e288e62SDimitry Andric static VNET_DEFINE(int, arp_maxhold) = 1; 95885f1aa4SPoul-Henning Kamp 961e77c105SRobert Watson #define V_arpt_keep VNET(arpt_keep) 9793704ac5SQing Li #define V_arpt_down VNET(arpt_down) 981e77c105SRobert Watson #define V_arp_maxtries VNET(arp_maxtries) 991e77c105SRobert Watson #define V_arp_proxyall VNET(arp_proxyall) 10054fc657dSGeorge V. Neville-Neil #define V_arpstat VNET(arpstat) 101e162ea60SGeorge V. Neville-Neil #define V_arp_maxhold VNET(arp_maxhold) 102885f1aa4SPoul-Henning Kamp 103eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_RW, 104eddfbb76SRobert Watson &VNET_NAME(arpt_keep), 0, 105eddfbb76SRobert Watson "ARP entry lifetime in seconds"); 106eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW, 107eddfbb76SRobert Watson &VNET_NAME(arp_maxtries), 0, 1088b615593SMarko Zec "ARP resolution attempts before returning error"); 109eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW, 110eddfbb76SRobert Watson &VNET_NAME(useloopback), 0, 1118b615593SMarko Zec "Use the loopback interface for local traffic"); 112eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW, 113eddfbb76SRobert Watson &VNET_NAME(arp_proxyall), 0, 1148b615593SMarko Zec "Enable proxy ARP for all suitable requests"); 115e162ea60SGeorge V. Neville-Neil SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, wait, CTLFLAG_RW, 116e162ea60SGeorge V. Neville-Neil &VNET_NAME(arpt_down), 0, 117e162ea60SGeorge V. Neville-Neil "Incomplete ARP entry lifetime in seconds"); 11854fc657dSGeorge V. Neville-Neil SYSCTL_VNET_STRUCT(_net_link_ether_arp, OID_AUTO, stats, CTLFLAG_RW, 11954fc657dSGeorge V. Neville-Neil &VNET_NAME(arpstat), arpstat, 12054fc657dSGeorge V. Neville-Neil "ARP statistics (struct arpstat, net/if_arp.h)"); 121e162ea60SGeorge V. Neville-Neil SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, maxhold, CTLFLAG_RW, 122e162ea60SGeorge V. Neville-Neil &VNET_NAME(arp_maxhold), 0, 123e162ea60SGeorge V. Neville-Neil "Number of packets to hold per ARP entry"); 124885f1aa4SPoul-Henning Kamp 1254d77a549SAlfred Perlstein static void arp_init(void); 1261cafed39SJonathan Lemon static void arpintr(struct mbuf *); 1274d77a549SAlfred Perlstein static void arptimer(void *); 1281d5e9e22SEivind Eklund #ifdef INET 1294d77a549SAlfred Perlstein static void in_arpinput(struct mbuf *); 1301d5e9e22SEivind Eklund #endif 13128e82295SGarrett Wollman 132d4b5cae4SRobert Watson static const struct netisr_handler arp_nh = { 133d4b5cae4SRobert Watson .nh_name = "arp", 134d4b5cae4SRobert Watson .nh_handler = arpintr, 135d4b5cae4SRobert Watson .nh_proto = NETISR_ARP, 136d4b5cae4SRobert Watson .nh_policy = NETISR_POLICY_SOURCE, 137d4b5cae4SRobert Watson }; 138d4b5cae4SRobert Watson 1396e6b3f7cSQing Li #ifdef AF_INET 140df8bae1dSRodney W. Grimes /* 1416e6b3f7cSQing Li * called by in_ifscrub to remove entry from the table when 1426e6b3f7cSQing Li * the interface goes away 1436e6b3f7cSQing Li */ 1446e6b3f7cSQing Li void 1456e6b3f7cSQing Li arp_ifscrub(struct ifnet *ifp, uint32_t addr) 1466e6b3f7cSQing Li { 1476e6b3f7cSQing Li struct sockaddr_in addr4; 1486e6b3f7cSQing Li 1496e6b3f7cSQing Li bzero((void *)&addr4, sizeof(addr4)); 1506e6b3f7cSQing Li addr4.sin_len = sizeof(addr4); 1516e6b3f7cSQing Li addr4.sin_family = AF_INET; 1526e6b3f7cSQing Li addr4.sin_addr.s_addr = addr; 1536e6b3f7cSQing Li IF_AFDATA_LOCK(ifp); 1546e6b3f7cSQing Li lla_lookup(LLTABLE(ifp), (LLE_DELETE | LLE_IFADDR), 1556e6b3f7cSQing Li (struct sockaddr *)&addr4); 1566e6b3f7cSQing Li IF_AFDATA_UNLOCK(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) { 170ea537929SGleb Smirnoff LLE_WUNLOCK(lle); 171ea537929SGleb Smirnoff return; 172ea537929SGleb Smirnoff } 173ea537929SGleb Smirnoff 1746e6b3f7cSQing Li ifp = lle->lle_tbl->llt_ifp; 17554fc657dSGeorge V. Neville-Neil CURVNET_SET(ifp->if_vnet); 17609fe6320SNavdeep Parhar 17709fe6320SNavdeep Parhar if (lle->la_flags != LLE_DELETED) { 17809fe6320SNavdeep Parhar int evt; 17909fe6320SNavdeep Parhar 18009fe6320SNavdeep Parhar if (lle->la_flags & LLE_VALID) 18109fe6320SNavdeep Parhar evt = LLENTRY_EXPIRED; 18209fe6320SNavdeep Parhar else 18309fe6320SNavdeep Parhar evt = LLENTRY_TIMEDOUT; 18409fe6320SNavdeep Parhar EVENTHANDLER_INVOKE(lle_event, lle, evt); 18509fe6320SNavdeep Parhar } 18609fe6320SNavdeep Parhar 187ea537929SGleb Smirnoff callout_stop(&lle->la_timer); 188ea537929SGleb Smirnoff 189ea537929SGleb Smirnoff /* XXX: LOR avoidance. We still have ref on lle. */ 190ea537929SGleb Smirnoff LLE_WUNLOCK(lle); 191ea537929SGleb Smirnoff IF_AFDATA_LOCK(ifp); 192ea537929SGleb Smirnoff LLE_WLOCK(lle); 193ea537929SGleb Smirnoff 194b1ec2940SGleb Smirnoff /* Guard against race with other llentry_free(). */ 195b1ec2940SGleb Smirnoff if (lle->la_flags & LLE_LINKED) { 196b1ec2940SGleb Smirnoff size_t pkts_dropped; 197b1ec2940SGleb Smirnoff 198ea537929SGleb Smirnoff LLE_REMREF(lle); 199e162ea60SGeorge V. Neville-Neil pkts_dropped = llentry_free(lle); 200e162ea60SGeorge V. Neville-Neil ARPSTAT_ADD(dropped, pkts_dropped); 201b1ec2940SGleb Smirnoff } else 202b1ec2940SGleb Smirnoff LLE_FREE_LOCKED(lle); 203b1ec2940SGleb Smirnoff 204b1ec2940SGleb Smirnoff IF_AFDATA_UNLOCK(ifp); 205b1ec2940SGleb Smirnoff 20654fc657dSGeorge V. Neville-Neil ARPSTAT_INC(timeouts); 207b1ec2940SGleb Smirnoff 20854fc657dSGeorge V. Neville-Neil CURVNET_RESTORE(); 209df8bae1dSRodney W. Grimes } 210df8bae1dSRodney W. Grimes 211df8bae1dSRodney W. Grimes /* 212df8bae1dSRodney W. Grimes * Broadcast an ARP request. Caller specifies: 213df8bae1dSRodney W. Grimes * - arp header source ip address 214df8bae1dSRodney W. Grimes * - arp header target ip address 215df8bae1dSRodney W. Grimes * - arp header source ethernet address 216df8bae1dSRodney W. Grimes */ 2176e6b3f7cSQing Li void 218*47e8d432SGleb Smirnoff arprequest(struct ifnet *ifp, const struct in_addr *sip, 219*47e8d432SGleb Smirnoff const struct in_addr *tip, u_char *enaddr) 220df8bae1dSRodney W. Grimes { 221e952fa39SMatthew N. Dodd struct mbuf *m; 222e952fa39SMatthew N. Dodd struct arphdr *ah; 223df8bae1dSRodney W. Grimes struct sockaddr sa; 224aa24ae3cSGleb Smirnoff u_char *carpaddr = NULL; 225df8bae1dSRodney W. Grimes 2266e6b3f7cSQing Li if (sip == NULL) { 2276e6b3f7cSQing Li /* 2286e6b3f7cSQing Li * The caller did not supply a source address, try to find 2296e6b3f7cSQing Li * a compatible one among those assigned to this interface. 2306e6b3f7cSQing Li */ 2316e6b3f7cSQing Li struct ifaddr *ifa; 2326e6b3f7cSQing Li 233aa24ae3cSGleb Smirnoff IF_ADDR_RLOCK(ifp); 2346e6b3f7cSQing Li TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 235aa24ae3cSGleb Smirnoff if (ifa->ifa_addr->sa_family != AF_INET) 2366e6b3f7cSQing Li continue; 237aa24ae3cSGleb Smirnoff 238aa24ae3cSGleb Smirnoff if (ifa->ifa_carp) { 239aa24ae3cSGleb Smirnoff if ((*carp_iamatch_p)(ifa, &carpaddr) == 0) 240aa24ae3cSGleb Smirnoff continue; 241aa24ae3cSGleb Smirnoff sip = &IA_SIN(ifa)->sin_addr; 242aa24ae3cSGleb Smirnoff } else { 243aa24ae3cSGleb Smirnoff carpaddr = NULL; 244aa24ae3cSGleb Smirnoff sip = &IA_SIN(ifa)->sin_addr; 245aa24ae3cSGleb Smirnoff } 246aa24ae3cSGleb Smirnoff 2476e6b3f7cSQing Li if (0 == ((sip->s_addr ^ tip->s_addr) & 248aa24ae3cSGleb Smirnoff IA_MASKSIN(ifa)->sin_addr.s_addr)) 2496e6b3f7cSQing Li break; /* found it. */ 2506e6b3f7cSQing Li } 251aa24ae3cSGleb Smirnoff IF_ADDR_RUNLOCK(ifp); 2526e6b3f7cSQing Li if (sip == NULL) { 2536e6b3f7cSQing Li printf("%s: cannot find matching address\n", __func__); 2546e6b3f7cSQing Li return; 2556e6b3f7cSQing Li } 2566e6b3f7cSQing Li } 257aa24ae3cSGleb Smirnoff if (enaddr == NULL) 258aa24ae3cSGleb Smirnoff enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp); 2596e6b3f7cSQing Li 260eb1b1807SGleb Smirnoff if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL) 261df8bae1dSRodney W. Grimes return; 26264bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) + 26364bf80ceSMatthew N. Dodd 2*ifp->if_data.ifi_addrlen; 26464bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 26564bf80ceSMatthew N. Dodd MH_ALIGN(m, m->m_len); 26664bf80ceSMatthew N. Dodd ah = mtod(m, struct arphdr *); 26764bf80ceSMatthew N. Dodd bzero((caddr_t)ah, m->m_len); 26819527d3eSRobert Watson #ifdef MAC 269b9b0dac3SRobert Watson mac_netinet_arp_send(ifp, m); 27019527d3eSRobert Watson #endif 271322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); 272322dcb8dSMax Khon ah->ar_hln = ifp->if_addrlen; /* hardware address length */ 273322dcb8dSMax Khon ah->ar_pln = sizeof(struct in_addr); /* protocol address length */ 274322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REQUEST); 275*47e8d432SGleb Smirnoff bcopy(enaddr, ar_sha(ah), ah->ar_hln); 276*47e8d432SGleb Smirnoff bcopy(sip, ar_spa(ah), ah->ar_pln); 277*47e8d432SGleb Smirnoff bcopy(tip, ar_tpa(ah), ah->ar_pln); 27864bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 27964bf80ceSMatthew N. Dodd sa.sa_len = 2; 28064bf80ceSMatthew N. Dodd m->m_flags |= M_BCAST; 281279aa3d4SKip Macy (*ifp->if_output)(ifp, m, &sa, NULL); 28254fc657dSGeorge V. Neville-Neil ARPSTAT_INC(txrequests); 283df8bae1dSRodney W. Grimes } 284df8bae1dSRodney W. Grimes 285df8bae1dSRodney W. Grimes /* 286cd46a114SLuigi Rizzo * Resolve an IP address into an ethernet address. 287cd46a114SLuigi Rizzo * On input: 288cd46a114SLuigi Rizzo * ifp is the interface we use 289cd46a114SLuigi Rizzo * rt0 is the route to the final destination (possibly useless) 290b6ae6984SJulian Elischer * m is the mbuf. May be NULL if we don't have a packet. 291b6ae6984SJulian Elischer * dst is the next hop, 292cd46a114SLuigi Rizzo * desten is where we want the address. 293cd46a114SLuigi Rizzo * 294cd46a114SLuigi Rizzo * On success, desten is filled in and the function returns 0; 295cd46a114SLuigi Rizzo * If the packet must be held pending resolution, we return EWOULDBLOCK 296cd46a114SLuigi Rizzo * On other errors, we return the corresponding error code. 297b6ae6984SJulian Elischer * Note that m_freem() handles NULL. 298df8bae1dSRodney W. Grimes */ 299df8bae1dSRodney W. Grimes int 300cd46a114SLuigi Rizzo arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, 301*47e8d432SGleb Smirnoff const struct sockaddr *dst, u_char *desten, struct llentry **lle) 302df8bae1dSRodney W. Grimes { 3036e6b3f7cSQing Li struct llentry *la = 0; 30400a46b31SKip Macy u_int flags = 0; 305e162ea60SGeorge V. Neville-Neil struct mbuf *curr = NULL; 306e162ea60SGeorge V. Neville-Neil struct mbuf *next = NULL; 3076e6b3f7cSQing Li int error, renew; 308df8bae1dSRodney W. Grimes 3096e6b3f7cSQing Li *lle = NULL; 3106e6b3f7cSQing Li if (m != NULL) { 311b6ae6984SJulian Elischer if (m->m_flags & M_BCAST) { 312b6ae6984SJulian Elischer /* broadcast */ 313b6ae6984SJulian Elischer (void)memcpy(desten, 314b6ae6984SJulian Elischer ifp->if_broadcastaddr, ifp->if_addrlen); 315cd46a114SLuigi Rizzo return (0); 316df8bae1dSRodney W. Grimes } 317b6ae6984SJulian Elischer if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) { 318b6ae6984SJulian Elischer /* multicast */ 319df8bae1dSRodney W. Grimes ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten); 320cd46a114SLuigi Rizzo return (0); 321df8bae1dSRodney W. Grimes } 322b6ae6984SJulian Elischer } 3236e6b3f7cSQing Li retry: 32400a46b31SKip Macy IF_AFDATA_RLOCK(ifp); 3256e6b3f7cSQing Li la = lla_lookup(LLTABLE(ifp), flags, dst); 32600a46b31SKip Macy IF_AFDATA_RUNLOCK(ifp); 32700a46b31SKip Macy if ((la == NULL) && ((flags & LLE_EXCLUSIVE) == 0) 32800a46b31SKip Macy && ((ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0)) { 32900a46b31SKip Macy flags |= (LLE_CREATE | LLE_EXCLUSIVE); 33000a46b31SKip Macy IF_AFDATA_WLOCK(ifp); 33100a46b31SKip Macy la = lla_lookup(LLTABLE(ifp), flags, dst); 33200a46b31SKip Macy IF_AFDATA_WUNLOCK(ifp); 33300a46b31SKip Macy } 3341ed7bf1eSGleb Smirnoff if (la == NULL) { 3356e6b3f7cSQing Li if (flags & LLE_CREATE) 3361ed7bf1eSGleb Smirnoff log(LOG_DEBUG, 3371ed7bf1eSGleb Smirnoff "arpresolve: can't allocate llinfo for %s\n", 3381ed7bf1eSGleb Smirnoff inet_ntoa(SIN(dst)->sin_addr)); 3391ed7bf1eSGleb Smirnoff m_freem(m); 3406e6b3f7cSQing Li return (EINVAL); 3411ed7bf1eSGleb Smirnoff } 342a20e2538SGleb Smirnoff 3436e6b3f7cSQing Li if ((la->la_flags & LLE_VALID) && 344a98c06f1SGleb Smirnoff ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) { 3456e6b3f7cSQing Li bcopy(&la->ll_addr, desten, ifp->if_addrlen); 346f0f3379eSOrion Hodson /* 347f0f3379eSOrion Hodson * If entry has an expiry time and it is approaching, 3486e6b3f7cSQing Li * see if we need to send an ARP request within this 3496e6b3f7cSQing Li * arpt_down interval. 350f0f3379eSOrion Hodson */ 3516e6b3f7cSQing Li if (!(la->la_flags & LLE_STATIC) && 352a98c06f1SGleb Smirnoff time_uptime + la->la_preempt > la->la_expire) { 353aa24ae3cSGleb Smirnoff arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL); 354022695f8SOrion Hodson la->la_preempt--; 355f0f3379eSOrion Hodson } 356f0f3379eSOrion Hodson 3576e6b3f7cSQing Li *lle = la; 3586e6b3f7cSQing Li error = 0; 3596e6b3f7cSQing Li goto done; 360df8bae1dSRodney W. Grimes } 3616e6b3f7cSQing Li 3626e6b3f7cSQing Li if (la->la_flags & LLE_STATIC) { /* should not happen! */ 3636e6b3f7cSQing Li log(LOG_DEBUG, "arpresolve: ouch, empty static llinfo for %s\n", 3646e6b3f7cSQing Li inet_ntoa(SIN(dst)->sin_addr)); 36547891de1SRuslan Ermilov m_freem(m); 3666e6b3f7cSQing Li error = EINVAL; 3676e6b3f7cSQing Li goto done; 3686e6b3f7cSQing Li } 3696e6b3f7cSQing Li 370a98c06f1SGleb Smirnoff renew = (la->la_asked == 0 || la->la_expire != time_uptime); 3716e6b3f7cSQing Li if ((renew || m != NULL) && (flags & LLE_EXCLUSIVE) == 0) { 3726e6b3f7cSQing Li flags |= LLE_EXCLUSIVE; 3736e6b3f7cSQing Li LLE_RUNLOCK(la); 3746e6b3f7cSQing Li goto retry; 37547891de1SRuslan Ermilov } 37608aadfbbSJonathan Lemon /* 377df8bae1dSRodney W. Grimes * There is an arptab entry, but no ethernet address 378e162ea60SGeorge V. Neville-Neil * response yet. Add the mbuf to the list, dropping 379e162ea60SGeorge V. Neville-Neil * the oldest packet if we have exceeded the system 380e162ea60SGeorge V. Neville-Neil * setting. 381df8bae1dSRodney W. Grimes */ 3826e6b3f7cSQing Li if (m != NULL) { 383e162ea60SGeorge V. Neville-Neil if (la->la_numheld >= V_arp_maxhold) { 38454fc657dSGeorge V. Neville-Neil if (la->la_hold != NULL) { 385e162ea60SGeorge V. Neville-Neil next = la->la_hold->m_nextpkt; 386df8bae1dSRodney W. Grimes m_freem(la->la_hold); 387e162ea60SGeorge V. Neville-Neil la->la_hold = next; 388e162ea60SGeorge V. Neville-Neil la->la_numheld--; 38954fc657dSGeorge V. Neville-Neil ARPSTAT_INC(dropped); 39054fc657dSGeorge V. Neville-Neil } 391e162ea60SGeorge V. Neville-Neil } 392e162ea60SGeorge V. Neville-Neil if (la->la_hold != NULL) { 393e162ea60SGeorge V. Neville-Neil curr = la->la_hold; 394e162ea60SGeorge V. Neville-Neil while (curr->m_nextpkt != NULL) 395e162ea60SGeorge V. Neville-Neil curr = curr->m_nextpkt; 396e162ea60SGeorge V. Neville-Neil curr->m_nextpkt = m; 397e162ea60SGeorge V. Neville-Neil } else 398df8bae1dSRodney W. Grimes la->la_hold = m; 399e162ea60SGeorge V. Neville-Neil la->la_numheld++; 4006e6b3f7cSQing Li if (renew == 0 && (flags & LLE_EXCLUSIVE)) { 4016e6b3f7cSQing Li flags &= ~LLE_EXCLUSIVE; 4026e6b3f7cSQing Li LLE_DOWNGRADE(la); 40358505389SKip Macy } 404e1ff74c5SGleb Smirnoff 4056e6b3f7cSQing Li } 406e1ff74c5SGleb Smirnoff /* 407e1ff74c5SGleb Smirnoff * Return EWOULDBLOCK if we have tried less than arp_maxtries. It 408e1ff74c5SGleb Smirnoff * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH 409e1ff74c5SGleb Smirnoff * if we have already sent arp_maxtries ARP requests. Retransmit the 410e1ff74c5SGleb Smirnoff * ARP request, but not faster than one request per second. 411e1ff74c5SGleb Smirnoff */ 412603724d3SBjoern A. Zeeb if (la->la_asked < V_arp_maxtries) 413e1ff74c5SGleb Smirnoff error = EWOULDBLOCK; /* First request. */ 414e1ff74c5SGleb Smirnoff else 41556714599SNavdeep Parhar error = rt0 != NULL && (rt0->rt_flags & RTF_GATEWAY) ? 41656714599SNavdeep Parhar EHOSTUNREACH : EHOSTDOWN; 417e1ff74c5SGleb Smirnoff 4186e6b3f7cSQing Li if (renew) { 419becba438SBjoern A. Zeeb int canceled; 420becba438SBjoern A. Zeeb 4216e6b3f7cSQing Li LLE_ADDREF(la); 422a98c06f1SGleb Smirnoff la->la_expire = time_uptime; 423becba438SBjoern A. Zeeb canceled = callout_reset(&la->la_timer, hz * V_arpt_down, 424becba438SBjoern A. Zeeb arptimer, la); 425becba438SBjoern A. Zeeb if (canceled) 426becba438SBjoern A. Zeeb LLE_REMREF(la); 42795ebcabeSMaxim Konovalov la->la_asked++; 4286e6b3f7cSQing Li LLE_WUNLOCK(la); 429aa24ae3cSGleb Smirnoff arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL); 4306e6b3f7cSQing Li return (error); 4316e6b3f7cSQing Li } 4326e6b3f7cSQing Li done: 4336e6b3f7cSQing Li if (flags & LLE_EXCLUSIVE) 4346e6b3f7cSQing Li LLE_WUNLOCK(la); 4356e6b3f7cSQing Li else 4366e6b3f7cSQing Li LLE_RUNLOCK(la); 437e1ff74c5SGleb Smirnoff return (error); 438df8bae1dSRodney W. Grimes } 439df8bae1dSRodney W. Grimes 440df8bae1dSRodney W. Grimes /* 441df8bae1dSRodney W. Grimes * Common length and type checks are done here, 442df8bae1dSRodney W. Grimes * then the protocol-specific routine is called. 443df8bae1dSRodney W. Grimes */ 444885f1aa4SPoul-Henning Kamp static void 4451cafed39SJonathan Lemon arpintr(struct mbuf *m) 446df8bae1dSRodney W. Grimes { 4471cafed39SJonathan Lemon struct arphdr *ar; 448df8bae1dSRodney W. Grimes 44976ec7b2fSRobert Watson if (m->m_len < sizeof(struct arphdr) && 45084365e2bSMatthew Dillon ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) { 451c9168718SGleb Smirnoff log(LOG_NOTICE, "arp: runt packet -- m_pullup failed\n"); 4521cafed39SJonathan Lemon return; 45376ec7b2fSRobert Watson } 45476ec7b2fSRobert Watson ar = mtod(m, struct arphdr *); 45576ec7b2fSRobert Watson 4561cafed39SJonathan Lemon if (ntohs(ar->ar_hrd) != ARPHRD_ETHER && 4571cafed39SJonathan Lemon ntohs(ar->ar_hrd) != ARPHRD_IEEE802 && 458b8b33234SDoug Rabson ntohs(ar->ar_hrd) != ARPHRD_ARCNET && 459e4cd31ddSJeff Roberson ntohs(ar->ar_hrd) != ARPHRD_IEEE1394 && 460e4cd31ddSJeff Roberson ntohs(ar->ar_hrd) != ARPHRD_INFINIBAND) { 46161905171SGleb Smirnoff log(LOG_NOTICE, "arp: unknown hardware address format (0x%2D)" 46261905171SGleb Smirnoff " (from %*D to %*D)\n", (unsigned char *)&ar->ar_hrd, "", 46361905171SGleb Smirnoff ETHER_ADDR_LEN, (u_char *)ar_sha(ar), ":", 46461905171SGleb Smirnoff ETHER_ADDR_LEN, (u_char *)ar_tha(ar), ":"); 46576ec7b2fSRobert Watson m_freem(m); 4661cafed39SJonathan Lemon return; 46776ec7b2fSRobert Watson } 46876ec7b2fSRobert Watson 46931175791SRuslan Ermilov if (m->m_len < arphdr_len(ar)) { 47078e2d2bdSRuslan Ermilov if ((m = m_pullup(m, arphdr_len(ar))) == NULL) { 471c9168718SGleb Smirnoff log(LOG_NOTICE, "arp: runt packet\n"); 47276ec7b2fSRobert Watson m_freem(m); 4731cafed39SJonathan Lemon return; 47476ec7b2fSRobert Watson } 47578e2d2bdSRuslan Ermilov ar = mtod(m, struct arphdr *); 47678e2d2bdSRuslan Ermilov } 477df8bae1dSRodney W. Grimes 47854fc657dSGeorge V. Neville-Neil ARPSTAT_INC(received); 479df8bae1dSRodney W. Grimes switch (ntohs(ar->ar_pro)) { 4801d5e9e22SEivind Eklund #ifdef INET 481df8bae1dSRodney W. Grimes case ETHERTYPE_IP: 482df8bae1dSRodney W. Grimes in_arpinput(m); 4831cafed39SJonathan Lemon return; 4841d5e9e22SEivind Eklund #endif 485df8bae1dSRodney W. Grimes } 486df8bae1dSRodney W. Grimes m_freem(m); 487df8bae1dSRodney W. Grimes } 488df8bae1dSRodney W. Grimes 4891d5e9e22SEivind Eklund #ifdef INET 490df8bae1dSRodney W. Grimes /* 491df8bae1dSRodney W. Grimes * ARP for Internet protocols on 10 Mb/s Ethernet. 492df8bae1dSRodney W. Grimes * Algorithm is that given in RFC 826. 493df8bae1dSRodney W. Grimes * In addition, a sanity check is performed on the sender 494df8bae1dSRodney W. Grimes * protocol address, to catch impersonators. 495df8bae1dSRodney W. Grimes * We no longer handle negotiations for use of trailer protocol: 496df8bae1dSRodney W. Grimes * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent 497df8bae1dSRodney W. Grimes * along with IP replies if we wanted trailers sent to us, 498df8bae1dSRodney W. Grimes * and also sent them in response to IP replies. 499df8bae1dSRodney W. Grimes * This allowed either end to announce the desire to receive 500df8bae1dSRodney W. Grimes * trailer packets. 501df8bae1dSRodney W. Grimes * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, 502df8bae1dSRodney W. Grimes * but formerly didn't normally send requests. 503df8bae1dSRodney W. Grimes */ 5043269187dSAlfred Perlstein static int log_arp_wrong_iface = 1; 505e3d123d6SAlfred Perlstein static int log_arp_movements = 1; 50639393906SGleb Smirnoff static int log_arp_permanent_modify = 1; 507478df1d5SGleb Smirnoff static int allow_multicast = 0; 5083269187dSAlfred Perlstein 5093269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW, 5103269187dSAlfred Perlstein &log_arp_wrong_iface, 0, 5113269187dSAlfred Perlstein "log arp packets arriving on the wrong interface"); 512e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW, 513e3d123d6SAlfred Perlstein &log_arp_movements, 0, 51475ce3221SAlfred Perlstein "log arp replies from MACs different than the one in the cache"); 51539393906SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW, 51639393906SGleb Smirnoff &log_arp_permanent_modify, 0, 51739393906SGleb Smirnoff "log arp replies from MACs different than the one in the permanent arp entry"); 518478df1d5SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW, 519478df1d5SGleb Smirnoff &allow_multicast, 0, "accept multicast addresses"); 5203269187dSAlfred Perlstein 521df8bae1dSRodney W. Grimes static void 522f2565d68SRobert Watson in_arpinput(struct mbuf *m) 523df8bae1dSRodney W. Grimes { 524e952fa39SMatthew N. Dodd struct arphdr *ah; 525e952fa39SMatthew N. Dodd struct ifnet *ifp = m->m_pkthdr.rcvif; 5266e6b3f7cSQing Li struct llentry *la = NULL; 527e952fa39SMatthew N. Dodd struct rtentry *rt; 528ca925d9cSJonathan Lemon struct ifaddr *ifa; 529ca925d9cSJonathan Lemon struct in_ifaddr *ia; 530df8bae1dSRodney W. Grimes struct sockaddr sa; 531df8bae1dSRodney W. Grimes struct in_addr isaddr, itaddr, myaddr; 532a9771948SGleb Smirnoff u_int8_t *enaddr = NULL; 5336e6b3f7cSQing Li int op, flags; 534322dcb8dSMax Khon int req_len; 53580b11ee4SPhilip Paeps int bridged = 0, is_bridge = 0; 53608b68b0eSGleb Smirnoff int carped; 5378e7e854cSKip Macy struct sockaddr_in sin; 5388e7e854cSKip Macy sin.sin_len = sizeof(struct sockaddr_in); 5398e7e854cSKip Macy sin.sin_family = AF_INET; 54029910a5aSKip Macy sin.sin_addr.s_addr = 0; 541df8bae1dSRodney W. Grimes 54274948aa6SAndrew Thompson if (ifp->if_bridge) 5438f867517SAndrew Thompson bridged = 1; 54480b11ee4SPhilip Paeps if (ifp->if_type == IFT_BRIDGE) 54580b11ee4SPhilip Paeps is_bridge = 1; 5468f867517SAndrew Thompson 547322dcb8dSMax Khon req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr)); 548322dcb8dSMax Khon if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) { 549c9168718SGleb Smirnoff log(LOG_NOTICE, "in_arp: runt packet -- m_pullup failed\n"); 5504cbc8ad1SYaroslav Tykhiy return; 5514cbc8ad1SYaroslav Tykhiy } 5524cbc8ad1SYaroslav Tykhiy 553322dcb8dSMax Khon ah = mtod(m, struct arphdr *); 55409d3f895SGeorge V. Neville-Neil /* 55509d3f895SGeorge V. Neville-Neil * ARP is only for IPv4 so we can reject packets with 55609d3f895SGeorge V. Neville-Neil * a protocol length not equal to an IPv4 address. 55709d3f895SGeorge V. Neville-Neil */ 55809d3f895SGeorge V. Neville-Neil if (ah->ar_pln != sizeof(struct in_addr)) { 559c9168718SGleb Smirnoff log(LOG_NOTICE, "in_arp: requested protocol length != %zu\n", 56009d3f895SGeorge V. Neville-Neil sizeof(struct in_addr)); 561414676baSGleb Smirnoff goto drop; 56209d3f895SGeorge V. Neville-Neil } 56309d3f895SGeorge V. Neville-Neil 564478df1d5SGleb Smirnoff if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) { 565478df1d5SGleb Smirnoff log(LOG_NOTICE, "arp: %*D is multicast\n", 566c9168718SGleb Smirnoff ifp->if_addrlen, (u_char *)ar_sha(ah), ":"); 567414676baSGleb Smirnoff goto drop; 56809d3f895SGeorge V. Neville-Neil } 56909d3f895SGeorge V. Neville-Neil 570322dcb8dSMax Khon op = ntohs(ah->ar_op); 571322dcb8dSMax Khon (void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr)); 572322dcb8dSMax Khon (void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr)); 57332439868SGleb Smirnoff 57454fc657dSGeorge V. Neville-Neil if (op == ARPOP_REPLY) 57554fc657dSGeorge V. Neville-Neil ARPSTAT_INC(rxreplies); 57654fc657dSGeorge V. Neville-Neil 577ca925d9cSJonathan Lemon /* 578ca925d9cSJonathan Lemon * For a bridge, we want to check the address irrespective 579ca925d9cSJonathan Lemon * of the receive interface. (This will change slightly 580ca925d9cSJonathan Lemon * when we have clusters of interfaces). 581ca925d9cSJonathan Lemon */ 5822d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 5832ef4a436SGleb Smirnoff LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 58496561547SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) || 5856e6b3f7cSQing Li ia->ia_ifp == ifp) && 58608b68b0eSGleb Smirnoff itaddr.s_addr == ia->ia_addr.sin_addr.s_addr && 58708b68b0eSGleb Smirnoff (ia->ia_ifa.ifa_carp == NULL || 58808b68b0eSGleb Smirnoff (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) { 58909d54778SRobert Watson ifa_ref(&ia->ia_ifa); 5902d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 5912ef4a436SGleb Smirnoff goto match; 5922ef4a436SGleb Smirnoff } 5932ef4a436SGleb Smirnoff } 594ca925d9cSJonathan Lemon LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash) 59596561547SAndrew Thompson if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) || 5966e6b3f7cSQing Li ia->ia_ifp == ifp) && 59709d54778SRobert Watson isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) { 59809d54778SRobert Watson ifa_ref(&ia->ia_ifa); 5992d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 600ca925d9cSJonathan Lemon goto match; 60109d54778SRobert Watson } 60280b11ee4SPhilip Paeps 60380b11ee4SPhilip Paeps #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia) \ 60480b11ee4SPhilip Paeps (ia->ia_ifp->if_bridge == ifp->if_softc && \ 60580b11ee4SPhilip Paeps !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) && \ 60680b11ee4SPhilip Paeps addr == ia->ia_addr.sin_addr.s_addr) 60780b11ee4SPhilip Paeps /* 60880b11ee4SPhilip Paeps * Check the case when bridge shares its MAC address with 60980b11ee4SPhilip Paeps * some of its children, so packets are claimed by bridge 61080b11ee4SPhilip Paeps * itself (bridge_input() does it first), but they are really 61180b11ee4SPhilip Paeps * meant to be destined to the bridge member. 61280b11ee4SPhilip Paeps */ 61380b11ee4SPhilip Paeps if (is_bridge) { 61480b11ee4SPhilip Paeps LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) { 61580b11ee4SPhilip Paeps if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) { 61609d54778SRobert Watson ifa_ref(&ia->ia_ifa); 61780b11ee4SPhilip Paeps ifp = ia->ia_ifp; 6182d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 61980b11ee4SPhilip Paeps goto match; 62080b11ee4SPhilip Paeps } 62180b11ee4SPhilip Paeps } 62280b11ee4SPhilip Paeps } 62380b11ee4SPhilip Paeps #undef BDG_MEMBER_MATCHES_ARP 6242d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 62580b11ee4SPhilip Paeps 626ca925d9cSJonathan Lemon /* 627d8b84d9eSJonathan Lemon * No match, use the first inet address on the receive interface 628ca925d9cSJonathan Lemon * as a dummy address for the rest of the function. 629ca925d9cSJonathan Lemon */ 630137f91e8SJohn Baldwin IF_ADDR_RLOCK(ifp); 631d8b84d9eSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 6329de96e89SGleb Smirnoff if (ifa->ifa_addr->sa_family == AF_INET && 6339de96e89SGleb Smirnoff (ifa->ifa_carp == NULL || 6349de96e89SGleb Smirnoff (*carp_iamatch_p)(ifa, &enaddr))) { 635ec691a10SJonathan Lemon ia = ifatoia(ifa); 63609d54778SRobert Watson ifa_ref(ifa); 637137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 638ec691a10SJonathan Lemon goto match; 639ec691a10SJonathan Lemon } 640137f91e8SJohn Baldwin IF_ADDR_RUNLOCK(ifp); 64109d54778SRobert Watson 642ec691a10SJonathan Lemon /* 643ec691a10SJonathan Lemon * If bridging, fall back to using any inet address. 644ec691a10SJonathan Lemon */ 6452d9cfabaSRobert Watson IN_IFADDR_RLOCK(); 6462d9cfabaSRobert Watson if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) { 6472d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 648b2a8ac7cSLuigi Rizzo goto drop; 6492d9cfabaSRobert Watson } 65009d54778SRobert Watson ifa_ref(&ia->ia_ifa); 6512d9cfabaSRobert Watson IN_IFADDR_RUNLOCK(); 652ca925d9cSJonathan Lemon match: 653a9771948SGleb Smirnoff if (!enaddr) 654a9771948SGleb Smirnoff enaddr = (u_int8_t *)IF_LLADDR(ifp); 65508b68b0eSGleb Smirnoff carped = (ia->ia_ifa.ifa_carp != NULL); 656ca925d9cSJonathan Lemon myaddr = ia->ia_addr.sin_addr; 65709d54778SRobert Watson ifa_free(&ia->ia_ifa); 658a9771948SGleb Smirnoff if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen)) 659b2a8ac7cSLuigi Rizzo goto drop; /* it's from me, ignore it. */ 660322dcb8dSMax Khon if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) { 661c9168718SGleb Smirnoff log(LOG_NOTICE, 662322dcb8dSMax Khon "arp: link address is broadcast for IP address %s!\n", 663ef0cdf33SGarrett Wollman inet_ntoa(isaddr)); 664b2a8ac7cSLuigi Rizzo goto drop; 665df8bae1dSRodney W. Grimes } 66600fcf9d1SRobert Watson /* 66700fcf9d1SRobert Watson * Warn if another host is using the same IP address, but only if the 66800fcf9d1SRobert Watson * IP address isn't 0.0.0.0, which is used for DHCP only, in which 66900fcf9d1SRobert Watson * case we suppress the warning to avoid false positive complaints of 67000fcf9d1SRobert Watson * potential misconfiguration. 67100fcf9d1SRobert Watson */ 67208b68b0eSGleb Smirnoff if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr && 67308b68b0eSGleb Smirnoff myaddr.s_addr != 0) { 67408b68b0eSGleb Smirnoff log(LOG_ERR, "arp: %*D is using my IP address %s on %s!\n", 675322dcb8dSMax Khon ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 6763affb6fbSYaroslav Tykhiy inet_ntoa(isaddr), ifp->if_xname); 677df8bae1dSRodney W. Grimes itaddr = myaddr; 67854fc657dSGeorge V. Neville-Neil ARPSTAT_INC(dupips); 679df8bae1dSRodney W. Grimes goto reply; 680df8bae1dSRodney W. Grimes } 681deb62e28SRuslan Ermilov if (ifp->if_flags & IFF_STATICARP) 682deb62e28SRuslan Ermilov goto reply; 6838b07e49aSJulian Elischer 6846e6b3f7cSQing Li bzero(&sin, sizeof(sin)); 6856e6b3f7cSQing Li sin.sin_len = sizeof(struct sockaddr_in); 6866e6b3f7cSQing Li sin.sin_family = AF_INET; 6876e6b3f7cSQing Li sin.sin_addr = isaddr; 6886e6b3f7cSQing Li flags = (itaddr.s_addr == myaddr.s_addr) ? LLE_CREATE : 0; 6896e6b3f7cSQing Li flags |= LLE_EXCLUSIVE; 6906e6b3f7cSQing Li IF_AFDATA_LOCK(ifp); 6916e6b3f7cSQing Li la = lla_lookup(LLTABLE(ifp), flags, (struct sockaddr *)&sin); 6926e6b3f7cSQing Li IF_AFDATA_UNLOCK(ifp); 6936e6b3f7cSQing Li if (la != NULL) { 6946e6b3f7cSQing Li /* the following is not an error when doing bridging */ 69508b68b0eSGleb Smirnoff if (!bridged && la->lle_tbl->llt_ifp != ifp) { 6963269187dSAlfred Perlstein if (log_arp_wrong_iface) 697c9168718SGleb Smirnoff log(LOG_WARNING, "arp: %s is on %s " 6986e6b3f7cSQing Li "but got reply from %*D on %s\n", 699dd9b6ddeSBill Fenner inet_ntoa(isaddr), 7006e6b3f7cSQing Li la->lle_tbl->llt_ifp->if_xname, 7016e6b3f7cSQing Li ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7029bf40edeSBrooks Davis ifp->if_xname); 703cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 7046e6b3f7cSQing Li goto reply; 705dd9b6ddeSBill Fenner } 7066e6b3f7cSQing Li if ((la->la_flags & LLE_VALID) && 7076e6b3f7cSQing Li bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) { 7086e6b3f7cSQing Li if (la->la_flags & LLE_STATIC) { 709cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 7104659e09dSAndrey V. Elsukov if (log_arp_permanent_modify) 7118b07e49aSJulian Elischer log(LOG_ERR, 7124659e09dSAndrey V. Elsukov "arp: %*D attempts to modify " 7134659e09dSAndrey V. Elsukov "permanent entry for %s on %s\n", 7144659e09dSAndrey V. Elsukov ifp->if_addrlen, 7154659e09dSAndrey V. Elsukov (u_char *)ar_sha(ah), ":", 7166e6b3f7cSQing Li inet_ntoa(isaddr), ifp->if_xname); 7176e6b3f7cSQing Li goto reply; 7186e6b3f7cSQing Li } 7196e6b3f7cSQing Li if (log_arp_movements) { 7206e6b3f7cSQing Li log(LOG_INFO, "arp: %s moved from %*D " 7216e6b3f7cSQing Li "to %*D on %s\n", 7228b07e49aSJulian Elischer inet_ntoa(isaddr), 7236e6b3f7cSQing Li ifp->if_addrlen, 7246e6b3f7cSQing Li (u_char *)&la->ll_addr, ":", 7256e6b3f7cSQing Li ifp->if_addrlen, (u_char *)ar_sha(ah), ":", 7268b07e49aSJulian Elischer ifp->if_xname); 727dd9b6ddeSBill Fenner } 728dfd5dee1SPeter Wemm } 7296e6b3f7cSQing Li 730322dcb8dSMax Khon if (ifp->if_addrlen != ah->ar_hln) { 731cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 732c9168718SGleb Smirnoff log(LOG_WARNING, "arp from %*D: addr len: new %d, " 733c9168718SGleb Smirnoff "i/f %d (ignored)\n", ifp->if_addrlen, 734c9168718SGleb Smirnoff (u_char *) ar_sha(ah), ":", ah->ar_hln, 735c9168718SGleb Smirnoff ifp->if_addrlen); 73609d3f895SGeorge V. Neville-Neil goto drop; 737322dcb8dSMax Khon } 7386e6b3f7cSQing Li (void)memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen); 7396e6b3f7cSQing Li la->la_flags |= LLE_VALID; 7408b07e49aSJulian Elischer 74109fe6320SNavdeep Parhar EVENTHANDLER_INVOKE(lle_event, la, LLENTRY_RESOLVED); 7429a311445SNavdeep Parhar 7436e6b3f7cSQing Li if (!(la->la_flags & LLE_STATIC)) { 744becba438SBjoern A. Zeeb int canceled; 745becba438SBjoern A. Zeeb 746becba438SBjoern A. Zeeb LLE_ADDREF(la); 747a98c06f1SGleb Smirnoff la->la_expire = time_uptime + V_arpt_keep; 748becba438SBjoern A. Zeeb canceled = callout_reset(&la->la_timer, 749becba438SBjoern A. Zeeb hz * V_arpt_keep, arptimer, la); 750becba438SBjoern A. Zeeb if (canceled) 751becba438SBjoern A. Zeeb LLE_REMREF(la); 7521daaa65dSGleb Smirnoff } 753022695f8SOrion Hodson la->la_asked = 0; 754603724d3SBjoern A. Zeeb la->la_preempt = V_arp_maxtries; 755e162ea60SGeorge V. Neville-Neil /* 756e162ea60SGeorge V. Neville-Neil * The packets are all freed within the call to the output 757e162ea60SGeorge V. Neville-Neil * routine. 758e162ea60SGeorge V. Neville-Neil * 759e162ea60SGeorge V. Neville-Neil * NB: The lock MUST be released before the call to the 760e162ea60SGeorge V. Neville-Neil * output routine. 761e162ea60SGeorge V. Neville-Neil */ 762e162ea60SGeorge V. Neville-Neil if (la->la_hold != NULL) { 763e162ea60SGeorge V. Neville-Neil struct mbuf *m_hold, *m_hold_next; 764e162ea60SGeorge V. Neville-Neil 76590fdff07SGeorge V. Neville-Neil m_hold = la->la_hold; 76690fdff07SGeorge V. Neville-Neil la->la_hold = NULL; 76790fdff07SGeorge V. Neville-Neil la->la_numheld = 0; 7686e6b3f7cSQing Li memcpy(&sa, L3_ADDR(la), sizeof(sa)); 769cc7e9d43SBjoern A. Zeeb LLE_WUNLOCK(la); 770ede99017SGeorge V. Neville-Neil for (; m_hold != NULL; m_hold = m_hold_next) { 771e162ea60SGeorge V. Neville-Neil m_hold_next = m_hold->m_nextpkt; 772e162ea60SGeorge V. Neville-Neil m_hold->m_nextpkt = NULL; 773e162ea60SGeorge V. Neville-Neil (*ifp->if_output)(ifp, m_hold, &sa, NULL); 7746e6b3f7cSQing Li } 775e162ea60SGeorge V. Neville-Neil } else 776e162ea60SGeorge V. Neville-Neil LLE_WUNLOCK(la); 777f4048639SBjoern A. Zeeb } 7786e6b3f7cSQing Li reply: 779b2a8ac7cSLuigi Rizzo if (op != ARPOP_REQUEST) 780b2a8ac7cSLuigi Rizzo goto drop; 78154fc657dSGeorge V. Neville-Neil ARPSTAT_INC(rxrequests); 7826e6b3f7cSQing Li 783df8bae1dSRodney W. Grimes if (itaddr.s_addr == myaddr.s_addr) { 7848b07e49aSJulian Elischer /* Shortcut.. the receiving interface is the target. */ 785322dcb8dSMax Khon (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 786a9771948SGleb Smirnoff (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 787df8bae1dSRodney W. Grimes } else { 788897d75c9SQing Li struct llentry *lle = NULL; 789897d75c9SQing Li 790cd29a779SQing Li sin.sin_addr = itaddr; 791cd29a779SQing Li IF_AFDATA_LOCK(ifp); 792cd29a779SQing Li lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin); 793cd29a779SQing Li IF_AFDATA_UNLOCK(ifp); 794cd29a779SQing Li 795cd29a779SQing Li if ((lle != NULL) && (lle->la_flags & LLE_PUB)) { 796cd29a779SQing Li (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 797cd29a779SQing Li (void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln); 798cd29a779SQing Li LLE_RUNLOCK(lle); 799cd29a779SQing Li } else { 800cd29a779SQing Li 801cd29a779SQing Li if (lle != NULL) 802cd29a779SQing Li LLE_RUNLOCK(lle); 803cd29a779SQing Li 804603724d3SBjoern A. Zeeb if (!V_arp_proxyall) 805b2a8ac7cSLuigi Rizzo goto drop; 80628e82295SGarrett Wollman 80728e82295SGarrett Wollman sin.sin_addr = itaddr; 8088b07e49aSJulian Elischer /* XXX MRT use table 0 for arp reply */ 8098b07e49aSJulian Elischer rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 810b2a8ac7cSLuigi Rizzo if (!rt) 811b2a8ac7cSLuigi Rizzo goto drop; 812897d75c9SQing Li 81328e82295SGarrett Wollman /* 81428e82295SGarrett Wollman * Don't send proxies for nodes on the same interface 81528e82295SGarrett Wollman * as this one came out of, or we'll get into a fight 81628e82295SGarrett Wollman * over who claims what Ether address. 81728e82295SGarrett Wollman */ 818897d75c9SQing Li if (!rt->rt_ifp || rt->rt_ifp == ifp) { 8194e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 820b2a8ac7cSLuigi Rizzo goto drop; 82128e82295SGarrett Wollman } 8224e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 823cc728227SDavid Malone 824897d75c9SQing Li (void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln); 825cd29a779SQing Li (void)memcpy(ar_sha(ah), enaddr, ah->ar_hln); 826897d75c9SQing Li 827cc728227SDavid Malone /* 828cc728227SDavid Malone * Also check that the node which sent the ARP packet 8296bccea7cSRebecca Cran * is on the interface we expect it to be on. This 830cc728227SDavid Malone * avoids ARP chaos if an interface is connected to the 831cc728227SDavid Malone * wrong network. 832cc728227SDavid Malone */ 833cc728227SDavid Malone sin.sin_addr = isaddr; 834cc728227SDavid Malone 8358b07e49aSJulian Elischer /* XXX MRT use table 0 for arp checks */ 8368b07e49aSJulian Elischer rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0); 837b2a8ac7cSLuigi Rizzo if (!rt) 838b2a8ac7cSLuigi Rizzo goto drop; 839322dcb8dSMax Khon if (rt->rt_ifp != ifp) { 840cc728227SDavid Malone log(LOG_INFO, "arp_proxy: ignoring request" 8419bf40edeSBrooks Davis " from %s via %s, expecting %s\n", 8429bf40edeSBrooks Davis inet_ntoa(isaddr), ifp->if_xname, 8439bf40edeSBrooks Davis rt->rt_ifp->if_xname); 8444e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 845b2a8ac7cSLuigi Rizzo goto drop; 846cc728227SDavid Malone } 8474e57bc33SChristian S.J. Peron RTFREE_LOCKED(rt); 848cc728227SDavid Malone 849ac234f93SGarrett Wollman #ifdef DEBUG_PROXY 850ea50c13eSGleb Smirnoff printf("arp: proxying for %s\n", inet_ntoa(itaddr)); 851ac234f93SGarrett Wollman #endif 852df8bae1dSRodney W. Grimes } 853cd29a779SQing Li } 854df8bae1dSRodney W. Grimes 855d0558157SBruce M Simpson if (itaddr.s_addr == myaddr.s_addr && 856d0558157SBruce M Simpson IN_LINKLOCAL(ntohl(itaddr.s_addr))) { 857d0558157SBruce M Simpson /* RFC 3927 link-local IPv4; always reply by broadcast. */ 858d0558157SBruce M Simpson #ifdef DEBUG_LINKLOCAL 859d0558157SBruce M Simpson printf("arp: sending reply for link-local addr %s\n", 860d0558157SBruce M Simpson inet_ntoa(itaddr)); 861d0558157SBruce M Simpson #endif 862d0558157SBruce M Simpson m->m_flags |= M_BCAST; 863d0558157SBruce M Simpson m->m_flags &= ~M_MCAST; 864d0558157SBruce M Simpson } else { 865d0558157SBruce M Simpson /* default behaviour; never reply by broadcast. */ 866d0558157SBruce M Simpson m->m_flags &= ~(M_BCAST|M_MCAST); 867d0558157SBruce M Simpson } 868322dcb8dSMax Khon (void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln); 869322dcb8dSMax Khon (void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln); 870322dcb8dSMax Khon ah->ar_op = htons(ARPOP_REPLY); 871322dcb8dSMax Khon ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */ 87264bf80ceSMatthew N. Dodd m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln); 87364bf80ceSMatthew N. Dodd m->m_pkthdr.len = m->m_len; 8742303570fSAndrey V. Elsukov m->m_pkthdr.rcvif = NULL; 87564bf80ceSMatthew N. Dodd sa.sa_family = AF_ARP; 87664bf80ceSMatthew N. Dodd sa.sa_len = 2; 877279aa3d4SKip Macy (*ifp->if_output)(ifp, m, &sa, NULL); 87854fc657dSGeorge V. Neville-Neil ARPSTAT_INC(txreplies); 879df8bae1dSRodney W. Grimes return; 880b2a8ac7cSLuigi Rizzo 881b2a8ac7cSLuigi Rizzo drop: 882b2a8ac7cSLuigi Rizzo m_freem(m); 883df8bae1dSRodney W. Grimes } 8841d5e9e22SEivind Eklund #endif 885df8bae1dSRodney W. Grimes 886dd2e4102SGarrett Wollman void 887f2565d68SRobert Watson arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa) 888dd2e4102SGarrett Wollman { 8896e6b3f7cSQing Li struct llentry *lle; 8906e6b3f7cSQing Li 89108b68b0eSGleb Smirnoff if (ifa->ifa_carp != NULL) 89208b68b0eSGleb Smirnoff return; 89308b68b0eSGleb Smirnoff 894ce9122fdSQing Li if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) { 895322dcb8dSMax Khon arprequest(ifp, &IA_SIN(ifa)->sin_addr, 896322dcb8dSMax Khon &IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp)); 8976e6b3f7cSQing Li /* 8986e6b3f7cSQing Li * interface address is considered static entry 8996e6b3f7cSQing Li * because the output of the arp utility shows 9006e6b3f7cSQing Li * that L2 entry as permanent 9016e6b3f7cSQing Li */ 9026e6b3f7cSQing Li IF_AFDATA_LOCK(ifp); 9036e6b3f7cSQing Li lle = lla_lookup(LLTABLE(ifp), (LLE_CREATE | LLE_IFADDR | LLE_STATIC), 9046e6b3f7cSQing Li (struct sockaddr *)IA_SIN(ifa)); 9056e6b3f7cSQing Li IF_AFDATA_UNLOCK(ifp); 9066e6b3f7cSQing Li if (lle == NULL) 9076e6b3f7cSQing Li log(LOG_INFO, "arp_ifinit: cannot create arp " 9086e6b3f7cSQing Li "entry for interface address\n"); 90986cd829dSKip Macy else 9106e6b3f7cSQing Li LLE_RUNLOCK(lle); 911ce9122fdSQing Li } 9126e6b3f7cSQing Li ifa->ifa_rtrequest = NULL; 913dd2e4102SGarrett Wollman } 914df5e1987SJonathan Lemon 915a9771948SGleb Smirnoff void 916f2565d68SRobert Watson arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr) 917a9771948SGleb Smirnoff { 918a9771948SGleb Smirnoff if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) 919a9771948SGleb Smirnoff arprequest(ifp, &IA_SIN(ifa)->sin_addr, 920a9771948SGleb Smirnoff &IA_SIN(ifa)->sin_addr, enaddr); 9216e6b3f7cSQing Li ifa->ifa_rtrequest = NULL; 922a9771948SGleb Smirnoff } 923a9771948SGleb Smirnoff 9241ed81b73SMarko Zec static void 9251ed81b73SMarko Zec arp_init(void) 9261ed81b73SMarko Zec { 9271ed81b73SMarko Zec 928d4b5cae4SRobert Watson netisr_register(&arp_nh); 929df5e1987SJonathan Lemon } 930df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0); 931