2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1995 Apple Computer, Inc.
27 * Created, March 17, 1997 by Tuyen Nguyen for MacOSX.
30 #include <sys/errno.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <machine/spl.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/filedesc.h>
38 #include <sys/fcntl.h>
39 #include <sys/file_internal.h>
41 #include <sys/ioctl.h>
42 #include <sys/malloc.h>
43 #include <kern/locks.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/ioccom.h>
47 #include <sys/uio_internal.h>
49 #include <sys/sysctl.h>
53 #include <netat/sysglue.h>
54 #include <netat/appletalk.h>
55 #include <netat/ddp.h>
56 #include <netat/at_pcb.h>
57 #include <netat/at_var.h>
58 #include <netat/routing_tables.h>
59 #include <netat/debug.h>
61 extern struct atpcb ddp_head
;
62 extern lck_mtx_t
* atalk_mutex
;
65 ddp_putmsg(gref_t
*gref
, gbuf_t
*m
),
66 elap_wput(gref_t
*gref
, gbuf_t
*m
),
67 atp_wput(gref_t
*gref
, gbuf_t
*m
),
68 asp_wput(gref_t
*gref
, gbuf_t
*m
),
70 aurp_wput(gref_t
*gref
, gbuf_t
*m
),
72 adsp_wput(gref_t
*gref
, gbuf_t
*m
);
74 int atp_free_cluster_timeout_set
= 0;
77 void atalk_putnext(gref_t
*gref
, gbuf_t
*m
);
78 /* bms: make gref_close non static so its callable from kernel */
79 int gref_close(gref_t
*gref
);
81 SYSCTL_DECL(_net_appletalk
);
83 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, debug
, CTLFLAG_WR
,
84 &dbgBits
, dbgBits
, "AppleTalk Debug Flags");
85 volatile int RouterMix
= RT_MIX_DEFAULT
; /* default for nbr of ppsec */
86 SYSCTL_INT(_net_appletalk
, OID_AUTO
, routermix
, CTLFLAG_WR
,
87 (int *)&RouterMix
, 0, "Appletalk RouterMix");
88 at_ddp_stats_t at_ddp_stats
; /* DDP statistics */
89 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, ddpstats
, CTLFLAG_RD
,
90 &at_ddp_stats
, at_ddp_stats
, "AppleTalk DDP Stats");
92 static void ioccmd_t_32_to_64( ioccmd_t
*from_p
, user_ioccmd_t
*to_p
);
93 static void ioccmd_t_64_to_32( user_ioccmd_t
*from_p
, ioccmd_t
*to_p
);
97 caddr_t atp_free_cluster_list
= 0;
99 void gref_wput(gref
, m
)
103 switch (gref
->proto
) {
105 ddp_putmsg(gref
, m
); break;
107 elap_wput(gref
, m
); break;
109 atp_wput(gref
, m
); break;
111 asp_wput(gref
, m
); break;
114 aurp_wput(gref
, m
); break;
117 adsp_wput(gref
, m
); break;
119 if (gbuf_type(m
) == MSG_IOCTL
) {
120 gbuf_freem(gbuf_cont(m
));
122 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= -1;
123 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= EPROTOTYPE
;
124 gbuf_set_type(m
, MSG_IOCNAK
);
125 atalk_putnext(gref
, m
);
135 int _ATsocket(proto
, err
, proc
)
143 /* make sure the specified protocol id is valid */
146 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
147 BSD-style socket interface. */
156 #ifdef APPLETALK_DEBUG
157 kprintf("_ATsocket: error EPROTOTYPE =%d\n", *err
);
162 /* allocate a protocol channel */
163 if ((*err
= gref_alloc(&gref
)) != 0) {
164 #ifdef APPLETALK_DEBUG
165 kprintf("_ATsocket: error gref_open =%d\n", *err
);
170 gref
->pid
= proc_pid((struct proc
*)proc
);
172 /* open the specified protocol */
173 switch (gref
->proto
) {
175 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
176 BSD-style socket interface. */
179 *err
= atp_open(gref
, 1); break;
181 *err
= asp_open(gref
); break;
184 *err
= aurp_open(gref
); break;
187 *err
= adsp_open(gref
); break;
190 /* create the descriptor for the channel */
192 #ifdef APPLETALK_DEBUG
193 kprintf("_ATsocket: open failed for %d proto; err = %d\n",
196 gref
->proto
= ATPROTO_NONE
;
198 if (*err
|| (*err
= atalk_openref(gref
, &fd
, proc
))) {
199 #ifdef APPLETALK_DEBUG
200 kprintf("_ATsocket: error atalk_openref =%d\n", *err
);
202 (void)gref_close(gref
);
206 kprintf("_ATsocket: proto=%d return=%d fd=%d\n", proto, *err, fd);
211 int _ATgetmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
222 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) == 0) {
223 switch (gref
->proto
) {
225 rc
= ASPgetmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
);
229 rc
= AURPgetmsg(err
);
233 *err
= EPROTONOSUPPORT
;
239 /* kprintf("_ATgetmsg: return=%d\n", *err);*/
243 int _ATputmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
254 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) == 0) {
255 switch (gref
->proto
) {
257 rc
= ASPputmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
); break;
259 *err
= EPROTONOSUPPORT
; break;
264 /* kprintf("_ATputmsg: return=%d\n", *err); */
268 int _ATclose(fg
, proc
)
275 if ((err
= atalk_closeref(fg
, &gref
)) == 0) {
277 (void)gref_close(gref
);
284 int _ATrw(fp
, rw
, uio
, p
)
290 int s
, err
, len
, clen
= 0, res
;
292 gbuf_t
*m
, *mhead
, *mprev
;
294 /* no need to get/drop iocount as the fp already has one */
295 if ((err
= atalk_getref_locked(fp
, 0, &gref
, p
, 1)) != 0)
298 // LP64todo - fix this!
299 if ((len
= uio_resid(uio
)) == 0)
302 ATDISABLE(s
, gref
->lock
);
304 if (rw
== UIO_READ
) {
305 KERNEL_DEBUG(DBG_ADSP_ATRW
, 0, gref
, len
, gref
->rdhead
, 0);
306 while ((gref
->errno
== 0) && ((mhead
= gref
->rdhead
) == 0)) {
307 gref
->sevents
|= POLLMSG
;
308 err
= msleep(&gref
->event
, atalk_mutex
, PSOCK
| PCATCH
, "AT read", 0);
309 gref
->sevents
&= ~POLLMSG
;
311 ATENABLE(s
, gref
->lock
);
314 KERNEL_DEBUG(DBG_ADSP_ATRW
, 1, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
318 ATENABLE(s
, gref
->lock
);
321 if ((gref
->rdhead
= gbuf_next(mhead
)) == 0)
324 KERNEL_DEBUG(DBG_ADSP_ATRW
, 2, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
326 ATENABLE(s
, gref
->lock
);
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 ATDISABLE(s
, gref
->lock
);
359 if (gref
->rdhead
== 0)
361 gbuf_next(m
) = gref
->rdhead
;
363 ATENABLE(s
, gref
->lock
);
368 // simple_unlock(&gref->lock);
370 if (gref
->writeable
) {
371 while (!(*gref
->writeable
)(gref
)) {
372 /* flow control on, wait to be enabled to write */
373 gref
->sevents
|= POLLSYNC
;
374 err
= msleep(&gref
->event
, atalk_mutex
, PSOCK
| PCATCH
, "AT write", 0);
375 gref
->sevents
&= ~POLLSYNC
;
377 ATENABLE(s
, gref
->lock
);
383 ATENABLE(s
, gref
->lock
);
385 /* allocate a buffer to copy in the write data */
386 if ((m
= gbuf_alloc(AT_WR_OFFSET
+len
, PRI_MED
)) == 0)
388 gbuf_rinc(m
,AT_WR_OFFSET
);
391 /* copy in the write data */
392 uio
->uio_rw
= UIO_WRITE
;
393 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
), len
, uio
))) {
394 #ifdef APPLETALK_DEBUG
395 kprintf("_ATrw: UIO_WRITE: res=%d\n", res
);
401 /* forward the write data to the appropriate protocol module */
408 int _ATread(fp
, uio
, cred
, flags
, p
)
418 stat
= _ATrw(fp
, UIO_READ
, uio
, p
);
423 int _ATwrite(fp
, uio
, cred
, flags
, p
)
433 stat
= _ATrw(fp
, UIO_WRITE
, uio
, p
);
439 /* Most of the processing from _ATioctl, so that it can be called
440 from the new ioctl code */
441 /* bms: update to be callable from kernel */
442 int at_ioctl(gref_t
*gref
, u_long cmd
, caddr_t arg
, int fromKernel
)
448 user_addr_t user_arg
;
449 user_ioccmd_t user_ioccmd
;
452 /* error if not for us */
453 if ((cmd
& 0xffff) != 0xff99)
456 size
= IOCPARM_LEN(cmd
);
457 if (size
!= sizeof(user_addr_t
))
460 user_arg
= *((user_addr_t
*)arg
);
462 /* copy in ioc command info */
463 is64bit
= proc_is64bit(current_proc());
466 bcopy (CAST_DOWN(caddr_t
, user_arg
), &tmp
, sizeof (tmp
));
467 ioccmd_t_32_to_64(&tmp
, &user_ioccmd
);
471 err
= copyin(user_arg
, (caddr_t
)&user_ioccmd
, sizeof(user_ioccmd
));
475 err
= copyin(user_arg
, (caddr_t
)&tmp
, sizeof(tmp
));
476 ioccmd_t_32_to_64(&tmp
, &user_ioccmd
);
479 #ifdef APPLETALK_DEBUG
480 kprintf("at_ioctl: err = %d, copyin(%llx, %x, %d)\n", err
,
481 user_arg
, (caddr_t
)&user_ioccmd
, sizeof(user_ioccmd
));
487 /* allocate a buffer to create an ioc command
488 first mbuf contains ioc command */
489 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) == 0)
491 gbuf_wset(m
, sizeof(ioc_t
)); /* mbuf->m_len */
492 gbuf_set_type(m
, MSG_IOCTL
); /* mbuf->m_type */
494 /* create the ioc command
495 second mbuf contains the actual ASP command */
496 if (user_ioccmd
.ic_len
) {
497 if ((gbuf_cont(m
) = gbuf_alloc(user_ioccmd
.ic_len
, PRI_HI
)) == 0) {
499 #ifdef APPLETALK_DEBUG
500 kprintf("at_ioctl: gbuf_alloc err=%d\n",ENOBUFS
);
504 gbuf_wset(gbuf_cont(m
), user_ioccmd
.ic_len
); /* mbuf->m_len */
506 bcopy (CAST_DOWN(caddr_t
, user_ioccmd
.ic_dp
), gbuf_rptr(gbuf_cont(m
)), user_ioccmd
.ic_len
);
508 if ((err
= copyin(user_ioccmd
.ic_dp
, (caddr_t
)gbuf_rptr(gbuf_cont(m
)), user_ioccmd
.ic_len
)) != 0) {
514 ioc
= (ioc_t
*) gbuf_rptr(m
);
515 ioc
->ioc_cmd
= user_ioccmd
.ic_cmd
;
516 ioc
->ioc_count
= user_ioccmd
.ic_len
;
520 /* send the ioc command to the appropriate recipient */
523 /* wait for the ioc ack */
524 ATDISABLE(s
, gref
->lock
);
525 while ((m
= gref
->ichead
) == 0) {
526 gref
->sevents
|= POLLPRI
;
527 #ifdef APPLETALK_DEBUG
528 kprintf("sleep gref = 0x%x\n", (unsigned)gref
);
530 err
= msleep(&gref
->iocevent
, atalk_mutex
, PSOCK
| PCATCH
, "AT ioctl", 0);
531 gref
->sevents
&= ~POLLPRI
;
533 ATENABLE(s
, gref
->lock
);
534 #ifdef APPLETALK_DEBUG
535 kprintf("at_ioctl: EINTR\n");
542 if (gbuf_next(m
) == m
) /* error case */
545 gref
->ichead
= gbuf_next(m
);
547 ATENABLE(s
, gref
->lock
);
549 #ifdef APPLETALK_DEBUG
550 kprintf("at_ioctl: woke up from ioc sleep gref = 0x%x\n",
554 /* process the ioc response */
555 ioc
= (ioc_t
*) gbuf_rptr(m
);
556 if ((err
= ioc
->ioc_error
) == 0) {
557 user_ioccmd
.ic_timout
= ioc
->ioc_rval
;
558 user_ioccmd
.ic_len
= 0;
559 mdata
= gbuf_cont(m
);
560 if (mdata
&& user_ioccmd
.ic_dp
) {
561 user_ioccmd
.ic_len
= gbuf_msgsize(mdata
);
562 for (len
= 0; mdata
; mdata
= gbuf_cont(mdata
)) {
564 bcopy (gbuf_rptr(mdata
), CAST_DOWN(caddr_t
, (user_ioccmd
.ic_dp
+ len
)), gbuf_len(mdata
));
566 if ((err
= copyout((caddr_t
)gbuf_rptr(mdata
), (user_ioccmd
.ic_dp
+ len
), gbuf_len(mdata
))) < 0) {
567 #ifdef APPLETALK_DEBUG
568 kprintf("at_ioctl: len=%d error copyout=%d from=%x to=%x gbuf_len=%x\n",
569 len
, err
, (caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&user_ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
574 len
+= gbuf_len(mdata
);
580 ioccmd_t_64_to_32(&user_ioccmd
, &tmp
);
581 bcopy (&tmp
, CAST_DOWN(caddr_t
, user_arg
), sizeof(tmp
));
585 err
= copyout((caddr_t
)&user_ioccmd
, user_arg
, sizeof(user_ioccmd
));
589 ioccmd_t_64_to_32(&user_ioccmd
, &tmp
);
590 err
= copyout((caddr_t
)&tmp
, user_arg
, sizeof(tmp
));
600 /*kprintf("at_ioctl: I_done=%d\n", err);*/
604 int _ATioctl(fp
, cmd
, arg
, proc
)
607 register caddr_t arg
;
614 /* No need to get a reference on fp as it already has one */
615 if ((err
= atalk_getref_locked(fp
, 0, &gref
, 0, 0)) != 0) {
616 #ifdef APPLETALK_DEBUG
617 kprintf("_ATioctl: atalk_getref err = %d\n", err
);
621 err
= at_ioctl(gref
, cmd
, arg
, 0);
628 int _ATselect(fp
, which
, wql
, proc
)
637 /* Radar 4128949: Drop the proc_fd lock here to avoid lock inversion issues with the other AT calls
638 * select() is already holding a reference on the fd, so it won't go away during the time it is unlocked.
643 /* no need to drop the iocount as select covers that */
644 err
= atalk_getref_locked(fp
, 0, &gref
, 0, 0);
647 /* Safe to re-grab the proc_fdlock at that point */
652 ATDISABLE(s
, gref
->lock
);
653 if (which
== FREAD
) {
654 if (gref
->rdhead
|| (gref
->readable
&& (*gref
->readable
)(gref
)))
657 gref
->sevents
|= POLLIN
;
658 selrecord(proc
, &gref
->si
, wql
);
661 else if (which
== POLLOUT
) {
662 if (gref
->writeable
) {
663 if ((*gref
->writeable
)(gref
))
666 gref
->sevents
|= POLLOUT
;
667 selrecord(proc
, &gref
->si
, wql
);
672 ATENABLE(s
, gref
->lock
);
678 int _ATkqfilter(fp
, kn
, p
)
686 void atalk_putnext(gref
, m
)
692 ATDISABLE(s
, gref
->lock
);
694 /* *** potential leak? *** */
697 switch (gbuf_type(m
)) {
701 gbuf_next(gref
->ichead
) = m
;
704 if (gref
->sevents
& POLLPRI
) {
705 #ifdef APPLETALK_DEBUG
706 kprintf("wakeup gref = 0x%x\n", (unsigned)gref
);
708 wakeup(&gref
->iocevent
);
713 /* *** this processing was moved to atalk_notify *** */
714 panic("atalk_putnext receved MSG_ERROR");
721 gbuf_next(gref
->rdtail
) = m
;
725 if (gref
->sevents
& POLLMSG
) {
726 gref
->sevents
&= ~POLLMSG
;
727 wakeup(&gref
->event
);
729 if (gref
->sevents
& POLLIN
) {
730 gref
->sevents
&= ~POLLIN
;
731 selwakeup(&gref
->si
);
735 } /* switch gbuf_type(m) */
737 ATENABLE(s
, gref
->lock
);
738 } /* atalk_putnext */
740 void atalk_enablew(gref
)
743 if (gref
->sevents
& POLLSYNC
)
744 wakeup(&gref
->event
);
747 void atalk_flush(gref
)
752 ATDISABLE(s
, gref
->lock
);
754 gbuf_freel(gref
->rdhead
);
758 gbuf_freel(gref
->ichead
);
761 ATENABLE(s
, gref
->lock
);
765 * Notify an appletalk user of an asynchronous error;
766 * just wake up so that he can collect error status.
768 void atalk_notify(gref
, errno
)
769 register gref_t
*gref
;
773 ATDISABLE(s
, gref
->lock
);
775 if (gref
->atpcb_socket
) {
777 This section is patterned after udp_notify() in
780 gref
->atpcb_socket
->so_error
= errno
;
781 sorwakeup(gref
->atpcb_socket
);
782 sowwakeup(gref
->atpcb_socket
);
784 /* for ATP, ASP, and ADSP */
785 if (gref
->errno
== 0) {
787 /* clear out data waiting to be read */
789 gbuf_freel(gref
->rdhead
);
793 if (gref
->sevents
& POLLMSG
) {
794 gref
->sevents
&= ~POLLMSG
;
795 wakeup(&gref
->event
);
798 if (gref
->sevents
& POLLIN
) {
799 gref
->sevents
&= ~POLLIN
;
800 selwakeup(&gref
->si
);
804 ATENABLE(s
, gref
->lock
);
807 void atalk_notify_sel(gref
)
812 ATDISABLE(s
, gref
->lock
);
813 if (gref
->sevents
& POLLIN
) {
814 gref
->sevents
&= ~POLLIN
;
815 selwakeup(&gref
->si
);
817 ATENABLE(s
, gref
->lock
);
820 int atalk_peek(gref
, event
)
822 unsigned char *event
;
826 ATDISABLE(s
, gref
->lock
);
828 *event
= *gbuf_rptr(gref
->rdhead
);
832 ATENABLE(s
, gref
->lock
);
837 static gbuf_t
*trace_msg
;
839 void atalk_settrace(str
, p1
, p2
, p3
, p4
, p5
)
846 sprintf(trace_buf
, str
, p1
, p2
, p3
, p4
, p5
);
847 len
= strlen(trace_buf
);
848 #ifdef APPLETALK_DEBUG
849 kprintf("atalk_settrace: gbufalloc size=%d\n", len
+1);
851 if ((m
= gbuf_alloc(len
+1, PRI_MED
)) == 0)
854 strcpy(gbuf_rptr(m
), trace_buf
);
856 for (nextm
=trace_msg
; gbuf_cont(nextm
); nextm
=gbuf_cont(nextm
)) ;
857 gbuf_cont(nextm
) = m
;
862 void atalk_gettrace(m
)
866 gbuf_cont(m
) = trace_msg
;
871 #define GREF_PER_BLK 32
872 static gref_t
*gref_free_list
= 0;
874 int gref_alloc(grefp
)
877 extern gbuf_t
*atp_resource_m
;
880 gref_t
*gref
, *gref_array
;
882 *grefp
= (gref_t
*)NULL
;
884 ATDISABLE(s
, refall_lock
);
885 if (gref_free_list
== 0) {
886 ATENABLE(s
, refall_lock
);
887 #ifdef APPLETALK_DEBUG
888 kprintf("gref_alloc: gbufalloc size=%d\n", GREF_PER_BLK
*sizeof(gref_t
));
890 if ((m
= gbuf_alloc(GREF_PER_BLK
*sizeof(gref_t
),PRI_HI
)) == 0)
892 bzero(gbuf_rptr(m
), GREF_PER_BLK
*sizeof(gref_t
));
893 gref_array
= (gref_t
*)gbuf_rptr(m
);
894 for (i
=0; i
< GREF_PER_BLK
-1; i
++)
895 gref_array
[i
].atpcb_next
= (gref_t
*)&gref_array
[i
+1];
896 ATDISABLE(s
, refall_lock
);
897 gbuf_cont(m
) = atp_resource_m
;
899 gref_array
[i
].atpcb_next
= gref_free_list
;
900 gref_free_list
= (gref_t
*)&gref_array
[0];
903 gref
= gref_free_list
;
904 gref_free_list
= gref
->atpcb_next
;
905 ATENABLE(s
, refall_lock
);
906 ATLOCKINIT(gref
->lock
);
907 //### LD Test 08/05/98
908 // simple_lock_init(&gref->lock);
909 ATEVENTINIT(gref
->event
);
910 ATEVENTINIT(gref
->iocevent
);
912 /* *** just for now *** */
913 gref
->atpcb_socket
= (struct socket
*)NULL
;
919 /* bms: make gref_close callable from kernel */
920 int gref_close(gref_t
*gref
)
924 switch (gref
->proto
) {
926 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
927 BSD-style socket interface. */
930 rc
= atp_close(gref
, 1); break;
932 rc
= asp_close(gref
); break;
935 rc
= aurp_close(gref
); break;
939 rc
= adsp_close(gref
); break;
947 selthreadclear(&gref
->si
);
949 /* from original gref_free() */
950 ATDISABLE(s
, refall_lock
);
951 bzero((char *)gref
, sizeof(gref_t
));
952 gref
->atpcb_next
= gref_free_list
;
953 gref_free_list
= gref
;
954 ATENABLE(s
, refall_lock
);
963 *** Some to be replaced with mbuf routines, some to be re-written
964 as mbuf routines (and moved to kern/uicp_mbuf.c or sys/mbuf.h?).
970 * LD 5/12/97 Added for MacOSX, defines a m_clattach function that:
971 * "Allocates an mbuf structure and attaches an external cluster."
974 struct mbuf
*m_clattach(extbuf
, extfree
, extsize
, extarg
, wait
)
976 void (*extfree
)(caddr_t
, u_int
, caddr_t
);
983 if ((m
= m_gethdr(wait
, MSG_DATA
)) == NULL
)
986 m
->m_ext
.ext_buf
= extbuf
;
987 m
->m_ext
.ext_free
= extfree
;
988 m
->m_ext
.ext_size
= extsize
;
989 m
->m_ext
.ext_arg
= extarg
;
990 m
->m_ext
.ext_refs
.forward
=
991 m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
1001 temp fix for bug 2731148 - until this code is re-written to use standard clusters
1002 Deletes any free clusters on the free list.
1004 void atp_delete_free_clusters()
1007 caddr_t cluster_list
;
1010 /* check for free clusters on the free_cluster_list to be deleted */
1011 MBUF_LOCK(); /* lock used by mbuf routines */
1013 untimeout(&atp_delete_free_clusters
, NULL
);
1014 atp_free_cluster_timeout_set
= 0;
1016 cluster_list
= atp_free_cluster_list
;
1017 atp_free_cluster_list
= 0;
1021 while (cluster
= cluster_list
)
1023 cluster_list
= *((caddr_t
*)cluster
);
1024 FREE(cluster
, M_MCLUST
);
1031 Used as the "free" routine for over-size clusters allocated using
1032 m_lgbuf_alloc(). Called by m_free while under MBUF_LOCK.
1035 void m_lgbuf_free(buf
, size
, arg
)
1038 caddr_t arg
; /* not needed, but they're in m_free() */
1040 /* FREE(buf, M_MCLUST); - can't free here - called from m_free while under lock */
1042 /* move to free_cluster_list to be deleted later */
1043 caddr_t cluster
= (caddr_t
)buf
;
1045 /* don't need a lock because this is only called called from m_free which */
1046 /* is under MBUF_LOCK */
1047 *((caddr_t
*)cluster
) = atp_free_cluster_list
;
1048 atp_free_cluster_list
= cluster
;
1050 if (atp_free_cluster_timeout_set
== 0)
1052 atp_free_cluster_timeout_set
= 1;
1053 timeout(&atp_delete_free_clusters
, NULL
, (1 * HZ
));
1058 Used to allocate an mbuf when there is the possibility that it may
1059 need to be larger than the size of a standard cluster.
1062 struct mbuf
*m_lgbuf_alloc(size
, wait
)
1067 if (atp_free_cluster_list
)
1068 atp_delete_free_clusters(); /* delete any free clusters on the free list */
1070 /* If size is too large, allocate a cluster, otherwise, use the
1071 standard mbuf allocation routines.*/
1072 if (size
> MCLBYTES
) {
1075 (buf
= (void *)_MALLOC(size
, M_MCLUST
,
1076 (wait
)? M_WAITOK
: M_NOWAIT
))) {
1080 (m
= m_clattach(buf
, m_lgbuf_free
, size
, 0,
1081 (wait
)? M_WAIT
: M_DONTWAIT
))) {
1082 m_lgbuf_free(buf
, 0, 0);
1086 m
= m_gethdr(((wait
)? M_WAIT
: M_DONTWAIT
), MSG_DATA
);
1087 if (m
&& (size
> MHLEN
)) {
1088 MCLGET(m
, ((wait
)? M_WAIT
: M_DONTWAIT
));
1089 if (!(m
->m_flags
& M_EXT
)) {
1097 } /* m_lgbuf_alloc */
1100 gbuf_alloc() is a wrapper for m_lgbuf_alloc(), which is used to
1101 allocate an mbuf when there is the possibility that it may need
1102 to be larger than the size of a standard cluster.
1104 gbuf_alloc() sets the mbuf lengths, unlike the standard mbuf routines.
1107 gbuf_t
*gbuf_alloc_wait(size
, wait
)
1110 gbuf_t
*m
= (gbuf_t
*)m_lgbuf_alloc(size
, wait
);
1112 /* Standard mbuf allocation routines assume that the caller
1113 will set the size. */
1115 (struct mbuf
*)m
->m_pkthdr
.len
= size
;
1116 (struct mbuf
*)m
->m_len
= size
;
1127 for (size
=0; m
; m
=gbuf_cont(m
))
1128 size
+= gbuf_len(m
);
1132 int append_copy(m1
, m2
, wait
)
1133 struct mbuf
*m1
, *m2
;
1136 if ((!(m1
->m_flags
& M_EXT
)) && (!(m2
->m_flags
& M_EXT
)) &&
1137 (m_trailingspace(m1
) >= m2
->m_len
)) {
1138 /* splat the data from one into the other */
1139 bcopy(mtod(m2
, caddr_t
), mtod(m1
, caddr_t
) + m1
->m_len
,
1141 m1
->m_len
+= m2
->m_len
;
1142 if (m1
->m_flags
& M_PKTHDR
)
1143 m1
->m_pkthdr
.len
+= m2
->m_len
;
1146 if ((m1
->m_next
= m_copym(m2
, 0, m2
->m_len
,
1147 (wait
)? M_WAIT
: M_DONTWAIT
)) == NULL
)
1153 Copy an mbuf chain, referencing existing external storage, if any.
1154 Leave space for a header in the new chain, if the space has been
1155 left in the origin chain.
1157 struct mbuf
*copy_pkt(mlist
, pad
)
1158 struct mbuf
*mlist
; /* the mbuf chain to be copied */
1159 int pad
; /* hint as to how long the header might be
1160 If pad is < 0, leave the same amount of space
1161 as there was in the original. */
1167 len
= m_leadingspace(mlist
);
1169 len
= min(pad
, m_leadingspace(mlist
));
1171 /* preserve space for the header at the beginning of the mbuf */
1173 mlist
->m_data
-= (len
);
1174 mlist
->m_len
+= (len
);
1175 if (mlist
->m_flags
& M_PKTHDR
)
1176 mlist
->m_pkthdr
.len
+= (len
);
1177 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1181 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1186 void gbuf_linkb(m1
, m2
)
1190 while (gbuf_cont(m1
) != 0)
1195 void gbuf_linkpkt(m1
, m2
)
1199 while (gbuf_next(m1
) != 0)
1209 while ((tmp_m
= m
) != 0) {
1211 gbuf_next(tmp_m
) = 0;
1217 /* free empty mbufs at the front of the chain */
1218 gbuf_t
*gbuf_strip(m
)
1223 while (m
&& gbuf_len(m
) == 0) {
1231 /**************************************/
1233 int ddp_adjmsg(m
, len
)
1238 gbuf_t
*curr_m
, *prev_m
;
1240 if (m
== (gbuf_t
*)0)
1244 for (curr_m
=m
; curr_m
;) {
1245 buf_len
= gbuf_len(curr_m
);
1246 if (len
< buf_len
) {
1247 gbuf_rinc(curr_m
,len
);
1251 gbuf_rinc(curr_m
,buf_len
);
1252 if ((curr_m
= gbuf_cont(curr_m
)) == 0) {
1258 } else if (len
< 0) {
1261 for (curr_m
=m
; gbuf_cont(curr_m
);
1262 prev_m
=curr_m
, curr_m
=gbuf_cont(curr_m
)) ;
1263 buf_len
= gbuf_len(curr_m
);
1264 if (len
< buf_len
) {
1265 gbuf_wdec(curr_m
,len
);
1270 gbuf_cont(prev_m
) = 0;
1280 * The message chain, m is grown in size by len contiguous bytes.
1281 * If len is non-negative, len bytes are added to the
1282 * end of the gbuf_t chain. If len is negative, the
1283 * bytes are added to the front. ddp_growmsg only adds bytes to
1284 * message blocks of the same type.
1285 * It returns a pointer to the new gbuf_t on sucess, 0 on failure.
1288 gbuf_t
*ddp_growmsg(mp
, len
)
1294 if ((m
= mp
) == (gbuf_t
*) 0)
1295 return ((gbuf_t
*) 0);
1299 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1300 return ((gbuf_t
*) 0);
1301 gbuf_set_type(d
, gbuf_type(m
));
1303 /* link in new gbuf_t */
1312 if ((count
= gbuf_msgsize(m
)) < 0)
1313 return ((gbuf_t
*) 0);
1314 /* find end of chain */
1315 for ( ; m
; m
= gbuf_cont(m
)) {
1316 if (gbuf_len(m
) >= count
)
1318 count
-= gbuf_len(m
);
1320 /* m now points to gbuf_t to add to */
1321 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1322 return ((gbuf_t
*) 0);
1323 gbuf_set_type(d
, gbuf_type(m
));
1324 /* link in new gbuf_t */
1325 gbuf_cont(d
) = gbuf_cont(m
);
1333 * return the MSG_IOCACK/MSG_IOCNAK. Note that the same message
1334 * block is used as the vehicle, and that if there is an error return,
1335 * then linked blocks are lopped off. BEWARE of multiple references.
1336 * Used by other appletalk modules, so it is not static!
1339 void ioc_ack(errno
, m
, gref
)
1342 register gref_t
*gref
;
1344 ioc_t
*iocbp
= (ioc_t
*)gbuf_rptr(m
);
1346 /*kprintf("ioc_ack: m=%x gref=%x errno=%d\n", m, gref, errno);*/
1347 if ((iocbp
->ioc_error
= errno
) != 0)
1348 { /* errno != 0, then there is an error, get rid of linked blocks! */
1351 gbuf_freem(gbuf_cont(m
));
1354 gbuf_set_type(m
, MSG_IOCNAK
);
1355 iocbp
->ioc_count
= 0; /* only make zero length if error */
1356 iocbp
->ioc_rval
= -1;
1358 gbuf_set_type(m
, MSG_IOCACK
);
1360 atalk_putnext(gref
, m
);
1364 static void ioccmd_t_32_to_64( ioccmd_t
*from_p
, user_ioccmd_t
*to_p
)
1366 to_p
->ic_cmd
= from_p
->ic_cmd
;
1367 to_p
->ic_timout
= from_p
->ic_timout
;
1368 to_p
->ic_len
= from_p
->ic_len
;
1369 to_p
->ic_dp
= CAST_USER_ADDR_T(from_p
->ic_dp
);
1373 static void ioccmd_t_64_to_32( user_ioccmd_t
*from_p
, ioccmd_t
*to_p
)
1375 to_p
->ic_cmd
= from_p
->ic_cmd
;
1376 to_p
->ic_timout
= from_p
->ic_timout
;
1377 to_p
->ic_len
= from_p
->ic_len
;
1378 to_p
->ic_dp
= CAST_DOWN(caddr_t
, from_p
->ic_dp
);