1 /*- 2 * Copyright (c) 2000, 2001 Boris Popov 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the author nor the names of any co-contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/endian.h> 36 #include <sys/errno.h> 37 #include <sys/mbuf.h> 38 #include <sys/module.h> 39 #include <sys/uio.h> 40 41 #include <sys/mchain.h> 42 43 MODULE_VERSION(libmchain, 1); 44 45 #define MBERROR(format, ...) printf("%s(%d): "format, __func__ , \ 46 __LINE__ , ## __VA_ARGS__) 47 48 #define MBPANIC(format, ...) printf("%s(%d): "format, __func__ , \ 49 __LINE__ , ## __VA_ARGS__) 50 51 /* 52 * Various helper functions 53 */ 54 int 55 mb_init(struct mbchain *mbp) 56 { 57 struct mbuf *m; 58 59 m = m_gethdr(M_WAIT, MT_DATA); 60 m->m_len = 0; 61 mb_initm(mbp, m); 62 return 0; 63 } 64 65 void 66 mb_initm(struct mbchain *mbp, struct mbuf *m) 67 { 68 bzero(mbp, sizeof(*mbp)); 69 mbp->mb_top = mbp->mb_cur = m; 70 mbp->mb_mleft = M_TRAILINGSPACE(m); 71 } 72 73 void 74 mb_done(struct mbchain *mbp) 75 { 76 if (mbp->mb_top) { 77 m_freem(mbp->mb_top); 78 mbp->mb_top = NULL; 79 } 80 } 81 82 struct mbuf * 83 mb_detach(struct mbchain *mbp) 84 { 85 struct mbuf *m; 86 87 m = mbp->mb_top; 88 mbp->mb_top = NULL; 89 return m; 90 } 91 92 int 93 mb_fixhdr(struct mbchain *mbp) 94 { 95 return mbp->mb_top->m_pkthdr.len = m_fixhdr(mbp->mb_top); 96 } 97 98 /* 99 * Check if object of size 'size' fit to the current position and 100 * allocate new mbuf if not. Advance pointers and increase length of mbuf(s). 101 * Return pointer to the object placeholder or NULL if any error occured. 102 * Note: size should be <= MLEN 103 */ 104 caddr_t 105 mb_reserve(struct mbchain *mbp, int size) 106 { 107 struct mbuf *m, *mn; 108 caddr_t bpos; 109 110 if (size > MLEN) 111 panic("mb_reserve: size = %d\n", size); 112 m = mbp->mb_cur; 113 if (mbp->mb_mleft < size) { 114 mn = m_get(M_WAIT, MT_DATA); 115 mbp->mb_cur = m->m_next = mn; 116 m = mn; 117 m->m_len = 0; 118 mbp->mb_mleft = M_TRAILINGSPACE(m); 119 } 120 mbp->mb_mleft -= size; 121 mbp->mb_count += size; 122 bpos = mtod(m, caddr_t) + m->m_len; 123 m->m_len += size; 124 return bpos; 125 } 126 127 int 128 mb_put_uint8(struct mbchain *mbp, uint8_t x) 129 { 130 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 131 } 132 133 int 134 mb_put_uint16be(struct mbchain *mbp, uint16_t x) 135 { 136 x = htobe16(x); 137 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 138 } 139 140 int 141 mb_put_uint16le(struct mbchain *mbp, uint16_t x) 142 { 143 x = htole16(x); 144 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 145 } 146 147 int 148 mb_put_uint32be(struct mbchain *mbp, uint32_t x) 149 { 150 x = htobe32(x); 151 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 152 } 153 154 int 155 mb_put_uint32le(struct mbchain *mbp, uint32_t x) 156 { 157 x = htole32(x); 158 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 159 } 160 161 int 162 mb_put_int64be(struct mbchain *mbp, int64_t x) 163 { 164 x = htobe64(x); 165 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 166 } 167 168 int 169 mb_put_int64le(struct mbchain *mbp, int64_t x) 170 { 171 x = htole64(x); 172 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 173 } 174 175 int 176 mb_put_mem(struct mbchain *mbp, c_caddr_t source, int size, int type) 177 { 178 struct mbuf *m; 179 caddr_t dst; 180 c_caddr_t src; 181 int cplen, error, mleft, count; 182 size_t srclen, dstlen; 183 184 m = mbp->mb_cur; 185 mleft = mbp->mb_mleft; 186 187 while (size > 0) { 188 if (mleft == 0) { 189 if (m->m_next == NULL) 190 m = m_getm(m, size, M_WAIT, MT_DATA); 191 else 192 m = m->m_next; 193 mleft = M_TRAILINGSPACE(m); 194 continue; 195 } 196 cplen = mleft > size ? size : mleft; 197 srclen = dstlen = cplen; 198 dst = mtod(m, caddr_t) + m->m_len; 199 switch (type) { 200 case MB_MCUSTOM: 201 srclen = size; 202 dstlen = mleft; 203 error = mbp->mb_copy(mbp, source, dst, &srclen, &dstlen); 204 if (error) 205 return error; 206 break; 207 case MB_MINLINE: 208 for (src = source, count = cplen; count; count--) 209 *dst++ = *src++; 210 break; 211 case MB_MSYSTEM: 212 bcopy(source, dst, cplen); 213 break; 214 case MB_MUSER: 215 error = copyin(source, dst, cplen); 216 if (error) 217 return error; 218 break; 219 case MB_MZERO: 220 bzero(dst, cplen); 221 break; 222 } 223 size -= srclen; 224 source += srclen; 225 m->m_len += dstlen; 226 mleft -= dstlen; 227 mbp->mb_count += dstlen; 228 } 229 mbp->mb_cur = m; 230 mbp->mb_mleft = mleft; 231 return 0; 232 } 233 234 int 235 mb_put_mbuf(struct mbchain *mbp, struct mbuf *m) 236 { 237 mbp->mb_cur->m_next = m; 238 while (m) { 239 mbp->mb_count += m->m_len; 240 if (m->m_next == NULL) 241 break; 242 m = m->m_next; 243 } 244 mbp->mb_mleft = M_TRAILINGSPACE(m); 245 mbp->mb_cur = m; 246 return 0; 247 } 248 249 /* 250 * copies a uio scatter/gather list to an mbuf chain. 251 */ 252 int 253 mb_put_uio(struct mbchain *mbp, struct uio *uiop, int size) 254 { 255 long left; 256 int mtype, error; 257 258 mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER; 259 260 while (size > 0 && uiop->uio_resid) { 261 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 262 return EFBIG; 263 left = uiop->uio_iov->iov_len; 264 if (left == 0) { 265 uiop->uio_iov++; 266 uiop->uio_iovcnt--; 267 continue; 268 } 269 if (left > size) 270 left = size; 271 error = mb_put_mem(mbp, uiop->uio_iov->iov_base, left, mtype); 272 if (error) 273 return error; 274 uiop->uio_offset += left; 275 uiop->uio_resid -= left; 276 uiop->uio_iov->iov_base = 277 (char *)uiop->uio_iov->iov_base + left; 278 uiop->uio_iov->iov_len -= left; 279 size -= left; 280 } 281 return 0; 282 } 283 284 /* 285 * Routines for fetching data from an mbuf chain 286 */ 287 int 288 md_init(struct mdchain *mdp) 289 { 290 struct mbuf *m; 291 292 m = m_gethdr(M_WAIT, MT_DATA); 293 m->m_len = 0; 294 md_initm(mdp, m); 295 return 0; 296 } 297 298 void 299 md_initm(struct mdchain *mdp, struct mbuf *m) 300 { 301 bzero(mdp, sizeof(*mdp)); 302 mdp->md_top = mdp->md_cur = m; 303 mdp->md_pos = mtod(m, u_char*); 304 } 305 306 void 307 md_done(struct mdchain *mdp) 308 { 309 if (mdp->md_top) { 310 m_freem(mdp->md_top); 311 mdp->md_top = NULL; 312 } 313 } 314 315 /* 316 * Append a separate mbuf chain. It is caller responsibility to prevent 317 * multiple calls to fetch/record routines. 318 */ 319 void 320 md_append_record(struct mdchain *mdp, struct mbuf *top) 321 { 322 struct mbuf *m; 323 324 if (mdp->md_top == NULL) { 325 md_initm(mdp, top); 326 return; 327 } 328 m = mdp->md_top; 329 while (m->m_nextpkt) 330 m = m->m_nextpkt; 331 m->m_nextpkt = top; 332 top->m_nextpkt = NULL; 333 return; 334 } 335 336 /* 337 * Put next record in place of existing 338 */ 339 int 340 md_next_record(struct mdchain *mdp) 341 { 342 struct mbuf *m; 343 344 if (mdp->md_top == NULL) 345 return ENOENT; 346 m = mdp->md_top->m_nextpkt; 347 md_done(mdp); 348 if (m == NULL) 349 return ENOENT; 350 md_initm(mdp, m); 351 return 0; 352 } 353 354 int 355 md_get_uint8(struct mdchain *mdp, uint8_t *x) 356 { 357 return md_get_mem(mdp, x, 1, MB_MINLINE); 358 } 359 360 int 361 md_get_uint16(struct mdchain *mdp, uint16_t *x) 362 { 363 return md_get_mem(mdp, (caddr_t)x, 2, MB_MINLINE); 364 } 365 366 int 367 md_get_uint16le(struct mdchain *mdp, uint16_t *x) 368 { 369 uint16_t v; 370 int error = md_get_uint16(mdp, &v); 371 372 if (x != NULL) 373 *x = le16toh(v); 374 return error; 375 } 376 377 int 378 md_get_uint16be(struct mdchain *mdp, uint16_t *x) 379 { 380 uint16_t v; 381 int error = md_get_uint16(mdp, &v); 382 383 if (x != NULL) 384 *x = be16toh(v); 385 return error; 386 } 387 388 int 389 md_get_uint32(struct mdchain *mdp, uint32_t *x) 390 { 391 return md_get_mem(mdp, (caddr_t)x, 4, MB_MINLINE); 392 } 393 394 int 395 md_get_uint32be(struct mdchain *mdp, uint32_t *x) 396 { 397 uint32_t v; 398 int error; 399 400 error = md_get_uint32(mdp, &v); 401 if (x != NULL) 402 *x = be32toh(v); 403 return error; 404 } 405 406 int 407 md_get_uint32le(struct mdchain *mdp, uint32_t *x) 408 { 409 uint32_t v; 410 int error; 411 412 error = md_get_uint32(mdp, &v); 413 if (x != NULL) 414 *x = le32toh(v); 415 return error; 416 } 417 418 int 419 md_get_int64(struct mdchain *mdp, int64_t *x) 420 { 421 return md_get_mem(mdp, (caddr_t)x, 8, MB_MINLINE); 422 } 423 424 int 425 md_get_int64be(struct mdchain *mdp, int64_t *x) 426 { 427 int64_t v; 428 int error; 429 430 error = md_get_int64(mdp, &v); 431 if (x != NULL) 432 *x = be64toh(v); 433 return error; 434 } 435 436 int 437 md_get_int64le(struct mdchain *mdp, int64_t *x) 438 { 439 int64_t v; 440 int error; 441 442 error = md_get_int64(mdp, &v); 443 if (x != NULL) 444 *x = le64toh(v); 445 return error; 446 } 447 448 int 449 md_get_mem(struct mdchain *mdp, caddr_t target, int size, int type) 450 { 451 struct mbuf *m = mdp->md_cur; 452 int error; 453 u_int count; 454 u_char *s; 455 456 while (size > 0) { 457 if (m == NULL) { 458 MBERROR("incomplete copy\n"); 459 return EBADRPC; 460 } 461 s = mdp->md_pos; 462 count = mtod(m, u_char*) + m->m_len - s; 463 if (count == 0) { 464 mdp->md_cur = m = m->m_next; 465 if (m) 466 s = mdp->md_pos = mtod(m, caddr_t); 467 continue; 468 } 469 if (count > size) 470 count = size; 471 size -= count; 472 mdp->md_pos += count; 473 if (target == NULL) 474 continue; 475 switch (type) { 476 case MB_MUSER: 477 error = copyout(s, target, count); 478 if (error) 479 return error; 480 break; 481 case MB_MSYSTEM: 482 bcopy(s, target, count); 483 break; 484 case MB_MINLINE: 485 while (count--) 486 *target++ = *s++; 487 continue; 488 } 489 target += count; 490 } 491 return 0; 492 } 493 494 int 495 md_get_mbuf(struct mdchain *mdp, int size, struct mbuf **ret) 496 { 497 struct mbuf *m = mdp->md_cur, *rm; 498 499 rm = m_copym(m, mdp->md_pos - mtod(m, u_char*), size, M_WAIT); 500 md_get_mem(mdp, NULL, size, MB_MZERO); 501 *ret = rm; 502 return 0; 503 } 504 505 int 506 md_get_uio(struct mdchain *mdp, struct uio *uiop, int size) 507 { 508 char *uiocp; 509 long left; 510 int mtype, error; 511 512 mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER; 513 while (size > 0 && uiop->uio_resid) { 514 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 515 return EFBIG; 516 left = uiop->uio_iov->iov_len; 517 if (left == 0) { 518 uiop->uio_iov++; 519 uiop->uio_iovcnt--; 520 continue; 521 } 522 uiocp = uiop->uio_iov->iov_base; 523 if (left > size) 524 left = size; 525 error = md_get_mem(mdp, uiocp, left, mtype); 526 if (error) 527 return error; 528 uiop->uio_offset += left; 529 uiop->uio_resid -= left; 530 uiop->uio_iov->iov_base = 531 (char *)uiop->uio_iov->iov_base + left; 532 uiop->uio_iov->iov_len -= left; 533 size -= left; 534 } 535 return 0; 536 } 537