xref: /netbsd-src/sys/netinet/if_arp.c (revision 27578b9aac214cc7796ead81dcc5427e79d5f2a0)
1 /*	$NetBSD: if_arp.c,v 1.78 2001/08/20 03:13:45 itojun Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2000 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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Copyright (c) 1982, 1986, 1988, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *	This product includes software developed by the University of
55  *	California, Berkeley and its contributors.
56  * 4. Neither the name of the University nor the names of its contributors
57  *    may be used to endorse or promote products derived from this software
58  *    without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  *	@(#)if_ether.c	8.2 (Berkeley) 9/26/94
73  */
74 
75 /*
76  * Ethernet address resolution protocol.
77  * TODO:
78  *	add "inuse/lock" bit (or ref. count) along with valid bit
79  */
80 
81 #include "opt_ddb.h"
82 #include "opt_inet.h"
83 
84 #ifdef INET
85 
86 #include "bridge.h"
87 
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/callout.h>
91 #include <sys/malloc.h>
92 #include <sys/mbuf.h>
93 #include <sys/socket.h>
94 #include <sys/time.h>
95 #include <sys/kernel.h>
96 #include <sys/errno.h>
97 #include <sys/ioctl.h>
98 #include <sys/syslog.h>
99 #include <sys/proc.h>
100 #include <sys/protosw.h>
101 #include <sys/domain.h>
102 
103 #include <net/ethertypes.h>
104 #include <net/if.h>
105 #include <net/if_dl.h>
106 #include <net/if_token.h>
107 #include <net/if_types.h>
108 #include <net/route.h>
109 
110 
111 #include <netinet/in.h>
112 #include <netinet/in_systm.h>
113 #include <netinet/in_var.h>
114 #include <netinet/ip.h>
115 #include <netinet/if_inarp.h>
116 
117 #include "loop.h"
118 #include "arc.h"
119 #if NARC > 0
120 #include <net/if_arc.h>
121 #endif
122 #include "fddi.h"
123 #if NFDDI > 0
124 #include <net/if_fddi.h>
125 #endif
126 #include "token.h"
127 #include "token.h"
128 
129 #define SIN(s) ((struct sockaddr_in *)s)
130 #define SDL(s) ((struct sockaddr_dl *)s)
131 #define SRP(s) ((struct sockaddr_inarp *)s)
132 
133 /*
134  * ARP trailer negotiation.  Trailer protocol is not IP specific,
135  * but ARP request/response use IP addresses.
136  */
137 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
138 
139 /* timer values */
140 int	arpt_prune = (5*60*1);	/* walk list every 5 minutes */
141 int	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
142 int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
143 #define	rt_expire rt_rmx.rmx_expire
144 
145 static	void arprequest __P((struct ifnet *,
146 	    struct in_addr *, struct in_addr *, u_int8_t *));
147 static	void arptfree __P((struct llinfo_arp *));
148 static	void arptimer __P((void *));
149 static	struct llinfo_arp *arplookup __P((struct mbuf *, struct in_addr *,
150 					  int, int));
151 static	void in_arpinput __P((struct mbuf *));
152 
153 #if NLOOP > 0
154 extern	struct ifnet loif[NLOOP];
155 #endif
156 LIST_HEAD(, llinfo_arp) llinfo_arp;
157 struct	ifqueue arpintrq = {0, 0, 0, 50};
158 int	arp_inuse, arp_allocated, arp_intimer;
159 int	arp_maxtries = 5;
160 int	useloopback = 1;	/* use loopback interface for local traffic */
161 int	arpinit_done = 0;
162 
163 struct	arpstat arpstat;
164 struct	callout arptimer_ch;
165 
166 
167 /* revarp state */
168 static struct	in_addr myip, srv_ip;
169 static int	myip_initialized = 0;
170 static int	revarp_in_progress = 0;
171 static struct	ifnet *myip_ifp = NULL;
172 
173 #ifdef DDB
174 static void db_print_sa __P((struct sockaddr *));
175 static void db_print_ifa __P((struct ifaddr *));
176 static void db_print_llinfo __P((caddr_t));
177 static int db_show_radix_node __P((struct radix_node *, void *));
178 #endif
179 
180 /*
181  * this should be elsewhere.
182  */
183 
184 static char *
185 lla_snprintf __P((u_int8_t *, int));
186 
187 static char *
188 lla_snprintf(adrp, len)
189 	u_int8_t *adrp;
190 	int len;
191 {
192 #define NUMBUFS 3
193 	static char buf[NUMBUFS][16*3];
194 	static int bnum = 0;
195 	static const char hexdigits[] = {
196 	    '0','1','2','3','4','5','6','7',
197 	    '8','9','a','b','c','d','e','f'
198 	};
199 
200 	int i;
201 	char *p;
202 
203 	p = buf[bnum];
204 
205 	*p++ = hexdigits[(*adrp)>>4];
206 	*p++ = hexdigits[(*adrp++)&0xf];
207 
208 	for (i=1; i<len && i<16; i++) {
209 		*p++ = ':';
210 		*p++ = hexdigits[(*adrp)>>4];
211 		*p++ = hexdigits[(*adrp++)&0xf];
212 	}
213 
214 	*p = 0;
215 	p = buf[bnum];
216 	bnum = (bnum + 1) % NUMBUFS;
217 	return p;
218 }
219 
220 struct protosw arpsw[] = {
221 	{ 0, 0, 0, 0,
222 	  0, 0, 0, 0,
223 	  0,
224 	  0, 0, 0, arp_drain,
225 	}
226 };
227 
228 
229 struct domain arpdomain =
230 { 	PF_ARP,  "arp", 0, 0, 0,
231 	arpsw, &arpsw[sizeof(arpsw)/sizeof(arpsw[0])]
232 };
233 
234 /*
235  * ARP table locking.
236  *
237  * to prevent lossage vs. the arp_drain routine (which may be called at
238  * any time, including in a device driver context), we do two things:
239  *
240  * 1) manipulation of la->la_hold is done at splnet() (for all of
241  * about two instructions).
242  *
243  * 2) manipulation of the arp table's linked list is done under the
244  * protection of the ARP_LOCK; if arp_drain() or arptimer is called
245  * while the arp table is locked, we punt and try again later.
246  */
247 
248 int	arp_locked;
249 
250 static __inline int arp_lock_try __P((int));
251 static __inline void arp_unlock __P((void));
252 
253 static __inline int
254 arp_lock_try(int recurse)
255 {
256 	int s;
257 
258 	/*
259 	 * Use splvm() -- we're blocking things that would cause
260 	 * mbuf allocation.
261 	 */
262 	s = splvm();
263 	if (!recurse && arp_locked) {
264 		splx(s);
265 		return (0);
266 	}
267 	arp_locked++;
268 	splx(s);
269 	return (1);
270 }
271 
272 static __inline void
273 arp_unlock()
274 {
275 	int s;
276 
277 	s = splvm();
278 	arp_locked--;
279 	splx(s);
280 }
281 
282 #ifdef DIAGNOSTIC
283 #define	ARP_LOCK(recurse)						\
284 do {									\
285 	if (arp_lock_try(recurse) == 0) {				\
286 		printf("%s:%d: arp already locked\n", __FILE__, __LINE__); \
287 		panic("arp_lock");					\
288 	}								\
289 } while (0)
290 #define	ARP_LOCK_CHECK()						\
291 do {									\
292 	if (arp_locked == 0) {						\
293 		printf("%s:%d: arp lock not held\n", __FILE__, __LINE__); \
294 		panic("arp lock check");				\
295 	}								\
296 } while (0)
297 #else
298 #define	ARP_LOCK(x)		(void) arp_lock_try(x)
299 #define	ARP_LOCK_CHECK()	/* nothing */
300 #endif
301 
302 #define	ARP_UNLOCK()		arp_unlock()
303 
304 /*
305  * ARP protocol drain routine.  Called when memory is in short supply.
306  * Called at splvm();
307  */
308 
309 void
310 arp_drain()
311 {
312 	struct llinfo_arp *la, *nla;
313 	int count = 0;
314 	struct mbuf *mold;
315 
316 	if (arp_lock_try(0) == 0) {
317 		printf("arp_drain: locked; punting\n");
318 		return;
319 	}
320 
321 	for (la = LIST_FIRST(&llinfo_arp); la != 0; la = nla) {
322 		nla = LIST_NEXT(la, la_list);
323 
324 		mold = la->la_hold;
325 		la->la_hold = 0;
326 
327 		if (mold) {
328 			m_freem(mold);
329 			count++;
330 		}
331 	}
332 	ARP_UNLOCK();
333 	arpstat.as_dfrdropped += count;
334 }
335 
336 
337 /*
338  * Timeout routine.  Age arp_tab entries periodically.
339  */
340 /* ARGSUSED */
341 static void
342 arptimer(arg)
343 	void *arg;
344 {
345 	int s;
346 	struct llinfo_arp *la, *nla;
347 
348 	s = splsoftnet();
349 
350 	if (arp_lock_try(0) == 0) {
351 		/* get it later.. */
352 		splx(s);
353 		return;
354 	}
355 
356 	callout_reset(&arptimer_ch, arpt_prune * hz, arptimer, NULL);
357 	for (la = LIST_FIRST(&llinfo_arp); la != 0; la = nla) {
358 		struct rtentry *rt = la->la_rt;
359 
360 		nla = LIST_NEXT(la, la_list);
361 		if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
362 			arptfree(la); /* timer has expired; clear */
363 	}
364 
365 	ARP_UNLOCK();
366 
367 	splx(s);
368 }
369 
370 /*
371  * Parallel to llc_rtrequest.
372  */
373 void
374 arp_rtrequest(req, rt, info)
375 	int req;
376 	struct rtentry *rt;
377 	struct rt_addrinfo *info;
378 {
379 	struct sockaddr *gate = rt->rt_gateway;
380 	struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
381 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
382 	size_t allocsize;
383 	struct mbuf *mold;
384 	int s;
385 	struct in_ifaddr *ia;
386 	struct ifaddr *ifa;
387 
388 	if (!arpinit_done) {
389 		arpinit_done = 1;
390 		/*
391 		 * We generate expiration times from time.tv_sec
392 		 * so avoid accidently creating permanent routes.
393 		 */
394 		if (time.tv_sec == 0) {
395 			time.tv_sec++;
396 		}
397 		callout_init(&arptimer_ch);
398 		callout_reset(&arptimer_ch, hz, arptimer, NULL);
399 	}
400 	if (rt->rt_flags & RTF_GATEWAY)
401 		return;
402 
403 	ARP_LOCK(1);		/* we may already be locked here. */
404 
405 	switch (req) {
406 
407 	case RTM_ADD:
408 		/*
409 		 * XXX: If this is a manually added route to interface
410 		 * such as older version of routed or gated might provide,
411 		 * restore cloning bit.
412 		 */
413 		if ((rt->rt_flags & RTF_HOST) == 0 &&
414 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
415 			rt->rt_flags |= RTF_CLONING;
416 		if (rt->rt_flags & RTF_CLONING) {
417 			/*
418 			 * Case 1: This route should come from a route to iface.
419 			 */
420 			rt_setgate(rt, rt_key(rt),
421 					(struct sockaddr *)&null_sdl);
422 			gate = rt->rt_gateway;
423 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
424 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
425 			/*
426 			 * Give this route an expiration time, even though
427 			 * it's a "permanent" route, so that routes cloned
428 			 * from it do not need their expiration time set.
429 			 */
430 			rt->rt_expire = time.tv_sec;
431 #if NFDDI > 0
432 			if (rt->rt_ifp->if_type == IFT_FDDI
433 			    && (rt->rt_rmx.rmx_mtu > FDDIIPMTU
434 				|| (rt->rt_rmx.rmx_mtu == 0
435 				    && rt->rt_ifp->if_mtu > FDDIIPMTU))) {
436 				rt->rt_rmx.rmx_mtu = FDDIIPMTU;
437 			}
438 #endif
439 #if NARC > 0
440 			if (rt->rt_ifp->if_type == IFT_ARCNET) {
441 				int arcipifmtu;
442 
443 				if (rt->rt_ifp->if_flags & IFF_LINK0)
444 					arcipifmtu = arc_ipmtu;
445 				else
446 					arcipifmtu = ARCMTU;
447 
448 			    	if (rt->rt_rmx.rmx_mtu > arcipifmtu ||
449 				    (rt->rt_rmx.rmx_mtu == 0 &&
450 				     rt->rt_ifp->if_mtu > arcipifmtu))
451 
452 					rt->rt_rmx.rmx_mtu = arcipifmtu;
453 			}
454 #endif
455 			break;
456 		}
457 		/* Announce a new entry if requested. */
458 		if (rt->rt_flags & RTF_ANNOUNCE)
459 			arprequest(rt->rt_ifp,
460 			    &SIN(rt_key(rt))->sin_addr,
461 			    &SIN(rt_key(rt))->sin_addr,
462 			    (u_char *)LLADDR(SDL(gate)));
463 		/*FALLTHROUGH*/
464 	case RTM_RESOLVE:
465 		if (gate->sa_family != AF_LINK ||
466 		    gate->sa_len < sizeof(null_sdl)) {
467 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
468 			break;
469 		}
470 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
471 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
472 		if (la != 0)
473 			break; /* This happens on a route change */
474 		/*
475 		 * Case 2:  This route may come from cloning, or a manual route
476 		 * add with a LL address.
477 		 */
478 		switch (SDL(gate)->sdl_type) {
479 #if NTOKEN > 0
480 		case IFT_ISO88025:
481 			allocsize = sizeof(*la) + sizeof(struct token_rif);
482 			break;
483 #endif /* NTOKEN > 0 */
484 		default:
485 			allocsize = sizeof(*la);
486 		}
487 		R_Malloc(la, struct llinfo_arp *, allocsize);
488 		rt->rt_llinfo = (caddr_t)la;
489 		if (la == 0) {
490 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
491 			break;
492 		}
493 		arp_inuse++, arp_allocated++;
494 		Bzero(la, allocsize);
495 		la->la_rt = rt;
496 		rt->rt_flags |= RTF_LLINFO;
497 		LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
498 
499 		INADDR_TO_IA(SIN(rt_key(rt))->sin_addr, ia);
500 		while (ia && ia->ia_ifp != rt->rt_ifp)
501 			NEXT_IA_WITH_SAME_ADDR(ia);
502 		if (ia) {
503 			/*
504 			 * This test used to be
505 			 *	if (loif.if_flags & IFF_UP)
506 			 * It allowed local traffic to be forced through
507 			 * the hardware by configuring the loopback down.
508 			 * However, it causes problems during network
509 			 * configuration for boards that can't receive
510 			 * packets they send.  It is now necessary to clear
511 			 * "useloopback" and remove the route to force
512 			 * traffic out to the hardware.
513 			 *
514 			 * In 4.4BSD, the above "if" statement checked
515 			 * rt->rt_ifa against rt_key(rt).  It was changed
516 			 * to the current form so that we can provide a
517 			 * better support for multiple IPv4 addresses on a
518 			 * interface.
519 			 */
520 			rt->rt_expire = 0;
521 			Bcopy(LLADDR(rt->rt_ifp->if_sadl),
522 			    LLADDR(SDL(gate)),
523 			    SDL(gate)->sdl_alen =
524 			    rt->rt_ifp->if_data.ifi_addrlen);
525 #if NLOOP > 0
526 			if (useloopback)
527 				rt->rt_ifp = &loif[0];
528 #endif
529 			/*
530 			 * make sure to set rt->rt_ifa to the interface
531 			 * address we are using, otherwise we will have trouble
532 			 * with source address selection.
533 			 */
534 			ifa = &ia->ia_ifa;
535 			if (ifa != rt->rt_ifa) {
536 				IFAFREE(rt->rt_ifa);
537 				IFAREF(ifa);
538 				rt->rt_ifa = ifa;
539 			}
540 		}
541 		break;
542 
543 	case RTM_DELETE:
544 		if (la == 0)
545 			break;
546 		arp_inuse--;
547 		LIST_REMOVE(la, la_list);
548 		rt->rt_llinfo = 0;
549 		rt->rt_flags &= ~RTF_LLINFO;
550 
551 		s = splnet();
552 		mold = la->la_hold;
553 		la->la_hold = 0;
554 		splx(s);
555 
556 		if (mold)
557 			m_freem(mold);
558 
559 		Free((caddr_t)la);
560 	}
561 	ARP_UNLOCK();
562 }
563 
564 /*
565  * Broadcast an ARP request. Caller specifies:
566  *	- arp header source ip address
567  *	- arp header target ip address
568  *	- arp header source ethernet address
569  */
570 static void
571 arprequest(ifp, sip, tip, enaddr)
572 	struct ifnet *ifp;
573 	struct in_addr *sip, *tip;
574 	u_int8_t *enaddr;
575 {
576 	struct mbuf *m;
577 	struct arphdr *ah;
578 	struct sockaddr sa;
579 
580 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
581 		return;
582 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
583 	    2*ifp->if_data.ifi_addrlen;
584 	m->m_pkthdr.len = m->m_len;
585 	MH_ALIGN(m, m->m_len);
586 	ah = mtod(m, struct arphdr *);
587 	bzero((caddr_t)ah, m->m_len);
588 	ah->ar_pro = htons(ETHERTYPE_IP);
589 	ah->ar_hln = ifp->if_data.ifi_addrlen;	/* hardware address length */
590 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
591 	ah->ar_op = htons(ARPOP_REQUEST);
592 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
593 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
594 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
595 	sa.sa_family = AF_ARP;
596 	sa.sa_len = 2;
597 	m->m_flags |= M_BCAST;
598 	arpstat.as_sndtotal++;
599 	arpstat.as_sndrequest++;
600 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
601 }
602 
603 /*
604  * Resolve an IP address into an ethernet address.  If success,
605  * desten is filled in.  If there is no entry in arptab,
606  * set one up and broadcast a request for the IP address.
607  * Hold onto this mbuf and resend it once the address
608  * is finally resolved.  A return value of 1 indicates
609  * that desten has been filled in and the packet should be sent
610  * normally; a 0 return indicates that the packet has been
611  * taken over here, either now or for later transmission.
612  */
613 int
614 arpresolve(ifp, rt, m, dst, desten)
615 	struct ifnet *ifp;
616 	struct rtentry *rt;
617 	struct mbuf *m;
618 	struct sockaddr *dst;
619 	u_char *desten;
620 {
621 	struct llinfo_arp *la;
622 	struct sockaddr_dl *sdl;
623 	struct mbuf *mold;
624 	int s;
625 
626 	if (rt)
627 		la = (struct llinfo_arp *)rt->rt_llinfo;
628 	else {
629 		if ((la = arplookup(m, &SIN(dst)->sin_addr, 1, 0)) != NULL)
630 			rt = la->la_rt;
631 	}
632 	if (la == 0 || rt == 0) {
633 		arpstat.as_allocfail++;
634 		log(LOG_DEBUG,
635 		    "arpresolve: can't allocate llinfo on %s for %s\n",
636 		    ifp->if_xname, in_fmtaddr(SIN(dst)->sin_addr));
637 		m_freem(m);
638 		return (0);
639 	}
640 	sdl = SDL(rt->rt_gateway);
641 	/*
642 	 * Check the address family and length is valid, the address
643 	 * is resolved; otherwise, try to resolve.
644 	 */
645 	if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
646 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
647 		bcopy(LLADDR(sdl), desten,
648 		    min(sdl->sdl_alen, ifp->if_data.ifi_addrlen));
649 		return 1;
650 	}
651 	/*
652 	 * There is an arptab entry, but no ethernet address
653 	 * response yet.  Replace the held mbuf with this
654 	 * latest one.
655 	 */
656 
657 	arpstat.as_dfrtotal++;
658 	s = splnet();
659 	mold = la->la_hold;
660 	la->la_hold = m;
661 	splx(s);
662 
663 	if (mold) {
664 		arpstat.as_dfrdropped++;
665 		m_freem(mold);
666 	}
667 
668 	/*
669 	 * Re-send the ARP request when appropriate.
670 	 */
671 #ifdef	DIAGNOSTIC
672 	if (rt->rt_expire == 0) {
673 		/* This should never happen. (Should it? -gwr) */
674 		printf("arpresolve: unresolved and rt_expire == 0\n");
675 		/* Set expiration time to now (expired). */
676 		rt->rt_expire = time.tv_sec;
677 	}
678 #endif
679 	if (rt->rt_expire) {
680 		rt->rt_flags &= ~RTF_REJECT;
681 		if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
682 			rt->rt_expire = time.tv_sec;
683 			if (la->la_asked++ < arp_maxtries)
684 				arprequest(ifp,
685 				    &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
686 				    &SIN(dst)->sin_addr,
687 				    LLADDR(ifp->if_sadl));
688 			else {
689 				rt->rt_flags |= RTF_REJECT;
690 				rt->rt_expire += arpt_down;
691 				la->la_asked = 0;
692 			}
693 		}
694 	}
695 	return (0);
696 }
697 
698 /*
699  * Common length and type checks are done here,
700  * then the protocol-specific routine is called.
701  */
702 void
703 arpintr()
704 {
705 	struct mbuf *m;
706 	struct arphdr *ar;
707 	int s;
708 
709 	while (arpintrq.ifq_head) {
710 		s = splnet();
711 		IF_DEQUEUE(&arpintrq, m);
712 		splx(s);
713 		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
714 			panic("arpintr");
715 
716 		arpstat.as_rcvtotal++;
717 
718 		if (m->m_len >= sizeof(struct arphdr) &&
719 		    (ar = mtod(m, struct arphdr *)) &&
720 		    /* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
721 		    m->m_len >=
722 		      sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
723 			switch (ntohs(ar->ar_pro)) {
724 
725 			case ETHERTYPE_IP:
726 			case ETHERTYPE_IPTRAILERS:
727 				in_arpinput(m);
728 				continue;
729 			default:
730 				arpstat.as_rcvbadproto++;
731 			}
732 		else
733 			arpstat.as_rcvbadlen++;
734 		m_freem(m);
735 	}
736 }
737 
738 /*
739  * ARP for Internet protocols on 10 Mb/s Ethernet.
740  * Algorithm is that given in RFC 826.
741  * In addition, a sanity check is performed on the sender
742  * protocol address, to catch impersonators.
743  * We no longer handle negotiations for use of trailer protocol:
744  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
745  * along with IP replies if we wanted trailers sent to us,
746  * and also sent them in response to IP replies.
747  * This allowed either end to announce the desire to receive
748  * trailer packets.
749  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
750  * but formerly didn't normally send requests.
751  */
752 static void
753 in_arpinput(m)
754 	struct mbuf *m;
755 {
756 	struct arphdr *ah;
757 	struct ifnet *ifp = m->m_pkthdr.rcvif;
758 	struct llinfo_arp *la = 0;
759 	struct rtentry  *rt;
760 	struct in_ifaddr *ia;
761 #if NBRIDGE > 0
762 	struct in_ifaddr *bridge_ia = NULL;
763 #endif
764 	struct sockaddr_dl *sdl;
765 	struct sockaddr sa;
766 	struct in_addr isaddr, itaddr, myaddr;
767 	int op;
768 	struct mbuf *mold;
769 	int s;
770 
771 	ah = mtod(m, struct arphdr *);
772 	op = ntohs(ah->ar_op);
773 	bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr));
774 	bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr));
775 
776 	if (m->m_flags & (M_BCAST|M_MCAST))
777 		arpstat.as_rcvmcast++;
778 
779 	/*
780 	 * If the target IP address is zero, ignore the packet.
781 	 * This prevents the code below from tring to answer
782 	 * when we are using IP address zero (booting).
783 	 */
784 	if (in_nullhost(itaddr)) {
785 		arpstat.as_rcvzerotpa++;
786 		goto out;
787 	}
788 
789 	/*
790 	 * If the source IP address is zero, this is most likely a
791 	 * confused host trying to use IP address zero. (Windoze?)
792 	 * XXX: Should we bother trying to reply to these?
793 	 */
794 	if (in_nullhost(isaddr)) {
795 		arpstat.as_rcvzerospa++;
796 		goto out;
797 	}
798 
799 	/*
800 	 * Search for a matching interface address
801 	 * or any address on the interface to use
802 	 * as a dummy address in the rest of this function
803 	 */
804 	INADDR_TO_IA(itaddr, ia);
805 	while (ia != NULL) {
806 		if (ia->ia_ifp == m->m_pkthdr.rcvif)
807 			break;
808 
809 #if NBRIDGE > 0
810 		/*
811 		 * If the interface we received the packet on
812 		 * is part of a bridge, check to see if we need
813 		 * to "bridge" the packet to ourselves at this
814 		 * layer.  Note we still prefer a perfect match,
815 		 * but allow this weaker match if necessary.
816 		 */
817 		if (m->m_pkthdr.rcvif->if_bridge != NULL &&
818 		    m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge)
819 			bridge_ia = ia;
820 #endif /* NBRIDGE > 0 */
821 
822 		NEXT_IA_WITH_SAME_ADDR(ia);
823 	}
824 
825 #if NBRIDGE > 0
826 	if (ia == NULL && bridge_ia != NULL) {
827 		ia = bridge_ia;
828 		ifp = bridge_ia->ia_ifp;
829 	}
830 #endif
831 
832 	if (ia == NULL) {
833 		INADDR_TO_IA(isaddr, ia);
834 		while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
835 			NEXT_IA_WITH_SAME_ADDR(ia);
836 
837 		if (ia == NULL) {
838 			IFP_TO_IA(ifp, ia);
839 			if (ia == NULL) {
840 				arpstat.as_rcvnoint++;
841 				goto out;
842 			}
843 		}
844 	}
845 
846 	myaddr = ia->ia_addr.sin_addr;
847 
848 	/* XXX checks for bridge case? */
849 	if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
850 	    ifp->if_data.ifi_addrlen)) {
851 		arpstat.as_rcvlocalsha++;
852 		goto out;	/* it's from me, ignore it. */
853 	}
854 
855 	/* XXX checks for bridge case? */
856 	if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)ifp->if_broadcastaddr,
857 	    ifp->if_data.ifi_addrlen)) {
858 		arpstat.as_rcvbcastsha++;
859 		log(LOG_ERR,
860 		    "%s: arp: link address is broadcast for IP address %s!\n",
861 		    ifp->if_xname, in_fmtaddr(isaddr));
862 		goto out;
863 	}
864 
865 	if (in_hosteq(isaddr, myaddr)) {
866 		arpstat.as_rcvlocalspa++;
867 		log(LOG_ERR,
868 		   "duplicate IP address %s sent from link address %s\n",
869 		   in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
870 		itaddr = myaddr;
871 		goto reply;
872 	}
873 	la = arplookup(m, &isaddr, in_hosteq(itaddr, myaddr), 0);
874 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
875 		if (sdl->sdl_alen &&
876 		    bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
877 			if (rt->rt_flags & RTF_STATIC) {
878 				arpstat.as_rcvoverperm++;
879 				log(LOG_INFO,
880 				    "%s tried to overwrite permanent arp info"
881 				    " for %s\n",
882 				    lla_snprintf(ar_sha(ah), ah->ar_hln),
883 				    in_fmtaddr(isaddr));
884 				goto out;
885 			} else if (rt->rt_ifp != ifp) {
886 				arpstat.as_rcvoverint++;
887 				log(LOG_INFO,
888 				    "%s on %s tried to overwrite "
889 				    "arp info for %s on %s\n",
890 				    lla_snprintf(ar_sha(ah), ah->ar_hln),
891 				    ifp->if_xname, in_fmtaddr(isaddr),
892 				    rt->rt_ifp->if_xname);
893 				    goto out;
894 			} else {
895 				arpstat.as_rcvover++;
896 				log(LOG_INFO,
897 				    "arp info overwritten for %s by %s\n",
898 				    in_fmtaddr(isaddr),
899 				    lla_snprintf(ar_sha(ah), ah->ar_hln));
900 			}
901 		}
902 		/*
903 		 * sanity check for the address length.
904 		 * XXX this does not work for protocols with variable address
905 		 * length. -is
906 		 */
907 		if (sdl->sdl_alen &&
908 		    sdl->sdl_alen != ah->ar_hln) {
909 			arpstat.as_rcvlenchg++;
910 			log(LOG_WARNING,
911 			    "arp from %s: new addr len %d, was %d",
912 			    in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
913 		}
914 		if (ifp->if_data.ifi_addrlen != ah->ar_hln) {
915 			arpstat.as_rcvbadlen++;
916 			log(LOG_WARNING,
917 			    "arp from %s: addr len: new %d, i/f %d (ignored)",
918 			    in_fmtaddr(isaddr), ah->ar_hln,
919 			    ifp->if_data.ifi_addrlen);
920 			goto reply;
921 		}
922 #if NTOKEN > 0
923 		/*
924 		 * XXX uses m_data and assumes the complete answer including
925 		 * XXX token-ring headers is in the same buf
926 		 */
927 		if (ifp->if_type == IFT_ISO88025) {
928 			struct token_header *trh;
929 
930 			trh = (struct token_header *)M_TRHSTART(m);
931 			if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
932 				struct token_rif	*rif;
933 				size_t	riflen;
934 
935 				rif = TOKEN_RIF(trh);
936 				riflen = (ntohs(rif->tr_rcf) &
937 				    TOKEN_RCF_LEN_MASK) >> 8;
938 
939 				if (riflen > 2 &&
940 				    riflen < sizeof(struct token_rif) &&
941 				    (riflen & 1) == 0) {
942 					rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
943 					rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
944 					bcopy(rif, TOKEN_RIF(la), riflen);
945 				}
946 			}
947 		}
948 #endif /* NTOKEN > 0 */
949 		bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
950 		    sdl->sdl_alen = ah->ar_hln);
951 		if (rt->rt_expire)
952 			rt->rt_expire = time.tv_sec + arpt_keep;
953 		rt->rt_flags &= ~RTF_REJECT;
954 		la->la_asked = 0;
955 
956 		s = splnet();
957 		mold = la->la_hold;
958 		la->la_hold = 0;
959 		splx(s);
960 
961 		if (mold) {
962 			arpstat.as_dfrsent++;
963 			(*ifp->if_output)(ifp, mold, rt_key(rt), rt);
964 		}
965 	}
966 reply:
967 	if (op != ARPOP_REQUEST) {
968 		if (op == ARPOP_REPLY)
969 			arpstat.as_rcvreply++;
970 	out:
971 		m_freem(m);
972 		return;
973 	}
974 	arpstat.as_rcvrequest++;
975 	if (in_hosteq(itaddr, myaddr)) {
976 		/* I am the target */
977 		bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
978 		bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
979 	} else {
980 		la = arplookup(m, &itaddr, 0, SIN_PROXY);
981 		if (la == 0)
982 			goto out;
983 		rt = la->la_rt;
984 		bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
985 		sdl = SDL(rt->rt_gateway);
986 		bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
987 	}
988 
989 	bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
990 	bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
991 	ah->ar_op = htons(ARPOP_REPLY);
992 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
993 	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
994 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
995 	m->m_pkthdr.len = m->m_len;
996 	sa.sa_family = AF_ARP;
997 	sa.sa_len = 2;
998 	arpstat.as_sndtotal++;
999 	arpstat.as_sndreply++;
1000 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
1001 	return;
1002 }
1003 
1004 /*
1005  * Free an arp entry.
1006  */
1007 static void
1008 arptfree(la)
1009 	struct llinfo_arp *la;
1010 {
1011 	struct rtentry *rt = la->la_rt;
1012 	struct sockaddr_dl *sdl;
1013 
1014 	ARP_LOCK_CHECK();
1015 
1016 	if (rt == 0)
1017 		panic("arptfree");
1018 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
1019 	    sdl->sdl_family == AF_LINK) {
1020 		sdl->sdl_alen = 0;
1021 		la->la_asked = 0;
1022 		rt->rt_flags &= ~RTF_REJECT;
1023 		return;
1024 	}
1025 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
1026 	    0, (struct rtentry **)0);
1027 }
1028 
1029 /*
1030  * Lookup or enter a new address in arptab.
1031  */
1032 static struct llinfo_arp *
1033 arplookup(m, addr, create, proxy)
1034 	struct mbuf *m;
1035 	struct in_addr *addr;
1036 	int create, proxy;
1037 {
1038 	struct arphdr *ah;
1039 	struct ifnet *ifp = m->m_pkthdr.rcvif;
1040 	struct rtentry *rt;
1041 	static struct sockaddr_inarp sin;
1042 	const char *why = 0;
1043 
1044 	ah = mtod(m, struct arphdr *);
1045 	sin.sin_len = sizeof(sin);
1046 	sin.sin_family = AF_INET;
1047 	sin.sin_addr = *addr;
1048 	sin.sin_other = proxy ? SIN_PROXY : 0;
1049 	rt = rtalloc1(sintosa(&sin), create);
1050 	if (rt == 0)
1051 		return (0);
1052 	rt->rt_refcnt--;
1053 
1054 	if (rt->rt_flags & RTF_GATEWAY)
1055 		why = "host is not on local network";
1056 	else if ((rt->rt_flags & RTF_LLINFO) == 0) {
1057 		arpstat.as_allocfail++;
1058 		why = "could not allocate llinfo";
1059 	} else if (rt->rt_gateway->sa_family != AF_LINK)
1060 		why = "gateway route is not ours";
1061 	else
1062 		return ((struct llinfo_arp *)rt->rt_llinfo);
1063 
1064 	if (create)
1065 		log(LOG_DEBUG, "arplookup: unable to enter address"
1066 		    " for %s@%s on %s (%s)\n",
1067 		    in_fmtaddr(*addr), lla_snprintf(ar_sha(ah), ah->ar_hln),
1068 		    ifp->if_xname, why);
1069 	return (0);
1070 }
1071 
1072 int
1073 arpioctl(cmd, data)
1074 	u_long cmd;
1075 	caddr_t data;
1076 {
1077 
1078 	return (EOPNOTSUPP);
1079 }
1080 
1081 void
1082 arp_ifinit(ifp, ifa)
1083 	struct ifnet *ifp;
1084 	struct ifaddr *ifa;
1085 {
1086 	struct in_addr *ip;
1087 
1088 	/*
1089 	 * Warn the user if another station has this IP address,
1090 	 * but only if the interface IP address is not zero.
1091 	 */
1092 	ip = &IA_SIN(ifa)->sin_addr;
1093 	if (!in_nullhost(*ip))
1094 		arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
1095 
1096 	ifa->ifa_rtrequest = arp_rtrequest;
1097 	ifa->ifa_flags |= RTF_CLONING;
1098 }
1099 
1100 /*
1101  * Called from 10 Mb/s Ethernet interrupt handlers
1102  * when ether packet type ETHERTYPE_REVARP
1103  * is received.  Common length and type checks are done here,
1104  * then the protocol-specific routine is called.
1105  */
1106 void
1107 revarpinput(m)
1108 	struct mbuf *m;
1109 {
1110 	struct arphdr *ar;
1111 
1112 	if (m->m_len < sizeof(struct arphdr))
1113 		goto out;
1114 	ar = mtod(m, struct arphdr *);
1115 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
1116 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
1117 		goto out;
1118 #endif
1119 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
1120 		goto out;
1121 	switch (ntohs(ar->ar_pro)) {
1122 
1123 	case ETHERTYPE_IP:
1124 	case ETHERTYPE_IPTRAILERS:
1125 		in_revarpinput(m);
1126 		return;
1127 
1128 	default:
1129 		break;
1130 	}
1131 out:
1132 	m_freem(m);
1133 }
1134 
1135 /*
1136  * RARP for Internet protocols on 10 Mb/s Ethernet.
1137  * Algorithm is that given in RFC 903.
1138  * We are only using for bootstrap purposes to get an ip address for one of
1139  * our interfaces.  Thus we support no user-interface.
1140  *
1141  * Since the contents of the RARP reply are specific to the interface that
1142  * sent the request, this code must ensure that they are properly associated.
1143  *
1144  * Note: also supports ARP via RARP packets, per the RFC.
1145  */
1146 void
1147 in_revarpinput(m)
1148 	struct mbuf *m;
1149 {
1150 	struct ifnet *ifp;
1151 	struct arphdr *ah;
1152 	int op;
1153 
1154 	ah = mtod(m, struct arphdr *);
1155 	op = ntohs(ah->ar_op);
1156 	switch (op) {
1157 	case ARPOP_REQUEST:
1158 	case ARPOP_REPLY:	/* per RFC */
1159 		in_arpinput(m);
1160 		return;
1161 	case ARPOP_REVREPLY:
1162 		break;
1163 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
1164 	default:
1165 		goto out;
1166 	}
1167 	if (!revarp_in_progress)
1168 		goto out;
1169 	ifp = m->m_pkthdr.rcvif;
1170 	if (ifp != myip_ifp) /* !same interface */
1171 		goto out;
1172 	if (myip_initialized)
1173 		goto wake;
1174 	if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
1175 		goto out;
1176 	bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
1177 	bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
1178 	myip_initialized = 1;
1179 wake:	/* Do wakeup every time in case it was missed. */
1180 	wakeup((caddr_t)&myip);
1181 
1182 out:
1183 	m_freem(m);
1184 }
1185 
1186 /*
1187  * Send a RARP request for the ip address of the specified interface.
1188  * The request should be RFC 903-compliant.
1189  */
1190 void
1191 revarprequest(ifp)
1192 	struct ifnet *ifp;
1193 {
1194 	struct sockaddr sa;
1195 	struct mbuf *m;
1196 	struct arphdr *ah;
1197 
1198 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
1199 		return;
1200 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
1201 	    2*ifp->if_data.ifi_addrlen;
1202 	m->m_pkthdr.len = m->m_len;
1203 	MH_ALIGN(m, m->m_len);
1204 	ah = mtod(m, struct arphdr *);
1205 	bzero((caddr_t)ah, m->m_len);
1206 	ah->ar_pro = htons(ETHERTYPE_IP);
1207 	ah->ar_hln = ifp->if_data.ifi_addrlen;	/* hardware address length */
1208 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
1209 	ah->ar_op = htons(ARPOP_REVREQUEST);
1210 
1211 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
1212 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
1213 
1214 	sa.sa_family = AF_ARP;
1215 	sa.sa_len = 2;
1216 	m->m_flags |= M_BCAST;
1217 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
1218 
1219 }
1220 
1221 /*
1222  * RARP for the ip address of the specified interface, but also
1223  * save the ip address of the server that sent the answer.
1224  * Timeout if no response is received.
1225  */
1226 int
1227 revarpwhoarewe(ifp, serv_in, clnt_in)
1228 	struct ifnet *ifp;
1229 	struct in_addr *serv_in;
1230 	struct in_addr *clnt_in;
1231 {
1232 	int result, count = 20;
1233 
1234 	myip_initialized = 0;
1235 	myip_ifp = ifp;
1236 
1237 	revarp_in_progress = 1;
1238 	while (count--) {
1239 		revarprequest(ifp);
1240 		result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
1241 		if (result != EWOULDBLOCK)
1242 			break;
1243 	}
1244 	revarp_in_progress = 0;
1245 
1246 	if (!myip_initialized)
1247 		return ENETUNREACH;
1248 
1249 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
1250 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
1251 	return 0;
1252 }
1253 
1254 
1255 
1256 #ifdef DDB
1257 
1258 #include <machine/db_machdep.h>
1259 #include <ddb/db_interface.h>
1260 #include <ddb/db_output.h>
1261 static void
1262 db_print_sa(sa)
1263 	struct sockaddr *sa;
1264 {
1265 	int len;
1266 	u_char *p;
1267 
1268 	if (sa == 0) {
1269 		db_printf("[NULL]");
1270 		return;
1271 	}
1272 
1273 	p = (u_char*)sa;
1274 	len = sa->sa_len;
1275 	db_printf("[");
1276 	while (len > 0) {
1277 		db_printf("%d", *p);
1278 		p++; len--;
1279 		if (len) db_printf(",");
1280 	}
1281 	db_printf("]\n");
1282 }
1283 static void
1284 db_print_ifa(ifa)
1285 	struct ifaddr *ifa;
1286 {
1287 	if (ifa == 0)
1288 		return;
1289 	db_printf("  ifa_addr=");
1290 	db_print_sa(ifa->ifa_addr);
1291 	db_printf("  ifa_dsta=");
1292 	db_print_sa(ifa->ifa_dstaddr);
1293 	db_printf("  ifa_mask=");
1294 	db_print_sa(ifa->ifa_netmask);
1295 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
1296 			  ifa->ifa_flags,
1297 			  ifa->ifa_refcnt,
1298 			  ifa->ifa_metric);
1299 }
1300 static void
1301 db_print_llinfo(li)
1302 	caddr_t li;
1303 {
1304 	struct llinfo_arp *la;
1305 
1306 	if (li == 0)
1307 		return;
1308 	la = (struct llinfo_arp *)li;
1309 	db_printf("  la_rt=%p la_hold=%p, la_asked=0x%lx\n",
1310 			  la->la_rt, la->la_hold, la->la_asked);
1311 }
1312 /*
1313  * Function to pass to rn_walktree().
1314  * Return non-zero error to abort walk.
1315  */
1316 static int
1317 db_show_radix_node(rn, w)
1318 	struct radix_node *rn;
1319 	void *w;
1320 {
1321 	struct rtentry *rt = (struct rtentry *)rn;
1322 
1323 	db_printf("rtentry=%p", rt);
1324 
1325 	db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
1326 			  rt->rt_flags, rt->rt_refcnt,
1327 			  rt->rt_use, rt->rt_expire);
1328 
1329 	db_printf(" key="); db_print_sa(rt_key(rt));
1330 	db_printf(" mask="); db_print_sa(rt_mask(rt));
1331 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
1332 
1333 	db_printf(" ifp=%p ", rt->rt_ifp);
1334 	if (rt->rt_ifp)
1335 		db_printf("(%s)", rt->rt_ifp->if_xname);
1336 	else
1337 		db_printf("(NULL)");
1338 
1339 	db_printf(" ifa=%p\n", rt->rt_ifa);
1340 	db_print_ifa(rt->rt_ifa);
1341 
1342 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
1343 
1344 	db_printf(" gwroute=%p llinfo=%p\n",
1345 			  rt->rt_gwroute, rt->rt_llinfo);
1346 	db_print_llinfo(rt->rt_llinfo);
1347 
1348 	return (0);
1349 }
1350 /*
1351  * Function to print all the route trees.
1352  * Use this from ddb:  "show arptab"
1353  */
1354 void
1355 db_show_arptab(addr, have_addr, count, modif)
1356 	db_expr_t	addr;
1357 	int		have_addr;
1358 	db_expr_t	count;
1359 	char *		modif;
1360 {
1361 	struct radix_node_head *rnh;
1362 	rnh = rt_tables[AF_INET];
1363 	db_printf("Route tree for AF_INET\n");
1364 	if (rnh == NULL) {
1365 		db_printf(" (not initialized)\n");
1366 		return;
1367 	}
1368 	rn_walktree(rnh, db_show_radix_node, NULL);
1369 	return;
1370 }
1371 #endif
1372 #endif /* INET */
1373 
1374