2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1995 Apple Computer, Inc.
32 * Created, March 17, 1997 by Tuyen Nguyen for MacOSX.
35 #include <sys/errno.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <machine/spl.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/filedesc.h>
43 #include <sys/fcntl.h>
44 #include <sys/file_internal.h>
46 #include <sys/ioctl.h>
47 #include <sys/malloc.h>
48 #include <kern/locks.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/ioccom.h>
52 #include <sys/uio_internal.h>
54 #include <sys/vnode.h>
56 #include <sys/sysctl.h>
60 #include <netat/sysglue.h>
61 #include <netat/appletalk.h>
62 #include <netat/ddp.h>
63 #include <netat/at_pcb.h>
64 #include <netat/at_var.h>
65 #include <netat/routing_tables.h>
66 #include <netat/adsp.h>
67 #include <netat/adsp_internal.h>
68 #include <netat/asp.h>
69 #include <netat/atp.h>
70 #include <netat/debug.h>
72 int _ATkqfilter(struct fileproc
*, struct knote
*, vfs_context_t
);
73 int _ATselect(struct fileproc
*, int, void *, vfs_context_t
);
74 int _ATioctl(struct fileproc
*, u_long
, caddr_t
, vfs_context_t
);
75 int _ATwrite(struct fileproc
*, struct uio
*, int, vfs_context_t
);
76 int _ATread(struct fileproc
*, struct uio
*, int, vfs_context_t
);
77 int _ATclose(struct fileglob
*, vfs_context_t
);
79 int _ATrw(struct fileproc
*, enum uio_rw
, struct uio
*, vfs_context_t
);
81 extern struct atpcb ddp_head
;
82 extern lck_mtx_t
* atalk_mutex
;
84 int atp_free_cluster_timeout_set
= 0;
86 int gref_alloc(gref_t
**);
89 /* bms: make gref_close non static so its callable from kernel */
90 int gref_close(gref_t
*gref
);
92 SYSCTL_DECL(_net_appletalk
);
94 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, debug
, CTLFLAG_WR
,
95 &dbgBits
, dbgBits
, "AppleTalk Debug Flags");
96 int RouterMix
= RT_MIX_DEFAULT
; /* default for nbr of ppsec */
97 SYSCTL_INT(_net_appletalk
, OID_AUTO
, routermix
, CTLFLAG_WR
,
98 &RouterMix
, 0, "Appletalk RouterMix");
99 at_ddp_stats_t at_ddp_stats
; /* DDP statistics */
100 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, ddpstats
, CTLFLAG_RD
,
101 &at_ddp_stats
, at_ddp_stats
, "AppleTalk DDP Stats");
103 static void ioccmd_t_32_to_64( ioccmd_t
*from_p
, user_ioccmd_t
*to_p
);
104 static void ioccmd_t_64_to_32( user_ioccmd_t
*from_p
, ioccmd_t
*to_p
);
106 extern lck_mtx_t
*atalk_cluster_lock
;
107 caddr_t atp_free_cluster_list
= NULL
;
109 void gref_wput(gref_t
*, gbuf_t
*m
);
111 void gref_wput(gref
, m
)
115 switch (gref
->proto
) {
117 ddp_putmsg(gref
, m
); break;
119 elap_wput(gref
, m
); break;
121 atp_wput(gref
, m
); break;
123 asp_wput(gref
, m
); break;
126 aurp_wput(gref
, m
); break;
129 adsp_wput(gref
, m
); break;
131 if (gbuf_type(m
) == MSG_IOCTL
) {
132 gbuf_freem(gbuf_cont(m
));
134 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= -1;
135 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= EPROTOTYPE
;
136 gbuf_set_type(m
, MSG_IOCNAK
);
137 atalk_putnext(gref
, m
);
147 int _ATsocket(proto
, err
, proc
)
155 /* make sure the specified protocol id is valid */
158 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
159 BSD-style socket interface. */
170 #ifdef APPLETALK_DEBUG
171 kprintf("_ATsocket: error EPROTOTYPE =%d\n", *err
);
176 /* allocate a protocol channel */
177 if ((*err
= gref_alloc(&gref
)) != 0) {
178 #ifdef APPLETALK_DEBUG
179 kprintf("_ATsocket: error gref_open =%d\n", *err
);
184 gref
->pid
= proc_pid((struct proc
*)proc
);
186 /* open the specified protocol */
187 switch (gref
->proto
) {
189 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
190 BSD-style socket interface. */
193 *err
= atp_open(gref
, 1); break;
195 *err
= asp_open(gref
); break;
198 *err
= aurp_open(gref
); break;
201 *err
= adsp_open(gref
); break;
204 /* create the descriptor for the channel */
206 #ifdef APPLETALK_DEBUG
207 kprintf("_ATsocket: open failed for %d proto; err = %d\n",
210 gref
->proto
= ATPROTO_NONE
;
212 if (*err
|| (*err
= atalk_openref(gref
, &fd
, proc
))) {
213 #ifdef APPLETALK_DEBUG
214 kprintf("_ATsocket: error atalk_openref =%d\n", *err
);
216 (void)gref_close(gref
);
220 kprintf("_ATsocket: proto=%d return=%d fd=%d\n", proto, *err, fd);
225 int _ATgetmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
236 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) == 0) {
237 switch (gref
->proto
) {
239 rc
= ASPgetmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
);
243 rc
= AURPgetmsg(err
);
247 *err
= EPROTONOSUPPORT
;
253 /* kprintf("_ATgetmsg: return=%d\n", *err);*/
257 int _ATputmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
268 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) == 0) {
269 switch (gref
->proto
) {
271 rc
= ASPputmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
); break;
273 *err
= EPROTONOSUPPORT
; break;
278 /* kprintf("_ATputmsg: return=%d\n", *err); */
284 __unused vfs_context_t ctx
)
289 if ((err
= atalk_closeref(fg
, &gref
)) == 0) {
291 (void)gref_close(gref
);
298 int _ATrw(fp
, rw
, uio
, ctx
)
304 int err
, len
, clen
= 0, res
;
306 gbuf_t
*m
, *mhead
, *mprev
;
307 proc_t p
= vfs_context_proc(ctx
);
309 /* no need to get/drop iocount as the fp already has one */
310 if ((err
= atalk_getref_locked(fp
, 0, &gref
, p
, 1)) != 0)
313 // LP64todo - fix this!
314 if ((len
= uio_resid(uio
)) == 0)
318 if (rw
== UIO_READ
) {
319 KERNEL_DEBUG(DBG_ADSP_ATRW
, 0, gref
, len
, gref
->rdhead
, 0);
320 while ((gref
->errno
== 0) && ((mhead
= gref
->rdhead
) == 0)) {
321 gref
->sevents
|= POLLMSG
;
322 err
= msleep(&gref
->event
, atalk_mutex
, PSOCK
| PCATCH
, "AT read", 0);
323 gref
->sevents
&= ~POLLMSG
;
326 KERNEL_DEBUG(DBG_ADSP_ATRW
, 1, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
331 if ((gref
->rdhead
= gbuf_next(mhead
)) == 0)
334 KERNEL_DEBUG(DBG_ADSP_ATRW
, 2, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
337 //##### LD TEST 08/05
338 // simple_lock(&gref->lock);
340 gbuf_next(mhead
) = 0;
342 for (mprev
=0, m
=mhead
; m
&& len
; len
-=clen
) {
343 if ((clen
= gbuf_len(m
)) > 0) {
346 uio
->uio_rw
= UIO_READ
;
347 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
),
349 KERNEL_DEBUG(DBG_ADSP_ATRW
, 3, m
, clen
,
353 if (gbuf_len(m
) > len
) {
362 KERNEL_DEBUG(DBG_ADSP_ATRW
, 4, m
, gbuf_len(m
), mprev
, gref
->rdhead
);
364 gbuf_cont(mprev
) = 0;
367 if (gref
->rdhead
== 0)
369 gbuf_next(m
) = gref
->rdhead
;
375 // simple_unlock(&gref->lock);
377 if (gref
->writeable
) {
378 while (!(*gref
->writeable
)(gref
)) {
379 /* flow control on, wait to be enabled to write */
380 gref
->sevents
|= POLLSYNC
;
381 err
= msleep(&gref
->event
, atalk_mutex
, PSOCK
| PCATCH
, "AT write", 0);
382 gref
->sevents
&= ~POLLSYNC
;
389 /* allocate a buffer to copy in the write data */
390 if ((m
= gbuf_alloc(AT_WR_OFFSET
+len
, PRI_MED
)) == 0)
392 gbuf_rinc(m
,AT_WR_OFFSET
);
395 /* copy in the write data */
396 uio
->uio_rw
= UIO_WRITE
;
397 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
), len
, uio
))) {
398 #ifdef APPLETALK_DEBUG
399 kprintf("_ATrw: UIO_WRITE: res=%d\n", res
);
405 /* forward the write data to the appropriate protocol module */
421 stat
= _ATrw(fp
, UIO_READ
, uio
, ctx
);
435 stat
= _ATrw(fp
, UIO_WRITE
, uio
, ctx
);
441 /* Most of the processing from _ATioctl, so that it can be called
442 from the new ioctl code */
443 /* bms: update to be callable from kernel */
444 int at_ioctl(gref_t
*gref
, u_long cmd
, caddr_t arg
, int fromKernel
)
450 user_addr_t user_arg
;
451 user_ioccmd_t user_ioccmd
;
454 /* error if not for us */
455 if ((cmd
& 0xffff) != 0xff99)
458 size
= IOCPARM_LEN(cmd
);
459 if (size
!= sizeof(user_addr_t
))
462 user_arg
= *((user_addr_t
*)arg
);
464 /* copy in ioc command info */
465 is64bit
= proc_is64bit(current_proc());
468 bcopy (CAST_DOWN(caddr_t
, user_arg
), &tmp
, sizeof (tmp
));
469 ioccmd_t_32_to_64(&tmp
, &user_ioccmd
);
473 err
= copyin(user_arg
, (caddr_t
)&user_ioccmd
, sizeof(user_ioccmd
));
477 err
= copyin(user_arg
, (caddr_t
)&tmp
, sizeof(tmp
));
478 ioccmd_t_32_to_64(&tmp
, &user_ioccmd
);
481 #ifdef APPLETALK_DEBUG
482 kprintf("at_ioctl: err = %d, copyin(%llx, %x, %d)\n", err
,
483 user_arg
, (caddr_t
)&user_ioccmd
, sizeof(user_ioccmd
));
489 /* allocate a buffer to create an ioc command
490 first mbuf contains ioc command */
491 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) == 0)
493 gbuf_wset(m
, sizeof(ioc_t
)); /* mbuf->m_len */
494 gbuf_set_type(m
, MSG_IOCTL
); /* mbuf->m_type */
496 /* create the ioc command
497 second mbuf contains the actual ASP command */
498 if (user_ioccmd
.ic_len
) {
499 if ((gbuf_cont(m
) = gbuf_alloc(user_ioccmd
.ic_len
, PRI_HI
)) == 0) {
501 #ifdef APPLETALK_DEBUG
502 kprintf("at_ioctl: gbuf_alloc err=%d\n",ENOBUFS
);
506 gbuf_wset(gbuf_cont(m
), user_ioccmd
.ic_len
); /* mbuf->m_len */
508 bcopy (CAST_DOWN(caddr_t
, user_ioccmd
.ic_dp
), gbuf_rptr(gbuf_cont(m
)), user_ioccmd
.ic_len
);
510 if ((err
= copyin(user_ioccmd
.ic_dp
, (caddr_t
)gbuf_rptr(gbuf_cont(m
)), user_ioccmd
.ic_len
)) != 0) {
516 ioc
= (ioc_t
*) gbuf_rptr(m
);
517 ioc
->ioc_cmd
= user_ioccmd
.ic_cmd
;
518 ioc
->ioc_count
= user_ioccmd
.ic_len
;
522 /* send the ioc command to the appropriate recipient */
525 /* wait for the ioc ack */
526 while ((m
= gref
->ichead
) == 0) {
527 gref
->sevents
|= POLLPRI
;
528 #ifdef APPLETALK_DEBUG
529 kprintf("sleep gref = 0x%x\n", (unsigned)gref
);
531 err
= msleep(&gref
->iocevent
, atalk_mutex
, PSOCK
| PCATCH
, "AT ioctl", 0);
532 gref
->sevents
&= ~POLLPRI
;
534 #ifdef APPLETALK_DEBUG
535 kprintf("at_ioctl: EINTR\n");
542 if (gbuf_next(m
) == m
) /* error case */
545 gref
->ichead
= gbuf_next(m
);
548 #ifdef APPLETALK_DEBUG
549 kprintf("at_ioctl: woke up from ioc sleep gref = 0x%x\n",
553 /* process the ioc response */
554 ioc
= (ioc_t
*) gbuf_rptr(m
);
555 if ((err
= ioc
->ioc_error
) == 0) {
556 user_ioccmd
.ic_timout
= ioc
->ioc_rval
;
557 user_ioccmd
.ic_len
= 0;
558 mdata
= gbuf_cont(m
);
559 if (mdata
&& user_ioccmd
.ic_dp
) {
560 user_ioccmd
.ic_len
= gbuf_msgsize(mdata
);
561 for (len
= 0; mdata
; mdata
= gbuf_cont(mdata
)) {
563 bcopy (gbuf_rptr(mdata
), CAST_DOWN(caddr_t
, (user_ioccmd
.ic_dp
+ len
)), gbuf_len(mdata
));
565 if ((err
= copyout((caddr_t
)gbuf_rptr(mdata
), (user_ioccmd
.ic_dp
+ len
), gbuf_len(mdata
))) < 0) {
566 #ifdef APPLETALK_DEBUG
567 kprintf("at_ioctl: len=%d error copyout=%d from=%x to=%x gbuf_len=%x\n",
568 len
, err
, (caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&user_ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
573 len
+= gbuf_len(mdata
);
579 ioccmd_t_64_to_32(&user_ioccmd
, &tmp
);
580 bcopy (&tmp
, CAST_DOWN(caddr_t
, user_arg
), sizeof(tmp
));
584 err
= copyout((caddr_t
)&user_ioccmd
, user_arg
, sizeof(user_ioccmd
));
588 ioccmd_t_64_to_32(&user_ioccmd
, &tmp
);
589 err
= copyout((caddr_t
)&tmp
, user_arg
, sizeof(tmp
));
599 /*kprintf("at_ioctl: I_done=%d\n", err);*/
606 register caddr_t arg
,
607 __unused vfs_context_t ctx
)
613 /* No need to get a reference on fp as it already has one */
614 if ((err
= atalk_getref_locked(fp
, 0, &gref
, 0, 0)) != 0) {
615 #ifdef APPLETALK_DEBUG
616 kprintf("_ATioctl: atalk_getref err = %d\n", err
);
620 err
= at_ioctl(gref
, cmd
, arg
, 0);
627 int _ATselect(fp
, which
, wql
, ctx
)
635 proc_t proc
= vfs_context_proc(ctx
);
637 /* Radar 4128949: Drop the proc_fd lock here to avoid lock inversion issues with the other AT calls
638 * select() is already holding a reference on the fd, so it won't go away during the time it is unlocked.
643 /* no need to drop the iocount as select covers that */
644 err
= atalk_getref_locked(fp
, 0, &gref
, 0, 0);
647 /* Safe to re-grab the proc_fdlock at that point */
652 if (which
== FREAD
) {
653 if (gref
->rdhead
|| (gref
->readable
&& (*gref
->readable
)(gref
)))
656 gref
->sevents
|= POLLIN
;
657 selrecord(proc
, &gref
->si
, wql
);
660 else if (which
== POLLOUT
) {
661 if (gref
->writeable
) {
662 if ((*gref
->writeable
)(gref
))
665 gref
->sevents
|= POLLOUT
;
666 selrecord(proc
, &gref
->si
, wql
);
677 __unused
struct fileproc
*fp
,
678 __unused
struct knote
*kn
,
679 __unused vfs_context_t ctx
)
684 void atalk_putnext(gref
, m
)
690 /* *** potential leak? *** */
693 switch (gbuf_type(m
)) {
697 gbuf_next(gref
->ichead
) = m
;
700 if (gref
->sevents
& POLLPRI
) {
701 #ifdef APPLETALK_DEBUG
702 kprintf("wakeup gref = 0x%x\n", (unsigned)gref
);
704 wakeup(&gref
->iocevent
);
709 /* *** this processing was moved to atalk_notify *** */
710 panic("atalk_putnext receved MSG_ERROR");
717 gbuf_next(gref
->rdtail
) = m
;
721 if (gref
->sevents
& POLLMSG
) {
722 gref
->sevents
&= ~POLLMSG
;
723 wakeup(&gref
->event
);
725 if (gref
->sevents
& POLLIN
) {
726 gref
->sevents
&= ~POLLIN
;
727 selwakeup(&gref
->si
);
731 } /* switch gbuf_type(m) */
733 } /* atalk_putnext */
735 void atalk_enablew(gref
)
738 if (gref
->sevents
& POLLSYNC
)
739 wakeup(&gref
->event
);
742 void atalk_flush(gref
)
747 gbuf_freel(gref
->rdhead
);
751 gbuf_freel(gref
->ichead
);
757 * Notify an appletalk user of an asynchronous error;
758 * just wake up so that he can collect error status.
760 void atalk_notify(gref
, errno
)
761 register gref_t
*gref
;
765 if (gref
->atpcb_socket
) {
767 This section is patterned after udp_notify() in
770 gref
->atpcb_socket
->so_error
= errno
;
771 sorwakeup(gref
->atpcb_socket
);
772 sowwakeup(gref
->atpcb_socket
);
774 /* for ATP, ASP, and ADSP */
775 if (gref
->errno
== 0) {
777 /* clear out data waiting to be read */
779 gbuf_freel(gref
->rdhead
);
783 if (gref
->sevents
& POLLMSG
) {
784 gref
->sevents
&= ~POLLMSG
;
785 wakeup(&gref
->event
);
788 if (gref
->sevents
& POLLIN
) {
789 gref
->sevents
&= ~POLLIN
;
790 selwakeup(&gref
->si
);
796 void atalk_notify_sel(gref
)
800 if (gref
->sevents
& POLLIN
) {
801 gref
->sevents
&= ~POLLIN
;
802 selwakeup(&gref
->si
);
806 int atalk_peek(gref
, event
)
808 unsigned char *event
;
813 *event
= *gbuf_rptr(gref
->rdhead
);
822 static gbuf_t
*trace_msg
;
824 void atalk_settrace(char * str
, p1
, p2
, p3
, p4
, p5
)
830 sprintf(trace_buf
, str
, p1
, p2
, p3
, p4
, p5
);
831 len
= strlen(trace_buf
);
832 #ifdef APPLETALK_DEBUG
833 kprintf("atalk_settrace: gbufalloc size=%d\n", len
+1);
835 if ((m
= gbuf_alloc(len
+1, PRI_MED
)) == 0)
838 strcpy(gbuf_rptr(m
), trace_buf
);
840 for (nextm
=trace_msg
; gbuf_cont(nextm
); nextm
=gbuf_cont(nextm
)) ;
841 gbuf_cont(nextm
) = m
;
846 void atalk_gettrace(m
)
850 gbuf_cont(m
) = trace_msg
;
856 #define GREF_PER_BLK 32
857 static gref_t
*gref_free_list
= 0;
858 extern gbuf_t
*atp_resource_m
;
860 int gref_alloc(grefp
)
865 gref_t
*gref
, *gref_array
;
867 *grefp
= (gref_t
*)NULL
;
869 if (gref_free_list
== 0) {
870 #ifdef APPLETALK_DEBUG
871 kprintf("gref_alloc: gbufalloc size=%d\n", GREF_PER_BLK
*sizeof(gref_t
));
873 if ((m
= gbuf_alloc(GREF_PER_BLK
*sizeof(gref_t
),PRI_HI
)) == 0)
875 bzero(gbuf_rptr(m
), GREF_PER_BLK
*sizeof(gref_t
));
876 gref_array
= (gref_t
*)gbuf_rptr(m
);
877 for (i
=0; i
< GREF_PER_BLK
-1; i
++)
878 gref_array
[i
].atpcb_next
= (gref_t
*)&gref_array
[i
+1];
879 gbuf_cont(m
) = atp_resource_m
;
881 gref_array
[i
].atpcb_next
= gref_free_list
;
882 gref_free_list
= (gref_t
*)&gref_array
[0];
885 gref
= gref_free_list
;
886 gref_free_list
= gref
->atpcb_next
;
887 ATEVENTINIT(gref
->event
);
888 ATEVENTINIT(gref
->iocevent
);
890 /* *** just for now *** */
891 gref
->atpcb_socket
= (struct socket
*)NULL
;
897 /* bms: make gref_close callable from kernel */
898 int gref_close(gref_t
*gref
)
902 switch (gref
->proto
) {
904 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
905 BSD-style socket interface. */
908 rc
= atp_close(gref
, 1); break;
910 rc
= asp_close(gref
); break;
913 rc
= aurp_close(gref
); break;
917 rc
= adsp_close(gref
); break;
925 selthreadclear(&gref
->si
);
927 /* from original gref_free() */
928 bzero((char *)gref
, sizeof(gref_t
));
929 gref
->atpcb_next
= gref_free_list
;
930 gref_free_list
= gref
;
937 temp fix for bug 2731148 - until this code is re-written to use standard clusters
938 Deletes any free clusters on the free list.
940 void atp_delete_free_clusters(__unused
void *junk
)
943 caddr_t cluster_list
;
945 /* check for free clusters on the free_cluster_list to be deleted */
947 untimeout(&atp_delete_free_clusters
, NULL
);
949 lck_mtx_lock(atalk_cluster_lock
);
951 atp_free_cluster_timeout_set
= 0;
953 cluster_list
= atp_free_cluster_list
;
954 atp_free_cluster_list
= NULL
;
956 lck_mtx_unlock(atalk_cluster_lock
);
958 while ((cluster
= cluster_list
))
960 cluster_list
= *((caddr_t
*)cluster
);
961 FREE(cluster
, M_MCLUST
);
967 Used as the "free" routine for over-size clusters allocated using
971 void m_lgbuf_free(caddr_t
, u_int
, caddr_t
);
976 __unused caddr_t arg
) /* not needed, but they're in m_free() */
980 /* move to free_cluster_list to be deleted later */
981 caddr_t cluster
= (caddr_t
)buf
;
983 lck_mtx_lock(atalk_cluster_lock
);
985 *((caddr_t
*)cluster
) = atp_free_cluster_list
;
986 atp_free_cluster_list
= cluster
;
988 if ((t
= atp_free_cluster_timeout_set
) == 0)
989 atp_free_cluster_timeout_set
= 1;
991 lck_mtx_unlock(atalk_cluster_lock
);
994 timeout(&atp_delete_free_clusters
, NULL
, (1 * HZ
));
998 Used to allocate an mbuf when there is the possibility that it may
999 need to be larger than the size of a standard cluster.
1002 struct mbuf
*m_lgbuf_alloc(size
, wait
)
1007 if (atp_free_cluster_list
)
1008 atp_delete_free_clusters(NULL
); /* delete any free clusters on the free list */
1011 * check that the passed size is within admissible boundaries
1012 * The max data size being ASP of 4576 (8 * ATP_DATA_SIZE),
1013 * allow for extra space for control data
1016 if (size
< 0 || size
> (ATP_DATA_SIZE
* 10))
1019 /* If size is too large, allocate a cluster, otherwise, use the
1020 standard mbuf allocation routines.*/
1021 if (size
> MCLBYTES
) {
1024 (buf
= (void *)_MALLOC(size
, M_MCLUST
,
1025 (wait
)? M_WAITOK
: M_NOWAIT
))) {
1029 (m
= m_clattach(NULL
, MSG_DATA
, buf
, m_lgbuf_free
, size
, 0,
1030 (wait
)? M_WAIT
: M_DONTWAIT
))) {
1031 m_lgbuf_free(buf
, 0, 0);
1035 m
= m_gethdr(((wait
)? M_WAIT
: M_DONTWAIT
), MSG_DATA
);
1036 if (m
&& ((size_t)size
> MHLEN
)) {
1037 MCLGET(m
, ((wait
)? M_WAIT
: M_DONTWAIT
));
1038 if (!(m
->m_flags
& M_EXT
)) {
1046 } /* m_lgbuf_alloc */
1049 gbuf_alloc() is a wrapper for m_lgbuf_alloc(), which is used to
1050 allocate an mbuf when there is the possibility that it may need
1051 to be larger than the size of a standard cluster.
1053 gbuf_alloc() sets the mbuf lengths, unlike the standard mbuf routines.
1056 gbuf_t
*gbuf_alloc_wait(size
, wait
)
1059 gbuf_t
*m
= (gbuf_t
*)m_lgbuf_alloc(size
, wait
);
1061 /* Standard mbuf allocation routines assume that the caller
1062 will set the size. */
1064 m
->m_pkthdr
.len
= size
;
1076 for (size
=0; m
; m
=gbuf_cont(m
))
1077 size
+= gbuf_len(m
);
1081 int append_copy(m1
, m2
, wait
)
1082 struct mbuf
*m1
, *m2
;
1085 if ((!(m1
->m_flags
& M_EXT
)) && (!(m2
->m_flags
& M_EXT
)) &&
1086 (m_trailingspace(m1
) >= m2
->m_len
)) {
1087 /* splat the data from one into the other */
1088 bcopy(mtod(m2
, caddr_t
), mtod(m1
, caddr_t
) + m1
->m_len
,
1090 m1
->m_len
+= m2
->m_len
;
1091 if (m1
->m_flags
& M_PKTHDR
)
1092 m1
->m_pkthdr
.len
+= m2
->m_len
;
1095 if ((m1
->m_next
= m_copym(m2
, 0, m2
->m_len
,
1096 (wait
)? M_WAIT
: M_DONTWAIT
)) == NULL
)
1102 Copy an mbuf chain, referencing existing external storage, if any.
1103 Leave space for a header in the new chain, if the space has been
1104 left in the origin chain.
1106 struct mbuf
*copy_pkt(mlist
, pad
)
1107 struct mbuf
*mlist
; /* the mbuf chain to be copied */
1108 int pad
; /* hint as to how long the header might be
1109 If pad is < 0, leave the same amount of space
1110 as there was in the original. */
1116 len
= m_leadingspace(mlist
);
1118 len
= min(pad
, m_leadingspace(mlist
));
1120 /* preserve space for the header at the beginning of the mbuf */
1122 mlist
->m_data
-= (len
);
1123 mlist
->m_len
+= (len
);
1124 if (mlist
->m_flags
& M_PKTHDR
)
1125 mlist
->m_pkthdr
.len
+= (len
);
1126 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1130 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1135 void gbuf_linkb(m1
, m2
)
1139 while (gbuf_cont(m1
) != 0)
1144 void gbuf_linkpkt(m1
, m2
)
1148 while (gbuf_next(m1
) != 0)
1158 while ((tmp_m
= m
) != 0) {
1160 gbuf_next(tmp_m
) = 0;
1166 /* free empty mbufs at the front of the chain */
1167 gbuf_t
*gbuf_strip(m
)
1172 while (m
&& gbuf_len(m
) == 0) {
1180 /**************************************/
1182 int ddp_adjmsg(m
, len
)
1187 gbuf_t
*curr_m
, *prev_m
;
1189 if (m
== (gbuf_t
*)0)
1193 for (curr_m
=m
; curr_m
;) {
1194 buf_len
= gbuf_len(curr_m
);
1195 if (len
< buf_len
) {
1196 gbuf_rinc(curr_m
,len
);
1200 gbuf_rinc(curr_m
,buf_len
);
1201 if ((curr_m
= gbuf_cont(curr_m
)) == 0) {
1207 } else if (len
< 0) {
1210 for (curr_m
=m
; gbuf_cont(curr_m
);
1211 prev_m
=curr_m
, curr_m
=gbuf_cont(curr_m
)) ;
1212 buf_len
= gbuf_len(curr_m
);
1213 if (len
< buf_len
) {
1214 gbuf_wdec(curr_m
,len
);
1219 gbuf_cont(prev_m
) = 0;
1230 * The message chain, m is grown in size by len contiguous bytes.
1231 * If len is non-negative, len bytes are added to the
1232 * end of the gbuf_t chain. If len is negative, the
1233 * bytes are added to the front. ddp_growmsg only adds bytes to
1234 * message blocks of the same type.
1235 * It returns a pointer to the new gbuf_t on sucess, 0 on failure.
1238 gbuf_t
*ddp_growmsg(mp
, len
)
1244 if ((m
= mp
) == (gbuf_t
*) 0)
1245 return ((gbuf_t
*) 0);
1249 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1250 return ((gbuf_t
*) 0);
1251 gbuf_set_type(d
, gbuf_type(m
));
1253 /* link in new gbuf_t */
1262 if ((count
= gbuf_msgsize(m
)) < 0)
1263 return ((gbuf_t
*) 0);
1264 /* find end of chain */
1265 for ( ; m
; m
= gbuf_cont(m
)) {
1266 if (gbuf_len(m
) >= count
)
1268 count
-= gbuf_len(m
);
1270 /* m now points to gbuf_t to add to */
1271 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1272 return ((gbuf_t
*) 0);
1273 gbuf_set_type(d
, gbuf_type(m
));
1274 /* link in new gbuf_t */
1275 gbuf_cont(d
) = gbuf_cont(m
);
1283 * return the MSG_IOCACK/MSG_IOCNAK. Note that the same message
1284 * block is used as the vehicle, and that if there is an error return,
1285 * then linked blocks are lopped off. BEWARE of multiple references.
1286 * Used by other appletalk modules, so it is not static!
1289 void ioc_ack(errno
, m
, gref
)
1292 register gref_t
*gref
;
1294 ioc_t
*iocbp
= (ioc_t
*)gbuf_rptr(m
);
1296 /*kprintf("ioc_ack: m=%x gref=%x errno=%d\n", m, gref, errno);*/
1297 if ((iocbp
->ioc_error
= errno
) != 0)
1298 { /* errno != 0, then there is an error, get rid of linked blocks! */
1301 gbuf_freem(gbuf_cont(m
));
1304 gbuf_set_type(m
, MSG_IOCNAK
);
1305 iocbp
->ioc_count
= 0; /* only make zero length if error */
1306 iocbp
->ioc_rval
= -1;
1308 gbuf_set_type(m
, MSG_IOCACK
);
1310 atalk_putnext(gref
, m
);
1314 static void ioccmd_t_32_to_64( ioccmd_t
*from_p
, user_ioccmd_t
*to_p
)
1316 to_p
->ic_cmd
= from_p
->ic_cmd
;
1317 to_p
->ic_timout
= from_p
->ic_timout
;
1318 to_p
->ic_len
= from_p
->ic_len
;
1319 to_p
->ic_dp
= CAST_USER_ADDR_T(from_p
->ic_dp
);
1323 static void ioccmd_t_64_to_32( user_ioccmd_t
*from_p
, ioccmd_t
*to_p
)
1325 to_p
->ic_cmd
= from_p
->ic_cmd
;
1326 to_p
->ic_timout
= from_p
->ic_timout
;
1327 to_p
->ic_len
= from_p
->ic_len
;
1328 to_p
->ic_dp
= CAST_DOWN(caddr_t
, from_p
->ic_dp
);