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