xref: /freebsd/sys/kern/uipc_mbuf.c (revision beb699c7ba3f23bef2c5971aa3e11a774b60afd1)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)uipc_mbuf.c	8.2 (Berkeley) 1/4/94
34df8bae1dSRodney W. Grimes  */
35df8bae1dSRodney W. Grimes 
36677b542eSDavid E. O'Brien #include <sys/cdefs.h>
37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
38677b542eSDavid E. O'Brien 
39e32a5b94SRobert Watson #include "opt_mac.h"
40240ef842SDavid E. O'Brien #include "opt_param.h"
41352d050eSMike Silbersack #include "opt_mbuf_stress_test.h"
42e32a5b94SRobert Watson 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
45fb919e4dSMark Murray #include <sys/kernel.h>
46beb699c7SMike Silbersack #include <sys/limits.h>
47fb919e4dSMark Murray #include <sys/lock.h>
48e32a5b94SRobert Watson #include <sys/mac.h>
49f9d0d524SRobert Watson #include <sys/malloc.h>
50df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
51639acc13SGarrett Wollman #include <sys/sysctl.h>
52df8bae1dSRodney W. Grimes #include <sys/domain.h>
53df8bae1dSRodney W. Grimes #include <sys/protosw.h>
54beb699c7SMike Silbersack #include <sys/uio.h>
55fb919e4dSMark Murray 
5628f8db14SBruce Evans int	max_linkhdr;
5728f8db14SBruce Evans int	max_protohdr;
5828f8db14SBruce Evans int	max_hdr;
5928f8db14SBruce Evans int	max_datalen;
6051710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
6155e9f80dSMike Silbersack int	m_defragpackets;
6255e9f80dSMike Silbersack int	m_defragbytes;
6355e9f80dSMike Silbersack int	m_defraguseless;
6455e9f80dSMike Silbersack int	m_defragfailure;
65352d050eSMike Silbersack int	m_defragrandomfailures;
66352d050eSMike Silbersack #endif
677d032714SBosko Milekic 
687d032714SBosko Milekic /*
697d032714SBosko Milekic  * sysctl(8) exported objects
707d032714SBosko Milekic  */
71ce02431fSDoug Rabson SYSCTL_DECL(_kern_ipc);
72639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
73639acc13SGarrett Wollman 	   &max_linkhdr, 0, "");
74639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
75639acc13SGarrett Wollman 	   &max_protohdr, 0, "");
76639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
77639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
78639acc13SGarrett Wollman 	   &max_datalen, 0, "");
7951710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
8055e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
8155e9f80dSMike Silbersack 	   &m_defragpackets, 0, "");
8255e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
8355e9f80dSMike Silbersack 	   &m_defragbytes, 0, "");
8455e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
8555e9f80dSMike Silbersack 	   &m_defraguseless, 0, "");
8655e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
8755e9f80dSMike Silbersack 	   &m_defragfailure, 0, "");
88352d050eSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
89352d050eSMike Silbersack 	   &m_defragrandomfailures, 0, "");
90352d050eSMike Silbersack #endif
91df8bae1dSRodney W. Grimes 
92df8bae1dSRodney W. Grimes /*
939967cafcSSam Leffler  * "Move" mbuf pkthdr from "from" to "to".
94e37b1fcdSRobert Watson  * "from" must have M_PKTHDR set, and "to" must be empty.
95e37b1fcdSRobert Watson  */
96e37b1fcdSRobert Watson void
979967cafcSSam Leffler m_move_pkthdr(struct mbuf *to, struct mbuf *from)
98e37b1fcdSRobert Watson {
99e37b1fcdSRobert Watson 
100e37b1fcdSRobert Watson #if 0
1019967cafcSSam Leffler 	/* see below for why these are not enabled */
102fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(to);
103225bff6fSRobert Watson 	/* Note: with MAC, this may not be a good assertion. */
1049967cafcSSam Leffler 	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
1059967cafcSSam Leffler 	    ("m_move_pkthdr: to has tags"));
106e37b1fcdSRobert Watson #endif
1079967cafcSSam Leffler 	KASSERT((to->m_flags & M_EXT) == 0, ("m_move_pkthdr: to has cluster"));
108e32a5b94SRobert Watson #ifdef MAC
109225bff6fSRobert Watson 	/*
110225bff6fSRobert Watson 	 * XXXMAC: It could be this should also occur for non-MAC?
111225bff6fSRobert Watson 	 */
112e32a5b94SRobert Watson 	if (to->m_flags & M_PKTHDR)
113225bff6fSRobert Watson 		m_tag_delete_chain(to, NULL);
114e32a5b94SRobert Watson #endif
115e37b1fcdSRobert Watson 	to->m_flags = from->m_flags & M_COPYFLAGS;
1169967cafcSSam Leffler 	to->m_data = to->m_pktdat;
1179967cafcSSam Leffler 	to->m_pkthdr = from->m_pkthdr;		/* especially tags */
1189967cafcSSam Leffler 	SLIST_INIT(&from->m_pkthdr.tags);	/* purge tags from src */
1199967cafcSSam Leffler 	from->m_flags &= ~M_PKTHDR;
1209967cafcSSam Leffler }
1219967cafcSSam Leffler 
1229967cafcSSam Leffler /*
1239967cafcSSam Leffler  * Duplicate "from"'s mbuf pkthdr in "to".
1249967cafcSSam Leffler  * "from" must have M_PKTHDR set, and "to" must be empty.
1259967cafcSSam Leffler  * In particular, this does a deep copy of the packet tags.
1269967cafcSSam Leffler  */
1279967cafcSSam Leffler int
1289967cafcSSam Leffler m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
1299967cafcSSam Leffler {
1309967cafcSSam Leffler 
1319967cafcSSam Leffler #if 0
1329967cafcSSam Leffler 	/*
1339967cafcSSam Leffler 	 * The mbuf allocator only initializes the pkthdr
1349967cafcSSam Leffler 	 * when the mbuf is allocated with MGETHDR. Many users
1359967cafcSSam Leffler 	 * (e.g. m_copy*, m_prepend) use MGET and then
1369967cafcSSam Leffler 	 * smash the pkthdr as needed causing these
1379967cafcSSam Leffler 	 * assertions to trip.  For now just disable them.
1389967cafcSSam Leffler 	 */
139fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(to);
140225bff6fSRobert Watson 	/* Note: with MAC, this may not be a good assertion. */
1419967cafcSSam Leffler 	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
1429967cafcSSam Leffler #endif
1439967cafcSSam Leffler #ifdef MAC
1449967cafcSSam Leffler 	if (to->m_flags & M_PKTHDR)
145225bff6fSRobert Watson 		m_tag_delete_chain(to, NULL);
1469967cafcSSam Leffler #endif
147df8c7fc9SMike Silbersack 	to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
148df8c7fc9SMike Silbersack 	if ((to->m_flags & M_EXT) == 0)
1499967cafcSSam Leffler 		to->m_data = to->m_pktdat;
150e37b1fcdSRobert Watson 	to->m_pkthdr = from->m_pkthdr;
1519967cafcSSam Leffler 	SLIST_INIT(&to->m_pkthdr.tags);
152aa65d9f5SRobert Watson 	return (m_tag_copy_chain(to, from, MBTOM(how)));
153e37b1fcdSRobert Watson }
154e37b1fcdSRobert Watson 
155e37b1fcdSRobert Watson /*
156df8bae1dSRodney W. Grimes  * Lesser-used path for M_PREPEND:
157df8bae1dSRodney W. Grimes  * allocate new mbuf to prepend to chain,
158df8bae1dSRodney W. Grimes  * copy junk along.
159df8bae1dSRodney W. Grimes  */
160df8bae1dSRodney W. Grimes struct mbuf *
161122a814aSBosko Milekic m_prepend(struct mbuf *m, int len, int how)
162df8bae1dSRodney W. Grimes {
163df8bae1dSRodney W. Grimes 	struct mbuf *mn;
164df8bae1dSRodney W. Grimes 
165f8bf8e39SMike Silbersack 	if (m->m_flags & M_PKTHDR)
166f8bf8e39SMike Silbersack 		MGETHDR(mn, how, m->m_type);
167f8bf8e39SMike Silbersack 	else
168df8bae1dSRodney W. Grimes 		MGET(mn, how, m->m_type);
169122a814aSBosko Milekic 	if (mn == NULL) {
170df8bae1dSRodney W. Grimes 		m_freem(m);
171122a814aSBosko Milekic 		return (NULL);
172df8bae1dSRodney W. Grimes 	}
173225bff6fSRobert Watson 	if (m->m_flags & M_PKTHDR)
1749967cafcSSam Leffler 		M_MOVE_PKTHDR(mn, m);
175df8bae1dSRodney W. Grimes 	mn->m_next = m;
176df8bae1dSRodney W. Grimes 	m = mn;
177df8bae1dSRodney W. Grimes 	if (len < MHLEN)
178df8bae1dSRodney W. Grimes 		MH_ALIGN(m, len);
179df8bae1dSRodney W. Grimes 	m->m_len = len;
180df8bae1dSRodney W. Grimes 	return (m);
181df8bae1dSRodney W. Grimes }
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes /*
184df8bae1dSRodney W. Grimes  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
185df8bae1dSRodney W. Grimes  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
186a163d034SWarner Losh  * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
1871c38f2eaSArchie Cobbs  * Note that the copy is read-only, because clusters are not copied,
1881c38f2eaSArchie Cobbs  * only their reference counts are incremented.
189df8bae1dSRodney W. Grimes  */
190df8bae1dSRodney W. Grimes struct mbuf *
191122a814aSBosko Milekic m_copym(struct mbuf *m, int off0, int len, int wait)
192df8bae1dSRodney W. Grimes {
193122a814aSBosko Milekic 	struct mbuf *n, **np;
194122a814aSBosko Milekic 	int off = off0;
195df8bae1dSRodney W. Grimes 	struct mbuf *top;
196df8bae1dSRodney W. Grimes 	int copyhdr = 0;
197df8bae1dSRodney W. Grimes 
198e0a653ddSAlfred Perlstein 	KASSERT(off >= 0, ("m_copym, negative off %d", off));
199e0a653ddSAlfred Perlstein 	KASSERT(len >= 0, ("m_copym, negative len %d", len));
200df8bae1dSRodney W. Grimes 	if (off == 0 && m->m_flags & M_PKTHDR)
201df8bae1dSRodney W. Grimes 		copyhdr = 1;
202df8bae1dSRodney W. Grimes 	while (off > 0) {
203e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
204df8bae1dSRodney W. Grimes 		if (off < m->m_len)
205df8bae1dSRodney W. Grimes 			break;
206df8bae1dSRodney W. Grimes 		off -= m->m_len;
207df8bae1dSRodney W. Grimes 		m = m->m_next;
208df8bae1dSRodney W. Grimes 	}
209df8bae1dSRodney W. Grimes 	np = &top;
210df8bae1dSRodney W. Grimes 	top = 0;
211df8bae1dSRodney W. Grimes 	while (len > 0) {
212122a814aSBosko Milekic 		if (m == NULL) {
213e0a653ddSAlfred Perlstein 			KASSERT(len == M_COPYALL,
214e0a653ddSAlfred Perlstein 			    ("m_copym, length > size of mbuf chain"));
215df8bae1dSRodney W. Grimes 			break;
216df8bae1dSRodney W. Grimes 		}
217f8bf8e39SMike Silbersack 		if (copyhdr)
218f8bf8e39SMike Silbersack 			MGETHDR(n, wait, m->m_type);
219f8bf8e39SMike Silbersack 		else
220df8bae1dSRodney W. Grimes 			MGET(n, wait, m->m_type);
221df8bae1dSRodney W. Grimes 		*np = n;
222122a814aSBosko Milekic 		if (n == NULL)
223df8bae1dSRodney W. Grimes 			goto nospace;
224df8bae1dSRodney W. Grimes 		if (copyhdr) {
2259967cafcSSam Leffler 			if (!m_dup_pkthdr(n, m, wait))
2269967cafcSSam Leffler 				goto nospace;
227df8bae1dSRodney W. Grimes 			if (len == M_COPYALL)
228df8bae1dSRodney W. Grimes 				n->m_pkthdr.len -= off0;
229df8bae1dSRodney W. Grimes 			else
230df8bae1dSRodney W. Grimes 				n->m_pkthdr.len = len;
231df8bae1dSRodney W. Grimes 			copyhdr = 0;
232df8bae1dSRodney W. Grimes 		}
233df8bae1dSRodney W. Grimes 		n->m_len = min(len, m->m_len - off);
234df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT) {
235df8bae1dSRodney W. Grimes 			n->m_data = m->m_data + off;
236df8bae1dSRodney W. Grimes 			n->m_ext = m->m_ext;
237df8bae1dSRodney W. Grimes 			n->m_flags |= M_EXT;
238a5c4836dSDavid Malone 			MEXT_ADD_REF(m);
239df8bae1dSRodney W. Grimes 		} else
240df8bae1dSRodney W. Grimes 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
241bd395ae8SBosko Milekic 			    (u_int)n->m_len);
242df8bae1dSRodney W. Grimes 		if (len != M_COPYALL)
243df8bae1dSRodney W. Grimes 			len -= n->m_len;
244df8bae1dSRodney W. Grimes 		off = 0;
245df8bae1dSRodney W. Grimes 		m = m->m_next;
246df8bae1dSRodney W. Grimes 		np = &n->m_next;
247df8bae1dSRodney W. Grimes 	}
24808442f8aSBosko Milekic 	if (top == NULL)
24908442f8aSBosko Milekic 		mbstat.m_mcfail++;	/* XXX: No consistency. */
25008442f8aSBosko Milekic 
251df8bae1dSRodney W. Grimes 	return (top);
252df8bae1dSRodney W. Grimes nospace:
253df8bae1dSRodney W. Grimes 	m_freem(top);
25408442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
255122a814aSBosko Milekic 	return (NULL);
256df8bae1dSRodney W. Grimes }
257df8bae1dSRodney W. Grimes 
258df8bae1dSRodney W. Grimes /*
2596a06dea0SGarrett Wollman  * Copy an entire packet, including header (which must be present).
2606a06dea0SGarrett Wollman  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
2611c38f2eaSArchie Cobbs  * Note that the copy is read-only, because clusters are not copied,
2621c38f2eaSArchie Cobbs  * only their reference counts are incremented.
2635fe86675SLuigi Rizzo  * Preserve alignment of the first mbuf so if the creator has left
2645fe86675SLuigi Rizzo  * some room at the beginning (e.g. for inserting protocol headers)
2655fe86675SLuigi Rizzo  * the copies still have the room available.
2666a06dea0SGarrett Wollman  */
2676a06dea0SGarrett Wollman struct mbuf *
268122a814aSBosko Milekic m_copypacket(struct mbuf *m, int how)
2696a06dea0SGarrett Wollman {
2706a06dea0SGarrett Wollman 	struct mbuf *top, *n, *o;
2716a06dea0SGarrett Wollman 
2726a06dea0SGarrett Wollman 	MGET(n, how, m->m_type);
2736a06dea0SGarrett Wollman 	top = n;
274122a814aSBosko Milekic 	if (n == NULL)
2756a06dea0SGarrett Wollman 		goto nospace;
2766a06dea0SGarrett Wollman 
2779967cafcSSam Leffler 	if (!m_dup_pkthdr(n, m, how))
2789967cafcSSam Leffler 		goto nospace;
2796a06dea0SGarrett Wollman 	n->m_len = m->m_len;
2806a06dea0SGarrett Wollman 	if (m->m_flags & M_EXT) {
2816a06dea0SGarrett Wollman 		n->m_data = m->m_data;
2826a06dea0SGarrett Wollman 		n->m_ext = m->m_ext;
2836a06dea0SGarrett Wollman 		n->m_flags |= M_EXT;
284a5c4836dSDavid Malone 		MEXT_ADD_REF(m);
2856a06dea0SGarrett Wollman 	} else {
2865fe86675SLuigi Rizzo 		n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
2876a06dea0SGarrett Wollman 		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
2886a06dea0SGarrett Wollman 	}
2896a06dea0SGarrett Wollman 
2906a06dea0SGarrett Wollman 	m = m->m_next;
2916a06dea0SGarrett Wollman 	while (m) {
2926a06dea0SGarrett Wollman 		MGET(o, how, m->m_type);
293122a814aSBosko Milekic 		if (o == NULL)
2946a06dea0SGarrett Wollman 			goto nospace;
2956a06dea0SGarrett Wollman 
2966a06dea0SGarrett Wollman 		n->m_next = o;
2976a06dea0SGarrett Wollman 		n = n->m_next;
2986a06dea0SGarrett Wollman 
2996a06dea0SGarrett Wollman 		n->m_len = m->m_len;
3006a06dea0SGarrett Wollman 		if (m->m_flags & M_EXT) {
3016a06dea0SGarrett Wollman 			n->m_data = m->m_data;
3026a06dea0SGarrett Wollman 			n->m_ext = m->m_ext;
3036a06dea0SGarrett Wollman 			n->m_flags |= M_EXT;
304a5c4836dSDavid Malone 			MEXT_ADD_REF(m);
3056a06dea0SGarrett Wollman 		} else {
3066a06dea0SGarrett Wollman 			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
3076a06dea0SGarrett Wollman 		}
3086a06dea0SGarrett Wollman 
3096a06dea0SGarrett Wollman 		m = m->m_next;
3106a06dea0SGarrett Wollman 	}
3116a06dea0SGarrett Wollman 	return top;
3126a06dea0SGarrett Wollman nospace:
3136a06dea0SGarrett Wollman 	m_freem(top);
31408442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
315122a814aSBosko Milekic 	return (NULL);
3166a06dea0SGarrett Wollman }
3176a06dea0SGarrett Wollman 
3186a06dea0SGarrett Wollman /*
319df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain starting "off" bytes from the beginning,
320df8bae1dSRodney W. Grimes  * continuing for "len" bytes, into the indicated buffer.
321df8bae1dSRodney W. Grimes  */
32226f9a767SRodney W. Grimes void
323a8cfc0eeSJulian Elischer m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
324df8bae1dSRodney W. Grimes {
325bd395ae8SBosko Milekic 	u_int count;
326df8bae1dSRodney W. Grimes 
327e0a653ddSAlfred Perlstein 	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
328e0a653ddSAlfred Perlstein 	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
329df8bae1dSRodney W. Grimes 	while (off > 0) {
330e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
331df8bae1dSRodney W. Grimes 		if (off < m->m_len)
332df8bae1dSRodney W. Grimes 			break;
333df8bae1dSRodney W. Grimes 		off -= m->m_len;
334df8bae1dSRodney W. Grimes 		m = m->m_next;
335df8bae1dSRodney W. Grimes 	}
336df8bae1dSRodney W. Grimes 	while (len > 0) {
337e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
338df8bae1dSRodney W. Grimes 		count = min(m->m_len - off, len);
339df8bae1dSRodney W. Grimes 		bcopy(mtod(m, caddr_t) + off, cp, count);
340df8bae1dSRodney W. Grimes 		len -= count;
341df8bae1dSRodney W. Grimes 		cp += count;
342df8bae1dSRodney W. Grimes 		off = 0;
343df8bae1dSRodney W. Grimes 		m = m->m_next;
344df8bae1dSRodney W. Grimes 	}
345df8bae1dSRodney W. Grimes }
346df8bae1dSRodney W. Grimes 
347df8bae1dSRodney W. Grimes /*
3481c38f2eaSArchie Cobbs  * Copy a packet header mbuf chain into a completely new chain, including
3491c38f2eaSArchie Cobbs  * copying any mbuf clusters.  Use this instead of m_copypacket() when
3501c38f2eaSArchie Cobbs  * you need a writable copy of an mbuf chain.
3511c38f2eaSArchie Cobbs  */
3521c38f2eaSArchie Cobbs struct mbuf *
353122a814aSBosko Milekic m_dup(struct mbuf *m, int how)
3541c38f2eaSArchie Cobbs {
3551c38f2eaSArchie Cobbs 	struct mbuf **p, *top = NULL;
3561c38f2eaSArchie Cobbs 	int remain, moff, nsize;
3571c38f2eaSArchie Cobbs 
3581c38f2eaSArchie Cobbs 	/* Sanity check */
3591c38f2eaSArchie Cobbs 	if (m == NULL)
360122a814aSBosko Milekic 		return (NULL);
361fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
3621c38f2eaSArchie Cobbs 
3631c38f2eaSArchie Cobbs 	/* While there's more data, get a new mbuf, tack it on, and fill it */
3641c38f2eaSArchie Cobbs 	remain = m->m_pkthdr.len;
3651c38f2eaSArchie Cobbs 	moff = 0;
3661c38f2eaSArchie Cobbs 	p = &top;
3671c38f2eaSArchie Cobbs 	while (remain > 0 || top == NULL) {	/* allow m->m_pkthdr.len == 0 */
3681c38f2eaSArchie Cobbs 		struct mbuf *n;
3691c38f2eaSArchie Cobbs 
3701c38f2eaSArchie Cobbs 		/* Get the next new mbuf */
3711c38f2eaSArchie Cobbs 		MGET(n, how, m->m_type);
3721c38f2eaSArchie Cobbs 		if (n == NULL)
3731c38f2eaSArchie Cobbs 			goto nospace;
3741c38f2eaSArchie Cobbs 		if (top == NULL) {		/* first one, must be PKTHDR */
3759967cafcSSam Leffler 			if (!m_dup_pkthdr(n, m, how))
3769967cafcSSam Leffler 				goto nospace;
3771c38f2eaSArchie Cobbs 			nsize = MHLEN;
3781c38f2eaSArchie Cobbs 		} else				/* not the first one */
3791c38f2eaSArchie Cobbs 			nsize = MLEN;
3801c38f2eaSArchie Cobbs 		if (remain >= MINCLSIZE) {
3811c38f2eaSArchie Cobbs 			MCLGET(n, how);
3821c38f2eaSArchie Cobbs 			if ((n->m_flags & M_EXT) == 0) {
3831c38f2eaSArchie Cobbs 				(void)m_free(n);
3841c38f2eaSArchie Cobbs 				goto nospace;
3851c38f2eaSArchie Cobbs 			}
3861c38f2eaSArchie Cobbs 			nsize = MCLBYTES;
3871c38f2eaSArchie Cobbs 		}
3881c38f2eaSArchie Cobbs 		n->m_len = 0;
3891c38f2eaSArchie Cobbs 
3901c38f2eaSArchie Cobbs 		/* Link it into the new chain */
3911c38f2eaSArchie Cobbs 		*p = n;
3921c38f2eaSArchie Cobbs 		p = &n->m_next;
3931c38f2eaSArchie Cobbs 
3941c38f2eaSArchie Cobbs 		/* Copy data from original mbuf(s) into new mbuf */
3951c38f2eaSArchie Cobbs 		while (n->m_len < nsize && m != NULL) {
3961c38f2eaSArchie Cobbs 			int chunk = min(nsize - n->m_len, m->m_len - moff);
3971c38f2eaSArchie Cobbs 
3981c38f2eaSArchie Cobbs 			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
3991c38f2eaSArchie Cobbs 			moff += chunk;
4001c38f2eaSArchie Cobbs 			n->m_len += chunk;
4011c38f2eaSArchie Cobbs 			remain -= chunk;
4021c38f2eaSArchie Cobbs 			if (moff == m->m_len) {
4031c38f2eaSArchie Cobbs 				m = m->m_next;
4041c38f2eaSArchie Cobbs 				moff = 0;
4051c38f2eaSArchie Cobbs 			}
4061c38f2eaSArchie Cobbs 		}
4071c38f2eaSArchie Cobbs 
4081c38f2eaSArchie Cobbs 		/* Check correct total mbuf length */
4091c38f2eaSArchie Cobbs 		KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
410a48740b6SDavid E. O'Brien 		    	("%s: bogus m_pkthdr.len", __func__));
4111c38f2eaSArchie Cobbs 	}
4121c38f2eaSArchie Cobbs 	return (top);
4131c38f2eaSArchie Cobbs 
4141c38f2eaSArchie Cobbs nospace:
4151c38f2eaSArchie Cobbs 	m_freem(top);
41608442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
417122a814aSBosko Milekic 	return (NULL);
4181c38f2eaSArchie Cobbs }
4191c38f2eaSArchie Cobbs 
4201c38f2eaSArchie Cobbs /*
421df8bae1dSRodney W. Grimes  * Concatenate mbuf chain n to m.
422df8bae1dSRodney W. Grimes  * Both chains must be of the same type (e.g. MT_DATA).
423df8bae1dSRodney W. Grimes  * Any m_pkthdr is not updated.
424df8bae1dSRodney W. Grimes  */
42526f9a767SRodney W. Grimes void
426122a814aSBosko Milekic m_cat(struct mbuf *m, struct mbuf *n)
427df8bae1dSRodney W. Grimes {
428df8bae1dSRodney W. Grimes 	while (m->m_next)
429df8bae1dSRodney W. Grimes 		m = m->m_next;
430df8bae1dSRodney W. Grimes 	while (n) {
431df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT ||
432df8bae1dSRodney W. Grimes 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
433df8bae1dSRodney W. Grimes 			/* just join the two chains */
434df8bae1dSRodney W. Grimes 			m->m_next = n;
435df8bae1dSRodney W. Grimes 			return;
436df8bae1dSRodney W. Grimes 		}
437df8bae1dSRodney W. Grimes 		/* splat the data from one into the other */
438df8bae1dSRodney W. Grimes 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
439df8bae1dSRodney W. Grimes 		    (u_int)n->m_len);
440df8bae1dSRodney W. Grimes 		m->m_len += n->m_len;
441df8bae1dSRodney W. Grimes 		n = m_free(n);
442df8bae1dSRodney W. Grimes 	}
443df8bae1dSRodney W. Grimes }
444df8bae1dSRodney W. Grimes 
44526f9a767SRodney W. Grimes void
446122a814aSBosko Milekic m_adj(struct mbuf *mp, int req_len)
447df8bae1dSRodney W. Grimes {
448122a814aSBosko Milekic 	int len = req_len;
449122a814aSBosko Milekic 	struct mbuf *m;
450122a814aSBosko Milekic 	int count;
451df8bae1dSRodney W. Grimes 
452df8bae1dSRodney W. Grimes 	if ((m = mp) == NULL)
453df8bae1dSRodney W. Grimes 		return;
454df8bae1dSRodney W. Grimes 	if (len >= 0) {
455df8bae1dSRodney W. Grimes 		/*
456df8bae1dSRodney W. Grimes 		 * Trim from head.
457df8bae1dSRodney W. Grimes 		 */
458df8bae1dSRodney W. Grimes 		while (m != NULL && len > 0) {
459df8bae1dSRodney W. Grimes 			if (m->m_len <= len) {
460df8bae1dSRodney W. Grimes 				len -= m->m_len;
461df8bae1dSRodney W. Grimes 				m->m_len = 0;
462df8bae1dSRodney W. Grimes 				m = m->m_next;
463df8bae1dSRodney W. Grimes 			} else {
464df8bae1dSRodney W. Grimes 				m->m_len -= len;
465df8bae1dSRodney W. Grimes 				m->m_data += len;
466df8bae1dSRodney W. Grimes 				len = 0;
467df8bae1dSRodney W. Grimes 			}
468df8bae1dSRodney W. Grimes 		}
469df8bae1dSRodney W. Grimes 		m = mp;
470df8bae1dSRodney W. Grimes 		if (mp->m_flags & M_PKTHDR)
471df8bae1dSRodney W. Grimes 			m->m_pkthdr.len -= (req_len - len);
472df8bae1dSRodney W. Grimes 	} else {
473df8bae1dSRodney W. Grimes 		/*
474df8bae1dSRodney W. Grimes 		 * Trim from tail.  Scan the mbuf chain,
475df8bae1dSRodney W. Grimes 		 * calculating its length and finding the last mbuf.
476df8bae1dSRodney W. Grimes 		 * If the adjustment only affects this mbuf, then just
477df8bae1dSRodney W. Grimes 		 * adjust and return.  Otherwise, rescan and truncate
478df8bae1dSRodney W. Grimes 		 * after the remaining size.
479df8bae1dSRodney W. Grimes 		 */
480df8bae1dSRodney W. Grimes 		len = -len;
481df8bae1dSRodney W. Grimes 		count = 0;
482df8bae1dSRodney W. Grimes 		for (;;) {
483df8bae1dSRodney W. Grimes 			count += m->m_len;
484df8bae1dSRodney W. Grimes 			if (m->m_next == (struct mbuf *)0)
485df8bae1dSRodney W. Grimes 				break;
486df8bae1dSRodney W. Grimes 			m = m->m_next;
487df8bae1dSRodney W. Grimes 		}
488df8bae1dSRodney W. Grimes 		if (m->m_len >= len) {
489df8bae1dSRodney W. Grimes 			m->m_len -= len;
490df8bae1dSRodney W. Grimes 			if (mp->m_flags & M_PKTHDR)
491df8bae1dSRodney W. Grimes 				mp->m_pkthdr.len -= len;
492df8bae1dSRodney W. Grimes 			return;
493df8bae1dSRodney W. Grimes 		}
494df8bae1dSRodney W. Grimes 		count -= len;
495df8bae1dSRodney W. Grimes 		if (count < 0)
496df8bae1dSRodney W. Grimes 			count = 0;
497df8bae1dSRodney W. Grimes 		/*
498df8bae1dSRodney W. Grimes 		 * Correct length for chain is "count".
499df8bae1dSRodney W. Grimes 		 * Find the mbuf with last data, adjust its length,
500df8bae1dSRodney W. Grimes 		 * and toss data from remaining mbufs on chain.
501df8bae1dSRodney W. Grimes 		 */
502df8bae1dSRodney W. Grimes 		m = mp;
503df8bae1dSRodney W. Grimes 		if (m->m_flags & M_PKTHDR)
504df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = count;
505df8bae1dSRodney W. Grimes 		for (; m; m = m->m_next) {
506df8bae1dSRodney W. Grimes 			if (m->m_len >= count) {
507df8bae1dSRodney W. Grimes 				m->m_len = count;
508df8bae1dSRodney W. Grimes 				break;
509df8bae1dSRodney W. Grimes 			}
510df8bae1dSRodney W. Grimes 			count -= m->m_len;
511df8bae1dSRodney W. Grimes 		}
512797f2d22SPoul-Henning Kamp 		while (m->m_next)
513797f2d22SPoul-Henning Kamp 			(m = m->m_next) ->m_len = 0;
514df8bae1dSRodney W. Grimes 	}
515df8bae1dSRodney W. Grimes }
516df8bae1dSRodney W. Grimes 
517df8bae1dSRodney W. Grimes /*
518df8bae1dSRodney W. Grimes  * Rearange an mbuf chain so that len bytes are contiguous
519df8bae1dSRodney W. Grimes  * and in the data area of an mbuf (so that mtod and dtom
520df8bae1dSRodney W. Grimes  * will work for a structure of size len).  Returns the resulting
521df8bae1dSRodney W. Grimes  * mbuf chain on success, frees it and returns null on failure.
522df8bae1dSRodney W. Grimes  * If there is room, it will add up to max_protohdr-len extra bytes to the
523df8bae1dSRodney W. Grimes  * contiguous region in an attempt to avoid being called next time.
524df8bae1dSRodney W. Grimes  */
525df8bae1dSRodney W. Grimes struct mbuf *
526122a814aSBosko Milekic m_pullup(struct mbuf *n, int len)
527df8bae1dSRodney W. Grimes {
528122a814aSBosko Milekic 	struct mbuf *m;
529122a814aSBosko Milekic 	int count;
530df8bae1dSRodney W. Grimes 	int space;
531df8bae1dSRodney W. Grimes 
532df8bae1dSRodney W. Grimes 	/*
533df8bae1dSRodney W. Grimes 	 * If first mbuf has no cluster, and has room for len bytes
534df8bae1dSRodney W. Grimes 	 * without shifting current data, pullup into it,
535df8bae1dSRodney W. Grimes 	 * otherwise allocate a new mbuf to prepend to the chain.
536df8bae1dSRodney W. Grimes 	 */
537df8bae1dSRodney W. Grimes 	if ((n->m_flags & M_EXT) == 0 &&
538df8bae1dSRodney W. Grimes 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
539df8bae1dSRodney W. Grimes 		if (n->m_len >= len)
540df8bae1dSRodney W. Grimes 			return (n);
541df8bae1dSRodney W. Grimes 		m = n;
542df8bae1dSRodney W. Grimes 		n = n->m_next;
543df8bae1dSRodney W. Grimes 		len -= m->m_len;
544df8bae1dSRodney W. Grimes 	} else {
545df8bae1dSRodney W. Grimes 		if (len > MHLEN)
546df8bae1dSRodney W. Grimes 			goto bad;
547a163d034SWarner Losh 		MGET(m, M_DONTWAIT, n->m_type);
548122a814aSBosko Milekic 		if (m == NULL)
549df8bae1dSRodney W. Grimes 			goto bad;
550df8bae1dSRodney W. Grimes 		m->m_len = 0;
5519967cafcSSam Leffler 		if (n->m_flags & M_PKTHDR)
5529967cafcSSam Leffler 			M_MOVE_PKTHDR(m, n);
553df8bae1dSRodney W. Grimes 	}
554df8bae1dSRodney W. Grimes 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
555df8bae1dSRodney W. Grimes 	do {
556df8bae1dSRodney W. Grimes 		count = min(min(max(len, max_protohdr), space), n->m_len);
557df8bae1dSRodney W. Grimes 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
558bd395ae8SBosko Milekic 		  (u_int)count);
559df8bae1dSRodney W. Grimes 		len -= count;
560df8bae1dSRodney W. Grimes 		m->m_len += count;
561df8bae1dSRodney W. Grimes 		n->m_len -= count;
562df8bae1dSRodney W. Grimes 		space -= count;
563df8bae1dSRodney W. Grimes 		if (n->m_len)
564df8bae1dSRodney W. Grimes 			n->m_data += count;
565df8bae1dSRodney W. Grimes 		else
566df8bae1dSRodney W. Grimes 			n = m_free(n);
567df8bae1dSRodney W. Grimes 	} while (len > 0 && n);
568df8bae1dSRodney W. Grimes 	if (len > 0) {
569df8bae1dSRodney W. Grimes 		(void) m_free(m);
570df8bae1dSRodney W. Grimes 		goto bad;
571df8bae1dSRodney W. Grimes 	}
572df8bae1dSRodney W. Grimes 	m->m_next = n;
573df8bae1dSRodney W. Grimes 	return (m);
574df8bae1dSRodney W. Grimes bad:
575df8bae1dSRodney W. Grimes 	m_freem(n);
57608442f8aSBosko Milekic 	mbstat.m_mpfail++;	/* XXX: No consistency. */
577122a814aSBosko Milekic 	return (NULL);
578df8bae1dSRodney W. Grimes }
579df8bae1dSRodney W. Grimes 
580df8bae1dSRodney W. Grimes /*
581df8bae1dSRodney W. Grimes  * Partition an mbuf chain in two pieces, returning the tail --
582df8bae1dSRodney W. Grimes  * all but the first len0 bytes.  In case of failure, it returns NULL and
583df8bae1dSRodney W. Grimes  * attempts to restore the chain to its original state.
58448d183faSArchie Cobbs  *
58548d183faSArchie Cobbs  * Note that the resulting mbufs might be read-only, because the new
58648d183faSArchie Cobbs  * mbuf can end up sharing an mbuf cluster with the original mbuf if
58748d183faSArchie Cobbs  * the "breaking point" happens to lie within a cluster mbuf. Use the
58848d183faSArchie Cobbs  * M_WRITABLE() macro to check for this case.
589df8bae1dSRodney W. Grimes  */
590df8bae1dSRodney W. Grimes struct mbuf *
591122a814aSBosko Milekic m_split(struct mbuf *m0, int len0, int wait)
592df8bae1dSRodney W. Grimes {
593122a814aSBosko Milekic 	struct mbuf *m, *n;
594bd395ae8SBosko Milekic 	u_int len = len0, remain;
595df8bae1dSRodney W. Grimes 
596df8bae1dSRodney W. Grimes 	for (m = m0; m && len > m->m_len; m = m->m_next)
597df8bae1dSRodney W. Grimes 		len -= m->m_len;
598122a814aSBosko Milekic 	if (m == NULL)
599122a814aSBosko Milekic 		return (NULL);
600df8bae1dSRodney W. Grimes 	remain = m->m_len - len;
601df8bae1dSRodney W. Grimes 	if (m0->m_flags & M_PKTHDR) {
602df8bae1dSRodney W. Grimes 		MGETHDR(n, wait, m0->m_type);
603122a814aSBosko Milekic 		if (n == NULL)
604122a814aSBosko Milekic 			return (NULL);
605df8bae1dSRodney W. Grimes 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
606df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
607df8bae1dSRodney W. Grimes 		m0->m_pkthdr.len = len0;
608df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT)
609df8bae1dSRodney W. Grimes 			goto extpacket;
610df8bae1dSRodney W. Grimes 		if (remain > MHLEN) {
611df8bae1dSRodney W. Grimes 			/* m can't be the lead packet */
612df8bae1dSRodney W. Grimes 			MH_ALIGN(n, 0);
613df8bae1dSRodney W. Grimes 			n->m_next = m_split(m, len, wait);
614122a814aSBosko Milekic 			if (n->m_next == NULL) {
615df8bae1dSRodney W. Grimes 				(void) m_free(n);
616122a814aSBosko Milekic 				return (NULL);
61740376987SJeffrey Hsu 			} else {
61840376987SJeffrey Hsu 				n->m_len = 0;
619df8bae1dSRodney W. Grimes 				return (n);
62040376987SJeffrey Hsu 			}
621df8bae1dSRodney W. Grimes 		} else
622df8bae1dSRodney W. Grimes 			MH_ALIGN(n, remain);
623df8bae1dSRodney W. Grimes 	} else if (remain == 0) {
624df8bae1dSRodney W. Grimes 		n = m->m_next;
625122a814aSBosko Milekic 		m->m_next = NULL;
626df8bae1dSRodney W. Grimes 		return (n);
627df8bae1dSRodney W. Grimes 	} else {
628df8bae1dSRodney W. Grimes 		MGET(n, wait, m->m_type);
629122a814aSBosko Milekic 		if (n == NULL)
630122a814aSBosko Milekic 			return (NULL);
631df8bae1dSRodney W. Grimes 		M_ALIGN(n, remain);
632df8bae1dSRodney W. Grimes 	}
633df8bae1dSRodney W. Grimes extpacket:
634df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT) {
635df8bae1dSRodney W. Grimes 		n->m_flags |= M_EXT;
636df8bae1dSRodney W. Grimes 		n->m_ext = m->m_ext;
637a5c4836dSDavid Malone 		MEXT_ADD_REF(m);
638df8bae1dSRodney W. Grimes 		n->m_data = m->m_data + len;
639df8bae1dSRodney W. Grimes 	} else {
640df8bae1dSRodney W. Grimes 		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
641df8bae1dSRodney W. Grimes 	}
642df8bae1dSRodney W. Grimes 	n->m_len = remain;
643df8bae1dSRodney W. Grimes 	m->m_len = len;
644df8bae1dSRodney W. Grimes 	n->m_next = m->m_next;
645122a814aSBosko Milekic 	m->m_next = NULL;
646df8bae1dSRodney W. Grimes 	return (n);
647df8bae1dSRodney W. Grimes }
648df8bae1dSRodney W. Grimes /*
649df8bae1dSRodney W. Grimes  * Routine to copy from device local memory into mbufs.
650f5eece3fSBosko Milekic  * Note that `off' argument is offset into first mbuf of target chain from
651f5eece3fSBosko Milekic  * which to begin copying the data to.
652df8bae1dSRodney W. Grimes  */
653df8bae1dSRodney W. Grimes struct mbuf *
654f5eece3fSBosko Milekic m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
655122a814aSBosko Milekic 	 void (*copy)(char *from, caddr_t to, u_int len))
656df8bae1dSRodney W. Grimes {
657122a814aSBosko Milekic 	struct mbuf *m;
658df8bae1dSRodney W. Grimes 	struct mbuf *top = 0, **mp = &top;
659f5eece3fSBosko Milekic 	int len;
660df8bae1dSRodney W. Grimes 
661f5eece3fSBosko Milekic 	if (off < 0 || off > MHLEN)
662f5eece3fSBosko Milekic 		return (NULL);
663f5eece3fSBosko Milekic 
664a163d034SWarner Losh 	MGETHDR(m, M_DONTWAIT, MT_DATA);
665122a814aSBosko Milekic 	if (m == NULL)
666122a814aSBosko Milekic 		return (NULL);
667df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = ifp;
668df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = totlen;
669f5eece3fSBosko Milekic 	len = MHLEN;
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 	while (totlen > 0) {
672df8bae1dSRodney W. Grimes 		if (top) {
673a163d034SWarner Losh 			MGET(m, M_DONTWAIT, MT_DATA);
674122a814aSBosko Milekic 			if (m == NULL) {
675df8bae1dSRodney W. Grimes 				m_freem(top);
676122a814aSBosko Milekic 				return (NULL);
677df8bae1dSRodney W. Grimes 			}
678f5eece3fSBosko Milekic 			len = MLEN;
679df8bae1dSRodney W. Grimes 		}
680f5eece3fSBosko Milekic 		if (totlen + off >= MINCLSIZE) {
681a163d034SWarner Losh 			MCLGET(m, M_DONTWAIT);
682df8bae1dSRodney W. Grimes 			if (m->m_flags & M_EXT)
683f5eece3fSBosko Milekic 				len = MCLBYTES;
684df8bae1dSRodney W. Grimes 		} else {
685df8bae1dSRodney W. Grimes 			/*
686df8bae1dSRodney W. Grimes 			 * Place initial small packet/header at end of mbuf.
687df8bae1dSRodney W. Grimes 			 */
688f5eece3fSBosko Milekic 			if (top == NULL && totlen + off + max_linkhdr <= len) {
689df8bae1dSRodney W. Grimes 				m->m_data += max_linkhdr;
690f5eece3fSBosko Milekic 				len -= max_linkhdr;
691df8bae1dSRodney W. Grimes 			}
692f5eece3fSBosko Milekic 		}
693f5eece3fSBosko Milekic 		if (off) {
694f5eece3fSBosko Milekic 			m->m_data += off;
695f5eece3fSBosko Milekic 			len -= off;
696f5eece3fSBosko Milekic 			off = 0;
697f5eece3fSBosko Milekic 		}
698f5eece3fSBosko Milekic 		m->m_len = len = min(totlen, len);
699df8bae1dSRodney W. Grimes 		if (copy)
700bd395ae8SBosko Milekic 			copy(buf, mtod(m, caddr_t), (u_int)len);
701df8bae1dSRodney W. Grimes 		else
702bd395ae8SBosko Milekic 			bcopy(buf, mtod(m, caddr_t), (u_int)len);
703f5eece3fSBosko Milekic 		buf += len;
704df8bae1dSRodney W. Grimes 		*mp = m;
705df8bae1dSRodney W. Grimes 		mp = &m->m_next;
706df8bae1dSRodney W. Grimes 		totlen -= len;
707df8bae1dSRodney W. Grimes 	}
708df8bae1dSRodney W. Grimes 	return (top);
709df8bae1dSRodney W. Grimes }
710c5789ba3SPoul-Henning Kamp 
711c5789ba3SPoul-Henning Kamp /*
712c5789ba3SPoul-Henning Kamp  * Copy data from a buffer back into the indicated mbuf chain,
713c5789ba3SPoul-Henning Kamp  * starting "off" bytes from the beginning, extending the mbuf
714c5789ba3SPoul-Henning Kamp  * chain if necessary.
715c5789ba3SPoul-Henning Kamp  */
716c5789ba3SPoul-Henning Kamp void
717122a814aSBosko Milekic m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
718c5789ba3SPoul-Henning Kamp {
719122a814aSBosko Milekic 	int mlen;
720122a814aSBosko Milekic 	struct mbuf *m = m0, *n;
721c5789ba3SPoul-Henning Kamp 	int totlen = 0;
722c5789ba3SPoul-Henning Kamp 
723122a814aSBosko Milekic 	if (m0 == NULL)
724c5789ba3SPoul-Henning Kamp 		return;
725c5789ba3SPoul-Henning Kamp 	while (off > (mlen = m->m_len)) {
726c5789ba3SPoul-Henning Kamp 		off -= mlen;
727c5789ba3SPoul-Henning Kamp 		totlen += mlen;
728122a814aSBosko Milekic 		if (m->m_next == NULL) {
729a163d034SWarner Losh 			n = m_get_clrd(M_DONTWAIT, m->m_type);
730122a814aSBosko Milekic 			if (n == NULL)
731c5789ba3SPoul-Henning Kamp 				goto out;
732c5789ba3SPoul-Henning Kamp 			n->m_len = min(MLEN, len + off);
733c5789ba3SPoul-Henning Kamp 			m->m_next = n;
734c5789ba3SPoul-Henning Kamp 		}
735c5789ba3SPoul-Henning Kamp 		m = m->m_next;
736c5789ba3SPoul-Henning Kamp 	}
737c5789ba3SPoul-Henning Kamp 	while (len > 0) {
738c5789ba3SPoul-Henning Kamp 		mlen = min (m->m_len - off, len);
739bd395ae8SBosko Milekic 		bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
740c5789ba3SPoul-Henning Kamp 		cp += mlen;
741c5789ba3SPoul-Henning Kamp 		len -= mlen;
742c5789ba3SPoul-Henning Kamp 		mlen += off;
743c5789ba3SPoul-Henning Kamp 		off = 0;
744c5789ba3SPoul-Henning Kamp 		totlen += mlen;
745c5789ba3SPoul-Henning Kamp 		if (len == 0)
746c5789ba3SPoul-Henning Kamp 			break;
747122a814aSBosko Milekic 		if (m->m_next == NULL) {
748a163d034SWarner Losh 			n = m_get(M_DONTWAIT, m->m_type);
749122a814aSBosko Milekic 			if (n == NULL)
750c5789ba3SPoul-Henning Kamp 				break;
751c5789ba3SPoul-Henning Kamp 			n->m_len = min(MLEN, len);
752c5789ba3SPoul-Henning Kamp 			m->m_next = n;
753c5789ba3SPoul-Henning Kamp 		}
754c5789ba3SPoul-Henning Kamp 		m = m->m_next;
755c5789ba3SPoul-Henning Kamp 	}
756c5789ba3SPoul-Henning Kamp out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
757c5789ba3SPoul-Henning Kamp 		m->m_pkthdr.len = totlen;
758c5789ba3SPoul-Henning Kamp }
759ce4a64f7SPoul-Henning Kamp 
76037621fd5SBruce M Simpson /*
76137621fd5SBruce M Simpson  * Apply function f to the data in an mbuf chain starting "off" bytes from
76237621fd5SBruce M Simpson  * the beginning, continuing for "len" bytes.
76337621fd5SBruce M Simpson  */
76437621fd5SBruce M Simpson int
76537621fd5SBruce M Simpson m_apply(struct mbuf *m, int off, int len,
76654065297SBruce M Simpson     int (*f)(void *, void *, u_int), void *arg)
76737621fd5SBruce M Simpson {
76854065297SBruce M Simpson 	u_int count;
76937621fd5SBruce M Simpson 	int rval;
77037621fd5SBruce M Simpson 
77137621fd5SBruce M Simpson 	KASSERT(off >= 0, ("m_apply, negative off %d", off));
77237621fd5SBruce M Simpson 	KASSERT(len >= 0, ("m_apply, negative len %d", len));
77337621fd5SBruce M Simpson 	while (off > 0) {
77437621fd5SBruce M Simpson 		KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
77537621fd5SBruce M Simpson 		if (off < m->m_len)
77637621fd5SBruce M Simpson 			break;
77737621fd5SBruce M Simpson 		off -= m->m_len;
77837621fd5SBruce M Simpson 		m = m->m_next;
77937621fd5SBruce M Simpson 	}
78037621fd5SBruce M Simpson 	while (len > 0) {
78137621fd5SBruce M Simpson 		KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
78237621fd5SBruce M Simpson 		count = min(m->m_len - off, len);
78337621fd5SBruce M Simpson 		rval = (*f)(arg, mtod(m, caddr_t) + off, count);
78437621fd5SBruce M Simpson 		if (rval)
78537621fd5SBruce M Simpson 			return (rval);
78637621fd5SBruce M Simpson 		len -= count;
78737621fd5SBruce M Simpson 		off = 0;
78837621fd5SBruce M Simpson 		m = m->m_next;
78937621fd5SBruce M Simpson 	}
79037621fd5SBruce M Simpson 	return (0);
79137621fd5SBruce M Simpson }
79237621fd5SBruce M Simpson 
79337621fd5SBruce M Simpson /*
79437621fd5SBruce M Simpson  * Return a pointer to mbuf/offset of location in mbuf chain.
79537621fd5SBruce M Simpson  */
79637621fd5SBruce M Simpson struct mbuf *
79737621fd5SBruce M Simpson m_getptr(struct mbuf *m, int loc, int *off)
79837621fd5SBruce M Simpson {
79937621fd5SBruce M Simpson 
80037621fd5SBruce M Simpson 	while (loc >= 0) {
80154065297SBruce M Simpson 		/* Normal end of search. */
80237621fd5SBruce M Simpson 		if (m->m_len > loc) {
80337621fd5SBruce M Simpson 			*off = loc;
80437621fd5SBruce M Simpson 			return (m);
80537621fd5SBruce M Simpson 		} else {
80637621fd5SBruce M Simpson 			loc -= m->m_len;
80737621fd5SBruce M Simpson 			if (m->m_next == NULL) {
80837621fd5SBruce M Simpson 				if (loc == 0) {
80954065297SBruce M Simpson 					/* Point at the end of valid data. */
81037621fd5SBruce M Simpson 					*off = m->m_len;
81137621fd5SBruce M Simpson 					return (m);
81254065297SBruce M Simpson 				}
81337621fd5SBruce M Simpson 				return (NULL);
81454065297SBruce M Simpson 			}
81537621fd5SBruce M Simpson 			m = m->m_next;
81637621fd5SBruce M Simpson 		}
81737621fd5SBruce M Simpson 	}
81837621fd5SBruce M Simpson 	return (NULL);
81937621fd5SBruce M Simpson }
82037621fd5SBruce M Simpson 
821ce4a64f7SPoul-Henning Kamp void
822ce4a64f7SPoul-Henning Kamp m_print(const struct mbuf *m)
823ce4a64f7SPoul-Henning Kamp {
824ce4a64f7SPoul-Henning Kamp 	int len;
8256357e7b5SEivind Eklund 	const struct mbuf *m2;
826ce4a64f7SPoul-Henning Kamp 
827ce4a64f7SPoul-Henning Kamp 	len = m->m_pkthdr.len;
828ce4a64f7SPoul-Henning Kamp 	m2 = m;
829ce4a64f7SPoul-Henning Kamp 	while (len) {
830ce4a64f7SPoul-Henning Kamp 		printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");
831ce4a64f7SPoul-Henning Kamp 		len -= m2->m_len;
832ce4a64f7SPoul-Henning Kamp 		m2 = m2->m_next;
833ce4a64f7SPoul-Henning Kamp 	}
834ce4a64f7SPoul-Henning Kamp 	return;
835ce4a64f7SPoul-Henning Kamp }
8363f2e06c5SPoul-Henning Kamp 
837bd395ae8SBosko Milekic u_int
8383f2e06c5SPoul-Henning Kamp m_fixhdr(struct mbuf *m0)
8393f2e06c5SPoul-Henning Kamp {
840bd395ae8SBosko Milekic 	u_int len;
8413f2e06c5SPoul-Henning Kamp 
842ac6e585dSPoul-Henning Kamp 	len = m_length(m0, NULL);
8433f2e06c5SPoul-Henning Kamp 	m0->m_pkthdr.len = len;
844ac6e585dSPoul-Henning Kamp 	return (len);
845ac6e585dSPoul-Henning Kamp }
846ac6e585dSPoul-Henning Kamp 
847bd395ae8SBosko Milekic u_int
848ac6e585dSPoul-Henning Kamp m_length(struct mbuf *m0, struct mbuf **last)
849ac6e585dSPoul-Henning Kamp {
850ac6e585dSPoul-Henning Kamp 	struct mbuf *m;
851bd395ae8SBosko Milekic 	u_int len;
852ac6e585dSPoul-Henning Kamp 
853ac6e585dSPoul-Henning Kamp 	len = 0;
854ac6e585dSPoul-Henning Kamp 	for (m = m0; m != NULL; m = m->m_next) {
855ac6e585dSPoul-Henning Kamp 		len += m->m_len;
856ac6e585dSPoul-Henning Kamp 		if (m->m_next == NULL)
857ac6e585dSPoul-Henning Kamp 			break;
858ac6e585dSPoul-Henning Kamp 	}
859ac6e585dSPoul-Henning Kamp 	if (last != NULL)
860ac6e585dSPoul-Henning Kamp 		*last = m;
861ac6e585dSPoul-Henning Kamp 	return (len);
8623f2e06c5SPoul-Henning Kamp }
86355e9f80dSMike Silbersack 
86455e9f80dSMike Silbersack /*
86555e9f80dSMike Silbersack  * Defragment a mbuf chain, returning the shortest possible
86655e9f80dSMike Silbersack  * chain of mbufs and clusters.  If allocation fails and
86755e9f80dSMike Silbersack  * this cannot be completed, NULL will be returned, but
86855e9f80dSMike Silbersack  * the passed in chain will be unchanged.  Upon success,
86955e9f80dSMike Silbersack  * the original chain will be freed, and the new chain
87055e9f80dSMike Silbersack  * will be returned.
87155e9f80dSMike Silbersack  *
87255e9f80dSMike Silbersack  * If a non-packet header is passed in, the original
87355e9f80dSMike Silbersack  * mbuf (chain?) will be returned unharmed.
87455e9f80dSMike Silbersack  */
87555e9f80dSMike Silbersack struct mbuf *
87655e9f80dSMike Silbersack m_defrag(struct mbuf *m0, int how)
87755e9f80dSMike Silbersack {
87855e9f80dSMike Silbersack 	struct mbuf	*m_new = NULL, *m_final = NULL;
87955e9f80dSMike Silbersack 	int		progress = 0, length;
88055e9f80dSMike Silbersack 
88155e9f80dSMike Silbersack 	if (!(m0->m_flags & M_PKTHDR))
88255e9f80dSMike Silbersack 		return (m0);
88355e9f80dSMike Silbersack 
884f8bf8e39SMike Silbersack 	m_fixhdr(m0); /* Needed sanity check */
885f8bf8e39SMike Silbersack 
886352d050eSMike Silbersack #ifdef MBUF_STRESS_TEST
887352d050eSMike Silbersack 	if (m_defragrandomfailures) {
888352d050eSMike Silbersack 		int temp = arc4random() & 0xff;
889352d050eSMike Silbersack 		if (temp == 0xba)
890352d050eSMike Silbersack 			goto nospace;
891352d050eSMike Silbersack 	}
892352d050eSMike Silbersack #endif
89355e9f80dSMike Silbersack 
89455e9f80dSMike Silbersack 	if (m0->m_pkthdr.len > MHLEN)
89555e9f80dSMike Silbersack 		m_final = m_getcl(how, MT_DATA, M_PKTHDR);
89655e9f80dSMike Silbersack 	else
89755e9f80dSMike Silbersack 		m_final = m_gethdr(how, MT_DATA);
89855e9f80dSMike Silbersack 
89955e9f80dSMike Silbersack 	if (m_final == NULL)
90055e9f80dSMike Silbersack 		goto nospace;
90155e9f80dSMike Silbersack 
902a89ec05eSPeter Wemm 	if (m_dup_pkthdr(m_final, m0, how) == 0)
90355e9f80dSMike Silbersack 		goto nospace;
90455e9f80dSMike Silbersack 
90555e9f80dSMike Silbersack 	m_new = m_final;
90655e9f80dSMike Silbersack 
90755e9f80dSMike Silbersack 	while (progress < m0->m_pkthdr.len) {
90855e9f80dSMike Silbersack 		length = m0->m_pkthdr.len - progress;
90955e9f80dSMike Silbersack 		if (length > MCLBYTES)
91055e9f80dSMike Silbersack 			length = MCLBYTES;
91155e9f80dSMike Silbersack 
91255e9f80dSMike Silbersack 		if (m_new == NULL) {
91355e9f80dSMike Silbersack 			if (length > MLEN)
91455e9f80dSMike Silbersack 				m_new = m_getcl(how, MT_DATA, 0);
91555e9f80dSMike Silbersack 			else
91655e9f80dSMike Silbersack 				m_new = m_get(how, MT_DATA);
91755e9f80dSMike Silbersack 			if (m_new == NULL)
91855e9f80dSMike Silbersack 				goto nospace;
91955e9f80dSMike Silbersack 		}
92055e9f80dSMike Silbersack 
92155e9f80dSMike Silbersack 		m_copydata(m0, progress, length, mtod(m_new, caddr_t));
92255e9f80dSMike Silbersack 		progress += length;
92355e9f80dSMike Silbersack 		m_new->m_len = length;
92455e9f80dSMike Silbersack 		if (m_new != m_final)
92555e9f80dSMike Silbersack 			m_cat(m_final, m_new);
92655e9f80dSMike Silbersack 		m_new = NULL;
92755e9f80dSMike Silbersack 	}
92851710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
92955e9f80dSMike Silbersack 	if (m0->m_next == NULL)
93055e9f80dSMike Silbersack 		m_defraguseless++;
93151710a45SMike Silbersack #endif
93255e9f80dSMike Silbersack 	m_freem(m0);
93355e9f80dSMike Silbersack 	m0 = m_final;
93451710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
93555e9f80dSMike Silbersack 	m_defragpackets++;
93655e9f80dSMike Silbersack 	m_defragbytes += m0->m_pkthdr.len;
93751710a45SMike Silbersack #endif
93855e9f80dSMike Silbersack 	return (m0);
93955e9f80dSMike Silbersack nospace:
94051710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
94155e9f80dSMike Silbersack 	m_defragfailure++;
94251710a45SMike Silbersack #endif
94355e9f80dSMike Silbersack 	if (m_new)
94455e9f80dSMike Silbersack 		m_free(m_new);
94555e9f80dSMike Silbersack 	if (m_final)
94655e9f80dSMike Silbersack 		m_freem(m_final);
94755e9f80dSMike Silbersack 	return (NULL);
94855e9f80dSMike Silbersack }
9493390d476SMike Silbersack 
9503390d476SMike Silbersack #ifdef MBUF_STRESS_TEST
9513390d476SMike Silbersack 
9523390d476SMike Silbersack /*
9533390d476SMike Silbersack  * Fragment an mbuf chain.  There's no reason you'd ever want to do
9543390d476SMike Silbersack  * this in normal usage, but it's great for stress testing various
9553390d476SMike Silbersack  * mbuf consumers.
9563390d476SMike Silbersack  *
9573390d476SMike Silbersack  * If fragmentation is not possible, the original chain will be
9583390d476SMike Silbersack  * returned.
9593390d476SMike Silbersack  *
9603390d476SMike Silbersack  * Possible length values:
9613390d476SMike Silbersack  * 0	 no fragmentation will occur
9623390d476SMike Silbersack  * > 0	each fragment will be of the specified length
9633390d476SMike Silbersack  * -1	each fragment will be the same random value in length
9643390d476SMike Silbersack  * -2	each fragment's length will be entirely random
9653390d476SMike Silbersack  * (Random values range from 1 to 256)
9663390d476SMike Silbersack  */
9673390d476SMike Silbersack struct mbuf *
9683390d476SMike Silbersack m_fragment(struct mbuf *m0, int how, int length)
9693390d476SMike Silbersack {
9703390d476SMike Silbersack 	struct mbuf	*m_new = NULL, *m_final = NULL;
9713390d476SMike Silbersack 	int		progress = 0;
9723390d476SMike Silbersack 
9733390d476SMike Silbersack 	if (!(m0->m_flags & M_PKTHDR))
9743390d476SMike Silbersack 		return (m0);
9753390d476SMike Silbersack 
9763390d476SMike Silbersack 	if ((length == 0) || (length < -2))
9773390d476SMike Silbersack 		return (m0);
9783390d476SMike Silbersack 
9793390d476SMike Silbersack 	m_fixhdr(m0); /* Needed sanity check */
9803390d476SMike Silbersack 
9813390d476SMike Silbersack 	m_final = m_getcl(how, MT_DATA, M_PKTHDR);
9823390d476SMike Silbersack 
9833390d476SMike Silbersack 	if (m_final == NULL)
9843390d476SMike Silbersack 		goto nospace;
9853390d476SMike Silbersack 
9868dee2f67SMike Silbersack 	if (m_dup_pkthdr(m_final, m0, how) == 0)
9873390d476SMike Silbersack 		goto nospace;
9883390d476SMike Silbersack 
9893390d476SMike Silbersack 	m_new = m_final;
9903390d476SMike Silbersack 
9913390d476SMike Silbersack 	if (length == -1)
9923390d476SMike Silbersack 		length = 1 + (arc4random() & 255);
9933390d476SMike Silbersack 
9943390d476SMike Silbersack 	while (progress < m0->m_pkthdr.len) {
9953390d476SMike Silbersack 		int fraglen;
9963390d476SMike Silbersack 
9973390d476SMike Silbersack 		if (length > 0)
9983390d476SMike Silbersack 			fraglen = length;
9993390d476SMike Silbersack 		else
10003390d476SMike Silbersack 			fraglen = 1 + (arc4random() & 255);
10013390d476SMike Silbersack 		if (fraglen > m0->m_pkthdr.len - progress)
10023390d476SMike Silbersack 			fraglen = m0->m_pkthdr.len - progress;
10033390d476SMike Silbersack 
10043390d476SMike Silbersack 		if (fraglen > MCLBYTES)
10053390d476SMike Silbersack 			fraglen = MCLBYTES;
10063390d476SMike Silbersack 
10073390d476SMike Silbersack 		if (m_new == NULL) {
10083390d476SMike Silbersack 			m_new = m_getcl(how, MT_DATA, 0);
10093390d476SMike Silbersack 			if (m_new == NULL)
10103390d476SMike Silbersack 				goto nospace;
10113390d476SMike Silbersack 		}
10123390d476SMike Silbersack 
10133390d476SMike Silbersack 		m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t));
10143390d476SMike Silbersack 		progress += fraglen;
10153390d476SMike Silbersack 		m_new->m_len = fraglen;
10163390d476SMike Silbersack 		if (m_new != m_final)
10173390d476SMike Silbersack 			m_cat(m_final, m_new);
10183390d476SMike Silbersack 		m_new = NULL;
10193390d476SMike Silbersack 	}
10203390d476SMike Silbersack 	m_freem(m0);
10213390d476SMike Silbersack 	m0 = m_final;
10223390d476SMike Silbersack 	return (m0);
10233390d476SMike Silbersack nospace:
10243390d476SMike Silbersack 	if (m_new)
10253390d476SMike Silbersack 		m_free(m_new);
10263390d476SMike Silbersack 	if (m_final)
10273390d476SMike Silbersack 		m_freem(m_final);
10283390d476SMike Silbersack 	/* Return the original chain on failure */
10293390d476SMike Silbersack 	return (m0);
10303390d476SMike Silbersack }
10313390d476SMike Silbersack 
10323390d476SMike Silbersack #endif
1033beb699c7SMike Silbersack 
1034beb699c7SMike Silbersack struct mbuf *
1035beb699c7SMike Silbersack m_uiotombuf(struct uio *uio, int how, int len)
1036beb699c7SMike Silbersack {
1037beb699c7SMike Silbersack 	struct mbuf	*m_new = NULL, *m_final = NULL;
1038beb699c7SMike Silbersack 	int		progress = 0, error = 0, length, total;
1039beb699c7SMike Silbersack 
1040beb699c7SMike Silbersack 	if (len > 0)
1041beb699c7SMike Silbersack 		total = min(uio->uio_resid, len);
1042beb699c7SMike Silbersack 	else
1043beb699c7SMike Silbersack 		total = uio->uio_resid;
1044beb699c7SMike Silbersack 
1045beb699c7SMike Silbersack 	if (total > MHLEN)
1046beb699c7SMike Silbersack 		m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1047beb699c7SMike Silbersack 	else
1048beb699c7SMike Silbersack 		m_final = m_gethdr(how, MT_DATA);
1049beb699c7SMike Silbersack 
1050beb699c7SMike Silbersack 	if (m_final == NULL)
1051beb699c7SMike Silbersack 		goto nospace;
1052beb699c7SMike Silbersack 
1053beb699c7SMike Silbersack 	m_new = m_final;
1054beb699c7SMike Silbersack 
1055beb699c7SMike Silbersack 	while (progress < total) {
1056beb699c7SMike Silbersack 		length = total - progress;
1057beb699c7SMike Silbersack 		if (length > MCLBYTES)
1058beb699c7SMike Silbersack 			length = MCLBYTES;
1059beb699c7SMike Silbersack 
1060beb699c7SMike Silbersack 		if (m_new == NULL) {
1061beb699c7SMike Silbersack 			if (length > MLEN)
1062beb699c7SMike Silbersack 				m_new = m_getcl(how, MT_DATA, 0);
1063beb699c7SMike Silbersack 			else
1064beb699c7SMike Silbersack 				m_new = m_get(how, MT_DATA);
1065beb699c7SMike Silbersack 			if (m_new == NULL)
1066beb699c7SMike Silbersack 				goto nospace;
1067beb699c7SMike Silbersack 		}
1068beb699c7SMike Silbersack 
1069beb699c7SMike Silbersack 		error = uiomove(mtod(m_new, void *), length, uio);
1070beb699c7SMike Silbersack 		if (error)
1071beb699c7SMike Silbersack 			goto nospace;
1072beb699c7SMike Silbersack 		progress += length;
1073beb699c7SMike Silbersack 		m_new->m_len = length;
1074beb699c7SMike Silbersack 		if (m_new != m_final)
1075beb699c7SMike Silbersack 			m_cat(m_final, m_new);
1076beb699c7SMike Silbersack 		m_new = NULL;
1077beb699c7SMike Silbersack 	}
1078beb699c7SMike Silbersack 	m_fixhdr(m_final);
1079beb699c7SMike Silbersack 	return (m_final);
1080beb699c7SMike Silbersack nospace:
1081beb699c7SMike Silbersack 	if (m_new)
1082beb699c7SMike Silbersack 		m_free(m_new);
1083beb699c7SMike Silbersack 	if (m_final)
1084beb699c7SMike Silbersack 		m_freem(m_final);
1085beb699c7SMike Silbersack 	return (NULL);
1086beb699c7SMike Silbersack }
1087