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