Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.89
      1 /*	uipc_mbuf.c,v 1.84 2004/07/21 12:06:46 yamt Exp	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1988, 1991, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. Neither the name of the University nor the names of its contributors
     53  *    may be used to endorse or promote products derived from this software
     54  *    without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66  * SUCH DAMAGE.
     67  *
     68  *	@(#)uipc_mbuf.c	8.4 (Berkeley) 2/14/95
     69  */
     70 
     71 #include <sys/cdefs.h>
     72 __KERNEL_RCSID(0, "uipc_mbuf.c,v 1.84 2004/07/21 12:06:46 yamt Exp");
     73 
     74 #include "opt_mbuftrace.h"
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/proc.h>
     79 #include <sys/malloc.h>
     80 #define MBTYPES
     81 #include <sys/mbuf.h>
     82 #include <sys/kernel.h>
     83 #include <sys/syslog.h>
     84 #include <sys/domain.h>
     85 #include <sys/protosw.h>
     86 #include <sys/pool.h>
     87 #include <sys/socket.h>
     88 #include <sys/sysctl.h>
     89 
     90 #include <net/if.h>
     91 
     92 #include <uvm/uvm.h>
     93 
     94 
     95 struct	pool mbpool;		/* mbuf pool */
     96 struct	pool mclpool;		/* mbuf cluster pool */
     97 
     98 struct pool_cache mbpool_cache;
     99 struct pool_cache mclpool_cache;
    100 
    101 struct mbstat mbstat;
    102 int	max_linkhdr;
    103 int	max_protohdr;
    104 int	max_hdr;
    105 int	max_datalen;
    106 
    107 static int mb_ctor(void *, void *, int);
    108 
    109 void	*mclpool_alloc(struct pool *, int);
    110 void	mclpool_release(struct pool *, void *);
    111 
    112 struct pool_allocator mclpool_allocator = {
    113 	mclpool_alloc, mclpool_release, 0,
    114 };
    115 
    116 static struct mbuf *m_copym0(struct mbuf *, int, int, int, int);
    117 static struct mbuf *m_split0(struct mbuf *, int, int, int);
    118 static int m_copyback0(struct mbuf **, int, int, const void *, int, int);
    119 
    120 /* flags for m_copyback0 */
    121 #define	M_COPYBACK0_COPYBACK	0x0001	/* copyback from cp */
    122 #define	M_COPYBACK0_PRESERVE	0x0002	/* preserve original data */
    123 #define	M_COPYBACK0_COW		0x0004	/* do copy-on-write */
    124 #define	M_COPYBACK0_EXTEND	0x0008	/* extend chain */
    125 
    126 const char mclpool_warnmsg[] =
    127     "WARNING: mclpool limit reached; increase NMBCLUSTERS";
    128 
    129 MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf");
    130 
    131 #ifdef MBUFTRACE
    132 struct mownerhead mowners = LIST_HEAD_INITIALIZER(mowners);
    133 struct mowner unknown_mowners[] = {
    134 	{ "unknown", "free" },
    135 	{ "unknown", "data" },
    136 	{ "unknown", "header" },
    137 	{ "unknown", "soname" },
    138 	{ "unknown", "soopts" },
    139 	{ "unknown", "ftable" },
    140 	{ "unknown", "control" },
    141 	{ "unknown", "oobdata" },
    142 };
    143 struct mowner revoked_mowner = { "revoked", "" };
    144 #endif
    145 
    146 /*
    147  * Initialize the mbuf allocator.
    148  */
    149 void
    150 mbinit(void)
    151 {
    152 
    153 	KASSERT(sizeof(struct _m_ext) <= MHLEN);
    154 	KASSERT(sizeof(struct mbuf) == MSIZE);
    155 
    156 	pool_init(&mbpool, msize, 0, 0, 0, "mbpl", NULL);
    157 	pool_init(&mclpool, mclbytes, 0, 0, 0, "mclpl", &mclpool_allocator);
    158 
    159 	pool_set_drain_hook(&mbpool, m_reclaim, NULL);
    160 	pool_set_drain_hook(&mclpool, m_reclaim, NULL);
    161 
    162 	pool_cache_init(&mbpool_cache, &mbpool, mb_ctor, NULL, NULL);
    163 	pool_cache_init(&mclpool_cache, &mclpool, NULL, NULL, NULL);
    164 
    165 	/*
    166 	 * Set the hard limit on the mclpool to the number of
    167 	 * mbuf clusters the kernel is to support.  Log the limit
    168 	 * reached message max once a minute.
    169 	 */
    170 	pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
    171 
    172 	/*
    173 	 * Set a low water mark for both mbufs and clusters.  This should
    174 	 * help ensure that they can be allocated in a memory starvation
    175 	 * situation.  This is important for e.g. diskless systems which
    176 	 * must allocate mbufs in order for the pagedaemon to clean pages.
    177 	 */
    178 	pool_setlowat(&mbpool, mblowat);
    179 	pool_setlowat(&mclpool, mcllowat);
    180 
    181 #ifdef MBUFTRACE
    182 	{
    183 		/*
    184 		 * Attach the unknown mowners.
    185 		 */
    186 		int i;
    187 		MOWNER_ATTACH(&revoked_mowner);
    188 		for (i = sizeof(unknown_mowners)/sizeof(unknown_mowners[0]);
    189 		     i-- > 0; )
    190 			MOWNER_ATTACH(&unknown_mowners[i]);
    191 	}
    192 #endif
    193 }
    194 
    195 /*
    196  * sysctl helper routine for the kern.mbuf subtree.  nmbclusters may
    197  * or may not be writable, and mblowat and mcllowat need range
    198  * checking and pool tweaking after being reset.
    199  */
    200 static int
    201 sysctl_kern_mbuf(SYSCTLFN_ARGS)
    202 {
    203 	int error, newval;
    204 	struct sysctlnode node;
    205 
    206 	node = *rnode;
    207 	node.sysctl_data = &newval;
    208 	switch (rnode->sysctl_num) {
    209 	case MBUF_NMBCLUSTERS:
    210 		if (mb_map != NULL) {
    211 			node.sysctl_flags &= ~CTLFLAG_READWRITE;
    212 			node.sysctl_flags |= CTLFLAG_READONLY;
    213 		}
    214 		/* FALLTHROUGH */
    215 	case MBUF_MBLOWAT:
    216 	case MBUF_MCLLOWAT:
    217 		newval = *(int*)rnode->sysctl_data;
    218 		break;
    219 	default:
    220 		return (EOPNOTSUPP);
    221 	}
    222 
    223 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    224 	if (error || newp == NULL)
    225 		return (error);
    226 	if (newval < 0)
    227 		return (EINVAL);
    228 
    229 	switch (node.sysctl_num) {
    230 	case MBUF_NMBCLUSTERS:
    231 		if (newval < nmbclusters)
    232 			return (EINVAL);
    233 		nmbclusters = newval;
    234 		pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
    235 		break;
    236 	case MBUF_MBLOWAT:
    237 		mblowat = newval;
    238 		pool_setlowat(&mbpool, mblowat);
    239 		break;
    240 	case MBUF_MCLLOWAT:
    241 		mcllowat = newval;
    242 		pool_setlowat(&mclpool, mcllowat);
    243 		break;
    244 	}
    245 
    246 	return (0);
    247 }
    248 
    249 #ifdef MBUFTRACE
    250 static int
    251 sysctl_kern_mbuf_mowners(SYSCTLFN_ARGS)
    252 {
    253 	struct mowner *mo;
    254 	size_t len = 0;
    255 	int error = 0;
    256 
    257 	if (namelen != 0)
    258 		return (EINVAL);
    259 	if (newp != NULL)
    260 		return (EPERM);
    261 
    262 	LIST_FOREACH(mo, &mowners, mo_link) {
    263 		if (oldp != NULL) {
    264 			if (*oldlenp - len < sizeof(*mo)) {
    265 				error = ENOMEM;
    266 				break;
    267 			}
    268 			error = copyout(mo, (caddr_t) oldp + len,
    269 					sizeof(*mo));
    270 			if (error)
    271 				break;
    272 		}
    273 		len += sizeof(*mo);
    274 	}
    275 
    276 	if (error == 0)
    277 		*oldlenp = len;
    278 
    279 	return (error);
    280 }
    281 #endif /* MBUFTRACE */
    282 
    283 SYSCTL_SETUP(sysctl_kern_mbuf_setup, "sysctl kern.mbuf subtree setup")
    284 {
    285 
    286 	sysctl_createv(clog, 0, NULL, NULL,
    287 		       CTLFLAG_PERMANENT,
    288 		       CTLTYPE_NODE, "kern", NULL,
    289 		       NULL, 0, NULL, 0,
    290 		       CTL_KERN, CTL_EOL);
    291 	sysctl_createv(clog, 0, NULL, NULL,
    292 		       CTLFLAG_PERMANENT,
    293 		       CTLTYPE_NODE, "mbuf",
    294 		       SYSCTL_DESCR("mbuf control variables"),
    295 		       NULL, 0, NULL, 0,
    296 		       CTL_KERN, KERN_MBUF, CTL_EOL);
    297 
    298 	sysctl_createv(clog, 0, NULL, NULL,
    299 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    300 		       CTLTYPE_INT, "msize",
    301 		       SYSCTL_DESCR("mbuf base size"),
    302 		       NULL, msize, NULL, 0,
    303 		       CTL_KERN, KERN_MBUF, MBUF_MSIZE, CTL_EOL);
    304 	sysctl_createv(clog, 0, NULL, NULL,
    305 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    306 		       CTLTYPE_INT, "mclbytes",
    307 		       SYSCTL_DESCR("mbuf cluster size"),
    308 		       NULL, mclbytes, NULL, 0,
    309 		       CTL_KERN, KERN_MBUF, MBUF_MCLBYTES, CTL_EOL);
    310 	sysctl_createv(clog, 0, NULL, NULL,
    311 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    312 		       CTLTYPE_INT, "nmbclusters",
    313 		       SYSCTL_DESCR("Limit on the number of mbuf clusters"),
    314 		       sysctl_kern_mbuf, 0, &nmbclusters, 0,
    315 		       CTL_KERN, KERN_MBUF, MBUF_NMBCLUSTERS, CTL_EOL);
    316 	sysctl_createv(clog, 0, NULL, NULL,
    317 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    318 		       CTLTYPE_INT, "mblowat",
    319 		       SYSCTL_DESCR("mbuf low water mark"),
    320 		       sysctl_kern_mbuf, 0, &mblowat, 0,
    321 		       CTL_KERN, KERN_MBUF, MBUF_MBLOWAT, CTL_EOL);
    322 	sysctl_createv(clog, 0, NULL, NULL,
    323 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    324 		       CTLTYPE_INT, "mcllowat",
    325 		       SYSCTL_DESCR("mbuf cluster low water mark"),
    326 		       sysctl_kern_mbuf, 0, &mcllowat, 0,
    327 		       CTL_KERN, KERN_MBUF, MBUF_MCLLOWAT, CTL_EOL);
    328 	sysctl_createv(clog, 0, NULL, NULL,
    329 		       CTLFLAG_PERMANENT,
    330 		       CTLTYPE_STRUCT, "stats",
    331 		       SYSCTL_DESCR("mbuf allocation statistics"),
    332 		       NULL, 0, &mbstat, sizeof(mbstat),
    333 		       CTL_KERN, KERN_MBUF, MBUF_STATS, CTL_EOL);
    334 #ifdef MBUFTRACE
    335 	sysctl_createv(clog, 0, NULL, NULL,
    336 		       CTLFLAG_PERMANENT,
    337 		       CTLTYPE_STRUCT, "mowners",
    338 		       SYSCTL_DESCR("Information about mbuf owners"),
    339 		       sysctl_kern_mbuf_mowners, 0, NULL, 0,
    340 		       CTL_KERN, KERN_MBUF, MBUF_MOWNERS, CTL_EOL);
    341 #endif /* MBUFTRACE */
    342 }
    343 
    344 void *
    345 mclpool_alloc(struct pool *pp, int flags)
    346 {
    347 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
    348 
    349 	return ((void *)uvm_km_alloc_poolpage1(mb_map, NULL, waitok));
    350 }
    351 
    352 void
    353 mclpool_release(struct pool *pp, void *v)
    354 {
    355 
    356 	uvm_km_free_poolpage1(mb_map, (vaddr_t)v);
    357 }
    358 
    359 /*ARGSUSED*/
    360 static int
    361 mb_ctor(void *arg, void *object, int flags)
    362 {
    363 	struct mbuf *m = object;
    364 
    365 #ifdef POOL_VTOPHYS
    366 	m->m_paddr = POOL_VTOPHYS(m);
    367 #else
    368 	m->m_paddr = M_PADDR_INVALID;
    369 #endif
    370 	return (0);
    371 }
    372 
    373 void
    374 m_reclaim(void *arg, int flags)
    375 {
    376 	struct domain *dp;
    377 	const struct protosw *pr;
    378 	struct ifnet *ifp;
    379 	int s = splvm();
    380 
    381 	for (dp = domains; dp; dp = dp->dom_next)
    382 		for (pr = dp->dom_protosw;
    383 		     pr < dp->dom_protoswNPROTOSW; pr++)
    384 			if (pr->pr_drain)
    385 				(*pr->pr_drain)();
    386 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
    387 		if (ifp->if_drain)
    388 			(*ifp->if_drain)(ifp);
    389 	splx(s);
    390 	mbstat.m_drain++;
    391 }
    392 
    393 /*
    394  * Space allocation routines.
    395  * These are also available as macros
    396  * for critical paths.
    397  */
    398 struct mbuf *
    399 m_get(int nowait, int type)
    400 {
    401 	struct mbuf *m;
    402 
    403 	MGET(m, nowait, type);
    404 	return (m);
    405 }
    406 
    407 struct mbuf *
    408 m_gethdr(int nowait, int type)
    409 {
    410 	struct mbuf *m;
    411 
    412 	MGETHDR(m, nowait, type);
    413 	return (m);
    414 }
    415 
    416 struct mbuf *
    417 m_getclr(int nowait, int type)
    418 {
    419 	struct mbuf *m;
    420 
    421 	MGET(m, nowait, type);
    422 	if (m == 0)
    423 		return (NULL);
    424 	memset(mtod(m, caddr_t), 0, MLEN);
    425 	return (m);
    426 }
    427 
    428 void
    429 m_clget(struct mbuf *m, int nowait)
    430 {
    431 
    432 	MCLGET(m, nowait);
    433 }
    434 
    435 struct mbuf *
    436 m_free(struct mbuf *m)
    437 {
    438 	struct mbuf *n;
    439 
    440 	MFREE(m, n);
    441 	return (n);
    442 }
    443 
    444 void
    445 m_freem(struct mbuf *m)
    446 {
    447 	struct mbuf *n;
    448 
    449 	if (m == NULL)
    450 		return;
    451 	do {
    452 		MFREE(m, n);
    453 		m = n;
    454 	} while (m);
    455 }
    456 
    457 #ifdef MBUFTRACE
    458 /*
    459  * Walk a chain of mbufs, claiming ownership of each mbuf in the chain.
    460  */
    461 void
    462 m_claimm(struct mbuf *m, struct mowner *mo)
    463 {
    464 
    465 	for (; m != NULL; m = m->m_next)
    466 		MCLAIM(m, mo);
    467 }
    468 #endif
    469 
    470 /*
    471  * Mbuffer utility routines.
    472  */
    473 
    474 /*
    475  * Lesser-used path for M_PREPEND:
    476  * allocate new mbuf to prepend to chain,
    477  * copy junk along.
    478  */
    479 struct mbuf *
    480 m_prepend(struct mbuf *m, int len, int how)
    481 {
    482 	struct mbuf *mn;
    483 
    484 	MGET(mn, how, m->m_type);
    485 	if (mn == (struct mbuf *)NULL) {
    486 		m_freem(m);
    487 		return ((struct mbuf *)NULL);
    488 	}
    489 	if (m->m_flags & M_PKTHDR) {
    490 		M_COPY_PKTHDR(mn, m);
    491 		m_tag_delete_chain(m, NULL);
    492 		m->m_flags &= ~M_PKTHDR;
    493 	} else {
    494 		MCLAIM(mn, m->m_owner);
    495 	}
    496 	mn->m_next = m;
    497 	m = mn;
    498 	if (len < MHLEN)
    499 		MH_ALIGN(m, len);
    500 	m->m_len = len;
    501 	return (m);
    502 }
    503 
    504 /*
    505  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    506  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    507  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    508  */
    509 int MCFail;
    510 
    511 struct mbuf *
    512 m_copym(struct mbuf *m, int off0, int len, int wait)
    513 {
    514 
    515 	return m_copym0(m, off0, len, wait, 0);	/* shallow copy on M_EXT */
    516 }
    517 
    518 struct mbuf *
    519 m_dup(struct mbuf *m, int off0, int len, int wait)
    520 {
    521 
    522 	return m_copym0(m, off0, len, wait, 1);	/* deep copy */
    523 }
    524 
    525 static struct mbuf *
    526 m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
    527 {
    528 	struct mbuf *n, **np;
    529 	int off = off0;
    530 	struct mbuf *top;
    531 	int copyhdr = 0;
    532 
    533 	if (off < 0 || len < 0)
    534 		panic("m_copym: off %d, len %d", off, len);
    535 	if (off == 0 && m->m_flags & M_PKTHDR)
    536 		copyhdr = 1;
    537 	while (off > 0) {
    538 		if (m == 0)
    539 			panic("m_copym: m == 0");
    540 		if (off < m->m_len)
    541 			break;
    542 		off -= m->m_len;
    543 		m = m->m_next;
    544 	}
    545 	np = &top;
    546 	top = 0;
    547 	while (len > 0) {
    548 		if (m == 0) {
    549 			if (len != M_COPYALL)
    550 				panic("m_copym: m == 0 and not COPYALL");
    551 			break;
    552 		}
    553 		MGET(n, wait, m->m_type);
    554 		*np = n;
    555 		if (n == 0)
    556 			goto nospace;
    557 		MCLAIM(n, m->m_owner);
    558 		if (copyhdr) {
    559 			M_COPY_PKTHDR(n, m);
    560 			if (len == M_COPYALL)
    561 				n->m_pkthdr.len -= off0;
    562 			else
    563 				n->m_pkthdr.len = len;
    564 			copyhdr = 0;
    565 		}
    566 		n->m_len = min(len, m->m_len - off);
    567 		if (m->m_flags & M_EXT) {
    568 			if (!deep) {
    569 				n->m_data = m->m_data + off;
    570 				n->m_ext = m->m_ext;
    571 				MCLADDREFERENCE(m, n);
    572 			} else {
    573 				/*
    574 				 * we are unsure about the way m was allocated.
    575 				 * copy into multiple MCLBYTES cluster mbufs.
    576 				 */
    577 				MCLGET(n, wait);
    578 				n->m_len = 0;
    579 				n->m_len = M_TRAILINGSPACE(n);
    580 				n->m_len = min(n->m_len, len);
    581 				n->m_len = min(n->m_len, m->m_len - off);
    582 				memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off,
    583 				    (unsigned)n->m_len);
    584 			}
    585 		} else
    586 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
    587 			    (unsigned)n->m_len);
    588 		if (len != M_COPYALL)
    589 			len -= n->m_len;
    590 		off += n->m_len;
    591 #ifdef DIAGNOSTIC
    592 		if (off > m->m_len)
    593 			panic("m_copym0 overrun");
    594 #endif
    595 		if (off == m->m_len) {
    596 			m = m->m_next;
    597 			off = 0;
    598 		}
    599 		np = &n->m_next;
    600 	}
    601 	if (top == 0)
    602 		MCFail++;
    603 	return (top);
    604 nospace:
    605 	m_freem(top);
    606 	MCFail++;
    607 	return (NULL);
    608 }
    609 
    610 /*
    611  * Copy an entire packet, including header (which must be present).
    612  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
    613  */
    614 struct mbuf *
    615 m_copypacket(struct mbuf *m, int how)
    616 {
    617 	struct mbuf *top, *n, *o;
    618 
    619 	MGET(n, how, m->m_type);
    620 	top = n;
    621 	if (!n)
    622 		goto nospace;
    623 
    624 	MCLAIM(n, m->m_owner);
    625 	M_COPY_PKTHDR(n, m);
    626 	n->m_len = m->m_len;
    627 	if (m->m_flags & M_EXT) {
    628 		n->m_data = m->m_data;
    629 		n->m_ext = m->m_ext;
    630 		MCLADDREFERENCE(m, n);
    631 	} else {
    632 		memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    633 	}
    634 
    635 	m = m->m_next;
    636 	while (m) {
    637 		MGET(o, how, m->m_type);
    638 		if (!o)
    639 			goto nospace;
    640 
    641 		MCLAIM(o, m->m_owner);
    642 		n->m_next = o;
    643 		n = n->m_next;
    644 
    645 		n->m_len = m->m_len;
    646 		if (m->m_flags & M_EXT) {
    647 			n->m_data = m->m_data;
    648 			n->m_ext = m->m_ext;
    649 			MCLADDREFERENCE(m, n);
    650 		} else {
    651 			memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    652 		}
    653 
    654 		m = m->m_next;
    655 	}
    656 	return top;
    657 nospace:
    658 	m_freem(top);
    659 	MCFail++;
    660 	return NULL;
    661 }
    662 
    663 /*
    664  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    665  * continuing for "len" bytes, into the indicated buffer.
    666  */
    667 void
    668 m_copydata(struct mbuf *m, int off, int len, void *vp)
    669 {
    670 	unsigned count;
    671 	char *cp = vp;
    672 
    673 	if (off < 0 || len < 0)
    674 		panic("m_copydata");
    675 	while (off > 0) {
    676 		if (m == 0)
    677 			panic("m_copydata");
    678 		if (off < m->m_len)
    679 			break;
    680 		off -= m->m_len;
    681 		m = m->m_next;
    682 	}
    683 	while (len > 0) {
    684 		if (m == 0)
    685 			panic("m_copydata");
    686 		count = min(m->m_len - off, len);
    687 		memcpy(cp, mtod(m, caddr_t) + off, count);
    688 		len -= count;
    689 		cp += count;
    690 		off = 0;
    691 		m = m->m_next;
    692 	}
    693 }
    694 
    695 /*
    696  * Concatenate mbuf chain n to m.
    697  * n might be copied into m (when n->m_len is small), therefore data portion of
    698  * n could be copied into an mbuf of different mbuf type.
    699  * Any m_pkthdr is not updated.
    700  */
    701 void
    702 m_cat(struct mbuf *m, struct mbuf *n)
    703 {
    704 
    705 	while (m->m_next)
    706 		m = m->m_next;
    707 	while (n) {
    708 		if (M_READONLY(m) || n->m_len > M_TRAILINGSPACE(m)) {
    709 			/* just join the two chains */
    710 			m->m_next = n;
    711 			return;
    712 		}
    713 		/* splat the data from one into the other */
    714 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    715 		    (u_int)n->m_len);
    716 		m->m_len += n->m_len;
    717 		n = m_free(n);
    718 	}
    719 }
    720 
    721 void
    722 m_adj(struct mbuf *mp, int req_len)
    723 {
    724 	int len = req_len;
    725 	struct mbuf *m;
    726 	int count;
    727 
    728 	if ((m = mp) == NULL)
    729 		return;
    730 	if (len >= 0) {
    731 		/*
    732 		 * Trim from head.
    733 		 */
    734 		while (m != NULL && len > 0) {
    735 			if (m->m_len <= len) {
    736 				len -= m->m_len;
    737 				m->m_len = 0;
    738 				m = m->m_next;
    739 			} else {
    740 				m->m_len -= len;
    741 				m->m_data += len;
    742 				len = 0;
    743 			}
    744 		}
    745 		m = mp;
    746 		if (mp->m_flags & M_PKTHDR)
    747 			m->m_pkthdr.len -= (req_len - len);
    748 	} else {
    749 		/*
    750 		 * Trim from tail.  Scan the mbuf chain,
    751 		 * calculating its length and finding the last mbuf.
    752 		 * If the adjustment only affects this mbuf, then just
    753 		 * adjust and return.  Otherwise, rescan and truncate
    754 		 * after the remaining size.
    755 		 */
    756 		len = -len;
    757 		count = 0;
    758 		for (;;) {
    759 			count += m->m_len;
    760 			if (m->m_next == (struct mbuf *)0)
    761 				break;
    762 			m = m->m_next;
    763 		}
    764 		if (m->m_len >= len) {
    765 			m->m_len -= len;
    766 			if (mp->m_flags & M_PKTHDR)
    767 				mp->m_pkthdr.len -= len;
    768 			return;
    769 		}
    770 		count -= len;
    771 		if (count < 0)
    772 			count = 0;
    773 		/*
    774 		 * Correct length for chain is "count".
    775 		 * Find the mbuf with last data, adjust its length,
    776 		 * and toss data from remaining mbufs on chain.
    777 		 */
    778 		m = mp;
    779 		if (m->m_flags & M_PKTHDR)
    780 			m->m_pkthdr.len = count;
    781 		for (; m; m = m->m_next) {
    782 			if (m->m_len >= count) {
    783 				m->m_len = count;
    784 				break;
    785 			}
    786 			count -= m->m_len;
    787 		}
    788 		while (m->m_next)
    789 			(m = m->m_next) ->m_len = 0;
    790 	}
    791 }
    792 
    793 /*
    794  * Rearange an mbuf chain so that len bytes are contiguous
    795  * and in the data area of an mbuf (so that mtod and dtom
    796  * will work for a structure of size len).  Returns the resulting
    797  * mbuf chain on success, frees it and returns null on failure.
    798  * If there is room, it will add up to max_protohdr-len extra bytes to the
    799  * contiguous region in an attempt to avoid being called next time.
    800  */
    801 int MPFail;
    802 
    803 struct mbuf *
    804 m_pullup(struct mbuf *n, int len)
    805 {
    806 	struct mbuf *m;
    807 	int count;
    808 	int space;
    809 
    810 	/*
    811 	 * If first mbuf has no cluster, and has room for len bytes
    812 	 * without shifting current data, pullup into it,
    813 	 * otherwise allocate a new mbuf to prepend to the chain.
    814 	 */
    815 	if ((n->m_flags & M_EXT) == 0 &&
    816 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
    817 		if (n->m_len >= len)
    818 			return (n);
    819 		m = n;
    820 		n = n->m_next;
    821 		len -= m->m_len;
    822 	} else {
    823 		if (len > MHLEN)
    824 			goto bad;
    825 		MGET(m, M_DONTWAIT, n->m_type);
    826 		if (m == 0)
    827 			goto bad;
    828 		MCLAIM(m, n->m_owner);
    829 		m->m_len = 0;
    830 		if (n->m_flags & M_PKTHDR) {
    831 			M_COPY_PKTHDR(m, n);
    832 			m_tag_delete_chain(n, NULL);
    833 			n->m_flags &= ~M_PKTHDR;
    834 		}
    835 	}
    836 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    837 	do {
    838 		count = min(min(max(len, max_protohdr), space), n->m_len);
    839 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    840 		  (unsigned)count);
    841 		len -= count;
    842 		m->m_len += count;
    843 		n->m_len -= count;
    844 		space -= count;
    845 		if (n->m_len)
    846 			n->m_data += count;
    847 		else
    848 			n = m_free(n);
    849 	} while (len > 0 && n);
    850 	if (len > 0) {
    851 		(void) m_free(m);
    852 		goto bad;
    853 	}
    854 	m->m_next = n;
    855 	return (m);
    856 bad:
    857 	m_freem(n);
    858 	MPFail++;
    859 	return (NULL);
    860 }
    861 
    862 /*
    863  * Like m_pullup(), except a new mbuf is always allocated, and we allow
    864  * the amount of empty space before the data in the new mbuf to be specified
    865  * (in the event that the caller expects to prepend later).
    866  */
    867 int MSFail;
    868 
    869 struct mbuf *
    870 m_copyup(struct mbuf *n, int len, int dstoff)
    871 {
    872 	struct mbuf *m;
    873 	int count, space;
    874 
    875 	if (len > (MHLEN - dstoff))
    876 		goto bad;
    877 	MGET(m, M_DONTWAIT, n->m_type);
    878 	if (m == NULL)
    879 		goto bad;
    880 	MCLAIM(m, n->m_owner);
    881 	m->m_len = 0;
    882 	if (n->m_flags & M_PKTHDR) {
    883 		M_COPY_PKTHDR(m, n);
    884 		m_tag_delete_chain(n, NULL);
    885 		n->m_flags &= ~M_PKTHDR;
    886 	}
    887 	m->m_data += dstoff;
    888 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    889 	do {
    890 		count = min(min(max(len, max_protohdr), space), n->m_len);
    891 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    892 		    (unsigned)count);
    893 		len -= count;
    894 		m->m_len += count;
    895 		n->m_len -= count;
    896 		space -= count;
    897 		if (n->m_len)
    898 			n->m_data += count;
    899 		else
    900 			n = m_free(n);
    901 	} while (len > 0 && n);
    902 	if (len > 0) {
    903 		(void) m_free(m);
    904 		goto bad;
    905 	}
    906 	m->m_next = n;
    907 	return (m);
    908  bad:
    909 	m_freem(n);
    910 	MSFail++;
    911 	return (NULL);
    912 }
    913 
    914 /*
    915  * Partition an mbuf chain in two pieces, returning the tail --
    916  * all but the first len0 bytes.  In case of failure, it returns NULL and
    917  * attempts to restore the chain to its original state.
    918  */
    919 struct mbuf *
    920 m_split(struct mbuf *m0, int len0, int wait)
    921 {
    922 
    923 	return m_split0(m0, len0, wait, 1);
    924 }
    925 
    926 static struct mbuf *
    927 m_split0(struct mbuf *m0, int len0, int wait, int copyhdr)
    928 {
    929 	struct mbuf *m, *n;
    930 	unsigned len = len0, remain, len_save;
    931 
    932 	for (m = m0; m && len > m->m_len; m = m->m_next)
    933 		len -= m->m_len;
    934 	if (m == 0)
    935 		return (NULL);
    936 	remain = m->m_len - len;
    937 	if (copyhdr && (m0->m_flags & M_PKTHDR)) {
    938 		MGETHDR(n, wait, m0->m_type);
    939 		if (n == 0)
    940 			return (NULL);
    941 		MCLAIM(m, m0->m_owner);
    942 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
    943 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
    944 		len_save = m0->m_pkthdr.len;
    945 		m0->m_pkthdr.len = len0;
    946 		if (m->m_flags & M_EXT)
    947 			goto extpacket;
    948 		if (remain > MHLEN) {
    949 			/* m can't be the lead packet */
    950 			MH_ALIGN(n, 0);
    951 			n->m_next = m_split(m, len, wait);
    952 			if (n->m_next == 0) {
    953 				(void) m_free(n);
    954 				m0->m_pkthdr.len = len_save;
    955 				return (NULL);
    956 			} else
    957 				return (n);
    958 		} else
    959 			MH_ALIGN(n, remain);
    960 	} else if (remain == 0) {
    961 		n = m->m_next;
    962 		m->m_next = 0;
    963 		return (n);
    964 	} else {
    965 		MGET(n, wait, m->m_type);
    966 		if (n == 0)
    967 			return (NULL);
    968 		MCLAIM(n, m->m_owner);
    969 		M_ALIGN(n, remain);
    970 	}
    971 extpacket:
    972 	if (m->m_flags & M_EXT) {
    973 		n->m_ext = m->m_ext;
    974 		MCLADDREFERENCE(m, n);
    975 		n->m_data = m->m_data + len;
    976 	} else {
    977 		memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
    978 	}
    979 	n->m_len = remain;
    980 	m->m_len = len;
    981 	n->m_next = m->m_next;
    982 	m->m_next = 0;
    983 	return (n);
    984 }
    985 /*
    986  * Routine to copy from device local memory into mbufs.
    987  */
    988 struct mbuf *
    989 m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
    990     void (*copy)(const void *from, void *to, size_t len))
    991 {
    992 	struct mbuf *m;
    993 	struct mbuf *top = 0, **mp = &top;
    994 	int off = off0, len;
    995 	char *cp;
    996 	char *epkt;
    997 
    998 	cp = buf;
    999 	epkt = cp + totlen;
   1000 	if (off) {
   1001 		/*
   1002 		 * If 'off' is non-zero, packet is trailer-encapsulated,
   1003 		 * so we have to skip the type and length fields.
   1004 		 */
   1005 		cp += off + 2 * sizeof(u_int16_t);
   1006 		totlen -= 2 * sizeof(u_int16_t);
   1007 	}
   1008 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1009 	if (m == 0)
   1010 		return (NULL);
   1011 	m->m_pkthdr.rcvif = ifp;
   1012 	m->m_pkthdr.len = totlen;
   1013 	m->m_len = MHLEN;
   1014 
   1015 	while (totlen > 0) {
   1016 		if (top) {
   1017 			MGET(m, M_DONTWAIT, MT_DATA);
   1018 			if (m == 0) {
   1019 				m_freem(top);
   1020 				return (NULL);
   1021 			}
   1022 			m->m_len = MLEN;
   1023 		}
   1024 		len = min(totlen, epkt - cp);
   1025 		if (len >= MINCLSIZE) {
   1026 			MCLGET(m, M_DONTWAIT);
   1027 			if ((m->m_flags & M_EXT) == 0) {
   1028 				m_free(m);
   1029 				m_freem(top);
   1030 				return (NULL);
   1031 			}
   1032 			m->m_len = len = min(len, MCLBYTES);
   1033 		} else {
   1034 			/*
   1035 			 * Place initial small packet/header at end of mbuf.
   1036 			 */
   1037 			if (len < m->m_len) {
   1038 				if (top == 0 && len + max_linkhdr <= m->m_len)
   1039 					m->m_data += max_linkhdr;
   1040 				m->m_len = len;
   1041 			} else
   1042 				len = m->m_len;
   1043 		}
   1044 		if (copy)
   1045 			copy(cp, mtod(m, caddr_t), (size_t)len);
   1046 		else
   1047 			memcpy(mtod(m, caddr_t), cp, (size_t)len);
   1048 		cp += len;
   1049 		*mp = m;
   1050 		mp = &m->m_next;
   1051 		totlen -= len;
   1052 		if (cp == epkt)
   1053 			cp = buf;
   1054 	}
   1055 	return (top);
   1056 }
   1057 
   1058 /*
   1059  * Copy data from a buffer back into the indicated mbuf chain,
   1060  * starting "off" bytes from the beginning, extending the mbuf
   1061  * chain if necessary.
   1062  */
   1063 void
   1064 m_copyback(struct mbuf *m0, int off, int len, const void *cp)
   1065 {
   1066 #if defined(DEBUG)
   1067 	struct mbuf *origm = m0;
   1068 	int error;
   1069 #endif /* defined(DEBUG) */
   1070 
   1071 	if (m0 == NULL)
   1072 		return;
   1073 
   1074 #if defined(DEBUG)
   1075 	error =
   1076 #endif /* defined(DEBUG) */
   1077 	m_copyback0(&m0, off, len, cp,
   1078 	    M_COPYBACK0_COPYBACK|M_COPYBACK0_EXTEND, M_DONTWAIT);
   1079 
   1080 #if defined(DEBUG)
   1081 	if (error != 0 || (m0 != NULL && origm != m0))
   1082 		panic("m_copyback");
   1083 #endif /* defined(DEBUG) */
   1084 }
   1085 
   1086 struct mbuf *
   1087 m_copyback_cow(struct mbuf *m0, int off, int len, const void *cp, int how)
   1088 {
   1089 	int error;
   1090 
   1091 	/* don't support chain expansion */
   1092 	KDASSERT(off + len <= m_length(m0));
   1093 
   1094 	error = m_copyback0(&m0, off, len, cp,
   1095 	    M_COPYBACK0_COPYBACK|M_COPYBACK0_COW, how);
   1096 	if (error) {
   1097 		/*
   1098 		 * no way to recover from partial success.
   1099 		 * just free the chain.
   1100 		 */
   1101 		m_freem(m0);
   1102 		return NULL;
   1103 	}
   1104 	return m0;
   1105 }
   1106 
   1107 /*
   1108  * m_makewritable: ensure the specified range writable.
   1109  */
   1110 int
   1111 m_makewritable(struct mbuf **mp, int off, int len, int how)
   1112 {
   1113 	int error;
   1114 #if defined(DEBUG)
   1115 	struct mbuf *n;
   1116 	int origlen, reslen;
   1117 
   1118 	origlen = m_length(*mp);
   1119 #endif /* defined(DEBUG) */
   1120 
   1121 #if 0 /* M_COPYALL is large enough */
   1122 	if (len == M_COPYALL)
   1123 		len = m_length(*mp) - off; /* XXX */
   1124 #endif
   1125 
   1126 	error = m_copyback0(mp, off, len, NULL,
   1127 	    M_COPYBACK0_PRESERVE|M_COPYBACK0_COW, how);
   1128 
   1129 #if defined(DEBUG)
   1130 	reslen = 0;
   1131 	for (n = *mp; n; n = n->m_next)
   1132 		reslen += n->m_len;
   1133 	if (origlen != reslen)
   1134 		panic("m_makewritable: length changed");
   1135 	if (((*mp)->m_flags & M_PKTHDR) != 0 && reslen != (*mp)->m_pkthdr.len)
   1136 		panic("m_makewritable: inconsist");
   1137 #endif /* defined(DEBUG) */
   1138 
   1139 	return error;
   1140 }
   1141 
   1142 int
   1143 m_copyback0(struct mbuf **mp0, int off, int len, const void *vp, int flags,
   1144     int how)
   1145 {
   1146 	int mlen;
   1147 	struct mbuf *m, *n;
   1148 	struct mbuf **mp;
   1149 	int totlen = 0;
   1150 	const char *cp = vp;
   1151 
   1152 	KASSERT(mp0 != NULL);
   1153 	KASSERT(*mp0 != NULL);
   1154 	KASSERT((flags & M_COPYBACK0_PRESERVE) == 0 || cp == NULL);
   1155 	KASSERT((flags & M_COPYBACK0_COPYBACK) == 0 || cp != NULL);
   1156 
   1157 	mp = mp0;
   1158 	m = *mp;
   1159 	while (off > (mlen = m->m_len)) {
   1160 		off -= mlen;
   1161 		totlen += mlen;
   1162 		if (m->m_next == 0) {
   1163 			if ((flags & M_COPYBACK0_EXTEND) == 0)
   1164 				goto out;
   1165 			n = m_getclr(how, m->m_type);
   1166 			if (n == 0)
   1167 				goto out;
   1168 			n->m_len = min(MLEN, len + off);
   1169 			m->m_next = n;
   1170 		}
   1171 		mp = &m->m_next;
   1172 		m = m->m_next;
   1173 	}
   1174 	while (len > 0) {
   1175 		mlen = m->m_len - off;
   1176 		if (mlen != 0 && M_READONLY(m)) {
   1177 			char *datap;
   1178 			int eatlen;
   1179 
   1180 			/*
   1181 			 * this mbuf is read-only.
   1182 			 * allocate a new writable mbuf and try again.
   1183 			 */
   1184 
   1185 #if defined(DIAGNOSTIC)
   1186 			if ((flags & M_COPYBACK0_COW) == 0)
   1187 				panic("m_copyback0: read-only");
   1188 #endif /* defined(DIAGNOSTIC) */
   1189 
   1190 			/*
   1191 			 * if we're going to write into the middle of
   1192 			 * a mbuf, split it first.
   1193 			 */
   1194 			if (off > 0 && len < mlen) {
   1195 				n = m_split0(m, off, how, 0);
   1196 				if (n == NULL)
   1197 					goto enobufs;
   1198 				m->m_next = n;
   1199 				mp = &m->m_next;
   1200 				m = n;
   1201 				off = 0;
   1202 				continue;
   1203 			}
   1204 
   1205 			/*
   1206 			 * XXX TODO coalesce into the trailingspace of
   1207 			 * the previous mbuf when possible.
   1208 			 */
   1209 
   1210 			/*
   1211 			 * allocate a new mbuf.  copy packet header if needed.
   1212 			 */
   1213 			MGET(n, how, m->m_type);
   1214 			if (n == NULL)
   1215 				goto enobufs;
   1216 			MCLAIM(n, m->m_owner);
   1217 			if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
   1218 				/* XXX M_MOVE_PKTHDR */
   1219 				M_COPY_PKTHDR(n, m);
   1220 				n->m_len = MHLEN;
   1221 			} else {
   1222 				if (len >= MINCLSIZE)
   1223 					MCLGET(n, M_DONTWAIT);
   1224 				n->m_len =
   1225 				    (n->m_flags & M_EXT) ? MCLBYTES : MLEN;
   1226 			}
   1227 			if (n->m_len > len)
   1228 				n->m_len = len;
   1229 
   1230 			/*
   1231 			 * free the region which has been overwritten.
   1232 			 * copying data from old mbufs if requested.
   1233 			 */
   1234 			if (flags & M_COPYBACK0_PRESERVE)
   1235 				datap = mtod(n, char *);
   1236 			else
   1237 				datap = NULL;
   1238 			eatlen = n->m_len;
   1239 			KDASSERT(off == 0 || eatlen >= mlen);
   1240 			if (off > 0) {
   1241 				KDASSERT(len >= mlen);
   1242 				m->m_len = off;
   1243 				m->m_next = n;
   1244 				if (datap) {
   1245 					m_copydata(m, off, mlen, datap);
   1246 					datap += mlen;
   1247 				}
   1248 				eatlen -= mlen;
   1249 				mp = &m->m_next;
   1250 				m = m->m_next;
   1251 			}
   1252 			while (m != NULL && M_READONLY(m) &&
   1253 			    n->m_type == m->m_type && eatlen > 0) {
   1254 				mlen = min(eatlen, m->m_len);
   1255 				if (datap) {
   1256 					m_copydata(m, 0, mlen, datap);
   1257 					datap += mlen;
   1258 				}
   1259 				m->m_data += mlen;
   1260 				m->m_len -= mlen;
   1261 				eatlen -= mlen;
   1262 				if (m->m_len == 0)
   1263 					*mp = m = m_free(m);
   1264 			}
   1265 			if (eatlen > 0)
   1266 				n->m_len -= eatlen;
   1267 			n->m_next = m;
   1268 			*mp = m = n;
   1269 			continue;
   1270 		}
   1271 		mlen = min(mlen, len);
   1272 		if (flags & M_COPYBACK0_COPYBACK) {
   1273 			memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
   1274 			cp += mlen;
   1275 		}
   1276 		len -= mlen;
   1277 		mlen += off;
   1278 		off = 0;
   1279 		totlen += mlen;
   1280 		if (len == 0)
   1281 			break;
   1282 		if (m->m_next == 0) {
   1283 			if ((flags & M_COPYBACK0_EXTEND) == 0)
   1284 				goto out;
   1285 			n = m_get(how, m->m_type);
   1286 			if (n == 0)
   1287 				break;
   1288 			n->m_len = min(MLEN, len);
   1289 			m->m_next = n;
   1290 		}
   1291 		mp = &m->m_next;
   1292 		m = m->m_next;
   1293 	}
   1294 out:	if (((m = *mp0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
   1295 		m->m_pkthdr.len = totlen;
   1296 
   1297 	return 0;
   1298 
   1299 enobufs:
   1300 	return ENOBUFS;
   1301 }
   1302 
   1303 /*
   1304  * Apply function f to the data in an mbuf chain starting "off" bytes from the
   1305  * beginning, continuing for "len" bytes.
   1306  */
   1307 int
   1308 m_apply(struct mbuf *m, int off, int len,
   1309     int (*f)(void *, caddr_t, unsigned int), void *arg)
   1310 {
   1311 	unsigned int count;
   1312 	int rval;
   1313 
   1314 	KASSERT(len >= 0);
   1315 	KASSERT(off >= 0);
   1316 
   1317 	while (off > 0) {
   1318 		KASSERT(m != NULL);
   1319 		if (off < m->m_len)
   1320 			break;
   1321 		off -= m->m_len;
   1322 		m = m->m_next;
   1323 	}
   1324 	while (len > 0) {
   1325 		KASSERT(m != NULL);
   1326 		count = min(m->m_len - off, len);
   1327 
   1328 		rval = (*f)(arg, mtod(m, caddr_t) + off, count);
   1329 		if (rval)
   1330 			return (rval);
   1331 
   1332 		len -= count;
   1333 		off = 0;
   1334 		m = m->m_next;
   1335 	}
   1336 
   1337 	return (0);
   1338 }
   1339 
   1340 /*
   1341  * Return a pointer to mbuf/offset of location in mbuf chain.
   1342  */
   1343 struct mbuf *
   1344 m_getptr(struct mbuf *m, int loc, int *off)
   1345 {
   1346 
   1347 	while (loc >= 0) {
   1348 		/* Normal end of search */
   1349 		if (m->m_len > loc) {
   1350 	    		*off = loc;
   1351 	    		return (m);
   1352 		} else {
   1353 	    		loc -= m->m_len;
   1354 
   1355 	    		if (m->m_next == NULL) {
   1356 				if (loc == 0) {
   1357  					/* Point at the end of valid data */
   1358 		    			*off = m->m_len;
   1359 		    			return (m);
   1360 				} else
   1361 		  			return (NULL);
   1362 	    		} else
   1363 	      			m = m->m_next;
   1364 		}
   1365     	}
   1366 
   1367 	return (NULL);
   1368 }
   1369