xref: /freebsd/sys/netinet/if_ether.c (revision b6a05070fa77edc7ce6e60b61623fd806e807be6)
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_fib.h>
67 #include <netinet/in_var.h>
68 #include <net/if_llatbl.h>
69 #include <netinet/if_ether.h>
70 #ifdef INET
71 #include <netinet/ip_carp.h>
72 #endif
73 
74 #include <security/mac/mac_framework.h>
75 
76 #define SIN(s) ((const struct sockaddr_in *)(s))
77 
78 static struct timeval arp_lastlog;
79 static int arp_curpps;
80 static int arp_maxpps = 1;
81 
82 /* Simple ARP state machine */
83 enum arp_llinfo_state {
84 	ARP_LLINFO_INCOMPLETE = 0, /* No LLE data */
85 	ARP_LLINFO_REACHABLE,	/* LLE is valid */
86 	ARP_LLINFO_VERIFY,	/* LLE is valid, need refresh */
87 	ARP_LLINFO_DELETED,	/* LLE is deleted */
88 };
89 
90 SYSCTL_DECL(_net_link_ether);
91 static SYSCTL_NODE(_net_link_ether, PF_INET, inet, CTLFLAG_RW, 0, "");
92 static SYSCTL_NODE(_net_link_ether, PF_ARP, arp, CTLFLAG_RW, 0, "");
93 
94 /* timer values */
95 static VNET_DEFINE(int, arpt_keep) = (20*60);	/* once resolved, good for 20
96 						 * minutes */
97 static VNET_DEFINE(int, arp_maxtries) = 5;
98 static VNET_DEFINE(int, arp_proxyall) = 0;
99 static VNET_DEFINE(int, arpt_down) = 20;	/* keep incomplete entries for
100 						 * 20 seconds */
101 static VNET_DEFINE(int, arpt_rexmit) = 1;	/* retransmit arp entries, sec*/
102 VNET_PCPUSTAT_DEFINE(struct arpstat, arpstat);  /* ARP statistics, see if_arp.h */
103 VNET_PCPUSTAT_SYSINIT(arpstat);
104 
105 #ifdef VIMAGE
106 VNET_PCPUSTAT_SYSUNINIT(arpstat);
107 #endif /* VIMAGE */
108 
109 static VNET_DEFINE(int, arp_maxhold) = 1;
110 
111 #define	V_arpt_keep		VNET(arpt_keep)
112 #define	V_arpt_down		VNET(arpt_down)
113 #define	V_arpt_rexmit		VNET(arpt_rexmit)
114 #define	V_arp_maxtries		VNET(arp_maxtries)
115 #define	V_arp_proxyall		VNET(arp_proxyall)
116 #define	V_arp_maxhold		VNET(arp_maxhold)
117 
118 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_age, CTLFLAG_VNET | CTLFLAG_RW,
119 	&VNET_NAME(arpt_keep), 0,
120 	"ARP entry lifetime in seconds");
121 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxtries, CTLFLAG_VNET | CTLFLAG_RW,
122 	&VNET_NAME(arp_maxtries), 0,
123 	"ARP resolution attempts before returning error");
124 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, proxyall, CTLFLAG_VNET | CTLFLAG_RW,
125 	&VNET_NAME(arp_proxyall), 0,
126 	"Enable proxy ARP for all suitable requests");
127 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, wait, CTLFLAG_VNET | CTLFLAG_RW,
128 	&VNET_NAME(arpt_down), 0,
129 	"Incomplete ARP entry lifetime in seconds");
130 SYSCTL_VNET_PCPUSTAT(_net_link_ether_arp, OID_AUTO, stats, struct arpstat,
131     arpstat, "ARP statistics (struct arpstat, net/if_arp.h)");
132 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, maxhold, CTLFLAG_VNET | CTLFLAG_RW,
133 	&VNET_NAME(arp_maxhold), 0,
134 	"Number of packets to hold per ARP entry");
135 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, max_log_per_second,
136 	CTLFLAG_RW, &arp_maxpps, 0,
137 	"Maximum number of remotely triggered ARP messages that can be "
138 	"logged per second");
139 
140 #define	ARP_LOG(pri, ...)	do {					\
141 	if (ppsratecheck(&arp_lastlog, &arp_curpps, arp_maxpps))	\
142 		log((pri), "arp: " __VA_ARGS__);			\
143 } while (0)
144 
145 
146 static void	arp_init(void);
147 static void	arpintr(struct mbuf *);
148 static void	arptimer(void *);
149 #ifdef INET
150 static void	in_arpinput(struct mbuf *);
151 #endif
152 
153 static void arp_check_update_lle(struct arphdr *ah, struct in_addr isaddr,
154     struct ifnet *ifp, int bridged, struct llentry *la);
155 static void arp_mark_lle_reachable(struct llentry *la);
156 static void arp_iflladdr(void *arg __unused, struct ifnet *ifp);
157 
158 static eventhandler_tag iflladdr_tag;
159 
160 static const struct netisr_handler arp_nh = {
161 	.nh_name = "arp",
162 	.nh_handler = arpintr,
163 	.nh_proto = NETISR_ARP,
164 	.nh_policy = NETISR_POLICY_SOURCE,
165 };
166 
167 /*
168  * Timeout routine.  Age arp_tab entries periodically.
169  */
170 static void
171 arptimer(void *arg)
172 {
173 	struct llentry *lle = (struct llentry *)arg;
174 	struct ifnet *ifp;
175 	int r_skip_req;
176 
177 	if (lle->la_flags & LLE_STATIC) {
178 		return;
179 	}
180 	LLE_WLOCK(lle);
181 	if (callout_pending(&lle->lle_timer)) {
182 		/*
183 		 * Here we are a bit odd here in the treatment of
184 		 * active/pending. If the pending bit is set, it got
185 		 * rescheduled before I ran. The active
186 		 * bit we ignore, since if it was stopped
187 		 * in ll_tablefree() and was currently running
188 		 * it would have return 0 so the code would
189 		 * not have deleted it since the callout could
190 		 * not be stopped so we want to go through
191 		 * with the delete here now. If the callout
192 		 * was restarted, the pending bit will be back on and
193 		 * we just want to bail since the callout_reset would
194 		 * return 1 and our reference would have been removed
195 		 * by arpresolve() below.
196 		 */
197 		LLE_WUNLOCK(lle);
198  		return;
199  	}
200 	ifp = lle->lle_tbl->llt_ifp;
201 	CURVNET_SET(ifp->if_vnet);
202 
203 	switch (lle->ln_state) {
204 	case ARP_LLINFO_REACHABLE:
205 
206 		/*
207 		 * Expiration time is approaching.
208 		 * Let's try to refresh entry if it is still
209 		 * in use.
210 		 *
211 		 * Set r_skip_req to get feedback from
212 		 * fast path. Change state and re-schedule
213 		 * ourselves.
214 		 */
215 		LLE_REQ_LOCK(lle);
216 		lle->r_skip_req = 1;
217 		LLE_REQ_UNLOCK(lle);
218 		lle->ln_state = ARP_LLINFO_VERIFY;
219 		callout_schedule(&lle->lle_timer, hz * V_arpt_rexmit);
220 		LLE_WUNLOCK(lle);
221 		CURVNET_RESTORE();
222 		return;
223 	case ARP_LLINFO_VERIFY:
224 		LLE_REQ_LOCK(lle);
225 		r_skip_req = lle->r_skip_req;
226 		LLE_REQ_UNLOCK(lle);
227 
228 		if (r_skip_req == 0 && lle->la_preempt > 0) {
229 			/* Entry was used, issue refresh request */
230 			struct in_addr dst;
231 			dst = lle->r_l3addr.addr4;
232 			lle->la_preempt--;
233 			callout_schedule(&lle->lle_timer, hz * V_arpt_rexmit);
234 			LLE_WUNLOCK(lle);
235 			arprequest(ifp, NULL, &dst, NULL);
236 			CURVNET_RESTORE();
237 			return;
238 		}
239 		/* Nothing happened. Reschedule if not too late */
240 		if (lle->la_expire > time_uptime) {
241 			callout_schedule(&lle->lle_timer, hz * V_arpt_rexmit);
242 			LLE_WUNLOCK(lle);
243 			CURVNET_RESTORE();
244 			return;
245 		}
246 		break;
247 	case ARP_LLINFO_INCOMPLETE:
248 	case ARP_LLINFO_DELETED:
249 		break;
250 	}
251 
252 	if ((lle->la_flags & LLE_DELETED) == 0) {
253 		int evt;
254 
255 		if (lle->la_flags & LLE_VALID)
256 			evt = LLENTRY_EXPIRED;
257 		else
258 			evt = LLENTRY_TIMEDOUT;
259 		EVENTHANDLER_INVOKE(lle_event, lle, evt);
260 	}
261 
262 	callout_stop(&lle->lle_timer);
263 
264 	/* XXX: LOR avoidance. We still have ref on lle. */
265 	LLE_WUNLOCK(lle);
266 	IF_AFDATA_LOCK(ifp);
267 	LLE_WLOCK(lle);
268 
269 	/* Guard against race with other llentry_free(). */
270 	if (lle->la_flags & LLE_LINKED) {
271 		LLE_REMREF(lle);
272 		lltable_unlink_entry(lle->lle_tbl, lle);
273 	}
274 	IF_AFDATA_UNLOCK(ifp);
275 
276 	size_t pkts_dropped = llentry_free(lle);
277 
278 	ARPSTAT_ADD(dropped, pkts_dropped);
279 	ARPSTAT_INC(timeouts);
280 
281 	CURVNET_RESTORE();
282 }
283 
284 /*
285  * Broadcast an ARP request. Caller specifies:
286  *	- arp header source ip address
287  *	- arp header target ip address
288  *	- arp header source ethernet address
289  */
290 void
291 arprequest(struct ifnet *ifp, const struct in_addr *sip,
292     const struct in_addr *tip, u_char *enaddr)
293 {
294 	struct mbuf *m;
295 	struct arphdr *ah;
296 	struct sockaddr sa;
297 	u_char *carpaddr = NULL;
298 
299 	if (sip == NULL) {
300 		/*
301 		 * The caller did not supply a source address, try to find
302 		 * a compatible one among those assigned to this interface.
303 		 */
304 		struct ifaddr *ifa;
305 
306 		IF_ADDR_RLOCK(ifp);
307 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
308 			if (ifa->ifa_addr->sa_family != AF_INET)
309 				continue;
310 
311 			if (ifa->ifa_carp) {
312 				if ((*carp_iamatch_p)(ifa, &carpaddr) == 0)
313 					continue;
314 				sip = &IA_SIN(ifa)->sin_addr;
315 			} else {
316 				carpaddr = NULL;
317 				sip = &IA_SIN(ifa)->sin_addr;
318 			}
319 
320 			if (0 == ((sip->s_addr ^ tip->s_addr) &
321 			    IA_MASKSIN(ifa)->sin_addr.s_addr))
322 				break;  /* found it. */
323 		}
324 		IF_ADDR_RUNLOCK(ifp);
325 		if (sip == NULL) {
326 			printf("%s: cannot find matching address\n", __func__);
327 			return;
328 		}
329 	}
330 	if (enaddr == NULL)
331 		enaddr = carpaddr ? carpaddr : (u_char *)IF_LLADDR(ifp);
332 
333 	if ((m = m_gethdr(M_NOWAIT, MT_DATA)) == NULL)
334 		return;
335 	m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
336 		2 * ifp->if_addrlen;
337 	m->m_pkthdr.len = m->m_len;
338 	M_ALIGN(m, m->m_len);
339 	ah = mtod(m, struct arphdr *);
340 	bzero((caddr_t)ah, m->m_len);
341 #ifdef MAC
342 	mac_netinet_arp_send(ifp, m);
343 #endif
344 	ah->ar_pro = htons(ETHERTYPE_IP);
345 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
346 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
347 	ah->ar_op = htons(ARPOP_REQUEST);
348 	bcopy(enaddr, ar_sha(ah), ah->ar_hln);
349 	bcopy(sip, ar_spa(ah), ah->ar_pln);
350 	bcopy(tip, ar_tpa(ah), ah->ar_pln);
351 	sa.sa_family = AF_ARP;
352 	sa.sa_len = 2;
353 	m->m_flags |= M_BCAST;
354 	m_clrprotoflags(m);	/* Avoid confusing lower layers. */
355 	(*ifp->if_output)(ifp, m, &sa, NULL);
356 	ARPSTAT_INC(txrequests);
357 }
358 
359 /*
360  * Resolve an IP address into an ethernet address - heavy version.
361  * Used internally by arpresolve().
362  * We have already checked than  we can't use existing lle without
363  * modification so we have to acquire LLE_EXCLUSIVE lle lock.
364  *
365  * On success, desten and flags are filled in and the function returns 0;
366  * If the packet must be held pending resolution, we return EWOULDBLOCK
367  * On other errors, we return the corresponding error code.
368  * Note that m_freem() handles NULL.
369  */
370 static int
371 arpresolve_full(struct ifnet *ifp, int is_gw, int create, struct mbuf *m,
372 	const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
373 {
374 	struct llentry *la = NULL, *la_tmp;
375 	struct mbuf *curr = NULL;
376 	struct mbuf *next = NULL;
377 	int error, renew;
378 
379 	if (pflags != NULL)
380 		*pflags = 0;
381 
382 	if (create == 0) {
383 		IF_AFDATA_RLOCK(ifp);
384 		la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
385 		IF_AFDATA_RUNLOCK(ifp);
386 	}
387 	if (la == NULL && (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) {
388 		la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
389 		if (la == NULL) {
390 			log(LOG_DEBUG,
391 			    "arpresolve: can't allocate llinfo for %s on %s\n",
392 			    inet_ntoa(SIN(dst)->sin_addr), if_name(ifp));
393 			m_freem(m);
394 			return (EINVAL);
395 		}
396 
397 		IF_AFDATA_WLOCK(ifp);
398 		LLE_WLOCK(la);
399 		la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
400 		/* Prefer ANY existing lle over newly-created one */
401 		if (la_tmp == NULL)
402 			lltable_link_entry(LLTABLE(ifp), la);
403 		IF_AFDATA_WUNLOCK(ifp);
404 		if (la_tmp != NULL) {
405 			lltable_free_entry(LLTABLE(ifp), la);
406 			la = la_tmp;
407 		}
408 	}
409 	if (la == NULL) {
410 		m_freem(m);
411 		return (EINVAL);
412 	}
413 
414 	if ((la->la_flags & LLE_VALID) &&
415 	    ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
416 		bcopy(&la->ll_addr, desten, ifp->if_addrlen);
417 
418 		/* Check if we have feedback request from arptimer() */
419 		if (la->r_skip_req != 0) {
420 			LLE_REQ_LOCK(la);
421 			la->r_skip_req = 0; /* Notify that entry was used */
422 			LLE_REQ_UNLOCK(la);
423 		}
424 		if (pflags != NULL)
425 			*pflags = la->la_flags & (LLE_VALID|LLE_IFADDR);
426 		LLE_WUNLOCK(la);
427 		return (0);
428 	}
429 
430 	renew = (la->la_asked == 0 || la->la_expire != time_uptime);
431 	/*
432 	 * There is an arptab entry, but no ethernet address
433 	 * response yet.  Add the mbuf to the list, dropping
434 	 * the oldest packet if we have exceeded the system
435 	 * setting.
436 	 */
437 	if (m != NULL) {
438 		if (la->la_numheld >= V_arp_maxhold) {
439 			if (la->la_hold != NULL) {
440 				next = la->la_hold->m_nextpkt;
441 				m_freem(la->la_hold);
442 				la->la_hold = next;
443 				la->la_numheld--;
444 				ARPSTAT_INC(dropped);
445 			}
446 		}
447 		if (la->la_hold != NULL) {
448 			curr = la->la_hold;
449 			while (curr->m_nextpkt != NULL)
450 				curr = curr->m_nextpkt;
451 			curr->m_nextpkt = m;
452 		} else
453 			la->la_hold = m;
454 		la->la_numheld++;
455 	}
456 	/*
457 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
458 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
459 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
460 	 * ARP request, but not faster than one request per second.
461 	 */
462 	if (la->la_asked < V_arp_maxtries)
463 		error = EWOULDBLOCK;	/* First request. */
464 	else
465 		error = is_gw != 0 ? EHOSTUNREACH : EHOSTDOWN;
466 
467 	if (renew) {
468 		int canceled;
469 
470 		LLE_ADDREF(la);
471 		la->la_expire = time_uptime;
472 		canceled = callout_reset(&la->lle_timer, hz * V_arpt_down,
473 		    arptimer, la);
474 		if (canceled)
475 			LLE_REMREF(la);
476 		la->la_asked++;
477 		LLE_WUNLOCK(la);
478 		arprequest(ifp, NULL, &SIN(dst)->sin_addr, NULL);
479 		return (error);
480 	}
481 
482 	LLE_WUNLOCK(la);
483 	return (error);
484 }
485 
486 /*
487  * Resolve an IP address into an ethernet address.
488  * On input:
489  *    ifp is the interface we use
490  *    is_gw != 0 if @dst represents gateway to some destination
491  *    m is the mbuf. May be NULL if we don't have a packet.
492  *    dst is the next hop,
493  *    desten is the storage to put LL address.
494  *    flags returns subset of lle flags: LLE_VALID | LLE_IFADDR
495  *
496  * On success, desten and flags are filled in and the function returns 0;
497  * If the packet must be held pending resolution, we return EWOULDBLOCK
498  * On other errors, we return the corresponding error code.
499  * Note that m_freem() handles NULL.
500  */
501 int
502 arpresolve(struct ifnet *ifp, int is_gw, struct mbuf *m,
503 	const struct sockaddr *dst, u_char *desten, uint32_t *pflags)
504 {
505 	struct llentry *la = 0;
506 
507 	if (pflags != NULL)
508 		*pflags = 0;
509 
510 	if (m != NULL) {
511 		if (m->m_flags & M_BCAST) {
512 			/* broadcast */
513 			(void)memcpy(desten,
514 			    ifp->if_broadcastaddr, ifp->if_addrlen);
515 			return (0);
516 		}
517 		if (m->m_flags & M_MCAST) {
518 			/* multicast */
519 			ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
520 			return (0);
521 		}
522 	}
523 
524 	IF_AFDATA_RLOCK(ifp);
525 	la = lla_lookup(LLTABLE(ifp), LLE_UNLOCKED, dst);
526 	if (la != NULL && (la->r_flags & RLLE_VALID) != 0) {
527 		/* Entry found, let's copy lle info */
528 		bcopy(&la->ll_addr, desten, ifp->if_addrlen);
529 		if (pflags != NULL)
530 			*pflags = LLE_VALID | (la->r_flags & RLLE_IFADDR);
531 		/* Check if we have feedback request from arptimer() */
532 		if (la->r_skip_req != 0) {
533 			LLE_REQ_LOCK(la);
534 			la->r_skip_req = 0; /* Notify that entry was used */
535 			LLE_REQ_UNLOCK(la);
536 		}
537 		IF_AFDATA_RUNLOCK(ifp);
538 		return (0);
539 	}
540 	IF_AFDATA_RUNLOCK(ifp);
541 
542 	return (arpresolve_full(ifp, is_gw, 1, m, dst, desten, pflags));
543 }
544 
545 /*
546  * Common length and type checks are done here,
547  * then the protocol-specific routine is called.
548  */
549 static void
550 arpintr(struct mbuf *m)
551 {
552 	struct arphdr *ar;
553 	struct ifnet *ifp;
554 	char *layer;
555 	int hlen;
556 
557 	ifp = m->m_pkthdr.rcvif;
558 
559 	if (m->m_len < sizeof(struct arphdr) &&
560 	    ((m = m_pullup(m, sizeof(struct arphdr))) == NULL)) {
561 		ARP_LOG(LOG_NOTICE, "packet with short header received on %s\n",
562 		    if_name(ifp));
563 		return;
564 	}
565 	ar = mtod(m, struct arphdr *);
566 
567 	/* Check if length is sufficient */
568 	if (m->m_len <  arphdr_len(ar)) {
569 		m = m_pullup(m, arphdr_len(ar));
570 		if (m == NULL) {
571 			ARP_LOG(LOG_NOTICE, "short packet received on %s\n",
572 			    if_name(ifp));
573 			return;
574 		}
575 		ar = mtod(m, struct arphdr *);
576 	}
577 
578 	hlen = 0;
579 	layer = "";
580 	switch (ntohs(ar->ar_hrd)) {
581 	case ARPHRD_ETHER:
582 		hlen = ETHER_ADDR_LEN; /* RFC 826 */
583 		layer = "ethernet";
584 		break;
585 	case ARPHRD_IEEE802:
586 		hlen = 6; /* RFC 1390, FDDI_ADDR_LEN */
587 		layer = "fddi";
588 		break;
589 	case ARPHRD_ARCNET:
590 		hlen = 1; /* RFC 1201, ARC_ADDR_LEN */
591 		layer = "arcnet";
592 		break;
593 	case ARPHRD_INFINIBAND:
594 		hlen = 20;	/* RFC 4391, INFINIBAND_ALEN */
595 		layer = "infiniband";
596 		break;
597 	case ARPHRD_IEEE1394:
598 		hlen = 0; /* SHALL be 16 */ /* RFC 2734 */
599 		layer = "firewire";
600 
601 		/*
602 		 * Restrict too long harware addresses.
603 		 * Currently we are capable of handling 20-byte
604 		 * addresses ( sizeof(lle->ll_addr) )
605 		 */
606 		if (ar->ar_hln >= 20)
607 			hlen = 16;
608 		break;
609 	default:
610 		ARP_LOG(LOG_NOTICE,
611 		    "packet with unknown harware format 0x%02d received on %s\n",
612 		    ntohs(ar->ar_hrd), if_name(ifp));
613 		m_freem(m);
614 		return;
615 	}
616 
617 	if (hlen != 0 && hlen != ar->ar_hln) {
618 		ARP_LOG(LOG_NOTICE,
619 		    "packet with invalid %s address length %d received on %s\n",
620 		    layer, ar->ar_hln, if_name(ifp));
621 		m_freem(m);
622 		return;
623 	}
624 
625 	ARPSTAT_INC(received);
626 	switch (ntohs(ar->ar_pro)) {
627 #ifdef INET
628 	case ETHERTYPE_IP:
629 		in_arpinput(m);
630 		return;
631 #endif
632 	}
633 	m_freem(m);
634 }
635 
636 #ifdef INET
637 /*
638  * ARP for Internet protocols on 10 Mb/s Ethernet.
639  * Algorithm is that given in RFC 826.
640  * In addition, a sanity check is performed on the sender
641  * protocol address, to catch impersonators.
642  * We no longer handle negotiations for use of trailer protocol:
643  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
644  * along with IP replies if we wanted trailers sent to us,
645  * and also sent them in response to IP replies.
646  * This allowed either end to announce the desire to receive
647  * trailer packets.
648  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
649  * but formerly didn't normally send requests.
650  */
651 static int log_arp_wrong_iface = 1;
652 static int log_arp_movements = 1;
653 static int log_arp_permanent_modify = 1;
654 static int allow_multicast = 0;
655 
656 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_wrong_iface, CTLFLAG_RW,
657 	&log_arp_wrong_iface, 0,
658 	"log arp packets arriving on the wrong interface");
659 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_movements, CTLFLAG_RW,
660 	&log_arp_movements, 0,
661 	"log arp replies from MACs different than the one in the cache");
662 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, log_arp_permanent_modify, CTLFLAG_RW,
663 	&log_arp_permanent_modify, 0,
664 	"log arp replies from MACs different than the one in the permanent arp entry");
665 SYSCTL_INT(_net_link_ether_inet, OID_AUTO, allow_multicast, CTLFLAG_RW,
666 	&allow_multicast, 0, "accept multicast addresses");
667 
668 static void
669 in_arpinput(struct mbuf *m)
670 {
671 	struct rm_priotracker in_ifa_tracker;
672 	struct arphdr *ah;
673 	struct ifnet *ifp = m->m_pkthdr.rcvif;
674 	struct llentry *la = NULL, *la_tmp;
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 	struct nhop4_basic nh4;
686 
687 	sin.sin_len = sizeof(struct sockaddr_in);
688 	sin.sin_family = AF_INET;
689 	sin.sin_addr.s_addr = 0;
690 
691 	if (ifp->if_bridge)
692 		bridged = 1;
693 	if (ifp->if_type == IFT_BRIDGE)
694 		is_bridge = 1;
695 
696 	/*
697 	 * We already have checked that mbuf contains enough contiguous data
698 	 * to hold entire arp message according to the arp header.
699 	 */
700 	ah = mtod(m, struct arphdr *);
701 
702 	/*
703 	 * ARP is only for IPv4 so we can reject packets with
704 	 * a protocol length not equal to an IPv4 address.
705 	 */
706 	if (ah->ar_pln != sizeof(struct in_addr)) {
707 		ARP_LOG(LOG_NOTICE, "requested protocol length != %zu\n",
708 		    sizeof(struct in_addr));
709 		goto drop;
710 	}
711 
712 	if (allow_multicast == 0 && ETHER_IS_MULTICAST(ar_sha(ah))) {
713 		ARP_LOG(LOG_NOTICE, "%*D is multicast\n",
714 		    ifp->if_addrlen, (u_char *)ar_sha(ah), ":");
715 		goto drop;
716 	}
717 
718 	op = ntohs(ah->ar_op);
719 	(void)memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
720 	(void)memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
721 
722 	if (op == ARPOP_REPLY)
723 		ARPSTAT_INC(rxreplies);
724 
725 	/*
726 	 * For a bridge, we want to check the address irrespective
727 	 * of the receive interface. (This will change slightly
728 	 * when we have clusters of interfaces).
729 	 */
730 	IN_IFADDR_RLOCK(&in_ifa_tracker);
731 	LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
732 		if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
733 		    ia->ia_ifp == ifp) &&
734 		    itaddr.s_addr == ia->ia_addr.sin_addr.s_addr &&
735 		    (ia->ia_ifa.ifa_carp == NULL ||
736 		    (*carp_iamatch_p)(&ia->ia_ifa, &enaddr))) {
737 			ifa_ref(&ia->ia_ifa);
738 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
739 			goto match;
740 		}
741 	}
742 	LIST_FOREACH(ia, INADDR_HASH(isaddr.s_addr), ia_hash)
743 		if (((bridged && ia->ia_ifp->if_bridge == ifp->if_bridge) ||
744 		    ia->ia_ifp == ifp) &&
745 		    isaddr.s_addr == ia->ia_addr.sin_addr.s_addr) {
746 			ifa_ref(&ia->ia_ifa);
747 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
748 			goto match;
749 		}
750 
751 #define BDG_MEMBER_MATCHES_ARP(addr, ifp, ia)				\
752   (ia->ia_ifp->if_bridge == ifp->if_softc &&				\
753   !bcmp(IF_LLADDR(ia->ia_ifp), IF_LLADDR(ifp), ifp->if_addrlen) &&	\
754   addr == ia->ia_addr.sin_addr.s_addr)
755 	/*
756 	 * Check the case when bridge shares its MAC address with
757 	 * some of its children, so packets are claimed by bridge
758 	 * itself (bridge_input() does it first), but they are really
759 	 * meant to be destined to the bridge member.
760 	 */
761 	if (is_bridge) {
762 		LIST_FOREACH(ia, INADDR_HASH(itaddr.s_addr), ia_hash) {
763 			if (BDG_MEMBER_MATCHES_ARP(itaddr.s_addr, ifp, ia)) {
764 				ifa_ref(&ia->ia_ifa);
765 				ifp = ia->ia_ifp;
766 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
767 				goto match;
768 			}
769 		}
770 	}
771 #undef BDG_MEMBER_MATCHES_ARP
772 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
773 
774 	/*
775 	 * No match, use the first inet address on the receive interface
776 	 * as a dummy address for the rest of the function.
777 	 */
778 	IF_ADDR_RLOCK(ifp);
779 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
780 		if (ifa->ifa_addr->sa_family == AF_INET &&
781 		    (ifa->ifa_carp == NULL ||
782 		    (*carp_iamatch_p)(ifa, &enaddr))) {
783 			ia = ifatoia(ifa);
784 			ifa_ref(ifa);
785 			IF_ADDR_RUNLOCK(ifp);
786 			goto match;
787 		}
788 	IF_ADDR_RUNLOCK(ifp);
789 
790 	/*
791 	 * If bridging, fall back to using any inet address.
792 	 */
793 	IN_IFADDR_RLOCK(&in_ifa_tracker);
794 	if (!bridged || (ia = TAILQ_FIRST(&V_in_ifaddrhead)) == NULL) {
795 		IN_IFADDR_RUNLOCK(&in_ifa_tracker);
796 		goto drop;
797 	}
798 	ifa_ref(&ia->ia_ifa);
799 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
800 match:
801 	if (!enaddr)
802 		enaddr = (u_int8_t *)IF_LLADDR(ifp);
803 	carped = (ia->ia_ifa.ifa_carp != NULL);
804 	myaddr = ia->ia_addr.sin_addr;
805 	ifa_free(&ia->ia_ifa);
806 	if (!bcmp(ar_sha(ah), enaddr, ifp->if_addrlen))
807 		goto drop;	/* it's from me, ignore it. */
808 	if (!bcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
809 		ARP_LOG(LOG_NOTICE, "link address is broadcast for IP address "
810 		    "%s!\n", inet_ntoa(isaddr));
811 		goto drop;
812 	}
813 
814 	if (ifp->if_addrlen != ah->ar_hln) {
815 		ARP_LOG(LOG_WARNING, "from %*D: addr len: new %d, "
816 		    "i/f %d (ignored)\n", ifp->if_addrlen,
817 		    (u_char *) ar_sha(ah), ":", ah->ar_hln,
818 		    ifp->if_addrlen);
819 		goto drop;
820 	}
821 
822 	/*
823 	 * Warn if another host is using the same IP address, but only if the
824 	 * IP address isn't 0.0.0.0, which is used for DHCP only, in which
825 	 * case we suppress the warning to avoid false positive complaints of
826 	 * potential misconfiguration.
827 	 */
828 	if (!bridged && !carped && isaddr.s_addr == myaddr.s_addr &&
829 	    myaddr.s_addr != 0) {
830 		ARP_LOG(LOG_ERR, "%*D is using my IP address %s on %s!\n",
831 		   ifp->if_addrlen, (u_char *)ar_sha(ah), ":",
832 		   inet_ntoa(isaddr), ifp->if_xname);
833 		itaddr = myaddr;
834 		ARPSTAT_INC(dupips);
835 		goto reply;
836 	}
837 	if (ifp->if_flags & IFF_STATICARP)
838 		goto reply;
839 
840 	bzero(&sin, sizeof(sin));
841 	sin.sin_len = sizeof(struct sockaddr_in);
842 	sin.sin_family = AF_INET;
843 	sin.sin_addr = isaddr;
844 	dst = (struct sockaddr *)&sin;
845 	IF_AFDATA_RLOCK(ifp);
846 	la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
847 	IF_AFDATA_RUNLOCK(ifp);
848 	if (la != NULL)
849 		arp_check_update_lle(ah, isaddr, ifp, bridged, la);
850 	else if (itaddr.s_addr == myaddr.s_addr) {
851 		/*
852 		 * Request/reply to our address, but no lle exists yet.
853 		 * Try to create new llentry.
854 		 */
855 		la = lltable_alloc_entry(LLTABLE(ifp), 0, dst);
856 		if (la == NULL) {
857 
858 			/*
859 			 * lle creation may fail if source address belongs
860 			 * to non-directly connected subnet. However, we
861 			 * will try to answer the request instead of dropping
862 			 * frame.
863 			 */
864 			goto reply;
865 		}
866 		lltable_set_entry_addr(ifp, la, ar_sha(ah));
867 
868 		IF_AFDATA_WLOCK(ifp);
869 		LLE_WLOCK(la);
870 		la_tmp = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
871 
872 		/*
873 		 * Check if lle still does not exists.
874 		 * If it does, that means that we either
875 		 * 1) have configured it explicitly, via
876 		 * 1a) 'arp -s' static entry or
877 		 * 1b) interface address static record
878 		 * or
879 		 * 2) it was the result of sending first packet to-host
880 		 * or
881 		 * 3) it was another arp reply packet we handled in
882 		 * different thread.
883 		 *
884 		 * In all cases except 3) we definitely need to prefer
885 		 * existing lle. For the sake of simplicity, prefer any
886 		 * existing lle over newly-create one.
887 		 */
888 		if (la_tmp == NULL)
889 			lltable_link_entry(LLTABLE(ifp), la);
890 		IF_AFDATA_WUNLOCK(ifp);
891 
892 		if (la_tmp == NULL) {
893 			arp_mark_lle_reachable(la);
894 			LLE_WUNLOCK(la);
895 		} else {
896 			/* Free newly-create entry and handle packet */
897 			lltable_free_entry(LLTABLE(ifp), la);
898 			la = la_tmp;
899 			la_tmp = NULL;
900 			arp_check_update_lle(ah, isaddr, ifp, bridged, la);
901 			/* arp_check_update_lle() returns @la unlocked */
902 		}
903 		la = NULL;
904 	}
905 reply:
906 	if (op != ARPOP_REQUEST)
907 		goto drop;
908 	ARPSTAT_INC(rxrequests);
909 
910 	if (itaddr.s_addr == myaddr.s_addr) {
911 		/* Shortcut.. the receiving interface is the target. */
912 		(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
913 		(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
914 	} else {
915 		struct llentry *lle = NULL;
916 
917 		sin.sin_addr = itaddr;
918 		IF_AFDATA_RLOCK(ifp);
919 		lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
920 		IF_AFDATA_RUNLOCK(ifp);
921 
922 		if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
923 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
924 			(void)memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
925 			LLE_RUNLOCK(lle);
926 		} else {
927 
928 			if (lle != NULL)
929 				LLE_RUNLOCK(lle);
930 
931 			if (!V_arp_proxyall)
932 				goto drop;
933 
934 			/* XXX MRT use table 0 for arp reply  */
935 			if (fib4_lookup_nh_basic(0, itaddr, 0, 0, &nh4) != 0)
936 				goto drop;
937 
938 			/*
939 			 * Don't send proxies for nodes on the same interface
940 			 * as this one came out of, or we'll get into a fight
941 			 * over who claims what Ether address.
942 			 */
943 			if (nh4.nh_ifp == ifp)
944 				goto drop;
945 
946 			(void)memcpy(ar_tha(ah), ar_sha(ah), ah->ar_hln);
947 			(void)memcpy(ar_sha(ah), enaddr, ah->ar_hln);
948 
949 			/*
950 			 * Also check that the node which sent the ARP packet
951 			 * is on the interface we expect it to be on. This
952 			 * avoids ARP chaos if an interface is connected to the
953 			 * wrong network.
954 			 */
955 
956 			/* XXX MRT use table 0 for arp checks */
957 			if (fib4_lookup_nh_basic(0, isaddr, 0, 0, &nh4) != 0)
958 				goto drop;
959 			if (nh4.nh_ifp != ifp) {
960 				ARP_LOG(LOG_INFO, "proxy: ignoring request"
961 				    " from %s via %s\n",
962 				    inet_ntoa(isaddr), ifp->if_xname);
963 				goto drop;
964 			}
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