1 /* $OpenBSD: uipc_socket.c,v 1.252 2020/12/25 12:59:52 visa 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 #include <sys/time.h> 55 56 #ifdef DDB 57 #include <machine/db_machdep.h> 58 #endif 59 60 void sbsync(struct sockbuf *, struct mbuf *); 61 62 int sosplice(struct socket *, int, off_t, struct timeval *); 63 void sounsplice(struct socket *, struct socket *, int); 64 void soidle(void *); 65 void sotask(void *); 66 void soreaper(void *); 67 void soput(void *); 68 int somove(struct socket *, int); 69 70 void filt_sordetach(struct knote *kn); 71 int filt_soread(struct knote *kn, long hint); 72 void filt_sowdetach(struct knote *kn); 73 int filt_sowrite(struct knote *kn, long hint); 74 int filt_solisten(struct knote *kn, long hint); 75 76 const struct filterops solisten_filtops = { 77 .f_flags = FILTEROP_ISFD, 78 .f_attach = NULL, 79 .f_detach = filt_sordetach, 80 .f_event = filt_solisten, 81 }; 82 83 const struct filterops soread_filtops = { 84 .f_flags = FILTEROP_ISFD, 85 .f_attach = NULL, 86 .f_detach = filt_sordetach, 87 .f_event = filt_soread, 88 }; 89 90 const struct filterops sowrite_filtops = { 91 .f_flags = FILTEROP_ISFD, 92 .f_attach = NULL, 93 .f_detach = filt_sowdetach, 94 .f_event = filt_sowrite, 95 }; 96 97 const struct filterops soexcept_filtops = { 98 .f_flags = FILTEROP_ISFD, 99 .f_attach = NULL, 100 .f_detach = filt_sordetach, 101 .f_event = filt_soread, 102 }; 103 104 #ifndef SOMINCONN 105 #define SOMINCONN 80 106 #endif /* SOMINCONN */ 107 108 int somaxconn = SOMAXCONN; 109 int sominconn = SOMINCONN; 110 111 struct pool socket_pool; 112 #ifdef SOCKET_SPLICE 113 struct pool sosplice_pool; 114 struct taskq *sosplice_taskq; 115 struct rwlock sosplice_lock = RWLOCK_INITIALIZER("sosplicelk"); 116 #endif 117 118 void 119 soinit(void) 120 { 121 pool_init(&socket_pool, sizeof(struct socket), 0, IPL_SOFTNET, 0, 122 "sockpl", NULL); 123 #ifdef SOCKET_SPLICE 124 pool_init(&sosplice_pool, sizeof(struct sosplice), 0, IPL_SOFTNET, 0, 125 "sosppl", NULL); 126 #endif 127 } 128 129 /* 130 * Socket operation routines. 131 * These routines are called by the routines in 132 * sys_socket.c or from a system process, and 133 * implement the semantics of socket operations by 134 * switching out to the protocol specific routines. 135 */ 136 int 137 socreate(int dom, struct socket **aso, int type, int proto) 138 { 139 struct proc *p = curproc; /* XXX */ 140 const struct protosw *prp; 141 struct socket *so; 142 int error, s; 143 144 if (proto) 145 prp = pffindproto(dom, proto, type); 146 else 147 prp = pffindtype(dom, type); 148 if (prp == NULL || prp->pr_attach == NULL) 149 return (EPROTONOSUPPORT); 150 if (prp->pr_type != type) 151 return (EPROTOTYPE); 152 so = pool_get(&socket_pool, PR_WAITOK | PR_ZERO); 153 sigio_init(&so->so_sigio); 154 TAILQ_INIT(&so->so_q0); 155 TAILQ_INIT(&so->so_q); 156 so->so_type = type; 157 if (suser(p) == 0) 158 so->so_state = SS_PRIV; 159 so->so_ruid = p->p_ucred->cr_ruid; 160 so->so_euid = p->p_ucred->cr_uid; 161 so->so_rgid = p->p_ucred->cr_rgid; 162 so->so_egid = p->p_ucred->cr_gid; 163 so->so_cpid = p->p_p->ps_pid; 164 so->so_proto = prp; 165 so->so_snd.sb_timeo_nsecs = INFSLP; 166 so->so_rcv.sb_timeo_nsecs = INFSLP; 167 168 s = solock(so); 169 error = (*prp->pr_attach)(so, proto); 170 if (error) { 171 so->so_state |= SS_NOFDREF; 172 /* sofree() calls sounlock(). */ 173 sofree(so, s); 174 return (error); 175 } 176 sounlock(so, s); 177 *aso = so; 178 return (0); 179 } 180 181 int 182 sobind(struct socket *so, struct mbuf *nam, struct proc *p) 183 { 184 int error; 185 186 soassertlocked(so); 187 188 error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, p); 189 return (error); 190 } 191 192 int 193 solisten(struct socket *so, int backlog) 194 { 195 int error; 196 197 soassertlocked(so); 198 199 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING)) 200 return (EINVAL); 201 #ifdef SOCKET_SPLICE 202 if (isspliced(so) || issplicedback(so)) 203 return (EOPNOTSUPP); 204 #endif /* SOCKET_SPLICE */ 205 error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, NULL, NULL, 206 curproc); 207 if (error) 208 return (error); 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 return (0); 217 } 218 219 #define SOSP_FREEING_READ 1 220 #define SOSP_FREEING_WRITE 2 221 void 222 sofree(struct socket *so, int s) 223 { 224 soassertlocked(so); 225 226 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) { 227 sounlock(so, s); 228 return; 229 } 230 if (so->so_head) { 231 /* 232 * We must not decommission a socket that's on the accept(2) 233 * queue. If we do, then accept(2) may hang after select(2) 234 * indicated that the listening socket was ready. 235 */ 236 if (!soqremque(so, 0)) { 237 sounlock(so, s); 238 return; 239 } 240 } 241 sigio_free(&so->so_sigio); 242 #ifdef SOCKET_SPLICE 243 if (so->so_sp) { 244 if (issplicedback(so)) { 245 int freeing = SOSP_FREEING_WRITE; 246 247 if (so->so_sp->ssp_soback == so) 248 freeing |= SOSP_FREEING_READ; 249 sounsplice(so->so_sp->ssp_soback, so, freeing); 250 } 251 if (isspliced(so)) { 252 int freeing = SOSP_FREEING_READ; 253 254 if (so == so->so_sp->ssp_socket) 255 freeing |= SOSP_FREEING_WRITE; 256 sounsplice(so, so->so_sp->ssp_socket, freeing); 257 } 258 } 259 #endif /* SOCKET_SPLICE */ 260 sbrelease(so, &so->so_snd); 261 sorflush(so); 262 sounlock(so, s); 263 #ifdef SOCKET_SPLICE 264 if (so->so_sp) { 265 /* Reuse splice idle, sounsplice() has been called before. */ 266 timeout_set_proc(&so->so_sp->ssp_idleto, soreaper, so); 267 timeout_add(&so->so_sp->ssp_idleto, 0); 268 } else 269 #endif /* SOCKET_SPLICE */ 270 { 271 pool_put(&socket_pool, so); 272 } 273 } 274 275 static inline uint64_t 276 solinger_nsec(struct socket *so) 277 { 278 if (so->so_linger == 0) 279 return INFSLP; 280 281 return SEC_TO_NSEC(so->so_linger); 282 } 283 284 /* 285 * Close a socket on last file table reference removal. 286 * Initiate disconnect if connected. 287 * Free socket when disconnect complete. 288 */ 289 int 290 soclose(struct socket *so, int flags) 291 { 292 struct socket *so2; 293 int s, error = 0; 294 295 s = solock(so); 296 /* Revoke async IO early. There is a final revocation in sofree(). */ 297 sigio_free(&so->so_sigio); 298 if (so->so_options & SO_ACCEPTCONN) { 299 while ((so2 = TAILQ_FIRST(&so->so_q0)) != NULL) { 300 (void) soqremque(so2, 0); 301 (void) soabort(so2); 302 } 303 while ((so2 = TAILQ_FIRST(&so->so_q)) != NULL) { 304 (void) soqremque(so2, 1); 305 (void) soabort(so2); 306 } 307 } 308 if (so->so_pcb == NULL) 309 goto discard; 310 if (so->so_state & SS_ISCONNECTED) { 311 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 312 error = sodisconnect(so); 313 if (error) 314 goto drop; 315 } 316 if (so->so_options & SO_LINGER) { 317 if ((so->so_state & SS_ISDISCONNECTING) && 318 (flags & MSG_DONTWAIT)) 319 goto drop; 320 while (so->so_state & SS_ISCONNECTED) { 321 error = sosleep_nsec(so, &so->so_timeo, 322 PSOCK | PCATCH, "netcls", 323 solinger_nsec(so)); 324 if (error) 325 break; 326 } 327 } 328 } 329 drop: 330 if (so->so_pcb) { 331 int error2; 332 KASSERT(so->so_proto->pr_detach); 333 error2 = (*so->so_proto->pr_detach)(so); 334 if (error == 0) 335 error = error2; 336 } 337 discard: 338 if (so->so_state & SS_NOFDREF) 339 panic("soclose NOFDREF: so %p, so_type %d", so, so->so_type); 340 so->so_state |= SS_NOFDREF; 341 /* sofree() calls sounlock(). */ 342 sofree(so, s); 343 return (error); 344 } 345 346 int 347 soabort(struct socket *so) 348 { 349 soassertlocked(so); 350 351 return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, NULL, NULL, 352 curproc); 353 } 354 355 int 356 soaccept(struct socket *so, struct mbuf *nam) 357 { 358 int error = 0; 359 360 soassertlocked(so); 361 362 if ((so->so_state & SS_NOFDREF) == 0) 363 panic("soaccept !NOFDREF: so %p, so_type %d", so, so->so_type); 364 so->so_state &= ~SS_NOFDREF; 365 if ((so->so_state & SS_ISDISCONNECTED) == 0 || 366 (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) 367 error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, NULL, 368 nam, NULL, curproc); 369 else 370 error = ECONNABORTED; 371 return (error); 372 } 373 374 int 375 soconnect(struct socket *so, struct mbuf *nam) 376 { 377 int error; 378 379 soassertlocked(so); 380 381 if (so->so_options & SO_ACCEPTCONN) 382 return (EOPNOTSUPP); 383 /* 384 * If protocol is connection-based, can only connect once. 385 * Otherwise, if connected, try to disconnect first. 386 * This allows user to disconnect by connecting to, e.g., 387 * a null address. 388 */ 389 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 390 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 391 (error = sodisconnect(so)))) 392 error = EISCONN; 393 else 394 error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, 395 NULL, nam, NULL, curproc); 396 return (error); 397 } 398 399 int 400 soconnect2(struct socket *so1, struct socket *so2) 401 { 402 int s, error; 403 404 s = solock(so1); 405 error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, NULL, 406 (struct mbuf *)so2, NULL, curproc); 407 sounlock(so1, s); 408 return (error); 409 } 410 411 int 412 sodisconnect(struct socket *so) 413 { 414 int error; 415 416 soassertlocked(so); 417 418 if ((so->so_state & SS_ISCONNECTED) == 0) 419 return (ENOTCONN); 420 if (so->so_state & SS_ISDISCONNECTING) 421 return (EALREADY); 422 error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, NULL, NULL, 423 NULL, curproc); 424 return (error); 425 } 426 427 int m_getuio(struct mbuf **, int, long, struct uio *); 428 429 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 430 /* 431 * Send on a socket. 432 * If send must go all at once and message is larger than 433 * send buffering, then hard error. 434 * Lock against other senders. 435 * If must go all at once and not enough room now, then 436 * inform user that this would block and do nothing. 437 * Otherwise, if nonblocking, send as much as possible. 438 * The data to be sent is described by "uio" if nonzero, 439 * otherwise by the mbuf chain "top" (which must be null 440 * if uio is not). Data provided in mbuf chain must be small 441 * enough to send all at once. 442 * 443 * Returns nonzero on error, timeout or signal; callers 444 * must check for short counts if EINTR/ERESTART are returned. 445 * Data and control buffers are freed on return. 446 */ 447 int 448 sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top, 449 struct mbuf *control, int flags) 450 { 451 long space, clen = 0; 452 size_t resid; 453 int error, s; 454 int atomic = sosendallatonce(so) || top; 455 456 if (uio) 457 resid = uio->uio_resid; 458 else 459 resid = top->m_pkthdr.len; 460 /* MSG_EOR on a SOCK_STREAM socket is invalid. */ 461 if (so->so_type == SOCK_STREAM && (flags & MSG_EOR)) { 462 m_freem(top); 463 m_freem(control); 464 return (EINVAL); 465 } 466 if (uio && uio->uio_procp) 467 uio->uio_procp->p_ru.ru_msgsnd++; 468 if (control) { 469 /* 470 * In theory clen should be unsigned (since control->m_len is). 471 * However, space must be signed, as it might be less than 0 472 * if we over-committed, and we must use a signed comparison 473 * of space and clen. 474 */ 475 clen = control->m_len; 476 /* reserve extra space for AF_UNIX's internalize */ 477 if (so->so_proto->pr_domain->dom_family == AF_UNIX && 478 clen >= CMSG_ALIGN(sizeof(struct cmsghdr)) && 479 mtod(control, struct cmsghdr *)->cmsg_type == SCM_RIGHTS) 480 clen = CMSG_SPACE( 481 (clen - CMSG_ALIGN(sizeof(struct cmsghdr))) * 482 (sizeof(struct fdpass) / sizeof(int))); 483 } 484 485 #define snderr(errno) { error = errno; goto release; } 486 487 s = solock(so); 488 restart: 489 if ((error = sblock(so, &so->so_snd, SBLOCKWAIT(flags))) != 0) 490 goto out; 491 so->so_state |= SS_ISSENDING; 492 do { 493 if (so->so_state & SS_CANTSENDMORE) 494 snderr(EPIPE); 495 if (so->so_error) { 496 error = so->so_error; 497 so->so_error = 0; 498 snderr(error); 499 } 500 if ((so->so_state & SS_ISCONNECTED) == 0) { 501 if (so->so_proto->pr_flags & PR_CONNREQUIRED) { 502 if (!(resid == 0 && clen != 0)) 503 snderr(ENOTCONN); 504 } else if (addr == 0) 505 snderr(EDESTADDRREQ); 506 } 507 space = sbspace(so, &so->so_snd); 508 if (flags & MSG_OOB) 509 space += 1024; 510 if (so->so_proto->pr_domain->dom_family == AF_UNIX) { 511 if (atomic && resid > so->so_snd.sb_hiwat) 512 snderr(EMSGSIZE); 513 } else { 514 if (clen > so->so_snd.sb_hiwat || 515 (atomic && resid > so->so_snd.sb_hiwat - clen)) 516 snderr(EMSGSIZE); 517 } 518 if (space < clen || 519 (space - clen < resid && 520 (atomic || space < so->so_snd.sb_lowat))) { 521 if (flags & MSG_DONTWAIT) 522 snderr(EWOULDBLOCK); 523 sbunlock(so, &so->so_snd); 524 error = sbwait(so, &so->so_snd); 525 so->so_state &= ~SS_ISSENDING; 526 if (error) 527 goto out; 528 goto restart; 529 } 530 space -= clen; 531 do { 532 if (uio == NULL) { 533 /* 534 * Data is prepackaged in "top". 535 */ 536 resid = 0; 537 if (flags & MSG_EOR) 538 top->m_flags |= M_EOR; 539 } else { 540 sounlock(so, s); 541 error = m_getuio(&top, atomic, space, uio); 542 s = solock(so); 543 if (error) 544 goto release; 545 space -= top->m_pkthdr.len; 546 resid = uio->uio_resid; 547 if (flags & MSG_EOR) 548 top->m_flags |= M_EOR; 549 } 550 if (resid == 0) 551 so->so_state &= ~SS_ISSENDING; 552 if (top && so->so_options & SO_ZEROIZE) 553 top->m_flags |= M_ZEROIZE; 554 error = (*so->so_proto->pr_usrreq)(so, 555 (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, 556 top, addr, control, curproc); 557 clen = 0; 558 control = NULL; 559 top = NULL; 560 if (error) 561 goto release; 562 } while (resid && space > 0); 563 } while (resid); 564 565 release: 566 so->so_state &= ~SS_ISSENDING; 567 sbunlock(so, &so->so_snd); 568 out: 569 sounlock(so, s); 570 m_freem(top); 571 m_freem(control); 572 return (error); 573 } 574 575 int 576 m_getuio(struct mbuf **mp, int atomic, long space, struct uio *uio) 577 { 578 struct mbuf *m, *top = NULL; 579 struct mbuf **nextp = ⊤ 580 u_long len, mlen; 581 size_t resid = uio->uio_resid; 582 int error; 583 584 do { 585 if (top == NULL) { 586 MGETHDR(m, M_WAIT, MT_DATA); 587 mlen = MHLEN; 588 m->m_pkthdr.len = 0; 589 m->m_pkthdr.ph_ifidx = 0; 590 } else { 591 MGET(m, M_WAIT, MT_DATA); 592 mlen = MLEN; 593 } 594 /* chain mbuf together */ 595 *nextp = m; 596 nextp = &m->m_next; 597 598 resid = ulmin(resid, space); 599 if (resid >= MINCLSIZE) { 600 MCLGETL(m, M_NOWAIT, ulmin(resid, MAXMCLBYTES)); 601 if ((m->m_flags & M_EXT) == 0) 602 MCLGETL(m, M_NOWAIT, MCLBYTES); 603 if ((m->m_flags & M_EXT) == 0) 604 goto nopages; 605 mlen = m->m_ext.ext_size; 606 len = ulmin(mlen, resid); 607 /* 608 * For datagram protocols, leave room 609 * for protocol headers in first mbuf. 610 */ 611 if (atomic && m == top && len < mlen - max_hdr) 612 m->m_data += max_hdr; 613 } else { 614 nopages: 615 len = ulmin(mlen, resid); 616 /* 617 * For datagram protocols, leave room 618 * for protocol headers in first mbuf. 619 */ 620 if (atomic && m == top && len < mlen - max_hdr) 621 m_align(m, len); 622 } 623 624 error = uiomove(mtod(m, caddr_t), len, uio); 625 if (error) { 626 m_freem(top); 627 return (error); 628 } 629 630 /* adjust counters */ 631 resid = uio->uio_resid; 632 space -= len; 633 m->m_len = len; 634 top->m_pkthdr.len += len; 635 636 /* Is there more space and more data? */ 637 } while (space > 0 && resid > 0); 638 639 *mp = top; 640 return 0; 641 } 642 643 /* 644 * Following replacement or removal of the first mbuf on the first 645 * mbuf chain of a socket buffer, push necessary state changes back 646 * into the socket buffer so that other consumers see the values 647 * consistently. 'nextrecord' is the callers locally stored value of 648 * the original value of sb->sb_mb->m_nextpkt which must be restored 649 * when the lead mbuf changes. NOTE: 'nextrecord' may be NULL. 650 */ 651 void 652 sbsync(struct sockbuf *sb, struct mbuf *nextrecord) 653 { 654 655 /* 656 * First, update for the new value of nextrecord. If necessary, 657 * make it the first record. 658 */ 659 if (sb->sb_mb != NULL) 660 sb->sb_mb->m_nextpkt = nextrecord; 661 else 662 sb->sb_mb = nextrecord; 663 664 /* 665 * Now update any dependent socket buffer fields to reflect 666 * the new state. This is an inline of SB_EMPTY_FIXUP, with 667 * the addition of a second clause that takes care of the 668 * case where sb_mb has been updated, but remains the last 669 * record. 670 */ 671 if (sb->sb_mb == NULL) { 672 sb->sb_mbtail = NULL; 673 sb->sb_lastrecord = NULL; 674 } else if (sb->sb_mb->m_nextpkt == NULL) 675 sb->sb_lastrecord = sb->sb_mb; 676 } 677 678 /* 679 * Implement receive operations on a socket. 680 * We depend on the way that records are added to the sockbuf 681 * by sbappend*. In particular, each record (mbufs linked through m_next) 682 * must begin with an address if the protocol so specifies, 683 * followed by an optional mbuf or mbufs containing ancillary data, 684 * and then zero or more mbufs of data. 685 * In order to avoid blocking network for the entire time here, we release 686 * the solock() while doing the actual copy to user space. 687 * Although the sockbuf is locked, new data may still be appended, 688 * and thus we must maintain consistency of the sockbuf during that time. 689 * 690 * The caller may receive the data as a single mbuf chain by supplying 691 * an mbuf **mp0 for use in returning the chain. The uio is then used 692 * only for the count in uio_resid. 693 */ 694 int 695 soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio, 696 struct mbuf **mp0, struct mbuf **controlp, int *flagsp, 697 socklen_t controllen) 698 { 699 struct mbuf *m, **mp; 700 struct mbuf *cm; 701 u_long len, offset, moff; 702 int flags, error, s, type, uio_error = 0; 703 const struct protosw *pr = so->so_proto; 704 struct mbuf *nextrecord; 705 size_t resid, orig_resid = uio->uio_resid; 706 707 mp = mp0; 708 if (paddr) 709 *paddr = NULL; 710 if (controlp) 711 *controlp = NULL; 712 if (flagsp) 713 flags = *flagsp &~ MSG_EOR; 714 else 715 flags = 0; 716 if (flags & MSG_OOB) { 717 m = m_get(M_WAIT, MT_DATA); 718 s = solock(so); 719 error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m, 720 (struct mbuf *)(long)(flags & MSG_PEEK), NULL, curproc); 721 sounlock(so, s); 722 if (error) 723 goto bad; 724 do { 725 error = uiomove(mtod(m, caddr_t), 726 ulmin(uio->uio_resid, m->m_len), uio); 727 m = m_free(m); 728 } while (uio->uio_resid && error == 0 && m); 729 bad: 730 m_freem(m); 731 return (error); 732 } 733 if (mp) 734 *mp = NULL; 735 736 s = solock(so); 737 restart: 738 if ((error = sblock(so, &so->so_rcv, SBLOCKWAIT(flags))) != 0) { 739 sounlock(so, s); 740 return (error); 741 } 742 743 m = so->so_rcv.sb_mb; 744 #ifdef SOCKET_SPLICE 745 if (isspliced(so)) 746 m = NULL; 747 #endif /* SOCKET_SPLICE */ 748 /* 749 * If we have less data than requested, block awaiting more 750 * (subject to any timeout) if: 751 * 1. the current count is less than the low water mark, 752 * 2. MSG_WAITALL is set, and it is possible to do the entire 753 * receive operation at once if we block (resid <= hiwat), or 754 * 3. MSG_DONTWAIT is not set. 755 * If MSG_WAITALL is set but resid is larger than the receive buffer, 756 * we have to do the receive in sections, and thus risk returning 757 * a short count if a timeout or signal occurs after we start. 758 */ 759 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && 760 so->so_rcv.sb_cc < uio->uio_resid) && 761 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 762 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && 763 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) { 764 #ifdef DIAGNOSTIC 765 if (m == NULL && so->so_rcv.sb_cc) 766 #ifdef SOCKET_SPLICE 767 if (!isspliced(so)) 768 #endif /* SOCKET_SPLICE */ 769 panic("receive 1: so %p, so_type %d, sb_cc %lu", 770 so, so->so_type, so->so_rcv.sb_cc); 771 #endif 772 if (so->so_error) { 773 if (m) 774 goto dontblock; 775 error = so->so_error; 776 if ((flags & MSG_PEEK) == 0) 777 so->so_error = 0; 778 goto release; 779 } 780 if (so->so_state & SS_CANTRCVMORE) { 781 if (m) 782 goto dontblock; 783 else if (so->so_rcv.sb_cc == 0) 784 goto release; 785 } 786 for (; m; m = m->m_next) 787 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 788 m = so->so_rcv.sb_mb; 789 goto dontblock; 790 } 791 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 792 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 793 error = ENOTCONN; 794 goto release; 795 } 796 if (uio->uio_resid == 0 && controlp == NULL) 797 goto release; 798 if (flags & MSG_DONTWAIT) { 799 error = EWOULDBLOCK; 800 goto release; 801 } 802 SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1"); 803 SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1"); 804 sbunlock(so, &so->so_rcv); 805 error = sbwait(so, &so->so_rcv); 806 if (error) { 807 sounlock(so, s); 808 return (error); 809 } 810 goto restart; 811 } 812 dontblock: 813 /* 814 * On entry here, m points to the first record of the socket buffer. 815 * From this point onward, we maintain 'nextrecord' as a cache of the 816 * pointer to the next record in the socket buffer. We must keep the 817 * various socket buffer pointers and local stack versions of the 818 * pointers in sync, pushing out modifications before operations that 819 * may sleep, and re-reading them afterwards. 820 * 821 * Otherwise, we will race with the network stack appending new data 822 * or records onto the socket buffer by using inconsistent/stale 823 * versions of the field, possibly resulting in socket buffer 824 * corruption. 825 */ 826 if (uio->uio_procp) 827 uio->uio_procp->p_ru.ru_msgrcv++; 828 KASSERT(m == so->so_rcv.sb_mb); 829 SBLASTRECORDCHK(&so->so_rcv, "soreceive 1"); 830 SBLASTMBUFCHK(&so->so_rcv, "soreceive 1"); 831 nextrecord = m->m_nextpkt; 832 if (pr->pr_flags & PR_ADDR) { 833 #ifdef DIAGNOSTIC 834 if (m->m_type != MT_SONAME) 835 panic("receive 1a: so %p, so_type %d, m %p, m_type %d", 836 so, so->so_type, m, m->m_type); 837 #endif 838 orig_resid = 0; 839 if (flags & MSG_PEEK) { 840 if (paddr) 841 *paddr = m_copym(m, 0, m->m_len, M_NOWAIT); 842 m = m->m_next; 843 } else { 844 sbfree(&so->so_rcv, m); 845 if (paddr) { 846 *paddr = m; 847 so->so_rcv.sb_mb = m->m_next; 848 m->m_next = 0; 849 m = so->so_rcv.sb_mb; 850 } else { 851 so->so_rcv.sb_mb = m_free(m); 852 m = so->so_rcv.sb_mb; 853 } 854 sbsync(&so->so_rcv, nextrecord); 855 } 856 } 857 while (m && m->m_type == MT_CONTROL && error == 0) { 858 int skip = 0; 859 if (flags & MSG_PEEK) { 860 if (mtod(m, struct cmsghdr *)->cmsg_type == 861 SCM_RIGHTS) { 862 /* don't leak internalized SCM_RIGHTS msgs */ 863 skip = 1; 864 } else if (controlp) 865 *controlp = m_copym(m, 0, m->m_len, M_NOWAIT); 866 m = m->m_next; 867 } else { 868 sbfree(&so->so_rcv, m); 869 so->so_rcv.sb_mb = m->m_next; 870 m->m_nextpkt = m->m_next = NULL; 871 cm = m; 872 m = so->so_rcv.sb_mb; 873 sbsync(&so->so_rcv, nextrecord); 874 if (controlp) { 875 if (pr->pr_domain->dom_externalize) { 876 error = 877 (*pr->pr_domain->dom_externalize) 878 (cm, controllen, flags); 879 } 880 *controlp = cm; 881 } else { 882 /* 883 * Dispose of any SCM_RIGHTS message that went 884 * through the read path rather than recv. 885 */ 886 if (pr->pr_domain->dom_dispose) 887 pr->pr_domain->dom_dispose(cm); 888 m_free(cm); 889 } 890 } 891 if (m != NULL) 892 nextrecord = so->so_rcv.sb_mb->m_nextpkt; 893 else 894 nextrecord = so->so_rcv.sb_mb; 895 if (controlp && !skip) 896 controlp = &(*controlp)->m_next; 897 orig_resid = 0; 898 } 899 900 /* If m is non-NULL, we have some data to read. */ 901 if (m) { 902 type = m->m_type; 903 if (type == MT_OOBDATA) 904 flags |= MSG_OOB; 905 if (m->m_flags & M_BCAST) 906 flags |= MSG_BCAST; 907 if (m->m_flags & M_MCAST) 908 flags |= MSG_MCAST; 909 } 910 SBLASTRECORDCHK(&so->so_rcv, "soreceive 2"); 911 SBLASTMBUFCHK(&so->so_rcv, "soreceive 2"); 912 913 moff = 0; 914 offset = 0; 915 while (m && uio->uio_resid > 0 && error == 0) { 916 if (m->m_type == MT_OOBDATA) { 917 if (type != MT_OOBDATA) 918 break; 919 } else if (type == MT_OOBDATA) { 920 break; 921 } else if (m->m_type == MT_CONTROL) { 922 /* 923 * If there is more than one control message in the 924 * stream, we do a short read. Next can be received 925 * or disposed by another system call. 926 */ 927 break; 928 #ifdef DIAGNOSTIC 929 } else if (m->m_type != MT_DATA && m->m_type != MT_HEADER) { 930 panic("receive 3: so %p, so_type %d, m %p, m_type %d", 931 so, so->so_type, m, m->m_type); 932 #endif 933 } 934 so->so_state &= ~SS_RCVATMARK; 935 len = uio->uio_resid; 936 if (so->so_oobmark && len > so->so_oobmark - offset) 937 len = so->so_oobmark - offset; 938 if (len > m->m_len - moff) 939 len = m->m_len - moff; 940 /* 941 * If mp is set, just pass back the mbufs. 942 * Otherwise copy them out via the uio, then free. 943 * Sockbuf must be consistent here (points to current mbuf, 944 * it points to next record) when we drop priority; 945 * we must note any additions to the sockbuf when we 946 * block interrupts again. 947 */ 948 if (mp == NULL && uio_error == 0) { 949 SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove"); 950 SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove"); 951 resid = uio->uio_resid; 952 sounlock(so, s); 953 uio_error = uiomove(mtod(m, caddr_t) + moff, len, uio); 954 s = solock(so); 955 if (uio_error) 956 uio->uio_resid = resid - len; 957 } else 958 uio->uio_resid -= len; 959 if (len == m->m_len - moff) { 960 if (m->m_flags & M_EOR) 961 flags |= MSG_EOR; 962 if (flags & MSG_PEEK) { 963 m = m->m_next; 964 moff = 0; 965 orig_resid = 0; 966 } else { 967 nextrecord = m->m_nextpkt; 968 sbfree(&so->so_rcv, m); 969 if (mp) { 970 *mp = m; 971 mp = &m->m_next; 972 so->so_rcv.sb_mb = m = m->m_next; 973 *mp = NULL; 974 } else { 975 so->so_rcv.sb_mb = m_free(m); 976 m = so->so_rcv.sb_mb; 977 } 978 /* 979 * If m != NULL, we also know that 980 * so->so_rcv.sb_mb != NULL. 981 */ 982 KASSERT(so->so_rcv.sb_mb == m); 983 if (m) { 984 m->m_nextpkt = nextrecord; 985 if (nextrecord == NULL) 986 so->so_rcv.sb_lastrecord = m; 987 } else { 988 so->so_rcv.sb_mb = nextrecord; 989 SB_EMPTY_FIXUP(&so->so_rcv); 990 } 991 SBLASTRECORDCHK(&so->so_rcv, "soreceive 3"); 992 SBLASTMBUFCHK(&so->so_rcv, "soreceive 3"); 993 } 994 } else { 995 if (flags & MSG_PEEK) { 996 moff += len; 997 orig_resid = 0; 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 || !timerisvalid(tv))) 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_locked(&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_locked(&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_locked(&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