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