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