]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/uipc_mbuf2.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / bsd / kern / uipc_mbuf2.c
1 /*
2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $ */
29
30 /*
31 * Copyright (C) 1999 WIDE Project.
32 * All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
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.
45 *
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
56 * SUCH DAMAGE.
57 */
58
59 /*
60 * Copyright (c) 1982, 1986, 1988, 1991, 1993
61 * The Regents of the University of California. All rights reserved.
62 *
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
65 * are met:
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.
78 *
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
89 * SUCH DAMAGE.
90 *
91 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
92 */
93 /*
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,
97 * Version 2.0.
98 */
99
100
101 /*#define PULLDOWN_DEBUG*/
102
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>
111 #if INET6
112 #include <netinet/ip6.h>
113 #include <netinet6/ip6_var.h>
114 #endif /* INET6 */
115
116 #if CONFIG_MACF_NET
117 #include <security/mac_framework.h>
118 #endif
119
120 /*
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.
125 *
126 * on error return (NULL return value), original "m" will be freed.
127 *
128 * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
129 */
130 struct mbuf *
131 m_pulldown(struct mbuf *m, int off, int len, int *offp)
132 {
133 struct mbuf *n = NULL, *o = NULL;
134 int hlen = 0, tlen = 0, olen = 0;
135 int sharedcluster = 0;
136
137 /* check invalid arguments. */
138 VERIFY(len >= 0 && off >= 0);
139
140 if (m == NULL) {
141 panic("m == NULL in m_pulldown()");
142 }
143 if (len > MCLBYTES) {
144 m_freem(m);
145 return NULL; /* impossible */
146 }
147 int tmp_len = 0;
148 if (os_add_overflow(off, len, &tmp_len)) {
149 m_free(m);
150 return NULL;
151 }
152
153 #ifdef PULLDOWN_DEBUG
154 {
155 struct mbuf *t;
156 printf("before:");
157 for (t = m; t; t = t->m_next) {
158 printf(" %d", t->m_len);
159 }
160 printf("\n");
161 }
162 #endif
163 n = m;
164
165 /*
166 * Iterate and make n point to the mbuf
167 * within which the first byte at length
168 * offset is contained from the start of
169 * mbuf chain.
170 */
171 while (n != NULL && off > 0) {
172 if (n->m_len > off) {
173 break;
174 }
175 off -= n->m_len;
176 n = n->m_next;
177 }
178
179 /* be sure to point non-empty mbuf */
180 while (n != NULL && n->m_len == 0) {
181 n = n->m_next;
182 }
183
184 if (!n) {
185 m_freem(m);
186 return NULL; /* mbuf chain too short */
187 }
188
189 /*
190 * the target data is on <n, off>.
191 * if we got enough data on the mbuf "n", we're done.
192 *
193 * It should be noted, that we should only do this either
194 * when offset is 0, i.e. data is pointing to the start
195 * or when the caller specifies an out argument to get
196 * the offset value in the mbuf to work with data pointer
197 * correctly.
198 *
199 * If offset is not 0 and caller did not provide out-argument
200 * to get offset, we should split the mbuf even when the length
201 * is contained in current mbuf.
202 */
203 if ((off == 0 || offp) && len <= n->m_len - off) {
204 goto ok;
205 }
206
207 /*
208 * when len <= n->m_len - off and off != 0, it is a special case.
209 * len bytes from <n, off> sits in single mbuf, but the caller does
210 * not like the starting position (off).
211 * chop the current mbuf into two pieces, set off to 0.
212 */
213 if (len <= n->m_len - off) {
214 o = m_copym(n, off, n->m_len - off, M_DONTWAIT);
215 if (o == NULL) {
216 m_freem(m);
217 return NULL; /* ENOBUFS */
218 }
219 n->m_len = off;
220 o->m_next = n->m_next;
221 n->m_next = o;
222 n = n->m_next;
223 off = 0;
224 goto ok;
225 }
226
227 /*
228 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
229 * and construct contiguous mbuf with m_len == len.
230 * note that hlen + tlen == len, and tlen > 0.
231 *
232 * Read these variables as head length and tail length
233 */
234 hlen = n->m_len - off;
235 tlen = len - hlen;
236
237 /*
238 * ensure that we have enough trailing data on mbuf chain.
239 * if not, we can do nothing about the chain.
240 */
241 olen = 0;
242 for (o = n->m_next; o != NULL; o = o->m_next) {
243 olen += o->m_len;
244 }
245 if (hlen + olen < len) {
246 m_freem(m);
247 return NULL; /* mbuf chain too short */
248 }
249
250 /*
251 * easy cases first.
252 * we need to use m_copydata() to get data from <n->m_next, 0>.
253 */
254 if ((n->m_flags & M_EXT) == 0) {
255 sharedcluster = 0;
256 } else {
257 if (m_get_ext_free(n) != NULL) {
258 sharedcluster = 1;
259 } else if (m_mclhasreference(n)) {
260 sharedcluster = 1;
261 } else {
262 sharedcluster = 0;
263 }
264 }
265
266 /*
267 * If we have enough space left in current mbuf to accomodate
268 * tail length, copy tail length worth of data starting with next mbuf
269 * and adjust the length of next one accordingly.
270 */
271 if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen
272 && !sharedcluster) {
273 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
274 n->m_len += tlen;
275 m_adj(n->m_next, tlen);
276 goto ok;
277 }
278
279 /*
280 * If have enough leading space in next mbuf to accomodate head length
281 * of current mbuf, and total resulting length of next mbuf is greater
282 * than or equal to requested len bytes, then just copy hlen from
283 * current to the next one and adjust sizes accordingly.
284 */
285 if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen &&
286 (n->m_next->m_len + hlen) >= len && !sharedcluster) {
287 n->m_next->m_data -= hlen;
288 n->m_next->m_len += hlen;
289 bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
290 n->m_len -= hlen;
291 n = n->m_next;
292 off = 0;
293 goto ok;
294 }
295
296 /*
297 * now, we need to do the hard way. don't m_copy as there's no room
298 * on both end.
299 */
300 MGET(o, M_DONTWAIT, m->m_type);
301 if (o == NULL) {
302 m_freem(m);
303 return NULL; /* ENOBUFS */
304 }
305 if (len > MHLEN) { /* use MHLEN just for safety */
306 MCLGET(o, M_DONTWAIT);
307 if ((o->m_flags & M_EXT) == 0) {
308 m_freem(m);
309 m_free(o);
310 return NULL; /* ENOBUFS */
311 }
312 }
313 /* get hlen from <n, off> into <o, 0> */
314 o->m_len = hlen;
315 bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen);
316 n->m_len -= hlen;
317 /* get tlen from <n->m_next, 0> into <o, hlen> */
318 m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
319 o->m_len += tlen;
320 m_adj(n->m_next, tlen);
321 o->m_next = n->m_next;
322 n->m_next = o;
323 n = o;
324 off = 0;
325
326 ok:
327 #ifdef PULLDOWN_DEBUG
328 {
329 struct mbuf *t;
330 printf("after:");
331 for (t = m; t; t = t->m_next) {
332 printf("%c%d", t == n ? '*' : ' ', t->m_len);
333 }
334 printf(" (off=%d)\n", off);
335 }
336 #endif
337 if (offp) {
338 *offp = off;
339 }
340 return n;
341 }
342
343 /*
344 * Create and return an m_tag, either by re-using space in a previous tag
345 * or by allocating a new mbuf/cluster
346 */
347 struct m_tag *
348 m_tag_create(u_int32_t id, u_int16_t type, int len, int wait, struct mbuf *buf)
349 {
350 struct m_tag *t = NULL;
351 struct m_tag *p;
352
353 if (len < 0) {
354 return NULL;
355 }
356
357 if (len + sizeof(struct m_tag) + sizeof(struct m_taghdr) > MLEN) {
358 return m_tag_alloc(id, type, len, wait);
359 }
360
361 /*
362 * We've exhausted all external cases. Now, go through the m_tag
363 * chain and see if we can fit it in any of them.
364 * If not (t == NULL), call m_tag_alloc to store it in a new mbuf.
365 */
366 p = SLIST_FIRST(&buf->m_pkthdr.tags);
367 while (p != NULL) {
368 /* 2KCL m_tag */
369 if (M_TAG_ALIGN(p->m_tag_len) +
370 sizeof(struct m_taghdr) > MLEN) {
371 p = SLIST_NEXT(p, m_tag_link);
372 continue;
373 }
374
375 VERIFY(p->m_tag_cookie == M_TAG_VALID_PATTERN);
376
377 struct mbuf *m = m_dtom(p);
378 struct m_taghdr *hdr = (struct m_taghdr *)(void *)m->m_data;
379
380 VERIFY(IS_P2ALIGNED(hdr + 1, sizeof(u_int64_t)));
381 VERIFY(m->m_flags & M_TAGHDR && !(m->m_flags & M_EXT));
382
383 /* The mbuf can store this m_tag */
384 if (M_TAG_ALIGN(len) <= MLEN - m->m_len) {
385 t = (struct m_tag *)(void *)(m->m_data + m->m_len);
386 VERIFY(IS_P2ALIGNED(t, sizeof(u_int64_t)));
387 hdr->refcnt++;
388 m->m_len += M_TAG_ALIGN(len);
389 VERIFY(m->m_len <= MLEN);
390 break;
391 }
392
393 p = SLIST_NEXT(p, m_tag_link);
394 }
395
396 if (t == NULL) {
397 return m_tag_alloc(id, type, len, wait);
398 }
399
400 t->m_tag_cookie = M_TAG_VALID_PATTERN;
401 t->m_tag_type = type;
402 t->m_tag_len = len;
403 t->m_tag_id = id;
404 if (len > 0) {
405 bzero(t + 1, len);
406 }
407 return t;
408 }
409
410 /* Get a packet tag structure along with specified data following. */
411 struct m_tag *
412 m_tag_alloc(u_int32_t id, u_int16_t type, int len, int wait)
413 {
414 struct m_tag *t;
415
416 if (len < 0) {
417 return NULL;
418 }
419
420 if (M_TAG_ALIGN(len) + sizeof(struct m_taghdr) <= MLEN) {
421 struct mbuf *m = m_get(wait, MT_TAG);
422 struct m_taghdr *hdr;
423
424 if (m == NULL) {
425 return NULL;
426 }
427
428 m->m_flags |= M_TAGHDR;
429
430 hdr = (struct m_taghdr *)(void *)m->m_data;
431 VERIFY(IS_P2ALIGNED(hdr + 1, sizeof(u_int64_t)));
432 hdr->refcnt = 1;
433 m->m_len += sizeof(struct m_taghdr);
434 t = (struct m_tag *)(void *)(m->m_data + m->m_len);
435 VERIFY(IS_P2ALIGNED(t, sizeof(u_int64_t)));
436 m->m_len += M_TAG_ALIGN(len);
437 VERIFY(m->m_len <= MLEN);
438 } else if (len + sizeof(struct m_tag) <= MCLBYTES) {
439 t = (struct m_tag *)(void *)m_mclalloc(wait);
440 } else {
441 t = NULL;
442 }
443
444 if (t == NULL) {
445 return NULL;
446 }
447
448 VERIFY(IS_P2ALIGNED(t, sizeof(u_int64_t)));
449 t->m_tag_cookie = M_TAG_VALID_PATTERN;
450 t->m_tag_type = type;
451 t->m_tag_len = len;
452 t->m_tag_id = id;
453 if (len > 0) {
454 bzero(t + 1, len);
455 }
456 return t;
457 }
458
459
460 /* Free a packet tag. */
461 void
462 m_tag_free(struct m_tag *t)
463 {
464 #if CONFIG_MACF_NET
465 if (t != NULL &&
466 t->m_tag_id == KERNEL_MODULE_TAG_ID &&
467 t->m_tag_type == KERNEL_TAG_TYPE_MACLABEL) {
468 mac_mbuf_tag_destroy(t);
469 }
470 #endif
471 if (t == NULL) {
472 return;
473 }
474
475 VERIFY(t->m_tag_cookie == M_TAG_VALID_PATTERN);
476
477 if (M_TAG_ALIGN(t->m_tag_len) + sizeof(struct m_taghdr) <= MLEN) {
478 struct mbuf * m = m_dtom(t);
479 VERIFY(m->m_flags & M_TAGHDR);
480 struct m_taghdr *hdr = (struct m_taghdr *)(void *)m->m_data;
481
482 VERIFY(IS_P2ALIGNED(hdr + 1, sizeof(u_int64_t)));
483
484 /* No other tags in this mbuf */
485 if (--hdr->refcnt == 0) {
486 m_free(m);
487 return;
488 }
489
490 /* Pattern-fill the header */
491 u_int64_t *fill_ptr = (u_int64_t *)t;
492 u_int64_t *end_ptr = (u_int64_t *)(t + 1);
493 while (fill_ptr < end_ptr) {
494 *fill_ptr = M_TAG_FREE_PATTERN;
495 fill_ptr++;
496 }
497 } else {
498 m_mclfree((caddr_t)t);
499 }
500 }
501
502 /* Prepend a packet tag. */
503 void
504 m_tag_prepend(struct mbuf *m, struct m_tag *t)
505 {
506 VERIFY(m != NULL && t != NULL);
507
508 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
509 }
510
511 /* Unlink a packet tag. */
512 void
513 m_tag_unlink(struct mbuf *m, struct m_tag *t)
514 {
515 VERIFY(m->m_flags & M_PKTHDR);
516 VERIFY(t != NULL && t->m_tag_cookie == M_TAG_VALID_PATTERN);
517
518 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
519 }
520
521 /* Unlink and free a packet tag. */
522 void
523 m_tag_delete(struct mbuf *m, struct m_tag *t)
524 {
525 m_tag_unlink(m, t);
526 m_tag_free(t);
527 }
528
529 /* Unlink and free a packet tag chain, starting from given tag. */
530 void
531 m_tag_delete_chain(struct mbuf *m, struct m_tag *t)
532 {
533 struct m_tag *p, *q;
534
535 VERIFY(m->m_flags & M_PKTHDR);
536
537 if (t != NULL) {
538 p = t;
539 } else {
540 p = SLIST_FIRST(&m->m_pkthdr.tags);
541 }
542 if (p == NULL) {
543 return;
544 }
545
546 VERIFY(p->m_tag_cookie == M_TAG_VALID_PATTERN);
547 while ((q = SLIST_NEXT(p, m_tag_link)) != NULL) {
548 VERIFY(q->m_tag_cookie == M_TAG_VALID_PATTERN);
549 m_tag_delete(m, q);
550 }
551 m_tag_delete(m, p);
552 }
553
554 /* Find a tag, starting from a given position. */
555 struct m_tag *
556 m_tag_locate(struct mbuf *m, u_int32_t id, u_int16_t type, struct m_tag *t)
557 {
558 struct m_tag *p;
559
560 VERIFY(m->m_flags & M_PKTHDR);
561
562 if (t == NULL) {
563 p = SLIST_FIRST(&m->m_pkthdr.tags);
564 } else {
565 VERIFY(t->m_tag_cookie == M_TAG_VALID_PATTERN);
566 p = SLIST_NEXT(t, m_tag_link);
567 }
568 while (p != NULL) {
569 VERIFY(p->m_tag_cookie == M_TAG_VALID_PATTERN);
570 if (p->m_tag_id == id && p->m_tag_type == type) {
571 return p;
572 }
573 p = SLIST_NEXT(p, m_tag_link);
574 }
575 return NULL;
576 }
577
578 /* Copy a single tag. */
579 struct m_tag *
580 m_tag_copy(struct m_tag *t, int how)
581 {
582 struct m_tag *p;
583
584 VERIFY(t != NULL);
585
586 p = m_tag_alloc(t->m_tag_id, t->m_tag_type, t->m_tag_len, how);
587 if (p == NULL) {
588 return NULL;
589 }
590 #if CONFIG_MACF_NET
591 /*
592 * XXXMAC: we should probably pass off the initialization, and
593 * copying here? can we hid that KERNEL_TAG_TYPE_MACLABEL is
594 * special from the mbuf code?
595 */
596 if (t != NULL &&
597 t->m_tag_id == KERNEL_MODULE_TAG_ID &&
598 t->m_tag_type == KERNEL_TAG_TYPE_MACLABEL) {
599 if (mac_mbuf_tag_init(p, how) != 0) {
600 m_tag_free(p);
601 return NULL;
602 }
603 mac_mbuf_tag_copy(t, p);
604 } else
605 #endif
606 bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */
607 return p;
608 }
609
610 /*
611 * Copy two tag chains. The destination mbuf (to) loses any attached
612 * tags even if the operation fails. This should not be a problem, as
613 * m_tag_copy_chain() is typically called with a newly-allocated
614 * destination mbuf.
615 */
616 int
617 m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how)
618 {
619 struct m_tag *p, *t, *tprev = NULL;
620
621 VERIFY((to->m_flags & M_PKTHDR) && (from->m_flags & M_PKTHDR));
622
623 m_tag_delete_chain(to, NULL);
624 SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
625 VERIFY(p->m_tag_cookie == M_TAG_VALID_PATTERN);
626 t = m_tag_copy(p, how);
627 if (t == NULL) {
628 m_tag_delete_chain(to, NULL);
629 return 0;
630 }
631 if (tprev == NULL) {
632 SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
633 } else {
634 SLIST_INSERT_AFTER(tprev, t, m_tag_link);
635 tprev = t;
636 }
637 }
638 return 1;
639 }
640
641 /* Initialize dynamic and static tags on an mbuf. */
642 void
643 m_tag_init(struct mbuf *m, int all)
644 {
645 VERIFY(m->m_flags & M_PKTHDR);
646
647 SLIST_INIT(&m->m_pkthdr.tags);
648 /*
649 * If the caller wants to preserve static mbuf tags
650 * (e.g. m_dup_pkthdr), don't zero them out.
651 */
652 if (all) {
653 bzero(&m->m_pkthdr.builtin_mtag._net_mtag,
654 sizeof(m->m_pkthdr.builtin_mtag._net_mtag));
655 }
656 }
657
658 /* Get first tag in chain. */
659 struct m_tag *
660 m_tag_first(struct mbuf *m)
661 {
662 VERIFY(m->m_flags & M_PKTHDR);
663
664 return SLIST_FIRST(&m->m_pkthdr.tags);
665 }
666
667 /* Get next tag in chain. */
668 struct m_tag *
669 m_tag_next(struct mbuf *m, struct m_tag *t)
670 {
671 #pragma unused(m)
672 VERIFY(t != NULL);
673 VERIFY(t->m_tag_cookie == M_TAG_VALID_PATTERN);
674
675 return SLIST_NEXT(t, m_tag_link);
676 }
677
678 int
679 m_set_traffic_class(struct mbuf *m, mbuf_traffic_class_t tc)
680 {
681 u_int32_t val = MBUF_TC2SCVAL(tc); /* just the val portion */
682
683 return m_set_service_class(m, m_service_class_from_val(val));
684 }
685
686 mbuf_traffic_class_t
687 m_get_traffic_class(struct mbuf *m)
688 {
689 return MBUF_SC2TC(m_get_service_class(m));
690 }
691
692 int
693 m_set_service_class(struct mbuf *m, mbuf_svc_class_t sc)
694 {
695 int error = 0;
696
697 VERIFY(m->m_flags & M_PKTHDR);
698
699 if (MBUF_VALID_SC(sc)) {
700 m->m_pkthdr.pkt_svc = sc;
701 } else {
702 error = EINVAL;
703 }
704
705 return error;
706 }
707
708 mbuf_svc_class_t
709 m_get_service_class(struct mbuf *m)
710 {
711 mbuf_svc_class_t sc;
712
713 VERIFY(m->m_flags & M_PKTHDR);
714
715 if (MBUF_VALID_SC(m->m_pkthdr.pkt_svc)) {
716 sc = m->m_pkthdr.pkt_svc;
717 } else {
718 sc = MBUF_SC_BE;
719 }
720
721 return sc;
722 }
723
724 mbuf_svc_class_t
725 m_service_class_from_idx(u_int32_t i)
726 {
727 mbuf_svc_class_t sc = MBUF_SC_BE;
728
729 switch (i) {
730 case SCIDX_BK_SYS:
731 return MBUF_SC_BK_SYS;
732
733 case SCIDX_BK:
734 return MBUF_SC_BK;
735
736 case SCIDX_BE:
737 return MBUF_SC_BE;
738
739 case SCIDX_RD:
740 return MBUF_SC_RD;
741
742 case SCIDX_OAM:
743 return MBUF_SC_OAM;
744
745 case SCIDX_AV:
746 return MBUF_SC_AV;
747
748 case SCIDX_RV:
749 return MBUF_SC_RV;
750
751 case SCIDX_VI:
752 return MBUF_SC_VI;
753
754 case SCIDX_VO:
755 return MBUF_SC_VO;
756
757 case SCIDX_CTL:
758 return MBUF_SC_CTL;
759
760 default:
761 break;
762 }
763
764 VERIFY(0);
765 /* NOTREACHED */
766 return sc;
767 }
768
769 mbuf_svc_class_t
770 m_service_class_from_val(u_int32_t v)
771 {
772 mbuf_svc_class_t sc = MBUF_SC_BE;
773
774 switch (v) {
775 case SCVAL_BK_SYS:
776 return MBUF_SC_BK_SYS;
777
778 case SCVAL_BK:
779 return MBUF_SC_BK;
780
781 case SCVAL_BE:
782 return MBUF_SC_BE;
783
784 case SCVAL_RD:
785 return MBUF_SC_RD;
786
787 case SCVAL_OAM:
788 return MBUF_SC_OAM;
789
790 case SCVAL_AV:
791 return MBUF_SC_AV;
792
793 case SCVAL_RV:
794 return MBUF_SC_RV;
795
796 case SCVAL_VI:
797 return MBUF_SC_VI;
798
799 case SCVAL_VO:
800 return MBUF_SC_VO;
801
802 case SCVAL_CTL:
803 return MBUF_SC_CTL;
804
805 default:
806 break;
807 }
808
809 VERIFY(0);
810 /* NOTREACHED */
811 return sc;
812 }
813
814 uint16_t
815 m_adj_sum16(struct mbuf *m, uint32_t start, uint32_t dataoff,
816 uint32_t datalen, uint32_t sum)
817 {
818 uint32_t total_sub = 0; /* total to subtract */
819 uint32_t mlen = m_pktlen(m); /* frame length */
820 uint32_t bytes = (dataoff + datalen); /* bytes covered by sum */
821 int len;
822
823 ASSERT(bytes <= mlen);
824
825 /*
826 * Take care of excluding (len > 0) or including (len < 0)
827 * extraneous octets at the beginning of the packet, taking
828 * into account the start offset.
829 */
830 len = (dataoff - start);
831 if (len > 0) {
832 total_sub = m_sum16(m, start, len);
833 } else if (len < 0) {
834 sum += m_sum16(m, dataoff, -len);
835 }
836
837 /*
838 * Take care of excluding any postpended extraneous octets.
839 */
840 len = (mlen - bytes);
841 if (len > 0) {
842 struct mbuf *m0 = m;
843 uint32_t extra = m_sum16(m, bytes, len);
844 uint32_t off = bytes, off0 = off;
845
846 while (off > 0) {
847 if (__improbable(m == NULL)) {
848 panic("%s: invalid mbuf chain %p [off %u, "
849 "len %u]", __func__, m0, off0, len);
850 /* NOTREACHED */
851 }
852 if (off < m->m_len) {
853 break;
854 }
855 off -= m->m_len;
856 m = m->m_next;
857 }
858
859 /* if we started on odd-alignment, swap the value */
860 if ((uintptr_t)(mtod(m, uint8_t *) + off) & 1) {
861 total_sub += ((extra << 8) & 0xffff) | (extra >> 8);
862 } else {
863 total_sub += extra;
864 }
865
866 total_sub = (total_sub >> 16) + (total_sub & 0xffff);
867 }
868
869 /*
870 * 1's complement subtract any extraneous octets.
871 */
872 if (total_sub != 0) {
873 if (total_sub >= sum) {
874 sum = ~(total_sub - sum) & 0xffff;
875 } else {
876 sum -= total_sub;
877 }
878 }
879
880 /* fold 32-bit to 16-bit */
881 sum = (sum >> 16) + (sum & 0xffff); /* 17-bit */
882 sum = (sum >> 16) + (sum & 0xffff); /* 16-bit + carry */
883 sum = (sum >> 16) + (sum & 0xffff); /* final carry */
884
885 return sum & 0xffff;
886 }
887
888 uint16_t
889 m_sum16(struct mbuf *m, uint32_t off, uint32_t len)
890 {
891 int mlen;
892
893 /*
894 * Sanity check
895 *
896 * Use m_length2() instead of m_length(), as we cannot rely on
897 * the caller setting m_pkthdr.len correctly, if the mbuf is
898 * a M_PKTHDR one.
899 */
900 if ((mlen = m_length2(m, NULL)) < (off + len)) {
901 panic("%s: mbuf %p len (%d) < off+len (%d+%d)\n", __func__,
902 m, mlen, off, len);
903 /* NOTREACHED */
904 }
905
906 return os_cpu_in_cksum_mbuf(m, len, off, 0);
907 }