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