Home | History | Annotate | Line # | Download | only in netinet
if_arp.c revision 1.23
      1 /*	$NetBSD: if_arp.c,v 1.23 1995/04/17 05:32:52 cgd 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
     84 	    __P((struct arpcom *, u_int32_t *, u_int32_t *, u_int8_t *));
     85 static	void arptfree __P((struct llinfo_arp *));
     86 static	void arptimer __P((void *));
     87 static	struct llinfo_arp *arplookup __P((u_int32_t, int, int));
     88 static	void in_arpinput __P((struct mbuf *));
     89 
     90 extern	struct ifnet loif;
     91 struct	llinfo_arp llinfo_arp = {&llinfo_arp, &llinfo_arp};
     92 struct	ifqueue arpintrq = {0, 0, 0, 50};
     93 int	arp_inuse, arp_allocated, arp_intimer;
     94 int	arp_maxtries = 5;
     95 int	useloopback = 1;	/* use loopback interface for local traffic */
     96 int	arpinit_done = 0;
     97 
     98 /* revarp state */
     99 static struct	in_addr myip, srv_ip;
    100 static int	myip_initialized = 0;
    101 static int	revarp_in_progress = 0;
    102 static struct	ifnet *myip_ifp = NULL;
    103 
    104 /*
    105  * Timeout routine.  Age arp_tab entries periodically.
    106  */
    107 /* ARGSUSED */
    108 static void
    109 arptimer(arg)
    110 	void *arg;
    111 {
    112 	int s = splnet();
    113 	register struct llinfo_arp *la = llinfo_arp.la_next;
    114 
    115 	timeout(arptimer, NULL, arpt_prune * hz);
    116 	while (la != &llinfo_arp) {
    117 		register struct rtentry *rt = la->la_rt;
    118 		la = la->la_next;
    119 		if (rt->rt_expire && rt->rt_expire <= time.tv_sec)
    120 			arptfree(la->la_prev); /* timer has expired; clear */
    121 	}
    122 	splx(s);
    123 }
    124 
    125 /*
    126  * Parallel to llc_rtrequest.
    127  */
    128 void
    129 arp_rtrequest(req, rt, sa)
    130 	int req;
    131 	register struct rtentry *rt;
    132 	struct sockaddr *sa;
    133 {
    134 	register struct sockaddr *gate = rt->rt_gateway;
    135 	register struct llinfo_arp *la = (struct llinfo_arp *)rt->rt_llinfo;
    136 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
    137 
    138 	if (!arpinit_done) {
    139 		arpinit_done = 1;
    140 		/*
    141 		 * We generate expiration times from time.tv_sec
    142 		 * so avoid accidently creating permanent routes.
    143 		 */
    144 		if (time.tv_sec == 0) {
    145 			time.tv_sec++;
    146 		}
    147 		timeout(arptimer, (caddr_t)0, hz);
    148 	}
    149 	if (rt->rt_flags & RTF_GATEWAY)
    150 		return;
    151 	switch (req) {
    152 
    153 	case RTM_ADD:
    154 		/*
    155 		 * XXX: If this is a manually added route to interface
    156 		 * such as older version of routed or gated might provide,
    157 		 * restore cloning bit.
    158 		 */
    159 		if ((rt->rt_flags & RTF_HOST) == 0 &&
    160 		    SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
    161 			rt->rt_flags |= RTF_CLONING;
    162 		if (rt->rt_flags & RTF_CLONING) {
    163 			/*
    164 			 * Case 1: This route should come from a route to iface.
    165 			 */
    166 			rt_setgate(rt, rt_key(rt),
    167 					(struct sockaddr *)&null_sdl);
    168 			gate = rt->rt_gateway;
    169 			SDL(gate)->sdl_type = rt->rt_ifp->if_type;
    170 			SDL(gate)->sdl_index = rt->rt_ifp->if_index;
    171 			/*
    172 			 * Give this route an expiration time, even though
    173 			 * it's a "permanent" route, so that routes cloned
    174 			 * from it do not need their expiration time set.
    175 			 */
    176 			rt->rt_expire = time.tv_sec;
    177 			break;
    178 		}
    179 		/* Announce a new entry if requested. */
    180 		if (rt->rt_flags & RTF_ANNOUNCE)
    181 			arprequest((struct arpcom *)rt->rt_ifp,
    182 			    &SIN(rt_key(rt))->sin_addr.s_addr,
    183 			    &SIN(rt_key(rt))->sin_addr.s_addr,
    184 			    (u_char *)LLADDR(SDL(gate)));
    185 		/*FALLTHROUGH*/
    186 	case RTM_RESOLVE:
    187 		if (gate->sa_family != AF_LINK ||
    188 		    gate->sa_len < sizeof(null_sdl)) {
    189 			log(LOG_DEBUG, "arp_rtrequest: bad gateway value");
    190 			break;
    191 		}
    192 		SDL(gate)->sdl_type = rt->rt_ifp->if_type;
    193 		SDL(gate)->sdl_index = rt->rt_ifp->if_index;
    194 		if (la != 0)
    195 			break; /* This happens on a route change */
    196 		/*
    197 		 * Case 2:  This route may come from cloning, or a manual route
    198 		 * add with a LL address.
    199 		 */
    200 		R_Malloc(la, struct llinfo_arp *, sizeof(*la));
    201 		rt->rt_llinfo = (caddr_t)la;
    202 		if (la == 0) {
    203 			log(LOG_DEBUG, "arp_rtrequest: malloc failed\n");
    204 			break;
    205 		}
    206 		arp_inuse++, arp_allocated++;
    207 		Bzero(la, sizeof(*la));
    208 		la->la_rt = rt;
    209 		rt->rt_flags |= RTF_LLINFO;
    210 		insque(la, &llinfo_arp);
    211 		if (SIN(rt_key(rt))->sin_addr.s_addr ==
    212 		    (IA_SIN(rt->rt_ifa))->sin_addr.s_addr) {
    213 		    /*
    214 		     * This test used to be
    215 		     *	if (loif.if_flags & IFF_UP)
    216 		     * It allowed local traffic to be forced
    217 		     * through the hardware by configuring the loopback down.
    218 		     * However, it causes problems during network configuration
    219 		     * for boards that can't receive packets they send.
    220 		     * It is now necessary to clear "useloopback" and remove
    221 		     * the route to force traffic out to the hardware.
    222 		     */
    223 			rt->rt_expire = 0;
    224 			Bcopy(((struct arpcom *)rt->rt_ifp)->ac_enaddr,
    225 				LLADDR(SDL(gate)), SDL(gate)->sdl_alen = 6);
    226 			if (useloopback)
    227 				rt->rt_ifp = &loif;
    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_int32_t *sip, *tip;
    265 	register u_int8_t *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_int32_t 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 	u_long cmd;
    584 	caddr_t data;
    585 {
    586 
    587 	return (EOPNOTSUPP);
    588 }
    589 
    590 void
    591 arp_ifinit(ac, ifa)
    592 	struct arpcom *ac;
    593 	struct ifaddr *ifa;
    594 {
    595 
    596 	ac->ac_ipaddr = IA_SIN(ifa)->sin_addr;
    597 	/* Warn the user if another station has this IP address. */
    598 	arpwhohas(ac, &ac->ac_ipaddr);
    599 	ifa->ifa_rtrequest = arp_rtrequest;
    600 	ifa->ifa_flags |= RTF_CLONING;
    601 }
    602 
    603 /*
    604  * Called from 10 Mb/s Ethernet interrupt handlers
    605  * when ether packet type ETHERTYPE_REVARP
    606  * is received.  Common length and type checks are done here,
    607  * then the protocol-specific routine is called.
    608  */
    609 void
    610 revarpinput(m)
    611 	struct mbuf *m;
    612 {
    613 	struct arphdr *ar;
    614 	int op, s;
    615 
    616 	if (m->m_len < sizeof(struct arphdr))
    617 		goto out;
    618 	ar = mtod(m, struct arphdr *);
    619 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
    620 		goto out;
    621 	if (m->m_len < sizeof(struct arphdr) + 2 * (ar->ar_hln + ar->ar_pln))
    622 		goto out;
    623 	switch (ntohs(ar->ar_pro)) {
    624 
    625 	case ETHERTYPE_IP:
    626 	case ETHERTYPE_IPTRAILERS:
    627 		in_revarpinput(m);
    628 		return;
    629 
    630 	default:
    631 		break;
    632 	}
    633 out:
    634 	m_freem(m);
    635 }
    636 
    637 /*
    638  * RARP for Internet protocols on 10 Mb/s Ethernet.
    639  * Algorithm is that given in RFC 903.
    640  * We are only using for bootstrap purposes to get an ip address for one of
    641  * our interfaces.  Thus we support no user-interface.
    642  *
    643  * Since the contents of the RARP reply are specific to the interface that
    644  * sent the request, this code must ensure that they are properly associated.
    645  *
    646  * Note: also supports ARP via RARP packets, per the RFC.
    647  */
    648 in_revarpinput(m)
    649 	struct mbuf *m;
    650 {
    651 	struct ifnet *ifp;
    652 	struct ether_arp *ar;
    653 	int op, s;
    654 
    655 	ar = mtod(m, struct ether_arp *);
    656 	op = ntohs(ar->arp_op);
    657 	switch (op) {
    658 	case ARPOP_REQUEST:
    659 	case ARPOP_REPLY:	/* per RFC */
    660 		in_arpinput(m);
    661 		return;
    662 	case ARPOP_REVREPLY:
    663 		break;
    664 	case ARPOP_REVREQUEST:	/* handled by rarpd(8) */
    665 	default:
    666 		goto out;
    667 	}
    668 	if (!revarp_in_progress)
    669 		goto out;
    670 	ifp = m->m_pkthdr.rcvif;
    671 	if (ifp != myip_ifp) /* !same interface */
    672 		goto out;
    673 	if (myip_initialized)
    674 		goto wake;
    675 	if (bcmp(ar->arp_tha, ((struct arpcom *)ifp)->ac_enaddr,
    676 	    sizeof(ar->arp_tha)))
    677 		goto out;
    678 	bcopy((caddr_t)ar->arp_spa, (caddr_t)&srv_ip, sizeof(srv_ip));
    679 	bcopy((caddr_t)ar->arp_tpa, (caddr_t)&myip, sizeof(myip));
    680 	myip_initialized = 1;
    681 wake:	/* Do wakeup every time in case it was missed. */
    682 	wakeup((caddr_t)&myip);
    683 
    684 out:
    685 	m_freem(m);
    686 }
    687 
    688 /*
    689  * Send a RARP request for the ip address of the specified interface.
    690  * The request should be RFC 903-compliant.
    691  */
    692 void
    693 revarprequest(ifp)
    694 	struct ifnet *ifp;
    695 {
    696 	struct sockaddr sa;
    697 	struct mbuf *m;
    698 	struct ether_header *eh;
    699 	struct ether_arp *ea;
    700 	struct arpcom *ac = (struct arpcom *)ifp;
    701 
    702 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    703 		return;
    704 	m->m_len = sizeof(*ea);
    705 	m->m_pkthdr.len = sizeof(*ea);
    706 	MH_ALIGN(m, sizeof(*ea));
    707 	ea = mtod(m, struct ether_arp *);
    708 	eh = (struct ether_header *)sa.sa_data;
    709 	bzero((caddr_t)ea, sizeof(*ea));
    710 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
    711 	    sizeof(eh->ether_dhost));
    712 	eh->ether_type = htons(ETHERTYPE_REVARP);
    713 	ea->arp_hrd = htons(ARPHRD_ETHER);
    714 	ea->arp_pro = htons(ETHERTYPE_IP);
    715 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
    716 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
    717 	ea->arp_op = htons(ARPOP_REVREQUEST);
    718 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
    719 	   sizeof(ea->arp_sha));
    720 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha,
    721 	   sizeof(ea->arp_tha));
    722 	sa.sa_family = AF_UNSPEC;
    723 	sa.sa_len = sizeof(sa);
    724 	ifp->if_output(ifp, m, &sa, (struct rtentry *)0);
    725 }
    726 
    727 /*
    728  * RARP for the ip address of the specified interface, but also
    729  * save the ip address of the server that sent the answer.
    730  * Timeout if no response is received.
    731  */
    732 int
    733 revarpwhoarewe(ifp, serv_in, clnt_in)
    734 	struct ifnet *ifp;
    735 	struct in_addr *serv_in;
    736 	struct in_addr *clnt_in;
    737 {
    738 	int result, count = 20;
    739 
    740 	if (myip_initialized)
    741 		return EIO;
    742 
    743 	myip_ifp = ifp;
    744 	revarp_in_progress = 1;
    745 	while (count--) {
    746 		revarprequest(ifp);
    747 		result = tsleep((caddr_t)&myip, PSOCK, "revarp", hz/2);
    748 		if (result != EWOULDBLOCK)
    749 			break;
    750 	}
    751 	revarp_in_progress = 0;
    752 	if (!myip_initialized)
    753 		return ENETUNREACH;
    754 
    755 	bcopy((caddr_t)&srv_ip, serv_in, sizeof(*serv_in));
    756 	bcopy((caddr_t)&myip, clnt_in, sizeof(*clnt_in));
    757 	return 0;
    758 }
    759 
    760 /* For compatibility: only saves interface address. */
    761 int
    762 revarpwhoami(in, ifp)
    763 	struct in_addr *in;
    764 	struct ifnet *ifp;
    765 {
    766 	struct in_addr server;
    767 	return (revarpwhoarewe(ifp, &server, in));
    768 }
    769 
    770 
    772 #ifdef DDB
    773 static void
    774 db_print_sa(sa)
    775 	struct sockaddr *sa;
    776 {
    777 	int len;
    778 	u_char *p;
    779 
    780 	if (sa == 0) {
    781 		db_printf("[NULL]");
    782 		return;
    783 	}
    784 
    785 	p = (u_char*)sa;
    786 	len = sa->sa_len;
    787 	db_printf("[");
    788 	while (len > 0) {
    789 		db_printf("%d", *p);
    790 		p++; len--;
    791 		if (len) db_printf(",");
    792 	}
    793 	db_printf("]\n");
    794 }
    795 static void
    796 db_print_ifa(ifa)
    797 	struct ifaddr *ifa;
    798 {
    799 	if (ifa == 0)
    800 		return;
    801 	db_printf("  ifa_addr=");
    802 	db_print_sa(ifa->ifa_addr);
    803 	db_printf("  ifa_dsta=");
    804 	db_print_sa(ifa->ifa_dstaddr);
    805 	db_printf("  ifa_mask=");
    806 	db_print_sa(ifa->ifa_netmask);
    807 	db_printf("  flags=0x%x,refcnt=%d,metric=%d\n",
    808 			  ifa->ifa_flags,
    809 			  ifa->ifa_refcnt,
    810 			  ifa->ifa_metric);
    811 }
    812 static void
    813 db_print_llinfo(li)
    814 	caddr_t li;
    815 {
    816 	struct llinfo_arp *la;
    817 
    818 	if (li == 0)
    819 		return;
    820 	la = (struct llinfo_arp *)li;
    821 	db_printf("  la_rt=0x%x la_hold=0x%x, la_asked=0x%x\n",
    822 			  la->la_rt, la->la_hold, la->la_asked);
    823 }
    824 /*
    825  * Function to pass to rn_walktree().
    826  * Return non-zero error to abort walk.
    827  */
    828 static int
    829 db_show_radix_node(rn, w)
    830 	struct radix_node *rn;
    831 	void *w;
    832 {
    833 	struct rtentry *rt = (struct rtentry *)rn;
    834 
    835 	db_printf("rtentry=0x%x", rt);
    836 
    837 	db_printf(" flags=0x%x refcnt=%d use=%d expire=%d\n",
    838 			  rt->rt_flags, rt->rt_refcnt,
    839 			  rt->rt_use, rt->rt_expire);
    840 
    841 	db_printf(" key="); db_print_sa(rt_key(rt));
    842 	db_printf(" mask="); db_print_sa(rt_mask(rt));
    843 	db_printf(" gw="); db_print_sa(rt->rt_gateway);
    844 
    845 	db_printf(" ifp=0x%lx ", rt->rt_ifp);
    846 	if (rt->rt_ifp)
    847 		db_printf("(%s%d)",
    848 				  rt->rt_ifp->if_name,
    849 				  rt->rt_ifp->if_unit);
    850 	else
    851 		db_printf("(NULL)");
    852 
    853 	db_printf(" ifa=0x%lx\n", rt->rt_ifa);
    854 	db_print_ifa(rt->rt_ifa);
    855 
    856 	db_printf(" genmask="); db_print_sa(rt->rt_genmask);
    857 
    858 	db_printf(" gwroute=0x%x llinfo=0x%x\n",
    859 			  rt->rt_gwroute, rt->rt_llinfo);
    860 	db_print_llinfo(rt->rt_llinfo);
    861 
    862 	return (0);
    863 }
    864 /*
    865  * Function to print all the route trees.
    866  * Use this from ddb:  "call db_show_arptab"
    867  */
    868 db_show_arptab()
    869 {
    870 	struct radix_node_head *rnh;
    871 	rnh = rt_tables[AF_INET];
    872 	db_printf("Route tree for AF_INET\n");
    873 	if (rnh == NULL) {
    874 		db_printf(" (not initialized)\n");
    875 		return (0);
    876 	}
    877 	rn_walktree(rnh, db_show_radix_node, NULL);
    878 	return (0);
    879 }
    880 #endif
    881 #endif /* INET */
    882