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
);
96 caddr_t atp_free_cluster_list
= 0;
98 void gref_wput(gref
, m
)
102 switch (gref
->proto
) {
104 ddp_putmsg(gref
, m
); break;
106 elap_wput(gref
, m
); break;
108 atp_wput(gref
, m
); break;
110 asp_wput(gref
, m
); break;
113 aurp_wput(gref
, m
); break;
116 adsp_wput(gref
, m
); break;
118 if (gbuf_type(m
) == MSG_IOCTL
) {
119 gbuf_freem(gbuf_cont(m
));
121 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= -1;
122 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= EPROTOTYPE
;
123 gbuf_set_type(m
, MSG_IOCNAK
);
124 atalk_putnext(gref
, m
);
134 int _ATsocket(proto
, err
, proc
)
142 /* make sure the specified protocol id is valid */
145 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
146 BSD-style socket interface. */
155 #ifdef APPLETALK_DEBUG
156 kprintf("_ATsocket: error EPROTOTYPE =%d\n", *err
);
161 /* allocate a protocol channel */
162 if ((*err
= gref_alloc(&gref
)) != 0) {
163 #ifdef APPLETALK_DEBUG
164 kprintf("_ATsocket: error gref_open =%d\n", *err
);
169 gref
->pid
= proc_pid((struct proc
*)proc
);
171 /* open the specified protocol */
172 switch (gref
->proto
) {
174 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
175 BSD-style socket interface. */
178 *err
= atp_open(gref
, 1); break;
180 *err
= asp_open(gref
); break;
183 *err
= aurp_open(gref
); break;
186 *err
= adsp_open(gref
); break;
189 /* create the descriptor for the channel */
191 #ifdef APPLETALK_DEBUG
192 kprintf("_ATsocket: open failed for %d proto; err = %d\n",
195 gref
->proto
= ATPROTO_NONE
;
197 if (*err
|| (*err
= atalk_openref(gref
, &fd
, proc
))) {
198 #ifdef APPLETALK_DEBUG
199 kprintf("_ATsocket: error atalk_openref =%d\n", *err
);
201 (void)gref_close(gref
);
205 kprintf("_ATsocket: proto=%d return=%d fd=%d\n", proto, *err, fd);
210 int _ATgetmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
221 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) == 0) {
222 switch (gref
->proto
) {
224 rc
= ASPgetmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
);
228 rc
= AURPgetmsg(err
);
232 *err
= EPROTONOSUPPORT
;
238 /* kprintf("_ATgetmsg: return=%d\n", *err);*/
242 int _ATputmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
253 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) == 0) {
254 switch (gref
->proto
) {
256 rc
= ASPputmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
); break;
258 *err
= EPROTONOSUPPORT
; break;
263 /* kprintf("_ATputmsg: return=%d\n", *err); */
267 int _ATclose(fg
, proc
)
274 if ((err
= atalk_closeref(fg
, &gref
)) == 0) {
276 (void)gref_close(gref
);
283 int _ATrw(fp
, rw
, uio
, ext
)
289 int s
, err
, len
, clen
= 0, res
;
291 gbuf_t
*m
, *mhead
, *mprev
;
293 /* no need to get/drop iocount as the fp already has one */
294 if ((err
= atalk_getref_locked(fp
, 0, &gref
, 0, 1)) != 0)
297 // LP64todo - fix this!
298 if ((len
= uio_resid(uio
)) == 0)
301 ATDISABLE(s
, gref
->lock
);
303 if (rw
== UIO_READ
) {
304 KERNEL_DEBUG(DBG_ADSP_ATRW
, 0, gref
, len
, gref
->rdhead
, 0);
305 while ((gref
->errno
== 0) && ((mhead
= gref
->rdhead
) == 0)) {
306 gref
->sevents
|= POLLMSG
;
307 err
= msleep(&gref
->event
, atalk_mutex
, PSOCK
| PCATCH
, "AT read", 0);
308 gref
->sevents
&= ~POLLMSG
;
310 ATENABLE(s
, gref
->lock
);
313 KERNEL_DEBUG(DBG_ADSP_ATRW
, 1, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
317 ATENABLE(s
, gref
->lock
);
320 if ((gref
->rdhead
= gbuf_next(mhead
)) == 0)
323 KERNEL_DEBUG(DBG_ADSP_ATRW
, 2, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
325 ATENABLE(s
, gref
->lock
);
327 //##### LD TEST 08/05
328 // simple_lock(&gref->lock);
330 gbuf_next(mhead
) = 0;
332 for (mprev
=0, m
=mhead
; m
&& len
; len
-=clen
) {
333 if ((clen
= gbuf_len(m
)) > 0) {
336 uio
->uio_rw
= UIO_READ
;
337 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
),
339 KERNEL_DEBUG(DBG_ADSP_ATRW
, 3, m
, clen
,
343 if (gbuf_len(m
) > len
) {
352 KERNEL_DEBUG(DBG_ADSP_ATRW
, 4, m
, gbuf_len(m
), mprev
, gref
->rdhead
);
354 gbuf_cont(mprev
) = 0;
357 ATDISABLE(s
, gref
->lock
);
358 if (gref
->rdhead
== 0)
360 gbuf_next(m
) = gref
->rdhead
;
362 ATENABLE(s
, gref
->lock
);
367 // simple_unlock(&gref->lock);
369 if (gref
->writeable
) {
370 while (!(*gref
->writeable
)(gref
)) {
371 /* flow control on, wait to be enabled to write */
372 gref
->sevents
|= POLLSYNC
;
373 err
= msleep(&gref
->event
, atalk_mutex
, PSOCK
| PCATCH
, "AT write", 0);
374 gref
->sevents
&= ~POLLSYNC
;
376 ATENABLE(s
, gref
->lock
);
382 ATENABLE(s
, gref
->lock
);
384 /* allocate a buffer to copy in the write data */
385 if ((m
= gbuf_alloc(AT_WR_OFFSET
+len
, PRI_MED
)) == 0)
387 gbuf_rinc(m
,AT_WR_OFFSET
);
390 /* copy in the write data */
391 uio
->uio_rw
= UIO_WRITE
;
392 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
), len
, uio
))) {
393 #ifdef APPLETALK_DEBUG
394 kprintf("_ATrw: UIO_WRITE: res=%d\n", res
);
400 /* forward the write data to the appropriate protocol module */
407 int _ATread(fp
, uio
, cred
, flags
, p
)
417 stat
= _ATrw(fp
, UIO_READ
, uio
, 0);
422 int _ATwrite(fp
, uio
, cred
, flags
, p
)
432 stat
= _ATrw(fp
, UIO_WRITE
, uio
, 0);
438 /* Most of the processing from _ATioctl, so that it can be called
439 from the new ioctl code */
440 /* bms: update to be callable from kernel */
441 int at_ioctl(gref_t
*gref
, u_long cmd
, caddr_t arg
, int fromKernel
)
447 user_addr_t user_arg
;
448 user_ioccmd_t user_ioccmd
;
451 /* error if not for us */
452 if ((cmd
& 0xffff) != 0xff99)
455 size
= IOCPARM_LEN(cmd
);
456 if (size
!= sizeof(user_addr_t
))
459 user_arg
= *((user_addr_t
*)arg
);
461 /* copy in ioc command info */
462 is64bit
= proc_is64bit(current_proc());
465 bcopy (CAST_DOWN(caddr_t
, user_arg
), &tmp
, sizeof (tmp
));
466 ioccmd_t_32_to_64(&tmp
, &user_ioccmd
);
470 err
= copyin(user_arg
, (caddr_t
)&user_ioccmd
, sizeof(user_ioccmd
));
474 err
= copyin(user_arg
, (caddr_t
)&tmp
, sizeof(tmp
));
475 ioccmd_t_32_to_64(&tmp
, &user_ioccmd
);
478 #ifdef APPLETALK_DEBUG
479 kprintf("at_ioctl: err = %d, copyin(%llx, %x, %d)\n", err
,
480 user_arg
, (caddr_t
)&user_ioccmd
, sizeof(user_ioccmd
));
486 /* allocate a buffer to create an ioc command
487 first mbuf contains ioc command */
488 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) == 0)
490 gbuf_wset(m
, sizeof(ioc_t
)); /* mbuf->m_len */
491 gbuf_set_type(m
, MSG_IOCTL
); /* mbuf->m_type */
493 /* create the ioc command
494 second mbuf contains the actual ASP command */
495 if (user_ioccmd
.ic_len
) {
496 if ((gbuf_cont(m
) = gbuf_alloc(user_ioccmd
.ic_len
, PRI_HI
)) == 0) {
498 #ifdef APPLETALK_DEBUG
499 kprintf("at_ioctl: gbuf_alloc err=%d\n",ENOBUFS
);
503 gbuf_wset(gbuf_cont(m
), user_ioccmd
.ic_len
); /* mbuf->m_len */
505 bcopy (CAST_DOWN(caddr_t
, user_ioccmd
.ic_dp
), gbuf_rptr(gbuf_cont(m
)), user_ioccmd
.ic_len
);
507 if ((err
= copyin(user_ioccmd
.ic_dp
, (caddr_t
)gbuf_rptr(gbuf_cont(m
)), user_ioccmd
.ic_len
)) != 0) {
513 ioc
= (ioc_t
*) gbuf_rptr(m
);
514 ioc
->ioc_cmd
= user_ioccmd
.ic_cmd
;
515 ioc
->ioc_count
= user_ioccmd
.ic_len
;
519 /* send the ioc command to the appropriate recipient */
522 /* wait for the ioc ack */
523 ATDISABLE(s
, gref
->lock
);
524 while ((m
= gref
->ichead
) == 0) {
525 gref
->sevents
|= POLLPRI
;
526 #ifdef APPLETALK_DEBUG
527 kprintf("sleep gref = 0x%x\n", (unsigned)gref
);
529 err
= msleep(&gref
->iocevent
, atalk_mutex
, PSOCK
| PCATCH
, "AT ioctl", 0);
530 gref
->sevents
&= ~POLLPRI
;
532 ATENABLE(s
, gref
->lock
);
533 #ifdef APPLETALK_DEBUG
534 kprintf("at_ioctl: EINTR\n");
541 if (gbuf_next(m
) == m
) /* error case */
544 gref
->ichead
= gbuf_next(m
);
546 ATENABLE(s
, gref
->lock
);
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);*/
603 int _ATioctl(fp
, cmd
, arg
, proc
)
606 register caddr_t arg
;
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
, proc
)
637 /* no need to drop the iocount as select covers that */
638 err
= atalk_getref_locked(fp
, 0, &gref
, 0, 0);
644 ATDISABLE(s
, gref
->lock
);
645 if (which
== FREAD
) {
646 if (gref
->rdhead
|| (gref
->readable
&& (*gref
->readable
)(gref
)))
649 gref
->sevents
|= POLLIN
;
650 selrecord(proc
, &gref
->si
, wql
);
653 else if (which
== POLLOUT
) {
654 if (gref
->writeable
) {
655 if ((*gref
->writeable
)(gref
))
658 gref
->sevents
|= POLLOUT
;
659 selrecord(proc
, &gref
->si
, wql
);
664 ATENABLE(s
, gref
->lock
);
670 int _ATkqfilter(fp
, kn
, p
)
678 void atalk_putnext(gref
, m
)
684 ATDISABLE(s
, gref
->lock
);
686 /* *** potential leak? *** */
689 switch (gbuf_type(m
)) {
693 gbuf_next(gref
->ichead
) = m
;
696 if (gref
->sevents
& POLLPRI
) {
697 #ifdef APPLETALK_DEBUG
698 kprintf("wakeup gref = 0x%x\n", (unsigned)gref
);
700 wakeup(&gref
->iocevent
);
705 /* *** this processing was moved to atalk_notify *** */
706 panic("atalk_putnext receved MSG_ERROR");
713 gbuf_next(gref
->rdtail
) = m
;
717 if (gref
->sevents
& POLLMSG
) {
718 gref
->sevents
&= ~POLLMSG
;
719 wakeup(&gref
->event
);
721 if (gref
->sevents
& POLLIN
) {
722 gref
->sevents
&= ~POLLIN
;
723 selwakeup(&gref
->si
);
727 } /* switch gbuf_type(m) */
729 ATENABLE(s
, gref
->lock
);
730 } /* atalk_putnext */
732 void atalk_enablew(gref
)
735 if (gref
->sevents
& POLLSYNC
)
736 wakeup(&gref
->event
);
739 void atalk_flush(gref
)
744 ATDISABLE(s
, gref
->lock
);
746 gbuf_freel(gref
->rdhead
);
750 gbuf_freel(gref
->ichead
);
753 ATENABLE(s
, gref
->lock
);
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 ATDISABLE(s
, gref
->lock
);
767 if (gref
->atpcb_socket
) {
769 This section is patterned after udp_notify() in
772 gref
->atpcb_socket
->so_error
= errno
;
773 sorwakeup(gref
->atpcb_socket
);
774 sowwakeup(gref
->atpcb_socket
);
776 /* for ATP, ASP, and ADSP */
777 if (gref
->errno
== 0) {
779 /* clear out data waiting to be read */
781 gbuf_freel(gref
->rdhead
);
785 if (gref
->sevents
& POLLMSG
) {
786 gref
->sevents
&= ~POLLMSG
;
787 wakeup(&gref
->event
);
790 if (gref
->sevents
& POLLIN
) {
791 gref
->sevents
&= ~POLLIN
;
792 selwakeup(&gref
->si
);
796 ATENABLE(s
, gref
->lock
);
799 void atalk_notify_sel(gref
)
804 ATDISABLE(s
, gref
->lock
);
805 if (gref
->sevents
& POLLIN
) {
806 gref
->sevents
&= ~POLLIN
;
807 selwakeup(&gref
->si
);
809 ATENABLE(s
, gref
->lock
);
812 int atalk_peek(gref
, event
)
814 unsigned char *event
;
818 ATDISABLE(s
, gref
->lock
);
820 *event
= *gbuf_rptr(gref
->rdhead
);
824 ATENABLE(s
, gref
->lock
);
829 static gbuf_t
*trace_msg
;
831 void atalk_settrace(str
, p1
, p2
, p3
, p4
, p5
)
838 sprintf(trace_buf
, str
, p1
, p2
, p3
, p4
, p5
);
839 len
= strlen(trace_buf
);
840 #ifdef APPLETALK_DEBUG
841 kprintf("atalk_settrace: gbufalloc size=%d\n", len
+1);
843 if ((m
= gbuf_alloc(len
+1, PRI_MED
)) == 0)
846 strcpy(gbuf_rptr(m
), trace_buf
);
848 for (nextm
=trace_msg
; gbuf_cont(nextm
); nextm
=gbuf_cont(nextm
)) ;
849 gbuf_cont(nextm
) = m
;
854 void atalk_gettrace(m
)
858 gbuf_cont(m
) = trace_msg
;
863 #define GREF_PER_BLK 32
864 static gref_t
*gref_free_list
= 0;
866 int gref_alloc(grefp
)
869 extern gbuf_t
*atp_resource_m
;
872 gref_t
*gref
, *gref_array
;
874 *grefp
= (gref_t
*)NULL
;
876 ATDISABLE(s
, refall_lock
);
877 if (gref_free_list
== 0) {
878 ATENABLE(s
, refall_lock
);
879 #ifdef APPLETALK_DEBUG
880 kprintf("gref_alloc: gbufalloc size=%d\n", GREF_PER_BLK
*sizeof(gref_t
));
882 if ((m
= gbuf_alloc(GREF_PER_BLK
*sizeof(gref_t
),PRI_HI
)) == 0)
884 bzero(gbuf_rptr(m
), GREF_PER_BLK
*sizeof(gref_t
));
885 gref_array
= (gref_t
*)gbuf_rptr(m
);
886 for (i
=0; i
< GREF_PER_BLK
-1; i
++)
887 gref_array
[i
].atpcb_next
= (gref_t
*)&gref_array
[i
+1];
888 ATDISABLE(s
, refall_lock
);
889 gbuf_cont(m
) = atp_resource_m
;
891 gref_array
[i
].atpcb_next
= gref_free_list
;
892 gref_free_list
= (gref_t
*)&gref_array
[0];
895 gref
= gref_free_list
;
896 gref_free_list
= gref
->atpcb_next
;
897 ATENABLE(s
, refall_lock
);
898 ATLOCKINIT(gref
->lock
);
899 //### LD Test 08/05/98
900 // simple_lock_init(&gref->lock);
901 ATEVENTINIT(gref
->event
);
902 ATEVENTINIT(gref
->iocevent
);
904 /* *** just for now *** */
905 gref
->atpcb_socket
= (struct socket
*)NULL
;
911 /* bms: make gref_close callable from kernel */
912 int gref_close(gref_t
*gref
)
916 switch (gref
->proto
) {
918 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
919 BSD-style socket interface. */
922 rc
= atp_close(gref
, 1); break;
924 rc
= asp_close(gref
); break;
927 rc
= aurp_close(gref
); break;
931 rc
= adsp_close(gref
); break;
939 selthreadclear(&gref
->si
);
941 /* from original gref_free() */
942 ATDISABLE(s
, refall_lock
);
943 bzero((char *)gref
, sizeof(gref_t
));
944 gref
->atpcb_next
= gref_free_list
;
945 gref_free_list
= gref
;
946 ATENABLE(s
, refall_lock
);
955 *** Some to be replaced with mbuf routines, some to be re-written
956 as mbuf routines (and moved to kern/uicp_mbuf.c or sys/mbuf.h?).
962 * LD 5/12/97 Added for MacOSX, defines a m_clattach function that:
963 * "Allocates an mbuf structure and attaches an external cluster."
966 struct mbuf
*m_clattach(extbuf
, extfree
, extsize
, extarg
, wait
)
968 void (*extfree
)(caddr_t
, u_int
, caddr_t
);
975 if ((m
= m_gethdr(wait
, MSG_DATA
)) == NULL
)
978 m
->m_ext
.ext_buf
= extbuf
;
979 m
->m_ext
.ext_free
= extfree
;
980 m
->m_ext
.ext_size
= extsize
;
981 m
->m_ext
.ext_arg
= extarg
;
982 m
->m_ext
.ext_refs
.forward
=
983 m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
993 temp fix for bug 2731148 - until this code is re-written to use standard clusters
994 Deletes any free clusters on the free list.
996 void atp_delete_free_clusters()
999 caddr_t cluster_list
;
1002 /* check for free clusters on the free_cluster_list to be deleted */
1003 MBUF_LOCK(); /* lock used by mbuf routines */
1005 untimeout(&atp_delete_free_clusters
, NULL
);
1006 atp_free_cluster_timeout_set
= 0;
1008 cluster_list
= atp_free_cluster_list
;
1009 atp_free_cluster_list
= 0;
1013 while (cluster
= cluster_list
)
1015 cluster_list
= *((caddr_t
*)cluster
);
1016 FREE(cluster
, M_MCLUST
);
1023 Used as the "free" routine for over-size clusters allocated using
1024 m_lgbuf_alloc(). Called by m_free while under MBUF_LOCK.
1027 void m_lgbuf_free(buf
, size
, arg
)
1030 caddr_t arg
; /* not needed, but they're in m_free() */
1032 /* FREE(buf, M_MCLUST); - can't free here - called from m_free while under lock */
1034 /* move to free_cluster_list to be deleted later */
1035 caddr_t cluster
= (caddr_t
)buf
;
1037 /* don't need a lock because this is only called called from m_free which */
1038 /* is under MBUF_LOCK */
1039 *((caddr_t
*)cluster
) = atp_free_cluster_list
;
1040 atp_free_cluster_list
= cluster
;
1042 if (atp_free_cluster_timeout_set
== 0)
1044 atp_free_cluster_timeout_set
= 1;
1045 timeout(&atp_delete_free_clusters
, NULL
, (1 * HZ
));
1050 Used to allocate an mbuf when there is the possibility that it may
1051 need to be larger than the size of a standard cluster.
1054 struct mbuf
*m_lgbuf_alloc(size
, wait
)
1059 if (atp_free_cluster_list
)
1060 atp_delete_free_clusters(); /* delete any free clusters on the free list */
1062 /* If size is too large, allocate a cluster, otherwise, use the
1063 standard mbuf allocation routines.*/
1064 if (size
> MCLBYTES
) {
1067 (buf
= (void *)_MALLOC(size
, M_MCLUST
,
1068 (wait
)? M_WAITOK
: M_NOWAIT
))) {
1072 (m
= m_clattach(buf
, m_lgbuf_free
, size
, 0,
1073 (wait
)? M_WAIT
: M_DONTWAIT
))) {
1074 m_lgbuf_free(buf
, 0, 0);
1078 m
= m_gethdr(((wait
)? M_WAIT
: M_DONTWAIT
), MSG_DATA
);
1079 if (m
&& (size
> MHLEN
)) {
1080 MCLGET(m
, ((wait
)? M_WAIT
: M_DONTWAIT
));
1081 if (!(m
->m_flags
& M_EXT
)) {
1089 } /* m_lgbuf_alloc */
1092 gbuf_alloc() is a wrapper for m_lgbuf_alloc(), which is used to
1093 allocate an mbuf when there is the possibility that it may need
1094 to be larger than the size of a standard cluster.
1096 gbuf_alloc() sets the mbuf lengths, unlike the standard mbuf routines.
1099 gbuf_t
*gbuf_alloc_wait(size
, wait
)
1102 gbuf_t
*m
= (gbuf_t
*)m_lgbuf_alloc(size
, wait
);
1104 /* Standard mbuf allocation routines assume that the caller
1105 will set the size. */
1107 (struct mbuf
*)m
->m_pkthdr
.len
= size
;
1108 (struct mbuf
*)m
->m_len
= size
;
1119 for (size
=0; m
; m
=gbuf_cont(m
))
1120 size
+= gbuf_len(m
);
1124 int append_copy(m1
, m2
, wait
)
1125 struct mbuf
*m1
, *m2
;
1128 if ((!(m1
->m_flags
& M_EXT
)) && (!(m2
->m_flags
& M_EXT
)) &&
1129 (m_trailingspace(m1
) >= m2
->m_len
)) {
1130 /* splat the data from one into the other */
1131 bcopy(mtod(m2
, caddr_t
), mtod(m1
, caddr_t
) + m1
->m_len
,
1133 m1
->m_len
+= m2
->m_len
;
1134 if (m1
->m_flags
& M_PKTHDR
)
1135 m1
->m_pkthdr
.len
+= m2
->m_len
;
1138 if ((m1
->m_next
= m_copym(m2
, 0, m2
->m_len
,
1139 (wait
)? M_WAIT
: M_DONTWAIT
)) == NULL
)
1145 Copy an mbuf chain, referencing existing external storage, if any.
1146 Leave space for a header in the new chain, if the space has been
1147 left in the origin chain.
1149 struct mbuf
*copy_pkt(mlist
, pad
)
1150 struct mbuf
*mlist
; /* the mbuf chain to be copied */
1151 int pad
; /* hint as to how long the header might be
1152 If pad is < 0, leave the same amount of space
1153 as there was in the original. */
1159 len
= m_leadingspace(mlist
);
1161 len
= min(pad
, m_leadingspace(mlist
));
1163 /* preserve space for the header at the beginning of the mbuf */
1165 mlist
->m_data
-= (len
);
1166 mlist
->m_len
+= (len
);
1167 if (mlist
->m_flags
& M_PKTHDR
)
1168 mlist
->m_pkthdr
.len
+= (len
);
1169 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1173 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1178 void gbuf_linkb(m1
, m2
)
1182 while (gbuf_cont(m1
) != 0)
1187 void gbuf_linkpkt(m1
, m2
)
1191 while (gbuf_next(m1
) != 0)
1201 while ((tmp_m
= m
) != 0) {
1203 gbuf_next(tmp_m
) = 0;
1209 /* free empty mbufs at the front of the chain */
1210 gbuf_t
*gbuf_strip(m
)
1215 while (m
&& gbuf_len(m
) == 0) {
1223 /**************************************/
1225 int ddp_adjmsg(m
, len
)
1230 gbuf_t
*curr_m
, *prev_m
;
1232 if (m
== (gbuf_t
*)0)
1236 for (curr_m
=m
; curr_m
;) {
1237 buf_len
= gbuf_len(curr_m
);
1238 if (len
< buf_len
) {
1239 gbuf_rinc(curr_m
,len
);
1243 gbuf_rinc(curr_m
,buf_len
);
1244 if ((curr_m
= gbuf_cont(curr_m
)) == 0) {
1250 } else if (len
< 0) {
1253 for (curr_m
=m
; gbuf_cont(curr_m
);
1254 prev_m
=curr_m
, curr_m
=gbuf_cont(curr_m
)) ;
1255 buf_len
= gbuf_len(curr_m
);
1256 if (len
< buf_len
) {
1257 gbuf_wdec(curr_m
,len
);
1262 gbuf_cont(prev_m
) = 0;
1272 * The message chain, m is grown in size by len contiguous bytes.
1273 * If len is non-negative, len bytes are added to the
1274 * end of the gbuf_t chain. If len is negative, the
1275 * bytes are added to the front. ddp_growmsg only adds bytes to
1276 * message blocks of the same type.
1277 * It returns a pointer to the new gbuf_t on sucess, 0 on failure.
1280 gbuf_t
*ddp_growmsg(mp
, len
)
1286 if ((m
= mp
) == (gbuf_t
*) 0)
1287 return ((gbuf_t
*) 0);
1291 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1292 return ((gbuf_t
*) 0);
1293 gbuf_set_type(d
, gbuf_type(m
));
1295 /* link in new gbuf_t */
1304 if ((count
= gbuf_msgsize(m
)) < 0)
1305 return ((gbuf_t
*) 0);
1306 /* find end of chain */
1307 for ( ; m
; m
= gbuf_cont(m
)) {
1308 if (gbuf_len(m
) >= count
)
1310 count
-= gbuf_len(m
);
1312 /* m now points to gbuf_t to add to */
1313 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1314 return ((gbuf_t
*) 0);
1315 gbuf_set_type(d
, gbuf_type(m
));
1316 /* link in new gbuf_t */
1317 gbuf_cont(d
) = gbuf_cont(m
);
1325 * return the MSG_IOCACK/MSG_IOCNAK. Note that the same message
1326 * block is used as the vehicle, and that if there is an error return,
1327 * then linked blocks are lopped off. BEWARE of multiple references.
1328 * Used by other appletalk modules, so it is not static!
1331 void ioc_ack(errno
, m
, gref
)
1334 register gref_t
*gref
;
1336 ioc_t
*iocbp
= (ioc_t
*)gbuf_rptr(m
);
1338 /*kprintf("ioc_ack: m=%x gref=%x errno=%d\n", m, gref, errno);*/
1339 if ((iocbp
->ioc_error
= errno
) != 0)
1340 { /* errno != 0, then there is an error, get rid of linked blocks! */
1343 gbuf_freem(gbuf_cont(m
));
1346 gbuf_set_type(m
, MSG_IOCNAK
);
1347 iocbp
->ioc_count
= 0; /* only make zero length if error */
1348 iocbp
->ioc_rval
= -1;
1350 gbuf_set_type(m
, MSG_IOCACK
);
1352 atalk_putnext(gref
, m
);
1356 static void ioccmd_t_32_to_64( ioccmd_t
*from_p
, user_ioccmd_t
*to_p
)
1358 to_p
->ic_cmd
= from_p
->ic_cmd
;
1359 to_p
->ic_timout
= from_p
->ic_timout
;
1360 to_p
->ic_len
= from_p
->ic_len
;
1361 to_p
->ic_dp
= CAST_USER_ADDR_T(from_p
->ic_dp
);
1365 static void ioccmd_t_64_to_32( user_ioccmd_t
*from_p
, ioccmd_t
*to_p
)
1367 to_p
->ic_cmd
= from_p
->ic_cmd
;
1368 to_p
->ic_timout
= from_p
->ic_timout
;
1369 to_p
->ic_len
= from_p
->ic_len
;
1370 to_p
->ic_dp
= CAST_DOWN(caddr_t
, from_p
->ic_dp
);