2 * Copyright (c) 2000-2002 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>
39 #include <sys/ioctl.h>
40 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/ioccom.h>
45 #include <sys/sysctl.h>
49 #include <netat/sysglue.h>
50 #include <netat/appletalk.h>
51 #include <netat/ddp.h>
52 #include <netat/at_pcb.h>
53 #include <netat/at_var.h>
54 #include <netat/routing_tables.h>
55 #include <netat/debug.h>
57 extern struct atpcb ddp_head
;
60 ddp_putmsg(gref_t
*gref
, gbuf_t
*m
),
61 elap_wput(gref_t
*gref
, gbuf_t
*m
),
62 atp_wput(gref_t
*gref
, gbuf_t
*m
),
63 asp_wput(gref_t
*gref
, gbuf_t
*m
),
65 aurp_wput(gref_t
*gref
, gbuf_t
*m
),
67 adsp_wput(gref_t
*gref
, gbuf_t
*m
);
69 int atp_free_cluster_timeout_set
= 0;
72 void atalk_putnext(gref_t
*gref
, gbuf_t
*m
);
73 /* bms: make gref_close non static so its callable from kernel */
74 int gref_close(gref_t
*gref
);
76 SYSCTL_DECL(_net_appletalk
);
78 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, debug
, CTLFLAG_WR
,
79 &dbgBits
, dbgBits
, "AppleTalk Debug Flags");
80 volatile int RouterMix
= RT_MIX_DEFAULT
; /* default for nbr of ppsec */
81 SYSCTL_INT(_net_appletalk
, OID_AUTO
, routermix
, CTLFLAG_WR
,
82 (int *)&RouterMix
, 0, "Appletalk RouterMix");
83 at_ddp_stats_t at_ddp_stats
; /* DDP statistics */
84 SYSCTL_STRUCT(_net_appletalk
, OID_AUTO
, ddpstats
, CTLFLAG_RD
,
85 &at_ddp_stats
, at_ddp_stats
, "AppleTalk DDP Stats");
89 caddr_t atp_free_cluster_list
= 0;
91 void gref_wput(gref
, m
)
95 switch (gref
->proto
) {
97 ddp_putmsg(gref
, m
); break;
99 elap_wput(gref
, m
); break;
101 atp_wput(gref
, m
); break;
103 asp_wput(gref
, m
); break;
106 aurp_wput(gref
, m
); break;
109 adsp_wput(gref
, m
); break;
111 if (gbuf_type(m
) == MSG_IOCTL
) {
112 gbuf_freem(gbuf_cont(m
));
114 ((ioc_t
*)gbuf_rptr(m
))->ioc_rval
= -1;
115 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= EPROTO
;
116 gbuf_set_type(m
, MSG_IOCNAK
);
117 atalk_putnext(gref
, m
);
127 int _ATsocket(proto
, err
, proc
)
135 /* make sure the specified protocol id is valid */
138 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
139 BSD-style socket interface. */
148 #ifdef APPLETALK_DEBUG
149 kprintf("_ATsocket: error EPROTOTYPE =%d\n", *err
);
154 /* allocate a protocol channel */
155 if ((*err
= gref_alloc(&gref
)) != 0) {
156 #ifdef APPLETALK_DEBUG
157 kprintf("_ATsocket: error gref_open =%d\n", *err
);
162 gref
->pid
= ((struct proc
*)proc
)->p_pid
;
164 /* open the specified protocol */
165 switch (gref
->proto
) {
167 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
168 BSD-style socket interface. */
171 *err
= atp_open(gref
, 1); break;
173 *err
= asp_open(gref
); break;
176 *err
= aurp_open(gref
); break;
179 *err
= adsp_open(gref
); break;
182 /* create the descriptor for the channel */
184 #ifdef APPLETALK_DEBUG
185 kprintf("_ATsocket: open failed for %d proto; err = %d\n",
188 gref
->proto
= ATPROTO_NONE
;
190 if (*err
|| (*err
= atalk_openref(gref
, &fd
, proc
))) {
191 #ifdef APPLETALK_DEBUG
192 kprintf("_ATsocket: error atalk_openref =%d\n", *err
);
194 (void)gref_close(gref
);
198 kprintf("_ATsocket: proto=%d return=%d fd=%d\n", proto, *err, fd);
203 int _ATgetmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
214 if ((*err
= atalk_getref(0, fd
, &gref
, proc
)) == 0) {
215 switch (gref
->proto
) {
217 rc
= ASPgetmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
);
221 rc
= AURPgetmsg(err
);
225 *err
= EPROTONOSUPPORT
;
230 /* kprintf("_ATgetmsg: return=%d\n", *err);*/
234 int _ATputmsg(fd
, ctlptr
, datptr
, flags
, err
, proc
)
245 if ((*err
= atalk_getref(0, fd
, &gref
, proc
)) == 0) {
246 switch (gref
->proto
) {
248 rc
= ASPputmsg(gref
, ctlptr
, datptr
, NULL
, flags
, err
); break;
250 *err
= EPROTONOSUPPORT
; break;
254 /* kprintf("_ATputmsg: return=%d\n", *err); */
258 int _ATclose(fp
, proc
)
265 if ((err
= atalk_closeref(fp
, &gref
)) == 0) {
266 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
267 (void)gref_close(gref
);
268 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
274 int _ATrw(fp
, rw
, uio
, ext
)
280 int s
, err
, len
, clen
= 0, res
;
282 gbuf_t
*m
, *mhead
, *mprev
;
284 if ((err
= atalk_getref(fp
, 0, &gref
, 0)) != 0)
287 if ((len
= uio
->uio_resid
) == 0)
290 ATDISABLE(s
, gref
->lock
);
292 if (rw
== UIO_READ
) {
293 KERNEL_DEBUG(DBG_ADSP_ATRW
, 0, gref
, len
, gref
->rdhead
, 0);
294 while ((gref
->errno
== 0) && ((mhead
= gref
->rdhead
) == 0)) {
295 gref
->sevents
|= POLLMSG
;
296 err
= tsleep(&gref
->event
, PSOCK
| PCATCH
, "AT read", 0);
297 gref
->sevents
&= ~POLLMSG
;
299 ATENABLE(s
, gref
->lock
);
302 KERNEL_DEBUG(DBG_ADSP_ATRW
, 1, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
306 ATENABLE(s
, gref
->lock
);
309 if ((gref
->rdhead
= gbuf_next(mhead
)) == 0)
312 KERNEL_DEBUG(DBG_ADSP_ATRW
, 2, gref
, gref
->rdhead
, mhead
, gbuf_next(mhead
));
314 ATENABLE(s
, gref
->lock
);
316 //##### LD TEST 08/05
317 // simple_lock(&gref->lock);
319 gbuf_next(mhead
) = 0;
321 for (mprev
=0, m
=mhead
; m
&& len
; len
-=clen
) {
322 if ((clen
= gbuf_len(m
)) > 0) {
325 uio
->uio_rw
= UIO_READ
;
326 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
),
328 KERNEL_DEBUG(DBG_ADSP_ATRW
, 3, m
, clen
,
332 if (gbuf_len(m
) > len
) {
341 KERNEL_DEBUG(DBG_ADSP_ATRW
, 4, m
, gbuf_len(m
), mprev
, gref
->rdhead
);
343 gbuf_cont(mprev
) = 0;
346 ATDISABLE(s
, gref
->lock
);
347 if (gref
->rdhead
== 0)
349 gbuf_next(m
) = gref
->rdhead
;
351 ATENABLE(s
, gref
->lock
);
356 // simple_unlock(&gref->lock);
358 if (gref
->writeable
) {
359 while (!(*gref
->writeable
)(gref
)) {
360 /* flow control on, wait to be enabled to write */
361 gref
->sevents
|= POLLSYNC
;
362 err
= tsleep(&gref
->event
, PSOCK
| PCATCH
, "AT write", 0);
363 gref
->sevents
&= ~POLLSYNC
;
365 ATENABLE(s
, gref
->lock
);
371 ATENABLE(s
, gref
->lock
);
373 /* allocate a buffer to copy in the write data */
374 if ((m
= gbuf_alloc(AT_WR_OFFSET
+len
, PRI_MED
)) == 0)
376 gbuf_rinc(m
,AT_WR_OFFSET
);
379 /* copy in the write data */
380 uio
->uio_rw
= UIO_WRITE
;
381 if ((res
= uiomove((caddr_t
)gbuf_rptr(m
), len
, uio
))) {
382 #ifdef APPLETALK_DEBUG
383 kprintf("_ATrw: UIO_WRITE: res=%d\n", res
);
389 /* forward the write data to the appropriate protocol module */
396 int _ATread(fp
, uio
, cred
, flags
, p
)
405 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
406 stat
= _ATrw(fp
, UIO_READ
, uio
, 0);
407 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
411 int _ATwrite(fp
, uio
, cred
, flags
, p
)
421 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
422 stat
= _ATrw(fp
, UIO_WRITE
, uio
, 0);
423 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
428 /* Most of the processing from _ATioctl, so that it can be called
429 from the new ioctl code */
430 /* bms: update to be callable from kernel */
431 int at_ioctl(gref_t
*gref
, u_long cmd
, caddr_t arg
, int fromKernel
)
438 /* error if not for us */
439 if ((cmd
& 0xffff) != 0xff99)
442 /* copy in ioc command info */
444 kprintf("at_ioctl: arg ioccmd.ic_cmd=%x ic_len=%x gref->lock=%x, gref->event=%x\n",
445 ((ioccmd_t *)arg)->ic_cmd, ((ioccmd_t *)arg)->ic_len,
446 gref->lock, gref->event);
449 bcopy (arg
, &ioccmd
, sizeof (ioccmd_t
));
451 if ((err
= copyin((caddr_t
)arg
, (caddr_t
)&ioccmd
, sizeof(ioccmd_t
))) != 0) {
452 #ifdef APPLETALK_DEBUG
453 kprintf("at_ioctl: err = %d, copyin(%x, %x, %d)\n", err
,
454 (caddr_t
)arg
, (caddr_t
)&ioccmd
, sizeof(ioccmd_t
));
460 /* allocate a buffer to create an ioc command
461 first mbuf contains ioc command */
462 if ((m
= gbuf_alloc(sizeof(ioc_t
), PRI_HI
)) == 0)
464 gbuf_wset(m
, sizeof(ioc_t
)); /* mbuf->m_len */
465 gbuf_set_type(m
, MSG_IOCTL
); /* mbuf->m_type */
467 /* create the ioc command
468 second mbuf contains the actual ASP command */
470 if ((gbuf_cont(m
) = gbuf_alloc(ioccmd
.ic_len
, PRI_HI
)) == 0) {
472 #ifdef APPLETALK_DEBUG
473 kprintf("at_ioctl: gbuf_alloc err=%d\n",ENOBUFS
);
477 gbuf_wset(gbuf_cont(m
), ioccmd
.ic_len
); /* mbuf->m_len */
479 bcopy (ioccmd
.ic_dp
, gbuf_rptr(gbuf_cont(m
)), ioccmd
.ic_len
);
481 if ((err
= copyin((caddr_t
)ioccmd
.ic_dp
, (caddr_t
)gbuf_rptr(gbuf_cont(m
)), ioccmd
.ic_len
)) != 0) {
487 ioc
= (ioc_t
*) gbuf_rptr(m
);
488 ioc
->ioc_cmd
= ioccmd
.ic_cmd
;
489 ioc
->ioc_count
= ioccmd
.ic_len
;
493 /* send the ioc command to the appropriate recipient */
496 /* wait for the ioc ack */
497 ATDISABLE(s
, gref
->lock
);
498 while ((m
= gref
->ichead
) == 0) {
499 gref
->sevents
|= POLLPRI
;
500 #ifdef APPLETALK_DEBUG
501 kprintf("sleep gref = 0x%x\n", (unsigned)gref
);
503 err
= tsleep(&gref
->iocevent
, PSOCK
| PCATCH
, "AT ioctl", 0);
504 gref
->sevents
&= ~POLLPRI
;
506 ATENABLE(s
, gref
->lock
);
507 #ifdef APPLETALK_DEBUG
508 kprintf("at_ioctl: EINTR\n");
515 if (gbuf_next(m
) == m
) /* error case */
518 gref
->ichead
= gbuf_next(m
);
520 ATENABLE(s
, gref
->lock
);
522 #ifdef APPLETALK_DEBUG
523 kprintf("at_ioctl: woke up from ioc sleep gref = 0x%x\n",
527 /* process the ioc response */
528 ioc
= (ioc_t
*) gbuf_rptr(m
);
529 if ((err
= ioc
->ioc_error
) == 0) {
530 ioccmd
.ic_timout
= ioc
->ioc_rval
;
532 mdata
= gbuf_cont(m
);
533 if (mdata
&& ioccmd
.ic_dp
) {
534 ioccmd
.ic_len
= gbuf_msgsize(mdata
);
535 for (len
= 0; mdata
; mdata
= gbuf_cont(mdata
)) {
537 bcopy (gbuf_rptr(mdata
), &ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
539 if ((err
= copyout((caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&ioccmd
.ic_dp
[len
], gbuf_len(mdata
))) < 0) {
540 #ifdef APPLETALK_DEBUG
541 kprintf("at_ioctl: len=%d error copyout=%d from=%x to=%x gbuf_len=%x\n",
542 len
, err
, (caddr_t
)gbuf_rptr(mdata
), (caddr_t
)&ioccmd
.ic_dp
[len
], gbuf_len(mdata
));
547 len
+= gbuf_len(mdata
);
552 bcopy (&ioccmd
, arg
, sizeof(ioccmd_t
));
554 if ((err
= copyout((caddr_t
)&ioccmd
, (caddr_t
)arg
, sizeof(ioccmd_t
))) != 0) {
555 #ifdef APPLETALK_DEBUG
556 kprintf("at_ioctl: error copyout2=%d from=%x to=%x len=%d\n",
557 err
, &ioccmd
, arg
, sizeof(ioccmd_t
));
566 /*kprintf("at_ioctl: I_done=%d\n", err);*/
570 int _ATioctl(fp
, cmd
, arg
, proc
)
573 register caddr_t arg
;
579 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
580 if ((err
= atalk_getref(fp
, 0, &gref
, 0)) != 0) {
581 #ifdef APPLETALK_DEBUG
582 kprintf("_ATioctl: atalk_getref err = %d\n", err
);
586 err
= at_ioctl(gref
, cmd
, arg
, 0);
588 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
593 int _ATselect(fp
, which
, wql
, proc
)
602 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
603 err
= atalk_getref(fp
, 0, &gref
, 0);
604 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
609 ATDISABLE(s
, gref
->lock
);
610 if (which
== FREAD
) {
611 if (gref
->rdhead
|| (gref
->readable
&& (*gref
->readable
)(gref
)))
614 gref
->sevents
|= POLLIN
;
615 selrecord(proc
, &gref
->si
, wql
);
618 else if (which
== POLLOUT
) {
619 if (gref
->writeable
) {
620 if ((*gref
->writeable
)(gref
))
623 gref
->sevents
|= POLLOUT
;
624 selrecord(proc
, &gref
->si
, wql
);
629 ATENABLE(s
, gref
->lock
);
635 int _ATkqfilter(fp
, kn
, p
)
643 void atalk_putnext(gref
, m
)
649 ATDISABLE(s
, gref
->lock
);
651 /* *** potential leak? *** */
654 switch (gbuf_type(m
)) {
658 gbuf_next(gref
->ichead
) = m
;
661 if (gref
->sevents
& POLLPRI
) {
662 #ifdef APPLETALK_DEBUG
663 kprintf("wakeup gref = 0x%x\n", (unsigned)gref
);
665 wakeup(&gref
->iocevent
);
670 /* *** this processing was moved to atalk_notify *** */
671 panic("atalk_putnext receved MSG_ERROR");
678 gbuf_next(gref
->rdtail
) = m
;
682 if (gref
->sevents
& POLLMSG
) {
683 gref
->sevents
&= ~POLLMSG
;
684 wakeup(&gref
->event
);
686 if (gref
->sevents
& POLLIN
) {
687 gref
->sevents
&= ~POLLIN
;
688 selwakeup(&gref
->si
);
692 } /* switch gbuf_type(m) */
694 ATENABLE(s
, gref
->lock
);
695 } /* atalk_putnext */
697 void atalk_enablew(gref
)
700 if (gref
->sevents
& POLLSYNC
)
701 wakeup(&gref
->event
);
704 void atalk_flush(gref
)
709 ATDISABLE(s
, gref
->lock
);
711 gbuf_freel(gref
->rdhead
);
715 gbuf_freel(gref
->ichead
);
718 ATENABLE(s
, gref
->lock
);
722 * Notify an appletalk user of an asynchronous error;
723 * just wake up so that he can collect error status.
725 void atalk_notify(gref
, errno
)
726 register gref_t
*gref
;
730 ATDISABLE(s
, gref
->lock
);
732 if (gref
->atpcb_socket
) {
734 This section is patterned after udp_notify() in
737 gref
->atpcb_socket
->so_error
= errno
;
738 sorwakeup(gref
->atpcb_socket
);
739 sowwakeup(gref
->atpcb_socket
);
741 /* for ATP, ASP, and ADSP */
742 if (gref
->errno
== 0) {
744 /* clear out data waiting to be read */
746 gbuf_freel(gref
->rdhead
);
750 if (gref
->sevents
& POLLMSG
) {
751 gref
->sevents
&= ~POLLMSG
;
752 wakeup(&gref
->event
);
755 if (gref
->sevents
& POLLIN
) {
756 gref
->sevents
&= ~POLLIN
;
757 selwakeup(&gref
->si
);
761 ATENABLE(s
, gref
->lock
);
764 void atalk_notify_sel(gref
)
769 ATDISABLE(s
, gref
->lock
);
770 if (gref
->sevents
& POLLIN
) {
771 gref
->sevents
&= ~POLLIN
;
772 selwakeup(&gref
->si
);
774 ATENABLE(s
, gref
->lock
);
777 int atalk_peek(gref
, event
)
779 unsigned char *event
;
783 ATDISABLE(s
, gref
->lock
);
785 *event
= *gbuf_rptr(gref
->rdhead
);
789 ATENABLE(s
, gref
->lock
);
794 static gbuf_t
*trace_msg
;
796 void atalk_settrace(str
, p1
, p2
, p3
, p4
, p5
)
803 sprintf(trace_buf
, str
, p1
, p2
, p3
, p4
, p5
);
804 len
= strlen(trace_buf
);
805 #ifdef APPLETALK_DEBUG
806 kprintf("atalk_settrace: gbufalloc size=%d\n", len
+1);
808 if ((m
= gbuf_alloc(len
+1, PRI_MED
)) == 0)
811 strcpy(gbuf_rptr(m
), trace_buf
);
813 for (nextm
=trace_msg
; gbuf_cont(nextm
); nextm
=gbuf_cont(nextm
)) ;
814 gbuf_cont(nextm
) = m
;
819 void atalk_gettrace(m
)
823 gbuf_cont(m
) = trace_msg
;
828 #define GREF_PER_BLK 32
829 static gref_t
*gref_free_list
= 0;
831 int gref_alloc(grefp
)
834 extern gbuf_t
*atp_resource_m
;
837 gref_t
*gref
, *gref_array
;
839 *grefp
= (gref_t
*)NULL
;
841 ATDISABLE(s
, refall_lock
);
842 if (gref_free_list
== 0) {
843 ATENABLE(s
, refall_lock
);
844 #ifdef APPLETALK_DEBUG
845 kprintf("gref_alloc: gbufalloc size=%d\n", GREF_PER_BLK
*sizeof(gref_t
));
847 if ((m
= gbuf_alloc(GREF_PER_BLK
*sizeof(gref_t
),PRI_HI
)) == 0)
849 bzero(gbuf_rptr(m
), GREF_PER_BLK
*sizeof(gref_t
));
850 gref_array
= (gref_t
*)gbuf_rptr(m
);
851 for (i
=0; i
< GREF_PER_BLK
-1; i
++)
852 gref_array
[i
].atpcb_next
= (gref_t
*)&gref_array
[i
+1];
853 ATDISABLE(s
, refall_lock
);
854 gbuf_cont(m
) = atp_resource_m
;
856 gref_array
[i
].atpcb_next
= gref_free_list
;
857 gref_free_list
= (gref_t
*)&gref_array
[0];
860 gref
= gref_free_list
;
861 gref_free_list
= gref
->atpcb_next
;
862 ATENABLE(s
, refall_lock
);
863 ATLOCKINIT(gref
->lock
);
864 //### LD Test 08/05/98
865 // simple_lock_init(&gref->lock);
866 ATEVENTINIT(gref
->event
);
867 ATEVENTINIT(gref
->iocevent
);
869 /* *** just for now *** */
870 gref
->atpcb_socket
= (struct socket
*)NULL
;
876 /* bms: make gref_close callable from kernel */
877 int gref_close(gref_t
*gref
)
881 switch (gref
->proto
) {
883 /* ATPROTO_DDP and ATPROTO_LAP have been replaced with
884 BSD-style socket interface. */
887 rc
= atp_close(gref
, 1); break;
889 rc
= asp_close(gref
); break;
892 rc
= aurp_close(gref
); break;
896 rc
= adsp_close(gref
); break;
904 selthreadclear(&gref
->si
);
906 /* from original gref_free() */
907 ATDISABLE(s
, refall_lock
);
908 bzero((char *)gref
, sizeof(gref_t
));
909 gref
->atpcb_next
= gref_free_list
;
910 gref_free_list
= gref
;
911 ATENABLE(s
, refall_lock
);
920 *** Some to be replaced with mbuf routines, some to be re-written
921 as mbuf routines (and moved to kern/uicp_mbuf.c or sys/mbuf.h?).
927 * LD 5/12/97 Added for MacOSX, defines a m_clattach function that:
928 * "Allocates an mbuf structure and attaches an external cluster."
931 struct mbuf
*m_clattach(extbuf
, extfree
, extsize
, extarg
, wait
)
933 void (*extfree
)(caddr_t
, u_int
, caddr_t
);
940 if ((m
= m_gethdr(wait
, MSG_DATA
)) == NULL
)
943 m
->m_ext
.ext_buf
= extbuf
;
944 m
->m_ext
.ext_free
= extfree
;
945 m
->m_ext
.ext_size
= extsize
;
946 m
->m_ext
.ext_arg
= extarg
;
947 m
->m_ext
.ext_refs
.forward
=
948 m
->m_ext
.ext_refs
.backward
= &m
->m_ext
.ext_refs
;
958 temp fix for bug 2731148 - until this code is re-written to use standard clusters
959 Deletes any free clusters on the free list.
961 void atp_delete_free_clusters()
964 caddr_t cluster_list
;
967 /* check for free clusters on the free_cluster_list to be deleted */
968 MBUF_LOCK(); /* lock used by mbuf routines */
970 untimeout(&atp_delete_free_clusters
, NULL
);
971 atp_free_cluster_timeout_set
= 0;
973 cluster_list
= atp_free_cluster_list
;
974 atp_free_cluster_list
= 0;
978 while (cluster
= cluster_list
)
980 cluster_list
= *((caddr_t
*)cluster
);
981 FREE(cluster
, M_MCLUST
);
988 Used as the "free" routine for over-size clusters allocated using
989 m_lgbuf_alloc(). Called by m_free while under MBUF_LOCK.
992 void m_lgbuf_free(buf
, size
, arg
)
995 caddr_t arg
; /* not needed, but they're in m_free() */
997 /* FREE(buf, M_MCLUST); - can't free here - called from m_free while under lock */
999 /* move to free_cluster_list to be deleted later */
1000 caddr_t cluster
= (caddr_t
)buf
;
1002 /* don't need a lock because this is only called called from m_free which */
1003 /* is under MBUF_LOCK */
1004 *((caddr_t
*)cluster
) = atp_free_cluster_list
;
1005 atp_free_cluster_list
= cluster
;
1007 if (atp_free_cluster_timeout_set
== 0)
1009 atp_free_cluster_timeout_set
= 1;
1010 timeout(&atp_delete_free_clusters
, NULL
, (1 * HZ
));
1015 Used to allocate an mbuf when there is the possibility that it may
1016 need to be larger than the size of a standard cluster.
1019 struct mbuf
*m_lgbuf_alloc(size
, wait
)
1024 if (atp_free_cluster_list
)
1025 atp_delete_free_clusters(); /* delete any free clusters on the free list */
1027 /* If size is too large, allocate a cluster, otherwise, use the
1028 standard mbuf allocation routines.*/
1029 if (size
> MCLBYTES
) {
1032 (buf
= (void *)_MALLOC(size
, M_MCLUST
,
1033 (wait
)? M_WAITOK
: M_NOWAIT
))) {
1037 (m
= m_clattach(buf
, m_lgbuf_free
, size
, 0,
1038 (wait
)? M_WAIT
: M_DONTWAIT
))) {
1039 m_lgbuf_free(buf
, 0, 0);
1043 m
= m_gethdr(((wait
)? M_WAIT
: M_DONTWAIT
), MSG_DATA
);
1044 if (m
&& (size
> MHLEN
)) {
1045 MCLGET(m
, ((wait
)? M_WAIT
: M_DONTWAIT
));
1046 if (!(m
->m_flags
& M_EXT
)) {
1054 } /* m_lgbuf_alloc */
1057 gbuf_alloc() is a wrapper for m_lgbuf_alloc(), which is used to
1058 allocate an mbuf when there is the possibility that it may need
1059 to be larger than the size of a standard cluster.
1061 gbuf_alloc() sets the mbuf lengths, unlike the standard mbuf routines.
1064 gbuf_t
*gbuf_alloc_wait(size
, wait
)
1067 gbuf_t
*m
= (gbuf_t
*)m_lgbuf_alloc(size
, wait
);
1069 /* Standard mbuf allocation routines assume that the caller
1070 will set the size. */
1072 (struct mbuf
*)m
->m_pkthdr
.len
= size
;
1073 (struct mbuf
*)m
->m_len
= size
;
1084 for (size
=0; m
; m
=gbuf_cont(m
))
1085 size
+= gbuf_len(m
);
1089 int append_copy(m1
, m2
, wait
)
1090 struct mbuf
*m1
, *m2
;
1093 if ((!(m1
->m_flags
& M_EXT
)) && (!(m2
->m_flags
& M_EXT
)) &&
1094 (m_trailingspace(m1
) >= m2
->m_len
)) {
1095 /* splat the data from one into the other */
1096 bcopy(mtod(m2
, caddr_t
), mtod(m1
, caddr_t
) + m1
->m_len
,
1098 m1
->m_len
+= m2
->m_len
;
1099 if (m1
->m_flags
& M_PKTHDR
)
1100 m1
->m_pkthdr
.len
+= m2
->m_len
;
1103 if ((m1
->m_next
= m_copym(m2
, 0, m2
->m_len
,
1104 (wait
)? M_WAIT
: M_DONTWAIT
)) == NULL
)
1110 Copy an mbuf chain, referencing existing external storage, if any.
1111 Leave space for a header in the new chain, if the space has been
1112 left in the origin chain.
1114 struct mbuf
*copy_pkt(mlist
, pad
)
1115 struct mbuf
*mlist
; /* the mbuf chain to be copied */
1116 int pad
; /* hint as to how long the header might be
1117 If pad is < 0, leave the same amount of space
1118 as there was in the original. */
1124 len
= m_leadingspace(mlist
);
1126 len
= min(pad
, m_leadingspace(mlist
));
1128 /* preserve space for the header at the beginning of the mbuf */
1130 mlist
->m_data
-= (len
);
1131 mlist
->m_len
+= (len
);
1132 if (mlist
->m_flags
& M_PKTHDR
)
1133 mlist
->m_pkthdr
.len
+= (len
);
1134 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1138 new_m
= m_copym(mlist
, 0, M_COPYALL
, M_DONTWAIT
);
1143 void gbuf_linkb(m1
, m2
)
1147 while (gbuf_cont(m1
) != 0)
1152 void gbuf_linkpkt(m1
, m2
)
1156 while (gbuf_next(m1
) != 0)
1166 while ((tmp_m
= m
) != 0) {
1168 gbuf_next(tmp_m
) = 0;
1174 /* free empty mbufs at the front of the chain */
1175 gbuf_t
*gbuf_strip(m
)
1180 while (m
&& gbuf_len(m
) == 0) {
1188 /**************************************/
1190 int ddp_adjmsg(m
, len
)
1195 gbuf_t
*curr_m
, *prev_m
;
1197 if (m
== (gbuf_t
*)0)
1201 for (curr_m
=m
; curr_m
;) {
1202 buf_len
= gbuf_len(curr_m
);
1203 if (len
< buf_len
) {
1204 gbuf_rinc(curr_m
,len
);
1208 gbuf_rinc(curr_m
,buf_len
);
1209 if ((curr_m
= gbuf_cont(curr_m
)) == 0) {
1215 } else if (len
< 0) {
1218 for (curr_m
=m
; gbuf_cont(curr_m
);
1219 prev_m
=curr_m
, curr_m
=gbuf_cont(curr_m
)) ;
1220 buf_len
= gbuf_len(curr_m
);
1221 if (len
< buf_len
) {
1222 gbuf_wdec(curr_m
,len
);
1227 gbuf_cont(prev_m
) = 0;
1237 * The message chain, m is grown in size by len contiguous bytes.
1238 * If len is non-negative, len bytes are added to the
1239 * end of the gbuf_t chain. If len is negative, the
1240 * bytes are added to the front. ddp_growmsg only adds bytes to
1241 * message blocks of the same type.
1242 * It returns a pointer to the new gbuf_t on sucess, 0 on failure.
1245 gbuf_t
*ddp_growmsg(mp
, len
)
1251 if ((m
= mp
) == (gbuf_t
*) 0)
1252 return ((gbuf_t
*) 0);
1256 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1257 return ((gbuf_t
*) 0);
1258 gbuf_set_type(d
, gbuf_type(m
));
1260 /* link in new gbuf_t */
1269 if ((count
= gbuf_msgsize(m
)) < 0)
1270 return ((gbuf_t
*) 0);
1271 /* find end of chain */
1272 for ( ; m
; m
= gbuf_cont(m
)) {
1273 if (gbuf_len(m
) >= count
)
1275 count
-= gbuf_len(m
);
1277 /* m now points to gbuf_t to add to */
1278 if ((d
= gbuf_alloc(len
, PRI_MED
)) == 0)
1279 return ((gbuf_t
*) 0);
1280 gbuf_set_type(d
, gbuf_type(m
));
1281 /* link in new gbuf_t */
1282 gbuf_cont(d
) = gbuf_cont(m
);
1290 * return the MSG_IOCACK/MSG_IOCNAK. Note that the same message
1291 * block is used as the vehicle, and that if there is an error return,
1292 * then linked blocks are lopped off. BEWARE of multiple references.
1293 * Used by other appletalk modules, so it is not static!
1296 void ioc_ack(errno
, m
, gref
)
1299 register gref_t
*gref
;
1301 ioc_t
*iocbp
= (ioc_t
*)gbuf_rptr(m
);
1303 /*kprintf("ioc_ack: m=%x gref=%x errno=%d\n", m, gref, errno);*/
1304 if ((iocbp
->ioc_error
= errno
) != 0)
1305 { /* errno != 0, then there is an error, get rid of linked blocks! */
1308 gbuf_freem(gbuf_cont(m
));
1311 gbuf_set_type(m
, MSG_IOCNAK
);
1312 iocbp
->ioc_count
= 0; /* only make zero length if error */
1313 iocbp
->ioc_rval
= -1;
1315 gbuf_set_type(m
, MSG_IOCACK
);
1317 atalk_putnext(gref
, m
);