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