Home | History | Annotate | Line # | Download | only in netinet
if_arp.c revision 1.41
      1 /*	$NetBSD: if_arp.c,v 1.41 1997/10/02 19:42:02 is Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)if_ether.c	8.1 (Berkeley) 6/10/93
     36  */
     37 
     38 /*
     39  * Ethernet address resolution protocol.
     40  * TODO:
     41  *	add "inuse/lock" bit (or ref. count) along with valid bit
     42  */
     43 
     44 #ifdef INET
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/malloc.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/socket.h>
     51 #include <sys/time.h>
     52 #include <sys/kernel.h>
     53 #include <sys/errno.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/syslog.h>
     56 #include <sys/proc.h>
     57 
     58 #include <net/ethertypes.h>
     59 #include <net/if.h>
     60 #include <net/if_dl.h>
     61 #include <net/route.h>
     62 
     63 #include <netinet/in.h>
     64 #include <netinet/in_systm.h>
     65 #include <netinet/in_var.h>
     66 #include <netinet/ip.h>
     67 #include <netinet/if_inarp.h>
     68 
     69 #include "loop.h"
     70 
     71 #define SIN(s) ((struct sockaddr_in *)s)
     72 #define SDL(s) ((struct sockaddr_dl *)s)
     73 #define SRP(s) ((struct sockaddr_inarp *)s)
     74 
     75 /*
     76  * ARP trailer negotiation.  Trailer protocol is not IP specific,
     77  * but ARP request/response use IP addresses.
     78  */
     79 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
     80 
     81 /* timer values */
     82 int	arpt_prune = (5*60*1);	/* walk list every 5 minutes */
     83 int	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
     84 int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
     85 #define	rt_expire rt_rmx.rmx_expire
     86 
     87 static	void arprequest __P((struct ifnet *,
     88 	    struct in_addr *, struct in_addr *, u_int8_t *));
     89 static	void arptfree __P((struct llinfo_arp *));
     90 static	void arptimer __P((void *));
     91 static	struct llinfo_arp *arplookup __P((struct in_addr *, int, int));
     92 static	void in_arpinput __P((struct mbuf *));
     93 
     94 extern	struct ifnet loif[NLOOP];
     95 LIST_HEAD(, llinfo_arp) llinfo_arp;
     96 struct	ifqueue arpintrq = {0, 0, 0, 50};
     97 int	arp_inuse, arp_allocated, arp_intimer;
     98 int	arp_maxtries = 5;
     99 int	useloopback = 1;	/* use loopback interface for local traffic */
    100 int	arpinit_done = 0;
    101 
    102 /* revarp state */
    103 static struct	in_addr myip, srv_ip;
    104 static int	myip_initialized = 0;
    105 static int	revarp_in_progress = 0;
    106 static struct	ifnet *myip_ifp = NULL;
    107 
    108 #ifdef DDB
    109 static void db_print_sa __P((struct sockaddr *));
    110 static void db_print_ifa __P((struct ifaddr *));
    111 static void db_print_llinfo __P((caddr_t));
    112 static int db_show_radix_node __P((struct radix_node *, void *));
    113 #endif
    114 
    115 /*
    116  * this should be elsewhere.
    117  */
    118 
    119 static char *
    120 lla_snprintf __P((u_int8_t *, int));
    121 
    122 static char *
    123 lla_snprintf(adrp, len)
    124 	u_int8_t *adrp;
    125 	int len;
    126 {
    127 	static char buf[16*3];
    128 	static const char hexdigits[] = {
    129 	    '0','1','2','3','4','5','6','7',
    130 	    '8','9','a','b','c','d','e','f'
    131 	};
    132 
    133 	int i;
    134 	char *p;
    135 
    136 	p = buf;
    137 
    138 	*p++ = hexdigits[(*adrp)>>4];
    139 	*p++ = hexdigits[(*adrp++)&0xf];
    140 
    141 	for (i=1; i<len && i<16; i++) {
    142 		*p++ = ':';
    143 		*p++ = hexdigits[(*adrp)>>4];
    144 		*p++ = hexdigits[(*adrp++)&0xf];
    145 	}
    146 
    147 	*p = 0;
    148 	return buf;
    149 }
    150 
    151 /*
    152  * Timeout routine.  Age arp_tab entries periodically.
    153  */
    154 /* ARGSUSED */
    155 static void
    156 arptimer(arg)
    157 	void *arg;
    158 {
    159 	int s;
    160 	register struct llinfo_arp *la, *nla;
    161 
    162 	s = splsoftnet();
    163 	timeout(arptimer, NULL, arpt_prune * hz);
    164 	for (la = llinfo_arp.lh_first; la != 0; la = nla) {
    165 		register struct rtentry *rt = la->la_rt;
    166 
    167 		nla = la->la_list.le_next;
    168 		if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
    169 			arptfree(la); /* timer has expired; clear */
    170 	}
    171 	splx(s);
    172 }
    173 
    174 /*
    175  * Parallel to llc_rtrequest.
    176  */
    177 void
    178 arp_rtrequest(req, rt, sa)
    179 	int req;
    180 	register struct rtentry *rt;
    181 	struct sockaddr *sa;
    182 {
    183 	register struct sockaddr *gate = rt->rt_gateway;
    184 	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
    185 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
    186 
    187 	if (!arpinit_done) {
    188 		arpinit_done = 1;
    189 		/*
    190 		 * We generate expiration times from time.tv_sec
    191 		 * so avoid accidently creating permanent routes.
    192 		 */
    193 		if (time.tv_sec == 0) {
    194 			time.tv_sec++;
    195 		}
    196 		timeout(arptimer, (caddr_t)0, hz);
    197 	}
    198 	if (rt->rt_flags & RTF_GATEWAY)
    199 		return;
    200 	switch (req) {
    201 
    202 	case RTM_ADD:
    203 		/*
    204 		 * XXX: If this is a manually added route to interface
    205 		 * such as older version of routed or gated might provide,
    206 		 * restore cloning bit.
    207 		 */
    208 		if ((rt->rt_flags & RTF_HOST) == 0 &&
    209 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
    210 			rt->rt_flags |= RTF_CLONING;
    211 		if (rt->rt_flags & RTF_CLONING) {
    212 			/*
    213 			 * Case 1: This route should come from a route to iface.
    214 			 */
    215 			rt_setgate(rt, rt_key(rt),
    216 					(struct sockaddr *)&null_sdl);
    217 			gate = rt->rt_gateway;
    218 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
    219 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
    220 			/*
    221 			 * Give this route an expiration time, even though
    222 			 * it's a "permanent" route, so that routes cloned
    223 			 * from it do not need their expiration time set.
    224 			 */
    225 			rt->rt_expire = time.tv_sec;
    226 			break;
    227 		}
    228 		/* Announce a new entry if requested. */
    229 		if (rt->rt_flags & RTF_ANNOUNCE)
    230 			arprequest(rt->rt_ifp,
    231 			    &SIN(rt_key(rt))->sin_addr,
    232 			    &SIN(rt_key(rt))->sin_addr,
    233 			    (u_char *)LLADDR(SDL(gate)));
    234 		/*FALLTHROUGH*/
    235 	case RTM_RESOLVE:
    236 		if (gate->sa_family != AF_LINK ||
    237 		    gate->sa_len < sizeof(null_sdl)) {
    238 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value\n");
    239 			break;
    240 		}
    241 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
    242 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
    243 		if (la != 0)
    244 			break; /* This happens on a route change */
    245 		/*
    246 		 * Case 2:  This route may come from cloning, or a manual route
    247 		 * add with a LL address.
    248 		 */
    249 		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
    250 		rt->rt_llinfo = (caddr_t)la;
    251 		if (la == 0) {
    252 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
    253 			break;
    254 		}
    255 		arp_inuse++, arp_allocated++;
    256 		Bzero(la, sizeof(*la));
    257 		la->la_rt = rt;
    258 		rt->rt_flags |= RTF_LLINFO;
    259 		LIST_INSERT_HEAD(&llinfo_arp, la, la_list);
    260 		if (in_hosteq(SIN(rt_key(rt))->sin_addr,
    261 		    (IA_SIN(rt->rt_ifa))->sin_addr)) {
    262 			/*
    263 			 * This test used to be
    264 			 *	if (loif.if_flags & IFF_UP)
    265 			 * It allowed local traffic to be forced through
    266 			 * the hardware by configuring the loopback down.
    267 			 * However, it causes problems during network
    268 			 * configuration for boards that can't receive
    269 			 * packets they send.  It is now necessary to clear
    270 			 * "useloopback" and remove the route to force
    271 			 * traffic out to the hardware.
    272 			 */
    273 			rt->rt_expire = 0;
    274 			Bcopy(LLADDR(rt->rt_ifp->if_sadl),
    275 			    LLADDR(SDL(gate)),
    276 			    SDL(gate)->sdl_alen =
    277 			    rt->rt_ifp->if_data.ifi_addrlen);
    278 			if (useloopback)
    279 				rt->rt_ifp = &loif[0];
    280 		}
    281 		break;
    282 
    283 	case RTM_DELETE:
    284 		if (la == 0)
    285 			break;
    286 		arp_inuse--;
    287 		LIST_REMOVE(la, la_list);
    288 		rt->rt_llinfo = 0;
    289 		rt->rt_flags &= ~RTF_LLINFO;
    290 		if (la->la_hold)
    291 			m_freem(la->la_hold);
    292 		Free((caddr_t)la);
    293 	}
    294 }
    295 
    296 /*
    297  * Broadcast an ARP request. Caller specifies:
    298  *	- arp header source ip address
    299  *	- arp header target ip address
    300  *	- arp header source ethernet address
    301  */
    302 static void
    303 arprequest(ifp, sip, tip, enaddr)
    304 	register struct ifnet *ifp;
    305 	register struct in_addr *sip, *tip;
    306 	register u_int8_t *enaddr;
    307 {
    308 	register struct mbuf *m;
    309 	struct arphdr *ah;
    310 	struct sockaddr sa;
    311 
    312 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    313 		return;
    314 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
    315 	    2*ifp->if_data.ifi_addrlen;
    316 	m->m_pkthdr.len = m->m_len;
    317 	MH_ALIGN(m, m->m_len);
    318 	ah = mtod(m, struct arphdr *);
    319 	bzero((caddr_t)ah, m->m_len);
    320 	ah->ar_pro = htons(ETHERTYPE_IP);
    321 	ah->ar_hln = ifp->if_data.ifi_addrlen;	/* hardware address length */
    322 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
    323 	ah->ar_op = htons(ARPOP_REQUEST);
    324 	bcopy((caddr_t)enaddr, (caddr_t)ar_sha(ah), ah->ar_hln);
    325 	bcopy((caddr_t)sip, (caddr_t)ar_spa(ah), ah->ar_pln);
    326 	bcopy((caddr_t)tip, (caddr_t)ar_tpa(ah), ah->ar_pln);
    327 	sa.sa_family = AF_ARP;
    328 	sa.sa_len = 2;
    329 	m->m_flags |= M_BCAST;
    330 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
    331 }
    332 
    333 /*
    334  * Resolve an IP address into an ethernet address.  If success,
    335  * desten is filled in.  If there is no entry in arptab,
    336  * set one up and broadcast a request for the IP address.
    337  * Hold onto this mbuf and resend it once the address
    338  * is finally resolved.  A return value of 1 indicates
    339  * that desten has been filled in and the packet should be sent
    340  * normally; a 0 return indicates that the packet has been
    341  * taken over here, either now or for later transmission.
    342  */
    343 int
    344 arpresolve(ifp, rt, m, dst, desten)
    345 	register struct ifnet *ifp;
    346 	register struct rtentry *rt;
    347 	struct mbuf *m;
    348 	register struct sockaddr *dst;
    349 	register u_char *desten;
    350 {
    351 	register struct llinfo_arp *la;
    352 	struct sockaddr_dl *sdl;
    353 
    354 	if (rt)
    355 		la = (struct llinfo_arp *)rt->rt_llinfo;
    356 	else {
    357 		if ((la = arplookup(&SIN(dst)->sin_addr, 1, 0)) != NULL)
    358 			rt = la->la_rt;
    359 	}
    360 	if (la == 0 || rt == 0) {
    361 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo\n");
    362 		m_freem(m);
    363 		return (0);
    364 	}
    365 	sdl = SDL(rt->rt_gateway);
    366 	/*
    367 	 * Check the address family and length is valid, the address
    368 	 * is resolved; otherwise, try to resolve.
    369 	 */
    370 	if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
    371 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
    372 		bcopy(LLADDR(sdl), desten,
    373 		    min(sdl->sdl_alen, ifp->if_data.ifi_addrlen));
    374 		return 1;
    375 	}
    376 	/*
    377 	 * There is an arptab entry, but no ethernet address
    378 	 * response yet.  Replace the held mbuf with this
    379 	 * latest one.
    380 	 */
    381 	if (la->la_hold)
    382 		m_freem(la->la_hold);
    383 	la->la_hold = m;
    384 	/*
    385 	 * Re-send the ARP request when appropriate.
    386 	 */
    387 #ifdef	DIAGNOSTIC
    388 	if (rt->rt_expire == 0) {
    389 		/* This should never happen. (Should it? -gwr) */
    390 		printf("arpresolve: unresolved and rt_expire == 0\n");
    391 		/* Set expiration time to now (expired). */
    392 		rt->rt_expire = time.tv_sec;
    393 	}
    394 #endif
    395 	if (rt->rt_expire) {
    396 		rt->rt_flags &= ~RTF_REJECT;
    397 		if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
    398 			rt->rt_expire = time.tv_sec;
    399 			if (la->la_asked++ < arp_maxtries)
    400 				arprequest(ifp,
    401 				    &SIN(rt->rt_ifa->ifa_addr)->sin_addr,
    402 				    &SIN(dst)->sin_addr,
    403 				    LLADDR(ifp->if_sadl));
    404 			else {
    405 				rt->rt_flags |= RTF_REJECT;
    406 				rt->rt_expire += arpt_down;
    407 				la->la_asked = 0;
    408 			}
    409 		}
    410 	}
    411 	return (0);
    412 }
    413 
    414 /*
    415  * Common length and type checks are done here,
    416  * then the protocol-specific routine is called.
    417  */
    418 void
    419 arpintr()
    420 {
    421 	register struct mbuf *m;
    422 	register struct arphdr *ar;
    423 	int s;
    424 
    425 	while (arpintrq.ifq_head) {
    426 		s = splimp();
    427 		IF_DEQUEUE(&arpintrq, m);
    428 		splx(s);
    429 		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
    430 			panic("arpintr");
    431 
    432 		if (m->m_len >= sizeof(struct arphdr) &&
    433 		    (ar = mtod(m, struct arphdr *)) &&
    434 		    /* XXX ntohs(ar->ar_hrd) == ARPHRD_ETHER && */
    435 		    m->m_len >=
    436 		      sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
    437 			switch (ntohs(ar->ar_pro)) {
    438 
    439 			case ETHERTYPE_IP:
    440 			case ETHERTYPE_IPTRAILERS:
    441 				in_arpinput(m);
    442 				continue;
    443 			}
    444 		m_freem(m);
    445 	}
    446 }
    447 
    448 /*
    449  * ARP for Internet protocols on 10 Mb/s Ethernet.
    450  * Algorithm is that given in RFC 826.
    451  * In addition, a sanity check is performed on the sender
    452  * protocol address, to catch impersonators.
    453  * We no longer handle negotiations for use of trailer protocol:
    454  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
    455  * along with IP replies if we wanted trailers sent to us,
    456  * and also sent them in response to IP replies.
    457  * This allowed either end to announce the desire to receive
    458  * trailer packets.
    459  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
    460  * but formerly didn't normally send requests.
    461  */
    462 static void
    463 in_arpinput(m)
    464 	struct mbuf *m;
    465 {
    466 	struct arphdr *ah;
    467 	register struct ifnet *ifp = m->m_pkthdr.rcvif;
    468 	register struct llinfo_arp *la = 0;
    469 	register struct rtentry *rt;
    470 	struct in_ifaddr *ia, *maybe_ia = 0;
    471 	struct sockaddr_dl *sdl;
    472 	struct sockaddr sa;
    473 	struct in_addr isaddr, itaddr, myaddr;
    474 	int op;
    475 
    476 	ah = mtod(m, struct arphdr *);
    477 	op = ntohs(ah->ar_op);
    478 	bcopy((caddr_t)ar_spa(ah), (caddr_t)&isaddr, sizeof (isaddr));
    479 	bcopy((caddr_t)ar_tpa(ah), (caddr_t)&itaddr, sizeof (itaddr));
    480 
    481 	/*
    482 	 * If either the source or destination IP address is zero, then
    483 	 * the packet is bogus and we can just ignore it.  If we try to
    484 	 * process such bogus packets normally, they cause odd problems,
    485 	 * especially when we are using IP address zero (diskless boot).
    486 	 */
    487 	if (in_nullhost(isaddr) || in_nullhost(itaddr)) {
    488 		/* On some networks, this happens a lot! (stupid PCs...) */
    489 		log(LOG_DEBUG, "arp: zero IP addr from link address %s\n",
    490 		    lla_snprintf(ar_sha(ah), ah->ar_hln));
    491 		goto out;
    492 	}
    493 
    494 	/* Search for a matching interface address. */
    495 	for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
    496 		if (ia->ia_ifp == ifp) {
    497 			maybe_ia = ia;
    498 			if (in_hosteq(itaddr, ia->ia_addr.sin_addr) ||
    499 			    in_hosteq(isaddr, ia->ia_addr.sin_addr))
    500 				break;
    501 		}
    502 	if (maybe_ia == 0)
    503 		goto out;
    504 
    505 	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
    506 	if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
    507 	    ifp->if_data.ifi_addrlen))
    508 		goto out;	/* it's from me, ignore it. */
    509 
    510 	if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)ifp->if_broadcastaddr,
    511 	    ifp->if_data.ifi_addrlen)) {
    512 		log(LOG_ERR,
    513 		    "%s: arp: link address is broadcast for IP address %x!\n",
    514 		    ifp->if_xname, ntohl(isaddr.s_addr));
    515 		goto out;
    516 	}
    517 
    518 	if (in_hosteq(isaddr, myaddr)) {
    519 		log(LOG_ERR,
    520 		   "duplicate IP address %08x sent from link address %s\n",
    521 		   ntohl(isaddr.s_addr), lla_snprintf(ar_sha(ah), ah->ar_hln));
    522 		itaddr = myaddr;
    523 		goto reply;
    524 	}
    525 	la = arplookup(&isaddr, in_hosteq(itaddr, myaddr), 0);
    526 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
    527 		if (sdl->sdl_alen &&
    528 		    bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen))
    529 			log(LOG_INFO, "arp info overwritten for %08x by %s\n",
    530 			    ntohl(isaddr.s_addr),
    531 			    lla_snprintf(ar_sha(ah), ah->ar_hln));
    532 		/*
    533 		 * sanity check for the address length.
    534 		 * XXX this does not work for protocols with variable address
    535 		 * length. -is
    536 		 */
    537 		if (sdl->sdl_alen &&
    538 		    sdl->sdl_alen != ah->ar_hln) {
    539 			log(LOG_WARNING,
    540 			    "arp from %08x: new addr len %d, was %d",
    541 			    ntohl(isaddr.s_addr), ah->ar_hln, sdl->sdl_alen);
    542 		}
    543 		if (ifp->if_data.ifi_addrlen != ah->ar_hln) {
    544 			log(LOG_WARNING,
    545 			    "arp from %08x: addr len: new %d, i/f %d (ignored)",
    546 			    ntohl(isaddr.s_addr), ah->ar_hln,
    547 			    ifp->if_data.ifi_addrlen);
    548 			goto reply;
    549 		}
    550 		bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
    551 		    sdl->sdl_alen = ah->ar_hln);
    552 		if (rt->rt_expire)
    553 			rt->rt_expire = time.tv_sec + arpt_keep;
    554 		rt->rt_flags &= ~RTF_REJECT;
    555 		la->la_asked = 0;
    556 		if (la->la_hold) {
    557 			(*ifp->if_output)(ifp, la->la_hold,
    558 				rt_key(rt), rt);
    559 			la->la_hold = 0;
    560 		}
    561 	}
    562 reply:
    563 	if (op != ARPOP_REQUEST) {
    564 	out:
    565 		m_freem(m);
    566 		return;
    567 	}
    568 	if (in_hosteq(itaddr, myaddr)) {
    569 		/* I am the target */
    570 		bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
    571 		bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
    572 	} else {
    573 		la = arplookup(&itaddr, 0, SIN_PROXY);
    574 		if (la == 0)
    575 			goto out;
    576 		rt = la->la_rt;
    577 		bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
    578 		sdl = SDL(rt->rt_gateway);
    579 		bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
    580 	}
    581 
    582 	bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
    583 	bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
    584 	ah->ar_op = htons(ARPOP_REPLY);
    585 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
    586 	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
    587 	sa.sa_family = AF_ARP;
    588 	sa.sa_len = 2;
    589 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
    590 	return;
    591 }
    592 
    593 /*
    594  * Free an arp entry.
    595  */
    596 static void
    597 arptfree(la)
    598 	register struct llinfo_arp *la;
    599 {
    600 	register struct rtentry *rt = la->la_rt;
    601 	register struct sockaddr_dl *sdl;
    602 
    603 	if (rt == 0)
    604 		panic("arptfree");
    605 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
    606 	    sdl->sdl_family == AF_LINK) {
    607 		sdl->sdl_alen = 0;
    608 		la->la_asked = 0;
    609 		rt->rt_flags &= ~RTF_REJECT;
    610 		return;
    611 	}
    612 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
    613 	    0, (struct rtentry **)0);
    614 }
    615 
    616 /*
    617  * Lookup or enter a new address in arptab.
    618  */
    619 static struct llinfo_arp *
    620 arplookup(addr, create, proxy)
    621 	struct in_addr *addr;
    622 	int create, proxy;
    623 {
    624 	register struct rtentry *rt;
    625 	static struct sockaddr_inarp sin;
    626 
    627 	sin.sin_len = sizeof(sin);
    628 	sin.sin_family = AF_INET;
    629 	sin.sin_addr = *addr;
    630 	sin.sin_other = proxy ? SIN_PROXY : 0;
    631 	rt = rtalloc1(sintosa(&sin), create);
    632 	if (rt == 0)
    633 		return (0);
    634 	rt->rt_refcnt--;
    635 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
    636 	    rt->rt_gateway->sa_family != AF_LINK) {
    637 		if (create)
    638 			log(LOG_DEBUG, "arplookup: unable to enter address for %x\n",
    639 			    ntohl(addr->s_addr));
    640 		return (0);
    641 	}
    642 	return ((struct llinfo_arp *)rt->rt_llinfo);
    643 }
    644 
    645 int
    646 arpioctl(cmd, data)
    647 	u_long cmd;
    648 	caddr_t data;
    649 {
    650 
    651 	return (EOPNOTSUPP);
    652 }
    653 
    654 void
    655 arp_ifinit(ifp, ifa)
    656 	struct ifnet *ifp;
    657 	struct ifaddr *ifa;
    658 {
    659 	struct in_addr *ip;
    660 
    661 	/*
    662 	 * Warn the user if another station has this IP address,
    663 	 * but only if the interface IP address is not zero.
    664 	 */
    665 	ip = &IA_SIN(ifa)->sin_addr;
    666 	if (!in_nullhost(*ip))
    667 		arprequest(ifp, ip, ip, LLADDR(ifp->if_sadl));
    668 
    669 	ifa->ifa_rtrequest = arp_rtrequest;
    670 	ifa->ifa_flags |= RTF_CLONING;
    671 }
    672 
    673 /*
    674  * Called from 10 Mb/s Ethernet interrupt handlers
    675  * when ether packet type ETHERTYPE_REVARP
    676  * is received.  Common length and type checks are done here,
    677  * then the protocol-specific routine is called.
    678  */
    679 void
    680 revarpinput(m)
    681 	struct mbuf *m;
    682 {
    683 	struct arphdr *ar;
    684 
    685 	if (m->m_len < sizeof(struct arphdr))
    686 		goto out;
    687 	ar = mtod(m, struct arphdr *);
    688 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
    689 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
    690 		goto out;
    691 #endif
    692 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
    693 		goto out;
    694 	switch (ntohs(ar->ar_pro)) {
    695 
    696 	case ETHERTYPE_IP:
    697 	case ETHERTYPE_IPTRAILERS:
    698 		in_revarpinput(m);
    699 		return;
    700 
    701 	default:
    702 		break;
    703 	}
    704 out:
    705 	m_freem(m);
    706 }
    707 
    708 /*
    709  * RARP for Internet protocols on 10 Mb/s Ethernet.
    710  * Algorithm is that given in RFC 903.
    711  * We are only using for bootstrap purposes to get an ip address for one of
    712  * our interfaces.  Thus we support no user-interface.
    713  *
    714  * Since the contents of the RARP reply are specific to the interface that
    715  * sent the request, this code must ensure that they are properly associated.
    716  *
    717  * Note: also supports ARP via RARP packets, per the RFC.
    718  */
    719 void
    720 in_revarpinput(m)
    721 	struct mbuf *m;
    722 {
    723 	struct ifnet *ifp;
    724 	struct arphdr *ah;
    725 	int op;
    726 
    727 	ah = mtod(m, struct arphdr *);
    728 	op = ntohs(ah->ar_op);
    729 	switch (op) {
    730 	case ARPOP_REQUEST:
    731 	case ARPOP_REPLY:	/* per RFC */
    732 		in_arpinput(m);
    733 		return;
    734 	case ARPOP_REVREPLY:
    735 		break;
    736 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
    737 	default:
    738 		goto out;
    739 	}
    740 	if (!revarp_in_progress)
    741 		goto out;
    742 	ifp = m->m_pkthdr.rcvif;
    743 	if (ifp != myip_ifp) /* !same interface */
    744 		goto out;
    745 	if (myip_initialized)
    746 		goto wake;
    747 	if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
    748 		goto out;
    749 	bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
    750 	bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
    751 	myip_initialized = 1;
    752 wake:	/* Do wakeup every time in case it was missed. */
    753 	wakeup((caddr_t)&myip);
    754 
    755 out:
    756 	m_freem(m);
    757 }
    758 
    759 /*
    760  * Send a RARP request for the ip address of the specified interface.
    761  * The request should be RFC 903-compliant.
    762  */
    763 void
    764 revarprequest(ifp)
    765 	struct ifnet *ifp;
    766 {
    767 	struct sockaddr sa;
    768 	struct mbuf *m;
    769 	struct arphdr *ah;
    770 
    771 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    772 		return;
    773 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
    774 	    2*ifp->if_data.ifi_addrlen;
    775 	m->m_pkthdr.len = m->m_len;
    776 	MH_ALIGN(m, m->m_len);
    777 	ah = mtod(m, struct arphdr *);
    778 	bzero((caddr_t)ah, m->m_len);
    779 	ah->ar_pro = htons(ETHERTYPE_IP);
    780 	ah->ar_hln = ifp->if_data.ifi_addrlen;	/* hardware address length */
    781 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
    782 	ah->ar_op = htons(ARPOP_REVREQUEST);
    783 
    784 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
    785 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
    786 
    787 	sa.sa_family = AF_ARP;
    788 	sa.sa_len = 2;
    789 	m->m_flags |= M_BCAST;
    790 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
    791 
    792 }
    793 
    794 /*
    795  * RARP for the ip address of the specified interface, but also
    796  * save the ip address of the server that sent the answer.
    797  * Timeout if no response is received.
    798  */
    799 int
    800 revarpwhoarewe(ifp, serv_in, clnt_in)
    801 	struct ifnet *ifp;
    802 	struct in_addr *serv_in;
    803 	struct in_addr *clnt_in;
    804 {
    805 	int result, count = 20;
    806 
    807 	if (!myip_initialized) {
    808 		myip_ifp = ifp;
    809 		revarp_in_progress = 1;
    810 		while (count--) {
    811 			revarprequest(ifp);
    812 			result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
    813 			if (result != EWOULDBLOCK)
    814 				break;
    815 		}
    816 		revarp_in_progress = 0;
    817 	}
    818 	if (!myip_initialized)
    819 		return ENETUNREACH;
    820 
    821 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
    822 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
    823 	return 0;
    824 }
    825 
    826 /* For compatibility: only saves interface address. */
    827 int
    828 revarpwhoami(in, ifp)
    829 	struct in_addr *in;
    830 	struct ifnet *ifp;
    831 {
    832 	struct in_addr server;
    833 	return (revarpwhoarewe(ifp, &server, in));
    834 }
    835 
    836 
    838 #ifdef DDB
    839 
    840 #include <machine/db_machdep.h>
    841 #include <ddb/db_interface.h>
    842 #include <ddb/db_output.h>
    843 static void
    844 db_print_sa(sa)
    845 	struct sockaddr *sa;
    846 {
    847 	int len;
    848 	u_char *p;
    849 
    850 	if (sa == 0) {
    851 		db_printf("[NULL]");
    852 		return;
    853 	}
    854 
    855 	p = (u_char*)sa;
    856 	len = sa->sa_len;
    857 	db_printf("[");
    858 	while (len > 0) {
    859 		db_printf("%d", *p);
    860 		p++; len--;
    861 		if (len) db_printf(",");
    862 	}
    863 	db_printf("]\n");
    864 }
    865 static void
    866 db_print_ifa(ifa)
    867 	struct ifaddr *ifa;
    868 {
    869 	if (ifa == 0)
    870 		return;
    871 	db_printf("  ifa_addr=");
    872 	db_print_sa(ifa->ifa_addr);
    873 	db_printf("  ifa_dsta=");
    874 	db_print_sa(ifa->ifa_dstaddr);
    875 	db_printf("  ifa_mask=");
    876 	db_print_sa(ifa->ifa_netmask);
    877 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
    878 			  ifa->ifa_flags,
    879 			  ifa->ifa_refcnt,
    880 			  ifa->ifa_metric);
    881 }
    882 static void
    883 db_print_llinfo(li)
    884 	caddr_t li;
    885 {
    886 	struct llinfo_arp *la;
    887 
    888 	if (li == 0)
    889 		return;
    890 	la = (struct llinfo_arp *)li;
    891 	db_printf("  la_rt=%p la_hold=%p, la_asked=0x%lx\n",
    892 			  la->la_rt, la->la_hold, la->la_asked);
    893 }
    894 /*
    895  * Function to pass to rn_walktree().
    896  * Return non-zero error to abort walk.
    897  */
    898 static int
    899 db_show_radix_node(rn, w)
    900 	struct radix_node *rn;
    901 	void *w;
    902 {
    903 	struct rtentry *rt = (struct rtentry *)rn;
    904 
    905 	db_printf("rtentry=%p", rt);
    906 
    907 	db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
    908 			  rt->rt_flags, rt->rt_refcnt,
    909 			  rt->rt_use, rt->rt_expire);
    910 
    911 	db_printf(" key="); db_print_sa(rt_key(rt));
    912 	db_printf(" mask="); db_print_sa(rt_mask(rt));
    913 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
    914 
    915 	db_printf(" ifp=%p ", rt->rt_ifp);
    916 	if (rt->rt_ifp)
    917 		db_printf("(%s)", rt->rt_ifp->if_xname);
    918 	else
    919 		db_printf("(NULL)");
    920 
    921 	db_printf(" ifa=%p\n", rt->rt_ifa);
    922 	db_print_ifa(rt->rt_ifa);
    923 
    924 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
    925 
    926 	db_printf(" gwroute=%p llinfo=%p\n",
    927 			  rt->rt_gwroute, rt->rt_llinfo);
    928 	db_print_llinfo(rt->rt_llinfo);
    929 
    930 	return (0);
    931 }
    932 /*
    933  * Function to print all the route trees.
    934  * Use this from ddb:  "call db_show_arptab"
    935  */
    936 int
    937 db_show_arptab()
    938 {
    939 	struct radix_node_head *rnh;
    940 	rnh = rt_tables[AF_INET];
    941 	db_printf("Route tree for AF_INET\n");
    942 	if (rnh == NULL) {
    943 		db_printf(" (not initialized)\n");
    944 		return (0);
    945 	}
    946 	rn_walktree(rnh, db_show_radix_node, NULL);
    947 	return (0);
    948 }
    949 #endif
    950 #endif /* INET */
    951