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/atp.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. */
158 #ifdef APPLETALK_DEBUG
159 kprintf("_ATsocket: error EPROTOTYPE =%d\n", *err
);
164 /* allocate a protocol channel */
165 if ((*err
= gref_alloc(&gref
)) != 0) {
166 #ifdef APPLETALK_DEBUG
167 kprintf("_ATsocket: error gref_open =%d\n", *err
);
172 gref
->pid
= proc_pid((struct proc
*)proc
);
174 /* open the specified protocol */
175 switch (gref
->proto
) {
177 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
178 BSD-style socket interface. */
181 *err
= atp_open(gref
, 1); break;
183 *err
= asp_open(gref
); break;
186 *err
= aurp_open(gref
); break;
189 *err
= adsp_open(gref
); break;
192 /* create the descriptor for the channel */
194 #ifdef APPLETALK_DEBUG
195 kprintf("_ATsocket: open failed for %d proto; err = %d\n",
198 gref
->proto
= ATPROTO_NONE
;
200 if (*err
|| (*err
= atalk_openref(gref
, &fd
, proc
))) {
201 #ifdef APPLETALK_DEBUG
202 kprintf("_ATsocket: error atalk_openref =%d\n", *err
);
204 (void)gref_close(gref
);
208 kprintf("_ATsocket: proto=%d return=%d fd=%d\n", proto, *err, fd);
213 int _ATgetmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
224 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) == 0) {
225 switch (gref
->proto
) {
227 rc
= ASPgetmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
);
231 rc
= AURPgetmsg(err
);
235 *err
= EPROTONOSUPPORT
;
241 /* kprintf("_ATgetmsg: return=%d\n", *err);*/
245 int _ATputmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
256 if ((*err
= atalk_getref(0, fd
, &gref
, proc
, 1)) == 0) {
257 switch (gref
->proto
) {
259 rc
= ASPputmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
); break;
261 *err
= EPROTONOSUPPORT
; break;
266 /* kprintf("_ATputmsg: return=%d\n", *err); */
270 int _ATclose(fg
, proc
)
277 if ((err
= atalk_closeref(fg
, &gref
)) == 0) {
279 (void)gref_close(gref
);
286 int _ATrw(fp
, rw
, uio
, p
)
292 int s
, err
, len
, clen
= 0, res
;
294 gbuf_t
*m
, *mhead
, *mprev
;
296 /* no need to get/drop iocount as the fp already has one */
297 if ((err
= atalk_getref_locked(fp
, 0, &gref
, p
, 1)) != 0)
300 // LP64todo - fix this!
301 if ((len
= uio_resid(uio
)) == 0)
304 ATDISABLE(s
, gref
->lock
);
306 if (rw
== UIO_READ
) {
307 KERNEL_DEBUG(DBG_ADSP_ATRW
, 0, gref
, len
, gref
->rdhead
, 0);
308 while ((gref
->errno
== 0) && ((mhead
= gref
->rdhead
) == 0)) {
309 gref
->sevents
|= POLLMSG
;
310 err
= msleep(&gref
->event
, atalk_mutex
, PSOCK
| PCATCH
, "AT read", 0);
311 gref
->sevents
&= ~POLLMSG
;
313 ATENABLE(s
, gref
->lock
);
316 KERNEL_DEBUG(DBG_ADSP_ATRW
, 1, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
320 ATENABLE(s
, gref
->lock
);
323 if ((gref
->rdhead
= gbuf_next(mhead
)) == 0)
326 KERNEL_DEBUG(DBG_ADSP_ATRW
, 2, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
328 ATENABLE(s
, gref
->lock
);
330 //##### LD TEST 08/05
331 // simple_lock(&gref->lock);
333 gbuf_next(mhead
) = 0;
335 for (mprev
=0, m
=mhead
; m
&& len
; len
-=clen
) {
336 if ((clen
= gbuf_len(m
)) > 0) {
339 uio
->uio_rw
= UIO_READ
;
340 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
),
342 KERNEL_DEBUG(DBG_ADSP_ATRW
, 3, m
, clen
,
346 if (gbuf_len(m
) > len
) {
355 KERNEL_DEBUG(DBG_ADSP_ATRW
, 4, m
, gbuf_len(m
), mprev
, gref
->rdhead
);
357 gbuf_cont(mprev
) = 0;
360 ATDISABLE(s
, gref
->lock
);
361 if (gref
->rdhead
== 0)
363 gbuf_next(m
) = gref
->rdhead
;
365 ATENABLE(s
, gref
->lock
);
370 // simple_unlock(&gref->lock);
372 if (gref
->writeable
) {
373 while (!(*gref
->writeable
)(gref
)) {
374 /* flow control on, wait to be enabled to write */
375 gref
->sevents
|= POLLSYNC
;
376 err
= msleep(&gref
->event
, atalk_mutex
, PSOCK
| PCATCH
, "AT write", 0);
377 gref
->sevents
&= ~POLLSYNC
;
379 ATENABLE(s
, gref
->lock
);
385 ATENABLE(s
, gref
->lock
);
387 /* allocate a buffer to copy in the write data */
388 if ((m
= gbuf_alloc(AT_WR_OFFSET
+len
, PRI_MED
)) == 0)
390 gbuf_rinc(m
,AT_WR_OFFSET
);
393 /* copy in the write data */
394 uio
->uio_rw
= UIO_WRITE
;
395 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
), len
, uio
))) {
396 #ifdef APPLETALK_DEBUG
397 kprintf("_ATrw: UIO_WRITE: res=%d\n", res
);
403 /* forward the write data to the appropriate protocol module */
410 int _ATread(fp
, uio
, cred
, flags
, p
)
420 stat
= _ATrw(fp
, UIO_READ
, uio
, p
);
425 int _ATwrite(fp
, uio
, cred
, flags
, p
)
435 stat
= _ATrw(fp
, UIO_WRITE
, uio
, p
);
441 /* Most of the processing from _ATioctl, so that it can be called
442 from the new ioctl code */
443 /* bms: update to be callable from kernel */
444 int at_ioctl(gref_t
*gref
, u_long cmd
, caddr_t arg
, int fromKernel
)
450 user_addr_t user_arg
;
451 user_ioccmd_t user_ioccmd
;
454 /* error if not for us */
455 if ((cmd
& 0xffff) != 0xff99)
458 size
= IOCPARM_LEN(cmd
);
459 if (size
!= sizeof(user_addr_t
))
462 user_arg
= *((user_addr_t
*)arg
);
464 /* copy in ioc command info */
465 is64bit
= proc_is64bit(current_proc());
468 bcopy (CAST_DOWN(caddr_t
, user_arg
), &tmp
, sizeof (tmp
));
469 ioccmd_t_32_to_64(&tmp
, &user_ioccmd
);
473 err
= copyin(user_arg
, (caddr_t
)&user_ioccmd
, sizeof(user_ioccmd
));
477 err
= copyin(user_arg
, (caddr_t
)&tmp
, sizeof(tmp
));
478 ioccmd_t_32_to_64(&tmp
, &user_ioccmd
);
481 #ifdef APPLETALK_DEBUG
482 kprintf("at_ioctl: err = %d, copyin(%llx, %x, %d)\n", err
,
483 user_arg
, (caddr_t
)&user_ioccmd
, sizeof(user_ioccmd
));
489 /* allocate a buffer to create an ioc command
490 first mbuf contains ioc command */
491 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) == 0)
493 gbuf_wset(m
, sizeof(ioc_t
)); /* mbuf->m_len */
494 gbuf_set_type(m
, MSG_IOCTL
); /* mbuf->m_type */
496 /* create the ioc command
497 second mbuf contains the actual ASP command */
498 if (user_ioccmd
.ic_len
) {
499 if ((gbuf_cont(m
) = gbuf_alloc(user_ioccmd
.ic_len
, PRI_HI
)) == 0) {
501 #ifdef APPLETALK_DEBUG
502 kprintf("at_ioctl: gbuf_alloc err=%d\n",ENOBUFS
);
506 gbuf_wset(gbuf_cont(m
), user_ioccmd
.ic_len
); /* mbuf->m_len */
508 bcopy (CAST_DOWN(caddr_t
, user_ioccmd
.ic_dp
), gbuf_rptr(gbuf_cont(m
)), user_ioccmd
.ic_len
);
510 if ((err
= copyin(user_ioccmd
.ic_dp
, (caddr_t
)gbuf_rptr(gbuf_cont(m
)), user_ioccmd
.ic_len
)) != 0) {
516 ioc
= (ioc_t
*) gbuf_rptr(m
);
517 ioc
->ioc_cmd
= user_ioccmd
.ic_cmd
;
518 ioc
->ioc_count
= user_ioccmd
.ic_len
;
522 /* send the ioc command to the appropriate recipient */
525 /* wait for the ioc ack */
526 ATDISABLE(s
, gref
->lock
);
527 while ((m
= gref
->ichead
) == 0) {
528 gref
->sevents
|= POLLPRI
;
529 #ifdef APPLETALK_DEBUG
530 kprintf("sleep gref = 0x%x\n", (unsigned)gref
);
532 err
= msleep(&gref
->iocevent
, atalk_mutex
, PSOCK
| PCATCH
, "AT ioctl", 0);
533 gref
->sevents
&= ~POLLPRI
;
535 ATENABLE(s
, gref
->lock
);
536 #ifdef APPLETALK_DEBUG
537 kprintf("at_ioctl: EINTR\n");
544 if (gbuf_next(m
) == m
) /* error case */
547 gref
->ichead
= gbuf_next(m
);
549 ATENABLE(s
, gref
->lock
);
551 #ifdef APPLETALK_DEBUG
552 kprintf("at_ioctl: woke up from ioc sleep gref = 0x%x\n",
556 /* process the ioc response */
557 ioc
= (ioc_t
*) gbuf_rptr(m
);
558 if ((err
= ioc
->ioc_error
) == 0) {
559 user_ioccmd
.ic_timout
= ioc
->ioc_rval
;
560 user_ioccmd
.ic_len
= 0;
561 mdata
= gbuf_cont(m
);
562 if (mdata
&& user_ioccmd
.ic_dp
) {
563 user_ioccmd
.ic_len
= gbuf_msgsize(mdata
);
564 for (len
= 0; mdata
; mdata
= gbuf_cont(mdata
)) {
566 bcopy (gbuf_rptr(mdata
), CAST_DOWN(caddr_t
, (user_ioccmd
.ic_dp
+ len
)), gbuf_len(mdata
));
568 if ((err
= copyout((caddr_t
)gbuf_rptr(mdata
), (user_ioccmd
.ic_dp
+ len
), gbuf_len(mdata
))) < 0) {
569 #ifdef APPLETALK_DEBUG
570 kprintf("at_ioctl: len=%d error copyout=%d from=%x to=%x gbuf_len=%x\n",
571 len
, err
, (caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&user_ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
576 len
+= gbuf_len(mdata
);
582 ioccmd_t_64_to_32(&user_ioccmd
, &tmp
);
583 bcopy (&tmp
, CAST_DOWN(caddr_t
, user_arg
), sizeof(tmp
));
587 err
= copyout((caddr_t
)&user_ioccmd
, user_arg
, sizeof(user_ioccmd
));
591 ioccmd_t_64_to_32(&user_ioccmd
, &tmp
);
592 err
= copyout((caddr_t
)&tmp
, user_arg
, sizeof(tmp
));
602 /*kprintf("at_ioctl: I_done=%d\n", err);*/
606 int _ATioctl(fp
, cmd
, arg
, proc
)
609 register caddr_t arg
;
616 /* No need to get a reference on fp as it already has one */
617 if ((err
= atalk_getref_locked(fp
, 0, &gref
, 0, 0)) != 0) {
618 #ifdef APPLETALK_DEBUG
619 kprintf("_ATioctl: atalk_getref err = %d\n", err
);
623 err
= at_ioctl(gref
, cmd
, arg
, 0);
630 int _ATselect(fp
, which
, wql
, proc
)
639 /* Radar 4128949: Drop the proc_fd lock here to avoid lock inversion issues with the other AT calls
640 * select() is already holding a reference on the fd, so it won't go away during the time it is unlocked.
645 /* no need to drop the iocount as select covers that */
646 err
= atalk_getref_locked(fp
, 0, &gref
, 0, 0);
649 /* Safe to re-grab the proc_fdlock at that point */
654 ATDISABLE(s
, gref
->lock
);
655 if (which
== FREAD
) {
656 if (gref
->rdhead
|| (gref
->readable
&& (*gref
->readable
)(gref
)))
659 gref
->sevents
|= POLLIN
;
660 selrecord(proc
, &gref
->si
, wql
);
663 else if (which
== POLLOUT
) {
664 if (gref
->writeable
) {
665 if ((*gref
->writeable
)(gref
))
668 gref
->sevents
|= POLLOUT
;
669 selrecord(proc
, &gref
->si
, wql
);
674 ATENABLE(s
, gref
->lock
);
680 int _ATkqfilter(fp
, kn
, p
)
688 void atalk_putnext(gref
, m
)
694 ATDISABLE(s
, gref
->lock
);
696 /* *** potential leak? *** */
699 switch (gbuf_type(m
)) {
703 gbuf_next(gref
->ichead
) = m
;
706 if (gref
->sevents
& POLLPRI
) {
707 #ifdef APPLETALK_DEBUG
708 kprintf("wakeup gref = 0x%x\n", (unsigned)gref
);
710 wakeup(&gref
->iocevent
);
715 /* *** this processing was moved to atalk_notify *** */
716 panic("atalk_putnext receved MSG_ERROR");
723 gbuf_next(gref
->rdtail
) = m
;
727 if (gref
->sevents
& POLLMSG
) {
728 gref
->sevents
&= ~POLLMSG
;
729 wakeup(&gref
->event
);
731 if (gref
->sevents
& POLLIN
) {
732 gref
->sevents
&= ~POLLIN
;
733 selwakeup(&gref
->si
);
737 } /* switch gbuf_type(m) */
739 ATENABLE(s
, gref
->lock
);
740 } /* atalk_putnext */
742 void atalk_enablew(gref
)
745 if (gref
->sevents
& POLLSYNC
)
746 wakeup(&gref
->event
);
749 void atalk_flush(gref
)
754 ATDISABLE(s
, gref
->lock
);
756 gbuf_freel(gref
->rdhead
);
760 gbuf_freel(gref
->ichead
);
763 ATENABLE(s
, gref
->lock
);
767 * Notify an appletalk user of an asynchronous error;
768 * just wake up so that he can collect error status.
770 void atalk_notify(gref
, errno
)
771 register gref_t
*gref
;
775 ATDISABLE(s
, gref
->lock
);
777 if (gref
->atpcb_socket
) {
779 This section is patterned after udp_notify() in
782 gref
->atpcb_socket
->so_error
= errno
;
783 sorwakeup(gref
->atpcb_socket
);
784 sowwakeup(gref
->atpcb_socket
);
786 /* for ATP, ASP, and ADSP */
787 if (gref
->errno
== 0) {
789 /* clear out data waiting to be read */
791 gbuf_freel(gref
->rdhead
);
795 if (gref
->sevents
& POLLMSG
) {
796 gref
->sevents
&= ~POLLMSG
;
797 wakeup(&gref
->event
);
800 if (gref
->sevents
& POLLIN
) {
801 gref
->sevents
&= ~POLLIN
;
802 selwakeup(&gref
->si
);
806 ATENABLE(s
, gref
->lock
);
809 void atalk_notify_sel(gref
)
814 ATDISABLE(s
, gref
->lock
);
815 if (gref
->sevents
& POLLIN
) {
816 gref
->sevents
&= ~POLLIN
;
817 selwakeup(&gref
->si
);
819 ATENABLE(s
, gref
->lock
);
822 int atalk_peek(gref
, event
)
824 unsigned char *event
;
828 ATDISABLE(s
, gref
->lock
);
830 *event
= *gbuf_rptr(gref
->rdhead
);
834 ATENABLE(s
, gref
->lock
);
839 static gbuf_t
*trace_msg
;
841 void atalk_settrace(str
, p1
, p2
, p3
, p4
, p5
)
848 sprintf(trace_buf
, str
, p1
, p2
, p3
, p4
, p5
);
849 len
= strlen(trace_buf
);
850 #ifdef APPLETALK_DEBUG
851 kprintf("atalk_settrace: gbufalloc size=%d\n", len
+1);
853 if ((m
= gbuf_alloc(len
+1, PRI_MED
)) == 0)
856 strcpy(gbuf_rptr(m
), trace_buf
);
858 for (nextm
=trace_msg
; gbuf_cont(nextm
); nextm
=gbuf_cont(nextm
)) ;
859 gbuf_cont(nextm
) = m
;
864 void atalk_gettrace(m
)
868 gbuf_cont(m
) = trace_msg
;
873 #define GREF_PER_BLK 32
874 static gref_t
*gref_free_list
= 0;
876 int gref_alloc(grefp
)
879 extern gbuf_t
*atp_resource_m
;
882 gref_t
*gref
, *gref_array
;
884 *grefp
= (gref_t
*)NULL
;
886 ATDISABLE(s
, refall_lock
);
887 if (gref_free_list
== 0) {
888 ATENABLE(s
, refall_lock
);
889 #ifdef APPLETALK_DEBUG
890 kprintf("gref_alloc: gbufalloc size=%d\n", GREF_PER_BLK
*sizeof(gref_t
));
892 if ((m
= gbuf_alloc(GREF_PER_BLK
*sizeof(gref_t
),PRI_HI
)) == 0)
894 bzero(gbuf_rptr(m
), GREF_PER_BLK
*sizeof(gref_t
));
895 gref_array
= (gref_t
*)gbuf_rptr(m
);
896 for (i
=0; i
< GREF_PER_BLK
-1; i
++)
897 gref_array
[i
].atpcb_next
= (gref_t
*)&gref_array
[i
+1];
898 ATDISABLE(s
, refall_lock
);
899 gbuf_cont(m
) = atp_resource_m
;
901 gref_array
[i
].atpcb_next
= gref_free_list
;
902 gref_free_list
= (gref_t
*)&gref_array
[0];
905 gref
= gref_free_list
;
906 gref_free_list
= gref
->atpcb_next
;
907 ATENABLE(s
, refall_lock
);
908 ATLOCKINIT(gref
->lock
);
909 //### LD Test 08/05/98
910 // simple_lock_init(&gref->lock);
911 ATEVENTINIT(gref
->event
);
912 ATEVENTINIT(gref
->iocevent
);
914 /* *** just for now *** */
915 gref
->atpcb_socket
= (struct socket
*)NULL
;
921 /* bms: make gref_close callable from kernel */
922 int gref_close(gref_t
*gref
)
926 switch (gref
->proto
) {
928 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
929 BSD-style socket interface. */
932 rc
= atp_close(gref
, 1); break;
934 rc
= asp_close(gref
); break;
937 rc
= aurp_close(gref
); break;
941 rc
= adsp_close(gref
); break;
949 selthreadclear(&gref
->si
);
951 /* from original gref_free() */
952 ATDISABLE(s
, refall_lock
);
953 bzero((char *)gref
, sizeof(gref_t
));
954 gref
->atpcb_next
= gref_free_list
;
955 gref_free_list
= gref
;
956 ATENABLE(s
, refall_lock
);
965 *** Some to be replaced with mbuf routines, some to be re-written
966 as mbuf routines (and moved to kern/uicp_mbuf.c or sys/mbuf.h?).
972 * LD 5/12/97 Added for MacOSX, defines a m_clattach function that:
973 * "Allocates an mbuf structure and attaches an external cluster."
976 struct mbuf
*m_clattach(extbuf
, extfree
, extsize
, extarg
, wait
)
978 void (*extfree
)(caddr_t
, u_int
, caddr_t
);
985 if ((m
= m_gethdr(wait
, MSG_DATA
)) == NULL
)
988 m
->m_ext
.ext_buf
= extbuf
;
989 m
->m_ext
.ext_free
= extfree
;
990 m
->m_ext
.ext_size
= extsize
;
991 m
->m_ext
.ext_arg
= extarg
;
992 m
->m_ext
.ext_refs
.forward
=
993 m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
1003 temp fix for bug 2731148 - until this code is re-written to use standard clusters
1004 Deletes any free clusters on the free list.
1006 void atp_delete_free_clusters()
1009 caddr_t cluster_list
;
1012 /* check for free clusters on the free_cluster_list to be deleted */
1013 MBUF_LOCK(); /* lock used by mbuf routines */
1015 untimeout(&atp_delete_free_clusters
, NULL
);
1016 atp_free_cluster_timeout_set
= 0;
1018 cluster_list
= atp_free_cluster_list
;
1019 atp_free_cluster_list
= 0;
1023 while (cluster
= cluster_list
)
1025 cluster_list
= *((caddr_t
*)cluster
);
1026 FREE(cluster
, M_MCLUST
);
1033 Used as the "free" routine for over-size clusters allocated using
1034 m_lgbuf_alloc(). Called by m_free while under MBUF_LOCK.
1037 void m_lgbuf_free(buf
, size
, arg
)
1040 caddr_t arg
; /* not needed, but they're in m_free() */
1042 /* FREE(buf, M_MCLUST); - can't free here - called from m_free while under lock */
1044 /* move to free_cluster_list to be deleted later */
1045 caddr_t cluster
= (caddr_t
)buf
;
1047 /* don't need a lock because this is only called called from m_free which */
1048 /* is under MBUF_LOCK */
1049 *((caddr_t
*)cluster
) = atp_free_cluster_list
;
1050 atp_free_cluster_list
= cluster
;
1052 if (atp_free_cluster_timeout_set
== 0)
1054 atp_free_cluster_timeout_set
= 1;
1055 timeout(&atp_delete_free_clusters
, NULL
, (1 * HZ
));
1060 Used to allocate an mbuf when there is the possibility that it may
1061 need to be larger than the size of a standard cluster.
1064 struct mbuf
*m_lgbuf_alloc(size
, wait
)
1069 if (atp_free_cluster_list
)
1070 atp_delete_free_clusters(NULL
); /* delete any free clusters on the free list */
1073 * check that the passed size is within admissible boundaries
1074 * The max data size being ASP of 4576 (8 * ATP_DATA_SIZE),
1075 * allow for extra space for control data
1078 if (size
< 0 || size
> (ATP_DATA_SIZE
* 10))
1081 /* If size is too large, allocate a cluster, otherwise, use the
1082 standard mbuf allocation routines.*/
1083 if (size
> MCLBYTES
) {
1086 (buf
= (void *)_MALLOC(size
, M_MCLUST
,
1087 (wait
)? M_WAITOK
: M_NOWAIT
))) {
1091 (m
= m_clattach(buf
, m_lgbuf_free
, size
, 0,
1092 (wait
)? M_WAIT
: M_DONTWAIT
))) {
1093 m_lgbuf_free(buf
, 0, 0);
1097 m
= m_gethdr(((wait
)? M_WAIT
: M_DONTWAIT
), MSG_DATA
);
1098 if (m
&& (size
> MHLEN
)) {
1099 MCLGET(m
, ((wait
)? M_WAIT
: M_DONTWAIT
));
1100 if (!(m
->m_flags
& M_EXT
)) {
1108 } /* m_lgbuf_alloc */
1111 gbuf_alloc() is a wrapper for m_lgbuf_alloc(), which is used to
1112 allocate an mbuf when there is the possibility that it may need
1113 to be larger than the size of a standard cluster.
1115 gbuf_alloc() sets the mbuf lengths, unlike the standard mbuf routines.
1118 gbuf_t
*gbuf_alloc_wait(size
, wait
)
1121 gbuf_t
*m
= (gbuf_t
*)m_lgbuf_alloc(size
, wait
);
1123 /* Standard mbuf allocation routines assume that the caller
1124 will set the size. */
1126 (struct mbuf
*)m
->m_pkthdr
.len
= size
;
1127 (struct mbuf
*)m
->m_len
= size
;
1138 for (size
=0; m
; m
=gbuf_cont(m
))
1139 size
+= gbuf_len(m
);
1143 int append_copy(m1
, m2
, wait
)
1144 struct mbuf
*m1
, *m2
;
1147 if ((!(m1
->m_flags
& M_EXT
)) && (!(m2
->m_flags
& M_EXT
)) &&
1148 (m_trailingspace(m1
) >= m2
->m_len
)) {
1149 /* splat the data from one into the other */
1150 bcopy(mtod(m2
, caddr_t
), mtod(m1
, caddr_t
) + m1
->m_len
,
1152 m1
->m_len
+= m2
->m_len
;
1153 if (m1
->m_flags
& M_PKTHDR
)
1154 m1
->m_pkthdr
.len
+= m2
->m_len
;
1157 if ((m1
->m_next
= m_copym(m2
, 0, m2
->m_len
,
1158 (wait
)? M_WAIT
: M_DONTWAIT
)) == NULL
)
1164 Copy an mbuf chain, referencing existing external storage, if any.
1165 Leave space for a header in the new chain, if the space has been
1166 left in the origin chain.
1168 struct mbuf
*copy_pkt(mlist
, pad
)
1169 struct mbuf
*mlist
; /* the mbuf chain to be copied */
1170 int pad
; /* hint as to how long the header might be
1171 If pad is < 0, leave the same amount of space
1172 as there was in the original. */
1178 len
= m_leadingspace(mlist
);
1180 len
= min(pad
, m_leadingspace(mlist
));
1182 /* preserve space for the header at the beginning of the mbuf */
1184 mlist
->m_data
-= (len
);
1185 mlist
->m_len
+= (len
);
1186 if (mlist
->m_flags
& M_PKTHDR
)
1187 mlist
->m_pkthdr
.len
+= (len
);
1188 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1192 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1197 void gbuf_linkb(m1
, m2
)
1201 while (gbuf_cont(m1
) != 0)
1206 void gbuf_linkpkt(m1
, m2
)
1210 while (gbuf_next(m1
) != 0)
1220 while ((tmp_m
= m
) != 0) {
1222 gbuf_next(tmp_m
) = 0;
1228 /* free empty mbufs at the front of the chain */
1229 gbuf_t
*gbuf_strip(m
)
1234 while (m
&& gbuf_len(m
) == 0) {
1242 /**************************************/
1244 int ddp_adjmsg(m
, len
)
1249 gbuf_t
*curr_m
, *prev_m
;
1251 if (m
== (gbuf_t
*)0)
1255 for (curr_m
=m
; curr_m
;) {
1256 buf_len
= gbuf_len(curr_m
);
1257 if (len
< buf_len
) {
1258 gbuf_rinc(curr_m
,len
);
1262 gbuf_rinc(curr_m
,buf_len
);
1263 if ((curr_m
= gbuf_cont(curr_m
)) == 0) {
1269 } else if (len
< 0) {
1272 for (curr_m
=m
; gbuf_cont(curr_m
);
1273 prev_m
=curr_m
, curr_m
=gbuf_cont(curr_m
)) ;
1274 buf_len
= gbuf_len(curr_m
);
1275 if (len
< buf_len
) {
1276 gbuf_wdec(curr_m
,len
);
1281 gbuf_cont(prev_m
) = 0;
1291 * The message chain, m is grown in size by len contiguous bytes.
1292 * If len is non-negative, len bytes are added to the
1293 * end of the gbuf_t chain. If len is negative, the
1294 * bytes are added to the front. ddp_growmsg only adds bytes to
1295 * message blocks of the same type.
1296 * It returns a pointer to the new gbuf_t on sucess, 0 on failure.
1299 gbuf_t
*ddp_growmsg(mp
, len
)
1305 if ((m
= mp
) == (gbuf_t
*) 0)
1306 return ((gbuf_t
*) 0);
1310 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1311 return ((gbuf_t
*) 0);
1312 gbuf_set_type(d
, gbuf_type(m
));
1314 /* link in new gbuf_t */
1323 if ((count
= gbuf_msgsize(m
)) < 0)
1324 return ((gbuf_t
*) 0);
1325 /* find end of chain */
1326 for ( ; m
; m
= gbuf_cont(m
)) {
1327 if (gbuf_len(m
) >= count
)
1329 count
-= gbuf_len(m
);
1331 /* m now points to gbuf_t to add to */
1332 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1333 return ((gbuf_t
*) 0);
1334 gbuf_set_type(d
, gbuf_type(m
));
1335 /* link in new gbuf_t */
1336 gbuf_cont(d
) = gbuf_cont(m
);
1344 * return the MSG_IOCACK/MSG_IOCNAK. Note that the same message
1345 * block is used as the vehicle, and that if there is an error return,
1346 * then linked blocks are lopped off. BEWARE of multiple references.
1347 * Used by other appletalk modules, so it is not static!
1350 void ioc_ack(errno
, m
, gref
)
1353 register gref_t
*gref
;
1355 ioc_t
*iocbp
= (ioc_t
*)gbuf_rptr(m
);
1357 /*kprintf("ioc_ack: m=%x gref=%x errno=%d\n", m, gref, errno);*/
1358 if ((iocbp
->ioc_error
= errno
) != 0)
1359 { /* errno != 0, then there is an error, get rid of linked blocks! */
1362 gbuf_freem(gbuf_cont(m
));
1365 gbuf_set_type(m
, MSG_IOCNAK
);
1366 iocbp
->ioc_count
= 0; /* only make zero length if error */
1367 iocbp
->ioc_rval
= -1;
1369 gbuf_set_type(m
, MSG_IOCACK
);
1371 atalk_putnext(gref
, m
);
1375 static void ioccmd_t_32_to_64( ioccmd_t
*from_p
, user_ioccmd_t
*to_p
)
1377 to_p
->ic_cmd
= from_p
->ic_cmd
;
1378 to_p
->ic_timout
= from_p
->ic_timout
;
1379 to_p
->ic_len
= from_p
->ic_len
;
1380 to_p
->ic_dp
= CAST_USER_ADDR_T(from_p
->ic_dp
);
1384 static void ioccmd_t_64_to_32( user_ioccmd_t
*from_p
, ioccmd_t
*to_p
)
1386 to_p
->ic_cmd
= from_p
->ic_cmd
;
1387 to_p
->ic_timout
= from_p
->ic_timout
;
1388 to_p
->ic_len
= from_p
->ic_len
;
1389 to_p
->ic_dp
= CAST_DOWN(caddr_t
, from_p
->ic_dp
);