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