]> git.saurik.com Git - apple/xnu.git/blame - bsd/netat/adsp_stream.c
xnu-123.5.tar.gz
[apple/xnu.git] / bsd / netat / adsp_stream.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * Copyright (c) 1995-1998 Apple Computer, Inc.
24 * All Rights Reserved.
25 */
26
27/*
28 * 09/07/95 - Modified for performance (Tuyen Nguyen)
29 * Modified for MP, 1996 by Tuyen Nguyen
30 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
31 */
32#include <sys/errno.h>
33#include <sys/types.h>
34#include <sys/param.h>
35#include <machine/spl.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/proc.h>
39#include <sys/filedesc.h>
40#include <sys/fcntl.h>
41#include <sys/mbuf.h>
42#include <sys/socket.h>
43#include <sys/socketvar.h>
44#include <sys/time.h>
45#include <sys/ioctl.h>
46#include <sys/malloc.h>
47
48#include <net/if.h>
49
50#include <netat/sysglue.h>
51#include <netat/appletalk.h>
52#include <netat/ddp.h>
53#include <netat/at_snmp.h>
54#include <netat/at_pcb.h>
55#include <netat/debug.h>
56#include <netat/at_var.h>
57#include <netat/adsp.h>
58#include <netat/adsp_internal.h>
59
60void SndMsgUp();
61void adsp_rput();
62static void adsp_iocack();
63static void adsp_iocnak();
64void adsp_dequeue_ccb();
65unsigned char adspAssignSocket();
66int adspallocate(), adsprelease();
67int adspInited = 0;
68
69atlock_t adspall_lock;
70atlock_t adspgen_lock;
71GLOBAL adspGlobal;
72
73/**********/
74
75int adsp_pidM[256];
76char adsp_inputC[256];
77CCB *adsp_inputQ[256];
78
79extern at_ifaddr_t *ifID_home;
80
81CCB *ccb_used_list;
82
83void adsp_input(mp)
84 gbuf_t *mp;
85{
86 gref_t *gref;
87 CCBPtr sp;
88 at_ddp_t *p;
89 int s, l;
90 gbuf_t *mb;
91
92 switch (gbuf_type(mp)) {
93 case MSG_DATA:
94 p = (at_ddp_t *)gbuf_rptr(mp);
95 ATDISABLE(s, adspall_lock);
96 sp = adsp_inputQ[p->dst_socket];
97 if ((sp == 0) || (sp->gref==0) || (sp->state==sClosed))
98 {
99 ATENABLE(s, adspall_lock);
100 gbuf_freem(mp);
101 return;
102 }
103 else if (sp->otccbLink != 0) {
104 do {
105 if ((sp->remoteAddress.a.node == p->src_node)
106 && (sp->remoteAddress.a.socket == p->src_socket)
107 && (sp->remoteAddress.a.net == NET_VALUE(p->src_net)))
108 break;
109 } while ((sp = sp->otccbLink) != 0);
110 if (sp == 0)
111 {
112 ATENABLE(s, adspall_lock);
113 gbuf_freem(mp);
114 return;
115 }
116 }
117 if (sp->lockFlag) {
118 gbuf_next(mp) = 0;
119 if (sp->deferred_mb) {
120 for (mb=sp->deferred_mb; gbuf_next(mb); mb=gbuf_next(mb)) ;
121 gbuf_next(mb) = mp;
122 } else
123 sp->deferred_mb = mp;
124 ATENABLE(s, adspall_lock);
125 return;
126 }
127 ATDISABLE(l, sp->lockRemove);
128 sp->lockFlag = 1;
129 ATENABLE(l, adspall_lock);
130 while (mp) {
131 adsp_rput(sp->gref, mp);
132 if ((mp = sp->deferred_mb) != 0) {
133 sp->deferred_mb = gbuf_next(mp);
134 gbuf_next(mp) = 0;
135 }
136 }
137 sp->lockFlag = 0;
138 ATENABLE(s, sp->lockRemove);
139 return;
140
141 case MSG_IOCACK:
142 case MSG_IOCNAK:
143 gref = (gref_t *)((ioc_t *)gbuf_rptr(mp))->ioc_private;
144 break;
145
146 case MSG_IOCTL:
147#ifdef APPLETALK_DEBUG
148 kprintf("unexpected MSG_IOCTL in adsp_input()");
149#endif
150 /* fall through */
151
152 default:
153 gbuf_freem(mp);
154 return;
155 }
156
157 adsp_rput(gref, mp);
158}
159
160/**********/
161int adsp_readable(gref)
162 gref_t *gref;
163{
164 int rc;
165 CCBPtr sp;
166
167 if (gref->info == 0)
168 /*
169 * we don't have the structure we need to determine
170 * if there's data available... we return readable in
171 * this case to keep from hanging up in the select
172 * a subsequent read will run into the same missing data
173 * structure and return an error... the ATselect code does
174 * this if it can't retrieve the 'gref' structure from the
175 * file table for the fd specified
176 */
177 return(1);
178
179 sp = (CCBPtr)gbuf_rptr(((gbuf_t *)gref->info));
180 rc = sp->rData;
181
182 return rc;
183}
184
185int adsp_writeable(gref)
186 gref_t *gref;
187{
188 int s, rc;
189 CCBPtr sp;
190
191 if (gref->info == 0)
192 /*
193 * we don't have the structure we need to determine
194 * if there's room available... we return writeable in
195 * this case to keep from hanging up in the select
196 * a subsequent write will run into the same missing data
197 * structure and return an error... the ATselect code does
198 * this if it can't retrieve the 'gref' structure from the
199 * file table for the fd specified
200 */
201 return(1);
202
203 sp = (CCBPtr)gbuf_rptr(((gbuf_t *)gref->info));
204 ATDISABLE(s, sp->lock);
205 rc = CalcSendQFree(sp);
206 ATENABLE(s, sp->lock);
207
208 return rc;
209}
210
211static void adsp_init()
212{
213 adspInited++;
214 InitGlobals();
215 ccb_used_list = 0;
216 bzero(adsp_pidM, sizeof(adsp_pidM));
217 bzero(adsp_inputC, sizeof(adsp_inputC));
218 bzero(adsp_inputQ, sizeof(adsp_inputQ));
219}
220
221/*
222 * Description:
223 * ADSP open and close routines. These routines
224 * initalize and release the ADSP structures. They do not
225 * have anything to do with "connections"
226 */
227
228int adsp_open(gref)
229 gref_t *gref;
230{
231 register CCBPtr sp;
232 int s;
233
234 if (!adspInited)
235 adsp_init();
236
237 if (!adspAllocateCCB(gref))
238 return(ENOBUFS); /* can't get buffers */
239
240 sp = (CCBPtr)gbuf_rptr(((gbuf_t *)gref->info));
241 gref->readable = adsp_readable;
242 gref->writeable = adsp_writeable;
243 ATDISABLE(s, adspall_lock);
244 if ((sp->otccbLink = ccb_used_list) != 0)
245 sp->otccbLink->ccbLink = sp;
246 ccb_used_list = sp;
247 ATENABLE(s, adspall_lock);
248 return 0;
249}
250
251int adsp_close(gref)
252 gref_t *gref;
253{
254 int s, l;
255 unsigned char localSocket;
256
257 /* make sure we've not yet removed the CCB (e.g., due to TrashSession) */
258 ATDISABLE(l, adspgen_lock);
259 if (gref->info) {
260 CCBPtr sp = (CCBPtr)gbuf_rptr(((gbuf_t *)gref->info));
261 ATDISABLE(s, sp->lock);
262 ATENABLE(s, adspgen_lock);
263 localSocket = sp->localSocket;
264 ATENABLE(l, sp->lock);
265 if (localSocket)
266 adspRelease(gref);
267 else
268 {
269 adsp_dequeue_ccb(sp);
270 gbuf_freeb((gbuf_t *)gref->info);
271 }
272 } else
273 ATENABLE(l, adspgen_lock);
274 return 0;
275}
276
277
278/*
279 * Name:
280 * adsp_rput
281 *
282 * Description:
283 * ADSP streams read put and service routines.
284 */
285
286void adsp_rput(gref, mp)
287 gref_t *gref; /* READ queue */
288 gbuf_t *mp;
289{
290 switch (gbuf_type(mp)) {
291 case MSG_HANGUP:
292 case MSG_IOCACK:
293 case MSG_IOCNAK:
294 switch (adspReadHandler(gref, mp)) {
295 case STR_PUTNEXT:
296 atalk_putnext(gref, mp);
297 break;
298 case STR_IGNORE:
299 break;
300 }
301 break;
302 case MSG_ERROR:
303#ifdef APPLETALK_DEBUG
304 kprintf("adsp_rput received MSG_ERROR");
305#endif
306 /* fall through */
307 default:
308 CheckReadQueue(gbuf_rptr(((gbuf_t *)gref->info)));
309 CheckSend(gbuf_rptr(((gbuf_t *)gref->info)));
310
311 switch (gbuf_type(mp)) {
312 case MSG_IOCTL:
313 case MSG_DATA:
314 case MSG_PROTO:
315 if (adspReadHandler(gref, mp) == STR_PUTNEXT)
316 atalk_putnext(gref, mp);
317 break;
318 default:
319 atalk_putnext(gref, mp);
320 break;
321 }
322 }
323}
324
325/*
326 * Name:
327 * adsp_wput
328 *
329 * Description:
330 * ADSP streams write put and service routines.
331 *
332 */
333
334int adsp_wput(gref, mp)
335 gref_t *gref; /* WRITE queue */
336 gbuf_t *mp;
337{
338 int rc;
339 int s;
340 gbuf_t *xm;
341 ioc_t *iocbp;
342 CCBPtr sp = (CCBPtr)gbuf_rptr(((gbuf_t *)gref->info));
343
344 if (gbuf_type(mp) == MSG_IOCTL) {
345 iocbp = (ioc_t *)gbuf_rptr(mp);
346 switch (iocbp->ioc_cmd) {
347 case ADSPBINDREQ:
348 {
349 unsigned char v;
350
351 if (gbuf_cont(mp) == NULL) {
352 iocbp->ioc_rval = -1;
353 adsp_iocnak(gref, mp, EINVAL);
354 }
355 v = *(unsigned char *)gbuf_rptr(gbuf_cont(mp));
356 ATDISABLE(s, adspall_lock);
357 if ( (v != 0)
358 && ((v > DDP_SOCKET_LAST) || (v < 2)
359 || ddp_socket_inuse(v, DDP_ADSP))) {
360 ATENABLE(s, adspall_lock);
361 iocbp->ioc_rval = -1;
362 adsp_iocnak(gref, mp, EINVAL);
363 }
364 else {
365 if (v == 0) {
366 ATENABLE(s, adspall_lock);
367 if ((v = adspAssignSocket(gref, 0)) == 0) {
368 iocbp->ioc_rval = -1;
369 adsp_iocnak(gref, mp, EINVAL);
370 return 0;
371 }
372 } else {
373 adsp_inputC[v] = 1;
374 adsp_inputQ[v] = sp;
375 adsp_pidM[v] = sp->pid;
376 ATENABLE(s, adspall_lock);
377 adsp_dequeue_ccb(sp);
378 }
379 *(unsigned char *)gbuf_rptr(gbuf_cont(mp)) = v;
380 sp->localSocket = v;
381 iocbp->ioc_rval = 0;
382 adsp_iocack(gref, mp);
383 }
384 return 0;
385 }
386
387 case ADSPGETSOCK:
388 case ADSPGETPEER:
389 {
390 at_inet_t *addr;
391
392 if (((xm = gbuf_cont(mp)) == NULL)
393 && ((xm = gbuf_alloc(sizeof(at_inet_t), PRI_MED)) == NULL)) {
394 iocbp->ioc_rval = -1;
395 adsp_iocnak(gref, mp, ENOBUFS);
396 return 0;
397 }
398 gbuf_cont(mp) = xm;
399 gbuf_wset(xm,sizeof(at_inet_t));
400 addr = (at_inet_t *)gbuf_rptr(xm);
401 if (iocbp->ioc_cmd == ADSPGETSOCK) {
402 /* Obtain Network and Node Id's from DDP */
403 /* *** was ddp_get_cfg() *** */
404 addr->net = ifID_home->ifThisNode.s_net;
405 addr->node = ifID_home->ifThisNode.s_node;
406 addr->socket = (sp)? sp->localSocket: 0;
407 } else
408 if (sp)
409 *addr = sp->remoteAddress.a;
410 else {
411 addr->net = 0;
412 addr->node = 0;
413 addr->socket = 0;
414 }
415 iocbp->ioc_rval = 0;
416 adsp_iocack(gref, mp);
417 return 0;
418 }
419 case DDP_IOC_GET_CFG:
420 /* respond to an DDP_IOC_GET_CFG sent on an adsp fd */
421 if (((xm = gbuf_cont(mp)) == NULL) &&
422 (xm = gbuf_alloc(sizeof(at_inet_t), PRI_MED)) == NULL) {
423 iocbp->ioc_rval = -1;
424 adsp_iocnak(gref, mp, ENOBUFS);
425 return 0;
426 }
427 gbuf_cont(mp) = xm;
428 gbuf_wset(xm, sizeof(ddp_addr_t));
429 /* Obtain Network and Node Id's from DDP */
430 {
431 /* *** was ddp_get_cfg() *** */
432 ddp_addr_t *cfgp =
433 (ddp_addr_t *)gbuf_rptr(gbuf_cont(mp));
434 cfgp->inet.net = ifID_home->ifThisNode.s_net;
435 cfgp->inet.node = ifID_home->ifThisNode.s_node;
436 cfgp->inet.socket = (sp)? sp->localSocket: 0;
437 cfgp->ddptype = DDP_ADSP;
438 }
439 iocbp->ioc_rval = 0;
440 adsp_iocack(gref, mp);
441 return 0;
442 } /* switch */
443 }
444
445 if (!gref->info)
446 gbuf_freem(mp);
447 else {
448 ATDISABLE(s, sp->lockClose);
449 rc = adspWriteHandler(gref, mp);
450 ATENABLE(s, sp->lockClose);
451
452 switch (rc) {
453 case STR_PUTNEXT:
454 if (gbuf_type(mp) == MSG_IOCTL) {
455 iocbp = (ioc_t *)gbuf_rptr(mp);
456 iocbp->ioc_private = (void *)gref;
457 }
458 DDP_OUTPUT(mp);
459 break;
460 case STR_IGNORE:
461 case STR_IGNORE+99:
462 break;
463 default:
464 gbuf_freem(mp);
465 break;
466 }
467 }
468
469 return 0;
470} /* adsp_wput */
471
472void adspioc_ack(errno, m, gref)
473 int errno;
474 gbuf_t *m;
475 gref_t *gref;
476{
477 ioc_t *iocbp;
478
479 if (m == NULL)
480 return;
481 iocbp = (ioc_t *) gbuf_rptr(m);
482
483 iocbp->ioc_error = errno; /* set the errno */
484 iocbp->ioc_count = gbuf_msgsize(gbuf_cont(m));
485 if (gbuf_type(m) == MSG_IOCTL) /* if an ioctl, this is an ack */
486 gbuf_set_type(m, MSG_IOCACK); /* and ALWAYS update the user */
487 /* ioctl structure */
488 trace_mbufs(D_M_ADSP,"A ", m);
489 SndMsgUp(gref, m);
490}
491
492static void adsp_iocack(gref, m)
493 gref_t *gref;
494 register gbuf_t *m;
495{
496 if (gbuf_type(m) == MSG_IOCTL)
497 gbuf_set_type(m, MSG_IOCACK);
498
499 if (gbuf_cont(m))
500 ((ioc_t *)gbuf_rptr(m))->ioc_count = gbuf_msgsize(gbuf_cont(m));
501 else
502 ((ioc_t *)gbuf_rptr(m))->ioc_count = 0;
503
504 SndMsgUp(gref, m);
505}
506
507
508static void adsp_iocnak(gref, m, err)
509 gref_t *gref;
510 register gbuf_t *m;
511 register int err;
512{
513 if (gbuf_type(m) == MSG_IOCTL)
514 gbuf_set_type(m, MSG_IOCNAK);
515 ((ioc_t *)gbuf_rptr(m))->ioc_count = 0;
516
517 if (err == 0)
518 err = ENXIO;
519 ((ioc_t *)gbuf_rptr(m))->ioc_error = err;
520
521 if (gbuf_cont(m)) {
522 gbuf_freem(gbuf_cont(m));
523 gbuf_cont(m) = NULL;
524 }
525 SndMsgUp(gref, m);
526}
527
528unsigned char
529adspAssignSocket(gref, flag)
530 gref_t *gref;
531 int flag;
532{
533 unsigned char sVal, sMax, sMin, sSav, inputC;
534 CCBPtr sp;
535 int s;
536
537 sMax = flag ? DDP_SOCKET_LAST-46 : DDP_SOCKET_LAST-6;
538 sMin = DDP_SOCKET_1st_DYNAMIC-64;
539
540 ATDISABLE(s, adspall_lock);
541 for (inputC=255, sVal=sMax; sVal >= sMin; sVal--) {
542 if (!ddp_socket_inuse(sVal, DDP_ADSP))
543 break;
544 else if (flag) {
545 if (adsp_inputC[sVal] &&
546 /* meaning that raw DDP doesn't have it */
547 (adsp_inputC[sVal] < inputC)
548 && (adsp_inputQ[sVal]->state == sOpen)) {
549 inputC = adsp_inputC[sVal];
550 sSav = sVal;
551 }
552 }
553 }
554 if (sVal < sMin) {
555 if (!flag || (inputC == 255)) {
556 ATENABLE(s, adspall_lock);
557 return 0;
558 }
559 sVal = sSav;
560 }
561 sp = (CCBPtr)gbuf_rptr(((gbuf_t *)gref->info));
562 ATENABLE(s, adspall_lock);
563 adsp_dequeue_ccb(sp);
564 ATDISABLE(s, adspall_lock);
565 adsp_inputC[sVal]++;
566 sp->otccbLink = adsp_inputQ[sVal];
567 adsp_inputQ[sVal] = sp;
568 if (!flag)
569 adsp_pidM[sVal] = sp->pid;
570 ATENABLE(s, adspall_lock);
571 return sVal;
572}
573
574int
575adspDeassignSocket(sp)
576 CCBPtr sp;
577{
578 unsigned char sVal;
579 CCBPtr curr_sp;
580 CCBPtr prev_sp;
581 int pid = 0;
582 int s, l;
583
584 dPrintf(D_M_ADSP, D_L_TRACE, ("adspDeassignSocket: pid=%d,s=%d\n",
585 sp->pid, sp->localSocket));
586 ATDISABLE(s, adspall_lock);
587 sVal = sp->localSocket;
588 if ((curr_sp = adsp_inputQ[sVal]) != 0) {
589 prev_sp = 0;
590 while (curr_sp != sp) {
591 prev_sp = curr_sp;
592 curr_sp = curr_sp->otccbLink;
593 }
594 if (curr_sp) {
595 ATDISABLE(l, sp->lockRemove);
596 if (prev_sp)
597 prev_sp->otccbLink = sp->otccbLink;
598 else
599 adsp_inputQ[sVal] = sp->otccbLink;
600 ATENABLE(l, sp->lockRemove);
601 if (adsp_inputQ[sVal])
602 adsp_inputC[sVal]--;
603 else {
604 pid = adsp_pidM[sVal];
605 adsp_inputC[sVal] = 0;
606 adsp_pidM[sVal] = 0;
607 }
608 sp->ccbLink = 0;
609 sp->otccbLink = 0;
610 sp->localSocket = 0;
611 ATENABLE(s, adspall_lock);
612 return pid ? 0 : 1;
613 }
614 }
615 ATENABLE(s, adspall_lock);
616
617 dPrintf(D_M_ADSP, D_L_ERROR,
618 ("adspDeassignSocket: closing, no CCB block, trouble ahead\n"));
619 return -1;
620} /* adspDeassignSocket */
621
622/*
623 * remove CCB from the use list
624 */
625void
626adsp_dequeue_ccb(sp)
627 CCB *sp;
628{
629 int s;
630
631 ATDISABLE(s, adspall_lock);
632 if (sp == ccb_used_list) {
633 if ((ccb_used_list = sp->otccbLink) != 0)
634 sp->otccbLink->ccbLink = 0;
635 } else if (sp->ccbLink) {
636 if ((sp->ccbLink->otccbLink = sp->otccbLink) != 0)
637 sp->otccbLink->ccbLink = sp->ccbLink;
638 }
639
640 sp->otccbLink = 0;
641 sp->ccbLink = 0;
642 ATENABLE(s, adspall_lock);
643}
644
645void SndMsgUp(gref, mp)
646 gref_t *gref; /* WRITE queue */
647 gbuf_t *mp;
648{
649/*
650 dPrintf(D_M_ADSP, D_L_TRACE,
651 ("SndMsgUp: gref=0x%x, mbuf=0x%x\n", (unsigned)gref, (unsigned)mp));
652 trace_mbufs(D_M_ADSP, " m", mp);
653*/
654 atalk_putnext(gref, mp);
655}