xref: /netbsd-src/sys/netinet/if_arp.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /*	$NetBSD: if_arp.c,v 1.144 2008/11/07 00:20:18 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.144 2008/11/07 00:20:18 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 	uint8_t namelen = strlen(ifp->if_xname);
463 	uint8_t addrlen = ifp->if_addrlen;
464 
465 	if (!arpinit_done) {
466 		arpinit_done = 1;
467 		/*
468 		 * We generate expiration times from time_second
469 		 * so avoid accidentally creating permanent routes.
470 		 */
471 		if (time_second == 0) {
472 			struct timespec ts;
473 			ts.tv_sec = 1;
474 			ts.tv_nsec = 0;
475 			tc_setclock(&ts);
476 		}
477 		callout_init(&arptimer_ch, CALLOUT_MPSAFE);
478 		callout_reset(&arptimer_ch, hz, arptimer, NULL);
479 	}
480 
481 	if (req == RTM_LLINFO_UPD) {
482 		struct in_addr *in;
483 
484 		if ((ifa = info->rti_ifa) == NULL)
485 			return;
486 
487 		in = &ifatoia(ifa)->ia_addr.sin_addr;
488 
489 		arprequest(ifa->ifa_ifp, in, in,
490 		    CLLADDR(ifa->ifa_ifp->if_sadl));
491 		return;
492 	}
493 
494 	if ((rt->rt_flags & RTF_GATEWAY) != 0) {
495 		if (req != RTM_ADD)
496 			return;
497 
498 		/*
499 		 * linklayers with particular link MTU limitation.
500 		 */
501 		switch(ifp->if_type) {
502 #if NFDDI > 0
503 		case IFT_FDDI:
504 			if (ifp->if_mtu > FDDIIPMTU)
505 				rt->rt_rmx.rmx_mtu = FDDIIPMTU;
506 			break;
507 #endif
508 #if NARC > 0
509 		case IFT_ARCNET:
510 		    {
511 			int arcipifmtu;
512 
513 			if (ifp->if_flags & IFF_LINK0)
514 				arcipifmtu = arc_ipmtu;
515 			else
516 				arcipifmtu = ARCMTU;
517 			if (ifp->if_mtu > arcipifmtu)
518 				rt->rt_rmx.rmx_mtu = arcipifmtu;
519 			break;
520 		    }
521 #endif
522 		}
523 		return;
524 	}
525 
526 	ARP_LOCK(1);		/* we may already be locked here. */
527 
528 	switch (req) {
529 	case RTM_SETGATE:
530 		gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
531 		break;
532 	case RTM_ADD:
533 		gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
534 		if (rt->rt_flags & RTF_CLONING) {
535 			/*
536 			 * Give this route an expiration time, even though
537 			 * it's a "permanent" route, so that routes cloned
538 			 * from it do not need their expiration time set.
539 			 */
540 			rt->rt_expire = time_second;
541 			/*
542 			 * linklayers with particular link MTU limitation.
543 			 */
544 			switch (ifp->if_type) {
545 #if NFDDI > 0
546 			case IFT_FDDI:
547 				if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
548 				    (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
549 				     (rt->rt_rmx.rmx_mtu == 0 &&
550 				      ifp->if_mtu > FDDIIPMTU)))
551 					rt->rt_rmx.rmx_mtu = FDDIIPMTU;
552 				break;
553 #endif
554 #if NARC > 0
555 			case IFT_ARCNET:
556 			    {
557 				int arcipifmtu;
558 				if (ifp->if_flags & IFF_LINK0)
559 					arcipifmtu = arc_ipmtu;
560 				else
561 					arcipifmtu = ARCMTU;
562 
563 				if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
564 				    (rt->rt_rmx.rmx_mtu > arcipifmtu ||
565 				     (rt->rt_rmx.rmx_mtu == 0 &&
566 				      ifp->if_mtu > arcipifmtu)))
567 					rt->rt_rmx.rmx_mtu = arcipifmtu;
568 				break;
569 			    }
570 #endif
571 			}
572 			break;
573 		}
574 		/* Announce a new entry if requested. */
575 		if (rt->rt_flags & RTF_ANNOUNCE)
576 			arprequest(ifp,
577 			    &satocsin(rt_getkey(rt))->sin_addr,
578 			    &satocsin(rt_getkey(rt))->sin_addr,
579 			    CLLADDR(satocsdl(gate)));
580 		/*FALLTHROUGH*/
581 	case RTM_RESOLVE:
582 		if (gate->sa_family != AF_LINK ||
583 		    gate->sa_len < sockaddr_dl_measure(namelen, addrlen)) {
584 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
585 			break;
586 		}
587 		satosdl(gate)->sdl_type = ifp->if_type;
588 		satosdl(gate)->sdl_index = ifp->if_index;
589 		if (la != NULL)
590 			break; /* This happens on a route change */
591 		/*
592 		 * Case 2:  This route may come from cloning, or a manual route
593 		 * add with a LL address.
594 		 */
595 		switch (satocsdl(gate)->sdl_type) {
596 #if NTOKEN > 0
597 		case IFT_ISO88025:
598 			allocsize = sizeof(*la) + sizeof(struct token_rif);
599 			break;
600 #endif /* NTOKEN > 0 */
601 		default:
602 			allocsize = sizeof(*la);
603 		}
604 		R_Malloc(la, struct llinfo_arp *, allocsize);
605 		rt->rt_llinfo = (void *)la;
606 		if (la == NULL) {
607 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
608 			break;
609 		}
610 		arp_inuse++, arp_allocated++;
611 		memset(la, 0, allocsize);
612 		la->la_rt = rt;
613 		rt->rt_flags |= RTF_LLINFO;
614 		LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
615 
616 		INADDR_TO_IA(satocsin(rt_getkey(rt))->sin_addr, ia);
617 		while (ia && ia->ia_ifp != ifp)
618 			NEXT_IA_WITH_SAME_ADDR(ia);
619 		if (ia) {
620 			/*
621 			 * This test used to be
622 			 *	if (lo0ifp->if_flags & IFF_UP)
623 			 * It allowed local traffic to be forced through
624 			 * the hardware by configuring the loopback down.
625 			 * However, it causes problems during network
626 			 * configuration for boards that can't receive
627 			 * packets they send.  It is now necessary to clear
628 			 * "useloopback" and remove the route to force
629 			 * traffic out to the hardware.
630 			 *
631 			 * In 4.4BSD, the above "if" statement checked
632 			 * rt->rt_ifa against rt_getkey(rt).  It was changed
633 			 * to the current form so that we can provide a
634 			 * better support for multiple IPv4 addresses on a
635 			 * interface.
636 			 */
637 			rt->rt_expire = 0;
638 			(void)sockaddr_dl_setaddr(satosdl(gate), gate->sa_len,
639 			    CLLADDR(ifp->if_sadl), ifp->if_addrlen);
640 			if (useloopback)
641 				ifp = rt->rt_ifp = lo0ifp;
642 			/*
643 			 * make sure to set rt->rt_ifa to the interface
644 			 * address we are using, otherwise we will have trouble
645 			 * with source address selection.
646 			 */
647 			ifa = &ia->ia_ifa;
648 			if (ifa != rt->rt_ifa)
649 				rt_replace_ifa(rt, ifa);
650 		}
651 		break;
652 
653 	case RTM_DELETE:
654 		if (la == NULL)
655 			break;
656 		arp_inuse--;
657 		LIST_REMOVE(la, la_list);
658 		rt->rt_llinfo = NULL;
659 		rt->rt_flags &= ~RTF_LLINFO;
660 
661 		s = splnet();
662 		mold = la->la_hold;
663 		la->la_hold = 0;
664 		splx(s);
665 
666 		if (mold)
667 			m_freem(mold);
668 
669 		Free((void *)la);
670 	}
671 	ARP_UNLOCK();
672 }
673 
674 /*
675  * Broadcast an ARP request. Caller specifies:
676  *	- arp header source ip address
677  *	- arp header target ip address
678  *	- arp header source ethernet address
679  */
680 void
681 arprequest(struct ifnet *ifp,
682     const struct in_addr *sip, const struct in_addr *tip,
683     const u_int8_t *enaddr)
684 {
685 	struct mbuf *m;
686 	struct arphdr *ah;
687 	struct sockaddr sa;
688 	uint64_t *arps;
689 
690 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
691 		return;
692 	MCLAIM(m, &arpdomain.dom_mowner);
693 	switch (ifp->if_type) {
694 	case IFT_IEEE1394:
695 		m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
696 		    ifp->if_addrlen;
697 		break;
698 	default:
699 		m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
700 		    2 * ifp->if_addrlen;
701 		break;
702 	}
703 	m->m_pkthdr.len = m->m_len;
704 	MH_ALIGN(m, m->m_len);
705 	ah = mtod(m, struct arphdr *);
706 	memset(ah, 0, m->m_len);
707 	switch (ifp->if_type) {
708 	case IFT_IEEE1394:	/* RFC2734 */
709 		/* fill it now for ar_tpa computation */
710 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
711 		break;
712 	default:
713 		/* ifp->if_output will fill ar_hrd */
714 		break;
715 	}
716 	ah->ar_pro = htons(ETHERTYPE_IP);
717 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
718 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
719 	ah->ar_op = htons(ARPOP_REQUEST);
720 	memcpy(ar_sha(ah), enaddr, ah->ar_hln);
721 	memcpy(ar_spa(ah), sip, ah->ar_pln);
722 	memcpy(ar_tpa(ah), tip, ah->ar_pln);
723 	sa.sa_family = AF_ARP;
724 	sa.sa_len = 2;
725 	m->m_flags |= M_BCAST;
726 	arps = ARP_STAT_GETREF();
727 	arps[ARP_STAT_SNDTOTAL]++;
728 	arps[ARP_STAT_SENDREQUEST]++;
729 	ARP_STAT_PUTREF();
730 	(*ifp->if_output)(ifp, m, &sa, NULL);
731 }
732 
733 /*
734  * Resolve an IP address into an ethernet address.  If success,
735  * desten is filled in.  If there is no entry in arptab,
736  * set one up and broadcast a request for the IP address.
737  * Hold onto this mbuf and resend it once the address
738  * is finally resolved.  A return value of 1 indicates
739  * that desten has been filled in and the packet should be sent
740  * normally; a 0 return indicates that the packet has been
741  * taken over here, either now or for later transmission.
742  */
743 int
744 arpresolve(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m,
745     const struct sockaddr *dst, u_char *desten)
746 {
747 	struct llinfo_arp *la;
748 	const struct sockaddr_dl *sdl;
749 	struct mbuf *mold;
750 	int s;
751 
752 	if ((la = arplookup1(m, &satocsin(dst)->sin_addr, 1, 0, rt)) != NULL)
753 		rt = la->la_rt;
754 
755 	if (la == NULL || rt == NULL) {
756 		ARP_STATINC(ARP_STAT_ALLOCFAIL);
757 		log(LOG_DEBUG,
758 		    "arpresolve: can't allocate llinfo on %s for %s\n",
759 		    ifp->if_xname, in_fmtaddr(satocsin(dst)->sin_addr));
760 		m_freem(m);
761 		return 0;
762 	}
763 	sdl = satocsdl(rt->rt_gateway);
764 	/*
765 	 * Check the address family and length is valid, the address
766 	 * is resolved; otherwise, try to resolve.
767 	 */
768 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
769 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
770 		memcpy(desten, CLLADDR(sdl),
771 		    min(sdl->sdl_alen, ifp->if_addrlen));
772 		rt->rt_pksent = time_second; /* Time for last pkt sent */
773 		return 1;
774 	}
775 	/*
776 	 * There is an arptab entry, but no ethernet address
777 	 * response yet.  Replace the held mbuf with this
778 	 * latest one.
779 	 */
780 
781 	ARP_STATINC(ARP_STAT_DFRTOTAL);
782 	s = splnet();
783 	mold = la->la_hold;
784 	la->la_hold = m;
785 	splx(s);
786 
787 	if (mold) {
788 		ARP_STATINC(ARP_STAT_DFRDROPPED);
789 		m_freem(mold);
790 	}
791 
792 	/*
793 	 * Re-send the ARP request when appropriate.
794 	 */
795 #ifdef	DIAGNOSTIC
796 	if (rt->rt_expire == 0) {
797 		/* This should never happen. (Should it? -gwr) */
798 		printf("arpresolve: unresolved and rt_expire == 0\n");
799 		/* Set expiration time to now (expired). */
800 		rt->rt_expire = time_second;
801 	}
802 #endif
803 	if (rt->rt_expire) {
804 		rt->rt_flags &= ~RTF_REJECT;
805 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
806 			rt->rt_expire = time_second;
807 			if (la->la_asked++ < arp_maxtries)
808 				arprequest(ifp,
809 				    &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
810 				    &satocsin(dst)->sin_addr,
811 #if NCARP > 0
812 				    (rt->rt_ifp->if_type == IFT_CARP) ?
813 				    CLLADDR(rt->rt_ifp->if_sadl):
814 #endif
815 				    CLLADDR(ifp->if_sadl));
816 			else {
817 				rt->rt_flags |= RTF_REJECT;
818 				rt->rt_expire += arpt_down;
819 				la->la_asked = 0;
820 			}
821 		}
822 	}
823 	return 0;
824 }
825 
826 /*
827  * Common length and type checks are done here,
828  * then the protocol-specific routine is called.
829  */
830 void
831 arpintr(void)
832 {
833 	struct mbuf *m;
834 	struct arphdr *ar;
835 	int s;
836 	int arplen;
837 
838 	mutex_enter(softnet_lock);
839 	KERNEL_LOCK(1, NULL);
840 	while (arpintrq.ifq_head) {
841 		s = splnet();
842 		IF_DEQUEUE(&arpintrq, m);
843 		splx(s);
844 		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
845 			panic("arpintr");
846 
847 		MCLAIM(m, &arpdomain.dom_mowner);
848 		ARP_STATINC(ARP_STAT_RCVTOTAL);
849 
850 		/*
851 		 * First, make sure we have at least struct arphdr.
852 		 */
853 		if (m->m_len < sizeof(struct arphdr) ||
854 		    (ar = mtod(m, struct arphdr *)) == NULL)
855 			goto badlen;
856 
857 		switch (m->m_pkthdr.rcvif->if_type) {
858 		case IFT_IEEE1394:
859 			arplen = sizeof(struct arphdr) +
860 			    ar->ar_hln + 2 * ar->ar_pln;
861 			break;
862 		default:
863 			arplen = sizeof(struct arphdr) +
864 			    2 * ar->ar_hln + 2 * ar->ar_pln;
865 			break;
866 		}
867 
868 		if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
869 		    m->m_len >= arplen)
870 			switch (ntohs(ar->ar_pro)) {
871 			case ETHERTYPE_IP:
872 			case ETHERTYPE_IPTRAILERS:
873 				in_arpinput(m);
874 				continue;
875 			default:
876 				ARP_STATINC(ARP_STAT_RCVBADPROTO);
877 			}
878 		else {
879 badlen:
880 			ARP_STATINC(ARP_STAT_RCVBADLEN);
881 		}
882 		m_freem(m);
883 	}
884 	KERNEL_UNLOCK_ONE(NULL);
885 	mutex_exit(softnet_lock);
886 }
887 
888 /*
889  * ARP for Internet protocols on 10 Mb/s Ethernet.
890  * Algorithm is that given in RFC 826.
891  * In addition, a sanity check is performed on the sender
892  * protocol address, to catch impersonators.
893  * We no longer handle negotiations for use of trailer protocol:
894  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
895  * along with IP replies if we wanted trailers sent to us,
896  * and also sent them in response to IP replies.
897  * This allowed either end to announce the desire to receive
898  * trailer packets.
899  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
900  * but formerly didn't normally send requests.
901  */
902 static void
903 in_arpinput(struct mbuf *m)
904 {
905 	struct arphdr *ah;
906 	struct ifnet *ifp = m->m_pkthdr.rcvif;
907 	struct llinfo_arp *la = NULL;
908 	struct rtentry  *rt;
909 	struct in_ifaddr *ia;
910 #if NBRIDGE > 0
911 	struct in_ifaddr *bridge_ia = NULL;
912 #endif
913 #if NCARP > 0
914 	u_int32_t count = 0, index = 0;
915 #endif
916 	struct sockaddr_dl *sdl;
917 	struct sockaddr sa;
918 	struct in_addr isaddr, itaddr, myaddr;
919 	int op;
920 	struct mbuf *mold;
921 	void *tha;
922 	int s;
923 	uint64_t *arps;
924 
925 	if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
926 		goto out;
927 	ah = mtod(m, struct arphdr *);
928 	op = ntohs(ah->ar_op);
929 
930 	/*
931 	 * Fix up ah->ar_hrd if necessary, before using ar_tha() or
932 	 * ar_tpa().
933 	 */
934 	switch (ifp->if_type) {
935 	case IFT_IEEE1394:
936 		if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
937 			;
938 		else {
939 			/* XXX this is to make sure we compute ar_tha right */
940 			/* XXX check ar_hrd more strictly? */
941 			ah->ar_hrd = htons(ARPHRD_IEEE1394);
942 		}
943 		break;
944 	default:
945 		/* XXX check ar_hrd? */
946 		break;
947 	}
948 
949 	memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
950 	memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
951 
952 	if (m->m_flags & (M_BCAST|M_MCAST))
953 		ARP_STATINC(ARP_STAT_RCVMCAST);
954 
955 	/*
956 	 * If the target IP address is zero, ignore the packet.
957 	 * This prevents the code below from tring to answer
958 	 * when we are using IP address zero (booting).
959 	 */
960 	if (in_nullhost(itaddr)) {
961 		ARP_STATINC(ARP_STAT_RCVZEROTPA);
962 		goto out;
963 	}
964 
965 	/*
966 	 * If the source IP address is zero, this is most likely a
967 	 * confused host trying to use IP address zero. (Windoze?)
968 	 * XXX: Should we bother trying to reply to these?
969 	 */
970 	if (in_nullhost(isaddr)) {
971 		ARP_STATINC(ARP_STAT_RCVZEROSPA);
972 		goto out;
973 	}
974 
975 	/*
976 	 * Search for a matching interface address
977 	 * or any address on the interface to use
978 	 * as a dummy address in the rest of this function
979 	 */
980 
981 	INADDR_TO_IA(itaddr, ia);
982 	while (ia != NULL) {
983 #if NCARP > 0
984 		if (ia->ia_ifp->if_type == IFT_CARP &&
985 		    ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
986 		    (IFF_UP|IFF_RUNNING))) {
987 			index++;
988 			if (ia->ia_ifp == m->m_pkthdr.rcvif &&
989 			    carp_iamatch(ia, ar_sha(ah),
990 			    &count, index)) {
991 				break;
992 				}
993 		} else
994 #endif
995 			    if (ia->ia_ifp == m->m_pkthdr.rcvif)
996 				break;
997 #if NBRIDGE > 0
998 		/*
999 		 * If the interface we received the packet on
1000 		 * is part of a bridge, check to see if we need
1001 		 * to "bridge" the packet to ourselves at this
1002 		 * layer.  Note we still prefer a perfect match,
1003 		 * but allow this weaker match if necessary.
1004 		 */
1005 		if (m->m_pkthdr.rcvif->if_bridge != NULL &&
1006 		    m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge)
1007 			bridge_ia = ia;
1008 #endif /* NBRIDGE > 0 */
1009 
1010 		NEXT_IA_WITH_SAME_ADDR(ia);
1011 	}
1012 
1013 #if NBRIDGE > 0
1014 	if (ia == NULL && bridge_ia != NULL) {
1015 		ia = bridge_ia;
1016 		ifp = bridge_ia->ia_ifp;
1017 	}
1018 #endif
1019 
1020 	if (ia == NULL) {
1021 		INADDR_TO_IA(isaddr, ia);
1022 		while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
1023 			NEXT_IA_WITH_SAME_ADDR(ia);
1024 
1025 		if (ia == NULL) {
1026 			IFP_TO_IA(ifp, ia);
1027 			if (ia == NULL) {
1028 				ARP_STATINC(ARP_STAT_RCVNOINT);
1029 				goto out;
1030 			}
1031 		}
1032 	}
1033 
1034 	myaddr = ia->ia_addr.sin_addr;
1035 
1036 	/* XXX checks for bridge case? */
1037 	if (!memcmp(ar_sha(ah), CLLADDR(ifp->if_sadl), ifp->if_addrlen)) {
1038 		ARP_STATINC(ARP_STAT_RCVLOCALSHA);
1039 		goto out;	/* it's from me, ignore it. */
1040 	}
1041 
1042 	/* XXX checks for bridge case? */
1043 	if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
1044 		ARP_STATINC(ARP_STAT_RCVBCASTSHA);
1045 		log(LOG_ERR,
1046 		    "%s: arp: link address is broadcast for IP address %s!\n",
1047 		    ifp->if_xname, in_fmtaddr(isaddr));
1048 		goto out;
1049 	}
1050 
1051 	if (in_hosteq(isaddr, myaddr)) {
1052 		ARP_STATINC(ARP_STAT_RCVLOCALSPA);
1053 		log(LOG_ERR,
1054 		   "duplicate IP address %s sent from link address %s\n",
1055 		   in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
1056 		itaddr = myaddr;
1057 		goto reply;
1058 	}
1059 	la = arplookup(m, &isaddr, in_hosteq(itaddr, myaddr), 0);
1060 	if (la != NULL && (rt = la->la_rt) && (sdl = satosdl(rt->rt_gateway))) {
1061 		if (sdl->sdl_alen &&
1062 		    memcmp(ar_sha(ah), CLLADDR(sdl), sdl->sdl_alen)) {
1063 			if (rt->rt_flags & RTF_STATIC) {
1064 				ARP_STATINC(ARP_STAT_RCVOVERPERM);
1065 				log(LOG_INFO,
1066 				    "%s tried to overwrite permanent arp info"
1067 				    " for %s\n",
1068 				    lla_snprintf(ar_sha(ah), ah->ar_hln),
1069 				    in_fmtaddr(isaddr));
1070 				goto out;
1071 			} else if (rt->rt_ifp != ifp) {
1072 				ARP_STATINC(ARP_STAT_RCVOVERINT);
1073 				log(LOG_INFO,
1074 				    "%s on %s tried to overwrite "
1075 				    "arp info for %s on %s\n",
1076 				    lla_snprintf(ar_sha(ah), ah->ar_hln),
1077 				    ifp->if_xname, in_fmtaddr(isaddr),
1078 				    rt->rt_ifp->if_xname);
1079 				    goto out;
1080 			} else {
1081 				ARP_STATINC(ARP_STAT_RCVOVER);
1082 				log(LOG_INFO,
1083 				    "arp info overwritten for %s by %s\n",
1084 				    in_fmtaddr(isaddr),
1085 				    lla_snprintf(ar_sha(ah), ah->ar_hln));
1086 			}
1087 		}
1088 		/*
1089 		 * sanity check for the address length.
1090 		 * XXX this does not work for protocols with variable address
1091 		 * length. -is
1092 		 */
1093 		if (sdl->sdl_alen &&
1094 		    sdl->sdl_alen != ah->ar_hln) {
1095 			ARP_STATINC(ARP_STAT_RCVLENCHG);
1096 			log(LOG_WARNING,
1097 			    "arp from %s: new addr len %d, was %d\n",
1098 			    in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
1099 		}
1100 		if (ifp->if_addrlen != ah->ar_hln) {
1101 			ARP_STATINC(ARP_STAT_RCVBADLEN);
1102 			log(LOG_WARNING,
1103 			    "arp from %s: addr len: new %d, i/f %d (ignored)\n",
1104 			    in_fmtaddr(isaddr), ah->ar_hln,
1105 			    ifp->if_addrlen);
1106 			goto reply;
1107 		}
1108 #if NTOKEN > 0
1109 		/*
1110 		 * XXX uses m_data and assumes the complete answer including
1111 		 * XXX token-ring headers is in the same buf
1112 		 */
1113 		if (ifp->if_type == IFT_ISO88025) {
1114 			struct token_header *trh;
1115 
1116 			trh = (struct token_header *)M_TRHSTART(m);
1117 			if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
1118 				struct token_rif	*rif;
1119 				size_t	riflen;
1120 
1121 				rif = TOKEN_RIF(trh);
1122 				riflen = (ntohs(rif->tr_rcf) &
1123 				    TOKEN_RCF_LEN_MASK) >> 8;
1124 
1125 				if (riflen > 2 &&
1126 				    riflen < sizeof(struct token_rif) &&
1127 				    (riflen & 1) == 0) {
1128 					rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
1129 					rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
1130 					memcpy(TOKEN_RIF(la), rif, riflen);
1131 				}
1132 			}
1133 		}
1134 #endif /* NTOKEN > 0 */
1135 		(void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, ar_sha(ah),
1136 		    ah->ar_hln);
1137 		if (rt->rt_expire)
1138 			rt->rt_expire = time_second + arpt_keep;
1139 		rt->rt_flags &= ~RTF_REJECT;
1140 		la->la_asked = 0;
1141 
1142 		s = splnet();
1143 		mold = la->la_hold;
1144 		la->la_hold = 0;
1145 		splx(s);
1146 
1147 		if (mold) {
1148 			ARP_STATINC(ARP_STAT_DFRSENT);
1149 			(*ifp->if_output)(ifp, mold, rt_getkey(rt), rt);
1150 		}
1151 	}
1152 reply:
1153 	if (op != ARPOP_REQUEST) {
1154 		if (op == ARPOP_REPLY)
1155 			ARP_STATINC(ARP_STAT_RCVREPLY);
1156 	out:
1157 		m_freem(m);
1158 		return;
1159 	}
1160 	ARP_STATINC(ARP_STAT_RCVREQUEST);
1161 	if (in_hosteq(itaddr, myaddr)) {
1162 		/* I am the target */
1163 		tha = ar_tha(ah);
1164 		if (tha)
1165 			memcpy(tha, ar_sha(ah), ah->ar_hln);
1166 		memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1167 	} else {
1168 		la = arplookup(m, &itaddr, 0, SIN_PROXY);
1169 		if (la == NULL)
1170 			goto out;
1171 		rt = la->la_rt;
1172 		if (rt->rt_ifp->if_type == IFT_CARP &&
1173 		    m->m_pkthdr.rcvif->if_type != IFT_CARP)
1174 			goto out;
1175 		tha = ar_tha(ah);
1176 		if (tha)
1177 			memcpy(tha, ar_sha(ah), ah->ar_hln);
1178 		sdl = satosdl(rt->rt_gateway);
1179 		memcpy(ar_sha(ah), CLLADDR(sdl), ah->ar_hln);
1180 	}
1181 
1182 	memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
1183 	memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
1184 	ah->ar_op = htons(ARPOP_REPLY);
1185 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
1186 	switch (ifp->if_type) {
1187 	case IFT_IEEE1394:
1188 		/*
1189 		 * ieee1394 arp reply is broadcast
1190 		 */
1191 		m->m_flags &= ~M_MCAST;
1192 		m->m_flags |= M_BCAST;
1193 		m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
1194 		break;
1195 
1196 	default:
1197 		m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
1198 		m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
1199 		break;
1200 	}
1201 	m->m_pkthdr.len = m->m_len;
1202 	sa.sa_family = AF_ARP;
1203 	sa.sa_len = 2;
1204 	arps = ARP_STAT_GETREF();
1205 	arps[ARP_STAT_SNDTOTAL]++;
1206 	arps[ARP_STAT_SNDREPLY]++;
1207 	ARP_STAT_PUTREF();
1208 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
1209 	return;
1210 }
1211 
1212 /*
1213  * Free an arp entry.
1214  */
1215 static void arptfree(struct llinfo_arp *la)
1216 {
1217 	struct rtentry *rt = la->la_rt;
1218 	struct sockaddr_dl *sdl;
1219 
1220 	ARP_LOCK_CHECK();
1221 
1222 	if (rt == NULL)
1223 		panic("arptfree");
1224 	if (rt->rt_refcnt > 0 && (sdl = satosdl(rt->rt_gateway)) &&
1225 	    sdl->sdl_family == AF_LINK) {
1226 		sdl->sdl_alen = 0;
1227 		la->la_asked = 0;
1228 		rt->rt_flags &= ~RTF_REJECT;
1229 		return;
1230 	}
1231 	rtrequest(RTM_DELETE, rt_getkey(rt), NULL, rt_mask(rt), 0, NULL);
1232 }
1233 
1234 static struct llinfo_arp *
1235 arplookup(struct mbuf *m, const struct in_addr *addr, int create, int proxy)
1236 {
1237 	return arplookup1(m, addr, create, proxy, NULL);
1238 }
1239 
1240 /*
1241  * Lookup or enter a new address in arptab.
1242  */
1243 static struct llinfo_arp *
1244 arplookup1(struct mbuf *m, const struct in_addr *addr, int create, int proxy,
1245     struct rtentry *rt0)
1246 {
1247 	struct arphdr *ah;
1248 	struct ifnet *ifp = m->m_pkthdr.rcvif;
1249 	struct rtentry *rt;
1250 	struct sockaddr_inarp sin;
1251 	const char *why = NULL;
1252 
1253 	ah = mtod(m, struct arphdr *);
1254 	if (rt0 == NULL) {
1255 		memset(&sin, 0, sizeof(sin));
1256 		sin.sin_len = sizeof(sin);
1257 		sin.sin_family = AF_INET;
1258 		sin.sin_addr = *addr;
1259 		sin.sin_other = proxy ? SIN_PROXY : 0;
1260 		rt = rtalloc1(sintosa(&sin), create);
1261 		if (rt == NULL)
1262 			return NULL;
1263 		rt->rt_refcnt--;
1264 	} else
1265 		rt = rt0;
1266 
1267 #define	IS_LLINFO(__rt)							  \
1268 	(((__rt)->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) == RTF_LLINFO && \
1269 	 (__rt)->rt_gateway->sa_family == AF_LINK)
1270 
1271 
1272 	if (IS_LLINFO(rt))
1273 		return (struct llinfo_arp *)rt->rt_llinfo;
1274 
1275 	if (create) {
1276 		if (rt->rt_flags & RTF_GATEWAY)
1277 			why = "host is not on local network";
1278 		else if ((rt->rt_flags & RTF_LLINFO) == 0) {
1279 			ARP_STATINC(ARP_STAT_ALLOCFAIL);
1280 			why = "could not allocate llinfo";
1281 		} else
1282 			why = "gateway route is not ours";
1283 		log(LOG_DEBUG, "arplookup: unable to enter address"
1284 		    " for %s@%s on %s (%s)\n",
1285 		    in_fmtaddr(*addr), lla_snprintf(ar_sha(ah), ah->ar_hln),
1286 		    (ifp) ? ifp->if_xname : "null", why);
1287 		if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_CLONED) != 0) {
1288 			rtrequest(RTM_DELETE, rt_getkey(rt),
1289 		    	    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
1290 		}
1291 	}
1292 	return NULL;
1293 }
1294 
1295 int
1296 arpioctl(u_long cmd, void *data)
1297 {
1298 
1299 	return EOPNOTSUPP;
1300 }
1301 
1302 void
1303 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
1304 {
1305 	struct in_addr *ip;
1306 
1307 	/*
1308 	 * Warn the user if another station has this IP address,
1309 	 * but only if the interface IP address is not zero.
1310 	 */
1311 	ip = &IA_SIN(ifa)->sin_addr;
1312 	if (!in_nullhost(*ip))
1313 		arprequest(ifp, ip, ip, CLLADDR(ifp->if_sadl));
1314 
1315 	ifa->ifa_rtrequest = arp_rtrequest;
1316 	ifa->ifa_flags |= RTF_CLONING;
1317 }
1318 
1319 /*
1320  * Called from 10 Mb/s Ethernet interrupt handlers
1321  * when ether packet type ETHERTYPE_REVARP
1322  * is received.  Common length and type checks are done here,
1323  * then the protocol-specific routine is called.
1324  */
1325 void
1326 revarpinput(struct mbuf *m)
1327 {
1328 	struct arphdr *ar;
1329 
1330 	if (m->m_len < sizeof(struct arphdr))
1331 		goto out;
1332 	ar = mtod(m, struct arphdr *);
1333 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
1334 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
1335 		goto out;
1336 #endif
1337 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
1338 		goto out;
1339 	switch (ntohs(ar->ar_pro)) {
1340 	case ETHERTYPE_IP:
1341 	case ETHERTYPE_IPTRAILERS:
1342 		in_revarpinput(m);
1343 		return;
1344 
1345 	default:
1346 		break;
1347 	}
1348 out:
1349 	m_freem(m);
1350 }
1351 
1352 /*
1353  * RARP for Internet protocols on 10 Mb/s Ethernet.
1354  * Algorithm is that given in RFC 903.
1355  * We are only using for bootstrap purposes to get an ip address for one of
1356  * our interfaces.  Thus we support no user-interface.
1357  *
1358  * Since the contents of the RARP reply are specific to the interface that
1359  * sent the request, this code must ensure that they are properly associated.
1360  *
1361  * Note: also supports ARP via RARP packets, per the RFC.
1362  */
1363 void
1364 in_revarpinput(struct mbuf *m)
1365 {
1366 	struct ifnet *ifp;
1367 	struct arphdr *ah;
1368 	void *tha;
1369 	int op;
1370 
1371 	ah = mtod(m, struct arphdr *);
1372 	op = ntohs(ah->ar_op);
1373 
1374 	switch (m->m_pkthdr.rcvif->if_type) {
1375 	case IFT_IEEE1394:
1376 		/* ARP without target hardware address is not supported */
1377 		goto out;
1378 	default:
1379 		break;
1380 	}
1381 
1382 	switch (op) {
1383 	case ARPOP_REQUEST:
1384 	case ARPOP_REPLY:	/* per RFC */
1385 		in_arpinput(m);
1386 		return;
1387 	case ARPOP_REVREPLY:
1388 		break;
1389 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
1390 	default:
1391 		goto out;
1392 	}
1393 	if (!revarp_in_progress)
1394 		goto out;
1395 	ifp = m->m_pkthdr.rcvif;
1396 	if (ifp != myip_ifp) /* !same interface */
1397 		goto out;
1398 	if (myip_initialized)
1399 		goto wake;
1400 	tha = ar_tha(ah);
1401 	KASSERT(tha);
1402 	if (memcmp(tha, CLLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
1403 		goto out;
1404 	memcpy(&srv_ip, ar_spa(ah), sizeof(srv_ip));
1405 	memcpy(&myip, ar_tpa(ah), sizeof(myip));
1406 	myip_initialized = 1;
1407 wake:	/* Do wakeup every time in case it was missed. */
1408 	wakeup((void *)&myip);
1409 
1410 out:
1411 	m_freem(m);
1412 }
1413 
1414 /*
1415  * Send a RARP request for the ip address of the specified interface.
1416  * The request should be RFC 903-compliant.
1417  */
1418 void
1419 revarprequest(struct ifnet *ifp)
1420 {
1421 	struct sockaddr sa;
1422 	struct mbuf *m;
1423 	struct arphdr *ah;
1424 	void *tha;
1425 
1426 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
1427 		return;
1428 	MCLAIM(m, &arpdomain.dom_mowner);
1429 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
1430 	    2*ifp->if_addrlen;
1431 	m->m_pkthdr.len = m->m_len;
1432 	MH_ALIGN(m, m->m_len);
1433 	ah = mtod(m, struct arphdr *);
1434 	memset(ah, 0, m->m_len);
1435 	ah->ar_pro = htons(ETHERTYPE_IP);
1436 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
1437 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
1438 	ah->ar_op = htons(ARPOP_REVREQUEST);
1439 
1440 	memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
1441 	tha = ar_tha(ah);
1442 	KASSERT(tha);
1443 	memcpy(tha, CLLADDR(ifp->if_sadl), ah->ar_hln);
1444 
1445 	sa.sa_family = AF_ARP;
1446 	sa.sa_len = 2;
1447 	m->m_flags |= M_BCAST;
1448 	(*ifp->if_output)(ifp, m, &sa, NULL);
1449 
1450 }
1451 
1452 /*
1453  * RARP for the ip address of the specified interface, but also
1454  * save the ip address of the server that sent the answer.
1455  * Timeout if no response is received.
1456  */
1457 int
1458 revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in,
1459     struct in_addr *clnt_in)
1460 {
1461 	int result, count = 20;
1462 
1463 	myip_initialized = 0;
1464 	myip_ifp = ifp;
1465 
1466 	revarp_in_progress = 1;
1467 	while (count--) {
1468 		revarprequest(ifp);
1469 		result = tsleep((void *)&myip, PSOCK, "revarp", hz/2);
1470 		if (result != EWOULDBLOCK)
1471 			break;
1472 	}
1473 	revarp_in_progress = 0;
1474 
1475 	if (!myip_initialized)
1476 		return ENETUNREACH;
1477 
1478 	memcpy(serv_in, &srv_ip, sizeof(*serv_in));
1479 	memcpy(clnt_in, &myip, sizeof(*clnt_in));
1480 	return 0;
1481 }
1482 
1483 
1484 
1485 #ifdef DDB
1486 
1487 #include <machine/db_machdep.h>
1488 #include <ddb/db_interface.h>
1489 #include <ddb/db_output.h>
1490 
1491 static void
1492 db_print_sa(const struct sockaddr *sa)
1493 {
1494 	int len;
1495 	const u_char *p;
1496 
1497 	if (sa == NULL) {
1498 		db_printf("[NULL]");
1499 		return;
1500 	}
1501 
1502 	p = (const u_char *)sa;
1503 	len = sa->sa_len;
1504 	db_printf("[");
1505 	while (len > 0) {
1506 		db_printf("%d", *p);
1507 		p++; len--;
1508 		if (len) db_printf(",");
1509 	}
1510 	db_printf("]\n");
1511 }
1512 
1513 static void
1514 db_print_ifa(struct ifaddr *ifa)
1515 {
1516 	if (ifa == NULL)
1517 		return;
1518 	db_printf("  ifa_addr=");
1519 	db_print_sa(ifa->ifa_addr);
1520 	db_printf("  ifa_dsta=");
1521 	db_print_sa(ifa->ifa_dstaddr);
1522 	db_printf("  ifa_mask=");
1523 	db_print_sa(ifa->ifa_netmask);
1524 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
1525 			  ifa->ifa_flags,
1526 			  ifa->ifa_refcnt,
1527 			  ifa->ifa_metric);
1528 }
1529 
1530 static void
1531 db_print_llinfo(void *li)
1532 {
1533 	struct llinfo_arp *la;
1534 
1535 	if (li == NULL)
1536 		return;
1537 	la = (struct llinfo_arp *)li;
1538 	db_printf("  la_rt=%p la_hold=%p, la_asked=0x%lx\n",
1539 			  la->la_rt, la->la_hold, la->la_asked);
1540 }
1541 
1542 /*
1543  * Function to pass to rt_walktree().
1544  * Return non-zero error to abort walk.
1545  */
1546 static int
1547 db_show_rtentry(struct rtentry *rt, void *w)
1548 {
1549 	db_printf("rtentry=%p", rt);
1550 
1551 	db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
1552 			  rt->rt_flags, rt->rt_refcnt,
1553 			  rt->rt_use, rt->rt_expire);
1554 
1555 	db_printf(" key="); db_print_sa(rt_getkey(rt));
1556 	db_printf(" mask="); db_print_sa(rt_mask(rt));
1557 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
1558 
1559 	db_printf(" ifp=%p ", rt->rt_ifp);
1560 	if (rt->rt_ifp)
1561 		db_printf("(%s)", rt->rt_ifp->if_xname);
1562 	else
1563 		db_printf("(NULL)");
1564 
1565 	db_printf(" ifa=%p\n", rt->rt_ifa);
1566 	db_print_ifa(rt->rt_ifa);
1567 
1568 	db_printf(" gwroute=%p llinfo=%p\n",
1569 			  rt->rt_gwroute, rt->rt_llinfo);
1570 	db_print_llinfo(rt->rt_llinfo);
1571 
1572 	return 0;
1573 }
1574 
1575 /*
1576  * Function to print all the route trees.
1577  * Use this from ddb:  "show arptab"
1578  */
1579 void
1580 db_show_arptab(db_expr_t addr, bool have_addr,
1581     db_expr_t count, const char *modif)
1582 {
1583 	rt_walktree(AF_INET, db_show_rtentry, NULL);
1584 }
1585 #endif
1586 
1587 static int
1588 sysctl_net_inet_arp_stats(SYSCTLFN_ARGS)
1589 {
1590 
1591 	return NETSTAT_SYSCTL(arpstat_percpu, ARP_NSTATS);
1592 }
1593 
1594 SYSCTL_SETUP(sysctl_net_inet_arp_setup, "sysctl net.inet.arp subtree setup")
1595 {
1596 	const struct sysctlnode *node;
1597 
1598 	sysctl_createv(clog, 0, NULL, NULL,
1599 			CTLFLAG_PERMANENT,
1600 			CTLTYPE_NODE, "net", NULL,
1601 			NULL, 0, NULL, 0,
1602 			CTL_NET, CTL_EOL);
1603 	sysctl_createv(clog, 0, NULL, NULL,
1604 			CTLFLAG_PERMANENT,
1605 			CTLTYPE_NODE, "inet", NULL,
1606 			NULL, 0, NULL, 0,
1607 			CTL_NET, PF_INET, CTL_EOL);
1608 	sysctl_createv(clog, 0, NULL, &node,
1609 			CTLFLAG_PERMANENT,
1610 			CTLTYPE_NODE, "arp",
1611 			SYSCTL_DESCR("Address Resolution Protocol"),
1612 			NULL, 0, NULL, 0,
1613 			CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
1614 
1615 	sysctl_createv(clog, 0, NULL, NULL,
1616 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1617 			CTLTYPE_INT, "prune",
1618 			SYSCTL_DESCR("ARP cache pruning interval"),
1619 			NULL, 0, &arpt_prune, 0,
1620 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1621 
1622 	sysctl_createv(clog, 0, NULL, NULL,
1623 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1624 			CTLTYPE_INT, "keep",
1625 			SYSCTL_DESCR("Valid ARP entry lifetime"),
1626 			NULL, 0, &arpt_keep, 0,
1627 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1628 
1629 	sysctl_createv(clog, 0, NULL, NULL,
1630 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1631 			CTLTYPE_INT, "down",
1632 			SYSCTL_DESCR("Failed ARP entry lifetime"),
1633 			NULL, 0, &arpt_down, 0,
1634 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1635 
1636 	sysctl_createv(clog, 0, NULL, NULL,
1637 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1638 			CTLTYPE_INT, "refresh",
1639 			SYSCTL_DESCR("ARP entry refresh interval"),
1640 			NULL, 0, &arpt_refresh, 0,
1641 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1642 
1643 	sysctl_createv(clog, 0, NULL, NULL,
1644 			CTLFLAG_PERMANENT,
1645 			CTLTYPE_STRUCT, "stats",
1646 			SYSCTL_DESCR("ARP statistics"),
1647 			sysctl_net_inet_arp_stats, 0, NULL, 0,
1648 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
1649 }
1650 
1651 #endif /* INET */
1652