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