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