uipc_mbuf.c revision 169624
1139804Simp/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1988, 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
291541Srgrimes *	@(#)uipc_mbuf.c	8.2 (Berkeley) 1/4/94
301541Srgrimes */
311541Srgrimes
32116182Sobrien#include <sys/cdefs.h>
33116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/uipc_mbuf.c 169624 2007-05-16 20:41:08Z rwatson $");
34116182Sobrien
35101007Srwatson#include "opt_mac.h"
3677572Sobrien#include "opt_param.h"
37113490Ssilby#include "opt_mbuf_stress_test.h"
38101007Srwatson
391541Srgrimes#include <sys/param.h>
401541Srgrimes#include <sys/systm.h>
4176166Smarkm#include <sys/kernel.h>
42125296Ssilby#include <sys/limits.h>
4376166Smarkm#include <sys/lock.h>
4432036Sbde#include <sys/malloc.h>
451541Srgrimes#include <sys/mbuf.h>
4623081Swollman#include <sys/sysctl.h>
471541Srgrimes#include <sys/domain.h>
481541Srgrimes#include <sys/protosw.h>
49125296Ssilby#include <sys/uio.h>
5076166Smarkm
51163606Srwatson#include <security/mac/mac_framework.h>
52163606Srwatson
539759Sbdeint	max_linkhdr;
549759Sbdeint	max_protohdr;
559759Sbdeint	max_hdr;
569759Sbdeint	max_datalen;
57116455Ssilby#ifdef MBUF_STRESS_TEST
58112777Ssilbyint	m_defragpackets;
59112777Ssilbyint	m_defragbytes;
60112777Ssilbyint	m_defraguseless;
61112777Ssilbyint	m_defragfailure;
62113490Ssilbyint	m_defragrandomfailures;
63113490Ssilby#endif
641541Srgrimes
6566475Sbmilekic/*
6666475Sbmilekic * sysctl(8) exported objects
6766475Sbmilekic */
68155820SandreSYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RD,
69155820Sandre	   &max_linkhdr, 0, "Size of largest link layer header");
70155820SandreSYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RD,
71155820Sandre	   &max_protohdr, 0, "Size of largest protocol layer header");
72155820SandreSYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RD,
73155820Sandre	   &max_hdr, 0, "Size of largest link plus protocol header");
74155820SandreSYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RD,
75155820Sandre	   &max_datalen, 0, "Minimum space left in mbuf after max_hdr");
76116455Ssilby#ifdef MBUF_STRESS_TEST
77112777SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD,
78112777Ssilby	   &m_defragpackets, 0, "");
79112777SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD,
80112777Ssilby	   &m_defragbytes, 0, "");
81112777SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD,
82112777Ssilby	   &m_defraguseless, 0, "");
83112777SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD,
84112777Ssilby	   &m_defragfailure, 0, "");
85113490SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
86113490Ssilby	   &m_defragrandomfailures, 0, "");
87113490Ssilby#endif
8875112Sbmilekic
891541Srgrimes/*
90129906Sbmilekic * Allocate a given length worth of mbufs and/or clusters (whatever fits
91129906Sbmilekic * best) and return a pointer to the top of the allocated chain.  If an
92129906Sbmilekic * existing mbuf chain is provided, then we will append the new chain
93129906Sbmilekic * to the existing one but still return the top of the newly allocated
94129906Sbmilekic * chain.
95129906Sbmilekic */
96129906Sbmilekicstruct mbuf *
97163915Sandrem_getm2(struct mbuf *m, int len, int how, short type, int flags)
98129906Sbmilekic{
99163915Sandre	struct mbuf *mb, *nm = NULL, *mtail = NULL;
100129906Sbmilekic
101163915Sandre	KASSERT(len >= 0, ("%s: len is < 0", __func__));
102129906Sbmilekic
103163915Sandre	/* Validate flags. */
104163915Sandre	flags &= (M_PKTHDR | M_EOR);
105129906Sbmilekic
106163915Sandre	/* Packet header mbuf must be first in chain. */
107163915Sandre	if ((flags & M_PKTHDR) && m != NULL)
108163915Sandre		flags &= ~M_PKTHDR;
109129906Sbmilekic
110163915Sandre	/* Loop and append maximum sized mbufs to the chain tail. */
111163915Sandre	while (len > 0) {
112163915Sandre		if (len > MCLBYTES)
113163915Sandre			mb = m_getjcl(how, type, (flags & M_PKTHDR),
114163915Sandre			    MJUMPAGESIZE);
115163915Sandre		else if (len >= MINCLSIZE)
116163915Sandre			mb = m_getcl(how, type, (flags & M_PKTHDR));
117163915Sandre		else if (flags & M_PKTHDR)
118163915Sandre			mb = m_gethdr(how, type);
119129906Sbmilekic		else
120163915Sandre			mb = m_get(how, type);
121163915Sandre
122163915Sandre		/* Fail the whole operation if one mbuf can't be allocated. */
123163915Sandre		if (mb == NULL) {
124163915Sandre			if (nm != NULL)
125163915Sandre				m_freem(nm);
126163915Sandre			return (NULL);
127163915Sandre		}
128163915Sandre
129163915Sandre		/* Book keeping. */
130163915Sandre		len -= (mb->m_flags & M_EXT) ? mb->m_ext.ext_size :
131163915Sandre			((mb->m_flags & M_PKTHDR) ? MHLEN : MLEN);
132163915Sandre		if (mtail != NULL)
133163915Sandre			mtail->m_next = mb;
134163915Sandre		else
135163915Sandre			nm = mb;
136163915Sandre		mtail = mb;
137163915Sandre		flags &= ~M_PKTHDR;	/* Only valid on the first mbuf. */
138129906Sbmilekic	}
139163915Sandre	if (flags & M_EOR)
140163915Sandre		mtail->m_flags |= M_EOR;  /* Only valid on the last mbuf. */
141129906Sbmilekic
142163915Sandre	/* If mbuf was supplied, append new chain to the end of it. */
143163915Sandre	if (m != NULL) {
144163915Sandre		for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next)
145163915Sandre			;
146163915Sandre		mtail->m_next = nm;
147163915Sandre		mtail->m_flags &= ~M_EOR;
148163915Sandre	} else
149163915Sandre		m = nm;
150163915Sandre
151163915Sandre	return (m);
152129906Sbmilekic}
153129906Sbmilekic
154129906Sbmilekic/*
155129906Sbmilekic * Free an entire chain of mbufs and associated external buffers, if
156129906Sbmilekic * applicable.
157129906Sbmilekic */
158129906Sbmilekicvoid
159129906Sbmilekicm_freem(struct mbuf *mb)
160129906Sbmilekic{
161129906Sbmilekic
162129906Sbmilekic	while (mb != NULL)
163129906Sbmilekic		mb = m_free(mb);
164129906Sbmilekic}
165129906Sbmilekic
166129906Sbmilekic/*-
167129906Sbmilekic * Configure a provided mbuf to refer to the provided external storage
168129906Sbmilekic * buffer and setup a reference count for said buffer.  If the setting
169129906Sbmilekic * up of the reference count fails, the M_EXT bit will not be set.  If
170129906Sbmilekic * successfull, the M_EXT bit is set in the mbuf's flags.
171129906Sbmilekic *
172129906Sbmilekic * Arguments:
173129906Sbmilekic *    mb     The existing mbuf to which to attach the provided buffer.
174129906Sbmilekic *    buf    The address of the provided external storage buffer.
175129906Sbmilekic *    size   The size of the provided buffer.
176129906Sbmilekic *    freef  A pointer to a routine that is responsible for freeing the
177129906Sbmilekic *           provided external storage buffer.
178129906Sbmilekic *    args   A pointer to an argument structure (of any type) to be passed
179129906Sbmilekic *           to the provided freef routine (may be NULL).
180129906Sbmilekic *    flags  Any other flags to be passed to the provided mbuf.
181129906Sbmilekic *    type   The type that the external storage buffer should be
182129906Sbmilekic *           labeled with.
183129906Sbmilekic *
184129906Sbmilekic * Returns:
185129906Sbmilekic *    Nothing.
186129906Sbmilekic */
187129906Sbmilekicvoid
188129906Sbmilekicm_extadd(struct mbuf *mb, caddr_t buf, u_int size,
189129906Sbmilekic    void (*freef)(void *, void *), void *args, int flags, int type)
190129906Sbmilekic{
191151976Sandre	KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__));
192129906Sbmilekic
193151976Sandre	if (type != EXT_EXTREF)
194151976Sandre		mb->m_ext.ref_cnt = (u_int *)uma_zalloc(zone_ext_refcnt, M_NOWAIT);
195129906Sbmilekic	if (mb->m_ext.ref_cnt != NULL) {
196129906Sbmilekic		*(mb->m_ext.ref_cnt) = 1;
197129906Sbmilekic		mb->m_flags |= (M_EXT | flags);
198129906Sbmilekic		mb->m_ext.ext_buf = buf;
199129906Sbmilekic		mb->m_data = mb->m_ext.ext_buf;
200129906Sbmilekic		mb->m_ext.ext_size = size;
201129906Sbmilekic		mb->m_ext.ext_free = freef;
202129906Sbmilekic		mb->m_ext.ext_args = args;
203129906Sbmilekic		mb->m_ext.ext_type = type;
204129906Sbmilekic        }
205129906Sbmilekic}
206129906Sbmilekic
207129906Sbmilekic/*
208129906Sbmilekic * Non-directly-exported function to clean up after mbufs with M_EXT
209151976Sandre * storage attached to them if the reference count hits 1.
210129906Sbmilekic */
211129906Sbmilekicvoid
212129906Sbmilekicmb_free_ext(struct mbuf *m)
213129906Sbmilekic{
214151976Sandre	KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
215151976Sandre	KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__));
216129906Sbmilekic
217151976Sandre	/* Free attached storage if this mbuf is the only reference to it. */
218151976Sandre	if (*(m->m_ext.ref_cnt) == 1 ||
219162515Srrs	    atomic_fetchadd_int(m->m_ext.ref_cnt, -1) == 1) {
220151976Sandre		switch (m->m_ext.ext_type) {
221152101Sandre		case EXT_PACKET:	/* The packet zone is special. */
222152035Sandre			if (*(m->m_ext.ref_cnt) == 0)
223152035Sandre				*(m->m_ext.ref_cnt) = 1;
224151976Sandre			uma_zfree(zone_pack, m);
225151976Sandre			return;		/* Job done. */
226152101Sandre		case EXT_CLUSTER:
227152101Sandre			uma_zfree(zone_clust, m->m_ext.ext_buf);
228130289Sbmilekic			break;
229155780Sandre		case EXT_JUMBOP:
230155780Sandre			uma_zfree(zone_jumbop, m->m_ext.ext_buf);
231153232Sandre			break;
232151976Sandre		case EXT_JUMBO9:
233151976Sandre			uma_zfree(zone_jumbo9, m->m_ext.ext_buf);
234151976Sandre			break;
235151976Sandre		case EXT_JUMBO16:
236151976Sandre			uma_zfree(zone_jumbo16, m->m_ext.ext_buf);
237151976Sandre			break;
238151976Sandre		case EXT_SFBUF:
239151976Sandre		case EXT_NET_DRV:
240151976Sandre		case EXT_MOD_TYPE:
241151976Sandre		case EXT_DISPOSABLE:
242151976Sandre			*(m->m_ext.ref_cnt) = 0;
243151976Sandre			uma_zfree(zone_ext_refcnt, __DEVOLATILE(u_int *,
244151976Sandre				m->m_ext.ref_cnt));
245151976Sandre			/* FALLTHROUGH */
246151976Sandre		case EXT_EXTREF:
247151976Sandre			KASSERT(m->m_ext.ext_free != NULL,
248151976Sandre				("%s: ext_free not set", __func__));
249141668Sbmilekic			(*(m->m_ext.ext_free))(m->m_ext.ext_buf,
250141668Sbmilekic			    m->m_ext.ext_args);
251151976Sandre			break;
252151976Sandre		default:
253151976Sandre			KASSERT(m->m_ext.ext_type == 0,
254151976Sandre				("%s: unknown ext_type", __func__));
255141668Sbmilekic		}
256141668Sbmilekic	}
257151976Sandre	/*
258151976Sandre	 * Free this mbuf back to the mbuf zone with all m_ext
259151976Sandre	 * information purged.
260151976Sandre	 */
261151976Sandre	m->m_ext.ext_buf = NULL;
262151976Sandre	m->m_ext.ext_free = NULL;
263151976Sandre	m->m_ext.ext_args = NULL;
264151976Sandre	m->m_ext.ref_cnt = NULL;
265151976Sandre	m->m_ext.ext_size = 0;
266151976Sandre	m->m_ext.ext_type = 0;
267151976Sandre	m->m_flags &= ~M_EXT;
268130357Sbmilekic	uma_zfree(zone_mbuf, m);
269129906Sbmilekic}
270129906Sbmilekic
271129906Sbmilekic/*
272151976Sandre * Attach the the cluster from *m to *n, set up m_ext in *n
273151976Sandre * and bump the refcount of the cluster.
274151976Sandre */
275151976Sandrestatic void
276151976Sandremb_dupcl(struct mbuf *n, struct mbuf *m)
277151976Sandre{
278151976Sandre	KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
279151976Sandre	KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__));
280151976Sandre	KASSERT((n->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
281151976Sandre
282151976Sandre	if (*(m->m_ext.ref_cnt) == 1)
283151976Sandre		*(m->m_ext.ref_cnt) += 1;
284151976Sandre	else
285151976Sandre		atomic_add_int(m->m_ext.ref_cnt, 1);
286151976Sandre	n->m_ext.ext_buf = m->m_ext.ext_buf;
287151976Sandre	n->m_ext.ext_free = m->m_ext.ext_free;
288151976Sandre	n->m_ext.ext_args = m->m_ext.ext_args;
289151976Sandre	n->m_ext.ext_size = m->m_ext.ext_size;
290151976Sandre	n->m_ext.ref_cnt = m->m_ext.ref_cnt;
291151976Sandre	n->m_ext.ext_type = m->m_ext.ext_type;
292151976Sandre	n->m_flags |= M_EXT;
293151976Sandre}
294151976Sandre
295151976Sandre/*
296149598Sandre * Clean up mbuf (chain) from any tags and packet headers.
297149647Sandre * If "all" is set then the first mbuf in the chain will be
298149647Sandre * cleaned too.
299149598Sandre */
300149598Sandrevoid
301149647Sandrem_demote(struct mbuf *m0, int all)
302149598Sandre{
303149598Sandre	struct mbuf *m;
304149598Sandre
305149598Sandre	for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) {
306149598Sandre		if (m->m_flags & M_PKTHDR) {
307149598Sandre			m_tag_delete_chain(m, NULL);
308149598Sandre			m->m_flags &= ~M_PKTHDR;
309149598Sandre			bzero(&m->m_pkthdr, sizeof(struct pkthdr));
310149598Sandre		}
311149643Sandre		if (m->m_type == MT_HEADER)
312149598Sandre			m->m_type = MT_DATA;
313149643Sandre		if (m != m0 && m->m_nextpkt != NULL)
314149598Sandre			m->m_nextpkt = NULL;
315149598Sandre		m->m_flags = m->m_flags & (M_EXT|M_EOR|M_RDONLY|M_FREELIST);
316149598Sandre	}
317149598Sandre}
318149598Sandre
319149598Sandre/*
320149648Sandre * Sanity checks on mbuf (chain) for use in KASSERT() and general
321149648Sandre * debugging.
322149648Sandre * Returns 0 or panics when bad and 1 on all tests passed.
323149648Sandre * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they
324149648Sandre * blow up later.
325149599Sandre */
326149599Sandreint
327149599Sandrem_sanity(struct mbuf *m0, int sanitize)
328149599Sandre{
329149599Sandre	struct mbuf *m;
330149599Sandre	caddr_t a, b;
331149599Sandre	int pktlen = 0;
332149599Sandre
333168734Skmacy#ifdef INVARIANTS
334168734Skmacy#define	M_SANITY_ACTION(s)	panic("mbuf %p: " s, m)
335168734Skmacy#else
336168734Skmacy#define	M_SANITY_ACTION(s)	printf("mbuf %p: " s, m)
337168734Skmacy#endif
338149599Sandre
339149648Sandre	for (m = m0; m != NULL; m = m->m_next) {
340149599Sandre		/*
341149599Sandre		 * Basic pointer checks.  If any of these fails then some
342149599Sandre		 * unrelated kernel memory before or after us is trashed.
343149599Sandre		 * No way to recover from that.
344149599Sandre		 */
345149648Sandre		a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf :
346149648Sandre			((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) :
347149599Sandre			 (caddr_t)(&m->m_dat)) );
348149599Sandre		b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size :
349149648Sandre			((m->m_flags & M_PKTHDR) ? MHLEN : MLEN)));
350149599Sandre		if ((caddr_t)m->m_data < a)
351149599Sandre			M_SANITY_ACTION("m_data outside mbuf data range left");
352149599Sandre		if ((caddr_t)m->m_data > b)
353149599Sandre			M_SANITY_ACTION("m_data outside mbuf data range right");
354149599Sandre		if ((caddr_t)m->m_data + m->m_len > b)
355149599Sandre			M_SANITY_ACTION("m_data + m_len exeeds mbuf space");
356149648Sandre		if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.header) {
357149599Sandre			if ((caddr_t)m->m_pkthdr.header < a ||
358149599Sandre			    (caddr_t)m->m_pkthdr.header > b)
359149599Sandre				M_SANITY_ACTION("m_pkthdr.header outside mbuf data range");
360149599Sandre		}
361149599Sandre
362149599Sandre		/* m->m_nextpkt may only be set on first mbuf in chain. */
363149648Sandre		if (m != m0 && m->m_nextpkt != NULL) {
364149599Sandre			if (sanitize) {
365149599Sandre				m_freem(m->m_nextpkt);
366149599Sandre				m->m_nextpkt = (struct mbuf *)0xDEADC0DE;
367149599Sandre			} else
368149599Sandre				M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf");
369149599Sandre		}
370149599Sandre
371149599Sandre		/* packet length (not mbuf length!) calculation */
372149599Sandre		if (m0->m_flags & M_PKTHDR)
373149599Sandre			pktlen += m->m_len;
374149599Sandre
375149599Sandre		/* m_tags may only be attached to first mbuf in chain. */
376149599Sandre		if (m != m0 && m->m_flags & M_PKTHDR &&
377149599Sandre		    !SLIST_EMPTY(&m->m_pkthdr.tags)) {
378149599Sandre			if (sanitize) {
379149599Sandre				m_tag_delete_chain(m, NULL);
380149599Sandre				/* put in 0xDEADC0DE perhaps? */
381149648Sandre			} else
382149599Sandre				M_SANITY_ACTION("m_tags on in-chain mbuf");
383149599Sandre		}
384149599Sandre
385149599Sandre		/* M_PKTHDR may only be set on first mbuf in chain */
386149599Sandre		if (m != m0 && m->m_flags & M_PKTHDR) {
387149599Sandre			if (sanitize) {
388149599Sandre				bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
389149599Sandre				m->m_flags &= ~M_PKTHDR;
390149599Sandre				/* put in 0xDEADCODE and leave hdr flag in */
391149599Sandre			} else
392149599Sandre				M_SANITY_ACTION("M_PKTHDR on in-chain mbuf");
393149599Sandre		}
394149599Sandre	}
395149648Sandre	m = m0;
396149648Sandre	if (pktlen && pktlen != m->m_pkthdr.len) {
397149599Sandre		if (sanitize)
398149648Sandre			m->m_pkthdr.len = 0;
399149599Sandre		else
400149599Sandre			M_SANITY_ACTION("m_pkthdr.len != mbuf chain length");
401149599Sandre	}
402149648Sandre	return 1;
403149648Sandre
404149599Sandre#undef	M_SANITY_ACTION
405149599Sandre}
406149599Sandre
407149599Sandre
408149599Sandre/*
409108466Ssam * "Move" mbuf pkthdr from "from" to "to".
410100960Srwatson * "from" must have M_PKTHDR set, and "to" must be empty.
411100960Srwatson */
412100960Srwatsonvoid
413108466Ssamm_move_pkthdr(struct mbuf *to, struct mbuf *from)
414100960Srwatson{
415100960Srwatson
416100960Srwatson#if 0
417108466Ssam	/* see below for why these are not enabled */
418113255Sdes	M_ASSERTPKTHDR(to);
419113487Srwatson	/* Note: with MAC, this may not be a good assertion. */
420108466Ssam	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
421108466Ssam	    ("m_move_pkthdr: to has tags"));
422100960Srwatson#endif
423101007Srwatson#ifdef MAC
424113487Srwatson	/*
425113487Srwatson	 * XXXMAC: It could be this should also occur for non-MAC?
426113487Srwatson	 */
427101007Srwatson	if (to->m_flags & M_PKTHDR)
428113487Srwatson		m_tag_delete_chain(to, NULL);
429101007Srwatson#endif
430143302Ssam	to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
431143302Ssam	if ((to->m_flags & M_EXT) == 0)
432143302Ssam		to->m_data = to->m_pktdat;
433108466Ssam	to->m_pkthdr = from->m_pkthdr;		/* especially tags */
434108466Ssam	SLIST_INIT(&from->m_pkthdr.tags);	/* purge tags from src */
435108466Ssam	from->m_flags &= ~M_PKTHDR;
436108466Ssam}
437108466Ssam
438108466Ssam/*
439108466Ssam * Duplicate "from"'s mbuf pkthdr in "to".
440108466Ssam * "from" must have M_PKTHDR set, and "to" must be empty.
441108466Ssam * In particular, this does a deep copy of the packet tags.
442108466Ssam */
443108466Ssamint
444108466Ssamm_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
445108466Ssam{
446108466Ssam
447108466Ssam#if 0
448108466Ssam	/*
449108466Ssam	 * The mbuf allocator only initializes the pkthdr
450108466Ssam	 * when the mbuf is allocated with MGETHDR. Many users
451108466Ssam	 * (e.g. m_copy*, m_prepend) use MGET and then
452108466Ssam	 * smash the pkthdr as needed causing these
453108466Ssam	 * assertions to trip.  For now just disable them.
454108466Ssam	 */
455113255Sdes	M_ASSERTPKTHDR(to);
456113487Srwatson	/* Note: with MAC, this may not be a good assertion. */
457108466Ssam	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
458108466Ssam#endif
459132488Salfred	MBUF_CHECKSLEEP(how);
460108466Ssam#ifdef MAC
461108466Ssam	if (to->m_flags & M_PKTHDR)
462113487Srwatson		m_tag_delete_chain(to, NULL);
463108466Ssam#endif
464112733Ssilby	to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT);
465112733Ssilby	if ((to->m_flags & M_EXT) == 0)
466112733Ssilby		to->m_data = to->m_pktdat;
467100960Srwatson	to->m_pkthdr = from->m_pkthdr;
468108466Ssam	SLIST_INIT(&to->m_pkthdr.tags);
469113480Srwatson	return (m_tag_copy_chain(to, from, MBTOM(how)));
470100960Srwatson}
471100960Srwatson
472100960Srwatson/*
4731541Srgrimes * Lesser-used path for M_PREPEND:
4741541Srgrimes * allocate new mbuf to prepend to chain,
4751541Srgrimes * copy junk along.
4761541Srgrimes */
4771541Srgrimesstruct mbuf *
47872356Sbmilekicm_prepend(struct mbuf *m, int len, int how)
4791541Srgrimes{
4801541Srgrimes	struct mbuf *mn;
4811541Srgrimes
482117770Ssilby	if (m->m_flags & M_PKTHDR)
483117770Ssilby		MGETHDR(mn, how, m->m_type);
484117770Ssilby	else
485117770Ssilby		MGET(mn, how, m->m_type);
48672356Sbmilekic	if (mn == NULL) {
4871541Srgrimes		m_freem(m);
48872356Sbmilekic		return (NULL);
4891541Srgrimes	}
490113487Srwatson	if (m->m_flags & M_PKTHDR)
491108466Ssam		M_MOVE_PKTHDR(mn, m);
4921541Srgrimes	mn->m_next = m;
4931541Srgrimes	m = mn;
494165447Srrs	if(m->m_flags & M_PKTHDR) {
495165447Srrs		if (len < MHLEN)
496165447Srrs			MH_ALIGN(m, len);
497165447Srrs	} else {
498165447Srrs		if (len < MLEN)
499165447Srrs			M_ALIGN(m, len);
500165447Srrs	}
5011541Srgrimes	m->m_len = len;
5021541Srgrimes	return (m);
5031541Srgrimes}
5041541Srgrimes
5051541Srgrimes/*
5061541Srgrimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
5071541Srgrimes * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
508111119Simp * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
50954002Sarchie * Note that the copy is read-only, because clusters are not copied,
51054002Sarchie * only their reference counts are incremented.
5111541Srgrimes */
5121541Srgrimesstruct mbuf *
51372356Sbmilekicm_copym(struct mbuf *m, int off0, int len, int wait)
5141541Srgrimes{
51572356Sbmilekic	struct mbuf *n, **np;
51672356Sbmilekic	int off = off0;
5171541Srgrimes	struct mbuf *top;
5181541Srgrimes	int copyhdr = 0;
5191541Srgrimes
52052201Salfred	KASSERT(off >= 0, ("m_copym, negative off %d", off));
52152201Salfred	KASSERT(len >= 0, ("m_copym, negative len %d", len));
522132488Salfred	MBUF_CHECKSLEEP(wait);
5231541Srgrimes	if (off == 0 && m->m_flags & M_PKTHDR)
5241541Srgrimes		copyhdr = 1;
5251541Srgrimes	while (off > 0) {
52652201Salfred		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
5271541Srgrimes		if (off < m->m_len)
5281541Srgrimes			break;
5291541Srgrimes		off -= m->m_len;
5301541Srgrimes		m = m->m_next;
5311541Srgrimes	}
5321541Srgrimes	np = &top;
5331541Srgrimes	top = 0;
5341541Srgrimes	while (len > 0) {
53572356Sbmilekic		if (m == NULL) {
53652201Salfred			KASSERT(len == M_COPYALL,
53752201Salfred			    ("m_copym, length > size of mbuf chain"));
5381541Srgrimes			break;
5391541Srgrimes		}
540117770Ssilby		if (copyhdr)
541117770Ssilby			MGETHDR(n, wait, m->m_type);
542117770Ssilby		else
543117770Ssilby			MGET(n, wait, m->m_type);
5441541Srgrimes		*np = n;
54572356Sbmilekic		if (n == NULL)
5461541Srgrimes			goto nospace;
5471541Srgrimes		if (copyhdr) {
548108466Ssam			if (!m_dup_pkthdr(n, m, wait))
549108466Ssam				goto nospace;
5501541Srgrimes			if (len == M_COPYALL)
5511541Srgrimes				n->m_pkthdr.len -= off0;
5521541Srgrimes			else
5531541Srgrimes				n->m_pkthdr.len = len;
5541541Srgrimes			copyhdr = 0;
5551541Srgrimes		}
5561541Srgrimes		n->m_len = min(len, m->m_len - off);
5571541Srgrimes		if (m->m_flags & M_EXT) {
5581541Srgrimes			n->m_data = m->m_data + off;
559151976Sandre			mb_dupcl(n, m);
5601541Srgrimes		} else
5611541Srgrimes			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
562103569Sbmilekic			    (u_int)n->m_len);
5631541Srgrimes		if (len != M_COPYALL)
5641541Srgrimes			len -= n->m_len;
5651541Srgrimes		off = 0;
5661541Srgrimes		m = m->m_next;
5671541Srgrimes		np = &n->m_next;
5681541Srgrimes	}
56978592Sbmilekic	if (top == NULL)
57078592Sbmilekic		mbstat.m_mcfail++;	/* XXX: No consistency. */
57178592Sbmilekic
5721541Srgrimes	return (top);
5731541Srgrimesnospace:
5741541Srgrimes	m_freem(top);
57578592Sbmilekic	mbstat.m_mcfail++;	/* XXX: No consistency. */
57672356Sbmilekic	return (NULL);
5771541Srgrimes}
5781541Srgrimes
5791541Srgrimes/*
580149602Sandre * Returns mbuf chain with new head for the prepending case.
581149602Sandre * Copies from mbuf (chain) n from off for len to mbuf (chain) m
582149602Sandre * either prepending or appending the data.
583149602Sandre * The resulting mbuf (chain) m is fully writeable.
584149602Sandre * m is destination (is made writeable)
585149602Sandre * n is source, off is offset in source, len is len from offset
586149602Sandre * dir, 0 append, 1 prepend
587149602Sandre * how, wait or nowait
588149602Sandre */
589149602Sandre
590149602Sandrestatic int
591149602Sandrem_bcopyxxx(void *s, void *t, u_int len)
592149602Sandre{
593149602Sandre	bcopy(s, t, (size_t)len);
594149602Sandre	return 0;
595149602Sandre}
596149602Sandre
597149602Sandrestruct mbuf *
598149602Sandrem_copymdata(struct mbuf *m, struct mbuf *n, int off, int len,
599149602Sandre    int prep, int how)
600149602Sandre{
601151976Sandre	struct mbuf *mm, *x, *z, *prev = NULL;
602149602Sandre	caddr_t p;
603151976Sandre	int i, nlen = 0;
604149602Sandre	caddr_t buf[MLEN];
605149602Sandre
606149602Sandre	KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source"));
607149602Sandre	KASSERT(off >= 0, ("m_copymdata, negative off %d", off));
608149602Sandre	KASSERT(len >= 0, ("m_copymdata, negative len %d", len));
609149602Sandre	KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep));
610149602Sandre
611151976Sandre	mm = m;
612151976Sandre	if (!prep) {
613151976Sandre		while(mm->m_next) {
614151976Sandre			prev = mm;
615151976Sandre			mm = mm->m_next;
616149602Sandre		}
617149602Sandre	}
618149602Sandre	for (z = n; z != NULL; z = z->m_next)
619149602Sandre		nlen += z->m_len;
620149602Sandre	if (len == M_COPYALL)
621149602Sandre		len = nlen - off;
622149602Sandre	if (off + len > nlen || len < 1)
623149602Sandre		return NULL;
624149602Sandre
625151976Sandre	if (!M_WRITABLE(mm)) {
626151976Sandre		/* XXX: Use proper m_xxx function instead. */
627151976Sandre		x = m_getcl(how, MT_DATA, mm->m_flags);
628151976Sandre		if (x == NULL)
629151976Sandre			return NULL;
630151976Sandre		bcopy(mm->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size);
631151976Sandre		p = x->m_ext.ext_buf + (mm->m_data - mm->m_ext.ext_buf);
632151976Sandre		x->m_data = p;
633151976Sandre		mm->m_next = NULL;
634151976Sandre		if (mm != m)
635151976Sandre			prev->m_next = x;
636151976Sandre		m_free(mm);
637151976Sandre		mm = x;
638151976Sandre	}
639151976Sandre
640149602Sandre	/*
641149602Sandre	 * Append/prepend the data.  Allocating mbufs as necessary.
642149602Sandre	 */
643149602Sandre	/* Shortcut if enough free space in first/last mbuf. */
644149602Sandre	if (!prep && M_TRAILINGSPACE(mm) >= len) {
645149602Sandre		m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) +
646149602Sandre			 mm->m_len);
647149602Sandre		mm->m_len += len;
648149602Sandre		mm->m_pkthdr.len += len;
649149602Sandre		return m;
650149602Sandre	}
651149602Sandre	if (prep && M_LEADINGSPACE(mm) >= len) {
652149602Sandre		mm->m_data = mtod(mm, caddr_t) - len;
653149602Sandre		m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t));
654149602Sandre		mm->m_len += len;
655149602Sandre		mm->m_pkthdr.len += len;
656149602Sandre		return mm;
657149602Sandre	}
658149602Sandre
659149602Sandre	/* Expand first/last mbuf to cluster if possible. */
660149602Sandre	if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) {
661149602Sandre		bcopy(mm->m_data, &buf, mm->m_len);
662149602Sandre		m_clget(mm, how);
663149602Sandre		if (!(mm->m_flags & M_EXT))
664149602Sandre			return NULL;
665149602Sandre		bcopy(&buf, mm->m_ext.ext_buf, mm->m_len);
666149602Sandre		mm->m_data = mm->m_ext.ext_buf;
667149602Sandre		mm->m_pkthdr.header = NULL;
668149602Sandre	}
669149602Sandre	if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) {
670149602Sandre		bcopy(mm->m_data, &buf, mm->m_len);
671149602Sandre		m_clget(mm, how);
672149602Sandre		if (!(mm->m_flags & M_EXT))
673149602Sandre			return NULL;
674149602Sandre		bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf +
675149602Sandre		       mm->m_ext.ext_size - mm->m_len, mm->m_len);
676149602Sandre		mm->m_data = (caddr_t)mm->m_ext.ext_buf +
677149602Sandre			      mm->m_ext.ext_size - mm->m_len;
678149602Sandre		mm->m_pkthdr.header = NULL;
679149602Sandre	}
680149602Sandre
681149602Sandre	/* Append/prepend as many mbuf (clusters) as necessary to fit len. */
682149602Sandre	if (!prep && len > M_TRAILINGSPACE(mm)) {
683149602Sandre		if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA))
684149602Sandre			return NULL;
685149602Sandre	}
686149602Sandre	if (prep && len > M_LEADINGSPACE(mm)) {
687149602Sandre		if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA)))
688149602Sandre			return NULL;
689149602Sandre		i = 0;
690149602Sandre		for (x = z; x != NULL; x = x->m_next) {
691149602Sandre			i += x->m_flags & M_EXT ? x->m_ext.ext_size :
692149602Sandre			      (x->m_flags & M_PKTHDR ? MHLEN : MLEN);
693149602Sandre			if (!x->m_next)
694149602Sandre				break;
695149602Sandre		}
696149602Sandre		z->m_data += i - len;
697149602Sandre		m_move_pkthdr(mm, z);
698149602Sandre		x->m_next = mm;
699149602Sandre		mm = z;
700149602Sandre	}
701149602Sandre
702149602Sandre	/* Seek to start position in source mbuf. Optimization for long chains. */
703149602Sandre	while (off > 0) {
704149602Sandre		if (off < n->m_len)
705149602Sandre			break;
706149602Sandre		off -= n->m_len;
707149602Sandre		n = n->m_next;
708149602Sandre	}
709149602Sandre
710149602Sandre	/* Copy data into target mbuf. */
711149602Sandre	z = mm;
712149602Sandre	while (len > 0) {
713149602Sandre		KASSERT(z != NULL, ("m_copymdata, falling off target edge"));
714149602Sandre		i = M_TRAILINGSPACE(z);
715149602Sandre		m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len);
716149602Sandre		z->m_len += i;
717149602Sandre		/* fixup pkthdr.len if necessary */
718149602Sandre		if ((prep ? mm : m)->m_flags & M_PKTHDR)
719149602Sandre			(prep ? mm : m)->m_pkthdr.len += i;
720149602Sandre		off += i;
721149602Sandre		len -= i;
722149602Sandre		z = z->m_next;
723149602Sandre	}
724149602Sandre	return (prep ? mm : m);
725149602Sandre}
726149602Sandre
727149602Sandre/*
72815689Swollman * Copy an entire packet, including header (which must be present).
72915689Swollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
73054002Sarchie * Note that the copy is read-only, because clusters are not copied,
73154002Sarchie * only their reference counts are incremented.
73272750Sluigi * Preserve alignment of the first mbuf so if the creator has left
73372750Sluigi * some room at the beginning (e.g. for inserting protocol headers)
73472750Sluigi * the copies still have the room available.
73515689Swollman */
73615689Swollmanstruct mbuf *
73772356Sbmilekicm_copypacket(struct mbuf *m, int how)
73815689Swollman{
73915689Swollman	struct mbuf *top, *n, *o;
74015689Swollman
741132488Salfred	MBUF_CHECKSLEEP(how);
74215689Swollman	MGET(n, how, m->m_type);
74315689Swollman	top = n;
74472356Sbmilekic	if (n == NULL)
74515689Swollman		goto nospace;
74615689Swollman
747108466Ssam	if (!m_dup_pkthdr(n, m, how))
748108466Ssam		goto nospace;
74915689Swollman	n->m_len = m->m_len;
75015689Swollman	if (m->m_flags & M_EXT) {
75115689Swollman		n->m_data = m->m_data;
752151976Sandre		mb_dupcl(n, m);
75315689Swollman	} else {
75472750Sluigi		n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
75515689Swollman		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
75615689Swollman	}
75715689Swollman
75815689Swollman	m = m->m_next;
75915689Swollman	while (m) {
76015689Swollman		MGET(o, how, m->m_type);
76172356Sbmilekic		if (o == NULL)
76215689Swollman			goto nospace;
76315689Swollman
76415689Swollman		n->m_next = o;
76515689Swollman		n = n->m_next;
76615689Swollman
76715689Swollman		n->m_len = m->m_len;
76815689Swollman		if (m->m_flags & M_EXT) {
76915689Swollman			n->m_data = m->m_data;
770151976Sandre			mb_dupcl(n, m);
77115689Swollman		} else {
77215689Swollman			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
77315689Swollman		}
77415689Swollman
77515689Swollman		m = m->m_next;
77615689Swollman	}
77715689Swollman	return top;
77815689Swollmannospace:
77915689Swollman	m_freem(top);
78078592Sbmilekic	mbstat.m_mcfail++;	/* XXX: No consistency. */
78172356Sbmilekic	return (NULL);
78215689Swollman}
78315689Swollman
78415689Swollman/*
7851541Srgrimes * Copy data from an mbuf chain starting "off" bytes from the beginning,
7861541Srgrimes * continuing for "len" bytes, into the indicated buffer.
7871541Srgrimes */
7881549Srgrimesvoid
78981907Sjulianm_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
7901541Srgrimes{
791103569Sbmilekic	u_int count;
7921541Srgrimes
79352201Salfred	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
79452201Salfred	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
7951541Srgrimes	while (off > 0) {
79652201Salfred		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
7971541Srgrimes		if (off < m->m_len)
7981541Srgrimes			break;
7991541Srgrimes		off -= m->m_len;
8001541Srgrimes		m = m->m_next;
8011541Srgrimes	}
8021541Srgrimes	while (len > 0) {
80352201Salfred		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
8041541Srgrimes		count = min(m->m_len - off, len);
8051541Srgrimes		bcopy(mtod(m, caddr_t) + off, cp, count);
8061541Srgrimes		len -= count;
8071541Srgrimes		cp += count;
8081541Srgrimes		off = 0;
8091541Srgrimes		m = m->m_next;
8101541Srgrimes	}
8111541Srgrimes}
8121541Srgrimes
8131541Srgrimes/*
81454002Sarchie * Copy a packet header mbuf chain into a completely new chain, including
81554002Sarchie * copying any mbuf clusters.  Use this instead of m_copypacket() when
81654002Sarchie * you need a writable copy of an mbuf chain.
81754002Sarchie */
81854002Sarchiestruct mbuf *
81972356Sbmilekicm_dup(struct mbuf *m, int how)
82054002Sarchie{
82154002Sarchie	struct mbuf **p, *top = NULL;
82254002Sarchie	int remain, moff, nsize;
82354002Sarchie
824132488Salfred	MBUF_CHECKSLEEP(how);
82554002Sarchie	/* Sanity check */
82654002Sarchie	if (m == NULL)
82772356Sbmilekic		return (NULL);
828113255Sdes	M_ASSERTPKTHDR(m);
82954002Sarchie
83054002Sarchie	/* While there's more data, get a new mbuf, tack it on, and fill it */
83154002Sarchie	remain = m->m_pkthdr.len;
83254002Sarchie	moff = 0;
83354002Sarchie	p = &top;
83454002Sarchie	while (remain > 0 || top == NULL) {	/* allow m->m_pkthdr.len == 0 */
83554002Sarchie		struct mbuf *n;
83654002Sarchie
83754002Sarchie		/* Get the next new mbuf */
838129906Sbmilekic		if (remain >= MINCLSIZE) {
839129906Sbmilekic			n = m_getcl(how, m->m_type, 0);
840129906Sbmilekic			nsize = MCLBYTES;
841129906Sbmilekic		} else {
842129906Sbmilekic			n = m_get(how, m->m_type);
843129906Sbmilekic			nsize = MLEN;
844129906Sbmilekic		}
84554002Sarchie		if (n == NULL)
84654002Sarchie			goto nospace;
847129906Sbmilekic
848129906Sbmilekic		if (top == NULL) {		/* First one, must be PKTHDR */
849129906Sbmilekic			if (!m_dup_pkthdr(n, m, how)) {
850129906Sbmilekic				m_free(n);
851108466Ssam				goto nospace;
852129906Sbmilekic			}
853153428Semaste			if ((n->m_flags & M_EXT) == 0)
854153428Semaste				nsize = MHLEN;
85554002Sarchie		}
85654002Sarchie		n->m_len = 0;
85754002Sarchie
85854002Sarchie		/* Link it into the new chain */
85954002Sarchie		*p = n;
86054002Sarchie		p = &n->m_next;
86154002Sarchie
86254002Sarchie		/* Copy data from original mbuf(s) into new mbuf */
86354002Sarchie		while (n->m_len < nsize && m != NULL) {
86454002Sarchie			int chunk = min(nsize - n->m_len, m->m_len - moff);
86554002Sarchie
86654002Sarchie			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
86754002Sarchie			moff += chunk;
86854002Sarchie			n->m_len += chunk;
86954002Sarchie			remain -= chunk;
87054002Sarchie			if (moff == m->m_len) {
87154002Sarchie				m = m->m_next;
87254002Sarchie				moff = 0;
87354002Sarchie			}
87454002Sarchie		}
87554002Sarchie
87654002Sarchie		/* Check correct total mbuf length */
87754002Sarchie		KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
87887594Sobrien		    	("%s: bogus m_pkthdr.len", __func__));
87954002Sarchie	}
88054002Sarchie	return (top);
88154002Sarchie
88254002Sarchienospace:
88354002Sarchie	m_freem(top);
88478592Sbmilekic	mbstat.m_mcfail++;	/* XXX: No consistency. */
88572356Sbmilekic	return (NULL);
88654002Sarchie}
88754002Sarchie
88854002Sarchie/*
8891541Srgrimes * Concatenate mbuf chain n to m.
8901541Srgrimes * Both chains must be of the same type (e.g. MT_DATA).
8911541Srgrimes * Any m_pkthdr is not updated.
8921541Srgrimes */
8931549Srgrimesvoid
89472356Sbmilekicm_cat(struct mbuf *m, struct mbuf *n)
8951541Srgrimes{
8961541Srgrimes	while (m->m_next)
8971541Srgrimes		m = m->m_next;
8981541Srgrimes	while (n) {
8991541Srgrimes		if (m->m_flags & M_EXT ||
9001541Srgrimes		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
9011541Srgrimes			/* just join the two chains */
9021541Srgrimes			m->m_next = n;
9031541Srgrimes			return;
9041541Srgrimes		}
9051541Srgrimes		/* splat the data from one into the other */
9061541Srgrimes		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
9071541Srgrimes		    (u_int)n->m_len);
9081541Srgrimes		m->m_len += n->m_len;
9091541Srgrimes		n = m_free(n);
9101541Srgrimes	}
9111541Srgrimes}
9121541Srgrimes
9131549Srgrimesvoid
91472356Sbmilekicm_adj(struct mbuf *mp, int req_len)
9151541Srgrimes{
91672356Sbmilekic	int len = req_len;
91772356Sbmilekic	struct mbuf *m;
91872356Sbmilekic	int count;
9191541Srgrimes
9201541Srgrimes	if ((m = mp) == NULL)
9211541Srgrimes		return;
9221541Srgrimes	if (len >= 0) {
9231541Srgrimes		/*
9241541Srgrimes		 * Trim from head.
9251541Srgrimes		 */
9261541Srgrimes		while (m != NULL && len > 0) {
9271541Srgrimes			if (m->m_len <= len) {
9281541Srgrimes				len -= m->m_len;
9291541Srgrimes				m->m_len = 0;
9301541Srgrimes				m = m->m_next;
9311541Srgrimes			} else {
9321541Srgrimes				m->m_len -= len;
9331541Srgrimes				m->m_data += len;
9341541Srgrimes				len = 0;
9351541Srgrimes			}
9361541Srgrimes		}
9371541Srgrimes		m = mp;
9381541Srgrimes		if (mp->m_flags & M_PKTHDR)
9391541Srgrimes			m->m_pkthdr.len -= (req_len - len);
9401541Srgrimes	} else {
9411541Srgrimes		/*
9421541Srgrimes		 * Trim from tail.  Scan the mbuf chain,
9431541Srgrimes		 * calculating its length and finding the last mbuf.
9441541Srgrimes		 * If the adjustment only affects this mbuf, then just
9451541Srgrimes		 * adjust and return.  Otherwise, rescan and truncate
9461541Srgrimes		 * after the remaining size.
9471541Srgrimes		 */
9481541Srgrimes		len = -len;
9491541Srgrimes		count = 0;
9501541Srgrimes		for (;;) {
9511541Srgrimes			count += m->m_len;
9521541Srgrimes			if (m->m_next == (struct mbuf *)0)
9531541Srgrimes				break;
9541541Srgrimes			m = m->m_next;
9551541Srgrimes		}
9561541Srgrimes		if (m->m_len >= len) {
9571541Srgrimes			m->m_len -= len;
9581541Srgrimes			if (mp->m_flags & M_PKTHDR)
9591541Srgrimes				mp->m_pkthdr.len -= len;
9601541Srgrimes			return;
9611541Srgrimes		}
9621541Srgrimes		count -= len;
9631541Srgrimes		if (count < 0)
9641541Srgrimes			count = 0;
9651541Srgrimes		/*
9661541Srgrimes		 * Correct length for chain is "count".
9671541Srgrimes		 * Find the mbuf with last data, adjust its length,
9681541Srgrimes		 * and toss data from remaining mbufs on chain.
9691541Srgrimes		 */
9701541Srgrimes		m = mp;
9711541Srgrimes		if (m->m_flags & M_PKTHDR)
9721541Srgrimes			m->m_pkthdr.len = count;
9731541Srgrimes		for (; m; m = m->m_next) {
9741541Srgrimes			if (m->m_len >= count) {
9751541Srgrimes				m->m_len = count;
976142350Ssam				if (m->m_next != NULL) {
977142350Ssam					m_freem(m->m_next);
978142350Ssam					m->m_next = NULL;
979142350Ssam				}
9801541Srgrimes				break;
9811541Srgrimes			}
9821541Srgrimes			count -= m->m_len;
9831541Srgrimes		}
9841541Srgrimes	}
9851541Srgrimes}
9861541Srgrimes
9871541Srgrimes/*
9881541Srgrimes * Rearange an mbuf chain so that len bytes are contiguous
9891541Srgrimes * and in the data area of an mbuf (so that mtod and dtom
9901541Srgrimes * will work for a structure of size len).  Returns the resulting
9911541Srgrimes * mbuf chain on success, frees it and returns null on failure.
9921541Srgrimes * If there is room, it will add up to max_protohdr-len extra bytes to the
9931541Srgrimes * contiguous region in an attempt to avoid being called next time.
9941541Srgrimes */
9951541Srgrimesstruct mbuf *
99672356Sbmilekicm_pullup(struct mbuf *n, int len)
9971541Srgrimes{
99872356Sbmilekic	struct mbuf *m;
99972356Sbmilekic	int count;
10001541Srgrimes	int space;
10011541Srgrimes
10021541Srgrimes	/*
10031541Srgrimes	 * If first mbuf has no cluster, and has room for len bytes
10041541Srgrimes	 * without shifting current data, pullup into it,
10051541Srgrimes	 * otherwise allocate a new mbuf to prepend to the chain.
10061541Srgrimes	 */
10071541Srgrimes	if ((n->m_flags & M_EXT) == 0 &&
10081541Srgrimes	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
10091541Srgrimes		if (n->m_len >= len)
10101541Srgrimes			return (n);
10111541Srgrimes		m = n;
10121541Srgrimes		n = n->m_next;
10131541Srgrimes		len -= m->m_len;
10141541Srgrimes	} else {
10151541Srgrimes		if (len > MHLEN)
10161541Srgrimes			goto bad;
1017111119Simp		MGET(m, M_DONTWAIT, n->m_type);
101872356Sbmilekic		if (m == NULL)
10191541Srgrimes			goto bad;
10201541Srgrimes		m->m_len = 0;
1021108466Ssam		if (n->m_flags & M_PKTHDR)
1022108466Ssam			M_MOVE_PKTHDR(m, n);
10231541Srgrimes	}
10241541Srgrimes	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
10251541Srgrimes	do {
10261541Srgrimes		count = min(min(max(len, max_protohdr), space), n->m_len);
10271541Srgrimes		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
1028103569Sbmilekic		  (u_int)count);
10291541Srgrimes		len -= count;
10301541Srgrimes		m->m_len += count;
10311541Srgrimes		n->m_len -= count;
10321541Srgrimes		space -= count;
10331541Srgrimes		if (n->m_len)
10341541Srgrimes			n->m_data += count;
10351541Srgrimes		else
10361541Srgrimes			n = m_free(n);
10371541Srgrimes	} while (len > 0 && n);
10381541Srgrimes	if (len > 0) {
10391541Srgrimes		(void) m_free(m);
10401541Srgrimes		goto bad;
10411541Srgrimes	}
10421541Srgrimes	m->m_next = n;
10431541Srgrimes	return (m);
10441541Srgrimesbad:
10451541Srgrimes	m_freem(n);
104678592Sbmilekic	mbstat.m_mpfail++;	/* XXX: No consistency. */
104772356Sbmilekic	return (NULL);
10481541Srgrimes}
10491541Srgrimes
10501541Srgrimes/*
1051143761Sjmg * Like m_pullup(), except a new mbuf is always allocated, and we allow
1052143761Sjmg * the amount of empty space before the data in the new mbuf to be specified
1053143761Sjmg * (in the event that the caller expects to prepend later).
1054143761Sjmg */
1055143761Sjmgint MSFail;
1056143761Sjmg
1057143761Sjmgstruct mbuf *
1058143761Sjmgm_copyup(struct mbuf *n, int len, int dstoff)
1059143761Sjmg{
1060143761Sjmg	struct mbuf *m;
1061143761Sjmg	int count, space;
1062143761Sjmg
1063143761Sjmg	if (len > (MHLEN - dstoff))
1064143761Sjmg		goto bad;
1065143761Sjmg	MGET(m, M_DONTWAIT, n->m_type);
1066143761Sjmg	if (m == NULL)
1067143761Sjmg		goto bad;
1068143761Sjmg	m->m_len = 0;
1069143761Sjmg	if (n->m_flags & M_PKTHDR)
1070143761Sjmg		M_MOVE_PKTHDR(m, n);
1071143761Sjmg	m->m_data += dstoff;
1072143761Sjmg	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
1073143761Sjmg	do {
1074143761Sjmg		count = min(min(max(len, max_protohdr), space), n->m_len);
1075143761Sjmg		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
1076143761Sjmg		    (unsigned)count);
1077143761Sjmg		len -= count;
1078143761Sjmg		m->m_len += count;
1079143761Sjmg		n->m_len -= count;
1080143761Sjmg		space -= count;
1081143761Sjmg		if (n->m_len)
1082143761Sjmg			n->m_data += count;
1083143761Sjmg		else
1084143761Sjmg			n = m_free(n);
1085143761Sjmg	} while (len > 0 && n);
1086143761Sjmg	if (len > 0) {
1087143761Sjmg		(void) m_free(m);
1088143761Sjmg		goto bad;
1089143761Sjmg	}
1090143761Sjmg	m->m_next = n;
1091143761Sjmg	return (m);
1092143761Sjmg bad:
1093143761Sjmg	m_freem(n);
1094143761Sjmg	MSFail++;
1095143761Sjmg	return (NULL);
1096143761Sjmg}
1097143761Sjmg
1098143761Sjmg/*
10991541Srgrimes * Partition an mbuf chain in two pieces, returning the tail --
11001541Srgrimes * all but the first len0 bytes.  In case of failure, it returns NULL and
11011541Srgrimes * attempts to restore the chain to its original state.
110297681Sarchie *
110397681Sarchie * Note that the resulting mbufs might be read-only, because the new
110497681Sarchie * mbuf can end up sharing an mbuf cluster with the original mbuf if
110597681Sarchie * the "breaking point" happens to lie within a cluster mbuf. Use the
110697681Sarchie * M_WRITABLE() macro to check for this case.
11071541Srgrimes */
11081541Srgrimesstruct mbuf *
110972356Sbmilekicm_split(struct mbuf *m0, int len0, int wait)
11101541Srgrimes{
111172356Sbmilekic	struct mbuf *m, *n;
1112103569Sbmilekic	u_int len = len0, remain;
11131541Srgrimes
1114132488Salfred	MBUF_CHECKSLEEP(wait);
11151541Srgrimes	for (m = m0; m && len > m->m_len; m = m->m_next)
11161541Srgrimes		len -= m->m_len;
111772356Sbmilekic	if (m == NULL)
111872356Sbmilekic		return (NULL);
11191541Srgrimes	remain = m->m_len - len;
11201541Srgrimes	if (m0->m_flags & M_PKTHDR) {
11211541Srgrimes		MGETHDR(n, wait, m0->m_type);
112272356Sbmilekic		if (n == NULL)
112372356Sbmilekic			return (NULL);
11241541Srgrimes		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
11251541Srgrimes		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
11261541Srgrimes		m0->m_pkthdr.len = len0;
11271541Srgrimes		if (m->m_flags & M_EXT)
11281541Srgrimes			goto extpacket;
11291541Srgrimes		if (remain > MHLEN) {
11301541Srgrimes			/* m can't be the lead packet */
11311541Srgrimes			MH_ALIGN(n, 0);
11321541Srgrimes			n->m_next = m_split(m, len, wait);
113372356Sbmilekic			if (n->m_next == NULL) {
11341541Srgrimes				(void) m_free(n);
113572356Sbmilekic				return (NULL);
113694471Shsu			} else {
113794471Shsu				n->m_len = 0;
11381541Srgrimes				return (n);
113994471Shsu			}
11401541Srgrimes		} else
11411541Srgrimes			MH_ALIGN(n, remain);
11421541Srgrimes	} else if (remain == 0) {
11431541Srgrimes		n = m->m_next;
114472356Sbmilekic		m->m_next = NULL;
11451541Srgrimes		return (n);
11461541Srgrimes	} else {
11471541Srgrimes		MGET(n, wait, m->m_type);
114872356Sbmilekic		if (n == NULL)
114972356Sbmilekic			return (NULL);
11501541Srgrimes		M_ALIGN(n, remain);
11511541Srgrimes	}
11521541Srgrimesextpacket:
11531541Srgrimes	if (m->m_flags & M_EXT) {
11541541Srgrimes		n->m_data = m->m_data + len;
1155151976Sandre		mb_dupcl(n, m);
11561541Srgrimes	} else {
11571541Srgrimes		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
11581541Srgrimes	}
11591541Srgrimes	n->m_len = remain;
11601541Srgrimes	m->m_len = len;
11611541Srgrimes	n->m_next = m->m_next;
116272356Sbmilekic	m->m_next = NULL;
11631541Srgrimes	return (n);
11641541Srgrimes}
11651541Srgrimes/*
11661541Srgrimes * Routine to copy from device local memory into mbufs.
116778508Sbmilekic * Note that `off' argument is offset into first mbuf of target chain from
116878508Sbmilekic * which to begin copying the data to.
11691541Srgrimes */
11701541Srgrimesstruct mbuf *
117178508Sbmilekicm_devget(char *buf, int totlen, int off, struct ifnet *ifp,
1172169624Srwatson    void (*copy)(char *from, caddr_t to, u_int len))
11731541Srgrimes{
117472356Sbmilekic	struct mbuf *m;
1175129906Sbmilekic	struct mbuf *top = NULL, **mp = &top;
117678508Sbmilekic	int len;
11771541Srgrimes
117878508Sbmilekic	if (off < 0 || off > MHLEN)
117978508Sbmilekic		return (NULL);
118078508Sbmilekic
1181129906Sbmilekic	while (totlen > 0) {
1182129906Sbmilekic		if (top == NULL) {	/* First one, must be PKTHDR */
1183129906Sbmilekic			if (totlen + off >= MINCLSIZE) {
1184129906Sbmilekic				m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1185129906Sbmilekic				len = MCLBYTES;
1186129906Sbmilekic			} else {
1187129906Sbmilekic				m = m_gethdr(M_DONTWAIT, MT_DATA);
1188129906Sbmilekic				len = MHLEN;
11891541Srgrimes
1190129906Sbmilekic				/* Place initial small packet/header at end of mbuf */
1191129906Sbmilekic				if (m && totlen + off + max_linkhdr <= MLEN) {
1192129906Sbmilekic					m->m_data += max_linkhdr;
1193129906Sbmilekic					len -= max_linkhdr;
1194129906Sbmilekic				}
1195129906Sbmilekic			}
1196129906Sbmilekic			if (m == NULL)
1197129906Sbmilekic				return NULL;
1198129906Sbmilekic			m->m_pkthdr.rcvif = ifp;
1199129906Sbmilekic			m->m_pkthdr.len = totlen;
1200129906Sbmilekic		} else {
1201129906Sbmilekic			if (totlen + off >= MINCLSIZE) {
1202129906Sbmilekic				m = m_getcl(M_DONTWAIT, MT_DATA, 0);
1203129906Sbmilekic				len = MCLBYTES;
1204129906Sbmilekic			} else {
1205129906Sbmilekic				m = m_get(M_DONTWAIT, MT_DATA);
1206129906Sbmilekic				len = MLEN;
1207129906Sbmilekic			}
120872356Sbmilekic			if (m == NULL) {
12091541Srgrimes				m_freem(top);
1210129906Sbmilekic				return NULL;
12111541Srgrimes			}
12121541Srgrimes		}
121378508Sbmilekic		if (off) {
121478508Sbmilekic			m->m_data += off;
121578508Sbmilekic			len -= off;
121678508Sbmilekic			off = 0;
121778508Sbmilekic		}
121878508Sbmilekic		m->m_len = len = min(totlen, len);
12191541Srgrimes		if (copy)
1220103569Sbmilekic			copy(buf, mtod(m, caddr_t), (u_int)len);
12211541Srgrimes		else
1222103569Sbmilekic			bcopy(buf, mtod(m, caddr_t), (u_int)len);
122378508Sbmilekic		buf += len;
12241541Srgrimes		*mp = m;
12251541Srgrimes		mp = &m->m_next;
12261541Srgrimes		totlen -= len;
12271541Srgrimes	}
12281541Srgrimes	return (top);
12291541Srgrimes}
12303352Sphk
12313352Sphk/*
12323352Sphk * Copy data from a buffer back into the indicated mbuf chain,
12333352Sphk * starting "off" bytes from the beginning, extending the mbuf
12343352Sphk * chain if necessary.
12353352Sphk */
12363352Sphkvoid
1237128402Sluigim_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp)
12383352Sphk{
123972356Sbmilekic	int mlen;
124072356Sbmilekic	struct mbuf *m = m0, *n;
12413352Sphk	int totlen = 0;
12423352Sphk
124372356Sbmilekic	if (m0 == NULL)
12443352Sphk		return;
12453352Sphk	while (off > (mlen = m->m_len)) {
12463352Sphk		off -= mlen;
12473352Sphk		totlen += mlen;
124872356Sbmilekic		if (m->m_next == NULL) {
1249129906Sbmilekic			n = m_get(M_DONTWAIT, m->m_type);
125072356Sbmilekic			if (n == NULL)
12513352Sphk				goto out;
1252129906Sbmilekic			bzero(mtod(n, caddr_t), MLEN);
12533352Sphk			n->m_len = min(MLEN, len + off);
12543352Sphk			m->m_next = n;
12553352Sphk		}
12563352Sphk		m = m->m_next;
12573352Sphk	}
12583352Sphk	while (len > 0) {
12593352Sphk		mlen = min (m->m_len - off, len);
1260103569Sbmilekic		bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
12613352Sphk		cp += mlen;
12623352Sphk		len -= mlen;
12633352Sphk		mlen += off;
12643352Sphk		off = 0;
12653352Sphk		totlen += mlen;
12663352Sphk		if (len == 0)
12673352Sphk			break;
126872356Sbmilekic		if (m->m_next == NULL) {
1269111119Simp			n = m_get(M_DONTWAIT, m->m_type);
127072356Sbmilekic			if (n == NULL)
12713352Sphk				break;
12723352Sphk			n->m_len = min(MLEN, len);
12733352Sphk			m->m_next = n;
12743352Sphk		}
12753352Sphk		m = m->m_next;
12763352Sphk	}
12773352Sphkout:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
12783352Sphk		m->m_pkthdr.len = totlen;
12793352Sphk}
128052756Sphk
1281123557Sbms/*
1282138541Ssam * Append the specified data to the indicated mbuf chain,
1283138541Ssam * Extend the mbuf chain if the new data does not fit in
1284138541Ssam * existing space.
1285138541Ssam *
1286138541Ssam * Return 1 if able to complete the job; otherwise 0.
1287138541Ssam */
1288138541Ssamint
1289138541Ssamm_append(struct mbuf *m0, int len, c_caddr_t cp)
1290138541Ssam{
1291138541Ssam	struct mbuf *m, *n;
1292138541Ssam	int remainder, space;
1293138541Ssam
1294138541Ssam	for (m = m0; m->m_next != NULL; m = m->m_next)
1295138541Ssam		;
1296138541Ssam	remainder = len;
1297138541Ssam	space = M_TRAILINGSPACE(m);
1298138541Ssam	if (space > 0) {
1299138541Ssam		/*
1300138541Ssam		 * Copy into available space.
1301138541Ssam		 */
1302138541Ssam		if (space > remainder)
1303138541Ssam			space = remainder;
1304138541Ssam		bcopy(cp, mtod(m, caddr_t) + m->m_len, space);
1305138541Ssam		m->m_len += space;
1306138541Ssam		cp += space, remainder -= space;
1307138541Ssam	}
1308138541Ssam	while (remainder > 0) {
1309138541Ssam		/*
1310138541Ssam		 * Allocate a new mbuf; could check space
1311138541Ssam		 * and allocate a cluster instead.
1312138541Ssam		 */
1313138541Ssam		n = m_get(M_DONTWAIT, m->m_type);
1314138541Ssam		if (n == NULL)
1315138541Ssam			break;
1316138541Ssam		n->m_len = min(MLEN, remainder);
1317138894Ssam		bcopy(cp, mtod(n, caddr_t), n->m_len);
1318138894Ssam		cp += n->m_len, remainder -= n->m_len;
1319138541Ssam		m->m_next = n;
1320138541Ssam		m = n;
1321138541Ssam	}
1322138541Ssam	if (m0->m_flags & M_PKTHDR)
1323138541Ssam		m0->m_pkthdr.len += len - remainder;
1324138541Ssam	return (remainder == 0);
1325138541Ssam}
1326138541Ssam
1327138541Ssam/*
1328123557Sbms * Apply function f to the data in an mbuf chain starting "off" bytes from
1329123557Sbms * the beginning, continuing for "len" bytes.
1330123557Sbms */
1331123557Sbmsint
1332123557Sbmsm_apply(struct mbuf *m, int off, int len,
1333123564Sbms    int (*f)(void *, void *, u_int), void *arg)
1334123557Sbms{
1335123564Sbms	u_int count;
1336123557Sbms	int rval;
1337123557Sbms
1338123557Sbms	KASSERT(off >= 0, ("m_apply, negative off %d", off));
1339123557Sbms	KASSERT(len >= 0, ("m_apply, negative len %d", len));
1340123557Sbms	while (off > 0) {
1341123557Sbms		KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1342123557Sbms		if (off < m->m_len)
1343123557Sbms			break;
1344123557Sbms		off -= m->m_len;
1345123557Sbms		m = m->m_next;
1346123557Sbms	}
1347123557Sbms	while (len > 0) {
1348123557Sbms		KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain"));
1349123557Sbms		count = min(m->m_len - off, len);
1350123557Sbms		rval = (*f)(arg, mtod(m, caddr_t) + off, count);
1351123557Sbms		if (rval)
1352123557Sbms			return (rval);
1353123557Sbms		len -= count;
1354123557Sbms		off = 0;
1355123557Sbms		m = m->m_next;
1356123557Sbms	}
1357123557Sbms	return (0);
1358123557Sbms}
1359123557Sbms
1360123557Sbms/*
1361123557Sbms * Return a pointer to mbuf/offset of location in mbuf chain.
1362123557Sbms */
1363123557Sbmsstruct mbuf *
1364123557Sbmsm_getptr(struct mbuf *m, int loc, int *off)
1365123557Sbms{
1366123557Sbms
1367123557Sbms	while (loc >= 0) {
1368123564Sbms		/* Normal end of search. */
1369123557Sbms		if (m->m_len > loc) {
1370123557Sbms			*off = loc;
1371123557Sbms			return (m);
1372123557Sbms		} else {
1373123557Sbms			loc -= m->m_len;
1374123557Sbms			if (m->m_next == NULL) {
1375123557Sbms				if (loc == 0) {
1376123564Sbms					/* Point at the end of valid data. */
1377123557Sbms					*off = m->m_len;
1378123557Sbms					return (m);
1379123564Sbms				}
1380123564Sbms				return (NULL);
1381123564Sbms			}
1382123564Sbms			m = m->m_next;
1383123557Sbms		}
1384123557Sbms	}
1385123557Sbms	return (NULL);
1386123557Sbms}
1387123557Sbms
138852756Sphkvoid
1389135904Sjmgm_print(const struct mbuf *m, int maxlen)
139052756Sphk{
139152756Sphk	int len;
1392135904Sjmg	int pdata;
139354906Seivind	const struct mbuf *m2;
139452756Sphk
1395135904Sjmg	if (m->m_flags & M_PKTHDR)
1396135904Sjmg		len = m->m_pkthdr.len;
1397135904Sjmg	else
1398135904Sjmg		len = -1;
139952756Sphk	m2 = m;
1400135904Sjmg	while (m2 != NULL && (len == -1 || len)) {
1401135904Sjmg		pdata = m2->m_len;
1402135904Sjmg		if (maxlen != -1 && pdata > maxlen)
1403135904Sjmg			pdata = maxlen;
1404135904Sjmg		printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len,
1405135904Sjmg		    m2->m_next, m2->m_flags, "\20\20freelist\17skipfw"
1406135904Sjmg		    "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly"
1407135904Sjmg		    "\3eor\2pkthdr\1ext", pdata ? "" : "\n");
1408135904Sjmg		if (pdata)
1409156700Sjmg			printf(", %*D\n", pdata, (u_char *)m2->m_data, "-");
1410135904Sjmg		if (len != -1)
1411135904Sjmg			len -= m2->m_len;
141252756Sphk		m2 = m2->m_next;
141352756Sphk	}
1414135904Sjmg	if (len > 0)
1415135904Sjmg		printf("%d bytes unaccounted for.\n", len);
141652756Sphk	return;
141752756Sphk}
1418103540Sphk
1419103569Sbmilekicu_int
1420103540Sphkm_fixhdr(struct mbuf *m0)
1421103540Sphk{
1422103569Sbmilekic	u_int len;
1423103540Sphk
1424103544Sphk	len = m_length(m0, NULL);
1425103544Sphk	m0->m_pkthdr.len = len;
1426103544Sphk	return (len);
1427103544Sphk}
1428103544Sphk
1429103569Sbmilekicu_int
1430103544Sphkm_length(struct mbuf *m0, struct mbuf **last)
1431103544Sphk{
1432103544Sphk	struct mbuf *m;
1433103569Sbmilekic	u_int len;
1434103544Sphk
1435103544Sphk	len = 0;
1436103544Sphk	for (m = m0; m != NULL; m = m->m_next) {
1437103540Sphk		len += m->m_len;
1438103544Sphk		if (m->m_next == NULL)
1439103544Sphk			break;
1440103540Sphk	}
1441103544Sphk	if (last != NULL)
1442103544Sphk		*last = m;
1443103544Sphk	return (len);
1444103540Sphk}
1445112777Ssilby
1446112777Ssilby/*
1447112777Ssilby * Defragment a mbuf chain, returning the shortest possible
1448112777Ssilby * chain of mbufs and clusters.  If allocation fails and
1449112777Ssilby * this cannot be completed, NULL will be returned, but
1450112777Ssilby * the passed in chain will be unchanged.  Upon success,
1451112777Ssilby * the original chain will be freed, and the new chain
1452112777Ssilby * will be returned.
1453112777Ssilby *
1454112777Ssilby * If a non-packet header is passed in, the original
1455112777Ssilby * mbuf (chain?) will be returned unharmed.
1456112777Ssilby */
1457112777Ssilbystruct mbuf *
1458112777Ssilbym_defrag(struct mbuf *m0, int how)
1459112777Ssilby{
1460125472Ssilby	struct mbuf *m_new = NULL, *m_final = NULL;
1461125472Ssilby	int progress = 0, length;
1462112777Ssilby
1463132488Salfred	MBUF_CHECKSLEEP(how);
1464112777Ssilby	if (!(m0->m_flags & M_PKTHDR))
1465112777Ssilby		return (m0);
1466112777Ssilby
1467117770Ssilby	m_fixhdr(m0); /* Needed sanity check */
1468117770Ssilby
1469113490Ssilby#ifdef MBUF_STRESS_TEST
1470113490Ssilby	if (m_defragrandomfailures) {
1471113490Ssilby		int temp = arc4random() & 0xff;
1472113490Ssilby		if (temp == 0xba)
1473113490Ssilby			goto nospace;
1474113490Ssilby	}
1475113490Ssilby#endif
1476112777Ssilby
1477112777Ssilby	if (m0->m_pkthdr.len > MHLEN)
1478112777Ssilby		m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1479112777Ssilby	else
1480112777Ssilby		m_final = m_gethdr(how, MT_DATA);
1481112777Ssilby
1482112777Ssilby	if (m_final == NULL)
1483112777Ssilby		goto nospace;
1484112777Ssilby
1485123740Speter	if (m_dup_pkthdr(m_final, m0, how) == 0)
1486112777Ssilby		goto nospace;
1487112777Ssilby
1488112777Ssilby	m_new = m_final;
1489112777Ssilby
1490112777Ssilby	while (progress < m0->m_pkthdr.len) {
1491112777Ssilby		length = m0->m_pkthdr.len - progress;
1492112777Ssilby		if (length > MCLBYTES)
1493112777Ssilby			length = MCLBYTES;
1494112777Ssilby
1495112777Ssilby		if (m_new == NULL) {
1496112777Ssilby			if (length > MLEN)
1497112777Ssilby				m_new = m_getcl(how, MT_DATA, 0);
1498112777Ssilby			else
1499112777Ssilby				m_new = m_get(how, MT_DATA);
1500112777Ssilby			if (m_new == NULL)
1501112777Ssilby				goto nospace;
1502112777Ssilby		}
1503112777Ssilby
1504112777Ssilby		m_copydata(m0, progress, length, mtod(m_new, caddr_t));
1505112777Ssilby		progress += length;
1506112777Ssilby		m_new->m_len = length;
1507112777Ssilby		if (m_new != m_final)
1508112777Ssilby			m_cat(m_final, m_new);
1509112777Ssilby		m_new = NULL;
1510112777Ssilby	}
1511116455Ssilby#ifdef MBUF_STRESS_TEST
1512112777Ssilby	if (m0->m_next == NULL)
1513112777Ssilby		m_defraguseless++;
1514116455Ssilby#endif
1515112777Ssilby	m_freem(m0);
1516112777Ssilby	m0 = m_final;
1517116455Ssilby#ifdef MBUF_STRESS_TEST
1518112777Ssilby	m_defragpackets++;
1519112777Ssilby	m_defragbytes += m0->m_pkthdr.len;
1520116455Ssilby#endif
1521112777Ssilby	return (m0);
1522112777Ssilbynospace:
1523116455Ssilby#ifdef MBUF_STRESS_TEST
1524112777Ssilby	m_defragfailure++;
1525116455Ssilby#endif
1526112777Ssilby	if (m_final)
1527112777Ssilby		m_freem(m_final);
1528112777Ssilby	return (NULL);
1529112777Ssilby}
1530119644Ssilby
1531119644Ssilby#ifdef MBUF_STRESS_TEST
1532119644Ssilby
1533119644Ssilby/*
1534119644Ssilby * Fragment an mbuf chain.  There's no reason you'd ever want to do
1535119644Ssilby * this in normal usage, but it's great for stress testing various
1536119644Ssilby * mbuf consumers.
1537119644Ssilby *
1538119644Ssilby * If fragmentation is not possible, the original chain will be
1539119644Ssilby * returned.
1540119644Ssilby *
1541119644Ssilby * Possible length values:
1542119644Ssilby * 0	 no fragmentation will occur
1543119644Ssilby * > 0	each fragment will be of the specified length
1544119644Ssilby * -1	each fragment will be the same random value in length
1545119644Ssilby * -2	each fragment's length will be entirely random
1546119644Ssilby * (Random values range from 1 to 256)
1547119644Ssilby */
1548119644Ssilbystruct mbuf *
1549119644Ssilbym_fragment(struct mbuf *m0, int how, int length)
1550119644Ssilby{
1551125472Ssilby	struct mbuf *m_new = NULL, *m_final = NULL;
1552125472Ssilby	int progress = 0;
1553119644Ssilby
1554119644Ssilby	if (!(m0->m_flags & M_PKTHDR))
1555119644Ssilby		return (m0);
1556119644Ssilby
1557119644Ssilby	if ((length == 0) || (length < -2))
1558119644Ssilby		return (m0);
1559119644Ssilby
1560119644Ssilby	m_fixhdr(m0); /* Needed sanity check */
1561119644Ssilby
1562119644Ssilby	m_final = m_getcl(how, MT_DATA, M_PKTHDR);
1563119644Ssilby
1564119644Ssilby	if (m_final == NULL)
1565119644Ssilby		goto nospace;
1566119644Ssilby
1567123823Ssilby	if (m_dup_pkthdr(m_final, m0, how) == 0)
1568119644Ssilby		goto nospace;
1569119644Ssilby
1570119644Ssilby	m_new = m_final;
1571119644Ssilby
1572119644Ssilby	if (length == -1)
1573119644Ssilby		length = 1 + (arc4random() & 255);
1574119644Ssilby
1575119644Ssilby	while (progress < m0->m_pkthdr.len) {
1576119644Ssilby		int fraglen;
1577119644Ssilby
1578119644Ssilby		if (length > 0)
1579119644Ssilby			fraglen = length;
1580119644Ssilby		else
1581119644Ssilby			fraglen = 1 + (arc4random() & 255);
1582119644Ssilby		if (fraglen > m0->m_pkthdr.len - progress)
1583119644Ssilby			fraglen = m0->m_pkthdr.len - progress;
1584119644Ssilby
1585119644Ssilby		if (fraglen > MCLBYTES)
1586119644Ssilby			fraglen = MCLBYTES;
1587119644Ssilby
1588119644Ssilby		if (m_new == NULL) {
1589119644Ssilby			m_new = m_getcl(how, MT_DATA, 0);
1590119644Ssilby			if (m_new == NULL)
1591119644Ssilby				goto nospace;
1592119644Ssilby		}
1593119644Ssilby
1594119644Ssilby		m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t));
1595119644Ssilby		progress += fraglen;
1596119644Ssilby		m_new->m_len = fraglen;
1597119644Ssilby		if (m_new != m_final)
1598119644Ssilby			m_cat(m_final, m_new);
1599119644Ssilby		m_new = NULL;
1600119644Ssilby	}
1601119644Ssilby	m_freem(m0);
1602119644Ssilby	m0 = m_final;
1603119644Ssilby	return (m0);
1604119644Ssilbynospace:
1605119644Ssilby	if (m_final)
1606119644Ssilby		m_freem(m_final);
1607119644Ssilby	/* Return the original chain on failure */
1608119644Ssilby	return (m0);
1609119644Ssilby}
1610119644Ssilby
1611119644Ssilby#endif
1612125296Ssilby
1613163915Sandre/*
1614163915Sandre * Copy the contents of uio into a properly sized mbuf chain.
1615163915Sandre */
1616125296Ssilbystruct mbuf *
1617163915Sandrem_uiotombuf(struct uio *uio, int how, int len, int align, int flags)
1618125296Ssilby{
1619163915Sandre	struct mbuf *m, *mb;
1620163915Sandre	int error, length, total;
1621163915Sandre	int progress = 0;
1622125296Ssilby
1623163915Sandre	/*
1624163915Sandre	 * len can be zero or an arbitrary large value bound by
1625163915Sandre	 * the total data supplied by the uio.
1626163915Sandre	 */
1627125296Ssilby	if (len > 0)
1628125296Ssilby		total = min(uio->uio_resid, len);
1629125296Ssilby	else
1630125296Ssilby		total = uio->uio_resid;
1631163915Sandre
1632163915Sandre	/*
1633163915Sandre	 * The smallest unit returned by m_getm2() is a single mbuf
1634163915Sandre	 * with pkthdr.  We can't align past it.  Align align itself.
1635163915Sandre	 */
1636163915Sandre	if (align)
1637163915Sandre		align &= ~(sizeof(long) - 1);
1638145883Semax	if (align >= MHLEN)
1639163915Sandre		return (NULL);
1640163915Sandre
1641166171Sandre	/*
1642166171Sandre	 * Give us the full allocation or nothing.
1643166171Sandre	 * If len is zero return the smallest empty mbuf.
1644166171Sandre	 */
1645166171Sandre	m = m_getm2(NULL, max(total + align, 1), how, MT_DATA, flags);
1646163915Sandre	if (m == NULL)
1647163915Sandre		return (NULL);
1648163915Sandre	m->m_data += align;
1649163915Sandre
1650163915Sandre	/* Fill all mbufs with uio data and update header information. */
1651163915Sandre	for (mb = m; mb != NULL; mb = mb->m_next) {
1652163915Sandre		length = min(M_TRAILINGSPACE(mb), total - progress);
1653163915Sandre
1654163915Sandre		error = uiomove(mtod(mb, void *), length, uio);
1655163915Sandre		if (error) {
1656163915Sandre			m_freem(m);
1657163915Sandre			return (NULL);
1658125296Ssilby		}
1659163915Sandre
1660163915Sandre		mb->m_len = length;
1661125296Ssilby		progress += length;
1662163915Sandre		if (flags & M_PKTHDR)
1663163915Sandre			m->m_pkthdr.len += length;
1664125296Ssilby	}
1665163915Sandre	KASSERT(progress == total, ("%s: progress != total", __func__));
1666163915Sandre
1667163915Sandre	return (m);
1668125296Ssilby}
1669148552Ssam
1670148552Ssam/*
1671148552Ssam * Set the m_data pointer of a newly-allocated mbuf
1672148552Ssam * to place an object of the specified size at the
1673148552Ssam * end of the mbuf, longword aligned.
1674148552Ssam */
1675148552Ssamvoid
1676148552Ssamm_align(struct mbuf *m, int len)
1677148552Ssam{
1678148552Ssam	int adjust;
1679148552Ssam
1680148552Ssam	if (m->m_flags & M_EXT)
1681148552Ssam		adjust = m->m_ext.ext_size - len;
1682148552Ssam	else if (m->m_flags & M_PKTHDR)
1683148552Ssam		adjust = MHLEN - len;
1684148552Ssam	else
1685148552Ssam		adjust = MLEN - len;
1686148552Ssam	m->m_data += adjust &~ (sizeof(long)-1);
1687148552Ssam}
1688156756Ssam
1689156756Ssam/*
1690156756Ssam * Create a writable copy of the mbuf chain.  While doing this
1691156756Ssam * we compact the chain with a goal of producing a chain with
1692156756Ssam * at most two mbufs.  The second mbuf in this chain is likely
1693156756Ssam * to be a cluster.  The primary purpose of this work is to create
1694156756Ssam * a writable packet for encryption, compression, etc.  The
1695156756Ssam * secondary goal is to linearize the data so the data can be
1696156756Ssam * passed to crypto hardware in the most efficient manner possible.
1697156756Ssam */
1698156756Ssamstruct mbuf *
1699156756Ssamm_unshare(struct mbuf *m0, int how)
1700156756Ssam{
1701156756Ssam	struct mbuf *m, *mprev;
1702156756Ssam	struct mbuf *n, *mfirst, *mlast;
1703156756Ssam	int len, off;
1704156756Ssam
1705156756Ssam	mprev = NULL;
1706156756Ssam	for (m = m0; m != NULL; m = mprev->m_next) {
1707156756Ssam		/*
1708156756Ssam		 * Regular mbufs are ignored unless there's a cluster
1709156756Ssam		 * in front of it that we can use to coalesce.  We do
1710156756Ssam		 * the latter mainly so later clusters can be coalesced
1711156756Ssam		 * also w/o having to handle them specially (i.e. convert
1712156756Ssam		 * mbuf+cluster -> cluster).  This optimization is heavily
1713156756Ssam		 * influenced by the assumption that we're running over
1714156756Ssam		 * Ethernet where MCLBYTES is large enough that the max
1715156756Ssam		 * packet size will permit lots of coalescing into a
1716156756Ssam		 * single cluster.  This in turn permits efficient
1717156756Ssam		 * crypto operations, especially when using hardware.
1718156756Ssam		 */
1719156756Ssam		if ((m->m_flags & M_EXT) == 0) {
1720156756Ssam			if (mprev && (mprev->m_flags & M_EXT) &&
1721156756Ssam			    m->m_len <= M_TRAILINGSPACE(mprev)) {
1722156756Ssam				/* XXX: this ignores mbuf types */
1723156756Ssam				memcpy(mtod(mprev, caddr_t) + mprev->m_len,
1724156756Ssam				       mtod(m, caddr_t), m->m_len);
1725156756Ssam				mprev->m_len += m->m_len;
1726156756Ssam				mprev->m_next = m->m_next;	/* unlink from chain */
1727156756Ssam				m_free(m);			/* reclaim mbuf */
1728156756Ssam#if 0
1729156756Ssam				newipsecstat.ips_mbcoalesced++;
1730156756Ssam#endif
1731156756Ssam			} else {
1732156756Ssam				mprev = m;
1733156756Ssam			}
1734156756Ssam			continue;
1735156756Ssam		}
1736156756Ssam		/*
1737156756Ssam		 * Writable mbufs are left alone (for now).
1738156756Ssam		 */
1739156756Ssam		if (M_WRITABLE(m)) {
1740156756Ssam			mprev = m;
1741156756Ssam			continue;
1742156756Ssam		}
1743156756Ssam
1744156756Ssam		/*
1745156756Ssam		 * Not writable, replace with a copy or coalesce with
1746156756Ssam		 * the previous mbuf if possible (since we have to copy
1747156756Ssam		 * it anyway, we try to reduce the number of mbufs and
1748156756Ssam		 * clusters so that future work is easier).
1749156756Ssam		 */
1750156756Ssam		KASSERT(m->m_flags & M_EXT, ("m_flags 0x%x", m->m_flags));
1751156756Ssam		/* NB: we only coalesce into a cluster or larger */
1752156756Ssam		if (mprev != NULL && (mprev->m_flags & M_EXT) &&
1753156756Ssam		    m->m_len <= M_TRAILINGSPACE(mprev)) {
1754156756Ssam			/* XXX: this ignores mbuf types */
1755156756Ssam			memcpy(mtod(mprev, caddr_t) + mprev->m_len,
1756156756Ssam			       mtod(m, caddr_t), m->m_len);
1757156756Ssam			mprev->m_len += m->m_len;
1758156756Ssam			mprev->m_next = m->m_next;	/* unlink from chain */
1759156756Ssam			m_free(m);			/* reclaim mbuf */
1760156756Ssam#if 0
1761156756Ssam			newipsecstat.ips_clcoalesced++;
1762156756Ssam#endif
1763156756Ssam			continue;
1764156756Ssam		}
1765156756Ssam
1766156756Ssam		/*
1767156756Ssam		 * Allocate new space to hold the copy...
1768156756Ssam		 */
1769156756Ssam		/* XXX why can M_PKTHDR be set past the first mbuf? */
1770156756Ssam		if (mprev == NULL && (m->m_flags & M_PKTHDR)) {
1771156756Ssam			/*
1772156756Ssam			 * NB: if a packet header is present we must
1773156756Ssam			 * allocate the mbuf separately from any cluster
1774156756Ssam			 * because M_MOVE_PKTHDR will smash the data
1775156756Ssam			 * pointer and drop the M_EXT marker.
1776156756Ssam			 */
1777156756Ssam			MGETHDR(n, how, m->m_type);
1778156756Ssam			if (n == NULL) {
1779156756Ssam				m_freem(m0);
1780156756Ssam				return (NULL);
1781156756Ssam			}
1782156756Ssam			M_MOVE_PKTHDR(n, m);
1783156756Ssam			MCLGET(n, how);
1784156756Ssam			if ((n->m_flags & M_EXT) == 0) {
1785156756Ssam				m_free(n);
1786156756Ssam				m_freem(m0);
1787156756Ssam				return (NULL);
1788156756Ssam			}
1789156756Ssam		} else {
1790156756Ssam			n = m_getcl(how, m->m_type, m->m_flags);
1791156756Ssam			if (n == NULL) {
1792156756Ssam				m_freem(m0);
1793156756Ssam				return (NULL);
1794156756Ssam			}
1795156756Ssam		}
1796156756Ssam		/*
1797156756Ssam		 * ... and copy the data.  We deal with jumbo mbufs
1798156756Ssam		 * (i.e. m_len > MCLBYTES) by splitting them into
1799156756Ssam		 * clusters.  We could just malloc a buffer and make
1800156756Ssam		 * it external but too many device drivers don't know
1801156756Ssam		 * how to break up the non-contiguous memory when
1802156756Ssam		 * doing DMA.
1803156756Ssam		 */
1804156756Ssam		len = m->m_len;
1805156756Ssam		off = 0;
1806156756Ssam		mfirst = n;
1807156756Ssam		mlast = NULL;
1808156756Ssam		for (;;) {
1809156756Ssam			int cc = min(len, MCLBYTES);
1810156756Ssam			memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off, cc);
1811156756Ssam			n->m_len = cc;
1812156756Ssam			if (mlast != NULL)
1813156756Ssam				mlast->m_next = n;
1814156756Ssam			mlast = n;
1815156756Ssam#if 0
1816156756Ssam			newipsecstat.ips_clcopied++;
1817156756Ssam#endif
1818156756Ssam
1819156756Ssam			len -= cc;
1820156756Ssam			if (len <= 0)
1821156756Ssam				break;
1822156756Ssam			off += cc;
1823156756Ssam
1824156756Ssam			n = m_getcl(how, m->m_type, m->m_flags);
1825156756Ssam			if (n == NULL) {
1826156756Ssam				m_freem(mfirst);
1827156756Ssam				m_freem(m0);
1828156756Ssam				return (NULL);
1829156756Ssam			}
1830156756Ssam		}
1831156756Ssam		n->m_next = m->m_next;
1832156756Ssam		if (mprev == NULL)
1833156756Ssam			m0 = mfirst;		/* new head of chain */
1834156756Ssam		else
1835156756Ssam			mprev->m_next = mfirst;	/* replace old mbuf */
1836156756Ssam		m_free(m);			/* release old mbuf */
1837156756Ssam		mprev = mfirst;
1838156756Ssam	}
1839156756Ssam	return (m0);
1840156756Ssam}
1841