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