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