arp.c revision 1.6 1 /* $NetBSD: arp.c,v 1.6 1995/08/04 01:17:26 thorpej 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/types.h>
43 #include <sys/socket.h>
44 #include <net/if.h>
45 #include <netinet/in.h>
46
47 #include <netinet/if_ether.h>
48 #include <netinet/in_systm.h>
49
50 #include <string.h>
51
52 #include "stand.h"
53 #include "net.h"
54
55 /* Cache stuff */
56 #define ARP_NUM 8 /* need at most 3 arp entries */
57
58 static struct arp_list {
59 n_long addr;
60 u_char ea[6];
61 } arp_list[ARP_NUM] = {
62 { INADDR_BROADCAST, BA }
63 };
64 static int arp_num = 1;
65
66 /* Local forwards */
67 static size_t arpsend __P((struct iodesc *, void *, size_t));
68 static size_t arprecv __P((struct iodesc *, void *, size_t, time_t));
69
70 /* Broadcast an ARP packet, asking who has addr on interface d */
71 u_char *
72 arpwhohas(d, addr)
73 register struct iodesc *d;
74 n_long addr;
75 {
76 register int i;
77 register struct ether_arp *ah;
78 register struct arp_list *al;
79 struct {
80 u_char header[ETHER_SIZE];
81 struct {
82 struct ether_arp arp;
83 u_char pad[18]; /* 60 - sizeof(arp) */
84 } data;
85 } wbuf;
86 struct {
87 u_char header[ETHER_SIZE];
88 struct {
89 struct ether_arp arp;
90 u_char pad[24]; /* extra space */
91 } data;
92 } rbuf;
93
94 #ifdef ARP_DEBUG
95 if (debug)
96 printf("arpwhohas: called for %s\n", intoa(addr));
97 #endif
98 /* Try for cached answer first */
99 for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
100 if (addr == al->addr)
101 return (al->ea);
102
103 /* Don't overflow cache */
104 if (arp_num > ARP_NUM - 1)
105 panic("arpwhohas: overflowed arp_list!");
106
107 #ifdef ARP_DEBUG
108 if (debug)
109 printf("arpwhohas: not cached\n");
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 bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa));
122
123 /* Store ip address in cache */
124 al->addr = addr;
125
126 (void)sendrecv(d,
127 arpsend, &wbuf.data, sizeof(wbuf.data),
128 arprecv, &rbuf.data, sizeof(rbuf.data));
129
130 /* Store ethernet address in cache */
131 MACPY(rbuf.data.arp.arp_sha, al->ea);
132 ++arp_num;
133
134 return (al->ea);
135 }
136
137 static size_t
138 arpsend(d, pkt, len)
139 register struct iodesc *d;
140 register void *pkt;
141 register size_t len;
142 {
143
144 #ifdef ARP_DEBUG
145 if (debug)
146 printf("arpsend: called\n");
147 #endif
148
149 return (sendether(d, pkt, len, bcea, ETHERTYPE_ARP));
150 }
151
152 /* Returns 0 if this is the packet we're waiting for else -1 (and errno == 0) */
153 static size_t
154 arprecv(d, pkt, len, tleft)
155 register struct iodesc *d;
156 register void *pkt;
157 register size_t len;
158 time_t tleft;
159 {
160 register struct ether_header *eh;
161 register struct ether_arp *ah;
162
163 #ifdef ARP_DEBUG
164 if (debug)
165 printf("arprecv: ");
166 #endif
167
168 len = readether(d, pkt, len, tleft);
169 if (len == -1 || len < sizeof(struct ether_arp)) {
170 #ifdef ARP_DEBUG
171 if (debug)
172 printf("bad len=%d\n", len);
173 #endif
174 goto bad;
175 }
176
177 eh = (struct ether_header *)pkt - 1;
178 if (eh->ether_type != htons(ETHERTYPE_ARP)) {
179 #ifdef ARP_DEBUG
180 if (debug)
181 printf("not arp %d\n", ntohs(eh->ether_type));
182 #endif
183 goto bad;
184 }
185 /* Must be to us */
186 if (bcmp(d->myea, eh->ether_dhost, 6) != 0 &&
187 bcmp(bcea, eh->ether_dhost, 6) != 0) {
188 #ifdef ARP_DEBUG
189 if (debug)
190 printf("not ours %s\n", ether_sprintf(eh->ether_dhost));
191 #endif
192 goto bad;
193 }
194
195 ah = (struct ether_arp *)pkt;
196 HTONS(ah->arp_hrd);
197 HTONS(ah->arp_pro);
198 HTONS(ah->arp_op);
199 if (ah->arp_hrd != ARPHRD_ETHER || ah->arp_pro != ETHERTYPE_IP ||
200 ah->arp_hln != sizeof(ah->arp_sha) ||
201 ah->arp_pln != sizeof(ah->arp_spa) || ah->arp_op != ARPOP_REPLY) {
202 #ifdef ARP_DEBUG
203 if (debug)
204 printf("not valid reply\n");
205 #endif
206 goto bad;
207 }
208 if (bcmp(&arp_list[arp_num].addr, ah->arp_spa, sizeof(long)) != 0 ||
209 bcmp(&d->myip, ah->arp_tpa, sizeof(d->myip)) != 0) {
210 #ifdef ARP_DEBUG
211 if (debug)
212 printf("already cached??\n");
213 #endif
214 goto bad;
215 }
216
217 #ifdef ARP_DEBUG
218 if (debug)
219 printf("cacheing %s --> %s\n",
220 intoa(ah->arp_spa),
221 ether_sprintf(ah->arp_sha));
222 #endif
223 return (0);
224
225 bad:
226 errno = 0;
227 return (-1);
228 }
229