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