2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1996 Apple Computer, Inc.
31 * Created April 8, 1996 by Tuyen Nguyen
32 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
39 #include <sys/errno.h>
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <machine/spl.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/filedesc.h>
47 #include <sys/fcntl.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
52 #include <kern/assert.h>
54 #include <netat/sysglue.h>
55 #include <netat/appletalk.h>
56 #include <netat/at_pcb.h>
57 #include <netat/at_var.h>
58 #include <netat/routing_tables.h>
59 #include <netat/aurp.h>
60 #include <netat/debug.h>
62 static int AURPgetzi(int, unsigned char *, short *, gbuf_t
*, int);
63 static void AURPsetzi(unsigned char, gbuf_t
*, short, short);
66 void AURPsndZReq(state
)
72 short *net
, nets_cnt
, net_sent
=0, entry_num
=0;
73 RT_entry
*entry
= RT_table
;
75 if (!state
->get_zi
|| (state
->rcv_state
== AURPSTATE_Unconnected
))
79 msize
= sizeof(aurp_hdr_t
);
80 if ((m
= (gbuf_t
*)gbuf_alloc(msize
+AURP_MaxPktSize
, PRI_MED
)) == 0) {
81 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPsndZReq: node=%d, out of mblk\n",
87 /* construct the ZI request packet */
88 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
89 hdrp
->connection_id
= state
->rcv_connection_id
;
90 hdrp
->sequence_number
= 0;
91 hdrp
->command_code
= AURPCMD_ZReq
;
93 *(short *)(hdrp
+1) = AURPSUBCODE_ZoneInfo1
;
94 gbuf_winc(m
,sizeof(short));
96 net
= (short *)gbuf_wptr(m
);
99 while (entry_num
< RT_maxentry
) {
101 * scan the router table, and build the ZI request packet
102 * with the right entries, i.e.,
103 * - entry in use and not of the net_port
104 * - with no zones and in an active state
105 * - talking to the right router
107 if ( (entry
->NetPort
== net_port
) && entry
->NetStop
&&
108 ((entry
->EntryState
& 0x0F) >= RTE_STATE_SUSPECT
) &&
109 (!RT_ALL_ZONES_KNOWN(entry
)) ) {
110 *net
++ = (entry
->NetStart
) ? entry
->NetStart
: entry
->NetStop
;
114 if (nets_cnt
>= 640) {
115 /* query only 640 networks per packet */
116 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndZReq: node=%d\n",
118 gbuf_winc(m
,(nets_cnt
* sizeof(short)));
119 AURPsend(m
, AUD_AURP
, state
->rem_node
);
129 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndZReq: node=%d\n",
131 gbuf_winc(m
,(nets_cnt
* sizeof(short)));
132 AURPsend(m
, AUD_AURP
, state
->rem_node
);
142 void AURPsndZRsp(state
, dat_m
, flag
)
148 int msize
, next_entry
= 0;
152 if ((state
->snd_state
== AURPSTATE_Unconnected
) || (dat_m
== 0))
154 msize
= sizeof(aurp_hdr_t
);
157 if ((m
= (gbuf_t
*)gbuf_alloc(msize
+AURP_MaxPktSize
, PRI_MED
)) == 0) {
158 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPsndZRsp: node=%d, out of mblk\n",
164 /* construct the ZI response packet */
165 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
166 hdrp
->connection_id
= state
->snd_connection_id
;
167 hdrp
->sequence_number
= 0;
168 hdrp
->command_code
= AURPCMD_ZRsp
;
171 /* get zone info of the local networks */
172 next_entry
= AURPgetzi(next_entry
, gbuf_wptr(m
), &len
, dat_m
, flag
);
175 /* send the packet */
176 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndZRsp: len=%d\n", len
));
177 AURPsend(m
, AUD_AURP
, state
->rem_node
);
179 } while (next_entry
);
185 void AURPsndGZN(state
, dat_m
)
194 if (state
->snd_state
== AURPSTATE_Unconnected
)
197 msize
= sizeof(aurp_hdr_t
);
198 if ((m
= (gbuf_t
*)gbuf_alloc(msize
+AURP_MaxPktSize
, PRI_MED
)) == 0) {
199 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPsndGZN: node=%d, out of mblk\n",
205 /* construct the GZN response packet */
206 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
207 hdrp
->connection_id
= state
->snd_connection_id
;
208 hdrp
->sequence_number
= 0;
209 hdrp
->command_code
= AURPCMD_ZRsp
;
211 *(short *)(gbuf_wptr(m
)) = AURPSUBCODE_GetZoneNets
;
212 gbuf_winc(m
,sizeof(short));
213 zname_len
= gbuf_len(dat_m
);
214 bcopy(gbuf_rptr(dat_m
), gbuf_wptr(m
), zname_len
);
215 gbuf_winc(m
,zname_len
);
216 *(short *)(gbuf_wptr(m
)) = -1; /* number of tuples - proto not supported */
217 gbuf_winc(m
,sizeof(short));
219 /* send the packet */
220 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndGZN: count=%d\n", -1));
221 AURPsend(m
, AUD_AURP
, state
->rem_node
);
225 void AURPsndGDZL(state
, dat_m
)
233 if (state
->snd_state
== AURPSTATE_Unconnected
)
236 msize
= sizeof(aurp_hdr_t
);
237 if ((m
= (gbuf_t
*)gbuf_alloc(msize
+AURP_MaxPktSize
, PRI_MED
)) == 0) {
238 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPsndGDZL: node=%d, out of mblk\n",
244 /* construct the GDZL response packet */
245 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
246 hdrp
->connection_id
= state
->snd_connection_id
;
247 hdrp
->sequence_number
= 0;
248 hdrp
->command_code
= AURPCMD_ZRsp
;
250 *(short *)(gbuf_wptr(m
)) = AURPSUBCODE_GetDomainZoneList
;
251 gbuf_winc(m
,sizeof(short));
252 *(short *)(gbuf_wptr(m
)) = -1; /* start index - proto not supported */
253 gbuf_winc(m
,sizeof(short));
255 /* send the packet */
256 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndGDZL: index=%d\n", -1));
257 AURPsend(m
, AUD_AURP
, state
->rem_node
);
261 void AURPrcvZReq(state
, m
)
266 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
268 /* make sure we're in a valid state to accept it */
269 if (state
->snd_state
== AURPSTATE_Unconnected
) {
270 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPrcvZReq: unexpected response\n"));
275 /* check for the correct connection id */
276 if (hdrp
->connection_id
!= state
->snd_connection_id
) {
277 dPrintf(D_M_AURP
, D_L_WARNING
,
278 ("AURPrcvZReq: invalid connection id, r=%d, m=%d\n",
279 hdrp
->connection_id
, state
->snd_connection_id
));
284 gbuf_rinc(m
,sizeof(*hdrp
));
285 sub_code
= *(short *)gbuf_rptr(m
);
286 gbuf_rinc(m
,sizeof(short));
288 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPrcvZReq: len=%ld\n", gbuf_len(m
)));
291 case AURPSUBCODE_ZoneInfo1
:
292 AURPsndZRsp(state
, m
, 0);
295 case AURPSUBCODE_GetZoneNets
:
296 AURPsndGZN(state
, m
);
299 case AURPSUBCODE_GetDomainZoneList
:
300 AURPsndGDZL(state
, m
);
308 void AURPrcvZRsp(state
, m
)
312 short sub_code
, tuples_cnt
;
313 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
315 /* make sure we're in a valid state to accept it */
316 if (state
->rcv_state
== AURPSTATE_Unconnected
) {
317 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPrcvZRsp: unexpected response\n"));
322 /* check for the correct connection id */
323 if (hdrp
->connection_id
!= state
->rcv_connection_id
) {
324 dPrintf(D_M_AURP
, D_L_WARNING
,
325 ("AURPrcvZRsp: invalid connection id, r=%d, m=%d\n",
326 hdrp
->connection_id
, state
->rcv_connection_id
));
331 gbuf_rinc(m
,sizeof(*hdrp
));
332 sub_code
= *(short *)gbuf_rptr(m
);
333 gbuf_rinc(m
,sizeof(short));
335 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPrcvZRsp: len=%ld\n", gbuf_len(m
)));
338 case AURPSUBCODE_ZoneInfo1
:
339 case AURPSUBCODE_ZoneInfo2
:
340 tuples_cnt
= *(short *)gbuf_rptr(m
);
341 gbuf_rinc(m
,sizeof(short));
342 AURPsetzi(state
->rem_node
, m
, sub_code
, tuples_cnt
);
345 case AURPSUBCODE_GetZoneNets
:
348 case AURPSUBCODE_GetDomainZoneList
:
357 AURPgetzi(next_entry
, buf
, len
, dat_m
, flag
)
364 static int i_sav
=ZT_BYTES
-1, j_sav
=0, idx_sav
=-1;
365 unsigned char ev
, zname_len
, *zmap
, *zname_base
, *zname_sav
, *tuples_ptr
;
366 unsigned short net_num
, *net
, zname_offset
;
367 short *sub_codep
, *tuples_cntp
, tuples_cnt
, dat_len
;
368 int i
, j
, idx
, nets_cnt
;
372 * XXX CHS June-98: The compiler complains that some of these
373 * XXX variables may be used before they're set. I don't think
374 * XXX that's actually the case, but to check, I'll assign them
375 * XXX with some test value, and add asserts to check them at
376 * XXX run-time. The asserts won't be compiled in for production.
378 zname_sav
= tuples_ptr
= (unsigned char *) 0xdeadbeef; /* XXX */
379 net
= (unsigned short *) 0xdeadbeef; /* XXX */
380 net_num
= 0xdead; /* XXX */
381 nets_cnt
= 0xfeedface; /* XXX */
383 sub_codep
= (short *)buf
;
384 buf
+= sizeof(short);
385 tuples_cntp
= (short *)buf
;
386 buf
+= sizeof(short);
387 *len
= sizeof(short) + sizeof(short);
388 zname_base
= buf
+ sizeof(short);
391 /* set the subcode in the ZI response packet */
392 *sub_codep
= next_entry
? AURPSUBCODE_ZoneInfo2
: AURPSUBCODE_ZoneInfo1
;
395 case 0: /* zone info in response to ZI request */
396 net
= (unsigned short *)gbuf_rptr(dat_m
);
397 nets_cnt
= (gbuf_len(dat_m
))/2;
399 case 1: /* zone info in response to Ack of RI response */
400 tuples_ptr
= gbuf_rptr(dat_m
);
401 nets_cnt
= (gbuf_len(dat_m
))/3;
404 case 2: /* zone info in response to Ack of RI update */
405 tuples_ptr
= gbuf_rptr(dat_m
);
406 nets_cnt
= (gbuf_len(dat_m
))/4;
412 * for each network, find all the zones that it belongs to
414 assert(nets_cnt
!= 0xfeedface); /* XXX */
415 for (tuples_cnt
=0; next_entry
< nets_cnt
; next_entry
++) {
418 assert(net
!= 0xdeadbeef); /* XXX */
419 net_num
= net
[next_entry
];
422 assert(tuples_ptr
!= 0xdeadbeef); /* XXX */
423 net_num
= *(unsigned short *)tuples_ptr
;
426 if (tuples_ptr
[-1] & 0x80) {
433 if (gbuf_len(dat_m
) <= 0) {
434 next_entry
= nets_cnt
;
437 assert(tuples_ptr
!= 0xdeadbeef); /* XXX */
439 net_num
= *(unsigned short *)tuples_ptr
;
442 if (tuples_ptr
[-1] & 0x80) {
446 if (ev
!= AURPEV_NetAdded
)
452 * find the RT entry associated with the network
454 assert(net_num
!= 0xdead); /* XXX */
455 if ((entry
= rt_blookup(net_num
)) == 0) {
456 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPgetzi: invalid net, %d\n",
460 if ( ((entry
->EntryState
& 0x0F) < RTE_STATE_SUSPECT
) ||
461 !RT_ALL_ZONES_KNOWN(entry
) ||
462 (entry
->AURPFlag
& AURP_NetHiden
) ) {
463 dPrintf(D_M_AURP_LOW
, D_L_INFO
, ("AURPgetzi: zombie net, net=%d\n",
468 if (entry
->NetStart
== 0) {
469 if ((idx
= zt_ent_zindex(entry
->ZoneBitMap
)) == 0)
471 idx
--; /* index in the zone table */
472 zname_len
= ZT_table
[idx
].Zone
.len
;
474 assert(net_num
!= 0xdead); /* XXX */
475 *(unsigned short *)buf
= net_num
;
476 buf
+= sizeof(short);
477 if (idx
== idx_sav
) {
478 /* use the optimized format */
479 assert(zname_sav
!= 0xdeadbeef); /* XXX */
480 zname_offset
= zname_sav
- zname_base
;
481 *(unsigned short *)buf
= (0x8000 | zname_offset
);
482 buf
+= sizeof(short);
485 /* use the long format */
488 bcopy(ZT_table
[idx
].Zone
.str
, buf
, zname_len
);
490 dat_len
+= (3 + zname_len
);
497 zmap
= entry
->ZoneBitMap
;
498 for (i
=i_sav
; i
>=0; i
--) {
502 for (j
=j_sav
; j
< 8; j
++) {
503 if (!((zmap
[i
] << j
) & 0x80))
506 idx
= i
*8 + j
; /* index in the zone table */
507 zname_len
= ZT_table
[idx
].Zone
.len
;
509 if ((dat_len
+3+zname_len
) > AURP_MaxPktSize
) {
515 assert(net_num
!= 0xdead); /* XXX */
516 *(unsigned short *)buf
= net_num
;
517 buf
+= sizeof(short);
518 if (idx
== idx_sav
) {
519 /* use the optimized format */
520 assert(zname_sav
!= 0xdeadbeef);/*XXX*/
521 zname_offset
= zname_sav
- zname_base
;
522 *(unsigned short *)buf
= (0x8000 | zname_offset
);
523 buf
+= sizeof(short);
526 /* use the long format */
529 bcopy(ZT_table
[idx
].Zone
.str
, buf
, zname_len
);
531 dat_len
+= (3 + zname_len
);
539 if ((dat_len
+3+32) > AURP_MaxPktSize
) {
549 if (next_entry
== nets_cnt
)
552 /* set the subcode in the ZI response packet */
554 *sub_codep
= AURPSUBCODE_ZoneInfo2
;
556 /* set the tuples count in the ZI response packet */
557 *tuples_cntp
= tuples_cnt
;
565 AURPsetzi(node
, m
, sub_code
, tuples_cnt
)
572 unsigned short net_num
, zname_offset
;
573 unsigned char *buf
= gbuf_rptr(m
), *zname_base
;
577 /* compute the base of the zone names of the optimized tuples */
578 zname_base
= buf
+ sizeof(short);
580 /* process all tuples */
581 while (tuples_cnt
-- > 0) {
582 net_num
= *(unsigned short *)buf
;
583 buf
+= sizeof(short);
585 /* optimized-format tuple */
586 zname_offset
= (*(unsigned short *)buf
) & 0x7fff;
587 buf
+= sizeof(short);
588 zname
= (at_nvestr_t
*)(zname_base
+ zname_offset
);
590 dPrintf(D_M_AURP_LOW
, D_L_INFO
,
591 ("AURPsetzi: optimized fmt, net=%d. zlen=%d, zoffset=%d\n ",
592 net_num
, zname
->len
, zname_offset
));
594 /* long-format tuple */
595 zname
= (at_nvestr_t
*)buf
;
597 dPrintf(D_M_AURP_LOW
, D_L_INFO
,
598 ("AURPsetzi: long fmt, net=%d, zlen=%d\n ",
599 net_num
, zname
->len
));
603 * find the RT entry associated with the specified network
605 if ((entry
= rt_blookup(net_num
)) == 0) {
606 dPrintf(D_M_AURP
, D_L_WARNING
,
607 ("AURPsetzi: invalid net, net=%d\n", net_num
));
608 } else { /* entry found */
609 if (entry
->EntryState
>= RTE_STATE_SUSPECT
) {
610 if ((rc
= zt_add_zonename(zname
)) == ZT_MAXEDOUT
) {
611 dPrintf(D_M_AURP
, D_L_WARNING
,
612 ("AURPsetzi: ZT_table full\n"));
614 zt_set_zmap(rc
, entry
->ZoneBitMap
);
615 RT_SET_ZONE_KNOWN(entry
);
624 #endif /* AURP_SUPPORT */