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