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