uipc_mbuf.c revision 112185
1/*
2 * Copyright (c) 1982, 1986, 1988, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)uipc_mbuf.c	8.2 (Berkeley) 1/4/94
34 * $FreeBSD: head/sys/kern/uipc_mbuf.c 112185 2003-03-13 09:02:19Z iedowse $
35 */
36
37#include "opt_mac.h"
38#include "opt_param.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/lock.h>
44#include <sys/mac.h>
45#include <sys/malloc.h>
46#include <sys/mbuf.h>
47#include <sys/sysctl.h>
48#include <sys/domain.h>
49#include <sys/protosw.h>
50
51int	max_linkhdr;
52int	max_protohdr;
53int	max_hdr;
54int	max_datalen;
55
56/*
57 * sysctl(8) exported objects
58 */
59SYSCTL_DECL(_kern_ipc);
60SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW,
61	   &max_linkhdr, 0, "");
62SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW,
63	   &max_protohdr, 0, "");
64SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, "");
65SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW,
66	   &max_datalen, 0, "");
67
68/*
69 * "Move" mbuf pkthdr from "from" to "to".
70 * "from" must have M_PKTHDR set, and "to" must be empty.
71 */
72void
73m_move_pkthdr(struct mbuf *to, struct mbuf *from)
74{
75
76#if 0
77	/* see below for why these are not enabled */
78	KASSERT(to->m_flags & M_PKTHDR,
79	    ("m_move_pkthdr: called on non-header"));
80	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags),
81	    ("m_move_pkthdr: to has tags"));
82#endif
83	KASSERT((to->m_flags & M_EXT) == 0, ("m_move_pkthdr: to has cluster"));
84#ifdef MAC
85	if (to->m_flags & M_PKTHDR)
86		mac_destroy_mbuf(to);
87#endif
88	to->m_flags = from->m_flags & M_COPYFLAGS;
89	to->m_data = to->m_pktdat;
90	to->m_pkthdr = from->m_pkthdr;		/* especially tags */
91#ifdef MAC
92	mac_init_mbuf(to, 1);			/* XXXMAC no way to fail */
93	mac_create_mbuf_from_mbuf(from, to);
94#endif
95	SLIST_INIT(&from->m_pkthdr.tags);	/* purge tags from src */
96	from->m_flags &= ~M_PKTHDR;
97}
98
99/*
100 * Duplicate "from"'s mbuf pkthdr in "to".
101 * "from" must have M_PKTHDR set, and "to" must be empty.
102 * In particular, this does a deep copy of the packet tags.
103 */
104int
105m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how)
106{
107
108#if 0
109	/*
110	 * The mbuf allocator only initializes the pkthdr
111	 * when the mbuf is allocated with MGETHDR. Many users
112	 * (e.g. m_copy*, m_prepend) use MGET and then
113	 * smash the pkthdr as needed causing these
114	 * assertions to trip.  For now just disable them.
115	 */
116	KASSERT(to->m_flags & M_PKTHDR, ("m_dup_pkthdr: called on non-header"));
117	KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags"));
118#endif
119	KASSERT((to->m_flags & M_EXT) == 0, ("m_dup_pkthdr: to has cluster"));
120#ifdef MAC
121	if (to->m_flags & M_PKTHDR)
122		mac_destroy_mbuf(to);
123#endif
124	to->m_flags = from->m_flags & M_COPYFLAGS;
125	to->m_data = to->m_pktdat;
126	to->m_pkthdr = from->m_pkthdr;
127#ifdef MAC
128	mac_init_mbuf(to, 1);			/* XXXMAC no way to fail */
129	mac_create_mbuf_from_mbuf(from, to);
130#endif
131	SLIST_INIT(&to->m_pkthdr.tags);
132	return (m_tag_copy_chain(to, from, (how & M_TRYWAIT) ? M_WAITOK :
133	    M_NOWAIT));
134}
135
136/*
137 * Lesser-used path for M_PREPEND:
138 * allocate new mbuf to prepend to chain,
139 * copy junk along.
140 */
141struct mbuf *
142m_prepend(struct mbuf *m, int len, int how)
143{
144	struct mbuf *mn;
145
146	MGET(mn, how, m->m_type);
147	if (mn == NULL) {
148		m_freem(m);
149		return (NULL);
150	}
151	if (m->m_flags & M_PKTHDR) {
152		M_MOVE_PKTHDR(mn, m);
153#ifdef MAC
154		mac_destroy_mbuf(m);
155#endif
156	}
157	mn->m_next = m;
158	m = mn;
159	if (len < MHLEN)
160		MH_ALIGN(m, len);
161	m->m_len = len;
162	return (m);
163}
164
165/*
166 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
167 * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
168 * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller.
169 * Note that the copy is read-only, because clusters are not copied,
170 * only their reference counts are incremented.
171 */
172struct mbuf *
173m_copym(struct mbuf *m, int off0, int len, int wait)
174{
175	struct mbuf *n, **np;
176	int off = off0;
177	struct mbuf *top;
178	int copyhdr = 0;
179
180	KASSERT(off >= 0, ("m_copym, negative off %d", off));
181	KASSERT(len >= 0, ("m_copym, negative len %d", len));
182	if (off == 0 && m->m_flags & M_PKTHDR)
183		copyhdr = 1;
184	while (off > 0) {
185		KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain"));
186		if (off < m->m_len)
187			break;
188		off -= m->m_len;
189		m = m->m_next;
190	}
191	np = &top;
192	top = 0;
193	while (len > 0) {
194		if (m == NULL) {
195			KASSERT(len == M_COPYALL,
196			    ("m_copym, length > size of mbuf chain"));
197			break;
198		}
199		MGET(n, wait, m->m_type);
200		*np = n;
201		if (n == NULL)
202			goto nospace;
203		if (copyhdr) {
204			if (!m_dup_pkthdr(n, m, wait))
205				goto nospace;
206			if (len == M_COPYALL)
207				n->m_pkthdr.len -= off0;
208			else
209				n->m_pkthdr.len = len;
210			copyhdr = 0;
211		}
212		n->m_len = min(len, m->m_len - off);
213		if (m->m_flags & M_EXT) {
214			n->m_data = m->m_data + off;
215			n->m_ext = m->m_ext;
216			n->m_flags |= M_EXT;
217			MEXT_ADD_REF(m);
218		} else
219			bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t),
220			    (u_int)n->m_len);
221		if (len != M_COPYALL)
222			len -= n->m_len;
223		off = 0;
224		m = m->m_next;
225		np = &n->m_next;
226	}
227	if (top == NULL)
228		mbstat.m_mcfail++;	/* XXX: No consistency. */
229
230	return (top);
231nospace:
232	m_freem(top);
233	mbstat.m_mcfail++;	/* XXX: No consistency. */
234	return (NULL);
235}
236
237/*
238 * Copy an entire packet, including header (which must be present).
239 * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
240 * Note that the copy is read-only, because clusters are not copied,
241 * only their reference counts are incremented.
242 * Preserve alignment of the first mbuf so if the creator has left
243 * some room at the beginning (e.g. for inserting protocol headers)
244 * the copies still have the room available.
245 */
246struct mbuf *
247m_copypacket(struct mbuf *m, int how)
248{
249	struct mbuf *top, *n, *o;
250
251	MGET(n, how, m->m_type);
252	top = n;
253	if (n == NULL)
254		goto nospace;
255
256	if (!m_dup_pkthdr(n, m, how))
257		goto nospace;
258	n->m_len = m->m_len;
259	if (m->m_flags & M_EXT) {
260		n->m_data = m->m_data;
261		n->m_ext = m->m_ext;
262		n->m_flags |= M_EXT;
263		MEXT_ADD_REF(m);
264	} else {
265		n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat );
266		bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
267	}
268
269	m = m->m_next;
270	while (m) {
271		MGET(o, how, m->m_type);
272		if (o == NULL)
273			goto nospace;
274
275		n->m_next = o;
276		n = n->m_next;
277
278		n->m_len = m->m_len;
279		if (m->m_flags & M_EXT) {
280			n->m_data = m->m_data;
281			n->m_ext = m->m_ext;
282			n->m_flags |= M_EXT;
283			MEXT_ADD_REF(m);
284		} else {
285			bcopy(mtod(m, char *), mtod(n, char *), n->m_len);
286		}
287
288		m = m->m_next;
289	}
290	return top;
291nospace:
292	m_freem(top);
293	mbstat.m_mcfail++;	/* XXX: No consistency. */
294	return (NULL);
295}
296
297/*
298 * Copy data from an mbuf chain starting "off" bytes from the beginning,
299 * continuing for "len" bytes, into the indicated buffer.
300 */
301void
302m_copydata(const struct mbuf *m, int off, int len, caddr_t cp)
303{
304	u_int count;
305
306	KASSERT(off >= 0, ("m_copydata, negative off %d", off));
307	KASSERT(len >= 0, ("m_copydata, negative len %d", len));
308	while (off > 0) {
309		KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain"));
310		if (off < m->m_len)
311			break;
312		off -= m->m_len;
313		m = m->m_next;
314	}
315	while (len > 0) {
316		KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain"));
317		count = min(m->m_len - off, len);
318		bcopy(mtod(m, caddr_t) + off, cp, count);
319		len -= count;
320		cp += count;
321		off = 0;
322		m = m->m_next;
323	}
324}
325
326/*
327 * Copy a packet header mbuf chain into a completely new chain, including
328 * copying any mbuf clusters.  Use this instead of m_copypacket() when
329 * you need a writable copy of an mbuf chain.
330 */
331struct mbuf *
332m_dup(struct mbuf *m, int how)
333{
334	struct mbuf **p, *top = NULL;
335	int remain, moff, nsize;
336
337	/* Sanity check */
338	if (m == NULL)
339		return (NULL);
340	KASSERT((m->m_flags & M_PKTHDR) != 0, ("%s: !PKTHDR", __func__));
341
342	/* While there's more data, get a new mbuf, tack it on, and fill it */
343	remain = m->m_pkthdr.len;
344	moff = 0;
345	p = &top;
346	while (remain > 0 || top == NULL) {	/* allow m->m_pkthdr.len == 0 */
347		struct mbuf *n;
348
349		/* Get the next new mbuf */
350		MGET(n, how, m->m_type);
351		if (n == NULL)
352			goto nospace;
353		if (top == NULL) {		/* first one, must be PKTHDR */
354			if (!m_dup_pkthdr(n, m, how))
355				goto nospace;
356			nsize = MHLEN;
357		} else				/* not the first one */
358			nsize = MLEN;
359		if (remain >= MINCLSIZE) {
360			MCLGET(n, how);
361			if ((n->m_flags & M_EXT) == 0) {
362				(void)m_free(n);
363				goto nospace;
364			}
365			nsize = MCLBYTES;
366		}
367		n->m_len = 0;
368
369		/* Link it into the new chain */
370		*p = n;
371		p = &n->m_next;
372
373		/* Copy data from original mbuf(s) into new mbuf */
374		while (n->m_len < nsize && m != NULL) {
375			int chunk = min(nsize - n->m_len, m->m_len - moff);
376
377			bcopy(m->m_data + moff, n->m_data + n->m_len, chunk);
378			moff += chunk;
379			n->m_len += chunk;
380			remain -= chunk;
381			if (moff == m->m_len) {
382				m = m->m_next;
383				moff = 0;
384			}
385		}
386
387		/* Check correct total mbuf length */
388		KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL),
389		    	("%s: bogus m_pkthdr.len", __func__));
390	}
391	return (top);
392
393nospace:
394	m_freem(top);
395	mbstat.m_mcfail++;	/* XXX: No consistency. */
396	return (NULL);
397}
398
399/*
400 * Concatenate mbuf chain n to m.
401 * Both chains must be of the same type (e.g. MT_DATA).
402 * Any m_pkthdr is not updated.
403 */
404void
405m_cat(struct mbuf *m, struct mbuf *n)
406{
407	while (m->m_next)
408		m = m->m_next;
409	while (n) {
410		if (m->m_flags & M_EXT ||
411		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
412			/* just join the two chains */
413			m->m_next = n;
414			return;
415		}
416		/* splat the data from one into the other */
417		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
418		    (u_int)n->m_len);
419		m->m_len += n->m_len;
420		n = m_free(n);
421	}
422}
423
424void
425m_adj(struct mbuf *mp, int req_len)
426{
427	int len = req_len;
428	struct mbuf *m;
429	int count;
430
431	if ((m = mp) == NULL)
432		return;
433	if (len >= 0) {
434		/*
435		 * Trim from head.
436		 */
437		while (m != NULL && len > 0) {
438			if (m->m_len <= len) {
439				len -= m->m_len;
440				m->m_len = 0;
441				m = m->m_next;
442			} else {
443				m->m_len -= len;
444				m->m_data += len;
445				len = 0;
446			}
447		}
448		m = mp;
449		if (mp->m_flags & M_PKTHDR)
450			m->m_pkthdr.len -= (req_len - len);
451	} else {
452		/*
453		 * Trim from tail.  Scan the mbuf chain,
454		 * calculating its length and finding the last mbuf.
455		 * If the adjustment only affects this mbuf, then just
456		 * adjust and return.  Otherwise, rescan and truncate
457		 * after the remaining size.
458		 */
459		len = -len;
460		count = 0;
461		for (;;) {
462			count += m->m_len;
463			if (m->m_next == (struct mbuf *)0)
464				break;
465			m = m->m_next;
466		}
467		if (m->m_len >= len) {
468			m->m_len -= len;
469			if (mp->m_flags & M_PKTHDR)
470				mp->m_pkthdr.len -= len;
471			return;
472		}
473		count -= len;
474		if (count < 0)
475			count = 0;
476		/*
477		 * Correct length for chain is "count".
478		 * Find the mbuf with last data, adjust its length,
479		 * and toss data from remaining mbufs on chain.
480		 */
481		m = mp;
482		if (m->m_flags & M_PKTHDR)
483			m->m_pkthdr.len = count;
484		for (; m; m = m->m_next) {
485			if (m->m_len >= count) {
486				m->m_len = count;
487				break;
488			}
489			count -= m->m_len;
490		}
491		while (m->m_next)
492			(m = m->m_next) ->m_len = 0;
493	}
494}
495
496/*
497 * Rearange an mbuf chain so that len bytes are contiguous
498 * and in the data area of an mbuf (so that mtod and dtom
499 * will work for a structure of size len).  Returns the resulting
500 * mbuf chain on success, frees it and returns null on failure.
501 * If there is room, it will add up to max_protohdr-len extra bytes to the
502 * contiguous region in an attempt to avoid being called next time.
503 */
504struct mbuf *
505m_pullup(struct mbuf *n, int len)
506{
507	struct mbuf *m;
508	int count;
509	int space;
510
511	/*
512	 * If first mbuf has no cluster, and has room for len bytes
513	 * without shifting current data, pullup into it,
514	 * otherwise allocate a new mbuf to prepend to the chain.
515	 */
516	if ((n->m_flags & M_EXT) == 0 &&
517	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
518		if (n->m_len >= len)
519			return (n);
520		m = n;
521		n = n->m_next;
522		len -= m->m_len;
523	} else {
524		if (len > MHLEN)
525			goto bad;
526		MGET(m, M_DONTWAIT, n->m_type);
527		if (m == NULL)
528			goto bad;
529		m->m_len = 0;
530		if (n->m_flags & M_PKTHDR)
531			M_MOVE_PKTHDR(m, n);
532	}
533	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
534	do {
535		count = min(min(max(len, max_protohdr), space), n->m_len);
536		bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len,
537		  (u_int)count);
538		len -= count;
539		m->m_len += count;
540		n->m_len -= count;
541		space -= count;
542		if (n->m_len)
543			n->m_data += count;
544		else
545			n = m_free(n);
546	} while (len > 0 && n);
547	if (len > 0) {
548		(void) m_free(m);
549		goto bad;
550	}
551	m->m_next = n;
552	return (m);
553bad:
554	m_freem(n);
555	mbstat.m_mpfail++;	/* XXX: No consistency. */
556	return (NULL);
557}
558
559/*
560 * Partition an mbuf chain in two pieces, returning the tail --
561 * all but the first len0 bytes.  In case of failure, it returns NULL and
562 * attempts to restore the chain to its original state.
563 *
564 * Note that the resulting mbufs might be read-only, because the new
565 * mbuf can end up sharing an mbuf cluster with the original mbuf if
566 * the "breaking point" happens to lie within a cluster mbuf. Use the
567 * M_WRITABLE() macro to check for this case.
568 */
569struct mbuf *
570m_split(struct mbuf *m0, int len0, int wait)
571{
572	struct mbuf *m, *n;
573	u_int len = len0, remain;
574
575	for (m = m0; m && len > m->m_len; m = m->m_next)
576		len -= m->m_len;
577	if (m == NULL)
578		return (NULL);
579	remain = m->m_len - len;
580	if (m0->m_flags & M_PKTHDR) {
581		MGETHDR(n, wait, m0->m_type);
582		if (n == NULL)
583			return (NULL);
584		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
585		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
586		m0->m_pkthdr.len = len0;
587		if (m->m_flags & M_EXT)
588			goto extpacket;
589		if (remain > MHLEN) {
590			/* m can't be the lead packet */
591			MH_ALIGN(n, 0);
592			n->m_next = m_split(m, len, wait);
593			if (n->m_next == NULL) {
594				(void) m_free(n);
595				return (NULL);
596			} else {
597				n->m_len = 0;
598				return (n);
599			}
600		} else
601			MH_ALIGN(n, remain);
602	} else if (remain == 0) {
603		n = m->m_next;
604		m->m_next = NULL;
605		return (n);
606	} else {
607		MGET(n, wait, m->m_type);
608		if (n == NULL)
609			return (NULL);
610		M_ALIGN(n, remain);
611	}
612extpacket:
613	if (m->m_flags & M_EXT) {
614		n->m_flags |= M_EXT;
615		n->m_ext = m->m_ext;
616		MEXT_ADD_REF(m);
617		n->m_data = m->m_data + len;
618	} else {
619		bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain);
620	}
621	n->m_len = remain;
622	m->m_len = len;
623	n->m_next = m->m_next;
624	m->m_next = NULL;
625	return (n);
626}
627/*
628 * Routine to copy from device local memory into mbufs.
629 * Note that `off' argument is offset into first mbuf of target chain from
630 * which to begin copying the data to.
631 */
632struct mbuf *
633m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
634	 void (*copy)(char *from, caddr_t to, u_int len))
635{
636	struct mbuf *m;
637	struct mbuf *top = 0, **mp = &top;
638	int len;
639
640	if (off < 0 || off > MHLEN)
641		return (NULL);
642
643	MGETHDR(m, M_DONTWAIT, MT_DATA);
644	if (m == NULL)
645		return (NULL);
646	m->m_pkthdr.rcvif = ifp;
647	m->m_pkthdr.len = totlen;
648	len = MHLEN;
649
650	while (totlen > 0) {
651		if (top) {
652			MGET(m, M_DONTWAIT, MT_DATA);
653			if (m == NULL) {
654				m_freem(top);
655				return (NULL);
656			}
657			len = MLEN;
658		}
659		if (totlen + off >= MINCLSIZE) {
660			MCLGET(m, M_DONTWAIT);
661			if (m->m_flags & M_EXT)
662				len = MCLBYTES;
663		} else {
664			/*
665			 * Place initial small packet/header at end of mbuf.
666			 */
667			if (top == NULL && totlen + off + max_linkhdr <= len) {
668				m->m_data += max_linkhdr;
669				len -= max_linkhdr;
670			}
671		}
672		if (off) {
673			m->m_data += off;
674			len -= off;
675			off = 0;
676		}
677		m->m_len = len = min(totlen, len);
678		if (copy)
679			copy(buf, mtod(m, caddr_t), (u_int)len);
680		else
681			bcopy(buf, mtod(m, caddr_t), (u_int)len);
682		buf += len;
683		*mp = m;
684		mp = &m->m_next;
685		totlen -= len;
686	}
687	return (top);
688}
689
690/*
691 * Copy data from a buffer back into the indicated mbuf chain,
692 * starting "off" bytes from the beginning, extending the mbuf
693 * chain if necessary.
694 */
695void
696m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
697{
698	int mlen;
699	struct mbuf *m = m0, *n;
700	int totlen = 0;
701
702	if (m0 == NULL)
703		return;
704	while (off > (mlen = m->m_len)) {
705		off -= mlen;
706		totlen += mlen;
707		if (m->m_next == NULL) {
708			n = m_get_clrd(M_DONTWAIT, m->m_type);
709			if (n == NULL)
710				goto out;
711			n->m_len = min(MLEN, len + off);
712			m->m_next = n;
713		}
714		m = m->m_next;
715	}
716	while (len > 0) {
717		mlen = min (m->m_len - off, len);
718		bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen);
719		cp += mlen;
720		len -= mlen;
721		mlen += off;
722		off = 0;
723		totlen += mlen;
724		if (len == 0)
725			break;
726		if (m->m_next == NULL) {
727			n = m_get(M_DONTWAIT, m->m_type);
728			if (n == NULL)
729				break;
730			n->m_len = min(MLEN, len);
731			m->m_next = n;
732		}
733		m = m->m_next;
734	}
735out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
736		m->m_pkthdr.len = totlen;
737}
738
739void
740m_print(const struct mbuf *m)
741{
742	int len;
743	const struct mbuf *m2;
744
745	len = m->m_pkthdr.len;
746	m2 = m;
747	while (len) {
748		printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-");
749		len -= m2->m_len;
750		m2 = m2->m_next;
751	}
752	return;
753}
754
755u_int
756m_fixhdr(struct mbuf *m0)
757{
758	u_int len;
759
760	len = m_length(m0, NULL);
761	m0->m_pkthdr.len = len;
762	return (len);
763}
764
765u_int
766m_length(struct mbuf *m0, struct mbuf **last)
767{
768	struct mbuf *m;
769	u_int len;
770
771	len = 0;
772	for (m = m0; m != NULL; m = m->m_next) {
773		len += m->m_len;
774		if (m->m_next == NULL)
775			break;
776	}
777	if (last != NULL)
778		*last = m;
779	return (len);
780}
781