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