1/*-
2 * Copyright (c) 1982, 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/kern/uipc_mbuf.c 149648 2005-08-30 21:31:42Z andre $");
34
35#include "opt_mac.h"
36#include "opt_param.h"
37#include "opt_mbuf_stress_test.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/limits.h>
43#include <sys/lock.h>
44#include <sys/mac.h>
45#include <sys/malloc.h>
46#include <sys/mbuf.h>
47#include <sys/sysctl.h>
48#include <sys/domain.h>
49#include <sys/protosw.h>
50#include <sys/uio.h>
51
52int max_linkhdr;
53int max_protohdr;
54int max_hdr;
55int max_datalen;
56#ifdef MBUF_STRESS_TEST
57int m_defragpackets;
58int m_defragbytes;
59int m_defraguseless;
60int m_defragfailure;
61int m_defragrandomfailures;
62#endif
63
64/*
65 * sysctl(8) exported objects
66 */
67SYSCTL_DECL(_kern_ipc);
68SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
69 &max_linkhdr, 0, "");
70SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
71 &max_protohdr, 0, "");
72SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
73SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
74 &max_datalen, 0, "");
75#ifdef MBUF_STRESS_TEST
76SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
77 &m_defragpackets, 0, "");
78SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
79 &m_defragbytes, 0, "");
80SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
81 &m_defraguseless, 0, "");
82SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
83 &m_defragfailure, 0, "");
84SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
85 &m_defragrandomfailures, 0, "");
86#endif
87
88/*
89 * Malloc-type for external ext_buf ref counts.
90 */
91static MALLOC_DEFINE(M_MBUF, "mbextcnt", "mbuf external ref counts");
92
93/*
94 * Allocate a given length worth of mbufs and/or clusters (whatever fits
95 * best) and return a pointer to the top of the allocated chain. If an
96 * existing mbuf chain is provided, then we will append the new chain
97 * to the existing one but still return the top of the newly allocated
98 * chain.
99 */
100struct mbuf *
101m_getm(struct mbuf *m, int len, int how, short type)
102{
103 struct mbuf *mb, *top, *cur, *mtail;
104 int num, rem;
105 int i;
106
107 KASSERT(len >= 0, ("m_getm(): len is < 0"));
108
109 /* If m != NULL, we will append to the end of that chain. */
110 if (m != NULL)
111 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next);
112 else
113 mtail = NULL;
114
115 /*
116 * Calculate how many mbufs+clusters ("packets") we need and how much
117 * leftover there is after that and allocate the first mbuf+cluster
118 * if required.
119 */
120 num = len / MCLBYTES;
121 rem = len % MCLBYTES;
122 top = cur = NULL;
123 if (num > 0) {
124 if ((top = cur = m_getcl(how, type, 0)) == NULL)
125 goto failed;
126 top->m_len = 0;
127 }
128 num--;
129
130 for (i = 0; i < num; i++) {
131 mb = m_getcl(how, type, 0);
132 if (mb == NULL)
133 goto failed;
134 mb->m_len = 0;
135 cur = (cur->m_next = mb);
136 }
137 if (rem > 0) {
138 mb = (rem > MINCLSIZE) ?
139 m_getcl(how, type, 0) : m_get(how, type);
140 if (mb == NULL)
141 goto failed;
142 mb->m_len = 0;
143 if (cur == NULL)
144 top = mb;
145 else
146 cur->m_next = mb;
147 }
148
149 if (mtail != NULL)
150 mtail->m_next = top;
151 return top;
152failed:
153 if (top != NULL)
154 m_freem(top);
155 return NULL;
156}
157
158/*
159 * Free an entire chain of mbufs and associated external buffers, if
160 * applicable.
161 */
162void
163m_freem(struct mbuf *mb)
164{
165
166 while (mb != NULL)
167 mb = m_free(mb);
168}
169
170/*-
171 * Configure a provided mbuf to refer to the provided external storage
172 * buffer and setup a reference count for said buffer. If the setting
173 * up of the reference count fails, the M_EXT bit will not be set. If
174 * successfull, the M_EXT bit is set in the mbuf's flags.
175 *
176 * Arguments:
177 * mb The existing mbuf to which to attach the provided buffer.
178 * buf The address of the provided external storage buffer.
179 * size The size of the provided buffer.
180 * freef A pointer to a routine that is responsible for freeing the
181 * provided external storage buffer.
182 * args A pointer to an argument structure (of any type) to be passed
183 * to the provided freef routine (may be NULL).
184 * flags Any other flags to be passed to the provided mbuf.
185 * type The type that the external storage buffer should be
186 * labeled with.
187 *
188 * Returns:
189 * Nothing.
190 */
191void
192m_extadd(struct mbuf *mb, caddr_t buf, u_int size,
193 void (*freef)(void *, void *), void *args, int flags, int type)
194{
195 u_int *ref_cnt = NULL;
196
197 /* XXX Shouldn't be adding EXT_CLUSTER with this API */
198 if (type == EXT_CLUSTER)
199 ref_cnt = (u_int *)uma_find_refcnt(zone_clust,
200 mb->m_ext.ext_buf);
201 else if (type == EXT_EXTREF)
202 ref_cnt = __DEVOLATILE(u_int *, mb->m_ext.ref_cnt);
203 mb->m_ext.ref_cnt = (ref_cnt == NULL) ?
204 malloc(sizeof(u_int), M_MBUF, M_NOWAIT) : (u_int *)ref_cnt;
205 if (mb->m_ext.ref_cnt != NULL) {
206 *(mb->m_ext.ref_cnt) = 1;
207 mb->m_flags |= (M_EXT | flags);
208 mb->m_ext.ext_buf = buf;
209 mb->m_data = mb->m_ext.ext_buf;
210 mb->m_ext.ext_size = size;
211 mb->m_ext.ext_free = freef;
212 mb->m_ext.ext_args = args;
213 mb->m_ext.ext_type = type;
214 }
215}
216
217/*
218 * Non-directly-exported function to clean up after mbufs with M_EXT
219 * storage attached to them if the reference count hits 0.
220 */
221void
222mb_free_ext(struct mbuf *m)
223{
224 u_int cnt;
225 int dofree;
226
227 /* Account for lazy ref count assign. */
228 if (m->m_ext.ref_cnt == NULL)
229 dofree = 1;
230 else
231 dofree = 0;
232
233 /*
234 * This is tricky. We need to make sure to decrement the
235 * refcount in a safe way but to also clean up if we're the
236 * last reference. This method seems to do it without race.
237 */
238 while (dofree == 0) {
239 cnt = *(m->m_ext.ref_cnt);
240 if (atomic_cmpset_int(m->m_ext.ref_cnt, cnt, cnt - 1)) {
241 if (cnt == 1)
242 dofree = 1;
243 break;
244 }
245 }
246
247 if (dofree) {
248 /*
249 * Do the free, should be safe.
250 */
251 if (m->m_ext.ext_type == EXT_PACKET) {
252 uma_zfree(zone_pack, m);
253 return;
254 } else if (m->m_ext.ext_type == EXT_CLUSTER) {
255 uma_zfree(zone_clust, m->m_ext.ext_buf);
256 m->m_ext.ext_buf = NULL;
257 } else {
258 (*(m->m_ext.ext_free))(m->m_ext.ext_buf,
259 m->m_ext.ext_args);
260 if (m->m_ext.ext_type != EXT_EXTREF) {
261 if (m->m_ext.ref_cnt != NULL)
262 free(__DEVOLATILE(u_int *,
263 m->m_ext.ref_cnt), M_MBUF);
264 m->m_ext.ref_cnt = NULL;
265 }
266 m->m_ext.ext_buf = NULL;
267 }
268 }
269 uma_zfree(zone_mbuf, m);
270}
271
272/*
273 * Clean up mbuf (chain) from any tags and packet headers.
274 * If "all" is set then the first mbuf in the chain will be
275 * cleaned too.
276 */
277void
278m_demote(struct mbuf *m0, int all)
279{
280 struct mbuf *m;
281
282 for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) {
283 if (m->m_flags & M_PKTHDR) {
284 m_tag_delete_chain(m, NULL);
285 m->m_flags &= ~M_PKTHDR;
286 bzero(&m->m_pkthdr, sizeof(struct pkthdr));
287 }
288 if (m->m_type == MT_HEADER)
289 m->m_type = MT_DATA;
290 if (m != m0 && m->m_nextpkt != NULL)
291 m->m_nextpkt = NULL;
292 m->m_flags = m->m_flags & (M_EXT|M_EOR|M_RDONLY|M_FREELIST);
293 }
294}
295
296/*
297 * Sanity checks on mbuf (chain) for use in KASSERT() and general
298 * debugging.
299 * Returns 0 or panics when bad and 1 on all tests passed.
300 * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they
301 * blow up later.
302 */
303int
304m_sanity(struct mbuf *m0, int sanitize)
305{
306 struct mbuf *m;
307 caddr_t a, b;
308 int pktlen = 0;
309
310#define M_SANITY_ACTION(s) return (0)
311/* #define M_SANITY_ACTION(s) panic("mbuf %p: " s, m) */
312
313 for (m = m0; m != NULL; m = m->m_next) {
314 /*
315 * Basic pointer checks. If any of these fails then some
316 * unrelated kernel memory before or after us is trashed.
317 * No way to recover from that.
318 */
319 a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf :
320 ((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) :
321 (caddr_t)(&m->m_dat)) );
322 b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size :
323 ((m->m_flags & M_PKTHDR) ? MHLEN : MLEN)));
324 if ((caddr_t)m->m_data < a)
325 M_SANITY_ACTION("m_data outside mbuf data range left");
326 if ((caddr_t)m->m_data > b)
327 M_SANITY_ACTION("m_data outside mbuf data range right");
328 if ((caddr_t)m->m_data + m->m_len > b)
329 M_SANITY_ACTION("m_data + m_len exeeds mbuf space");
330 if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.header) {
331 if ((caddr_t)m->m_pkthdr.header < a ||
332 (caddr_t)m->m_pkthdr.header > b)
333 M_SANITY_ACTION("m_pkthdr.header outside mbuf data range");
334 }
335
336 /* m->m_nextpkt may only be set on first mbuf in chain. */
337 if (m != m0 && m->m_nextpkt != NULL) {
338 if (sanitize) {
339 m_freem(m->m_nextpkt);
340 m->m_nextpkt = (struct mbuf *)0xDEADC0DE;
341 } else
342 M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf");
343 }
344
345 /* correct type correlations. */
346 if (m->m_type == MT_HEADER && !(m->m_flags & M_PKTHDR)) {
347 if (sanitize)
348 m->m_type = MT_DATA;
349 else
350 M_SANITY_ACTION("MT_HEADER set but not M_PKTHDR");
351 }
352
353 /* packet length (not mbuf length!) calculation */
354 if (m0->m_flags & M_PKTHDR)
355 pktlen += m->m_len;
356
357 /* m_tags may only be attached to first mbuf in chain. */
358 if (m != m0 && m->m_flags & M_PKTHDR &&
359 !SLIST_EMPTY(&m->m_pkthdr.tags)) {
360 if (sanitize) {
361 m_tag_delete_chain(m, NULL);
362 /* put in 0xDEADC0DE perhaps? */
363 } else
364 M_SANITY_ACTION("m_tags on in-chain mbuf");
365 }
366
367 /* M_PKTHDR may only be set on first mbuf in chain */
368 if (m != m0 && m->m_flags & M_PKTHDR) {
369 if (sanitize) {
370 bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
371 m->m_flags &= ~M_PKTHDR;
372 /* put in 0xDEADCODE and leave hdr flag in */
373 } else
374 M_SANITY_ACTION("M_PKTHDR on in-chain mbuf");
375 }
376 }
377 m = m0;
378 if (pktlen && pktlen != m->m_pkthdr.len) {
379 if (sanitize)
380 m->m_pkthdr.len = 0;
381 else
382 M_SANITY_ACTION("m_pkthdr.len != mbuf chain length");
383 }
384 return 1;
385
386#undef M_SANITY_ACTION
387}
388
389
390/*
391 * "Move" mbuf pkthdr from "from" to "to".
392 * "from" must have M_PKTHDR set, and "to" must be empty.
393 */
394void
395m_move_pkthdr(struct mbuf *to, struct mbuf *from)
396{
397
398#if 0
399 /* see below for why these are not enabled */
400 M_ASSERTPKTHDR(to);
401 /* Note: with MAC, this may not be a good assertion. */
402 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
403 ("m_move_pkthdr: to has tags"));
404#endif
405#ifdef MAC
406 /*
407 * XXXMAC: It could be this should also occur for non-MAC?
408 */
409 if (to->m_flags & M_PKTHDR)
410 m_tag_delete_chain(to, NULL);
411#endif
412 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
413 if ((to->m_flags & M_EXT) == 0)
414 to->m_data = to->m_pktdat;
415 to->m_pkthdr = from->m_pkthdr; /* especially tags */
416 SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */
417 from->m_flags &= ~M_PKTHDR;
418}
419
420/*
421 * Duplicate "from"'s mbuf pkthdr in "to".
422 * "from" must have M_PKTHDR set, and "to" must be empty.
423 * In particular, this does a deep copy of the packet tags.
424 */
425int
426m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
427{
428
429#if 0
430 /*
431 * The mbuf allocator only initializes the pkthdr
432 * when the mbuf is allocated with MGETHDR. Many users
433 * (e.g. m_copy*, m_prepend) use MGET and then
434 * smash the pkthdr as needed causing these
435 * assertions to trip. For now just disable them.
436 */
437 M_ASSERTPKTHDR(to);
438 /* Note: with MAC, this may not be a good assertion. */
439 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
440#endif
441 MBUF_CHECKSLEEP(how);
442#ifdef MAC
443 if (to->m_flags & M_PKTHDR)
444 m_tag_delete_chain(to, NULL);
445#endif
446 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
447 if ((to->m_flags & M_EXT) == 0)
448 to->m_data = to->m_pktdat;
449 to->m_pkthdr = from->m_pkthdr;
450 SLIST_INIT(&to->m_pkthdr.tags);
451 return (m_tag_copy_chain(to, from, MBTOM(how)));
452}
453
454/*
455 * Lesser-used path for M_PREPEND:
456 * allocate new mbuf to prepend to chain,
457 * copy junk along.
458 */
459struct mbuf *
460m_prepend(struct mbuf *m, int len, int how)
461{
462 struct mbuf *mn;
463
464 if (m->m_flags & M_PKTHDR)
465 MGETHDR(mn, how, m->m_type);
466 else
467 MGET(mn, how, m->m_type);
468 if (mn == NULL) {
469 m_freem(m);
470 return (NULL);
471 }
472 if (m->m_flags & M_PKTHDR)
473 M_MOVE_PKTHDR(mn, m);
474 mn->m_next = m;
475 m = mn;
476 if (len < MHLEN)
477 MH_ALIGN(m, len);
478 m->m_len = len;
479 return (m);
480}
481
482/*
483 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
484 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
485 * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
486 * Note that the copy is read-only, because clusters are not copied,
487 * only their reference counts are incremented.
488 */
489struct mbuf *
490m_copym(struct mbuf *m, int off0, int len, int wait)
491{
492 struct mbuf *n, **np;
493 int off = off0;
494 struct mbuf *top;
495 int copyhdr = 0;
496
497 KASSERT(off >= 0, ("m_copym, negative off %d", off));
498 KASSERT(len >= 0, ("m_copym, negative len %d", len));
499 MBUF_CHECKSLEEP(wait);
500 if (off == 0 && m->m_flags & M_PKTHDR)
501 copyhdr = 1;
502 while (off > 0) {
503 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
504 if (off < m->m_len)
505 break;
506 off -= m->m_len;
507 m = m->m_next;
508 }
509 np = ⊤
510 top = 0;
511 while (len > 0) {
512 if (m == NULL) {
513 KASSERT(len == M_COPYALL,
514 ("m_copym, length > size of mbuf chain"));
515 break;
516 }
517 if (copyhdr)
518 MGETHDR(n, wait, m->m_type);
519 else
520 MGET(n, wait, m->m_type);
521 *np = n;
522 if (n == NULL)
523 goto nospace;
524 if (copyhdr) {
525 if (!m_dup_pkthdr(n, m, wait))
526 goto nospace;
527 if (len == M_COPYALL)
528 n->m_pkthdr.len -= off0;
529 else
530 n->m_pkthdr.len = len;
531 copyhdr = 0;
532 }
533 n->m_len = min(len, m->m_len - off);
534 if (m->m_flags & M_EXT) {
535 n->m_data = m->m_data + off;
536 n->m_ext = m->m_ext;
537 n->m_flags |= M_EXT;
538 MEXT_ADD_REF(m);
539 n->m_ext.ref_cnt = m->m_ext.ref_cnt;
540 } else
541 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
542 (u_int)n->m_len);
543 if (len != M_COPYALL)
544 len -= n->m_len;
545 off = 0;
546 m = m->m_next;
547 np = &n->m_next;
548 }
549 if (top == NULL)
550 mbstat.m_mcfail++; /* XXX: No consistency. */
551
552 return (top);
553nospace:
554 m_freem(top);
555 mbstat.m_mcfail++; /* XXX: No consistency. */
556 return (NULL);
557}
558
559/*
560 * Returns mbuf chain with new head for the prepending case.
561 * Copies from mbuf (chain) n from off for len to mbuf (chain) m
562 * either prepending or appending the data.
563 * The resulting mbuf (chain) m is fully writeable.
564 * m is destination (is made writeable)
565 * n is source, off is offset in source, len is len from offset
566 * dir, 0 append, 1 prepend
567 * how, wait or nowait
568 */
569
570static int
571m_bcopyxxx(void *s, void *t, u_int len)
572{
573 bcopy(s, t, (size_t)len);
574 return 0;
575}
576
577struct mbuf *
578m_copymdata(struct mbuf *m, struct mbuf *n, int off, int len,
579 int prep, int how)
580{
581 struct mbuf *mm, *x, *z;
582 caddr_t p;
583 int i, mlen, nlen = 0;
584 caddr_t buf[MLEN];
585
586 KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source"));
587 KASSERT(off >= 0, ("m_copymdata, negative off %d", off));
588 KASSERT(len >= 0, ("m_copymdata, negative len %d", len));
589 KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep));
590
591 /* Make sure environment is sane. */
592 for (z = m; z != NULL; z = z->m_next) {
593 mlen += z->m_len;
594 if (!M_WRITABLE(z)) {
595 /* Make clusters writeable. */
596 if (z->m_flags & M_RDONLY)
597 return NULL; /* Can't handle ext ref. */
598 x = m_getcl(how, MT_DATA, 0);
599 if (!x)
600 return NULL;
601 bcopy(z->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size);
602 p = x->m_ext.ext_buf + (z->m_data - z->m_ext.ext_buf);
603 MEXT_REM_REF(z); /* XXX */
604 z->m_data = p;
605 x->m_flags &= ~M_EXT;
606 (void)m_free(x);
607 }
608 }
609 mm = prep ? m : z;
610 for (z = n; z != NULL; z = z->m_next)
611 nlen += z->m_len;
612 if (len == M_COPYALL)
613 len = nlen - off;
614 if (off + len > nlen || len < 1)
615 return NULL;
616
617 /*
618 * Append/prepend the data. Allocating mbufs as necessary.
619 */
620 /* Shortcut if enough free space in first/last mbuf. */
621 if (!prep && M_TRAILINGSPACE(mm) >= len) {
622 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) +
623 mm->m_len);
624 mm->m_len += len;
625 mm->m_pkthdr.len += len;
626 return m;
627 }
628 if (prep && M_LEADINGSPACE(mm) >= len) {
629 mm->m_data = mtod(mm, caddr_t) - len;
630 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t));
631 mm->m_len += len;
632 mm->m_pkthdr.len += len;
633 return mm;
634 }
635
636 /* Expand first/last mbuf to cluster if possible. */
637 if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) {
638 bcopy(mm->m_data, &buf, mm->m_len);
639 m_clget(mm, how);
640 if (!(mm->m_flags & M_EXT))
641 return NULL;
642 bcopy(&buf, mm->m_ext.ext_buf, mm->m_len);
643 mm->m_data = mm->m_ext.ext_buf;
644 mm->m_pkthdr.header = NULL;
645 }
646 if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) {
647 bcopy(mm->m_data, &buf, mm->m_len);
648 m_clget(mm, how);
649 if (!(mm->m_flags & M_EXT))
650 return NULL;
651 bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf +
652 mm->m_ext.ext_size - mm->m_len, mm->m_len);
653 mm->m_data = (caddr_t)mm->m_ext.ext_buf +
654 mm->m_ext.ext_size - mm->m_len;
655 mm->m_pkthdr.header = NULL;
656 }
657
658 /* Append/prepend as many mbuf (clusters) as necessary to fit len. */
659 if (!prep && len > M_TRAILINGSPACE(mm)) {
660 if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA))
661 return NULL;
662 }
663 if (prep && len > M_LEADINGSPACE(mm)) {
664 if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA)))
665 return NULL;
666 i = 0;
667 for (x = z; x != NULL; x = x->m_next) {
668 i += x->m_flags & M_EXT ? x->m_ext.ext_size :
669 (x->m_flags & M_PKTHDR ? MHLEN : MLEN);
670 if (!x->m_next)
671 break;
672 }
673 z->m_data += i - len;
674 m_move_pkthdr(mm, z);
675 x->m_next = mm;
676 mm = z;
677 }
678
679 /* Seek to start position in source mbuf. Optimization for long chains. */
680 while (off > 0) {
681 if (off < n->m_len)
682 break;
683 off -= n->m_len;
684 n = n->m_next;
685 }
686
687 /* Copy data into target mbuf. */
688 z = mm;
689 while (len > 0) {
690 KASSERT(z != NULL, ("m_copymdata, falling off target edge"));
691 i = M_TRAILINGSPACE(z);
692 m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len);
693 z->m_len += i;
694 /* fixup pkthdr.len if necessary */
695 if ((prep ? mm : m)->m_flags & M_PKTHDR)
696 (prep ? mm : m)->m_pkthdr.len += i;
697 off += i;
698 len -= i;
699 z = z->m_next;
700 }
701 return (prep ? mm : m);
702}
703
704/*
705 * Copy an entire packet, including header (which must be present).
706 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
707 * Note that the copy is read-only, because clusters are not copied,
708 * only their reference counts are incremented.
709 * Preserve alignment of the first mbuf so if the creator has left
710 * some room at the beginning (e.g. for inserting protocol headers)
711 * the copies still have the room available.
712 */
713struct mbuf *
714m_copypacket(struct mbuf *m, int how)
715{
716 struct mbuf *top, *n, *o;
717
718 MBUF_CHECKSLEEP(how);
719 MGET(n, how, m->m_type);
720 top = n;
721 if (n == NULL)
722 goto nospace;
723
724 if (!m_dup_pkthdr(n, m, how))
725 goto nospace;
726 n->m_len = m->m_len;
727 if (m->m_flags & M_EXT) {
728 n->m_data = m->m_data;
729 n->m_ext = m->m_ext;
730 n->m_flags |= M_EXT;
731 MEXT_ADD_REF(m);
732 n->m_ext.ref_cnt = m->m_ext.ref_cnt;
733 } else {
734 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
735 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
736 }
737
738 m = m->m_next;
739 while (m) {
740 MGET(o, how, m->m_type);
741 if (o == NULL)
742 goto nospace;
743
744 n->m_next = o;
745 n = n->m_next;
746
747 n->m_len = m->m_len;
748 if (m->m_flags & M_EXT) {
749 n->m_data = m->m_data;
750 n->m_ext = m->m_ext;
751 n->m_flags |= M_EXT;
752 MEXT_ADD_REF(m);
753 n->m_ext.ref_cnt = m->m_ext.ref_cnt;
754 } else {
755 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
756 }
757
758 m = m->m_next;
759 }
760 return top;
761nospace:
762 m_freem(top);
763 mbstat.m_mcfail++; /* XXX: No consistency. */
764 return (NULL);
765}
766
767/*
768 * Copy data from an mbuf chain starting "off" bytes from the beginning,
769 * continuing for "len" bytes, into the indicated buffer.
770 */
771void
772m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
773{
774 u_int count;
775
776 KASSERT(off >= 0, ("m_copydata, negative off %d", off));
777 KASSERT(len >= 0, ("m_copydata, negative len %d", len));
778 while (off > 0) {
779 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
780 if (off < m->m_len)
781 break;
782 off -= m->m_len;
783 m = m->m_next;
784 }
785 while (len > 0) {
786 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
787 count = min(m->m_len - off, len);
788 bcopy(mtod(m, caddr_t) + off, cp, count);
789 len -= count;
790 cp += count;
791 off = 0;
792 m = m->m_next;
793 }
794}
795
796/*
797 * Copy a packet header mbuf chain into a completely new chain, including
798 * copying any mbuf clusters. Use this instead of m_copypacket() when
799 * you need a writable copy of an mbuf chain.
800 */
801struct mbuf *
802m_dup(struct mbuf *m, int how)
803{
804 struct mbuf **p, *top = NULL;
805 int remain, moff, nsize;
806
807 MBUF_CHECKSLEEP(how);
808 /* Sanity check */
809 if (m == NULL)
810 return (NULL);
811 M_ASSERTPKTHDR(m);
812
813 /* While there's more data, get a new mbuf, tack it on, and fill it */
814 remain = m->m_pkthdr.len;
815 moff = 0;
816 p = ⊤
817 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */
818 struct mbuf *n;
819
820 /* Get the next new mbuf */
821 if (remain >= MINCLSIZE) {
822 n = m_getcl(how, m->m_type, 0);
823 nsize = MCLBYTES;
824 } else {
825 n = m_get(how, m->m_type);
826 nsize = MLEN;
827 }
828 if (n == NULL)
829 goto nospace;
830
831 if (top == NULL) { /* First one, must be PKTHDR */
832 if (!m_dup_pkthdr(n, m, how)) {
833 m_free(n);
834 goto nospace;
835 }
836 nsize = MHLEN;
837 }
838 n->m_len = 0;
839
840 /* Link it into the new chain */
841 *p = n;
842 p = &n->m_next;
843
844 /* Copy data from original mbuf(s) into new mbuf */
845 while (n->m_len < nsize && m != NULL) {
846 int chunk = min(nsize - n->m_len, m->m_len - moff);
847
848 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
849 moff += chunk;
850 n->m_len += chunk;
851 remain -= chunk;
852 if (moff == m->m_len) {
853 m = m->m_next;
854 moff = 0;
855 }
856 }
857
858 /* Check correct total mbuf length */
859 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
860 ("%s: bogus m_pkthdr.len", __func__));
861 }
862 return (top);
863
864nospace:
865 m_freem(top);
866 mbstat.m_mcfail++; /* XXX: No consistency. */
867 return (NULL);
868}
869
870/*
871 * Concatenate mbuf chain n to m.
872 * Both chains must be of the same type (e.g. MT_DATA).
873 * Any m_pkthdr is not updated.
874 */
875void
876m_cat(struct mbuf *m, struct mbuf *n)
877{
878 while (m->m_next)
879 m = m->m_next;
880 while (n) {
881 if (m->m_flags & M_EXT ||
882 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
883 /* just join the two chains */
884 m->m_next = n;
885 return;
886 }
887 /* splat the data from one into the other */
888 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
889 (u_int)n->m_len);
890 m->m_len += n->m_len;
891 n = m_free(n);
892 }
893}
894
895void
896m_adj(struct mbuf *mp, int req_len)
897{
898 int len = req_len;
899 struct mbuf *m;
900 int count;
901
902 if ((m = mp) == NULL)
903 return;
904 if (len >= 0) {
905 /*
906 * Trim from head.
907 */
908 while (m != NULL && len > 0) {
909 if (m->m_len <= len) {
910 len -= m->m_len;
911 m->m_len = 0;
912 m = m->m_next;
913 } else {
914 m->m_len -= len;
915 m->m_data += len;
916 len = 0;
917 }
918 }
919 m = mp;
920 if (mp->m_flags & M_PKTHDR)
921 m->m_pkthdr.len -= (req_len - len);
922 } else {
923 /*
924 * Trim from tail. Scan the mbuf chain,
925 * calculating its length and finding the last mbuf.
926 * If the adjustment only affects this mbuf, then just
927 * adjust and return. Otherwise, rescan and truncate
928 * after the remaining size.
929 */
930 len = -len;
931 count = 0;
932 for (;;) {
933 count += m->m_len;
934 if (m->m_next == (struct mbuf *)0)
935 break;
936 m = m->m_next;
937 }
938 if (m->m_len >= len) {
939 m->m_len -= len;
940 if (mp->m_flags & M_PKTHDR)
941 mp->m_pkthdr.len -= len;
942 return;
943 }
944 count -= len;
945 if (count < 0)
946 count = 0;
947 /*
948 * Correct length for chain is "count".
949 * Find the mbuf with last data, adjust its length,
950 * and toss data from remaining mbufs on chain.
951 */
952 m = mp;
953 if (m->m_flags & M_PKTHDR)
954 m->m_pkthdr.len = count;
955 for (; m; m = m->m_next) {
956 if (m->m_len >= count) {
957 m->m_len = count;
958 if (m->m_next != NULL) {
959 m_freem(m->m_next);
960 m->m_next = NULL;
961 }
962 break;
963 }
964 count -= m->m_len;
965 }
966 }
967}
968
969/*
970 * Rearange an mbuf chain so that len bytes are contiguous
971 * and in the data area of an mbuf (so that mtod and dtom
972 * will work for a structure of size len). Returns the resulting
973 * mbuf chain on success, frees it and returns null on failure.
974 * If there is room, it will add up to max_protohdr-len extra bytes to the
975 * contiguous region in an attempt to avoid being called next time.
976 */
977struct mbuf *
978m_pullup(struct mbuf *n, int len)
979{
980 struct mbuf *m;
981 int count;
982 int space;
983
984 /*
985 * If first mbuf has no cluster, and has room for len bytes
986 * without shifting current data, pullup into it,
987 * otherwise allocate a new mbuf to prepend to the chain.
988 */
989 if ((n->m_flags & M_EXT) == 0 &&
990 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
991 if (n->m_len >= len)
992 return (n);
993 m = n;
994 n = n->m_next;
995 len -= m->m_len;
996 } else {
997 if (len > MHLEN)
998 goto bad;
999 MGET(m, M_DONTWAIT, n->m_type);
1000 if (m == NULL)
1001 goto bad;
1002 m->m_len = 0;
1003 if (n->m_flags & M_PKTHDR)
1004 M_MOVE_PKTHDR(m, n);
1005 }
1006 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1007 do {
1008 count = min(min(max(len, max_protohdr), space), n->m_len);
1009 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1010 (u_int)count);
1011 len -= count;
1012 m->m_len += count;
1013 n->m_len -= count;
1014 space -= count;
1015 if (n->m_len)
1016 n->m_data += count;
1017 else
1018 n = m_free(n);
1019 } while (len > 0 && n);
1020 if (len > 0) {
1021 (void) m_free(m);
1022 goto bad;
1023 }
1024 m->m_next = n;
1025 return (m);
1026bad:
1027 m_freem(n);
1028 mbstat.m_mpfail++; /* XXX: No consistency. */
1029 return (NULL);
1030}
1031
1032/*
1033 * Like m_pullup(), except a new mbuf is always allocated, and we allow
1034 * the amount of empty space before the data in the new mbuf to be specified
1035 * (in the event that the caller expects to prepend later).
1036 */
1037int MSFail;
1038
1039struct mbuf *
1040m_copyup(struct mbuf *n, int len, int dstoff)
1041{
1042 struct mbuf *m;
1043 int count, space;
1044
1045 if (len > (MHLEN - dstoff))
1046 goto bad;
1047 MGET(m, M_DONTWAIT, n->m_type);
1048 if (m == NULL)
1049 goto bad;
1050 m->m_len = 0;
1051 if (n->m_flags & M_PKTHDR)
1052 M_MOVE_PKTHDR(m, n);
1053 m->m_data += dstoff;
1054 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1055 do {
1056 count = min(min(max(len, max_protohdr), space), n->m_len);
1057 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
1058 (unsigned)count);
1059 len -= count;
1060 m->m_len += count;
1061 n->m_len -= count;
1062 space -= count;
1063 if (n->m_len)
1064 n->m_data += count;
1065 else
1066 n = m_free(n);
1067 } while (len > 0 && n);
1068 if (len > 0) {
1069 (void) m_free(m);
1070 goto bad;
1071 }
1072 m->m_next = n;
1073 return (m);
1074 bad:
1075 m_freem(n);
1076 MSFail++;
1077 return (NULL);
1078}
1079
1080/*
1081 * Partition an mbuf chain in two pieces, returning the tail --
1082 * all but the first len0 bytes. In case of failure, it returns NULL and
1083 * attempts to restore the chain to its original state.
1084 *
1085 * Note that the resulting mbufs might be read-only, because the new
1086 * mbuf can end up sharing an mbuf cluster with the original mbuf if
1087 * the "breaking point" happens to lie within a cluster mbuf. Use the
1088 * M_WRITABLE() macro to check for this case.
1089 */
1090struct mbuf *
1091m_split(struct mbuf *m0, int len0, int wait)
1092{
1093 struct mbuf *m, *n;
1094 u_int len = len0, remain;
1095
1096 MBUF_CHECKSLEEP(wait);
1097 for (m = m0; m && len > m->m_len; m = m->m_next)
1098 len -= m->m_len;
1099 if (m == NULL)
1100 return (NULL);
1101 remain = m->m_len - len;
1102 if (m0->m_flags & M_PKTHDR) {
1103 MGETHDR(n, wait, m0->m_type);
1104 if (n == NULL)
1105 return (NULL);
1106 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1107 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1108 m0->m_pkthdr.len = len0;
1109 if (m->m_flags & M_EXT)
1110 goto extpacket;
1111 if (remain > MHLEN) {
1112 /* m can't be the lead packet */
1113 MH_ALIGN(n, 0);
1114 n->m_next = m_split(m, len, wait);
1115 if (n->m_next == NULL) {
1116 (void) m_free(n);
1117 return (NULL);
1118 } else {
1119 n->m_len = 0;
1120 return (n);
1121 }
1122 } else
1123 MH_ALIGN(n, remain);
1124 } else if (remain == 0) {
1125 n = m->m_next;
1126 m->m_next = NULL;
1127 return (n);
1128 } else {
1129 MGET(n, wait, m->m_type);
1130 if (n == NULL)
1131 return (NULL);
1132 M_ALIGN(n, remain);
1133 }
1134extpacket:
1135 if (m->m_flags & M_EXT) {
1136 n->m_flags |= M_EXT;
1137 n->m_ext = m->m_ext;
1138 MEXT_ADD_REF(m);
1139 n->m_ext.ref_cnt = m->m_ext.ref_cnt;
1140 n->m_data = m->m_data + len;
1141 } else {
1142 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
1143 }
1144 n->m_len = remain;
1145 m->m_len = len;
1146 n->m_next = m->m_next;
1147 m->m_next = NULL;
1148 return (n);
1149}
1150/*
1151 * Routine to copy from device local memory into mbufs.
1152 * Note that `off' argument is offset into first mbuf of target chain from
1153 * which to begin copying the data to.
1154 */
1155struct mbuf *
1156m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
1157 void (*copy)(char *from, caddr_t to, u_int len))
1158{
1159 struct mbuf *m;
1160 struct mbuf *top = NULL, **mp = ⊤
1161 int len;
1162
1163 if (off < 0 || off > MHLEN)
1164 return (NULL);
1165
1166 while (totlen > 0) {
1167 if (top == NULL) { /* First one, must be PKTHDR */
1168 if (totlen + off >= MINCLSIZE) {
1169 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1170 len = MCLBYTES;
1171 } else {
1172 m = m_gethdr(M_DONTWAIT, MT_DATA);
1173 len = MHLEN;
1174
1175 /* Place initial small packet/header at end of mbuf */
1176 if (m && totlen + off + max_linkhdr <= MLEN) {
1177 m->m_data += max_linkhdr;
1178 len -= max_linkhdr;
1179 }
1180 }
1181 if (m == NULL)
1182 return NULL;
1183 m->m_pkthdr.rcvif = ifp;
1184 m->m_pkthdr.len = totlen;
1185 } else {
1186 if (totlen + off >= MINCLSIZE) {
1187 m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1188 len = MCLBYTES;
1189 } else {
1190 m = m_get(M_DONTWAIT, MT_DATA);
1191 len = MLEN;
1192 }
1193 if (m == NULL) {
1194 m_freem(top);
1195 return NULL;
1196 }
1197 }
1198 if (off) {
1199 m->m_data += off;
1200 len -= off;
1201 off = 0;
1202 }
1203 m->m_len = len = min(totlen, len);
1204 if (copy)
1205 copy(buf, mtod(m, caddr_t), (u_int)len);
1206 else
1207 bcopy(buf, mtod(m, caddr_t), (u_int)len);
1208 buf += len;
1209 *mp = m;
1210 mp = &m->m_next;
1211 totlen -= len;
1212 }
1213 return (top);
1214}
1215
1216/*
1217 * Copy data from a buffer back into the indicated mbuf chain,
1218 * starting "off" bytes from the beginning, extending the mbuf
1219 * chain if necessary.
1220 */
1221void
1222m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp)
1223{
1224 int mlen;
1225 struct mbuf *m = m0, *n;
1226 int totlen = 0;
1227
1228 if (m0 == NULL)
1229 return;
1230 while (off > (mlen = m->m_len)) {
1231 off -= mlen;
1232 totlen += mlen;
1233 if (m->m_next == NULL) {
1234 n = m_get(M_DONTWAIT, m->m_type);
1235 if (n == NULL)
1236 goto out;
1237 bzero(mtod(n, caddr_t), MLEN);
1238 n->m_len = min(MLEN, len + off);
1239 m->m_next = n;
1240 }
1241 m = m->m_next;
1242 }
1243 while (len > 0) {
1244 mlen = min (m->m_len - off, len);
1245 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
1246 cp += mlen;
1247 len -= mlen;
1248 mlen += off;
1249 off = 0;
1250 totlen += mlen;
1251 if (len == 0)
1252 break;
1253 if (m->m_next == NULL) {
1254 n = m_get(M_DONTWAIT, m->m_type);
1255 if (n == NULL)
1256 break;
1257 n->m_len = min(MLEN, len);
1258 m->m_next = n;
1259 }
1260 m = m->m_next;
1261 }
1262out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1263 m->m_pkthdr.len = totlen;
1264}
1265
1266/*
1267 * Append the specified data to the indicated mbuf chain,
1268 * Extend the mbuf chain if the new data does not fit in
1269 * existing space.
1270 *
1271 * Return 1 if able to complete the job; otherwise 0.
1272 */
1273int
1274m_append(struct mbuf *m0, int len, c_caddr_t cp)
1275{
1276 struct mbuf *m, *n;
1277 int remainder, space;
1278
1279 for (m = m0; m->m_next != NULL; m = m->m_next)
1280 ;
1281 remainder = len;
1282 space = M_TRAILINGSPACE(m);
1283 if (space > 0) {
1284 /*
1285 * Copy into available space.
1286 */
1287 if (space > remainder)
1288 space = remainder;
1289 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1290 m->m_len += space;
1291 cp += space, remainder -= space;
1292 }
1293 while (remainder > 0) {
1294 /*
1295 * Allocate a new mbuf; could check space
1296 * and allocate a cluster instead.
1297 */
1298 n = m_get(M_DONTWAIT, m->m_type);
1299 if (n == NULL)
1300 break;
1301 n->m_len = min(MLEN, remainder);
1302 bcopy(cp, mtod(n, caddr_t), n->m_len);
1303 cp += n->m_len, remainder -= n->m_len;
1304 m->m_next = n;
1305 m = n;
1306 }
1307 if (m0->m_flags & M_PKTHDR)
1308 m0->m_pkthdr.len += len - remainder;
1309 return (remainder == 0);
1310}
1311
1312/*
1313 * Apply function f to the data in an mbuf chain starting "off" bytes from
1314 * the beginning, continuing for "len" bytes.
1315 */
1316int
1317m_apply(struct mbuf *m, int off, int len,
1318 int (*f)(void *, void *, u_int), void *arg)
1319{
1320 u_int count;
1321 int rval;
1322
1323 KASSERT(off >= 0, ("m_apply, negative off %d", off));
1324 KASSERT(len >= 0, ("m_apply, negative len %d", len));
1325 while (off > 0) {
1326 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1327 if (off < m->m_len)
1328 break;
1329 off -= m->m_len;
1330 m = m->m_next;
1331 }
1332 while (len > 0) {
1333 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1334 count = min(m->m_len - off, len);
1335 rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1336 if (rval)
1337 return (rval);
1338 len -= count;
1339 off = 0;
1340 m = m->m_next;
1341 }
1342 return (0);
1343}
1344
1345/*
1346 * Return a pointer to mbuf/offset of location in mbuf chain.
1347 */
1348struct mbuf *
1349m_getptr(struct mbuf *m, int loc, int *off)
1350{
1351
1352 while (loc >= 0) {
1353 /* Normal end of search. */
1354 if (m->m_len > loc) {
1355 *off = loc;
1356 return (m);
1357 } else {
1358 loc -= m->m_len;
1359 if (m->m_next == NULL) {
1360 if (loc == 0) {
1361 /* Point at the end of valid data. */
1362 *off = m->m_len;
1363 return (m);
1364 }
1365 return (NULL);
1366 }
1367 m = m->m_next;
1368 }
1369 }
1370 return (NULL);
1371}
1372
1373void
1374m_print(const struct mbuf *m, int maxlen)
1375{
1376 int len;
1377 int pdata;
1378 const struct mbuf *m2;
1379
1380 if (m->m_flags & M_PKTHDR)
1381 len = m->m_pkthdr.len;
1382 else
1383 len = -1;
1384 m2 = m;
1385 while (m2 != NULL && (len == -1 || len)) {
1386 pdata = m2->m_len;
1387 if (maxlen != -1 && pdata > maxlen)
1388 pdata = maxlen;
1389 printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len,
1390 m2->m_next, m2->m_flags, "\20\20freelist\17skipfw"
1391 "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly"
1392 "\3eor\2pkthdr\1ext", pdata ? "" : "\n");
1393 if (pdata)
1394 printf(", %*D\n", m2->m_len, (u_char *)m2->m_data, "-");
1395 if (len != -1)
1396 len -= m2->m_len;
1397 m2 = m2->m_next;
1398 }
1399 if (len > 0)
1400 printf("%d bytes unaccounted for.\n", len);
1401 return;
1402}
1403
1404u_int
1405m_fixhdr(struct mbuf *m0)
1406{
1407 u_int len;
1408
1409 len = m_length(m0, NULL);
1410 m0->m_pkthdr.len = len;
1411 return (len);
1412}
1413
1414u_int
1415m_length(struct mbuf *m0, struct mbuf **last)
1416{
1417 struct mbuf *m;
1418 u_int len;
1419
1420 len = 0;
1421 for (m = m0; m != NULL; m = m->m_next) {
1422 len += m->m_len;
1423 if (m->m_next == NULL)
1424 break;
1425 }
1426 if (last != NULL)
1427 *last = m;
1428 return (len);
1429}
1430
1431/*
1432 * Defragment a mbuf chain, returning the shortest possible
1433 * chain of mbufs and clusters. If allocation fails and
1434 * this cannot be completed, NULL will be returned, but
1435 * the passed in chain will be unchanged. Upon success,
1436 * the original chain will be freed, and the new chain
1437 * will be returned.
1438 *
1439 * If a non-packet header is passed in, the original
1440 * mbuf (chain?) will be returned unharmed.
1441 */
1442struct mbuf *
1443m_defrag(struct mbuf *m0, int how)
1444{
1445 struct mbuf *m_new = NULL, *m_final = NULL;
1446 int progress = 0, length;
1447
1448 MBUF_CHECKSLEEP(how);
1449 if (!(m0->m_flags & M_PKTHDR))
1450 return (m0);
1451
1452 m_fixhdr(m0); /* Needed sanity check */
1453
1454#ifdef MBUF_STRESS_TEST
1455 if (m_defragrandomfailures) {
1456 int temp = arc4random() & 0xff;
1457 if (temp == 0xba)
1458 goto nospace;
1459 }
1460#endif
1461
1462 if (m0->m_pkthdr.len > MHLEN)
1463 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1464 else
1465 m_final = m_gethdr(how, MT_DATA);
1466
1467 if (m_final == NULL)
1468 goto nospace;
1469
1470 if (m_dup_pkthdr(m_final, m0, how) == 0)
1471 goto nospace;
1472
1473 m_new = m_final;
1474
1475 while (progress < m0->m_pkthdr.len) {
1476 length = m0->m_pkthdr.len - progress;
1477 if (length > MCLBYTES)
1478 length = MCLBYTES;
1479
1480 if (m_new == NULL) {
1481 if (length > MLEN)
1482 m_new = m_getcl(how, MT_DATA, 0);
1483 else
1484 m_new = m_get(how, MT_DATA);
1485 if (m_new == NULL)
1486 goto nospace;
1487 }
1488
1489 m_copydata(m0, progress, length, mtod(m_new, caddr_t));
1490 progress += length;
1491 m_new->m_len = length;
1492 if (m_new != m_final)
1493 m_cat(m_final, m_new);
1494 m_new = NULL;
1495 }
1496#ifdef MBUF_STRESS_TEST
1497 if (m0->m_next == NULL)
1498 m_defraguseless++;
1499#endif
1500 m_freem(m0);
1501 m0 = m_final;
1502#ifdef MBUF_STRESS_TEST
1503 m_defragpackets++;
1504 m_defragbytes += m0->m_pkthdr.len;
1505#endif
1506 return (m0);
1507nospace:
1508#ifdef MBUF_STRESS_TEST
1509 m_defragfailure++;
1510#endif
1511 if (m_final)
1512 m_freem(m_final);
1513 return (NULL);
1514}
1515
1516#ifdef MBUF_STRESS_TEST
1517
1518/*
1519 * Fragment an mbuf chain. There's no reason you'd ever want to do
1520 * this in normal usage, but it's great for stress testing various
1521 * mbuf consumers.
1522 *
1523 * If fragmentation is not possible, the original chain will be
1524 * returned.
1525 *
1526 * Possible length values:
1527 * 0 no fragmentation will occur
1528 * > 0 each fragment will be of the specified length
1529 * -1 each fragment will be the same random value in length
1530 * -2 each fragment's length will be entirely random
1531 * (Random values range from 1 to 256)
1532 */
1533struct mbuf *
1534m_fragment(struct mbuf *m0, int how, int length)
1535{
1536 struct mbuf *m_new = NULL, *m_final = NULL;
1537 int progress = 0;
1538
1539 if (!(m0->m_flags & M_PKTHDR))
1540 return (m0);
1541
1542 if ((length == 0) || (length < -2))
1543 return (m0);
1544
1545 m_fixhdr(m0); /* Needed sanity check */
1546
1547 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1548
1549 if (m_final == NULL)
1550 goto nospace;
1551
1552 if (m_dup_pkthdr(m_final, m0, how) == 0)
1553 goto nospace;
1554
1555 m_new = m_final;
1556
1557 if (length == -1)
1558 length = 1 + (arc4random() & 255);
1559
1560 while (progress < m0->m_pkthdr.len) {
1561 int fraglen;
1562
1563 if (length > 0)
1564 fraglen = length;
1565 else
1566 fraglen = 1 + (arc4random() & 255);
1567 if (fraglen > m0->m_pkthdr.len - progress)
1568 fraglen = m0->m_pkthdr.len - progress;
1569
1570 if (fraglen > MCLBYTES)
1571 fraglen = MCLBYTES;
1572
1573 if (m_new == NULL) {
1574 m_new = m_getcl(how, MT_DATA, 0);
1575 if (m_new == NULL)
1576 goto nospace;
1577 }
1578
1579 m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t));
1580 progress += fraglen;
1581 m_new->m_len = fraglen;
1582 if (m_new != m_final)
1583 m_cat(m_final, m_new);
1584 m_new = NULL;
1585 }
1586 m_freem(m0);
1587 m0 = m_final;
1588 return (m0);
1589nospace:
1590 if (m_final)
1591 m_freem(m_final);
1592 /* Return the original chain on failure */
1593 return (m0);
1594}
1595
1596#endif
1597
1598struct mbuf *
1599m_uiotombuf(struct uio *uio, int how, int len, int align)
1600{
1601 struct mbuf *m_new = NULL, *m_final = NULL;
1602 int progress = 0, error = 0, length, total;
1603
1604 if (len > 0)
1605 total = min(uio->uio_resid, len);
1606 else
1607 total = uio->uio_resid;
1608 if (align >= MHLEN)
1609 goto nospace;
1610 if (total + align > MHLEN)
1611 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1612 else
1613 m_final = m_gethdr(how, MT_DATA);
1614 if (m_final == NULL)
1615 goto nospace;
1616 m_final->m_data += align;
1617 m_new = m_final;
1618 while (progress < total) {
1619 length = total - progress;
1620 if (length > MCLBYTES)
1621 length = MCLBYTES;
1622 if (m_new == NULL) {
1623 if (length > MLEN)
1624 m_new = m_getcl(how, MT_DATA, 0);
1625 else
1626 m_new = m_get(how, MT_DATA);
1627 if (m_new == NULL)
1628 goto nospace;
1629 }
1630 error = uiomove(mtod(m_new, void *), length, uio);
1631 if (error)
1632 goto nospace;
1633 progress += length;
1634 m_new->m_len = length;
1635 if (m_new != m_final)
1636 m_cat(m_final, m_new);
1637 m_new = NULL;
1638 }
1639 m_fixhdr(m_final);
1640 return (m_final);
1641nospace:
1642 if (m_new)
1643 m_free(m_new);
1644 if (m_final)
1645 m_freem(m_final);
1646 return (NULL);
1647}
1648
1649/*
1650 * Set the m_data pointer of a newly-allocated mbuf
1651 * to place an object of the specified size at the
1652 * end of the mbuf, longword aligned.
1653 */
1654void
1655m_align(struct mbuf *m, int len)
1656{
1657 int adjust;
1658
1659 if (m->m_flags & M_EXT)
1660 adjust = m->m_ext.ext_size - len;
1661 else if (m->m_flags & M_PKTHDR)
1662 adjust = MHLEN - len;
1663 else
1664 adjust = MLEN - len;
1665 m->m_data += adjust &~ (sizeof(long)-1);
1666}
2 * Copyright (c) 1982, 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/kern/uipc_mbuf.c 149648 2005-08-30 21:31:42Z andre $");
34
35#include "opt_mac.h"
36#include "opt_param.h"
37#include "opt_mbuf_stress_test.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/limits.h>
43#include <sys/lock.h>
44#include <sys/mac.h>
45#include <sys/malloc.h>
46#include <sys/mbuf.h>
47#include <sys/sysctl.h>
48#include <sys/domain.h>
49#include <sys/protosw.h>
50#include <sys/uio.h>
51
52int max_linkhdr;
53int max_protohdr;
54int max_hdr;
55int max_datalen;
56#ifdef MBUF_STRESS_TEST
57int m_defragpackets;
58int m_defragbytes;
59int m_defraguseless;
60int m_defragfailure;
61int m_defragrandomfailures;
62#endif
63
64/*
65 * sysctl(8) exported objects
66 */
67SYSCTL_DECL(_kern_ipc);
68SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
69 &max_linkhdr, 0, "");
70SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
71 &max_protohdr, 0, "");
72SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
73SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
74 &max_datalen, 0, "");
75#ifdef MBUF_STRESS_TEST
76SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
77 &m_defragpackets, 0, "");
78SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
79 &m_defragbytes, 0, "");
80SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
81 &m_defraguseless, 0, "");
82SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
83 &m_defragfailure, 0, "");
84SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
85 &m_defragrandomfailures, 0, "");
86#endif
87
88/*
89 * Malloc-type for external ext_buf ref counts.
90 */
91static MALLOC_DEFINE(M_MBUF, "mbextcnt", "mbuf external ref counts");
92
93/*
94 * Allocate a given length worth of mbufs and/or clusters (whatever fits
95 * best) and return a pointer to the top of the allocated chain. If an
96 * existing mbuf chain is provided, then we will append the new chain
97 * to the existing one but still return the top of the newly allocated
98 * chain.
99 */
100struct mbuf *
101m_getm(struct mbuf *m, int len, int how, short type)
102{
103 struct mbuf *mb, *top, *cur, *mtail;
104 int num, rem;
105 int i;
106
107 KASSERT(len >= 0, ("m_getm(): len is < 0"));
108
109 /* If m != NULL, we will append to the end of that chain. */
110 if (m != NULL)
111 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next);
112 else
113 mtail = NULL;
114
115 /*
116 * Calculate how many mbufs+clusters ("packets") we need and how much
117 * leftover there is after that and allocate the first mbuf+cluster
118 * if required.
119 */
120 num = len / MCLBYTES;
121 rem = len % MCLBYTES;
122 top = cur = NULL;
123 if (num > 0) {
124 if ((top = cur = m_getcl(how, type, 0)) == NULL)
125 goto failed;
126 top->m_len = 0;
127 }
128 num--;
129
130 for (i = 0; i < num; i++) {
131 mb = m_getcl(how, type, 0);
132 if (mb == NULL)
133 goto failed;
134 mb->m_len = 0;
135 cur = (cur->m_next = mb);
136 }
137 if (rem > 0) {
138 mb = (rem > MINCLSIZE) ?
139 m_getcl(how, type, 0) : m_get(how, type);
140 if (mb == NULL)
141 goto failed;
142 mb->m_len = 0;
143 if (cur == NULL)
144 top = mb;
145 else
146 cur->m_next = mb;
147 }
148
149 if (mtail != NULL)
150 mtail->m_next = top;
151 return top;
152failed:
153 if (top != NULL)
154 m_freem(top);
155 return NULL;
156}
157
158/*
159 * Free an entire chain of mbufs and associated external buffers, if
160 * applicable.
161 */
162void
163m_freem(struct mbuf *mb)
164{
165
166 while (mb != NULL)
167 mb = m_free(mb);
168}
169
170/*-
171 * Configure a provided mbuf to refer to the provided external storage
172 * buffer and setup a reference count for said buffer. If the setting
173 * up of the reference count fails, the M_EXT bit will not be set. If
174 * successfull, the M_EXT bit is set in the mbuf's flags.
175 *
176 * Arguments:
177 * mb The existing mbuf to which to attach the provided buffer.
178 * buf The address of the provided external storage buffer.
179 * size The size of the provided buffer.
180 * freef A pointer to a routine that is responsible for freeing the
181 * provided external storage buffer.
182 * args A pointer to an argument structure (of any type) to be passed
183 * to the provided freef routine (may be NULL).
184 * flags Any other flags to be passed to the provided mbuf.
185 * type The type that the external storage buffer should be
186 * labeled with.
187 *
188 * Returns:
189 * Nothing.
190 */
191void
192m_extadd(struct mbuf *mb, caddr_t buf, u_int size,
193 void (*freef)(void *, void *), void *args, int flags, int type)
194{
195 u_int *ref_cnt = NULL;
196
197 /* XXX Shouldn't be adding EXT_CLUSTER with this API */
198 if (type == EXT_CLUSTER)
199 ref_cnt = (u_int *)uma_find_refcnt(zone_clust,
200 mb->m_ext.ext_buf);
201 else if (type == EXT_EXTREF)
202 ref_cnt = __DEVOLATILE(u_int *, mb->m_ext.ref_cnt);
203 mb->m_ext.ref_cnt = (ref_cnt == NULL) ?
204 malloc(sizeof(u_int), M_MBUF, M_NOWAIT) : (u_int *)ref_cnt;
205 if (mb->m_ext.ref_cnt != NULL) {
206 *(mb->m_ext.ref_cnt) = 1;
207 mb->m_flags |= (M_EXT | flags);
208 mb->m_ext.ext_buf = buf;
209 mb->m_data = mb->m_ext.ext_buf;
210 mb->m_ext.ext_size = size;
211 mb->m_ext.ext_free = freef;
212 mb->m_ext.ext_args = args;
213 mb->m_ext.ext_type = type;
214 }
215}
216
217/*
218 * Non-directly-exported function to clean up after mbufs with M_EXT
219 * storage attached to them if the reference count hits 0.
220 */
221void
222mb_free_ext(struct mbuf *m)
223{
224 u_int cnt;
225 int dofree;
226
227 /* Account for lazy ref count assign. */
228 if (m->m_ext.ref_cnt == NULL)
229 dofree = 1;
230 else
231 dofree = 0;
232
233 /*
234 * This is tricky. We need to make sure to decrement the
235 * refcount in a safe way but to also clean up if we're the
236 * last reference. This method seems to do it without race.
237 */
238 while (dofree == 0) {
239 cnt = *(m->m_ext.ref_cnt);
240 if (atomic_cmpset_int(m->m_ext.ref_cnt, cnt, cnt - 1)) {
241 if (cnt == 1)
242 dofree = 1;
243 break;
244 }
245 }
246
247 if (dofree) {
248 /*
249 * Do the free, should be safe.
250 */
251 if (m->m_ext.ext_type == EXT_PACKET) {
252 uma_zfree(zone_pack, m);
253 return;
254 } else if (m->m_ext.ext_type == EXT_CLUSTER) {
255 uma_zfree(zone_clust, m->m_ext.ext_buf);
256 m->m_ext.ext_buf = NULL;
257 } else {
258 (*(m->m_ext.ext_free))(m->m_ext.ext_buf,
259 m->m_ext.ext_args);
260 if (m->m_ext.ext_type != EXT_EXTREF) {
261 if (m->m_ext.ref_cnt != NULL)
262 free(__DEVOLATILE(u_int *,
263 m->m_ext.ref_cnt), M_MBUF);
264 m->m_ext.ref_cnt = NULL;
265 }
266 m->m_ext.ext_buf = NULL;
267 }
268 }
269 uma_zfree(zone_mbuf, m);
270}
271
272/*
273 * Clean up mbuf (chain) from any tags and packet headers.
274 * If "all" is set then the first mbuf in the chain will be
275 * cleaned too.
276 */
277void
278m_demote(struct mbuf *m0, int all)
279{
280 struct mbuf *m;
281
282 for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) {
283 if (m->m_flags & M_PKTHDR) {
284 m_tag_delete_chain(m, NULL);
285 m->m_flags &= ~M_PKTHDR;
286 bzero(&m->m_pkthdr, sizeof(struct pkthdr));
287 }
288 if (m->m_type == MT_HEADER)
289 m->m_type = MT_DATA;
290 if (m != m0 && m->m_nextpkt != NULL)
291 m->m_nextpkt = NULL;
292 m->m_flags = m->m_flags & (M_EXT|M_EOR|M_RDONLY|M_FREELIST);
293 }
294}
295
296/*
297 * Sanity checks on mbuf (chain) for use in KASSERT() and general
298 * debugging.
299 * Returns 0 or panics when bad and 1 on all tests passed.
300 * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they
301 * blow up later.
302 */
303int
304m_sanity(struct mbuf *m0, int sanitize)
305{
306 struct mbuf *m;
307 caddr_t a, b;
308 int pktlen = 0;
309
310#define M_SANITY_ACTION(s) return (0)
311/* #define M_SANITY_ACTION(s) panic("mbuf %p: " s, m) */
312
313 for (m = m0; m != NULL; m = m->m_next) {
314 /*
315 * Basic pointer checks. If any of these fails then some
316 * unrelated kernel memory before or after us is trashed.
317 * No way to recover from that.
318 */
319 a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf :
320 ((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) :
321 (caddr_t)(&m->m_dat)) );
322 b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size :
323 ((m->m_flags & M_PKTHDR) ? MHLEN : MLEN)));
324 if ((caddr_t)m->m_data < a)
325 M_SANITY_ACTION("m_data outside mbuf data range left");
326 if ((caddr_t)m->m_data > b)
327 M_SANITY_ACTION("m_data outside mbuf data range right");
328 if ((caddr_t)m->m_data + m->m_len > b)
329 M_SANITY_ACTION("m_data + m_len exeeds mbuf space");
330 if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.header) {
331 if ((caddr_t)m->m_pkthdr.header < a ||
332 (caddr_t)m->m_pkthdr.header > b)
333 M_SANITY_ACTION("m_pkthdr.header outside mbuf data range");
334 }
335
336 /* m->m_nextpkt may only be set on first mbuf in chain. */
337 if (m != m0 && m->m_nextpkt != NULL) {
338 if (sanitize) {
339 m_freem(m->m_nextpkt);
340 m->m_nextpkt = (struct mbuf *)0xDEADC0DE;
341 } else
342 M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf");
343 }
344
345 /* correct type correlations. */
346 if (m->m_type == MT_HEADER && !(m->m_flags & M_PKTHDR)) {
347 if (sanitize)
348 m->m_type = MT_DATA;
349 else
350 M_SANITY_ACTION("MT_HEADER set but not M_PKTHDR");
351 }
352
353 /* packet length (not mbuf length!) calculation */
354 if (m0->m_flags & M_PKTHDR)
355 pktlen += m->m_len;
356
357 /* m_tags may only be attached to first mbuf in chain. */
358 if (m != m0 && m->m_flags & M_PKTHDR &&
359 !SLIST_EMPTY(&m->m_pkthdr.tags)) {
360 if (sanitize) {
361 m_tag_delete_chain(m, NULL);
362 /* put in 0xDEADC0DE perhaps? */
363 } else
364 M_SANITY_ACTION("m_tags on in-chain mbuf");
365 }
366
367 /* M_PKTHDR may only be set on first mbuf in chain */
368 if (m != m0 && m->m_flags & M_PKTHDR) {
369 if (sanitize) {
370 bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
371 m->m_flags &= ~M_PKTHDR;
372 /* put in 0xDEADCODE and leave hdr flag in */
373 } else
374 M_SANITY_ACTION("M_PKTHDR on in-chain mbuf");
375 }
376 }
377 m = m0;
378 if (pktlen && pktlen != m->m_pkthdr.len) {
379 if (sanitize)
380 m->m_pkthdr.len = 0;
381 else
382 M_SANITY_ACTION("m_pkthdr.len != mbuf chain length");
383 }
384 return 1;
385
386#undef M_SANITY_ACTION
387}
388
389
390/*
391 * "Move" mbuf pkthdr from "from" to "to".
392 * "from" must have M_PKTHDR set, and "to" must be empty.
393 */
394void
395m_move_pkthdr(struct mbuf *to, struct mbuf *from)
396{
397
398#if 0
399 /* see below for why these are not enabled */
400 M_ASSERTPKTHDR(to);
401 /* Note: with MAC, this may not be a good assertion. */
402 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
403 ("m_move_pkthdr: to has tags"));
404#endif
405#ifdef MAC
406 /*
407 * XXXMAC: It could be this should also occur for non-MAC?
408 */
409 if (to->m_flags & M_PKTHDR)
410 m_tag_delete_chain(to, NULL);
411#endif
412 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
413 if ((to->m_flags & M_EXT) == 0)
414 to->m_data = to->m_pktdat;
415 to->m_pkthdr = from->m_pkthdr; /* especially tags */
416 SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */
417 from->m_flags &= ~M_PKTHDR;
418}
419
420/*
421 * Duplicate "from"'s mbuf pkthdr in "to".
422 * "from" must have M_PKTHDR set, and "to" must be empty.
423 * In particular, this does a deep copy of the packet tags.
424 */
425int
426m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
427{
428
429#if 0
430 /*
431 * The mbuf allocator only initializes the pkthdr
432 * when the mbuf is allocated with MGETHDR. Many users
433 * (e.g. m_copy*, m_prepend) use MGET and then
434 * smash the pkthdr as needed causing these
435 * assertions to trip. For now just disable them.
436 */
437 M_ASSERTPKTHDR(to);
438 /* Note: with MAC, this may not be a good assertion. */
439 KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
440#endif
441 MBUF_CHECKSLEEP(how);
442#ifdef MAC
443 if (to->m_flags & M_PKTHDR)
444 m_tag_delete_chain(to, NULL);
445#endif
446 to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
447 if ((to->m_flags & M_EXT) == 0)
448 to->m_data = to->m_pktdat;
449 to->m_pkthdr = from->m_pkthdr;
450 SLIST_INIT(&to->m_pkthdr.tags);
451 return (m_tag_copy_chain(to, from, MBTOM(how)));
452}
453
454/*
455 * Lesser-used path for M_PREPEND:
456 * allocate new mbuf to prepend to chain,
457 * copy junk along.
458 */
459struct mbuf *
460m_prepend(struct mbuf *m, int len, int how)
461{
462 struct mbuf *mn;
463
464 if (m->m_flags & M_PKTHDR)
465 MGETHDR(mn, how, m->m_type);
466 else
467 MGET(mn, how, m->m_type);
468 if (mn == NULL) {
469 m_freem(m);
470 return (NULL);
471 }
472 if (m->m_flags & M_PKTHDR)
473 M_MOVE_PKTHDR(mn, m);
474 mn->m_next = m;
475 m = mn;
476 if (len < MHLEN)
477 MH_ALIGN(m, len);
478 m->m_len = len;
479 return (m);
480}
481
482/*
483 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
484 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
485 * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
486 * Note that the copy is read-only, because clusters are not copied,
487 * only their reference counts are incremented.
488 */
489struct mbuf *
490m_copym(struct mbuf *m, int off0, int len, int wait)
491{
492 struct mbuf *n, **np;
493 int off = off0;
494 struct mbuf *top;
495 int copyhdr = 0;
496
497 KASSERT(off >= 0, ("m_copym, negative off %d", off));
498 KASSERT(len >= 0, ("m_copym, negative len %d", len));
499 MBUF_CHECKSLEEP(wait);
500 if (off == 0 && m->m_flags & M_PKTHDR)
501 copyhdr = 1;
502 while (off > 0) {
503 KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
504 if (off < m->m_len)
505 break;
506 off -= m->m_len;
507 m = m->m_next;
508 }
509 np = ⊤
510 top = 0;
511 while (len > 0) {
512 if (m == NULL) {
513 KASSERT(len == M_COPYALL,
514 ("m_copym, length > size of mbuf chain"));
515 break;
516 }
517 if (copyhdr)
518 MGETHDR(n, wait, m->m_type);
519 else
520 MGET(n, wait, m->m_type);
521 *np = n;
522 if (n == NULL)
523 goto nospace;
524 if (copyhdr) {
525 if (!m_dup_pkthdr(n, m, wait))
526 goto nospace;
527 if (len == M_COPYALL)
528 n->m_pkthdr.len -= off0;
529 else
530 n->m_pkthdr.len = len;
531 copyhdr = 0;
532 }
533 n->m_len = min(len, m->m_len - off);
534 if (m->m_flags & M_EXT) {
535 n->m_data = m->m_data + off;
536 n->m_ext = m->m_ext;
537 n->m_flags |= M_EXT;
538 MEXT_ADD_REF(m);
539 n->m_ext.ref_cnt = m->m_ext.ref_cnt;
540 } else
541 bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
542 (u_int)n->m_len);
543 if (len != M_COPYALL)
544 len -= n->m_len;
545 off = 0;
546 m = m->m_next;
547 np = &n->m_next;
548 }
549 if (top == NULL)
550 mbstat.m_mcfail++; /* XXX: No consistency. */
551
552 return (top);
553nospace:
554 m_freem(top);
555 mbstat.m_mcfail++; /* XXX: No consistency. */
556 return (NULL);
557}
558
559/*
560 * Returns mbuf chain with new head for the prepending case.
561 * Copies from mbuf (chain) n from off for len to mbuf (chain) m
562 * either prepending or appending the data.
563 * The resulting mbuf (chain) m is fully writeable.
564 * m is destination (is made writeable)
565 * n is source, off is offset in source, len is len from offset
566 * dir, 0 append, 1 prepend
567 * how, wait or nowait
568 */
569
570static int
571m_bcopyxxx(void *s, void *t, u_int len)
572{
573 bcopy(s, t, (size_t)len);
574 return 0;
575}
576
577struct mbuf *
578m_copymdata(struct mbuf *m, struct mbuf *n, int off, int len,
579 int prep, int how)
580{
581 struct mbuf *mm, *x, *z;
582 caddr_t p;
583 int i, mlen, nlen = 0;
584 caddr_t buf[MLEN];
585
586 KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source"));
587 KASSERT(off >= 0, ("m_copymdata, negative off %d", off));
588 KASSERT(len >= 0, ("m_copymdata, negative len %d", len));
589 KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep));
590
591 /* Make sure environment is sane. */
592 for (z = m; z != NULL; z = z->m_next) {
593 mlen += z->m_len;
594 if (!M_WRITABLE(z)) {
595 /* Make clusters writeable. */
596 if (z->m_flags & M_RDONLY)
597 return NULL; /* Can't handle ext ref. */
598 x = m_getcl(how, MT_DATA, 0);
599 if (!x)
600 return NULL;
601 bcopy(z->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size);
602 p = x->m_ext.ext_buf + (z->m_data - z->m_ext.ext_buf);
603 MEXT_REM_REF(z); /* XXX */
604 z->m_data = p;
605 x->m_flags &= ~M_EXT;
606 (void)m_free(x);
607 }
608 }
609 mm = prep ? m : z;
610 for (z = n; z != NULL; z = z->m_next)
611 nlen += z->m_len;
612 if (len == M_COPYALL)
613 len = nlen - off;
614 if (off + len > nlen || len < 1)
615 return NULL;
616
617 /*
618 * Append/prepend the data. Allocating mbufs as necessary.
619 */
620 /* Shortcut if enough free space in first/last mbuf. */
621 if (!prep && M_TRAILINGSPACE(mm) >= len) {
622 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) +
623 mm->m_len);
624 mm->m_len += len;
625 mm->m_pkthdr.len += len;
626 return m;
627 }
628 if (prep && M_LEADINGSPACE(mm) >= len) {
629 mm->m_data = mtod(mm, caddr_t) - len;
630 m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t));
631 mm->m_len += len;
632 mm->m_pkthdr.len += len;
633 return mm;
634 }
635
636 /* Expand first/last mbuf to cluster if possible. */
637 if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) {
638 bcopy(mm->m_data, &buf, mm->m_len);
639 m_clget(mm, how);
640 if (!(mm->m_flags & M_EXT))
641 return NULL;
642 bcopy(&buf, mm->m_ext.ext_buf, mm->m_len);
643 mm->m_data = mm->m_ext.ext_buf;
644 mm->m_pkthdr.header = NULL;
645 }
646 if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) {
647 bcopy(mm->m_data, &buf, mm->m_len);
648 m_clget(mm, how);
649 if (!(mm->m_flags & M_EXT))
650 return NULL;
651 bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf +
652 mm->m_ext.ext_size - mm->m_len, mm->m_len);
653 mm->m_data = (caddr_t)mm->m_ext.ext_buf +
654 mm->m_ext.ext_size - mm->m_len;
655 mm->m_pkthdr.header = NULL;
656 }
657
658 /* Append/prepend as many mbuf (clusters) as necessary to fit len. */
659 if (!prep && len > M_TRAILINGSPACE(mm)) {
660 if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA))
661 return NULL;
662 }
663 if (prep && len > M_LEADINGSPACE(mm)) {
664 if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA)))
665 return NULL;
666 i = 0;
667 for (x = z; x != NULL; x = x->m_next) {
668 i += x->m_flags & M_EXT ? x->m_ext.ext_size :
669 (x->m_flags & M_PKTHDR ? MHLEN : MLEN);
670 if (!x->m_next)
671 break;
672 }
673 z->m_data += i - len;
674 m_move_pkthdr(mm, z);
675 x->m_next = mm;
676 mm = z;
677 }
678
679 /* Seek to start position in source mbuf. Optimization for long chains. */
680 while (off > 0) {
681 if (off < n->m_len)
682 break;
683 off -= n->m_len;
684 n = n->m_next;
685 }
686
687 /* Copy data into target mbuf. */
688 z = mm;
689 while (len > 0) {
690 KASSERT(z != NULL, ("m_copymdata, falling off target edge"));
691 i = M_TRAILINGSPACE(z);
692 m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len);
693 z->m_len += i;
694 /* fixup pkthdr.len if necessary */
695 if ((prep ? mm : m)->m_flags & M_PKTHDR)
696 (prep ? mm : m)->m_pkthdr.len += i;
697 off += i;
698 len -= i;
699 z = z->m_next;
700 }
701 return (prep ? mm : m);
702}
703
704/*
705 * Copy an entire packet, including header (which must be present).
706 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
707 * Note that the copy is read-only, because clusters are not copied,
708 * only their reference counts are incremented.
709 * Preserve alignment of the first mbuf so if the creator has left
710 * some room at the beginning (e.g. for inserting protocol headers)
711 * the copies still have the room available.
712 */
713struct mbuf *
714m_copypacket(struct mbuf *m, int how)
715{
716 struct mbuf *top, *n, *o;
717
718 MBUF_CHECKSLEEP(how);
719 MGET(n, how, m->m_type);
720 top = n;
721 if (n == NULL)
722 goto nospace;
723
724 if (!m_dup_pkthdr(n, m, how))
725 goto nospace;
726 n->m_len = m->m_len;
727 if (m->m_flags & M_EXT) {
728 n->m_data = m->m_data;
729 n->m_ext = m->m_ext;
730 n->m_flags |= M_EXT;
731 MEXT_ADD_REF(m);
732 n->m_ext.ref_cnt = m->m_ext.ref_cnt;
733 } else {
734 n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
735 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
736 }
737
738 m = m->m_next;
739 while (m) {
740 MGET(o, how, m->m_type);
741 if (o == NULL)
742 goto nospace;
743
744 n->m_next = o;
745 n = n->m_next;
746
747 n->m_len = m->m_len;
748 if (m->m_flags & M_EXT) {
749 n->m_data = m->m_data;
750 n->m_ext = m->m_ext;
751 n->m_flags |= M_EXT;
752 MEXT_ADD_REF(m);
753 n->m_ext.ref_cnt = m->m_ext.ref_cnt;
754 } else {
755 bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
756 }
757
758 m = m->m_next;
759 }
760 return top;
761nospace:
762 m_freem(top);
763 mbstat.m_mcfail++; /* XXX: No consistency. */
764 return (NULL);
765}
766
767/*
768 * Copy data from an mbuf chain starting "off" bytes from the beginning,
769 * continuing for "len" bytes, into the indicated buffer.
770 */
771void
772m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
773{
774 u_int count;
775
776 KASSERT(off >= 0, ("m_copydata, negative off %d", off));
777 KASSERT(len >= 0, ("m_copydata, negative len %d", len));
778 while (off > 0) {
779 KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
780 if (off < m->m_len)
781 break;
782 off -= m->m_len;
783 m = m->m_next;
784 }
785 while (len > 0) {
786 KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
787 count = min(m->m_len - off, len);
788 bcopy(mtod(m, caddr_t) + off, cp, count);
789 len -= count;
790 cp += count;
791 off = 0;
792 m = m->m_next;
793 }
794}
795
796/*
797 * Copy a packet header mbuf chain into a completely new chain, including
798 * copying any mbuf clusters. Use this instead of m_copypacket() when
799 * you need a writable copy of an mbuf chain.
800 */
801struct mbuf *
802m_dup(struct mbuf *m, int how)
803{
804 struct mbuf **p, *top = NULL;
805 int remain, moff, nsize;
806
807 MBUF_CHECKSLEEP(how);
808 /* Sanity check */
809 if (m == NULL)
810 return (NULL);
811 M_ASSERTPKTHDR(m);
812
813 /* While there's more data, get a new mbuf, tack it on, and fill it */
814 remain = m->m_pkthdr.len;
815 moff = 0;
816 p = ⊤
817 while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */
818 struct mbuf *n;
819
820 /* Get the next new mbuf */
821 if (remain >= MINCLSIZE) {
822 n = m_getcl(how, m->m_type, 0);
823 nsize = MCLBYTES;
824 } else {
825 n = m_get(how, m->m_type);
826 nsize = MLEN;
827 }
828 if (n == NULL)
829 goto nospace;
830
831 if (top == NULL) { /* First one, must be PKTHDR */
832 if (!m_dup_pkthdr(n, m, how)) {
833 m_free(n);
834 goto nospace;
835 }
836 nsize = MHLEN;
837 }
838 n->m_len = 0;
839
840 /* Link it into the new chain */
841 *p = n;
842 p = &n->m_next;
843
844 /* Copy data from original mbuf(s) into new mbuf */
845 while (n->m_len < nsize && m != NULL) {
846 int chunk = min(nsize - n->m_len, m->m_len - moff);
847
848 bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
849 moff += chunk;
850 n->m_len += chunk;
851 remain -= chunk;
852 if (moff == m->m_len) {
853 m = m->m_next;
854 moff = 0;
855 }
856 }
857
858 /* Check correct total mbuf length */
859 KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
860 ("%s: bogus m_pkthdr.len", __func__));
861 }
862 return (top);
863
864nospace:
865 m_freem(top);
866 mbstat.m_mcfail++; /* XXX: No consistency. */
867 return (NULL);
868}
869
870/*
871 * Concatenate mbuf chain n to m.
872 * Both chains must be of the same type (e.g. MT_DATA).
873 * Any m_pkthdr is not updated.
874 */
875void
876m_cat(struct mbuf *m, struct mbuf *n)
877{
878 while (m->m_next)
879 m = m->m_next;
880 while (n) {
881 if (m->m_flags & M_EXT ||
882 m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
883 /* just join the two chains */
884 m->m_next = n;
885 return;
886 }
887 /* splat the data from one into the other */
888 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
889 (u_int)n->m_len);
890 m->m_len += n->m_len;
891 n = m_free(n);
892 }
893}
894
895void
896m_adj(struct mbuf *mp, int req_len)
897{
898 int len = req_len;
899 struct mbuf *m;
900 int count;
901
902 if ((m = mp) == NULL)
903 return;
904 if (len >= 0) {
905 /*
906 * Trim from head.
907 */
908 while (m != NULL && len > 0) {
909 if (m->m_len <= len) {
910 len -= m->m_len;
911 m->m_len = 0;
912 m = m->m_next;
913 } else {
914 m->m_len -= len;
915 m->m_data += len;
916 len = 0;
917 }
918 }
919 m = mp;
920 if (mp->m_flags & M_PKTHDR)
921 m->m_pkthdr.len -= (req_len - len);
922 } else {
923 /*
924 * Trim from tail. Scan the mbuf chain,
925 * calculating its length and finding the last mbuf.
926 * If the adjustment only affects this mbuf, then just
927 * adjust and return. Otherwise, rescan and truncate
928 * after the remaining size.
929 */
930 len = -len;
931 count = 0;
932 for (;;) {
933 count += m->m_len;
934 if (m->m_next == (struct mbuf *)0)
935 break;
936 m = m->m_next;
937 }
938 if (m->m_len >= len) {
939 m->m_len -= len;
940 if (mp->m_flags & M_PKTHDR)
941 mp->m_pkthdr.len -= len;
942 return;
943 }
944 count -= len;
945 if (count < 0)
946 count = 0;
947 /*
948 * Correct length for chain is "count".
949 * Find the mbuf with last data, adjust its length,
950 * and toss data from remaining mbufs on chain.
951 */
952 m = mp;
953 if (m->m_flags & M_PKTHDR)
954 m->m_pkthdr.len = count;
955 for (; m; m = m->m_next) {
956 if (m->m_len >= count) {
957 m->m_len = count;
958 if (m->m_next != NULL) {
959 m_freem(m->m_next);
960 m->m_next = NULL;
961 }
962 break;
963 }
964 count -= m->m_len;
965 }
966 }
967}
968
969/*
970 * Rearange an mbuf chain so that len bytes are contiguous
971 * and in the data area of an mbuf (so that mtod and dtom
972 * will work for a structure of size len). Returns the resulting
973 * mbuf chain on success, frees it and returns null on failure.
974 * If there is room, it will add up to max_protohdr-len extra bytes to the
975 * contiguous region in an attempt to avoid being called next time.
976 */
977struct mbuf *
978m_pullup(struct mbuf *n, int len)
979{
980 struct mbuf *m;
981 int count;
982 int space;
983
984 /*
985 * If first mbuf has no cluster, and has room for len bytes
986 * without shifting current data, pullup into it,
987 * otherwise allocate a new mbuf to prepend to the chain.
988 */
989 if ((n->m_flags & M_EXT) == 0 &&
990 n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
991 if (n->m_len >= len)
992 return (n);
993 m = n;
994 n = n->m_next;
995 len -= m->m_len;
996 } else {
997 if (len > MHLEN)
998 goto bad;
999 MGET(m, M_DONTWAIT, n->m_type);
1000 if (m == NULL)
1001 goto bad;
1002 m->m_len = 0;
1003 if (n->m_flags & M_PKTHDR)
1004 M_MOVE_PKTHDR(m, n);
1005 }
1006 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1007 do {
1008 count = min(min(max(len, max_protohdr), space), n->m_len);
1009 bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1010 (u_int)count);
1011 len -= count;
1012 m->m_len += count;
1013 n->m_len -= count;
1014 space -= count;
1015 if (n->m_len)
1016 n->m_data += count;
1017 else
1018 n = m_free(n);
1019 } while (len > 0 && n);
1020 if (len > 0) {
1021 (void) m_free(m);
1022 goto bad;
1023 }
1024 m->m_next = n;
1025 return (m);
1026bad:
1027 m_freem(n);
1028 mbstat.m_mpfail++; /* XXX: No consistency. */
1029 return (NULL);
1030}
1031
1032/*
1033 * Like m_pullup(), except a new mbuf is always allocated, and we allow
1034 * the amount of empty space before the data in the new mbuf to be specified
1035 * (in the event that the caller expects to prepend later).
1036 */
1037int MSFail;
1038
1039struct mbuf *
1040m_copyup(struct mbuf *n, int len, int dstoff)
1041{
1042 struct mbuf *m;
1043 int count, space;
1044
1045 if (len > (MHLEN - dstoff))
1046 goto bad;
1047 MGET(m, M_DONTWAIT, n->m_type);
1048 if (m == NULL)
1049 goto bad;
1050 m->m_len = 0;
1051 if (n->m_flags & M_PKTHDR)
1052 M_MOVE_PKTHDR(m, n);
1053 m->m_data += dstoff;
1054 space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1055 do {
1056 count = min(min(max(len, max_protohdr), space), n->m_len);
1057 memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
1058 (unsigned)count);
1059 len -= count;
1060 m->m_len += count;
1061 n->m_len -= count;
1062 space -= count;
1063 if (n->m_len)
1064 n->m_data += count;
1065 else
1066 n = m_free(n);
1067 } while (len > 0 && n);
1068 if (len > 0) {
1069 (void) m_free(m);
1070 goto bad;
1071 }
1072 m->m_next = n;
1073 return (m);
1074 bad:
1075 m_freem(n);
1076 MSFail++;
1077 return (NULL);
1078}
1079
1080/*
1081 * Partition an mbuf chain in two pieces, returning the tail --
1082 * all but the first len0 bytes. In case of failure, it returns NULL and
1083 * attempts to restore the chain to its original state.
1084 *
1085 * Note that the resulting mbufs might be read-only, because the new
1086 * mbuf can end up sharing an mbuf cluster with the original mbuf if
1087 * the "breaking point" happens to lie within a cluster mbuf. Use the
1088 * M_WRITABLE() macro to check for this case.
1089 */
1090struct mbuf *
1091m_split(struct mbuf *m0, int len0, int wait)
1092{
1093 struct mbuf *m, *n;
1094 u_int len = len0, remain;
1095
1096 MBUF_CHECKSLEEP(wait);
1097 for (m = m0; m && len > m->m_len; m = m->m_next)
1098 len -= m->m_len;
1099 if (m == NULL)
1100 return (NULL);
1101 remain = m->m_len - len;
1102 if (m0->m_flags & M_PKTHDR) {
1103 MGETHDR(n, wait, m0->m_type);
1104 if (n == NULL)
1105 return (NULL);
1106 n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
1107 n->m_pkthdr.len = m0->m_pkthdr.len - len0;
1108 m0->m_pkthdr.len = len0;
1109 if (m->m_flags & M_EXT)
1110 goto extpacket;
1111 if (remain > MHLEN) {
1112 /* m can't be the lead packet */
1113 MH_ALIGN(n, 0);
1114 n->m_next = m_split(m, len, wait);
1115 if (n->m_next == NULL) {
1116 (void) m_free(n);
1117 return (NULL);
1118 } else {
1119 n->m_len = 0;
1120 return (n);
1121 }
1122 } else
1123 MH_ALIGN(n, remain);
1124 } else if (remain == 0) {
1125 n = m->m_next;
1126 m->m_next = NULL;
1127 return (n);
1128 } else {
1129 MGET(n, wait, m->m_type);
1130 if (n == NULL)
1131 return (NULL);
1132 M_ALIGN(n, remain);
1133 }
1134extpacket:
1135 if (m->m_flags & M_EXT) {
1136 n->m_flags |= M_EXT;
1137 n->m_ext = m->m_ext;
1138 MEXT_ADD_REF(m);
1139 n->m_ext.ref_cnt = m->m_ext.ref_cnt;
1140 n->m_data = m->m_data + len;
1141 } else {
1142 bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
1143 }
1144 n->m_len = remain;
1145 m->m_len = len;
1146 n->m_next = m->m_next;
1147 m->m_next = NULL;
1148 return (n);
1149}
1150/*
1151 * Routine to copy from device local memory into mbufs.
1152 * Note that `off' argument is offset into first mbuf of target chain from
1153 * which to begin copying the data to.
1154 */
1155struct mbuf *
1156m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
1157 void (*copy)(char *from, caddr_t to, u_int len))
1158{
1159 struct mbuf *m;
1160 struct mbuf *top = NULL, **mp = ⊤
1161 int len;
1162
1163 if (off < 0 || off > MHLEN)
1164 return (NULL);
1165
1166 while (totlen > 0) {
1167 if (top == NULL) { /* First one, must be PKTHDR */
1168 if (totlen + off >= MINCLSIZE) {
1169 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1170 len = MCLBYTES;
1171 } else {
1172 m = m_gethdr(M_DONTWAIT, MT_DATA);
1173 len = MHLEN;
1174
1175 /* Place initial small packet/header at end of mbuf */
1176 if (m && totlen + off + max_linkhdr <= MLEN) {
1177 m->m_data += max_linkhdr;
1178 len -= max_linkhdr;
1179 }
1180 }
1181 if (m == NULL)
1182 return NULL;
1183 m->m_pkthdr.rcvif = ifp;
1184 m->m_pkthdr.len = totlen;
1185 } else {
1186 if (totlen + off >= MINCLSIZE) {
1187 m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1188 len = MCLBYTES;
1189 } else {
1190 m = m_get(M_DONTWAIT, MT_DATA);
1191 len = MLEN;
1192 }
1193 if (m == NULL) {
1194 m_freem(top);
1195 return NULL;
1196 }
1197 }
1198 if (off) {
1199 m->m_data += off;
1200 len -= off;
1201 off = 0;
1202 }
1203 m->m_len = len = min(totlen, len);
1204 if (copy)
1205 copy(buf, mtod(m, caddr_t), (u_int)len);
1206 else
1207 bcopy(buf, mtod(m, caddr_t), (u_int)len);
1208 buf += len;
1209 *mp = m;
1210 mp = &m->m_next;
1211 totlen -= len;
1212 }
1213 return (top);
1214}
1215
1216/*
1217 * Copy data from a buffer back into the indicated mbuf chain,
1218 * starting "off" bytes from the beginning, extending the mbuf
1219 * chain if necessary.
1220 */
1221void
1222m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp)
1223{
1224 int mlen;
1225 struct mbuf *m = m0, *n;
1226 int totlen = 0;
1227
1228 if (m0 == NULL)
1229 return;
1230 while (off > (mlen = m->m_len)) {
1231 off -= mlen;
1232 totlen += mlen;
1233 if (m->m_next == NULL) {
1234 n = m_get(M_DONTWAIT, m->m_type);
1235 if (n == NULL)
1236 goto out;
1237 bzero(mtod(n, caddr_t), MLEN);
1238 n->m_len = min(MLEN, len + off);
1239 m->m_next = n;
1240 }
1241 m = m->m_next;
1242 }
1243 while (len > 0) {
1244 mlen = min (m->m_len - off, len);
1245 bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
1246 cp += mlen;
1247 len -= mlen;
1248 mlen += off;
1249 off = 0;
1250 totlen += mlen;
1251 if (len == 0)
1252 break;
1253 if (m->m_next == NULL) {
1254 n = m_get(M_DONTWAIT, m->m_type);
1255 if (n == NULL)
1256 break;
1257 n->m_len = min(MLEN, len);
1258 m->m_next = n;
1259 }
1260 m = m->m_next;
1261 }
1262out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1263 m->m_pkthdr.len = totlen;
1264}
1265
1266/*
1267 * Append the specified data to the indicated mbuf chain,
1268 * Extend the mbuf chain if the new data does not fit in
1269 * existing space.
1270 *
1271 * Return 1 if able to complete the job; otherwise 0.
1272 */
1273int
1274m_append(struct mbuf *m0, int len, c_caddr_t cp)
1275{
1276 struct mbuf *m, *n;
1277 int remainder, space;
1278
1279 for (m = m0; m->m_next != NULL; m = m->m_next)
1280 ;
1281 remainder = len;
1282 space = M_TRAILINGSPACE(m);
1283 if (space > 0) {
1284 /*
1285 * Copy into available space.
1286 */
1287 if (space > remainder)
1288 space = remainder;
1289 bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1290 m->m_len += space;
1291 cp += space, remainder -= space;
1292 }
1293 while (remainder > 0) {
1294 /*
1295 * Allocate a new mbuf; could check space
1296 * and allocate a cluster instead.
1297 */
1298 n = m_get(M_DONTWAIT, m->m_type);
1299 if (n == NULL)
1300 break;
1301 n->m_len = min(MLEN, remainder);
1302 bcopy(cp, mtod(n, caddr_t), n->m_len);
1303 cp += n->m_len, remainder -= n->m_len;
1304 m->m_next = n;
1305 m = n;
1306 }
1307 if (m0->m_flags & M_PKTHDR)
1308 m0->m_pkthdr.len += len - remainder;
1309 return (remainder == 0);
1310}
1311
1312/*
1313 * Apply function f to the data in an mbuf chain starting "off" bytes from
1314 * the beginning, continuing for "len" bytes.
1315 */
1316int
1317m_apply(struct mbuf *m, int off, int len,
1318 int (*f)(void *, void *, u_int), void *arg)
1319{
1320 u_int count;
1321 int rval;
1322
1323 KASSERT(off >= 0, ("m_apply, negative off %d", off));
1324 KASSERT(len >= 0, ("m_apply, negative len %d", len));
1325 while (off > 0) {
1326 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1327 if (off < m->m_len)
1328 break;
1329 off -= m->m_len;
1330 m = m->m_next;
1331 }
1332 while (len > 0) {
1333 KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1334 count = min(m->m_len - off, len);
1335 rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1336 if (rval)
1337 return (rval);
1338 len -= count;
1339 off = 0;
1340 m = m->m_next;
1341 }
1342 return (0);
1343}
1344
1345/*
1346 * Return a pointer to mbuf/offset of location in mbuf chain.
1347 */
1348struct mbuf *
1349m_getptr(struct mbuf *m, int loc, int *off)
1350{
1351
1352 while (loc >= 0) {
1353 /* Normal end of search. */
1354 if (m->m_len > loc) {
1355 *off = loc;
1356 return (m);
1357 } else {
1358 loc -= m->m_len;
1359 if (m->m_next == NULL) {
1360 if (loc == 0) {
1361 /* Point at the end of valid data. */
1362 *off = m->m_len;
1363 return (m);
1364 }
1365 return (NULL);
1366 }
1367 m = m->m_next;
1368 }
1369 }
1370 return (NULL);
1371}
1372
1373void
1374m_print(const struct mbuf *m, int maxlen)
1375{
1376 int len;
1377 int pdata;
1378 const struct mbuf *m2;
1379
1380 if (m->m_flags & M_PKTHDR)
1381 len = m->m_pkthdr.len;
1382 else
1383 len = -1;
1384 m2 = m;
1385 while (m2 != NULL && (len == -1 || len)) {
1386 pdata = m2->m_len;
1387 if (maxlen != -1 && pdata > maxlen)
1388 pdata = maxlen;
1389 printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len,
1390 m2->m_next, m2->m_flags, "\20\20freelist\17skipfw"
1391 "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly"
1392 "\3eor\2pkthdr\1ext", pdata ? "" : "\n");
1393 if (pdata)
1394 printf(", %*D\n", m2->m_len, (u_char *)m2->m_data, "-");
1395 if (len != -1)
1396 len -= m2->m_len;
1397 m2 = m2->m_next;
1398 }
1399 if (len > 0)
1400 printf("%d bytes unaccounted for.\n", len);
1401 return;
1402}
1403
1404u_int
1405m_fixhdr(struct mbuf *m0)
1406{
1407 u_int len;
1408
1409 len = m_length(m0, NULL);
1410 m0->m_pkthdr.len = len;
1411 return (len);
1412}
1413
1414u_int
1415m_length(struct mbuf *m0, struct mbuf **last)
1416{
1417 struct mbuf *m;
1418 u_int len;
1419
1420 len = 0;
1421 for (m = m0; m != NULL; m = m->m_next) {
1422 len += m->m_len;
1423 if (m->m_next == NULL)
1424 break;
1425 }
1426 if (last != NULL)
1427 *last = m;
1428 return (len);
1429}
1430
1431/*
1432 * Defragment a mbuf chain, returning the shortest possible
1433 * chain of mbufs and clusters. If allocation fails and
1434 * this cannot be completed, NULL will be returned, but
1435 * the passed in chain will be unchanged. Upon success,
1436 * the original chain will be freed, and the new chain
1437 * will be returned.
1438 *
1439 * If a non-packet header is passed in, the original
1440 * mbuf (chain?) will be returned unharmed.
1441 */
1442struct mbuf *
1443m_defrag(struct mbuf *m0, int how)
1444{
1445 struct mbuf *m_new = NULL, *m_final = NULL;
1446 int progress = 0, length;
1447
1448 MBUF_CHECKSLEEP(how);
1449 if (!(m0->m_flags & M_PKTHDR))
1450 return (m0);
1451
1452 m_fixhdr(m0); /* Needed sanity check */
1453
1454#ifdef MBUF_STRESS_TEST
1455 if (m_defragrandomfailures) {
1456 int temp = arc4random() & 0xff;
1457 if (temp == 0xba)
1458 goto nospace;
1459 }
1460#endif
1461
1462 if (m0->m_pkthdr.len > MHLEN)
1463 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1464 else
1465 m_final = m_gethdr(how, MT_DATA);
1466
1467 if (m_final == NULL)
1468 goto nospace;
1469
1470 if (m_dup_pkthdr(m_final, m0, how) == 0)
1471 goto nospace;
1472
1473 m_new = m_final;
1474
1475 while (progress < m0->m_pkthdr.len) {
1476 length = m0->m_pkthdr.len - progress;
1477 if (length > MCLBYTES)
1478 length = MCLBYTES;
1479
1480 if (m_new == NULL) {
1481 if (length > MLEN)
1482 m_new = m_getcl(how, MT_DATA, 0);
1483 else
1484 m_new = m_get(how, MT_DATA);
1485 if (m_new == NULL)
1486 goto nospace;
1487 }
1488
1489 m_copydata(m0, progress, length, mtod(m_new, caddr_t));
1490 progress += length;
1491 m_new->m_len = length;
1492 if (m_new != m_final)
1493 m_cat(m_final, m_new);
1494 m_new = NULL;
1495 }
1496#ifdef MBUF_STRESS_TEST
1497 if (m0->m_next == NULL)
1498 m_defraguseless++;
1499#endif
1500 m_freem(m0);
1501 m0 = m_final;
1502#ifdef MBUF_STRESS_TEST
1503 m_defragpackets++;
1504 m_defragbytes += m0->m_pkthdr.len;
1505#endif
1506 return (m0);
1507nospace:
1508#ifdef MBUF_STRESS_TEST
1509 m_defragfailure++;
1510#endif
1511 if (m_final)
1512 m_freem(m_final);
1513 return (NULL);
1514}
1515
1516#ifdef MBUF_STRESS_TEST
1517
1518/*
1519 * Fragment an mbuf chain. There's no reason you'd ever want to do
1520 * this in normal usage, but it's great for stress testing various
1521 * mbuf consumers.
1522 *
1523 * If fragmentation is not possible, the original chain will be
1524 * returned.
1525 *
1526 * Possible length values:
1527 * 0 no fragmentation will occur
1528 * > 0 each fragment will be of the specified length
1529 * -1 each fragment will be the same random value in length
1530 * -2 each fragment's length will be entirely random
1531 * (Random values range from 1 to 256)
1532 */
1533struct mbuf *
1534m_fragment(struct mbuf *m0, int how, int length)
1535{
1536 struct mbuf *m_new = NULL, *m_final = NULL;
1537 int progress = 0;
1538
1539 if (!(m0->m_flags & M_PKTHDR))
1540 return (m0);
1541
1542 if ((length == 0) || (length < -2))
1543 return (m0);
1544
1545 m_fixhdr(m0); /* Needed sanity check */
1546
1547 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1548
1549 if (m_final == NULL)
1550 goto nospace;
1551
1552 if (m_dup_pkthdr(m_final, m0, how) == 0)
1553 goto nospace;
1554
1555 m_new = m_final;
1556
1557 if (length == -1)
1558 length = 1 + (arc4random() & 255);
1559
1560 while (progress < m0->m_pkthdr.len) {
1561 int fraglen;
1562
1563 if (length > 0)
1564 fraglen = length;
1565 else
1566 fraglen = 1 + (arc4random() & 255);
1567 if (fraglen > m0->m_pkthdr.len - progress)
1568 fraglen = m0->m_pkthdr.len - progress;
1569
1570 if (fraglen > MCLBYTES)
1571 fraglen = MCLBYTES;
1572
1573 if (m_new == NULL) {
1574 m_new = m_getcl(how, MT_DATA, 0);
1575 if (m_new == NULL)
1576 goto nospace;
1577 }
1578
1579 m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t));
1580 progress += fraglen;
1581 m_new->m_len = fraglen;
1582 if (m_new != m_final)
1583 m_cat(m_final, m_new);
1584 m_new = NULL;
1585 }
1586 m_freem(m0);
1587 m0 = m_final;
1588 return (m0);
1589nospace:
1590 if (m_final)
1591 m_freem(m_final);
1592 /* Return the original chain on failure */
1593 return (m0);
1594}
1595
1596#endif
1597
1598struct mbuf *
1599m_uiotombuf(struct uio *uio, int how, int len, int align)
1600{
1601 struct mbuf *m_new = NULL, *m_final = NULL;
1602 int progress = 0, error = 0, length, total;
1603
1604 if (len > 0)
1605 total = min(uio->uio_resid, len);
1606 else
1607 total = uio->uio_resid;
1608 if (align >= MHLEN)
1609 goto nospace;
1610 if (total + align > MHLEN)
1611 m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1612 else
1613 m_final = m_gethdr(how, MT_DATA);
1614 if (m_final == NULL)
1615 goto nospace;
1616 m_final->m_data += align;
1617 m_new = m_final;
1618 while (progress < total) {
1619 length = total - progress;
1620 if (length > MCLBYTES)
1621 length = MCLBYTES;
1622 if (m_new == NULL) {
1623 if (length > MLEN)
1624 m_new = m_getcl(how, MT_DATA, 0);
1625 else
1626 m_new = m_get(how, MT_DATA);
1627 if (m_new == NULL)
1628 goto nospace;
1629 }
1630 error = uiomove(mtod(m_new, void *), length, uio);
1631 if (error)
1632 goto nospace;
1633 progress += length;
1634 m_new->m_len = length;
1635 if (m_new != m_final)
1636 m_cat(m_final, m_new);
1637 m_new = NULL;
1638 }
1639 m_fixhdr(m_final);
1640 return (m_final);
1641nospace:
1642 if (m_new)
1643 m_free(m_new);
1644 if (m_final)
1645 m_freem(m_final);
1646 return (NULL);
1647}
1648
1649/*
1650 * Set the m_data pointer of a newly-allocated mbuf
1651 * to place an object of the specified size at the
1652 * end of the mbuf, longword aligned.
1653 */
1654void
1655m_align(struct mbuf *m, int len)
1656{
1657 int adjust;
1658
1659 if (m->m_flags & M_EXT)
1660 adjust = m->m_ext.ext_size - len;
1661 else if (m->m_flags & M_PKTHDR)
1662 adjust = MHLEN - len;
1663 else
1664 adjust = MLEN - len;
1665 m->m_data += adjust &~ (sizeof(long)-1);
1666}