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
, p
)
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
, p
, 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
, p
);
422 int _ATwrite(fp
, uio
, cred
, flags
, p
)
432 stat
= _ATrw(fp
, UIO_WRITE
, uio
, p
);
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
)
636 /* Radar 4128949: Drop the proc_fd lock here to avoid lock inversion issues with the other AT calls
637 * select() is already holding a reference on the fd, so it won't go away during the time it is unlocked.
642 /* no need to drop the iocount as select covers that */
643 err
= atalk_getref_locked(fp
, 0, &gref
, 0, 0);
646 /* Safe to re-grab the proc_fdlock at that point */
651 ATDISABLE(s
, gref
->lock
);
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
);
671 ATENABLE(s
, gref
->lock
);
677 int _ATkqfilter(fp
, kn
, p
)
685 void atalk_putnext(gref
, m
)
691 ATDISABLE(s
, gref
->lock
);
693 /* *** potential leak? *** */
696 switch (gbuf_type(m
)) {
700 gbuf_next(gref
->ichead
) = m
;
703 if (gref
->sevents
& POLLPRI
) {
704 #ifdef APPLETALK_DEBUG
705 kprintf("wakeup gref = 0x%x\n", (unsigned)gref
);
707 wakeup(&gref
->iocevent
);
712 /* *** this processing was moved to atalk_notify *** */
713 panic("atalk_putnext receved MSG_ERROR");
720 gbuf_next(gref
->rdtail
) = m
;
724 if (gref
->sevents
& POLLMSG
) {
725 gref
->sevents
&= ~POLLMSG
;
726 wakeup(&gref
->event
);
728 if (gref
->sevents
& POLLIN
) {
729 gref
->sevents
&= ~POLLIN
;
730 selwakeup(&gref
->si
);
734 } /* switch gbuf_type(m) */
736 ATENABLE(s
, gref
->lock
);
737 } /* atalk_putnext */
739 void atalk_enablew(gref
)
742 if (gref
->sevents
& POLLSYNC
)
743 wakeup(&gref
->event
);
746 void atalk_flush(gref
)
751 ATDISABLE(s
, gref
->lock
);
753 gbuf_freel(gref
->rdhead
);
757 gbuf_freel(gref
->ichead
);
760 ATENABLE(s
, gref
->lock
);
764 * Notify an appletalk user of an asynchronous error;
765 * just wake up so that he can collect error status.
767 void atalk_notify(gref
, errno
)
768 register gref_t
*gref
;
772 ATDISABLE(s
, gref
->lock
);
774 if (gref
->atpcb_socket
) {
776 This section is patterned after udp_notify() in
779 gref
->atpcb_socket
->so_error
= errno
;
780 sorwakeup(gref
->atpcb_socket
);
781 sowwakeup(gref
->atpcb_socket
);
783 /* for ATP, ASP, and ADSP */
784 if (gref
->errno
== 0) {
786 /* clear out data waiting to be read */
788 gbuf_freel(gref
->rdhead
);
792 if (gref
->sevents
& POLLMSG
) {
793 gref
->sevents
&= ~POLLMSG
;
794 wakeup(&gref
->event
);
797 if (gref
->sevents
& POLLIN
) {
798 gref
->sevents
&= ~POLLIN
;
799 selwakeup(&gref
->si
);
803 ATENABLE(s
, gref
->lock
);
806 void atalk_notify_sel(gref
)
811 ATDISABLE(s
, gref
->lock
);
812 if (gref
->sevents
& POLLIN
) {
813 gref
->sevents
&= ~POLLIN
;
814 selwakeup(&gref
->si
);
816 ATENABLE(s
, gref
->lock
);
819 int atalk_peek(gref
, event
)
821 unsigned char *event
;
825 ATDISABLE(s
, gref
->lock
);
827 *event
= *gbuf_rptr(gref
->rdhead
);
831 ATENABLE(s
, gref
->lock
);
836 static gbuf_t
*trace_msg
;
838 void atalk_settrace(str
, p1
, p2
, p3
, p4
, p5
)
845 sprintf(trace_buf
, str
, p1
, p2
, p3
, p4
, p5
);
846 len
= strlen(trace_buf
);
847 #ifdef APPLETALK_DEBUG
848 kprintf("atalk_settrace: gbufalloc size=%d\n", len
+1);
850 if ((m
= gbuf_alloc(len
+1, PRI_MED
)) == 0)
853 strcpy(gbuf_rptr(m
), trace_buf
);
855 for (nextm
=trace_msg
; gbuf_cont(nextm
); nextm
=gbuf_cont(nextm
)) ;
856 gbuf_cont(nextm
) = m
;
861 void atalk_gettrace(m
)
865 gbuf_cont(m
) = trace_msg
;
870 #define GREF_PER_BLK 32
871 static gref_t
*gref_free_list
= 0;
873 int gref_alloc(grefp
)
876 extern gbuf_t
*atp_resource_m
;
879 gref_t
*gref
, *gref_array
;
881 *grefp
= (gref_t
*)NULL
;
883 ATDISABLE(s
, refall_lock
);
884 if (gref_free_list
== 0) {
885 ATENABLE(s
, refall_lock
);
886 #ifdef APPLETALK_DEBUG
887 kprintf("gref_alloc: gbufalloc size=%d\n", GREF_PER_BLK
*sizeof(gref_t
));
889 if ((m
= gbuf_alloc(GREF_PER_BLK
*sizeof(gref_t
),PRI_HI
)) == 0)
891 bzero(gbuf_rptr(m
), GREF_PER_BLK
*sizeof(gref_t
));
892 gref_array
= (gref_t
*)gbuf_rptr(m
);
893 for (i
=0; i
< GREF_PER_BLK
-1; i
++)
894 gref_array
[i
].atpcb_next
= (gref_t
*)&gref_array
[i
+1];
895 ATDISABLE(s
, refall_lock
);
896 gbuf_cont(m
) = atp_resource_m
;
898 gref_array
[i
].atpcb_next
= gref_free_list
;
899 gref_free_list
= (gref_t
*)&gref_array
[0];
902 gref
= gref_free_list
;
903 gref_free_list
= gref
->atpcb_next
;
904 ATENABLE(s
, refall_lock
);
905 ATLOCKINIT(gref
->lock
);
906 //### LD Test 08/05/98
907 // simple_lock_init(&gref->lock);
908 ATEVENTINIT(gref
->event
);
909 ATEVENTINIT(gref
->iocevent
);
911 /* *** just for now *** */
912 gref
->atpcb_socket
= (struct socket
*)NULL
;
918 /* bms: make gref_close callable from kernel */
919 int gref_close(gref_t
*gref
)
923 switch (gref
->proto
) {
925 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
926 BSD-style socket interface. */
929 rc
= atp_close(gref
, 1); break;
931 rc
= asp_close(gref
); break;
934 rc
= aurp_close(gref
); break;
938 rc
= adsp_close(gref
); break;
946 selthreadclear(&gref
->si
);
948 /* from original gref_free() */
949 ATDISABLE(s
, refall_lock
);
950 bzero((char *)gref
, sizeof(gref_t
));
951 gref
->atpcb_next
= gref_free_list
;
952 gref_free_list
= gref
;
953 ATENABLE(s
, refall_lock
);
962 *** Some to be replaced with mbuf routines, some to be re-written
963 as mbuf routines (and moved to kern/uicp_mbuf.c or sys/mbuf.h?).
969 * LD 5/12/97 Added for MacOSX, defines a m_clattach function that:
970 * "Allocates an mbuf structure and attaches an external cluster."
973 struct mbuf
*m_clattach(extbuf
, extfree
, extsize
, extarg
, wait
)
975 void (*extfree
)(caddr_t
, u_int
, caddr_t
);
982 if ((m
= m_gethdr(wait
, MSG_DATA
)) == NULL
)
985 m
->m_ext
.ext_buf
= extbuf
;
986 m
->m_ext
.ext_free
= extfree
;
987 m
->m_ext
.ext_size
= extsize
;
988 m
->m_ext
.ext_arg
= extarg
;
989 m
->m_ext
.ext_refs
.forward
=
990 m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
1000 temp fix for bug 2731148 - until this code is re-written to use standard clusters
1001 Deletes any free clusters on the free list.
1003 void atp_delete_free_clusters()
1006 caddr_t cluster_list
;
1009 /* check for free clusters on the free_cluster_list to be deleted */
1010 MBUF_LOCK(); /* lock used by mbuf routines */
1012 untimeout(&atp_delete_free_clusters
, NULL
);
1013 atp_free_cluster_timeout_set
= 0;
1015 cluster_list
= atp_free_cluster_list
;
1016 atp_free_cluster_list
= 0;
1020 while (cluster
= cluster_list
)
1022 cluster_list
= *((caddr_t
*)cluster
);
1023 FREE(cluster
, M_MCLUST
);
1030 Used as the "free" routine for over-size clusters allocated using
1031 m_lgbuf_alloc(). Called by m_free while under MBUF_LOCK.
1034 void m_lgbuf_free(buf
, size
, arg
)
1037 caddr_t arg
; /* not needed, but they're in m_free() */
1039 /* FREE(buf, M_MCLUST); - can't free here - called from m_free while under lock */
1041 /* move to free_cluster_list to be deleted later */
1042 caddr_t cluster
= (caddr_t
)buf
;
1044 /* don't need a lock because this is only called called from m_free which */
1045 /* is under MBUF_LOCK */
1046 *((caddr_t
*)cluster
) = atp_free_cluster_list
;
1047 atp_free_cluster_list
= cluster
;
1049 if (atp_free_cluster_timeout_set
== 0)
1051 atp_free_cluster_timeout_set
= 1;
1052 timeout(&atp_delete_free_clusters
, NULL
, (1 * HZ
));
1057 Used to allocate an mbuf when there is the possibility that it may
1058 need to be larger than the size of a standard cluster.
1061 struct mbuf
*m_lgbuf_alloc(size
, wait
)
1066 if (atp_free_cluster_list
)
1067 atp_delete_free_clusters(); /* delete any free clusters on the free list */
1069 /* If size is too large, allocate a cluster, otherwise, use the
1070 standard mbuf allocation routines.*/
1071 if (size
> MCLBYTES
) {
1074 (buf
= (void *)_MALLOC(size
, M_MCLUST
,
1075 (wait
)? M_WAITOK
: M_NOWAIT
))) {
1079 (m
= m_clattach(buf
, m_lgbuf_free
, size
, 0,
1080 (wait
)? M_WAIT
: M_DONTWAIT
))) {
1081 m_lgbuf_free(buf
, 0, 0);
1085 m
= m_gethdr(((wait
)? M_WAIT
: M_DONTWAIT
), MSG_DATA
);
1086 if (m
&& (size
> MHLEN
)) {
1087 MCLGET(m
, ((wait
)? M_WAIT
: M_DONTWAIT
));
1088 if (!(m
->m_flags
& M_EXT
)) {
1096 } /* m_lgbuf_alloc */
1099 gbuf_alloc() is a wrapper for m_lgbuf_alloc(), which is used to
1100 allocate an mbuf when there is the possibility that it may need
1101 to be larger than the size of a standard cluster.
1103 gbuf_alloc() sets the mbuf lengths, unlike the standard mbuf routines.
1106 gbuf_t
*gbuf_alloc_wait(size
, wait
)
1109 gbuf_t
*m
= (gbuf_t
*)m_lgbuf_alloc(size
, wait
);
1111 /* Standard mbuf allocation routines assume that the caller
1112 will set the size. */
1114 (struct mbuf
*)m
->m_pkthdr
.len
= size
;
1115 (struct mbuf
*)m
->m_len
= size
;
1126 for (size
=0; m
; m
=gbuf_cont(m
))
1127 size
+= gbuf_len(m
);
1131 int append_copy(m1
, m2
, wait
)
1132 struct mbuf
*m1
, *m2
;
1135 if ((!(m1
->m_flags
& M_EXT
)) && (!(m2
->m_flags
& M_EXT
)) &&
1136 (m_trailingspace(m1
) >= m2
->m_len
)) {
1137 /* splat the data from one into the other */
1138 bcopy(mtod(m2
, caddr_t
), mtod(m1
, caddr_t
) + m1
->m_len
,
1140 m1
->m_len
+= m2
->m_len
;
1141 if (m1
->m_flags
& M_PKTHDR
)
1142 m1
->m_pkthdr
.len
+= m2
->m_len
;
1145 if ((m1
->m_next
= m_copym(m2
, 0, m2
->m_len
,
1146 (wait
)? M_WAIT
: M_DONTWAIT
)) == NULL
)
1152 Copy an mbuf chain, referencing existing external storage, if any.
1153 Leave space for a header in the new chain, if the space has been
1154 left in the origin chain.
1156 struct mbuf
*copy_pkt(mlist
, pad
)
1157 struct mbuf
*mlist
; /* the mbuf chain to be copied */
1158 int pad
; /* hint as to how long the header might be
1159 If pad is < 0, leave the same amount of space
1160 as there was in the original. */
1166 len
= m_leadingspace(mlist
);
1168 len
= min(pad
, m_leadingspace(mlist
));
1170 /* preserve space for the header at the beginning of the mbuf */
1172 mlist
->m_data
-= (len
);
1173 mlist
->m_len
+= (len
);
1174 if (mlist
->m_flags
& M_PKTHDR
)
1175 mlist
->m_pkthdr
.len
+= (len
);
1176 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1180 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1185 void gbuf_linkb(m1
, m2
)
1189 while (gbuf_cont(m1
) != 0)
1194 void gbuf_linkpkt(m1
, m2
)
1198 while (gbuf_next(m1
) != 0)
1208 while ((tmp_m
= m
) != 0) {
1210 gbuf_next(tmp_m
) = 0;
1216 /* free empty mbufs at the front of the chain */
1217 gbuf_t
*gbuf_strip(m
)
1222 while (m
&& gbuf_len(m
) == 0) {
1230 /**************************************/
1232 int ddp_adjmsg(m
, len
)
1237 gbuf_t
*curr_m
, *prev_m
;
1239 if (m
== (gbuf_t
*)0)
1243 for (curr_m
=m
; curr_m
;) {
1244 buf_len
= gbuf_len(curr_m
);
1245 if (len
< buf_len
) {
1246 gbuf_rinc(curr_m
,len
);
1250 gbuf_rinc(curr_m
,buf_len
);
1251 if ((curr_m
= gbuf_cont(curr_m
)) == 0) {
1257 } else if (len
< 0) {
1260 for (curr_m
=m
; gbuf_cont(curr_m
);
1261 prev_m
=curr_m
, curr_m
=gbuf_cont(curr_m
)) ;
1262 buf_len
= gbuf_len(curr_m
);
1263 if (len
< buf_len
) {
1264 gbuf_wdec(curr_m
,len
);
1269 gbuf_cont(prev_m
) = 0;
1279 * The message chain, m is grown in size by len contiguous bytes.
1280 * If len is non-negative, len bytes are added to the
1281 * end of the gbuf_t chain. If len is negative, the
1282 * bytes are added to the front. ddp_growmsg only adds bytes to
1283 * message blocks of the same type.
1284 * It returns a pointer to the new gbuf_t on sucess, 0 on failure.
1287 gbuf_t
*ddp_growmsg(mp
, len
)
1293 if ((m
= mp
) == (gbuf_t
*) 0)
1294 return ((gbuf_t
*) 0);
1298 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1299 return ((gbuf_t
*) 0);
1300 gbuf_set_type(d
, gbuf_type(m
));
1302 /* link in new gbuf_t */
1311 if ((count
= gbuf_msgsize(m
)) < 0)
1312 return ((gbuf_t
*) 0);
1313 /* find end of chain */
1314 for ( ; m
; m
= gbuf_cont(m
)) {
1315 if (gbuf_len(m
) >= count
)
1317 count
-= gbuf_len(m
);
1319 /* m now points to gbuf_t to add to */
1320 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1321 return ((gbuf_t
*) 0);
1322 gbuf_set_type(d
, gbuf_type(m
));
1323 /* link in new gbuf_t */
1324 gbuf_cont(d
) = gbuf_cont(m
);
1332 * return the MSG_IOCACK/MSG_IOCNAK. Note that the same message
1333 * block is used as the vehicle, and that if there is an error return,
1334 * then linked blocks are lopped off. BEWARE of multiple references.
1335 * Used by other appletalk modules, so it is not static!
1338 void ioc_ack(errno
, m
, gref
)
1341 register gref_t
*gref
;
1343 ioc_t
*iocbp
= (ioc_t
*)gbuf_rptr(m
);
1345 /*kprintf("ioc_ack: m=%x gref=%x errno=%d\n", m, gref, errno);*/
1346 if ((iocbp
->ioc_error
= errno
) != 0)
1347 { /* errno != 0, then there is an error, get rid of linked blocks! */
1350 gbuf_freem(gbuf_cont(m
));
1353 gbuf_set_type(m
, MSG_IOCNAK
);
1354 iocbp
->ioc_count
= 0; /* only make zero length if error */
1355 iocbp
->ioc_rval
= -1;
1357 gbuf_set_type(m
, MSG_IOCACK
);
1359 atalk_putnext(gref
, m
);
1363 static void ioccmd_t_32_to_64( ioccmd_t
*from_p
, user_ioccmd_t
*to_p
)
1365 to_p
->ic_cmd
= from_p
->ic_cmd
;
1366 to_p
->ic_timout
= from_p
->ic_timout
;
1367 to_p
->ic_len
= from_p
->ic_len
;
1368 to_p
->ic_dp
= CAST_USER_ADDR_T(from_p
->ic_dp
);
1372 static void ioccmd_t_64_to_32( user_ioccmd_t
*from_p
, ioccmd_t
*to_p
)
1374 to_p
->ic_cmd
= from_p
->ic_cmd
;
1375 to_p
->ic_timout
= from_p
->ic_timout
;
1376 to_p
->ic_len
= from_p
->ic_len
;
1377 to_p
->ic_dp
= CAST_DOWN(caddr_t
, from_p
->ic_dp
);