1 /* $OpenBSD: uipc_mbuf2.c,v 1.47 2024/08/29 16:42:30 bluhm 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/malloc.h>
68 #include <sys/pool.h>
69 #include <sys/percpu.h>
70 #include <sys/mbuf.h>
71
72 extern struct pool mtagpool;
73
74 /* can't call it m_dup(), as freebsd[34] uses m_dup() with different arg */
75 static struct mbuf *m_dup1(struct mbuf *, int, int, int);
76
77 /*
78 * ensure that [off, off + len] is contiguous on the mbuf chain "m".
79 * packet chain before "off" is kept untouched.
80 * if offp == NULL, the target will start at <retval, 0> on resulting chain.
81 * if offp != NULL, the target will start at <retval, *offp> on resulting chain.
82 *
83 * on error return (NULL return value), original "m" will be freed.
84 *
85 * XXX m_trailingspace/m_leadingspace on shared cluster (sharedcluster)
86 */
87 struct mbuf *
m_pulldown(struct mbuf * m,int off,int len,int * offp)88 m_pulldown(struct mbuf *m, int off, int len, int *offp)
89 {
90 struct mbuf *n, *o;
91 int hlen, tlen, olen;
92 int sharedcluster;
93
94 /* check invalid arguments. */
95 if (m == NULL)
96 panic("m == NULL in m_pulldown()");
97
98 if ((n = m_getptr(m, off, &off)) == NULL) {
99 m_freem(m);
100 return (NULL); /* mbuf chain too short */
101 }
102
103 sharedcluster = M_READONLY(n);
104
105 /*
106 * the target data is on <n, off>.
107 * if we got enough data on the mbuf "n", we're done.
108 */
109 if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster)
110 goto ok;
111
112 /*
113 * when len <= n->m_len - off and off != 0, it is a special case.
114 * len bytes from <n, off> sits in single mbuf, but the caller does
115 * not like the starting position (off).
116 * chop the current mbuf into two pieces, set off to 0.
117 */
118 if (len <= n->m_len - off) {
119 struct mbuf *mlast;
120
121 counters_inc(mbstat, MBSTAT_PULLDOWN_ALLOC);
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 counters_inc(mbstat, MBSTAT_PULLDOWN_COPY);
164 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len);
165 n->m_len += tlen;
166 m_adj(n->m_next, tlen);
167 goto ok;
168 }
169 if ((off == 0 || offp) && m_leadingspace(n->m_next) >= hlen &&
170 !sharedcluster && n->m_next->m_len >= tlen) {
171 n->m_next->m_data -= hlen;
172 n->m_next->m_len += hlen;
173 counters_inc(mbstat, MBSTAT_PULLDOWN_COPY);
174 memcpy(mtod(n->m_next, caddr_t), mtod(n, caddr_t) + off, hlen);
175 n->m_len -= hlen;
176 n = n->m_next;
177 off = 0;
178 goto ok;
179 }
180
181 /*
182 * now, we need to do the hard way. don't m_copym as there's no room
183 * on both ends.
184 */
185 if (len > MAXMCLBYTES) {
186 m_freem(m);
187 return (NULL);
188 }
189 counters_inc(mbstat, MBSTAT_PULLDOWN_ALLOC);
190 MGET(o, M_DONTWAIT, m->m_type);
191 if (o && len > MLEN) {
192 MCLGETL(o, M_DONTWAIT, len);
193 if ((o->m_flags & M_EXT) == 0) {
194 m_free(o);
195 o = NULL;
196 }
197 }
198 if (!o) {
199 m_freem(m);
200 return (NULL); /* ENOBUFS */
201 }
202 /* get hlen from <n, off> into <o, 0> */
203 o->m_len = hlen;
204 memcpy(mtod(o, caddr_t), mtod(n, caddr_t) + off, hlen);
205 n->m_len -= hlen;
206 /* get tlen from <n->m_next, 0> into <o, hlen> */
207 m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len);
208 o->m_len += tlen;
209 m_adj(n->m_next, tlen);
210 o->m_next = n->m_next;
211 n->m_next = o;
212 n = o;
213 off = 0;
214
215 ok:
216 if (offp)
217 *offp = off;
218 return (n);
219 }
220
221 static struct mbuf *
m_dup1(struct mbuf * m,int off,int len,int wait)222 m_dup1(struct mbuf *m, int off, int len, int wait)
223 {
224 struct mbuf *n;
225 int l;
226
227 if (len > MAXMCLBYTES)
228 return (NULL);
229 if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
230 MGETHDR(n, wait, m->m_type);
231 if (n == NULL)
232 return (NULL);
233 if (m_dup_pkthdr(n, m, wait)) {
234 m_free(n);
235 return (NULL);
236 }
237 l = MHLEN;
238 } else {
239 MGET(n, wait, m->m_type);
240 l = MLEN;
241 }
242 if (n && len > l) {
243 MCLGETL(n, wait, len);
244 if ((n->m_flags & M_EXT) == 0) {
245 m_free(n);
246 n = NULL;
247 }
248 }
249 if (!n)
250 return (NULL);
251
252 m_copydata(m, off, len, mtod(n, caddr_t));
253 n->m_len = len;
254
255 return (n);
256 }
257
258 /* Get a packet tag structure along with specified data following. */
259 struct m_tag *
m_tag_get(int type,int len,int wait)260 m_tag_get(int type, int len, int wait)
261 {
262 struct m_tag *t;
263
264 if (len < 0)
265 return (NULL);
266 if (len > PACKET_TAG_MAXSIZE)
267 panic("requested tag size for pool %#x is too big", type);
268 t = pool_get(&mtagpool, wait == M_WAITOK ? PR_WAITOK : PR_NOWAIT);
269 if (t == NULL)
270 return (NULL);
271 t->m_tag_id = type;
272 t->m_tag_len = len;
273 return (t);
274 }
275
276 /* Prepend a packet tag. */
277 void
m_tag_prepend(struct mbuf * m,struct m_tag * t)278 m_tag_prepend(struct mbuf *m, struct m_tag *t)
279 {
280 SLIST_INSERT_HEAD(&m->m_pkthdr.ph_tags, t, m_tag_link);
281 m->m_pkthdr.ph_tagsset |= t->m_tag_id;
282 }
283
284 /* Unlink and free a packet tag. */
285 void
m_tag_delete(struct mbuf * m,struct m_tag * t)286 m_tag_delete(struct mbuf *m, struct m_tag *t)
287 {
288 u_int32_t ph_tagsset = 0;
289 struct m_tag *p;
290
291 SLIST_REMOVE(&m->m_pkthdr.ph_tags, t, m_tag, m_tag_link);
292 pool_put(&mtagpool, t);
293
294 SLIST_FOREACH(p, &m->m_pkthdr.ph_tags, m_tag_link)
295 ph_tagsset |= p->m_tag_id;
296 m->m_pkthdr.ph_tagsset = ph_tagsset;
297
298 }
299
300 /* Unlink and free a packet tag chain. */
301 void
m_tag_delete_chain(struct mbuf * m)302 m_tag_delete_chain(struct mbuf *m)
303 {
304 struct m_tag *p;
305
306 while ((p = SLIST_FIRST(&m->m_pkthdr.ph_tags)) != NULL) {
307 SLIST_REMOVE_HEAD(&m->m_pkthdr.ph_tags, m_tag_link);
308 pool_put(&mtagpool, p);
309 }
310 m->m_pkthdr.ph_tagsset = 0;
311 }
312
313 /* Find a tag, starting from a given position. */
314 struct m_tag *
m_tag_find(struct mbuf * m,int type,struct m_tag * t)315 m_tag_find(struct mbuf *m, int type, struct m_tag *t)
316 {
317 struct m_tag *p;
318
319 if (!(m->m_pkthdr.ph_tagsset & type))
320 return (NULL);
321
322 if (t == NULL)
323 p = SLIST_FIRST(&m->m_pkthdr.ph_tags);
324 else
325 p = SLIST_NEXT(t, m_tag_link);
326 while (p != NULL) {
327 if (p->m_tag_id == type)
328 return (p);
329 p = SLIST_NEXT(p, m_tag_link);
330 }
331 return (NULL);
332 }
333
334 /* Copy a single tag. */
335 struct m_tag *
m_tag_copy(struct m_tag * t,int wait)336 m_tag_copy(struct m_tag *t, int wait)
337 {
338 struct m_tag *p;
339
340 p = m_tag_get(t->m_tag_id, t->m_tag_len, wait);
341 if (p == NULL)
342 return (NULL);
343 memcpy(p + 1, t + 1, t->m_tag_len); /* Copy the data */
344 return (p);
345 }
346
347 /*
348 * Copy two tag chains. The destination mbuf (to) loses any attached
349 * tags even if the operation fails. This should not be a problem, as
350 * m_tag_copy_chain() is typically called with a newly-allocated
351 * destination mbuf.
352 */
353 int
m_tag_copy_chain(struct mbuf * to,struct mbuf * from,int wait)354 m_tag_copy_chain(struct mbuf *to, struct mbuf *from, int wait)
355 {
356 struct m_tag *p, *t, *tprev = NULL;
357
358 m_tag_delete_chain(to);
359 SLIST_FOREACH(p, &from->m_pkthdr.ph_tags, m_tag_link) {
360 t = m_tag_copy(p, wait);
361 if (t == NULL) {
362 m_tag_delete_chain(to);
363 return (ENOBUFS);
364 }
365 if (tprev == NULL)
366 SLIST_INSERT_HEAD(&to->m_pkthdr.ph_tags, t, m_tag_link);
367 else
368 SLIST_INSERT_AFTER(tprev, t, m_tag_link);
369 tprev = t;
370 to->m_pkthdr.ph_tagsset |= t->m_tag_id;
371 }
372 return (0);
373 }
374
375 /* Initialize tags on an mbuf. */
376 void
m_tag_init(struct mbuf * m)377 m_tag_init(struct mbuf *m)
378 {
379 SLIST_INIT(&m->m_pkthdr.ph_tags);
380 }
381
382 /* Get first tag in chain. */
383 struct m_tag *
m_tag_first(struct mbuf * m)384 m_tag_first(struct mbuf *m)
385 {
386 return (SLIST_FIRST(&m->m_pkthdr.ph_tags));
387 }
388
389 /* Get next tag in chain. */
390 struct m_tag *
m_tag_next(struct mbuf * m,struct m_tag * t)391 m_tag_next(struct mbuf *m, struct m_tag *t)
392 {
393 return (SLIST_NEXT(t, m_tag_link));
394 }
395