uipc_mbuf.c revision 152101
1139804Simp/*- 21541Srgrimes * Copyright (c) 1982, 1986, 1988, 1991, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 4. Neither the name of the University nor the names of its contributors 141541Srgrimes * may be used to endorse or promote products derived from this software 151541Srgrimes * without specific prior written permission. 161541Srgrimes * 171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271541Srgrimes * SUCH DAMAGE. 281541Srgrimes * 291541Srgrimes * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94 301541Srgrimes */ 311541Srgrimes 32116182Sobrien#include <sys/cdefs.h> 33116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/uipc_mbuf.c 152101 2005-11-05 19:43:55Z andre $"); 34116182Sobrien 35101007Srwatson#include "opt_mac.h" 3677572Sobrien#include "opt_param.h" 37113490Ssilby#include "opt_mbuf_stress_test.h" 38101007Srwatson 391541Srgrimes#include <sys/param.h> 401541Srgrimes#include <sys/systm.h> 4176166Smarkm#include <sys/kernel.h> 42125296Ssilby#include <sys/limits.h> 4376166Smarkm#include <sys/lock.h> 44101173Srwatson#include <sys/mac.h> 4532036Sbde#include <sys/malloc.h> 461541Srgrimes#include <sys/mbuf.h> 4723081Swollman#include <sys/sysctl.h> 481541Srgrimes#include <sys/domain.h> 491541Srgrimes#include <sys/protosw.h> 50125296Ssilby#include <sys/uio.h> 5176166Smarkm 529759Sbdeint max_linkhdr; 539759Sbdeint max_protohdr; 549759Sbdeint max_hdr; 559759Sbdeint max_datalen; 56116455Ssilby#ifdef MBUF_STRESS_TEST 57112777Ssilbyint m_defragpackets; 58112777Ssilbyint m_defragbytes; 59112777Ssilbyint m_defraguseless; 60112777Ssilbyint m_defragfailure; 61113490Ssilbyint m_defragrandomfailures; 62113490Ssilby#endif 631541Srgrimes 6466475Sbmilekic/* 6566475Sbmilekic * sysctl(8) exported objects 6666475Sbmilekic */ 6744078SdfrSYSCTL_DECL(_kern_ipc); 6823081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_LINKHDR, max_linkhdr, CTLFLAG_RW, 6923081Swollman &max_linkhdr, 0, ""); 7023081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_PROTOHDR, max_protohdr, CTLFLAG_RW, 7123081Swollman &max_protohdr, 0, ""); 7223081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_HDR, max_hdr, CTLFLAG_RW, &max_hdr, 0, ""); 7323081SwollmanSYSCTL_INT(_kern_ipc, KIPC_MAX_DATALEN, max_datalen, CTLFLAG_RW, 7423081Swollman &max_datalen, 0, ""); 75116455Ssilby#ifdef MBUF_STRESS_TEST 76112777SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defragpackets, CTLFLAG_RD, 77112777Ssilby &m_defragpackets, 0, ""); 78112777SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defragbytes, CTLFLAG_RD, 79112777Ssilby &m_defragbytes, 0, ""); 80112777SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defraguseless, CTLFLAG_RD, 81112777Ssilby &m_defraguseless, 0, ""); 82112777SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defragfailure, CTLFLAG_RD, 83112777Ssilby &m_defragfailure, 0, ""); 84113490SsilbySYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW, 85113490Ssilby &m_defragrandomfailures, 0, ""); 86113490Ssilby#endif 8775112Sbmilekic 881541Srgrimes/* 89129906Sbmilekic * Allocate a given length worth of mbufs and/or clusters (whatever fits 90129906Sbmilekic * best) and return a pointer to the top of the allocated chain. If an 91129906Sbmilekic * existing mbuf chain is provided, then we will append the new chain 92129906Sbmilekic * to the existing one but still return the top of the newly allocated 93129906Sbmilekic * chain. 94129906Sbmilekic */ 95129906Sbmilekicstruct mbuf * 96129906Sbmilekicm_getm(struct mbuf *m, int len, int how, short type) 97129906Sbmilekic{ 98129906Sbmilekic struct mbuf *mb, *top, *cur, *mtail; 99129906Sbmilekic int num, rem; 100129906Sbmilekic int i; 101129906Sbmilekic 102129906Sbmilekic KASSERT(len >= 0, ("m_getm(): len is < 0")); 103129906Sbmilekic 104129906Sbmilekic /* If m != NULL, we will append to the end of that chain. */ 105129906Sbmilekic if (m != NULL) 106129906Sbmilekic for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next); 107129906Sbmilekic else 108129906Sbmilekic mtail = NULL; 109129906Sbmilekic 110129906Sbmilekic /* 111129906Sbmilekic * Calculate how many mbufs+clusters ("packets") we need and how much 112129906Sbmilekic * leftover there is after that and allocate the first mbuf+cluster 113129906Sbmilekic * if required. 114129906Sbmilekic */ 115129906Sbmilekic num = len / MCLBYTES; 116129906Sbmilekic rem = len % MCLBYTES; 117129906Sbmilekic top = cur = NULL; 118129906Sbmilekic if (num > 0) { 119129906Sbmilekic if ((top = cur = m_getcl(how, type, 0)) == NULL) 120129906Sbmilekic goto failed; 121130271Smux top->m_len = 0; 122129906Sbmilekic } 123129906Sbmilekic num--; 124129906Sbmilekic 125129906Sbmilekic for (i = 0; i < num; i++) { 126129906Sbmilekic mb = m_getcl(how, type, 0); 127129906Sbmilekic if (mb == NULL) 128129906Sbmilekic goto failed; 129129906Sbmilekic mb->m_len = 0; 130129906Sbmilekic cur = (cur->m_next = mb); 131129906Sbmilekic } 132129906Sbmilekic if (rem > 0) { 133129906Sbmilekic mb = (rem > MINCLSIZE) ? 134129906Sbmilekic m_getcl(how, type, 0) : m_get(how, type); 135129906Sbmilekic if (mb == NULL) 136129906Sbmilekic goto failed; 137129906Sbmilekic mb->m_len = 0; 138129906Sbmilekic if (cur == NULL) 139129906Sbmilekic top = mb; 140129906Sbmilekic else 141129906Sbmilekic cur->m_next = mb; 142129906Sbmilekic } 143129906Sbmilekic 144129906Sbmilekic if (mtail != NULL) 145129906Sbmilekic mtail->m_next = top; 146129906Sbmilekic return top; 147129906Sbmilekicfailed: 148129906Sbmilekic if (top != NULL) 149129906Sbmilekic m_freem(top); 150129906Sbmilekic return NULL; 151129906Sbmilekic} 152129906Sbmilekic 153129906Sbmilekic/* 154129906Sbmilekic * Free an entire chain of mbufs and associated external buffers, if 155129906Sbmilekic * applicable. 156129906Sbmilekic */ 157129906Sbmilekicvoid 158129906Sbmilekicm_freem(struct mbuf *mb) 159129906Sbmilekic{ 160129906Sbmilekic 161129906Sbmilekic while (mb != NULL) 162129906Sbmilekic mb = m_free(mb); 163129906Sbmilekic} 164129906Sbmilekic 165129906Sbmilekic/*- 166129906Sbmilekic * Configure a provided mbuf to refer to the provided external storage 167129906Sbmilekic * buffer and setup a reference count for said buffer. If the setting 168129906Sbmilekic * up of the reference count fails, the M_EXT bit will not be set. If 169129906Sbmilekic * successfull, the M_EXT bit is set in the mbuf's flags. 170129906Sbmilekic * 171129906Sbmilekic * Arguments: 172129906Sbmilekic * mb The existing mbuf to which to attach the provided buffer. 173129906Sbmilekic * buf The address of the provided external storage buffer. 174129906Sbmilekic * size The size of the provided buffer. 175129906Sbmilekic * freef A pointer to a routine that is responsible for freeing the 176129906Sbmilekic * provided external storage buffer. 177129906Sbmilekic * args A pointer to an argument structure (of any type) to be passed 178129906Sbmilekic * to the provided freef routine (may be NULL). 179129906Sbmilekic * flags Any other flags to be passed to the provided mbuf. 180129906Sbmilekic * type The type that the external storage buffer should be 181129906Sbmilekic * labeled with. 182129906Sbmilekic * 183129906Sbmilekic * Returns: 184129906Sbmilekic * Nothing. 185129906Sbmilekic */ 186129906Sbmilekicvoid 187129906Sbmilekicm_extadd(struct mbuf *mb, caddr_t buf, u_int size, 188129906Sbmilekic void (*freef)(void *, void *), void *args, int flags, int type) 189129906Sbmilekic{ 190151976Sandre KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__)); 191129906Sbmilekic 192151976Sandre if (type != EXT_EXTREF) 193151976Sandre mb->m_ext.ref_cnt = (u_int *)uma_zalloc(zone_ext_refcnt, M_NOWAIT); 194129906Sbmilekic if (mb->m_ext.ref_cnt != NULL) { 195129906Sbmilekic *(mb->m_ext.ref_cnt) = 1; 196129906Sbmilekic mb->m_flags |= (M_EXT | flags); 197129906Sbmilekic mb->m_ext.ext_buf = buf; 198129906Sbmilekic mb->m_data = mb->m_ext.ext_buf; 199129906Sbmilekic mb->m_ext.ext_size = size; 200129906Sbmilekic mb->m_ext.ext_free = freef; 201129906Sbmilekic mb->m_ext.ext_args = args; 202129906Sbmilekic mb->m_ext.ext_type = type; 203129906Sbmilekic } 204129906Sbmilekic} 205129906Sbmilekic 206129906Sbmilekic/* 207129906Sbmilekic * Non-directly-exported function to clean up after mbufs with M_EXT 208151976Sandre * storage attached to them if the reference count hits 1. 209129906Sbmilekic */ 210129906Sbmilekicvoid 211129906Sbmilekicmb_free_ext(struct mbuf *m) 212129906Sbmilekic{ 213151976Sandre KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 214151976Sandre KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__)); 215129906Sbmilekic 216151976Sandre /* Free attached storage if this mbuf is the only reference to it. */ 217151976Sandre if (*(m->m_ext.ref_cnt) == 1 || 218152035Sandre atomic_fetchadd_int(m->m_ext.ref_cnt, -1) == 0) { 219151976Sandre switch (m->m_ext.ext_type) { 220152101Sandre case EXT_PACKET: /* The packet zone is special. */ 221152035Sandre if (*(m->m_ext.ref_cnt) == 0) 222152035Sandre *(m->m_ext.ref_cnt) = 1; 223151976Sandre uma_zfree(zone_pack, m); 224151976Sandre return; /* Job done. */ 225152101Sandre case EXT_CLUSTER: 226152101Sandre uma_zfree(zone_clust, m->m_ext.ext_buf); 227130289Sbmilekic break; 228151976Sandre case EXT_JUMBO9: 229151976Sandre uma_zfree(zone_jumbo9, m->m_ext.ext_buf); 230151976Sandre break; 231151976Sandre case EXT_JUMBO16: 232151976Sandre uma_zfree(zone_jumbo16, m->m_ext.ext_buf); 233151976Sandre break; 234151976Sandre case EXT_SFBUF: 235151976Sandre case EXT_NET_DRV: 236151976Sandre case EXT_MOD_TYPE: 237151976Sandre case EXT_DISPOSABLE: 238151976Sandre *(m->m_ext.ref_cnt) = 0; 239151976Sandre uma_zfree(zone_ext_refcnt, __DEVOLATILE(u_int *, 240151976Sandre m->m_ext.ref_cnt)); 241151976Sandre /* FALLTHROUGH */ 242151976Sandre case EXT_EXTREF: 243151976Sandre KASSERT(m->m_ext.ext_free != NULL, 244151976Sandre ("%s: ext_free not set", __func__)); 245141668Sbmilekic (*(m->m_ext.ext_free))(m->m_ext.ext_buf, 246141668Sbmilekic m->m_ext.ext_args); 247151976Sandre break; 248151976Sandre default: 249151976Sandre KASSERT(m->m_ext.ext_type == 0, 250151976Sandre ("%s: unknown ext_type", __func__)); 251141668Sbmilekic } 252141668Sbmilekic } 253151976Sandre /* 254151976Sandre * Free this mbuf back to the mbuf zone with all m_ext 255151976Sandre * information purged. 256151976Sandre */ 257151976Sandre m->m_ext.ext_buf = NULL; 258151976Sandre m->m_ext.ext_free = NULL; 259151976Sandre m->m_ext.ext_args = NULL; 260151976Sandre m->m_ext.ref_cnt = NULL; 261151976Sandre m->m_ext.ext_size = 0; 262151976Sandre m->m_ext.ext_type = 0; 263151976Sandre m->m_flags &= ~M_EXT; 264130357Sbmilekic uma_zfree(zone_mbuf, m); 265129906Sbmilekic} 266129906Sbmilekic 267129906Sbmilekic/* 268151976Sandre * Attach the the cluster from *m to *n, set up m_ext in *n 269151976Sandre * and bump the refcount of the cluster. 270151976Sandre */ 271151976Sandrestatic void 272151976Sandremb_dupcl(struct mbuf *n, struct mbuf *m) 273151976Sandre{ 274151976Sandre KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__)); 275151976Sandre KASSERT(m->m_ext.ref_cnt != NULL, ("%s: ref_cnt not set", __func__)); 276151976Sandre KASSERT((n->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__)); 277151976Sandre 278151976Sandre if (*(m->m_ext.ref_cnt) == 1) 279151976Sandre *(m->m_ext.ref_cnt) += 1; 280151976Sandre else 281151976Sandre atomic_add_int(m->m_ext.ref_cnt, 1); 282151976Sandre n->m_ext.ext_buf = m->m_ext.ext_buf; 283151976Sandre n->m_ext.ext_free = m->m_ext.ext_free; 284151976Sandre n->m_ext.ext_args = m->m_ext.ext_args; 285151976Sandre n->m_ext.ext_size = m->m_ext.ext_size; 286151976Sandre n->m_ext.ref_cnt = m->m_ext.ref_cnt; 287151976Sandre n->m_ext.ext_type = m->m_ext.ext_type; 288151976Sandre n->m_flags |= M_EXT; 289151976Sandre} 290151976Sandre 291151976Sandre/* 292149598Sandre * Clean up mbuf (chain) from any tags and packet headers. 293149647Sandre * If "all" is set then the first mbuf in the chain will be 294149647Sandre * cleaned too. 295149598Sandre */ 296149598Sandrevoid 297149647Sandrem_demote(struct mbuf *m0, int all) 298149598Sandre{ 299149598Sandre struct mbuf *m; 300149598Sandre 301149598Sandre for (m = all ? m0 : m0->m_next; m != NULL; m = m->m_next) { 302149598Sandre if (m->m_flags & M_PKTHDR) { 303149598Sandre m_tag_delete_chain(m, NULL); 304149598Sandre m->m_flags &= ~M_PKTHDR; 305149598Sandre bzero(&m->m_pkthdr, sizeof(struct pkthdr)); 306149598Sandre } 307149643Sandre if (m->m_type == MT_HEADER) 308149598Sandre m->m_type = MT_DATA; 309149643Sandre if (m != m0 && m->m_nextpkt != NULL) 310149598Sandre m->m_nextpkt = NULL; 311149598Sandre m->m_flags = m->m_flags & (M_EXT|M_EOR|M_RDONLY|M_FREELIST); 312149598Sandre } 313149598Sandre} 314149598Sandre 315149598Sandre/* 316149648Sandre * Sanity checks on mbuf (chain) for use in KASSERT() and general 317149648Sandre * debugging. 318149648Sandre * Returns 0 or panics when bad and 1 on all tests passed. 319149648Sandre * Sanitize, 0 to run M_SANITY_ACTION, 1 to garble things so they 320149648Sandre * blow up later. 321149599Sandre */ 322149599Sandreint 323149599Sandrem_sanity(struct mbuf *m0, int sanitize) 324149599Sandre{ 325149599Sandre struct mbuf *m; 326149599Sandre caddr_t a, b; 327149599Sandre int pktlen = 0; 328149599Sandre 329149599Sandre#define M_SANITY_ACTION(s) return (0) 330149599Sandre/* #define M_SANITY_ACTION(s) panic("mbuf %p: " s, m) */ 331149599Sandre 332149648Sandre for (m = m0; m != NULL; m = m->m_next) { 333149599Sandre /* 334149599Sandre * Basic pointer checks. If any of these fails then some 335149599Sandre * unrelated kernel memory before or after us is trashed. 336149599Sandre * No way to recover from that. 337149599Sandre */ 338149648Sandre a = ((m->m_flags & M_EXT) ? m->m_ext.ext_buf : 339149648Sandre ((m->m_flags & M_PKTHDR) ? (caddr_t)(&m->m_pktdat) : 340149599Sandre (caddr_t)(&m->m_dat)) ); 341149599Sandre b = (caddr_t)(a + (m->m_flags & M_EXT ? m->m_ext.ext_size : 342149648Sandre ((m->m_flags & M_PKTHDR) ? MHLEN : MLEN))); 343149599Sandre if ((caddr_t)m->m_data < a) 344149599Sandre M_SANITY_ACTION("m_data outside mbuf data range left"); 345149599Sandre if ((caddr_t)m->m_data > b) 346149599Sandre M_SANITY_ACTION("m_data outside mbuf data range right"); 347149599Sandre if ((caddr_t)m->m_data + m->m_len > b) 348149599Sandre M_SANITY_ACTION("m_data + m_len exeeds mbuf space"); 349149648Sandre if ((m->m_flags & M_PKTHDR) && m->m_pkthdr.header) { 350149599Sandre if ((caddr_t)m->m_pkthdr.header < a || 351149599Sandre (caddr_t)m->m_pkthdr.header > b) 352149599Sandre M_SANITY_ACTION("m_pkthdr.header outside mbuf data range"); 353149599Sandre } 354149599Sandre 355149599Sandre /* m->m_nextpkt may only be set on first mbuf in chain. */ 356149648Sandre if (m != m0 && m->m_nextpkt != NULL) { 357149599Sandre if (sanitize) { 358149599Sandre m_freem(m->m_nextpkt); 359149599Sandre m->m_nextpkt = (struct mbuf *)0xDEADC0DE; 360149599Sandre } else 361149599Sandre M_SANITY_ACTION("m->m_nextpkt on in-chain mbuf"); 362149599Sandre } 363149599Sandre 364149599Sandre /* correct type correlations. */ 365149599Sandre if (m->m_type == MT_HEADER && !(m->m_flags & M_PKTHDR)) { 366149599Sandre if (sanitize) 367149599Sandre m->m_type = MT_DATA; 368149599Sandre else 369149599Sandre M_SANITY_ACTION("MT_HEADER set but not M_PKTHDR"); 370149599Sandre } 371149599Sandre 372149599Sandre /* packet length (not mbuf length!) calculation */ 373149599Sandre if (m0->m_flags & M_PKTHDR) 374149599Sandre pktlen += m->m_len; 375149599Sandre 376149599Sandre /* m_tags may only be attached to first mbuf in chain. */ 377149599Sandre if (m != m0 && m->m_flags & M_PKTHDR && 378149599Sandre !SLIST_EMPTY(&m->m_pkthdr.tags)) { 379149599Sandre if (sanitize) { 380149599Sandre m_tag_delete_chain(m, NULL); 381149599Sandre /* put in 0xDEADC0DE perhaps? */ 382149648Sandre } else 383149599Sandre M_SANITY_ACTION("m_tags on in-chain mbuf"); 384149599Sandre } 385149599Sandre 386149599Sandre /* M_PKTHDR may only be set on first mbuf in chain */ 387149599Sandre if (m != m0 && m->m_flags & M_PKTHDR) { 388149599Sandre if (sanitize) { 389149599Sandre bzero(&m->m_pkthdr, sizeof(m->m_pkthdr)); 390149599Sandre m->m_flags &= ~M_PKTHDR; 391149599Sandre /* put in 0xDEADCODE and leave hdr flag in */ 392149599Sandre } else 393149599Sandre M_SANITY_ACTION("M_PKTHDR on in-chain mbuf"); 394149599Sandre } 395149599Sandre } 396149648Sandre m = m0; 397149648Sandre if (pktlen && pktlen != m->m_pkthdr.len) { 398149599Sandre if (sanitize) 399149648Sandre m->m_pkthdr.len = 0; 400149599Sandre else 401149599Sandre M_SANITY_ACTION("m_pkthdr.len != mbuf chain length"); 402149599Sandre } 403149648Sandre return 1; 404149648Sandre 405149599Sandre#undef M_SANITY_ACTION 406149599Sandre} 407149599Sandre 408149599Sandre 409149599Sandre/* 410108466Ssam * "Move" mbuf pkthdr from "from" to "to". 411100960Srwatson * "from" must have M_PKTHDR set, and "to" must be empty. 412100960Srwatson */ 413100960Srwatsonvoid 414108466Ssamm_move_pkthdr(struct mbuf *to, struct mbuf *from) 415100960Srwatson{ 416100960Srwatson 417100960Srwatson#if 0 418108466Ssam /* see below for why these are not enabled */ 419113255Sdes M_ASSERTPKTHDR(to); 420113487Srwatson /* Note: with MAC, this may not be a good assertion. */ 421108466Ssam KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), 422108466Ssam ("m_move_pkthdr: to has tags")); 423100960Srwatson#endif 424101007Srwatson#ifdef MAC 425113487Srwatson /* 426113487Srwatson * XXXMAC: It could be this should also occur for non-MAC? 427113487Srwatson */ 428101007Srwatson if (to->m_flags & M_PKTHDR) 429113487Srwatson m_tag_delete_chain(to, NULL); 430101007Srwatson#endif 431143302Ssam to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 432143302Ssam if ((to->m_flags & M_EXT) == 0) 433143302Ssam to->m_data = to->m_pktdat; 434108466Ssam to->m_pkthdr = from->m_pkthdr; /* especially tags */ 435108466Ssam SLIST_INIT(&from->m_pkthdr.tags); /* purge tags from src */ 436108466Ssam from->m_flags &= ~M_PKTHDR; 437108466Ssam} 438108466Ssam 439108466Ssam/* 440108466Ssam * Duplicate "from"'s mbuf pkthdr in "to". 441108466Ssam * "from" must have M_PKTHDR set, and "to" must be empty. 442108466Ssam * In particular, this does a deep copy of the packet tags. 443108466Ssam */ 444108466Ssamint 445108466Ssamm_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how) 446108466Ssam{ 447108466Ssam 448108466Ssam#if 0 449108466Ssam /* 450108466Ssam * The mbuf allocator only initializes the pkthdr 451108466Ssam * when the mbuf is allocated with MGETHDR. Many users 452108466Ssam * (e.g. m_copy*, m_prepend) use MGET and then 453108466Ssam * smash the pkthdr as needed causing these 454108466Ssam * assertions to trip. For now just disable them. 455108466Ssam */ 456113255Sdes M_ASSERTPKTHDR(to); 457113487Srwatson /* Note: with MAC, this may not be a good assertion. */ 458108466Ssam KASSERT(SLIST_EMPTY(&to->m_pkthdr.tags), ("m_dup_pkthdr: to has tags")); 459108466Ssam#endif 460132488Salfred MBUF_CHECKSLEEP(how); 461108466Ssam#ifdef MAC 462108466Ssam if (to->m_flags & M_PKTHDR) 463113487Srwatson m_tag_delete_chain(to, NULL); 464108466Ssam#endif 465112733Ssilby to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); 466112733Ssilby if ((to->m_flags & M_EXT) == 0) 467112733Ssilby to->m_data = to->m_pktdat; 468100960Srwatson to->m_pkthdr = from->m_pkthdr; 469108466Ssam SLIST_INIT(&to->m_pkthdr.tags); 470113480Srwatson return (m_tag_copy_chain(to, from, MBTOM(how))); 471100960Srwatson} 472100960Srwatson 473100960Srwatson/* 4741541Srgrimes * Lesser-used path for M_PREPEND: 4751541Srgrimes * allocate new mbuf to prepend to chain, 4761541Srgrimes * copy junk along. 4771541Srgrimes */ 4781541Srgrimesstruct mbuf * 47972356Sbmilekicm_prepend(struct mbuf *m, int len, int how) 4801541Srgrimes{ 4811541Srgrimes struct mbuf *mn; 4821541Srgrimes 483117770Ssilby if (m->m_flags & M_PKTHDR) 484117770Ssilby MGETHDR(mn, how, m->m_type); 485117770Ssilby else 486117770Ssilby MGET(mn, how, m->m_type); 48772356Sbmilekic if (mn == NULL) { 4881541Srgrimes m_freem(m); 48972356Sbmilekic return (NULL); 4901541Srgrimes } 491113487Srwatson if (m->m_flags & M_PKTHDR) 492108466Ssam M_MOVE_PKTHDR(mn, m); 4931541Srgrimes mn->m_next = m; 4941541Srgrimes m = mn; 4951541Srgrimes if (len < MHLEN) 4961541Srgrimes MH_ALIGN(m, len); 4971541Srgrimes m->m_len = len; 4981541Srgrimes return (m); 4991541Srgrimes} 5001541Srgrimes 5011541Srgrimes/* 5021541Srgrimes * Make a copy of an mbuf chain starting "off0" bytes from the beginning, 5031541Srgrimes * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. 504111119Simp * The wait parameter is a choice of M_TRYWAIT/M_DONTWAIT from caller. 50554002Sarchie * Note that the copy is read-only, because clusters are not copied, 50654002Sarchie * only their reference counts are incremented. 5071541Srgrimes */ 5081541Srgrimesstruct mbuf * 50972356Sbmilekicm_copym(struct mbuf *m, int off0, int len, int wait) 5101541Srgrimes{ 51172356Sbmilekic struct mbuf *n, **np; 51272356Sbmilekic int off = off0; 5131541Srgrimes struct mbuf *top; 5141541Srgrimes int copyhdr = 0; 5151541Srgrimes 51652201Salfred KASSERT(off >= 0, ("m_copym, negative off %d", off)); 51752201Salfred KASSERT(len >= 0, ("m_copym, negative len %d", len)); 518132488Salfred MBUF_CHECKSLEEP(wait); 5191541Srgrimes if (off == 0 && m->m_flags & M_PKTHDR) 5201541Srgrimes copyhdr = 1; 5211541Srgrimes while (off > 0) { 52252201Salfred KASSERT(m != NULL, ("m_copym, offset > size of mbuf chain")); 5231541Srgrimes if (off < m->m_len) 5241541Srgrimes break; 5251541Srgrimes off -= m->m_len; 5261541Srgrimes m = m->m_next; 5271541Srgrimes } 5281541Srgrimes np = ⊤ 5291541Srgrimes top = 0; 5301541Srgrimes while (len > 0) { 53172356Sbmilekic if (m == NULL) { 53252201Salfred KASSERT(len == M_COPYALL, 53352201Salfred ("m_copym, length > size of mbuf chain")); 5341541Srgrimes break; 5351541Srgrimes } 536117770Ssilby if (copyhdr) 537117770Ssilby MGETHDR(n, wait, m->m_type); 538117770Ssilby else 539117770Ssilby MGET(n, wait, m->m_type); 5401541Srgrimes *np = n; 54172356Sbmilekic if (n == NULL) 5421541Srgrimes goto nospace; 5431541Srgrimes if (copyhdr) { 544108466Ssam if (!m_dup_pkthdr(n, m, wait)) 545108466Ssam goto nospace; 5461541Srgrimes if (len == M_COPYALL) 5471541Srgrimes n->m_pkthdr.len -= off0; 5481541Srgrimes else 5491541Srgrimes n->m_pkthdr.len = len; 5501541Srgrimes copyhdr = 0; 5511541Srgrimes } 5521541Srgrimes n->m_len = min(len, m->m_len - off); 5531541Srgrimes if (m->m_flags & M_EXT) { 5541541Srgrimes n->m_data = m->m_data + off; 555151976Sandre mb_dupcl(n, m); 5561541Srgrimes } else 5571541Srgrimes bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 558103569Sbmilekic (u_int)n->m_len); 5591541Srgrimes if (len != M_COPYALL) 5601541Srgrimes len -= n->m_len; 5611541Srgrimes off = 0; 5621541Srgrimes m = m->m_next; 5631541Srgrimes np = &n->m_next; 5641541Srgrimes } 56578592Sbmilekic if (top == NULL) 56678592Sbmilekic mbstat.m_mcfail++; /* XXX: No consistency. */ 56778592Sbmilekic 5681541Srgrimes return (top); 5691541Srgrimesnospace: 5701541Srgrimes m_freem(top); 57178592Sbmilekic mbstat.m_mcfail++; /* XXX: No consistency. */ 57272356Sbmilekic return (NULL); 5731541Srgrimes} 5741541Srgrimes 5751541Srgrimes/* 576149602Sandre * Returns mbuf chain with new head for the prepending case. 577149602Sandre * Copies from mbuf (chain) n from off for len to mbuf (chain) m 578149602Sandre * either prepending or appending the data. 579149602Sandre * The resulting mbuf (chain) m is fully writeable. 580149602Sandre * m is destination (is made writeable) 581149602Sandre * n is source, off is offset in source, len is len from offset 582149602Sandre * dir, 0 append, 1 prepend 583149602Sandre * how, wait or nowait 584149602Sandre */ 585149602Sandre 586149602Sandrestatic int 587149602Sandrem_bcopyxxx(void *s, void *t, u_int len) 588149602Sandre{ 589149602Sandre bcopy(s, t, (size_t)len); 590149602Sandre return 0; 591149602Sandre} 592149602Sandre 593149602Sandrestruct mbuf * 594149602Sandrem_copymdata(struct mbuf *m, struct mbuf *n, int off, int len, 595149602Sandre int prep, int how) 596149602Sandre{ 597151976Sandre struct mbuf *mm, *x, *z, *prev = NULL; 598149602Sandre caddr_t p; 599151976Sandre int i, nlen = 0; 600149602Sandre caddr_t buf[MLEN]; 601149602Sandre 602149602Sandre KASSERT(m != NULL && n != NULL, ("m_copymdata, no target or source")); 603149602Sandre KASSERT(off >= 0, ("m_copymdata, negative off %d", off)); 604149602Sandre KASSERT(len >= 0, ("m_copymdata, negative len %d", len)); 605149602Sandre KASSERT(prep == 0 || prep == 1, ("m_copymdata, unknown direction %d", prep)); 606149602Sandre 607151976Sandre mm = m; 608151976Sandre if (!prep) { 609151976Sandre while(mm->m_next) { 610151976Sandre prev = mm; 611151976Sandre mm = mm->m_next; 612149602Sandre } 613149602Sandre } 614149602Sandre for (z = n; z != NULL; z = z->m_next) 615149602Sandre nlen += z->m_len; 616149602Sandre if (len == M_COPYALL) 617149602Sandre len = nlen - off; 618149602Sandre if (off + len > nlen || len < 1) 619149602Sandre return NULL; 620149602Sandre 621151976Sandre if (!M_WRITABLE(mm)) { 622151976Sandre /* XXX: Use proper m_xxx function instead. */ 623151976Sandre x = m_getcl(how, MT_DATA, mm->m_flags); 624151976Sandre if (x == NULL) 625151976Sandre return NULL; 626151976Sandre bcopy(mm->m_ext.ext_buf, x->m_ext.ext_buf, x->m_ext.ext_size); 627151976Sandre p = x->m_ext.ext_buf + (mm->m_data - mm->m_ext.ext_buf); 628151976Sandre x->m_data = p; 629151976Sandre mm->m_next = NULL; 630151976Sandre if (mm != m) 631151976Sandre prev->m_next = x; 632151976Sandre m_free(mm); 633151976Sandre mm = x; 634151976Sandre } 635151976Sandre 636149602Sandre /* 637149602Sandre * Append/prepend the data. Allocating mbufs as necessary. 638149602Sandre */ 639149602Sandre /* Shortcut if enough free space in first/last mbuf. */ 640149602Sandre if (!prep && M_TRAILINGSPACE(mm) >= len) { 641149602Sandre m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t) + 642149602Sandre mm->m_len); 643149602Sandre mm->m_len += len; 644149602Sandre mm->m_pkthdr.len += len; 645149602Sandre return m; 646149602Sandre } 647149602Sandre if (prep && M_LEADINGSPACE(mm) >= len) { 648149602Sandre mm->m_data = mtod(mm, caddr_t) - len; 649149602Sandre m_apply(n, off, len, m_bcopyxxx, mtod(mm, caddr_t)); 650149602Sandre mm->m_len += len; 651149602Sandre mm->m_pkthdr.len += len; 652149602Sandre return mm; 653149602Sandre } 654149602Sandre 655149602Sandre /* Expand first/last mbuf to cluster if possible. */ 656149602Sandre if (!prep && !(mm->m_flags & M_EXT) && len > M_TRAILINGSPACE(mm)) { 657149602Sandre bcopy(mm->m_data, &buf, mm->m_len); 658149602Sandre m_clget(mm, how); 659149602Sandre if (!(mm->m_flags & M_EXT)) 660149602Sandre return NULL; 661149602Sandre bcopy(&buf, mm->m_ext.ext_buf, mm->m_len); 662149602Sandre mm->m_data = mm->m_ext.ext_buf; 663149602Sandre mm->m_pkthdr.header = NULL; 664149602Sandre } 665149602Sandre if (prep && !(mm->m_flags & M_EXT) && len > M_LEADINGSPACE(mm)) { 666149602Sandre bcopy(mm->m_data, &buf, mm->m_len); 667149602Sandre m_clget(mm, how); 668149602Sandre if (!(mm->m_flags & M_EXT)) 669149602Sandre return NULL; 670149602Sandre bcopy(&buf, (caddr_t *)mm->m_ext.ext_buf + 671149602Sandre mm->m_ext.ext_size - mm->m_len, mm->m_len); 672149602Sandre mm->m_data = (caddr_t)mm->m_ext.ext_buf + 673149602Sandre mm->m_ext.ext_size - mm->m_len; 674149602Sandre mm->m_pkthdr.header = NULL; 675149602Sandre } 676149602Sandre 677149602Sandre /* Append/prepend as many mbuf (clusters) as necessary to fit len. */ 678149602Sandre if (!prep && len > M_TRAILINGSPACE(mm)) { 679149602Sandre if (!m_getm(mm, len - M_TRAILINGSPACE(mm), how, MT_DATA)) 680149602Sandre return NULL; 681149602Sandre } 682149602Sandre if (prep && len > M_LEADINGSPACE(mm)) { 683149602Sandre if (!(z = m_getm(NULL, len - M_LEADINGSPACE(mm), how, MT_DATA))) 684149602Sandre return NULL; 685149602Sandre i = 0; 686149602Sandre for (x = z; x != NULL; x = x->m_next) { 687149602Sandre i += x->m_flags & M_EXT ? x->m_ext.ext_size : 688149602Sandre (x->m_flags & M_PKTHDR ? MHLEN : MLEN); 689149602Sandre if (!x->m_next) 690149602Sandre break; 691149602Sandre } 692149602Sandre z->m_data += i - len; 693149602Sandre m_move_pkthdr(mm, z); 694149602Sandre x->m_next = mm; 695149602Sandre mm = z; 696149602Sandre } 697149602Sandre 698149602Sandre /* Seek to start position in source mbuf. Optimization for long chains. */ 699149602Sandre while (off > 0) { 700149602Sandre if (off < n->m_len) 701149602Sandre break; 702149602Sandre off -= n->m_len; 703149602Sandre n = n->m_next; 704149602Sandre } 705149602Sandre 706149602Sandre /* Copy data into target mbuf. */ 707149602Sandre z = mm; 708149602Sandre while (len > 0) { 709149602Sandre KASSERT(z != NULL, ("m_copymdata, falling off target edge")); 710149602Sandre i = M_TRAILINGSPACE(z); 711149602Sandre m_apply(n, off, i, m_bcopyxxx, mtod(z, caddr_t) + z->m_len); 712149602Sandre z->m_len += i; 713149602Sandre /* fixup pkthdr.len if necessary */ 714149602Sandre if ((prep ? mm : m)->m_flags & M_PKTHDR) 715149602Sandre (prep ? mm : m)->m_pkthdr.len += i; 716149602Sandre off += i; 717149602Sandre len -= i; 718149602Sandre z = z->m_next; 719149602Sandre } 720149602Sandre return (prep ? mm : m); 721149602Sandre} 722149602Sandre 723149602Sandre/* 72415689Swollman * Copy an entire packet, including header (which must be present). 72515689Swollman * An optimization of the common case `m_copym(m, 0, M_COPYALL, how)'. 72654002Sarchie * Note that the copy is read-only, because clusters are not copied, 72754002Sarchie * only their reference counts are incremented. 72872750Sluigi * Preserve alignment of the first mbuf so if the creator has left 72972750Sluigi * some room at the beginning (e.g. for inserting protocol headers) 73072750Sluigi * the copies still have the room available. 73115689Swollman */ 73215689Swollmanstruct mbuf * 73372356Sbmilekicm_copypacket(struct mbuf *m, int how) 73415689Swollman{ 73515689Swollman struct mbuf *top, *n, *o; 73615689Swollman 737132488Salfred MBUF_CHECKSLEEP(how); 73815689Swollman MGET(n, how, m->m_type); 73915689Swollman top = n; 74072356Sbmilekic if (n == NULL) 74115689Swollman goto nospace; 74215689Swollman 743108466Ssam if (!m_dup_pkthdr(n, m, how)) 744108466Ssam goto nospace; 74515689Swollman n->m_len = m->m_len; 74615689Swollman if (m->m_flags & M_EXT) { 74715689Swollman n->m_data = m->m_data; 748151976Sandre mb_dupcl(n, m); 74915689Swollman } else { 75072750Sluigi n->m_data = n->m_pktdat + (m->m_data - m->m_pktdat ); 75115689Swollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 75215689Swollman } 75315689Swollman 75415689Swollman m = m->m_next; 75515689Swollman while (m) { 75615689Swollman MGET(o, how, m->m_type); 75772356Sbmilekic if (o == NULL) 75815689Swollman goto nospace; 75915689Swollman 76015689Swollman n->m_next = o; 76115689Swollman n = n->m_next; 76215689Swollman 76315689Swollman n->m_len = m->m_len; 76415689Swollman if (m->m_flags & M_EXT) { 76515689Swollman n->m_data = m->m_data; 766151976Sandre mb_dupcl(n, m); 76715689Swollman } else { 76815689Swollman bcopy(mtod(m, char *), mtod(n, char *), n->m_len); 76915689Swollman } 77015689Swollman 77115689Swollman m = m->m_next; 77215689Swollman } 77315689Swollman return top; 77415689Swollmannospace: 77515689Swollman m_freem(top); 77678592Sbmilekic mbstat.m_mcfail++; /* XXX: No consistency. */ 77772356Sbmilekic return (NULL); 77815689Swollman} 77915689Swollman 78015689Swollman/* 7811541Srgrimes * Copy data from an mbuf chain starting "off" bytes from the beginning, 7821541Srgrimes * continuing for "len" bytes, into the indicated buffer. 7831541Srgrimes */ 7841549Srgrimesvoid 78581907Sjulianm_copydata(const struct mbuf *m, int off, int len, caddr_t cp) 7861541Srgrimes{ 787103569Sbmilekic u_int count; 7881541Srgrimes 78952201Salfred KASSERT(off >= 0, ("m_copydata, negative off %d", off)); 79052201Salfred KASSERT(len >= 0, ("m_copydata, negative len %d", len)); 7911541Srgrimes while (off > 0) { 79252201Salfred KASSERT(m != NULL, ("m_copydata, offset > size of mbuf chain")); 7931541Srgrimes if (off < m->m_len) 7941541Srgrimes break; 7951541Srgrimes off -= m->m_len; 7961541Srgrimes m = m->m_next; 7971541Srgrimes } 7981541Srgrimes while (len > 0) { 79952201Salfred KASSERT(m != NULL, ("m_copydata, length > size of mbuf chain")); 8001541Srgrimes count = min(m->m_len - off, len); 8011541Srgrimes bcopy(mtod(m, caddr_t) + off, cp, count); 8021541Srgrimes len -= count; 8031541Srgrimes cp += count; 8041541Srgrimes off = 0; 8051541Srgrimes m = m->m_next; 8061541Srgrimes } 8071541Srgrimes} 8081541Srgrimes 8091541Srgrimes/* 81054002Sarchie * Copy a packet header mbuf chain into a completely new chain, including 81154002Sarchie * copying any mbuf clusters. Use this instead of m_copypacket() when 81254002Sarchie * you need a writable copy of an mbuf chain. 81354002Sarchie */ 81454002Sarchiestruct mbuf * 81572356Sbmilekicm_dup(struct mbuf *m, int how) 81654002Sarchie{ 81754002Sarchie struct mbuf **p, *top = NULL; 81854002Sarchie int remain, moff, nsize; 81954002Sarchie 820132488Salfred MBUF_CHECKSLEEP(how); 82154002Sarchie /* Sanity check */ 82254002Sarchie if (m == NULL) 82372356Sbmilekic return (NULL); 824113255Sdes M_ASSERTPKTHDR(m); 82554002Sarchie 82654002Sarchie /* While there's more data, get a new mbuf, tack it on, and fill it */ 82754002Sarchie remain = m->m_pkthdr.len; 82854002Sarchie moff = 0; 82954002Sarchie p = ⊤ 83054002Sarchie while (remain > 0 || top == NULL) { /* allow m->m_pkthdr.len == 0 */ 83154002Sarchie struct mbuf *n; 83254002Sarchie 83354002Sarchie /* Get the next new mbuf */ 834129906Sbmilekic if (remain >= MINCLSIZE) { 835129906Sbmilekic n = m_getcl(how, m->m_type, 0); 836129906Sbmilekic nsize = MCLBYTES; 837129906Sbmilekic } else { 838129906Sbmilekic n = m_get(how, m->m_type); 839129906Sbmilekic nsize = MLEN; 840129906Sbmilekic } 84154002Sarchie if (n == NULL) 84254002Sarchie goto nospace; 843129906Sbmilekic 844129906Sbmilekic if (top == NULL) { /* First one, must be PKTHDR */ 845129906Sbmilekic if (!m_dup_pkthdr(n, m, how)) { 846129906Sbmilekic m_free(n); 847108466Ssam goto nospace; 848129906Sbmilekic } 84954002Sarchie nsize = MHLEN; 85054002Sarchie } 85154002Sarchie n->m_len = 0; 85254002Sarchie 85354002Sarchie /* Link it into the new chain */ 85454002Sarchie *p = n; 85554002Sarchie p = &n->m_next; 85654002Sarchie 85754002Sarchie /* Copy data from original mbuf(s) into new mbuf */ 85854002Sarchie while (n->m_len < nsize && m != NULL) { 85954002Sarchie int chunk = min(nsize - n->m_len, m->m_len - moff); 86054002Sarchie 86154002Sarchie bcopy(m->m_data + moff, n->m_data + n->m_len, chunk); 86254002Sarchie moff += chunk; 86354002Sarchie n->m_len += chunk; 86454002Sarchie remain -= chunk; 86554002Sarchie if (moff == m->m_len) { 86654002Sarchie m = m->m_next; 86754002Sarchie moff = 0; 86854002Sarchie } 86954002Sarchie } 87054002Sarchie 87154002Sarchie /* Check correct total mbuf length */ 87254002Sarchie KASSERT((remain > 0 && m != NULL) || (remain == 0 && m == NULL), 87387594Sobrien ("%s: bogus m_pkthdr.len", __func__)); 87454002Sarchie } 87554002Sarchie return (top); 87654002Sarchie 87754002Sarchienospace: 87854002Sarchie m_freem(top); 87978592Sbmilekic mbstat.m_mcfail++; /* XXX: No consistency. */ 88072356Sbmilekic return (NULL); 88154002Sarchie} 88254002Sarchie 88354002Sarchie/* 8841541Srgrimes * Concatenate mbuf chain n to m. 8851541Srgrimes * Both chains must be of the same type (e.g. MT_DATA). 8861541Srgrimes * Any m_pkthdr is not updated. 8871541Srgrimes */ 8881549Srgrimesvoid 88972356Sbmilekicm_cat(struct mbuf *m, struct mbuf *n) 8901541Srgrimes{ 8911541Srgrimes while (m->m_next) 8921541Srgrimes m = m->m_next; 8931541Srgrimes while (n) { 8941541Srgrimes if (m->m_flags & M_EXT || 8951541Srgrimes m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { 8961541Srgrimes /* just join the two chains */ 8971541Srgrimes m->m_next = n; 8981541Srgrimes return; 8991541Srgrimes } 9001541Srgrimes /* splat the data from one into the other */ 9011541Srgrimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 9021541Srgrimes (u_int)n->m_len); 9031541Srgrimes m->m_len += n->m_len; 9041541Srgrimes n = m_free(n); 9051541Srgrimes } 9061541Srgrimes} 9071541Srgrimes 9081549Srgrimesvoid 90972356Sbmilekicm_adj(struct mbuf *mp, int req_len) 9101541Srgrimes{ 91172356Sbmilekic int len = req_len; 91272356Sbmilekic struct mbuf *m; 91372356Sbmilekic int count; 9141541Srgrimes 9151541Srgrimes if ((m = mp) == NULL) 9161541Srgrimes return; 9171541Srgrimes if (len >= 0) { 9181541Srgrimes /* 9191541Srgrimes * Trim from head. 9201541Srgrimes */ 9211541Srgrimes while (m != NULL && len > 0) { 9221541Srgrimes if (m->m_len <= len) { 9231541Srgrimes len -= m->m_len; 9241541Srgrimes m->m_len = 0; 9251541Srgrimes m = m->m_next; 9261541Srgrimes } else { 9271541Srgrimes m->m_len -= len; 9281541Srgrimes m->m_data += len; 9291541Srgrimes len = 0; 9301541Srgrimes } 9311541Srgrimes } 9321541Srgrimes m = mp; 9331541Srgrimes if (mp->m_flags & M_PKTHDR) 9341541Srgrimes m->m_pkthdr.len -= (req_len - len); 9351541Srgrimes } else { 9361541Srgrimes /* 9371541Srgrimes * Trim from tail. Scan the mbuf chain, 9381541Srgrimes * calculating its length and finding the last mbuf. 9391541Srgrimes * If the adjustment only affects this mbuf, then just 9401541Srgrimes * adjust and return. Otherwise, rescan and truncate 9411541Srgrimes * after the remaining size. 9421541Srgrimes */ 9431541Srgrimes len = -len; 9441541Srgrimes count = 0; 9451541Srgrimes for (;;) { 9461541Srgrimes count += m->m_len; 9471541Srgrimes if (m->m_next == (struct mbuf *)0) 9481541Srgrimes break; 9491541Srgrimes m = m->m_next; 9501541Srgrimes } 9511541Srgrimes if (m->m_len >= len) { 9521541Srgrimes m->m_len -= len; 9531541Srgrimes if (mp->m_flags & M_PKTHDR) 9541541Srgrimes mp->m_pkthdr.len -= len; 9551541Srgrimes return; 9561541Srgrimes } 9571541Srgrimes count -= len; 9581541Srgrimes if (count < 0) 9591541Srgrimes count = 0; 9601541Srgrimes /* 9611541Srgrimes * Correct length for chain is "count". 9621541Srgrimes * Find the mbuf with last data, adjust its length, 9631541Srgrimes * and toss data from remaining mbufs on chain. 9641541Srgrimes */ 9651541Srgrimes m = mp; 9661541Srgrimes if (m->m_flags & M_PKTHDR) 9671541Srgrimes m->m_pkthdr.len = count; 9681541Srgrimes for (; m; m = m->m_next) { 9691541Srgrimes if (m->m_len >= count) { 9701541Srgrimes m->m_len = count; 971142350Ssam if (m->m_next != NULL) { 972142350Ssam m_freem(m->m_next); 973142350Ssam m->m_next = NULL; 974142350Ssam } 9751541Srgrimes break; 9761541Srgrimes } 9771541Srgrimes count -= m->m_len; 9781541Srgrimes } 9791541Srgrimes } 9801541Srgrimes} 9811541Srgrimes 9821541Srgrimes/* 9831541Srgrimes * Rearange an mbuf chain so that len bytes are contiguous 9841541Srgrimes * and in the data area of an mbuf (so that mtod and dtom 9851541Srgrimes * will work for a structure of size len). Returns the resulting 9861541Srgrimes * mbuf chain on success, frees it and returns null on failure. 9871541Srgrimes * If there is room, it will add up to max_protohdr-len extra bytes to the 9881541Srgrimes * contiguous region in an attempt to avoid being called next time. 9891541Srgrimes */ 9901541Srgrimesstruct mbuf * 99172356Sbmilekicm_pullup(struct mbuf *n, int len) 9921541Srgrimes{ 99372356Sbmilekic struct mbuf *m; 99472356Sbmilekic int count; 9951541Srgrimes int space; 9961541Srgrimes 9971541Srgrimes /* 9981541Srgrimes * If first mbuf has no cluster, and has room for len bytes 9991541Srgrimes * without shifting current data, pullup into it, 10001541Srgrimes * otherwise allocate a new mbuf to prepend to the chain. 10011541Srgrimes */ 10021541Srgrimes if ((n->m_flags & M_EXT) == 0 && 10031541Srgrimes n->m_data + len < &n->m_dat[MLEN] && n->m_next) { 10041541Srgrimes if (n->m_len >= len) 10051541Srgrimes return (n); 10061541Srgrimes m = n; 10071541Srgrimes n = n->m_next; 10081541Srgrimes len -= m->m_len; 10091541Srgrimes } else { 10101541Srgrimes if (len > MHLEN) 10111541Srgrimes goto bad; 1012111119Simp MGET(m, M_DONTWAIT, n->m_type); 101372356Sbmilekic if (m == NULL) 10141541Srgrimes goto bad; 10151541Srgrimes m->m_len = 0; 1016108466Ssam if (n->m_flags & M_PKTHDR) 1017108466Ssam M_MOVE_PKTHDR(m, n); 10181541Srgrimes } 10191541Srgrimes space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 10201541Srgrimes do { 10211541Srgrimes count = min(min(max(len, max_protohdr), space), n->m_len); 10221541Srgrimes bcopy(mtod(n, caddr_t), mtod(m, caddr_t) + m->m_len, 1023103569Sbmilekic (u_int)count); 10241541Srgrimes len -= count; 10251541Srgrimes m->m_len += count; 10261541Srgrimes n->m_len -= count; 10271541Srgrimes space -= count; 10281541Srgrimes if (n->m_len) 10291541Srgrimes n->m_data += count; 10301541Srgrimes else 10311541Srgrimes n = m_free(n); 10321541Srgrimes } while (len > 0 && n); 10331541Srgrimes if (len > 0) { 10341541Srgrimes (void) m_free(m); 10351541Srgrimes goto bad; 10361541Srgrimes } 10371541Srgrimes m->m_next = n; 10381541Srgrimes return (m); 10391541Srgrimesbad: 10401541Srgrimes m_freem(n); 104178592Sbmilekic mbstat.m_mpfail++; /* XXX: No consistency. */ 104272356Sbmilekic return (NULL); 10431541Srgrimes} 10441541Srgrimes 10451541Srgrimes/* 1046143761Sjmg * Like m_pullup(), except a new mbuf is always allocated, and we allow 1047143761Sjmg * the amount of empty space before the data in the new mbuf to be specified 1048143761Sjmg * (in the event that the caller expects to prepend later). 1049143761Sjmg */ 1050143761Sjmgint MSFail; 1051143761Sjmg 1052143761Sjmgstruct mbuf * 1053143761Sjmgm_copyup(struct mbuf *n, int len, int dstoff) 1054143761Sjmg{ 1055143761Sjmg struct mbuf *m; 1056143761Sjmg int count, space; 1057143761Sjmg 1058143761Sjmg if (len > (MHLEN - dstoff)) 1059143761Sjmg goto bad; 1060143761Sjmg MGET(m, M_DONTWAIT, n->m_type); 1061143761Sjmg if (m == NULL) 1062143761Sjmg goto bad; 1063143761Sjmg m->m_len = 0; 1064143761Sjmg if (n->m_flags & M_PKTHDR) 1065143761Sjmg M_MOVE_PKTHDR(m, n); 1066143761Sjmg m->m_data += dstoff; 1067143761Sjmg space = &m->m_dat[MLEN] - (m->m_data + m->m_len); 1068143761Sjmg do { 1069143761Sjmg count = min(min(max(len, max_protohdr), space), n->m_len); 1070143761Sjmg memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t), 1071143761Sjmg (unsigned)count); 1072143761Sjmg len -= count; 1073143761Sjmg m->m_len += count; 1074143761Sjmg n->m_len -= count; 1075143761Sjmg space -= count; 1076143761Sjmg if (n->m_len) 1077143761Sjmg n->m_data += count; 1078143761Sjmg else 1079143761Sjmg n = m_free(n); 1080143761Sjmg } while (len > 0 && n); 1081143761Sjmg if (len > 0) { 1082143761Sjmg (void) m_free(m); 1083143761Sjmg goto bad; 1084143761Sjmg } 1085143761Sjmg m->m_next = n; 1086143761Sjmg return (m); 1087143761Sjmg bad: 1088143761Sjmg m_freem(n); 1089143761Sjmg MSFail++; 1090143761Sjmg return (NULL); 1091143761Sjmg} 1092143761Sjmg 1093143761Sjmg/* 10941541Srgrimes * Partition an mbuf chain in two pieces, returning the tail -- 10951541Srgrimes * all but the first len0 bytes. In case of failure, it returns NULL and 10961541Srgrimes * attempts to restore the chain to its original state. 109797681Sarchie * 109897681Sarchie * Note that the resulting mbufs might be read-only, because the new 109997681Sarchie * mbuf can end up sharing an mbuf cluster with the original mbuf if 110097681Sarchie * the "breaking point" happens to lie within a cluster mbuf. Use the 110197681Sarchie * M_WRITABLE() macro to check for this case. 11021541Srgrimes */ 11031541Srgrimesstruct mbuf * 110472356Sbmilekicm_split(struct mbuf *m0, int len0, int wait) 11051541Srgrimes{ 110672356Sbmilekic struct mbuf *m, *n; 1107103569Sbmilekic u_int len = len0, remain; 11081541Srgrimes 1109132488Salfred MBUF_CHECKSLEEP(wait); 11101541Srgrimes for (m = m0; m && len > m->m_len; m = m->m_next) 11111541Srgrimes len -= m->m_len; 111272356Sbmilekic if (m == NULL) 111372356Sbmilekic return (NULL); 11141541Srgrimes remain = m->m_len - len; 11151541Srgrimes if (m0->m_flags & M_PKTHDR) { 11161541Srgrimes MGETHDR(n, wait, m0->m_type); 111772356Sbmilekic if (n == NULL) 111872356Sbmilekic return (NULL); 11191541Srgrimes n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; 11201541Srgrimes n->m_pkthdr.len = m0->m_pkthdr.len - len0; 11211541Srgrimes m0->m_pkthdr.len = len0; 11221541Srgrimes if (m->m_flags & M_EXT) 11231541Srgrimes goto extpacket; 11241541Srgrimes if (remain > MHLEN) { 11251541Srgrimes /* m can't be the lead packet */ 11261541Srgrimes MH_ALIGN(n, 0); 11271541Srgrimes n->m_next = m_split(m, len, wait); 112872356Sbmilekic if (n->m_next == NULL) { 11291541Srgrimes (void) m_free(n); 113072356Sbmilekic return (NULL); 113194471Shsu } else { 113294471Shsu n->m_len = 0; 11331541Srgrimes return (n); 113494471Shsu } 11351541Srgrimes } else 11361541Srgrimes MH_ALIGN(n, remain); 11371541Srgrimes } else if (remain == 0) { 11381541Srgrimes n = m->m_next; 113972356Sbmilekic m->m_next = NULL; 11401541Srgrimes return (n); 11411541Srgrimes } else { 11421541Srgrimes MGET(n, wait, m->m_type); 114372356Sbmilekic if (n == NULL) 114472356Sbmilekic return (NULL); 11451541Srgrimes M_ALIGN(n, remain); 11461541Srgrimes } 11471541Srgrimesextpacket: 11481541Srgrimes if (m->m_flags & M_EXT) { 11491541Srgrimes n->m_data = m->m_data + len; 1150151976Sandre mb_dupcl(n, m); 11511541Srgrimes } else { 11521541Srgrimes bcopy(mtod(m, caddr_t) + len, mtod(n, caddr_t), remain); 11531541Srgrimes } 11541541Srgrimes n->m_len = remain; 11551541Srgrimes m->m_len = len; 11561541Srgrimes n->m_next = m->m_next; 115772356Sbmilekic m->m_next = NULL; 11581541Srgrimes return (n); 11591541Srgrimes} 11601541Srgrimes/* 11611541Srgrimes * Routine to copy from device local memory into mbufs. 116278508Sbmilekic * Note that `off' argument is offset into first mbuf of target chain from 116378508Sbmilekic * which to begin copying the data to. 11641541Srgrimes */ 11651541Srgrimesstruct mbuf * 116678508Sbmilekicm_devget(char *buf, int totlen, int off, struct ifnet *ifp, 116772356Sbmilekic void (*copy)(char *from, caddr_t to, u_int len)) 11681541Srgrimes{ 116972356Sbmilekic struct mbuf *m; 1170129906Sbmilekic struct mbuf *top = NULL, **mp = ⊤ 117178508Sbmilekic int len; 11721541Srgrimes 117378508Sbmilekic if (off < 0 || off > MHLEN) 117478508Sbmilekic return (NULL); 117578508Sbmilekic 1176129906Sbmilekic while (totlen > 0) { 1177129906Sbmilekic if (top == NULL) { /* First one, must be PKTHDR */ 1178129906Sbmilekic if (totlen + off >= MINCLSIZE) { 1179129906Sbmilekic m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1180129906Sbmilekic len = MCLBYTES; 1181129906Sbmilekic } else { 1182129906Sbmilekic m = m_gethdr(M_DONTWAIT, MT_DATA); 1183129906Sbmilekic len = MHLEN; 11841541Srgrimes 1185129906Sbmilekic /* Place initial small packet/header at end of mbuf */ 1186129906Sbmilekic if (m && totlen + off + max_linkhdr <= MLEN) { 1187129906Sbmilekic m->m_data += max_linkhdr; 1188129906Sbmilekic len -= max_linkhdr; 1189129906Sbmilekic } 1190129906Sbmilekic } 1191129906Sbmilekic if (m == NULL) 1192129906Sbmilekic return NULL; 1193129906Sbmilekic m->m_pkthdr.rcvif = ifp; 1194129906Sbmilekic m->m_pkthdr.len = totlen; 1195129906Sbmilekic } else { 1196129906Sbmilekic if (totlen + off >= MINCLSIZE) { 1197129906Sbmilekic m = m_getcl(M_DONTWAIT, MT_DATA, 0); 1198129906Sbmilekic len = MCLBYTES; 1199129906Sbmilekic } else { 1200129906Sbmilekic m = m_get(M_DONTWAIT, MT_DATA); 1201129906Sbmilekic len = MLEN; 1202129906Sbmilekic } 120372356Sbmilekic if (m == NULL) { 12041541Srgrimes m_freem(top); 1205129906Sbmilekic return NULL; 12061541Srgrimes } 12071541Srgrimes } 120878508Sbmilekic if (off) { 120978508Sbmilekic m->m_data += off; 121078508Sbmilekic len -= off; 121178508Sbmilekic off = 0; 121278508Sbmilekic } 121378508Sbmilekic m->m_len = len = min(totlen, len); 12141541Srgrimes if (copy) 1215103569Sbmilekic copy(buf, mtod(m, caddr_t), (u_int)len); 12161541Srgrimes else 1217103569Sbmilekic bcopy(buf, mtod(m, caddr_t), (u_int)len); 121878508Sbmilekic buf += len; 12191541Srgrimes *mp = m; 12201541Srgrimes mp = &m->m_next; 12211541Srgrimes totlen -= len; 12221541Srgrimes } 12231541Srgrimes return (top); 12241541Srgrimes} 12253352Sphk 12263352Sphk/* 12273352Sphk * Copy data from a buffer back into the indicated mbuf chain, 12283352Sphk * starting "off" bytes from the beginning, extending the mbuf 12293352Sphk * chain if necessary. 12303352Sphk */ 12313352Sphkvoid 1232128402Sluigim_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp) 12333352Sphk{ 123472356Sbmilekic int mlen; 123572356Sbmilekic struct mbuf *m = m0, *n; 12363352Sphk int totlen = 0; 12373352Sphk 123872356Sbmilekic if (m0 == NULL) 12393352Sphk return; 12403352Sphk while (off > (mlen = m->m_len)) { 12413352Sphk off -= mlen; 12423352Sphk totlen += mlen; 124372356Sbmilekic if (m->m_next == NULL) { 1244129906Sbmilekic n = m_get(M_DONTWAIT, m->m_type); 124572356Sbmilekic if (n == NULL) 12463352Sphk goto out; 1247129906Sbmilekic bzero(mtod(n, caddr_t), MLEN); 12483352Sphk n->m_len = min(MLEN, len + off); 12493352Sphk m->m_next = n; 12503352Sphk } 12513352Sphk m = m->m_next; 12523352Sphk } 12533352Sphk while (len > 0) { 12543352Sphk mlen = min (m->m_len - off, len); 1255103569Sbmilekic bcopy(cp, off + mtod(m, caddr_t), (u_int)mlen); 12563352Sphk cp += mlen; 12573352Sphk len -= mlen; 12583352Sphk mlen += off; 12593352Sphk off = 0; 12603352Sphk totlen += mlen; 12613352Sphk if (len == 0) 12623352Sphk break; 126372356Sbmilekic if (m->m_next == NULL) { 1264111119Simp n = m_get(M_DONTWAIT, m->m_type); 126572356Sbmilekic if (n == NULL) 12663352Sphk break; 12673352Sphk n->m_len = min(MLEN, len); 12683352Sphk m->m_next = n; 12693352Sphk } 12703352Sphk m = m->m_next; 12713352Sphk } 12723352Sphkout: if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) 12733352Sphk m->m_pkthdr.len = totlen; 12743352Sphk} 127552756Sphk 1276123557Sbms/* 1277138541Ssam * Append the specified data to the indicated mbuf chain, 1278138541Ssam * Extend the mbuf chain if the new data does not fit in 1279138541Ssam * existing space. 1280138541Ssam * 1281138541Ssam * Return 1 if able to complete the job; otherwise 0. 1282138541Ssam */ 1283138541Ssamint 1284138541Ssamm_append(struct mbuf *m0, int len, c_caddr_t cp) 1285138541Ssam{ 1286138541Ssam struct mbuf *m, *n; 1287138541Ssam int remainder, space; 1288138541Ssam 1289138541Ssam for (m = m0; m->m_next != NULL; m = m->m_next) 1290138541Ssam ; 1291138541Ssam remainder = len; 1292138541Ssam space = M_TRAILINGSPACE(m); 1293138541Ssam if (space > 0) { 1294138541Ssam /* 1295138541Ssam * Copy into available space. 1296138541Ssam */ 1297138541Ssam if (space > remainder) 1298138541Ssam space = remainder; 1299138541Ssam bcopy(cp, mtod(m, caddr_t) + m->m_len, space); 1300138541Ssam m->m_len += space; 1301138541Ssam cp += space, remainder -= space; 1302138541Ssam } 1303138541Ssam while (remainder > 0) { 1304138541Ssam /* 1305138541Ssam * Allocate a new mbuf; could check space 1306138541Ssam * and allocate a cluster instead. 1307138541Ssam */ 1308138541Ssam n = m_get(M_DONTWAIT, m->m_type); 1309138541Ssam if (n == NULL) 1310138541Ssam break; 1311138541Ssam n->m_len = min(MLEN, remainder); 1312138894Ssam bcopy(cp, mtod(n, caddr_t), n->m_len); 1313138894Ssam cp += n->m_len, remainder -= n->m_len; 1314138541Ssam m->m_next = n; 1315138541Ssam m = n; 1316138541Ssam } 1317138541Ssam if (m0->m_flags & M_PKTHDR) 1318138541Ssam m0->m_pkthdr.len += len - remainder; 1319138541Ssam return (remainder == 0); 1320138541Ssam} 1321138541Ssam 1322138541Ssam/* 1323123557Sbms * Apply function f to the data in an mbuf chain starting "off" bytes from 1324123557Sbms * the beginning, continuing for "len" bytes. 1325123557Sbms */ 1326123557Sbmsint 1327123557Sbmsm_apply(struct mbuf *m, int off, int len, 1328123564Sbms int (*f)(void *, void *, u_int), void *arg) 1329123557Sbms{ 1330123564Sbms u_int count; 1331123557Sbms int rval; 1332123557Sbms 1333123557Sbms KASSERT(off >= 0, ("m_apply, negative off %d", off)); 1334123557Sbms KASSERT(len >= 0, ("m_apply, negative len %d", len)); 1335123557Sbms while (off > 0) { 1336123557Sbms KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1337123557Sbms if (off < m->m_len) 1338123557Sbms break; 1339123557Sbms off -= m->m_len; 1340123557Sbms m = m->m_next; 1341123557Sbms } 1342123557Sbms while (len > 0) { 1343123557Sbms KASSERT(m != NULL, ("m_apply, offset > size of mbuf chain")); 1344123557Sbms count = min(m->m_len - off, len); 1345123557Sbms rval = (*f)(arg, mtod(m, caddr_t) + off, count); 1346123557Sbms if (rval) 1347123557Sbms return (rval); 1348123557Sbms len -= count; 1349123557Sbms off = 0; 1350123557Sbms m = m->m_next; 1351123557Sbms } 1352123557Sbms return (0); 1353123557Sbms} 1354123557Sbms 1355123557Sbms/* 1356123557Sbms * Return a pointer to mbuf/offset of location in mbuf chain. 1357123557Sbms */ 1358123557Sbmsstruct mbuf * 1359123557Sbmsm_getptr(struct mbuf *m, int loc, int *off) 1360123557Sbms{ 1361123557Sbms 1362123557Sbms while (loc >= 0) { 1363123564Sbms /* Normal end of search. */ 1364123557Sbms if (m->m_len > loc) { 1365123557Sbms *off = loc; 1366123557Sbms return (m); 1367123557Sbms } else { 1368123557Sbms loc -= m->m_len; 1369123557Sbms if (m->m_next == NULL) { 1370123557Sbms if (loc == 0) { 1371123564Sbms /* Point at the end of valid data. */ 1372123557Sbms *off = m->m_len; 1373123557Sbms return (m); 1374123564Sbms } 1375123564Sbms return (NULL); 1376123564Sbms } 1377123564Sbms m = m->m_next; 1378123557Sbms } 1379123557Sbms } 1380123557Sbms return (NULL); 1381123557Sbms} 1382123557Sbms 138352756Sphkvoid 1384135904Sjmgm_print(const struct mbuf *m, int maxlen) 138552756Sphk{ 138652756Sphk int len; 1387135904Sjmg int pdata; 138854906Seivind const struct mbuf *m2; 138952756Sphk 1390135904Sjmg if (m->m_flags & M_PKTHDR) 1391135904Sjmg len = m->m_pkthdr.len; 1392135904Sjmg else 1393135904Sjmg len = -1; 139452756Sphk m2 = m; 1395135904Sjmg while (m2 != NULL && (len == -1 || len)) { 1396135904Sjmg pdata = m2->m_len; 1397135904Sjmg if (maxlen != -1 && pdata > maxlen) 1398135904Sjmg pdata = maxlen; 1399135904Sjmg printf("mbuf: %p len: %d, next: %p, %b%s", m2, m2->m_len, 1400135904Sjmg m2->m_next, m2->m_flags, "\20\20freelist\17skipfw" 1401135904Sjmg "\11proto5\10proto4\7proto3\6proto2\5proto1\4rdonly" 1402135904Sjmg "\3eor\2pkthdr\1ext", pdata ? "" : "\n"); 1403135904Sjmg if (pdata) 1404135904Sjmg printf(", %*D\n", m2->m_len, (u_char *)m2->m_data, "-"); 1405135904Sjmg if (len != -1) 1406135904Sjmg len -= m2->m_len; 140752756Sphk m2 = m2->m_next; 140852756Sphk } 1409135904Sjmg if (len > 0) 1410135904Sjmg printf("%d bytes unaccounted for.\n", len); 141152756Sphk return; 141252756Sphk} 1413103540Sphk 1414103569Sbmilekicu_int 1415103540Sphkm_fixhdr(struct mbuf *m0) 1416103540Sphk{ 1417103569Sbmilekic u_int len; 1418103540Sphk 1419103544Sphk len = m_length(m0, NULL); 1420103544Sphk m0->m_pkthdr.len = len; 1421103544Sphk return (len); 1422103544Sphk} 1423103544Sphk 1424103569Sbmilekicu_int 1425103544Sphkm_length(struct mbuf *m0, struct mbuf **last) 1426103544Sphk{ 1427103544Sphk struct mbuf *m; 1428103569Sbmilekic u_int len; 1429103544Sphk 1430103544Sphk len = 0; 1431103544Sphk for (m = m0; m != NULL; m = m->m_next) { 1432103540Sphk len += m->m_len; 1433103544Sphk if (m->m_next == NULL) 1434103544Sphk break; 1435103540Sphk } 1436103544Sphk if (last != NULL) 1437103544Sphk *last = m; 1438103544Sphk return (len); 1439103540Sphk} 1440112777Ssilby 1441112777Ssilby/* 1442112777Ssilby * Defragment a mbuf chain, returning the shortest possible 1443112777Ssilby * chain of mbufs and clusters. If allocation fails and 1444112777Ssilby * this cannot be completed, NULL will be returned, but 1445112777Ssilby * the passed in chain will be unchanged. Upon success, 1446112777Ssilby * the original chain will be freed, and the new chain 1447112777Ssilby * will be returned. 1448112777Ssilby * 1449112777Ssilby * If a non-packet header is passed in, the original 1450112777Ssilby * mbuf (chain?) will be returned unharmed. 1451112777Ssilby */ 1452112777Ssilbystruct mbuf * 1453112777Ssilbym_defrag(struct mbuf *m0, int how) 1454112777Ssilby{ 1455125472Ssilby struct mbuf *m_new = NULL, *m_final = NULL; 1456125472Ssilby int progress = 0, length; 1457112777Ssilby 1458132488Salfred MBUF_CHECKSLEEP(how); 1459112777Ssilby if (!(m0->m_flags & M_PKTHDR)) 1460112777Ssilby return (m0); 1461112777Ssilby 1462117770Ssilby m_fixhdr(m0); /* Needed sanity check */ 1463117770Ssilby 1464113490Ssilby#ifdef MBUF_STRESS_TEST 1465113490Ssilby if (m_defragrandomfailures) { 1466113490Ssilby int temp = arc4random() & 0xff; 1467113490Ssilby if (temp == 0xba) 1468113490Ssilby goto nospace; 1469113490Ssilby } 1470113490Ssilby#endif 1471112777Ssilby 1472112777Ssilby if (m0->m_pkthdr.len > MHLEN) 1473112777Ssilby m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1474112777Ssilby else 1475112777Ssilby m_final = m_gethdr(how, MT_DATA); 1476112777Ssilby 1477112777Ssilby if (m_final == NULL) 1478112777Ssilby goto nospace; 1479112777Ssilby 1480123740Speter if (m_dup_pkthdr(m_final, m0, how) == 0) 1481112777Ssilby goto nospace; 1482112777Ssilby 1483112777Ssilby m_new = m_final; 1484112777Ssilby 1485112777Ssilby while (progress < m0->m_pkthdr.len) { 1486112777Ssilby length = m0->m_pkthdr.len - progress; 1487112777Ssilby if (length > MCLBYTES) 1488112777Ssilby length = MCLBYTES; 1489112777Ssilby 1490112777Ssilby if (m_new == NULL) { 1491112777Ssilby if (length > MLEN) 1492112777Ssilby m_new = m_getcl(how, MT_DATA, 0); 1493112777Ssilby else 1494112777Ssilby m_new = m_get(how, MT_DATA); 1495112777Ssilby if (m_new == NULL) 1496112777Ssilby goto nospace; 1497112777Ssilby } 1498112777Ssilby 1499112777Ssilby m_copydata(m0, progress, length, mtod(m_new, caddr_t)); 1500112777Ssilby progress += length; 1501112777Ssilby m_new->m_len = length; 1502112777Ssilby if (m_new != m_final) 1503112777Ssilby m_cat(m_final, m_new); 1504112777Ssilby m_new = NULL; 1505112777Ssilby } 1506116455Ssilby#ifdef MBUF_STRESS_TEST 1507112777Ssilby if (m0->m_next == NULL) 1508112777Ssilby m_defraguseless++; 1509116455Ssilby#endif 1510112777Ssilby m_freem(m0); 1511112777Ssilby m0 = m_final; 1512116455Ssilby#ifdef MBUF_STRESS_TEST 1513112777Ssilby m_defragpackets++; 1514112777Ssilby m_defragbytes += m0->m_pkthdr.len; 1515116455Ssilby#endif 1516112777Ssilby return (m0); 1517112777Ssilbynospace: 1518116455Ssilby#ifdef MBUF_STRESS_TEST 1519112777Ssilby m_defragfailure++; 1520116455Ssilby#endif 1521112777Ssilby if (m_final) 1522112777Ssilby m_freem(m_final); 1523112777Ssilby return (NULL); 1524112777Ssilby} 1525119644Ssilby 1526119644Ssilby#ifdef MBUF_STRESS_TEST 1527119644Ssilby 1528119644Ssilby/* 1529119644Ssilby * Fragment an mbuf chain. There's no reason you'd ever want to do 1530119644Ssilby * this in normal usage, but it's great for stress testing various 1531119644Ssilby * mbuf consumers. 1532119644Ssilby * 1533119644Ssilby * If fragmentation is not possible, the original chain will be 1534119644Ssilby * returned. 1535119644Ssilby * 1536119644Ssilby * Possible length values: 1537119644Ssilby * 0 no fragmentation will occur 1538119644Ssilby * > 0 each fragment will be of the specified length 1539119644Ssilby * -1 each fragment will be the same random value in length 1540119644Ssilby * -2 each fragment's length will be entirely random 1541119644Ssilby * (Random values range from 1 to 256) 1542119644Ssilby */ 1543119644Ssilbystruct mbuf * 1544119644Ssilbym_fragment(struct mbuf *m0, int how, int length) 1545119644Ssilby{ 1546125472Ssilby struct mbuf *m_new = NULL, *m_final = NULL; 1547125472Ssilby int progress = 0; 1548119644Ssilby 1549119644Ssilby if (!(m0->m_flags & M_PKTHDR)) 1550119644Ssilby return (m0); 1551119644Ssilby 1552119644Ssilby if ((length == 0) || (length < -2)) 1553119644Ssilby return (m0); 1554119644Ssilby 1555119644Ssilby m_fixhdr(m0); /* Needed sanity check */ 1556119644Ssilby 1557119644Ssilby m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1558119644Ssilby 1559119644Ssilby if (m_final == NULL) 1560119644Ssilby goto nospace; 1561119644Ssilby 1562123823Ssilby if (m_dup_pkthdr(m_final, m0, how) == 0) 1563119644Ssilby goto nospace; 1564119644Ssilby 1565119644Ssilby m_new = m_final; 1566119644Ssilby 1567119644Ssilby if (length == -1) 1568119644Ssilby length = 1 + (arc4random() & 255); 1569119644Ssilby 1570119644Ssilby while (progress < m0->m_pkthdr.len) { 1571119644Ssilby int fraglen; 1572119644Ssilby 1573119644Ssilby if (length > 0) 1574119644Ssilby fraglen = length; 1575119644Ssilby else 1576119644Ssilby fraglen = 1 + (arc4random() & 255); 1577119644Ssilby if (fraglen > m0->m_pkthdr.len - progress) 1578119644Ssilby fraglen = m0->m_pkthdr.len - progress; 1579119644Ssilby 1580119644Ssilby if (fraglen > MCLBYTES) 1581119644Ssilby fraglen = MCLBYTES; 1582119644Ssilby 1583119644Ssilby if (m_new == NULL) { 1584119644Ssilby m_new = m_getcl(how, MT_DATA, 0); 1585119644Ssilby if (m_new == NULL) 1586119644Ssilby goto nospace; 1587119644Ssilby } 1588119644Ssilby 1589119644Ssilby m_copydata(m0, progress, fraglen, mtod(m_new, caddr_t)); 1590119644Ssilby progress += fraglen; 1591119644Ssilby m_new->m_len = fraglen; 1592119644Ssilby if (m_new != m_final) 1593119644Ssilby m_cat(m_final, m_new); 1594119644Ssilby m_new = NULL; 1595119644Ssilby } 1596119644Ssilby m_freem(m0); 1597119644Ssilby m0 = m_final; 1598119644Ssilby return (m0); 1599119644Ssilbynospace: 1600119644Ssilby if (m_final) 1601119644Ssilby m_freem(m_final); 1602119644Ssilby /* Return the original chain on failure */ 1603119644Ssilby return (m0); 1604119644Ssilby} 1605119644Ssilby 1606119644Ssilby#endif 1607125296Ssilby 1608125296Ssilbystruct mbuf * 1609145883Semaxm_uiotombuf(struct uio *uio, int how, int len, int align) 1610125296Ssilby{ 1611125472Ssilby struct mbuf *m_new = NULL, *m_final = NULL; 1612125472Ssilby int progress = 0, error = 0, length, total; 1613125296Ssilby 1614125296Ssilby if (len > 0) 1615125296Ssilby total = min(uio->uio_resid, len); 1616125296Ssilby else 1617125296Ssilby total = uio->uio_resid; 1618145883Semax if (align >= MHLEN) 1619145883Semax goto nospace; 1620145883Semax if (total + align > MHLEN) 1621125296Ssilby m_final = m_getcl(how, MT_DATA, M_PKTHDR); 1622125296Ssilby else 1623125296Ssilby m_final = m_gethdr(how, MT_DATA); 1624125296Ssilby if (m_final == NULL) 1625125296Ssilby goto nospace; 1626145883Semax m_final->m_data += align; 1627125296Ssilby m_new = m_final; 1628125296Ssilby while (progress < total) { 1629125296Ssilby length = total - progress; 1630125296Ssilby if (length > MCLBYTES) 1631125296Ssilby length = MCLBYTES; 1632125296Ssilby if (m_new == NULL) { 1633125296Ssilby if (length > MLEN) 1634125296Ssilby m_new = m_getcl(how, MT_DATA, 0); 1635125296Ssilby else 1636125296Ssilby m_new = m_get(how, MT_DATA); 1637125296Ssilby if (m_new == NULL) 1638125296Ssilby goto nospace; 1639125296Ssilby } 1640125296Ssilby error = uiomove(mtod(m_new, void *), length, uio); 1641125296Ssilby if (error) 1642125296Ssilby goto nospace; 1643125296Ssilby progress += length; 1644125296Ssilby m_new->m_len = length; 1645125296Ssilby if (m_new != m_final) 1646125296Ssilby m_cat(m_final, m_new); 1647125296Ssilby m_new = NULL; 1648125296Ssilby } 1649125296Ssilby m_fixhdr(m_final); 1650125296Ssilby return (m_final); 1651125296Ssilbynospace: 1652125296Ssilby if (m_new) 1653125296Ssilby m_free(m_new); 1654125296Ssilby if (m_final) 1655125296Ssilby m_freem(m_final); 1656125296Ssilby return (NULL); 1657125296Ssilby} 1658148552Ssam 1659148552Ssam/* 1660148552Ssam * Set the m_data pointer of a newly-allocated mbuf 1661148552Ssam * to place an object of the specified size at the 1662148552Ssam * end of the mbuf, longword aligned. 1663148552Ssam */ 1664148552Ssamvoid 1665148552Ssamm_align(struct mbuf *m, int len) 1666148552Ssam{ 1667148552Ssam int adjust; 1668148552Ssam 1669148552Ssam if (m->m_flags & M_EXT) 1670148552Ssam adjust = m->m_ext.ext_size - len; 1671148552Ssam else if (m->m_flags & M_PKTHDR) 1672148552Ssam adjust = MHLEN - len; 1673148552Ssam else 1674148552Ssam adjust = MLEN - len; 1675148552Ssam m->m_data += adjust &~ (sizeof(long)-1); 1676148552Ssam} 1677