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