1 /* 2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved. 3 * Copyright (c) 2004 The DragonFly Project. All rights reserved. 4 * 5 * This code is derived from software contributed to The DragonFly Project 6 * by Jeffrey M. Hsu. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of The DragonFly Project nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific, prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1982, 1986, 1988, 1990, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. Neither the name of the University nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 * 62 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94 63 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.24 2003/11/11 17:18:18 silby Exp $ 64 */ 65 66 #include "opt_inet.h" 67 68 #include <sys/param.h> 69 #include <sys/systm.h> 70 #include <sys/fcntl.h> 71 #include <sys/malloc.h> 72 #include <sys/mbuf.h> 73 #include <sys/domain.h> 74 #include <sys/file.h> /* for struct knote */ 75 #include <sys/kernel.h> 76 #include <sys/event.h> 77 #include <sys/proc.h> 78 #include <sys/protosw.h> 79 #include <sys/socket.h> 80 #include <sys/socketvar.h> 81 #include <sys/socketops.h> 82 #include <sys/resourcevar.h> 83 #include <sys/signalvar.h> 84 #include <sys/sysctl.h> 85 #include <sys/uio.h> 86 #include <sys/jail.h> 87 #include <vm/vm_zone.h> 88 #include <vm/pmap.h> 89 #include <net/netmsg2.h> 90 #include <net/netisr2.h> 91 92 #include <sys/thread2.h> 93 #include <sys/socketvar2.h> 94 #include <sys/spinlock2.h> 95 96 #include <machine/limits.h> 97 98 #ifdef INET 99 extern int tcp_sosend_agglim; 100 extern int tcp_sosend_async; 101 extern int tcp_sosend_jcluster; 102 extern int udp_sosend_async; 103 extern int udp_sosend_prepend; 104 105 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt); 106 #endif /* INET */ 107 108 static void filt_sordetach(struct knote *kn); 109 static int filt_soread(struct knote *kn, long hint); 110 static void filt_sowdetach(struct knote *kn); 111 static int filt_sowrite(struct knote *kn, long hint); 112 static int filt_solisten(struct knote *kn, long hint); 113 114 static int soclose_sync(struct socket *so, int fflag); 115 static void soclose_fast(struct socket *so); 116 117 static struct filterops solisten_filtops = 118 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sordetach, filt_solisten }; 119 static struct filterops soread_filtops = 120 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sordetach, filt_soread }; 121 static struct filterops sowrite_filtops = 122 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sowdetach, filt_sowrite }; 123 static struct filterops soexcept_filtops = 124 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sordetach, filt_soread }; 125 126 MALLOC_DEFINE(M_SOCKET, "socket", "socket struct"); 127 MALLOC_DEFINE(M_SONAME, "soname", "socket name"); 128 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block"); 129 130 131 static int somaxconn = SOMAXCONN; 132 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW, 133 &somaxconn, 0, "Maximum pending socket connection queue size"); 134 135 static int use_soclose_fast = 1; 136 SYSCTL_INT(_kern_ipc, OID_AUTO, soclose_fast, CTLFLAG_RW, 137 &use_soclose_fast, 0, "Fast socket close"); 138 139 int use_soaccept_pred_fast = 1; 140 SYSCTL_INT(_kern_ipc, OID_AUTO, soaccept_pred_fast, CTLFLAG_RW, 141 &use_soaccept_pred_fast, 0, "Fast socket accept predication"); 142 143 int use_sendfile_async = 1; 144 SYSCTL_INT(_kern_ipc, OID_AUTO, sendfile_async, CTLFLAG_RW, 145 &use_sendfile_async, 0, "sendfile uses asynchronized pru_send"); 146 147 int use_soconnect_async = 1; 148 SYSCTL_INT(_kern_ipc, OID_AUTO, soconnect_async, CTLFLAG_RW, 149 &use_soconnect_async, 0, "soconnect uses asynchronized pru_connect"); 150 151 /* 152 * Socket operation routines. 153 * These routines are called by the routines in 154 * sys_socket.c or from a system process, and 155 * implement the semantics of socket operations by 156 * switching out to the protocol specific routines. 157 */ 158 159 /* 160 * Get a socket structure, and initialize it. 161 * Note that it would probably be better to allocate socket 162 * and PCB at the same time, but I'm not convinced that all 163 * the protocols can be easily modified to do this. 164 */ 165 struct socket * 166 soalloc(int waitok, struct protosw *pr) 167 { 168 struct socket *so; 169 unsigned waitmask; 170 171 waitmask = waitok ? M_WAITOK : M_NOWAIT; 172 so = kmalloc(sizeof(struct socket), M_SOCKET, M_ZERO|waitmask); 173 if (so) { 174 /* XXX race condition for reentrant kernel */ 175 so->so_proto = pr; 176 TAILQ_INIT(&so->so_aiojobq); 177 TAILQ_INIT(&so->so_rcv.ssb_kq.ki_mlist); 178 TAILQ_INIT(&so->so_snd.ssb_kq.ki_mlist); 179 lwkt_token_init(&so->so_rcv.ssb_token, "rcvtok"); 180 lwkt_token_init(&so->so_snd.ssb_token, "sndtok"); 181 spin_init(&so->so_rcvd_spin, "soalloc"); 182 netmsg_init(&so->so_rcvd_msg.base, so, &netisr_adone_rport, 183 MSGF_DROPABLE | MSGF_PRIORITY, 184 so->so_proto->pr_usrreqs->pru_rcvd); 185 so->so_rcvd_msg.nm_pru_flags |= PRUR_ASYNC; 186 so->so_state = SS_NOFDREF; 187 so->so_refs = 1; 188 } 189 return so; 190 } 191 192 int 193 socreate(int dom, struct socket **aso, int type, 194 int proto, struct thread *td) 195 { 196 struct proc *p = td->td_proc; 197 struct protosw *prp; 198 struct socket *so; 199 struct pru_attach_info ai; 200 int error; 201 202 if (proto) 203 prp = pffindproto(dom, proto, type); 204 else 205 prp = pffindtype(dom, type); 206 207 if (prp == NULL || prp->pr_usrreqs->pru_attach == 0) 208 return (EPROTONOSUPPORT); 209 210 if (p->p_ucred->cr_prison && jail_socket_unixiproute_only && 211 prp->pr_domain->dom_family != PF_LOCAL && 212 prp->pr_domain->dom_family != PF_INET && 213 prp->pr_domain->dom_family != PF_INET6 && 214 prp->pr_domain->dom_family != PF_ROUTE) { 215 return (EPROTONOSUPPORT); 216 } 217 218 if (prp->pr_type != type) 219 return (EPROTOTYPE); 220 so = soalloc(p != NULL, prp); 221 if (so == NULL) 222 return (ENOBUFS); 223 224 /* 225 * Callers of socreate() presumably will connect up a descriptor 226 * and call soclose() if they cannot. This represents our so_refs 227 * (which should be 1) from soalloc(). 228 */ 229 soclrstate(so, SS_NOFDREF); 230 231 /* 232 * Set a default port for protocol processing. No action will occur 233 * on the socket on this port until an inpcb is attached to it and 234 * is able to match incoming packets, or until the socket becomes 235 * available to userland. 236 * 237 * We normally default the socket to the protocol thread on cpu 0, 238 * if protocol does not provide its own method to initialize the 239 * default port. 240 * 241 * If PR_SYNC_PORT is set (unix domain sockets) there is no protocol 242 * thread and all pr_*()/pru_*() calls are executed synchronously. 243 */ 244 if (prp->pr_flags & PR_SYNC_PORT) 245 so->so_port = &netisr_sync_port; 246 else if (prp->pr_initport != NULL) 247 so->so_port = prp->pr_initport(); 248 else 249 so->so_port = netisr_cpuport(0); 250 251 TAILQ_INIT(&so->so_incomp); 252 TAILQ_INIT(&so->so_comp); 253 so->so_type = type; 254 so->so_cred = crhold(p->p_ucred); 255 ai.sb_rlimit = &p->p_rlimit[RLIMIT_SBSIZE]; 256 ai.p_ucred = p->p_ucred; 257 ai.fd_rdir = p->p_fd->fd_rdir; 258 259 /* 260 * Auto-sizing of socket buffers is managed by the protocols and 261 * the appropriate flags must be set in the pru_attach function. 262 */ 263 error = so_pru_attach(so, proto, &ai); 264 if (error) { 265 sosetstate(so, SS_NOFDREF); 266 sofree(so); /* from soalloc */ 267 return error; 268 } 269 270 /* 271 * NOTE: Returns referenced socket. 272 */ 273 *aso = so; 274 return (0); 275 } 276 277 int 278 sobind(struct socket *so, struct sockaddr *nam, struct thread *td) 279 { 280 int error; 281 282 error = so_pru_bind(so, nam, td); 283 return (error); 284 } 285 286 static void 287 sodealloc(struct socket *so) 288 { 289 KKASSERT((so->so_state & (SS_INCOMP | SS_COMP)) == 0); 290 if (so->so_rcv.ssb_hiwat) 291 (void)chgsbsize(so->so_cred->cr_uidinfo, 292 &so->so_rcv.ssb_hiwat, 0, RLIM_INFINITY); 293 if (so->so_snd.ssb_hiwat) 294 (void)chgsbsize(so->so_cred->cr_uidinfo, 295 &so->so_snd.ssb_hiwat, 0, RLIM_INFINITY); 296 #ifdef INET 297 /* remove accept filter if present */ 298 if (so->so_accf != NULL) 299 do_setopt_accept_filter(so, NULL); 300 #endif /* INET */ 301 crfree(so->so_cred); 302 if (so->so_faddr != NULL) 303 kfree(so->so_faddr, M_SONAME); 304 kfree(so, M_SOCKET); 305 } 306 307 int 308 solisten(struct socket *so, int backlog, struct thread *td) 309 { 310 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING)) 311 return (EINVAL); 312 313 lwkt_gettoken(&so->so_rcv.ssb_token); 314 if (TAILQ_EMPTY(&so->so_comp)) 315 so->so_options |= SO_ACCEPTCONN; 316 lwkt_reltoken(&so->so_rcv.ssb_token); 317 if (backlog < 0 || backlog > somaxconn) 318 backlog = somaxconn; 319 so->so_qlimit = backlog; 320 return so_pru_listen(so, td); 321 } 322 323 /* 324 * Destroy a disconnected socket. This routine is a NOP if entities 325 * still have a reference on the socket: 326 * 327 * so_pcb - The protocol stack still has a reference 328 * SS_NOFDREF - There is no longer a file pointer reference 329 */ 330 void 331 sofree(struct socket *so) 332 { 333 struct socket *head; 334 335 /* 336 * This is a bit hackish at the moment. We need to interlock 337 * any accept queue we are on before we potentially lose the 338 * last reference to avoid races against a re-reference from 339 * someone operating on the queue. 340 */ 341 while ((head = so->so_head) != NULL) { 342 lwkt_getpooltoken(head); 343 if (so->so_head == head) 344 break; 345 lwkt_relpooltoken(head); 346 } 347 348 /* 349 * Arbitrage the last free. 350 */ 351 KKASSERT(so->so_refs > 0); 352 if (atomic_fetchadd_int(&so->so_refs, -1) != 1) { 353 if (head) 354 lwkt_relpooltoken(head); 355 return; 356 } 357 358 KKASSERT(so->so_pcb == NULL && (so->so_state & SS_NOFDREF)); 359 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0); 360 361 /* 362 * We're done, remove ourselves from the accept queue we are 363 * on, if we are on one. 364 */ 365 if (head != NULL) { 366 if (so->so_state & SS_INCOMP) { 367 KKASSERT((so->so_state & (SS_INCOMP | SS_COMP)) == 368 SS_INCOMP); 369 TAILQ_REMOVE(&head->so_incomp, so, so_list); 370 head->so_incqlen--; 371 } else if (so->so_state & SS_COMP) { 372 /* 373 * We must not decommission a socket that's 374 * on the accept(2) queue. If we do, then 375 * accept(2) may hang after select(2) indicated 376 * that the listening socket was ready. 377 */ 378 KKASSERT((so->so_state & (SS_INCOMP | SS_COMP)) == 379 SS_COMP); 380 lwkt_relpooltoken(head); 381 return; 382 } else { 383 panic("sofree: not queued"); 384 } 385 soclrstate(so, SS_INCOMP); 386 so->so_head = NULL; 387 lwkt_relpooltoken(head); 388 } 389 ssb_release(&so->so_snd, so); 390 sorflush(so); 391 sodealloc(so); 392 } 393 394 /* 395 * Close a socket on last file table reference removal. 396 * Initiate disconnect if connected. 397 * Free socket when disconnect complete. 398 */ 399 int 400 soclose(struct socket *so, int fflag) 401 { 402 int error; 403 404 funsetown(&so->so_sigio); 405 sosetstate(so, SS_ISCLOSING); 406 if (!use_soclose_fast || 407 (so->so_proto->pr_flags & PR_SYNC_PORT) || 408 ((so->so_state & SS_ISCONNECTED) && 409 (so->so_options & SO_LINGER))) { 410 error = soclose_sync(so, fflag); 411 } else { 412 soclose_fast(so); 413 error = 0; 414 } 415 return error; 416 } 417 418 void 419 sodiscard(struct socket *so) 420 { 421 lwkt_getpooltoken(so); 422 if (so->so_options & SO_ACCEPTCONN) { 423 struct socket *sp; 424 425 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) { 426 KKASSERT((sp->so_state & (SS_INCOMP | SS_COMP)) == 427 SS_INCOMP); 428 TAILQ_REMOVE(&so->so_incomp, sp, so_list); 429 so->so_incqlen--; 430 soclrstate(sp, SS_INCOMP); 431 soabort_async(sp, TRUE); 432 } 433 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) { 434 KKASSERT((sp->so_state & (SS_INCOMP | SS_COMP)) == 435 SS_COMP); 436 TAILQ_REMOVE(&so->so_comp, sp, so_list); 437 so->so_qlen--; 438 soclrstate(sp, SS_COMP); 439 soabort_async(sp, TRUE); 440 } 441 } 442 lwkt_relpooltoken(so); 443 444 if (so->so_state & SS_NOFDREF) 445 panic("soclose: NOFDREF"); 446 sosetstate(so, SS_NOFDREF); /* take ref */ 447 } 448 449 /* 450 * Append the completed queue of head to head_inh (inherting listen socket). 451 */ 452 void 453 soinherit(struct socket *head, struct socket *head_inh) 454 { 455 boolean_t do_wakeup = FALSE; 456 457 KASSERT(head->so_options & SO_ACCEPTCONN, 458 ("head does not accept connection")); 459 KASSERT(head_inh->so_options & SO_ACCEPTCONN, 460 ("head_inh does not accept connection")); 461 462 lwkt_getpooltoken(head); 463 lwkt_getpooltoken(head_inh); 464 465 if (head->so_qlen > 0) 466 do_wakeup = TRUE; 467 468 while (!TAILQ_EMPTY(&head->so_comp)) { 469 struct ucred *old_cr; 470 struct socket *sp; 471 472 sp = TAILQ_FIRST(&head->so_comp); 473 KKASSERT((sp->so_state & (SS_INCOMP | SS_COMP)) == SS_COMP); 474 475 /* 476 * Remove this socket from the current listen socket 477 * completed queue. 478 */ 479 TAILQ_REMOVE(&head->so_comp, sp, so_list); 480 head->so_qlen--; 481 482 /* Save the old ucred for later free. */ 483 old_cr = sp->so_cred; 484 485 /* 486 * Install this socket to the inheriting listen socket 487 * completed queue. 488 */ 489 sp->so_cred = crhold(head_inh->so_cred); /* non-blocking */ 490 sp->so_head = head_inh; 491 492 TAILQ_INSERT_TAIL(&head_inh->so_comp, sp, so_list); 493 head_inh->so_qlen++; 494 495 /* 496 * NOTE: 497 * crfree() may block and release the tokens temporarily. 498 * However, we are fine here, since the transition is done. 499 */ 500 crfree(old_cr); 501 } 502 503 lwkt_relpooltoken(head_inh); 504 lwkt_relpooltoken(head); 505 506 if (do_wakeup) { 507 /* 508 * "New" connections have arrived 509 */ 510 sorwakeup(head_inh); 511 wakeup(&head_inh->so_timeo); 512 } 513 } 514 515 static int 516 soclose_sync(struct socket *so, int fflag) 517 { 518 int error = 0; 519 520 if (so->so_pcb == NULL) 521 goto discard; 522 if (so->so_state & SS_ISCONNECTED) { 523 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 524 error = sodisconnect(so); 525 if (error) 526 goto drop; 527 } 528 if (so->so_options & SO_LINGER) { 529 if ((so->so_state & SS_ISDISCONNECTING) && 530 (fflag & FNONBLOCK)) 531 goto drop; 532 while (so->so_state & SS_ISCONNECTED) { 533 error = tsleep(&so->so_timeo, PCATCH, 534 "soclos", so->so_linger * hz); 535 if (error) 536 break; 537 } 538 } 539 } 540 drop: 541 if (so->so_pcb) { 542 int error2; 543 544 error2 = so_pru_detach(so); 545 if (error2 == EJUSTRETURN) { 546 /* 547 * Protocol will call sodiscard() 548 * and sofree() for us. 549 */ 550 return error; 551 } 552 if (error == 0) 553 error = error2; 554 } 555 discard: 556 sodiscard(so); 557 so_pru_sync(so); /* unpend async sending */ 558 sofree(so); /* dispose of ref */ 559 560 return (error); 561 } 562 563 static void 564 soclose_sofree_async_handler(netmsg_t msg) 565 { 566 sofree(msg->base.nm_so); 567 } 568 569 static void 570 soclose_sofree_async(struct socket *so) 571 { 572 struct netmsg_base *base = &so->so_clomsg; 573 574 netmsg_init(base, so, &netisr_apanic_rport, 0, 575 soclose_sofree_async_handler); 576 lwkt_sendmsg(so->so_port, &base->lmsg); 577 } 578 579 static void 580 soclose_disconn_async_handler(netmsg_t msg) 581 { 582 struct socket *so = msg->base.nm_so; 583 584 if ((so->so_state & SS_ISCONNECTED) && 585 (so->so_state & SS_ISDISCONNECTING) == 0) 586 so_pru_disconnect_direct(so); 587 588 if (so->so_pcb) { 589 int error; 590 591 error = so_pru_detach_direct(so); 592 if (error == EJUSTRETURN) { 593 /* 594 * Protocol will call sodiscard() 595 * and sofree() for us. 596 */ 597 return; 598 } 599 } 600 601 sodiscard(so); 602 sofree(so); 603 } 604 605 static void 606 soclose_disconn_async(struct socket *so) 607 { 608 struct netmsg_base *base = &so->so_clomsg; 609 610 netmsg_init(base, so, &netisr_apanic_rport, 0, 611 soclose_disconn_async_handler); 612 lwkt_sendmsg(so->so_port, &base->lmsg); 613 } 614 615 static void 616 soclose_detach_async_handler(netmsg_t msg) 617 { 618 struct socket *so = msg->base.nm_so; 619 620 if (so->so_pcb) { 621 int error; 622 623 error = so_pru_detach_direct(so); 624 if (error == EJUSTRETURN) { 625 /* 626 * Protocol will call sodiscard() 627 * and sofree() for us. 628 */ 629 return; 630 } 631 } 632 633 sodiscard(so); 634 sofree(so); 635 } 636 637 static void 638 soclose_detach_async(struct socket *so) 639 { 640 struct netmsg_base *base = &so->so_clomsg; 641 642 netmsg_init(base, so, &netisr_apanic_rport, 0, 643 soclose_detach_async_handler); 644 lwkt_sendmsg(so->so_port, &base->lmsg); 645 } 646 647 static void 648 soclose_fast(struct socket *so) 649 { 650 if (so->so_pcb == NULL) 651 goto discard; 652 653 if ((so->so_state & SS_ISCONNECTED) && 654 (so->so_state & SS_ISDISCONNECTING) == 0) { 655 soclose_disconn_async(so); 656 return; 657 } 658 659 if (so->so_pcb) { 660 soclose_detach_async(so); 661 return; 662 } 663 664 discard: 665 sodiscard(so); 666 soclose_sofree_async(so); 667 } 668 669 /* 670 * Abort and destroy a socket. Only one abort can be in progress 671 * at any given moment. 672 */ 673 void 674 soabort_async(struct socket *so, boolean_t clr_head) 675 { 676 /* 677 * Keep a reference before clearing the so_head 678 * to avoid racing socket close in netisr. 679 */ 680 soreference(so); 681 if (clr_head) 682 so->so_head = NULL; 683 so_pru_abort_async(so); 684 } 685 686 void 687 soabort_oncpu(struct socket *so) 688 { 689 soreference(so); 690 so_pru_abort_direct(so); 691 } 692 693 /* 694 * so is passed in ref'd, which becomes owned by 695 * the cleared SS_NOFDREF flag. 696 */ 697 void 698 soaccept_generic(struct socket *so) 699 { 700 if ((so->so_state & SS_NOFDREF) == 0) 701 panic("soaccept: !NOFDREF"); 702 soclrstate(so, SS_NOFDREF); /* owned by lack of SS_NOFDREF */ 703 } 704 705 int 706 soaccept(struct socket *so, struct sockaddr **nam) 707 { 708 int error; 709 710 soaccept_generic(so); 711 error = so_pru_accept(so, nam); 712 return (error); 713 } 714 715 int 716 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td, 717 boolean_t sync) 718 { 719 int error; 720 721 if (so->so_options & SO_ACCEPTCONN) 722 return (EOPNOTSUPP); 723 /* 724 * If protocol is connection-based, can only connect once. 725 * Otherwise, if connected, try to disconnect first. 726 * This allows user to disconnect by connecting to, e.g., 727 * a null address. 728 */ 729 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 730 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 731 (error = sodisconnect(so)))) { 732 error = EISCONN; 733 } else { 734 /* 735 * Prevent accumulated error from previous connection 736 * from biting us. 737 */ 738 so->so_error = 0; 739 if (!sync && so->so_proto->pr_usrreqs->pru_preconnect) 740 error = so_pru_connect_async(so, nam, td); 741 else 742 error = so_pru_connect(so, nam, td); 743 } 744 return (error); 745 } 746 747 int 748 soconnect2(struct socket *so1, struct socket *so2) 749 { 750 int error; 751 752 error = so_pru_connect2(so1, so2); 753 return (error); 754 } 755 756 int 757 sodisconnect(struct socket *so) 758 { 759 int error; 760 761 if ((so->so_state & SS_ISCONNECTED) == 0) { 762 error = ENOTCONN; 763 goto bad; 764 } 765 if (so->so_state & SS_ISDISCONNECTING) { 766 error = EALREADY; 767 goto bad; 768 } 769 error = so_pru_disconnect(so); 770 bad: 771 return (error); 772 } 773 774 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 775 /* 776 * Send on a socket. 777 * If send must go all at once and message is larger than 778 * send buffering, then hard error. 779 * Lock against other senders. 780 * If must go all at once and not enough room now, then 781 * inform user that this would block and do nothing. 782 * Otherwise, if nonblocking, send as much as possible. 783 * The data to be sent is described by "uio" if nonzero, 784 * otherwise by the mbuf chain "top" (which must be null 785 * if uio is not). Data provided in mbuf chain must be small 786 * enough to send all at once. 787 * 788 * Returns nonzero on error, timeout or signal; callers 789 * must check for short counts if EINTR/ERESTART are returned. 790 * Data and control buffers are freed on return. 791 */ 792 int 793 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, 794 struct mbuf *top, struct mbuf *control, int flags, 795 struct thread *td) 796 { 797 struct mbuf **mp; 798 struct mbuf *m; 799 size_t resid; 800 int space, len; 801 int clen = 0, error, dontroute, mlen; 802 int atomic = sosendallatonce(so) || top; 803 int pru_flags; 804 805 if (uio) { 806 resid = uio->uio_resid; 807 } else { 808 resid = (size_t)top->m_pkthdr.len; 809 #ifdef INVARIANTS 810 len = 0; 811 for (m = top; m; m = m->m_next) 812 len += m->m_len; 813 KKASSERT(top->m_pkthdr.len == len); 814 #endif 815 } 816 817 /* 818 * WARNING! resid is unsigned, space and len are signed. space 819 * can wind up negative if the sockbuf is overcommitted. 820 * 821 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 822 * type sockets since that's an error. 823 */ 824 if (so->so_type == SOCK_STREAM && (flags & MSG_EOR)) { 825 error = EINVAL; 826 goto out; 827 } 828 829 dontroute = 830 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 831 (so->so_proto->pr_flags & PR_ATOMIC); 832 if (td->td_lwp != NULL) 833 td->td_lwp->lwp_ru.ru_msgsnd++; 834 if (control) 835 clen = control->m_len; 836 #define gotoerr(errcode) { error = errcode; goto release; } 837 838 restart: 839 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags)); 840 if (error) 841 goto out; 842 843 do { 844 if (so->so_state & SS_CANTSENDMORE) 845 gotoerr(EPIPE); 846 if (so->so_error) { 847 error = so->so_error; 848 so->so_error = 0; 849 goto release; 850 } 851 if ((so->so_state & SS_ISCONNECTED) == 0) { 852 /* 853 * `sendto' and `sendmsg' is allowed on a connection- 854 * based socket if it supports implied connect. 855 * Return ENOTCONN if not connected and no address is 856 * supplied. 857 */ 858 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 859 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 860 if ((so->so_state & SS_ISCONFIRMING) == 0 && 861 !(resid == 0 && clen != 0)) 862 gotoerr(ENOTCONN); 863 } else if (addr == NULL) 864 gotoerr(so->so_proto->pr_flags & PR_CONNREQUIRED ? 865 ENOTCONN : EDESTADDRREQ); 866 } 867 if ((atomic && resid > so->so_snd.ssb_hiwat) || 868 clen > so->so_snd.ssb_hiwat) { 869 gotoerr(EMSGSIZE); 870 } 871 space = ssb_space(&so->so_snd); 872 if (flags & MSG_OOB) 873 space += 1024; 874 if ((space < 0 || (size_t)space < resid + clen) && uio && 875 (atomic || space < so->so_snd.ssb_lowat || space < clen)) { 876 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) 877 gotoerr(EWOULDBLOCK); 878 ssb_unlock(&so->so_snd); 879 error = ssb_wait(&so->so_snd); 880 if (error) 881 goto out; 882 goto restart; 883 } 884 mp = ⊤ 885 space -= clen; 886 do { 887 if (uio == NULL) { 888 /* 889 * Data is prepackaged in "top". 890 */ 891 resid = 0; 892 if (flags & MSG_EOR) 893 top->m_flags |= M_EOR; 894 } else do { 895 if (resid > INT_MAX) 896 resid = INT_MAX; 897 m = m_getl((int)resid, M_WAITOK, MT_DATA, 898 top == NULL ? M_PKTHDR : 0, &mlen); 899 if (top == NULL) { 900 m->m_pkthdr.len = 0; 901 m->m_pkthdr.rcvif = NULL; 902 } 903 len = imin((int)szmin(mlen, resid), space); 904 if (resid < MINCLSIZE) { 905 /* 906 * For datagram protocols, leave room 907 * for protocol headers in first mbuf. 908 */ 909 if (atomic && top == NULL && len < mlen) 910 MH_ALIGN(m, len); 911 } 912 space -= len; 913 error = uiomove(mtod(m, caddr_t), (size_t)len, uio); 914 resid = uio->uio_resid; 915 m->m_len = len; 916 *mp = m; 917 top->m_pkthdr.len += len; 918 if (error) 919 goto release; 920 mp = &m->m_next; 921 if (resid == 0) { 922 if (flags & MSG_EOR) 923 top->m_flags |= M_EOR; 924 break; 925 } 926 } while (space > 0 && atomic); 927 if (dontroute) 928 so->so_options |= SO_DONTROUTE; 929 if (flags & MSG_OOB) { 930 pru_flags = PRUS_OOB; 931 } else if ((flags & MSG_EOF) && 932 (so->so_proto->pr_flags & PR_IMPLOPCL) && 933 (resid == 0)) { 934 /* 935 * If the user set MSG_EOF, the protocol 936 * understands this flag and nothing left to 937 * send then use PRU_SEND_EOF instead of PRU_SEND. 938 */ 939 pru_flags = PRUS_EOF; 940 } else if (resid > 0 && space > 0) { 941 /* If there is more to send, set PRUS_MORETOCOME */ 942 pru_flags = PRUS_MORETOCOME; 943 } else { 944 pru_flags = 0; 945 } 946 /* 947 * XXX all the SS_CANTSENDMORE checks previously 948 * done could be out of date. We could have recieved 949 * a reset packet in an interrupt or maybe we slept 950 * while doing page faults in uiomove() etc. We could 951 * probably recheck again inside the splnet() protection 952 * here, but there are probably other places that this 953 * also happens. We must rethink this. 954 */ 955 error = so_pru_send(so, pru_flags, top, addr, control, td); 956 if (dontroute) 957 so->so_options &= ~SO_DONTROUTE; 958 clen = 0; 959 control = NULL; 960 top = NULL; 961 mp = ⊤ 962 if (error) 963 goto release; 964 } while (resid && space > 0); 965 } while (resid); 966 967 release: 968 ssb_unlock(&so->so_snd); 969 out: 970 if (top) 971 m_freem(top); 972 if (control) 973 m_freem(control); 974 return (error); 975 } 976 977 #ifdef INET 978 /* 979 * A specialization of sosend() for UDP based on protocol-specific knowledge: 980 * so->so_proto->pr_flags has the PR_ATOMIC field set. This means that 981 * sosendallatonce() returns true, 982 * the "atomic" variable is true, 983 * and sosendudp() blocks until space is available for the entire send. 984 * so->so_proto->pr_flags does not have the PR_CONNREQUIRED or 985 * PR_IMPLOPCL flags set. 986 * UDP has no out-of-band data. 987 * UDP has no control data. 988 * UDP does not support MSG_EOR. 989 */ 990 int 991 sosendudp(struct socket *so, struct sockaddr *addr, struct uio *uio, 992 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 993 { 994 size_t resid; 995 int error, pru_flags = 0; 996 int space; 997 998 if (td->td_lwp != NULL) 999 td->td_lwp->lwp_ru.ru_msgsnd++; 1000 if (control) 1001 m_freem(control); 1002 1003 KASSERT((uio && !top) || (top && !uio), ("bad arguments to sosendudp")); 1004 resid = uio ? uio->uio_resid : (size_t)top->m_pkthdr.len; 1005 1006 restart: 1007 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags)); 1008 if (error) 1009 goto out; 1010 1011 if (so->so_state & SS_CANTSENDMORE) 1012 gotoerr(EPIPE); 1013 if (so->so_error) { 1014 error = so->so_error; 1015 so->so_error = 0; 1016 goto release; 1017 } 1018 if (!(so->so_state & SS_ISCONNECTED) && addr == NULL) 1019 gotoerr(EDESTADDRREQ); 1020 if (resid > so->so_snd.ssb_hiwat) 1021 gotoerr(EMSGSIZE); 1022 space = ssb_space(&so->so_snd); 1023 if (uio && (space < 0 || (size_t)space < resid)) { 1024 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) 1025 gotoerr(EWOULDBLOCK); 1026 ssb_unlock(&so->so_snd); 1027 error = ssb_wait(&so->so_snd); 1028 if (error) 1029 goto out; 1030 goto restart; 1031 } 1032 1033 if (uio) { 1034 int hdrlen = max_hdr; 1035 1036 /* 1037 * We try to optimize out the additional mbuf 1038 * allocations in M_PREPEND() on output path, e.g. 1039 * - udp_output(), when it tries to prepend protocol 1040 * headers. 1041 * - Link layer output function, when it tries to 1042 * prepend link layer header. 1043 * 1044 * This probably will not benefit any data that will 1045 * be fragmented, so this optimization is only performed 1046 * when the size of data and max size of protocol+link 1047 * headers fit into one mbuf cluster. 1048 */ 1049 if (uio->uio_resid > MCLBYTES - hdrlen || 1050 !udp_sosend_prepend) { 1051 top = m_uiomove(uio); 1052 if (top == NULL) 1053 goto release; 1054 } else { 1055 int nsize; 1056 1057 top = m_getl(uio->uio_resid + hdrlen, M_WAITOK, 1058 MT_DATA, M_PKTHDR, &nsize); 1059 KASSERT(nsize >= uio->uio_resid + hdrlen, 1060 ("sosendudp invalid nsize %d, " 1061 "resid %zu, hdrlen %d", 1062 nsize, uio->uio_resid, hdrlen)); 1063 1064 top->m_len = uio->uio_resid; 1065 top->m_pkthdr.len = uio->uio_resid; 1066 top->m_data += hdrlen; 1067 1068 error = uiomove(mtod(top, caddr_t), top->m_len, uio); 1069 if (error) 1070 goto out; 1071 } 1072 } 1073 1074 if (flags & MSG_DONTROUTE) 1075 pru_flags |= PRUS_DONTROUTE; 1076 1077 if (udp_sosend_async && (flags & MSG_SYNC) == 0) { 1078 so_pru_send_async(so, pru_flags, top, addr, NULL, td); 1079 error = 0; 1080 } else { 1081 error = so_pru_send(so, pru_flags, top, addr, NULL, td); 1082 } 1083 top = NULL; /* sent or freed in lower layer */ 1084 1085 release: 1086 ssb_unlock(&so->so_snd); 1087 out: 1088 if (top) 1089 m_freem(top); 1090 return (error); 1091 } 1092 1093 int 1094 sosendtcp(struct socket *so, struct sockaddr *addr, struct uio *uio, 1095 struct mbuf *top, struct mbuf *control, int flags, 1096 struct thread *td) 1097 { 1098 struct mbuf **mp; 1099 struct mbuf *m; 1100 size_t resid; 1101 int space, len; 1102 int error, mlen; 1103 int allatonce; 1104 int pru_flags; 1105 1106 if (uio) { 1107 KKASSERT(top == NULL); 1108 allatonce = 0; 1109 resid = uio->uio_resid; 1110 } else { 1111 allatonce = 1; 1112 resid = (size_t)top->m_pkthdr.len; 1113 #ifdef INVARIANTS 1114 len = 0; 1115 for (m = top; m; m = m->m_next) 1116 len += m->m_len; 1117 KKASSERT(top->m_pkthdr.len == len); 1118 #endif 1119 } 1120 1121 /* 1122 * WARNING! resid is unsigned, space and len are signed. space 1123 * can wind up negative if the sockbuf is overcommitted. 1124 * 1125 * Also check to make sure that MSG_EOR isn't used on TCP 1126 */ 1127 if (flags & MSG_EOR) { 1128 error = EINVAL; 1129 goto out; 1130 } 1131 1132 if (control) { 1133 /* TCP doesn't do control messages (rights, creds, etc) */ 1134 if (control->m_len) { 1135 error = EINVAL; 1136 goto out; 1137 } 1138 m_freem(control); /* empty control, just free it */ 1139 control = NULL; 1140 } 1141 1142 if (td->td_lwp != NULL) 1143 td->td_lwp->lwp_ru.ru_msgsnd++; 1144 1145 #define gotoerr(errcode) { error = errcode; goto release; } 1146 1147 restart: 1148 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags)); 1149 if (error) 1150 goto out; 1151 1152 do { 1153 if (so->so_state & SS_CANTSENDMORE) 1154 gotoerr(EPIPE); 1155 if (so->so_error) { 1156 error = so->so_error; 1157 so->so_error = 0; 1158 goto release; 1159 } 1160 if ((so->so_state & SS_ISCONNECTED) == 0 && 1161 (so->so_state & SS_ISCONFIRMING) == 0) 1162 gotoerr(ENOTCONN); 1163 if (allatonce && resid > so->so_snd.ssb_hiwat) 1164 gotoerr(EMSGSIZE); 1165 1166 space = ssb_space_prealloc(&so->so_snd); 1167 if (flags & MSG_OOB) 1168 space += 1024; 1169 if ((space < 0 || (size_t)space < resid) && !allatonce && 1170 space < so->so_snd.ssb_lowat) { 1171 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) 1172 gotoerr(EWOULDBLOCK); 1173 ssb_unlock(&so->so_snd); 1174 error = ssb_wait(&so->so_snd); 1175 if (error) 1176 goto out; 1177 goto restart; 1178 } 1179 mp = ⊤ 1180 do { 1181 int cnt = 0, async = 0; 1182 1183 if (uio == NULL) { 1184 /* 1185 * Data is prepackaged in "top". 1186 */ 1187 resid = 0; 1188 } else do { 1189 if (resid > INT_MAX) 1190 resid = INT_MAX; 1191 if (tcp_sosend_jcluster) { 1192 m = m_getlj((int)resid, M_WAITOK, MT_DATA, 1193 top == NULL ? M_PKTHDR : 0, &mlen); 1194 } else { 1195 m = m_getl((int)resid, M_WAITOK, MT_DATA, 1196 top == NULL ? M_PKTHDR : 0, &mlen); 1197 } 1198 if (top == NULL) { 1199 m->m_pkthdr.len = 0; 1200 m->m_pkthdr.rcvif = NULL; 1201 } 1202 len = imin((int)szmin(mlen, resid), space); 1203 space -= len; 1204 error = uiomove(mtod(m, caddr_t), (size_t)len, uio); 1205 resid = uio->uio_resid; 1206 m->m_len = len; 1207 *mp = m; 1208 top->m_pkthdr.len += len; 1209 if (error) 1210 goto release; 1211 mp = &m->m_next; 1212 if (resid == 0) 1213 break; 1214 ++cnt; 1215 } while (space > 0 && cnt < tcp_sosend_agglim); 1216 1217 if (tcp_sosend_async) 1218 async = 1; 1219 1220 if (flags & MSG_OOB) { 1221 pru_flags = PRUS_OOB; 1222 async = 0; 1223 } else if ((flags & MSG_EOF) && resid == 0) { 1224 pru_flags = PRUS_EOF; 1225 } else if (resid > 0 && space > 0) { 1226 /* If there is more to send, set PRUS_MORETOCOME */ 1227 pru_flags = PRUS_MORETOCOME; 1228 async = 1; 1229 } else { 1230 pru_flags = 0; 1231 } 1232 1233 if (flags & MSG_SYNC) 1234 async = 0; 1235 1236 /* 1237 * XXX all the SS_CANTSENDMORE checks previously 1238 * done could be out of date. We could have recieved 1239 * a reset packet in an interrupt or maybe we slept 1240 * while doing page faults in uiomove() etc. We could 1241 * probably recheck again inside the splnet() protection 1242 * here, but there are probably other places that this 1243 * also happens. We must rethink this. 1244 */ 1245 for (m = top; m; m = m->m_next) 1246 ssb_preallocstream(&so->so_snd, m); 1247 if (!async) { 1248 error = so_pru_send(so, pru_flags, top, 1249 NULL, NULL, td); 1250 } else { 1251 so_pru_send_async(so, pru_flags, top, 1252 NULL, NULL, td); 1253 error = 0; 1254 } 1255 1256 top = NULL; 1257 mp = ⊤ 1258 if (error) 1259 goto release; 1260 } while (resid && space > 0); 1261 } while (resid); 1262 1263 release: 1264 ssb_unlock(&so->so_snd); 1265 out: 1266 if (top) 1267 m_freem(top); 1268 if (control) 1269 m_freem(control); 1270 return (error); 1271 } 1272 #endif 1273 1274 /* 1275 * Implement receive operations on a socket. 1276 * 1277 * We depend on the way that records are added to the signalsockbuf 1278 * by sbappend*. In particular, each record (mbufs linked through m_next) 1279 * must begin with an address if the protocol so specifies, 1280 * followed by an optional mbuf or mbufs containing ancillary data, 1281 * and then zero or more mbufs of data. 1282 * 1283 * Although the signalsockbuf is locked, new data may still be appended. 1284 * A token inside the ssb_lock deals with MP issues and still allows 1285 * the network to access the socket if we block in a uio. 1286 * 1287 * The caller may receive the data as a single mbuf chain by supplying 1288 * an mbuf **mp0 for use in returning the chain. The uio is then used 1289 * only for the count in uio_resid. 1290 */ 1291 int 1292 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio, 1293 struct sockbuf *sio, struct mbuf **controlp, int *flagsp) 1294 { 1295 struct mbuf *m, *n; 1296 struct mbuf *free_chain = NULL; 1297 int flags, len, error, offset; 1298 struct protosw *pr = so->so_proto; 1299 int moff, type = 0; 1300 size_t resid, orig_resid; 1301 1302 if (uio) 1303 resid = uio->uio_resid; 1304 else 1305 resid = (size_t)(sio->sb_climit - sio->sb_cc); 1306 orig_resid = resid; 1307 1308 if (psa) 1309 *psa = NULL; 1310 if (controlp) 1311 *controlp = NULL; 1312 if (flagsp) 1313 flags = *flagsp &~ MSG_EOR; 1314 else 1315 flags = 0; 1316 if (flags & MSG_OOB) { 1317 m = m_get(M_WAITOK, MT_DATA); 1318 if (m == NULL) 1319 return (ENOBUFS); 1320 error = so_pru_rcvoob(so, m, flags & MSG_PEEK); 1321 if (error) 1322 goto bad; 1323 if (sio) { 1324 do { 1325 sbappend(sio, m); 1326 KKASSERT(resid >= (size_t)m->m_len); 1327 resid -= (size_t)m->m_len; 1328 } while (resid > 0 && m); 1329 } else { 1330 do { 1331 uio->uio_resid = resid; 1332 error = uiomove(mtod(m, caddr_t), 1333 (int)szmin(resid, m->m_len), 1334 uio); 1335 resid = uio->uio_resid; 1336 m = m_free(m); 1337 } while (uio->uio_resid && error == 0 && m); 1338 } 1339 bad: 1340 if (m) 1341 m_freem(m); 1342 return (error); 1343 } 1344 if ((so->so_state & SS_ISCONFIRMING) && resid) 1345 so_pru_rcvd(so, 0); 1346 1347 /* 1348 * The token interlocks against the protocol thread while 1349 * ssb_lock is a blocking lock against other userland entities. 1350 */ 1351 lwkt_gettoken(&so->so_rcv.ssb_token); 1352 restart: 1353 error = ssb_lock(&so->so_rcv, SBLOCKWAIT(flags)); 1354 if (error) 1355 goto done; 1356 1357 m = so->so_rcv.ssb_mb; 1358 /* 1359 * If we have less data than requested, block awaiting more 1360 * (subject to any timeout) if: 1361 * 1. the current count is less than the low water mark, or 1362 * 2. MSG_WAITALL is set, and it is possible to do the entire 1363 * receive operation at once if we block (resid <= hiwat). 1364 * 3. MSG_DONTWAIT is not set 1365 * If MSG_WAITALL is set but resid is larger than the receive buffer, 1366 * we have to do the receive in sections, and thus risk returning 1367 * a short count if a timeout or signal occurs after we start. 1368 */ 1369 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && 1370 (size_t)so->so_rcv.ssb_cc < resid) && 1371 (so->so_rcv.ssb_cc < so->so_rcv.ssb_lowat || 1372 ((flags & MSG_WAITALL) && resid <= (size_t)so->so_rcv.ssb_hiwat)) && 1373 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) { 1374 KASSERT(m != NULL || !so->so_rcv.ssb_cc, ("receive 1")); 1375 if (so->so_error) { 1376 if (m) 1377 goto dontblock; 1378 error = so->so_error; 1379 if ((flags & MSG_PEEK) == 0) 1380 so->so_error = 0; 1381 goto release; 1382 } 1383 if (so->so_state & SS_CANTRCVMORE) { 1384 if (m) 1385 goto dontblock; 1386 else 1387 goto release; 1388 } 1389 for (; m; m = m->m_next) { 1390 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 1391 m = so->so_rcv.ssb_mb; 1392 goto dontblock; 1393 } 1394 } 1395 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 1396 (pr->pr_flags & PR_CONNREQUIRED)) { 1397 error = ENOTCONN; 1398 goto release; 1399 } 1400 if (resid == 0) 1401 goto release; 1402 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) { 1403 error = EWOULDBLOCK; 1404 goto release; 1405 } 1406 ssb_unlock(&so->so_rcv); 1407 error = ssb_wait(&so->so_rcv); 1408 if (error) 1409 goto done; 1410 goto restart; 1411 } 1412 dontblock: 1413 if (uio && uio->uio_td && uio->uio_td->td_proc) 1414 uio->uio_td->td_lwp->lwp_ru.ru_msgrcv++; 1415 1416 /* 1417 * note: m should be == sb_mb here. Cache the next record while 1418 * cleaning up. Note that calling m_free*() will break out critical 1419 * section. 1420 */ 1421 KKASSERT(m == so->so_rcv.ssb_mb); 1422 1423 /* 1424 * Skip any address mbufs prepending the record. 1425 */ 1426 if (pr->pr_flags & PR_ADDR) { 1427 KASSERT(m->m_type == MT_SONAME, ("receive 1a")); 1428 orig_resid = 0; 1429 if (psa) 1430 *psa = dup_sockaddr(mtod(m, struct sockaddr *)); 1431 if (flags & MSG_PEEK) 1432 m = m->m_next; 1433 else 1434 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain); 1435 } 1436 1437 /* 1438 * Skip any control mbufs prepending the record. 1439 */ 1440 while (m && m->m_type == MT_CONTROL && error == 0) { 1441 if (flags & MSG_PEEK) { 1442 if (controlp) 1443 *controlp = m_copy(m, 0, m->m_len); 1444 m = m->m_next; /* XXX race */ 1445 } else { 1446 if (controlp) { 1447 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL); 1448 if (pr->pr_domain->dom_externalize && 1449 mtod(m, struct cmsghdr *)->cmsg_type == 1450 SCM_RIGHTS) 1451 error = (*pr->pr_domain->dom_externalize)(m); 1452 *controlp = m; 1453 m = n; 1454 } else { 1455 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain); 1456 } 1457 } 1458 if (controlp && *controlp) { 1459 orig_resid = 0; 1460 controlp = &(*controlp)->m_next; 1461 } 1462 } 1463 1464 /* 1465 * flag OOB data. 1466 */ 1467 if (m) { 1468 type = m->m_type; 1469 if (type == MT_OOBDATA) 1470 flags |= MSG_OOB; 1471 } 1472 1473 /* 1474 * Copy to the UIO or mbuf return chain (*mp). 1475 */ 1476 moff = 0; 1477 offset = 0; 1478 while (m && resid > 0 && error == 0) { 1479 if (m->m_type == MT_OOBDATA) { 1480 if (type != MT_OOBDATA) 1481 break; 1482 } else if (type == MT_OOBDATA) 1483 break; 1484 else 1485 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER, 1486 ("receive 3")); 1487 soclrstate(so, SS_RCVATMARK); 1488 len = (resid > INT_MAX) ? INT_MAX : resid; 1489 if (so->so_oobmark && len > so->so_oobmark - offset) 1490 len = so->so_oobmark - offset; 1491 if (len > m->m_len - moff) 1492 len = m->m_len - moff; 1493 1494 /* 1495 * Copy out to the UIO or pass the mbufs back to the SIO. 1496 * The SIO is dealt with when we eat the mbuf, but deal 1497 * with the resid here either way. 1498 */ 1499 if (uio) { 1500 uio->uio_resid = resid; 1501 error = uiomove(mtod(m, caddr_t) + moff, len, uio); 1502 resid = uio->uio_resid; 1503 if (error) 1504 goto release; 1505 } else { 1506 resid -= (size_t)len; 1507 } 1508 1509 /* 1510 * Eat the entire mbuf or just a piece of it 1511 */ 1512 if (len == m->m_len - moff) { 1513 if (m->m_flags & M_EOR) 1514 flags |= MSG_EOR; 1515 if (flags & MSG_PEEK) { 1516 m = m->m_next; 1517 moff = 0; 1518 } else { 1519 if (sio) { 1520 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL); 1521 sbappend(sio, m); 1522 m = n; 1523 } else { 1524 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain); 1525 } 1526 } 1527 } else { 1528 if (flags & MSG_PEEK) { 1529 moff += len; 1530 } else { 1531 if (sio) { 1532 n = m_copym(m, 0, len, M_WAITOK); 1533 if (n) 1534 sbappend(sio, n); 1535 } 1536 m->m_data += len; 1537 m->m_len -= len; 1538 so->so_rcv.ssb_cc -= len; 1539 } 1540 } 1541 if (so->so_oobmark) { 1542 if ((flags & MSG_PEEK) == 0) { 1543 so->so_oobmark -= len; 1544 if (so->so_oobmark == 0) { 1545 sosetstate(so, SS_RCVATMARK); 1546 break; 1547 } 1548 } else { 1549 offset += len; 1550 if (offset == so->so_oobmark) 1551 break; 1552 } 1553 } 1554 if (flags & MSG_EOR) 1555 break; 1556 /* 1557 * If the MSG_WAITALL flag is set (for non-atomic socket), 1558 * we must not quit until resid == 0 or an error 1559 * termination. If a signal/timeout occurs, return 1560 * with a short count but without error. 1561 * Keep signalsockbuf locked against other readers. 1562 */ 1563 while ((flags & MSG_WAITALL) && m == NULL && 1564 resid > 0 && !sosendallatonce(so) && 1565 so->so_rcv.ssb_mb == NULL) { 1566 if (so->so_error || so->so_state & SS_CANTRCVMORE) 1567 break; 1568 /* 1569 * The window might have closed to zero, make 1570 * sure we send an ack now that we've drained 1571 * the buffer or we might end up blocking until 1572 * the idle takes over (5 seconds). 1573 */ 1574 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 1575 so_pru_rcvd(so, flags); 1576 error = ssb_wait(&so->so_rcv); 1577 if (error) { 1578 ssb_unlock(&so->so_rcv); 1579 error = 0; 1580 goto done; 1581 } 1582 m = so->so_rcv.ssb_mb; 1583 } 1584 } 1585 1586 /* 1587 * If an atomic read was requested but unread data still remains 1588 * in the record, set MSG_TRUNC. 1589 */ 1590 if (m && pr->pr_flags & PR_ATOMIC) 1591 flags |= MSG_TRUNC; 1592 1593 /* 1594 * Cleanup. If an atomic read was requested drop any unread data. 1595 */ 1596 if ((flags & MSG_PEEK) == 0) { 1597 if (m && (pr->pr_flags & PR_ATOMIC)) 1598 sbdroprecord(&so->so_rcv.sb); 1599 if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb) 1600 so_pru_rcvd(so, flags); 1601 } 1602 1603 if (orig_resid == resid && orig_resid && 1604 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { 1605 ssb_unlock(&so->so_rcv); 1606 goto restart; 1607 } 1608 1609 if (flagsp) 1610 *flagsp |= flags; 1611 release: 1612 ssb_unlock(&so->so_rcv); 1613 done: 1614 lwkt_reltoken(&so->so_rcv.ssb_token); 1615 if (free_chain) 1616 m_freem(free_chain); 1617 return (error); 1618 } 1619 1620 int 1621 sorecvtcp(struct socket *so, struct sockaddr **psa, struct uio *uio, 1622 struct sockbuf *sio, struct mbuf **controlp, int *flagsp) 1623 { 1624 struct mbuf *m, *n; 1625 struct mbuf *free_chain = NULL; 1626 int flags, len, error, offset; 1627 struct protosw *pr = so->so_proto; 1628 int moff; 1629 int didoob; 1630 size_t resid, orig_resid, restmp; 1631 1632 if (uio) 1633 resid = uio->uio_resid; 1634 else 1635 resid = (size_t)(sio->sb_climit - sio->sb_cc); 1636 orig_resid = resid; 1637 1638 if (psa) 1639 *psa = NULL; 1640 if (controlp) 1641 *controlp = NULL; 1642 if (flagsp) 1643 flags = *flagsp &~ MSG_EOR; 1644 else 1645 flags = 0; 1646 if (flags & MSG_OOB) { 1647 m = m_get(M_WAITOK, MT_DATA); 1648 if (m == NULL) 1649 return (ENOBUFS); 1650 error = so_pru_rcvoob(so, m, flags & MSG_PEEK); 1651 if (error) 1652 goto bad; 1653 if (sio) { 1654 do { 1655 sbappend(sio, m); 1656 KKASSERT(resid >= (size_t)m->m_len); 1657 resid -= (size_t)m->m_len; 1658 } while (resid > 0 && m); 1659 } else { 1660 do { 1661 uio->uio_resid = resid; 1662 error = uiomove(mtod(m, caddr_t), 1663 (int)szmin(resid, m->m_len), 1664 uio); 1665 resid = uio->uio_resid; 1666 m = m_free(m); 1667 } while (uio->uio_resid && error == 0 && m); 1668 } 1669 bad: 1670 if (m) 1671 m_freem(m); 1672 return (error); 1673 } 1674 1675 /* 1676 * The token interlocks against the protocol thread while 1677 * ssb_lock is a blocking lock against other userland entities. 1678 * 1679 * Lock a limited number of mbufs (not all, so sbcompress() still 1680 * works well). The token is used as an interlock for sbwait() so 1681 * release it afterwords. 1682 */ 1683 restart: 1684 error = ssb_lock(&so->so_rcv, SBLOCKWAIT(flags)); 1685 if (error) 1686 goto done; 1687 1688 lwkt_gettoken(&so->so_rcv.ssb_token); 1689 m = so->so_rcv.ssb_mb; 1690 1691 /* 1692 * If we have less data than requested, block awaiting more 1693 * (subject to any timeout) if: 1694 * 1. the current count is less than the low water mark, or 1695 * 2. MSG_WAITALL is set, and it is possible to do the entire 1696 * receive operation at once if we block (resid <= hiwat). 1697 * 3. MSG_DONTWAIT is not set 1698 * If MSG_WAITALL is set but resid is larger than the receive buffer, 1699 * we have to do the receive in sections, and thus risk returning 1700 * a short count if a timeout or signal occurs after we start. 1701 */ 1702 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && 1703 (size_t)so->so_rcv.ssb_cc < resid) && 1704 (so->so_rcv.ssb_cc < so->so_rcv.ssb_lowat || 1705 ((flags & MSG_WAITALL) && resid <= (size_t)so->so_rcv.ssb_hiwat)))) { 1706 KASSERT(m != NULL || !so->so_rcv.ssb_cc, ("receive 1")); 1707 if (so->so_error) { 1708 if (m) 1709 goto dontblock; 1710 lwkt_reltoken(&so->so_rcv.ssb_token); 1711 error = so->so_error; 1712 if ((flags & MSG_PEEK) == 0) 1713 so->so_error = 0; 1714 goto release; 1715 } 1716 if (so->so_state & SS_CANTRCVMORE) { 1717 if (m) 1718 goto dontblock; 1719 lwkt_reltoken(&so->so_rcv.ssb_token); 1720 goto release; 1721 } 1722 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 1723 (pr->pr_flags & PR_CONNREQUIRED)) { 1724 lwkt_reltoken(&so->so_rcv.ssb_token); 1725 error = ENOTCONN; 1726 goto release; 1727 } 1728 if (resid == 0) { 1729 lwkt_reltoken(&so->so_rcv.ssb_token); 1730 goto release; 1731 } 1732 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) { 1733 lwkt_reltoken(&so->so_rcv.ssb_token); 1734 error = EWOULDBLOCK; 1735 goto release; 1736 } 1737 ssb_unlock(&so->so_rcv); 1738 error = ssb_wait(&so->so_rcv); 1739 lwkt_reltoken(&so->so_rcv.ssb_token); 1740 if (error) 1741 goto done; 1742 goto restart; 1743 } 1744 1745 /* 1746 * Token still held 1747 */ 1748 dontblock: 1749 n = m; 1750 restmp = 0; 1751 while (n && restmp < resid) { 1752 n->m_flags |= M_SOLOCKED; 1753 restmp += n->m_len; 1754 if (n->m_next == NULL) 1755 n = n->m_nextpkt; 1756 else 1757 n = n->m_next; 1758 } 1759 1760 /* 1761 * Release token for loop 1762 */ 1763 lwkt_reltoken(&so->so_rcv.ssb_token); 1764 if (uio && uio->uio_td && uio->uio_td->td_proc) 1765 uio->uio_td->td_lwp->lwp_ru.ru_msgrcv++; 1766 1767 /* 1768 * note: m should be == sb_mb here. Cache the next record while 1769 * cleaning up. Note that calling m_free*() will break out critical 1770 * section. 1771 */ 1772 KKASSERT(m == so->so_rcv.ssb_mb); 1773 1774 /* 1775 * Copy to the UIO or mbuf return chain (*mp). 1776 * 1777 * NOTE: Token is not held for loop 1778 */ 1779 moff = 0; 1780 offset = 0; 1781 didoob = 0; 1782 1783 while (m && (m->m_flags & M_SOLOCKED) && resid > 0 && error == 0) { 1784 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER, 1785 ("receive 3")); 1786 1787 soclrstate(so, SS_RCVATMARK); 1788 len = (resid > INT_MAX) ? INT_MAX : resid; 1789 if (so->so_oobmark && len > so->so_oobmark - offset) 1790 len = so->so_oobmark - offset; 1791 if (len > m->m_len - moff) 1792 len = m->m_len - moff; 1793 1794 /* 1795 * Copy out to the UIO or pass the mbufs back to the SIO. 1796 * The SIO is dealt with when we eat the mbuf, but deal 1797 * with the resid here either way. 1798 */ 1799 if (uio) { 1800 uio->uio_resid = resid; 1801 error = uiomove(mtod(m, caddr_t) + moff, len, uio); 1802 resid = uio->uio_resid; 1803 if (error) 1804 goto release; 1805 } else { 1806 resid -= (size_t)len; 1807 } 1808 1809 /* 1810 * Eat the entire mbuf or just a piece of it 1811 */ 1812 offset += len; 1813 if (len == m->m_len - moff) { 1814 m = m->m_next; 1815 moff = 0; 1816 } else { 1817 moff += len; 1818 } 1819 1820 /* 1821 * Check oobmark 1822 */ 1823 if (so->so_oobmark && offset == so->so_oobmark) { 1824 didoob = 1; 1825 break; 1826 } 1827 } 1828 1829 /* 1830 * Synchronize sockbuf with data we read. 1831 * 1832 * NOTE: (m) is junk on entry (it could be left over from the 1833 * previous loop). 1834 */ 1835 if ((flags & MSG_PEEK) == 0) { 1836 lwkt_gettoken(&so->so_rcv.ssb_token); 1837 m = so->so_rcv.ssb_mb; 1838 while (m && offset >= m->m_len) { 1839 if (so->so_oobmark) { 1840 so->so_oobmark -= m->m_len; 1841 if (so->so_oobmark == 0) { 1842 sosetstate(so, SS_RCVATMARK); 1843 didoob = 1; 1844 } 1845 } 1846 offset -= m->m_len; 1847 if (sio) { 1848 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL); 1849 sbappend(sio, m); 1850 m = n; 1851 } else { 1852 m = sbunlinkmbuf(&so->so_rcv.sb, 1853 m, &free_chain); 1854 } 1855 } 1856 if (offset) { 1857 KKASSERT(m); 1858 if (sio) { 1859 n = m_copym(m, 0, offset, M_WAITOK); 1860 if (n) 1861 sbappend(sio, n); 1862 } 1863 m->m_data += offset; 1864 m->m_len -= offset; 1865 so->so_rcv.ssb_cc -= offset; 1866 if (so->so_oobmark) { 1867 so->so_oobmark -= offset; 1868 if (so->so_oobmark == 0) { 1869 sosetstate(so, SS_RCVATMARK); 1870 didoob = 1; 1871 } 1872 } 1873 offset = 0; 1874 } 1875 lwkt_reltoken(&so->so_rcv.ssb_token); 1876 } 1877 1878 /* 1879 * If the MSG_WAITALL flag is set (for non-atomic socket), 1880 * we must not quit until resid == 0 or an error termination. 1881 * 1882 * If a signal/timeout occurs, return with a short count but without 1883 * error. 1884 * 1885 * Keep signalsockbuf locked against other readers. 1886 * 1887 * XXX if MSG_PEEK we currently do quit. 1888 */ 1889 if ((flags & MSG_WAITALL) && !(flags & MSG_PEEK) && 1890 didoob == 0 && resid > 0 && 1891 !sosendallatonce(so)) { 1892 lwkt_gettoken(&so->so_rcv.ssb_token); 1893 error = 0; 1894 while ((m = so->so_rcv.ssb_mb) == NULL) { 1895 if (so->so_error || (so->so_state & SS_CANTRCVMORE)) { 1896 error = so->so_error; 1897 break; 1898 } 1899 /* 1900 * The window might have closed to zero, make 1901 * sure we send an ack now that we've drained 1902 * the buffer or we might end up blocking until 1903 * the idle takes over (5 seconds). 1904 */ 1905 if (so->so_pcb) 1906 so_pru_rcvd_async(so); 1907 if (so->so_rcv.ssb_mb == NULL) 1908 error = ssb_wait(&so->so_rcv); 1909 if (error) { 1910 lwkt_reltoken(&so->so_rcv.ssb_token); 1911 ssb_unlock(&so->so_rcv); 1912 error = 0; 1913 goto done; 1914 } 1915 } 1916 if (m && error == 0) 1917 goto dontblock; 1918 lwkt_reltoken(&so->so_rcv.ssb_token); 1919 } 1920 1921 /* 1922 * Token not held here. 1923 * 1924 * Cleanup. If an atomic read was requested drop any unread data XXX 1925 */ 1926 if ((flags & MSG_PEEK) == 0) { 1927 if (so->so_pcb) 1928 so_pru_rcvd_async(so); 1929 } 1930 1931 if (orig_resid == resid && orig_resid && 1932 (so->so_state & SS_CANTRCVMORE) == 0) { 1933 ssb_unlock(&so->so_rcv); 1934 goto restart; 1935 } 1936 1937 if (flagsp) 1938 *flagsp |= flags; 1939 release: 1940 ssb_unlock(&so->so_rcv); 1941 done: 1942 if (free_chain) 1943 m_freem(free_chain); 1944 return (error); 1945 } 1946 1947 /* 1948 * Shut a socket down. Note that we do not get a frontend lock as we 1949 * want to be able to shut the socket down even if another thread is 1950 * blocked in a read(), thus waking it up. 1951 */ 1952 int 1953 soshutdown(struct socket *so, int how) 1954 { 1955 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) 1956 return (EINVAL); 1957 1958 if (how != SHUT_WR) { 1959 /*ssb_lock(&so->so_rcv, M_WAITOK);*/ 1960 sorflush(so); 1961 /*ssb_unlock(&so->so_rcv);*/ 1962 } 1963 if (how != SHUT_RD) 1964 return (so_pru_shutdown(so)); 1965 return (0); 1966 } 1967 1968 void 1969 sorflush(struct socket *so) 1970 { 1971 struct signalsockbuf *ssb = &so->so_rcv; 1972 struct protosw *pr = so->so_proto; 1973 struct signalsockbuf asb; 1974 1975 atomic_set_int(&ssb->ssb_flags, SSB_NOINTR); 1976 1977 lwkt_gettoken(&ssb->ssb_token); 1978 socantrcvmore(so); 1979 asb = *ssb; 1980 1981 /* 1982 * Can't just blow up the ssb structure here 1983 */ 1984 bzero(&ssb->sb, sizeof(ssb->sb)); 1985 ssb->ssb_timeo = 0; 1986 ssb->ssb_lowat = 0; 1987 ssb->ssb_hiwat = 0; 1988 ssb->ssb_mbmax = 0; 1989 atomic_clear_int(&ssb->ssb_flags, SSB_CLEAR_MASK); 1990 1991 if ((pr->pr_flags & PR_RIGHTS) && pr->pr_domain->dom_dispose) 1992 (*pr->pr_domain->dom_dispose)(asb.ssb_mb); 1993 ssb_release(&asb, so); 1994 1995 lwkt_reltoken(&ssb->ssb_token); 1996 } 1997 1998 #ifdef INET 1999 static int 2000 do_setopt_accept_filter(struct socket *so, struct sockopt *sopt) 2001 { 2002 struct accept_filter_arg *afap = NULL; 2003 struct accept_filter *afp; 2004 struct so_accf *af = so->so_accf; 2005 int error = 0; 2006 2007 /* do not set/remove accept filters on non listen sockets */ 2008 if ((so->so_options & SO_ACCEPTCONN) == 0) { 2009 error = EINVAL; 2010 goto out; 2011 } 2012 2013 /* removing the filter */ 2014 if (sopt == NULL) { 2015 if (af != NULL) { 2016 if (af->so_accept_filter != NULL && 2017 af->so_accept_filter->accf_destroy != NULL) { 2018 af->so_accept_filter->accf_destroy(so); 2019 } 2020 if (af->so_accept_filter_str != NULL) { 2021 kfree(af->so_accept_filter_str, M_ACCF); 2022 } 2023 kfree(af, M_ACCF); 2024 so->so_accf = NULL; 2025 } 2026 so->so_options &= ~SO_ACCEPTFILTER; 2027 return (0); 2028 } 2029 /* adding a filter */ 2030 /* must remove previous filter first */ 2031 if (af != NULL) { 2032 error = EINVAL; 2033 goto out; 2034 } 2035 /* don't put large objects on the kernel stack */ 2036 afap = kmalloc(sizeof(*afap), M_TEMP, M_WAITOK); 2037 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap); 2038 afap->af_name[sizeof(afap->af_name)-1] = '\0'; 2039 afap->af_arg[sizeof(afap->af_arg)-1] = '\0'; 2040 if (error) 2041 goto out; 2042 afp = accept_filt_get(afap->af_name); 2043 if (afp == NULL) { 2044 error = ENOENT; 2045 goto out; 2046 } 2047 af = kmalloc(sizeof(*af), M_ACCF, M_WAITOK | M_ZERO); 2048 if (afp->accf_create != NULL) { 2049 if (afap->af_name[0] != '\0') { 2050 int len = strlen(afap->af_name) + 1; 2051 2052 af->so_accept_filter_str = kmalloc(len, M_ACCF, 2053 M_WAITOK); 2054 strcpy(af->so_accept_filter_str, afap->af_name); 2055 } 2056 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg); 2057 if (af->so_accept_filter_arg == NULL) { 2058 kfree(af->so_accept_filter_str, M_ACCF); 2059 kfree(af, M_ACCF); 2060 so->so_accf = NULL; 2061 error = EINVAL; 2062 goto out; 2063 } 2064 } 2065 af->so_accept_filter = afp; 2066 so->so_accf = af; 2067 so->so_options |= SO_ACCEPTFILTER; 2068 out: 2069 if (afap != NULL) 2070 kfree(afap, M_TEMP); 2071 return (error); 2072 } 2073 #endif /* INET */ 2074 2075 /* 2076 * Perhaps this routine, and sooptcopyout(), below, ought to come in 2077 * an additional variant to handle the case where the option value needs 2078 * to be some kind of integer, but not a specific size. 2079 * In addition to their use here, these functions are also called by the 2080 * protocol-level pr_ctloutput() routines. 2081 */ 2082 int 2083 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen) 2084 { 2085 return soopt_to_kbuf(sopt, buf, len, minlen); 2086 } 2087 2088 int 2089 soopt_to_kbuf(struct sockopt *sopt, void *buf, size_t len, size_t minlen) 2090 { 2091 size_t valsize; 2092 2093 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val)); 2094 KKASSERT(kva_p(buf)); 2095 2096 /* 2097 * If the user gives us more than we wanted, we ignore it, 2098 * but if we don't get the minimum length the caller 2099 * wants, we return EINVAL. On success, sopt->sopt_valsize 2100 * is set to however much we actually retrieved. 2101 */ 2102 if ((valsize = sopt->sopt_valsize) < minlen) 2103 return EINVAL; 2104 if (valsize > len) 2105 sopt->sopt_valsize = valsize = len; 2106 2107 bcopy(sopt->sopt_val, buf, valsize); 2108 return 0; 2109 } 2110 2111 2112 int 2113 sosetopt(struct socket *so, struct sockopt *sopt) 2114 { 2115 int error, optval; 2116 struct linger l; 2117 struct timeval tv; 2118 u_long val; 2119 struct signalsockbuf *sotmp; 2120 2121 error = 0; 2122 sopt->sopt_dir = SOPT_SET; 2123 if (sopt->sopt_level != SOL_SOCKET) { 2124 if (so->so_proto && so->so_proto->pr_ctloutput) { 2125 return (so_pr_ctloutput(so, sopt)); 2126 } 2127 error = ENOPROTOOPT; 2128 } else { 2129 switch (sopt->sopt_name) { 2130 #ifdef INET 2131 case SO_ACCEPTFILTER: 2132 error = do_setopt_accept_filter(so, sopt); 2133 if (error) 2134 goto bad; 2135 break; 2136 #endif /* INET */ 2137 case SO_LINGER: 2138 error = sooptcopyin(sopt, &l, sizeof l, sizeof l); 2139 if (error) 2140 goto bad; 2141 2142 so->so_linger = l.l_linger; 2143 if (l.l_onoff) 2144 so->so_options |= SO_LINGER; 2145 else 2146 so->so_options &= ~SO_LINGER; 2147 break; 2148 2149 case SO_DEBUG: 2150 case SO_KEEPALIVE: 2151 case SO_DONTROUTE: 2152 case SO_USELOOPBACK: 2153 case SO_BROADCAST: 2154 case SO_REUSEADDR: 2155 case SO_REUSEPORT: 2156 case SO_OOBINLINE: 2157 case SO_TIMESTAMP: 2158 case SO_NOSIGPIPE: 2159 error = sooptcopyin(sopt, &optval, sizeof optval, 2160 sizeof optval); 2161 if (error) 2162 goto bad; 2163 if (optval) 2164 so->so_options |= sopt->sopt_name; 2165 else 2166 so->so_options &= ~sopt->sopt_name; 2167 break; 2168 2169 case SO_SNDBUF: 2170 case SO_RCVBUF: 2171 case SO_SNDLOWAT: 2172 case SO_RCVLOWAT: 2173 error = sooptcopyin(sopt, &optval, sizeof optval, 2174 sizeof optval); 2175 if (error) 2176 goto bad; 2177 2178 /* 2179 * Values < 1 make no sense for any of these 2180 * options, so disallow them. 2181 */ 2182 if (optval < 1) { 2183 error = EINVAL; 2184 goto bad; 2185 } 2186 2187 switch (sopt->sopt_name) { 2188 case SO_SNDBUF: 2189 case SO_RCVBUF: 2190 if (ssb_reserve(sopt->sopt_name == SO_SNDBUF ? 2191 &so->so_snd : &so->so_rcv, (u_long)optval, 2192 so, 2193 &curproc->p_rlimit[RLIMIT_SBSIZE]) == 0) { 2194 error = ENOBUFS; 2195 goto bad; 2196 } 2197 sotmp = (sopt->sopt_name == SO_SNDBUF) ? 2198 &so->so_snd : &so->so_rcv; 2199 atomic_clear_int(&sotmp->ssb_flags, 2200 SSB_AUTOSIZE); 2201 break; 2202 2203 /* 2204 * Make sure the low-water is never greater than 2205 * the high-water. 2206 */ 2207 case SO_SNDLOWAT: 2208 so->so_snd.ssb_lowat = 2209 (optval > so->so_snd.ssb_hiwat) ? 2210 so->so_snd.ssb_hiwat : optval; 2211 atomic_clear_int(&so->so_snd.ssb_flags, 2212 SSB_AUTOLOWAT); 2213 break; 2214 case SO_RCVLOWAT: 2215 so->so_rcv.ssb_lowat = 2216 (optval > so->so_rcv.ssb_hiwat) ? 2217 so->so_rcv.ssb_hiwat : optval; 2218 atomic_clear_int(&so->so_rcv.ssb_flags, 2219 SSB_AUTOLOWAT); 2220 break; 2221 } 2222 break; 2223 2224 case SO_SNDTIMEO: 2225 case SO_RCVTIMEO: 2226 error = sooptcopyin(sopt, &tv, sizeof tv, 2227 sizeof tv); 2228 if (error) 2229 goto bad; 2230 2231 /* assert(hz > 0); */ 2232 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz || 2233 tv.tv_usec < 0 || tv.tv_usec >= 1000000) { 2234 error = EDOM; 2235 goto bad; 2236 } 2237 /* assert(tick > 0); */ 2238 /* assert(ULONG_MAX - INT_MAX >= 1000000); */ 2239 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / ustick; 2240 if (val > INT_MAX) { 2241 error = EDOM; 2242 goto bad; 2243 } 2244 if (val == 0 && tv.tv_usec != 0) 2245 val = 1; 2246 2247 switch (sopt->sopt_name) { 2248 case SO_SNDTIMEO: 2249 so->so_snd.ssb_timeo = val; 2250 break; 2251 case SO_RCVTIMEO: 2252 so->so_rcv.ssb_timeo = val; 2253 break; 2254 } 2255 break; 2256 default: 2257 error = ENOPROTOOPT; 2258 break; 2259 } 2260 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) { 2261 (void) so_pr_ctloutput(so, sopt); 2262 } 2263 } 2264 bad: 2265 return (error); 2266 } 2267 2268 /* Helper routine for getsockopt */ 2269 int 2270 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len) 2271 { 2272 soopt_from_kbuf(sopt, buf, len); 2273 return 0; 2274 } 2275 2276 void 2277 soopt_from_kbuf(struct sockopt *sopt, const void *buf, size_t len) 2278 { 2279 size_t valsize; 2280 2281 if (len == 0) { 2282 sopt->sopt_valsize = 0; 2283 return; 2284 } 2285 2286 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val)); 2287 KKASSERT(kva_p(buf)); 2288 2289 /* 2290 * Documented get behavior is that we always return a value, 2291 * possibly truncated to fit in the user's buffer. 2292 * Traditional behavior is that we always tell the user 2293 * precisely how much we copied, rather than something useful 2294 * like the total amount we had available for her. 2295 * Note that this interface is not idempotent; the entire answer must 2296 * generated ahead of time. 2297 */ 2298 valsize = szmin(len, sopt->sopt_valsize); 2299 sopt->sopt_valsize = valsize; 2300 if (sopt->sopt_val != 0) { 2301 bcopy(buf, sopt->sopt_val, valsize); 2302 } 2303 } 2304 2305 int 2306 sogetopt(struct socket *so, struct sockopt *sopt) 2307 { 2308 int error, optval; 2309 long optval_l; 2310 struct linger l; 2311 struct timeval tv; 2312 #ifdef INET 2313 struct accept_filter_arg *afap; 2314 #endif 2315 2316 error = 0; 2317 sopt->sopt_dir = SOPT_GET; 2318 if (sopt->sopt_level != SOL_SOCKET) { 2319 if (so->so_proto && so->so_proto->pr_ctloutput) { 2320 return (so_pr_ctloutput(so, sopt)); 2321 } else 2322 return (ENOPROTOOPT); 2323 } else { 2324 switch (sopt->sopt_name) { 2325 #ifdef INET 2326 case SO_ACCEPTFILTER: 2327 if ((so->so_options & SO_ACCEPTCONN) == 0) 2328 return (EINVAL); 2329 afap = kmalloc(sizeof(*afap), M_TEMP, 2330 M_WAITOK | M_ZERO); 2331 if ((so->so_options & SO_ACCEPTFILTER) != 0) { 2332 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name); 2333 if (so->so_accf->so_accept_filter_str != NULL) 2334 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str); 2335 } 2336 error = sooptcopyout(sopt, afap, sizeof(*afap)); 2337 kfree(afap, M_TEMP); 2338 break; 2339 #endif /* INET */ 2340 2341 case SO_LINGER: 2342 l.l_onoff = so->so_options & SO_LINGER; 2343 l.l_linger = so->so_linger; 2344 error = sooptcopyout(sopt, &l, sizeof l); 2345 break; 2346 2347 case SO_USELOOPBACK: 2348 case SO_DONTROUTE: 2349 case SO_DEBUG: 2350 case SO_KEEPALIVE: 2351 case SO_REUSEADDR: 2352 case SO_REUSEPORT: 2353 case SO_BROADCAST: 2354 case SO_OOBINLINE: 2355 case SO_TIMESTAMP: 2356 case SO_NOSIGPIPE: 2357 optval = so->so_options & sopt->sopt_name; 2358 integer: 2359 error = sooptcopyout(sopt, &optval, sizeof optval); 2360 break; 2361 2362 case SO_TYPE: 2363 optval = so->so_type; 2364 goto integer; 2365 2366 case SO_ERROR: 2367 optval = so->so_error; 2368 so->so_error = 0; 2369 goto integer; 2370 2371 case SO_SNDBUF: 2372 optval = so->so_snd.ssb_hiwat; 2373 goto integer; 2374 2375 case SO_RCVBUF: 2376 optval = so->so_rcv.ssb_hiwat; 2377 goto integer; 2378 2379 case SO_SNDLOWAT: 2380 optval = so->so_snd.ssb_lowat; 2381 goto integer; 2382 2383 case SO_RCVLOWAT: 2384 optval = so->so_rcv.ssb_lowat; 2385 goto integer; 2386 2387 case SO_SNDTIMEO: 2388 case SO_RCVTIMEO: 2389 optval = (sopt->sopt_name == SO_SNDTIMEO ? 2390 so->so_snd.ssb_timeo : so->so_rcv.ssb_timeo); 2391 2392 tv.tv_sec = optval / hz; 2393 tv.tv_usec = (optval % hz) * ustick; 2394 error = sooptcopyout(sopt, &tv, sizeof tv); 2395 break; 2396 2397 case SO_SNDSPACE: 2398 optval_l = ssb_space(&so->so_snd); 2399 error = sooptcopyout(sopt, &optval_l, sizeof(optval_l)); 2400 break; 2401 2402 case SO_CPUHINT: 2403 optval = -1; /* no hint */ 2404 goto integer; 2405 2406 default: 2407 error = ENOPROTOOPT; 2408 break; 2409 } 2410 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) 2411 so_pr_ctloutput(so, sopt); 2412 return (error); 2413 } 2414 } 2415 2416 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 2417 int 2418 soopt_getm(struct sockopt *sopt, struct mbuf **mp) 2419 { 2420 struct mbuf *m, *m_prev; 2421 int sopt_size = sopt->sopt_valsize, msize; 2422 2423 m = m_getl(sopt_size, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA, 2424 0, &msize); 2425 if (m == NULL) 2426 return (ENOBUFS); 2427 m->m_len = min(msize, sopt_size); 2428 sopt_size -= m->m_len; 2429 *mp = m; 2430 m_prev = m; 2431 2432 while (sopt_size > 0) { 2433 m = m_getl(sopt_size, sopt->sopt_td ? M_WAITOK : M_NOWAIT, 2434 MT_DATA, 0, &msize); 2435 if (m == NULL) { 2436 m_freem(*mp); 2437 return (ENOBUFS); 2438 } 2439 m->m_len = min(msize, sopt_size); 2440 sopt_size -= m->m_len; 2441 m_prev->m_next = m; 2442 m_prev = m; 2443 } 2444 return (0); 2445 } 2446 2447 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */ 2448 int 2449 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m) 2450 { 2451 soopt_to_mbuf(sopt, m); 2452 return 0; 2453 } 2454 2455 void 2456 soopt_to_mbuf(struct sockopt *sopt, struct mbuf *m) 2457 { 2458 size_t valsize; 2459 void *val; 2460 2461 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val)); 2462 KKASSERT(kva_p(m)); 2463 if (sopt->sopt_val == NULL) 2464 return; 2465 val = sopt->sopt_val; 2466 valsize = sopt->sopt_valsize; 2467 while (m != NULL && valsize >= m->m_len) { 2468 bcopy(val, mtod(m, char *), m->m_len); 2469 valsize -= m->m_len; 2470 val = (caddr_t)val + m->m_len; 2471 m = m->m_next; 2472 } 2473 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */ 2474 panic("ip6_sooptmcopyin"); 2475 } 2476 2477 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */ 2478 int 2479 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m) 2480 { 2481 return soopt_from_mbuf(sopt, m); 2482 } 2483 2484 int 2485 soopt_from_mbuf(struct sockopt *sopt, struct mbuf *m) 2486 { 2487 struct mbuf *m0 = m; 2488 size_t valsize = 0; 2489 size_t maxsize; 2490 void *val; 2491 2492 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val)); 2493 KKASSERT(kva_p(m)); 2494 if (sopt->sopt_val == NULL) 2495 return 0; 2496 val = sopt->sopt_val; 2497 maxsize = sopt->sopt_valsize; 2498 while (m != NULL && maxsize >= m->m_len) { 2499 bcopy(mtod(m, char *), val, m->m_len); 2500 maxsize -= m->m_len; 2501 val = (caddr_t)val + m->m_len; 2502 valsize += m->m_len; 2503 m = m->m_next; 2504 } 2505 if (m != NULL) { 2506 /* enough soopt buffer should be given from user-land */ 2507 m_freem(m0); 2508 return (EINVAL); 2509 } 2510 sopt->sopt_valsize = valsize; 2511 return 0; 2512 } 2513 2514 void 2515 sohasoutofband(struct socket *so) 2516 { 2517 if (so->so_sigio != NULL) 2518 pgsigio(so->so_sigio, SIGURG, 0); 2519 KNOTE(&so->so_rcv.ssb_kq.ki_note, NOTE_OOB); 2520 } 2521 2522 int 2523 sokqfilter(struct file *fp, struct knote *kn) 2524 { 2525 struct socket *so = (struct socket *)kn->kn_fp->f_data; 2526 struct signalsockbuf *ssb; 2527 2528 switch (kn->kn_filter) { 2529 case EVFILT_READ: 2530 if (so->so_options & SO_ACCEPTCONN) 2531 kn->kn_fop = &solisten_filtops; 2532 else 2533 kn->kn_fop = &soread_filtops; 2534 ssb = &so->so_rcv; 2535 break; 2536 case EVFILT_WRITE: 2537 kn->kn_fop = &sowrite_filtops; 2538 ssb = &so->so_snd; 2539 break; 2540 case EVFILT_EXCEPT: 2541 kn->kn_fop = &soexcept_filtops; 2542 ssb = &so->so_rcv; 2543 break; 2544 default: 2545 return (EOPNOTSUPP); 2546 } 2547 2548 knote_insert(&ssb->ssb_kq.ki_note, kn); 2549 atomic_set_int(&ssb->ssb_flags, SSB_KNOTE); 2550 return (0); 2551 } 2552 2553 static void 2554 filt_sordetach(struct knote *kn) 2555 { 2556 struct socket *so = (struct socket *)kn->kn_fp->f_data; 2557 2558 knote_remove(&so->so_rcv.ssb_kq.ki_note, kn); 2559 if (SLIST_EMPTY(&so->so_rcv.ssb_kq.ki_note)) 2560 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_KNOTE); 2561 } 2562 2563 /*ARGSUSED*/ 2564 static int 2565 filt_soread(struct knote *kn, long hint) 2566 { 2567 struct socket *so = (struct socket *)kn->kn_fp->f_data; 2568 2569 if (kn->kn_sfflags & NOTE_OOB) { 2570 if ((so->so_oobmark || (so->so_state & SS_RCVATMARK))) { 2571 kn->kn_fflags |= NOTE_OOB; 2572 return (1); 2573 } 2574 return (0); 2575 } 2576 kn->kn_data = so->so_rcv.ssb_cc; 2577 2578 if (so->so_state & SS_CANTRCVMORE) { 2579 /* 2580 * Only set NODATA if all data has been exhausted. 2581 */ 2582 if (kn->kn_data == 0) 2583 kn->kn_flags |= EV_NODATA; 2584 kn->kn_flags |= EV_EOF; 2585 kn->kn_fflags = so->so_error; 2586 return (1); 2587 } 2588 if (so->so_error) /* temporary udp error */ 2589 return (1); 2590 if (kn->kn_sfflags & NOTE_LOWAT) 2591 return (kn->kn_data >= kn->kn_sdata); 2592 return ((kn->kn_data >= so->so_rcv.ssb_lowat) || 2593 !TAILQ_EMPTY(&so->so_comp)); 2594 } 2595 2596 static void 2597 filt_sowdetach(struct knote *kn) 2598 { 2599 struct socket *so = (struct socket *)kn->kn_fp->f_data; 2600 2601 knote_remove(&so->so_snd.ssb_kq.ki_note, kn); 2602 if (SLIST_EMPTY(&so->so_snd.ssb_kq.ki_note)) 2603 atomic_clear_int(&so->so_snd.ssb_flags, SSB_KNOTE); 2604 } 2605 2606 /*ARGSUSED*/ 2607 static int 2608 filt_sowrite(struct knote *kn, long hint) 2609 { 2610 struct socket *so = (struct socket *)kn->kn_fp->f_data; 2611 2612 kn->kn_data = ssb_space(&so->so_snd); 2613 if (so->so_state & SS_CANTSENDMORE) { 2614 kn->kn_flags |= (EV_EOF | EV_NODATA); 2615 kn->kn_fflags = so->so_error; 2616 return (1); 2617 } 2618 if (so->so_error) /* temporary udp error */ 2619 return (1); 2620 if (((so->so_state & SS_ISCONNECTED) == 0) && 2621 (so->so_proto->pr_flags & PR_CONNREQUIRED)) 2622 return (0); 2623 if (kn->kn_sfflags & NOTE_LOWAT) 2624 return (kn->kn_data >= kn->kn_sdata); 2625 return (kn->kn_data >= so->so_snd.ssb_lowat); 2626 } 2627 2628 /*ARGSUSED*/ 2629 static int 2630 filt_solisten(struct knote *kn, long hint) 2631 { 2632 struct socket *so = (struct socket *)kn->kn_fp->f_data; 2633 2634 kn->kn_data = so->so_qlen; 2635 return (! TAILQ_EMPTY(&so->so_comp)); 2636 } 2637