xref: /netbsd-src/sys/netinet/if_arp.c (revision de4fa6c51a9708fc05f88b618fa6fad87c9508ec)
1 /*	$NetBSD: if_arp.c,v 1.146 2009/08/12 22:16:15 dyoung 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.146 2009/08/12 22:16:15 dyoung Exp $");
72 
73 #include "opt_ddb.h"
74 #include "opt_inet.h"
75 
76 #ifdef INET
77 
78 #include "bridge.h"
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/callout.h>
83 #include <sys/malloc.h>
84 #include <sys/mbuf.h>
85 #include <sys/socket.h>
86 #include <sys/time.h>
87 #include <sys/timetc.h>
88 #include <sys/kernel.h>
89 #include <sys/errno.h>
90 #include <sys/ioctl.h>
91 #include <sys/syslog.h>
92 #include <sys/proc.h>
93 #include <sys/protosw.h>
94 #include <sys/domain.h>
95 #include <sys/sysctl.h>
96 #include <sys/socketvar.h>
97 #include <sys/percpu.h>
98 
99 #include <net/ethertypes.h>
100 #include <net/if.h>
101 #include <net/if_dl.h>
102 #include <net/if_token.h>
103 #include <net/if_types.h>
104 #include <net/if_ether.h>
105 #include <net/route.h>
106 #include <net/net_stats.h>
107 
108 #include <netinet/in.h>
109 #include <netinet/in_systm.h>
110 #include <netinet/in_var.h>
111 #include <netinet/ip.h>
112 #include <netinet/if_inarp.h>
113 
114 #include "arcnet.h"
115 #if NARCNET > 0
116 #include <net/if_arc.h>
117 #endif
118 #include "fddi.h"
119 #if NFDDI > 0
120 #include <net/if_fddi.h>
121 #endif
122 #include "token.h"
123 #include "carp.h"
124 #if NCARP > 0
125 #include <netinet/ip_carp.h>
126 #endif
127 
128 #define SIN(s) ((struct sockaddr_in *)s)
129 #define SRP(s) ((struct sockaddr_inarp *)s)
130 
131 /*
132  * ARP trailer negotiation.  Trailer protocol is not IP specific,
133  * but ARP request/response use IP addresses.
134  */
135 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
136 
137 /* timer values */
138 int	arpt_prune = (5*60*1);	/* walk list every 5 minutes */
139 int	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
140 int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
141 int	arpt_refresh = (5*60);	/* time left before refreshing */
142 #define	rt_expire rt_rmx.rmx_expire
143 #define	rt_pksent rt_rmx.rmx_pksent
144 
145 static	struct sockaddr *arp_setgate(struct rtentry *, struct sockaddr *,
146 	    const struct sockaddr *);
147 static	void arptfree(struct llinfo_arp *);
148 static	void arptimer(void *);
149 static	struct llinfo_arp *arplookup1(struct mbuf *, const struct in_addr *,
150 				      int, int, struct rtentry *);
151 static	struct llinfo_arp *arplookup(struct mbuf *, const struct in_addr *,
152 					  int, int);
153 static	void in_arpinput(struct mbuf *);
154 
155 LIST_HEAD(, llinfo_arp) llinfo_arp;
156 struct	ifqueue arpintrq = {
157 	.ifq_head = NULL,
158 	.ifq_tail = NULL,
159 	.ifq_len = 0,
160 	.ifq_maxlen = 50,
161 	.ifq_drops = 0,
162 };
163 int	arp_inuse, arp_allocated, arp_intimer;
164 int	arp_maxtries = 5;
165 int	useloopback = 1;	/* use loopback interface for local traffic */
166 int	arpinit_done = 0;
167 
168 static percpu_t *arpstat_percpu;
169 
170 #define	ARP_STAT_GETREF()	_NET_STAT_GETREF(arpstat_percpu)
171 #define	ARP_STAT_PUTREF()	_NET_STAT_PUTREF(arpstat_percpu)
172 
173 #define	ARP_STATINC(x)		_NET_STATINC(arpstat_percpu, x)
174 #define	ARP_STATADD(x, v)	_NET_STATADD(arpstat_percpu, x, v)
175 
176 struct	callout arptimer_ch;
177 
178 /* revarp state */
179 struct	in_addr myip, srv_ip;
180 int	myip_initialized = 0;
181 int	revarp_in_progress = 0;
182 struct	ifnet *myip_ifp = NULL;
183 
184 #ifdef DDB
185 static void db_print_sa(const struct sockaddr *);
186 static void db_print_ifa(struct ifaddr *);
187 static void db_print_llinfo(void *);
188 static int db_show_rtentry(struct rtentry *, void *);
189 #endif
190 
191 /*
192  * this should be elsewhere.
193  */
194 
195 static char *
196 lla_snprintf(u_int8_t *, int);
197 
198 static char *
199 lla_snprintf(u_int8_t *adrp, int len)
200 {
201 #define NUMBUFS 3
202 	static char buf[NUMBUFS][16*3];
203 	static int bnum = 0;
204 
205 	int i;
206 	char *p;
207 
208 	p = buf[bnum];
209 
210 	*p++ = hexdigits[(*adrp)>>4];
211 	*p++ = hexdigits[(*adrp++)&0xf];
212 
213 	for (i=1; i<len && i<16; i++) {
214 		*p++ = ':';
215 		*p++ = hexdigits[(*adrp)>>4];
216 		*p++ = hexdigits[(*adrp++)&0xf];
217 	}
218 
219 	*p = 0;
220 	p = buf[bnum];
221 	bnum = (bnum + 1) % NUMBUFS;
222 	return p;
223 }
224 
225 DOMAIN_DEFINE(arpdomain);	/* forward declare and add to link set */
226 
227 const struct protosw arpsw[] = {
228 	{ .pr_type = 0,
229 	  .pr_domain = &arpdomain,
230 	  .pr_protocol = 0,
231 	  .pr_flags = 0,
232 	  .pr_input = 0,
233 	  .pr_output = 0,
234 	  .pr_ctlinput = 0,
235 	  .pr_ctloutput = 0,
236 	  .pr_usrreq =  0,
237 	  .pr_init = arp_init,
238 	  .pr_fasttimo = 0,
239 	  .pr_slowtimo = 0,
240 	  .pr_drain = arp_drain,
241 	}
242 };
243 
244 
245 struct domain arpdomain = {
246 	.dom_family = PF_ARP,
247 	.dom_name = "arp",
248 	.dom_protosw = arpsw,
249 	.dom_protoswNPROTOSW = &arpsw[__arraycount(arpsw)],
250 };
251 
252 /*
253  * ARP table locking.
254  *
255  * to prevent lossage vs. the arp_drain routine (which may be called at
256  * any time, including in a device driver context), we do two things:
257  *
258  * 1) manipulation of la->la_hold is done at splnet() (for all of
259  * about two instructions).
260  *
261  * 2) manipulation of the arp table's linked list is done under the
262  * protection of the ARP_LOCK; if arp_drain() or arptimer is called
263  * while the arp table is locked, we punt and try again later.
264  */
265 
266 static int	arp_locked;
267 static inline int arp_lock_try(int);
268 static inline void arp_unlock(void);
269 
270 static inline int
271 arp_lock_try(int recurse)
272 {
273 	int s;
274 
275 	/*
276 	 * Use splvm() -- we're blocking things that would cause
277 	 * mbuf allocation.
278 	 */
279 	s = splvm();
280 	if (!recurse && arp_locked) {
281 		splx(s);
282 		return 0;
283 	}
284 	arp_locked++;
285 	splx(s);
286 	return 1;
287 }
288 
289 static inline void
290 arp_unlock(void)
291 {
292 	int s;
293 
294 	s = splvm();
295 	arp_locked--;
296 	splx(s);
297 }
298 
299 #ifdef DIAGNOSTIC
300 #define	ARP_LOCK(recurse)						\
301 do {									\
302 	if (arp_lock_try(recurse) == 0) {				\
303 		printf("%s:%d: arp already locked\n", __FILE__, __LINE__); \
304 		panic("arp_lock");					\
305 	}								\
306 } while (/*CONSTCOND*/ 0)
307 #define	ARP_LOCK_CHECK()						\
308 do {									\
309 	if (arp_locked == 0) {						\
310 		printf("%s:%d: arp lock not held\n", __FILE__, __LINE__); \
311 		panic("arp lock check");				\
312 	}								\
313 } while (/*CONSTCOND*/ 0)
314 #else
315 #define	ARP_LOCK(x)		(void) arp_lock_try(x)
316 #define	ARP_LOCK_CHECK()	/* nothing */
317 #endif
318 
319 #define	ARP_UNLOCK()		arp_unlock()
320 
321 void
322 arp_init(void)
323 {
324 
325 	arpstat_percpu = percpu_alloc(sizeof(uint64_t) * ARP_NSTATS);
326 }
327 
328 /*
329  * ARP protocol drain routine.  Called when memory is in short supply.
330  * Called at splvm();  don't acquire softnet_lock as can be called from
331  * hardware interrupt handlers.
332  */
333 void
334 arp_drain(void)
335 {
336 	struct llinfo_arp *la, *nla;
337 	int count = 0;
338 	struct mbuf *mold;
339 
340 	KERNEL_LOCK(1, NULL);
341 
342 	if (arp_lock_try(0) == 0) {
343 		KERNEL_UNLOCK_ONE(NULL);
344 		return;
345 	}
346 
347 	for (la = LIST_FIRST(&llinfo_arp); la != NULL; la = nla) {
348 		nla = LIST_NEXT(la, la_list);
349 
350 		mold = la->la_hold;
351 		la->la_hold = 0;
352 
353 		if (mold) {
354 			m_freem(mold);
355 			count++;
356 		}
357 	}
358 	ARP_UNLOCK();
359 	ARP_STATADD(ARP_STAT_DFRDROPPED, count);
360 	KERNEL_UNLOCK_ONE(NULL);
361 }
362 
363 
364 /*
365  * Timeout routine.  Age arp_tab entries periodically.
366  */
367 /* ARGSUSED */
368 static void
369 arptimer(void *arg)
370 {
371 	struct llinfo_arp *la, *nla;
372 
373 	mutex_enter(softnet_lock);
374 	KERNEL_LOCK(1, NULL);
375 
376 	if (arp_lock_try(0) == 0) {
377 		/* get it later.. */
378 		KERNEL_UNLOCK_ONE(NULL);
379 		mutex_exit(softnet_lock);
380 		return;
381 	}
382 
383 	callout_reset(&arptimer_ch, arpt_prune * hz, arptimer, NULL);
384 	for (la = LIST_FIRST(&llinfo_arp); la != NULL; la = nla) {
385 		struct rtentry *rt = la->la_rt;
386 
387 		nla = LIST_NEXT(la, la_list);
388 		if (rt->rt_expire == 0)
389 			continue;
390 		if ((rt->rt_expire - time_second) < arpt_refresh &&
391 		    rt->rt_pksent > (time_second - arpt_keep)) {
392 			/*
393 			 * If the entry has been used during since last
394 			 * refresh, try to renew it before deleting.
395 			 */
396 			arprequest(rt->rt_ifp,
397 			    &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
398 			    &satocsin(rt_getkey(rt))->sin_addr,
399 			    CLLADDR(rt->rt_ifp->if_sadl));
400 		} else if (rt->rt_expire <= time_second)
401 			arptfree(la); /* timer has expired; clear */
402 	}
403 
404 	ARP_UNLOCK();
405 
406 	KERNEL_UNLOCK_ONE(NULL);
407 	mutex_exit(softnet_lock);
408 }
409 
410 /*
411  * We set the gateway for RTF_CLONING routes to a "prototype"
412  * link-layer sockaddr whose interface type (if_type) and interface
413  * index (if_index) fields are prepared.
414  */
415 static struct sockaddr *
416 arp_setgate(struct rtentry *rt, struct sockaddr *gate,
417     const struct sockaddr *netmask)
418 {
419 	const struct ifnet *ifp = rt->rt_ifp;
420 	uint8_t namelen = strlen(ifp->if_xname);
421 	uint8_t addrlen = ifp->if_addrlen;
422 
423 	/*
424 	 * XXX: If this is a manually added route to interface
425 	 * such as older version of routed or gated might provide,
426 	 * restore cloning bit.
427 	 */
428 	if ((rt->rt_flags & RTF_HOST) == 0 && netmask != NULL &&
429 	    satocsin(netmask)->sin_addr.s_addr != 0xffffffff)
430 		rt->rt_flags |= RTF_CLONING;
431 	if (rt->rt_flags & RTF_CLONING) {
432 		union {
433 			struct sockaddr sa;
434 			struct sockaddr_storage ss;
435 			struct sockaddr_dl sdl;
436 		} u;
437 		/*
438 		 * Case 1: This route should come from a route to iface.
439 		 */
440 		sockaddr_dl_init(&u.sdl, sizeof(u.ss),
441 		    ifp->if_index, ifp->if_type, NULL, namelen, NULL, addrlen);
442 		rt_setgate(rt, &u.sa);
443 		gate = rt->rt_gateway;
444 	}
445 	return gate;
446 }
447 
448 /*
449  * Parallel to llc_rtrequest.
450  */
451 void
452 arp_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
453 {
454 	struct sockaddr *gate = rt->rt_gateway;
455 	struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
456 	size_t allocsize;
457 	struct mbuf *mold;
458 	int s;
459 	struct in_ifaddr *ia;
460 	struct ifaddr *ifa;
461 	struct ifnet *ifp = rt->rt_ifp;
462 
463 	if (!arpinit_done) {
464 		arpinit_done = 1;
465 		/*
466 		 * We generate expiration times from time_second
467 		 * so avoid accidentally creating permanent routes.
468 		 */
469 		if (time_second == 0) {
470 			struct timespec ts;
471 			ts.tv_sec = 1;
472 			ts.tv_nsec = 0;
473 			tc_setclock(&ts);
474 		}
475 		callout_init(&arptimer_ch, CALLOUT_MPSAFE);
476 		callout_reset(&arptimer_ch, hz, arptimer, NULL);
477 	}
478 
479 	if (req == RTM_LLINFO_UPD) {
480 		struct in_addr *in;
481 
482 		if ((ifa = info->rti_ifa) == NULL)
483 			return;
484 
485 		in = &ifatoia(ifa)->ia_addr.sin_addr;
486 
487 		arprequest(ifa->ifa_ifp, in, in,
488 		    CLLADDR(ifa->ifa_ifp->if_sadl));
489 		return;
490 	}
491 
492 	if ((rt->rt_flags & RTF_GATEWAY) != 0) {
493 		if (req != RTM_ADD)
494 			return;
495 
496 		/*
497 		 * linklayers with particular link MTU limitation.
498 		 */
499 		switch(ifp->if_type) {
500 #if NFDDI > 0
501 		case IFT_FDDI:
502 			if (ifp->if_mtu > FDDIIPMTU)
503 				rt->rt_rmx.rmx_mtu = FDDIIPMTU;
504 			break;
505 #endif
506 #if NARC > 0
507 		case IFT_ARCNET:
508 		    {
509 			int arcipifmtu;
510 
511 			if (ifp->if_flags & IFF_LINK0)
512 				arcipifmtu = arc_ipmtu;
513 			else
514 				arcipifmtu = ARCMTU;
515 			if (ifp->if_mtu > arcipifmtu)
516 				rt->rt_rmx.rmx_mtu = arcipifmtu;
517 			break;
518 		    }
519 #endif
520 		}
521 		return;
522 	}
523 
524 	ARP_LOCK(1);		/* we may already be locked here. */
525 
526 	switch (req) {
527 	case RTM_SETGATE:
528 		gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
529 		break;
530 	case RTM_ADD:
531 		gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
532 		if (rt->rt_flags & RTF_CLONING) {
533 			/*
534 			 * Give this route an expiration time, even though
535 			 * it's a "permanent" route, so that routes cloned
536 			 * from it do not need their expiration time set.
537 			 */
538 			rt->rt_expire = time_second;
539 			/*
540 			 * linklayers with particular link MTU limitation.
541 			 */
542 			switch (ifp->if_type) {
543 #if NFDDI > 0
544 			case IFT_FDDI:
545 				if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
546 				    (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
547 				     (rt->rt_rmx.rmx_mtu == 0 &&
548 				      ifp->if_mtu > FDDIIPMTU)))
549 					rt->rt_rmx.rmx_mtu = FDDIIPMTU;
550 				break;
551 #endif
552 #if NARC > 0
553 			case IFT_ARCNET:
554 			    {
555 				int arcipifmtu;
556 				if (ifp->if_flags & IFF_LINK0)
557 					arcipifmtu = arc_ipmtu;
558 				else
559 					arcipifmtu = ARCMTU;
560 
561 				if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
562 				    (rt->rt_rmx.rmx_mtu > arcipifmtu ||
563 				     (rt->rt_rmx.rmx_mtu == 0 &&
564 				      ifp->if_mtu > arcipifmtu)))
565 					rt->rt_rmx.rmx_mtu = arcipifmtu;
566 				break;
567 			    }
568 #endif
569 			}
570 			break;
571 		}
572 		/* Announce a new entry if requested. */
573 		if (rt->rt_flags & RTF_ANNOUNCE) {
574 			arprequest(ifp,
575 			    &satocsin(rt_getkey(rt))->sin_addr,
576 			    &satocsin(rt_getkey(rt))->sin_addr,
577 			    CLLADDR(satocsdl(gate)));
578 		}
579 		/*FALLTHROUGH*/
580 	case RTM_RESOLVE:
581 		if (gate->sa_family != AF_LINK ||
582 		    gate->sa_len < sockaddr_dl_measure(0, ifp->if_addrlen)) {
583 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
584 			break;
585 		}
586 		satosdl(gate)->sdl_type = ifp->if_type;
587 		satosdl(gate)->sdl_index = ifp->if_index;
588 		if (la != NULL)
589 			break; /* This happens on a route change */
590 		/*
591 		 * Case 2:  This route may come from cloning, or a manual route
592 		 * add with a LL address.
593 		 */
594 		switch (ifp->if_type) {
595 #if NTOKEN > 0
596 		case IFT_ISO88025:
597 			allocsize = sizeof(*la) + sizeof(struct token_rif);
598 			break;
599 #endif /* NTOKEN > 0 */
600 		default:
601 			allocsize = sizeof(*la);
602 		}
603 		R_Malloc(la, struct llinfo_arp *, allocsize);
604 		rt->rt_llinfo = (void *)la;
605 		if (la == NULL) {
606 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
607 			break;
608 		}
609 		arp_inuse++, arp_allocated++;
610 		memset(la, 0, allocsize);
611 		la->la_rt = rt;
612 		rt->rt_flags |= RTF_LLINFO;
613 		LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
614 
615 		INADDR_TO_IA(satocsin(rt_getkey(rt))->sin_addr, ia);
616 		while (ia && ia->ia_ifp != ifp)
617 			NEXT_IA_WITH_SAME_ADDR(ia);
618 		if (ia) {
619 			/*
620 			 * This test used to be
621 			 *	if (lo0ifp->if_flags & IFF_UP)
622 			 * It allowed local traffic to be forced through
623 			 * the hardware by configuring the loopback down.
624 			 * However, it causes problems during network
625 			 * configuration for boards that can't receive
626 			 * packets they send.  It is now necessary to clear
627 			 * "useloopback" and remove the route to force
628 			 * traffic out to the hardware.
629 			 *
630 			 * In 4.4BSD, the above "if" statement checked
631 			 * rt->rt_ifa against rt_getkey(rt).  It was changed
632 			 * to the current form so that we can provide a
633 			 * better support for multiple IPv4 addresses on a
634 			 * interface.
635 			 */
636 			rt->rt_expire = 0;
637 			if (sockaddr_dl_init(satosdl(gate), gate->sa_len,
638 			    ifp->if_index, ifp->if_type, NULL, 0,
639 			    CLLADDR(ifp->if_sadl), ifp->if_addrlen) == NULL) {
640 				panic("%s(%s): sockaddr_dl_init cannot fail",
641 				    __func__, ifp->if_xname);
642 			}
643 			if (useloopback)
644 				ifp = rt->rt_ifp = lo0ifp;
645 			/*
646 			 * make sure to set rt->rt_ifa to the interface
647 			 * address we are using, otherwise we will have trouble
648 			 * with source address selection.
649 			 */
650 			ifa = &ia->ia_ifa;
651 			if (ifa != rt->rt_ifa)
652 				rt_replace_ifa(rt, ifa);
653 		}
654 		break;
655 
656 	case RTM_DELETE:
657 		if (la == NULL)
658 			break;
659 		arp_inuse--;
660 		LIST_REMOVE(la, la_list);
661 		rt->rt_llinfo = NULL;
662 		rt->rt_flags &= ~RTF_LLINFO;
663 
664 		s = splnet();
665 		mold = la->la_hold;
666 		la->la_hold = 0;
667 		splx(s);
668 
669 		if (mold)
670 			m_freem(mold);
671 
672 		Free((void *)la);
673 	}
674 	ARP_UNLOCK();
675 }
676 
677 /*
678  * Broadcast an ARP request. Caller specifies:
679  *	- arp header source ip address
680  *	- arp header target ip address
681  *	- arp header source ethernet address
682  */
683 void
684 arprequest(struct ifnet *ifp,
685     const struct in_addr *sip, const struct in_addr *tip,
686     const u_int8_t *enaddr)
687 {
688 	struct mbuf *m;
689 	struct arphdr *ah;
690 	struct sockaddr sa;
691 	uint64_t *arps;
692 
693 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
694 		return;
695 	MCLAIM(m, &arpdomain.dom_mowner);
696 	switch (ifp->if_type) {
697 	case IFT_IEEE1394:
698 		m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
699 		    ifp->if_addrlen;
700 		break;
701 	default:
702 		m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
703 		    2 * ifp->if_addrlen;
704 		break;
705 	}
706 	m->m_pkthdr.len = m->m_len;
707 	MH_ALIGN(m, m->m_len);
708 	ah = mtod(m, struct arphdr *);
709 	memset(ah, 0, m->m_len);
710 	switch (ifp->if_type) {
711 	case IFT_IEEE1394:	/* RFC2734 */
712 		/* fill it now for ar_tpa computation */
713 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
714 		break;
715 	default:
716 		/* ifp->if_output will fill ar_hrd */
717 		break;
718 	}
719 	ah->ar_pro = htons(ETHERTYPE_IP);
720 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
721 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
722 	ah->ar_op = htons(ARPOP_REQUEST);
723 	memcpy(ar_sha(ah), enaddr, ah->ar_hln);
724 	memcpy(ar_spa(ah), sip, ah->ar_pln);
725 	memcpy(ar_tpa(ah), tip, ah->ar_pln);
726 	sa.sa_family = AF_ARP;
727 	sa.sa_len = 2;
728 	m->m_flags |= M_BCAST;
729 	arps = ARP_STAT_GETREF();
730 	arps[ARP_STAT_SNDTOTAL]++;
731 	arps[ARP_STAT_SENDREQUEST]++;
732 	ARP_STAT_PUTREF();
733 	(*ifp->if_output)(ifp, m, &sa, NULL);
734 }
735 
736 /*
737  * Resolve an IP address into an ethernet address.  If success,
738  * desten is filled in.  If there is no entry in arptab,
739  * set one up and broadcast a request for the IP address.
740  * Hold onto this mbuf and resend it once the address
741  * is finally resolved.  A return value of 1 indicates
742  * that desten has been filled in and the packet should be sent
743  * normally; a 0 return indicates that the packet has been
744  * taken over here, either now or for later transmission.
745  */
746 int
747 arpresolve(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m,
748     const struct sockaddr *dst, u_char *desten)
749 {
750 	struct llinfo_arp *la;
751 	const struct sockaddr_dl *sdl;
752 	struct mbuf *mold;
753 	int s;
754 
755 	if ((la = arplookup1(m, &satocsin(dst)->sin_addr, 1, 0, rt)) != NULL)
756 		rt = la->la_rt;
757 
758 	if (la == NULL || rt == NULL) {
759 		ARP_STATINC(ARP_STAT_ALLOCFAIL);
760 		log(LOG_DEBUG,
761 		    "arpresolve: can't allocate llinfo on %s for %s\n",
762 		    ifp->if_xname, in_fmtaddr(satocsin(dst)->sin_addr));
763 		m_freem(m);
764 		return 0;
765 	}
766 	sdl = satocsdl(rt->rt_gateway);
767 	/*
768 	 * Check the address family and length is valid, the address
769 	 * is resolved; otherwise, try to resolve.
770 	 */
771 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
772 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
773 		memcpy(desten, CLLADDR(sdl),
774 		    min(sdl->sdl_alen, ifp->if_addrlen));
775 		rt->rt_pksent = time_second; /* Time for last pkt sent */
776 		return 1;
777 	}
778 	/*
779 	 * There is an arptab entry, but no ethernet address
780 	 * response yet.  Replace the held mbuf with this
781 	 * latest one.
782 	 */
783 
784 	ARP_STATINC(ARP_STAT_DFRTOTAL);
785 	s = splnet();
786 	mold = la->la_hold;
787 	la->la_hold = m;
788 	splx(s);
789 
790 	if (mold) {
791 		ARP_STATINC(ARP_STAT_DFRDROPPED);
792 		m_freem(mold);
793 	}
794 
795 	/*
796 	 * Re-send the ARP request when appropriate.
797 	 */
798 #ifdef	DIAGNOSTIC
799 	if (rt->rt_expire == 0) {
800 		/* This should never happen. (Should it? -gwr) */
801 		printf("arpresolve: unresolved and rt_expire == 0\n");
802 		/* Set expiration time to now (expired). */
803 		rt->rt_expire = time_second;
804 	}
805 #endif
806 	if (rt->rt_expire) {
807 		rt->rt_flags &= ~RTF_REJECT;
808 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
809 			rt->rt_expire = time_second;
810 			if (la->la_asked++ < arp_maxtries) {
811 				arprequest(ifp,
812 				    &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
813 				    &satocsin(dst)->sin_addr,
814 #if NCARP > 0
815 				    (rt->rt_ifp->if_type == IFT_CARP) ?
816 				    CLLADDR(rt->rt_ifp->if_sadl):
817 #endif
818 				    CLLADDR(ifp->if_sadl));
819 			} else {
820 				rt->rt_flags |= RTF_REJECT;
821 				rt->rt_expire += arpt_down;
822 				la->la_asked = 0;
823 			}
824 		}
825 	}
826 	return 0;
827 }
828 
829 /*
830  * Common length and type checks are done here,
831  * then the protocol-specific routine is called.
832  */
833 void
834 arpintr(void)
835 {
836 	struct mbuf *m;
837 	struct arphdr *ar;
838 	int s;
839 	int arplen;
840 
841 	mutex_enter(softnet_lock);
842 	KERNEL_LOCK(1, NULL);
843 	while (arpintrq.ifq_head) {
844 		s = splnet();
845 		IF_DEQUEUE(&arpintrq, m);
846 		splx(s);
847 		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
848 			panic("arpintr");
849 
850 		MCLAIM(m, &arpdomain.dom_mowner);
851 		ARP_STATINC(ARP_STAT_RCVTOTAL);
852 
853 		/*
854 		 * First, make sure we have at least struct arphdr.
855 		 */
856 		if (m->m_len < sizeof(struct arphdr) ||
857 		    (ar = mtod(m, struct arphdr *)) == NULL)
858 			goto badlen;
859 
860 		switch (m->m_pkthdr.rcvif->if_type) {
861 		case IFT_IEEE1394:
862 			arplen = sizeof(struct arphdr) +
863 			    ar->ar_hln + 2 * ar->ar_pln;
864 			break;
865 		default:
866 			arplen = sizeof(struct arphdr) +
867 			    2 * ar->ar_hln + 2 * ar->ar_pln;
868 			break;
869 		}
870 
871 		if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
872 		    m->m_len >= arplen)
873 			switch (ntohs(ar->ar_pro)) {
874 			case ETHERTYPE_IP:
875 			case ETHERTYPE_IPTRAILERS:
876 				in_arpinput(m);
877 				continue;
878 			default:
879 				ARP_STATINC(ARP_STAT_RCVBADPROTO);
880 			}
881 		else {
882 badlen:
883 			ARP_STATINC(ARP_STAT_RCVBADLEN);
884 		}
885 		m_freem(m);
886 	}
887 	KERNEL_UNLOCK_ONE(NULL);
888 	mutex_exit(softnet_lock);
889 }
890 
891 /*
892  * ARP for Internet protocols on 10 Mb/s Ethernet.
893  * Algorithm is that given in RFC 826.
894  * In addition, a sanity check is performed on the sender
895  * protocol address, to catch impersonators.
896  * We no longer handle negotiations for use of trailer protocol:
897  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
898  * along with IP replies if we wanted trailers sent to us,
899  * and also sent them in response to IP replies.
900  * This allowed either end to announce the desire to receive
901  * trailer packets.
902  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
903  * but formerly didn't normally send requests.
904  */
905 static void
906 in_arpinput(struct mbuf *m)
907 {
908 	struct arphdr *ah;
909 	struct ifnet *ifp = m->m_pkthdr.rcvif;
910 	struct llinfo_arp *la = NULL;
911 	struct rtentry  *rt;
912 	struct in_ifaddr *ia;
913 #if NBRIDGE > 0
914 	struct in_ifaddr *bridge_ia = NULL;
915 #endif
916 #if NCARP > 0
917 	u_int32_t count = 0, index = 0;
918 #endif
919 	struct sockaddr_dl *sdl;
920 	struct sockaddr sa;
921 	struct in_addr isaddr, itaddr, myaddr;
922 	int op;
923 	struct mbuf *mold;
924 	void *tha;
925 	int s;
926 	uint64_t *arps;
927 
928 	if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
929 		goto out;
930 	ah = mtod(m, struct arphdr *);
931 	op = ntohs(ah->ar_op);
932 
933 	/*
934 	 * Fix up ah->ar_hrd if necessary, before using ar_tha() or
935 	 * ar_tpa().
936 	 */
937 	switch (ifp->if_type) {
938 	case IFT_IEEE1394:
939 		if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
940 			;
941 		else {
942 			/* XXX this is to make sure we compute ar_tha right */
943 			/* XXX check ar_hrd more strictly? */
944 			ah->ar_hrd = htons(ARPHRD_IEEE1394);
945 		}
946 		break;
947 	default:
948 		/* XXX check ar_hrd? */
949 		break;
950 	}
951 
952 	memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
953 	memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
954 
955 	if (m->m_flags & (M_BCAST|M_MCAST))
956 		ARP_STATINC(ARP_STAT_RCVMCAST);
957 
958 	/*
959 	 * If the target IP address is zero, ignore the packet.
960 	 * This prevents the code below from tring to answer
961 	 * when we are using IP address zero (booting).
962 	 */
963 	if (in_nullhost(itaddr)) {
964 		ARP_STATINC(ARP_STAT_RCVZEROTPA);
965 		goto out;
966 	}
967 
968 	/*
969 	 * If the source IP address is zero, this is most likely a
970 	 * confused host trying to use IP address zero. (Windoze?)
971 	 * XXX: Should we bother trying to reply to these?
972 	 */
973 	if (in_nullhost(isaddr)) {
974 		ARP_STATINC(ARP_STAT_RCVZEROSPA);
975 		goto out;
976 	}
977 
978 	/*
979 	 * Search for a matching interface address
980 	 * or any address on the interface to use
981 	 * as a dummy address in the rest of this function
982 	 */
983 
984 	INADDR_TO_IA(itaddr, ia);
985 	while (ia != NULL) {
986 #if NCARP > 0
987 		if (ia->ia_ifp->if_type == IFT_CARP &&
988 		    ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
989 		    (IFF_UP|IFF_RUNNING))) {
990 			index++;
991 			if (ia->ia_ifp == m->m_pkthdr.rcvif &&
992 			    carp_iamatch(ia, ar_sha(ah),
993 			    &count, index)) {
994 				break;
995 				}
996 		} else
997 #endif
998 			    if (ia->ia_ifp == m->m_pkthdr.rcvif)
999 				break;
1000 #if NBRIDGE > 0
1001 		/*
1002 		 * If the interface we received the packet on
1003 		 * is part of a bridge, check to see if we need
1004 		 * to "bridge" the packet to ourselves at this
1005 		 * layer.  Note we still prefer a perfect match,
1006 		 * but allow this weaker match if necessary.
1007 		 */
1008 		if (m->m_pkthdr.rcvif->if_bridge != NULL &&
1009 		    m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge)
1010 			bridge_ia = ia;
1011 #endif /* NBRIDGE > 0 */
1012 
1013 		NEXT_IA_WITH_SAME_ADDR(ia);
1014 	}
1015 
1016 #if NBRIDGE > 0
1017 	if (ia == NULL && bridge_ia != NULL) {
1018 		ia = bridge_ia;
1019 		ifp = bridge_ia->ia_ifp;
1020 	}
1021 #endif
1022 
1023 	if (ia == NULL) {
1024 		INADDR_TO_IA(isaddr, ia);
1025 		while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
1026 			NEXT_IA_WITH_SAME_ADDR(ia);
1027 
1028 		if (ia == NULL) {
1029 			IFP_TO_IA(ifp, ia);
1030 			if (ia == NULL) {
1031 				ARP_STATINC(ARP_STAT_RCVNOINT);
1032 				goto out;
1033 			}
1034 		}
1035 	}
1036 
1037 	myaddr = ia->ia_addr.sin_addr;
1038 
1039 	/* XXX checks for bridge case? */
1040 	if (!memcmp(ar_sha(ah), CLLADDR(ifp->if_sadl), ifp->if_addrlen)) {
1041 		ARP_STATINC(ARP_STAT_RCVLOCALSHA);
1042 		goto out;	/* it's from me, ignore it. */
1043 	}
1044 
1045 	/* XXX checks for bridge case? */
1046 	if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1047 		ARP_STATINC(ARP_STAT_RCVBCASTSHA);
1048 		log(LOG_ERR,
1049 		    "%s: arp: link address is broadcast for IP address %s!\n",
1050 		    ifp->if_xname, in_fmtaddr(isaddr));
1051 		goto out;
1052 	}
1053 
1054 	if (in_hosteq(isaddr, myaddr)) {
1055 		ARP_STATINC(ARP_STAT_RCVLOCALSPA);
1056 		log(LOG_ERR,
1057 		   "duplicate IP address %s sent from link address %s\n",
1058 		   in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
1059 		itaddr = myaddr;
1060 		goto reply;
1061 	}
1062 	la = arplookup(m, &isaddr, in_hosteq(itaddr, myaddr), 0);
1063 	if (la != NULL && (rt = la->la_rt) && (sdl = satosdl(rt->rt_gateway))) {
1064 		if (sdl->sdl_alen &&
1065 		    memcmp(ar_sha(ah), CLLADDR(sdl), sdl->sdl_alen)) {
1066 			if (rt->rt_flags & RTF_STATIC) {
1067 				ARP_STATINC(ARP_STAT_RCVOVERPERM);
1068 				log(LOG_INFO,
1069 				    "%s tried to overwrite permanent arp info"
1070 				    " for %s\n",
1071 				    lla_snprintf(ar_sha(ah), ah->ar_hln),
1072 				    in_fmtaddr(isaddr));
1073 				goto out;
1074 			} else if (rt->rt_ifp != ifp) {
1075 				ARP_STATINC(ARP_STAT_RCVOVERINT);
1076 				log(LOG_INFO,
1077 				    "%s on %s tried to overwrite "
1078 				    "arp info for %s on %s\n",
1079 				    lla_snprintf(ar_sha(ah), ah->ar_hln),
1080 				    ifp->if_xname, in_fmtaddr(isaddr),
1081 				    rt->rt_ifp->if_xname);
1082 				    goto out;
1083 			} else {
1084 				ARP_STATINC(ARP_STAT_RCVOVER);
1085 				log(LOG_INFO,
1086 				    "arp info overwritten for %s by %s\n",
1087 				    in_fmtaddr(isaddr),
1088 				    lla_snprintf(ar_sha(ah), ah->ar_hln));
1089 			}
1090 		}
1091 		/*
1092 		 * sanity check for the address length.
1093 		 * XXX this does not work for protocols with variable address
1094 		 * length. -is
1095 		 */
1096 		if (sdl->sdl_alen &&
1097 		    sdl->sdl_alen != ah->ar_hln) {
1098 			ARP_STATINC(ARP_STAT_RCVLENCHG);
1099 			log(LOG_WARNING,
1100 			    "arp from %s: new addr len %d, was %d\n",
1101 			    in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
1102 		}
1103 		if (ifp->if_addrlen != ah->ar_hln) {
1104 			ARP_STATINC(ARP_STAT_RCVBADLEN);
1105 			log(LOG_WARNING,
1106 			    "arp from %s: addr len: new %d, i/f %d (ignored)\n",
1107 			    in_fmtaddr(isaddr), ah->ar_hln,
1108 			    ifp->if_addrlen);
1109 			goto reply;
1110 		}
1111 #if NTOKEN > 0
1112 		/*
1113 		 * XXX uses m_data and assumes the complete answer including
1114 		 * XXX token-ring headers is in the same buf
1115 		 */
1116 		if (ifp->if_type == IFT_ISO88025) {
1117 			struct token_header *trh;
1118 
1119 			trh = (struct token_header *)M_TRHSTART(m);
1120 			if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
1121 				struct token_rif	*rif;
1122 				size_t	riflen;
1123 
1124 				rif = TOKEN_RIF(trh);
1125 				riflen = (ntohs(rif->tr_rcf) &
1126 				    TOKEN_RCF_LEN_MASK) >> 8;
1127 
1128 				if (riflen > 2 &&
1129 				    riflen < sizeof(struct token_rif) &&
1130 				    (riflen & 1) == 0) {
1131 					rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
1132 					rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
1133 					memcpy(TOKEN_RIF(la), rif, riflen);
1134 				}
1135 			}
1136 		}
1137 #endif /* NTOKEN > 0 */
1138 		(void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, ar_sha(ah),
1139 		    ah->ar_hln);
1140 		if (rt->rt_expire)
1141 			rt->rt_expire = time_second + arpt_keep;
1142 		rt->rt_flags &= ~RTF_REJECT;
1143 		la->la_asked = 0;
1144 
1145 		s = splnet();
1146 		mold = la->la_hold;
1147 		la->la_hold = 0;
1148 		splx(s);
1149 
1150 		if (mold) {
1151 			ARP_STATINC(ARP_STAT_DFRSENT);
1152 			(*ifp->if_output)(ifp, mold, rt_getkey(rt), rt);
1153 		}
1154 	}
1155 reply:
1156 	if (op != ARPOP_REQUEST) {
1157 		if (op == ARPOP_REPLY)
1158 			ARP_STATINC(ARP_STAT_RCVREPLY);
1159 	out:
1160 		m_freem(m);
1161 		return;
1162 	}
1163 	ARP_STATINC(ARP_STAT_RCVREQUEST);
1164 	if (in_hosteq(itaddr, myaddr)) {
1165 		/* I am the target */
1166 		tha = ar_tha(ah);
1167 		if (tha)
1168 			memcpy(tha, ar_sha(ah), ah->ar_hln);
1169 		memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1170 	} else {
1171 		la = arplookup(m, &itaddr, 0, SIN_PROXY);
1172 		if (la == NULL)
1173 			goto out;
1174 		rt = la->la_rt;
1175 		if (rt->rt_ifp->if_type == IFT_CARP &&
1176 		    m->m_pkthdr.rcvif->if_type != IFT_CARP)
1177 			goto out;
1178 		tha = ar_tha(ah);
1179 		if (tha)
1180 			memcpy(tha, ar_sha(ah), ah->ar_hln);
1181 		sdl = satosdl(rt->rt_gateway);
1182 		memcpy(ar_sha(ah), CLLADDR(sdl), ah->ar_hln);
1183 	}
1184 
1185 	memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1186 	memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1187 	ah->ar_op = htons(ARPOP_REPLY);
1188 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1189 	switch (ifp->if_type) {
1190 	case IFT_IEEE1394:
1191 		/*
1192 		 * ieee1394 arp reply is broadcast
1193 		 */
1194 		m->m_flags &= ~M_MCAST;
1195 		m->m_flags |= M_BCAST;
1196 		m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
1197 		break;
1198 
1199 	default:
1200 		m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
1201 		m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
1202 		break;
1203 	}
1204 	m->m_pkthdr.len = m->m_len;
1205 	sa.sa_family = AF_ARP;
1206 	sa.sa_len = 2;
1207 	arps = ARP_STAT_GETREF();
1208 	arps[ARP_STAT_SNDTOTAL]++;
1209 	arps[ARP_STAT_SNDREPLY]++;
1210 	ARP_STAT_PUTREF();
1211 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
1212 	return;
1213 }
1214 
1215 /*
1216  * Free an arp entry.
1217  */
1218 static void arptfree(struct llinfo_arp *la)
1219 {
1220 	struct rtentry *rt = la->la_rt;
1221 	struct sockaddr_dl *sdl;
1222 
1223 	ARP_LOCK_CHECK();
1224 
1225 	if (rt == NULL)
1226 		panic("arptfree");
1227 	if (rt->rt_refcnt > 0 && (sdl = satosdl(rt->rt_gateway)) &&
1228 	    sdl->sdl_family == AF_LINK) {
1229 		sdl->sdl_alen = 0;
1230 		la->la_asked = 0;
1231 		rt->rt_flags &= ~RTF_REJECT;
1232 		return;
1233 	}
1234 	rtrequest(RTM_DELETE, rt_getkey(rt), NULL, rt_mask(rt), 0, NULL);
1235 }
1236 
1237 static struct llinfo_arp *
1238 arplookup(struct mbuf *m, const struct in_addr *addr, int create, int proxy)
1239 {
1240 	return arplookup1(m, addr, create, proxy, NULL);
1241 }
1242 
1243 /*
1244  * Lookup or enter a new address in arptab.
1245  */
1246 static struct llinfo_arp *
1247 arplookup1(struct mbuf *m, const struct in_addr *addr, int create, int proxy,
1248     struct rtentry *rt0)
1249 {
1250 	struct arphdr *ah;
1251 	struct ifnet *ifp = m->m_pkthdr.rcvif;
1252 	struct rtentry *rt;
1253 	struct sockaddr_inarp sin;
1254 	const char *why = NULL;
1255 
1256 	ah = mtod(m, struct arphdr *);
1257 	if (rt0 == NULL) {
1258 		memset(&sin, 0, sizeof(sin));
1259 		sin.sin_len = sizeof(sin);
1260 		sin.sin_family = AF_INET;
1261 		sin.sin_addr = *addr;
1262 		sin.sin_other = proxy ? SIN_PROXY : 0;
1263 		rt = rtalloc1(sintosa(&sin), create);
1264 		if (rt == NULL)
1265 			return NULL;
1266 		rt->rt_refcnt--;
1267 	} else
1268 		rt = rt0;
1269 
1270 #define	IS_LLINFO(__rt)							  \
1271 	(((__rt)->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) == RTF_LLINFO && \
1272 	 (__rt)->rt_gateway->sa_family == AF_LINK)
1273 
1274 
1275 	if (IS_LLINFO(rt))
1276 		return (struct llinfo_arp *)rt->rt_llinfo;
1277 
1278 	if (create) {
1279 		if (rt->rt_flags & RTF_GATEWAY)
1280 			why = "host is not on local network";
1281 		else if ((rt->rt_flags & RTF_LLINFO) == 0) {
1282 			ARP_STATINC(ARP_STAT_ALLOCFAIL);
1283 			why = "could not allocate llinfo";
1284 		} else
1285 			why = "gateway route is not ours";
1286 		log(LOG_DEBUG, "arplookup: unable to enter address"
1287 		    " for %s@%s on %s (%s)\n",
1288 		    in_fmtaddr(*addr), lla_snprintf(ar_sha(ah), ah->ar_hln),
1289 		    (ifp) ? ifp->if_xname : "null", why);
1290 		if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_CLONED) != 0) {
1291 			rtrequest(RTM_DELETE, rt_getkey(rt),
1292 		    	    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
1293 		}
1294 	}
1295 	return NULL;
1296 }
1297 
1298 int
1299 arpioctl(u_long cmd, void *data)
1300 {
1301 
1302 	return EOPNOTSUPP;
1303 }
1304 
1305 void
1306 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1307 {
1308 	struct in_addr *ip;
1309 
1310 	/*
1311 	 * Warn the user if another station has this IP address,
1312 	 * but only if the interface IP address is not zero.
1313 	 */
1314 	ip = &IA_SIN(ifa)->sin_addr;
1315 	if (!in_nullhost(*ip))
1316 		arprequest(ifp, ip, ip, CLLADDR(ifp->if_sadl));
1317 
1318 	ifa->ifa_rtrequest = arp_rtrequest;
1319 	ifa->ifa_flags |= RTF_CLONING;
1320 }
1321 
1322 /*
1323  * Called from 10 Mb/s Ethernet interrupt handlers
1324  * when ether packet type ETHERTYPE_REVARP
1325  * is received.  Common length and type checks are done here,
1326  * then the protocol-specific routine is called.
1327  */
1328 void
1329 revarpinput(struct mbuf *m)
1330 {
1331 	struct arphdr *ar;
1332 
1333 	if (m->m_len < sizeof(struct arphdr))
1334 		goto out;
1335 	ar = mtod(m, struct arphdr *);
1336 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
1337 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
1338 		goto out;
1339 #endif
1340 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
1341 		goto out;
1342 	switch (ntohs(ar->ar_pro)) {
1343 	case ETHERTYPE_IP:
1344 	case ETHERTYPE_IPTRAILERS:
1345 		in_revarpinput(m);
1346 		return;
1347 
1348 	default:
1349 		break;
1350 	}
1351 out:
1352 	m_freem(m);
1353 }
1354 
1355 /*
1356  * RARP for Internet protocols on 10 Mb/s Ethernet.
1357  * Algorithm is that given in RFC 903.
1358  * We are only using for bootstrap purposes to get an ip address for one of
1359  * our interfaces.  Thus we support no user-interface.
1360  *
1361  * Since the contents of the RARP reply are specific to the interface that
1362  * sent the request, this code must ensure that they are properly associated.
1363  *
1364  * Note: also supports ARP via RARP packets, per the RFC.
1365  */
1366 void
1367 in_revarpinput(struct mbuf *m)
1368 {
1369 	struct ifnet *ifp;
1370 	struct arphdr *ah;
1371 	void *tha;
1372 	int op;
1373 
1374 	ah = mtod(m, struct arphdr *);
1375 	op = ntohs(ah->ar_op);
1376 
1377 	switch (m->m_pkthdr.rcvif->if_type) {
1378 	case IFT_IEEE1394:
1379 		/* ARP without target hardware address is not supported */
1380 		goto out;
1381 	default:
1382 		break;
1383 	}
1384 
1385 	switch (op) {
1386 	case ARPOP_REQUEST:
1387 	case ARPOP_REPLY:	/* per RFC */
1388 		in_arpinput(m);
1389 		return;
1390 	case ARPOP_REVREPLY:
1391 		break;
1392 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
1393 	default:
1394 		goto out;
1395 	}
1396 	if (!revarp_in_progress)
1397 		goto out;
1398 	ifp = m->m_pkthdr.rcvif;
1399 	if (ifp != myip_ifp) /* !same interface */
1400 		goto out;
1401 	if (myip_initialized)
1402 		goto wake;
1403 	tha = ar_tha(ah);
1404 	KASSERT(tha);
1405 	if (memcmp(tha, CLLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
1406 		goto out;
1407 	memcpy(&srv_ip, ar_spa(ah), sizeof(srv_ip));
1408 	memcpy(&myip, ar_tpa(ah), sizeof(myip));
1409 	myip_initialized = 1;
1410 wake:	/* Do wakeup every time in case it was missed. */
1411 	wakeup((void *)&myip);
1412 
1413 out:
1414 	m_freem(m);
1415 }
1416 
1417 /*
1418  * Send a RARP request for the ip address of the specified interface.
1419  * The request should be RFC 903-compliant.
1420  */
1421 void
1422 revarprequest(struct ifnet *ifp)
1423 {
1424 	struct sockaddr sa;
1425 	struct mbuf *m;
1426 	struct arphdr *ah;
1427 	void *tha;
1428 
1429 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
1430 		return;
1431 	MCLAIM(m, &arpdomain.dom_mowner);
1432 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
1433 	    2*ifp->if_addrlen;
1434 	m->m_pkthdr.len = m->m_len;
1435 	MH_ALIGN(m, m->m_len);
1436 	ah = mtod(m, struct arphdr *);
1437 	memset(ah, 0, m->m_len);
1438 	ah->ar_pro = htons(ETHERTYPE_IP);
1439 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
1440 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
1441 	ah->ar_op = htons(ARPOP_REVREQUEST);
1442 
1443 	memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1444 	tha = ar_tha(ah);
1445 	KASSERT(tha);
1446 	memcpy(tha, CLLADDR(ifp->if_sadl), ah->ar_hln);
1447 
1448 	sa.sa_family = AF_ARP;
1449 	sa.sa_len = 2;
1450 	m->m_flags |= M_BCAST;
1451 	(*ifp->if_output)(ifp, m, &sa, NULL);
1452 
1453 }
1454 
1455 /*
1456  * RARP for the ip address of the specified interface, but also
1457  * save the ip address of the server that sent the answer.
1458  * Timeout if no response is received.
1459  */
1460 int
1461 revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in,
1462     struct in_addr *clnt_in)
1463 {
1464 	int result, count = 20;
1465 
1466 	myip_initialized = 0;
1467 	myip_ifp = ifp;
1468 
1469 	revarp_in_progress = 1;
1470 	while (count--) {
1471 		revarprequest(ifp);
1472 		result = tsleep((void *)&myip, PSOCK, "revarp", hz/2);
1473 		if (result != EWOULDBLOCK)
1474 			break;
1475 	}
1476 	revarp_in_progress = 0;
1477 
1478 	if (!myip_initialized)
1479 		return ENETUNREACH;
1480 
1481 	memcpy(serv_in, &srv_ip, sizeof(*serv_in));
1482 	memcpy(clnt_in, &myip, sizeof(*clnt_in));
1483 	return 0;
1484 }
1485 
1486 
1487 
1488 #ifdef DDB
1489 
1490 #include <machine/db_machdep.h>
1491 #include <ddb/db_interface.h>
1492 #include <ddb/db_output.h>
1493 
1494 static void
1495 db_print_sa(const struct sockaddr *sa)
1496 {
1497 	int len;
1498 	const u_char *p;
1499 
1500 	if (sa == NULL) {
1501 		db_printf("[NULL]");
1502 		return;
1503 	}
1504 
1505 	p = (const u_char *)sa;
1506 	len = sa->sa_len;
1507 	db_printf("[");
1508 	while (len > 0) {
1509 		db_printf("%d", *p);
1510 		p++; len--;
1511 		if (len) db_printf(",");
1512 	}
1513 	db_printf("]\n");
1514 }
1515 
1516 static void
1517 db_print_ifa(struct ifaddr *ifa)
1518 {
1519 	if (ifa == NULL)
1520 		return;
1521 	db_printf("  ifa_addr=");
1522 	db_print_sa(ifa->ifa_addr);
1523 	db_printf("  ifa_dsta=");
1524 	db_print_sa(ifa->ifa_dstaddr);
1525 	db_printf("  ifa_mask=");
1526 	db_print_sa(ifa->ifa_netmask);
1527 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
1528 			  ifa->ifa_flags,
1529 			  ifa->ifa_refcnt,
1530 			  ifa->ifa_metric);
1531 }
1532 
1533 static void
1534 db_print_llinfo(void *li)
1535 {
1536 	struct llinfo_arp *la;
1537 
1538 	if (li == NULL)
1539 		return;
1540 	la = (struct llinfo_arp *)li;
1541 	db_printf("  la_rt=%p la_hold=%p, la_asked=0x%lx\n",
1542 			  la->la_rt, la->la_hold, la->la_asked);
1543 }
1544 
1545 /*
1546  * Function to pass to rt_walktree().
1547  * Return non-zero error to abort walk.
1548  */
1549 static int
1550 db_show_rtentry(struct rtentry *rt, void *w)
1551 {
1552 	db_printf("rtentry=%p", rt);
1553 
1554 	db_printf(" flags=0x%x refcnt=%d use=%ld expire=%lld\n",
1555 			  rt->rt_flags, rt->rt_refcnt,
1556 			  rt->rt_use, (long long)rt->rt_expire);
1557 
1558 	db_printf(" key="); db_print_sa(rt_getkey(rt));
1559 	db_printf(" mask="); db_print_sa(rt_mask(rt));
1560 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
1561 
1562 	db_printf(" ifp=%p ", rt->rt_ifp);
1563 	if (rt->rt_ifp)
1564 		db_printf("(%s)", rt->rt_ifp->if_xname);
1565 	else
1566 		db_printf("(NULL)");
1567 
1568 	db_printf(" ifa=%p\n", rt->rt_ifa);
1569 	db_print_ifa(rt->rt_ifa);
1570 
1571 	db_printf(" gwroute=%p llinfo=%p\n",
1572 			  rt->rt_gwroute, rt->rt_llinfo);
1573 	db_print_llinfo(rt->rt_llinfo);
1574 
1575 	return 0;
1576 }
1577 
1578 /*
1579  * Function to print all the route trees.
1580  * Use this from ddb:  "show arptab"
1581  */
1582 void
1583 db_show_arptab(db_expr_t addr, bool have_addr,
1584     db_expr_t count, const char *modif)
1585 {
1586 	rt_walktree(AF_INET, db_show_rtentry, NULL);
1587 }
1588 #endif
1589 
1590 static int
1591 sysctl_net_inet_arp_stats(SYSCTLFN_ARGS)
1592 {
1593 
1594 	return NETSTAT_SYSCTL(arpstat_percpu, ARP_NSTATS);
1595 }
1596 
1597 SYSCTL_SETUP(sysctl_net_inet_arp_setup, "sysctl net.inet.arp subtree setup")
1598 {
1599 	const struct sysctlnode *node;
1600 
1601 	sysctl_createv(clog, 0, NULL, NULL,
1602 			CTLFLAG_PERMANENT,
1603 			CTLTYPE_NODE, "net", NULL,
1604 			NULL, 0, NULL, 0,
1605 			CTL_NET, CTL_EOL);
1606 	sysctl_createv(clog, 0, NULL, NULL,
1607 			CTLFLAG_PERMANENT,
1608 			CTLTYPE_NODE, "inet", NULL,
1609 			NULL, 0, NULL, 0,
1610 			CTL_NET, PF_INET, CTL_EOL);
1611 	sysctl_createv(clog, 0, NULL, &node,
1612 			CTLFLAG_PERMANENT,
1613 			CTLTYPE_NODE, "arp",
1614 			SYSCTL_DESCR("Address Resolution Protocol"),
1615 			NULL, 0, NULL, 0,
1616 			CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
1617 
1618 	sysctl_createv(clog, 0, NULL, NULL,
1619 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1620 			CTLTYPE_INT, "prune",
1621 			SYSCTL_DESCR("ARP cache pruning interval"),
1622 			NULL, 0, &arpt_prune, 0,
1623 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1624 
1625 	sysctl_createv(clog, 0, NULL, NULL,
1626 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1627 			CTLTYPE_INT, "keep",
1628 			SYSCTL_DESCR("Valid ARP entry lifetime"),
1629 			NULL, 0, &arpt_keep, 0,
1630 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1631 
1632 	sysctl_createv(clog, 0, NULL, NULL,
1633 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1634 			CTLTYPE_INT, "down",
1635 			SYSCTL_DESCR("Failed ARP entry lifetime"),
1636 			NULL, 0, &arpt_down, 0,
1637 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1638 
1639 	sysctl_createv(clog, 0, NULL, NULL,
1640 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1641 			CTLTYPE_INT, "refresh",
1642 			SYSCTL_DESCR("ARP entry refresh interval"),
1643 			NULL, 0, &arpt_refresh, 0,
1644 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1645 
1646 	sysctl_createv(clog, 0, NULL, NULL,
1647 			CTLFLAG_PERMANENT,
1648 			CTLTYPE_STRUCT, "stats",
1649 			SYSCTL_DESCR("ARP statistics"),
1650 			sysctl_net_inet_arp_stats, 0, NULL, 0,
1651 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1652 }
1653 
1654 #endif /* INET */
1655