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