/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728/*29* IPsec-specific mbuf routines.30*/3132#include "opt_ipsec.h"3334#include <sys/param.h>35#include <sys/systm.h>36#include <sys/malloc.h>37#include <sys/mbuf.h>38#include <sys/socket.h>3940#include <net/vnet.h>41#include <netinet/in.h>42#include <netipsec/ipsec.h>4344/*45* Make space for a new header of length hlen at skip bytes46* into the packet. When doing this we allocate new mbufs only47* when absolutely necessary. The mbuf where the new header48* is to go is returned together with an offset into the mbuf.49* If NULL is returned then the mbuf chain may have been modified;50* the caller is assumed to always free the chain.51*/52struct mbuf *53m_makespace(struct mbuf *m0, int skip, int hlen, int *off)54{55struct mbuf *m;56unsigned remain;5758IPSEC_ASSERT(m0 != NULL, ("null mbuf"));59IPSEC_ASSERT(hlen < MHLEN, ("hlen too big: %u", hlen));6061for (m = m0; m && skip > m->m_len; m = m->m_next)62skip -= m->m_len;63if (m == NULL)64return (NULL);65/*66* At this point skip is the offset into the mbuf m67* where the new header should be placed. Figure out68* if there's space to insert the new header. If so,69* and copying the remainder makes sense then do so.70* Otherwise insert a new mbuf in the chain, splitting71* the contents of m as needed.72*/73remain = m->m_len - skip; /* data to move */74if (remain > skip &&75hlen + max_linkhdr < M_LEADINGSPACE(m)) {76/*77* mbuf has enough free space at the beginning.78* XXX: which operation is the most heavy - copying of79* possible several hundred of bytes or allocation80* of new mbuf? We can remove max_linkhdr check81* here, but it is possible that this will lead82* to allocation of new mbuf in Layer 2 code.83*/84m->m_data -= hlen;85bcopy(mtodo(m, hlen), mtod(m, caddr_t), skip);86m->m_len += hlen;87*off = skip;88} else if (hlen > M_TRAILINGSPACE(m)) {89struct mbuf *n0, *n, **np;90int todo, len, done;9192n0 = NULL;93np = &n0;94done = 0;95todo = remain;96while (todo > 0) {97if (todo > MHLEN) {98n = m_getcl(M_NOWAIT, m->m_type, 0);99len = MCLBYTES;100}101else {102n = m_get(M_NOWAIT, m->m_type);103len = MHLEN;104}105if (n == NULL) {106m_freem(n0);107return NULL;108}109*np = n;110np = &n->m_next;111len = min(todo, len);112memcpy(n->m_data, mtod(m, char *) + skip + done, len);113n->m_len = len;114done += len;115todo -= len;116}117118if (hlen <= M_TRAILINGSPACE(m) + remain) {119m->m_len = skip + hlen;120*off = skip;121if (n0 != NULL) {122*np = m->m_next;123m->m_next = n0;124}125}126else {127n = m_get(M_NOWAIT, m->m_type);128if (n == NULL) {129m_freem(n0);130return NULL;131}132133if ((n->m_next = n0) == NULL)134np = &n->m_next;135n0 = n;136137*np = m->m_next;138m->m_next = n0;139140n->m_len = hlen;141m->m_len = skip;142143m = n; /* header is at front ... */144*off = 0; /* ... of new mbuf */145}146IPSECSTAT_INC(ips_mbinserted);147} else {148/*149* Copy the remainder to the back of the mbuf150* so there's space to write the new header.151*/152bcopy(mtod(m, caddr_t) + skip,153mtod(m, caddr_t) + skip + hlen, remain);154m->m_len += hlen;155*off = skip;156}157m0->m_pkthdr.len += hlen; /* adjust packet length */158return m;159}160161/*162* m_pad(m, n) pads <m> with <n> bytes at the end. The packet header163* length is updated, and a pointer to the first byte of the padding164* (which is guaranteed to be all in one mbuf) is returned.165*/166caddr_t167m_pad(struct mbuf *m, int n)168{169struct mbuf *m0, *m1;170int len, pad;171caddr_t retval;172173if (n <= 0) { /* No stupid arguments. */174DPRINTF(("%s: pad length invalid (%d)\n", __func__, n));175m_freem(m);176return NULL;177}178179len = m->m_pkthdr.len;180pad = n;181m0 = m;182183while (m0->m_len < len) {184len -= m0->m_len;185m0 = m0->m_next;186}187188if (m0->m_len != len) {189DPRINTF(("%s: length mismatch (should be %d instead of %d)\n",190__func__, m->m_pkthdr.len,191m->m_pkthdr.len + m0->m_len - len));192193m_freem(m);194return NULL;195}196197/* Check for zero-length trailing mbufs, and find the last one. */198for (m1 = m0; m1->m_next; m1 = m1->m_next) {199if (m1->m_next->m_len != 0) {200DPRINTF(("%s: length mismatch (should be %d instead "201"of %d)\n", __func__,202m->m_pkthdr.len,203m->m_pkthdr.len + m1->m_next->m_len));204205m_freem(m);206return NULL;207}208209m0 = m1->m_next;210}211212if (pad > M_TRAILINGSPACE(m0)) {213/* Add an mbuf to the chain. */214MGET(m1, M_NOWAIT, MT_DATA);215if (m1 == NULL) {216m_freem(m0);217DPRINTF(("%s: unable to get extra mbuf\n", __func__));218return NULL;219}220221m0->m_next = m1;222m0 = m1;223m0->m_len = 0;224}225226retval = m0->m_data + m0->m_len;227m0->m_len += pad;228m->m_pkthdr.len += pad;229230return retval;231}232233/*234* Remove hlen data at offset skip in the packet. This is used by235* the protocols strip protocol headers and associated data (e.g. IV,236* authenticator) on input.237*/238int239m_striphdr(struct mbuf *m, int skip, int hlen)240{241struct mbuf *m1;242int roff;243244/* Find beginning of header */245m1 = m_getptr(m, skip, &roff);246if (m1 == NULL)247return (EINVAL);248249/* Remove the header and associated data from the mbuf. */250if (roff == 0) {251/* The header was at the beginning of the mbuf */252IPSECSTAT_INC(ips_input_front);253m_adj(m1, hlen);254if (m1 != m)255m->m_pkthdr.len -= hlen;256} else if (roff + hlen >= m1->m_len) {257struct mbuf *mo;258int adjlen;259260/*261* Part or all of the header is at the end of this mbuf,262* so first let's remove the remainder of the header from263* the beginning of the remainder of the mbuf chain, if any.264*/265IPSECSTAT_INC(ips_input_end);266if (roff + hlen > m1->m_len) {267adjlen = roff + hlen - m1->m_len;268269/* Adjust the next mbuf by the remainder */270m_adj(m1->m_next, adjlen);271272/* The second mbuf is guaranteed not to have a pkthdr... */273m->m_pkthdr.len -= adjlen;274}275276/* Now, let's unlink the mbuf chain for a second...*/277mo = m1->m_next;278m1->m_next = NULL;279280/* ...and trim the end of the first part of the chain...sick */281adjlen = m1->m_len - roff;282m_adj(m1, -adjlen);283if (m1 != m)284m->m_pkthdr.len -= adjlen;285286/* Finally, let's relink */287m1->m_next = mo;288} else {289/*290* The header lies in the "middle" of the mbuf; copy291* the remainder of the mbuf down over the header.292*/293IPSECSTAT_INC(ips_input_middle);294bcopy(mtod(m1, u_char *) + roff + hlen,295mtod(m1, u_char *) + roff,296m1->m_len - (roff + hlen));297m1->m_len -= hlen;298m->m_pkthdr.len -= hlen;299}300return (0);301}302303/*304* Diagnostic routine to check mbuf alignment as required by the305* crypto device drivers (that use DMA).306*/307void308m_checkalignment(const char* where, struct mbuf *m0, int off, int len)309{310int roff;311struct mbuf *m = m_getptr(m0, off, &roff);312caddr_t addr;313314if (m == NULL)315return;316printf("%s (off %u len %u): ", where, off, len);317addr = mtod(m, caddr_t) + roff;318do {319int mlen;320321if (((uintptr_t) addr) & 3) {322printf("addr misaligned %p,", addr);323break;324}325mlen = m->m_len;326if (mlen > len)327mlen = len;328len -= mlen;329if (len && (mlen & 3)) {330printf("len mismatch %u,", mlen);331break;332}333m = m->m_next;334addr = m ? mtod(m, caddr_t) : NULL;335} while (m && len > 0);336for (m = m0; m; m = m->m_next)337printf(" [%p:%u]", mtod(m, caddr_t), m->m_len);338printf("\n");339}340341342