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