2 * Copyright (c) 2000-2004 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/sysctl.h>
58 #include <netat/sysglue.h>
59 #include <netat/appletalk.h>
60 #include <netat/ddp.h>
61 #include <netat/at_pcb.h>
62 #include <netat/at_var.h>
63 #include <netat/routing_tables.h>
64 #include <netat/debug.h>
66 extern struct atpcb ddp_head
;
67 extern lck_mtx_t
* atalk_mutex
;
70 ddp_putmsg(gref_t
*gref
, gbuf_t
*m
),
71 elap_wput(gref_t
*gref
, gbuf_t
*m
),
72 atp_wput(gref_t
*gref
, gbuf_t
*m
),
73 asp_wput(gref_t
*gref
, gbuf_t
*m
),
75 aurp_wput(gref_t
*gref
, gbuf_t
*m
),
77 adsp_wput(gref_t
*gref
, gbuf_t
*m
);
79 int atp_free_cluster_timeout_set
= 0;
82 void atalk_putnext(gref_t
*gref
, gbuf_t
*m
);
83 /* bms: make gref_close non static so its callable from kernel */
84 int gref_close(gref_t
*gref
);
86 SYSCTL_DECL(_net_appletalk
);
88 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, debug
, CTLFLAG_WR
,
89 &dbgBits
, dbgBits
, "AppleTalk Debug Flags");
90 volatile int RouterMix
= RT_MIX_DEFAULT
; /* default for nbr of ppsec */
91 SYSCTL_INT(_net_appletalk
, OID_AUTO
, routermix
, CTLFLAG_WR
,
92 (int *)&RouterMix
, 0, "Appletalk RouterMix");
93 at_ddp_stats_t at_ddp_stats
; /* DDP statistics */
94 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, ddpstats
, CTLFLAG_RD
,
95 &at_ddp_stats
, at_ddp_stats
, "AppleTalk DDP Stats");
97 static void ioccmd_t_32_to_64( ioccmd_t
*from_p
, user_ioccmd_t
*to_p
);
98 static void ioccmd_t_64_to_32( user_ioccmd_t
*from_p
, ioccmd_t
*to_p
);
101 caddr_t atp_free_cluster_list
= 0;
103 void gref_wput(gref
, m
)
107 switch (gref
->proto
) {
109 ddp_putmsg(gref
, m
); break;
111 elap_wput(gref
, m
); break;
113 atp_wput(gref
, m
); break;
115 asp_wput(gref
, m
); break;
118 aurp_wput(gref
, m
); break;
121 adsp_wput(gref
, m
); break;
123 if (gbuf_type(m
) == MSG_IOCTL
) {
124 gbuf_freem(gbuf_cont(m
));
126 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= -1;
127 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= EPROTOTYPE
;
128 gbuf_set_type(m
, MSG_IOCNAK
);
129 atalk_putnext(gref
, m
);
139 int _ATsocket(proto
, err
, proc
)
147 /* make sure the specified protocol id is valid */
150 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
151 BSD-style socket interface. */
162 #ifdef APPLETALK_DEBUG
163 kprintf("_ATsocket: error EPROTOTYPE =%d\n", *err
);
168 /* allocate a protocol channel */
169 if ((*err
= gref_alloc(&gref
)) != 0) {
170 #ifdef APPLETALK_DEBUG
171 kprintf("_ATsocket: error gref_open =%d\n", *err
);
176 gref
->pid
= proc_pid((struct proc
*)proc
);
178 /* open the specified protocol */
179 switch (gref
->proto
) {
181 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
182 BSD-style socket interface. */
185 *err
= atp_open(gref
, 1); break;
187 *err
= asp_open(gref
); break;
190 *err
= aurp_open(gref
); break;
193 *err
= adsp_open(gref
); break;
196 /* create the descriptor for the channel */
198 #ifdef APPLETALK_DEBUG
199 kprintf("_ATsocket: open failed for %d proto; err = %d\n",
202 gref
->proto
= ATPROTO_NONE
;
204 if (*err
|| (*err
= atalk_openref(gref
, &fd
, proc
))) {
205 #ifdef APPLETALK_DEBUG
206 kprintf("_ATsocket: error atalk_openref =%d\n", *err
);
208 (void)gref_close(gref
);
212 kprintf("_ATsocket: proto=%d return=%d fd=%d\n", proto, *err, fd);
217 int _ATgetmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
228 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) == 0) {
229 switch (gref
->proto
) {
231 rc
= ASPgetmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
);
235 rc
= AURPgetmsg(err
);
239 *err
= EPROTONOSUPPORT
;
245 /* kprintf("_ATgetmsg: return=%d\n", *err);*/
249 int _ATputmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
260 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) == 0) {
261 switch (gref
->proto
) {
263 rc
= ASPputmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
); break;
265 *err
= EPROTONOSUPPORT
; break;
270 /* kprintf("_ATputmsg: return=%d\n", *err); */
274 int _ATclose(fg
, proc
)
281 if ((err
= atalk_closeref(fg
, &gref
)) == 0) {
283 (void)gref_close(gref
);
290 int _ATrw(fp
, rw
, uio
, p
)
296 int err
, len
, clen
= 0, res
;
298 gbuf_t
*m
, *mhead
, *mprev
;
300 /* no need to get/drop iocount as the fp already has one */
301 if ((err
= atalk_getref_locked(fp
, 0, &gref
, p
, 1)) != 0)
304 // LP64todo - fix this!
305 if ((len
= uio_resid(uio
)) == 0)
309 if (rw
== UIO_READ
) {
310 KERNEL_DEBUG(DBG_ADSP_ATRW
, 0, gref
, len
, gref
->rdhead
, 0);
311 while ((gref
->errno
== 0) && ((mhead
= gref
->rdhead
) == 0)) {
312 gref
->sevents
|= POLLMSG
;
313 err
= msleep(&gref
->event
, atalk_mutex
, PSOCK
| PCATCH
, "AT read", 0);
314 gref
->sevents
&= ~POLLMSG
;
317 KERNEL_DEBUG(DBG_ADSP_ATRW
, 1, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
322 if ((gref
->rdhead
= gbuf_next(mhead
)) == 0)
325 KERNEL_DEBUG(DBG_ADSP_ATRW
, 2, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
328 //##### LD TEST 08/05
329 // simple_lock(&gref->lock);
331 gbuf_next(mhead
) = 0;
333 for (mprev
=0, m
=mhead
; m
&& len
; len
-=clen
) {
334 if ((clen
= gbuf_len(m
)) > 0) {
337 uio
->uio_rw
= UIO_READ
;
338 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
),
340 KERNEL_DEBUG(DBG_ADSP_ATRW
, 3, m
, clen
,
344 if (gbuf_len(m
) > len
) {
353 KERNEL_DEBUG(DBG_ADSP_ATRW
, 4, m
, gbuf_len(m
), mprev
, gref
->rdhead
);
355 gbuf_cont(mprev
) = 0;
358 if (gref
->rdhead
== 0)
360 gbuf_next(m
) = gref
->rdhead
;
366 // simple_unlock(&gref->lock);
368 if (gref
->writeable
) {
369 while (!(*gref
->writeable
)(gref
)) {
370 /* flow control on, wait to be enabled to write */
371 gref
->sevents
|= POLLSYNC
;
372 err
= msleep(&gref
->event
, atalk_mutex
, PSOCK
| PCATCH
, "AT write", 0);
373 gref
->sevents
&= ~POLLSYNC
;
380 /* allocate a buffer to copy in the write data */
381 if ((m
= gbuf_alloc(AT_WR_OFFSET
+len
, PRI_MED
)) == 0)
383 gbuf_rinc(m
,AT_WR_OFFSET
);
386 /* copy in the write data */
387 uio
->uio_rw
= UIO_WRITE
;
388 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
), len
, uio
))) {
389 #ifdef APPLETALK_DEBUG
390 kprintf("_ATrw: UIO_WRITE: res=%d\n", res
);
396 /* forward the write data to the appropriate protocol module */
403 int _ATread(fp
, uio
, cred
, flags
, p
)
413 stat
= _ATrw(fp
, UIO_READ
, uio
, p
);
418 int _ATwrite(fp
, uio
, cred
, flags
, p
)
428 stat
= _ATrw(fp
, UIO_WRITE
, uio
, p
);
434 /* Most of the processing from _ATioctl, so that it can be called
435 from the new ioctl code */
436 /* bms: update to be callable from kernel */
437 int at_ioctl(gref_t
*gref
, u_long cmd
, caddr_t arg
, int fromKernel
)
443 user_addr_t user_arg
;
444 user_ioccmd_t user_ioccmd
;
447 /* error if not for us */
448 if ((cmd
& 0xffff) != 0xff99)
451 size
= IOCPARM_LEN(cmd
);
452 if (size
!= sizeof(user_addr_t
))
455 user_arg
= *((user_addr_t
*)arg
);
457 /* copy in ioc command info */
458 is64bit
= proc_is64bit(current_proc());
461 bcopy (CAST_DOWN(caddr_t
, user_arg
), &tmp
, sizeof (tmp
));
462 ioccmd_t_32_to_64(&tmp
, &user_ioccmd
);
466 err
= copyin(user_arg
, (caddr_t
)&user_ioccmd
, sizeof(user_ioccmd
));
470 err
= copyin(user_arg
, (caddr_t
)&tmp
, sizeof(tmp
));
471 ioccmd_t_32_to_64(&tmp
, &user_ioccmd
);
474 #ifdef APPLETALK_DEBUG
475 kprintf("at_ioctl: err = %d, copyin(%llx, %x, %d)\n", err
,
476 user_arg
, (caddr_t
)&user_ioccmd
, sizeof(user_ioccmd
));
482 /* allocate a buffer to create an ioc command
483 first mbuf contains ioc command */
484 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) == 0)
486 gbuf_wset(m
, sizeof(ioc_t
)); /* mbuf->m_len */
487 gbuf_set_type(m
, MSG_IOCTL
); /* mbuf->m_type */
489 /* create the ioc command
490 second mbuf contains the actual ASP command */
491 if (user_ioccmd
.ic_len
) {
492 if ((gbuf_cont(m
) = gbuf_alloc(user_ioccmd
.ic_len
, PRI_HI
)) == 0) {
494 #ifdef APPLETALK_DEBUG
495 kprintf("at_ioctl: gbuf_alloc err=%d\n",ENOBUFS
);
499 gbuf_wset(gbuf_cont(m
), user_ioccmd
.ic_len
); /* mbuf->m_len */
501 bcopy (CAST_DOWN(caddr_t
, user_ioccmd
.ic_dp
), gbuf_rptr(gbuf_cont(m
)), user_ioccmd
.ic_len
);
503 if ((err
= copyin(user_ioccmd
.ic_dp
, (caddr_t
)gbuf_rptr(gbuf_cont(m
)), user_ioccmd
.ic_len
)) != 0) {
509 ioc
= (ioc_t
*) gbuf_rptr(m
);
510 ioc
->ioc_cmd
= user_ioccmd
.ic_cmd
;
511 ioc
->ioc_count
= user_ioccmd
.ic_len
;
515 /* send the ioc command to the appropriate recipient */
518 /* wait for the ioc ack */
519 while ((m
= gref
->ichead
) == 0) {
520 gref
->sevents
|= POLLPRI
;
521 #ifdef APPLETALK_DEBUG
522 kprintf("sleep gref = 0x%x\n", (unsigned)gref
);
524 err
= msleep(&gref
->iocevent
, atalk_mutex
, PSOCK
| PCATCH
, "AT ioctl", 0);
525 gref
->sevents
&= ~POLLPRI
;
527 #ifdef APPLETALK_DEBUG
528 kprintf("at_ioctl: EINTR\n");
535 if (gbuf_next(m
) == m
) /* error case */
538 gref
->ichead
= gbuf_next(m
);
541 #ifdef APPLETALK_DEBUG
542 kprintf("at_ioctl: woke up from ioc sleep gref = 0x%x\n",
546 /* process the ioc response */
547 ioc
= (ioc_t
*) gbuf_rptr(m
);
548 if ((err
= ioc
->ioc_error
) == 0) {
549 user_ioccmd
.ic_timout
= ioc
->ioc_rval
;
550 user_ioccmd
.ic_len
= 0;
551 mdata
= gbuf_cont(m
);
552 if (mdata
&& user_ioccmd
.ic_dp
) {
553 user_ioccmd
.ic_len
= gbuf_msgsize(mdata
);
554 for (len
= 0; mdata
; mdata
= gbuf_cont(mdata
)) {
556 bcopy (gbuf_rptr(mdata
), CAST_DOWN(caddr_t
, (user_ioccmd
.ic_dp
+ len
)), gbuf_len(mdata
));
558 if ((err
= copyout((caddr_t
)gbuf_rptr(mdata
), (user_ioccmd
.ic_dp
+ len
), gbuf_len(mdata
))) < 0) {
559 #ifdef APPLETALK_DEBUG
560 kprintf("at_ioctl: len=%d error copyout=%d from=%x to=%x gbuf_len=%x\n",
561 len
, err
, (caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&user_ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
566 len
+= gbuf_len(mdata
);
572 ioccmd_t_64_to_32(&user_ioccmd
, &tmp
);
573 bcopy (&tmp
, CAST_DOWN(caddr_t
, user_arg
), sizeof(tmp
));
577 err
= copyout((caddr_t
)&user_ioccmd
, user_arg
, sizeof(user_ioccmd
));
581 ioccmd_t_64_to_32(&user_ioccmd
, &tmp
);
582 err
= copyout((caddr_t
)&tmp
, user_arg
, sizeof(tmp
));
592 /*kprintf("at_ioctl: I_done=%d\n", err);*/
596 int _ATioctl(fp
, cmd
, arg
, proc
)
599 register caddr_t arg
;
606 /* No need to get a reference on fp as it already has one */
607 if ((err
= atalk_getref_locked(fp
, 0, &gref
, 0, 0)) != 0) {
608 #ifdef APPLETALK_DEBUG
609 kprintf("_ATioctl: atalk_getref err = %d\n", err
);
613 err
= at_ioctl(gref
, cmd
, arg
, 0);
620 int _ATselect(fp
, which
, wql
, proc
)
629 /* Radar 4128949: Drop the proc_fd lock here to avoid lock inversion issues with the other AT calls
630 * select() is already holding a reference on the fd, so it won't go away during the time it is unlocked.
635 /* no need to drop the iocount as select covers that */
636 err
= atalk_getref_locked(fp
, 0, &gref
, 0, 0);
639 /* Safe to re-grab the proc_fdlock at that point */
644 if (which
== FREAD
) {
645 if (gref
->rdhead
|| (gref
->readable
&& (*gref
->readable
)(gref
)))
648 gref
->sevents
|= POLLIN
;
649 selrecord(proc
, &gref
->si
, wql
);
652 else if (which
== POLLOUT
) {
653 if (gref
->writeable
) {
654 if ((*gref
->writeable
)(gref
))
657 gref
->sevents
|= POLLOUT
;
658 selrecord(proc
, &gref
->si
, wql
);
668 int _ATkqfilter(fp
, kn
, p
)
676 void atalk_putnext(gref
, m
)
682 /* *** potential leak? *** */
685 switch (gbuf_type(m
)) {
689 gbuf_next(gref
->ichead
) = m
;
692 if (gref
->sevents
& POLLPRI
) {
693 #ifdef APPLETALK_DEBUG
694 kprintf("wakeup gref = 0x%x\n", (unsigned)gref
);
696 wakeup(&gref
->iocevent
);
701 /* *** this processing was moved to atalk_notify *** */
702 panic("atalk_putnext receved MSG_ERROR");
709 gbuf_next(gref
->rdtail
) = m
;
713 if (gref
->sevents
& POLLMSG
) {
714 gref
->sevents
&= ~POLLMSG
;
715 wakeup(&gref
->event
);
717 if (gref
->sevents
& POLLIN
) {
718 gref
->sevents
&= ~POLLIN
;
719 selwakeup(&gref
->si
);
723 } /* switch gbuf_type(m) */
725 } /* atalk_putnext */
727 void atalk_enablew(gref
)
730 if (gref
->sevents
& POLLSYNC
)
731 wakeup(&gref
->event
);
734 void atalk_flush(gref
)
739 gbuf_freel(gref
->rdhead
);
743 gbuf_freel(gref
->ichead
);
749 * Notify an appletalk user of an asynchronous error;
750 * just wake up so that he can collect error status.
752 void atalk_notify(gref
, errno
)
753 register gref_t
*gref
;
757 if (gref
->atpcb_socket
) {
759 This section is patterned after udp_notify() in
762 gref
->atpcb_socket
->so_error
= errno
;
763 sorwakeup(gref
->atpcb_socket
);
764 sowwakeup(gref
->atpcb_socket
);
766 /* for ATP, ASP, and ADSP */
767 if (gref
->errno
== 0) {
769 /* clear out data waiting to be read */
771 gbuf_freel(gref
->rdhead
);
775 if (gref
->sevents
& POLLMSG
) {
776 gref
->sevents
&= ~POLLMSG
;
777 wakeup(&gref
->event
);
780 if (gref
->sevents
& POLLIN
) {
781 gref
->sevents
&= ~POLLIN
;
782 selwakeup(&gref
->si
);
788 void atalk_notify_sel(gref
)
792 if (gref
->sevents
& POLLIN
) {
793 gref
->sevents
&= ~POLLIN
;
794 selwakeup(&gref
->si
);
798 int atalk_peek(gref
, event
)
800 unsigned char *event
;
805 *event
= *gbuf_rptr(gref
->rdhead
);
813 static gbuf_t
*trace_msg
;
815 void atalk_settrace(str
, p1
, p2
, p3
, p4
, p5
)
822 sprintf(trace_buf
, str
, p1
, p2
, p3
, p4
, p5
);
823 len
= strlen(trace_buf
);
824 #ifdef APPLETALK_DEBUG
825 kprintf("atalk_settrace: gbufalloc size=%d\n", len
+1);
827 if ((m
= gbuf_alloc(len
+1, PRI_MED
)) == 0)
830 strcpy(gbuf_rptr(m
), trace_buf
);
832 for (nextm
=trace_msg
; gbuf_cont(nextm
); nextm
=gbuf_cont(nextm
)) ;
833 gbuf_cont(nextm
) = m
;
838 void atalk_gettrace(m
)
842 gbuf_cont(m
) = trace_msg
;
847 #define GREF_PER_BLK 32
848 static gref_t
*gref_free_list
= 0;
850 int gref_alloc(grefp
)
853 extern gbuf_t
*atp_resource_m
;
856 gref_t
*gref
, *gref_array
;
858 *grefp
= (gref_t
*)NULL
;
860 if (gref_free_list
== 0) {
861 #ifdef APPLETALK_DEBUG
862 kprintf("gref_alloc: gbufalloc size=%d\n", GREF_PER_BLK
*sizeof(gref_t
));
864 if ((m
= gbuf_alloc(GREF_PER_BLK
*sizeof(gref_t
),PRI_HI
)) == 0)
866 bzero(gbuf_rptr(m
), GREF_PER_BLK
*sizeof(gref_t
));
867 gref_array
= (gref_t
*)gbuf_rptr(m
);
868 for (i
=0; i
< GREF_PER_BLK
-1; i
++)
869 gref_array
[i
].atpcb_next
= (gref_t
*)&gref_array
[i
+1];
870 gbuf_cont(m
) = atp_resource_m
;
872 gref_array
[i
].atpcb_next
= gref_free_list
;
873 gref_free_list
= (gref_t
*)&gref_array
[0];
876 gref
= gref_free_list
;
877 gref_free_list
= gref
->atpcb_next
;
878 ATEVENTINIT(gref
->event
);
879 ATEVENTINIT(gref
->iocevent
);
881 /* *** just for now *** */
882 gref
->atpcb_socket
= (struct socket
*)NULL
;
888 /* bms: make gref_close callable from kernel */
889 int gref_close(gref_t
*gref
)
893 switch (gref
->proto
) {
895 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
896 BSD-style socket interface. */
899 rc
= atp_close(gref
, 1); break;
901 rc
= asp_close(gref
); break;
904 rc
= aurp_close(gref
); break;
908 rc
= adsp_close(gref
); break;
916 selthreadclear(&gref
->si
);
918 /* from original gref_free() */
919 bzero((char *)gref
, sizeof(gref_t
));
920 gref
->atpcb_next
= gref_free_list
;
921 gref_free_list
= gref
;
930 *** Some to be replaced with mbuf routines, some to be re-written
931 as mbuf routines (and moved to kern/uicp_mbuf.c or sys/mbuf.h?).
937 * LD 5/12/97 Added for MacOSX, defines a m_clattach function that:
938 * "Allocates an mbuf structure and attaches an external cluster."
941 struct mbuf
*m_clattach(extbuf
, extfree
, extsize
, extarg
, wait
)
943 void (*extfree
)(caddr_t
, u_int
, caddr_t
);
950 if ((m
= m_gethdr(wait
, MSG_DATA
)) == NULL
)
953 m
->m_ext
.ext_buf
= extbuf
;
954 m
->m_ext
.ext_free
= extfree
;
955 m
->m_ext
.ext_size
= extsize
;
956 m
->m_ext
.ext_arg
= extarg
;
957 m
->m_ext
.ext_refs
.forward
=
958 m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
968 temp fix for bug 2731148 - until this code is re-written to use standard clusters
969 Deletes any free clusters on the free list.
971 void atp_delete_free_clusters()
974 caddr_t cluster_list
;
977 /* check for free clusters on the free_cluster_list to be deleted */
978 MBUF_LOCK(); /* lock used by mbuf routines */
980 untimeout(&atp_delete_free_clusters
, NULL
);
981 atp_free_cluster_timeout_set
= 0;
983 cluster_list
= atp_free_cluster_list
;
984 atp_free_cluster_list
= 0;
988 while (cluster
= cluster_list
)
990 cluster_list
= *((caddr_t
*)cluster
);
991 FREE(cluster
, M_MCLUST
);
998 Used as the "free" routine for over-size clusters allocated using
999 m_lgbuf_alloc(). Called by m_free while under MBUF_LOCK.
1002 void m_lgbuf_free(buf
, size
, arg
)
1005 caddr_t arg
; /* not needed, but they're in m_free() */
1007 /* FREE(buf, M_MCLUST); - can't free here - called from m_free while under lock */
1009 /* move to free_cluster_list to be deleted later */
1010 caddr_t cluster
= (caddr_t
)buf
;
1012 /* don't need a lock because this is only called called from m_free which */
1013 /* is under MBUF_LOCK */
1014 *((caddr_t
*)cluster
) = atp_free_cluster_list
;
1015 atp_free_cluster_list
= cluster
;
1017 if (atp_free_cluster_timeout_set
== 0)
1019 atp_free_cluster_timeout_set
= 1;
1020 timeout(&atp_delete_free_clusters
, NULL
, (1 * HZ
));
1025 Used to allocate an mbuf when there is the possibility that it may
1026 need to be larger than the size of a standard cluster.
1029 struct mbuf
*m_lgbuf_alloc(size
, wait
)
1034 if (atp_free_cluster_list
)
1035 atp_delete_free_clusters(); /* delete any free clusters on the free list */
1037 /* If size is too large, allocate a cluster, otherwise, use the
1038 standard mbuf allocation routines.*/
1039 if (size
> MCLBYTES
) {
1042 (buf
= (void *)_MALLOC(size
, M_MCLUST
,
1043 (wait
)? M_WAITOK
: M_NOWAIT
))) {
1047 (m
= m_clattach(buf
, m_lgbuf_free
, size
, 0,
1048 (wait
)? M_WAIT
: M_DONTWAIT
))) {
1049 m_lgbuf_free(buf
, 0, 0);
1053 m
= m_gethdr(((wait
)? M_WAIT
: M_DONTWAIT
), MSG_DATA
);
1054 if (m
&& (size
> MHLEN
)) {
1055 MCLGET(m
, ((wait
)? M_WAIT
: M_DONTWAIT
));
1056 if (!(m
->m_flags
& M_EXT
)) {
1064 } /* m_lgbuf_alloc */
1067 gbuf_alloc() is a wrapper for m_lgbuf_alloc(), which is used to
1068 allocate an mbuf when there is the possibility that it may need
1069 to be larger than the size of a standard cluster.
1071 gbuf_alloc() sets the mbuf lengths, unlike the standard mbuf routines.
1074 gbuf_t
*gbuf_alloc_wait(size
, wait
)
1077 gbuf_t
*m
= (gbuf_t
*)m_lgbuf_alloc(size
, wait
);
1079 /* Standard mbuf allocation routines assume that the caller
1080 will set the size. */
1082 (struct mbuf
*)m
->m_pkthdr
.len
= size
;
1083 (struct mbuf
*)m
->m_len
= size
;
1094 for (size
=0; m
; m
=gbuf_cont(m
))
1095 size
+= gbuf_len(m
);
1099 int append_copy(m1
, m2
, wait
)
1100 struct mbuf
*m1
, *m2
;
1103 if ((!(m1
->m_flags
& M_EXT
)) && (!(m2
->m_flags
& M_EXT
)) &&
1104 (m_trailingspace(m1
) >= m2
->m_len
)) {
1105 /* splat the data from one into the other */
1106 bcopy(mtod(m2
, caddr_t
), mtod(m1
, caddr_t
) + m1
->m_len
,
1108 m1
->m_len
+= m2
->m_len
;
1109 if (m1
->m_flags
& M_PKTHDR
)
1110 m1
->m_pkthdr
.len
+= m2
->m_len
;
1113 if ((m1
->m_next
= m_copym(m2
, 0, m2
->m_len
,
1114 (wait
)? M_WAIT
: M_DONTWAIT
)) == NULL
)
1120 Copy an mbuf chain, referencing existing external storage, if any.
1121 Leave space for a header in the new chain, if the space has been
1122 left in the origin chain.
1124 struct mbuf
*copy_pkt(mlist
, pad
)
1125 struct mbuf
*mlist
; /* the mbuf chain to be copied */
1126 int pad
; /* hint as to how long the header might be
1127 If pad is < 0, leave the same amount of space
1128 as there was in the original. */
1134 len
= m_leadingspace(mlist
);
1136 len
= min(pad
, m_leadingspace(mlist
));
1138 /* preserve space for the header at the beginning of the mbuf */
1140 mlist
->m_data
-= (len
);
1141 mlist
->m_len
+= (len
);
1142 if (mlist
->m_flags
& M_PKTHDR
)
1143 mlist
->m_pkthdr
.len
+= (len
);
1144 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1148 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1153 void gbuf_linkb(m1
, m2
)
1157 while (gbuf_cont(m1
) != 0)
1162 void gbuf_linkpkt(m1
, m2
)
1166 while (gbuf_next(m1
) != 0)
1176 while ((tmp_m
= m
) != 0) {
1178 gbuf_next(tmp_m
) = 0;
1184 /* free empty mbufs at the front of the chain */
1185 gbuf_t
*gbuf_strip(m
)
1190 while (m
&& gbuf_len(m
) == 0) {
1198 /**************************************/
1200 int ddp_adjmsg(m
, len
)
1205 gbuf_t
*curr_m
, *prev_m
;
1207 if (m
== (gbuf_t
*)0)
1211 for (curr_m
=m
; curr_m
;) {
1212 buf_len
= gbuf_len(curr_m
);
1213 if (len
< buf_len
) {
1214 gbuf_rinc(curr_m
,len
);
1218 gbuf_rinc(curr_m
,buf_len
);
1219 if ((curr_m
= gbuf_cont(curr_m
)) == 0) {
1225 } else if (len
< 0) {
1228 for (curr_m
=m
; gbuf_cont(curr_m
);
1229 prev_m
=curr_m
, curr_m
=gbuf_cont(curr_m
)) ;
1230 buf_len
= gbuf_len(curr_m
);
1231 if (len
< buf_len
) {
1232 gbuf_wdec(curr_m
,len
);
1237 gbuf_cont(prev_m
) = 0;
1247 * The message chain, m is grown in size by len contiguous bytes.
1248 * If len is non-negative, len bytes are added to the
1249 * end of the gbuf_t chain. If len is negative, the
1250 * bytes are added to the front. ddp_growmsg only adds bytes to
1251 * message blocks of the same type.
1252 * It returns a pointer to the new gbuf_t on sucess, 0 on failure.
1255 gbuf_t
*ddp_growmsg(mp
, len
)
1261 if ((m
= mp
) == (gbuf_t
*) 0)
1262 return ((gbuf_t
*) 0);
1266 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1267 return ((gbuf_t
*) 0);
1268 gbuf_set_type(d
, gbuf_type(m
));
1270 /* link in new gbuf_t */
1279 if ((count
= gbuf_msgsize(m
)) < 0)
1280 return ((gbuf_t
*) 0);
1281 /* find end of chain */
1282 for ( ; m
; m
= gbuf_cont(m
)) {
1283 if (gbuf_len(m
) >= count
)
1285 count
-= gbuf_len(m
);
1287 /* m now points to gbuf_t to add to */
1288 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1289 return ((gbuf_t
*) 0);
1290 gbuf_set_type(d
, gbuf_type(m
));
1291 /* link in new gbuf_t */
1292 gbuf_cont(d
) = gbuf_cont(m
);
1300 * return the MSG_IOCACK/MSG_IOCNAK. Note that the same message
1301 * block is used as the vehicle, and that if there is an error return,
1302 * then linked blocks are lopped off. BEWARE of multiple references.
1303 * Used by other appletalk modules, so it is not static!
1306 void ioc_ack(errno
, m
, gref
)
1309 register gref_t
*gref
;
1311 ioc_t
*iocbp
= (ioc_t
*)gbuf_rptr(m
);
1313 /*kprintf("ioc_ack: m=%x gref=%x errno=%d\n", m, gref, errno);*/
1314 if ((iocbp
->ioc_error
= errno
) != 0)
1315 { /* errno != 0, then there is an error, get rid of linked blocks! */
1318 gbuf_freem(gbuf_cont(m
));
1321 gbuf_set_type(m
, MSG_IOCNAK
);
1322 iocbp
->ioc_count
= 0; /* only make zero length if error */
1323 iocbp
->ioc_rval
= -1;
1325 gbuf_set_type(m
, MSG_IOCACK
);
1327 atalk_putnext(gref
, m
);
1331 static void ioccmd_t_32_to_64( ioccmd_t
*from_p
, user_ioccmd_t
*to_p
)
1333 to_p
->ic_cmd
= from_p
->ic_cmd
;
1334 to_p
->ic_timout
= from_p
->ic_timout
;
1335 to_p
->ic_len
= from_p
->ic_len
;
1336 to_p
->ic_dp
= CAST_USER_ADDR_T(from_p
->ic_dp
);
1340 static void ioccmd_t_64_to_32( user_ioccmd_t
*from_p
, ioccmd_t
*to_p
)
1342 to_p
->ic_cmd
= from_p
->ic_cmd
;
1343 to_p
->ic_timout
= from_p
->ic_timout
;
1344 to_p
->ic_len
= from_p
->ic_len
;
1345 to_p
->ic_dp
= CAST_DOWN(caddr_t
, from_p
->ic_dp
);