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