Home | History | Annotate | Line # | Download | only in netinet
if_arp.c revision 1.275
      1 /*	$NetBSD: if_arp.c,v 1.275 2018/05/11 13:56:43 maxv 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.275 2018/05/11 13:56:43 maxv 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 		 * make sure to set rt->rt_ifa to the interface
    610 		 * address we are using, otherwise we will have trouble
    611 		 * with source address selection.
    612 		 */
    613 		ifa = &ia->ia_ifa;
    614 		if (ifa != rt->rt_ifa)
    615 			/* Assume it doesn't sleep */
    616 			rt_replace_ifa(rt, ifa);
    617 		pserialize_read_exit(s);
    618 	out:
    619 		curlwp_bindx(bound);
    620 		break;
    621 	}
    622 }
    623 
    624 /*
    625  * Broadcast an ARP request. Caller specifies:
    626  *	- arp header source ip address
    627  *	- arp header target ip address
    628  *	- arp header source ethernet address
    629  */
    630 static void
    631 arprequest(struct ifnet *ifp,
    632     const struct in_addr *sip, const struct in_addr *tip,
    633     const uint8_t *enaddr)
    634 {
    635 	struct mbuf *m;
    636 	struct arphdr *ah;
    637 	struct sockaddr sa;
    638 	uint64_t *arps;
    639 
    640 	KASSERT(sip != NULL);
    641 	KASSERT(tip != NULL);
    642 	KASSERT(enaddr != NULL);
    643 
    644 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    645 		return;
    646 	MCLAIM(m, &arpdomain.dom_mowner);
    647 	switch (ifp->if_type) {
    648 	case IFT_IEEE1394:
    649 		m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
    650 		    ifp->if_addrlen;
    651 		break;
    652 	default:
    653 		m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
    654 		    2 * ifp->if_addrlen;
    655 		break;
    656 	}
    657 	m->m_pkthdr.len = m->m_len;
    658 	MH_ALIGN(m, m->m_len);
    659 	ah = mtod(m, struct arphdr *);
    660 	memset(ah, 0, m->m_len);
    661 	switch (ifp->if_type) {
    662 	case IFT_IEEE1394:	/* RFC2734 */
    663 		/* fill it now for ar_tpa computation */
    664 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
    665 		break;
    666 	default:
    667 		/* ifp->if_output will fill ar_hrd */
    668 		break;
    669 	}
    670 	ah->ar_pro = htons(ETHERTYPE_IP);
    671 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
    672 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
    673 	ah->ar_op = htons(ARPOP_REQUEST);
    674 	memcpy(ar_sha(ah), enaddr, ah->ar_hln);
    675 	memcpy(ar_spa(ah), sip, ah->ar_pln);
    676 	memcpy(ar_tpa(ah), tip, ah->ar_pln);
    677 	sa.sa_family = AF_ARP;
    678 	sa.sa_len = 2;
    679 	m->m_flags |= M_BCAST;
    680 	arps = ARP_STAT_GETREF();
    681 	arps[ARP_STAT_SNDTOTAL]++;
    682 	arps[ARP_STAT_SENDREQUEST]++;
    683 	ARP_STAT_PUTREF();
    684 	if_output_lock(ifp, ifp, m, &sa, NULL);
    685 }
    686 
    687 void
    688 arpannounce(struct ifnet *ifp, struct ifaddr *ifa, const uint8_t *enaddr)
    689 {
    690 	struct in_ifaddr *ia = ifatoia(ifa);
    691 	struct in_addr *ip = &IA_SIN(ifa)->sin_addr;
    692 
    693 	if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) {
    694 		ARPLOG(LOG_DEBUG, "%s not ready\n", ARPLOGADDR(ip));
    695 		return;
    696 	}
    697 	arprequest(ifp, ip, ip, enaddr);
    698 }
    699 
    700 static void
    701 arpannounce1(struct ifaddr *ifa)
    702 {
    703 
    704 	arpannounce(ifa->ifa_ifp, ifa, CLLADDR(ifa->ifa_ifp->if_sadl));
    705 }
    706 
    707 /*
    708  * Resolve an IP address into an ethernet address.  If success, desten is
    709  * filled in. If there is no entry in arptab, set one up and broadcast a
    710  * request for the IP address. Hold onto this mbuf and resend it once the
    711  * address is finally resolved.
    712  *
    713  * A return value of 0 indicates that desten has been filled in and the packet
    714  * should be sent normally; a return value of EWOULDBLOCK indicates that the
    715  * packet has been held pending resolution. Any other value indicates an
    716  * error.
    717  */
    718 int
    719 arpresolve(struct ifnet *ifp, const struct rtentry *rt, struct mbuf *m,
    720     const struct sockaddr *dst, void *desten, size_t destlen)
    721 {
    722 	struct llentry *la;
    723 	const char *create_lookup;
    724 	bool renew;
    725 	int error;
    726 	struct ifnet *origifp = ifp;
    727 
    728 #if NCARP > 0
    729 	if (rt != NULL && rt->rt_ifp->if_type == IFT_CARP)
    730 		ifp = rt->rt_ifp;
    731 #endif
    732 
    733 	KASSERT(m != NULL);
    734 
    735 	la = arplookup(ifp, NULL, dst, 0);
    736 	if (la == NULL)
    737 		goto notfound;
    738 
    739 	if ((la->la_flags & LLE_VALID) &&
    740 	    ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime)) {
    741 		KASSERT(destlen >= ifp->if_addrlen);
    742 		memcpy(desten, &la->ll_addr, ifp->if_addrlen);
    743 		LLE_RUNLOCK(la);
    744 		return 0;
    745 	}
    746 
    747 notfound:
    748 	if (ifp->if_flags & IFF_NOARP) {
    749 		if (la != NULL)
    750 			LLE_RUNLOCK(la);
    751 		error = ENOTSUP;
    752 		goto bad;
    753 	}
    754 
    755 	if (la == NULL) {
    756 		struct rtentry *_rt;
    757 
    758 		create_lookup = "create";
    759 		_rt = rtalloc1(dst, 0);
    760 		IF_AFDATA_WLOCK(ifp);
    761 		la = lla_create(LLTABLE(ifp), LLE_EXCLUSIVE, dst, _rt);
    762 		IF_AFDATA_WUNLOCK(ifp);
    763 		if (_rt != NULL)
    764 			rt_unref(_rt);
    765 		if (la == NULL)
    766 			ARP_STATINC(ARP_STAT_ALLOCFAIL);
    767 		else {
    768 			struct sockaddr_in sin;
    769 
    770 			arp_init_llentry(ifp, la);
    771 			sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
    772 			rt_clonedmsg(sintosa(&sin), ifp, rt);
    773 		}
    774 	} else if (LLE_TRY_UPGRADE(la) == 0) {
    775 		create_lookup = "lookup";
    776 		LLE_RUNLOCK(la);
    777 		IF_AFDATA_RLOCK(ifp);
    778 		la = lla_lookup(LLTABLE(ifp), LLE_EXCLUSIVE, dst);
    779 		IF_AFDATA_RUNLOCK(ifp);
    780 	}
    781 
    782 	error = EINVAL;
    783 	if (la == NULL) {
    784 		log(LOG_DEBUG,
    785 		    "%s: failed to %s llentry for %s on %s\n",
    786 		    __func__, create_lookup, inet_ntoa(satocsin(dst)->sin_addr),
    787 		    ifp->if_xname);
    788 		goto bad;
    789 	}
    790 
    791 	if ((la->la_flags & LLE_VALID) &&
    792 	    ((la->la_flags & LLE_STATIC) || la->la_expire > time_uptime))
    793 	{
    794 		KASSERT(destlen >= ifp->if_addrlen);
    795 		memcpy(desten, &la->ll_addr, ifp->if_addrlen);
    796 		renew = false;
    797 		/*
    798 		 * If entry has an expiry time and it is approaching,
    799 		 * see if we need to send an ARP request within this
    800 		 * arpt_down interval.
    801 		 */
    802 		if (!(la->la_flags & LLE_STATIC) &&
    803 		    time_uptime + la->la_preempt > la->la_expire)
    804 		{
    805 			renew = true;
    806 			la->la_preempt--;
    807 		}
    808 
    809 		LLE_WUNLOCK(la);
    810 
    811 		if (renew) {
    812 			const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
    813 			arprequest(origifp,
    814 			    &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
    815 			    &satocsin(dst)->sin_addr, enaddr);
    816 		}
    817 
    818 		return 0;
    819 	}
    820 
    821 	if (la->la_flags & LLE_STATIC) {   /* should not happen! */
    822 		LLE_RUNLOCK(la);
    823 		log(LOG_DEBUG, "%s: ouch, empty static llinfo for %s\n",
    824 		    __func__, inet_ntoa(satocsin(dst)->sin_addr));
    825 		error = EINVAL;
    826 		goto bad;
    827 	}
    828 
    829 	renew = (la->la_asked == 0 || la->la_expire != time_uptime);
    830 
    831 	/*
    832 	 * There is an arptab entry, but no ethernet address
    833 	 * response yet.  Add the mbuf to the list, dropping
    834 	 * the oldest packet if we have exceeded the system
    835 	 * setting.
    836 	 */
    837 	LLE_WLOCK_ASSERT(la);
    838 	if (la->la_numheld >= arp_maxhold) {
    839 		if (la->la_hold != NULL) {
    840 			struct mbuf *next = la->la_hold->m_nextpkt;
    841 			m_freem(la->la_hold);
    842 			la->la_hold = next;
    843 			la->la_numheld--;
    844 			ARP_STATINC(ARP_STAT_DFRDROPPED);
    845 			ARP_STATINC(ARP_STAT_DFRTOTAL);
    846 		}
    847 	}
    848 	if (la->la_hold != NULL) {
    849 		struct mbuf *curr = la->la_hold;
    850 		while (curr->m_nextpkt != NULL)
    851 			curr = curr->m_nextpkt;
    852 		curr->m_nextpkt = m;
    853 	} else
    854 		la->la_hold = m;
    855 	la->la_numheld++;
    856 	if (!renew)
    857 		LLE_DOWNGRADE(la);
    858 
    859 	/*
    860 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
    861 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
    862 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
    863 	 * ARP request, but not faster than one request per second.
    864 	 */
    865 	if (la->la_asked < arp_maxtries)
    866 		error = EWOULDBLOCK;	/* First request. */
    867 	else
    868 		error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ?
    869 		    EHOSTUNREACH : EHOSTDOWN;
    870 
    871 	if (renew) {
    872 		const uint8_t *enaddr = CLLADDR(ifp->if_sadl);
    873 		la->la_expire = time_uptime;
    874 		arp_settimer(la, arpt_down);
    875 		la->la_asked++;
    876 		LLE_WUNLOCK(la);
    877 
    878 		if (rt != NULL) {
    879 			arprequest(origifp,
    880 			    &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
    881 			    &satocsin(dst)->sin_addr, enaddr);
    882 		} else {
    883 			struct sockaddr_in sin;
    884 			struct rtentry *_rt;
    885 
    886 			sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
    887 
    888 			/* XXX */
    889 			_rt = rtalloc1((struct sockaddr *)&sin, 0);
    890 			if (_rt == NULL)
    891 				goto bad;
    892 			arprequest(origifp,
    893 			    &satocsin(_rt->rt_ifa->ifa_addr)->sin_addr,
    894 			    &satocsin(dst)->sin_addr, enaddr);
    895 			rt_unref(_rt);
    896 		}
    897 		return error;
    898 	}
    899 
    900 	LLE_RUNLOCK(la);
    901 	return error;
    902 
    903 bad:
    904 	m_freem(m);
    905 	return error;
    906 }
    907 
    908 /*
    909  * Common length and type checks are done here,
    910  * then the protocol-specific routine is called.
    911  */
    912 void
    913 arpintr(void)
    914 {
    915 	struct mbuf *m;
    916 	struct arphdr *ar;
    917 	int s;
    918 	int arplen;
    919 
    920 	SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
    921 	for (;;) {
    922 		struct ifnet *rcvif;
    923 
    924 		IFQ_LOCK(&arpintrq);
    925 		IF_DEQUEUE(&arpintrq, m);
    926 		IFQ_UNLOCK(&arpintrq);
    927 		if (m == NULL)
    928 			goto out;
    929 		if ((m->m_flags & M_PKTHDR) == 0)
    930 			panic("arpintr");
    931 
    932 		MCLAIM(m, &arpdomain.dom_mowner);
    933 		ARP_STATINC(ARP_STAT_RCVTOTAL);
    934 
    935 		arplen = sizeof(struct arphdr);
    936 		if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
    937 			goto badlen;
    938 		ar = mtod(m, struct arphdr *);
    939 
    940 		rcvif = m_get_rcvif(m, &s);
    941 		if (__predict_false(rcvif == NULL)) {
    942 			ARP_STATINC(ARP_STAT_RCVNOINT);
    943 			goto free;
    944 		}
    945 
    946 		/*
    947 		 * We don't want non-IEEE1394 ARP packets on IEEE1394
    948 		 * interfaces, and vice versa. Our life depends on that.
    949 		 */
    950 		switch (rcvif->if_type) {
    951 		case IFT_IEEE1394:
    952 			if (ntohs(ar->ar_hrd) != ARPHRD_IEEE1394) {
    953 				m_put_rcvif(rcvif, &s);
    954 				ARP_STATINC(ARP_STAT_RCVBADPROTO);
    955 				goto free;
    956 			}
    957 
    958 			arplen = sizeof(struct arphdr) +
    959 			    ar->ar_hln + 2 * ar->ar_pln;
    960 			break;
    961 		default:
    962 			if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394) {
    963 				m_put_rcvif(rcvif, &s);
    964 				ARP_STATINC(ARP_STAT_RCVBADPROTO);
    965 				goto free;
    966 			}
    967 
    968 			arplen = sizeof(struct arphdr) +
    969 			    2 * ar->ar_hln + 2 * ar->ar_pln;
    970 			break;
    971 		}
    972 
    973 		m_put_rcvif(rcvif, &s);
    974 
    975 		if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
    976 			goto badlen;
    977 		ar = mtod(m, struct arphdr *);
    978 
    979 		switch (ntohs(ar->ar_pro)) {
    980 		case ETHERTYPE_IP:
    981 		case ETHERTYPE_IPTRAILERS:
    982 			in_arpinput(m);
    983 			continue;
    984 		default:
    985 			ARP_STATINC(ARP_STAT_RCVBADPROTO);
    986 			goto free;
    987 		}
    988 
    989 badlen:
    990 		ARP_STATINC(ARP_STAT_RCVBADLEN);
    991 free:
    992 		m_freem(m);
    993 	}
    994 
    995 out:
    996 	SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
    997 	return; /* XXX gcc */
    998 }
    999 
   1000 /*
   1001  * ARP for Internet protocols on 10 Mb/s Ethernet. Algorithm is that given in
   1002  * RFC 826. In addition, a sanity check is performed on the sender protocol
   1003  * address, to catch impersonators.
   1004  *
   1005  * We no longer handle negotiations for use of trailer protocol: formerly, ARP
   1006  * replied for protocol type ETHERTYPE_TRAIL sent along with IP replies if we
   1007  * wanted trailers sent to us, and also sent them in response to IP replies.
   1008  * This allowed either end to announce the desire to receive trailer packets.
   1009  *
   1010  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, but
   1011  * formerly didn't normally send requests.
   1012  */
   1013 static void
   1014 in_arpinput(struct mbuf *m)
   1015 {
   1016 	struct arphdr *ah;
   1017 	struct ifnet *ifp, *rcvif = NULL;
   1018 	struct llentry *la = NULL;
   1019 	struct in_ifaddr *ia = NULL;
   1020 #if NBRIDGE > 0
   1021 	struct in_ifaddr *bridge_ia = NULL;
   1022 #endif
   1023 #if NCARP > 0
   1024 	uint32_t count = 0, index = 0;
   1025 #endif
   1026 	struct sockaddr sa;
   1027 	struct in_addr isaddr, itaddr, myaddr;
   1028 	int op;
   1029 	void *tha;
   1030 	uint64_t *arps;
   1031 	struct psref psref, psref_ia;
   1032 	int s;
   1033 	char llabuf[LLA_ADDRSTRLEN];
   1034 	char ipbuf[INET_ADDRSTRLEN];
   1035 	bool do_dad;
   1036 
   1037 	if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
   1038 		goto out;
   1039 	ah = mtod(m, struct arphdr *);
   1040 	op = ntohs(ah->ar_op);
   1041 
   1042 	if (ah->ar_pln != sizeof(struct in_addr))
   1043 		goto out;
   1044 
   1045 	ifp = if_get_bylla(ar_sha(ah), ah->ar_hln, &psref);
   1046 	if (ifp) {
   1047 		/* it's from me, ignore it. */
   1048 		if_put(ifp, &psref);
   1049 		ARP_STATINC(ARP_STAT_RCVLOCALSHA);
   1050 		goto out;
   1051 	}
   1052 
   1053 	rcvif = ifp = m_get_rcvif_psref(m, &psref);
   1054 	if (__predict_false(rcvif == NULL))
   1055 		goto out;
   1056 	if (rcvif->if_flags & IFF_NOARP)
   1057 		goto out;
   1058 
   1059 	memcpy(&isaddr, ar_spa(ah), sizeof(isaddr));
   1060 	memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr));
   1061 
   1062 	if (m->m_flags & (M_BCAST|M_MCAST))
   1063 		ARP_STATINC(ARP_STAT_RCVMCAST);
   1064 
   1065 	/*
   1066 	 * Search for a matching interface address
   1067 	 * or any address on the interface to use
   1068 	 * as a dummy address in the rest of this function.
   1069 	 *
   1070 	 * If the target IP address is zero then try and find
   1071 	 * the sender address for DAD.
   1072 	 */
   1073 	myaddr = in_nullhost(itaddr) ? isaddr : itaddr;
   1074 	s = pserialize_read_enter();
   1075 	IN_ADDRHASH_READER_FOREACH(ia, myaddr.s_addr) {
   1076 		if (!in_hosteq(ia->ia_addr.sin_addr, myaddr))
   1077 			continue;
   1078 #if NCARP > 0
   1079 		if (ia->ia_ifp->if_type == IFT_CARP &&
   1080 		    ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
   1081 		    (IFF_UP|IFF_RUNNING))) {
   1082 			index++;
   1083 			/* XXX: ar_hln? */
   1084 			if (ia->ia_ifp == rcvif && (ah->ar_hln >= 6) &&
   1085 			    carp_iamatch(ia, ar_sha(ah),
   1086 			    &count, index)) {
   1087 				break;
   1088 			}
   1089 		} else
   1090 #endif
   1091 		if (ia->ia_ifp == rcvif)
   1092 			break;
   1093 #if NBRIDGE > 0
   1094 		/*
   1095 		 * If the interface we received the packet on
   1096 		 * is part of a bridge, check to see if we need
   1097 		 * to "bridge" the packet to ourselves at this
   1098 		 * layer.  Note we still prefer a perfect match,
   1099 		 * but allow this weaker match if necessary.
   1100 		 */
   1101 		if (rcvif->if_bridge != NULL &&
   1102 		    rcvif->if_bridge == ia->ia_ifp->if_bridge)
   1103 			bridge_ia = ia;
   1104 #endif
   1105 	}
   1106 
   1107 #if NBRIDGE > 0
   1108 	if (ia == NULL && bridge_ia != NULL) {
   1109 		ia = bridge_ia;
   1110 		m_put_rcvif_psref(rcvif, &psref);
   1111 		rcvif = NULL;
   1112 		/* FIXME */
   1113 		ifp = bridge_ia->ia_ifp;
   1114 	}
   1115 #endif
   1116 	if (ia != NULL)
   1117 		ia4_acquire(ia, &psref_ia);
   1118 	pserialize_read_exit(s);
   1119 
   1120 	if (ah->ar_hln != ifp->if_addrlen) {
   1121 		ARP_STATINC(ARP_STAT_RCVBADLEN);
   1122 		log(LOG_WARNING,
   1123 		    "arp from %s: addr len: new %d, i/f %d (ignored)\n",
   1124 		    IN_PRINT(ipbuf, &isaddr), ah->ar_hln, ifp->if_addrlen);
   1125 		goto out;
   1126 	}
   1127 
   1128 	/* Only do DaD if we have a matching address. */
   1129 	do_dad = (ia != NULL);
   1130 
   1131 	if (ia == NULL) {
   1132 		ia = in_get_ia_on_iface_psref(isaddr, rcvif, &psref_ia);
   1133 		if (ia == NULL) {
   1134 			ia = in_get_ia_from_ifp_psref(ifp, &psref_ia);
   1135 			if (ia == NULL) {
   1136 				ARP_STATINC(ARP_STAT_RCVNOINT);
   1137 				goto out;
   1138 			}
   1139 		}
   1140 	}
   1141 
   1142 	myaddr = ia->ia_addr.sin_addr;
   1143 
   1144 	/* XXX checks for bridge case? */
   1145 	if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
   1146 		ARP_STATINC(ARP_STAT_RCVBCASTSHA);
   1147 		log(LOG_ERR,
   1148 		    "%s: arp: link address is broadcast for IP address %s!\n",
   1149 		    ifp->if_xname, IN_PRINT(ipbuf, &isaddr));
   1150 		goto out;
   1151 	}
   1152 
   1153 	/*
   1154 	 * If the source IP address is zero, this is an RFC 5227 ARP probe
   1155 	 */
   1156 	if (in_nullhost(isaddr))
   1157 		ARP_STATINC(ARP_STAT_RCVZEROSPA);
   1158 	else if (in_hosteq(isaddr, myaddr))
   1159 		ARP_STATINC(ARP_STAT_RCVLOCALSPA);
   1160 
   1161 	if (in_nullhost(itaddr))
   1162 		ARP_STATINC(ARP_STAT_RCVZEROTPA);
   1163 
   1164 	/*
   1165 	 * DAD check, RFC 5227.
   1166 	 * Collision on sender address is always a duplicate.
   1167 	 * Collision on target address is only a duplicate IF
   1168 	 * the sender address is the null host (ie a DAD probe) AND
   1169 	 * our address is in the TENTATIVE state.
   1170 	 * DUPLICATED state is also checked so that processing stops here
   1171 	 * and an error can be logged.
   1172 	 */
   1173 	if (do_dad &&
   1174 	    (in_hosteq(isaddr, myaddr) ||
   1175 	    (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr)
   1176 	    && ia->ia4_flags & (IN_IFF_TENTATIVE | IN_IFF_DUPLICATED))))
   1177 	{
   1178 		arp_dad_duplicated((struct ifaddr *)ia,
   1179 		    lla_snprintf(llabuf, ar_sha(ah), ah->ar_hln));
   1180 		goto out;
   1181 	}
   1182 
   1183 	/*
   1184 	 * If the target IP address is zero, ignore the packet.
   1185 	 * This prevents the code below from trying to answer
   1186 	 * when we are using IP address zero (booting).
   1187 	 */
   1188 	if (in_nullhost(itaddr))
   1189 		goto out;
   1190 
   1191 	if (in_nullhost(isaddr))
   1192 		goto reply;
   1193 
   1194 	if (in_hosteq(itaddr, myaddr))
   1195 		la = arpcreate(ifp, &isaddr, NULL, 1);
   1196 	else
   1197 		la = arplookup(ifp, &isaddr, NULL, 1);
   1198 	if (la == NULL)
   1199 		goto reply;
   1200 
   1201 	if ((la->la_flags & LLE_VALID) &&
   1202 	    memcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
   1203 		if (la->la_flags & LLE_STATIC) {
   1204 			ARP_STATINC(ARP_STAT_RCVOVERPERM);
   1205 			if (!log_permanent_modify)
   1206 				goto out;
   1207 			log(LOG_INFO,
   1208 			    "%s tried to overwrite permanent arp info"
   1209 			    " for %s\n",
   1210 			    lla_snprintf(llabuf, ar_sha(ah), ah->ar_hln),
   1211 			    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 			    lla_snprintf(llabuf, ar_sha(ah), ah->ar_hln),
   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),
   1231 				    lla_snprintf(llabuf, ar_sha(ah),
   1232 				    ah->ar_hln));
   1233 		}
   1234 	}
   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 (la->la_hold != NULL) {
   1277 		int n = la->la_numheld;
   1278 		struct mbuf *m_hold, *m_hold_next;
   1279 		struct sockaddr_in sin;
   1280 
   1281 		sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
   1282 
   1283 		m_hold = la->la_hold;
   1284 		la->la_hold = NULL;
   1285 		la->la_numheld = 0;
   1286 		/*
   1287 		 * We have to unlock here because if_output would call
   1288 		 * arpresolve
   1289 		 */
   1290 		LLE_WUNLOCK(la);
   1291 		ARP_STATADD(ARP_STAT_DFRSENT, n);
   1292 		ARP_STATADD(ARP_STAT_DFRTOTAL, n);
   1293 		for (; m_hold != NULL; m_hold = m_hold_next) {
   1294 			m_hold_next = m_hold->m_nextpkt;
   1295 			m_hold->m_nextpkt = NULL;
   1296 			if_output_lock(ifp, ifp, m_hold, sintosa(&sin), NULL);
   1297 		}
   1298 	} else
   1299 		LLE_WUNLOCK(la);
   1300 	la = NULL;
   1301 
   1302 reply:
   1303 	if (la != NULL) {
   1304 		LLE_WUNLOCK(la);
   1305 		la = NULL;
   1306 	}
   1307 	if (op != ARPOP_REQUEST) {
   1308 		if (op == ARPOP_REPLY)
   1309 			ARP_STATINC(ARP_STAT_RCVREPLY);
   1310 		goto out;
   1311 	}
   1312 	ARP_STATINC(ARP_STAT_RCVREQUEST);
   1313 	if (in_hosteq(itaddr, myaddr)) {
   1314 		/* If our address is unusable, don't reply */
   1315 		if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
   1316 			goto out;
   1317 		/* I am the target */
   1318 		tha = ar_tha(ah);
   1319 		if (tha)
   1320 			memcpy(tha, ar_sha(ah), ah->ar_hln);
   1321 		memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
   1322 	} else {
   1323 		/* Proxy ARP */
   1324 		struct llentry *lle = NULL;
   1325 		struct sockaddr_in sin;
   1326 
   1327 #if NCARP > 0
   1328 		if (ifp->if_type == IFT_CARP) {
   1329 			struct ifnet *_rcvif = m_get_rcvif(m, &s);
   1330 			int iftype = 0;
   1331 			if (__predict_true(_rcvif != NULL))
   1332 				iftype = _rcvif->if_type;
   1333 			m_put_rcvif(_rcvif, &s);
   1334 			if (iftype != IFT_CARP)
   1335 				goto out;
   1336 		}
   1337 #endif
   1338 
   1339 		tha = ar_tha(ah);
   1340 
   1341 		sockaddr_in_init(&sin, &itaddr, 0);
   1342 
   1343 		IF_AFDATA_RLOCK(ifp);
   1344 		lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
   1345 		IF_AFDATA_RUNLOCK(ifp);
   1346 
   1347 		if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
   1348 			if (tha)
   1349 				memcpy(tha, ar_sha(ah), ah->ar_hln);
   1350 			memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
   1351 			LLE_RUNLOCK(lle);
   1352 		} else {
   1353 			if (lle != NULL)
   1354 				LLE_RUNLOCK(lle);
   1355 			goto out;
   1356 		}
   1357 	}
   1358 	ia4_release(ia, &psref_ia);
   1359 
   1360 	/*
   1361 	 * XXX XXX: Here we're recycling the mbuf. But the mbuf could have
   1362 	 * other mbufs in its chain, and just overwriting m->m_pkthdr.len
   1363 	 * would be wrong in this case (the length becomes smaller than the
   1364 	 * real chain size).
   1365 	 *
   1366 	 * This can theoretically cause bugs in the lower layers (drivers,
   1367 	 * and L2encap), in some corner cases.
   1368 	 */
   1369 	memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
   1370 	memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
   1371 	ah->ar_op = htons(ARPOP_REPLY);
   1372 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
   1373 	switch (ifp->if_type) {
   1374 	case IFT_IEEE1394:
   1375 		/* ieee1394 arp reply is broadcast */
   1376 		m->m_flags &= ~M_MCAST;
   1377 		m->m_flags |= M_BCAST;
   1378 		m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
   1379 		break;
   1380 	default:
   1381 		m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
   1382 		m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
   1383 		break;
   1384 	}
   1385 	m->m_pkthdr.len = m->m_len;
   1386 	sa.sa_family = AF_ARP;
   1387 	sa.sa_len = 2;
   1388 	arps = ARP_STAT_GETREF();
   1389 	arps[ARP_STAT_SNDTOTAL]++;
   1390 	arps[ARP_STAT_SNDREPLY]++;
   1391 	ARP_STAT_PUTREF();
   1392 	if_output_lock(ifp, ifp, m, &sa, NULL);
   1393 	if (rcvif != NULL)
   1394 		m_put_rcvif_psref(rcvif, &psref);
   1395 	return;
   1396 
   1397 out:
   1398 	if (la != NULL)
   1399 		LLE_WUNLOCK(la);
   1400 	if (ia != NULL)
   1401 		ia4_release(ia, &psref_ia);
   1402 	if (rcvif != NULL)
   1403 		m_put_rcvif_psref(rcvif, &psref);
   1404 	m_freem(m);
   1405 }
   1406 
   1407 /*
   1408  * Lookup or a new address in arptab.
   1409  */
   1410 static struct llentry *
   1411 arplookup(struct ifnet *ifp, const struct in_addr *addr,
   1412     const struct sockaddr *sa, int wlock)
   1413 {
   1414 	struct sockaddr_in sin;
   1415 	struct llentry *la;
   1416 	int flags = wlock ? LLE_EXCLUSIVE : 0;
   1417 
   1418 	if (sa == NULL) {
   1419 		KASSERT(addr != NULL);
   1420 		sockaddr_in_init(&sin, addr, 0);
   1421 		sa = sintocsa(&sin);
   1422 	}
   1423 
   1424 	IF_AFDATA_RLOCK(ifp);
   1425 	la = lla_lookup(LLTABLE(ifp), flags, sa);
   1426 	IF_AFDATA_RUNLOCK(ifp);
   1427 
   1428 	return la;
   1429 }
   1430 
   1431 static struct llentry *
   1432 arpcreate(struct ifnet *ifp, const struct in_addr *addr,
   1433     const struct sockaddr *sa, int wlock)
   1434 {
   1435 	struct sockaddr_in sin;
   1436 	struct llentry *la;
   1437 	int flags = wlock ? LLE_EXCLUSIVE : 0;
   1438 
   1439 	if (sa == NULL) {
   1440 		KASSERT(addr != NULL);
   1441 		sockaddr_in_init(&sin, addr, 0);
   1442 		sa = sintocsa(&sin);
   1443 	}
   1444 
   1445 	la = arplookup(ifp, addr, sa, wlock);
   1446 
   1447 	if (la == NULL) {
   1448 		struct rtentry *rt;
   1449 
   1450 		rt = rtalloc1(sa, 0);
   1451 		IF_AFDATA_WLOCK(ifp);
   1452 		la = lla_create(LLTABLE(ifp), flags, sa, rt);
   1453 		IF_AFDATA_WUNLOCK(ifp);
   1454 		if (rt != NULL)
   1455 			rt_unref(rt);
   1456 
   1457 		if (la != NULL)
   1458 			arp_init_llentry(ifp, la);
   1459 	}
   1460 
   1461 	return la;
   1462 }
   1463 
   1464 int
   1465 arpioctl(u_long cmd, void *data)
   1466 {
   1467 
   1468 	return EOPNOTSUPP;
   1469 }
   1470 
   1471 void
   1472 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
   1473 {
   1474 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
   1475 
   1476 	ifa->ifa_rtrequest = arp_rtrequest;
   1477 	ifa->ifa_flags |= RTF_CONNECTED;
   1478 
   1479 	/* ARP will handle DAD for this address. */
   1480 	if (in_nullhost(IA_SIN(ifa)->sin_addr)) {
   1481 		if (ia->ia_dad_stop != NULL)	/* safety */
   1482 			ia->ia_dad_stop(ifa);
   1483 		ia->ia_dad_start = NULL;
   1484 		ia->ia_dad_stop = NULL;
   1485 		ia->ia4_flags &= ~IN_IFF_TENTATIVE;
   1486 	} else {
   1487 		ia->ia_dad_start = arp_dad_start;
   1488 		ia->ia_dad_stop = arp_dad_stop;
   1489 		if (ia->ia4_flags & IN_IFF_TRYTENTATIVE && ip_dad_count > 0)
   1490 			ia->ia4_flags |= IN_IFF_TENTATIVE;
   1491 		else
   1492 			arpannounce1(ifa);
   1493 	}
   1494 }
   1495 
   1496 TAILQ_HEAD(dadq_head, dadq);
   1497 struct dadq {
   1498 	TAILQ_ENTRY(dadq) dad_list;
   1499 	struct ifaddr *dad_ifa;
   1500 	int dad_count;		/* max ARP to send */
   1501 	int dad_arp_tcount;	/* # of trials to send ARP */
   1502 	int dad_arp_ocount;	/* ARP sent so far */
   1503 	int dad_arp_announce;	/* max ARP announcements */
   1504 	int dad_arp_acount;	/* # of announcements */
   1505 	struct callout dad_timer_ch;
   1506 };
   1507 
   1508 static struct dadq_head dadq;
   1509 static int dad_init = 0;
   1510 static int dad_maxtry = 15;     /* max # of *tries* to transmit DAD packet */
   1511 static kmutex_t arp_dad_lock;
   1512 
   1513 static struct dadq *
   1514 arp_dad_find(struct ifaddr *ifa)
   1515 {
   1516 	struct dadq *dp;
   1517 
   1518 	KASSERT(mutex_owned(&arp_dad_lock));
   1519 
   1520 	TAILQ_FOREACH(dp, &dadq, dad_list) {
   1521 		if (dp->dad_ifa == ifa)
   1522 			return dp;
   1523 	}
   1524 	return NULL;
   1525 }
   1526 
   1527 static void
   1528 arp_dad_starttimer(struct dadq *dp, int ticks)
   1529 {
   1530 
   1531 	callout_reset(&dp->dad_timer_ch, ticks,
   1532 	    (void (*)(void *))arp_dad_timer, dp);
   1533 }
   1534 
   1535 static void
   1536 arp_dad_stoptimer(struct dadq *dp)
   1537 {
   1538 
   1539 	KASSERT(mutex_owned(&arp_dad_lock));
   1540 
   1541 	TAILQ_REMOVE(&dadq, dp, dad_list);
   1542 	/* Tell the timer that dp is being destroyed. */
   1543 	dp->dad_ifa = NULL;
   1544 	callout_halt(&dp->dad_timer_ch, &arp_dad_lock);
   1545 }
   1546 
   1547 static void
   1548 arp_dad_destroytimer(struct dadq *dp)
   1549 {
   1550 
   1551 	callout_destroy(&dp->dad_timer_ch);
   1552 	KASSERT(dp->dad_ifa == NULL);
   1553 	kmem_intr_free(dp, sizeof(*dp));
   1554 }
   1555 
   1556 static void
   1557 arp_dad_output(struct dadq *dp, struct ifaddr *ifa)
   1558 {
   1559 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
   1560 	struct ifnet *ifp = ifa->ifa_ifp;
   1561 	struct in_addr sip;
   1562 
   1563 	dp->dad_arp_tcount++;
   1564 	if ((ifp->if_flags & IFF_UP) == 0)
   1565 		return;
   1566 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   1567 		return;
   1568 
   1569 	dp->dad_arp_tcount = 0;
   1570 	dp->dad_arp_ocount++;
   1571 
   1572 	memset(&sip, 0, sizeof(sip));
   1573 	arprequest(ifa->ifa_ifp, &sip, &ia->ia_addr.sin_addr,
   1574 	    CLLADDR(ifa->ifa_ifp->if_sadl));
   1575 }
   1576 
   1577 /*
   1578  * Start Duplicate Address Detection (DAD) for specified interface address.
   1579  */
   1580 static void
   1581 arp_dad_start(struct ifaddr *ifa)
   1582 {
   1583 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
   1584 	struct dadq *dp;
   1585 	char ipbuf[INET_ADDRSTRLEN];
   1586 
   1587 	if (!dad_init) {
   1588 		TAILQ_INIT(&dadq);
   1589 		mutex_init(&arp_dad_lock, MUTEX_DEFAULT, IPL_NONE);
   1590 		dad_init++;
   1591 	}
   1592 
   1593 	/*
   1594 	 * If we don't need DAD, don't do it.
   1595 	 * - DAD is disabled (ip_dad_count == 0)
   1596 	 */
   1597 	if (!(ia->ia4_flags & IN_IFF_TENTATIVE)) {
   1598 		log(LOG_DEBUG,
   1599 		    "%s: called with non-tentative address %s(%s)\n", __func__,
   1600 		    IN_PRINT(ipbuf, &ia->ia_addr.sin_addr),
   1601 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1602 		return;
   1603 	}
   1604 	if (!ip_dad_count) {
   1605 		ia->ia4_flags &= ~IN_IFF_TENTATIVE;
   1606 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1607 		arpannounce1(ifa);
   1608 		return;
   1609 	}
   1610 	KASSERT(ifa->ifa_ifp != NULL);
   1611 	if (!(ifa->ifa_ifp->if_flags & IFF_UP))
   1612 		return;
   1613 
   1614 	dp = kmem_intr_alloc(sizeof(*dp), KM_NOSLEEP);
   1615 
   1616 	mutex_enter(&arp_dad_lock);
   1617 	if (arp_dad_find(ifa) != NULL) {
   1618 		mutex_exit(&arp_dad_lock);
   1619 		/* DAD already in progress */
   1620 		if (dp != NULL)
   1621 			kmem_intr_free(dp, sizeof(*dp));
   1622 		return;
   1623 	}
   1624 
   1625 	if (dp == NULL) {
   1626 		mutex_exit(&arp_dad_lock);
   1627 		log(LOG_ERR, "%s: memory allocation failed for %s(%s)\n",
   1628 		    __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr),
   1629 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1630 		return;
   1631 	}
   1632 
   1633 	/*
   1634 	 * Send ARP packet for DAD, ip_dad_count times.
   1635 	 * Note that we must delay the first transmission.
   1636 	 */
   1637 	callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
   1638 	dp->dad_ifa = ifa;
   1639 	ifaref(ifa);	/* just for safety */
   1640 	dp->dad_count = ip_dad_count;
   1641 	dp->dad_arp_announce = 0; /* Will be set when starting to announce */
   1642 	dp->dad_arp_acount = dp->dad_arp_ocount = dp->dad_arp_tcount = 0;
   1643 	TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
   1644 
   1645 	ARPLOG(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
   1646 	    ARPLOGADDR(&ia->ia_addr.sin_addr));
   1647 
   1648 	arp_dad_starttimer(dp, cprng_fast32() % (PROBE_WAIT * hz));
   1649 
   1650 	mutex_exit(&arp_dad_lock);
   1651 }
   1652 
   1653 /*
   1654  * terminate DAD unconditionally.  used for address removals.
   1655  */
   1656 static void
   1657 arp_dad_stop(struct ifaddr *ifa)
   1658 {
   1659 	struct dadq *dp;
   1660 
   1661 	if (!dad_init)
   1662 		return;
   1663 
   1664 	mutex_enter(&arp_dad_lock);
   1665 	dp = arp_dad_find(ifa);
   1666 	if (dp == NULL) {
   1667 		mutex_exit(&arp_dad_lock);
   1668 		/* DAD wasn't started yet */
   1669 		return;
   1670 	}
   1671 
   1672 	arp_dad_stoptimer(dp);
   1673 
   1674 	mutex_exit(&arp_dad_lock);
   1675 
   1676 	arp_dad_destroytimer(dp);
   1677 	ifafree(ifa);
   1678 }
   1679 
   1680 static void
   1681 arp_dad_timer(struct dadq *dp)
   1682 {
   1683 	struct ifaddr *ifa;
   1684 	struct in_ifaddr *ia;
   1685 	char ipbuf[INET_ADDRSTRLEN];
   1686 	bool need_free = false;
   1687 
   1688 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
   1689 	mutex_enter(&arp_dad_lock);
   1690 
   1691 	ifa = dp->dad_ifa;
   1692 	if (ifa == NULL) {
   1693 		/* dp is being destroyed by someone.  Do nothing. */
   1694 		goto done;
   1695 	}
   1696 
   1697 	ia = (struct in_ifaddr *)ifa;
   1698 	if (ia->ia4_flags & IN_IFF_DUPLICATED) {
   1699 		log(LOG_ERR, "%s: called with duplicate address %s(%s)\n",
   1700 		    __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr),
   1701 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1702 		goto done;
   1703 	}
   1704 	if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0 && dp->dad_arp_acount == 0)
   1705 	{
   1706 		log(LOG_ERR, "%s: called with non-tentative address %s(%s)\n",
   1707 		    __func__, IN_PRINT(ipbuf, &ia->ia_addr.sin_addr),
   1708 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1709 		goto done;
   1710 	}
   1711 
   1712 	/* timeouted with IFF_{RUNNING,UP} check */
   1713 	if (dp->dad_arp_tcount > dad_maxtry) {
   1714 		ARPLOG(LOG_INFO, "%s: could not run DAD, driver problem?\n",
   1715 		    if_name(ifa->ifa_ifp));
   1716 
   1717 		arp_dad_stoptimer(dp);
   1718 		need_free = true;
   1719 		goto done;
   1720 	}
   1721 
   1722 	/* Need more checks? */
   1723 	if (dp->dad_arp_ocount < dp->dad_count) {
   1724 		int adelay;
   1725 
   1726 		/*
   1727 		 * We have more ARP to go.  Send ARP packet for DAD.
   1728 		 */
   1729 		arp_dad_output(dp, ifa);
   1730 		if (dp->dad_arp_ocount < dp->dad_count)
   1731 			adelay = (PROBE_MIN * hz) +
   1732 			    (cprng_fast32() %
   1733 			    ((PROBE_MAX * hz) - (PROBE_MIN * hz)));
   1734 		else
   1735 			adelay = ANNOUNCE_WAIT * hz;
   1736 		arp_dad_starttimer(dp, adelay);
   1737 		goto done;
   1738 	} else if (dp->dad_arp_acount == 0) {
   1739 		/*
   1740 		 * We are done with DAD.
   1741 		 * No duplicate address found.
   1742 		 */
   1743 		ia->ia4_flags &= ~IN_IFF_TENTATIVE;
   1744 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1745 		ARPLOG(LOG_DEBUG,
   1746 		    "%s: DAD complete for %s - no duplicates found\n",
   1747 		    if_name(ifa->ifa_ifp), ARPLOGADDR(&ia->ia_addr.sin_addr));
   1748 		dp->dad_arp_announce = ANNOUNCE_NUM;
   1749 		goto announce;
   1750 	} else if (dp->dad_arp_acount < dp->dad_arp_announce) {
   1751 announce:
   1752 		/*
   1753 		 * Announce the address.
   1754 		 */
   1755 		arpannounce1(ifa);
   1756 		dp->dad_arp_acount++;
   1757 		if (dp->dad_arp_acount < dp->dad_arp_announce) {
   1758 			arp_dad_starttimer(dp, ANNOUNCE_INTERVAL * hz);
   1759 			goto done;
   1760 		}
   1761 		ARPLOG(LOG_DEBUG,
   1762 		    "%s: ARP announcement complete for %s\n",
   1763 		    if_name(ifa->ifa_ifp), ARPLOGADDR(&ia->ia_addr.sin_addr));
   1764 	}
   1765 
   1766 	arp_dad_stoptimer(dp);
   1767 	need_free = true;
   1768 done:
   1769 	mutex_exit(&arp_dad_lock);
   1770 
   1771 	if (need_free) {
   1772 		arp_dad_destroytimer(dp);
   1773 		KASSERT(ifa != NULL);
   1774 		ifafree(ifa);
   1775 	}
   1776 
   1777 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   1778 }
   1779 
   1780 static void
   1781 arp_dad_duplicated(struct ifaddr *ifa, const char *sha)
   1782 {
   1783 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
   1784 	struct ifnet *ifp = ifa->ifa_ifp;
   1785 	char ipbuf[INET_ADDRSTRLEN];
   1786 	const char *iastr;
   1787 
   1788 	iastr = IN_PRINT(ipbuf, &ia->ia_addr.sin_addr);
   1789 
   1790 	if (ia->ia4_flags & (IN_IFF_TENTATIVE|IN_IFF_DUPLICATED)) {
   1791 		log(LOG_ERR,
   1792 		    "%s: DAD duplicate address %s from %s\n",
   1793 		    if_name(ifp), iastr, sha);
   1794 	} else if (ia->ia_dad_defended == 0 ||
   1795 		   ia->ia_dad_defended < time_uptime - DEFEND_INTERVAL) {
   1796 		ia->ia_dad_defended = time_uptime;
   1797 		arpannounce1(ifa);
   1798 		log(LOG_ERR,
   1799 		    "%s: DAD defended address %s from %s\n",
   1800 		    if_name(ifp), iastr, sha);
   1801 		return;
   1802 	} else {
   1803 		/* If DAD is disabled, just report the duplicate. */
   1804 		if (ip_dad_count == 0) {
   1805 			log(LOG_ERR,
   1806 			    "%s: DAD ignoring duplicate address %s from %s\n",
   1807 			    if_name(ifp), iastr, sha);
   1808 			return;
   1809 		}
   1810 		log(LOG_ERR,
   1811 		    "%s: DAD defence failed for %s from %s\n",
   1812 		    if_name(ifp), iastr, sha);
   1813 	}
   1814 
   1815 	arp_dad_stop(ifa);
   1816 
   1817 	ia->ia4_flags &= ~IN_IFF_TENTATIVE;
   1818 	if ((ia->ia4_flags & IN_IFF_DUPLICATED) == 0) {
   1819 		ia->ia4_flags |= IN_IFF_DUPLICATED;
   1820 		/* Inform the routing socket of the duplicate address */
   1821 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1822 	}
   1823 }
   1824 
   1825 /*
   1826  * Called from 10 Mb/s Ethernet interrupt handlers
   1827  * when ether packet type ETHERTYPE_REVARP
   1828  * is received.  Common length and type checks are done here,
   1829  * then the protocol-specific routine is called.
   1830  */
   1831 void
   1832 revarpinput(struct mbuf *m)
   1833 {
   1834 	struct arphdr *ar;
   1835 	int arplen;
   1836 
   1837 	arplen = sizeof(struct arphdr);
   1838 	if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
   1839 		return;
   1840 	ar = mtod(m, struct arphdr *);
   1841 
   1842 	if (ntohs(ar->ar_hrd) == ARPHRD_IEEE1394) {
   1843 		goto out;
   1844 	}
   1845 
   1846 	arplen = sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln);
   1847 	if (m->m_len < arplen && (m = m_pullup(m, arplen)) == NULL)
   1848 		return;
   1849 	ar = mtod(m, struct arphdr *);
   1850 
   1851 	switch (ntohs(ar->ar_pro)) {
   1852 	case ETHERTYPE_IP:
   1853 	case ETHERTYPE_IPTRAILERS:
   1854 		in_revarpinput(m);
   1855 		return;
   1856 
   1857 	default:
   1858 		break;
   1859 	}
   1860 
   1861 out:
   1862 	m_freem(m);
   1863 }
   1864 
   1865 /*
   1866  * RARP for Internet protocols on 10 Mb/s Ethernet.
   1867  * Algorithm is that given in RFC 903.
   1868  * We are only using for bootstrap purposes to get an ip address for one of
   1869  * our interfaces.  Thus we support no user-interface.
   1870  *
   1871  * Since the contents of the RARP reply are specific to the interface that
   1872  * sent the request, this code must ensure that they are properly associated.
   1873  *
   1874  * Note: also supports ARP via RARP packets, per the RFC.
   1875  */
   1876 void
   1877 in_revarpinput(struct mbuf *m)
   1878 {
   1879 	struct arphdr *ah;
   1880 	void *tha;
   1881 	int op;
   1882 	struct ifnet *rcvif;
   1883 	int s;
   1884 
   1885 	ah = mtod(m, struct arphdr *);
   1886 	op = ntohs(ah->ar_op);
   1887 
   1888 	rcvif = m_get_rcvif(m, &s);
   1889 	if (__predict_false(rcvif == NULL))
   1890 		goto out;
   1891 	if (rcvif->if_flags & IFF_NOARP)
   1892 		goto out;
   1893 
   1894 	switch (rcvif->if_type) {
   1895 	case IFT_IEEE1394:
   1896 		/* ARP without target hardware address is not supported */
   1897 		goto out;
   1898 	default:
   1899 		break;
   1900 	}
   1901 
   1902 	switch (op) {
   1903 	case ARPOP_REQUEST:
   1904 	case ARPOP_REPLY:	/* per RFC */
   1905 		m_put_rcvif(rcvif, &s);
   1906 		in_arpinput(m);
   1907 		return;
   1908 	case ARPOP_REVREPLY:
   1909 		break;
   1910 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
   1911 	default:
   1912 		goto out;
   1913 	}
   1914 	if (!revarp_in_progress)
   1915 		goto out;
   1916 	if (rcvif != myip_ifp) /* !same interface */
   1917 		goto out;
   1918 	if (myip_initialized)
   1919 		goto wake;
   1920 	tha = ar_tha(ah);
   1921 	if (tha == NULL)
   1922 		goto out;
   1923 	if (ah->ar_pln != sizeof(struct in_addr))
   1924 		goto out;
   1925 	if (ah->ar_hln != rcvif->if_sadl->sdl_alen)
   1926 		goto out;
   1927 	if (memcmp(tha, CLLADDR(rcvif->if_sadl), rcvif->if_sadl->sdl_alen))
   1928 		goto out;
   1929 	memcpy(&srv_ip, ar_spa(ah), sizeof(srv_ip));
   1930 	memcpy(&myip, ar_tpa(ah), sizeof(myip));
   1931 	myip_initialized = 1;
   1932 wake:	/* Do wakeup every time in case it was missed. */
   1933 	wakeup((void *)&myip);
   1934 
   1935 out:
   1936 	m_put_rcvif(rcvif, &s);
   1937 	m_freem(m);
   1938 }
   1939 
   1940 /*
   1941  * Send a RARP request for the ip address of the specified interface.
   1942  * The request should be RFC 903-compliant.
   1943  */
   1944 static void
   1945 revarprequest(struct ifnet *ifp)
   1946 {
   1947 	struct sockaddr sa;
   1948 	struct mbuf *m;
   1949 	struct arphdr *ah;
   1950 	void *tha;
   1951 
   1952 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
   1953 		return;
   1954 	MCLAIM(m, &arpdomain.dom_mowner);
   1955 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
   1956 	    2*ifp->if_addrlen;
   1957 	m->m_pkthdr.len = m->m_len;
   1958 	MH_ALIGN(m, m->m_len);
   1959 	ah = mtod(m, struct arphdr *);
   1960 	memset(ah, 0, m->m_len);
   1961 	ah->ar_pro = htons(ETHERTYPE_IP);
   1962 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
   1963 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
   1964 	ah->ar_op = htons(ARPOP_REVREQUEST);
   1965 
   1966 	memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
   1967 	tha = ar_tha(ah);
   1968 	if (tha == NULL) {
   1969 		m_free(m);
   1970 		return;
   1971 	}
   1972 	memcpy(tha, CLLADDR(ifp->if_sadl), ah->ar_hln);
   1973 
   1974 	sa.sa_family = AF_ARP;
   1975 	sa.sa_len = 2;
   1976 	m->m_flags |= M_BCAST;
   1977 
   1978 	if_output_lock(ifp, ifp, m, &sa, NULL);
   1979 }
   1980 
   1981 /*
   1982  * RARP for the ip address of the specified interface, but also
   1983  * save the ip address of the server that sent the answer.
   1984  * Timeout if no response is received.
   1985  */
   1986 int
   1987 revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in,
   1988     struct in_addr *clnt_in)
   1989 {
   1990 	int result, count = 20;
   1991 
   1992 	myip_initialized = 0;
   1993 	myip_ifp = ifp;
   1994 
   1995 	revarp_in_progress = 1;
   1996 	while (count--) {
   1997 		revarprequest(ifp);
   1998 		result = tsleep((void *)&myip, PSOCK, "revarp", hz/2);
   1999 		if (result != EWOULDBLOCK)
   2000 			break;
   2001 	}
   2002 	revarp_in_progress = 0;
   2003 
   2004 	if (!myip_initialized)
   2005 		return ENETUNREACH;
   2006 
   2007 	memcpy(serv_in, &srv_ip, sizeof(*serv_in));
   2008 	memcpy(clnt_in, &myip, sizeof(*clnt_in));
   2009 	return 0;
   2010 }
   2011 
   2012 void
   2013 arp_stat_add(int type, uint64_t count)
   2014 {
   2015 	ARP_STATADD(type, count);
   2016 }
   2017 
   2018 static int
   2019 sysctl_net_inet_arp_stats(SYSCTLFN_ARGS)
   2020 {
   2021 
   2022 	return NETSTAT_SYSCTL(arpstat_percpu, ARP_NSTATS);
   2023 }
   2024 
   2025 static void
   2026 sysctl_net_inet_arp_setup(struct sysctllog **clog)
   2027 {
   2028 	const struct sysctlnode *node;
   2029 
   2030 	sysctl_createv(clog, 0, NULL, NULL,
   2031 			CTLFLAG_PERMANENT,
   2032 			CTLTYPE_NODE, "inet", NULL,
   2033 			NULL, 0, NULL, 0,
   2034 			CTL_NET, PF_INET, CTL_EOL);
   2035 	sysctl_createv(clog, 0, NULL, &node,
   2036 			CTLFLAG_PERMANENT,
   2037 			CTLTYPE_NODE, "arp",
   2038 			SYSCTL_DESCR("Address Resolution Protocol"),
   2039 			NULL, 0, NULL, 0,
   2040 			CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
   2041 
   2042 	sysctl_createv(clog, 0, NULL, NULL,
   2043 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2044 			CTLTYPE_INT, "keep",
   2045 			SYSCTL_DESCR("Valid ARP entry lifetime in seconds"),
   2046 			NULL, 0, &arpt_keep, 0,
   2047 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2048 
   2049 	sysctl_createv(clog, 0, NULL, NULL,
   2050 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2051 			CTLTYPE_INT, "down",
   2052 			SYSCTL_DESCR("Failed ARP entry lifetime in seconds"),
   2053 			NULL, 0, &arpt_down, 0,
   2054 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2055 
   2056 	sysctl_createv(clog, 0, NULL, NULL,
   2057 			CTLFLAG_PERMANENT,
   2058 			CTLTYPE_STRUCT, "stats",
   2059 			SYSCTL_DESCR("ARP statistics"),
   2060 			sysctl_net_inet_arp_stats, 0, NULL, 0,
   2061 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2062 
   2063 	sysctl_createv(clog, 0, NULL, NULL,
   2064 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2065 			CTLTYPE_INT, "log_movements",
   2066 			SYSCTL_DESCR("log ARP replies from MACs different than"
   2067 			    " the one in the cache"),
   2068 			NULL, 0, &log_movements, 0,
   2069 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2070 
   2071 	sysctl_createv(clog, 0, NULL, NULL,
   2072 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2073 			CTLTYPE_INT, "log_permanent_modify",
   2074 			SYSCTL_DESCR("log ARP replies from MACs different than"
   2075 			    " the one in the permanent arp entry"),
   2076 			NULL, 0, &log_permanent_modify, 0,
   2077 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2078 
   2079 	sysctl_createv(clog, 0, NULL, NULL,
   2080 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2081 			CTLTYPE_INT, "log_wrong_iface",
   2082 			SYSCTL_DESCR("log ARP packets arriving on the wrong"
   2083 			    " interface"),
   2084 			NULL, 0, &log_wrong_iface, 0,
   2085 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2086 
   2087 	sysctl_createv(clog, 0, NULL, NULL,
   2088 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2089 			CTLTYPE_INT, "log_unknown_network",
   2090 			SYSCTL_DESCR("log ARP packets from non-local network"),
   2091 			NULL, 0, &log_unknown_network, 0,
   2092 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2093 
   2094 	sysctl_createv(clog, 0, NULL, NULL,
   2095 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2096 			CTLTYPE_INT, "debug",
   2097 			SYSCTL_DESCR("Enable ARP DAD debug output"),
   2098 			NULL, 0, &arp_debug, 0,
   2099 			CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2100 }
   2101 
   2102 #endif /* INET */
   2103