ipsec_mbuf.c revision 1.27
1/*	$NetBSD: ipsec_mbuf.c,v 1.27 2018/04/28 14:39:34 maxv Exp $	*/
2
3/*
4 * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: sys/netipsec/ipsec_mbuf.c,v 1.5.2.2 2003/03/28 20:32:53 sam Exp $
29 */
30
31#include <sys/cdefs.h>
32__KERNEL_RCSID(0, "$NetBSD: ipsec_mbuf.c,v 1.27 2018/04/28 14:39:34 maxv Exp $");
33
34/*
35 * IPsec-specific mbuf routines.
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/mbuf.h>
41
42#include <netipsec/ipsec.h>
43#include <netipsec/ipsec_var.h>
44#include <netipsec/ipsec_private.h>
45
46/*
47 * Create a writable copy of the mbuf chain.  While doing this
48 * we compact the chain with a goal of producing a chain with
49 * at most two mbufs.  The second mbuf in this chain is likely
50 * to be a cluster.  The primary purpose of this work is to create
51 * a writable packet for encryption, compression, etc.  The
52 * secondary goal is to linearize the data so the data can be
53 * passed to crypto hardware in the most efficient manner possible.
54 */
55struct mbuf *
56m_clone(struct mbuf *m0)
57{
58	struct mbuf *m, *mprev;
59	struct mbuf *n, *mfirst, *mlast;
60	int len, off;
61
62	KASSERT(m0 != NULL);
63
64	mprev = NULL;
65	for (m = m0; m != NULL; m = mprev->m_next) {
66		/*
67		 * Regular mbufs are ignored unless there's a cluster
68		 * in front of it that we can use to coalesce.  We do
69		 * the latter mainly so later clusters can be coalesced
70		 * also w/o having to handle them specially (i.e. convert
71		 * mbuf+cluster -> cluster).  This optimization is heavily
72		 * influenced by the assumption that we're running over
73		 * Ethernet where MCLBYTES is large enough that the max
74		 * packet size will permit lots of coalescing into a
75		 * single cluster.  This in turn permits efficient
76		 * crypto operations, especially when using hardware.
77		 */
78		if ((m->m_flags & M_EXT) == 0) {
79			if (mprev && (mprev->m_flags & M_EXT) &&
80			    m->m_len <= M_TRAILINGSPACE(mprev)) {
81				/* XXX: this ignores mbuf types */
82				memcpy(mtod(mprev, char *) + mprev->m_len,
83				       mtod(m, char *), m->m_len);
84				mprev->m_len += m->m_len;
85				mprev->m_next = m->m_next;	/* unlink from chain */
86				m_free(m);			/* reclaim mbuf */
87				IPSEC_STATINC(IPSEC_STAT_MBCOALESCED);
88			} else {
89				mprev = m;
90			}
91			continue;
92		}
93
94		/*
95		 * Writable mbufs are left alone (for now).
96		 */
97		if (!M_READONLY(m)) {
98			mprev = m;
99			continue;
100		}
101
102		/*
103		 * Not writable, replace with a copy or coalesce with
104		 * the previous mbuf if possible (since we have to copy
105		 * it anyway, we try to reduce the number of mbufs and
106		 * clusters so that future work is easier).
107		 */
108		KASSERTMSG(m->m_flags & M_EXT, "m_flags 0x%x", m->m_flags);
109		/* NB: we only coalesce into a cluster or larger */
110		if (mprev != NULL && (mprev->m_flags & M_EXT) &&
111		    m->m_len <= M_TRAILINGSPACE(mprev)) {
112			/* XXX: this ignores mbuf types */
113			memcpy(mtod(mprev, char *) + mprev->m_len,
114			       mtod(m, char *), m->m_len);
115			mprev->m_len += m->m_len;
116			mprev->m_next = m->m_next;	/* unlink from chain */
117			m_free(m);			/* reclaim mbuf */
118			IPSEC_STATINC(IPSEC_STAT_CLCOALESCED);
119			continue;
120		}
121
122		/*
123		 * Allocate new space to hold the copy...
124		 */
125		if (mprev == NULL && (m->m_flags & M_PKTHDR)) {
126			/*
127			 * NB: if a packet header is present we must
128			 * allocate the mbuf separately from any cluster
129			 * because M_MOVE_PKTHDR will smash the data
130			 * pointer and drop the M_EXT marker.
131			 */
132			MGETHDR(n, M_DONTWAIT, m->m_type);
133			if (n == NULL) {
134				m_freem(m0);
135				return NULL;
136			}
137			M_MOVE_PKTHDR(n, m);
138			MCLGET(n, M_DONTWAIT);
139			if ((n->m_flags & M_EXT) == 0) {
140				m_free(n);
141				m_freem(m0);
142				return NULL;
143			}
144		} else {
145			n = m_getcl(M_DONTWAIT, m->m_type, m->m_flags);
146			if (n == NULL) {
147				m_freem(m0);
148				return NULL;
149			}
150		}
151
152		/*
153		 * ... and copy the data.  We deal with jumbo mbufs
154		 * (i.e. m_len > MCLBYTES) by splitting them into
155		 * clusters.  We could just malloc a buffer and make
156		 * it external but too many device drivers don't know
157		 * how to break up the non-contiguous memory when
158		 * doing DMA.
159		 */
160		len = m->m_len;
161		off = 0;
162		mfirst = n;
163		mlast = NULL;
164		for (;;) {
165			int cc = min(len, MCLBYTES);
166			memcpy(mtod(n, char *), mtod(m, char *) + off, cc);
167			n->m_len = cc;
168			if (mlast != NULL)
169				mlast->m_next = n;
170			mlast = n;
171			IPSEC_STATINC(IPSEC_STAT_CLCOPIED);
172
173			len -= cc;
174			if (len <= 0)
175				break;
176			off += cc;
177
178			n = m_getcl(M_DONTWAIT, m->m_type, m->m_flags);
179			if (n == NULL) {
180				m_freem(mfirst);
181				m_freem(m0);
182				return NULL;
183			}
184		}
185		n->m_next = m->m_next;
186		if (mprev == NULL)
187			m0 = mfirst;		/* new head of chain */
188		else
189			mprev->m_next = mfirst;	/* replace old mbuf */
190		m_free(m);			/* release old mbuf */
191		mprev = mfirst;
192	}
193
194	return m0;
195}
196
197/*
198 * Make space for a new header of length hlen at skip bytes
199 * into the packet.  When doing this we allocate new mbufs only
200 * when absolutely necessary.  The mbuf where the new header
201 * is to go is returned together with an offset into the mbuf.
202 * If NULL is returned then the mbuf chain may have been modified;
203 * the caller is assumed to always free the chain.
204 */
205struct mbuf *
206m_makespace(struct mbuf *m0, int skip, int hlen, int *off)
207{
208	struct mbuf *m;
209	unsigned remain;
210
211	KASSERT(m0 != NULL);
212	KASSERT(m0->m_flags & M_PKTHDR);
213	KASSERTMSG(hlen < MHLEN, "hlen too big: %u", hlen);
214
215	for (m = m0; m && skip > m->m_len; m = m->m_next)
216		skip -= m->m_len;
217	if (m == NULL)
218		return NULL;
219
220	/*
221	 * At this point skip is the offset into the mbuf m
222	 * where the new header should be placed.  Figure out
223	 * if there's space to insert the new header.  If so,
224	 * and copying the remainder makes sense then do so.
225	 * Otherwise insert a new mbuf in the chain, splitting
226	 * the contents of m as needed.
227	 */
228	remain = m->m_len - skip;		/* data to move */
229	if (hlen > M_TRAILINGSPACE(m)) {
230		struct mbuf *n0, *n, **np;
231		int todo, len, done, alloc;
232
233		n0 = NULL;
234		np = &n0;
235		alloc = 0;
236		done = 0;
237		todo = remain;
238		while (todo > 0) {
239			if (todo > MHLEN) {
240				n = m_getcl(M_DONTWAIT, m->m_type, 0);
241				len = MCLBYTES;
242			} else {
243				n = m_get(M_DONTWAIT, m->m_type);
244				len = MHLEN;
245			}
246			if (n == NULL) {
247				m_freem(n0);
248				return NULL;
249			}
250			*np = n;
251			np = &n->m_next;
252			alloc++;
253			len = min(todo, len);
254			memcpy(n->m_data, mtod(m, char *) + skip + done, len);
255			n->m_len = len;
256			done += len;
257			todo -= len;
258		}
259
260		if (hlen <= M_TRAILINGSPACE(m) + remain) {
261			m->m_len = skip + hlen;
262			*off = skip;
263			if (n0 != NULL) {
264				*np = m->m_next;
265				m->m_next = n0;
266			}
267		} else {
268			n = m_get(M_DONTWAIT, m->m_type);
269			if (n == NULL) {
270				m_freem(n0);
271				return NULL;
272			}
273			alloc++;
274
275			if ((n->m_next = n0) == NULL)
276				np = &n->m_next;
277			n0 = n;
278
279			*np = m->m_next;
280			m->m_next = n0;
281
282			n->m_len = hlen;
283			m->m_len = skip;
284
285			m = n;			/* header is at front ... */
286			*off = 0;		/* ... of new mbuf */
287		}
288
289		IPSEC_STATADD(IPSEC_STAT_MBINSERTED, alloc);
290	} else {
291		/*
292		 * Copy the remainder to the back of the mbuf
293		 * so there's space to write the new header.
294		 */
295		/* XXX can this be memcpy? does it handle overlap? */
296		memmove(mtod(m, char *) + skip + hlen,
297			mtod(m, char *) + skip, remain);
298		m->m_len += hlen;
299		*off = skip;
300	}
301
302	m0->m_pkthdr.len += hlen;		/* adjust packet length */
303	return m;
304}
305
306/*
307 * m_pad(m, n) pads <m> with <n> bytes at the end. The packet header
308 * length is updated, and a pointer to the first byte of the padding
309 * (which is guaranteed to be all in one mbuf) is returned.
310 */
311void *
312m_pad(struct mbuf *m, int n)
313{
314	register struct mbuf *m0, *m1;
315	register int len, pad;
316	void *retval;
317
318	if (__predict_false(n > MLEN)) {
319		panic("%s: %d > MLEN", __func__, n);
320	}
321	KASSERT(m->m_flags & M_PKTHDR);
322
323	len = m->m_pkthdr.len;
324	pad = n;
325	m0 = m;
326
327	while (m0->m_len < len) {
328		KASSERTMSG(m0->m_next != NULL,
329		    "m0 null, len %u m_len %u", len, m0->m_len);
330		len -= m0->m_len;
331		m0 = m0->m_next;
332	}
333
334	if (m0->m_len != len) {
335		IPSECLOG(LOG_DEBUG,
336		    "length mismatch (should be %d instead of %d)\n",
337		    m->m_pkthdr.len, m->m_pkthdr.len + m0->m_len - len);
338		m_freem(m);
339		return NULL;
340	}
341
342	/* Check for zero-length trailing mbufs, and find the last one. */
343	for (m1 = m0; m1->m_next; m1 = m1->m_next) {
344		if (m1->m_next->m_len != 0) {
345			IPSECLOG(LOG_DEBUG,
346			    "length mismatch (should be %d instead of %d)\n",
347			    m->m_pkthdr.len,
348			    m->m_pkthdr.len + m1->m_next->m_len);
349			m_freem(m);
350			return NULL;
351		}
352
353		m0 = m1->m_next;
354	}
355
356	if (pad > M_TRAILINGSPACE(m0)) {
357		/* Add an mbuf to the chain. */
358		MGET(m1, M_DONTWAIT, MT_DATA);
359		if (m1 == NULL) {
360			m_freem(m);
361			IPSECLOG(LOG_DEBUG, "unable to get extra mbuf\n");
362			return NULL;
363		}
364
365		m0->m_next = m1;
366		m0 = m1;
367		m0->m_len = 0;
368	}
369
370	retval = m0->m_data + m0->m_len;
371	m0->m_len += pad;
372	m->m_pkthdr.len += pad;
373
374	return retval;
375}
376
377/*
378 * Remove hlen data at offset skip in the packet.  This is used by
379 * the protocols strip protocol headers and associated data (e.g. IV,
380 * authenticator) on input.
381 */
382int
383m_striphdr(struct mbuf *m, int skip, int hlen)
384{
385	struct mbuf *m1;
386	int roff;
387
388	KASSERT(m->m_flags & M_PKTHDR);
389
390	/* Find beginning of header */
391	m1 = m_getptr(m, skip, &roff);
392	if (m1 == NULL)
393		return EINVAL;
394
395	/* Remove the header and associated data from the mbuf. */
396	if (roff == 0) {
397		/* The header was at the beginning of the mbuf */
398		IPSEC_STATINC(IPSEC_STAT_INPUT_FRONT);
399		m_adj(m1, hlen);
400		if (m1 != m)
401			m->m_pkthdr.len -= hlen;
402	} else if (roff + hlen >= m1->m_len) {
403		struct mbuf *mo;
404		int adjlen;
405
406		/*
407		 * Part or all of the header is at the end of this mbuf,
408		 * so first let's remove the remainder of the header from
409		 * the beginning of the remainder of the mbuf chain, if any.
410		 */
411		IPSEC_STATINC(IPSEC_STAT_INPUT_END);
412		if (roff + hlen > m1->m_len) {
413			adjlen = roff + hlen - m1->m_len;
414
415			/* Adjust the next mbuf by the remainder */
416			m_adj(m1->m_next, adjlen);
417
418			/* The second mbuf is guaranteed not to have a pkthdr... */
419			m->m_pkthdr.len -= adjlen;
420		}
421
422		/* Now, let's unlink the mbuf chain for a second...*/
423		mo = m1->m_next;
424		m1->m_next = NULL;
425
426		/* ...and trim the end of the first part of the chain...sick */
427		adjlen = m1->m_len - roff;
428		m_adj(m1, -adjlen);
429		if (m1 != m)
430			m->m_pkthdr.len -= adjlen;
431
432		/* Finally, let's relink */
433		m1->m_next = mo;
434	} else {
435		/*
436		 * The header lies in the "middle" of the mbuf; copy
437		 * the remainder of the mbuf down over the header.
438		 */
439		IPSEC_STATINC(IPSEC_STAT_INPUT_MIDDLE);
440		memmove(mtod(m1, u_char *) + roff,
441		      mtod(m1, u_char *) + roff + hlen,
442		      m1->m_len - (roff + hlen));
443		m1->m_len -= hlen;
444		m->m_pkthdr.len -= hlen;
445	}
446
447	return 0;
448}
449