xref: /freebsd/sys/netinet/if_ether.c (revision 897d75c98eec85ead9ba56164272753a2be0e64a)
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"
4219527d3eSRobert Watson #include "opt_mac.h"
43a9771948SGleb Smirnoff #include "opt_carp.h"
441d5e9e22SEivind Eklund 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/kernel.h>
47ce02431fSDoug Rabson #include <sys/queue.h>
48885f1aa4SPoul-Henning Kamp #include <sys/sysctl.h>
49885f1aa4SPoul-Henning Kamp #include <sys/systm.h>
50885f1aa4SPoul-Henning Kamp #include <sys/mbuf.h>
51885f1aa4SPoul-Henning Kamp #include <sys/malloc.h>
52de34ad3fSJulian Elischer #include <sys/proc.h>
534458ac71SBruce Evans #include <sys/socket.h>
54885f1aa4SPoul-Henning Kamp #include <sys/syslog.h>
55603724d3SBjoern A. Zeeb #include <sys/vimage.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <net/if.h>
58df8bae1dSRodney W. Grimes #include <net/if_dl.h>
59722012ccSJulian Elischer #include <net/if_types.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
61748e0b0aSGarrett Wollman #include <net/netisr.h>
62b149dd6cSLarry Lile #include <net/if_llc.h>
63c8f8e9c1SJulian Elischer #include <net/ethernet.h>
644b79449eSBjoern A. Zeeb #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>
704b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
71df8bae1dSRodney W. Grimes 
72322dcb8dSMax Khon #include <net/if_arc.h>
73fda82fc2SJulian Elischer #include <net/iso88025.h>
74fda82fc2SJulian Elischer 
75a9771948SGleb Smirnoff #ifdef DEV_CARP
76a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
77a9771948SGleb Smirnoff #endif
78a9771948SGleb Smirnoff 
79aed55708SRobert Watson #include <security/mac/mac_framework.h>
80aed55708SRobert Watson 
81df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s)
82df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s)
836e6b3f7cSQing Li #define LLTABLE(ifp)	((struct lltable *)(ifp)->if_afdata[AF_INET])
84df8bae1dSRodney W. Grimes 
85ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether);
86602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
87df8bae1dSRodney W. Grimes 
88df8bae1dSRodney W. Grimes /* timer values */
8944e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
9044e33a07SMarko Zec static int	arpt_keep; /* once resolved, good for 20 more minutes */
9144e33a07SMarko Zec static int	arp_maxtries;
926e6b3f7cSQing Li int	useloopback; /* use loopback interface for local traffic */
9344e33a07SMarko Zec static int	arp_proxyall;
9444e33a07SMarko Zec #endif
95885f1aa4SPoul-Henning Kamp 
9697021c24SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, max_age,
9797021c24SMarko Zec     CTLFLAG_RW, arpt_keep, 0, "ARP entry lifetime in seconds");
98885f1aa4SPoul-Henning Kamp 
991cafed39SJonathan Lemon static struct	ifqueue arpintrq;
100df8bae1dSRodney W. Grimes 
1018b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, maxtries,
1028b615593SMarko Zec 	CTLFLAG_RW, arp_maxtries, 0,
1038b615593SMarko Zec 	"ARP resolution attempts before returning error");
1048b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, useloopback,
1058b615593SMarko Zec 	CTLFLAG_RW, useloopback, 0,
1068b615593SMarko Zec 	"Use the loopback interface for local traffic");
1078b615593SMarko Zec SYSCTL_V_INT(V_NET, vnet_inet, _net_link_ether_inet, OID_AUTO, proxyall,
1088b615593SMarko Zec 	CTLFLAG_RW, arp_proxyall, 0,
1098b615593SMarko Zec 	"Enable proxy ARP for all suitable requests");
110885f1aa4SPoul-Henning Kamp 
1114d77a549SAlfred Perlstein static void	arp_init(void);
1126e6b3f7cSQing Li void		arprequest(struct ifnet *,
1134d77a549SAlfred Perlstein 			struct in_addr *, struct in_addr *, u_char *);
1141cafed39SJonathan Lemon static void	arpintr(struct mbuf *);
1154d77a549SAlfred Perlstein static void	arptimer(void *);
1161d5e9e22SEivind Eklund #ifdef INET
1174d77a549SAlfred Perlstein static void	in_arpinput(struct mbuf *);
1181d5e9e22SEivind Eklund #endif
11928e82295SGarrett Wollman 
1206e6b3f7cSQing Li #ifdef AF_INET
1216e6b3f7cSQing Li void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
1226e6b3f7cSQing Li 
123df8bae1dSRodney W. Grimes /*
1246e6b3f7cSQing Li  * called by in_ifscrub to remove entry from the table when
1256e6b3f7cSQing Li  * the interface goes away
1266e6b3f7cSQing Li  */
1276e6b3f7cSQing Li void
1286e6b3f7cSQing Li arp_ifscrub(struct ifnet *ifp, uint32_t addr)
1296e6b3f7cSQing Li {
1306e6b3f7cSQing Li 	struct sockaddr_in addr4;
1316e6b3f7cSQing Li 
1326e6b3f7cSQing Li 	bzero((void *)&addr4, sizeof(addr4));
1336e6b3f7cSQing Li 	addr4.sin_len    = sizeof(addr4);
1346e6b3f7cSQing Li 	addr4.sin_family = AF_INET;
1356e6b3f7cSQing Li 	addr4.sin_addr.s_addr = addr;
1366e6b3f7cSQing Li 	IF_AFDATA_LOCK(ifp);
1376e6b3f7cSQing Li 	lla_lookup(LLTABLE(ifp), (LLE_DELETE | LLE_IFADDR),
1386e6b3f7cSQing Li 	    (struct sockaddr *)&addr4);
1396e6b3f7cSQing Li 	IF_AFDATA_UNLOCK(ifp);
1406e6b3f7cSQing Li }
1416e6b3f7cSQing Li #endif
1426e6b3f7cSQing Li 
1436e6b3f7cSQing Li /*
1446e6b3f7cSQing Li  * Timeout routine.  Age arp_tab entries periodically.
145df8bae1dSRodney W. Grimes  */
146df8bae1dSRodney W. Grimes static void
1471daaa65dSGleb Smirnoff arptimer(void *arg)
148df8bae1dSRodney W. Grimes {
1496e6b3f7cSQing Li 	struct ifnet *ifp;
1506e6b3f7cSQing Li 	struct llentry   *lle = (struct llentry *)arg;
151df8bae1dSRodney W. Grimes 
1526e6b3f7cSQing Li 	if (lle == NULL) {
1536e6b3f7cSQing Li 		panic("%s: NULL entry!\n", __func__);
154df8bae1dSRodney W. Grimes 		return;
1556e6b3f7cSQing Li 	}
1566e6b3f7cSQing Li 	ifp = lle->lle_tbl->llt_ifp;
1576e6b3f7cSQing Li 	IF_AFDATA_LOCK(ifp);
1586e6b3f7cSQing Li 	LLE_WLOCK(lle);
1596e6b3f7cSQing Li 	if ((lle->la_flags & LLE_DELETED) ||
1606e6b3f7cSQing Li 	    (time_second >= lle->la_expire)) {
1616e6b3f7cSQing Li 		if (!callout_pending(&lle->la_timer) &&
1626e6b3f7cSQing Li 		    callout_active(&lle->la_timer))
1636e6b3f7cSQing Li 			(void) llentry_free(lle);
1646e6b3f7cSQing Li 	} else {
165df8bae1dSRodney W. Grimes 		/*
1666e6b3f7cSQing Li 		 * Still valid, just drop our reference
167df8bae1dSRodney W. Grimes 		 */
1686e6b3f7cSQing Li 		LLE_FREE_LOCKED(lle);
169df8bae1dSRodney W. Grimes 	}
1706e6b3f7cSQing Li 	IF_AFDATA_UNLOCK(ifp);
171df8bae1dSRodney W. Grimes }
172df8bae1dSRodney W. Grimes 
173df8bae1dSRodney W. Grimes /*
174df8bae1dSRodney W. Grimes  * Broadcast an ARP request. Caller specifies:
175df8bae1dSRodney W. Grimes  *	- arp header source ip address
176df8bae1dSRodney W. Grimes  *	- arp header target ip address
177df8bae1dSRodney W. Grimes  *	- arp header source ethernet address
178df8bae1dSRodney W. Grimes  */
1796e6b3f7cSQing Li void
180f2565d68SRobert Watson arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr  *tip,
181f2565d68SRobert Watson     u_char *enaddr)
182df8bae1dSRodney W. Grimes {
183e952fa39SMatthew N. Dodd 	struct mbuf *m;
184e952fa39SMatthew N. Dodd 	struct arphdr *ah;
185df8bae1dSRodney W. Grimes 	struct sockaddr sa;
186df8bae1dSRodney W. Grimes 
1876e6b3f7cSQing Li 	if (sip == NULL) {
1886e6b3f7cSQing Li 		/* XXX don't believe this can happen (or explain why) */
1896e6b3f7cSQing Li 		/*
1906e6b3f7cSQing Li 		 * The caller did not supply a source address, try to find
1916e6b3f7cSQing Li 		 * a compatible one among those assigned to this interface.
1926e6b3f7cSQing Li 		 */
1936e6b3f7cSQing Li 		struct ifaddr *ifa;
1946e6b3f7cSQing Li 
1956e6b3f7cSQing Li 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1966e6b3f7cSQing Li 			if (!ifa->ifa_addr ||
1976e6b3f7cSQing Li 			    ifa->ifa_addr->sa_family != AF_INET)
1986e6b3f7cSQing Li 				continue;
1996e6b3f7cSQing Li 			sip = &SIN(ifa->ifa_addr)->sin_addr;
2006e6b3f7cSQing Li 			if (0 == ((sip->s_addr ^ tip->s_addr) &
2016e6b3f7cSQing Li 			    SIN(ifa->ifa_netmask)->sin_addr.s_addr) )
2026e6b3f7cSQing Li 				break;  /* found it. */
2036e6b3f7cSQing Li 		}
2046e6b3f7cSQing Li 		if (sip == NULL) {
2056e6b3f7cSQing Li 			printf("%s: cannot find matching address\n", __func__);
2066e6b3f7cSQing Li 			return;
2076e6b3f7cSQing Li 		}
2086e6b3f7cSQing Li 	}
2096e6b3f7cSQing Li 
210a163d034SWarner Losh 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
211df8bae1dSRodney W. Grimes 		return;
21264bf80ceSMatthew N. Dodd 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
21364bf80ceSMatthew N. Dodd 		2*ifp->if_data.ifi_addrlen;
21464bf80ceSMatthew N. Dodd 	m->m_pkthdr.len = m->m_len;
21564bf80ceSMatthew N. Dodd 	MH_ALIGN(m, m->m_len);
21664bf80ceSMatthew N. Dodd 	ah = mtod(m, struct arphdr *);
21764bf80ceSMatthew N. Dodd 	bzero((caddr_t)ah, m->m_len);
21819527d3eSRobert Watson #ifdef MAC
219b9b0dac3SRobert Watson 	mac_netinet_arp_send(ifp, m);
22019527d3eSRobert Watson #endif
221322dcb8dSMax Khon 	ah->ar_pro = htons(ETHERTYPE_IP);
222322dcb8dSMax Khon 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
223322dcb8dSMax Khon 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
224322dcb8dSMax Khon 	ah->ar_op = htons(ARPOP_REQUEST);
22564bf80ceSMatthew N. Dodd 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
22664bf80ceSMatthew N. Dodd 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
22764bf80ceSMatthew N. Dodd 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
22864bf80ceSMatthew N. Dodd 	sa.sa_family = AF_ARP;
22964bf80ceSMatthew N. Dodd 	sa.sa_len = 2;
23064bf80ceSMatthew N. Dodd 	m->m_flags |= M_BCAST;
231322dcb8dSMax Khon 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
232df8bae1dSRodney W. Grimes }
233df8bae1dSRodney W. Grimes 
234df8bae1dSRodney W. Grimes /*
235cd46a114SLuigi Rizzo  * Resolve an IP address into an ethernet address.
236cd46a114SLuigi Rizzo  * On input:
237cd46a114SLuigi Rizzo  *    ifp is the interface we use
238cd46a114SLuigi Rizzo  *    rt0 is the route to the final destination (possibly useless)
239b6ae6984SJulian Elischer  *    m is the mbuf. May be NULL if we don't have a packet.
240b6ae6984SJulian Elischer  *    dst is the next hop,
241cd46a114SLuigi Rizzo  *    desten is where we want the address.
242cd46a114SLuigi Rizzo  *
243cd46a114SLuigi Rizzo  * On success, desten is filled in and the function returns 0;
244cd46a114SLuigi Rizzo  * If the packet must be held pending resolution, we return EWOULDBLOCK
245cd46a114SLuigi Rizzo  * On other errors, we return the corresponding error code.
246b6ae6984SJulian Elischer  * Note that m_freem() handles NULL.
247df8bae1dSRodney W. Grimes  */
248df8bae1dSRodney W. Grimes int
249cd46a114SLuigi Rizzo arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
2506e6b3f7cSQing Li 	struct sockaddr *dst, u_char *desten, struct llentry **lle)
251df8bae1dSRodney W. Grimes {
2528b615593SMarko Zec 	INIT_VNET_INET(ifp->if_vnet);
2536e6b3f7cSQing Li 	struct llentry *la = 0;
25400a46b31SKip Macy 	u_int flags = 0;
2556e6b3f7cSQing Li 	int error, renew;
256df8bae1dSRodney W. Grimes 
2576e6b3f7cSQing Li 	*lle = NULL;
2586e6b3f7cSQing Li 	if (m != NULL) {
259b6ae6984SJulian Elischer 		if (m->m_flags & M_BCAST) {
260b6ae6984SJulian Elischer 			/* broadcast */
261b6ae6984SJulian Elischer 			(void)memcpy(desten,
262b6ae6984SJulian Elischer 			    ifp->if_broadcastaddr, ifp->if_addrlen);
263cd46a114SLuigi Rizzo 			return (0);
264df8bae1dSRodney W. Grimes 		}
265b6ae6984SJulian Elischer 		if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {
266b6ae6984SJulian Elischer 			/* multicast */
267df8bae1dSRodney W. Grimes 			ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
268cd46a114SLuigi Rizzo 			return (0);
269df8bae1dSRodney W. Grimes 		}
270b6ae6984SJulian Elischer 	}
2716e6b3f7cSQing Li 	/* XXXXX
27293fcb5a2SJulian Elischer 	 */
2736e6b3f7cSQing Li retry:
27400a46b31SKip Macy 	IF_AFDATA_RLOCK(ifp);
2756e6b3f7cSQing Li 	la = lla_lookup(LLTABLE(ifp), flags, dst);
27600a46b31SKip Macy 	IF_AFDATA_RUNLOCK(ifp);
27700a46b31SKip Macy 	if ((la == NULL) && ((flags & LLE_EXCLUSIVE) == 0)
27800a46b31SKip Macy 	    && ((ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0)) {
27900a46b31SKip Macy 		flags |= (LLE_CREATE | LLE_EXCLUSIVE);
28000a46b31SKip Macy 		IF_AFDATA_WLOCK(ifp);
28100a46b31SKip Macy 		la = lla_lookup(LLTABLE(ifp), flags, dst);
28200a46b31SKip Macy 		IF_AFDATA_WUNLOCK(ifp);
28300a46b31SKip Macy 	}
2841ed7bf1eSGleb Smirnoff 	if (la == NULL) {
2856e6b3f7cSQing Li 		if (flags & LLE_CREATE)
2861ed7bf1eSGleb Smirnoff 			log(LOG_DEBUG,
2871ed7bf1eSGleb Smirnoff 			    "arpresolve: can't allocate llinfo for %s\n",
2881ed7bf1eSGleb Smirnoff 			    inet_ntoa(SIN(dst)->sin_addr));
2891ed7bf1eSGleb Smirnoff 		m_freem(m);
2906e6b3f7cSQing Li 		return (EINVAL);
2911ed7bf1eSGleb Smirnoff 	}
292a20e2538SGleb Smirnoff 
2936e6b3f7cSQing Li 	if ((la->la_flags & LLE_VALID) &&
2946e6b3f7cSQing Li 	    ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
2956e6b3f7cSQing Li 		bcopy(&la->ll_addr, desten, ifp->if_addrlen);
296f0f3379eSOrion Hodson 		/*
297f0f3379eSOrion Hodson 		 * If entry has an expiry time and it is approaching,
2986e6b3f7cSQing Li 		 * see if we need to send an ARP request within this
2996e6b3f7cSQing Li 		 * arpt_down interval.
300f0f3379eSOrion Hodson 		 */
3016e6b3f7cSQing Li 		if (!(la->la_flags & LLE_STATIC) &&
3026e6b3f7cSQing Li 		    time_uptime + la->la_preempt > la->la_expire) {
3036e6b3f7cSQing Li 			arprequest(ifp, NULL,
3046e6b3f7cSQing Li 			    &SIN(dst)->sin_addr, IF_LLADDR(ifp));
305a20e2538SGleb Smirnoff 
306022695f8SOrion Hodson 			la->la_preempt--;
307f0f3379eSOrion Hodson 		}
308f0f3379eSOrion Hodson 
3096e6b3f7cSQing Li 		*lle = la;
3106e6b3f7cSQing Li 		error = 0;
3116e6b3f7cSQing Li 		goto done;
312df8bae1dSRodney W. Grimes 	}
3136e6b3f7cSQing Li 
3146e6b3f7cSQing Li 	if (la->la_flags & LLE_STATIC) {   /* should not happen! */
3156e6b3f7cSQing Li 		log(LOG_DEBUG, "arpresolve: ouch, empty static llinfo for %s\n",
3166e6b3f7cSQing Li 		    inet_ntoa(SIN(dst)->sin_addr));
31747891de1SRuslan Ermilov 		m_freem(m);
3186e6b3f7cSQing Li 		error = EINVAL;
3196e6b3f7cSQing Li 		goto done;
3206e6b3f7cSQing Li 	}
3216e6b3f7cSQing Li 
3226e6b3f7cSQing Li 	renew = (la->la_asked == 0 || la->la_expire != time_uptime);
3236e6b3f7cSQing Li 	if ((renew || m != NULL) && (flags & LLE_EXCLUSIVE) == 0) {
3246e6b3f7cSQing Li 		flags |= LLE_EXCLUSIVE;
3256e6b3f7cSQing Li 		LLE_RUNLOCK(la);
3266e6b3f7cSQing Li 		goto retry;
32747891de1SRuslan Ermilov 	}
32808aadfbbSJonathan Lemon 	/*
329df8bae1dSRodney W. Grimes 	 * There is an arptab entry, but no ethernet address
330df8bae1dSRodney W. Grimes 	 * response yet.  Replace the held mbuf with this
331df8bae1dSRodney W. Grimes 	 * latest one.
332df8bae1dSRodney W. Grimes 	 */
3336e6b3f7cSQing Li 	if (m != NULL) {
3346e6b3f7cSQing Li 		if (la->la_hold != NULL)
335df8bae1dSRodney W. Grimes 			m_freem(la->la_hold);
336df8bae1dSRodney W. Grimes 		la->la_hold = m;
3376e6b3f7cSQing Li 		if (renew == 0 && (flags & LLE_EXCLUSIVE)) {
3386e6b3f7cSQing Li 			flags &= ~LLE_EXCLUSIVE;
3396e6b3f7cSQing Li 			LLE_DOWNGRADE(la);
34058505389SKip Macy 		}
341e1ff74c5SGleb Smirnoff 
3426e6b3f7cSQing Li 	}
343e1ff74c5SGleb Smirnoff 	/*
344e1ff74c5SGleb Smirnoff 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
345e1ff74c5SGleb Smirnoff 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
346e1ff74c5SGleb Smirnoff 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
347e1ff74c5SGleb Smirnoff 	 * ARP request, but not faster than one request per second.
348e1ff74c5SGleb Smirnoff 	 */
349603724d3SBjoern A. Zeeb 	if (la->la_asked < V_arp_maxtries)
350e1ff74c5SGleb Smirnoff 		error = EWOULDBLOCK;	/* First request. */
351e1ff74c5SGleb Smirnoff 	else
3526e6b3f7cSQing Li 		error =
3536e6b3f7cSQing Li 		    (rt0->rt_flags & RTF_GATEWAY) ? EHOSTDOWN : EHOSTUNREACH;
354e1ff74c5SGleb Smirnoff 
3556e6b3f7cSQing Li 	if (renew) {
3566e6b3f7cSQing Li 		LLE_ADDREF(la);
3576e6b3f7cSQing Li 		la->la_expire = time_uptime;
3586e6b3f7cSQing Li 		callout_reset(&la->la_timer, hz, arptimer, la);
35995ebcabeSMaxim Konovalov 		la->la_asked++;
3606e6b3f7cSQing Li 		LLE_WUNLOCK(la);
3616e6b3f7cSQing Li 		arprequest(ifp, NULL, &SIN(dst)->sin_addr,
362322dcb8dSMax Khon 		    IF_LLADDR(ifp));
3636e6b3f7cSQing Li 		return (error);
3646e6b3f7cSQing Li 	}
3656e6b3f7cSQing Li done:
3666e6b3f7cSQing Li 	if (flags & LLE_EXCLUSIVE)
3676e6b3f7cSQing Li 		LLE_WUNLOCK(la);
3686e6b3f7cSQing Li 	else
3696e6b3f7cSQing Li 		LLE_RUNLOCK(la);
370e1ff74c5SGleb Smirnoff 	return (error);
371df8bae1dSRodney W. Grimes }
372df8bae1dSRodney W. Grimes 
373df8bae1dSRodney W. Grimes /*
374df8bae1dSRodney W. Grimes  * Common length and type checks are done here,
375df8bae1dSRodney W. Grimes  * then the protocol-specific routine is called.
376df8bae1dSRodney W. Grimes  */
377885f1aa4SPoul-Henning Kamp static void
3781cafed39SJonathan Lemon arpintr(struct mbuf *m)
379df8bae1dSRodney W. Grimes {
3801cafed39SJonathan Lemon 	struct arphdr *ar;
381df8bae1dSRodney W. Grimes 
38276ec7b2fSRobert Watson 	if (m->m_len < sizeof(struct arphdr) &&
38384365e2bSMatthew Dillon 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
384e44d6283SJoerg Wunsch 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
3851cafed39SJonathan Lemon 		return;
38676ec7b2fSRobert Watson 	}
38776ec7b2fSRobert Watson 	ar = mtod(m, struct arphdr *);
38876ec7b2fSRobert Watson 
3891cafed39SJonathan Lemon 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
3901cafed39SJonathan Lemon 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
391b8b33234SDoug Rabson 	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
392b8b33234SDoug Rabson 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
3931cafed39SJonathan Lemon 		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
39476ec7b2fSRobert Watson 		    (unsigned char *)&ar->ar_hrd, "");
39576ec7b2fSRobert Watson 		m_freem(m);
3961cafed39SJonathan Lemon 		return;
39776ec7b2fSRobert Watson 	}
39876ec7b2fSRobert Watson 
39931175791SRuslan Ermilov 	if (m->m_len < arphdr_len(ar)) {
40078e2d2bdSRuslan Ermilov 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
401f72d9d83SJoerg Wunsch 			log(LOG_ERR, "arp: runt packet\n");
40276ec7b2fSRobert Watson 			m_freem(m);
4031cafed39SJonathan Lemon 			return;
40476ec7b2fSRobert Watson 		}
40578e2d2bdSRuslan Ermilov 		ar = mtod(m, struct arphdr *);
40678e2d2bdSRuslan Ermilov 	}
407df8bae1dSRodney W. Grimes 
408df8bae1dSRodney W. Grimes 	switch (ntohs(ar->ar_pro)) {
4091d5e9e22SEivind Eklund #ifdef INET
410df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
411df8bae1dSRodney W. Grimes 		in_arpinput(m);
4121cafed39SJonathan Lemon 		return;
4131d5e9e22SEivind Eklund #endif
414df8bae1dSRodney W. Grimes 	}
415df8bae1dSRodney W. Grimes 	m_freem(m);
416df8bae1dSRodney W. Grimes }
417df8bae1dSRodney W. Grimes 
4181d5e9e22SEivind Eklund #ifdef INET
419df8bae1dSRodney W. Grimes /*
420df8bae1dSRodney W. Grimes  * ARP for Internet protocols on 10 Mb/s Ethernet.
421df8bae1dSRodney W. Grimes  * Algorithm is that given in RFC 826.
422df8bae1dSRodney W. Grimes  * In addition, a sanity check is performed on the sender
423df8bae1dSRodney W. Grimes  * protocol address, to catch impersonators.
424df8bae1dSRodney W. Grimes  * We no longer handle negotiations for use of trailer protocol:
425df8bae1dSRodney W. Grimes  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
426df8bae1dSRodney W. Grimes  * along with IP replies if we wanted trailers sent to us,
427df8bae1dSRodney W. Grimes  * and also sent them in response to IP replies.
428df8bae1dSRodney W. Grimes  * This allowed either end to announce the desire to receive
429df8bae1dSRodney W. Grimes  * trailer packets.
430df8bae1dSRodney W. Grimes  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
431df8bae1dSRodney W. Grimes  * but formerly didn't normally send requests.
432df8bae1dSRodney W. Grimes  */
4333269187dSAlfred Perlstein static int log_arp_wrong_iface = 1;
434e3d123d6SAlfred Perlstein static int log_arp_movements = 1;
43539393906SGleb Smirnoff static int log_arp_permanent_modify = 1;
4363269187dSAlfred Perlstein 
4373269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
4383269187dSAlfred Perlstein 	&log_arp_wrong_iface, 0,
4393269187dSAlfred Perlstein 	"log arp packets arriving on the wrong interface");
440e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
441e3d123d6SAlfred Perlstein         &log_arp_movements, 0,
44275ce3221SAlfred Perlstein         "log arp replies from MACs different than the one in the cache");
44339393906SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
44439393906SGleb Smirnoff         &log_arp_permanent_modify, 0,
44539393906SGleb Smirnoff         "log arp replies from MACs different than the one in the permanent arp entry");
446e3d123d6SAlfred Perlstein 
4473269187dSAlfred Perlstein 
448df8bae1dSRodney W. Grimes static void
449f2565d68SRobert Watson in_arpinput(struct mbuf *m)
450df8bae1dSRodney W. Grimes {
451e952fa39SMatthew N. Dodd 	struct arphdr *ah;
452e952fa39SMatthew N. Dodd 	struct ifnet *ifp = m->m_pkthdr.rcvif;
4536e6b3f7cSQing Li 	struct llentry *la = NULL;
454e952fa39SMatthew N. Dodd 	struct rtentry *rt;
455ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
456ca925d9cSJonathan Lemon 	struct in_ifaddr *ia;
457df8bae1dSRodney W. Grimes 	struct sockaddr sa;
458df8bae1dSRodney W. Grimes 	struct in_addr isaddr, itaddr, myaddr;
459a9771948SGleb Smirnoff 	u_int8_t *enaddr = NULL;
4606e6b3f7cSQing Li 	int op, flags;
4616e6b3f7cSQing Li 	struct mbuf *m0;
462322dcb8dSMax Khon 	int req_len;
46380b11ee4SPhilip Paeps 	int bridged = 0, is_bridge = 0;
464422a115aSGleb Smirnoff #ifdef DEV_CARP
4652ef4a436SGleb Smirnoff 	int carp_match = 0;
466422a115aSGleb Smirnoff #endif
4678e7e854cSKip Macy 	struct sockaddr_in sin;
4688e7e854cSKip Macy 	sin.sin_len = sizeof(struct sockaddr_in);
4698e7e854cSKip Macy 	sin.sin_family = AF_INET;
47029910a5aSKip Macy 	sin.sin_addr.s_addr = 0;
4718b615593SMarko Zec 	INIT_VNET_INET(ifp->if_vnet);
472df8bae1dSRodney W. Grimes 
47374948aa6SAndrew Thompson 	if (ifp->if_bridge)
4748f867517SAndrew Thompson 		bridged = 1;
47580b11ee4SPhilip Paeps 	if (ifp->if_type == IFT_BRIDGE)
47680b11ee4SPhilip Paeps 		is_bridge = 1;
4778f867517SAndrew Thompson 
478322dcb8dSMax Khon 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
479322dcb8dSMax Khon 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
4804cbc8ad1SYaroslav Tykhiy 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
4814cbc8ad1SYaroslav Tykhiy 		return;
4824cbc8ad1SYaroslav Tykhiy 	}
4834cbc8ad1SYaroslav Tykhiy 
484322dcb8dSMax Khon 	ah = mtod(m, struct arphdr *);
485322dcb8dSMax Khon 	op = ntohs(ah->ar_op);
486322dcb8dSMax Khon 	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
487322dcb8dSMax Khon 	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
48832439868SGleb Smirnoff 
489ca925d9cSJonathan Lemon 	/*
490ca925d9cSJonathan Lemon 	 * For a bridge, we want to check the address irrespective
491ca925d9cSJonathan Lemon 	 * of the receive interface. (This will change slightly
492ca925d9cSJonathan Lemon 	 * when we have clusters of interfaces).
493a9771948SGleb Smirnoff 	 * If the interface does not match, but the recieving interface
494a9771948SGleb Smirnoff 	 * is part of carp, we call carp_iamatch to see if this is a
495a9771948SGleb Smirnoff 	 * request for the virtual host ip.
496a9771948SGleb Smirnoff 	 * XXX: This is really ugly!
497ca925d9cSJonathan Lemon 	 */
4982ef4a436SGleb Smirnoff 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
4997f2d8767SAndrew Thompson 		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
5006e6b3f7cSQing Li 		    ia->ia_ifp == ifp) &&
5012ef4a436SGleb Smirnoff 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
502ca925d9cSJonathan Lemon 			goto match;
5032ef4a436SGleb Smirnoff #ifdef DEV_CARP
5042ef4a436SGleb Smirnoff 		if (ifp->if_carp != NULL &&
5052ef4a436SGleb Smirnoff 		    carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
5062ef4a436SGleb Smirnoff 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
5072ef4a436SGleb Smirnoff 			carp_match = 1;
5082ef4a436SGleb Smirnoff 			goto match;
5092ef4a436SGleb Smirnoff 		}
5102ef4a436SGleb Smirnoff #endif
5112ef4a436SGleb Smirnoff 	}
512ca925d9cSJonathan Lemon 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
5137f2d8767SAndrew Thompson 		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
5146e6b3f7cSQing Li 		    ia->ia_ifp == ifp) &&
515ca925d9cSJonathan Lemon 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)
516ca925d9cSJonathan Lemon 			goto match;
51780b11ee4SPhilip Paeps 
51880b11ee4SPhilip Paeps #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia)				\
51980b11ee4SPhilip Paeps   (ia->ia_ifp->if_bridge == ifp->if_softc &&				\
52080b11ee4SPhilip Paeps   !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) &&	\
52180b11ee4SPhilip Paeps   addr == ia->ia_addr.sin_addr.s_addr)
52280b11ee4SPhilip Paeps 	/*
52380b11ee4SPhilip Paeps 	 * Check the case when bridge shares its MAC address with
52480b11ee4SPhilip Paeps 	 * some of its children, so packets are claimed by bridge
52580b11ee4SPhilip Paeps 	 * itself (bridge_input() does it first), but they are really
52680b11ee4SPhilip Paeps 	 * meant to be destined to the bridge member.
52780b11ee4SPhilip Paeps 	 */
52880b11ee4SPhilip Paeps 	if (is_bridge) {
52980b11ee4SPhilip Paeps 		LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
53080b11ee4SPhilip Paeps 			if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
53180b11ee4SPhilip Paeps 				ifp = ia->ia_ifp;
53280b11ee4SPhilip Paeps 				goto match;
53380b11ee4SPhilip Paeps 			}
53480b11ee4SPhilip Paeps 		}
53580b11ee4SPhilip Paeps 	}
53680b11ee4SPhilip Paeps #undef BDG_MEMBER_MATCHES_ARP
53780b11ee4SPhilip Paeps 
538ca925d9cSJonathan Lemon 	/*
539d8b84d9eSJonathan Lemon 	 * No match, use the first inet address on the receive interface
540ca925d9cSJonathan Lemon 	 * as a dummy address for the rest of the function.
541ca925d9cSJonathan Lemon 	 */
542d8b84d9eSJonathan Lemon 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
5434b97d7afSYaroslav Tykhiy 		if (ifa->ifa_addr->sa_family == AF_INET) {
544ec691a10SJonathan Lemon 			ia = ifatoia(ifa);
545ec691a10SJonathan Lemon 			goto match;
546ec691a10SJonathan Lemon 		}
547ec691a10SJonathan Lemon 	/*
548ec691a10SJonathan Lemon 	 * If bridging, fall back to using any inet address.
549ec691a10SJonathan Lemon 	 */
550603724d3SBjoern A. Zeeb 	if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL)
551b2a8ac7cSLuigi Rizzo 		goto drop;
552ca925d9cSJonathan Lemon match:
553a9771948SGleb Smirnoff 	if (!enaddr)
554a9771948SGleb Smirnoff 		enaddr = (u_int8_t *)IF_LLADDR(ifp);
555ca925d9cSJonathan Lemon 	myaddr = ia->ia_addr.sin_addr;
556a9771948SGleb Smirnoff 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
557b2a8ac7cSLuigi Rizzo 		goto drop;	/* it's from me, ignore it. */
558322dcb8dSMax Khon 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
559df8bae1dSRodney W. Grimes 		log(LOG_ERR,
560322dcb8dSMax Khon 		    "arp: link address is broadcast for IP address %s!\n",
561ef0cdf33SGarrett Wollman 		    inet_ntoa(isaddr));
562b2a8ac7cSLuigi Rizzo 		goto drop;
563df8bae1dSRodney W. Grimes 	}
56400fcf9d1SRobert Watson 	/*
56500fcf9d1SRobert Watson 	 * Warn if another host is using the same IP address, but only if the
56600fcf9d1SRobert Watson 	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
56700fcf9d1SRobert Watson 	 * case we suppress the warning to avoid false positive complaints of
56800fcf9d1SRobert Watson 	 * potential misconfiguration.
56900fcf9d1SRobert Watson 	 */
570f69453caSAndrew Thompson 	if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
571df8bae1dSRodney W. Grimes 		log(LOG_ERR,
5723affb6fbSYaroslav Tykhiy 		   "arp: %*D is using my IP address %s on %s!\n",
573322dcb8dSMax Khon 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
5743affb6fbSYaroslav Tykhiy 		   inet_ntoa(isaddr), ifp->if_xname);
575df8bae1dSRodney W. Grimes 		itaddr = myaddr;
576df8bae1dSRodney W. Grimes 		goto reply;
577df8bae1dSRodney W. Grimes 	}
578deb62e28SRuslan Ermilov 	if (ifp->if_flags & IFF_STATICARP)
579deb62e28SRuslan Ermilov 		goto reply;
5808b07e49aSJulian Elischer 
5816e6b3f7cSQing Li 	bzero(&sin, sizeof(sin));
5826e6b3f7cSQing Li 	sin.sin_len = sizeof(struct sockaddr_in);
5836e6b3f7cSQing Li 	sin.sin_family = AF_INET;
5846e6b3f7cSQing Li 	sin.sin_addr = isaddr;
5856e6b3f7cSQing Li 	flags = (itaddr.s_addr == myaddr.s_addr) ? LLE_CREATE : 0;
5866e6b3f7cSQing Li 	flags |= LLE_EXCLUSIVE;
5876e6b3f7cSQing Li 	IF_AFDATA_LOCK(ifp);
5886e6b3f7cSQing Li 	la = lla_lookup(LLTABLE(ifp), flags, (struct sockaddr *)&sin);
5896e6b3f7cSQing Li 	IF_AFDATA_UNLOCK(ifp);
5906e6b3f7cSQing Li 	if (la != NULL) {
5916e6b3f7cSQing Li 		/* the following is not an error when doing bridging */
5926e6b3f7cSQing Li 		if (!bridged && la->lle_tbl->llt_ifp != ifp
593422a115aSGleb Smirnoff #ifdef DEV_CARP
594422a115aSGleb Smirnoff 		    && (ifp->if_type != IFT_CARP || !carp_match)
595422a115aSGleb Smirnoff #endif
596422a115aSGleb Smirnoff 			) {
5973269187dSAlfred Perlstein 			if (log_arp_wrong_iface)
5988b07e49aSJulian Elischer 				log(LOG_ERR, "arp: %s is on %s "
5996e6b3f7cSQing Li 				    "but got reply from %*D on %s\n",
600dd9b6ddeSBill Fenner 				    inet_ntoa(isaddr),
6016e6b3f7cSQing Li 				    la->lle_tbl->llt_ifp->if_xname,
6026e6b3f7cSQing Li 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
6039bf40edeSBrooks Davis 				    ifp->if_xname);
6046e6b3f7cSQing Li 			goto reply;
605dd9b6ddeSBill Fenner 		}
6066e6b3f7cSQing Li 		if ((la->la_flags & LLE_VALID) &&
6076e6b3f7cSQing Li 		    bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
6086e6b3f7cSQing Li 			if (la->la_flags & LLE_STATIC) {
6098b07e49aSJulian Elischer 				log(LOG_ERR,
6106e6b3f7cSQing Li 				    "arp: %*D attempts to modify permanent "
6116e6b3f7cSQing Li 				    "entry for %s on %s\n",
6126e6b3f7cSQing Li 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
6136e6b3f7cSQing Li 				    inet_ntoa(isaddr), ifp->if_xname);
6146e6b3f7cSQing Li 				goto reply;
6156e6b3f7cSQing Li 			}
6166e6b3f7cSQing Li 			if (log_arp_movements) {
6176e6b3f7cSQing Li 			        log(LOG_INFO, "arp: %s moved from %*D "
6186e6b3f7cSQing Li 				    "to %*D on %s\n",
6198b07e49aSJulian Elischer 				    inet_ntoa(isaddr),
6206e6b3f7cSQing Li 				    ifp->if_addrlen,
6216e6b3f7cSQing Li 				    (u_char *)&la->ll_addr, ":",
6226e6b3f7cSQing Li 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
6238b07e49aSJulian Elischer 				    ifp->if_xname);
624dd9b6ddeSBill Fenner 			}
625dfd5dee1SPeter Wemm 		}
6266e6b3f7cSQing Li 
627322dcb8dSMax Khon 		if (ifp->if_addrlen != ah->ar_hln) {
628322dcb8dSMax Khon 			log(LOG_WARNING,
6296e6b3f7cSQing Li 			    "arp from %*D: addr len: new %d, i/f %d (ignored)",
6306e6b3f7cSQing Li 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
6316e6b3f7cSQing Li 			    ah->ar_hln, ifp->if_addrlen);
6326e6b3f7cSQing Li 			goto reply;
633322dcb8dSMax Khon 		}
6346e6b3f7cSQing Li 		(void)memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
6356e6b3f7cSQing Li 		la->la_flags |= LLE_VALID;
6368b07e49aSJulian Elischer 
6376e6b3f7cSQing Li 		if (!(la->la_flags & LLE_STATIC)) {
6386e6b3f7cSQing Li 			la->la_expire = time_uptime + V_arpt_keep;
639603724d3SBjoern A. Zeeb 			callout_reset(&la->la_timer, hz * V_arpt_keep,
6406e6b3f7cSQing Li 			    arptimer, la);
6411daaa65dSGleb Smirnoff 		}
642022695f8SOrion Hodson 		la->la_asked = 0;
643603724d3SBjoern A. Zeeb 		la->la_preempt = V_arp_maxtries;
6446e6b3f7cSQing Li 		if (la->la_hold != NULL) {
6456e6b3f7cSQing Li 			m0 = la->la_hold;
6466e6b3f7cSQing Li 			la->la_hold = 0;
6476e6b3f7cSQing Li 			memcpy(&sa, L3_ADDR(la), sizeof(sa));
6486e6b3f7cSQing Li 			LLE_WUNLOCK(la);
6498b07e49aSJulian Elischer 
6506e6b3f7cSQing Li 			(*ifp->if_output)(ifp, m0, &sa, NULL);
6516e6b3f7cSQing Li 			return;
6526e6b3f7cSQing Li 		}
6536e6b3f7cSQing Li 	}
6546e6b3f7cSQing Li reply:
655b2a8ac7cSLuigi Rizzo 	if (op != ARPOP_REQUEST)
656b2a8ac7cSLuigi Rizzo 		goto drop;
6576e6b3f7cSQing Li 
658df8bae1dSRodney W. Grimes 	if (itaddr.s_addr == myaddr.s_addr) {
6598b07e49aSJulian Elischer 		/* Shortcut.. the receiving interface is the target. */
660322dcb8dSMax Khon 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
661a9771948SGleb Smirnoff 		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
662df8bae1dSRodney W. Grimes 	} else {
663897d75c9SQing Li 		struct llentry *lle = NULL;
664897d75c9SQing Li 
665603724d3SBjoern A. Zeeb 		if (!V_arp_proxyall)
666b2a8ac7cSLuigi Rizzo 			goto drop;
66728e82295SGarrett Wollman 
66828e82295SGarrett Wollman 		sin.sin_addr = itaddr;
6698b07e49aSJulian Elischer 		/* XXX MRT use table 0 for arp reply  */
6708b07e49aSJulian Elischer 		rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
671b2a8ac7cSLuigi Rizzo 		if (!rt)
672b2a8ac7cSLuigi Rizzo 			goto drop;
673897d75c9SQing Li 
67428e82295SGarrett Wollman 		/*
67528e82295SGarrett Wollman 		 * Don't send proxies for nodes on the same interface
67628e82295SGarrett Wollman 		 * as this one came out of, or we'll get into a fight
67728e82295SGarrett Wollman 		 * over who claims what Ether address.
67828e82295SGarrett Wollman 		 */
679897d75c9SQing Li 		if (!rt->rt_ifp || rt->rt_ifp == ifp) {
6804e57bc33SChristian S.J. Peron 			RTFREE_LOCKED(rt);
681b2a8ac7cSLuigi Rizzo 			goto drop;
68228e82295SGarrett Wollman 		}
683897d75c9SQing Li 		IF_AFDATA_LOCK(rt->rt_ifp);
684897d75c9SQing Li 		lle = lla_lookup(LLTABLE(rt->rt_ifp), 0, (struct sockaddr *)&sin);
685897d75c9SQing Li 		IF_AFDATA_UNLOCK(rt->rt_ifp);
6864e57bc33SChristian S.J. Peron 		RTFREE_LOCKED(rt);
687cc728227SDavid Malone 
688897d75c9SQing Li 		if (lle != NULL) {
689897d75c9SQing Li 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
690897d75c9SQing Li 			(void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
691897d75c9SQing Li 			LLE_RUNLOCK(lle);
692897d75c9SQing Li 		} else
693897d75c9SQing Li 			goto drop;
694897d75c9SQing Li 
695cc728227SDavid Malone 		/*
696cc728227SDavid Malone 		 * Also check that the node which sent the ARP packet
697cc728227SDavid Malone 		 * is on the the interface we expect it to be on. This
698cc728227SDavid Malone 		 * avoids ARP chaos if an interface is connected to the
699cc728227SDavid Malone 		 * wrong network.
700cc728227SDavid Malone 		 */
701cc728227SDavid Malone 		sin.sin_addr = isaddr;
702cc728227SDavid Malone 
7038b07e49aSJulian Elischer 		/* XXX MRT use table 0 for arp checks */
7048b07e49aSJulian Elischer 		rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
705b2a8ac7cSLuigi Rizzo 		if (!rt)
706b2a8ac7cSLuigi Rizzo 			goto drop;
707322dcb8dSMax Khon 		if (rt->rt_ifp != ifp) {
708cc728227SDavid Malone 			log(LOG_INFO, "arp_proxy: ignoring request"
7099bf40edeSBrooks Davis 			    " from %s via %s, expecting %s\n",
7109bf40edeSBrooks Davis 			    inet_ntoa(isaddr), ifp->if_xname,
7119bf40edeSBrooks Davis 			    rt->rt_ifp->if_xname);
7124e57bc33SChristian S.J. Peron 			RTFREE_LOCKED(rt);
713b2a8ac7cSLuigi Rizzo 			goto drop;
714cc728227SDavid Malone 		}
7154e57bc33SChristian S.J. Peron 		RTFREE_LOCKED(rt);
716cc728227SDavid Malone 
717ac234f93SGarrett Wollman #ifdef DEBUG_PROXY
718ac234f93SGarrett Wollman 		printf("arp: proxying for %s\n",
719ef0cdf33SGarrett Wollman 		       inet_ntoa(itaddr));
720ac234f93SGarrett Wollman #endif
721df8bae1dSRodney W. Grimes 	}
722df8bae1dSRodney W. Grimes 
7236e6b3f7cSQing Li 	if (la != NULL)
7246e6b3f7cSQing Li 		LLE_WUNLOCK(la);
725d0558157SBruce M Simpson 	if (itaddr.s_addr == myaddr.s_addr &&
726d0558157SBruce M Simpson 	    IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
727d0558157SBruce M Simpson 		/* RFC 3927 link-local IPv4; always reply by broadcast. */
728d0558157SBruce M Simpson #ifdef DEBUG_LINKLOCAL
729d0558157SBruce M Simpson 		printf("arp: sending reply for link-local addr %s\n",
730d0558157SBruce M Simpson 		    inet_ntoa(itaddr));
731d0558157SBruce M Simpson #endif
732d0558157SBruce M Simpson 		m->m_flags |= M_BCAST;
733d0558157SBruce M Simpson 		m->m_flags &= ~M_MCAST;
734d0558157SBruce M Simpson 	} else {
735d0558157SBruce M Simpson 		/* default behaviour; never reply by broadcast. */
736d0558157SBruce M Simpson 		m->m_flags &= ~(M_BCAST|M_MCAST);
737d0558157SBruce M Simpson 	}
738322dcb8dSMax Khon 	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
739322dcb8dSMax Khon 	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
740322dcb8dSMax Khon 	ah->ar_op = htons(ARPOP_REPLY);
741322dcb8dSMax Khon 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
74264bf80ceSMatthew N. Dodd 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
74364bf80ceSMatthew N. Dodd 	m->m_pkthdr.len = m->m_len;
74464bf80ceSMatthew N. Dodd 	sa.sa_family = AF_ARP;
74564bf80ceSMatthew N. Dodd 	sa.sa_len = 2;
746322dcb8dSMax Khon 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
747df8bae1dSRodney W. Grimes 	return;
748b2a8ac7cSLuigi Rizzo 
749b2a8ac7cSLuigi Rizzo drop:
7506e6b3f7cSQing Li 	if (la != NULL)
7516e6b3f7cSQing Li 		LLE_WUNLOCK(la);
752b2a8ac7cSLuigi Rizzo 	m_freem(m);
753df8bae1dSRodney W. Grimes }
7541d5e9e22SEivind Eklund #endif
755df8bae1dSRodney W. Grimes 
756dd2e4102SGarrett Wollman void
757f2565d68SRobert Watson arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
758dd2e4102SGarrett Wollman {
7596e6b3f7cSQing Li 	struct llentry *lle;
7606e6b3f7cSQing Li 
761dd570d4dSTor Egge 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
762322dcb8dSMax Khon 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
763322dcb8dSMax Khon 				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
7646e6b3f7cSQing Li 	/*
7656e6b3f7cSQing Li 	 * interface address is considered static entry
7666e6b3f7cSQing Li 	 * because the output of the arp utility shows
7676e6b3f7cSQing Li 	 * that L2 entry as permanent
7686e6b3f7cSQing Li 	 */
7696e6b3f7cSQing Li 	IF_AFDATA_LOCK(ifp);
7706e6b3f7cSQing Li 	lle = lla_lookup(LLTABLE(ifp), (LLE_CREATE | LLE_IFADDR | LLE_STATIC),
7716e6b3f7cSQing Li 	    (struct sockaddr *)IA_SIN(ifa));
7726e6b3f7cSQing Li 	IF_AFDATA_UNLOCK(ifp);
7736e6b3f7cSQing Li 	if (lle == NULL)
7746e6b3f7cSQing Li 		log(LOG_INFO, "arp_ifinit: cannot create arp "
7756e6b3f7cSQing Li 		    "entry for interface address\n");
77686cd829dSKip Macy 	else
7776e6b3f7cSQing Li 		LLE_RUNLOCK(lle);
7786e6b3f7cSQing Li 	ifa->ifa_rtrequest = NULL;
779dd2e4102SGarrett Wollman }
780df5e1987SJonathan Lemon 
781a9771948SGleb Smirnoff void
782f2565d68SRobert Watson arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
783a9771948SGleb Smirnoff {
784a9771948SGleb Smirnoff 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
785a9771948SGleb Smirnoff 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
786a9771948SGleb Smirnoff 				&IA_SIN(ifa)->sin_addr, enaddr);
7876e6b3f7cSQing Li 	ifa->ifa_rtrequest = NULL;
788a9771948SGleb Smirnoff }
789a9771948SGleb Smirnoff 
790df5e1987SJonathan Lemon static void
791df5e1987SJonathan Lemon arp_init(void)
792df5e1987SJonathan Lemon {
79344e33a07SMarko Zec 	INIT_VNET_INET(curvnet);
79444e33a07SMarko Zec 
79544e33a07SMarko Zec 	V_arpt_keep = (20*60); /* once resolved, good for 20 more minutes */
79644e33a07SMarko Zec 	V_arp_maxtries = 5;
79744e33a07SMarko Zec 	V_useloopback = 1; /* use loopback interface for local traffic */
79844e33a07SMarko Zec 	V_arp_proxyall = 0;
799df5e1987SJonathan Lemon 
800df5e1987SJonathan Lemon 	arpintrq.ifq_maxlen = 50;
8016008862bSJohn Baldwin 	mtx_init(&arpintrq.ifq_mtx, "arp_inq", NULL, MTX_DEF);
80259dd72d0SRobert Watson 	netisr_register(NETISR_ARP, arpintr, &arpintrq, 0);
803df5e1987SJonathan Lemon }
804df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
805