Home | History | Annotate | Line # | Download | only in netinet
if_arp.c revision 1.161
      1 /*	$NetBSD: if_arp.c,v 1.161 2015/02/26 09:54:46 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.161 2015/02/26 09:54:46 roy Exp $");
     72 
     73 #include "opt_ddb.h"
     74 #include "opt_inet.h"
     75 
     76 #ifdef INET
     77 
     78 #include "bridge.h"
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/callout.h>
     83 #include <sys/malloc.h>
     84 #include <sys/mbuf.h>
     85 #include <sys/socket.h>
     86 #include <sys/time.h>
     87 #include <sys/timetc.h>
     88 #include <sys/kernel.h>
     89 #include <sys/errno.h>
     90 #include <sys/ioctl.h>
     91 #include <sys/syslog.h>
     92 #include <sys/proc.h>
     93 #include <sys/protosw.h>
     94 #include <sys/domain.h>
     95 #include <sys/sysctl.h>
     96 #include <sys/socketvar.h>
     97 #include <sys/percpu.h>
     98 
     99 #include <net/ethertypes.h>
    100 #include <net/if.h>
    101 #include <net/if_dl.h>
    102 #include <net/if_token.h>
    103 #include <net/if_types.h>
    104 #include <net/if_ether.h>
    105 #include <net/route.h>
    106 #include <net/net_stats.h>
    107 
    108 #include <netinet/in.h>
    109 #include <netinet/in_systm.h>
    110 #include <netinet/in_var.h>
    111 #include <netinet/ip.h>
    112 #include <netinet/if_inarp.h>
    113 
    114 #include "arcnet.h"
    115 #if NARCNET > 0
    116 #include <net/if_arc.h>
    117 #endif
    118 #include "fddi.h"
    119 #if NFDDI > 0
    120 #include <net/if_fddi.h>
    121 #endif
    122 #include "token.h"
    123 #include "carp.h"
    124 #if NCARP > 0
    125 #include <netinet/ip_carp.h>
    126 #endif
    127 
    128 #define SIN(s) ((struct sockaddr_in *)s)
    129 #define SRP(s) ((struct sockaddr_inarp *)s)
    130 
    131 /*
    132  * ARP trailer negotiation.  Trailer protocol is not IP specific,
    133  * but ARP request/response use IP addresses.
    134  */
    135 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
    136 
    137 /* timer values */
    138 int	arpt_prune = (5*60*1);	/* walk list every 5 minutes */
    139 int	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
    140 int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
    141 int	arpt_refresh = (5*60);	/* time left before refreshing */
    142 #define	rt_expire rt_rmx.rmx_expire
    143 #define	rt_pksent rt_rmx.rmx_pksent
    144 
    145 static	struct sockaddr *arp_setgate(struct rtentry *, struct sockaddr *,
    146 	    const struct sockaddr *);
    147 static	void arptfree(struct llinfo_arp *);
    148 static	void arptimer(void *);
    149 static	struct llinfo_arp *arplookup1(struct mbuf *, const struct in_addr *,
    150 				      int, int, struct rtentry *);
    151 static	struct llinfo_arp *arplookup(struct mbuf *, const struct in_addr *,
    152 					  int, int);
    153 static	void in_arpinput(struct mbuf *);
    154 static	void arp_drainstub(void);
    155 
    156 LIST_HEAD(llinfo_arpq, llinfo_arp) llinfo_arp;
    157 struct	ifqueue arpintrq = {
    158 	.ifq_head = NULL,
    159 	.ifq_tail = NULL,
    160 	.ifq_len = 0,
    161 	.ifq_maxlen = 50,
    162 	.ifq_drops = 0,
    163 };
    164 int	arp_inuse, arp_allocated;
    165 int	arp_maxtries = 5;
    166 int	useloopback = 1;	/* use loopback interface for local traffic */
    167 int	arpinit_done = 0;
    168 
    169 static percpu_t *arpstat_percpu;
    170 
    171 #define	ARP_STAT_GETREF()	_NET_STAT_GETREF(arpstat_percpu)
    172 #define	ARP_STAT_PUTREF()	_NET_STAT_PUTREF(arpstat_percpu)
    173 
    174 #define	ARP_STATINC(x)		_NET_STATINC(arpstat_percpu, x)
    175 #define	ARP_STATADD(x, v)	_NET_STATADD(arpstat_percpu, x, v)
    176 
    177 struct	callout arptimer_ch;
    178 
    179 /* revarp state */
    180 struct	in_addr myip, srv_ip;
    181 int	myip_initialized = 0;
    182 int	revarp_in_progress = 0;
    183 struct	ifnet *myip_ifp = NULL;
    184 
    185 #ifdef DDB
    186 static void db_print_sa(const struct sockaddr *);
    187 static void db_print_ifa(struct ifaddr *);
    188 static void db_print_llinfo(void *);
    189 static int db_show_rtentry(struct rtentry *, void *);
    190 #endif
    191 
    192 static int arp_drainwanted;
    193 
    194 static int log_movements = 1;
    195 static int log_permanent_modify = 1;
    196 static int log_wrong_iface = 1;
    197 static int log_unknown_network = 1;
    198 
    199 /*
    200  * this should be elsewhere.
    201  */
    202 
    203 static char *
    204 lla_snprintf(u_int8_t *, int);
    205 
    206 static char *
    207 lla_snprintf(u_int8_t *adrp, int len)
    208 {
    209 #define NUMBUFS 3
    210 	static char buf[NUMBUFS][16*3];
    211 	static int bnum = 0;
    212 
    213 	int i;
    214 	char *p;
    215 
    216 	p = buf[bnum];
    217 
    218 	*p++ = hexdigits[(*adrp)>>4];
    219 	*p++ = hexdigits[(*adrp++)&0xf];
    220 
    221 	for (i=1; i<len && i<16; i++) {
    222 		*p++ = ':';
    223 		*p++ = hexdigits[(*adrp)>>4];
    224 		*p++ = hexdigits[(*adrp++)&0xf];
    225 	}
    226 
    227 	*p = 0;
    228 	p = buf[bnum];
    229 	bnum = (bnum + 1) % NUMBUFS;
    230 	return p;
    231 }
    232 
    233 DOMAIN_DEFINE(arpdomain);	/* forward declare and add to link set */
    234 
    235 static void
    236 arp_fasttimo(void)
    237 {
    238 	if (arp_drainwanted) {
    239 		arp_drain();
    240 		arp_drainwanted = 0;
    241 	}
    242 }
    243 
    244 const struct protosw arpsw[] = {
    245 	{ .pr_type = 0,
    246 	  .pr_domain = &arpdomain,
    247 	  .pr_protocol = 0,
    248 	  .pr_flags = 0,
    249 	  .pr_input = 0,
    250 	  .pr_output = 0,
    251 	  .pr_ctlinput = 0,
    252 	  .pr_ctloutput = 0,
    253 	  .pr_usrreqs = 0,
    254 	  .pr_init = arp_init,
    255 	  .pr_fasttimo = arp_fasttimo,
    256 	  .pr_slowtimo = 0,
    257 	  .pr_drain = arp_drainstub,
    258 	}
    259 };
    260 
    261 struct domain arpdomain = {
    262 	.dom_family = PF_ARP,
    263 	.dom_name = "arp",
    264 	.dom_protosw = arpsw,
    265 	.dom_protoswNPROTOSW = &arpsw[__arraycount(arpsw)],
    266 };
    267 
    268 /*
    269  * ARP table locking.
    270  *
    271  * to prevent lossage vs. the arp_drain routine (which may be called at
    272  * any time, including in a device driver context), we do two things:
    273  *
    274  * 1) manipulation of la->la_hold is done at splnet() (for all of
    275  * about two instructions).
    276  *
    277  * 2) manipulation of the arp table's linked list is done under the
    278  * protection of the ARP_LOCK; if arp_drain() or arptimer is called
    279  * while the arp table is locked, we punt and try again later.
    280  */
    281 
    282 static int	arp_locked;
    283 static inline int arp_lock_try(int);
    284 static inline void arp_unlock(void);
    285 
    286 static inline int
    287 arp_lock_try(int recurse)
    288 {
    289 	int s;
    290 
    291 	/*
    292 	 * Use splvm() -- we're blocking things that would cause
    293 	 * mbuf allocation.
    294 	 */
    295 	s = splvm();
    296 	if (!recurse && arp_locked) {
    297 		splx(s);
    298 		return 0;
    299 	}
    300 	arp_locked++;
    301 	splx(s);
    302 	return 1;
    303 }
    304 
    305 static inline void
    306 arp_unlock(void)
    307 {
    308 	int s;
    309 
    310 	s = splvm();
    311 	arp_locked--;
    312 	splx(s);
    313 }
    314 
    315 #ifdef DIAGNOSTIC
    316 #define	ARP_LOCK(recurse)						\
    317 do {									\
    318 	if (arp_lock_try(recurse) == 0) {				\
    319 		printf("%s:%d: arp already locked\n", __FILE__, __LINE__); \
    320 		panic("arp_lock");					\
    321 	}								\
    322 } while (/*CONSTCOND*/ 0)
    323 #define	ARP_LOCK_CHECK()						\
    324 do {									\
    325 	if (arp_locked == 0) {						\
    326 		printf("%s:%d: arp lock not held\n", __FILE__, __LINE__); \
    327 		panic("arp lock check");				\
    328 	}								\
    329 } while (/*CONSTCOND*/ 0)
    330 #else
    331 #define	ARP_LOCK(x)		(void) arp_lock_try(x)
    332 #define	ARP_LOCK_CHECK()	/* nothing */
    333 #endif
    334 
    335 #define	ARP_UNLOCK()		arp_unlock()
    336 
    337 static void sysctl_net_inet_arp_setup(struct sysctllog **);
    338 
    339 void
    340 arp_init(void)
    341 {
    342 
    343 	sysctl_net_inet_arp_setup(NULL);
    344 	arpstat_percpu = percpu_alloc(sizeof(uint64_t) * ARP_NSTATS);
    345 }
    346 
    347 static void
    348 arp_drainstub(void)
    349 {
    350 	arp_drainwanted = 1;
    351 }
    352 
    353 /*
    354  * ARP protocol drain routine.  Called when memory is in short supply.
    355  * Called at splvm();  don't acquire softnet_lock as can be called from
    356  * hardware interrupt handlers.
    357  */
    358 void
    359 arp_drain(void)
    360 {
    361 	struct llinfo_arp *la, *nla;
    362 	int count = 0;
    363 	struct mbuf *mold;
    364 
    365 	KERNEL_LOCK(1, NULL);
    366 
    367 	if (arp_lock_try(0) == 0) {
    368 		KERNEL_UNLOCK_ONE(NULL);
    369 		return;
    370 	}
    371 
    372 	for (la = LIST_FIRST(&llinfo_arp); la != NULL; la = nla) {
    373 		nla = LIST_NEXT(la, la_list);
    374 
    375 		mold = la->la_hold;
    376 		la->la_hold = 0;
    377 
    378 		if (mold) {
    379 			m_freem(mold);
    380 			count++;
    381 		}
    382 	}
    383 	ARP_UNLOCK();
    384 	ARP_STATADD(ARP_STAT_DFRDROPPED, count);
    385 	KERNEL_UNLOCK_ONE(NULL);
    386 }
    387 
    388 
    389 /*
    390  * Timeout routine.  Age arp_tab entries periodically.
    391  */
    392 /* ARGSUSED */
    393 static void
    394 arptimer(void *arg)
    395 {
    396 	struct llinfo_arp *la, *nla;
    397 
    398 	mutex_enter(softnet_lock);
    399 	KERNEL_LOCK(1, NULL);
    400 
    401 	if (arp_lock_try(0) == 0) {
    402 		/* get it later.. */
    403 		KERNEL_UNLOCK_ONE(NULL);
    404 		mutex_exit(softnet_lock);
    405 		return;
    406 	}
    407 
    408 	callout_reset(&arptimer_ch, arpt_prune * hz, arptimer, NULL);
    409 	for (la = LIST_FIRST(&llinfo_arp); la != NULL; la = nla) {
    410 		struct rtentry *rt = la->la_rt;
    411 
    412 		nla = LIST_NEXT(la, la_list);
    413 		if (rt->rt_expire == 0)
    414 			continue;
    415 		if ((rt->rt_expire - time_second) < arpt_refresh &&
    416 		    rt->rt_pksent > (time_second - arpt_keep)) {
    417 			/*
    418 			 * If the entry has been used during since last
    419 			 * refresh, try to renew it before deleting.
    420 			 */
    421 			arprequest(rt->rt_ifp,
    422 			    &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
    423 			    &satocsin(rt_getkey(rt))->sin_addr,
    424 			    CLLADDR(rt->rt_ifp->if_sadl));
    425 		} else if (rt->rt_expire <= time_second)
    426 			arptfree(la); /* timer has expired; clear */
    427 	}
    428 
    429 	ARP_UNLOCK();
    430 
    431 	KERNEL_UNLOCK_ONE(NULL);
    432 	mutex_exit(softnet_lock);
    433 }
    434 
    435 /*
    436  * We set the gateway for RTF_CLONING routes to a "prototype"
    437  * link-layer sockaddr whose interface type (if_type) and interface
    438  * index (if_index) fields are prepared.
    439  */
    440 static struct sockaddr *
    441 arp_setgate(struct rtentry *rt, struct sockaddr *gate,
    442     const struct sockaddr *netmask)
    443 {
    444 	const struct ifnet *ifp = rt->rt_ifp;
    445 	uint8_t namelen = strlen(ifp->if_xname);
    446 	uint8_t addrlen = ifp->if_addrlen;
    447 
    448 	/*
    449 	 * XXX: If this is a manually added route to interface
    450 	 * such as older version of routed or gated might provide,
    451 	 * restore cloning bit.
    452 	 */
    453 	if ((rt->rt_flags & RTF_HOST) == 0 && netmask != NULL &&
    454 	    satocsin(netmask)->sin_addr.s_addr != 0xffffffff)
    455 		rt->rt_flags |= RTF_CLONING;
    456 	if (rt->rt_flags & RTF_CLONING ||
    457 	    ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && !rt->rt_llinfo))
    458 	{
    459 		union {
    460 			struct sockaddr sa;
    461 			struct sockaddr_storage ss;
    462 			struct sockaddr_dl sdl;
    463 		} u;
    464 		/*
    465 		 * Case 1: This route should come from a route to iface.
    466 		 */
    467 		sockaddr_dl_init(&u.sdl, sizeof(u.ss),
    468 		    ifp->if_index, ifp->if_type, NULL, namelen, NULL, addrlen);
    469 		rt_setgate(rt, &u.sa);
    470 		gate = rt->rt_gateway;
    471 	}
    472 	return gate;
    473 }
    474 
    475 /*
    476  * Parallel to llc_rtrequest.
    477  */
    478 void
    479 arp_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
    480 {
    481 	struct sockaddr *gate = rt->rt_gateway;
    482 	struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
    483 	size_t allocsize;
    484 	struct mbuf *mold;
    485 	int s;
    486 	struct in_ifaddr *ia;
    487 	struct ifaddr *ifa;
    488 	struct ifnet *ifp = rt->rt_ifp;
    489 
    490 	if (!arpinit_done) {
    491 		arpinit_done = 1;
    492 		/*
    493 		 * We generate expiration times from time_second
    494 		 * so avoid accidentally creating permanent routes.
    495 		 */
    496 		if (time_second == 0) {
    497 			struct timespec ts;
    498 			ts.tv_sec = 1;
    499 			ts.tv_nsec = 0;
    500 			tc_setclock(&ts);
    501 		}
    502 		callout_init(&arptimer_ch, CALLOUT_MPSAFE);
    503 		callout_reset(&arptimer_ch, hz, arptimer, NULL);
    504 	}
    505 
    506 	if (req == RTM_LLINFO_UPD) {
    507 		struct in_addr *in;
    508 
    509 		if ((ifa = info->rti_ifa) == NULL)
    510 			return;
    511 
    512 		in = &ifatoia(ifa)->ia_addr.sin_addr;
    513 
    514 		arprequest(ifa->ifa_ifp, in, in,
    515 		    CLLADDR(ifa->ifa_ifp->if_sadl));
    516 		return;
    517 	}
    518 
    519 	if ((rt->rt_flags & RTF_GATEWAY) != 0) {
    520 		if (req != RTM_ADD)
    521 			return;
    522 
    523 		/*
    524 		 * linklayers with particular link MTU limitation.
    525 		 */
    526 		switch(ifp->if_type) {
    527 #if NFDDI > 0
    528 		case IFT_FDDI:
    529 			if (ifp->if_mtu > FDDIIPMTU)
    530 				rt->rt_rmx.rmx_mtu = FDDIIPMTU;
    531 			break;
    532 #endif
    533 #if NARC > 0
    534 		case IFT_ARCNET:
    535 		    {
    536 			int arcipifmtu;
    537 
    538 			if (ifp->if_flags & IFF_LINK0)
    539 				arcipifmtu = arc_ipmtu;
    540 			else
    541 				arcipifmtu = ARCMTU;
    542 			if (ifp->if_mtu > arcipifmtu)
    543 				rt->rt_rmx.rmx_mtu = arcipifmtu;
    544 			break;
    545 		    }
    546 #endif
    547 		}
    548 		return;
    549 	}
    550 
    551 	ARP_LOCK(1);		/* we may already be locked here. */
    552 
    553 	switch (req) {
    554 	case RTM_SETGATE:
    555 		gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
    556 		break;
    557 	case RTM_ADD:
    558 		gate = arp_setgate(rt, gate, info->rti_info[RTAX_NETMASK]);
    559 		if (rt->rt_flags & RTF_CLONING ||
    560 		    ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && !la))
    561 		{
    562 			/*
    563 			 * Give this route an expiration time, even though
    564 			 * it's a "permanent" route, so that routes cloned
    565 			 * from it do not need their expiration time set.
    566 			 */
    567 			rt->rt_expire = time_second;
    568 			/*
    569 			 * linklayers with particular link MTU limitation.
    570 			 */
    571 			switch (ifp->if_type) {
    572 #if NFDDI > 0
    573 			case IFT_FDDI:
    574 				if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
    575 				    (rt->rt_rmx.rmx_mtu > FDDIIPMTU ||
    576 				     (rt->rt_rmx.rmx_mtu == 0 &&
    577 				      ifp->if_mtu > FDDIIPMTU)))
    578 					rt->rt_rmx.rmx_mtu = FDDIIPMTU;
    579 				break;
    580 #endif
    581 #if NARC > 0
    582 			case IFT_ARCNET:
    583 			    {
    584 				int arcipifmtu;
    585 				if (ifp->if_flags & IFF_LINK0)
    586 					arcipifmtu = arc_ipmtu;
    587 				else
    588 					arcipifmtu = ARCMTU;
    589 
    590 				if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0 &&
    591 				    (rt->rt_rmx.rmx_mtu > arcipifmtu ||
    592 				     (rt->rt_rmx.rmx_mtu == 0 &&
    593 				      ifp->if_mtu > arcipifmtu)))
    594 					rt->rt_rmx.rmx_mtu = arcipifmtu;
    595 				break;
    596 			    }
    597 #endif
    598 			}
    599 			if (rt->rt_flags & RTF_CLONING)
    600 				break;
    601 		}
    602 		/* Announce a new entry if requested. */
    603 		if (rt->rt_flags & RTF_ANNOUNCE) {
    604 			arprequest(ifp,
    605 			    &satocsin(rt_getkey(rt))->sin_addr,
    606 			    &satocsin(rt_getkey(rt))->sin_addr,
    607 			    CLLADDR(satocsdl(gate)));
    608 		}
    609 		/*FALLTHROUGH*/
    610 	case RTM_RESOLVE:
    611 		if (gate->sa_family != AF_LINK ||
    612 		    gate->sa_len < sockaddr_dl_measure(0, ifp->if_addrlen)) {
    613 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
    614 			break;
    615 		}
    616 		satosdl(gate)->sdl_type = ifp->if_type;
    617 		satosdl(gate)->sdl_index = ifp->if_index;
    618 		if (la != NULL)
    619 			break; /* This happens on a route change */
    620 		/*
    621 		 * Case 2:  This route may come from cloning, or a manual route
    622 		 * add with a LL address.
    623 		 */
    624 		switch (ifp->if_type) {
    625 #if NTOKEN > 0
    626 		case IFT_ISO88025:
    627 			allocsize = sizeof(*la) + sizeof(struct token_rif);
    628 			break;
    629 #endif /* NTOKEN > 0 */
    630 		default:
    631 			allocsize = sizeof(*la);
    632 		}
    633 		R_Malloc(la, struct llinfo_arp *, allocsize);
    634 		rt->rt_llinfo = (void *)la;
    635 		if (la == NULL) {
    636 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
    637 			break;
    638 		}
    639 		arp_inuse++, arp_allocated++;
    640 		memset(la, 0, allocsize);
    641 		la->la_rt = rt;
    642 		rt->rt_flags |= RTF_LLINFO;
    643 		LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
    644 
    645 		INADDR_TO_IA(satocsin(rt_getkey(rt))->sin_addr, ia);
    646 		while (ia && ia->ia_ifp != ifp)
    647 			NEXT_IA_WITH_SAME_ADDR(ia);
    648 		if (ia) {
    649 			/*
    650 			 * This test used to be
    651 			 *	if (lo0ifp->if_flags & IFF_UP)
    652 			 * It allowed local traffic to be forced through
    653 			 * the hardware by configuring the loopback down.
    654 			 * However, it causes problems during network
    655 			 * configuration for boards that can't receive
    656 			 * packets they send.  It is now necessary to clear
    657 			 * "useloopback" and remove the route to force
    658 			 * traffic out to the hardware.
    659 			 *
    660 			 * In 4.4BSD, the above "if" statement checked
    661 			 * rt->rt_ifa against rt_getkey(rt).  It was changed
    662 			 * to the current form so that we can provide a
    663 			 * better support for multiple IPv4 addresses on a
    664 			 * interface.
    665 			 */
    666 			rt->rt_expire = 0;
    667 			if (sockaddr_dl_init(satosdl(gate), gate->sa_len,
    668 			    ifp->if_index, ifp->if_type, NULL, 0,
    669 			    CLLADDR(ifp->if_sadl), ifp->if_addrlen) == NULL) {
    670 				panic("%s(%s): sockaddr_dl_init cannot fail",
    671 				    __func__, ifp->if_xname);
    672 			}
    673 			if (useloopback)
    674 				ifp = rt->rt_ifp = lo0ifp;
    675 			/*
    676 			 * make sure to set rt->rt_ifa to the interface
    677 			 * address we are using, otherwise we will have trouble
    678 			 * with source address selection.
    679 			 */
    680 			ifa = &ia->ia_ifa;
    681 			if (ifa != rt->rt_ifa)
    682 				rt_replace_ifa(rt, ifa);
    683 		}
    684 		break;
    685 
    686 	case RTM_DELETE:
    687 		if (la == NULL)
    688 			break;
    689 		arp_inuse--;
    690 		LIST_REMOVE(la, la_list);
    691 		rt->rt_llinfo = NULL;
    692 		rt->rt_flags &= ~RTF_LLINFO;
    693 
    694 		s = splnet();
    695 		mold = la->la_hold;
    696 		la->la_hold = 0;
    697 		splx(s);
    698 
    699 		if (mold)
    700 			m_freem(mold);
    701 
    702 		Free((void *)la);
    703 	}
    704 	ARP_UNLOCK();
    705 }
    706 
    707 /*
    708  * Broadcast an ARP request. Caller specifies:
    709  *	- arp header source ip address
    710  *	- arp header target ip address
    711  *	- arp header source ethernet address
    712  */
    713 void
    714 arprequest(struct ifnet *ifp,
    715     const struct in_addr *sip, const struct in_addr *tip,
    716     const u_int8_t *enaddr)
    717 {
    718 	struct mbuf *m;
    719 	struct arphdr *ah;
    720 	struct sockaddr sa;
    721 	uint64_t *arps;
    722 
    723 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    724 		return;
    725 	MCLAIM(m, &arpdomain.dom_mowner);
    726 	switch (ifp->if_type) {
    727 	case IFT_IEEE1394:
    728 		m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
    729 		    ifp->if_addrlen;
    730 		break;
    731 	default:
    732 		m->m_len = sizeof(*ah) + 2 * sizeof(struct in_addr) +
    733 		    2 * ifp->if_addrlen;
    734 		break;
    735 	}
    736 	m->m_pkthdr.len = m->m_len;
    737 	MH_ALIGN(m, m->m_len);
    738 	ah = mtod(m, struct arphdr *);
    739 	memset(ah, 0, m->m_len);
    740 	switch (ifp->if_type) {
    741 	case IFT_IEEE1394:	/* RFC2734 */
    742 		/* fill it now for ar_tpa computation */
    743 		ah->ar_hrd = htons(ARPHRD_IEEE1394);
    744 		break;
    745 	default:
    746 		/* ifp->if_output will fill ar_hrd */
    747 		break;
    748 	}
    749 	ah->ar_pro = htons(ETHERTYPE_IP);
    750 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
    751 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
    752 	ah->ar_op = htons(ARPOP_REQUEST);
    753 	memcpy(ar_sha(ah), enaddr, ah->ar_hln);
    754 	memcpy(ar_spa(ah), sip, ah->ar_pln);
    755 	memcpy(ar_tpa(ah), tip, ah->ar_pln);
    756 	sa.sa_family = AF_ARP;
    757 	sa.sa_len = 2;
    758 	m->m_flags |= M_BCAST;
    759 	arps = ARP_STAT_GETREF();
    760 	arps[ARP_STAT_SNDTOTAL]++;
    761 	arps[ARP_STAT_SENDREQUEST]++;
    762 	ARP_STAT_PUTREF();
    763 	(*ifp->if_output)(ifp, m, &sa, NULL);
    764 }
    765 
    766 /*
    767  * Resolve an IP address into an ethernet address.  If success,
    768  * desten is filled in.  If there is no entry in arptab,
    769  * set one up and broadcast a request for the IP address.
    770  * Hold onto this mbuf and resend it once the address
    771  * is finally resolved.  A return value of 1 indicates
    772  * that desten has been filled in and the packet should be sent
    773  * normally; a 0 return indicates that the packet has been
    774  * taken over here, either now or for later transmission.
    775  */
    776 int
    777 arpresolve(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m,
    778     const struct sockaddr *dst, u_char *desten)
    779 {
    780 	struct llinfo_arp *la;
    781 	const struct sockaddr_dl *sdl;
    782 	struct mbuf *mold;
    783 	int s;
    784 
    785 	if ((la = arplookup1(m, &satocsin(dst)->sin_addr, 1, 0, rt)) != NULL)
    786 		rt = la->la_rt;
    787 
    788 	if (la == NULL || rt == NULL) {
    789 		ARP_STATINC(ARP_STAT_ALLOCFAIL);
    790 		log(LOG_DEBUG,
    791 		    "arpresolve: can't allocate llinfo on %s for %s\n",
    792 		    ifp->if_xname, in_fmtaddr(satocsin(dst)->sin_addr));
    793 		m_freem(m);
    794 		return 0;
    795 	}
    796 	sdl = satocsdl(rt->rt_gateway);
    797 	/*
    798 	 * Check the address family and length is valid, the address
    799 	 * is resolved; otherwise, try to resolve.
    800 	 */
    801 	if ((rt->rt_expire == 0 || rt->rt_expire > time_second) &&
    802 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
    803 		memcpy(desten, CLLADDR(sdl),
    804 		    min(sdl->sdl_alen, ifp->if_addrlen));
    805 		rt->rt_pksent = time_second; /* Time for last pkt sent */
    806 		return 1;
    807 	}
    808 	/*
    809 	 * There is an arptab entry, but no ethernet address
    810 	 * response yet.  Replace the held mbuf with this
    811 	 * latest one.
    812 	 */
    813 
    814 	ARP_STATINC(ARP_STAT_DFRTOTAL);
    815 	s = splnet();
    816 	mold = la->la_hold;
    817 	la->la_hold = m;
    818 	splx(s);
    819 
    820 	if (mold) {
    821 		ARP_STATINC(ARP_STAT_DFRDROPPED);
    822 		m_freem(mold);
    823 	}
    824 
    825 	/*
    826 	 * Re-send the ARP request when appropriate.
    827 	 */
    828 #ifdef	DIAGNOSTIC
    829 	if (rt->rt_expire == 0) {
    830 		/* This should never happen. (Should it? -gwr) */
    831 		printf("arpresolve: unresolved and rt_expire == 0\n");
    832 		/* Set expiration time to now (expired). */
    833 		rt->rt_expire = time_second;
    834 	}
    835 #endif
    836 	if (rt->rt_expire) {
    837 		rt->rt_flags &= ~RTF_REJECT;
    838 		if (la->la_asked == 0 || rt->rt_expire != time_second) {
    839 			rt->rt_expire = time_second;
    840 			if (la->la_asked++ < arp_maxtries) {
    841 				arprequest(ifp,
    842 				    &satocsin(rt->rt_ifa->ifa_addr)->sin_addr,
    843 				    &satocsin(dst)->sin_addr,
    844 #if NCARP > 0
    845 				    (rt->rt_ifp->if_type == IFT_CARP) ?
    846 				    CLLADDR(rt->rt_ifp->if_sadl):
    847 #endif
    848 				    CLLADDR(ifp->if_sadl));
    849 			} else {
    850 				rt->rt_flags |= RTF_REJECT;
    851 				rt->rt_expire += arpt_down;
    852 				la->la_asked = 0;
    853 			}
    854 		}
    855 	}
    856 	return 0;
    857 }
    858 
    859 /*
    860  * Common length and type checks are done here,
    861  * then the protocol-specific routine is called.
    862  */
    863 void
    864 arpintr(void)
    865 {
    866 	struct mbuf *m;
    867 	struct arphdr *ar;
    868 	int s;
    869 	int arplen;
    870 
    871 	mutex_enter(softnet_lock);
    872 	KERNEL_LOCK(1, NULL);
    873 	while (arpintrq.ifq_head) {
    874 		s = splnet();
    875 		IF_DEQUEUE(&arpintrq, m);
    876 		splx(s);
    877 		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
    878 			panic("arpintr");
    879 
    880 		MCLAIM(m, &arpdomain.dom_mowner);
    881 		ARP_STATINC(ARP_STAT_RCVTOTAL);
    882 
    883 		/*
    884 		 * First, make sure we have at least struct arphdr.
    885 		 */
    886 		if (m->m_len < sizeof(struct arphdr) ||
    887 		    (ar = mtod(m, struct arphdr *)) == NULL)
    888 			goto badlen;
    889 
    890 		switch (m->m_pkthdr.rcvif->if_type) {
    891 		case IFT_IEEE1394:
    892 			arplen = sizeof(struct arphdr) +
    893 			    ar->ar_hln + 2 * ar->ar_pln;
    894 			break;
    895 		default:
    896 			arplen = sizeof(struct arphdr) +
    897 			    2 * ar->ar_hln + 2 * ar->ar_pln;
    898 			break;
    899 		}
    900 
    901 		if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
    902 		    m->m_len >= arplen)
    903 			switch (ntohs(ar->ar_pro)) {
    904 			case ETHERTYPE_IP:
    905 			case ETHERTYPE_IPTRAILERS:
    906 				in_arpinput(m);
    907 				continue;
    908 			default:
    909 				ARP_STATINC(ARP_STAT_RCVBADPROTO);
    910 			}
    911 		else {
    912 badlen:
    913 			ARP_STATINC(ARP_STAT_RCVBADLEN);
    914 		}
    915 		m_freem(m);
    916 	}
    917 	KERNEL_UNLOCK_ONE(NULL);
    918 	mutex_exit(softnet_lock);
    919 }
    920 
    921 /*
    922  * ARP for Internet protocols on 10 Mb/s Ethernet.
    923  * Algorithm is that given in RFC 826.
    924  * In addition, a sanity check is performed on the sender
    925  * protocol address, to catch impersonators.
    926  * We no longer handle negotiations for use of trailer protocol:
    927  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
    928  * along with IP replies if we wanted trailers sent to us,
    929  * and also sent them in response to IP replies.
    930  * This allowed either end to announce the desire to receive
    931  * trailer packets.
    932  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
    933  * but formerly didn't normally send requests.
    934  */
    935 static void
    936 in_arpinput(struct mbuf *m)
    937 {
    938 	struct arphdr *ah;
    939 	struct ifnet *ifp = m->m_pkthdr.rcvif;
    940 	struct llinfo_arp *la = NULL;
    941 	struct rtentry  *rt;
    942 	struct in_ifaddr *ia;
    943 #if NBRIDGE > 0
    944 	struct in_ifaddr *bridge_ia = NULL;
    945 #endif
    946 #if NCARP > 0
    947 	u_int32_t count = 0, index = 0;
    948 #endif
    949 	struct sockaddr_dl *sdl;
    950 	struct sockaddr sa;
    951 	struct in_addr isaddr, itaddr, myaddr;
    952 	int op;
    953 	struct mbuf *mold;
    954 	void *tha;
    955 	int s;
    956 	uint64_t *arps;
    957 
    958 	if (__predict_false(m_makewritable(&m, 0, m->m_pkthdr.len, M_DONTWAIT)))
    959 		goto out;
    960 	ah = mtod(m, struct arphdr *);
    961 	op = ntohs(ah->ar_op);
    962 
    963 	/*
    964 	 * Fix up ah->ar_hrd if necessary, before using ar_tha() or
    965 	 * ar_tpa().
    966 	 */
    967 	switch (ifp->if_type) {
    968 	case IFT_IEEE1394:
    969 		if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
    970 			;
    971 		else {
    972 			/* XXX this is to make sure we compute ar_tha right */
    973 			/* XXX check ar_hrd more strictly? */
    974 			ah->ar_hrd = htons(ARPHRD_IEEE1394);
    975 		}
    976 		break;
    977 	default:
    978 		/* XXX check ar_hrd? */
    979 		break;
    980 	}
    981 
    982 	memcpy(&isaddr, ar_spa(ah), sizeof (isaddr));
    983 	memcpy(&itaddr, ar_tpa(ah), sizeof (itaddr));
    984 
    985 	if (m->m_flags & (M_BCAST|M_MCAST))
    986 		ARP_STATINC(ARP_STAT_RCVMCAST);
    987 
    988 	/*
    989 	 * If the target IP address is zero, ignore the packet.
    990 	 * This prevents the code below from tring to answer
    991 	 * when we are using IP address zero (booting).
    992 	 */
    993 	if (in_nullhost(itaddr)) {
    994 		ARP_STATINC(ARP_STAT_RCVZEROTPA);
    995 		goto out;
    996 	}
    997 
    998 
    999 	/*
   1000 	 * Search for a matching interface address
   1001 	 * or any address on the interface to use
   1002 	 * as a dummy address in the rest of this function
   1003 	 */
   1004 
   1005 	INADDR_TO_IA(itaddr, ia);
   1006 	while (ia != NULL) {
   1007 #if NCARP > 0
   1008 		if (ia->ia_ifp->if_type == IFT_CARP &&
   1009 		    ((ia->ia_ifp->if_flags & (IFF_UP|IFF_RUNNING)) ==
   1010 		    (IFF_UP|IFF_RUNNING))) {
   1011 			index++;
   1012 			if (ia->ia_ifp == m->m_pkthdr.rcvif &&
   1013 			    carp_iamatch(ia, ar_sha(ah),
   1014 			    &count, index)) {
   1015 				break;
   1016 				}
   1017 		} else
   1018 #endif
   1019 			    if (ia->ia_ifp == m->m_pkthdr.rcvif)
   1020 				break;
   1021 #if NBRIDGE > 0
   1022 		/*
   1023 		 * If the interface we received the packet on
   1024 		 * is part of a bridge, check to see if we need
   1025 		 * to "bridge" the packet to ourselves at this
   1026 		 * layer.  Note we still prefer a perfect match,
   1027 		 * but allow this weaker match if necessary.
   1028 		 */
   1029 		if (m->m_pkthdr.rcvif->if_bridge != NULL &&
   1030 		    m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge)
   1031 			bridge_ia = ia;
   1032 #endif /* NBRIDGE > 0 */
   1033 
   1034 		NEXT_IA_WITH_SAME_ADDR(ia);
   1035 	}
   1036 
   1037 #if NBRIDGE > 0
   1038 	if (ia == NULL && bridge_ia != NULL) {
   1039 		ia = bridge_ia;
   1040 		ifp = bridge_ia->ia_ifp;
   1041 	}
   1042 #endif
   1043 
   1044 	if (ia == NULL) {
   1045 		INADDR_TO_IA(isaddr, ia);
   1046 		while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
   1047 			NEXT_IA_WITH_SAME_ADDR(ia);
   1048 
   1049 		if (ia == NULL) {
   1050 			IFP_TO_IA(ifp, ia);
   1051 			if (ia == NULL) {
   1052 				ARP_STATINC(ARP_STAT_RCVNOINT);
   1053 				goto out;
   1054 			}
   1055 		}
   1056 	}
   1057 
   1058 	myaddr = ia->ia_addr.sin_addr;
   1059 
   1060 	/* XXX checks for bridge case? */
   1061 	if (!memcmp(ar_sha(ah), CLLADDR(ifp->if_sadl), ifp->if_addrlen)) {
   1062 		ARP_STATINC(ARP_STAT_RCVLOCALSHA);
   1063 		goto out;	/* it's from me, ignore it. */
   1064 	}
   1065 
   1066 	/* XXX checks for bridge case? */
   1067 	if (!memcmp(ar_sha(ah), ifp->if_broadcastaddr, ifp->if_addrlen)) {
   1068 		ARP_STATINC(ARP_STAT_RCVBCASTSHA);
   1069 		log(LOG_ERR,
   1070 		    "%s: arp: link address is broadcast for IP address %s!\n",
   1071 		    ifp->if_xname, in_fmtaddr(isaddr));
   1072 		goto out;
   1073 	}
   1074 
   1075 	/*
   1076 	 * If the source IP address is zero, this is an RFC 5227 ARP probe
   1077 	 */
   1078 	if (in_nullhost(isaddr)) {
   1079 		ARP_STATINC(ARP_STAT_RCVZEROSPA);
   1080 		goto reply;
   1081 	}
   1082 
   1083 	if (in_hosteq(isaddr, myaddr)) {
   1084 		ARP_STATINC(ARP_STAT_RCVLOCALSPA);
   1085 		log(LOG_ERR,
   1086 		   "duplicate IP address %s sent from link address %s\n",
   1087 		   in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
   1088 		itaddr = myaddr;
   1089 		goto reply;
   1090 	}
   1091 	la = arplookup(m, &isaddr, in_hosteq(itaddr, myaddr), 0);
   1092 	if (la != NULL && (rt = la->la_rt) && (sdl = satosdl(rt->rt_gateway))) {
   1093 		if (sdl->sdl_alen &&
   1094 		    memcmp(ar_sha(ah), CLLADDR(sdl), sdl->sdl_alen)) {
   1095 			if (rt->rt_flags & RTF_STATIC) {
   1096 				ARP_STATINC(ARP_STAT_RCVOVERPERM);
   1097 				if (!log_permanent_modify)
   1098 					goto out;
   1099 				log(LOG_INFO,
   1100 				    "%s tried to overwrite permanent arp info"
   1101 				    " for %s\n",
   1102 				    lla_snprintf(ar_sha(ah), ah->ar_hln),
   1103 				    in_fmtaddr(isaddr));
   1104 				goto out;
   1105 			} else if (rt->rt_ifp != ifp) {
   1106 				ARP_STATINC(ARP_STAT_RCVOVERINT);
   1107 				if (!log_wrong_iface)
   1108 					goto out;
   1109 				log(LOG_INFO,
   1110 				    "%s on %s tried to overwrite "
   1111 				    "arp info for %s on %s\n",
   1112 				    lla_snprintf(ar_sha(ah), ah->ar_hln),
   1113 				    ifp->if_xname, in_fmtaddr(isaddr),
   1114 				    rt->rt_ifp->if_xname);
   1115 				    goto out;
   1116 			} else {
   1117 				ARP_STATINC(ARP_STAT_RCVOVER);
   1118 				if (log_movements)
   1119 					log(LOG_INFO, "arp info overwritten "
   1120 					    "for %s by %s\n",
   1121 					    in_fmtaddr(isaddr),
   1122 					    lla_snprintf(ar_sha(ah),
   1123 					    ah->ar_hln));
   1124 			}
   1125 		}
   1126 		/*
   1127 		 * sanity check for the address length.
   1128 		 * XXX this does not work for protocols with variable address
   1129 		 * length. -is
   1130 		 */
   1131 		if (sdl->sdl_alen &&
   1132 		    sdl->sdl_alen != ah->ar_hln) {
   1133 			ARP_STATINC(ARP_STAT_RCVLENCHG);
   1134 			log(LOG_WARNING,
   1135 			    "arp from %s: new addr len %d, was %d\n",
   1136 			    in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
   1137 		}
   1138 		if (ifp->if_addrlen != ah->ar_hln) {
   1139 			ARP_STATINC(ARP_STAT_RCVBADLEN);
   1140 			log(LOG_WARNING,
   1141 			    "arp from %s: addr len: new %d, i/f %d (ignored)\n",
   1142 			    in_fmtaddr(isaddr), ah->ar_hln,
   1143 			    ifp->if_addrlen);
   1144 			goto reply;
   1145 		}
   1146 #if NTOKEN > 0
   1147 		/*
   1148 		 * XXX uses m_data and assumes the complete answer including
   1149 		 * XXX token-ring headers is in the same buf
   1150 		 */
   1151 		if (ifp->if_type == IFT_ISO88025) {
   1152 			struct token_header *trh;
   1153 
   1154 			trh = (struct token_header *)M_TRHSTART(m);
   1155 			if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
   1156 				struct token_rif	*rif;
   1157 				size_t	riflen;
   1158 
   1159 				rif = TOKEN_RIF(trh);
   1160 				riflen = (ntohs(rif->tr_rcf) &
   1161 				    TOKEN_RCF_LEN_MASK) >> 8;
   1162 
   1163 				if (riflen > 2 &&
   1164 				    riflen < sizeof(struct token_rif) &&
   1165 				    (riflen & 1) == 0) {
   1166 					rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
   1167 					rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
   1168 					memcpy(TOKEN_RIF(la), rif, riflen);
   1169 				}
   1170 			}
   1171 		}
   1172 #endif /* NTOKEN > 0 */
   1173 		(void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, ar_sha(ah),
   1174 		    ah->ar_hln);
   1175 		if (rt->rt_expire)
   1176 			rt->rt_expire = time_second + arpt_keep;
   1177 		rt->rt_flags &= ~RTF_REJECT;
   1178 		la->la_asked = 0;
   1179 
   1180 		s = splnet();
   1181 		mold = la->la_hold;
   1182 		la->la_hold = 0;
   1183 		splx(s);
   1184 
   1185 		if (mold) {
   1186 			ARP_STATINC(ARP_STAT_DFRSENT);
   1187 			(*ifp->if_output)(ifp, mold, rt_getkey(rt), rt);
   1188 		}
   1189 	}
   1190 reply:
   1191 	if (op != ARPOP_REQUEST) {
   1192 		if (op == ARPOP_REPLY)
   1193 			ARP_STATINC(ARP_STAT_RCVREPLY);
   1194 	out:
   1195 		m_freem(m);
   1196 		return;
   1197 	}
   1198 	ARP_STATINC(ARP_STAT_RCVREQUEST);
   1199 	if (in_hosteq(itaddr, myaddr)) {
   1200 		/* I am the target */
   1201 		tha = ar_tha(ah);
   1202 		if (tha)
   1203 			memcpy(tha, ar_sha(ah), ah->ar_hln);
   1204 		memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
   1205 	} else {
   1206 		la = arplookup(m, &itaddr, 0, SIN_PROXY);
   1207 		if (la == NULL)
   1208 			goto out;
   1209 		rt = la->la_rt;
   1210 		if (rt->rt_ifp->if_type == IFT_CARP &&
   1211 		    m->m_pkthdr.rcvif->if_type != IFT_CARP)
   1212 			goto out;
   1213 		tha = ar_tha(ah);
   1214 		if (tha)
   1215 			memcpy(tha, ar_sha(ah), ah->ar_hln);
   1216 		sdl = satosdl(rt->rt_gateway);
   1217 		memcpy(ar_sha(ah), CLLADDR(sdl), ah->ar_hln);
   1218 	}
   1219 
   1220 	memcpy(ar_tpa(ah), ar_spa(ah), ah->ar_pln);
   1221 	memcpy(ar_spa(ah), &itaddr, ah->ar_pln);
   1222 	ah->ar_op = htons(ARPOP_REPLY);
   1223 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
   1224 	switch (ifp->if_type) {
   1225 	case IFT_IEEE1394:
   1226 		/*
   1227 		 * ieee1394 arp reply is broadcast
   1228 		 */
   1229 		m->m_flags &= ~M_MCAST;
   1230 		m->m_flags |= M_BCAST;
   1231 		m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + ah->ar_hln;
   1232 		break;
   1233 
   1234 	default:
   1235 		m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
   1236 		m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
   1237 		break;
   1238 	}
   1239 	m->m_pkthdr.len = m->m_len;
   1240 	sa.sa_family = AF_ARP;
   1241 	sa.sa_len = 2;
   1242 	arps = ARP_STAT_GETREF();
   1243 	arps[ARP_STAT_SNDTOTAL]++;
   1244 	arps[ARP_STAT_SNDREPLY]++;
   1245 	ARP_STAT_PUTREF();
   1246 	(*ifp->if_output)(ifp, m, &sa, NULL);
   1247 	return;
   1248 }
   1249 
   1250 /*
   1251  * Free an arp entry.
   1252  */
   1253 static void arptfree(struct llinfo_arp *la)
   1254 {
   1255 	struct rtentry *rt = la->la_rt;
   1256 	struct sockaddr_dl *sdl;
   1257 
   1258 	ARP_LOCK_CHECK();
   1259 
   1260 	if (rt == NULL)
   1261 		panic("arptfree");
   1262 	if (rt->rt_refcnt > 0 && (sdl = satosdl(rt->rt_gateway)) &&
   1263 	    sdl->sdl_family == AF_LINK) {
   1264 		sdl->sdl_alen = 0;
   1265 		la->la_asked = 0;
   1266 		rt->rt_flags &= ~RTF_REJECT;
   1267 		return;
   1268 	}
   1269 	rtrequest(RTM_DELETE, rt_getkey(rt), NULL, rt_mask(rt), 0, NULL);
   1270 }
   1271 
   1272 static struct llinfo_arp *
   1273 arplookup(struct mbuf *m, const struct in_addr *addr, int create, int proxy)
   1274 {
   1275 	return arplookup1(m, addr, create, proxy, NULL);
   1276 }
   1277 
   1278 /*
   1279  * Lookup or enter a new address in arptab.
   1280  */
   1281 static struct llinfo_arp *
   1282 arplookup1(struct mbuf *m, const struct in_addr *addr, int create, int proxy,
   1283     struct rtentry *rt0)
   1284 {
   1285 	struct arphdr *ah;
   1286 	struct ifnet *ifp = m->m_pkthdr.rcvif;
   1287 	struct rtentry *rt;
   1288 	struct sockaddr_inarp sin;
   1289 	const char *why = NULL;
   1290 
   1291 	ah = mtod(m, struct arphdr *);
   1292 	if (rt0 == NULL) {
   1293 		memset(&sin, 0, sizeof(sin));
   1294 		sin.sin_len = sizeof(sin);
   1295 		sin.sin_family = AF_INET;
   1296 		sin.sin_addr = *addr;
   1297 		sin.sin_other = proxy ? SIN_PROXY : 0;
   1298 		rt = rtalloc1(sintosa(&sin), create);
   1299 		if (rt == NULL)
   1300 			return NULL;
   1301 		rt->rt_refcnt--;
   1302 	} else
   1303 		rt = rt0;
   1304 
   1305 #define	IS_LLINFO(__rt)							  \
   1306 	(((__rt)->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) == RTF_LLINFO && \
   1307 	 (__rt)->rt_gateway->sa_family == AF_LINK)
   1308 
   1309 
   1310 	if (IS_LLINFO(rt))
   1311 		return (struct llinfo_arp *)rt->rt_llinfo;
   1312 
   1313 	if (create) {
   1314 		if (rt->rt_flags & RTF_GATEWAY) {
   1315 			if (log_unknown_network)
   1316 				why = "host is not on local network";
   1317 		} else if ((rt->rt_flags & RTF_LLINFO) == 0) {
   1318 			ARP_STATINC(ARP_STAT_ALLOCFAIL);
   1319 			why = "could not allocate llinfo";
   1320 		} else
   1321 			why = "gateway route is not ours";
   1322 		if (why) {
   1323 			log(LOG_DEBUG, "arplookup: unable to enter address"
   1324 			    " for %s@%s on %s (%s)\n", in_fmtaddr(*addr),
   1325 			    lla_snprintf(ar_sha(ah), ah->ar_hln),
   1326 			    (ifp) ? ifp->if_xname : "null", why);
   1327 		}
   1328 		if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_CLONED) != 0) {
   1329 			rtrequest(RTM_DELETE, rt_getkey(rt),
   1330 		    	    rt->rt_gateway, rt_mask(rt), rt->rt_flags, NULL);
   1331 		}
   1332 	}
   1333 	return NULL;
   1334 }
   1335 
   1336 int
   1337 arpioctl(u_long cmd, void *data)
   1338 {
   1339 
   1340 	return EOPNOTSUPP;
   1341 }
   1342 
   1343 void
   1344 arp_ifinit(struct ifnet *ifp, struct ifaddr *ifa)
   1345 {
   1346 	struct in_addr *ip;
   1347 
   1348 	/*
   1349 	 * Warn the user if another station has this IP address,
   1350 	 * but only if the interface IP address is not zero.
   1351 	 */
   1352 	ip = &IA_SIN(ifa)->sin_addr;
   1353 	if (!in_nullhost(*ip))
   1354 		arprequest(ifp, ip, ip, CLLADDR(ifp->if_sadl));
   1355 
   1356 	ifa->ifa_rtrequest = arp_rtrequest;
   1357 	ifa->ifa_flags |= RTF_CLONING;
   1358 }
   1359 
   1360 /*
   1361  * Called from 10 Mb/s Ethernet interrupt handlers
   1362  * when ether packet type ETHERTYPE_REVARP
   1363  * is received.  Common length and type checks are done here,
   1364  * then the protocol-specific routine is called.
   1365  */
   1366 void
   1367 revarpinput(struct mbuf *m)
   1368 {
   1369 	struct arphdr *ar;
   1370 
   1371 	if (m->m_len < sizeof(struct arphdr))
   1372 		goto out;
   1373 	ar = mtod(m, struct arphdr *);
   1374 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
   1375 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
   1376 		goto out;
   1377 #endif
   1378 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
   1379 		goto out;
   1380 	switch (ntohs(ar->ar_pro)) {
   1381 	case ETHERTYPE_IP:
   1382 	case ETHERTYPE_IPTRAILERS:
   1383 		in_revarpinput(m);
   1384 		return;
   1385 
   1386 	default:
   1387 		break;
   1388 	}
   1389 out:
   1390 	m_freem(m);
   1391 }
   1392 
   1393 /*
   1394  * RARP for Internet protocols on 10 Mb/s Ethernet.
   1395  * Algorithm is that given in RFC 903.
   1396  * We are only using for bootstrap purposes to get an ip address for one of
   1397  * our interfaces.  Thus we support no user-interface.
   1398  *
   1399  * Since the contents of the RARP reply are specific to the interface that
   1400  * sent the request, this code must ensure that they are properly associated.
   1401  *
   1402  * Note: also supports ARP via RARP packets, per the RFC.
   1403  */
   1404 void
   1405 in_revarpinput(struct mbuf *m)
   1406 {
   1407 	struct ifnet *ifp;
   1408 	struct arphdr *ah;
   1409 	void *tha;
   1410 	int op;
   1411 
   1412 	ah = mtod(m, struct arphdr *);
   1413 	op = ntohs(ah->ar_op);
   1414 
   1415 	switch (m->m_pkthdr.rcvif->if_type) {
   1416 	case IFT_IEEE1394:
   1417 		/* ARP without target hardware address is not supported */
   1418 		goto out;
   1419 	default:
   1420 		break;
   1421 	}
   1422 
   1423 	switch (op) {
   1424 	case ARPOP_REQUEST:
   1425 	case ARPOP_REPLY:	/* per RFC */
   1426 		in_arpinput(m);
   1427 		return;
   1428 	case ARPOP_REVREPLY:
   1429 		break;
   1430 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
   1431 	default:
   1432 		goto out;
   1433 	}
   1434 	if (!revarp_in_progress)
   1435 		goto out;
   1436 	ifp = m->m_pkthdr.rcvif;
   1437 	if (ifp != myip_ifp) /* !same interface */
   1438 		goto out;
   1439 	if (myip_initialized)
   1440 		goto wake;
   1441 	tha = ar_tha(ah);
   1442 	if (tha == NULL)
   1443 		goto out;
   1444 	if (memcmp(tha, CLLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
   1445 		goto out;
   1446 	memcpy(&srv_ip, ar_spa(ah), sizeof(srv_ip));
   1447 	memcpy(&myip, ar_tpa(ah), sizeof(myip));
   1448 	myip_initialized = 1;
   1449 wake:	/* Do wakeup every time in case it was missed. */
   1450 	wakeup((void *)&myip);
   1451 
   1452 out:
   1453 	m_freem(m);
   1454 }
   1455 
   1456 /*
   1457  * Send a RARP request for the ip address of the specified interface.
   1458  * The request should be RFC 903-compliant.
   1459  */
   1460 void
   1461 revarprequest(struct ifnet *ifp)
   1462 {
   1463 	struct sockaddr sa;
   1464 	struct mbuf *m;
   1465 	struct arphdr *ah;
   1466 	void *tha;
   1467 
   1468 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
   1469 		return;
   1470 	MCLAIM(m, &arpdomain.dom_mowner);
   1471 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
   1472 	    2*ifp->if_addrlen;
   1473 	m->m_pkthdr.len = m->m_len;
   1474 	MH_ALIGN(m, m->m_len);
   1475 	ah = mtod(m, struct arphdr *);
   1476 	memset(ah, 0, m->m_len);
   1477 	ah->ar_pro = htons(ETHERTYPE_IP);
   1478 	ah->ar_hln = ifp->if_addrlen;		/* hardware address length */
   1479 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
   1480 	ah->ar_op = htons(ARPOP_REVREQUEST);
   1481 
   1482 	memcpy(ar_sha(ah), CLLADDR(ifp->if_sadl), ah->ar_hln);
   1483 	tha = ar_tha(ah);
   1484 	if (tha == NULL) {
   1485 		m_free(m);
   1486 		return;
   1487 	}
   1488 	memcpy(tha, CLLADDR(ifp->if_sadl), ah->ar_hln);
   1489 
   1490 	sa.sa_family = AF_ARP;
   1491 	sa.sa_len = 2;
   1492 	m->m_flags |= M_BCAST;
   1493 
   1494 	KERNEL_LOCK(1, NULL);
   1495 	(*ifp->if_output)(ifp, m, &sa, NULL);
   1496 	KERNEL_UNLOCK_ONE(NULL);
   1497 }
   1498 
   1499 /*
   1500  * RARP for the ip address of the specified interface, but also
   1501  * save the ip address of the server that sent the answer.
   1502  * Timeout if no response is received.
   1503  */
   1504 int
   1505 revarpwhoarewe(struct ifnet *ifp, struct in_addr *serv_in,
   1506     struct in_addr *clnt_in)
   1507 {
   1508 	int result, count = 20;
   1509 
   1510 	myip_initialized = 0;
   1511 	myip_ifp = ifp;
   1512 
   1513 	revarp_in_progress = 1;
   1514 	while (count--) {
   1515 		revarprequest(ifp);
   1516 		result = tsleep((void *)&myip, PSOCK, "revarp", hz/2);
   1517 		if (result != EWOULDBLOCK)
   1518 			break;
   1519 	}
   1520 	revarp_in_progress = 0;
   1521 
   1522 	if (!myip_initialized)
   1523 		return ENETUNREACH;
   1524 
   1525 	memcpy(serv_in, &srv_ip, sizeof(*serv_in));
   1526 	memcpy(clnt_in, &myip, sizeof(*clnt_in));
   1527 	return 0;
   1528 }
   1529 
   1530 
   1531 
   1533 #ifdef DDB
   1534 
   1535 #include <machine/db_machdep.h>
   1536 #include <ddb/db_interface.h>
   1537 #include <ddb/db_output.h>
   1538 
   1539 static void
   1540 db_print_sa(const struct sockaddr *sa)
   1541 {
   1542 	int len;
   1543 	const u_char *p;
   1544 
   1545 	if (sa == NULL) {
   1546 		db_printf("[NULL]");
   1547 		return;
   1548 	}
   1549 
   1550 	p = (const u_char *)sa;
   1551 	len = sa->sa_len;
   1552 	db_printf("[");
   1553 	while (len > 0) {
   1554 		db_printf("%d", *p);
   1555 		p++; len--;
   1556 		if (len) db_printf(",");
   1557 	}
   1558 	db_printf("]\n");
   1559 }
   1560 
   1561 static void
   1562 db_print_ifa(struct ifaddr *ifa)
   1563 {
   1564 	if (ifa == NULL)
   1565 		return;
   1566 	db_printf("  ifa_addr=");
   1567 	db_print_sa(ifa->ifa_addr);
   1568 	db_printf("  ifa_dsta=");
   1569 	db_print_sa(ifa->ifa_dstaddr);
   1570 	db_printf("  ifa_mask=");
   1571 	db_print_sa(ifa->ifa_netmask);
   1572 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
   1573 			  ifa->ifa_flags,
   1574 			  ifa->ifa_refcnt,
   1575 			  ifa->ifa_metric);
   1576 }
   1577 
   1578 static void
   1579 db_print_llinfo(void *li)
   1580 {
   1581 	struct llinfo_arp *la;
   1582 
   1583 	if (li == NULL)
   1584 		return;
   1585 	la = (struct llinfo_arp *)li;
   1586 	db_printf("  la_rt=%p la_hold=%p, la_asked=0x%lx\n",
   1587 			  la->la_rt, la->la_hold, la->la_asked);
   1588 }
   1589 
   1590 /*
   1591  * Function to pass to rt_walktree().
   1592  * Return non-zero error to abort walk.
   1593  */
   1594 static int
   1595 db_show_rtentry(struct rtentry *rt, void *w)
   1596 {
   1597 	db_printf("rtentry=%p", rt);
   1598 
   1599 	db_printf(" flags=0x%x refcnt=%d use=%"PRId64" expire=%"PRId64"\n",
   1600 			  rt->rt_flags, rt->rt_refcnt,
   1601 			  rt->rt_use, (uint64_t)rt->rt_expire);
   1602 
   1603 	db_printf(" key="); db_print_sa(rt_getkey(rt));
   1604 	db_printf(" mask="); db_print_sa(rt_mask(rt));
   1605 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
   1606 
   1607 	db_printf(" ifp=%p ", rt->rt_ifp);
   1608 	if (rt->rt_ifp)
   1609 		db_printf("(%s)", rt->rt_ifp->if_xname);
   1610 	else
   1611 		db_printf("(NULL)");
   1612 
   1613 	db_printf(" ifa=%p\n", rt->rt_ifa);
   1614 	db_print_ifa(rt->rt_ifa);
   1615 
   1616 	db_printf(" gwroute=%p llinfo=%p\n",
   1617 			  rt->rt_gwroute, rt->rt_llinfo);
   1618 	db_print_llinfo(rt->rt_llinfo);
   1619 
   1620 	return 0;
   1621 }
   1622 
   1623 /*
   1624  * Function to print all the route trees.
   1625  * Use this from ddb:  "show arptab"
   1626  */
   1627 void
   1628 db_show_arptab(db_expr_t addr, bool have_addr,
   1629     db_expr_t count, const char *modif)
   1630 {
   1631 	rt_walktree(AF_INET, db_show_rtentry, NULL);
   1632 }
   1633 #endif
   1634 
   1635 static int
   1636 sysctl_net_inet_arp_stats(SYSCTLFN_ARGS)
   1637 {
   1638 
   1639 	return NETSTAT_SYSCTL(arpstat_percpu, ARP_NSTATS);
   1640 }
   1641 
   1642 static void
   1643 sysctl_net_inet_arp_setup(struct sysctllog **clog)
   1644 {
   1645 	const struct sysctlnode *node;
   1646 
   1647 	sysctl_createv(clog, 0, NULL, NULL,
   1648 			CTLFLAG_PERMANENT,
   1649 			CTLTYPE_NODE, "inet", NULL,
   1650 			NULL, 0, NULL, 0,
   1651 			CTL_NET, PF_INET, CTL_EOL);
   1652 	sysctl_createv(clog, 0, NULL, &node,
   1653 			CTLFLAG_PERMANENT,
   1654 			CTLTYPE_NODE, "arp",
   1655 			SYSCTL_DESCR("Address Resolution Protocol"),
   1656 			NULL, 0, NULL, 0,
   1657 			CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
   1658 
   1659 	sysctl_createv(clog, 0, NULL, NULL,
   1660 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1661 			CTLTYPE_INT, "prune",
   1662 			SYSCTL_DESCR("ARP cache pruning interval in seconds"),
   1663 			NULL, 0, &arpt_prune, 0,
   1664 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1665 
   1666 	sysctl_createv(clog, 0, NULL, NULL,
   1667 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1668 			CTLTYPE_INT, "keep",
   1669 			SYSCTL_DESCR("Valid ARP entry lifetime in seconds"),
   1670 			NULL, 0, &arpt_keep, 0,
   1671 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1672 
   1673 	sysctl_createv(clog, 0, NULL, NULL,
   1674 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1675 			CTLTYPE_INT, "down",
   1676 			SYSCTL_DESCR("Failed ARP entry lifetime in seconds"),
   1677 			NULL, 0, &arpt_down, 0,
   1678 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1679 
   1680 	sysctl_createv(clog, 0, NULL, NULL,
   1681 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1682 			CTLTYPE_INT, "refresh",
   1683 			SYSCTL_DESCR("ARP entry refresh interval"),
   1684 			NULL, 0, &arpt_refresh, 0,
   1685 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1686 
   1687 	sysctl_createv(clog, 0, NULL, NULL,
   1688 			CTLFLAG_PERMANENT,
   1689 			CTLTYPE_STRUCT, "stats",
   1690 			SYSCTL_DESCR("ARP statistics"),
   1691 			sysctl_net_inet_arp_stats, 0, NULL, 0,
   1692 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1693 
   1694 	sysctl_createv(clog, 0, NULL, NULL,
   1695 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1696 			CTLTYPE_INT, "log_movements",
   1697 			SYSCTL_DESCR("log ARP replies from MACs different than"
   1698 			    " the one in the cache"),
   1699 			NULL, 0, &log_movements, 0,
   1700 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1701 
   1702 	sysctl_createv(clog, 0, NULL, NULL,
   1703 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1704 			CTLTYPE_INT, "log_permanent_modify",
   1705 			SYSCTL_DESCR("log ARP replies from MACs different than"
   1706 			    " the one in the permanent arp entry"),
   1707 			NULL, 0, &log_permanent_modify, 0,
   1708 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1709 
   1710 	sysctl_createv(clog, 0, NULL, NULL,
   1711 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1712 			CTLTYPE_INT, "log_wrong_iface",
   1713 			SYSCTL_DESCR("log ARP packets arriving on the wrong"
   1714 			    " interface"),
   1715 			NULL, 0, &log_wrong_iface, 0,
   1716 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1717 
   1718 	sysctl_createv(clog, 0, NULL, NULL,
   1719 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1720 			CTLTYPE_INT, "log_unknown_network",
   1721 			SYSCTL_DESCR("log ARP packets from non-local network"),
   1722 			NULL, 0, &log_unknown_network, 0,
   1723 			CTL_NET,PF_INET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1724 }
   1725 
   1726 #endif /* INET */
   1727