xref: /netbsd-src/sys/netinet/if_arp.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: if_arp.c,v 1.242 2017/02/11 15:37:30 roy Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Public Access Networks Corporation ("Panix").  It was developed under
9  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1988, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)if_ether.c	8.2 (Berkeley) 9/26/94
62  */
63 
64 /*
65  * Ethernet address resolution protocol.
66  * TODO:
67  *	add "inuse/lock" bit (or ref. count) along with valid bit
68  */
69 
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.242 2017/02/11 15:37:30 roy Exp $");
72 
73 #ifdef _KERNEL_OPT
74 #include "opt_ddb.h"
75 #include "opt_inet.h"
76 #include "opt_net_mpsafe.h"
77 #endif
78 
79 #ifdef INET
80 
81 #include "arp.h"
82 #include "bridge.h"
83 
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/callout.h>
87 #include <sys/malloc.h>
88 #include <sys/mbuf.h>
89 #include <sys/socket.h>
90 #include <sys/time.h>
91 #include <sys/timetc.h>
92 #include <sys/kernel.h>
93 #include <sys/errno.h>
94 #include <sys/ioctl.h>
95 #include <sys/syslog.h>
96 #include <sys/proc.h>
97 #include <sys/protosw.h>
98 #include <sys/domain.h>
99 #include <sys/sysctl.h>
100 #include <sys/socketvar.h>
101 #include <sys/percpu.h>
102 #include <sys/cprng.h>
103 #include <sys/kmem.h>
104 
105 #include <net/ethertypes.h>
106 #include <net/if.h>
107 #include <net/if_dl.h>
108 #include <net/if_token.h>
109 #include <net/if_types.h>
110 #include <net/if_ether.h>
111 #include <net/if_llatbl.h>
112 #include <net/net_osdep.h>
113 #include <net/route.h>
114 #include <net/net_stats.h>
115 
116 #include <netinet/in.h>
117 #include <netinet/in_systm.h>
118 #include <netinet/in_var.h>
119 #include <netinet/ip.h>
120 #include <netinet/if_inarp.h>
121 
122 #include "arcnet.h"
123 #if NARCNET > 0
124 #include <net/if_arc.h>
125 #endif
126 #include "fddi.h"
127 #if NFDDI > 0
128 #include <net/if_fddi.h>
129 #endif
130 #include "token.h"
131 #include "carp.h"
132 #if NCARP > 0
133 #include <netinet/ip_carp.h>
134 #endif
135 
136 #define SIN(s) ((struct sockaddr_in *)s)
137 #define SRP(s) ((struct sockaddr_inarp *)s)
138 
139 /*
140  * ARP trailer negotiation.  Trailer protocol is not IP specific,
141  * but ARP request/response use IP addresses.
142  */
143 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
144 
145 /* timer values */
146 static int	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
147 static int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
148 static int	arp_maxhold = 1;	/* number of packets to hold per ARP entry */
149 #define	rt_expire rt_rmx.rmx_expire
150 #define	rt_pksent rt_rmx.rmx_pksent
151 
152 int		ip_dad_count = PROBE_NUM;
153 #ifdef ARP_DEBUG
154 int		arp_debug = 1;
155 #else
156 int		arp_debug = 0;
157 #endif
158 
159 static	void arp_init(void);
160 
161 static	void arprequest(struct ifnet *,
162     const struct in_addr *, const struct in_addr *,
163     const u_int8_t *);
164 static	void arpannounce1(struct ifaddr *);
165 static	struct sockaddr *arp_setgate(struct rtentry *, struct sockaddr *,
166 	    const struct sockaddr *);
167 static	void arptimer(void *);
168 static	void arp_settimer(struct llentry *, int);
169 static	struct llentry *arplookup(struct ifnet *, struct mbuf *,
170 	    const struct in_addr *, const struct sockaddr *, int);
171 static	struct llentry *arpcreate(struct ifnet *, struct mbuf *,
172 	    const struct in_addr *, const struct sockaddr *, int);
173 static	void in_arpinput(struct mbuf *);
174 static	void in_revarpinput(struct mbuf *);
175 static	void revarprequest(struct ifnet *);
176 
177 static	void arp_drainstub(void);
178 
179 static void arp_dad_timer(struct ifaddr *);
180 static void arp_dad_start(struct ifaddr *);
181 static void arp_dad_stop(struct ifaddr *);
182 static void arp_dad_duplicated(struct ifaddr *, const char *);
183 
184 static void arp_init_llentry(struct ifnet *, struct llentry *);
185 #if NTOKEN > 0
186 static void arp_free_llentry_tokenring(struct llentry *);
187 #endif
188 
189 struct	ifqueue arpintrq = {
190 	.ifq_head = NULL,
191 	.ifq_tail = NULL,
192 	.ifq_len = 0,
193 	.ifq_maxlen = 50,
194 	.ifq_drops = 0,
195 };
196 static int	arp_maxtries = 5;
197 static int	useloopback = 1;	/* use loopback interface for local traffic */
198 
199 static percpu_t *arpstat_percpu;
200 
201 #define	ARP_STAT_GETREF()	_NET_STAT_GETREF(arpstat_percpu)
202 #define	ARP_STAT_PUTREF()	_NET_STAT_PUTREF(arpstat_percpu)
203 
204 #define	ARP_STATINC(x)		_NET_STATINC(arpstat_percpu, x)
205 #define	ARP_STATADD(x, v)	_NET_STATADD(arpstat_percpu, x, v)
206 
207 /* revarp state */
208 static struct	in_addr myip, srv_ip;
209 static int	myip_initialized = 0;
210 static int	revarp_in_progress = 0;
211 static struct	ifnet *myip_ifp = NULL;
212 
213 static int arp_drainwanted;
214 
215 static int log_movements = 1;
216 static int log_permanent_modify = 1;
217 static int log_wrong_iface = 1;
218 static int log_unknown_network = 1;
219 
220 /*
221  * this should be elsewhere.
222  */
223 
224 #define	LLA_ADDRSTRLEN	(16 * 3)
225 
226 static char *
227 lla_snprintf(char *, u_int8_t *, int);
228 
229 static char *
230 lla_snprintf(char *dst, u_int8_t *adrp, int len)
231 {
232 	int i;
233 	char *p;
234 
235 	p = dst;
236 
237 	*p++ = hexdigits[(*adrp) >> 4];
238 	*p++ = hexdigits[(*adrp++) & 0xf];
239 
240 	for (i = 1; i < len && i < 16; i++) {
241 		*p++ = ':';
242 		*p++ = hexdigits[(*adrp) >> 4];
243 		*p++ = hexdigits[(*adrp++) & 0xf];
244 	}
245 
246 	*p = 0;
247 	return dst;
248 }
249 
250 DOMAIN_DEFINE(arpdomain);	/* forward declare and add to link set */
251 
252 static void
253 arp_fasttimo(void)
254 {
255 	if (arp_drainwanted) {
256 		arp_drain();
257 		arp_drainwanted = 0;
258 	}
259 }
260 
261 const struct protosw arpsw[] = {
262 	{ .pr_type = 0,
263 	  .pr_domain = &arpdomain,
264 	  .pr_protocol = 0,
265 	  .pr_flags = 0,
266 	  .pr_input = 0,
267 	  .pr_ctlinput = 0,
268 	  .pr_ctloutput = 0,
269 	  .pr_usrreqs = 0,
270 	  .pr_init = arp_init,
271 	  .pr_fasttimo = arp_fasttimo,
272 	  .pr_slowtimo = 0,
273 	  .pr_drain = arp_drainstub,
274 	}
275 };
276 
277 struct domain arpdomain = {
278 	.dom_family = PF_ARP,
279 	.dom_name = "arp",
280 	.dom_protosw = arpsw,
281 	.dom_protoswNPROTOSW = &arpsw[__arraycount(arpsw)],
282 };
283 
284 static void sysctl_net_inet_arp_setup(struct sysctllog **);
285 
286 void
287 arp_init(void)
288 {
289 
290 	sysctl_net_inet_arp_setup(NULL);
291 	arpstat_percpu = percpu_alloc(sizeof(uint64_t) * ARP_NSTATS);
292 	IFQ_LOCK_INIT(&arpintrq);
293 }
294 
295 static void
296 arp_drainstub(void)
297 {
298 	arp_drainwanted = 1;
299 }
300 
301 /*
302  * ARP protocol drain routine.  Called when memory is in short supply.
303  * Called at splvm();  don't acquire softnet_lock as can be called from
304  * hardware interrupt handlers.
305  */
306 void
307 arp_drain(void)
308 {
309 
310 	lltable_drain(AF_INET);
311 }
312 
313 static void
314 arptimer(void *arg)
315 {
316 	struct llentry *lle = arg;
317 	struct ifnet *ifp;
318 
319 	if (lle == NULL)
320 		return;
321 
322 	if (lle->la_flags & LLE_STATIC)
323 		return;
324 
325 	LLE_WLOCK(lle);
326 	if (callout_pending(&lle->la_timer)) {
327 		/*
328 		 * Here we are a bit odd here in the treatment of
329 		 * active/pending. If the pending bit is set, it got
330 		 * rescheduled before I ran. The active
331 		 * bit we ignore, since if it was stopped
332 		 * in ll_tablefree() and was currently running
333 		 * it would have return 0 so the code would
334 		 * not have deleted it since the callout could
335 		 * not be stopped so we want to go through
336 		 * with the delete here now. If the callout
337 		 * was restarted, the pending bit will be back on and
338 		 * we just want to bail since the callout_reset would
339 		 * return 1 and our reference would have been removed
340 		 * by arpresolve() below.
341 		 */
342 		LLE_WUNLOCK(lle);
343 		return;
344 	}
345 	ifp = lle->lle_tbl->llt_ifp;
346 
347 	callout_stop(&lle->la_timer);
348 
349 	/* XXX: LOR avoidance. We still have ref on lle. */
350 	LLE_WUNLOCK(lle);
351 
352 	IF_AFDATA_LOCK(ifp);
353 	LLE_WLOCK(lle);
354 
355 	/* Guard against race with other llentry_free(). */
356 	if (lle->la_flags & LLE_LINKED) {
357 		size_t pkts_dropped;
358 
359 		LLE_REMREF(lle);
360 		pkts_dropped = llentry_free(lle);
361 		ARP_STATADD(ARP_STAT_DFRDROPPED, pkts_dropped);
362 		ARP_STATADD(ARP_STAT_DFRTOTAL, pkts_dropped);
363 	} else {
364 		LLE_FREE_LOCKED(lle);
365 	}
366 
367 	IF_AFDATA_UNLOCK(ifp);
368 }
369 
370 static void
371 arp_settimer(struct llentry *la, int sec)
372 {
373 
374 	LLE_WLOCK_ASSERT(la);
375 	LLE_ADDREF(la);
376 	callout_reset(&la->la_timer, hz * sec, arptimer, la);
377 }
378 
379 /*
380  * We set the gateway for RTF_CLONING routes to a "prototype"
381  * link-layer sockaddr whose interface type (if_type) and interface
382  * index (if_index) fields are prepared.
383  */
384 static struct sockaddr *
385 arp_setgate(struct rtentry *rt, struct sockaddr *gate,
386     const struct sockaddr *netmask)
387 {
388 	const struct ifnet *ifp = rt->rt_ifp;
389 	uint8_t namelen = strlen(ifp->if_xname);
390 	uint8_t addrlen = ifp->if_addrlen;
391 
392 	/*
393 	 * XXX: If this is a manually added route to interface
394 	 * such as older version of routed or gated might provide,
395 	 * restore cloning bit.
396 	 */
397 	if ((rt->rt_flags & RTF_HOST) == 0 && netmask != NULL &&
398 	    satocsin(netmask)->sin_addr.s_addr != 0xffffffff)
399 		rt->rt_flags |= RTF_CONNECTED;
400 
401 	if ((rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL))) {
402 		union {
403 			struct sockaddr sa;
404 			struct sockaddr_storage ss;
405 			struct sockaddr_dl sdl;
406 		} u;
407 		/*
408 		 * Case 1: This route should come from a route to iface.
409 		 */
410 		sockaddr_dl_init(&u.sdl, sizeof(u.ss),
411 		    ifp->if_index, ifp->if_type, NULL, namelen, NULL, addrlen);
412 		rt_setgate(rt, &u.sa);
413 		gate = rt->rt_gateway;
414 	}
415 	return gate;
416 }
417 
418 static void
419 arp_init_llentry(struct ifnet *ifp, struct llentry *lle)
420 {
421 
422 	switch (ifp->if_type) {
423 #if NTOKEN > 0
424 	case IFT_ISO88025:
425 		lle->la_opaque = kmem_intr_alloc(sizeof(struct token_rif),
426 		    KM_NOSLEEP);
427 		lle->lle_ll_free = arp_free_llentry_tokenring;
428 		break;
429 #endif
430 	}
431 }
432 
433 #if NTOKEN > 0
434 static void
435 arp_free_llentry_tokenring(struct llentry *lle)
436 {
437 
438 	kmem_intr_free(lle->la_opaque, sizeof(struct token_rif));
439 }
440 #endif
441 
442 /*
443  * Parallel to llc_rtrequest.
444  */
445 void
446 arp_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
447 {
448 	struct sockaddr *gate = rt->rt_gateway;
449 	struct in_ifaddr *ia;
450 	struct ifaddr *ifa;
451 	struct ifnet *ifp = rt->rt_ifp;
452 	int bound;
453 	int s;
454 
455 	if (req == RTM_LLINFO_UPD) {
456 		if ((ifa = info->rti_ifa) != NULL)
457 			arpannounce1(ifa);
458 		return;
459 	}
460 
461 	if ((rt->rt_flags & RTF_GATEWAY) != 0) {
462 		if (req != RTM_ADD)
463 			return;
464 
465 		/*
466 		 * linklayers with particular link MTU limitation.
467 		 */
468 		switch(ifp->if_type) {
469 #if NFDDI > 0
470 		case IFT_FDDI:
471 			if (ifp->if_mtu > FDDIIPMTU)
472 				rt->rt_rmx.rmx_mtu = FDDIIPMTU;
473 			break;
474 #endif
475 #if NARCNET > 0
476 		case IFT_ARCNET:
477 		    {
478 			int arcipifmtu;
479 
480 			if (ifp->if_flags & IFF_LINK0)
481 				arcipifmtu = arc_ipmtu;
482 			else
483 				arcipifmtu = ARCMTU;
484 			if (ifp->if_mtu > arcipifmtu)
485 				rt->rt_rmx.rmx_mtu = arcipifmtu;
486 			break;
487 		    }
488 #endif
489 		}
490 		return;
491 	}
492 
493 	switch (req) {
494 	case RTM_SETGATE:
495 		gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
496 		break;
497 	case RTM_ADD:
498 		gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
499 		if (gate == NULL) {
500 			log(LOG_ERR, "%s: arp_setgate failed\n", __func__);
501 			break;
502 		}
503 		if ((rt->rt_flags & RTF_CONNECTED) ||
504 		    (rt->rt_flags & RTF_LOCAL)) {
505 			/*
506 			 * Give this route an expiration time, even though
507 			 * it's a "permanent" route, so that routes cloned
508 			 * from it do not need their expiration time set.
509 			 */
510 			KASSERT(time_uptime != 0);
511 			rt->rt_expire = time_uptime;
512 			/*
513 			 * linklayers with particular link MTU limitation.
514 			 */
515 			switch (ifp->if_type) {
516 #if NFDDI > 0
517 			case IFT_FDDI:
518 				if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
519 				    (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
520 				     (rt->rt_rmx.rmx_mtu == 0 &&
521 				      ifp->if_mtu > FDDIIPMTU)))
522 					rt->rt_rmx.rmx_mtu = FDDIIPMTU;
523 				break;
524 #endif
525 #if NARCNET > 0
526 			case IFT_ARCNET:
527 			    {
528 				int arcipifmtu;
529 				if (ifp->if_flags & IFF_LINK0)
530 					arcipifmtu = arc_ipmtu;
531 				else
532 					arcipifmtu = ARCMTU;
533 
534 				if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
535 				    (rt->rt_rmx.rmx_mtu > arcipifmtu ||
536 				     (rt->rt_rmx.rmx_mtu == 0 &&
537 				      ifp->if_mtu > arcipifmtu)))
538 					rt->rt_rmx.rmx_mtu = arcipifmtu;
539 				break;
540 			    }
541 #endif
542 			}
543 			if (rt->rt_flags & RTF_CONNECTED)
544 				break;
545 		}
546 
547 		bound = curlwp_bind();
548 		/* Announce a new entry if requested. */
549 		if (rt->rt_flags & RTF_ANNOUNCE) {
550 			struct psref psref;
551 			ia = in_get_ia_on_iface_psref(
552 			    satocsin(rt_getkey(rt))->sin_addr, ifp, &psref);
553 			if (ia != NULL) {
554 				arpannounce(ifp, &ia->ia_ifa,
555 				    CLLADDR(satocsdl(gate)));
556 				ia4_release(ia, &psref);
557 			}
558 		}
559 
560 		if (gate->sa_family != AF_LINK ||
561 		    gate->sa_len < sockaddr_dl_measure(0, ifp->if_addrlen)) {
562 			log(LOG_DEBUG, "%s: bad gateway value\n", __func__);
563 			goto out;
564 		}
565 
566 		satosdl(gate)->sdl_type = ifp->if_type;
567 		satosdl(gate)->sdl_index = ifp->if_index;
568 
569 		/* If the route is for a broadcast address mark it as such.
570 		 * This way we can avoid an expensive call to in_broadcast()
571 		 * in ip_output() most of the time (because the route passed
572 		 * to ip_output() is almost always a host route). */
573 		if (rt->rt_flags & RTF_HOST &&
574 		    !(rt->rt_flags & RTF_BROADCAST) &&
575 		    in_broadcast(satocsin(rt_getkey(rt))->sin_addr, rt->rt_ifp))
576 			rt->rt_flags |= RTF_BROADCAST;
577 		/* There is little point in resolving the broadcast address */
578 		if (rt->rt_flags & RTF_BROADCAST)
579 			goto out;
580 
581 		/*
582 		 * When called from rt_ifa_addlocal, we cannot depend on that
583 		 * the address (rt_getkey(rt)) exits in the address list of the
584 		 * interface. So check RTF_LOCAL instead.
585 		 */
586 		if (rt->rt_flags & RTF_LOCAL) {
587 			rt->rt_expire = 0;
588 			if (useloopback) {
589 				rt->rt_ifp = lo0ifp;
590 				rt->rt_rmx.rmx_mtu = 0;
591 			}
592 			goto out;
593 		}
594 
595 		s = pserialize_read_enter();
596 		ia = in_get_ia_on_iface(satocsin(rt_getkey(rt))->sin_addr, ifp);
597 		if (ia == NULL) {
598 			pserialize_read_exit(s);
599 			goto out;
600 		}
601 
602 		rt->rt_expire = 0;
603 		if (useloopback) {
604 			rt->rt_ifp = lo0ifp;
605 			rt->rt_rmx.rmx_mtu = 0;
606 		}
607 		rt->rt_flags |= RTF_LOCAL;
608 		/*
609 		 * make sure to set rt->rt_ifa to the interface
610 		 * address we are using, otherwise we will have trouble
611 		 * with source address selection.
612 		 */
613 		ifa = &ia->ia_ifa;
614 		if (ifa != rt->rt_ifa)
615 			/* Assume it doesn't sleep */
616 			rt_replace_ifa(rt, ifa);
617 		pserialize_read_exit(s);
618 	out:
619 		curlwp_bindx(bound);
620 		break;
621 	}
622 }
623 
624 /*
625  * Broadcast an ARP request. Caller specifies:
626  *	- arp header source ip address
627  *	- arp header target ip address
628  *	- arp header source ethernet address
629  */
630 static void
631 arprequest(struct ifnet *ifp,
632     const struct in_addr *sip, const struct in_addr *tip,
633     const u_int8_t *enaddr)
634 {
635 	struct mbuf *m;
636 	struct arphdr *ah;
637 	struct sockaddr sa;
638 	uint64_t *arps;
639 
640 	KASSERT(sip != NULL);
641 	KASSERT(tip != NULL);
642 	KASSERT(enaddr != NULL);
643 
644 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
645 		return;
646 	MCLAIM(m, &arpdomain.dom_mowner);
647 	switch (ifp->if_type) {
648 	case IFT_IEEE1394:
649 		m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
650 		    ifp->if_addrlen;
651 		break;
652 	default:
653 		m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
654 		    2 * ifp->if_addrlen;
655 		break;
656 	}
657 	m->m_pkthdr.len = m->m_len;
658 	MH_ALIGN(m, m->m_len);
659 	ah = mtod(m, struct arphdr *);
660 	memset(ah, 0, m->m_len);
661 	switch (ifp->if_type) {
662 	case IFT_IEEE1394:	/* RFC2734 */
663 		/* fill it now for ar_tpa computation */
664 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
665 		break;
666 	default:
667 		/* ifp->if_output will fill ar_hrd */
668 		break;
669 	}
670 	ah->ar_pro = htons(ETHERTYPE_IP);
671 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
672 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
673 	ah->ar_op = htons(ARPOP_REQUEST);
674 	memcpy(ar_sha(ah), enaddr, ah->ar_hln);
675 	memcpy(ar_spa(ah), sip, ah->ar_pln);
676 	memcpy(ar_tpa(ah), tip, ah->ar_pln);
677 	sa.sa_family = AF_ARP;
678 	sa.sa_len = 2;
679 	m->m_flags |= M_BCAST;
680 	arps = ARP_STAT_GETREF();
681 	arps[ARP_STAT_SNDTOTAL]++;
682 	arps[ARP_STAT_SENDREQUEST]++;
683 	ARP_STAT_PUTREF();
684 	if_output_lock(ifp, ifp, m, &sa, NULL);
685 }
686 
687 void
688 arpannounce(struct ifnet *ifp, struct ifaddr *ifa, const uint8_t *enaddr)
689 {
690 	struct in_ifaddr *ia = ifatoia(ifa);
691 	struct in_addr *ip = &IA_SIN(ifa)->sin_addr;
692 
693 	if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) {
694 		ARPLOG(LOG_DEBUG, "%s not ready\n", ARPLOGADDR(*ip));
695 		return;
696 	}
697 	arprequest(ifp, ip, ip, enaddr);
698 }
699 
700 static void
701 arpannounce1(struct ifaddr *ifa)
702 {
703 
704 	arpannounce(ifa->ifa_ifp, ifa, CLLADDR(ifa->ifa_ifp->if_sadl));
705 }
706 
707 /*
708  * Resolve an IP address into an ethernet address.  If success,
709  * desten is filled in.  If there is no entry in arptab,
710  * set one up and broadcast a request for the IP address.
711  * Hold onto this mbuf and resend it once the address
712  * is finally resolved.  A return value of 0 indicates
713  * that desten has been filled in and the packet should be sent
714  * normally; a return value of EWOULDBLOCK indicates that the packet has been
715  * held pending resolution.
716  * Any other value indicates an error.
717  */
718 int
719 arpresolve(struct ifnet *ifp, const struct rtentry *rt, struct mbuf *m,
720     const struct sockaddr *dst, void *desten, size_t destlen)
721 {
722 	struct llentry *la;
723 	const char *create_lookup;
724 	bool renew;
725 	int error;
726 
727 	KASSERT(m != NULL);
728 
729 	la = arplookup(ifp, m, NULL, dst, 0);
730 	if (la == NULL)
731 		goto notfound;
732 
733 	if ((la->la_flags & LLE_VALID) &&
734 	    ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
735 		KASSERT(destlen >= ifp->if_addrlen);
736 		memcpy(desten, &la->ll_addr, ifp->if_addrlen);
737 		LLE_RUNLOCK(la);
738 		return 0;
739 	}
740 
741 notfound:
742 #ifdef IFF_STATICARP /* FreeBSD */
743 #define _IFF_NOARP (IFF_NOARP | IFF_STATICARP)
744 #else
745 #define _IFF_NOARP IFF_NOARP
746 #endif
747 	if (ifp->if_flags & _IFF_NOARP) {
748 		if (la != NULL)
749 			LLE_RUNLOCK(la);
750 		error = ENOTSUP;
751 		goto bad;
752 	}
753 #undef _IFF_NOARP
754 	if (la == NULL) {
755 		create_lookup = "create";
756 		IF_AFDATA_WLOCK(ifp);
757 		la = lla_create(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
758 		IF_AFDATA_WUNLOCK(ifp);
759 		if (la == NULL)
760 			ARP_STATINC(ARP_STAT_ALLOCFAIL);
761 		else
762 			arp_init_llentry(ifp, la);
763 	} else if (LLE_TRY_UPGRADE(la) == 0) {
764 		create_lookup = "lookup";
765 		LLE_RUNLOCK(la);
766 		IF_AFDATA_RLOCK(ifp);
767 		la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
768 		IF_AFDATA_RUNLOCK(ifp);
769 	}
770 
771 	error = EINVAL;
772 	if (la == NULL) {
773 		log(LOG_DEBUG,
774 		    "%s: failed to %s llentry for %s on %s\n",
775 		    __func__, create_lookup, inet_ntoa(satocsin(dst)->sin_addr),
776 		    ifp->if_xname);
777 		goto bad;
778 	}
779 
780 	if ((la->la_flags & LLE_VALID) &&
781 	    ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime))
782 	{
783 		KASSERT(destlen >= ifp->if_addrlen);
784 		memcpy(desten, &la->ll_addr, ifp->if_addrlen);
785 		renew = false;
786 		/*
787 		 * If entry has an expiry time and it is approaching,
788 		 * see if we need to send an ARP request within this
789 		 * arpt_down interval.
790 		 */
791 		if (!(la->la_flags & LLE_STATIC) &&
792 		    time_uptime + la->la_preempt > la->la_expire)
793 		{
794 			renew = true;
795 			la->la_preempt--;
796 		}
797 
798 		LLE_WUNLOCK(la);
799 
800 		if (renew) {
801 			const u_int8_t *enaddr =
802 #if NCARP > 0
803 			    (ifp->if_type == IFT_CARP) ?
804 			    CLLADDR(ifp->if_sadl):
805 #endif
806 			    CLLADDR(ifp->if_sadl);
807 			arprequest(ifp,
808 			    &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
809 			    &satocsin(dst)->sin_addr, enaddr);
810 		}
811 
812 		return 0;
813 	}
814 
815 	if (la->la_flags & LLE_STATIC) {   /* should not happen! */
816 		LLE_RUNLOCK(la);
817 		log(LOG_DEBUG, "%s: ouch, empty static llinfo for %s\n",
818 		    __func__, inet_ntoa(satocsin(dst)->sin_addr));
819 		error = EINVAL;
820 		goto bad;
821 	}
822 
823 	renew = (la->la_asked == 0 || la->la_expire != time_uptime);
824 
825 	/*
826 	 * There is an arptab entry, but no ethernet address
827 	 * response yet.  Add the mbuf to the list, dropping
828 	 * the oldest packet if we have exceeded the system
829 	 * setting.
830 	 */
831 	LLE_WLOCK_ASSERT(la);
832 	if (la->la_numheld >= arp_maxhold) {
833 		if (la->la_hold != NULL) {
834 			struct mbuf *next = la->la_hold->m_nextpkt;
835 			m_freem(la->la_hold);
836 			la->la_hold = next;
837 			la->la_numheld--;
838 			ARP_STATINC(ARP_STAT_DFRDROPPED);
839 			ARP_STATINC(ARP_STAT_DFRTOTAL);
840 		}
841 	}
842 	if (la->la_hold != NULL) {
843 		struct mbuf *curr = la->la_hold;
844 		while (curr->m_nextpkt != NULL)
845 			curr = curr->m_nextpkt;
846 		curr->m_nextpkt = m;
847 	} else
848 		la->la_hold = m;
849 	la->la_numheld++;
850 	if (!renew)
851 		LLE_DOWNGRADE(la);
852 
853 	/*
854 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
855 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
856 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
857 	 * ARP request, but not faster than one request per second.
858 	 */
859 	if (la->la_asked < arp_maxtries)
860 		error = EWOULDBLOCK;	/* First request. */
861 	else
862 		error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ?
863 		    EHOSTUNREACH : EHOSTDOWN;
864 
865 	if (renew) {
866 		const u_int8_t *enaddr =
867 #if NCARP > 0
868 		    (rt != NULL && rt->rt_ifp->if_type == IFT_CARP) ?
869 		    CLLADDR(rt->rt_ifp->if_sadl):
870 #endif
871 		    CLLADDR(ifp->if_sadl);
872 		la->la_expire = time_uptime;
873 		arp_settimer(la, arpt_down);
874 		la->la_asked++;
875 		LLE_WUNLOCK(la);
876 
877 		if (rt != NULL) {
878 			arprequest(ifp, &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
879 			    &satocsin(dst)->sin_addr, enaddr);
880 		} else {
881 			struct sockaddr_in sin;
882 			struct rtentry *_rt;
883 
884 			sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
885 
886 			/* XXX */
887 			_rt = rtalloc1((struct sockaddr *)&sin, 0);
888 			if (_rt == NULL)
889 				goto bad;
890 			arprequest(ifp,
891 			    &satocsin(_rt->rt_ifa->ifa_addr)->sin_addr,
892 			    &satocsin(dst)->sin_addr, enaddr);
893 			rt_unref(_rt);
894 		}
895 		return error;
896 	}
897 
898 	LLE_RUNLOCK(la);
899 	return error;
900 
901 bad:
902 	m_freem(m);
903 	return error;
904 }
905 
906 /*
907  * Common length and type checks are done here,
908  * then the protocol-specific routine is called.
909  */
910 void
911 arpintr(void)
912 {
913 	struct mbuf *m;
914 	struct arphdr *ar;
915 	int s;
916 	int arplen;
917 
918 #ifndef NET_MPSAFE
919 	mutex_enter(softnet_lock);
920 	KERNEL_LOCK(1, NULL);
921 #endif
922 	for (;;) {
923 		struct ifnet *rcvif;
924 
925 		IFQ_LOCK(&arpintrq);
926 		IF_DEQUEUE(&arpintrq, m);
927 		IFQ_UNLOCK(&arpintrq);
928 		if (m == NULL)
929 			goto out;
930 		if ((m->m_flags & M_PKTHDR) == 0)
931 			panic("arpintr");
932 
933 		MCLAIM(m, &arpdomain.dom_mowner);
934 		ARP_STATINC(ARP_STAT_RCVTOTAL);
935 
936 		/*
937 		 * First, make sure we have at least struct arphdr.
938 		 */
939 		if (m->m_len < sizeof(struct arphdr) ||
940 		    (ar = mtod(m, struct arphdr *)) == NULL)
941 			goto badlen;
942 
943 		rcvif = m_get_rcvif(m, &s);
944 		if (__predict_false(rcvif == NULL)) {
945 			ARP_STATINC(ARP_STAT_RCVNOINT);
946 			goto free;
947 		}
948 		switch (rcvif->if_type) {
949 		case IFT_IEEE1394:
950 			arplen = sizeof(struct arphdr) +
951 			    ar->ar_hln + 2 * ar->ar_pln;
952 			break;
953 		default:
954 			arplen = sizeof(struct arphdr) +
955 			    2 * ar->ar_hln + 2 * ar->ar_pln;
956 			break;
957 		}
958 		m_put_rcvif(rcvif, &s);
959 
960 		if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
961 		    m->m_len >= arplen)
962 			switch (ntohs(ar->ar_pro)) {
963 			case ETHERTYPE_IP:
964 			case ETHERTYPE_IPTRAILERS:
965 				in_arpinput(m);
966 				continue;
967 			default:
968 				ARP_STATINC(ARP_STAT_RCVBADPROTO);
969 			}
970 		else {
971 badlen:
972 			ARP_STATINC(ARP_STAT_RCVBADLEN);
973 		}
974 free:
975 		m_freem(m);
976 	}
977 out:
978 #ifndef NET_MPSAFE
979 	KERNEL_UNLOCK_ONE(NULL);
980 	mutex_exit(softnet_lock);
981 #else
982 	return; /* XXX gcc */
983 #endif
984 }
985 
986 /*
987  * ARP for Internet protocols on 10 Mb/s Ethernet. Algorithm is that given in
988  * RFC 826. In addition, a sanity check is performed on the sender protocol
989  * address, to catch impersonators.
990  *
991  * We no longer handle negotiations for use of trailer protocol: formerly, ARP
992  * replied for protocol type ETHERTYPE_TRAIL sent along with IP replies if we
993  * wanted trailers sent to us, and also sent them in response to IP replies.
994  * This allowed either end to announce the desire to receive trailer packets.
995  *
996  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, but
997  * formerly didn't normally send requests.
998  */
999 static void
1000 in_arpinput(struct mbuf *m)
1001 {
1002 	struct arphdr *ah;
1003 	struct ifnet *ifp, *rcvif = NULL;
1004 	struct llentry *la = NULL;
1005 	struct in_ifaddr *ia = NULL;
1006 #if NBRIDGE > 0
1007 	struct in_ifaddr *bridge_ia = NULL;
1008 #endif
1009 #if NCARP > 0
1010 	u_int32_t count = 0, index = 0;
1011 #endif
1012 	struct sockaddr sa;
1013 	struct in_addr isaddr, itaddr, myaddr;
1014 	int op;
1015 	void *tha;
1016 	uint64_t *arps;
1017 	struct psref psref, psref_ia;
1018 	int s;
1019 	char llabuf[LLA_ADDRSTRLEN];
1020 	char ipbuf[INET_ADDRSTRLEN];
1021 
1022 	if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
1023 		goto out;
1024 	ah = mtod(m, struct arphdr *);
1025 	op = ntohs(ah->ar_op);
1026 
1027 	rcvif = ifp = m_get_rcvif_psref(m, &psref);
1028 	if (__predict_false(rcvif == NULL))
1029 		goto drop;
1030 
1031 	/*
1032 	 * Fix up ah->ar_hrd if necessary, before using ar_tha() or ar_tpa().
1033 	 * XXX check ar_hrd more strictly?
1034 	 */
1035 	switch (ifp->if_type) {
1036 	case IFT_IEEE1394:
1037 		if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
1038 			;
1039 		else {
1040 			/* XXX this is to make sure we compute ar_tha right */
1041 			ah->ar_hrd = htons(ARPHRD_IEEE1394);
1042 		}
1043 		break;
1044 	default:
1045 		break;
1046 	}
1047 
1048 	if (ah->ar_pln != sizeof(struct in_addr))
1049 		goto drop;
1050 
1051 	memcpy(&isaddr, ar_spa(ah), sizeof(isaddr));
1052 	memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr));
1053 
1054 	if (m->m_flags & (M_BCAST|M_MCAST))
1055 		ARP_STATINC(ARP_STAT_RCVMCAST);
1056 
1057 	/*
1058 	 * Search for a matching interface address
1059 	 * or any address on the interface to use
1060 	 * as a dummy address in the rest of this function
1061 	 */
1062 	s = pserialize_read_enter();
1063 	IN_ADDRHASH_READER_FOREACH(ia, itaddr.s_addr) {
1064 		if (!in_hosteq(ia->ia_addr.sin_addr, itaddr))
1065 			continue;
1066 #if NCARP > 0
1067 		if (ia->ia_ifp->if_type == IFT_CARP &&
1068 		    ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
1069 		    (IFF_UP|IFF_RUNNING))) {
1070 			index++;
1071 			/* XXX: ar_hln? */
1072 			if (ia->ia_ifp == rcvif && (ah->ar_hln >= 6) &&
1073 			    carp_iamatch(ia, ar_sha(ah),
1074 			    &count, index)) {
1075 				break;
1076 			}
1077 		} else
1078 #endif
1079 		if (ia->ia_ifp == rcvif)
1080 			break;
1081 #if NBRIDGE > 0
1082 		/*
1083 		 * If the interface we received the packet on
1084 		 * is part of a bridge, check to see if we need
1085 		 * to "bridge" the packet to ourselves at this
1086 		 * layer.  Note we still prefer a perfect match,
1087 		 * but allow this weaker match if necessary.
1088 		 */
1089 		if (rcvif->if_bridge != NULL &&
1090 		    rcvif->if_bridge == ia->ia_ifp->if_bridge)
1091 			bridge_ia = ia;
1092 #endif /* NBRIDGE > 0 */
1093 	}
1094 
1095 #if NBRIDGE > 0
1096 	if (ia == NULL && bridge_ia != NULL) {
1097 		ia = bridge_ia;
1098 		m_put_rcvif_psref(rcvif, &psref);
1099 		rcvif = NULL;
1100 		/* FIXME */
1101 		ifp = bridge_ia->ia_ifp;
1102 	}
1103 #endif
1104 	if (ia != NULL)
1105 		ia4_acquire(ia, &psref_ia);
1106 	pserialize_read_exit(s);
1107 
1108 	if (ah->ar_hln != ifp->if_addrlen) {
1109 		ARP_STATINC(ARP_STAT_RCVBADLEN);
1110 		log(LOG_WARNING,
1111 		    "arp from %s: addr len: new %d, i/f %d (ignored)\n",
1112 		    in_fmtaddr(ipbuf, isaddr), ah->ar_hln, ifp->if_addrlen);
1113 		goto out;
1114 	}
1115 
1116 	if (ia == NULL) {
1117 		ia = in_get_ia_on_iface_psref(isaddr, rcvif, &psref_ia);
1118 		if (ia == NULL) {
1119 			ia = in_get_ia_from_ifp_psref(ifp, &psref_ia);
1120 			if (ia == NULL) {
1121 				ARP_STATINC(ARP_STAT_RCVNOINT);
1122 				goto out;
1123 			}
1124 		}
1125 	}
1126 
1127 	myaddr = ia->ia_addr.sin_addr;
1128 
1129 	/* XXX checks for bridge case? */
1130 	if (!memcmp(ar_sha(ah), CLLADDR(ifp->if_sadl), ifp->if_addrlen)) {
1131 		ARP_STATINC(ARP_STAT_RCVLOCALSHA);
1132 		goto out;	/* it's from me, ignore it. */
1133 	}
1134 
1135 	/* XXX checks for bridge case? */
1136 	if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1137 		ARP_STATINC(ARP_STAT_RCVBCASTSHA);
1138 		log(LOG_ERR,
1139 		    "%s: arp: link address is broadcast for IP address %s!\n",
1140 		    ifp->if_xname, in_fmtaddr(ipbuf, isaddr));
1141 		goto out;
1142 	}
1143 
1144 	/*
1145 	 * If the source IP address is zero, this is an RFC 5227 ARP probe
1146 	 */
1147 	if (in_nullhost(isaddr))
1148 		ARP_STATINC(ARP_STAT_RCVZEROSPA);
1149 	else if (in_hosteq(isaddr, myaddr))
1150 		ARP_STATINC(ARP_STAT_RCVLOCALSPA);
1151 
1152 	/*
1153 	 * If the target IP address is zero, ignore the packet.
1154 	 * This prevents the code below from trying to answer
1155 	 * when we are using IP address zero (booting).
1156 	 */
1157 	if (in_nullhost(itaddr)) {
1158 		ARP_STATINC(ARP_STAT_RCVZEROTPA);
1159 		goto out;
1160 	}
1161 
1162 	/* DAD check, RFC 5227 */
1163 	if (in_hosteq(isaddr, myaddr) ||
1164 	    (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr)
1165 	    && ISSET(m->m_flags, M_BCAST))) /* Allow Unicast Poll, RFC 1122 */
1166 	{
1167 		arp_dad_duplicated((struct ifaddr *)ia,
1168 		    lla_snprintf(llabuf, ar_sha(ah), ah->ar_hln));
1169 		goto out;
1170 	}
1171 
1172 	if (in_nullhost(isaddr))
1173 		goto reply;
1174 
1175 	if (in_hosteq(itaddr, myaddr))
1176 		la = arpcreate(ifp, m, &isaddr, NULL, 1);
1177 	else
1178 		la = arplookup(ifp, m, &isaddr, NULL, 1);
1179 	if (la == NULL)
1180 		goto reply;
1181 
1182 	if ((la->la_flags & LLE_VALID) &&
1183 	    memcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
1184 		if (la->la_flags & LLE_STATIC) {
1185 			ARP_STATINC(ARP_STAT_RCVOVERPERM);
1186 			if (!log_permanent_modify)
1187 				goto out;
1188 			log(LOG_INFO,
1189 			    "%s tried to overwrite permanent arp info"
1190 			    " for %s\n",
1191 			    lla_snprintf(llabuf, ar_sha(ah), ah->ar_hln),
1192 			    in_fmtaddr(ipbuf, isaddr));
1193 			goto out;
1194 		} else if (la->lle_tbl->llt_ifp != ifp) {
1195 			/* XXX should not happen? */
1196 			ARP_STATINC(ARP_STAT_RCVOVERINT);
1197 			if (!log_wrong_iface)
1198 				goto out;
1199 			log(LOG_INFO,
1200 			    "%s on %s tried to overwrite "
1201 			    "arp info for %s on %s\n",
1202 			    lla_snprintf(llabuf, ar_sha(ah), ah->ar_hln),
1203 			    ifp->if_xname, in_fmtaddr(ipbuf, isaddr),
1204 			    la->lle_tbl->llt_ifp->if_xname);
1205 				goto out;
1206 		} else {
1207 			ARP_STATINC(ARP_STAT_RCVOVER);
1208 			if (log_movements)
1209 				log(LOG_INFO, "arp info overwritten "
1210 				    "for %s by %s\n",
1211 				    in_fmtaddr(ipbuf, isaddr),
1212 				    lla_snprintf(llabuf, ar_sha(ah),
1213 				    ah->ar_hln));
1214 		}
1215 	}
1216 
1217 	/* XXX llentry should have addrlen? */
1218 #if 0
1219 	/*
1220 	 * sanity check for the address length.
1221 	 * XXX this does not work for protocols with variable address
1222 	 * length. -is
1223 	 */
1224 	if (sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
1225 		ARP_STATINC(ARP_STAT_RCVLENCHG);
1226 		log(LOG_WARNING,
1227 		    "arp from %s: new addr len %d, was %d\n",
1228 		    in_fmtaddr(ipbuf, isaddr), ah->ar_hln, sdl->sdl_alen);
1229 	}
1230 #endif
1231 
1232 #if NTOKEN > 0
1233 	/*
1234 	 * XXX uses m_data and assumes the complete answer including
1235 	 * XXX token-ring headers is in the same buf
1236 	 */
1237 	if (ifp->if_type == IFT_ISO88025) {
1238 		struct token_header *trh;
1239 
1240 		trh = (struct token_header *)M_TRHSTART(m);
1241 		if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
1242 			struct token_rif *rif;
1243 			size_t riflen;
1244 
1245 			rif = TOKEN_RIF(trh);
1246 			riflen = (ntohs(rif->tr_rcf) &
1247 			    TOKEN_RCF_LEN_MASK) >> 8;
1248 
1249 			if (riflen > 2 &&
1250 			    riflen < sizeof(struct token_rif) &&
1251 			    (riflen & 1) == 0) {
1252 				rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
1253 				rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
1254 				memcpy(TOKEN_RIF_LLE(la), rif, riflen);
1255 			}
1256 		}
1257 	}
1258 #endif /* NTOKEN > 0 */
1259 
1260 	KASSERT(sizeof(la->ll_addr) >= ifp->if_addrlen);
1261 	memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
1262 	la->la_flags |= LLE_VALID;
1263 	if ((la->la_flags & LLE_STATIC) == 0) {
1264 		la->la_expire = time_uptime + arpt_keep;
1265 		arp_settimer(la, arpt_keep);
1266 	}
1267 	la->la_asked = 0;
1268 	/* rt->rt_flags &= ~RTF_REJECT; */
1269 
1270 	if (la->la_hold != NULL) {
1271 		int n = la->la_numheld;
1272 		struct mbuf *m_hold, *m_hold_next;
1273 		struct sockaddr_in sin;
1274 
1275 		sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
1276 
1277 		m_hold = la->la_hold;
1278 		la->la_hold = NULL;
1279 		la->la_numheld = 0;
1280 		/*
1281 		 * We have to unlock here because if_output would call
1282 		 * arpresolve
1283 		 */
1284 		LLE_WUNLOCK(la);
1285 		ARP_STATADD(ARP_STAT_DFRSENT, n);
1286 		ARP_STATADD(ARP_STAT_DFRTOTAL, n);
1287 		for (; m_hold != NULL; m_hold = m_hold_next) {
1288 			m_hold_next = m_hold->m_nextpkt;
1289 			m_hold->m_nextpkt = NULL;
1290 			if_output_lock(ifp, ifp, m_hold, sintosa(&sin), NULL);
1291 		}
1292 	} else
1293 		LLE_WUNLOCK(la);
1294 	la = NULL;
1295 
1296 reply:
1297 	if (la != NULL) {
1298 		LLE_WUNLOCK(la);
1299 		la = NULL;
1300 	}
1301 	if (op != ARPOP_REQUEST) {
1302 		if (op == ARPOP_REPLY)
1303 			ARP_STATINC(ARP_STAT_RCVREPLY);
1304 		goto out;
1305 	}
1306 	ARP_STATINC(ARP_STAT_RCVREQUEST);
1307 	if (in_hosteq(itaddr, myaddr)) {
1308 		/* If our address is unusable, don't reply */
1309 		if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
1310 			goto out;
1311 		/* I am the target */
1312 		tha = ar_tha(ah);
1313 		if (tha)
1314 			memcpy(tha, ar_sha(ah), ah->ar_hln);
1315 		memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1316 	} else {
1317 		/* Proxy ARP */
1318 		struct llentry *lle = NULL;
1319 		struct sockaddr_in sin;
1320 #if NCARP > 0
1321 		if (ifp->if_type == IFT_CARP) {
1322 			struct ifnet *_rcvif = m_get_rcvif(m, &s);
1323 			int iftype = 0;
1324 			if (__predict_true(_rcvif != NULL))
1325 				iftype = _rcvif->if_type;
1326 			m_put_rcvif(_rcvif, &s);
1327 			if (iftype != IFT_CARP)
1328 				goto out;
1329 		}
1330 #endif
1331 
1332 		tha = ar_tha(ah);
1333 
1334 		sockaddr_in_init(&sin, &itaddr, 0);
1335 
1336 		IF_AFDATA_RLOCK(ifp);
1337 		lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
1338 		IF_AFDATA_RUNLOCK(ifp);
1339 
1340 		if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
1341 			if (tha)
1342 				memcpy(tha, ar_sha(ah), ah->ar_hln);
1343 			memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
1344 			LLE_RUNLOCK(lle);
1345 		} else {
1346 			if (lle != NULL)
1347 				LLE_RUNLOCK(lle);
1348 			goto drop;
1349 		}
1350 	}
1351 	ia4_release(ia, &psref_ia);
1352 
1353 	memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1354 	memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1355 	ah->ar_op = htons(ARPOP_REPLY);
1356 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1357 	switch (ifp->if_type) {
1358 	case IFT_IEEE1394:
1359 		/*
1360 		 * ieee1394 arp reply is broadcast
1361 		 */
1362 		m->m_flags &= ~M_MCAST;
1363 		m->m_flags |= M_BCAST;
1364 		m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
1365 		break;
1366 	default:
1367 		m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
1368 		m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
1369 		break;
1370 	}
1371 	m->m_pkthdr.len = m->m_len;
1372 	sa.sa_family = AF_ARP;
1373 	sa.sa_len = 2;
1374 	arps = ARP_STAT_GETREF();
1375 	arps[ARP_STAT_SNDTOTAL]++;
1376 	arps[ARP_STAT_SNDREPLY]++;
1377 	ARP_STAT_PUTREF();
1378 	if_output_lock(ifp, ifp, m, &sa, NULL);
1379 	if (rcvif != NULL)
1380 		m_put_rcvif_psref(rcvif, &psref);
1381 	return;
1382 
1383 out:
1384 	if (la != NULL)
1385 		LLE_WUNLOCK(la);
1386 drop:
1387 	if (ia != NULL)
1388 		ia4_release(ia, &psref_ia);
1389 	if (rcvif != NULL)
1390 		m_put_rcvif_psref(rcvif, &psref);
1391 	m_freem(m);
1392 }
1393 
1394 /*
1395  * Lookup or a new address in arptab.
1396  */
1397 static struct llentry *
1398 arplookup(struct ifnet *ifp, struct mbuf *m, const struct in_addr *addr,
1399     const struct sockaddr *sa, int wlock)
1400 {
1401 	struct sockaddr_in sin;
1402 	struct llentry *la;
1403 	int flags = wlock ? LLE_EXCLUSIVE : 0;
1404 
1405 
1406 	if (sa == NULL) {
1407 		KASSERT(addr != NULL);
1408 		sockaddr_in_init(&sin, addr, 0);
1409 		sa = sintocsa(&sin);
1410 	}
1411 
1412 	IF_AFDATA_RLOCK(ifp);
1413 	la = lla_lookup(LLTABLE(ifp), flags, sa);
1414 	IF_AFDATA_RUNLOCK(ifp);
1415 
1416 	return la;
1417 }
1418 
1419 static struct llentry *
1420 arpcreate(struct ifnet *ifp, struct mbuf *m, const struct in_addr *addr,
1421     const struct sockaddr *sa, int wlock)
1422 {
1423 	struct sockaddr_in sin;
1424 	struct llentry *la;
1425 	int flags = wlock ? LLE_EXCLUSIVE : 0;
1426 
1427 	if (sa == NULL) {
1428 		KASSERT(addr != NULL);
1429 		sockaddr_in_init(&sin, addr, 0);
1430 		sa = sintocsa(&sin);
1431 	}
1432 
1433 	la = arplookup(ifp, m, addr, sa, wlock);
1434 
1435 	if (la == NULL) {
1436 		IF_AFDATA_WLOCK(ifp);
1437 		la = lla_create(LLTABLE(ifp), flags, sa);
1438 		IF_AFDATA_WUNLOCK(ifp);
1439 
1440 		if (la != NULL)
1441 			arp_init_llentry(ifp, la);
1442 	}
1443 
1444 	return la;
1445 }
1446 
1447 int
1448 arpioctl(u_long cmd, void *data)
1449 {
1450 
1451 	return EOPNOTSUPP;
1452 }
1453 
1454 void
1455 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1456 {
1457 	struct in_addr *ip;
1458 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1459 
1460 	/*
1461 	 * Warn the user if another station has this IP address,
1462 	 * but only if the interface IP address is not zero.
1463 	 */
1464 	ip = &IA_SIN(ifa)->sin_addr;
1465 	if (!in_nullhost(*ip) &&
1466 	    (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) == 0) {
1467 		struct llentry *lle;
1468 
1469 		/*
1470 		 * interface address is considered static entry
1471 		 * because the output of the arp utility shows
1472 		 * that L2 entry as permanent
1473 		 */
1474 		IF_AFDATA_WLOCK(ifp);
1475 		lle = lla_create(LLTABLE(ifp), (LLE_IFADDR | LLE_STATIC),
1476 				 (struct sockaddr *)IA_SIN(ifa));
1477 		IF_AFDATA_WUNLOCK(ifp);
1478 		if (lle == NULL)
1479 			log(LOG_INFO, "%s: cannot create arp entry for"
1480 			    " interface address\n", __func__);
1481 		else {
1482 			arp_init_llentry(ifp, lle);
1483 			LLE_RUNLOCK(lle);
1484 		}
1485 	}
1486 
1487 	ifa->ifa_rtrequest = arp_rtrequest;
1488 	ifa->ifa_flags |= RTF_CONNECTED;
1489 
1490 	/* ARP will handle DAD for this address. */
1491 	if (in_nullhost(*ip)) {
1492 		if (ia->ia_dad_stop != NULL)	/* safety */
1493 			ia->ia_dad_stop(ifa);
1494 		ia->ia_dad_start = NULL;
1495 		ia->ia_dad_stop = NULL;
1496 		ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1497 	} else {
1498 		ia->ia_dad_start = arp_dad_start;
1499 		ia->ia_dad_stop = arp_dad_stop;
1500 		if (ia->ia4_flags & IN_IFF_TRYTENTATIVE)
1501 			ia->ia4_flags |= IN_IFF_TENTATIVE;
1502 		else
1503 			arpannounce1(ifa);
1504 	}
1505 }
1506 
1507 TAILQ_HEAD(dadq_head, dadq);
1508 struct dadq {
1509 	TAILQ_ENTRY(dadq) dad_list;
1510 	struct ifaddr *dad_ifa;
1511 	int dad_count;		/* max ARP to send */
1512 	int dad_arp_tcount;	/* # of trials to send ARP */
1513 	int dad_arp_ocount;	/* ARP sent so far */
1514 	int dad_arp_announce;	/* max ARP announcements */
1515 	int dad_arp_acount;	/* # of announcements */
1516 	struct callout dad_timer_ch;
1517 };
1518 MALLOC_JUSTDEFINE(M_IPARP, "ARP DAD", "ARP DAD Structure");
1519 
1520 static struct dadq_head dadq;
1521 static int dad_init = 0;
1522 static int dad_maxtry = 15;     /* max # of *tries* to transmit DAD packet */
1523 static kmutex_t arp_dad_lock;
1524 
1525 static struct dadq *
1526 arp_dad_find(struct ifaddr *ifa)
1527 {
1528 	struct dadq *dp;
1529 
1530 	KASSERT(mutex_owned(&arp_dad_lock));
1531 
1532 	TAILQ_FOREACH(dp, &dadq, dad_list) {
1533 		if (dp->dad_ifa == ifa)
1534 			return dp;
1535 	}
1536 	return NULL;
1537 }
1538 
1539 static void
1540 arp_dad_starttimer(struct dadq *dp, int ticks)
1541 {
1542 
1543 	callout_reset(&dp->dad_timer_ch, ticks,
1544 	    (void (*)(void *))arp_dad_timer, (void *)dp->dad_ifa);
1545 }
1546 
1547 static void
1548 arp_dad_stoptimer(struct dadq *dp)
1549 {
1550 
1551 #ifdef NET_MPSAFE
1552 	callout_halt(&dp->dad_timer_ch, NULL);
1553 #else
1554 	callout_halt(&dp->dad_timer_ch, softnet_lock);
1555 #endif
1556 }
1557 
1558 static void
1559 arp_dad_output(struct dadq *dp, struct ifaddr *ifa)
1560 {
1561 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1562 	struct ifnet *ifp = ifa->ifa_ifp;
1563 	struct in_addr sip;
1564 
1565 	dp->dad_arp_tcount++;
1566 	if ((ifp->if_flags & IFF_UP) == 0)
1567 		return;
1568 	if ((ifp->if_flags & IFF_RUNNING) == 0)
1569 		return;
1570 
1571 	dp->dad_arp_tcount = 0;
1572 	dp->dad_arp_ocount++;
1573 
1574 	memset(&sip, 0, sizeof(sip));
1575 	arprequest(ifa->ifa_ifp, &sip, &ia->ia_addr.sin_addr,
1576 	    CLLADDR(ifa->ifa_ifp->if_sadl));
1577 }
1578 
1579 /*
1580  * Start Duplicate Address Detection (DAD) for specified interface address.
1581  */
1582 static void
1583 arp_dad_start(struct ifaddr *ifa)
1584 {
1585 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1586 	struct dadq *dp;
1587 	char ipbuf[INET_ADDRSTRLEN];
1588 
1589 	if (!dad_init) {
1590 		TAILQ_INIT(&dadq);
1591 		mutex_init(&arp_dad_lock, MUTEX_DEFAULT, IPL_NONE);
1592 		dad_init++;
1593 	}
1594 
1595 	/*
1596 	 * If we don't need DAD, don't do it.
1597 	 * - DAD is disabled (ip_dad_count == 0)
1598 	 */
1599 	if (!(ia->ia4_flags & IN_IFF_TENTATIVE)) {
1600 		log(LOG_DEBUG,
1601 		    "%s: called with non-tentative address %s(%s)\n", __func__,
1602 		    in_fmtaddr(ipbuf, ia->ia_addr.sin_addr),
1603 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1604 		return;
1605 	}
1606 	if (!ip_dad_count) {
1607 		ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1608 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1609 		arpannounce1(ifa);
1610 		return;
1611 	}
1612 	KASSERT(ifa->ifa_ifp != NULL);
1613 	if (!(ifa->ifa_ifp->if_flags & IFF_UP))
1614 		return;
1615 
1616 	mutex_enter(&arp_dad_lock);
1617 	if (arp_dad_find(ifa) != NULL) {
1618 		mutex_exit(&arp_dad_lock);
1619 		/* DAD already in progress */
1620 		return;
1621 	}
1622 
1623 	dp = malloc(sizeof(*dp), M_IPARP, M_NOWAIT);
1624 	if (dp == NULL) {
1625 		mutex_exit(&arp_dad_lock);
1626 		log(LOG_ERR, "%s: memory allocation failed for %s(%s)\n",
1627 		    __func__, in_fmtaddr(ipbuf, ia->ia_addr.sin_addr),
1628 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1629 		return;
1630 	}
1631 	memset(dp, 0, sizeof(*dp));
1632 	callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
1633 
1634 	/*
1635 	 * Send ARP packet for DAD, ip_dad_count times.
1636 	 * Note that we must delay the first transmission.
1637 	 */
1638 	dp->dad_ifa = ifa;
1639 	ifaref(ifa);	/* just for safety */
1640 	dp->dad_count = ip_dad_count;
1641 	dp->dad_arp_announce = 0; /* Will be set when starting to announce */
1642 	dp->dad_arp_acount = dp->dad_arp_ocount = dp->dad_arp_tcount = 0;
1643 	TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
1644 
1645 	ARPLOG(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
1646 	    ARPLOGADDR(ia->ia_addr.sin_addr));
1647 
1648 	arp_dad_starttimer(dp, cprng_fast32() % (PROBE_WAIT * hz));
1649 
1650 	mutex_exit(&arp_dad_lock);
1651 }
1652 
1653 /*
1654  * terminate DAD unconditionally.  used for address removals.
1655  */
1656 static void
1657 arp_dad_stop(struct ifaddr *ifa)
1658 {
1659 	struct dadq *dp;
1660 
1661 	if (!dad_init)
1662 		return;
1663 
1664 	mutex_enter(&arp_dad_lock);
1665 	dp = arp_dad_find(ifa);
1666 	if (dp == NULL) {
1667 		mutex_exit(&arp_dad_lock);
1668 		/* DAD wasn't started yet */
1669 		return;
1670 	}
1671 
1672 	/* Prevent the timer from running anymore. */
1673 	TAILQ_REMOVE(&dadq, dp, dad_list);
1674 	mutex_exit(&arp_dad_lock);
1675 
1676 	arp_dad_stoptimer(dp);
1677 
1678 	free(dp, M_IPARP);
1679 	dp = NULL;
1680 	ifafree(ifa);
1681 }
1682 
1683 static void
1684 arp_dad_timer(struct ifaddr *ifa)
1685 {
1686 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1687 	struct dadq *dp;
1688 	char ipbuf[INET_ADDRSTRLEN];
1689 
1690 #ifndef NET_MPSAFE
1691 	mutex_enter(softnet_lock);
1692 	KERNEL_LOCK(1, NULL);
1693 #endif
1694 	mutex_enter(&arp_dad_lock);
1695 
1696 	/* Sanity check */
1697 	if (ia == NULL) {
1698 		log(LOG_ERR, "%s: called with null parameter\n", __func__);
1699 		goto done;
1700 	}
1701 	dp = arp_dad_find(ifa);
1702 	if (dp == NULL) {
1703 		/* DAD seems to be stopping, so do nothing. */
1704 		goto done;
1705 	}
1706 	if (ia->ia4_flags & IN_IFF_DUPLICATED) {
1707 		log(LOG_ERR, "%s: called with duplicate address %s(%s)\n",
1708 		    __func__, in_fmtaddr(ipbuf, ia->ia_addr.sin_addr),
1709 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1710 		goto done;
1711 	}
1712 	if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0 && dp->dad_arp_acount == 0)
1713 	{
1714 		log(LOG_ERR, "%s: called with non-tentative address %s(%s)\n",
1715 		    __func__, in_fmtaddr(ipbuf, ia->ia_addr.sin_addr),
1716 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
1717 		goto done;
1718 	}
1719 
1720 	/* timeouted with IFF_{RUNNING,UP} check */
1721 	if (dp->dad_arp_tcount > dad_maxtry) {
1722 		ARPLOG(LOG_INFO, "%s: could not run DAD, driver problem?\n",
1723 		    if_name(ifa->ifa_ifp));
1724 
1725 		TAILQ_REMOVE(&dadq, dp, dad_list);
1726 		free(dp, M_IPARP);
1727 		dp = NULL;
1728 		ifafree(ifa);
1729 		goto done;
1730 	}
1731 
1732 	/* Need more checks? */
1733 	if (dp->dad_arp_ocount < dp->dad_count) {
1734 		int adelay;
1735 
1736 		/*
1737 		 * We have more ARP to go.  Send ARP packet for DAD.
1738 		 */
1739 		arp_dad_output(dp, ifa);
1740 		if (dp->dad_arp_ocount < dp->dad_count)
1741 			adelay = (PROBE_MIN * hz) +
1742 			    (cprng_fast32() %
1743 			    ((PROBE_MAX * hz) - (PROBE_MIN * hz)));
1744 		else
1745 			adelay = ANNOUNCE_WAIT * hz;
1746 		arp_dad_starttimer(dp, adelay);
1747 		goto done;
1748 	} else if (dp->dad_arp_acount == 0) {
1749 		/*
1750 		 * We are done with DAD.
1751 		 * No duplicate address found.
1752 		 */
1753 		ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1754 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1755 		ARPLOG(LOG_DEBUG,
1756 		    "%s: DAD complete for %s - no duplicates found\n",
1757 		    if_name(ifa->ifa_ifp), ARPLOGADDR(ia->ia_addr.sin_addr));
1758 		dp->dad_arp_announce = ANNOUNCE_NUM;
1759 		goto announce;
1760 	} else if (dp->dad_arp_acount < dp->dad_arp_announce) {
1761 announce:
1762 		/*
1763 		 * Announce the address.
1764 		 */
1765 		arpannounce1(ifa);
1766 		dp->dad_arp_acount++;
1767 		if (dp->dad_arp_acount < dp->dad_arp_announce) {
1768 			arp_dad_starttimer(dp, ANNOUNCE_INTERVAL * hz);
1769 			goto done;
1770 		}
1771 		ARPLOG(LOG_DEBUG,
1772 		    "%s: ARP announcement complete for %s\n",
1773 		    if_name(ifa->ifa_ifp), ARPLOGADDR(ia->ia_addr.sin_addr));
1774 	}
1775 
1776 	TAILQ_REMOVE(&dadq, dp, dad_list);
1777 	free(dp, M_IPARP);
1778 	dp = NULL;
1779 	ifafree(ifa);
1780 
1781 done:
1782 	mutex_exit(&arp_dad_lock);
1783 #ifndef NET_MPSAFE
1784 	KERNEL_UNLOCK_ONE(NULL);
1785 	mutex_exit(softnet_lock);
1786 #endif
1787 }
1788 
1789 static void
1790 arp_dad_duplicated(struct ifaddr *ifa, const char *sha)
1791 {
1792 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
1793 	struct ifnet *ifp = ifa->ifa_ifp;
1794 	char ipbuf[INET_ADDRSTRLEN];
1795 	const char *iastr;
1796 
1797 	iastr = in_fmtaddr(ipbuf, ia->ia_addr.sin_addr);
1798 
1799 	if (ia->ia4_flags & (IN_IFF_TENTATIVE|IN_IFF_DUPLICATED)) {
1800 		log(LOG_ERR,
1801 		    "%s: DAD duplicate address %s from %s\n",
1802 		    if_name(ifp), iastr, sha);
1803 	} else if (ia->ia_dad_defended == 0 ||
1804 		   ia->ia_dad_defended < time_uptime - DEFEND_INTERVAL) {
1805 		ia->ia_dad_defended = time_uptime;
1806 		arpannounce1(ifa);
1807 		log(LOG_ERR,
1808 		    "%s: DAD defended address %s from %s\n",
1809 		    if_name(ifp), iastr, sha);
1810 		return;
1811 	} else {
1812 		/* If DAD is disabled, just report the duplicate. */
1813 		if (ip_dad_count == 0) {
1814 			log(LOG_ERR,
1815 			    "%s: DAD ignoring duplicate address %s from %s\n",
1816 			    if_name(ifp), iastr, sha);
1817 			return;
1818 		}
1819 		log(LOG_ERR,
1820 		    "%s: DAD defence failed for %s from %s\n",
1821 		    if_name(ifp), iastr, sha);
1822 	}
1823 
1824 	arp_dad_stop(ifa);
1825 
1826 	ia->ia4_flags &= ~IN_IFF_TENTATIVE;
1827 	if ((ia->ia4_flags & IN_IFF_DUPLICATED) == 0) {
1828 		ia->ia4_flags |= IN_IFF_DUPLICATED;
1829 		/* Inform the routing socket of the duplicate address */
1830 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
1831 	}
1832 }
1833 
1834 /*
1835  * Called from 10 Mb/s Ethernet interrupt handlers
1836  * when ether packet type ETHERTYPE_REVARP
1837  * is received.  Common length and type checks are done here,
1838  * then the protocol-specific routine is called.
1839  */
1840 void
1841 revarpinput(struct mbuf *m)
1842 {
1843 	struct arphdr *ar;
1844 
1845 	if (m->m_len < sizeof(struct arphdr))
1846 		goto out;
1847 	ar = mtod(m, struct arphdr *);
1848 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
1849 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
1850 		goto out;
1851 #endif
1852 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
1853 		goto out;
1854 	switch (ntohs(ar->ar_pro)) {
1855 	case ETHERTYPE_IP:
1856 	case ETHERTYPE_IPTRAILERS:
1857 		in_revarpinput(m);
1858 		return;
1859 
1860 	default:
1861 		break;
1862 	}
1863 out:
1864 	m_freem(m);
1865 }
1866 
1867 /*
1868  * RARP for Internet protocols on 10 Mb/s Ethernet.
1869  * Algorithm is that given in RFC 903.
1870  * We are only using for bootstrap purposes to get an ip address for one of
1871  * our interfaces.  Thus we support no user-interface.
1872  *
1873  * Since the contents of the RARP reply are specific to the interface that
1874  * sent the request, this code must ensure that they are properly associated.
1875  *
1876  * Note: also supports ARP via RARP packets, per the RFC.
1877  */
1878 void
1879 in_revarpinput(struct mbuf *m)
1880 {
1881 	struct arphdr *ah;
1882 	void *tha;
1883 	int op;
1884 	struct ifnet *rcvif;
1885 	int s;
1886 
1887 	ah = mtod(m, struct arphdr *);
1888 	op = ntohs(ah->ar_op);
1889 
1890 	rcvif = m_get_rcvif(m, &s);
1891 	if (__predict_false(rcvif == NULL))
1892 		goto out;
1893 	switch (rcvif->if_type) {
1894 	case IFT_IEEE1394:
1895 		/* ARP without target hardware address is not supported */
1896 		goto out;
1897 	default:
1898 		break;
1899 	}
1900 
1901 	switch (op) {
1902 	case ARPOP_REQUEST:
1903 	case ARPOP_REPLY:	/* per RFC */
1904 		m_put_rcvif(rcvif, &s);
1905 		in_arpinput(m);
1906 		return;
1907 	case ARPOP_REVREPLY:
1908 		break;
1909 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
1910 	default:
1911 		goto out;
1912 	}
1913 	if (!revarp_in_progress)
1914 		goto out;
1915 	if (rcvif != myip_ifp) /* !same interface */
1916 		goto out;
1917 	if (myip_initialized)
1918 		goto wake;
1919 	tha = ar_tha(ah);
1920 	if (tha == NULL)
1921 		goto out;
1922 	if (ah->ar_pln != sizeof(struct in_addr))
1923 		goto out;
1924 	if (ah->ar_hln != rcvif->if_sadl->sdl_alen)
1925 		goto out;
1926 	if (memcmp(tha, CLLADDR(rcvif->if_sadl), rcvif->if_sadl->sdl_alen))
1927 		goto out;
1928 	memcpy(&srv_ip, ar_spa(ah), sizeof(srv_ip));
1929 	memcpy(&myip, ar_tpa(ah), sizeof(myip));
1930 	myip_initialized = 1;
1931 wake:	/* Do wakeup every time in case it was missed. */
1932 	wakeup((void *)&myip);
1933 
1934 out:
1935 	m_put_rcvif(rcvif, &s);
1936 	m_freem(m);
1937 }
1938 
1939 /*
1940  * Send a RARP request for the ip address of the specified interface.
1941  * The request should be RFC 903-compliant.
1942  */
1943 static void
1944 revarprequest(struct ifnet *ifp)
1945 {
1946 	struct sockaddr sa;
1947 	struct mbuf *m;
1948 	struct arphdr *ah;
1949 	void *tha;
1950 
1951 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
1952 		return;
1953 	MCLAIM(m, &arpdomain.dom_mowner);
1954 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
1955 	    2*ifp->if_addrlen;
1956 	m->m_pkthdr.len = m->m_len;
1957 	MH_ALIGN(m, m->m_len);
1958 	ah = mtod(m, struct arphdr *);
1959 	memset(ah, 0, m->m_len);
1960 	ah->ar_pro = htons(ETHERTYPE_IP);
1961 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
1962 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
1963 	ah->ar_op = htons(ARPOP_REVREQUEST);
1964 
1965 	memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1966 	tha = ar_tha(ah);
1967 	if (tha == NULL) {
1968 		m_free(m);
1969 		return;
1970 	}
1971 	memcpy(tha, CLLADDR(ifp->if_sadl), ah->ar_hln);
1972 
1973 	sa.sa_family = AF_ARP;
1974 	sa.sa_len = 2;
1975 	m->m_flags |= M_BCAST;
1976 
1977 	if_output_lock(ifp, ifp, m, &sa, NULL);
1978 }
1979 
1980 /*
1981  * RARP for the ip address of the specified interface, but also
1982  * save the ip address of the server that sent the answer.
1983  * Timeout if no response is received.
1984  */
1985 int
1986 revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in,
1987     struct in_addr *clnt_in)
1988 {
1989 	int result, count = 20;
1990 
1991 	myip_initialized = 0;
1992 	myip_ifp = ifp;
1993 
1994 	revarp_in_progress = 1;
1995 	while (count--) {
1996 		revarprequest(ifp);
1997 		result = tsleep((void *)&myip, PSOCK, "revarp", hz/2);
1998 		if (result != EWOULDBLOCK)
1999 			break;
2000 	}
2001 	revarp_in_progress = 0;
2002 
2003 	if (!myip_initialized)
2004 		return ENETUNREACH;
2005 
2006 	memcpy(serv_in, &srv_ip, sizeof(*serv_in));
2007 	memcpy(clnt_in, &myip, sizeof(*clnt_in));
2008 	return 0;
2009 }
2010 
2011 void
2012 arp_stat_add(int type, uint64_t count)
2013 {
2014 	ARP_STATADD(type, count);
2015 }
2016 
2017 static int
2018 sysctl_net_inet_arp_stats(SYSCTLFN_ARGS)
2019 {
2020 
2021 	return NETSTAT_SYSCTL(arpstat_percpu, ARP_NSTATS);
2022 }
2023 
2024 static void
2025 sysctl_net_inet_arp_setup(struct sysctllog **clog)
2026 {
2027 	const struct sysctlnode *node;
2028 
2029 	sysctl_createv(clog, 0, NULL, NULL,
2030 			CTLFLAG_PERMANENT,
2031 			CTLTYPE_NODE, "inet", NULL,
2032 			NULL, 0, NULL, 0,
2033 			CTL_NET, PF_INET, CTL_EOL);
2034 	sysctl_createv(clog, 0, NULL, &node,
2035 			CTLFLAG_PERMANENT,
2036 			CTLTYPE_NODE, "arp",
2037 			SYSCTL_DESCR("Address Resolution Protocol"),
2038 			NULL, 0, NULL, 0,
2039 			CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
2040 
2041 	sysctl_createv(clog, 0, NULL, NULL,
2042 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2043 			CTLTYPE_INT, "keep",
2044 			SYSCTL_DESCR("Valid ARP entry lifetime in seconds"),
2045 			NULL, 0, &arpt_keep, 0,
2046 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2047 
2048 	sysctl_createv(clog, 0, NULL, NULL,
2049 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2050 			CTLTYPE_INT, "down",
2051 			SYSCTL_DESCR("Failed ARP entry lifetime in seconds"),
2052 			NULL, 0, &arpt_down, 0,
2053 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2054 
2055 	sysctl_createv(clog, 0, NULL, NULL,
2056 			CTLFLAG_PERMANENT,
2057 			CTLTYPE_STRUCT, "stats",
2058 			SYSCTL_DESCR("ARP statistics"),
2059 			sysctl_net_inet_arp_stats, 0, NULL, 0,
2060 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2061 
2062 	sysctl_createv(clog, 0, NULL, NULL,
2063 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2064 			CTLTYPE_INT, "log_movements",
2065 			SYSCTL_DESCR("log ARP replies from MACs different than"
2066 			    " the one in the cache"),
2067 			NULL, 0, &log_movements, 0,
2068 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2069 
2070 	sysctl_createv(clog, 0, NULL, NULL,
2071 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2072 			CTLTYPE_INT, "log_permanent_modify",
2073 			SYSCTL_DESCR("log ARP replies from MACs different than"
2074 			    " the one in the permanent arp entry"),
2075 			NULL, 0, &log_permanent_modify, 0,
2076 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2077 
2078 	sysctl_createv(clog, 0, NULL, NULL,
2079 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2080 			CTLTYPE_INT, "log_wrong_iface",
2081 			SYSCTL_DESCR("log ARP packets arriving on the wrong"
2082 			    " interface"),
2083 			NULL, 0, &log_wrong_iface, 0,
2084 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2085 
2086 	sysctl_createv(clog, 0, NULL, NULL,
2087 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2088 			CTLTYPE_INT, "log_unknown_network",
2089 			SYSCTL_DESCR("log ARP packets from non-local network"),
2090 			NULL, 0, &log_unknown_network, 0,
2091 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2092 
2093 	sysctl_createv(clog, 0, NULL, NULL,
2094 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
2095 		       CTLTYPE_INT, "debug",
2096 		       SYSCTL_DESCR("Enable ARP DAD debug output"),
2097 		       NULL, 0, &arp_debug, 0,
2098 		       CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
2099 }
2100 
2101 #endif /* INET */
2102