1 /* $OpenBSD: uipc_socket.c,v 1.247 2020/06/22 13:14:32 mpi Exp $ */ 2 /* $NetBSD: uipc_socket.c,v 1.21 1996/02/04 02:17:52 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 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 University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/proc.h> 38 #include <sys/file.h> 39 #include <sys/filedesc.h> 40 #include <sys/malloc.h> 41 #include <sys/mbuf.h> 42 #include <sys/domain.h> 43 #include <sys/kernel.h> 44 #include <sys/event.h> 45 #include <sys/protosw.h> 46 #include <sys/socket.h> 47 #include <sys/unpcb.h> 48 #include <sys/socketvar.h> 49 #include <sys/signalvar.h> 50 #include <net/if.h> 51 #include <sys/pool.h> 52 #include <sys/atomic.h> 53 #include <sys/rwlock.h> 54 55 #ifdef DDB 56 #include <machine/db_machdep.h> 57 #endif 58 59 void sbsync(struct sockbuf *, struct mbuf *); 60 61 int sosplice(struct socket *, int, off_t, struct timeval *); 62 void sounsplice(struct socket *, struct socket *, int); 63 void soidle(void *); 64 void sotask(void *); 65 void soreaper(void *); 66 void soput(void *); 67 int somove(struct socket *, int); 68 69 void filt_sordetach(struct knote *kn); 70 int filt_soread(struct knote *kn, long hint); 71 void filt_sowdetach(struct knote *kn); 72 int filt_sowrite(struct knote *kn, long hint); 73 int filt_solisten(struct knote *kn, long hint); 74 75 const struct filterops solisten_filtops = { 76 .f_flags = FILTEROP_ISFD, 77 .f_attach = NULL, 78 .f_detach = filt_sordetach, 79 .f_event = filt_solisten, 80 }; 81 82 const struct filterops soread_filtops = { 83 .f_flags = FILTEROP_ISFD, 84 .f_attach = NULL, 85 .f_detach = filt_sordetach, 86 .f_event = filt_soread, 87 }; 88 89 const struct filterops sowrite_filtops = { 90 .f_flags = FILTEROP_ISFD, 91 .f_attach = NULL, 92 .f_detach = filt_sowdetach, 93 .f_event = filt_sowrite, 94 }; 95 96 const struct filterops soexcept_filtops = { 97 .f_flags = FILTEROP_ISFD, 98 .f_attach = NULL, 99 .f_detach = filt_sordetach, 100 .f_event = filt_soread, 101 }; 102 103 #ifndef SOMINCONN 104 #define SOMINCONN 80 105 #endif /* SOMINCONN */ 106 107 int somaxconn = SOMAXCONN; 108 int sominconn = SOMINCONN; 109 110 struct pool socket_pool; 111 #ifdef SOCKET_SPLICE 112 struct pool sosplice_pool; 113 struct taskq *sosplice_taskq; 114 struct rwlock sosplice_lock = RWLOCK_INITIALIZER("sosplicelk"); 115 #endif 116 117 void 118 soinit(void) 119 { 120 pool_init(&socket_pool, sizeof(struct socket), 0, IPL_SOFTNET, 0, 121 "sockpl", NULL); 122 #ifdef SOCKET_SPLICE 123 pool_init(&sosplice_pool, sizeof(struct sosplice), 0, IPL_SOFTNET, 0, 124 "sosppl", NULL); 125 #endif 126 } 127 128 /* 129 * Socket operation routines. 130 * These routines are called by the routines in 131 * sys_socket.c or from a system process, and 132 * implement the semantics of socket operations by 133 * switching out to the protocol specific routines. 134 */ 135 int 136 socreate(int dom, struct socket **aso, int type, int proto) 137 { 138 struct proc *p = curproc; /* XXX */ 139 const struct protosw *prp; 140 struct socket *so; 141 int error, s; 142 143 if (proto) 144 prp = pffindproto(dom, proto, type); 145 else 146 prp = pffindtype(dom, type); 147 if (prp == NULL || prp->pr_attach == NULL) 148 return (EPROTONOSUPPORT); 149 if (prp->pr_type != type) 150 return (EPROTOTYPE); 151 so = pool_get(&socket_pool, PR_WAITOK | PR_ZERO); 152 sigio_init(&so->so_sigio); 153 TAILQ_INIT(&so->so_q0); 154 TAILQ_INIT(&so->so_q); 155 so->so_type = type; 156 if (suser(p) == 0) 157 so->so_state = SS_PRIV; 158 so->so_ruid = p->p_ucred->cr_ruid; 159 so->so_euid = p->p_ucred->cr_uid; 160 so->so_rgid = p->p_ucred->cr_rgid; 161 so->so_egid = p->p_ucred->cr_gid; 162 so->so_cpid = p->p_p->ps_pid; 163 so->so_proto = prp; 164 so->so_snd.sb_timeo_nsecs = INFSLP; 165 so->so_rcv.sb_timeo_nsecs = INFSLP; 166 167 s = solock(so); 168 error = (*prp->pr_attach)(so, proto); 169 if (error) { 170 so->so_state |= SS_NOFDREF; 171 /* sofree() calls sounlock(). */ 172 sofree(so, s); 173 return (error); 174 } 175 sounlock(so, s); 176 *aso = so; 177 return (0); 178 } 179 180 int 181 sobind(struct socket *so, struct mbuf *nam, struct proc *p) 182 { 183 int error; 184 185 soassertlocked(so); 186 187 error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, p); 188 return (error); 189 } 190 191 int 192 solisten(struct socket *so, int backlog) 193 { 194 int s, error; 195 196 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING)) 197 return (EINVAL); 198 #ifdef SOCKET_SPLICE 199 if (isspliced(so) || issplicedback(so)) 200 return (EOPNOTSUPP); 201 #endif /* SOCKET_SPLICE */ 202 s = solock(so); 203 error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, NULL, NULL, 204 curproc); 205 if (error) { 206 sounlock(so, s); 207 return (error); 208 } 209 if (TAILQ_FIRST(&so->so_q) == NULL) 210 so->so_options |= SO_ACCEPTCONN; 211 if (backlog < 0 || backlog > somaxconn) 212 backlog = somaxconn; 213 if (backlog < sominconn) 214 backlog = sominconn; 215 so->so_qlimit = backlog; 216 sounlock(so, s); 217 return (0); 218 } 219 220 #define SOSP_FREEING_READ 1 221 #define SOSP_FREEING_WRITE 2 222 void 223 sofree(struct socket *so, int s) 224 { 225 soassertlocked(so); 226 227 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) { 228 sounlock(so, s); 229 return; 230 } 231 if (so->so_head) { 232 /* 233 * We must not decommission a socket that's on the accept(2) 234 * queue. If we do, then accept(2) may hang after select(2) 235 * indicated that the listening socket was ready. 236 */ 237 if (!soqremque(so, 0)) { 238 sounlock(so, s); 239 return; 240 } 241 } 242 sigio_free(&so->so_sigio); 243 #ifdef SOCKET_SPLICE 244 if (so->so_sp) { 245 if (issplicedback(so)) { 246 int freeing = SOSP_FREEING_WRITE; 247 248 if (so->so_sp->ssp_soback == so) 249 freeing |= SOSP_FREEING_READ; 250 sounsplice(so->so_sp->ssp_soback, so, freeing); 251 } 252 if (isspliced(so)) { 253 int freeing = SOSP_FREEING_READ; 254 255 if (so == so->so_sp->ssp_socket) 256 freeing |= SOSP_FREEING_WRITE; 257 sounsplice(so, so->so_sp->ssp_socket, freeing); 258 } 259 } 260 #endif /* SOCKET_SPLICE */ 261 sbrelease(so, &so->so_snd); 262 sorflush(so); 263 sounlock(so, s); 264 #ifdef SOCKET_SPLICE 265 if (so->so_sp) { 266 /* Reuse splice idle, sounsplice() has been called before. */ 267 timeout_set_proc(&so->so_sp->ssp_idleto, soreaper, so); 268 timeout_add(&so->so_sp->ssp_idleto, 0); 269 } else 270 #endif /* SOCKET_SPLICE */ 271 { 272 pool_put(&socket_pool, so); 273 } 274 } 275 276 static inline uint64_t 277 solinger_nsec(struct socket *so) 278 { 279 if (so->so_linger == 0) 280 return INFSLP; 281 282 return SEC_TO_NSEC(so->so_linger); 283 } 284 285 /* 286 * Close a socket on last file table reference removal. 287 * Initiate disconnect if connected. 288 * Free socket when disconnect complete. 289 */ 290 int 291 soclose(struct socket *so, int flags) 292 { 293 struct socket *so2; 294 int s, error = 0; 295 296 s = solock(so); 297 /* Revoke async IO early. There is a final revocation in sofree(). */ 298 sigio_free(&so->so_sigio); 299 if (so->so_options & SO_ACCEPTCONN) { 300 while ((so2 = TAILQ_FIRST(&so->so_q0)) != NULL) { 301 (void) soqremque(so2, 0); 302 (void) soabort(so2); 303 } 304 while ((so2 = TAILQ_FIRST(&so->so_q)) != NULL) { 305 (void) soqremque(so2, 1); 306 (void) soabort(so2); 307 } 308 } 309 if (so->so_pcb == NULL) 310 goto discard; 311 if (so->so_state & SS_ISCONNECTED) { 312 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 313 error = sodisconnect(so); 314 if (error) 315 goto drop; 316 } 317 if (so->so_options & SO_LINGER) { 318 if ((so->so_state & SS_ISDISCONNECTING) && 319 (flags & MSG_DONTWAIT)) 320 goto drop; 321 while (so->so_state & SS_ISCONNECTED) { 322 error = sosleep_nsec(so, &so->so_timeo, 323 PSOCK | PCATCH, "netcls", 324 solinger_nsec(so)); 325 if (error) 326 break; 327 } 328 } 329 } 330 drop: 331 if (so->so_pcb) { 332 int error2; 333 KASSERT(so->so_proto->pr_detach); 334 error2 = (*so->so_proto->pr_detach)(so); 335 if (error == 0) 336 error = error2; 337 } 338 discard: 339 if (so->so_state & SS_NOFDREF) 340 panic("soclose NOFDREF: so %p, so_type %d", so, so->so_type); 341 so->so_state |= SS_NOFDREF; 342 /* sofree() calls sounlock(). */ 343 sofree(so, s); 344 return (error); 345 } 346 347 int 348 soabort(struct socket *so) 349 { 350 soassertlocked(so); 351 352 return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, NULL, NULL, 353 curproc); 354 } 355 356 int 357 soaccept(struct socket *so, struct mbuf *nam) 358 { 359 int error = 0; 360 361 soassertlocked(so); 362 363 if ((so->so_state & SS_NOFDREF) == 0) 364 panic("soaccept !NOFDREF: so %p, so_type %d", so, so->so_type); 365 so->so_state &= ~SS_NOFDREF; 366 if ((so->so_state & SS_ISDISCONNECTED) == 0 || 367 (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) 368 error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, NULL, 369 nam, NULL, curproc); 370 else 371 error = ECONNABORTED; 372 return (error); 373 } 374 375 int 376 soconnect(struct socket *so, struct mbuf *nam) 377 { 378 int error; 379 380 soassertlocked(so); 381 382 if (so->so_options & SO_ACCEPTCONN) 383 return (EOPNOTSUPP); 384 /* 385 * If protocol is connection-based, can only connect once. 386 * Otherwise, if connected, try to disconnect first. 387 * This allows user to disconnect by connecting to, e.g., 388 * a null address. 389 */ 390 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 391 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 392 (error = sodisconnect(so)))) 393 error = EISCONN; 394 else 395 error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, 396 NULL, nam, NULL, curproc); 397 return (error); 398 } 399 400 int 401 soconnect2(struct socket *so1, struct socket *so2) 402 { 403 int s, error; 404 405 s = solock(so1); 406 error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, NULL, 407 (struct mbuf *)so2, NULL, curproc); 408 sounlock(so1, s); 409 return (error); 410 } 411 412 int 413 sodisconnect(struct socket *so) 414 { 415 int error; 416 417 soassertlocked(so); 418 419 if ((so->so_state & SS_ISCONNECTED) == 0) 420 return (ENOTCONN); 421 if (so->so_state & SS_ISDISCONNECTING) 422 return (EALREADY); 423 error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, NULL, NULL, 424 NULL, curproc); 425 return (error); 426 } 427 428 int m_getuio(struct mbuf **, int, long, struct uio *); 429 430 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 431 /* 432 * Send on a socket. 433 * If send must go all at once and message is larger than 434 * send buffering, then hard error. 435 * Lock against other senders. 436 * If must go all at once and not enough room now, then 437 * inform user that this would block and do nothing. 438 * Otherwise, if nonblocking, send as much as possible. 439 * The data to be sent is described by "uio" if nonzero, 440 * otherwise by the mbuf chain "top" (which must be null 441 * if uio is not). Data provided in mbuf chain must be small 442 * enough to send all at once. 443 * 444 * Returns nonzero on error, timeout or signal; callers 445 * must check for short counts if EINTR/ERESTART are returned. 446 * Data and control buffers are freed on return. 447 */ 448 int 449 sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top, 450 struct mbuf *control, int flags) 451 { 452 long space, clen = 0; 453 size_t resid; 454 int error, s; 455 int atomic = sosendallatonce(so) || top; 456 457 if (uio) 458 resid = uio->uio_resid; 459 else 460 resid = top->m_pkthdr.len; 461 /* MSG_EOR on a SOCK_STREAM socket is invalid. */ 462 if (so->so_type == SOCK_STREAM && (flags & MSG_EOR)) { 463 m_freem(top); 464 m_freem(control); 465 return (EINVAL); 466 } 467 if (uio && uio->uio_procp) 468 uio->uio_procp->p_ru.ru_msgsnd++; 469 if (control) { 470 /* 471 * In theory clen should be unsigned (since control->m_len is). 472 * However, space must be signed, as it might be less than 0 473 * if we over-committed, and we must use a signed comparison 474 * of space and clen. 475 */ 476 clen = control->m_len; 477 /* reserve extra space for AF_UNIX's internalize */ 478 if (so->so_proto->pr_domain->dom_family == AF_UNIX && 479 clen >= CMSG_ALIGN(sizeof(struct cmsghdr)) && 480 mtod(control, struct cmsghdr *)->cmsg_type == SCM_RIGHTS) 481 clen = CMSG_SPACE( 482 (clen - CMSG_ALIGN(sizeof(struct cmsghdr))) * 483 (sizeof(struct fdpass) / sizeof(int))); 484 } 485 486 #define snderr(errno) { error = errno; goto release; } 487 488 s = solock(so); 489 restart: 490 if ((error = sblock(so, &so->so_snd, SBLOCKWAIT(flags))) != 0) 491 goto out; 492 so->so_state |= SS_ISSENDING; 493 do { 494 if (so->so_state & SS_CANTSENDMORE) 495 snderr(EPIPE); 496 if (so->so_error) { 497 error = so->so_error; 498 so->so_error = 0; 499 snderr(error); 500 } 501 if ((so->so_state & SS_ISCONNECTED) == 0) { 502 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 503 if (!(resid == 0 && clen != 0)) 504 snderr(ENOTCONN); 505 } else if (addr == 0) 506 snderr(EDESTADDRREQ); 507 } 508 space = sbspace(so, &so->so_snd); 509 if (flags & MSG_OOB) 510 space += 1024; 511 if (so->so_proto->pr_domain->dom_family == AF_UNIX) { 512 if (atomic && resid > so->so_snd.sb_hiwat) 513 snderr(EMSGSIZE); 514 } else { 515 if (clen > so->so_snd.sb_hiwat || 516 (atomic && resid > so->so_snd.sb_hiwat - clen)) 517 snderr(EMSGSIZE); 518 } 519 if (space < clen || 520 (space - clen < resid && 521 (atomic || space < so->so_snd.sb_lowat))) { 522 if (flags & MSG_DONTWAIT) 523 snderr(EWOULDBLOCK); 524 sbunlock(so, &so->so_snd); 525 error = sbwait(so, &so->so_snd); 526 so->so_state &= ~SS_ISSENDING; 527 if (error) 528 goto out; 529 goto restart; 530 } 531 space -= clen; 532 do { 533 if (uio == NULL) { 534 /* 535 * Data is prepackaged in "top". 536 */ 537 resid = 0; 538 if (flags & MSG_EOR) 539 top->m_flags |= M_EOR; 540 } else { 541 sounlock(so, s); 542 error = m_getuio(&top, atomic, space, uio); 543 s = solock(so); 544 if (error) 545 goto release; 546 space -= top->m_pkthdr.len; 547 resid = uio->uio_resid; 548 if (flags & MSG_EOR) 549 top->m_flags |= M_EOR; 550 } 551 if (resid == 0) 552 so->so_state &= ~SS_ISSENDING; 553 if (top && so->so_options & SO_ZEROIZE) 554 top->m_flags |= M_ZEROIZE; 555 error = (*so->so_proto->pr_usrreq)(so, 556 (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, 557 top, addr, control, curproc); 558 clen = 0; 559 control = NULL; 560 top = NULL; 561 if (error) 562 goto release; 563 } while (resid && space > 0); 564 } while (resid); 565 566 release: 567 so->so_state &= ~SS_ISSENDING; 568 sbunlock(so, &so->so_snd); 569 out: 570 sounlock(so, s); 571 m_freem(top); 572 m_freem(control); 573 return (error); 574 } 575 576 int 577 m_getuio(struct mbuf **mp, int atomic, long space, struct uio *uio) 578 { 579 struct mbuf *m, *top = NULL; 580 struct mbuf **nextp = ⊤ 581 u_long len, mlen; 582 size_t resid = uio->uio_resid; 583 int error; 584 585 do { 586 if (top == NULL) { 587 MGETHDR(m, M_WAIT, MT_DATA); 588 mlen = MHLEN; 589 m->m_pkthdr.len = 0; 590 m->m_pkthdr.ph_ifidx = 0; 591 } else { 592 MGET(m, M_WAIT, MT_DATA); 593 mlen = MLEN; 594 } 595 /* chain mbuf together */ 596 *nextp = m; 597 nextp = &m->m_next; 598 599 resid = ulmin(resid, space); 600 if (resid >= MINCLSIZE) { 601 MCLGETI(m, M_NOWAIT, NULL, ulmin(resid, MAXMCLBYTES)); 602 if ((m->m_flags & M_EXT) == 0) 603 MCLGETI(m, M_NOWAIT, NULL, MCLBYTES); 604 if ((m->m_flags & M_EXT) == 0) 605 goto nopages; 606 mlen = m->m_ext.ext_size; 607 len = ulmin(mlen, resid); 608 /* 609 * For datagram protocols, leave room 610 * for protocol headers in first mbuf. 611 */ 612 if (atomic && m == top && len < mlen - max_hdr) 613 m->m_data += max_hdr; 614 } else { 615 nopages: 616 len = ulmin(mlen, resid); 617 /* 618 * For datagram protocols, leave room 619 * for protocol headers in first mbuf. 620 */ 621 if (atomic && m == top && len < mlen - max_hdr) 622 m_align(m, len); 623 } 624 625 error = uiomove(mtod(m, caddr_t), len, uio); 626 if (error) { 627 m_freem(top); 628 return (error); 629 } 630 631 /* adjust counters */ 632 resid = uio->uio_resid; 633 space -= len; 634 m->m_len = len; 635 top->m_pkthdr.len += len; 636 637 /* Is there more space and more data? */ 638 } while (space > 0 && resid > 0); 639 640 *mp = top; 641 return 0; 642 } 643 644 /* 645 * Following replacement or removal of the first mbuf on the first 646 * mbuf chain of a socket buffer, push necessary state changes back 647 * into the socket buffer so that other consumers see the values 648 * consistently. 'nextrecord' is the callers locally stored value of 649 * the original value of sb->sb_mb->m_nextpkt which must be restored 650 * when the lead mbuf changes. NOTE: 'nextrecord' may be NULL. 651 */ 652 void 653 sbsync(struct sockbuf *sb, struct mbuf *nextrecord) 654 { 655 656 /* 657 * First, update for the new value of nextrecord. If necessary, 658 * make it the first record. 659 */ 660 if (sb->sb_mb != NULL) 661 sb->sb_mb->m_nextpkt = nextrecord; 662 else 663 sb->sb_mb = nextrecord; 664 665 /* 666 * Now update any dependent socket buffer fields to reflect 667 * the new state. This is an inline of SB_EMPTY_FIXUP, with 668 * the addition of a second clause that takes care of the 669 * case where sb_mb has been updated, but remains the last 670 * record. 671 */ 672 if (sb->sb_mb == NULL) { 673 sb->sb_mbtail = NULL; 674 sb->sb_lastrecord = NULL; 675 } else if (sb->sb_mb->m_nextpkt == NULL) 676 sb->sb_lastrecord = sb->sb_mb; 677 } 678 679 /* 680 * Implement receive operations on a socket. 681 * We depend on the way that records are added to the sockbuf 682 * by sbappend*. In particular, each record (mbufs linked through m_next) 683 * must begin with an address if the protocol so specifies, 684 * followed by an optional mbuf or mbufs containing ancillary data, 685 * and then zero or more mbufs of data. 686 * In order to avoid blocking network for the entire time here, we release 687 * the solock() while doing the actual copy to user space. 688 * Although the sockbuf is locked, new data may still be appended, 689 * and thus we must maintain consistency of the sockbuf during that time. 690 * 691 * The caller may receive the data as a single mbuf chain by supplying 692 * an mbuf **mp0 for use in returning the chain. The uio is then used 693 * only for the count in uio_resid. 694 */ 695 int 696 soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio, 697 struct mbuf **mp0, struct mbuf **controlp, int *flagsp, 698 socklen_t controllen) 699 { 700 struct mbuf *m, **mp; 701 struct mbuf *cm; 702 u_long len, offset, moff; 703 int flags, error, s, type, uio_error = 0; 704 const struct protosw *pr = so->so_proto; 705 struct mbuf *nextrecord; 706 size_t resid, orig_resid = uio->uio_resid; 707 708 mp = mp0; 709 if (paddr) 710 *paddr = NULL; 711 if (controlp) 712 *controlp = NULL; 713 if (flagsp) 714 flags = *flagsp &~ MSG_EOR; 715 else 716 flags = 0; 717 if (flags & MSG_OOB) { 718 m = m_get(M_WAIT, MT_DATA); 719 s = solock(so); 720 error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m, 721 (struct mbuf *)(long)(flags & MSG_PEEK), NULL, curproc); 722 sounlock(so, s); 723 if (error) 724 goto bad; 725 do { 726 error = uiomove(mtod(m, caddr_t), 727 ulmin(uio->uio_resid, m->m_len), uio); 728 m = m_free(m); 729 } while (uio->uio_resid && error == 0 && m); 730 bad: 731 m_freem(m); 732 return (error); 733 } 734 if (mp) 735 *mp = NULL; 736 737 s = solock(so); 738 restart: 739 if ((error = sblock(so, &so->so_rcv, SBLOCKWAIT(flags))) != 0) { 740 sounlock(so, s); 741 return (error); 742 } 743 744 m = so->so_rcv.sb_mb; 745 #ifdef SOCKET_SPLICE 746 if (isspliced(so)) 747 m = NULL; 748 #endif /* SOCKET_SPLICE */ 749 /* 750 * If we have less data than requested, block awaiting more 751 * (subject to any timeout) if: 752 * 1. the current count is less than the low water mark, 753 * 2. MSG_WAITALL is set, and it is possible to do the entire 754 * receive operation at once if we block (resid <= hiwat), or 755 * 3. MSG_DONTWAIT is not set. 756 * If MSG_WAITALL is set but resid is larger than the receive buffer, 757 * we have to do the receive in sections, and thus risk returning 758 * a short count if a timeout or signal occurs after we start. 759 */ 760 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && 761 so->so_rcv.sb_cc < uio->uio_resid) && 762 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 763 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && 764 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) { 765 #ifdef DIAGNOSTIC 766 if (m == NULL && so->so_rcv.sb_cc) 767 #ifdef SOCKET_SPLICE 768 if (!isspliced(so)) 769 #endif /* SOCKET_SPLICE */ 770 panic("receive 1: so %p, so_type %d, sb_cc %lu", 771 so, so->so_type, so->so_rcv.sb_cc); 772 #endif 773 if (so->so_error) { 774 if (m) 775 goto dontblock; 776 error = so->so_error; 777 if ((flags & MSG_PEEK) == 0) 778 so->so_error = 0; 779 goto release; 780 } 781 if (so->so_state & SS_CANTRCVMORE) { 782 if (m) 783 goto dontblock; 784 else if (so->so_rcv.sb_cc == 0) 785 goto release; 786 } 787 for (; m; m = m->m_next) 788 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 789 m = so->so_rcv.sb_mb; 790 goto dontblock; 791 } 792 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 793 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 794 error = ENOTCONN; 795 goto release; 796 } 797 if (uio->uio_resid == 0 && controlp == NULL) 798 goto release; 799 if (flags & MSG_DONTWAIT) { 800 error = EWOULDBLOCK; 801 goto release; 802 } 803 SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1"); 804 SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1"); 805 sbunlock(so, &so->so_rcv); 806 error = sbwait(so, &so->so_rcv); 807 if (error) { 808 sounlock(so, s); 809 return (error); 810 } 811 goto restart; 812 } 813 dontblock: 814 /* 815 * On entry here, m points to the first record of the socket buffer. 816 * From this point onward, we maintain 'nextrecord' as a cache of the 817 * pointer to the next record in the socket buffer. We must keep the 818 * various socket buffer pointers and local stack versions of the 819 * pointers in sync, pushing out modifications before operations that 820 * may sleep, and re-reading them afterwards. 821 * 822 * Otherwise, we will race with the network stack appending new data 823 * or records onto the socket buffer by using inconsistent/stale 824 * versions of the field, possibly resulting in socket buffer 825 * corruption. 826 */ 827 if (uio->uio_procp) 828 uio->uio_procp->p_ru.ru_msgrcv++; 829 KASSERT(m == so->so_rcv.sb_mb); 830 SBLASTRECORDCHK(&so->so_rcv, "soreceive 1"); 831 SBLASTMBUFCHK(&so->so_rcv, "soreceive 1"); 832 nextrecord = m->m_nextpkt; 833 if (pr->pr_flags & PR_ADDR) { 834 #ifdef DIAGNOSTIC 835 if (m->m_type != MT_SONAME) 836 panic("receive 1a: so %p, so_type %d, m %p, m_type %d", 837 so, so->so_type, m, m->m_type); 838 #endif 839 orig_resid = 0; 840 if (flags & MSG_PEEK) { 841 if (paddr) 842 *paddr = m_copym(m, 0, m->m_len, M_NOWAIT); 843 m = m->m_next; 844 } else { 845 sbfree(&so->so_rcv, m); 846 if (paddr) { 847 *paddr = m; 848 so->so_rcv.sb_mb = m->m_next; 849 m->m_next = 0; 850 m = so->so_rcv.sb_mb; 851 } else { 852 so->so_rcv.sb_mb = m_free(m); 853 m = so->so_rcv.sb_mb; 854 } 855 sbsync(&so->so_rcv, nextrecord); 856 } 857 } 858 while (m && m->m_type == MT_CONTROL && error == 0) { 859 int skip = 0; 860 if (flags & MSG_PEEK) { 861 if (mtod(m, struct cmsghdr *)->cmsg_type == 862 SCM_RIGHTS) { 863 /* don't leak internalized SCM_RIGHTS msgs */ 864 skip = 1; 865 } else if (controlp) 866 *controlp = m_copym(m, 0, m->m_len, M_NOWAIT); 867 m = m->m_next; 868 } else { 869 sbfree(&so->so_rcv, m); 870 so->so_rcv.sb_mb = m->m_next; 871 m->m_nextpkt = m->m_next = NULL; 872 cm = m; 873 m = so->so_rcv.sb_mb; 874 sbsync(&so->so_rcv, nextrecord); 875 if (controlp) { 876 if (pr->pr_domain->dom_externalize) { 877 error = 878 (*pr->pr_domain->dom_externalize) 879 (cm, controllen, flags); 880 } 881 *controlp = cm; 882 } else { 883 /* 884 * Dispose of any SCM_RIGHTS message that went 885 * through the read path rather than recv. 886 */ 887 if (pr->pr_domain->dom_dispose) 888 pr->pr_domain->dom_dispose(cm); 889 m_free(cm); 890 } 891 } 892 if (m != NULL) 893 nextrecord = so->so_rcv.sb_mb->m_nextpkt; 894 else 895 nextrecord = so->so_rcv.sb_mb; 896 if (controlp && !skip) { 897 orig_resid = 0; 898 controlp = &(*controlp)->m_next; 899 } 900 } 901 902 /* If m is non-NULL, we have some data to read. */ 903 if (m) { 904 type = m->m_type; 905 if (type == MT_OOBDATA) 906 flags |= MSG_OOB; 907 if (m->m_flags & M_BCAST) 908 flags |= MSG_BCAST; 909 if (m->m_flags & M_MCAST) 910 flags |= MSG_MCAST; 911 } 912 SBLASTRECORDCHK(&so->so_rcv, "soreceive 2"); 913 SBLASTMBUFCHK(&so->so_rcv, "soreceive 2"); 914 915 moff = 0; 916 offset = 0; 917 while (m && uio->uio_resid > 0 && error == 0) { 918 if (m->m_type == MT_OOBDATA) { 919 if (type != MT_OOBDATA) 920 break; 921 } else if (type == MT_OOBDATA) { 922 break; 923 } else if (m->m_type == MT_CONTROL) { 924 /* 925 * If there is more than one control message in the 926 * stream, we do a short read. Next can be received 927 * or disposed by another system call. 928 */ 929 break; 930 #ifdef DIAGNOSTIC 931 } else if (m->m_type != MT_DATA && m->m_type != MT_HEADER) { 932 panic("receive 3: so %p, so_type %d, m %p, m_type %d", 933 so, so->so_type, m, m->m_type); 934 #endif 935 } 936 so->so_state &= ~SS_RCVATMARK; 937 len = uio->uio_resid; 938 if (so->so_oobmark && len > so->so_oobmark - offset) 939 len = so->so_oobmark - offset; 940 if (len > m->m_len - moff) 941 len = m->m_len - moff; 942 /* 943 * If mp is set, just pass back the mbufs. 944 * Otherwise copy them out via the uio, then free. 945 * Sockbuf must be consistent here (points to current mbuf, 946 * it points to next record) when we drop priority; 947 * we must note any additions to the sockbuf when we 948 * block interrupts again. 949 */ 950 if (mp == NULL && uio_error == 0) { 951 SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove"); 952 SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove"); 953 resid = uio->uio_resid; 954 sounlock(so, s); 955 uio_error = uiomove(mtod(m, caddr_t) + moff, len, uio); 956 s = solock(so); 957 if (uio_error) 958 uio->uio_resid = resid - len; 959 } else 960 uio->uio_resid -= len; 961 if (len == m->m_len - moff) { 962 if (m->m_flags & M_EOR) 963 flags |= MSG_EOR; 964 if (flags & MSG_PEEK) { 965 m = m->m_next; 966 moff = 0; 967 } else { 968 nextrecord = m->m_nextpkt; 969 sbfree(&so->so_rcv, m); 970 if (mp) { 971 *mp = m; 972 mp = &m->m_next; 973 so->so_rcv.sb_mb = m = m->m_next; 974 *mp = NULL; 975 } else { 976 so->so_rcv.sb_mb = m_free(m); 977 m = so->so_rcv.sb_mb; 978 } 979 /* 980 * If m != NULL, we also know that 981 * so->so_rcv.sb_mb != NULL. 982 */ 983 KASSERT(so->so_rcv.sb_mb == m); 984 if (m) { 985 m->m_nextpkt = nextrecord; 986 if (nextrecord == NULL) 987 so->so_rcv.sb_lastrecord = m; 988 } else { 989 so->so_rcv.sb_mb = nextrecord; 990 SB_EMPTY_FIXUP(&so->so_rcv); 991 } 992 SBLASTRECORDCHK(&so->so_rcv, "soreceive 3"); 993 SBLASTMBUFCHK(&so->so_rcv, "soreceive 3"); 994 } 995 } else { 996 if (flags & MSG_PEEK) 997 moff += len; 998 else { 999 if (mp) 1000 *mp = m_copym(m, 0, len, M_WAIT); 1001 m->m_data += len; 1002 m->m_len -= len; 1003 so->so_rcv.sb_cc -= len; 1004 so->so_rcv.sb_datacc -= len; 1005 } 1006 } 1007 if (so->so_oobmark) { 1008 if ((flags & MSG_PEEK) == 0) { 1009 so->so_oobmark -= len; 1010 if (so->so_oobmark == 0) { 1011 so->so_state |= SS_RCVATMARK; 1012 break; 1013 } 1014 } else { 1015 offset += len; 1016 if (offset == so->so_oobmark) 1017 break; 1018 } 1019 } 1020 if (flags & MSG_EOR) 1021 break; 1022 /* 1023 * If the MSG_WAITALL flag is set (for non-atomic socket), 1024 * we must not quit until "uio->uio_resid == 0" or an error 1025 * termination. If a signal/timeout occurs, return 1026 * with a short count but without error. 1027 * Keep sockbuf locked against other readers. 1028 */ 1029 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 && 1030 !sosendallatonce(so) && !nextrecord) { 1031 if (so->so_error || so->so_state & SS_CANTRCVMORE) 1032 break; 1033 SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); 1034 SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); 1035 error = sbwait(so, &so->so_rcv); 1036 if (error) { 1037 sbunlock(so, &so->so_rcv); 1038 sounlock(so, s); 1039 return (0); 1040 } 1041 if ((m = so->so_rcv.sb_mb) != NULL) 1042 nextrecord = m->m_nextpkt; 1043 } 1044 } 1045 1046 if (m && pr->pr_flags & PR_ATOMIC) { 1047 flags |= MSG_TRUNC; 1048 if ((flags & MSG_PEEK) == 0) 1049 (void) sbdroprecord(&so->so_rcv); 1050 } 1051 if ((flags & MSG_PEEK) == 0) { 1052 if (m == NULL) { 1053 /* 1054 * First part is an inline SB_EMPTY_FIXUP(). Second 1055 * part makes sure sb_lastrecord is up-to-date if 1056 * there is still data in the socket buffer. 1057 */ 1058 so->so_rcv.sb_mb = nextrecord; 1059 if (so->so_rcv.sb_mb == NULL) { 1060 so->so_rcv.sb_mbtail = NULL; 1061 so->so_rcv.sb_lastrecord = NULL; 1062 } else if (nextrecord->m_nextpkt == NULL) 1063 so->so_rcv.sb_lastrecord = nextrecord; 1064 } 1065 SBLASTRECORDCHK(&so->so_rcv, "soreceive 4"); 1066 SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); 1067 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 1068 (*pr->pr_usrreq)(so, PRU_RCVD, NULL, 1069 (struct mbuf *)(long)flags, NULL, curproc); 1070 } 1071 if (orig_resid == uio->uio_resid && orig_resid && 1072 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { 1073 sbunlock(so, &so->so_rcv); 1074 goto restart; 1075 } 1076 1077 if (uio_error) 1078 error = uio_error; 1079 1080 if (flagsp) 1081 *flagsp |= flags; 1082 release: 1083 sbunlock(so, &so->so_rcv); 1084 sounlock(so, s); 1085 return (error); 1086 } 1087 1088 int 1089 soshutdown(struct socket *so, int how) 1090 { 1091 const struct protosw *pr = so->so_proto; 1092 int s, error = 0; 1093 1094 s = solock(so); 1095 switch (how) { 1096 case SHUT_RD: 1097 sorflush(so); 1098 break; 1099 case SHUT_RDWR: 1100 sorflush(so); 1101 /* FALLTHROUGH */ 1102 case SHUT_WR: 1103 error = (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL, NULL, NULL, 1104 curproc); 1105 break; 1106 default: 1107 error = EINVAL; 1108 break; 1109 } 1110 sounlock(so, s); 1111 1112 return (error); 1113 } 1114 1115 void 1116 sorflush(struct socket *so) 1117 { 1118 struct sockbuf *sb = &so->so_rcv; 1119 const struct protosw *pr = so->so_proto; 1120 struct socket aso; 1121 int error; 1122 1123 sb->sb_flags |= SB_NOINTR; 1124 error = sblock(so, sb, M_WAITOK); 1125 /* with SB_NOINTR and M_WAITOK sblock() must not fail */ 1126 KASSERT(error == 0); 1127 socantrcvmore(so); 1128 sbunlock(so, sb); 1129 aso.so_proto = pr; 1130 aso.so_rcv = *sb; 1131 memset(&sb->sb_startzero, 0, 1132 (caddr_t)&sb->sb_endzero - (caddr_t)&sb->sb_startzero); 1133 sb->sb_timeo_nsecs = INFSLP; 1134 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) 1135 (*pr->pr_domain->dom_dispose)(aso.so_rcv.sb_mb); 1136 sbrelease(&aso, &aso.so_rcv); 1137 } 1138 1139 #ifdef SOCKET_SPLICE 1140 1141 #define so_splicelen so_sp->ssp_len 1142 #define so_splicemax so_sp->ssp_max 1143 #define so_idletv so_sp->ssp_idletv 1144 #define so_idleto so_sp->ssp_idleto 1145 #define so_splicetask so_sp->ssp_task 1146 1147 int 1148 sosplice(struct socket *so, int fd, off_t max, struct timeval *tv) 1149 { 1150 struct file *fp; 1151 struct socket *sosp; 1152 struct sosplice *sp; 1153 struct taskq *tq; 1154 int error = 0; 1155 1156 soassertlocked(so); 1157 1158 if (sosplice_taskq == NULL) { 1159 rw_enter_write(&sosplice_lock); 1160 if (sosplice_taskq == NULL) { 1161 tq = taskq_create("sosplice", 1, IPL_SOFTNET, 1162 TASKQ_MPSAFE); 1163 /* Ensure the taskq is fully visible to other CPUs. */ 1164 membar_producer(); 1165 sosplice_taskq = tq; 1166 } 1167 rw_exit_write(&sosplice_lock); 1168 } 1169 if (sosplice_taskq == NULL) 1170 return (ENOMEM); 1171 1172 if ((so->so_proto->pr_flags & PR_SPLICE) == 0) 1173 return (EPROTONOSUPPORT); 1174 if (so->so_options & SO_ACCEPTCONN) 1175 return (EOPNOTSUPP); 1176 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 1177 (so->so_proto->pr_flags & PR_CONNREQUIRED)) 1178 return (ENOTCONN); 1179 if (so->so_sp == NULL) { 1180 sp = pool_get(&sosplice_pool, PR_WAITOK | PR_ZERO); 1181 if (so->so_sp == NULL) 1182 so->so_sp = sp; 1183 else 1184 pool_put(&sosplice_pool, sp); 1185 } 1186 1187 /* If no fd is given, unsplice by removing existing link. */ 1188 if (fd < 0) { 1189 /* Lock receive buffer. */ 1190 if ((error = sblock(so, &so->so_rcv, M_WAITOK)) != 0) { 1191 return (error); 1192 } 1193 if (so->so_sp->ssp_socket) 1194 sounsplice(so, so->so_sp->ssp_socket, 0); 1195 sbunlock(so, &so->so_rcv); 1196 return (0); 1197 } 1198 1199 if (max && max < 0) 1200 return (EINVAL); 1201 1202 if (tv && (tv->tv_sec < 0 || tv->tv_usec < 0)) 1203 return (EINVAL); 1204 1205 /* Find sosp, the drain socket where data will be spliced into. */ 1206 if ((error = getsock(curproc, fd, &fp)) != 0) 1207 return (error); 1208 sosp = fp->f_data; 1209 if (sosp->so_proto->pr_usrreq != so->so_proto->pr_usrreq) { 1210 error = EPROTONOSUPPORT; 1211 goto frele; 1212 } 1213 if (sosp->so_sp == NULL) { 1214 sp = pool_get(&sosplice_pool, PR_WAITOK | PR_ZERO); 1215 if (sosp->so_sp == NULL) 1216 sosp->so_sp = sp; 1217 else 1218 pool_put(&sosplice_pool, sp); 1219 } 1220 1221 /* Lock both receive and send buffer. */ 1222 if ((error = sblock(so, &so->so_rcv, M_WAITOK)) != 0) { 1223 goto frele; 1224 } 1225 if ((error = sblock(so, &sosp->so_snd, M_WAITOK)) != 0) { 1226 sbunlock(so, &so->so_rcv); 1227 goto frele; 1228 } 1229 1230 if (so->so_sp->ssp_socket || sosp->so_sp->ssp_soback) { 1231 error = EBUSY; 1232 goto release; 1233 } 1234 if (sosp->so_options & SO_ACCEPTCONN) { 1235 error = EOPNOTSUPP; 1236 goto release; 1237 } 1238 if ((sosp->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0) { 1239 error = ENOTCONN; 1240 goto release; 1241 } 1242 1243 /* Splice so and sosp together. */ 1244 so->so_sp->ssp_socket = sosp; 1245 sosp->so_sp->ssp_soback = so; 1246 so->so_splicelen = 0; 1247 so->so_splicemax = max; 1248 if (tv) 1249 so->so_idletv = *tv; 1250 else 1251 timerclear(&so->so_idletv); 1252 timeout_set_proc(&so->so_idleto, soidle, so); 1253 task_set(&so->so_splicetask, sotask, so); 1254 1255 /* 1256 * To prevent softnet interrupt from calling somove() while 1257 * we sleep, the socket buffers are not marked as spliced yet. 1258 */ 1259 if (somove(so, M_WAIT)) { 1260 so->so_rcv.sb_flags |= SB_SPLICE; 1261 sosp->so_snd.sb_flags |= SB_SPLICE; 1262 } 1263 1264 release: 1265 sbunlock(sosp, &sosp->so_snd); 1266 sbunlock(so, &so->so_rcv); 1267 frele: 1268 /* 1269 * FRELE() must not be called with the socket lock held. It is safe to 1270 * release the lock here as long as no other operation happen on the 1271 * socket when sosplice() returns. The dance could be avoided by 1272 * grabbing the socket lock inside this function. 1273 */ 1274 sounlock(so, SL_LOCKED); 1275 FRELE(fp, curproc); 1276 solock(so); 1277 return (error); 1278 } 1279 1280 void 1281 sounsplice(struct socket *so, struct socket *sosp, int freeing) 1282 { 1283 soassertlocked(so); 1284 1285 task_del(sosplice_taskq, &so->so_splicetask); 1286 timeout_del(&so->so_idleto); 1287 sosp->so_snd.sb_flags &= ~SB_SPLICE; 1288 so->so_rcv.sb_flags &= ~SB_SPLICE; 1289 so->so_sp->ssp_socket = sosp->so_sp->ssp_soback = NULL; 1290 /* Do not wakeup a socket that is about to be freed. */ 1291 if ((freeing & SOSP_FREEING_READ) == 0 && soreadable(so)) 1292 sorwakeup(so); 1293 if ((freeing & SOSP_FREEING_WRITE) == 0 && sowriteable(sosp)) 1294 sowwakeup(sosp); 1295 } 1296 1297 void 1298 soidle(void *arg) 1299 { 1300 struct socket *so = arg; 1301 int s; 1302 1303 s = solock(so); 1304 if (so->so_rcv.sb_flags & SB_SPLICE) { 1305 so->so_error = ETIMEDOUT; 1306 sounsplice(so, so->so_sp->ssp_socket, 0); 1307 } 1308 sounlock(so, s); 1309 } 1310 1311 void 1312 sotask(void *arg) 1313 { 1314 struct socket *so = arg; 1315 int s; 1316 1317 s = solock(so); 1318 if (so->so_rcv.sb_flags & SB_SPLICE) { 1319 /* 1320 * We may not sleep here as sofree() and unsplice() may be 1321 * called from softnet interrupt context. This would remove 1322 * the socket during somove(). 1323 */ 1324 somove(so, M_DONTWAIT); 1325 } 1326 sounlock(so, s); 1327 1328 /* Avoid user land starvation. */ 1329 yield(); 1330 } 1331 1332 /* 1333 * The socket splicing task or idle timeout may sleep while grabbing the net 1334 * lock. As sofree() can be called anytime, sotask() or soidle() could access 1335 * the socket memory of a freed socket after wakeup. So delay the pool_put() 1336 * after all pending socket splicing tasks or timeouts have finished. Do this 1337 * by scheduling it on the same threads. 1338 */ 1339 void 1340 soreaper(void *arg) 1341 { 1342 struct socket *so = arg; 1343 1344 /* Reuse splice task, sounsplice() has been called before. */ 1345 task_set(&so->so_sp->ssp_task, soput, so); 1346 task_add(sosplice_taskq, &so->so_sp->ssp_task); 1347 } 1348 1349 void 1350 soput(void *arg) 1351 { 1352 struct socket *so = arg; 1353 1354 pool_put(&sosplice_pool, so->so_sp); 1355 pool_put(&socket_pool, so); 1356 } 1357 1358 /* 1359 * Move data from receive buffer of spliced source socket to send 1360 * buffer of drain socket. Try to move as much as possible in one 1361 * big chunk. It is a TCP only implementation. 1362 * Return value 0 means splicing has been finished, 1 continue. 1363 */ 1364 int 1365 somove(struct socket *so, int wait) 1366 { 1367 struct socket *sosp = so->so_sp->ssp_socket; 1368 struct mbuf *m, **mp, *nextrecord; 1369 u_long len, off, oobmark; 1370 long space; 1371 int error = 0, maxreached = 0; 1372 unsigned int state; 1373 1374 soassertlocked(so); 1375 1376 nextpkt: 1377 if (so->so_error) { 1378 error = so->so_error; 1379 goto release; 1380 } 1381 if (sosp->so_state & SS_CANTSENDMORE) { 1382 error = EPIPE; 1383 goto release; 1384 } 1385 if (sosp->so_error && sosp->so_error != ETIMEDOUT && 1386 sosp->so_error != EFBIG && sosp->so_error != ELOOP) { 1387 error = sosp->so_error; 1388 goto release; 1389 } 1390 if ((sosp->so_state & SS_ISCONNECTED) == 0) 1391 goto release; 1392 1393 /* Calculate how many bytes can be copied now. */ 1394 len = so->so_rcv.sb_datacc; 1395 if (so->so_splicemax) { 1396 KASSERT(so->so_splicelen < so->so_splicemax); 1397 if (so->so_splicemax <= so->so_splicelen + len) { 1398 len = so->so_splicemax - so->so_splicelen; 1399 maxreached = 1; 1400 } 1401 } 1402 space = sbspace(sosp, &sosp->so_snd); 1403 if (so->so_oobmark && so->so_oobmark < len && 1404 so->so_oobmark < space + 1024) 1405 space += 1024; 1406 if (space <= 0) { 1407 maxreached = 0; 1408 goto release; 1409 } 1410 if (space < len) { 1411 maxreached = 0; 1412 if (space < sosp->so_snd.sb_lowat) 1413 goto release; 1414 len = space; 1415 } 1416 sosp->so_state |= SS_ISSENDING; 1417 1418 SBLASTRECORDCHK(&so->so_rcv, "somove 1"); 1419 SBLASTMBUFCHK(&so->so_rcv, "somove 1"); 1420 m = so->so_rcv.sb_mb; 1421 if (m == NULL) 1422 goto release; 1423 nextrecord = m->m_nextpkt; 1424 1425 /* Drop address and control information not used with splicing. */ 1426 if (so->so_proto->pr_flags & PR_ADDR) { 1427 #ifdef DIAGNOSTIC 1428 if (m->m_type != MT_SONAME) 1429 panic("somove soname: so %p, so_type %d, m %p, " 1430 "m_type %d", so, so->so_type, m, m->m_type); 1431 #endif 1432 m = m->m_next; 1433 } 1434 while (m && m->m_type == MT_CONTROL) 1435 m = m->m_next; 1436 if (m == NULL) { 1437 sbdroprecord(&so->so_rcv); 1438 if (so->so_proto->pr_flags & PR_WANTRCVD && so->so_pcb) 1439 (so->so_proto->pr_usrreq)(so, PRU_RCVD, NULL, 1440 NULL, NULL, NULL); 1441 goto nextpkt; 1442 } 1443 1444 /* 1445 * By splicing sockets connected to localhost, userland might create a 1446 * loop. Dissolve splicing with error if loop is detected by counter. 1447 * 1448 * If we deal with looped broadcast/multicast packet we bail out with 1449 * no error to suppress splice termination. 1450 */ 1451 if ((m->m_flags & M_PKTHDR) && 1452 ((m->m_pkthdr.ph_loopcnt++ >= M_MAXLOOP) || 1453 ((m->m_flags & M_LOOP) && (m->m_flags & (M_BCAST|M_MCAST))))) { 1454 if (m->m_pkthdr.ph_loopcnt >= M_MAXLOOP) 1455 error = ELOOP; 1456 goto release; 1457 } 1458 1459 if (so->so_proto->pr_flags & PR_ATOMIC) { 1460 if ((m->m_flags & M_PKTHDR) == 0) 1461 panic("somove !PKTHDR: so %p, so_type %d, m %p, " 1462 "m_type %d", so, so->so_type, m, m->m_type); 1463 if (sosp->so_snd.sb_hiwat < m->m_pkthdr.len) { 1464 error = EMSGSIZE; 1465 goto release; 1466 } 1467 if (len < m->m_pkthdr.len) 1468 goto release; 1469 if (m->m_pkthdr.len < len) { 1470 maxreached = 0; 1471 len = m->m_pkthdr.len; 1472 } 1473 /* 1474 * Throw away the name mbuf after it has been assured 1475 * that the whole first record can be processed. 1476 */ 1477 m = so->so_rcv.sb_mb; 1478 sbfree(&so->so_rcv, m); 1479 so->so_rcv.sb_mb = m_free(m); 1480 sbsync(&so->so_rcv, nextrecord); 1481 } 1482 /* 1483 * Throw away the control mbufs after it has been assured 1484 * that the whole first record can be processed. 1485 */ 1486 m = so->so_rcv.sb_mb; 1487 while (m && m->m_type == MT_CONTROL) { 1488 sbfree(&so->so_rcv, m); 1489 so->so_rcv.sb_mb = m_free(m); 1490 m = so->so_rcv.sb_mb; 1491 sbsync(&so->so_rcv, nextrecord); 1492 } 1493 1494 SBLASTRECORDCHK(&so->so_rcv, "somove 2"); 1495 SBLASTMBUFCHK(&so->so_rcv, "somove 2"); 1496 1497 /* Take at most len mbufs out of receive buffer. */ 1498 for (off = 0, mp = &m; off <= len && *mp; 1499 off += (*mp)->m_len, mp = &(*mp)->m_next) { 1500 u_long size = len - off; 1501 1502 #ifdef DIAGNOSTIC 1503 if ((*mp)->m_type != MT_DATA && (*mp)->m_type != MT_HEADER) 1504 panic("somove type: so %p, so_type %d, m %p, " 1505 "m_type %d", so, so->so_type, *mp, (*mp)->m_type); 1506 #endif 1507 if ((*mp)->m_len > size) { 1508 /* 1509 * Move only a partial mbuf at maximum splice length or 1510 * if the drain buffer is too small for this large mbuf. 1511 */ 1512 if (!maxreached && so->so_snd.sb_datacc > 0) { 1513 len -= size; 1514 break; 1515 } 1516 *mp = m_copym(so->so_rcv.sb_mb, 0, size, wait); 1517 if (*mp == NULL) { 1518 len -= size; 1519 break; 1520 } 1521 so->so_rcv.sb_mb->m_data += size; 1522 so->so_rcv.sb_mb->m_len -= size; 1523 so->so_rcv.sb_cc -= size; 1524 so->so_rcv.sb_datacc -= size; 1525 } else { 1526 *mp = so->so_rcv.sb_mb; 1527 sbfree(&so->so_rcv, *mp); 1528 so->so_rcv.sb_mb = (*mp)->m_next; 1529 sbsync(&so->so_rcv, nextrecord); 1530 } 1531 } 1532 *mp = NULL; 1533 1534 SBLASTRECORDCHK(&so->so_rcv, "somove 3"); 1535 SBLASTMBUFCHK(&so->so_rcv, "somove 3"); 1536 SBCHECK(&so->so_rcv); 1537 if (m == NULL) 1538 goto release; 1539 m->m_nextpkt = NULL; 1540 if (m->m_flags & M_PKTHDR) { 1541 m_resethdr(m); 1542 m->m_pkthdr.len = len; 1543 } 1544 1545 /* Send window update to source peer as receive buffer has changed. */ 1546 if (so->so_proto->pr_flags & PR_WANTRCVD && so->so_pcb) 1547 (so->so_proto->pr_usrreq)(so, PRU_RCVD, NULL, 1548 NULL, NULL, NULL); 1549 1550 /* Receive buffer did shrink by len bytes, adjust oob. */ 1551 state = so->so_state; 1552 so->so_state &= ~SS_RCVATMARK; 1553 oobmark = so->so_oobmark; 1554 so->so_oobmark = oobmark > len ? oobmark - len : 0; 1555 if (oobmark) { 1556 if (oobmark == len) 1557 so->so_state |= SS_RCVATMARK; 1558 if (oobmark >= len) 1559 oobmark = 0; 1560 } 1561 1562 /* 1563 * Handle oob data. If any malloc fails, ignore error. 1564 * TCP urgent data is not very reliable anyway. 1565 */ 1566 while (((state & SS_RCVATMARK) || oobmark) && 1567 (so->so_options & SO_OOBINLINE)) { 1568 struct mbuf *o = NULL; 1569 1570 if (state & SS_RCVATMARK) { 1571 o = m_get(wait, MT_DATA); 1572 state &= ~SS_RCVATMARK; 1573 } else if (oobmark) { 1574 o = m_split(m, oobmark, wait); 1575 if (o) { 1576 error = (*sosp->so_proto->pr_usrreq)(sosp, 1577 PRU_SEND, m, NULL, NULL, NULL); 1578 if (error) { 1579 if (sosp->so_state & SS_CANTSENDMORE) 1580 error = EPIPE; 1581 m_freem(o); 1582 goto release; 1583 } 1584 len -= oobmark; 1585 so->so_splicelen += oobmark; 1586 m = o; 1587 o = m_get(wait, MT_DATA); 1588 } 1589 oobmark = 0; 1590 } 1591 if (o) { 1592 o->m_len = 1; 1593 *mtod(o, caddr_t) = *mtod(m, caddr_t); 1594 error = (*sosp->so_proto->pr_usrreq)(sosp, PRU_SENDOOB, 1595 o, NULL, NULL, NULL); 1596 if (error) { 1597 if (sosp->so_state & SS_CANTSENDMORE) 1598 error = EPIPE; 1599 m_freem(m); 1600 goto release; 1601 } 1602 len -= 1; 1603 so->so_splicelen += 1; 1604 if (oobmark) { 1605 oobmark -= 1; 1606 if (oobmark == 0) 1607 state |= SS_RCVATMARK; 1608 } 1609 m_adj(m, 1); 1610 } 1611 } 1612 1613 /* Append all remaining data to drain socket. */ 1614 if (so->so_rcv.sb_cc == 0 || maxreached) 1615 sosp->so_state &= ~SS_ISSENDING; 1616 error = (*sosp->so_proto->pr_usrreq)(sosp, PRU_SEND, m, NULL, NULL, 1617 NULL); 1618 if (error) { 1619 if (sosp->so_state & SS_CANTSENDMORE) 1620 error = EPIPE; 1621 goto release; 1622 } 1623 so->so_splicelen += len; 1624 1625 /* Move several packets if possible. */ 1626 if (!maxreached && nextrecord) 1627 goto nextpkt; 1628 1629 release: 1630 sosp->so_state &= ~SS_ISSENDING; 1631 if (!error && maxreached && so->so_splicemax == so->so_splicelen) 1632 error = EFBIG; 1633 if (error) 1634 so->so_error = error; 1635 if (((so->so_state & SS_CANTRCVMORE) && so->so_rcv.sb_cc == 0) || 1636 (sosp->so_state & SS_CANTSENDMORE) || maxreached || error) { 1637 sounsplice(so, sosp, 0); 1638 return (0); 1639 } 1640 if (timerisset(&so->so_idletv)) 1641 timeout_add_tv(&so->so_idleto, &so->so_idletv); 1642 return (1); 1643 } 1644 1645 #endif /* SOCKET_SPLICE */ 1646 1647 void 1648 sorwakeup(struct socket *so) 1649 { 1650 soassertlocked(so); 1651 1652 #ifdef SOCKET_SPLICE 1653 if (so->so_rcv.sb_flags & SB_SPLICE) { 1654 /* 1655 * TCP has a sendbuffer that can handle multiple packets 1656 * at once. So queue the stream a bit to accumulate data. 1657 * The sosplice thread will call somove() later and send 1658 * the packets calling tcp_output() only once. 1659 * In the UDP case, send out the packets immediately. 1660 * Using a thread would make things slower. 1661 */ 1662 if (so->so_proto->pr_flags & PR_WANTRCVD) 1663 task_add(sosplice_taskq, &so->so_splicetask); 1664 else 1665 somove(so, M_DONTWAIT); 1666 } 1667 if (isspliced(so)) 1668 return; 1669 #endif 1670 sowakeup(so, &so->so_rcv); 1671 if (so->so_upcall) 1672 (*(so->so_upcall))(so, so->so_upcallarg, M_DONTWAIT); 1673 } 1674 1675 void 1676 sowwakeup(struct socket *so) 1677 { 1678 soassertlocked(so); 1679 1680 #ifdef SOCKET_SPLICE 1681 if (so->so_snd.sb_flags & SB_SPLICE) 1682 task_add(sosplice_taskq, &so->so_sp->ssp_soback->so_splicetask); 1683 if (issplicedback(so)) 1684 return; 1685 #endif 1686 sowakeup(so, &so->so_snd); 1687 } 1688 1689 int 1690 sosetopt(struct socket *so, int level, int optname, struct mbuf *m) 1691 { 1692 int error = 0; 1693 1694 soassertlocked(so); 1695 1696 if (level != SOL_SOCKET) { 1697 if (so->so_proto->pr_ctloutput) { 1698 error = (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, 1699 level, optname, m); 1700 return (error); 1701 } 1702 error = ENOPROTOOPT; 1703 } else { 1704 switch (optname) { 1705 case SO_BINDANY: 1706 if ((error = suser(curproc)) != 0) /* XXX */ 1707 return (error); 1708 break; 1709 } 1710 1711 switch (optname) { 1712 1713 case SO_LINGER: 1714 if (m == NULL || m->m_len != sizeof (struct linger) || 1715 mtod(m, struct linger *)->l_linger < 0 || 1716 mtod(m, struct linger *)->l_linger > SHRT_MAX) 1717 return (EINVAL); 1718 so->so_linger = mtod(m, struct linger *)->l_linger; 1719 /* FALLTHROUGH */ 1720 1721 case SO_BINDANY: 1722 case SO_DEBUG: 1723 case SO_KEEPALIVE: 1724 case SO_USELOOPBACK: 1725 case SO_BROADCAST: 1726 case SO_REUSEADDR: 1727 case SO_REUSEPORT: 1728 case SO_OOBINLINE: 1729 case SO_TIMESTAMP: 1730 case SO_ZEROIZE: 1731 if (m == NULL || m->m_len < sizeof (int)) 1732 return (EINVAL); 1733 if (*mtod(m, int *)) 1734 so->so_options |= optname; 1735 else 1736 so->so_options &= ~optname; 1737 break; 1738 1739 case SO_DONTROUTE: 1740 if (m == NULL || m->m_len < sizeof (int)) 1741 return (EINVAL); 1742 if (*mtod(m, int *)) 1743 error = EOPNOTSUPP; 1744 break; 1745 1746 case SO_SNDBUF: 1747 case SO_RCVBUF: 1748 case SO_SNDLOWAT: 1749 case SO_RCVLOWAT: 1750 { 1751 u_long cnt; 1752 1753 if (m == NULL || m->m_len < sizeof (int)) 1754 return (EINVAL); 1755 cnt = *mtod(m, int *); 1756 if ((long)cnt <= 0) 1757 cnt = 1; 1758 switch (optname) { 1759 1760 case SO_SNDBUF: 1761 if (so->so_state & SS_CANTSENDMORE) 1762 return (EINVAL); 1763 if (sbcheckreserve(cnt, so->so_snd.sb_wat) || 1764 sbreserve(so, &so->so_snd, cnt)) 1765 return (ENOBUFS); 1766 so->so_snd.sb_wat = cnt; 1767 break; 1768 1769 case SO_RCVBUF: 1770 if (so->so_state & SS_CANTRCVMORE) 1771 return (EINVAL); 1772 if (sbcheckreserve(cnt, so->so_rcv.sb_wat) || 1773 sbreserve(so, &so->so_rcv, cnt)) 1774 return (ENOBUFS); 1775 so->so_rcv.sb_wat = cnt; 1776 break; 1777 1778 case SO_SNDLOWAT: 1779 so->so_snd.sb_lowat = 1780 (cnt > so->so_snd.sb_hiwat) ? 1781 so->so_snd.sb_hiwat : cnt; 1782 break; 1783 case SO_RCVLOWAT: 1784 so->so_rcv.sb_lowat = 1785 (cnt > so->so_rcv.sb_hiwat) ? 1786 so->so_rcv.sb_hiwat : cnt; 1787 break; 1788 } 1789 break; 1790 } 1791 1792 case SO_SNDTIMEO: 1793 case SO_RCVTIMEO: 1794 { 1795 struct timeval tv; 1796 uint64_t nsecs; 1797 1798 if (m == NULL || m->m_len < sizeof (tv)) 1799 return (EINVAL); 1800 memcpy(&tv, mtod(m, struct timeval *), sizeof tv); 1801 if (!timerisvalid(&tv)) 1802 return (EINVAL); 1803 nsecs = TIMEVAL_TO_NSEC(&tv); 1804 if (nsecs == UINT64_MAX) 1805 return (EDOM); 1806 if (nsecs == 0) 1807 nsecs = INFSLP; 1808 switch (optname) { 1809 1810 case SO_SNDTIMEO: 1811 so->so_snd.sb_timeo_nsecs = nsecs; 1812 break; 1813 case SO_RCVTIMEO: 1814 so->so_rcv.sb_timeo_nsecs = nsecs; 1815 break; 1816 } 1817 break; 1818 } 1819 1820 case SO_RTABLE: 1821 if (so->so_proto->pr_domain && 1822 so->so_proto->pr_domain->dom_protosw && 1823 so->so_proto->pr_ctloutput) { 1824 struct domain *dom = so->so_proto->pr_domain; 1825 1826 level = dom->dom_protosw->pr_protocol; 1827 error = (*so->so_proto->pr_ctloutput) 1828 (PRCO_SETOPT, so, level, optname, m); 1829 return (error); 1830 } 1831 error = ENOPROTOOPT; 1832 break; 1833 1834 #ifdef SOCKET_SPLICE 1835 case SO_SPLICE: 1836 if (m == NULL) { 1837 error = sosplice(so, -1, 0, NULL); 1838 } else if (m->m_len < sizeof(int)) { 1839 return (EINVAL); 1840 } else if (m->m_len < sizeof(struct splice)) { 1841 error = sosplice(so, *mtod(m, int *), 0, NULL); 1842 } else { 1843 error = sosplice(so, 1844 mtod(m, struct splice *)->sp_fd, 1845 mtod(m, struct splice *)->sp_max, 1846 &mtod(m, struct splice *)->sp_idle); 1847 } 1848 break; 1849 #endif /* SOCKET_SPLICE */ 1850 1851 default: 1852 error = ENOPROTOOPT; 1853 break; 1854 } 1855 if (error == 0 && so->so_proto->pr_ctloutput) { 1856 (*so->so_proto->pr_ctloutput)(PRCO_SETOPT, so, 1857 level, optname, m); 1858 } 1859 } 1860 1861 return (error); 1862 } 1863 1864 int 1865 sogetopt(struct socket *so, int level, int optname, struct mbuf *m) 1866 { 1867 int error = 0; 1868 1869 soassertlocked(so); 1870 1871 if (level != SOL_SOCKET) { 1872 if (so->so_proto->pr_ctloutput) { 1873 m->m_len = 0; 1874 1875 error = (*so->so_proto->pr_ctloutput)(PRCO_GETOPT, so, 1876 level, optname, m); 1877 if (error) 1878 return (error); 1879 return (0); 1880 } else 1881 return (ENOPROTOOPT); 1882 } else { 1883 m->m_len = sizeof (int); 1884 1885 switch (optname) { 1886 1887 case SO_LINGER: 1888 m->m_len = sizeof (struct linger); 1889 mtod(m, struct linger *)->l_onoff = 1890 so->so_options & SO_LINGER; 1891 mtod(m, struct linger *)->l_linger = so->so_linger; 1892 break; 1893 1894 case SO_BINDANY: 1895 case SO_USELOOPBACK: 1896 case SO_DEBUG: 1897 case SO_KEEPALIVE: 1898 case SO_REUSEADDR: 1899 case SO_REUSEPORT: 1900 case SO_BROADCAST: 1901 case SO_OOBINLINE: 1902 case SO_TIMESTAMP: 1903 case SO_ZEROIZE: 1904 *mtod(m, int *) = so->so_options & optname; 1905 break; 1906 1907 case SO_DONTROUTE: 1908 *mtod(m, int *) = 0; 1909 break; 1910 1911 case SO_TYPE: 1912 *mtod(m, int *) = so->so_type; 1913 break; 1914 1915 case SO_ERROR: 1916 *mtod(m, int *) = so->so_error; 1917 so->so_error = 0; 1918 break; 1919 1920 case SO_DOMAIN: 1921 *mtod(m, int *) = so->so_proto->pr_domain->dom_family; 1922 break; 1923 1924 case SO_PROTOCOL: 1925 *mtod(m, int *) = so->so_proto->pr_protocol; 1926 break; 1927 1928 case SO_SNDBUF: 1929 *mtod(m, int *) = so->so_snd.sb_hiwat; 1930 break; 1931 1932 case SO_RCVBUF: 1933 *mtod(m, int *) = so->so_rcv.sb_hiwat; 1934 break; 1935 1936 case SO_SNDLOWAT: 1937 *mtod(m, int *) = so->so_snd.sb_lowat; 1938 break; 1939 1940 case SO_RCVLOWAT: 1941 *mtod(m, int *) = so->so_rcv.sb_lowat; 1942 break; 1943 1944 case SO_SNDTIMEO: 1945 case SO_RCVTIMEO: 1946 { 1947 struct timeval tv; 1948 uint64_t nsecs = (optname == SO_SNDTIMEO ? 1949 so->so_snd.sb_timeo_nsecs : 1950 so->so_rcv.sb_timeo_nsecs); 1951 1952 m->m_len = sizeof(struct timeval); 1953 memset(&tv, 0, sizeof(tv)); 1954 if (nsecs != INFSLP) 1955 NSEC_TO_TIMEVAL(nsecs, &tv); 1956 memcpy(mtod(m, struct timeval *), &tv, sizeof tv); 1957 break; 1958 } 1959 1960 case SO_RTABLE: 1961 if (so->so_proto->pr_domain && 1962 so->so_proto->pr_domain->dom_protosw && 1963 so->so_proto->pr_ctloutput) { 1964 struct domain *dom = so->so_proto->pr_domain; 1965 1966 level = dom->dom_protosw->pr_protocol; 1967 error = (*so->so_proto->pr_ctloutput) 1968 (PRCO_GETOPT, so, level, optname, m); 1969 if (error) 1970 return (error); 1971 break; 1972 } 1973 return (ENOPROTOOPT); 1974 1975 #ifdef SOCKET_SPLICE 1976 case SO_SPLICE: 1977 { 1978 off_t len; 1979 1980 m->m_len = sizeof(off_t); 1981 len = so->so_sp ? so->so_sp->ssp_len : 0; 1982 memcpy(mtod(m, off_t *), &len, sizeof(off_t)); 1983 break; 1984 } 1985 #endif /* SOCKET_SPLICE */ 1986 1987 case SO_PEERCRED: 1988 if (so->so_proto->pr_protocol == AF_UNIX) { 1989 struct unpcb *unp = sotounpcb(so); 1990 1991 if (unp->unp_flags & UNP_FEIDS) { 1992 m->m_len = sizeof(unp->unp_connid); 1993 memcpy(mtod(m, caddr_t), 1994 &(unp->unp_connid), m->m_len); 1995 break; 1996 } 1997 return (ENOTCONN); 1998 } 1999 return (EOPNOTSUPP); 2000 2001 default: 2002 return (ENOPROTOOPT); 2003 } 2004 return (0); 2005 } 2006 } 2007 2008 void 2009 sohasoutofband(struct socket *so) 2010 { 2011 pgsigio(&so->so_sigio, SIGURG, 0); 2012 selwakeup(&so->so_rcv.sb_sel); 2013 } 2014 2015 int 2016 soo_kqfilter(struct file *fp, struct knote *kn) 2017 { 2018 struct socket *so = kn->kn_fp->f_data; 2019 struct sockbuf *sb; 2020 2021 KERNEL_ASSERT_LOCKED(); 2022 2023 switch (kn->kn_filter) { 2024 case EVFILT_READ: 2025 if (so->so_options & SO_ACCEPTCONN) 2026 kn->kn_fop = &solisten_filtops; 2027 else 2028 kn->kn_fop = &soread_filtops; 2029 sb = &so->so_rcv; 2030 break; 2031 case EVFILT_WRITE: 2032 kn->kn_fop = &sowrite_filtops; 2033 sb = &so->so_snd; 2034 break; 2035 case EVFILT_EXCEPT: 2036 kn->kn_fop = &soexcept_filtops; 2037 sb = &so->so_rcv; 2038 break; 2039 default: 2040 return (EINVAL); 2041 } 2042 2043 klist_insert(&sb->sb_sel.si_note, kn); 2044 sb->sb_flagsintr |= SB_KNOTE; 2045 2046 return (0); 2047 } 2048 2049 void 2050 filt_sordetach(struct knote *kn) 2051 { 2052 struct socket *so = kn->kn_fp->f_data; 2053 2054 KERNEL_ASSERT_LOCKED(); 2055 2056 klist_remove(&so->so_rcv.sb_sel.si_note, kn); 2057 if (klist_empty(&so->so_rcv.sb_sel.si_note)) 2058 so->so_rcv.sb_flagsintr &= ~SB_KNOTE; 2059 } 2060 2061 int 2062 filt_soread(struct knote *kn, long hint) 2063 { 2064 struct socket *so = kn->kn_fp->f_data; 2065 int s, rv = 0; 2066 2067 if ((hint & NOTE_SUBMIT) == 0) 2068 s = solock(so); 2069 kn->kn_data = so->so_rcv.sb_cc; 2070 #ifdef SOCKET_SPLICE 2071 if (isspliced(so)) { 2072 rv = 0; 2073 } else 2074 #endif /* SOCKET_SPLICE */ 2075 if (kn->kn_sfflags & NOTE_OOB) { 2076 if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) { 2077 kn->kn_fflags |= NOTE_OOB; 2078 kn->kn_data -= so->so_oobmark; 2079 rv = 1; 2080 } 2081 } else if (so->so_state & SS_CANTRCVMORE) { 2082 kn->kn_flags |= EV_EOF; 2083 if (kn->kn_flags & __EV_POLL) { 2084 if (so->so_state & SS_ISDISCONNECTED) 2085 kn->kn_flags |= __EV_HUP; 2086 } 2087 kn->kn_fflags = so->so_error; 2088 rv = 1; 2089 } else if (so->so_error) { /* temporary udp error */ 2090 rv = 1; 2091 } else if (kn->kn_sfflags & NOTE_LOWAT) { 2092 rv = (kn->kn_data >= kn->kn_sdata); 2093 } else { 2094 rv = (kn->kn_data >= so->so_rcv.sb_lowat); 2095 } 2096 if ((hint & NOTE_SUBMIT) == 0) 2097 sounlock(so, s); 2098 2099 return rv; 2100 } 2101 2102 void 2103 filt_sowdetach(struct knote *kn) 2104 { 2105 struct socket *so = kn->kn_fp->f_data; 2106 2107 KERNEL_ASSERT_LOCKED(); 2108 2109 klist_remove(&so->so_snd.sb_sel.si_note, kn); 2110 if (klist_empty(&so->so_snd.sb_sel.si_note)) 2111 so->so_snd.sb_flagsintr &= ~SB_KNOTE; 2112 } 2113 2114 int 2115 filt_sowrite(struct knote *kn, long hint) 2116 { 2117 struct socket *so = kn->kn_fp->f_data; 2118 int s, rv; 2119 2120 if ((hint & NOTE_SUBMIT) == 0) 2121 s = solock(so); 2122 kn->kn_data = sbspace(so, &so->so_snd); 2123 if (so->so_state & SS_CANTSENDMORE) { 2124 kn->kn_flags |= EV_EOF; 2125 if (kn->kn_flags & __EV_POLL) { 2126 if (so->so_state & SS_ISDISCONNECTED) 2127 kn->kn_flags |= __EV_HUP; 2128 } 2129 kn->kn_fflags = so->so_error; 2130 rv = 1; 2131 } else if (so->so_error) { /* temporary udp error */ 2132 rv = 1; 2133 } else if (((so->so_state & SS_ISCONNECTED) == 0) && 2134 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 2135 rv = 0; 2136 } else if (kn->kn_sfflags & NOTE_LOWAT) { 2137 rv = (kn->kn_data >= kn->kn_sdata); 2138 } else { 2139 rv = (kn->kn_data >= so->so_snd.sb_lowat); 2140 } 2141 if ((hint & NOTE_SUBMIT) == 0) 2142 sounlock(so, s); 2143 2144 return (rv); 2145 } 2146 2147 int 2148 filt_solisten(struct knote *kn, long hint) 2149 { 2150 struct socket *so = kn->kn_fp->f_data; 2151 int s; 2152 2153 if ((hint & NOTE_SUBMIT) == 0) 2154 s = solock(so); 2155 kn->kn_data = so->so_qlen; 2156 if ((hint & NOTE_SUBMIT) == 0) 2157 sounlock(so, s); 2158 2159 return (kn->kn_data != 0); 2160 } 2161 2162 #ifdef DDB 2163 void 2164 sobuf_print(struct sockbuf *, 2165 int (*)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))); 2166 2167 void 2168 sobuf_print(struct sockbuf *sb, 2169 int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) 2170 { 2171 (*pr)("\tsb_cc: %lu\n", sb->sb_cc); 2172 (*pr)("\tsb_datacc: %lu\n", sb->sb_datacc); 2173 (*pr)("\tsb_hiwat: %lu\n", sb->sb_hiwat); 2174 (*pr)("\tsb_wat: %lu\n", sb->sb_wat); 2175 (*pr)("\tsb_mbcnt: %lu\n", sb->sb_mbcnt); 2176 (*pr)("\tsb_mbmax: %lu\n", sb->sb_mbmax); 2177 (*pr)("\tsb_lowat: %ld\n", sb->sb_lowat); 2178 (*pr)("\tsb_mb: %p\n", sb->sb_mb); 2179 (*pr)("\tsb_mbtail: %p\n", sb->sb_mbtail); 2180 (*pr)("\tsb_lastrecord: %p\n", sb->sb_lastrecord); 2181 (*pr)("\tsb_sel: ...\n"); 2182 (*pr)("\tsb_flagsintr: %d\n", sb->sb_flagsintr); 2183 (*pr)("\tsb_flags: %i\n", sb->sb_flags); 2184 (*pr)("\tsb_timeo_nsecs: %llu\n", sb->sb_timeo_nsecs); 2185 } 2186 2187 void 2188 so_print(void *v, 2189 int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) 2190 { 2191 struct socket *so = v; 2192 2193 (*pr)("socket %p\n", so); 2194 (*pr)("so_type: %i\n", so->so_type); 2195 (*pr)("so_options: 0x%04x\n", so->so_options); /* %b */ 2196 (*pr)("so_linger: %i\n", so->so_linger); 2197 (*pr)("so_state: 0x%04x\n", so->so_state); 2198 (*pr)("so_pcb: %p\n", so->so_pcb); 2199 (*pr)("so_proto: %p\n", so->so_proto); 2200 (*pr)("so_sigio: %p\n", so->so_sigio.sir_sigio); 2201 2202 (*pr)("so_head: %p\n", so->so_head); 2203 (*pr)("so_onq: %p\n", so->so_onq); 2204 (*pr)("so_q0: @%p first: %p\n", &so->so_q0, TAILQ_FIRST(&so->so_q0)); 2205 (*pr)("so_q: @%p first: %p\n", &so->so_q, TAILQ_FIRST(&so->so_q)); 2206 (*pr)("so_eq: next: %p\n", TAILQ_NEXT(so, so_qe)); 2207 (*pr)("so_q0len: %i\n", so->so_q0len); 2208 (*pr)("so_qlen: %i\n", so->so_qlen); 2209 (*pr)("so_qlimit: %i\n", so->so_qlimit); 2210 (*pr)("so_timeo: %i\n", so->so_timeo); 2211 (*pr)("so_obmark: %lu\n", so->so_oobmark); 2212 2213 (*pr)("so_sp: %p\n", so->so_sp); 2214 if (so->so_sp != NULL) { 2215 (*pr)("\tssp_socket: %p\n", so->so_sp->ssp_socket); 2216 (*pr)("\tssp_soback: %p\n", so->so_sp->ssp_soback); 2217 (*pr)("\tssp_len: %lld\n", 2218 (unsigned long long)so->so_sp->ssp_len); 2219 (*pr)("\tssp_max: %lld\n", 2220 (unsigned long long)so->so_sp->ssp_max); 2221 (*pr)("\tssp_idletv: %lld %ld\n", so->so_sp->ssp_idletv.tv_sec, 2222 so->so_sp->ssp_idletv.tv_usec); 2223 (*pr)("\tssp_idleto: %spending (@%i)\n", 2224 timeout_pending(&so->so_sp->ssp_idleto) ? "" : "not ", 2225 so->so_sp->ssp_idleto.to_time); 2226 } 2227 2228 (*pr)("so_rcv:\n"); 2229 sobuf_print(&so->so_rcv, pr); 2230 (*pr)("so_snd:\n"); 2231 sobuf_print(&so->so_snd, pr); 2232 2233 (*pr)("so_upcall: %p so_upcallarg: %p\n", 2234 so->so_upcall, so->so_upcallarg); 2235 2236 (*pr)("so_euid: %d so_ruid: %d\n", so->so_euid, so->so_ruid); 2237 (*pr)("so_egid: %d so_rgid: %d\n", so->so_egid, so->so_rgid); 2238 (*pr)("so_cpid: %d\n", so->so_cpid); 2239 } 2240 #endif 2241