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