arp.c revision 1.15.6.1 1 /* $NetBSD: arp.c,v 1.15.6.1 1997/03/07 21:36:08 is 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/in_systm.h>
48
49 #include <string.h>
50
51 #include "if_ether.h"
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 struct arp_list {
59 struct in_addr addr;
60 u_char ea[6];
61 } arp_list[ARP_NUM] = {
62 /* XXX - net order `INADDR_BROADCAST' must be a constant */
63 { {0xffffffff}, BA }
64 };
65 int arp_num = 1;
66
67 /* Local forwards */
68 static ssize_t arpsend __P((struct iodesc *, void *, size_t));
69 static ssize_t arprecv __P((struct iodesc *, void *, size_t, time_t));
70
71 /* Broadcast an ARP packet, asking who has addr on interface d */
72 u_char *
73 arpwhohas(d, addr)
74 register struct iodesc *d;
75 struct in_addr addr;
76 {
77 register int i;
78 register struct ether_arp *ah;
79 register struct arp_list *al;
80 struct {
81 struct ether_header eh;
82 struct {
83 struct ether_arp arp;
84 u_char pad[18]; /* 60 - sizeof(...) */
85 } data;
86 } wbuf;
87 struct {
88 struct ether_header eh;
89 struct {
90 struct ether_arp arp;
91 u_char pad[24]; /* extra space */
92 } data;
93 } rbuf;
94
95 /* Try for cached answer first */
96 for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
97 if (addr.s_addr == al->addr.s_addr)
98 return (al->ea);
99
100 /* Don't overflow cache */
101 if (arp_num > ARP_NUM - 1) {
102 arp_num = 1; /* recycle */
103 printf("arpwhohas: overflowed arp_list!\n");
104 }
105
106 #ifdef ARP_DEBUG
107 if (debug)
108 printf("arpwhohas: send request for %s\n", inet_ntoa(addr));
109 #endif
110
111 bzero((char*)&wbuf.data, sizeof(wbuf.data));
112 ah = &wbuf.data.arp;
113 ah->arp_hrd = htons(ARPHRD_ETHER);
114 ah->arp_pro = htons(ETHERTYPE_IP);
115 ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */
116 ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */
117 ah->arp_op = htons(ARPOP_REQUEST);
118 MACPY(d->myea, ah->arp_sha);
119 bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa));
120 /* Leave zeros in arp_tha */
121 bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa));
122
123 /* Store ip address in cache (incomplete entry). */
124 al->addr = addr;
125
126 i = sendrecv(d,
127 arpsend, &wbuf.data, sizeof(wbuf.data),
128 arprecv, &rbuf.data, sizeof(rbuf.data));
129 if (i == -1) {
130 panic("arp: no response for %s\n",
131 inet_ntoa(addr));
132 }
133
134 /* Store ethernet address in cache */
135 ah = &rbuf.data.arp;
136 #ifdef ARP_DEBUG
137 if (debug) {
138 printf("arp: response from %s\n",
139 ether_sprintf(rbuf.eh.ether_shost));
140 printf("arp: cacheing %s --> %s\n",
141 inet_ntoa(addr), ether_sprintf(ah->arp_sha));
142 }
143 #endif
144 MACPY(ah->arp_sha, al->ea);
145 ++arp_num;
146
147 return (al->ea);
148 }
149
150 static ssize_t
151 arpsend(d, pkt, len)
152 register struct iodesc *d;
153 register void *pkt;
154 register size_t len;
155 {
156
157 #ifdef ARP_DEBUG
158 if (debug)
159 printf("arpsend: called\n");
160 #endif
161
162 return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP));
163 }
164
165 /*
166 * Returns 0 if this is the packet we're waiting for
167 * else -1 (and errno == 0)
168 */
169 static ssize_t
170 arprecv(d, pkt, len, tleft)
171 register struct iodesc *d;
172 register void *pkt;
173 register size_t len;
174 time_t tleft;
175 {
176 register ssize_t n;
177 register struct ether_arp *ah;
178 u_int16_t etype; /* host order */
179
180 #ifdef ARP_DEBUG
181 if (debug)
182 printf("arprecv: ");
183 #endif
184
185 n = readether(d, pkt, len, tleft, &etype);
186 errno = 0; /* XXX */
187 if (n == -1 || n < sizeof(struct ether_arp)) {
188 #ifdef ARP_DEBUG
189 if (debug)
190 printf("bad len=%d\n", n);
191 #endif
192 return (-1);
193 }
194
195 if (etype != ETHERTYPE_ARP) {
196 #ifdef ARP_DEBUG
197 if (debug)
198 printf("not arp type=%d\n", etype);
199 #endif
200 return (-1);
201 }
202
203 /* Ethernet address now checked in readether() */
204
205 ah = (struct ether_arp *)pkt;
206 if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
207 ah->arp_pro != htons(ETHERTYPE_IP) ||
208 ah->arp_hln != sizeof(ah->arp_sha) ||
209 ah->arp_pln != sizeof(ah->arp_spa) )
210 {
211 #ifdef ARP_DEBUG
212 if (debug)
213 printf("bad hrd/pro/hln/pln\n");
214 #endif
215 return (-1);
216 }
217
218 if (ah->arp_op == htons(ARPOP_REQUEST)) {
219 #ifdef ARP_DEBUG
220 if (debug)
221 printf("is request\n");
222 #endif
223 arp_reply(d, ah);
224 return (-1);
225 }
226
227 if (ah->arp_op != htons(ARPOP_REPLY)) {
228 #ifdef ARP_DEBUG
229 if (debug)
230 printf("not ARP reply\n");
231 #endif
232 return (-1);
233 }
234
235 /* Is the reply from the source we want? */
236 if (bcmp(&arp_list[arp_num].addr,
237 ah->arp_spa, sizeof(ah->arp_spa)))
238 {
239 #ifdef ARP_DEBUG
240 if (debug)
241 printf("unwanted address\n");
242 #endif
243 return (-1);
244 }
245 /* We don't care who the reply was sent to. */
246
247 /* We have our answer. */
248 #ifdef ARP_DEBUG
249 if (debug)
250 printf("got it\n");
251 #endif
252 return (n);
253 }
254
255 /*
256 * Convert an ARP request into a reply and send it.
257 * Notes: Re-uses buffer. Pad to length = 46.
258 */
259 void
260 arp_reply(d, pkt)
261 register struct iodesc *d;
262 register void *pkt; /* the request */
263 {
264 struct ether_arp *arp = pkt;
265
266 if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
267 arp->arp_pro != htons(ETHERTYPE_IP) ||
268 arp->arp_hln != sizeof(arp->arp_sha) ||
269 arp->arp_pln != sizeof(arp->arp_spa) )
270 {
271 #ifdef ARP_DEBUG
272 if (debug)
273 printf("arp_reply: bad hrd/pro/hln/pln\n");
274 #endif
275 return;
276 }
277
278 if (arp->arp_op != htons(ARPOP_REQUEST)) {
279 #ifdef ARP_DEBUG
280 if (debug)
281 printf("arp_reply: not request!\n");
282 #endif
283 return;
284 }
285
286 /* If we are not the target, ignore the request. */
287 if (bcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
288 return;
289
290 #ifdef ARP_DEBUG
291 if (debug) {
292 printf("arp_reply: to %s\n", ether_sprintf(arp->arp_sha));
293 }
294 #endif
295
296 arp->arp_op = htons(ARPOP_REPLY);
297 /* source becomes target */
298 bcopy(arp->arp_sha, arp->arp_tha, sizeof(arp->arp_tha));
299 bcopy(arp->arp_spa, arp->arp_tpa, sizeof(arp->arp_tpa));
300 /* here becomes source */
301 bcopy(d->myea, arp->arp_sha, sizeof(arp->arp_sha));
302 bcopy(&d->myip, arp->arp_spa, sizeof(arp->arp_spa));
303
304 /*
305 * No need to get fancy here. If the send fails, the
306 * requestor will just ask again.
307 */
308 (void) sendether(d, pkt, sizeof(*arp) + 18,
309 arp->arp_tha, ETHERTYPE_ARP);
310 }
311