Home | History | Annotate | Line # | Download | only in netinet
if_arp.c revision 1.10
      1 /*
      2  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
      3  * 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	7.13 (Berkeley) 10/31/90
     34  *	$Id: if_arp.c,v 1.10 1994/04/18 23:25:57 mycroft Exp $
     35  */
     36 
     37 /*
     38  * Ethernet address resolution protocol.
     39  * TODO:
     40  *	run at splnet (add ARP protocol intr.)
     41  *	link entries onto hash chains, keep free list
     42  *	add "inuse/lock" bit (or ref. count) along with valid bit
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/malloc.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/socket.h>
     50 #include <sys/time.h>
     51 #include <sys/kernel.h>
     52 #include <sys/errno.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/syslog.h>
     55 
     56 #include <net/if.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 #ifdef GATEWAY
     65 #define	ARPTAB_BSIZ	16		/* bucket size */
     66 #define	ARPTAB_NB	37		/* number of buckets */
     67 #else
     68 #define	ARPTAB_BSIZ	9		/* bucket size */
     69 #define	ARPTAB_NB	19		/* number of buckets */
     70 #endif
     71 #define	ARPTAB_SIZE	(ARPTAB_BSIZ * ARPTAB_NB)
     72 struct	arptab arptab[ARPTAB_SIZE];
     73 int	arptab_size = ARPTAB_SIZE;	/* for arp command */
     74 
     75 /*
     76  * ARP trailer negotiation.  Trailer protocol is not IP specific,
     77  * but ARP request/response use IP addresses.
     78  */
     79 #define	ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL
     80 
     81 #define	ARPTAB_HASH(a) \
     82 	((u_long)(a) % ARPTAB_NB)
     83 
     84 #define	ARPTAB_LOOK(at,addr) { \
     85 	register n; \
     86 	at = &arptab[ARPTAB_HASH(addr) * ARPTAB_BSIZ]; \
     87 	for (n = 0 ; n < ARPTAB_BSIZ ; n++,at++) \
     88 		if (at->at_iaddr.s_addr == addr) \
     89 			break; \
     90 	if (n >= ARPTAB_BSIZ) \
     91 		at = 0; \
     92 }
     93 
     94 /* timer values */
     95 #define	ARPT_AGE	(60*1)	/* aging timer, 1 min. */
     96 #define	ARPT_KILLC	20	/* kill completed entry in 20 mins. */
     97 #define	ARPT_KILLI	3	/* kill incomplete entry in 3 minutes */
     98 
     99 /* revarp state*/
    100 struct in_addr myip;
    101 int myip_initialized = 0;
    102 int revarp_in_progress = 0;
    103 struct ifnet *myip_ifp = NULL;
    104 
    105 extern struct ifnet loif;
    106 
    107 /*
    108  * Timeout routine.  Age arp_tab entries once a minute.
    109  */
    110 /* ARGSUSED */
    111 void
    112 arptimer(caddr_t arg)
    113 {
    114 	register struct arptab *at;
    115 	register i;
    116 
    117 	timeout(arptimer, (caddr_t)0, ARPT_AGE * hz);
    118 	at = &arptab[0];
    119 	for (i = 0; i < ARPTAB_SIZE; i++, at++) {
    120 		if (at->at_flags == 0 || (at->at_flags & ATF_PERM))
    121 			continue;
    122 		if (++at->at_timer < ((at->at_flags&ATF_COM) ?
    123 		    ARPT_KILLC : ARPT_KILLI))
    124 			continue;
    125 		/* timer has expired, clear entry */
    126 		arptfree(at);
    127 	}
    128 }
    129 
    130 /*
    131  * Broadcast an ARP packet, asking who has addr on interface ac.
    132  */
    133 arpwhohas(ac, addr)
    134 	register struct arpcom *ac;
    135 	struct in_addr *addr;
    136 {
    137 	register struct mbuf *m;
    138 	register struct ether_header *eh;
    139 	register struct ether_arp *ea;
    140 	struct sockaddr sa;
    141 
    142 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    143 		return;
    144 	m->m_len = sizeof(*ea);
    145 	m->m_pkthdr.len = sizeof(*ea);
    146 	MH_ALIGN(m, sizeof(*ea));
    147 	ea = mtod(m, struct ether_arp *);
    148 	eh = (struct ether_header *)sa.sa_data;
    149 	bzero((caddr_t)ea, sizeof (*ea));
    150 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
    151 	    sizeof(eh->ether_dhost));
    152 	eh->ether_type = htons(ETHERTYPE_ARP);
    153 	ea->arp_hrd = htons(ARPHRD_ETHER);
    154 	ea->arp_pro = htons(ETHERTYPE_IP);
    155 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
    156 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
    157 	ea->arp_op = htons(ARPOP_REQUEST);
    158 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
    159 	   sizeof(ea->arp_sha));
    160 	bcopy((caddr_t)&ac->ac_ipaddr, (caddr_t)ea->arp_spa,
    161 	   sizeof(ea->arp_spa));
    162 	bcopy((caddr_t)addr, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa));
    163 	sa.sa_family = AF_UNSPEC;
    164 	sa.sa_len = sizeof(sa);
    165 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
    166 }
    167 
    168 int	useloopback = 1;	/* use loopback interface for local traffic */
    169 
    170 /*
    171  * Resolve an IP address into an ethernet address.  If success,
    172  * desten is filled in.  If there is no entry in arptab,
    173  * set one up and broadcast a request for the IP address.
    174  * Hold onto this mbuf and resend it once the address
    175  * is finally resolved.  A return value of 1 indicates
    176  * that desten has been filled in and the packet should be sent
    177  * normally; a 0 return indicates that the packet has been
    178  * taken over here, either now or for later transmission.
    179  *
    180  * We do some (conservative) locking here at splimp, since
    181  * arptab is also altered from input interrupt service (ecintr/ilintr
    182  * calls arpinput when ETHERTYPE_ARP packets come in).
    183  */
    184 arpresolve(ac, m, destip, desten, usetrailers)
    185 	register struct arpcom *ac;
    186 	struct mbuf *m;
    187 	register struct in_addr *destip;
    188 	register u_char *desten;
    189 	int *usetrailers;
    190 {
    191 	register struct arptab *at;
    192 	struct sockaddr_in sin;
    193 	register struct in_ifaddr *ia;
    194 	u_long lna;
    195 	int s;
    196 
    197 	*usetrailers = 0;
    198 	if (m->m_flags & M_BCAST) {	/* broadcast */
    199 		bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten,
    200 		    sizeof(etherbroadcastaddr));
    201 		return (1);
    202 	}
    203 	if (m->m_flags & M_MCAST) {	/* multicast */
    204 		ETHER_MAP_IP_MULTICAST(destip, desten);
    205 		return(1);
    206 	}
    207 	lna = in_lnaof(*destip);
    208 	/* if for us, use software loopback driver if up */
    209 	for (ia = in_ifaddr; ia; ia = ia->ia_next)
    210 	    if ((ia->ia_ifp == &ac->ac_if) &&
    211 		(destip->s_addr == ia->ia_addr.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"
    220 		 * to force traffic out to the hardware.
    221 		 */
    222 		if (useloopback) {
    223 			sin.sin_family = AF_INET;
    224 			sin.sin_addr = *destip;
    225 			(void) looutput(&loif, m, (struct sockaddr *)&sin, 0);
    226 			/*
    227 			 * The packet has already been sent and freed.
    228 			 */
    229 			return (0);
    230 		} else {
    231 			bcopy((caddr_t)ac->ac_enaddr, (caddr_t)desten,
    232 			    sizeof(ac->ac_enaddr));
    233 			return (1);
    234 		}
    235 	}
    236 	s = splimp();
    237 	ARPTAB_LOOK(at, destip->s_addr);
    238 	if (at == 0) {			/* not found */
    239 		if (ac->ac_if.if_flags & IFF_NOARP) {
    240 			bcopy((caddr_t)ac->ac_enaddr, (caddr_t)desten, 3);
    241 			desten[3] = (lna >> 16) & 0x7f;
    242 			desten[4] = (lna >> 8) & 0xff;
    243 			desten[5] = lna & 0xff;
    244 			splx(s);
    245 			return (1);
    246 		} else {
    247 			at = arptnew(destip);
    248 			if (at == 0)
    249 				panic("arpresolve: no free entry");
    250 			at->at_hold = m;
    251 			arpwhohas(ac, destip);
    252 			splx(s);
    253 			return (0);
    254 		}
    255 	}
    256 	at->at_timer = 0;		/* restart the timer */
    257 	if (at->at_flags & ATF_COM) {	/* entry IS complete */
    258 		bcopy((caddr_t)at->at_enaddr, (caddr_t)desten,
    259 		    sizeof(at->at_enaddr));
    260 		if (at->at_flags & ATF_USETRAILERS)
    261 			*usetrailers = 1;
    262 		splx(s);
    263 		return (1);
    264 	}
    265 	/*
    266 	 * There is an arptab entry, but no ethernet address
    267 	 * response yet.  Replace the held mbuf with this
    268 	 * latest one.
    269 	 */
    270 	if (at->at_hold)
    271 		m_freem(at->at_hold);
    272 	at->at_hold = m;
    273 	arpwhohas(ac, destip);		/* ask again */
    274 	splx(s);
    275 	return (0);
    276 }
    277 
    278 void
    279 arpintr()
    280 {
    281 }
    282 
    283 /*
    284  * Called from 10 Mb/s Ethernet interrupt handlers
    285  * when ether packet type ETHERTYPE_ARP
    286  * is received.  Common length and type checks are done here,
    287  * then the protocol-specific routine is called.
    288  */
    289 arpinput(ac, m)
    290 	struct arpcom *ac;
    291 	struct mbuf *m;
    292 {
    293 	register struct arphdr *ar;
    294 
    295 	if (ac->ac_if.if_flags & IFF_NOARP)
    296 		goto out;
    297 	if (m->m_len < sizeof(struct arphdr))
    298 		goto out;
    299 	ar = mtod(m, struct arphdr *);
    300 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
    301 		goto out;
    302 	if (m->m_len < sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
    303 		goto out;
    304 
    305 	switch (ntohs(ar->ar_pro)) {
    306 
    307 	case ETHERTYPE_IP:
    308 	case ETHERTYPE_IPTRAILERS:
    309 		in_arpinput(ac, m);
    310 		return;
    311 
    312 	default:
    313 		break;
    314 	}
    315 out:
    316 	m_freem(m);
    317 }
    318 
    319 /*
    320  * ARP for Internet protocols on 10 Mb/s Ethernet.
    321  * Algorithm is that given in RFC 826.
    322  * In addition, a sanity check is performed on the sender
    323  * protocol address, to catch impersonators.
    324  * We also handle negotiations for use of trailer protocol:
    325  * ARP replies for protocol type ETHERTYPE_TRAIL are sent
    326  * along with IP replies if we want trailers sent to us,
    327  * and also send them in response to IP replies.
    328  * This allows either end to announce the desire to receive
    329  * trailer packets.
    330  * We reply to requests for ETHERTYPE_TRAIL protocol as well,
    331  * but don't normally send requests.
    332  */
    333 in_arpinput(ac, m)
    334 	register struct arpcom *ac;
    335 	struct mbuf *m;
    336 {
    337 	register struct ether_arp *ea;
    338 	struct ether_header *eh;
    339 	register struct arptab *at;  /* same as "merge" flag */
    340 	register struct in_ifaddr *ia;
    341 	struct in_ifaddr *maybe_ia = 0;
    342 	struct mbuf *mcopy = 0;
    343 	struct sockaddr_in sin;
    344 	struct sockaddr sa;
    345 	struct in_addr isaddr, itaddr, myaddr;
    346 	int proto, op, s, completed = 0;
    347 
    348 	ea = mtod(m, struct ether_arp *);
    349 	proto = ntohs(ea->arp_pro);
    350 	op = ntohs(ea->arp_op);
    351 	bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr));
    352 	bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr));
    353 	for (ia = in_ifaddr; ia; ia = ia->ia_next)
    354 		if (ia->ia_ifp == &ac->ac_if) {
    355 			maybe_ia = ia;
    356 			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
    357 			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
    358 				break;
    359 		}
    360 	if (maybe_ia == 0)
    361 		goto out;
    362 	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
    363 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
    364 	    sizeof (ea->arp_sha)))
    365 		goto out;	/* it's from me, ignore it. */
    366 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
    367 	    sizeof (ea->arp_sha))) {
    368 		log(LOG_ERR,
    369 		    "arp: ether address is broadcast for IP address %x!\n",
    370 		    ntohl(isaddr.s_addr));
    371 		goto out;
    372 	}
    373 	if (isaddr.s_addr == myaddr.s_addr) {
    374 		log(LOG_ERR,
    375 		   "duplicate IP address %x!! sent from ethernet address: %s\n",
    376 		   ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha));
    377 		itaddr = myaddr;
    378 		if (op == ARPOP_REQUEST)
    379 			goto reply;
    380 		goto out;
    381 	}
    382 	s = splimp();
    383 	ARPTAB_LOOK(at, isaddr.s_addr);
    384 	if (at) {
    385 		bcopy((caddr_t)ea->arp_sha, (caddr_t)at->at_enaddr,
    386 		    sizeof(ea->arp_sha));
    387 		if ((at->at_flags & ATF_COM) == 0)
    388 			completed = 1;
    389 		at->at_flags |= ATF_COM;
    390 		if (at->at_hold) {
    391 			sin.sin_family = AF_INET;
    392 			sin.sin_addr = isaddr;
    393 			(*ac->ac_if.if_output)(&ac->ac_if, at->at_hold,
    394 				(struct sockaddr *)&sin, (struct rtentry *)0);
    395 			at->at_hold = 0;
    396 		}
    397 	}
    398 	if (at == 0 && itaddr.s_addr == myaddr.s_addr) {
    399 		/* ensure we have a table entry */
    400 		if (at = arptnew(&isaddr)) {
    401 			bcopy((caddr_t)ea->arp_sha, (caddr_t)at->at_enaddr,
    402 			    sizeof(ea->arp_sha));
    403 			completed = 1;
    404 			at->at_flags |= ATF_COM;
    405 		}
    406 	}
    407 	splx(s);
    408 reply:
    409 	switch (proto) {
    410 
    411 	case ETHERTYPE_IPTRAILERS:
    412 		/* partner says trailers are OK */
    413 		if (at)
    414 			at->at_flags |= ATF_USETRAILERS;
    415 		/*
    416 		 * Reply to request iff we want trailers.
    417 		 */
    418 		if (op != ARPOP_REQUEST || ac->ac_if.if_flags & IFF_NOTRAILERS)
    419 			goto out;
    420 		break;
    421 
    422 	case ETHERTYPE_IP:
    423 		/*
    424 		 * Reply if this is an IP request,
    425 		 * or if we want to send a trailer response.
    426 		 * Send the latter only to the IP response
    427 		 * that completes the current ARP entry.
    428 		 */
    429 		if (op != ARPOP_REQUEST &&
    430 		    (completed == 0 || ac->ac_if.if_flags & IFF_NOTRAILERS))
    431 			goto out;
    432 	}
    433 	if (itaddr.s_addr == myaddr.s_addr) {
    434 		/* I am the target */
    435 		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
    436 		    sizeof(ea->arp_sha));
    437 		bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
    438 		    sizeof(ea->arp_sha));
    439 	} else {
    440 		ARPTAB_LOOK(at, itaddr.s_addr);
    441 		if (at == NULL || (at->at_flags & ATF_PUBL) == 0)
    442 			goto out;
    443 		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
    444 		    sizeof(ea->arp_sha));
    445 		bcopy((caddr_t)at->at_enaddr, (caddr_t)ea->arp_sha,
    446 		    sizeof(ea->arp_sha));
    447 	}
    448 
    449 	bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa,
    450 	    sizeof(ea->arp_spa));
    451 	bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa,
    452 	    sizeof(ea->arp_spa));
    453 	ea->arp_op = htons(ARPOP_REPLY);
    454 	/*
    455 	 * If incoming packet was an IP reply,
    456 	 * we are sending a reply for type IPTRAILERS.
    457 	 * If we are sending a reply for type IP
    458 	 * and we want to receive trailers,
    459 	 * send a trailer reply as well.
    460 	 */
    461 	if (op == ARPOP_REPLY)
    462 		ea->arp_pro = htons(ETHERTYPE_IPTRAILERS);
    463 	else if (proto == ETHERTYPE_IP &&
    464 	    (ac->ac_if.if_flags & IFF_NOTRAILERS) == 0)
    465 		mcopy = m_copy(m, 0, (int)M_COPYALL);
    466 	eh = (struct ether_header *)sa.sa_data;
    467 	bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost,
    468 	    sizeof(eh->ether_dhost));
    469 	eh->ether_type = htons(ETHERTYPE_ARP);
    470 	sa.sa_family = AF_UNSPEC;
    471 	sa.sa_len = sizeof(sa);
    472 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
    473 	if (mcopy) {
    474 		ea = mtod(mcopy, struct ether_arp *);
    475 		ea->arp_pro = htons(ETHERTYPE_IPTRAILERS);
    476 		(*ac->ac_if.if_output)(&ac->ac_if,
    477 					mcopy, &sa, (struct rtentry *)0);
    478 	}
    479 	return;
    480 out:
    481 	m_freem(m);
    482 	return;
    483 }
    484 
    485 /*
    486  * Free an arptab entry.
    487  */
    488 arptfree(at)
    489 	register struct arptab *at;
    490 {
    491 	int s = splimp();
    492 
    493 	if (at->at_hold)
    494 		m_freem(at->at_hold);
    495 	at->at_hold = 0;
    496 	at->at_timer = at->at_flags = 0;
    497 	at->at_iaddr.s_addr = 0;
    498 	splx(s);
    499 }
    500 
    501 /*
    502  * Enter a new address in arptab, pushing out the oldest entry
    503  * from the bucket if there is no room.
    504  * This always succeeds since no bucket can be completely filled
    505  * with permanent entries (except from arpioctl when testing whether
    506  * another permanent entry will fit).
    507  * MUST BE CALLED AT SPLIMP.
    508  */
    509 struct arptab *
    510 arptnew(addr)
    511 	struct in_addr *addr;
    512 {
    513 	register n;
    514 	int oldest = -1;
    515 	register struct arptab *at, *ato = NULL;
    516 	static int first = 1;
    517 
    518 	if (first) {
    519 		first = 0;
    520 		timeout(arptimer, (caddr_t)0, hz);
    521 	}
    522 	at = &arptab[ARPTAB_HASH(addr->s_addr) * ARPTAB_BSIZ];
    523 	for (n = 0; n < ARPTAB_BSIZ; n++,at++) {
    524 		if (at->at_flags == 0)
    525 			goto out;	 /* found an empty entry */
    526 		if (at->at_flags & ATF_PERM)
    527 			continue;
    528 		if ((int) at->at_timer > oldest) {
    529 			oldest = at->at_timer;
    530 			ato = at;
    531 		}
    532 	}
    533 	if (ato == NULL)
    534 		return (NULL);
    535 	at = ato;
    536 	arptfree(at);
    537 out:
    538 	at->at_iaddr = *addr;
    539 	at->at_flags = ATF_INUSE;
    540 	return (at);
    541 }
    542 
    543 arpioctl(cmd, data)
    544 	int cmd;
    545 	caddr_t data;
    546 {
    547 	register struct arpreq *ar = (struct arpreq *)data;
    548 	register struct arptab *at;
    549 	register struct sockaddr_in *sin;
    550 	int s;
    551 
    552 	sin = (struct sockaddr_in *)&ar->arp_ha;
    553 #if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN
    554 	if (sin->sin_family == 0 && sin->sin_len < 16)
    555 		sin->sin_family = sin->sin_len;
    556 #endif
    557 	sin->sin_len = sizeof(ar->arp_ha);
    558 	sin = (struct sockaddr_in *)&ar->arp_pa;
    559 #if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN
    560 	if (sin->sin_family == 0 && sin->sin_len < 16)
    561 		sin->sin_family = sin->sin_len;
    562 #endif
    563 	sin->sin_len = sizeof(ar->arp_pa);
    564 	if (ar->arp_pa.sa_family != AF_INET ||
    565 	    ar->arp_ha.sa_family != AF_UNSPEC)
    566 		return (EAFNOSUPPORT);
    567 	s = splimp();
    568 	ARPTAB_LOOK(at, sin->sin_addr.s_addr);
    569 	if (at == NULL) {		/* not found */
    570 		if (cmd != SIOCSARP) {
    571 			splx(s);
    572 			return (ENXIO);
    573 		}
    574 		if (ifa_ifwithnet(&ar->arp_pa) == NULL) {
    575 			splx(s);
    576 			return (ENETUNREACH);
    577 		}
    578 	}
    579 	switch (cmd) {
    580 
    581 	case SIOCSARP:		/* set entry */
    582 		if (at == NULL) {
    583 			at = arptnew(&sin->sin_addr);
    584 			if (at == NULL) {
    585 				splx(s);
    586 				return (EADDRNOTAVAIL);
    587 			}
    588 			if (ar->arp_flags & ATF_PERM) {
    589 			/* never make all entries in a bucket permanent */
    590 				register struct arptab *tat;
    591 
    592 				/* try to re-allocate */
    593 				tat = arptnew(&sin->sin_addr);
    594 				if (tat == NULL) {
    595 					arptfree(at);
    596 					splx(s);
    597 					return (EADDRNOTAVAIL);
    598 				}
    599 				arptfree(tat);
    600 			}
    601 		}
    602 		bcopy((caddr_t)ar->arp_ha.sa_data, (caddr_t)at->at_enaddr,
    603 		    sizeof(at->at_enaddr));
    604 		at->at_flags = ATF_COM | ATF_INUSE |
    605 			(ar->arp_flags & (ATF_PERM|ATF_PUBL|ATF_USETRAILERS));
    606 		at->at_timer = 0;
    607 		break;
    608 
    609 	case SIOCDARP:		/* delete entry */
    610 		arptfree(at);
    611 		break;
    612 
    613 	case SIOCGARP:		/* get entry */
    614 	case OSIOCGARP:
    615 		bcopy((caddr_t)at->at_enaddr, (caddr_t)ar->arp_ha.sa_data,
    616 		    sizeof(at->at_enaddr));
    617 #ifdef COMPAT_43
    618 		if (cmd == OSIOCGARP)
    619 			*(u_short *)&ar->arp_ha = ar->arp_ha.sa_family;
    620 #endif
    621 		ar->arp_flags = at->at_flags;
    622 		break;
    623 	}
    624 	splx(s);
    625 	return (0);
    626 }
    627 
    628 /*
    629  * Called from 10 Mb/s Ethernet interrupt handlers
    630  * when ether packet type ETHERTYPE_REVARP
    631  * is received.  Common length and type checks are done here,
    632  * then the protocol-specific routine is called.
    633  */
    634 void revarpinput(ac, m)
    635 	struct arpcom *ac;
    636 	struct mbuf *m;
    637 {
    638 	struct arphdr *ar;
    639 	int op, s;
    640 
    641 	if (m->m_len < sizeof(struct arphdr))
    642 		goto out;
    643 	ar = mtod(m, struct arphdr *);
    644 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
    645 		goto out;
    646 	if (m->m_len < sizeof(struct arphdr) + 2 * ar->ar_hln +
    647 	    2 * ar->ar_pln)
    648 		goto out;
    649 	switch (ntohs(ar->ar_pro)) {
    650 
    651 	case ETHERTYPE_IP:
    652 	case ETHERTYPE_IPTRAILERS:
    653 		in_revarpinput(ac, m);
    654 		return;
    655 
    656 	default:
    657 		break;
    658 	}
    659 out:
    660 	m_freem(m);
    661 }
    662 
    663 /*
    664  * RARP for Internet protocols on 10 Mb/s Ethernet.
    665  * Algorithm is that given in RFC 903.
    666  * We are only using for bootstrap purposes to get an ip address for one of
    667  * our interfaces.  Thus we support no user-interface.
    668  *
    669  * Since the contents of the RARP reply are specific to the interface that
    670  * sent the request, this code must ensure that they are properly associated.
    671  *
    672  * Note: also supports ARP via RARP packets, per the RFC.
    673  */
    674 in_revarpinput(ac, m)
    675 	struct arpcom *ac;
    676 	struct mbuf *m;
    677 {
    678 	struct ether_arp *ar;
    679 	int op, s;
    680 
    681 	ar = mtod(m, struct ether_arp *);
    682 	op = ntohs(ar->arp_op);
    683 	switch (op) {
    684 	case ARPOP_REQUEST:
    685 	case ARPOP_REPLY:	/* per RFC */
    686 		if (ac->ac_if.if_flags & IFF_NOARP)
    687 			goto out;
    688 		in_arpinput(ac, m);
    689 		return;
    690 		break;
    691 	case REVARP_REPLY:
    692 		break;
    693 	case REVARP_REQUEST:	/* handled by rarpd(8) */
    694 	default:
    695 		goto out;
    696 	}
    697 	if (!revarp_in_progress)
    698 		goto out;
    699 	if ((struct ifnet *) ac != myip_ifp) /* !same interface */
    700 		goto out;
    701 	if (myip_initialized != 0)
    702 		goto out;
    703 	if (bcmp(ar->arp_tha, ac->ac_enaddr, sizeof(ar->arp_tha)))
    704 		goto out;
    705 	bcopy((caddr_t) ar->arp_tpa, (caddr_t) &myip, sizeof(myip));
    706 	myip_initialized = 1;
    707 	wakeup((caddr_t) &myip);
    708  out:
    709 	m_freem(m);
    710 }
    711 
    712 /*
    713  * Send a RARP request for the ip address of the specified interface.
    714  * The request should be RFC 903-compliant.
    715  */
    716 void revarp_request(ifp)
    717 	struct ifnet *ifp;
    718 {
    719 	struct sockaddr sa;
    720 	struct mbuf *m;
    721 	struct ether_header *eh;
    722 	struct ether_arp *ea;
    723 	struct arpcom *ac = (struct arpcom *) ifp;
    724 
    725 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    726 		return;
    727 	m->m_len = sizeof(*ea);
    728 	m->m_pkthdr.len = sizeof(*ea);
    729 	MH_ALIGN(m, sizeof(*ea));
    730 	ea = mtod(m, struct ether_arp *);
    731 	eh = (struct ether_header *) sa.sa_data;
    732 	bzero((caddr_t)ea, sizeof (*ea));
    733 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
    734 	      sizeof(eh->ether_dhost));
    735 	eh->ether_type = htons(ETHERTYPE_REVARP);
    736 	ea->arp_hrd = htons(ARPHRD_ETHER);
    737 	ea->arp_pro = htons(ETHERTYPE_IP);
    738 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
    739 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
    740 	ea->arp_op = htons(REVARP_REQUEST);
    741 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
    742 	   sizeof(ea->arp_sha));
    743 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha,
    744 	   sizeof(ea->arp_tha));
    745 	sa.sa_family = AF_UNSPEC;
    746 	sa.sa_len = sizeof(sa);
    747 	ifp->if_output(ifp, m, &sa, (struct rtentry *)0);
    748 }
    749 
    750 /*
    751  * RARP for the ip address of the specified interface.  Timeout if
    752  * no response is received.
    753  */
    754 int revarp_whoami(in, ifp)
    755 	struct in_addr *in;
    756 	struct ifnet *ifp;
    757 {
    758 	int result, count = 20;
    759 
    760 	if (myip_initialized)
    761 		return EIO;
    762 
    763 	myip_ifp = ifp;
    764 	revarp_in_progress = 1;
    765 	while (count--) {
    766 		revarp_request(ifp);
    767 		result = tsleep((caddr_t) &myip, PSOCK, "revarp", hz/2);
    768 		if (result != EWOULDBLOCK) break;
    769 	}
    770 	revarp_in_progress = 0;
    771 	if (!myip_initialized)
    772 		return ENETUNREACH;
    773 
    774 	bcopy((caddr_t) &myip, in, sizeof(*in));
    775 	return 0;
    776 }
    777