1/*	$KAME: uipc_mbuf2.c,v 1.31 2001/11/28 11:08:53 itojun Exp $	*/
2/*	$NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $	*/
3
4/*-
5 * Copyright (C) 1999 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32/*-
33 * Copyright (c) 1982, 1986, 1988, 1991, 1993
34 *	The Regents of the University of California.  All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 *    may be used to endorse or promote products derived from this software
46 *    without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 *	@(#)uipc_mbuf.c	8.4 (Berkeley) 2/14/95
61 */
62#include <sys/cdefs.h>
63__FBSDID("$FreeBSD$");
64
65/*#define PULLDOWN_DEBUG*/
66
67#include <sys/param.h>
68#include <sys/systm.h>
69#include <sys/kernel.h>
70#include <sys/lock.h>
71#include <sys/malloc.h>
72#include <sys/mbuf.h>
73#include <sys/mutex.h>
74
75#include <security/mac/mac_framework.h>
76
77#define malloc(size, tag, flags) kernel_malloc(size, tag, flags)
78#define free(pointer, tag) kernel_free(pointer, tag)
79
80/* can't call it m_dup(), as freebsd[34] uses m_dup() with different arg */
81static struct mbuf *m_dup1(struct mbuf *, int, int, int);
82
83/*
84 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
85 * packet chain before "off" is kept untouched.
86 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
87 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
88 *
89 * on error return (NULL return value), original "m" will be freed.
90 *
91 * XXX: M_TRAILINGSPACE/M_LEADINGSPACE only permitted on writable ext_buf.
92 */
93struct mbuf *
94m_pulldown(struct mbuf *m, int off, int len, int *offp)
95{
96	struct mbuf *n, *o;
97	int hlen, tlen, olen;
98	int writable;
99
100	/* check invalid arguments. */
101	if (m == NULL)
102		panic("m == NULL in m_pulldown()");
103	if (len > MCLBYTES) {
104		m_freem(m);
105		return NULL;	/* impossible */
106	}
107
108#ifdef PULLDOWN_DEBUG
109    {
110	struct mbuf *t;
111	printf("before:");
112	for (t = m; t; t = t->m_next)
113		printf(" %d", t->m_len);
114	printf("\n");
115    }
116#endif
117	n = m;
118	while (n != NULL && off > 0) {
119		if (n->m_len > off)
120			break;
121		off -= n->m_len;
122		n = n->m_next;
123	}
124	/* be sure to point non-empty mbuf */
125	while (n != NULL && n->m_len == 0)
126		n = n->m_next;
127	if (!n) {
128		m_freem(m);
129		return NULL;	/* mbuf chain too short */
130	}
131
132	/*
133	 * XXX: This code is flawed because it considers a "writable" mbuf
134	 *      data region to require all of the following:
135	 *	  (i) mbuf _has_ to have M_EXT set; if it is just a regular
136	 *	      mbuf, it is still not considered "writable."
137	 *	  (ii) since mbuf has M_EXT, the ext_type _has_ to be
138	 *	       EXT_CLUSTER. Anything else makes it non-writable.
139	 *	  (iii) M_WRITABLE() must evaluate true.
140	 *      Ideally, the requirement should only be (iii).
141	 *
142	 * If we're writable, we're sure we're writable, because the ref. count
143	 * cannot increase from 1, as that would require posession of mbuf
144	 * n by someone else (which is impossible). However, if we're _not_
145	 * writable, we may eventually become writable )if the ref. count drops
146	 * to 1), but we'll fail to notice it unless we re-evaluate
147	 * M_WRITABLE(). For now, we only evaluate once at the beginning and
148	 * live with this.
149	 */
150	/*
151	 * XXX: This is dumb. If we're just a regular mbuf with no M_EXT,
152	 *      then we're not "writable," according to this code.
153	 */
154	writable = 0;
155	if ((n->m_flags & M_EXT) == 0 ||
156	    (n->m_ext.ext_type == EXT_CLUSTER && M_WRITABLE(n)))
157		writable = 1;
158
159	/*
160	 * the target data is on <n, off>.
161	 * if we got enough data on the mbuf "n", we're done.
162	 */
163	if ((off == 0 || offp) && len <= n->m_len - off && writable)
164		goto ok;
165
166	/*
167	 * when len <= n->m_len - off and off != 0, it is a special case.
168	 * len bytes from <n, off> sits in single mbuf, but the caller does
169	 * not like the starting position (off).
170	 * chop the current mbuf into two pieces, set off to 0.
171	 */
172	if (len <= n->m_len - off) {
173		o = m_dup1(n, off, n->m_len - off, M_DONTWAIT);
174		if (o == NULL) {
175			m_freem(m);
176			return NULL;	/* ENOBUFS */
177		}
178		n->m_len = off;
179		o->m_next = n->m_next;
180		n->m_next = o;
181		n = n->m_next;
182		off = 0;
183		goto ok;
184	}
185
186	/*
187	 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
188	 * and construct contiguous mbuf with m_len == len.
189	 * note that hlen + tlen == len, and tlen > 0.
190	 */
191	hlen = n->m_len - off;
192	tlen = len - hlen;
193
194	/*
195	 * ensure that we have enough trailing data on mbuf chain.
196	 * if not, we can do nothing about the chain.
197	 */
198	olen = 0;
199	for (o = n->m_next; o != NULL; o = o->m_next)
200		olen += o->m_len;
201	if (hlen + olen < len) {
202		m_freem(m);
203		return NULL;	/* mbuf chain too short */
204	}
205
206	/*
207	 * easy cases first.
208	 * we need to use m_copydata() to get data from <n->m_next, 0>.
209	 */
210	if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen
211	 && writable) {
212		m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
213		n->m_len += tlen;
214		m_adj(n->m_next, tlen);
215		goto ok;
216	}
217	if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen
218	 && writable) {
219		n->m_next->m_data -= hlen;
220		n->m_next->m_len += hlen;
221		bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
222		n->m_len -= hlen;
223		n = n->m_next;
224		off = 0;
225		goto ok;
226	}
227
228	/*
229	 * now, we need to do the hard way.  don't m_copy as there's no room
230	 * on both end.
231	 */
232	if (len > MLEN)
233		o = m_getcl(M_DONTWAIT, m->m_type, 0);
234	else
235		o = m_get(M_DONTWAIT, m->m_type);
236	if (!o) {
237		m_freem(m);
238		return NULL;	/* ENOBUFS */
239	}
240	/* get hlen from <n, off> into <o, 0> */
241	o->m_len = hlen;
242	bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen);
243	n->m_len -= hlen;
244	/* get tlen from <n->m_next, 0> into <o, hlen> */
245	m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
246	o->m_len += tlen;
247	m_adj(n->m_next, tlen);
248	o->m_next = n->m_next;
249	n->m_next = o;
250	n = o;
251	off = 0;
252
253ok:
254#ifdef PULLDOWN_DEBUG
255    {
256	struct mbuf *t;
257	printf("after:");
258	for (t = m; t; t = t->m_next)
259		printf("%c%d", t == n ? '*' : ' ', t->m_len);
260	printf(" (off=%d)\n", off);
261    }
262#endif
263	if (offp)
264		*offp = off;
265	return n;
266}
267
268static struct mbuf *
269m_dup1(struct mbuf *m, int off, int len, int wait)
270{
271	struct mbuf *n;
272	int copyhdr;
273
274	if (len > MCLBYTES)
275		return NULL;
276	if (off == 0 && (m->m_flags & M_PKTHDR) != 0)
277		copyhdr = 1;
278	else
279		copyhdr = 0;
280	if (len >= MINCLSIZE) {
281		if (copyhdr == 1)
282			n = m_getcl(wait, m->m_type, M_PKTHDR);
283		else
284			n = m_getcl(wait, m->m_type, 0);
285	} else {
286		if (copyhdr == 1)
287			n = m_gethdr(wait, m->m_type);
288		else
289			n = m_get(wait, m->m_type);
290	}
291	if (!n)
292		return NULL; /* ENOBUFS */
293
294	if (copyhdr && !m_dup_pkthdr(n, m, wait)) {
295		m_free(n);
296		return NULL;
297	}
298	m_copydata(m, off, len, mtod(n, caddr_t));
299	n->m_len = len;
300	return n;
301}
302
303/* Free a packet tag. */
304void
305m_tag_free_default(struct m_tag *t)
306{
307#ifdef MAC
308	if (t->m_tag_id == PACKET_TAG_MACLABEL)
309		mac_mbuf_tag_destroy(t);
310#endif
311	free(t, M_PACKET_TAGS);
312}
313
314/* Get a packet tag structure along with specified data following. */
315struct m_tag *
316m_tag_alloc(u_int32_t cookie, int type, int len, int wait)
317{
318	struct m_tag *t;
319
320	MBUF_CHECKSLEEP(wait);
321	if (len < 0)
322		return NULL;
323	t = malloc(len + sizeof(struct m_tag), M_PACKET_TAGS, wait);
324	if (t == NULL)
325		return NULL;
326	m_tag_setup(t, cookie, type, len);
327	t->m_tag_free = m_tag_free_default;
328	return t;
329}
330
331/* Unlink and free a packet tag. */
332void
333m_tag_delete(struct mbuf *m, struct m_tag *t)
334{
335
336	KASSERT(m && t, ("m_tag_delete: null argument, m %p t %p", m, t));
337	m_tag_unlink(m, t);
338	m_tag_free(t);
339}
340
341/* Unlink and free a packet tag chain, starting from given tag. */
342void
343m_tag_delete_chain(struct mbuf *m, struct m_tag *t)
344{
345	struct m_tag *p, *q;
346
347	KASSERT(m, ("m_tag_delete_chain: null mbuf"));
348	if (t != NULL)
349		p = t;
350	else
351		p = SLIST_FIRST(&m->m_pkthdr.tags);
352	if (p == NULL)
353		return;
354	while ((q = SLIST_NEXT(p, m_tag_link)) != NULL)
355		m_tag_delete(m, q);
356	m_tag_delete(m, p);
357}
358
359/*
360 * Strip off all tags that would normally vanish when
361 * passing through a network interface.  Only persistent
362 * tags will exist after this; these are expected to remain
363 * so long as the mbuf chain exists, regardless of the
364 * path the mbufs take.
365 */
366void
367m_tag_delete_nonpersistent(struct mbuf *m)
368{
369	struct m_tag *p, *q;
370
371	SLIST_FOREACH_SAFE(p, &m->m_pkthdr.tags, m_tag_link, q)
372		if ((p->m_tag_id & MTAG_PERSISTENT) == 0)
373			m_tag_delete(m, p);
374}
375
376/* Find a tag, starting from a given position. */
377struct m_tag *
378m_tag_locate(struct mbuf *m, u_int32_t cookie, int type, struct m_tag *t)
379{
380	struct m_tag *p;
381
382	KASSERT(m, ("m_tag_locate: null mbuf"));
383	if (t == NULL)
384		p = SLIST_FIRST(&m->m_pkthdr.tags);
385	else
386		p = SLIST_NEXT(t, m_tag_link);
387	while (p != NULL) {
388		if (p->m_tag_cookie == cookie && p->m_tag_id == type)
389			return p;
390		p = SLIST_NEXT(p, m_tag_link);
391	}
392	return NULL;
393}
394
395/* Copy a single tag. */
396struct m_tag *
397m_tag_copy(struct m_tag *t, int how)
398{
399	struct m_tag *p;
400
401	MBUF_CHECKSLEEP(how);
402	KASSERT(t, ("m_tag_copy: null tag"));
403	p = m_tag_alloc(t->m_tag_cookie, t->m_tag_id, t->m_tag_len, how);
404	if (p == NULL)
405		return (NULL);
406#ifdef MAC
407	/*
408	 * XXXMAC: we should probably pass off the initialization, and
409	 * copying here?  can we hide that PACKET_TAG_MACLABEL is
410	 * special from the mbuf code?
411	 */
412	if (t->m_tag_id == PACKET_TAG_MACLABEL) {
413		if (mac_mbuf_tag_init(p, how) != 0) {
414			m_tag_free(p);
415			return (NULL);
416		}
417		mac_mbuf_tag_copy(t, p);
418	} else
419#endif
420		bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */
421	return p;
422}
423
424/*
425 * Copy two tag chains. The destination mbuf (to) loses any attached
426 * tags even if the operation fails. This should not be a problem, as
427 * m_tag_copy_chain() is typically called with a newly-allocated
428 * destination mbuf.
429 */
430int
431m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how)
432{
433	struct m_tag *p, *t, *tprev = NULL;
434
435	MBUF_CHECKSLEEP(how);
436	KASSERT(to && from,
437		("m_tag_copy_chain: null argument, to %p from %p", to, from));
438	m_tag_delete_chain(to, NULL);
439	SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
440		t = m_tag_copy(p, how);
441		if (t == NULL) {
442			m_tag_delete_chain(to, NULL);
443			return 0;
444		}
445		if (tprev == NULL)
446			SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
447		else
448			SLIST_INSERT_AFTER(tprev, t, m_tag_link);
449		tprev = t;
450	}
451	return 1;
452}
453