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