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