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