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