2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1996 Apple Computer, Inc.
33 * Created April 8, 1996 by Tuyen Nguyen
34 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
38 #include <sys/errno.h>
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <machine/spl.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/filedesc.h>
46 #include <sys/fcntl.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
51 #include <kern/assert.h>
53 #include <netat/sysglue.h>
54 #include <netat/appletalk.h>
55 #include <netat/at_var.h>
56 #include <netat/routing_tables.h>
57 #include <netat/at_pcb.h>
58 #include <netat/aurp.h>
59 #include <netat/debug.h>
61 static int AURPgetzi(int, unsigned char *, short *, gbuf_t
*, int);
62 static void AURPsetzi(unsigned char, gbuf_t
*, short, short);
65 void AURPsndZReq(state
)
71 short *net
, nets_cnt
, net_sent
=0, entry_num
=0;
72 RT_entry
*entry
= RT_table
;
74 if (!state
->get_zi
|| (state
->rcv_state
== AURPSTATE_Unconnected
))
78 msize
= sizeof(aurp_hdr_t
);
79 if ((m
= (gbuf_t
*)gbuf_alloc(msize
+AURP_MaxPktSize
, PRI_MED
)) == 0) {
80 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPsndZReq: node=%d, out of mblk\n",
86 /* construct the ZI request packet */
87 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
88 hdrp
->connection_id
= state
->rcv_connection_id
;
89 hdrp
->sequence_number
= 0;
90 hdrp
->command_code
= AURPCMD_ZReq
;
92 *(short *)(hdrp
+1) = AURPSUBCODE_ZoneInfo1
;
93 gbuf_winc(m
,sizeof(short));
95 net
= (short *)gbuf_wptr(m
);
98 while (entry_num
< RT_maxentry
) {
100 * scan the router table, and build the ZI request packet
101 * with the right entries, i.e.,
102 * - entry in use and not of the net_port
103 * - with no zones and in an active state
104 * - talking to the right router
106 if ( (entry
->NetPort
== net_port
) && entry
->NetStop
&&
107 ((entry
->EntryState
& 0x0F) >= RTE_STATE_SUSPECT
) &&
108 (!RT_ALL_ZONES_KNOWN(entry
)) ) {
109 *net
++ = (entry
->NetStart
) ? entry
->NetStart
: entry
->NetStop
;
113 if (nets_cnt
>= 640) {
114 /* query only 640 networks per packet */
115 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndZReq: node=%d\n",
117 gbuf_winc(m
,(nets_cnt
* sizeof(short)));
118 AURPsend(m
, AUD_AURP
, state
->rem_node
);
128 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndZReq: node=%d\n",
130 gbuf_winc(m
,(nets_cnt
* sizeof(short)));
131 AURPsend(m
, AUD_AURP
, state
->rem_node
);
141 void AURPsndZRsp(state
, dat_m
, flag
)
147 int msize
, next_entry
= 0;
151 if ((state
->snd_state
== AURPSTATE_Unconnected
) || (dat_m
== 0))
153 msize
= sizeof(aurp_hdr_t
);
156 if ((m
= (gbuf_t
*)gbuf_alloc(msize
+AURP_MaxPktSize
, PRI_MED
)) == 0) {
157 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPsndZRsp: node=%d, out of mblk\n",
163 /* construct the ZI response packet */
164 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
165 hdrp
->connection_id
= state
->snd_connection_id
;
166 hdrp
->sequence_number
= 0;
167 hdrp
->command_code
= AURPCMD_ZRsp
;
170 /* get zone info of the local networks */
171 next_entry
= AURPgetzi(next_entry
, gbuf_wptr(m
), &len
, dat_m
, flag
);
174 /* send the packet */
175 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndZRsp: len=%d\n", len
));
176 AURPsend(m
, AUD_AURP
, state
->rem_node
);
178 } while (next_entry
);
184 void AURPsndGZN(state
, dat_m
)
193 if (state
->snd_state
== AURPSTATE_Unconnected
)
196 msize
= sizeof(aurp_hdr_t
);
197 if ((m
= (gbuf_t
*)gbuf_alloc(msize
+AURP_MaxPktSize
, PRI_MED
)) == 0) {
198 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPsndGZN: node=%d, out of mblk\n",
204 /* construct the GZN response packet */
205 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
206 hdrp
->connection_id
= state
->snd_connection_id
;
207 hdrp
->sequence_number
= 0;
208 hdrp
->command_code
= AURPCMD_ZRsp
;
210 *(short *)(gbuf_wptr(m
)) = AURPSUBCODE_GetZoneNets
;
211 gbuf_winc(m
,sizeof(short));
212 zname_len
= gbuf_len(dat_m
);
213 bcopy(gbuf_rptr(dat_m
), gbuf_wptr(m
), zname_len
);
214 gbuf_winc(m
,zname_len
);
215 *(short *)(gbuf_wptr(m
)) = -1; /* number of tuples - proto not supported */
216 gbuf_winc(m
,sizeof(short));
218 /* send the packet */
219 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndGZN: count=%d\n", -1));
220 AURPsend(m
, AUD_AURP
, state
->rem_node
);
224 void AURPsndGDZL(state
, dat_m
)
232 if (state
->snd_state
== AURPSTATE_Unconnected
)
235 msize
= sizeof(aurp_hdr_t
);
236 if ((m
= (gbuf_t
*)gbuf_alloc(msize
+AURP_MaxPktSize
, PRI_MED
)) == 0) {
237 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPsndGDZL: node=%d, out of mblk\n",
243 /* construct the GDZL response packet */
244 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
245 hdrp
->connection_id
= state
->snd_connection_id
;
246 hdrp
->sequence_number
= 0;
247 hdrp
->command_code
= AURPCMD_ZRsp
;
249 *(short *)(gbuf_wptr(m
)) = AURPSUBCODE_GetDomainZoneList
;
250 gbuf_winc(m
,sizeof(short));
251 *(short *)(gbuf_wptr(m
)) = -1; /* start index - proto not supported */
252 gbuf_winc(m
,sizeof(short));
254 /* send the packet */
255 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPsndGDZL: index=%d\n", -1));
256 AURPsend(m
, AUD_AURP
, state
->rem_node
);
260 void AURPrcvZReq(state
, m
)
265 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
267 /* make sure we're in a valid state to accept it */
268 if (state
->snd_state
== AURPSTATE_Unconnected
) {
269 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPrcvZReq: unexpected response\n"));
274 /* check for the correct connection id */
275 if (hdrp
->connection_id
!= state
->snd_connection_id
) {
276 dPrintf(D_M_AURP
, D_L_WARNING
,
277 ("AURPrcvZReq: invalid connection id, r=%d, m=%d\n",
278 hdrp
->connection_id
, state
->snd_connection_id
));
283 gbuf_rinc(m
,sizeof(*hdrp
));
284 sub_code
= *(short *)gbuf_rptr(m
);
285 gbuf_rinc(m
,sizeof(short));
287 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPrcvZReq: len=%ld\n", gbuf_len(m
)));
290 case AURPSUBCODE_ZoneInfo1
:
291 AURPsndZRsp(state
, m
, 0);
294 case AURPSUBCODE_GetZoneNets
:
295 AURPsndGZN(state
, m
);
298 case AURPSUBCODE_GetDomainZoneList
:
299 AURPsndGDZL(state
, m
);
307 void AURPrcvZRsp(state
, m
)
311 short sub_code
, tuples_cnt
;
312 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
314 /* make sure we're in a valid state to accept it */
315 if (state
->rcv_state
== AURPSTATE_Unconnected
) {
316 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPrcvZRsp: unexpected response\n"));
321 /* check for the correct connection id */
322 if (hdrp
->connection_id
!= state
->rcv_connection_id
) {
323 dPrintf(D_M_AURP
, D_L_WARNING
,
324 ("AURPrcvZRsp: invalid connection id, r=%d, m=%d\n",
325 hdrp
->connection_id
, state
->rcv_connection_id
));
330 gbuf_rinc(m
,sizeof(*hdrp
));
331 sub_code
= *(short *)gbuf_rptr(m
);
332 gbuf_rinc(m
,sizeof(short));
334 dPrintf(D_M_AURP
, D_L_INFO
, ("AURPrcvZRsp: len=%ld\n", gbuf_len(m
)));
337 case AURPSUBCODE_ZoneInfo1
:
338 case AURPSUBCODE_ZoneInfo2
:
339 tuples_cnt
= *(short *)gbuf_rptr(m
);
340 gbuf_rinc(m
,sizeof(short));
341 AURPsetzi(state
->rem_node
, m
, sub_code
, tuples_cnt
);
344 case AURPSUBCODE_GetZoneNets
:
347 case AURPSUBCODE_GetDomainZoneList
:
356 AURPgetzi(next_entry
, buf
, len
, dat_m
, flag
)
363 static int i_sav
=ZT_BYTES
-1, j_sav
=0, idx_sav
=-1;
364 unsigned char ev
, zname_len
, *zmap
, *zname_base
, *zname_sav
, *tuples_ptr
;
365 unsigned short net_num
, *net
, zname_offset
;
366 short *sub_codep
, *tuples_cntp
, tuples_cnt
, dat_len
;
367 int i
, j
, idx
, nets_cnt
;
371 * XXX CHS June-98: The compiler complains that some of these
372 * XXX variables may be used before they're set. I don't think
373 * XXX that's actually the case, but to check, I'll assign them
374 * XXX with some test value, and add asserts to check them at
375 * XXX run-time. The asserts won't be compiled in for production.
377 zname_sav
= tuples_ptr
= (unsigned char *) 0xdeadbeef; /* XXX */
378 net
= (unsigned short *) 0xdeadbeef; /* XXX */
379 net_num
= 0xdead; /* XXX */
380 nets_cnt
= 0xfeedface; /* XXX */
382 sub_codep
= (short *)buf
;
383 buf
+= sizeof(short);
384 tuples_cntp
= (short *)buf
;
385 buf
+= sizeof(short);
386 *len
= sizeof(short) + sizeof(short);
387 zname_base
= buf
+ sizeof(short);
390 /* set the subcode in the ZI response packet */
391 *sub_codep
= next_entry
? AURPSUBCODE_ZoneInfo2
: AURPSUBCODE_ZoneInfo1
;
394 case 0: /* zone info in response to ZI request */
395 net
= (unsigned short *)gbuf_rptr(dat_m
);
396 nets_cnt
= (gbuf_len(dat_m
))/2;
398 case 1: /* zone info in response to Ack of RI response */
399 tuples_ptr
= gbuf_rptr(dat_m
);
400 nets_cnt
= (gbuf_len(dat_m
))/3;
403 case 2: /* zone info in response to Ack of RI update */
404 tuples_ptr
= gbuf_rptr(dat_m
);
405 nets_cnt
= (gbuf_len(dat_m
))/4;
411 * for each network, find all the zones that it belongs to
413 assert(nets_cnt
!= 0xfeedface); /* XXX */
414 for (tuples_cnt
=0; next_entry
< nets_cnt
; next_entry
++) {
417 assert(net
!= 0xdeadbeef); /* XXX */
418 net_num
= net
[next_entry
];
421 assert(tuples_ptr
!= 0xdeadbeef); /* XXX */
422 net_num
= *(unsigned short *)tuples_ptr
;
425 if (tuples_ptr
[-1] & 0x80) {
432 if (gbuf_len(dat_m
) <= 0) {
433 next_entry
= nets_cnt
;
436 assert(tuples_ptr
!= 0xdeadbeef); /* XXX */
438 net_num
= *(unsigned short *)tuples_ptr
;
441 if (tuples_ptr
[-1] & 0x80) {
445 if (ev
!= AURPEV_NetAdded
)
451 * find the RT entry associated with the network
453 assert(net_num
!= 0xdead); /* XXX */
454 if ((entry
= rt_blookup(net_num
)) == 0) {
455 dPrintf(D_M_AURP
, D_L_WARNING
, ("AURPgetzi: invalid net, %d\n",
459 if ( ((entry
->EntryState
& 0x0F) < RTE_STATE_SUSPECT
) ||
460 !RT_ALL_ZONES_KNOWN(entry
) ||
461 (entry
->AURPFlag
& AURP_NetHiden
) ) {
462 dPrintf(D_M_AURP_LOW
, D_L_INFO
, ("AURPgetzi: zombie net, net=%d\n",
467 if (entry
->NetStart
== 0) {
468 if ((idx
= zt_ent_zindex(entry
->ZoneBitMap
)) == 0)
470 idx
--; /* index in the zone table */
471 zname_len
= ZT_table
[idx
].Zone
.len
;
473 assert(net_num
!= 0xdead); /* XXX */
474 *(unsigned short *)buf
= net_num
;
475 buf
+= sizeof(short);
476 if (idx
== idx_sav
) {
477 /* use the optimized format */
478 assert(zname_sav
!= 0xdeadbeef); /* XXX */
479 zname_offset
= zname_sav
- zname_base
;
480 *(unsigned short *)buf
= (0x8000 | zname_offset
);
481 buf
+= sizeof(short);
484 /* use the long format */
487 bcopy(ZT_table
[idx
].Zone
.str
, buf
, zname_len
);
489 dat_len
+= (3 + zname_len
);
496 zmap
= entry
->ZoneBitMap
;
497 for (i
=i_sav
; i
>=0; i
--) {
501 for (j
=j_sav
; j
< 8; j
++) {
502 if (!((zmap
[i
] << j
) & 0x80))
505 idx
= i
*8 + j
; /* index in the zone table */
506 zname_len
= ZT_table
[idx
].Zone
.len
;
508 if ((dat_len
+3+zname_len
) > AURP_MaxPktSize
) {
514 assert(net_num
!= 0xdead); /* XXX */
515 *(unsigned short *)buf
= net_num
;
516 buf
+= sizeof(short);
517 if (idx
== idx_sav
) {
518 /* use the optimized format */
519 assert(zname_sav
!= 0xdeadbeef);/*XXX*/
520 zname_offset
= zname_sav
- zname_base
;
521 *(unsigned short *)buf
= (0x8000 | zname_offset
);
522 buf
+= sizeof(short);
525 /* use the long format */
528 bcopy(ZT_table
[idx
].Zone
.str
, buf
, zname_len
);
530 dat_len
+= (3 + zname_len
);
538 if ((dat_len
+3+32) > AURP_MaxPktSize
) {
548 if (next_entry
== nets_cnt
)
551 /* set the subcode in the ZI response packet */
553 *sub_codep
= AURPSUBCODE_ZoneInfo2
;
555 /* set the tuples count in the ZI response packet */
556 *tuples_cntp
= tuples_cnt
;
564 AURPsetzi(node
, m
, sub_code
, tuples_cnt
)
571 unsigned short net_num
, zname_offset
;
572 unsigned char *buf
= gbuf_rptr(m
), *zname_base
;
576 /* compute the base of the zone names of the optimized tuples */
577 zname_base
= buf
+ sizeof(short);
579 /* process all tuples */
580 while (tuples_cnt
-- > 0) {
581 net_num
= *(unsigned short *)buf
;
582 buf
+= sizeof(short);
584 /* optimized-format tuple */
585 zname_offset
= (*(unsigned short *)buf
) & 0x7fff;
586 buf
+= sizeof(short);
587 zname
= (at_nvestr_t
*)(zname_base
+ zname_offset
);
589 dPrintf(D_M_AURP_LOW
, D_L_INFO
,
590 ("AURPsetzi: optimized fmt, net=%d. zlen=%d, zoffset=%d\n ",
591 net_num
, zname
->len
, zname_offset
));
593 /* long-format tuple */
594 zname
= (at_nvestr_t
*)buf
;
596 dPrintf(D_M_AURP_LOW
, D_L_INFO
,
597 ("AURPsetzi: long fmt, net=%d, zlen=%d\n ",
598 net_num
, zname
->len
));
602 * find the RT entry associated with the specified network
604 if ((entry
= rt_blookup(net_num
)) == 0) {
605 dPrintf(D_M_AURP
, D_L_WARNING
,
606 ("AURPsetzi: invalid net, net=%d\n", net_num
));
607 } else { /* entry found */
608 if (entry
->EntryState
>= RTE_STATE_SUSPECT
) {
609 if ((rc
= zt_add_zonename(zname
)) == ZT_MAXEDOUT
) {
610 dPrintf(D_M_AURP
, D_L_WARNING
,
611 ("AURPsetzi: ZT_table full\n"));
613 zt_set_zmap(rc
, entry
->ZoneBitMap
);
614 RT_SET_ZONE_KNOWN(entry
);