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