Home | History | Annotate | Line # | Download | only in netinet
if_arp.c revision 1.39
      1 /*	$NetBSD: if_arp.c,v 1.39 1997/08/04 06:18:49 lukem 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 	for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
    481 		if (ia->ia_ifp == ifp) {
    482 			maybe_ia = ia;
    483 			if (in_hosteq(itaddr, ia->ia_addr.sin_addr) ||
    484 			    in_hosteq(isaddr, ia->ia_addr.sin_addr))
    485 				break;
    486 		}
    487 	if (maybe_ia == 0)
    488 		goto out;
    489 
    490 	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
    491 	if (!bcmp((caddr_t)ar_sha(ah), LLADDR(ifp->if_sadl),
    492 	    ifp->if_data.ifi_addrlen))
    493 		goto out;	/* it's from me, ignore it. */
    494 /*
    495  *  XXX
    496  *	if (!bcmp((caddr_t)ar_sha(ah), (caddr_t)etherbroadcastaddr,
    497  *	    sizeof (ea->arp_sha))) {
    498  *		log(LOG_ERR,
    499  *		    "arp: ether address is broadcast for IP address %x!\n",
    500  *		    ntohl(isaddr.s_addr));
    501  *		goto out;
    502  *	}
    503  */
    504 	if (in_hosteq(isaddr, myaddr)) {
    505 		log(LOG_ERR,
    506 		   "duplicate IP address %08x sent from link address %s\n",
    507 		   ntohl(isaddr.s_addr), lla_snprintf(ar_sha(ah), ah->ar_hln));
    508 		itaddr = myaddr;
    509 		goto reply;
    510 	}
    511 	la = arplookup(&isaddr, in_hosteq(itaddr, myaddr), 0);
    512 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
    513 		if (sdl->sdl_alen &&
    514 		    bcmp((caddr_t)ar_sha(ah), LLADDR(sdl), sdl->sdl_alen))
    515 			log(LOG_INFO, "arp info overwritten for %08x by %s\n",
    516 			    ntohl(isaddr.s_addr),
    517 			    lla_snprintf(ar_sha(ah), ah->ar_hln));
    518 		/*
    519 		 * sanity check for the address length.
    520 		 * XXX this does not work for protocols with variable address
    521 		 * length. -is
    522 		 */
    523 		if (sdl->sdl_alen &&
    524 		    sdl->sdl_alen != ah->ar_hln) {
    525 			log(LOG_WARNING,
    526 			    "arp from %08x: new addr len %d, was %d",
    527 			    ntohl(isaddr.s_addr), ah->ar_hln, sdl->sdl_alen);
    528 		}
    529 		if (ifp->if_data.ifi_addrlen != ah->ar_hln) {
    530 			log(LOG_WARNING,
    531 			    "arp from %08x: addr len: new %d, i/f %d (ignored)",
    532 			    ntohl(isaddr.s_addr), ah->ar_hln,
    533 			    ifp->if_data.ifi_addrlen);
    534 			goto reply;
    535 		}
    536 		bcopy((caddr_t)ar_sha(ah), LLADDR(sdl),
    537 		    sdl->sdl_alen = ah->ar_hln);
    538 		if (rt->rt_expire)
    539 			rt->rt_expire = time.tv_sec + arpt_keep;
    540 		rt->rt_flags &= ~RTF_REJECT;
    541 		la->la_asked = 0;
    542 		if (la->la_hold) {
    543 			(*ifp->if_output)(ifp, la->la_hold,
    544 				rt_key(rt), rt);
    545 			la->la_hold = 0;
    546 		}
    547 	}
    548 reply:
    549 	if (op != ARPOP_REQUEST) {
    550 	out:
    551 		m_freem(m);
    552 		return;
    553 	}
    554 	if (in_hosteq(itaddr, myaddr)) {
    555 		/* I am the target */
    556 		bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
    557 		bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
    558 	} else {
    559 		la = arplookup(&itaddr, 0, SIN_PROXY);
    560 		if (la == 0)
    561 			goto out;
    562 		rt = la->la_rt;
    563 		bcopy((caddr_t)ar_sha(ah), (caddr_t)ar_tha(ah), ah->ar_hln);
    564 		sdl = SDL(rt->rt_gateway);
    565 		bcopy(LLADDR(sdl), (caddr_t)ar_sha(ah), ah->ar_hln);
    566 	}
    567 
    568 	bcopy((caddr_t)ar_spa(ah), (caddr_t)ar_tpa(ah), ah->ar_pln);
    569 	bcopy((caddr_t)&itaddr, (caddr_t)ar_spa(ah), ah->ar_pln);
    570 	ah->ar_op = htons(ARPOP_REPLY);
    571 	ah->ar_pro = htons(ETHERTYPE_IP); /* let's be sure! */
    572 	m->m_flags &= ~(M_BCAST|M_MCAST); /* never reply by broadcast */
    573 	sa.sa_family = AF_ARP;
    574 	sa.sa_len = 2;
    575 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
    576 	return;
    577 }
    578 
    579 /*
    580  * Free an arp entry.
    581  */
    582 static void
    583 arptfree(la)
    584 	register struct llinfo_arp *la;
    585 {
    586 	register struct rtentry *rt = la->la_rt;
    587 	register struct sockaddr_dl *sdl;
    588 
    589 	if (rt == 0)
    590 		panic("arptfree");
    591 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
    592 	    sdl->sdl_family == AF_LINK) {
    593 		sdl->sdl_alen = 0;
    594 		la->la_asked = 0;
    595 		rt->rt_flags &= ~RTF_REJECT;
    596 		return;
    597 	}
    598 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
    599 	    0, (struct rtentry **)0);
    600 }
    601 
    602 /*
    603  * Lookup or enter a new address in arptab.
    604  */
    605 static struct llinfo_arp *
    606 arplookup(addr, create, proxy)
    607 	struct in_addr *addr;
    608 	int create, proxy;
    609 {
    610 	register struct rtentry *rt;
    611 	static struct sockaddr_inarp sin;
    612 
    613 	sin.sin_len = sizeof(sin);
    614 	sin.sin_family = AF_INET;
    615 	sin.sin_addr = *addr;
    616 	sin.sin_other = proxy ? SIN_PROXY : 0;
    617 	rt = rtalloc1(sintosa(&sin), create);
    618 	if (rt == 0)
    619 		return (0);
    620 	rt->rt_refcnt--;
    621 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
    622 	    rt->rt_gateway->sa_family != AF_LINK) {
    623 		if (create)
    624 			log(LOG_DEBUG, "arplookup: unable to enter address for %x\n",
    625 			    ntohl(addr->s_addr));
    626 		return (0);
    627 	}
    628 	return ((struct llinfo_arp *)rt->rt_llinfo);
    629 }
    630 
    631 int
    632 arpioctl(cmd, data)
    633 	u_long cmd;
    634 	caddr_t data;
    635 {
    636 
    637 	return (EOPNOTSUPP);
    638 }
    639 
    640 void
    641 arp_ifinit(ifp, ifa)
    642 	struct ifnet *ifp;
    643 	struct ifaddr *ifa;
    644 {
    645 
    646 	/* Warn the user if another station has this IP address. */
    647 	arprequest(ifp,
    648 	    &IA_SIN(ifa)->sin_addr,
    649 	    &IA_SIN(ifa)->sin_addr,
    650 	    LLADDR(ifp->if_sadl));
    651 	ifa->ifa_rtrequest = arp_rtrequest;
    652 	ifa->ifa_flags |= RTF_CLONING;
    653 }
    654 
    655 /*
    656  * Called from 10 Mb/s Ethernet interrupt handlers
    657  * when ether packet type ETHERTYPE_REVARP
    658  * is received.  Common length and type checks are done here,
    659  * then the protocol-specific routine is called.
    660  */
    661 void
    662 revarpinput(m)
    663 	struct mbuf *m;
    664 {
    665 	struct arphdr *ar;
    666 
    667 	if (m->m_len < sizeof(struct arphdr))
    668 		goto out;
    669 	ar = mtod(m, struct arphdr *);
    670 #if 0 /* XXX I don't think we need this... and it will prevent other LL */
    671 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
    672 		goto out;
    673 #endif
    674 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
    675 		goto out;
    676 	switch (ntohs(ar->ar_pro)) {
    677 
    678 	case ETHERTYPE_IP:
    679 	case ETHERTYPE_IPTRAILERS:
    680 		in_revarpinput(m);
    681 		return;
    682 
    683 	default:
    684 		break;
    685 	}
    686 out:
    687 	m_freem(m);
    688 }
    689 
    690 /*
    691  * RARP for Internet protocols on 10 Mb/s Ethernet.
    692  * Algorithm is that given in RFC 903.
    693  * We are only using for bootstrap purposes to get an ip address for one of
    694  * our interfaces.  Thus we support no user-interface.
    695  *
    696  * Since the contents of the RARP reply are specific to the interface that
    697  * sent the request, this code must ensure that they are properly associated.
    698  *
    699  * Note: also supports ARP via RARP packets, per the RFC.
    700  */
    701 void
    702 in_revarpinput(m)
    703 	struct mbuf *m;
    704 {
    705 	struct ifnet *ifp;
    706 	struct arphdr *ah;
    707 	int op;
    708 
    709 	ah = mtod(m, struct arphdr *);
    710 	op = ntohs(ah->ar_op);
    711 	switch (op) {
    712 	case ARPOP_REQUEST:
    713 	case ARPOP_REPLY:	/* per RFC */
    714 		in_arpinput(m);
    715 		return;
    716 	case ARPOP_REVREPLY:
    717 		break;
    718 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
    719 	default:
    720 		goto out;
    721 	}
    722 	if (!revarp_in_progress)
    723 		goto out;
    724 	ifp = m->m_pkthdr.rcvif;
    725 	if (ifp != myip_ifp) /* !same interface */
    726 		goto out;
    727 	if (myip_initialized)
    728 		goto wake;
    729 	if (bcmp(ar_tha(ah), LLADDR(ifp->if_sadl), ifp->if_sadl->sdl_alen))
    730 		goto out;
    731 	bcopy((caddr_t)ar_spa(ah), (caddr_t)&srv_ip, sizeof(srv_ip));
    732 	bcopy((caddr_t)ar_tpa(ah), (caddr_t)&myip, sizeof(myip));
    733 	myip_initialized = 1;
    734 wake:	/* Do wakeup every time in case it was missed. */
    735 	wakeup((caddr_t)&myip);
    736 
    737 out:
    738 	m_freem(m);
    739 }
    740 
    741 /*
    742  * Send a RARP request for the ip address of the specified interface.
    743  * The request should be RFC 903-compliant.
    744  */
    745 void
    746 revarprequest(ifp)
    747 	struct ifnet *ifp;
    748 {
    749 	struct sockaddr sa;
    750 	struct mbuf *m;
    751 	struct arphdr *ah;
    752 
    753 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    754 		return;
    755 	m->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
    756 	    2*ifp->if_data.ifi_addrlen;
    757 	m->m_pkthdr.len = m->m_len;
    758 	MH_ALIGN(m, m->m_len);
    759 	ah = mtod(m, struct arphdr *);
    760 	bzero((caddr_t)ah, m->m_len);
    761 	ah->ar_pro = htons(ETHERTYPE_IP);
    762 	ah->ar_hln = ifp->if_data.ifi_addrlen;	/* hardware address length */
    763 	ah->ar_pln = sizeof(struct in_addr);	/* protocol address length */
    764 	ah->ar_op = htons(ARPOP_REVREQUEST);
    765 
    766 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_sha(ah), ah->ar_hln);
    767 	bcopy(LLADDR(ifp->if_sadl), (caddr_t)ar_tha(ah), ah->ar_hln);
    768 
    769 	sa.sa_family = AF_ARP;
    770 	sa.sa_len = 2;
    771 	m->m_flags |= M_BCAST;
    772 	(*ifp->if_output)(ifp, m, &sa, (struct rtentry *)0);
    773 
    774 }
    775 
    776 /*
    777  * RARP for the ip address of the specified interface, but also
    778  * save the ip address of the server that sent the answer.
    779  * Timeout if no response is received.
    780  */
    781 int
    782 revarpwhoarewe(ifp, serv_in, clnt_in)
    783 	struct ifnet *ifp;
    784 	struct in_addr *serv_in;
    785 	struct in_addr *clnt_in;
    786 {
    787 	int result, count = 20;
    788 
    789 	if (!myip_initialized) {
    790 		myip_ifp = ifp;
    791 		revarp_in_progress = 1;
    792 		while (count--) {
    793 			revarprequest(ifp);
    794 			result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
    795 			if (result != EWOULDBLOCK)
    796 				break;
    797 		}
    798 		revarp_in_progress = 0;
    799 	}
    800 	if (!myip_initialized)
    801 		return ENETUNREACH;
    802 
    803 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
    804 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
    805 	return 0;
    806 }
    807 
    808 /* For compatibility: only saves interface address. */
    809 int
    810 revarpwhoami(in, ifp)
    811 	struct in_addr *in;
    812 	struct ifnet *ifp;
    813 {
    814 	struct in_addr server;
    815 	return (revarpwhoarewe(ifp, &server, in));
    816 }
    817 
    818 
    820 #ifdef DDB
    821 
    822 #include <machine/db_machdep.h>
    823 #include <ddb/db_interface.h>
    824 #include <ddb/db_output.h>
    825 static void
    826 db_print_sa(sa)
    827 	struct sockaddr *sa;
    828 {
    829 	int len;
    830 	u_char *p;
    831 
    832 	if (sa == 0) {
    833 		db_printf("[NULL]");
    834 		return;
    835 	}
    836 
    837 	p = (u_char*)sa;
    838 	len = sa->sa_len;
    839 	db_printf("[");
    840 	while (len > 0) {
    841 		db_printf("%d", *p);
    842 		p++; len--;
    843 		if (len) db_printf(",");
    844 	}
    845 	db_printf("]\n");
    846 }
    847 static void
    848 db_print_ifa(ifa)
    849 	struct ifaddr *ifa;
    850 {
    851 	if (ifa == 0)
    852 		return;
    853 	db_printf("  ifa_addr=");
    854 	db_print_sa(ifa->ifa_addr);
    855 	db_printf("  ifa_dsta=");
    856 	db_print_sa(ifa->ifa_dstaddr);
    857 	db_printf("  ifa_mask=");
    858 	db_print_sa(ifa->ifa_netmask);
    859 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
    860 			  ifa->ifa_flags,
    861 			  ifa->ifa_refcnt,
    862 			  ifa->ifa_metric);
    863 }
    864 static void
    865 db_print_llinfo(li)
    866 	caddr_t li;
    867 {
    868 	struct llinfo_arp *la;
    869 
    870 	if (li == 0)
    871 		return;
    872 	la = (struct llinfo_arp *)li;
    873 	db_printf("  la_rt=%p la_hold=%p, la_asked=0x%lx\n",
    874 			  la->la_rt, la->la_hold, la->la_asked);
    875 }
    876 /*
    877  * Function to pass to rn_walktree().
    878  * Return non-zero error to abort walk.
    879  */
    880 static int
    881 db_show_radix_node(rn, w)
    882 	struct radix_node *rn;
    883 	void *w;
    884 {
    885 	struct rtentry *rt = (struct rtentry *)rn;
    886 
    887 	db_printf("rtentry=%p", rt);
    888 
    889 	db_printf(" flags=0x%x refcnt=%d use=%ld expire=%ld\n",
    890 			  rt->rt_flags, rt->rt_refcnt,
    891 			  rt->rt_use, rt->rt_expire);
    892 
    893 	db_printf(" key="); db_print_sa(rt_key(rt));
    894 	db_printf(" mask="); db_print_sa(rt_mask(rt));
    895 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
    896 
    897 	db_printf(" ifp=%p ", rt->rt_ifp);
    898 	if (rt->rt_ifp)
    899 		db_printf("(%s)", rt->rt_ifp->if_xname);
    900 	else
    901 		db_printf("(NULL)");
    902 
    903 	db_printf(" ifa=%p\n", rt->rt_ifa);
    904 	db_print_ifa(rt->rt_ifa);
    905 
    906 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
    907 
    908 	db_printf(" gwroute=%p llinfo=%p\n",
    909 			  rt->rt_gwroute, rt->rt_llinfo);
    910 	db_print_llinfo(rt->rt_llinfo);
    911 
    912 	return (0);
    913 }
    914 /*
    915  * Function to print all the route trees.
    916  * Use this from ddb:  "call db_show_arptab"
    917  */
    918 int
    919 db_show_arptab()
    920 {
    921 	struct radix_node_head *rnh;
    922 	rnh = rt_tables[AF_INET];
    923 	db_printf("Route tree for AF_INET\n");
    924 	if (rnh == NULL) {
    925 		db_printf(" (not initialized)\n");
    926 		return (0);
    927 	}
    928 	rn_walktree(rnh, db_show_radix_node, NULL);
    929 	return (0);
    930 }
    931 #endif
    932 #endif /* INET */
    933