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