195023Ssuz/* $KAME: uipc_mbuf2.c,v 1.31 2001/11/28 11:08:53 itojun Exp $ */ 262587Sitojun/* $NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $ */ 362587Sitojun 4139804Simp/*- 562587Sitojun * Copyright (C) 1999 WIDE Project. 662587Sitojun * All rights reserved. 762587Sitojun * 862587Sitojun * Redistribution and use in source and binary forms, with or without 962587Sitojun * modification, are permitted provided that the following conditions 1062587Sitojun * are met: 1162587Sitojun * 1. Redistributions of source code must retain the above copyright 1262587Sitojun * notice, this list of conditions and the following disclaimer. 1362587Sitojun * 2. Redistributions in binary form must reproduce the above copyright 1462587Sitojun * notice, this list of conditions and the following disclaimer in the 1562587Sitojun * documentation and/or other materials provided with the distribution. 1662587Sitojun * 3. Neither the name of the project nor the names of its contributors 1762587Sitojun * may be used to endorse or promote products derived from this software 1862587Sitojun * without specific prior written permission. 1962587Sitojun * 2062587Sitojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2162587Sitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2262587Sitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2362587Sitojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2462587Sitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2562587Sitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2662587Sitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2762587Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2862587Sitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2962587Sitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3062587Sitojun * SUCH DAMAGE. 3162587Sitojun */ 32139804Simp/*- 3362587Sitojun * Copyright (c) 1982, 1986, 1988, 1991, 1993 3462587Sitojun * The Regents of the University of California. All rights reserved. 3562587Sitojun * 3662587Sitojun * Redistribution and use in source and binary forms, with or without 3762587Sitojun * modification, are permitted provided that the following conditions 3862587Sitojun * are met: 3962587Sitojun * 1. Redistributions of source code must retain the above copyright 4062587Sitojun * notice, this list of conditions and the following disclaimer. 4162587Sitojun * 2. Redistributions in binary form must reproduce the above copyright 4262587Sitojun * notice, this list of conditions and the following disclaimer in the 4362587Sitojun * documentation and/or other materials provided with the distribution. 4462587Sitojun * 4. Neither the name of the University nor the names of its contributors 4562587Sitojun * may be used to endorse or promote products derived from this software 4662587Sitojun * without specific prior written permission. 4762587Sitojun * 4862587Sitojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 4962587Sitojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5062587Sitojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5162587Sitojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5262587Sitojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5362587Sitojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5462587Sitojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5562587Sitojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5662587Sitojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5762587Sitojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5862587Sitojun * SUCH DAMAGE. 5962587Sitojun * 6062587Sitojun * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95 6162587Sitojun */ 6262587Sitojun 63116182Sobrien#include <sys/cdefs.h> 64116182Sobrien__FBSDID("$FreeBSD: stable/11/sys/kern/uipc_mbuf2.c 356449 2020-01-07 16:49:56Z bz $"); 65116182Sobrien 6662587Sitojun/*#define PULLDOWN_DEBUG*/ 6762587Sitojun 6862587Sitojun#include <sys/param.h> 6962587Sitojun#include <sys/systm.h> 70105194Ssam#include <sys/kernel.h> 7176166Smarkm#include <sys/lock.h> 7262587Sitojun#include <sys/malloc.h> 7362587Sitojun#include <sys/mbuf.h> 7476166Smarkm#include <sys/mutex.h> 7562587Sitojun 76163606Srwatson#include <security/mac/mac_framework.h> 77163606Srwatson 78148095Srwatsonstatic MALLOC_DEFINE(M_PACKET_TAGS, MBUF_TAG_MEM_NAME, 79148095Srwatson "packet-attached information"); 80105194Ssam 8178064Sume/* can't call it m_dup(), as freebsd[34] uses m_dup() with different arg */ 8292723Salfredstatic struct mbuf *m_dup1(struct mbuf *, int, int, int); 8378064Sume 8462587Sitojun/* 8562587Sitojun * ensure that [off, off + len) is contiguous on the mbuf chain "m". 8662587Sitojun * packet chain before "off" is kept untouched. 8762587Sitojun * if offp == NULL, the target will start at <retval, 0> on resulting chain. 8862587Sitojun * if offp != NULL, the target will start at <retval, *offp> on resulting chain. 8962587Sitojun * 9062587Sitojun * on error return (NULL return value), original "m" will be freed. 9162587Sitojun * 9268618Sbmilekic * XXX: M_TRAILINGSPACE/M_LEADINGSPACE only permitted on writable ext_buf. 9362587Sitojun */ 9462587Sitojunstruct mbuf * 9572356Sbmilekicm_pulldown(struct mbuf *m, int off, int len, int *offp) 9662587Sitojun{ 9762587Sitojun struct mbuf *n, *o; 9862587Sitojun int hlen, tlen, olen; 9968618Sbmilekic int writable; 10062587Sitojun 10162587Sitojun /* check invalid arguments. */ 102356449Sbz KASSERT(m != NULL, ("%s: fix caller: m is NULL off %d len %d offp %p\n", 103356449Sbz __func__, off, len, offp)); 10462587Sitojun if (len > MCLBYTES) { 10562587Sitojun m_freem(m); 10662587Sitojun return NULL; /* impossible */ 10762587Sitojun } 10862587Sitojun 10962587Sitojun#ifdef PULLDOWN_DEBUG 11062587Sitojun { 11162587Sitojun struct mbuf *t; 11262587Sitojun printf("before:"); 11362587Sitojun for (t = m; t; t = t->m_next) 11462587Sitojun printf(" %d", t->m_len); 11562587Sitojun printf("\n"); 11662587Sitojun } 11762587Sitojun#endif 11862587Sitojun n = m; 11962587Sitojun while (n != NULL && off > 0) { 12062587Sitojun if (n->m_len > off) 12162587Sitojun break; 12262587Sitojun off -= n->m_len; 12362587Sitojun n = n->m_next; 12462587Sitojun } 12562587Sitojun /* be sure to point non-empty mbuf */ 12662587Sitojun while (n != NULL && n->m_len == 0) 12762587Sitojun n = n->m_next; 12862587Sitojun if (!n) { 12962587Sitojun m_freem(m); 13062587Sitojun return NULL; /* mbuf chain too short */ 13162587Sitojun } 13262587Sitojun 13362587Sitojun /* 134276884Srwatson * The following comment is dated but still partially applies: 135276884Srwatson * 13678064Sume * XXX: This code is flawed because it considers a "writable" mbuf 13778064Sume * data region to require all of the following: 13878064Sume * (i) mbuf _has_ to have M_EXT set; if it is just a regular 13978064Sume * mbuf, it is still not considered "writable." 14078064Sume * (ii) since mbuf has M_EXT, the ext_type _has_ to be 14178064Sume * EXT_CLUSTER. Anything else makes it non-writable. 14278064Sume * (iii) M_WRITABLE() must evaluate true. 14378064Sume * Ideally, the requirement should only be (iii). 14478064Sume * 14578064Sume * If we're writable, we're sure we're writable, because the ref. count 146298819Spfg * cannot increase from 1, as that would require possession of mbuf 14778064Sume * n by someone else (which is impossible). However, if we're _not_ 14878064Sume * writable, we may eventually become writable )if the ref. count drops 14978064Sume * to 1), but we'll fail to notice it unless we re-evaluate 15078064Sume * M_WRITABLE(). For now, we only evaluate once at the beginning and 15178064Sume * live with this. 15278064Sume */ 15378064Sume writable = 0; 15478064Sume if ((n->m_flags & M_EXT) == 0 || 15578064Sume (n->m_ext.ext_type == EXT_CLUSTER && M_WRITABLE(n))) 15678064Sume writable = 1; 15778064Sume 15878064Sume /* 15962587Sitojun * the target data is on <n, off>. 16062587Sitojun * if we got enough data on the mbuf "n", we're done. 16162587Sitojun */ 162312441Srpokala if ((off == 0 || offp) && len <= n->m_len - off) 16362587Sitojun goto ok; 16462587Sitojun 16562587Sitojun /* 16678064Sume * when len <= n->m_len - off and off != 0, it is a special case. 16762587Sitojun * len bytes from <n, off> sits in single mbuf, but the caller does 16862587Sitojun * not like the starting position (off). 16962587Sitojun * chop the current mbuf into two pieces, set off to 0. 17062587Sitojun */ 17178064Sume if (len <= n->m_len - off) { 172243882Sglebius o = m_dup1(n, off, n->m_len - off, M_NOWAIT); 17362587Sitojun if (o == NULL) { 17462587Sitojun m_freem(m); 17562587Sitojun return NULL; /* ENOBUFS */ 17662587Sitojun } 17762587Sitojun n->m_len = off; 17862587Sitojun o->m_next = n->m_next; 17962587Sitojun n->m_next = o; 18062587Sitojun n = n->m_next; 18162587Sitojun off = 0; 18262587Sitojun goto ok; 18362587Sitojun } 18462587Sitojun 18562587Sitojun /* 18662587Sitojun * we need to take hlen from <n, off> and tlen from <n->m_next, 0>, 18762587Sitojun * and construct contiguous mbuf with m_len == len. 18862587Sitojun * note that hlen + tlen == len, and tlen > 0. 18962587Sitojun */ 19062587Sitojun hlen = n->m_len - off; 19162587Sitojun tlen = len - hlen; 19262587Sitojun 19362587Sitojun /* 19462587Sitojun * ensure that we have enough trailing data on mbuf chain. 19562587Sitojun * if not, we can do nothing about the chain. 19662587Sitojun */ 19762587Sitojun olen = 0; 19862587Sitojun for (o = n->m_next; o != NULL; o = o->m_next) 19962587Sitojun olen += o->m_len; 20062587Sitojun if (hlen + olen < len) { 20162587Sitojun m_freem(m); 20262587Sitojun return NULL; /* mbuf chain too short */ 20362587Sitojun } 20462587Sitojun 20562587Sitojun /* 20662587Sitojun * easy cases first. 20762587Sitojun * we need to use m_copydata() to get data from <n->m_next, 0>. 20862587Sitojun */ 20962587Sitojun if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen 21068618Sbmilekic && writable) { 21162587Sitojun m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len); 21262587Sitojun n->m_len += tlen; 21362587Sitojun m_adj(n->m_next, tlen); 21462587Sitojun goto ok; 21562587Sitojun } 21662587Sitojun if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen 217350829Sjtl && writable && n->m_next->m_len >= tlen) { 21862587Sitojun n->m_next->m_data -= hlen; 21962587Sitojun n->m_next->m_len += hlen; 22062587Sitojun bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen); 22162587Sitojun n->m_len -= hlen; 22262587Sitojun n = n->m_next; 22362587Sitojun off = 0; 22462587Sitojun goto ok; 22562587Sitojun } 22662587Sitojun 22762587Sitojun /* 22862587Sitojun * now, we need to do the hard way. don't m_copy as there's no room 22962587Sitojun * on both end. 23062587Sitojun */ 231129906Sbmilekic if (len > MLEN) 232243882Sglebius o = m_getcl(M_NOWAIT, m->m_type, 0); 233129906Sbmilekic else 234243882Sglebius o = m_get(M_NOWAIT, m->m_type); 23578064Sume if (!o) { 23678064Sume m_freem(m); 23778064Sume return NULL; /* ENOBUFS */ 23878064Sume } 23962587Sitojun /* get hlen from <n, off> into <o, 0> */ 24062587Sitojun o->m_len = hlen; 24162587Sitojun bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen); 24262587Sitojun n->m_len -= hlen; 24362587Sitojun /* get tlen from <n->m_next, 0> into <o, hlen> */ 24462587Sitojun m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len); 24562587Sitojun o->m_len += tlen; 24662587Sitojun m_adj(n->m_next, tlen); 24762587Sitojun o->m_next = n->m_next; 24862587Sitojun n->m_next = o; 24962587Sitojun n = o; 25062587Sitojun off = 0; 25162587Sitojun 25262587Sitojunok: 25362587Sitojun#ifdef PULLDOWN_DEBUG 25462587Sitojun { 25562587Sitojun struct mbuf *t; 25662587Sitojun printf("after:"); 25762587Sitojun for (t = m; t; t = t->m_next) 25862587Sitojun printf("%c%d", t == n ? '*' : ' ', t->m_len); 25962587Sitojun printf(" (off=%d)\n", off); 26062587Sitojun } 26162587Sitojun#endif 26262587Sitojun if (offp) 26362587Sitojun *offp = off; 26462587Sitojun return n; 26562587Sitojun} 26662587Sitojun 26778064Sumestatic struct mbuf * 26878064Sumem_dup1(struct mbuf *m, int off, int len, int wait) 26978064Sume{ 27078064Sume struct mbuf *n; 27178064Sume int copyhdr; 27278064Sume 27378064Sume if (len > MCLBYTES) 27478064Sume return NULL; 275129906Sbmilekic if (off == 0 && (m->m_flags & M_PKTHDR) != 0) 27678064Sume copyhdr = 1; 277129906Sbmilekic else 278129906Sbmilekic copyhdr = 0; 279129906Sbmilekic if (len >= MINCLSIZE) { 280129906Sbmilekic if (copyhdr == 1) 281129906Sbmilekic n = m_getcl(wait, m->m_type, M_PKTHDR); 282129906Sbmilekic else 283129906Sbmilekic n = m_getcl(wait, m->m_type, 0); 28478064Sume } else { 285129906Sbmilekic if (copyhdr == 1) 286129906Sbmilekic n = m_gethdr(wait, m->m_type); 287129906Sbmilekic else 288129906Sbmilekic n = m_get(wait, m->m_type); 28978064Sume } 29078064Sume if (!n) 291129906Sbmilekic return NULL; /* ENOBUFS */ 29278064Sume 293108466Ssam if (copyhdr && !m_dup_pkthdr(n, m, wait)) { 294108466Ssam m_free(n); 295108466Ssam return NULL; 296108466Ssam } 29778064Sume m_copydata(m, off, len, mtod(n, caddr_t)); 298129062Ssam n->m_len = len; 29978064Sume return n; 30078064Sume} 30178064Sume 302136347Sglebius/* Free a packet tag. */ 303136386Sglebiusvoid 304136386Sglebiusm_tag_free_default(struct m_tag *t) 305124077Ssam{ 306124077Ssam#ifdef MAC 307124077Ssam if (t->m_tag_id == PACKET_TAG_MACLABEL) 308172930Srwatson mac_mbuf_tag_destroy(t); 309124077Ssam#endif 310124077Ssam free(t, M_PACKET_TAGS); 311124077Ssam} 312124077Ssam 313105194Ssam/* Get a packet tag structure along with specified data following. */ 314105194Ssamstruct m_tag * 315209390Sedm_tag_alloc(uint32_t cookie, int type, int len, int wait) 31662587Sitojun{ 317105194Ssam struct m_tag *t; 31862587Sitojun 319132488Salfred MBUF_CHECKSLEEP(wait); 320105194Ssam if (len < 0) 32162587Sitojun return NULL; 322105194Ssam t = malloc(len + sizeof(struct m_tag), M_PACKET_TAGS, wait); 323105194Ssam if (t == NULL) 324105194Ssam return NULL; 325124077Ssam m_tag_setup(t, cookie, type, len); 326136386Sglebius t->m_tag_free = m_tag_free_default; 327105194Ssam return t; 328105194Ssam} 32962587Sitojun 330105194Ssam/* Unlink and free a packet tag. */ 331105194Ssamvoid 332105194Ssamm_tag_delete(struct mbuf *m, struct m_tag *t) 33362587Sitojun{ 334132488Salfred 335105194Ssam KASSERT(m && t, ("m_tag_delete: null argument, m %p t %p", m, t)); 336105194Ssam m_tag_unlink(m, t); 337136347Sglebius m_tag_free(t); 338105194Ssam} 33962587Sitojun 340105194Ssam/* Unlink and free a packet tag chain, starting from given tag. */ 341105194Ssamvoid 342105194Ssamm_tag_delete_chain(struct mbuf *m, struct m_tag *t) 343105194Ssam{ 344105194Ssam struct m_tag *p, *q; 34562587Sitojun 346105194Ssam KASSERT(m, ("m_tag_delete_chain: null mbuf")); 347105194Ssam if (t != NULL) 348105194Ssam p = t; 349105194Ssam else 350105194Ssam p = SLIST_FIRST(&m->m_pkthdr.tags); 351105194Ssam if (p == NULL) 352105194Ssam return; 353105194Ssam while ((q = SLIST_NEXT(p, m_tag_link)) != NULL) 354105194Ssam m_tag_delete(m, q); 355105194Ssam m_tag_delete(m, p); 356105194Ssam} 357105194Ssam 358121645Ssam/* 359121645Ssam * Strip off all tags that would normally vanish when 360121645Ssam * passing through a network interface. Only persistent 361121645Ssam * tags will exist after this; these are expected to remain 362121645Ssam * so long as the mbuf chain exists, regardless of the 363121645Ssam * path the mbufs take. 364121645Ssam */ 365121645Ssamvoid 366121645Ssamm_tag_delete_nonpersistent(struct mbuf *m) 367121645Ssam{ 368121645Ssam struct m_tag *p, *q; 369121645Ssam 370121645Ssam SLIST_FOREACH_SAFE(p, &m->m_pkthdr.tags, m_tag_link, q) 371121645Ssam if ((p->m_tag_id & MTAG_PERSISTENT) == 0) 372121645Ssam m_tag_delete(m, p); 373121645Ssam} 374121645Ssam 375105194Ssam/* Find a tag, starting from a given position. */ 376105194Ssamstruct m_tag * 377209390Sedm_tag_locate(struct mbuf *m, uint32_t cookie, int type, struct m_tag *t) 378105194Ssam{ 379105194Ssam struct m_tag *p; 380105194Ssam 381107283Ssam KASSERT(m, ("m_tag_locate: null mbuf")); 382105194Ssam if (t == NULL) 383105194Ssam p = SLIST_FIRST(&m->m_pkthdr.tags); 384105194Ssam else 385105194Ssam p = SLIST_NEXT(t, m_tag_link); 386105194Ssam while (p != NULL) { 387105194Ssam if (p->m_tag_cookie == cookie && p->m_tag_id == type) 388105194Ssam return p; 389105194Ssam p = SLIST_NEXT(p, m_tag_link); 39062587Sitojun } 39162587Sitojun return NULL; 39262587Sitojun} 39362587Sitojun 394105194Ssam/* Copy a single tag. */ 395105194Ssamstruct m_tag * 396108466Ssamm_tag_copy(struct m_tag *t, int how) 39778064Sume{ 398105194Ssam struct m_tag *p; 39978064Sume 400132488Salfred MBUF_CHECKSLEEP(how); 401105194Ssam KASSERT(t, ("m_tag_copy: null tag")); 402108466Ssam p = m_tag_alloc(t->m_tag_cookie, t->m_tag_id, t->m_tag_len, how); 403105194Ssam if (p == NULL) 404105194Ssam return (NULL); 405113487Srwatson#ifdef MAC 406113487Srwatson /* 407113487Srwatson * XXXMAC: we should probably pass off the initialization, and 408113487Srwatson * copying here? can we hide that PACKET_TAG_MACLABEL is 409113487Srwatson * special from the mbuf code? 410113487Srwatson */ 411113487Srwatson if (t->m_tag_id == PACKET_TAG_MACLABEL) { 412172930Srwatson if (mac_mbuf_tag_init(p, how) != 0) { 413113487Srwatson m_tag_free(p); 414113487Srwatson return (NULL); 415113487Srwatson } 416172930Srwatson mac_mbuf_tag_copy(t, p); 417113487Srwatson } else 418113487Srwatson#endif 419113487Srwatson bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */ 420105194Ssam return p; 42178064Sume} 42278064Sume 423105194Ssam/* 424105194Ssam * Copy two tag chains. The destination mbuf (to) loses any attached 425105194Ssam * tags even if the operation fails. This should not be a problem, as 426105194Ssam * m_tag_copy_chain() is typically called with a newly-allocated 427105194Ssam * destination mbuf. 428105194Ssam */ 429105194Ssamint 430286450Smelifarom_tag_copy_chain(struct mbuf *to, const struct mbuf *from, int how) 43178064Sume{ 432105194Ssam struct m_tag *p, *t, *tprev = NULL; 43378064Sume 434132488Salfred MBUF_CHECKSLEEP(how); 435105194Ssam KASSERT(to && from, 436107283Ssam ("m_tag_copy_chain: null argument, to %p from %p", to, from)); 437105194Ssam m_tag_delete_chain(to, NULL); 438105194Ssam SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) { 439108466Ssam t = m_tag_copy(p, how); 440105194Ssam if (t == NULL) { 441105194Ssam m_tag_delete_chain(to, NULL); 442105194Ssam return 0; 443105194Ssam } 444105194Ssam if (tprev == NULL) 445105194Ssam SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link); 446109619Ssam else 447105194Ssam SLIST_INSERT_AFTER(tprev, t, m_tag_link); 448109619Ssam tprev = t; 449105194Ssam } 450105194Ssam return 1; 45178064Sume} 452