xref: /freebsd/sys/kern/uipc_mbuf.c (revision 37621fd5d93015b59e08fc1278cbabfbf393fa7b)
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>
46fb919e4dSMark Murray #include <sys/lock.h>
47e32a5b94SRobert Watson #include <sys/mac.h>
48f9d0d524SRobert Watson #include <sys/malloc.h>
49df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
50639acc13SGarrett Wollman #include <sys/sysctl.h>
51df8bae1dSRodney W. Grimes #include <sys/domain.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
53fb919e4dSMark Murray 
5428f8db14SBruce Evans int	max_linkhdr;
5528f8db14SBruce Evans int	max_protohdr;
5628f8db14SBruce Evans int	max_hdr;
5728f8db14SBruce Evans int	max_datalen;
5851710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
5955e9f80dSMike Silbersack int	m_defragpackets;
6055e9f80dSMike Silbersack int	m_defragbytes;
6155e9f80dSMike Silbersack int	m_defraguseless;
6255e9f80dSMike Silbersack int	m_defragfailure;
63352d050eSMike Silbersack int	m_defragrandomfailures;
64352d050eSMike Silbersack #endif
657d032714SBosko Milekic 
667d032714SBosko Milekic /*
677d032714SBosko Milekic  * sysctl(8) exported objects
687d032714SBosko Milekic  */
69ce02431fSDoug Rabson SYSCTL_DECL(_kern_ipc);
70639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
71639acc13SGarrett Wollman 	   &max_linkhdr, 0, "");
72639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
73639acc13SGarrett Wollman 	   &max_protohdr, 0, "");
74639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
75639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
76639acc13SGarrett Wollman 	   &max_datalen, 0, "");
7751710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
7855e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
7955e9f80dSMike Silbersack 	   &m_defragpackets, 0, "");
8055e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
8155e9f80dSMike Silbersack 	   &m_defragbytes, 0, "");
8255e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
8355e9f80dSMike Silbersack 	   &m_defraguseless, 0, "");
8455e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
8555e9f80dSMike Silbersack 	   &m_defragfailure, 0, "");
86352d050eSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
87352d050eSMike Silbersack 	   &m_defragrandomfailures, 0, "");
88352d050eSMike Silbersack #endif
89df8bae1dSRodney W. Grimes 
90df8bae1dSRodney W. Grimes /*
919967cafcSSam Leffler  * "Move" mbuf pkthdr from "from" to "to".
92e37b1fcdSRobert Watson  * "from" must have M_PKTHDR set, and "to" must be empty.
93e37b1fcdSRobert Watson  */
94e37b1fcdSRobert Watson void
959967cafcSSam Leffler m_move_pkthdr(struct mbuf *to, struct mbuf *from)
96e37b1fcdSRobert Watson {
97e37b1fcdSRobert Watson 
98e37b1fcdSRobert Watson #if 0
999967cafcSSam Leffler 	/* see below for why these are not enabled */
100fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(to);
101225bff6fSRobert Watson 	/* Note: with MAC, this may not be a good assertion. */
1029967cafcSSam Leffler 	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
1039967cafcSSam Leffler 	    ("m_move_pkthdr: to has tags"));
104e37b1fcdSRobert Watson #endif
1059967cafcSSam Leffler 	KASSERT((to->m_flags & M_EXT) == 0, ("m_move_pkthdr: to has cluster"));
106e32a5b94SRobert Watson #ifdef MAC
107225bff6fSRobert Watson 	/*
108225bff6fSRobert Watson 	 * XXXMAC: It could be this should also occur for non-MAC?
109225bff6fSRobert Watson 	 */
110e32a5b94SRobert Watson 	if (to->m_flags & M_PKTHDR)
111225bff6fSRobert Watson 		m_tag_delete_chain(to, NULL);
112e32a5b94SRobert Watson #endif
113e37b1fcdSRobert Watson 	to->m_flags = from->m_flags & M_COPYFLAGS;
1149967cafcSSam Leffler 	to->m_data = to->m_pktdat;
1159967cafcSSam Leffler 	to->m_pkthdr = from->m_pkthdr;		/* especially tags */
1169967cafcSSam Leffler 	SLIST_INIT(&from->m_pkthdr.tags);	/* purge tags from src */
1179967cafcSSam Leffler 	from->m_flags &= ~M_PKTHDR;
1189967cafcSSam Leffler }
1199967cafcSSam Leffler 
1209967cafcSSam Leffler /*
1219967cafcSSam Leffler  * Duplicate "from"'s mbuf pkthdr in "to".
1229967cafcSSam Leffler  * "from" must have M_PKTHDR set, and "to" must be empty.
1239967cafcSSam Leffler  * In particular, this does a deep copy of the packet tags.
1249967cafcSSam Leffler  */
1259967cafcSSam Leffler int
1269967cafcSSam Leffler m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
1279967cafcSSam Leffler {
1289967cafcSSam Leffler 
1299967cafcSSam Leffler #if 0
1309967cafcSSam Leffler 	/*
1319967cafcSSam Leffler 	 * The mbuf allocator only initializes the pkthdr
1329967cafcSSam Leffler 	 * when the mbuf is allocated with MGETHDR. Many users
1339967cafcSSam Leffler 	 * (e.g. m_copy*, m_prepend) use MGET and then
1349967cafcSSam Leffler 	 * smash the pkthdr as needed causing these
1359967cafcSSam Leffler 	 * assertions to trip.  For now just disable them.
1369967cafcSSam Leffler 	 */
137fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(to);
138225bff6fSRobert Watson 	/* Note: with MAC, this may not be a good assertion. */
1399967cafcSSam Leffler 	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
1409967cafcSSam Leffler #endif
1419967cafcSSam Leffler #ifdef MAC
1429967cafcSSam Leffler 	if (to->m_flags & M_PKTHDR)
143225bff6fSRobert Watson 		m_tag_delete_chain(to, NULL);
1449967cafcSSam Leffler #endif
145df8c7fc9SMike Silbersack 	to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
146df8c7fc9SMike Silbersack 	if ((to->m_flags & M_EXT) == 0)
1479967cafcSSam Leffler 		to->m_data = to->m_pktdat;
148e37b1fcdSRobert Watson 	to->m_pkthdr = from->m_pkthdr;
1499967cafcSSam Leffler 	SLIST_INIT(&to->m_pkthdr.tags);
150aa65d9f5SRobert Watson 	return (m_tag_copy_chain(to, from, MBTOM(how)));
151e37b1fcdSRobert Watson }
152e37b1fcdSRobert Watson 
153e37b1fcdSRobert Watson /*
154df8bae1dSRodney W. Grimes  * Lesser-used path for M_PREPEND:
155df8bae1dSRodney W. Grimes  * allocate new mbuf to prepend to chain,
156df8bae1dSRodney W. Grimes  * copy junk along.
157df8bae1dSRodney W. Grimes  */
158df8bae1dSRodney W. Grimes struct mbuf *
159122a814aSBosko Milekic m_prepend(struct mbuf *m, int len, int how)
160df8bae1dSRodney W. Grimes {
161df8bae1dSRodney W. Grimes 	struct mbuf *mn;
162df8bae1dSRodney W. Grimes 
163f8bf8e39SMike Silbersack 	if (m->m_flags & M_PKTHDR)
164f8bf8e39SMike Silbersack 		MGETHDR(mn, how, m->m_type);
165f8bf8e39SMike Silbersack 	else
166df8bae1dSRodney W. Grimes 		MGET(mn, how, m->m_type);
167122a814aSBosko Milekic 	if (mn == NULL) {
168df8bae1dSRodney W. Grimes 		m_freem(m);
169122a814aSBosko Milekic 		return (NULL);
170df8bae1dSRodney W. Grimes 	}
171225bff6fSRobert Watson 	if (m->m_flags & M_PKTHDR)
1729967cafcSSam Leffler 		M_MOVE_PKTHDR(mn, m);
173df8bae1dSRodney W. Grimes 	mn->m_next = m;
174df8bae1dSRodney W. Grimes 	m = mn;
175df8bae1dSRodney W. Grimes 	if (len < MHLEN)
176df8bae1dSRodney W. Grimes 		MH_ALIGN(m, len);
177df8bae1dSRodney W. Grimes 	m->m_len = len;
178df8bae1dSRodney W. Grimes 	return (m);
179df8bae1dSRodney W. Grimes }
180df8bae1dSRodney W. Grimes 
181df8bae1dSRodney W. Grimes /*
182df8bae1dSRodney W. Grimes  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
183df8bae1dSRodney W. Grimes  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
184a163d034SWarner Losh  * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
1851c38f2eaSArchie Cobbs  * Note that the copy is read-only, because clusters are not copied,
1861c38f2eaSArchie Cobbs  * only their reference counts are incremented.
187df8bae1dSRodney W. Grimes  */
188df8bae1dSRodney W. Grimes struct mbuf *
189122a814aSBosko Milekic m_copym(struct mbuf *m, int off0, int len, int wait)
190df8bae1dSRodney W. Grimes {
191122a814aSBosko Milekic 	struct mbuf *n, **np;
192122a814aSBosko Milekic 	int off = off0;
193df8bae1dSRodney W. Grimes 	struct mbuf *top;
194df8bae1dSRodney W. Grimes 	int copyhdr = 0;
195df8bae1dSRodney W. Grimes 
196e0a653ddSAlfred Perlstein 	KASSERT(off >= 0, ("m_copym, negative off %d", off));
197e0a653ddSAlfred Perlstein 	KASSERT(len >= 0, ("m_copym, negative len %d", len));
198df8bae1dSRodney W. Grimes 	if (off == 0 && m->m_flags & M_PKTHDR)
199df8bae1dSRodney W. Grimes 		copyhdr = 1;
200df8bae1dSRodney W. Grimes 	while (off > 0) {
201e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
202df8bae1dSRodney W. Grimes 		if (off < m->m_len)
203df8bae1dSRodney W. Grimes 			break;
204df8bae1dSRodney W. Grimes 		off -= m->m_len;
205df8bae1dSRodney W. Grimes 		m = m->m_next;
206df8bae1dSRodney W. Grimes 	}
207df8bae1dSRodney W. Grimes 	np = &top;
208df8bae1dSRodney W. Grimes 	top = 0;
209df8bae1dSRodney W. Grimes 	while (len > 0) {
210122a814aSBosko Milekic 		if (m == NULL) {
211e0a653ddSAlfred Perlstein 			KASSERT(len == M_COPYALL,
212e0a653ddSAlfred Perlstein 			    ("m_copym, length > size of mbuf chain"));
213df8bae1dSRodney W. Grimes 			break;
214df8bae1dSRodney W. Grimes 		}
215f8bf8e39SMike Silbersack 		if (copyhdr)
216f8bf8e39SMike Silbersack 			MGETHDR(n, wait, m->m_type);
217f8bf8e39SMike Silbersack 		else
218df8bae1dSRodney W. Grimes 			MGET(n, wait, m->m_type);
219df8bae1dSRodney W. Grimes 		*np = n;
220122a814aSBosko Milekic 		if (n == NULL)
221df8bae1dSRodney W. Grimes 			goto nospace;
222df8bae1dSRodney W. Grimes 		if (copyhdr) {
2239967cafcSSam Leffler 			if (!m_dup_pkthdr(n, m, wait))
2249967cafcSSam Leffler 				goto nospace;
225df8bae1dSRodney W. Grimes 			if (len == M_COPYALL)
226df8bae1dSRodney W. Grimes 				n->m_pkthdr.len -= off0;
227df8bae1dSRodney W. Grimes 			else
228df8bae1dSRodney W. Grimes 				n->m_pkthdr.len = len;
229df8bae1dSRodney W. Grimes 			copyhdr = 0;
230df8bae1dSRodney W. Grimes 		}
231df8bae1dSRodney W. Grimes 		n->m_len = min(len, m->m_len - off);
232df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT) {
233df8bae1dSRodney W. Grimes 			n->m_data = m->m_data + off;
234df8bae1dSRodney W. Grimes 			n->m_ext = m->m_ext;
235df8bae1dSRodney W. Grimes 			n->m_flags |= M_EXT;
236a5c4836dSDavid Malone 			MEXT_ADD_REF(m);
237df8bae1dSRodney W. Grimes 		} else
238df8bae1dSRodney W. Grimes 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
239bd395ae8SBosko Milekic 			    (u_int)n->m_len);
240df8bae1dSRodney W. Grimes 		if (len != M_COPYALL)
241df8bae1dSRodney W. Grimes 			len -= n->m_len;
242df8bae1dSRodney W. Grimes 		off = 0;
243df8bae1dSRodney W. Grimes 		m = m->m_next;
244df8bae1dSRodney W. Grimes 		np = &n->m_next;
245df8bae1dSRodney W. Grimes 	}
24608442f8aSBosko Milekic 	if (top == NULL)
24708442f8aSBosko Milekic 		mbstat.m_mcfail++;	/* XXX: No consistency. */
24808442f8aSBosko Milekic 
249df8bae1dSRodney W. Grimes 	return (top);
250df8bae1dSRodney W. Grimes nospace:
251df8bae1dSRodney W. Grimes 	m_freem(top);
25208442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
253122a814aSBosko Milekic 	return (NULL);
254df8bae1dSRodney W. Grimes }
255df8bae1dSRodney W. Grimes 
256df8bae1dSRodney W. Grimes /*
2576a06dea0SGarrett Wollman  * Copy an entire packet, including header (which must be present).
2586a06dea0SGarrett Wollman  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
2591c38f2eaSArchie Cobbs  * Note that the copy is read-only, because clusters are not copied,
2601c38f2eaSArchie Cobbs  * only their reference counts are incremented.
2615fe86675SLuigi Rizzo  * Preserve alignment of the first mbuf so if the creator has left
2625fe86675SLuigi Rizzo  * some room at the beginning (e.g. for inserting protocol headers)
2635fe86675SLuigi Rizzo  * the copies still have the room available.
2646a06dea0SGarrett Wollman  */
2656a06dea0SGarrett Wollman struct mbuf *
266122a814aSBosko Milekic m_copypacket(struct mbuf *m, int how)
2676a06dea0SGarrett Wollman {
2686a06dea0SGarrett Wollman 	struct mbuf *top, *n, *o;
2696a06dea0SGarrett Wollman 
2706a06dea0SGarrett Wollman 	MGET(n, how, m->m_type);
2716a06dea0SGarrett Wollman 	top = n;
272122a814aSBosko Milekic 	if (n == NULL)
2736a06dea0SGarrett Wollman 		goto nospace;
2746a06dea0SGarrett Wollman 
2759967cafcSSam Leffler 	if (!m_dup_pkthdr(n, m, how))
2769967cafcSSam Leffler 		goto nospace;
2776a06dea0SGarrett Wollman 	n->m_len = m->m_len;
2786a06dea0SGarrett Wollman 	if (m->m_flags & M_EXT) {
2796a06dea0SGarrett Wollman 		n->m_data = m->m_data;
2806a06dea0SGarrett Wollman 		n->m_ext = m->m_ext;
2816a06dea0SGarrett Wollman 		n->m_flags |= M_EXT;
282a5c4836dSDavid Malone 		MEXT_ADD_REF(m);
2836a06dea0SGarrett Wollman 	} else {
2845fe86675SLuigi Rizzo 		n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
2856a06dea0SGarrett Wollman 		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
2866a06dea0SGarrett Wollman 	}
2876a06dea0SGarrett Wollman 
2886a06dea0SGarrett Wollman 	m = m->m_next;
2896a06dea0SGarrett Wollman 	while (m) {
2906a06dea0SGarrett Wollman 		MGET(o, how, m->m_type);
291122a814aSBosko Milekic 		if (o == NULL)
2926a06dea0SGarrett Wollman 			goto nospace;
2936a06dea0SGarrett Wollman 
2946a06dea0SGarrett Wollman 		n->m_next = o;
2956a06dea0SGarrett Wollman 		n = n->m_next;
2966a06dea0SGarrett Wollman 
2976a06dea0SGarrett Wollman 		n->m_len = m->m_len;
2986a06dea0SGarrett Wollman 		if (m->m_flags & M_EXT) {
2996a06dea0SGarrett Wollman 			n->m_data = m->m_data;
3006a06dea0SGarrett Wollman 			n->m_ext = m->m_ext;
3016a06dea0SGarrett Wollman 			n->m_flags |= M_EXT;
302a5c4836dSDavid Malone 			MEXT_ADD_REF(m);
3036a06dea0SGarrett Wollman 		} else {
3046a06dea0SGarrett Wollman 			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
3056a06dea0SGarrett Wollman 		}
3066a06dea0SGarrett Wollman 
3076a06dea0SGarrett Wollman 		m = m->m_next;
3086a06dea0SGarrett Wollman 	}
3096a06dea0SGarrett Wollman 	return top;
3106a06dea0SGarrett Wollman nospace:
3116a06dea0SGarrett Wollman 	m_freem(top);
31208442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
313122a814aSBosko Milekic 	return (NULL);
3146a06dea0SGarrett Wollman }
3156a06dea0SGarrett Wollman 
3166a06dea0SGarrett Wollman /*
317df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain starting "off" bytes from the beginning,
318df8bae1dSRodney W. Grimes  * continuing for "len" bytes, into the indicated buffer.
319df8bae1dSRodney W. Grimes  */
32026f9a767SRodney W. Grimes void
321a8cfc0eeSJulian Elischer m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
322df8bae1dSRodney W. Grimes {
323bd395ae8SBosko Milekic 	u_int count;
324df8bae1dSRodney W. Grimes 
325e0a653ddSAlfred Perlstein 	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
326e0a653ddSAlfred Perlstein 	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
327df8bae1dSRodney W. Grimes 	while (off > 0) {
328e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
329df8bae1dSRodney W. Grimes 		if (off < m->m_len)
330df8bae1dSRodney W. Grimes 			break;
331df8bae1dSRodney W. Grimes 		off -= m->m_len;
332df8bae1dSRodney W. Grimes 		m = m->m_next;
333df8bae1dSRodney W. Grimes 	}
334df8bae1dSRodney W. Grimes 	while (len > 0) {
335e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
336df8bae1dSRodney W. Grimes 		count = min(m->m_len - off, len);
337df8bae1dSRodney W. Grimes 		bcopy(mtod(m, caddr_t) + off, cp, count);
338df8bae1dSRodney W. Grimes 		len -= count;
339df8bae1dSRodney W. Grimes 		cp += count;
340df8bae1dSRodney W. Grimes 		off = 0;
341df8bae1dSRodney W. Grimes 		m = m->m_next;
342df8bae1dSRodney W. Grimes 	}
343df8bae1dSRodney W. Grimes }
344df8bae1dSRodney W. Grimes 
345df8bae1dSRodney W. Grimes /*
3461c38f2eaSArchie Cobbs  * Copy a packet header mbuf chain into a completely new chain, including
3471c38f2eaSArchie Cobbs  * copying any mbuf clusters.  Use this instead of m_copypacket() when
3481c38f2eaSArchie Cobbs  * you need a writable copy of an mbuf chain.
3491c38f2eaSArchie Cobbs  */
3501c38f2eaSArchie Cobbs struct mbuf *
351122a814aSBosko Milekic m_dup(struct mbuf *m, int how)
3521c38f2eaSArchie Cobbs {
3531c38f2eaSArchie Cobbs 	struct mbuf **p, *top = NULL;
3541c38f2eaSArchie Cobbs 	int remain, moff, nsize;
3551c38f2eaSArchie Cobbs 
3561c38f2eaSArchie Cobbs 	/* Sanity check */
3571c38f2eaSArchie Cobbs 	if (m == NULL)
358122a814aSBosko Milekic 		return (NULL);
359fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
3601c38f2eaSArchie Cobbs 
3611c38f2eaSArchie Cobbs 	/* While there's more data, get a new mbuf, tack it on, and fill it */
3621c38f2eaSArchie Cobbs 	remain = m->m_pkthdr.len;
3631c38f2eaSArchie Cobbs 	moff = 0;
3641c38f2eaSArchie Cobbs 	p = &top;
3651c38f2eaSArchie Cobbs 	while (remain > 0 || top == NULL) {	/* allow m->m_pkthdr.len == 0 */
3661c38f2eaSArchie Cobbs 		struct mbuf *n;
3671c38f2eaSArchie Cobbs 
3681c38f2eaSArchie Cobbs 		/* Get the next new mbuf */
3691c38f2eaSArchie Cobbs 		MGET(n, how, m->m_type);
3701c38f2eaSArchie Cobbs 		if (n == NULL)
3711c38f2eaSArchie Cobbs 			goto nospace;
3721c38f2eaSArchie Cobbs 		if (top == NULL) {		/* first one, must be PKTHDR */
3739967cafcSSam Leffler 			if (!m_dup_pkthdr(n, m, how))
3749967cafcSSam Leffler 				goto nospace;
3751c38f2eaSArchie Cobbs 			nsize = MHLEN;
3761c38f2eaSArchie Cobbs 		} else				/* not the first one */
3771c38f2eaSArchie Cobbs 			nsize = MLEN;
3781c38f2eaSArchie Cobbs 		if (remain >= MINCLSIZE) {
3791c38f2eaSArchie Cobbs 			MCLGET(n, how);
3801c38f2eaSArchie Cobbs 			if ((n->m_flags & M_EXT) == 0) {
3811c38f2eaSArchie Cobbs 				(void)m_free(n);
3821c38f2eaSArchie Cobbs 				goto nospace;
3831c38f2eaSArchie Cobbs 			}
3841c38f2eaSArchie Cobbs 			nsize = MCLBYTES;
3851c38f2eaSArchie Cobbs 		}
3861c38f2eaSArchie Cobbs 		n->m_len = 0;
3871c38f2eaSArchie Cobbs 
3881c38f2eaSArchie Cobbs 		/* Link it into the new chain */
3891c38f2eaSArchie Cobbs 		*p = n;
3901c38f2eaSArchie Cobbs 		p = &n->m_next;
3911c38f2eaSArchie Cobbs 
3921c38f2eaSArchie Cobbs 		/* Copy data from original mbuf(s) into new mbuf */
3931c38f2eaSArchie Cobbs 		while (n->m_len < nsize && m != NULL) {
3941c38f2eaSArchie Cobbs 			int chunk = min(nsize - n->m_len, m->m_len - moff);
3951c38f2eaSArchie Cobbs 
3961c38f2eaSArchie Cobbs 			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
3971c38f2eaSArchie Cobbs 			moff += chunk;
3981c38f2eaSArchie Cobbs 			n->m_len += chunk;
3991c38f2eaSArchie Cobbs 			remain -= chunk;
4001c38f2eaSArchie Cobbs 			if (moff == m->m_len) {
4011c38f2eaSArchie Cobbs 				m = m->m_next;
4021c38f2eaSArchie Cobbs 				moff = 0;
4031c38f2eaSArchie Cobbs 			}
4041c38f2eaSArchie Cobbs 		}
4051c38f2eaSArchie Cobbs 
4061c38f2eaSArchie Cobbs 		/* Check correct total mbuf length */
4071c38f2eaSArchie Cobbs 		KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
408a48740b6SDavid E. O'Brien 		    	("%s: bogus m_pkthdr.len", __func__));
4091c38f2eaSArchie Cobbs 	}
4101c38f2eaSArchie Cobbs 	return (top);
4111c38f2eaSArchie Cobbs 
4121c38f2eaSArchie Cobbs nospace:
4131c38f2eaSArchie Cobbs 	m_freem(top);
41408442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
415122a814aSBosko Milekic 	return (NULL);
4161c38f2eaSArchie Cobbs }
4171c38f2eaSArchie Cobbs 
4181c38f2eaSArchie Cobbs /*
419df8bae1dSRodney W. Grimes  * Concatenate mbuf chain n to m.
420df8bae1dSRodney W. Grimes  * Both chains must be of the same type (e.g. MT_DATA).
421df8bae1dSRodney W. Grimes  * Any m_pkthdr is not updated.
422df8bae1dSRodney W. Grimes  */
42326f9a767SRodney W. Grimes void
424122a814aSBosko Milekic m_cat(struct mbuf *m, struct mbuf *n)
425df8bae1dSRodney W. Grimes {
426df8bae1dSRodney W. Grimes 	while (m->m_next)
427df8bae1dSRodney W. Grimes 		m = m->m_next;
428df8bae1dSRodney W. Grimes 	while (n) {
429df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT ||
430df8bae1dSRodney W. Grimes 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
431df8bae1dSRodney W. Grimes 			/* just join the two chains */
432df8bae1dSRodney W. Grimes 			m->m_next = n;
433df8bae1dSRodney W. Grimes 			return;
434df8bae1dSRodney W. Grimes 		}
435df8bae1dSRodney W. Grimes 		/* splat the data from one into the other */
436df8bae1dSRodney W. Grimes 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
437df8bae1dSRodney W. Grimes 		    (u_int)n->m_len);
438df8bae1dSRodney W. Grimes 		m->m_len += n->m_len;
439df8bae1dSRodney W. Grimes 		n = m_free(n);
440df8bae1dSRodney W. Grimes 	}
441df8bae1dSRodney W. Grimes }
442df8bae1dSRodney W. Grimes 
44326f9a767SRodney W. Grimes void
444122a814aSBosko Milekic m_adj(struct mbuf *mp, int req_len)
445df8bae1dSRodney W. Grimes {
446122a814aSBosko Milekic 	int len = req_len;
447122a814aSBosko Milekic 	struct mbuf *m;
448122a814aSBosko Milekic 	int count;
449df8bae1dSRodney W. Grimes 
450df8bae1dSRodney W. Grimes 	if ((m = mp) == NULL)
451df8bae1dSRodney W. Grimes 		return;
452df8bae1dSRodney W. Grimes 	if (len >= 0) {
453df8bae1dSRodney W. Grimes 		/*
454df8bae1dSRodney W. Grimes 		 * Trim from head.
455df8bae1dSRodney W. Grimes 		 */
456df8bae1dSRodney W. Grimes 		while (m != NULL && len > 0) {
457df8bae1dSRodney W. Grimes 			if (m->m_len <= len) {
458df8bae1dSRodney W. Grimes 				len -= m->m_len;
459df8bae1dSRodney W. Grimes 				m->m_len = 0;
460df8bae1dSRodney W. Grimes 				m = m->m_next;
461df8bae1dSRodney W. Grimes 			} else {
462df8bae1dSRodney W. Grimes 				m->m_len -= len;
463df8bae1dSRodney W. Grimes 				m->m_data += len;
464df8bae1dSRodney W. Grimes 				len = 0;
465df8bae1dSRodney W. Grimes 			}
466df8bae1dSRodney W. Grimes 		}
467df8bae1dSRodney W. Grimes 		m = mp;
468df8bae1dSRodney W. Grimes 		if (mp->m_flags & M_PKTHDR)
469df8bae1dSRodney W. Grimes 			m->m_pkthdr.len -= (req_len - len);
470df8bae1dSRodney W. Grimes 	} else {
471df8bae1dSRodney W. Grimes 		/*
472df8bae1dSRodney W. Grimes 		 * Trim from tail.  Scan the mbuf chain,
473df8bae1dSRodney W. Grimes 		 * calculating its length and finding the last mbuf.
474df8bae1dSRodney W. Grimes 		 * If the adjustment only affects this mbuf, then just
475df8bae1dSRodney W. Grimes 		 * adjust and return.  Otherwise, rescan and truncate
476df8bae1dSRodney W. Grimes 		 * after the remaining size.
477df8bae1dSRodney W. Grimes 		 */
478df8bae1dSRodney W. Grimes 		len = -len;
479df8bae1dSRodney W. Grimes 		count = 0;
480df8bae1dSRodney W. Grimes 		for (;;) {
481df8bae1dSRodney W. Grimes 			count += m->m_len;
482df8bae1dSRodney W. Grimes 			if (m->m_next == (struct mbuf *)0)
483df8bae1dSRodney W. Grimes 				break;
484df8bae1dSRodney W. Grimes 			m = m->m_next;
485df8bae1dSRodney W. Grimes 		}
486df8bae1dSRodney W. Grimes 		if (m->m_len >= len) {
487df8bae1dSRodney W. Grimes 			m->m_len -= len;
488df8bae1dSRodney W. Grimes 			if (mp->m_flags & M_PKTHDR)
489df8bae1dSRodney W. Grimes 				mp->m_pkthdr.len -= len;
490df8bae1dSRodney W. Grimes 			return;
491df8bae1dSRodney W. Grimes 		}
492df8bae1dSRodney W. Grimes 		count -= len;
493df8bae1dSRodney W. Grimes 		if (count < 0)
494df8bae1dSRodney W. Grimes 			count = 0;
495df8bae1dSRodney W. Grimes 		/*
496df8bae1dSRodney W. Grimes 		 * Correct length for chain is "count".
497df8bae1dSRodney W. Grimes 		 * Find the mbuf with last data, adjust its length,
498df8bae1dSRodney W. Grimes 		 * and toss data from remaining mbufs on chain.
499df8bae1dSRodney W. Grimes 		 */
500df8bae1dSRodney W. Grimes 		m = mp;
501df8bae1dSRodney W. Grimes 		if (m->m_flags & M_PKTHDR)
502df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = count;
503df8bae1dSRodney W. Grimes 		for (; m; m = m->m_next) {
504df8bae1dSRodney W. Grimes 			if (m->m_len >= count) {
505df8bae1dSRodney W. Grimes 				m->m_len = count;
506df8bae1dSRodney W. Grimes 				break;
507df8bae1dSRodney W. Grimes 			}
508df8bae1dSRodney W. Grimes 			count -= m->m_len;
509df8bae1dSRodney W. Grimes 		}
510797f2d22SPoul-Henning Kamp 		while (m->m_next)
511797f2d22SPoul-Henning Kamp 			(m = m->m_next) ->m_len = 0;
512df8bae1dSRodney W. Grimes 	}
513df8bae1dSRodney W. Grimes }
514df8bae1dSRodney W. Grimes 
515df8bae1dSRodney W. Grimes /*
516df8bae1dSRodney W. Grimes  * Rearange an mbuf chain so that len bytes are contiguous
517df8bae1dSRodney W. Grimes  * and in the data area of an mbuf (so that mtod and dtom
518df8bae1dSRodney W. Grimes  * will work for a structure of size len).  Returns the resulting
519df8bae1dSRodney W. Grimes  * mbuf chain on success, frees it and returns null on failure.
520df8bae1dSRodney W. Grimes  * If there is room, it will add up to max_protohdr-len extra bytes to the
521df8bae1dSRodney W. Grimes  * contiguous region in an attempt to avoid being called next time.
522df8bae1dSRodney W. Grimes  */
523df8bae1dSRodney W. Grimes struct mbuf *
524122a814aSBosko Milekic m_pullup(struct mbuf *n, int len)
525df8bae1dSRodney W. Grimes {
526122a814aSBosko Milekic 	struct mbuf *m;
527122a814aSBosko Milekic 	int count;
528df8bae1dSRodney W. Grimes 	int space;
529df8bae1dSRodney W. Grimes 
530df8bae1dSRodney W. Grimes 	/*
531df8bae1dSRodney W. Grimes 	 * If first mbuf has no cluster, and has room for len bytes
532df8bae1dSRodney W. Grimes 	 * without shifting current data, pullup into it,
533df8bae1dSRodney W. Grimes 	 * otherwise allocate a new mbuf to prepend to the chain.
534df8bae1dSRodney W. Grimes 	 */
535df8bae1dSRodney W. Grimes 	if ((n->m_flags & M_EXT) == 0 &&
536df8bae1dSRodney W. Grimes 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
537df8bae1dSRodney W. Grimes 		if (n->m_len >= len)
538df8bae1dSRodney W. Grimes 			return (n);
539df8bae1dSRodney W. Grimes 		m = n;
540df8bae1dSRodney W. Grimes 		n = n->m_next;
541df8bae1dSRodney W. Grimes 		len -= m->m_len;
542df8bae1dSRodney W. Grimes 	} else {
543df8bae1dSRodney W. Grimes 		if (len > MHLEN)
544df8bae1dSRodney W. Grimes 			goto bad;
545a163d034SWarner Losh 		MGET(m, M_DONTWAIT, n->m_type);
546122a814aSBosko Milekic 		if (m == NULL)
547df8bae1dSRodney W. Grimes 			goto bad;
548df8bae1dSRodney W. Grimes 		m->m_len = 0;
5499967cafcSSam Leffler 		if (n->m_flags & M_PKTHDR)
5509967cafcSSam Leffler 			M_MOVE_PKTHDR(m, n);
551df8bae1dSRodney W. Grimes 	}
552df8bae1dSRodney W. Grimes 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
553df8bae1dSRodney W. Grimes 	do {
554df8bae1dSRodney W. Grimes 		count = min(min(max(len, max_protohdr), space), n->m_len);
555df8bae1dSRodney W. Grimes 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
556bd395ae8SBosko Milekic 		  (u_int)count);
557df8bae1dSRodney W. Grimes 		len -= count;
558df8bae1dSRodney W. Grimes 		m->m_len += count;
559df8bae1dSRodney W. Grimes 		n->m_len -= count;
560df8bae1dSRodney W. Grimes 		space -= count;
561df8bae1dSRodney W. Grimes 		if (n->m_len)
562df8bae1dSRodney W. Grimes 			n->m_data += count;
563df8bae1dSRodney W. Grimes 		else
564df8bae1dSRodney W. Grimes 			n = m_free(n);
565df8bae1dSRodney W. Grimes 	} while (len > 0 && n);
566df8bae1dSRodney W. Grimes 	if (len > 0) {
567df8bae1dSRodney W. Grimes 		(void) m_free(m);
568df8bae1dSRodney W. Grimes 		goto bad;
569df8bae1dSRodney W. Grimes 	}
570df8bae1dSRodney W. Grimes 	m->m_next = n;
571df8bae1dSRodney W. Grimes 	return (m);
572df8bae1dSRodney W. Grimes bad:
573df8bae1dSRodney W. Grimes 	m_freem(n);
57408442f8aSBosko Milekic 	mbstat.m_mpfail++;	/* XXX: No consistency. */
575122a814aSBosko Milekic 	return (NULL);
576df8bae1dSRodney W. Grimes }
577df8bae1dSRodney W. Grimes 
578df8bae1dSRodney W. Grimes /*
579df8bae1dSRodney W. Grimes  * Partition an mbuf chain in two pieces, returning the tail --
580df8bae1dSRodney W. Grimes  * all but the first len0 bytes.  In case of failure, it returns NULL and
581df8bae1dSRodney W. Grimes  * attempts to restore the chain to its original state.
58248d183faSArchie Cobbs  *
58348d183faSArchie Cobbs  * Note that the resulting mbufs might be read-only, because the new
58448d183faSArchie Cobbs  * mbuf can end up sharing an mbuf cluster with the original mbuf if
58548d183faSArchie Cobbs  * the "breaking point" happens to lie within a cluster mbuf. Use the
58648d183faSArchie Cobbs  * M_WRITABLE() macro to check for this case.
587df8bae1dSRodney W. Grimes  */
588df8bae1dSRodney W. Grimes struct mbuf *
589122a814aSBosko Milekic m_split(struct mbuf *m0, int len0, int wait)
590df8bae1dSRodney W. Grimes {
591122a814aSBosko Milekic 	struct mbuf *m, *n;
592bd395ae8SBosko Milekic 	u_int len = len0, remain;
593df8bae1dSRodney W. Grimes 
594df8bae1dSRodney W. Grimes 	for (m = m0; m && len > m->m_len; m = m->m_next)
595df8bae1dSRodney W. Grimes 		len -= m->m_len;
596122a814aSBosko Milekic 	if (m == NULL)
597122a814aSBosko Milekic 		return (NULL);
598df8bae1dSRodney W. Grimes 	remain = m->m_len - len;
599df8bae1dSRodney W. Grimes 	if (m0->m_flags & M_PKTHDR) {
600df8bae1dSRodney W. Grimes 		MGETHDR(n, wait, m0->m_type);
601122a814aSBosko Milekic 		if (n == NULL)
602122a814aSBosko Milekic 			return (NULL);
603df8bae1dSRodney W. Grimes 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
604df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
605df8bae1dSRodney W. Grimes 		m0->m_pkthdr.len = len0;
606df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT)
607df8bae1dSRodney W. Grimes 			goto extpacket;
608df8bae1dSRodney W. Grimes 		if (remain > MHLEN) {
609df8bae1dSRodney W. Grimes 			/* m can't be the lead packet */
610df8bae1dSRodney W. Grimes 			MH_ALIGN(n, 0);
611df8bae1dSRodney W. Grimes 			n->m_next = m_split(m, len, wait);
612122a814aSBosko Milekic 			if (n->m_next == NULL) {
613df8bae1dSRodney W. Grimes 				(void) m_free(n);
614122a814aSBosko Milekic 				return (NULL);
61540376987SJeffrey Hsu 			} else {
61640376987SJeffrey Hsu 				n->m_len = 0;
617df8bae1dSRodney W. Grimes 				return (n);
61840376987SJeffrey Hsu 			}
619df8bae1dSRodney W. Grimes 		} else
620df8bae1dSRodney W. Grimes 			MH_ALIGN(n, remain);
621df8bae1dSRodney W. Grimes 	} else if (remain == 0) {
622df8bae1dSRodney W. Grimes 		n = m->m_next;
623122a814aSBosko Milekic 		m->m_next = NULL;
624df8bae1dSRodney W. Grimes 		return (n);
625df8bae1dSRodney W. Grimes 	} else {
626df8bae1dSRodney W. Grimes 		MGET(n, wait, m->m_type);
627122a814aSBosko Milekic 		if (n == NULL)
628122a814aSBosko Milekic 			return (NULL);
629df8bae1dSRodney W. Grimes 		M_ALIGN(n, remain);
630df8bae1dSRodney W. Grimes 	}
631df8bae1dSRodney W. Grimes extpacket:
632df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT) {
633df8bae1dSRodney W. Grimes 		n->m_flags |= M_EXT;
634df8bae1dSRodney W. Grimes 		n->m_ext = m->m_ext;
635a5c4836dSDavid Malone 		MEXT_ADD_REF(m);
636df8bae1dSRodney W. Grimes 		n->m_data = m->m_data + len;
637df8bae1dSRodney W. Grimes 	} else {
638df8bae1dSRodney W. Grimes 		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
639df8bae1dSRodney W. Grimes 	}
640df8bae1dSRodney W. Grimes 	n->m_len = remain;
641df8bae1dSRodney W. Grimes 	m->m_len = len;
642df8bae1dSRodney W. Grimes 	n->m_next = m->m_next;
643122a814aSBosko Milekic 	m->m_next = NULL;
644df8bae1dSRodney W. Grimes 	return (n);
645df8bae1dSRodney W. Grimes }
646df8bae1dSRodney W. Grimes /*
647df8bae1dSRodney W. Grimes  * Routine to copy from device local memory into mbufs.
648f5eece3fSBosko Milekic  * Note that `off' argument is offset into first mbuf of target chain from
649f5eece3fSBosko Milekic  * which to begin copying the data to.
650df8bae1dSRodney W. Grimes  */
651df8bae1dSRodney W. Grimes struct mbuf *
652f5eece3fSBosko Milekic m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
653122a814aSBosko Milekic 	 void (*copy)(char *from, caddr_t to, u_int len))
654df8bae1dSRodney W. Grimes {
655122a814aSBosko Milekic 	struct mbuf *m;
656df8bae1dSRodney W. Grimes 	struct mbuf *top = 0, **mp = &top;
657f5eece3fSBosko Milekic 	int len;
658df8bae1dSRodney W. Grimes 
659f5eece3fSBosko Milekic 	if (off < 0 || off > MHLEN)
660f5eece3fSBosko Milekic 		return (NULL);
661f5eece3fSBosko Milekic 
662a163d034SWarner Losh 	MGETHDR(m, M_DONTWAIT, MT_DATA);
663122a814aSBosko Milekic 	if (m == NULL)
664122a814aSBosko Milekic 		return (NULL);
665df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = ifp;
666df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = totlen;
667f5eece3fSBosko Milekic 	len = MHLEN;
668df8bae1dSRodney W. Grimes 
669df8bae1dSRodney W. Grimes 	while (totlen > 0) {
670df8bae1dSRodney W. Grimes 		if (top) {
671a163d034SWarner Losh 			MGET(m, M_DONTWAIT, MT_DATA);
672122a814aSBosko Milekic 			if (m == NULL) {
673df8bae1dSRodney W. Grimes 				m_freem(top);
674122a814aSBosko Milekic 				return (NULL);
675df8bae1dSRodney W. Grimes 			}
676f5eece3fSBosko Milekic 			len = MLEN;
677df8bae1dSRodney W. Grimes 		}
678f5eece3fSBosko Milekic 		if (totlen + off >= MINCLSIZE) {
679a163d034SWarner Losh 			MCLGET(m, M_DONTWAIT);
680df8bae1dSRodney W. Grimes 			if (m->m_flags & M_EXT)
681f5eece3fSBosko Milekic 				len = MCLBYTES;
682df8bae1dSRodney W. Grimes 		} else {
683df8bae1dSRodney W. Grimes 			/*
684df8bae1dSRodney W. Grimes 			 * Place initial small packet/header at end of mbuf.
685df8bae1dSRodney W. Grimes 			 */
686f5eece3fSBosko Milekic 			if (top == NULL && totlen + off + max_linkhdr <= len) {
687df8bae1dSRodney W. Grimes 				m->m_data += max_linkhdr;
688f5eece3fSBosko Milekic 				len -= max_linkhdr;
689df8bae1dSRodney W. Grimes 			}
690f5eece3fSBosko Milekic 		}
691f5eece3fSBosko Milekic 		if (off) {
692f5eece3fSBosko Milekic 			m->m_data += off;
693f5eece3fSBosko Milekic 			len -= off;
694f5eece3fSBosko Milekic 			off = 0;
695f5eece3fSBosko Milekic 		}
696f5eece3fSBosko Milekic 		m->m_len = len = min(totlen, len);
697df8bae1dSRodney W. Grimes 		if (copy)
698bd395ae8SBosko Milekic 			copy(buf, mtod(m, caddr_t), (u_int)len);
699df8bae1dSRodney W. Grimes 		else
700bd395ae8SBosko Milekic 			bcopy(buf, mtod(m, caddr_t), (u_int)len);
701f5eece3fSBosko Milekic 		buf += len;
702df8bae1dSRodney W. Grimes 		*mp = m;
703df8bae1dSRodney W. Grimes 		mp = &m->m_next;
704df8bae1dSRodney W. Grimes 		totlen -= len;
705df8bae1dSRodney W. Grimes 	}
706df8bae1dSRodney W. Grimes 	return (top);
707df8bae1dSRodney W. Grimes }
708c5789ba3SPoul-Henning Kamp 
709c5789ba3SPoul-Henning Kamp /*
710c5789ba3SPoul-Henning Kamp  * Copy data from a buffer back into the indicated mbuf chain,
711c5789ba3SPoul-Henning Kamp  * starting "off" bytes from the beginning, extending the mbuf
712c5789ba3SPoul-Henning Kamp  * chain if necessary.
713c5789ba3SPoul-Henning Kamp  */
714c5789ba3SPoul-Henning Kamp void
715122a814aSBosko Milekic m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
716c5789ba3SPoul-Henning Kamp {
717122a814aSBosko Milekic 	int mlen;
718122a814aSBosko Milekic 	struct mbuf *m = m0, *n;
719c5789ba3SPoul-Henning Kamp 	int totlen = 0;
720c5789ba3SPoul-Henning Kamp 
721122a814aSBosko Milekic 	if (m0 == NULL)
722c5789ba3SPoul-Henning Kamp 		return;
723c5789ba3SPoul-Henning Kamp 	while (off > (mlen = m->m_len)) {
724c5789ba3SPoul-Henning Kamp 		off -= mlen;
725c5789ba3SPoul-Henning Kamp 		totlen += mlen;
726122a814aSBosko Milekic 		if (m->m_next == NULL) {
727a163d034SWarner Losh 			n = m_get_clrd(M_DONTWAIT, m->m_type);
728122a814aSBosko Milekic 			if (n == NULL)
729c5789ba3SPoul-Henning Kamp 				goto out;
730c5789ba3SPoul-Henning Kamp 			n->m_len = min(MLEN, len + off);
731c5789ba3SPoul-Henning Kamp 			m->m_next = n;
732c5789ba3SPoul-Henning Kamp 		}
733c5789ba3SPoul-Henning Kamp 		m = m->m_next;
734c5789ba3SPoul-Henning Kamp 	}
735c5789ba3SPoul-Henning Kamp 	while (len > 0) {
736c5789ba3SPoul-Henning Kamp 		mlen = min (m->m_len - off, len);
737bd395ae8SBosko Milekic 		bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
738c5789ba3SPoul-Henning Kamp 		cp += mlen;
739c5789ba3SPoul-Henning Kamp 		len -= mlen;
740c5789ba3SPoul-Henning Kamp 		mlen += off;
741c5789ba3SPoul-Henning Kamp 		off = 0;
742c5789ba3SPoul-Henning Kamp 		totlen += mlen;
743c5789ba3SPoul-Henning Kamp 		if (len == 0)
744c5789ba3SPoul-Henning Kamp 			break;
745122a814aSBosko Milekic 		if (m->m_next == NULL) {
746a163d034SWarner Losh 			n = m_get(M_DONTWAIT, m->m_type);
747122a814aSBosko Milekic 			if (n == NULL)
748c5789ba3SPoul-Henning Kamp 				break;
749c5789ba3SPoul-Henning Kamp 			n->m_len = min(MLEN, len);
750c5789ba3SPoul-Henning Kamp 			m->m_next = n;
751c5789ba3SPoul-Henning Kamp 		}
752c5789ba3SPoul-Henning Kamp 		m = m->m_next;
753c5789ba3SPoul-Henning Kamp 	}
754c5789ba3SPoul-Henning Kamp out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
755c5789ba3SPoul-Henning Kamp 		m->m_pkthdr.len = totlen;
756c5789ba3SPoul-Henning Kamp }
757ce4a64f7SPoul-Henning Kamp 
75837621fd5SBruce M Simpson /*
75937621fd5SBruce M Simpson  * Apply function f to the data in an mbuf chain starting "off" bytes from
76037621fd5SBruce M Simpson  * the beginning, continuing for "len" bytes.
76137621fd5SBruce M Simpson  */
76237621fd5SBruce M Simpson int
76337621fd5SBruce M Simpson m_apply(struct mbuf *m, int off, int len,
76437621fd5SBruce M Simpson     int (*f)(void *, caddr_t, unsigned int), void *arg)
76537621fd5SBruce M Simpson {
76637621fd5SBruce M Simpson 	unsigned int count;
76737621fd5SBruce M Simpson 	int rval;
76837621fd5SBruce M Simpson 
76937621fd5SBruce M Simpson 	KASSERT(off >= 0, ("m_apply, negative off %d", off));
77037621fd5SBruce M Simpson 	KASSERT(len >= 0, ("m_apply, negative len %d", len));
77137621fd5SBruce M Simpson 
77237621fd5SBruce M Simpson 	while (off > 0) {
77337621fd5SBruce M Simpson 		KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
77437621fd5SBruce M Simpson 		if (off < m->m_len)
77537621fd5SBruce M Simpson 			break;
77637621fd5SBruce M Simpson 		off -= m->m_len;
77737621fd5SBruce M Simpson 		m = m->m_next;
77837621fd5SBruce M Simpson 	}
77937621fd5SBruce M Simpson 	while (len > 0) {
78037621fd5SBruce M Simpson 		KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
78137621fd5SBruce M Simpson 		count = min(m->m_len - off, len);
78237621fd5SBruce M Simpson 
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 
78737621fd5SBruce M Simpson 		len -= count;
78837621fd5SBruce M Simpson 		off = 0;
78937621fd5SBruce M Simpson 		m = m->m_next;
79037621fd5SBruce M Simpson 	}
79137621fd5SBruce M Simpson 
79237621fd5SBruce M Simpson 	return (0);
79337621fd5SBruce M Simpson }
79437621fd5SBruce M Simpson 
79537621fd5SBruce M Simpson /*
79637621fd5SBruce M Simpson  * Return a pointer to mbuf/offset of location in mbuf chain.
79737621fd5SBruce M Simpson  */
79837621fd5SBruce M Simpson struct mbuf *
79937621fd5SBruce M Simpson m_getptr(struct mbuf *m, int loc, int *off)
80037621fd5SBruce M Simpson {
80137621fd5SBruce M Simpson 
80237621fd5SBruce M Simpson 	while (loc >= 0) {
80337621fd5SBruce M Simpson 		/* Normal end of search */
80437621fd5SBruce M Simpson 		if (m->m_len > loc) {
80537621fd5SBruce M Simpson 			*off = loc;
80637621fd5SBruce M Simpson 			return (m);
80737621fd5SBruce M Simpson 		} else {
80837621fd5SBruce M Simpson 			loc -= m->m_len;
80937621fd5SBruce M Simpson 
81037621fd5SBruce M Simpson 			if (m->m_next == NULL) {
81137621fd5SBruce M Simpson 				if (loc == 0) {
81237621fd5SBruce M Simpson 					/* Point at the end of valid data */
81337621fd5SBruce M Simpson 					*off = m->m_len;
81437621fd5SBruce M Simpson 					return (m);
81537621fd5SBruce M Simpson 				} else
81637621fd5SBruce M Simpson 					return (NULL);
81737621fd5SBruce M Simpson 			} else
81837621fd5SBruce M Simpson 				m = m->m_next;
81937621fd5SBruce M Simpson 		}
82037621fd5SBruce M Simpson 	}
82137621fd5SBruce M Simpson 
82237621fd5SBruce M Simpson 	return (NULL);
82337621fd5SBruce M Simpson }
82437621fd5SBruce M Simpson 
825ce4a64f7SPoul-Henning Kamp void
826ce4a64f7SPoul-Henning Kamp m_print(const struct mbuf *m)
827ce4a64f7SPoul-Henning Kamp {
828ce4a64f7SPoul-Henning Kamp 	int len;
8296357e7b5SEivind Eklund 	const struct mbuf *m2;
830ce4a64f7SPoul-Henning Kamp 
831ce4a64f7SPoul-Henning Kamp 	len = m->m_pkthdr.len;
832ce4a64f7SPoul-Henning Kamp 	m2 = m;
833ce4a64f7SPoul-Henning Kamp 	while (len) {
834ce4a64f7SPoul-Henning Kamp 		printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");
835ce4a64f7SPoul-Henning Kamp 		len -= m2->m_len;
836ce4a64f7SPoul-Henning Kamp 		m2 = m2->m_next;
837ce4a64f7SPoul-Henning Kamp 	}
838ce4a64f7SPoul-Henning Kamp 	return;
839ce4a64f7SPoul-Henning Kamp }
8403f2e06c5SPoul-Henning Kamp 
841bd395ae8SBosko Milekic u_int
8423f2e06c5SPoul-Henning Kamp m_fixhdr(struct mbuf *m0)
8433f2e06c5SPoul-Henning Kamp {
844bd395ae8SBosko Milekic 	u_int len;
8453f2e06c5SPoul-Henning Kamp 
846ac6e585dSPoul-Henning Kamp 	len = m_length(m0, NULL);
8473f2e06c5SPoul-Henning Kamp 	m0->m_pkthdr.len = len;
848ac6e585dSPoul-Henning Kamp 	return (len);
849ac6e585dSPoul-Henning Kamp }
850ac6e585dSPoul-Henning Kamp 
851bd395ae8SBosko Milekic u_int
852ac6e585dSPoul-Henning Kamp m_length(struct mbuf *m0, struct mbuf **last)
853ac6e585dSPoul-Henning Kamp {
854ac6e585dSPoul-Henning Kamp 	struct mbuf *m;
855bd395ae8SBosko Milekic 	u_int len;
856ac6e585dSPoul-Henning Kamp 
857ac6e585dSPoul-Henning Kamp 	len = 0;
858ac6e585dSPoul-Henning Kamp 	for (m = m0; m != NULL; m = m->m_next) {
859ac6e585dSPoul-Henning Kamp 		len += m->m_len;
860ac6e585dSPoul-Henning Kamp 		if (m->m_next == NULL)
861ac6e585dSPoul-Henning Kamp 			break;
862ac6e585dSPoul-Henning Kamp 	}
863ac6e585dSPoul-Henning Kamp 	if (last != NULL)
864ac6e585dSPoul-Henning Kamp 		*last = m;
865ac6e585dSPoul-Henning Kamp 	return (len);
8663f2e06c5SPoul-Henning Kamp }
86755e9f80dSMike Silbersack 
86855e9f80dSMike Silbersack /*
86955e9f80dSMike Silbersack  * Defragment a mbuf chain, returning the shortest possible
87055e9f80dSMike Silbersack  * chain of mbufs and clusters.  If allocation fails and
87155e9f80dSMike Silbersack  * this cannot be completed, NULL will be returned, but
87255e9f80dSMike Silbersack  * the passed in chain will be unchanged.  Upon success,
87355e9f80dSMike Silbersack  * the original chain will be freed, and the new chain
87455e9f80dSMike Silbersack  * will be returned.
87555e9f80dSMike Silbersack  *
87655e9f80dSMike Silbersack  * If a non-packet header is passed in, the original
87755e9f80dSMike Silbersack  * mbuf (chain?) will be returned unharmed.
87855e9f80dSMike Silbersack  */
87955e9f80dSMike Silbersack struct mbuf *
88055e9f80dSMike Silbersack m_defrag(struct mbuf *m0, int how)
88155e9f80dSMike Silbersack {
88255e9f80dSMike Silbersack 	struct mbuf	*m_new = NULL, *m_final = NULL;
88355e9f80dSMike Silbersack 	int		progress = 0, length;
88455e9f80dSMike Silbersack 
88555e9f80dSMike Silbersack 	if (!(m0->m_flags & M_PKTHDR))
88655e9f80dSMike Silbersack 		return (m0);
88755e9f80dSMike Silbersack 
888f8bf8e39SMike Silbersack 	m_fixhdr(m0); /* Needed sanity check */
889f8bf8e39SMike Silbersack 
890352d050eSMike Silbersack #ifdef MBUF_STRESS_TEST
891352d050eSMike Silbersack 	if (m_defragrandomfailures) {
892352d050eSMike Silbersack 		int temp = arc4random() & 0xff;
893352d050eSMike Silbersack 		if (temp == 0xba)
894352d050eSMike Silbersack 			goto nospace;
895352d050eSMike Silbersack 	}
896352d050eSMike Silbersack #endif
89755e9f80dSMike Silbersack 
89855e9f80dSMike Silbersack 	if (m0->m_pkthdr.len > MHLEN)
89955e9f80dSMike Silbersack 		m_final = m_getcl(how, MT_DATA, M_PKTHDR);
90055e9f80dSMike Silbersack 	else
90155e9f80dSMike Silbersack 		m_final = m_gethdr(how, MT_DATA);
90255e9f80dSMike Silbersack 
90355e9f80dSMike Silbersack 	if (m_final == NULL)
90455e9f80dSMike Silbersack 		goto nospace;
90555e9f80dSMike Silbersack 
90655e9f80dSMike Silbersack 	if (m_dup_pkthdr(m_final, m0, how) == NULL)
90755e9f80dSMike Silbersack 		goto nospace;
90855e9f80dSMike Silbersack 
90955e9f80dSMike Silbersack 	m_new = m_final;
91055e9f80dSMike Silbersack 
91155e9f80dSMike Silbersack 	while (progress < m0->m_pkthdr.len) {
91255e9f80dSMike Silbersack 		length = m0->m_pkthdr.len - progress;
91355e9f80dSMike Silbersack 		if (length > MCLBYTES)
91455e9f80dSMike Silbersack 			length = MCLBYTES;
91555e9f80dSMike Silbersack 
91655e9f80dSMike Silbersack 		if (m_new == NULL) {
91755e9f80dSMike Silbersack 			if (length > MLEN)
91855e9f80dSMike Silbersack 				m_new = m_getcl(how, MT_DATA, 0);
91955e9f80dSMike Silbersack 			else
92055e9f80dSMike Silbersack 				m_new = m_get(how, MT_DATA);
92155e9f80dSMike Silbersack 			if (m_new == NULL)
92255e9f80dSMike Silbersack 				goto nospace;
92355e9f80dSMike Silbersack 		}
92455e9f80dSMike Silbersack 
92555e9f80dSMike Silbersack 		m_copydata(m0, progress, length, mtod(m_new, caddr_t));
92655e9f80dSMike Silbersack 		progress += length;
92755e9f80dSMike Silbersack 		m_new->m_len = length;
92855e9f80dSMike Silbersack 		if (m_new != m_final)
92955e9f80dSMike Silbersack 			m_cat(m_final, m_new);
93055e9f80dSMike Silbersack 		m_new = NULL;
93155e9f80dSMike Silbersack 	}
93251710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
93355e9f80dSMike Silbersack 	if (m0->m_next == NULL)
93455e9f80dSMike Silbersack 		m_defraguseless++;
93551710a45SMike Silbersack #endif
93655e9f80dSMike Silbersack 	m_freem(m0);
93755e9f80dSMike Silbersack 	m0 = m_final;
93851710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
93955e9f80dSMike Silbersack 	m_defragpackets++;
94055e9f80dSMike Silbersack 	m_defragbytes += m0->m_pkthdr.len;
94151710a45SMike Silbersack #endif
94255e9f80dSMike Silbersack 	return (m0);
94355e9f80dSMike Silbersack nospace:
94451710a45SMike Silbersack #ifdef MBUF_STRESS_TEST
94555e9f80dSMike Silbersack 	m_defragfailure++;
94651710a45SMike Silbersack #endif
94755e9f80dSMike Silbersack 	if (m_new)
94855e9f80dSMike Silbersack 		m_free(m_new);
94955e9f80dSMike Silbersack 	if (m_final)
95055e9f80dSMike Silbersack 		m_freem(m_final);
95155e9f80dSMike Silbersack 	return (NULL);
95255e9f80dSMike Silbersack }
9533390d476SMike Silbersack 
9543390d476SMike Silbersack #ifdef MBUF_STRESS_TEST
9553390d476SMike Silbersack 
9563390d476SMike Silbersack /*
9573390d476SMike Silbersack  * Fragment an mbuf chain.  There's no reason you'd ever want to do
9583390d476SMike Silbersack  * this in normal usage, but it's great for stress testing various
9593390d476SMike Silbersack  * mbuf consumers.
9603390d476SMike Silbersack  *
9613390d476SMike Silbersack  * If fragmentation is not possible, the original chain will be
9623390d476SMike Silbersack  * returned.
9633390d476SMike Silbersack  *
9643390d476SMike Silbersack  * Possible length values:
9653390d476SMike Silbersack  * 0	 no fragmentation will occur
9663390d476SMike Silbersack  * > 0	each fragment will be of the specified length
9673390d476SMike Silbersack  * -1	each fragment will be the same random value in length
9683390d476SMike Silbersack  * -2	each fragment's length will be entirely random
9693390d476SMike Silbersack  * (Random values range from 1 to 256)
9703390d476SMike Silbersack  */
9713390d476SMike Silbersack struct mbuf *
9723390d476SMike Silbersack m_fragment(struct mbuf *m0, int how, int length)
9733390d476SMike Silbersack {
9743390d476SMike Silbersack 	struct mbuf	*m_new = NULL, *m_final = NULL;
9753390d476SMike Silbersack 	int		progress = 0;
9763390d476SMike Silbersack 
9773390d476SMike Silbersack 	if (!(m0->m_flags & M_PKTHDR))
9783390d476SMike Silbersack 		return (m0);
9793390d476SMike Silbersack 
9803390d476SMike Silbersack 	if ((length == 0) || (length < -2))
9813390d476SMike Silbersack 		return (m0);
9823390d476SMike Silbersack 
9833390d476SMike Silbersack 	m_fixhdr(m0); /* Needed sanity check */
9843390d476SMike Silbersack 
9853390d476SMike Silbersack 	m_final = m_getcl(how, MT_DATA, M_PKTHDR);
9863390d476SMike Silbersack 
9873390d476SMike Silbersack 	if (m_final == NULL)
9883390d476SMike Silbersack 		goto nospace;
9893390d476SMike Silbersack 
9903390d476SMike Silbersack 	if (m_dup_pkthdr(m_final, m0, how) == NULL)
9913390d476SMike Silbersack 		goto nospace;
9923390d476SMike Silbersack 
9933390d476SMike Silbersack 	m_new = m_final;
9943390d476SMike Silbersack 
9953390d476SMike Silbersack 	if (length == -1)
9963390d476SMike Silbersack 		length = 1 + (arc4random() & 255);
9973390d476SMike Silbersack 
9983390d476SMike Silbersack 	while (progress < m0->m_pkthdr.len) {
9993390d476SMike Silbersack 		int fraglen;
10003390d476SMike Silbersack 
10013390d476SMike Silbersack 		if (length > 0)
10023390d476SMike Silbersack 			fraglen = length;
10033390d476SMike Silbersack 		else
10043390d476SMike Silbersack 			fraglen = 1 + (arc4random() & 255);
10053390d476SMike Silbersack 		if (fraglen > m0->m_pkthdr.len - progress)
10063390d476SMike Silbersack 			fraglen = m0->m_pkthdr.len - progress;
10073390d476SMike Silbersack 
10083390d476SMike Silbersack 		if (fraglen > MCLBYTES)
10093390d476SMike Silbersack 			fraglen = MCLBYTES;
10103390d476SMike Silbersack 
10113390d476SMike Silbersack 		if (m_new == NULL) {
10123390d476SMike Silbersack 			m_new = m_getcl(how, MT_DATA, 0);
10133390d476SMike Silbersack 			if (m_new == NULL)
10143390d476SMike Silbersack 				goto nospace;
10153390d476SMike Silbersack 		}
10163390d476SMike Silbersack 
10173390d476SMike Silbersack 		m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t));
10183390d476SMike Silbersack 		progress += fraglen;
10193390d476SMike Silbersack 		m_new->m_len = fraglen;
10203390d476SMike Silbersack 		if (m_new != m_final)
10213390d476SMike Silbersack 			m_cat(m_final, m_new);
10223390d476SMike Silbersack 		m_new = NULL;
10233390d476SMike Silbersack 	}
10243390d476SMike Silbersack 	m_freem(m0);
10253390d476SMike Silbersack 	m0 = m_final;
10263390d476SMike Silbersack 	return (m0);
10273390d476SMike Silbersack nospace:
10283390d476SMike Silbersack 	if (m_new)
10293390d476SMike Silbersack 		m_free(m_new);
10303390d476SMike Silbersack 	if (m_final)
10313390d476SMike Silbersack 		m_freem(m_final);
10323390d476SMike Silbersack 	/* Return the original chain on failure */
10333390d476SMike Silbersack 	return (m0);
10343390d476SMike Silbersack }
10353390d476SMike Silbersack 
10363390d476SMike Silbersack #endif
1037