custom message type for VM_INFO
[minix3.git] / sys / lib / libsa / arp.c
blob8716fd6e1fbb86c256e494031df08673e315be2a
1 /* $NetBSD: arp.c,v 1.33 2009/01/17 14:00:36 tsutsui Exp $ */
3 /*
4 * Copyright (c) 1992 Regents of the University of California.
5 * All rights reserved.
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.
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.
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.
39 * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp (LBL)
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>
48 #include <netinet/in_systm.h>
50 #ifdef _STANDALONE
51 #include <lib/libkern/libkern.h>
52 #else
53 #include <string.h>
54 #endif
56 #include "stand.h"
57 #include "net.h"
60 * Ethernet Address Resolution Protocol.
62 * See RFC 826 for protocol description. Structure below is adapted
63 * to resolving internet addresses. Field names used correspond to
64 * RFC 826.
66 struct ether_arp {
67 struct arphdr ea_hdr; /* fixed-size header */
68 u_int8_t arp_sha[ETHER_ADDR_LEN]; /* sender hardware address */
69 u_int8_t arp_spa[4]; /* sender protocol address */
70 u_int8_t arp_tha[ETHER_ADDR_LEN]; /* target hardware address */
71 u_int8_t arp_tpa[4]; /* target protocol address */
73 #define arp_hrd ea_hdr.ar_hrd
74 #define arp_pro ea_hdr.ar_pro
75 #define arp_hln ea_hdr.ar_hln
76 #define arp_pln ea_hdr.ar_pln
77 #define arp_op ea_hdr.ar_op
79 /* Cache stuff */
80 #define ARP_NUM 8 /* need at most 3 arp entries */
82 struct arp_list {
83 struct in_addr addr;
84 u_char ea[6];
85 } arp_list[ARP_NUM] = {
86 /* XXX - net order `INADDR_BROADCAST' must be a constant */
87 { {0xffffffff}, BA }
89 int arp_num = 1;
91 /* Local forwards */
92 static ssize_t arpsend(struct iodesc *, void *, size_t);
93 static ssize_t arprecv(struct iodesc *, void *, size_t, saseconds_t);
95 /* Broadcast an ARP packet, asking who has addr on interface d */
96 u_char *
97 arpwhohas(struct iodesc *d, struct in_addr addr)
99 int i;
100 struct ether_arp *ah;
101 struct arp_list *al;
102 struct {
103 struct ether_header eh;
104 struct {
105 struct ether_arp arp;
106 u_char pad[18]; /* 60 - sizeof(...) */
107 } data;
108 } wbuf;
109 struct {
110 struct ether_header eh;
111 struct {
112 struct ether_arp arp;
113 u_char pad[24]; /* extra space */
114 } data;
115 } rbuf;
117 /* Try for cached answer first */
118 for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
119 if (addr.s_addr == al->addr.s_addr)
120 return al->ea;
122 /* Don't overflow cache */
123 if (arp_num > ARP_NUM - 1) {
124 arp_num = 1; /* recycle */
125 printf("arpwhohas: overflowed arp_list!\n");
128 #ifdef ARP_DEBUG
129 if (debug)
130 printf("arpwhohas: send request for %s\n", inet_ntoa(addr));
131 #endif
133 (void)memset(&wbuf.data, 0, sizeof(wbuf.data));
134 ah = &wbuf.data.arp;
135 ah->arp_hrd = htons(ARPHRD_ETHER);
136 ah->arp_pro = htons(ETHERTYPE_IP);
137 ah->arp_hln = sizeof(ah->arp_sha); /* hardware address length */
138 ah->arp_pln = sizeof(ah->arp_spa); /* protocol address length */
139 ah->arp_op = htons(ARPOP_REQUEST);
140 MACPY(d->myea, ah->arp_sha);
141 (void)memcpy(ah->arp_spa, &d->myip, sizeof(ah->arp_spa));
142 /* Leave zeros in arp_tha */
143 (void)memcpy(ah->arp_tpa, &addr, sizeof(ah->arp_tpa));
145 /* Store ip address in cache (incomplete entry). */
146 al->addr = addr;
148 i = sendrecv(d,
149 arpsend, &wbuf.data, sizeof(wbuf.data),
150 arprecv, &rbuf.data, sizeof(rbuf.data));
151 if (i == -1) {
152 panic("arp: no response for %s",
153 inet_ntoa(addr));
156 /* Store ethernet address in cache */
157 ah = &rbuf.data.arp;
158 #ifdef ARP_DEBUG
159 if (debug) {
160 printf("arp: response from %s\n",
161 ether_sprintf(rbuf.eh.ether_shost));
162 printf("arp: cacheing %s --> %s\n",
163 inet_ntoa(addr), ether_sprintf(ah->arp_sha));
165 #endif
166 MACPY(ah->arp_sha, al->ea);
167 ++arp_num;
169 return al->ea;
172 static ssize_t
173 arpsend(struct iodesc *d, void *pkt, size_t len)
176 #ifdef ARP_DEBUG
177 if (debug)
178 printf("arpsend: called\n");
179 #endif
181 return sendether(d, pkt, len, bcea, ETHERTYPE_ARP);
185 * Returns 0 if this is the packet we're waiting for
186 * else -1 (and errno == 0)
188 static ssize_t
189 arprecv(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft)
191 ssize_t n;
192 struct ether_arp *ah;
193 u_int16_t etype; /* host order */
195 #ifdef ARP_DEBUG
196 if (debug)
197 printf("arprecv: ");
198 #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=%ld\n", (signed long) n);
206 #endif
207 return -1;
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;
218 /* Ethernet address now checked in readether() */
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) )
226 #ifdef ARP_DEBUG
227 if (debug)
228 printf("bad hrd/pro/hln/pln\n");
229 #endif
230 return -1;
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;
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;
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)))
254 #ifdef ARP_DEBUG
255 if (debug)
256 printf("unwanted address\n");
257 #endif
258 return -1;
260 /* We don't care who the reply was sent to. */
262 /* We have our answer. */
263 #ifdef ARP_DEBUG
264 if (debug)
265 printf("got it\n");
266 #endif
267 return n;
271 * Convert an ARP request into a reply and send it.
272 * Notes: Re-uses buffer. Pad to length = 46.
274 void
275 arp_reply(struct iodesc *d, void *pkt)
277 struct ether_arp *arp = pkt;
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) )
284 #ifdef ARP_DEBUG
285 if (debug)
286 printf("arp_reply: bad hrd/pro/hln/pln\n");
287 #endif
288 return;
291 if (arp->arp_op != htons(ARPOP_REQUEST)) {
292 #ifdef ARP_DEBUG
293 if (debug)
294 printf("arp_reply: not request!\n");
295 #endif
296 return;
299 /* If we are not the target, ignore the request. */
300 if (memcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
301 return;
303 #ifdef ARP_DEBUG
304 if (debug) {
305 printf("arp_reply: to %s\n", ether_sprintf(arp->arp_sha));
307 #endif
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));
318 * No need to get fancy here. If the send fails, the
319 * requestor will just ask again.
321 (void) sendether(d, pkt, sizeof(*arp) + 18,
322 arp->arp_tha, ETHERTYPE_ARP);