Home | History | Annotate | Line # | Download | only in netinet
if_arp.c revision 1.84
      1 /*	$NetBSD: if_arp.c,v 1.84 2002/06/24 10:52:15 enami 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.84 2002/06/24 10:52:15 enami 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 		/*
    779 		 * First, make sure we have at least struct arphdr.
    780 		 */
    781 		if (m->m_len < sizeof(struct arphdr) ||
    782 		    (ar = mtod(m, struct arphdr *)) == NULL)
    783 			goto badlen;
    784 
    785 		switch (m->m_pkthdr.rcvif->if_type) {
    786 		case IFT_IEEE1394:
    787 			arplen = sizeof(struct arphdr) +
    788 			    ar->ar_hln + 2 * ar->ar_pln;
    789 			break;
    790 		default:
    791 			arplen = sizeof(struct arphdr) +
    792 			    2 * ar->ar_hln + 2 * ar->ar_pln;
    793 			break;
    794 		}
    795 
    796 		if (/* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
    797 		    m->m_len >= arplen)
    798 			switch (ntohs(ar->ar_pro)) {
    799 			case ETHERTYPE_IP:
    800 			case ETHERTYPE_IPTRAILERS:
    801 				in_arpinput(m);
    802 				continue;
    803 			default:
    804 				arpstat.as_rcvbadproto++;
    805 			}
    806 		else {
    807 badlen:
    808 			arpstat.as_rcvbadlen++;
    809 		}
    810 		m_freem(m);
    811 	}
    812 }
    813 
    814 /*
    815  * ARP for Internet protocols on 10 Mb/s Ethernet.
    816  * Algorithm is that given in RFC 826.
    817  * In addition, a sanity check is performed on the sender
    818  * protocol address, to catch impersonators.
    819  * We no longer handle negotiations for use of trailer protocol:
    820  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
    821  * along with IP replies if we wanted trailers sent to us,
    822  * and also sent them in response to IP replies.
    823  * This allowed either end to announce the desire to receive
    824  * trailer packets.
    825  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
    826  * but formerly didn't normally send requests.
    827  */
    828 static void
    829 in_arpinput(m)
    830 	struct mbuf *m;
    831 {
    832 	struct arphdr *ah;
    833 	struct ifnet *ifp = m->m_pkthdr.rcvif;
    834 	struct llinfo_arp *la = 0;
    835 	struct rtentry  *rt;
    836 	struct in_ifaddr *ia;
    837 #if NBRIDGE > 0
    838 	struct in_ifaddr *bridge_ia = NULL;
    839 #endif
    840 	struct sockaddr_dl *sdl;
    841 	struct sockaddr sa;
    842 	struct in_addr isaddr, itaddr, myaddr;
    843 	int op;
    844 	struct mbuf *mold;
    845 	int s;
    846 
    847 	ah = mtod(m, struct arphdr *);
    848 	op = ntohs(ah->ar_op);
    849 	bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr));
    850 	bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr));
    851 
    852 	switch (m->m_pkthdr.rcvif->if_type) {
    853 	case IFT_IEEE1394:
    854 		if (ntohs(ah->ar_hrd) == ARPHRD_IEEE1394)
    855 			;
    856 		else {
    857 			/* XXX this is to make sure we compute ar_tha right */
    858 			/* XXX check ar_hrd more strictly? */
    859 			ah->ar_hrd = htons(ARPHRD_IEEE1394);
    860 		}
    861 		break;
    862 	default:
    863 		/* XXX check ar_hrd? */
    864 		break;
    865 	}
    866 
    867 	if (m->m_flags & (M_BCAST|M_MCAST))
    868 		arpstat.as_rcvmcast++;
    869 
    870 	/*
    871 	 * If the target IP address is zero, ignore the packet.
    872 	 * This prevents the code below from tring to answer
    873 	 * when we are using IP address zero (booting).
    874 	 */
    875 	if (in_nullhost(itaddr)) {
    876 		arpstat.as_rcvzerotpa++;
    877 		goto out;
    878 	}
    879 
    880 	/*
    881 	 * If the source IP address is zero, this is most likely a
    882 	 * confused host trying to use IP address zero. (Windoze?)
    883 	 * XXX: Should we bother trying to reply to these?
    884 	 */
    885 	if (in_nullhost(isaddr)) {
    886 		arpstat.as_rcvzerospa++;
    887 		goto out;
    888 	}
    889 
    890 	/*
    891 	 * Search for a matching interface address
    892 	 * or any address on the interface to use
    893 	 * as a dummy address in the rest of this function
    894 	 */
    895 	INADDR_TO_IA(itaddr, ia);
    896 	while (ia != NULL) {
    897 		if (ia->ia_ifp == m->m_pkthdr.rcvif)
    898 			break;
    899 
    900 #if NBRIDGE > 0
    901 		/*
    902 		 * If the interface we received the packet on
    903 		 * is part of a bridge, check to see if we need
    904 		 * to "bridge" the packet to ourselves at this
    905 		 * layer.  Note we still prefer a perfect match,
    906 		 * but allow this weaker match if necessary.
    907 		 */
    908 		if (m->m_pkthdr.rcvif->if_bridge != NULL &&
    909 		    m->m_pkthdr.rcvif->if_bridge == ia->ia_ifp->if_bridge)
    910 			bridge_ia = ia;
    911 #endif /* NBRIDGE > 0 */
    912 
    913 		NEXT_IA_WITH_SAME_ADDR(ia);
    914 	}
    915 
    916 #if NBRIDGE > 0
    917 	if (ia == NULL && bridge_ia != NULL) {
    918 		ia = bridge_ia;
    919 		ifp = bridge_ia->ia_ifp;
    920 	}
    921 #endif
    922 
    923 	if (ia == NULL) {
    924 		INADDR_TO_IA(isaddr, ia);
    925 		while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
    926 			NEXT_IA_WITH_SAME_ADDR(ia);
    927 
    928 		if (ia == NULL) {
    929 			IFP_TO_IA(ifp, ia);
    930 			if (ia == NULL) {
    931 				arpstat.as_rcvnoint++;
    932 				goto out;
    933 			}
    934 		}
    935 	}
    936 
    937 	myaddr = ia->ia_addr.sin_addr;
    938 
    939 	/* XXX checks for bridge case? */
    940 	if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
    941 	    ifp->if_data.ifi_addrlen)) {
    942 		arpstat.as_rcvlocalsha++;
    943 		goto out;	/* it's from me, ignore it. */
    944 	}
    945 
    946 	/* XXX checks for bridge case? */
    947 	if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)ifp->if_broadcastaddr,
    948 	    ifp->if_data.ifi_addrlen)) {
    949 		arpstat.as_rcvbcastsha++;
    950 		log(LOG_ERR,
    951 		    "%s: arp: link address is broadcast for IP address %s!\n",
    952 		    ifp->if_xname, in_fmtaddr(isaddr));
    953 		goto out;
    954 	}
    955 
    956 	if (in_hosteq(isaddr, myaddr)) {
    957 		arpstat.as_rcvlocalspa++;
    958 		log(LOG_ERR,
    959 		   "duplicate IP address %s sent from link address %s\n",
    960 		   in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
    961 		itaddr = myaddr;
    962 		goto reply;
    963 	}
    964 	la = arplookup(m, &isaddr, in_hosteq(itaddr, myaddr), 0);
    965 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
    966 		if (sdl->sdl_alen &&
    967 		    bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
    968 			if (rt->rt_flags & RTF_STATIC) {
    969 				arpstat.as_rcvoverperm++;
    970 				log(LOG_INFO,
    971 				    "%s tried to overwrite permanent arp info"
    972 				    " for %s\n",
    973 				    lla_snprintf(ar_sha(ah), ah->ar_hln),
    974 				    in_fmtaddr(isaddr));
    975 				goto out;
    976 			} else if (rt->rt_ifp != ifp) {
    977 				arpstat.as_rcvoverint++;
    978 				log(LOG_INFO,
    979 				    "%s on %s tried to overwrite "
    980 				    "arp info for %s on %s\n",
    981 				    lla_snprintf(ar_sha(ah), ah->ar_hln),
    982 				    ifp->if_xname, in_fmtaddr(isaddr),
    983 				    rt->rt_ifp->if_xname);
    984 				    goto out;
    985 			} else {
    986 				arpstat.as_rcvover++;
    987 				log(LOG_INFO,
    988 				    "arp info overwritten for %s by %s\n",
    989 				    in_fmtaddr(isaddr),
    990 				    lla_snprintf(ar_sha(ah), ah->ar_hln));
    991 			}
    992 		}
    993 		/*
    994 		 * sanity check for the address length.
    995 		 * XXX this does not work for protocols with variable address
    996 		 * length. -is
    997 		 */
    998 		if (sdl->sdl_alen &&
    999 		    sdl->sdl_alen != ah->ar_hln) {
   1000 			arpstat.as_rcvlenchg++;
   1001 			log(LOG_WARNING,
   1002 			    "arp from %s: new addr len %d, was %d",
   1003 			    in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
   1004 		}
   1005 		if (ifp->if_data.ifi_addrlen != ah->ar_hln) {
   1006 			arpstat.as_rcvbadlen++;
   1007 			log(LOG_WARNING,
   1008 			    "arp from %s: addr len: new %d, i/f %d (ignored)",
   1009 			    in_fmtaddr(isaddr), ah->ar_hln,
   1010 			    ifp->if_data.ifi_addrlen);
   1011 			goto reply;
   1012 		}
   1013 #if NTOKEN > 0
   1014 		/*
   1015 		 * XXX uses m_data and assumes the complete answer including
   1016 		 * XXX token-ring headers is in the same buf
   1017 		 */
   1018 		if (ifp->if_type == IFT_ISO88025) {
   1019 			struct token_header *trh;
   1020 
   1021 			trh = (struct token_header *)M_TRHSTART(m);
   1022 			if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
   1023 				struct token_rif	*rif;
   1024 				size_t	riflen;
   1025 
   1026 				rif = TOKEN_RIF(trh);
   1027 				riflen = (ntohs(rif->tr_rcf) &
   1028 				    TOKEN_RCF_LEN_MASK) >> 8;
   1029 
   1030 				if (riflen > 2 &&
   1031 				    riflen < sizeof(struct token_rif) &&
   1032 				    (riflen & 1) == 0) {
   1033 					rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
   1034 					rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
   1035 					bcopy(rif, TOKEN_RIF(la), riflen);
   1036 				}
   1037 			}
   1038 		}
   1039 #endif /* NTOKEN > 0 */
   1040 		bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
   1041 		    sdl->sdl_alen = ah->ar_hln);
   1042 		if (rt->rt_expire)
   1043 			rt->rt_expire = time.tv_sec + arpt_keep;
   1044 		rt->rt_flags &= ~RTF_REJECT;
   1045 		la->la_asked = 0;
   1046 
   1047 		s = splnet();
   1048 		mold = la->la_hold;
   1049 		la->la_hold = 0;
   1050 		splx(s);
   1051 
   1052 		if (mold) {
   1053 			arpstat.as_dfrsent++;
   1054 			(*ifp->if_output)(ifp, mold, rt_key(rt), rt);
   1055 		}
   1056 	}
   1057 reply:
   1058 	if (op != ARPOP_REQUEST) {
   1059 		if (op == ARPOP_REPLY)
   1060 			arpstat.as_rcvreply++;
   1061 	out:
   1062 		m_freem(m);
   1063 		return;
   1064 	}
   1065 	arpstat.as_rcvrequest++;
   1066 	if (in_hosteq(itaddr, myaddr)) {
   1067 		/* I am the target */
   1068 		if (ar_tha(ah))
   1069 			bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah),
   1070 			    ah->ar_hln);
   1071 		bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
   1072 	} else {
   1073 		la = arplookup(m, &itaddr, 0, SIN_PROXY);
   1074 		if (la == 0)
   1075 			goto out;
   1076 		rt = la->la_rt;
   1077 		if (ar_tha(ah))
   1078 			bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah),
   1079 			    ah->ar_hln);
   1080 		sdl = SDL(rt->rt_gateway);
   1081 		bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
   1082 	}
   1083 
   1084 	bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
   1085 	bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
   1086 	ah->ar_op = htons(ARPOP_REPLY);
   1087 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
   1088 	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
   1089 	m->m_len = sizeof(*ah) + (2 * ah->ar_pln) + (2 * ah->ar_hln);
   1090 	m->m_pkthdr.len = m->m_len;
   1091 	sa.sa_family = AF_ARP;
   1092 	sa.sa_len = 2;
   1093 	arpstat.as_sndtotal++;
   1094 	arpstat.as_sndreply++;
   1095 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
   1096 	return;
   1097 }
   1098 
   1099 /*
   1100  * Free an arp entry.
   1101  */
   1102 static void
   1103 arptfree(la)
   1104 	struct llinfo_arp *la;
   1105 {
   1106 	struct rtentry *rt = la->la_rt;
   1107 	struct sockaddr_dl *sdl;
   1108 
   1109 	ARP_LOCK_CHECK();
   1110 
   1111 	if (rt == 0)
   1112 		panic("arptfree");
   1113 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
   1114 	    sdl->sdl_family == AF_LINK) {
   1115 		sdl->sdl_alen = 0;
   1116 		la->la_asked = 0;
   1117 		rt->rt_flags &= ~RTF_REJECT;
   1118 		return;
   1119 	}
   1120 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
   1121 	    0, (struct rtentry **)0);
   1122 }
   1123 
   1124 /*
   1125  * Lookup or enter a new address in arptab.
   1126  */
   1127 static struct llinfo_arp *
   1128 arplookup(m, addr, create, proxy)
   1129 	struct mbuf *m;
   1130 	struct in_addr *addr;
   1131 	int create, proxy;
   1132 {
   1133 	struct arphdr *ah;
   1134 	struct ifnet *ifp = m->m_pkthdr.rcvif;
   1135 	struct rtentry *rt;
   1136 	static struct sockaddr_inarp sin;
   1137 	const char *why = 0;
   1138 
   1139 	ah = mtod(m, struct arphdr *);
   1140 	sin.sin_len = sizeof(sin);
   1141 	sin.sin_family = AF_INET;
   1142 	sin.sin_addr = *addr;
   1143 	sin.sin_other = proxy ? SIN_PROXY : 0;
   1144 	rt = rtalloc1(sintosa(&sin), create);
   1145 	if (rt == 0)
   1146 		return (0);
   1147 	rt->rt_refcnt--;
   1148 
   1149 	if (rt->rt_flags & RTF_GATEWAY)
   1150 		why = "host is not on local network";
   1151 	else if ((rt->rt_flags & RTF_LLINFO) == 0) {
   1152 		arpstat.as_allocfail++;
   1153 		why = "could not allocate llinfo";
   1154 	} else if (rt->rt_gateway->sa_family != AF_LINK)
   1155 		why = "gateway route is not ours";
   1156 	else
   1157 		return ((struct llinfo_arp *)rt->rt_llinfo);
   1158 
   1159 	if (create)
   1160 		log(LOG_DEBUG, "arplookup: unable to enter address"
   1161 		    " for %s@%s on %s (%s)\n",
   1162 		    in_fmtaddr(*addr), lla_snprintf(ar_sha(ah), ah->ar_hln),
   1163 		    ifp->if_xname, why);
   1164 	return (0);
   1165 }
   1166 
   1167 int
   1168 arpioctl(cmd, data)
   1169 	u_long cmd;
   1170 	caddr_t data;
   1171 {
   1172 
   1173 	return (EOPNOTSUPP);
   1174 }
   1175 
   1176 void
   1177 arp_ifinit(ifp, ifa)
   1178 	struct ifnet *ifp;
   1179 	struct ifaddr *ifa;
   1180 {
   1181 	struct in_addr *ip;
   1182 
   1183 	/*
   1184 	 * Warn the user if another station has this IP address,
   1185 	 * but only if the interface IP address is not zero.
   1186 	 */
   1187 	ip = &IA_SIN(ifa)->sin_addr;
   1188 	if (!in_nullhost(*ip))
   1189 		arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
   1190 
   1191 	ifa->ifa_rtrequest = arp_rtrequest;
   1192 	ifa->ifa_flags |= RTF_CLONING;
   1193 }
   1194 
   1195 /*
   1196  * Called from 10 Mb/s Ethernet interrupt handlers
   1197  * when ether packet type ETHERTYPE_REVARP
   1198  * is received.  Common length and type checks are done here,
   1199  * then the protocol-specific routine is called.
   1200  */
   1201 void
   1202 revarpinput(m)
   1203 	struct mbuf *m;
   1204 {
   1205 	struct arphdr *ar;
   1206 
   1207 	if (m->m_len < sizeof(struct arphdr))
   1208 		goto out;
   1209 	ar = mtod(m, struct arphdr *);
   1210 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
   1211 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
   1212 		goto out;
   1213 #endif
   1214 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
   1215 		goto out;
   1216 	switch (ntohs(ar->ar_pro)) {
   1217 	case ETHERTYPE_IP:
   1218 	case ETHERTYPE_IPTRAILERS:
   1219 		in_revarpinput(m);
   1220 		return;
   1221 
   1222 	default:
   1223 		break;
   1224 	}
   1225 out:
   1226 	m_freem(m);
   1227 }
   1228 
   1229 /*
   1230  * RARP for Internet protocols on 10 Mb/s Ethernet.
   1231  * Algorithm is that given in RFC 903.
   1232  * We are only using for bootstrap purposes to get an ip address for one of
   1233  * our interfaces.  Thus we support no user-interface.
   1234  *
   1235  * Since the contents of the RARP reply are specific to the interface that
   1236  * sent the request, this code must ensure that they are properly associated.
   1237  *
   1238  * Note: also supports ARP via RARP packets, per the RFC.
   1239  */
   1240 void
   1241 in_revarpinput(m)
   1242 	struct mbuf *m;
   1243 {
   1244 	struct ifnet *ifp;
   1245 	struct arphdr *ah;
   1246 	int op;
   1247 
   1248 	ah = mtod(m, struct arphdr *);
   1249 	op = ntohs(ah->ar_op);
   1250 
   1251 	switch (m->m_pkthdr.rcvif->if_type) {
   1252 	case IFT_IEEE1394:
   1253 		/* ARP without target hardware address is not supported */
   1254 		goto out;
   1255 	default:
   1256 		break;
   1257 	}
   1258 
   1259 	switch (op) {
   1260 	case ARPOP_REQUEST:
   1261 	case ARPOP_REPLY:	/* per RFC */
   1262 		in_arpinput(m);
   1263 		return;
   1264 	case ARPOP_REVREPLY:
   1265 		break;
   1266 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
   1267 	default:
   1268 		goto out;
   1269 	}
   1270 	if (!revarp_in_progress)
   1271 		goto out;
   1272 	ifp = m->m_pkthdr.rcvif;
   1273 	if (ifp != myip_ifp) /* !same interface */
   1274 		goto out;
   1275 	if (myip_initialized)
   1276 		goto wake;
   1277 	if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
   1278 		goto out;
   1279 	bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
   1280 	bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
   1281 	myip_initialized = 1;
   1282 wake:	/* Do wakeup every time in case it was missed. */
   1283 	wakeup((caddr_t)&myip);
   1284 
   1285 out:
   1286 	m_freem(m);
   1287 }
   1288 
   1289 /*
   1290  * Send a RARP request for the ip address of the specified interface.
   1291  * The request should be RFC 903-compliant.
   1292  */
   1293 void
   1294 revarprequest(ifp)
   1295 	struct ifnet *ifp;
   1296 {
   1297 	struct sockaddr sa;
   1298 	struct mbuf *m;
   1299 	struct arphdr *ah;
   1300 
   1301 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
   1302 		return;
   1303 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
   1304 	    2*ifp->if_data.ifi_addrlen;
   1305 	m->m_pkthdr.len = m->m_len;
   1306 	MH_ALIGN(m, m->m_len);
   1307 	ah = mtod(m, struct arphdr *);
   1308 	bzero((caddr_t)ah, m->m_len);
   1309 	ah->ar_pro = htons(ETHERTYPE_IP);
   1310 	ah->ar_hln = ifp->if_data.ifi_addrlen;	/* hardware address length */
   1311 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
   1312 	ah->ar_op = htons(ARPOP_REVREQUEST);
   1313 
   1314 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
   1315 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
   1316 
   1317 	sa.sa_family = AF_ARP;
   1318 	sa.sa_len = 2;
   1319 	m->m_flags |= M_BCAST;
   1320 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
   1321 
   1322 }
   1323 
   1324 /*
   1325  * RARP for the ip address of the specified interface, but also
   1326  * save the ip address of the server that sent the answer.
   1327  * Timeout if no response is received.
   1328  */
   1329 int
   1330 revarpwhoarewe(ifp, serv_in, clnt_in)
   1331 	struct ifnet *ifp;
   1332 	struct in_addr *serv_in;
   1333 	struct in_addr *clnt_in;
   1334 {
   1335 	int result, count = 20;
   1336 
   1337 	myip_initialized = 0;
   1338 	myip_ifp = ifp;
   1339 
   1340 	revarp_in_progress = 1;
   1341 	while (count--) {
   1342 		revarprequest(ifp);
   1343 		result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
   1344 		if (result != EWOULDBLOCK)
   1345 			break;
   1346 	}
   1347 	revarp_in_progress = 0;
   1348 
   1349 	if (!myip_initialized)
   1350 		return ENETUNREACH;
   1351 
   1352 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
   1353 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
   1354 	return 0;
   1355 }
   1356 
   1357 
   1358 
   1360 #ifdef DDB
   1361 
   1362 #include <machine/db_machdep.h>
   1363 #include <ddb/db_interface.h>
   1364 #include <ddb/db_output.h>
   1365 static void
   1366 db_print_sa(sa)
   1367 	struct sockaddr *sa;
   1368 {
   1369 	int len;
   1370 	u_char *p;
   1371 
   1372 	if (sa == 0) {
   1373 		db_printf("[NULL]");
   1374 		return;
   1375 	}
   1376 
   1377 	p = (u_char*)sa;
   1378 	len = sa->sa_len;
   1379 	db_printf("[");
   1380 	while (len > 0) {
   1381 		db_printf("%d", *p);
   1382 		p++; len--;
   1383 		if (len) db_printf(",");
   1384 	}
   1385 	db_printf("]\n");
   1386 }
   1387 static void
   1388 db_print_ifa(ifa)
   1389 	struct ifaddr *ifa;
   1390 {
   1391 	if (ifa == 0)
   1392 		return;
   1393 	db_printf("  ifa_addr=");
   1394 	db_print_sa(ifa->ifa_addr);
   1395 	db_printf("  ifa_dsta=");
   1396 	db_print_sa(ifa->ifa_dstaddr);
   1397 	db_printf("  ifa_mask=");
   1398 	db_print_sa(ifa->ifa_netmask);
   1399 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
   1400 			  ifa->ifa_flags,
   1401 			  ifa->ifa_refcnt,
   1402 			  ifa->ifa_metric);
   1403 }
   1404 static void
   1405 db_print_llinfo(li)
   1406 	caddr_t li;
   1407 {
   1408 	struct llinfo_arp *la;
   1409 
   1410 	if (li == 0)
   1411 		return;
   1412 	la = (struct llinfo_arp *)li;
   1413 	db_printf("  la_rt=%p la_hold=%p, la_asked=0x%lx\n",
   1414 			  la->la_rt, la->la_hold, la->la_asked);
   1415 }
   1416 /*
   1417  * Function to pass to rn_walktree().
   1418  * Return non-zero error to abort walk.
   1419  */
   1420 static int
   1421 db_show_radix_node(rn, w)
   1422 	struct radix_node *rn;
   1423 	void *w;
   1424 {
   1425 	struct rtentry *rt = (struct rtentry *)rn;
   1426 
   1427 	db_printf("rtentry=%p", rt);
   1428 
   1429 	db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
   1430 			  rt->rt_flags, rt->rt_refcnt,
   1431 			  rt->rt_use, rt->rt_expire);
   1432 
   1433 	db_printf(" key="); db_print_sa(rt_key(rt));
   1434 	db_printf(" mask="); db_print_sa(rt_mask(rt));
   1435 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
   1436 
   1437 	db_printf(" ifp=%p ", rt->rt_ifp);
   1438 	if (rt->rt_ifp)
   1439 		db_printf("(%s)", rt->rt_ifp->if_xname);
   1440 	else
   1441 		db_printf("(NULL)");
   1442 
   1443 	db_printf(" ifa=%p\n", rt->rt_ifa);
   1444 	db_print_ifa(rt->rt_ifa);
   1445 
   1446 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
   1447 
   1448 	db_printf(" gwroute=%p llinfo=%p\n",
   1449 			  rt->rt_gwroute, rt->rt_llinfo);
   1450 	db_print_llinfo(rt->rt_llinfo);
   1451 
   1452 	return (0);
   1453 }
   1454 /*
   1455  * Function to print all the route trees.
   1456  * Use this from ddb:  "show arptab"
   1457  */
   1458 void
   1459 db_show_arptab(addr, have_addr, count, modif)
   1460 	db_expr_t	addr;
   1461 	int		have_addr;
   1462 	db_expr_t	count;
   1463 	char *		modif;
   1464 {
   1465 	struct radix_node_head *rnh;
   1466 	rnh = rt_tables[AF_INET];
   1467 	db_printf("Route tree for AF_INET\n");
   1468 	if (rnh == NULL) {
   1469 		db_printf(" (not initialized)\n");
   1470 		return;
   1471 	}
   1472 	rn_walktree(rnh, db_show_radix_node, NULL);
   1473 	return;
   1474 }
   1475 #endif
   1476 #endif /* INET */
   1477