1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33df8bae1dSRodney W. Grimes * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37e32a5b94SRobert Watson #include "opt_mac.h" 38240ef842SDavid E. O'Brien #include "opt_param.h" 39e32a5b94SRobert Watson 40df8bae1dSRodney W. Grimes #include <sys/param.h> 41df8bae1dSRodney W. Grimes #include <sys/systm.h> 42fb919e4dSMark Murray #include <sys/kernel.h> 43fb919e4dSMark Murray #include <sys/lock.h> 44e32a5b94SRobert Watson #include <sys/mac.h> 45f9d0d524SRobert Watson #include <sys/malloc.h> 46df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 47639acc13SGarrett Wollman #include <sys/sysctl.h> 48df8bae1dSRodney W. Grimes #include <sys/domain.h> 49df8bae1dSRodney W. Grimes #include <sys/protosw.h> 50fb919e4dSMark Murray 5128f8db14SBruce Evans int max_linkhdr; 5228f8db14SBruce Evans int max_protohdr; 5328f8db14SBruce Evans int max_hdr; 5428f8db14SBruce Evans int max_datalen; 557d032714SBosko Milekic 567d032714SBosko Milekic /* 577d032714SBosko Milekic * sysctl(8) exported objects 587d032714SBosko Milekic */ 59ce02431fSDoug Rabson SYSCTL_DECL(_kern_ipc); 60639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW, 61639acc13SGarrett Wollman &max_linkhdr, 0, ""); 62639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW, 63639acc13SGarrett Wollman &max_protohdr, 0, ""); 64639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, ""); 65639acc13SGarrett Wollman SYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW, 66639acc13SGarrett Wollman &max_datalen, 0, ""); 67df8bae1dSRodney W. Grimes 68df8bae1dSRodney W. Grimes /* 69e37b1fcdSRobert Watson * Copy mbuf pkthdr from "from" to "to". 70e37b1fcdSRobert Watson * "from" must have M_PKTHDR set, and "to" must be empty. 71e37b1fcdSRobert Watson * aux pointer will be moved to "to". 72e37b1fcdSRobert Watson */ 73e37b1fcdSRobert Watson void 74e37b1fcdSRobert Watson m_copy_pkthdr(struct mbuf *to, struct mbuf *from) 75e37b1fcdSRobert Watson { 76e37b1fcdSRobert Watson 77e37b1fcdSRobert Watson #if 0 78e37b1fcdSRobert Watson KASSERT(to->m_flags & M_PKTHDR, 79e37b1fcdSRobert Watson ("m_copy_pkthdr() called on non-header")); 80e37b1fcdSRobert Watson #endif 81e32a5b94SRobert Watson #ifdef MAC 82e32a5b94SRobert Watson if (to->m_flags & M_PKTHDR) 83e32a5b94SRobert Watson mac_destroy_mbuf(to); 84e32a5b94SRobert Watson #endif 85e37b1fcdSRobert Watson to->m_data = to->m_pktdat; 86e37b1fcdSRobert Watson to->m_flags = from->m_flags & M_COPYFLAGS; 87e37b1fcdSRobert Watson to->m_pkthdr = from->m_pkthdr; 88e32a5b94SRobert Watson #ifdef MAC 89e32a5b94SRobert Watson mac_init_mbuf(to, 1); /* XXXMAC no way to fail */ 90e32a5b94SRobert Watson mac_create_mbuf_from_mbuf(from, to); 91e32a5b94SRobert Watson #endif 92e37b1fcdSRobert Watson from->m_pkthdr.aux = NULL; 93e37b1fcdSRobert Watson } 94e37b1fcdSRobert Watson 95e37b1fcdSRobert Watson /* 96df8bae1dSRodney W. Grimes * Lesser-used path for M_PREPEND: 97df8bae1dSRodney W. Grimes * allocate new mbuf to prepend to chain, 98df8bae1dSRodney W. Grimes * copy junk along. 99df8bae1dSRodney W. Grimes */ 100df8bae1dSRodney W. Grimes struct mbuf * 101122a814aSBosko Milekic m_prepend(struct mbuf *m, int len, int how) 102df8bae1dSRodney W. Grimes { 103df8bae1dSRodney W. Grimes struct mbuf *mn; 104df8bae1dSRodney W. Grimes 105df8bae1dSRodney W. Grimes MGET(mn, how, m->m_type); 106122a814aSBosko Milekic if (mn == NULL) { 107df8bae1dSRodney W. Grimes m_freem(m); 108122a814aSBosko Milekic return (NULL); 109df8bae1dSRodney W. Grimes } 110df8bae1dSRodney W. Grimes if (m->m_flags & M_PKTHDR) { 111df8bae1dSRodney W. Grimes M_COPY_PKTHDR(mn, m); 112e32a5b94SRobert Watson #ifdef MAC 113e32a5b94SRobert Watson mac_destroy_mbuf(m); 114e32a5b94SRobert Watson #endif 115df8bae1dSRodney W. Grimes m->m_flags &= ~M_PKTHDR; 116df8bae1dSRodney W. Grimes } 117df8bae1dSRodney W. Grimes mn->m_next = m; 118df8bae1dSRodney W. Grimes m = mn; 119df8bae1dSRodney W. Grimes if (len < MHLEN) 120df8bae1dSRodney W. Grimes MH_ALIGN(m, len); 121df8bae1dSRodney W. Grimes m->m_len = len; 122df8bae1dSRodney W. Grimes return (m); 123df8bae1dSRodney W. Grimes } 124df8bae1dSRodney W. Grimes 125df8bae1dSRodney W. Grimes /* 126df8bae1dSRodney W. Grimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 127df8bae1dSRodney W. Grimes * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 1282a0c503eSBosko Milekic * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller. 1291c38f2eaSArchie Cobbs * Note that the copy is read-only, because clusters are not copied, 1301c38f2eaSArchie Cobbs * only their reference counts are incremented. 131df8bae1dSRodney W. Grimes */ 132df8bae1dSRodney W. Grimes struct mbuf * 133122a814aSBosko Milekic m_copym(struct mbuf *m, int off0, int len, int wait) 134df8bae1dSRodney W. Grimes { 135122a814aSBosko Milekic struct mbuf *n, **np; 136122a814aSBosko Milekic int off = off0; 137df8bae1dSRodney W. Grimes struct mbuf *top; 138df8bae1dSRodney W. Grimes int copyhdr = 0; 139df8bae1dSRodney W. Grimes 140e0a653ddSAlfred Perlstein KASSERT(off >= 0, ("m_copym, negative off %d", off)); 141e0a653ddSAlfred Perlstein KASSERT(len >= 0, ("m_copym, negative len %d", len)); 142df8bae1dSRodney W. Grimes if (off == 0 && m->m_flags & M_PKTHDR) 143df8bae1dSRodney W. Grimes copyhdr = 1; 144df8bae1dSRodney W. Grimes while (off > 0) { 145e0a653ddSAlfred Perlstein KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); 146df8bae1dSRodney W. Grimes if (off < m->m_len) 147df8bae1dSRodney W. Grimes break; 148df8bae1dSRodney W. Grimes off -= m->m_len; 149df8bae1dSRodney W. Grimes m = m->m_next; 150df8bae1dSRodney W. Grimes } 151df8bae1dSRodney W. Grimes np = ⊤ 152df8bae1dSRodney W. Grimes top = 0; 153df8bae1dSRodney W. Grimes while (len > 0) { 154122a814aSBosko Milekic if (m == NULL) { 155e0a653ddSAlfred Perlstein KASSERT(len == M_COPYALL, 156e0a653ddSAlfred Perlstein ("m_copym, length > size of mbuf chain")); 157df8bae1dSRodney W. Grimes break; 158df8bae1dSRodney W. Grimes } 159df8bae1dSRodney W. Grimes MGET(n, wait, m->m_type); 160df8bae1dSRodney W. Grimes *np = n; 161122a814aSBosko Milekic if (n == NULL) 162df8bae1dSRodney W. Grimes goto nospace; 163df8bae1dSRodney W. Grimes if (copyhdr) { 164df8bae1dSRodney W. Grimes M_COPY_PKTHDR(n, m); 165df8bae1dSRodney W. Grimes if (len == M_COPYALL) 166df8bae1dSRodney W. Grimes n->m_pkthdr.len -= off0; 167df8bae1dSRodney W. Grimes else 168df8bae1dSRodney W. Grimes n->m_pkthdr.len = len; 169df8bae1dSRodney W. Grimes copyhdr = 0; 170df8bae1dSRodney W. Grimes } 171df8bae1dSRodney W. Grimes n->m_len = min(len, m->m_len - off); 172df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) { 173df8bae1dSRodney W. Grimes n->m_data = m->m_data + off; 174df8bae1dSRodney W. Grimes n->m_ext = m->m_ext; 175df8bae1dSRodney W. Grimes n->m_flags |= M_EXT; 176a5c4836dSDavid Malone MEXT_ADD_REF(m); 177df8bae1dSRodney W. Grimes } else 178df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 179bd395ae8SBosko Milekic (u_int)n->m_len); 180df8bae1dSRodney W. Grimes if (len != M_COPYALL) 181df8bae1dSRodney W. Grimes len -= n->m_len; 182df8bae1dSRodney W. Grimes off = 0; 183df8bae1dSRodney W. Grimes m = m->m_next; 184df8bae1dSRodney W. Grimes np = &n->m_next; 185df8bae1dSRodney W. Grimes } 18608442f8aSBosko Milekic if (top == NULL) 18708442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 18808442f8aSBosko Milekic 189df8bae1dSRodney W. Grimes return (top); 190df8bae1dSRodney W. Grimes nospace: 191df8bae1dSRodney W. Grimes m_freem(top); 19208442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 193122a814aSBosko Milekic return (NULL); 194df8bae1dSRodney W. Grimes } 195df8bae1dSRodney W. Grimes 196df8bae1dSRodney W. Grimes /* 1976a06dea0SGarrett Wollman * Copy an entire packet, including header (which must be present). 1986a06dea0SGarrett Wollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 1991c38f2eaSArchie Cobbs * Note that the copy is read-only, because clusters are not copied, 2001c38f2eaSArchie Cobbs * only their reference counts are incremented. 2015fe86675SLuigi Rizzo * Preserve alignment of the first mbuf so if the creator has left 2025fe86675SLuigi Rizzo * some room at the beginning (e.g. for inserting protocol headers) 2035fe86675SLuigi Rizzo * the copies still have the room available. 2046a06dea0SGarrett Wollman */ 2056a06dea0SGarrett Wollman struct mbuf * 206122a814aSBosko Milekic m_copypacket(struct mbuf *m, int how) 2076a06dea0SGarrett Wollman { 2086a06dea0SGarrett Wollman struct mbuf *top, *n, *o; 2096a06dea0SGarrett Wollman 2106a06dea0SGarrett Wollman MGET(n, how, m->m_type); 2116a06dea0SGarrett Wollman top = n; 212122a814aSBosko Milekic if (n == NULL) 2136a06dea0SGarrett Wollman goto nospace; 2146a06dea0SGarrett Wollman 2156a06dea0SGarrett Wollman M_COPY_PKTHDR(n, m); 2166a06dea0SGarrett Wollman n->m_len = m->m_len; 2176a06dea0SGarrett Wollman if (m->m_flags & M_EXT) { 2186a06dea0SGarrett Wollman n->m_data = m->m_data; 2196a06dea0SGarrett Wollman n->m_ext = m->m_ext; 2206a06dea0SGarrett Wollman n->m_flags |= M_EXT; 221a5c4836dSDavid Malone MEXT_ADD_REF(m); 2226a06dea0SGarrett Wollman } else { 2235fe86675SLuigi Rizzo n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat ); 2246a06dea0SGarrett Wollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 2256a06dea0SGarrett Wollman } 2266a06dea0SGarrett Wollman 2276a06dea0SGarrett Wollman m = m->m_next; 2286a06dea0SGarrett Wollman while (m) { 2296a06dea0SGarrett Wollman MGET(o, how, m->m_type); 230122a814aSBosko Milekic if (o == NULL) 2316a06dea0SGarrett Wollman goto nospace; 2326a06dea0SGarrett Wollman 2336a06dea0SGarrett Wollman n->m_next = o; 2346a06dea0SGarrett Wollman n = n->m_next; 2356a06dea0SGarrett Wollman 2366a06dea0SGarrett Wollman n->m_len = m->m_len; 2376a06dea0SGarrett Wollman if (m->m_flags & M_EXT) { 2386a06dea0SGarrett Wollman n->m_data = m->m_data; 2396a06dea0SGarrett Wollman n->m_ext = m->m_ext; 2406a06dea0SGarrett Wollman n->m_flags |= M_EXT; 241a5c4836dSDavid Malone MEXT_ADD_REF(m); 2426a06dea0SGarrett Wollman } else { 2436a06dea0SGarrett Wollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 2446a06dea0SGarrett Wollman } 2456a06dea0SGarrett Wollman 2466a06dea0SGarrett Wollman m = m->m_next; 2476a06dea0SGarrett Wollman } 2486a06dea0SGarrett Wollman return top; 2496a06dea0SGarrett Wollman nospace: 2506a06dea0SGarrett Wollman m_freem(top); 25108442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 252122a814aSBosko Milekic return (NULL); 2536a06dea0SGarrett Wollman } 2546a06dea0SGarrett Wollman 2556a06dea0SGarrett Wollman /* 256df8bae1dSRodney W. Grimes * Copy data from an mbuf chain starting "off" bytes from the beginning, 257df8bae1dSRodney W. Grimes * continuing for "len" bytes, into the indicated buffer. 258df8bae1dSRodney W. Grimes */ 25926f9a767SRodney W. Grimes void 260a8cfc0eeSJulian Elischer m_copydata(const struct mbuf *m, int off, int len, caddr_t cp) 261df8bae1dSRodney W. Grimes { 262bd395ae8SBosko Milekic u_int count; 263df8bae1dSRodney W. Grimes 264e0a653ddSAlfred Perlstein KASSERT(off >= 0, ("m_copydata, negative off %d", off)); 265e0a653ddSAlfred Perlstein KASSERT(len >= 0, ("m_copydata, negative len %d", len)); 266df8bae1dSRodney W. Grimes while (off > 0) { 267e0a653ddSAlfred Perlstein KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); 268df8bae1dSRodney W. Grimes if (off < m->m_len) 269df8bae1dSRodney W. Grimes break; 270df8bae1dSRodney W. Grimes off -= m->m_len; 271df8bae1dSRodney W. Grimes m = m->m_next; 272df8bae1dSRodney W. Grimes } 273df8bae1dSRodney W. Grimes while (len > 0) { 274e0a653ddSAlfred Perlstein KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); 275df8bae1dSRodney W. Grimes count = min(m->m_len - off, len); 276df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t) + off, cp, count); 277df8bae1dSRodney W. Grimes len -= count; 278df8bae1dSRodney W. Grimes cp += count; 279df8bae1dSRodney W. Grimes off = 0; 280df8bae1dSRodney W. Grimes m = m->m_next; 281df8bae1dSRodney W. Grimes } 282df8bae1dSRodney W. Grimes } 283df8bae1dSRodney W. Grimes 284df8bae1dSRodney W. Grimes /* 2851c38f2eaSArchie Cobbs * Copy a packet header mbuf chain into a completely new chain, including 2861c38f2eaSArchie Cobbs * copying any mbuf clusters. Use this instead of m_copypacket() when 2871c38f2eaSArchie Cobbs * you need a writable copy of an mbuf chain. 2881c38f2eaSArchie Cobbs */ 2891c38f2eaSArchie Cobbs struct mbuf * 290122a814aSBosko Milekic m_dup(struct mbuf *m, int how) 2911c38f2eaSArchie Cobbs { 2921c38f2eaSArchie Cobbs struct mbuf **p, *top = NULL; 2931c38f2eaSArchie Cobbs int remain, moff, nsize; 2941c38f2eaSArchie Cobbs 2951c38f2eaSArchie Cobbs /* Sanity check */ 2961c38f2eaSArchie Cobbs if (m == NULL) 297122a814aSBosko Milekic return (NULL); 298a48740b6SDavid E. O'Brien KASSERT((m->m_flags & M_PKTHDR) != 0, ("%s: !PKTHDR", __func__)); 2991c38f2eaSArchie Cobbs 3001c38f2eaSArchie Cobbs /* While there's more data, get a new mbuf, tack it on, and fill it */ 3011c38f2eaSArchie Cobbs remain = m->m_pkthdr.len; 3021c38f2eaSArchie Cobbs moff = 0; 3031c38f2eaSArchie Cobbs p = ⊤ 3041c38f2eaSArchie Cobbs while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */ 3051c38f2eaSArchie Cobbs struct mbuf *n; 3061c38f2eaSArchie Cobbs 3071c38f2eaSArchie Cobbs /* Get the next new mbuf */ 3081c38f2eaSArchie Cobbs MGET(n, how, m->m_type); 3091c38f2eaSArchie Cobbs if (n == NULL) 3101c38f2eaSArchie Cobbs goto nospace; 3111c38f2eaSArchie Cobbs if (top == NULL) { /* first one, must be PKTHDR */ 3121c38f2eaSArchie Cobbs M_COPY_PKTHDR(n, m); 3131c38f2eaSArchie Cobbs nsize = MHLEN; 3141c38f2eaSArchie Cobbs } else /* not the first one */ 3151c38f2eaSArchie Cobbs nsize = MLEN; 3161c38f2eaSArchie Cobbs if (remain >= MINCLSIZE) { 3171c38f2eaSArchie Cobbs MCLGET(n, how); 3181c38f2eaSArchie Cobbs if ((n->m_flags & M_EXT) == 0) { 3191c38f2eaSArchie Cobbs (void)m_free(n); 3201c38f2eaSArchie Cobbs goto nospace; 3211c38f2eaSArchie Cobbs } 3221c38f2eaSArchie Cobbs nsize = MCLBYTES; 3231c38f2eaSArchie Cobbs } 3241c38f2eaSArchie Cobbs n->m_len = 0; 3251c38f2eaSArchie Cobbs 3261c38f2eaSArchie Cobbs /* Link it into the new chain */ 3271c38f2eaSArchie Cobbs *p = n; 3281c38f2eaSArchie Cobbs p = &n->m_next; 3291c38f2eaSArchie Cobbs 3301c38f2eaSArchie Cobbs /* Copy data from original mbuf(s) into new mbuf */ 3311c38f2eaSArchie Cobbs while (n->m_len < nsize && m != NULL) { 3321c38f2eaSArchie Cobbs int chunk = min(nsize - n->m_len, m->m_len - moff); 3331c38f2eaSArchie Cobbs 3341c38f2eaSArchie Cobbs bcopy(m->m_data + moff, n->m_data + n->m_len, chunk); 3351c38f2eaSArchie Cobbs moff += chunk; 3361c38f2eaSArchie Cobbs n->m_len += chunk; 3371c38f2eaSArchie Cobbs remain -= chunk; 3381c38f2eaSArchie Cobbs if (moff == m->m_len) { 3391c38f2eaSArchie Cobbs m = m->m_next; 3401c38f2eaSArchie Cobbs moff = 0; 3411c38f2eaSArchie Cobbs } 3421c38f2eaSArchie Cobbs } 3431c38f2eaSArchie Cobbs 3441c38f2eaSArchie Cobbs /* Check correct total mbuf length */ 3451c38f2eaSArchie Cobbs KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL), 346a48740b6SDavid E. O'Brien ("%s: bogus m_pkthdr.len", __func__)); 3471c38f2eaSArchie Cobbs } 3481c38f2eaSArchie Cobbs return (top); 3491c38f2eaSArchie Cobbs 3501c38f2eaSArchie Cobbs nospace: 3511c38f2eaSArchie Cobbs m_freem(top); 35208442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 353122a814aSBosko Milekic return (NULL); 3541c38f2eaSArchie Cobbs } 3551c38f2eaSArchie Cobbs 3561c38f2eaSArchie Cobbs /* 357df8bae1dSRodney W. Grimes * Concatenate mbuf chain n to m. 358df8bae1dSRodney W. Grimes * Both chains must be of the same type (e.g. MT_DATA). 359df8bae1dSRodney W. Grimes * Any m_pkthdr is not updated. 360df8bae1dSRodney W. Grimes */ 36126f9a767SRodney W. Grimes void 362122a814aSBosko Milekic m_cat(struct mbuf *m, struct mbuf *n) 363df8bae1dSRodney W. Grimes { 364df8bae1dSRodney W. Grimes while (m->m_next) 365df8bae1dSRodney W. Grimes m = m->m_next; 366df8bae1dSRodney W. Grimes while (n) { 367df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT || 368df8bae1dSRodney W. Grimes m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 369df8bae1dSRodney W. Grimes /* just join the two chains */ 370df8bae1dSRodney W. Grimes m->m_next = n; 371df8bae1dSRodney W. Grimes return; 372df8bae1dSRodney W. Grimes } 373df8bae1dSRodney W. Grimes /* splat the data from one into the other */ 374df8bae1dSRodney W. Grimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 375df8bae1dSRodney W. Grimes (u_int)n->m_len); 376df8bae1dSRodney W. Grimes m->m_len += n->m_len; 377df8bae1dSRodney W. Grimes n = m_free(n); 378df8bae1dSRodney W. Grimes } 379df8bae1dSRodney W. Grimes } 380df8bae1dSRodney W. Grimes 38126f9a767SRodney W. Grimes void 382122a814aSBosko Milekic m_adj(struct mbuf *mp, int req_len) 383df8bae1dSRodney W. Grimes { 384122a814aSBosko Milekic int len = req_len; 385122a814aSBosko Milekic struct mbuf *m; 386122a814aSBosko Milekic int count; 387df8bae1dSRodney W. Grimes 388df8bae1dSRodney W. Grimes if ((m = mp) == NULL) 389df8bae1dSRodney W. Grimes return; 390df8bae1dSRodney W. Grimes if (len >= 0) { 391df8bae1dSRodney W. Grimes /* 392df8bae1dSRodney W. Grimes * Trim from head. 393df8bae1dSRodney W. Grimes */ 394df8bae1dSRodney W. Grimes while (m != NULL && len > 0) { 395df8bae1dSRodney W. Grimes if (m->m_len <= len) { 396df8bae1dSRodney W. Grimes len -= m->m_len; 397df8bae1dSRodney W. Grimes m->m_len = 0; 398df8bae1dSRodney W. Grimes m = m->m_next; 399df8bae1dSRodney W. Grimes } else { 400df8bae1dSRodney W. Grimes m->m_len -= len; 401df8bae1dSRodney W. Grimes m->m_data += len; 402df8bae1dSRodney W. Grimes len = 0; 403df8bae1dSRodney W. Grimes } 404df8bae1dSRodney W. Grimes } 405df8bae1dSRodney W. Grimes m = mp; 406df8bae1dSRodney W. Grimes if (mp->m_flags & M_PKTHDR) 407df8bae1dSRodney W. Grimes m->m_pkthdr.len -= (req_len - len); 408df8bae1dSRodney W. Grimes } else { 409df8bae1dSRodney W. Grimes /* 410df8bae1dSRodney W. Grimes * Trim from tail. Scan the mbuf chain, 411df8bae1dSRodney W. Grimes * calculating its length and finding the last mbuf. 412df8bae1dSRodney W. Grimes * If the adjustment only affects this mbuf, then just 413df8bae1dSRodney W. Grimes * adjust and return. Otherwise, rescan and truncate 414df8bae1dSRodney W. Grimes * after the remaining size. 415df8bae1dSRodney W. Grimes */ 416df8bae1dSRodney W. Grimes len = -len; 417df8bae1dSRodney W. Grimes count = 0; 418df8bae1dSRodney W. Grimes for (;;) { 419df8bae1dSRodney W. Grimes count += m->m_len; 420df8bae1dSRodney W. Grimes if (m->m_next == (struct mbuf *)0) 421df8bae1dSRodney W. Grimes break; 422df8bae1dSRodney W. Grimes m = m->m_next; 423df8bae1dSRodney W. Grimes } 424df8bae1dSRodney W. Grimes if (m->m_len >= len) { 425df8bae1dSRodney W. Grimes m->m_len -= len; 426df8bae1dSRodney W. Grimes if (mp->m_flags & M_PKTHDR) 427df8bae1dSRodney W. Grimes mp->m_pkthdr.len -= len; 428df8bae1dSRodney W. Grimes return; 429df8bae1dSRodney W. Grimes } 430df8bae1dSRodney W. Grimes count -= len; 431df8bae1dSRodney W. Grimes if (count < 0) 432df8bae1dSRodney W. Grimes count = 0; 433df8bae1dSRodney W. Grimes /* 434df8bae1dSRodney W. Grimes * Correct length for chain is "count". 435df8bae1dSRodney W. Grimes * Find the mbuf with last data, adjust its length, 436df8bae1dSRodney W. Grimes * and toss data from remaining mbufs on chain. 437df8bae1dSRodney W. Grimes */ 438df8bae1dSRodney W. Grimes m = mp; 439df8bae1dSRodney W. Grimes if (m->m_flags & M_PKTHDR) 440df8bae1dSRodney W. Grimes m->m_pkthdr.len = count; 441df8bae1dSRodney W. Grimes for (; m; m = m->m_next) { 442df8bae1dSRodney W. Grimes if (m->m_len >= count) { 443df8bae1dSRodney W. Grimes m->m_len = count; 444df8bae1dSRodney W. Grimes break; 445df8bae1dSRodney W. Grimes } 446df8bae1dSRodney W. Grimes count -= m->m_len; 447df8bae1dSRodney W. Grimes } 448797f2d22SPoul-Henning Kamp while (m->m_next) 449797f2d22SPoul-Henning Kamp (m = m->m_next) ->m_len = 0; 450df8bae1dSRodney W. Grimes } 451df8bae1dSRodney W. Grimes } 452df8bae1dSRodney W. Grimes 453df8bae1dSRodney W. Grimes /* 454df8bae1dSRodney W. Grimes * Rearange an mbuf chain so that len bytes are contiguous 455df8bae1dSRodney W. Grimes * and in the data area of an mbuf (so that mtod and dtom 456df8bae1dSRodney W. Grimes * will work for a structure of size len). Returns the resulting 457df8bae1dSRodney W. Grimes * mbuf chain on success, frees it and returns null on failure. 458df8bae1dSRodney W. Grimes * If there is room, it will add up to max_protohdr-len extra bytes to the 459df8bae1dSRodney W. Grimes * contiguous region in an attempt to avoid being called next time. 460df8bae1dSRodney W. Grimes */ 461df8bae1dSRodney W. Grimes struct mbuf * 462122a814aSBosko Milekic m_pullup(struct mbuf *n, int len) 463df8bae1dSRodney W. Grimes { 464122a814aSBosko Milekic struct mbuf *m; 465122a814aSBosko Milekic int count; 466df8bae1dSRodney W. Grimes int space; 467df8bae1dSRodney W. Grimes 468df8bae1dSRodney W. Grimes /* 469df8bae1dSRodney W. Grimes * If first mbuf has no cluster, and has room for len bytes 470df8bae1dSRodney W. Grimes * without shifting current data, pullup into it, 471df8bae1dSRodney W. Grimes * otherwise allocate a new mbuf to prepend to the chain. 472df8bae1dSRodney W. Grimes */ 473df8bae1dSRodney W. Grimes if ((n->m_flags & M_EXT) == 0 && 474df8bae1dSRodney W. Grimes n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 475df8bae1dSRodney W. Grimes if (n->m_len >= len) 476df8bae1dSRodney W. Grimes return (n); 477df8bae1dSRodney W. Grimes m = n; 478df8bae1dSRodney W. Grimes n = n->m_next; 479df8bae1dSRodney W. Grimes len -= m->m_len; 480df8bae1dSRodney W. Grimes } else { 481df8bae1dSRodney W. Grimes if (len > MHLEN) 482df8bae1dSRodney W. Grimes goto bad; 483df8bae1dSRodney W. Grimes MGET(m, M_DONTWAIT, n->m_type); 484122a814aSBosko Milekic if (m == NULL) 485df8bae1dSRodney W. Grimes goto bad; 486df8bae1dSRodney W. Grimes m->m_len = 0; 487df8bae1dSRodney W. Grimes if (n->m_flags & M_PKTHDR) { 488df8bae1dSRodney W. Grimes M_COPY_PKTHDR(m, n); 489df8bae1dSRodney W. Grimes n->m_flags &= ~M_PKTHDR; 490df8bae1dSRodney W. Grimes } 491df8bae1dSRodney W. Grimes } 492df8bae1dSRodney W. Grimes space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 493df8bae1dSRodney W. Grimes do { 494df8bae1dSRodney W. Grimes count = min(min(max(len, max_protohdr), space), n->m_len); 495df8bae1dSRodney W. Grimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 496bd395ae8SBosko Milekic (u_int)count); 497df8bae1dSRodney W. Grimes len -= count; 498df8bae1dSRodney W. Grimes m->m_len += count; 499df8bae1dSRodney W. Grimes n->m_len -= count; 500df8bae1dSRodney W. Grimes space -= count; 501df8bae1dSRodney W. Grimes if (n->m_len) 502df8bae1dSRodney W. Grimes n->m_data += count; 503df8bae1dSRodney W. Grimes else 504df8bae1dSRodney W. Grimes n = m_free(n); 505df8bae1dSRodney W. Grimes } while (len > 0 && n); 506df8bae1dSRodney W. Grimes if (len > 0) { 507df8bae1dSRodney W. Grimes (void) m_free(m); 508df8bae1dSRodney W. Grimes goto bad; 509df8bae1dSRodney W. Grimes } 510df8bae1dSRodney W. Grimes m->m_next = n; 511df8bae1dSRodney W. Grimes return (m); 512df8bae1dSRodney W. Grimes bad: 513df8bae1dSRodney W. Grimes m_freem(n); 51408442f8aSBosko Milekic mbstat.m_mpfail++; /* XXX: No consistency. */ 515122a814aSBosko Milekic return (NULL); 516df8bae1dSRodney W. Grimes } 517df8bae1dSRodney W. Grimes 518df8bae1dSRodney W. Grimes /* 519df8bae1dSRodney W. Grimes * Partition an mbuf chain in two pieces, returning the tail -- 520df8bae1dSRodney W. Grimes * all but the first len0 bytes. In case of failure, it returns NULL and 521df8bae1dSRodney W. Grimes * attempts to restore the chain to its original state. 52248d183faSArchie Cobbs * 52348d183faSArchie Cobbs * Note that the resulting mbufs might be read-only, because the new 52448d183faSArchie Cobbs * mbuf can end up sharing an mbuf cluster with the original mbuf if 52548d183faSArchie Cobbs * the "breaking point" happens to lie within a cluster mbuf. Use the 52648d183faSArchie Cobbs * M_WRITABLE() macro to check for this case. 527df8bae1dSRodney W. Grimes */ 528df8bae1dSRodney W. Grimes struct mbuf * 529122a814aSBosko Milekic m_split(struct mbuf *m0, int len0, int wait) 530df8bae1dSRodney W. Grimes { 531122a814aSBosko Milekic struct mbuf *m, *n; 532bd395ae8SBosko Milekic u_int len = len0, remain; 533df8bae1dSRodney W. Grimes 534df8bae1dSRodney W. Grimes for (m = m0; m && len > m->m_len; m = m->m_next) 535df8bae1dSRodney W. Grimes len -= m->m_len; 536122a814aSBosko Milekic if (m == NULL) 537122a814aSBosko Milekic return (NULL); 538df8bae1dSRodney W. Grimes remain = m->m_len - len; 539df8bae1dSRodney W. Grimes if (m0->m_flags & M_PKTHDR) { 540df8bae1dSRodney W. Grimes MGETHDR(n, wait, m0->m_type); 541122a814aSBosko Milekic if (n == NULL) 542122a814aSBosko Milekic return (NULL); 543df8bae1dSRodney W. Grimes n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 544df8bae1dSRodney W. Grimes n->m_pkthdr.len = m0->m_pkthdr.len - len0; 545df8bae1dSRodney W. Grimes m0->m_pkthdr.len = len0; 546df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) 547df8bae1dSRodney W. Grimes goto extpacket; 548df8bae1dSRodney W. Grimes if (remain > MHLEN) { 549df8bae1dSRodney W. Grimes /* m can't be the lead packet */ 550df8bae1dSRodney W. Grimes MH_ALIGN(n, 0); 551df8bae1dSRodney W. Grimes n->m_next = m_split(m, len, wait); 552122a814aSBosko Milekic if (n->m_next == NULL) { 553df8bae1dSRodney W. Grimes (void) m_free(n); 554122a814aSBosko Milekic return (NULL); 55540376987SJeffrey Hsu } else { 55640376987SJeffrey Hsu n->m_len = 0; 557df8bae1dSRodney W. Grimes return (n); 55840376987SJeffrey Hsu } 559df8bae1dSRodney W. Grimes } else 560df8bae1dSRodney W. Grimes MH_ALIGN(n, remain); 561df8bae1dSRodney W. Grimes } else if (remain == 0) { 562df8bae1dSRodney W. Grimes n = m->m_next; 563122a814aSBosko Milekic m->m_next = NULL; 564df8bae1dSRodney W. Grimes return (n); 565df8bae1dSRodney W. Grimes } else { 566df8bae1dSRodney W. Grimes MGET(n, wait, m->m_type); 567122a814aSBosko Milekic if (n == NULL) 568122a814aSBosko Milekic return (NULL); 569df8bae1dSRodney W. Grimes M_ALIGN(n, remain); 570df8bae1dSRodney W. Grimes } 571df8bae1dSRodney W. Grimes extpacket: 572df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) { 573df8bae1dSRodney W. Grimes n->m_flags |= M_EXT; 574df8bae1dSRodney W. Grimes n->m_ext = m->m_ext; 575a5c4836dSDavid Malone MEXT_ADD_REF(m); 576df8bae1dSRodney W. Grimes n->m_data = m->m_data + len; 577df8bae1dSRodney W. Grimes } else { 578df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 579df8bae1dSRodney W. Grimes } 580df8bae1dSRodney W. Grimes n->m_len = remain; 581df8bae1dSRodney W. Grimes m->m_len = len; 582df8bae1dSRodney W. Grimes n->m_next = m->m_next; 583122a814aSBosko Milekic m->m_next = NULL; 584df8bae1dSRodney W. Grimes return (n); 585df8bae1dSRodney W. Grimes } 586df8bae1dSRodney W. Grimes /* 587df8bae1dSRodney W. Grimes * Routine to copy from device local memory into mbufs. 588f5eece3fSBosko Milekic * Note that `off' argument is offset into first mbuf of target chain from 589f5eece3fSBosko Milekic * which to begin copying the data to. 590df8bae1dSRodney W. Grimes */ 591df8bae1dSRodney W. Grimes struct mbuf * 592f5eece3fSBosko Milekic m_devget(char *buf, int totlen, int off, struct ifnet *ifp, 593122a814aSBosko Milekic void (*copy)(char *from, caddr_t to, u_int len)) 594df8bae1dSRodney W. Grimes { 595122a814aSBosko Milekic struct mbuf *m; 596df8bae1dSRodney W. Grimes struct mbuf *top = 0, **mp = ⊤ 597f5eece3fSBosko Milekic int len; 598df8bae1dSRodney W. Grimes 599f5eece3fSBosko Milekic if (off < 0 || off > MHLEN) 600f5eece3fSBosko Milekic return (NULL); 601f5eece3fSBosko Milekic 602df8bae1dSRodney W. Grimes MGETHDR(m, M_DONTWAIT, MT_DATA); 603122a814aSBosko Milekic if (m == NULL) 604122a814aSBosko Milekic return (NULL); 605df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = ifp; 606df8bae1dSRodney W. Grimes m->m_pkthdr.len = totlen; 607f5eece3fSBosko Milekic len = MHLEN; 608df8bae1dSRodney W. Grimes 609df8bae1dSRodney W. Grimes while (totlen > 0) { 610df8bae1dSRodney W. Grimes if (top) { 611df8bae1dSRodney W. Grimes MGET(m, M_DONTWAIT, MT_DATA); 612122a814aSBosko Milekic if (m == NULL) { 613df8bae1dSRodney W. Grimes m_freem(top); 614122a814aSBosko Milekic return (NULL); 615df8bae1dSRodney W. Grimes } 616f5eece3fSBosko Milekic len = MLEN; 617df8bae1dSRodney W. Grimes } 618f5eece3fSBosko Milekic if (totlen + off >= MINCLSIZE) { 619df8bae1dSRodney W. Grimes MCLGET(m, M_DONTWAIT); 620df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) 621f5eece3fSBosko Milekic len = MCLBYTES; 622df8bae1dSRodney W. Grimes } else { 623df8bae1dSRodney W. Grimes /* 624df8bae1dSRodney W. Grimes * Place initial small packet/header at end of mbuf. 625df8bae1dSRodney W. Grimes */ 626f5eece3fSBosko Milekic if (top == NULL && totlen + off + max_linkhdr <= len) { 627df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 628f5eece3fSBosko Milekic len -= max_linkhdr; 629df8bae1dSRodney W. Grimes } 630f5eece3fSBosko Milekic } 631f5eece3fSBosko Milekic if (off) { 632f5eece3fSBosko Milekic m->m_data += off; 633f5eece3fSBosko Milekic len -= off; 634f5eece3fSBosko Milekic off = 0; 635f5eece3fSBosko Milekic } 636f5eece3fSBosko Milekic m->m_len = len = min(totlen, len); 637df8bae1dSRodney W. Grimes if (copy) 638bd395ae8SBosko Milekic copy(buf, mtod(m, caddr_t), (u_int)len); 639df8bae1dSRodney W. Grimes else 640bd395ae8SBosko Milekic bcopy(buf, mtod(m, caddr_t), (u_int)len); 641f5eece3fSBosko Milekic buf += len; 642df8bae1dSRodney W. Grimes *mp = m; 643df8bae1dSRodney W. Grimes mp = &m->m_next; 644df8bae1dSRodney W. Grimes totlen -= len; 645df8bae1dSRodney W. Grimes } 646df8bae1dSRodney W. Grimes return (top); 647df8bae1dSRodney W. Grimes } 648c5789ba3SPoul-Henning Kamp 649c5789ba3SPoul-Henning Kamp /* 650c5789ba3SPoul-Henning Kamp * Copy data from a buffer back into the indicated mbuf chain, 651c5789ba3SPoul-Henning Kamp * starting "off" bytes from the beginning, extending the mbuf 652c5789ba3SPoul-Henning Kamp * chain if necessary. 653c5789ba3SPoul-Henning Kamp */ 654c5789ba3SPoul-Henning Kamp void 655122a814aSBosko Milekic m_copyback(struct mbuf *m0, int off, int len, caddr_t cp) 656c5789ba3SPoul-Henning Kamp { 657122a814aSBosko Milekic int mlen; 658122a814aSBosko Milekic struct mbuf *m = m0, *n; 659c5789ba3SPoul-Henning Kamp int totlen = 0; 660c5789ba3SPoul-Henning Kamp 661122a814aSBosko Milekic if (m0 == NULL) 662c5789ba3SPoul-Henning Kamp return; 663c5789ba3SPoul-Henning Kamp while (off > (mlen = m->m_len)) { 664c5789ba3SPoul-Henning Kamp off -= mlen; 665c5789ba3SPoul-Henning Kamp totlen += mlen; 666122a814aSBosko Milekic if (m->m_next == NULL) { 66708442f8aSBosko Milekic n = m_get_clrd(M_DONTWAIT, m->m_type); 668122a814aSBosko Milekic if (n == NULL) 669c5789ba3SPoul-Henning Kamp goto out; 670c5789ba3SPoul-Henning Kamp n->m_len = min(MLEN, len + off); 671c5789ba3SPoul-Henning Kamp m->m_next = n; 672c5789ba3SPoul-Henning Kamp } 673c5789ba3SPoul-Henning Kamp m = m->m_next; 674c5789ba3SPoul-Henning Kamp } 675c5789ba3SPoul-Henning Kamp while (len > 0) { 676c5789ba3SPoul-Henning Kamp mlen = min (m->m_len - off, len); 677bd395ae8SBosko Milekic bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen); 678c5789ba3SPoul-Henning Kamp cp += mlen; 679c5789ba3SPoul-Henning Kamp len -= mlen; 680c5789ba3SPoul-Henning Kamp mlen += off; 681c5789ba3SPoul-Henning Kamp off = 0; 682c5789ba3SPoul-Henning Kamp totlen += mlen; 683c5789ba3SPoul-Henning Kamp if (len == 0) 684c5789ba3SPoul-Henning Kamp break; 685122a814aSBosko Milekic if (m->m_next == NULL) { 686c5789ba3SPoul-Henning Kamp n = m_get(M_DONTWAIT, m->m_type); 687122a814aSBosko Milekic if (n == NULL) 688c5789ba3SPoul-Henning Kamp break; 689c5789ba3SPoul-Henning Kamp n->m_len = min(MLEN, len); 690c5789ba3SPoul-Henning Kamp m->m_next = n; 691c5789ba3SPoul-Henning Kamp } 692c5789ba3SPoul-Henning Kamp m = m->m_next; 693c5789ba3SPoul-Henning Kamp } 694c5789ba3SPoul-Henning Kamp out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 695c5789ba3SPoul-Henning Kamp m->m_pkthdr.len = totlen; 696c5789ba3SPoul-Henning Kamp } 697ce4a64f7SPoul-Henning Kamp 698ce4a64f7SPoul-Henning Kamp void 699ce4a64f7SPoul-Henning Kamp m_print(const struct mbuf *m) 700ce4a64f7SPoul-Henning Kamp { 701ce4a64f7SPoul-Henning Kamp int len; 7026357e7b5SEivind Eklund const struct mbuf *m2; 703ce4a64f7SPoul-Henning Kamp 704ce4a64f7SPoul-Henning Kamp len = m->m_pkthdr.len; 705ce4a64f7SPoul-Henning Kamp m2 = m; 706ce4a64f7SPoul-Henning Kamp while (len) { 707ce4a64f7SPoul-Henning Kamp printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-"); 708ce4a64f7SPoul-Henning Kamp len -= m2->m_len; 709ce4a64f7SPoul-Henning Kamp m2 = m2->m_next; 710ce4a64f7SPoul-Henning Kamp } 711ce4a64f7SPoul-Henning Kamp return; 712ce4a64f7SPoul-Henning Kamp } 7133f2e06c5SPoul-Henning Kamp 714bd395ae8SBosko Milekic u_int 7153f2e06c5SPoul-Henning Kamp m_fixhdr(struct mbuf *m0) 7163f2e06c5SPoul-Henning Kamp { 717bd395ae8SBosko Milekic u_int len; 7183f2e06c5SPoul-Henning Kamp 719ac6e585dSPoul-Henning Kamp len = m_length(m0, NULL); 7203f2e06c5SPoul-Henning Kamp m0->m_pkthdr.len = len; 721ac6e585dSPoul-Henning Kamp return (len); 722ac6e585dSPoul-Henning Kamp } 723ac6e585dSPoul-Henning Kamp 724bd395ae8SBosko Milekic u_int 725ac6e585dSPoul-Henning Kamp m_length(struct mbuf *m0, struct mbuf **last) 726ac6e585dSPoul-Henning Kamp { 727ac6e585dSPoul-Henning Kamp struct mbuf *m; 728bd395ae8SBosko Milekic u_int len; 729ac6e585dSPoul-Henning Kamp 730ac6e585dSPoul-Henning Kamp len = 0; 731ac6e585dSPoul-Henning Kamp for (m = m0; m != NULL; m = m->m_next) { 732ac6e585dSPoul-Henning Kamp len += m->m_len; 733ac6e585dSPoul-Henning Kamp if (m->m_next == NULL) 734ac6e585dSPoul-Henning Kamp break; 735ac6e585dSPoul-Henning Kamp } 736ac6e585dSPoul-Henning Kamp if (last != NULL) 737ac6e585dSPoul-Henning Kamp *last = m; 738ac6e585dSPoul-Henning Kamp return (len); 7393f2e06c5SPoul-Henning Kamp } 740