Home | History | Annotate | Line # | Download | only in netinet
if_arp.c revision 1.61
      1 /*	$NetBSD: if_arp.c,v 1.61 1999/05/30 00:39:07 bad 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_data 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 			struct token_header *trh;
    647 
    648 			trh = (struct token_header *)M_TRHSTART(m);
    649 			if (trh->token_shost[0] & TOKEN_RI_PRESENT) {
    650 				struct token_rif	*rif;
    651 				size_t	riflen;
    652 
    653 				rif = TOKEN_RIF(trh);
    654 				riflen = (ntohs(rif->tr_rcf) &
    655 				    TOKEN_RCF_LEN_MASK) >> 8;
    656 
    657 				if (riflen > 2 &&
    658 				    riflen < sizeof(struct token_rif) &&
    659 				    (riflen & 1) == 0) {
    660 					rif->tr_rcf ^= htons(TOKEN_RCF_DIRECTION);
    661 					rif->tr_rcf &= htons(~TOKEN_RCF_BROADCAST_MASK);
    662 					bcopy(rif, TOKEN_RIF(la), riflen);
    663 				}
    664 			}
    665 		}
    666 #endif /* NTOKEN > 0 */
    667 		bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
    668 		    sdl->sdl_alen = ah->ar_hln);
    669 		if (rt->rt_expire)
    670 			rt->rt_expire = time.tv_sec + arpt_keep;
    671 		rt->rt_flags &= ~RTF_REJECT;
    672 		la->la_asked = 0;
    673 		if (la->la_hold) {
    674 			(*ifp->if_output)(ifp, la->la_hold,
    675 				rt_key(rt), rt);
    676 			la->la_hold = 0;
    677 		}
    678 	}
    679 reply:
    680 	if (op != ARPOP_REQUEST) {
    681 	out:
    682 		m_freem(m);
    683 		return;
    684 	}
    685 	if (in_hosteq(itaddr, myaddr)) {
    686 		/* I am the target */
    687 		bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
    688 		bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
    689 	} else {
    690 		la = arplookup(&itaddr, 0, SIN_PROXY);
    691 		if (la == 0)
    692 			goto out;
    693 		rt = la->la_rt;
    694 		bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
    695 		sdl = SDL(rt->rt_gateway);
    696 		bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
    697 	}
    698 
    699 	bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
    700 	bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
    701 	ah->ar_op = htons(ARPOP_REPLY);
    702 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
    703 	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
    704 	sa.sa_family = AF_ARP;
    705 	sa.sa_len = 2;
    706 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
    707 	return;
    708 }
    709 
    710 /*
    711  * Free an arp entry.
    712  */
    713 static void
    714 arptfree(la)
    715 	register struct llinfo_arp *la;
    716 {
    717 	register struct rtentry *rt = la->la_rt;
    718 	register struct sockaddr_dl *sdl;
    719 
    720 	if (rt == 0)
    721 		panic("arptfree");
    722 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
    723 	    sdl->sdl_family == AF_LINK) {
    724 		sdl->sdl_alen = 0;
    725 		la->la_asked = 0;
    726 		rt->rt_flags &= ~RTF_REJECT;
    727 		return;
    728 	}
    729 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
    730 	    0, (struct rtentry **)0);
    731 }
    732 
    733 /*
    734  * Lookup or enter a new address in arptab.
    735  */
    736 static struct llinfo_arp *
    737 arplookup(addr, create, proxy)
    738 	struct in_addr *addr;
    739 	int create, proxy;
    740 {
    741 	register struct rtentry *rt;
    742 	static struct sockaddr_inarp sin;
    743 	const char *why = 0;
    744 
    745 	sin.sin_len = sizeof(sin);
    746 	sin.sin_family = AF_INET;
    747 	sin.sin_addr = *addr;
    748 	sin.sin_other = proxy ? SIN_PROXY : 0;
    749 	rt = rtalloc1(sintosa(&sin), create);
    750 	if (rt == 0)
    751 		return (0);
    752 	rt->rt_refcnt--;
    753 
    754 	if (rt->rt_flags & RTF_GATEWAY)
    755 		why = "host is not on local network";
    756 	else if ((rt->rt_flags & RTF_LLINFO) == 0)
    757 		why = "could not allocate llinfo";
    758 	else if (rt->rt_gateway->sa_family != AF_LINK)
    759 		why = "gateway route is not ours";
    760 	else
    761 		return ((struct llinfo_arp *)rt->rt_llinfo);
    762 
    763 	if (create)
    764 		log(LOG_DEBUG, "arplookup: unable to enter address"
    765 		    " for %s (%s)\n",
    766 		    in_fmtaddr(*addr), why);
    767 	return (0);
    768 }
    769 
    770 int
    771 arpioctl(cmd, data)
    772 	u_long cmd;
    773 	caddr_t data;
    774 {
    775 
    776 	return (EOPNOTSUPP);
    777 }
    778 
    779 void
    780 arp_ifinit(ifp, ifa)
    781 	struct ifnet *ifp;
    782 	struct ifaddr *ifa;
    783 {
    784 	struct in_addr *ip;
    785 
    786 	/*
    787 	 * Warn the user if another station has this IP address,
    788 	 * but only if the interface IP address is not zero.
    789 	 */
    790 	ip = &IA_SIN(ifa)->sin_addr;
    791 	if (!in_nullhost(*ip))
    792 		arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
    793 
    794 	ifa->ifa_rtrequest = arp_rtrequest;
    795 	ifa->ifa_flags |= RTF_CLONING;
    796 }
    797 
    798 /*
    799  * Called from 10 Mb/s Ethernet interrupt handlers
    800  * when ether packet type ETHERTYPE_REVARP
    801  * is received.  Common length and type checks are done here,
    802  * then the protocol-specific routine is called.
    803  */
    804 void
    805 revarpinput(m)
    806 	struct mbuf *m;
    807 {
    808 	struct arphdr *ar;
    809 
    810 	if (m->m_len < sizeof(struct arphdr))
    811 		goto out;
    812 	ar = mtod(m, struct arphdr *);
    813 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
    814 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
    815 		goto out;
    816 #endif
    817 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
    818 		goto out;
    819 	switch (ntohs(ar->ar_pro)) {
    820 
    821 	case ETHERTYPE_IP:
    822 	case ETHERTYPE_IPTRAILERS:
    823 		in_revarpinput(m);
    824 		return;
    825 
    826 	default:
    827 		break;
    828 	}
    829 out:
    830 	m_freem(m);
    831 }
    832 
    833 /*
    834  * RARP for Internet protocols on 10 Mb/s Ethernet.
    835  * Algorithm is that given in RFC 903.
    836  * We are only using for bootstrap purposes to get an ip address for one of
    837  * our interfaces.  Thus we support no user-interface.
    838  *
    839  * Since the contents of the RARP reply are specific to the interface that
    840  * sent the request, this code must ensure that they are properly associated.
    841  *
    842  * Note: also supports ARP via RARP packets, per the RFC.
    843  */
    844 void
    845 in_revarpinput(m)
    846 	struct mbuf *m;
    847 {
    848 	struct ifnet *ifp;
    849 	struct arphdr *ah;
    850 	int op;
    851 
    852 	ah = mtod(m, struct arphdr *);
    853 	op = ntohs(ah->ar_op);
    854 	switch (op) {
    855 	case ARPOP_REQUEST:
    856 	case ARPOP_REPLY:	/* per RFC */
    857 		in_arpinput(m);
    858 		return;
    859 	case ARPOP_REVREPLY:
    860 		break;
    861 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
    862 	default:
    863 		goto out;
    864 	}
    865 	if (!revarp_in_progress)
    866 		goto out;
    867 	ifp = m->m_pkthdr.rcvif;
    868 	if (ifp != myip_ifp) /* !same interface */
    869 		goto out;
    870 	if (myip_initialized)
    871 		goto wake;
    872 	if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
    873 		goto out;
    874 	bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
    875 	bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
    876 	myip_initialized = 1;
    877 wake:	/* Do wakeup every time in case it was missed. */
    878 	wakeup((caddr_t)&myip);
    879 
    880 out:
    881 	m_freem(m);
    882 }
    883 
    884 /*
    885  * Send a RARP request for the ip address of the specified interface.
    886  * The request should be RFC 903-compliant.
    887  */
    888 void
    889 revarprequest(ifp)
    890 	struct ifnet *ifp;
    891 {
    892 	struct sockaddr sa;
    893 	struct mbuf *m;
    894 	struct arphdr *ah;
    895 
    896 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    897 		return;
    898 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
    899 	    2*ifp->if_data.ifi_addrlen;
    900 	m->m_pkthdr.len = m->m_len;
    901 	MH_ALIGN(m, m->m_len);
    902 	ah = mtod(m, struct arphdr *);
    903 	bzero((caddr_t)ah, m->m_len);
    904 	ah->ar_pro = htons(ETHERTYPE_IP);
    905 	ah->ar_hln = ifp->if_data.ifi_addrlen;	/* hardware address length */
    906 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
    907 	ah->ar_op = htons(ARPOP_REVREQUEST);
    908 
    909 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
    910 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
    911 
    912 	sa.sa_family = AF_ARP;
    913 	sa.sa_len = 2;
    914 	m->m_flags |= M_BCAST;
    915 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
    916 
    917 }
    918 
    919 /*
    920  * RARP for the ip address of the specified interface, but also
    921  * save the ip address of the server that sent the answer.
    922  * Timeout if no response is received.
    923  */
    924 int
    925 revarpwhoarewe(ifp, serv_in, clnt_in)
    926 	struct ifnet *ifp;
    927 	struct in_addr *serv_in;
    928 	struct in_addr *clnt_in;
    929 {
    930 	int result, count = 20;
    931 
    932 	myip_initialized = 0;
    933 	myip_ifp = ifp;
    934 
    935 	revarp_in_progress = 1;
    936 	while (count--) {
    937 		revarprequest(ifp);
    938 		result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
    939 		if (result != EWOULDBLOCK)
    940 			break;
    941 	}
    942 	revarp_in_progress = 0;
    943 
    944 	if (!myip_initialized)
    945 		return ENETUNREACH;
    946 
    947 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
    948 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
    949 	return 0;
    950 }
    951 
    952 
    953 
    955 #ifdef DDB
    956 
    957 #include <machine/db_machdep.h>
    958 #include <ddb/db_interface.h>
    959 #include <ddb/db_output.h>
    960 static void
    961 db_print_sa(sa)
    962 	struct sockaddr *sa;
    963 {
    964 	int len;
    965 	u_char *p;
    966 
    967 	if (sa == 0) {
    968 		db_printf("[NULL]");
    969 		return;
    970 	}
    971 
    972 	p = (u_char*)sa;
    973 	len = sa->sa_len;
    974 	db_printf("[");
    975 	while (len > 0) {
    976 		db_printf("%d", *p);
    977 		p++; len--;
    978 		if (len) db_printf(",");
    979 	}
    980 	db_printf("]\n");
    981 }
    982 static void
    983 db_print_ifa(ifa)
    984 	struct ifaddr *ifa;
    985 {
    986 	if (ifa == 0)
    987 		return;
    988 	db_printf("  ifa_addr=");
    989 	db_print_sa(ifa->ifa_addr);
    990 	db_printf("  ifa_dsta=");
    991 	db_print_sa(ifa->ifa_dstaddr);
    992 	db_printf("  ifa_mask=");
    993 	db_print_sa(ifa->ifa_netmask);
    994 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
    995 			  ifa->ifa_flags,
    996 			  ifa->ifa_refcnt,
    997 			  ifa->ifa_metric);
    998 }
    999 static void
   1000 db_print_llinfo(li)
   1001 	caddr_t li;
   1002 {
   1003 	struct llinfo_arp *la;
   1004 
   1005 	if (li == 0)
   1006 		return;
   1007 	la = (struct llinfo_arp *)li;
   1008 	db_printf("  la_rt=%p la_hold=%p, la_asked=0x%lx\n",
   1009 			  la->la_rt, la->la_hold, la->la_asked);
   1010 }
   1011 /*
   1012  * Function to pass to rn_walktree().
   1013  * Return non-zero error to abort walk.
   1014  */
   1015 static int
   1016 db_show_radix_node(rn, w)
   1017 	struct radix_node *rn;
   1018 	void *w;
   1019 {
   1020 	struct rtentry *rt = (struct rtentry *)rn;
   1021 
   1022 	db_printf("rtentry=%p", rt);
   1023 
   1024 	db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
   1025 			  rt->rt_flags, rt->rt_refcnt,
   1026 			  rt->rt_use, rt->rt_expire);
   1027 
   1028 	db_printf(" key="); db_print_sa(rt_key(rt));
   1029 	db_printf(" mask="); db_print_sa(rt_mask(rt));
   1030 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
   1031 
   1032 	db_printf(" ifp=%p ", rt->rt_ifp);
   1033 	if (rt->rt_ifp)
   1034 		db_printf("(%s)", rt->rt_ifp->if_xname);
   1035 	else
   1036 		db_printf("(NULL)");
   1037 
   1038 	db_printf(" ifa=%p\n", rt->rt_ifa);
   1039 	db_print_ifa(rt->rt_ifa);
   1040 
   1041 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
   1042 
   1043 	db_printf(" gwroute=%p llinfo=%p\n",
   1044 			  rt->rt_gwroute, rt->rt_llinfo);
   1045 	db_print_llinfo(rt->rt_llinfo);
   1046 
   1047 	return (0);
   1048 }
   1049 /*
   1050  * Function to print all the route trees.
   1051  * Use this from ddb:  "call db_show_arptab"
   1052  */
   1053 int
   1054 db_show_arptab()
   1055 {
   1056 	struct radix_node_head *rnh;
   1057 	rnh = rt_tables[AF_INET];
   1058 	db_printf("Route tree for AF_INET\n");
   1059 	if (rnh == NULL) {
   1060 		db_printf(" (not initialized)\n");
   1061 		return (0);
   1062 	}
   1063 	rn_walktree(rnh, db_show_radix_node, NULL);
   1064 	return (0);
   1065 }
   1066 #endif
   1067 #endif /* INET */
   1068