]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/uipc_mbuf2.c
xnu-6153.41.3.tar.gz
[apple/xnu.git] / bsd / kern / uipc_mbuf2.c
CommitLineData
1c79356b 1/*
a39ff7e2 2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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.
0a7de745 33 *
1c79356b
A
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.
0a7de745 45 *
1c79356b
A
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 */
2d21ac55
A
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 */
1c79356b 99
1c79356b 100
9bccf70c 101/*#define PULLDOWN_DEBUG*/
1c79356b
A
102
103#include <sys/param.h>
104#include <sys/systm.h>
91447636 105#include <sys/proc_internal.h>
1c79356b
A
106#include <sys/malloc.h>
107#include <sys/mbuf.h>
6d2010ae 108#include <sys/mcache.h>
1c79356b 109#include <netinet/in.h>
39236c6e
A
110#include <netinet/ip_var.h>
111#if INET6
1c79356b
A
112#include <netinet/ip6.h>
113#include <netinet6/ip6_var.h>
6d2010ae 114#endif /* INET6 */
1c79356b 115
2d21ac55
A
116#if CONFIG_MACF_NET
117#include <security/mac_framework.h>
118#endif
119
1c79356b
A
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 */
130struct mbuf *
2d21ac55 131m_pulldown(struct mbuf *m, int off, int len, int *offp)
1c79356b 132{
a39ff7e2
A
133 struct mbuf *n = NULL, *o = NULL;
134 int hlen = 0, tlen = 0, olen = 0;
135 int sharedcluster = 0;
1c79356b
A
136
137 /* check invalid arguments. */
cb323159
A
138 VERIFY(len >= 0 && off >= 0);
139
0a7de745 140 if (m == NULL) {
1c79356b 141 panic("m == NULL in m_pulldown()");
0a7de745 142 }
1c79356b
A
143 if (len > MCLBYTES) {
144 m_freem(m);
0a7de745 145 return NULL; /* impossible */
1c79356b 146 }
cb323159
A
147 int tmp_len = 0;
148 if (os_add_overflow(off, len, &tmp_len)) {
149 m_free(m);
150 return NULL;
1c79356b
A
151 }
152
1c79356b 153#ifdef PULLDOWN_DEBUG
0a7de745
A
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 }
1c79356b
A
162#endif
163 n = m;
a39ff7e2
A
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 */
1c79356b 171 while (n != NULL && off > 0) {
0a7de745 172 if (n->m_len > off) {
1c79356b 173 break;
0a7de745 174 }
1c79356b
A
175 off -= n->m_len;
176 n = n->m_next;
177 }
a39ff7e2 178
1c79356b 179 /* be sure to point non-empty mbuf */
0a7de745 180 while (n != NULL && n->m_len == 0) {
1c79356b 181 n = n->m_next;
0a7de745 182 }
a39ff7e2 183
1c79356b
A
184 if (!n) {
185 m_freem(m);
0a7de745 186 return NULL; /* mbuf chain too short */
1c79356b
A
187 }
188
189 /*
190 * the target data is on <n, off>.
191 * if we got enough data on the mbuf "n", we're done.
a39ff7e2
A
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.
1c79356b 202 */
0a7de745 203 if ((off == 0 || offp) && len <= n->m_len - off) {
1c79356b 204 goto ok;
0a7de745 205 }
1c79356b 206
1c79356b 207 /*
a39ff7e2 208 * when len <= n->m_len - off and off != 0, it is a special case.
1c79356b
A
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 */
a39ff7e2 213 if (len <= n->m_len - off) {
1c79356b
A
214 o = m_copym(n, off, n->m_len - off, M_DONTWAIT);
215 if (o == NULL) {
216 m_freem(m);
0a7de745 217 return NULL; /* ENOBUFS */
1c79356b
A
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.
a39ff7e2
A
231 *
232 * Read these variables as head length and tail length
1c79356b
A
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;
0a7de745 242 for (o = n->m_next; o != NULL; o = o->m_next) {
1c79356b 243 olen += o->m_len;
0a7de745 244 }
1c79356b
A
245 if (hlen + olen < len) {
246 m_freem(m);
0a7de745 247 return NULL; /* mbuf chain too short */
1c79356b
A
248 }
249
250 /*
251 * easy cases first.
252 * we need to use m_copydata() to get data from <n->m_next, 0>.
253 */
0a7de745 254 if ((n->m_flags & M_EXT) == 0) {
1c79356b 255 sharedcluster = 0;
0a7de745
A
256 } else {
257 if (m_get_ext_free(n) != NULL) {
1c79356b 258 sharedcluster = 1;
0a7de745 259 } else if (m_mclhasreference(n)) {
1c79356b 260 sharedcluster = 1;
0a7de745 261 } else {
1c79356b 262 sharedcluster = 0;
0a7de745 263 }
1c79356b 264 }
a39ff7e2
A
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 */
1c79356b 271 if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen
0a7de745 272 && !sharedcluster) {
1c79356b
A
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 }
a39ff7e2
A
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) {
1c79356b
A
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 */
1c79356b
A
300 MGET(o, M_DONTWAIT, m->m_type);
301 if (o == NULL) {
302 m_freem(m);
0a7de745 303 return NULL; /* ENOBUFS */
1c79356b 304 }
0a7de745 305 if (len > MHLEN) { /* use MHLEN just for safety */
1c79356b
A
306 MCLGET(o, M_DONTWAIT);
307 if ((o->m_flags & M_EXT) == 0) {
308 m_freem(m);
309 m_free(o);
0a7de745 310 return NULL; /* ENOBUFS */
1c79356b
A
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
326ok:
327#ifdef PULLDOWN_DEBUG
0a7de745
A
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 }
1c79356b 336#endif
0a7de745 337 if (offp) {
1c79356b 338 *offp = off;
0a7de745 339 }
1c79356b
A
340 return n;
341}
342
6d2010ae
A
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 */
347struct m_tag *
348m_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
0a7de745
A
353 if (len < 0) {
354 return NULL;
355 }
6d2010ae 356
0a7de745
A
357 if (len + sizeof(struct m_tag) + sizeof(struct m_taghdr) > MLEN) {
358 return m_tag_alloc(id, type, len, wait);
359 }
6d2010ae
A
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);
0a7de745 367 while (p != NULL) {
6d2010ae
A
368 /* 2KCL m_tag */
369 if (M_TAG_ALIGN(p->m_tag_len) +
0a7de745 370 sizeof(struct m_taghdr) > MLEN) {
6d2010ae
A
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);
316670eb 378 struct m_taghdr *hdr = (struct m_taghdr *)(void *)m->m_data;
6d2010ae 379
0a7de745 380 VERIFY(IS_P2ALIGNED(hdr + 1, sizeof(u_int64_t)));
6d2010ae
A
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) {
316670eb 385 t = (struct m_tag *)(void *)(m->m_data + m->m_len);
0a7de745 386 VERIFY(IS_P2ALIGNED(t, sizeof(u_int64_t)));
6d2010ae
A
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
0a7de745
A
396 if (t == NULL) {
397 return m_tag_alloc(id, type, len, wait);
398 }
6d2010ae
A
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;
0a7de745 404 if (len > 0) {
6d2010ae 405 bzero(t + 1, len);
0a7de745
A
406 }
407 return t;
6d2010ae
A
408}
409
91447636
A
410/* Get a packet tag structure along with specified data following. */
411struct m_tag *
412m_tag_alloc(u_int32_t id, u_int16_t type, int len, int wait)
413{
414 struct m_tag *t;
415
0a7de745
A
416 if (len < 0) {
417 return NULL;
418 }
6d2010ae 419
0a7de745 420 if (M_TAG_ALIGN(len) + sizeof(struct m_taghdr) <= MLEN) {
91447636 421 struct mbuf *m = m_get(wait, MT_TAG);
6d2010ae
A
422 struct m_taghdr *hdr;
423
0a7de745
A
424 if (m == NULL) {
425 return NULL;
426 }
6d2010ae
A
427
428 m->m_flags |= M_TAGHDR;
429
316670eb 430 hdr = (struct m_taghdr *)(void *)m->m_data;
0a7de745 431 VERIFY(IS_P2ALIGNED(hdr + 1, sizeof(u_int64_t)));
6d2010ae 432 hdr->refcnt = 1;
0a7de745 433 m->m_len += sizeof(struct m_taghdr);
316670eb 434 t = (struct m_tag *)(void *)(m->m_data + m->m_len);
0a7de745 435 VERIFY(IS_P2ALIGNED(t, sizeof(u_int64_t)));
6d2010ae
A
436 m->m_len += M_TAG_ALIGN(len);
437 VERIFY(m->m_len <= MLEN);
0a7de745 438 } else if (len + sizeof(struct m_tag) <= MCLBYTES) {
316670eb 439 t = (struct m_tag *)(void *)m_mclalloc(wait);
0a7de745
A
440 } else {
441 t = NULL;
6d2010ae
A
442 }
443
0a7de745
A
444 if (t == NULL) {
445 return NULL;
446 }
6d2010ae 447
0a7de745 448 VERIFY(IS_P2ALIGNED(t, sizeof(u_int64_t)));
6d2010ae 449 t->m_tag_cookie = M_TAG_VALID_PATTERN;
91447636
A
450 t->m_tag_type = type;
451 t->m_tag_len = len;
452 t->m_tag_id = id;
0a7de745 453 if (len > 0) {
6d2010ae 454 bzero(t + 1, len);
0a7de745
A
455 }
456 return t;
91447636
A
457}
458
459
460/* Free a packet tag. */
461void
462m_tag_free(struct m_tag *t)
463{
2d21ac55 464#if CONFIG_MACF_NET
0a7de745
A
465 if (t != NULL &&
466 t->m_tag_id == KERNEL_MODULE_TAG_ID &&
467 t->m_tag_type == KERNEL_TAG_TYPE_MACLABEL) {
2d21ac55 468 mac_mbuf_tag_destroy(t);
0a7de745 469 }
2d21ac55 470#endif
0a7de745 471 if (t == NULL) {
91447636 472 return;
0a7de745 473 }
316670eb
A
474
475 VERIFY(t->m_tag_cookie == M_TAG_VALID_PATTERN);
476
0a7de745 477 if (M_TAG_ALIGN(t->m_tag_len) + sizeof(struct m_taghdr) <= MLEN) {
91447636 478 struct mbuf * m = m_dtom(t);
6d2010ae 479 VERIFY(m->m_flags & M_TAGHDR);
316670eb
A
480 struct m_taghdr *hdr = (struct m_taghdr *)(void *)m->m_data;
481
0a7de745 482 VERIFY(IS_P2ALIGNED(hdr + 1, sizeof(u_int64_t)));
6d2010ae
A
483
484 /* No other tags in this mbuf */
0a7de745 485 if (--hdr->refcnt == 0) {
6d2010ae
A
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 }
91447636 497 } else {
6d2010ae 498 m_mclfree((caddr_t)t);
91447636 499 }
91447636
A
500}
501
502/* Prepend a packet tag. */
503void
504m_tag_prepend(struct mbuf *m, struct m_tag *t)
505{
6d2010ae
A
506 VERIFY(m != NULL && t != NULL);
507
91447636
A
508 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
509}
510
511/* Unlink a packet tag. */
512void
513m_tag_unlink(struct mbuf *m, struct m_tag *t)
514{
39236c6e
A
515 VERIFY(m->m_flags & M_PKTHDR);
516 VERIFY(t != NULL && t->m_tag_cookie == M_TAG_VALID_PATTERN);
6d2010ae 517
91447636
A
518 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
519}
520
521/* Unlink and free a packet tag. */
522void
523m_tag_delete(struct mbuf *m, struct m_tag *t)
524{
91447636
A
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. */
530void
531m_tag_delete_chain(struct mbuf *m, struct m_tag *t)
532{
533 struct m_tag *p, *q;
534
39236c6e 535 VERIFY(m->m_flags & M_PKTHDR);
6d2010ae
A
536
537 if (t != NULL) {
91447636 538 p = t;
6d2010ae 539 } else {
91447636 540 p = SLIST_FIRST(&m->m_pkthdr.tags);
6d2010ae 541 }
0a7de745 542 if (p == NULL) {
91447636 543 return;
0a7de745 544 }
6d2010ae
A
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);
91447636 549 m_tag_delete(m, q);
6d2010ae 550 }
91447636
A
551 m_tag_delete(m, p);
552}
553
554/* Find a tag, starting from a given position. */
555struct m_tag *
556m_tag_locate(struct mbuf *m, u_int32_t id, u_int16_t type, struct m_tag *t)
557{
558 struct m_tag *p;
559
39236c6e 560 VERIFY(m->m_flags & M_PKTHDR);
6d2010ae
A
561
562 if (t == NULL) {
91447636 563 p = SLIST_FIRST(&m->m_pkthdr.tags);
6d2010ae
A
564 } else {
565 VERIFY(t->m_tag_cookie == M_TAG_VALID_PATTERN);
91447636 566 p = SLIST_NEXT(t, m_tag_link);
6d2010ae 567 }
91447636 568 while (p != NULL) {
6d2010ae 569 VERIFY(p->m_tag_cookie == M_TAG_VALID_PATTERN);
0a7de745
A
570 if (p->m_tag_id == id && p->m_tag_type == type) {
571 return p;
572 }
91447636
A
573 p = SLIST_NEXT(p, m_tag_link);
574 }
0a7de745 575 return NULL;
91447636
A
576}
577
578/* Copy a single tag. */
579struct m_tag *
580m_tag_copy(struct m_tag *t, int how)
581{
582 struct m_tag *p;
583
6d2010ae
A
584 VERIFY(t != NULL);
585
3a60a9f5 586 p = m_tag_alloc(t->m_tag_id, t->m_tag_type, t->m_tag_len, how);
0a7de745
A
587 if (p == NULL) {
588 return NULL;
589 }
2d21ac55
A
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 &&
0a7de745 597 t->m_tag_id == KERNEL_MODULE_TAG_ID &&
2d21ac55
A
598 t->m_tag_type == KERNEL_TAG_TYPE_MACLABEL) {
599 if (mac_mbuf_tag_init(p, how) != 0) {
600 m_tag_free(p);
0a7de745 601 return NULL;
2d21ac55
A
602 }
603 mac_mbuf_tag_copy(t, p);
604 } else
605#endif
91447636 606 bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */
0a7de745 607 return p;
91447636
A
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 */
616int
617m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how)
618{
619 struct m_tag *p, *t, *tprev = NULL;
620
39236c6e 621 VERIFY((to->m_flags & M_PKTHDR) && (from->m_flags & M_PKTHDR));
6d2010ae 622
91447636
A
623 m_tag_delete_chain(to, NULL);
624 SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
6d2010ae 625 VERIFY(p->m_tag_cookie == M_TAG_VALID_PATTERN);
91447636
A
626 t = m_tag_copy(p, how);
627 if (t == NULL) {
628 m_tag_delete_chain(to, NULL);
0a7de745 629 return 0;
91447636 630 }
6d2010ae 631 if (tprev == NULL) {
91447636 632 SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
6d2010ae 633 } else {
91447636
A
634 SLIST_INSERT_AFTER(tprev, t, m_tag_link);
635 tprev = t;
636 }
637 }
0a7de745 638 return 1;
91447636
A
639}
640
39236c6e 641/* Initialize dynamic and static tags on an mbuf. */
91447636 642void
39236c6e 643m_tag_init(struct mbuf *m, int all)
91447636 644{
39236c6e 645 VERIFY(m->m_flags & M_PKTHDR);
6d2010ae 646
91447636 647 SLIST_INIT(&m->m_pkthdr.tags);
39236c6e
A
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) {
5ba3f43e 653 bzero(&m->m_pkthdr.builtin_mtag._net_mtag,
0a7de745 654 sizeof(m->m_pkthdr.builtin_mtag._net_mtag));
39236c6e 655 }
91447636
A
656}
657
658/* Get first tag in chain. */
659struct m_tag *
660m_tag_first(struct mbuf *m)
661{
39236c6e 662 VERIFY(m->m_flags & M_PKTHDR);
6d2010ae 663
0a7de745 664 return SLIST_FIRST(&m->m_pkthdr.tags);
91447636
A
665}
666
667/* Get next tag in chain. */
668struct m_tag *
6d2010ae 669m_tag_next(struct mbuf *m, struct m_tag *t)
91447636 670{
d1ecb069 671#pragma unused(m)
6d2010ae
A
672 VERIFY(t != NULL);
673 VERIFY(t->m_tag_cookie == M_TAG_VALID_PATTERN);
674
0a7de745 675 return SLIST_NEXT(t, m_tag_link);
d1ecb069
A
676}
677
316670eb
A
678int
679m_set_traffic_class(struct mbuf *m, mbuf_traffic_class_t tc)
680{
0a7de745 681 u_int32_t val = MBUF_TC2SCVAL(tc); /* just the val portion */
316670eb 682
0a7de745 683 return m_set_service_class(m, m_service_class_from_val(val));
316670eb
A
684}
685
686mbuf_traffic_class_t
687m_get_traffic_class(struct mbuf *m)
688{
0a7de745 689 return MBUF_SC2TC(m_get_service_class(m));
316670eb
A
690}
691
316670eb
A
692int
693m_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
0a7de745 699 if (MBUF_VALID_SC(sc)) {
39236c6e 700 m->m_pkthdr.pkt_svc = sc;
0a7de745 701 } else {
316670eb 702 error = EINVAL;
0a7de745 703 }
316670eb 704
0a7de745 705 return error;
316670eb
A
706}
707
708mbuf_svc_class_t
709m_get_service_class(struct mbuf *m)
710{
711 mbuf_svc_class_t sc;
712
713 VERIFY(m->m_flags & M_PKTHDR);
714
0a7de745 715 if (MBUF_VALID_SC(m->m_pkthdr.pkt_svc)) {
39236c6e 716 sc = m->m_pkthdr.pkt_svc;
0a7de745 717 } else {
316670eb 718 sc = MBUF_SC_BE;
0a7de745 719 }
316670eb 720
0a7de745 721 return sc;
316670eb
A
722}
723
724mbuf_svc_class_t
725m_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:
0a7de745 731 return MBUF_SC_BK_SYS;
316670eb
A
732
733 case SCIDX_BK:
0a7de745 734 return MBUF_SC_BK;
316670eb
A
735
736 case SCIDX_BE:
0a7de745 737 return MBUF_SC_BE;
316670eb
A
738
739 case SCIDX_RD:
0a7de745 740 return MBUF_SC_RD;
316670eb
A
741
742 case SCIDX_OAM:
0a7de745 743 return MBUF_SC_OAM;
316670eb
A
744
745 case SCIDX_AV:
0a7de745 746 return MBUF_SC_AV;
316670eb
A
747
748 case SCIDX_RV:
0a7de745 749 return MBUF_SC_RV;
316670eb
A
750
751 case SCIDX_VI:
0a7de745 752 return MBUF_SC_VI;
316670eb
A
753
754 case SCIDX_VO:
0a7de745 755 return MBUF_SC_VO;
316670eb
A
756
757 case SCIDX_CTL:
0a7de745 758 return MBUF_SC_CTL;
316670eb
A
759
760 default:
761 break;
762 }
763
764 VERIFY(0);
765 /* NOTREACHED */
0a7de745 766 return sc;
316670eb
A
767}
768
769mbuf_svc_class_t
770m_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:
0a7de745 776 return MBUF_SC_BK_SYS;
316670eb
A
777
778 case SCVAL_BK:
0a7de745 779 return MBUF_SC_BK;
316670eb
A
780
781 case SCVAL_BE:
0a7de745 782 return MBUF_SC_BE;
316670eb
A
783
784 case SCVAL_RD:
0a7de745 785 return MBUF_SC_RD;
316670eb
A
786
787 case SCVAL_OAM:
0a7de745 788 return MBUF_SC_OAM;
316670eb
A
789
790 case SCVAL_AV:
0a7de745 791 return MBUF_SC_AV;
316670eb
A
792
793 case SCVAL_RV:
0a7de745 794 return MBUF_SC_RV;
316670eb
A
795
796 case SCVAL_VI:
0a7de745 797 return MBUF_SC_VI;
316670eb
A
798
799 case SCVAL_VO:
0a7de745 800 return MBUF_SC_VO;
316670eb
A
801
802 case SCVAL_CTL:
0a7de745 803 return MBUF_SC_CTL;
316670eb
A
804
805 default:
806 break;
807 }
808
809 VERIFY(0);
810 /* NOTREACHED */
0a7de745 811 return sc;
d1ecb069 812}
39236c6e
A
813
814uint16_t
5ba3f43e
A
815m_adj_sum16(struct mbuf *m, uint32_t start, uint32_t dataoff,
816 uint32_t datalen, uint32_t sum)
39236c6e 817{
0a7de745
A
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 */
5ba3f43e 821 int len;
39236c6e 822
5ba3f43e
A
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);
0a7de745 831 if (len > 0) {
5ba3f43e 832 total_sub = m_sum16(m, start, len);
0a7de745 833 } else if (len < 0) {
5ba3f43e 834 sum += m_sum16(m, dataoff, -len);
0a7de745 835 }
5ba3f43e
A
836
837 /*
838 * Take care of excluding any postpended extraneous octets.
839 */
840 len = (mlen - bytes);
39236c6e 841 if (len > 0) {
5ba3f43e
A
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 }
0a7de745 852 if (off < m->m_len) {
5ba3f43e 853 break;
0a7de745 854 }
5ba3f43e
A
855 off -= m->m_len;
856 m = m->m_next;
857 }
858
859 /* if we started on odd-alignment, swap the value */
0a7de745 860 if ((uintptr_t)(mtod(m, uint8_t *) + off) & 1) {
5ba3f43e 861 total_sub += ((extra << 8) & 0xffff) | (extra >> 8);
0a7de745 862 } else {
5ba3f43e 863 total_sub += extra;
0a7de745 864 }
5ba3f43e
A
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) {
0a7de745 873 if (total_sub >= sum) {
5ba3f43e 874 sum = ~(total_sub - sum) & 0xffff;
0a7de745 875 } else {
5ba3f43e 876 sum -= total_sub;
0a7de745 877 }
39236c6e
A
878 }
879
5ba3f43e 880 /* fold 32-bit to 16-bit */
0a7de745
A
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 */
39236c6e 884
0a7de745 885 return sum & 0xffff;
39236c6e
A
886}
887
39236c6e
A
888uint16_t
889m_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)) {
5ba3f43e
A
901 panic("%s: mbuf %p len (%d) < off+len (%d+%d)\n", __func__,
902 m, mlen, off, len);
903 /* NOTREACHED */
39236c6e
A
904 }
905
0a7de745 906 return os_cpu_in_cksum_mbuf(m, len, off, 0);
39236c6e 907}