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