]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/asp_proto.c
e582936a81d1728dc37e3a5b097bdb4eff7dc807
[apple/xnu.git] / bsd / netat / asp_proto.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1995 Apple Computer, Inc.
27 *
28 * Change Log:
29 * Created February 20, 1995 by Tuyen Nguyen
30 * Modified for MP, 1996 by Tuyen Nguyen
31 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
32 */
33
34 #include <sys/errno.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <machine/spl.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/filedesc.h>
42 #include <sys/fcntl.h>
43 #include <sys/mbuf.h>
44 #include <sys/ioctl.h>
45 #include <sys/malloc.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48
49 #include <net/if.h>
50
51 #include <netat/appletalk.h>
52 #include <netat/sysglue.h>
53 #include <netat/at_pcb.h>
54 #include <netat/atp.h>
55 #include <netat/ddp.h>
56 #include <netat/asp.h>
57 #include <netat/at_var.h>
58 #include <netat/debug.h>
59
60 static int loop_cnt;
61 #define CHK_LOOP(str) { \
62 if (loop_cnt++ > 100) { \
63 kprintf("%s", str); \
64 break; \
65 } \
66 }
67
68 #define atpBDSsize (sizeof(struct atpBDS)*ATP_TRESP_MAX)
69 #define aspCMDsize (atpBDSsize+sizeof(struct atp_set_default)+TOTAL_ATP_HDR_SIZE)
70 #define SCBS_PER_BLK 16
71 #define TICKS_PER_SEC HZ
72 #define SESS_TMO_RES 2
73 #define DEF_SESS_TMO 120
74 #define NEXT_SEQ_NUM(x) (x = (x == 65535) ? 0 : (x + 1))
75 #define MAX_RCV_CNT 5
76 #define BAD_REMADDR(addr) \
77 ( (*(long *)&scb->rem_addr != *(long *)&addr) \
78 && ((scb->rem_addr.net != addr.net) \
79 || (scb->rem_addr.node != addr.node)) )
80
81 int ASPputmsg();
82 int ASPgetmsg();
83 void asp_init();
84 void asp_ack_reply();
85 void asp_nak_reply();
86 void asp_clock();
87 void asp_clock_funnel();
88 int asp_open();
89 int asp_close();
90 int asp_wput();
91 void atp_retry_req();
92 StaticProc asp_scb_t *asp_find_scb();
93 StaticProc asp_scb_t *asp_scb_alloc();
94
95 StaticProc void asp_putnext();
96 StaticProc void asp_iocack();
97 StaticProc void asp_iocnak();
98 StaticProc void asp_dequeue_scb();
99 StaticProc void asp_scb_free();
100 StaticProc void asp_timout();
101 StaticProc void asp_untimout();
102 StaticProc void asp_hangup();
103 StaticProc void asp_send_tickle();
104 StaticProc void asp_send_tickle_funnel();
105 StaticProc void asp_accept();
106 StaticProc int asp_send_req();
107
108 extern at_ifaddr_t *ifID_home;
109 extern int atp_pidM[];
110 extern gref_t *atp_inputQ[];
111 gbuf_t *scb_resource_m = 0;
112 unsigned char asp_inpC[256];
113 asp_scb_t *asp_scbQ[256];
114
115 static at_retry_t asp_def_retry = {2, -1, 1};
116 static unsigned char scb_tmo_cnt;
117 asp_scb_t *scb_used_list;
118 static asp_scb_t *scb_tmo_list;
119 asp_scb_t *scb_free_list;
120 atlock_t aspall_lock, asptmo_lock;
121
122 int
123 asp_readable(gref)
124 gref_t *gref;
125 {
126 return (((asp_scb_t *)gref->info)->sess_ioc ? 1 : 0);
127 }
128
129 void
130 asp_init()
131 {
132 scb_tmo_cnt = 1;
133 scb_tmo_list = 0;
134 scb_used_list = 0;
135 scb_free_list = 0;
136 bzero(asp_inpC, sizeof(asp_inpC));
137 bzero(asp_scbQ, sizeof(asp_scbQ));
138 }
139
140 /*
141 * the open routine allocates a state structure
142 */
143 int asp_open(gref)
144 gref_t *gref;
145 {
146 int s;
147 asp_scb_t *scb;
148
149 /*
150 * if no asp structure available, return failure
151 */
152 if ((scb = asp_scb_alloc()) == 0)
153 return ENOBUFS;
154
155 /*
156 * initialize the gref data structure
157 */
158 gref->info = (void *)scb;
159 gref->readable = asp_readable;
160
161 /*
162 * initialize the scb data structure
163 */
164 scb->dflag = 1;
165 scb->magic_num = 222;
166 scb->state = ASPSTATE_Idle;
167 scb->pid = gref->pid;
168 scb->gref = gref;
169 scb->session_timer = DEF_SESS_TMO;
170 scb->cmd_retry = asp_def_retry;
171 ATDISABLE(s, aspall_lock);
172 if ((scb->next_scb = scb_used_list) != 0)
173 scb->next_scb->prev_scb = scb;
174 scb_used_list = scb;
175 ATENABLE(s, aspall_lock);
176
177 /*
178 * return success
179 */
180 dPrintf(D_M_ASP, D_L_INFO, ("asp_open: pid=%d\n", scb->pid));
181 return 0;
182 } /* asp_open */
183
184 /*
185 * the close routine frees all the data structures
186 */
187 int
188 asp_close(gref)
189 gref_t *gref;
190 {
191 int s;
192 unsigned char sock_num;
193 asp_scb_t *scb, *new_scb;
194 gbuf_t *m;
195
196 scb = (asp_scb_t *)gref->info;
197 dPrintf(D_M_ASP, D_L_INFO, ("asp_close: loc=%d\n",
198 scb->loc_addr.socket));
199
200 if (scb->pid && scb->sess_ioc && (scb->dflag != 1)) {
201 /*
202 * send the CloseSess response to peer
203 */
204 if (gbuf_type(scb->sess_ioc) != MSG_PROTO) {
205 ATDISABLE(s, scb->lock);
206 m = scb->sess_ioc;
207 scb->sess_ioc = gbuf_next(m);
208 ATENABLE(s, scb->lock);
209 atp_send_rsp(scb->gref, m, TRUE);
210 }
211 }
212
213 if (scb->atp_state) {
214 sock_num = scb->loc_addr.socket;
215 ATDISABLE(s, aspall_lock);
216 if ((scb->dflag != 1) && scb->stat_msg) {
217 untimeout(atp_retry_req, scb->stat_msg);
218 gbuf_freem(scb->stat_msg);
219 scb->stat_msg = 0;
220 }
221 if (asp_scbQ[sock_num]->next_scb == 0) {
222 asp_scbQ[sock_num] = 0;
223 asp_inpC[sock_num] = 0;
224 ATENABLE(s, aspall_lock);
225 dPrintf(D_M_ASP, D_L_INFO,
226 (" : atp_close(), loc=%d\n", scb->loc_addr.socket));
227 atp_close(gref, 0);
228 } else {
229 asp_inpC[sock_num]--;
230 if (scb == asp_scbQ[sock_num]) {
231 new_scb = scb->next_scb;
232 new_scb->prev_scb = 0;
233 asp_scbQ[sock_num] = new_scb;
234 new_scb->atp_state->atp_gref = new_scb->gref;
235 new_scb->atp_state->pid = new_scb->pid;
236 atp_inputQ[sock_num] = new_scb->gref;
237 } else {
238 if ((scb->prev_scb->next_scb = scb->next_scb) != 0)
239 scb->next_scb->prev_scb = scb->prev_scb;
240 }
241 scb->next_scb = 0;
242 ATENABLE(s, aspall_lock);
243 }
244 } else
245 asp_dequeue_scb(scb);
246
247 /*
248 * free all allocated blocks if any
249 */
250 ATDISABLE(s, scb->lock);
251 if (scb->stat_msg) {
252 gbuf_freem(scb->stat_msg);
253 scb->stat_msg = 0;
254 }
255 if (scb->sess_ioc) {
256 gbuf_freel(scb->sess_ioc);
257 scb->sess_ioc = 0;
258 }
259 if (scb->req_msgq) {
260 gbuf_freel(scb->req_msgq);
261 scb->req_msgq = 0;
262 }
263
264 scb->rem_addr.node = 0;
265 ATENABLE(s, scb->lock);
266
267 /*
268 * stop all timers
269 */
270 scb->tmo_cnt = 0;
271 asp_untimout(asp_hangup, scb);
272 untimeout(asp_send_tickle_funnel, (void *)scb); /* added for 2225395 */
273
274 /*
275 * free the asp session control block
276 */
277 scb->state = ASPSTATE_Close;
278 asp_scb_free(scb);
279 return 0;
280 } /* asp_close */
281
282 static char *aspStateStr(state)
283 int state;
284 {
285 return ((state==ASPSTATE_Close)? "Close":
286 (state==ASPSTATE_Idle)? "Idle":
287 (state==ASPSTATE_WaitingForGetStatusRsp)? "GetStatusRsp":
288 (state==ASPSTATE_WaitingForOpenSessRsp)? "OpenSessRsp":
289 (state==ASPSTATE_WaitingForCommandRsp)? "CmdRsp":
290 (state==ASPSTATE_WaitingForWriteContinue)? "WriteCont":
291 (state==ASPSTATE_WaitingForWriteRsp)? "WriteRsp":
292 (state==ASPSTATE_WaitingForWriteContinueRsp)? "WriteContRsp":
293 (state==ASPSTATE_WaitingForCloseSessRsp)? "CloseSessRsp":
294 "unknown");
295 }
296
297 static char *aspCmdStr(aspCmd)
298 int aspCmd;
299 {
300 return ((aspCmd==ASPFUNC_CloseSess)? "CloseSess":
301 (aspCmd==ASPFUNC_Command)? "Command":
302 (aspCmd==ASPFUNC_GetStatus)? "GetStatus":
303 (aspCmd==ASPFUNC_OpenSess)? "OpenSess":
304 (aspCmd==ASPFUNC_Tickle)? "Tickle":
305 (aspCmd==ASPFUNC_Write)? "Write":
306 (aspCmd==ASPFUNC_WriteContinue)? "WriteContinue":
307 (aspCmd==ASPFUNC_Attention)? "Attention":
308 (aspCmd==ASPFUNC_CmdReply)? "CmdReply": "unknown");
309 }
310
311 static char *aspIOCStr(aspIOC)
312 int aspIOC;
313 {
314 return (
315 (aspIOC==ASPIOC_ClientBind)? "ClientBind":
316 (aspIOC==ASPIOC_CloseSession)? "CloseSession":
317 (aspIOC==ASPIOC_GetLocEntity)? "GetLocEntity":
318 (aspIOC==ASPIOC_GetRemEntity)? "GetRemEntity":
319 (aspIOC==ASPIOC_GetSession)? "GetSession":
320 (aspIOC==ASPIOC_GetStatus)? "GetStatus":
321 (aspIOC==ASPIOC_ListenerBind)? "ListenerBind":
322 (aspIOC==ASPIOC_OpenSession)? "OpenSession":
323 (aspIOC==ASPIOC_StatusBlock)? "StatusBlock":
324 (aspIOC==ASPIOC_SetPid)? "SetPid":
325 (aspIOC==ASPIOC_GetSessId)? "GetSessId":
326 (aspIOC==ASPIOC_EnableSelect)? "EnableSelect":
327 (aspIOC==ASPIOC_Look)? "Look":
328 "unknown"
329 );
330 }
331
332 #ifdef AT_MBUF_TRACE
333
334 static char mbuf_str[100];
335 char *mbuf_totals()
336 {
337 sprintf(mbuf_str,
338 /*
339 "dat = %d, prot = %d, ioc = %d, err = %d, hu = %d, ack = %d, nak = %d, ctl = %d",
340 */
341 "dat = %d, prot = %d, ioc = %d, ctl = %d",
342 mbstat.m_mtypes[MSG_DATA], mbstat.m_mtypes[MSG_PROTO], mbstat.m_mtypes[MSG_IOCTL],
343 /*
344 mbstat.m_mtypes[MSG_ERROR], mbstat.m_mtypes[MSG_HANGUP], mbstat.m_mtypes[MSG_IOCACK],
345 mbstat.m_mtypes[MSG_IOCNAK],
346 */
347 mbstat.m_mtypes[MSG_CTL]);
348 return(&mbuf_str[0]);
349 }
350
351 void trace_beg(str, m)
352 char *str;
353 gbuf_t *m;
354 {
355 int i = 0, j = 0;
356 gbuf_t *mdata, *mchain;
357
358 if (m)
359 for (i = 0, j = 0, mdata = m, mchain = m; mdata; i++) {
360 mdata = gbuf_cont(mdata);
361 if (!mdata && mchain) {
362 mdata = gbuf_next(mchain);
363 mchain = mdata;
364 j++;
365 }
366 }
367 dPrintf(D_M_ASP, D_L_TRACE,
368 ("%s: %s, m# = %d, c# = %d\n", str, mbuf_totals(), i, j));
369 }
370
371 void trace_end(str)
372 char *str;
373 {
374 dPrintf(D_M_ASP, D_L_TRACE,
375 (" %s: %s\n", str, mbuf_totals()));
376 }
377 #endif AT_MBUF_TRACE
378
379 /*
380 * the write routine
381 */
382 int asp_wput(gref, m)
383 gref_t *gref;
384 gbuf_t *m;
385 {
386 int s, err;
387 unsigned char sockSav, sock_num;
388 gbuf_t *mioc, *mdata;
389 ioc_t *iocbp;
390 asp_scb_t *scb, *server_scb, *curr_scb;
391 at_inet_t *addr;
392 asp_word_t aw;
393 union asp_primitives *primitives;
394 asp_status_cmd_t *status_cmd;
395 asp_open_cmd_t *open_cmd;
396 at_retry_t Retry;
397
398 scb = (asp_scb_t *)gref->info;
399 if (scb->dflag == 0) {
400 atp_wput(gref, m);
401 return 0;
402 }
403
404 if (gbuf_type(m) != MSG_IOCTL) {
405 dPrintf(D_M_ASP, D_L_WARNING,
406 ("asp_wput: UNKNOWN message, type=%d\n",
407 gbuf_type(m)));
408 gbuf_freem(m);
409 return 0;
410 }
411
412 mioc = m;
413 iocbp = (ioc_t *)gbuf_rptr(mioc);
414
415 dPrintf(D_M_ASP_LOW, D_L_INFO,
416 ("asp_wput: %s, loc=%d, state=%s\n",
417 aspIOCStr(iocbp->ioc_cmd), scb->loc_addr.socket,
418 aspStateStr(scb->state)));
419
420 switch (iocbp->ioc_cmd) {
421 case ASPIOC_CloseSession:
422 if ((scb->state == ASPSTATE_Close) || (scb->rem_addr.node == 0))
423 break;
424
425 Retry.retries = 3;
426 Retry.interval = 1;
427 aw.func = ASPFUNC_CloseSess;
428 aw.param1 = scb->sess_id;
429 aw.param2 = 0;
430 iocbp->ioc_private = (void *)scb;
431 scb->ioc_wait = (unsigned char)(iocbp->ioc_cmd & 0xff);
432 iocbp->ioc_cmd = AT_ATP_ISSUE_REQUEST;
433 asp_send_req(gref, mioc, &scb->rem_addr, &Retry, &aw,
434 0, ASPSTATE_WaitingForCloseSessRsp, 0x01);
435 return 0;
436
437 case ASPIOC_ClientBind:
438 /*
439 * open an ATP channel
440 */
441 if ((err = atp_open(gref, 0)) != 0) {
442 asp_iocnak(gref, mioc, err);
443 return 0;
444 }
445 scb->atp_state = (atp_state_t *)gref->info;
446 scb->atp_state->pid = scb->pid;
447 /*
448 * bind to any available socket
449 */
450 scb->dflag = 2;
451 sockSav = scb->dflag;
452 if ((sock_num = (at_socket)atp_bind(gref, 0, &sockSav)) == 0) {
453 scb->atp_state = (atp_state_t *)0;
454 atp_close(gref, 0);
455 gref->info = (void *)scb;
456 asp_iocnak(gref, mioc, EINVAL);
457 return 0;
458 }
459 gref->info = (void *)scb;
460 asp_dequeue_scb(scb);
461 scb->atp_state->dflag = scb->dflag;
462 scb->loc_addr.socket = sock_num;
463 asp_scbQ[sock_num] = scb;
464 asp_inpC[sock_num]++;
465 atp_pidM[sock_num] = 0;
466 break;
467
468 case ASPIOC_ListenerBind:
469 /*
470 * open an ATP channel
471 */
472 if ((err = atp_open(gref, 0)) != 0) {
473 asp_iocnak(gref, mioc, err);
474 return 0;
475 }
476 scb->atp_state = (atp_state_t *)gref->info;
477 scb->atp_state->pid = scb->pid;
478 /*
479 * bind to any available socket
480 */
481 if ((sock_num = (at_socket)atp_bind(gref, 0, 0)) == 0) {
482 scb->atp_state = (atp_state_t *)0;
483 atp_close(gref, 0);
484 gref->info = (void *)scb;
485 asp_iocnak(gref, mioc, EINVAL);
486 return 0;
487 }
488 gref->info = (void *)scb;
489 asp_dequeue_scb(scb);
490 scb->atp_state->dflag = scb->dflag;
491 scb->loc_addr.socket = sock_num;
492 asp_scbQ[sock_num] = scb;
493 asp_inpC[sock_num]++;
494 if (gbuf_cont(mioc))
495 *(at_inet_t *)gbuf_rptr(gbuf_cont(mioc)) = scb->loc_addr;
496 break;
497
498 case ASPIOC_GetLocEntity:
499 if ((gbuf_cont(mioc) == 0) || (scb->atp_state == 0)) {
500 asp_iocnak(gref, mioc, EPROTO);
501 return 0;
502 }
503 *(at_inet_t *)gbuf_rptr(gbuf_cont(mioc)) = scb->loc_addr;
504 break;
505
506 case ASPIOC_GetRemEntity:
507 if ((gbuf_cont(mioc) == 0) || (scb->atp_state == 0)) {
508 asp_iocnak(gref, mioc, EPROTO);
509 return 0;
510 }
511 *(at_inet_t *)gbuf_rptr(gbuf_cont(mioc)) = scb->rem_addr;
512 break;
513
514 case ASPIOC_GetSession:
515 if ((mdata = gbuf_cont(mioc)) == 0) {
516 asp_iocnak(gref, mioc, EPROTO);
517 return 0;
518 }
519 addr = (at_inet_t *)gbuf_rptr(mdata);
520 scb->tickle_interval = (unsigned short)addr->node;
521 scb->session_timer = addr->net;
522 server_scb = asp_scbQ[addr->socket];
523 /*### LD 10/28/97: changed to make sure we're not accessing a null server_scb */
524 if (server_scb == 0) {
525 asp_iocnak(gref, mioc, EPROTO);
526 return 0;
527 }
528 if (server_scb->sess_ioc == 0) {
529 asp_iocnak(gref, mioc, EPROTO);
530 return 0;
531 }
532
533 /*
534 * open an ATP channel
535 */
536 if ((err = atp_open(gref, 0)) != 0) {
537 gref->info = (void *)scb;
538 asp_iocnak(gref, mioc, err);
539 return 0;
540 }
541 scb->atp_state = (atp_state_t *)gref->info;
542 scb->atp_state->pid = scb->pid;
543 /*
544 * bind to any available socket
545 */
546 scb->dflag = 3;
547 sockSav = scb->dflag;
548 if ((sock_num = (at_socket)atp_bind(gref, 0, &sockSav)) == 0) {
549 atp_close(gref, 0);
550 asp_dequeue_scb(scb);
551 ATDISABLE(s, aspall_lock);
552 sock_num = sockSav;
553 scb->loc_addr.socket = sock_num;
554 for (curr_scb = asp_scbQ[sock_num];
555 curr_scb->next_scb; curr_scb = curr_scb->next_scb) ;
556 scb->prev_scb = curr_scb;
557 curr_scb->next_scb = scb;
558 scb->atp_state = curr_scb->atp_state;
559 ATENABLE(s, aspall_lock);
560 } else {
561 asp_dequeue_scb(scb);
562 ATDISABLE(s, aspall_lock);
563 scb->loc_addr.socket = sock_num;
564 asp_scbQ[sock_num] = scb;
565 scb->atp_state->dflag = scb->dflag;
566 ATENABLE(s, aspall_lock);
567 }
568 gref->info = (void *)scb;
569 asp_inpC[sock_num]++;
570 gbuf_cont(mioc) = 0;
571 asp_accept(server_scb, scb, mdata);
572 break;
573
574 case ASPIOC_GetStatus:
575 if ((mdata = gbuf_cont(mioc)) == 0) {
576 asp_iocnak(gref, mioc, EINVAL);
577 return 0;
578 }
579 gbuf_cont(mioc) = 0;
580 status_cmd = (asp_status_cmd_t *)gbuf_rptr(mdata);
581 aw.func = ASPFUNC_GetStatus;
582 aw.param1 = 0;
583 aw.param2 = 0;
584 scb->ioc_wait = (unsigned char)(iocbp->ioc_cmd & 0xff);
585 iocbp->ioc_cmd = AT_ATP_ISSUE_REQUEST_DEF;
586 /* bms: make sure this is an ALO request */
587 asp_send_req(gref, mioc, &status_cmd->SLSEntityIdentifier,
588 &status_cmd->Retry, &aw, 0, ASPSTATE_WaitingForGetStatusRsp, 0xff);
589 gbuf_freeb(mdata);
590 return 0;
591
592 case ASPIOC_OpenSession:
593 if ((mdata = gbuf_cont(mioc)) == 0) {
594 asp_iocnak(gref, mioc, EINVAL);
595 return 0;
596 }
597 gbuf_cont(mioc) = 0;
598 open_cmd = (asp_open_cmd_t *)gbuf_rptr(mdata);
599 scb->svc_addr = open_cmd->SLSEntityIdentifier;
600 scb->rem_addr = scb->svc_addr;
601 scb->rem_node = scb->rem_addr.node;
602 scb->rem_addr.node = 0;
603 scb->tickle_interval = open_cmd->TickleInterval;
604 scb->session_timer = open_cmd->SessionTimer;
605 aw.func = ASPFUNC_OpenSess;
606 aw.param1 = scb->loc_addr.socket;
607 aw.param2 = ASP_Version;
608 scb->ioc_wait = (unsigned char)(iocbp->ioc_cmd & 0xff);
609 iocbp->ioc_cmd = AT_ATP_ISSUE_REQUEST_DEF;
610 asp_send_req(gref, mioc, &open_cmd->SLSEntityIdentifier,
611 &open_cmd->Retry, &aw, 1, ASPSTATE_WaitingForOpenSessRsp, 0x01);
612 gbuf_freeb(mdata);
613 return 0;
614
615 case ASPIOC_StatusBlock:
616 /*
617 * save the server status block
618 */
619 if (scb->stat_msg)
620 gbuf_freem(scb->stat_msg);
621 scb->stat_msg = gbuf_cont(mioc);
622 gbuf_cont(mioc) = 0;
623 break;
624
625 /* *** Does scb->pid get used in a packet header,
626 and if so is it in ASP, or in ATP?
627 If not, do we need this call for anything?
628 (cap does currently use it in _ANS code.)
629 *** */
630 case ASPIOC_SetPid:
631 if (gbuf_cont(mioc) == 0) {
632 asp_iocnak(gref, mioc, EINVAL);
633 return 0;
634 }
635 scb->pid = *(int *)gbuf_rptr(gbuf_cont(mioc));
636 break;
637
638 case ASPIOC_GetSessId:
639 if (gbuf_cont(mioc) == 0) {
640 asp_iocnak(gref, mioc, EINVAL);
641 return 0;
642 }
643 *(gref_t **)gbuf_rptr(gbuf_cont(mioc)) = gref;
644 break;
645
646 case ASPIOC_Look:
647 if (gbuf_cont(mioc) == 0) {
648 asp_iocnak(gref, mioc, EINVAL);
649 return 0;
650 }
651 if (scb->sess_ioc) {
652 primitives = (union asp_primitives *)gbuf_rptr(scb->sess_ioc);
653 if (primitives->Primitive == ASPFUNC_CmdReply)
654 *(int *)gbuf_rptr(gbuf_cont(mioc)) = 0;
655 else
656 *(int *)gbuf_rptr(gbuf_cont(mioc)) = 1;
657 } else
658 *(int *)gbuf_rptr(gbuf_cont(mioc)) = -1;
659 break;
660
661 case DDP_IOC_GET_CFG:
662 {
663 struct atp_state *atp = (struct atp_state *)gref->info;
664 if (atp->dflag)
665 atp = atp->atp_msgq;
666
667 if (gbuf_cont(mioc) == 0) {
668 asp_iocnak(gref, mioc, EINVAL);
669 return 0;
670 }
671 /* *** borrowed from ddp_proto.c to handle DDP_IOC_GET_CFG
672 on atp fd *** */
673 scb->state = ASPSTATE_Idle;
674 {
675 /* *** was ddp_get_cfg() *** */
676 ddp_addr_t *cfgp =
677 (ddp_addr_t *)gbuf_rptr(gbuf_cont(mioc));
678 cfgp->inet.net = ifID_home->ifThisNode.s_net;
679 cfgp->inet.node = ifID_home->ifThisNode.s_node;
680 cfgp->inet.socket = atp->atp_socket_no;
681 cfgp->ddptype = DDP_ATP;
682 }
683 gbuf_wset(gbuf_cont(mioc), sizeof(at_inet_t));
684 }
685 break;
686
687 default:
688 asp_iocnak(gref, mioc, EINVAL);
689 return 0;
690 }
691
692 asp_iocack(gref, mioc);
693 return 0;
694 } /* asp_wput */
695
696 /*
697 * send request routine
698 */
699 StaticProc int
700 asp_send_req(gref, mioc, dest, retry, awp, xo, state, bitmap)
701 gref_t *gref;
702 gbuf_t *mioc;
703 at_inet_t *dest;
704 at_retry_t *retry;
705 asp_word_t *awp;
706 unsigned char xo;
707 unsigned char state;
708 unsigned char bitmap;
709 {
710 int i;
711 gbuf_t *mdata;
712 ioc_t *iocbp;
713 struct atp_set_default *sd;
714 at_ddp_t *ddp;
715 at_atp_t *atp;
716 struct atpBDS *atpBDS;
717 asp_scb_t *scb = (asp_scb_t *)gref->info;
718
719 /*
720 * allocate an ATP buffer for the request
721 */
722 if ((gbuf_cont(mioc) = gbuf_alloc(aspCMDsize, PRI_MED)) == 0) {
723 if (awp->func == ASPFUNC_Tickle)
724 gbuf_freem(mioc);
725 else
726 asp_iocnak(gref, mioc, ENOBUFS);
727 dPrintf(D_M_ASP, D_L_WARNING,
728 ("asp_send_req: ENOBUFS, loc=%d\n", scb->loc_addr.socket));
729
730 return -1;
731 }
732 mdata = gbuf_cont(mioc);
733 iocbp = (ioc_t *)gbuf_rptr(mioc);
734
735 /*
736 * build the request
737 */
738 atpBDS = (struct atpBDS *)gbuf_rptr(mdata);
739 gbuf_wset(mdata,atpBDSsize);
740 for (i=0; i < ATP_TRESP_MAX; i++) {
741 *(unsigned long *)atpBDS[i].bdsBuffAddr = 1;
742 *(unsigned short *)atpBDS[i].bdsBuffSz = ATP_DATA_SIZE;
743 }
744 sd = (struct atp_set_default *)gbuf_wptr(mdata);
745 gbuf_winc(mdata,sizeof(struct atp_set_default));
746 sd->def_retries = (retry->retries == -1) ?
747 ATP_INFINITE_RETRIES : retry->retries;
748 sd->def_rate = retry->interval*TICKS_PER_SEC;
749 sd->def_BDSlen = atpBDSsize;
750 ddp = (at_ddp_t *)gbuf_wptr(mdata);
751 NET_ASSIGN(ddp->src_net, scb->loc_addr.net);
752 ddp->src_node = scb->loc_addr.node;
753 NET_ASSIGN(ddp->dst_net, dest->net);
754 ddp->dst_node = dest->node;
755 ddp->dst_socket = dest->socket;
756 UAS_ASSIGN(ddp->checksum, 0);
757 atp = ATP_ATP_HDR(gbuf_wptr(mdata));
758 atp->xo = xo;
759 atp->xo_relt = xo;
760 atp->bitmap = bitmap;
761 gbuf_winc(mdata,TOTAL_ATP_HDR_SIZE);
762 *(asp_word_t *)atp->user_bytes = *awp;
763 iocbp->ioc_count = gbuf_len(mdata);
764 iocbp->ioc_rval = 0;
765
766 /*
767 * send the request
768 */
769 scb->state = state;
770 dPrintf(D_M_ASP, D_L_INFO,
771 ("asp_send_req: %s, loc=%d, rem= %d, len=%d, state=%s\n",
772 aspCmdStr(awp->func),
773 scb->loc_addr.socket, ddp->dst_socket, iocbp->ioc_count,
774 aspStateStr(scb->state)));
775
776 atp_send_req(gref, mioc);
777 return 0;
778 }
779
780 /*
781 * send tickle routine - funnelled version
782 */
783 StaticProc void
784 asp_send_tickle_funnel(scb)
785 asp_scb_t *scb;
786 {
787 thread_funnel_set(network_flock, TRUE);
788 asp_send_tickle(scb);
789 thread_funnel_set(network_flock, FALSE);
790 }
791
792
793 /*
794 * send tickle routine
795 */
796 StaticProc void
797 asp_send_tickle(scb)
798 asp_scb_t *scb;
799 {
800 gbuf_t *mioc;
801 at_retry_t retry;
802 asp_word_t aw;
803 at_inet_t *dest;
804
805
806 /*
807 * make sure the connection is still there
808 */
809 if (scb->rem_addr.node == 0) {
810 return;
811 }
812
813 if ((mioc = gbuf_alloc(sizeof(ioc_t), PRI_HI)) == 0) {
814 dPrintf(D_M_ASP, D_L_WARNING,
815 ("asp_send_tickle: ENOBUFS 0, loc=%d, rem=%d\n",
816 scb->loc_addr.socket,scb->rem_addr.socket));
817 timeout(asp_send_tickle_funnel, (void *)scb, 10);
818 return;
819 }
820 gbuf_wset(mioc,sizeof(ioc_t));
821 gbuf_set_type(mioc, MSG_IOCTL);
822
823 dest = scb->svc_addr.node ?
824 (at_inet_t *)&scb->svc_addr : (at_inet_t *)&scb->rem_addr;
825 retry.interval = scb->tickle_interval;
826 retry.retries = -1;
827 retry.backoff = 1;
828 aw.func = ASPFUNC_Tickle;
829 aw.param1 = scb->sess_id;
830 aw.param2 = 0;
831 ((ioc_t *)gbuf_rptr(mioc))->ioc_cr = (void *)scb;
832 ((ioc_t *)gbuf_rptr(mioc))->ioc_cmd = AT_ATP_ISSUE_REQUEST_TICKLE;
833
834 if (asp_send_req(scb->gref, mioc, dest, &retry, &aw, 0, scb->state, 0)) {
835 dPrintf(D_M_ASP, D_L_WARNING,
836 ("asp_send_tickle: ENOBUFS 1, loc=%d, rem=%d\n",
837 scb->loc_addr.socket,scb->rem_addr.socket));
838
839 timeout(asp_send_tickle_funnel, (void *)scb, 10);
840 return;
841 }
842 }
843
844 /*
845 * accept connection routine
846 */
847 StaticProc void
848 asp_accept(scb, sess_scb, m)
849 asp_scb_t *scb;
850 asp_scb_t *sess_scb;
851 gbuf_t *m;
852 {
853 int s;
854 gbuf_t *mdata;
855 at_ddp_t *ddp;
856 at_atp_t *atp;
857 asp_word_t *awp;
858 at_inet_t rem_addr;
859
860 mdata = scb->sess_ioc;
861 ddp = (at_ddp_t *)gbuf_rptr(mdata);
862 atp = (at_atp_t *)(gbuf_rptr(mdata) + DDP_X_HDR_SIZE);
863 rem_addr.net = NET_VALUE(ddp->src_net);
864 rem_addr.node = ddp->src_node;
865 rem_addr.socket = ddp->src_socket;
866 awp = (asp_word_t *)atp->user_bytes;
867
868 sess_scb->loc_addr.net = NET_VALUE(ddp->dst_net);
869 sess_scb->loc_addr.node = ddp->dst_node;
870 NET_ASSIGN(ddp->src_net, sess_scb->loc_addr.net);
871 ddp->src_node = sess_scb->loc_addr.node;
872 NET_ASSIGN(ddp->dst_net, rem_addr.net);
873 ddp->dst_node = rem_addr.node;
874 ddp->dst_socket = rem_addr.socket;
875
876 sess_scb->sess_id = sess_scb->loc_addr.socket;
877 sess_scb->rem_socket = rem_addr.socket;
878 sess_scb->rem_addr = rem_addr;
879 sess_scb->rem_addr.socket = awp->param1;
880 sess_scb->reply_socket = sess_scb->rem_addr.socket;
881 awp->func = sess_scb->loc_addr.socket;
882 awp->param1 = sess_scb->sess_id;
883 awp->param2 = 0;
884 gbuf_freeb(m);
885 ATDISABLE(s, scb->lock);
886 scb->sess_ioc = gbuf_next(mdata);
887 ATENABLE(s, scb->lock);
888 gbuf_next(mdata) = 0;
889 asp_timout(asp_hangup, sess_scb, sess_scb->session_timer);
890 atp_send_rsp(scb->gref, mdata, TRUE);
891 asp_send_tickle(sess_scb);
892 dPrintf(D_M_ASP, D_L_INFO,
893 ("asp_accept: ACCEPT connect request, loc=%d, rem=%x.%x.%d\n",
894 sess_scb->loc_addr.socket,
895 sess_scb->rem_addr.net,
896 sess_scb->rem_addr.node,sess_scb->rem_addr.socket));
897 } /* asp_accept */
898
899 /*
900 * timer routine - funneled version
901 */
902 void asp_clock_funnel(arg)
903 void *arg;
904 {
905 thread_funnel_set(network_flock, TRUE);
906 asp_clock(arg);
907 thread_funnel_set(network_flock, FALSE);
908 }
909
910 /*
911 * timer routine
912 */
913 void asp_clock(arg)
914 void *arg;
915 {
916 int s;
917 asp_scb_t *scb;
918 void (*tmo_func)();
919
920 ATDISABLE(s, asptmo_lock);
921 if (scb_tmo_list)
922 scb_tmo_list->tmo_delta--;
923 while (((scb = scb_tmo_list) != 0) && (scb_tmo_list->tmo_delta == 0)) {
924 if ((scb_tmo_list = scb->next_tmo) != 0)
925 scb_tmo_list->prev_tmo = 0;
926 if ((tmo_func = scb->tmo_func) != 0) {
927 scb->tmo_func = 0;
928 ATENABLE(s, asptmo_lock);
929 (*tmo_func)(scb);
930 ATDISABLE(s, asptmo_lock);
931 }
932 }
933 ATENABLE(s, asptmo_lock);
934
935 if (++scb_tmo_cnt == 0) scb_tmo_cnt++;
936 timeout(asp_clock_funnel, (void *)arg, (1<<SESS_TMO_RES)*TICKS_PER_SEC);
937
938 }
939
940 /*
941 * ACK reply routine
942 */
943 void
944 asp_ack_reply(gref, mioc)
945 register gref_t *gref;
946 register gbuf_t *mioc;
947 {
948 int s;
949 int len, msize, nbds;
950 register gbuf_t *mdata, *m, *mx;
951 struct atpBDS *atpBDS;
952 at_ddp_t *ddp;
953 at_atp_t *atp;
954 register asp_scb_t *scb, *sess_scb;
955 register ioc_t *iocbp;
956 register asp_word_t *awp;
957 register asp_command_ind_t *command_ind;
958 register asp_cmdreply_ind_t *cmdreply_ind;
959 at_inet_t rem_addr;
960
961 iocbp = (ioc_t *)gbuf_rptr(mioc);
962
963 if (iocbp->ioc_cmd == AT_ATP_ISSUE_REQUEST_TICKLE) {
964 /*
965 * ignore the ack for the tickle request
966 */
967 scb = (asp_scb_t *)iocbp->ioc_cr;
968 scb->tickle_tid = (unsigned short)iocbp->ioc_rval;
969 gbuf_freem(mioc);
970 return;
971 }
972
973 scb = (asp_scb_t *)gref->info;
974 if (scb == 0) {
975 gbuf_freem(mioc);
976 return;
977 }
978
979 if (iocbp->ioc_cmd == AT_ATP_GET_POLL) {
980 /*
981 * if no data, just drop the request
982 */
983 if ((mdata = gbuf_cont(mioc)) == 0) {
984 gbuf_freeb(mioc);
985 return;
986 }
987
988 gbuf_set_type(mioc, MSG_IOCTL);
989 ddp = (at_ddp_t *)gbuf_rptr(mdata);
990 gbuf_rinc(mdata,DDP_X_HDR_SIZE);
991 atp = (at_atp_t *)gbuf_rptr(mdata);
992 gbuf_rinc(mdata,ATP_HDR_SIZE);
993 rem_addr.net = NET_VALUE(ddp->src_net);
994 rem_addr.node = ddp->src_node;
995 rem_addr.socket = ddp->src_socket;
996 awp = (asp_word_t *)atp->user_bytes;
997
998 if (scb->next_scb) {
999 /*
1000 * find the responsible scb
1001 */
1002 if ((scb = asp_find_scb(scb->loc_addr.socket, &rem_addr)) == 0) {
1003 gbuf_freem(mioc);
1004 return;
1005 }
1006 }
1007 dPrintf(D_M_ASP, D_L_INFO,
1008 ("asp_ack_reply: %s, loc=%d, rem=%x.%x.%d\n",
1009 aspCmdStr(awp->func),scb->loc_addr.socket,
1010 NET_VALUE(ddp->src_net) ,ddp->src_node,ddp->src_socket));
1011
1012 if (scb->rem_addr.node)
1013 asp_untimout(asp_hangup, scb);
1014
1015 switch (awp->func) {
1016 case ASPFUNC_GetStatus:
1017 /*
1018 * ignore if this is not a server socket
1019 */
1020 mx = 0;
1021 if ((scb->dflag != 1) || (scb->stat_msg
1022 && ((mx = gbuf_dupb(scb->stat_msg)) == 0)))
1023 break;
1024 gbuf_freeb(mioc);
1025
1026 /*
1027 * send the status block
1028 */
1029 if (gbuf_cont(mdata)) {
1030 gbuf_freem(gbuf_cont(mdata));
1031 gbuf_cont(mdata) = 0;
1032 }
1033 gbuf_rdec(mdata,TOTAL_ATP_HDR_SIZE);
1034 if ((m = gbuf_alloc( (TOTAL_ATP_HDR_SIZE+atpBDSsize), PRI_MED)) == 0) {
1035 gbuf_freem(mdata);
1036 gbuf_freeb(mx);
1037 goto l_done;
1038 }
1039 bcopy(gbuf_rptr(mdata), gbuf_rptr(m), TOTAL_ATP_HDR_SIZE);
1040 gbuf_freeb(mdata);
1041 mdata = m;
1042 ddp = (at_ddp_t *)gbuf_rptr(mdata);
1043 gbuf_wset(mdata,DDP_X_HDR_SIZE);
1044 atp = (at_atp_t *)gbuf_wptr(mdata);
1045 gbuf_winc(mdata,ATP_HDR_SIZE);
1046 awp = (asp_word_t *)atp->user_bytes;
1047 NET_NET(ddp->src_net, ddp->dst_net);
1048 ddp->src_node = ddp->dst_node;
1049 NET_ASSIGN(ddp->dst_net, rem_addr.net);
1050 ddp->dst_node = rem_addr.node;
1051 ddp->dst_socket = rem_addr.socket;
1052 UAS_ASSIGN(ddp->checksum, 0);
1053 atpBDS = (struct atpBDS *)gbuf_wptr(mdata);
1054 msize = mx ? gbuf_msgsize(mx) : 0;
1055 for (nbds=0; (nbds < ATP_TRESP_MAX) && (msize > 0); nbds++) {
1056 len = msize < ATP_DATA_SIZE ? msize : ATP_DATA_SIZE;
1057 msize -= ATP_DATA_SIZE;
1058 *(long *)atpBDS[nbds].bdsUserData = 0;
1059 UAL_ASSIGN(atpBDS[nbds].bdsBuffAddr, 1);
1060 UAS_ASSIGN(atpBDS[nbds].bdsBuffSz, len);
1061 }
1062 UAS_ASSIGN(atpBDS[0].bdsDataSz, nbds);
1063 gbuf_winc(mdata,atpBDSsize);
1064 gbuf_cont(mdata) = mx;
1065 atp_send_rsp(gref, mdata, FALSE);
1066 goto l_done;
1067
1068 case ASPFUNC_OpenSess:
1069 /*
1070 * ignore if server is not ready
1071 */
1072 if ((scb->dflag != 1) || (scb->stat_msg == 0))
1073 break;
1074 gbuf_freeb(mioc);
1075
1076 if (gbuf_cont(mdata)) {
1077 gbuf_freem(gbuf_cont(mdata));
1078 gbuf_cont(mdata) = 0;
1079 }
1080 gbuf_rdec(mdata,TOTAL_ATP_HDR_SIZE);
1081 gbuf_wset(mdata,TOTAL_ATP_HDR_SIZE);
1082 if (awp->param2 != ASP_Version) {
1083 /*
1084 * bad version number, send the OpenSession response
1085 */
1086 awp->func = 0;
1087 awp->param1 = 0;
1088 awp->param2 = (unsigned short)ASPERR_BadVersNum;
1089 dPrintf(D_M_ASP, D_L_INFO,
1090 (" : version=%d\n",
1091 ASPERR_BadVersNum));
1092
1093 NET_NET(ddp->src_net, ddp->dst_net);
1094 ddp->src_node = ddp->dst_node;
1095 NET_ASSIGN(ddp->dst_net, rem_addr.net);
1096 ddp->dst_node = rem_addr.node;
1097 ddp->dst_socket = rem_addr.socket;
1098 atp_send_rsp(gref, mdata, FALSE);
1099 return;
1100 }
1101
1102 /*
1103 * queue the connection request
1104 */
1105 ATDISABLE(s, scb->lock);
1106 gbuf_next(mdata) = 0;
1107 if ((m = scb->sess_ioc) == 0) {
1108 scb->sess_ioc = mdata;
1109 if (scb->get_wait)
1110 wakeup(&scb->event);
1111 else
1112 atalk_notify_sel(gref);
1113 } else {
1114 while (gbuf_next(m))
1115 m = gbuf_next(m);
1116 gbuf_next(m) = mdata;
1117 }
1118 ATENABLE(s, scb->lock);
1119 dPrintf(D_M_ASP, D_L_INFO,
1120 (" : QUEUE connect request\n"));
1121
1122 return;
1123
1124 case ASPFUNC_Command:
1125 case ASPFUNC_Write:
1126 if ( (scb->sess_id != awp->param1)
1127 || (scb->rcv_seq_num != awp->param2)
1128 || BAD_REMADDR(rem_addr) ) {
1129 char era[8], ra[8];
1130 sprintf(era,"%d.%d", scb->rem_addr.node,scb->rem_addr.socket);
1131 sprintf(ra,"%d.%d", rem_addr.node,rem_addr.socket);
1132 dPrintf(D_M_ASP, D_L_WARNING,
1133 (" : DROP, id=%d,esn=%d,sn=%d,erem=%s,rem=%s\n",
1134 scb->sess_id,scb->rcv_seq_num,awp->param2,era,ra));
1135 gbuf_cont(mioc) = 0;
1136 gbuf_rdec(mdata,TOTAL_ATP_HDR_SIZE);
1137 atp_drop_req(gref, mdata);
1138 break;
1139 }
1140 scb->reply_socket = rem_addr.socket;
1141 if (awp->func == ASPFUNC_Write)
1142 scb->wrt_seq_num = scb->rcv_seq_num;
1143 NEXT_SEQ_NUM(scb->rcv_seq_num);
1144 gbuf_set_type(mioc, MSG_PROTO);
1145 gbuf_wset(mioc,sizeof(asp_command_ind_t));
1146 command_ind = (asp_command_ind_t *)gbuf_rptr(mioc);
1147 command_ind->Primitive = (int)awp->func;
1148 command_ind->ReqRefNum =
1149 *(unsigned short *)atp->tid;
1150 command_ind->ReqType = awp->func;
1151
1152 mdata = gbuf_strip(mdata);
1153 gbuf_cont(mioc) = mdata;
1154 ATDISABLE(s, scb->lock);
1155 if (scb->req_flag) {
1156 if ((mx = scb->req_msgq) != 0) {
1157 while (gbuf_next(mx))
1158 mx = gbuf_next(mx);
1159 gbuf_next(mx) = mioc;
1160 } else
1161 scb->req_msgq = mioc;
1162 ATENABLE(s, scb->lock);
1163 } else {
1164 scb->req_flag = 1;
1165 ATENABLE(s, scb->lock);
1166 asp_putnext(scb->gref, mioc);
1167 }
1168 goto l_done;
1169
1170 case ASPFUNC_WriteContinue:
1171 if ( (scb->sess_id != awp->param1)
1172 || (scb->snd_seq_num != awp->param2)
1173 || BAD_REMADDR(rem_addr) ) {
1174 break;
1175 }
1176 scb->reply_socket = rem_addr.socket;
1177 gbuf_set_type(mioc, MSG_PROTO);
1178 gbuf_wset(mioc,sizeof(asp_command_ind_t));
1179 command_ind = (asp_command_ind_t *)gbuf_rptr(mioc);
1180 command_ind->Primitive = (int)awp->func;
1181 command_ind->ReqRefNum =
1182 *(unsigned short *)atp->tid;
1183 command_ind->ReqType = awp->func;
1184
1185 mdata = gbuf_strip(mdata);
1186 gbuf_cont(mioc) = mdata;
1187 asp_putnext(scb->gref, mioc);
1188 goto l_done;
1189
1190 case ASPFUNC_Tickle:
1191 if (scb->stat_msg) {
1192 sess_scb = asp_scbQ[awp->param1];
1193 if (sess_scb && sess_scb->next_scb)
1194 sess_scb = asp_find_scb(
1195 sess_scb->loc_addr.socket, &rem_addr);
1196 if (sess_scb) {
1197 if (sess_scb->rem_addr.node)
1198 asp_untimout(asp_hangup, sess_scb);
1199 if (sess_scb->rem_addr.node)
1200 asp_timout(asp_hangup, sess_scb, sess_scb->session_timer);
1201 }
1202 }
1203 dPrintf(D_M_ASP, D_L_INFO,
1204 (" : Tickle, %d -> %d, id=%d\n",
1205 ddp->src_socket,ddp->dst_socket,awp->param1));
1206 break;
1207
1208 case ASPFUNC_CloseSess:
1209 if ( (scb->sess_id != awp->param1)
1210 || (scb->state == ASPSTATE_Close)
1211 || (scb->state == ASPSTATE_WaitingForCloseSessRsp)
1212 || (scb->rem_addr.net != rem_addr.net)
1213 || (scb->rem_addr.node != rem_addr.node) ) {
1214 dPrintf(D_M_ASP, D_L_INFO,
1215 (" : CLOSE retry, loc=%d, rem=%x.%x.%d\n",
1216 scb->loc_addr.socket,
1217 scb->rem_addr.net,
1218 scb->rem_addr.node,
1219 scb->rem_addr.socket));
1220
1221 break;
1222 }
1223 gbuf_freeb(mioc);
1224
1225 /*
1226 * build the CloseSess response to be sent to peer
1227 * when the session is closed by the user.
1228 */
1229 if (gbuf_cont(mdata)) {
1230 gbuf_freem(gbuf_cont(mdata));
1231 gbuf_cont(mdata) = 0;
1232 }
1233 gbuf_rdec(mdata,TOTAL_ATP_HDR_SIZE);
1234 gbuf_wset(mdata,TOTAL_ATP_HDR_SIZE);
1235 NET_NET(ddp->src_net, ddp->dst_net);
1236 ddp->src_node = ddp->dst_node;
1237 NET_ASSIGN(ddp->dst_net, rem_addr.net);
1238 ddp->dst_node = rem_addr.node;
1239 ddp->dst_socket = rem_addr.socket;
1240 awp->func = 0;
1241 awp->param1 = 0;
1242 awp->param2 = 0;
1243 dPrintf(D_M_ASP,D_L_INFO,
1244 (" : CLOSE, loc=%d, rem=%x.%x.%d\n",
1245 scb->loc_addr.socket,
1246 scb->rem_addr.net,
1247 scb->rem_addr.node,
1248 scb->rem_addr.socket));
1249
1250 gbuf_next(mdata) = 0;
1251 ATDISABLE(s, scb->lock);
1252 if (scb->sess_ioc)
1253 gbuf_freel(scb->sess_ioc);
1254 scb->sess_ioc = mdata;
1255 scb->state = ASPSTATE_Close;
1256 ATENABLE(s, scb->lock);
1257
1258 /*
1259 * notify upstream of the CloseSess from peer
1260 */
1261 asp_hangup(scb);
1262 return;
1263
1264 case ASPFUNC_Attention:
1265 if ( (scb->sess_id != awp->param1)
1266 || (scb->rem_addr.net != rem_addr.net)
1267 || (scb->rem_addr.node != rem_addr.node) ) {
1268 break;
1269 }
1270 gbuf_set_type(mioc, MSG_PROTO);
1271 gbuf_wset(mioc,sizeof(asp_command_ind_t));
1272 command_ind = (asp_command_ind_t *)gbuf_rptr(mioc);
1273 command_ind->Primitive = (int)awp->func;
1274 command_ind->ReqRefNum =
1275 *(unsigned short *)atp->tid;
1276 command_ind->ReqType = awp->func;
1277 scb->attn_tid = *(unsigned short *)atp->tid;
1278 scb->attn_flag = 1;
1279 gbuf_rdec(mdata,2); /* attention code */
1280
1281 mdata = gbuf_strip(mdata);
1282 gbuf_cont(mioc) = mdata;
1283 asp_putnext(scb->gref, mioc);
1284 goto l_done;
1285
1286 default:
1287 dPrintf(D_M_ASP, D_L_WARNING,
1288 (" : UNKNOWN func, func=%d\n",
1289 awp->func));
1290
1291 break;
1292 }
1293 }
1294
1295 else if (iocbp->ioc_cmd == AT_ATP_REQUEST_COMPLETE) {
1296 if (scb->next_scb) {
1297 /*
1298 * find the responsible scb
1299 */
1300 scb = (asp_scb_t *)iocbp->ioc_private;
1301 if ((scb == 0) || (scb->magic_num != 222)) {
1302 dPrintf(D_M_ASP, D_L_ERROR,
1303 ("asp_ack_reply: CAN'T find scb 1\n"));
1304 gbuf_freem(mioc);
1305 return;
1306 }
1307 }
1308 dPrintf(D_M_ASP, D_L_INFO,
1309 ("asp_ack_reply: RSP, loc=%d, rem=%x.%x.%d, state=%s\n",
1310 scb->loc_addr.socket,
1311 scb->rem_addr.net,
1312 scb->rem_addr.node,
1313 scb->rem_addr.socket,
1314 aspStateStr(scb->state)));
1315
1316 switch (scb->state) {
1317 case ASPSTATE_Close:
1318 case ASPSTATE_Idle:
1319 scb->rem_addr.node = 0;
1320 gbuf_freem(mioc);
1321 ATDISABLE(s, scb->lock);
1322 if (scb->get_wait)
1323 wakeup(&scb->event);
1324 else
1325 atalk_notify_sel(gref);
1326 ATENABLE(s, scb->lock);
1327 return;
1328
1329 case ASPSTATE_WaitingForGetStatusRsp:
1330 scb->ioc_wait = 0;
1331 scb->state = ASPSTATE_Idle;
1332 mx = gbuf_cont(mioc);
1333 gbuf_cont(mioc) = 0;
1334 mdata = gbuf_cont(mx);
1335 gbuf_cont(mx) = 0;
1336 iocbp->ioc_cmd = ASPIOC_GetStatus;
1337 iocbp->ioc_count = 0;
1338 iocbp->ioc_rval = mdata ? gbuf_msgsize(mdata) : 0;
1339 gbuf_freeb(mx);
1340 atalk_putnext(gref, mioc);
1341 atalk_putnext(gref, mdata);
1342 return;
1343
1344 case ASPSTATE_WaitingForOpenSessRsp:
1345 scb->ioc_wait = 0;
1346 scb->state = ASPSTATE_Idle;
1347 mx = gbuf_cont(mioc);
1348 gbuf_cont(mioc) = 0;
1349 if (gbuf_cont(mx)) {
1350 gbuf_freem(gbuf_cont(mx));
1351 gbuf_cont(mx) = 0;
1352 }
1353 iocbp->ioc_cmd = ASPIOC_OpenSession;
1354 iocbp->ioc_rval = 0;
1355 iocbp->ioc_count = 0;
1356 atpBDS = (struct atpBDS *)gbuf_rptr(mx);
1357 awp = (asp_word_t *)atpBDS->bdsUserData;
1358 if (awp->param2) {
1359 gbuf_freeb(mx);
1360 asp_iocnak(gref, mioc, ECONNREFUSED);
1361 } else {
1362 scb->rem_addr.node = scb->rem_node;
1363 scb->rem_addr.socket = awp->func;
1364 /* bms: need to set the reply_socket for client side too.
1365 This makes ALO atten replies sent by the client work. */
1366 scb->reply_socket = scb->rem_addr.socket;
1367 scb->sess_id = awp->param1;
1368 gbuf_freeb(mx);
1369 atalk_putnext(gref, mioc);
1370 asp_timout(asp_hangup, scb, scb->session_timer);
1371 asp_send_tickle(scb);
1372 dPrintf(D_M_ASP, D_L_INFO,
1373 ("asp_ack_reply: CONNECT, loc=%d, rem=%x.%x.%d\n",
1374 scb->loc_addr.socket,
1375 scb->rem_addr.net,
1376 scb->rem_addr.node,
1377 scb->rem_addr.socket));
1378 }
1379 return;
1380
1381 case ASPSTATE_WaitingForCommandRsp:
1382 case ASPSTATE_WaitingForWriteRsp:
1383 case ASPSTATE_WaitingForWriteContinueRsp:
1384 if (scb->rem_addr.node)
1385 asp_untimout(asp_hangup, scb);
1386 NEXT_SEQ_NUM(scb->snd_seq_num);
1387 scb->state = ASPSTATE_Idle;
1388 gbuf_set_type(mioc, MSG_PROTO);
1389 mx = gbuf_cont(mioc);
1390 mdata = gbuf_cont(mx);
1391 gbuf_cont(mioc) = mdata;
1392 atpBDS = (struct atpBDS *)gbuf_rptr(mx);
1393 cmdreply_ind = (asp_cmdreply_ind_t *)gbuf_rptr(mioc);
1394 cmdreply_ind->Primitive = ASPFUNC_CmdReply;
1395 cmdreply_ind->CmdResult = *(int *)atpBDS->bdsUserData;
1396 gbuf_wset(mioc,sizeof(asp_cmdreply_ind_t));
1397 gbuf_freeb(mx);
1398 asp_putnext(scb->gref, mioc);
1399 goto l_done;
1400
1401 case ASPSTATE_WaitingForCloseSessRsp:
1402 scb->ioc_wait = 0;
1403 scb->state = ASPSTATE_Close;
1404 scb->rem_addr.node = 0;
1405 iocbp->ioc_cmd = ASPIOC_CloseSession;
1406 iocbp->ioc_rval = 0;
1407 if (gbuf_cont(mioc)) {
1408 gbuf_freem(gbuf_cont(mioc));
1409 gbuf_cont(mioc) = 0;
1410 }
1411 atalk_putnext(scb->gref, mioc);
1412 atp_cancel_req(scb->gref, (unsigned int)scb->tickle_tid);
1413 scb->tickle_tid = 0;
1414 return;
1415
1416 default:
1417 dPrintf(D_M_ASP, D_L_WARNING,
1418 (" : UNKNOWN state, state=%s\n",
1419 aspStateStr(scb->state)));
1420 break;
1421 }
1422 }
1423
1424 else {
1425 if (scb->next_scb) {
1426 /*
1427 * find the responsible scb
1428 */
1429 scb = (asp_scb_t *)iocbp->ioc_cr;
1430 if ((scb == 0) || (scb->magic_num != 222)) {
1431 dPrintf(D_M_ASP, D_L_ERROR,
1432 ("asp_ack_reply: CAN'T find scb 2\n"));
1433 gbuf_freem(mioc);
1434 return;
1435 }
1436 }
1437
1438 switch (scb->state) {
1439 case ASPSTATE_Close:
1440 scb->rem_addr.node = 0;
1441 break;
1442 }
1443 }
1444
1445 if (mioc != 0)
1446 gbuf_freem(mioc);
1447
1448 l_done:
1449 if (scb->rem_addr.node)
1450 asp_timout(asp_hangup, scb, scb->session_timer);
1451 } /* asp_ack_reply */
1452
1453 /*
1454 * NAK reply routine
1455 */
1456 void
1457 asp_nak_reply(gref, mioc)
1458 register gref_t *gref;
1459 register gbuf_t *mioc;
1460 {
1461 register asp_scb_t *scb;
1462 register ioc_t *iocbp;
1463
1464 iocbp = (ioc_t *)gbuf_rptr(mioc);
1465
1466 if (iocbp->ioc_cmd == AT_ATP_ISSUE_REQUEST_TICKLE) {
1467 /*
1468 * no tickle, close session
1469 */
1470 scb = (asp_scb_t *)iocbp->ioc_cr;
1471 gbuf_freem(mioc);
1472 asp_hangup(scb);
1473 dPrintf(D_M_ASP, D_L_WARNING,
1474 ("tickle_nak: loc=%d, rem=%x.%x.%d, state=%s\n",
1475 scb->loc_addr.socket,
1476 scb->rem_addr.net,
1477 scb->rem_addr.node,
1478 scb->rem_addr.socket,
1479 aspStateStr(scb->state)));
1480
1481 return;
1482 }
1483
1484 scb = (asp_scb_t *)gref->info;
1485 if (scb == 0) {
1486 gbuf_freem(mioc);
1487 return;
1488 }
1489
1490 if (iocbp->ioc_cmd == AT_ATP_REQUEST_COMPLETE) {
1491 if (scb->next_scb) {
1492 /*
1493 * find the responsible scb
1494 */
1495 scb = (asp_scb_t *)iocbp->ioc_private;
1496 if ((scb == 0) || (scb->magic_num != 222)) {
1497 dPrintf(D_M_ASP, D_L_ERROR,
1498 ("asp_nak_reply: CAN'T find scb 1\n"));
1499 gbuf_freem(mioc);
1500 return;
1501 }
1502 }
1503 dPrintf(D_M_ASP, D_L_WARNING,
1504 ("asp_nak_reply: RSP, loc=%d, rem=%x.%x.%d, state=%s\n",
1505 scb->loc_addr.socket,
1506 scb->rem_addr.net,
1507 scb->rem_addr.node,
1508 scb->rem_addr.socket,
1509 aspStateStr(scb->state)));
1510
1511 switch (scb->state) {
1512 case ASPSTATE_WaitingForGetStatusRsp:
1513 iocbp->ioc_cmd = ASPIOC_GetStatus;
1514 break;
1515
1516 case ASPSTATE_WaitingForOpenSessRsp:
1517 iocbp->ioc_cmd = ASPIOC_OpenSession;
1518 break;
1519
1520 case ASPSTATE_WaitingForCommandRsp:
1521 case ASPSTATE_WaitingForWriteRsp:
1522 case ASPSTATE_WaitingForWriteContinueRsp:
1523 scb->state = ASPSTATE_Idle;
1524
1525 /* last remaining use of MSG_ERROR */
1526 gbuf_set_type(mioc, MSG_ERROR);
1527 *gbuf_rptr(mioc) = (u_char)EPROTO;
1528 gbuf_wset(mioc, 1);
1529 if (gbuf_cont(mioc)) {
1530 gbuf_freem(gbuf_cont(mioc));
1531 gbuf_cont(mioc) = 0;
1532 }
1533
1534 asp_putnext(scb->gref, mioc);
1535 return;
1536
1537 case ASPSTATE_WaitingForCloseSessRsp:
1538 scb->state = ASPSTATE_Close;
1539 /* fall through */
1540 case ASPSTATE_Close: /* new for PR-2296832 */
1541 scb->rem_addr.node = 0;
1542 iocbp->ioc_cmd = ASPIOC_CloseSession;
1543 iocbp->ioc_rval = 0;
1544 if (gbuf_cont(mioc)) {
1545 gbuf_freem(gbuf_cont(mioc));
1546 gbuf_cont(mioc) = 0;
1547 }
1548 gbuf_set_type(mioc, MSG_IOCACK);
1549 atalk_putnext(scb->gref, mioc);
1550 return;
1551
1552 default:
1553 gbuf_freem(mioc);
1554 return;
1555 }
1556 scb->state = ASPSTATE_Idle;
1557 atalk_putnext(gref, mioc);
1558 }
1559
1560 else {
1561 if (scb->next_scb) {
1562 /*
1563 * find the responsible scb
1564 */
1565 scb = (asp_scb_t *)iocbp->ioc_cr;
1566 if ((scb == 0) || (scb->magic_num != 222)) {
1567 dPrintf(D_M_ASP, D_L_ERROR,
1568 ("asp_nak_reply: CAN'T find scb 2\n"));
1569 gbuf_freem(mioc);
1570 return;
1571 }
1572 }
1573
1574 switch (scb->state) {
1575 case ASPSTATE_Close:
1576 scb->rem_addr.node = 0;
1577 break;
1578 }
1579
1580 gbuf_freem(mioc);
1581 }
1582 } /* asp_nak_reply */
1583
1584 /*
1585 * delete scb from the use list
1586 */
1587 StaticProc void
1588 asp_dequeue_scb(scb)
1589 asp_scb_t *scb;
1590 {
1591 int s;
1592
1593 ATDISABLE(s, aspall_lock);
1594 if (scb == scb_used_list) {
1595 if ((scb_used_list = scb->next_scb) != 0)
1596 scb->next_scb->prev_scb = 0;
1597 } else {
1598 if ((scb->prev_scb->next_scb = scb->next_scb) != 0)
1599 scb->next_scb->prev_scb = scb->prev_scb;
1600 }
1601 ATENABLE(s, aspall_lock);
1602
1603 scb->next_scb = 0;
1604 scb->prev_scb = 0;
1605 }
1606
1607 /*
1608 * find scb routine
1609 */
1610 StaticProc asp_scb_t *
1611 asp_find_scb(sock_num, rem_addr)
1612 unsigned char sock_num;
1613 at_inet_t *rem_addr;
1614 {
1615 int s;
1616 asp_scb_t *scb;
1617 asp_scb_t *alt_scb = 0;
1618
1619 ATDISABLE(s, aspall_lock);
1620 for (scb = asp_scbQ[sock_num]; scb; scb = scb->next_scb) {
1621 if ((scb->rem_addr.net == rem_addr->net)
1622 && (scb->rem_addr.node == rem_addr->node)) {
1623 if ((scb->rem_addr.socket == rem_addr->socket)
1624 || (scb->rem_socket == rem_addr->socket))
1625 break;
1626 else if (alt_scb == 0)
1627 alt_scb = scb;
1628 }
1629 }
1630
1631 if ((scb == 0) && ((scb = alt_scb) == 0)) {
1632 dPrintf(D_M_ASP, D_L_ERROR,
1633 ("asp_find_scb: CAN'T find scb, loc=%d, rem=%x.%x.%d\n",
1634 sock_num,
1635 rem_addr->net,
1636 rem_addr->node,
1637 rem_addr->socket));
1638 }
1639 ATENABLE(s, aspall_lock);
1640
1641 return scb;
1642 }
1643
1644 /*
1645 * timout routine
1646 */
1647 StaticProc void
1648 asp_timout(func, scb, seconds)
1649 void (*func)();
1650 register asp_scb_t *scb;
1651 int seconds;
1652 {
1653 int s;
1654 unsigned char sum;
1655 register asp_scb_t *curr_scb, *prev_scb;
1656
1657 if (scb->tmo_func)
1658 return;
1659
1660 scb->tmo_func = func;
1661 scb->tmo_delta = (seconds>>SESS_TMO_RES);
1662 scb->tmo_cnt = scb_tmo_cnt;
1663
1664 ATDISABLE(s, asptmo_lock);
1665 if (scb_tmo_list == 0) {
1666 scb->next_tmo = scb->prev_tmo = 0;
1667 scb_tmo_list = scb;
1668 ATENABLE(s, asptmo_lock);
1669 return;
1670 }
1671
1672 prev_scb = 0;
1673 curr_scb = scb_tmo_list;
1674 sum = 0;
1675
1676 while (1) {
1677 sum += curr_scb->tmo_delta;
1678 if (sum > scb->tmo_delta) {
1679 sum -= curr_scb->tmo_delta;
1680 scb->tmo_delta -= sum;
1681 curr_scb->tmo_delta -= scb->tmo_delta;
1682 break;
1683 }
1684 prev_scb = curr_scb;
1685 if ((curr_scb = curr_scb->next_tmo) == 0) {
1686 scb->tmo_delta -= sum;
1687 break;
1688 }
1689 }
1690
1691 if (prev_scb) {
1692 scb->prev_tmo = prev_scb;
1693 if ((scb->next_tmo = prev_scb->next_tmo) != 0)
1694 prev_scb->next_tmo->prev_tmo = scb;
1695 prev_scb->next_tmo = scb;
1696 } else {
1697 scb->prev_tmo = 0;
1698 scb->next_tmo = scb_tmo_list;
1699 scb_tmo_list->prev_tmo = scb;
1700 scb_tmo_list = scb;
1701 }
1702 ATENABLE(s, asptmo_lock);
1703 }
1704
1705 /*
1706 * untimout routine
1707 */
1708 StaticProc void
1709 asp_untimout(func, scb)
1710 void (*func)();
1711 register asp_scb_t *scb;
1712 {
1713 int s;
1714
1715 if ((scb->tmo_cnt == scb_tmo_cnt) || (scb->tmo_func == 0))
1716 return;
1717
1718 ATDISABLE(s, asptmo_lock);
1719 if (scb_tmo_list == scb) {
1720 if ((scb_tmo_list = scb->next_tmo) != 0) {
1721 scb_tmo_list->prev_tmo = 0;
1722 scb->next_tmo->tmo_delta += scb->tmo_delta;
1723 }
1724 } else if (scb->prev_tmo) {
1725 if ((scb->prev_tmo->next_tmo = scb->next_tmo) != 0) {
1726 scb->next_tmo->prev_tmo = scb->prev_tmo;
1727 scb->next_tmo->tmo_delta += scb->tmo_delta;
1728 }
1729 scb->prev_tmo = 0;
1730 }
1731 scb->tmo_func = 0;
1732 ATENABLE(s, asptmo_lock);
1733 }
1734
1735 /*
1736 * hangup routine
1737 */
1738 StaticProc void
1739 asp_hangup(scb)
1740 asp_scb_t *scb;
1741 {
1742 int s;
1743
1744 /*
1745 * set the state to Close
1746 */
1747 ATDISABLE(s, scb->lock);
1748 scb->state = ASPSTATE_Close;
1749 if (scb->tickle_tid) {
1750 atp_cancel_req(scb->gref, (unsigned int)scb->tickle_tid);
1751 scb->tickle_tid = 0;
1752 }
1753
1754 /*
1755 * notify upstream of the hangup
1756 */
1757 if (scb->rem_addr.node) {
1758 if (scb->get_wait) {
1759 wakeup(&scb->event);
1760 ATENABLE(s, scb->lock);
1761 } else {
1762 ATENABLE(s, scb->lock);
1763 atalk_notify_sel(scb->gref);
1764 }
1765 } else
1766 ATENABLE(s, scb->lock);
1767 }
1768
1769 StaticProc void
1770 asp_iocack(gref, mioc)
1771 gref_t *gref;
1772 gbuf_t *mioc;
1773 {
1774 if (gbuf_cont(mioc))
1775 ((ioc_t *)gbuf_rptr(mioc))->ioc_count = gbuf_msgsize(gbuf_cont(mioc));
1776 else
1777 ((ioc_t *)gbuf_rptr(mioc))->ioc_count = 0;
1778
1779 gbuf_set_type(mioc, MSG_IOCACK);
1780 atalk_putnext(gref, mioc);
1781 }
1782
1783 StaticProc void
1784 asp_iocnak(gref, mioc, err)
1785 gref_t *gref;
1786 gbuf_t *mioc;
1787 int err;
1788 {
1789 ((ioc_t *)gbuf_rptr(mioc))->ioc_count = 0;
1790 if (err == 0)
1791 err = ENXIO;
1792 ((ioc_t *)gbuf_rptr(mioc))->ioc_error = err;
1793 ((ioc_t *)gbuf_rptr(mioc))->ioc_rval = -1;
1794 if (gbuf_cont(mioc)) {
1795 gbuf_freem(gbuf_cont(mioc));
1796 gbuf_cont(mioc) = 0;
1797 }
1798
1799 gbuf_set_type(mioc, MSG_IOCNAK);
1800 atalk_putnext(gref, mioc);
1801 }
1802
1803 /*
1804 * the alloc scb routine
1805 */
1806 StaticProc asp_scb_t *
1807 asp_scb_alloc()
1808 {
1809 int s, i;
1810 gbuf_t *m;
1811 asp_scb_t *scb, *scb_array;
1812
1813 ATDISABLE(s, aspall_lock);
1814 if (scb_free_list == 0) {
1815 if ((m = gbuf_alloc(SCBS_PER_BLK*sizeof(asp_scb_t), PRI_MED)) == 0)
1816 {
1817 ATENABLE(s, aspall_lock);
1818 return (asp_scb_t *)0;
1819 }
1820 bzero((char *)gbuf_rptr(m), SCBS_PER_BLK*sizeof(asp_scb_t));
1821 gbuf_cont(m) = scb_resource_m;
1822 scb_resource_m = m;
1823 scb_array = (asp_scb_t *)gbuf_rptr(m);
1824 for (i=0; i < SCBS_PER_BLK-1; i++)
1825 scb_array[i].next_scb = (asp_scb_t *)&scb_array[i+1];
1826 scb_array[i].next_scb = 0;
1827 scb_free_list = (asp_scb_t *)&scb_array[0];
1828 }
1829
1830 scb = scb_free_list;
1831 scb_free_list = scb->next_scb;
1832 ATENABLE(s, aspall_lock);
1833 ATLOCKINIT(scb->lock);
1834 ATLOCKINIT(scb->delay_lock);
1835 ATEVENTINIT(scb->event);
1836 ATEVENTINIT(scb->delay_event);
1837
1838 return scb;
1839 }
1840
1841 /*
1842 * the free scb routine
1843 */
1844 StaticProc void
1845 asp_scb_free(scb)
1846 asp_scb_t *scb;
1847 {
1848 int s;
1849
1850 bzero((char *)scb, sizeof(asp_scb_t));
1851 ATDISABLE(s, aspall_lock);
1852 scb->next_scb = scb_free_list;
1853 scb_free_list = scb;
1854 ATENABLE(s, aspall_lock);
1855 }
1856
1857 /*
1858 * routine to pass up receive data
1859 */
1860 StaticProc void
1861 asp_putnext(gref, mproto)
1862 gref_t *gref;
1863 gbuf_t *mproto;
1864 {
1865 int s;
1866 gbuf_t *m;
1867 asp_scb_t *scb;
1868
1869 scb = (asp_scb_t *)gref->info;
1870
1871 /*
1872 * queue the message.
1873 */
1874 ATDISABLE(s, scb->lock);
1875 gbuf_next(mproto) = 0;
1876 if ((m = scb->sess_ioc) == 0)
1877 scb->sess_ioc = mproto;
1878 else {
1879 while (gbuf_next(m))
1880 m = gbuf_next(m);
1881 gbuf_next(m) = mproto;
1882 }
1883 scb->rcv_cnt++;
1884 if (scb->rcv_cnt >= MAX_RCV_CNT)
1885 scb->snd_stop = 1;
1886
1887 if (scb->get_wait) {
1888 wakeup(&scb->event);
1889 ATENABLE(s, scb->lock);
1890 } else if (mproto == scb->sess_ioc) {
1891 ATENABLE(s, scb->lock);
1892 atalk_notify_sel(gref);
1893 } else
1894 ATENABLE(s, scb->lock);
1895 } /* asp_putnext */
1896
1897 /*
1898 * The following two routines are direct entries from system
1899 * calls to allow fast sending and recving of ASP data.
1900 */
1901
1902 /* in ASPputmsg we expect:
1903
1904 ASPFUNC_CmdReply
1905 ASPFUNC_Attention
1906 ASPFUNC_Command
1907 ASPFUNC_Write
1908 ASPFUNC_WriteContinue
1909
1910 bms: Make this callable from the kernel.
1911 If mreq != NULL, then must be called from kernel space and the following apply:
1912 1) *mreq is data to be sent already in mbuf chains.
1913 2) datptr->len = size of data
1914 */
1915
1916 int ASPputmsg(gref_t *gref, strbuf_t *ctlptr, strbuf_t *datptr, gbuf_t *mreq, int flags, int *errp)
1917 {
1918 int s, i, err, len;
1919 gbuf_t *mioc, *mdata, *mx;
1920 ioc_t *iocbp;
1921 strbuf_t ctlbuf;
1922 strbuf_t datbuf;
1923 asp_scb_t *scb;
1924 int nbds, result, msize, Primitive;
1925 unsigned char *wptr;
1926 struct atp_set_default *sd;
1927 at_ddp_t *ddp;
1928 at_atp_t *atp;
1929 struct atpBDS *atpBDS;
1930 asp_word_t *awp;
1931 union asp_primitives *primitives;
1932 unsigned short tid;
1933
1934 if ((scb = (asp_scb_t *)gref->info) == 0) {
1935 dPrintf(D_M_ASP, D_L_ERROR,
1936 ("ASPputmsg: stale handle=0x%x, pid=%d\n",
1937 (u_int) gref, gref->pid));
1938
1939 *errp = EINVAL;
1940 return -1;
1941 }
1942
1943 if (scb->state == ASPSTATE_Close)
1944 return 0;
1945 if (scb->snd_stop) {
1946 *errp = EAGAIN;
1947 return -1;
1948 }
1949
1950 /*
1951 * copy in the control and data info
1952 */
1953 if (mreq != NULL) {
1954 /* being called from kernel space */
1955 bcopy (ctlptr, &ctlbuf, sizeof (strbuf_t));
1956 bcopy (datptr, &datbuf, sizeof (strbuf_t));
1957 } else {
1958 /* being called from user space */
1959 if ((err = copyin((caddr_t)ctlptr, (caddr_t)&ctlbuf, sizeof(ctlbuf))) != 0)
1960 goto l_err;
1961 if ((err = copyin((caddr_t)datptr, (caddr_t)&datbuf, sizeof(datbuf))) != 0)
1962 goto l_err;
1963 }
1964
1965 /*
1966 * allocate buffer and copy in the control content
1967 */
1968 if (!(mioc = gbuf_alloc_wait(ctlbuf.len, TRUE))) {
1969 /* error return should not be possible */
1970 err = ENOBUFS;
1971 goto l_err;
1972 }
1973 gbuf_set_type(mioc, MSG_IOCTL); /* for later, in ATP */
1974 gbuf_wset(mioc, ctlbuf.len);
1975
1976 if (mreq != NULL) {
1977 /* being called from kernel space */
1978 bcopy (ctlbuf.buf, gbuf_rptr(mioc), ctlbuf.len);
1979 } else {
1980 /* being called from user space */
1981 if ((err = copyin((caddr_t)ctlbuf.buf, (caddr_t)gbuf_rptr(mioc), ctlbuf.len)) != 0) {
1982 gbuf_freem(mioc);
1983 goto l_err;
1984 }
1985 }
1986
1987 iocbp = (ioc_t *)gbuf_rptr(mioc);
1988 primitives = (union asp_primitives *)gbuf_rptr(mioc);
1989 Primitive = primitives->Primitive;
1990 dPrintf(D_M_ASP, D_L_INFO,
1991 ("ASPputmsg: %s\n", aspCmdStr(Primitive)));
1992
1993 /*
1994 * allocate buffer and copy in the data content
1995 */
1996 len = (Primitive == ASPFUNC_CmdReply) ? 0 : aspCMDsize;
1997
1998 if (!(mdata = gbuf_alloc_wait(datbuf.len+len, TRUE))) {
1999 /* error return should not be possible */
2000 err = ENOBUFS;
2001 gbuf_freem(mioc);
2002 goto l_err;
2003 }
2004 gbuf_wset(mdata, (datbuf.len+len));
2005 gbuf_cont(mioc) = mdata;
2006
2007 if (mreq != NULL) {
2008 /* being called from kernel space */
2009 gbuf_t *tmp = mreq;
2010 unsigned long offset = 0;
2011
2012 /* copy afp cmd data from the passed in mbufs to mdata. I cant
2013 chain mreq to mdata since the rest of this code assumes
2014 just one big mbuf with space in front for the BDS */
2015 offset = len;
2016 while (tmp != NULL) {
2017 bcopy (gbuf_rptr(tmp), (gbuf_rptr(mdata) + offset), gbuf_len(tmp));
2018 offset += gbuf_len(tmp);
2019 tmp = gbuf_cont(tmp); /* on to next mbuf in chain */
2020 }
2021
2022 /* all data copied out of mreq so free it */
2023 gbuf_freem(mreq);
2024 } else {
2025 /* being called from user space */
2026 if ((err = copyin((caddr_t)datbuf.buf,
2027 (caddr_t)(gbuf_rptr(mdata)+len), datbuf.len)) != 0) {
2028 gbuf_freem(mioc);
2029 goto l_err;
2030 }
2031 }
2032
2033 switch (Primitive) {
2034
2035 case ASPFUNC_Command:
2036 case ASPFUNC_Write:
2037 case ASPFUNC_WriteContinue:
2038 case ASPFUNC_Attention:
2039 /*
2040 * build the command/write/write_continue request
2041 */
2042 wptr = gbuf_rptr(mdata);
2043 atpBDS = (struct atpBDS *)wptr;
2044 wptr += atpBDSsize;
2045 for (i=0; i < ATP_TRESP_MAX; i++) {
2046 *(unsigned long *)atpBDS[i].bdsBuffAddr = 1;
2047 *(unsigned short *)atpBDS[i].bdsBuffSz = ATP_DATA_SIZE;
2048 }
2049 sd = (struct atp_set_default *)wptr;
2050 wptr += sizeof(struct atp_set_default);
2051 sd->def_retries = (scb->cmd_retry.retries == -1) ?
2052 ATP_INFINITE_RETRIES : scb->cmd_retry.retries;
2053 sd->def_rate = scb->cmd_retry.interval*TICKS_PER_SEC;
2054 sd->def_BDSlen = atpBDSsize;
2055 ddp = (at_ddp_t *)wptr;
2056 NET_ASSIGN(ddp->src_net, scb->loc_addr.net);
2057 ddp->src_node = scb->loc_addr.node;
2058 NET_ASSIGN(ddp->dst_net, scb->rem_addr.net);
2059 ddp->dst_node = scb->rem_addr.node;
2060 ddp->dst_socket = scb->rem_addr.socket;
2061 UAS_ASSIGN(ddp->checksum, 0);
2062 atp = ATP_ATP_HDR(wptr);
2063 wptr += TOTAL_ATP_HDR_SIZE;
2064 atp->xo = 1;
2065 atp->xo_relt = 1;
2066 atp->bitmap = 0xff;
2067 awp = (asp_word_t *)atp->user_bytes;
2068 awp->func = (unsigned char)Primitive;
2069 awp->param1 = scb->sess_id;
2070 awp->param2 = scb->snd_seq_num;
2071 iocbp->ioc_private = (void *)scb;
2072 iocbp->ioc_count = gbuf_len(mdata);
2073 iocbp->ioc_rval = 0;
2074 iocbp->ioc_cmd = AT_ATP_ISSUE_REQUEST_DEF;
2075
2076 /*
2077 * send the command/write/write_continue/attention request
2078 */
2079 ATDISABLE(s, scb->lock);
2080 switch (awp->func) {
2081 case ASPFUNC_Command:
2082 scb->state = ASPSTATE_WaitingForCommandRsp;
2083 break;
2084 case ASPFUNC_Write:
2085 scb->state = ASPSTATE_WaitingForWriteRsp;
2086 break;
2087 case ASPFUNC_WriteContinue:
2088 scb->state = ASPSTATE_WaitingForWriteContinueRsp;
2089 awp->param2 = scb->wrt_seq_num;
2090 break;
2091 case ASPFUNC_Attention:
2092 scb->state = ASPSTATE_WaitingForCommandRsp;
2093 atp->xo = 0;
2094 atp->xo_relt = 0;
2095 atp->bitmap = 0x01;
2096 gbuf_wdec(mdata,2);
2097 awp->param2 = *(unsigned short *)gbuf_wptr(mdata);
2098 break;
2099 }
2100 ATENABLE(s, scb->lock);
2101 dPrintf(D_M_ASP,D_L_INFO,
2102 ("ASPputmsg: %s, loc=%d, rem=%x.%x.%d\n",
2103 (awp->func == ASPFUNC_Command ? "CommandReq" :
2104 awp->func == ASPFUNC_Write ? "WriteReq" :
2105 awp->func == ASPFUNC_WriteContinue ? "WriteContinue" :
2106 "AttentionReq"),scb->loc_addr.socket,
2107 NET_VALUE(ddp->dst_net),ddp->dst_node,ddp->dst_socket));
2108 atp_send_req(gref, mioc);
2109 return 0;
2110
2111 case ASPFUNC_CmdReply:
2112
2113 ATDISABLE(s, scb->lock);
2114 if (scb->req_msgq) {
2115 mx = scb->req_msgq;
2116 scb->req_msgq = gbuf_next(mx);
2117 gbuf_next(mx) = 0;
2118 ATENABLE(s, scb->lock);
2119 asp_putnext(scb->gref, mx);
2120 } else {
2121 scb->req_flag = 0;
2122 ATENABLE(s, scb->lock);
2123 }
2124 result = primitives->CmdReplyReq.CmdResult;
2125 tid = primitives->CmdReplyReq.ReqRefNum;
2126
2127 /* Re-use the original mioc mbuf to send the response. */
2128 gbuf_rinc(mioc,sizeof(void *));
2129 gbuf_wset(mioc,0);
2130 ddp = (at_ddp_t *)gbuf_wptr(mioc);
2131 gbuf_winc(mioc,DDP_X_HDR_SIZE);
2132 atp = (at_atp_t *)gbuf_wptr(mioc);
2133 gbuf_winc(mioc,ATP_HDR_SIZE);
2134 NET_ASSIGN(ddp->src_net, scb->loc_addr.net);
2135 ddp->src_node = scb->loc_addr.node;
2136 NET_ASSIGN(ddp->dst_net, scb->rem_addr.net);
2137 ddp->dst_node = scb->rem_addr.node;
2138 ddp->dst_socket = scb->reply_socket;
2139 ddp->type = DDP_ATP;
2140 UAS_ASSIGN(ddp->checksum, 0);
2141 UAS_ASSIGN(atp->tid, tid);
2142 if (scb->attn_flag && (tid == scb->attn_tid)) {
2143 scb->attn_flag = 0;
2144 atp->xo = 0;
2145 atp->xo_relt = 0;
2146 } else {
2147 atp->xo = 1;
2148 atp->xo_relt = 1;
2149 }
2150 atpBDS = (struct atpBDS *)gbuf_wptr(mioc);
2151 msize = mdata ? gbuf_msgsize(mdata) : 0;
2152 for (nbds=0; (nbds < ATP_TRESP_MAX) && (msize > 0); nbds++) {
2153 len = msize < ATP_DATA_SIZE ? msize : ATP_DATA_SIZE;
2154 msize -= ATP_DATA_SIZE;
2155 *(long *)atpBDS[nbds].bdsUserData = 0;
2156 UAL_ASSIGN(atpBDS[nbds].bdsBuffAddr, 1);
2157 UAS_ASSIGN(atpBDS[nbds].bdsBuffSz, len);
2158 }
2159 UAS_ASSIGN(atpBDS[0].bdsDataSz, nbds);
2160 *(long *)atpBDS[0].bdsUserData = (long)result;
2161 *(long *)atp->user_bytes = (long)result;
2162 gbuf_winc(mioc,atpBDSsize);
2163 dPrintf(D_M_ASP, D_L_INFO,
2164 ("ASPputmsg: ATP CmdReplyReq, loc=%d, state=%s, msgsize = %d, result = %d, tid = %d\n",
2165 scb->loc_addr.socket, aspStateStr(scb->state),
2166 (mdata ? gbuf_msgsize(mdata) : 0), result, tid));
2167 atp_send_rsp(gref, mioc, TRUE);
2168 return 0;
2169 }
2170
2171 /* Not an expected ASPFUNC */
2172 gbuf_freem(mioc);
2173 err = EOPNOTSUPP;
2174
2175 l_err:
2176 *errp = err;
2177 return -1;
2178 } /* ASPputmsg */
2179
2180
2181 /* bms: make this callable from kernel. reply date is passed back as a mbuf chain in *mreply */
2182 int ASPgetmsg(gref_t *gref, strbuf_t *ctlptr, strbuf_t *datptr, gbuf_t **mreply, int *flags, int *errp)
2183 {
2184 int err, s, len, sum, rval;
2185 gbuf_t *mproto, *mdata;
2186 strbuf_t ctlbuf;
2187 strbuf_t datbuf;
2188 asp_scb_t *scb;
2189 unsigned char get_wait;
2190
2191 if ((scb = (asp_scb_t *)gref->info) == 0) {
2192 dPrintf(D_M_ASP, D_L_ERROR,
2193 ("ASPgetmsg: stale handle=0x%x, pid=%d\n",
2194 (u_int) gref, gref->pid));
2195
2196 *errp = EINVAL;
2197 return -1;
2198 }
2199
2200 ATDISABLE(s, scb->lock);
2201 if (scb->state == ASPSTATE_Close) {
2202 ATENABLE(s, scb->lock);
2203 return 0;
2204 }
2205
2206 /*
2207 * get receive data
2208 */
2209 while ((mproto = scb->sess_ioc) == 0) {
2210 scb->get_wait = 1;
2211 err = tsleep(&scb->event, PSOCK | PCATCH, "aspgetmsg", 0);
2212 if (err != 0) {
2213 scb->get_wait = 0;
2214 ATENABLE(s, scb->lock);
2215 *errp = err;
2216 return -1;
2217 }
2218 if (scb->state == ASPSTATE_Close) {
2219 scb->get_wait = 0;
2220 ATENABLE(s, scb->lock);
2221 return 0;
2222 }
2223 }
2224 get_wait = scb->get_wait;
2225 scb->get_wait = 0;
2226 if ((ctlptr == 0) && (datptr == 0)) {
2227 ATENABLE(s, scb->lock);
2228 return 0;
2229 }
2230 scb->sess_ioc = gbuf_next(mproto);
2231 mdata = gbuf_cont(mproto);
2232 ATENABLE(s, scb->lock);
2233
2234 /* last remaining use of MSG_ERROR */
2235 if (gbuf_type(mproto) == MSG_ERROR) {
2236 err = (int)gbuf_rptr(mproto)[0];
2237 goto l_err;
2238 }
2239
2240 /*
2241 * copy in the control and data info
2242 */
2243 if (mreply != NULL) {
2244 /* called from kernel space */
2245 bcopy (ctlptr, &ctlbuf, sizeof(ctlbuf));
2246 bcopy (datptr, &datbuf, sizeof(datbuf));
2247 } else {
2248 /* called from user space */
2249 if ((err = copyin((caddr_t)ctlptr,
2250 (caddr_t)&ctlbuf, sizeof(ctlbuf))) != 0)
2251 goto l_err;
2252 if ((err = copyin((caddr_t)datptr,
2253 (caddr_t)&datbuf, sizeof(datbuf))) != 0)
2254 goto l_err;
2255 }
2256 if ((datbuf.maxlen < 0) || (datbuf.maxlen < gbuf_msgsize(mdata))) {
2257 ATDISABLE(s, scb->lock);
2258 gbuf_next(mproto) = scb->sess_ioc;
2259 scb->sess_ioc = mproto;
2260 ATENABLE(s, scb->lock);
2261 return MOREDATA;
2262 }
2263
2264 if (get_wait == 0) {
2265 /*
2266 * this is a hack to support the select() call.
2267 * we're not supposed to dequeue messages in the Streams
2268 * head's read queue this way; but there is no better way.
2269 */
2270 ATDISABLE(s, scb->lock);
2271 if (scb->sess_ioc == 0) {
2272 ATENABLE(s, scb->lock);
2273 } else {
2274 ATENABLE(s, scb->lock);
2275 atalk_notify_sel(gref);
2276 }
2277 }
2278
2279 /*
2280 * copy out the control content and info
2281 */
2282 ctlbuf.len = gbuf_len(mproto);
2283
2284 if (mreply != NULL) {
2285 /* called from kernel space */
2286 bcopy (gbuf_rptr(mproto), ctlbuf.buf, ctlbuf.len);
2287 bcopy (&ctlbuf, ctlptr, sizeof(ctlbuf));
2288 } else {
2289 /* called from user space */
2290 if ((err = copyout((caddr_t)gbuf_rptr(mproto),
2291 (caddr_t)ctlbuf.buf, ctlbuf.len)) != 0)
2292 goto l_err;
2293 if ((err = copyout((caddr_t)&ctlbuf,
2294 (caddr_t)ctlptr, sizeof(ctlbuf))) != 0)
2295 goto l_err;
2296 }
2297
2298 /*
2299 * copy out the data content and info
2300 */
2301 for (rval = 0, sum = 0; mdata && (rval == 0); mdata = gbuf_cont(mdata))
2302 {
2303 len = gbuf_len(mdata);
2304 if (len) {
2305 if ((len + sum) > datbuf.maxlen) {
2306 len = datbuf.maxlen - sum;
2307 rval = MOREDATA;
2308 }
2309
2310 if (mreply == NULL) {
2311 /* called from user space */
2312 if ((err = copyout((caddr_t)gbuf_rptr(mdata), (caddr_t)&datbuf.buf[sum], len)) != 0)
2313 goto l_err;
2314 }
2315 sum += len;
2316 }
2317 }
2318 datbuf.len = sum;
2319 if (mreply != NULL) {
2320 /* called from kernel space */
2321 bcopy (&datbuf, datptr, sizeof(datbuf));
2322 } else {
2323 /* called from user space */
2324 if ((err = copyout((caddr_t)&datbuf, (caddr_t)datptr, sizeof(datbuf))) != 0)
2325 goto l_err;
2326 }
2327
2328 if (mreply != NULL) {
2329 /* called from kernel space */
2330 /* return the reply data in mbufs, so dont free them.
2331 Just free the proto info */
2332 mdata = gbuf_cont(mproto);
2333 *mreply = mdata;
2334 gbuf_cont(mproto) = NULL;
2335 gbuf_freem(mproto);
2336 } else {
2337 /* called from user space */
2338 gbuf_freem(mproto);
2339 }
2340
2341 ATDISABLE(s, scb->lock);
2342 if (scb->sess_ioc)
2343 scb->rcv_cnt--;
2344 else {
2345 scb->rcv_cnt = 0;
2346 scb->snd_stop = 0;
2347 }
2348 ATENABLE(s, scb->lock);
2349 return rval;
2350
2351 l_err:
2352 ATDISABLE(s, scb->lock);
2353 gbuf_next(mproto) = scb->sess_ioc;
2354 scb->sess_ioc = mproto;
2355 ATENABLE(s, scb->lock);
2356 *errp = err;
2357 return -1;
2358 }