]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/aurp_open.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / bsd / netat / aurp_open.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
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
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
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.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * Copyright (c) 1996 Apple Computer, Inc.
32 *
33 * Created April 8, 1996 by Tuyen Nguyen
34 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
35 *
36 * File: open.c
37 */
38
39 #ifdef AURP_SUPPORT
40
41 #include <sys/errno.h>
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <machine/spl.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/proc.h>
48 #include <sys/filedesc.h>
49 #include <sys/fcntl.h>
50 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <net/if.h>
54
55 #include <netat/sysglue.h>
56 #include <netat/appletalk.h>
57 #include <netat/at_var.h>
58 #include <netat/routing_tables.h>
59 #include <netat/at_pcb.h>
60 #include <netat/aurp.h>
61 #include <netat/debug.h>
62
63
64 /* locked version of AURPsndOpenReq */
65 void AURPsndOpenReq_locked(state)
66 aurp_state_t *state;
67 {
68 atalk_lock();
69 AURPsndOpenReq(state);
70 atalk_unlock();
71 }
72
73 /* */
74 void AURPsndOpenReq(state)
75 aurp_state_t *state;
76 {
77 int msize;
78 gbuf_t *m;
79 aurp_hdr_t *hdrp;
80
81 if (aurp_gref == 0) {
82 return;
83 }
84 if (state->rcv_retry && (state->rcv_state != AURPSTATE_WaitingForOpenRsp)) {
85 return;
86 }
87
88 /* stop trying if the retry count exceeds the maximum value */
89 if (++state->rcv_retry > AURP_MaxRetry) {
90 dPrintf(D_M_AURP, D_L_WARNING,
91 ("AURPsndOpenReq: no response, node %u\n",
92 state->rem_node));
93 state->rcv_state = AURPSTATE_Unconnected;
94 state->rcv_tmo = 0;
95 state->rcv_retry = 0;
96 return;
97 }
98
99 msize = sizeof(aurp_hdr_t) + 3;
100 if ((m = (gbuf_t *)gbuf_alloc(msize, PRI_MED)) != 0) {
101 gbuf_wset(m,msize);
102
103 /* construct the open request packet */
104 hdrp = (aurp_hdr_t *)gbuf_rptr(m);
105 if (state->rcv_retry > 1)
106 hdrp->connection_id = state->rcv_connection_id;
107 else {
108 if (++rcv_connection_id == 0)
109 rcv_connection_id = 1;
110 hdrp->connection_id = rcv_connection_id;
111 }
112 hdrp->sequence_number = 0;
113 hdrp->command_code = AURPCMD_OpenReq;
114 hdrp->flags = (AURPFLG_NA | AURPFLG_ND | AURPFLG_NDC | AURPFLG_ZC);
115 *(short *)(hdrp+1) = AURP_Version;
116 ((char *)(hdrp+1))[2] = 0; /* option count */
117
118 /* update state info */
119 state->rcv_connection_id = hdrp->connection_id;
120 state->rcv_state = AURPSTATE_WaitingForOpenRsp;
121
122 /* send the packet */
123 dPrintf(D_M_AURP, D_L_TRACE,
124 ("AURPsndOpenReq: sending AURPCMD_OpenReq, node %u\n",
125 state->rem_node));
126 AURPsend(m, AUD_AURP, state->rem_node);
127 }
128
129 /* start the retry timer */
130 timeout(AURPsndOpenReq_locked, state, AURP_RetryInterval*HZ);
131 state->rcv_tmo = 1;
132 }
133
134 /* */
135 void AURPrcvOpenReq(state, m)
136 aurp_state_t *state;
137 gbuf_t *m;
138 {
139 short rc, version;
140 aurp_hdr_t *hdrp = (aurp_hdr_t *)gbuf_rptr(m);
141 unsigned short sui = hdrp->flags;
142
143 /* make sure we're in a valid state to accept it */
144 if ((update_tmo == 0) || ((state->snd_state != AURPSTATE_Unconnected) &&
145 (state->snd_state != AURPSTATE_Connected))) {
146 dPrintf(D_M_AURP, D_L_WARNING,
147 ("AURPrcvOpenReq: unexpected request, update_tmo=0x%x, snd_state=%u\n", (unsigned int) update_tmo, state->snd_state));
148 gbuf_freem(m);
149 return;
150 }
151
152 /* check for the correct version number */
153 version = *(short *)(hdrp+1);
154 if (version != AURP_Version) {
155 dPrintf(D_M_AURP, D_L_WARNING,
156 ("AURPrcvOpenReq: invalid version number %d, expected %d\n", version, AURP_Version));
157 rc = AURPERR_InvalidVersionNumber;
158 } else
159 rc = (short)AURP_UpdateRate;
160
161 /* construct the open response packet */
162 gbuf_wset(m,sizeof(aurp_hdr_t)+sizeof(short));
163 hdrp->command_code = AURPCMD_OpenRsp;
164 hdrp->flags = 0;
165 *(short *)(hdrp+1) = rc;
166 ((char *)(hdrp+1))[2] = 0; /* option count */
167
168 /*
169 * reset if we're in the Connected state and this is
170 * a completely new open request
171 */
172 if ((state->snd_state == AURPSTATE_Connected) &&
173 ((state->snd_connection_id != hdrp->connection_id) ||
174 (state->snd_sequence_number != AURP_FirstSeqNum))) {
175 extern void AURPsndTickle();
176 if (state->rcv_state == AURPSTATE_Connected) {
177 state->rcv_state = AURPSTATE_Unconnected;
178 untimeout(AURPsndTickle, state);
179 }
180 state->snd_state = AURPSTATE_Unconnected;
181 AURPcleanup(state);
182 AURPpurgeri(state->rem_node);
183 }
184
185 /* update state info */
186 if (state->snd_state == AURPSTATE_Unconnected) {
187 state->snd_state = AURPSTATE_Connected;
188 state->snd_sui = sui;
189 state->snd_connection_id = hdrp->connection_id;
190 state->snd_sequence_number = AURP_FirstSeqNum;
191 }
192
193 /* send the packet */
194 AURPsend(m, AUD_AURP, state->rem_node);
195
196 /* open connection for the data receiver side if not yet connected */
197 if (state->rcv_state == AURPSTATE_Unconnected) {
198 state->rcv_retry = 0;
199 state->tickle_retry = 0;
200 state->rcv_sequence_number = 0;
201 AURPsndOpenReq(state);
202 }
203 }
204
205 /* */
206 void AURPrcvOpenRsp(state, m)
207 aurp_state_t *state;
208 gbuf_t *m;
209 {
210 extern void AURPsndTickle();
211 short rc;
212 aurp_hdr_t *hdrp = (aurp_hdr_t *)gbuf_rptr(m);
213
214 /* make sure we're in a valid state to accept it */
215 if (state->rcv_state != AURPSTATE_WaitingForOpenRsp) {
216 dPrintf(D_M_AURP, D_L_WARNING,
217 ("AURPrcvOpenRsp: unexpected response\n"));
218 gbuf_freem(m);
219 return;
220 }
221
222 /* check for the correct connection id */
223 if (hdrp->connection_id != state->rcv_connection_id) {
224 dPrintf(D_M_AURP, D_L_WARNING,
225 ("AURPrcvOpenRsp: invalid connection id, r=%d, m=%d\n",
226 hdrp->connection_id, state->rcv_connection_id));
227 gbuf_freem(m);
228 return;
229 }
230
231 /* cancel the retry timer */
232 untimeout(AURPsndOpenReq_locked, state);
233 state->rcv_tmo = 0;
234 state->rcv_retry = 0;
235
236 /* update state info */
237 state->rcv_sequence_number = AURP_FirstSeqNum;
238 state->rcv_env = hdrp->flags;
239
240 /* check for error */
241 rc = *(short *)(hdrp+1);
242 gbuf_freem(m);
243 if (rc < 0) {
244 dPrintf(D_M_AURP, D_L_WARNING,
245 ("AURPrcvOpenRsp: error=%d\n", rc));
246 return;
247 }
248
249 /* update state info */
250 state->rcv_update_rate = (unsigned short)rc;
251 state->rcv_state = AURPSTATE_Connected;
252 dPrintf(D_M_AURP, D_L_TRACE, ("AURPrcvOpenRsp: moved rcv_state to AURPSTATE_Connected\n"));
253
254 /* start tickle */
255 timeout(AURPsndTickle, state, AURP_TickleRetryInterval*HZ);
256
257 /* get routing info */
258 AURPsndRIReq(state);
259 }
260
261 #endif /* AURP_SUPPORT */