1 /* $OpenBSD: uipc_socket2.c,v 1.35 2003/07/21 22:44:50 tedu Exp $ */ 2 /* $NetBSD: uipc_socket2.c,v 1.11 1996/02/04 02:17:55 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/proc.h> 38 #include <sys/file.h> 39 #include <sys/buf.h> 40 #include <sys/malloc.h> 41 #include <sys/mbuf.h> 42 #include <sys/protosw.h> 43 #include <sys/socket.h> 44 #include <sys/socketvar.h> 45 #include <sys/signalvar.h> 46 #include <sys/event.h> 47 48 /* 49 * Primitive routines for operating on sockets and socket buffers 50 */ 51 52 /* strings for sleep message: */ 53 const char netcon[] = "netcon"; 54 const char netcls[] = "netcls"; 55 const char netio[] = "netio"; 56 const char netlck[] = "netlck"; 57 58 u_long sb_max = SB_MAX; /* patchable */ 59 60 /* 61 * Procedures to manipulate state flags of socket 62 * and do appropriate wakeups. Normal sequence from the 63 * active (originating) side is that soisconnecting() is 64 * called during processing of connect() call, 65 * resulting in an eventual call to soisconnected() if/when the 66 * connection is established. When the connection is torn down 67 * soisdisconnecting() is called during processing of disconnect() call, 68 * and soisdisconnected() is called when the connection to the peer 69 * is totally severed. The semantics of these routines are such that 70 * connectionless protocols can call soisconnected() and soisdisconnected() 71 * only, bypassing the in-progress calls when setting up a ``connection'' 72 * takes no time. 73 * 74 * From the passive side, a socket is created with 75 * two queues of sockets: so_q0 for connections in progress 76 * and so_q for connections already made and awaiting user acceptance. 77 * As a protocol is preparing incoming connections, it creates a socket 78 * structure queued on so_q0 by calling sonewconn(). When the connection 79 * is established, soisconnected() is called, and transfers the 80 * socket structure to so_q, making it available to accept(). 81 * 82 * If a socket is closed with sockets on either 83 * so_q0 or so_q, these sockets are dropped. 84 * 85 * If higher level protocols are implemented in 86 * the kernel, the wakeups done here will sometimes 87 * cause software-interrupt process scheduling. 88 */ 89 90 void 91 soisconnecting(so) 92 register struct socket *so; 93 { 94 95 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 96 so->so_state |= SS_ISCONNECTING; 97 } 98 99 void 100 soisconnected(so) 101 register struct socket *so; 102 { 103 register struct socket *head = so->so_head; 104 105 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 106 so->so_state |= SS_ISCONNECTED; 107 if (head && soqremque(so, 0)) { 108 soqinsque(head, so, 1); 109 sorwakeup(head); 110 wakeup_one(&head->so_timeo); 111 } else { 112 wakeup(&so->so_timeo); 113 sorwakeup(so); 114 sowwakeup(so); 115 } 116 } 117 118 void 119 soisdisconnecting(so) 120 register struct socket *so; 121 { 122 123 so->so_state &= ~SS_ISCONNECTING; 124 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 125 wakeup(&so->so_timeo); 126 sowwakeup(so); 127 sorwakeup(so); 128 } 129 130 void 131 soisdisconnected(so) 132 register struct socket *so; 133 { 134 135 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 136 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); 137 wakeup(&so->so_timeo); 138 sowwakeup(so); 139 sorwakeup(so); 140 } 141 142 /* 143 * When an attempt at a new connection is noted on a socket 144 * which accepts connections, sonewconn is called. If the 145 * connection is possible (subject to space constraints, etc.) 146 * then we allocate a new structure, properly linked into the 147 * data structure of the original socket, and return this. 148 * Connstatus may be 0, or SS_ISCONFIRMING, or SS_ISCONNECTED. 149 * 150 * Must be called at splsoftnet() 151 */ 152 struct socket * 153 sonewconn(struct socket *head, int connstatus) 154 { 155 struct socket *so; 156 int soqueue = connstatus ? 1 : 0; 157 158 splassert(IPL_SOFTNET); 159 160 if (head->so_qlen + head->so_q0len > head->so_qlimit * 3) 161 return ((struct socket *)0); 162 so = pool_get(&socket_pool, PR_NOWAIT); 163 if (so == NULL) 164 return ((struct socket *)0); 165 bzero(so, sizeof(*so)); 166 so->so_type = head->so_type; 167 so->so_options = head->so_options &~ SO_ACCEPTCONN; 168 so->so_linger = head->so_linger; 169 so->so_state = head->so_state | SS_NOFDREF; 170 so->so_proto = head->so_proto; 171 so->so_timeo = head->so_timeo; 172 so->so_pgid = head->so_pgid; 173 so->so_euid = head->so_euid; 174 so->so_ruid = head->so_ruid; 175 so->so_egid = head->so_egid; 176 so->so_rgid = head->so_rgid; 177 so->so_siguid = head->so_siguid; 178 so->so_sigeuid = head->so_sigeuid; 179 (void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat); 180 soqinsque(head, so, soqueue); 181 if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, 182 (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) { 183 (void) soqremque(so, soqueue); 184 pool_put(&socket_pool, so); 185 return ((struct socket *)0); 186 } 187 if (connstatus) { 188 sorwakeup(head); 189 wakeup(&head->so_timeo); 190 so->so_state |= connstatus; 191 } 192 return (so); 193 } 194 195 void 196 soqinsque(struct socket *head, struct socket *so, int q) 197 { 198 199 #ifdef DIAGNOSTIC 200 if (so->so_onq != NULL) 201 panic("soqinsque"); 202 #endif 203 204 so->so_head = head; 205 if (q == 0) { 206 head->so_q0len++; 207 so->so_onq = &head->so_q0; 208 } else { 209 head->so_qlen++; 210 so->so_onq = &head->so_q; 211 } 212 TAILQ_INSERT_TAIL(so->so_onq, so, so_qe); 213 } 214 215 int 216 soqremque(struct socket *so, int q) 217 { 218 struct socket *head; 219 220 head = so->so_head; 221 if (q == 0) { 222 if (so->so_onq != &head->so_q0) 223 return (0); 224 head->so_q0len--; 225 } else { 226 if (so->so_onq != &head->so_q) 227 return (0); 228 head->so_qlen--; 229 } 230 TAILQ_REMOVE(so->so_onq, so, so_qe); 231 so->so_onq = NULL; 232 so->so_head = NULL; 233 return (1); 234 } 235 236 /* 237 * Socantsendmore indicates that no more data will be sent on the 238 * socket; it would normally be applied to a socket when the user 239 * informs the system that no more data is to be sent, by the protocol 240 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 241 * will be received, and will normally be applied to the socket by a 242 * protocol when it detects that the peer will send no more data. 243 * Data queued for reading in the socket may yet be read. 244 */ 245 246 void 247 socantsendmore(so) 248 struct socket *so; 249 { 250 251 so->so_state |= SS_CANTSENDMORE; 252 sowwakeup(so); 253 } 254 255 void 256 socantrcvmore(so) 257 struct socket *so; 258 { 259 260 so->so_state |= SS_CANTRCVMORE; 261 sorwakeup(so); 262 } 263 264 /* 265 * Wait for data to arrive at/drain from a socket buffer. 266 */ 267 int 268 sbwait(sb) 269 struct sockbuf *sb; 270 { 271 272 sb->sb_flags |= SB_WAIT; 273 return (tsleep(&sb->sb_cc, 274 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, netio, 275 sb->sb_timeo)); 276 } 277 278 /* 279 * Lock a sockbuf already known to be locked; 280 * return any error returned from sleep (EINTR). 281 */ 282 int 283 sb_lock(sb) 284 register struct sockbuf *sb; 285 { 286 int error; 287 288 while (sb->sb_flags & SB_LOCK) { 289 sb->sb_flags |= SB_WANT; 290 error = tsleep(&sb->sb_flags, 291 (sb->sb_flags & SB_NOINTR) ? 292 PSOCK : PSOCK|PCATCH, netlck, 0); 293 if (error) 294 return (error); 295 } 296 sb->sb_flags |= SB_LOCK; 297 return (0); 298 } 299 300 /* 301 * Wakeup processes waiting on a socket buffer. 302 * Do asynchronous notification via SIGIO 303 * if the socket has the SS_ASYNC flag set. 304 */ 305 void 306 sowakeup(so, sb) 307 register struct socket *so; 308 register struct sockbuf *sb; 309 { 310 selwakeup(&sb->sb_sel); 311 sb->sb_flags &= ~SB_SEL; 312 if (sb->sb_flags & SB_WAIT) { 313 sb->sb_flags &= ~SB_WAIT; 314 wakeup(&sb->sb_cc); 315 } 316 if (so->so_state & SS_ASYNC) 317 csignal(so->so_pgid, SIGIO, so->so_siguid, so->so_sigeuid); 318 KNOTE(&sb->sb_sel.si_note, 0); 319 } 320 321 /* 322 * Socket buffer (struct sockbuf) utility routines. 323 * 324 * Each socket contains two socket buffers: one for sending data and 325 * one for receiving data. Each buffer contains a queue of mbufs, 326 * information about the number of mbufs and amount of data in the 327 * queue, and other fields allowing select() statements and notification 328 * on data availability to be implemented. 329 * 330 * Data stored in a socket buffer is maintained as a list of records. 331 * Each record is a list of mbufs chained together with the m_next 332 * field. Records are chained together with the m_nextpkt field. The upper 333 * level routine soreceive() expects the following conventions to be 334 * observed when placing information in the receive buffer: 335 * 336 * 1. If the protocol requires each message be preceded by the sender's 337 * name, then a record containing that name must be present before 338 * any associated data (mbuf's must be of type MT_SONAME). 339 * 2. If the protocol supports the exchange of ``access rights'' (really 340 * just additional data associated with the message), and there are 341 * ``rights'' to be received, then a record containing this data 342 * should be present (mbuf's must be of type MT_CONTROL). 343 * 3. If a name or rights record exists, then it must be followed by 344 * a data record, perhaps of zero length. 345 * 346 * Before using a new socket structure it is first necessary to reserve 347 * buffer space to the socket, by calling sbreserve(). This should commit 348 * some of the available buffer space in the system buffer pool for the 349 * socket (currently, it does nothing but enforce limits). The space 350 * should be released by calling sbrelease() when the socket is destroyed. 351 */ 352 353 int 354 soreserve(so, sndcc, rcvcc) 355 register struct socket *so; 356 u_long sndcc, rcvcc; 357 { 358 359 if (sbreserve(&so->so_snd, sndcc) == 0) 360 goto bad; 361 if (sbreserve(&so->so_rcv, rcvcc) == 0) 362 goto bad2; 363 if (so->so_rcv.sb_lowat == 0) 364 so->so_rcv.sb_lowat = 1; 365 if (so->so_snd.sb_lowat == 0) 366 so->so_snd.sb_lowat = MCLBYTES; 367 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 368 so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 369 return (0); 370 bad2: 371 sbrelease(&so->so_snd); 372 bad: 373 return (ENOBUFS); 374 } 375 376 /* 377 * Allot mbufs to a sockbuf. 378 * Attempt to scale mbmax so that mbcnt doesn't become limiting 379 * if buffering efficiency is near the normal case. 380 */ 381 int 382 sbreserve(sb, cc) 383 struct sockbuf *sb; 384 u_long cc; 385 { 386 387 if (cc == 0 || 388 (u_int64_t)cc > (u_int64_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES)) 389 return (0); 390 sb->sb_hiwat = cc; 391 sb->sb_mbmax = min(cc * 2, sb_max); 392 if (sb->sb_lowat > sb->sb_hiwat) 393 sb->sb_lowat = sb->sb_hiwat; 394 return (1); 395 } 396 397 /* 398 * Free mbufs held by a socket, and reserved mbuf space. 399 */ 400 void 401 sbrelease(sb) 402 struct sockbuf *sb; 403 { 404 405 sbflush(sb); 406 sb->sb_hiwat = sb->sb_mbmax = 0; 407 } 408 409 /* 410 * Routines to add and remove 411 * data from an mbuf queue. 412 * 413 * The routines sbappend() or sbappendrecord() are normally called to 414 * append new mbufs to a socket buffer, after checking that adequate 415 * space is available, comparing the function sbspace() with the amount 416 * of data to be added. sbappendrecord() differs from sbappend() in 417 * that data supplied is treated as the beginning of a new record. 418 * To place a sender's address, optional access rights, and data in a 419 * socket receive buffer, sbappendaddr() should be used. To place 420 * access rights and data in a socket receive buffer, sbappendrights() 421 * should be used. In either case, the new data begins a new record. 422 * Note that unlike sbappend() and sbappendrecord(), these routines check 423 * for the caller that there will be enough space to store the data. 424 * Each fails if there is not enough space, or if it cannot find mbufs 425 * to store additional information in. 426 * 427 * Reliable protocols may use the socket send buffer to hold data 428 * awaiting acknowledgement. Data is normally copied from a socket 429 * send buffer in a protocol with m_copy for output to a peer, 430 * and then removing the data from the socket buffer with sbdrop() 431 * or sbdroprecord() when the data is acknowledged by the peer. 432 */ 433 434 #ifdef SOCKBUF_DEBUG 435 void 436 sblastrecordchk(struct sockbuf *sb, const char *where) 437 { 438 struct mbuf *m = sb->sb_mb; 439 440 while (m && m->m_nextpkt) 441 m = m->m_nextpkt; 442 443 if (m != sb->sb_lastrecord) { 444 printf("sblastrecordchk: sb_mb %p sb_lastrecord %p last %p\n", 445 sb->sb_mb, sb->sb_lastrecord, m); 446 printf("packet chain:\n"); 447 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) 448 printf("\t%p\n", m); 449 panic("sblastrecordchk from %s", where); 450 } 451 } 452 453 void 454 sblastmbufchk(struct sockbuf *sb, const char *where) 455 { 456 struct mbuf *m = sb->sb_mb; 457 struct mbuf *n; 458 459 while (m && m->m_nextpkt) 460 m = m->m_nextpkt; 461 462 while (m && m->m_next) 463 m = m->m_next; 464 465 if (m != sb->sb_mbtail) { 466 printf("sblastmbufchk: sb_mb %p sb_mbtail %p last %p\n", 467 sb->sb_mb, sb->sb_mbtail, m); 468 printf("packet tree:\n"); 469 for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) { 470 printf("\t"); 471 for (n = m; n != NULL; n = n->m_next) 472 printf("%p ", n); 473 printf("\n"); 474 } 475 panic("sblastmbufchk from %s", where); 476 } 477 } 478 #endif /* SOCKBUF_DEBUG */ 479 480 #define SBLINKRECORD(sb, m0) \ 481 do { \ 482 if ((sb)->sb_lastrecord != NULL) \ 483 (sb)->sb_lastrecord->m_nextpkt = (m0); \ 484 else \ 485 (sb)->sb_mb = (m0); \ 486 (sb)->sb_lastrecord = (m0); \ 487 } while (/*CONSTCOND*/0) 488 489 /* 490 * Append mbuf chain m to the last record in the 491 * socket buffer sb. The additional space associated 492 * the mbuf chain is recorded in sb. Empty mbufs are 493 * discarded and mbufs are compacted where possible. 494 */ 495 void 496 sbappend(sb, m) 497 struct sockbuf *sb; 498 struct mbuf *m; 499 { 500 register struct mbuf *n; 501 502 if (m == 0) 503 return; 504 505 SBLASTRECORDCHK(sb, "sbappend 1"); 506 507 if ((n = sb->sb_lastrecord) != NULL) { 508 /* 509 * XXX Would like to simply use sb_mbtail here, but 510 * XXX I need to verify that I won't miss an EOR that 511 * XXX way. 512 */ 513 do { 514 if (n->m_flags & M_EOR) { 515 sbappendrecord(sb, m); /* XXXXXX!!!! */ 516 return; 517 } 518 } while (n->m_next && (n = n->m_next)); 519 } else { 520 /* 521 * If this is the first record in the socket buffer, it's 522 * also the last record. 523 */ 524 sb->sb_lastrecord = m; 525 } 526 sbcompress(sb, m, n); 527 SBLASTRECORDCHK(sb, "sbappend 2"); 528 } 529 530 /* 531 * This version of sbappend() should only be used when the caller 532 * absolutely knows that there will never be more than one record 533 * in the socket buffer, that is, a stream protocol (such as TCP). 534 */ 535 void 536 sbappendstream(struct sockbuf *sb, struct mbuf *m) 537 { 538 539 KDASSERT(m->m_nextpkt == NULL); 540 KASSERT(sb->sb_mb == sb->sb_lastrecord); 541 542 SBLASTMBUFCHK(sb, __func__); 543 544 sbcompress(sb, m, sb->sb_mbtail); 545 546 sb->sb_lastrecord = sb->sb_mb; 547 SBLASTRECORDCHK(sb, __func__); 548 } 549 550 #ifdef SOCKBUF_DEBUG 551 void 552 sbcheck(struct sockbuf *sb) 553 { 554 struct mbuf *m; 555 u_long len = 0, mbcnt = 0; 556 557 for (m = sb->sb_mb; m; m = m->m_next) { 558 len += m->m_len; 559 mbcnt += MSIZE; 560 if (m->m_flags & M_EXT) 561 mbcnt += m->m_ext.ext_size; 562 if (m->m_nextpkt) 563 panic("sbcheck nextpkt"); 564 } 565 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 566 printf("cc %lu != %lu || mbcnt %lu != %lu\n", len, sb->sb_cc, 567 mbcnt, sb->sb_mbcnt); 568 panic("sbcheck"); 569 } 570 } 571 #endif 572 573 /* 574 * As above, except the mbuf chain 575 * begins a new record. 576 */ 577 void 578 sbappendrecord(struct sockbuf *sb, struct mbuf *m0) 579 { 580 struct mbuf *m; 581 582 if (m0 == 0) 583 return; 584 585 /* 586 * Put the first mbuf on the queue. 587 * Note this permits zero length records. 588 */ 589 sballoc(sb, m0); 590 SBLASTRECORDCHK(sb, "sbappendrecord 1"); 591 SBLINKRECORD(sb, m0); 592 m = m0->m_next; 593 m0->m_next = 0; 594 if (m && (m0->m_flags & M_EOR)) { 595 m0->m_flags &= ~M_EOR; 596 m->m_flags |= M_EOR; 597 } 598 sbcompress(sb, m, m0); 599 SBLASTRECORDCHK(sb, "sbappendrecord 2"); 600 } 601 602 /* 603 * As above except that OOB data 604 * is inserted at the beginning of the sockbuf, 605 * but after any other OOB data. 606 */ 607 void 608 sbinsertoob(struct sockbuf *sb, struct mbuf *m0) 609 { 610 struct mbuf *m, **mp; 611 612 if (m0 == 0) 613 return; 614 615 SBLASTRECORDCHK(sb, "sbinsertoob 1"); 616 617 for (mp = &sb->sb_mb; (m = *mp) != NULL; mp = &((*mp)->m_nextpkt)) { 618 again: 619 switch (m->m_type) { 620 621 case MT_OOBDATA: 622 continue; /* WANT next train */ 623 624 case MT_CONTROL: 625 if ((m = m->m_next) != NULL) 626 goto again; /* inspect THIS train further */ 627 } 628 break; 629 } 630 /* 631 * Put the first mbuf on the queue. 632 * Note this permits zero length records. 633 */ 634 sballoc(sb, m0); 635 m0->m_nextpkt = *mp; 636 if (*mp == NULL) { 637 /* m0 is actually the new tail */ 638 sb->sb_lastrecord = m0; 639 } 640 *mp = m0; 641 m = m0->m_next; 642 m0->m_next = 0; 643 if (m && (m0->m_flags & M_EOR)) { 644 m0->m_flags &= ~M_EOR; 645 m->m_flags |= M_EOR; 646 } 647 sbcompress(sb, m, m0); 648 SBLASTRECORDCHK(sb, "sbinsertoob 2"); 649 } 650 651 /* 652 * Append address and data, and optionally, control (ancillary) data 653 * to the receive queue of a socket. If present, 654 * m0 must include a packet header with total length. 655 * Returns 0 if no space in sockbuf or insufficient mbufs. 656 */ 657 int 658 sbappendaddr(struct sockbuf *sb, struct sockaddr *asa, struct mbuf *m0, 659 struct mbuf *control) 660 { 661 struct mbuf *m, *n, *nlast; 662 int space = asa->sa_len; 663 664 if (m0 && (m0->m_flags & M_PKTHDR) == 0) 665 panic("sbappendaddr"); 666 if (m0) 667 space += m0->m_pkthdr.len; 668 for (n = control; n; n = n->m_next) { 669 space += n->m_len; 670 if (n->m_next == 0) /* keep pointer to last control buf */ 671 break; 672 } 673 if (space > sbspace(sb)) 674 return (0); 675 if (asa->sa_len > MLEN) 676 return (0); 677 MGET(m, M_DONTWAIT, MT_SONAME); 678 if (m == 0) 679 return (0); 680 m->m_len = asa->sa_len; 681 bcopy(asa, mtod(m, caddr_t), asa->sa_len); 682 if (n) 683 n->m_next = m0; /* concatenate data to control */ 684 else 685 control = m0; 686 m->m_next = control; 687 688 SBLASTRECORDCHK(sb, "sbappendaddr 1"); 689 690 for (n = m; n->m_next != NULL; n = n->m_next) 691 sballoc(sb, n); 692 sballoc(sb, n); 693 nlast = n; 694 SBLINKRECORD(sb, m); 695 696 sb->sb_mbtail = nlast; 697 SBLASTMBUFCHK(sb, "sbappendaddr"); 698 699 SBLASTRECORDCHK(sb, "sbappendaddr 2"); 700 701 return (1); 702 } 703 704 int 705 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control) 706 { 707 struct mbuf *m, *mlast, *n; 708 int space = 0; 709 710 if (control == 0) 711 panic("sbappendcontrol"); 712 for (m = control; ; m = m->m_next) { 713 space += m->m_len; 714 if (m->m_next == 0) 715 break; 716 } 717 n = m; /* save pointer to last control buffer */ 718 for (m = m0; m; m = m->m_next) 719 space += m->m_len; 720 if (space > sbspace(sb)) 721 return (0); 722 n->m_next = m0; /* concatenate data to control */ 723 724 SBLASTRECORDCHK(sb, "sbappendcontrol 1"); 725 726 for (m = control; m->m_next != NULL; m = m->m_next) 727 sballoc(sb, m); 728 sballoc(sb, m); 729 mlast = m; 730 SBLINKRECORD(sb, control); 731 732 sb->sb_mbtail = mlast; 733 SBLASTMBUFCHK(sb, "sbappendcontrol"); 734 735 SBLASTRECORDCHK(sb, "sbappendcontrol 2"); 736 737 return (1); 738 } 739 740 /* 741 * Compress mbuf chain m into the socket 742 * buffer sb following mbuf n. If n 743 * is null, the buffer is presumed empty. 744 */ 745 void 746 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n) 747 { 748 int eor = 0; 749 struct mbuf *o; 750 751 while (m) { 752 eor |= m->m_flags & M_EOR; 753 if (m->m_len == 0 && 754 (eor == 0 || 755 (((o = m->m_next) || (o = n)) && 756 o->m_type == m->m_type))) { 757 if (sb->sb_lastrecord == m) 758 sb->sb_lastrecord = m->m_next; 759 m = m_free(m); 760 continue; 761 } 762 if (n && (n->m_flags & M_EOR) == 0 && 763 /* M_TRAILINGSPACE() checks buffer writeability */ 764 m->m_len <= MCLBYTES / 4 && /* XXX Don't copy too much */ 765 m->m_len <= M_TRAILINGSPACE(n) && 766 n->m_type == m->m_type) { 767 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 768 (unsigned)m->m_len); 769 n->m_len += m->m_len; 770 sb->sb_cc += m->m_len; 771 m = m_free(m); 772 continue; 773 } 774 if (n) 775 n->m_next = m; 776 else 777 sb->sb_mb = m; 778 sb->sb_mbtail = m; 779 sballoc(sb, m); 780 n = m; 781 m->m_flags &= ~M_EOR; 782 m = m->m_next; 783 n->m_next = 0; 784 } 785 if (eor) { 786 if (n) 787 n->m_flags |= eor; 788 else 789 printf("semi-panic: sbcompress\n"); 790 } 791 SBLASTMBUFCHK(sb, __func__); 792 } 793 794 /* 795 * Free all mbufs in a sockbuf. 796 * Check that all resources are reclaimed. 797 */ 798 void 799 sbflush(struct sockbuf *sb) 800 { 801 802 KASSERT((sb->sb_flags & SB_LOCK) == 0); 803 804 while (sb->sb_mbcnt) 805 sbdrop(sb, (int)sb->sb_cc); 806 807 KASSERT(sb->sb_cc == 0); 808 KASSERT(sb->sb_mb == NULL); 809 KASSERT(sb->sb_mbtail == NULL); 810 KASSERT(sb->sb_lastrecord == NULL); 811 } 812 813 /* 814 * Drop data from (the front of) a sockbuf. 815 */ 816 void 817 sbdrop(struct sockbuf *sb, int len) 818 { 819 struct mbuf *m, *mn; 820 struct mbuf *next; 821 822 next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 823 while (len > 0) { 824 if (m == 0) { 825 if (next == 0) 826 panic("sbdrop"); 827 m = next; 828 next = m->m_nextpkt; 829 continue; 830 } 831 if (m->m_len > len) { 832 m->m_len -= len; 833 m->m_data += len; 834 sb->sb_cc -= len; 835 break; 836 } 837 len -= m->m_len; 838 sbfree(sb, m); 839 MFREE(m, mn); 840 m = mn; 841 } 842 while (m && m->m_len == 0) { 843 sbfree(sb, m); 844 MFREE(m, mn); 845 m = mn; 846 } 847 if (m) { 848 sb->sb_mb = m; 849 m->m_nextpkt = next; 850 } else 851 sb->sb_mb = next; 852 /* 853 * First part is an inline SB_EMPTY_FIXUP(). Second part 854 * makes sure sb_lastrecord is up-to-date if we dropped 855 * part of the last record. 856 */ 857 m = sb->sb_mb; 858 if (m == NULL) { 859 sb->sb_mbtail = NULL; 860 sb->sb_lastrecord = NULL; 861 } else if (m->m_nextpkt == NULL) 862 sb->sb_lastrecord = m; 863 } 864 865 /* 866 * Drop a record off the front of a sockbuf 867 * and move the next record to the front. 868 */ 869 void 870 sbdroprecord(struct sockbuf *sb) 871 { 872 struct mbuf *m, *mn; 873 874 m = sb->sb_mb; 875 if (m) { 876 sb->sb_mb = m->m_nextpkt; 877 do { 878 sbfree(sb, m); 879 MFREE(m, mn); 880 } while ((m = mn) != NULL); 881 } 882 SB_EMPTY_FIXUP(sb); 883 } 884 885 /* 886 * Create a "control" mbuf containing the specified data 887 * with the specified type for presentation on a socket buffer. 888 */ 889 struct mbuf * 890 sbcreatecontrol(p, size, type, level) 891 caddr_t p; 892 register int size; 893 int type, level; 894 { 895 register struct cmsghdr *cp; 896 struct mbuf *m; 897 898 if (CMSG_SPACE(size) > MCLBYTES) { 899 printf("sbcreatecontrol: message too large %d\n", size); 900 return NULL; 901 } 902 903 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) 904 return ((struct mbuf *) NULL); 905 if (CMSG_SPACE(size) > MLEN) { 906 MCLGET(m, M_DONTWAIT); 907 if ((m->m_flags & M_EXT) == 0) { 908 m_free(m); 909 return NULL; 910 } 911 } 912 cp = mtod(m, struct cmsghdr *); 913 bcopy(p, CMSG_DATA(cp), size); 914 m->m_len = CMSG_SPACE(size); 915 cp->cmsg_len = CMSG_LEN(size); 916 cp->cmsg_level = level; 917 cp->cmsg_type = type; 918 return (m); 919 } 920