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