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_socket.c 7.24 (Berkeley) 11/05/90 8 */ 9 10 #include "param.h" 11 #include "user.h" 12 #include "proc.h" 13 #include "file.h" 14 #include "malloc.h" 15 #include "mbuf.h" 16 #include "domain.h" 17 #include "kernel.h" 18 #include "protosw.h" 19 #include "socket.h" 20 #include "socketvar.h" 21 #include "time.h" 22 23 /* 24 * Socket operation routines. 25 * These routines are called by the routines in 26 * sys_socket.c or from a system process, and 27 * implement the semantics of socket operations by 28 * switching out to the protocol specific routines. 29 * 30 * TODO: 31 * test socketpair 32 * clean up async 33 * out-of-band is a kludge 34 */ 35 /*ARGSUSED*/ 36 socreate(dom, aso, type, proto) 37 struct socket **aso; 38 register int type; 39 int proto; 40 { 41 register struct protosw *prp; 42 register struct socket *so; 43 register int error; 44 45 if (proto) 46 prp = pffindproto(dom, proto, type); 47 else 48 prp = pffindtype(dom, type); 49 if (prp == 0) 50 return (EPROTONOSUPPORT); 51 if (prp->pr_type != type) 52 return (EPROTOTYPE); 53 MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_WAIT); 54 bzero((caddr_t)so, sizeof(*so)); 55 so->so_type = type; 56 if (u.u_uid == 0) 57 so->so_state = SS_PRIV; 58 so->so_proto = prp; 59 error = 60 (*prp->pr_usrreq)(so, PRU_ATTACH, 61 (struct mbuf *)0, (struct mbuf *)proto, (struct mbuf *)0); 62 if (error) { 63 so->so_state |= SS_NOFDREF; 64 sofree(so); 65 return (error); 66 } 67 *aso = so; 68 return (0); 69 } 70 71 sobind(so, nam) 72 struct socket *so; 73 struct mbuf *nam; 74 { 75 int s = splnet(); 76 int error; 77 78 error = 79 (*so->so_proto->pr_usrreq)(so, PRU_BIND, 80 (struct mbuf *)0, nam, (struct mbuf *)0); 81 splx(s); 82 return (error); 83 } 84 85 solisten(so, backlog) 86 register struct socket *so; 87 int backlog; 88 { 89 int s = splnet(), error; 90 91 error = 92 (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, 93 (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); 94 if (error) { 95 splx(s); 96 return (error); 97 } 98 if (so->so_q == 0) 99 so->so_options |= SO_ACCEPTCONN; 100 if (backlog < 0) 101 backlog = 0; 102 so->so_qlimit = min(backlog, SOMAXCONN); 103 splx(s); 104 return (0); 105 } 106 107 sofree(so) 108 register struct socket *so; 109 { 110 111 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) 112 return; 113 if (so->so_head) { 114 if (!soqremque(so, 0) && !soqremque(so, 1)) 115 panic("sofree dq"); 116 so->so_head = 0; 117 } 118 sbrelease(&so->so_snd); 119 sorflush(so); 120 FREE(so, M_SOCKET); 121 } 122 123 /* 124 * Close a socket on last file table reference removal. 125 * Initiate disconnect if connected. 126 * Free socket when disconnect complete. 127 */ 128 soclose(so) 129 register struct socket *so; 130 { 131 int s = splnet(); /* conservative */ 132 int error = 0; 133 134 if (so->so_options & SO_ACCEPTCONN) { 135 while (so->so_q0) 136 (void) soabort(so->so_q0); 137 while (so->so_q) 138 (void) soabort(so->so_q); 139 } 140 if (so->so_pcb == 0) 141 goto discard; 142 if (so->so_state & SS_ISCONNECTED) { 143 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 144 error = sodisconnect(so); 145 if (error) 146 goto drop; 147 } 148 if (so->so_options & SO_LINGER) { 149 if ((so->so_state & SS_ISDISCONNECTING) && 150 (so->so_state & SS_NBIO)) 151 goto drop; 152 while (so->so_state & SS_ISCONNECTED) 153 if (error = tsleep((caddr_t)&so->so_timeo, 154 PSOCK | PCATCH, netcls, so->so_linger)) 155 break; 156 } 157 } 158 drop: 159 if (so->so_pcb) { 160 int error2 = 161 (*so->so_proto->pr_usrreq)(so, PRU_DETACH, 162 (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); 163 if (error == 0) 164 error = error2; 165 } 166 discard: 167 if (so->so_state & SS_NOFDREF) 168 panic("soclose: NOFDREF"); 169 so->so_state |= SS_NOFDREF; 170 sofree(so); 171 splx(s); 172 return (error); 173 } 174 175 /* 176 * Must be called at splnet... 177 */ 178 soabort(so) 179 struct socket *so; 180 { 181 182 return ( 183 (*so->so_proto->pr_usrreq)(so, PRU_ABORT, 184 (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)); 185 } 186 187 soaccept(so, nam) 188 register struct socket *so; 189 struct mbuf *nam; 190 { 191 int s = splnet(); 192 int error; 193 194 if ((so->so_state & SS_NOFDREF) == 0) 195 panic("soaccept: !NOFDREF"); 196 so->so_state &= ~SS_NOFDREF; 197 error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, 198 (struct mbuf *)0, nam, (struct mbuf *)0); 199 splx(s); 200 return (error); 201 } 202 203 soconnect(so, nam) 204 register struct socket *so; 205 struct mbuf *nam; 206 { 207 int s; 208 int error; 209 210 if (so->so_options & SO_ACCEPTCONN) 211 return (EOPNOTSUPP); 212 s = splnet(); 213 /* 214 * If protocol is connection-based, can only connect once. 215 * Otherwise, if connected, try to disconnect first. 216 * This allows user to disconnect by connecting to, e.g., 217 * a null address. 218 */ 219 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 220 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 221 (error = sodisconnect(so)))) 222 error = EISCONN; 223 else 224 error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, 225 (struct mbuf *)0, nam, (struct mbuf *)0); 226 splx(s); 227 return (error); 228 } 229 230 soconnect2(so1, so2) 231 register struct socket *so1; 232 struct socket *so2; 233 { 234 int s = splnet(); 235 int error; 236 237 error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, 238 (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0); 239 splx(s); 240 return (error); 241 } 242 243 sodisconnect(so) 244 register struct socket *so; 245 { 246 int s = splnet(); 247 int error; 248 249 if ((so->so_state & SS_ISCONNECTED) == 0) { 250 error = ENOTCONN; 251 goto bad; 252 } 253 if (so->so_state & SS_ISDISCONNECTING) { 254 error = EALREADY; 255 goto bad; 256 } 257 error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, 258 (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0); 259 bad: 260 splx(s); 261 return (error); 262 } 263 264 /* 265 * Send on a socket. 266 * If send must go all at once and message is larger than 267 * send buffering, then hard error. 268 * Lock against other senders. 269 * If must go all at once and not enough room now, then 270 * inform user that this would block and do nothing. 271 * Otherwise, if nonblocking, send as much as possible. 272 * The data to be sent is described by "uio" if nonzero, 273 * otherwise by the mbuf chain "top" (which must be null 274 * if uio is not). Data provided in mbuf chain must be small 275 * enough to send all at once. 276 * 277 * Returns nonzero on error, timeout or signal; callers 278 * must check for short counts if EINTR/ERESTART are returned. 279 * Data and control buffers are freed on return. 280 */ 281 sosend(so, addr, uio, top, control, flags) 282 register struct socket *so; 283 struct mbuf *addr; 284 struct uio *uio; 285 struct mbuf *top; 286 struct mbuf *control; 287 int flags; 288 { 289 struct mbuf **mp; 290 register struct mbuf *m; 291 register long space, len, resid; 292 int clen = 0, error, s, dontroute, mlen; 293 int atomic = sosendallatonce(so) || top; 294 295 if (uio) 296 resid = uio->uio_resid; 297 else 298 resid = top->m_pkthdr.len; 299 dontroute = 300 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 301 (so->so_proto->pr_flags & PR_ATOMIC); 302 u.u_ru.ru_msgsnd++; 303 if (control) 304 clen = control->m_len; 305 #define snderr(errno) { error = errno; splx(s); goto release; } 306 307 restart: 308 if (error = sblock(&so->so_snd)) 309 goto out; 310 do { 311 s = splnet(); 312 if (so->so_state & SS_CANTSENDMORE) 313 snderr(EPIPE); 314 if (so->so_error) 315 snderr(so->so_error); 316 if ((so->so_state & SS_ISCONNECTED) == 0) { 317 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 318 if ((so->so_state & SS_ISCONFIRMING) == 0) 319 snderr(ENOTCONN); 320 } else if (addr == 0) 321 snderr(EDESTADDRREQ); 322 } 323 space = sbspace(&so->so_snd); 324 if (flags & MSG_OOB) 325 space += 1024; 326 if (space < resid + clen && 327 (atomic || space < so->so_snd.sb_lowat || space < clen)) { 328 if (atomic && resid > so->so_snd.sb_hiwat || 329 clen > so->so_snd.sb_hiwat) 330 snderr(EMSGSIZE); 331 if (so->so_state & SS_NBIO) 332 snderr(EWOULDBLOCK); 333 sbunlock(&so->so_snd); 334 error = sbwait(&so->so_snd); 335 splx(s); 336 if (error) 337 goto out; 338 goto restart; 339 } 340 splx(s); 341 mp = ⊤ 342 space -= clen; 343 do { 344 if (uio == NULL) { 345 /* 346 * Data is prepackaged in "top". 347 */ 348 resid = 0; 349 if (flags & MSG_EOR) 350 top->m_flags |= M_EOR; 351 } else do { 352 if (top == 0) { 353 MGETHDR(m, M_WAIT, MT_DATA); 354 mlen = MHLEN; 355 m->m_pkthdr.len = 0; 356 m->m_pkthdr.rcvif = (struct ifnet *)0; 357 } else { 358 MGET(m, M_WAIT, MT_DATA); 359 mlen = MLEN; 360 } 361 if (resid >= MINCLSIZE && space >= MCLBYTES) { 362 MCLGET(m, M_WAIT); 363 if ((m->m_flags & M_EXT) == 0) 364 goto nopages; 365 mlen = MCLBYTES; 366 #ifdef MAPPED_MBUFS 367 len = min(MCLBYTES, resid); 368 #else 369 if (top == 0) { 370 len = min(MCLBYTES - max_hdr, resid); 371 m->m_data += max_hdr; 372 } 373 #endif 374 space -= MCLBYTES; 375 } else { 376 nopages: 377 len = min(min(mlen, resid), space); 378 space -= len; 379 /* 380 * For datagram protocols, leave room 381 * for protocol headers in first mbuf. 382 */ 383 if (atomic && top == 0 && len < mlen) 384 MH_ALIGN(m, len); 385 } 386 error = uiomove(mtod(m, caddr_t), (int)len, uio); 387 resid = uio->uio_resid; 388 m->m_len = len; 389 *mp = m; 390 top->m_pkthdr.len += len; 391 if (error) 392 goto release; 393 mp = &m->m_next; 394 if (resid <= 0) { 395 if (flags & MSG_EOR) 396 top->m_flags |= M_EOR; 397 break; 398 } 399 } while (space > 0 && atomic); 400 if (dontroute) 401 so->so_options |= SO_DONTROUTE; 402 s = splnet(); /* XXX */ 403 error = (*so->so_proto->pr_usrreq)(so, 404 (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, 405 top, addr, control); 406 splx(s); 407 if (dontroute) 408 so->so_options &= ~SO_DONTROUTE; 409 clen = 0; 410 control = 0; 411 top = 0; 412 mp = ⊤ 413 if (error) 414 goto release; 415 } while (resid && space > 0); 416 } while (resid); 417 418 release: 419 sbunlock(&so->so_snd); 420 out: 421 if (top) 422 m_freem(top); 423 if (control) 424 m_freem(control); 425 return (error); 426 } 427 428 /* 429 * Implement receive operations on a socket. 430 * We depend on the way that records are added to the sockbuf 431 * by sbappend*. In particular, each record (mbufs linked through m_next) 432 * must begin with an address if the protocol so specifies, 433 * followed by an optional mbuf or mbufs containing ancillary data, 434 * and then zero or more mbufs of data. 435 * In order to avoid blocking network interrupts for the entire time here, 436 * we splx() while doing the actual copy to user space. 437 * Although the sockbuf is locked, new data may still be appended, 438 * and thus we must maintain consistency of the sockbuf during that time. 439 * 440 * The caller may receive the data as a single mbuf chain by supplying 441 * an mbuf **mp0 for use in returning the chain. The uio is then used 442 * only for the count in uio_resid. 443 */ 444 soreceive(so, paddr, uio, mp0, controlp, flagsp) 445 register struct socket *so; 446 struct mbuf **paddr; 447 struct uio *uio; 448 struct mbuf **mp0; 449 struct mbuf **controlp; 450 int *flagsp; 451 { 452 register struct mbuf *m, **mp; 453 register int flags, len, error, s, offset; 454 struct protosw *pr = so->so_proto; 455 struct mbuf *nextrecord; 456 int moff, type; 457 458 mp = mp0; 459 if (paddr) 460 *paddr = 0; 461 if (controlp) 462 *controlp = 0; 463 if (flagsp) 464 flags = *flagsp &~ MSG_EOR; 465 else 466 flags = 0; 467 if (flags & MSG_OOB) { 468 m = m_get(M_WAIT, MT_DATA); 469 error = (*pr->pr_usrreq)(so, PRU_RCVOOB, 470 m, (struct mbuf *)(flags & MSG_PEEK), (struct mbuf *)0); 471 if (error) 472 goto bad; 473 do { 474 error = uiomove(mtod(m, caddr_t), 475 (int) min(uio->uio_resid, m->m_len), uio); 476 m = m_free(m); 477 } while (uio->uio_resid && error == 0 && m); 478 bad: 479 if (m) 480 m_freem(m); 481 return (error); 482 } 483 if (mp) 484 *mp = (struct mbuf *)0; 485 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) 486 (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, 487 (struct mbuf *)0, (struct mbuf *)0); 488 489 restart: 490 if (error = sblock(&so->so_rcv)) 491 return (error); 492 s = splnet(); 493 494 m = so->so_rcv.sb_mb; 495 /* 496 * If we have less data than requested, block awaiting more 497 * (subject to any timeout) if: 498 * 1. the current count is less than the low water mark, or 499 * 2. MSG_WAITALL is set, and it is possible to do the entire 500 * receive operation at once if we block (resid <= hiwat). 501 * If MSG_WAITALL is set but resid is larger than the receive buffer, 502 * we have to do the receive in sections, and thus risk returning 503 * a short count if a timeout or signal occurs after we start. 504 */ 505 if (m == 0 || so->so_rcv.sb_cc < uio->uio_resid && 506 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 507 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat))) { 508 #ifdef DIAGNOSTIC 509 if (m == 0 && so->so_rcv.sb_cc) 510 panic("receive 1"); 511 #endif 512 if (so->so_error) { 513 error = so->so_error; 514 so->so_error = 0; 515 goto release; 516 } 517 if (so->so_state & SS_CANTRCVMORE) 518 goto release; 519 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 520 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 521 error = ENOTCONN; 522 goto release; 523 } 524 if (uio->uio_resid == 0) 525 goto release; 526 if (so->so_state & SS_NBIO) { 527 error = EWOULDBLOCK; 528 goto release; 529 } 530 sbunlock(&so->so_rcv); 531 error = sbwait(&so->so_rcv); 532 splx(s); 533 if (error) 534 return (error); 535 goto restart; 536 } 537 u.u_ru.ru_msgrcv++; 538 nextrecord = m->m_nextpkt; 539 if (pr->pr_flags & PR_ADDR) { 540 #ifdef DIAGNOSTIC 541 if (m->m_type != MT_SONAME) 542 panic("receive 1a"); 543 #endif 544 if (flags & MSG_PEEK) { 545 if (paddr) 546 *paddr = m_copy(m, 0, m->m_len); 547 m = m->m_next; 548 } else { 549 sbfree(&so->so_rcv, m); 550 if (paddr) { 551 *paddr = m; 552 so->so_rcv.sb_mb = m->m_next; 553 m->m_next = 0; 554 m = so->so_rcv.sb_mb; 555 } else { 556 MFREE(m, so->so_rcv.sb_mb); 557 m = so->so_rcv.sb_mb; 558 } 559 } 560 } 561 while (m && m->m_type == MT_CONTROL && error == 0) { 562 if (flags & MSG_PEEK) { 563 if (controlp) 564 *controlp = m_copy(m, 0, m->m_len); 565 m = m->m_next; 566 } else { 567 sbfree(&so->so_rcv, m); 568 if (controlp) { 569 if (pr->pr_domain->dom_externalize && 570 mtod(m, struct cmsghdr *)->cmsg_type == 571 SCM_RIGHTS) 572 error = (*pr->pr_domain->dom_externalize)(m); 573 *controlp = m; 574 so->so_rcv.sb_mb = m->m_next; 575 m->m_next = 0; 576 m = so->so_rcv.sb_mb; 577 } else { 578 MFREE(m, so->so_rcv.sb_mb); 579 m = so->so_rcv.sb_mb; 580 } 581 } 582 if (controlp) 583 controlp = &(*controlp)->m_next; 584 } 585 if (m) { 586 if ((flags & MSG_PEEK) == 0) 587 m->m_nextpkt = nextrecord; 588 type = m->m_type; 589 } 590 moff = 0; 591 offset = 0; 592 while (m && m->m_type == type && uio->uio_resid > 0 && error == 0) { 593 if (m->m_type == MT_OOBDATA) 594 flags |= MSG_OOB; 595 #ifdef DIAGNOSTIC 596 else if (m->m_type != MT_DATA && m->m_type != MT_HEADER) 597 panic("receive 3"); 598 #endif 599 so->so_state &= ~SS_RCVATMARK; 600 len = uio->uio_resid; 601 if (so->so_oobmark && len > so->so_oobmark - offset) 602 len = so->so_oobmark - offset; 603 if (len > m->m_len - moff) 604 len = m->m_len - moff; 605 /* 606 * If mp is set, just pass back the mbufs. 607 * Otherwise copy them out via the uio, then free. 608 * Sockbuf must be consistent here (points to current mbuf, 609 * it points to next record) when we drop priority; 610 * we must note any additions to the sockbuf when we 611 * block interrupts again. 612 */ 613 if (mp == 0) { 614 splx(s); 615 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio); 616 s = splnet(); 617 } else 618 uio->uio_resid -= len; 619 if (len == m->m_len - moff) { 620 if (m->m_flags & M_EOR) 621 flags |= MSG_EOR; 622 if (flags & MSG_PEEK) { 623 m = m->m_next; 624 moff = 0; 625 } else { 626 nextrecord = m->m_nextpkt; 627 sbfree(&so->so_rcv, m); 628 if (mp) { 629 *mp = m; 630 mp = &m->m_next; 631 so->so_rcv.sb_mb = m = m->m_next; 632 *mp = (struct mbuf *)0; 633 } else { 634 MFREE(m, so->so_rcv.sb_mb); 635 m = so->so_rcv.sb_mb; 636 } 637 if (m) 638 m->m_nextpkt = nextrecord; 639 } 640 } else { 641 if (flags & MSG_PEEK) 642 moff += len; 643 else { 644 if (mp) 645 *mp = m_copym(m, 0, len, M_WAIT); 646 m->m_data += len; 647 m->m_len -= len; 648 so->so_rcv.sb_cc -= len; 649 } 650 } 651 if (so->so_oobmark) { 652 if ((flags & MSG_PEEK) == 0) { 653 so->so_oobmark -= len; 654 if (so->so_oobmark == 0) { 655 so->so_state |= SS_RCVATMARK; 656 break; 657 } 658 } else 659 offset += len; 660 } 661 if (flags & MSG_EOR) 662 break; 663 /* 664 * If the MSG_WAITALL flag is set (for non-atomic socket), 665 * we must not quit until "uio->uio_resid == 0" or an error 666 * termination. If a signal/timeout occurs, return 667 * with a short count but without error. 668 * Keep sockbuf locked against other readers. 669 */ 670 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 && 671 !sosendallatonce(so)) { 672 error = sbwait(&so->so_rcv); 673 if (error) { 674 sbunlock(&so->so_rcv); 675 splx(s); 676 return (0); 677 } 678 if (m = so->so_rcv.sb_mb) 679 nextrecord = m->m_nextpkt; 680 if (so->so_error || so->so_state & SS_CANTRCVMORE) 681 break; 682 continue; 683 } 684 } 685 if ((flags & MSG_PEEK) == 0) { 686 if (m == 0) 687 so->so_rcv.sb_mb = nextrecord; 688 else if (pr->pr_flags & PR_ATOMIC) { 689 flags |= MSG_TRUNC; 690 (void) sbdroprecord(&so->so_rcv); 691 } 692 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 693 (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, 694 (struct mbuf *)flags, (struct mbuf *)0, 695 (struct mbuf *)0); 696 } 697 if (flagsp) 698 *flagsp |= flags; 699 release: 700 sbunlock(&so->so_rcv); 701 splx(s); 702 return (error); 703 } 704 705 soshutdown(so, how) 706 register struct socket *so; 707 register int how; 708 { 709 register struct protosw *pr = so->so_proto; 710 711 how++; 712 if (how & FREAD) 713 sorflush(so); 714 if (how & FWRITE) 715 return ((*pr->pr_usrreq)(so, PRU_SHUTDOWN, 716 (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)); 717 return (0); 718 } 719 720 sorflush(so) 721 register struct socket *so; 722 { 723 register struct sockbuf *sb = &so->so_rcv; 724 register struct protosw *pr = so->so_proto; 725 register int s; 726 struct sockbuf asb; 727 728 sb->sb_flags |= SB_NOINTR; 729 (void) sblock(sb); 730 s = splimp(); 731 socantrcvmore(so); 732 sbunlock(sb); 733 asb = *sb; 734 bzero((caddr_t)sb, sizeof (*sb)); 735 splx(s); 736 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) 737 (*pr->pr_domain->dom_dispose)(asb.sb_mb); 738 sbrelease(&asb); 739 } 740 741 sosetopt(so, level, optname, m0) 742 register struct socket *so; 743 int level, optname; 744 struct mbuf *m0; 745 { 746 int error = 0; 747 register struct mbuf *m = m0; 748 749 if (level != SOL_SOCKET) { 750 if (so->so_proto && so->so_proto->pr_ctloutput) 751 return ((*so->so_proto->pr_ctloutput) 752 (PRCO_SETOPT, so, level, optname, &m0)); 753 error = ENOPROTOOPT; 754 } else { 755 switch (optname) { 756 757 case SO_LINGER: 758 if (m == NULL || m->m_len != sizeof (struct linger)) { 759 error = EINVAL; 760 goto bad; 761 } 762 so->so_linger = mtod(m, struct linger *)->l_linger; 763 /* fall thru... */ 764 765 case SO_DEBUG: 766 case SO_KEEPALIVE: 767 case SO_DONTROUTE: 768 case SO_USELOOPBACK: 769 case SO_BROADCAST: 770 case SO_REUSEADDR: 771 case SO_OOBINLINE: 772 if (m == NULL || m->m_len < sizeof (int)) { 773 error = EINVAL; 774 goto bad; 775 } 776 if (*mtod(m, int *)) 777 so->so_options |= optname; 778 else 779 so->so_options &= ~optname; 780 break; 781 782 case SO_SNDBUF: 783 case SO_RCVBUF: 784 case SO_SNDLOWAT: 785 case SO_RCVLOWAT: 786 if (m == NULL || m->m_len < sizeof (int)) { 787 error = EINVAL; 788 goto bad; 789 } 790 switch (optname) { 791 792 case SO_SNDBUF: 793 case SO_RCVBUF: 794 if (sbreserve(optname == SO_SNDBUF ? 795 &so->so_snd : &so->so_rcv, 796 (u_long) *mtod(m, int *)) == 0) { 797 error = ENOBUFS; 798 goto bad; 799 } 800 break; 801 802 case SO_SNDLOWAT: 803 so->so_snd.sb_lowat = *mtod(m, int *); 804 break; 805 case SO_RCVLOWAT: 806 so->so_rcv.sb_lowat = *mtod(m, int *); 807 break; 808 } 809 break; 810 811 case SO_SNDTIMEO: 812 case SO_RCVTIMEO: 813 { 814 struct timeval *tv; 815 short val; 816 817 if (m == NULL || m->m_len < sizeof (*tv)) { 818 error = EINVAL; 819 goto bad; 820 } 821 tv = mtod(m, struct timeval *); 822 if (tv->tv_sec > SHRT_MAX / hz - hz) { 823 error = EDOM; 824 goto bad; 825 } 826 val = tv->tv_sec * hz + tv->tv_usec / tick; 827 828 switch (optname) { 829 830 case SO_SNDTIMEO: 831 so->so_snd.sb_timeo = val; 832 break; 833 case SO_RCVTIMEO: 834 so->so_rcv.sb_timeo = val; 835 break; 836 } 837 break; 838 } 839 840 default: 841 error = ENOPROTOOPT; 842 break; 843 } 844 } 845 bad: 846 if (m) 847 (void) m_free(m); 848 return (error); 849 } 850 851 sogetopt(so, level, optname, mp) 852 register struct socket *so; 853 int level, optname; 854 struct mbuf **mp; 855 { 856 register struct mbuf *m; 857 858 if (level != SOL_SOCKET) { 859 if (so->so_proto && so->so_proto->pr_ctloutput) { 860 return ((*so->so_proto->pr_ctloutput) 861 (PRCO_GETOPT, so, level, optname, mp)); 862 } else 863 return (ENOPROTOOPT); 864 } else { 865 m = m_get(M_WAIT, MT_SOOPTS); 866 m->m_len = sizeof (int); 867 868 switch (optname) { 869 870 case SO_LINGER: 871 m->m_len = sizeof (struct linger); 872 mtod(m, struct linger *)->l_onoff = 873 so->so_options & SO_LINGER; 874 mtod(m, struct linger *)->l_linger = so->so_linger; 875 break; 876 877 case SO_USELOOPBACK: 878 case SO_DONTROUTE: 879 case SO_DEBUG: 880 case SO_KEEPALIVE: 881 case SO_REUSEADDR: 882 case SO_BROADCAST: 883 case SO_OOBINLINE: 884 *mtod(m, int *) = so->so_options & optname; 885 break; 886 887 case SO_TYPE: 888 *mtod(m, int *) = so->so_type; 889 break; 890 891 case SO_ERROR: 892 *mtod(m, int *) = so->so_error; 893 so->so_error = 0; 894 break; 895 896 case SO_SNDBUF: 897 *mtod(m, int *) = so->so_snd.sb_hiwat; 898 break; 899 900 case SO_RCVBUF: 901 *mtod(m, int *) = so->so_rcv.sb_hiwat; 902 break; 903 904 case SO_SNDLOWAT: 905 *mtod(m, int *) = so->so_snd.sb_lowat; 906 break; 907 908 case SO_RCVLOWAT: 909 *mtod(m, int *) = so->so_rcv.sb_lowat; 910 break; 911 912 case SO_SNDTIMEO: 913 case SO_RCVTIMEO: 914 { 915 int val = (optname == SO_SNDTIMEO ? 916 so->so_snd.sb_timeo : so->so_rcv.sb_timeo); 917 918 m->m_len = sizeof(struct timeval); 919 mtod(m, struct timeval *)->tv_sec = val / hz; 920 mtod(m, struct timeval *)->tv_usec = 921 (val % hz) / tick; 922 break; 923 } 924 925 default: 926 (void)m_free(m); 927 return (ENOPROTOOPT); 928 } 929 *mp = m; 930 return (0); 931 } 932 } 933 934 sohasoutofband(so) 935 register struct socket *so; 936 { 937 struct proc *p; 938 939 if (so->so_pgid < 0) 940 gsignal(-so->so_pgid, SIGURG); 941 else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0) 942 psignal(p, SIGURG); 943 if (so->so_rcv.sb_sel) { 944 selwakeup(so->so_rcv.sb_sel, so->so_rcv.sb_flags & SB_COLL); 945 so->so_rcv.sb_sel = 0; 946 so->so_rcv.sb_flags &= ~SB_COLL; 947 } 948 } 949