uipc_socket.c revision 86305
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 86305 2001-11-12 20:50:06Z keramida $ 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 = crhold(td->td_proc->p_ucred); 164 so->so_proto = prp; 165 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td); 166 if (error) { 167 so->so_state |= SS_NOFDREF; 168 sofree(so); 169 return (error); 170 } 171 *aso = so; 172 return (0); 173} 174 175int 176sobind(so, nam, td) 177 struct socket *so; 178 struct sockaddr *nam; 179 struct thread *td; 180{ 181 int s = splnet(); 182 int error; 183 184 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td); 185 splx(s); 186 return (error); 187} 188 189void 190sodealloc(so) 191 struct socket *so; 192{ 193 194 so->so_gencnt = ++so_gencnt; 195 if (so->so_rcv.sb_hiwat) 196 (void)chgsbsize(so->so_cred->cr_uidinfo, 197 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY); 198 if (so->so_snd.sb_hiwat) 199 (void)chgsbsize(so->so_cred->cr_uidinfo, 200 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY); 201#ifdef INET 202 if (so->so_accf != NULL) { 203 if (so->so_accf->so_accept_filter != NULL && 204 so->so_accf->so_accept_filter->accf_destroy != NULL) { 205 so->so_accf->so_accept_filter->accf_destroy(so); 206 } 207 if (so->so_accf->so_accept_filter_str != NULL) 208 FREE(so->so_accf->so_accept_filter_str, M_ACCF); 209 FREE(so->so_accf, M_ACCF); 210 } 211#endif 212 crfree(so->so_cred); 213 zfree(so->so_zone, so); 214} 215 216int 217solisten(so, backlog, td) 218 register struct socket *so; 219 int backlog; 220 struct thread *td; 221{ 222 int s, error; 223 224 s = splnet(); 225 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, td); 226 if (error) { 227 splx(s); 228 return (error); 229 } 230 if (TAILQ_EMPTY(&so->so_comp)) 231 so->so_options |= SO_ACCEPTCONN; 232 if (backlog < 0 || backlog > somaxconn) 233 backlog = somaxconn; 234 so->so_qlimit = backlog; 235 splx(s); 236 return (0); 237} 238 239void 240sofree(so) 241 register struct socket *so; 242{ 243 struct socket *head = so->so_head; 244 245 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) 246 return; 247 if (head != NULL) { 248 if (so->so_state & SS_INCOMP) { 249 TAILQ_REMOVE(&head->so_incomp, so, so_list); 250 head->so_incqlen--; 251 } else if (so->so_state & SS_COMP) { 252 /* 253 * We must not decommission a socket that's 254 * on the accept(2) queue. If we do, then 255 * accept(2) may hang after select(2) indicated 256 * that the listening socket was ready. 257 */ 258 return; 259 } else { 260 panic("sofree: not queued"); 261 } 262 head->so_qlen--; 263 so->so_state &= ~SS_INCOMP; 264 so->so_head = NULL; 265 } 266 sbrelease(&so->so_snd, so); 267 sorflush(so); 268 sodealloc(so); 269} 270 271/* 272 * Close a socket on last file table reference removal. 273 * Initiate disconnect if connected. 274 * Free socket when disconnect complete. 275 */ 276int 277soclose(so) 278 register struct socket *so; 279{ 280 int s = splnet(); /* conservative */ 281 int error = 0; 282 283 funsetown(so->so_sigio); 284 if (so->so_options & SO_ACCEPTCONN) { 285 struct socket *sp, *sonext; 286 287 sp = TAILQ_FIRST(&so->so_incomp); 288 for (; sp != NULL; sp = sonext) { 289 sonext = TAILQ_NEXT(sp, so_list); 290 (void) soabort(sp); 291 } 292 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) { 293 sonext = TAILQ_NEXT(sp, so_list); 294 /* Dequeue from so_comp since sofree() won't do it */ 295 TAILQ_REMOVE(&so->so_comp, sp, so_list); 296 so->so_qlen--; 297 sp->so_state &= ~SS_COMP; 298 sp->so_head = NULL; 299 (void) soabort(sp); 300 } 301 } 302 if (so->so_pcb == 0) 303 goto discard; 304 if (so->so_state & SS_ISCONNECTED) { 305 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 306 error = sodisconnect(so); 307 if (error) 308 goto drop; 309 } 310 if (so->so_options & SO_LINGER) { 311 if ((so->so_state & SS_ISDISCONNECTING) && 312 (so->so_state & SS_NBIO)) 313 goto drop; 314 while (so->so_state & SS_ISCONNECTED) { 315 error = tsleep((caddr_t)&so->so_timeo, 316 PSOCK | PCATCH, "soclos", so->so_linger * hz); 317 if (error) 318 break; 319 } 320 } 321 } 322drop: 323 if (so->so_pcb) { 324 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so); 325 if (error == 0) 326 error = error2; 327 } 328discard: 329 if (so->so_state & SS_NOFDREF) 330 panic("soclose: NOFDREF"); 331 so->so_state |= SS_NOFDREF; 332 sofree(so); 333 splx(s); 334 return (error); 335} 336 337/* 338 * Must be called at splnet... 339 */ 340int 341soabort(so) 342 struct socket *so; 343{ 344 int error; 345 346 error = (*so->so_proto->pr_usrreqs->pru_abort)(so); 347 if (error) { 348 sofree(so); 349 return error; 350 } 351 return (0); 352} 353 354int 355soaccept(so, nam) 356 register struct socket *so; 357 struct sockaddr **nam; 358{ 359 int s = splnet(); 360 int error; 361 362 if ((so->so_state & SS_NOFDREF) == 0) 363 panic("soaccept: !NOFDREF"); 364 so->so_state &= ~SS_NOFDREF; 365 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam); 366 splx(s); 367 return (error); 368} 369 370int 371soconnect(so, nam, td) 372 register struct socket *so; 373 struct sockaddr *nam; 374 struct thread *td; 375{ 376 int s; 377 int error; 378 379 if (so->so_options & SO_ACCEPTCONN) 380 return (EOPNOTSUPP); 381 s = splnet(); 382 /* 383 * If protocol is connection-based, can only connect once. 384 * Otherwise, if connected, try to disconnect first. 385 * This allows user to disconnect by connecting to, e.g., 386 * a null address. 387 */ 388 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 389 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 390 (error = sodisconnect(so)))) 391 error = EISCONN; 392 else 393 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td); 394 splx(s); 395 return (error); 396} 397 398int 399soconnect2(so1, so2) 400 register struct socket *so1; 401 struct socket *so2; 402{ 403 int s = splnet(); 404 int error; 405 406 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2); 407 splx(s); 408 return (error); 409} 410 411int 412sodisconnect(so) 413 register struct socket *so; 414{ 415 int s = splnet(); 416 int error; 417 418 if ((so->so_state & SS_ISCONNECTED) == 0) { 419 error = ENOTCONN; 420 goto bad; 421 } 422 if (so->so_state & SS_ISDISCONNECTING) { 423 error = EALREADY; 424 goto bad; 425 } 426 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so); 427bad: 428 splx(s); 429 return (error); 430} 431 432#define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 433/* 434 * Send on a socket. 435 * If send must go all at once and message is larger than 436 * send buffering, then hard error. 437 * Lock against other senders. 438 * If must go all at once and not enough room now, then 439 * inform user that this would block and do nothing. 440 * Otherwise, if nonblocking, send as much as possible. 441 * The data to be sent is described by "uio" if nonzero, 442 * otherwise by the mbuf chain "top" (which must be null 443 * if uio is not). Data provided in mbuf chain must be small 444 * enough to send all at once. 445 * 446 * Returns nonzero on error, timeout or signal; callers 447 * must check for short counts if EINTR/ERESTART are returned. 448 * Data and control buffers are freed on return. 449 */ 450int 451sosend(so, addr, uio, top, control, flags, td) 452 register struct socket *so; 453 struct sockaddr *addr; 454 struct uio *uio; 455 struct mbuf *top; 456 struct mbuf *control; 457 int flags; 458 struct thread *td; 459{ 460 struct mbuf **mp; 461 register struct mbuf *m; 462 register long space, len, resid; 463 int clen = 0, error, s, dontroute, mlen; 464 int atomic = sosendallatonce(so) || top; 465 466 if (uio) 467 resid = uio->uio_resid; 468 else 469 resid = top->m_pkthdr.len; 470 /* 471 * In theory resid should be unsigned. 472 * However, space must be signed, as it might be less than 0 473 * if we over-committed, and we must use a signed comparison 474 * of space and resid. On the other hand, a negative resid 475 * causes us to loop sending 0-length segments to the protocol. 476 * 477 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 478 * type sockets since that's an error. 479 */ 480 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) { 481 error = EINVAL; 482 goto out; 483 } 484 485 dontroute = 486 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 487 (so->so_proto->pr_flags & PR_ATOMIC); 488 if (td) 489 td->td_proc->p_stats->p_ru.ru_msgsnd++; 490 if (control) 491 clen = control->m_len; 492#define snderr(errno) { error = errno; splx(s); goto release; } 493 494restart: 495 error = sblock(&so->so_snd, SBLOCKWAIT(flags)); 496 if (error) 497 goto out; 498 do { 499 s = splnet(); 500 if (so->so_state & SS_CANTSENDMORE) 501 snderr(EPIPE); 502 if (so->so_error) { 503 error = so->so_error; 504 so->so_error = 0; 505 splx(s); 506 goto release; 507 } 508 if ((so->so_state & SS_ISCONNECTED) == 0) { 509 /* 510 * `sendto' and `sendmsg' is allowed on a connection- 511 * based socket if it supports implied connect. 512 * Return ENOTCONN if not connected and no address is 513 * supplied. 514 */ 515 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 516 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 517 if ((so->so_state & SS_ISCONFIRMING) == 0 && 518 !(resid == 0 && clen != 0)) 519 snderr(ENOTCONN); 520 } else if (addr == 0) 521 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ? 522 ENOTCONN : EDESTADDRREQ); 523 } 524 space = sbspace(&so->so_snd); 525 if (flags & MSG_OOB) 526 space += 1024; 527 if ((atomic && resid > so->so_snd.sb_hiwat) || 528 clen > so->so_snd.sb_hiwat) 529 snderr(EMSGSIZE); 530 if (space < resid + clen && uio && 531 (atomic || space < so->so_snd.sb_lowat || space < clen)) { 532 if (so->so_state & SS_NBIO) 533 snderr(EWOULDBLOCK); 534 sbunlock(&so->so_snd); 535 error = sbwait(&so->so_snd); 536 splx(s); 537 if (error) 538 goto out; 539 goto restart; 540 } 541 splx(s); 542 mp = ⊤ 543 space -= clen; 544 do { 545 if (uio == NULL) { 546 /* 547 * Data is prepackaged in "top". 548 */ 549 resid = 0; 550 if (flags & MSG_EOR) 551 top->m_flags |= M_EOR; 552 } else do { 553 if (top == 0) { 554 MGETHDR(m, M_TRYWAIT, MT_DATA); 555 if (m == NULL) { 556 error = ENOBUFS; 557 goto release; 558 } 559 mlen = MHLEN; 560 m->m_pkthdr.len = 0; 561 m->m_pkthdr.rcvif = (struct ifnet *)0; 562 } else { 563 MGET(m, M_TRYWAIT, MT_DATA); 564 if (m == NULL) { 565 error = ENOBUFS; 566 goto release; 567 } 568 mlen = MLEN; 569 } 570 if (resid >= MINCLSIZE) { 571 MCLGET(m, M_TRYWAIT); 572 if ((m->m_flags & M_EXT) == 0) 573 goto nopages; 574 mlen = MCLBYTES; 575 len = min(min(mlen, resid), space); 576 } else { 577nopages: 578 len = min(min(mlen, resid), space); 579 /* 580 * For datagram protocols, leave room 581 * for protocol headers in first mbuf. 582 */ 583 if (atomic && top == 0 && len < mlen) 584 MH_ALIGN(m, len); 585 } 586 space -= len; 587 error = uiomove(mtod(m, caddr_t), (int)len, uio); 588 resid = uio->uio_resid; 589 m->m_len = len; 590 *mp = m; 591 top->m_pkthdr.len += len; 592 if (error) 593 goto release; 594 mp = &m->m_next; 595 if (resid <= 0) { 596 if (flags & MSG_EOR) 597 top->m_flags |= M_EOR; 598 break; 599 } 600 } while (space > 0 && atomic); 601 if (dontroute) 602 so->so_options |= SO_DONTROUTE; 603 s = splnet(); /* XXX */ 604 /* 605 * XXX all the SS_CANTSENDMORE checks previously 606 * done could be out of date. We could have recieved 607 * a reset packet in an interrupt or maybe we slept 608 * while doing page faults in uiomove() etc. We could 609 * probably recheck again inside the splnet() protection 610 * here, but there are probably other places that this 611 * also happens. We must rethink this. 612 */ 613 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 614 (flags & MSG_OOB) ? PRUS_OOB : 615 /* 616 * If the user set MSG_EOF, the protocol 617 * understands this flag and nothing left to 618 * send then use PRU_SEND_EOF instead of PRU_SEND. 619 */ 620 ((flags & MSG_EOF) && 621 (so->so_proto->pr_flags & PR_IMPLOPCL) && 622 (resid <= 0)) ? 623 PRUS_EOF : 624 /* If there is more to send set PRUS_MORETOCOME */ 625 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 626 top, addr, control, td); 627 splx(s); 628 if (dontroute) 629 so->so_options &= ~SO_DONTROUTE; 630 clen = 0; 631 control = 0; 632 top = 0; 633 mp = ⊤ 634 if (error) 635 goto release; 636 } while (resid && space > 0); 637 } while (resid); 638 639release: 640 sbunlock(&so->so_snd); 641out: 642 if (top) 643 m_freem(top); 644 if (control) 645 m_freem(control); 646 return (error); 647} 648 649/* 650 * Implement receive operations on a socket. 651 * We depend on the way that records are added to the sockbuf 652 * by sbappend*. In particular, each record (mbufs linked through m_next) 653 * must begin with an address if the protocol so specifies, 654 * followed by an optional mbuf or mbufs containing ancillary data, 655 * and then zero or more mbufs of data. 656 * In order to avoid blocking network interrupts for the entire time here, 657 * we splx() while doing the actual copy to user space. 658 * Although the sockbuf is locked, new data may still be appended, 659 * and thus we must maintain consistency of the sockbuf during that time. 660 * 661 * The caller may receive the data as a single mbuf chain by supplying 662 * an mbuf **mp0 for use in returning the chain. The uio is then used 663 * only for the count in uio_resid. 664 */ 665int 666soreceive(so, psa, uio, mp0, controlp, flagsp) 667 register struct socket *so; 668 struct sockaddr **psa; 669 struct uio *uio; 670 struct mbuf **mp0; 671 struct mbuf **controlp; 672 int *flagsp; 673{ 674 struct mbuf *m, **mp; 675 register int flags, len, error, s, offset; 676 struct protosw *pr = so->so_proto; 677 struct mbuf *nextrecord; 678 int moff, type = 0; 679 int orig_resid = uio->uio_resid; 680 681 mp = mp0; 682 if (psa) 683 *psa = 0; 684 if (controlp) 685 *controlp = 0; 686 if (flagsp) 687 flags = *flagsp &~ MSG_EOR; 688 else 689 flags = 0; 690 if (flags & MSG_OOB) { 691 m = m_get(M_TRYWAIT, MT_DATA); 692 if (m == NULL) 693 return (ENOBUFS); 694 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK); 695 if (error) 696 goto bad; 697 do { 698 error = uiomove(mtod(m, caddr_t), 699 (int) min(uio->uio_resid, m->m_len), uio); 700 m = m_free(m); 701 } while (uio->uio_resid && error == 0 && m); 702bad: 703 if (m) 704 m_freem(m); 705 return (error); 706 } 707 if (mp) 708 *mp = (struct mbuf *)0; 709 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) 710 (*pr->pr_usrreqs->pru_rcvd)(so, 0); 711 712restart: 713 error = sblock(&so->so_rcv, SBLOCKWAIT(flags)); 714 if (error) 715 return (error); 716 s = splnet(); 717 718 m = so->so_rcv.sb_mb; 719 /* 720 * If we have less data than requested, block awaiting more 721 * (subject to any timeout) if: 722 * 1. the current count is less than the low water mark, or 723 * 2. MSG_WAITALL is set, and it is possible to do the entire 724 * receive operation at once if we block (resid <= hiwat). 725 * 3. MSG_DONTWAIT is not set 726 * If MSG_WAITALL is set but resid is larger than the receive buffer, 727 * we have to do the receive in sections, and thus risk returning 728 * a short count if a timeout or signal occurs after we start. 729 */ 730 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 && 731 so->so_rcv.sb_cc < uio->uio_resid) && 732 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 733 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && 734 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) { 735 KASSERT(m != 0 || !so->so_rcv.sb_cc, 736 ("receive: m == %p so->so_rcv.sb_cc == %lu", 737 m, so->so_rcv.sb_cc)); 738 if (so->so_error) { 739 if (m) 740 goto dontblock; 741 error = so->so_error; 742 if ((flags & MSG_PEEK) == 0) 743 so->so_error = 0; 744 goto release; 745 } 746 if (so->so_state & SS_CANTRCVMORE) { 747 if (m) 748 goto dontblock; 749 else 750 goto release; 751 } 752 for (; m; m = m->m_next) 753 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 754 m = so->so_rcv.sb_mb; 755 goto dontblock; 756 } 757 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 758 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 759 error = ENOTCONN; 760 goto release; 761 } 762 if (uio->uio_resid == 0) 763 goto release; 764 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) { 765 error = EWOULDBLOCK; 766 goto release; 767 } 768 sbunlock(&so->so_rcv); 769 error = sbwait(&so->so_rcv); 770 splx(s); 771 if (error) 772 return (error); 773 goto restart; 774 } 775dontblock: 776 if (uio->uio_td) 777 uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++; 778 nextrecord = m->m_nextpkt; 779 if (pr->pr_flags & PR_ADDR) { 780 KASSERT(m->m_type == MT_SONAME, 781 ("m->m_type == %d", m->m_type)); 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 ("m->m_type == %d", m->m_type)); 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 1648int 1649socheckuid(struct socket *so, uid_t uid) 1650{ 1651 1652 if (so == NULL) 1653 return (EPERM); 1654 if (so->so_cred->cr_uid == uid) 1655 return (0); 1656 return (EPERM); 1657} 1658