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