Home | History | Annotate | Line # | Download | only in libsa
arp.c revision 1.8
      1  1.8      gwr /*	$NetBSD: arp.c,v 1.8 1995/09/13 15:06:12 gwr Exp $	*/
      2  1.3      cgd 
      3  1.1   brezak /*
      4  1.1   brezak  * Copyright (c) 1992 Regents of the University of California.
      5  1.1   brezak  * All rights reserved.
      6  1.1   brezak  *
      7  1.1   brezak  * This software was developed by the Computer Systems Engineering group
      8  1.1   brezak  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  1.1   brezak  * contributed to Berkeley.
     10  1.1   brezak  *
     11  1.1   brezak  * Redistribution and use in source and binary forms, with or without
     12  1.1   brezak  * modification, are permitted provided that the following conditions
     13  1.1   brezak  * are met:
     14  1.1   brezak  * 1. Redistributions of source code must retain the above copyright
     15  1.1   brezak  *    notice, this list of conditions and the following disclaimer.
     16  1.1   brezak  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1   brezak  *    notice, this list of conditions and the following disclaimer in the
     18  1.1   brezak  *    documentation and/or other materials provided with the distribution.
     19  1.1   brezak  * 3. All advertising materials mentioning features or use of this software
     20  1.1   brezak  *    must display the following acknowledgement:
     21  1.1   brezak  *	This product includes software developed by the University of
     22  1.1   brezak  *	California, Lawrence Berkeley Laboratory and its contributors.
     23  1.1   brezak  * 4. Neither the name of the University nor the names of its contributors
     24  1.1   brezak  *    may be used to endorse or promote products derived from this software
     25  1.1   brezak  *    without specific prior written permission.
     26  1.1   brezak  *
     27  1.1   brezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  1.1   brezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  1.1   brezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  1.1   brezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  1.1   brezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  1.1   brezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  1.1   brezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  1.1   brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  1.1   brezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  1.1   brezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  1.1   brezak  * SUCH DAMAGE.
     38  1.1   brezak  *
     39  1.3      cgd  * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp  (LBL)
     40  1.1   brezak  */
     41  1.1   brezak 
     42  1.1   brezak #include <sys/types.h>
     43  1.1   brezak #include <sys/socket.h>
     44  1.1   brezak #include <net/if.h>
     45  1.1   brezak #include <netinet/in.h>
     46  1.1   brezak 
     47  1.1   brezak #include <netinet/if_ether.h>
     48  1.1   brezak #include <netinet/in_systm.h>
     49  1.1   brezak 
     50  1.1   brezak #include <string.h>
     51  1.1   brezak 
     52  1.1   brezak #include "stand.h"
     53  1.1   brezak #include "net.h"
     54  1.5      gwr 
     55  1.1   brezak /* Cache stuff */
     56  1.1   brezak #define ARP_NUM 8			/* need at most 3 arp entries */
     57  1.1   brezak 
     58  1.1   brezak static struct arp_list {
     59  1.1   brezak 	n_long	addr;
     60  1.1   brezak 	u_char	ea[6];
     61  1.1   brezak } arp_list[ARP_NUM] = {
     62  1.1   brezak 	{ INADDR_BROADCAST, BA }
     63  1.1   brezak };
     64  1.1   brezak static	int arp_num = 1;
     65  1.1   brezak 
     66  1.1   brezak /* Local forwards */
     67  1.4  mycroft static	size_t arpsend __P((struct iodesc *, void *, size_t));
     68  1.4  mycroft static	size_t arprecv __P((struct iodesc *, void *, size_t, time_t));
     69  1.1   brezak 
     70  1.1   brezak /* Broadcast an ARP packet, asking who has addr on interface d */
     71  1.1   brezak u_char *
     72  1.1   brezak arpwhohas(d, addr)
     73  1.1   brezak 	register struct iodesc *d;
     74  1.1   brezak 	n_long addr;
     75  1.1   brezak {
     76  1.1   brezak 	register int i;
     77  1.1   brezak 	register struct ether_arp *ah;
     78  1.1   brezak 	register struct arp_list *al;
     79  1.1   brezak 	struct {
     80  1.5      gwr 		u_char header[ETHER_SIZE];
     81  1.5      gwr 		struct {
     82  1.5      gwr 			struct ether_arp arp;
     83  1.5      gwr 			u_char pad[18]; 	/* 60 - sizeof(arp) */
     84  1.5      gwr 		} data;
     85  1.1   brezak 	} wbuf;
     86  1.4  mycroft 	struct {
     87  1.5      gwr 		u_char header[ETHER_SIZE];
     88  1.5      gwr 		struct {
     89  1.5      gwr 			struct ether_arp arp;
     90  1.5      gwr 			u_char pad[24]; 	/* extra space */
     91  1.5      gwr 		} data;
     92  1.1   brezak 	} rbuf;
     93  1.1   brezak 
     94  1.1   brezak #ifdef ARP_DEBUG
     95  1.1   brezak  	if (debug)
     96  1.1   brezak  	    printf("arpwhohas: called for %s\n", intoa(addr));
     97  1.1   brezak #endif
     98  1.1   brezak 	/* Try for cached answer first */
     99  1.1   brezak 	for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
    100  1.1   brezak 		if (addr == al->addr)
    101  1.1   brezak 			return (al->ea);
    102  1.1   brezak 
    103  1.1   brezak 	/* Don't overflow cache */
    104  1.1   brezak 	if (arp_num > ARP_NUM - 1)
    105  1.1   brezak 		panic("arpwhohas: overflowed arp_list!");
    106  1.1   brezak 
    107  1.1   brezak #ifdef ARP_DEBUG
    108  1.1   brezak  	if (debug)
    109  1.4  mycroft 		printf("arpwhohas: not cached\n");
    110  1.1   brezak #endif
    111  1.4  mycroft 
    112  1.5      gwr 	bzero((char*)&wbuf.data, sizeof(wbuf.data));
    113  1.5      gwr 	ah = &wbuf.data.arp;
    114  1.1   brezak 	ah->arp_hrd = htons(ARPHRD_ETHER);
    115  1.1   brezak 	ah->arp_pro = htons(ETHERTYPE_IP);
    116  1.1   brezak 	ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */
    117  1.1   brezak 	ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */
    118  1.1   brezak 	ah->arp_op = htons(ARPOP_REQUEST);
    119  1.1   brezak 	MACPY(d->myea, ah->arp_sha);
    120  1.1   brezak 	bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa));
    121  1.1   brezak 	bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa));
    122  1.1   brezak 
    123  1.1   brezak 	/* Store ip address in cache */
    124  1.1   brezak 	al->addr = addr;
    125  1.1   brezak 
    126  1.4  mycroft 	(void)sendrecv(d,
    127  1.5      gwr 	    arpsend, &wbuf.data, sizeof(wbuf.data),
    128  1.5      gwr 	    arprecv, &rbuf.data, sizeof(rbuf.data));
    129  1.1   brezak 
    130  1.1   brezak 	/* Store ethernet address in cache */
    131  1.5      gwr 	MACPY(rbuf.data.arp.arp_sha, al->ea);
    132  1.1   brezak 	++arp_num;
    133  1.4  mycroft 
    134  1.1   brezak 	return (al->ea);
    135  1.1   brezak }
    136  1.1   brezak 
    137  1.4  mycroft static size_t
    138  1.1   brezak arpsend(d, pkt, len)
    139  1.1   brezak 	register struct iodesc *d;
    140  1.1   brezak 	register void *pkt;
    141  1.4  mycroft 	register size_t len;
    142  1.1   brezak {
    143  1.4  mycroft 
    144  1.1   brezak #ifdef ARP_DEBUG
    145  1.1   brezak  	if (debug)
    146  1.4  mycroft 		printf("arpsend: called\n");
    147  1.1   brezak #endif
    148  1.4  mycroft 
    149  1.1   brezak 	return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP));
    150  1.1   brezak }
    151  1.1   brezak 
    152  1.1   brezak /* Returns 0 if this is the packet we're waiting for else -1 (and errno == 0) */
    153  1.4  mycroft static size_t
    154  1.4  mycroft arprecv(d, pkt, len, tleft)
    155  1.1   brezak 	register struct iodesc *d;
    156  1.1   brezak 	register void *pkt;
    157  1.4  mycroft 	register size_t len;
    158  1.4  mycroft 	time_t tleft;
    159  1.1   brezak {
    160  1.1   brezak 	register struct ether_arp *ah;
    161  1.7  thorpej 	u_int16_t etype;	/* host order */
    162  1.1   brezak 
    163  1.1   brezak #ifdef ARP_DEBUG
    164  1.1   brezak  	if (debug)
    165  1.5      gwr 		printf("arprecv: ");
    166  1.1   brezak #endif
    167  1.1   brezak 
    168  1.7  thorpej 	len = readether(d, pkt, len, tleft, &etype);
    169  1.5      gwr 	if (len == -1 || len < sizeof(struct ether_arp)) {
    170  1.5      gwr #ifdef ARP_DEBUG
    171  1.5      gwr 		if (debug)
    172  1.5      gwr 			printf("bad len=%d\n", len);
    173  1.5      gwr #endif
    174  1.4  mycroft 		goto bad;
    175  1.5      gwr 	}
    176  1.1   brezak 
    177  1.7  thorpej 	if (etype != ETHERTYPE_ARP) {
    178  1.1   brezak #ifdef ARP_DEBUG
    179  1.1   brezak 		if (debug)
    180  1.7  thorpej 			printf("not arp type=%d\n", etype);
    181  1.1   brezak #endif
    182  1.4  mycroft 		goto bad;
    183  1.1   brezak 	}
    184  1.7  thorpej 
    185  1.7  thorpej 	/* Ethernet address now checked in readether() */
    186  1.7  thorpej 
    187  1.7  thorpej 	ah = (struct ether_arp *)pkt;
    188  1.7  thorpej 	if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
    189  1.7  thorpej 	    ah->arp_pro != htons(ETHERTYPE_IP) ||
    190  1.7  thorpej 	    ah->arp_hln != sizeof(ah->arp_sha) ||
    191  1.7  thorpej 	    ah->arp_pln != sizeof(ah->arp_spa) )
    192  1.7  thorpej 	{
    193  1.1   brezak #ifdef ARP_DEBUG
    194  1.1   brezak 		if (debug)
    195  1.7  thorpej 			printf("bad hrd/pro/hln/pln\n")
    196  1.1   brezak #endif
    197  1.4  mycroft 		goto bad;
    198  1.1   brezak 	}
    199  1.1   brezak 
    200  1.7  thorpej 	if (ah->arp_op == htons(ARPOP_REQUEST)) {
    201  1.7  thorpej 		arp_reply(d, ah);
    202  1.7  thorpej 		goto bad;
    203  1.7  thorpej 	}
    204  1.7  thorpej 
    205  1.7  thorpej 	if (ah->arp_op != htons(ARPOP_REPLY)) {
    206  1.1   brezak #ifdef ARP_DEBUG
    207  1.1   brezak 		if (debug)
    208  1.7  thorpej 			printf("not ARP reply\n");
    209  1.1   brezak #endif
    210  1.4  mycroft 		goto bad;
    211  1.1   brezak 	}
    212  1.7  thorpej 
    213  1.1   brezak 	if (bcmp(&arp_list[arp_num].addr, ah->arp_spa, sizeof(long)) != 0 ||
    214  1.1   brezak 	    bcmp(&d->myip, ah->arp_tpa, sizeof(d->myip)) != 0) {
    215  1.1   brezak #ifdef ARP_DEBUG
    216  1.1   brezak 		if (debug)
    217  1.5      gwr 			printf("already cached??\n");
    218  1.1   brezak #endif
    219  1.4  mycroft 		goto bad;
    220  1.1   brezak 	}
    221  1.1   brezak 
    222  1.5      gwr #ifdef ARP_DEBUG
    223  1.5      gwr  	if (debug)
    224  1.5      gwr 		printf("cacheing %s --> %s\n",
    225  1.5      gwr 			   intoa(ah->arp_spa),
    226  1.5      gwr 			   ether_sprintf(ah->arp_sha));
    227  1.5      gwr #endif
    228  1.1   brezak 	return (0);
    229  1.4  mycroft 
    230  1.4  mycroft bad:
    231  1.4  mycroft 	errno = 0;
    232  1.4  mycroft 	return (-1);
    233  1.7  thorpej }
    234  1.7  thorpej 
    235  1.7  thorpej /*
    236  1.7  thorpej  * Convert an ARP request into a reply and send it.
    237  1.8      gwr  * Notes:  Re-uses buffer.  Pad to length = 46.
    238  1.7  thorpej  */
    239  1.7  thorpej void
    240  1.7  thorpej arp_reply(d, pkt)
    241  1.7  thorpej 	register struct iodesc *d;
    242  1.7  thorpej 	register void *pkt;		/* the request */
    243  1.7  thorpej {
    244  1.7  thorpej 	struct ether_arp *arp = pkt;
    245  1.7  thorpej 
    246  1.7  thorpej 	if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
    247  1.7  thorpej 	    arp->arp_pro != htons(ETHERTYPE_IP) ||
    248  1.7  thorpej 	    arp->arp_hln != sizeof(arp->arp_sha) ||
    249  1.7  thorpej 	    arp->arp_pln != sizeof(arp->arp_spa) )
    250  1.7  thorpej 	{
    251  1.7  thorpej #ifdef ARP_DEBUG
    252  1.7  thorpej 		if (debug)
    253  1.7  thorpej 			printf("arp_reply: bad hrd/pro/hln/pln\n")
    254  1.7  thorpej #endif
    255  1.7  thorpej 		return;
    256  1.7  thorpej 	}
    257  1.7  thorpej 
    258  1.7  thorpej 	if (arp->arp_op != htons(ARPOP_REQUEST)) {
    259  1.7  thorpej #ifdef ARP_DEBUG
    260  1.7  thorpej 		if (debug)
    261  1.7  thorpej 			printf("arp_reply: not request!\n");
    262  1.7  thorpej #endif
    263  1.7  thorpej 		return;
    264  1.7  thorpej 	}
    265  1.7  thorpej 
    266  1.7  thorpej 	/* If we are not the target, ignore the request. */
    267  1.7  thorpej 	if (bcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
    268  1.7  thorpej 		return;
    269  1.7  thorpej 
    270  1.7  thorpej #ifdef ARP_DEBUG
    271  1.7  thorpej 	if (debug) {
    272  1.7  thorpej 		printf("arp_reply: to %s\n",
    273  1.7  thorpej 		       ether_sprintf(arp->arp_sha));
    274  1.7  thorpej 	}
    275  1.7  thorpej #endif
    276  1.7  thorpej 
    277  1.7  thorpej 	arp->arp_op = htons(ARPOP_REPLY);
    278  1.7  thorpej 	/* source becomes target */
    279  1.7  thorpej 	bcopy(arp->arp_sha, arp->arp_tha, sizeof(arp->arp_tha));
    280  1.7  thorpej 	bcopy(arp->arp_spa, arp->arp_tpa, sizeof(arp->arp_tpa));
    281  1.7  thorpej 	/* here becomes source */
    282  1.7  thorpej 	bcopy(d->myea,  arp->arp_sha, sizeof(arp->arp_sha));
    283  1.7  thorpej 	bcopy(&d->myip, arp->arp_spa, sizeof(arp->arp_spa));
    284  1.7  thorpej 
    285  1.7  thorpej 	/*
    286  1.7  thorpej 	 * No need to get fancy here.  If the send fails, the
    287  1.7  thorpej 	 * requestor will just ask again.
    288  1.7  thorpej 	 */
    289  1.8      gwr 	(void) sendether(d, pkt, sizeof(*arp) + 18,
    290  1.8      gwr 	                 arp->arp_tha, ETHERTYPE_ARP);
    291  1.1   brezak }
    292