Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1982, 1986, 1988 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | * | |
38e82238 | 33 | * from: @(#)if_ether.c 7.13 (Berkeley) 10/31/90 |
4c45483e | 34 | * $Id: if_ether.c,v 1.3 1993/10/16 18:25:54 rgrimes Exp $ |
15637ed4 RG |
35 | */ |
36 | ||
37 | /* | |
38 | * Ethernet address resolution protocol. | |
39 | * TODO: | |
40 | * run at splnet (add ARP protocol intr.) | |
41 | * link entries onto hash chains, keep free list | |
42 | * add "inuse/lock" bit (or ref. count) along with valid bit | |
43 | */ | |
44 | ||
45 | #include "param.h" | |
46 | #include "systm.h" | |
47 | #include "malloc.h" | |
48 | #include "mbuf.h" | |
49 | #include "socket.h" | |
50 | #include "time.h" | |
51 | #include "kernel.h" | |
52 | #include "errno.h" | |
53 | #include "ioctl.h" | |
54 | #include "syslog.h" | |
55 | ||
56 | #include "../net/if.h" | |
57 | #include "in.h" | |
58 | #include "in_systm.h" | |
59 | #include "in_var.h" | |
60 | #include "ip.h" | |
61 | #include "if_ether.h" | |
62 | ||
4c45483e GW |
63 | static void in_arpinput(struct arpcom *, struct mbuf *); |
64 | static void arptfree(struct arptab *); | |
65 | ||
15637ed4 RG |
66 | #ifdef GATEWAY |
67 | #define ARPTAB_BSIZ 16 /* bucket size */ | |
68 | #define ARPTAB_NB 37 /* number of buckets */ | |
69 | #else | |
70 | #define ARPTAB_BSIZ 9 /* bucket size */ | |
71 | #define ARPTAB_NB 19 /* number of buckets */ | |
72 | #endif | |
73 | #define ARPTAB_SIZE (ARPTAB_BSIZ * ARPTAB_NB) | |
74 | struct arptab arptab[ARPTAB_SIZE]; | |
75 | int arptab_size = ARPTAB_SIZE; /* for arp command */ | |
76 | ||
77 | /* | |
78 | * ARP trailer negotiation. Trailer protocol is not IP specific, | |
79 | * but ARP request/response use IP addresses. | |
80 | */ | |
81 | #define ETHERTYPE_IPTRAILERS ETHERTYPE_TRAIL | |
82 | ||
83 | #define ARPTAB_HASH(a) \ | |
84 | ((u_long)(a) % ARPTAB_NB) | |
85 | ||
86 | #define ARPTAB_LOOK(at,addr) { \ | |
87 | register n; \ | |
88 | at = &arptab[ARPTAB_HASH(addr) * ARPTAB_BSIZ]; \ | |
89 | for (n = 0 ; n < ARPTAB_BSIZ ; n++,at++) \ | |
90 | if (at->at_iaddr.s_addr == addr) \ | |
91 | break; \ | |
92 | if (n >= ARPTAB_BSIZ) \ | |
93 | at = 0; \ | |
94 | } | |
95 | ||
96 | /* timer values */ | |
97 | #define ARPT_AGE (60*1) /* aging timer, 1 min. */ | |
98 | #define ARPT_KILLC 20 /* kill completed entry in 20 mins. */ | |
99 | #define ARPT_KILLI 3 /* kill incomplete entry in 3 minutes */ | |
100 | ||
101 | extern struct ifnet loif; | |
102 | ||
103 | /* | |
104 | * Timeout routine. Age arp_tab entries once a minute. | |
105 | */ | |
4c45483e | 106 | void |
15637ed4 RG |
107 | arptimer() |
108 | { | |
109 | register struct arptab *at; | |
110 | register i; | |
111 | ||
112 | timeout(arptimer, (caddr_t)0, ARPT_AGE * hz); | |
113 | at = &arptab[0]; | |
114 | for (i = 0; i < ARPTAB_SIZE; i++, at++) { | |
115 | if (at->at_flags == 0 || (at->at_flags & ATF_PERM)) | |
116 | continue; | |
117 | if (++at->at_timer < ((at->at_flags&ATF_COM) ? | |
118 | ARPT_KILLC : ARPT_KILLI)) | |
119 | continue; | |
120 | /* timer has expired, clear entry */ | |
121 | arptfree(at); | |
122 | } | |
123 | } | |
124 | ||
125 | /* | |
126 | * Broadcast an ARP packet, asking who has addr on interface ac. | |
127 | */ | |
4c45483e | 128 | void |
15637ed4 RG |
129 | arpwhohas(ac, addr) |
130 | register struct arpcom *ac; | |
131 | struct in_addr *addr; | |
132 | { | |
133 | register struct mbuf *m; | |
134 | register struct ether_header *eh; | |
135 | register struct ether_arp *ea; | |
136 | struct sockaddr sa; | |
137 | ||
138 | if ((m = m_gethdr(M_DONTWAIT, MT_DATA)) == NULL) | |
139 | return; | |
140 | m->m_len = sizeof(*ea); | |
141 | m->m_pkthdr.len = sizeof(*ea); | |
142 | MH_ALIGN(m, sizeof(*ea)); | |
143 | ea = mtod(m, struct ether_arp *); | |
144 | eh = (struct ether_header *)sa.sa_data; | |
145 | bzero((caddr_t)ea, sizeof (*ea)); | |
146 | bcopy((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, | |
147 | sizeof(eh->ether_dhost)); | |
148 | eh->ether_type = ETHERTYPE_ARP; /* if_output will swap */ | |
149 | ea->arp_hrd = htons(ARPHRD_ETHER); | |
150 | ea->arp_pro = htons(ETHERTYPE_IP); | |
151 | ea->arp_hln = sizeof(ea->arp_sha); /* hardware address length */ | |
152 | ea->arp_pln = sizeof(ea->arp_spa); /* protocol address length */ | |
153 | ea->arp_op = htons(ARPOP_REQUEST); | |
154 | bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, | |
155 | sizeof(ea->arp_sha)); | |
156 | bcopy((caddr_t)&ac->ac_ipaddr, (caddr_t)ea->arp_spa, | |
157 | sizeof(ea->arp_spa)); | |
158 | bcopy((caddr_t)addr, (caddr_t)ea->arp_tpa, sizeof(ea->arp_tpa)); | |
159 | sa.sa_family = AF_UNSPEC; | |
160 | sa.sa_len = sizeof(sa); | |
161 | (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); | |
162 | } | |
163 | ||
164 | int useloopback = 1; /* use loopback interface for local traffic */ | |
165 | ||
166 | /* | |
167 | * Resolve an IP address into an ethernet address. If success, | |
168 | * desten is filled in. If there is no entry in arptab, | |
169 | * set one up and broadcast a request for the IP address. | |
170 | * Hold onto this mbuf and resend it once the address | |
171 | * is finally resolved. A return value of 1 indicates | |
172 | * that desten has been filled in and the packet should be sent | |
173 | * normally; a 0 return indicates that the packet has been | |
174 | * taken over here, either now or for later transmission. | |
175 | * | |
176 | * We do some (conservative) locking here at splimp, since | |
177 | * arptab is also altered from input interrupt service (ecintr/ilintr | |
178 | * calls arpinput when ETHERTYPE_ARP packets come in). | |
179 | */ | |
4c45483e | 180 | int |
15637ed4 RG |
181 | arpresolve(ac, m, destip, desten, usetrailers) |
182 | register struct arpcom *ac; | |
183 | struct mbuf *m; | |
184 | register struct in_addr *destip; | |
185 | register u_char *desten; | |
186 | int *usetrailers; | |
187 | { | |
188 | register struct arptab *at; | |
189 | struct sockaddr_in sin; | |
190 | register struct in_ifaddr *ia; | |
191 | u_long lna; | |
192 | int s; | |
193 | ||
194 | *usetrailers = 0; | |
195 | if (m->m_flags & M_BCAST) { /* broadcast */ | |
196 | bcopy((caddr_t)etherbroadcastaddr, (caddr_t)desten, | |
197 | sizeof(etherbroadcastaddr)); | |
198 | return (1); | |
199 | } | |
200 | lna = in_lnaof(*destip); | |
201 | /* if for us, use software loopback driver if up */ | |
202 | for (ia = in_ifaddr; ia; ia = ia->ia_next) | |
203 | if ((ia->ia_ifp == &ac->ac_if) && | |
204 | (destip->s_addr == ia->ia_addr.sin_addr.s_addr)) { | |
205 | /* | |
206 | * This test used to be | |
207 | * if (loif.if_flags & IFF_UP) | |
208 | * It allowed local traffic to be forced | |
209 | * through the hardware by configuring the loopback down. | |
210 | * However, it causes problems during network configuration | |
211 | * for boards that can't receive packets they send. | |
212 | * It is now necessary to clear "useloopback" | |
213 | * to force traffic out to the hardware. | |
214 | */ | |
215 | if (useloopback) { | |
216 | sin.sin_family = AF_INET; | |
217 | sin.sin_addr = *destip; | |
218 | (void) looutput(&loif, m, (struct sockaddr *)&sin, 0); | |
219 | /* | |
220 | * The packet has already been sent and freed. | |
221 | */ | |
222 | return (0); | |
223 | } else { | |
224 | bcopy((caddr_t)ac->ac_enaddr, (caddr_t)desten, | |
225 | sizeof(ac->ac_enaddr)); | |
226 | return (1); | |
227 | } | |
228 | } | |
229 | s = splimp(); | |
230 | ARPTAB_LOOK(at, destip->s_addr); | |
231 | if (at == 0) { /* not found */ | |
232 | if (ac->ac_if.if_flags & IFF_NOARP) { | |
233 | bcopy((caddr_t)ac->ac_enaddr, (caddr_t)desten, 3); | |
234 | desten[3] = (lna >> 16) & 0x7f; | |
235 | desten[4] = (lna >> 8) & 0xff; | |
236 | desten[5] = lna & 0xff; | |
237 | splx(s); | |
238 | return (1); | |
239 | } else { | |
240 | at = arptnew(destip); | |
241 | if (at == 0) | |
242 | panic("arpresolve: no free entry"); | |
243 | at->at_hold = m; | |
244 | arpwhohas(ac, destip); | |
245 | splx(s); | |
246 | return (0); | |
247 | } | |
248 | } | |
249 | at->at_timer = 0; /* restart the timer */ | |
250 | if (at->at_flags & ATF_COM) { /* entry IS complete */ | |
47110967 RG |
251 | |
252 | *(int *) desten = *(int *) at->at_enaddr; | |
253 | ((short *) desten)[2] = ((short *) at->at_enaddr)[2]; | |
254 | ||
15637ed4 RG |
255 | if (at->at_flags & ATF_USETRAILERS) |
256 | *usetrailers = 1; | |
257 | splx(s); | |
258 | return (1); | |
259 | } | |
260 | /* | |
261 | * There is an arptab entry, but no ethernet address | |
262 | * response yet. Replace the held mbuf with this | |
263 | * latest one. | |
264 | */ | |
265 | if (at->at_hold) | |
266 | m_freem(at->at_hold); | |
267 | at->at_hold = m; | |
268 | arpwhohas(ac, destip); /* ask again */ | |
269 | splx(s); | |
270 | return (0); | |
271 | } | |
272 | ||
273 | /* | |
274 | * Called from 10 Mb/s Ethernet interrupt handlers | |
275 | * when ether packet type ETHERTYPE_ARP | |
276 | * is received. Common length and type checks are done here, | |
277 | * then the protocol-specific routine is called. | |
278 | */ | |
4c45483e | 279 | void |
15637ed4 RG |
280 | arpinput(ac, m) |
281 | struct arpcom *ac; | |
282 | struct mbuf *m; | |
283 | { | |
284 | register struct arphdr *ar; | |
285 | ||
286 | if (ac->ac_if.if_flags & IFF_NOARP) | |
287 | goto out; | |
288 | if (m->m_len < sizeof(struct arphdr)) | |
289 | goto out; | |
290 | ar = mtod(m, struct arphdr *); | |
291 | if (ntohs(ar->ar_hrd) != ARPHRD_ETHER) | |
292 | goto out; | |
293 | if (m->m_len < sizeof(struct arphdr) + 2 * ar->ar_hln + 2 * ar->ar_pln) | |
294 | goto out; | |
295 | ||
296 | switch (ntohs(ar->ar_pro)) { | |
297 | ||
298 | case ETHERTYPE_IP: | |
299 | case ETHERTYPE_IPTRAILERS: | |
300 | in_arpinput(ac, m); | |
301 | return; | |
302 | ||
303 | default: | |
304 | break; | |
305 | } | |
306 | out: | |
307 | m_freem(m); | |
308 | } | |
309 | ||
310 | /* | |
311 | * ARP for Internet protocols on 10 Mb/s Ethernet. | |
312 | * Algorithm is that given in RFC 826. | |
313 | * In addition, a sanity check is performed on the sender | |
314 | * protocol address, to catch impersonators. | |
315 | * We also handle negotiations for use of trailer protocol: | |
316 | * ARP replies for protocol type ETHERTYPE_TRAIL are sent | |
317 | * along with IP replies if we want trailers sent to us, | |
318 | * and also send them in response to IP replies. | |
319 | * This allows either end to announce the desire to receive | |
320 | * trailer packets. | |
321 | * We reply to requests for ETHERTYPE_TRAIL protocol as well, | |
322 | * but don't normally send requests. | |
323 | */ | |
4c45483e | 324 | void |
15637ed4 RG |
325 | in_arpinput(ac, m) |
326 | register struct arpcom *ac; | |
327 | struct mbuf *m; | |
328 | { | |
329 | register struct ether_arp *ea; | |
330 | struct ether_header *eh; | |
4c45483e | 331 | register struct arptab *at = 0; /* same as "merge" flag */ |
15637ed4 RG |
332 | register struct in_ifaddr *ia; |
333 | struct in_ifaddr *maybe_ia = 0; | |
334 | struct mbuf *mcopy = 0; | |
335 | struct sockaddr_in sin; | |
336 | struct sockaddr sa; | |
337 | struct in_addr isaddr, itaddr, myaddr; | |
338 | int proto, op, s, completed = 0; | |
339 | ||
340 | ea = mtod(m, struct ether_arp *); | |
341 | proto = ntohs(ea->arp_pro); | |
342 | op = ntohs(ea->arp_op); | |
343 | bcopy((caddr_t)ea->arp_spa, (caddr_t)&isaddr, sizeof (isaddr)); | |
344 | bcopy((caddr_t)ea->arp_tpa, (caddr_t)&itaddr, sizeof (itaddr)); | |
345 | for (ia = in_ifaddr; ia; ia = ia->ia_next) | |
346 | if (ia->ia_ifp == &ac->ac_if) { | |
347 | maybe_ia = ia; | |
348 | if ((itaddr.s_addr == ia->ia_addr.sin_addr.s_addr) || | |
349 | (isaddr.s_addr == ia->ia_addr.sin_addr.s_addr)) | |
350 | break; | |
351 | } | |
352 | if (maybe_ia == 0) | |
353 | goto out; | |
354 | myaddr = ia ? ia->ia_addr.sin_addr : maybe_ia->ia_addr.sin_addr; | |
355 | if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)ac->ac_enaddr, | |
356 | sizeof (ea->arp_sha))) | |
357 | goto out; /* it's from me, ignore it. */ | |
358 | if (!bcmp((caddr_t)ea->arp_sha, (caddr_t)etherbroadcastaddr, | |
359 | sizeof (ea->arp_sha))) { | |
360 | log(LOG_ERR, | |
361 | "arp: ether address is broadcast for IP address %x!\n", | |
362 | ntohl(isaddr.s_addr)); | |
363 | goto out; | |
364 | } | |
365 | if (isaddr.s_addr == myaddr.s_addr) { | |
366 | log(LOG_ERR, | |
367 | "duplicate IP address %x!! sent from ethernet address: %s\n", | |
368 | ntohl(isaddr.s_addr), ether_sprintf(ea->arp_sha)); | |
369 | itaddr = myaddr; | |
370 | if (op == ARPOP_REQUEST) | |
371 | goto reply; | |
372 | goto out; | |
373 | } | |
374 | s = splimp(); | |
375 | ARPTAB_LOOK(at, isaddr.s_addr); | |
376 | if (at) { | |
377 | bcopy((caddr_t)ea->arp_sha, (caddr_t)at->at_enaddr, | |
378 | sizeof(ea->arp_sha)); | |
379 | if ((at->at_flags & ATF_COM) == 0) | |
380 | completed = 1; | |
381 | at->at_flags |= ATF_COM; | |
382 | if (at->at_hold) { | |
383 | sin.sin_family = AF_INET; | |
384 | sin.sin_addr = isaddr; | |
385 | (*ac->ac_if.if_output)(&ac->ac_if, at->at_hold, | |
386 | (struct sockaddr *)&sin, (struct rtentry *)0); | |
387 | at->at_hold = 0; | |
388 | } | |
389 | } | |
390 | if (at == 0 && itaddr.s_addr == myaddr.s_addr) { | |
391 | /* ensure we have a table entry */ | |
392 | if (at = arptnew(&isaddr)) { | |
393 | bcopy((caddr_t)ea->arp_sha, (caddr_t)at->at_enaddr, | |
394 | sizeof(ea->arp_sha)); | |
395 | completed = 1; | |
396 | at->at_flags |= ATF_COM; | |
397 | } | |
398 | } | |
399 | splx(s); | |
400 | reply: | |
401 | switch (proto) { | |
402 | ||
403 | case ETHERTYPE_IPTRAILERS: | |
404 | /* partner says trailers are OK */ | |
405 | if (at) | |
406 | at->at_flags |= ATF_USETRAILERS; | |
407 | /* | |
408 | * Reply to request iff we want trailers. | |
409 | */ | |
410 | if (op != ARPOP_REQUEST || ac->ac_if.if_flags & IFF_NOTRAILERS) | |
411 | goto out; | |
412 | break; | |
413 | ||
414 | case ETHERTYPE_IP: | |
415 | /* | |
416 | * Reply if this is an IP request, | |
417 | * or if we want to send a trailer response. | |
418 | * Send the latter only to the IP response | |
419 | * that completes the current ARP entry. | |
420 | */ | |
421 | if (op != ARPOP_REQUEST && | |
422 | (completed == 0 || ac->ac_if.if_flags & IFF_NOTRAILERS)) | |
423 | goto out; | |
424 | } | |
425 | if (itaddr.s_addr == myaddr.s_addr) { | |
426 | /* I am the target */ | |
427 | bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, | |
428 | sizeof(ea->arp_sha)); | |
429 | bcopy((caddr_t)ac->ac_enaddr, (caddr_t)ea->arp_sha, | |
430 | sizeof(ea->arp_sha)); | |
431 | } else { | |
432 | ARPTAB_LOOK(at, itaddr.s_addr); | |
433 | if (at == NULL || (at->at_flags & ATF_PUBL) == 0) | |
434 | goto out; | |
435 | bcopy((caddr_t)ea->arp_sha, (caddr_t)ea->arp_tha, | |
436 | sizeof(ea->arp_sha)); | |
437 | bcopy((caddr_t)at->at_enaddr, (caddr_t)ea->arp_sha, | |
438 | sizeof(ea->arp_sha)); | |
439 | } | |
440 | ||
441 | bcopy((caddr_t)ea->arp_spa, (caddr_t)ea->arp_tpa, | |
442 | sizeof(ea->arp_spa)); | |
443 | bcopy((caddr_t)&itaddr, (caddr_t)ea->arp_spa, | |
444 | sizeof(ea->arp_spa)); | |
445 | ea->arp_op = htons(ARPOP_REPLY); | |
446 | /* | |
447 | * If incoming packet was an IP reply, | |
448 | * we are sending a reply for type IPTRAILERS. | |
449 | * If we are sending a reply for type IP | |
450 | * and we want to receive trailers, | |
451 | * send a trailer reply as well. | |
452 | */ | |
453 | if (op == ARPOP_REPLY) | |
454 | ea->arp_pro = htons(ETHERTYPE_IPTRAILERS); | |
455 | else if (proto == ETHERTYPE_IP && | |
456 | (ac->ac_if.if_flags & IFF_NOTRAILERS) == 0) | |
457 | mcopy = m_copy(m, 0, (int)M_COPYALL); | |
458 | eh = (struct ether_header *)sa.sa_data; | |
459 | bcopy((caddr_t)ea->arp_tha, (caddr_t)eh->ether_dhost, | |
460 | sizeof(eh->ether_dhost)); | |
461 | eh->ether_type = ETHERTYPE_ARP; | |
462 | sa.sa_family = AF_UNSPEC; | |
463 | sa.sa_len = sizeof(sa); | |
464 | (*ac->ac_if.if_output)(&ac->ac_if, m, &sa, (struct rtentry *)0); | |
465 | if (mcopy) { | |
466 | ea = mtod(mcopy, struct ether_arp *); | |
467 | ea->arp_pro = htons(ETHERTYPE_IPTRAILERS); | |
468 | (*ac->ac_if.if_output)(&ac->ac_if, | |
469 | mcopy, &sa, (struct rtentry *)0); | |
470 | } | |
471 | return; | |
472 | out: | |
473 | m_freem(m); | |
474 | return; | |
475 | } | |
476 | ||
477 | /* | |
478 | * Free an arptab entry. | |
479 | */ | |
4c45483e | 480 | void |
15637ed4 RG |
481 | arptfree(at) |
482 | register struct arptab *at; | |
483 | { | |
484 | int s = splimp(); | |
485 | ||
486 | if (at->at_hold) | |
487 | m_freem(at->at_hold); | |
488 | at->at_hold = 0; | |
489 | at->at_timer = at->at_flags = 0; | |
490 | at->at_iaddr.s_addr = 0; | |
491 | splx(s); | |
492 | } | |
493 | ||
494 | /* | |
495 | * Enter a new address in arptab, pushing out the oldest entry | |
496 | * from the bucket if there is no room. | |
497 | * This always succeeds since no bucket can be completely filled | |
498 | * with permanent entries (except from arpioctl when testing whether | |
499 | * another permanent entry will fit). | |
500 | * MUST BE CALLED AT SPLIMP. | |
501 | */ | |
502 | struct arptab * | |
503 | arptnew(addr) | |
504 | struct in_addr *addr; | |
505 | { | |
506 | register n; | |
507 | int oldest = -1; | |
508 | register struct arptab *at, *ato = NULL; | |
509 | static int first = 1; | |
510 | ||
511 | if (first) { | |
512 | first = 0; | |
513 | timeout(arptimer, (caddr_t)0, hz); | |
514 | } | |
515 | at = &arptab[ARPTAB_HASH(addr->s_addr) * ARPTAB_BSIZ]; | |
516 | for (n = 0; n < ARPTAB_BSIZ; n++,at++) { | |
517 | if (at->at_flags == 0) | |
518 | goto out; /* found an empty entry */ | |
519 | if (at->at_flags & ATF_PERM) | |
520 | continue; | |
521 | if ((int) at->at_timer > oldest) { | |
522 | oldest = at->at_timer; | |
523 | ato = at; | |
524 | } | |
525 | } | |
526 | if (ato == NULL) | |
527 | return (NULL); | |
528 | at = ato; | |
529 | arptfree(at); | |
530 | out: | |
531 | at->at_iaddr = *addr; | |
532 | at->at_flags = ATF_INUSE; | |
533 | return (at); | |
534 | } | |
535 | ||
4c45483e | 536 | int |
15637ed4 RG |
537 | arpioctl(cmd, data) |
538 | int cmd; | |
539 | caddr_t data; | |
540 | { | |
541 | register struct arpreq *ar = (struct arpreq *)data; | |
542 | register struct arptab *at; | |
543 | register struct sockaddr_in *sin; | |
544 | int s; | |
545 | ||
546 | sin = (struct sockaddr_in *)&ar->arp_ha; | |
547 | #if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN | |
548 | if (sin->sin_family == 0 && sin->sin_len < 16) | |
549 | sin->sin_family = sin->sin_len; | |
550 | #endif | |
551 | sin->sin_len = sizeof(ar->arp_ha); | |
552 | sin = (struct sockaddr_in *)&ar->arp_pa; | |
553 | #if defined(COMPAT_43) && BYTE_ORDER != BIG_ENDIAN | |
554 | if (sin->sin_family == 0 && sin->sin_len < 16) | |
555 | sin->sin_family = sin->sin_len; | |
556 | #endif | |
557 | sin->sin_len = sizeof(ar->arp_pa); | |
558 | if (ar->arp_pa.sa_family != AF_INET || | |
559 | ar->arp_ha.sa_family != AF_UNSPEC) | |
560 | return (EAFNOSUPPORT); | |
561 | s = splimp(); | |
562 | ARPTAB_LOOK(at, sin->sin_addr.s_addr); | |
563 | if (at == NULL) { /* not found */ | |
564 | if (cmd != SIOCSARP) { | |
565 | splx(s); | |
566 | return (ENXIO); | |
567 | } | |
568 | if (ifa_ifwithnet(&ar->arp_pa) == NULL) { | |
569 | splx(s); | |
570 | return (ENETUNREACH); | |
571 | } | |
572 | } | |
573 | switch (cmd) { | |
574 | ||
575 | case SIOCSARP: /* set entry */ | |
576 | if (at == NULL) { | |
577 | at = arptnew(&sin->sin_addr); | |
578 | if (at == NULL) { | |
579 | splx(s); | |
580 | return (EADDRNOTAVAIL); | |
581 | } | |
582 | if (ar->arp_flags & ATF_PERM) { | |
583 | /* never make all entries in a bucket permanent */ | |
584 | register struct arptab *tat; | |
585 | ||
586 | /* try to re-allocate */ | |
587 | tat = arptnew(&sin->sin_addr); | |
588 | if (tat == NULL) { | |
589 | arptfree(at); | |
590 | splx(s); | |
591 | return (EADDRNOTAVAIL); | |
592 | } | |
593 | arptfree(tat); | |
594 | } | |
595 | } | |
596 | bcopy((caddr_t)ar->arp_ha.sa_data, (caddr_t)at->at_enaddr, | |
597 | sizeof(at->at_enaddr)); | |
598 | at->at_flags = ATF_COM | ATF_INUSE | | |
599 | (ar->arp_flags & (ATF_PERM|ATF_PUBL|ATF_USETRAILERS)); | |
600 | at->at_timer = 0; | |
601 | break; | |
602 | ||
603 | case SIOCDARP: /* delete entry */ | |
604 | arptfree(at); | |
605 | break; | |
606 | ||
607 | case SIOCGARP: /* get entry */ | |
608 | case OSIOCGARP: | |
609 | bcopy((caddr_t)at->at_enaddr, (caddr_t)ar->arp_ha.sa_data, | |
610 | sizeof(at->at_enaddr)); | |
611 | #ifdef COMPAT_43 | |
612 | if (cmd == OSIOCGARP) | |
613 | *(u_short *)&ar->arp_ha = ar->arp_ha.sa_family; | |
614 | #endif | |
615 | ar->arp_flags = at->at_flags; | |
616 | break; | |
617 | } | |
618 | splx(s); | |
619 | return (0); | |
620 | } |