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