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