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