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