]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
de355530 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
1c79356b | 11 | * |
de355530 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
de355530 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* Copyright (c) 1988, 1989, 1997, 1998 Apple Computer, Inc. | |
23 | * | |
24 | * Modified for MP, 1996 by Tuyen Nguyen | |
25 | * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX. | |
26 | */ | |
27 | ||
28 | /* at_aarp.c: 2.0, 1.17; 10/4/93; Apple Computer, Inc. */; | |
29 | ||
30 | /* This file is at_aarp.c and it contains all the routines used by AARP. This | |
31 | * is part of the LAP layer. | |
32 | */ | |
33 | ||
34 | #include <sys/errno.h> | |
35 | #include <sys/types.h> | |
36 | #include <sys/param.h> | |
37 | #include <machine/spl.h> | |
38 | #include <sys/systm.h> | |
39 | #include <sys/kernel.h> | |
40 | #include <sys/proc.h> | |
41 | #include <sys/filedesc.h> | |
42 | #include <sys/fcntl.h> | |
43 | #include <sys/mbuf.h> | |
44 | #include <sys/ioctl.h> | |
45 | #include <sys/malloc.h> | |
46 | #include <sys/socket.h> | |
47 | #include <sys/socketvar.h> | |
48 | ||
49 | #include <net/if.h> | |
50 | #include <net/if_types.h> | |
51 | ||
52 | #include <netat/sysglue.h> | |
53 | #include <netat/appletalk.h> | |
54 | #include <netat/ddp.h> | |
55 | #include <netat/at_snmp.h> | |
56 | #include <netat/at_pcb.h> | |
57 | #include <netat/at_var.h> | |
58 | #include <netat/at_aarp.h> | |
59 | #include <netat/debug.h> | |
60 | ||
9bccf70c A |
61 | #include <sys/kern_event.h> |
62 | ||
1c79356b A |
63 | static int probing; |
64 | /* Following two variables are used to keep track of how many dynamic addresses | |
65 | * we have tried out at startup. | |
66 | */ | |
67 | int no_of_nodes_tried; /* no of node addresses we've tried | |
68 | * so far, within a network number | |
69 | */ | |
70 | int no_of_nets_tried; /* no. of network numbers tried | |
71 | */ | |
72 | ||
73 | struct etalk_addr et_zeroaddr = { | |
74 | {0, 0, 0, 0, 0, 0}}; | |
75 | ||
76 | aarp_amt_t probe_cb; | |
77 | aarp_amt_array *aarp_table[IF_TOTAL_MAX]; | |
78 | ||
79 | int aarp_init1(), aarp_init2(); | |
80 | int aarp_send_data(); | |
81 | int aarp_sched_probe(); | |
82 | ||
83 | StaticProc int aarp_req_cmd_in(); | |
84 | StaticProc int aarp_resp_cmd_in(); | |
85 | StaticProc int aarp_probe_cmd_in(); | |
86 | StaticProc int aarp_send_resp(); | |
87 | StaticProc int aarp_send_req(); | |
88 | StaticProc int aarp_send_probe(); | |
89 | StaticProc aarp_amt_t *aarp_lru_entry(); | |
90 | StaticProc int aarp_glean_info(); | |
91 | StaticProc int aarp_delete_amt_info(); | |
92 | StaticProc void aarp_build_pkt(); | |
93 | StaticProc int aarp_sched_req(); | |
94 | StaticProc int aarp_get_rand_node(); | |
95 | StaticProc int aarp_get_next_node(); | |
96 | StaticProc int aarp_get_rand_net(); | |
97 | atlock_t arpinp_lock; | |
98 | ||
99 | extern void AARPwakeup(aarp_amt_t *); | |
100 | extern int pat_output(at_ifaddr_t *, gbuf_t *, unsigned char *, int); | |
101 | ||
102 | /**************************************************************************** | |
103 | * aarp_init() | |
104 | * | |
105 | ****************************************************************************/ | |
106 | ||
107 | int aarp_init1(elapp) | |
108 | register at_ifaddr_t *elapp; | |
109 | { | |
110 | elapp->ifThisNode.s_net = 0; | |
111 | elapp->ifThisNode.s_node = 0; | |
112 | ||
113 | if (probing != PROBE_TENTATIVE) /* How do I set the initial probe */ | |
114 | probing = PROBE_IDLE; /* state ???*/ | |
115 | else { | |
116 | dPrintf(D_M_AARP,D_L_ERROR, | |
117 | ("aarp_init: error :probing == PROBE_TENTATIVE\n")); | |
118 | return(-1); | |
119 | } | |
120 | ||
121 | /* pick a random addr or start with what we have from initial_node addr */ | |
122 | if (elapp->initial_addr.s_net == 0 && elapp->initial_addr.s_node == 0) { | |
123 | dPrintf(D_M_AARP, D_L_INFO, | |
124 | ("aarp_init: pick up a new node number\n")); | |
125 | aarp_get_rand_node(elapp); | |
126 | aarp_get_rand_net(elapp); | |
127 | } | |
128 | probe_cb.elapp = elapp; | |
129 | probe_cb.no_of_retries = 0; | |
130 | probe_cb.error = 0; | |
131 | ||
132 | no_of_nodes_tried = 0; /* haven't tried any addresses yet */ | |
133 | no_of_nets_tried = 0; | |
134 | ||
135 | if (aarp_send_probe() == -1) { | |
136 | probing = PROBE_IDLE; /* not probing any more */ | |
137 | dPrintf(D_M_AARP, D_L_ERROR, | |
138 | ("aarp_init: aarp_send_probe returns error\n")); | |
139 | return(-1); | |
140 | } | |
141 | return(ENOTREADY); | |
142 | } | |
143 | ||
144 | int aarp_init2(elapp) | |
145 | register at_ifaddr_t *elapp; | |
146 | { | |
147 | if (probe_cb.error != 0) { | |
148 | probing = PROBE_IDLE; /* not probing any more */ | |
149 | dPrintf(D_M_AARP, D_L_ERROR, | |
150 | ("aarp_init: probe_cb.error creates error =%d\n", | |
151 | probe_cb.error)); | |
152 | return(-1); | |
153 | } | |
154 | ||
155 | if (aarp_table[elapp->ifPort]) | |
156 | bzero ((caddr_t)&aarp_table[elapp->ifPort]->et_aarp_amt[0], | |
157 | sizeof(aarp_amt_array)); | |
158 | else | |
159 | return(-1); | |
160 | ||
161 | elapp->ifThisNode = elapp->initial_addr; | |
162 | probing = PROBE_DONE; | |
9bccf70c A |
163 | |
164 | /* AppleTalk was successfully started up. Send event with node and net. */ | |
165 | atalk_post_msg(elapp->aa_ifp, KEV_ATALK_ENABLED, &(elapp->ifThisNode), 0); | |
166 | ||
167 | /* Set global flag */ | |
168 | at_state.flags |= AT_ST_STARTED; | |
169 | ||
1c79356b A |
170 | return(0); |
171 | } | |
172 | ||
173 | /**************************************************************************** | |
174 | * aarp_rcv_pkt() | |
175 | * | |
176 | * remarks : | |
177 | * (1) The caller must take care of freeing the real storage (gbuf) | |
178 | * (2) The incoming packet is of the form {802.3, 802.2, aarp}. | |
179 | * | |
180 | ****************************************************************************/ | |
181 | int aarp_rcv_pkt(pkt, elapp) | |
182 | aarp_pkt_t *pkt; | |
183 | at_ifaddr_t *elapp; | |
184 | { | |
185 | switch (pkt->aarp_cmd) { | |
186 | case AARP_REQ_CMD: | |
187 | return (aarp_req_cmd_in (pkt, elapp)); | |
188 | case AARP_RESP_CMD: | |
189 | return (aarp_resp_cmd_in (pkt, elapp)); | |
190 | case AARP_PROBE_CMD: | |
191 | return (aarp_probe_cmd_in (pkt, elapp)); | |
192 | default: | |
193 | return (-1); | |
194 | }/* end of switch*/ | |
195 | } | |
196 | ||
197 | /**************************************************************************** | |
198 | * aarp_req_cmd_in() | |
199 | * | |
200 | ****************************************************************************/ | |
201 | StaticProc int aarp_req_cmd_in (pkt, elapp) | |
202 | aarp_pkt_t *pkt; | |
203 | at_ifaddr_t *elapp; | |
204 | { | |
205 | /* | |
206 | kprintf("aarp_req_cmd_in: ifThisNode=%d:%d srcNode=%d:%d dstNode=%d:%d\n", | |
207 | elapp->ifThisNode.s_net, | |
208 | elapp->ifThisNode.s_node, | |
209 | NET_VALUE(pkt->src_at_addr.atalk_net), | |
210 | pkt->src_at_addr.atalk_node, | |
211 | NET_VALUE(pkt->dest_at_addr.atalk_net), | |
212 | pkt->dest_at_addr.atalk_node); | |
213 | */ | |
214 | if ((probing == PROBE_DONE) && | |
215 | (NET_VALUE(pkt->dest_at_addr.atalk_net) == elapp->ifThisNode.s_net) && | |
216 | (pkt->dest_at_addr.atalk_node == elapp->ifThisNode.s_node)) { | |
217 | if (aarp_send_resp(elapp, pkt) == -1) | |
218 | return(-1); | |
219 | } | |
220 | /* now to glean some info */ | |
221 | aarp_glean_info(pkt, elapp); | |
222 | return (0); | |
223 | } | |
224 | ||
225 | ||
226 | ||
227 | /**************************************************************************** | |
228 | * aarp_resp_cmd_in() | |
229 | * | |
230 | ****************************************************************************/ | |
231 | StaticProc int aarp_resp_cmd_in (pkt, elapp) | |
232 | aarp_pkt_t *pkt; | |
233 | at_ifaddr_t *elapp; | |
234 | { | |
235 | register aarp_amt_t *amt_ptr; | |
236 | gbuf_t *m; | |
237 | ||
238 | switch (probing) { | |
239 | case PROBE_TENTATIVE : | |
240 | if ((NET_VALUE(pkt->src_at_addr.atalk_net) == | |
241 | probe_cb.elapp->initial_addr.s_net) && | |
242 | (pkt->src_at_addr.atalk_node == | |
243 | probe_cb.elapp->initial_addr.s_node)) { | |
244 | ||
245 | /* this is a response to AARP_PROBE_CMD. There's | |
246 | * someone out there with the address we desire | |
247 | * for ourselves. | |
248 | */ | |
249 | untimeout(aarp_sched_probe, 0); | |
250 | probe_cb.no_of_retries = 0; | |
251 | aarp_get_next_node(probe_cb.elapp); | |
252 | no_of_nodes_tried++; | |
253 | ||
254 | if (no_of_nodes_tried == AARP_MAX_NODES_TRIED) { | |
255 | aarp_get_rand_net(probe_cb.elapp); | |
256 | aarp_get_rand_node(probe_cb.elapp); | |
257 | no_of_nodes_tried = 0; | |
258 | no_of_nets_tried++; | |
259 | } | |
260 | if (no_of_nets_tried == AARP_MAX_NETS_TRIED) { | |
261 | /* We have tried enough nodes and nets, give up. | |
262 | */ | |
263 | probe_cb.error = EADDRNOTAVAIL; | |
264 | AARPwakeup(&probe_cb); | |
265 | return(0); | |
266 | } | |
267 | if (aarp_send_probe() == -1) { | |
268 | /* expecting aarp_send_probe to fill in | |
269 | * probe_cb.error | |
270 | */ | |
271 | AARPwakeup(&probe_cb); | |
272 | return(-1); | |
273 | } | |
274 | } else { | |
275 | /* hmmmm! got a response packet while still probing | |
276 | * for AT address and the AT dest address doesn't | |
277 | * match!! | |
278 | * What should I do here?? kkkkkkkkk | |
279 | */ | |
280 | return(-1); | |
281 | } | |
282 | break; | |
283 | ||
284 | case PROBE_DONE : | |
285 | AMT_LOOK(amt_ptr, pkt->src_at_addr, elapp); | |
286 | if (amt_ptr == NULL) | |
287 | return(-1); | |
288 | if (amt_ptr->tmo) { | |
289 | untimeout(aarp_sched_req, amt_ptr); | |
290 | amt_ptr->tmo = 0; | |
291 | } | |
292 | ||
293 | if (amt_ptr->m == NULL) { | |
294 | /* this may be because of a belated response to | |
295 | * aarp reaquest. Based on an earlier response, we | |
296 | * might have already sent the packet out, so | |
297 | * there's nothing to send now. This is okay, no | |
298 | * error. | |
299 | */ | |
300 | return(0); | |
301 | } | |
302 | amt_ptr->dest_addr = pkt->src_addr; | |
303 | if (FDDI_OR_TOKENRING(elapp->aa_ifp->if_type)) | |
304 | ddp_bit_reverse(&amt_ptr->dest_addr); | |
305 | m = amt_ptr->m; | |
306 | amt_ptr->m = NULL; | |
307 | pat_output(amt_ptr->elapp, m, | |
308 | (unsigned char *)&amt_ptr->dest_addr, 0); | |
309 | break; | |
310 | default : | |
311 | /* probing in a weird state?? */ | |
312 | return(-1); | |
313 | } | |
314 | return(0); | |
315 | } | |
316 | ||
317 | ||
318 | ||
319 | /**************************************************************************** | |
320 | * aarp_probe_cmd_in() | |
321 | * | |
322 | ****************************************************************************/ | |
323 | StaticProc int aarp_probe_cmd_in (pkt, elapp) | |
324 | register aarp_pkt_t *pkt; | |
325 | at_ifaddr_t *elapp; | |
326 | { | |
327 | register aarp_amt_t *amt_ptr; | |
328 | ||
329 | switch (probing) { | |
330 | case PROBE_TENTATIVE : | |
331 | if ((elapp == probe_cb.elapp) && | |
332 | (NET_VALUE(pkt->src_at_addr.atalk_net) == | |
333 | probe_cb.elapp->initial_addr.s_net) && | |
334 | (pkt->src_at_addr.atalk_node == | |
335 | probe_cb.elapp->initial_addr.s_node)) { | |
336 | /* some bozo is probing for address I want... and I | |
337 | * can't tell him to shove off! | |
338 | */ | |
339 | untimeout(aarp_sched_probe, 0); | |
340 | probe_cb.no_of_retries = 0; | |
341 | aarp_get_next_node(probe_cb.elapp); | |
342 | no_of_nodes_tried++; | |
343 | ||
344 | if (no_of_nodes_tried == AARP_MAX_NODES_TRIED) { | |
345 | aarp_get_rand_net(probe_cb.elapp); | |
346 | aarp_get_rand_node(probe_cb.elapp); | |
347 | no_of_nodes_tried = 0; | |
348 | no_of_nets_tried++; | |
349 | } | |
350 | if (no_of_nets_tried == AARP_MAX_NETS_TRIED) { | |
351 | /* We have tried enough nodes and nets, give up. | |
352 | */ | |
353 | probe_cb.error = EADDRNOTAVAIL; | |
354 | AARPwakeup(&probe_cb); | |
355 | return(0); | |
356 | } | |
357 | if (aarp_send_probe() == -1) { | |
358 | /* expecting aarp_send_probe to fill in | |
359 | * probe_cb.error | |
360 | */ | |
361 | AARPwakeup(&probe_cb); | |
362 | return(-1); | |
363 | } | |
364 | } else { | |
365 | /* somebody's probing... none of my business yet, so | |
366 | * just ignore the packet | |
367 | */ | |
368 | return (0); | |
369 | } | |
370 | break; | |
371 | ||
372 | case PROBE_DONE : | |
373 | if ((NET_VALUE(pkt->src_at_addr.atalk_net) == elapp->ifThisNode.s_net) && | |
374 | (pkt->src_at_addr.atalk_node == elapp->ifThisNode.s_node)) { | |
375 | if (aarp_send_resp(elapp, pkt) == -1) | |
376 | return (-1); | |
377 | return (0); | |
378 | } | |
379 | AMT_LOOK(amt_ptr, pkt->src_at_addr, elapp); | |
380 | ||
381 | if (amt_ptr) | |
382 | aarp_delete_amt_info(amt_ptr); | |
383 | break; | |
384 | default : | |
385 | /* probing in a weird state?? */ | |
386 | return (-1); | |
387 | } | |
388 | return (0); | |
389 | } | |
390 | ||
391 | ||
392 | ||
393 | /**************************************************************************** | |
394 | * aarp_chk_addr() | |
395 | ****************************************************************************/ | |
396 | int aarp_chk_addr(ddp_hdrp, elapp) | |
397 | at_ddp_t *ddp_hdrp; | |
398 | at_ifaddr_t *elapp; | |
399 | { | |
400 | if ((ddp_hdrp->dst_node == elapp->ifThisNode.s_node) && | |
401 | (NET_VALUE(ddp_hdrp->dst_net) == elapp->ifThisNode.s_net)) { | |
402 | return(0); /* exact match in address */ | |
403 | } | |
404 | ||
405 | if (AARP_BROADCAST(ddp_hdrp, elapp)) { | |
406 | return(0); /* some kind of broadcast address */ | |
407 | } | |
408 | return (AARP_ERR_NOT_OURS); /* not for us */ | |
409 | } | |
410 | ||
411 | ||
412 | ||
413 | /**************************************************************************** | |
414 | * aarp_send_data() | |
415 | * | |
416 | * remarks : | |
417 | * 1. The message coming in would be of the form {802.3, 802.2, ddp,...} | |
418 | * | |
419 | * 2. The message coming in would be freed here if transmission goes | |
420 | * through okay. If an error is returned by aarp_send_data, the caller | |
421 | * can assume that the message is not freed. The exception to | |
422 | * this scenario is the prepended atalk_addr field. This field | |
423 | * will ALWAYS be removed. If the message is dropped, | |
424 | * it's not an "error". | |
425 | * | |
426 | ****************************************************************************/ | |
427 | ||
428 | int aarp_send_data(m, elapp, dest_at_addr, loop) | |
429 | register gbuf_t *m; | |
430 | register at_ifaddr_t *elapp; | |
431 | struct atalk_addr *dest_at_addr; | |
432 | int loop; /* if true, loopback broadcasts */ | |
433 | { | |
434 | register aarp_amt_t *amt_ptr; | |
435 | register at_ddp_t *ddp_hdrp; | |
436 | int error; | |
437 | int s; | |
438 | ||
439 | if (gbuf_len(m) <= 0) | |
440 | ddp_hdrp = (at_ddp_t *)gbuf_rptr(gbuf_cont(m)); | |
441 | else | |
442 | ddp_hdrp = (at_ddp_t *)gbuf_rptr(m); | |
443 | ||
444 | if ((ddp_hdrp->dst_node == ddp_hdrp->src_node) && | |
445 | (NET_VALUE(ddp_hdrp->dst_net) == NET_VALUE(ddp_hdrp->src_net))) { | |
446 | /* | |
447 | * we're sending to ourselves | |
448 | * so loop it back upstream | |
449 | */ | |
450 | ddp_input(m, elapp); | |
451 | return(0); | |
452 | } | |
453 | ATDISABLE(s, arpinp_lock); | |
454 | AMT_LOOK(amt_ptr, *dest_at_addr, elapp); | |
455 | ||
456 | ||
457 | if (amt_ptr) { | |
458 | if (amt_ptr->m) { | |
459 | /* | |
460 | * there's already a packet awaiting transmission, so | |
461 | * drop this one and let the upper layer retransmit | |
462 | * later. | |
463 | */ | |
464 | ATENABLE(s, arpinp_lock); | |
465 | gbuf_freel(m); | |
466 | return (0); | |
467 | } | |
468 | ATENABLE(s, arpinp_lock); | |
469 | return (pat_output(elapp, m, | |
470 | (unsigned char *)&amt_ptr->dest_addr, 0)); | |
471 | } | |
472 | /* | |
473 | * either this is a packet to be broadcasted, or the address | |
474 | * resolution needs to be done | |
475 | */ | |
476 | if (AARP_BROADCAST(ddp_hdrp, elapp)) { | |
477 | gbuf_t *newm = 0; | |
478 | struct etalk_addr *dest_addr; | |
479 | ||
480 | ATENABLE(s, arpinp_lock); | |
481 | dest_addr = &elapp->cable_multicast_addr; | |
482 | if (loop) | |
483 | newm = (gbuf_t *)gbuf_dupm(m); | |
484 | ||
485 | if ( !(error = pat_output(elapp, m, | |
486 | (unsigned char *)dest_addr, 0))) { | |
487 | /* | |
488 | * The message transmitted successfully; | |
489 | * Also loop a copy back up since this | |
490 | * is a broadcast message. | |
491 | */ | |
492 | if (loop) { | |
493 | if (newm == NULL) | |
494 | return (error); | |
495 | ddp_input(newm, elapp); | |
496 | } /* endif loop */ | |
497 | } else { | |
498 | if (newm) | |
499 | gbuf_freem(newm); | |
500 | } | |
501 | return (error); | |
502 | } | |
503 | NEW_AMT(amt_ptr, *dest_at_addr,elapp); | |
504 | ||
505 | if (amt_ptr->m) { | |
506 | /* | |
507 | * no non-busy slots available in the cache, so | |
508 | * drop this one and let the upper layer retransmit | |
509 | * later. | |
510 | */ | |
511 | ATENABLE(s, arpinp_lock); | |
512 | gbuf_freel(m); | |
513 | return (0); | |
514 | } | |
515 | amt_ptr->dest_at_addr = *dest_at_addr; | |
516 | amt_ptr->dest_at_addr.atalk_unused = 0; | |
517 | ||
518 | amt_ptr->last_time = time.tv_sec; | |
519 | amt_ptr->m = m; | |
520 | amt_ptr->elapp = elapp; | |
521 | amt_ptr->no_of_retries = 0; | |
522 | ATENABLE(s, arpinp_lock); | |
523 | ||
524 | if ((error = aarp_send_req(amt_ptr))) { | |
525 | aarp_delete_amt_info(amt_ptr); | |
526 | return(error); | |
527 | } | |
528 | return(0); | |
529 | } | |
530 | ||
531 | ||
532 | ||
533 | /**************************************************************************** | |
534 | * aarp_send_resp() | |
535 | * | |
536 | * remarks : | |
537 | * The pkt being passed here is only to "look at". It should neither | |
538 | * be used for transmission, nor freed. Its contents also must not be | |
539 | * altered. | |
540 | * | |
541 | ****************************************************************************/ | |
542 | StaticProc int aarp_send_resp(elapp, pkt) | |
543 | register at_ifaddr_t *elapp; | |
544 | aarp_pkt_t *pkt; | |
545 | { | |
546 | register aarp_pkt_t *new_pkt; | |
547 | register gbuf_t *m; | |
548 | ||
549 | if ((m = gbuf_alloc(AT_WR_OFFSET+sizeof(aarp_pkt_t), PRI_MED)) == NULL) { | |
550 | return (-1); | |
551 | } | |
552 | gbuf_rinc(m,AT_WR_OFFSET); | |
553 | gbuf_wset(m,0); | |
554 | ||
555 | new_pkt = (aarp_pkt_t *)gbuf_rptr(m); | |
556 | aarp_build_pkt(new_pkt, elapp); | |
557 | ||
558 | new_pkt->aarp_cmd = AARP_RESP_CMD; | |
559 | new_pkt->dest_addr = pkt->src_addr; | |
560 | ||
561 | new_pkt->dest_at_addr = pkt->src_at_addr; | |
562 | new_pkt->dest_at_addr.atalk_unused = 0; | |
563 | ||
564 | ATALK_ASSIGN(new_pkt->src_at_addr, elapp->ifThisNode.s_net, | |
565 | elapp->ifThisNode.s_node, 0); | |
566 | ||
567 | gbuf_winc(m,sizeof(aarp_pkt_t)); | |
568 | if (FDDI_OR_TOKENRING(elapp->aa_ifp->if_type)) | |
569 | ddp_bit_reverse(&new_pkt->dest_addr); | |
570 | ||
571 | if (pat_output(elapp, m, (unsigned char *)&new_pkt->dest_addr, | |
572 | AARP_AT_TYPE)) | |
573 | return(-1); | |
574 | return(0); | |
575 | } | |
576 | ||
577 | ||
578 | ||
579 | /**************************************************************************** | |
580 | * aarp_send_req() | |
581 | * | |
582 | ****************************************************************************/ | |
583 | ||
584 | StaticProc int aarp_send_req (amt_ptr) | |
585 | register aarp_amt_t *amt_ptr; | |
586 | { | |
587 | register aarp_pkt_t *pkt; | |
588 | register gbuf_t *m; | |
589 | int error; | |
590 | ||
591 | if ((m = gbuf_alloc(AT_WR_OFFSET+sizeof(aarp_pkt_t), PRI_MED)) == NULL) { | |
592 | return (ENOBUFS); | |
593 | } | |
594 | gbuf_rinc(m,AT_WR_OFFSET); | |
595 | gbuf_wset(m,0); | |
596 | ||
597 | pkt = (aarp_pkt_t *)gbuf_rptr(m); | |
598 | aarp_build_pkt(pkt, amt_ptr->elapp); | |
599 | ||
600 | pkt->aarp_cmd = AARP_REQ_CMD; | |
601 | pkt->dest_addr = et_zeroaddr; | |
602 | pkt->dest_at_addr = amt_ptr->dest_at_addr; | |
603 | pkt->dest_at_addr.atalk_unused = 0; | |
604 | ATALK_ASSIGN(pkt->src_at_addr, amt_ptr->elapp->ifThisNode.s_net, | |
605 | amt_ptr->elapp->ifThisNode.s_node, 0); | |
606 | gbuf_winc(m,sizeof(aarp_pkt_t)); | |
607 | ||
608 | amt_ptr->no_of_retries++; | |
609 | timeout(aarp_sched_req, amt_ptr, AARP_REQ_TIMER_INT); | |
610 | amt_ptr->tmo = 1; | |
611 | error = pat_output(amt_ptr->elapp, m, | |
612 | (unsigned char *)&amt_ptr->elapp->cable_multicast_addr, AARP_AT_TYPE); | |
613 | if (error) | |
614 | { | |
615 | untimeout(aarp_sched_req, amt_ptr); | |
616 | amt_ptr->tmo = 0; | |
617 | return(error); | |
618 | } | |
619 | ||
620 | return(0); | |
621 | } | |
622 | ||
623 | ||
624 | ||
625 | /**************************************************************************** | |
626 | * aarp_send_probe() | |
627 | * | |
628 | ****************************************************************************/ | |
629 | StaticProc int aarp_send_probe() | |
630 | { | |
631 | register aarp_pkt_t *pkt; | |
632 | register gbuf_t *m; | |
633 | ||
634 | if ((m = gbuf_alloc(AT_WR_OFFSET+sizeof(aarp_pkt_t), PRI_MED)) == NULL) { | |
635 | probe_cb.error = ENOBUFS; | |
636 | return (-1); | |
637 | } | |
638 | gbuf_rinc(m,AT_WR_OFFSET); | |
639 | gbuf_wset(m,0); | |
640 | pkt = (aarp_pkt_t *)gbuf_rptr(m); | |
641 | aarp_build_pkt(pkt, probe_cb.elapp); | |
642 | ||
643 | pkt->aarp_cmd = AARP_PROBE_CMD; | |
644 | pkt->dest_addr = et_zeroaddr; | |
645 | ||
646 | ATALK_ASSIGN(pkt->src_at_addr, probe_cb.elapp->initial_addr.s_net, | |
647 | probe_cb.elapp->initial_addr.s_node, 0); | |
648 | ||
649 | ATALK_ASSIGN(pkt->dest_at_addr, probe_cb.elapp->initial_addr.s_net, | |
650 | probe_cb.elapp->initial_addr.s_node, 0); | |
651 | ||
652 | gbuf_winc(m,sizeof(aarp_pkt_t)); | |
653 | ||
654 | probe_cb.error = pat_output(probe_cb.elapp, m, | |
655 | (unsigned char *)&probe_cb.elapp->cable_multicast_addr, AARP_AT_TYPE); | |
656 | if (probe_cb.error) { | |
657 | return(-1); | |
658 | } | |
659 | ||
660 | probing = PROBE_TENTATIVE; | |
661 | probe_cb.no_of_retries++; | |
662 | timeout(aarp_sched_probe, 0, AARP_PROBE_TIMER_INT); | |
663 | ||
664 | return(0); | |
665 | } | |
666 | ||
667 | ||
668 | ||
669 | /**************************************************************************** | |
670 | * aarp_lru_entry() | |
671 | * | |
672 | ****************************************************************************/ | |
673 | ||
674 | StaticProc aarp_amt_t *aarp_lru_entry(at) | |
675 | register aarp_amt_t *at; | |
676 | { | |
677 | register aarp_amt_t *at_ret; | |
678 | register int i; | |
679 | ||
680 | at_ret = at; | |
681 | ||
682 | for (i = 1, at++; i < AMT_BSIZ; i++, at++) { | |
683 | if (at->last_time < at_ret->last_time && (at->m == NULL)) | |
684 | at_ret = at; | |
685 | } | |
686 | return(at_ret); | |
687 | } | |
688 | ||
689 | ||
690 | ||
691 | /**************************************************************************** | |
692 | * aarp_glean_info() | |
693 | * | |
694 | ****************************************************************************/ | |
695 | ||
696 | StaticProc int aarp_glean_info(pkt, elapp) | |
697 | register aarp_pkt_t *pkt; | |
698 | at_ifaddr_t *elapp; | |
699 | { | |
700 | register aarp_amt_t *amt_ptr; | |
701 | int s; | |
702 | ||
703 | ATDISABLE(s, arpinp_lock); | |
704 | AMT_LOOK(amt_ptr, pkt->src_at_addr, elapp); | |
705 | ||
706 | if (amt_ptr == NULL) { | |
707 | /* | |
708 | * amt entry for this address doesn't exist, add it to the cache | |
709 | */ | |
710 | NEW_AMT(amt_ptr, pkt->src_at_addr,elapp); | |
711 | ||
712 | if (amt_ptr->m) | |
713 | { | |
714 | ATENABLE(s, arpinp_lock); | |
715 | return(0); /* no non-busy slots available in the cache */ | |
716 | } | |
717 | amt_ptr->dest_at_addr = pkt->src_at_addr; | |
718 | amt_ptr->dest_at_addr.atalk_unused = 0; | |
719 | ||
720 | amt_ptr->last_time = (int)random(); | |
721 | } | |
722 | /* | |
723 | * update the ethernet address | |
724 | * in either case | |
725 | */ | |
726 | amt_ptr->dest_addr = pkt->src_addr; | |
727 | if (FDDI_OR_TOKENRING(elapp->aa_ifp->if_type)) | |
728 | ddp_bit_reverse(&amt_ptr->dest_addr); | |
729 | ATENABLE(s, arpinp_lock); | |
730 | return(1); | |
731 | } | |
732 | ||
733 | ||
734 | /**************************************************************************** | |
735 | * aarp_delete_amt_info() | |
736 | * | |
737 | ****************************************************************************/ | |
738 | ||
739 | StaticProc int aarp_delete_amt_info(amt_ptr) | |
740 | register aarp_amt_t *amt_ptr; | |
741 | { | |
742 | register s; | |
743 | register gbuf_t *m; | |
744 | ATDISABLE(s, arpinp_lock); | |
745 | amt_ptr->last_time = 0; | |
746 | ATALK_ASSIGN(amt_ptr->dest_at_addr, 0, 0, 0); | |
747 | amt_ptr->no_of_retries = 0; | |
748 | ||
749 | if (amt_ptr->m) { | |
750 | m = amt_ptr->m; | |
751 | amt_ptr->m = NULL; | |
752 | ATENABLE(s, arpinp_lock); | |
753 | gbuf_freel(m); | |
754 | } | |
755 | else | |
756 | ATENABLE(s, arpinp_lock); | |
757 | return(0); | |
758 | } | |
759 | ||
760 | ||
761 | ||
762 | /**************************************************************************** | |
763 | * aarp_sched_probe() | |
764 | * | |
765 | ****************************************************************************/ | |
766 | ||
767 | int aarp_sched_probe() | |
768 | { | |
769 | boolean_t funnel_state; | |
770 | ||
771 | funnel_state = thread_funnel_set(network_flock, TRUE); | |
772 | ||
773 | if (probe_cb.no_of_retries != AARP_MAX_PROBE_RETRIES) { | |
774 | if (aarp_send_probe() == -1) | |
775 | AARPwakeup(&probe_cb); | |
776 | } else { | |
777 | probe_cb.error = 0; | |
778 | AARPwakeup(&probe_cb); | |
779 | } | |
780 | ||
781 | (void) thread_funnel_set(network_flock, FALSE); | |
782 | return(0); | |
783 | } | |
784 | ||
785 | ||
786 | ||
787 | /**************************************************************************** | |
788 | * aarp_build_pkt() | |
789 | * | |
790 | ****************************************************************************/ | |
791 | ||
792 | StaticProc void aarp_build_pkt(pkt, elapp) | |
793 | register aarp_pkt_t *pkt; | |
794 | at_ifaddr_t *elapp; | |
795 | { | |
796 | pkt->hardware_type = AARP_ETHER_HW_TYPE; | |
797 | pkt->stack_type = AARP_AT_PROTO; | |
798 | pkt->hw_addr_len = ETHERNET_ADDR_LEN; | |
799 | pkt->stack_addr_len = AARP_AT_ADDR_LEN; | |
800 | bcopy(elapp->xaddr, pkt->src_addr.etalk_addr_octet, sizeof(elapp->xaddr)); | |
801 | if (FDDI_OR_TOKENRING(elapp->aa_ifp->if_type)) | |
802 | ddp_bit_reverse(pkt->src_addr.etalk_addr_octet); | |
803 | } | |
804 | ||
805 | /**************************************************************************** | |
806 | * aarp_sched_req() | |
807 | * | |
808 | ****************************************************************************/ | |
809 | ||
810 | StaticProc int aarp_sched_req(amt_ptr) | |
811 | register aarp_amt_t *amt_ptr; | |
812 | { | |
0b4e3aa0 | 813 | int s, i; |
1c79356b A |
814 | boolean_t funnel_state; |
815 | ||
816 | funnel_state = thread_funnel_set(network_flock, TRUE); | |
817 | ||
0b4e3aa0 A |
818 | /* |
819 | * make sure pointer still valid in case interface removed | |
820 | * while trying to acquire the funnel. make sure it points | |
821 | * into one of the amt arrays. | |
822 | */ | |
823 | for (i = 0; i < IF_TOTAL_MAX; i++) { | |
824 | if (aarp_table[i] == NULL || amt_ptr < aarp_table[i] || amt_ptr >= (aarp_table[i] + 1)) | |
825 | continue; /* no match - try next entry */ | |
826 | ||
827 | /* | |
828 | * found match - pointer is valid | |
829 | */ | |
830 | ATDISABLE(s, arpinp_lock); | |
831 | if (amt_ptr->tmo == 0) { | |
832 | ATENABLE(s, arpinp_lock); | |
833 | (void) thread_funnel_set(network_flock, FALSE); | |
834 | return(0); | |
835 | } | |
836 | if (amt_ptr->no_of_retries < AARP_MAX_REQ_RETRIES) { | |
837 | ATENABLE(s, arpinp_lock); | |
838 | if (aarp_send_req(amt_ptr) == 0) { | |
839 | (void) thread_funnel_set(network_flock, FALSE); | |
840 | return(0); | |
841 | } | |
842 | ATDISABLE(s, arpinp_lock); | |
843 | } | |
844 | ATENABLE(s, arpinp_lock); | |
845 | aarp_delete_amt_info(amt_ptr); | |
846 | break; | |
847 | } | |
1c79356b A |
848 | (void) thread_funnel_set(network_flock, FALSE); |
849 | ||
850 | return(0); | |
851 | } | |
852 | ||
853 | ||
854 | ||
855 | /**************************************************************************** | |
856 | * aarp_get_rand_node() | |
857 | * | |
858 | ****************************************************************************/ | |
859 | StaticProc int aarp_get_rand_node(elapp) | |
860 | at_ifaddr_t *elapp; | |
861 | { | |
862 | register u_char node; | |
863 | ||
864 | /* | |
865 | * generate a starting node number in the range 1 thru 0xfd. | |
866 | * we use this as the starting probe point for a given net | |
867 | * To generate a different node number each time we call | |
868 | * aarp_get_next_node | |
869 | */ | |
870 | node = ((u_char)(random() & 0xff)) % 0xfd + 2; | |
871 | ||
872 | elapp->initial_addr.s_node = node; | |
873 | return(0); | |
874 | } | |
875 | ||
876 | ||
877 | ||
878 | StaticProc int aarp_get_next_node(elapp) | |
879 | at_ifaddr_t *elapp; | |
880 | { | |
881 | register u_char node = elapp->initial_addr.s_node; | |
882 | ||
883 | /* | |
884 | * return the next node number in the range 1 thru 0xfd. | |
885 | */ | |
886 | node = (node == 0xfd) ? (1) : (node+1); | |
887 | ||
888 | elapp->initial_addr.s_node = node; | |
889 | return(0); | |
890 | } | |
891 | ||
892 | ||
893 | ||
894 | ||
895 | ||
896 | /**************************************************************************** | |
897 | * aarp_get_rand_net() | |
898 | * | |
899 | ****************************************************************************/ | |
900 | StaticProc int aarp_get_rand_net(elapp) | |
901 | register at_ifaddr_t *elapp; | |
902 | { | |
903 | register at_net_al last_net, new_net; | |
904 | ||
905 | if (elapp->ifThisCableStart) { | |
906 | last_net = elapp->initial_addr.s_net; | |
907 | /* | |
908 | * the range of network numbers valid for this | |
909 | * cable is known. Try to choose a number from | |
910 | * this range only. | |
911 | */ | |
912 | new_net= ((at_net_al)random() & 0xffff); | |
913 | /* two-byte random number generated... now fit it in | |
914 | * the prescribed range | |
915 | */ | |
916 | new_net = new_net % (unsigned) (elapp->ifThisCableEnd - | |
917 | elapp->ifThisCableStart + 1) | |
918 | + elapp->ifThisCableStart; | |
919 | ||
920 | if (new_net == last_net) { | |
921 | if (new_net == elapp->ifThisCableEnd) | |
922 | new_net = elapp->ifThisCableStart; | |
923 | else | |
924 | new_net++; | |
925 | } | |
926 | elapp->initial_addr.s_net = new_net; | |
927 | } else { | |
928 | /* The range of valid network numbers for this cable | |
929 | * is not known... choose a network number from | |
930 | * startup range. | |
931 | */ | |
932 | last_net = (elapp->initial_addr.s_net & 0x00ff); | |
933 | new_net = (at_net_al)random() & 0x00ff; | |
934 | ||
935 | if (new_net == last_net) | |
936 | new_net++; | |
937 | if (new_net == 0xff) | |
938 | new_net = 0; | |
939 | elapp->initial_addr.s_net = (DDP_STARTUP_LOW | new_net); | |
940 | } | |
941 | return(0); | |
942 | } | |
943 | ||
944 | ||
945 | int getAarpTableSize(elapId) | |
946 | int elapId; /* elap_specifics array index (should be | |
947 | * changed when we add a non-ethernet type | |
948 | * of I/F to the mix. Unused for now. | |
949 | */ | |
950 | { | |
951 | return(AMTSIZE); | |
952 | } | |
953 | ||
954 | int getPhysAddrSize(elapId) | |
955 | int elapId; /* elap_specifics array index (should be | |
956 | * changed when we add a non-ethernet type | |
957 | * of I/F to the mix. Unused for now. | |
958 | */ | |
959 | { | |
960 | return(ETHERNET_ADDR_LEN); | |
961 | } | |
962 | ||
963 | #define ENTRY_SIZE sizeof(struct atalk_addr) + sizeof(struct etalk_addr) | |
964 | ||
965 | snmpAarpEnt_t *getAarp(elapId) | |
966 | int *elapId; /* I/F table to retrieve & table | |
967 | size entries on return */ | |
968 | ||
969 | /* gets aarp table for specified interface and builds | |
970 | a table in SNMP expected format. Returns pointer to said | |
971 | table and sets elapId to byte size of used portion of table | |
972 | */ | |
973 | { | |
974 | int i, cnt=0; | |
975 | aarp_amt_t *amtp; | |
976 | static snmpAarpEnt_t snmp[AMTSIZE]; | |
977 | snmpAarpEnt_t *snmpp; | |
978 | ||
979 | ||
980 | if (*elapId <0 || *elapId >= IF_TOTAL_MAX) | |
981 | return NULL; | |
982 | ||
983 | ||
984 | for (i=0, amtp = &(aarp_table[*elapId]->et_aarp_amt[0]), snmpp = snmp; | |
985 | i < AMTSIZE; i++,amtp++) { | |
986 | ||
987 | /* last_time will be 0 if entry was never used */ | |
988 | if (amtp->last_time) { | |
989 | /* copy just network & mac address. | |
990 | * For speed, we assume that the atalk_addr | |
991 | * & etalk_addr positions in the aarp_amt_t struct | |
992 | * has not changed and copy both at once | |
993 | */ | |
994 | bcopy(&amtp->dest_at_addr, &snmpp->ap_ddpAddr, ENTRY_SIZE); | |
995 | snmpp++; | |
996 | cnt++; | |
997 | ||
998 | } | |
999 | } | |
1000 | *elapId = cnt; | |
1001 | return(snmp); | |
1002 | } | |
1003 | /*#endif *//* COMMENTED_OUT */ | |
1004 |