Home | History | Annotate | Line # | Download | only in netinet
      1 /*	$NetBSD: if_arp.c,v 1.247 2017/03/17 16:15:11 roy 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.247 2017/03/17 16:15:11 roy 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 
    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 			ARP_STATINC(ARP_STAT_DFRTOTAL);
    837 		}
    838 	}
    839 	if (la->la_hold != NULL) {
    840 		struct mbuf *curr = la->la_hold;
    841 		while (curr->m_nextpkt != NULL)
    842 			curr = curr->m_nextpkt;
    843 		curr->m_nextpkt = m;
    844 	} else
    845 		la->la_hold = m;
    846 	la->la_numheld++;
    847 	if (!renew)
    848 		LLE_DOWNGRADE(la);
    849 
    850 	/*
    851 	 * Return EWOULDBLOCK if we have tried less than arp_maxtries. It
    852 	 * will be masked by ether_output(). Return EHOSTDOWN/EHOSTUNREACH
    853 	 * if we have already sent arp_maxtries ARP requests. Retransmit the
    854 	 * ARP request, but not faster than one request per second.
    855 	 */
    856 	if (la->la_asked < arp_maxtries)
    857 		error = EWOULDBLOCK;	/* First request. */
    858 	else
    859 		error = (rt != NULL && rt->rt_flags & RTF_GATEWAY) ?
    860 		    EHOSTUNREACH : EHOSTDOWN;
    861 
    862 	if (renew) {
    863 		const u_int8_t *enaddr =
    864 #if NCARP > 0
    865 		    (rt != NULL && rt->rt_ifp->if_type == IFT_CARP) ?
    866 		    CLLADDR(rt->rt_ifp->if_sadl):
    867 #endif
    868 		    CLLADDR(ifp->if_sadl);
    869 		la->la_expire = time_uptime;
    870 		arp_settimer(la, arpt_down);
    871 		la->la_asked++;
    872 		LLE_WUNLOCK(la);
    873 
    874 		if (rt != NULL) {
    875 			arprequest(ifp, &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
    876 			    &satocsin(dst)->sin_addr, enaddr);
    877 		} else {
    878 			struct sockaddr_in sin;
    879 			struct rtentry *_rt;
    880 
    881 			sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
    882 
    883 			/* XXX */
    884 			_rt = rtalloc1((struct sockaddr *)&sin, 0);
    885 			if (_rt == NULL)
    886 				goto bad;
    887 			arprequest(ifp,
    888 			    &satocsin(_rt->rt_ifa->ifa_addr)->sin_addr,
    889 			    &satocsin(dst)->sin_addr, enaddr);
    890 			rt_unref(_rt);
    891 		}
    892 		return error;
    893 	}
    894 
    895 	LLE_RUNLOCK(la);
    896 	return error;
    897 
    898 bad:
    899 	m_freem(m);
    900 	return error;
    901 }
    902 
    903 /*
    904  * Common length and type checks are done here,
    905  * then the protocol-specific routine is called.
    906  */
    907 void
    908 arpintr(void)
    909 {
    910 	struct mbuf *m;
    911 	struct arphdr *ar;
    912 	int s;
    913 	int arplen;
    914 
    915 #ifndef NET_MPSAFE
    916 	mutex_enter(softnet_lock);
    917 	KERNEL_LOCK(1, NULL);
    918 #endif
    919 	for (;;) {
    920 		struct ifnet *rcvif;
    921 
    922 		IFQ_LOCK(&arpintrq);
    923 		IF_DEQUEUE(&arpintrq, m);
    924 		IFQ_UNLOCK(&arpintrq);
    925 		if (m == NULL)
    926 			goto out;
    927 		if ((m->m_flags & M_PKTHDR) == 0)
    928 			panic("arpintr");
    929 
    930 		MCLAIM(m, &arpdomain.dom_mowner);
    931 		ARP_STATINC(ARP_STAT_RCVTOTAL);
    932 
    933 		/*
    934 		 * First, make sure we have at least struct arphdr.
    935 		 */
    936 		if (m->m_len < sizeof(struct arphdr) ||
    937 		    (ar = mtod(m, struct arphdr *)) == NULL)
    938 			goto badlen;
    939 
    940 		rcvif = m_get_rcvif(m, &s);
    941 		if (__predict_false(rcvif == NULL)) {
    942 			ARP_STATINC(ARP_STAT_RCVNOINT);
    943 			goto free;
    944 		}
    945 		switch (rcvif->if_type) {
    946 		case IFT_IEEE1394:
    947 			arplen = sizeof(struct arphdr) +
    948 			    ar->ar_hln + 2 * ar->ar_pln;
    949 			break;
    950 		default:
    951 			arplen = sizeof(struct arphdr) +
    952 			    2 * ar->ar_hln + 2 * ar->ar_pln;
    953 			break;
    954 		}
    955 		m_put_rcvif(rcvif, &s);
    956 
    957 		if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
    958 		    m->m_len >= arplen)
    959 			switch (ntohs(ar->ar_pro)) {
    960 			case ETHERTYPE_IP:
    961 			case ETHERTYPE_IPTRAILERS:
    962 				in_arpinput(m);
    963 				continue;
    964 			default:
    965 				ARP_STATINC(ARP_STAT_RCVBADPROTO);
    966 			}
    967 		else {
    968 badlen:
    969 			ARP_STATINC(ARP_STAT_RCVBADLEN);
    970 		}
    971 free:
    972 		m_freem(m);
    973 	}
    974 out:
    975 #ifndef NET_MPSAFE
    976 	KERNEL_UNLOCK_ONE(NULL);
    977 	mutex_exit(softnet_lock);
    978 #else
    979 	return; /* XXX gcc */
    980 #endif
    981 }
    982 
    983 /*
    984  * ARP for Internet protocols on 10 Mb/s Ethernet. Algorithm is that given in
    985  * RFC 826. In addition, a sanity check is performed on the sender protocol
    986  * address, to catch impersonators.
    987  *
    988  * We no longer handle negotiations for use of trailer protocol: formerly, ARP
    989  * replied for protocol type ETHERTYPE_TRAIL sent along with IP replies if we
    990  * wanted trailers sent to us, and also sent them in response to IP replies.
    991  * This allowed either end to announce the desire to receive trailer packets.
    992  *
    993  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either, but
    994  * formerly didn't normally send requests.
    995  */
    996 static void
    997 in_arpinput(struct mbuf *m)
    998 {
    999 	struct arphdr *ah;
   1000 	struct ifnet *ifp, *rcvif = NULL;
   1001 	struct llentry *la = NULL;
   1002 	struct in_ifaddr *ia = NULL;
   1003 #if NBRIDGE > 0
   1004 	struct in_ifaddr *bridge_ia = NULL;
   1005 #endif
   1006 #if NCARP > 0
   1007 	u_int32_t count = 0, index = 0;
   1008 #endif
   1009 	struct sockaddr sa;
   1010 	struct in_addr isaddr, itaddr, myaddr;
   1011 	int op;
   1012 	void *tha;
   1013 	uint64_t *arps;
   1014 	struct psref psref, psref_ia;
   1015 	int s;
   1016 	char llabuf[LLA_ADDRSTRLEN];
   1017 	char ipbuf[INET_ADDRSTRLEN];
   1018 	bool do_dad;
   1019 
   1020 	if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
   1021 		goto out;
   1022 	ah = mtod(m, struct arphdr *);
   1023 	op = ntohs(ah->ar_op);
   1024 
   1025 	rcvif = ifp = m_get_rcvif_psref(m, &psref);
   1026 	if (__predict_false(rcvif == NULL))
   1027 		goto drop;
   1028 
   1029 	/*
   1030 	 * Fix up ah->ar_hrd if necessary, before using ar_tha() or ar_tpa().
   1031 	 * XXX check ar_hrd more strictly?
   1032 	 */
   1033 	switch (ifp->if_type) {
   1034 	case IFT_IEEE1394:
   1035 		if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
   1036 			;
   1037 		else {
   1038 			/* XXX this is to make sure we compute ar_tha right */
   1039 			ah->ar_hrd = htons(ARPHRD_IEEE1394);
   1040 		}
   1041 		break;
   1042 	default:
   1043 		break;
   1044 	}
   1045 
   1046 	if (ah->ar_pln != sizeof(struct in_addr))
   1047 		goto drop;
   1048 
   1049 	memcpy(&isaddr, ar_spa(ah), sizeof(isaddr));
   1050 	memcpy(&itaddr, ar_tpa(ah), sizeof(itaddr));
   1051 
   1052 	if (m->m_flags & (M_BCAST|M_MCAST))
   1053 		ARP_STATINC(ARP_STAT_RCVMCAST);
   1054 
   1055 	/*
   1056 	 * Search for a matching interface address
   1057 	 * or any address on the interface to use
   1058 	 * as a dummy address in the rest of this function.
   1059 	 *
   1060 	 * If the target IP address is zero then try and find
   1061 	 * the sender address for DAD.
   1062 	 */
   1063 	myaddr = in_nullhost(itaddr) ? isaddr : itaddr;
   1064 	s = pserialize_read_enter();
   1065 	IN_ADDRHASH_READER_FOREACH(ia, myaddr.s_addr) {
   1066 		if (!in_hosteq(ia->ia_addr.sin_addr, myaddr))
   1067 			continue;
   1068 #if NCARP > 0
   1069 		if (ia->ia_ifp->if_type == IFT_CARP &&
   1070 		    ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
   1071 		    (IFF_UP|IFF_RUNNING))) {
   1072 			index++;
   1073 			/* XXX: ar_hln? */
   1074 			if (ia->ia_ifp == rcvif && (ah->ar_hln >= 6) &&
   1075 			    carp_iamatch(ia, ar_sha(ah),
   1076 			    &count, index)) {
   1077 				break;
   1078 			}
   1079 		} else
   1080 #endif
   1081 		if (ia->ia_ifp == rcvif)
   1082 			break;
   1083 #if NBRIDGE > 0
   1084 		/*
   1085 		 * If the interface we received the packet on
   1086 		 * is part of a bridge, check to see if we need
   1087 		 * to "bridge" the packet to ourselves at this
   1088 		 * layer.  Note we still prefer a perfect match,
   1089 		 * but allow this weaker match if necessary.
   1090 		 */
   1091 		if (rcvif->if_bridge != NULL &&
   1092 		    rcvif->if_bridge == ia->ia_ifp->if_bridge)
   1093 			bridge_ia = ia;
   1094 #endif /* NBRIDGE > 0 */
   1095 	}
   1096 
   1097 #if NBRIDGE > 0
   1098 	if (ia == NULL && bridge_ia != NULL) {
   1099 		ia = bridge_ia;
   1100 		m_put_rcvif_psref(rcvif, &psref);
   1101 		rcvif = NULL;
   1102 		/* FIXME */
   1103 		ifp = bridge_ia->ia_ifp;
   1104 	}
   1105 #endif
   1106 	if (ia != NULL)
   1107 		ia4_acquire(ia, &psref_ia);
   1108 	pserialize_read_exit(s);
   1109 
   1110 	if (ah->ar_hln != ifp->if_addrlen) {
   1111 		ARP_STATINC(ARP_STAT_RCVBADLEN);
   1112 		log(LOG_WARNING,
   1113 		    "arp from %s: addr len: new %d, i/f %d (ignored)\n",
   1114 		    in_fmtaddr(ipbuf, isaddr), ah->ar_hln, ifp->if_addrlen);
   1115 		goto out;
   1116 	}
   1117 
   1118 	/* Only do DaD if we have a matching address. */
   1119 	do_dad = (ia != NULL);
   1120 
   1121 	if (ia == NULL) {
   1122 		ia = in_get_ia_on_iface_psref(isaddr, rcvif, &psref_ia);
   1123 		if (ia == NULL) {
   1124 			ia = in_get_ia_from_ifp_psref(ifp, &psref_ia);
   1125 			if (ia == NULL) {
   1126 				ARP_STATINC(ARP_STAT_RCVNOINT);
   1127 				goto out;
   1128 			}
   1129 		}
   1130 	}
   1131 
   1132 	myaddr = ia->ia_addr.sin_addr;
   1133 
   1134 	/* XXX checks for bridge case? */
   1135 	if (!memcmp(ar_sha(ah), CLLADDR(ifp->if_sadl), ifp->if_addrlen)) {
   1136 		ARP_STATINC(ARP_STAT_RCVLOCALSHA);
   1137 		goto out;	/* it's from me, ignore it. */
   1138 	}
   1139 
   1140 	/* XXX checks for bridge case? */
   1141 	if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
   1142 		ARP_STATINC(ARP_STAT_RCVBCASTSHA);
   1143 		log(LOG_ERR,
   1144 		    "%s: arp: link address is broadcast for IP address %s!\n",
   1145 		    ifp->if_xname, in_fmtaddr(ipbuf, isaddr));
   1146 		goto out;
   1147 	}
   1148 
   1149 	/*
   1150 	 * If the source IP address is zero, this is an RFC 5227 ARP probe
   1151 	 */
   1152 	if (in_nullhost(isaddr))
   1153 		ARP_STATINC(ARP_STAT_RCVZEROSPA);
   1154 	else if (in_hosteq(isaddr, myaddr))
   1155 		ARP_STATINC(ARP_STAT_RCVLOCALSPA);
   1156 
   1157 	if (in_nullhost(itaddr))
   1158 		ARP_STATINC(ARP_STAT_RCVZEROTPA);
   1159 
   1160 	/*
   1161 	 * DAD check, RFC 5227.
   1162 	 * Collision on sender address is always a duplicate.
   1163 	 * Collision on target address is only a duplicate IF
   1164 	 * the sender address is the null host (ie a DAD probe) AND
   1165 	 * our address is in the TENTATIVE state.
   1166 	 * DUPLICATED state is also checked so that processing stops here
   1167 	 * and an error can be logged.
   1168 	 */
   1169 	if (do_dad &&
   1170 	    (in_hosteq(isaddr, myaddr) ||
   1171 	    (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr)
   1172 	    && ia->ia4_flags & (IN_IFF_TENTATIVE | IN_IFF_DUPLICATED))))
   1173 	{
   1174 		arp_dad_duplicated((struct ifaddr *)ia,
   1175 		    lla_snprintf(llabuf, ar_sha(ah), ah->ar_hln));
   1176 		goto out;
   1177 	}
   1178 
   1179 	/*
   1180 	 * If the target IP address is zero, ignore the packet.
   1181 	 * This prevents the code below from trying to answer
   1182 	 * when we are using IP address zero (booting).
   1183 	 */
   1184 	if (in_nullhost(itaddr))
   1185 		goto out;
   1186 
   1187 	if (in_nullhost(isaddr))
   1188 		goto reply;
   1189 
   1190 	if (in_hosteq(itaddr, myaddr))
   1191 		la = arpcreate(ifp, m, &isaddr, NULL, 1);
   1192 	else
   1193 		la = arplookup(ifp, m, &isaddr, NULL, 1);
   1194 	if (la == NULL)
   1195 		goto reply;
   1196 
   1197 	if ((la->la_flags & LLE_VALID) &&
   1198 	    memcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
   1199 		if (la->la_flags & LLE_STATIC) {
   1200 			ARP_STATINC(ARP_STAT_RCVOVERPERM);
   1201 			if (!log_permanent_modify)
   1202 				goto out;
   1203 			log(LOG_INFO,
   1204 			    "%s tried to overwrite permanent arp info"
   1205 			    " for %s\n",
   1206 			    lla_snprintf(llabuf, ar_sha(ah), ah->ar_hln),
   1207 			    in_fmtaddr(ipbuf, isaddr));
   1208 			goto out;
   1209 		} else if (la->lle_tbl->llt_ifp != ifp) {
   1210 			/* XXX should not happen? */
   1211 			ARP_STATINC(ARP_STAT_RCVOVERINT);
   1212 			if (!log_wrong_iface)
   1213 				goto out;
   1214 			log(LOG_INFO,
   1215 			    "%s on %s tried to overwrite "
   1216 			    "arp info for %s on %s\n",
   1217 			    lla_snprintf(llabuf, ar_sha(ah), ah->ar_hln),
   1218 			    ifp->if_xname, in_fmtaddr(ipbuf, isaddr),
   1219 			    la->lle_tbl->llt_ifp->if_xname);
   1220 				goto out;
   1221 		} else {
   1222 			ARP_STATINC(ARP_STAT_RCVOVER);
   1223 			if (log_movements)
   1224 				log(LOG_INFO, "arp info overwritten "
   1225 				    "for %s by %s\n",
   1226 				    in_fmtaddr(ipbuf, isaddr),
   1227 				    lla_snprintf(llabuf, ar_sha(ah),
   1228 				    ah->ar_hln));
   1229 		}
   1230 	}
   1231 
   1232 	/* XXX llentry should have addrlen? */
   1233 #if 0
   1234 	/*
   1235 	 * sanity check for the address length.
   1236 	 * XXX this does not work for protocols with variable address
   1237 	 * length. -is
   1238 	 */
   1239 	if (sdl->sdl_alen && sdl->sdl_alen != ah->ar_hln) {
   1240 		ARP_STATINC(ARP_STAT_RCVLENCHG);
   1241 		log(LOG_WARNING,
   1242 		    "arp from %s: new addr len %d, was %d\n",
   1243 		    in_fmtaddr(ipbuf, isaddr), ah->ar_hln, sdl->sdl_alen);
   1244 	}
   1245 #endif
   1246 
   1247 #if NTOKEN > 0
   1248 	/*
   1249 	 * XXX uses m_data and assumes the complete answer including
   1250 	 * XXX token-ring headers is in the same buf
   1251 	 */
   1252 	if (ifp->if_type == IFT_ISO88025) {
   1253 		struct token_header *trh;
   1254 
   1255 		trh = (struct token_header *)M_TRHSTART(m);
   1256 		if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
   1257 			struct token_rif *rif;
   1258 			size_t riflen;
   1259 
   1260 			rif = TOKEN_RIF(trh);
   1261 			riflen = (ntohs(rif->tr_rcf) &
   1262 			    TOKEN_RCF_LEN_MASK) >> 8;
   1263 
   1264 			if (riflen > 2 &&
   1265 			    riflen < sizeof(struct token_rif) &&
   1266 			    (riflen & 1) == 0) {
   1267 				rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
   1268 				rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
   1269 				memcpy(TOKEN_RIF_LLE(la), rif, riflen);
   1270 			}
   1271 		}
   1272 	}
   1273 #endif /* NTOKEN > 0 */
   1274 
   1275 	KASSERT(sizeof(la->ll_addr) >= ifp->if_addrlen);
   1276 	memcpy(&la->ll_addr, ar_sha(ah), ifp->if_addrlen);
   1277 	la->la_flags |= LLE_VALID;
   1278 	if ((la->la_flags & LLE_STATIC) == 0) {
   1279 		la->la_expire = time_uptime + arpt_keep;
   1280 		arp_settimer(la, arpt_keep);
   1281 	}
   1282 	la->la_asked = 0;
   1283 	/* rt->rt_flags &= ~RTF_REJECT; */
   1284 
   1285 	if (la->la_hold != NULL) {
   1286 		int n = la->la_numheld;
   1287 		struct mbuf *m_hold, *m_hold_next;
   1288 		struct sockaddr_in sin;
   1289 
   1290 		sockaddr_in_init(&sin, &la->r_l3addr.addr4, 0);
   1291 
   1292 		m_hold = la->la_hold;
   1293 		la->la_hold = NULL;
   1294 		la->la_numheld = 0;
   1295 		/*
   1296 		 * We have to unlock here because if_output would call
   1297 		 * arpresolve
   1298 		 */
   1299 		LLE_WUNLOCK(la);
   1300 		ARP_STATADD(ARP_STAT_DFRSENT, n);
   1301 		ARP_STATADD(ARP_STAT_DFRTOTAL, n);
   1302 		for (; m_hold != NULL; m_hold = m_hold_next) {
   1303 			m_hold_next = m_hold->m_nextpkt;
   1304 			m_hold->m_nextpkt = NULL;
   1305 			if_output_lock(ifp, ifp, m_hold, sintosa(&sin), NULL);
   1306 		}
   1307 	} else
   1308 		LLE_WUNLOCK(la);
   1309 	la = NULL;
   1310 
   1311 reply:
   1312 	if (la != NULL) {
   1313 		LLE_WUNLOCK(la);
   1314 		la = NULL;
   1315 	}
   1316 	if (op != ARPOP_REQUEST) {
   1317 		if (op == ARPOP_REPLY)
   1318 			ARP_STATINC(ARP_STAT_RCVREPLY);
   1319 		goto out;
   1320 	}
   1321 	ARP_STATINC(ARP_STAT_RCVREQUEST);
   1322 	if (in_hosteq(itaddr, myaddr)) {
   1323 		/* If our address is unusable, don't reply */
   1324 		if (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED))
   1325 			goto out;
   1326 		/* I am the target */
   1327 		tha = ar_tha(ah);
   1328 		if (tha)
   1329 			memcpy(tha, ar_sha(ah), ah->ar_hln);
   1330 		memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
   1331 	} else {
   1332 		/* Proxy ARP */
   1333 		struct llentry *lle = NULL;
   1334 		struct sockaddr_in sin;
   1335 #if NCARP > 0
   1336 		if (ifp->if_type == IFT_CARP) {
   1337 			struct ifnet *_rcvif = m_get_rcvif(m, &s);
   1338 			int iftype = 0;
   1339 			if (__predict_true(_rcvif != NULL))
   1340 				iftype = _rcvif->if_type;
   1341 			m_put_rcvif(_rcvif, &s);
   1342 			if (iftype != IFT_CARP)
   1343 				goto out;
   1344 		}
   1345 #endif
   1346 
   1347 		tha = ar_tha(ah);
   1348 
   1349 		sockaddr_in_init(&sin, &itaddr, 0);
   1350 
   1351 		IF_AFDATA_RLOCK(ifp);
   1352 		lle = lla_lookup(LLTABLE(ifp), 0, (struct sockaddr *)&sin);
   1353 		IF_AFDATA_RUNLOCK(ifp);
   1354 
   1355 		if ((lle != NULL) && (lle->la_flags & LLE_PUB)) {
   1356 			if (tha)
   1357 				memcpy(tha, ar_sha(ah), ah->ar_hln);
   1358 			memcpy(ar_sha(ah), &lle->ll_addr, ah->ar_hln);
   1359 			LLE_RUNLOCK(lle);
   1360 		} else {
   1361 			if (lle != NULL)
   1362 				LLE_RUNLOCK(lle);
   1363 			goto drop;
   1364 		}
   1365 	}
   1366 	ia4_release(ia, &psref_ia);
   1367 
   1368 	memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
   1369 	memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
   1370 	ah->ar_op = htons(ARPOP_REPLY);
   1371 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
   1372 	switch (ifp->if_type) {
   1373 	case IFT_IEEE1394:
   1374 		/*
   1375 		 * ieee1394 arp reply is broadcast
   1376 		 */
   1377 		m->m_flags &= ~M_MCAST;
   1378 		m->m_flags |= M_BCAST;
   1379 		m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
   1380 		break;
   1381 	default:
   1382 		m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
   1383 		m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
   1384 		break;
   1385 	}
   1386 	m->m_pkthdr.len = m->m_len;
   1387 	sa.sa_family = AF_ARP;
   1388 	sa.sa_len = 2;
   1389 	arps = ARP_STAT_GETREF();
   1390 	arps[ARP_STAT_SNDTOTAL]++;
   1391 	arps[ARP_STAT_SNDREPLY]++;
   1392 	ARP_STAT_PUTREF();
   1393 	if_output_lock(ifp, ifp, m, &sa, NULL);
   1394 	if (rcvif != NULL)
   1395 		m_put_rcvif_psref(rcvif, &psref);
   1396 	return;
   1397 
   1398 out:
   1399 	if (la != NULL)
   1400 		LLE_WUNLOCK(la);
   1401 drop:
   1402 	if (ia != NULL)
   1403 		ia4_release(ia, &psref_ia);
   1404 	if (rcvif != NULL)
   1405 		m_put_rcvif_psref(rcvif, &psref);
   1406 	m_freem(m);
   1407 }
   1408 
   1409 /*
   1410  * Lookup or a new address in arptab.
   1411  */
   1412 static struct llentry *
   1413 arplookup(struct ifnet *ifp, struct mbuf *m, const struct in_addr *addr,
   1414     const struct sockaddr *sa, int wlock)
   1415 {
   1416 	struct sockaddr_in sin;
   1417 	struct llentry *la;
   1418 	int flags = wlock ? LLE_EXCLUSIVE : 0;
   1419 
   1420 
   1421 	if (sa == NULL) {
   1422 		KASSERT(addr != NULL);
   1423 		sockaddr_in_init(&sin, addr, 0);
   1424 		sa = sintocsa(&sin);
   1425 	}
   1426 
   1427 	IF_AFDATA_RLOCK(ifp);
   1428 	la = lla_lookup(LLTABLE(ifp), flags, sa);
   1429 	IF_AFDATA_RUNLOCK(ifp);
   1430 
   1431 	return la;
   1432 }
   1433 
   1434 static struct llentry *
   1435 arpcreate(struct ifnet *ifp, struct mbuf *m, const struct in_addr *addr,
   1436     const struct sockaddr *sa, int wlock)
   1437 {
   1438 	struct sockaddr_in sin;
   1439 	struct llentry *la;
   1440 	int flags = wlock ? LLE_EXCLUSIVE : 0;
   1441 
   1442 	if (sa == NULL) {
   1443 		KASSERT(addr != NULL);
   1444 		sockaddr_in_init(&sin, addr, 0);
   1445 		sa = sintocsa(&sin);
   1446 	}
   1447 
   1448 	la = arplookup(ifp, m, addr, sa, wlock);
   1449 
   1450 	if (la == NULL) {
   1451 		IF_AFDATA_WLOCK(ifp);
   1452 		la = lla_create(LLTABLE(ifp), flags, sa);
   1453 		IF_AFDATA_WUNLOCK(ifp);
   1454 
   1455 		if (la != NULL)
   1456 			arp_init_llentry(ifp, la);
   1457 	}
   1458 
   1459 	return la;
   1460 }
   1461 
   1462 int
   1463 arpioctl(u_long cmd, void *data)
   1464 {
   1465 
   1466 	return EOPNOTSUPP;
   1467 }
   1468 
   1469 void
   1470 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
   1471 {
   1472 	struct in_addr *ip;
   1473 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
   1474 
   1475 	/*
   1476 	 * Warn the user if another station has this IP address,
   1477 	 * but only if the interface IP address is not zero.
   1478 	 */
   1479 	ip = &IA_SIN(ifa)->sin_addr;
   1480 	if (!in_nullhost(*ip) &&
   1481 	    (ia->ia4_flags & (IN_IFF_NOTREADY | IN_IFF_DETACHED)) == 0) {
   1482 		struct llentry *lle;
   1483 
   1484 		/*
   1485 		 * interface address is considered static entry
   1486 		 * because the output of the arp utility shows
   1487 		 * that L2 entry as permanent
   1488 		 */
   1489 		IF_AFDATA_WLOCK(ifp);
   1490 		lle = lla_create(LLTABLE(ifp), (LLE_IFADDR | LLE_STATIC),
   1491 				 (struct sockaddr *)IA_SIN(ifa));
   1492 		IF_AFDATA_WUNLOCK(ifp);
   1493 		if (lle == NULL)
   1494 			log(LOG_INFO, "%s: cannot create arp entry for"
   1495 			    " interface address\n", __func__);
   1496 		else {
   1497 			arp_init_llentry(ifp, lle);
   1498 			LLE_RUNLOCK(lle);
   1499 		}
   1500 	}
   1501 
   1502 	ifa->ifa_rtrequest = arp_rtrequest;
   1503 	ifa->ifa_flags |= RTF_CONNECTED;
   1504 
   1505 	/* ARP will handle DAD for this address. */
   1506 	if (in_nullhost(*ip)) {
   1507 		if (ia->ia_dad_stop != NULL)	/* safety */
   1508 			ia->ia_dad_stop(ifa);
   1509 		ia->ia_dad_start = NULL;
   1510 		ia->ia_dad_stop = NULL;
   1511 		ia->ia4_flags &= ~IN_IFF_TENTATIVE;
   1512 	} else {
   1513 		ia->ia_dad_start = arp_dad_start;
   1514 		ia->ia_dad_stop = arp_dad_stop;
   1515 		if (ia->ia4_flags & IN_IFF_TRYTENTATIVE && ip_dad_count > 0)
   1516 			ia->ia4_flags |= IN_IFF_TENTATIVE;
   1517 		else
   1518 			arpannounce1(ifa);
   1519 	}
   1520 }
   1521 
   1522 TAILQ_HEAD(dadq_head, dadq);
   1523 struct dadq {
   1524 	TAILQ_ENTRY(dadq) dad_list;
   1525 	struct ifaddr *dad_ifa;
   1526 	int dad_count;		/* max ARP to send */
   1527 	int dad_arp_tcount;	/* # of trials to send ARP */
   1528 	int dad_arp_ocount;	/* ARP sent so far */
   1529 	int dad_arp_announce;	/* max ARP announcements */
   1530 	int dad_arp_acount;	/* # of announcements */
   1531 	struct callout dad_timer_ch;
   1532 };
   1533 
   1534 static struct dadq_head dadq;
   1535 static int dad_init = 0;
   1536 static int dad_maxtry = 15;     /* max # of *tries* to transmit DAD packet */
   1537 static kmutex_t arp_dad_lock;
   1538 
   1539 static struct dadq *
   1540 arp_dad_find(struct ifaddr *ifa)
   1541 {
   1542 	struct dadq *dp;
   1543 
   1544 	KASSERT(mutex_owned(&arp_dad_lock));
   1545 
   1546 	TAILQ_FOREACH(dp, &dadq, dad_list) {
   1547 		if (dp->dad_ifa == ifa)
   1548 			return dp;
   1549 	}
   1550 	return NULL;
   1551 }
   1552 
   1553 static void
   1554 arp_dad_starttimer(struct dadq *dp, int ticks)
   1555 {
   1556 
   1557 	callout_reset(&dp->dad_timer_ch, ticks,
   1558 	    (void (*)(void *))arp_dad_timer, (void *)dp->dad_ifa);
   1559 }
   1560 
   1561 static void
   1562 arp_dad_stoptimer(struct dadq *dp)
   1563 {
   1564 
   1565 #ifdef NET_MPSAFE
   1566 	callout_halt(&dp->dad_timer_ch, NULL);
   1567 #else
   1568 	callout_halt(&dp->dad_timer_ch, softnet_lock);
   1569 #endif
   1570 }
   1571 
   1572 static void
   1573 arp_dad_output(struct dadq *dp, struct ifaddr *ifa)
   1574 {
   1575 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
   1576 	struct ifnet *ifp = ifa->ifa_ifp;
   1577 	struct in_addr sip;
   1578 
   1579 	dp->dad_arp_tcount++;
   1580 	if ((ifp->if_flags & IFF_UP) == 0)
   1581 		return;
   1582 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   1583 		return;
   1584 
   1585 	dp->dad_arp_tcount = 0;
   1586 	dp->dad_arp_ocount++;
   1587 
   1588 	memset(&sip, 0, sizeof(sip));
   1589 	arprequest(ifa->ifa_ifp, &sip, &ia->ia_addr.sin_addr,
   1590 	    CLLADDR(ifa->ifa_ifp->if_sadl));
   1591 }
   1592 
   1593 /*
   1594  * Start Duplicate Address Detection (DAD) for specified interface address.
   1595  */
   1596 static void
   1597 arp_dad_start(struct ifaddr *ifa)
   1598 {
   1599 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
   1600 	struct dadq *dp;
   1601 	char ipbuf[INET_ADDRSTRLEN];
   1602 
   1603 	if (!dad_init) {
   1604 		TAILQ_INIT(&dadq);
   1605 		mutex_init(&arp_dad_lock, MUTEX_DEFAULT, IPL_NONE);
   1606 		dad_init++;
   1607 	}
   1608 
   1609 	/*
   1610 	 * If we don't need DAD, don't do it.
   1611 	 * - DAD is disabled (ip_dad_count == 0)
   1612 	 */
   1613 	if (!(ia->ia4_flags & IN_IFF_TENTATIVE)) {
   1614 		log(LOG_DEBUG,
   1615 		    "%s: called with non-tentative address %s(%s)\n", __func__,
   1616 		    in_fmtaddr(ipbuf, ia->ia_addr.sin_addr),
   1617 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1618 		return;
   1619 	}
   1620 	if (!ip_dad_count) {
   1621 		ia->ia4_flags &= ~IN_IFF_TENTATIVE;
   1622 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1623 		arpannounce1(ifa);
   1624 		return;
   1625 	}
   1626 	KASSERT(ifa->ifa_ifp != NULL);
   1627 	if (!(ifa->ifa_ifp->if_flags & IFF_UP))
   1628 		return;
   1629 
   1630 	dp = kmem_intr_alloc(sizeof(*dp), KM_NOSLEEP);
   1631 
   1632 	mutex_enter(&arp_dad_lock);
   1633 	if (arp_dad_find(ifa) != NULL) {
   1634 		mutex_exit(&arp_dad_lock);
   1635 		/* DAD already in progress */
   1636 		if (dp != NULL)
   1637 			kmem_intr_free(dp, sizeof(*dp));
   1638 		return;
   1639 	}
   1640 
   1641 	if (dp == NULL) {
   1642 		mutex_exit(&arp_dad_lock);
   1643 		log(LOG_ERR, "%s: memory allocation failed for %s(%s)\n",
   1644 		    __func__, in_fmtaddr(ipbuf, ia->ia_addr.sin_addr),
   1645 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1646 		return;
   1647 	}
   1648 
   1649 	/*
   1650 	 * Send ARP packet for DAD, ip_dad_count times.
   1651 	 * Note that we must delay the first transmission.
   1652 	 */
   1653 	callout_init(&dp->dad_timer_ch, CALLOUT_MPSAFE);
   1654 	dp->dad_ifa = ifa;
   1655 	ifaref(ifa);	/* just for safety */
   1656 	dp->dad_count = ip_dad_count;
   1657 	dp->dad_arp_announce = 0; /* Will be set when starting to announce */
   1658 	dp->dad_arp_acount = dp->dad_arp_ocount = dp->dad_arp_tcount = 0;
   1659 	TAILQ_INSERT_TAIL(&dadq, (struct dadq *)dp, dad_list);
   1660 
   1661 	ARPLOG(LOG_DEBUG, "%s: starting DAD for %s\n", if_name(ifa->ifa_ifp),
   1662 	    ARPLOGADDR(ia->ia_addr.sin_addr));
   1663 
   1664 	arp_dad_starttimer(dp, cprng_fast32() % (PROBE_WAIT * hz));
   1665 
   1666 	mutex_exit(&arp_dad_lock);
   1667 }
   1668 
   1669 /*
   1670  * terminate DAD unconditionally.  used for address removals.
   1671  */
   1672 static void
   1673 arp_dad_stop(struct ifaddr *ifa)
   1674 {
   1675 	struct dadq *dp;
   1676 
   1677 	if (!dad_init)
   1678 		return;
   1679 
   1680 	mutex_enter(&arp_dad_lock);
   1681 	dp = arp_dad_find(ifa);
   1682 	if (dp == NULL) {
   1683 		mutex_exit(&arp_dad_lock);
   1684 		/* DAD wasn't started yet */
   1685 		return;
   1686 	}
   1687 
   1688 	/* Prevent the timer from running anymore. */
   1689 	TAILQ_REMOVE(&dadq, dp, dad_list);
   1690 	mutex_exit(&arp_dad_lock);
   1691 
   1692 	arp_dad_stoptimer(dp);
   1693 
   1694 	kmem_intr_free(dp, sizeof(*dp));
   1695 	ifafree(ifa);
   1696 }
   1697 
   1698 static void
   1699 arp_dad_timer(struct ifaddr *ifa)
   1700 {
   1701 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
   1702 	struct dadq *dp;
   1703 	char ipbuf[INET_ADDRSTRLEN];
   1704 	bool need_free = false;
   1705 
   1706 #ifndef NET_MPSAFE
   1707 	mutex_enter(softnet_lock);
   1708 	KERNEL_LOCK(1, NULL);
   1709 #endif
   1710 	mutex_enter(&arp_dad_lock);
   1711 
   1712 	/* Sanity check */
   1713 	if (ia == NULL) {
   1714 		log(LOG_ERR, "%s: called with null parameter\n", __func__);
   1715 		goto done;
   1716 	}
   1717 	dp = arp_dad_find(ifa);
   1718 	if (dp == NULL) {
   1719 		/* DAD seems to be stopping, so do nothing. */
   1720 		goto done;
   1721 	}
   1722 	if (ia->ia4_flags & IN_IFF_DUPLICATED) {
   1723 		log(LOG_ERR, "%s: called with duplicate address %s(%s)\n",
   1724 		    __func__, in_fmtaddr(ipbuf, ia->ia_addr.sin_addr),
   1725 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1726 		goto done;
   1727 	}
   1728 	if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0 && dp->dad_arp_acount == 0)
   1729 	{
   1730 		log(LOG_ERR, "%s: called with non-tentative address %s(%s)\n",
   1731 		    __func__, in_fmtaddr(ipbuf, ia->ia_addr.sin_addr),
   1732 		    ifa->ifa_ifp ? if_name(ifa->ifa_ifp) : "???");
   1733 		goto done;
   1734 	}
   1735 
   1736 	/* timeouted with IFF_{RUNNING,UP} check */
   1737 	if (dp->dad_arp_tcount > dad_maxtry) {
   1738 		ARPLOG(LOG_INFO, "%s: could not run DAD, driver problem?\n",
   1739 		    if_name(ifa->ifa_ifp));
   1740 
   1741 		TAILQ_REMOVE(&dadq, dp, dad_list);
   1742 		need_free = true;
   1743 		goto done;
   1744 	}
   1745 
   1746 	/* Need more checks? */
   1747 	if (dp->dad_arp_ocount < dp->dad_count) {
   1748 		int adelay;
   1749 
   1750 		/*
   1751 		 * We have more ARP to go.  Send ARP packet for DAD.
   1752 		 */
   1753 		arp_dad_output(dp, ifa);
   1754 		if (dp->dad_arp_ocount < dp->dad_count)
   1755 			adelay = (PROBE_MIN * hz) +
   1756 			    (cprng_fast32() %
   1757 			    ((PROBE_MAX * hz) - (PROBE_MIN * hz)));
   1758 		else
   1759 			adelay = ANNOUNCE_WAIT * hz;
   1760 		arp_dad_starttimer(dp, adelay);
   1761 		goto done;
   1762 	} else if (dp->dad_arp_acount == 0) {
   1763 		/*
   1764 		 * We are done with DAD.
   1765 		 * No duplicate address found.
   1766 		 */
   1767 		ia->ia4_flags &= ~IN_IFF_TENTATIVE;
   1768 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1769 		ARPLOG(LOG_DEBUG,
   1770 		    "%s: DAD complete for %s - no duplicates found\n",
   1771 		    if_name(ifa->ifa_ifp), ARPLOGADDR(ia->ia_addr.sin_addr));
   1772 		dp->dad_arp_announce = ANNOUNCE_NUM;
   1773 		goto announce;
   1774 	} else if (dp->dad_arp_acount < dp->dad_arp_announce) {
   1775 announce:
   1776 		/*
   1777 		 * Announce the address.
   1778 		 */
   1779 		arpannounce1(ifa);
   1780 		dp->dad_arp_acount++;
   1781 		if (dp->dad_arp_acount < dp->dad_arp_announce) {
   1782 			arp_dad_starttimer(dp, ANNOUNCE_INTERVAL * hz);
   1783 			goto done;
   1784 		}
   1785 		ARPLOG(LOG_DEBUG,
   1786 		    "%s: ARP announcement complete for %s\n",
   1787 		    if_name(ifa->ifa_ifp), ARPLOGADDR(ia->ia_addr.sin_addr));
   1788 	}
   1789 
   1790 	TAILQ_REMOVE(&dadq, dp, dad_list);
   1791 	need_free = true;
   1792 done:
   1793 	mutex_exit(&arp_dad_lock);
   1794 
   1795 	if (need_free) {
   1796 		kmem_intr_free(dp, sizeof(*dp));
   1797 		ifafree(ifa);
   1798 	}
   1799 
   1800 #ifndef NET_MPSAFE
   1801 	KERNEL_UNLOCK_ONE(NULL);
   1802 	mutex_exit(softnet_lock);
   1803 #endif
   1804 }
   1805 
   1806 static void
   1807 arp_dad_duplicated(struct ifaddr *ifa, const char *sha)
   1808 {
   1809 	struct in_ifaddr *ia = (struct in_ifaddr *)ifa;
   1810 	struct ifnet *ifp = ifa->ifa_ifp;
   1811 	char ipbuf[INET_ADDRSTRLEN];
   1812 	const char *iastr;
   1813 
   1814 	iastr = in_fmtaddr(ipbuf, ia->ia_addr.sin_addr);
   1815 
   1816 	if (ia->ia4_flags & (IN_IFF_TENTATIVE|IN_IFF_DUPLICATED)) {
   1817 		log(LOG_ERR,
   1818 		    "%s: DAD duplicate address %s from %s\n",
   1819 		    if_name(ifp), iastr, sha);
   1820 	} else if (ia->ia_dad_defended == 0 ||
   1821 		   ia->ia_dad_defended < time_uptime - DEFEND_INTERVAL) {
   1822 		ia->ia_dad_defended = time_uptime;
   1823 		arpannounce1(ifa);
   1824 		log(LOG_ERR,
   1825 		    "%s: DAD defended address %s from %s\n",
   1826 		    if_name(ifp), iastr, sha);
   1827 		return;
   1828 	} else {
   1829 		/* If DAD is disabled, just report the duplicate. */
   1830 		if (ip_dad_count == 0) {
   1831 			log(LOG_ERR,
   1832 			    "%s: DAD ignoring duplicate address %s from %s\n",
   1833 			    if_name(ifp), iastr, sha);
   1834 			return;
   1835 		}
   1836 		log(LOG_ERR,
   1837 		    "%s: DAD defence failed for %s from %s\n",
   1838 		    if_name(ifp), iastr, sha);
   1839 	}
   1840 
   1841 	arp_dad_stop(ifa);
   1842 
   1843 	ia->ia4_flags &= ~IN_IFF_TENTATIVE;
   1844 	if ((ia->ia4_flags & IN_IFF_DUPLICATED) == 0) {
   1845 		ia->ia4_flags |= IN_IFF_DUPLICATED;
   1846 		/* Inform the routing socket of the duplicate address */
   1847 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1848 	}
   1849 }
   1850 
   1851 /*
   1852  * Called from 10 Mb/s Ethernet interrupt handlers
   1853  * when ether packet type ETHERTYPE_REVARP
   1854  * is received.  Common length and type checks are done here,
   1855  * then the protocol-specific routine is called.
   1856  */
   1857 void
   1858 revarpinput(struct mbuf *m)
   1859 {
   1860 	struct arphdr *ar;
   1861 
   1862 	if (m->m_len < sizeof(struct arphdr))
   1863 		goto out;
   1864 	ar = mtod(m, struct arphdr *);
   1865 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
   1866 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
   1867 		goto out;
   1868 #endif
   1869 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
   1870 		goto out;
   1871 	switch (ntohs(ar->ar_pro)) {
   1872 	case ETHERTYPE_IP:
   1873 	case ETHERTYPE_IPTRAILERS:
   1874 		in_revarpinput(m);
   1875 		return;
   1876 
   1877 	default:
   1878 		break;
   1879 	}
   1880 out:
   1881 	m_freem(m);
   1882 }
   1883 
   1884 /*
   1885  * RARP for Internet protocols on 10 Mb/s Ethernet.
   1886  * Algorithm is that given in RFC 903.
   1887  * We are only using for bootstrap purposes to get an ip address for one of
   1888  * our interfaces.  Thus we support no user-interface.
   1889  *
   1890  * Since the contents of the RARP reply are specific to the interface that
   1891  * sent the request, this code must ensure that they are properly associated.
   1892  *
   1893  * Note: also supports ARP via RARP packets, per the RFC.
   1894  */
   1895 void
   1896 in_revarpinput(struct mbuf *m)
   1897 {
   1898 	struct arphdr *ah;
   1899 	void *tha;
   1900 	int op;
   1901 	struct ifnet *rcvif;
   1902 	int s;
   1903 
   1904 	ah = mtod(m, struct arphdr *);
   1905 	op = ntohs(ah->ar_op);
   1906 
   1907 	rcvif = m_get_rcvif(m, &s);
   1908 	if (__predict_false(rcvif == NULL))
   1909 		goto out;
   1910 	switch (rcvif->if_type) {
   1911 	case IFT_IEEE1394:
   1912 		/* ARP without target hardware address is not supported */
   1913 		goto out;
   1914 	default:
   1915 		break;
   1916 	}
   1917 
   1918 	switch (op) {
   1919 	case ARPOP_REQUEST:
   1920 	case ARPOP_REPLY:	/* per RFC */
   1921 		m_put_rcvif(rcvif, &s);
   1922 		in_arpinput(m);
   1923 		return;
   1924 	case ARPOP_REVREPLY:
   1925 		break;
   1926 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
   1927 	default:
   1928 		goto out;
   1929 	}
   1930 	if (!revarp_in_progress)
   1931 		goto out;
   1932 	if (rcvif != myip_ifp) /* !same interface */
   1933 		goto out;
   1934 	if (myip_initialized)
   1935 		goto wake;
   1936 	tha = ar_tha(ah);
   1937 	if (tha == NULL)
   1938 		goto out;
   1939 	if (ah->ar_pln != sizeof(struct in_addr))
   1940 		goto out;
   1941 	if (ah->ar_hln != rcvif->if_sadl->sdl_alen)
   1942 		goto out;
   1943 	if (memcmp(tha, CLLADDR(rcvif->if_sadl), rcvif->if_sadl->sdl_alen))
   1944 		goto out;
   1945 	memcpy(&srv_ip, ar_spa(ah), sizeof(srv_ip));
   1946 	memcpy(&myip, ar_tpa(ah), sizeof(myip));
   1947 	myip_initialized = 1;
   1948 wake:	/* Do wakeup every time in case it was missed. */
   1949 	wakeup((void *)&myip);
   1950 
   1951 out:
   1952 	m_put_rcvif(rcvif, &s);
   1953 	m_freem(m);
   1954 }
   1955 
   1956 /*
   1957  * Send a RARP request for the ip address of the specified interface.
   1958  * The request should be RFC 903-compliant.
   1959  */
   1960 static void
   1961 revarprequest(struct ifnet *ifp)
   1962 {
   1963 	struct sockaddr sa;
   1964 	struct mbuf *m;
   1965 	struct arphdr *ah;
   1966 	void *tha;
   1967 
   1968 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
   1969 		return;
   1970 	MCLAIM(m, &arpdomain.dom_mowner);
   1971 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
   1972 	    2*ifp->if_addrlen;
   1973 	m->m_pkthdr.len = m->m_len;
   1974 	MH_ALIGN(m, m->m_len);
   1975 	ah = mtod(m, struct arphdr *);
   1976 	memset(ah, 0, m->m_len);
   1977 	ah->ar_pro = htons(ETHERTYPE_IP);
   1978 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
   1979 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
   1980 	ah->ar_op = htons(ARPOP_REVREQUEST);
   1981 
   1982 	memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
   1983 	tha = ar_tha(ah);
   1984 	if (tha == NULL) {
   1985 		m_free(m);
   1986 		return;
   1987 	}
   1988 	memcpy(tha, CLLADDR(ifp->if_sadl), ah->ar_hln);
   1989 
   1990 	sa.sa_family = AF_ARP;
   1991 	sa.sa_len = 2;
   1992 	m->m_flags |= M_BCAST;
   1993 
   1994 	if_output_lock(ifp, ifp, m, &sa, NULL);
   1995 }
   1996 
   1997 /*
   1998  * RARP for the ip address of the specified interface, but also
   1999  * save the ip address of the server that sent the answer.
   2000  * Timeout if no response is received.
   2001  */
   2002 int
   2003 revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in,
   2004     struct in_addr *clnt_in)
   2005 {
   2006 	int result, count = 20;
   2007 
   2008 	myip_initialized = 0;
   2009 	myip_ifp = ifp;
   2010 
   2011 	revarp_in_progress = 1;
   2012 	while (count--) {
   2013 		revarprequest(ifp);
   2014 		result = tsleep((void *)&myip, PSOCK, "revarp", hz/2);
   2015 		if (result != EWOULDBLOCK)
   2016 			break;
   2017 	}
   2018 	revarp_in_progress = 0;
   2019 
   2020 	if (!myip_initialized)
   2021 		return ENETUNREACH;
   2022 
   2023 	memcpy(serv_in, &srv_ip, sizeof(*serv_in));
   2024 	memcpy(clnt_in, &myip, sizeof(*clnt_in));
   2025 	return 0;
   2026 }
   2027 
   2028 void
   2029 arp_stat_add(int type, uint64_t count)
   2030 {
   2031 	ARP_STATADD(type, count);
   2032 }
   2033 
   2034 static int
   2035 sysctl_net_inet_arp_stats(SYSCTLFN_ARGS)
   2036 {
   2037 
   2038 	return NETSTAT_SYSCTL(arpstat_percpu, ARP_NSTATS);
   2039 }
   2040 
   2041 static void
   2042 sysctl_net_inet_arp_setup(struct sysctllog **clog)
   2043 {
   2044 	const struct sysctlnode *node;
   2045 
   2046 	sysctl_createv(clog, 0, NULL, NULL,
   2047 			CTLFLAG_PERMANENT,
   2048 			CTLTYPE_NODE, "inet", NULL,
   2049 			NULL, 0, NULL, 0,
   2050 			CTL_NET, PF_INET, CTL_EOL);
   2051 	sysctl_createv(clog, 0, NULL, &node,
   2052 			CTLFLAG_PERMANENT,
   2053 			CTLTYPE_NODE, "arp",
   2054 			SYSCTL_DESCR("Address Resolution Protocol"),
   2055 			NULL, 0, NULL, 0,
   2056 			CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
   2057 
   2058 	sysctl_createv(clog, 0, NULL, NULL,
   2059 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2060 			CTLTYPE_INT, "keep",
   2061 			SYSCTL_DESCR("Valid ARP entry lifetime in seconds"),
   2062 			NULL, 0, &arpt_keep, 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, "down",
   2068 			SYSCTL_DESCR("Failed ARP entry lifetime in seconds"),
   2069 			NULL, 0, &arpt_down, 0,
   2070 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2071 
   2072 	sysctl_createv(clog, 0, NULL, NULL,
   2073 			CTLFLAG_PERMANENT,
   2074 			CTLTYPE_STRUCT, "stats",
   2075 			SYSCTL_DESCR("ARP statistics"),
   2076 			sysctl_net_inet_arp_stats, 0, NULL, 0,
   2077 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2078 
   2079 	sysctl_createv(clog, 0, NULL, NULL,
   2080 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2081 			CTLTYPE_INT, "log_movements",
   2082 			SYSCTL_DESCR("log ARP replies from MACs different than"
   2083 			    " the one in the cache"),
   2084 			NULL, 0, &log_movements, 0,
   2085 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2086 
   2087 	sysctl_createv(clog, 0, NULL, NULL,
   2088 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2089 			CTLTYPE_INT, "log_permanent_modify",
   2090 			SYSCTL_DESCR("log ARP replies from MACs different than"
   2091 			    " the one in the permanent arp entry"),
   2092 			NULL, 0, &log_permanent_modify, 0,
   2093 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2094 
   2095 	sysctl_createv(clog, 0, NULL, NULL,
   2096 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2097 			CTLTYPE_INT, "log_wrong_iface",
   2098 			SYSCTL_DESCR("log ARP packets arriving on the wrong"
   2099 			    " interface"),
   2100 			NULL, 0, &log_wrong_iface, 0,
   2101 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2102 
   2103 	sysctl_createv(clog, 0, NULL, NULL,
   2104 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2105 			CTLTYPE_INT, "log_unknown_network",
   2106 			SYSCTL_DESCR("log ARP packets from non-local network"),
   2107 			NULL, 0, &log_unknown_network, 0,
   2108 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2109 
   2110 	sysctl_createv(clog, 0, NULL, NULL,
   2111 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2112 		       CTLTYPE_INT, "debug",
   2113 		       SYSCTL_DESCR("Enable ARP DAD debug output"),
   2114 		       NULL, 0, &arp_debug, 0,
   2115 		       CTL_NET, PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   2116 }
   2117 
   2118 #endif /* INET */
   2119