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