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