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