2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * IPsec-specific mbuf routines.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
39 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netipsec/ipsec.h>
46 * Make space for a new header of length hlen at skip bytes
47 * into the packet. When doing this we allocate new mbufs only
48 * when absolutely necessary. The mbuf where the new header
49 * is to go is returned together with an offset into the mbuf.
50 * If NULL is returned then the mbuf chain may have been modified;
51 * the caller is assumed to always free the chain.
54 m_makespace(struct mbuf *m0, int skip, int hlen, int *off)
59 IPSEC_ASSERT(m0 != NULL, ("null mbuf"));
60 IPSEC_ASSERT(hlen < MHLEN, ("hlen too big: %u", hlen));
62 for (m = m0; m && skip > m->m_len; m = m->m_next)
67 * At this point skip is the offset into the mbuf m
68 * where the new header should be placed. Figure out
69 * if there's space to insert the new header. If so,
70 * and copying the remainder makes sense then do so.
71 * Otherwise insert a new mbuf in the chain, splitting
72 * the contents of m as needed.
74 remain = m->m_len - skip; /* data to move */
76 hlen + max_linkhdr < M_LEADINGSPACE(m)) {
78 * mbuf has enough free space at the beginning.
79 * XXX: which operation is the most heavy - copying of
80 * possible several hundred of bytes or allocation
81 * of new mbuf? We can remove max_linkhdr check
82 * here, but it is possible that this will lead
83 * to allocation of new mbuf in Layer 2 code.
86 bcopy(mtodo(m, hlen), mtod(m, caddr_t), skip);
89 } else if (hlen > M_TRAILINGSPACE(m)) {
90 struct mbuf *n0, *n, **np;
91 int todo, len, done, alloc;
100 n = m_getcl(M_NOWAIT, m->m_type, 0);
104 n = m_get(M_NOWAIT, m->m_type);
114 len = min(todo, len);
115 memcpy(n->m_data, mtod(m, char *) + skip + done, len);
121 if (hlen <= M_TRAILINGSPACE(m) + remain) {
122 m->m_len = skip + hlen;
130 n = m_get(M_NOWAIT, m->m_type);
137 if ((n->m_next = n0) == NULL)
147 m = n; /* header is at front ... */
148 *off = 0; /* ... of new mbuf */
150 IPSECSTAT_INC(ips_mbinserted);
153 * Copy the remainder to the back of the mbuf
154 * so there's space to write the new header.
156 bcopy(mtod(m, caddr_t) + skip,
157 mtod(m, caddr_t) + skip + hlen, remain);
161 m0->m_pkthdr.len += hlen; /* adjust packet length */
166 * m_pad(m, n) pads <m> with <n> bytes at the end. The packet header
167 * length is updated, and a pointer to the first byte of the padding
168 * (which is guaranteed to be all in one mbuf) is returned.
171 m_pad(struct mbuf *m, int n)
173 struct mbuf *m0, *m1;
177 if (n <= 0) { /* No stupid arguments. */
178 DPRINTF(("%s: pad length invalid (%d)\n", __func__, n));
183 len = m->m_pkthdr.len;
187 while (m0->m_len < len) {
192 if (m0->m_len != len) {
193 DPRINTF(("%s: length mismatch (should be %d instead of %d)\n",
194 __func__, m->m_pkthdr.len,
195 m->m_pkthdr.len + m0->m_len - len));
201 /* Check for zero-length trailing mbufs, and find the last one. */
202 for (m1 = m0; m1->m_next; m1 = m1->m_next) {
203 if (m1->m_next->m_len != 0) {
204 DPRINTF(("%s: length mismatch (should be %d instead "
205 "of %d)\n", __func__,
207 m->m_pkthdr.len + m1->m_next->m_len));
216 if (pad > M_TRAILINGSPACE(m0)) {
217 /* Add an mbuf to the chain. */
218 MGET(m1, M_NOWAIT, MT_DATA);
221 DPRINTF(("%s: unable to get extra mbuf\n", __func__));
230 retval = m0->m_data + m0->m_len;
232 m->m_pkthdr.len += pad;
238 * Remove hlen data at offset skip in the packet. This is used by
239 * the protocols strip protocol headers and associated data (e.g. IV,
240 * authenticator) on input.
243 m_striphdr(struct mbuf *m, int skip, int hlen)
248 /* Find beginning of header */
249 m1 = m_getptr(m, skip, &roff);
253 /* Remove the header and associated data from the mbuf. */
255 /* The header was at the beginning of the mbuf */
256 IPSECSTAT_INC(ips_input_front);
259 m->m_pkthdr.len -= hlen;
260 } else if (roff + hlen >= m1->m_len) {
265 * Part or all of the header is at the end of this mbuf,
266 * so first let's remove the remainder of the header from
267 * the beginning of the remainder of the mbuf chain, if any.
269 IPSECSTAT_INC(ips_input_end);
270 if (roff + hlen > m1->m_len) {
271 adjlen = roff + hlen - m1->m_len;
273 /* Adjust the next mbuf by the remainder */
274 m_adj(m1->m_next, adjlen);
276 /* The second mbuf is guaranteed not to have a pkthdr... */
277 m->m_pkthdr.len -= adjlen;
280 /* Now, let's unlink the mbuf chain for a second...*/
284 /* ...and trim the end of the first part of the chain...sick */
285 adjlen = m1->m_len - roff;
288 m->m_pkthdr.len -= adjlen;
290 /* Finally, let's relink */
294 * The header lies in the "middle" of the mbuf; copy
295 * the remainder of the mbuf down over the header.
297 IPSECSTAT_INC(ips_input_middle);
298 bcopy(mtod(m1, u_char *) + roff + hlen,
299 mtod(m1, u_char *) + roff,
300 m1->m_len - (roff + hlen));
302 m->m_pkthdr.len -= hlen;
308 * Diagnostic routine to check mbuf alignment as required by the
309 * crypto device drivers (that use DMA).
312 m_checkalignment(const char* where, struct mbuf *m0, int off, int len)
315 struct mbuf *m = m_getptr(m0, off, &roff);
320 printf("%s (off %u len %u): ", where, off, len);
321 addr = mtod(m, caddr_t) + roff;
325 if (((uintptr_t) addr) & 3) {
326 printf("addr misaligned %p,", addr);
333 if (len && (mlen & 3)) {
334 printf("len mismatch %u,", mlen);
338 addr = m ? mtod(m, caddr_t) : NULL;
339 } while (m && len > 0);
340 for (m = m0; m; m = m->m_next)
341 printf(" [%p:%u]", mtod(m, caddr_t), m->m_len);