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