2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1995 Apple Computer, Inc.
26 * Created, March 17, 1997 by Tuyen Nguyen for MacOSX.
29 #include <sys/errno.h>
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <machine/spl.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/filedesc.h>
37 #include <sys/fcntl.h>
38 #include <sys/file_internal.h>
40 #include <sys/ioctl.h>
41 #include <sys/malloc.h>
42 #include <kern/locks.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/ioccom.h>
46 #include <sys/uio_internal.h>
48 #include <sys/sysctl.h>
52 #include <netat/sysglue.h>
53 #include <netat/appletalk.h>
54 #include <netat/ddp.h>
55 #include <netat/at_pcb.h>
56 #include <netat/at_var.h>
57 #include <netat/routing_tables.h>
58 #include <netat/debug.h>
60 extern struct atpcb ddp_head
;
61 extern lck_mtx_t
* atalk_mutex
;
64 ddp_putmsg(gref_t
*gref
, gbuf_t
*m
),
65 elap_wput(gref_t
*gref
, gbuf_t
*m
),
66 atp_wput(gref_t
*gref
, gbuf_t
*m
),
67 asp_wput(gref_t
*gref
, gbuf_t
*m
),
69 aurp_wput(gref_t
*gref
, gbuf_t
*m
),
71 adsp_wput(gref_t
*gref
, gbuf_t
*m
);
73 int atp_free_cluster_timeout_set
= 0;
76 void atalk_putnext(gref_t
*gref
, gbuf_t
*m
);
77 /* bms: make gref_close non static so its callable from kernel */
78 int gref_close(gref_t
*gref
);
80 SYSCTL_DECL(_net_appletalk
);
82 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, debug
, CTLFLAG_WR
,
83 &dbgBits
, dbgBits
, "AppleTalk Debug Flags");
84 volatile int RouterMix
= RT_MIX_DEFAULT
; /* default for nbr of ppsec */
85 SYSCTL_INT(_net_appletalk
, OID_AUTO
, routermix
, CTLFLAG_WR
,
86 (int *)&RouterMix
, 0, "Appletalk RouterMix");
87 at_ddp_stats_t at_ddp_stats
; /* DDP statistics */
88 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, ddpstats
, CTLFLAG_RD
,
89 &at_ddp_stats
, at_ddp_stats
, "AppleTalk DDP Stats");
91 static void ioccmd_t_32_to_64( ioccmd_t
*from_p
, user_ioccmd_t
*to_p
);
92 static void ioccmd_t_64_to_32( user_ioccmd_t
*from_p
, ioccmd_t
*to_p
);
95 caddr_t atp_free_cluster_list
= 0;
97 void gref_wput(gref
, m
)
101 switch (gref
->proto
) {
103 ddp_putmsg(gref
, m
); break;
105 elap_wput(gref
, m
); break;
107 atp_wput(gref
, m
); break;
109 asp_wput(gref
, m
); break;
112 aurp_wput(gref
, m
); break;
115 adsp_wput(gref
, m
); break;
117 if (gbuf_type(m
) == MSG_IOCTL
) {
118 gbuf_freem(gbuf_cont(m
));
120 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= -1;
121 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= EPROTOTYPE
;
122 gbuf_set_type(m
, MSG_IOCNAK
);
123 atalk_putnext(gref
, m
);
133 int _ATsocket(proto
, err
, proc
)
141 /* make sure the specified protocol id is valid */
144 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
145 BSD-style socket interface. */
154 #ifdef APPLETALK_DEBUG
155 kprintf("_ATsocket: error EPROTOTYPE =%d\n", *err
);
160 /* allocate a protocol channel */
161 if ((*err
= gref_alloc(&gref
)) != 0) {
162 #ifdef APPLETALK_DEBUG
163 kprintf("_ATsocket: error gref_open =%d\n", *err
);
168 gref
->pid
= proc_pid((struct proc
*)proc
);
170 /* open the specified protocol */
171 switch (gref
->proto
) {
173 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
174 BSD-style socket interface. */
177 *err
= atp_open(gref
, 1); break;
179 *err
= asp_open(gref
); break;
182 *err
= aurp_open(gref
); break;
185 *err
= adsp_open(gref
); break;
188 /* create the descriptor for the channel */
190 #ifdef APPLETALK_DEBUG
191 kprintf("_ATsocket: open failed for %d proto; err = %d\n",
194 gref
->proto
= ATPROTO_NONE
;
196 if (*err
|| (*err
= atalk_openref(gref
, &fd
, proc
))) {
197 #ifdef APPLETALK_DEBUG
198 kprintf("_ATsocket: error atalk_openref =%d\n", *err
);
200 (void)gref_close(gref
);
204 kprintf("_ATsocket: proto=%d return=%d fd=%d\n", proto, *err, fd);
209 int _ATgetmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
220 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) == 0) {
221 switch (gref
->proto
) {
223 rc
= ASPgetmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
);
227 rc
= AURPgetmsg(err
);
231 *err
= EPROTONOSUPPORT
;
237 /* kprintf("_ATgetmsg: return=%d\n", *err);*/
241 int _ATputmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
252 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) == 0) {
253 switch (gref
->proto
) {
255 rc
= ASPputmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
); break;
257 *err
= EPROTONOSUPPORT
; break;
262 /* kprintf("_ATputmsg: return=%d\n", *err); */
266 int _ATclose(fg
, proc
)
273 if ((err
= atalk_closeref(fg
, &gref
)) == 0) {
275 (void)gref_close(gref
);
282 int _ATrw(fp
, rw
, uio
, p
)
288 int err
, len
, clen
= 0, res
;
290 gbuf_t
*m
, *mhead
, *mprev
;
292 /* no need to get/drop iocount as the fp already has one */
293 if ((err
= atalk_getref_locked(fp
, 0, &gref
, p
, 1)) != 0)
296 // LP64todo - fix this!
297 if ((len
= uio_resid(uio
)) == 0)
301 if (rw
== UIO_READ
) {
302 KERNEL_DEBUG(DBG_ADSP_ATRW
, 0, gref
, len
, gref
->rdhead
, 0);
303 while ((gref
->errno
== 0) && ((mhead
= gref
->rdhead
) == 0)) {
304 gref
->sevents
|= POLLMSG
;
305 err
= msleep(&gref
->event
, atalk_mutex
, PSOCK
| PCATCH
, "AT read", 0);
306 gref
->sevents
&= ~POLLMSG
;
309 KERNEL_DEBUG(DBG_ADSP_ATRW
, 1, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
314 if ((gref
->rdhead
= gbuf_next(mhead
)) == 0)
317 KERNEL_DEBUG(DBG_ADSP_ATRW
, 2, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
320 //##### LD TEST 08/05
321 // simple_lock(&gref->lock);
323 gbuf_next(mhead
) = 0;
325 for (mprev
=0, m
=mhead
; m
&& len
; len
-=clen
) {
326 if ((clen
= gbuf_len(m
)) > 0) {
329 uio
->uio_rw
= UIO_READ
;
330 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
),
332 KERNEL_DEBUG(DBG_ADSP_ATRW
, 3, m
, clen
,
336 if (gbuf_len(m
) > len
) {
345 KERNEL_DEBUG(DBG_ADSP_ATRW
, 4, m
, gbuf_len(m
), mprev
, gref
->rdhead
);
347 gbuf_cont(mprev
) = 0;
350 if (gref
->rdhead
== 0)
352 gbuf_next(m
) = gref
->rdhead
;
358 // simple_unlock(&gref->lock);
360 if (gref
->writeable
) {
361 while (!(*gref
->writeable
)(gref
)) {
362 /* flow control on, wait to be enabled to write */
363 gref
->sevents
|= POLLSYNC
;
364 err
= msleep(&gref
->event
, atalk_mutex
, PSOCK
| PCATCH
, "AT write", 0);
365 gref
->sevents
&= ~POLLSYNC
;
372 /* allocate a buffer to copy in the write data */
373 if ((m
= gbuf_alloc(AT_WR_OFFSET
+len
, PRI_MED
)) == 0)
375 gbuf_rinc(m
,AT_WR_OFFSET
);
378 /* copy in the write data */
379 uio
->uio_rw
= UIO_WRITE
;
380 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
), len
, uio
))) {
381 #ifdef APPLETALK_DEBUG
382 kprintf("_ATrw: UIO_WRITE: res=%d\n", res
);
388 /* forward the write data to the appropriate protocol module */
395 int _ATread(fp
, uio
, cred
, flags
, p
)
405 stat
= _ATrw(fp
, UIO_READ
, uio
, p
);
410 int _ATwrite(fp
, uio
, cred
, flags
, p
)
420 stat
= _ATrw(fp
, UIO_WRITE
, uio
, p
);
426 /* Most of the processing from _ATioctl, so that it can be called
427 from the new ioctl code */
428 /* bms: update to be callable from kernel */
429 int at_ioctl(gref_t
*gref
, u_long cmd
, caddr_t arg
, int fromKernel
)
435 user_addr_t user_arg
;
436 user_ioccmd_t user_ioccmd
;
439 /* error if not for us */
440 if ((cmd
& 0xffff) != 0xff99)
443 size
= IOCPARM_LEN(cmd
);
444 if (size
!= sizeof(user_addr_t
))
447 user_arg
= *((user_addr_t
*)arg
);
449 /* copy in ioc command info */
450 is64bit
= proc_is64bit(current_proc());
453 bcopy (CAST_DOWN(caddr_t
, user_arg
), &tmp
, sizeof (tmp
));
454 ioccmd_t_32_to_64(&tmp
, &user_ioccmd
);
458 err
= copyin(user_arg
, (caddr_t
)&user_ioccmd
, sizeof(user_ioccmd
));
462 err
= copyin(user_arg
, (caddr_t
)&tmp
, sizeof(tmp
));
463 ioccmd_t_32_to_64(&tmp
, &user_ioccmd
);
466 #ifdef APPLETALK_DEBUG
467 kprintf("at_ioctl: err = %d, copyin(%llx, %x, %d)\n", err
,
468 user_arg
, (caddr_t
)&user_ioccmd
, sizeof(user_ioccmd
));
474 /* allocate a buffer to create an ioc command
475 first mbuf contains ioc command */
476 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) == 0)
478 gbuf_wset(m
, sizeof(ioc_t
)); /* mbuf->m_len */
479 gbuf_set_type(m
, MSG_IOCTL
); /* mbuf->m_type */
481 /* create the ioc command
482 second mbuf contains the actual ASP command */
483 if (user_ioccmd
.ic_len
) {
484 if ((gbuf_cont(m
) = gbuf_alloc(user_ioccmd
.ic_len
, PRI_HI
)) == 0) {
486 #ifdef APPLETALK_DEBUG
487 kprintf("at_ioctl: gbuf_alloc err=%d\n",ENOBUFS
);
491 gbuf_wset(gbuf_cont(m
), user_ioccmd
.ic_len
); /* mbuf->m_len */
493 bcopy (CAST_DOWN(caddr_t
, user_ioccmd
.ic_dp
), gbuf_rptr(gbuf_cont(m
)), user_ioccmd
.ic_len
);
495 if ((err
= copyin(user_ioccmd
.ic_dp
, (caddr_t
)gbuf_rptr(gbuf_cont(m
)), user_ioccmd
.ic_len
)) != 0) {
501 ioc
= (ioc_t
*) gbuf_rptr(m
);
502 ioc
->ioc_cmd
= user_ioccmd
.ic_cmd
;
503 ioc
->ioc_count
= user_ioccmd
.ic_len
;
507 /* send the ioc command to the appropriate recipient */
510 /* wait for the ioc ack */
511 while ((m
= gref
->ichead
) == 0) {
512 gref
->sevents
|= POLLPRI
;
513 #ifdef APPLETALK_DEBUG
514 kprintf("sleep gref = 0x%x\n", (unsigned)gref
);
516 err
= msleep(&gref
->iocevent
, atalk_mutex
, PSOCK
| PCATCH
, "AT ioctl", 0);
517 gref
->sevents
&= ~POLLPRI
;
519 #ifdef APPLETALK_DEBUG
520 kprintf("at_ioctl: EINTR\n");
527 if (gbuf_next(m
) == m
) /* error case */
530 gref
->ichead
= gbuf_next(m
);
533 #ifdef APPLETALK_DEBUG
534 kprintf("at_ioctl: woke up from ioc sleep gref = 0x%x\n",
538 /* process the ioc response */
539 ioc
= (ioc_t
*) gbuf_rptr(m
);
540 if ((err
= ioc
->ioc_error
) == 0) {
541 user_ioccmd
.ic_timout
= ioc
->ioc_rval
;
542 user_ioccmd
.ic_len
= 0;
543 mdata
= gbuf_cont(m
);
544 if (mdata
&& user_ioccmd
.ic_dp
) {
545 user_ioccmd
.ic_len
= gbuf_msgsize(mdata
);
546 for (len
= 0; mdata
; mdata
= gbuf_cont(mdata
)) {
548 bcopy (gbuf_rptr(mdata
), CAST_DOWN(caddr_t
, (user_ioccmd
.ic_dp
+ len
)), gbuf_len(mdata
));
550 if ((err
= copyout((caddr_t
)gbuf_rptr(mdata
), (user_ioccmd
.ic_dp
+ len
), gbuf_len(mdata
))) < 0) {
551 #ifdef APPLETALK_DEBUG
552 kprintf("at_ioctl: len=%d error copyout=%d from=%x to=%x gbuf_len=%x\n",
553 len
, err
, (caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&user_ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
558 len
+= gbuf_len(mdata
);
564 ioccmd_t_64_to_32(&user_ioccmd
, &tmp
);
565 bcopy (&tmp
, CAST_DOWN(caddr_t
, user_arg
), sizeof(tmp
));
569 err
= copyout((caddr_t
)&user_ioccmd
, user_arg
, sizeof(user_ioccmd
));
573 ioccmd_t_64_to_32(&user_ioccmd
, &tmp
);
574 err
= copyout((caddr_t
)&tmp
, user_arg
, sizeof(tmp
));
584 /*kprintf("at_ioctl: I_done=%d\n", err);*/
588 int _ATioctl(fp
, cmd
, arg
, proc
)
591 register caddr_t arg
;
598 /* No need to get a reference on fp as it already has one */
599 if ((err
= atalk_getref_locked(fp
, 0, &gref
, 0, 0)) != 0) {
600 #ifdef APPLETALK_DEBUG
601 kprintf("_ATioctl: atalk_getref err = %d\n", err
);
605 err
= at_ioctl(gref
, cmd
, arg
, 0);
612 int _ATselect(fp
, which
, wql
, proc
)
621 /* Radar 4128949: Drop the proc_fd lock here to avoid lock inversion issues with the other AT calls
622 * select() is already holding a reference on the fd, so it won't go away during the time it is unlocked.
627 /* no need to drop the iocount as select covers that */
628 err
= atalk_getref_locked(fp
, 0, &gref
, 0, 0);
631 /* Safe to re-grab the proc_fdlock at that point */
636 if (which
== FREAD
) {
637 if (gref
->rdhead
|| (gref
->readable
&& (*gref
->readable
)(gref
)))
640 gref
->sevents
|= POLLIN
;
641 selrecord(proc
, &gref
->si
, wql
);
644 else if (which
== POLLOUT
) {
645 if (gref
->writeable
) {
646 if ((*gref
->writeable
)(gref
))
649 gref
->sevents
|= POLLOUT
;
650 selrecord(proc
, &gref
->si
, wql
);
660 int _ATkqfilter(fp
, kn
, p
)
668 void atalk_putnext(gref
, m
)
674 /* *** potential leak? *** */
677 switch (gbuf_type(m
)) {
681 gbuf_next(gref
->ichead
) = m
;
684 if (gref
->sevents
& POLLPRI
) {
685 #ifdef APPLETALK_DEBUG
686 kprintf("wakeup gref = 0x%x\n", (unsigned)gref
);
688 wakeup(&gref
->iocevent
);
693 /* *** this processing was moved to atalk_notify *** */
694 panic("atalk_putnext receved MSG_ERROR");
701 gbuf_next(gref
->rdtail
) = m
;
705 if (gref
->sevents
& POLLMSG
) {
706 gref
->sevents
&= ~POLLMSG
;
707 wakeup(&gref
->event
);
709 if (gref
->sevents
& POLLIN
) {
710 gref
->sevents
&= ~POLLIN
;
711 selwakeup(&gref
->si
);
715 } /* switch gbuf_type(m) */
717 } /* atalk_putnext */
719 void atalk_enablew(gref
)
722 if (gref
->sevents
& POLLSYNC
)
723 wakeup(&gref
->event
);
726 void atalk_flush(gref
)
731 gbuf_freel(gref
->rdhead
);
735 gbuf_freel(gref
->ichead
);
741 * Notify an appletalk user of an asynchronous error;
742 * just wake up so that he can collect error status.
744 void atalk_notify(gref
, errno
)
745 register gref_t
*gref
;
749 if (gref
->atpcb_socket
) {
751 This section is patterned after udp_notify() in
754 gref
->atpcb_socket
->so_error
= errno
;
755 sorwakeup(gref
->atpcb_socket
);
756 sowwakeup(gref
->atpcb_socket
);
758 /* for ATP, ASP, and ADSP */
759 if (gref
->errno
== 0) {
761 /* clear out data waiting to be read */
763 gbuf_freel(gref
->rdhead
);
767 if (gref
->sevents
& POLLMSG
) {
768 gref
->sevents
&= ~POLLMSG
;
769 wakeup(&gref
->event
);
772 if (gref
->sevents
& POLLIN
) {
773 gref
->sevents
&= ~POLLIN
;
774 selwakeup(&gref
->si
);
780 void atalk_notify_sel(gref
)
784 if (gref
->sevents
& POLLIN
) {
785 gref
->sevents
&= ~POLLIN
;
786 selwakeup(&gref
->si
);
790 int atalk_peek(gref
, event
)
792 unsigned char *event
;
797 *event
= *gbuf_rptr(gref
->rdhead
);
805 static gbuf_t
*trace_msg
;
807 void atalk_settrace(str
, p1
, p2
, p3
, p4
, p5
)
814 sprintf(trace_buf
, str
, p1
, p2
, p3
, p4
, p5
);
815 len
= strlen(trace_buf
);
816 #ifdef APPLETALK_DEBUG
817 kprintf("atalk_settrace: gbufalloc size=%d\n", len
+1);
819 if ((m
= gbuf_alloc(len
+1, PRI_MED
)) == 0)
822 strcpy(gbuf_rptr(m
), trace_buf
);
824 for (nextm
=trace_msg
; gbuf_cont(nextm
); nextm
=gbuf_cont(nextm
)) ;
825 gbuf_cont(nextm
) = m
;
830 void atalk_gettrace(m
)
834 gbuf_cont(m
) = trace_msg
;
839 #define GREF_PER_BLK 32
840 static gref_t
*gref_free_list
= 0;
842 int gref_alloc(grefp
)
845 extern gbuf_t
*atp_resource_m
;
848 gref_t
*gref
, *gref_array
;
850 *grefp
= (gref_t
*)NULL
;
852 if (gref_free_list
== 0) {
853 #ifdef APPLETALK_DEBUG
854 kprintf("gref_alloc: gbufalloc size=%d\n", GREF_PER_BLK
*sizeof(gref_t
));
856 if ((m
= gbuf_alloc(GREF_PER_BLK
*sizeof(gref_t
),PRI_HI
)) == 0)
858 bzero(gbuf_rptr(m
), GREF_PER_BLK
*sizeof(gref_t
));
859 gref_array
= (gref_t
*)gbuf_rptr(m
);
860 for (i
=0; i
< GREF_PER_BLK
-1; i
++)
861 gref_array
[i
].atpcb_next
= (gref_t
*)&gref_array
[i
+1];
862 gbuf_cont(m
) = atp_resource_m
;
864 gref_array
[i
].atpcb_next
= gref_free_list
;
865 gref_free_list
= (gref_t
*)&gref_array
[0];
868 gref
= gref_free_list
;
869 gref_free_list
= gref
->atpcb_next
;
870 ATEVENTINIT(gref
->event
);
871 ATEVENTINIT(gref
->iocevent
);
873 /* *** just for now *** */
874 gref
->atpcb_socket
= (struct socket
*)NULL
;
880 /* bms: make gref_close callable from kernel */
881 int gref_close(gref_t
*gref
)
885 switch (gref
->proto
) {
887 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
888 BSD-style socket interface. */
891 rc
= atp_close(gref
, 1); break;
893 rc
= asp_close(gref
); break;
896 rc
= aurp_close(gref
); break;
900 rc
= adsp_close(gref
); break;
908 selthreadclear(&gref
->si
);
910 /* from original gref_free() */
911 bzero((char *)gref
, sizeof(gref_t
));
912 gref
->atpcb_next
= gref_free_list
;
913 gref_free_list
= gref
;
922 *** Some to be replaced with mbuf routines, some to be re-written
923 as mbuf routines (and moved to kern/uicp_mbuf.c or sys/mbuf.h?).
929 * LD 5/12/97 Added for MacOSX, defines a m_clattach function that:
930 * "Allocates an mbuf structure and attaches an external cluster."
933 struct mbuf
*m_clattach(extbuf
, extfree
, extsize
, extarg
, wait
)
935 void (*extfree
)(caddr_t
, u_int
, caddr_t
);
942 if ((m
= m_gethdr(wait
, MSG_DATA
)) == NULL
)
945 m
->m_ext
.ext_buf
= extbuf
;
946 m
->m_ext
.ext_free
= extfree
;
947 m
->m_ext
.ext_size
= extsize
;
948 m
->m_ext
.ext_arg
= extarg
;
949 m
->m_ext
.ext_refs
.forward
=
950 m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
960 temp fix for bug 2731148 - until this code is re-written to use standard clusters
961 Deletes any free clusters on the free list.
963 void atp_delete_free_clusters()
966 caddr_t cluster_list
;
969 /* check for free clusters on the free_cluster_list to be deleted */
970 MBUF_LOCK(); /* lock used by mbuf routines */
972 untimeout(&atp_delete_free_clusters
, NULL
);
973 atp_free_cluster_timeout_set
= 0;
975 cluster_list
= atp_free_cluster_list
;
976 atp_free_cluster_list
= 0;
980 while (cluster
= cluster_list
)
982 cluster_list
= *((caddr_t
*)cluster
);
983 FREE(cluster
, M_MCLUST
);
990 Used as the "free" routine for over-size clusters allocated using
991 m_lgbuf_alloc(). Called by m_free while under MBUF_LOCK.
994 void m_lgbuf_free(buf
, size
, arg
)
997 caddr_t arg
; /* not needed, but they're in m_free() */
999 /* FREE(buf, M_MCLUST); - can't free here - called from m_free while under lock */
1001 /* move to free_cluster_list to be deleted later */
1002 caddr_t cluster
= (caddr_t
)buf
;
1004 /* don't need a lock because this is only called called from m_free which */
1005 /* is under MBUF_LOCK */
1006 *((caddr_t
*)cluster
) = atp_free_cluster_list
;
1007 atp_free_cluster_list
= cluster
;
1009 if (atp_free_cluster_timeout_set
== 0)
1011 atp_free_cluster_timeout_set
= 1;
1012 timeout(&atp_delete_free_clusters
, NULL
, (1 * HZ
));
1017 Used to allocate an mbuf when there is the possibility that it may
1018 need to be larger than the size of a standard cluster.
1021 struct mbuf
*m_lgbuf_alloc(size
, wait
)
1026 if (atp_free_cluster_list
)
1027 atp_delete_free_clusters(); /* delete any free clusters on the free list */
1029 /* If size is too large, allocate a cluster, otherwise, use the
1030 standard mbuf allocation routines.*/
1031 if (size
> MCLBYTES
) {
1034 (buf
= (void *)_MALLOC(size
, M_MCLUST
,
1035 (wait
)? M_WAITOK
: M_NOWAIT
))) {
1039 (m
= m_clattach(buf
, m_lgbuf_free
, size
, 0,
1040 (wait
)? M_WAIT
: M_DONTWAIT
))) {
1041 m_lgbuf_free(buf
, 0, 0);
1045 m
= m_gethdr(((wait
)? M_WAIT
: M_DONTWAIT
), MSG_DATA
);
1046 if (m
&& (size
> MHLEN
)) {
1047 MCLGET(m
, ((wait
)? M_WAIT
: M_DONTWAIT
));
1048 if (!(m
->m_flags
& M_EXT
)) {
1056 } /* m_lgbuf_alloc */
1059 gbuf_alloc() is a wrapper for m_lgbuf_alloc(), which is used to
1060 allocate an mbuf when there is the possibility that it may need
1061 to be larger than the size of a standard cluster.
1063 gbuf_alloc() sets the mbuf lengths, unlike the standard mbuf routines.
1066 gbuf_t
*gbuf_alloc_wait(size
, wait
)
1069 gbuf_t
*m
= (gbuf_t
*)m_lgbuf_alloc(size
, wait
);
1071 /* Standard mbuf allocation routines assume that the caller
1072 will set the size. */
1074 (struct mbuf
*)m
->m_pkthdr
.len
= size
;
1075 (struct mbuf
*)m
->m_len
= size
;
1086 for (size
=0; m
; m
=gbuf_cont(m
))
1087 size
+= gbuf_len(m
);
1091 int append_copy(m1
, m2
, wait
)
1092 struct mbuf
*m1
, *m2
;
1095 if ((!(m1
->m_flags
& M_EXT
)) && (!(m2
->m_flags
& M_EXT
)) &&
1096 (m_trailingspace(m1
) >= m2
->m_len
)) {
1097 /* splat the data from one into the other */
1098 bcopy(mtod(m2
, caddr_t
), mtod(m1
, caddr_t
) + m1
->m_len
,
1100 m1
->m_len
+= m2
->m_len
;
1101 if (m1
->m_flags
& M_PKTHDR
)
1102 m1
->m_pkthdr
.len
+= m2
->m_len
;
1105 if ((m1
->m_next
= m_copym(m2
, 0, m2
->m_len
,
1106 (wait
)? M_WAIT
: M_DONTWAIT
)) == NULL
)
1112 Copy an mbuf chain, referencing existing external storage, if any.
1113 Leave space for a header in the new chain, if the space has been
1114 left in the origin chain.
1116 struct mbuf
*copy_pkt(mlist
, pad
)
1117 struct mbuf
*mlist
; /* the mbuf chain to be copied */
1118 int pad
; /* hint as to how long the header might be
1119 If pad is < 0, leave the same amount of space
1120 as there was in the original. */
1126 len
= m_leadingspace(mlist
);
1128 len
= min(pad
, m_leadingspace(mlist
));
1130 /* preserve space for the header at the beginning of the mbuf */
1132 mlist
->m_data
-= (len
);
1133 mlist
->m_len
+= (len
);
1134 if (mlist
->m_flags
& M_PKTHDR
)
1135 mlist
->m_pkthdr
.len
+= (len
);
1136 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1140 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1145 void gbuf_linkb(m1
, m2
)
1149 while (gbuf_cont(m1
) != 0)
1154 void gbuf_linkpkt(m1
, m2
)
1158 while (gbuf_next(m1
) != 0)
1168 while ((tmp_m
= m
) != 0) {
1170 gbuf_next(tmp_m
) = 0;
1176 /* free empty mbufs at the front of the chain */
1177 gbuf_t
*gbuf_strip(m
)
1182 while (m
&& gbuf_len(m
) == 0) {
1190 /**************************************/
1192 int ddp_adjmsg(m
, len
)
1197 gbuf_t
*curr_m
, *prev_m
;
1199 if (m
== (gbuf_t
*)0)
1203 for (curr_m
=m
; curr_m
;) {
1204 buf_len
= gbuf_len(curr_m
);
1205 if (len
< buf_len
) {
1206 gbuf_rinc(curr_m
,len
);
1210 gbuf_rinc(curr_m
,buf_len
);
1211 if ((curr_m
= gbuf_cont(curr_m
)) == 0) {
1217 } else if (len
< 0) {
1220 for (curr_m
=m
; gbuf_cont(curr_m
);
1221 prev_m
=curr_m
, curr_m
=gbuf_cont(curr_m
)) ;
1222 buf_len
= gbuf_len(curr_m
);
1223 if (len
< buf_len
) {
1224 gbuf_wdec(curr_m
,len
);
1229 gbuf_cont(prev_m
) = 0;
1239 * The message chain, m is grown in size by len contiguous bytes.
1240 * If len is non-negative, len bytes are added to the
1241 * end of the gbuf_t chain. If len is negative, the
1242 * bytes are added to the front. ddp_growmsg only adds bytes to
1243 * message blocks of the same type.
1244 * It returns a pointer to the new gbuf_t on sucess, 0 on failure.
1247 gbuf_t
*ddp_growmsg(mp
, len
)
1253 if ((m
= mp
) == (gbuf_t
*) 0)
1254 return ((gbuf_t
*) 0);
1258 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1259 return ((gbuf_t
*) 0);
1260 gbuf_set_type(d
, gbuf_type(m
));
1262 /* link in new gbuf_t */
1271 if ((count
= gbuf_msgsize(m
)) < 0)
1272 return ((gbuf_t
*) 0);
1273 /* find end of chain */
1274 for ( ; m
; m
= gbuf_cont(m
)) {
1275 if (gbuf_len(m
) >= count
)
1277 count
-= gbuf_len(m
);
1279 /* m now points to gbuf_t to add to */
1280 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1281 return ((gbuf_t
*) 0);
1282 gbuf_set_type(d
, gbuf_type(m
));
1283 /* link in new gbuf_t */
1284 gbuf_cont(d
) = gbuf_cont(m
);
1292 * return the MSG_IOCACK/MSG_IOCNAK. Note that the same message
1293 * block is used as the vehicle, and that if there is an error return,
1294 * then linked blocks are lopped off. BEWARE of multiple references.
1295 * Used by other appletalk modules, so it is not static!
1298 void ioc_ack(errno
, m
, gref
)
1301 register gref_t
*gref
;
1303 ioc_t
*iocbp
= (ioc_t
*)gbuf_rptr(m
);
1305 /*kprintf("ioc_ack: m=%x gref=%x errno=%d\n", m, gref, errno);*/
1306 if ((iocbp
->ioc_error
= errno
) != 0)
1307 { /* errno != 0, then there is an error, get rid of linked blocks! */
1310 gbuf_freem(gbuf_cont(m
));
1313 gbuf_set_type(m
, MSG_IOCNAK
);
1314 iocbp
->ioc_count
= 0; /* only make zero length if error */
1315 iocbp
->ioc_rval
= -1;
1317 gbuf_set_type(m
, MSG_IOCACK
);
1319 atalk_putnext(gref
, m
);
1323 static void ioccmd_t_32_to_64( ioccmd_t
*from_p
, user_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_USER_ADDR_T(from_p
->ic_dp
);
1332 static void ioccmd_t_64_to_32( user_ioccmd_t
*from_p
, ioccmd_t
*to_p
)
1334 to_p
->ic_cmd
= from_p
->ic_cmd
;
1335 to_p
->ic_timout
= from_p
->ic_timout
;
1336 to_p
->ic_len
= from_p
->ic_len
;
1337 to_p
->ic_dp
= CAST_DOWN(caddr_t
, from_p
->ic_dp
);