This source file includes following definitions.
- arpwhohas
- arpsend
- arprecv
- arp_reply
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <net/if.h>
46 #include <netinet/in.h>
47
48 #include <netinet/if_ether.h>
49 #include <netinet/in_systm.h>
50
51 #include "stand.h"
52 #include "net.h"
53
54
55 #define ARP_NUM 8
56
57 struct arp_list {
58 struct in_addr addr;
59 u_char ea[6];
60 } arp_list[ARP_NUM] = {
61
62 { {0xffffffff}, BA }
63 };
64 int arp_num = 1;
65
66
67 static ssize_t arpsend(struct iodesc *, void *, size_t);
68 static ssize_t arprecv(struct iodesc *, void *, size_t, time_t);
69
70
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 struct {
78 struct ether_header eh;
79 struct {
80 struct ether_arp arp;
81 u_char pad[18];
82 } data;
83 } wbuf;
84 struct {
85 struct ether_header eh;
86 struct {
87 struct ether_arp arp;
88 u_char pad[24];
89 } data;
90 } rbuf;
91
92
93 for (i = 0, al = arp_list; i < arp_num; ++i, ++al)
94 if (addr.s_addr == al->addr.s_addr)
95 return (al->ea);
96
97
98 if (arp_num > ARP_NUM - 1) {
99 arp_num = 1;
100 printf("arpwhohas: overflowed arp_list!\n");
101 }
102
103 #ifdef ARP_DEBUG
104 if (debug)
105 printf("arpwhohas: send request for %s\n", inet_ntoa(addr));
106 #endif
107
108 bzero((char *)&wbuf.data, sizeof(wbuf.data));
109 ah = &wbuf.data.arp;
110 ah->arp_hrd = htons(ARPHRD_ETHER);
111 ah->arp_pro = htons(ETHERTYPE_IP);
112 ah->arp_hln = sizeof(ah->arp_sha);
113 ah->arp_pln = sizeof(ah->arp_spa);
114 ah->arp_op = htons(ARPOP_REQUEST);
115 MACPY(d->myea, ah->arp_sha);
116 bcopy(&d->myip, ah->arp_spa, sizeof(ah->arp_spa));
117
118 bcopy(&addr, ah->arp_tpa, sizeof(ah->arp_tpa));
119
120
121 al->addr = addr;
122
123 i = sendrecv(d,
124 arpsend, &wbuf.data, sizeof(wbuf.data),
125 arprecv, &rbuf.data, sizeof(rbuf.data));
126 if (i == -1) {
127 panic("arp: no response for %s", inet_ntoa(addr));
128 }
129
130
131 ah = &rbuf.data.arp;
132 #ifdef ARP_DEBUG
133 if (debug) {
134 printf("arp: response from %s\n",
135 ether_sprintf(rbuf.eh.ether_shost));
136 printf("arp: cacheing %s --> %s\n",
137 inet_ntoa(addr), ether_sprintf(ah->arp_sha));
138 }
139 #endif
140 MACPY(ah->arp_sha, al->ea);
141 ++arp_num;
142
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
160
161
162 static ssize_t
163 arprecv(struct iodesc *d, void *pkt, size_t len, time_t tleft)
164 {
165 ssize_t n;
166 struct ether_arp *ah;
167 u_int16_t etype;
168
169 #ifdef ARP_DEBUG
170 if (debug)
171 printf("arprecv: ");
172 #endif
173
174 n = readether(d, pkt, len, tleft, &etype);
175 errno = 0;
176 if (n < 0 || (size_t)n < sizeof(struct ether_arp)) {
177 #ifdef ARP_DEBUG
178 if (debug)
179 printf("bad len=%d\n", n);
180 #endif
181 return (-1);
182 }
183
184 if (etype != ETHERTYPE_ARP) {
185 #ifdef ARP_DEBUG
186 if (debug)
187 printf("not arp type=%d\n", etype);
188 #endif
189 return (-1);
190 }
191
192
193
194 ah = (struct ether_arp *)pkt;
195 if (ah->arp_hrd != htons(ARPHRD_ETHER) ||
196 ah->arp_pro != htons(ETHERTYPE_IP) ||
197 ah->arp_hln != sizeof(ah->arp_sha) ||
198 ah->arp_pln != sizeof(ah->arp_spa)) {
199 #ifdef ARP_DEBUG
200 if (debug)
201 printf("bad hrd/pro/hln/pln\n");
202 #endif
203 return (-1);
204 }
205
206 if (ah->arp_op == htons(ARPOP_REQUEST)) {
207 #ifdef ARP_DEBUG
208 if (debug)
209 printf("is request\n");
210 #endif
211 arp_reply(d, ah);
212 return (-1);
213 }
214
215 if (ah->arp_op != htons(ARPOP_REPLY)) {
216 #ifdef ARP_DEBUG
217 if (debug)
218 printf("not ARP reply\n");
219 #endif
220 return (-1);
221 }
222
223
224 if (bcmp(&arp_list[arp_num].addr, ah->arp_spa,
225 sizeof(ah->arp_spa))) {
226 #ifdef ARP_DEBUG
227 if (debug)
228 printf("unwanted address\n");
229 #endif
230 return (-1);
231 }
232
233
234
235 #ifdef ARP_DEBUG
236 if (debug)
237 printf("got it\n");
238 #endif
239 return (n);
240 }
241
242
243
244
245
246 void
247 arp_reply(struct iodesc *d, void *pkt)
248 {
249 struct ether_arp *arp = pkt;
250
251 if (arp->arp_hrd != htons(ARPHRD_ETHER) ||
252 arp->arp_pro != htons(ETHERTYPE_IP) ||
253 arp->arp_hln != sizeof(arp->arp_sha) ||
254 arp->arp_pln != sizeof(arp->arp_spa)) {
255 #ifdef ARP_DEBUG
256 if (debug)
257 printf("arp_reply: bad hrd/pro/hln/pln\n");
258 #endif
259 return;
260 }
261
262 if (arp->arp_op != htons(ARPOP_REQUEST)) {
263 #ifdef ARP_DEBUG
264 if (debug)
265 printf("arp_reply: not request!\n");
266 #endif
267 return;
268 }
269
270
271 if (bcmp(arp->arp_tpa, &d->myip, sizeof(arp->arp_tpa)))
272 return;
273
274 #ifdef ARP_DEBUG
275 if (debug) {
276 printf("arp_reply: to %s\n", ether_sprintf(arp->arp_sha));
277 }
278 #endif
279
280 arp->arp_op = htons(ARPOP_REPLY);
281
282 bcopy(arp->arp_sha, arp->arp_tha, sizeof(arp->arp_tha));
283 bcopy(arp->arp_spa, arp->arp_tpa, sizeof(arp->arp_tpa));
284
285 bcopy(d->myea, arp->arp_sha, sizeof(arp->arp_sha));
286 bcopy(&d->myip, arp->arp_spa, sizeof(arp->arp_spa));
287
288
289
290
291
292 (void) sendether(d, pkt, sizeof(*arp) + 18,
293 arp->arp_tha, ETHERTYPE_ARP);
294 }