Home | History | Annotate | Line # | Download | only in netinet
if_arp.c revision 1.19
      1 /*	$NetBSD: if_arp.c,v 1.19 1995/03/06 19:06:09 glass 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 
     57 #include <net/if.h>
     58 #include <net/if_dl.h>
     59 #include <net/route.h>
     60 
     61 #include <netinet/in.h>
     62 #include <netinet/in_systm.h>
     63 #include <netinet/in_var.h>
     64 #include <netinet/ip.h>
     65 #include <netinet/if_ether.h>
     66 
     67 #define SIN(s) ((struct sockaddr_in *)s)
     68 #define SDL(s) ((struct sockaddr_dl *)s)
     69 #define SRP(s) ((struct sockaddr_inarp *)s)
     70 
     71 /*
     72  * ARP trailer negotiation.  Trailer protocol is not IP specific,
     73  * but ARP request/response use IP addresses.
     74  */
     75 #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
     76 
     77 /* timer values */
     78 int	arpt_prune = (5*60*1);	/* walk list every 5 minutes */
     79 int	arpt_keep = (20*60);	/* once resolved, good for 20 more minutes */
     80 int	arpt_down = 20;		/* once declared down, don't send for 20 secs */
     81 #define	rt_expire rt_rmx.rmx_expire
     82 
     83 static	void arprequest __P((struct arpcom *, u_long *, u_long *, u_char *));
     84 static	void arptfree __P((struct llinfo_arp *));
     85 static	void arptimer __P((void *));
     86 static	struct llinfo_arp *arplookup __P((u_long, int, int));
     87 static	void in_arpinput __P((struct mbuf *));
     88 
     89 extern	struct ifnet loif;
     90 struct	llinfo_arp llinfo_arp = {&llinfo_arp, &llinfo_arp};
     91 struct	ifqueue arpintrq = {0, 0, 0, 50};
     92 int	arp_inuse, arp_allocated, arp_intimer;
     93 int	arp_maxtries = 5;
     94 int	useloopback = 1;	/* use loopback interface for local traffic */
     95 int	arpinit_done = 0;
     96 
     97 /* revarp state */
     98 static struct	in_addr myip, srv_ip;
     99 static int	myip_initialized = 0;
    100 static int	revarp_in_progress = 0;
    101 static struct	ifnet *myip_ifp = NULL;
    102 
    103 /*
    104  * Timeout routine.  Age arp_tab entries periodically.
    105  */
    106 /* ARGSUSED */
    107 static void
    108 arptimer(arg)
    109 	void *arg;
    110 {
    111 	int s = splnet();
    112 	register struct llinfo_arp *la = llinfo_arp.la_next;
    113 
    114 	timeout(arptimer, NULL, arpt_prune * hz);
    115 	while (la != &llinfo_arp) {
    116 		register struct rtentry *rt = la->la_rt;
    117 		la = la->la_next;
    118 		if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
    119 			arptfree(la->la_prev); /* timer has expired; clear */
    120 	}
    121 	splx(s);
    122 }
    123 
    124 /*
    125  * Parallel to llc_rtrequest.
    126  */
    127 void
    128 arp_rtrequest(req, rt, sa)
    129 	int req;
    130 	register struct rtentry *rt;
    131 	struct sockaddr *sa;
    132 {
    133 	register struct sockaddr *gate = rt->rt_gateway;
    134 	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
    135 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
    136 
    137 	if (!arpinit_done) {
    138 		arpinit_done = 1;
    139 		/*
    140 		 * We generate expiration times from time.tv_sec
    141 		 * so avoid accidently creating permanent routes.
    142 		 */
    143 		if (time.tv_sec == 0) {
    144 			time.tv_sec++;
    145 		}
    146 		timeout(arptimer, (caddr_t)0, hz);
    147 	}
    148 	if (rt->rt_flags & RTF_GATEWAY)
    149 		return;
    150 	switch (req) {
    151 
    152 	case RTM_ADD:
    153 		/*
    154 		 * XXX: If this is a manually added route to interface
    155 		 * such as older version of routed or gated might provide,
    156 		 * restore cloning bit.
    157 		 */
    158 		if ((rt->rt_flags & RTF_HOST) == 0 &&
    159 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
    160 			rt->rt_flags |= RTF_CLONING;
    161 		if (rt->rt_flags & RTF_CLONING) {
    162 			/*
    163 			 * Case 1: This route should come from a route to iface.
    164 			 */
    165 			rt_setgate(rt, rt_key(rt),
    166 					(struct sockaddr *)&null_sdl);
    167 			gate = rt->rt_gateway;
    168 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
    169 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
    170 			/*
    171 			 * Give this route an expiration time, even though
    172 			 * it's a "permanent" route, so that routes cloned
    173 			 * from it do not need their expiration time set.
    174 			 */
    175 			rt->rt_expire = time.tv_sec;
    176 			break;
    177 		}
    178 		/* Announce a new entry if requested. */
    179 		if (rt->rt_flags & RTF_ANNOUNCE)
    180 			arprequest((struct arpcom *)rt->rt_ifp,
    181 			    &SIN(rt_key(rt))->sin_addr.s_addr,
    182 			    &SIN(rt_key(rt))->sin_addr.s_addr,
    183 			    (u_char *)LLADDR(SDL(gate)));
    184 		/*FALLTHROUGH*/
    185 	case RTM_RESOLVE:
    186 		if (gate->sa_family != AF_LINK ||
    187 		    gate->sa_len < sizeof(null_sdl)) {
    188 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value");
    189 			break;
    190 		}
    191 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
    192 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
    193 		if (la != 0)
    194 			break; /* This happens on a route change */
    195 		/*
    196 		 * Case 2:  This route may come from cloning, or a manual route
    197 		 * add with a LL address.
    198 		 */
    199 		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
    200 		rt->rt_llinfo = (caddr_t)la;
    201 		if (la == 0) {
    202 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
    203 			break;
    204 		}
    205 		arp_inuse++, arp_allocated++;
    206 		Bzero(la, sizeof(*la));
    207 		la->la_rt = rt;
    208 		rt->rt_flags |= RTF_LLINFO;
    209 		insque(la, &llinfo_arp);
    210 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
    211 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
    212 		    /*
    213 		     * This test used to be
    214 		     *	if (loif.if_flags & IFF_UP)
    215 		     * It allowed local traffic to be forced
    216 		     * through the hardware by configuring the loopback down.
    217 		     * However, it causes problems during network configuration
    218 		     * for boards that can't receive packets they send.
    219 		     * It is now necessary to clear "useloopback" and remove
    220 		     * the route to force traffic out to the hardware.
    221 		     */
    222 			rt->rt_expire = 0;
    223 			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
    224 				LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
    225 			if (useloopback)
    226 				rt->rt_ifp = &loif;
    227 
    228 		}
    229 		break;
    230 
    231 	case RTM_DELETE:
    232 		if (la == 0)
    233 			break;
    234 		arp_inuse--;
    235 		remque(la);
    236 		rt->rt_llinfo = 0;
    237 		rt->rt_flags &= ~RTF_LLINFO;
    238 		if (la->la_hold)
    239 			m_freem(la->la_hold);
    240 		Free((caddr_t)la);
    241 	}
    242 }
    243 
    244 /*
    245  * Broadcast an ARP packet, asking who has addr on interface ac.
    246  */
    247 void
    248 arpwhohas(ac, addr)
    249 	register struct arpcom *ac;
    250 	register struct in_addr *addr;
    251 {
    252 	arprequest(ac, &ac->ac_ipaddr.s_addr, &addr->s_addr, ac->ac_enaddr);
    253 }
    254 
    255 /*
    256  * Broadcast an ARP request. Caller specifies:
    257  *	- arp header source ip address
    258  *	- arp header target ip address
    259  *	- arp header source ethernet address
    260  */
    261 static void
    262 arprequest(ac, sip, tip, enaddr)
    263 	register struct arpcom *ac;
    264 	register u_long *sip, *tip;
    265 	register u_char *enaddr;
    266 {
    267 	register struct mbuf *m;
    268 	register struct ether_header *eh;
    269 	register struct ether_arp *ea;
    270 	struct sockaddr sa;
    271 
    272 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    273 		return;
    274 	m->m_len = sizeof(*ea);
    275 	m->m_pkthdr.len = sizeof(*ea);
    276 	MH_ALIGN(m, sizeof(*ea));
    277 	ea = mtod(m, struct ether_arp *);
    278 	eh = (struct ether_header *)sa.sa_data;
    279 	bzero((caddr_t)ea, sizeof (*ea));
    280 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
    281 	    sizeof(eh->ether_dhost));
    282 	eh->ether_type = htons(ETHERTYPE_ARP);
    283 	ea->arp_hrd = htons(ARPHRD_ETHER);
    284 	ea->arp_pro = htons(ETHERTYPE_IP);
    285 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
    286 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
    287 	ea->arp_op = htons(ARPOP_REQUEST);
    288 	bcopy((caddr_t)enaddr, (caddr_t)ea->arp_sha, sizeof(ea->arp_sha));
    289 	bcopy((caddr_t)sip, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa));
    290 	bcopy((caddr_t)tip, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa));
    291 	sa.sa_family = AF_UNSPEC;
    292 	sa.sa_len = sizeof(sa);
    293 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
    294 }
    295 
    296 /*
    297  * Resolve an IP address into an ethernet address.  If success,
    298  * desten is filled in.  If there is no entry in arptab,
    299  * set one up and broadcast a request for the IP address.
    300  * Hold onto this mbuf and resend it once the address
    301  * is finally resolved.  A return value of 1 indicates
    302  * that desten has been filled in and the packet should be sent
    303  * normally; a 0 return indicates that the packet has been
    304  * taken over here, either now or for later transmission.
    305  */
    306 int
    307 arpresolve(ac, rt, m, dst, desten)
    308 	register struct arpcom *ac;
    309 	register struct rtentry *rt;
    310 	struct mbuf *m;
    311 	register struct sockaddr *dst;
    312 	register u_char *desten;
    313 {
    314 	register struct llinfo_arp *la;
    315 	struct sockaddr_dl *sdl;
    316 
    317 	if (m->m_flags & M_BCAST) {	/* broadcast */
    318 		bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten,
    319 		    sizeof(etherbroadcastaddr));
    320 		return (1);
    321 	}
    322 	if (m->m_flags & M_MCAST) {	/* multicast */
    323 		ETHER_MAP_IP_MULTICAST(&SIN(dst)->sin_addr, desten);
    324 		return(1);
    325 	}
    326 	if (rt)
    327 		la = (struct llinfo_arp *)rt->rt_llinfo;
    328 	else {
    329 		if (la = arplookup(SIN(dst)->sin_addr.s_addr, 1, 0))
    330 			rt = la->la_rt;
    331 	}
    332 	if (la == 0 || rt == 0) {
    333 		log(LOG_DEBUG, "arpresolve: can't allocate llinfo");
    334 		m_freem(m);
    335 		return (0);
    336 	}
    337 	sdl = SDL(rt->rt_gateway);
    338 	/*
    339 	 * Check the address family and length is valid, the address
    340 	 * is resolved; otherwise, try to resolve.
    341 	 */
    342 	if ((rt->rt_expire == 0 || rt->rt_expire > time.tv_sec) &&
    343 	    sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) {
    344 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
    345 		return 1;
    346 	}
    347 	/*
    348 	 * There is an arptab entry, but no ethernet address
    349 	 * response yet.  Replace the held mbuf with this
    350 	 * latest one.
    351 	 */
    352 	if (la->la_hold)
    353 		m_freem(la->la_hold);
    354 	la->la_hold = m;
    355 	/*
    356 	 * Re-send the ARP request when appropriate.
    357 	 */
    358 #ifdef	DIAGNOSTIC
    359 	if (rt->rt_expire == 0) {
    360 		/* This should never happen. (Should it? -gwr) */
    361 		printf("arpresolve: unresolved and rt_expire == 0\n");
    362 		/* Set expiration time to now (expired). */
    363 		rt->rt_expire = time.tv_sec;
    364 	}
    365 #endif
    366 	if (rt->rt_expire) {
    367 		rt->rt_flags &= ~RTF_REJECT;
    368 		if (la->la_asked == 0 || rt->rt_expire != time.tv_sec) {
    369 			rt->rt_expire = time.tv_sec;
    370 			if (la->la_asked++ < arp_maxtries)
    371 				arpwhohas(ac, &(SIN(dst)->sin_addr));
    372 			else {
    373 				rt->rt_flags |= RTF_REJECT;
    374 				rt->rt_expire += arpt_down;
    375 				la->la_asked = 0;
    376 			}
    377 		}
    378 	}
    379 	return (0);
    380 }
    381 
    382 /*
    383  * Common length and type checks are done here,
    384  * then the protocol-specific routine is called.
    385  */
    386 void
    387 arpintr()
    388 {
    389 	register struct mbuf *m;
    390 	register struct arphdr *ar;
    391 	int s;
    392 
    393 	while (arpintrq.ifq_head) {
    394 		s = splimp();
    395 		IF_DEQUEUE(&arpintrq, m);
    396 		splx(s);
    397 		if (m == 0 || (m->m_flags & M_PKTHDR) == 0)
    398 			panic("arpintr");
    399 		if (m->m_len >= sizeof(struct arphdr) &&
    400 		    (ar = mtod(m, struct arphdr *)) &&
    401 		    ntohs(ar->ar_hrd) == ARPHRD_ETHER &&
    402 		    m->m_len >=
    403 		      sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
    404 			switch (ntohs(ar->ar_pro)) {
    405 
    406 			case ETHERTYPE_IP:
    407 			case ETHERTYPE_IPTRAILERS:
    408 				in_arpinput(m);
    409 				continue;
    410 			}
    411 		m_freem(m);
    412 	}
    413 }
    414 
    415 /*
    416  * ARP for Internet protocols on 10 Mb/s Ethernet.
    417  * Algorithm is that given in RFC 826.
    418  * In addition, a sanity check is performed on the sender
    419  * protocol address, to catch impersonators.
    420  * We no longer handle negotiations for use of trailer protocol:
    421  * Formerly, ARP replied for protocol type ETHERTYPE_TRAIL sent
    422  * along with IP replies if we wanted trailers sent to us,
    423  * and also sent them in response to IP replies.
    424  * This allowed either end to announce the desire to receive
    425  * trailer packets.
    426  * We no longer reply to requests for ETHERTYPE_TRAIL protocol either,
    427  * but formerly didn't normally send requests.
    428  */
    429 static void
    430 in_arpinput(m)
    431 	struct mbuf *m;
    432 {
    433 	register struct ether_arp *ea;
    434 	register struct arpcom *ac = (struct arpcom *)m->m_pkthdr.rcvif;
    435 	struct ether_header *eh;
    436 	register struct llinfo_arp *la = 0;
    437 	register struct rtentry *rt;
    438 	struct in_ifaddr *ia, *maybe_ia = 0;
    439 	struct sockaddr_dl *sdl;
    440 	struct sockaddr sa;
    441 	struct in_addr isaddr, itaddr, myaddr;
    442 	int op;
    443 
    444 	ea = mtod(m, struct ether_arp *);
    445 	op = ntohs(ea->arp_op);
    446 	bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr));
    447 	bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr));
    448 	for (ia = in_ifaddr; ia; ia = ia->ia_next)
    449 		if (ia->ia_ifp == &ac->ac_if) {
    450 			maybe_ia = ia;
    451 			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
    452 			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
    453 				break;
    454 		}
    455 	if (maybe_ia == 0)
    456 		goto out;
    457 	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
    458 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
    459 	    sizeof (ea->arp_sha)))
    460 		goto out;	/* it's from me, ignore it. */
    461 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
    462 	    sizeof (ea->arp_sha))) {
    463 		log(LOG_ERR,
    464 		    "arp: ether address is broadcast for IP address %x!\n",
    465 		    ntohl(isaddr.s_addr));
    466 		goto out;
    467 	}
    468 	if (isaddr.s_addr == myaddr.s_addr) {
    469 		log(LOG_ERR,
    470 		   "duplicate IP address %08x sent from ethernet address %s\n",
    471 		   ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha));
    472 		itaddr = myaddr;
    473 		goto reply;
    474 	}
    475 	la = arplookup(isaddr.s_addr, itaddr.s_addr == myaddr.s_addr, 0);
    476 	if (la && (rt = la->la_rt) && (sdl = SDL(rt->rt_gateway))) {
    477 		if (sdl->sdl_alen &&
    478 		    bcmp((caddr_t)ea->arp_sha, LLADDR(sdl), sdl->sdl_alen))
    479 			log(LOG_INFO, "arp info overwritten for %08x by %s\n",
    480 			    ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha));
    481 		bcopy((caddr_t)ea->arp_sha, LLADDR(sdl),
    482 		    sdl->sdl_alen = sizeof(ea->arp_sha));
    483 		if (rt->rt_expire)
    484 			rt->rt_expire = time.tv_sec + arpt_keep;
    485 		rt->rt_flags &= ~RTF_REJECT;
    486 		la->la_asked = 0;
    487 		if (la->la_hold) {
    488 			(*ac->ac_if.if_output)(&ac->ac_if, la->la_hold,
    489 				rt_key(rt), rt);
    490 			la->la_hold = 0;
    491 		}
    492 	}
    493 reply:
    494 	if (op != ARPOP_REQUEST) {
    495 	out:
    496 		m_freem(m);
    497 		return;
    498 	}
    499 	if (itaddr.s_addr == myaddr.s_addr) {
    500 		/* I am the target */
    501 		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
    502 		    sizeof(ea->arp_sha));
    503 		bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
    504 		    sizeof(ea->arp_sha));
    505 	} else {
    506 		la = arplookup(itaddr.s_addr, 0, SIN_PROXY);
    507 		if (la == 0)
    508 			goto out;
    509 		rt = la->la_rt;
    510 		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
    511 		    sizeof(ea->arp_sha));
    512 		sdl = SDL(rt->rt_gateway);
    513 		bcopy(LLADDR(sdl), (caddr_t)ea->arp_sha, sizeof(ea->arp_sha));
    514 	}
    515 
    516 	bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, sizeof(ea->arp_spa));
    517 	bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, sizeof(ea->arp_spa));
    518 	ea->arp_op = htons(ARPOP_REPLY);
    519 	ea->arp_pro = htons(ETHERTYPE_IP); /* let's be sure! */
    520 	eh = (struct ether_header *)sa.sa_data;
    521 	bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost,
    522 	    sizeof(eh->ether_dhost));
    523 	eh->ether_type = htons(ETHERTYPE_ARP);
    524 	sa.sa_family = AF_UNSPEC;
    525 	sa.sa_len = sizeof(sa);
    526 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
    527 	return;
    528 }
    529 
    530 /*
    531  * Free an arp entry.
    532  */
    533 static void
    534 arptfree(la)
    535 	register struct llinfo_arp *la;
    536 {
    537 	register struct rtentry *rt = la->la_rt;
    538 	register struct sockaddr_dl *sdl;
    539 
    540 	if (rt == 0)
    541 		panic("arptfree");
    542 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
    543 	    sdl->sdl_family == AF_LINK) {
    544 		sdl->sdl_alen = 0;
    545 		la->la_asked = 0;
    546 		rt->rt_flags &= ~RTF_REJECT;
    547 		return;
    548 	}
    549 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
    550 	    0, (struct rtentry **)0);
    551 }
    552 
    553 /*
    554  * Lookup or enter a new address in arptab.
    555  */
    556 static struct llinfo_arp *
    557 arplookup(addr, create, proxy)
    558 	u_long addr;
    559 	int create, proxy;
    560 {
    561 	register struct rtentry *rt;
    562 	static struct sockaddr_inarp sin;
    563 
    564 	sin.sin_len = sizeof(sin);
    565 	sin.sin_family = AF_INET;
    566 	sin.sin_addr.s_addr = addr;
    567 	sin.sin_other = proxy ? SIN_PROXY : 0;
    568 	rt = rtalloc1((struct sockaddr *)&sin, create);
    569 	if (rt == 0)
    570 		return (0);
    571 	rt->rt_refcnt--;
    572 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
    573 	    rt->rt_gateway->sa_family != AF_LINK) {
    574 		if (create)
    575 			log(LOG_DEBUG, "arplookup: unable to enter address for %x\n", ntohl(addr));
    576 		return (0);
    577 	}
    578 	return ((struct llinfo_arp *)rt->rt_llinfo);
    579 }
    580 
    581 int
    582 arpioctl(cmd, data)
    583 	int cmd;
    584 	caddr_t data;
    585 {
    586 	return (EOPNOTSUPP);
    587 }
    588 
    589 /*
    590  * Called from 10 Mb/s Ethernet interrupt handlers
    591  * when ether packet type ETHERTYPE_REVARP
    592  * is received.  Common length and type checks are done here,
    593  * then the protocol-specific routine is called.
    594  */
    595 void
    596 revarpinput(m)
    597 	struct mbuf *m;
    598 {
    599 	struct arphdr *ar;
    600 	int op, s;
    601 
    602 	if (m->m_len < sizeof(struct arphdr))
    603 		goto out;
    604 	ar = mtod(m, struct arphdr *);
    605 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
    606 		goto out;
    607 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
    608 		goto out;
    609 	switch (ntohs(ar->ar_pro)) {
    610 
    611 	case ETHERTYPE_IP:
    612 	case ETHERTYPE_IPTRAILERS:
    613 		in_revarpinput(m);
    614 		return;
    615 
    616 	default:
    617 		break;
    618 	}
    619 out:
    620 	m_freem(m);
    621 }
    622 
    623 /*
    624  * RARP for Internet protocols on 10 Mb/s Ethernet.
    625  * Algorithm is that given in RFC 903.
    626  * We are only using for bootstrap purposes to get an ip address for one of
    627  * our interfaces.  Thus we support no user-interface.
    628  *
    629  * Since the contents of the RARP reply are specific to the interface that
    630  * sent the request, this code must ensure that they are properly associated.
    631  *
    632  * Note: also supports ARP via RARP packets, per the RFC.
    633  */
    634 in_revarpinput(m)
    635 	struct mbuf *m;
    636 {
    637 	struct ifnet *ifp;
    638 	struct ether_arp *ar;
    639 	int op, s;
    640 
    641 	ar = mtod(m, struct ether_arp *);
    642 	op = ntohs(ar->arp_op);
    643 	switch (op) {
    644 	case ARPOP_REQUEST:
    645 	case ARPOP_REPLY:	/* per RFC */
    646 		in_arpinput(m);
    647 		return;
    648 	case ARPOP_REVREPLY:
    649 		break;
    650 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
    651 	default:
    652 		goto out;
    653 	}
    654 	if (!revarp_in_progress)
    655 		goto out;
    656 	ifp = m->m_pkthdr.rcvif;
    657 	if (ifp != myip_ifp) /* !same interface */
    658 		goto out;
    659 	if (myip_initialized)
    660 		goto wake;
    661 	if (bcmp(ar->arp_tha, ((struct arpcom *)ifp)->ac_enaddr,
    662 	    sizeof(ar->arp_tha)))
    663 		goto out;
    664 	bcopy((caddr_t)ar->arp_spa, (caddr_t)&srv_ip, sizeof(srv_ip));
    665 	bcopy((caddr_t)ar->arp_tpa, (caddr_t)&myip, sizeof(myip));
    666 	myip_initialized = 1;
    667 wake:	/* Do wakeup every time in case it was missed. */
    668 	wakeup((caddr_t)&myip);
    669 
    670 out:
    671 	m_freem(m);
    672 }
    673 
    674 /*
    675  * Send a RARP request for the ip address of the specified interface.
    676  * The request should be RFC 903-compliant.
    677  */
    678 void
    679 revarprequest(ifp)
    680 	struct ifnet *ifp;
    681 {
    682 	struct sockaddr sa;
    683 	struct mbuf *m;
    684 	struct ether_header *eh;
    685 	struct ether_arp *ea;
    686 	struct arpcom *ac = (struct arpcom *)ifp;
    687 
    688 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    689 		return;
    690 	m->m_len = sizeof(*ea);
    691 	m->m_pkthdr.len = sizeof(*ea);
    692 	MH_ALIGN(m, sizeof(*ea));
    693 	ea = mtod(m, struct ether_arp *);
    694 	eh = (struct ether_header *)sa.sa_data;
    695 	bzero((caddr_t)ea, sizeof(*ea));
    696 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
    697 	    sizeof(eh->ether_dhost));
    698 	eh->ether_type = htons(ETHERTYPE_REVARP);
    699 	ea->arp_hrd = htons(ARPHRD_ETHER);
    700 	ea->arp_pro = htons(ETHERTYPE_IP);
    701 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
    702 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
    703 	ea->arp_op = htons(ARPOP_REVREQUEST);
    704 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
    705 	   sizeof(ea->arp_sha));
    706 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha,
    707 	   sizeof(ea->arp_tha));
    708 	sa.sa_family = AF_UNSPEC;
    709 	sa.sa_len = sizeof(sa);
    710 	ifp->if_output(ifp, m, &sa, (struct rtentry *)0);
    711 }
    712 
    713 /*
    714  * RARP for the ip address of the specified interface, but also
    715  * save the ip address of the server that sent the answer.
    716  * Timeout if no response is received.
    717  */
    718 int
    719 revarpwhoarewe(ifp, serv_in, clnt_in)
    720 	struct ifnet *ifp;
    721 	struct in_addr *serv_in;
    722 	struct in_addr *clnt_in;
    723 {
    724 	int result, count = 20;
    725 
    726 	if (myip_initialized)
    727 		return EIO;
    728 
    729 	myip_ifp = ifp;
    730 	revarp_in_progress = 1;
    731 	while (count--) {
    732 		revarprequest(ifp);
    733 		result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
    734 		if (result != EWOULDBLOCK)
    735 			break;
    736 	}
    737 	revarp_in_progress = 0;
    738 	if (!myip_initialized)
    739 		return ENETUNREACH;
    740 
    741 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
    742 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
    743 	return 0;
    744 }
    745 
    746 /* For compatibility: only saves interface address. */
    747 int
    748 revarpwhoami(in, ifp)
    749 	struct in_addr *in;
    750 	struct ifnet *ifp;
    751 {
    752 	struct in_addr server;
    753 	return (revarpwhoarewe(ifp, &server, in));
    754 }
    755 
    756 
    758 #ifdef	DDB
    759 static void
    760 db_print_sa(sa)
    761 	struct sockaddr *sa;
    762 {
    763 	int len;
    764 	u_char *p;
    765 
    766 	if (sa == 0) {
    767 		db_printf("[NULL]");
    768 		return;
    769 	}
    770 
    771 	p = (u_char*)sa;
    772 	len = sa->sa_len;
    773 	db_printf("[");
    774 	while (len > 0) {
    775 		db_printf("%d", *p);
    776 		p++; len--;
    777 		if (len) db_printf(",");
    778 	}
    779 	db_printf("]\n");
    780 }
    781 static void
    782 db_print_ifa(ifa)
    783 	struct ifaddr *ifa;
    784 {
    785 	if (ifa == 0)
    786 		return;
    787 	db_printf("  ifa_addr=");
    788 	db_print_sa(ifa->ifa_addr);
    789 	db_printf("  ifa_dsta=");
    790 	db_print_sa(ifa->ifa_dstaddr);
    791 	db_printf("  ifa_mask=");
    792 	db_print_sa(ifa->ifa_netmask);
    793 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
    794 			  ifa->ifa_flags,
    795 			  ifa->ifa_refcnt,
    796 			  ifa->ifa_metric);
    797 }
    798 static void
    799 db_print_llinfo(li)
    800 	caddr_t li;
    801 {
    802 	struct llinfo_arp *la;
    803 
    804 	if (li == 0)
    805 		return;
    806 	la = (struct llinfo_arp *)li;
    807 	db_printf("  la_rt=0x%x la_hold=0x%x, la_asked=0x%x\n",
    808 			  la->la_rt, la->la_hold, la->la_asked);
    809 }
    810 /*
    811  * Function to pass to rn_walktree().
    812  * Return non-zero error to abort walk.
    813  */
    814 static int
    815 db_show_radix_node(rn, w)
    816 	struct radix_node *rn;
    817 	void *w;
    818 {
    819 	struct rtentry *rt = (struct rtentry *)rn;
    820 
    821 	db_printf("rtentry=0x%x", rt);
    822 
    823 	db_printf(" flags=0x%x refcnt=%d use=%d expire=%d\n",
    824 			  rt->rt_flags, rt->rt_refcnt,
    825 			  rt->rt_use, rt->rt_expire);
    826 
    827 	db_printf(" key="); db_print_sa(rt_key(rt));
    828 	db_printf(" mask="); db_print_sa(rt_mask(rt));
    829 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
    830 
    831 	db_printf(" ifp=0x%x ", rt->rt_ifp);
    832 	if (rt->rt_ifp)
    833 		db_printf("(%s%d)",
    834 				  rt->rt_ifp->if_name,
    835 				  rt->rt_ifp->if_unit);
    836 	else
    837 		db_printf("(NULL)");
    838 
    839 	db_printf(" ifa=0x%x\n", rt->rt_ifa);
    840 	db_print_ifa(rt->rt_ifa);
    841 
    842 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
    843 
    844 	db_printf(" gwroute=0x%x llinfo=0x%x\n",
    845 			  rt->rt_gwroute, rt->rt_llinfo);
    846 	db_print_llinfo(rt->rt_llinfo);
    847 
    848 	return (0);
    849 }
    850 /*
    851  * Function to print all the route trees.
    852  * Use this from ddb:  "call db_show_arptab"
    853  */
    854 db_show_arptab()
    855 {
    856 	struct radix_node_head *rnh;
    857 	rnh = rt_tables[AF_INET];
    858 	db_printf("Route tree for AF_INET\n");
    859 	if (rnh == NULL) {
    860 		db_printf(" (not initialized)\n");
    861 		return (0);
    862 	}
    863 	rn_walktree(rnh, db_show_radix_node, NULL);
    864 	return (0);
    865 }
    866 #endif
    867 #endif /* INET */
    868