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$"); 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. */ 10262587Sitojun if (m == NULL) 10362587Sitojun panic("m == NULL in m_pulldown()"); 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 /* 13478064Sume * XXX: This code is flawed because it considers a "writable" mbuf 13578064Sume * data region to require all of the following: 13678064Sume * (i) mbuf _has_ to have M_EXT set; if it is just a regular 13778064Sume * mbuf, it is still not considered "writable." 13878064Sume * (ii) since mbuf has M_EXT, the ext_type _has_ to be 13978064Sume * EXT_CLUSTER. Anything else makes it non-writable. 14078064Sume * (iii) M_WRITABLE() must evaluate true. 14178064Sume * Ideally, the requirement should only be (iii). 14278064Sume * 14378064Sume * If we're writable, we're sure we're writable, because the ref. count 14478064Sume * cannot increase from 1, as that would require posession of mbuf 14578064Sume * n by someone else (which is impossible). However, if we're _not_ 14678064Sume * writable, we may eventually become writable )if the ref. count drops 14778064Sume * to 1), but we'll fail to notice it unless we re-evaluate 14878064Sume * M_WRITABLE(). For now, we only evaluate once at the beginning and 14978064Sume * live with this. 15078064Sume */ 15178064Sume /* 15278064Sume * XXX: This is dumb. If we're just a regular mbuf with no M_EXT, 15378064Sume * then we're not "writable," according to this code. 15478064Sume */ 15578064Sume writable = 0; 15678064Sume if ((n->m_flags & M_EXT) == 0 || 15778064Sume (n->m_ext.ext_type == EXT_CLUSTER && M_WRITABLE(n))) 15878064Sume writable = 1; 15978064Sume 16078064Sume /* 16162587Sitojun * the target data is on <n, off>. 16262587Sitojun * if we got enough data on the mbuf "n", we're done. 16362587Sitojun */ 16478064Sume if ((off == 0 || offp) && len <= n->m_len - off && writable) 16562587Sitojun goto ok; 16662587Sitojun 16762587Sitojun /* 16878064Sume * when len <= n->m_len - off and off != 0, it is a special case. 16962587Sitojun * len bytes from <n, off> sits in single mbuf, but the caller does 17062587Sitojun * not like the starting position (off). 17162587Sitojun * chop the current mbuf into two pieces, set off to 0. 17262587Sitojun */ 17378064Sume if (len <= n->m_len - off) { 174111119Simp o = m_dup1(n, off, n->m_len - off, M_DONTWAIT); 17562587Sitojun if (o == NULL) { 17662587Sitojun m_freem(m); 17762587Sitojun return NULL; /* ENOBUFS */ 17862587Sitojun } 17962587Sitojun n->m_len = off; 18062587Sitojun o->m_next = n->m_next; 18162587Sitojun n->m_next = o; 18262587Sitojun n = n->m_next; 18362587Sitojun off = 0; 18462587Sitojun goto ok; 18562587Sitojun } 18662587Sitojun 18762587Sitojun /* 18862587Sitojun * we need to take hlen from <n, off> and tlen from <n->m_next, 0>, 18962587Sitojun * and construct contiguous mbuf with m_len == len. 19062587Sitojun * note that hlen + tlen == len, and tlen > 0. 19162587Sitojun */ 19262587Sitojun hlen = n->m_len - off; 19362587Sitojun tlen = len - hlen; 19462587Sitojun 19562587Sitojun /* 19662587Sitojun * ensure that we have enough trailing data on mbuf chain. 19762587Sitojun * if not, we can do nothing about the chain. 19862587Sitojun */ 19962587Sitojun olen = 0; 20062587Sitojun for (o = n->m_next; o != NULL; o = o->m_next) 20162587Sitojun olen += o->m_len; 20262587Sitojun if (hlen + olen < len) { 20362587Sitojun m_freem(m); 20462587Sitojun return NULL; /* mbuf chain too short */ 20562587Sitojun } 20662587Sitojun 20762587Sitojun /* 20862587Sitojun * easy cases first. 20962587Sitojun * we need to use m_copydata() to get data from <n->m_next, 0>. 21062587Sitojun */ 21162587Sitojun if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen 21268618Sbmilekic && writable) { 21362587Sitojun m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len); 21462587Sitojun n->m_len += tlen; 21562587Sitojun m_adj(n->m_next, tlen); 21662587Sitojun goto ok; 21762587Sitojun } 21862587Sitojun if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen 21968618Sbmilekic && writable) { 22062587Sitojun n->m_next->m_data -= hlen; 22162587Sitojun n->m_next->m_len += hlen; 22262587Sitojun bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen); 22362587Sitojun n->m_len -= hlen; 22462587Sitojun n = n->m_next; 22562587Sitojun off = 0; 22662587Sitojun goto ok; 22762587Sitojun } 22862587Sitojun 22962587Sitojun /* 23062587Sitojun * now, we need to do the hard way. don't m_copy as there's no room 23162587Sitojun * on both end. 23262587Sitojun */ 233129906Sbmilekic if (len > MLEN) 234129906Sbmilekic o = m_getcl(M_DONTWAIT, m->m_type, 0); 235129906Sbmilekic else 236129906Sbmilekic o = m_get(M_DONTWAIT, m->m_type); 23778064Sume if (!o) { 23878064Sume m_freem(m); 23978064Sume return NULL; /* ENOBUFS */ 24078064Sume } 24162587Sitojun /* get hlen from <n, off> into <o, 0> */ 24262587Sitojun o->m_len = hlen; 24362587Sitojun bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen); 24462587Sitojun n->m_len -= hlen; 24562587Sitojun /* get tlen from <n->m_next, 0> into <o, hlen> */ 24662587Sitojun m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len); 24762587Sitojun o->m_len += tlen; 24862587Sitojun m_adj(n->m_next, tlen); 24962587Sitojun o->m_next = n->m_next; 25062587Sitojun n->m_next = o; 25162587Sitojun n = o; 25262587Sitojun off = 0; 25362587Sitojun 25462587Sitojunok: 25562587Sitojun#ifdef PULLDOWN_DEBUG 25662587Sitojun { 25762587Sitojun struct mbuf *t; 25862587Sitojun printf("after:"); 25962587Sitojun for (t = m; t; t = t->m_next) 26062587Sitojun printf("%c%d", t == n ? '*' : ' ', t->m_len); 26162587Sitojun printf(" (off=%d)\n", off); 26262587Sitojun } 26362587Sitojun#endif 26462587Sitojun if (offp) 26562587Sitojun *offp = off; 26662587Sitojun return n; 26762587Sitojun} 26862587Sitojun 26978064Sumestatic struct mbuf * 27078064Sumem_dup1(struct mbuf *m, int off, int len, int wait) 27178064Sume{ 27278064Sume struct mbuf *n; 27378064Sume int copyhdr; 27478064Sume 27578064Sume if (len > MCLBYTES) 27678064Sume return NULL; 277129906Sbmilekic if (off == 0 && (m->m_flags & M_PKTHDR) != 0) 27878064Sume copyhdr = 1; 279129906Sbmilekic else 280129906Sbmilekic copyhdr = 0; 281129906Sbmilekic if (len >= MINCLSIZE) { 282129906Sbmilekic if (copyhdr == 1) 283129906Sbmilekic n = m_getcl(wait, m->m_type, M_PKTHDR); 284129906Sbmilekic else 285129906Sbmilekic n = m_getcl(wait, m->m_type, 0); 28678064Sume } else { 287129906Sbmilekic if (copyhdr == 1) 288129906Sbmilekic n = m_gethdr(wait, m->m_type); 289129906Sbmilekic else 290129906Sbmilekic n = m_get(wait, m->m_type); 29178064Sume } 29278064Sume if (!n) 293129906Sbmilekic return NULL; /* ENOBUFS */ 29478064Sume 295108466Ssam if (copyhdr && !m_dup_pkthdr(n, m, wait)) { 296108466Ssam m_free(n); 297108466Ssam return NULL; 298108466Ssam } 29978064Sume m_copydata(m, off, len, mtod(n, caddr_t)); 300129062Ssam n->m_len = len; 30178064Sume return n; 30278064Sume} 30378064Sume 304136347Sglebius/* Free a packet tag. */ 305136386Sglebiusvoid 306136386Sglebiusm_tag_free_default(struct m_tag *t) 307124077Ssam{ 308124077Ssam#ifdef MAC 309124077Ssam if (t->m_tag_id == PACKET_TAG_MACLABEL) 310172930Srwatson mac_mbuf_tag_destroy(t); 311124077Ssam#endif 312124077Ssam free(t, M_PACKET_TAGS); 313124077Ssam} 314124077Ssam 315105194Ssam/* Get a packet tag structure along with specified data following. */ 316105194Ssamstruct m_tag * 317209390Sedm_tag_alloc(uint32_t cookie, int type, int len, int wait) 31862587Sitojun{ 319105194Ssam struct m_tag *t; 32062587Sitojun 321132488Salfred MBUF_CHECKSLEEP(wait); 322105194Ssam if (len < 0) 32362587Sitojun return NULL; 324105194Ssam t = malloc(len + sizeof(struct m_tag), M_PACKET_TAGS, wait); 325105194Ssam if (t == NULL) 326105194Ssam return NULL; 327124077Ssam m_tag_setup(t, cookie, type, len); 328136386Sglebius t->m_tag_free = m_tag_free_default; 329105194Ssam return t; 330105194Ssam} 33162587Sitojun 332105194Ssam/* Unlink and free a packet tag. */ 333105194Ssamvoid 334105194Ssamm_tag_delete(struct mbuf *m, struct m_tag *t) 33562587Sitojun{ 336132488Salfred 337105194Ssam KASSERT(m && t, ("m_tag_delete: null argument, m %p t %p", m, t)); 338105194Ssam m_tag_unlink(m, t); 339136347Sglebius m_tag_free(t); 340105194Ssam} 34162587Sitojun 342105194Ssam/* Unlink and free a packet tag chain, starting from given tag. */ 343105194Ssamvoid 344105194Ssamm_tag_delete_chain(struct mbuf *m, struct m_tag *t) 345105194Ssam{ 346105194Ssam struct m_tag *p, *q; 34762587Sitojun 348105194Ssam KASSERT(m, ("m_tag_delete_chain: null mbuf")); 349105194Ssam if (t != NULL) 350105194Ssam p = t; 351105194Ssam else 352105194Ssam p = SLIST_FIRST(&m->m_pkthdr.tags); 353105194Ssam if (p == NULL) 354105194Ssam return; 355105194Ssam while ((q = SLIST_NEXT(p, m_tag_link)) != NULL) 356105194Ssam m_tag_delete(m, q); 357105194Ssam m_tag_delete(m, p); 358105194Ssam} 359105194Ssam 360121645Ssam/* 361121645Ssam * Strip off all tags that would normally vanish when 362121645Ssam * passing through a network interface. Only persistent 363121645Ssam * tags will exist after this; these are expected to remain 364121645Ssam * so long as the mbuf chain exists, regardless of the 365121645Ssam * path the mbufs take. 366121645Ssam */ 367121645Ssamvoid 368121645Ssamm_tag_delete_nonpersistent(struct mbuf *m) 369121645Ssam{ 370121645Ssam struct m_tag *p, *q; 371121645Ssam 372121645Ssam SLIST_FOREACH_SAFE(p, &m->m_pkthdr.tags, m_tag_link, q) 373121645Ssam if ((p->m_tag_id & MTAG_PERSISTENT) == 0) 374121645Ssam m_tag_delete(m, p); 375121645Ssam} 376121645Ssam 377105194Ssam/* Find a tag, starting from a given position. */ 378105194Ssamstruct m_tag * 379209390Sedm_tag_locate(struct mbuf *m, uint32_t cookie, int type, struct m_tag *t) 380105194Ssam{ 381105194Ssam struct m_tag *p; 382105194Ssam 383107283Ssam KASSERT(m, ("m_tag_locate: null mbuf")); 384105194Ssam if (t == NULL) 385105194Ssam p = SLIST_FIRST(&m->m_pkthdr.tags); 386105194Ssam else 387105194Ssam p = SLIST_NEXT(t, m_tag_link); 388105194Ssam while (p != NULL) { 389105194Ssam if (p->m_tag_cookie == cookie && p->m_tag_id == type) 390105194Ssam return p; 391105194Ssam p = SLIST_NEXT(p, m_tag_link); 39262587Sitojun } 39362587Sitojun return NULL; 39462587Sitojun} 39562587Sitojun 396105194Ssam/* Copy a single tag. */ 397105194Ssamstruct m_tag * 398108466Ssamm_tag_copy(struct m_tag *t, int how) 39978064Sume{ 400105194Ssam struct m_tag *p; 40178064Sume 402132488Salfred MBUF_CHECKSLEEP(how); 403105194Ssam KASSERT(t, ("m_tag_copy: null tag")); 404108466Ssam p = m_tag_alloc(t->m_tag_cookie, t->m_tag_id, t->m_tag_len, how); 405105194Ssam if (p == NULL) 406105194Ssam return (NULL); 407113487Srwatson#ifdef MAC 408113487Srwatson /* 409113487Srwatson * XXXMAC: we should probably pass off the initialization, and 410113487Srwatson * copying here? can we hide that PACKET_TAG_MACLABEL is 411113487Srwatson * special from the mbuf code? 412113487Srwatson */ 413113487Srwatson if (t->m_tag_id == PACKET_TAG_MACLABEL) { 414172930Srwatson if (mac_mbuf_tag_init(p, how) != 0) { 415113487Srwatson m_tag_free(p); 416113487Srwatson return (NULL); 417113487Srwatson } 418172930Srwatson mac_mbuf_tag_copy(t, p); 419113487Srwatson } else 420113487Srwatson#endif 421113487Srwatson bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */ 422105194Ssam return p; 42378064Sume} 42478064Sume 425105194Ssam/* 426105194Ssam * Copy two tag chains. The destination mbuf (to) loses any attached 427105194Ssam * tags even if the operation fails. This should not be a problem, as 428105194Ssam * m_tag_copy_chain() is typically called with a newly-allocated 429105194Ssam * destination mbuf. 430105194Ssam */ 431105194Ssamint 432108466Ssamm_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how) 43378064Sume{ 434105194Ssam struct m_tag *p, *t, *tprev = NULL; 43578064Sume 436132488Salfred MBUF_CHECKSLEEP(how); 437105194Ssam KASSERT(to && from, 438107283Ssam ("m_tag_copy_chain: null argument, to %p from %p", to, from)); 439105194Ssam m_tag_delete_chain(to, NULL); 440105194Ssam SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) { 441108466Ssam t = m_tag_copy(p, how); 442105194Ssam if (t == NULL) { 443105194Ssam m_tag_delete_chain(to, NULL); 444105194Ssam return 0; 445105194Ssam } 446105194Ssam if (tprev == NULL) 447105194Ssam SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link); 448109619Ssam else 449105194Ssam SLIST_INSERT_AFTER(tprev, t, m_tag_link); 450109619Ssam tprev = t; 451105194Ssam } 452105194Ssam return 1; 45378064Sume} 454