Home | History | Annotate | Line # | Download | only in libsa
arp.c revision 1.9
      1 /*	$NetBSD: arp.c,v 1.9 1995/09/14 23:45:21 pk 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	ssize_t arpsend __P((struct iodesc *, void *, size_t));
     68 static	ssize_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 ssize_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 ssize_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 ssize_t n;
    161 	register struct ether_arp *ah;
    162 	u_int16_t etype;	/* host order */
    163 
    164 #ifdef ARP_DEBUG
    165  	if (debug)
    166 		printf("arprecv: ");
    167 #endif
    168 
    169 	n = readether(d, pkt, len, tleft, &etype);
    170 	if (n == -1 || n < sizeof(struct ether_arp)) {
    171 #ifdef ARP_DEBUG
    172 		if (debug)
    173 			printf("bad len=%d\n", n);
    174 #endif
    175 		goto bad;
    176 	}
    177 
    178 	if (etype != ETHERTYPE_ARP) {
    179 #ifdef ARP_DEBUG
    180 		if (debug)
    181 			printf("not arp type=%d\n", etype);
    182 #endif
    183 		goto bad;
    184 	}
    185 
    186 	/* Ethernet address now checked in readether() */
    187 
    188 	ah = (struct ether_arp *)pkt;
    189 	if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
    190 	    ah->arp_pro != htons(ETHERTYPE_IP) ||
    191 	    ah->arp_hln != sizeof(ah->arp_sha) ||
    192 	    ah->arp_pln != sizeof(ah->arp_spa) )
    193 	{
    194 #ifdef ARP_DEBUG
    195 		if (debug)
    196 			printf("bad hrd/pro/hln/pln\n")
    197 #endif
    198 		goto bad;
    199 	}
    200 
    201 	if (ah->arp_op == htons(ARPOP_REQUEST)) {
    202 		arp_reply(d, ah);
    203 		goto bad;
    204 	}
    205 
    206 	if (ah->arp_op != htons(ARPOP_REPLY)) {
    207 #ifdef ARP_DEBUG
    208 		if (debug)
    209 			printf("not ARP reply\n");
    210 #endif
    211 		goto bad;
    212 	}
    213 
    214 	if (bcmp(&arp_list[arp_num].addr, ah->arp_spa, sizeof(long)) != 0 ||
    215 	    bcmp(&d->myip, ah->arp_tpa, sizeof(d->myip)) != 0) {
    216 #ifdef ARP_DEBUG
    217 		if (debug)
    218 			printf("already cached??\n");
    219 #endif
    220 		goto bad;
    221 	}
    222 
    223 #ifdef ARP_DEBUG
    224  	if (debug)
    225 		printf("cacheing %s --> %s\n",
    226 			   intoa(ah->arp_spa),
    227 			   ether_sprintf(ah->arp_sha));
    228 #endif
    229 	return (n);
    230 
    231 bad:
    232 	errno = 0;
    233 	return (-1);
    234 }
    235 
    236 /*
    237  * Convert an ARP request into a reply and send it.
    238  * Notes:  Re-uses buffer.  Pad to length = 46.
    239  */
    240 void
    241 arp_reply(d, pkt)
    242 	register struct iodesc *d;
    243 	register void *pkt;		/* the request */
    244 {
    245 	struct ether_arp *arp = pkt;
    246 
    247 	if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
    248 	    arp->arp_pro != htons(ETHERTYPE_IP) ||
    249 	    arp->arp_hln != sizeof(arp->arp_sha) ||
    250 	    arp->arp_pln != sizeof(arp->arp_spa) )
    251 	{
    252 #ifdef ARP_DEBUG
    253 		if (debug)
    254 			printf("arp_reply: bad hrd/pro/hln/pln\n")
    255 #endif
    256 		return;
    257 	}
    258 
    259 	if (arp->arp_op != htons(ARPOP_REQUEST)) {
    260 #ifdef ARP_DEBUG
    261 		if (debug)
    262 			printf("arp_reply: not request!\n");
    263 #endif
    264 		return;
    265 	}
    266 
    267 	/* If we are not the target, ignore the request. */
    268 	if (bcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
    269 		return;
    270 
    271 #ifdef ARP_DEBUG
    272 	if (debug) {
    273 		printf("arp_reply: to %s\n",
    274 		       ether_sprintf(arp->arp_sha));
    275 	}
    276 #endif
    277 
    278 	arp->arp_op = htons(ARPOP_REPLY);
    279 	/* source becomes target */
    280 	bcopy(arp->arp_sha, arp->arp_tha, sizeof(arp->arp_tha));
    281 	bcopy(arp->arp_spa, arp->arp_tpa, sizeof(arp->arp_tpa));
    282 	/* here becomes source */
    283 	bcopy(d->myea,  arp->arp_sha, sizeof(arp->arp_sha));
    284 	bcopy(&d->myip, arp->arp_spa, sizeof(arp->arp_spa));
    285 
    286 	/*
    287 	 * No need to get fancy here.  If the send fails, the
    288 	 * requestor will just ask again.
    289 	 */
    290 	(void) sendether(d, pkt, sizeof(*arp) + 18,
    291 	                 arp->arp_tha, ETHERTYPE_ARP);
    292 }
    293