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