Home | History | Annotate | Line # | Download | only in netinet
if_arp.c revision 1.9
      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.9 1994/04/18 06:18:16 glass 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 /*
    279  * Called from 10 Mb/s Ethernet interrupt handlers
    280  * when ether packet type ETHERTYPE_ARP
    281  * is received.  Common length and type checks are done here,
    282  * then the protocol-specific routine is called.
    283  */
    284 arpinput(ac, m)
    285 	struct arpcom *ac;
    286 	struct mbuf *m;
    287 {
    288 	register struct arphdr *ar;
    289 
    290 	if (ac->ac_if.if_flags & IFF_NOARP)
    291 		goto out;
    292 	if (m->m_len < sizeof(struct arphdr))
    293 		goto out;
    294 	ar = mtod(m, struct arphdr *);
    295 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
    296 		goto out;
    297 	if (m->m_len < sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln)
    298 		goto out;
    299 
    300 	switch (ntohs(ar->ar_pro)) {
    301 
    302 	case ETHERTYPE_IP:
    303 	case ETHERTYPE_IPTRAILERS:
    304 		in_arpinput(ac, m);
    305 		return;
    306 
    307 	default:
    308 		break;
    309 	}
    310 out:
    311 	m_freem(m);
    312 }
    313 
    314 /*
    315  * ARP for Internet protocols on 10 Mb/s Ethernet.
    316  * Algorithm is that given in RFC 826.
    317  * In addition, a sanity check is performed on the sender
    318  * protocol address, to catch impersonators.
    319  * We also handle negotiations for use of trailer protocol:
    320  * ARP replies for protocol type ETHERTYPE_TRAIL are sent
    321  * along with IP replies if we want trailers sent to us,
    322  * and also send them in response to IP replies.
    323  * This allows either end to announce the desire to receive
    324  * trailer packets.
    325  * We reply to requests for ETHERTYPE_TRAIL protocol as well,
    326  * but don't normally send requests.
    327  */
    328 in_arpinput(ac, m)
    329 	register struct arpcom *ac;
    330 	struct mbuf *m;
    331 {
    332 	register struct ether_arp *ea;
    333 	struct ether_header *eh;
    334 	register struct arptab *at;  /* same as "merge" flag */
    335 	register struct in_ifaddr *ia;
    336 	struct in_ifaddr *maybe_ia = 0;
    337 	struct mbuf *mcopy = 0;
    338 	struct sockaddr_in sin;
    339 	struct sockaddr sa;
    340 	struct in_addr isaddr, itaddr, myaddr;
    341 	int proto, op, s, completed = 0;
    342 
    343 	ea = mtod(m, struct ether_arp *);
    344 	proto = ntohs(ea->arp_pro);
    345 	op = ntohs(ea->arp_op);
    346 	bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr));
    347 	bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr));
    348 	for (ia = in_ifaddr; ia; ia = ia->ia_next)
    349 		if (ia->ia_ifp == &ac->ac_if) {
    350 			maybe_ia = ia;
    351 			if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) ||
    352 			     (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr))
    353 				break;
    354 		}
    355 	if (maybe_ia == 0)
    356 		goto out;
    357 	myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr;
    358 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr,
    359 	    sizeof (ea->arp_sha)))
    360 		goto out;	/* it's from me, ignore it. */
    361 	if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr,
    362 	    sizeof (ea->arp_sha))) {
    363 		log(LOG_ERR,
    364 		    "arp: ether address is broadcast for IP address %x!\n",
    365 		    ntohl(isaddr.s_addr));
    366 		goto out;
    367 	}
    368 	if (isaddr.s_addr == myaddr.s_addr) {
    369 		log(LOG_ERR,
    370 		   "duplicate IP address %x!! sent from ethernet address: %s\n",
    371 		   ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha));
    372 		itaddr = myaddr;
    373 		if (op == ARPOP_REQUEST)
    374 			goto reply;
    375 		goto out;
    376 	}
    377 	s = splimp();
    378 	ARPTAB_LOOK(at, isaddr.s_addr);
    379 	if (at) {
    380 		bcopy((caddr_t)ea->arp_sha, (caddr_t)at->at_enaddr,
    381 		    sizeof(ea->arp_sha));
    382 		if ((at->at_flags & ATF_COM) == 0)
    383 			completed = 1;
    384 		at->at_flags |= ATF_COM;
    385 		if (at->at_hold) {
    386 			sin.sin_family = AF_INET;
    387 			sin.sin_addr = isaddr;
    388 			(*ac->ac_if.if_output)(&ac->ac_if, at->at_hold,
    389 				(struct sockaddr *)&sin, (struct rtentry *)0);
    390 			at->at_hold = 0;
    391 		}
    392 	}
    393 	if (at == 0 && itaddr.s_addr == myaddr.s_addr) {
    394 		/* ensure we have a table entry */
    395 		if (at = arptnew(&isaddr)) {
    396 			bcopy((caddr_t)ea->arp_sha, (caddr_t)at->at_enaddr,
    397 			    sizeof(ea->arp_sha));
    398 			completed = 1;
    399 			at->at_flags |= ATF_COM;
    400 		}
    401 	}
    402 	splx(s);
    403 reply:
    404 	switch (proto) {
    405 
    406 	case ETHERTYPE_IPTRAILERS:
    407 		/* partner says trailers are OK */
    408 		if (at)
    409 			at->at_flags |= ATF_USETRAILERS;
    410 		/*
    411 		 * Reply to request iff we want trailers.
    412 		 */
    413 		if (op != ARPOP_REQUEST || ac->ac_if.if_flags & IFF_NOTRAILERS)
    414 			goto out;
    415 		break;
    416 
    417 	case ETHERTYPE_IP:
    418 		/*
    419 		 * Reply if this is an IP request,
    420 		 * or if we want to send a trailer response.
    421 		 * Send the latter only to the IP response
    422 		 * that completes the current ARP entry.
    423 		 */
    424 		if (op != ARPOP_REQUEST &&
    425 		    (completed == 0 || ac->ac_if.if_flags & IFF_NOTRAILERS))
    426 			goto out;
    427 	}
    428 	if (itaddr.s_addr == myaddr.s_addr) {
    429 		/* I am the target */
    430 		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
    431 		    sizeof(ea->arp_sha));
    432 		bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
    433 		    sizeof(ea->arp_sha));
    434 	} else {
    435 		ARPTAB_LOOK(at, itaddr.s_addr);
    436 		if (at == NULL || (at->at_flags & ATF_PUBL) == 0)
    437 			goto out;
    438 		bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha,
    439 		    sizeof(ea->arp_sha));
    440 		bcopy((caddr_t)at->at_enaddr, (caddr_t)ea->arp_sha,
    441 		    sizeof(ea->arp_sha));
    442 	}
    443 
    444 	bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa,
    445 	    sizeof(ea->arp_spa));
    446 	bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa,
    447 	    sizeof(ea->arp_spa));
    448 	ea->arp_op = htons(ARPOP_REPLY);
    449 	/*
    450 	 * If incoming packet was an IP reply,
    451 	 * we are sending a reply for type IPTRAILERS.
    452 	 * If we are sending a reply for type IP
    453 	 * and we want to receive trailers,
    454 	 * send a trailer reply as well.
    455 	 */
    456 	if (op == ARPOP_REPLY)
    457 		ea->arp_pro = htons(ETHERTYPE_IPTRAILERS);
    458 	else if (proto == ETHERTYPE_IP &&
    459 	    (ac->ac_if.if_flags & IFF_NOTRAILERS) == 0)
    460 		mcopy = m_copy(m, 0, (int)M_COPYALL);
    461 	eh = (struct ether_header *)sa.sa_data;
    462 	bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost,
    463 	    sizeof(eh->ether_dhost));
    464 	eh->ether_type = htons(ETHERTYPE_ARP);
    465 	sa.sa_family = AF_UNSPEC;
    466 	sa.sa_len = sizeof(sa);
    467 	(*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0);
    468 	if (mcopy) {
    469 		ea = mtod(mcopy, struct ether_arp *);
    470 		ea->arp_pro = htons(ETHERTYPE_IPTRAILERS);
    471 		(*ac->ac_if.if_output)(&ac->ac_if,
    472 					mcopy, &sa, (struct rtentry *)0);
    473 	}
    474 	return;
    475 out:
    476 	m_freem(m);
    477 	return;
    478 }
    479 
    480 /*
    481  * Free an arptab entry.
    482  */
    483 arptfree(at)
    484 	register struct arptab *at;
    485 {
    486 	int s = splimp();
    487 
    488 	if (at->at_hold)
    489 		m_freem(at->at_hold);
    490 	at->at_hold = 0;
    491 	at->at_timer = at->at_flags = 0;
    492 	at->at_iaddr.s_addr = 0;
    493 	splx(s);
    494 }
    495 
    496 /*
    497  * Enter a new address in arptab, pushing out the oldest entry
    498  * from the bucket if there is no room.
    499  * This always succeeds since no bucket can be completely filled
    500  * with permanent entries (except from arpioctl when testing whether
    501  * another permanent entry will fit).
    502  * MUST BE CALLED AT SPLIMP.
    503  */
    504 struct arptab *
    505 arptnew(addr)
    506 	struct in_addr *addr;
    507 {
    508 	register n;
    509 	int oldest = -1;
    510 	register struct arptab *at, *ato = NULL;
    511 	static int first = 1;
    512 
    513 	if (first) {
    514 		first = 0;
    515 		timeout(arptimer, (caddr_t)0, hz);
    516 	}
    517 	at = &arptab[ARPTAB_HASH(addr->s_addr) * ARPTAB_BSIZ];
    518 	for (n = 0; n < ARPTAB_BSIZ; n++,at++) {
    519 		if (at->at_flags == 0)
    520 			goto out;	 /* found an empty entry */
    521 		if (at->at_flags & ATF_PERM)
    522 			continue;
    523 		if ((int) at->at_timer > oldest) {
    524 			oldest = at->at_timer;
    525 			ato = at;
    526 		}
    527 	}
    528 	if (ato == NULL)
    529 		return (NULL);
    530 	at = ato;
    531 	arptfree(at);
    532 out:
    533 	at->at_iaddr = *addr;
    534 	at->at_flags = ATF_INUSE;
    535 	return (at);
    536 }
    537 
    538 arpioctl(cmd, data)
    539 	int cmd;
    540 	caddr_t data;
    541 {
    542 	register struct arpreq *ar = (struct arpreq *)data;
    543 	register struct arptab *at;
    544 	register struct sockaddr_in *sin;
    545 	int s;
    546 
    547 	sin = (struct sockaddr_in *)&ar->arp_ha;
    548 #if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN
    549 	if (sin->sin_family == 0 && sin->sin_len < 16)
    550 		sin->sin_family = sin->sin_len;
    551 #endif
    552 	sin->sin_len = sizeof(ar->arp_ha);
    553 	sin = (struct sockaddr_in *)&ar->arp_pa;
    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_pa);
    559 	if (ar->arp_pa.sa_family != AF_INET ||
    560 	    ar->arp_ha.sa_family != AF_UNSPEC)
    561 		return (EAFNOSUPPORT);
    562 	s = splimp();
    563 	ARPTAB_LOOK(at, sin->sin_addr.s_addr);
    564 	if (at == NULL) {		/* not found */
    565 		if (cmd != SIOCSARP) {
    566 			splx(s);
    567 			return (ENXIO);
    568 		}
    569 		if (ifa_ifwithnet(&ar->arp_pa) == NULL) {
    570 			splx(s);
    571 			return (ENETUNREACH);
    572 		}
    573 	}
    574 	switch (cmd) {
    575 
    576 	case SIOCSARP:		/* set entry */
    577 		if (at == NULL) {
    578 			at = arptnew(&sin->sin_addr);
    579 			if (at == NULL) {
    580 				splx(s);
    581 				return (EADDRNOTAVAIL);
    582 			}
    583 			if (ar->arp_flags & ATF_PERM) {
    584 			/* never make all entries in a bucket permanent */
    585 				register struct arptab *tat;
    586 
    587 				/* try to re-allocate */
    588 				tat = arptnew(&sin->sin_addr);
    589 				if (tat == NULL) {
    590 					arptfree(at);
    591 					splx(s);
    592 					return (EADDRNOTAVAIL);
    593 				}
    594 				arptfree(tat);
    595 			}
    596 		}
    597 		bcopy((caddr_t)ar->arp_ha.sa_data, (caddr_t)at->at_enaddr,
    598 		    sizeof(at->at_enaddr));
    599 		at->at_flags = ATF_COM | ATF_INUSE |
    600 			(ar->arp_flags & (ATF_PERM|ATF_PUBL|ATF_USETRAILERS));
    601 		at->at_timer = 0;
    602 		break;
    603 
    604 	case SIOCDARP:		/* delete entry */
    605 		arptfree(at);
    606 		break;
    607 
    608 	case SIOCGARP:		/* get entry */
    609 	case OSIOCGARP:
    610 		bcopy((caddr_t)at->at_enaddr, (caddr_t)ar->arp_ha.sa_data,
    611 		    sizeof(at->at_enaddr));
    612 #ifdef COMPAT_43
    613 		if (cmd == OSIOCGARP)
    614 			*(u_short *)&ar->arp_ha = ar->arp_ha.sa_family;
    615 #endif
    616 		ar->arp_flags = at->at_flags;
    617 		break;
    618 	}
    619 	splx(s);
    620 	return (0);
    621 }
    622 
    623 /*
    624  * Called from 10 Mb/s Ethernet interrupt handlers
    625  * when ether packet type ETHERTYPE_REVARP
    626  * is received.  Common length and type checks are done here,
    627  * then the protocol-specific routine is called.
    628  */
    629 void revarpinput(ac, m)
    630 	struct arpcom *ac;
    631 	struct mbuf *m;
    632 {
    633 	struct arphdr *ar;
    634 	int op, s;
    635 
    636 	if (m->m_len < sizeof(struct arphdr))
    637 		goto out;
    638 	ar = mtod(m, struct arphdr *);
    639 	if (ntohs(ar->ar_hrd) != ARPHRD_ETHER)
    640 		goto out;
    641 	if (m->m_len < sizeof(struct arphdr) + 2 * ar->ar_hln +
    642 	    2 * ar->ar_pln)
    643 		goto out;
    644 	switch (ntohs(ar->ar_pro)) {
    645 
    646 	case ETHERTYPE_IP:
    647 	case ETHERTYPE_IPTRAILERS:
    648 		in_revarpinput(ac, m);
    649 		return;
    650 
    651 	default:
    652 		break;
    653 	}
    654 out:
    655 	m_freem(m);
    656 }
    657 
    658 /*
    659  * RARP for Internet protocols on 10 Mb/s Ethernet.
    660  * Algorithm is that given in RFC 903.
    661  * We are only using for bootstrap purposes to get an ip address for one of
    662  * our interfaces.  Thus we support no user-interface.
    663  *
    664  * Since the contents of the RARP reply are specific to the interface that
    665  * sent the request, this code must ensure that they are properly associated.
    666  *
    667  * Note: also supports ARP via RARP packets, per the RFC.
    668  */
    669 in_revarpinput(ac, m)
    670 	struct arpcom *ac;
    671 	struct mbuf *m;
    672 {
    673 	struct ether_arp *ar;
    674 	int op, s;
    675 
    676 	ar = mtod(m, struct ether_arp *);
    677 	op = ntohs(ar->arp_op);
    678 	switch (op) {
    679 	case ARPOP_REQUEST:
    680 	case ARPOP_REPLY:	/* per RFC */
    681 		if (ac->ac_if.if_flags & IFF_NOARP)
    682 			goto out;
    683 		in_arpinput(ac, m);
    684 		return;
    685 		break;
    686 	case REVARP_REPLY:
    687 		break;
    688 	case REVARP_REQUEST:	/* handled by rarpd(8) */
    689 	default:
    690 		goto out;
    691 	}
    692 	if (!revarp_in_progress)
    693 		goto out;
    694 	if ((struct ifnet *) ac != myip_ifp) /* !same interface */
    695 		goto out;
    696 	if (myip_initialized != 0)
    697 		goto out;
    698 	if (bcmp(ar->arp_tha, ac->ac_enaddr, sizeof(ar->arp_tha)))
    699 		goto out;
    700 	bcopy((caddr_t) ar->arp_tpa, (caddr_t) &myip, sizeof(myip));
    701 	myip_initialized = 1;
    702 	wakeup((caddr_t) &myip);
    703  out:
    704 	m_freem(m);
    705 }
    706 
    707 /*
    708  * Send a RARP request for the ip address of the specified interface.
    709  * The request should be RFC 903-compliant.
    710  */
    711 void revarp_request(ifp)
    712 	struct ifnet *ifp;
    713 {
    714 	struct sockaddr sa;
    715 	struct mbuf *m;
    716 	struct ether_header *eh;
    717 	struct ether_arp *ea;
    718 	struct arpcom *ac = (struct arpcom *) ifp;
    719 
    720 	if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL)
    721 		return;
    722 	m->m_len = sizeof(*ea);
    723 	m->m_pkthdr.len = sizeof(*ea);
    724 	MH_ALIGN(m, sizeof(*ea));
    725 	ea = mtod(m, struct ether_arp *);
    726 	eh = (struct ether_header *) sa.sa_data;
    727 	bzero((caddr_t)ea, sizeof (*ea));
    728 	bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
    729 	      sizeof(eh->ether_dhost));
    730 	eh->ether_type = htons(ETHERTYPE_REVARP);
    731 	ea->arp_hrd = htons(ARPHRD_ETHER);
    732 	ea->arp_pro = htons(ETHERTYPE_IP);
    733 	ea->arp_hln = sizeof(ea->arp_sha);	/* hardware address length */
    734 	ea->arp_pln = sizeof(ea->arp_spa);	/* protocol address length */
    735 	ea->arp_op = htons(REVARP_REQUEST);
    736 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha,
    737 	   sizeof(ea->arp_sha));
    738 	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_tha,
    739 	   sizeof(ea->arp_tha));
    740 	sa.sa_family = AF_UNSPEC;
    741 	sa.sa_len = sizeof(sa);
    742 	ifp->if_output(ifp, m, &sa, (struct rtentry *)0);
    743 }
    744 
    745 /*
    746  * RARP for the ip address of the specified interface.  Timeout if
    747  * no response is received.
    748  */
    749 int revarp_whoami(in, ifp)
    750 	struct in_addr *in;
    751 	struct ifnet *ifp;
    752 {
    753 	int result, count = 20;
    754 
    755 	if (myip_initialized)
    756 		return EIO;
    757 
    758 	myip_ifp = ifp;
    759 	revarp_in_progress = 1;
    760 	while (count--) {
    761 		revarp_request(ifp);
    762 		result = tsleep((caddr_t) &myip, PSOCK, "revarp", hz/2);
    763 		if (result != EWOULDBLOCK) break;
    764 	}
    765 	revarp_in_progress = 0;
    766 	if (!myip_initialized)
    767 		return ENETUNREACH;
    768 
    769 	bcopy((caddr_t) &myip, in, sizeof(*in));
    770 	return 0;
    771 }
    772