]> git.saurik.com Git - apple/xnu.git/blame - bsd/netat/ddp_aarp.c
xnu-201.19.tar.gz
[apple/xnu.git] / bsd / netat / ddp_aarp.c
CommitLineData
1c79356b
A
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
61static int probing;
62/* Following two variables are used to keep track of how many dynamic addresses
63 * we have tried out at startup.
64 */
65int no_of_nodes_tried; /* no of node addresses we've tried
66 * so far, within a network number
67 */
68int no_of_nets_tried; /* no. of network numbers tried
69 */
70
71struct etalk_addr et_zeroaddr = {
72 {0, 0, 0, 0, 0, 0}};
73
74aarp_amt_t probe_cb;
75aarp_amt_array *aarp_table[IF_TOTAL_MAX];
76
77int aarp_init1(), aarp_init2();
78int aarp_send_data();
79int aarp_sched_probe();
80
81StaticProc int aarp_req_cmd_in();
82StaticProc int aarp_resp_cmd_in();
83StaticProc int aarp_probe_cmd_in();
84StaticProc int aarp_send_resp();
85StaticProc int aarp_send_req();
86StaticProc int aarp_send_probe();
87StaticProc aarp_amt_t *aarp_lru_entry();
88StaticProc int aarp_glean_info();
89StaticProc int aarp_delete_amt_info();
90StaticProc void aarp_build_pkt();
91StaticProc int aarp_sched_req();
92StaticProc int aarp_get_rand_node();
93StaticProc int aarp_get_next_node();
94StaticProc int aarp_get_rand_net();
95atlock_t arpinp_lock;
96
97extern void AARPwakeup(aarp_amt_t *);
98extern int pat_output(at_ifaddr_t *, gbuf_t *, unsigned char *, int);
99
100/****************************************************************************
101 * aarp_init()
102 *
103 ****************************************************************************/
104
105int 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
142int 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 ****************************************************************************/
173int 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 ****************************************************************************/
193StaticProc int aarp_req_cmd_in (pkt, elapp)
194aarp_pkt_t *pkt;
195at_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 ****************************************************************************/
223StaticProc 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 ****************************************************************************/
315StaticProc int aarp_probe_cmd_in (pkt, elapp)
316register aarp_pkt_t *pkt;
317at_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 ****************************************************************************/
388int 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
420int 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 ****************************************************************************/
534StaticProc 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
576StaticProc int aarp_send_req (amt_ptr)
577register 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 ****************************************************************************/
621StaticProc 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
666StaticProc aarp_amt_t *aarp_lru_entry(at)
667register 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
688StaticProc int aarp_glean_info(pkt, elapp)
689register aarp_pkt_t *pkt;
690at_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
731StaticProc int aarp_delete_amt_info(amt_ptr)
732register 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
759int 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
784StaticProc 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
802StaticProc int aarp_sched_req(amt_ptr)
803 register aarp_amt_t *amt_ptr;
804{
0b4e3aa0 805 int s, i;
1c79356b
A
806 boolean_t funnel_state;
807
808 funnel_state = thread_funnel_set(network_flock, TRUE);
809
0b4e3aa0
A
810 /*
811 * make sure pointer still valid in case interface removed
812 * while trying to acquire the funnel. make sure it points
813 * into one of the amt arrays.
814 */
815 for (i = 0; i < IF_TOTAL_MAX; i++) {
816 if (aarp_table[i] == NULL || amt_ptr < aarp_table[i] || amt_ptr >= (aarp_table[i] + 1))
817 continue; /* no match - try next entry */
818
819 /*
820 * found match - pointer is valid
821 */
822 ATDISABLE(s, arpinp_lock);
823 if (amt_ptr->tmo == 0) {
824 ATENABLE(s, arpinp_lock);
825 (void) thread_funnel_set(network_flock, FALSE);
826 return(0);
827 }
828 if (amt_ptr->no_of_retries < AARP_MAX_REQ_RETRIES) {
829 ATENABLE(s, arpinp_lock);
830 if (aarp_send_req(amt_ptr) == 0) {
831 (void) thread_funnel_set(network_flock, FALSE);
832 return(0);
833 }
834 ATDISABLE(s, arpinp_lock);
835 }
836 ATENABLE(s, arpinp_lock);
837 aarp_delete_amt_info(amt_ptr);
838 break;
839 }
1c79356b
A
840 (void) thread_funnel_set(network_flock, FALSE);
841
842 return(0);
843}
844
845
846
847/****************************************************************************
848 * aarp_get_rand_node()
849 *
850 ****************************************************************************/
851StaticProc int aarp_get_rand_node(elapp)
852at_ifaddr_t *elapp;
853{
854 register u_char node;
855
856 /*
857 * generate a starting node number in the range 1 thru 0xfd.
858 * we use this as the starting probe point for a given net
859 * To generate a different node number each time we call
860 * aarp_get_next_node
861 */
862 node = ((u_char)(random() & 0xff)) % 0xfd + 2;
863
864 elapp->initial_addr.s_node = node;
865 return(0);
866}
867
868
869
870StaticProc int aarp_get_next_node(elapp)
871at_ifaddr_t *elapp;
872{
873 register u_char node = elapp->initial_addr.s_node;
874
875 /*
876 * return the next node number in the range 1 thru 0xfd.
877 */
878 node = (node == 0xfd) ? (1) : (node+1);
879
880 elapp->initial_addr.s_node = node;
881 return(0);
882}
883
884
885
886
887
888/****************************************************************************
889 * aarp_get_rand_net()
890 *
891 ****************************************************************************/
892StaticProc int aarp_get_rand_net(elapp)
893register at_ifaddr_t *elapp;
894{
895 register at_net_al last_net, new_net;
896
897 if (elapp->ifThisCableStart) {
898 last_net = elapp->initial_addr.s_net;
899 /*
900 * the range of network numbers valid for this
901 * cable is known. Try to choose a number from
902 * this range only.
903 */
904 new_net= ((at_net_al)random() & 0xffff);
905 /* two-byte random number generated... now fit it in
906 * the prescribed range
907 */
908 new_net = new_net % (unsigned) (elapp->ifThisCableEnd -
909 elapp->ifThisCableStart + 1)
910 + elapp->ifThisCableStart;
911
912 if (new_net == last_net) {
913 if (new_net == elapp->ifThisCableEnd)
914 new_net = elapp->ifThisCableStart;
915 else
916 new_net++;
917 }
918 elapp->initial_addr.s_net = new_net;
919 } else {
920 /* The range of valid network numbers for this cable
921 * is not known... choose a network number from
922 * startup range.
923 */
924 last_net = (elapp->initial_addr.s_net & 0x00ff);
925 new_net = (at_net_al)random() & 0x00ff;
926
927 if (new_net == last_net)
928 new_net++;
929 if (new_net == 0xff)
930 new_net = 0;
931 elapp->initial_addr.s_net = (DDP_STARTUP_LOW | new_net);
932 }
933 return(0);
934}
935
936
937int getAarpTableSize(elapId)
938 int elapId; /* elap_specifics array index (should be
939 * changed when we add a non-ethernet type
940 * of I/F to the mix. Unused for now.
941 */
942{
943 return(AMTSIZE);
944}
945
946int getPhysAddrSize(elapId)
947 int elapId; /* elap_specifics array index (should be
948 * changed when we add a non-ethernet type
949 * of I/F to the mix. Unused for now.
950 */
951{
952 return(ETHERNET_ADDR_LEN);
953}
954
955#define ENTRY_SIZE sizeof(struct atalk_addr) + sizeof(struct etalk_addr)
956
957snmpAarpEnt_t *getAarp(elapId)
958 int *elapId; /* I/F table to retrieve & table
959 size entries on return */
960
961/* gets aarp table for specified interface and builds
962 a table in SNMP expected format. Returns pointer to said
963 table and sets elapId to byte size of used portion of table
964*/
965{
966 int i, cnt=0;
967 aarp_amt_t *amtp;
968 static snmpAarpEnt_t snmp[AMTSIZE];
969 snmpAarpEnt_t *snmpp;
970
971
972 if (*elapId <0 || *elapId >= IF_TOTAL_MAX)
973 return NULL;
974
975
976 for (i=0, amtp = &(aarp_table[*elapId]->et_aarp_amt[0]), snmpp = snmp;
977 i < AMTSIZE; i++,amtp++) {
978
979 /* last_time will be 0 if entry was never used */
980 if (amtp->last_time) {
981 /* copy just network & mac address.
982 * For speed, we assume that the atalk_addr
983 * & etalk_addr positions in the aarp_amt_t struct
984 * has not changed and copy both at once
985 */
986 bcopy(&amtp->dest_at_addr, &snmpp->ap_ddpAddr, ENTRY_SIZE);
987 snmpp++;
988 cnt++;
989
990 }
991 }
992 *elapId = cnt;
993 return(snmp);
994}
995/*#endif *//* COMMENTED_OUT */
996