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