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