xref: /openbsd-src/sys/kern/uipc_mbuf2.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: uipc_mbuf2.c,v 1.35 2011/11/30 10:26:56 dlg 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 
95 	if ((n = m_getptr(m, off, &off)) == NULL) {
96 		m_freem(m);
97 		return (NULL);	/* mbuf chain too short */
98 	}
99 
100 	sharedcluster = M_READONLY(n);
101 
102 	/*
103 	 * the target data is on <n, off>.
104 	 * if we got enough data on the mbuf "n", we're done.
105 	 */
106 	if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster)
107 		goto ok;
108 
109 	/*
110 	 * when len <= n->m_len - off and off != 0, it is a special case.
111 	 * len bytes from <n, off> sits in single mbuf, but the caller does
112 	 * not like the starting position (off).
113 	 * chop the current mbuf into two pieces, set off to 0.
114 	 */
115 	if (len <= n->m_len - off) {
116 		struct mbuf *mlast;
117 
118 		o = m_dup1(n, off, n->m_len - off, M_DONTWAIT);
119 		if (o == NULL) {
120 			m_freem(m);
121 			return (NULL);	/* ENOBUFS */
122 		}
123 		for (mlast = o; mlast->m_next != NULL; mlast = mlast->m_next)
124 			;
125 		n->m_len = off;
126 		mlast->m_next = n->m_next;
127 		n->m_next = o;
128 		n = o;
129 		off = 0;
130 		goto ok;
131 	}
132 
133 	/*
134 	 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>,
135 	 * and construct contiguous mbuf with m_len == len.
136 	 * note that hlen + tlen == len, and tlen > 0.
137 	 */
138 	hlen = n->m_len - off;
139 	tlen = len - hlen;
140 
141 	/*
142 	 * ensure that we have enough trailing data on mbuf chain.
143 	 * if not, we can do nothing about the chain.
144 	 */
145 	olen = 0;
146 	for (o = n->m_next; o != NULL; o = o->m_next)
147 		olen += o->m_len;
148 	if (hlen + olen < len) {
149 		m_freem(m);
150 		return (NULL);	/* mbuf chain too short */
151 	}
152 
153 	/*
154 	 * easy cases first.
155 	 * we need to use m_copydata() to get data from <n->m_next, 0>.
156 	 */
157 	if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen &&
158 	    !sharedcluster) {
159 		m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
160 		n->m_len += tlen;
161 		m_adj(n->m_next, tlen);
162 		goto ok;
163 	}
164 	if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen &&
165 	    !sharedcluster && n->m_next->m_len >= tlen) {
166 		n->m_next->m_data -= hlen;
167 		n->m_next->m_len += hlen;
168 		bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen);
169 		n->m_len -= hlen;
170 		n = n->m_next;
171 		off = 0;
172 		goto ok;
173 	}
174 
175 	/*
176 	 * now, we need to do the hard way.  don't m_copy as there's no room
177 	 * on both ends.
178 	 */
179 	if (len > MAXMCLBYTES) {
180 		m_freem(m);
181 		return (NULL);
182 	}
183 	MGET(o, M_DONTWAIT, m->m_type);
184 	if (o && len > MLEN) {
185 		MCLGETI(o, M_DONTWAIT, NULL, len);
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 > MAXMCLBYTES)
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 		MCLGETI(n, wait, NULL, len);
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