]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_mbuf2.c
2 * Copyright (c) 2000-2017 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 #include <sys/mcache.h>
109 #include <netinet/in.h>
110 #include <netinet/ip_var.h>
112 #include <netinet/ip6.h>
113 #include <netinet6/ip6_var.h>
117 #include <security/mac_framework.h>
121 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
122 * packet chain before "off" is kept untouched.
123 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
124 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
126 * on error return (NULL return value), original "m" will be freed.
128 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
131 m_pulldown(struct mbuf
*m
, int off
, int len
, int *offp
)
134 int hlen
, tlen
, olen
;
136 #if defined(PULLDOWN_STAT) && INET6
137 static struct mbuf
*prev
= NULL
;
138 int prevlen
= 0, prevmlen
= 0;
141 /* check invalid arguments. */
143 panic("m == NULL in m_pulldown()");
144 if (len
> MCLBYTES
) {
146 return NULL
; /* impossible */
149 #if defined(PULLDOWN_STAT) && INET6
150 ip6stat
.ip6s_pulldown
++;
153 #if defined(PULLDOWN_STAT) && INET6
154 /* statistics for m_pullup */
155 ip6stat
.ip6s_pullup
++;
156 if (off
+ len
> MHLEN
)
157 ip6stat
.ip6s_pullup_fail
++;
161 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
162 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
164 if (dlen
>= off
+ len
)
165 ip6stat
.ip6s_pullup
--; /* call will not be made! */
166 else if ((m
->m_flags
& M_EXT
) != 0) {
167 ip6stat
.ip6s_pullup_alloc
++;
168 ip6stat
.ip6s_pullup_copy
++;
170 if (mlen
>= off
+ len
)
171 ip6stat
.ip6s_pullup_copy
++;
173 ip6stat
.ip6s_pullup_alloc
++;
174 ip6stat
.ip6s_pullup_copy
++;
182 /* statistics for m_pullup2 */
183 ip6stat
.ip6s_pullup2
++;
184 if (off
+ len
> MCLBYTES
)
185 ip6stat
.ip6s_pullup2_fail
++;
189 dlen
= (prev
== m
) ? prevlen
: m
->m_len
;
190 mlen
= (prev
== m
) ? prevmlen
: m
->m_len
+ M_TRAILINGSPACE(m
);
194 if (dlen
>= off
+ len
)
195 ip6stat
.ip6s_pullup2
--; /* call will not be made! */
196 else if ((m
->m_flags
& M_EXT
) != 0) {
197 ip6stat
.ip6s_pullup2_alloc
++;
198 ip6stat
.ip6s_pullup2_copy
++;
199 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
: MHLEN
;
201 if (mlen
>= off
+ len
)
202 ip6stat
.ip6s_pullup2_copy
++;
204 ip6stat
.ip6s_pullup2_alloc
++;
205 ip6stat
.ip6s_pullup2_copy
++;
206 prevmlen
= (off
+ len
> MHLEN
) ? MCLBYTES
215 #ifdef PULLDOWN_DEBUG
219 for (t
= m
; t
; t
= t
->m_next
)
220 printf(" %d", t
->m_len
);
225 while (n
!= NULL
&& off
> 0) {
231 /* be sure to point non-empty mbuf */
232 while (n
!= NULL
&& n
->m_len
== 0)
236 return NULL
; /* mbuf chain too short */
240 * the target data is on <n, off>.
241 * if we got enough data on the mbuf "n", we're done.
243 if ((off
== 0 || offp
) && len
<= n
->m_len
- off
)
246 #if defined(PULLDOWN_STAT) && INET6
247 ip6stat
.ip6s_pulldown_copy
++;
251 * when len < n->m_len - off and off != 0, it is a special case.
252 * len bytes from <n, off> sits in single mbuf, but the caller does
253 * not like the starting position (off).
254 * chop the current mbuf into two pieces, set off to 0.
256 if (len
< n
->m_len
- off
) {
257 o
= m_copym(n
, off
, n
->m_len
- off
, M_DONTWAIT
);
260 return NULL
; /* ENOBUFS */
263 o
->m_next
= n
->m_next
;
271 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
272 * and construct contiguous mbuf with m_len == len.
273 * note that hlen + tlen == len, and tlen > 0.
275 hlen
= n
->m_len
- off
;
279 * ensure that we have enough trailing data on mbuf chain.
280 * if not, we can do nothing about the chain.
283 for (o
= n
->m_next
; o
!= NULL
; o
= o
->m_next
)
285 if (hlen
+ olen
< len
) {
287 return NULL
; /* mbuf chain too short */
292 * we need to use m_copydata() to get data from <n->m_next, 0>.
294 if ((n
->m_flags
& M_EXT
) == 0)
297 if (m_get_ext_free(n
) != NULL
)
299 else if (m_mclhasreference(n
))
304 if ((off
== 0 || offp
) && M_TRAILINGSPACE(n
) >= tlen
306 m_copydata(n
->m_next
, 0, tlen
, mtod(n
, caddr_t
) + n
->m_len
);
308 m_adj(n
->m_next
, tlen
);
311 if ((off
== 0 || offp
) && M_LEADINGSPACE(n
->m_next
) >= hlen
313 n
->m_next
->m_data
-= hlen
;
314 n
->m_next
->m_len
+= hlen
;
315 bcopy(mtod(n
, caddr_t
) + off
, mtod(n
->m_next
, caddr_t
), hlen
);
323 * now, we need to do the hard way. don't m_copy as there's no room
326 #if defined(PULLDOWN_STAT) && INET6
327 ip6stat
.ip6s_pulldown_alloc
++;
329 MGET(o
, M_DONTWAIT
, m
->m_type
);
332 return NULL
; /* ENOBUFS */
334 if (len
> MHLEN
) { /* use MHLEN just for safety */
335 MCLGET(o
, M_DONTWAIT
);
336 if ((o
->m_flags
& M_EXT
) == 0) {
339 return NULL
; /* ENOBUFS */
342 /* get hlen from <n, off> into <o, 0> */
344 bcopy(mtod(n
, caddr_t
) + off
, mtod(o
, caddr_t
), hlen
);
346 /* get tlen from <n->m_next, 0> into <o, hlen> */
347 m_copydata(n
->m_next
, 0, tlen
, mtod(o
, caddr_t
) + o
->m_len
);
349 m_adj(n
->m_next
, tlen
);
350 o
->m_next
= n
->m_next
;
356 #ifdef PULLDOWN_DEBUG
360 for (t
= m
; t
; t
= t
->m_next
)
361 printf("%c%d", t
== n
? '*' : ' ', t
->m_len
);
362 printf(" (off=%d)\n", off
);
371 * Create and return an m_tag, either by re-using space in a previous tag
372 * or by allocating a new mbuf/cluster
375 m_tag_create(u_int32_t id
, u_int16_t type
, int len
, int wait
, struct mbuf
*buf
)
377 struct m_tag
*t
= NULL
;
383 if (len
+ sizeof (struct m_tag
) + sizeof (struct m_taghdr
) > MLEN
)
384 return (m_tag_alloc(id
, type
, len
, wait
));
387 * We've exhausted all external cases. Now, go through the m_tag
388 * chain and see if we can fit it in any of them.
389 * If not (t == NULL), call m_tag_alloc to store it in a new mbuf.
391 p
= SLIST_FIRST(&buf
->m_pkthdr
.tags
);
394 if (M_TAG_ALIGN(p
->m_tag_len
) +
395 sizeof (struct m_taghdr
) > MLEN
) {
396 p
= SLIST_NEXT(p
, m_tag_link
);
400 VERIFY(p
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
402 struct mbuf
*m
= m_dtom(p
);
403 struct m_taghdr
*hdr
= (struct m_taghdr
*)(void *)m
->m_data
;
405 VERIFY(IS_P2ALIGNED(hdr
+ 1, sizeof (u_int64_t
)));
406 VERIFY(m
->m_flags
& M_TAGHDR
&& !(m
->m_flags
& M_EXT
));
408 /* The mbuf can store this m_tag */
409 if (M_TAG_ALIGN(len
) <= MLEN
- m
->m_len
) {
410 t
= (struct m_tag
*)(void *)(m
->m_data
+ m
->m_len
);
411 VERIFY(IS_P2ALIGNED(t
, sizeof (u_int64_t
)));
413 m
->m_len
+= M_TAG_ALIGN(len
);
414 VERIFY(m
->m_len
<= MLEN
);
418 p
= SLIST_NEXT(p
, m_tag_link
);
422 return (m_tag_alloc(id
, type
, len
, wait
));
424 t
->m_tag_cookie
= M_TAG_VALID_PATTERN
;
425 t
->m_tag_type
= type
;
433 /* Get a packet tag structure along with specified data following. */
435 m_tag_alloc(u_int32_t id
, u_int16_t type
, int len
, int wait
)
442 if (M_TAG_ALIGN(len
) + sizeof (struct m_taghdr
) <= MLEN
) {
443 struct mbuf
*m
= m_get(wait
, MT_TAG
);
444 struct m_taghdr
*hdr
;
449 m
->m_flags
|= M_TAGHDR
;
451 hdr
= (struct m_taghdr
*)(void *)m
->m_data
;
452 VERIFY(IS_P2ALIGNED(hdr
+ 1, sizeof (u_int64_t
)));
454 m
->m_len
+= sizeof (struct m_taghdr
);
455 t
= (struct m_tag
*)(void *)(m
->m_data
+ m
->m_len
);
456 VERIFY(IS_P2ALIGNED(t
, sizeof (u_int64_t
)));
457 m
->m_len
+= M_TAG_ALIGN(len
);
458 VERIFY(m
->m_len
<= MLEN
);
459 } else if (len
+ sizeof (struct m_tag
) <= MCLBYTES
) {
460 t
= (struct m_tag
*)(void *)m_mclalloc(wait
);
468 VERIFY(IS_P2ALIGNED(t
, sizeof (u_int64_t
)));
469 t
->m_tag_cookie
= M_TAG_VALID_PATTERN
;
470 t
->m_tag_type
= type
;
479 /* Free a packet tag. */
481 m_tag_free(struct m_tag
*t
)
485 t
->m_tag_id
== KERNEL_MODULE_TAG_ID
&&
486 t
->m_tag_type
== KERNEL_TAG_TYPE_MACLABEL
)
487 mac_mbuf_tag_destroy(t
);
492 VERIFY(t
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
494 if (M_TAG_ALIGN(t
->m_tag_len
) + sizeof (struct m_taghdr
) <= MLEN
) {
495 struct mbuf
* m
= m_dtom(t
);
496 VERIFY(m
->m_flags
& M_TAGHDR
);
497 struct m_taghdr
*hdr
= (struct m_taghdr
*)(void *)m
->m_data
;
499 VERIFY(IS_P2ALIGNED(hdr
+ 1, sizeof (u_int64_t
)));
501 /* No other tags in this mbuf */
502 if(--hdr
->refcnt
== 0) {
507 /* Pattern-fill the header */
508 u_int64_t
*fill_ptr
= (u_int64_t
*)t
;
509 u_int64_t
*end_ptr
= (u_int64_t
*)(t
+ 1);
510 while (fill_ptr
< end_ptr
) {
511 *fill_ptr
= M_TAG_FREE_PATTERN
;
515 m_mclfree((caddr_t
)t
);
519 /* Prepend a packet tag. */
521 m_tag_prepend(struct mbuf
*m
, struct m_tag
*t
)
523 VERIFY(m
!= NULL
&& t
!= NULL
);
525 SLIST_INSERT_HEAD(&m
->m_pkthdr
.tags
, t
, m_tag_link
);
528 /* Unlink a packet tag. */
530 m_tag_unlink(struct mbuf
*m
, struct m_tag
*t
)
532 VERIFY(m
->m_flags
& M_PKTHDR
);
533 VERIFY(t
!= NULL
&& t
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
535 SLIST_REMOVE(&m
->m_pkthdr
.tags
, t
, m_tag
, m_tag_link
);
538 /* Unlink and free a packet tag. */
540 m_tag_delete(struct mbuf
*m
, struct m_tag
*t
)
546 /* Unlink and free a packet tag chain, starting from given tag. */
548 m_tag_delete_chain(struct mbuf
*m
, struct m_tag
*t
)
552 VERIFY(m
->m_flags
& M_PKTHDR
);
557 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
562 VERIFY(p
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
563 while ((q
= SLIST_NEXT(p
, m_tag_link
)) != NULL
) {
564 VERIFY(q
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
570 /* Find a tag, starting from a given position. */
572 m_tag_locate(struct mbuf
*m
, u_int32_t id
, u_int16_t type
, struct m_tag
*t
)
576 VERIFY(m
->m_flags
& M_PKTHDR
);
579 p
= SLIST_FIRST(&m
->m_pkthdr
.tags
);
581 VERIFY(t
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
582 p
= SLIST_NEXT(t
, m_tag_link
);
585 VERIFY(p
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
586 if (p
->m_tag_id
== id
&& p
->m_tag_type
== type
)
588 p
= SLIST_NEXT(p
, m_tag_link
);
593 /* Copy a single tag. */
595 m_tag_copy(struct m_tag
*t
, int how
)
601 p
= m_tag_alloc(t
->m_tag_id
, t
->m_tag_type
, t
->m_tag_len
, how
);
606 * XXXMAC: we should probably pass off the initialization, and
607 * copying here? can we hid that KERNEL_TAG_TYPE_MACLABEL is
608 * special from the mbuf code?
611 t
->m_tag_id
== KERNEL_MODULE_TAG_ID
&&
612 t
->m_tag_type
== KERNEL_TAG_TYPE_MACLABEL
) {
613 if (mac_mbuf_tag_init(p
, how
) != 0) {
617 mac_mbuf_tag_copy(t
, p
);
620 bcopy(t
+ 1, p
+ 1, t
->m_tag_len
); /* Copy the data */
625 * Copy two tag chains. The destination mbuf (to) loses any attached
626 * tags even if the operation fails. This should not be a problem, as
627 * m_tag_copy_chain() is typically called with a newly-allocated
631 m_tag_copy_chain(struct mbuf
*to
, struct mbuf
*from
, int how
)
633 struct m_tag
*p
, *t
, *tprev
= NULL
;
635 VERIFY((to
->m_flags
& M_PKTHDR
) && (from
->m_flags
& M_PKTHDR
));
637 m_tag_delete_chain(to
, NULL
);
638 SLIST_FOREACH(p
, &from
->m_pkthdr
.tags
, m_tag_link
) {
639 VERIFY(p
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
640 t
= m_tag_copy(p
, how
);
642 m_tag_delete_chain(to
, NULL
);
646 SLIST_INSERT_HEAD(&to
->m_pkthdr
.tags
, t
, m_tag_link
);
648 SLIST_INSERT_AFTER(tprev
, t
, m_tag_link
);
655 /* Initialize dynamic and static tags on an mbuf. */
657 m_tag_init(struct mbuf
*m
, int all
)
659 VERIFY(m
->m_flags
& M_PKTHDR
);
661 SLIST_INIT(&m
->m_pkthdr
.tags
);
663 * If the caller wants to preserve static mbuf tags
664 * (e.g. m_dup_pkthdr), don't zero them out.
667 bzero(&m
->m_pkthdr
.builtin_mtag
._net_mtag
,
668 sizeof (m
->m_pkthdr
.builtin_mtag
._net_mtag
));
672 /* Get first tag in chain. */
674 m_tag_first(struct mbuf
*m
)
676 VERIFY(m
->m_flags
& M_PKTHDR
);
678 return (SLIST_FIRST(&m
->m_pkthdr
.tags
));
681 /* Get next tag in chain. */
683 m_tag_next(struct mbuf
*m
, struct m_tag
*t
)
687 VERIFY(t
->m_tag_cookie
== M_TAG_VALID_PATTERN
);
689 return (SLIST_NEXT(t
, m_tag_link
));
693 m_set_traffic_class(struct mbuf
*m
, mbuf_traffic_class_t tc
)
695 u_int32_t val
= MBUF_TC2SCVAL(tc
); /* just the val portion */
697 return (m_set_service_class(m
, m_service_class_from_val(val
)));
701 m_get_traffic_class(struct mbuf
*m
)
703 return (MBUF_SC2TC(m_get_service_class(m
)));
707 m_set_service_class(struct mbuf
*m
, mbuf_svc_class_t sc
)
711 VERIFY(m
->m_flags
& M_PKTHDR
);
713 if (MBUF_VALID_SC(sc
))
714 m
->m_pkthdr
.pkt_svc
= sc
;
722 m_get_service_class(struct mbuf
*m
)
726 VERIFY(m
->m_flags
& M_PKTHDR
);
728 if (MBUF_VALID_SC(m
->m_pkthdr
.pkt_svc
))
729 sc
= m
->m_pkthdr
.pkt_svc
;
737 m_service_class_from_idx(u_int32_t i
)
739 mbuf_svc_class_t sc
= MBUF_SC_BE
;
743 return (MBUF_SC_BK_SYS
);
755 return (MBUF_SC_OAM
);
770 return (MBUF_SC_CTL
);
782 m_service_class_from_val(u_int32_t v
)
784 mbuf_svc_class_t sc
= MBUF_SC_BE
;
788 return (MBUF_SC_BK_SYS
);
800 return (MBUF_SC_OAM
);
815 return (MBUF_SC_CTL
);
827 m_adj_sum16(struct mbuf
*m
, uint32_t start
, uint32_t dataoff
,
828 uint32_t datalen
, uint32_t sum
)
830 uint32_t total_sub
= 0; /* total to subtract */
831 uint32_t mlen
= m_pktlen(m
); /* frame length */
832 uint32_t bytes
= (dataoff
+ datalen
); /* bytes covered by sum */
835 ASSERT(bytes
<= mlen
);
838 * Take care of excluding (len > 0) or including (len < 0)
839 * extraneous octets at the beginning of the packet, taking
840 * into account the start offset.
842 len
= (dataoff
- start
);
844 total_sub
= m_sum16(m
, start
, len
);
846 sum
+= m_sum16(m
, dataoff
, -len
);
849 * Take care of excluding any postpended extraneous octets.
851 len
= (mlen
- bytes
);
854 uint32_t extra
= m_sum16(m
, bytes
, len
);
855 uint32_t off
= bytes
, off0
= off
;
858 if (__improbable(m
== NULL
)) {
859 panic("%s: invalid mbuf chain %p [off %u, "
860 "len %u]", __func__
, m0
, off0
, len
);
869 /* if we started on odd-alignment, swap the value */
870 if ((uintptr_t)(mtod(m
, uint8_t *) + off
) & 1)
871 total_sub
+= ((extra
<< 8) & 0xffff) | (extra
>> 8);
875 total_sub
= (total_sub
>> 16) + (total_sub
& 0xffff);
879 * 1's complement subtract any extraneous octets.
881 if (total_sub
!= 0) {
882 if (total_sub
>= sum
)
883 sum
= ~(total_sub
- sum
) & 0xffff;
888 /* fold 32-bit to 16-bit */
889 sum
= (sum
>> 16) + (sum
& 0xffff); /* 17-bit */
890 sum
= (sum
>> 16) + (sum
& 0xffff); /* 16-bit + carry */
891 sum
= (sum
>> 16) + (sum
& 0xffff); /* final carry */
893 return (sum
& 0xffff);
897 m_sum16(struct mbuf
*m
, uint32_t off
, uint32_t len
)
904 * Use m_length2() instead of m_length(), as we cannot rely on
905 * the caller setting m_pkthdr.len correctly, if the mbuf is
908 if ((mlen
= m_length2(m
, NULL
)) < (off
+ len
)) {
909 panic("%s: mbuf %p len (%d) < off+len (%d+%d)\n", __func__
,
914 return (os_cpu_in_cksum_mbuf(m
, len
, off
, 0));