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