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