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