xref: /openbsd-src/sys/kern/uipc_mbuf2.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: uipc_mbuf2.c,v 1.34 2011/04/05 11:48:28 blambert Exp $	*/
2 /*	$KAME: uipc_mbuf2.c,v 1.29 2001/02/14 13:42:10 itojun Exp $	*/
3 /*	$NetBSD: uipc_mbuf.c,v 1.40 1999/04/01 00:23:25 thorpej Exp $	*/
4 
5 /*
6  * Copyright (C) 1999 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
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/param.h>
66 #include <sys/systm.h>
67 #include <sys/proc.h>
68 #include <sys/malloc.h>
69 #include <sys/mbuf.h>
70 
71 /* can't call it m_dup(), as freebsd[34] uses m_dup() with different arg */
72 static struct mbuf *m_dup1(struct mbuf *, int, int, int);
73 
74 /*
75  * ensure that [off, off + len] is contiguous on the mbuf chain "m".
76  * packet chain before "off" is kept untouched.
77  * if offp == NULL, the target will start at <retval, 0> on resulting chain.
78  * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
79  *
80  * on error return (NULL return value), original "m" will be freed.
81  *
82  * XXX M_TRAILINGSPACE/M_LEADINGSPACE on shared cluster (sharedcluster)
83  */
84 struct mbuf *
85 m_pulldown(struct mbuf *m, int off, int len, int *offp)
86 {
87 	struct mbuf *n, *o;
88 	int hlen, tlen, olen;
89 	int sharedcluster;
90 
91 	/* check invalid arguments. */
92 	if (m == NULL)
93 		panic("m == NULL in m_pulldown()");
94 	if (len > MCLBYTES) {
95 		m_freem(m);
96 		return (NULL);	/* impossible */
97 	}
98 
99 	if ((n = m_getptr(m, off, &off)) == NULL) {
100 		m_freem(m);
101 		return (NULL);	/* mbuf chain too short */
102 	}
103 
104 	sharedcluster = M_READONLY(n);
105 
106 	/*
107 	 * the target data is on <n, off>.
108 	 * if we got enough data on the mbuf "n", we're done.
109 	 */
110 	if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster)
111 		goto ok;
112 
113 	/*
114 	 * when len <= n->m_len - off and off != 0, it is a special case.
115 	 * len bytes from <n, off> sits in single mbuf, but the caller does
116 	 * not like the starting position (off).
117 	 * chop the current mbuf into two pieces, set off to 0.
118 	 */
119 	if (len <= n->m_len - off) {
120 		struct mbuf *mlast;
121 
122 		o = m_dup1(n, off, n->m_len - off, M_DONTWAIT);
123 		if (o == NULL) {
124 			m_freem(m);
125 			return (NULL);	/* ENOBUFS */
126 		}
127 		for (mlast = o; mlast->m_next != NULL; mlast = mlast->m_next)
128 			;
129 		n->m_len = off;
130 		mlast->m_next = n->m_next;
131 		n->m_next = o;
132 		n = o;
133 		off = 0;
134 		goto ok;
135 	}
136 
137 	/*
138 	 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
139 	 * and construct contiguous mbuf with m_len == len.
140 	 * note that hlen + tlen == len, and tlen > 0.
141 	 */
142 	hlen = n->m_len - off;
143 	tlen = len - hlen;
144 
145 	/*
146 	 * ensure that we have enough trailing data on mbuf chain.
147 	 * if not, we can do nothing about the chain.
148 	 */
149 	olen = 0;
150 	for (o = n->m_next; o != NULL; o = o->m_next)
151 		olen += o->m_len;
152 	if (hlen + olen < len) {
153 		m_freem(m);
154 		return (NULL);	/* mbuf chain too short */
155 	}
156 
157 	/*
158 	 * easy cases first.
159 	 * we need to use m_copydata() to get data from <n->m_next, 0>.
160 	 */
161 	if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen &&
162 	    !sharedcluster) {
163 		m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
164 		n->m_len += tlen;
165 		m_adj(n->m_next, tlen);
166 		goto ok;
167 	}
168 	if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen &&
169 	    !sharedcluster && n->m_next->m_len >= tlen) {
170 		n->m_next->m_data -= hlen;
171 		n->m_next->m_len += hlen;
172 		bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
173 		n->m_len -= hlen;
174 		n = n->m_next;
175 		off = 0;
176 		goto ok;
177 	}
178 
179 	/*
180 	 * now, we need to do the hard way.  don't m_copy as there's no room
181 	 * on both ends.
182 	 */
183 	MGET(o, M_DONTWAIT, m->m_type);
184 	if (o && len > MLEN) {
185 		MCLGET(o, M_DONTWAIT);
186 		if ((o->m_flags & M_EXT) == 0) {
187 			m_free(o);
188 			o = NULL;
189 		}
190 	}
191 	if (!o) {
192 		m_freem(m);
193 		return (NULL);	/* ENOBUFS */
194 	}
195 	/* get hlen from <n, off> into <o, 0> */
196 	o->m_len = hlen;
197 	bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen);
198 	n->m_len -= hlen;
199 	/* get tlen from <n->m_next, 0> into <o, hlen> */
200 	m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
201 	o->m_len += tlen;
202 	m_adj(n->m_next, tlen);
203 	o->m_next = n->m_next;
204 	n->m_next = o;
205 	n = o;
206 	off = 0;
207 
208 ok:
209 	if (offp)
210 		*offp = off;
211 	return (n);
212 }
213 
214 static struct mbuf *
215 m_dup1(struct mbuf *m, int off, int len, int wait)
216 {
217 	struct mbuf *n;
218 	int l;
219 
220 	if (len > MCLBYTES)
221 		return (NULL);
222 	if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
223 		MGETHDR(n, wait, m->m_type);
224 		if (n == NULL)
225 			return (NULL);
226 		if (m_dup_pkthdr(n, m, wait)) {
227 			m_free(n);
228 			return (NULL);
229 		}
230 		l = MHLEN;
231 	} else {
232 		MGET(n, wait, m->m_type);
233 		l = MLEN;
234 	}
235 	if (n && len > l) {
236 		MCLGET(n, wait);
237 		if ((n->m_flags & M_EXT) == 0) {
238 			m_free(n);
239 			n = NULL;
240 		}
241 	}
242 	if (!n)
243 		return (NULL);
244 
245 	m_copydata(m, off, len, mtod(n, caddr_t));
246 	n->m_len = len;
247 
248 	return (n);
249 }
250 
251 /* Get a packet tag structure along with specified data following. */
252 struct m_tag *
253 m_tag_get(int type, int len, int wait)
254 {
255 	struct m_tag *t;
256 
257 	if (len < 0)
258 		return (NULL);
259 	t = malloc(sizeof(struct m_tag) + len, M_PACKET_TAGS, wait);
260 	if (t == NULL)
261 		return (NULL);
262 	t->m_tag_id = type;
263 	t->m_tag_len = len;
264 	return (t);
265 }
266 
267 /* Prepend a packet tag. */
268 void
269 m_tag_prepend(struct mbuf *m, struct m_tag *t)
270 {
271 	SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link);
272 	m->m_pkthdr.tagsset |= t->m_tag_id;
273 }
274 
275 /* Unlink and free a packet tag. */
276 void
277 m_tag_delete(struct mbuf *m, struct m_tag *t)
278 {
279 	u_int32_t	 tagsset = 0;
280 	struct m_tag	*p;
281 
282 	SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link);
283 	free(t, M_PACKET_TAGS);
284 
285 	SLIST_FOREACH(p, &m->m_pkthdr.tags, m_tag_link)
286 		tagsset |= p->m_tag_id;
287 	m->m_pkthdr.tagsset = tagsset;
288 
289 }
290 
291 /* Unlink and free a packet tag chain. */
292 void
293 m_tag_delete_chain(struct mbuf *m)
294 {
295 	struct m_tag *p;
296 
297 	while ((p = SLIST_FIRST(&m->m_pkthdr.tags)) != NULL) {
298 		SLIST_REMOVE_HEAD(&m->m_pkthdr.tags, m_tag_link);
299 		free(p, M_PACKET_TAGS);
300 	}
301 	m->m_pkthdr.tagsset = 0;
302 }
303 
304 /* Find a tag, starting from a given position. */
305 struct m_tag *
306 m_tag_find(struct mbuf *m, int type, struct m_tag *t)
307 {
308 	struct m_tag *p;
309 
310 	if (!(m->m_pkthdr.tagsset & type))
311 		return (NULL);
312 
313 	if (t == NULL)
314 		p = SLIST_FIRST(&m->m_pkthdr.tags);
315 	else
316 		p = SLIST_NEXT(t, m_tag_link);
317 	while (p != NULL) {
318 		if (p->m_tag_id == type)
319 			return (p);
320 		p = SLIST_NEXT(p, m_tag_link);
321 	}
322 	return (NULL);
323 }
324 
325 /* Copy a single tag. */
326 struct m_tag *
327 m_tag_copy(struct m_tag *t, int wait)
328 {
329 	struct m_tag *p;
330 
331 	p = m_tag_get(t->m_tag_id, t->m_tag_len, wait);
332 	if (p == NULL)
333 		return (NULL);
334 	bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */
335 	return (p);
336 }
337 
338 /*
339  * Copy two tag chains. The destination mbuf (to) loses any attached
340  * tags even if the operation fails. This should not be a problem, as
341  * m_tag_copy_chain() is typically called with a newly-allocated
342  * destination mbuf.
343  */
344 int
345 m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int wait)
346 {
347 	struct m_tag *p, *t, *tprev = NULL;
348 
349 	m_tag_delete_chain(to);
350 	SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) {
351 		t = m_tag_copy(p, wait);
352 		if (t == NULL) {
353 			m_tag_delete_chain(to);
354 			return (ENOMEM);
355 		}
356 		if (tprev == NULL)
357 			SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link);
358 		else
359 			SLIST_INSERT_AFTER(tprev, t, m_tag_link);
360 		tprev = t;
361 		to->m_pkthdr.tagsset |= t->m_tag_id;
362 	}
363 	return (0);
364 }
365 
366 /* Initialize tags on an mbuf. */
367 void
368 m_tag_init(struct mbuf *m)
369 {
370 	SLIST_INIT(&m->m_pkthdr.tags);
371 }
372 
373 /* Get first tag in chain. */
374 struct m_tag *
375 m_tag_first(struct mbuf *m)
376 {
377 	return (SLIST_FIRST(&m->m_pkthdr.tags));
378 }
379 
380 /* Get next tag in chain. */
381 struct m_tag *
382 m_tag_next(struct mbuf *m, struct m_tag *t)
383 {
384 	return (SLIST_NEXT(t, m_tag_link));
385 }
386