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