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