xref: /freebsd/sys/kern/uipc_mbuf.c (revision 352d050e790e3ee7e8441dfd38d23bbd62c337a8)
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
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37e32a5b94SRobert Watson #include "opt_mac.h"
38240ef842SDavid E. O'Brien #include "opt_param.h"
39352d050eSMike Silbersack #include "opt_mbuf_stress_test.h"
40e32a5b94SRobert Watson 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42df8bae1dSRodney W. Grimes #include <sys/systm.h>
43fb919e4dSMark Murray #include <sys/kernel.h>
44fb919e4dSMark Murray #include <sys/lock.h>
45e32a5b94SRobert Watson #include <sys/mac.h>
46f9d0d524SRobert Watson #include <sys/malloc.h>
47df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
48639acc13SGarrett Wollman #include <sys/sysctl.h>
49df8bae1dSRodney W. Grimes #include <sys/domain.h>
50df8bae1dSRodney W. Grimes #include <sys/protosw.h>
51fb919e4dSMark Murray 
5228f8db14SBruce Evans int	max_linkhdr;
5328f8db14SBruce Evans int	max_protohdr;
5428f8db14SBruce Evans int	max_hdr;
5528f8db14SBruce Evans int	max_datalen;
5655e9f80dSMike Silbersack int	m_defragpackets;
5755e9f80dSMike Silbersack int	m_defragbytes;
5855e9f80dSMike Silbersack int	m_defraguseless;
5955e9f80dSMike Silbersack int	m_defragfailure;
60352d050eSMike Silbersack #ifdef MBUF_STRESS_TEST
61352d050eSMike Silbersack int	m_defragrandomfailures;
62352d050eSMike Silbersack #endif
637d032714SBosko Milekic 
647d032714SBosko Milekic /*
657d032714SBosko Milekic  * sysctl(8) exported objects
667d032714SBosko Milekic  */
67ce02431fSDoug Rabson SYSCTL_DECL(_kern_ipc);
68639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
69639acc13SGarrett Wollman 	   &max_linkhdr, 0, "");
70639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
71639acc13SGarrett Wollman 	   &max_protohdr, 0, "");
72639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
73639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
74639acc13SGarrett Wollman 	   &max_datalen, 0, "");
7555e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
7655e9f80dSMike Silbersack 	   &m_defragpackets, 0, "");
7755e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
7855e9f80dSMike Silbersack 	   &m_defragbytes, 0, "");
7955e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
8055e9f80dSMike Silbersack 	   &m_defraguseless, 0, "");
8155e9f80dSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
8255e9f80dSMike Silbersack 	   &m_defragfailure, 0, "");
83352d050eSMike Silbersack #ifdef MBUF_STRESS_TEST
84352d050eSMike Silbersack SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
85352d050eSMike Silbersack 	   &m_defragrandomfailures, 0, "");
86352d050eSMike Silbersack #endif
87df8bae1dSRodney W. Grimes 
88df8bae1dSRodney W. Grimes /*
899967cafcSSam Leffler  * "Move" mbuf pkthdr from "from" to "to".
90e37b1fcdSRobert Watson  * "from" must have M_PKTHDR set, and "to" must be empty.
91e37b1fcdSRobert Watson  */
92e37b1fcdSRobert Watson void
939967cafcSSam Leffler m_move_pkthdr(struct mbuf *to, struct mbuf *from)
94e37b1fcdSRobert Watson {
95e37b1fcdSRobert Watson 
96e37b1fcdSRobert Watson #if 0
979967cafcSSam Leffler 	/* see below for why these are not enabled */
98fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(to);
99225bff6fSRobert Watson 	/* Note: with MAC, this may not be a good assertion. */
1009967cafcSSam Leffler 	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
1019967cafcSSam Leffler 	    ("m_move_pkthdr: to has tags"));
102e37b1fcdSRobert Watson #endif
1039967cafcSSam Leffler 	KASSERT((to->m_flags & M_EXT) == 0, ("m_move_pkthdr: to has cluster"));
104e32a5b94SRobert Watson #ifdef MAC
105225bff6fSRobert Watson 	/*
106225bff6fSRobert Watson 	 * XXXMAC: It could be this should also occur for non-MAC?
107225bff6fSRobert Watson 	 */
108e32a5b94SRobert Watson 	if (to->m_flags & M_PKTHDR)
109225bff6fSRobert Watson 		m_tag_delete_chain(to, NULL);
110e32a5b94SRobert Watson #endif
111e37b1fcdSRobert Watson 	to->m_flags = from->m_flags & M_COPYFLAGS;
1129967cafcSSam Leffler 	to->m_data = to->m_pktdat;
1139967cafcSSam Leffler 	to->m_pkthdr = from->m_pkthdr;		/* especially tags */
1149967cafcSSam Leffler 	SLIST_INIT(&from->m_pkthdr.tags);	/* purge tags from src */
1159967cafcSSam Leffler 	from->m_flags &= ~M_PKTHDR;
1169967cafcSSam Leffler }
1179967cafcSSam Leffler 
1189967cafcSSam Leffler /*
1199967cafcSSam Leffler  * Duplicate "from"'s mbuf pkthdr in "to".
1209967cafcSSam Leffler  * "from" must have M_PKTHDR set, and "to" must be empty.
1219967cafcSSam Leffler  * In particular, this does a deep copy of the packet tags.
1229967cafcSSam Leffler  */
1239967cafcSSam Leffler int
1249967cafcSSam Leffler m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
1259967cafcSSam Leffler {
1269967cafcSSam Leffler 
1279967cafcSSam Leffler #if 0
1289967cafcSSam Leffler 	/*
1299967cafcSSam Leffler 	 * The mbuf allocator only initializes the pkthdr
1309967cafcSSam Leffler 	 * when the mbuf is allocated with MGETHDR. Many users
1319967cafcSSam Leffler 	 * (e.g. m_copy*, m_prepend) use MGET and then
1329967cafcSSam Leffler 	 * smash the pkthdr as needed causing these
1339967cafcSSam Leffler 	 * assertions to trip.  For now just disable them.
1349967cafcSSam Leffler 	 */
135fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(to);
136225bff6fSRobert Watson 	/* Note: with MAC, this may not be a good assertion. */
1379967cafcSSam Leffler 	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
1389967cafcSSam Leffler #endif
1399967cafcSSam Leffler #ifdef MAC
1409967cafcSSam Leffler 	if (to->m_flags & M_PKTHDR)
141225bff6fSRobert Watson 		m_tag_delete_chain(to, NULL);
1429967cafcSSam Leffler #endif
143df8c7fc9SMike Silbersack 	to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
144df8c7fc9SMike Silbersack 	if ((to->m_flags & M_EXT) == 0)
1459967cafcSSam Leffler 		to->m_data = to->m_pktdat;
146e37b1fcdSRobert Watson 	to->m_pkthdr = from->m_pkthdr;
1479967cafcSSam Leffler 	SLIST_INIT(&to->m_pkthdr.tags);
148aa65d9f5SRobert Watson 	return (m_tag_copy_chain(to, from, MBTOM(how)));
149e37b1fcdSRobert Watson }
150e37b1fcdSRobert Watson 
151e37b1fcdSRobert Watson /*
152df8bae1dSRodney W. Grimes  * Lesser-used path for M_PREPEND:
153df8bae1dSRodney W. Grimes  * allocate new mbuf to prepend to chain,
154df8bae1dSRodney W. Grimes  * copy junk along.
155df8bae1dSRodney W. Grimes  */
156df8bae1dSRodney W. Grimes struct mbuf *
157122a814aSBosko Milekic m_prepend(struct mbuf *m, int len, int how)
158df8bae1dSRodney W. Grimes {
159df8bae1dSRodney W. Grimes 	struct mbuf *mn;
160df8bae1dSRodney W. Grimes 
161df8bae1dSRodney W. Grimes 	MGET(mn, how, m->m_type);
162122a814aSBosko Milekic 	if (mn == NULL) {
163df8bae1dSRodney W. Grimes 		m_freem(m);
164122a814aSBosko Milekic 		return (NULL);
165df8bae1dSRodney W. Grimes 	}
166225bff6fSRobert Watson 	if (m->m_flags & M_PKTHDR)
1679967cafcSSam Leffler 		M_MOVE_PKTHDR(mn, m);
168df8bae1dSRodney W. Grimes 	mn->m_next = m;
169df8bae1dSRodney W. Grimes 	m = mn;
170df8bae1dSRodney W. Grimes 	if (len < MHLEN)
171df8bae1dSRodney W. Grimes 		MH_ALIGN(m, len);
172df8bae1dSRodney W. Grimes 	m->m_len = len;
173df8bae1dSRodney W. Grimes 	return (m);
174df8bae1dSRodney W. Grimes }
175df8bae1dSRodney W. Grimes 
176df8bae1dSRodney W. Grimes /*
177df8bae1dSRodney W. Grimes  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
178df8bae1dSRodney W. Grimes  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
179a163d034SWarner Losh  * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
1801c38f2eaSArchie Cobbs  * Note that the copy is read-only, because clusters are not copied,
1811c38f2eaSArchie Cobbs  * only their reference counts are incremented.
182df8bae1dSRodney W. Grimes  */
183df8bae1dSRodney W. Grimes struct mbuf *
184122a814aSBosko Milekic m_copym(struct mbuf *m, int off0, int len, int wait)
185df8bae1dSRodney W. Grimes {
186122a814aSBosko Milekic 	struct mbuf *n, **np;
187122a814aSBosko Milekic 	int off = off0;
188df8bae1dSRodney W. Grimes 	struct mbuf *top;
189df8bae1dSRodney W. Grimes 	int copyhdr = 0;
190df8bae1dSRodney W. Grimes 
191e0a653ddSAlfred Perlstein 	KASSERT(off >= 0, ("m_copym, negative off %d", off));
192e0a653ddSAlfred Perlstein 	KASSERT(len >= 0, ("m_copym, negative len %d", len));
193df8bae1dSRodney W. Grimes 	if (off == 0 && m->m_flags & M_PKTHDR)
194df8bae1dSRodney W. Grimes 		copyhdr = 1;
195df8bae1dSRodney W. Grimes 	while (off > 0) {
196e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
197df8bae1dSRodney W. Grimes 		if (off < m->m_len)
198df8bae1dSRodney W. Grimes 			break;
199df8bae1dSRodney W. Grimes 		off -= m->m_len;
200df8bae1dSRodney W. Grimes 		m = m->m_next;
201df8bae1dSRodney W. Grimes 	}
202df8bae1dSRodney W. Grimes 	np = &top;
203df8bae1dSRodney W. Grimes 	top = 0;
204df8bae1dSRodney W. Grimes 	while (len > 0) {
205122a814aSBosko Milekic 		if (m == NULL) {
206e0a653ddSAlfred Perlstein 			KASSERT(len == M_COPYALL,
207e0a653ddSAlfred Perlstein 			    ("m_copym, length > size of mbuf chain"));
208df8bae1dSRodney W. Grimes 			break;
209df8bae1dSRodney W. Grimes 		}
210df8bae1dSRodney W. Grimes 		MGET(n, wait, m->m_type);
211df8bae1dSRodney W. Grimes 		*np = n;
212122a814aSBosko Milekic 		if (n == NULL)
213df8bae1dSRodney W. Grimes 			goto nospace;
214df8bae1dSRodney W. Grimes 		if (copyhdr) {
2159967cafcSSam Leffler 			if (!m_dup_pkthdr(n, m, wait))
2169967cafcSSam Leffler 				goto nospace;
217df8bae1dSRodney W. Grimes 			if (len == M_COPYALL)
218df8bae1dSRodney W. Grimes 				n->m_pkthdr.len -= off0;
219df8bae1dSRodney W. Grimes 			else
220df8bae1dSRodney W. Grimes 				n->m_pkthdr.len = len;
221df8bae1dSRodney W. Grimes 			copyhdr = 0;
222df8bae1dSRodney W. Grimes 		}
223df8bae1dSRodney W. Grimes 		n->m_len = min(len, m->m_len - off);
224df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT) {
225df8bae1dSRodney W. Grimes 			n->m_data = m->m_data + off;
226df8bae1dSRodney W. Grimes 			n->m_ext = m->m_ext;
227df8bae1dSRodney W. Grimes 			n->m_flags |= M_EXT;
228a5c4836dSDavid Malone 			MEXT_ADD_REF(m);
229df8bae1dSRodney W. Grimes 		} else
230df8bae1dSRodney W. Grimes 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
231bd395ae8SBosko Milekic 			    (u_int)n->m_len);
232df8bae1dSRodney W. Grimes 		if (len != M_COPYALL)
233df8bae1dSRodney W. Grimes 			len -= n->m_len;
234df8bae1dSRodney W. Grimes 		off = 0;
235df8bae1dSRodney W. Grimes 		m = m->m_next;
236df8bae1dSRodney W. Grimes 		np = &n->m_next;
237df8bae1dSRodney W. Grimes 	}
23808442f8aSBosko Milekic 	if (top == NULL)
23908442f8aSBosko Milekic 		mbstat.m_mcfail++;	/* XXX: No consistency. */
24008442f8aSBosko Milekic 
241df8bae1dSRodney W. Grimes 	return (top);
242df8bae1dSRodney W. Grimes nospace:
243df8bae1dSRodney W. Grimes 	m_freem(top);
24408442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
245122a814aSBosko Milekic 	return (NULL);
246df8bae1dSRodney W. Grimes }
247df8bae1dSRodney W. Grimes 
248df8bae1dSRodney W. Grimes /*
2496a06dea0SGarrett Wollman  * Copy an entire packet, including header (which must be present).
2506a06dea0SGarrett Wollman  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
2511c38f2eaSArchie Cobbs  * Note that the copy is read-only, because clusters are not copied,
2521c38f2eaSArchie Cobbs  * only their reference counts are incremented.
2535fe86675SLuigi Rizzo  * Preserve alignment of the first mbuf so if the creator has left
2545fe86675SLuigi Rizzo  * some room at the beginning (e.g. for inserting protocol headers)
2555fe86675SLuigi Rizzo  * the copies still have the room available.
2566a06dea0SGarrett Wollman  */
2576a06dea0SGarrett Wollman struct mbuf *
258122a814aSBosko Milekic m_copypacket(struct mbuf *m, int how)
2596a06dea0SGarrett Wollman {
2606a06dea0SGarrett Wollman 	struct mbuf *top, *n, *o;
2616a06dea0SGarrett Wollman 
2626a06dea0SGarrett Wollman 	MGET(n, how, m->m_type);
2636a06dea0SGarrett Wollman 	top = n;
264122a814aSBosko Milekic 	if (n == NULL)
2656a06dea0SGarrett Wollman 		goto nospace;
2666a06dea0SGarrett Wollman 
2679967cafcSSam Leffler 	if (!m_dup_pkthdr(n, m, how))
2689967cafcSSam Leffler 		goto nospace;
2696a06dea0SGarrett Wollman 	n->m_len = m->m_len;
2706a06dea0SGarrett Wollman 	if (m->m_flags & M_EXT) {
2716a06dea0SGarrett Wollman 		n->m_data = m->m_data;
2726a06dea0SGarrett Wollman 		n->m_ext = m->m_ext;
2736a06dea0SGarrett Wollman 		n->m_flags |= M_EXT;
274a5c4836dSDavid Malone 		MEXT_ADD_REF(m);
2756a06dea0SGarrett Wollman 	} else {
2765fe86675SLuigi Rizzo 		n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
2776a06dea0SGarrett Wollman 		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
2786a06dea0SGarrett Wollman 	}
2796a06dea0SGarrett Wollman 
2806a06dea0SGarrett Wollman 	m = m->m_next;
2816a06dea0SGarrett Wollman 	while (m) {
2826a06dea0SGarrett Wollman 		MGET(o, how, m->m_type);
283122a814aSBosko Milekic 		if (o == NULL)
2846a06dea0SGarrett Wollman 			goto nospace;
2856a06dea0SGarrett Wollman 
2866a06dea0SGarrett Wollman 		n->m_next = o;
2876a06dea0SGarrett Wollman 		n = n->m_next;
2886a06dea0SGarrett Wollman 
2896a06dea0SGarrett Wollman 		n->m_len = m->m_len;
2906a06dea0SGarrett Wollman 		if (m->m_flags & M_EXT) {
2916a06dea0SGarrett Wollman 			n->m_data = m->m_data;
2926a06dea0SGarrett Wollman 			n->m_ext = m->m_ext;
2936a06dea0SGarrett Wollman 			n->m_flags |= M_EXT;
294a5c4836dSDavid Malone 			MEXT_ADD_REF(m);
2956a06dea0SGarrett Wollman 		} else {
2966a06dea0SGarrett Wollman 			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
2976a06dea0SGarrett Wollman 		}
2986a06dea0SGarrett Wollman 
2996a06dea0SGarrett Wollman 		m = m->m_next;
3006a06dea0SGarrett Wollman 	}
3016a06dea0SGarrett Wollman 	return top;
3026a06dea0SGarrett Wollman nospace:
3036a06dea0SGarrett Wollman 	m_freem(top);
30408442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
305122a814aSBosko Milekic 	return (NULL);
3066a06dea0SGarrett Wollman }
3076a06dea0SGarrett Wollman 
3086a06dea0SGarrett Wollman /*
309df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain starting "off" bytes from the beginning,
310df8bae1dSRodney W. Grimes  * continuing for "len" bytes, into the indicated buffer.
311df8bae1dSRodney W. Grimes  */
31226f9a767SRodney W. Grimes void
313a8cfc0eeSJulian Elischer m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
314df8bae1dSRodney W. Grimes {
315bd395ae8SBosko Milekic 	u_int count;
316df8bae1dSRodney W. Grimes 
317e0a653ddSAlfred Perlstein 	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
318e0a653ddSAlfred Perlstein 	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
319df8bae1dSRodney W. Grimes 	while (off > 0) {
320e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
321df8bae1dSRodney W. Grimes 		if (off < m->m_len)
322df8bae1dSRodney W. Grimes 			break;
323df8bae1dSRodney W. Grimes 		off -= m->m_len;
324df8bae1dSRodney W. Grimes 		m = m->m_next;
325df8bae1dSRodney W. Grimes 	}
326df8bae1dSRodney W. Grimes 	while (len > 0) {
327e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
328df8bae1dSRodney W. Grimes 		count = min(m->m_len - off, len);
329df8bae1dSRodney W. Grimes 		bcopy(mtod(m, caddr_t) + off, cp, count);
330df8bae1dSRodney W. Grimes 		len -= count;
331df8bae1dSRodney W. Grimes 		cp += count;
332df8bae1dSRodney W. Grimes 		off = 0;
333df8bae1dSRodney W. Grimes 		m = m->m_next;
334df8bae1dSRodney W. Grimes 	}
335df8bae1dSRodney W. Grimes }
336df8bae1dSRodney W. Grimes 
337df8bae1dSRodney W. Grimes /*
3381c38f2eaSArchie Cobbs  * Copy a packet header mbuf chain into a completely new chain, including
3391c38f2eaSArchie Cobbs  * copying any mbuf clusters.  Use this instead of m_copypacket() when
3401c38f2eaSArchie Cobbs  * you need a writable copy of an mbuf chain.
3411c38f2eaSArchie Cobbs  */
3421c38f2eaSArchie Cobbs struct mbuf *
343122a814aSBosko Milekic m_dup(struct mbuf *m, int how)
3441c38f2eaSArchie Cobbs {
3451c38f2eaSArchie Cobbs 	struct mbuf **p, *top = NULL;
3461c38f2eaSArchie Cobbs 	int remain, moff, nsize;
3471c38f2eaSArchie Cobbs 
3481c38f2eaSArchie Cobbs 	/* Sanity check */
3491c38f2eaSArchie Cobbs 	if (m == NULL)
350122a814aSBosko Milekic 		return (NULL);
351fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
3521c38f2eaSArchie Cobbs 
3531c38f2eaSArchie Cobbs 	/* While there's more data, get a new mbuf, tack it on, and fill it */
3541c38f2eaSArchie Cobbs 	remain = m->m_pkthdr.len;
3551c38f2eaSArchie Cobbs 	moff = 0;
3561c38f2eaSArchie Cobbs 	p = &top;
3571c38f2eaSArchie Cobbs 	while (remain > 0 || top == NULL) {	/* allow m->m_pkthdr.len == 0 */
3581c38f2eaSArchie Cobbs 		struct mbuf *n;
3591c38f2eaSArchie Cobbs 
3601c38f2eaSArchie Cobbs 		/* Get the next new mbuf */
3611c38f2eaSArchie Cobbs 		MGET(n, how, m->m_type);
3621c38f2eaSArchie Cobbs 		if (n == NULL)
3631c38f2eaSArchie Cobbs 			goto nospace;
3641c38f2eaSArchie Cobbs 		if (top == NULL) {		/* first one, must be PKTHDR */
3659967cafcSSam Leffler 			if (!m_dup_pkthdr(n, m, how))
3669967cafcSSam Leffler 				goto nospace;
3671c38f2eaSArchie Cobbs 			nsize = MHLEN;
3681c38f2eaSArchie Cobbs 		} else				/* not the first one */
3691c38f2eaSArchie Cobbs 			nsize = MLEN;
3701c38f2eaSArchie Cobbs 		if (remain >= MINCLSIZE) {
3711c38f2eaSArchie Cobbs 			MCLGET(n, how);
3721c38f2eaSArchie Cobbs 			if ((n->m_flags & M_EXT) == 0) {
3731c38f2eaSArchie Cobbs 				(void)m_free(n);
3741c38f2eaSArchie Cobbs 				goto nospace;
3751c38f2eaSArchie Cobbs 			}
3761c38f2eaSArchie Cobbs 			nsize = MCLBYTES;
3771c38f2eaSArchie Cobbs 		}
3781c38f2eaSArchie Cobbs 		n->m_len = 0;
3791c38f2eaSArchie Cobbs 
3801c38f2eaSArchie Cobbs 		/* Link it into the new chain */
3811c38f2eaSArchie Cobbs 		*p = n;
3821c38f2eaSArchie Cobbs 		p = &n->m_next;
3831c38f2eaSArchie Cobbs 
3841c38f2eaSArchie Cobbs 		/* Copy data from original mbuf(s) into new mbuf */
3851c38f2eaSArchie Cobbs 		while (n->m_len < nsize && m != NULL) {
3861c38f2eaSArchie Cobbs 			int chunk = min(nsize - n->m_len, m->m_len - moff);
3871c38f2eaSArchie Cobbs 
3881c38f2eaSArchie Cobbs 			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
3891c38f2eaSArchie Cobbs 			moff += chunk;
3901c38f2eaSArchie Cobbs 			n->m_len += chunk;
3911c38f2eaSArchie Cobbs 			remain -= chunk;
3921c38f2eaSArchie Cobbs 			if (moff == m->m_len) {
3931c38f2eaSArchie Cobbs 				m = m->m_next;
3941c38f2eaSArchie Cobbs 				moff = 0;
3951c38f2eaSArchie Cobbs 			}
3961c38f2eaSArchie Cobbs 		}
3971c38f2eaSArchie Cobbs 
3981c38f2eaSArchie Cobbs 		/* Check correct total mbuf length */
3991c38f2eaSArchie Cobbs 		KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
400a48740b6SDavid E. O'Brien 		    	("%s: bogus m_pkthdr.len", __func__));
4011c38f2eaSArchie Cobbs 	}
4021c38f2eaSArchie Cobbs 	return (top);
4031c38f2eaSArchie Cobbs 
4041c38f2eaSArchie Cobbs nospace:
4051c38f2eaSArchie Cobbs 	m_freem(top);
40608442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
407122a814aSBosko Milekic 	return (NULL);
4081c38f2eaSArchie Cobbs }
4091c38f2eaSArchie Cobbs 
4101c38f2eaSArchie Cobbs /*
411df8bae1dSRodney W. Grimes  * Concatenate mbuf chain n to m.
412df8bae1dSRodney W. Grimes  * Both chains must be of the same type (e.g. MT_DATA).
413df8bae1dSRodney W. Grimes  * Any m_pkthdr is not updated.
414df8bae1dSRodney W. Grimes  */
41526f9a767SRodney W. Grimes void
416122a814aSBosko Milekic m_cat(struct mbuf *m, struct mbuf *n)
417df8bae1dSRodney W. Grimes {
418df8bae1dSRodney W. Grimes 	while (m->m_next)
419df8bae1dSRodney W. Grimes 		m = m->m_next;
420df8bae1dSRodney W. Grimes 	while (n) {
421df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT ||
422df8bae1dSRodney W. Grimes 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
423df8bae1dSRodney W. Grimes 			/* just join the two chains */
424df8bae1dSRodney W. Grimes 			m->m_next = n;
425df8bae1dSRodney W. Grimes 			return;
426df8bae1dSRodney W. Grimes 		}
427df8bae1dSRodney W. Grimes 		/* splat the data from one into the other */
428df8bae1dSRodney W. Grimes 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
429df8bae1dSRodney W. Grimes 		    (u_int)n->m_len);
430df8bae1dSRodney W. Grimes 		m->m_len += n->m_len;
431df8bae1dSRodney W. Grimes 		n = m_free(n);
432df8bae1dSRodney W. Grimes 	}
433df8bae1dSRodney W. Grimes }
434df8bae1dSRodney W. Grimes 
43526f9a767SRodney W. Grimes void
436122a814aSBosko Milekic m_adj(struct mbuf *mp, int req_len)
437df8bae1dSRodney W. Grimes {
438122a814aSBosko Milekic 	int len = req_len;
439122a814aSBosko Milekic 	struct mbuf *m;
440122a814aSBosko Milekic 	int count;
441df8bae1dSRodney W. Grimes 
442df8bae1dSRodney W. Grimes 	if ((m = mp) == NULL)
443df8bae1dSRodney W. Grimes 		return;
444df8bae1dSRodney W. Grimes 	if (len >= 0) {
445df8bae1dSRodney W. Grimes 		/*
446df8bae1dSRodney W. Grimes 		 * Trim from head.
447df8bae1dSRodney W. Grimes 		 */
448df8bae1dSRodney W. Grimes 		while (m != NULL && len > 0) {
449df8bae1dSRodney W. Grimes 			if (m->m_len <= len) {
450df8bae1dSRodney W. Grimes 				len -= m->m_len;
451df8bae1dSRodney W. Grimes 				m->m_len = 0;
452df8bae1dSRodney W. Grimes 				m = m->m_next;
453df8bae1dSRodney W. Grimes 			} else {
454df8bae1dSRodney W. Grimes 				m->m_len -= len;
455df8bae1dSRodney W. Grimes 				m->m_data += len;
456df8bae1dSRodney W. Grimes 				len = 0;
457df8bae1dSRodney W. Grimes 			}
458df8bae1dSRodney W. Grimes 		}
459df8bae1dSRodney W. Grimes 		m = mp;
460df8bae1dSRodney W. Grimes 		if (mp->m_flags & M_PKTHDR)
461df8bae1dSRodney W. Grimes 			m->m_pkthdr.len -= (req_len - len);
462df8bae1dSRodney W. Grimes 	} else {
463df8bae1dSRodney W. Grimes 		/*
464df8bae1dSRodney W. Grimes 		 * Trim from tail.  Scan the mbuf chain,
465df8bae1dSRodney W. Grimes 		 * calculating its length and finding the last mbuf.
466df8bae1dSRodney W. Grimes 		 * If the adjustment only affects this mbuf, then just
467df8bae1dSRodney W. Grimes 		 * adjust and return.  Otherwise, rescan and truncate
468df8bae1dSRodney W. Grimes 		 * after the remaining size.
469df8bae1dSRodney W. Grimes 		 */
470df8bae1dSRodney W. Grimes 		len = -len;
471df8bae1dSRodney W. Grimes 		count = 0;
472df8bae1dSRodney W. Grimes 		for (;;) {
473df8bae1dSRodney W. Grimes 			count += m->m_len;
474df8bae1dSRodney W. Grimes 			if (m->m_next == (struct mbuf *)0)
475df8bae1dSRodney W. Grimes 				break;
476df8bae1dSRodney W. Grimes 			m = m->m_next;
477df8bae1dSRodney W. Grimes 		}
478df8bae1dSRodney W. Grimes 		if (m->m_len >= len) {
479df8bae1dSRodney W. Grimes 			m->m_len -= len;
480df8bae1dSRodney W. Grimes 			if (mp->m_flags & M_PKTHDR)
481df8bae1dSRodney W. Grimes 				mp->m_pkthdr.len -= len;
482df8bae1dSRodney W. Grimes 			return;
483df8bae1dSRodney W. Grimes 		}
484df8bae1dSRodney W. Grimes 		count -= len;
485df8bae1dSRodney W. Grimes 		if (count < 0)
486df8bae1dSRodney W. Grimes 			count = 0;
487df8bae1dSRodney W. Grimes 		/*
488df8bae1dSRodney W. Grimes 		 * Correct length for chain is "count".
489df8bae1dSRodney W. Grimes 		 * Find the mbuf with last data, adjust its length,
490df8bae1dSRodney W. Grimes 		 * and toss data from remaining mbufs on chain.
491df8bae1dSRodney W. Grimes 		 */
492df8bae1dSRodney W. Grimes 		m = mp;
493df8bae1dSRodney W. Grimes 		if (m->m_flags & M_PKTHDR)
494df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = count;
495df8bae1dSRodney W. Grimes 		for (; m; m = m->m_next) {
496df8bae1dSRodney W. Grimes 			if (m->m_len >= count) {
497df8bae1dSRodney W. Grimes 				m->m_len = count;
498df8bae1dSRodney W. Grimes 				break;
499df8bae1dSRodney W. Grimes 			}
500df8bae1dSRodney W. Grimes 			count -= m->m_len;
501df8bae1dSRodney W. Grimes 		}
502797f2d22SPoul-Henning Kamp 		while (m->m_next)
503797f2d22SPoul-Henning Kamp 			(m = m->m_next) ->m_len = 0;
504df8bae1dSRodney W. Grimes 	}
505df8bae1dSRodney W. Grimes }
506df8bae1dSRodney W. Grimes 
507df8bae1dSRodney W. Grimes /*
508df8bae1dSRodney W. Grimes  * Rearange an mbuf chain so that len bytes are contiguous
509df8bae1dSRodney W. Grimes  * and in the data area of an mbuf (so that mtod and dtom
510df8bae1dSRodney W. Grimes  * will work for a structure of size len).  Returns the resulting
511df8bae1dSRodney W. Grimes  * mbuf chain on success, frees it and returns null on failure.
512df8bae1dSRodney W. Grimes  * If there is room, it will add up to max_protohdr-len extra bytes to the
513df8bae1dSRodney W. Grimes  * contiguous region in an attempt to avoid being called next time.
514df8bae1dSRodney W. Grimes  */
515df8bae1dSRodney W. Grimes struct mbuf *
516122a814aSBosko Milekic m_pullup(struct mbuf *n, int len)
517df8bae1dSRodney W. Grimes {
518122a814aSBosko Milekic 	struct mbuf *m;
519122a814aSBosko Milekic 	int count;
520df8bae1dSRodney W. Grimes 	int space;
521df8bae1dSRodney W. Grimes 
522df8bae1dSRodney W. Grimes 	/*
523df8bae1dSRodney W. Grimes 	 * If first mbuf has no cluster, and has room for len bytes
524df8bae1dSRodney W. Grimes 	 * without shifting current data, pullup into it,
525df8bae1dSRodney W. Grimes 	 * otherwise allocate a new mbuf to prepend to the chain.
526df8bae1dSRodney W. Grimes 	 */
527df8bae1dSRodney W. Grimes 	if ((n->m_flags & M_EXT) == 0 &&
528df8bae1dSRodney W. Grimes 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
529df8bae1dSRodney W. Grimes 		if (n->m_len >= len)
530df8bae1dSRodney W. Grimes 			return (n);
531df8bae1dSRodney W. Grimes 		m = n;
532df8bae1dSRodney W. Grimes 		n = n->m_next;
533df8bae1dSRodney W. Grimes 		len -= m->m_len;
534df8bae1dSRodney W. Grimes 	} else {
535df8bae1dSRodney W. Grimes 		if (len > MHLEN)
536df8bae1dSRodney W. Grimes 			goto bad;
537a163d034SWarner Losh 		MGET(m, M_DONTWAIT, n->m_type);
538122a814aSBosko Milekic 		if (m == NULL)
539df8bae1dSRodney W. Grimes 			goto bad;
540df8bae1dSRodney W. Grimes 		m->m_len = 0;
5419967cafcSSam Leffler 		if (n->m_flags & M_PKTHDR)
5429967cafcSSam Leffler 			M_MOVE_PKTHDR(m, n);
543df8bae1dSRodney W. Grimes 	}
544df8bae1dSRodney W. Grimes 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
545df8bae1dSRodney W. Grimes 	do {
546df8bae1dSRodney W. Grimes 		count = min(min(max(len, max_protohdr), space), n->m_len);
547df8bae1dSRodney W. Grimes 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
548bd395ae8SBosko Milekic 		  (u_int)count);
549df8bae1dSRodney W. Grimes 		len -= count;
550df8bae1dSRodney W. Grimes 		m->m_len += count;
551df8bae1dSRodney W. Grimes 		n->m_len -= count;
552df8bae1dSRodney W. Grimes 		space -= count;
553df8bae1dSRodney W. Grimes 		if (n->m_len)
554df8bae1dSRodney W. Grimes 			n->m_data += count;
555df8bae1dSRodney W. Grimes 		else
556df8bae1dSRodney W. Grimes 			n = m_free(n);
557df8bae1dSRodney W. Grimes 	} while (len > 0 && n);
558df8bae1dSRodney W. Grimes 	if (len > 0) {
559df8bae1dSRodney W. Grimes 		(void) m_free(m);
560df8bae1dSRodney W. Grimes 		goto bad;
561df8bae1dSRodney W. Grimes 	}
562df8bae1dSRodney W. Grimes 	m->m_next = n;
563df8bae1dSRodney W. Grimes 	return (m);
564df8bae1dSRodney W. Grimes bad:
565df8bae1dSRodney W. Grimes 	m_freem(n);
56608442f8aSBosko Milekic 	mbstat.m_mpfail++;	/* XXX: No consistency. */
567122a814aSBosko Milekic 	return (NULL);
568df8bae1dSRodney W. Grimes }
569df8bae1dSRodney W. Grimes 
570df8bae1dSRodney W. Grimes /*
571df8bae1dSRodney W. Grimes  * Partition an mbuf chain in two pieces, returning the tail --
572df8bae1dSRodney W. Grimes  * all but the first len0 bytes.  In case of failure, it returns NULL and
573df8bae1dSRodney W. Grimes  * attempts to restore the chain to its original state.
57448d183faSArchie Cobbs  *
57548d183faSArchie Cobbs  * Note that the resulting mbufs might be read-only, because the new
57648d183faSArchie Cobbs  * mbuf can end up sharing an mbuf cluster with the original mbuf if
57748d183faSArchie Cobbs  * the "breaking point" happens to lie within a cluster mbuf. Use the
57848d183faSArchie Cobbs  * M_WRITABLE() macro to check for this case.
579df8bae1dSRodney W. Grimes  */
580df8bae1dSRodney W. Grimes struct mbuf *
581122a814aSBosko Milekic m_split(struct mbuf *m0, int len0, int wait)
582df8bae1dSRodney W. Grimes {
583122a814aSBosko Milekic 	struct mbuf *m, *n;
584bd395ae8SBosko Milekic 	u_int len = len0, remain;
585df8bae1dSRodney W. Grimes 
586df8bae1dSRodney W. Grimes 	for (m = m0; m && len > m->m_len; m = m->m_next)
587df8bae1dSRodney W. Grimes 		len -= m->m_len;
588122a814aSBosko Milekic 	if (m == NULL)
589122a814aSBosko Milekic 		return (NULL);
590df8bae1dSRodney W. Grimes 	remain = m->m_len - len;
591df8bae1dSRodney W. Grimes 	if (m0->m_flags & M_PKTHDR) {
592df8bae1dSRodney W. Grimes 		MGETHDR(n, wait, m0->m_type);
593122a814aSBosko Milekic 		if (n == NULL)
594122a814aSBosko Milekic 			return (NULL);
595df8bae1dSRodney W. Grimes 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
596df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
597df8bae1dSRodney W. Grimes 		m0->m_pkthdr.len = len0;
598df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT)
599df8bae1dSRodney W. Grimes 			goto extpacket;
600df8bae1dSRodney W. Grimes 		if (remain > MHLEN) {
601df8bae1dSRodney W. Grimes 			/* m can't be the lead packet */
602df8bae1dSRodney W. Grimes 			MH_ALIGN(n, 0);
603df8bae1dSRodney W. Grimes 			n->m_next = m_split(m, len, wait);
604122a814aSBosko Milekic 			if (n->m_next == NULL) {
605df8bae1dSRodney W. Grimes 				(void) m_free(n);
606122a814aSBosko Milekic 				return (NULL);
60740376987SJeffrey Hsu 			} else {
60840376987SJeffrey Hsu 				n->m_len = 0;
609df8bae1dSRodney W. Grimes 				return (n);
61040376987SJeffrey Hsu 			}
611df8bae1dSRodney W. Grimes 		} else
612df8bae1dSRodney W. Grimes 			MH_ALIGN(n, remain);
613df8bae1dSRodney W. Grimes 	} else if (remain == 0) {
614df8bae1dSRodney W. Grimes 		n = m->m_next;
615122a814aSBosko Milekic 		m->m_next = NULL;
616df8bae1dSRodney W. Grimes 		return (n);
617df8bae1dSRodney W. Grimes 	} else {
618df8bae1dSRodney W. Grimes 		MGET(n, wait, m->m_type);
619122a814aSBosko Milekic 		if (n == NULL)
620122a814aSBosko Milekic 			return (NULL);
621df8bae1dSRodney W. Grimes 		M_ALIGN(n, remain);
622df8bae1dSRodney W. Grimes 	}
623df8bae1dSRodney W. Grimes extpacket:
624df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT) {
625df8bae1dSRodney W. Grimes 		n->m_flags |= M_EXT;
626df8bae1dSRodney W. Grimes 		n->m_ext = m->m_ext;
627a5c4836dSDavid Malone 		MEXT_ADD_REF(m);
628df8bae1dSRodney W. Grimes 		n->m_data = m->m_data + len;
629df8bae1dSRodney W. Grimes 	} else {
630df8bae1dSRodney W. Grimes 		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
631df8bae1dSRodney W. Grimes 	}
632df8bae1dSRodney W. Grimes 	n->m_len = remain;
633df8bae1dSRodney W. Grimes 	m->m_len = len;
634df8bae1dSRodney W. Grimes 	n->m_next = m->m_next;
635122a814aSBosko Milekic 	m->m_next = NULL;
636df8bae1dSRodney W. Grimes 	return (n);
637df8bae1dSRodney W. Grimes }
638df8bae1dSRodney W. Grimes /*
639df8bae1dSRodney W. Grimes  * Routine to copy from device local memory into mbufs.
640f5eece3fSBosko Milekic  * Note that `off' argument is offset into first mbuf of target chain from
641f5eece3fSBosko Milekic  * which to begin copying the data to.
642df8bae1dSRodney W. Grimes  */
643df8bae1dSRodney W. Grimes struct mbuf *
644f5eece3fSBosko Milekic m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
645122a814aSBosko Milekic 	 void (*copy)(char *from, caddr_t to, u_int len))
646df8bae1dSRodney W. Grimes {
647122a814aSBosko Milekic 	struct mbuf *m;
648df8bae1dSRodney W. Grimes 	struct mbuf *top = 0, **mp = &top;
649f5eece3fSBosko Milekic 	int len;
650df8bae1dSRodney W. Grimes 
651f5eece3fSBosko Milekic 	if (off < 0 || off > MHLEN)
652f5eece3fSBosko Milekic 		return (NULL);
653f5eece3fSBosko Milekic 
654a163d034SWarner Losh 	MGETHDR(m, M_DONTWAIT, MT_DATA);
655122a814aSBosko Milekic 	if (m == NULL)
656122a814aSBosko Milekic 		return (NULL);
657df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = ifp;
658df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = totlen;
659f5eece3fSBosko Milekic 	len = MHLEN;
660df8bae1dSRodney W. Grimes 
661df8bae1dSRodney W. Grimes 	while (totlen > 0) {
662df8bae1dSRodney W. Grimes 		if (top) {
663a163d034SWarner Losh 			MGET(m, M_DONTWAIT, MT_DATA);
664122a814aSBosko Milekic 			if (m == NULL) {
665df8bae1dSRodney W. Grimes 				m_freem(top);
666122a814aSBosko Milekic 				return (NULL);
667df8bae1dSRodney W. Grimes 			}
668f5eece3fSBosko Milekic 			len = MLEN;
669df8bae1dSRodney W. Grimes 		}
670f5eece3fSBosko Milekic 		if (totlen + off >= MINCLSIZE) {
671a163d034SWarner Losh 			MCLGET(m, M_DONTWAIT);
672df8bae1dSRodney W. Grimes 			if (m->m_flags & M_EXT)
673f5eece3fSBosko Milekic 				len = MCLBYTES;
674df8bae1dSRodney W. Grimes 		} else {
675df8bae1dSRodney W. Grimes 			/*
676df8bae1dSRodney W. Grimes 			 * Place initial small packet/header at end of mbuf.
677df8bae1dSRodney W. Grimes 			 */
678f5eece3fSBosko Milekic 			if (top == NULL && totlen + off + max_linkhdr <= len) {
679df8bae1dSRodney W. Grimes 				m->m_data += max_linkhdr;
680f5eece3fSBosko Milekic 				len -= max_linkhdr;
681df8bae1dSRodney W. Grimes 			}
682f5eece3fSBosko Milekic 		}
683f5eece3fSBosko Milekic 		if (off) {
684f5eece3fSBosko Milekic 			m->m_data += off;
685f5eece3fSBosko Milekic 			len -= off;
686f5eece3fSBosko Milekic 			off = 0;
687f5eece3fSBosko Milekic 		}
688f5eece3fSBosko Milekic 		m->m_len = len = min(totlen, len);
689df8bae1dSRodney W. Grimes 		if (copy)
690bd395ae8SBosko Milekic 			copy(buf, mtod(m, caddr_t), (u_int)len);
691df8bae1dSRodney W. Grimes 		else
692bd395ae8SBosko Milekic 			bcopy(buf, mtod(m, caddr_t), (u_int)len);
693f5eece3fSBosko Milekic 		buf += len;
694df8bae1dSRodney W. Grimes 		*mp = m;
695df8bae1dSRodney W. Grimes 		mp = &m->m_next;
696df8bae1dSRodney W. Grimes 		totlen -= len;
697df8bae1dSRodney W. Grimes 	}
698df8bae1dSRodney W. Grimes 	return (top);
699df8bae1dSRodney W. Grimes }
700c5789ba3SPoul-Henning Kamp 
701c5789ba3SPoul-Henning Kamp /*
702c5789ba3SPoul-Henning Kamp  * Copy data from a buffer back into the indicated mbuf chain,
703c5789ba3SPoul-Henning Kamp  * starting "off" bytes from the beginning, extending the mbuf
704c5789ba3SPoul-Henning Kamp  * chain if necessary.
705c5789ba3SPoul-Henning Kamp  */
706c5789ba3SPoul-Henning Kamp void
707122a814aSBosko Milekic m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
708c5789ba3SPoul-Henning Kamp {
709122a814aSBosko Milekic 	int mlen;
710122a814aSBosko Milekic 	struct mbuf *m = m0, *n;
711c5789ba3SPoul-Henning Kamp 	int totlen = 0;
712c5789ba3SPoul-Henning Kamp 
713122a814aSBosko Milekic 	if (m0 == NULL)
714c5789ba3SPoul-Henning Kamp 		return;
715c5789ba3SPoul-Henning Kamp 	while (off > (mlen = m->m_len)) {
716c5789ba3SPoul-Henning Kamp 		off -= mlen;
717c5789ba3SPoul-Henning Kamp 		totlen += mlen;
718122a814aSBosko Milekic 		if (m->m_next == NULL) {
719a163d034SWarner Losh 			n = m_get_clrd(M_DONTWAIT, m->m_type);
720122a814aSBosko Milekic 			if (n == NULL)
721c5789ba3SPoul-Henning Kamp 				goto out;
722c5789ba3SPoul-Henning Kamp 			n->m_len = min(MLEN, len + off);
723c5789ba3SPoul-Henning Kamp 			m->m_next = n;
724c5789ba3SPoul-Henning Kamp 		}
725c5789ba3SPoul-Henning Kamp 		m = m->m_next;
726c5789ba3SPoul-Henning Kamp 	}
727c5789ba3SPoul-Henning Kamp 	while (len > 0) {
728c5789ba3SPoul-Henning Kamp 		mlen = min (m->m_len - off, len);
729bd395ae8SBosko Milekic 		bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
730c5789ba3SPoul-Henning Kamp 		cp += mlen;
731c5789ba3SPoul-Henning Kamp 		len -= mlen;
732c5789ba3SPoul-Henning Kamp 		mlen += off;
733c5789ba3SPoul-Henning Kamp 		off = 0;
734c5789ba3SPoul-Henning Kamp 		totlen += mlen;
735c5789ba3SPoul-Henning Kamp 		if (len == 0)
736c5789ba3SPoul-Henning Kamp 			break;
737122a814aSBosko Milekic 		if (m->m_next == NULL) {
738a163d034SWarner Losh 			n = m_get(M_DONTWAIT, m->m_type);
739122a814aSBosko Milekic 			if (n == NULL)
740c5789ba3SPoul-Henning Kamp 				break;
741c5789ba3SPoul-Henning Kamp 			n->m_len = min(MLEN, len);
742c5789ba3SPoul-Henning Kamp 			m->m_next = n;
743c5789ba3SPoul-Henning Kamp 		}
744c5789ba3SPoul-Henning Kamp 		m = m->m_next;
745c5789ba3SPoul-Henning Kamp 	}
746c5789ba3SPoul-Henning Kamp out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
747c5789ba3SPoul-Henning Kamp 		m->m_pkthdr.len = totlen;
748c5789ba3SPoul-Henning Kamp }
749ce4a64f7SPoul-Henning Kamp 
750ce4a64f7SPoul-Henning Kamp void
751ce4a64f7SPoul-Henning Kamp m_print(const struct mbuf *m)
752ce4a64f7SPoul-Henning Kamp {
753ce4a64f7SPoul-Henning Kamp 	int len;
7546357e7b5SEivind Eklund 	const struct mbuf *m2;
755ce4a64f7SPoul-Henning Kamp 
756ce4a64f7SPoul-Henning Kamp 	len = m->m_pkthdr.len;
757ce4a64f7SPoul-Henning Kamp 	m2 = m;
758ce4a64f7SPoul-Henning Kamp 	while (len) {
759ce4a64f7SPoul-Henning Kamp 		printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");
760ce4a64f7SPoul-Henning Kamp 		len -= m2->m_len;
761ce4a64f7SPoul-Henning Kamp 		m2 = m2->m_next;
762ce4a64f7SPoul-Henning Kamp 	}
763ce4a64f7SPoul-Henning Kamp 	return;
764ce4a64f7SPoul-Henning Kamp }
7653f2e06c5SPoul-Henning Kamp 
766bd395ae8SBosko Milekic u_int
7673f2e06c5SPoul-Henning Kamp m_fixhdr(struct mbuf *m0)
7683f2e06c5SPoul-Henning Kamp {
769bd395ae8SBosko Milekic 	u_int len;
7703f2e06c5SPoul-Henning Kamp 
771ac6e585dSPoul-Henning Kamp 	len = m_length(m0, NULL);
7723f2e06c5SPoul-Henning Kamp 	m0->m_pkthdr.len = len;
773ac6e585dSPoul-Henning Kamp 	return (len);
774ac6e585dSPoul-Henning Kamp }
775ac6e585dSPoul-Henning Kamp 
776bd395ae8SBosko Milekic u_int
777ac6e585dSPoul-Henning Kamp m_length(struct mbuf *m0, struct mbuf **last)
778ac6e585dSPoul-Henning Kamp {
779ac6e585dSPoul-Henning Kamp 	struct mbuf *m;
780bd395ae8SBosko Milekic 	u_int len;
781ac6e585dSPoul-Henning Kamp 
782ac6e585dSPoul-Henning Kamp 	len = 0;
783ac6e585dSPoul-Henning Kamp 	for (m = m0; m != NULL; m = m->m_next) {
784ac6e585dSPoul-Henning Kamp 		len += m->m_len;
785ac6e585dSPoul-Henning Kamp 		if (m->m_next == NULL)
786ac6e585dSPoul-Henning Kamp 			break;
787ac6e585dSPoul-Henning Kamp 	}
788ac6e585dSPoul-Henning Kamp 	if (last != NULL)
789ac6e585dSPoul-Henning Kamp 		*last = m;
790ac6e585dSPoul-Henning Kamp 	return (len);
7913f2e06c5SPoul-Henning Kamp }
79255e9f80dSMike Silbersack 
79355e9f80dSMike Silbersack /*
79455e9f80dSMike Silbersack  * Defragment a mbuf chain, returning the shortest possible
79555e9f80dSMike Silbersack  * chain of mbufs and clusters.  If allocation fails and
79655e9f80dSMike Silbersack  * this cannot be completed, NULL will be returned, but
79755e9f80dSMike Silbersack  * the passed in chain will be unchanged.  Upon success,
79855e9f80dSMike Silbersack  * the original chain will be freed, and the new chain
79955e9f80dSMike Silbersack  * will be returned.
80055e9f80dSMike Silbersack  *
80155e9f80dSMike Silbersack  * If a non-packet header is passed in, the original
80255e9f80dSMike Silbersack  * mbuf (chain?) will be returned unharmed.
80355e9f80dSMike Silbersack  */
80455e9f80dSMike Silbersack struct mbuf *
80555e9f80dSMike Silbersack m_defrag(struct mbuf *m0, int how)
80655e9f80dSMike Silbersack {
80755e9f80dSMike Silbersack 	struct mbuf	*m_new = NULL, *m_final = NULL;
80855e9f80dSMike Silbersack 	int		progress = 0, length;
80955e9f80dSMike Silbersack 
81055e9f80dSMike Silbersack 	if (!(m0->m_flags & M_PKTHDR))
81155e9f80dSMike Silbersack 		return (m0);
81255e9f80dSMike Silbersack 
813352d050eSMike Silbersack #ifdef MBUF_STRESS_TEST
814352d050eSMike Silbersack 	if (m_defragrandomfailures) {
815352d050eSMike Silbersack 		int temp = arc4random() & 0xff;
816352d050eSMike Silbersack 		if (temp == 0xba)
817352d050eSMike Silbersack 			goto nospace;
818352d050eSMike Silbersack 	}
819352d050eSMike Silbersack #endif
82055e9f80dSMike Silbersack 
82155e9f80dSMike Silbersack 	if (m0->m_pkthdr.len > MHLEN)
82255e9f80dSMike Silbersack 		m_final = m_getcl(how, MT_DATA, M_PKTHDR);
82355e9f80dSMike Silbersack 	else
82455e9f80dSMike Silbersack 		m_final = m_gethdr(how, MT_DATA);
82555e9f80dSMike Silbersack 
82655e9f80dSMike Silbersack 	if (m_final == NULL)
82755e9f80dSMike Silbersack 		goto nospace;
82855e9f80dSMike Silbersack 
82955e9f80dSMike Silbersack 	if (m_dup_pkthdr(m_final, m0, how) == NULL)
83055e9f80dSMike Silbersack 		goto nospace;
83155e9f80dSMike Silbersack 
83255e9f80dSMike Silbersack 	m_new = m_final;
83355e9f80dSMike Silbersack 
83455e9f80dSMike Silbersack 	while (progress < m0->m_pkthdr.len) {
83555e9f80dSMike Silbersack 		length = m0->m_pkthdr.len - progress;
83655e9f80dSMike Silbersack 		if (length > MCLBYTES)
83755e9f80dSMike Silbersack 			length = MCLBYTES;
83855e9f80dSMike Silbersack 
83955e9f80dSMike Silbersack 		if (m_new == NULL) {
84055e9f80dSMike Silbersack 			if (length > MLEN)
84155e9f80dSMike Silbersack 				m_new = m_getcl(how, MT_DATA, 0);
84255e9f80dSMike Silbersack 			else
84355e9f80dSMike Silbersack 				m_new = m_get(how, MT_DATA);
84455e9f80dSMike Silbersack 			if (m_new == NULL)
84555e9f80dSMike Silbersack 				goto nospace;
84655e9f80dSMike Silbersack 		}
84755e9f80dSMike Silbersack 
84855e9f80dSMike Silbersack 		m_copydata(m0, progress, length, mtod(m_new, caddr_t));
84955e9f80dSMike Silbersack 		progress += length;
85055e9f80dSMike Silbersack 		m_new->m_len = length;
85155e9f80dSMike Silbersack 		if (m_new != m_final)
85255e9f80dSMike Silbersack 			m_cat(m_final, m_new);
85355e9f80dSMike Silbersack 		m_new = NULL;
85455e9f80dSMike Silbersack 	}
85555e9f80dSMike Silbersack 	if (m0->m_next == NULL)
85655e9f80dSMike Silbersack 		m_defraguseless++;
85755e9f80dSMike Silbersack 	m_freem(m0);
85855e9f80dSMike Silbersack 	m0 = m_final;
85955e9f80dSMike Silbersack 	m_defragpackets++;
86055e9f80dSMike Silbersack 	m_defragbytes += m0->m_pkthdr.len;
86155e9f80dSMike Silbersack 	return (m0);
86255e9f80dSMike Silbersack nospace:
86355e9f80dSMike Silbersack 	m_defragfailure++;
86455e9f80dSMike Silbersack 	if (m_new)
86555e9f80dSMike Silbersack 		m_free(m_new);
86655e9f80dSMike Silbersack 	if (m_final)
86755e9f80dSMike Silbersack 		m_freem(m_final);
86855e9f80dSMike Silbersack 	return (NULL);
86955e9f80dSMike Silbersack }
870