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