xref: /freebsd/sys/netinet/if_ether.c (revision 93704ac5d738d1ba176f56af8a95c37aca69850e)
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"
42a9771948SGleb Smirnoff #include "opt_carp.h"
431d5e9e22SEivind Eklund 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/kernel.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>
524458ac71SBruce Evans #include <sys/socket.h>
53885f1aa4SPoul-Henning Kamp #include <sys/syslog.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <net/if.h>
56df8bae1dSRodney W. Grimes #include <net/if_dl.h>
57722012ccSJulian Elischer #include <net/if_types.h>
58748e0b0aSGarrett Wollman #include <net/netisr.h>
59b149dd6cSLarry Lile #include <net/if_llc.h>
60c8f8e9c1SJulian Elischer #include <net/ethernet.h>
615736e6fbSBjoern A. Zeeb #include <net/route.h>
62530c0060SRobert Watson #include <net/vnet.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <netinet/in.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
666e6b3f7cSQing Li #include <net/if_llatbl.h>
67df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
68df8bae1dSRodney W. Grimes 
69322dcb8dSMax Khon #include <net/if_arc.h>
70fda82fc2SJulian Elischer #include <net/iso88025.h>
71fda82fc2SJulian Elischer 
72a9771948SGleb Smirnoff #ifdef DEV_CARP
73a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
74a9771948SGleb Smirnoff #endif
75a9771948SGleb Smirnoff 
76aed55708SRobert Watson #include <security/mac/mac_framework.h>
77aed55708SRobert Watson 
78df8bae1dSRodney W. Grimes #define SIN(s) ((struct sockaddr_in *)s)
79df8bae1dSRodney W. Grimes #define SDL(s) ((struct sockaddr_dl *)s)
80df8bae1dSRodney W. Grimes 
81ce02431fSDoug Rabson SYSCTL_DECL(_net_link_ether);
82602d513cSGarrett Wollman SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
8354fc657dSGeorge V. Neville-Neil SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, "");
84df8bae1dSRodney W. Grimes 
85df813b7eSQing Li VNET_DEFINE(int, useloopback) = 1;	/* use loopback interface for
86df813b7eSQing Li 					 * local traffic */
87df813b7eSQing Li 
88df8bae1dSRodney W. Grimes /* timer values */
89eddfbb76SRobert Watson static VNET_DEFINE(int, arpt_keep) = (20*60);	/* once resolved, good for 20
90eddfbb76SRobert Watson 						 * minutes */
9193704ac5SQing Li static VNET_DEFINE(int, arpt_down) = 20;      /* keep incomplete entries for
9293704ac5SQing Li 					       * 20 seconds */
93eddfbb76SRobert Watson static VNET_DEFINE(int, arp_maxtries) = 5;
94eddfbb76SRobert Watson static VNET_DEFINE(int, arp_proxyall);
9554fc657dSGeorge V. Neville-Neil static VNET_DEFINE(struct arpstat, arpstat);  /* ARP statistics, see if_arp.h */
96885f1aa4SPoul-Henning Kamp 
971e77c105SRobert Watson #define	V_arpt_keep		VNET(arpt_keep)
9893704ac5SQing Li #define	V_arpt_down		VNET(arpt_down)
991e77c105SRobert Watson #define	V_arp_maxtries		VNET(arp_maxtries)
1001e77c105SRobert Watson #define	V_arp_proxyall		VNET(arp_proxyall)
10154fc657dSGeorge V. Neville-Neil #define	V_arpstat		VNET(arpstat)
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 
107eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_RW,
108eddfbb76SRobert Watson 	&VNET_NAME(arp_maxtries), 0,
1098b615593SMarko Zec 	"ARP resolution attempts before returning error");
110eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, useloopback, CTLFLAG_RW,
111eddfbb76SRobert Watson 	&VNET_NAME(useloopback), 0,
1128b615593SMarko Zec 	"Use the loopback interface for local traffic");
113eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_RW,
114eddfbb76SRobert Watson 	&VNET_NAME(arp_proxyall), 0,
1158b615593SMarko Zec 	"Enable proxy ARP for all suitable requests");
11654fc657dSGeorge V. Neville-Neil SYSCTL_VNET_STRUCT(_net_link_ether_arp, OID_AUTO, stats, CTLFLAG_RW,
11754fc657dSGeorge V. Neville-Neil 	&VNET_NAME(arpstat), arpstat,
11854fc657dSGeorge V. Neville-Neil 	"ARP statistics (struct arpstat, net/if_arp.h)");
119885f1aa4SPoul-Henning Kamp 
1204d77a549SAlfred Perlstein static void	arp_init(void);
1216e6b3f7cSQing Li void		arprequest(struct ifnet *,
1224d77a549SAlfred Perlstein 			struct in_addr *, struct in_addr *, u_char *);
1231cafed39SJonathan Lemon static void	arpintr(struct mbuf *);
1244d77a549SAlfred Perlstein static void	arptimer(void *);
1251d5e9e22SEivind Eklund #ifdef INET
1264d77a549SAlfred Perlstein static void	in_arpinput(struct mbuf *);
1271d5e9e22SEivind Eklund #endif
12828e82295SGarrett Wollman 
129d4b5cae4SRobert Watson static const struct netisr_handler arp_nh = {
130d4b5cae4SRobert Watson 	.nh_name = "arp",
131d4b5cae4SRobert Watson 	.nh_handler = arpintr,
132d4b5cae4SRobert Watson 	.nh_proto = NETISR_ARP,
133d4b5cae4SRobert Watson 	.nh_policy = NETISR_POLICY_SOURCE,
134d4b5cae4SRobert Watson };
135d4b5cae4SRobert Watson 
1366e6b3f7cSQing Li #ifdef AF_INET
1376e6b3f7cSQing Li void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
1386e6b3f7cSQing Li 
139df8bae1dSRodney W. Grimes /*
1406e6b3f7cSQing Li  * called by in_ifscrub to remove entry from the table when
1416e6b3f7cSQing Li  * the interface goes away
1426e6b3f7cSQing Li  */
1436e6b3f7cSQing Li void
1446e6b3f7cSQing Li arp_ifscrub(struct ifnet *ifp, uint32_t addr)
1456e6b3f7cSQing Li {
1466e6b3f7cSQing Li 	struct sockaddr_in addr4;
1476e6b3f7cSQing Li 
1486e6b3f7cSQing Li 	bzero((void *)&addr4, sizeof(addr4));
1496e6b3f7cSQing Li 	addr4.sin_len    = sizeof(addr4);
1506e6b3f7cSQing Li 	addr4.sin_family = AF_INET;
1516e6b3f7cSQing Li 	addr4.sin_addr.s_addr = addr;
15221ca7b57SMarko Zec 	CURVNET_SET(ifp->if_vnet);
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);
15721ca7b57SMarko Zec 	CURVNET_RESTORE();
1586e6b3f7cSQing Li }
1596e6b3f7cSQing Li #endif
1606e6b3f7cSQing Li 
1616e6b3f7cSQing Li /*
1626e6b3f7cSQing Li  * Timeout routine.  Age arp_tab entries periodically.
163df8bae1dSRodney W. Grimes  */
164df8bae1dSRodney W. Grimes static void
1651daaa65dSGleb Smirnoff arptimer(void *arg)
166df8bae1dSRodney W. Grimes {
1676e6b3f7cSQing Li 	struct ifnet *ifp;
1686e6b3f7cSQing Li 	struct llentry   *lle = (struct llentry *)arg;
169df8bae1dSRodney W. Grimes 
1706e6b3f7cSQing Li 	if (lle == NULL) {
1716e6b3f7cSQing Li 		panic("%s: NULL entry!\n", __func__);
172df8bae1dSRodney W. Grimes 		return;
1736e6b3f7cSQing Li 	}
1746e6b3f7cSQing Li 	ifp = lle->lle_tbl->llt_ifp;
17554fc657dSGeorge V. Neville-Neil 	CURVNET_SET(ifp->if_vnet);
1766e6b3f7cSQing Li 	IF_AFDATA_LOCK(ifp);
1776e6b3f7cSQing Li 	LLE_WLOCK(lle);
17854fc657dSGeorge V. Neville-Neil 	if (((lle->la_flags & LLE_DELETED) ||
17954fc657dSGeorge V. Neville-Neil 	    (time_second >= lle->la_expire)) &&
18054fc657dSGeorge V. Neville-Neil 	    (!callout_pending(&lle->la_timer) &&
18154fc657dSGeorge V. Neville-Neil 	    callout_active(&lle->la_timer))) {
1826e6b3f7cSQing Li 		(void) llentry_free(lle);
18354fc657dSGeorge V. Neville-Neil 		ARPSTAT_INC(timeouts);
18454fc657dSGeorge V. Neville-Neil 	} else {
185df8bae1dSRodney W. Grimes 		/*
1866e6b3f7cSQing Li 		 * Still valid, just drop our reference
187df8bae1dSRodney W. Grimes 		 */
1886e6b3f7cSQing Li 		LLE_FREE_LOCKED(lle);
189df8bae1dSRodney W. Grimes 	}
1906e6b3f7cSQing Li 	IF_AFDATA_UNLOCK(ifp);
19154fc657dSGeorge V. Neville-Neil 	CURVNET_RESTORE();
192df8bae1dSRodney W. Grimes }
193df8bae1dSRodney W. Grimes 
194df8bae1dSRodney W. Grimes /*
195df8bae1dSRodney W. Grimes  * Broadcast an ARP request. Caller specifies:
196df8bae1dSRodney W. Grimes  *	- arp header source ip address
197df8bae1dSRodney W. Grimes  *	- arp header target ip address
198df8bae1dSRodney W. Grimes  *	- arp header source ethernet address
199df8bae1dSRodney W. Grimes  */
2006e6b3f7cSQing Li void
201f2565d68SRobert Watson arprequest(struct ifnet *ifp, struct in_addr *sip, struct in_addr  *tip,
202f2565d68SRobert Watson     u_char *enaddr)
203df8bae1dSRodney W. Grimes {
204e952fa39SMatthew N. Dodd 	struct mbuf *m;
205e952fa39SMatthew N. Dodd 	struct arphdr *ah;
206df8bae1dSRodney W. Grimes 	struct sockaddr sa;
207df8bae1dSRodney W. Grimes 
2086e6b3f7cSQing Li 	if (sip == NULL) {
2096e6b3f7cSQing Li 		/* XXX don't believe this can happen (or explain why) */
2106e6b3f7cSQing Li 		/*
2116e6b3f7cSQing Li 		 * The caller did not supply a source address, try to find
2126e6b3f7cSQing Li 		 * a compatible one among those assigned to this interface.
2136e6b3f7cSQing Li 		 */
2146e6b3f7cSQing Li 		struct ifaddr *ifa;
2156e6b3f7cSQing Li 
2166e6b3f7cSQing Li 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
2176e6b3f7cSQing Li 			if (!ifa->ifa_addr ||
2186e6b3f7cSQing Li 			    ifa->ifa_addr->sa_family != AF_INET)
2196e6b3f7cSQing Li 				continue;
2206e6b3f7cSQing Li 			sip = &SIN(ifa->ifa_addr)->sin_addr;
2216e6b3f7cSQing Li 			if (0 == ((sip->s_addr ^ tip->s_addr) &
2226e6b3f7cSQing Li 			    SIN(ifa->ifa_netmask)->sin_addr.s_addr) )
2236e6b3f7cSQing Li 				break;  /* found it. */
2246e6b3f7cSQing Li 		}
2256e6b3f7cSQing Li 		if (sip == NULL) {
2266e6b3f7cSQing Li 			printf("%s: cannot find matching address\n", __func__);
2276e6b3f7cSQing Li 			return;
2286e6b3f7cSQing Li 		}
2296e6b3f7cSQing Li 	}
2306e6b3f7cSQing Li 
231a163d034SWarner Losh 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
232df8bae1dSRodney W. Grimes 		return;
23364bf80ceSMatthew N. Dodd 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
23464bf80ceSMatthew N. Dodd 		2*ifp->if_data.ifi_addrlen;
23564bf80ceSMatthew N. Dodd 	m->m_pkthdr.len = m->m_len;
23664bf80ceSMatthew N. Dodd 	MH_ALIGN(m, m->m_len);
23764bf80ceSMatthew N. Dodd 	ah = mtod(m, struct arphdr *);
23864bf80ceSMatthew N. Dodd 	bzero((caddr_t)ah, m->m_len);
23919527d3eSRobert Watson #ifdef MAC
240b9b0dac3SRobert Watson 	mac_netinet_arp_send(ifp, m);
24119527d3eSRobert Watson #endif
242322dcb8dSMax Khon 	ah->ar_pro = htons(ETHERTYPE_IP);
243322dcb8dSMax Khon 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
244322dcb8dSMax Khon 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
245322dcb8dSMax Khon 	ah->ar_op = htons(ARPOP_REQUEST);
24664bf80ceSMatthew N. Dodd 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
24764bf80ceSMatthew N. Dodd 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
24864bf80ceSMatthew N. Dodd 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
24964bf80ceSMatthew N. Dodd 	sa.sa_family = AF_ARP;
25064bf80ceSMatthew N. Dodd 	sa.sa_len = 2;
25164bf80ceSMatthew N. Dodd 	m->m_flags |= M_BCAST;
252279aa3d4SKip Macy 	(*ifp->if_output)(ifp, m, &sa, NULL);
25354fc657dSGeorge V. Neville-Neil 	ARPSTAT_INC(txrequests);
254df8bae1dSRodney W. Grimes }
255df8bae1dSRodney W. Grimes 
256df8bae1dSRodney W. Grimes /*
257cd46a114SLuigi Rizzo  * Resolve an IP address into an ethernet address.
258cd46a114SLuigi Rizzo  * On input:
259cd46a114SLuigi Rizzo  *    ifp is the interface we use
260cd46a114SLuigi Rizzo  *    rt0 is the route to the final destination (possibly useless)
261b6ae6984SJulian Elischer  *    m is the mbuf. May be NULL if we don't have a packet.
262b6ae6984SJulian Elischer  *    dst is the next hop,
263cd46a114SLuigi Rizzo  *    desten is where we want the address.
264cd46a114SLuigi Rizzo  *
265cd46a114SLuigi Rizzo  * On success, desten is filled in and the function returns 0;
266cd46a114SLuigi Rizzo  * If the packet must be held pending resolution, we return EWOULDBLOCK
267cd46a114SLuigi Rizzo  * On other errors, we return the corresponding error code.
268b6ae6984SJulian Elischer  * Note that m_freem() handles NULL.
269df8bae1dSRodney W. Grimes  */
270df8bae1dSRodney W. Grimes int
271cd46a114SLuigi Rizzo arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
2726e6b3f7cSQing Li 	struct sockaddr *dst, u_char *desten, struct llentry **lle)
273df8bae1dSRodney W. Grimes {
2746e6b3f7cSQing Li 	struct llentry *la = 0;
27500a46b31SKip Macy 	u_int flags = 0;
2766e6b3f7cSQing Li 	int error, renew;
277df8bae1dSRodney W. Grimes 
2786e6b3f7cSQing Li 	*lle = NULL;
2796e6b3f7cSQing Li 	if (m != NULL) {
280b6ae6984SJulian Elischer 		if (m->m_flags & M_BCAST) {
281b6ae6984SJulian Elischer 			/* broadcast */
282b6ae6984SJulian Elischer 			(void)memcpy(desten,
283b6ae6984SJulian Elischer 			    ifp->if_broadcastaddr, ifp->if_addrlen);
284cd46a114SLuigi Rizzo 			return (0);
285df8bae1dSRodney W. Grimes 		}
286b6ae6984SJulian Elischer 		if (m->m_flags & M_MCAST && ifp->if_type != IFT_ARCNET) {
287b6ae6984SJulian Elischer 			/* multicast */
288df8bae1dSRodney W. Grimes 			ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
289cd46a114SLuigi Rizzo 			return (0);
290df8bae1dSRodney W. Grimes 		}
291b6ae6984SJulian Elischer 	}
2926e6b3f7cSQing Li 	/* XXXXX
29393fcb5a2SJulian Elischer 	 */
2946e6b3f7cSQing Li retry:
29500a46b31SKip Macy 	IF_AFDATA_RLOCK(ifp);
2966e6b3f7cSQing Li 	la = lla_lookup(LLTABLE(ifp), flags, dst);
29700a46b31SKip Macy 	IF_AFDATA_RUNLOCK(ifp);
29800a46b31SKip Macy 	if ((la == NULL) && ((flags & LLE_EXCLUSIVE) == 0)
29900a46b31SKip Macy 	    && ((ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0)) {
30000a46b31SKip Macy 		flags |= (LLE_CREATE | LLE_EXCLUSIVE);
30100a46b31SKip Macy 		IF_AFDATA_WLOCK(ifp);
30200a46b31SKip Macy 		la = lla_lookup(LLTABLE(ifp), flags, dst);
30300a46b31SKip Macy 		IF_AFDATA_WUNLOCK(ifp);
30400a46b31SKip Macy 	}
3051ed7bf1eSGleb Smirnoff 	if (la == NULL) {
3066e6b3f7cSQing Li 		if (flags & LLE_CREATE)
3071ed7bf1eSGleb Smirnoff 			log(LOG_DEBUG,
3081ed7bf1eSGleb Smirnoff 			    "arpresolve: can't allocate llinfo for %s\n",
3091ed7bf1eSGleb Smirnoff 			    inet_ntoa(SIN(dst)->sin_addr));
3101ed7bf1eSGleb Smirnoff 		m_freem(m);
3116e6b3f7cSQing Li 		return (EINVAL);
3121ed7bf1eSGleb Smirnoff 	}
313a20e2538SGleb Smirnoff 
3146e6b3f7cSQing Li 	if ((la->la_flags & LLE_VALID) &&
31593704ac5SQing Li 	    ((la->la_flags & LLE_STATIC) || la->la_expire > time_second)) {
3166e6b3f7cSQing Li 		bcopy(&la->ll_addr, desten, ifp->if_addrlen);
317f0f3379eSOrion Hodson 		/*
318f0f3379eSOrion Hodson 		 * If entry has an expiry time and it is approaching,
3196e6b3f7cSQing Li 		 * see if we need to send an ARP request within this
3206e6b3f7cSQing Li 		 * arpt_down interval.
321f0f3379eSOrion Hodson 		 */
3226e6b3f7cSQing Li 		if (!(la->la_flags & LLE_STATIC) &&
32393704ac5SQing Li 		    time_second + la->la_preempt > la->la_expire) {
3246e6b3f7cSQing Li 			arprequest(ifp, NULL,
3256e6b3f7cSQing Li 			    &SIN(dst)->sin_addr, IF_LLADDR(ifp));
326a20e2538SGleb Smirnoff 
327022695f8SOrion Hodson 			la->la_preempt--;
328f0f3379eSOrion Hodson 		}
329f0f3379eSOrion Hodson 
3306e6b3f7cSQing Li 		*lle = la;
3316e6b3f7cSQing Li 		error = 0;
3326e6b3f7cSQing Li 		goto done;
333df8bae1dSRodney W. Grimes 	}
3346e6b3f7cSQing Li 
3356e6b3f7cSQing Li 	if (la->la_flags & LLE_STATIC) {   /* should not happen! */
3366e6b3f7cSQing Li 		log(LOG_DEBUG, "arpresolve: ouch, empty static llinfo for %s\n",
3376e6b3f7cSQing Li 		    inet_ntoa(SIN(dst)->sin_addr));
33847891de1SRuslan Ermilov 		m_freem(m);
3396e6b3f7cSQing Li 		error = EINVAL;
3406e6b3f7cSQing Li 		goto done;
3416e6b3f7cSQing Li 	}
3426e6b3f7cSQing Li 
34393704ac5SQing Li 	renew = (la->la_asked == 0 || la->la_expire != time_second);
3446e6b3f7cSQing Li 	if ((renew || m != NULL) && (flags & LLE_EXCLUSIVE) == 0) {
3456e6b3f7cSQing Li 		flags |= LLE_EXCLUSIVE;
3466e6b3f7cSQing Li 		LLE_RUNLOCK(la);
3476e6b3f7cSQing Li 		goto retry;
34847891de1SRuslan Ermilov 	}
34908aadfbbSJonathan Lemon 	/*
350df8bae1dSRodney W. Grimes 	 * There is an arptab entry, but no ethernet address
351df8bae1dSRodney W. Grimes 	 * response yet.  Replace the held mbuf with this
352df8bae1dSRodney W. Grimes 	 * latest one.
353df8bae1dSRodney W. Grimes 	 */
3546e6b3f7cSQing Li 	if (m != NULL) {
35554fc657dSGeorge V. Neville-Neil 		if (la->la_hold != NULL) {
356df8bae1dSRodney W. Grimes 			m_freem(la->la_hold);
35754fc657dSGeorge V. Neville-Neil 			ARPSTAT_INC(dropped);
35854fc657dSGeorge V. Neville-Neil 		}
359df8bae1dSRodney W. Grimes 		la->la_hold = m;
3606e6b3f7cSQing Li 		if (renew == 0 && (flags & LLE_EXCLUSIVE)) {
3616e6b3f7cSQing Li 			flags &= ~LLE_EXCLUSIVE;
3626e6b3f7cSQing Li 			LLE_DOWNGRADE(la);
36358505389SKip Macy 		}
364e1ff74c5SGleb Smirnoff 
3656e6b3f7cSQing Li 	}
366e1ff74c5SGleb Smirnoff 	/*
367e1ff74c5SGleb Smirnoff 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
368e1ff74c5SGleb Smirnoff 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
369e1ff74c5SGleb Smirnoff 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
370e1ff74c5SGleb Smirnoff 	 * ARP request, but not faster than one request per second.
371e1ff74c5SGleb Smirnoff 	 */
372603724d3SBjoern A. Zeeb 	if (la->la_asked < V_arp_maxtries)
373e1ff74c5SGleb Smirnoff 		error = EWOULDBLOCK;	/* First request. */
374e1ff74c5SGleb Smirnoff 	else
3756e6b3f7cSQing Li 		error =
37693704ac5SQing Li 			(rt0->rt_flags & RTF_GATEWAY) ? EHOSTUNREACH : EHOSTDOWN;
377e1ff74c5SGleb Smirnoff 
3786e6b3f7cSQing Li 	if (renew) {
3796e6b3f7cSQing Li 		LLE_ADDREF(la);
38093704ac5SQing Li 		la->la_expire = time_second;
38193704ac5SQing Li 		callout_reset(&la->la_timer, hz * V_arpt_down, arptimer, la);
38295ebcabeSMaxim Konovalov 		la->la_asked++;
3836e6b3f7cSQing Li 		LLE_WUNLOCK(la);
3846e6b3f7cSQing Li 		arprequest(ifp, NULL, &SIN(dst)->sin_addr,
385322dcb8dSMax Khon 		    IF_LLADDR(ifp));
3866e6b3f7cSQing Li 		return (error);
3876e6b3f7cSQing Li 	}
3886e6b3f7cSQing Li done:
3896e6b3f7cSQing Li 	if (flags & LLE_EXCLUSIVE)
3906e6b3f7cSQing Li 		LLE_WUNLOCK(la);
3916e6b3f7cSQing Li 	else
3926e6b3f7cSQing Li 		LLE_RUNLOCK(la);
393e1ff74c5SGleb Smirnoff 	return (error);
394df8bae1dSRodney W. Grimes }
395df8bae1dSRodney W. Grimes 
396df8bae1dSRodney W. Grimes /*
397df8bae1dSRodney W. Grimes  * Common length and type checks are done here,
398df8bae1dSRodney W. Grimes  * then the protocol-specific routine is called.
399df8bae1dSRodney W. Grimes  */
400885f1aa4SPoul-Henning Kamp static void
4011cafed39SJonathan Lemon arpintr(struct mbuf *m)
402df8bae1dSRodney W. Grimes {
4031cafed39SJonathan Lemon 	struct arphdr *ar;
404df8bae1dSRodney W. Grimes 
40576ec7b2fSRobert Watson 	if (m->m_len < sizeof(struct arphdr) &&
40684365e2bSMatthew Dillon 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
407e44d6283SJoerg Wunsch 		log(LOG_ERR, "arp: runt packet -- m_pullup failed\n");
4081cafed39SJonathan Lemon 		return;
40976ec7b2fSRobert Watson 	}
41076ec7b2fSRobert Watson 	ar = mtod(m, struct arphdr *);
41176ec7b2fSRobert Watson 
4121cafed39SJonathan Lemon 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER &&
4131cafed39SJonathan Lemon 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE802 &&
414b8b33234SDoug Rabson 	    ntohs(ar->ar_hrd) != ARPHRD_ARCNET &&
415b8b33234SDoug Rabson 	    ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
4161cafed39SJonathan Lemon 		log(LOG_ERR, "arp: unknown hardware address format (0x%2D)\n",
41776ec7b2fSRobert Watson 		    (unsigned char *)&ar->ar_hrd, "");
41876ec7b2fSRobert Watson 		m_freem(m);
4191cafed39SJonathan Lemon 		return;
42076ec7b2fSRobert Watson 	}
42176ec7b2fSRobert Watson 
42231175791SRuslan Ermilov 	if (m->m_len < arphdr_len(ar)) {
42378e2d2bdSRuslan Ermilov 		if ((m = m_pullup(m, arphdr_len(ar))) == NULL) {
424f72d9d83SJoerg Wunsch 			log(LOG_ERR, "arp: runt packet\n");
42576ec7b2fSRobert Watson 			m_freem(m);
4261cafed39SJonathan Lemon 			return;
42776ec7b2fSRobert Watson 		}
42878e2d2bdSRuslan Ermilov 		ar = mtod(m, struct arphdr *);
42978e2d2bdSRuslan Ermilov 	}
430df8bae1dSRodney W. Grimes 
43154fc657dSGeorge V. Neville-Neil 	ARPSTAT_INC(received);
432df8bae1dSRodney W. Grimes 	switch (ntohs(ar->ar_pro)) {
4331d5e9e22SEivind Eklund #ifdef INET
434df8bae1dSRodney W. Grimes 	case ETHERTYPE_IP:
435df8bae1dSRodney W. Grimes 		in_arpinput(m);
4361cafed39SJonathan Lemon 		return;
4371d5e9e22SEivind Eklund #endif
438df8bae1dSRodney W. Grimes 	}
439df8bae1dSRodney W. Grimes 	m_freem(m);
440df8bae1dSRodney W. Grimes }
441df8bae1dSRodney W. Grimes 
4421d5e9e22SEivind Eklund #ifdef INET
443df8bae1dSRodney W. Grimes /*
444df8bae1dSRodney W. Grimes  * ARP for Internet protocols on 10 Mb/s Ethernet.
445df8bae1dSRodney W. Grimes  * Algorithm is that given in RFC 826.
446df8bae1dSRodney W. Grimes  * In addition, a sanity check is performed on the sender
447df8bae1dSRodney W. Grimes  * protocol address, to catch impersonators.
448df8bae1dSRodney W. Grimes  * We no longer handle negotiations for use of trailer protocol:
449df8bae1dSRodney W. Grimes  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
450df8bae1dSRodney W. Grimes  * along with IP replies if we wanted trailers sent to us,
451df8bae1dSRodney W. Grimes  * and also sent them in response to IP replies.
452df8bae1dSRodney W. Grimes  * This allowed either end to announce the desire to receive
453df8bae1dSRodney W. Grimes  * trailer packets.
454df8bae1dSRodney W. Grimes  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
455df8bae1dSRodney W. Grimes  * but formerly didn't normally send requests.
456df8bae1dSRodney W. Grimes  */
4573269187dSAlfred Perlstein static int log_arp_wrong_iface = 1;
458e3d123d6SAlfred Perlstein static int log_arp_movements = 1;
45939393906SGleb Smirnoff static int log_arp_permanent_modify = 1;
4603269187dSAlfred Perlstein 
4613269187dSAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
4623269187dSAlfred Perlstein 	&log_arp_wrong_iface, 0,
4633269187dSAlfred Perlstein 	"log arp packets arriving on the wrong interface");
464e3d123d6SAlfred Perlstein SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
465e3d123d6SAlfred Perlstein         &log_arp_movements, 0,
46675ce3221SAlfred Perlstein         "log arp replies from MACs different than the one in the cache");
46739393906SGleb Smirnoff SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
46839393906SGleb Smirnoff         &log_arp_permanent_modify, 0,
46939393906SGleb Smirnoff         "log arp replies from MACs different than the one in the permanent arp entry");
470e3d123d6SAlfred Perlstein 
4713269187dSAlfred Perlstein 
472df8bae1dSRodney W. Grimes static void
473f2565d68SRobert Watson in_arpinput(struct mbuf *m)
474df8bae1dSRodney W. Grimes {
475e952fa39SMatthew N. Dodd 	struct arphdr *ah;
476e952fa39SMatthew N. Dodd 	struct ifnet *ifp = m->m_pkthdr.rcvif;
4776e6b3f7cSQing Li 	struct llentry *la = NULL;
478e952fa39SMatthew N. Dodd 	struct rtentry *rt;
479ca925d9cSJonathan Lemon 	struct ifaddr *ifa;
480ca925d9cSJonathan Lemon 	struct in_ifaddr *ia;
481cc7e9d43SBjoern A. Zeeb 	struct mbuf *hold;
482df8bae1dSRodney W. Grimes 	struct sockaddr sa;
483df8bae1dSRodney W. Grimes 	struct in_addr isaddr, itaddr, myaddr;
484a9771948SGleb Smirnoff 	u_int8_t *enaddr = NULL;
4856e6b3f7cSQing Li 	int op, flags;
486322dcb8dSMax Khon 	int req_len;
48780b11ee4SPhilip Paeps 	int bridged = 0, is_bridge = 0;
488422a115aSGleb Smirnoff #ifdef DEV_CARP
4892ef4a436SGleb Smirnoff 	int carp_match = 0;
490422a115aSGleb Smirnoff #endif
4918e7e854cSKip Macy 	struct sockaddr_in sin;
4928e7e854cSKip Macy 	sin.sin_len = sizeof(struct sockaddr_in);
4938e7e854cSKip Macy 	sin.sin_family = AF_INET;
49429910a5aSKip Macy 	sin.sin_addr.s_addr = 0;
495df8bae1dSRodney W. Grimes 
49674948aa6SAndrew Thompson 	if (ifp->if_bridge)
4978f867517SAndrew Thompson 		bridged = 1;
49880b11ee4SPhilip Paeps 	if (ifp->if_type == IFT_BRIDGE)
49980b11ee4SPhilip Paeps 		is_bridge = 1;
5008f867517SAndrew Thompson 
501322dcb8dSMax Khon 	req_len = arphdr_len2(ifp->if_addrlen, sizeof(struct in_addr));
502322dcb8dSMax Khon 	if (m->m_len < req_len && (m = m_pullup(m, req_len)) == NULL) {
5034cbc8ad1SYaroslav Tykhiy 		log(LOG_ERR, "in_arp: runt packet -- m_pullup failed\n");
5044cbc8ad1SYaroslav Tykhiy 		return;
5054cbc8ad1SYaroslav Tykhiy 	}
5064cbc8ad1SYaroslav Tykhiy 
507322dcb8dSMax Khon 	ah = mtod(m, struct arphdr *);
508322dcb8dSMax Khon 	op = ntohs(ah->ar_op);
509322dcb8dSMax Khon 	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
510322dcb8dSMax Khon 	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
51132439868SGleb Smirnoff 
51254fc657dSGeorge V. Neville-Neil 	if (op == ARPOP_REPLY)
51354fc657dSGeorge V. Neville-Neil 		ARPSTAT_INC(rxreplies);
51454fc657dSGeorge V. Neville-Neil 
515ca925d9cSJonathan Lemon 	/*
516ca925d9cSJonathan Lemon 	 * For a bridge, we want to check the address irrespective
517ca925d9cSJonathan Lemon 	 * of the receive interface. (This will change slightly
518ca925d9cSJonathan Lemon 	 * when we have clusters of interfaces).
519a9771948SGleb Smirnoff 	 * If the interface does not match, but the recieving interface
520a9771948SGleb Smirnoff 	 * is part of carp, we call carp_iamatch to see if this is a
521a9771948SGleb Smirnoff 	 * request for the virtual host ip.
522a9771948SGleb Smirnoff 	 * XXX: This is really ugly!
523ca925d9cSJonathan Lemon 	 */
5242d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
5252ef4a436SGleb Smirnoff 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
5267f2d8767SAndrew Thompson 		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
5276e6b3f7cSQing Li 		    ia->ia_ifp == ifp) &&
52809d54778SRobert Watson 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
52909d54778SRobert Watson 			ifa_ref(&ia->ia_ifa);
5302d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
531ca925d9cSJonathan Lemon 			goto match;
53209d54778SRobert Watson 		}
5332ef4a436SGleb Smirnoff #ifdef DEV_CARP
5342ef4a436SGleb Smirnoff 		if (ifp->if_carp != NULL &&
5352ef4a436SGleb Smirnoff 		    carp_iamatch(ifp->if_carp, ia, &isaddr, &enaddr) &&
5362ef4a436SGleb Smirnoff 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
5372ef4a436SGleb Smirnoff 			carp_match = 1;
53809d54778SRobert Watson 			ifa_ref(&ia->ia_ifa);
5392d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
5402ef4a436SGleb Smirnoff 			goto match;
5412ef4a436SGleb Smirnoff 		}
5422ef4a436SGleb Smirnoff #endif
5432ef4a436SGleb Smirnoff 	}
544ca925d9cSJonathan Lemon 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
5457f2d8767SAndrew Thompson 		if (((bridged && ia->ia_ifp->if_bridge != NULL) ||
5466e6b3f7cSQing Li 		    ia->ia_ifp == ifp) &&
54709d54778SRobert Watson 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
54809d54778SRobert Watson 			ifa_ref(&ia->ia_ifa);
5492d9cfabaSRobert Watson 			IN_IFADDR_RUNLOCK();
550ca925d9cSJonathan Lemon 			goto match;
55109d54778SRobert Watson 		}
55280b11ee4SPhilip Paeps 
55380b11ee4SPhilip Paeps #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia)				\
55480b11ee4SPhilip Paeps   (ia->ia_ifp->if_bridge == ifp->if_softc &&				\
55580b11ee4SPhilip Paeps   !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) &&	\
55680b11ee4SPhilip Paeps   addr == ia->ia_addr.sin_addr.s_addr)
55780b11ee4SPhilip Paeps 	/*
55880b11ee4SPhilip Paeps 	 * Check the case when bridge shares its MAC address with
55980b11ee4SPhilip Paeps 	 * some of its children, so packets are claimed by bridge
56080b11ee4SPhilip Paeps 	 * itself (bridge_input() does it first), but they are really
56180b11ee4SPhilip Paeps 	 * meant to be destined to the bridge member.
56280b11ee4SPhilip Paeps 	 */
56380b11ee4SPhilip Paeps 	if (is_bridge) {
56480b11ee4SPhilip Paeps 		LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
56580b11ee4SPhilip Paeps 			if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
56609d54778SRobert Watson 				ifa_ref(&ia->ia_ifa);
56780b11ee4SPhilip Paeps 				ifp = ia->ia_ifp;
5682d9cfabaSRobert Watson 				IN_IFADDR_RUNLOCK();
56980b11ee4SPhilip Paeps 				goto match;
57080b11ee4SPhilip Paeps 			}
57180b11ee4SPhilip Paeps 		}
57280b11ee4SPhilip Paeps 	}
57380b11ee4SPhilip Paeps #undef BDG_MEMBER_MATCHES_ARP
5742d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
57580b11ee4SPhilip Paeps 
576ca925d9cSJonathan Lemon 	/*
577d8b84d9eSJonathan Lemon 	 * No match, use the first inet address on the receive interface
578ca925d9cSJonathan Lemon 	 * as a dummy address for the rest of the function.
579ca925d9cSJonathan Lemon 	 */
58009d54778SRobert Watson 	IF_ADDR_LOCK(ifp);
581d8b84d9eSJonathan Lemon 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
5824b97d7afSYaroslav Tykhiy 		if (ifa->ifa_addr->sa_family == AF_INET) {
583ec691a10SJonathan Lemon 			ia = ifatoia(ifa);
58409d54778SRobert Watson 			ifa_ref(ifa);
585f8574c7aSRobert Watson 			IF_ADDR_UNLOCK(ifp);
586ec691a10SJonathan Lemon 			goto match;
587ec691a10SJonathan Lemon 		}
58809d54778SRobert Watson 	IF_ADDR_UNLOCK(ifp);
58909d54778SRobert Watson 
590ec691a10SJonathan Lemon 	/*
591ec691a10SJonathan Lemon 	 * If bridging, fall back to using any inet address.
592ec691a10SJonathan Lemon 	 */
5932d9cfabaSRobert Watson 	IN_IFADDR_RLOCK();
5942d9cfabaSRobert Watson 	if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) {
5952d9cfabaSRobert Watson 		IN_IFADDR_RUNLOCK();
596b2a8ac7cSLuigi Rizzo 		goto drop;
5972d9cfabaSRobert Watson 	}
59809d54778SRobert Watson 	ifa_ref(&ia->ia_ifa);
5992d9cfabaSRobert Watson 	IN_IFADDR_RUNLOCK();
600ca925d9cSJonathan Lemon match:
601a9771948SGleb Smirnoff 	if (!enaddr)
602a9771948SGleb Smirnoff 		enaddr = (u_int8_t *)IF_LLADDR(ifp);
603ca925d9cSJonathan Lemon 	myaddr = ia->ia_addr.sin_addr;
60409d54778SRobert Watson 	ifa_free(&ia->ia_ifa);
605a9771948SGleb Smirnoff 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
606b2a8ac7cSLuigi Rizzo 		goto drop;	/* it's from me, ignore it. */
607322dcb8dSMax Khon 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
608df8bae1dSRodney W. Grimes 		log(LOG_ERR,
609322dcb8dSMax Khon 		    "arp: link address is broadcast for IP address %s!\n",
610ef0cdf33SGarrett Wollman 		    inet_ntoa(isaddr));
611b2a8ac7cSLuigi Rizzo 		goto drop;
612df8bae1dSRodney W. Grimes 	}
61300fcf9d1SRobert Watson 	/*
61400fcf9d1SRobert Watson 	 * Warn if another host is using the same IP address, but only if the
61500fcf9d1SRobert Watson 	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
61600fcf9d1SRobert Watson 	 * case we suppress the warning to avoid false positive complaints of
61700fcf9d1SRobert Watson 	 * potential misconfiguration.
61800fcf9d1SRobert Watson 	 */
619f69453caSAndrew Thompson 	if (!bridged && isaddr.s_addr == myaddr.s_addr && myaddr.s_addr != 0) {
620df8bae1dSRodney W. Grimes 		log(LOG_ERR,
6213affb6fbSYaroslav Tykhiy 		   "arp: %*D is using my IP address %s on %s!\n",
622322dcb8dSMax Khon 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
6233affb6fbSYaroslav Tykhiy 		   inet_ntoa(isaddr), ifp->if_xname);
624df8bae1dSRodney W. Grimes 		itaddr = myaddr;
62554fc657dSGeorge V. Neville-Neil 		ARPSTAT_INC(dupips);
626df8bae1dSRodney W. Grimes 		goto reply;
627df8bae1dSRodney W. Grimes 	}
628deb62e28SRuslan Ermilov 	if (ifp->if_flags & IFF_STATICARP)
629deb62e28SRuslan Ermilov 		goto reply;
6308b07e49aSJulian Elischer 
6316e6b3f7cSQing Li 	bzero(&sin, sizeof(sin));
6326e6b3f7cSQing Li 	sin.sin_len = sizeof(struct sockaddr_in);
6336e6b3f7cSQing Li 	sin.sin_family = AF_INET;
6346e6b3f7cSQing Li 	sin.sin_addr = isaddr;
6356e6b3f7cSQing Li 	flags = (itaddr.s_addr == myaddr.s_addr) ? LLE_CREATE : 0;
6366e6b3f7cSQing Li 	flags |= LLE_EXCLUSIVE;
6376e6b3f7cSQing Li 	IF_AFDATA_LOCK(ifp);
6386e6b3f7cSQing Li 	la = lla_lookup(LLTABLE(ifp), flags, (struct sockaddr *)&sin);
6396e6b3f7cSQing Li 	IF_AFDATA_UNLOCK(ifp);
6406e6b3f7cSQing Li 	if (la != NULL) {
6416e6b3f7cSQing Li 		/* the following is not an error when doing bridging */
6426e6b3f7cSQing Li 		if (!bridged && la->lle_tbl->llt_ifp != ifp
643422a115aSGleb Smirnoff #ifdef DEV_CARP
644422a115aSGleb Smirnoff 		    && (ifp->if_type != IFT_CARP || !carp_match)
645422a115aSGleb Smirnoff #endif
646422a115aSGleb Smirnoff 			) {
6473269187dSAlfred Perlstein 			if (log_arp_wrong_iface)
6488b07e49aSJulian Elischer 				log(LOG_ERR, "arp: %s is on %s "
6496e6b3f7cSQing Li 				    "but got reply from %*D on %s\n",
650dd9b6ddeSBill Fenner 				    inet_ntoa(isaddr),
6516e6b3f7cSQing Li 				    la->lle_tbl->llt_ifp->if_xname,
6526e6b3f7cSQing Li 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
6539bf40edeSBrooks Davis 				    ifp->if_xname);
654cc7e9d43SBjoern A. Zeeb 			LLE_WUNLOCK(la);
6556e6b3f7cSQing Li 			goto reply;
656dd9b6ddeSBill Fenner 		}
6576e6b3f7cSQing Li 		if ((la->la_flags & LLE_VALID) &&
6586e6b3f7cSQing Li 		    bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
6596e6b3f7cSQing Li 			if (la->la_flags & LLE_STATIC) {
660cc7e9d43SBjoern A. Zeeb 				LLE_WUNLOCK(la);
6618b07e49aSJulian Elischer 				log(LOG_ERR,
6626e6b3f7cSQing Li 				    "arp: %*D attempts to modify permanent "
6636e6b3f7cSQing Li 				    "entry for %s on %s\n",
6646e6b3f7cSQing Li 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
6656e6b3f7cSQing Li 				    inet_ntoa(isaddr), ifp->if_xname);
6666e6b3f7cSQing Li 				goto reply;
6676e6b3f7cSQing Li 			}
6686e6b3f7cSQing Li 			if (log_arp_movements) {
6696e6b3f7cSQing Li 			        log(LOG_INFO, "arp: %s moved from %*D "
6706e6b3f7cSQing Li 				    "to %*D on %s\n",
6718b07e49aSJulian Elischer 				    inet_ntoa(isaddr),
6726e6b3f7cSQing Li 				    ifp->if_addrlen,
6736e6b3f7cSQing Li 				    (u_char *)&la->ll_addr, ":",
6746e6b3f7cSQing Li 				    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
6758b07e49aSJulian Elischer 				    ifp->if_xname);
676dd9b6ddeSBill Fenner 			}
677dfd5dee1SPeter Wemm 		}
6786e6b3f7cSQing Li 
679322dcb8dSMax Khon 		if (ifp->if_addrlen != ah->ar_hln) {
680cc7e9d43SBjoern A. Zeeb 			LLE_WUNLOCK(la);
681322dcb8dSMax Khon 			log(LOG_WARNING,
6826e6b3f7cSQing Li 			    "arp from %*D: addr len: new %d, i/f %d (ignored)",
6836e6b3f7cSQing Li 			    ifp->if_addrlen, (u_char *) ar_sha(ah), ":",
6846e6b3f7cSQing Li 			    ah->ar_hln, ifp->if_addrlen);
6856e6b3f7cSQing Li 			goto reply;
686322dcb8dSMax Khon 		}
6876e6b3f7cSQing Li 		(void)memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
6886e6b3f7cSQing Li 		la->la_flags |= LLE_VALID;
6898b07e49aSJulian Elischer 
6909a311445SNavdeep Parhar 		EVENTHANDLER_INVOKE(arp_update_event, la);
6919a311445SNavdeep Parhar 
6926e6b3f7cSQing Li 		if (!(la->la_flags & LLE_STATIC)) {
69393704ac5SQing Li 			la->la_expire = time_second + V_arpt_keep;
694603724d3SBjoern A. Zeeb 			callout_reset(&la->la_timer, hz * V_arpt_keep,
6956e6b3f7cSQing Li 			    arptimer, la);
6961daaa65dSGleb Smirnoff 		}
697022695f8SOrion Hodson 		la->la_asked = 0;
698603724d3SBjoern A. Zeeb 		la->la_preempt = V_arp_maxtries;
699cc7e9d43SBjoern A. Zeeb 		hold = la->la_hold;
700cc7e9d43SBjoern A. Zeeb 		if (hold != NULL) {
701cc7e9d43SBjoern A. Zeeb 			la->la_hold = NULL;
7026e6b3f7cSQing Li 			memcpy(&sa, L3_ADDR(la), sizeof(sa));
7036e6b3f7cSQing Li 		}
704cc7e9d43SBjoern A. Zeeb 		LLE_WUNLOCK(la);
705cc7e9d43SBjoern A. Zeeb 		if (hold != NULL)
706cc7e9d43SBjoern A. Zeeb 			(*ifp->if_output)(ifp, hold, &sa, NULL);
7076e6b3f7cSQing Li 	}
7086e6b3f7cSQing Li reply:
709b2a8ac7cSLuigi Rizzo 	if (op != ARPOP_REQUEST)
710b2a8ac7cSLuigi Rizzo 		goto drop;
71154fc657dSGeorge V. Neville-Neil 	ARPSTAT_INC(rxrequests);
7126e6b3f7cSQing Li 
713df8bae1dSRodney W. Grimes 	if (itaddr.s_addr == myaddr.s_addr) {
7148b07e49aSJulian Elischer 		/* Shortcut.. the receiving interface is the target. */
715322dcb8dSMax Khon 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
716a9771948SGleb Smirnoff 		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
717df8bae1dSRodney W. Grimes 	} else {
718897d75c9SQing Li 		struct llentry *lle = NULL;
719897d75c9SQing Li 
720cd29a779SQing Li 		sin.sin_addr = itaddr;
721cd29a779SQing Li 		IF_AFDATA_LOCK(ifp);
722cd29a779SQing Li 		lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
723cd29a779SQing Li 		IF_AFDATA_UNLOCK(ifp);
724cd29a779SQing Li 
725cd29a779SQing Li 		if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
726cd29a779SQing Li 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
727cd29a779SQing Li 			(void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
728cd29a779SQing Li 			LLE_RUNLOCK(lle);
729cd29a779SQing Li 		} else {
730cd29a779SQing Li 
731cd29a779SQing Li 			if (lle != NULL)
732cd29a779SQing Li 				LLE_RUNLOCK(lle);
733cd29a779SQing Li 
734603724d3SBjoern A. Zeeb 			if (!V_arp_proxyall)
735b2a8ac7cSLuigi Rizzo 				goto drop;
73628e82295SGarrett Wollman 
73728e82295SGarrett Wollman 			sin.sin_addr = itaddr;
7388b07e49aSJulian Elischer 			/* XXX MRT use table 0 for arp reply  */
7398b07e49aSJulian Elischer 			rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
740b2a8ac7cSLuigi Rizzo 			if (!rt)
741b2a8ac7cSLuigi Rizzo 				goto drop;
742897d75c9SQing Li 
74328e82295SGarrett Wollman 			/*
74428e82295SGarrett Wollman 			 * Don't send proxies for nodes on the same interface
74528e82295SGarrett Wollman 			 * as this one came out of, or we'll get into a fight
74628e82295SGarrett Wollman 			 * over who claims what Ether address.
74728e82295SGarrett Wollman 			 */
748897d75c9SQing Li 			if (!rt->rt_ifp || rt->rt_ifp == ifp) {
7494e57bc33SChristian S.J. Peron 				RTFREE_LOCKED(rt);
750b2a8ac7cSLuigi Rizzo 				goto drop;
75128e82295SGarrett Wollman 			}
7524e57bc33SChristian S.J. Peron 			RTFREE_LOCKED(rt);
753cc728227SDavid Malone 
754897d75c9SQing Li 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
755cd29a779SQing Li 			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
756897d75c9SQing Li 
757cc728227SDavid Malone 			/*
758cc728227SDavid Malone 			 * Also check that the node which sent the ARP packet
759cc728227SDavid Malone 			 * is on the the interface we expect it to be on. This
760cc728227SDavid Malone 			 * avoids ARP chaos if an interface is connected to the
761cc728227SDavid Malone 			 * wrong network.
762cc728227SDavid Malone 			 */
763cc728227SDavid Malone 			sin.sin_addr = isaddr;
764cc728227SDavid Malone 
7658b07e49aSJulian Elischer 			/* XXX MRT use table 0 for arp checks */
7668b07e49aSJulian Elischer 			rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
767b2a8ac7cSLuigi Rizzo 			if (!rt)
768b2a8ac7cSLuigi Rizzo 				goto drop;
769322dcb8dSMax Khon 			if (rt->rt_ifp != ifp) {
770cc728227SDavid Malone 				log(LOG_INFO, "arp_proxy: ignoring request"
7719bf40edeSBrooks Davis 				    " from %s via %s, expecting %s\n",
7729bf40edeSBrooks Davis 				    inet_ntoa(isaddr), ifp->if_xname,
7739bf40edeSBrooks Davis 				    rt->rt_ifp->if_xname);
7744e57bc33SChristian S.J. Peron 				RTFREE_LOCKED(rt);
775b2a8ac7cSLuigi Rizzo 				goto drop;
776cc728227SDavid Malone 			}
7774e57bc33SChristian S.J. Peron 			RTFREE_LOCKED(rt);
778cc728227SDavid Malone 
779ac234f93SGarrett Wollman #ifdef DEBUG_PROXY
780ac234f93SGarrett Wollman 			printf("arp: proxying for %s\n",
781ef0cdf33SGarrett Wollman 			       inet_ntoa(itaddr));
782ac234f93SGarrett Wollman #endif
783df8bae1dSRodney W. Grimes 		}
784cd29a779SQing Li 	}
785df8bae1dSRodney W. Grimes 
786d0558157SBruce M Simpson 	if (itaddr.s_addr == myaddr.s_addr &&
787d0558157SBruce M Simpson 	    IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
788d0558157SBruce M Simpson 		/* RFC 3927 link-local IPv4; always reply by broadcast. */
789d0558157SBruce M Simpson #ifdef DEBUG_LINKLOCAL
790d0558157SBruce M Simpson 		printf("arp: sending reply for link-local addr %s\n",
791d0558157SBruce M Simpson 		    inet_ntoa(itaddr));
792d0558157SBruce M Simpson #endif
793d0558157SBruce M Simpson 		m->m_flags |= M_BCAST;
794d0558157SBruce M Simpson 		m->m_flags &= ~M_MCAST;
795d0558157SBruce M Simpson 	} else {
796d0558157SBruce M Simpson 		/* default behaviour; never reply by broadcast. */
797d0558157SBruce M Simpson 		m->m_flags &= ~(M_BCAST|M_MCAST);
798d0558157SBruce M Simpson 	}
799322dcb8dSMax Khon 	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
800322dcb8dSMax Khon 	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
801322dcb8dSMax Khon 	ah->ar_op = htons(ARPOP_REPLY);
802322dcb8dSMax Khon 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
80364bf80ceSMatthew N. Dodd 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
80464bf80ceSMatthew N. Dodd 	m->m_pkthdr.len = m->m_len;
80564bf80ceSMatthew N. Dodd 	sa.sa_family = AF_ARP;
80664bf80ceSMatthew N. Dodd 	sa.sa_len = 2;
807279aa3d4SKip Macy 	(*ifp->if_output)(ifp, m, &sa, NULL);
80854fc657dSGeorge V. Neville-Neil 	ARPSTAT_INC(txreplies);
809df8bae1dSRodney W. Grimes 	return;
810b2a8ac7cSLuigi Rizzo 
811b2a8ac7cSLuigi Rizzo drop:
812b2a8ac7cSLuigi Rizzo 	m_freem(m);
813df8bae1dSRodney W. Grimes }
8141d5e9e22SEivind Eklund #endif
815df8bae1dSRodney W. Grimes 
816dd2e4102SGarrett Wollman void
817f2565d68SRobert Watson arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
818dd2e4102SGarrett Wollman {
8196e6b3f7cSQing Li 	struct llentry *lle;
8206e6b3f7cSQing Li 
821ce9122fdSQing Li 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY) {
822322dcb8dSMax Khon 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
823322dcb8dSMax Khon 				&IA_SIN(ifa)->sin_addr, IF_LLADDR(ifp));
8246e6b3f7cSQing Li 		/*
8256e6b3f7cSQing Li 		 * interface address is considered static entry
8266e6b3f7cSQing Li 		 * because the output of the arp utility shows
8276e6b3f7cSQing Li 		 * that L2 entry as permanent
8286e6b3f7cSQing Li 		 */
8296e6b3f7cSQing Li 		IF_AFDATA_LOCK(ifp);
8306e6b3f7cSQing Li 		lle = lla_lookup(LLTABLE(ifp), (LLE_CREATE | LLE_IFADDR | LLE_STATIC),
8316e6b3f7cSQing Li 				 (struct sockaddr *)IA_SIN(ifa));
8326e6b3f7cSQing Li 		IF_AFDATA_UNLOCK(ifp);
8336e6b3f7cSQing Li 		if (lle == NULL)
8346e6b3f7cSQing Li 			log(LOG_INFO, "arp_ifinit: cannot create arp "
8356e6b3f7cSQing Li 			    "entry for interface address\n");
83686cd829dSKip Macy 		else
8376e6b3f7cSQing Li 			LLE_RUNLOCK(lle);
838ce9122fdSQing Li 	}
8396e6b3f7cSQing Li 	ifa->ifa_rtrequest = NULL;
840dd2e4102SGarrett Wollman }
841df5e1987SJonathan Lemon 
842a9771948SGleb Smirnoff void
843f2565d68SRobert Watson arp_ifinit2(struct ifnet *ifp, struct ifaddr *ifa, u_char *enaddr)
844a9771948SGleb Smirnoff {
845a9771948SGleb Smirnoff 	if (ntohl(IA_SIN(ifa)->sin_addr.s_addr) != INADDR_ANY)
846a9771948SGleb Smirnoff 		arprequest(ifp, &IA_SIN(ifa)->sin_addr,
847a9771948SGleb Smirnoff 				&IA_SIN(ifa)->sin_addr, enaddr);
8486e6b3f7cSQing Li 	ifa->ifa_rtrequest = NULL;
849a9771948SGleb Smirnoff }
850a9771948SGleb Smirnoff 
8511ed81b73SMarko Zec static void
8521ed81b73SMarko Zec arp_init(void)
8531ed81b73SMarko Zec {
8541ed81b73SMarko Zec 
855d4b5cae4SRobert Watson 	netisr_register(&arp_nh);
856df5e1987SJonathan Lemon }
857df5e1987SJonathan Lemon SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
858