FreeBSD/Linux Kernel Cross Reference
sys/kern/uipc_mbuf2.c
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 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)uipc_mbuf.c 8.4 (Berkeley) 2/14/95
65 */
66
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD: releng/5.2/sys/kern/uipc_mbuf2.c 121645 2003-10-29 05:40:07Z sam $");
69
70 /*#define PULLDOWN_DEBUG*/
71
72 #include "opt_mac.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/lock.h>
78 #include <sys/mac.h>
79 #include <sys/malloc.h>
80 #include <sys/mbuf.h>
81 #include <sys/mutex.h>
82
83 MALLOC_DEFINE(M_PACKET_TAGS, "tag", "packet-attached information");
84
85 /* can't call it m_dup(), as freebsd[34] uses m_dup() with different arg */
86 static struct mbuf *m_dup1(struct mbuf *, int, int, int);
87
88 /*
89 * ensure that [off, off + len) is contiguous on the mbuf chain "m".
90 * packet chain before "off" is kept untouched.
91 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
92 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
93 *
94 * on error return (NULL return value), original "m" will be freed.
95 *
96 * XXX: M_TRAILINGSPACE/M_LEADINGSPACE only permitted on writable ext_buf.
97 */
98 struct mbuf *
99 m_pulldown(struct mbuf *m, int off, int len, int *offp)
100 {
101 struct mbuf *n, *o;
102 int hlen, tlen, olen;
103 int writable;
104
105 /* check invalid arguments. */
106 if (m == NULL)
107 panic("m == NULL in m_pulldown()");
108 if (len > MCLBYTES) {
109 m_freem(m);
110 return NULL; /* impossible */
111 }
112
113 #ifdef PULLDOWN_DEBUG
114 {
115 struct mbuf *t;
116 printf("before:");
117 for (t = m; t; t = t->m_next)
118 printf(" %d", t->m_len);
119 printf("\n");
120 }
121 #endif
122 n = m;
123 while (n != NULL && off > 0) {
124 if (n->m_len > off)
125 break;
126 off -= n->m_len;
127 n = n->m_next;
128 }
129 /* be sure to point non-empty mbuf */
130 while (n != NULL && n->m_len == 0)
131 n = n->m_next;
132 if (!n) {
133 m_freem(m);
134 return NULL; /* mbuf chain too short */
135 }
136
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 posession 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 /*
156 * XXX: This is dumb. If we're just a regular mbuf with no M_EXT,
157 * then we're not "writable," according to this code.
158 */
159 writable = 0;
160 if ((n->m_flags & M_EXT) == 0 ||
161 (n->m_ext.ext_type == EXT_CLUSTER && M_WRITABLE(n)))
162 writable = 1;
163
164 /*
165 * the target data is on <n, off>.
166 * if we got enough data on the mbuf "n", we're done.
167 */
168 if ((off == 0 || offp) && len <= n->m_len - off && writable)
169 goto ok;
170
171 /*
172 * when len <= n->m_len - off and off != 0, it is a special case.
173 * len bytes from <n, off> sits in single mbuf, but the caller does
174 * not like the starting position (off).
175 * chop the current mbuf into two pieces, set off to 0.
176 */
177 if (len <= n->m_len - off) {
178 o = m_dup1(n, off, n->m_len - off, M_DONTWAIT);
179 if (o == NULL) {
180 m_freem(m);
181 return NULL; /* ENOBUFS */
182 }
183 n->m_len = off;
184 o->m_next = n->m_next;
185 n->m_next = o;
186 n = n->m_next;
187 off = 0;
188 goto ok;
189 }
190
191 /*
192 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
193 * and construct contiguous mbuf with m_len == len.
194 * note that hlen + tlen == len, and tlen > 0.
195 */
196 hlen = n->m_len - off;
197 tlen = len - hlen;
198
199 /*
200 * ensure that we have enough trailing data on mbuf chain.
201 * if not, we can do nothing about the chain.
202 */
203 olen = 0;
204 for (o = n->m_next; o != NULL; o = o->m_next)
205 olen += o->m_len;
206 if (hlen + olen < len) {
207 m_freem(m);
208 return NULL; /* mbuf chain too short */
209 }
210
211 /*
212 * easy cases first.
213 * we need to use m_copydata() to get data from <n->m_next, 0>.
214 */
215 if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen
216 && writable) {
217 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
218 n->m_len += tlen;
219 m_adj(n->m_next, tlen);
220 goto ok;
221 }
222 if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen
223 && writable) {
224 n->m_next->m_data -= hlen;
225 n->m_next->m_len += hlen;
226 bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
227 n->m_len -= hlen;
228 n = n->m_next;
229 off = 0;
230 goto ok;
231 }
232
233 /*
234 * now, we need to do the hard way. don't m_copy as there's no room
235 * on both end.
236 */
237 MGET(o, M_DONTWAIT, m->m_type);
238 if (o && len > MLEN) {
239 MCLGET(o, M_DONTWAIT);
240 if ((o->m_flags & M_EXT) == 0) {
241 m_free(o);
242 o = NULL;
243 }
244 }
245 if (!o) {
246 m_freem(m);
247 return NULL; /* ENOBUFS */
248 }
249 /* get hlen from <n, off> into <o, 0> */
250 o->m_len = hlen;
251 bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen);
252 n->m_len -= hlen;
253 /* get tlen from <n->m_next, 0> into <o, hlen> */
254 m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
255 o->m_len += tlen;
256 m_adj(n->m_next, tlen);
257 o->m_next = n->m_next;
258 n->m_next = o;
259 n = o;
260 off = 0;
261
262 ok:
263 #ifdef PULLDOWN_DEBUG
264 {
265 struct mbuf *t;
266 printf("after:");
267 for (t = m; t; t = t->m_next)
268 printf("%c%d", t == n ? '*' : ' ', t->m_len);
269 printf(" (off=%d)\n", off);
270 }
271 #endif
272 if (offp)
273 *offp = off;
274 return n;
275 }
276
277 static struct mbuf *
278 m_dup1(struct mbuf *m, int off, int len, int wait)
279 {
280 struct mbuf *n;
281 int l;
282 int copyhdr;
283
284 if (len > MCLBYTES)
285 return NULL;
286 if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
287 copyhdr = 1;
288 MGETHDR(n, wait, m->m_type);
289 l = MHLEN;
290 } else {
291 copyhdr = 0;
292 MGET(n, wait, m->m_type);
293 l = MLEN;
294 }
295 if (n && len > l) {
296 MCLGET(n, wait);
297 if ((n->m_flags & M_EXT) == 0) {
298 m_free(n);
299 n = NULL;
300 }
301 }
302 if (!n)
303 return NULL;
304
305 if (copyhdr && !m_dup_pkthdr(n, m, wait)) {
306 m_free(n);
307 return NULL;
308 }
309 m_copydata(m, off, len, mtod(n, caddr_t));
310 return n;
311 }
312
313 /* Get a packet tag structure along with specified data following. */
314 struct m_tag *
315 m_tag_alloc(u_int32_t cookie, int type, int len, int wait)
316 {
317 struct m_tag *t;
318
319 if (len < 0)
320 return NULL;
321 t = malloc(len + sizeof(struct m_tag), M_PACKET_TAGS, wait);
322 if (t == NULL)
323 return NULL;
324 t->m_tag_id = type;
325 t->m_tag_len = len;
326 t->m_tag_cookie = cookie;
327 return t;
328 }
329
330 /* Free a packet tag. */
331 void
332 m_tag_free(struct m_tag *t)
333 {
334 #ifdef MAC
335 if (t->m_tag_id == PACKET_TAG_MACLABEL)
336 mac_destroy_mbuf_tag(t);
337 #endif
338 free(t, M_PACKET_TAGS);
339 }
340
341 /* Prepend a packet tag. */
342 void
343 m_tag_prepend(struct mbuf *m, struct m_tag *t)
344 {
345 KASSERT(m && t, ("m_tag_prepend: null argument, m %p t %p", m, t));
346 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
347 }
348
349 /* Unlink a packet tag. */
350 void
351 m_tag_unlink(struct mbuf *m, struct m_tag *t)
352 {
353 KASSERT(m && t, ("m_tag_unlink: null argument, m %p t %p", m, t));
354 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
355 }
356
357 /* Unlink and free a packet tag. */
358 void
359 m_tag_delete(struct mbuf *m, struct m_tag *t)
360 {
361 KASSERT(m && t, ("m_tag_delete: null argument, m %p t %p", m, t));
362 m_tag_unlink(m, t);
363 m_tag_free(t);
364 }
365
366 /* Unlink and free a packet tag chain, starting from given tag. */
367 void
368 m_tag_delete_chain(struct mbuf *m, struct m_tag *t)
369 {
370 struct m_tag *p, *q;
371
372 KASSERT(m, ("m_tag_delete_chain: null mbuf"));
373 if (t != NULL)
374 p = t;
375 else
376 p = SLIST_FIRST(&m->m_pkthdr.tags);
377 if (p == NULL)
378 return;
379 while ((q = SLIST_NEXT(p, m_tag_link)) != NULL)
380 m_tag_delete(m, q);
381 m_tag_delete(m, p);
382 }
383
384 /*
385 * Strip off all tags that would normally vanish when
386 * passing through a network interface. Only persistent
387 * tags will exist after this; these are expected to remain
388 * so long as the mbuf chain exists, regardless of the
389 * path the mbufs take.
390 */
391 void
392 m_tag_delete_nonpersistent(struct mbuf *m)
393 {
394 struct m_tag *p, *q;
395
396 SLIST_FOREACH_SAFE(p, &m->m_pkthdr.tags, m_tag_link, q)
397 if ((p->m_tag_id & MTAG_PERSISTENT) == 0)
398 m_tag_delete(m, p);
399 }
400
401 /* Find a tag, starting from a given position. */
402 struct m_tag *
403 m_tag_locate(struct mbuf *m, u_int32_t cookie, int type, struct m_tag *t)
404 {
405 struct m_tag *p;
406
407 KASSERT(m, ("m_tag_locate: null mbuf"));
408 if (t == NULL)
409 p = SLIST_FIRST(&m->m_pkthdr.tags);
410 else
411 p = SLIST_NEXT(t, m_tag_link);
412 while (p != NULL) {
413 if (p->m_tag_cookie == cookie && p->m_tag_id == type)
414 return p;
415 p = SLIST_NEXT(p, m_tag_link);
416 }
417 return NULL;
418 }
419
420 /* Copy a single tag. */
421 struct m_tag *
422 m_tag_copy(struct m_tag *t, int how)
423 {
424 struct m_tag *p;
425
426 KASSERT(t, ("m_tag_copy: null tag"));
427 p = m_tag_alloc(t->m_tag_cookie, t->m_tag_id, t->m_tag_len, how);
428 if (p == NULL)
429 return (NULL);
430 #ifdef MAC
431 /*
432 * XXXMAC: we should probably pass off the initialization, and
433 * copying here? can we hide that PACKET_TAG_MACLABEL is
434 * special from the mbuf code?
435 */
436 if (t->m_tag_id == PACKET_TAG_MACLABEL) {
437 if (mac_init_mbuf_tag(p, how) != 0) {
438 m_tag_free(p);
439 return (NULL);
440 }
441 mac_copy_mbuf_tag(t, p);
442 } else
443 #endif
444 bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */
445 return p;
446 }
447
448 /*
449 * Copy two tag chains. The destination mbuf (to) loses any attached
450 * tags even if the operation fails. This should not be a problem, as
451 * m_tag_copy_chain() is typically called with a newly-allocated
452 * destination mbuf.
453 */
454 int
455 m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int how)
456 {
457 struct m_tag *p, *t, *tprev = NULL;
458
459 KASSERT(to && from,
460 ("m_tag_copy_chain: null argument, to %p from %p", to, from));
461 m_tag_delete_chain(to, NULL);
462 SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
463 t = m_tag_copy(p, how);
464 if (t == NULL) {
465 m_tag_delete_chain(to, NULL);
466 return 0;
467 }
468 if (tprev == NULL)
469 SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
470 else
471 SLIST_INSERT_AFTER(tprev, t, m_tag_link);
472 tprev = t;
473 }
474 return 1;
475 }
476
477 /* Initialize tags on an mbuf. */
478 void
479 m_tag_init(struct mbuf *m)
480 {
481 SLIST_INIT(&m->m_pkthdr.tags);
482 }
483
484 /* Get first tag in chain. */
485 struct m_tag *
486 m_tag_first(struct mbuf *m)
487 {
488 return SLIST_FIRST(&m->m_pkthdr.tags);
489 }
490
491 /* Get next tag in chain. */
492 struct m_tag *
493 m_tag_next(struct mbuf *m, struct m_tag *t)
494 {
495 return SLIST_NEXT(t, m_tag_link);
496 }
Cache object: 1c87f7efe452a1d7b9d1fcffdf50de8e
|