35 */ 36 37#include "opt_inet.h" 38#include "opt_mac.h" 39#include "opt_zero.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/fcntl.h> 44#include <sys/lock.h> 45#include <sys/mac.h> 46#include <sys/malloc.h> 47#include <sys/mbuf.h> 48#include <sys/mutex.h> 49#include <sys/domain.h> 50#include <sys/file.h> /* for struct knote */ 51#include <sys/kernel.h> 52#include <sys/malloc.h> 53#include <sys/event.h> 54#include <sys/poll.h> 55#include <sys/proc.h> 56#include <sys/protosw.h> 57#include <sys/socket.h> 58#include <sys/socketvar.h> 59#include <sys/resourcevar.h> 60#include <sys/signalvar.h> 61#include <sys/sysctl.h> 62#include <sys/uio.h> 63#include <sys/jail.h> 64 65#include <vm/uma.h> 66 67#include <machine/limits.h> 68 69#ifdef INET 70static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt); 71#endif 72 73static void filt_sordetach(struct knote *kn); 74static int filt_soread(struct knote *kn, long hint); 75static void filt_sowdetach(struct knote *kn); 76static int filt_sowrite(struct knote *kn, long hint); 77static int filt_solisten(struct knote *kn, long hint); 78 79static struct filterops solisten_filtops = 80 { 1, NULL, filt_sordetach, filt_solisten }; 81static struct filterops soread_filtops = 82 { 1, NULL, filt_sordetach, filt_soread }; 83static struct filterops sowrite_filtops = 84 { 1, NULL, filt_sowdetach, filt_sowrite }; 85 86uma_zone_t socket_zone; 87so_gen_t so_gencnt; /* generation count for sockets */ 88 89MALLOC_DEFINE(M_SONAME, "soname", "socket name"); 90MALLOC_DEFINE(M_PCB, "pcb", "protocol control block"); 91 92SYSCTL_DECL(_kern_ipc); 93 94static int somaxconn = SOMAXCONN; 95SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW, 96 &somaxconn, 0, "Maximum pending socket connection queue size"); 97static int numopensockets; 98SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD, 99 &numopensockets, 0, "Number of open sockets"); 100#ifdef ZERO_COPY_SOCKETS 101/* These aren't static because they're used in other files. */ 102int so_zero_copy_send = 1; 103int so_zero_copy_receive = 1; 104SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0, 105 "Zero copy controls"); 106SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW, 107 &so_zero_copy_receive, 0, "Enable zero copy receive"); 108SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW, 109 &so_zero_copy_send, 0, "Enable zero copy send"); 110#endif /* ZERO_COPY_SOCKETS */ 111 112 113/* 114 * Socket operation routines. 115 * These routines are called by the routines in 116 * sys_socket.c or from a system process, and 117 * implement the semantics of socket operations by 118 * switching out to the protocol specific routines. 119 */ 120 121/* 122 * Get a socket structure from our zone, and initialize it. 123 * Note that it would probably be better to allocate socket 124 * and PCB at the same time, but I'm not convinced that all 125 * the protocols can be easily modified to do this. 126 * 127 * soalloc() returns a socket with a ref count of 0. 128 */ 129struct socket * 130soalloc(waitok) 131 int waitok; 132{ 133 struct socket *so; 134#ifdef MAC 135 int error; 136#endif 137 int flag; 138 139 if (waitok == 1) 140 flag = M_WAITOK; 141 else 142 flag = M_NOWAIT; 143 flag |= M_ZERO; 144 so = uma_zalloc(socket_zone, flag); 145 if (so) { 146#ifdef MAC 147 error = mac_init_socket(so, flag); 148 if (error != 0) { 149 uma_zfree(socket_zone, so); 150 so = NULL; 151 return so; 152 } 153#endif 154 /* XXX race condition for reentrant kernel */ 155 so->so_gencnt = ++so_gencnt; 156 /* sx_init(&so->so_sxlock, "socket sxlock"); */ 157 TAILQ_INIT(&so->so_aiojobq); 158 ++numopensockets; 159 } 160 return so; 161} 162 163/* 164 * socreate returns a socket with a ref count of 1. The socket should be 165 * closed with soclose(). 166 */ 167int 168socreate(dom, aso, type, proto, cred, td) 169 int dom; 170 struct socket **aso; 171 register int type; 172 int proto; 173 struct ucred *cred; 174 struct thread *td; 175{ 176 register struct protosw *prp; 177 register struct socket *so; 178 register int error; 179 180 if (proto) 181 prp = pffindproto(dom, proto, type); 182 else 183 prp = pffindtype(dom, type); 184 185 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0) 186 return (EPROTONOSUPPORT); 187 188 if (jailed(cred) && jail_socket_unixiproute_only && 189 prp->pr_domain->dom_family != PF_LOCAL && 190 prp->pr_domain->dom_family != PF_INET && 191 prp->pr_domain->dom_family != PF_ROUTE) { 192 return (EPROTONOSUPPORT); 193 } 194 195 if (prp->pr_type != type) 196 return (EPROTOTYPE); 197 so = soalloc(M_NOWAIT); 198 if (so == NULL) 199 return (ENOBUFS); 200 201 TAILQ_INIT(&so->so_incomp); 202 TAILQ_INIT(&so->so_comp); 203 so->so_type = type; 204 so->so_cred = crhold(cred); 205 so->so_proto = prp; 206#ifdef MAC 207 mac_create_socket(cred, so); 208#endif 209 soref(so); 210 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td); 211 if (error) { 212 so->so_state |= SS_NOFDREF; 213 sorele(so); 214 return (error); 215 } 216 *aso = so; 217 return (0); 218} 219 220int 221sobind(so, nam, td) 222 struct socket *so; 223 struct sockaddr *nam; 224 struct thread *td; 225{ 226 int s = splnet(); 227 int error; 228 229 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td); 230 splx(s); 231 return (error); 232} 233 234void 235sodealloc(struct socket *so) 236{ 237 238 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count)); 239 so->so_gencnt = ++so_gencnt; 240 if (so->so_rcv.sb_hiwat) 241 (void)chgsbsize(so->so_cred->cr_uidinfo, 242 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY); 243 if (so->so_snd.sb_hiwat) 244 (void)chgsbsize(so->so_cred->cr_uidinfo, 245 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY); 246#ifdef INET 247 if (so->so_accf != NULL) { 248 if (so->so_accf->so_accept_filter != NULL && 249 so->so_accf->so_accept_filter->accf_destroy != NULL) { 250 so->so_accf->so_accept_filter->accf_destroy(so); 251 } 252 if (so->so_accf->so_accept_filter_str != NULL) 253 FREE(so->so_accf->so_accept_filter_str, M_ACCF); 254 FREE(so->so_accf, M_ACCF); 255 } 256#endif 257#ifdef MAC 258 mac_destroy_socket(so); 259#endif 260 crfree(so->so_cred); 261 /* sx_destroy(&so->so_sxlock); */ 262 uma_zfree(socket_zone, so); 263 --numopensockets; 264} 265 266int 267solisten(so, backlog, td) 268 register struct socket *so; 269 int backlog; 270 struct thread *td; 271{ 272 int s, error; 273 274 s = splnet(); 275 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, td); 276 if (error) { 277 splx(s); 278 return (error); 279 } 280 if (TAILQ_EMPTY(&so->so_comp)) 281 so->so_options |= SO_ACCEPTCONN; 282 if (backlog < 0 || backlog > somaxconn) 283 backlog = somaxconn; 284 so->so_qlimit = backlog; 285 splx(s); 286 return (0); 287} 288 289void 290sofree(so) 291 register struct socket *so; 292{ 293 struct socket *head = so->so_head; 294 295 KASSERT(so->so_count == 0, ("socket %p so_count not 0", so)); 296 297 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) 298 return; 299 if (head != NULL) { 300 if (so->so_state & SS_INCOMP) { 301 TAILQ_REMOVE(&head->so_incomp, so, so_list); 302 head->so_incqlen--; 303 } else if (so->so_state & SS_COMP) { 304 /* 305 * We must not decommission a socket that's 306 * on the accept(2) queue. If we do, then 307 * accept(2) may hang after select(2) indicated 308 * that the listening socket was ready. 309 */ 310 return; 311 } else { 312 panic("sofree: not queued"); 313 } 314 so->so_state &= ~SS_INCOMP; 315 so->so_head = NULL; 316 } 317 sbrelease(&so->so_snd, so); 318 sorflush(so); 319 sodealloc(so); 320} 321 322/* 323 * Close a socket on last file table reference removal. 324 * Initiate disconnect if connected. 325 * Free socket when disconnect complete. 326 * 327 * This function will sorele() the socket. Note that soclose() may be 328 * called prior to the ref count reaching zero. The actual socket 329 * structure will not be freed until the ref count reaches zero. 330 */ 331int 332soclose(so) 333 register struct socket *so; 334{ 335 int s = splnet(); /* conservative */ 336 int error = 0; 337 338 funsetown(&so->so_sigio); 339 if (so->so_options & SO_ACCEPTCONN) { 340 struct socket *sp, *sonext; 341 342 sp = TAILQ_FIRST(&so->so_incomp); 343 for (; sp != NULL; sp = sonext) { 344 sonext = TAILQ_NEXT(sp, so_list); 345 (void) soabort(sp); 346 } 347 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) { 348 sonext = TAILQ_NEXT(sp, so_list); 349 /* Dequeue from so_comp since sofree() won't do it */ 350 TAILQ_REMOVE(&so->so_comp, sp, so_list); 351 so->so_qlen--; 352 sp->so_state &= ~SS_COMP; 353 sp->so_head = NULL; 354 (void) soabort(sp); 355 } 356 } 357 if (so->so_pcb == 0) 358 goto discard; 359 if (so->so_state & SS_ISCONNECTED) { 360 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 361 error = sodisconnect(so); 362 if (error) 363 goto drop; 364 } 365 if (so->so_options & SO_LINGER) { 366 if ((so->so_state & SS_ISDISCONNECTING) && 367 (so->so_state & SS_NBIO)) 368 goto drop; 369 while (so->so_state & SS_ISCONNECTED) { 370 error = tsleep(&so->so_timeo, 371 PSOCK | PCATCH, "soclos", so->so_linger * hz); 372 if (error) 373 break; 374 } 375 } 376 } 377drop: 378 if (so->so_pcb) { 379 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so); 380 if (error == 0) 381 error = error2; 382 } 383discard: 384 if (so->so_state & SS_NOFDREF) 385 panic("soclose: NOFDREF"); 386 so->so_state |= SS_NOFDREF; 387 sorele(so); 388 splx(s); 389 return (error); 390} 391 392/* 393 * Must be called at splnet... 394 */ 395int 396soabort(so) 397 struct socket *so; 398{ 399 int error; 400 401 error = (*so->so_proto->pr_usrreqs->pru_abort)(so); 402 if (error) { 403 sotryfree(so); /* note: does not decrement the ref count */ 404 return error; 405 } 406 return (0); 407} 408 409int 410soaccept(so, nam) 411 register struct socket *so; 412 struct sockaddr **nam; 413{ 414 int s = splnet(); 415 int error; 416 417 if ((so->so_state & SS_NOFDREF) == 0) 418 panic("soaccept: !NOFDREF"); 419 so->so_state &= ~SS_NOFDREF; 420 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam); 421 splx(s); 422 return (error); 423} 424 425int 426soconnect(so, nam, td) 427 register struct socket *so; 428 struct sockaddr *nam; 429 struct thread *td; 430{ 431 int s; 432 int error; 433 434 if (so->so_options & SO_ACCEPTCONN) 435 return (EOPNOTSUPP); 436 s = splnet(); 437 /* 438 * If protocol is connection-based, can only connect once. 439 * Otherwise, if connected, try to disconnect first. 440 * This allows user to disconnect by connecting to, e.g., 441 * a null address. 442 */ 443 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 444 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 445 (error = sodisconnect(so)))) 446 error = EISCONN; 447 else 448 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td); 449 splx(s); 450 return (error); 451} 452 453int 454soconnect2(so1, so2) 455 register struct socket *so1; 456 struct socket *so2; 457{ 458 int s = splnet(); 459 int error; 460 461 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2); 462 splx(s); 463 return (error); 464} 465 466int 467sodisconnect(so) 468 register struct socket *so; 469{ 470 int s = splnet(); 471 int error; 472 473 if ((so->so_state & SS_ISCONNECTED) == 0) { 474 error = ENOTCONN; 475 goto bad; 476 } 477 if (so->so_state & SS_ISDISCONNECTING) { 478 error = EALREADY; 479 goto bad; 480 } 481 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so); 482bad: 483 splx(s); 484 return (error); 485} 486 487#define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 488/* 489 * Send on a socket. 490 * If send must go all at once and message is larger than 491 * send buffering, then hard error. 492 * Lock against other senders. 493 * If must go all at once and not enough room now, then 494 * inform user that this would block and do nothing. 495 * Otherwise, if nonblocking, send as much as possible. 496 * The data to be sent is described by "uio" if nonzero, 497 * otherwise by the mbuf chain "top" (which must be null 498 * if uio is not). Data provided in mbuf chain must be small 499 * enough to send all at once. 500 * 501 * Returns nonzero on error, timeout or signal; callers 502 * must check for short counts if EINTR/ERESTART are returned. 503 * Data and control buffers are freed on return. 504 */ 505 506#ifdef ZERO_COPY_SOCKETS 507struct so_zerocopy_stats{ 508 int size_ok; 509 int align_ok; 510 int found_ifp; 511}; 512struct so_zerocopy_stats so_zerocp_stats = {0,0,0}; 513#include <netinet/in.h> 514#include <net/route.h> 515#include <netinet/in_pcb.h> 516#include <vm/vm.h> 517#include <vm/vm_page.h> 518#include <vm/vm_object.h> 519#endif /*ZERO_COPY_SOCKETS*/ 520 521int 522sosend(so, addr, uio, top, control, flags, td) 523 register struct socket *so; 524 struct sockaddr *addr; 525 struct uio *uio; 526 struct mbuf *top; 527 struct mbuf *control; 528 int flags; 529 struct thread *td; 530{ 531 struct mbuf **mp; 532 register struct mbuf *m; 533 register long space, len, resid; 534 int clen = 0, error, s, dontroute, mlen; 535 int atomic = sosendallatonce(so) || top; 536#ifdef ZERO_COPY_SOCKETS 537 int cow_send; 538#endif /* ZERO_COPY_SOCKETS */ 539 540 if (uio) 541 resid = uio->uio_resid; 542 else 543 resid = top->m_pkthdr.len; 544 /* 545 * In theory resid should be unsigned. 546 * However, space must be signed, as it might be less than 0 547 * if we over-committed, and we must use a signed comparison 548 * of space and resid. On the other hand, a negative resid 549 * causes us to loop sending 0-length segments to the protocol. 550 * 551 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 552 * type sockets since that's an error. 553 */ 554 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) { 555 error = EINVAL; 556 goto out; 557 } 558 559 dontroute = 560 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 561 (so->so_proto->pr_flags & PR_ATOMIC); 562 if (td) 563 td->td_proc->p_stats->p_ru.ru_msgsnd++; 564 if (control) 565 clen = control->m_len; 566#define snderr(errno) { error = (errno); splx(s); goto release; } 567 568restart: 569 error = sblock(&so->so_snd, SBLOCKWAIT(flags)); 570 if (error) 571 goto out; 572 do { 573 s = splnet(); 574 if (so->so_state & SS_CANTSENDMORE) 575 snderr(EPIPE); 576 if (so->so_error) { 577 error = so->so_error; 578 so->so_error = 0; 579 splx(s); 580 goto release; 581 } 582 if ((so->so_state & SS_ISCONNECTED) == 0) { 583 /* 584 * `sendto' and `sendmsg' is allowed on a connection- 585 * based socket if it supports implied connect. 586 * Return ENOTCONN if not connected and no address is 587 * supplied. 588 */ 589 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 590 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 591 if ((so->so_state & SS_ISCONFIRMING) == 0 && 592 !(resid == 0 && clen != 0)) 593 snderr(ENOTCONN); 594 } else if (addr == 0) 595 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ? 596 ENOTCONN : EDESTADDRREQ); 597 } 598 space = sbspace(&so->so_snd); 599 if (flags & MSG_OOB) 600 space += 1024; 601 if ((atomic && resid > so->so_snd.sb_hiwat) || 602 clen > so->so_snd.sb_hiwat) 603 snderr(EMSGSIZE); 604 if (space < resid + clen && 605 (atomic || space < so->so_snd.sb_lowat || space < clen)) { 606 if (so->so_state & SS_NBIO) 607 snderr(EWOULDBLOCK); 608 sbunlock(&so->so_snd); 609 error = sbwait(&so->so_snd); 610 splx(s); 611 if (error) 612 goto out; 613 goto restart; 614 } 615 splx(s); 616 mp = ⊤ 617 space -= clen; 618 do { 619 if (uio == NULL) { 620 /* 621 * Data is prepackaged in "top". 622 */ 623 resid = 0; 624 if (flags & MSG_EOR) 625 top->m_flags |= M_EOR; 626 } else do { 627#ifdef ZERO_COPY_SOCKETS 628 cow_send = 0; 629#endif /* ZERO_COPY_SOCKETS */ 630 if (top == 0) { 631 MGETHDR(m, M_TRYWAIT, MT_DATA); 632 if (m == NULL) { 633 error = ENOBUFS; 634 goto release; 635 } 636 mlen = MHLEN; 637 m->m_pkthdr.len = 0; 638 m->m_pkthdr.rcvif = (struct ifnet *)0; 639 } else { 640 MGET(m, M_TRYWAIT, MT_DATA); 641 if (m == NULL) { 642 error = ENOBUFS; 643 goto release; 644 } 645 mlen = MLEN; 646 } 647 if (resid >= MINCLSIZE) { 648#ifdef ZERO_COPY_SOCKETS 649 if (so_zero_copy_send && 650 resid>=PAGE_SIZE && 651 space>=PAGE_SIZE && 652 uio->uio_iov->iov_len>=PAGE_SIZE) { 653 so_zerocp_stats.size_ok++; 654 if (!((vm_offset_t) 655 uio->uio_iov->iov_base & PAGE_MASK)){ 656 so_zerocp_stats.align_ok++; 657 cow_send = socow_setup(m, uio); 658 } 659 } 660 if (!cow_send){ 661#endif /* ZERO_COPY_SOCKETS */ 662 MCLGET(m, M_TRYWAIT); 663 if ((m->m_flags & M_EXT) == 0) 664 goto nopages; 665 mlen = MCLBYTES; 666 len = min(min(mlen, resid), space); 667 } else { 668#ifdef ZERO_COPY_SOCKETS 669 len = PAGE_SIZE; 670 } 671 672 } else { 673#endif /* ZERO_COPY_SOCKETS */ 674nopages: 675 len = min(min(mlen, resid), space); 676 /* 677 * For datagram protocols, leave room 678 * for protocol headers in first mbuf. 679 */ 680 if (atomic && top == 0 && len < mlen) 681 MH_ALIGN(m, len); 682 } 683 space -= len; 684#ifdef ZERO_COPY_SOCKETS 685 if (cow_send) 686 error = 0; 687 else 688#endif /* ZERO_COPY_SOCKETS */ 689 error = uiomove(mtod(m, caddr_t), (int)len, uio); 690 resid = uio->uio_resid; 691 m->m_len = len; 692 *mp = m; 693 top->m_pkthdr.len += len; 694 if (error) 695 goto release; 696 mp = &m->m_next; 697 if (resid <= 0) { 698 if (flags & MSG_EOR) 699 top->m_flags |= M_EOR; 700 break; 701 } 702 } while (space > 0 && atomic); 703 if (dontroute) 704 so->so_options |= SO_DONTROUTE; 705 s = splnet(); /* XXX */ 706 /* 707 * XXX all the SS_CANTSENDMORE checks previously 708 * done could be out of date. We could have recieved 709 * a reset packet in an interrupt or maybe we slept 710 * while doing page faults in uiomove() etc. We could 711 * probably recheck again inside the splnet() protection 712 * here, but there are probably other places that this 713 * also happens. We must rethink this. 714 */ 715 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 716 (flags & MSG_OOB) ? PRUS_OOB : 717 /* 718 * If the user set MSG_EOF, the protocol 719 * understands this flag and nothing left to 720 * send then use PRU_SEND_EOF instead of PRU_SEND. 721 */ 722 ((flags & MSG_EOF) && 723 (so->so_proto->pr_flags & PR_IMPLOPCL) && 724 (resid <= 0)) ? 725 PRUS_EOF : 726 /* If there is more to send set PRUS_MORETOCOME */ 727 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 728 top, addr, control, td); 729 splx(s); 730 if (dontroute) 731 so->so_options &= ~SO_DONTROUTE; 732 clen = 0; 733 control = 0; 734 top = 0; 735 mp = ⊤ 736 if (error) 737 goto release; 738 } while (resid && space > 0); 739 } while (resid); 740 741release: 742 sbunlock(&so->so_snd); 743out: 744 if (top) 745 m_freem(top); 746 if (control) 747 m_freem(control); 748 return (error); 749} 750 751/* 752 * Implement receive operations on a socket. 753 * We depend on the way that records are added to the sockbuf 754 * by sbappend*. In particular, each record (mbufs linked through m_next) 755 * must begin with an address if the protocol so specifies, 756 * followed by an optional mbuf or mbufs containing ancillary data, 757 * and then zero or more mbufs of data. 758 * In order to avoid blocking network interrupts for the entire time here, 759 * we splx() while doing the actual copy to user space. 760 * Although the sockbuf is locked, new data may still be appended, 761 * and thus we must maintain consistency of the sockbuf during that time. 762 * 763 * The caller may receive the data as a single mbuf chain by supplying 764 * an mbuf **mp0 for use in returning the chain. The uio is then used 765 * only for the count in uio_resid. 766 */ 767int 768soreceive(so, psa, uio, mp0, controlp, flagsp) 769 register struct socket *so; 770 struct sockaddr **psa; 771 struct uio *uio; 772 struct mbuf **mp0; 773 struct mbuf **controlp; 774 int *flagsp; 775{ 776 struct mbuf *m, **mp; 777 register int flags, len, error, s, offset; 778 struct protosw *pr = so->so_proto; 779 struct mbuf *nextrecord; 780 int moff, type = 0; 781 int orig_resid = uio->uio_resid; 782 783 mp = mp0; 784 if (psa) 785 *psa = 0; 786 if (controlp) 787 *controlp = 0; 788 if (flagsp) 789 flags = *flagsp &~ MSG_EOR; 790 else 791 flags = 0; 792 if (flags & MSG_OOB) { 793 m = m_get(M_TRYWAIT, MT_DATA); 794 if (m == NULL) 795 return (ENOBUFS); 796 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK); 797 if (error) 798 goto bad; 799 do { 800#ifdef ZERO_COPY_SOCKETS 801 if (so_zero_copy_receive) { 802 vm_page_t pg; 803 int disposable; 804 805 if ((m->m_flags & M_EXT) 806 && (m->m_ext.ext_type == EXT_DISPOSABLE)) 807 disposable = 1; 808 else 809 disposable = 0; 810 811 pg = PHYS_TO_VM_PAGE(vtophys(mtod(m, caddr_t))); 812 if (uio->uio_offset == -1) 813 uio->uio_offset =IDX_TO_OFF(pg->pindex); 814 815 error = uiomoveco(mtod(m, caddr_t), 816 min(uio->uio_resid, m->m_len), 817 uio, pg->object, 818 disposable); 819 } else 820#endif /* ZERO_COPY_SOCKETS */ 821 error = uiomove(mtod(m, caddr_t), 822 (int) min(uio->uio_resid, m->m_len), uio); 823 m = m_free(m); 824 } while (uio->uio_resid && error == 0 && m); 825bad: 826 if (m) 827 m_freem(m); 828 return (error); 829 } 830 if (mp) 831 *mp = (struct mbuf *)0; 832 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) 833 (*pr->pr_usrreqs->pru_rcvd)(so, 0); 834 835restart: 836 error = sblock(&so->so_rcv, SBLOCKWAIT(flags)); 837 if (error) 838 return (error); 839 s = splnet(); 840 841 m = so->so_rcv.sb_mb; 842 /* 843 * If we have less data than requested, block awaiting more 844 * (subject to any timeout) if: 845 * 1. the current count is less than the low water mark, or 846 * 2. MSG_WAITALL is set, and it is possible to do the entire 847 * receive operation at once if we block (resid <= hiwat). 848 * 3. MSG_DONTWAIT is not set 849 * If MSG_WAITALL is set but resid is larger than the receive buffer, 850 * we have to do the receive in sections, and thus risk returning 851 * a short count if a timeout or signal occurs after we start. 852 */ 853 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 && 854 so->so_rcv.sb_cc < uio->uio_resid) && 855 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 856 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && 857 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) { 858 KASSERT(m != 0 || !so->so_rcv.sb_cc, 859 ("receive: m == %p so->so_rcv.sb_cc == %u", 860 m, so->so_rcv.sb_cc)); 861 if (so->so_error) { 862 if (m) 863 goto dontblock; 864 error = so->so_error; 865 if ((flags & MSG_PEEK) == 0) 866 so->so_error = 0; 867 goto release; 868 } 869 if (so->so_state & SS_CANTRCVMORE) { 870 if (m) 871 goto dontblock; 872 else 873 goto release; 874 } 875 for (; m; m = m->m_next) 876 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 877 m = so->so_rcv.sb_mb; 878 goto dontblock; 879 } 880 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 881 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 882 error = ENOTCONN; 883 goto release; 884 } 885 if (uio->uio_resid == 0) 886 goto release; 887 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) { 888 error = EWOULDBLOCK; 889 goto release; 890 } 891 sbunlock(&so->so_rcv); 892 error = sbwait(&so->so_rcv); 893 splx(s); 894 if (error) 895 return (error); 896 goto restart; 897 } 898dontblock: 899 if (uio->uio_td) 900 uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++; 901 nextrecord = m->m_nextpkt; 902 if (pr->pr_flags & PR_ADDR) { 903 KASSERT(m->m_type == MT_SONAME, 904 ("m->m_type == %d", m->m_type)); 905 orig_resid = 0; 906 if (psa) 907 *psa = dup_sockaddr(mtod(m, struct sockaddr *), 908 mp0 == 0); 909 if (flags & MSG_PEEK) { 910 m = m->m_next; 911 } else { 912 sbfree(&so->so_rcv, m); 913 so->so_rcv.sb_mb = m_free(m); 914 m = so->so_rcv.sb_mb; 915 } 916 } 917 while (m && m->m_type == MT_CONTROL && error == 0) { 918 if (flags & MSG_PEEK) { 919 if (controlp) 920 *controlp = m_copy(m, 0, m->m_len); 921 m = m->m_next; 922 } else { 923 sbfree(&so->so_rcv, m); 924 so->so_rcv.sb_mb = m->m_next; 925 m->m_next = NULL; 926 if (pr->pr_domain->dom_externalize) 927 error = 928 (*pr->pr_domain->dom_externalize)(m, controlp); 929 else if (controlp) 930 *controlp = m; 931 else 932 m_freem(m); 933 m = so->so_rcv.sb_mb; 934 } 935 if (controlp) { 936 orig_resid = 0; 937 do 938 controlp = &(*controlp)->m_next; 939 while (*controlp != NULL); 940 } 941 } 942 if (m) { 943 if ((flags & MSG_PEEK) == 0) 944 m->m_nextpkt = nextrecord; 945 type = m->m_type; 946 if (type == MT_OOBDATA) 947 flags |= MSG_OOB; 948 } 949 moff = 0; 950 offset = 0; 951 while (m && uio->uio_resid > 0 && error == 0) { 952 if (m->m_type == MT_OOBDATA) { 953 if (type != MT_OOBDATA) 954 break; 955 } else if (type == MT_OOBDATA) 956 break; 957 else 958 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER, 959 ("m->m_type == %d", m->m_type)); 960 so->so_state &= ~SS_RCVATMARK; 961 len = uio->uio_resid; 962 if (so->so_oobmark && len > so->so_oobmark - offset) 963 len = so->so_oobmark - offset; 964 if (len > m->m_len - moff) 965 len = m->m_len - moff; 966 /* 967 * If mp is set, just pass back the mbufs. 968 * Otherwise copy them out via the uio, then free. 969 * Sockbuf must be consistent here (points to current mbuf, 970 * it points to next record) when we drop priority; 971 * we must note any additions to the sockbuf when we 972 * block interrupts again. 973 */ 974 if (mp == 0) { 975 splx(s); 976#ifdef ZERO_COPY_SOCKETS 977 if (so_zero_copy_receive) { 978 vm_page_t pg; 979 int disposable; 980 981 if ((m->m_flags & M_EXT) 982 && (m->m_ext.ext_type == EXT_DISPOSABLE)) 983 disposable = 1; 984 else 985 disposable = 0; 986 987 pg = PHYS_TO_VM_PAGE(vtophys(mtod(m, caddr_t) + 988 moff)); 989 990 if (uio->uio_offset == -1) 991 uio->uio_offset =IDX_TO_OFF(pg->pindex); 992 993 error = uiomoveco(mtod(m, caddr_t) + moff, 994 (int)len, uio,pg->object, 995 disposable); 996 } else 997#endif /* ZERO_COPY_SOCKETS */ 998 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio); 999 s = splnet(); 1000 if (error) 1001 goto release; 1002 } else 1003 uio->uio_resid -= len; 1004 if (len == m->m_len - moff) { 1005 if (m->m_flags & M_EOR) 1006 flags |= MSG_EOR; 1007 if (flags & MSG_PEEK) { 1008 m = m->m_next; 1009 moff = 0; 1010 } else { 1011 nextrecord = m->m_nextpkt; 1012 sbfree(&so->so_rcv, m); 1013 if (mp) { 1014 *mp = m; 1015 mp = &m->m_next; 1016 so->so_rcv.sb_mb = m = m->m_next; 1017 *mp = (struct mbuf *)0; 1018 } else { 1019 so->so_rcv.sb_mb = m_free(m); 1020 m = so->so_rcv.sb_mb; 1021 } 1022 if (m) 1023 m->m_nextpkt = nextrecord; 1024 } 1025 } else { 1026 if (flags & MSG_PEEK) 1027 moff += len; 1028 else { 1029 if (mp) 1030 *mp = m_copym(m, 0, len, M_TRYWAIT); 1031 m->m_data += len; 1032 m->m_len -= len; 1033 so->so_rcv.sb_cc -= len; 1034 } 1035 } 1036 if (so->so_oobmark) { 1037 if ((flags & MSG_PEEK) == 0) { 1038 so->so_oobmark -= len; 1039 if (so->so_oobmark == 0) { 1040 so->so_state |= SS_RCVATMARK; 1041 break; 1042 } 1043 } else { 1044 offset += len; 1045 if (offset == so->so_oobmark) 1046 break; 1047 } 1048 } 1049 if (flags & MSG_EOR) 1050 break; 1051 /* 1052 * If the MSG_WAITALL flag is set (for non-atomic socket), 1053 * we must not quit until "uio->uio_resid == 0" or an error 1054 * termination. If a signal/timeout occurs, return 1055 * with a short count but without error. 1056 * Keep sockbuf locked against other readers. 1057 */ 1058 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 && 1059 !sosendallatonce(so) && !nextrecord) { 1060 if (so->so_error || so->so_state & SS_CANTRCVMORE) 1061 break; 1062 /* 1063 * Notify the protocol that some data has been 1064 * drained before blocking. 1065 */ 1066 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 1067 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1068 error = sbwait(&so->so_rcv); 1069 if (error) { 1070 sbunlock(&so->so_rcv); 1071 splx(s); 1072 return (0); 1073 } 1074 m = so->so_rcv.sb_mb; 1075 if (m) 1076 nextrecord = m->m_nextpkt; 1077 } 1078 } 1079 1080 if (m && pr->pr_flags & PR_ATOMIC) { 1081 flags |= MSG_TRUNC; 1082 if ((flags & MSG_PEEK) == 0) 1083 (void) sbdroprecord(&so->so_rcv); 1084 } 1085 if ((flags & MSG_PEEK) == 0) { 1086 if (m == 0) 1087 so->so_rcv.sb_mb = nextrecord; 1088 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 1089 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1090 } 1091 if (orig_resid == uio->uio_resid && orig_resid && 1092 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { 1093 sbunlock(&so->so_rcv); 1094 splx(s); 1095 goto restart; 1096 } 1097 1098 if (flagsp) 1099 *flagsp |= flags; 1100release: 1101 sbunlock(&so->so_rcv); 1102 splx(s); 1103 return (error); 1104} 1105 1106int 1107soshutdown(so, how) 1108 register struct socket *so; 1109 register int how; 1110{ 1111 register struct protosw *pr = so->so_proto; 1112 1113 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) 1114 return (EINVAL); 1115 1116 if (how != SHUT_WR) 1117 sorflush(so); 1118 if (how != SHUT_RD) 1119 return ((*pr->pr_usrreqs->pru_shutdown)(so)); 1120 return (0); 1121} 1122 1123void 1124sorflush(so) 1125 register struct socket *so; 1126{ 1127 register struct sockbuf *sb = &so->so_rcv; 1128 register struct protosw *pr = so->so_proto; 1129 register int s; 1130 struct sockbuf asb; 1131 1132 sb->sb_flags |= SB_NOINTR; 1133 (void) sblock(sb, M_WAITOK); 1134 s = splimp(); 1135 socantrcvmore(so); 1136 sbunlock(sb); 1137 asb = *sb; 1138 bzero(sb, sizeof (*sb)); 1139 splx(s); 1140 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) 1141 (*pr->pr_domain->dom_dispose)(asb.sb_mb); 1142 sbrelease(&asb, so); 1143} 1144 1145#ifdef INET 1146static int 1147do_setopt_accept_filter(so, sopt) 1148 struct socket *so; 1149 struct sockopt *sopt; 1150{ 1151 struct accept_filter_arg *afap = NULL; 1152 struct accept_filter *afp; 1153 struct so_accf *af = so->so_accf; 1154 int error = 0; 1155 1156 /* do not set/remove accept filters on non listen sockets */ 1157 if ((so->so_options & SO_ACCEPTCONN) == 0) { 1158 error = EINVAL; 1159 goto out; 1160 } 1161 1162 /* removing the filter */ 1163 if (sopt == NULL) { 1164 if (af != NULL) { 1165 if (af->so_accept_filter != NULL && 1166 af->so_accept_filter->accf_destroy != NULL) { 1167 af->so_accept_filter->accf_destroy(so); 1168 } 1169 if (af->so_accept_filter_str != NULL) { 1170 FREE(af->so_accept_filter_str, M_ACCF); 1171 } 1172 FREE(af, M_ACCF); 1173 so->so_accf = NULL; 1174 } 1175 so->so_options &= ~SO_ACCEPTFILTER; 1176 return (0); 1177 } 1178 /* adding a filter */ 1179 /* must remove previous filter first */ 1180 if (af != NULL) { 1181 error = EINVAL; 1182 goto out; 1183 } 1184 /* don't put large objects on the kernel stack */ 1185 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK); 1186 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap); 1187 afap->af_name[sizeof(afap->af_name)-1] = '\0'; 1188 afap->af_arg[sizeof(afap->af_arg)-1] = '\0'; 1189 if (error) 1190 goto out; 1191 afp = accept_filt_get(afap->af_name); 1192 if (afp == NULL) { 1193 error = ENOENT; 1194 goto out; 1195 } 1196 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK | M_ZERO); 1197 if (afp->accf_create != NULL) { 1198 if (afap->af_name[0] != '\0') { 1199 int len = strlen(afap->af_name) + 1; 1200 1201 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK); 1202 strcpy(af->so_accept_filter_str, afap->af_name); 1203 } 1204 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg); 1205 if (af->so_accept_filter_arg == NULL) { 1206 FREE(af->so_accept_filter_str, M_ACCF); 1207 FREE(af, M_ACCF); 1208 so->so_accf = NULL; 1209 error = EINVAL; 1210 goto out; 1211 } 1212 } 1213 af->so_accept_filter = afp; 1214 so->so_accf = af; 1215 so->so_options |= SO_ACCEPTFILTER; 1216out: 1217 if (afap != NULL) 1218 FREE(afap, M_TEMP); 1219 return (error); 1220} 1221#endif /* INET */ 1222 1223/* 1224 * Perhaps this routine, and sooptcopyout(), below, ought to come in 1225 * an additional variant to handle the case where the option value needs 1226 * to be some kind of integer, but not a specific size. 1227 * In addition to their use here, these functions are also called by the 1228 * protocol-level pr_ctloutput() routines. 1229 */ 1230int 1231sooptcopyin(sopt, buf, len, minlen) 1232 struct sockopt *sopt; 1233 void *buf; 1234 size_t len; 1235 size_t minlen; 1236{ 1237 size_t valsize; 1238 1239 /* 1240 * If the user gives us more than we wanted, we ignore it, 1241 * but if we don't get the minimum length the caller 1242 * wants, we return EINVAL. On success, sopt->sopt_valsize 1243 * is set to however much we actually retrieved. 1244 */ 1245 if ((valsize = sopt->sopt_valsize) < minlen) 1246 return EINVAL; 1247 if (valsize > len) 1248 sopt->sopt_valsize = valsize = len; 1249 1250 if (sopt->sopt_td != 0) 1251 return (copyin(sopt->sopt_val, buf, valsize)); 1252 1253 bcopy(sopt->sopt_val, buf, valsize); 1254 return 0; 1255} 1256 1257int 1258sosetopt(so, sopt) 1259 struct socket *so; 1260 struct sockopt *sopt; 1261{ 1262 int error, optval; 1263 struct linger l; 1264 struct timeval tv; 1265 u_long val; 1266#ifdef MAC 1267 struct mac extmac; 1268#endif 1269 1270 error = 0; 1271 if (sopt->sopt_level != SOL_SOCKET) { 1272 if (so->so_proto && so->so_proto->pr_ctloutput) 1273 return ((*so->so_proto->pr_ctloutput) 1274 (so, sopt)); 1275 error = ENOPROTOOPT; 1276 } else { 1277 switch (sopt->sopt_name) { 1278#ifdef INET 1279 case SO_ACCEPTFILTER: 1280 error = do_setopt_accept_filter(so, sopt); 1281 if (error) 1282 goto bad; 1283 break; 1284#endif 1285 case SO_LINGER: 1286 error = sooptcopyin(sopt, &l, sizeof l, sizeof l); 1287 if (error) 1288 goto bad; 1289 1290 so->so_linger = l.l_linger; 1291 if (l.l_onoff) 1292 so->so_options |= SO_LINGER; 1293 else 1294 so->so_options &= ~SO_LINGER; 1295 break; 1296 1297 case SO_DEBUG: 1298 case SO_KEEPALIVE: 1299 case SO_DONTROUTE: 1300 case SO_USELOOPBACK: 1301 case SO_BROADCAST: 1302 case SO_REUSEADDR: 1303 case SO_REUSEPORT: 1304 case SO_OOBINLINE: 1305 case SO_TIMESTAMP: 1306 case SO_NOSIGPIPE: 1307 error = sooptcopyin(sopt, &optval, sizeof optval, 1308 sizeof optval); 1309 if (error) 1310 goto bad; 1311 if (optval) 1312 so->so_options |= sopt->sopt_name; 1313 else 1314 so->so_options &= ~sopt->sopt_name; 1315 break; 1316 1317 case SO_SNDBUF: 1318 case SO_RCVBUF: 1319 case SO_SNDLOWAT: 1320 case SO_RCVLOWAT: 1321 error = sooptcopyin(sopt, &optval, sizeof optval, 1322 sizeof optval); 1323 if (error) 1324 goto bad; 1325 1326 /* 1327 * Values < 1 make no sense for any of these 1328 * options, so disallow them. 1329 */ 1330 if (optval < 1) { 1331 error = EINVAL; 1332 goto bad; 1333 } 1334 1335 switch (sopt->sopt_name) { 1336 case SO_SNDBUF: 1337 case SO_RCVBUF: 1338 if (sbreserve(sopt->sopt_name == SO_SNDBUF ? 1339 &so->so_snd : &so->so_rcv, (u_long)optval, 1340 so, curthread) == 0) { 1341 error = ENOBUFS; 1342 goto bad; 1343 } 1344 break; 1345 1346 /* 1347 * Make sure the low-water is never greater than 1348 * the high-water. 1349 */ 1350 case SO_SNDLOWAT: 1351 so->so_snd.sb_lowat = 1352 (optval > so->so_snd.sb_hiwat) ? 1353 so->so_snd.sb_hiwat : optval; 1354 break; 1355 case SO_RCVLOWAT: 1356 so->so_rcv.sb_lowat = 1357 (optval > so->so_rcv.sb_hiwat) ? 1358 so->so_rcv.sb_hiwat : optval; 1359 break; 1360 } 1361 break; 1362 1363 case SO_SNDTIMEO: 1364 case SO_RCVTIMEO: 1365 error = sooptcopyin(sopt, &tv, sizeof tv, 1366 sizeof tv); 1367 if (error) 1368 goto bad; 1369 1370 /* assert(hz > 0); */ 1371 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz || 1372 tv.tv_usec < 0 || tv.tv_usec >= 1000000) { 1373 error = EDOM; 1374 goto bad; 1375 } 1376 /* assert(tick > 0); */ 1377 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */ 1378 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick; 1379 if (val > SHRT_MAX) { 1380 error = EDOM; 1381 goto bad; 1382 } 1383 if (val == 0 && tv.tv_usec != 0) 1384 val = 1; 1385 1386 switch (sopt->sopt_name) { 1387 case SO_SNDTIMEO: 1388 so->so_snd.sb_timeo = val; 1389 break; 1390 case SO_RCVTIMEO: 1391 so->so_rcv.sb_timeo = val; 1392 break; 1393 } 1394 break; 1395 case SO_LABEL: 1396#ifdef MAC 1397 error = sooptcopyin(sopt, &extmac, sizeof extmac, 1398 sizeof extmac); 1399 if (error) 1400 goto bad; 1401 1402 error = mac_setsockopt_label_set( 1403 sopt->sopt_td->td_ucred, so, &extmac); 1404 1405#else 1406 error = EOPNOTSUPP; 1407#endif 1408 break; 1409 default: 1410 error = ENOPROTOOPT; 1411 break; 1412 } 1413 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) { 1414 (void) ((*so->so_proto->pr_ctloutput) 1415 (so, sopt)); 1416 } 1417 } 1418bad: 1419 return (error); 1420} 1421 1422/* Helper routine for getsockopt */ 1423int 1424sooptcopyout(sopt, buf, len) 1425 struct sockopt *sopt; 1426 void *buf; 1427 size_t len; 1428{ 1429 int error; 1430 size_t valsize; 1431 1432 error = 0; 1433 1434 /* 1435 * Documented get behavior is that we always return a value, 1436 * possibly truncated to fit in the user's buffer. 1437 * Traditional behavior is that we always tell the user 1438 * precisely how much we copied, rather than something useful 1439 * like the total amount we had available for her. 1440 * Note that this interface is not idempotent; the entire answer must 1441 * generated ahead of time. 1442 */ 1443 valsize = min(len, sopt->sopt_valsize); 1444 sopt->sopt_valsize = valsize; 1445 if (sopt->sopt_val != 0) { 1446 if (sopt->sopt_td != 0) 1447 error = copyout(buf, sopt->sopt_val, valsize); 1448 else 1449 bcopy(buf, sopt->sopt_val, valsize); 1450 } 1451 return error; 1452} 1453 1454int 1455sogetopt(so, sopt) 1456 struct socket *so; 1457 struct sockopt *sopt; 1458{ 1459 int error, optval; 1460 struct linger l; 1461 struct timeval tv; 1462#ifdef INET 1463 struct accept_filter_arg *afap; 1464#endif 1465#ifdef MAC 1466 struct mac extmac; 1467#endif 1468 1469 error = 0; 1470 if (sopt->sopt_level != SOL_SOCKET) { 1471 if (so->so_proto && so->so_proto->pr_ctloutput) { 1472 return ((*so->so_proto->pr_ctloutput) 1473 (so, sopt)); 1474 } else 1475 return (ENOPROTOOPT); 1476 } else { 1477 switch (sopt->sopt_name) { 1478#ifdef INET 1479 case SO_ACCEPTFILTER: 1480 if ((so->so_options & SO_ACCEPTCONN) == 0) 1481 return (EINVAL); 1482 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), 1483 M_TEMP, M_WAITOK | M_ZERO); 1484 if ((so->so_options & SO_ACCEPTFILTER) != 0) { 1485 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name); 1486 if (so->so_accf->so_accept_filter_str != NULL) 1487 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str); 1488 } 1489 error = sooptcopyout(sopt, afap, sizeof(*afap)); 1490 FREE(afap, M_TEMP); 1491 break; 1492#endif 1493 1494 case SO_LINGER: 1495 l.l_onoff = so->so_options & SO_LINGER; 1496 l.l_linger = so->so_linger; 1497 error = sooptcopyout(sopt, &l, sizeof l); 1498 break; 1499 1500 case SO_USELOOPBACK: 1501 case SO_DONTROUTE: 1502 case SO_DEBUG: 1503 case SO_KEEPALIVE: 1504 case SO_REUSEADDR: 1505 case SO_REUSEPORT: 1506 case SO_BROADCAST: 1507 case SO_OOBINLINE: 1508 case SO_TIMESTAMP: 1509 case SO_NOSIGPIPE: 1510 optval = so->so_options & sopt->sopt_name; 1511integer: 1512 error = sooptcopyout(sopt, &optval, sizeof optval); 1513 break; 1514 1515 case SO_TYPE: 1516 optval = so->so_type; 1517 goto integer; 1518 1519 case SO_ERROR: 1520 optval = so->so_error; 1521 so->so_error = 0; 1522 goto integer; 1523 1524 case SO_SNDBUF: 1525 optval = so->so_snd.sb_hiwat; 1526 goto integer; 1527 1528 case SO_RCVBUF: 1529 optval = so->so_rcv.sb_hiwat; 1530 goto integer; 1531 1532 case SO_SNDLOWAT: 1533 optval = so->so_snd.sb_lowat; 1534 goto integer; 1535 1536 case SO_RCVLOWAT: 1537 optval = so->so_rcv.sb_lowat; 1538 goto integer; 1539 1540 case SO_SNDTIMEO: 1541 case SO_RCVTIMEO: 1542 optval = (sopt->sopt_name == SO_SNDTIMEO ? 1543 so->so_snd.sb_timeo : so->so_rcv.sb_timeo); 1544 1545 tv.tv_sec = optval / hz; 1546 tv.tv_usec = (optval % hz) * tick; 1547 error = sooptcopyout(sopt, &tv, sizeof tv); 1548 break; 1549 case SO_LABEL: 1550#ifdef MAC 1551 error = mac_getsockopt_label_get( 1552 sopt->sopt_td->td_ucred, so, &extmac); 1553 if (error) 1554 return (error); 1555 error = sooptcopyout(sopt, &extmac, sizeof extmac); 1556#else 1557 error = EOPNOTSUPP; 1558#endif 1559 break; 1560 case SO_PEERLABEL: 1561#ifdef MAC 1562 error = mac_getsockopt_peerlabel_get( 1563 sopt->sopt_td->td_ucred, so, &extmac); 1564 if (error) 1565 return (error); 1566 error = sooptcopyout(sopt, &extmac, sizeof extmac); 1567#else 1568 error = EOPNOTSUPP; 1569#endif 1570 break; 1571 default: 1572 error = ENOPROTOOPT; 1573 break; 1574 } 1575 return (error); 1576 } 1577} 1578 1579/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 1580int 1581soopt_getm(struct sockopt *sopt, struct mbuf **mp) 1582{ 1583 struct mbuf *m, *m_prev; 1584 int sopt_size = sopt->sopt_valsize; 1585 1586 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 1587 if (m == 0) 1588 return ENOBUFS; 1589 if (sopt_size > MLEN) { 1590 MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT); 1591 if ((m->m_flags & M_EXT) == 0) { 1592 m_free(m); 1593 return ENOBUFS; 1594 } 1595 m->m_len = min(MCLBYTES, sopt_size); 1596 } else { 1597 m->m_len = min(MLEN, sopt_size); 1598 } 1599 sopt_size -= m->m_len; 1600 *mp = m; 1601 m_prev = m; 1602 1603 while (sopt_size) { 1604 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 1605 if (m == 0) { 1606 m_freem(*mp); 1607 return ENOBUFS; 1608 } 1609 if (sopt_size > MLEN) { 1610 MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT); 1611 if ((m->m_flags & M_EXT) == 0) { 1612 m_freem(*mp); 1613 return ENOBUFS; 1614 } 1615 m->m_len = min(MCLBYTES, sopt_size); 1616 } else { 1617 m->m_len = min(MLEN, sopt_size); 1618 } 1619 sopt_size -= m->m_len; 1620 m_prev->m_next = m; 1621 m_prev = m; 1622 } 1623 return 0; 1624} 1625 1626/* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */ 1627int 1628soopt_mcopyin(struct sockopt *sopt, struct mbuf *m) 1629{ 1630 struct mbuf *m0 = m; 1631 1632 if (sopt->sopt_val == NULL) 1633 return 0; 1634 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 1635 if (sopt->sopt_td != NULL) { 1636 int error; 1637 1638 error = copyin(sopt->sopt_val, mtod(m, char *), 1639 m->m_len); 1640 if (error != 0) { 1641 m_freem(m0); 1642 return(error); 1643 } 1644 } else 1645 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len); 1646 sopt->sopt_valsize -= m->m_len; 1647 (caddr_t)sopt->sopt_val += m->m_len; 1648 m = m->m_next; 1649 } 1650 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */ 1651 panic("ip6_sooptmcopyin"); 1652 return 0; 1653} 1654 1655/* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */ 1656int 1657soopt_mcopyout(struct sockopt *sopt, struct mbuf *m) 1658{ 1659 struct mbuf *m0 = m; 1660 size_t valsize = 0; 1661 1662 if (sopt->sopt_val == NULL) 1663 return 0; 1664 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 1665 if (sopt->sopt_td != NULL) { 1666 int error; 1667 1668 error = copyout(mtod(m, char *), sopt->sopt_val, 1669 m->m_len); 1670 if (error != 0) { 1671 m_freem(m0); 1672 return(error); 1673 } 1674 } else 1675 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len); 1676 sopt->sopt_valsize -= m->m_len; 1677 (caddr_t)sopt->sopt_val += m->m_len; 1678 valsize += m->m_len; 1679 m = m->m_next; 1680 } 1681 if (m != NULL) { 1682 /* enough soopt buffer should be given from user-land */ 1683 m_freem(m0); 1684 return(EINVAL); 1685 } 1686 sopt->sopt_valsize = valsize; 1687 return 0; 1688} 1689 1690void 1691sohasoutofband(so) 1692 register struct socket *so; 1693{ 1694 if (so->so_sigio != NULL) 1695 pgsigio(&so->so_sigio, SIGURG, 0); 1696 selwakeup(&so->so_rcv.sb_sel); 1697} 1698 1699int 1700sopoll(struct socket *so, int events, struct ucred *active_cred, 1701 struct thread *td) 1702{ 1703 int revents = 0; 1704 int s = splnet(); 1705 1706 if (events & (POLLIN | POLLRDNORM)) 1707 if (soreadable(so)) 1708 revents |= events & (POLLIN | POLLRDNORM); 1709 1710 if (events & POLLINIGNEOF) 1711 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat || 1712 !TAILQ_EMPTY(&so->so_comp) || so->so_error) 1713 revents |= POLLINIGNEOF; 1714 1715 if (events & (POLLOUT | POLLWRNORM)) 1716 if (sowriteable(so)) 1717 revents |= events & (POLLOUT | POLLWRNORM); 1718 1719 if (events & (POLLPRI | POLLRDBAND)) 1720 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) 1721 revents |= events & (POLLPRI | POLLRDBAND); 1722 1723 if (revents == 0) { 1724 if (events & 1725 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | 1726 POLLRDBAND)) { 1727 selrecord(td, &so->so_rcv.sb_sel); 1728 so->so_rcv.sb_flags |= SB_SEL; 1729 } 1730 1731 if (events & (POLLOUT | POLLWRNORM)) { 1732 selrecord(td, &so->so_snd.sb_sel); 1733 so->so_snd.sb_flags |= SB_SEL; 1734 } 1735 } 1736 1737 splx(s); 1738 return (revents); 1739} 1740 1741int
| 35 */ 36 37#include "opt_inet.h" 38#include "opt_mac.h" 39#include "opt_zero.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/fcntl.h> 44#include <sys/lock.h> 45#include <sys/mac.h> 46#include <sys/malloc.h> 47#include <sys/mbuf.h> 48#include <sys/mutex.h> 49#include <sys/domain.h> 50#include <sys/file.h> /* for struct knote */ 51#include <sys/kernel.h> 52#include <sys/malloc.h> 53#include <sys/event.h> 54#include <sys/poll.h> 55#include <sys/proc.h> 56#include <sys/protosw.h> 57#include <sys/socket.h> 58#include <sys/socketvar.h> 59#include <sys/resourcevar.h> 60#include <sys/signalvar.h> 61#include <sys/sysctl.h> 62#include <sys/uio.h> 63#include <sys/jail.h> 64 65#include <vm/uma.h> 66 67#include <machine/limits.h> 68 69#ifdef INET 70static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt); 71#endif 72 73static void filt_sordetach(struct knote *kn); 74static int filt_soread(struct knote *kn, long hint); 75static void filt_sowdetach(struct knote *kn); 76static int filt_sowrite(struct knote *kn, long hint); 77static int filt_solisten(struct knote *kn, long hint); 78 79static struct filterops solisten_filtops = 80 { 1, NULL, filt_sordetach, filt_solisten }; 81static struct filterops soread_filtops = 82 { 1, NULL, filt_sordetach, filt_soread }; 83static struct filterops sowrite_filtops = 84 { 1, NULL, filt_sowdetach, filt_sowrite }; 85 86uma_zone_t socket_zone; 87so_gen_t so_gencnt; /* generation count for sockets */ 88 89MALLOC_DEFINE(M_SONAME, "soname", "socket name"); 90MALLOC_DEFINE(M_PCB, "pcb", "protocol control block"); 91 92SYSCTL_DECL(_kern_ipc); 93 94static int somaxconn = SOMAXCONN; 95SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW, 96 &somaxconn, 0, "Maximum pending socket connection queue size"); 97static int numopensockets; 98SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD, 99 &numopensockets, 0, "Number of open sockets"); 100#ifdef ZERO_COPY_SOCKETS 101/* These aren't static because they're used in other files. */ 102int so_zero_copy_send = 1; 103int so_zero_copy_receive = 1; 104SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0, 105 "Zero copy controls"); 106SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW, 107 &so_zero_copy_receive, 0, "Enable zero copy receive"); 108SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW, 109 &so_zero_copy_send, 0, "Enable zero copy send"); 110#endif /* ZERO_COPY_SOCKETS */ 111 112 113/* 114 * Socket operation routines. 115 * These routines are called by the routines in 116 * sys_socket.c or from a system process, and 117 * implement the semantics of socket operations by 118 * switching out to the protocol specific routines. 119 */ 120 121/* 122 * Get a socket structure from our zone, and initialize it. 123 * Note that it would probably be better to allocate socket 124 * and PCB at the same time, but I'm not convinced that all 125 * the protocols can be easily modified to do this. 126 * 127 * soalloc() returns a socket with a ref count of 0. 128 */ 129struct socket * 130soalloc(waitok) 131 int waitok; 132{ 133 struct socket *so; 134#ifdef MAC 135 int error; 136#endif 137 int flag; 138 139 if (waitok == 1) 140 flag = M_WAITOK; 141 else 142 flag = M_NOWAIT; 143 flag |= M_ZERO; 144 so = uma_zalloc(socket_zone, flag); 145 if (so) { 146#ifdef MAC 147 error = mac_init_socket(so, flag); 148 if (error != 0) { 149 uma_zfree(socket_zone, so); 150 so = NULL; 151 return so; 152 } 153#endif 154 /* XXX race condition for reentrant kernel */ 155 so->so_gencnt = ++so_gencnt; 156 /* sx_init(&so->so_sxlock, "socket sxlock"); */ 157 TAILQ_INIT(&so->so_aiojobq); 158 ++numopensockets; 159 } 160 return so; 161} 162 163/* 164 * socreate returns a socket with a ref count of 1. The socket should be 165 * closed with soclose(). 166 */ 167int 168socreate(dom, aso, type, proto, cred, td) 169 int dom; 170 struct socket **aso; 171 register int type; 172 int proto; 173 struct ucred *cred; 174 struct thread *td; 175{ 176 register struct protosw *prp; 177 register struct socket *so; 178 register int error; 179 180 if (proto) 181 prp = pffindproto(dom, proto, type); 182 else 183 prp = pffindtype(dom, type); 184 185 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0) 186 return (EPROTONOSUPPORT); 187 188 if (jailed(cred) && jail_socket_unixiproute_only && 189 prp->pr_domain->dom_family != PF_LOCAL && 190 prp->pr_domain->dom_family != PF_INET && 191 prp->pr_domain->dom_family != PF_ROUTE) { 192 return (EPROTONOSUPPORT); 193 } 194 195 if (prp->pr_type != type) 196 return (EPROTOTYPE); 197 so = soalloc(M_NOWAIT); 198 if (so == NULL) 199 return (ENOBUFS); 200 201 TAILQ_INIT(&so->so_incomp); 202 TAILQ_INIT(&so->so_comp); 203 so->so_type = type; 204 so->so_cred = crhold(cred); 205 so->so_proto = prp; 206#ifdef MAC 207 mac_create_socket(cred, so); 208#endif 209 soref(so); 210 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td); 211 if (error) { 212 so->so_state |= SS_NOFDREF; 213 sorele(so); 214 return (error); 215 } 216 *aso = so; 217 return (0); 218} 219 220int 221sobind(so, nam, td) 222 struct socket *so; 223 struct sockaddr *nam; 224 struct thread *td; 225{ 226 int s = splnet(); 227 int error; 228 229 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td); 230 splx(s); 231 return (error); 232} 233 234void 235sodealloc(struct socket *so) 236{ 237 238 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count)); 239 so->so_gencnt = ++so_gencnt; 240 if (so->so_rcv.sb_hiwat) 241 (void)chgsbsize(so->so_cred->cr_uidinfo, 242 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY); 243 if (so->so_snd.sb_hiwat) 244 (void)chgsbsize(so->so_cred->cr_uidinfo, 245 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY); 246#ifdef INET 247 if (so->so_accf != NULL) { 248 if (so->so_accf->so_accept_filter != NULL && 249 so->so_accf->so_accept_filter->accf_destroy != NULL) { 250 so->so_accf->so_accept_filter->accf_destroy(so); 251 } 252 if (so->so_accf->so_accept_filter_str != NULL) 253 FREE(so->so_accf->so_accept_filter_str, M_ACCF); 254 FREE(so->so_accf, M_ACCF); 255 } 256#endif 257#ifdef MAC 258 mac_destroy_socket(so); 259#endif 260 crfree(so->so_cred); 261 /* sx_destroy(&so->so_sxlock); */ 262 uma_zfree(socket_zone, so); 263 --numopensockets; 264} 265 266int 267solisten(so, backlog, td) 268 register struct socket *so; 269 int backlog; 270 struct thread *td; 271{ 272 int s, error; 273 274 s = splnet(); 275 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, td); 276 if (error) { 277 splx(s); 278 return (error); 279 } 280 if (TAILQ_EMPTY(&so->so_comp)) 281 so->so_options |= SO_ACCEPTCONN; 282 if (backlog < 0 || backlog > somaxconn) 283 backlog = somaxconn; 284 so->so_qlimit = backlog; 285 splx(s); 286 return (0); 287} 288 289void 290sofree(so) 291 register struct socket *so; 292{ 293 struct socket *head = so->so_head; 294 295 KASSERT(so->so_count == 0, ("socket %p so_count not 0", so)); 296 297 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) 298 return; 299 if (head != NULL) { 300 if (so->so_state & SS_INCOMP) { 301 TAILQ_REMOVE(&head->so_incomp, so, so_list); 302 head->so_incqlen--; 303 } else if (so->so_state & SS_COMP) { 304 /* 305 * We must not decommission a socket that's 306 * on the accept(2) queue. If we do, then 307 * accept(2) may hang after select(2) indicated 308 * that the listening socket was ready. 309 */ 310 return; 311 } else { 312 panic("sofree: not queued"); 313 } 314 so->so_state &= ~SS_INCOMP; 315 so->so_head = NULL; 316 } 317 sbrelease(&so->so_snd, so); 318 sorflush(so); 319 sodealloc(so); 320} 321 322/* 323 * Close a socket on last file table reference removal. 324 * Initiate disconnect if connected. 325 * Free socket when disconnect complete. 326 * 327 * This function will sorele() the socket. Note that soclose() may be 328 * called prior to the ref count reaching zero. The actual socket 329 * structure will not be freed until the ref count reaches zero. 330 */ 331int 332soclose(so) 333 register struct socket *so; 334{ 335 int s = splnet(); /* conservative */ 336 int error = 0; 337 338 funsetown(&so->so_sigio); 339 if (so->so_options & SO_ACCEPTCONN) { 340 struct socket *sp, *sonext; 341 342 sp = TAILQ_FIRST(&so->so_incomp); 343 for (; sp != NULL; sp = sonext) { 344 sonext = TAILQ_NEXT(sp, so_list); 345 (void) soabort(sp); 346 } 347 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) { 348 sonext = TAILQ_NEXT(sp, so_list); 349 /* Dequeue from so_comp since sofree() won't do it */ 350 TAILQ_REMOVE(&so->so_comp, sp, so_list); 351 so->so_qlen--; 352 sp->so_state &= ~SS_COMP; 353 sp->so_head = NULL; 354 (void) soabort(sp); 355 } 356 } 357 if (so->so_pcb == 0) 358 goto discard; 359 if (so->so_state & SS_ISCONNECTED) { 360 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 361 error = sodisconnect(so); 362 if (error) 363 goto drop; 364 } 365 if (so->so_options & SO_LINGER) { 366 if ((so->so_state & SS_ISDISCONNECTING) && 367 (so->so_state & SS_NBIO)) 368 goto drop; 369 while (so->so_state & SS_ISCONNECTED) { 370 error = tsleep(&so->so_timeo, 371 PSOCK | PCATCH, "soclos", so->so_linger * hz); 372 if (error) 373 break; 374 } 375 } 376 } 377drop: 378 if (so->so_pcb) { 379 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so); 380 if (error == 0) 381 error = error2; 382 } 383discard: 384 if (so->so_state & SS_NOFDREF) 385 panic("soclose: NOFDREF"); 386 so->so_state |= SS_NOFDREF; 387 sorele(so); 388 splx(s); 389 return (error); 390} 391 392/* 393 * Must be called at splnet... 394 */ 395int 396soabort(so) 397 struct socket *so; 398{ 399 int error; 400 401 error = (*so->so_proto->pr_usrreqs->pru_abort)(so); 402 if (error) { 403 sotryfree(so); /* note: does not decrement the ref count */ 404 return error; 405 } 406 return (0); 407} 408 409int 410soaccept(so, nam) 411 register struct socket *so; 412 struct sockaddr **nam; 413{ 414 int s = splnet(); 415 int error; 416 417 if ((so->so_state & SS_NOFDREF) == 0) 418 panic("soaccept: !NOFDREF"); 419 so->so_state &= ~SS_NOFDREF; 420 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam); 421 splx(s); 422 return (error); 423} 424 425int 426soconnect(so, nam, td) 427 register struct socket *so; 428 struct sockaddr *nam; 429 struct thread *td; 430{ 431 int s; 432 int error; 433 434 if (so->so_options & SO_ACCEPTCONN) 435 return (EOPNOTSUPP); 436 s = splnet(); 437 /* 438 * If protocol is connection-based, can only connect once. 439 * Otherwise, if connected, try to disconnect first. 440 * This allows user to disconnect by connecting to, e.g., 441 * a null address. 442 */ 443 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 444 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 445 (error = sodisconnect(so)))) 446 error = EISCONN; 447 else 448 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td); 449 splx(s); 450 return (error); 451} 452 453int 454soconnect2(so1, so2) 455 register struct socket *so1; 456 struct socket *so2; 457{ 458 int s = splnet(); 459 int error; 460 461 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2); 462 splx(s); 463 return (error); 464} 465 466int 467sodisconnect(so) 468 register struct socket *so; 469{ 470 int s = splnet(); 471 int error; 472 473 if ((so->so_state & SS_ISCONNECTED) == 0) { 474 error = ENOTCONN; 475 goto bad; 476 } 477 if (so->so_state & SS_ISDISCONNECTING) { 478 error = EALREADY; 479 goto bad; 480 } 481 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so); 482bad: 483 splx(s); 484 return (error); 485} 486 487#define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 488/* 489 * Send on a socket. 490 * If send must go all at once and message is larger than 491 * send buffering, then hard error. 492 * Lock against other senders. 493 * If must go all at once and not enough room now, then 494 * inform user that this would block and do nothing. 495 * Otherwise, if nonblocking, send as much as possible. 496 * The data to be sent is described by "uio" if nonzero, 497 * otherwise by the mbuf chain "top" (which must be null 498 * if uio is not). Data provided in mbuf chain must be small 499 * enough to send all at once. 500 * 501 * Returns nonzero on error, timeout or signal; callers 502 * must check for short counts if EINTR/ERESTART are returned. 503 * Data and control buffers are freed on return. 504 */ 505 506#ifdef ZERO_COPY_SOCKETS 507struct so_zerocopy_stats{ 508 int size_ok; 509 int align_ok; 510 int found_ifp; 511}; 512struct so_zerocopy_stats so_zerocp_stats = {0,0,0}; 513#include <netinet/in.h> 514#include <net/route.h> 515#include <netinet/in_pcb.h> 516#include <vm/vm.h> 517#include <vm/vm_page.h> 518#include <vm/vm_object.h> 519#endif /*ZERO_COPY_SOCKETS*/ 520 521int 522sosend(so, addr, uio, top, control, flags, td) 523 register struct socket *so; 524 struct sockaddr *addr; 525 struct uio *uio; 526 struct mbuf *top; 527 struct mbuf *control; 528 int flags; 529 struct thread *td; 530{ 531 struct mbuf **mp; 532 register struct mbuf *m; 533 register long space, len, resid; 534 int clen = 0, error, s, dontroute, mlen; 535 int atomic = sosendallatonce(so) || top; 536#ifdef ZERO_COPY_SOCKETS 537 int cow_send; 538#endif /* ZERO_COPY_SOCKETS */ 539 540 if (uio) 541 resid = uio->uio_resid; 542 else 543 resid = top->m_pkthdr.len; 544 /* 545 * In theory resid should be unsigned. 546 * However, space must be signed, as it might be less than 0 547 * if we over-committed, and we must use a signed comparison 548 * of space and resid. On the other hand, a negative resid 549 * causes us to loop sending 0-length segments to the protocol. 550 * 551 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 552 * type sockets since that's an error. 553 */ 554 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) { 555 error = EINVAL; 556 goto out; 557 } 558 559 dontroute = 560 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 561 (so->so_proto->pr_flags & PR_ATOMIC); 562 if (td) 563 td->td_proc->p_stats->p_ru.ru_msgsnd++; 564 if (control) 565 clen = control->m_len; 566#define snderr(errno) { error = (errno); splx(s); goto release; } 567 568restart: 569 error = sblock(&so->so_snd, SBLOCKWAIT(flags)); 570 if (error) 571 goto out; 572 do { 573 s = splnet(); 574 if (so->so_state & SS_CANTSENDMORE) 575 snderr(EPIPE); 576 if (so->so_error) { 577 error = so->so_error; 578 so->so_error = 0; 579 splx(s); 580 goto release; 581 } 582 if ((so->so_state & SS_ISCONNECTED) == 0) { 583 /* 584 * `sendto' and `sendmsg' is allowed on a connection- 585 * based socket if it supports implied connect. 586 * Return ENOTCONN if not connected and no address is 587 * supplied. 588 */ 589 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 590 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 591 if ((so->so_state & SS_ISCONFIRMING) == 0 && 592 !(resid == 0 && clen != 0)) 593 snderr(ENOTCONN); 594 } else if (addr == 0) 595 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ? 596 ENOTCONN : EDESTADDRREQ); 597 } 598 space = sbspace(&so->so_snd); 599 if (flags & MSG_OOB) 600 space += 1024; 601 if ((atomic && resid > so->so_snd.sb_hiwat) || 602 clen > so->so_snd.sb_hiwat) 603 snderr(EMSGSIZE); 604 if (space < resid + clen && 605 (atomic || space < so->so_snd.sb_lowat || space < clen)) { 606 if (so->so_state & SS_NBIO) 607 snderr(EWOULDBLOCK); 608 sbunlock(&so->so_snd); 609 error = sbwait(&so->so_snd); 610 splx(s); 611 if (error) 612 goto out; 613 goto restart; 614 } 615 splx(s); 616 mp = ⊤ 617 space -= clen; 618 do { 619 if (uio == NULL) { 620 /* 621 * Data is prepackaged in "top". 622 */ 623 resid = 0; 624 if (flags & MSG_EOR) 625 top->m_flags |= M_EOR; 626 } else do { 627#ifdef ZERO_COPY_SOCKETS 628 cow_send = 0; 629#endif /* ZERO_COPY_SOCKETS */ 630 if (top == 0) { 631 MGETHDR(m, M_TRYWAIT, MT_DATA); 632 if (m == NULL) { 633 error = ENOBUFS; 634 goto release; 635 } 636 mlen = MHLEN; 637 m->m_pkthdr.len = 0; 638 m->m_pkthdr.rcvif = (struct ifnet *)0; 639 } else { 640 MGET(m, M_TRYWAIT, MT_DATA); 641 if (m == NULL) { 642 error = ENOBUFS; 643 goto release; 644 } 645 mlen = MLEN; 646 } 647 if (resid >= MINCLSIZE) { 648#ifdef ZERO_COPY_SOCKETS 649 if (so_zero_copy_send && 650 resid>=PAGE_SIZE && 651 space>=PAGE_SIZE && 652 uio->uio_iov->iov_len>=PAGE_SIZE) { 653 so_zerocp_stats.size_ok++; 654 if (!((vm_offset_t) 655 uio->uio_iov->iov_base & PAGE_MASK)){ 656 so_zerocp_stats.align_ok++; 657 cow_send = socow_setup(m, uio); 658 } 659 } 660 if (!cow_send){ 661#endif /* ZERO_COPY_SOCKETS */ 662 MCLGET(m, M_TRYWAIT); 663 if ((m->m_flags & M_EXT) == 0) 664 goto nopages; 665 mlen = MCLBYTES; 666 len = min(min(mlen, resid), space); 667 } else { 668#ifdef ZERO_COPY_SOCKETS 669 len = PAGE_SIZE; 670 } 671 672 } else { 673#endif /* ZERO_COPY_SOCKETS */ 674nopages: 675 len = min(min(mlen, resid), space); 676 /* 677 * For datagram protocols, leave room 678 * for protocol headers in first mbuf. 679 */ 680 if (atomic && top == 0 && len < mlen) 681 MH_ALIGN(m, len); 682 } 683 space -= len; 684#ifdef ZERO_COPY_SOCKETS 685 if (cow_send) 686 error = 0; 687 else 688#endif /* ZERO_COPY_SOCKETS */ 689 error = uiomove(mtod(m, caddr_t), (int)len, uio); 690 resid = uio->uio_resid; 691 m->m_len = len; 692 *mp = m; 693 top->m_pkthdr.len += len; 694 if (error) 695 goto release; 696 mp = &m->m_next; 697 if (resid <= 0) { 698 if (flags & MSG_EOR) 699 top->m_flags |= M_EOR; 700 break; 701 } 702 } while (space > 0 && atomic); 703 if (dontroute) 704 so->so_options |= SO_DONTROUTE; 705 s = splnet(); /* XXX */ 706 /* 707 * XXX all the SS_CANTSENDMORE checks previously 708 * done could be out of date. We could have recieved 709 * a reset packet in an interrupt or maybe we slept 710 * while doing page faults in uiomove() etc. We could 711 * probably recheck again inside the splnet() protection 712 * here, but there are probably other places that this 713 * also happens. We must rethink this. 714 */ 715 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 716 (flags & MSG_OOB) ? PRUS_OOB : 717 /* 718 * If the user set MSG_EOF, the protocol 719 * understands this flag and nothing left to 720 * send then use PRU_SEND_EOF instead of PRU_SEND. 721 */ 722 ((flags & MSG_EOF) && 723 (so->so_proto->pr_flags & PR_IMPLOPCL) && 724 (resid <= 0)) ? 725 PRUS_EOF : 726 /* If there is more to send set PRUS_MORETOCOME */ 727 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 728 top, addr, control, td); 729 splx(s); 730 if (dontroute) 731 so->so_options &= ~SO_DONTROUTE; 732 clen = 0; 733 control = 0; 734 top = 0; 735 mp = ⊤ 736 if (error) 737 goto release; 738 } while (resid && space > 0); 739 } while (resid); 740 741release: 742 sbunlock(&so->so_snd); 743out: 744 if (top) 745 m_freem(top); 746 if (control) 747 m_freem(control); 748 return (error); 749} 750 751/* 752 * Implement receive operations on a socket. 753 * We depend on the way that records are added to the sockbuf 754 * by sbappend*. In particular, each record (mbufs linked through m_next) 755 * must begin with an address if the protocol so specifies, 756 * followed by an optional mbuf or mbufs containing ancillary data, 757 * and then zero or more mbufs of data. 758 * In order to avoid blocking network interrupts for the entire time here, 759 * we splx() while doing the actual copy to user space. 760 * Although the sockbuf is locked, new data may still be appended, 761 * and thus we must maintain consistency of the sockbuf during that time. 762 * 763 * The caller may receive the data as a single mbuf chain by supplying 764 * an mbuf **mp0 for use in returning the chain. The uio is then used 765 * only for the count in uio_resid. 766 */ 767int 768soreceive(so, psa, uio, mp0, controlp, flagsp) 769 register struct socket *so; 770 struct sockaddr **psa; 771 struct uio *uio; 772 struct mbuf **mp0; 773 struct mbuf **controlp; 774 int *flagsp; 775{ 776 struct mbuf *m, **mp; 777 register int flags, len, error, s, offset; 778 struct protosw *pr = so->so_proto; 779 struct mbuf *nextrecord; 780 int moff, type = 0; 781 int orig_resid = uio->uio_resid; 782 783 mp = mp0; 784 if (psa) 785 *psa = 0; 786 if (controlp) 787 *controlp = 0; 788 if (flagsp) 789 flags = *flagsp &~ MSG_EOR; 790 else 791 flags = 0; 792 if (flags & MSG_OOB) { 793 m = m_get(M_TRYWAIT, MT_DATA); 794 if (m == NULL) 795 return (ENOBUFS); 796 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK); 797 if (error) 798 goto bad; 799 do { 800#ifdef ZERO_COPY_SOCKETS 801 if (so_zero_copy_receive) { 802 vm_page_t pg; 803 int disposable; 804 805 if ((m->m_flags & M_EXT) 806 && (m->m_ext.ext_type == EXT_DISPOSABLE)) 807 disposable = 1; 808 else 809 disposable = 0; 810 811 pg = PHYS_TO_VM_PAGE(vtophys(mtod(m, caddr_t))); 812 if (uio->uio_offset == -1) 813 uio->uio_offset =IDX_TO_OFF(pg->pindex); 814 815 error = uiomoveco(mtod(m, caddr_t), 816 min(uio->uio_resid, m->m_len), 817 uio, pg->object, 818 disposable); 819 } else 820#endif /* ZERO_COPY_SOCKETS */ 821 error = uiomove(mtod(m, caddr_t), 822 (int) min(uio->uio_resid, m->m_len), uio); 823 m = m_free(m); 824 } while (uio->uio_resid && error == 0 && m); 825bad: 826 if (m) 827 m_freem(m); 828 return (error); 829 } 830 if (mp) 831 *mp = (struct mbuf *)0; 832 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) 833 (*pr->pr_usrreqs->pru_rcvd)(so, 0); 834 835restart: 836 error = sblock(&so->so_rcv, SBLOCKWAIT(flags)); 837 if (error) 838 return (error); 839 s = splnet(); 840 841 m = so->so_rcv.sb_mb; 842 /* 843 * If we have less data than requested, block awaiting more 844 * (subject to any timeout) if: 845 * 1. the current count is less than the low water mark, or 846 * 2. MSG_WAITALL is set, and it is possible to do the entire 847 * receive operation at once if we block (resid <= hiwat). 848 * 3. MSG_DONTWAIT is not set 849 * If MSG_WAITALL is set but resid is larger than the receive buffer, 850 * we have to do the receive in sections, and thus risk returning 851 * a short count if a timeout or signal occurs after we start. 852 */ 853 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 && 854 so->so_rcv.sb_cc < uio->uio_resid) && 855 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 856 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && 857 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) { 858 KASSERT(m != 0 || !so->so_rcv.sb_cc, 859 ("receive: m == %p so->so_rcv.sb_cc == %u", 860 m, so->so_rcv.sb_cc)); 861 if (so->so_error) { 862 if (m) 863 goto dontblock; 864 error = so->so_error; 865 if ((flags & MSG_PEEK) == 0) 866 so->so_error = 0; 867 goto release; 868 } 869 if (so->so_state & SS_CANTRCVMORE) { 870 if (m) 871 goto dontblock; 872 else 873 goto release; 874 } 875 for (; m; m = m->m_next) 876 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 877 m = so->so_rcv.sb_mb; 878 goto dontblock; 879 } 880 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 881 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 882 error = ENOTCONN; 883 goto release; 884 } 885 if (uio->uio_resid == 0) 886 goto release; 887 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) { 888 error = EWOULDBLOCK; 889 goto release; 890 } 891 sbunlock(&so->so_rcv); 892 error = sbwait(&so->so_rcv); 893 splx(s); 894 if (error) 895 return (error); 896 goto restart; 897 } 898dontblock: 899 if (uio->uio_td) 900 uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++; 901 nextrecord = m->m_nextpkt; 902 if (pr->pr_flags & PR_ADDR) { 903 KASSERT(m->m_type == MT_SONAME, 904 ("m->m_type == %d", m->m_type)); 905 orig_resid = 0; 906 if (psa) 907 *psa = dup_sockaddr(mtod(m, struct sockaddr *), 908 mp0 == 0); 909 if (flags & MSG_PEEK) { 910 m = m->m_next; 911 } else { 912 sbfree(&so->so_rcv, m); 913 so->so_rcv.sb_mb = m_free(m); 914 m = so->so_rcv.sb_mb; 915 } 916 } 917 while (m && m->m_type == MT_CONTROL && error == 0) { 918 if (flags & MSG_PEEK) { 919 if (controlp) 920 *controlp = m_copy(m, 0, m->m_len); 921 m = m->m_next; 922 } else { 923 sbfree(&so->so_rcv, m); 924 so->so_rcv.sb_mb = m->m_next; 925 m->m_next = NULL; 926 if (pr->pr_domain->dom_externalize) 927 error = 928 (*pr->pr_domain->dom_externalize)(m, controlp); 929 else if (controlp) 930 *controlp = m; 931 else 932 m_freem(m); 933 m = so->so_rcv.sb_mb; 934 } 935 if (controlp) { 936 orig_resid = 0; 937 do 938 controlp = &(*controlp)->m_next; 939 while (*controlp != NULL); 940 } 941 } 942 if (m) { 943 if ((flags & MSG_PEEK) == 0) 944 m->m_nextpkt = nextrecord; 945 type = m->m_type; 946 if (type == MT_OOBDATA) 947 flags |= MSG_OOB; 948 } 949 moff = 0; 950 offset = 0; 951 while (m && uio->uio_resid > 0 && error == 0) { 952 if (m->m_type == MT_OOBDATA) { 953 if (type != MT_OOBDATA) 954 break; 955 } else if (type == MT_OOBDATA) 956 break; 957 else 958 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER, 959 ("m->m_type == %d", m->m_type)); 960 so->so_state &= ~SS_RCVATMARK; 961 len = uio->uio_resid; 962 if (so->so_oobmark && len > so->so_oobmark - offset) 963 len = so->so_oobmark - offset; 964 if (len > m->m_len - moff) 965 len = m->m_len - moff; 966 /* 967 * If mp is set, just pass back the mbufs. 968 * Otherwise copy them out via the uio, then free. 969 * Sockbuf must be consistent here (points to current mbuf, 970 * it points to next record) when we drop priority; 971 * we must note any additions to the sockbuf when we 972 * block interrupts again. 973 */ 974 if (mp == 0) { 975 splx(s); 976#ifdef ZERO_COPY_SOCKETS 977 if (so_zero_copy_receive) { 978 vm_page_t pg; 979 int disposable; 980 981 if ((m->m_flags & M_EXT) 982 && (m->m_ext.ext_type == EXT_DISPOSABLE)) 983 disposable = 1; 984 else 985 disposable = 0; 986 987 pg = PHYS_TO_VM_PAGE(vtophys(mtod(m, caddr_t) + 988 moff)); 989 990 if (uio->uio_offset == -1) 991 uio->uio_offset =IDX_TO_OFF(pg->pindex); 992 993 error = uiomoveco(mtod(m, caddr_t) + moff, 994 (int)len, uio,pg->object, 995 disposable); 996 } else 997#endif /* ZERO_COPY_SOCKETS */ 998 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio); 999 s = splnet(); 1000 if (error) 1001 goto release; 1002 } else 1003 uio->uio_resid -= len; 1004 if (len == m->m_len - moff) { 1005 if (m->m_flags & M_EOR) 1006 flags |= MSG_EOR; 1007 if (flags & MSG_PEEK) { 1008 m = m->m_next; 1009 moff = 0; 1010 } else { 1011 nextrecord = m->m_nextpkt; 1012 sbfree(&so->so_rcv, m); 1013 if (mp) { 1014 *mp = m; 1015 mp = &m->m_next; 1016 so->so_rcv.sb_mb = m = m->m_next; 1017 *mp = (struct mbuf *)0; 1018 } else { 1019 so->so_rcv.sb_mb = m_free(m); 1020 m = so->so_rcv.sb_mb; 1021 } 1022 if (m) 1023 m->m_nextpkt = nextrecord; 1024 } 1025 } else { 1026 if (flags & MSG_PEEK) 1027 moff += len; 1028 else { 1029 if (mp) 1030 *mp = m_copym(m, 0, len, M_TRYWAIT); 1031 m->m_data += len; 1032 m->m_len -= len; 1033 so->so_rcv.sb_cc -= len; 1034 } 1035 } 1036 if (so->so_oobmark) { 1037 if ((flags & MSG_PEEK) == 0) { 1038 so->so_oobmark -= len; 1039 if (so->so_oobmark == 0) { 1040 so->so_state |= SS_RCVATMARK; 1041 break; 1042 } 1043 } else { 1044 offset += len; 1045 if (offset == so->so_oobmark) 1046 break; 1047 } 1048 } 1049 if (flags & MSG_EOR) 1050 break; 1051 /* 1052 * If the MSG_WAITALL flag is set (for non-atomic socket), 1053 * we must not quit until "uio->uio_resid == 0" or an error 1054 * termination. If a signal/timeout occurs, return 1055 * with a short count but without error. 1056 * Keep sockbuf locked against other readers. 1057 */ 1058 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 && 1059 !sosendallatonce(so) && !nextrecord) { 1060 if (so->so_error || so->so_state & SS_CANTRCVMORE) 1061 break; 1062 /* 1063 * Notify the protocol that some data has been 1064 * drained before blocking. 1065 */ 1066 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 1067 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1068 error = sbwait(&so->so_rcv); 1069 if (error) { 1070 sbunlock(&so->so_rcv); 1071 splx(s); 1072 return (0); 1073 } 1074 m = so->so_rcv.sb_mb; 1075 if (m) 1076 nextrecord = m->m_nextpkt; 1077 } 1078 } 1079 1080 if (m && pr->pr_flags & PR_ATOMIC) { 1081 flags |= MSG_TRUNC; 1082 if ((flags & MSG_PEEK) == 0) 1083 (void) sbdroprecord(&so->so_rcv); 1084 } 1085 if ((flags & MSG_PEEK) == 0) { 1086 if (m == 0) 1087 so->so_rcv.sb_mb = nextrecord; 1088 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 1089 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1090 } 1091 if (orig_resid == uio->uio_resid && orig_resid && 1092 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { 1093 sbunlock(&so->so_rcv); 1094 splx(s); 1095 goto restart; 1096 } 1097 1098 if (flagsp) 1099 *flagsp |= flags; 1100release: 1101 sbunlock(&so->so_rcv); 1102 splx(s); 1103 return (error); 1104} 1105 1106int 1107soshutdown(so, how) 1108 register struct socket *so; 1109 register int how; 1110{ 1111 register struct protosw *pr = so->so_proto; 1112 1113 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) 1114 return (EINVAL); 1115 1116 if (how != SHUT_WR) 1117 sorflush(so); 1118 if (how != SHUT_RD) 1119 return ((*pr->pr_usrreqs->pru_shutdown)(so)); 1120 return (0); 1121} 1122 1123void 1124sorflush(so) 1125 register struct socket *so; 1126{ 1127 register struct sockbuf *sb = &so->so_rcv; 1128 register struct protosw *pr = so->so_proto; 1129 register int s; 1130 struct sockbuf asb; 1131 1132 sb->sb_flags |= SB_NOINTR; 1133 (void) sblock(sb, M_WAITOK); 1134 s = splimp(); 1135 socantrcvmore(so); 1136 sbunlock(sb); 1137 asb = *sb; 1138 bzero(sb, sizeof (*sb)); 1139 splx(s); 1140 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) 1141 (*pr->pr_domain->dom_dispose)(asb.sb_mb); 1142 sbrelease(&asb, so); 1143} 1144 1145#ifdef INET 1146static int 1147do_setopt_accept_filter(so, sopt) 1148 struct socket *so; 1149 struct sockopt *sopt; 1150{ 1151 struct accept_filter_arg *afap = NULL; 1152 struct accept_filter *afp; 1153 struct so_accf *af = so->so_accf; 1154 int error = 0; 1155 1156 /* do not set/remove accept filters on non listen sockets */ 1157 if ((so->so_options & SO_ACCEPTCONN) == 0) { 1158 error = EINVAL; 1159 goto out; 1160 } 1161 1162 /* removing the filter */ 1163 if (sopt == NULL) { 1164 if (af != NULL) { 1165 if (af->so_accept_filter != NULL && 1166 af->so_accept_filter->accf_destroy != NULL) { 1167 af->so_accept_filter->accf_destroy(so); 1168 } 1169 if (af->so_accept_filter_str != NULL) { 1170 FREE(af->so_accept_filter_str, M_ACCF); 1171 } 1172 FREE(af, M_ACCF); 1173 so->so_accf = NULL; 1174 } 1175 so->so_options &= ~SO_ACCEPTFILTER; 1176 return (0); 1177 } 1178 /* adding a filter */ 1179 /* must remove previous filter first */ 1180 if (af != NULL) { 1181 error = EINVAL; 1182 goto out; 1183 } 1184 /* don't put large objects on the kernel stack */ 1185 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK); 1186 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap); 1187 afap->af_name[sizeof(afap->af_name)-1] = '\0'; 1188 afap->af_arg[sizeof(afap->af_arg)-1] = '\0'; 1189 if (error) 1190 goto out; 1191 afp = accept_filt_get(afap->af_name); 1192 if (afp == NULL) { 1193 error = ENOENT; 1194 goto out; 1195 } 1196 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK | M_ZERO); 1197 if (afp->accf_create != NULL) { 1198 if (afap->af_name[0] != '\0') { 1199 int len = strlen(afap->af_name) + 1; 1200 1201 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK); 1202 strcpy(af->so_accept_filter_str, afap->af_name); 1203 } 1204 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg); 1205 if (af->so_accept_filter_arg == NULL) { 1206 FREE(af->so_accept_filter_str, M_ACCF); 1207 FREE(af, M_ACCF); 1208 so->so_accf = NULL; 1209 error = EINVAL; 1210 goto out; 1211 } 1212 } 1213 af->so_accept_filter = afp; 1214 so->so_accf = af; 1215 so->so_options |= SO_ACCEPTFILTER; 1216out: 1217 if (afap != NULL) 1218 FREE(afap, M_TEMP); 1219 return (error); 1220} 1221#endif /* INET */ 1222 1223/* 1224 * Perhaps this routine, and sooptcopyout(), below, ought to come in 1225 * an additional variant to handle the case where the option value needs 1226 * to be some kind of integer, but not a specific size. 1227 * In addition to their use here, these functions are also called by the 1228 * protocol-level pr_ctloutput() routines. 1229 */ 1230int 1231sooptcopyin(sopt, buf, len, minlen) 1232 struct sockopt *sopt; 1233 void *buf; 1234 size_t len; 1235 size_t minlen; 1236{ 1237 size_t valsize; 1238 1239 /* 1240 * If the user gives us more than we wanted, we ignore it, 1241 * but if we don't get the minimum length the caller 1242 * wants, we return EINVAL. On success, sopt->sopt_valsize 1243 * is set to however much we actually retrieved. 1244 */ 1245 if ((valsize = sopt->sopt_valsize) < minlen) 1246 return EINVAL; 1247 if (valsize > len) 1248 sopt->sopt_valsize = valsize = len; 1249 1250 if (sopt->sopt_td != 0) 1251 return (copyin(sopt->sopt_val, buf, valsize)); 1252 1253 bcopy(sopt->sopt_val, buf, valsize); 1254 return 0; 1255} 1256 1257int 1258sosetopt(so, sopt) 1259 struct socket *so; 1260 struct sockopt *sopt; 1261{ 1262 int error, optval; 1263 struct linger l; 1264 struct timeval tv; 1265 u_long val; 1266#ifdef MAC 1267 struct mac extmac; 1268#endif 1269 1270 error = 0; 1271 if (sopt->sopt_level != SOL_SOCKET) { 1272 if (so->so_proto && so->so_proto->pr_ctloutput) 1273 return ((*so->so_proto->pr_ctloutput) 1274 (so, sopt)); 1275 error = ENOPROTOOPT; 1276 } else { 1277 switch (sopt->sopt_name) { 1278#ifdef INET 1279 case SO_ACCEPTFILTER: 1280 error = do_setopt_accept_filter(so, sopt); 1281 if (error) 1282 goto bad; 1283 break; 1284#endif 1285 case SO_LINGER: 1286 error = sooptcopyin(sopt, &l, sizeof l, sizeof l); 1287 if (error) 1288 goto bad; 1289 1290 so->so_linger = l.l_linger; 1291 if (l.l_onoff) 1292 so->so_options |= SO_LINGER; 1293 else 1294 so->so_options &= ~SO_LINGER; 1295 break; 1296 1297 case SO_DEBUG: 1298 case SO_KEEPALIVE: 1299 case SO_DONTROUTE: 1300 case SO_USELOOPBACK: 1301 case SO_BROADCAST: 1302 case SO_REUSEADDR: 1303 case SO_REUSEPORT: 1304 case SO_OOBINLINE: 1305 case SO_TIMESTAMP: 1306 case SO_NOSIGPIPE: 1307 error = sooptcopyin(sopt, &optval, sizeof optval, 1308 sizeof optval); 1309 if (error) 1310 goto bad; 1311 if (optval) 1312 so->so_options |= sopt->sopt_name; 1313 else 1314 so->so_options &= ~sopt->sopt_name; 1315 break; 1316 1317 case SO_SNDBUF: 1318 case SO_RCVBUF: 1319 case SO_SNDLOWAT: 1320 case SO_RCVLOWAT: 1321 error = sooptcopyin(sopt, &optval, sizeof optval, 1322 sizeof optval); 1323 if (error) 1324 goto bad; 1325 1326 /* 1327 * Values < 1 make no sense for any of these 1328 * options, so disallow them. 1329 */ 1330 if (optval < 1) { 1331 error = EINVAL; 1332 goto bad; 1333 } 1334 1335 switch (sopt->sopt_name) { 1336 case SO_SNDBUF: 1337 case SO_RCVBUF: 1338 if (sbreserve(sopt->sopt_name == SO_SNDBUF ? 1339 &so->so_snd : &so->so_rcv, (u_long)optval, 1340 so, curthread) == 0) { 1341 error = ENOBUFS; 1342 goto bad; 1343 } 1344 break; 1345 1346 /* 1347 * Make sure the low-water is never greater than 1348 * the high-water. 1349 */ 1350 case SO_SNDLOWAT: 1351 so->so_snd.sb_lowat = 1352 (optval > so->so_snd.sb_hiwat) ? 1353 so->so_snd.sb_hiwat : optval; 1354 break; 1355 case SO_RCVLOWAT: 1356 so->so_rcv.sb_lowat = 1357 (optval > so->so_rcv.sb_hiwat) ? 1358 so->so_rcv.sb_hiwat : optval; 1359 break; 1360 } 1361 break; 1362 1363 case SO_SNDTIMEO: 1364 case SO_RCVTIMEO: 1365 error = sooptcopyin(sopt, &tv, sizeof tv, 1366 sizeof tv); 1367 if (error) 1368 goto bad; 1369 1370 /* assert(hz > 0); */ 1371 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz || 1372 tv.tv_usec < 0 || tv.tv_usec >= 1000000) { 1373 error = EDOM; 1374 goto bad; 1375 } 1376 /* assert(tick > 0); */ 1377 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */ 1378 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick; 1379 if (val > SHRT_MAX) { 1380 error = EDOM; 1381 goto bad; 1382 } 1383 if (val == 0 && tv.tv_usec != 0) 1384 val = 1; 1385 1386 switch (sopt->sopt_name) { 1387 case SO_SNDTIMEO: 1388 so->so_snd.sb_timeo = val; 1389 break; 1390 case SO_RCVTIMEO: 1391 so->so_rcv.sb_timeo = val; 1392 break; 1393 } 1394 break; 1395 case SO_LABEL: 1396#ifdef MAC 1397 error = sooptcopyin(sopt, &extmac, sizeof extmac, 1398 sizeof extmac); 1399 if (error) 1400 goto bad; 1401 1402 error = mac_setsockopt_label_set( 1403 sopt->sopt_td->td_ucred, so, &extmac); 1404 1405#else 1406 error = EOPNOTSUPP; 1407#endif 1408 break; 1409 default: 1410 error = ENOPROTOOPT; 1411 break; 1412 } 1413 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) { 1414 (void) ((*so->so_proto->pr_ctloutput) 1415 (so, sopt)); 1416 } 1417 } 1418bad: 1419 return (error); 1420} 1421 1422/* Helper routine for getsockopt */ 1423int 1424sooptcopyout(sopt, buf, len) 1425 struct sockopt *sopt; 1426 void *buf; 1427 size_t len; 1428{ 1429 int error; 1430 size_t valsize; 1431 1432 error = 0; 1433 1434 /* 1435 * Documented get behavior is that we always return a value, 1436 * possibly truncated to fit in the user's buffer. 1437 * Traditional behavior is that we always tell the user 1438 * precisely how much we copied, rather than something useful 1439 * like the total amount we had available for her. 1440 * Note that this interface is not idempotent; the entire answer must 1441 * generated ahead of time. 1442 */ 1443 valsize = min(len, sopt->sopt_valsize); 1444 sopt->sopt_valsize = valsize; 1445 if (sopt->sopt_val != 0) { 1446 if (sopt->sopt_td != 0) 1447 error = copyout(buf, sopt->sopt_val, valsize); 1448 else 1449 bcopy(buf, sopt->sopt_val, valsize); 1450 } 1451 return error; 1452} 1453 1454int 1455sogetopt(so, sopt) 1456 struct socket *so; 1457 struct sockopt *sopt; 1458{ 1459 int error, optval; 1460 struct linger l; 1461 struct timeval tv; 1462#ifdef INET 1463 struct accept_filter_arg *afap; 1464#endif 1465#ifdef MAC 1466 struct mac extmac; 1467#endif 1468 1469 error = 0; 1470 if (sopt->sopt_level != SOL_SOCKET) { 1471 if (so->so_proto && so->so_proto->pr_ctloutput) { 1472 return ((*so->so_proto->pr_ctloutput) 1473 (so, sopt)); 1474 } else 1475 return (ENOPROTOOPT); 1476 } else { 1477 switch (sopt->sopt_name) { 1478#ifdef INET 1479 case SO_ACCEPTFILTER: 1480 if ((so->so_options & SO_ACCEPTCONN) == 0) 1481 return (EINVAL); 1482 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), 1483 M_TEMP, M_WAITOK | M_ZERO); 1484 if ((so->so_options & SO_ACCEPTFILTER) != 0) { 1485 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name); 1486 if (so->so_accf->so_accept_filter_str != NULL) 1487 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str); 1488 } 1489 error = sooptcopyout(sopt, afap, sizeof(*afap)); 1490 FREE(afap, M_TEMP); 1491 break; 1492#endif 1493 1494 case SO_LINGER: 1495 l.l_onoff = so->so_options & SO_LINGER; 1496 l.l_linger = so->so_linger; 1497 error = sooptcopyout(sopt, &l, sizeof l); 1498 break; 1499 1500 case SO_USELOOPBACK: 1501 case SO_DONTROUTE: 1502 case SO_DEBUG: 1503 case SO_KEEPALIVE: 1504 case SO_REUSEADDR: 1505 case SO_REUSEPORT: 1506 case SO_BROADCAST: 1507 case SO_OOBINLINE: 1508 case SO_TIMESTAMP: 1509 case SO_NOSIGPIPE: 1510 optval = so->so_options & sopt->sopt_name; 1511integer: 1512 error = sooptcopyout(sopt, &optval, sizeof optval); 1513 break; 1514 1515 case SO_TYPE: 1516 optval = so->so_type; 1517 goto integer; 1518 1519 case SO_ERROR: 1520 optval = so->so_error; 1521 so->so_error = 0; 1522 goto integer; 1523 1524 case SO_SNDBUF: 1525 optval = so->so_snd.sb_hiwat; 1526 goto integer; 1527 1528 case SO_RCVBUF: 1529 optval = so->so_rcv.sb_hiwat; 1530 goto integer; 1531 1532 case SO_SNDLOWAT: 1533 optval = so->so_snd.sb_lowat; 1534 goto integer; 1535 1536 case SO_RCVLOWAT: 1537 optval = so->so_rcv.sb_lowat; 1538 goto integer; 1539 1540 case SO_SNDTIMEO: 1541 case SO_RCVTIMEO: 1542 optval = (sopt->sopt_name == SO_SNDTIMEO ? 1543 so->so_snd.sb_timeo : so->so_rcv.sb_timeo); 1544 1545 tv.tv_sec = optval / hz; 1546 tv.tv_usec = (optval % hz) * tick; 1547 error = sooptcopyout(sopt, &tv, sizeof tv); 1548 break; 1549 case SO_LABEL: 1550#ifdef MAC 1551 error = mac_getsockopt_label_get( 1552 sopt->sopt_td->td_ucred, so, &extmac); 1553 if (error) 1554 return (error); 1555 error = sooptcopyout(sopt, &extmac, sizeof extmac); 1556#else 1557 error = EOPNOTSUPP; 1558#endif 1559 break; 1560 case SO_PEERLABEL: 1561#ifdef MAC 1562 error = mac_getsockopt_peerlabel_get( 1563 sopt->sopt_td->td_ucred, so, &extmac); 1564 if (error) 1565 return (error); 1566 error = sooptcopyout(sopt, &extmac, sizeof extmac); 1567#else 1568 error = EOPNOTSUPP; 1569#endif 1570 break; 1571 default: 1572 error = ENOPROTOOPT; 1573 break; 1574 } 1575 return (error); 1576 } 1577} 1578 1579/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 1580int 1581soopt_getm(struct sockopt *sopt, struct mbuf **mp) 1582{ 1583 struct mbuf *m, *m_prev; 1584 int sopt_size = sopt->sopt_valsize; 1585 1586 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 1587 if (m == 0) 1588 return ENOBUFS; 1589 if (sopt_size > MLEN) { 1590 MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT); 1591 if ((m->m_flags & M_EXT) == 0) { 1592 m_free(m); 1593 return ENOBUFS; 1594 } 1595 m->m_len = min(MCLBYTES, sopt_size); 1596 } else { 1597 m->m_len = min(MLEN, sopt_size); 1598 } 1599 sopt_size -= m->m_len; 1600 *mp = m; 1601 m_prev = m; 1602 1603 while (sopt_size) { 1604 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 1605 if (m == 0) { 1606 m_freem(*mp); 1607 return ENOBUFS; 1608 } 1609 if (sopt_size > MLEN) { 1610 MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT); 1611 if ((m->m_flags & M_EXT) == 0) { 1612 m_freem(*mp); 1613 return ENOBUFS; 1614 } 1615 m->m_len = min(MCLBYTES, sopt_size); 1616 } else { 1617 m->m_len = min(MLEN, sopt_size); 1618 } 1619 sopt_size -= m->m_len; 1620 m_prev->m_next = m; 1621 m_prev = m; 1622 } 1623 return 0; 1624} 1625 1626/* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */ 1627int 1628soopt_mcopyin(struct sockopt *sopt, struct mbuf *m) 1629{ 1630 struct mbuf *m0 = m; 1631 1632 if (sopt->sopt_val == NULL) 1633 return 0; 1634 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 1635 if (sopt->sopt_td != NULL) { 1636 int error; 1637 1638 error = copyin(sopt->sopt_val, mtod(m, char *), 1639 m->m_len); 1640 if (error != 0) { 1641 m_freem(m0); 1642 return(error); 1643 } 1644 } else 1645 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len); 1646 sopt->sopt_valsize -= m->m_len; 1647 (caddr_t)sopt->sopt_val += m->m_len; 1648 m = m->m_next; 1649 } 1650 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */ 1651 panic("ip6_sooptmcopyin"); 1652 return 0; 1653} 1654 1655/* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */ 1656int 1657soopt_mcopyout(struct sockopt *sopt, struct mbuf *m) 1658{ 1659 struct mbuf *m0 = m; 1660 size_t valsize = 0; 1661 1662 if (sopt->sopt_val == NULL) 1663 return 0; 1664 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 1665 if (sopt->sopt_td != NULL) { 1666 int error; 1667 1668 error = copyout(mtod(m, char *), sopt->sopt_val, 1669 m->m_len); 1670 if (error != 0) { 1671 m_freem(m0); 1672 return(error); 1673 } 1674 } else 1675 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len); 1676 sopt->sopt_valsize -= m->m_len; 1677 (caddr_t)sopt->sopt_val += m->m_len; 1678 valsize += m->m_len; 1679 m = m->m_next; 1680 } 1681 if (m != NULL) { 1682 /* enough soopt buffer should be given from user-land */ 1683 m_freem(m0); 1684 return(EINVAL); 1685 } 1686 sopt->sopt_valsize = valsize; 1687 return 0; 1688} 1689 1690void 1691sohasoutofband(so) 1692 register struct socket *so; 1693{ 1694 if (so->so_sigio != NULL) 1695 pgsigio(&so->so_sigio, SIGURG, 0); 1696 selwakeup(&so->so_rcv.sb_sel); 1697} 1698 1699int 1700sopoll(struct socket *so, int events, struct ucred *active_cred, 1701 struct thread *td) 1702{ 1703 int revents = 0; 1704 int s = splnet(); 1705 1706 if (events & (POLLIN | POLLRDNORM)) 1707 if (soreadable(so)) 1708 revents |= events & (POLLIN | POLLRDNORM); 1709 1710 if (events & POLLINIGNEOF) 1711 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat || 1712 !TAILQ_EMPTY(&so->so_comp) || so->so_error) 1713 revents |= POLLINIGNEOF; 1714 1715 if (events & (POLLOUT | POLLWRNORM)) 1716 if (sowriteable(so)) 1717 revents |= events & (POLLOUT | POLLWRNORM); 1718 1719 if (events & (POLLPRI | POLLRDBAND)) 1720 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) 1721 revents |= events & (POLLPRI | POLLRDBAND); 1722 1723 if (revents == 0) { 1724 if (events & 1725 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | 1726 POLLRDBAND)) { 1727 selrecord(td, &so->so_rcv.sb_sel); 1728 so->so_rcv.sb_flags |= SB_SEL; 1729 } 1730 1731 if (events & (POLLOUT | POLLWRNORM)) { 1732 selrecord(td, &so->so_snd.sb_sel); 1733 so->so_snd.sb_flags |= SB_SEL; 1734 } 1735 } 1736 1737 splx(s); 1738 return (revents); 1739} 1740 1741int
|