1 /* $OpenBSD: uipc_mbuf2.c,v 1.21 2003/06/02 23:28:06 millert 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(m, off, len, offp) 86 struct mbuf *m; 87 int off, len; 88 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 if (len > MCLBYTES) { 98 m_freem(m); 99 return (NULL); /* impossible */ 100 } 101 102 n = m; 103 while (n != NULL && off > 0) { 104 if (n->m_len > off) 105 break; 106 off -= n->m_len; 107 n = n->m_next; 108 } 109 /* be sure to point non-empty mbuf */ 110 while (n != NULL && n->m_len == 0) 111 n = n->m_next; 112 if (!n) { 113 m_freem(m); 114 return (NULL); /* mbuf chain too short */ 115 } 116 117 sharedcluster = M_READONLY(n); 118 119 /* 120 * the target data is on <n, off>. 121 * if we got enough data on the mbuf "n", we're done. 122 */ 123 if ((off == 0 || offp) && len <= n->m_len - off && !sharedcluster) 124 goto ok; 125 126 /* 127 * when len <= n->m_len - off and off != 0, it is a special case. 128 * len bytes from <n, off> sits in single mbuf, but the caller does 129 * not like the starting position (off). 130 * chop the current mbuf into two pieces, set off to 0. 131 */ 132 if (len <= n->m_len - off) { 133 o = m_dup1(n, off, n->m_len - off, M_DONTWAIT); 134 if (o == NULL) { 135 m_freem(m); 136 return (NULL); /* ENOBUFS */ 137 } 138 n->m_len = off; 139 o->m_next = n->m_next; 140 n->m_next = o; 141 n = n->m_next; 142 off = 0; 143 goto ok; 144 } 145 146 /* 147 * we need to take hlen from <n, off> and tlen from <n->m_next, 0>, 148 * and construct contiguous mbuf with m_len == len. 149 * note that hlen + tlen == len, and tlen > 0. 150 */ 151 hlen = n->m_len - off; 152 tlen = len - hlen; 153 154 /* 155 * ensure that we have enough trailing data on mbuf chain. 156 * if not, we can do nothing about the chain. 157 */ 158 olen = 0; 159 for (o = n->m_next; o != NULL; o = o->m_next) 160 olen += o->m_len; 161 if (hlen + olen < len) { 162 m_freem(m); 163 return (NULL); /* mbuf chain too short */ 164 } 165 166 /* 167 * easy cases first. 168 * we need to use m_copydata() to get data from <n->m_next, 0>. 169 */ 170 if ((off == 0 || offp) && M_TRAILINGSPACE(n) >= tlen && 171 !sharedcluster) { 172 m_copydata(n->m_next, 0, tlen, mtod(n, caddr_t) + n->m_len); 173 n->m_len += tlen; 174 m_adj(n->m_next, tlen); 175 goto ok; 176 } 177 if ((off == 0 || offp) && M_LEADINGSPACE(n->m_next) >= hlen && 178 !sharedcluster) { 179 n->m_next->m_data -= hlen; 180 n->m_next->m_len += hlen; 181 bcopy(mtod(n, caddr_t) + off, mtod(n->m_next, caddr_t), hlen); 182 n->m_len -= hlen; 183 n = n->m_next; 184 off = 0; 185 goto ok; 186 } 187 188 /* 189 * now, we need to do the hard way. don't m_copy as there's no room 190 * on both end. 191 */ 192 MGET(o, M_DONTWAIT, m->m_type); 193 if (o && len > MLEN) { 194 MCLGET(o, M_DONTWAIT); 195 if ((o->m_flags & M_EXT) == 0) { 196 m_free(o); 197 o = NULL; 198 } 199 } 200 if (!o) { 201 m_freem(m); 202 return (NULL); /* ENOBUFS */ 203 } 204 /* get hlen from <n, off> into <o, 0> */ 205 o->m_len = hlen; 206 bcopy(mtod(n, caddr_t) + off, mtod(o, caddr_t), hlen); 207 n->m_len -= hlen; 208 /* get tlen from <n->m_next, 0> into <o, hlen> */ 209 m_copydata(n->m_next, 0, tlen, mtod(o, caddr_t) + o->m_len); 210 o->m_len += tlen; 211 m_adj(n->m_next, tlen); 212 o->m_next = n->m_next; 213 n->m_next = o; 214 n = o; 215 off = 0; 216 217 ok: 218 if (offp) 219 *offp = off; 220 return (n); 221 } 222 223 static struct mbuf * 224 m_dup1(m, off, len, wait) 225 struct mbuf *m; 226 int off; 227 int len; 228 int wait; 229 { 230 struct mbuf *n; 231 int l; 232 int copyhdr; 233 234 if (len > MCLBYTES) 235 return (NULL); 236 if (off == 0 && (m->m_flags & M_PKTHDR) != 0) { 237 copyhdr = 1; 238 MGETHDR(n, wait, m->m_type); 239 l = MHLEN; 240 } else { 241 copyhdr = 0; 242 MGET(n, wait, m->m_type); 243 l = MLEN; 244 } 245 if (n && len > l) { 246 MCLGET(n, wait); 247 if ((n->m_flags & M_EXT) == 0) { 248 m_free(n); 249 n = NULL; 250 } 251 } 252 if (!n) 253 return (NULL); 254 255 if (copyhdr) 256 M_DUP_PKTHDR(n, m); 257 m_copydata(m, off, len, mtod(n, caddr_t)); 258 n->m_len = len; 259 260 return (n); 261 } 262 263 /* Get a packet tag structure along with specified data following. */ 264 struct m_tag * 265 m_tag_get(type, len, wait) 266 int type; 267 int len; 268 int wait; 269 { 270 struct m_tag *t; 271 272 if (len < 0) 273 return (NULL); 274 t = malloc(len + sizeof(struct m_tag), M_PACKET_TAGS, wait); 275 if (t == NULL) 276 return (NULL); 277 t->m_tag_id = type; 278 t->m_tag_len = len; 279 return (t); 280 } 281 282 /* Free a packet tag. */ 283 void 284 m_tag_free(t) 285 struct m_tag *t; 286 { 287 free(t, M_PACKET_TAGS); 288 } 289 290 /* Prepend a packet tag. */ 291 void 292 m_tag_prepend(m, t) 293 struct mbuf *m; 294 struct m_tag *t; 295 { 296 SLIST_INSERT_HEAD(&m->m_pkthdr.tags, t, m_tag_link); 297 } 298 299 /* Unlink a packet tag. */ 300 void 301 m_tag_unlink(m, t) 302 struct mbuf *m; 303 struct m_tag *t; 304 { 305 SLIST_REMOVE(&m->m_pkthdr.tags, t, m_tag, m_tag_link); 306 } 307 308 /* Unlink and free a packet tag. */ 309 void 310 m_tag_delete(m, t) 311 struct mbuf *m; 312 struct m_tag *t; 313 { 314 m_tag_unlink(m, t); 315 m_tag_free(t); 316 } 317 318 /* Unlink and free a packet tag chain, starting from given tag. */ 319 void 320 m_tag_delete_chain(m, t) 321 struct mbuf *m; 322 struct m_tag *t; 323 { 324 struct m_tag *p, *q; 325 326 if (t != NULL) 327 p = t; 328 else 329 p = SLIST_FIRST(&m->m_pkthdr.tags); 330 if (p == NULL) 331 return; 332 while ((q = SLIST_NEXT(p, m_tag_link)) != NULL) 333 m_tag_delete(m, q); 334 m_tag_delete(m, p); 335 } 336 337 /* Find a tag, starting from a given position. */ 338 struct m_tag * 339 m_tag_find(m, type, t) 340 struct mbuf *m; 341 int type; 342 struct m_tag *t; 343 { 344 struct m_tag *p; 345 346 if (t == NULL) 347 p = SLIST_FIRST(&m->m_pkthdr.tags); 348 else 349 p = SLIST_NEXT(t, m_tag_link); 350 while (p != NULL) { 351 if (p->m_tag_id == type) 352 return (p); 353 p = SLIST_NEXT(p, m_tag_link); 354 } 355 return (NULL); 356 } 357 358 /* Copy a single tag. */ 359 struct m_tag * 360 m_tag_copy(t) 361 struct m_tag *t; 362 { 363 struct m_tag *p; 364 365 p = m_tag_get(t->m_tag_id, t->m_tag_len, M_NOWAIT); 366 if (p == NULL) 367 return (NULL); 368 bcopy(t + 1, p + 1, t->m_tag_len); /* Copy the data */ 369 return (p); 370 } 371 372 /* 373 * Copy two tag chains. The destination mbuf (to) loses any attached 374 * tags even if the operation fails. This should not be a problem, as 375 * m_tag_copy_chain() is typically called with a newly-allocated 376 * destination mbuf. 377 */ 378 int 379 m_tag_copy_chain(to, from) 380 struct mbuf *to; 381 struct mbuf *from; 382 { 383 struct m_tag *p, *t, *tprev = NULL; 384 385 m_tag_delete_chain(to, NULL); 386 SLIST_FOREACH(p, &from->m_pkthdr.tags, m_tag_link) { 387 t = m_tag_copy(p); 388 if (t == NULL) { 389 m_tag_delete_chain(to, NULL); 390 return (0); 391 } 392 if (tprev == NULL) 393 SLIST_INSERT_HEAD(&to->m_pkthdr.tags, t, m_tag_link); 394 else 395 SLIST_INSERT_AFTER(tprev, t, m_tag_link); 396 tprev = t; 397 } 398 return (1); 399 } 400 401 /* Initialize tags on an mbuf. */ 402 void 403 m_tag_init(m) 404 struct mbuf *m; 405 { 406 SLIST_INIT(&m->m_pkthdr.tags); 407 } 408 409 /* Get first tag in chain. */ 410 struct m_tag * 411 m_tag_first(m) 412 struct mbuf *m; 413 { 414 return (SLIST_FIRST(&m->m_pkthdr.tags)); 415 } 416 417 /* Get next tag in chain. */ 418 struct m_tag * 419 m_tag_next(m, t) 420 struct mbuf *m; 421 struct m_tag *t; 422 { 423 return (SLIST_NEXT(t, m_tag_link)); 424 } 425