1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)uipc_socket2.c 7.19 (Berkeley) 10/11/92 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/proc.h> 13 #include <sys/file.h> 14 #include <sys/buf.h> 15 #include <sys/malloc.h> 16 #include <sys/mbuf.h> 17 #include <sys/protosw.h> 18 #include <sys/socket.h> 19 #include <sys/socketvar.h> 20 21 /* 22 * Primitive routines for operating on sockets and socket buffers 23 */ 24 25 /* strings for sleep message: */ 26 char netio[] = "netio"; 27 char netcon[] = "netcon"; 28 char netcls[] = "netcls"; 29 30 u_long sb_max = SB_MAX; /* patchable */ 31 32 /* 33 * Procedures to manipulate state flags of socket 34 * and do appropriate wakeups. Normal sequence from the 35 * active (originating) side is that soisconnecting() is 36 * called during processing of connect() call, 37 * resulting in an eventual call to soisconnected() if/when the 38 * connection is established. When the connection is torn down 39 * soisdisconnecting() is called during processing of disconnect() call, 40 * and soisdisconnected() is called when the connection to the peer 41 * is totally severed. The semantics of these routines are such that 42 * connectionless protocols can call soisconnected() and soisdisconnected() 43 * only, bypassing the in-progress calls when setting up a ``connection'' 44 * takes no time. 45 * 46 * From the passive side, a socket is created with 47 * two queues of sockets: so_q0 for connections in progress 48 * and so_q for connections already made and awaiting user acceptance. 49 * As a protocol is preparing incoming connections, it creates a socket 50 * structure queued on so_q0 by calling sonewconn(). When the connection 51 * is established, soisconnected() is called, and transfers the 52 * socket structure to so_q, making it available to accept(). 53 * 54 * If a socket is closed with sockets on either 55 * so_q0 or so_q, these sockets are dropped. 56 * 57 * If higher level protocols are implemented in 58 * the kernel, the wakeups done here will sometimes 59 * cause software-interrupt process scheduling. 60 */ 61 62 soisconnecting(so) 63 register struct socket *so; 64 { 65 66 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 67 so->so_state |= SS_ISCONNECTING; 68 } 69 70 soisconnected(so) 71 register struct socket *so; 72 { 73 register struct socket *head = so->so_head; 74 75 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 76 so->so_state |= SS_ISCONNECTED; 77 if (head && soqremque(so, 0)) { 78 soqinsque(head, so, 1); 79 sorwakeup(head); 80 wakeup((caddr_t)&head->so_timeo); 81 } else { 82 wakeup((caddr_t)&so->so_timeo); 83 sorwakeup(so); 84 sowwakeup(so); 85 } 86 } 87 88 soisdisconnecting(so) 89 register struct socket *so; 90 { 91 92 so->so_state &= ~SS_ISCONNECTING; 93 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 94 wakeup((caddr_t)&so->so_timeo); 95 sowwakeup(so); 96 sorwakeup(so); 97 } 98 99 soisdisconnected(so) 100 register struct socket *so; 101 { 102 103 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 104 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE); 105 wakeup((caddr_t)&so->so_timeo); 106 sowwakeup(so); 107 sorwakeup(so); 108 } 109 110 /* 111 * When an attempt at a new connection is noted on a socket 112 * which accepts connections, sonewconn is called. If the 113 * connection is possible (subject to space constraints, etc.) 114 * then we allocate a new structure, propoerly linked into the 115 * data structure of the original socket, and return this. 116 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. 117 * 118 * Currently, sonewconn() is defined as sonewconn1() in socketvar.h 119 * to catch calls that are missing the (new) second parameter. 120 */ 121 struct socket * 122 sonewconn1(head, connstatus) 123 register struct socket *head; 124 int connstatus; 125 { 126 register struct socket *so; 127 int soqueue = connstatus ? 1 : 0; 128 129 if (head->so_qlen + head->so_q0len > 3 * head->so_qlimit / 2) 130 return ((struct socket *)0); 131 MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_DONTWAIT); 132 if (so == NULL) 133 return ((struct socket *)0); 134 bzero((caddr_t)so, sizeof(*so)); 135 so->so_type = head->so_type; 136 so->so_options = head->so_options &~ SO_ACCEPTCONN; 137 so->so_linger = head->so_linger; 138 so->so_state = head->so_state | SS_NOFDREF; 139 so->so_proto = head->so_proto; 140 so->so_timeo = head->so_timeo; 141 so->so_pgid = head->so_pgid; 142 (void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat); 143 soqinsque(head, so, soqueue); 144 if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH, 145 (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) { 146 (void) soqremque(so, soqueue); 147 (void) free((caddr_t)so, M_SOCKET); 148 return ((struct socket *)0); 149 } 150 if (connstatus) { 151 sorwakeup(head); 152 wakeup((caddr_t)&head->so_timeo); 153 so->so_state |= connstatus; 154 } 155 return (so); 156 } 157 158 soqinsque(head, so, q) 159 register struct socket *head, *so; 160 int q; 161 { 162 163 register struct socket **prev; 164 so->so_head = head; 165 if (q == 0) { 166 head->so_q0len++; 167 so->so_q0 = 0; 168 for (prev = &(head->so_q0); *prev; ) 169 prev = &((*prev)->so_q0); 170 } else { 171 head->so_qlen++; 172 so->so_q = 0; 173 for (prev = &(head->so_q); *prev; ) 174 prev = &((*prev)->so_q); 175 } 176 *prev = so; 177 } 178 179 soqremque(so, q) 180 register struct socket *so; 181 int q; 182 { 183 register struct socket *head, *prev, *next; 184 185 head = so->so_head; 186 prev = head; 187 for (;;) { 188 next = q ? prev->so_q : prev->so_q0; 189 if (next == so) 190 break; 191 if (next == 0) 192 return (0); 193 prev = next; 194 } 195 if (q == 0) { 196 prev->so_q0 = next->so_q0; 197 head->so_q0len--; 198 } else { 199 prev->so_q = next->so_q; 200 head->so_qlen--; 201 } 202 next->so_q0 = next->so_q = 0; 203 next->so_head = 0; 204 return (1); 205 } 206 207 /* 208 * Socantsendmore indicates that no more data will be sent on the 209 * socket; it would normally be applied to a socket when the user 210 * informs the system that no more data is to be sent, by the protocol 211 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 212 * will be received, and will normally be applied to the socket by a 213 * protocol when it detects that the peer will send no more data. 214 * Data queued for reading in the socket may yet be read. 215 */ 216 217 socantsendmore(so) 218 struct socket *so; 219 { 220 221 so->so_state |= SS_CANTSENDMORE; 222 sowwakeup(so); 223 } 224 225 socantrcvmore(so) 226 struct socket *so; 227 { 228 229 so->so_state |= SS_CANTRCVMORE; 230 sorwakeup(so); 231 } 232 233 /* 234 * Wait for data to arrive at/drain from a socket buffer. 235 */ 236 sbwait(sb) 237 struct sockbuf *sb; 238 { 239 240 sb->sb_flags |= SB_WAIT; 241 return (tsleep((caddr_t)&sb->sb_cc, 242 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, netio, 243 sb->sb_timeo)); 244 } 245 246 /* 247 * Lock a sockbuf already known to be locked; 248 * return any error returned from sleep (EINTR). 249 */ 250 sb_lock(sb) 251 register struct sockbuf *sb; 252 { 253 int error; 254 255 while (sb->sb_flags & SB_LOCK) { 256 sb->sb_flags |= SB_WANT; 257 if (error = tsleep((caddr_t)&sb->sb_flags, 258 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH, 259 netio, 0)) 260 return (error); 261 } 262 sb->sb_flags |= SB_LOCK; 263 return (0); 264 } 265 266 /* 267 * Wakeup processes waiting on a socket buffer. 268 * Do asynchronous notification via SIGIO 269 * if the socket has the SS_ASYNC flag set. 270 */ 271 sowakeup(so, sb) 272 register struct socket *so; 273 register struct sockbuf *sb; 274 { 275 struct proc *p; 276 277 selwakeup(&sb->sb_sel); 278 sb->sb_flags &= ~SB_SEL; 279 if (sb->sb_flags & SB_WAIT) { 280 sb->sb_flags &= ~SB_WAIT; 281 wakeup((caddr_t)&sb->sb_cc); 282 } 283 if (so->so_state & SS_ASYNC) { 284 if (so->so_pgid < 0) 285 gsignal(-so->so_pgid, SIGIO); 286 else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0) 287 psignal(p, SIGIO); 288 } 289 } 290 291 /* 292 * Socket buffer (struct sockbuf) utility routines. 293 * 294 * Each socket contains two socket buffers: one for sending data and 295 * one for receiving data. Each buffer contains a queue of mbufs, 296 * information about the number of mbufs and amount of data in the 297 * queue, and other fields allowing select() statements and notification 298 * on data availability to be implemented. 299 * 300 * Data stored in a socket buffer is maintained as a list of records. 301 * Each record is a list of mbufs chained together with the m_next 302 * field. Records are chained together with the m_nextpkt field. The upper 303 * level routine soreceive() expects the following conventions to be 304 * observed when placing information in the receive buffer: 305 * 306 * 1. If the protocol requires each message be preceded by the sender's 307 * name, then a record containing that name must be present before 308 * any associated data (mbuf's must be of type MT_SONAME). 309 * 2. If the protocol supports the exchange of ``access rights'' (really 310 * just additional data associated with the message), and there are 311 * ``rights'' to be received, then a record containing this data 312 * should be present (mbuf's must be of type MT_RIGHTS). 313 * 3. If a name or rights record exists, then it must be followed by 314 * a data record, perhaps of zero length. 315 * 316 * Before using a new socket structure it is first necessary to reserve 317 * buffer space to the socket, by calling sbreserve(). This should commit 318 * some of the available buffer space in the system buffer pool for the 319 * socket (currently, it does nothing but enforce limits). The space 320 * should be released by calling sbrelease() when the socket is destroyed. 321 */ 322 323 soreserve(so, sndcc, rcvcc) 324 register struct socket *so; 325 u_long sndcc, rcvcc; 326 { 327 328 if (sbreserve(&so->so_snd, sndcc) == 0) 329 goto bad; 330 if (sbreserve(&so->so_rcv, rcvcc) == 0) 331 goto bad2; 332 if (so->so_rcv.sb_lowat == 0) 333 so->so_rcv.sb_lowat = 1; 334 if (so->so_snd.sb_lowat == 0) 335 so->so_snd.sb_lowat = MCLBYTES; 336 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) 337 so->so_snd.sb_lowat = so->so_snd.sb_hiwat; 338 return (0); 339 bad2: 340 sbrelease(&so->so_snd); 341 bad: 342 return (ENOBUFS); 343 } 344 345 /* 346 * Allot mbufs to a sockbuf. 347 * Attempt to scale mbmax so that mbcnt doesn't become limiting 348 * if buffering efficiency is near the normal case. 349 */ 350 sbreserve(sb, cc) 351 struct sockbuf *sb; 352 u_long cc; 353 { 354 355 if (cc > sb_max * MCLBYTES / (MSIZE + MCLBYTES)) 356 return (0); 357 sb->sb_hiwat = cc; 358 sb->sb_mbmax = min(cc * 2, sb_max); 359 if (sb->sb_lowat > sb->sb_hiwat) 360 sb->sb_lowat = sb->sb_hiwat; 361 return (1); 362 } 363 364 /* 365 * Free mbufs held by a socket, and reserved mbuf space. 366 */ 367 sbrelease(sb) 368 struct sockbuf *sb; 369 { 370 371 sbflush(sb); 372 sb->sb_hiwat = sb->sb_mbmax = 0; 373 } 374 375 /* 376 * Routines to add and remove 377 * data from an mbuf queue. 378 * 379 * The routines sbappend() or sbappendrecord() are normally called to 380 * append new mbufs to a socket buffer, after checking that adequate 381 * space is available, comparing the function sbspace() with the amount 382 * of data to be added. sbappendrecord() differs from sbappend() in 383 * that data supplied is treated as the beginning of a new record. 384 * To place a sender's address, optional access rights, and data in a 385 * socket receive buffer, sbappendaddr() should be used. To place 386 * access rights and data in a socket receive buffer, sbappendrights() 387 * should be used. In either case, the new data begins a new record. 388 * Note that unlike sbappend() and sbappendrecord(), these routines check 389 * for the caller that there will be enough space to store the data. 390 * Each fails if there is not enough space, or if it cannot find mbufs 391 * to store additional information in. 392 * 393 * Reliable protocols may use the socket send buffer to hold data 394 * awaiting acknowledgement. Data is normally copied from a socket 395 * send buffer in a protocol with m_copy for output to a peer, 396 * and then removing the data from the socket buffer with sbdrop() 397 * or sbdroprecord() when the data is acknowledged by the peer. 398 */ 399 400 /* 401 * Append mbuf chain m to the last record in the 402 * socket buffer sb. The additional space associated 403 * the mbuf chain is recorded in sb. Empty mbufs are 404 * discarded and mbufs are compacted where possible. 405 */ 406 sbappend(sb, m) 407 struct sockbuf *sb; 408 struct mbuf *m; 409 { 410 register struct mbuf *n; 411 412 if (m == 0) 413 return; 414 if (n = sb->sb_mb) { 415 while (n->m_nextpkt) 416 n = n->m_nextpkt; 417 do { 418 if (n->m_flags & M_EOR) { 419 sbappendrecord(sb, m); /* XXXXXX!!!! */ 420 return; 421 } 422 } while (n->m_next && (n = n->m_next)); 423 } 424 sbcompress(sb, m, n); 425 } 426 427 #ifdef SOCKBUF_DEBUG 428 sbcheck(sb) 429 register struct sockbuf *sb; 430 { 431 register struct mbuf *m; 432 register int len = 0, mbcnt = 0; 433 434 for (m = sb->sb_mb; m; m = m->m_next) { 435 len += m->m_len; 436 mbcnt += MSIZE; 437 if (m->m_flags & M_EXT) 438 mbcnt += m->m_ext.ext_size; 439 if (m->m_nextpkt) 440 panic("sbcheck nextpkt"); 441 } 442 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) { 443 printf("cc %d != %d || mbcnt %d != %d\n", len, sb->sb_cc, 444 mbcnt, sb->sb_mbcnt); 445 panic("sbcheck"); 446 } 447 } 448 #endif 449 450 /* 451 * As above, except the mbuf chain 452 * begins a new record. 453 */ 454 sbappendrecord(sb, m0) 455 register struct sockbuf *sb; 456 register struct mbuf *m0; 457 { 458 register struct mbuf *m; 459 460 if (m0 == 0) 461 return; 462 if (m = sb->sb_mb) 463 while (m->m_nextpkt) 464 m = m->m_nextpkt; 465 /* 466 * Put the first mbuf on the queue. 467 * Note this permits zero length records. 468 */ 469 sballoc(sb, m0); 470 if (m) 471 m->m_nextpkt = m0; 472 else 473 sb->sb_mb = m0; 474 m = m0->m_next; 475 m0->m_next = 0; 476 if (m && (m0->m_flags & M_EOR)) { 477 m0->m_flags &= ~M_EOR; 478 m->m_flags |= M_EOR; 479 } 480 sbcompress(sb, m, m0); 481 } 482 483 /* 484 * As above except that OOB data 485 * is inserted at the beginning of the sockbuf, 486 * but after any other OOB data. 487 */ 488 sbinsertoob(sb, m0) 489 register struct sockbuf *sb; 490 register struct mbuf *m0; 491 { 492 register struct mbuf *m; 493 register struct mbuf **mp; 494 495 if (m0 == 0) 496 return; 497 for (mp = &sb->sb_mb; m = *mp; mp = &((*mp)->m_nextpkt)) { 498 again: 499 switch (m->m_type) { 500 501 case MT_OOBDATA: 502 continue; /* WANT next train */ 503 504 case MT_CONTROL: 505 if (m = m->m_next) 506 goto again; /* inspect THIS train further */ 507 } 508 break; 509 } 510 /* 511 * Put the first mbuf on the queue. 512 * Note this permits zero length records. 513 */ 514 sballoc(sb, m0); 515 m0->m_nextpkt = *mp; 516 *mp = m0; 517 m = m0->m_next; 518 m0->m_next = 0; 519 if (m && (m0->m_flags & M_EOR)) { 520 m0->m_flags &= ~M_EOR; 521 m->m_flags |= M_EOR; 522 } 523 sbcompress(sb, m, m0); 524 } 525 526 /* 527 * Append address and data, and optionally, control (ancillary) data 528 * to the receive queue of a socket. If present, 529 * m0 must include a packet header with total length. 530 * Returns 0 if no space in sockbuf or insufficient mbufs. 531 */ 532 sbappendaddr(sb, asa, m0, control) 533 register struct sockbuf *sb; 534 struct sockaddr *asa; 535 struct mbuf *m0, *control; 536 { 537 register struct mbuf *m, *n; 538 int space = asa->sa_len; 539 540 if (m0 && (m0->m_flags & M_PKTHDR) == 0) 541 panic("sbappendaddr"); 542 if (m0) 543 space += m0->m_pkthdr.len; 544 for (n = control; n; n = n->m_next) { 545 space += n->m_len; 546 if (n->m_next == 0) /* keep pointer to last control buf */ 547 break; 548 } 549 if (space > sbspace(sb)) 550 return (0); 551 if (asa->sa_len > MLEN) 552 return (0); 553 MGET(m, M_DONTWAIT, MT_SONAME); 554 if (m == 0) 555 return (0); 556 m->m_len = asa->sa_len; 557 bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len); 558 if (n) 559 n->m_next = m0; /* concatenate data to control */ 560 else 561 control = m0; 562 m->m_next = control; 563 for (n = m; n; n = n->m_next) 564 sballoc(sb, n); 565 if (n = sb->sb_mb) { 566 while (n->m_nextpkt) 567 n = n->m_nextpkt; 568 n->m_nextpkt = m; 569 } else 570 sb->sb_mb = m; 571 return (1); 572 } 573 574 sbappendcontrol(sb, m0, control) 575 struct sockbuf *sb; 576 struct mbuf *control, *m0; 577 { 578 register struct mbuf *m, *n; 579 int space = 0; 580 581 if (control == 0) 582 panic("sbappendcontrol"); 583 for (m = control; ; m = m->m_next) { 584 space += m->m_len; 585 if (m->m_next == 0) 586 break; 587 } 588 n = m; /* save pointer to last control buffer */ 589 for (m = m0; m; m = m->m_next) 590 space += m->m_len; 591 if (space > sbspace(sb)) 592 return (0); 593 n->m_next = m0; /* concatenate data to control */ 594 for (m = control; m; m = m->m_next) 595 sballoc(sb, m); 596 if (n = sb->sb_mb) { 597 while (n->m_nextpkt) 598 n = n->m_nextpkt; 599 n->m_nextpkt = control; 600 } else 601 sb->sb_mb = control; 602 return (1); 603 } 604 605 /* 606 * Compress mbuf chain m into the socket 607 * buffer sb following mbuf n. If n 608 * is null, the buffer is presumed empty. 609 */ 610 sbcompress(sb, m, n) 611 register struct sockbuf *sb; 612 register struct mbuf *m, *n; 613 { 614 register int eor = 0; 615 register struct mbuf *o; 616 617 while (m) { 618 eor |= m->m_flags & M_EOR; 619 if (m->m_len == 0 && 620 (eor == 0 || 621 (((o = m->m_next) || (o = n)) && 622 o->m_type == m->m_type))) { 623 m = m_free(m); 624 continue; 625 } 626 if (n && (n->m_flags & (M_EXT | M_EOR)) == 0 && 627 (n->m_data + n->m_len + m->m_len) < &n->m_dat[MLEN] && 628 n->m_type == m->m_type) { 629 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len, 630 (unsigned)m->m_len); 631 n->m_len += m->m_len; 632 sb->sb_cc += m->m_len; 633 m = m_free(m); 634 continue; 635 } 636 if (n) 637 n->m_next = m; 638 else 639 sb->sb_mb = m; 640 sballoc(sb, m); 641 n = m; 642 m->m_flags &= ~M_EOR; 643 m = m->m_next; 644 n->m_next = 0; 645 } 646 if (eor) { 647 if (n) 648 n->m_flags |= eor; 649 else 650 printf("semi-panic: sbcompress\n"); 651 } 652 } 653 654 /* 655 * Free all mbufs in a sockbuf. 656 * Check that all resources are reclaimed. 657 */ 658 sbflush(sb) 659 register struct sockbuf *sb; 660 { 661 662 if (sb->sb_flags & SB_LOCK) 663 panic("sbflush"); 664 while (sb->sb_mbcnt) 665 sbdrop(sb, (int)sb->sb_cc); 666 if (sb->sb_cc || sb->sb_mb) 667 panic("sbflush 2"); 668 } 669 670 /* 671 * Drop data from (the front of) a sockbuf. 672 */ 673 sbdrop(sb, len) 674 register struct sockbuf *sb; 675 register int len; 676 { 677 register struct mbuf *m, *mn; 678 struct mbuf *next; 679 680 next = (m = sb->sb_mb) ? m->m_nextpkt : 0; 681 while (len > 0) { 682 if (m == 0) { 683 if (next == 0) 684 panic("sbdrop"); 685 m = next; 686 next = m->m_nextpkt; 687 continue; 688 } 689 if (m->m_len > len) { 690 m->m_len -= len; 691 m->m_data += len; 692 sb->sb_cc -= len; 693 break; 694 } 695 len -= m->m_len; 696 sbfree(sb, m); 697 MFREE(m, mn); 698 m = mn; 699 } 700 while (m && m->m_len == 0) { 701 sbfree(sb, m); 702 MFREE(m, mn); 703 m = mn; 704 } 705 if (m) { 706 sb->sb_mb = m; 707 m->m_nextpkt = next; 708 } else 709 sb->sb_mb = next; 710 } 711 712 /* 713 * Drop a record off the front of a sockbuf 714 * and move the next record to the front. 715 */ 716 sbdroprecord(sb) 717 register struct sockbuf *sb; 718 { 719 register struct mbuf *m, *mn; 720 721 m = sb->sb_mb; 722 if (m) { 723 sb->sb_mb = m->m_nextpkt; 724 do { 725 sbfree(sb, m); 726 MFREE(m, mn); 727 } while (m = mn); 728 } 729 } 730