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