1 /* 2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved. 3 * Copyright (c) 2004 The DragonFly Project. All rights reserved. 4 * 5 * This code is derived from software contributed to The DragonFly Project 6 * by Jeffrey M. Hsu. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of The DragonFly Project nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific, prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1982, 1986, 1988, 1990, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Berkeley and its contributors. 50 * 4. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94 67 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.24 2003/11/11 17:18:18 silby Exp $ 68 * $DragonFly: src/sys/kern/uipc_socket.c,v 1.55 2008/09/02 16:17:52 dillon Exp $ 69 */ 70 71 #include "opt_inet.h" 72 #include "opt_sctp.h" 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/fcntl.h> 77 #include <sys/malloc.h> 78 #include <sys/mbuf.h> 79 #include <sys/domain.h> 80 #include <sys/file.h> /* for struct knote */ 81 #include <sys/kernel.h> 82 #include <sys/malloc.h> 83 #include <sys/event.h> 84 #include <sys/proc.h> 85 #include <sys/protosw.h> 86 #include <sys/socket.h> 87 #include <sys/socketvar.h> 88 #include <sys/socketops.h> 89 #include <sys/resourcevar.h> 90 #include <sys/signalvar.h> 91 #include <sys/sysctl.h> 92 #include <sys/uio.h> 93 #include <sys/jail.h> 94 #include <vm/vm_zone.h> 95 #include <vm/pmap.h> 96 97 #include <sys/thread2.h> 98 #include <sys/socketvar2.h> 99 100 #include <machine/limits.h> 101 102 #ifdef INET 103 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt); 104 #endif /* INET */ 105 106 static void filt_sordetach(struct knote *kn); 107 static int filt_soread(struct knote *kn, long hint); 108 static void filt_sowdetach(struct knote *kn); 109 static int filt_sowrite(struct knote *kn, long hint); 110 static int filt_solisten(struct knote *kn, long hint); 111 112 static struct filterops solisten_filtops = 113 { FILTEROP_ISFD, NULL, filt_sordetach, filt_solisten }; 114 static struct filterops soread_filtops = 115 { FILTEROP_ISFD, NULL, filt_sordetach, filt_soread }; 116 static struct filterops sowrite_filtops = 117 { FILTEROP_ISFD, NULL, filt_sowdetach, filt_sowrite }; 118 static struct filterops soexcept_filtops = 119 { FILTEROP_ISFD, NULL, filt_sordetach, filt_soread }; 120 121 MALLOC_DEFINE(M_SOCKET, "socket", "socket struct"); 122 MALLOC_DEFINE(M_SONAME, "soname", "socket name"); 123 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block"); 124 125 126 static int somaxconn = SOMAXCONN; 127 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW, 128 &somaxconn, 0, "Maximum pending socket connection queue size"); 129 130 /* 131 * Socket operation routines. 132 * These routines are called by the routines in 133 * sys_socket.c or from a system process, and 134 * implement the semantics of socket operations by 135 * switching out to the protocol specific routines. 136 */ 137 138 /* 139 * Get a socket structure, and initialize it. 140 * Note that it would probably be better to allocate socket 141 * and PCB at the same time, but I'm not convinced that all 142 * the protocols can be easily modified to do this. 143 */ 144 struct socket * 145 soalloc(int waitok) 146 { 147 struct socket *so; 148 unsigned waitmask; 149 150 waitmask = waitok ? M_WAITOK : M_NOWAIT; 151 so = kmalloc(sizeof(struct socket), M_SOCKET, M_ZERO|waitmask); 152 if (so) { 153 /* XXX race condition for reentrant kernel */ 154 TAILQ_INIT(&so->so_aiojobq); 155 TAILQ_INIT(&so->so_rcv.ssb_kq.ki_mlist); 156 TAILQ_INIT(&so->so_snd.ssb_kq.ki_mlist); 157 } 158 return so; 159 } 160 161 int 162 socreate(int dom, struct socket **aso, int type, 163 int proto, struct thread *td) 164 { 165 struct proc *p = td->td_proc; 166 struct protosw *prp; 167 struct socket *so; 168 struct pru_attach_info ai; 169 int error; 170 171 if (proto) 172 prp = pffindproto(dom, proto, type); 173 else 174 prp = pffindtype(dom, type); 175 176 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0) 177 return (EPROTONOSUPPORT); 178 179 if (p->p_ucred->cr_prison && jail_socket_unixiproute_only && 180 prp->pr_domain->dom_family != PF_LOCAL && 181 prp->pr_domain->dom_family != PF_INET && 182 prp->pr_domain->dom_family != PF_INET6 && 183 prp->pr_domain->dom_family != PF_ROUTE) { 184 return (EPROTONOSUPPORT); 185 } 186 187 if (prp->pr_type != type) 188 return (EPROTOTYPE); 189 so = soalloc(p != 0); 190 if (so == 0) 191 return (ENOBUFS); 192 193 /* 194 * Set a default port for protocol processing. No action will occur 195 * on the socket on this port until an inpcb is attached to it and 196 * is able to match incoming packets, or until the socket becomes 197 * available to userland. 198 */ 199 so->so_port = cpu0_soport(so, NULL, NULL); 200 201 TAILQ_INIT(&so->so_incomp); 202 TAILQ_INIT(&so->so_comp); 203 so->so_type = type; 204 so->so_cred = crhold(p->p_ucred); 205 so->so_proto = prp; 206 ai.sb_rlimit = &p->p_rlimit[RLIMIT_SBSIZE]; 207 ai.p_ucred = p->p_ucred; 208 ai.fd_rdir = p->p_fd->fd_rdir; 209 210 /* 211 * Auto-sizing of socket buffers is managed by the protocols and 212 * the appropriate flags must be set in the pru_attach function. 213 */ 214 error = so_pru_attach(so, proto, &ai); 215 if (error) { 216 so->so_state |= SS_NOFDREF; 217 sofree(so); 218 return (error); 219 } 220 221 *aso = so; 222 return (0); 223 } 224 225 int 226 sobind(struct socket *so, struct sockaddr *nam, struct thread *td) 227 { 228 int error; 229 230 crit_enter(); 231 error = so_pru_bind(so, nam, td); 232 crit_exit(); 233 return (error); 234 } 235 236 void 237 sodealloc(struct socket *so) 238 { 239 if (so->so_rcv.ssb_hiwat) 240 (void)chgsbsize(so->so_cred->cr_uidinfo, 241 &so->so_rcv.ssb_hiwat, 0, RLIM_INFINITY); 242 if (so->so_snd.ssb_hiwat) 243 (void)chgsbsize(so->so_cred->cr_uidinfo, 244 &so->so_snd.ssb_hiwat, 0, RLIM_INFINITY); 245 #ifdef INET 246 /* remove accept filter if present */ 247 if (so->so_accf != NULL) 248 do_setopt_accept_filter(so, NULL); 249 #endif /* INET */ 250 crfree(so->so_cred); 251 kfree(so, M_SOCKET); 252 } 253 254 int 255 solisten(struct socket *so, int backlog, struct thread *td) 256 { 257 int error; 258 #ifdef SCTP 259 short oldopt, oldqlimit; 260 #endif /* SCTP */ 261 262 crit_enter(); 263 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING)) { 264 crit_exit(); 265 return (EINVAL); 266 } 267 268 #ifdef SCTP 269 oldopt = so->so_options; 270 oldqlimit = so->so_qlimit; 271 #endif /* SCTP */ 272 273 if (TAILQ_EMPTY(&so->so_comp)) 274 so->so_options |= SO_ACCEPTCONN; 275 if (backlog < 0 || backlog > somaxconn) 276 backlog = somaxconn; 277 so->so_qlimit = backlog; 278 /* SCTP needs to look at tweak both the inbound backlog parameter AND 279 * the so_options (UDP model both connect's and gets inbound 280 * connections .. implicitly). 281 */ 282 error = so_pru_listen(so, td); 283 if (error) { 284 #ifdef SCTP 285 /* Restore the params */ 286 so->so_options = oldopt; 287 so->so_qlimit = oldqlimit; 288 #endif /* SCTP */ 289 crit_exit(); 290 return (error); 291 } 292 crit_exit(); 293 return (0); 294 } 295 296 /* 297 * Destroy a disconnected socket. This routine is a NOP if entities 298 * still have a reference on the socket: 299 * 300 * so_pcb - The protocol stack still has a reference 301 * SS_NOFDREF - There is no longer a file pointer reference 302 * SS_ABORTING - An abort netmsg is in-flight 303 */ 304 void 305 sofree(struct socket *so) 306 { 307 struct socket *head = so->so_head; 308 309 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) 310 return; 311 if (so->so_state & SS_ABORTING) 312 return; 313 if (head != NULL) { 314 if (so->so_state & SS_INCOMP) { 315 TAILQ_REMOVE(&head->so_incomp, so, so_list); 316 head->so_incqlen--; 317 } else if (so->so_state & SS_COMP) { 318 /* 319 * We must not decommission a socket that's 320 * on the accept(2) queue. If we do, then 321 * accept(2) may hang after select(2) indicated 322 * that the listening socket was ready. 323 */ 324 return; 325 } else { 326 panic("sofree: not queued"); 327 } 328 so->so_state &= ~SS_INCOMP; 329 so->so_head = NULL; 330 } 331 ssb_release(&so->so_snd, so); 332 sorflush(so); 333 sodealloc(so); 334 } 335 336 /* 337 * Close a socket on last file table reference removal. 338 * Initiate disconnect if connected. 339 * Free socket when disconnect complete. 340 */ 341 int 342 soclose(struct socket *so, int fflag) 343 { 344 int error = 0; 345 346 crit_enter(); 347 funsetown(so->so_sigio); 348 if (so->so_pcb == NULL) 349 goto discard; 350 if (so->so_state & SS_ISCONNECTED) { 351 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 352 error = sodisconnect(so); 353 if (error) 354 goto drop; 355 } 356 if (so->so_options & SO_LINGER) { 357 if ((so->so_state & SS_ISDISCONNECTING) && 358 (fflag & FNONBLOCK)) 359 goto drop; 360 while (so->so_state & SS_ISCONNECTED) { 361 error = tsleep((caddr_t)&so->so_timeo, 362 PCATCH, "soclos", so->so_linger * hz); 363 if (error) 364 break; 365 } 366 } 367 } 368 drop: 369 if (so->so_pcb) { 370 int error2; 371 372 error2 = so_pru_detach(so); 373 if (error == 0) 374 error = error2; 375 } 376 discard: 377 if (so->so_options & SO_ACCEPTCONN) { 378 struct socket *sp; 379 380 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) { 381 TAILQ_REMOVE(&so->so_incomp, sp, so_list); 382 sp->so_state &= ~SS_INCOMP; 383 sp->so_head = NULL; 384 so->so_incqlen--; 385 soaborta(sp); 386 } 387 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) { 388 TAILQ_REMOVE(&so->so_comp, sp, so_list); 389 sp->so_state &= ~SS_COMP; 390 sp->so_head = NULL; 391 so->so_qlen--; 392 soaborta(sp); 393 } 394 } 395 if (so->so_state & SS_NOFDREF) 396 panic("soclose: NOFDREF"); 397 so->so_state |= SS_NOFDREF; 398 sofree(so); 399 crit_exit(); 400 return (error); 401 } 402 403 /* 404 * Abort and destroy a socket. Only one abort can be in progress 405 * at any given moment. 406 */ 407 void 408 soabort(struct socket *so) 409 { 410 if ((so->so_state & SS_ABORTING) == 0) { 411 so->so_state |= SS_ABORTING; 412 so_pru_abort(so); 413 } 414 } 415 416 void 417 soaborta(struct socket *so) 418 { 419 if ((so->so_state & SS_ABORTING) == 0) { 420 so->so_state |= SS_ABORTING; 421 so_pru_aborta(so); 422 } 423 } 424 425 void 426 soabort_oncpu(struct socket *so) 427 { 428 if ((so->so_state & SS_ABORTING) == 0) { 429 so->so_state |= SS_ABORTING; 430 so_pru_abort_oncpu(so); 431 } 432 } 433 434 int 435 soaccept(struct socket *so, struct sockaddr **nam) 436 { 437 int error; 438 439 crit_enter(); 440 if ((so->so_state & SS_NOFDREF) == 0) 441 panic("soaccept: !NOFDREF"); 442 so->so_state &= ~SS_NOFDREF; 443 error = so_pru_accept(so, nam); 444 crit_exit(); 445 return (error); 446 } 447 448 int 449 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td) 450 { 451 int error; 452 453 if (so->so_options & SO_ACCEPTCONN) 454 return (EOPNOTSUPP); 455 crit_enter(); 456 /* 457 * If protocol is connection-based, can only connect once. 458 * Otherwise, if connected, try to disconnect first. 459 * This allows user to disconnect by connecting to, e.g., 460 * a null address. 461 */ 462 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 463 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 464 (error = sodisconnect(so)))) { 465 error = EISCONN; 466 } else { 467 /* 468 * Prevent accumulated error from previous connection 469 * from biting us. 470 */ 471 so->so_error = 0; 472 error = so_pru_connect(so, nam, td); 473 } 474 crit_exit(); 475 return (error); 476 } 477 478 int 479 soconnect2(struct socket *so1, struct socket *so2) 480 { 481 int error; 482 483 crit_enter(); 484 error = so_pru_connect2(so1, so2); 485 crit_exit(); 486 return (error); 487 } 488 489 int 490 sodisconnect(struct socket *so) 491 { 492 int error; 493 494 crit_enter(); 495 if ((so->so_state & SS_ISCONNECTED) == 0) { 496 error = ENOTCONN; 497 goto bad; 498 } 499 if (so->so_state & SS_ISDISCONNECTING) { 500 error = EALREADY; 501 goto bad; 502 } 503 error = so_pru_disconnect(so); 504 bad: 505 crit_exit(); 506 return (error); 507 } 508 509 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 510 /* 511 * Send on a socket. 512 * If send must go all at once and message is larger than 513 * send buffering, then hard error. 514 * Lock against other senders. 515 * If must go all at once and not enough room now, then 516 * inform user that this would block and do nothing. 517 * Otherwise, if nonblocking, send as much as possible. 518 * The data to be sent is described by "uio" if nonzero, 519 * otherwise by the mbuf chain "top" (which must be null 520 * if uio is not). Data provided in mbuf chain must be small 521 * enough to send all at once. 522 * 523 * Returns nonzero on error, timeout or signal; callers 524 * must check for short counts if EINTR/ERESTART are returned. 525 * Data and control buffers are freed on return. 526 */ 527 int 528 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, 529 struct mbuf *top, struct mbuf *control, int flags, 530 struct thread *td) 531 { 532 struct mbuf **mp; 533 struct mbuf *m; 534 size_t resid; 535 int space, len; 536 int clen = 0, error, dontroute, mlen; 537 int atomic = sosendallatonce(so) || top; 538 int pru_flags; 539 540 if (uio) { 541 resid = uio->uio_resid; 542 } else { 543 resid = (size_t)top->m_pkthdr.len; 544 #ifdef INVARIANTS 545 len = 0; 546 for (m = top; m; m = m->m_next) 547 len += m->m_len; 548 KKASSERT(top->m_pkthdr.len == len); 549 #endif 550 } 551 552 /* 553 * WARNING! resid is unsigned, space and len are signed. space 554 * can wind up negative if the sockbuf is overcommitted. 555 * 556 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 557 * type sockets since that's an error. 558 */ 559 if (so->so_type == SOCK_STREAM && (flags & MSG_EOR)) { 560 error = EINVAL; 561 goto out; 562 } 563 564 dontroute = 565 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 566 (so->so_proto->pr_flags & PR_ATOMIC); 567 if (td->td_lwp != NULL) 568 td->td_lwp->lwp_ru.ru_msgsnd++; 569 if (control) 570 clen = control->m_len; 571 #define gotoerr(errcode) { error = errcode; crit_exit(); goto release; } 572 573 restart: 574 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags)); 575 if (error) 576 goto out; 577 578 do { 579 crit_enter(); 580 if (so->so_state & SS_CANTSENDMORE) 581 gotoerr(EPIPE); 582 if (so->so_error) { 583 error = so->so_error; 584 so->so_error = 0; 585 crit_exit(); 586 goto release; 587 } 588 if ((so->so_state & SS_ISCONNECTED) == 0) { 589 /* 590 * `sendto' and `sendmsg' is allowed on a connection- 591 * based socket if it supports implied connect. 592 * Return ENOTCONN if not connected and no address is 593 * supplied. 594 */ 595 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 596 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 597 if ((so->so_state & SS_ISCONFIRMING) == 0 && 598 !(resid == 0 && clen != 0)) 599 gotoerr(ENOTCONN); 600 } else if (addr == 0) 601 gotoerr(so->so_proto->pr_flags & PR_CONNREQUIRED ? 602 ENOTCONN : EDESTADDRREQ); 603 } 604 if ((atomic && resid > so->so_snd.ssb_hiwat) || 605 clen > so->so_snd.ssb_hiwat) { 606 gotoerr(EMSGSIZE); 607 } 608 space = ssb_space(&so->so_snd); 609 if (flags & MSG_OOB) 610 space += 1024; 611 if ((space < 0 || (size_t)space < resid + clen) && uio && 612 (atomic || space < so->so_snd.ssb_lowat || space < clen)) { 613 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) 614 gotoerr(EWOULDBLOCK); 615 ssb_unlock(&so->so_snd); 616 error = ssb_wait(&so->so_snd); 617 crit_exit(); 618 if (error) 619 goto out; 620 goto restart; 621 } 622 crit_exit(); 623 mp = ⊤ 624 space -= clen; 625 do { 626 if (uio == NULL) { 627 /* 628 * Data is prepackaged in "top". 629 */ 630 resid = 0; 631 if (flags & MSG_EOR) 632 top->m_flags |= M_EOR; 633 } else do { 634 if (resid > INT_MAX) 635 resid = INT_MAX; 636 m = m_getl((int)resid, MB_WAIT, MT_DATA, 637 top == NULL ? M_PKTHDR : 0, &mlen); 638 if (top == NULL) { 639 m->m_pkthdr.len = 0; 640 m->m_pkthdr.rcvif = NULL; 641 } 642 len = imin((int)szmin(mlen, resid), space); 643 if (resid < MINCLSIZE) { 644 /* 645 * For datagram protocols, leave room 646 * for protocol headers in first mbuf. 647 */ 648 if (atomic && top == 0 && len < mlen) 649 MH_ALIGN(m, len); 650 } 651 space -= len; 652 error = uiomove(mtod(m, caddr_t), (size_t)len, uio); 653 resid = uio->uio_resid; 654 m->m_len = len; 655 *mp = m; 656 top->m_pkthdr.len += len; 657 if (error) 658 goto release; 659 mp = &m->m_next; 660 if (resid == 0) { 661 if (flags & MSG_EOR) 662 top->m_flags |= M_EOR; 663 break; 664 } 665 } while (space > 0 && atomic); 666 if (dontroute) 667 so->so_options |= SO_DONTROUTE; 668 if (flags & MSG_OOB) { 669 pru_flags = PRUS_OOB; 670 } else if ((flags & MSG_EOF) && 671 (so->so_proto->pr_flags & PR_IMPLOPCL) && 672 (resid == 0)) { 673 /* 674 * If the user set MSG_EOF, the protocol 675 * understands this flag and nothing left to 676 * send then use PRU_SEND_EOF instead of PRU_SEND. 677 */ 678 pru_flags = PRUS_EOF; 679 } else if (resid > 0 && space > 0) { 680 /* If there is more to send, set PRUS_MORETOCOME */ 681 pru_flags = PRUS_MORETOCOME; 682 } else { 683 pru_flags = 0; 684 } 685 crit_enter(); 686 /* 687 * XXX all the SS_CANTSENDMORE checks previously 688 * done could be out of date. We could have recieved 689 * a reset packet in an interrupt or maybe we slept 690 * while doing page faults in uiomove() etc. We could 691 * probably recheck again inside the splnet() protection 692 * here, but there are probably other places that this 693 * also happens. We must rethink this. 694 */ 695 error = so_pru_send(so, pru_flags, top, addr, control, td); 696 crit_exit(); 697 if (dontroute) 698 so->so_options &= ~SO_DONTROUTE; 699 clen = 0; 700 control = 0; 701 top = 0; 702 mp = ⊤ 703 if (error) 704 goto release; 705 } while (resid && space > 0); 706 } while (resid); 707 708 release: 709 ssb_unlock(&so->so_snd); 710 out: 711 if (top) 712 m_freem(top); 713 if (control) 714 m_freem(control); 715 return (error); 716 } 717 718 /* 719 * A specialization of sosend() for UDP based on protocol-specific knowledge: 720 * so->so_proto->pr_flags has the PR_ATOMIC field set. This means that 721 * sosendallatonce() returns true, 722 * the "atomic" variable is true, 723 * and sosendudp() blocks until space is available for the entire send. 724 * so->so_proto->pr_flags does not have the PR_CONNREQUIRED or 725 * PR_IMPLOPCL flags set. 726 * UDP has no out-of-band data. 727 * UDP has no control data. 728 * UDP does not support MSG_EOR. 729 */ 730 int 731 sosendudp(struct socket *so, struct sockaddr *addr, struct uio *uio, 732 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 733 { 734 boolean_t dontroute; /* temporary SO_DONTROUTE setting */ 735 size_t resid; 736 int error; 737 int space; 738 739 if (td->td_lwp != NULL) 740 td->td_lwp->lwp_ru.ru_msgsnd++; 741 if (control) 742 m_freem(control); 743 744 KASSERT((uio && !top) || (top && !uio), ("bad arguments to sosendudp")); 745 resid = uio ? uio->uio_resid : (size_t)top->m_pkthdr.len; 746 747 restart: 748 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags)); 749 if (error) 750 goto out; 751 752 crit_enter(); 753 if (so->so_state & SS_CANTSENDMORE) 754 gotoerr(EPIPE); 755 if (so->so_error) { 756 error = so->so_error; 757 so->so_error = 0; 758 crit_exit(); 759 goto release; 760 } 761 if (!(so->so_state & SS_ISCONNECTED) && addr == NULL) 762 gotoerr(EDESTADDRREQ); 763 if (resid > so->so_snd.ssb_hiwat) 764 gotoerr(EMSGSIZE); 765 space = ssb_space(&so->so_snd); 766 if (uio && (space < 0 || (size_t)space < resid)) { 767 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) 768 gotoerr(EWOULDBLOCK); 769 ssb_unlock(&so->so_snd); 770 error = ssb_wait(&so->so_snd); 771 crit_exit(); 772 if (error) 773 goto out; 774 goto restart; 775 } 776 crit_exit(); 777 778 if (uio) { 779 top = m_uiomove(uio); 780 if (top == NULL) 781 goto release; 782 } 783 784 dontroute = (flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE); 785 if (dontroute) 786 so->so_options |= SO_DONTROUTE; 787 788 error = so_pru_send(so, 0, top, addr, NULL, td); 789 top = NULL; /* sent or freed in lower layer */ 790 791 if (dontroute) 792 so->so_options &= ~SO_DONTROUTE; 793 794 release: 795 ssb_unlock(&so->so_snd); 796 out: 797 if (top) 798 m_freem(top); 799 return (error); 800 } 801 802 /* 803 * Implement receive operations on a socket. 804 * We depend on the way that records are added to the signalsockbuf 805 * by sbappend*. In particular, each record (mbufs linked through m_next) 806 * must begin with an address if the protocol so specifies, 807 * followed by an optional mbuf or mbufs containing ancillary data, 808 * and then zero or more mbufs of data. 809 * In order to avoid blocking network interrupts for the entire time here, 810 * we exit the critical section while doing the actual copy to user space. 811 * Although the signalsockbuf is locked, new data may still be appended, 812 * and thus we must maintain consistency of the signalsockbuf during that time. 813 * 814 * The caller may receive the data as a single mbuf chain by supplying 815 * an mbuf **mp0 for use in returning the chain. The uio is then used 816 * only for the count in uio_resid. 817 */ 818 int 819 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio, 820 struct sockbuf *sio, struct mbuf **controlp, int *flagsp) 821 { 822 struct mbuf *m, *n; 823 struct mbuf *free_chain = NULL; 824 int flags, len, error, offset; 825 struct protosw *pr = so->so_proto; 826 int moff, type = 0; 827 size_t resid, orig_resid; 828 829 if (uio) 830 resid = uio->uio_resid; 831 else 832 resid = (size_t)(sio->sb_climit - sio->sb_cc); 833 orig_resid = resid; 834 835 if (psa) 836 *psa = NULL; 837 if (controlp) 838 *controlp = NULL; 839 if (flagsp) 840 flags = *flagsp &~ MSG_EOR; 841 else 842 flags = 0; 843 if (flags & MSG_OOB) { 844 m = m_get(MB_WAIT, MT_DATA); 845 if (m == NULL) 846 return (ENOBUFS); 847 error = so_pru_rcvoob(so, m, flags & MSG_PEEK); 848 if (error) 849 goto bad; 850 if (sio) { 851 do { 852 sbappend(sio, m); 853 KKASSERT(resid >= (size_t)m->m_len); 854 resid -= (size_t)m->m_len; 855 } while (resid > 0 && m); 856 } else { 857 do { 858 uio->uio_resid = resid; 859 error = uiomove(mtod(m, caddr_t), 860 (int)szmin(resid, m->m_len), 861 uio); 862 resid = uio->uio_resid; 863 m = m_free(m); 864 } while (uio->uio_resid && error == 0 && m); 865 } 866 bad: 867 if (m) 868 m_freem(m); 869 return (error); 870 } 871 if ((so->so_state & SS_ISCONFIRMING) && resid) 872 so_pru_rcvd(so, 0); 873 874 restart: 875 crit_enter(); 876 error = ssb_lock(&so->so_rcv, SBLOCKWAIT(flags)); 877 if (error) 878 goto done; 879 880 m = so->so_rcv.ssb_mb; 881 /* 882 * If we have less data than requested, block awaiting more 883 * (subject to any timeout) if: 884 * 1. the current count is less than the low water mark, or 885 * 2. MSG_WAITALL is set, and it is possible to do the entire 886 * receive operation at once if we block (resid <= hiwat). 887 * 3. MSG_DONTWAIT is not set 888 * If MSG_WAITALL is set but resid is larger than the receive buffer, 889 * we have to do the receive in sections, and thus risk returning 890 * a short count if a timeout or signal occurs after we start. 891 */ 892 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && 893 (size_t)so->so_rcv.ssb_cc < resid) && 894 (so->so_rcv.ssb_cc < so->so_rcv.ssb_lowat || 895 ((flags & MSG_WAITALL) && resid <= (size_t)so->so_rcv.ssb_hiwat)) && 896 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) { 897 KASSERT(m != NULL || !so->so_rcv.ssb_cc, ("receive 1")); 898 if (so->so_error) { 899 if (m) 900 goto dontblock; 901 error = so->so_error; 902 if ((flags & MSG_PEEK) == 0) 903 so->so_error = 0; 904 goto release; 905 } 906 if (so->so_state & SS_CANTRCVMORE) { 907 if (m) 908 goto dontblock; 909 else 910 goto release; 911 } 912 for (; m; m = m->m_next) { 913 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 914 m = so->so_rcv.ssb_mb; 915 goto dontblock; 916 } 917 } 918 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 919 (pr->pr_flags & PR_CONNREQUIRED)) { 920 error = ENOTCONN; 921 goto release; 922 } 923 if (resid == 0) 924 goto release; 925 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) { 926 error = EWOULDBLOCK; 927 goto release; 928 } 929 ssb_unlock(&so->so_rcv); 930 error = ssb_wait(&so->so_rcv); 931 if (error) 932 goto done; 933 crit_exit(); 934 goto restart; 935 } 936 dontblock: 937 if (uio && uio->uio_td && uio->uio_td->td_proc) 938 uio->uio_td->td_lwp->lwp_ru.ru_msgrcv++; 939 940 /* 941 * note: m should be == sb_mb here. Cache the next record while 942 * cleaning up. Note that calling m_free*() will break out critical 943 * section. 944 */ 945 KKASSERT(m == so->so_rcv.ssb_mb); 946 947 /* 948 * Skip any address mbufs prepending the record. 949 */ 950 if (pr->pr_flags & PR_ADDR) { 951 KASSERT(m->m_type == MT_SONAME, ("receive 1a")); 952 orig_resid = 0; 953 if (psa) 954 *psa = dup_sockaddr(mtod(m, struct sockaddr *)); 955 if (flags & MSG_PEEK) 956 m = m->m_next; 957 else 958 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain); 959 } 960 961 /* 962 * Skip any control mbufs prepending the record. 963 */ 964 #ifdef SCTP 965 if (pr->pr_flags & PR_ADDR_OPT) { 966 /* 967 * For SCTP we may be getting a 968 * whole message OR a partial delivery. 969 */ 970 if (m && m->m_type == MT_SONAME) { 971 orig_resid = 0; 972 if (psa) 973 *psa = dup_sockaddr(mtod(m, struct sockaddr *)); 974 if (flags & MSG_PEEK) 975 m = m->m_next; 976 else 977 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain); 978 } 979 } 980 #endif /* SCTP */ 981 while (m && m->m_type == MT_CONTROL && error == 0) { 982 if (flags & MSG_PEEK) { 983 if (controlp) 984 *controlp = m_copy(m, 0, m->m_len); 985 m = m->m_next; /* XXX race */ 986 } else { 987 if (controlp) { 988 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL); 989 if (pr->pr_domain->dom_externalize && 990 mtod(m, struct cmsghdr *)->cmsg_type == 991 SCM_RIGHTS) 992 error = (*pr->pr_domain->dom_externalize)(m); 993 *controlp = m; 994 m = n; 995 } else { 996 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain); 997 } 998 } 999 if (controlp && *controlp) { 1000 orig_resid = 0; 1001 controlp = &(*controlp)->m_next; 1002 } 1003 } 1004 1005 /* 1006 * flag OOB data. 1007 */ 1008 if (m) { 1009 type = m->m_type; 1010 if (type == MT_OOBDATA) 1011 flags |= MSG_OOB; 1012 } 1013 1014 /* 1015 * Copy to the UIO or mbuf return chain (*mp). 1016 */ 1017 moff = 0; 1018 offset = 0; 1019 while (m && resid > 0 && error == 0) { 1020 if (m->m_type == MT_OOBDATA) { 1021 if (type != MT_OOBDATA) 1022 break; 1023 } else if (type == MT_OOBDATA) 1024 break; 1025 else 1026 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER, 1027 ("receive 3")); 1028 so->so_state &= ~SS_RCVATMARK; 1029 len = (resid > INT_MAX) ? INT_MAX : resid; 1030 if (so->so_oobmark && len > so->so_oobmark - offset) 1031 len = so->so_oobmark - offset; 1032 if (len > m->m_len - moff) 1033 len = m->m_len - moff; 1034 1035 /* 1036 * Copy out to the UIO or pass the mbufs back to the SIO. 1037 * The SIO is dealt with when we eat the mbuf, but deal 1038 * with the resid here either way. 1039 */ 1040 if (uio) { 1041 crit_exit(); 1042 uio->uio_resid = resid; 1043 error = uiomove(mtod(m, caddr_t) + moff, len, uio); 1044 resid = uio->uio_resid; 1045 crit_enter(); 1046 if (error) 1047 goto release; 1048 } else { 1049 resid -= (size_t)len; 1050 } 1051 1052 /* 1053 * Eat the entire mbuf or just a piece of it 1054 */ 1055 if (len == m->m_len - moff) { 1056 if (m->m_flags & M_EOR) 1057 flags |= MSG_EOR; 1058 #ifdef SCTP 1059 if (m->m_flags & M_NOTIFICATION) 1060 flags |= MSG_NOTIFICATION; 1061 #endif /* SCTP */ 1062 if (flags & MSG_PEEK) { 1063 m = m->m_next; 1064 moff = 0; 1065 } else { 1066 if (sio) { 1067 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL); 1068 sbappend(sio, m); 1069 m = n; 1070 } else { 1071 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain); 1072 } 1073 } 1074 } else { 1075 if (flags & MSG_PEEK) { 1076 moff += len; 1077 } else { 1078 if (sio) { 1079 n = m_copym(m, 0, len, MB_WAIT); 1080 if (n) 1081 sbappend(sio, n); 1082 } 1083 m->m_data += len; 1084 m->m_len -= len; 1085 so->so_rcv.ssb_cc -= len; 1086 } 1087 } 1088 if (so->so_oobmark) { 1089 if ((flags & MSG_PEEK) == 0) { 1090 so->so_oobmark -= len; 1091 if (so->so_oobmark == 0) { 1092 so->so_state |= SS_RCVATMARK; 1093 break; 1094 } 1095 } else { 1096 offset += len; 1097 if (offset == so->so_oobmark) 1098 break; 1099 } 1100 } 1101 if (flags & MSG_EOR) 1102 break; 1103 /* 1104 * If the MSG_WAITALL flag is set (for non-atomic socket), 1105 * we must not quit until resid == 0 or an error 1106 * termination. If a signal/timeout occurs, return 1107 * with a short count but without error. 1108 * Keep signalsockbuf locked against other readers. 1109 */ 1110 while ((flags & MSG_WAITALL) && m == NULL && 1111 resid > 0 && !sosendallatonce(so) && 1112 so->so_rcv.ssb_mb == NULL) { 1113 if (so->so_error || so->so_state & SS_CANTRCVMORE) 1114 break; 1115 /* 1116 * The window might have closed to zero, make 1117 * sure we send an ack now that we've drained 1118 * the buffer or we might end up blocking until 1119 * the idle takes over (5 seconds). 1120 */ 1121 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) 1122 so_pru_rcvd(so, flags); 1123 error = ssb_wait(&so->so_rcv); 1124 if (error) { 1125 ssb_unlock(&so->so_rcv); 1126 error = 0; 1127 goto done; 1128 } 1129 m = so->so_rcv.ssb_mb; 1130 } 1131 } 1132 1133 /* 1134 * If an atomic read was requested but unread data still remains 1135 * in the record, set MSG_TRUNC. 1136 */ 1137 if (m && pr->pr_flags & PR_ATOMIC) 1138 flags |= MSG_TRUNC; 1139 1140 /* 1141 * Cleanup. If an atomic read was requested drop any unread data. 1142 */ 1143 if ((flags & MSG_PEEK) == 0) { 1144 if (m && (pr->pr_flags & PR_ATOMIC)) 1145 sbdroprecord(&so->so_rcv.sb); 1146 if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb) 1147 so_pru_rcvd(so, flags); 1148 } 1149 1150 if (orig_resid == resid && orig_resid && 1151 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { 1152 ssb_unlock(&so->so_rcv); 1153 crit_exit(); 1154 goto restart; 1155 } 1156 1157 if (flagsp) 1158 *flagsp |= flags; 1159 release: 1160 ssb_unlock(&so->so_rcv); 1161 done: 1162 crit_exit(); 1163 if (free_chain) 1164 m_freem(free_chain); 1165 return (error); 1166 } 1167 1168 int 1169 soshutdown(struct socket *so, int how) 1170 { 1171 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) 1172 return (EINVAL); 1173 1174 if (how != SHUT_WR) 1175 sorflush(so); 1176 if (how != SHUT_RD) 1177 return (so_pru_shutdown(so)); 1178 return (0); 1179 } 1180 1181 void 1182 sorflush(struct socket *so) 1183 { 1184 struct signalsockbuf *ssb = &so->so_rcv; 1185 struct protosw *pr = so->so_proto; 1186 struct signalsockbuf asb; 1187 1188 atomic_set_int(&ssb->ssb_flags, SSB_NOINTR); 1189 1190 ssb_lock(ssb, M_WAITOK); 1191 socantrcvmore(so); 1192 asb = *ssb; 1193 1194 /* 1195 * Can't just blow up the ssb structure here 1196 */ 1197 ssb->ssb_timeo = 0; 1198 ssb->ssb_unused01 = 0; 1199 ssb->ssb_lowat = 0; 1200 ssb->ssb_hiwat = 0; 1201 ssb->ssb_mbmax = 0; 1202 atomic_clear_int(&ssb->ssb_flags, SSB_CLEAR_MASK); 1203 1204 ssb_unlock(ssb); 1205 1206 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) 1207 (*pr->pr_domain->dom_dispose)(asb.ssb_mb); 1208 ssb_release(&asb, so); 1209 } 1210 1211 #ifdef INET 1212 static int 1213 do_setopt_accept_filter(struct socket *so, struct sockopt *sopt) 1214 { 1215 struct accept_filter_arg *afap = NULL; 1216 struct accept_filter *afp; 1217 struct so_accf *af = so->so_accf; 1218 int error = 0; 1219 1220 /* do not set/remove accept filters on non listen sockets */ 1221 if ((so->so_options & SO_ACCEPTCONN) == 0) { 1222 error = EINVAL; 1223 goto out; 1224 } 1225 1226 /* removing the filter */ 1227 if (sopt == NULL) { 1228 if (af != NULL) { 1229 if (af->so_accept_filter != NULL && 1230 af->so_accept_filter->accf_destroy != NULL) { 1231 af->so_accept_filter->accf_destroy(so); 1232 } 1233 if (af->so_accept_filter_str != NULL) { 1234 FREE(af->so_accept_filter_str, M_ACCF); 1235 } 1236 FREE(af, M_ACCF); 1237 so->so_accf = NULL; 1238 } 1239 so->so_options &= ~SO_ACCEPTFILTER; 1240 return (0); 1241 } 1242 /* adding a filter */ 1243 /* must remove previous filter first */ 1244 if (af != NULL) { 1245 error = EINVAL; 1246 goto out; 1247 } 1248 /* don't put large objects on the kernel stack */ 1249 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK); 1250 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap); 1251 afap->af_name[sizeof(afap->af_name)-1] = '\0'; 1252 afap->af_arg[sizeof(afap->af_arg)-1] = '\0'; 1253 if (error) 1254 goto out; 1255 afp = accept_filt_get(afap->af_name); 1256 if (afp == NULL) { 1257 error = ENOENT; 1258 goto out; 1259 } 1260 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK | M_ZERO); 1261 if (afp->accf_create != NULL) { 1262 if (afap->af_name[0] != '\0') { 1263 int len = strlen(afap->af_name) + 1; 1264 1265 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK); 1266 strcpy(af->so_accept_filter_str, afap->af_name); 1267 } 1268 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg); 1269 if (af->so_accept_filter_arg == NULL) { 1270 FREE(af->so_accept_filter_str, M_ACCF); 1271 FREE(af, M_ACCF); 1272 so->so_accf = NULL; 1273 error = EINVAL; 1274 goto out; 1275 } 1276 } 1277 af->so_accept_filter = afp; 1278 so->so_accf = af; 1279 so->so_options |= SO_ACCEPTFILTER; 1280 out: 1281 if (afap != NULL) 1282 FREE(afap, M_TEMP); 1283 return (error); 1284 } 1285 #endif /* INET */ 1286 1287 /* 1288 * Perhaps this routine, and sooptcopyout(), below, ought to come in 1289 * an additional variant to handle the case where the option value needs 1290 * to be some kind of integer, but not a specific size. 1291 * In addition to their use here, these functions are also called by the 1292 * protocol-level pr_ctloutput() routines. 1293 */ 1294 int 1295 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen) 1296 { 1297 return soopt_to_kbuf(sopt, buf, len, minlen); 1298 } 1299 1300 int 1301 soopt_to_kbuf(struct sockopt *sopt, void *buf, size_t len, size_t minlen) 1302 { 1303 size_t valsize; 1304 1305 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val)); 1306 KKASSERT(kva_p(buf)); 1307 1308 /* 1309 * If the user gives us more than we wanted, we ignore it, 1310 * but if we don't get the minimum length the caller 1311 * wants, we return EINVAL. On success, sopt->sopt_valsize 1312 * is set to however much we actually retrieved. 1313 */ 1314 if ((valsize = sopt->sopt_valsize) < minlen) 1315 return EINVAL; 1316 if (valsize > len) 1317 sopt->sopt_valsize = valsize = len; 1318 1319 bcopy(sopt->sopt_val, buf, valsize); 1320 return 0; 1321 } 1322 1323 1324 int 1325 sosetopt(struct socket *so, struct sockopt *sopt) 1326 { 1327 int error, optval; 1328 struct linger l; 1329 struct timeval tv; 1330 u_long val; 1331 struct signalsockbuf *sotmp; 1332 1333 error = 0; 1334 sopt->sopt_dir = SOPT_SET; 1335 if (sopt->sopt_level != SOL_SOCKET) { 1336 if (so->so_proto && so->so_proto->pr_ctloutput) { 1337 return (so_pru_ctloutput(so, sopt)); 1338 } 1339 error = ENOPROTOOPT; 1340 } else { 1341 switch (sopt->sopt_name) { 1342 #ifdef INET 1343 case SO_ACCEPTFILTER: 1344 error = do_setopt_accept_filter(so, sopt); 1345 if (error) 1346 goto bad; 1347 break; 1348 #endif /* INET */ 1349 case SO_LINGER: 1350 error = sooptcopyin(sopt, &l, sizeof l, sizeof l); 1351 if (error) 1352 goto bad; 1353 1354 so->so_linger = l.l_linger; 1355 if (l.l_onoff) 1356 so->so_options |= SO_LINGER; 1357 else 1358 so->so_options &= ~SO_LINGER; 1359 break; 1360 1361 case SO_DEBUG: 1362 case SO_KEEPALIVE: 1363 case SO_DONTROUTE: 1364 case SO_USELOOPBACK: 1365 case SO_BROADCAST: 1366 case SO_REUSEADDR: 1367 case SO_REUSEPORT: 1368 case SO_OOBINLINE: 1369 case SO_TIMESTAMP: 1370 error = sooptcopyin(sopt, &optval, sizeof optval, 1371 sizeof optval); 1372 if (error) 1373 goto bad; 1374 if (optval) 1375 so->so_options |= sopt->sopt_name; 1376 else 1377 so->so_options &= ~sopt->sopt_name; 1378 break; 1379 1380 case SO_SNDBUF: 1381 case SO_RCVBUF: 1382 case SO_SNDLOWAT: 1383 case SO_RCVLOWAT: 1384 error = sooptcopyin(sopt, &optval, sizeof optval, 1385 sizeof optval); 1386 if (error) 1387 goto bad; 1388 1389 /* 1390 * Values < 1 make no sense for any of these 1391 * options, so disallow them. 1392 */ 1393 if (optval < 1) { 1394 error = EINVAL; 1395 goto bad; 1396 } 1397 1398 switch (sopt->sopt_name) { 1399 case SO_SNDBUF: 1400 case SO_RCVBUF: 1401 if (ssb_reserve(sopt->sopt_name == SO_SNDBUF ? 1402 &so->so_snd : &so->so_rcv, (u_long)optval, 1403 so, 1404 &curproc->p_rlimit[RLIMIT_SBSIZE]) == 0) { 1405 error = ENOBUFS; 1406 goto bad; 1407 } 1408 sotmp = (sopt->sopt_name == SO_SNDBUF) ? 1409 &so->so_snd : &so->so_rcv; 1410 atomic_clear_int(&sotmp->ssb_flags, 1411 SSB_AUTOSIZE); 1412 break; 1413 1414 /* 1415 * Make sure the low-water is never greater than 1416 * the high-water. 1417 */ 1418 case SO_SNDLOWAT: 1419 so->so_snd.ssb_lowat = 1420 (optval > so->so_snd.ssb_hiwat) ? 1421 so->so_snd.ssb_hiwat : optval; 1422 atomic_clear_int(&so->so_snd.ssb_flags, 1423 SSB_AUTOLOWAT); 1424 break; 1425 case SO_RCVLOWAT: 1426 so->so_rcv.ssb_lowat = 1427 (optval > so->so_rcv.ssb_hiwat) ? 1428 so->so_rcv.ssb_hiwat : optval; 1429 atomic_clear_int(&so->so_rcv.ssb_flags, 1430 SSB_AUTOLOWAT); 1431 break; 1432 } 1433 break; 1434 1435 case SO_SNDTIMEO: 1436 case SO_RCVTIMEO: 1437 error = sooptcopyin(sopt, &tv, sizeof tv, 1438 sizeof tv); 1439 if (error) 1440 goto bad; 1441 1442 /* assert(hz > 0); */ 1443 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz || 1444 tv.tv_usec < 0 || tv.tv_usec >= 1000000) { 1445 error = EDOM; 1446 goto bad; 1447 } 1448 /* assert(tick > 0); */ 1449 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */ 1450 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / ustick; 1451 if (val > SHRT_MAX) { 1452 error = EDOM; 1453 goto bad; 1454 } 1455 if (val == 0 && tv.tv_usec != 0) 1456 val = 1; 1457 1458 switch (sopt->sopt_name) { 1459 case SO_SNDTIMEO: 1460 so->so_snd.ssb_timeo = val; 1461 break; 1462 case SO_RCVTIMEO: 1463 so->so_rcv.ssb_timeo = val; 1464 break; 1465 } 1466 break; 1467 default: 1468 error = ENOPROTOOPT; 1469 break; 1470 } 1471 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) { 1472 (void) so_pru_ctloutput(so, sopt); 1473 } 1474 } 1475 bad: 1476 return (error); 1477 } 1478 1479 /* Helper routine for getsockopt */ 1480 int 1481 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len) 1482 { 1483 soopt_from_kbuf(sopt, buf, len); 1484 return 0; 1485 } 1486 1487 void 1488 soopt_from_kbuf(struct sockopt *sopt, const void *buf, size_t len) 1489 { 1490 size_t valsize; 1491 1492 if (len == 0) { 1493 sopt->sopt_valsize = 0; 1494 return; 1495 } 1496 1497 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val)); 1498 KKASSERT(kva_p(buf)); 1499 1500 /* 1501 * Documented get behavior is that we always return a value, 1502 * possibly truncated to fit in the user's buffer. 1503 * Traditional behavior is that we always tell the user 1504 * precisely how much we copied, rather than something useful 1505 * like the total amount we had available for her. 1506 * Note that this interface is not idempotent; the entire answer must 1507 * generated ahead of time. 1508 */ 1509 valsize = szmin(len, sopt->sopt_valsize); 1510 sopt->sopt_valsize = valsize; 1511 if (sopt->sopt_val != 0) { 1512 bcopy(buf, sopt->sopt_val, valsize); 1513 } 1514 } 1515 1516 int 1517 sogetopt(struct socket *so, struct sockopt *sopt) 1518 { 1519 int error, optval; 1520 struct linger l; 1521 struct timeval tv; 1522 #ifdef INET 1523 struct accept_filter_arg *afap; 1524 #endif 1525 1526 error = 0; 1527 sopt->sopt_dir = SOPT_GET; 1528 if (sopt->sopt_level != SOL_SOCKET) { 1529 if (so->so_proto && so->so_proto->pr_ctloutput) { 1530 return (so_pru_ctloutput(so, sopt)); 1531 } else 1532 return (ENOPROTOOPT); 1533 } else { 1534 switch (sopt->sopt_name) { 1535 #ifdef INET 1536 case SO_ACCEPTFILTER: 1537 if ((so->so_options & SO_ACCEPTCONN) == 0) 1538 return (EINVAL); 1539 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), 1540 M_TEMP, M_WAITOK | M_ZERO); 1541 if ((so->so_options & SO_ACCEPTFILTER) != 0) { 1542 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name); 1543 if (so->so_accf->so_accept_filter_str != NULL) 1544 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str); 1545 } 1546 error = sooptcopyout(sopt, afap, sizeof(*afap)); 1547 FREE(afap, M_TEMP); 1548 break; 1549 #endif /* INET */ 1550 1551 case SO_LINGER: 1552 l.l_onoff = so->so_options & SO_LINGER; 1553 l.l_linger = so->so_linger; 1554 error = sooptcopyout(sopt, &l, sizeof l); 1555 break; 1556 1557 case SO_USELOOPBACK: 1558 case SO_DONTROUTE: 1559 case SO_DEBUG: 1560 case SO_KEEPALIVE: 1561 case SO_REUSEADDR: 1562 case SO_REUSEPORT: 1563 case SO_BROADCAST: 1564 case SO_OOBINLINE: 1565 case SO_TIMESTAMP: 1566 optval = so->so_options & sopt->sopt_name; 1567 integer: 1568 error = sooptcopyout(sopt, &optval, sizeof optval); 1569 break; 1570 1571 case SO_TYPE: 1572 optval = so->so_type; 1573 goto integer; 1574 1575 case SO_ERROR: 1576 optval = so->so_error; 1577 so->so_error = 0; 1578 goto integer; 1579 1580 case SO_SNDBUF: 1581 optval = so->so_snd.ssb_hiwat; 1582 goto integer; 1583 1584 case SO_RCVBUF: 1585 optval = so->so_rcv.ssb_hiwat; 1586 goto integer; 1587 1588 case SO_SNDLOWAT: 1589 optval = so->so_snd.ssb_lowat; 1590 goto integer; 1591 1592 case SO_RCVLOWAT: 1593 optval = so->so_rcv.ssb_lowat; 1594 goto integer; 1595 1596 case SO_SNDTIMEO: 1597 case SO_RCVTIMEO: 1598 optval = (sopt->sopt_name == SO_SNDTIMEO ? 1599 so->so_snd.ssb_timeo : so->so_rcv.ssb_timeo); 1600 1601 tv.tv_sec = optval / hz; 1602 tv.tv_usec = (optval % hz) * ustick; 1603 error = sooptcopyout(sopt, &tv, sizeof tv); 1604 break; 1605 1606 default: 1607 error = ENOPROTOOPT; 1608 break; 1609 } 1610 return (error); 1611 } 1612 } 1613 1614 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 1615 int 1616 soopt_getm(struct sockopt *sopt, struct mbuf **mp) 1617 { 1618 struct mbuf *m, *m_prev; 1619 int sopt_size = sopt->sopt_valsize, msize; 1620 1621 m = m_getl(sopt_size, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_DATA, 1622 0, &msize); 1623 if (m == NULL) 1624 return (ENOBUFS); 1625 m->m_len = min(msize, sopt_size); 1626 sopt_size -= m->m_len; 1627 *mp = m; 1628 m_prev = m; 1629 1630 while (sopt_size > 0) { 1631 m = m_getl(sopt_size, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, 1632 MT_DATA, 0, &msize); 1633 if (m == NULL) { 1634 m_freem(*mp); 1635 return (ENOBUFS); 1636 } 1637 m->m_len = min(msize, sopt_size); 1638 sopt_size -= m->m_len; 1639 m_prev->m_next = m; 1640 m_prev = m; 1641 } 1642 return (0); 1643 } 1644 1645 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */ 1646 int 1647 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m) 1648 { 1649 soopt_to_mbuf(sopt, m); 1650 return 0; 1651 } 1652 1653 void 1654 soopt_to_mbuf(struct sockopt *sopt, struct mbuf *m) 1655 { 1656 size_t valsize; 1657 void *val; 1658 1659 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val)); 1660 KKASSERT(kva_p(m)); 1661 if (sopt->sopt_val == NULL) 1662 return; 1663 val = sopt->sopt_val; 1664 valsize = sopt->sopt_valsize; 1665 while (m != NULL && valsize >= m->m_len) { 1666 bcopy(val, mtod(m, char *), m->m_len); 1667 valsize -= m->m_len; 1668 val = (caddr_t)val + m->m_len; 1669 m = m->m_next; 1670 } 1671 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */ 1672 panic("ip6_sooptmcopyin"); 1673 } 1674 1675 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */ 1676 int 1677 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m) 1678 { 1679 return soopt_from_mbuf(sopt, m); 1680 } 1681 1682 int 1683 soopt_from_mbuf(struct sockopt *sopt, struct mbuf *m) 1684 { 1685 struct mbuf *m0 = m; 1686 size_t valsize = 0; 1687 size_t maxsize; 1688 void *val; 1689 1690 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val)); 1691 KKASSERT(kva_p(m)); 1692 if (sopt->sopt_val == NULL) 1693 return 0; 1694 val = sopt->sopt_val; 1695 maxsize = sopt->sopt_valsize; 1696 while (m != NULL && maxsize >= m->m_len) { 1697 bcopy(mtod(m, char *), val, m->m_len); 1698 maxsize -= m->m_len; 1699 val = (caddr_t)val + m->m_len; 1700 valsize += m->m_len; 1701 m = m->m_next; 1702 } 1703 if (m != NULL) { 1704 /* enough soopt buffer should be given from user-land */ 1705 m_freem(m0); 1706 return (EINVAL); 1707 } 1708 sopt->sopt_valsize = valsize; 1709 return 0; 1710 } 1711 1712 void 1713 sohasoutofband(struct socket *so) 1714 { 1715 if (so->so_sigio != NULL) 1716 pgsigio(so->so_sigio, SIGURG, 0); 1717 KNOTE(&so->so_rcv.ssb_kq.ki_note, NOTE_OOB); 1718 } 1719 1720 int 1721 sokqfilter(struct file *fp, struct knote *kn) 1722 { 1723 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1724 struct signalsockbuf *ssb; 1725 1726 switch (kn->kn_filter) { 1727 case EVFILT_READ: 1728 if (so->so_options & SO_ACCEPTCONN) 1729 kn->kn_fop = &solisten_filtops; 1730 else 1731 kn->kn_fop = &soread_filtops; 1732 ssb = &so->so_rcv; 1733 break; 1734 case EVFILT_WRITE: 1735 kn->kn_fop = &sowrite_filtops; 1736 ssb = &so->so_snd; 1737 break; 1738 case EVFILT_EXCEPT: 1739 kn->kn_fop = &soexcept_filtops; 1740 ssb = &so->so_rcv; 1741 break; 1742 default: 1743 return (EOPNOTSUPP); 1744 } 1745 1746 knote_insert(&ssb->ssb_kq.ki_note, kn); 1747 atomic_set_int(&ssb->ssb_flags, SSB_KNOTE); 1748 return (0); 1749 } 1750 1751 static void 1752 filt_sordetach(struct knote *kn) 1753 { 1754 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1755 1756 knote_remove(&so->so_rcv.ssb_kq.ki_note, kn); 1757 if (SLIST_EMPTY(&so->so_rcv.ssb_kq.ki_note)) 1758 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_KNOTE); 1759 } 1760 1761 /*ARGSUSED*/ 1762 static int 1763 filt_soread(struct knote *kn, long hint) 1764 { 1765 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1766 1767 if (kn->kn_sfflags & NOTE_OOB) { 1768 if ((so->so_oobmark || (so->so_state & SS_RCVATMARK))) { 1769 kn->kn_fflags |= NOTE_OOB; 1770 return (1); 1771 } 1772 return (0); 1773 } 1774 kn->kn_data = so->so_rcv.ssb_cc; 1775 1776 /* 1777 * Only set EOF if all data has been exhausted. 1778 */ 1779 if ((so->so_state & SS_CANTRCVMORE) && kn->kn_data == 0) { 1780 kn->kn_flags |= EV_EOF; 1781 kn->kn_fflags = so->so_error; 1782 return (1); 1783 } 1784 if (so->so_error) /* temporary udp error */ 1785 return (1); 1786 if (kn->kn_sfflags & NOTE_LOWAT) 1787 return (kn->kn_data >= kn->kn_sdata); 1788 return ((kn->kn_data >= so->so_rcv.ssb_lowat) || 1789 !TAILQ_EMPTY(&so->so_comp)); 1790 } 1791 1792 static void 1793 filt_sowdetach(struct knote *kn) 1794 { 1795 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1796 1797 knote_remove(&so->so_snd.ssb_kq.ki_note, kn); 1798 if (SLIST_EMPTY(&so->so_snd.ssb_kq.ki_note)) 1799 atomic_clear_int(&so->so_snd.ssb_flags, SSB_KNOTE); 1800 } 1801 1802 /*ARGSUSED*/ 1803 static int 1804 filt_sowrite(struct knote *kn, long hint) 1805 { 1806 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1807 1808 kn->kn_data = ssb_space(&so->so_snd); 1809 if (so->so_state & SS_CANTSENDMORE) { 1810 kn->kn_flags |= EV_EOF; 1811 kn->kn_fflags = so->so_error; 1812 return (1); 1813 } 1814 if (so->so_error) /* temporary udp error */ 1815 return (1); 1816 if (((so->so_state & SS_ISCONNECTED) == 0) && 1817 (so->so_proto->pr_flags & PR_CONNREQUIRED)) 1818 return (0); 1819 if (kn->kn_sfflags & NOTE_LOWAT) 1820 return (kn->kn_data >= kn->kn_sdata); 1821 return (kn->kn_data >= so->so_snd.ssb_lowat); 1822 } 1823 1824 /*ARGSUSED*/ 1825 static int 1826 filt_solisten(struct knote *kn, long hint) 1827 { 1828 struct socket *so = (struct socket *)kn->kn_fp->f_data; 1829 1830 kn->kn_data = so->so_qlen; 1831 return (! TAILQ_EMPTY(&so->so_comp)); 1832 } 1833