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