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