xref: /freebsd/sys/netinet/if_ether.c (revision 4d293dd8dcde59fc9842a0ce1125fef8fcf83a8c)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
30  */
31 
32 /*
33  * Ethernet address resolution protocol.
34  * TODO:
35  *	add "inuse/lock" bit (or ref. count) along with valid bit
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include "opt_inet.h"
42 
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/queue.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49 #include <sys/mbuf.h>
50 #include <sys/malloc.h>
51 #include <sys/proc.h>
52 #include <sys/rmlock.h>
53 #include <sys/socket.h>
54 #include <sys/syslog.h>
55 
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/netisr.h>
61 #include <net/ethernet.h>
62 #include <net/route.h>
63 #include <net/vnet.h>
64 
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <net/if_llatbl.h>
68 #include <netinet/if_ether.h>
69 #ifdef INET
70 #include <netinet/ip_carp.h>
71 #endif
72 
73 #include <security/mac/mac_framework.h>
74 
75 #define SIN(s) ((const struct sockaddr_in *)(s))
76 
77 static struct timeval arp_lastlog;
78 static int arp_curpps;
79 static int arp_maxpps = 1;
80 
81 /* Simple ARP state machine */
82 enum arp_llinfo_state {
83 	ARP_LLINFO_INCOMPLETE = 0, /* No LLE data */
84 	ARP_LLINFO_REACHABLE,	/* LLE is valid */
85 	ARP_LLINFO_VERIFY,	/* LLE is valid, need refresh */
86 	ARP_LLINFO_DELETED,	/* LLE is deleted */
87 };
88 
89 SYSCTL_DECL(_net_link_ether);
90 static SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
91 static SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, "");
92 
93 /* timer values */
94 static VNET_DEFINE(int, arpt_keep) = (20*60);	/* once resolved, good for 20
95 						 * minutes */
96 static VNET_DEFINE(int, arp_maxtries) = 5;
97 static VNET_DEFINE(int, arp_proxyall) = 0;
98 static VNET_DEFINE(int, arpt_down) = 20;	/* keep incomplete entries for
99 						 * 20 seconds */
100 static VNET_DEFINE(int, arpt_rexmit) = 1;	/* retransmit arp entries, sec*/
101 VNET_PCPUSTAT_DEFINE(struct arpstat, arpstat);  /* ARP statistics, see if_arp.h */
102 VNET_PCPUSTAT_SYSINIT(arpstat);
103 
104 #ifdef VIMAGE
105 VNET_PCPUSTAT_SYSUNINIT(arpstat);
106 #endif /* VIMAGE */
107 
108 static VNET_DEFINE(int, arp_maxhold) = 1;
109 
110 #define	V_arpt_keep		VNET(arpt_keep)
111 #define	V_arpt_down		VNET(arpt_down)
112 #define	V_arpt_rexmit		VNET(arpt_rexmit)
113 #define	V_arp_maxtries		VNET(arp_maxtries)
114 #define	V_arp_proxyall		VNET(arp_proxyall)
115 #define	V_arp_maxhold		VNET(arp_maxhold)
116 
117 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_VNET | CTLFLAG_RW,
118 	&VNET_NAME(arpt_keep), 0,
119 	"ARP entry lifetime in seconds");
120 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_VNET | CTLFLAG_RW,
121 	&VNET_NAME(arp_maxtries), 0,
122 	"ARP resolution attempts before returning error");
123 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_VNET | CTLFLAG_RW,
124 	&VNET_NAME(arp_proxyall), 0,
125 	"Enable proxy ARP for all suitable requests");
126 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, wait, CTLFLAG_VNET | CTLFLAG_RW,
127 	&VNET_NAME(arpt_down), 0,
128 	"Incomplete ARP entry lifetime in seconds");
129 SYSCTL_VNET_PCPUSTAT(_net_link_ether_arp, OID_AUTO, stats, struct arpstat,
130     arpstat, "ARP statistics (struct arpstat, net/if_arp.h)");
131 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxhold, CTLFLAG_VNET | CTLFLAG_RW,
132 	&VNET_NAME(arp_maxhold), 0,
133 	"Number of packets to hold per ARP entry");
134 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_log_per_second,
135 	CTLFLAG_RW, &arp_maxpps, 0,
136 	"Maximum number of remotely triggered ARP messages that can be "
137 	"logged per second");
138 
139 #define	ARP_LOG(pri, ...)	do {					\
140 	if (ppsratecheck(&arp_lastlog, &arp_curpps, arp_maxpps))	\
141 		log((pri), "arp: " __VA_ARGS__);			\
142 } while (0)
143 
144 
145 static void	arp_init(void);
146 static void	arpintr(struct mbuf *);
147 static void	arptimer(void *);
148 #ifdef INET
149 static void	in_arpinput(struct mbuf *);
150 #endif
151 
152 static void arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr,
153     struct ifnet *ifp, int bridged, struct llentry *la);
154 static void arp_mark_lle_reachable(struct llentry *la);
155 static void arp_iflladdr(void *arg __unused, struct ifnet *ifp);
156 
157 static eventhandler_tag iflladdr_tag;
158 
159 static const struct netisr_handler arp_nh = {
160 	.nh_name = "arp",
161 	.nh_handler = arpintr,
162 	.nh_proto = NETISR_ARP,
163 	.nh_policy = NETISR_POLICY_SOURCE,
164 };
165 
166 /*
167  * Timeout routine.  Age arp_tab entries periodically.
168  */
169 static void
170 arptimer(void *arg)
171 {
172 	struct llentry *lle = (struct llentry *)arg;
173 	struct ifnet *ifp;
174 	int r_skip_req;
175 
176 	if (lle->la_flags & LLE_STATIC) {
177 		return;
178 	}
179 	LLE_WLOCK(lle);
180 	if (callout_pending(&lle->lle_timer)) {
181 		/*
182 		 * Here we are a bit odd here in the treatment of
183 		 * active/pending. If the pending bit is set, it got
184 		 * rescheduled before I ran. The active
185 		 * bit we ignore, since if it was stopped
186 		 * in ll_tablefree() and was currently running
187 		 * it would have return 0 so the code would
188 		 * not have deleted it since the callout could
189 		 * not be stopped so we want to go through
190 		 * with the delete here now. If the callout
191 		 * was restarted, the pending bit will be back on and
192 		 * we just want to bail since the callout_reset would
193 		 * return 1 and our reference would have been removed
194 		 * by arpresolve() below.
195 		 */
196 		LLE_WUNLOCK(lle);
197  		return;
198  	}
199 	ifp = lle->lle_tbl->llt_ifp;
200 	CURVNET_SET(ifp->if_vnet);
201 
202 	switch (lle->ln_state) {
203 	case ARP_LLINFO_REACHABLE:
204 
205 		/*
206 		 * Expiration time is approaching.
207 		 * Let's try to refresh entry if it is still
208 		 * in use.
209 		 *
210 		 * Set r_skip_req to get feedback from
211 		 * fast path. Change state and re-schedule
212 		 * ourselves.
213 		 */
214 		LLE_REQ_LOCK(lle);
215 		lle->r_skip_req = 1;
216 		LLE_REQ_UNLOCK(lle);
217 		lle->ln_state = ARP_LLINFO_VERIFY;
218 		callout_schedule(&lle->lle_timer, hz * V_arpt_rexmit);
219 		LLE_WUNLOCK(lle);
220 		CURVNET_RESTORE();
221 		return;
222 	case ARP_LLINFO_VERIFY:
223 		LLE_REQ_LOCK(lle);
224 		r_skip_req = lle->r_skip_req;
225 		LLE_REQ_UNLOCK(lle);
226 
227 		if (r_skip_req == 0 && lle->la_preempt > 0) {
228 			/* Entry was used, issue refresh request */
229 			struct in_addr dst;
230 			dst = lle->r_l3addr.addr4;
231 			lle->la_preempt--;
232 			callout_schedule(&lle->lle_timer, hz * V_arpt_rexmit);
233 			LLE_WUNLOCK(lle);
234 			arprequest(ifp, NULL, &dst, NULL);
235 			CURVNET_RESTORE();
236 			return;
237 		}
238 		/* Nothing happened. Reschedule if not too late */
239 		if (lle->la_expire > time_uptime) {
240 			callout_schedule(&lle->lle_timer, hz * V_arpt_rexmit);
241 			LLE_WUNLOCK(lle);
242 			CURVNET_RESTORE();
243 			return;
244 		}
245 		break;
246 	case ARP_LLINFO_INCOMPLETE:
247 	case ARP_LLINFO_DELETED:
248 		break;
249 	}
250 
251 	if ((lle->la_flags & LLE_DELETED) == 0) {
252 		int evt;
253 
254 		if (lle->la_flags & LLE_VALID)
255 			evt = LLENTRY_EXPIRED;
256 		else
257 			evt = LLENTRY_TIMEDOUT;
258 		EVENTHANDLER_INVOKE(lle_event, lle, evt);
259 	}
260 
261 	callout_stop(&lle->lle_timer);
262 
263 	/* XXX: LOR avoidance. We still have ref on lle. */
264 	LLE_WUNLOCK(lle);
265 	IF_AFDATA_LOCK(ifp);
266 	LLE_WLOCK(lle);
267 
268 	/* Guard against race with other llentry_free(). */
269 	if (lle->la_flags & LLE_LINKED) {
270 		LLE_REMREF(lle);
271 		lltable_unlink_entry(lle->lle_tbl, lle);
272 	}
273 	IF_AFDATA_UNLOCK(ifp);
274 
275 	size_t pkts_dropped = llentry_free(lle);
276 
277 	ARPSTAT_ADD(dropped, pkts_dropped);
278 	ARPSTAT_INC(timeouts);
279 
280 	CURVNET_RESTORE();
281 }
282 
283 /*
284  * Broadcast an ARP request. Caller specifies:
285  *	- arp header source ip address
286  *	- arp header target ip address
287  *	- arp header source ethernet address
288  */
289 void
290 arprequest(struct ifnet *ifp, const struct in_addr *sip,
291     const struct in_addr *tip, u_char *enaddr)
292 {
293 	struct mbuf *m;
294 	struct arphdr *ah;
295 	struct sockaddr sa;
296 	u_char *carpaddr = NULL;
297 
298 	if (sip == NULL) {
299 		/*
300 		 * The caller did not supply a source address, try to find
301 		 * a compatible one among those assigned to this interface.
302 		 */
303 		struct ifaddr *ifa;
304 
305 		IF_ADDR_RLOCK(ifp);
306 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
307 			if (ifa->ifa_addr->sa_family != AF_INET)
308 				continue;
309 
310 			if (ifa->ifa_carp) {
311 				if ((*carp_iamatch_p)(ifa, &carpaddr) == 0)
312 					continue;
313 				sip = &IA_SIN(ifa)->sin_addr;
314 			} else {
315 				carpaddr = NULL;
316 				sip = &IA_SIN(ifa)->sin_addr;
317 			}
318 
319 			if (0 == ((sip->s_addr ^ tip->s_addr) &
320 			    IA_MASKSIN(ifa)->sin_addr.s_addr))
321 				break;  /* found it. */
322 		}
323 		IF_ADDR_RUNLOCK(ifp);
324 		if (sip == NULL) {
325 			printf("%s: cannot find matching address\n", __func__);
326 			return;
327 		}
328 	}
329 	if (enaddr == NULL)
330 		enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp);
331 
332 	if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
333 		return;
334 	m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
335 		2 * ifp->if_addrlen;
336 	m->m_pkthdr.len = m->m_len;
337 	M_ALIGN(m, m->m_len);
338 	ah = mtod(m, struct arphdr *);
339 	bzero((caddr_t)ah, m->m_len);
340 #ifdef MAC
341 	mac_netinet_arp_send(ifp, m);
342 #endif
343 	ah->ar_pro = htons(ETHERTYPE_IP);
344 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
345 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
346 	ah->ar_op = htons(ARPOP_REQUEST);
347 	bcopy(enaddr, ar_sha(ah), ah->ar_hln);
348 	bcopy(sip, ar_spa(ah), ah->ar_pln);
349 	bcopy(tip, ar_tpa(ah), ah->ar_pln);
350 	sa.sa_family = AF_ARP;
351 	sa.sa_len = 2;
352 	m->m_flags |= M_BCAST;
353 	m_clrprotoflags(m);	/* Avoid confusing lower layers. */
354 	(*ifp->if_output)(ifp, m, &sa, NULL);
355 	ARPSTAT_INC(txrequests);
356 }
357 
358 /*
359  * Resolve an IP address into an ethernet address - heavy version.
360  * Used internally by arpresolve().
361  * We have already checked than  we can't use existing lle without
362  * modification so we have to acquire LLE_EXCLUSIVE lle lock.
363  *
364  * On success, desten and flags are filled in and the function returns 0;
365  * If the packet must be held pending resolution, we return EWOULDBLOCK
366  * On other errors, we return the corresponding error code.
367  * Note that m_freem() handles NULL.
368  */
369 static int
370 arpresolve_full(struct ifnet *ifp, int is_gw, int create, struct mbuf *m,
371 	const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
372 {
373 	struct llentry *la = NULL, *la_tmp;
374 	struct mbuf *curr = NULL;
375 	struct mbuf *next = NULL;
376 	int error, renew;
377 
378 	if (pflags != NULL)
379 		*pflags = 0;
380 
381 	if (create == 0) {
382 		IF_AFDATA_RLOCK(ifp);
383 		la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
384 		IF_AFDATA_RUNLOCK(ifp);
385 	}
386 	if (la == NULL && (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) {
387 		la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
388 		if (la == NULL) {
389 			log(LOG_DEBUG,
390 			    "arpresolve: can't allocate llinfo for %s on %s\n",
391 			    inet_ntoa(SIN(dst)->sin_addr), if_name(ifp));
392 			m_freem(m);
393 			return (EINVAL);
394 		}
395 
396 		IF_AFDATA_WLOCK(ifp);
397 		LLE_WLOCK(la);
398 		la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
399 		/* Prefer ANY existing lle over newly-created one */
400 		if (la_tmp == NULL)
401 			lltable_link_entry(LLTABLE(ifp), la);
402 		IF_AFDATA_WUNLOCK(ifp);
403 		if (la_tmp != NULL) {
404 			lltable_free_entry(LLTABLE(ifp), la);
405 			la = la_tmp;
406 		}
407 	}
408 	if (la == NULL) {
409 		m_freem(m);
410 		return (EINVAL);
411 	}
412 
413 	if ((la->la_flags & LLE_VALID) &&
414 	    ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
415 		bcopy(&la->ll_addr, desten, ifp->if_addrlen);
416 
417 		/* Check if we have feedback request from arptimer() */
418 		if (la->r_skip_req != 0) {
419 			LLE_REQ_LOCK(la);
420 			la->r_skip_req = 0; /* Notify that entry was used */
421 			LLE_REQ_UNLOCK(la);
422 		}
423 		if (pflags != NULL)
424 			*pflags = la->la_flags & (LLE_VALID|LLE_IFADDR);
425 		LLE_WUNLOCK(la);
426 		return (0);
427 	}
428 
429 	renew = (la->la_asked == 0 || la->la_expire != time_uptime);
430 	/*
431 	 * There is an arptab entry, but no ethernet address
432 	 * response yet.  Add the mbuf to the list, dropping
433 	 * the oldest packet if we have exceeded the system
434 	 * setting.
435 	 */
436 	if (m != NULL) {
437 		if (la->la_numheld >= V_arp_maxhold) {
438 			if (la->la_hold != NULL) {
439 				next = la->la_hold->m_nextpkt;
440 				m_freem(la->la_hold);
441 				la->la_hold = next;
442 				la->la_numheld--;
443 				ARPSTAT_INC(dropped);
444 			}
445 		}
446 		if (la->la_hold != NULL) {
447 			curr = la->la_hold;
448 			while (curr->m_nextpkt != NULL)
449 				curr = curr->m_nextpkt;
450 			curr->m_nextpkt = m;
451 		} else
452 			la->la_hold = m;
453 		la->la_numheld++;
454 	}
455 	/*
456 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
457 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
458 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
459 	 * ARP request, but not faster than one request per second.
460 	 */
461 	if (la->la_asked < V_arp_maxtries)
462 		error = EWOULDBLOCK;	/* First request. */
463 	else
464 		error = is_gw != 0 ? EHOSTUNREACH : EHOSTDOWN;
465 
466 	if (renew) {
467 		int canceled;
468 
469 		LLE_ADDREF(la);
470 		la->la_expire = time_uptime;
471 		canceled = callout_reset(&la->lle_timer, hz * V_arpt_down,
472 		    arptimer, la);
473 		if (canceled)
474 			LLE_REMREF(la);
475 		la->la_asked++;
476 		LLE_WUNLOCK(la);
477 		arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
478 		return (error);
479 	}
480 
481 	LLE_WUNLOCK(la);
482 	return (error);
483 }
484 
485 /*
486  * Resolve an IP address into an ethernet address.
487  * On input:
488  *    ifp is the interface we use
489  *    is_gw != 0 if @dst represents gateway to some destination
490  *    m is the mbuf. May be NULL if we don't have a packet.
491  *    dst is the next hop,
492  *    desten is the storage to put LL address.
493  *    flags returns subset of lle flags: LLE_VALID | LLE_IFADDR
494  *
495  * On success, desten and flags are filled in and the function returns 0;
496  * If the packet must be held pending resolution, we return EWOULDBLOCK
497  * On other errors, we return the corresponding error code.
498  * Note that m_freem() handles NULL.
499  */
500 int
501 arpresolve(struct ifnet *ifp, int is_gw, struct mbuf *m,
502 	const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
503 {
504 	struct llentry *la = 0;
505 
506 	if (pflags != NULL)
507 		*pflags = 0;
508 
509 	if (m != NULL) {
510 		if (m->m_flags & M_BCAST) {
511 			/* broadcast */
512 			(void)memcpy(desten,
513 			    ifp->if_broadcastaddr, ifp->if_addrlen);
514 			return (0);
515 		}
516 		if (m->m_flags & M_MCAST) {
517 			/* multicast */
518 			ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
519 			return (0);
520 		}
521 	}
522 
523 	IF_AFDATA_RLOCK(ifp);
524 	la = lla_lookup(LLTABLE(ifp), LLE_UNLOCKED, dst);
525 	if (la != NULL && (la->r_flags & RLLE_VALID) != 0) {
526 		/* Entry found, let's copy lle info */
527 		bcopy(&la->ll_addr, desten, ifp->if_addrlen);
528 		if (pflags != NULL)
529 			*pflags = LLE_VALID | (la->r_flags & RLLE_IFADDR);
530 		/* Check if we have feedback request from arptimer() */
531 		if (la->r_skip_req != 0) {
532 			LLE_REQ_LOCK(la);
533 			la->r_skip_req = 0; /* Notify that entry was used */
534 			LLE_REQ_UNLOCK(la);
535 		}
536 		IF_AFDATA_RUNLOCK(ifp);
537 		return (0);
538 	}
539 	IF_AFDATA_RUNLOCK(ifp);
540 
541 	return (arpresolve_full(ifp, is_gw, 1, m, dst, desten, pflags));
542 }
543 
544 /*
545  * Common length and type checks are done here,
546  * then the protocol-specific routine is called.
547  */
548 static void
549 arpintr(struct mbuf *m)
550 {
551 	struct arphdr *ar;
552 	struct ifnet *ifp;
553 	char *layer;
554 	int hlen;
555 
556 	ifp = m->m_pkthdr.rcvif;
557 
558 	if (m->m_len < sizeof(struct arphdr) &&
559 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
560 		ARP_LOG(LOG_NOTICE, "packet with short header received on %s\n",
561 		    if_name(ifp));
562 		return;
563 	}
564 	ar = mtod(m, struct arphdr *);
565 
566 	/* Check if length is sufficient */
567 	if (m->m_len <  arphdr_len(ar)) {
568 		m = m_pullup(m, arphdr_len(ar));
569 		if (m == NULL) {
570 			ARP_LOG(LOG_NOTICE, "short packet received on %s\n",
571 			    if_name(ifp));
572 			return;
573 		}
574 		ar = mtod(m, struct arphdr *);
575 	}
576 
577 	hlen = 0;
578 	layer = "";
579 	switch (ntohs(ar->ar_hrd)) {
580 	case ARPHRD_ETHER:
581 		hlen = ETHER_ADDR_LEN; /* RFC 826 */
582 		layer = "ethernet";
583 		break;
584 	case ARPHRD_IEEE802:
585 		hlen = 6; /* RFC 1390, FDDI_ADDR_LEN */
586 		layer = "fddi";
587 		break;
588 	case ARPHRD_ARCNET:
589 		hlen = 1; /* RFC 1201, ARC_ADDR_LEN */
590 		layer = "arcnet";
591 		break;
592 	case ARPHRD_INFINIBAND:
593 		hlen = 20;	/* RFC 4391, INFINIBAND_ALEN */
594 		layer = "infiniband";
595 		break;
596 	case ARPHRD_IEEE1394:
597 		hlen = 0; /* SHALL be 16 */ /* RFC 2734 */
598 		layer = "firewire";
599 
600 		/*
601 		 * Restrict too long harware addresses.
602 		 * Currently we are capable of handling 20-byte
603 		 * addresses ( sizeof(lle->ll_addr) )
604 		 */
605 		if (ar->ar_hln >= 20)
606 			hlen = 16;
607 		break;
608 	default:
609 		ARP_LOG(LOG_NOTICE,
610 		    "packet with unknown harware format 0x%02d received on %s\n",
611 		    ntohs(ar->ar_hrd), if_name(ifp));
612 		m_freem(m);
613 		return;
614 	}
615 
616 	if (hlen != 0 && hlen != ar->ar_hln) {
617 		ARP_LOG(LOG_NOTICE,
618 		    "packet with invalid %s address length %d received on %s\n",
619 		    layer, ar->ar_hln, if_name(ifp));
620 		m_freem(m);
621 		return;
622 	}
623 
624 	ARPSTAT_INC(received);
625 	switch (ntohs(ar->ar_pro)) {
626 #ifdef INET
627 	case ETHERTYPE_IP:
628 		in_arpinput(m);
629 		return;
630 #endif
631 	}
632 	m_freem(m);
633 }
634 
635 #ifdef INET
636 /*
637  * ARP for Internet protocols on 10 Mb/s Ethernet.
638  * Algorithm is that given in RFC 826.
639  * In addition, a sanity check is performed on the sender
640  * protocol address, to catch impersonators.
641  * We no longer handle negotiations for use of trailer protocol:
642  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
643  * along with IP replies if we wanted trailers sent to us,
644  * and also sent them in response to IP replies.
645  * This allowed either end to announce the desire to receive
646  * trailer packets.
647  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
648  * but formerly didn't normally send requests.
649  */
650 static int log_arp_wrong_iface = 1;
651 static int log_arp_movements = 1;
652 static int log_arp_permanent_modify = 1;
653 static int allow_multicast = 0;
654 
655 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
656 	&log_arp_wrong_iface, 0,
657 	"log arp packets arriving on the wrong interface");
658 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
659 	&log_arp_movements, 0,
660 	"log arp replies from MACs different than the one in the cache");
661 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
662 	&log_arp_permanent_modify, 0,
663 	"log arp replies from MACs different than the one in the permanent arp entry");
664 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW,
665 	&allow_multicast, 0, "accept multicast addresses");
666 
667 static void
668 in_arpinput(struct mbuf *m)
669 {
670 	struct rm_priotracker in_ifa_tracker;
671 	struct arphdr *ah;
672 	struct ifnet *ifp = m->m_pkthdr.rcvif;
673 	struct llentry *la = NULL, *la_tmp;
674 	struct rtentry *rt;
675 	struct ifaddr *ifa;
676 	struct in_ifaddr *ia;
677 	struct sockaddr sa;
678 	struct in_addr isaddr, itaddr, myaddr;
679 	u_int8_t *enaddr = NULL;
680 	int op;
681 	int bridged = 0, is_bridge = 0;
682 	int carped;
683 	struct sockaddr_in sin;
684 	struct sockaddr *dst;
685 	sin.sin_len = sizeof(struct sockaddr_in);
686 	sin.sin_family = AF_INET;
687 	sin.sin_addr.s_addr = 0;
688 
689 	if (ifp->if_bridge)
690 		bridged = 1;
691 	if (ifp->if_type == IFT_BRIDGE)
692 		is_bridge = 1;
693 
694 	/*
695 	 * We already have checked that mbuf contains enough contiguous data
696 	 * to hold entire arp message according to the arp header.
697 	 */
698 	ah = mtod(m, struct arphdr *);
699 
700 	/*
701 	 * ARP is only for IPv4 so we can reject packets with
702 	 * a protocol length not equal to an IPv4 address.
703 	 */
704 	if (ah->ar_pln != sizeof(struct in_addr)) {
705 		ARP_LOG(LOG_NOTICE, "requested protocol length != %zu\n",
706 		    sizeof(struct in_addr));
707 		goto drop;
708 	}
709 
710 	if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) {
711 		ARP_LOG(LOG_NOTICE, "%*D is multicast\n",
712 		    ifp->if_addrlen, (u_char *)ar_sha(ah), ":");
713 		goto drop;
714 	}
715 
716 	op = ntohs(ah->ar_op);
717 	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
718 	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
719 
720 	if (op == ARPOP_REPLY)
721 		ARPSTAT_INC(rxreplies);
722 
723 	/*
724 	 * For a bridge, we want to check the address irrespective
725 	 * of the receive interface. (This will change slightly
726 	 * when we have clusters of interfaces).
727 	 */
728 	IN_IFADDR_RLOCK(&in_ifa_tracker);
729 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
730 		if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
731 		    ia->ia_ifp == ifp) &&
732 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr &&
733 		    (ia->ia_ifa.ifa_carp == NULL ||
734 		    (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) {
735 			ifa_ref(&ia->ia_ifa);
736 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
737 			goto match;
738 		}
739 	}
740 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
741 		if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
742 		    ia->ia_ifp == ifp) &&
743 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
744 			ifa_ref(&ia->ia_ifa);
745 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
746 			goto match;
747 		}
748 
749 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia)				\
750   (ia->ia_ifp->if_bridge == ifp->if_softc &&				\
751   !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) &&	\
752   addr == ia->ia_addr.sin_addr.s_addr)
753 	/*
754 	 * Check the case when bridge shares its MAC address with
755 	 * some of its children, so packets are claimed by bridge
756 	 * itself (bridge_input() does it first), but they are really
757 	 * meant to be destined to the bridge member.
758 	 */
759 	if (is_bridge) {
760 		LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
761 			if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
762 				ifa_ref(&ia->ia_ifa);
763 				ifp = ia->ia_ifp;
764 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
765 				goto match;
766 			}
767 		}
768 	}
769 #undef BDG_MEMBER_MATCHES_ARP
770 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
771 
772 	/*
773 	 * No match, use the first inet address on the receive interface
774 	 * as a dummy address for the rest of the function.
775 	 */
776 	IF_ADDR_RLOCK(ifp);
777 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
778 		if (ifa->ifa_addr->sa_family == AF_INET &&
779 		    (ifa->ifa_carp == NULL ||
780 		    (*carp_iamatch_p)(ifa, &enaddr))) {
781 			ia = ifatoia(ifa);
782 			ifa_ref(ifa);
783 			IF_ADDR_RUNLOCK(ifp);
784 			goto match;
785 		}
786 	IF_ADDR_RUNLOCK(ifp);
787 
788 	/*
789 	 * If bridging, fall back to using any inet address.
790 	 */
791 	IN_IFADDR_RLOCK(&in_ifa_tracker);
792 	if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) {
793 		IN_IFADDR_RUNLOCK(&in_ifa_tracker);
794 		goto drop;
795 	}
796 	ifa_ref(&ia->ia_ifa);
797 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
798 match:
799 	if (!enaddr)
800 		enaddr = (u_int8_t *)IF_LLADDR(ifp);
801 	carped = (ia->ia_ifa.ifa_carp != NULL);
802 	myaddr = ia->ia_addr.sin_addr;
803 	ifa_free(&ia->ia_ifa);
804 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
805 		goto drop;	/* it's from me, ignore it. */
806 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
807 		ARP_LOG(LOG_NOTICE, "link address is broadcast for IP address "
808 		    "%s!\n", inet_ntoa(isaddr));
809 		goto drop;
810 	}
811 
812 	if (ifp->if_addrlen != ah->ar_hln) {
813 		ARP_LOG(LOG_WARNING, "from %*D: addr len: new %d, "
814 		    "i/f %d (ignored)\n", ifp->if_addrlen,
815 		    (u_char *) ar_sha(ah), ":", ah->ar_hln,
816 		    ifp->if_addrlen);
817 		goto drop;
818 	}
819 
820 	/*
821 	 * Warn if another host is using the same IP address, but only if the
822 	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
823 	 * case we suppress the warning to avoid false positive complaints of
824 	 * potential misconfiguration.
825 	 */
826 	if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr &&
827 	    myaddr.s_addr != 0) {
828 		ARP_LOG(LOG_ERR, "%*D is using my IP address %s on %s!\n",
829 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
830 		   inet_ntoa(isaddr), ifp->if_xname);
831 		itaddr = myaddr;
832 		ARPSTAT_INC(dupips);
833 		goto reply;
834 	}
835 	if (ifp->if_flags & IFF_STATICARP)
836 		goto reply;
837 
838 	bzero(&sin, sizeof(sin));
839 	sin.sin_len = sizeof(struct sockaddr_in);
840 	sin.sin_family = AF_INET;
841 	sin.sin_addr = isaddr;
842 	dst = (struct sockaddr *)&sin;
843 	IF_AFDATA_RLOCK(ifp);
844 	la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
845 	IF_AFDATA_RUNLOCK(ifp);
846 	if (la != NULL)
847 		arp_check_update_lle(ah, isaddr, ifp, bridged, la);
848 	else if (itaddr.s_addr == myaddr.s_addr) {
849 		/*
850 		 * Reply to our address, but no lle exists yet.
851 		 * do we really have to create an entry?
852 		 */
853 		la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
854 		if (la == NULL)
855 			goto drop;
856 		lltable_set_entry_addr(ifp, la, ar_sha(ah));
857 
858 		IF_AFDATA_WLOCK(ifp);
859 		LLE_WLOCK(la);
860 		la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
861 
862 		/*
863 		 * Check if lle still does not exists.
864 		 * If it does, that means that we either
865 		 * 1) have configured it explicitly, via
866 		 * 1a) 'arp -s' static entry or
867 		 * 1b) interface address static record
868 		 * or
869 		 * 2) it was the result of sending first packet to-host
870 		 * or
871 		 * 3) it was another arp reply packet we handled in
872 		 * different thread.
873 		 *
874 		 * In all cases except 3) we definitely need to prefer
875 		 * existing lle. For the sake of simplicity, prefer any
876 		 * existing lle over newly-create one.
877 		 */
878 		if (la_tmp == NULL)
879 			lltable_link_entry(LLTABLE(ifp), la);
880 		IF_AFDATA_WUNLOCK(ifp);
881 
882 		if (la_tmp == NULL) {
883 			arp_mark_lle_reachable(la);
884 			LLE_WUNLOCK(la);
885 		} else {
886 			/* Free newly-create entry and handle packet */
887 			lltable_free_entry(LLTABLE(ifp), la);
888 			la = la_tmp;
889 			la_tmp = NULL;
890 			arp_check_update_lle(ah, isaddr, ifp, bridged, la);
891 			/* arp_check_update_lle() returns @la unlocked */
892 		}
893 		la = NULL;
894 	}
895 reply:
896 	if (op != ARPOP_REQUEST)
897 		goto drop;
898 	ARPSTAT_INC(rxrequests);
899 
900 	if (itaddr.s_addr == myaddr.s_addr) {
901 		/* Shortcut.. the receiving interface is the target. */
902 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
903 		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
904 	} else {
905 		struct llentry *lle = NULL;
906 
907 		sin.sin_addr = itaddr;
908 		IF_AFDATA_RLOCK(ifp);
909 		lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
910 		IF_AFDATA_RUNLOCK(ifp);
911 
912 		if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
913 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
914 			(void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
915 			LLE_RUNLOCK(lle);
916 		} else {
917 
918 			if (lle != NULL)
919 				LLE_RUNLOCK(lle);
920 
921 			if (!V_arp_proxyall)
922 				goto drop;
923 
924 			sin.sin_addr = itaddr;
925 			/* XXX MRT use table 0 for arp reply  */
926 			rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
927 			if (!rt)
928 				goto drop;
929 
930 			/*
931 			 * Don't send proxies for nodes on the same interface
932 			 * as this one came out of, or we'll get into a fight
933 			 * over who claims what Ether address.
934 			 */
935 			if (!rt->rt_ifp || rt->rt_ifp == ifp) {
936 				RTFREE_LOCKED(rt);
937 				goto drop;
938 			}
939 			RTFREE_LOCKED(rt);
940 
941 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
942 			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
943 
944 			/*
945 			 * Also check that the node which sent the ARP packet
946 			 * is on the interface we expect it to be on. This
947 			 * avoids ARP chaos if an interface is connected to the
948 			 * wrong network.
949 			 */
950 			sin.sin_addr = isaddr;
951 
952 			/* XXX MRT use table 0 for arp checks */
953 			rt = in_rtalloc1((struct sockaddr *)&sin, 0, 0UL, 0);
954 			if (!rt)
955 				goto drop;
956 			if (rt->rt_ifp != ifp) {
957 				ARP_LOG(LOG_INFO, "proxy: ignoring request"
958 				    " from %s via %s, expecting %s\n",
959 				    inet_ntoa(isaddr), ifp->if_xname,
960 				    rt->rt_ifp->if_xname);
961 				RTFREE_LOCKED(rt);
962 				goto drop;
963 			}
964 			RTFREE_LOCKED(rt);
965 
966 #ifdef DEBUG_PROXY
967 			printf("arp: proxying for %s\n", inet_ntoa(itaddr));
968 #endif
969 		}
970 	}
971 
972 	if (itaddr.s_addr == myaddr.s_addr &&
973 	    IN_LINKLOCAL(ntohl(itaddr.s_addr))) {
974 		/* RFC 3927 link-local IPv4; always reply by broadcast. */
975 #ifdef DEBUG_LINKLOCAL
976 		printf("arp: sending reply for link-local addr %s\n",
977 		    inet_ntoa(itaddr));
978 #endif
979 		m->m_flags |= M_BCAST;
980 		m->m_flags &= ~M_MCAST;
981 	} else {
982 		/* default behaviour; never reply by broadcast. */
983 		m->m_flags &= ~(M_BCAST|M_MCAST);
984 	}
985 	(void)memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
986 	(void)memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
987 	ah->ar_op = htons(ARPOP_REPLY);
988 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
989 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
990 	m->m_pkthdr.len = m->m_len;
991 	m->m_pkthdr.rcvif = NULL;
992 	sa.sa_family = AF_ARP;
993 	sa.sa_len = 2;
994 	m_clrprotoflags(m);	/* Avoid confusing lower layers. */
995 	(*ifp->if_output)(ifp, m, &sa, NULL);
996 	ARPSTAT_INC(txreplies);
997 	return;
998 
999 drop:
1000 	m_freem(m);
1001 }
1002 #endif
1003 
1004 /*
1005  * Checks received arp data against existing @la.
1006  * Updates lle state/performs notification if necessary.
1007  */
1008 static void
1009 arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr, struct ifnet *ifp,
1010     int bridged, struct llentry *la)
1011 {
1012 	struct sockaddr sa;
1013 	struct mbuf *m_hold, *m_hold_next;
1014 
1015 	LLE_WLOCK_ASSERT(la);
1016 
1017 	/* the following is not an error when doing bridging */
1018 	if (!bridged && la->lle_tbl->llt_ifp != ifp) {
1019 		if (log_arp_wrong_iface)
1020 			ARP_LOG(LOG_WARNING, "%s is on %s "
1021 			    "but got reply from %*D on %s\n",
1022 			    inet_ntoa(isaddr),
1023 			    la->lle_tbl->llt_ifp->if_xname,
1024 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
1025 			    ifp->if_xname);
1026 		LLE_WUNLOCK(la);
1027 		return;
1028 	}
1029 	if ((la->la_flags & LLE_VALID) &&
1030 	    bcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
1031 		if (la->la_flags & LLE_STATIC) {
1032 			LLE_WUNLOCK(la);
1033 			if (log_arp_permanent_modify)
1034 				ARP_LOG(LOG_ERR,
1035 				    "%*D attempts to modify "
1036 				    "permanent entry for %s on %s\n",
1037 				    ifp->if_addrlen,
1038 				    (u_char *)ar_sha(ah), ":",
1039 				    inet_ntoa(isaddr), ifp->if_xname);
1040 			return;
1041 		}
1042 		if (log_arp_movements) {
1043 			ARP_LOG(LOG_INFO, "%s moved from %*D "
1044 			    "to %*D on %s\n",
1045 			    inet_ntoa(isaddr),
1046 			    ifp->if_addrlen,
1047 			    (u_char *)&la->ll_addr, ":",
1048 			    ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
1049 			    ifp->if_xname);
1050 		}
1051 	}
1052 
1053 	/* Check if something has changed */
1054 	if (memcmp(&la->ll_addr, ar_sha(ah), ifp->if_addrlen) != 0 ||
1055 	    (la->la_flags & LLE_VALID) == 0) {
1056 		/* Perform real LLE update */
1057 		/* use afdata WLOCK to update fields */
1058 		LLE_ADDREF(la);
1059 		LLE_WUNLOCK(la);
1060 		IF_AFDATA_WLOCK(ifp);
1061 		LLE_WLOCK(la);
1062 
1063 		/*
1064 		 * Since we droppped LLE lock, other thread might have deleted
1065 		 * this lle. Check and return
1066 		 */
1067 		if ((la->la_flags & LLE_DELETED) != 0) {
1068 			IF_AFDATA_WUNLOCK(ifp);
1069 			LLE_FREE_LOCKED(la);
1070 			return;
1071 		}
1072 
1073 		/* Update data */
1074 		lltable_set_entry_addr(ifp, la, ar_sha(ah));
1075 
1076 		IF_AFDATA_WUNLOCK(ifp);
1077 		LLE_REMREF(la);
1078 
1079 		/* Clear fast path feedback request if set */
1080 		la->r_skip_req = 0;
1081 	}
1082 
1083 	arp_mark_lle_reachable(la);
1084 
1085 	/*
1086 	 * The packets are all freed within the call to the output
1087 	 * routine.
1088 	 *
1089 	 * NB: The lock MUST be released before the call to the
1090 	 * output routine.
1091 	 */
1092 	if (la->la_hold != NULL) {
1093 		m_hold = la->la_hold;
1094 		la->la_hold = NULL;
1095 		la->la_numheld = 0;
1096 		lltable_fill_sa_entry(la, &sa);
1097 		LLE_WUNLOCK(la);
1098 		for (; m_hold != NULL; m_hold = m_hold_next) {
1099 			m_hold_next = m_hold->m_nextpkt;
1100 			m_hold->m_nextpkt = NULL;
1101 			/* Avoid confusing lower layers. */
1102 			m_clrprotoflags(m_hold);
1103 			(*ifp->if_output)(ifp, m_hold, &sa, NULL);
1104 		}
1105 	} else
1106 		LLE_WUNLOCK(la);
1107 }
1108 
1109 static void
1110 arp_mark_lle_reachable(struct llentry *la)
1111 {
1112 	int canceled, wtime;
1113 
1114 	LLE_WLOCK_ASSERT(la);
1115 
1116 	la->ln_state = ARP_LLINFO_REACHABLE;
1117 	EVENTHANDLER_INVOKE(lle_event, la, LLENTRY_RESOLVED);
1118 
1119 	if (!(la->la_flags & LLE_STATIC)) {
1120 		LLE_ADDREF(la);
1121 		la->la_expire = time_uptime + V_arpt_keep;
1122 		wtime = V_arpt_keep - V_arp_maxtries * V_arpt_rexmit;
1123 		if (wtime < 0)
1124 			wtime = V_arpt_keep;
1125 		canceled = callout_reset(&la->lle_timer,
1126 		    hz * wtime, arptimer, la);
1127 		if (canceled)
1128 			LLE_REMREF(la);
1129 	}
1130 	la->la_asked = 0;
1131 	la->la_preempt = V_arp_maxtries;
1132 }
1133 
1134 /*
1135  * Add pernament link-layer record for given interface address.
1136  */
1137 static __noinline void
1138 arp_add_ifa_lle(struct ifnet *ifp, const struct sockaddr *dst)
1139 {
1140 	struct llentry *lle, *lle_tmp;
1141 
1142 	/*
1143 	 * Interface address LLE record is considered static
1144 	 * because kernel code relies on LLE_STATIC flag to check
1145 	 * if these entries can be rewriten by arp updates.
1146 	 */
1147 	lle = lltable_alloc_entry(LLTABLE(ifp), LLE_IFADDR | LLE_STATIC, dst);
1148 	if (lle == NULL) {
1149 		log(LOG_INFO, "arp_ifinit: cannot create arp "
1150 		    "entry for interface address\n");
1151 		return;
1152 	}
1153 
1154 	IF_AFDATA_WLOCK(ifp);
1155 	LLE_WLOCK(lle);
1156 	/* Unlink any entry if exists */
1157 	lle_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
1158 	if (lle_tmp != NULL)
1159 		lltable_unlink_entry(LLTABLE(ifp), lle_tmp);
1160 
1161 	lltable_link_entry(LLTABLE(ifp), lle);
1162 	IF_AFDATA_WUNLOCK(ifp);
1163 
1164 	if (lle_tmp != NULL)
1165 		EVENTHANDLER_INVOKE(lle_event, lle_tmp, LLENTRY_EXPIRED);
1166 
1167 	EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED);
1168 	LLE_WUNLOCK(lle);
1169 	if (lle_tmp != NULL)
1170 		lltable_free_entry(LLTABLE(ifp), lle_tmp);
1171 }
1172 
1173 void
1174 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1175 {
1176 	const struct sockaddr_in *dst_in;
1177 	const struct sockaddr *dst;
1178 
1179 	if (ifa->ifa_carp != NULL)
1180 		return;
1181 
1182 	dst = ifa->ifa_addr;
1183 	dst_in = (const struct sockaddr_in *)dst;
1184 
1185 	if (ntohl(dst_in->sin_addr.s_addr) == INADDR_ANY)
1186 		return;
1187 	arp_announce_ifaddr(ifp, dst_in->sin_addr, IF_LLADDR(ifp));
1188 
1189 	arp_add_ifa_lle(ifp, dst);
1190 }
1191 
1192 void
1193 arp_announce_ifaddr(struct ifnet *ifp, struct in_addr addr, u_char *enaddr)
1194 {
1195 
1196 	if (ntohl(addr.s_addr) != INADDR_ANY)
1197 		arprequest(ifp, &addr, &addr, enaddr);
1198 }
1199 
1200 /*
1201  * Sends gratuitous ARPs for each ifaddr to notify other
1202  * nodes about the address change.
1203  */
1204 static __noinline void
1205 arp_handle_ifllchange(struct ifnet *ifp)
1206 {
1207 	struct ifaddr *ifa;
1208 
1209 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1210 		if (ifa->ifa_addr->sa_family == AF_INET)
1211 			arp_ifinit(ifp, ifa);
1212 	}
1213 }
1214 
1215 /*
1216  * A handler for interface link layer address change event.
1217  */
1218 static __noinline void
1219 arp_iflladdr(void *arg __unused, struct ifnet *ifp)
1220 {
1221 
1222 	if ((ifp->if_flags & IFF_UP) != 0)
1223 		arp_handle_ifllchange(ifp);
1224 }
1225 
1226 static void
1227 arp_init(void)
1228 {
1229 
1230 	netisr_register(&arp_nh);
1231 	if (IS_DEFAULT_VNET(curvnet))
1232 		iflladdr_tag = EVENTHANDLER_REGISTER(iflladdr_event,
1233 		    arp_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
1234 }
1235 SYSINIT(arp, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, arp_init, 0);
1236