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