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 /* 699967cafcSSam Leffler * "Move" mbuf pkthdr from "from" to "to". 70e37b1fcdSRobert Watson * "from" must have M_PKTHDR set, and "to" must be empty. 71e37b1fcdSRobert Watson */ 72e37b1fcdSRobert Watson void 739967cafcSSam Leffler m_move_pkthdr(struct mbuf *to, struct mbuf *from) 74e37b1fcdSRobert Watson { 75e37b1fcdSRobert Watson 76e37b1fcdSRobert Watson #if 0 779967cafcSSam Leffler /* see below for why these are not enabled */ 78e37b1fcdSRobert Watson KASSERT(to->m_flags & M_PKTHDR, 799967cafcSSam Leffler ("m_move_pkthdr: called on non-header")); 809967cafcSSam Leffler KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), 819967cafcSSam Leffler ("m_move_pkthdr: to has tags")); 82e37b1fcdSRobert Watson #endif 839967cafcSSam Leffler KASSERT((to->m_flags & M_EXT) == 0, ("m_move_pkthdr: to has cluster")); 84e32a5b94SRobert Watson #ifdef MAC 85e32a5b94SRobert Watson if (to->m_flags & M_PKTHDR) 86e32a5b94SRobert Watson mac_destroy_mbuf(to); 87e32a5b94SRobert Watson #endif 88e37b1fcdSRobert Watson to->m_flags = from->m_flags & M_COPYFLAGS; 899967cafcSSam Leffler to->m_data = to->m_pktdat; 909967cafcSSam Leffler to->m_pkthdr = from->m_pkthdr; /* especially tags */ 919967cafcSSam Leffler #ifdef MAC 929967cafcSSam Leffler mac_init_mbuf(to, 1); /* XXXMAC no way to fail */ 939967cafcSSam Leffler mac_create_mbuf_from_mbuf(from, to); 949967cafcSSam Leffler #endif 959967cafcSSam Leffler SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */ 969967cafcSSam Leffler from->m_flags &= ~M_PKTHDR; 979967cafcSSam Leffler } 989967cafcSSam Leffler 999967cafcSSam Leffler /* 1009967cafcSSam Leffler * Duplicate "from"'s mbuf pkthdr in "to". 1019967cafcSSam Leffler * "from" must have M_PKTHDR set, and "to" must be empty. 1029967cafcSSam Leffler * In particular, this does a deep copy of the packet tags. 1039967cafcSSam Leffler */ 1049967cafcSSam Leffler int 1059967cafcSSam Leffler m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how) 1069967cafcSSam Leffler { 1079967cafcSSam Leffler 1089967cafcSSam Leffler #if 0 1099967cafcSSam Leffler /* 1109967cafcSSam Leffler * The mbuf allocator only initializes the pkthdr 1119967cafcSSam Leffler * when the mbuf is allocated with MGETHDR. Many users 1129967cafcSSam Leffler * (e.g. m_copy*, m_prepend) use MGET and then 1139967cafcSSam Leffler * smash the pkthdr as needed causing these 1149967cafcSSam Leffler * assertions to trip. For now just disable them. 1159967cafcSSam Leffler */ 1169967cafcSSam Leffler KASSERT(to->m_flags & M_PKTHDR, ("m_dup_pkthdr: called on non-header")); 1179967cafcSSam Leffler KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags")); 1189967cafcSSam Leffler #endif 1199967cafcSSam Leffler #ifdef MAC 1209967cafcSSam Leffler if (to->m_flags & M_PKTHDR) 1219967cafcSSam Leffler mac_destroy_mbuf(to); 1229967cafcSSam Leffler #endif 123df8c7fc9SMike Silbersack to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 124df8c7fc9SMike Silbersack if ((to->m_flags & M_EXT) == 0) 1259967cafcSSam Leffler to->m_data = to->m_pktdat; 126e37b1fcdSRobert Watson to->m_pkthdr = from->m_pkthdr; 127e32a5b94SRobert Watson #ifdef MAC 128e32a5b94SRobert Watson mac_init_mbuf(to, 1); /* XXXMAC no way to fail */ 129e32a5b94SRobert Watson mac_create_mbuf_from_mbuf(from, to); 130e32a5b94SRobert Watson #endif 1319967cafcSSam Leffler SLIST_INIT(&to->m_pkthdr.tags); 132a80cc4e1SIan Dowse return (m_tag_copy_chain(to, from, (how & M_TRYWAIT) ? M_WAITOK : 133a80cc4e1SIan Dowse M_NOWAIT)); 134e37b1fcdSRobert Watson } 135e37b1fcdSRobert Watson 136e37b1fcdSRobert Watson /* 137df8bae1dSRodney W. Grimes * Lesser-used path for M_PREPEND: 138df8bae1dSRodney W. Grimes * allocate new mbuf to prepend to chain, 139df8bae1dSRodney W. Grimes * copy junk along. 140df8bae1dSRodney W. Grimes */ 141df8bae1dSRodney W. Grimes struct mbuf * 142122a814aSBosko Milekic m_prepend(struct mbuf *m, int len, int how) 143df8bae1dSRodney W. Grimes { 144df8bae1dSRodney W. Grimes struct mbuf *mn; 145df8bae1dSRodney W. Grimes 146df8bae1dSRodney W. Grimes MGET(mn, how, m->m_type); 147122a814aSBosko Milekic if (mn == NULL) { 148df8bae1dSRodney W. Grimes m_freem(m); 149122a814aSBosko Milekic return (NULL); 150df8bae1dSRodney W. Grimes } 151df8bae1dSRodney W. Grimes if (m->m_flags & M_PKTHDR) { 1529967cafcSSam Leffler M_MOVE_PKTHDR(mn, m); 153e32a5b94SRobert Watson #ifdef MAC 154e32a5b94SRobert Watson mac_destroy_mbuf(m); 155e32a5b94SRobert Watson #endif 156df8bae1dSRodney W. Grimes } 157df8bae1dSRodney W. Grimes mn->m_next = m; 158df8bae1dSRodney W. Grimes m = mn; 159df8bae1dSRodney W. Grimes if (len < MHLEN) 160df8bae1dSRodney W. Grimes MH_ALIGN(m, len); 161df8bae1dSRodney W. Grimes m->m_len = len; 162df8bae1dSRodney W. Grimes return (m); 163df8bae1dSRodney W. Grimes } 164df8bae1dSRodney W. Grimes 165df8bae1dSRodney W. Grimes /* 166df8bae1dSRodney W. Grimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 167df8bae1dSRodney W. Grimes * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 168a163d034SWarner Losh * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller. 1691c38f2eaSArchie Cobbs * Note that the copy is read-only, because clusters are not copied, 1701c38f2eaSArchie Cobbs * only their reference counts are incremented. 171df8bae1dSRodney W. Grimes */ 172df8bae1dSRodney W. Grimes struct mbuf * 173122a814aSBosko Milekic m_copym(struct mbuf *m, int off0, int len, int wait) 174df8bae1dSRodney W. Grimes { 175122a814aSBosko Milekic struct mbuf *n, **np; 176122a814aSBosko Milekic int off = off0; 177df8bae1dSRodney W. Grimes struct mbuf *top; 178df8bae1dSRodney W. Grimes int copyhdr = 0; 179df8bae1dSRodney W. Grimes 180e0a653ddSAlfred Perlstein KASSERT(off >= 0, ("m_copym, negative off %d", off)); 181e0a653ddSAlfred Perlstein KASSERT(len >= 0, ("m_copym, negative len %d", len)); 182df8bae1dSRodney W. Grimes if (off == 0 && m->m_flags & M_PKTHDR) 183df8bae1dSRodney W. Grimes copyhdr = 1; 184df8bae1dSRodney W. Grimes while (off > 0) { 185e0a653ddSAlfred Perlstein KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); 186df8bae1dSRodney W. Grimes if (off < m->m_len) 187df8bae1dSRodney W. Grimes break; 188df8bae1dSRodney W. Grimes off -= m->m_len; 189df8bae1dSRodney W. Grimes m = m->m_next; 190df8bae1dSRodney W. Grimes } 191df8bae1dSRodney W. Grimes np = ⊤ 192df8bae1dSRodney W. Grimes top = 0; 193df8bae1dSRodney W. Grimes while (len > 0) { 194122a814aSBosko Milekic if (m == NULL) { 195e0a653ddSAlfred Perlstein KASSERT(len == M_COPYALL, 196e0a653ddSAlfred Perlstein ("m_copym, length > size of mbuf chain")); 197df8bae1dSRodney W. Grimes break; 198df8bae1dSRodney W. Grimes } 199df8bae1dSRodney W. Grimes MGET(n, wait, m->m_type); 200df8bae1dSRodney W. Grimes *np = n; 201122a814aSBosko Milekic if (n == NULL) 202df8bae1dSRodney W. Grimes goto nospace; 203df8bae1dSRodney W. Grimes if (copyhdr) { 2049967cafcSSam Leffler if (!m_dup_pkthdr(n, m, wait)) 2059967cafcSSam Leffler goto nospace; 206df8bae1dSRodney W. Grimes if (len == M_COPYALL) 207df8bae1dSRodney W. Grimes n->m_pkthdr.len -= off0; 208df8bae1dSRodney W. Grimes else 209df8bae1dSRodney W. Grimes n->m_pkthdr.len = len; 210df8bae1dSRodney W. Grimes copyhdr = 0; 211df8bae1dSRodney W. Grimes } 212df8bae1dSRodney W. Grimes n->m_len = min(len, m->m_len - off); 213df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) { 214df8bae1dSRodney W. Grimes n->m_data = m->m_data + off; 215df8bae1dSRodney W. Grimes n->m_ext = m->m_ext; 216df8bae1dSRodney W. Grimes n->m_flags |= M_EXT; 217a5c4836dSDavid Malone MEXT_ADD_REF(m); 218df8bae1dSRodney W. Grimes } else 219df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 220bd395ae8SBosko Milekic (u_int)n->m_len); 221df8bae1dSRodney W. Grimes if (len != M_COPYALL) 222df8bae1dSRodney W. Grimes len -= n->m_len; 223df8bae1dSRodney W. Grimes off = 0; 224df8bae1dSRodney W. Grimes m = m->m_next; 225df8bae1dSRodney W. Grimes np = &n->m_next; 226df8bae1dSRodney W. Grimes } 22708442f8aSBosko Milekic if (top == NULL) 22808442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 22908442f8aSBosko Milekic 230df8bae1dSRodney W. Grimes return (top); 231df8bae1dSRodney W. Grimes nospace: 232df8bae1dSRodney W. Grimes m_freem(top); 23308442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 234122a814aSBosko Milekic return (NULL); 235df8bae1dSRodney W. Grimes } 236df8bae1dSRodney W. Grimes 237df8bae1dSRodney W. Grimes /* 2386a06dea0SGarrett Wollman * Copy an entire packet, including header (which must be present). 2396a06dea0SGarrett Wollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 2401c38f2eaSArchie Cobbs * Note that the copy is read-only, because clusters are not copied, 2411c38f2eaSArchie Cobbs * only their reference counts are incremented. 2425fe86675SLuigi Rizzo * Preserve alignment of the first mbuf so if the creator has left 2435fe86675SLuigi Rizzo * some room at the beginning (e.g. for inserting protocol headers) 2445fe86675SLuigi Rizzo * the copies still have the room available. 2456a06dea0SGarrett Wollman */ 2466a06dea0SGarrett Wollman struct mbuf * 247122a814aSBosko Milekic m_copypacket(struct mbuf *m, int how) 2486a06dea0SGarrett Wollman { 2496a06dea0SGarrett Wollman struct mbuf *top, *n, *o; 2506a06dea0SGarrett Wollman 2516a06dea0SGarrett Wollman MGET(n, how, m->m_type); 2526a06dea0SGarrett Wollman top = n; 253122a814aSBosko Milekic if (n == NULL) 2546a06dea0SGarrett Wollman goto nospace; 2556a06dea0SGarrett Wollman 2569967cafcSSam Leffler if (!m_dup_pkthdr(n, m, how)) 2579967cafcSSam Leffler goto nospace; 2586a06dea0SGarrett Wollman n->m_len = m->m_len; 2596a06dea0SGarrett Wollman if (m->m_flags & M_EXT) { 2606a06dea0SGarrett Wollman n->m_data = m->m_data; 2616a06dea0SGarrett Wollman n->m_ext = m->m_ext; 2626a06dea0SGarrett Wollman n->m_flags |= M_EXT; 263a5c4836dSDavid Malone MEXT_ADD_REF(m); 2646a06dea0SGarrett Wollman } else { 2655fe86675SLuigi Rizzo n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat ); 2666a06dea0SGarrett Wollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 2676a06dea0SGarrett Wollman } 2686a06dea0SGarrett Wollman 2696a06dea0SGarrett Wollman m = m->m_next; 2706a06dea0SGarrett Wollman while (m) { 2716a06dea0SGarrett Wollman MGET(o, how, m->m_type); 272122a814aSBosko Milekic if (o == NULL) 2736a06dea0SGarrett Wollman goto nospace; 2746a06dea0SGarrett Wollman 2756a06dea0SGarrett Wollman n->m_next = o; 2766a06dea0SGarrett Wollman n = n->m_next; 2776a06dea0SGarrett Wollman 2786a06dea0SGarrett Wollman n->m_len = m->m_len; 2796a06dea0SGarrett Wollman if (m->m_flags & M_EXT) { 2806a06dea0SGarrett Wollman n->m_data = m->m_data; 2816a06dea0SGarrett Wollman n->m_ext = m->m_ext; 2826a06dea0SGarrett Wollman n->m_flags |= M_EXT; 283a5c4836dSDavid Malone MEXT_ADD_REF(m); 2846a06dea0SGarrett Wollman } else { 2856a06dea0SGarrett Wollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 2866a06dea0SGarrett Wollman } 2876a06dea0SGarrett Wollman 2886a06dea0SGarrett Wollman m = m->m_next; 2896a06dea0SGarrett Wollman } 2906a06dea0SGarrett Wollman return top; 2916a06dea0SGarrett Wollman nospace: 2926a06dea0SGarrett Wollman m_freem(top); 29308442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 294122a814aSBosko Milekic return (NULL); 2956a06dea0SGarrett Wollman } 2966a06dea0SGarrett Wollman 2976a06dea0SGarrett Wollman /* 298df8bae1dSRodney W. Grimes * Copy data from an mbuf chain starting "off" bytes from the beginning, 299df8bae1dSRodney W. Grimes * continuing for "len" bytes, into the indicated buffer. 300df8bae1dSRodney W. Grimes */ 30126f9a767SRodney W. Grimes void 302a8cfc0eeSJulian Elischer m_copydata(const struct mbuf *m, int off, int len, caddr_t cp) 303df8bae1dSRodney W. Grimes { 304bd395ae8SBosko Milekic u_int count; 305df8bae1dSRodney W. Grimes 306e0a653ddSAlfred Perlstein KASSERT(off >= 0, ("m_copydata, negative off %d", off)); 307e0a653ddSAlfred Perlstein KASSERT(len >= 0, ("m_copydata, negative len %d", len)); 308df8bae1dSRodney W. Grimes while (off > 0) { 309e0a653ddSAlfred Perlstein KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); 310df8bae1dSRodney W. Grimes if (off < m->m_len) 311df8bae1dSRodney W. Grimes break; 312df8bae1dSRodney W. Grimes off -= m->m_len; 313df8bae1dSRodney W. Grimes m = m->m_next; 314df8bae1dSRodney W. Grimes } 315df8bae1dSRodney W. Grimes while (len > 0) { 316e0a653ddSAlfred Perlstein KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); 317df8bae1dSRodney W. Grimes count = min(m->m_len - off, len); 318df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t) + off, cp, count); 319df8bae1dSRodney W. Grimes len -= count; 320df8bae1dSRodney W. Grimes cp += count; 321df8bae1dSRodney W. Grimes off = 0; 322df8bae1dSRodney W. Grimes m = m->m_next; 323df8bae1dSRodney W. Grimes } 324df8bae1dSRodney W. Grimes } 325df8bae1dSRodney W. Grimes 326df8bae1dSRodney W. Grimes /* 3271c38f2eaSArchie Cobbs * Copy a packet header mbuf chain into a completely new chain, including 3281c38f2eaSArchie Cobbs * copying any mbuf clusters. Use this instead of m_copypacket() when 3291c38f2eaSArchie Cobbs * you need a writable copy of an mbuf chain. 3301c38f2eaSArchie Cobbs */ 3311c38f2eaSArchie Cobbs struct mbuf * 332122a814aSBosko Milekic m_dup(struct mbuf *m, int how) 3331c38f2eaSArchie Cobbs { 3341c38f2eaSArchie Cobbs struct mbuf **p, *top = NULL; 3351c38f2eaSArchie Cobbs int remain, moff, nsize; 3361c38f2eaSArchie Cobbs 3371c38f2eaSArchie Cobbs /* Sanity check */ 3381c38f2eaSArchie Cobbs if (m == NULL) 339122a814aSBosko Milekic return (NULL); 340a48740b6SDavid E. O'Brien KASSERT((m->m_flags & M_PKTHDR) != 0, ("%s: !PKTHDR", __func__)); 3411c38f2eaSArchie Cobbs 3421c38f2eaSArchie Cobbs /* While there's more data, get a new mbuf, tack it on, and fill it */ 3431c38f2eaSArchie Cobbs remain = m->m_pkthdr.len; 3441c38f2eaSArchie Cobbs moff = 0; 3451c38f2eaSArchie Cobbs p = ⊤ 3461c38f2eaSArchie Cobbs while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */ 3471c38f2eaSArchie Cobbs struct mbuf *n; 3481c38f2eaSArchie Cobbs 3491c38f2eaSArchie Cobbs /* Get the next new mbuf */ 3501c38f2eaSArchie Cobbs MGET(n, how, m->m_type); 3511c38f2eaSArchie Cobbs if (n == NULL) 3521c38f2eaSArchie Cobbs goto nospace; 3531c38f2eaSArchie Cobbs if (top == NULL) { /* first one, must be PKTHDR */ 3549967cafcSSam Leffler if (!m_dup_pkthdr(n, m, how)) 3559967cafcSSam Leffler goto nospace; 3561c38f2eaSArchie Cobbs nsize = MHLEN; 3571c38f2eaSArchie Cobbs } else /* not the first one */ 3581c38f2eaSArchie Cobbs nsize = MLEN; 3591c38f2eaSArchie Cobbs if (remain >= MINCLSIZE) { 3601c38f2eaSArchie Cobbs MCLGET(n, how); 3611c38f2eaSArchie Cobbs if ((n->m_flags & M_EXT) == 0) { 3621c38f2eaSArchie Cobbs (void)m_free(n); 3631c38f2eaSArchie Cobbs goto nospace; 3641c38f2eaSArchie Cobbs } 3651c38f2eaSArchie Cobbs nsize = MCLBYTES; 3661c38f2eaSArchie Cobbs } 3671c38f2eaSArchie Cobbs n->m_len = 0; 3681c38f2eaSArchie Cobbs 3691c38f2eaSArchie Cobbs /* Link it into the new chain */ 3701c38f2eaSArchie Cobbs *p = n; 3711c38f2eaSArchie Cobbs p = &n->m_next; 3721c38f2eaSArchie Cobbs 3731c38f2eaSArchie Cobbs /* Copy data from original mbuf(s) into new mbuf */ 3741c38f2eaSArchie Cobbs while (n->m_len < nsize && m != NULL) { 3751c38f2eaSArchie Cobbs int chunk = min(nsize - n->m_len, m->m_len - moff); 3761c38f2eaSArchie Cobbs 3771c38f2eaSArchie Cobbs bcopy(m->m_data + moff, n->m_data + n->m_len, chunk); 3781c38f2eaSArchie Cobbs moff += chunk; 3791c38f2eaSArchie Cobbs n->m_len += chunk; 3801c38f2eaSArchie Cobbs remain -= chunk; 3811c38f2eaSArchie Cobbs if (moff == m->m_len) { 3821c38f2eaSArchie Cobbs m = m->m_next; 3831c38f2eaSArchie Cobbs moff = 0; 3841c38f2eaSArchie Cobbs } 3851c38f2eaSArchie Cobbs } 3861c38f2eaSArchie Cobbs 3871c38f2eaSArchie Cobbs /* Check correct total mbuf length */ 3881c38f2eaSArchie Cobbs KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL), 389a48740b6SDavid E. O'Brien ("%s: bogus m_pkthdr.len", __func__)); 3901c38f2eaSArchie Cobbs } 3911c38f2eaSArchie Cobbs return (top); 3921c38f2eaSArchie Cobbs 3931c38f2eaSArchie Cobbs nospace: 3941c38f2eaSArchie Cobbs m_freem(top); 39508442f8aSBosko Milekic mbstat.m_mcfail++; /* XXX: No consistency. */ 396122a814aSBosko Milekic return (NULL); 3971c38f2eaSArchie Cobbs } 3981c38f2eaSArchie Cobbs 3991c38f2eaSArchie Cobbs /* 400df8bae1dSRodney W. Grimes * Concatenate mbuf chain n to m. 401df8bae1dSRodney W. Grimes * Both chains must be of the same type (e.g. MT_DATA). 402df8bae1dSRodney W. Grimes * Any m_pkthdr is not updated. 403df8bae1dSRodney W. Grimes */ 40426f9a767SRodney W. Grimes void 405122a814aSBosko Milekic m_cat(struct mbuf *m, struct mbuf *n) 406df8bae1dSRodney W. Grimes { 407df8bae1dSRodney W. Grimes while (m->m_next) 408df8bae1dSRodney W. Grimes m = m->m_next; 409df8bae1dSRodney W. Grimes while (n) { 410df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT || 411df8bae1dSRodney W. Grimes m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 412df8bae1dSRodney W. Grimes /* just join the two chains */ 413df8bae1dSRodney W. Grimes m->m_next = n; 414df8bae1dSRodney W. Grimes return; 415df8bae1dSRodney W. Grimes } 416df8bae1dSRodney W. Grimes /* splat the data from one into the other */ 417df8bae1dSRodney W. Grimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 418df8bae1dSRodney W. Grimes (u_int)n->m_len); 419df8bae1dSRodney W. Grimes m->m_len += n->m_len; 420df8bae1dSRodney W. Grimes n = m_free(n); 421df8bae1dSRodney W. Grimes } 422df8bae1dSRodney W. Grimes } 423df8bae1dSRodney W. Grimes 42426f9a767SRodney W. Grimes void 425122a814aSBosko Milekic m_adj(struct mbuf *mp, int req_len) 426df8bae1dSRodney W. Grimes { 427122a814aSBosko Milekic int len = req_len; 428122a814aSBosko Milekic struct mbuf *m; 429122a814aSBosko Milekic int count; 430df8bae1dSRodney W. Grimes 431df8bae1dSRodney W. Grimes if ((m = mp) == NULL) 432df8bae1dSRodney W. Grimes return; 433df8bae1dSRodney W. Grimes if (len >= 0) { 434df8bae1dSRodney W. Grimes /* 435df8bae1dSRodney W. Grimes * Trim from head. 436df8bae1dSRodney W. Grimes */ 437df8bae1dSRodney W. Grimes while (m != NULL && len > 0) { 438df8bae1dSRodney W. Grimes if (m->m_len <= len) { 439df8bae1dSRodney W. Grimes len -= m->m_len; 440df8bae1dSRodney W. Grimes m->m_len = 0; 441df8bae1dSRodney W. Grimes m = m->m_next; 442df8bae1dSRodney W. Grimes } else { 443df8bae1dSRodney W. Grimes m->m_len -= len; 444df8bae1dSRodney W. Grimes m->m_data += len; 445df8bae1dSRodney W. Grimes len = 0; 446df8bae1dSRodney W. Grimes } 447df8bae1dSRodney W. Grimes } 448df8bae1dSRodney W. Grimes m = mp; 449df8bae1dSRodney W. Grimes if (mp->m_flags & M_PKTHDR) 450df8bae1dSRodney W. Grimes m->m_pkthdr.len -= (req_len - len); 451df8bae1dSRodney W. Grimes } else { 452df8bae1dSRodney W. Grimes /* 453df8bae1dSRodney W. Grimes * Trim from tail. Scan the mbuf chain, 454df8bae1dSRodney W. Grimes * calculating its length and finding the last mbuf. 455df8bae1dSRodney W. Grimes * If the adjustment only affects this mbuf, then just 456df8bae1dSRodney W. Grimes * adjust and return. Otherwise, rescan and truncate 457df8bae1dSRodney W. Grimes * after the remaining size. 458df8bae1dSRodney W. Grimes */ 459df8bae1dSRodney W. Grimes len = -len; 460df8bae1dSRodney W. Grimes count = 0; 461df8bae1dSRodney W. Grimes for (;;) { 462df8bae1dSRodney W. Grimes count += m->m_len; 463df8bae1dSRodney W. Grimes if (m->m_next == (struct mbuf *)0) 464df8bae1dSRodney W. Grimes break; 465df8bae1dSRodney W. Grimes m = m->m_next; 466df8bae1dSRodney W. Grimes } 467df8bae1dSRodney W. Grimes if (m->m_len >= len) { 468df8bae1dSRodney W. Grimes m->m_len -= len; 469df8bae1dSRodney W. Grimes if (mp->m_flags & M_PKTHDR) 470df8bae1dSRodney W. Grimes mp->m_pkthdr.len -= len; 471df8bae1dSRodney W. Grimes return; 472df8bae1dSRodney W. Grimes } 473df8bae1dSRodney W. Grimes count -= len; 474df8bae1dSRodney W. Grimes if (count < 0) 475df8bae1dSRodney W. Grimes count = 0; 476df8bae1dSRodney W. Grimes /* 477df8bae1dSRodney W. Grimes * Correct length for chain is "count". 478df8bae1dSRodney W. Grimes * Find the mbuf with last data, adjust its length, 479df8bae1dSRodney W. Grimes * and toss data from remaining mbufs on chain. 480df8bae1dSRodney W. Grimes */ 481df8bae1dSRodney W. Grimes m = mp; 482df8bae1dSRodney W. Grimes if (m->m_flags & M_PKTHDR) 483df8bae1dSRodney W. Grimes m->m_pkthdr.len = count; 484df8bae1dSRodney W. Grimes for (; m; m = m->m_next) { 485df8bae1dSRodney W. Grimes if (m->m_len >= count) { 486df8bae1dSRodney W. Grimes m->m_len = count; 487df8bae1dSRodney W. Grimes break; 488df8bae1dSRodney W. Grimes } 489df8bae1dSRodney W. Grimes count -= m->m_len; 490df8bae1dSRodney W. Grimes } 491797f2d22SPoul-Henning Kamp while (m->m_next) 492797f2d22SPoul-Henning Kamp (m = m->m_next) ->m_len = 0; 493df8bae1dSRodney W. Grimes } 494df8bae1dSRodney W. Grimes } 495df8bae1dSRodney W. Grimes 496df8bae1dSRodney W. Grimes /* 497df8bae1dSRodney W. Grimes * Rearange an mbuf chain so that len bytes are contiguous 498df8bae1dSRodney W. Grimes * and in the data area of an mbuf (so that mtod and dtom 499df8bae1dSRodney W. Grimes * will work for a structure of size len). Returns the resulting 500df8bae1dSRodney W. Grimes * mbuf chain on success, frees it and returns null on failure. 501df8bae1dSRodney W. Grimes * If there is room, it will add up to max_protohdr-len extra bytes to the 502df8bae1dSRodney W. Grimes * contiguous region in an attempt to avoid being called next time. 503df8bae1dSRodney W. Grimes */ 504df8bae1dSRodney W. Grimes struct mbuf * 505122a814aSBosko Milekic m_pullup(struct mbuf *n, int len) 506df8bae1dSRodney W. Grimes { 507122a814aSBosko Milekic struct mbuf *m; 508122a814aSBosko Milekic int count; 509df8bae1dSRodney W. Grimes int space; 510df8bae1dSRodney W. Grimes 511df8bae1dSRodney W. Grimes /* 512df8bae1dSRodney W. Grimes * If first mbuf has no cluster, and has room for len bytes 513df8bae1dSRodney W. Grimes * without shifting current data, pullup into it, 514df8bae1dSRodney W. Grimes * otherwise allocate a new mbuf to prepend to the chain. 515df8bae1dSRodney W. Grimes */ 516df8bae1dSRodney W. Grimes if ((n->m_flags & M_EXT) == 0 && 517df8bae1dSRodney W. Grimes n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 518df8bae1dSRodney W. Grimes if (n->m_len >= len) 519df8bae1dSRodney W. Grimes return (n); 520df8bae1dSRodney W. Grimes m = n; 521df8bae1dSRodney W. Grimes n = n->m_next; 522df8bae1dSRodney W. Grimes len -= m->m_len; 523df8bae1dSRodney W. Grimes } else { 524df8bae1dSRodney W. Grimes if (len > MHLEN) 525df8bae1dSRodney W. Grimes goto bad; 526a163d034SWarner Losh MGET(m, M_DONTWAIT, n->m_type); 527122a814aSBosko Milekic if (m == NULL) 528df8bae1dSRodney W. Grimes goto bad; 529df8bae1dSRodney W. Grimes m->m_len = 0; 5309967cafcSSam Leffler if (n->m_flags & M_PKTHDR) 5319967cafcSSam Leffler M_MOVE_PKTHDR(m, n); 532df8bae1dSRodney W. Grimes } 533df8bae1dSRodney W. Grimes space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 534df8bae1dSRodney W. Grimes do { 535df8bae1dSRodney W. Grimes count = min(min(max(len, max_protohdr), space), n->m_len); 536df8bae1dSRodney W. Grimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 537bd395ae8SBosko Milekic (u_int)count); 538df8bae1dSRodney W. Grimes len -= count; 539df8bae1dSRodney W. Grimes m->m_len += count; 540df8bae1dSRodney W. Grimes n->m_len -= count; 541df8bae1dSRodney W. Grimes space -= count; 542df8bae1dSRodney W. Grimes if (n->m_len) 543df8bae1dSRodney W. Grimes n->m_data += count; 544df8bae1dSRodney W. Grimes else 545df8bae1dSRodney W. Grimes n = m_free(n); 546df8bae1dSRodney W. Grimes } while (len > 0 && n); 547df8bae1dSRodney W. Grimes if (len > 0) { 548df8bae1dSRodney W. Grimes (void) m_free(m); 549df8bae1dSRodney W. Grimes goto bad; 550df8bae1dSRodney W. Grimes } 551df8bae1dSRodney W. Grimes m->m_next = n; 552df8bae1dSRodney W. Grimes return (m); 553df8bae1dSRodney W. Grimes bad: 554df8bae1dSRodney W. Grimes m_freem(n); 55508442f8aSBosko Milekic mbstat.m_mpfail++; /* XXX: No consistency. */ 556122a814aSBosko Milekic return (NULL); 557df8bae1dSRodney W. Grimes } 558df8bae1dSRodney W. Grimes 559df8bae1dSRodney W. Grimes /* 560df8bae1dSRodney W. Grimes * Partition an mbuf chain in two pieces, returning the tail -- 561df8bae1dSRodney W. Grimes * all but the first len0 bytes. In case of failure, it returns NULL and 562df8bae1dSRodney W. Grimes * attempts to restore the chain to its original state. 56348d183faSArchie Cobbs * 56448d183faSArchie Cobbs * Note that the resulting mbufs might be read-only, because the new 56548d183faSArchie Cobbs * mbuf can end up sharing an mbuf cluster with the original mbuf if 56648d183faSArchie Cobbs * the "breaking point" happens to lie within a cluster mbuf. Use the 56748d183faSArchie Cobbs * M_WRITABLE() macro to check for this case. 568df8bae1dSRodney W. Grimes */ 569df8bae1dSRodney W. Grimes struct mbuf * 570122a814aSBosko Milekic m_split(struct mbuf *m0, int len0, int wait) 571df8bae1dSRodney W. Grimes { 572122a814aSBosko Milekic struct mbuf *m, *n; 573bd395ae8SBosko Milekic u_int len = len0, remain; 574df8bae1dSRodney W. Grimes 575df8bae1dSRodney W. Grimes for (m = m0; m && len > m->m_len; m = m->m_next) 576df8bae1dSRodney W. Grimes len -= m->m_len; 577122a814aSBosko Milekic if (m == NULL) 578122a814aSBosko Milekic return (NULL); 579df8bae1dSRodney W. Grimes remain = m->m_len - len; 580df8bae1dSRodney W. Grimes if (m0->m_flags & M_PKTHDR) { 581df8bae1dSRodney W. Grimes MGETHDR(n, wait, m0->m_type); 582122a814aSBosko Milekic if (n == NULL) 583122a814aSBosko Milekic return (NULL); 584df8bae1dSRodney W. Grimes n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 585df8bae1dSRodney W. Grimes n->m_pkthdr.len = m0->m_pkthdr.len - len0; 586df8bae1dSRodney W. Grimes m0->m_pkthdr.len = len0; 587df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) 588df8bae1dSRodney W. Grimes goto extpacket; 589df8bae1dSRodney W. Grimes if (remain > MHLEN) { 590df8bae1dSRodney W. Grimes /* m can't be the lead packet */ 591df8bae1dSRodney W. Grimes MH_ALIGN(n, 0); 592df8bae1dSRodney W. Grimes n->m_next = m_split(m, len, wait); 593122a814aSBosko Milekic if (n->m_next == NULL) { 594df8bae1dSRodney W. Grimes (void) m_free(n); 595122a814aSBosko Milekic return (NULL); 59640376987SJeffrey Hsu } else { 59740376987SJeffrey Hsu n->m_len = 0; 598df8bae1dSRodney W. Grimes return (n); 59940376987SJeffrey Hsu } 600df8bae1dSRodney W. Grimes } else 601df8bae1dSRodney W. Grimes MH_ALIGN(n, remain); 602df8bae1dSRodney W. Grimes } else if (remain == 0) { 603df8bae1dSRodney W. Grimes n = m->m_next; 604122a814aSBosko Milekic m->m_next = NULL; 605df8bae1dSRodney W. Grimes return (n); 606df8bae1dSRodney W. Grimes } else { 607df8bae1dSRodney W. Grimes MGET(n, wait, m->m_type); 608122a814aSBosko Milekic if (n == NULL) 609122a814aSBosko Milekic return (NULL); 610df8bae1dSRodney W. Grimes M_ALIGN(n, remain); 611df8bae1dSRodney W. Grimes } 612df8bae1dSRodney W. Grimes extpacket: 613df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) { 614df8bae1dSRodney W. Grimes n->m_flags |= M_EXT; 615df8bae1dSRodney W. Grimes n->m_ext = m->m_ext; 616a5c4836dSDavid Malone MEXT_ADD_REF(m); 617df8bae1dSRodney W. Grimes n->m_data = m->m_data + len; 618df8bae1dSRodney W. Grimes } else { 619df8bae1dSRodney W. Grimes bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 620df8bae1dSRodney W. Grimes } 621df8bae1dSRodney W. Grimes n->m_len = remain; 622df8bae1dSRodney W. Grimes m->m_len = len; 623df8bae1dSRodney W. Grimes n->m_next = m->m_next; 624122a814aSBosko Milekic m->m_next = NULL; 625df8bae1dSRodney W. Grimes return (n); 626df8bae1dSRodney W. Grimes } 627df8bae1dSRodney W. Grimes /* 628df8bae1dSRodney W. Grimes * Routine to copy from device local memory into mbufs. 629f5eece3fSBosko Milekic * Note that `off' argument is offset into first mbuf of target chain from 630f5eece3fSBosko Milekic * which to begin copying the data to. 631df8bae1dSRodney W. Grimes */ 632df8bae1dSRodney W. Grimes struct mbuf * 633f5eece3fSBosko Milekic m_devget(char *buf, int totlen, int off, struct ifnet *ifp, 634122a814aSBosko Milekic void (*copy)(char *from, caddr_t to, u_int len)) 635df8bae1dSRodney W. Grimes { 636122a814aSBosko Milekic struct mbuf *m; 637df8bae1dSRodney W. Grimes struct mbuf *top = 0, **mp = ⊤ 638f5eece3fSBosko Milekic int len; 639df8bae1dSRodney W. Grimes 640f5eece3fSBosko Milekic if (off < 0 || off > MHLEN) 641f5eece3fSBosko Milekic return (NULL); 642f5eece3fSBosko Milekic 643a163d034SWarner Losh MGETHDR(m, M_DONTWAIT, MT_DATA); 644122a814aSBosko Milekic if (m == NULL) 645122a814aSBosko Milekic return (NULL); 646df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = ifp; 647df8bae1dSRodney W. Grimes m->m_pkthdr.len = totlen; 648f5eece3fSBosko Milekic len = MHLEN; 649df8bae1dSRodney W. Grimes 650df8bae1dSRodney W. Grimes while (totlen > 0) { 651df8bae1dSRodney W. Grimes if (top) { 652a163d034SWarner Losh MGET(m, M_DONTWAIT, MT_DATA); 653122a814aSBosko Milekic if (m == NULL) { 654df8bae1dSRodney W. Grimes m_freem(top); 655122a814aSBosko Milekic return (NULL); 656df8bae1dSRodney W. Grimes } 657f5eece3fSBosko Milekic len = MLEN; 658df8bae1dSRodney W. Grimes } 659f5eece3fSBosko Milekic if (totlen + off >= MINCLSIZE) { 660a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 661df8bae1dSRodney W. Grimes if (m->m_flags & M_EXT) 662f5eece3fSBosko Milekic len = MCLBYTES; 663df8bae1dSRodney W. Grimes } else { 664df8bae1dSRodney W. Grimes /* 665df8bae1dSRodney W. Grimes * Place initial small packet/header at end of mbuf. 666df8bae1dSRodney W. Grimes */ 667f5eece3fSBosko Milekic if (top == NULL && totlen + off + max_linkhdr <= len) { 668df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 669f5eece3fSBosko Milekic len -= max_linkhdr; 670df8bae1dSRodney W. Grimes } 671f5eece3fSBosko Milekic } 672f5eece3fSBosko Milekic if (off) { 673f5eece3fSBosko Milekic m->m_data += off; 674f5eece3fSBosko Milekic len -= off; 675f5eece3fSBosko Milekic off = 0; 676f5eece3fSBosko Milekic } 677f5eece3fSBosko Milekic m->m_len = len = min(totlen, len); 678df8bae1dSRodney W. Grimes if (copy) 679bd395ae8SBosko Milekic copy(buf, mtod(m, caddr_t), (u_int)len); 680df8bae1dSRodney W. Grimes else 681bd395ae8SBosko Milekic bcopy(buf, mtod(m, caddr_t), (u_int)len); 682f5eece3fSBosko Milekic buf += len; 683df8bae1dSRodney W. Grimes *mp = m; 684df8bae1dSRodney W. Grimes mp = &m->m_next; 685df8bae1dSRodney W. Grimes totlen -= len; 686df8bae1dSRodney W. Grimes } 687df8bae1dSRodney W. Grimes return (top); 688df8bae1dSRodney W. Grimes } 689c5789ba3SPoul-Henning Kamp 690c5789ba3SPoul-Henning Kamp /* 691c5789ba3SPoul-Henning Kamp * Copy data from a buffer back into the indicated mbuf chain, 692c5789ba3SPoul-Henning Kamp * starting "off" bytes from the beginning, extending the mbuf 693c5789ba3SPoul-Henning Kamp * chain if necessary. 694c5789ba3SPoul-Henning Kamp */ 695c5789ba3SPoul-Henning Kamp void 696122a814aSBosko Milekic m_copyback(struct mbuf *m0, int off, int len, caddr_t cp) 697c5789ba3SPoul-Henning Kamp { 698122a814aSBosko Milekic int mlen; 699122a814aSBosko Milekic struct mbuf *m = m0, *n; 700c5789ba3SPoul-Henning Kamp int totlen = 0; 701c5789ba3SPoul-Henning Kamp 702122a814aSBosko Milekic if (m0 == NULL) 703c5789ba3SPoul-Henning Kamp return; 704c5789ba3SPoul-Henning Kamp while (off > (mlen = m->m_len)) { 705c5789ba3SPoul-Henning Kamp off -= mlen; 706c5789ba3SPoul-Henning Kamp totlen += mlen; 707122a814aSBosko Milekic if (m->m_next == NULL) { 708a163d034SWarner Losh n = m_get_clrd(M_DONTWAIT, m->m_type); 709122a814aSBosko Milekic if (n == NULL) 710c5789ba3SPoul-Henning Kamp goto out; 711c5789ba3SPoul-Henning Kamp n->m_len = min(MLEN, len + off); 712c5789ba3SPoul-Henning Kamp m->m_next = n; 713c5789ba3SPoul-Henning Kamp } 714c5789ba3SPoul-Henning Kamp m = m->m_next; 715c5789ba3SPoul-Henning Kamp } 716c5789ba3SPoul-Henning Kamp while (len > 0) { 717c5789ba3SPoul-Henning Kamp mlen = min (m->m_len - off, len); 718bd395ae8SBosko Milekic bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen); 719c5789ba3SPoul-Henning Kamp cp += mlen; 720c5789ba3SPoul-Henning Kamp len -= mlen; 721c5789ba3SPoul-Henning Kamp mlen += off; 722c5789ba3SPoul-Henning Kamp off = 0; 723c5789ba3SPoul-Henning Kamp totlen += mlen; 724c5789ba3SPoul-Henning Kamp if (len == 0) 725c5789ba3SPoul-Henning Kamp break; 726122a814aSBosko Milekic if (m->m_next == NULL) { 727a163d034SWarner Losh n = m_get(M_DONTWAIT, m->m_type); 728122a814aSBosko Milekic if (n == NULL) 729c5789ba3SPoul-Henning Kamp break; 730c5789ba3SPoul-Henning Kamp n->m_len = min(MLEN, len); 731c5789ba3SPoul-Henning Kamp m->m_next = n; 732c5789ba3SPoul-Henning Kamp } 733c5789ba3SPoul-Henning Kamp m = m->m_next; 734c5789ba3SPoul-Henning Kamp } 735c5789ba3SPoul-Henning Kamp out: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 736c5789ba3SPoul-Henning Kamp m->m_pkthdr.len = totlen; 737c5789ba3SPoul-Henning Kamp } 738ce4a64f7SPoul-Henning Kamp 739ce4a64f7SPoul-Henning Kamp void 740ce4a64f7SPoul-Henning Kamp m_print(const struct mbuf *m) 741ce4a64f7SPoul-Henning Kamp { 742ce4a64f7SPoul-Henning Kamp int len; 7436357e7b5SEivind Eklund const struct mbuf *m2; 744ce4a64f7SPoul-Henning Kamp 745ce4a64f7SPoul-Henning Kamp len = m->m_pkthdr.len; 746ce4a64f7SPoul-Henning Kamp m2 = m; 747ce4a64f7SPoul-Henning Kamp while (len) { 748ce4a64f7SPoul-Henning Kamp printf("%p %*D\n", m2, m2->m_len, (u_char *)m2->m_data, "-"); 749ce4a64f7SPoul-Henning Kamp len -= m2->m_len; 750ce4a64f7SPoul-Henning Kamp m2 = m2->m_next; 751ce4a64f7SPoul-Henning Kamp } 752ce4a64f7SPoul-Henning Kamp return; 753ce4a64f7SPoul-Henning Kamp } 7543f2e06c5SPoul-Henning Kamp 755bd395ae8SBosko Milekic u_int 7563f2e06c5SPoul-Henning Kamp m_fixhdr(struct mbuf *m0) 7573f2e06c5SPoul-Henning Kamp { 758bd395ae8SBosko Milekic u_int len; 7593f2e06c5SPoul-Henning Kamp 760ac6e585dSPoul-Henning Kamp len = m_length(m0, NULL); 7613f2e06c5SPoul-Henning Kamp m0->m_pkthdr.len = len; 762ac6e585dSPoul-Henning Kamp return (len); 763ac6e585dSPoul-Henning Kamp } 764ac6e585dSPoul-Henning Kamp 765bd395ae8SBosko Milekic u_int 766ac6e585dSPoul-Henning Kamp m_length(struct mbuf *m0, struct mbuf **last) 767ac6e585dSPoul-Henning Kamp { 768ac6e585dSPoul-Henning Kamp struct mbuf *m; 769bd395ae8SBosko Milekic u_int len; 770ac6e585dSPoul-Henning Kamp 771ac6e585dSPoul-Henning Kamp len = 0; 772ac6e585dSPoul-Henning Kamp for (m = m0; m != NULL; m = m->m_next) { 773ac6e585dSPoul-Henning Kamp len += m->m_len; 774ac6e585dSPoul-Henning Kamp if (m->m_next == NULL) 775ac6e585dSPoul-Henning Kamp break; 776ac6e585dSPoul-Henning Kamp } 777ac6e585dSPoul-Henning Kamp if (last != NULL) 778ac6e585dSPoul-Henning Kamp *last = m; 779ac6e585dSPoul-Henning Kamp return (len); 7803f2e06c5SPoul-Henning Kamp } 781