2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1996 Apple Computer, Inc.
25 * Created April 8, 1996 by Tuyen Nguyen
26 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
30 #include <sys/errno.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <machine/spl.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/filedesc.h>
38 #include <sys/fcntl.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
43 #include <kern/assert.h>
45 #include <netat/sysglue.h>
46 #include <netat/appletalk.h>
47 #include <netat/at_var.h>
48 #include <netat/routing_tables.h>
49 #include <netat/at_pcb.h>
50 #include <netat/aurp.h>
51 #include <netat/debug.h>
53 static int AURPgetzi(int, unsigned char *, short *, gbuf_t
*, int);
54 static void AURPsetzi(unsigned char, gbuf_t
*, short, short);
57 void AURPsndZReq(state
)
63 short *net
, nets_cnt
, net_sent
=0, entry_num
=0;
64 RT_entry
*entry
= RT_table
;
66 if (!state
->get_zi
|| (state
->rcv_state
== AURPSTATE_Unconnected
))
70 msize
= sizeof(aurp_hdr_t
);
71 if ((m
= (gbuf_t
*)gbuf_alloc(msize
+AURP_MaxPktSize
, PRI_MED
)) == 0) {
72 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPsndZReq: node=%d, out of mblk\n",
78 /* construct the ZI request packet */
79 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
80 hdrp
->connection_id
= state
->rcv_connection_id
;
81 hdrp
->sequence_number
= 0;
82 hdrp
->command_code
= AURPCMD_ZReq
;
84 *(short *)(hdrp
+1) = AURPSUBCODE_ZoneInfo1
;
85 gbuf_winc(m
,sizeof(short));
87 net
= (short *)gbuf_wptr(m
);
90 while (entry_num
< RT_maxentry
) {
92 * scan the router table, and build the ZI request packet
93 * with the right entries, i.e.,
94 * - entry in use and not of the net_port
95 * - with no zones and in an active state
96 * - talking to the right router
98 if ( (entry
->NetPort
== net_port
) && entry
->NetStop
&&
99 ((entry
->EntryState
& 0x0F) >= RTE_STATE_SUSPECT
) &&
100 (!RT_ALL_ZONES_KNOWN(entry
)) ) {
101 *net
++ = (entry
->NetStart
) ? entry
->NetStart
: entry
->NetStop
;
105 if (nets_cnt
>= 640) {
106 /* query only 640 networks per packet */
107 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndZReq: node=%d\n",
109 gbuf_winc(m
,(nets_cnt
* sizeof(short)));
110 AURPsend(m
, AUD_AURP
, state
->rem_node
);
120 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndZReq: node=%d\n",
122 gbuf_winc(m
,(nets_cnt
* sizeof(short)));
123 AURPsend(m
, AUD_AURP
, state
->rem_node
);
133 void AURPsndZRsp(state
, dat_m
, flag
)
139 int msize
, next_entry
= 0;
143 if ((state
->snd_state
== AURPSTATE_Unconnected
) || (dat_m
== 0))
145 msize
= sizeof(aurp_hdr_t
);
148 if ((m
= (gbuf_t
*)gbuf_alloc(msize
+AURP_MaxPktSize
, PRI_MED
)) == 0) {
149 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPsndZRsp: node=%d, out of mblk\n",
155 /* construct the ZI response packet */
156 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
157 hdrp
->connection_id
= state
->snd_connection_id
;
158 hdrp
->sequence_number
= 0;
159 hdrp
->command_code
= AURPCMD_ZRsp
;
162 /* get zone info of the local networks */
163 next_entry
= AURPgetzi(next_entry
, gbuf_wptr(m
), &len
, dat_m
, flag
);
166 /* send the packet */
167 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndZRsp: len=%d\n", len
));
168 AURPsend(m
, AUD_AURP
, state
->rem_node
);
170 } while (next_entry
);
176 void AURPsndGZN(state
, dat_m
)
185 if (state
->snd_state
== AURPSTATE_Unconnected
)
188 msize
= sizeof(aurp_hdr_t
);
189 if ((m
= (gbuf_t
*)gbuf_alloc(msize
+AURP_MaxPktSize
, PRI_MED
)) == 0) {
190 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPsndGZN: node=%d, out of mblk\n",
196 /* construct the GZN response packet */
197 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
198 hdrp
->connection_id
= state
->snd_connection_id
;
199 hdrp
->sequence_number
= 0;
200 hdrp
->command_code
= AURPCMD_ZRsp
;
202 *(short *)(gbuf_wptr(m
)) = AURPSUBCODE_GetZoneNets
;
203 gbuf_winc(m
,sizeof(short));
204 zname_len
= gbuf_len(dat_m
);
205 bcopy(gbuf_rptr(dat_m
), gbuf_wptr(m
), zname_len
);
206 gbuf_winc(m
,zname_len
);
207 *(short *)(gbuf_wptr(m
)) = -1; /* number of tuples - proto not supported */
208 gbuf_winc(m
,sizeof(short));
210 /* send the packet */
211 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndGZN: count=%d\n", -1));
212 AURPsend(m
, AUD_AURP
, state
->rem_node
);
216 void AURPsndGDZL(state
, dat_m
)
224 if (state
->snd_state
== AURPSTATE_Unconnected
)
227 msize
= sizeof(aurp_hdr_t
);
228 if ((m
= (gbuf_t
*)gbuf_alloc(msize
+AURP_MaxPktSize
, PRI_MED
)) == 0) {
229 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPsndGDZL: node=%d, out of mblk\n",
235 /* construct the GDZL response packet */
236 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
237 hdrp
->connection_id
= state
->snd_connection_id
;
238 hdrp
->sequence_number
= 0;
239 hdrp
->command_code
= AURPCMD_ZRsp
;
241 *(short *)(gbuf_wptr(m
)) = AURPSUBCODE_GetDomainZoneList
;
242 gbuf_winc(m
,sizeof(short));
243 *(short *)(gbuf_wptr(m
)) = -1; /* start index - proto not supported */
244 gbuf_winc(m
,sizeof(short));
246 /* send the packet */
247 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndGDZL: index=%d\n", -1));
248 AURPsend(m
, AUD_AURP
, state
->rem_node
);
252 void AURPrcvZReq(state
, m
)
257 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
259 /* make sure we're in a valid state to accept it */
260 if (state
->snd_state
== AURPSTATE_Unconnected
) {
261 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPrcvZReq: unexpected response\n"));
266 /* check for the correct connection id */
267 if (hdrp
->connection_id
!= state
->snd_connection_id
) {
268 dPrintf(D_M_AURP
, D_L_WARNING
,
269 ("AURPrcvZReq: invalid connection id, r=%d, m=%d\n",
270 hdrp
->connection_id
, state
->snd_connection_id
));
275 gbuf_rinc(m
,sizeof(*hdrp
));
276 sub_code
= *(short *)gbuf_rptr(m
);
277 gbuf_rinc(m
,sizeof(short));
279 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPrcvZReq: len=%ld\n", gbuf_len(m
)));
282 case AURPSUBCODE_ZoneInfo1
:
283 AURPsndZRsp(state
, m
, 0);
286 case AURPSUBCODE_GetZoneNets
:
287 AURPsndGZN(state
, m
);
290 case AURPSUBCODE_GetDomainZoneList
:
291 AURPsndGDZL(state
, m
);
299 void AURPrcvZRsp(state
, m
)
303 short sub_code
, tuples_cnt
;
304 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
306 /* make sure we're in a valid state to accept it */
307 if (state
->rcv_state
== AURPSTATE_Unconnected
) {
308 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPrcvZRsp: unexpected response\n"));
313 /* check for the correct connection id */
314 if (hdrp
->connection_id
!= state
->rcv_connection_id
) {
315 dPrintf(D_M_AURP
, D_L_WARNING
,
316 ("AURPrcvZRsp: invalid connection id, r=%d, m=%d\n",
317 hdrp
->connection_id
, state
->rcv_connection_id
));
322 gbuf_rinc(m
,sizeof(*hdrp
));
323 sub_code
= *(short *)gbuf_rptr(m
);
324 gbuf_rinc(m
,sizeof(short));
326 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPrcvZRsp: len=%ld\n", gbuf_len(m
)));
329 case AURPSUBCODE_ZoneInfo1
:
330 case AURPSUBCODE_ZoneInfo2
:
331 tuples_cnt
= *(short *)gbuf_rptr(m
);
332 gbuf_rinc(m
,sizeof(short));
333 AURPsetzi(state
->rem_node
, m
, sub_code
, tuples_cnt
);
336 case AURPSUBCODE_GetZoneNets
:
339 case AURPSUBCODE_GetDomainZoneList
:
348 AURPgetzi(next_entry
, buf
, len
, dat_m
, flag
)
355 static int i_sav
=ZT_BYTES
-1, j_sav
=0, idx_sav
=-1;
356 unsigned char ev
, zname_len
, *zmap
, *zname_base
, *zname_sav
, *tuples_ptr
;
357 unsigned short net_num
, *net
, zname_offset
;
358 short *sub_codep
, *tuples_cntp
, tuples_cnt
, dat_len
;
359 int i
, j
, idx
, nets_cnt
;
363 * XXX CHS June-98: The compiler complains that some of these
364 * XXX variables may be used before they're set. I don't think
365 * XXX that's actually the case, but to check, I'll assign them
366 * XXX with some test value, and add asserts to check them at
367 * XXX run-time. The asserts won't be compiled in for production.
369 zname_sav
= tuples_ptr
= (unsigned char *) 0xdeadbeef; /* XXX */
370 net
= (unsigned short *) 0xdeadbeef; /* XXX */
371 net_num
= 0xdead; /* XXX */
372 nets_cnt
= 0xfeedface; /* XXX */
374 sub_codep
= (short *)buf
;
375 buf
+= sizeof(short);
376 tuples_cntp
= (short *)buf
;
377 buf
+= sizeof(short);
378 *len
= sizeof(short) + sizeof(short);
379 zname_base
= buf
+ sizeof(short);
382 /* set the subcode in the ZI response packet */
383 *sub_codep
= next_entry
? AURPSUBCODE_ZoneInfo2
: AURPSUBCODE_ZoneInfo1
;
386 case 0: /* zone info in response to ZI request */
387 net
= (unsigned short *)gbuf_rptr(dat_m
);
388 nets_cnt
= (gbuf_len(dat_m
))/2;
390 case 1: /* zone info in response to Ack of RI response */
391 tuples_ptr
= gbuf_rptr(dat_m
);
392 nets_cnt
= (gbuf_len(dat_m
))/3;
395 case 2: /* zone info in response to Ack of RI update */
396 tuples_ptr
= gbuf_rptr(dat_m
);
397 nets_cnt
= (gbuf_len(dat_m
))/4;
403 * for each network, find all the zones that it belongs to
405 assert(nets_cnt
!= 0xfeedface); /* XXX */
406 for (tuples_cnt
=0; next_entry
< nets_cnt
; next_entry
++) {
409 assert(net
!= 0xdeadbeef); /* XXX */
410 net_num
= net
[next_entry
];
413 assert(tuples_ptr
!= 0xdeadbeef); /* XXX */
414 net_num
= *(unsigned short *)tuples_ptr
;
417 if (tuples_ptr
[-1] & 0x80) {
424 if (gbuf_len(dat_m
) <= 0) {
425 next_entry
= nets_cnt
;
428 assert(tuples_ptr
!= 0xdeadbeef); /* XXX */
430 net_num
= *(unsigned short *)tuples_ptr
;
433 if (tuples_ptr
[-1] & 0x80) {
437 if (ev
!= AURPEV_NetAdded
)
443 * find the RT entry associated with the network
445 assert(net_num
!= 0xdead); /* XXX */
446 if ((entry
= rt_blookup(net_num
)) == 0) {
447 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPgetzi: invalid net, %d\n",
451 if ( ((entry
->EntryState
& 0x0F) < RTE_STATE_SUSPECT
) ||
452 !RT_ALL_ZONES_KNOWN(entry
) ||
453 (entry
->AURPFlag
& AURP_NetHiden
) ) {
454 dPrintf(D_M_AURP_LOW
, D_L_INFO
, ("AURPgetzi: zombie net, net=%d\n",
459 if (entry
->NetStart
== 0) {
460 if ((idx
= zt_ent_zindex(entry
->ZoneBitMap
)) == 0)
462 idx
--; /* index in the zone table */
463 zname_len
= ZT_table
[idx
].Zone
.len
;
465 assert(net_num
!= 0xdead); /* XXX */
466 *(unsigned short *)buf
= net_num
;
467 buf
+= sizeof(short);
468 if (idx
== idx_sav
) {
469 /* use the optimized format */
470 assert(zname_sav
!= 0xdeadbeef); /* XXX */
471 zname_offset
= zname_sav
- zname_base
;
472 *(unsigned short *)buf
= (0x8000 | zname_offset
);
473 buf
+= sizeof(short);
476 /* use the long format */
479 bcopy(ZT_table
[idx
].Zone
.str
, buf
, zname_len
);
481 dat_len
+= (3 + zname_len
);
488 zmap
= entry
->ZoneBitMap
;
489 for (i
=i_sav
; i
>=0; i
--) {
493 for (j
=j_sav
; j
< 8; j
++) {
494 if (!((zmap
[i
] << j
) & 0x80))
497 idx
= i
*8 + j
; /* index in the zone table */
498 zname_len
= ZT_table
[idx
].Zone
.len
;
500 if ((dat_len
+3+zname_len
) > AURP_MaxPktSize
) {
506 assert(net_num
!= 0xdead); /* XXX */
507 *(unsigned short *)buf
= net_num
;
508 buf
+= sizeof(short);
509 if (idx
== idx_sav
) {
510 /* use the optimized format */
511 assert(zname_sav
!= 0xdeadbeef);/*XXX*/
512 zname_offset
= zname_sav
- zname_base
;
513 *(unsigned short *)buf
= (0x8000 | zname_offset
);
514 buf
+= sizeof(short);
517 /* use the long format */
520 bcopy(ZT_table
[idx
].Zone
.str
, buf
, zname_len
);
522 dat_len
+= (3 + zname_len
);
530 if ((dat_len
+3+32) > AURP_MaxPktSize
) {
540 if (next_entry
== nets_cnt
)
543 /* set the subcode in the ZI response packet */
545 *sub_codep
= AURPSUBCODE_ZoneInfo2
;
547 /* set the tuples count in the ZI response packet */
548 *tuples_cntp
= tuples_cnt
;
556 AURPsetzi(node
, m
, sub_code
, tuples_cnt
)
563 unsigned short net_num
, zname_offset
;
564 unsigned char *buf
= gbuf_rptr(m
), *zname_base
;
568 /* compute the base of the zone names of the optimized tuples */
569 zname_base
= buf
+ sizeof(short);
571 /* process all tuples */
572 while (tuples_cnt
-- > 0) {
573 net_num
= *(unsigned short *)buf
;
574 buf
+= sizeof(short);
576 /* optimized-format tuple */
577 zname_offset
= (*(unsigned short *)buf
) & 0x7fff;
578 buf
+= sizeof(short);
579 zname
= (at_nvestr_t
*)(zname_base
+ zname_offset
);
581 dPrintf(D_M_AURP_LOW
, D_L_INFO
,
582 ("AURPsetzi: optimized fmt, net=%d. zlen=%d, zoffset=%d\n ",
583 net_num
, zname
->len
, zname_offset
));
585 /* long-format tuple */
586 zname
= (at_nvestr_t
*)buf
;
588 dPrintf(D_M_AURP_LOW
, D_L_INFO
,
589 ("AURPsetzi: long fmt, net=%d, zlen=%d\n ",
590 net_num
, zname
->len
));
594 * find the RT entry associated with the specified network
596 if ((entry
= rt_blookup(net_num
)) == 0) {
597 dPrintf(D_M_AURP
, D_L_WARNING
,
598 ("AURPsetzi: invalid net, net=%d\n", net_num
));
599 } else { /* entry found */
600 if (entry
->EntryState
>= RTE_STATE_SUSPECT
) {
601 if ((rc
= zt_add_zonename(zname
)) == ZT_MAXEDOUT
) {
602 dPrintf(D_M_AURP
, D_L_WARNING
,
603 ("AURPsetzi: ZT_table full\n"));
605 zt_set_zmap(rc
, entry
->ZoneBitMap
);
606 RT_SET_ZONE_KNOWN(entry
);