Home | History | Annotate | Line # | Download | only in kern
uipc_mbuf.c revision 1.62
      1  1.62   thorpej /*	$NetBSD: uipc_mbuf.c,v 1.62 2003/01/31 04:55:52 thorpej Exp $	*/
      2  1.42   thorpej 
      3  1.42   thorpej /*-
      4  1.53   thorpej  * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
      5  1.42   thorpej  * All rights reserved.
      6  1.42   thorpej  *
      7  1.42   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.42   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.42   thorpej  * NASA Ames Research Center.
     10  1.42   thorpej  *
     11  1.42   thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.42   thorpej  * modification, are permitted provided that the following conditions
     13  1.42   thorpej  * are met:
     14  1.42   thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.42   thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.42   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.42   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.42   thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.42   thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.42   thorpej  *    must display the following acknowledgement:
     21  1.42   thorpej  *	This product includes software developed by the NetBSD
     22  1.42   thorpej  *	Foundation, Inc. and its contributors.
     23  1.42   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.42   thorpej  *    contributors may be used to endorse or promote products derived
     25  1.42   thorpej  *    from this software without specific prior written permission.
     26  1.42   thorpej  *
     27  1.42   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.42   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.42   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.42   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.42   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.42   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.42   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.42   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.42   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.42   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.42   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.42   thorpej  */
     39  1.10       cgd 
     40   1.1       cgd /*
     41   1.9   mycroft  * Copyright (c) 1982, 1986, 1988, 1991, 1993
     42   1.9   mycroft  *	The Regents of the University of California.  All rights reserved.
     43   1.1       cgd  *
     44   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     45   1.1       cgd  * modification, are permitted provided that the following conditions
     46   1.1       cgd  * are met:
     47   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     48   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     49   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     50   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     51   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     52   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     53   1.1       cgd  *    must display the following acknowledgement:
     54   1.1       cgd  *	This product includes software developed by the University of
     55   1.1       cgd  *	California, Berkeley and its contributors.
     56   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     57   1.1       cgd  *    may be used to endorse or promote products derived from this software
     58   1.1       cgd  *    without specific prior written permission.
     59   1.1       cgd  *
     60   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70   1.1       cgd  * SUCH DAMAGE.
     71   1.1       cgd  *
     72  1.26      fvdl  *	@(#)uipc_mbuf.c	8.4 (Berkeley) 2/14/95
     73   1.1       cgd  */
     74  1.56     lukem 
     75  1.56     lukem #include <sys/cdefs.h>
     76  1.62   thorpej __KERNEL_RCSID(0, "$NetBSD: uipc_mbuf.c,v 1.62 2003/01/31 04:55:52 thorpej Exp $");
     77  1.24       mrg 
     78   1.6   mycroft #include <sys/param.h>
     79   1.6   mycroft #include <sys/systm.h>
     80   1.6   mycroft #include <sys/proc.h>
     81   1.6   mycroft #include <sys/malloc.h>
     82   1.1       cgd #define MBTYPES
     83   1.6   mycroft #include <sys/mbuf.h>
     84   1.6   mycroft #include <sys/kernel.h>
     85   1.6   mycroft #include <sys/syslog.h>
     86   1.6   mycroft #include <sys/domain.h>
     87   1.6   mycroft #include <sys/protosw.h>
     88  1.28   thorpej #include <sys/pool.h>
     89  1.27      matt #include <sys/socket.h>
     90  1.55    simonb #include <sys/sysctl.h>
     91  1.55    simonb 
     92  1.27      matt #include <net/if.h>
     93  1.14  christos 
     94  1.23       mrg #include <uvm/uvm_extern.h>
     95  1.23       mrg 
     96  1.42   thorpej 
     97  1.28   thorpej struct	pool mbpool;		/* mbuf pool */
     98  1.28   thorpej struct	pool mclpool;		/* mbuf cluster pool */
     99  1.28   thorpej 
    100  1.53   thorpej struct pool_cache mbpool_cache;
    101  1.53   thorpej struct pool_cache mclpool_cache;
    102  1.53   thorpej 
    103  1.18   thorpej struct mbstat mbstat;
    104  1.18   thorpej int	max_linkhdr;
    105  1.18   thorpej int	max_protohdr;
    106  1.18   thorpej int	max_hdr;
    107  1.18   thorpej int	max_datalen;
    108  1.18   thorpej 
    109  1.62   thorpej void	*mclpool_alloc(struct pool *, int);
    110  1.62   thorpej void	mclpool_release(struct pool *, void *);
    111  1.58   thorpej 
    112  1.58   thorpej struct pool_allocator mclpool_allocator = {
    113  1.58   thorpej 	mclpool_alloc, mclpool_release, 0,
    114  1.58   thorpej };
    115  1.58   thorpej 
    116  1.44    itojun static struct mbuf *m_copym0 __P((struct mbuf *, int, int, int, int));
    117  1.28   thorpej 
    118  1.57   thorpej const char mclpool_warnmsg[] =
    119  1.42   thorpej     "WARNING: mclpool limit reached; increase NMBCLUSTERS";
    120  1.42   thorpej 
    121  1.28   thorpej /*
    122  1.40   thorpej  * Initialize the mbuf allcator.
    123  1.28   thorpej  */
    124   1.4       jtc void
    125  1.62   thorpej mbinit(void)
    126   1.1       cgd {
    127   1.1       cgd 
    128  1.58   thorpej 	pool_init(&mbpool, msize, 0, 0, 0, "mbpl", NULL);
    129  1.58   thorpej 	pool_init(&mclpool, mclbytes, 0, 0, 0, "mclpl", &mclpool_allocator);
    130  1.53   thorpej 
    131  1.59   thorpej 	pool_set_drain_hook(&mbpool, m_reclaim, NULL);
    132  1.59   thorpej 	pool_set_drain_hook(&mclpool, m_reclaim, NULL);
    133  1.59   thorpej 
    134  1.53   thorpej 	pool_cache_init(&mbpool_cache, &mbpool, NULL, NULL, NULL);
    135  1.53   thorpej 	pool_cache_init(&mclpool_cache, &mclpool, NULL, NULL, NULL);
    136  1.37   thorpej 
    137  1.37   thorpej 	/*
    138  1.39   thorpej 	 * Set the hard limit on the mclpool to the number of
    139  1.39   thorpej 	 * mbuf clusters the kernel is to support.  Log the limit
    140  1.39   thorpej 	 * reached message max once a minute.
    141  1.39   thorpej 	 */
    142  1.42   thorpej 	pool_sethardlimit(&mclpool, nmbclusters, mclpool_warnmsg, 60);
    143  1.42   thorpej 
    144  1.39   thorpej 	/*
    145  1.42   thorpej 	 * Set a low water mark for both mbufs and clusters.  This should
    146  1.42   thorpej 	 * help ensure that they can be allocated in a memory starvation
    147  1.42   thorpej 	 * situation.  This is important for e.g. diskless systems which
    148  1.42   thorpej 	 * must allocate mbufs in order for the pagedaemon to clean pages.
    149  1.37   thorpej 	 */
    150  1.42   thorpej 	pool_setlowat(&mbpool, mblowat);
    151  1.42   thorpej 	pool_setlowat(&mclpool, mcllowat);
    152  1.42   thorpej }
    153  1.42   thorpej 
    154  1.42   thorpej int
    155  1.62   thorpej sysctl_dombuf(int *name, u_int namelen, void *oldp, size_t *oldlenp,
    156  1.62   thorpej     void *newp, size_t newlen)
    157  1.42   thorpej {
    158  1.42   thorpej 	int error, newval;
    159  1.42   thorpej 
    160  1.42   thorpej 	/* All sysctl names at this level are terminal. */
    161  1.42   thorpej 	if (namelen != 1)
    162  1.42   thorpej 		return (ENOTDIR);		/* overloaded */
    163  1.42   thorpej 
    164  1.42   thorpej 	switch (name[0]) {
    165  1.42   thorpej 	case MBUF_MSIZE:
    166  1.42   thorpej 		return (sysctl_rdint(oldp, oldlenp, newp, msize));
    167  1.42   thorpej 	case MBUF_MCLBYTES:
    168  1.42   thorpej 		return (sysctl_rdint(oldp, oldlenp, newp, mclbytes));
    169  1.42   thorpej 	case MBUF_NMBCLUSTERS:
    170  1.42   thorpej 		/*
    171  1.42   thorpej 		 * If we have direct-mapped pool pages, we can adjust this
    172  1.42   thorpej 		 * number on the fly.  If not, we're limited by the size
    173  1.42   thorpej 		 * of mb_map, and cannot change this value.
    174  1.42   thorpej 		 *
    175  1.42   thorpej 		 * Note: we only allow the value to be increased, never
    176  1.42   thorpej 		 * decreased.
    177  1.42   thorpej 		 */
    178  1.42   thorpej 		if (mb_map == NULL) {
    179  1.42   thorpej 			newval = nmbclusters;
    180  1.42   thorpej 			error = sysctl_int(oldp, oldlenp, newp, newlen,
    181  1.42   thorpej 			    &newval);
    182  1.42   thorpej 			if (error != 0)
    183  1.42   thorpej 				return (error);
    184  1.42   thorpej 			if (newp != NULL) {
    185  1.42   thorpej 				if (newval >= nmbclusters) {
    186  1.42   thorpej 					nmbclusters = newval;
    187  1.42   thorpej 					pool_sethardlimit(&mclpool,
    188  1.42   thorpej 					    nmbclusters, mclpool_warnmsg, 60);
    189  1.42   thorpej 				} else
    190  1.42   thorpej 					error = EINVAL;
    191  1.42   thorpej 			}
    192  1.42   thorpej 			return (error);
    193  1.42   thorpej 		} else
    194  1.42   thorpej 			return (sysctl_rdint(oldp, oldlenp, newp, nmbclusters));
    195  1.42   thorpej 	case MBUF_MBLOWAT:
    196  1.42   thorpej 	case MBUF_MCLLOWAT:
    197  1.42   thorpej 		/* New value must be >= 0. */
    198  1.42   thorpej 		newval = (name[0] == MBUF_MBLOWAT) ? mblowat : mcllowat;
    199  1.42   thorpej 		error = sysctl_int(oldp, oldlenp, newp, newlen, &newval);
    200  1.42   thorpej 		if (error != 0)
    201  1.42   thorpej 			return (error);
    202  1.42   thorpej 		if (newp != NULL) {
    203  1.42   thorpej 			if (newval >= 0) {
    204  1.42   thorpej 				if (name[0] == MBUF_MBLOWAT) {
    205  1.42   thorpej 					mblowat = newval;
    206  1.42   thorpej 					pool_setlowat(&mbpool, newval);
    207  1.42   thorpej 				} else {
    208  1.42   thorpej 					mcllowat = newval;
    209  1.42   thorpej 					pool_setlowat(&mclpool, newval);
    210  1.42   thorpej 				}
    211  1.42   thorpej 			} else
    212  1.42   thorpej 				error = EINVAL;
    213  1.42   thorpej 		}
    214  1.42   thorpej 		return (error);
    215  1.42   thorpej 	default:
    216  1.42   thorpej 		return (EOPNOTSUPP);
    217  1.42   thorpej 	}
    218  1.42   thorpej 	/* NOTREACHED */
    219  1.28   thorpej }
    220  1.28   thorpej 
    221  1.28   thorpej void *
    222  1.62   thorpej mclpool_alloc(struct pool *pp, int flags)
    223  1.28   thorpej {
    224  1.32   thorpej 	boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
    225  1.28   thorpej 
    226  1.54       chs 	return ((void *)uvm_km_alloc_poolpage1(mb_map, NULL, waitok));
    227   1.1       cgd }
    228   1.1       cgd 
    229  1.28   thorpej void
    230  1.62   thorpej mclpool_release(struct pool *pp, void *v)
    231   1.1       cgd {
    232   1.1       cgd 
    233  1.31   thorpej 	uvm_km_free_poolpage1(mb_map, (vaddr_t)v);
    234   1.1       cgd }
    235   1.1       cgd 
    236  1.14  christos void
    237  1.59   thorpej m_reclaim(void *arg, int flags)
    238   1.1       cgd {
    239  1.27      matt 	struct domain *dp;
    240  1.27      matt 	struct protosw *pr;
    241  1.27      matt 	struct ifnet *ifp;
    242  1.52   thorpej 	int s = splvm();
    243   1.1       cgd 
    244  1.33   thorpej 	for (dp = domains; dp; dp = dp->dom_next)
    245  1.33   thorpej 		for (pr = dp->dom_protosw;
    246  1.33   thorpej 		     pr < dp->dom_protoswNPROTOSW; pr++)
    247  1.33   thorpej 			if (pr->pr_drain)
    248  1.33   thorpej 				(*pr->pr_drain)();
    249  1.27      matt 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
    250  1.27      matt 		if (ifp->if_drain)
    251  1.27      matt 			(*ifp->if_drain)(ifp);
    252   1.1       cgd 	splx(s);
    253   1.1       cgd 	mbstat.m_drain++;
    254   1.1       cgd }
    255   1.1       cgd 
    256   1.1       cgd /*
    257   1.1       cgd  * Space allocation routines.
    258   1.1       cgd  * These are also available as macros
    259   1.1       cgd  * for critical paths.
    260   1.1       cgd  */
    261   1.1       cgd struct mbuf *
    262  1.62   thorpej m_get(int nowait, int type)
    263   1.1       cgd {
    264  1.27      matt 	struct mbuf *m;
    265   1.1       cgd 
    266   1.5       cgd 	MGET(m, nowait, type);
    267   1.1       cgd 	return (m);
    268   1.1       cgd }
    269   1.1       cgd 
    270   1.1       cgd struct mbuf *
    271  1.62   thorpej m_gethdr(int nowait, int type)
    272   1.1       cgd {
    273  1.27      matt 	struct mbuf *m;
    274   1.1       cgd 
    275   1.5       cgd 	MGETHDR(m, nowait, type);
    276   1.1       cgd 	return (m);
    277   1.1       cgd }
    278   1.1       cgd 
    279   1.1       cgd struct mbuf *
    280  1.62   thorpej m_getclr(int nowait, int type)
    281   1.1       cgd {
    282  1.27      matt 	struct mbuf *m;
    283   1.1       cgd 
    284   1.5       cgd 	MGET(m, nowait, type);
    285   1.1       cgd 	if (m == 0)
    286   1.1       cgd 		return (0);
    287  1.30     perry 	memset(mtod(m, caddr_t), 0, MLEN);
    288   1.1       cgd 	return (m);
    289   1.1       cgd }
    290   1.1       cgd 
    291   1.1       cgd struct mbuf *
    292  1.62   thorpej m_free(struct mbuf *m)
    293   1.1       cgd {
    294  1.27      matt 	struct mbuf *n;
    295   1.1       cgd 
    296   1.1       cgd 	MFREE(m, n);
    297   1.1       cgd 	return (n);
    298   1.1       cgd }
    299   1.1       cgd 
    300   1.9   mycroft void
    301  1.62   thorpej m_freem(struct mbuf *m)
    302   1.1       cgd {
    303  1.27      matt 	struct mbuf *n;
    304   1.1       cgd 
    305   1.1       cgd 	if (m == NULL)
    306   1.1       cgd 		return;
    307   1.1       cgd 	do {
    308   1.1       cgd 		MFREE(m, n);
    309  1.18   thorpej 		m = n;
    310  1.18   thorpej 	} while (m);
    311   1.1       cgd }
    312   1.1       cgd 
    313   1.1       cgd /*
    314   1.1       cgd  * Mbuffer utility routines.
    315   1.1       cgd  */
    316   1.1       cgd 
    317   1.1       cgd /*
    318   1.1       cgd  * Lesser-used path for M_PREPEND:
    319   1.1       cgd  * allocate new mbuf to prepend to chain,
    320   1.1       cgd  * copy junk along.
    321   1.1       cgd  */
    322   1.1       cgd struct mbuf *
    323  1.62   thorpej m_prepend(struct mbuf *m, int len, int how)
    324   1.1       cgd {
    325   1.1       cgd 	struct mbuf *mn;
    326   1.1       cgd 
    327   1.9   mycroft 	MGET(mn, how, m->m_type);
    328   1.1       cgd 	if (mn == (struct mbuf *)NULL) {
    329   1.1       cgd 		m_freem(m);
    330   1.1       cgd 		return ((struct mbuf *)NULL);
    331   1.1       cgd 	}
    332   1.1       cgd 	if (m->m_flags & M_PKTHDR) {
    333   1.1       cgd 		M_COPY_PKTHDR(mn, m);
    334   1.1       cgd 		m->m_flags &= ~M_PKTHDR;
    335   1.1       cgd 	}
    336   1.1       cgd 	mn->m_next = m;
    337   1.1       cgd 	m = mn;
    338   1.1       cgd 	if (len < MHLEN)
    339   1.1       cgd 		MH_ALIGN(m, len);
    340   1.1       cgd 	m->m_len = len;
    341   1.1       cgd 	return (m);
    342   1.1       cgd }
    343   1.1       cgd 
    344   1.1       cgd /*
    345   1.1       cgd  * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
    346   1.1       cgd  * continuing for "len" bytes.  If len is M_COPYALL, copy to end of mbuf.
    347   1.1       cgd  * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
    348   1.1       cgd  */
    349   1.1       cgd int MCFail;
    350   1.1       cgd 
    351   1.1       cgd struct mbuf *
    352  1.62   thorpej m_copym(struct mbuf *m, int off0, int len, int wait)
    353   1.1       cgd {
    354  1.44    itojun 	return m_copym0(m, off0, len, wait, 0);	/* shallow copy on M_EXT */
    355  1.44    itojun }
    356  1.44    itojun 
    357  1.44    itojun struct mbuf *
    358  1.62   thorpej m_dup(struct mbuf *m, int off0, int len, int wait)
    359  1.44    itojun {
    360  1.44    itojun 	return m_copym0(m, off0, len, wait, 1);	/* deep copy */
    361  1.44    itojun }
    362  1.44    itojun 
    363  1.44    itojun static struct mbuf *
    364  1.62   thorpej m_copym0(struct mbuf *m, int off0, int len, int wait, int deep)
    365  1.44    itojun {
    366  1.27      matt 	struct mbuf *n, **np;
    367  1.27      matt 	int off = off0;
    368   1.1       cgd 	struct mbuf *top;
    369   1.1       cgd 	int copyhdr = 0;
    370   1.1       cgd 
    371   1.1       cgd 	if (off < 0 || len < 0)
    372  1.43   thorpej 		panic("m_copym: off %d, len %d", off, len);
    373   1.1       cgd 	if (off == 0 && m->m_flags & M_PKTHDR)
    374   1.1       cgd 		copyhdr = 1;
    375   1.1       cgd 	while (off > 0) {
    376   1.1       cgd 		if (m == 0)
    377  1.43   thorpej 			panic("m_copym: m == 0");
    378   1.1       cgd 		if (off < m->m_len)
    379   1.1       cgd 			break;
    380   1.1       cgd 		off -= m->m_len;
    381   1.1       cgd 		m = m->m_next;
    382   1.1       cgd 	}
    383   1.1       cgd 	np = &top;
    384   1.1       cgd 	top = 0;
    385   1.1       cgd 	while (len > 0) {
    386   1.1       cgd 		if (m == 0) {
    387   1.1       cgd 			if (len != M_COPYALL)
    388  1.43   thorpej 				panic("m_copym: m == 0 and not COPYALL");
    389   1.1       cgd 			break;
    390   1.1       cgd 		}
    391   1.1       cgd 		MGET(n, wait, m->m_type);
    392   1.1       cgd 		*np = n;
    393   1.1       cgd 		if (n == 0)
    394   1.1       cgd 			goto nospace;
    395   1.1       cgd 		if (copyhdr) {
    396   1.1       cgd 			M_COPY_PKTHDR(n, m);
    397   1.1       cgd 			if (len == M_COPYALL)
    398   1.1       cgd 				n->m_pkthdr.len -= off0;
    399   1.1       cgd 			else
    400   1.1       cgd 				n->m_pkthdr.len = len;
    401   1.1       cgd 			copyhdr = 0;
    402   1.1       cgd 		}
    403   1.9   mycroft 		n->m_len = min(len, m->m_len - off);
    404   1.1       cgd 		if (m->m_flags & M_EXT) {
    405  1.44    itojun 			if (!deep) {
    406  1.44    itojun 				n->m_data = m->m_data + off;
    407  1.44    itojun 				n->m_ext = m->m_ext;
    408  1.44    itojun 				MCLADDREFERENCE(m, n);
    409  1.44    itojun 			} else {
    410  1.48    itojun 				/*
    411  1.50    itojun 				 * we are unsure about the way m was allocated.
    412  1.50    itojun 				 * copy into multiple MCLBYTES cluster mbufs.
    413  1.48    itojun 				 */
    414  1.44    itojun 				MCLGET(n, wait);
    415  1.50    itojun 				n->m_len = 0;
    416  1.50    itojun 				n->m_len = M_TRAILINGSPACE(n);
    417  1.50    itojun 				n->m_len = min(n->m_len, len);
    418  1.50    itojun 				n->m_len = min(n->m_len, m->m_len - off);
    419  1.50    itojun 				memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + off,
    420  1.44    itojun 				    (unsigned)n->m_len);
    421  1.44    itojun 			}
    422   1.1       cgd 		} else
    423  1.30     perry 			memcpy(mtod(n, caddr_t), mtod(m, caddr_t)+off,
    424   1.1       cgd 			    (unsigned)n->m_len);
    425   1.1       cgd 		if (len != M_COPYALL)
    426   1.1       cgd 			len -= n->m_len;
    427  1.50    itojun 		off += n->m_len;
    428  1.50    itojun #ifdef DIAGNOSTIC
    429  1.50    itojun 		if (off > m->m_len)
    430  1.50    itojun 			panic("m_copym0 overrun");
    431  1.50    itojun #endif
    432  1.50    itojun 		if (off == m->m_len) {
    433  1.50    itojun 			m = m->m_next;
    434  1.50    itojun 			off = 0;
    435  1.50    itojun 		}
    436   1.1       cgd 		np = &n->m_next;
    437   1.1       cgd 	}
    438   1.1       cgd 	if (top == 0)
    439   1.1       cgd 		MCFail++;
    440   1.1       cgd 	return (top);
    441   1.1       cgd nospace:
    442   1.1       cgd 	m_freem(top);
    443   1.1       cgd 	MCFail++;
    444   1.1       cgd 	return (0);
    445   1.1       cgd }
    446   1.1       cgd 
    447   1.1       cgd /*
    448  1.18   thorpej  * Copy an entire packet, including header (which must be present).
    449  1.18   thorpej  * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'.
    450  1.18   thorpej  */
    451  1.18   thorpej struct mbuf *
    452  1.62   thorpej m_copypacket(struct mbuf *m, int how)
    453  1.18   thorpej {
    454  1.18   thorpej 	struct mbuf *top, *n, *o;
    455  1.18   thorpej 
    456  1.18   thorpej 	MGET(n, how, m->m_type);
    457  1.18   thorpej 	top = n;
    458  1.18   thorpej 	if (!n)
    459  1.18   thorpej 		goto nospace;
    460  1.18   thorpej 
    461  1.18   thorpej 	M_COPY_PKTHDR(n, m);
    462  1.18   thorpej 	n->m_len = m->m_len;
    463  1.18   thorpej 	if (m->m_flags & M_EXT) {
    464  1.18   thorpej 		n->m_data = m->m_data;
    465  1.18   thorpej 		n->m_ext = m->m_ext;
    466  1.18   thorpej 		MCLADDREFERENCE(m, n);
    467  1.18   thorpej 	} else {
    468  1.30     perry 		memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    469  1.18   thorpej 	}
    470  1.18   thorpej 
    471  1.18   thorpej 	m = m->m_next;
    472  1.18   thorpej 	while (m) {
    473  1.18   thorpej 		MGET(o, how, m->m_type);
    474  1.18   thorpej 		if (!o)
    475  1.18   thorpej 			goto nospace;
    476  1.18   thorpej 
    477  1.18   thorpej 		n->m_next = o;
    478  1.18   thorpej 		n = n->m_next;
    479  1.18   thorpej 
    480  1.18   thorpej 		n->m_len = m->m_len;
    481  1.18   thorpej 		if (m->m_flags & M_EXT) {
    482  1.18   thorpej 			n->m_data = m->m_data;
    483  1.18   thorpej 			n->m_ext = m->m_ext;
    484  1.18   thorpej 			MCLADDREFERENCE(m, n);
    485  1.18   thorpej 		} else {
    486  1.30     perry 			memcpy(mtod(n, char *), mtod(m, char *), n->m_len);
    487  1.18   thorpej 		}
    488  1.18   thorpej 
    489  1.18   thorpej 		m = m->m_next;
    490  1.18   thorpej 	}
    491  1.18   thorpej 	return top;
    492  1.18   thorpej nospace:
    493  1.18   thorpej 	m_freem(top);
    494  1.18   thorpej 	MCFail++;
    495  1.18   thorpej 	return 0;
    496  1.18   thorpej }
    497  1.18   thorpej 
    498  1.18   thorpej /*
    499   1.1       cgd  * Copy data from an mbuf chain starting "off" bytes from the beginning,
    500   1.1       cgd  * continuing for "len" bytes, into the indicated buffer.
    501   1.1       cgd  */
    502  1.14  christos void
    503  1.62   thorpej m_copydata(struct mbuf *m, int off, int len, caddr_t cp)
    504   1.1       cgd {
    505  1.27      matt 	unsigned count;
    506   1.1       cgd 
    507   1.1       cgd 	if (off < 0 || len < 0)
    508   1.1       cgd 		panic("m_copydata");
    509   1.1       cgd 	while (off > 0) {
    510   1.1       cgd 		if (m == 0)
    511   1.1       cgd 			panic("m_copydata");
    512   1.1       cgd 		if (off < m->m_len)
    513   1.1       cgd 			break;
    514   1.1       cgd 		off -= m->m_len;
    515   1.1       cgd 		m = m->m_next;
    516   1.1       cgd 	}
    517   1.1       cgd 	while (len > 0) {
    518   1.1       cgd 		if (m == 0)
    519   1.1       cgd 			panic("m_copydata");
    520   1.9   mycroft 		count = min(m->m_len - off, len);
    521  1.30     perry 		memcpy(cp, mtod(m, caddr_t) + off, count);
    522   1.1       cgd 		len -= count;
    523   1.1       cgd 		cp += count;
    524   1.1       cgd 		off = 0;
    525   1.1       cgd 		m = m->m_next;
    526   1.1       cgd 	}
    527   1.1       cgd }
    528   1.1       cgd 
    529   1.1       cgd /*
    530   1.1       cgd  * Concatenate mbuf chain n to m.
    531   1.1       cgd  * Both chains must be of the same type (e.g. MT_DATA).
    532   1.1       cgd  * Any m_pkthdr is not updated.
    533   1.1       cgd  */
    534  1.14  christos void
    535  1.62   thorpej m_cat(struct mbuf *m, struct mbuf *n)
    536   1.1       cgd {
    537   1.1       cgd 	while (m->m_next)
    538   1.1       cgd 		m = m->m_next;
    539   1.1       cgd 	while (n) {
    540   1.1       cgd 		if (m->m_flags & M_EXT ||
    541   1.1       cgd 		    m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) {
    542   1.1       cgd 			/* just join the two chains */
    543   1.1       cgd 			m->m_next = n;
    544   1.1       cgd 			return;
    545   1.1       cgd 		}
    546   1.1       cgd 		/* splat the data from one into the other */
    547  1.30     perry 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    548   1.1       cgd 		    (u_int)n->m_len);
    549   1.1       cgd 		m->m_len += n->m_len;
    550   1.1       cgd 		n = m_free(n);
    551   1.1       cgd 	}
    552   1.1       cgd }
    553   1.1       cgd 
    554  1.11   mycroft void
    555  1.62   thorpej m_adj(struct mbuf *mp, int req_len)
    556   1.1       cgd {
    557  1.27      matt 	int len = req_len;
    558  1.27      matt 	struct mbuf *m;
    559  1.27      matt 	int count;
    560   1.1       cgd 
    561   1.1       cgd 	if ((m = mp) == NULL)
    562   1.1       cgd 		return;
    563   1.1       cgd 	if (len >= 0) {
    564   1.1       cgd 		/*
    565   1.1       cgd 		 * Trim from head.
    566   1.1       cgd 		 */
    567   1.1       cgd 		while (m != NULL && len > 0) {
    568   1.1       cgd 			if (m->m_len <= len) {
    569   1.1       cgd 				len -= m->m_len;
    570   1.1       cgd 				m->m_len = 0;
    571   1.1       cgd 				m = m->m_next;
    572   1.1       cgd 			} else {
    573   1.1       cgd 				m->m_len -= len;
    574   1.1       cgd 				m->m_data += len;
    575   1.1       cgd 				len = 0;
    576   1.1       cgd 			}
    577   1.1       cgd 		}
    578   1.1       cgd 		m = mp;
    579   1.1       cgd 		if (mp->m_flags & M_PKTHDR)
    580   1.1       cgd 			m->m_pkthdr.len -= (req_len - len);
    581   1.1       cgd 	} else {
    582   1.1       cgd 		/*
    583   1.1       cgd 		 * Trim from tail.  Scan the mbuf chain,
    584   1.1       cgd 		 * calculating its length and finding the last mbuf.
    585   1.1       cgd 		 * If the adjustment only affects this mbuf, then just
    586   1.1       cgd 		 * adjust and return.  Otherwise, rescan and truncate
    587   1.1       cgd 		 * after the remaining size.
    588   1.1       cgd 		 */
    589   1.1       cgd 		len = -len;
    590   1.1       cgd 		count = 0;
    591   1.1       cgd 		for (;;) {
    592   1.1       cgd 			count += m->m_len;
    593   1.1       cgd 			if (m->m_next == (struct mbuf *)0)
    594   1.1       cgd 				break;
    595   1.1       cgd 			m = m->m_next;
    596   1.1       cgd 		}
    597   1.1       cgd 		if (m->m_len >= len) {
    598   1.1       cgd 			m->m_len -= len;
    599   1.8   deraadt 			if (mp->m_flags & M_PKTHDR)
    600   1.8   deraadt 				mp->m_pkthdr.len -= len;
    601   1.1       cgd 			return;
    602   1.1       cgd 		}
    603   1.1       cgd 		count -= len;
    604   1.1       cgd 		if (count < 0)
    605   1.1       cgd 			count = 0;
    606   1.1       cgd 		/*
    607   1.1       cgd 		 * Correct length for chain is "count".
    608   1.1       cgd 		 * Find the mbuf with last data, adjust its length,
    609   1.1       cgd 		 * and toss data from remaining mbufs on chain.
    610   1.1       cgd 		 */
    611   1.1       cgd 		m = mp;
    612   1.1       cgd 		if (m->m_flags & M_PKTHDR)
    613   1.1       cgd 			m->m_pkthdr.len = count;
    614   1.1       cgd 		for (; m; m = m->m_next) {
    615   1.1       cgd 			if (m->m_len >= count) {
    616   1.1       cgd 				m->m_len = count;
    617   1.1       cgd 				break;
    618   1.1       cgd 			}
    619   1.1       cgd 			count -= m->m_len;
    620   1.1       cgd 		}
    621  1.18   thorpej 		while (m->m_next)
    622  1.18   thorpej 			(m = m->m_next) ->m_len = 0;
    623   1.1       cgd 	}
    624   1.1       cgd }
    625   1.1       cgd 
    626   1.1       cgd /*
    627   1.1       cgd  * Rearange an mbuf chain so that len bytes are contiguous
    628   1.1       cgd  * and in the data area of an mbuf (so that mtod and dtom
    629   1.1       cgd  * will work for a structure of size len).  Returns the resulting
    630   1.1       cgd  * mbuf chain on success, frees it and returns null on failure.
    631   1.1       cgd  * If there is room, it will add up to max_protohdr-len extra bytes to the
    632   1.1       cgd  * contiguous region in an attempt to avoid being called next time.
    633   1.1       cgd  */
    634   1.1       cgd int MPFail;
    635   1.1       cgd 
    636   1.1       cgd struct mbuf *
    637  1.62   thorpej m_pullup(struct mbuf *n, int len)
    638   1.1       cgd {
    639  1.27      matt 	struct mbuf *m;
    640  1.27      matt 	int count;
    641   1.1       cgd 	int space;
    642   1.1       cgd 
    643   1.1       cgd 	/*
    644   1.1       cgd 	 * If first mbuf has no cluster, and has room for len bytes
    645   1.1       cgd 	 * without shifting current data, pullup into it,
    646   1.1       cgd 	 * otherwise allocate a new mbuf to prepend to the chain.
    647   1.1       cgd 	 */
    648   1.1       cgd 	if ((n->m_flags & M_EXT) == 0 &&
    649   1.1       cgd 	    n->m_data + len < &n->m_dat[MLEN] && n->m_next) {
    650   1.1       cgd 		if (n->m_len >= len)
    651   1.1       cgd 			return (n);
    652   1.1       cgd 		m = n;
    653   1.1       cgd 		n = n->m_next;
    654   1.1       cgd 		len -= m->m_len;
    655   1.1       cgd 	} else {
    656   1.1       cgd 		if (len > MHLEN)
    657   1.1       cgd 			goto bad;
    658   1.1       cgd 		MGET(m, M_DONTWAIT, n->m_type);
    659   1.1       cgd 		if (m == 0)
    660   1.1       cgd 			goto bad;
    661   1.1       cgd 		m->m_len = 0;
    662   1.1       cgd 		if (n->m_flags & M_PKTHDR) {
    663   1.1       cgd 			M_COPY_PKTHDR(m, n);
    664   1.1       cgd 			n->m_flags &= ~M_PKTHDR;
    665   1.1       cgd 		}
    666   1.1       cgd 	}
    667   1.1       cgd 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    668   1.1       cgd 	do {
    669   1.1       cgd 		count = min(min(max(len, max_protohdr), space), n->m_len);
    670  1.30     perry 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    671   1.1       cgd 		  (unsigned)count);
    672   1.1       cgd 		len -= count;
    673   1.1       cgd 		m->m_len += count;
    674   1.1       cgd 		n->m_len -= count;
    675   1.1       cgd 		space -= count;
    676   1.1       cgd 		if (n->m_len)
    677   1.1       cgd 			n->m_data += count;
    678   1.1       cgd 		else
    679   1.1       cgd 			n = m_free(n);
    680   1.1       cgd 	} while (len > 0 && n);
    681   1.1       cgd 	if (len > 0) {
    682   1.1       cgd 		(void) m_free(m);
    683   1.1       cgd 		goto bad;
    684   1.1       cgd 	}
    685   1.1       cgd 	m->m_next = n;
    686   1.1       cgd 	return (m);
    687   1.1       cgd bad:
    688   1.1       cgd 	m_freem(n);
    689   1.1       cgd 	MPFail++;
    690   1.1       cgd 	return (0);
    691  1.60   thorpej }
    692  1.60   thorpej 
    693  1.60   thorpej /*
    694  1.60   thorpej  * Like m_pullup(), except a new mbuf is always allocated, and we allow
    695  1.60   thorpej  * the amount of empty space before the data in the new mbuf to be specified
    696  1.60   thorpej  * (in the event that the caller expects to prepend later).
    697  1.60   thorpej  */
    698  1.60   thorpej int MSFail;
    699  1.60   thorpej 
    700  1.60   thorpej struct mbuf *
    701  1.60   thorpej m_copyup(struct mbuf *n, int len, int dstoff)
    702  1.60   thorpej {
    703  1.60   thorpej 	struct mbuf *m;
    704  1.60   thorpej 	int count, space;
    705  1.60   thorpej 
    706  1.60   thorpej 	if (len > (MHLEN - dstoff))
    707  1.60   thorpej 		goto bad;
    708  1.60   thorpej 	MGET(m, M_DONTWAIT, n->m_type);
    709  1.60   thorpej 	if (m == NULL)
    710  1.60   thorpej 		goto bad;
    711  1.60   thorpej 	m->m_len = 0;
    712  1.60   thorpej 	if (n->m_flags & M_PKTHDR) {
    713  1.60   thorpej 		M_COPY_PKTHDR(m, n);
    714  1.60   thorpej 		n->m_flags &= ~M_PKTHDR;
    715  1.60   thorpej 	}
    716  1.60   thorpej 	m->m_data += dstoff;
    717  1.60   thorpej 	space = &m->m_dat[MLEN] - (m->m_data + m->m_len);
    718  1.60   thorpej 	do {
    719  1.60   thorpej 		count = min(min(max(len, max_protohdr), space), n->m_len);
    720  1.60   thorpej 		memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t),
    721  1.60   thorpej 		    (unsigned)count);
    722  1.60   thorpej 		len -= count;
    723  1.60   thorpej 		m->m_len += count;
    724  1.60   thorpej 		n->m_len -= count;
    725  1.60   thorpej 		space -= count;
    726  1.60   thorpej 		if (n->m_len)
    727  1.60   thorpej 			n->m_data += count;
    728  1.60   thorpej 		else
    729  1.60   thorpej 			n = m_free(n);
    730  1.60   thorpej 	} while (len > 0 && n);
    731  1.60   thorpej 	if (len > 0) {
    732  1.60   thorpej 		(void) m_free(m);
    733  1.60   thorpej 		goto bad;
    734  1.60   thorpej 	}
    735  1.60   thorpej 	m->m_next = n;
    736  1.60   thorpej 	return (m);
    737  1.60   thorpej  bad:
    738  1.60   thorpej 	m_freem(n);
    739  1.60   thorpej 	MSFail++;
    740  1.60   thorpej 	return (NULL);
    741   1.9   mycroft }
    742   1.9   mycroft 
    743   1.9   mycroft /*
    744   1.9   mycroft  * Partition an mbuf chain in two pieces, returning the tail --
    745   1.9   mycroft  * all but the first len0 bytes.  In case of failure, it returns NULL and
    746   1.9   mycroft  * attempts to restore the chain to its original state.
    747   1.9   mycroft  */
    748   1.9   mycroft struct mbuf *
    749  1.62   thorpej m_split(struct mbuf *m0, int len0, int wait)
    750   1.9   mycroft {
    751  1.27      matt 	struct mbuf *m, *n;
    752  1.22   thorpej 	unsigned len = len0, remain, len_save;
    753   1.9   mycroft 
    754   1.9   mycroft 	for (m = m0; m && len > m->m_len; m = m->m_next)
    755   1.9   mycroft 		len -= m->m_len;
    756   1.9   mycroft 	if (m == 0)
    757   1.9   mycroft 		return (0);
    758   1.9   mycroft 	remain = m->m_len - len;
    759   1.9   mycroft 	if (m0->m_flags & M_PKTHDR) {
    760   1.9   mycroft 		MGETHDR(n, wait, m0->m_type);
    761   1.9   mycroft 		if (n == 0)
    762   1.9   mycroft 			return (0);
    763   1.9   mycroft 		n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif;
    764   1.9   mycroft 		n->m_pkthdr.len = m0->m_pkthdr.len - len0;
    765  1.22   thorpej 		len_save = m0->m_pkthdr.len;
    766   1.9   mycroft 		m0->m_pkthdr.len = len0;
    767   1.9   mycroft 		if (m->m_flags & M_EXT)
    768   1.9   mycroft 			goto extpacket;
    769   1.9   mycroft 		if (remain > MHLEN) {
    770   1.9   mycroft 			/* m can't be the lead packet */
    771   1.9   mycroft 			MH_ALIGN(n, 0);
    772   1.9   mycroft 			n->m_next = m_split(m, len, wait);
    773   1.9   mycroft 			if (n->m_next == 0) {
    774   1.9   mycroft 				(void) m_free(n);
    775  1.22   thorpej 				m0->m_pkthdr.len = len_save;
    776   1.9   mycroft 				return (0);
    777   1.9   mycroft 			} else
    778   1.9   mycroft 				return (n);
    779   1.9   mycroft 		} else
    780   1.9   mycroft 			MH_ALIGN(n, remain);
    781   1.9   mycroft 	} else if (remain == 0) {
    782   1.9   mycroft 		n = m->m_next;
    783   1.9   mycroft 		m->m_next = 0;
    784   1.9   mycroft 		return (n);
    785   1.9   mycroft 	} else {
    786   1.9   mycroft 		MGET(n, wait, m->m_type);
    787   1.9   mycroft 		if (n == 0)
    788   1.9   mycroft 			return (0);
    789   1.9   mycroft 		M_ALIGN(n, remain);
    790   1.9   mycroft 	}
    791   1.9   mycroft extpacket:
    792   1.9   mycroft 	if (m->m_flags & M_EXT) {
    793   1.9   mycroft 		n->m_ext = m->m_ext;
    794  1.18   thorpej 		MCLADDREFERENCE(m, n);
    795   1.9   mycroft 		n->m_data = m->m_data + len;
    796   1.9   mycroft 	} else {
    797  1.30     perry 		memcpy(mtod(n, caddr_t), mtod(m, caddr_t) + len, remain);
    798   1.9   mycroft 	}
    799   1.9   mycroft 	n->m_len = remain;
    800   1.9   mycroft 	m->m_len = len;
    801   1.9   mycroft 	n->m_next = m->m_next;
    802   1.9   mycroft 	m->m_next = 0;
    803   1.9   mycroft 	return (n);
    804   1.9   mycroft }
    805   1.9   mycroft /*
    806   1.9   mycroft  * Routine to copy from device local memory into mbufs.
    807   1.9   mycroft  */
    808   1.9   mycroft struct mbuf *
    809  1.62   thorpej m_devget(char *buf, int totlen, int off0, struct ifnet *ifp,
    810  1.62   thorpej     void (*copy)(const void *from, void *to, size_t len))
    811   1.9   mycroft {
    812  1.27      matt 	struct mbuf *m;
    813   1.9   mycroft 	struct mbuf *top = 0, **mp = &top;
    814  1.27      matt 	int off = off0, len;
    815  1.27      matt 	char *cp;
    816   1.9   mycroft 	char *epkt;
    817   1.9   mycroft 
    818   1.9   mycroft 	cp = buf;
    819   1.9   mycroft 	epkt = cp + totlen;
    820   1.9   mycroft 	if (off) {
    821  1.13       cgd 		/*
    822  1.13       cgd 		 * If 'off' is non-zero, packet is trailer-encapsulated,
    823  1.13       cgd 		 * so we have to skip the type and length fields.
    824  1.13       cgd 		 */
    825  1.13       cgd 		cp += off + 2 * sizeof(u_int16_t);
    826  1.13       cgd 		totlen -= 2 * sizeof(u_int16_t);
    827   1.9   mycroft 	}
    828   1.9   mycroft 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    829   1.9   mycroft 	if (m == 0)
    830   1.9   mycroft 		return (0);
    831   1.9   mycroft 	m->m_pkthdr.rcvif = ifp;
    832   1.9   mycroft 	m->m_pkthdr.len = totlen;
    833   1.9   mycroft 	m->m_len = MHLEN;
    834   1.9   mycroft 
    835   1.9   mycroft 	while (totlen > 0) {
    836   1.9   mycroft 		if (top) {
    837   1.9   mycroft 			MGET(m, M_DONTWAIT, MT_DATA);
    838   1.9   mycroft 			if (m == 0) {
    839   1.9   mycroft 				m_freem(top);
    840   1.9   mycroft 				return (0);
    841   1.9   mycroft 			}
    842   1.9   mycroft 			m->m_len = MLEN;
    843   1.9   mycroft 		}
    844   1.9   mycroft 		len = min(totlen, epkt - cp);
    845   1.9   mycroft 		if (len >= MINCLSIZE) {
    846   1.9   mycroft 			MCLGET(m, M_DONTWAIT);
    847  1.19   mycroft 			if ((m->m_flags & M_EXT) == 0) {
    848  1.20   mycroft 				m_free(m);
    849  1.19   mycroft 				m_freem(top);
    850  1.19   mycroft 				return (0);
    851  1.19   mycroft 			}
    852  1.19   mycroft 			m->m_len = len = min(len, MCLBYTES);
    853   1.9   mycroft 		} else {
    854   1.9   mycroft 			/*
    855   1.9   mycroft 			 * Place initial small packet/header at end of mbuf.
    856   1.9   mycroft 			 */
    857   1.9   mycroft 			if (len < m->m_len) {
    858   1.9   mycroft 				if (top == 0 && len + max_linkhdr <= m->m_len)
    859   1.9   mycroft 					m->m_data += max_linkhdr;
    860   1.9   mycroft 				m->m_len = len;
    861   1.9   mycroft 			} else
    862   1.9   mycroft 				len = m->m_len;
    863   1.9   mycroft 		}
    864   1.9   mycroft 		if (copy)
    865  1.14  christos 			copy(cp, mtod(m, caddr_t), (size_t)len);
    866   1.9   mycroft 		else
    867  1.30     perry 			memcpy(mtod(m, caddr_t), cp, (size_t)len);
    868   1.9   mycroft 		cp += len;
    869   1.9   mycroft 		*mp = m;
    870   1.9   mycroft 		mp = &m->m_next;
    871   1.9   mycroft 		totlen -= len;
    872   1.9   mycroft 		if (cp == epkt)
    873   1.9   mycroft 			cp = buf;
    874   1.9   mycroft 	}
    875   1.9   mycroft 	return (top);
    876  1.18   thorpej }
    877  1.18   thorpej 
    878  1.18   thorpej /*
    879  1.18   thorpej  * Copy data from a buffer back into the indicated mbuf chain,
    880  1.18   thorpej  * starting "off" bytes from the beginning, extending the mbuf
    881  1.18   thorpej  * chain if necessary.
    882  1.18   thorpej  */
    883  1.18   thorpej void
    884  1.62   thorpej m_copyback(struct mbuf *m0, int off, int len, caddr_t cp)
    885  1.18   thorpej {
    886  1.27      matt 	int mlen;
    887  1.27      matt 	struct mbuf *m = m0, *n;
    888  1.18   thorpej 	int totlen = 0;
    889  1.18   thorpej 
    890  1.18   thorpej 	if (m0 == 0)
    891  1.18   thorpej 		return;
    892  1.18   thorpej 	while (off > (mlen = m->m_len)) {
    893  1.18   thorpej 		off -= mlen;
    894  1.18   thorpej 		totlen += mlen;
    895  1.18   thorpej 		if (m->m_next == 0) {
    896  1.18   thorpej 			n = m_getclr(M_DONTWAIT, m->m_type);
    897  1.18   thorpej 			if (n == 0)
    898  1.18   thorpej 				goto out;
    899  1.18   thorpej 			n->m_len = min(MLEN, len + off);
    900  1.18   thorpej 			m->m_next = n;
    901  1.18   thorpej 		}
    902  1.18   thorpej 		m = m->m_next;
    903  1.18   thorpej 	}
    904  1.18   thorpej 	while (len > 0) {
    905  1.18   thorpej 		mlen = min (m->m_len - off, len);
    906  1.30     perry 		memcpy(mtod(m, caddr_t) + off, cp, (unsigned)mlen);
    907  1.18   thorpej 		cp += mlen;
    908  1.18   thorpej 		len -= mlen;
    909  1.18   thorpej 		mlen += off;
    910  1.18   thorpej 		off = 0;
    911  1.18   thorpej 		totlen += mlen;
    912  1.18   thorpej 		if (len == 0)
    913  1.18   thorpej 			break;
    914  1.18   thorpej 		if (m->m_next == 0) {
    915  1.18   thorpej 			n = m_get(M_DONTWAIT, m->m_type);
    916  1.18   thorpej 			if (n == 0)
    917  1.18   thorpej 				break;
    918  1.18   thorpej 			n->m_len = min(MLEN, len);
    919  1.18   thorpej 			m->m_next = n;
    920  1.18   thorpej 		}
    921  1.18   thorpej 		m = m->m_next;
    922  1.18   thorpej 	}
    923  1.18   thorpej out:	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
    924  1.18   thorpej 		m->m_pkthdr.len = totlen;
    925   1.1       cgd }
    926