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