xref: /freebsd/sys/kern/uipc_mbuf.c (revision 48d183faca1a4eab786d57c494f6a063ef95d1d2)
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 
37240ef842SDavid E. O'Brien #include "opt_param.h"
38df8bae1dSRodney W. Grimes #include <sys/param.h>
39df8bae1dSRodney W. Grimes #include <sys/systm.h>
40fb919e4dSMark Murray #include <sys/kernel.h>
41fb919e4dSMark Murray #include <sys/lock.h>
4264cfdf46SBruce Evans #include <sys/malloc.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
44639acc13SGarrett Wollman #include <sys/sysctl.h>
45df8bae1dSRodney W. Grimes #include <sys/domain.h>
46df8bae1dSRodney W. Grimes #include <sys/protosw.h>
47fb919e4dSMark Murray 
4828f8db14SBruce Evans int	max_linkhdr;
4928f8db14SBruce Evans int	max_protohdr;
5028f8db14SBruce Evans int	max_hdr;
5128f8db14SBruce Evans int	max_datalen;
527d032714SBosko Milekic 
537d032714SBosko Milekic /*
547d032714SBosko Milekic  * sysctl(8) exported objects
557d032714SBosko Milekic  */
56ce02431fSDoug Rabson SYSCTL_DECL(_kern_ipc);
57639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
58639acc13SGarrett Wollman 	   &max_linkhdr, 0, "");
59639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
60639acc13SGarrett Wollman 	   &max_protohdr, 0, "");
61639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
62639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
63639acc13SGarrett Wollman 	   &max_datalen, 0, "");
64df8bae1dSRodney W. Grimes 
65fffd12bdSBosko Milekic /*
66fffd12bdSBosko Milekic  * struct mbuf *
67fffd12bdSBosko Milekic  * m_getm(m, len, how, type)
68fffd12bdSBosko Milekic  *
69fffd12bdSBosko Milekic  * This will allocate len-worth of mbufs and/or mbuf clusters (whatever fits
70fffd12bdSBosko Milekic  * best) and return a pointer to the top of the allocated chain. If m is
71fffd12bdSBosko Milekic  * non-null, then we assume that it is a single mbuf or an mbuf chain to
72fffd12bdSBosko Milekic  * which we want len bytes worth of mbufs and/or clusters attached, and so
73fffd12bdSBosko Milekic  * if we succeed in allocating it, we will just return a pointer to m.
74fffd12bdSBosko Milekic  *
75fffd12bdSBosko Milekic  * If we happen to fail at any point during the allocation, we will free
76fffd12bdSBosko Milekic  * up everything we have already allocated and return NULL.
77fffd12bdSBosko Milekic  *
78fffd12bdSBosko Milekic  */
79fffd12bdSBosko Milekic struct mbuf *
80fffd12bdSBosko Milekic m_getm(struct mbuf *m, int len, int how, int type)
81fffd12bdSBosko Milekic {
82fffd12bdSBosko Milekic 	struct mbuf *top, *tail, *mp, *mtail = NULL;
83fffd12bdSBosko Milekic 
84fffd12bdSBosko Milekic 	KASSERT(len >= 0, ("len is < 0 in m_getm"));
85fffd12bdSBosko Milekic 
8603137ec8SBoris Popov 	MGET(mp, how, type);
87fffd12bdSBosko Milekic 	if (mp == NULL)
88fffd12bdSBosko Milekic 		return (NULL);
89fffd12bdSBosko Milekic 	else if (len > MINCLSIZE) {
90fffd12bdSBosko Milekic 		MCLGET(mp, how);
91fffd12bdSBosko Milekic 		if ((mp->m_flags & M_EXT) == 0) {
92fffd12bdSBosko Milekic 			m_free(mp);
93fffd12bdSBosko Milekic 			return (NULL);
94fffd12bdSBosko Milekic 		}
95fffd12bdSBosko Milekic 	}
96fffd12bdSBosko Milekic 	mp->m_len = 0;
97fffd12bdSBosko Milekic 	len -= M_TRAILINGSPACE(mp);
98fffd12bdSBosko Milekic 
99fffd12bdSBosko Milekic 	if (m != NULL)
100fffd12bdSBosko Milekic 		for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next);
101fffd12bdSBosko Milekic 	else
102fffd12bdSBosko Milekic 		m = mp;
103fffd12bdSBosko Milekic 
104fffd12bdSBosko Milekic 	top = tail = mp;
105fffd12bdSBosko Milekic 	while (len > 0) {
10603137ec8SBoris Popov 		MGET(mp, how, type);
107fffd12bdSBosko Milekic 		if (mp == NULL)
108fffd12bdSBosko Milekic 			goto failed;
109fffd12bdSBosko Milekic 
110fffd12bdSBosko Milekic 		tail->m_next = mp;
111fffd12bdSBosko Milekic 		tail = mp;
112fffd12bdSBosko Milekic 		if (len > MINCLSIZE) {
113fffd12bdSBosko Milekic 			MCLGET(mp, how);
114fffd12bdSBosko Milekic 			if ((mp->m_flags & M_EXT) == 0)
115fffd12bdSBosko Milekic 				goto failed;
116fffd12bdSBosko Milekic 		}
117fffd12bdSBosko Milekic 
118fffd12bdSBosko Milekic 		mp->m_len = 0;
119fffd12bdSBosko Milekic 		len -= M_TRAILINGSPACE(mp);
120fffd12bdSBosko Milekic 	}
121fffd12bdSBosko Milekic 
122fffd12bdSBosko Milekic 	if (mtail != NULL)
123fffd12bdSBosko Milekic 		mtail->m_next = top;
124fffd12bdSBosko Milekic 	return (m);
125fffd12bdSBosko Milekic 
126fffd12bdSBosko Milekic failed:
127fffd12bdSBosko Milekic 	m_freem(top);
128fffd12bdSBosko Milekic 	return (NULL);
129fffd12bdSBosko Milekic }
130fffd12bdSBosko Milekic 
131df8bae1dSRodney W. Grimes void
132122a814aSBosko Milekic m_freem(struct mbuf *m)
133df8bae1dSRodney W. Grimes {
134ecde8f7cSMatthew Dillon 	while (m) {
135ecde8f7cSMatthew Dillon 		m = m_free(m);
136ecde8f7cSMatthew Dillon 	}
137df8bae1dSRodney W. Grimes }
138df8bae1dSRodney W. Grimes 
139df8bae1dSRodney W. Grimes /*
140df8bae1dSRodney W. Grimes  * Lesser-used path for M_PREPEND:
141df8bae1dSRodney W. Grimes  * allocate new mbuf to prepend to chain,
142df8bae1dSRodney W. Grimes  * copy junk along.
143df8bae1dSRodney W. Grimes  */
144df8bae1dSRodney W. Grimes struct mbuf *
145122a814aSBosko Milekic m_prepend(struct mbuf *m, int len, int how)
146df8bae1dSRodney W. Grimes {
147df8bae1dSRodney W. Grimes 	struct mbuf *mn;
148df8bae1dSRodney W. Grimes 
149df8bae1dSRodney W. Grimes 	MGET(mn, how, m->m_type);
150122a814aSBosko Milekic 	if (mn == NULL) {
151df8bae1dSRodney W. Grimes 		m_freem(m);
152122a814aSBosko Milekic 		return (NULL);
153df8bae1dSRodney W. Grimes 	}
154df8bae1dSRodney W. Grimes 	if (m->m_flags & M_PKTHDR) {
155df8bae1dSRodney W. Grimes 		M_COPY_PKTHDR(mn, m);
156df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_PKTHDR;
157df8bae1dSRodney W. Grimes 	}
158df8bae1dSRodney W. Grimes 	mn->m_next = m;
159df8bae1dSRodney W. Grimes 	m = mn;
160df8bae1dSRodney W. Grimes 	if (len < MHLEN)
161df8bae1dSRodney W. Grimes 		MH_ALIGN(m, len);
162df8bae1dSRodney W. Grimes 	m->m_len = len;
163df8bae1dSRodney W. Grimes 	return (m);
164df8bae1dSRodney W. Grimes }
165df8bae1dSRodney W. Grimes 
166df8bae1dSRodney W. Grimes /*
167df8bae1dSRodney W. Grimes  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
168df8bae1dSRodney W. Grimes  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
1692a0c503eSBosko Milekic  * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
1701c38f2eaSArchie Cobbs  * Note that the copy is read-only, because clusters are not copied,
1711c38f2eaSArchie Cobbs  * only their reference counts are incremented.
172df8bae1dSRodney W. Grimes  */
173df8bae1dSRodney W. Grimes struct mbuf *
174122a814aSBosko Milekic m_copym(struct mbuf *m, int off0, int len, int wait)
175df8bae1dSRodney W. Grimes {
176122a814aSBosko Milekic 	struct mbuf *n, **np;
177122a814aSBosko Milekic 	int off = off0;
178df8bae1dSRodney W. Grimes 	struct mbuf *top;
179df8bae1dSRodney W. Grimes 	int copyhdr = 0;
180df8bae1dSRodney W. Grimes 
181e0a653ddSAlfred Perlstein 	KASSERT(off >= 0, ("m_copym, negative off %d", off));
182e0a653ddSAlfred Perlstein 	KASSERT(len >= 0, ("m_copym, negative len %d", len));
183df8bae1dSRodney W. Grimes 	if (off == 0 && m->m_flags & M_PKTHDR)
184df8bae1dSRodney W. Grimes 		copyhdr = 1;
185df8bae1dSRodney W. Grimes 	while (off > 0) {
186e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
187df8bae1dSRodney W. Grimes 		if (off < m->m_len)
188df8bae1dSRodney W. Grimes 			break;
189df8bae1dSRodney W. Grimes 		off -= m->m_len;
190df8bae1dSRodney W. Grimes 		m = m->m_next;
191df8bae1dSRodney W. Grimes 	}
192df8bae1dSRodney W. Grimes 	np = &top;
193df8bae1dSRodney W. Grimes 	top = 0;
194df8bae1dSRodney W. Grimes 	while (len > 0) {
195122a814aSBosko Milekic 		if (m == NULL) {
196e0a653ddSAlfred Perlstein 			KASSERT(len == M_COPYALL,
197e0a653ddSAlfred Perlstein 			    ("m_copym, length > size of mbuf chain"));
198df8bae1dSRodney W. Grimes 			break;
199df8bae1dSRodney W. Grimes 		}
200df8bae1dSRodney W. Grimes 		MGET(n, wait, m->m_type);
201df8bae1dSRodney W. Grimes 		*np = n;
202122a814aSBosko Milekic 		if (n == NULL)
203df8bae1dSRodney W. Grimes 			goto nospace;
204df8bae1dSRodney W. Grimes 		if (copyhdr) {
205df8bae1dSRodney W. Grimes 			M_COPY_PKTHDR(n, m);
206df8bae1dSRodney W. Grimes 			if (len == M_COPYALL)
207df8bae1dSRodney W. Grimes 				n->m_pkthdr.len -= off0;
208df8bae1dSRodney W. Grimes 			else
209df8bae1dSRodney W. Grimes 				n->m_pkthdr.len = len;
210df8bae1dSRodney W. Grimes 			copyhdr = 0;
211df8bae1dSRodney W. Grimes 		}
212df8bae1dSRodney W. Grimes 		n->m_len = min(len, m->m_len - off);
213df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT) {
214df8bae1dSRodney W. Grimes 			n->m_data = m->m_data + off;
215df8bae1dSRodney W. Grimes 			n->m_ext = m->m_ext;
216df8bae1dSRodney W. Grimes 			n->m_flags |= M_EXT;
217a5c4836dSDavid Malone 			MEXT_ADD_REF(m);
218df8bae1dSRodney W. Grimes 		} else
219df8bae1dSRodney W. Grimes 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
220df8bae1dSRodney W. Grimes 			    (unsigned)n->m_len);
221df8bae1dSRodney W. Grimes 		if (len != M_COPYALL)
222df8bae1dSRodney W. Grimes 			len -= n->m_len;
223df8bae1dSRodney W. Grimes 		off = 0;
224df8bae1dSRodney W. Grimes 		m = m->m_next;
225df8bae1dSRodney W. Grimes 		np = &n->m_next;
226df8bae1dSRodney W. Grimes 	}
22708442f8aSBosko Milekic 	if (top == NULL)
22808442f8aSBosko Milekic 		mbstat.m_mcfail++;	/* XXX: No consistency. */
22908442f8aSBosko Milekic 
230df8bae1dSRodney W. Grimes 	return (top);
231df8bae1dSRodney W. Grimes nospace:
232df8bae1dSRodney W. Grimes 	m_freem(top);
23308442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
234122a814aSBosko Milekic 	return (NULL);
235df8bae1dSRodney W. Grimes }
236df8bae1dSRodney W. Grimes 
237df8bae1dSRodney W. Grimes /*
2386a06dea0SGarrett Wollman  * Copy an entire packet, including header (which must be present).
2396a06dea0SGarrett Wollman  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
2401c38f2eaSArchie Cobbs  * Note that the copy is read-only, because clusters are not copied,
2411c38f2eaSArchie Cobbs  * only their reference counts are incremented.
2425fe86675SLuigi Rizzo  * Preserve alignment of the first mbuf so if the creator has left
2435fe86675SLuigi Rizzo  * some room at the beginning (e.g. for inserting protocol headers)
2445fe86675SLuigi Rizzo  * the copies still have the room available.
2456a06dea0SGarrett Wollman  */
2466a06dea0SGarrett Wollman struct mbuf *
247122a814aSBosko Milekic m_copypacket(struct mbuf *m, int how)
2486a06dea0SGarrett Wollman {
2496a06dea0SGarrett Wollman 	struct mbuf *top, *n, *o;
2506a06dea0SGarrett Wollman 
2516a06dea0SGarrett Wollman 	MGET(n, how, m->m_type);
2526a06dea0SGarrett Wollman 	top = n;
253122a814aSBosko Milekic 	if (n == NULL)
2546a06dea0SGarrett Wollman 		goto nospace;
2556a06dea0SGarrett Wollman 
2566a06dea0SGarrett Wollman 	M_COPY_PKTHDR(n, m);
2576a06dea0SGarrett Wollman 	n->m_len = m->m_len;
2586a06dea0SGarrett Wollman 	if (m->m_flags & M_EXT) {
2596a06dea0SGarrett Wollman 		n->m_data = m->m_data;
2606a06dea0SGarrett Wollman 		n->m_ext = m->m_ext;
2616a06dea0SGarrett Wollman 		n->m_flags |= M_EXT;
262a5c4836dSDavid Malone 		MEXT_ADD_REF(m);
2636a06dea0SGarrett Wollman 	} else {
2645fe86675SLuigi Rizzo 		n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
2656a06dea0SGarrett Wollman 		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
2666a06dea0SGarrett Wollman 	}
2676a06dea0SGarrett Wollman 
2686a06dea0SGarrett Wollman 	m = m->m_next;
2696a06dea0SGarrett Wollman 	while (m) {
2706a06dea0SGarrett Wollman 		MGET(o, how, m->m_type);
271122a814aSBosko Milekic 		if (o == NULL)
2726a06dea0SGarrett Wollman 			goto nospace;
2736a06dea0SGarrett Wollman 
2746a06dea0SGarrett Wollman 		n->m_next = o;
2756a06dea0SGarrett Wollman 		n = n->m_next;
2766a06dea0SGarrett Wollman 
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 {
2846a06dea0SGarrett Wollman 			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
2856a06dea0SGarrett Wollman 		}
2866a06dea0SGarrett Wollman 
2876a06dea0SGarrett Wollman 		m = m->m_next;
2886a06dea0SGarrett Wollman 	}
2896a06dea0SGarrett Wollman 	return top;
2906a06dea0SGarrett Wollman nospace:
2916a06dea0SGarrett Wollman 	m_freem(top);
29208442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
293122a814aSBosko Milekic 	return (NULL);
2946a06dea0SGarrett Wollman }
2956a06dea0SGarrett Wollman 
2966a06dea0SGarrett Wollman /*
297df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain starting "off" bytes from the beginning,
298df8bae1dSRodney W. Grimes  * continuing for "len" bytes, into the indicated buffer.
299df8bae1dSRodney W. Grimes  */
30026f9a767SRodney W. Grimes void
301a8cfc0eeSJulian Elischer m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
302df8bae1dSRodney W. Grimes {
303122a814aSBosko Milekic 	unsigned count;
304df8bae1dSRodney W. Grimes 
305e0a653ddSAlfred Perlstein 	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
306e0a653ddSAlfred Perlstein 	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
307df8bae1dSRodney W. Grimes 	while (off > 0) {
308e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
309df8bae1dSRodney W. Grimes 		if (off < m->m_len)
310df8bae1dSRodney W. Grimes 			break;
311df8bae1dSRodney W. Grimes 		off -= m->m_len;
312df8bae1dSRodney W. Grimes 		m = m->m_next;
313df8bae1dSRodney W. Grimes 	}
314df8bae1dSRodney W. Grimes 	while (len > 0) {
315e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
316df8bae1dSRodney W. Grimes 		count = min(m->m_len - off, len);
317df8bae1dSRodney W. Grimes 		bcopy(mtod(m, caddr_t) + off, cp, count);
318df8bae1dSRodney W. Grimes 		len -= count;
319df8bae1dSRodney W. Grimes 		cp += count;
320df8bae1dSRodney W. Grimes 		off = 0;
321df8bae1dSRodney W. Grimes 		m = m->m_next;
322df8bae1dSRodney W. Grimes 	}
323df8bae1dSRodney W. Grimes }
324df8bae1dSRodney W. Grimes 
325df8bae1dSRodney W. Grimes /*
3261c38f2eaSArchie Cobbs  * Copy a packet header mbuf chain into a completely new chain, including
3271c38f2eaSArchie Cobbs  * copying any mbuf clusters.  Use this instead of m_copypacket() when
3281c38f2eaSArchie Cobbs  * you need a writable copy of an mbuf chain.
3291c38f2eaSArchie Cobbs  */
3301c38f2eaSArchie Cobbs struct mbuf *
331122a814aSBosko Milekic m_dup(struct mbuf *m, int how)
3321c38f2eaSArchie Cobbs {
3331c38f2eaSArchie Cobbs 	struct mbuf **p, *top = NULL;
3341c38f2eaSArchie Cobbs 	int remain, moff, nsize;
3351c38f2eaSArchie Cobbs 
3361c38f2eaSArchie Cobbs 	/* Sanity check */
3371c38f2eaSArchie Cobbs 	if (m == NULL)
338122a814aSBosko Milekic 		return (NULL);
339a48740b6SDavid E. O'Brien 	KASSERT((m->m_flags & M_PKTHDR) != 0, ("%s: !PKTHDR", __func__));
3401c38f2eaSArchie Cobbs 
3411c38f2eaSArchie Cobbs 	/* While there's more data, get a new mbuf, tack it on, and fill it */
3421c38f2eaSArchie Cobbs 	remain = m->m_pkthdr.len;
3431c38f2eaSArchie Cobbs 	moff = 0;
3441c38f2eaSArchie Cobbs 	p = &top;
3451c38f2eaSArchie Cobbs 	while (remain > 0 || top == NULL) {	/* allow m->m_pkthdr.len == 0 */
3461c38f2eaSArchie Cobbs 		struct mbuf *n;
3471c38f2eaSArchie Cobbs 
3481c38f2eaSArchie Cobbs 		/* Get the next new mbuf */
3491c38f2eaSArchie Cobbs 		MGET(n, how, m->m_type);
3501c38f2eaSArchie Cobbs 		if (n == NULL)
3511c38f2eaSArchie Cobbs 			goto nospace;
3521c38f2eaSArchie Cobbs 		if (top == NULL) {		/* first one, must be PKTHDR */
3531c38f2eaSArchie Cobbs 			M_COPY_PKTHDR(n, m);
3541c38f2eaSArchie Cobbs 			nsize = MHLEN;
3551c38f2eaSArchie Cobbs 		} else				/* not the first one */
3561c38f2eaSArchie Cobbs 			nsize = MLEN;
3571c38f2eaSArchie Cobbs 		if (remain >= MINCLSIZE) {
3581c38f2eaSArchie Cobbs 			MCLGET(n, how);
3591c38f2eaSArchie Cobbs 			if ((n->m_flags & M_EXT) == 0) {
3601c38f2eaSArchie Cobbs 				(void)m_free(n);
3611c38f2eaSArchie Cobbs 				goto nospace;
3621c38f2eaSArchie Cobbs 			}
3631c38f2eaSArchie Cobbs 			nsize = MCLBYTES;
3641c38f2eaSArchie Cobbs 		}
3651c38f2eaSArchie Cobbs 		n->m_len = 0;
3661c38f2eaSArchie Cobbs 
3671c38f2eaSArchie Cobbs 		/* Link it into the new chain */
3681c38f2eaSArchie Cobbs 		*p = n;
3691c38f2eaSArchie Cobbs 		p = &n->m_next;
3701c38f2eaSArchie Cobbs 
3711c38f2eaSArchie Cobbs 		/* Copy data from original mbuf(s) into new mbuf */
3721c38f2eaSArchie Cobbs 		while (n->m_len < nsize && m != NULL) {
3731c38f2eaSArchie Cobbs 			int chunk = min(nsize - n->m_len, m->m_len - moff);
3741c38f2eaSArchie Cobbs 
3751c38f2eaSArchie Cobbs 			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
3761c38f2eaSArchie Cobbs 			moff += chunk;
3771c38f2eaSArchie Cobbs 			n->m_len += chunk;
3781c38f2eaSArchie Cobbs 			remain -= chunk;
3791c38f2eaSArchie Cobbs 			if (moff == m->m_len) {
3801c38f2eaSArchie Cobbs 				m = m->m_next;
3811c38f2eaSArchie Cobbs 				moff = 0;
3821c38f2eaSArchie Cobbs 			}
3831c38f2eaSArchie Cobbs 		}
3841c38f2eaSArchie Cobbs 
3851c38f2eaSArchie Cobbs 		/* Check correct total mbuf length */
3861c38f2eaSArchie Cobbs 		KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
387a48740b6SDavid E. O'Brien 		    	("%s: bogus m_pkthdr.len", __func__));
3881c38f2eaSArchie Cobbs 	}
3891c38f2eaSArchie Cobbs 	return (top);
3901c38f2eaSArchie Cobbs 
3911c38f2eaSArchie Cobbs nospace:
3921c38f2eaSArchie Cobbs 	m_freem(top);
39308442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
394122a814aSBosko Milekic 	return (NULL);
3951c38f2eaSArchie Cobbs }
3961c38f2eaSArchie Cobbs 
3971c38f2eaSArchie Cobbs /*
398df8bae1dSRodney W. Grimes  * Concatenate mbuf chain n to m.
399df8bae1dSRodney W. Grimes  * Both chains must be of the same type (e.g. MT_DATA).
400df8bae1dSRodney W. Grimes  * Any m_pkthdr is not updated.
401df8bae1dSRodney W. Grimes  */
40226f9a767SRodney W. Grimes void
403122a814aSBosko Milekic m_cat(struct mbuf *m, struct mbuf *n)
404df8bae1dSRodney W. Grimes {
405df8bae1dSRodney W. Grimes 	while (m->m_next)
406df8bae1dSRodney W. Grimes 		m = m->m_next;
407df8bae1dSRodney W. Grimes 	while (n) {
408df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT ||
409df8bae1dSRodney W. Grimes 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
410df8bae1dSRodney W. Grimes 			/* just join the two chains */
411df8bae1dSRodney W. Grimes 			m->m_next = n;
412df8bae1dSRodney W. Grimes 			return;
413df8bae1dSRodney W. Grimes 		}
414df8bae1dSRodney W. Grimes 		/* splat the data from one into the other */
415df8bae1dSRodney W. Grimes 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
416df8bae1dSRodney W. Grimes 		    (u_int)n->m_len);
417df8bae1dSRodney W. Grimes 		m->m_len += n->m_len;
418df8bae1dSRodney W. Grimes 		n = m_free(n);
419df8bae1dSRodney W. Grimes 	}
420df8bae1dSRodney W. Grimes }
421df8bae1dSRodney W. Grimes 
42226f9a767SRodney W. Grimes void
423122a814aSBosko Milekic m_adj(struct mbuf *mp, int req_len)
424df8bae1dSRodney W. Grimes {
425122a814aSBosko Milekic 	int len = req_len;
426122a814aSBosko Milekic 	struct mbuf *m;
427122a814aSBosko Milekic 	int count;
428df8bae1dSRodney W. Grimes 
429df8bae1dSRodney W. Grimes 	if ((m = mp) == NULL)
430df8bae1dSRodney W. Grimes 		return;
431df8bae1dSRodney W. Grimes 	if (len >= 0) {
432df8bae1dSRodney W. Grimes 		/*
433df8bae1dSRodney W. Grimes 		 * Trim from head.
434df8bae1dSRodney W. Grimes 		 */
435df8bae1dSRodney W. Grimes 		while (m != NULL && len > 0) {
436df8bae1dSRodney W. Grimes 			if (m->m_len <= len) {
437df8bae1dSRodney W. Grimes 				len -= m->m_len;
438df8bae1dSRodney W. Grimes 				m->m_len = 0;
439df8bae1dSRodney W. Grimes 				m = m->m_next;
440df8bae1dSRodney W. Grimes 			} else {
441df8bae1dSRodney W. Grimes 				m->m_len -= len;
442df8bae1dSRodney W. Grimes 				m->m_data += len;
443df8bae1dSRodney W. Grimes 				len = 0;
444df8bae1dSRodney W. Grimes 			}
445df8bae1dSRodney W. Grimes 		}
446df8bae1dSRodney W. Grimes 		m = mp;
447df8bae1dSRodney W. Grimes 		if (mp->m_flags & M_PKTHDR)
448df8bae1dSRodney W. Grimes 			m->m_pkthdr.len -= (req_len - len);
449df8bae1dSRodney W. Grimes 	} else {
450df8bae1dSRodney W. Grimes 		/*
451df8bae1dSRodney W. Grimes 		 * Trim from tail.  Scan the mbuf chain,
452df8bae1dSRodney W. Grimes 		 * calculating its length and finding the last mbuf.
453df8bae1dSRodney W. Grimes 		 * If the adjustment only affects this mbuf, then just
454df8bae1dSRodney W. Grimes 		 * adjust and return.  Otherwise, rescan and truncate
455df8bae1dSRodney W. Grimes 		 * after the remaining size.
456df8bae1dSRodney W. Grimes 		 */
457df8bae1dSRodney W. Grimes 		len = -len;
458df8bae1dSRodney W. Grimes 		count = 0;
459df8bae1dSRodney W. Grimes 		for (;;) {
460df8bae1dSRodney W. Grimes 			count += m->m_len;
461df8bae1dSRodney W. Grimes 			if (m->m_next == (struct mbuf *)0)
462df8bae1dSRodney W. Grimes 				break;
463df8bae1dSRodney W. Grimes 			m = m->m_next;
464df8bae1dSRodney W. Grimes 		}
465df8bae1dSRodney W. Grimes 		if (m->m_len >= len) {
466df8bae1dSRodney W. Grimes 			m->m_len -= len;
467df8bae1dSRodney W. Grimes 			if (mp->m_flags & M_PKTHDR)
468df8bae1dSRodney W. Grimes 				mp->m_pkthdr.len -= len;
469df8bae1dSRodney W. Grimes 			return;
470df8bae1dSRodney W. Grimes 		}
471df8bae1dSRodney W. Grimes 		count -= len;
472df8bae1dSRodney W. Grimes 		if (count < 0)
473df8bae1dSRodney W. Grimes 			count = 0;
474df8bae1dSRodney W. Grimes 		/*
475df8bae1dSRodney W. Grimes 		 * Correct length for chain is "count".
476df8bae1dSRodney W. Grimes 		 * Find the mbuf with last data, adjust its length,
477df8bae1dSRodney W. Grimes 		 * and toss data from remaining mbufs on chain.
478df8bae1dSRodney W. Grimes 		 */
479df8bae1dSRodney W. Grimes 		m = mp;
480df8bae1dSRodney W. Grimes 		if (m->m_flags & M_PKTHDR)
481df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = count;
482df8bae1dSRodney W. Grimes 		for (; m; m = m->m_next) {
483df8bae1dSRodney W. Grimes 			if (m->m_len >= count) {
484df8bae1dSRodney W. Grimes 				m->m_len = count;
485df8bae1dSRodney W. Grimes 				break;
486df8bae1dSRodney W. Grimes 			}
487df8bae1dSRodney W. Grimes 			count -= m->m_len;
488df8bae1dSRodney W. Grimes 		}
489797f2d22SPoul-Henning Kamp 		while (m->m_next)
490797f2d22SPoul-Henning Kamp 			(m = m->m_next) ->m_len = 0;
491df8bae1dSRodney W. Grimes 	}
492df8bae1dSRodney W. Grimes }
493df8bae1dSRodney W. Grimes 
494df8bae1dSRodney W. Grimes /*
495df8bae1dSRodney W. Grimes  * Rearange an mbuf chain so that len bytes are contiguous
496df8bae1dSRodney W. Grimes  * and in the data area of an mbuf (so that mtod and dtom
497df8bae1dSRodney W. Grimes  * will work for a structure of size len).  Returns the resulting
498df8bae1dSRodney W. Grimes  * mbuf chain on success, frees it and returns null on failure.
499df8bae1dSRodney W. Grimes  * If there is room, it will add up to max_protohdr-len extra bytes to the
500df8bae1dSRodney W. Grimes  * contiguous region in an attempt to avoid being called next time.
501df8bae1dSRodney W. Grimes  */
502df8bae1dSRodney W. Grimes struct mbuf *
503122a814aSBosko Milekic m_pullup(struct mbuf *n, int len)
504df8bae1dSRodney W. Grimes {
505122a814aSBosko Milekic 	struct mbuf *m;
506122a814aSBosko Milekic 	int count;
507df8bae1dSRodney W. Grimes 	int space;
508df8bae1dSRodney W. Grimes 
509df8bae1dSRodney W. Grimes 	/*
510df8bae1dSRodney W. Grimes 	 * If first mbuf has no cluster, and has room for len bytes
511df8bae1dSRodney W. Grimes 	 * without shifting current data, pullup into it,
512df8bae1dSRodney W. Grimes 	 * otherwise allocate a new mbuf to prepend to the chain.
513df8bae1dSRodney W. Grimes 	 */
514df8bae1dSRodney W. Grimes 	if ((n->m_flags & M_EXT) == 0 &&
515df8bae1dSRodney W. Grimes 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
516df8bae1dSRodney W. Grimes 		if (n->m_len >= len)
517df8bae1dSRodney W. Grimes 			return (n);
518df8bae1dSRodney W. Grimes 		m = n;
519df8bae1dSRodney W. Grimes 		n = n->m_next;
520df8bae1dSRodney W. Grimes 		len -= m->m_len;
521df8bae1dSRodney W. Grimes 	} else {
522df8bae1dSRodney W. Grimes 		if (len > MHLEN)
523df8bae1dSRodney W. Grimes 			goto bad;
524df8bae1dSRodney W. Grimes 		MGET(m, M_DONTWAIT, n->m_type);
525122a814aSBosko Milekic 		if (m == NULL)
526df8bae1dSRodney W. Grimes 			goto bad;
527df8bae1dSRodney W. Grimes 		m->m_len = 0;
528df8bae1dSRodney W. Grimes 		if (n->m_flags & M_PKTHDR) {
529df8bae1dSRodney W. Grimes 			M_COPY_PKTHDR(m, n);
530df8bae1dSRodney W. Grimes 			n->m_flags &= ~M_PKTHDR;
531df8bae1dSRodney W. Grimes 		}
532df8bae1dSRodney W. Grimes 	}
533df8bae1dSRodney W. Grimes 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
534df8bae1dSRodney W. Grimes 	do {
535df8bae1dSRodney W. Grimes 		count = min(min(max(len, max_protohdr), space), n->m_len);
536df8bae1dSRodney W. Grimes 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
537df8bae1dSRodney W. Grimes 		  (unsigned)count);
538df8bae1dSRodney W. Grimes 		len -= count;
539df8bae1dSRodney W. Grimes 		m->m_len += count;
540df8bae1dSRodney W. Grimes 		n->m_len -= count;
541df8bae1dSRodney W. Grimes 		space -= count;
542df8bae1dSRodney W. Grimes 		if (n->m_len)
543df8bae1dSRodney W. Grimes 			n->m_data += count;
544df8bae1dSRodney W. Grimes 		else
545df8bae1dSRodney W. Grimes 			n = m_free(n);
546df8bae1dSRodney W. Grimes 	} while (len > 0 && n);
547df8bae1dSRodney W. Grimes 	if (len > 0) {
548df8bae1dSRodney W. Grimes 		(void) m_free(m);
549df8bae1dSRodney W. Grimes 		goto bad;
550df8bae1dSRodney W. Grimes 	}
551df8bae1dSRodney W. Grimes 	m->m_next = n;
552df8bae1dSRodney W. Grimes 	return (m);
553df8bae1dSRodney W. Grimes bad:
554df8bae1dSRodney W. Grimes 	m_freem(n);
55508442f8aSBosko Milekic 	mbstat.m_mpfail++;	/* XXX: No consistency. */
556122a814aSBosko Milekic 	return (NULL);
557df8bae1dSRodney W. Grimes }
558df8bae1dSRodney W. Grimes 
559df8bae1dSRodney W. Grimes /*
560df8bae1dSRodney W. Grimes  * Partition an mbuf chain in two pieces, returning the tail --
561df8bae1dSRodney W. Grimes  * all but the first len0 bytes.  In case of failure, it returns NULL and
562df8bae1dSRodney W. Grimes  * attempts to restore the chain to its original state.
56348d183faSArchie Cobbs  *
56448d183faSArchie Cobbs  * Note that the resulting mbufs might be read-only, because the new
56548d183faSArchie Cobbs  * mbuf can end up sharing an mbuf cluster with the original mbuf if
56648d183faSArchie Cobbs  * the "breaking point" happens to lie within a cluster mbuf. Use the
56748d183faSArchie Cobbs  * M_WRITABLE() macro to check for this case.
568df8bae1dSRodney W. Grimes  */
569df8bae1dSRodney W. Grimes struct mbuf *
570122a814aSBosko Milekic m_split(struct mbuf *m0, int len0, int wait)
571df8bae1dSRodney W. Grimes {
572122a814aSBosko Milekic 	struct mbuf *m, *n;
573df8bae1dSRodney W. Grimes 	unsigned len = len0, remain;
574df8bae1dSRodney W. Grimes 
575df8bae1dSRodney W. Grimes 	for (m = m0; m && len > m->m_len; m = m->m_next)
576df8bae1dSRodney W. Grimes 		len -= m->m_len;
577122a814aSBosko Milekic 	if (m == NULL)
578122a814aSBosko Milekic 		return (NULL);
579df8bae1dSRodney W. Grimes 	remain = m->m_len - len;
580df8bae1dSRodney W. Grimes 	if (m0->m_flags & M_PKTHDR) {
581df8bae1dSRodney W. Grimes 		MGETHDR(n, wait, m0->m_type);
582122a814aSBosko Milekic 		if (n == NULL)
583122a814aSBosko Milekic 			return (NULL);
584df8bae1dSRodney W. Grimes 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
585df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
586df8bae1dSRodney W. Grimes 		m0->m_pkthdr.len = len0;
587df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT)
588df8bae1dSRodney W. Grimes 			goto extpacket;
589df8bae1dSRodney W. Grimes 		if (remain > MHLEN) {
590df8bae1dSRodney W. Grimes 			/* m can't be the lead packet */
591df8bae1dSRodney W. Grimes 			MH_ALIGN(n, 0);
592df8bae1dSRodney W. Grimes 			n->m_next = m_split(m, len, wait);
593122a814aSBosko Milekic 			if (n->m_next == NULL) {
594df8bae1dSRodney W. Grimes 				(void) m_free(n);
595122a814aSBosko Milekic 				return (NULL);
59640376987SJeffrey Hsu 			} else {
59740376987SJeffrey Hsu 				n->m_len = 0;
598df8bae1dSRodney W. Grimes 				return (n);
59940376987SJeffrey Hsu 			}
600df8bae1dSRodney W. Grimes 		} else
601df8bae1dSRodney W. Grimes 			MH_ALIGN(n, remain);
602df8bae1dSRodney W. Grimes 	} else if (remain == 0) {
603df8bae1dSRodney W. Grimes 		n = m->m_next;
604122a814aSBosko Milekic 		m->m_next = NULL;
605df8bae1dSRodney W. Grimes 		return (n);
606df8bae1dSRodney W. Grimes 	} else {
607df8bae1dSRodney W. Grimes 		MGET(n, wait, m->m_type);
608122a814aSBosko Milekic 		if (n == NULL)
609122a814aSBosko Milekic 			return (NULL);
610df8bae1dSRodney W. Grimes 		M_ALIGN(n, remain);
611df8bae1dSRodney W. Grimes 	}
612df8bae1dSRodney W. Grimes extpacket:
613df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT) {
614df8bae1dSRodney W. Grimes 		n->m_flags |= M_EXT;
615df8bae1dSRodney W. Grimes 		n->m_ext = m->m_ext;
616a5c4836dSDavid Malone 		MEXT_ADD_REF(m);
617df8bae1dSRodney W. Grimes 		n->m_data = m->m_data + len;
618df8bae1dSRodney W. Grimes 	} else {
619df8bae1dSRodney W. Grimes 		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
620df8bae1dSRodney W. Grimes 	}
621df8bae1dSRodney W. Grimes 	n->m_len = remain;
622df8bae1dSRodney W. Grimes 	m->m_len = len;
623df8bae1dSRodney W. Grimes 	n->m_next = m->m_next;
624122a814aSBosko Milekic 	m->m_next = NULL;
625df8bae1dSRodney W. Grimes 	return (n);
626df8bae1dSRodney W. Grimes }
627df8bae1dSRodney W. Grimes /*
628df8bae1dSRodney W. Grimes  * Routine to copy from device local memory into mbufs.
629f5eece3fSBosko Milekic  * Note that `off' argument is offset into first mbuf of target chain from
630f5eece3fSBosko Milekic  * which to begin copying the data to.
631df8bae1dSRodney W. Grimes  */
632df8bae1dSRodney W. Grimes struct mbuf *
633f5eece3fSBosko Milekic m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
634122a814aSBosko Milekic 	 void (*copy)(char *from, caddr_t to, u_int len))
635df8bae1dSRodney W. Grimes {
636122a814aSBosko Milekic 	struct mbuf *m;
637df8bae1dSRodney W. Grimes 	struct mbuf *top = 0, **mp = &top;
638f5eece3fSBosko Milekic 	int len;
639df8bae1dSRodney W. Grimes 
640f5eece3fSBosko Milekic 	if (off < 0 || off > MHLEN)
641f5eece3fSBosko Milekic 		return (NULL);
642f5eece3fSBosko Milekic 
643df8bae1dSRodney W. Grimes 	MGETHDR(m, M_DONTWAIT, MT_DATA);
644122a814aSBosko Milekic 	if (m == NULL)
645122a814aSBosko Milekic 		return (NULL);
646df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = ifp;
647df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = totlen;
648f5eece3fSBosko Milekic 	len = MHLEN;
649df8bae1dSRodney W. Grimes 
650df8bae1dSRodney W. Grimes 	while (totlen > 0) {
651df8bae1dSRodney W. Grimes 		if (top) {
652df8bae1dSRodney W. Grimes 			MGET(m, M_DONTWAIT, MT_DATA);
653122a814aSBosko Milekic 			if (m == NULL) {
654df8bae1dSRodney W. Grimes 				m_freem(top);
655122a814aSBosko Milekic 				return (NULL);
656df8bae1dSRodney W. Grimes 			}
657f5eece3fSBosko Milekic 			len = MLEN;
658df8bae1dSRodney W. Grimes 		}
659f5eece3fSBosko Milekic 		if (totlen + off >= MINCLSIZE) {
660df8bae1dSRodney W. Grimes 			MCLGET(m, M_DONTWAIT);
661df8bae1dSRodney W. Grimes 			if (m->m_flags & M_EXT)
662f5eece3fSBosko Milekic 				len = MCLBYTES;
663df8bae1dSRodney W. Grimes 		} else {
664df8bae1dSRodney W. Grimes 			/*
665df8bae1dSRodney W. Grimes 			 * Place initial small packet/header at end of mbuf.
666df8bae1dSRodney W. Grimes 			 */
667f5eece3fSBosko Milekic 			if (top == NULL && totlen + off + max_linkhdr <= len) {
668df8bae1dSRodney W. Grimes 				m->m_data += max_linkhdr;
669f5eece3fSBosko Milekic 				len -= max_linkhdr;
670df8bae1dSRodney W. Grimes 			}
671f5eece3fSBosko Milekic 		}
672f5eece3fSBosko Milekic 		if (off) {
673f5eece3fSBosko Milekic 			m->m_data += off;
674f5eece3fSBosko Milekic 			len -= off;
675f5eece3fSBosko Milekic 			off = 0;
676f5eece3fSBosko Milekic 		}
677f5eece3fSBosko Milekic 		m->m_len = len = min(totlen, len);
678df8bae1dSRodney W. Grimes 		if (copy)
679f5eece3fSBosko Milekic 			copy(buf, mtod(m, caddr_t), (unsigned)len);
680df8bae1dSRodney W. Grimes 		else
681f5eece3fSBosko Milekic 			bcopy(buf, mtod(m, caddr_t), (unsigned)len);
682f5eece3fSBosko Milekic 		buf += len;
683df8bae1dSRodney W. Grimes 		*mp = m;
684df8bae1dSRodney W. Grimes 		mp = &m->m_next;
685df8bae1dSRodney W. Grimes 		totlen -= len;
686df8bae1dSRodney W. Grimes 	}
687df8bae1dSRodney W. Grimes 	return (top);
688df8bae1dSRodney W. Grimes }
689c5789ba3SPoul-Henning Kamp 
690c5789ba3SPoul-Henning Kamp /*
691c5789ba3SPoul-Henning Kamp  * Copy data from a buffer back into the indicated mbuf chain,
692c5789ba3SPoul-Henning Kamp  * starting "off" bytes from the beginning, extending the mbuf
693c5789ba3SPoul-Henning Kamp  * chain if necessary.
694c5789ba3SPoul-Henning Kamp  */
695c5789ba3SPoul-Henning Kamp void
696122a814aSBosko Milekic m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
697c5789ba3SPoul-Henning Kamp {
698122a814aSBosko Milekic 	int mlen;
699122a814aSBosko Milekic 	struct mbuf *m = m0, *n;
700c5789ba3SPoul-Henning Kamp 	int totlen = 0;
701c5789ba3SPoul-Henning Kamp 
702122a814aSBosko Milekic 	if (m0 == NULL)
703c5789ba3SPoul-Henning Kamp 		return;
704c5789ba3SPoul-Henning Kamp 	while (off > (mlen = m->m_len)) {
705c5789ba3SPoul-Henning Kamp 		off -= mlen;
706c5789ba3SPoul-Henning Kamp 		totlen += mlen;
707122a814aSBosko Milekic 		if (m->m_next == NULL) {
70808442f8aSBosko Milekic 			n = m_get_clrd(M_DONTWAIT, m->m_type);
709122a814aSBosko Milekic 			if (n == NULL)
710c5789ba3SPoul-Henning Kamp 				goto out;
711c5789ba3SPoul-Henning Kamp 			n->m_len = min(MLEN, len + off);
712c5789ba3SPoul-Henning Kamp 			m->m_next = n;
713c5789ba3SPoul-Henning Kamp 		}
714c5789ba3SPoul-Henning Kamp 		m = m->m_next;
715c5789ba3SPoul-Henning Kamp 	}
716c5789ba3SPoul-Henning Kamp 	while (len > 0) {
717c5789ba3SPoul-Henning Kamp 		mlen = min (m->m_len - off, len);
718c5789ba3SPoul-Henning Kamp 		bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
719c5789ba3SPoul-Henning Kamp 		cp += mlen;
720c5789ba3SPoul-Henning Kamp 		len -= mlen;
721c5789ba3SPoul-Henning Kamp 		mlen += off;
722c5789ba3SPoul-Henning Kamp 		off = 0;
723c5789ba3SPoul-Henning Kamp 		totlen += mlen;
724c5789ba3SPoul-Henning Kamp 		if (len == 0)
725c5789ba3SPoul-Henning Kamp 			break;
726122a814aSBosko Milekic 		if (m->m_next == NULL) {
727c5789ba3SPoul-Henning Kamp 			n = m_get(M_DONTWAIT, m->m_type);
728122a814aSBosko Milekic 			if (n == NULL)
729c5789ba3SPoul-Henning Kamp 				break;
730c5789ba3SPoul-Henning Kamp 			n->m_len = min(MLEN, len);
731c5789ba3SPoul-Henning Kamp 			m->m_next = n;
732c5789ba3SPoul-Henning Kamp 		}
733c5789ba3SPoul-Henning Kamp 		m = m->m_next;
734c5789ba3SPoul-Henning Kamp 	}
735c5789ba3SPoul-Henning Kamp out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
736c5789ba3SPoul-Henning Kamp 		m->m_pkthdr.len = totlen;
737c5789ba3SPoul-Henning Kamp }
738ce4a64f7SPoul-Henning Kamp 
739ce4a64f7SPoul-Henning Kamp void
740ce4a64f7SPoul-Henning Kamp m_print(const struct mbuf *m)
741ce4a64f7SPoul-Henning Kamp {
742ce4a64f7SPoul-Henning Kamp 	int len;
7436357e7b5SEivind Eklund 	const struct mbuf *m2;
744ce4a64f7SPoul-Henning Kamp 
745ce4a64f7SPoul-Henning Kamp 	len = m->m_pkthdr.len;
746ce4a64f7SPoul-Henning Kamp 	m2 = m;
747ce4a64f7SPoul-Henning Kamp 	while (len) {
748ce4a64f7SPoul-Henning Kamp 		printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");
749ce4a64f7SPoul-Henning Kamp 		len -= m2->m_len;
750ce4a64f7SPoul-Henning Kamp 		m2 = m2->m_next;
751ce4a64f7SPoul-Henning Kamp 	}
752ce4a64f7SPoul-Henning Kamp 	return;
753ce4a64f7SPoul-Henning Kamp }
754