Home | History | Annotate | Line # | Download | only in netinet
if_arp.c revision 1.59
      1 /*	$NetBSD: if_arp.c,v 1.59 1999/05/23 20:21:51 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 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 "opt_ddb.h"
     82 #include "opt_inet.h"
     83 
     84 #ifdef INET
     85 
     86 #include <sys/param.h>
     87 #include <sys/systm.h>
     88 #include <sys/malloc.h>
     89 #include <sys/mbuf.h>
     90 #include <sys/socket.h>
     91 #include <sys/time.h>
     92 #include <sys/kernel.h>
     93 #include <sys/errno.h>
     94 #include <sys/ioctl.h>
     95 #include <sys/syslog.h>
     96 #include <sys/proc.h>
     97 
     98 #include <net/ethertypes.h>
     99 #include <net/if.h>
    100 #include <net/if_dl.h>
    101 #include <net/if_token.h>
    102 #include <net/if_types.h>
    103 #include <net/route.h>
    104 
    105 #include <netinet/in.h>
    106 #include <netinet/in_systm.h>
    107 #include <netinet/in_var.h>
    108 #include <netinet/ip.h>
    109 #include <netinet/if_inarp.h>
    110 
    111 #include "loop.h"
    112 #include "token.h"
    113 
    114 #define SIN(s) ((struct sockaddr_in *)s)
    115 #define SDL(s) ((struct sockaddr_dl *)s)
    116 #define SRP(s) ((struct sockaddr_inarp *)s)
    117 
    118 /*
    119  * ARP trailer negotiation.  Trailer protocol is not IP specific,
    120  * but ARP request/response use IP addresses.
    121  */
    122 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
    123 
    124 /* timer values */
    125 int	arpt_prune = (5*60*1);	/* walk list every 5 minutes */
    126 int	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
    127 int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
    128 #define	rt_expire rt_rmx.rmx_expire
    129 
    130 static	void arprequest __P((struct ifnet *,
    131 	    struct in_addr *, struct in_addr *, u_int8_t *));
    132 static	void arptfree __P((struct llinfo_arp *));
    133 static	void arptimer __P((void *));
    134 static	struct llinfo_arp *arplookup __P((struct in_addr *, int, int));
    135 static	void in_arpinput __P((struct mbuf *));
    136 
    137 #if NLOOP > 0
    138 extern	struct ifnet loif[NLOOP];
    139 #endif
    140 LIST_HEAD(, llinfo_arp) llinfo_arp;
    141 struct	ifqueue arpintrq = {0, 0, 0, 50};
    142 int	arp_inuse, arp_allocated, arp_intimer;
    143 int	arp_maxtries = 5;
    144 int	useloopback = 1;	/* use loopback interface for local traffic */
    145 int	arpinit_done = 0;
    146 
    147 /* revarp state */
    148 static struct	in_addr myip, srv_ip;
    149 static int	myip_initialized = 0;
    150 static int	revarp_in_progress = 0;
    151 static struct	ifnet *myip_ifp = NULL;
    152 
    153 #ifdef DDB
    154 static void db_print_sa __P((struct sockaddr *));
    155 static void db_print_ifa __P((struct ifaddr *));
    156 static void db_print_llinfo __P((caddr_t));
    157 static int db_show_radix_node __P((struct radix_node *, void *));
    158 #endif
    159 
    160 /*
    161  * this should be elsewhere.
    162  */
    163 
    164 static char *
    165 lla_snprintf __P((u_int8_t *, int));
    166 
    167 static char *
    168 lla_snprintf(adrp, len)
    169 	u_int8_t *adrp;
    170 	int len;
    171 {
    172 	static char buf[16*3];
    173 	static const char hexdigits[] = {
    174 	    '0','1','2','3','4','5','6','7',
    175 	    '8','9','a','b','c','d','e','f'
    176 	};
    177 
    178 	int i;
    179 	char *p;
    180 
    181 	p = buf;
    182 
    183 	*p++ = hexdigits[(*adrp)>>4];
    184 	*p++ = hexdigits[(*adrp++)&0xf];
    185 
    186 	for (i=1; i<len && i<16; i++) {
    187 		*p++ = ':';
    188 		*p++ = hexdigits[(*adrp)>>4];
    189 		*p++ = hexdigits[(*adrp++)&0xf];
    190 	}
    191 
    192 	*p = 0;
    193 	return buf;
    194 }
    195 
    196 /*
    197  * Timeout routine.  Age arp_tab entries periodically.
    198  */
    199 /* ARGSUSED */
    200 static void
    201 arptimer(arg)
    202 	void *arg;
    203 {
    204 	int s;
    205 	register struct llinfo_arp *la, *nla;
    206 
    207 	s = splsoftnet();
    208 	timeout(arptimer, NULL, arpt_prune * hz);
    209 	for (la = llinfo_arp.lh_first; la != 0; la = nla) {
    210 		register struct rtentry *rt = la->la_rt;
    211 
    212 		nla = la->la_list.le_next;
    213 		if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
    214 			arptfree(la); /* timer has expired; clear */
    215 	}
    216 	splx(s);
    217 }
    218 
    219 /*
    220  * Parallel to llc_rtrequest.
    221  */
    222 void
    223 arp_rtrequest(req, rt, sa)
    224 	int req;
    225 	register struct rtentry *rt;
    226 	struct sockaddr *sa;
    227 {
    228 	register struct sockaddr *gate = rt->rt_gateway;
    229 	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
    230 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
    231 	size_t allocsize;
    232 
    233 	if (!arpinit_done) {
    234 		arpinit_done = 1;
    235 		/*
    236 		 * We generate expiration times from time.tv_sec
    237 		 * so avoid accidently creating permanent routes.
    238 		 */
    239 		if (time.tv_sec == 0) {
    240 			time.tv_sec++;
    241 		}
    242 		timeout(arptimer, (caddr_t)0, hz);
    243 	}
    244 	if (rt->rt_flags & RTF_GATEWAY)
    245 		return;
    246 	switch (req) {
    247 
    248 	case RTM_ADD:
    249 		/*
    250 		 * XXX: If this is a manually added route to interface
    251 		 * such as older version of routed or gated might provide,
    252 		 * restore cloning bit.
    253 		 */
    254 		if ((rt->rt_flags & RTF_HOST) == 0 &&
    255 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
    256 			rt->rt_flags |= RTF_CLONING;
    257 		if (rt->rt_flags & RTF_CLONING) {
    258 			/*
    259 			 * Case 1: This route should come from a route to iface.
    260 			 */
    261 			rt_setgate(rt, rt_key(rt),
    262 					(struct sockaddr *)&null_sdl);
    263 			gate = rt->rt_gateway;
    264 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
    265 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
    266 			/*
    267 			 * Give this route an expiration time, even though
    268 			 * it's a "permanent" route, so that routes cloned
    269 			 * from it do not need their expiration time set.
    270 			 */
    271 			rt->rt_expire = time.tv_sec;
    272 			break;
    273 		}
    274 		/* Announce a new entry if requested. */
    275 		if (rt->rt_flags & RTF_ANNOUNCE)
    276 			arprequest(rt->rt_ifp,
    277 			    &SIN(rt_key(rt))->sin_addr,
    278 			    &SIN(rt_key(rt))->sin_addr,
    279 			    (u_char *)LLADDR(SDL(gate)));
    280 		/*FALLTHROUGH*/
    281 	case RTM_RESOLVE:
    282 		if (gate->sa_family != AF_LINK ||
    283 		    gate->sa_len < sizeof(null_sdl)) {
    284 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
    285 			break;
    286 		}
    287 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
    288 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
    289 		if (la != 0)
    290 			break; /* This happens on a route change */
    291 		/*
    292 		 * Case 2:  This route may come from cloning, or a manual route
    293 		 * add with a LL address.
    294 		 */
    295 		switch (SDL(gate)->sdl_type) {
    296 #if NTOKEN > 0
    297 		case IFT_ISO88025:
    298 			allocsize = sizeof(*la) + sizeof(struct token_rif);
    299 			break;
    300 #endif /* NTOKEN > 0 */
    301 		default:
    302 			allocsize = sizeof(*la);
    303 		}
    304 		R_Malloc(la, struct llinfo_arp *, allocsize);
    305 		rt->rt_llinfo = (caddr_t)la;
    306 		if (la == 0) {
    307 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
    308 			break;
    309 		}
    310 		arp_inuse++, arp_allocated++;
    311 		Bzero(la, allocsize);
    312 		la->la_rt = rt;
    313 		rt->rt_flags |= RTF_LLINFO;
    314 		LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
    315 		if (in_hosteq(SIN(rt_key(rt))->sin_addr,
    316 		    (IA_SIN(rt->rt_ifa))->sin_addr)) {
    317 			/*
    318 			 * This test used to be
    319 			 *	if (loif.if_flags & IFF_UP)
    320 			 * It allowed local traffic to be forced through
    321 			 * the hardware by configuring the loopback down.
    322 			 * However, it causes problems during network
    323 			 * configuration for boards that can't receive
    324 			 * packets they send.  It is now necessary to clear
    325 			 * "useloopback" and remove the route to force
    326 			 * traffic out to the hardware.
    327 			 */
    328 			rt->rt_expire = 0;
    329 			Bcopy(LLADDR(rt->rt_ifp->if_sadl),
    330 			    LLADDR(SDL(gate)),
    331 			    SDL(gate)->sdl_alen =
    332 			    rt->rt_ifp->if_data.ifi_addrlen);
    333 #if NLOOP > 0
    334 			if (useloopback)
    335 				rt->rt_ifp = &loif[0];
    336 #endif
    337 		}
    338 		break;
    339 
    340 	case RTM_DELETE:
    341 		if (la == 0)
    342 			break;
    343 		arp_inuse--;
    344 		LIST_REMOVE(la, la_list);
    345 		rt->rt_llinfo = 0;
    346 		rt->rt_flags &= ~RTF_LLINFO;
    347 		if (la->la_hold)
    348 			m_freem(la->la_hold);
    349 		Free((caddr_t)la);
    350 	}
    351 }
    352 
    353 /*
    354  * Broadcast an ARP request. Caller specifies:
    355  *	- arp header source ip address
    356  *	- arp header target ip address
    357  *	- arp header source ethernet address
    358  */
    359 static void
    360 arprequest(ifp, sip, tip, enaddr)
    361 	register struct ifnet *ifp;
    362 	register struct in_addr *sip, *tip;
    363 	register u_int8_t *enaddr;
    364 {
    365 	register struct mbuf *m;
    366 	struct arphdr *ah;
    367 	struct sockaddr sa;
    368 
    369 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    370 		return;
    371 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
    372 	    2*ifp->if_data.ifi_addrlen;
    373 	m->m_pkthdr.len = m->m_len;
    374 	MH_ALIGN(m, m->m_len);
    375 	ah = mtod(m, struct arphdr *);
    376 	bzero((caddr_t)ah, m->m_len);
    377 	ah->ar_pro = htons(ETHERTYPE_IP);
    378 	ah->ar_hln = ifp->if_data.ifi_addrlen;	/* hardware address length */
    379 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
    380 	ah->ar_op = htons(ARPOP_REQUEST);
    381 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
    382 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
    383 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
    384 	sa.sa_family = AF_ARP;
    385 	sa.sa_len = 2;
    386 	m->m_flags |= M_BCAST;
    387 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
    388 }
    389 
    390 /*
    391  * Resolve an IP address into an ethernet address.  If success,
    392  * desten is filled in.  If there is no entry in arptab,
    393  * set one up and broadcast a request for the IP address.
    394  * Hold onto this mbuf and resend it once the address
    395  * is finally resolved.  A return value of 1 indicates
    396  * that desten has been filled in and the packet should be sent
    397  * normally; a 0 return indicates that the packet has been
    398  * taken over here, either now or for later transmission.
    399  */
    400 int
    401 arpresolve(ifp, rt, m, dst, desten)
    402 	register struct ifnet *ifp;
    403 	register struct rtentry *rt;
    404 	struct mbuf *m;
    405 	register struct sockaddr *dst;
    406 	register u_char *desten;
    407 {
    408 	register struct llinfo_arp *la;
    409 	struct sockaddr_dl *sdl;
    410 
    411 	if (rt)
    412 		la = (struct llinfo_arp *)rt->rt_llinfo;
    413 	else {
    414 		if ((la = arplookup(&SIN(dst)->sin_addr, 1, 0)) != NULL)
    415 			rt = la->la_rt;
    416 	}
    417 	if (la == 0 || rt == 0) {
    418 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo\n");
    419 		m_freem(m);
    420 		return (0);
    421 	}
    422 	sdl = SDL(rt->rt_gateway);
    423 	/*
    424 	 * Check the address family and length is valid, the address
    425 	 * is resolved; otherwise, try to resolve.
    426 	 */
    427 	if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
    428 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
    429 		bcopy(LLADDR(sdl), desten,
    430 		    min(sdl->sdl_alen, ifp->if_data.ifi_addrlen));
    431 		return 1;
    432 	}
    433 	/*
    434 	 * There is an arptab entry, but no ethernet address
    435 	 * response yet.  Replace the held mbuf with this
    436 	 * latest one.
    437 	 */
    438 	if (la->la_hold)
    439 		m_freem(la->la_hold);
    440 	la->la_hold = m;
    441 	/*
    442 	 * Re-send the ARP request when appropriate.
    443 	 */
    444 #ifdef	DIAGNOSTIC
    445 	if (rt->rt_expire == 0) {
    446 		/* This should never happen. (Should it? -gwr) */
    447 		printf("arpresolve: unresolved and rt_expire == 0\n");
    448 		/* Set expiration time to now (expired). */
    449 		rt->rt_expire = time.tv_sec;
    450 	}
    451 #endif
    452 	if (rt->rt_expire) {
    453 		rt->rt_flags &= ~RTF_REJECT;
    454 		if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
    455 			rt->rt_expire = time.tv_sec;
    456 			if (la->la_asked++ < arp_maxtries)
    457 				arprequest(ifp,
    458 				    &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
    459 				    &SIN(dst)->sin_addr,
    460 				    LLADDR(ifp->if_sadl));
    461 			else {
    462 				rt->rt_flags |= RTF_REJECT;
    463 				rt->rt_expire += arpt_down;
    464 				la->la_asked = 0;
    465 			}
    466 		}
    467 	}
    468 	return (0);
    469 }
    470 
    471 /*
    472  * Common length and type checks are done here,
    473  * then the protocol-specific routine is called.
    474  */
    475 void
    476 arpintr()
    477 {
    478 	register struct mbuf *m;
    479 	register struct arphdr *ar;
    480 	int s;
    481 
    482 	while (arpintrq.ifq_head) {
    483 		s = splimp();
    484 		IF_DEQUEUE(&arpintrq, m);
    485 		splx(s);
    486 		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
    487 			panic("arpintr");
    488 
    489 		if (m->m_len >= sizeof(struct arphdr) &&
    490 		    (ar = mtod(m, struct arphdr *)) &&
    491 		    /* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
    492 		    m->m_len >=
    493 		      sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
    494 			switch (ntohs(ar->ar_pro)) {
    495 
    496 			case ETHERTYPE_IP:
    497 			case ETHERTYPE_IPTRAILERS:
    498 				in_arpinput(m);
    499 				continue;
    500 			}
    501 		m_freem(m);
    502 	}
    503 }
    504 
    505 /*
    506  * ARP for Internet protocols on 10 Mb/s Ethernet.
    507  * Algorithm is that given in RFC 826.
    508  * In addition, a sanity check is performed on the sender
    509  * protocol address, to catch impersonators.
    510  * We no longer handle negotiations for use of trailer protocol:
    511  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
    512  * along with IP replies if we wanted trailers sent to us,
    513  * and also sent them in response to IP replies.
    514  * This allowed either end to announce the desire to receive
    515  * trailer packets.
    516  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
    517  * but formerly didn't normally send requests.
    518  */
    519 static void
    520 in_arpinput(m)
    521 	struct mbuf *m;
    522 {
    523 	struct arphdr *ah;
    524 	register struct ifnet *ifp = m->m_pkthdr.rcvif;
    525 	register struct llinfo_arp *la = 0;
    526 	register struct rtentry *rt;
    527 	struct in_ifaddr *ia;
    528 	struct sockaddr_dl *sdl;
    529 	struct sockaddr sa;
    530 	struct in_addr isaddr, itaddr, myaddr;
    531 	int op;
    532 
    533 	ah = mtod(m, struct arphdr *);
    534 	op = ntohs(ah->ar_op);
    535 	bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr));
    536 	bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr));
    537 
    538 	/*
    539 	 * If the target IP address is zero, ignore the packet.
    540 	 * This prevents the code below from tring to answer
    541 	 * when we are using IP address zero (booting).
    542 	 */
    543 	if (in_nullhost(itaddr))
    544 		goto out;
    545 
    546 	/*
    547 	 * If the source IP address is zero, this is most likely a
    548 	 * confused host trying to use IP address zero. (Windoze?)
    549 	 * XXX: Should we bother trying to reply to these?
    550 	 */
    551 	if (in_nullhost(isaddr))
    552 		goto out;
    553 
    554 	/*
    555 	 * Search for a matching interface address
    556 	 * or any address on the interface to use
    557 	 * as a dummy address in the rest of this function
    558 	 */
    559 	INADDR_TO_IA(itaddr, ia);
    560 	while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
    561 		NEXT_IA_WITH_SAME_ADDR(ia);
    562 
    563 	if (ia == NULL) {
    564 		INADDR_TO_IA(isaddr, ia);
    565 		while ((ia != NULL) && ia->ia_ifp != m->m_pkthdr.rcvif)
    566 			NEXT_IA_WITH_SAME_ADDR(ia);
    567 
    568 		if (ia == NULL) {
    569 			IFP_TO_IA(ifp, ia);
    570 			if (ia == NULL)
    571 				goto out;
    572 		}
    573 	}
    574 
    575 	myaddr = ia->ia_addr.sin_addr;
    576 
    577 	if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
    578 	    ifp->if_data.ifi_addrlen))
    579 		goto out;	/* it's from me, ignore it. */
    580 
    581 	if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)ifp->if_broadcastaddr,
    582 	    ifp->if_data.ifi_addrlen)) {
    583 		log(LOG_ERR,
    584 		    "%s: arp: link address is broadcast for IP address %s!\n",
    585 		    ifp->if_xname, in_fmtaddr(isaddr));
    586 		goto out;
    587 	}
    588 
    589 	if (in_hosteq(isaddr, myaddr)) {
    590 		log(LOG_ERR,
    591 		   "duplicate IP address %s sent from link address %s\n",
    592 		   in_fmtaddr(isaddr), lla_snprintf(ar_sha(ah), ah->ar_hln));
    593 		itaddr = myaddr;
    594 		goto reply;
    595 	}
    596 	la = arplookup(&isaddr, in_hosteq(itaddr, myaddr), 0);
    597 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
    598 		if (sdl->sdl_alen &&
    599 		    bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen)) {
    600 			if (rt->rt_flags & RTF_STATIC) {
    601 				log(LOG_INFO,
    602 				    "%s tried to overwrite permanent arp info"
    603 				    " for %s\n",
    604 				    lla_snprintf(ar_sha(ah), ah->ar_hln),
    605 				    in_fmtaddr(isaddr));
    606 				goto out;
    607 			} else if (rt->rt_ifp != ifp) {
    608 				log(LOG_INFO,
    609 				    "%s on %s tried to overwrite "
    610 				    "arp info for %s on %s\n",
    611 				    lla_snprintf(ar_sha(ah), ah->ar_hln),
    612 				    ifp->if_xname, in_fmtaddr(isaddr),
    613 				    rt->rt_ifp->if_xname);
    614 				    goto out;
    615 			} else {
    616 				log(LOG_INFO,
    617 				    "arp info overwritten for %s by %s\n",
    618 				    in_fmtaddr(isaddr),
    619 				    lla_snprintf(ar_sha(ah), ah->ar_hln));
    620 			}
    621 		}
    622 		/*
    623 		 * sanity check for the address length.
    624 		 * XXX this does not work for protocols with variable address
    625 		 * length. -is
    626 		 */
    627 		if (sdl->sdl_alen &&
    628 		    sdl->sdl_alen != ah->ar_hln) {
    629 			log(LOG_WARNING,
    630 			    "arp from %s: new addr len %d, was %d",
    631 			    in_fmtaddr(isaddr), ah->ar_hln, sdl->sdl_alen);
    632 		}
    633 		if (ifp->if_data.ifi_addrlen != ah->ar_hln) {
    634 			log(LOG_WARNING,
    635 			    "arp from %s: addr len: new %d, i/f %d (ignored)",
    636 			    in_fmtaddr(isaddr), ah->ar_hln,
    637 			    ifp->if_data.ifi_addrlen);
    638 			goto reply;
    639 		}
    640 #if NTOKEN > 0
    641 		/*
    642 		 * XXX uses m_pktdat and assumes the complete answer including
    643 		 * XXX token-ring headers is in the same buf
    644 		 */
    645 		if (ifp->if_type == IFT_ISO88025 &&
    646 			m->m_pktdat[8] & TOKEN_RI_PRESENT) {
    647 			struct token_rif	*rif;
    648 			size_t	riflen;
    649 
    650 			rif = TOKEN_RIF((struct token_header *) m->m_pktdat);
    651 			riflen = (ntohs(rif->tr_rcf) & TOKEN_RCF_LEN_MASK) >> 8;
    652 
    653 			if (riflen > 2 && riflen < sizeof(struct token_rif) &&
    654 				(riflen & 1) == 0) {
    655 				rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
    656 				rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
    657 				bcopy(rif, TOKEN_RIF(la), riflen);
    658 			}
    659 		}
    660 #endif /* NTOKEN > 0 */
    661 		bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
    662 		    sdl->sdl_alen = ah->ar_hln);
    663 		if (rt->rt_expire)
    664 			rt->rt_expire = time.tv_sec + arpt_keep;
    665 		rt->rt_flags &= ~RTF_REJECT;
    666 		la->la_asked = 0;
    667 		if (la->la_hold) {
    668 			(*ifp->if_output)(ifp, la->la_hold,
    669 				rt_key(rt), rt);
    670 			la->la_hold = 0;
    671 		}
    672 	}
    673 reply:
    674 	if (op != ARPOP_REQUEST) {
    675 	out:
    676 		m_freem(m);
    677 		return;
    678 	}
    679 	if (in_hosteq(itaddr, myaddr)) {
    680 		/* I am the target */
    681 		bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
    682 		bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
    683 	} else {
    684 		la = arplookup(&itaddr, 0, SIN_PROXY);
    685 		if (la == 0)
    686 			goto out;
    687 		rt = la->la_rt;
    688 		bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
    689 		sdl = SDL(rt->rt_gateway);
    690 		bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
    691 	}
    692 
    693 	bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
    694 	bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
    695 	ah->ar_op = htons(ARPOP_REPLY);
    696 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
    697 	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
    698 	sa.sa_family = AF_ARP;
    699 	sa.sa_len = 2;
    700 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
    701 	return;
    702 }
    703 
    704 /*
    705  * Free an arp entry.
    706  */
    707 static void
    708 arptfree(la)
    709 	register struct llinfo_arp *la;
    710 {
    711 	register struct rtentry *rt = la->la_rt;
    712 	register struct sockaddr_dl *sdl;
    713 
    714 	if (rt == 0)
    715 		panic("arptfree");
    716 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
    717 	    sdl->sdl_family == AF_LINK) {
    718 		sdl->sdl_alen = 0;
    719 		la->la_asked = 0;
    720 		rt->rt_flags &= ~RTF_REJECT;
    721 		return;
    722 	}
    723 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
    724 	    0, (struct rtentry **)0);
    725 }
    726 
    727 /*
    728  * Lookup or enter a new address in arptab.
    729  */
    730 static struct llinfo_arp *
    731 arplookup(addr, create, proxy)
    732 	struct in_addr *addr;
    733 	int create, proxy;
    734 {
    735 	register struct rtentry *rt;
    736 	static struct sockaddr_inarp sin;
    737 	const char *why = 0;
    738 
    739 	sin.sin_len = sizeof(sin);
    740 	sin.sin_family = AF_INET;
    741 	sin.sin_addr = *addr;
    742 	sin.sin_other = proxy ? SIN_PROXY : 0;
    743 	rt = rtalloc1(sintosa(&sin), create);
    744 	if (rt == 0)
    745 		return (0);
    746 	rt->rt_refcnt--;
    747 
    748 	if (rt->rt_flags & RTF_GATEWAY)
    749 		why = "host is not on local network";
    750 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
    751 		why = "could not allocate llinfo";
    752 	else if (rt->rt_gateway->sa_family != AF_LINK)
    753 		why = "gateway route is not ours";
    754 	else
    755 		return ((struct llinfo_arp *)rt->rt_llinfo);
    756 
    757 	if (create)
    758 		log(LOG_DEBUG, "arplookup: unable to enter address"
    759 		    " for %s (%s)\n",
    760 		    in_fmtaddr(*addr), why);
    761 	return (0);
    762 }
    763 
    764 int
    765 arpioctl(cmd, data)
    766 	u_long cmd;
    767 	caddr_t data;
    768 {
    769 
    770 	return (EOPNOTSUPP);
    771 }
    772 
    773 void
    774 arp_ifinit(ifp, ifa)
    775 	struct ifnet *ifp;
    776 	struct ifaddr *ifa;
    777 {
    778 	struct in_addr *ip;
    779 
    780 	/*
    781 	 * Warn the user if another station has this IP address,
    782 	 * but only if the interface IP address is not zero.
    783 	 */
    784 	ip = &IA_SIN(ifa)->sin_addr;
    785 	if (!in_nullhost(*ip))
    786 		arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
    787 
    788 	ifa->ifa_rtrequest = arp_rtrequest;
    789 	ifa->ifa_flags |= RTF_CLONING;
    790 }
    791 
    792 /*
    793  * Called from 10 Mb/s Ethernet interrupt handlers
    794  * when ether packet type ETHERTYPE_REVARP
    795  * is received.  Common length and type checks are done here,
    796  * then the protocol-specific routine is called.
    797  */
    798 void
    799 revarpinput(m)
    800 	struct mbuf *m;
    801 {
    802 	struct arphdr *ar;
    803 
    804 	if (m->m_len < sizeof(struct arphdr))
    805 		goto out;
    806 	ar = mtod(m, struct arphdr *);
    807 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
    808 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
    809 		goto out;
    810 #endif
    811 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
    812 		goto out;
    813 	switch (ntohs(ar->ar_pro)) {
    814 
    815 	case ETHERTYPE_IP:
    816 	case ETHERTYPE_IPTRAILERS:
    817 		in_revarpinput(m);
    818 		return;
    819 
    820 	default:
    821 		break;
    822 	}
    823 out:
    824 	m_freem(m);
    825 }
    826 
    827 /*
    828  * RARP for Internet protocols on 10 Mb/s Ethernet.
    829  * Algorithm is that given in RFC 903.
    830  * We are only using for bootstrap purposes to get an ip address for one of
    831  * our interfaces.  Thus we support no user-interface.
    832  *
    833  * Since the contents of the RARP reply are specific to the interface that
    834  * sent the request, this code must ensure that they are properly associated.
    835  *
    836  * Note: also supports ARP via RARP packets, per the RFC.
    837  */
    838 void
    839 in_revarpinput(m)
    840 	struct mbuf *m;
    841 {
    842 	struct ifnet *ifp;
    843 	struct arphdr *ah;
    844 	int op;
    845 
    846 	ah = mtod(m, struct arphdr *);
    847 	op = ntohs(ah->ar_op);
    848 	switch (op) {
    849 	case ARPOP_REQUEST:
    850 	case ARPOP_REPLY:	/* per RFC */
    851 		in_arpinput(m);
    852 		return;
    853 	case ARPOP_REVREPLY:
    854 		break;
    855 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
    856 	default:
    857 		goto out;
    858 	}
    859 	if (!revarp_in_progress)
    860 		goto out;
    861 	ifp = m->m_pkthdr.rcvif;
    862 	if (ifp != myip_ifp) /* !same interface */
    863 		goto out;
    864 	if (myip_initialized)
    865 		goto wake;
    866 	if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
    867 		goto out;
    868 	bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
    869 	bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
    870 	myip_initialized = 1;
    871 wake:	/* Do wakeup every time in case it was missed. */
    872 	wakeup((caddr_t)&myip);
    873 
    874 out:
    875 	m_freem(m);
    876 }
    877 
    878 /*
    879  * Send a RARP request for the ip address of the specified interface.
    880  * The request should be RFC 903-compliant.
    881  */
    882 void
    883 revarprequest(ifp)
    884 	struct ifnet *ifp;
    885 {
    886 	struct sockaddr sa;
    887 	struct mbuf *m;
    888 	struct arphdr *ah;
    889 
    890 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    891 		return;
    892 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
    893 	    2*ifp->if_data.ifi_addrlen;
    894 	m->m_pkthdr.len = m->m_len;
    895 	MH_ALIGN(m, m->m_len);
    896 	ah = mtod(m, struct arphdr *);
    897 	bzero((caddr_t)ah, m->m_len);
    898 	ah->ar_pro = htons(ETHERTYPE_IP);
    899 	ah->ar_hln = ifp->if_data.ifi_addrlen;	/* hardware address length */
    900 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
    901 	ah->ar_op = htons(ARPOP_REVREQUEST);
    902 
    903 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
    904 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
    905 
    906 	sa.sa_family = AF_ARP;
    907 	sa.sa_len = 2;
    908 	m->m_flags |= M_BCAST;
    909 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
    910 
    911 }
    912 
    913 /*
    914  * RARP for the ip address of the specified interface, but also
    915  * save the ip address of the server that sent the answer.
    916  * Timeout if no response is received.
    917  */
    918 int
    919 revarpwhoarewe(ifp, serv_in, clnt_in)
    920 	struct ifnet *ifp;
    921 	struct in_addr *serv_in;
    922 	struct in_addr *clnt_in;
    923 {
    924 	int result, count = 20;
    925 
    926 	myip_initialized = 0;
    927 	myip_ifp = ifp;
    928 
    929 	revarp_in_progress = 1;
    930 	while (count--) {
    931 		revarprequest(ifp);
    932 		result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
    933 		if (result != EWOULDBLOCK)
    934 			break;
    935 	}
    936 	revarp_in_progress = 0;
    937 
    938 	if (!myip_initialized)
    939 		return ENETUNREACH;
    940 
    941 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
    942 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
    943 	return 0;
    944 }
    945 
    946 
    947 
    949 #ifdef DDB
    950 
    951 #include <machine/db_machdep.h>
    952 #include <ddb/db_interface.h>
    953 #include <ddb/db_output.h>
    954 static void
    955 db_print_sa(sa)
    956 	struct sockaddr *sa;
    957 {
    958 	int len;
    959 	u_char *p;
    960 
    961 	if (sa == 0) {
    962 		db_printf("[NULL]");
    963 		return;
    964 	}
    965 
    966 	p = (u_char*)sa;
    967 	len = sa->sa_len;
    968 	db_printf("[");
    969 	while (len > 0) {
    970 		db_printf("%d", *p);
    971 		p++; len--;
    972 		if (len) db_printf(",");
    973 	}
    974 	db_printf("]\n");
    975 }
    976 static void
    977 db_print_ifa(ifa)
    978 	struct ifaddr *ifa;
    979 {
    980 	if (ifa == 0)
    981 		return;
    982 	db_printf("  ifa_addr=");
    983 	db_print_sa(ifa->ifa_addr);
    984 	db_printf("  ifa_dsta=");
    985 	db_print_sa(ifa->ifa_dstaddr);
    986 	db_printf("  ifa_mask=");
    987 	db_print_sa(ifa->ifa_netmask);
    988 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
    989 			  ifa->ifa_flags,
    990 			  ifa->ifa_refcnt,
    991 			  ifa->ifa_metric);
    992 }
    993 static void
    994 db_print_llinfo(li)
    995 	caddr_t li;
    996 {
    997 	struct llinfo_arp *la;
    998 
    999 	if (li == 0)
   1000 		return;
   1001 	la = (struct llinfo_arp *)li;
   1002 	db_printf("  la_rt=%p la_hold=%p, la_asked=0x%lx\n",
   1003 			  la->la_rt, la->la_hold, la->la_asked);
   1004 }
   1005 /*
   1006  * Function to pass to rn_walktree().
   1007  * Return non-zero error to abort walk.
   1008  */
   1009 static int
   1010 db_show_radix_node(rn, w)
   1011 	struct radix_node *rn;
   1012 	void *w;
   1013 {
   1014 	struct rtentry *rt = (struct rtentry *)rn;
   1015 
   1016 	db_printf("rtentry=%p", rt);
   1017 
   1018 	db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
   1019 			  rt->rt_flags, rt->rt_refcnt,
   1020 			  rt->rt_use, rt->rt_expire);
   1021 
   1022 	db_printf(" key="); db_print_sa(rt_key(rt));
   1023 	db_printf(" mask="); db_print_sa(rt_mask(rt));
   1024 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
   1025 
   1026 	db_printf(" ifp=%p ", rt->rt_ifp);
   1027 	if (rt->rt_ifp)
   1028 		db_printf("(%s)", rt->rt_ifp->if_xname);
   1029 	else
   1030 		db_printf("(NULL)");
   1031 
   1032 	db_printf(" ifa=%p\n", rt->rt_ifa);
   1033 	db_print_ifa(rt->rt_ifa);
   1034 
   1035 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
   1036 
   1037 	db_printf(" gwroute=%p llinfo=%p\n",
   1038 			  rt->rt_gwroute, rt->rt_llinfo);
   1039 	db_print_llinfo(rt->rt_llinfo);
   1040 
   1041 	return (0);
   1042 }
   1043 /*
   1044  * Function to print all the route trees.
   1045  * Use this from ddb:  "call db_show_arptab"
   1046  */
   1047 int
   1048 db_show_arptab()
   1049 {
   1050 	struct radix_node_head *rnh;
   1051 	rnh = rt_tables[AF_INET];
   1052 	db_printf("Route tree for AF_INET\n");
   1053 	if (rnh == NULL) {
   1054 		db_printf(" (not initialized)\n");
   1055 		return (0);
   1056 	}
   1057 	rn_walktree(rnh, db_show_radix_node, NULL);
   1058 	return (0);
   1059 }
   1060 #endif
   1061 #endif /* INET */
   1062