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