xref: /netbsd-src/sys/lib/libsa/arp.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: arp.c,v 1.4 1995/02/20 11:04:00 mycroft 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	ether_arp warp;
82 	} wbuf;
83 	struct {
84 		u_char	header[ETHER_SIZE];
85 		struct	ether_arp rarp;
86 	} rbuf;
87 
88 #ifdef ARP_DEBUG
89  	if (debug)
90  	    printf("arpwhohas: called for %s\n", intoa(addr));
91 #endif
92 	/* Try for cached answer first */
93 	for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
94 		if (addr == al->addr)
95 			return (al->ea);
96 
97 	/* Don't overflow cache */
98 	if (arp_num > ARP_NUM - 1)
99 		panic("arpwhohas: overflowed arp_list!");
100 
101 #ifdef ARP_DEBUG
102  	if (debug)
103 		printf("arpwhohas: not cached\n");
104 #endif
105 
106 	ah = &wbuf.warp;
107 	bzero(ah, sizeof(*ah));
108 
109 	ah->arp_hrd = htons(ARPHRD_ETHER);
110 	ah->arp_pro = htons(ETHERTYPE_IP);
111 	ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */
112 	ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */
113 	ah->arp_op = htons(ARPOP_REQUEST);
114 	MACPY(d->myea, ah->arp_sha);
115 	bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa));
116 	bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa));
117 
118 	/* Store ip address in cache */
119 	al->addr = addr;
120 
121 	(void)sendrecv(d,
122 	    arpsend, ah, sizeof(*ah),
123 	    arprecv, &rbuf.rarp, sizeof(rbuf.rarp));
124 
125 	/* Store ethernet address in cache */
126 	MACPY(rbuf.rarp.arp_sha, al->ea);
127 	++arp_num;
128 
129 #ifdef ARP_DEBUG
130  	if (debug)
131 		printf("arpwhohas: cacheing %s --> %s\n", intoa(addr), ether_sprintf(al->ea));
132 #endif
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_header *eh;
161 	register struct ether_arp *ah;
162 
163 #ifdef ARP_DEBUG
164  	if (debug)
165  		printf("arprecv: called\n");
166 #endif
167 
168 	len = readether(d, pkt, len, tleft);
169 	if (len == -1 || len < sizeof(struct ether_arp))
170 		goto bad;
171 
172 	eh = (struct ether_header *)pkt - 1;
173 	/* Must be to us */
174 	if (bcmp(d->myea, eh->ether_dhost, 6) != 0 &&
175 	    bcmp(bcea, eh->ether_dhost, 6) != 0) {
176 #ifdef ARP_DEBUG
177 		if (debug)
178 			printf("arprecv: not ours %s\n", ether_sprintf(eh->ether_dhost));
179 #endif
180 		goto bad;
181 	}
182 	if (ntohs(eh->ether_type) != ETHERTYPE_ARP) {
183 #ifdef ARP_DEBUG
184 		if (debug)
185 			printf("arprecv: not arp %d\n", ntohs(eh->ether_type));
186 #endif
187 		goto bad;
188 	}
189 
190 	ah = (struct ether_arp *)pkt;
191 	HTONS(ah->arp_hrd);
192 	HTONS(ah->arp_pro);
193 	HTONS(ah->arp_op);
194 	if (ah->arp_hrd != ARPHRD_ETHER || ah->arp_pro != ETHERTYPE_IP ||
195 	    ah->arp_hln != sizeof(ah->arp_sha) ||
196 	    ah->arp_pln != sizeof(ah->arp_spa) || ah->arp_op != ARPOP_REPLY) {
197 #ifdef ARP_DEBUG
198 		if (debug)
199 			printf("arprecv: not valid reply\n");
200 #endif
201 		goto bad;
202 	}
203 	if (bcmp(&arp_list[arp_num].addr, ah->arp_spa, sizeof(long)) != 0 ||
204 	    bcmp(&d->myip, ah->arp_tpa, sizeof(d->myip)) != 0) {
205 #ifdef ARP_DEBUG
206 		if (debug)
207 			printf("arprecv: already cached??\n");
208 #endif
209 		goto bad;
210 	}
211 
212 	return (0);
213 
214 bad:
215 	errno = 0;
216 	return (-1);
217 }
218