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_TRYWAIT, MT_DATA); 60 if (m == NULL) 61 return ENOBUFS; 62 m->m_len = 0; 63 mb_initm(mbp, m); 64 return 0; 65 } 66 67 void 68 mb_initm(struct mbchain *mbp, struct mbuf *m) 69 { 70 bzero(mbp, sizeof(*mbp)); 71 mbp->mb_top = mbp->mb_cur = m; 72 mbp->mb_mleft = M_TRAILINGSPACE(m); 73 } 74 75 void 76 mb_done(struct mbchain *mbp) 77 { 78 if (mbp->mb_top) { 79 m_freem(mbp->mb_top); 80 mbp->mb_top = NULL; 81 } 82 } 83 84 struct mbuf * 85 mb_detach(struct mbchain *mbp) 86 { 87 struct mbuf *m; 88 89 m = mbp->mb_top; 90 mbp->mb_top = NULL; 91 return m; 92 } 93 94 int 95 mb_fixhdr(struct mbchain *mbp) 96 { 97 return mbp->mb_top->m_pkthdr.len = m_fixhdr(mbp->mb_top); 98 } 99 100 /* 101 * Check if object of size 'size' fit to the current position and 102 * allocate new mbuf if not. Advance pointers and increase length of mbuf(s). 103 * Return pointer to the object placeholder or NULL if any error occured. 104 * Note: size should be <= MLEN 105 */ 106 caddr_t 107 mb_reserve(struct mbchain *mbp, int size) 108 { 109 struct mbuf *m, *mn; 110 caddr_t bpos; 111 112 if (size > MLEN) 113 panic("mb_reserve: size = %d\n", size); 114 m = mbp->mb_cur; 115 if (mbp->mb_mleft < size) { 116 mn = m_get(M_TRYWAIT, MT_DATA); 117 if (mn == NULL) 118 return NULL; 119 mbp->mb_cur = m->m_next = mn; 120 m = mn; 121 m->m_len = 0; 122 mbp->mb_mleft = M_TRAILINGSPACE(m); 123 } 124 mbp->mb_mleft -= size; 125 mbp->mb_count += size; 126 bpos = mtod(m, caddr_t) + m->m_len; 127 m->m_len += size; 128 return bpos; 129 } 130 131 int 132 mb_put_uint8(struct mbchain *mbp, u_int8_t x) 133 { 134 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 135 } 136 137 int 138 mb_put_uint16be(struct mbchain *mbp, u_int16_t x) 139 { 140 x = htobe16(x); 141 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 142 } 143 144 int 145 mb_put_uint16le(struct mbchain *mbp, u_int16_t x) 146 { 147 x = htole16(x); 148 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 149 } 150 151 int 152 mb_put_uint32be(struct mbchain *mbp, u_int32_t x) 153 { 154 x = htobe32(x); 155 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 156 } 157 158 int 159 mb_put_uint32le(struct mbchain *mbp, u_int32_t x) 160 { 161 x = htole32(x); 162 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 163 } 164 165 int 166 mb_put_int64be(struct mbchain *mbp, int64_t x) 167 { 168 x = htobe64(x); 169 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 170 } 171 172 int 173 mb_put_int64le(struct mbchain *mbp, int64_t x) 174 { 175 x = htole64(x); 176 return mb_put_mem(mbp, (caddr_t)&x, sizeof(x), MB_MSYSTEM); 177 } 178 179 int 180 mb_put_mem(struct mbchain *mbp, c_caddr_t source, int size, int type) 181 { 182 struct mbuf *m; 183 caddr_t dst; 184 c_caddr_t src; 185 int cplen, error, mleft, count; 186 187 m = mbp->mb_cur; 188 mleft = mbp->mb_mleft; 189 190 while (size > 0) { 191 if (mleft == 0) { 192 if (m->m_next == NULL) { 193 m = m_getm(m, size, M_TRYWAIT, MT_DATA); 194 if (m == NULL) 195 return ENOBUFS; 196 } else 197 m = m->m_next; 198 mleft = M_TRAILINGSPACE(m); 199 continue; 200 } 201 cplen = mleft > size ? size : mleft; 202 dst = mtod(m, caddr_t) + m->m_len; 203 switch (type) { 204 case MB_MCUSTOM: 205 error = mbp->mb_copy(mbp, source, dst, cplen); 206 if (error) 207 return error; 208 break; 209 case MB_MINLINE: 210 for (src = source, count = cplen; count; count--) 211 *dst++ = *src++; 212 break; 213 case MB_MSYSTEM: 214 bcopy(source, dst, cplen); 215 break; 216 case MB_MUSER: 217 error = copyin(source, dst, cplen); 218 if (error) 219 return error; 220 break; 221 case MB_MZERO: 222 bzero(dst, cplen); 223 break; 224 } 225 size -= cplen; 226 source += cplen; 227 m->m_len += cplen; 228 mleft -= cplen; 229 mbp->mb_count += cplen; 230 } 231 mbp->mb_cur = m; 232 mbp->mb_mleft = mleft; 233 return 0; 234 } 235 236 int 237 mb_put_mbuf(struct mbchain *mbp, struct mbuf *m) 238 { 239 mbp->mb_cur->m_next = m; 240 while (m) { 241 mbp->mb_count += m->m_len; 242 if (m->m_next == NULL) 243 break; 244 m = m->m_next; 245 } 246 mbp->mb_mleft = M_TRAILINGSPACE(m); 247 mbp->mb_cur = m; 248 return 0; 249 } 250 251 /* 252 * copies a uio scatter/gather list to an mbuf chain. 253 */ 254 int 255 mb_put_uio(struct mbchain *mbp, struct uio *uiop, int size) 256 { 257 long left; 258 int mtype, error; 259 260 mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER; 261 262 while (size > 0 && uiop->uio_resid) { 263 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 264 return EFBIG; 265 left = uiop->uio_iov->iov_len; 266 if (left == 0) { 267 uiop->uio_iov++; 268 uiop->uio_iovcnt--; 269 continue; 270 } 271 if (left > size) 272 left = size; 273 error = mb_put_mem(mbp, uiop->uio_iov->iov_base, left, mtype); 274 if (error) 275 return error; 276 uiop->uio_offset += left; 277 uiop->uio_resid -= left; 278 uiop->uio_iov->iov_base = 279 (char *)uiop->uio_iov->iov_base + left; 280 uiop->uio_iov->iov_len -= left; 281 size -= left; 282 } 283 return 0; 284 } 285 286 /* 287 * Routines for fetching data from an mbuf chain 288 */ 289 int 290 md_init(struct mdchain *mdp) 291 { 292 struct mbuf *m; 293 294 m = m_gethdr(M_TRYWAIT, MT_DATA); 295 if (m == NULL) 296 return ENOBUFS; 297 m->m_len = 0; 298 md_initm(mdp, m); 299 return 0; 300 } 301 302 void 303 md_initm(struct mdchain *mdp, struct mbuf *m) 304 { 305 bzero(mdp, sizeof(*mdp)); 306 mdp->md_top = mdp->md_cur = m; 307 mdp->md_pos = mtod(m, u_char*); 308 } 309 310 void 311 md_done(struct mdchain *mdp) 312 { 313 if (mdp->md_top) { 314 m_freem(mdp->md_top); 315 mdp->md_top = NULL; 316 } 317 } 318 319 /* 320 * Append a separate mbuf chain. It is caller responsibility to prevent 321 * multiple calls to fetch/record routines. 322 */ 323 void 324 md_append_record(struct mdchain *mdp, struct mbuf *top) 325 { 326 struct mbuf *m; 327 328 if (mdp->md_top == NULL) { 329 md_initm(mdp, top); 330 return; 331 } 332 m = mdp->md_top; 333 while (m->m_nextpkt) 334 m = m->m_nextpkt; 335 m->m_nextpkt = top; 336 top->m_nextpkt = NULL; 337 return; 338 } 339 340 /* 341 * Put next record in place of existing 342 */ 343 int 344 md_next_record(struct mdchain *mdp) 345 { 346 struct mbuf *m; 347 348 if (mdp->md_top == NULL) 349 return ENOENT; 350 m = mdp->md_top->m_nextpkt; 351 md_done(mdp); 352 if (m == NULL) 353 return ENOENT; 354 md_initm(mdp, m); 355 return 0; 356 } 357 358 int 359 md_get_uint8(struct mdchain *mdp, u_int8_t *x) 360 { 361 return md_get_mem(mdp, x, 1, MB_MINLINE); 362 } 363 364 int 365 md_get_uint16(struct mdchain *mdp, u_int16_t *x) 366 { 367 return md_get_mem(mdp, (caddr_t)x, 2, MB_MINLINE); 368 } 369 370 int 371 md_get_uint16le(struct mdchain *mdp, u_int16_t *x) 372 { 373 u_int16_t v; 374 int error = md_get_uint16(mdp, &v); 375 376 if (x != NULL) 377 *x = le16toh(v); 378 return error; 379 } 380 381 int 382 md_get_uint16be(struct mdchain *mdp, u_int16_t *x) { 383 u_int16_t v; 384 int error = md_get_uint16(mdp, &v); 385 386 if (x != NULL) 387 *x = be16toh(v); 388 return error; 389 } 390 391 int 392 md_get_uint32(struct mdchain *mdp, u_int32_t *x) 393 { 394 return md_get_mem(mdp, (caddr_t)x, 4, MB_MINLINE); 395 } 396 397 int 398 md_get_uint32be(struct mdchain *mdp, u_int32_t *x) 399 { 400 u_int32_t v; 401 int error; 402 403 error = md_get_uint32(mdp, &v); 404 if (x != NULL) 405 *x = be32toh(v); 406 return error; 407 } 408 409 int 410 md_get_uint32le(struct mdchain *mdp, u_int32_t *x) 411 { 412 u_int32_t v; 413 int error; 414 415 error = md_get_uint32(mdp, &v); 416 if (x != NULL) 417 *x = le32toh(v); 418 return error; 419 } 420 421 int 422 md_get_int64(struct mdchain *mdp, int64_t *x) 423 { 424 return md_get_mem(mdp, (caddr_t)x, 8, MB_MINLINE); 425 } 426 427 int 428 md_get_int64be(struct mdchain *mdp, int64_t *x) 429 { 430 int64_t v; 431 int error; 432 433 error = md_get_int64(mdp, &v); 434 if (x != NULL) 435 *x = be64toh(v); 436 return error; 437 } 438 439 int 440 md_get_int64le(struct mdchain *mdp, int64_t *x) 441 { 442 int64_t v; 443 int error; 444 445 error = md_get_int64(mdp, &v); 446 if (x != NULL) 447 *x = le64toh(v); 448 return error; 449 } 450 451 int 452 md_get_mem(struct mdchain *mdp, caddr_t target, int size, int type) 453 { 454 struct mbuf *m = mdp->md_cur; 455 int error; 456 u_int count; 457 u_char *s; 458 459 while (size > 0) { 460 if (m == NULL) { 461 MBERROR("incomplete copy\n"); 462 return EBADRPC; 463 } 464 s = mdp->md_pos; 465 count = mtod(m, u_char*) + m->m_len - s; 466 if (count == 0) { 467 mdp->md_cur = m = m->m_next; 468 if (m) 469 s = mdp->md_pos = mtod(m, caddr_t); 470 continue; 471 } 472 if (count > size) 473 count = size; 474 size -= count; 475 mdp->md_pos += count; 476 if (target == NULL) 477 continue; 478 switch (type) { 479 case MB_MUSER: 480 error = copyout(s, target, count); 481 if (error) 482 return error; 483 break; 484 case MB_MSYSTEM: 485 bcopy(s, target, count); 486 break; 487 case MB_MINLINE: 488 while (count--) 489 *target++ = *s++; 490 continue; 491 } 492 target += count; 493 } 494 return 0; 495 } 496 497 int 498 md_get_mbuf(struct mdchain *mdp, int size, struct mbuf **ret) 499 { 500 struct mbuf *m = mdp->md_cur, *rm; 501 502 rm = m_copym(m, mdp->md_pos - mtod(m, u_char*), size, M_TRYWAIT); 503 if (rm == NULL) 504 return EBADRPC; 505 md_get_mem(mdp, NULL, size, MB_MZERO); 506 *ret = rm; 507 return 0; 508 } 509 510 int 511 md_get_uio(struct mdchain *mdp, struct uio *uiop, int size) 512 { 513 char *uiocp; 514 long left; 515 int mtype, error; 516 517 mtype = (uiop->uio_segflg == UIO_SYSSPACE) ? MB_MSYSTEM : MB_MUSER; 518 while (size > 0 && uiop->uio_resid) { 519 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) 520 return EFBIG; 521 left = uiop->uio_iov->iov_len; 522 if (left == 0) { 523 uiop->uio_iov++; 524 uiop->uio_iovcnt--; 525 continue; 526 } 527 uiocp = uiop->uio_iov->iov_base; 528 if (left > size) 529 left = size; 530 error = md_get_mem(mdp, uiocp, left, mtype); 531 if (error) 532 return error; 533 uiop->uio_offset += left; 534 uiop->uio_resid -= left; 535 uiop->uio_iov->iov_base = 536 (char *)uiop->uio_iov->iov_base + left; 537 uiop->uio_iov->iov_len -= left; 538 size -= left; 539 } 540 return 0; 541 } 542