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