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