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