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