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