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