uipc_socket.c revision 131999
1/* 2 * Copyright (c) 2004 The FreeBSD Foundation 3 * Copyright (c) 2004 Robert Watson 4 * Copyright (c) 1982, 1986, 1988, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 4. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94 32 */ 33 34#include <sys/cdefs.h> 35__FBSDID("$FreeBSD: head/sys/kern/uipc_socket.c 131999 2004-07-11 23:13:14Z rwatson $"); 36 37#include "opt_inet.h" 38#include "opt_mac.h" 39#include "opt_zero.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/fcntl.h> 44#include <sys/limits.h> 45#include <sys/lock.h> 46#include <sys/mac.h> 47#include <sys/malloc.h> 48#include <sys/mbuf.h> 49#include <sys/mutex.h> 50#include <sys/domain.h> 51#include <sys/file.h> /* for struct knote */ 52#include <sys/kernel.h> 53#include <sys/event.h> 54#include <sys/poll.h> 55#include <sys/proc.h> 56#include <sys/protosw.h> 57#include <sys/socket.h> 58#include <sys/socketvar.h> 59#include <sys/resourcevar.h> 60#include <sys/signalvar.h> 61#include <sys/sysctl.h> 62#include <sys/uio.h> 63#include <sys/jail.h> 64 65#include <vm/uma.h> 66 67 68static int soreceive_rcvoob(struct socket *so, struct uio *uio, 69 int flags); 70 71#ifdef INET 72static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt); 73#endif 74 75static void filt_sordetach(struct knote *kn); 76static int filt_soread(struct knote *kn, long hint); 77static void filt_sowdetach(struct knote *kn); 78static int filt_sowrite(struct knote *kn, long hint); 79static int filt_solisten(struct knote *kn, long hint); 80 81static struct filterops solisten_filtops = 82 { 1, NULL, filt_sordetach, filt_solisten }; 83static struct filterops soread_filtops = 84 { 1, NULL, filt_sordetach, filt_soread }; 85static struct filterops sowrite_filtops = 86 { 1, NULL, filt_sowdetach, filt_sowrite }; 87 88uma_zone_t socket_zone; 89so_gen_t so_gencnt; /* generation count for sockets */ 90 91MALLOC_DEFINE(M_SONAME, "soname", "socket name"); 92MALLOC_DEFINE(M_PCB, "pcb", "protocol control block"); 93 94SYSCTL_DECL(_kern_ipc); 95 96static int somaxconn = SOMAXCONN; 97SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW, 98 &somaxconn, 0, "Maximum pending socket connection queue size"); 99static int numopensockets; 100SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD, 101 &numopensockets, 0, "Number of open sockets"); 102#ifdef ZERO_COPY_SOCKETS 103/* These aren't static because they're used in other files. */ 104int so_zero_copy_send = 1; 105int so_zero_copy_receive = 1; 106SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0, 107 "Zero copy controls"); 108SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW, 109 &so_zero_copy_receive, 0, "Enable zero copy receive"); 110SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW, 111 &so_zero_copy_send, 0, "Enable zero copy send"); 112#endif /* ZERO_COPY_SOCKETS */ 113 114/* 115 * accept_mtx locks down per-socket fields relating to accept queues. See 116 * socketvar.h for an annotation of the protected fields of struct socket. 117 */ 118struct mtx accept_mtx; 119MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF); 120 121/* 122 * so_global_mtx protects so_gencnt, numopensockets, and the per-socket 123 * so_gencnt field. 124 * 125 * XXXRW: These variables might be better manipulated using atomic operations 126 * for improved efficiency. 127 */ 128static struct mtx so_global_mtx; 129MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF); 130 131/* 132 * Socket operation routines. 133 * These routines are called by the routines in 134 * sys_socket.c or from a system process, and 135 * implement the semantics of socket operations by 136 * switching out to the protocol specific routines. 137 */ 138 139/* 140 * Get a socket structure from our zone, and initialize it. 141 * Note that it would probably be better to allocate socket 142 * and PCB at the same time, but I'm not convinced that all 143 * the protocols can be easily modified to do this. 144 * 145 * soalloc() returns a socket with a ref count of 0. 146 */ 147struct socket * 148soalloc(int mflags) 149{ 150 struct socket *so; 151#ifdef MAC 152 int error; 153#endif 154 155 so = uma_zalloc(socket_zone, mflags | M_ZERO); 156 if (so != NULL) { 157#ifdef MAC 158 error = mac_init_socket(so, mflags); 159 if (error != 0) { 160 uma_zfree(socket_zone, so); 161 so = NULL; 162 return so; 163 } 164#endif 165 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd"); 166 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv"); 167 /* sx_init(&so->so_sxlock, "socket sxlock"); */ 168 TAILQ_INIT(&so->so_aiojobq); 169 mtx_lock(&so_global_mtx); 170 so->so_gencnt = ++so_gencnt; 171 ++numopensockets; 172 mtx_unlock(&so_global_mtx); 173 } 174 return so; 175} 176 177/* 178 * socreate returns a socket with a ref count of 1. The socket should be 179 * closed with soclose(). 180 */ 181int 182socreate(dom, aso, type, proto, cred, td) 183 int dom; 184 struct socket **aso; 185 int type; 186 int proto; 187 struct ucred *cred; 188 struct thread *td; 189{ 190 struct protosw *prp; 191 struct socket *so; 192 int error; 193 194 if (proto) 195 prp = pffindproto(dom, proto, type); 196 else 197 prp = pffindtype(dom, type); 198 199 if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL) 200 return (EPROTONOSUPPORT); 201 202 if (jailed(cred) && jail_socket_unixiproute_only && 203 prp->pr_domain->dom_family != PF_LOCAL && 204 prp->pr_domain->dom_family != PF_INET && 205 prp->pr_domain->dom_family != PF_ROUTE) { 206 return (EPROTONOSUPPORT); 207 } 208 209 if (prp->pr_type != type) 210 return (EPROTOTYPE); 211 so = soalloc(M_WAITOK); 212 if (so == NULL) 213 return (ENOBUFS); 214 215 TAILQ_INIT(&so->so_incomp); 216 TAILQ_INIT(&so->so_comp); 217 so->so_type = type; 218 so->so_cred = crhold(cred); 219 so->so_proto = prp; 220#ifdef MAC 221 mac_create_socket(cred, so); 222#endif 223 SOCK_LOCK(so); 224 soref(so); 225 SOCK_UNLOCK(so); 226 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td); 227 if (error) { 228 SOCK_LOCK(so); 229 so->so_state |= SS_NOFDREF; 230 sorele(so); 231 return (error); 232 } 233 *aso = so; 234 return (0); 235} 236 237int 238sobind(so, nam, td) 239 struct socket *so; 240 struct sockaddr *nam; 241 struct thread *td; 242{ 243 244 return ((*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td)); 245} 246 247void 248sodealloc(struct socket *so) 249{ 250 251 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count)); 252 mtx_lock(&so_global_mtx); 253 so->so_gencnt = ++so_gencnt; 254 mtx_unlock(&so_global_mtx); 255 if (so->so_rcv.sb_hiwat) 256 (void)chgsbsize(so->so_cred->cr_uidinfo, 257 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY); 258 if (so->so_snd.sb_hiwat) 259 (void)chgsbsize(so->so_cred->cr_uidinfo, 260 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY); 261#ifdef INET 262 /* remove acccept filter if one is present. */ 263 if (so->so_accf != NULL) 264 do_setopt_accept_filter(so, NULL); 265#endif 266#ifdef MAC 267 mac_destroy_socket(so); 268#endif 269 crfree(so->so_cred); 270 SOCKBUF_LOCK_DESTROY(&so->so_snd); 271 SOCKBUF_LOCK_DESTROY(&so->so_rcv); 272 /* sx_destroy(&so->so_sxlock); */ 273 uma_zfree(socket_zone, so); 274 /* 275 * XXXRW: Seems like a shame to grab the mutex again down here, but 276 * we don't want to decrement the socket count until after we free 277 * the socket, and we can't increment the gencnt on the socket after 278 * we free, it so... 279 */ 280 mtx_lock(&so_global_mtx); 281 --numopensockets; 282 mtx_unlock(&so_global_mtx); 283} 284 285int 286solisten(so, backlog, td) 287 struct socket *so; 288 int backlog; 289 struct thread *td; 290{ 291 int error; 292 293 /* 294 * XXXRW: Ordering issue here -- perhaps we need to set 295 * SO_ACCEPTCONN before the call to pru_listen()? 296 * XXXRW: General atomic test-and-set concerns here also. 297 */ 298 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | 299 SS_ISDISCONNECTING)) 300 return (EINVAL); 301 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, td); 302 if (error) 303 return (error); 304 ACCEPT_LOCK(); 305 if (TAILQ_EMPTY(&so->so_comp)) { 306 SOCK_LOCK(so); 307 so->so_options |= SO_ACCEPTCONN; 308 SOCK_UNLOCK(so); 309 } 310 if (backlog < 0 || backlog > somaxconn) 311 backlog = somaxconn; 312 so->so_qlimit = backlog; 313 ACCEPT_UNLOCK(); 314 return (0); 315} 316 317void 318sofree(so) 319 struct socket *so; 320{ 321 struct socket *head; 322 323 KASSERT(so->so_count == 0, ("socket %p so_count not 0", so)); 324 SOCK_LOCK_ASSERT(so); 325 326 if (so->so_pcb != NULL || (so->so_state & SS_NOFDREF) == 0) { 327 SOCK_UNLOCK(so); 328 return; 329 } 330 331 SOCK_UNLOCK(so); 332 ACCEPT_LOCK(); 333 head = so->so_head; 334 if (head != NULL) { 335 KASSERT((so->so_qstate & SQ_COMP) != 0 || 336 (so->so_qstate & SQ_INCOMP) != 0, 337 ("sofree: so_head != NULL, but neither SQ_COMP nor " 338 "SQ_INCOMP")); 339 KASSERT((so->so_qstate & SQ_COMP) == 0 || 340 (so->so_qstate & SQ_INCOMP) == 0, 341 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP")); 342 /* 343 * accept(2) is responsible draining the completed 344 * connection queue and freeing those sockets, so 345 * we just return here if this socket is currently 346 * on the completed connection queue. Otherwise, 347 * accept(2) may hang after select(2) has indicating 348 * that a listening socket was ready. If it's an 349 * incomplete connection, we remove it from the queue 350 * and free it; otherwise, it won't be released until 351 * the listening socket is closed. 352 */ 353 if ((so->so_qstate & SQ_COMP) != 0) { 354 ACCEPT_UNLOCK(); 355 return; 356 } 357 TAILQ_REMOVE(&head->so_incomp, so, so_list); 358 head->so_incqlen--; 359 so->so_qstate &= ~SQ_INCOMP; 360 so->so_head = NULL; 361 } 362 KASSERT((so->so_qstate & SQ_COMP) == 0 && 363 (so->so_qstate & SQ_INCOMP) == 0, 364 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)", 365 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP)); 366 ACCEPT_UNLOCK(); 367 SOCKBUF_LOCK(&so->so_snd); 368 so->so_snd.sb_flags |= SB_NOINTR; 369 (void)sblock(&so->so_snd, M_WAITOK); 370 /* 371 * socantsendmore_locked() drops the socket buffer mutex so that it 372 * can safely perform wakeups. Re-acquire the mutex before 373 * continuing. 374 */ 375 socantsendmore_locked(so); 376 SOCKBUF_LOCK(&so->so_snd); 377 sbunlock(&so->so_snd); 378 sbrelease_locked(&so->so_snd, so); 379 SOCKBUF_UNLOCK(&so->so_snd); 380 sorflush(so); 381 sodealloc(so); 382} 383 384/* 385 * Close a socket on last file table reference removal. 386 * Initiate disconnect if connected. 387 * Free socket when disconnect complete. 388 * 389 * This function will sorele() the socket. Note that soclose() may be 390 * called prior to the ref count reaching zero. The actual socket 391 * structure will not be freed until the ref count reaches zero. 392 */ 393int 394soclose(so) 395 struct socket *so; 396{ 397 int error = 0; 398 399 funsetown(&so->so_sigio); 400 if (so->so_options & SO_ACCEPTCONN) { 401 struct socket *sp; 402 ACCEPT_LOCK(); 403 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) { 404 TAILQ_REMOVE(&so->so_incomp, sp, so_list); 405 so->so_incqlen--; 406 sp->so_qstate &= ~SQ_INCOMP; 407 sp->so_head = NULL; 408 ACCEPT_UNLOCK(); 409 (void) soabort(sp); 410 ACCEPT_LOCK(); 411 } 412 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) { 413 TAILQ_REMOVE(&so->so_comp, sp, so_list); 414 so->so_qlen--; 415 sp->so_qstate &= ~SQ_COMP; 416 sp->so_head = NULL; 417 ACCEPT_UNLOCK(); 418 (void) soabort(sp); 419 ACCEPT_LOCK(); 420 } 421 ACCEPT_UNLOCK(); 422 } 423 if (so->so_pcb == NULL) 424 goto discard; 425 if (so->so_state & SS_ISCONNECTED) { 426 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 427 error = sodisconnect(so); 428 if (error) 429 goto drop; 430 } 431 if (so->so_options & SO_LINGER) { 432 if ((so->so_state & SS_ISDISCONNECTING) && 433 (so->so_state & SS_NBIO)) 434 goto drop; 435 while (so->so_state & SS_ISCONNECTED) { 436 error = tsleep(&so->so_timeo, 437 PSOCK | PCATCH, "soclos", so->so_linger * hz); 438 if (error) 439 break; 440 } 441 } 442 } 443drop: 444 if (so->so_pcb != NULL) { 445 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so); 446 if (error == 0) 447 error = error2; 448 } 449discard: 450 SOCK_LOCK(so); 451 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF")); 452 so->so_state |= SS_NOFDREF; 453 sorele(so); 454 return (error); 455} 456 457/* 458 * soabort() must not be called with any socket locks held, as it calls 459 * into the protocol, which will call back into the socket code causing 460 * it to acquire additional socket locks that may cause recursion or lock 461 * order reversals. 462 */ 463int 464soabort(so) 465 struct socket *so; 466{ 467 int error; 468 469 error = (*so->so_proto->pr_usrreqs->pru_abort)(so); 470 if (error) { 471 SOCK_LOCK(so); 472 sotryfree(so); /* note: does not decrement the ref count */ 473 return error; 474 } 475 return (0); 476} 477 478int 479soaccept(so, nam) 480 struct socket *so; 481 struct sockaddr **nam; 482{ 483 int error; 484 485 SOCK_LOCK(so); 486 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF")); 487 so->so_state &= ~SS_NOFDREF; 488 SOCK_UNLOCK(so); 489 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam); 490 return (error); 491} 492 493int 494soconnect(so, nam, td) 495 struct socket *so; 496 struct sockaddr *nam; 497 struct thread *td; 498{ 499 int error; 500 501 if (so->so_options & SO_ACCEPTCONN) 502 return (EOPNOTSUPP); 503 /* 504 * If protocol is connection-based, can only connect once. 505 * Otherwise, if connected, try to disconnect first. 506 * This allows user to disconnect by connecting to, e.g., 507 * a null address. 508 */ 509 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 510 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 511 (error = sodisconnect(so)))) 512 error = EISCONN; 513 else 514 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td); 515 return (error); 516} 517 518int 519soconnect2(so1, so2) 520 struct socket *so1; 521 struct socket *so2; 522{ 523 524 return ((*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2)); 525} 526 527int 528sodisconnect(so) 529 struct socket *so; 530{ 531 int error; 532 533 if ((so->so_state & SS_ISCONNECTED) == 0) 534 return (ENOTCONN); 535 if (so->so_state & SS_ISDISCONNECTING) 536 return (EALREADY); 537 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so); 538 return (error); 539} 540 541#define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 542/* 543 * Send on a socket. 544 * If send must go all at once and message is larger than 545 * send buffering, then hard error. 546 * Lock against other senders. 547 * If must go all at once and not enough room now, then 548 * inform user that this would block and do nothing. 549 * Otherwise, if nonblocking, send as much as possible. 550 * The data to be sent is described by "uio" if nonzero, 551 * otherwise by the mbuf chain "top" (which must be null 552 * if uio is not). Data provided in mbuf chain must be small 553 * enough to send all at once. 554 * 555 * Returns nonzero on error, timeout or signal; callers 556 * must check for short counts if EINTR/ERESTART are returned. 557 * Data and control buffers are freed on return. 558 */ 559 560#ifdef ZERO_COPY_SOCKETS 561struct so_zerocopy_stats{ 562 int size_ok; 563 int align_ok; 564 int found_ifp; 565}; 566struct so_zerocopy_stats so_zerocp_stats = {0,0,0}; 567#include <netinet/in.h> 568#include <net/route.h> 569#include <netinet/in_pcb.h> 570#include <vm/vm.h> 571#include <vm/vm_page.h> 572#include <vm/vm_object.h> 573#endif /*ZERO_COPY_SOCKETS*/ 574 575int 576sosend(so, addr, uio, top, control, flags, td) 577 struct socket *so; 578 struct sockaddr *addr; 579 struct uio *uio; 580 struct mbuf *top; 581 struct mbuf *control; 582 int flags; 583 struct thread *td; 584{ 585 struct mbuf **mp; 586 struct mbuf *m; 587 long space, len = 0, resid; 588 int clen = 0, error, dontroute; 589 int atomic = sosendallatonce(so) || top; 590#ifdef ZERO_COPY_SOCKETS 591 int cow_send; 592#endif /* ZERO_COPY_SOCKETS */ 593 594 if (uio != NULL) 595 resid = uio->uio_resid; 596 else 597 resid = top->m_pkthdr.len; 598 /* 599 * In theory resid should be unsigned. 600 * However, space must be signed, as it might be less than 0 601 * if we over-committed, and we must use a signed comparison 602 * of space and resid. On the other hand, a negative resid 603 * causes us to loop sending 0-length segments to the protocol. 604 * 605 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 606 * type sockets since that's an error. 607 */ 608 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) { 609 error = EINVAL; 610 goto out; 611 } 612 613 dontroute = 614 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 615 (so->so_proto->pr_flags & PR_ATOMIC); 616 if (td != NULL) 617 td->td_proc->p_stats->p_ru.ru_msgsnd++; 618 if (control != NULL) 619 clen = control->m_len; 620#define snderr(errno) { error = (errno); goto release; } 621 622 SOCKBUF_LOCK(&so->so_snd); 623restart: 624 SOCKBUF_LOCK_ASSERT(&so->so_snd); 625 error = sblock(&so->so_snd, SBLOCKWAIT(flags)); 626 if (error) 627 goto out_locked; 628 do { 629 SOCKBUF_LOCK_ASSERT(&so->so_snd); 630 if (so->so_snd.sb_state & SBS_CANTSENDMORE) 631 snderr(EPIPE); 632 if (so->so_error) { 633 error = so->so_error; 634 so->so_error = 0; 635 goto release; 636 } 637 if ((so->so_state & SS_ISCONNECTED) == 0) { 638 /* 639 * `sendto' and `sendmsg' is allowed on a connection- 640 * based socket if it supports implied connect. 641 * Return ENOTCONN if not connected and no address is 642 * supplied. 643 */ 644 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 645 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 646 if ((so->so_state & SS_ISCONFIRMING) == 0 && 647 !(resid == 0 && clen != 0)) 648 snderr(ENOTCONN); 649 } else if (addr == NULL) 650 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ? 651 ENOTCONN : EDESTADDRREQ); 652 } 653 space = sbspace(&so->so_snd); 654 if (flags & MSG_OOB) 655 space += 1024; 656 if ((atomic && resid > so->so_snd.sb_hiwat) || 657 clen > so->so_snd.sb_hiwat) 658 snderr(EMSGSIZE); 659 if (space < resid + clen && 660 (atomic || space < so->so_snd.sb_lowat || space < clen)) { 661 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) 662 snderr(EWOULDBLOCK); 663 sbunlock(&so->so_snd); 664 error = sbwait(&so->so_snd); 665 if (error) 666 goto out_locked; 667 goto restart; 668 } 669 SOCKBUF_UNLOCK(&so->so_snd); 670 mp = ⊤ 671 space -= clen; 672 do { 673 if (uio == NULL) { 674 /* 675 * Data is prepackaged in "top". 676 */ 677 resid = 0; 678 if (flags & MSG_EOR) 679 top->m_flags |= M_EOR; 680 } else do { 681#ifdef ZERO_COPY_SOCKETS 682 cow_send = 0; 683#endif /* ZERO_COPY_SOCKETS */ 684 if (resid >= MINCLSIZE) { 685#ifdef ZERO_COPY_SOCKETS 686 if (top == NULL) { 687 MGETHDR(m, M_TRYWAIT, MT_DATA); 688 if (m == NULL) { 689 error = ENOBUFS; 690 SOCKBUF_LOCK(&so->so_snd); 691 goto release; 692 } 693 m->m_pkthdr.len = 0; 694 m->m_pkthdr.rcvif = (struct ifnet *)0; 695 } else { 696 MGET(m, M_TRYWAIT, MT_DATA); 697 if (m == NULL) { 698 error = ENOBUFS; 699 SOCKBUF_LOCK(&so->so_snd); 700 goto release; 701 } 702 } 703 if (so_zero_copy_send && 704 resid>=PAGE_SIZE && 705 space>=PAGE_SIZE && 706 uio->uio_iov->iov_len>=PAGE_SIZE) { 707 so_zerocp_stats.size_ok++; 708 if (!((vm_offset_t) 709 uio->uio_iov->iov_base & PAGE_MASK)){ 710 so_zerocp_stats.align_ok++; 711 cow_send = socow_setup(m, uio); 712 } 713 } 714 if (!cow_send) { 715 MCLGET(m, M_TRYWAIT); 716 if ((m->m_flags & M_EXT) == 0) { 717 m_free(m); 718 m = NULL; 719 } else { 720 len = min(min(MCLBYTES, resid), space); 721 } 722 } else 723 len = PAGE_SIZE; 724#else /* ZERO_COPY_SOCKETS */ 725 if (top == NULL) { 726 m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR); 727 m->m_pkthdr.len = 0; 728 m->m_pkthdr.rcvif = (struct ifnet *)0; 729 } else 730 m = m_getcl(M_TRYWAIT, MT_DATA, 0); 731 len = min(min(MCLBYTES, resid), space); 732#endif /* ZERO_COPY_SOCKETS */ 733 } else { 734 if (top == NULL) { 735 m = m_gethdr(M_TRYWAIT, MT_DATA); 736 m->m_pkthdr.len = 0; 737 m->m_pkthdr.rcvif = (struct ifnet *)0; 738 739 len = min(min(MHLEN, resid), space); 740 /* 741 * For datagram protocols, leave room 742 * for protocol headers in first mbuf. 743 */ 744 if (atomic && m && len < MHLEN) 745 MH_ALIGN(m, len); 746 } else { 747 m = m_get(M_TRYWAIT, MT_DATA); 748 len = min(min(MLEN, resid), space); 749 } 750 } 751 if (m == NULL) { 752 error = ENOBUFS; 753 SOCKBUF_LOCK(&so->so_snd); 754 goto release; 755 } 756 757 space -= len; 758#ifdef ZERO_COPY_SOCKETS 759 if (cow_send) 760 error = 0; 761 else 762#endif /* ZERO_COPY_SOCKETS */ 763 error = uiomove(mtod(m, void *), (int)len, uio); 764 resid = uio->uio_resid; 765 m->m_len = len; 766 *mp = m; 767 top->m_pkthdr.len += len; 768 if (error) { 769 SOCKBUF_LOCK(&so->so_snd); 770 goto release; 771 } 772 mp = &m->m_next; 773 if (resid <= 0) { 774 if (flags & MSG_EOR) 775 top->m_flags |= M_EOR; 776 break; 777 } 778 } while (space > 0 && atomic); 779 if (dontroute) { 780 SOCK_LOCK(so); 781 so->so_options |= SO_DONTROUTE; 782 SOCK_UNLOCK(so); 783 } 784 /* 785 * XXX all the SBS_CANTSENDMORE checks previously 786 * done could be out of date. We could have recieved 787 * a reset packet in an interrupt or maybe we slept 788 * while doing page faults in uiomove() etc. We could 789 * probably recheck again inside the splnet() protection 790 * here, but there are probably other places that this 791 * also happens. We must rethink this. 792 */ 793 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 794 (flags & MSG_OOB) ? PRUS_OOB : 795 /* 796 * If the user set MSG_EOF, the protocol 797 * understands this flag and nothing left to 798 * send then use PRU_SEND_EOF instead of PRU_SEND. 799 */ 800 ((flags & MSG_EOF) && 801 (so->so_proto->pr_flags & PR_IMPLOPCL) && 802 (resid <= 0)) ? 803 PRUS_EOF : 804 /* If there is more to send set PRUS_MORETOCOME */ 805 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 806 top, addr, control, td); 807 if (dontroute) { 808 SOCK_LOCK(so); 809 so->so_options &= ~SO_DONTROUTE; 810 SOCK_UNLOCK(so); 811 } 812 clen = 0; 813 control = NULL; 814 top = NULL; 815 mp = ⊤ 816 if (error) { 817 SOCKBUF_LOCK(&so->so_snd); 818 goto release; 819 } 820 } while (resid && space > 0); 821 SOCKBUF_LOCK(&so->so_snd); 822 } while (resid); 823 824release: 825 SOCKBUF_LOCK_ASSERT(&so->so_snd); 826 sbunlock(&so->so_snd); 827out_locked: 828 SOCKBUF_LOCK_ASSERT(&so->so_snd); 829 SOCKBUF_UNLOCK(&so->so_snd); 830out: 831 if (top != NULL) 832 m_freem(top); 833 if (control != NULL) 834 m_freem(control); 835 return (error); 836} 837 838/* 839 * The part of soreceive() that implements reading non-inline out-of-band 840 * data from a socket. For more complete comments, see soreceive(), from 841 * which this code originated. 842 * 843 * XXXRW: Note that soreceive_rcvoob(), unlike the remainder of soreiceve(), 844 * is unable to return an mbuf chain to the caller. 845 */ 846static int 847soreceive_rcvoob(so, uio, flags) 848 struct socket *so; 849 struct uio *uio; 850 int flags; 851{ 852 struct protosw *pr = so->so_proto; 853 struct mbuf *m; 854 int error; 855 856 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0")); 857 858 m = m_get(M_TRYWAIT, MT_DATA); 859 if (m == NULL) 860 return (ENOBUFS); 861 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK); 862 if (error) 863 goto bad; 864 do { 865#ifdef ZERO_COPY_SOCKETS 866 if (so_zero_copy_receive) { 867 vm_page_t pg; 868 int disposable; 869 870 if ((m->m_flags & M_EXT) 871 && (m->m_ext.ext_type == EXT_DISPOSABLE)) 872 disposable = 1; 873 else 874 disposable = 0; 875 876 pg = PHYS_TO_VM_PAGE(vtophys(mtod(m, caddr_t))); 877 if (uio->uio_offset == -1) 878 uio->uio_offset =IDX_TO_OFF(pg->pindex); 879 880 error = uiomoveco(mtod(m, void *), 881 min(uio->uio_resid, m->m_len), 882 uio, pg->object, 883 disposable); 884 } else 885#endif /* ZERO_COPY_SOCKETS */ 886 error = uiomove(mtod(m, void *), 887 (int) min(uio->uio_resid, m->m_len), uio); 888 m = m_free(m); 889 } while (uio->uio_resid && error == 0 && m); 890bad: 891 if (m != NULL) 892 m_freem(m); 893 return (error); 894} 895 896/* 897 * Following replacement or removal of the first mbuf on the first mbuf chain 898 * of a socket buffer, push necessary state changes back into the socket 899 * buffer so that other consumers see the values consistently. 'nextrecord' 900 * is the callers locally stored value of the original value of 901 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes. 902 * NOTE: 'nextrecord' may be NULL. 903 */ 904static __inline void 905sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord) 906{ 907 908 SOCKBUF_LOCK_ASSERT(sb); 909 /* 910 * First, update for the new value of nextrecord. If necessary, make 911 * it the first record. 912 */ 913 if (sb->sb_mb != NULL) 914 sb->sb_mb->m_nextpkt = nextrecord; 915 else 916 sb->sb_mb = nextrecord; 917 918 /* 919 * Now update any dependent socket buffer fields to reflect the new 920 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the 921 * addition of a second clause that takes care of the case where 922 * sb_mb has been updated, but remains the last record. 923 */ 924 if (sb->sb_mb == NULL) { 925 sb->sb_mbtail = NULL; 926 sb->sb_lastrecord = NULL; 927 } else if (sb->sb_mb->m_nextpkt == NULL) 928 sb->sb_lastrecord = sb->sb_mb; 929} 930 931 932/* 933 * Implement receive operations on a socket. 934 * We depend on the way that records are added to the sockbuf 935 * by sbappend*. In particular, each record (mbufs linked through m_next) 936 * must begin with an address if the protocol so specifies, 937 * followed by an optional mbuf or mbufs containing ancillary data, 938 * and then zero or more mbufs of data. 939 * In order to avoid blocking network interrupts for the entire time here, 940 * we splx() while doing the actual copy to user space. 941 * Although the sockbuf is locked, new data may still be appended, 942 * and thus we must maintain consistency of the sockbuf during that time. 943 * 944 * The caller may receive the data as a single mbuf chain by supplying 945 * an mbuf **mp0 for use in returning the chain. The uio is then used 946 * only for the count in uio_resid. 947 */ 948int 949soreceive(so, psa, uio, mp0, controlp, flagsp) 950 struct socket *so; 951 struct sockaddr **psa; 952 struct uio *uio; 953 struct mbuf **mp0; 954 struct mbuf **controlp; 955 int *flagsp; 956{ 957 struct mbuf *m, **mp; 958 int flags, len, error, offset; 959 struct protosw *pr = so->so_proto; 960 struct mbuf *nextrecord; 961 int moff, type = 0; 962 int orig_resid = uio->uio_resid; 963 964 mp = mp0; 965 if (psa != NULL) 966 *psa = NULL; 967 if (controlp != NULL) 968 *controlp = NULL; 969 if (flagsp != NULL) 970 flags = *flagsp &~ MSG_EOR; 971 else 972 flags = 0; 973 if (flags & MSG_OOB) 974 return (soreceive_rcvoob(so, uio, flags)); 975 if (mp != NULL) 976 *mp = NULL; 977 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) 978 (*pr->pr_usrreqs->pru_rcvd)(so, 0); 979 980 SOCKBUF_LOCK(&so->so_rcv); 981restart: 982 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 983 error = sblock(&so->so_rcv, SBLOCKWAIT(flags)); 984 if (error) 985 goto out; 986 987 m = so->so_rcv.sb_mb; 988 /* 989 * If we have less data than requested, block awaiting more 990 * (subject to any timeout) if: 991 * 1. the current count is less than the low water mark, or 992 * 2. MSG_WAITALL is set, and it is possible to do the entire 993 * receive operation at once if we block (resid <= hiwat). 994 * 3. MSG_DONTWAIT is not set 995 * If MSG_WAITALL is set but resid is larger than the receive buffer, 996 * we have to do the receive in sections, and thus risk returning 997 * a short count if a timeout or signal occurs after we start. 998 */ 999 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && 1000 so->so_rcv.sb_cc < uio->uio_resid) && 1001 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 1002 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && 1003 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) { 1004 KASSERT(m != NULL || !so->so_rcv.sb_cc, 1005 ("receive: m == %p so->so_rcv.sb_cc == %u", 1006 m, so->so_rcv.sb_cc)); 1007 if (so->so_error) { 1008 if (m != NULL) 1009 goto dontblock; 1010 error = so->so_error; 1011 if ((flags & MSG_PEEK) == 0) 1012 so->so_error = 0; 1013 goto release; 1014 } 1015 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1016 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1017 if (m) 1018 goto dontblock; 1019 else 1020 goto release; 1021 } 1022 for (; m != NULL; m = m->m_next) 1023 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 1024 m = so->so_rcv.sb_mb; 1025 goto dontblock; 1026 } 1027 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 1028 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 1029 error = ENOTCONN; 1030 goto release; 1031 } 1032 if (uio->uio_resid == 0) 1033 goto release; 1034 if ((so->so_state & SS_NBIO) || 1035 (flags & (MSG_DONTWAIT|MSG_NBIO))) { 1036 error = EWOULDBLOCK; 1037 goto release; 1038 } 1039 SBLASTRECORDCHK(&so->so_rcv); 1040 SBLASTMBUFCHK(&so->so_rcv); 1041 sbunlock(&so->so_rcv); 1042 error = sbwait(&so->so_rcv); 1043 if (error) 1044 goto out; 1045 goto restart; 1046 } 1047dontblock: 1048 /* 1049 * From this point onward, we maintain 'nextrecord' as a cache of the 1050 * pointer to the next record in the socket buffer. We must keep the 1051 * various socket buffer pointers and local stack versions of the 1052 * pointers in sync, pushing out modifications before dropping the 1053 * socket buffer mutex, and re-reading them when picking it up. 1054 * 1055 * Otherwise, we will race with the network stack appending new data 1056 * or records onto the socket buffer by using inconsistent/stale 1057 * versions of the field, possibly resulting in socket buffer 1058 * corruption. 1059 * 1060 * By holding the high-level sblock(), we prevent simultaneous 1061 * readers from pulling off the front of the socket buffer. 1062 */ 1063 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1064 if (uio->uio_td) 1065 uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++; 1066 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb")); 1067 SBLASTRECORDCHK(&so->so_rcv); 1068 SBLASTMBUFCHK(&so->so_rcv); 1069 nextrecord = m->m_nextpkt; 1070 if (pr->pr_flags & PR_ADDR) { 1071 KASSERT(m->m_type == MT_SONAME, 1072 ("m->m_type == %d", m->m_type)); 1073 orig_resid = 0; 1074 if (psa != NULL) 1075 *psa = sodupsockaddr(mtod(m, struct sockaddr *), 1076 M_NOWAIT); 1077 if (flags & MSG_PEEK) { 1078 m = m->m_next; 1079 } else { 1080 sbfree(&so->so_rcv, m); 1081 so->so_rcv.sb_mb = m_free(m); 1082 m = so->so_rcv.sb_mb; 1083 sockbuf_pushsync(&so->so_rcv, nextrecord); 1084 } 1085 } 1086 1087 /* 1088 * Process one or more MT_CONTROL mbufs present before any data mbufs 1089 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we 1090 * just copy the data; if !MSG_PEEK, we call into the protocol to 1091 * perform externalization. 1092 */ 1093 if (m != NULL && m->m_type == MT_CONTROL) { 1094 struct mbuf *cm = NULL; 1095 struct mbuf **cme = &cm; 1096 1097 do { 1098 if (flags & MSG_PEEK) { 1099 if (controlp != NULL) { 1100 *controlp = m_copy(m, 0, m->m_len); 1101 controlp = &(*controlp)->m_next; 1102 } 1103 m = m->m_next; 1104 } else { 1105 sbfree(&so->so_rcv, m); 1106 so->so_rcv.sb_mb = m->m_next; 1107 m->m_next = NULL; 1108 if (controlp) { 1109 /* 1110 * Collect mbufs for processing below. 1111 */ 1112 *cme = m; 1113 cme = &(*cme)->m_next; 1114 } else 1115 m_free(m); 1116 m = so->so_rcv.sb_mb; 1117 } 1118 } while (m != NULL && m->m_type == MT_CONTROL); 1119 if ((flags & MSG_PEEK) == 0) 1120 sockbuf_pushsync(&so->so_rcv, nextrecord); 1121 if (cm != NULL) { 1122 if (pr->pr_domain->dom_externalize != NULL) { 1123 SOCKBUF_UNLOCK(&so->so_rcv); 1124 error = (*pr->pr_domain->dom_externalize) 1125 (cm, controlp); 1126 SOCKBUF_LOCK(&so->so_rcv); 1127 } else 1128 m_freem(cm); 1129 } 1130 nextrecord = so->so_rcv.sb_mb->m_nextpkt; 1131 orig_resid = 0; 1132 } 1133 if (m != NULL) { 1134 if ((flags & MSG_PEEK) == 0) { 1135 KASSERT(m->m_nextpkt == nextrecord, 1136 ("soreceive: post-control, nextrecord !sync")); 1137 if (nextrecord == NULL) { 1138 KASSERT(so->so_rcv.sb_mb == m, 1139 ("soreceive: post-control, sb_mb!=m")); 1140 KASSERT(so->so_rcv.sb_lastrecord == m, 1141 ("soreceive: post-control, lastrecord!=m")); 1142 } 1143 } 1144 type = m->m_type; 1145 if (type == MT_OOBDATA) 1146 flags |= MSG_OOB; 1147 } else { 1148 if ((flags & MSG_PEEK) == 0) { 1149 KASSERT(so->so_rcv.sb_mb == nextrecord, 1150 ("soreceive: sb_mb != nextrecord")); 1151 if (so->so_rcv.sb_mb == NULL) { 1152 KASSERT(so->so_rcv.sb_lastrecord == NULL, 1153 ("soreceive: sb_lastercord != NULL")); 1154 } 1155 } 1156 } 1157 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1158 SBLASTRECORDCHK(&so->so_rcv); 1159 SBLASTMBUFCHK(&so->so_rcv); 1160 1161 /* 1162 * Now continue to read any data mbufs off of the head of the socket 1163 * buffer until the read request is satisfied. Note that 'type' is 1164 * used to store the type of any mbuf reads that have happened so far 1165 * such that soreceive() can stop reading if the type changes, which 1166 * causes soreceive() to return only one of regular data and inline 1167 * out-of-band data in a single socket receive operation. 1168 */ 1169 moff = 0; 1170 offset = 0; 1171 while (m != NULL && uio->uio_resid > 0 && error == 0) { 1172 /* 1173 * If the type of mbuf has changed since the last mbuf 1174 * examined ('type'), end the receive operation. 1175 */ 1176 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1177 if (m->m_type == MT_OOBDATA) { 1178 if (type != MT_OOBDATA) 1179 break; 1180 } else if (type == MT_OOBDATA) 1181 break; 1182 else 1183 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER, 1184 ("m->m_type == %d", m->m_type)); 1185 so->so_rcv.sb_state &= ~SBS_RCVATMARK; 1186 len = uio->uio_resid; 1187 if (so->so_oobmark && len > so->so_oobmark - offset) 1188 len = so->so_oobmark - offset; 1189 if (len > m->m_len - moff) 1190 len = m->m_len - moff; 1191 /* 1192 * If mp is set, just pass back the mbufs. 1193 * Otherwise copy them out via the uio, then free. 1194 * Sockbuf must be consistent here (points to current mbuf, 1195 * it points to next record) when we drop priority; 1196 * we must note any additions to the sockbuf when we 1197 * block interrupts again. 1198 */ 1199 if (mp == NULL) { 1200 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1201 SBLASTRECORDCHK(&so->so_rcv); 1202 SBLASTMBUFCHK(&so->so_rcv); 1203 SOCKBUF_UNLOCK(&so->so_rcv); 1204#ifdef ZERO_COPY_SOCKETS 1205 if (so_zero_copy_receive) { 1206 vm_page_t pg; 1207 int disposable; 1208 1209 if ((m->m_flags & M_EXT) 1210 && (m->m_ext.ext_type == EXT_DISPOSABLE)) 1211 disposable = 1; 1212 else 1213 disposable = 0; 1214 1215 pg = PHYS_TO_VM_PAGE(vtophys(mtod(m, caddr_t) + 1216 moff)); 1217 1218 if (uio->uio_offset == -1) 1219 uio->uio_offset =IDX_TO_OFF(pg->pindex); 1220 1221 error = uiomoveco(mtod(m, char *) + moff, 1222 (int)len, uio,pg->object, 1223 disposable); 1224 } else 1225#endif /* ZERO_COPY_SOCKETS */ 1226 error = uiomove(mtod(m, char *) + moff, (int)len, uio); 1227 SOCKBUF_LOCK(&so->so_rcv); 1228 if (error) 1229 goto release; 1230 } else 1231 uio->uio_resid -= len; 1232 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1233 if (len == m->m_len - moff) { 1234 if (m->m_flags & M_EOR) 1235 flags |= MSG_EOR; 1236 if (flags & MSG_PEEK) { 1237 m = m->m_next; 1238 moff = 0; 1239 } else { 1240 nextrecord = m->m_nextpkt; 1241 sbfree(&so->so_rcv, m); 1242 if (mp != NULL) { 1243 *mp = m; 1244 mp = &m->m_next; 1245 so->so_rcv.sb_mb = m = m->m_next; 1246 *mp = NULL; 1247 } else { 1248 so->so_rcv.sb_mb = m_free(m); 1249 m = so->so_rcv.sb_mb; 1250 } 1251 if (m != NULL) { 1252 m->m_nextpkt = nextrecord; 1253 if (nextrecord == NULL) 1254 so->so_rcv.sb_lastrecord = m; 1255 } else { 1256 so->so_rcv.sb_mb = nextrecord; 1257 SB_EMPTY_FIXUP(&so->so_rcv); 1258 } 1259 SBLASTRECORDCHK(&so->so_rcv); 1260 SBLASTMBUFCHK(&so->so_rcv); 1261 } 1262 } else { 1263 if (flags & MSG_PEEK) 1264 moff += len; 1265 else { 1266 if (mp != NULL) { 1267 SOCKBUF_UNLOCK(&so->so_rcv); 1268 *mp = m_copym(m, 0, len, M_TRYWAIT); 1269 SOCKBUF_LOCK(&so->so_rcv); 1270 } 1271 m->m_data += len; 1272 m->m_len -= len; 1273 so->so_rcv.sb_cc -= len; 1274 } 1275 } 1276 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1277 if (so->so_oobmark) { 1278 if ((flags & MSG_PEEK) == 0) { 1279 so->so_oobmark -= len; 1280 if (so->so_oobmark == 0) { 1281 so->so_rcv.sb_state |= SBS_RCVATMARK; 1282 break; 1283 } 1284 } else { 1285 offset += len; 1286 if (offset == so->so_oobmark) 1287 break; 1288 } 1289 } 1290 if (flags & MSG_EOR) 1291 break; 1292 /* 1293 * If the MSG_WAITALL flag is set (for non-atomic socket), 1294 * we must not quit until "uio->uio_resid == 0" or an error 1295 * termination. If a signal/timeout occurs, return 1296 * with a short count but without error. 1297 * Keep sockbuf locked against other readers. 1298 */ 1299 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 && 1300 !sosendallatonce(so) && nextrecord == NULL) { 1301 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1302 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE) 1303 break; 1304 /* 1305 * Notify the protocol that some data has been 1306 * drained before blocking. 1307 */ 1308 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb != NULL) { 1309 SOCKBUF_UNLOCK(&so->so_rcv); 1310 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1311 SOCKBUF_LOCK(&so->so_rcv); 1312 } 1313 SBLASTRECORDCHK(&so->so_rcv); 1314 SBLASTMBUFCHK(&so->so_rcv); 1315 error = sbwait(&so->so_rcv); 1316 if (error) 1317 goto release; 1318 m = so->so_rcv.sb_mb; 1319 if (m != NULL) 1320 nextrecord = m->m_nextpkt; 1321 } 1322 } 1323 1324 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1325 if (m != NULL && pr->pr_flags & PR_ATOMIC) { 1326 flags |= MSG_TRUNC; 1327 if ((flags & MSG_PEEK) == 0) 1328 (void) sbdroprecord_locked(&so->so_rcv); 1329 } 1330 if ((flags & MSG_PEEK) == 0) { 1331 if (m == NULL) { 1332 /* 1333 * First part is an inline SB_EMPTY_FIXUP(). Second 1334 * part makes sure sb_lastrecord is up-to-date if 1335 * there is still data in the socket buffer. 1336 */ 1337 so->so_rcv.sb_mb = nextrecord; 1338 if (so->so_rcv.sb_mb == NULL) { 1339 so->so_rcv.sb_mbtail = NULL; 1340 so->so_rcv.sb_lastrecord = NULL; 1341 } else if (nextrecord->m_nextpkt == NULL) 1342 so->so_rcv.sb_lastrecord = nextrecord; 1343 } 1344 SBLASTRECORDCHK(&so->so_rcv); 1345 SBLASTMBUFCHK(&so->so_rcv); 1346 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) { 1347 SOCKBUF_UNLOCK(&so->so_rcv); 1348 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1349 SOCKBUF_LOCK(&so->so_rcv); 1350 } 1351 } 1352 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1353 if (orig_resid == uio->uio_resid && orig_resid && 1354 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) { 1355 sbunlock(&so->so_rcv); 1356 goto restart; 1357 } 1358 1359 if (flagsp != NULL) 1360 *flagsp |= flags; 1361release: 1362 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1363 sbunlock(&so->so_rcv); 1364out: 1365 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1366 SOCKBUF_UNLOCK(&so->so_rcv); 1367 return (error); 1368} 1369 1370int 1371soshutdown(so, how) 1372 struct socket *so; 1373 int how; 1374{ 1375 struct protosw *pr = so->so_proto; 1376 1377 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) 1378 return (EINVAL); 1379 1380 if (how != SHUT_WR) 1381 sorflush(so); 1382 if (how != SHUT_RD) 1383 return ((*pr->pr_usrreqs->pru_shutdown)(so)); 1384 return (0); 1385} 1386 1387void 1388sorflush(so) 1389 struct socket *so; 1390{ 1391 struct sockbuf *sb = &so->so_rcv; 1392 struct protosw *pr = so->so_proto; 1393 struct sockbuf asb; 1394 1395 /* 1396 * XXXRW: This is quite ugly. The existing code made a copy of the 1397 * socket buffer, then zero'd the original to clear the buffer 1398 * fields. However, with mutexes in the socket buffer, this causes 1399 * problems. We only clear the zeroable bits of the original; 1400 * however, we have to initialize and destroy the mutex in the copy 1401 * so that dom_dispose() and sbrelease() can lock t as needed. 1402 */ 1403 SOCKBUF_LOCK(sb); 1404 sb->sb_flags |= SB_NOINTR; 1405 (void) sblock(sb, M_WAITOK); 1406 /* 1407 * socantrcvmore_locked() drops the socket buffer mutex so that it 1408 * can safely perform wakeups. Re-acquire the mutex before 1409 * continuing. 1410 */ 1411 socantrcvmore_locked(so); 1412 SOCKBUF_LOCK(sb); 1413 sbunlock(sb); 1414 /* 1415 * Invalidate/clear most of the sockbuf structure, but leave 1416 * selinfo and mutex data unchanged. 1417 */ 1418 bzero(&asb, offsetof(struct sockbuf, sb_startzero)); 1419 bcopy(&sb->sb_startzero, &asb.sb_startzero, 1420 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero)); 1421 bzero(&sb->sb_startzero, 1422 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero)); 1423 SOCKBUF_UNLOCK(sb); 1424 1425 SOCKBUF_LOCK_INIT(&asb, "so_rcv"); 1426 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL) 1427 (*pr->pr_domain->dom_dispose)(asb.sb_mb); 1428 sbrelease(&asb, so); 1429 SOCKBUF_LOCK_DESTROY(&asb); 1430} 1431 1432#ifdef INET 1433static int 1434do_setopt_accept_filter(so, sopt) 1435 struct socket *so; 1436 struct sockopt *sopt; 1437{ 1438 struct accept_filter_arg *afap = NULL; 1439 struct accept_filter *afp; 1440 struct so_accf *af = so->so_accf; 1441 int error = 0; 1442 1443 /* do not set/remove accept filters on non listen sockets */ 1444 if ((so->so_options & SO_ACCEPTCONN) == 0) { 1445 error = EINVAL; 1446 goto out; 1447 } 1448 1449 /* removing the filter */ 1450 if (sopt == NULL) { 1451 if (af != NULL) { 1452 if (af->so_accept_filter != NULL && 1453 af->so_accept_filter->accf_destroy != NULL) { 1454 af->so_accept_filter->accf_destroy(so); 1455 } 1456 if (af->so_accept_filter_str != NULL) { 1457 FREE(af->so_accept_filter_str, M_ACCF); 1458 } 1459 FREE(af, M_ACCF); 1460 so->so_accf = NULL; 1461 } 1462 so->so_options &= ~SO_ACCEPTFILTER; 1463 return (0); 1464 } 1465 /* adding a filter */ 1466 /* must remove previous filter first */ 1467 if (af != NULL) { 1468 error = EINVAL; 1469 goto out; 1470 } 1471 /* don't put large objects on the kernel stack */ 1472 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK); 1473 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap); 1474 afap->af_name[sizeof(afap->af_name)-1] = '\0'; 1475 afap->af_arg[sizeof(afap->af_arg)-1] = '\0'; 1476 if (error) 1477 goto out; 1478 afp = accept_filt_get(afap->af_name); 1479 if (afp == NULL) { 1480 error = ENOENT; 1481 goto out; 1482 } 1483 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK | M_ZERO); 1484 if (afp->accf_create != NULL) { 1485 if (afap->af_name[0] != '\0') { 1486 int len = strlen(afap->af_name) + 1; 1487 1488 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK); 1489 strcpy(af->so_accept_filter_str, afap->af_name); 1490 } 1491 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg); 1492 if (af->so_accept_filter_arg == NULL) { 1493 FREE(af->so_accept_filter_str, M_ACCF); 1494 FREE(af, M_ACCF); 1495 so->so_accf = NULL; 1496 error = EINVAL; 1497 goto out; 1498 } 1499 } 1500 af->so_accept_filter = afp; 1501 so->so_accf = af; 1502 so->so_options |= SO_ACCEPTFILTER; 1503out: 1504 if (afap != NULL) 1505 FREE(afap, M_TEMP); 1506 return (error); 1507} 1508#endif /* INET */ 1509 1510/* 1511 * Perhaps this routine, and sooptcopyout(), below, ought to come in 1512 * an additional variant to handle the case where the option value needs 1513 * to be some kind of integer, but not a specific size. 1514 * In addition to their use here, these functions are also called by the 1515 * protocol-level pr_ctloutput() routines. 1516 */ 1517int 1518sooptcopyin(sopt, buf, len, minlen) 1519 struct sockopt *sopt; 1520 void *buf; 1521 size_t len; 1522 size_t minlen; 1523{ 1524 size_t valsize; 1525 1526 /* 1527 * If the user gives us more than we wanted, we ignore it, 1528 * but if we don't get the minimum length the caller 1529 * wants, we return EINVAL. On success, sopt->sopt_valsize 1530 * is set to however much we actually retrieved. 1531 */ 1532 if ((valsize = sopt->sopt_valsize) < minlen) 1533 return EINVAL; 1534 if (valsize > len) 1535 sopt->sopt_valsize = valsize = len; 1536 1537 if (sopt->sopt_td != NULL) 1538 return (copyin(sopt->sopt_val, buf, valsize)); 1539 1540 bcopy(sopt->sopt_val, buf, valsize); 1541 return 0; 1542} 1543 1544int 1545sosetopt(so, sopt) 1546 struct socket *so; 1547 struct sockopt *sopt; 1548{ 1549 int error, optval; 1550 struct linger l; 1551 struct timeval tv; 1552 u_long val; 1553#ifdef MAC 1554 struct mac extmac; 1555#endif 1556 1557 error = 0; 1558 if (sopt->sopt_level != SOL_SOCKET) { 1559 if (so->so_proto && so->so_proto->pr_ctloutput) 1560 return ((*so->so_proto->pr_ctloutput) 1561 (so, sopt)); 1562 error = ENOPROTOOPT; 1563 } else { 1564 switch (sopt->sopt_name) { 1565#ifdef INET 1566 case SO_ACCEPTFILTER: 1567 error = do_setopt_accept_filter(so, sopt); 1568 if (error) 1569 goto bad; 1570 break; 1571#endif 1572 case SO_LINGER: 1573 error = sooptcopyin(sopt, &l, sizeof l, sizeof l); 1574 if (error) 1575 goto bad; 1576 1577 SOCK_LOCK(so); 1578 so->so_linger = l.l_linger; 1579 if (l.l_onoff) 1580 so->so_options |= SO_LINGER; 1581 else 1582 so->so_options &= ~SO_LINGER; 1583 SOCK_UNLOCK(so); 1584 break; 1585 1586 case SO_DEBUG: 1587 case SO_KEEPALIVE: 1588 case SO_DONTROUTE: 1589 case SO_USELOOPBACK: 1590 case SO_BROADCAST: 1591 case SO_REUSEADDR: 1592 case SO_REUSEPORT: 1593 case SO_OOBINLINE: 1594 case SO_TIMESTAMP: 1595 case SO_BINTIME: 1596 case SO_NOSIGPIPE: 1597 error = sooptcopyin(sopt, &optval, sizeof optval, 1598 sizeof optval); 1599 if (error) 1600 goto bad; 1601 SOCK_LOCK(so); 1602 if (optval) 1603 so->so_options |= sopt->sopt_name; 1604 else 1605 so->so_options &= ~sopt->sopt_name; 1606 SOCK_UNLOCK(so); 1607 break; 1608 1609 case SO_SNDBUF: 1610 case SO_RCVBUF: 1611 case SO_SNDLOWAT: 1612 case SO_RCVLOWAT: 1613 error = sooptcopyin(sopt, &optval, sizeof optval, 1614 sizeof optval); 1615 if (error) 1616 goto bad; 1617 1618 /* 1619 * Values < 1 make no sense for any of these 1620 * options, so disallow them. 1621 */ 1622 if (optval < 1) { 1623 error = EINVAL; 1624 goto bad; 1625 } 1626 1627 switch (sopt->sopt_name) { 1628 case SO_SNDBUF: 1629 case SO_RCVBUF: 1630 if (sbreserve(sopt->sopt_name == SO_SNDBUF ? 1631 &so->so_snd : &so->so_rcv, (u_long)optval, 1632 so, curthread) == 0) { 1633 error = ENOBUFS; 1634 goto bad; 1635 } 1636 break; 1637 1638 /* 1639 * Make sure the low-water is never greater than 1640 * the high-water. 1641 */ 1642 case SO_SNDLOWAT: 1643 SOCKBUF_LOCK(&so->so_snd); 1644 so->so_snd.sb_lowat = 1645 (optval > so->so_snd.sb_hiwat) ? 1646 so->so_snd.sb_hiwat : optval; 1647 SOCKBUF_UNLOCK(&so->so_snd); 1648 break; 1649 case SO_RCVLOWAT: 1650 SOCKBUF_LOCK(&so->so_rcv); 1651 so->so_rcv.sb_lowat = 1652 (optval > so->so_rcv.sb_hiwat) ? 1653 so->so_rcv.sb_hiwat : optval; 1654 SOCKBUF_UNLOCK(&so->so_rcv); 1655 break; 1656 } 1657 break; 1658 1659 case SO_SNDTIMEO: 1660 case SO_RCVTIMEO: 1661 error = sooptcopyin(sopt, &tv, sizeof tv, 1662 sizeof tv); 1663 if (error) 1664 goto bad; 1665 1666 /* assert(hz > 0); */ 1667 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz || 1668 tv.tv_usec < 0 || tv.tv_usec >= 1000000) { 1669 error = EDOM; 1670 goto bad; 1671 } 1672 /* assert(tick > 0); */ 1673 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */ 1674 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick; 1675 if (val > SHRT_MAX) { 1676 error = EDOM; 1677 goto bad; 1678 } 1679 if (val == 0 && tv.tv_usec != 0) 1680 val = 1; 1681 1682 switch (sopt->sopt_name) { 1683 case SO_SNDTIMEO: 1684 so->so_snd.sb_timeo = val; 1685 break; 1686 case SO_RCVTIMEO: 1687 so->so_rcv.sb_timeo = val; 1688 break; 1689 } 1690 break; 1691 case SO_LABEL: 1692#ifdef MAC 1693 error = sooptcopyin(sopt, &extmac, sizeof extmac, 1694 sizeof extmac); 1695 if (error) 1696 goto bad; 1697 error = mac_setsockopt_label(sopt->sopt_td->td_ucred, 1698 so, &extmac); 1699#else 1700 error = EOPNOTSUPP; 1701#endif 1702 break; 1703 default: 1704 error = ENOPROTOOPT; 1705 break; 1706 } 1707 if (error == 0 && so->so_proto != NULL && 1708 so->so_proto->pr_ctloutput != NULL) { 1709 (void) ((*so->so_proto->pr_ctloutput) 1710 (so, sopt)); 1711 } 1712 } 1713bad: 1714 return (error); 1715} 1716 1717/* Helper routine for getsockopt */ 1718int 1719sooptcopyout(struct sockopt *sopt, const void *buf, size_t len) 1720{ 1721 int error; 1722 size_t valsize; 1723 1724 error = 0; 1725 1726 /* 1727 * Documented get behavior is that we always return a value, 1728 * possibly truncated to fit in the user's buffer. 1729 * Traditional behavior is that we always tell the user 1730 * precisely how much we copied, rather than something useful 1731 * like the total amount we had available for her. 1732 * Note that this interface is not idempotent; the entire answer must 1733 * generated ahead of time. 1734 */ 1735 valsize = min(len, sopt->sopt_valsize); 1736 sopt->sopt_valsize = valsize; 1737 if (sopt->sopt_val != NULL) { 1738 if (sopt->sopt_td != NULL) 1739 error = copyout(buf, sopt->sopt_val, valsize); 1740 else 1741 bcopy(buf, sopt->sopt_val, valsize); 1742 } 1743 return error; 1744} 1745 1746int 1747sogetopt(so, sopt) 1748 struct socket *so; 1749 struct sockopt *sopt; 1750{ 1751 int error, optval; 1752 struct linger l; 1753 struct timeval tv; 1754#ifdef INET 1755 struct accept_filter_arg *afap; 1756#endif 1757#ifdef MAC 1758 struct mac extmac; 1759#endif 1760 1761 error = 0; 1762 if (sopt->sopt_level != SOL_SOCKET) { 1763 if (so->so_proto && so->so_proto->pr_ctloutput) { 1764 return ((*so->so_proto->pr_ctloutput) 1765 (so, sopt)); 1766 } else 1767 return (ENOPROTOOPT); 1768 } else { 1769 switch (sopt->sopt_name) { 1770#ifdef INET 1771 case SO_ACCEPTFILTER: 1772 if ((so->so_options & SO_ACCEPTCONN) == 0) 1773 return (EINVAL); 1774 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), 1775 M_TEMP, M_WAITOK | M_ZERO); 1776 if ((so->so_options & SO_ACCEPTFILTER) != 0) { 1777 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name); 1778 if (so->so_accf->so_accept_filter_str != NULL) 1779 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str); 1780 } 1781 error = sooptcopyout(sopt, afap, sizeof(*afap)); 1782 FREE(afap, M_TEMP); 1783 break; 1784#endif 1785 1786 case SO_LINGER: 1787 /* 1788 * XXXRW: We grab the lock here to get a consistent 1789 * snapshot of both fields. This may not really 1790 * be necessary. 1791 */ 1792 SOCK_LOCK(so); 1793 l.l_onoff = so->so_options & SO_LINGER; 1794 l.l_linger = so->so_linger; 1795 SOCK_UNLOCK(so); 1796 error = sooptcopyout(sopt, &l, sizeof l); 1797 break; 1798 1799 case SO_USELOOPBACK: 1800 case SO_DONTROUTE: 1801 case SO_DEBUG: 1802 case SO_KEEPALIVE: 1803 case SO_REUSEADDR: 1804 case SO_REUSEPORT: 1805 case SO_BROADCAST: 1806 case SO_OOBINLINE: 1807 case SO_TIMESTAMP: 1808 case SO_BINTIME: 1809 case SO_NOSIGPIPE: 1810 optval = so->so_options & sopt->sopt_name; 1811integer: 1812 error = sooptcopyout(sopt, &optval, sizeof optval); 1813 break; 1814 1815 case SO_TYPE: 1816 optval = so->so_type; 1817 goto integer; 1818 1819 case SO_ERROR: 1820 optval = so->so_error; 1821 so->so_error = 0; 1822 goto integer; 1823 1824 case SO_SNDBUF: 1825 optval = so->so_snd.sb_hiwat; 1826 goto integer; 1827 1828 case SO_RCVBUF: 1829 optval = so->so_rcv.sb_hiwat; 1830 goto integer; 1831 1832 case SO_SNDLOWAT: 1833 optval = so->so_snd.sb_lowat; 1834 goto integer; 1835 1836 case SO_RCVLOWAT: 1837 optval = so->so_rcv.sb_lowat; 1838 goto integer; 1839 1840 case SO_SNDTIMEO: 1841 case SO_RCVTIMEO: 1842 optval = (sopt->sopt_name == SO_SNDTIMEO ? 1843 so->so_snd.sb_timeo : so->so_rcv.sb_timeo); 1844 1845 tv.tv_sec = optval / hz; 1846 tv.tv_usec = (optval % hz) * tick; 1847 error = sooptcopyout(sopt, &tv, sizeof tv); 1848 break; 1849 case SO_LABEL: 1850#ifdef MAC 1851 error = sooptcopyin(sopt, &extmac, sizeof(extmac), 1852 sizeof(extmac)); 1853 if (error) 1854 return (error); 1855 error = mac_getsockopt_label(sopt->sopt_td->td_ucred, 1856 so, &extmac); 1857 if (error) 1858 return (error); 1859 error = sooptcopyout(sopt, &extmac, sizeof extmac); 1860#else 1861 error = EOPNOTSUPP; 1862#endif 1863 break; 1864 case SO_PEERLABEL: 1865#ifdef MAC 1866 error = sooptcopyin(sopt, &extmac, sizeof(extmac), 1867 sizeof(extmac)); 1868 if (error) 1869 return (error); 1870 error = mac_getsockopt_peerlabel( 1871 sopt->sopt_td->td_ucred, so, &extmac); 1872 if (error) 1873 return (error); 1874 error = sooptcopyout(sopt, &extmac, sizeof extmac); 1875#else 1876 error = EOPNOTSUPP; 1877#endif 1878 break; 1879 default: 1880 error = ENOPROTOOPT; 1881 break; 1882 } 1883 return (error); 1884 } 1885} 1886 1887/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 1888int 1889soopt_getm(struct sockopt *sopt, struct mbuf **mp) 1890{ 1891 struct mbuf *m, *m_prev; 1892 int sopt_size = sopt->sopt_valsize; 1893 1894 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 1895 if (m == NULL) 1896 return ENOBUFS; 1897 if (sopt_size > MLEN) { 1898 MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT); 1899 if ((m->m_flags & M_EXT) == 0) { 1900 m_free(m); 1901 return ENOBUFS; 1902 } 1903 m->m_len = min(MCLBYTES, sopt_size); 1904 } else { 1905 m->m_len = min(MLEN, sopt_size); 1906 } 1907 sopt_size -= m->m_len; 1908 *mp = m; 1909 m_prev = m; 1910 1911 while (sopt_size) { 1912 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 1913 if (m == NULL) { 1914 m_freem(*mp); 1915 return ENOBUFS; 1916 } 1917 if (sopt_size > MLEN) { 1918 MCLGET(m, sopt->sopt_td != NULL ? M_TRYWAIT : 1919 M_DONTWAIT); 1920 if ((m->m_flags & M_EXT) == 0) { 1921 m_freem(m); 1922 m_freem(*mp); 1923 return ENOBUFS; 1924 } 1925 m->m_len = min(MCLBYTES, sopt_size); 1926 } else { 1927 m->m_len = min(MLEN, sopt_size); 1928 } 1929 sopt_size -= m->m_len; 1930 m_prev->m_next = m; 1931 m_prev = m; 1932 } 1933 return 0; 1934} 1935 1936/* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */ 1937int 1938soopt_mcopyin(struct sockopt *sopt, struct mbuf *m) 1939{ 1940 struct mbuf *m0 = m; 1941 1942 if (sopt->sopt_val == NULL) 1943 return 0; 1944 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 1945 if (sopt->sopt_td != NULL) { 1946 int error; 1947 1948 error = copyin(sopt->sopt_val, mtod(m, char *), 1949 m->m_len); 1950 if (error != 0) { 1951 m_freem(m0); 1952 return(error); 1953 } 1954 } else 1955 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len); 1956 sopt->sopt_valsize -= m->m_len; 1957 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len; 1958 m = m->m_next; 1959 } 1960 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */ 1961 panic("ip6_sooptmcopyin"); 1962 return 0; 1963} 1964 1965/* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */ 1966int 1967soopt_mcopyout(struct sockopt *sopt, struct mbuf *m) 1968{ 1969 struct mbuf *m0 = m; 1970 size_t valsize = 0; 1971 1972 if (sopt->sopt_val == NULL) 1973 return 0; 1974 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 1975 if (sopt->sopt_td != NULL) { 1976 int error; 1977 1978 error = copyout(mtod(m, char *), sopt->sopt_val, 1979 m->m_len); 1980 if (error != 0) { 1981 m_freem(m0); 1982 return(error); 1983 } 1984 } else 1985 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len); 1986 sopt->sopt_valsize -= m->m_len; 1987 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len; 1988 valsize += m->m_len; 1989 m = m->m_next; 1990 } 1991 if (m != NULL) { 1992 /* enough soopt buffer should be given from user-land */ 1993 m_freem(m0); 1994 return(EINVAL); 1995 } 1996 sopt->sopt_valsize = valsize; 1997 return 0; 1998} 1999 2000void 2001sohasoutofband(so) 2002 struct socket *so; 2003{ 2004 if (so->so_sigio != NULL) 2005 pgsigio(&so->so_sigio, SIGURG, 0); 2006 selwakeuppri(&so->so_rcv.sb_sel, PSOCK); 2007} 2008 2009int 2010sopoll(struct socket *so, int events, struct ucred *active_cred, 2011 struct thread *td) 2012{ 2013 int revents = 0; 2014 2015 if (events & (POLLIN | POLLRDNORM)) 2016 if (soreadable(so)) 2017 revents |= events & (POLLIN | POLLRDNORM); 2018 2019 if (events & POLLINIGNEOF) 2020 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat || 2021 !TAILQ_EMPTY(&so->so_comp) || so->so_error) 2022 revents |= POLLINIGNEOF; 2023 2024 if (events & (POLLOUT | POLLWRNORM)) 2025 if (sowriteable(so)) 2026 revents |= events & (POLLOUT | POLLWRNORM); 2027 2028 if (events & (POLLPRI | POLLRDBAND)) 2029 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK)) 2030 revents |= events & (POLLPRI | POLLRDBAND); 2031 2032 if (revents == 0) { 2033 if (events & 2034 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | 2035 POLLRDBAND)) { 2036 SOCKBUF_LOCK(&so->so_rcv); 2037 selrecord(td, &so->so_rcv.sb_sel); 2038 so->so_rcv.sb_flags |= SB_SEL; 2039 SOCKBUF_UNLOCK(&so->so_rcv); 2040 } 2041 2042 if (events & (POLLOUT | POLLWRNORM)) { 2043 SOCKBUF_LOCK(&so->so_snd); 2044 selrecord(td, &so->so_snd.sb_sel); 2045 so->so_snd.sb_flags |= SB_SEL; 2046 SOCKBUF_UNLOCK(&so->so_snd); 2047 } 2048 } 2049 2050 return (revents); 2051} 2052 2053int 2054soo_kqfilter(struct file *fp, struct knote *kn) 2055{ 2056 struct socket *so = kn->kn_fp->f_data; 2057 struct sockbuf *sb; 2058 2059 switch (kn->kn_filter) { 2060 case EVFILT_READ: 2061 if (so->so_options & SO_ACCEPTCONN) 2062 kn->kn_fop = &solisten_filtops; 2063 else 2064 kn->kn_fop = &soread_filtops; 2065 sb = &so->so_rcv; 2066 break; 2067 case EVFILT_WRITE: 2068 kn->kn_fop = &sowrite_filtops; 2069 sb = &so->so_snd; 2070 break; 2071 default: 2072 return (1); 2073 } 2074 2075 SOCKBUF_LOCK(sb); 2076 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext); 2077 sb->sb_flags |= SB_KNOTE; 2078 SOCKBUF_UNLOCK(sb); 2079 return (0); 2080} 2081 2082static void 2083filt_sordetach(struct knote *kn) 2084{ 2085 struct socket *so = kn->kn_fp->f_data; 2086 2087 SOCKBUF_LOCK(&so->so_rcv); 2088 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext); 2089 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note)) 2090 so->so_rcv.sb_flags &= ~SB_KNOTE; 2091 SOCKBUF_UNLOCK(&so->so_rcv); 2092} 2093 2094/*ARGSUSED*/ 2095static int 2096filt_soread(struct knote *kn, long hint) 2097{ 2098 struct socket *so = kn->kn_fp->f_data; 2099 int need_lock, result; 2100 2101 /* 2102 * XXXRW: Conditional locking because filt_soread() can be called 2103 * either from KNOTE() in the socket context where the socket buffer 2104 * lock is already held, or from kqueue() itself. 2105 */ 2106 need_lock = !SOCKBUF_OWNED(&so->so_rcv); 2107 if (need_lock) 2108 SOCKBUF_LOCK(&so->so_rcv); 2109 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl; 2110 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 2111 kn->kn_flags |= EV_EOF; 2112 kn->kn_fflags = so->so_error; 2113 result = 1; 2114 } else if (so->so_error) /* temporary udp error */ 2115 result = 1; 2116 else if (kn->kn_sfflags & NOTE_LOWAT) 2117 result = (kn->kn_data >= kn->kn_sdata); 2118 else 2119 result = (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat); 2120 if (need_lock) 2121 SOCKBUF_UNLOCK(&so->so_rcv); 2122 return (result); 2123} 2124 2125static void 2126filt_sowdetach(struct knote *kn) 2127{ 2128 struct socket *so = kn->kn_fp->f_data; 2129 2130 SOCKBUF_LOCK(&so->so_snd); 2131 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext); 2132 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note)) 2133 so->so_snd.sb_flags &= ~SB_KNOTE; 2134 SOCKBUF_UNLOCK(&so->so_snd); 2135} 2136 2137/*ARGSUSED*/ 2138static int 2139filt_sowrite(struct knote *kn, long hint) 2140{ 2141 struct socket *so = kn->kn_fp->f_data; 2142 int need_lock, result; 2143 2144 /* 2145 * XXXRW: Conditional locking because filt_soread() can be called 2146 * either from KNOTE() in the socket context where the socket buffer 2147 * lock is already held, or from kqueue() itself. 2148 */ 2149 need_lock = !SOCKBUF_OWNED(&so->so_snd); 2150 if (need_lock) 2151 SOCKBUF_LOCK(&so->so_snd); 2152 kn->kn_data = sbspace(&so->so_snd); 2153 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 2154 kn->kn_flags |= EV_EOF; 2155 kn->kn_fflags = so->so_error; 2156 result = 1; 2157 } else if (so->so_error) /* temporary udp error */ 2158 result = 1; 2159 else if (((so->so_state & SS_ISCONNECTED) == 0) && 2160 (so->so_proto->pr_flags & PR_CONNREQUIRED)) 2161 result = 0; 2162 else if (kn->kn_sfflags & NOTE_LOWAT) 2163 result = (kn->kn_data >= kn->kn_sdata); 2164 else 2165 result = (kn->kn_data >= so->so_snd.sb_lowat); 2166 if (need_lock) 2167 SOCKBUF_UNLOCK(&so->so_snd); 2168 return (result); 2169} 2170 2171/*ARGSUSED*/ 2172static int 2173filt_solisten(struct knote *kn, long hint) 2174{ 2175 struct socket *so = kn->kn_fp->f_data; 2176 2177 kn->kn_data = so->so_qlen; 2178 return (! TAILQ_EMPTY(&so->so_comp)); 2179} 2180 2181int 2182socheckuid(struct socket *so, uid_t uid) 2183{ 2184 2185 if (so == NULL) 2186 return (EPERM); 2187 if (so->so_cred->cr_uid == uid) 2188 return (0); 2189 return (EPERM); 2190} 2191