]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_mbuf2.c
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $ */
31 * Copyright (C) 1999 WIDE Project.
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. Neither the name of the project nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * Copyright (c) 1982, 1986, 1988, 1991, 1993
61 * The Regents of the University of California. All rights reserved.
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
66 * 1. Redistributions of source code must retain the above copyright
67 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in the
70 * documentation and/or other materials provided with the distribution.
71 * 3. All advertising materials mentioning features or use of this software
72 * must display the following acknowledgement:
73 * This product includes software developed by the University of
74 * California, Berkeley and its contributors.
75 * 4. Neither the name of the University nor the names of its contributors
76 * may be used to endorse or promote products derived from this software
77 * without specific prior written permission.
79 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
91 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
94 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
95 * support for mandatory and extensible security protections. This notice
96 * is included in support of clause 2.2 (b) of the Apple Public License,
101 /*#define PULLDOWN_DEBUG*/
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/proc_internal.h>
106 #include <sys/malloc.h>
107 #include <sys/mbuf.h>
108 #if defined(PULLDOWN_STAT) && defined(INET6)
109 #include <netinet/in.h>
110 #include <netinet/ip6.h>
111 #include <netinet6/ip6_var.h>
115 #include <security/mac_framework.h>
119 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
120 * packet chain before "off" is kept untouched.
121 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
122 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
124 * on error return (NULL return value), original "m" will be freed.
126 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
129 m_pulldown(struct mbuf
*m
, int off
, int len
, int *offp
)
132 int hlen
, tlen
, olen
;
134 #if defined(PULLDOWN_STAT) && defined(INET6)
135 static struct mbuf
*prev
= NULL
;
136 int prevlen
= 0, prevmlen
= 0;
139 /* check invalid arguments. */
141 panic("m == NULL in m_pulldown()");
142 if (len
> MCLBYTES
) {
144 return NULL
; /* impossible */
147 #if defined(PULLDOWN_STAT) && defined(INET6)
148 ip6stat
.ip6s_pulldown
++;
151 #if defined(PULLDOWN_STAT) && defined(INET6)
152 /* statistics for m_pullup */
153 ip6stat
.ip6s_pullup
++;
154 if (off
+ len
> MHLEN
)
155 ip6stat
.ip6s_pullup_fail
++;
159 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
160 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
162 if (dlen
>= off
+ len
)
163 ip6stat
.ip6s_pullup
--; /* call will not be made! */
164 else if ((m
->m_flags
& M_EXT
) != 0) {
165 ip6stat
.ip6s_pullup_alloc
++;
166 ip6stat
.ip6s_pullup_copy
++;
168 if (mlen
>= off
+ len
)
169 ip6stat
.ip6s_pullup_copy
++;
171 ip6stat
.ip6s_pullup_alloc
++;
172 ip6stat
.ip6s_pullup_copy
++;
180 /* statistics for m_pullup2 */
181 ip6stat
.ip6s_pullup2
++;
182 if (off
+ len
> MCLBYTES
)
183 ip6stat
.ip6s_pullup2_fail
++;
187 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
188 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
192 if (dlen
>= off
+ len
)
193 ip6stat
.ip6s_pullup2
--; /* call will not be made! */
194 else if ((m
->m_flags
& M_EXT
) != 0) {
195 ip6stat
.ip6s_pullup2_alloc
++;
196 ip6stat
.ip6s_pullup2_copy
++;
197 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
: MHLEN
;
199 if (mlen
>= off
+ len
)
200 ip6stat
.ip6s_pullup2_copy
++;
202 ip6stat
.ip6s_pullup2_alloc
++;
203 ip6stat
.ip6s_pullup2_copy
++;
204 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
213 #ifdef PULLDOWN_DEBUG
217 for (t
= m
; t
; t
= t
->m_next
)
218 printf(" %d", t
->m_len
);
223 while (n
!= NULL
&& off
> 0) {
229 /* be sure to point non-empty mbuf */
230 while (n
!= NULL
&& n
->m_len
== 0)
234 return NULL
; /* mbuf chain too short */
238 * the target data is on <n, off>.
239 * if we got enough data on the mbuf "n", we're done.
241 if ((off
== 0 || offp
) && len
<= n
->m_len
- off
)
244 #if defined(PULLDOWN_STAT) && defined(INET6)
245 ip6stat
.ip6s_pulldown_copy
++;
249 * when len < n->m_len - off and off != 0, it is a special case.
250 * len bytes from <n, off> sits in single mbuf, but the caller does
251 * not like the starting position (off).
252 * chop the current mbuf into two pieces, set off to 0.
254 if (len
< n
->m_len
- off
) {
255 o
= m_copym(n
, off
, n
->m_len
- off
, M_DONTWAIT
);
258 return NULL
; /* ENOBUFS */
261 o
->m_next
= n
->m_next
;
269 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
270 * and construct contiguous mbuf with m_len == len.
271 * note that hlen + tlen == len, and tlen > 0.
273 hlen
= n
->m_len
- off
;
277 * ensure that we have enough trailing data on mbuf chain.
278 * if not, we can do nothing about the chain.
281 for (o
= n
->m_next
; o
!= NULL
; o
= o
->m_next
)
283 if (hlen
+ olen
< len
) {
285 return NULL
; /* mbuf chain too short */
290 * we need to use m_copydata() to get data from <n->m_next, 0>.
292 if ((n
->m_flags
& M_EXT
) == 0)
295 if (n
->m_ext
.ext_free
)
297 else if (m_mclhasreference(n
))
302 if ((off
== 0 || offp
) && M_TRAILINGSPACE(n
) >= tlen
304 m_copydata(n
->m_next
, 0, tlen
, mtod(n
, caddr_t
) + n
->m_len
);
306 m_adj(n
->m_next
, tlen
);
309 if ((off
== 0 || offp
) && M_LEADINGSPACE(n
->m_next
) >= hlen
311 n
->m_next
->m_data
-= hlen
;
312 n
->m_next
->m_len
+= hlen
;
313 bcopy(mtod(n
, caddr_t
) + off
, mtod(n
->m_next
, caddr_t
), hlen
);
321 * now, we need to do the hard way. don't m_copy as there's no room
324 #if defined(PULLDOWN_STAT) && defined(INET6)
325 ip6stat
.ip6s_pulldown_alloc
++;
327 MGET(o
, M_DONTWAIT
, m
->m_type
);
330 return NULL
; /* ENOBUFS */
332 if (len
> MHLEN
) { /* use MHLEN just for safety */
333 MCLGET(o
, M_DONTWAIT
);
334 if ((o
->m_flags
& M_EXT
) == 0) {
337 return NULL
; /* ENOBUFS */
340 /* get hlen from <n, off> into <o, 0> */
342 bcopy(mtod(n
, caddr_t
) + off
, mtod(o
, caddr_t
), hlen
);
344 /* get tlen from <n->m_next, 0> into <o, hlen> */
345 m_copydata(n
->m_next
, 0, tlen
, mtod(o
, caddr_t
) + o
->m_len
);
347 m_adj(n
->m_next
, tlen
);
348 o
->m_next
= n
->m_next
;
354 #ifdef PULLDOWN_DEBUG
358 for (t
= m
; t
; t
= t
->m_next
)
359 printf("%c%d", t
== n
? '*' : ' ', t
->m_len
);
360 printf(" (off=%d)\n", off
);
368 /* Get a packet tag structure along with specified data following. */
370 m_tag_alloc(u_int32_t id
, u_int16_t type
, int len
, int wait
)
377 t
= malloc(len
+ sizeof(struct m_tag
), M_PACKET_TAGS
, wait
);
379 /*MALLOC(t, struct m_tag *, len + sizeof(struct m_tag), M_TEMP, M_WAITOK);*/
380 if (len
+ sizeof(struct m_tag
) <= MLEN
) {
381 struct mbuf
*m
= m_get(wait
, MT_TAG
);
384 t
= mtod(m
, struct m_tag
*);
385 } else if (len
+ sizeof(struct m_tag
) <= MCLBYTES
) {
386 t
= (struct m_tag
*) m_mclalloc(wait
);
392 t
->m_tag_type
= type
;
399 /* Free a packet tag. */
401 m_tag_free(struct m_tag
*t
)
405 t
->m_tag_id
== KERNEL_MODULE_TAG_ID
&&
406 t
->m_tag_type
== KERNEL_TAG_TYPE_MACLABEL
)
407 mac_mbuf_tag_destroy(t
);
410 free(t
, M_PACKET_TAGS
);
412 /* FREE(t, M_TEMP); */
415 if (t
->m_tag_len
+ sizeof(struct m_tag
) <= MLEN
) {
416 struct mbuf
* m
= m_dtom(t
);
424 /* Prepend a packet tag. */
426 m_tag_prepend(struct mbuf
*m
, struct m_tag
*t
)
428 KASSERT(m
&& t
, ("m_tag_prepend: null argument, m %p t %p", m
, t
));
429 SLIST_INSERT_HEAD(&m
->m_pkthdr
.tags
, t
, m_tag_link
);
432 /* Unlink a packet tag. */
434 m_tag_unlink(struct mbuf
*m
, struct m_tag
*t
)
436 KASSERT(m
&& t
, ("m_tag_unlink: null argument, m %p t %p", m
, t
));
437 SLIST_REMOVE(&m
->m_pkthdr
.tags
, t
, m_tag
, m_tag_link
);
440 /* Unlink and free a packet tag. */
442 m_tag_delete(struct mbuf
*m
, struct m_tag
*t
)
444 KASSERT(m
&& t
, ("m_tag_delete: null argument, m %p t %p", m
, t
));
449 /* Unlink and free a packet tag chain, starting from given tag. */
451 m_tag_delete_chain(struct mbuf
*m
, struct m_tag
*t
)
455 KASSERT(m
, ("m_tag_delete_chain: null mbuf"));
459 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
462 while ((q
= SLIST_NEXT(p
, m_tag_link
)) != NULL
)
467 /* Find a tag, starting from a given position. */
469 m_tag_locate(struct mbuf
*m
, u_int32_t id
, u_int16_t type
, struct m_tag
*t
)
473 KASSERT(m
, ("m_tag_find: null mbuf"));
475 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
477 p
= SLIST_NEXT(t
, m_tag_link
);
479 if (p
->m_tag_id
== id
&& p
->m_tag_type
== type
)
481 p
= SLIST_NEXT(p
, m_tag_link
);
486 /* Copy a single tag. */
488 m_tag_copy(struct m_tag
*t
, int how
)
492 KASSERT(t
, ("m_tag_copy: null tag"));
493 p
= m_tag_alloc(t
->m_tag_id
, t
->m_tag_type
, t
->m_tag_len
, how
);
498 * XXXMAC: we should probably pass off the initialization, and
499 * copying here? can we hid that KERNEL_TAG_TYPE_MACLABEL is
500 * special from the mbuf code?
503 t
->m_tag_id
== KERNEL_MODULE_TAG_ID
&&
504 t
->m_tag_type
== KERNEL_TAG_TYPE_MACLABEL
) {
505 if (mac_mbuf_tag_init(p
, how
) != 0) {
509 mac_mbuf_tag_copy(t
, p
);
512 bcopy(t
+ 1, p
+ 1, t
->m_tag_len
); /* Copy the data */
517 * Copy two tag chains. The destination mbuf (to) loses any attached
518 * tags even if the operation fails. This should not be a problem, as
519 * m_tag_copy_chain() is typically called with a newly-allocated
523 m_tag_copy_chain(struct mbuf
*to
, struct mbuf
*from
, int how
)
525 struct m_tag
*p
, *t
, *tprev
= NULL
;
528 ("m_tag_copy: null argument, to %p from %p", to
, from
));
529 m_tag_delete_chain(to
, NULL
);
530 SLIST_FOREACH(p
, &from
->m_pkthdr
.tags
, m_tag_link
) {
531 t
= m_tag_copy(p
, how
);
533 m_tag_delete_chain(to
, NULL
);
537 SLIST_INSERT_HEAD(&to
->m_pkthdr
.tags
, t
, m_tag_link
);
539 SLIST_INSERT_AFTER(tprev
, t
, m_tag_link
);
546 /* Initialize tags on an mbuf. */
548 m_tag_init(struct mbuf
*m
)
550 SLIST_INIT(&m
->m_pkthdr
.tags
);
553 /* Get first tag in chain. */
555 m_tag_first(struct mbuf
*m
)
557 return SLIST_FIRST(&m
->m_pkthdr
.tags
);
560 /* Get next tag in chain. */
562 m_tag_next(__unused
struct mbuf
*m
, struct m_tag
*t
)
564 return SLIST_NEXT(t
, m_tag_link
);