xref: /freebsd/sys/kern/uipc_mbuf.c (revision a8cfc0ee40bf8c504a25b64bc7ddbc7a726a56ec)
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 {
134122a814aSBosko Milekic 	struct mbuf *n;
135df8bae1dSRodney W. Grimes 
136df8bae1dSRodney W. Grimes 	if (m == NULL)
137df8bae1dSRodney W. Grimes 		return;
138df8bae1dSRodney W. Grimes 	do {
139df8bae1dSRodney W. Grimes 		MFREE(m, n);
140797f2d22SPoul-Henning Kamp 		m = n;
141797f2d22SPoul-Henning Kamp 	} while (m);
142df8bae1dSRodney W. Grimes }
143df8bae1dSRodney W. Grimes 
144df8bae1dSRodney W. Grimes /*
145df8bae1dSRodney W. Grimes  * Lesser-used path for M_PREPEND:
146df8bae1dSRodney W. Grimes  * allocate new mbuf to prepend to chain,
147df8bae1dSRodney W. Grimes  * copy junk along.
148df8bae1dSRodney W. Grimes  */
149df8bae1dSRodney W. Grimes struct mbuf *
150122a814aSBosko Milekic m_prepend(struct mbuf *m, int len, int how)
151df8bae1dSRodney W. Grimes {
152df8bae1dSRodney W. Grimes 	struct mbuf *mn;
153df8bae1dSRodney W. Grimes 
154df8bae1dSRodney W. Grimes 	MGET(mn, how, m->m_type);
155122a814aSBosko Milekic 	if (mn == NULL) {
156df8bae1dSRodney W. Grimes 		m_freem(m);
157122a814aSBosko Milekic 		return (NULL);
158df8bae1dSRodney W. Grimes 	}
159df8bae1dSRodney W. Grimes 	if (m->m_flags & M_PKTHDR) {
160df8bae1dSRodney W. Grimes 		M_COPY_PKTHDR(mn, m);
161df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_PKTHDR;
162df8bae1dSRodney W. Grimes 	}
163df8bae1dSRodney W. Grimes 	mn->m_next = m;
164df8bae1dSRodney W. Grimes 	m = mn;
165df8bae1dSRodney W. Grimes 	if (len < MHLEN)
166df8bae1dSRodney W. Grimes 		MH_ALIGN(m, len);
167df8bae1dSRodney W. Grimes 	m->m_len = len;
168df8bae1dSRodney W. Grimes 	return (m);
169df8bae1dSRodney W. Grimes }
170df8bae1dSRodney W. Grimes 
171df8bae1dSRodney W. Grimes /*
172df8bae1dSRodney W. Grimes  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
173df8bae1dSRodney W. Grimes  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
1742a0c503eSBosko Milekic  * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
1751c38f2eaSArchie Cobbs  * Note that the copy is read-only, because clusters are not copied,
1761c38f2eaSArchie Cobbs  * only their reference counts are incremented.
177df8bae1dSRodney W. Grimes  */
178df8bae1dSRodney W. Grimes struct mbuf *
179122a814aSBosko Milekic m_copym(struct mbuf *m, int off0, int len, int wait)
180df8bae1dSRodney W. Grimes {
181122a814aSBosko Milekic 	struct mbuf *n, **np;
182122a814aSBosko Milekic 	int off = off0;
183df8bae1dSRodney W. Grimes 	struct mbuf *top;
184df8bae1dSRodney W. Grimes 	int copyhdr = 0;
185df8bae1dSRodney W. Grimes 
186e0a653ddSAlfred Perlstein 	KASSERT(off >= 0, ("m_copym, negative off %d", off));
187e0a653ddSAlfred Perlstein 	KASSERT(len >= 0, ("m_copym, negative len %d", len));
188df8bae1dSRodney W. Grimes 	if (off == 0 && m->m_flags & M_PKTHDR)
189df8bae1dSRodney W. Grimes 		copyhdr = 1;
190df8bae1dSRodney W. Grimes 	while (off > 0) {
191e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
192df8bae1dSRodney W. Grimes 		if (off < m->m_len)
193df8bae1dSRodney W. Grimes 			break;
194df8bae1dSRodney W. Grimes 		off -= m->m_len;
195df8bae1dSRodney W. Grimes 		m = m->m_next;
196df8bae1dSRodney W. Grimes 	}
197df8bae1dSRodney W. Grimes 	np = &top;
198df8bae1dSRodney W. Grimes 	top = 0;
199df8bae1dSRodney W. Grimes 	while (len > 0) {
200122a814aSBosko Milekic 		if (m == NULL) {
201e0a653ddSAlfred Perlstein 			KASSERT(len == M_COPYALL,
202e0a653ddSAlfred Perlstein 			    ("m_copym, length > size of mbuf chain"));
203df8bae1dSRodney W. Grimes 			break;
204df8bae1dSRodney W. Grimes 		}
205df8bae1dSRodney W. Grimes 		MGET(n, wait, m->m_type);
206df8bae1dSRodney W. Grimes 		*np = n;
207122a814aSBosko Milekic 		if (n == NULL)
208df8bae1dSRodney W. Grimes 			goto nospace;
209df8bae1dSRodney W. Grimes 		if (copyhdr) {
210df8bae1dSRodney W. Grimes 			M_COPY_PKTHDR(n, m);
211df8bae1dSRodney W. Grimes 			if (len == M_COPYALL)
212df8bae1dSRodney W. Grimes 				n->m_pkthdr.len -= off0;
213df8bae1dSRodney W. Grimes 			else
214df8bae1dSRodney W. Grimes 				n->m_pkthdr.len = len;
215df8bae1dSRodney W. Grimes 			copyhdr = 0;
216df8bae1dSRodney W. Grimes 		}
217df8bae1dSRodney W. Grimes 		n->m_len = min(len, m->m_len - off);
218df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT) {
219df8bae1dSRodney W. Grimes 			n->m_data = m->m_data + off;
220df8bae1dSRodney W. Grimes 			n->m_ext = m->m_ext;
221df8bae1dSRodney W. Grimes 			n->m_flags |= M_EXT;
222a5c4836dSDavid Malone 			MEXT_ADD_REF(m);
223df8bae1dSRodney W. Grimes 		} else
224df8bae1dSRodney W. Grimes 			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
225df8bae1dSRodney W. Grimes 			    (unsigned)n->m_len);
226df8bae1dSRodney W. Grimes 		if (len != M_COPYALL)
227df8bae1dSRodney W. Grimes 			len -= n->m_len;
228df8bae1dSRodney W. Grimes 		off = 0;
229df8bae1dSRodney W. Grimes 		m = m->m_next;
230df8bae1dSRodney W. Grimes 		np = &n->m_next;
231df8bae1dSRodney W. Grimes 	}
23208442f8aSBosko Milekic 	if (top == NULL)
23308442f8aSBosko Milekic 		mbstat.m_mcfail++;	/* XXX: No consistency. */
23408442f8aSBosko Milekic 
235df8bae1dSRodney W. Grimes 	return (top);
236df8bae1dSRodney W. Grimes nospace:
237df8bae1dSRodney W. Grimes 	m_freem(top);
23808442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
239122a814aSBosko Milekic 	return (NULL);
240df8bae1dSRodney W. Grimes }
241df8bae1dSRodney W. Grimes 
242df8bae1dSRodney W. Grimes /*
2436a06dea0SGarrett Wollman  * Copy an entire packet, including header (which must be present).
2446a06dea0SGarrett Wollman  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
2451c38f2eaSArchie Cobbs  * Note that the copy is read-only, because clusters are not copied,
2461c38f2eaSArchie Cobbs  * only their reference counts are incremented.
2475fe86675SLuigi Rizzo  * Preserve alignment of the first mbuf so if the creator has left
2485fe86675SLuigi Rizzo  * some room at the beginning (e.g. for inserting protocol headers)
2495fe86675SLuigi Rizzo  * the copies still have the room available.
2506a06dea0SGarrett Wollman  */
2516a06dea0SGarrett Wollman struct mbuf *
252122a814aSBosko Milekic m_copypacket(struct mbuf *m, int how)
2536a06dea0SGarrett Wollman {
2546a06dea0SGarrett Wollman 	struct mbuf *top, *n, *o;
2556a06dea0SGarrett Wollman 
2566a06dea0SGarrett Wollman 	MGET(n, how, m->m_type);
2576a06dea0SGarrett Wollman 	top = n;
258122a814aSBosko Milekic 	if (n == NULL)
2596a06dea0SGarrett Wollman 		goto nospace;
2606a06dea0SGarrett Wollman 
2616a06dea0SGarrett Wollman 	M_COPY_PKTHDR(n, m);
2626a06dea0SGarrett Wollman 	n->m_len = m->m_len;
2636a06dea0SGarrett Wollman 	if (m->m_flags & M_EXT) {
2646a06dea0SGarrett Wollman 		n->m_data = m->m_data;
2656a06dea0SGarrett Wollman 		n->m_ext = m->m_ext;
2666a06dea0SGarrett Wollman 		n->m_flags |= M_EXT;
267a5c4836dSDavid Malone 		MEXT_ADD_REF(m);
2686a06dea0SGarrett Wollman 	} else {
2695fe86675SLuigi Rizzo 		n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
2706a06dea0SGarrett Wollman 		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
2716a06dea0SGarrett Wollman 	}
2726a06dea0SGarrett Wollman 
2736a06dea0SGarrett Wollman 	m = m->m_next;
2746a06dea0SGarrett Wollman 	while (m) {
2756a06dea0SGarrett Wollman 		MGET(o, how, m->m_type);
276122a814aSBosko Milekic 		if (o == NULL)
2776a06dea0SGarrett Wollman 			goto nospace;
2786a06dea0SGarrett Wollman 
2796a06dea0SGarrett Wollman 		n->m_next = o;
2806a06dea0SGarrett Wollman 		n = n->m_next;
2816a06dea0SGarrett Wollman 
2826a06dea0SGarrett Wollman 		n->m_len = m->m_len;
2836a06dea0SGarrett Wollman 		if (m->m_flags & M_EXT) {
2846a06dea0SGarrett Wollman 			n->m_data = m->m_data;
2856a06dea0SGarrett Wollman 			n->m_ext = m->m_ext;
2866a06dea0SGarrett Wollman 			n->m_flags |= M_EXT;
287a5c4836dSDavid Malone 			MEXT_ADD_REF(m);
2886a06dea0SGarrett Wollman 		} else {
2896a06dea0SGarrett Wollman 			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
2906a06dea0SGarrett Wollman 		}
2916a06dea0SGarrett Wollman 
2926a06dea0SGarrett Wollman 		m = m->m_next;
2936a06dea0SGarrett Wollman 	}
2946a06dea0SGarrett Wollman 	return top;
2956a06dea0SGarrett Wollman nospace:
2966a06dea0SGarrett Wollman 	m_freem(top);
29708442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
298122a814aSBosko Milekic 	return (NULL);
2996a06dea0SGarrett Wollman }
3006a06dea0SGarrett Wollman 
3016a06dea0SGarrett Wollman /*
302df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain starting "off" bytes from the beginning,
303df8bae1dSRodney W. Grimes  * continuing for "len" bytes, into the indicated buffer.
304df8bae1dSRodney W. Grimes  */
30526f9a767SRodney W. Grimes void
306a8cfc0eeSJulian Elischer m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
307df8bae1dSRodney W. Grimes {
308122a814aSBosko Milekic 	unsigned count;
309df8bae1dSRodney W. Grimes 
310e0a653ddSAlfred Perlstein 	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
311e0a653ddSAlfred Perlstein 	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
312df8bae1dSRodney W. Grimes 	while (off > 0) {
313e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
314df8bae1dSRodney W. Grimes 		if (off < m->m_len)
315df8bae1dSRodney W. Grimes 			break;
316df8bae1dSRodney W. Grimes 		off -= m->m_len;
317df8bae1dSRodney W. Grimes 		m = m->m_next;
318df8bae1dSRodney W. Grimes 	}
319df8bae1dSRodney W. Grimes 	while (len > 0) {
320e0a653ddSAlfred Perlstein 		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
321df8bae1dSRodney W. Grimes 		count = min(m->m_len - off, len);
322df8bae1dSRodney W. Grimes 		bcopy(mtod(m, caddr_t) + off, cp, count);
323df8bae1dSRodney W. Grimes 		len -= count;
324df8bae1dSRodney W. Grimes 		cp += count;
325df8bae1dSRodney W. Grimes 		off = 0;
326df8bae1dSRodney W. Grimes 		m = m->m_next;
327df8bae1dSRodney W. Grimes 	}
328df8bae1dSRodney W. Grimes }
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes /*
3311c38f2eaSArchie Cobbs  * Copy a packet header mbuf chain into a completely new chain, including
3321c38f2eaSArchie Cobbs  * copying any mbuf clusters.  Use this instead of m_copypacket() when
3331c38f2eaSArchie Cobbs  * you need a writable copy of an mbuf chain.
3341c38f2eaSArchie Cobbs  */
3351c38f2eaSArchie Cobbs struct mbuf *
336122a814aSBosko Milekic m_dup(struct mbuf *m, int how)
3371c38f2eaSArchie Cobbs {
3381c38f2eaSArchie Cobbs 	struct mbuf **p, *top = NULL;
3391c38f2eaSArchie Cobbs 	int remain, moff, nsize;
3401c38f2eaSArchie Cobbs 
3411c38f2eaSArchie Cobbs 	/* Sanity check */
3421c38f2eaSArchie Cobbs 	if (m == NULL)
343122a814aSBosko Milekic 		return (NULL);
3441c38f2eaSArchie Cobbs 	KASSERT((m->m_flags & M_PKTHDR) != 0, ("%s: !PKTHDR", __FUNCTION__));
3451c38f2eaSArchie Cobbs 
3461c38f2eaSArchie Cobbs 	/* While there's more data, get a new mbuf, tack it on, and fill it */
3471c38f2eaSArchie Cobbs 	remain = m->m_pkthdr.len;
3481c38f2eaSArchie Cobbs 	moff = 0;
3491c38f2eaSArchie Cobbs 	p = &top;
3501c38f2eaSArchie Cobbs 	while (remain > 0 || top == NULL) {	/* allow m->m_pkthdr.len == 0 */
3511c38f2eaSArchie Cobbs 		struct mbuf *n;
3521c38f2eaSArchie Cobbs 
3531c38f2eaSArchie Cobbs 		/* Get the next new mbuf */
3541c38f2eaSArchie Cobbs 		MGET(n, how, m->m_type);
3551c38f2eaSArchie Cobbs 		if (n == NULL)
3561c38f2eaSArchie Cobbs 			goto nospace;
3571c38f2eaSArchie Cobbs 		if (top == NULL) {		/* first one, must be PKTHDR */
3581c38f2eaSArchie Cobbs 			M_COPY_PKTHDR(n, m);
3591c38f2eaSArchie Cobbs 			nsize = MHLEN;
3601c38f2eaSArchie Cobbs 		} else				/* not the first one */
3611c38f2eaSArchie Cobbs 			nsize = MLEN;
3621c38f2eaSArchie Cobbs 		if (remain >= MINCLSIZE) {
3631c38f2eaSArchie Cobbs 			MCLGET(n, how);
3641c38f2eaSArchie Cobbs 			if ((n->m_flags & M_EXT) == 0) {
3651c38f2eaSArchie Cobbs 				(void)m_free(n);
3661c38f2eaSArchie Cobbs 				goto nospace;
3671c38f2eaSArchie Cobbs 			}
3681c38f2eaSArchie Cobbs 			nsize = MCLBYTES;
3691c38f2eaSArchie Cobbs 		}
3701c38f2eaSArchie Cobbs 		n->m_len = 0;
3711c38f2eaSArchie Cobbs 
3721c38f2eaSArchie Cobbs 		/* Link it into the new chain */
3731c38f2eaSArchie Cobbs 		*p = n;
3741c38f2eaSArchie Cobbs 		p = &n->m_next;
3751c38f2eaSArchie Cobbs 
3761c38f2eaSArchie Cobbs 		/* Copy data from original mbuf(s) into new mbuf */
3771c38f2eaSArchie Cobbs 		while (n->m_len < nsize && m != NULL) {
3781c38f2eaSArchie Cobbs 			int chunk = min(nsize - n->m_len, m->m_len - moff);
3791c38f2eaSArchie Cobbs 
3801c38f2eaSArchie Cobbs 			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
3811c38f2eaSArchie Cobbs 			moff += chunk;
3821c38f2eaSArchie Cobbs 			n->m_len += chunk;
3831c38f2eaSArchie Cobbs 			remain -= chunk;
3841c38f2eaSArchie Cobbs 			if (moff == m->m_len) {
3851c38f2eaSArchie Cobbs 				m = m->m_next;
3861c38f2eaSArchie Cobbs 				moff = 0;
3871c38f2eaSArchie Cobbs 			}
3881c38f2eaSArchie Cobbs 		}
3891c38f2eaSArchie Cobbs 
3901c38f2eaSArchie Cobbs 		/* Check correct total mbuf length */
3911c38f2eaSArchie Cobbs 		KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
3921c38f2eaSArchie Cobbs 		    	("%s: bogus m_pkthdr.len", __FUNCTION__));
3931c38f2eaSArchie Cobbs 	}
3941c38f2eaSArchie Cobbs 	return (top);
3951c38f2eaSArchie Cobbs 
3961c38f2eaSArchie Cobbs nospace:
3971c38f2eaSArchie Cobbs 	m_freem(top);
39808442f8aSBosko Milekic 	mbstat.m_mcfail++;	/* XXX: No consistency. */
399122a814aSBosko Milekic 	return (NULL);
4001c38f2eaSArchie Cobbs }
4011c38f2eaSArchie Cobbs 
4021c38f2eaSArchie Cobbs /*
403df8bae1dSRodney W. Grimes  * Concatenate mbuf chain n to m.
404df8bae1dSRodney W. Grimes  * Both chains must be of the same type (e.g. MT_DATA).
405df8bae1dSRodney W. Grimes  * Any m_pkthdr is not updated.
406df8bae1dSRodney W. Grimes  */
40726f9a767SRodney W. Grimes void
408122a814aSBosko Milekic m_cat(struct mbuf *m, struct mbuf *n)
409df8bae1dSRodney W. Grimes {
410df8bae1dSRodney W. Grimes 	while (m->m_next)
411df8bae1dSRodney W. Grimes 		m = m->m_next;
412df8bae1dSRodney W. Grimes 	while (n) {
413df8bae1dSRodney W. Grimes 		if (m->m_flags & M_EXT ||
414df8bae1dSRodney W. Grimes 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
415df8bae1dSRodney W. Grimes 			/* just join the two chains */
416df8bae1dSRodney W. Grimes 			m->m_next = n;
417df8bae1dSRodney W. Grimes 			return;
418df8bae1dSRodney W. Grimes 		}
419df8bae1dSRodney W. Grimes 		/* splat the data from one into the other */
420df8bae1dSRodney W. Grimes 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
421df8bae1dSRodney W. Grimes 		    (u_int)n->m_len);
422df8bae1dSRodney W. Grimes 		m->m_len += n->m_len;
423df8bae1dSRodney W. Grimes 		n = m_free(n);
424df8bae1dSRodney W. Grimes 	}
425df8bae1dSRodney W. Grimes }
426df8bae1dSRodney W. Grimes 
42726f9a767SRodney W. Grimes void
428122a814aSBosko Milekic m_adj(struct mbuf *mp, int req_len)
429df8bae1dSRodney W. Grimes {
430122a814aSBosko Milekic 	int len = req_len;
431122a814aSBosko Milekic 	struct mbuf *m;
432122a814aSBosko Milekic 	int count;
433df8bae1dSRodney W. Grimes 
434df8bae1dSRodney W. Grimes 	if ((m = mp) == NULL)
435df8bae1dSRodney W. Grimes 		return;
436df8bae1dSRodney W. Grimes 	if (len >= 0) {
437df8bae1dSRodney W. Grimes 		/*
438df8bae1dSRodney W. Grimes 		 * Trim from head.
439df8bae1dSRodney W. Grimes 		 */
440df8bae1dSRodney W. Grimes 		while (m != NULL && len > 0) {
441df8bae1dSRodney W. Grimes 			if (m->m_len <= len) {
442df8bae1dSRodney W. Grimes 				len -= m->m_len;
443df8bae1dSRodney W. Grimes 				m->m_len = 0;
444df8bae1dSRodney W. Grimes 				m = m->m_next;
445df8bae1dSRodney W. Grimes 			} else {
446df8bae1dSRodney W. Grimes 				m->m_len -= len;
447df8bae1dSRodney W. Grimes 				m->m_data += len;
448df8bae1dSRodney W. Grimes 				len = 0;
449df8bae1dSRodney W. Grimes 			}
450df8bae1dSRodney W. Grimes 		}
451df8bae1dSRodney W. Grimes 		m = mp;
452df8bae1dSRodney W. Grimes 		if (mp->m_flags & M_PKTHDR)
453df8bae1dSRodney W. Grimes 			m->m_pkthdr.len -= (req_len - len);
454df8bae1dSRodney W. Grimes 	} else {
455df8bae1dSRodney W. Grimes 		/*
456df8bae1dSRodney W. Grimes 		 * Trim from tail.  Scan the mbuf chain,
457df8bae1dSRodney W. Grimes 		 * calculating its length and finding the last mbuf.
458df8bae1dSRodney W. Grimes 		 * If the adjustment only affects this mbuf, then just
459df8bae1dSRodney W. Grimes 		 * adjust and return.  Otherwise, rescan and truncate
460df8bae1dSRodney W. Grimes 		 * after the remaining size.
461df8bae1dSRodney W. Grimes 		 */
462df8bae1dSRodney W. Grimes 		len = -len;
463df8bae1dSRodney W. Grimes 		count = 0;
464df8bae1dSRodney W. Grimes 		for (;;) {
465df8bae1dSRodney W. Grimes 			count += m->m_len;
466df8bae1dSRodney W. Grimes 			if (m->m_next == (struct mbuf *)0)
467df8bae1dSRodney W. Grimes 				break;
468df8bae1dSRodney W. Grimes 			m = m->m_next;
469df8bae1dSRodney W. Grimes 		}
470df8bae1dSRodney W. Grimes 		if (m->m_len >= len) {
471df8bae1dSRodney W. Grimes 			m->m_len -= len;
472df8bae1dSRodney W. Grimes 			if (mp->m_flags & M_PKTHDR)
473df8bae1dSRodney W. Grimes 				mp->m_pkthdr.len -= len;
474df8bae1dSRodney W. Grimes 			return;
475df8bae1dSRodney W. Grimes 		}
476df8bae1dSRodney W. Grimes 		count -= len;
477df8bae1dSRodney W. Grimes 		if (count < 0)
478df8bae1dSRodney W. Grimes 			count = 0;
479df8bae1dSRodney W. Grimes 		/*
480df8bae1dSRodney W. Grimes 		 * Correct length for chain is "count".
481df8bae1dSRodney W. Grimes 		 * Find the mbuf with last data, adjust its length,
482df8bae1dSRodney W. Grimes 		 * and toss data from remaining mbufs on chain.
483df8bae1dSRodney W. Grimes 		 */
484df8bae1dSRodney W. Grimes 		m = mp;
485df8bae1dSRodney W. Grimes 		if (m->m_flags & M_PKTHDR)
486df8bae1dSRodney W. Grimes 			m->m_pkthdr.len = count;
487df8bae1dSRodney W. Grimes 		for (; m; m = m->m_next) {
488df8bae1dSRodney W. Grimes 			if (m->m_len >= count) {
489df8bae1dSRodney W. Grimes 				m->m_len = count;
490df8bae1dSRodney W. Grimes 				break;
491df8bae1dSRodney W. Grimes 			}
492df8bae1dSRodney W. Grimes 			count -= m->m_len;
493df8bae1dSRodney W. Grimes 		}
494797f2d22SPoul-Henning Kamp 		while (m->m_next)
495797f2d22SPoul-Henning Kamp 			(m = m->m_next) ->m_len = 0;
496df8bae1dSRodney W. Grimes 	}
497df8bae1dSRodney W. Grimes }
498df8bae1dSRodney W. Grimes 
499df8bae1dSRodney W. Grimes /*
500df8bae1dSRodney W. Grimes  * Rearange an mbuf chain so that len bytes are contiguous
501df8bae1dSRodney W. Grimes  * and in the data area of an mbuf (so that mtod and dtom
502df8bae1dSRodney W. Grimes  * will work for a structure of size len).  Returns the resulting
503df8bae1dSRodney W. Grimes  * mbuf chain on success, frees it and returns null on failure.
504df8bae1dSRodney W. Grimes  * If there is room, it will add up to max_protohdr-len extra bytes to the
505df8bae1dSRodney W. Grimes  * contiguous region in an attempt to avoid being called next time.
506df8bae1dSRodney W. Grimes  */
507df8bae1dSRodney W. Grimes struct mbuf *
508122a814aSBosko Milekic m_pullup(struct mbuf *n, int len)
509df8bae1dSRodney W. Grimes {
510122a814aSBosko Milekic 	struct mbuf *m;
511122a814aSBosko Milekic 	int count;
512df8bae1dSRodney W. Grimes 	int space;
513df8bae1dSRodney W. Grimes 
514df8bae1dSRodney W. Grimes 	/*
515df8bae1dSRodney W. Grimes 	 * If first mbuf has no cluster, and has room for len bytes
516df8bae1dSRodney W. Grimes 	 * without shifting current data, pullup into it,
517df8bae1dSRodney W. Grimes 	 * otherwise allocate a new mbuf to prepend to the chain.
518df8bae1dSRodney W. Grimes 	 */
519df8bae1dSRodney W. Grimes 	if ((n->m_flags & M_EXT) == 0 &&
520df8bae1dSRodney W. Grimes 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
521df8bae1dSRodney W. Grimes 		if (n->m_len >= len)
522df8bae1dSRodney W. Grimes 			return (n);
523df8bae1dSRodney W. Grimes 		m = n;
524df8bae1dSRodney W. Grimes 		n = n->m_next;
525df8bae1dSRodney W. Grimes 		len -= m->m_len;
526df8bae1dSRodney W. Grimes 	} else {
527df8bae1dSRodney W. Grimes 		if (len > MHLEN)
528df8bae1dSRodney W. Grimes 			goto bad;
529df8bae1dSRodney W. Grimes 		MGET(m, M_DONTWAIT, n->m_type);
530122a814aSBosko Milekic 		if (m == NULL)
531df8bae1dSRodney W. Grimes 			goto bad;
532df8bae1dSRodney W. Grimes 		m->m_len = 0;
533df8bae1dSRodney W. Grimes 		if (n->m_flags & M_PKTHDR) {
534df8bae1dSRodney W. Grimes 			M_COPY_PKTHDR(m, n);
535df8bae1dSRodney W. Grimes 			n->m_flags &= ~M_PKTHDR;
536df8bae1dSRodney W. Grimes 		}
537df8bae1dSRodney W. Grimes 	}
538df8bae1dSRodney W. Grimes 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
539df8bae1dSRodney W. Grimes 	do {
540df8bae1dSRodney W. Grimes 		count = min(min(max(len, max_protohdr), space), n->m_len);
541df8bae1dSRodney W. Grimes 		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
542df8bae1dSRodney W. Grimes 		  (unsigned)count);
543df8bae1dSRodney W. Grimes 		len -= count;
544df8bae1dSRodney W. Grimes 		m->m_len += count;
545df8bae1dSRodney W. Grimes 		n->m_len -= count;
546df8bae1dSRodney W. Grimes 		space -= count;
547df8bae1dSRodney W. Grimes 		if (n->m_len)
548df8bae1dSRodney W. Grimes 			n->m_data += count;
549df8bae1dSRodney W. Grimes 		else
550df8bae1dSRodney W. Grimes 			n = m_free(n);
551df8bae1dSRodney W. Grimes 	} while (len > 0 && n);
552df8bae1dSRodney W. Grimes 	if (len > 0) {
553df8bae1dSRodney W. Grimes 		(void) m_free(m);
554df8bae1dSRodney W. Grimes 		goto bad;
555df8bae1dSRodney W. Grimes 	}
556df8bae1dSRodney W. Grimes 	m->m_next = n;
557df8bae1dSRodney W. Grimes 	return (m);
558df8bae1dSRodney W. Grimes bad:
559df8bae1dSRodney W. Grimes 	m_freem(n);
56008442f8aSBosko Milekic 	mbstat.m_mpfail++;	/* XXX: No consistency. */
561122a814aSBosko Milekic 	return (NULL);
562df8bae1dSRodney W. Grimes }
563df8bae1dSRodney W. Grimes 
564df8bae1dSRodney W. Grimes /*
565df8bae1dSRodney W. Grimes  * Partition an mbuf chain in two pieces, returning the tail --
566df8bae1dSRodney W. Grimes  * all but the first len0 bytes.  In case of failure, it returns NULL and
567df8bae1dSRodney W. Grimes  * attempts to restore the chain to its original state.
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);
596df8bae1dSRodney W. Grimes 			} else
597df8bae1dSRodney W. Grimes 				return (n);
598df8bae1dSRodney W. Grimes 		} else
599df8bae1dSRodney W. Grimes 			MH_ALIGN(n, remain);
600df8bae1dSRodney W. Grimes 	} else if (remain == 0) {
601df8bae1dSRodney W. Grimes 		n = m->m_next;
602122a814aSBosko Milekic 		m->m_next = NULL;
603df8bae1dSRodney W. Grimes 		return (n);
604df8bae1dSRodney W. Grimes 	} else {
605df8bae1dSRodney W. Grimes 		MGET(n, wait, m->m_type);
606122a814aSBosko Milekic 		if (n == NULL)
607122a814aSBosko Milekic 			return (NULL);
608df8bae1dSRodney W. Grimes 		M_ALIGN(n, remain);
609df8bae1dSRodney W. Grimes 	}
610df8bae1dSRodney W. Grimes extpacket:
611df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT) {
612df8bae1dSRodney W. Grimes 		n->m_flags |= M_EXT;
613df8bae1dSRodney W. Grimes 		n->m_ext = m->m_ext;
614a5c4836dSDavid Malone 		MEXT_ADD_REF(m);
615df8bae1dSRodney W. Grimes 		m->m_ext.ext_size = 0; /* For Accounting XXXXXX danger */
616df8bae1dSRodney W. Grimes 		n->m_data = m->m_data + len;
617df8bae1dSRodney W. Grimes 	} else {
618df8bae1dSRodney W. Grimes 		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
619df8bae1dSRodney W. Grimes 	}
620df8bae1dSRodney W. Grimes 	n->m_len = remain;
621df8bae1dSRodney W. Grimes 	m->m_len = len;
622df8bae1dSRodney W. Grimes 	n->m_next = m->m_next;
623122a814aSBosko Milekic 	m->m_next = NULL;
624df8bae1dSRodney W. Grimes 	return (n);
625df8bae1dSRodney W. Grimes }
626df8bae1dSRodney W. Grimes /*
627df8bae1dSRodney W. Grimes  * Routine to copy from device local memory into mbufs.
628f5eece3fSBosko Milekic  * Note that `off' argument is offset into first mbuf of target chain from
629f5eece3fSBosko Milekic  * which to begin copying the data to.
630df8bae1dSRodney W. Grimes  */
631df8bae1dSRodney W. Grimes struct mbuf *
632f5eece3fSBosko Milekic m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
633122a814aSBosko Milekic 	 void (*copy)(char *from, caddr_t to, u_int len))
634df8bae1dSRodney W. Grimes {
635122a814aSBosko Milekic 	struct mbuf *m;
636df8bae1dSRodney W. Grimes 	struct mbuf *top = 0, **mp = &top;
637f5eece3fSBosko Milekic 	int len;
638df8bae1dSRodney W. Grimes 
639f5eece3fSBosko Milekic 	if (off < 0 || off > MHLEN)
640f5eece3fSBosko Milekic 		return (NULL);
641f5eece3fSBosko Milekic 
642df8bae1dSRodney W. Grimes 	MGETHDR(m, M_DONTWAIT, MT_DATA);
643122a814aSBosko Milekic 	if (m == NULL)
644122a814aSBosko Milekic 		return (NULL);
645df8bae1dSRodney W. Grimes 	m->m_pkthdr.rcvif = ifp;
646df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = totlen;
647f5eece3fSBosko Milekic 	len = MHLEN;
648df8bae1dSRodney W. Grimes 
649df8bae1dSRodney W. Grimes 	while (totlen > 0) {
650df8bae1dSRodney W. Grimes 		if (top) {
651df8bae1dSRodney W. Grimes 			MGET(m, M_DONTWAIT, MT_DATA);
652122a814aSBosko Milekic 			if (m == NULL) {
653df8bae1dSRodney W. Grimes 				m_freem(top);
654122a814aSBosko Milekic 				return (NULL);
655df8bae1dSRodney W. Grimes 			}
656f5eece3fSBosko Milekic 			len = MLEN;
657df8bae1dSRodney W. Grimes 		}
658f5eece3fSBosko Milekic 		if (totlen + off >= MINCLSIZE) {
659df8bae1dSRodney W. Grimes 			MCLGET(m, M_DONTWAIT);
660df8bae1dSRodney W. Grimes 			if (m->m_flags & M_EXT)
661f5eece3fSBosko Milekic 				len = MCLBYTES;
662df8bae1dSRodney W. Grimes 		} else {
663df8bae1dSRodney W. Grimes 			/*
664df8bae1dSRodney W. Grimes 			 * Place initial small packet/header at end of mbuf.
665df8bae1dSRodney W. Grimes 			 */
666f5eece3fSBosko Milekic 			if (top == NULL && totlen + off + max_linkhdr <= len) {
667df8bae1dSRodney W. Grimes 				m->m_data += max_linkhdr;
668f5eece3fSBosko Milekic 				len -= max_linkhdr;
669df8bae1dSRodney W. Grimes 			}
670f5eece3fSBosko Milekic 		}
671f5eece3fSBosko Milekic 		if (off) {
672f5eece3fSBosko Milekic 			m->m_data += off;
673f5eece3fSBosko Milekic 			len -= off;
674f5eece3fSBosko Milekic 			off = 0;
675f5eece3fSBosko Milekic 		}
676f5eece3fSBosko Milekic 		m->m_len = len = min(totlen, len);
677df8bae1dSRodney W. Grimes 		if (copy)
678f5eece3fSBosko Milekic 			copy(buf, mtod(m, caddr_t), (unsigned)len);
679df8bae1dSRodney W. Grimes 		else
680f5eece3fSBosko Milekic 			bcopy(buf, mtod(m, caddr_t), (unsigned)len);
681f5eece3fSBosko Milekic 		buf += len;
682df8bae1dSRodney W. Grimes 		*mp = m;
683df8bae1dSRodney W. Grimes 		mp = &m->m_next;
684df8bae1dSRodney W. Grimes 		totlen -= len;
685df8bae1dSRodney W. Grimes 	}
686df8bae1dSRodney W. Grimes 	return (top);
687df8bae1dSRodney W. Grimes }
688c5789ba3SPoul-Henning Kamp 
689c5789ba3SPoul-Henning Kamp /*
690c5789ba3SPoul-Henning Kamp  * Copy data from a buffer back into the indicated mbuf chain,
691c5789ba3SPoul-Henning Kamp  * starting "off" bytes from the beginning, extending the mbuf
692c5789ba3SPoul-Henning Kamp  * chain if necessary.
693c5789ba3SPoul-Henning Kamp  */
694c5789ba3SPoul-Henning Kamp void
695122a814aSBosko Milekic m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
696c5789ba3SPoul-Henning Kamp {
697122a814aSBosko Milekic 	int mlen;
698122a814aSBosko Milekic 	struct mbuf *m = m0, *n;
699c5789ba3SPoul-Henning Kamp 	int totlen = 0;
700c5789ba3SPoul-Henning Kamp 
701122a814aSBosko Milekic 	if (m0 == NULL)
702c5789ba3SPoul-Henning Kamp 		return;
703c5789ba3SPoul-Henning Kamp 	while (off > (mlen = m->m_len)) {
704c5789ba3SPoul-Henning Kamp 		off -= mlen;
705c5789ba3SPoul-Henning Kamp 		totlen += mlen;
706122a814aSBosko Milekic 		if (m->m_next == NULL) {
70708442f8aSBosko Milekic 			n = m_get_clrd(M_DONTWAIT, m->m_type);
708122a814aSBosko Milekic 			if (n == NULL)
709c5789ba3SPoul-Henning Kamp 				goto out;
710c5789ba3SPoul-Henning Kamp 			n->m_len = min(MLEN, len + off);
711c5789ba3SPoul-Henning Kamp 			m->m_next = n;
712c5789ba3SPoul-Henning Kamp 		}
713c5789ba3SPoul-Henning Kamp 		m = m->m_next;
714c5789ba3SPoul-Henning Kamp 	}
715c5789ba3SPoul-Henning Kamp 	while (len > 0) {
716c5789ba3SPoul-Henning Kamp 		mlen = min (m->m_len - off, len);
717c5789ba3SPoul-Henning Kamp 		bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
718c5789ba3SPoul-Henning Kamp 		cp += mlen;
719c5789ba3SPoul-Henning Kamp 		len -= mlen;
720c5789ba3SPoul-Henning Kamp 		mlen += off;
721c5789ba3SPoul-Henning Kamp 		off = 0;
722c5789ba3SPoul-Henning Kamp 		totlen += mlen;
723c5789ba3SPoul-Henning Kamp 		if (len == 0)
724c5789ba3SPoul-Henning Kamp 			break;
725122a814aSBosko Milekic 		if (m->m_next == NULL) {
726c5789ba3SPoul-Henning Kamp 			n = m_get(M_DONTWAIT, m->m_type);
727122a814aSBosko Milekic 			if (n == NULL)
728c5789ba3SPoul-Henning Kamp 				break;
729c5789ba3SPoul-Henning Kamp 			n->m_len = min(MLEN, len);
730c5789ba3SPoul-Henning Kamp 			m->m_next = n;
731c5789ba3SPoul-Henning Kamp 		}
732c5789ba3SPoul-Henning Kamp 		m = m->m_next;
733c5789ba3SPoul-Henning Kamp 	}
734c5789ba3SPoul-Henning Kamp out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
735c5789ba3SPoul-Henning Kamp 		m->m_pkthdr.len = totlen;
736c5789ba3SPoul-Henning Kamp }
737ce4a64f7SPoul-Henning Kamp 
738ce4a64f7SPoul-Henning Kamp void
739ce4a64f7SPoul-Henning Kamp m_print(const struct mbuf *m)
740ce4a64f7SPoul-Henning Kamp {
741ce4a64f7SPoul-Henning Kamp 	int len;
7426357e7b5SEivind Eklund 	const struct mbuf *m2;
743ce4a64f7SPoul-Henning Kamp 
744ce4a64f7SPoul-Henning Kamp 	len = m->m_pkthdr.len;
745ce4a64f7SPoul-Henning Kamp 	m2 = m;
746ce4a64f7SPoul-Henning Kamp 	while (len) {
747ce4a64f7SPoul-Henning Kamp 		printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");
748ce4a64f7SPoul-Henning Kamp 		len -= m2->m_len;
749ce4a64f7SPoul-Henning Kamp 		m2 = m2->m_next;
750ce4a64f7SPoul-Henning Kamp 	}
751ce4a64f7SPoul-Henning Kamp 	return;
752ce4a64f7SPoul-Henning Kamp }
753