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