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