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 #ifdef ZERO_COPY_SOCKETS 579 struct so_zerocopy_stats{ 580 int size_ok; 581 int align_ok; 582 int found_ifp; 583 }; 584 struct so_zerocopy_stats so_zerocp_stats = {0,0,0}; 585 #include <netinet/in.h> 586 #include <net/route.h> 587 #include <netinet/in_pcb.h> 588 #include <vm/vm.h> 589 #include <vm/vm_page.h> 590 #include <vm/vm_object.h> 591 #endif /*ZERO_COPY_SOCKETS*/ 592 593 /* 594 * sosend_copyin() accepts a uio and prepares an mbuf chain holding part or 595 * all of the data referenced by the uio. If desired, it uses zero-copy. 596 * *space will be updated to reflect data copied in. 597 * 598 * NB: If atomic I/O is requested, the caller must already have checked that 599 * space can hold resid bytes. 600 * 601 * NB: In the event of an error, the caller may need to free the partial 602 * chain pointed to by *mpp. The contents of both *uio and *space may be 603 * modified even in the case of an error. 604 */ 605 static int 606 sosend_copyin(struct uio *uio, struct mbuf **retmp, int atomic, long *space, 607 int flags) 608 { 609 struct mbuf *m, **mp, *top; 610 long len, resid; 611 int error; 612 #ifdef ZERO_COPY_SOCKETS 613 int cow_send; 614 #endif 615 616 *retmp = top = NULL; 617 mp = ⊤ 618 len = 0; 619 resid = uio->uio_resid; 620 error = 0; 621 do { 622 #ifdef ZERO_COPY_SOCKETS 623 cow_send = 0; 624 #endif /* ZERO_COPY_SOCKETS */ 625 if (resid >= MINCLSIZE) { 626 #ifdef ZERO_COPY_SOCKETS 627 if (top == NULL) { 628 MGETHDR(m, M_TRYWAIT, MT_DATA); 629 if (m == NULL) { 630 error = ENOBUFS; 631 goto out; 632 } 633 m->m_pkthdr.len = 0; 634 m->m_pkthdr.rcvif = NULL; 635 } else { 636 MGET(m, M_TRYWAIT, MT_DATA); 637 if (m == NULL) { 638 error = ENOBUFS; 639 goto out; 640 } 641 } 642 if (so_zero_copy_send && 643 resid>=PAGE_SIZE && 644 *space>=PAGE_SIZE && 645 uio->uio_iov->iov_len>=PAGE_SIZE) { 646 so_zerocp_stats.size_ok++; 647 so_zerocp_stats.align_ok++; 648 cow_send = socow_setup(m, uio); 649 len = cow_send; 650 } 651 if (!cow_send) { 652 MCLGET(m, M_TRYWAIT); 653 if ((m->m_flags & M_EXT) == 0) { 654 m_free(m); 655 m = NULL; 656 } else { 657 len = min(min(MCLBYTES, resid), 658 *space); 659 } 660 } 661 #else /* ZERO_COPY_SOCKETS */ 662 if (top == NULL) { 663 m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR); 664 m->m_pkthdr.len = 0; 665 m->m_pkthdr.rcvif = NULL; 666 } else 667 m = m_getcl(M_TRYWAIT, MT_DATA, 0); 668 len = min(min(MCLBYTES, resid), *space); 669 #endif /* ZERO_COPY_SOCKETS */ 670 } else { 671 if (top == NULL) { 672 m = m_gethdr(M_TRYWAIT, MT_DATA); 673 m->m_pkthdr.len = 0; 674 m->m_pkthdr.rcvif = NULL; 675 676 len = min(min(MHLEN, resid), *space); 677 /* 678 * For datagram protocols, leave room 679 * for protocol headers in first mbuf. 680 */ 681 if (atomic && m && len < MHLEN) 682 MH_ALIGN(m, len); 683 } else { 684 m = m_get(M_TRYWAIT, MT_DATA); 685 len = min(min(MLEN, resid), *space); 686 } 687 } 688 if (m == NULL) { 689 error = ENOBUFS; 690 goto out; 691 } 692 693 *space -= len; 694 #ifdef ZERO_COPY_SOCKETS 695 if (cow_send) 696 error = 0; 697 else 698 #endif /* ZERO_COPY_SOCKETS */ 699 error = uiomove(mtod(m, void *), (int)len, uio); 700 resid = uio->uio_resid; 701 m->m_len = len; 702 *mp = m; 703 top->m_pkthdr.len += len; 704 if (error) 705 goto out; 706 mp = &m->m_next; 707 if (resid <= 0) { 708 if (flags & MSG_EOR) 709 top->m_flags |= M_EOR; 710 break; 711 } 712 } while (*space > 0 && atomic); 713 out: 714 *retmp = top; 715 return (error); 716 } 717 718 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 719 720 int 721 sosend_dgram(so, addr, uio, top, control, flags, td) 722 struct socket *so; 723 struct sockaddr *addr; 724 struct uio *uio; 725 struct mbuf *top; 726 struct mbuf *control; 727 int flags; 728 struct thread *td; 729 { 730 long space, resid; 731 int clen = 0, error, dontroute; 732 int atomic = sosendallatonce(so) || top; 733 734 KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM")); 735 KASSERT(so->so_proto->pr_flags & PR_ATOMIC, 736 ("sodgram_send: !PR_ATOMIC")); 737 738 if (uio != NULL) 739 resid = uio->uio_resid; 740 else 741 resid = top->m_pkthdr.len; 742 /* 743 * In theory resid should be unsigned. 744 * However, space must be signed, as it might be less than 0 745 * if we over-committed, and we must use a signed comparison 746 * of space and resid. On the other hand, a negative resid 747 * causes us to loop sending 0-length segments to the protocol. 748 * 749 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 750 * type sockets since that's an error. 751 */ 752 if (resid < 0) { 753 error = EINVAL; 754 goto out; 755 } 756 757 dontroute = 758 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0; 759 if (td != NULL) 760 td->td_proc->p_stats->p_ru.ru_msgsnd++; 761 if (control != NULL) 762 clen = control->m_len; 763 764 SOCKBUF_LOCK(&so->so_snd); 765 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 766 SOCKBUF_UNLOCK(&so->so_snd); 767 error = EPIPE; 768 goto out; 769 } 770 if (so->so_error) { 771 error = so->so_error; 772 so->so_error = 0; 773 SOCKBUF_UNLOCK(&so->so_snd); 774 goto out; 775 } 776 if ((so->so_state & SS_ISCONNECTED) == 0) { 777 /* 778 * `sendto' and `sendmsg' is allowed on a connection- 779 * based socket if it supports implied connect. 780 * Return ENOTCONN if not connected and no address is 781 * supplied. 782 */ 783 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 784 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 785 if ((so->so_state & SS_ISCONFIRMING) == 0 && 786 !(resid == 0 && clen != 0)) { 787 SOCKBUF_UNLOCK(&so->so_snd); 788 error = ENOTCONN; 789 goto out; 790 } 791 } else if (addr == NULL) { 792 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 793 error = ENOTCONN; 794 else 795 error = EDESTADDRREQ; 796 SOCKBUF_UNLOCK(&so->so_snd); 797 goto out; 798 } 799 } 800 801 /* 802 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a 803 * problem and need fixing. 804 */ 805 space = sbspace(&so->so_snd); 806 if (flags & MSG_OOB) 807 space += 1024; 808 space -= clen; 809 if (resid > space) { 810 error = EMSGSIZE; 811 goto out; 812 } 813 SOCKBUF_UNLOCK(&so->so_snd); 814 if (uio == NULL) { 815 resid = 0; 816 if (flags & MSG_EOR) 817 top->m_flags |= M_EOR; 818 } else { 819 error = sosend_copyin(uio, &top, atomic, &space, flags); 820 if (error) 821 goto out; 822 resid = uio->uio_resid; 823 } 824 KASSERT(resid == 0, ("sosend_dgram: resid != 0")); 825 /* 826 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock 827 * than with. 828 */ 829 if (dontroute) { 830 SOCK_LOCK(so); 831 so->so_options |= SO_DONTROUTE; 832 SOCK_UNLOCK(so); 833 } 834 /* 835 * XXX all the SBS_CANTSENDMORE checks previously 836 * done could be out of date. We could have recieved 837 * a reset packet in an interrupt or maybe we slept 838 * while doing page faults in uiomove() etc. We could 839 * probably recheck again inside the locking protection 840 * here, but there are probably other places that this 841 * also happens. We must rethink this. 842 */ 843 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 844 (flags & MSG_OOB) ? PRUS_OOB : 845 /* 846 * If the user set MSG_EOF, the protocol 847 * understands this flag and nothing left to 848 * send then use PRU_SEND_EOF instead of PRU_SEND. 849 */ 850 ((flags & MSG_EOF) && 851 (so->so_proto->pr_flags & PR_IMPLOPCL) && 852 (resid <= 0)) ? 853 PRUS_EOF : 854 /* If there is more to send set PRUS_MORETOCOME */ 855 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 856 top, addr, control, td); 857 if (dontroute) { 858 SOCK_LOCK(so); 859 so->so_options &= ~SO_DONTROUTE; 860 SOCK_UNLOCK(so); 861 } 862 clen = 0; 863 control = NULL; 864 top = NULL; 865 out: 866 if (top != NULL) 867 m_freem(top); 868 if (control != NULL) 869 m_freem(control); 870 return (error); 871 } 872 873 /* 874 * Send on a socket. 875 * If send must go all at once and message is larger than 876 * send buffering, then hard error. 877 * Lock against other senders. 878 * If must go all at once and not enough room now, then 879 * inform user that this would block and do nothing. 880 * Otherwise, if nonblocking, send as much as possible. 881 * The data to be sent is described by "uio" if nonzero, 882 * otherwise by the mbuf chain "top" (which must be null 883 * if uio is not). Data provided in mbuf chain must be small 884 * enough to send all at once. 885 * 886 * Returns nonzero on error, timeout or signal; callers 887 * must check for short counts if EINTR/ERESTART are returned. 888 * Data and control buffers are freed on return. 889 */ 890 #define snderr(errno) { error = (errno); goto release; } 891 int 892 sosend(so, addr, uio, top, control, flags, td) 893 struct socket *so; 894 struct sockaddr *addr; 895 struct uio *uio; 896 struct mbuf *top; 897 struct mbuf *control; 898 int flags; 899 struct thread *td; 900 { 901 long space, resid; 902 int clen = 0, error, dontroute; 903 int atomic = sosendallatonce(so) || top; 904 905 if (uio != NULL) 906 resid = uio->uio_resid; 907 else 908 resid = top->m_pkthdr.len; 909 /* 910 * In theory resid should be unsigned. 911 * However, space must be signed, as it might be less than 0 912 * if we over-committed, and we must use a signed comparison 913 * of space and resid. On the other hand, a negative resid 914 * causes us to loop sending 0-length segments to the protocol. 915 * 916 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 917 * type sockets since that's an error. 918 */ 919 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) { 920 error = EINVAL; 921 goto out; 922 } 923 924 dontroute = 925 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 926 (so->so_proto->pr_flags & PR_ATOMIC); 927 if (td != NULL) 928 td->td_proc->p_stats->p_ru.ru_msgsnd++; 929 if (control != NULL) 930 clen = control->m_len; 931 932 SOCKBUF_LOCK(&so->so_snd); 933 restart: 934 SOCKBUF_LOCK_ASSERT(&so->so_snd); 935 error = sblock(&so->so_snd, SBLOCKWAIT(flags)); 936 if (error) 937 goto out_locked; 938 do { 939 SOCKBUF_LOCK_ASSERT(&so->so_snd); 940 if (so->so_snd.sb_state & SBS_CANTSENDMORE) 941 snderr(EPIPE); 942 if (so->so_error) { 943 error = so->so_error; 944 so->so_error = 0; 945 goto release; 946 } 947 if ((so->so_state & SS_ISCONNECTED) == 0) { 948 /* 949 * `sendto' and `sendmsg' is allowed on a connection- 950 * based socket if it supports implied connect. 951 * Return ENOTCONN if not connected and no address is 952 * supplied. 953 */ 954 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 955 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 956 if ((so->so_state & SS_ISCONFIRMING) == 0 && 957 !(resid == 0 && clen != 0)) 958 snderr(ENOTCONN); 959 } else if (addr == NULL) 960 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ? 961 ENOTCONN : EDESTADDRREQ); 962 } 963 space = sbspace(&so->so_snd); 964 if (flags & MSG_OOB) 965 space += 1024; 966 if ((atomic && resid > so->so_snd.sb_hiwat) || 967 clen > so->so_snd.sb_hiwat) 968 snderr(EMSGSIZE); 969 if (space < resid + clen && 970 (atomic || space < so->so_snd.sb_lowat || space < clen)) { 971 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) 972 snderr(EWOULDBLOCK); 973 sbunlock(&so->so_snd); 974 error = sbwait(&so->so_snd); 975 if (error) 976 goto out_locked; 977 goto restart; 978 } 979 SOCKBUF_UNLOCK(&so->so_snd); 980 space -= clen; 981 do { 982 if (uio == NULL) { 983 resid = 0; 984 if (flags & MSG_EOR) 985 top->m_flags |= M_EOR; 986 } else { 987 error = sosend_copyin(uio, &top, atomic, 988 &space, flags); 989 if (error != 0) { 990 SOCKBUF_LOCK(&so->so_snd); 991 goto release; 992 } 993 resid = uio->uio_resid; 994 } 995 if (dontroute) { 996 SOCK_LOCK(so); 997 so->so_options |= SO_DONTROUTE; 998 SOCK_UNLOCK(so); 999 } 1000 /* 1001 * XXX all the SBS_CANTSENDMORE checks previously 1002 * done could be out of date. We could have recieved 1003 * a reset packet in an interrupt or maybe we slept 1004 * while doing page faults in uiomove() etc. We could 1005 * probably recheck again inside the locking protection 1006 * here, but there are probably other places that this 1007 * also happens. We must rethink this. 1008 */ 1009 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 1010 (flags & MSG_OOB) ? PRUS_OOB : 1011 /* 1012 * If the user set MSG_EOF, the protocol 1013 * understands this flag and nothing left to 1014 * send then use PRU_SEND_EOF instead of PRU_SEND. 1015 */ 1016 ((flags & MSG_EOF) && 1017 (so->so_proto->pr_flags & PR_IMPLOPCL) && 1018 (resid <= 0)) ? 1019 PRUS_EOF : 1020 /* If there is more to send set PRUS_MORETOCOME */ 1021 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 1022 top, addr, control, td); 1023 if (dontroute) { 1024 SOCK_LOCK(so); 1025 so->so_options &= ~SO_DONTROUTE; 1026 SOCK_UNLOCK(so); 1027 } 1028 clen = 0; 1029 control = NULL; 1030 top = NULL; 1031 if (error) { 1032 SOCKBUF_LOCK(&so->so_snd); 1033 goto release; 1034 } 1035 } while (resid && space > 0); 1036 SOCKBUF_LOCK(&so->so_snd); 1037 } while (resid); 1038 1039 release: 1040 SOCKBUF_LOCK_ASSERT(&so->so_snd); 1041 sbunlock(&so->so_snd); 1042 out_locked: 1043 SOCKBUF_LOCK_ASSERT(&so->so_snd); 1044 SOCKBUF_UNLOCK(&so->so_snd); 1045 out: 1046 if (top != NULL) 1047 m_freem(top); 1048 if (control != NULL) 1049 m_freem(control); 1050 return (error); 1051 } 1052 #undef snderr 1053 1054 /* 1055 * The part of soreceive() that implements reading non-inline out-of-band 1056 * data from a socket. For more complete comments, see soreceive(), from 1057 * which this code originated. 1058 * 1059 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is 1060 * unable to return an mbuf chain to the caller. 1061 */ 1062 static int 1063 soreceive_rcvoob(so, uio, flags) 1064 struct socket *so; 1065 struct uio *uio; 1066 int flags; 1067 { 1068 struct protosw *pr = so->so_proto; 1069 struct mbuf *m; 1070 int error; 1071 1072 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0")); 1073 1074 m = m_get(M_TRYWAIT, MT_DATA); 1075 if (m == NULL) 1076 return (ENOBUFS); 1077 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK); 1078 if (error) 1079 goto bad; 1080 do { 1081 #ifdef ZERO_COPY_SOCKETS 1082 if (so_zero_copy_receive) { 1083 int disposable; 1084 1085 if ((m->m_flags & M_EXT) 1086 && (m->m_ext.ext_type == EXT_DISPOSABLE)) 1087 disposable = 1; 1088 else 1089 disposable = 0; 1090 1091 error = uiomoveco(mtod(m, void *), 1092 min(uio->uio_resid, m->m_len), 1093 uio, disposable); 1094 } else 1095 #endif /* ZERO_COPY_SOCKETS */ 1096 error = uiomove(mtod(m, void *), 1097 (int) min(uio->uio_resid, m->m_len), uio); 1098 m = m_free(m); 1099 } while (uio->uio_resid && error == 0 && m); 1100 bad: 1101 if (m != NULL) 1102 m_freem(m); 1103 return (error); 1104 } 1105 1106 /* 1107 * Following replacement or removal of the first mbuf on the first mbuf chain 1108 * of a socket buffer, push necessary state changes back into the socket 1109 * buffer so that other consumers see the values consistently. 'nextrecord' 1110 * is the callers locally stored value of the original value of 1111 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes. 1112 * NOTE: 'nextrecord' may be NULL. 1113 */ 1114 static __inline void 1115 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord) 1116 { 1117 1118 SOCKBUF_LOCK_ASSERT(sb); 1119 /* 1120 * First, update for the new value of nextrecord. If necessary, make 1121 * it the first record. 1122 */ 1123 if (sb->sb_mb != NULL) 1124 sb->sb_mb->m_nextpkt = nextrecord; 1125 else 1126 sb->sb_mb = nextrecord; 1127 1128 /* 1129 * Now update any dependent socket buffer fields to reflect the new 1130 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the 1131 * addition of a second clause that takes care of the case where 1132 * sb_mb has been updated, but remains the last record. 1133 */ 1134 if (sb->sb_mb == NULL) { 1135 sb->sb_mbtail = NULL; 1136 sb->sb_lastrecord = NULL; 1137 } else if (sb->sb_mb->m_nextpkt == NULL) 1138 sb->sb_lastrecord = sb->sb_mb; 1139 } 1140 1141 1142 /* 1143 * Implement receive operations on a socket. 1144 * We depend on the way that records are added to the sockbuf 1145 * by sbappend*. In particular, each record (mbufs linked through m_next) 1146 * must begin with an address if the protocol so specifies, 1147 * followed by an optional mbuf or mbufs containing ancillary data, 1148 * and then zero or more mbufs of data. 1149 * In order to avoid blocking network interrupts for the entire time here, 1150 * we splx() while doing the actual copy to user space. 1151 * Although the sockbuf is locked, new data may still be appended, 1152 * and thus we must maintain consistency of the sockbuf during that time. 1153 * 1154 * The caller may receive the data as a single mbuf chain by supplying 1155 * an mbuf **mp0 for use in returning the chain. The uio is then used 1156 * only for the count in uio_resid. 1157 */ 1158 int 1159 soreceive(so, psa, uio, mp0, controlp, flagsp) 1160 struct socket *so; 1161 struct sockaddr **psa; 1162 struct uio *uio; 1163 struct mbuf **mp0; 1164 struct mbuf **controlp; 1165 int *flagsp; 1166 { 1167 struct mbuf *m, **mp; 1168 int flags, len, error, offset; 1169 struct protosw *pr = so->so_proto; 1170 struct mbuf *nextrecord; 1171 int moff, type = 0; 1172 int orig_resid = uio->uio_resid; 1173 1174 mp = mp0; 1175 if (psa != NULL) 1176 *psa = NULL; 1177 if (controlp != NULL) 1178 *controlp = NULL; 1179 if (flagsp != NULL) 1180 flags = *flagsp &~ MSG_EOR; 1181 else 1182 flags = 0; 1183 if (flags & MSG_OOB) 1184 return (soreceive_rcvoob(so, uio, flags)); 1185 if (mp != NULL) 1186 *mp = NULL; 1187 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING) 1188 && uio->uio_resid) 1189 (*pr->pr_usrreqs->pru_rcvd)(so, 0); 1190 1191 SOCKBUF_LOCK(&so->so_rcv); 1192 restart: 1193 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1194 error = sblock(&so->so_rcv, SBLOCKWAIT(flags)); 1195 if (error) 1196 goto out; 1197 1198 m = so->so_rcv.sb_mb; 1199 /* 1200 * If we have less data than requested, block awaiting more 1201 * (subject to any timeout) if: 1202 * 1. the current count is less than the low water mark, or 1203 * 2. MSG_WAITALL is set, and it is possible to do the entire 1204 * receive operation at once if we block (resid <= hiwat). 1205 * 3. MSG_DONTWAIT is not set 1206 * If MSG_WAITALL is set but resid is larger than the receive buffer, 1207 * we have to do the receive in sections, and thus risk returning 1208 * a short count if a timeout or signal occurs after we start. 1209 */ 1210 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && 1211 so->so_rcv.sb_cc < uio->uio_resid) && 1212 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 1213 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && 1214 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) { 1215 KASSERT(m != NULL || !so->so_rcv.sb_cc, 1216 ("receive: m == %p so->so_rcv.sb_cc == %u", 1217 m, so->so_rcv.sb_cc)); 1218 if (so->so_error) { 1219 if (m != NULL) 1220 goto dontblock; 1221 error = so->so_error; 1222 if ((flags & MSG_PEEK) == 0) 1223 so->so_error = 0; 1224 goto release; 1225 } 1226 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1227 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1228 if (m) 1229 goto dontblock; 1230 else 1231 goto release; 1232 } 1233 for (; m != NULL; m = m->m_next) 1234 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 1235 m = so->so_rcv.sb_mb; 1236 goto dontblock; 1237 } 1238 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 1239 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 1240 error = ENOTCONN; 1241 goto release; 1242 } 1243 if (uio->uio_resid == 0) 1244 goto release; 1245 if ((so->so_state & SS_NBIO) || 1246 (flags & (MSG_DONTWAIT|MSG_NBIO))) { 1247 error = EWOULDBLOCK; 1248 goto release; 1249 } 1250 SBLASTRECORDCHK(&so->so_rcv); 1251 SBLASTMBUFCHK(&so->so_rcv); 1252 sbunlock(&so->so_rcv); 1253 error = sbwait(&so->so_rcv); 1254 if (error) 1255 goto out; 1256 goto restart; 1257 } 1258 dontblock: 1259 /* 1260 * From this point onward, we maintain 'nextrecord' as a cache of the 1261 * pointer to the next record in the socket buffer. We must keep the 1262 * various socket buffer pointers and local stack versions of the 1263 * pointers in sync, pushing out modifications before dropping the 1264 * socket buffer mutex, and re-reading them when picking it up. 1265 * 1266 * Otherwise, we will race with the network stack appending new data 1267 * or records onto the socket buffer by using inconsistent/stale 1268 * versions of the field, possibly resulting in socket buffer 1269 * corruption. 1270 * 1271 * By holding the high-level sblock(), we prevent simultaneous 1272 * readers from pulling off the front of the socket buffer. 1273 */ 1274 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1275 if (uio->uio_td) 1276 uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++; 1277 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb")); 1278 SBLASTRECORDCHK(&so->so_rcv); 1279 SBLASTMBUFCHK(&so->so_rcv); 1280 nextrecord = m->m_nextpkt; 1281 if (pr->pr_flags & PR_ADDR) { 1282 KASSERT(m->m_type == MT_SONAME, 1283 ("m->m_type == %d", m->m_type)); 1284 orig_resid = 0; 1285 if (psa != NULL) 1286 *psa = sodupsockaddr(mtod(m, struct sockaddr *), 1287 M_NOWAIT); 1288 if (flags & MSG_PEEK) { 1289 m = m->m_next; 1290 } else { 1291 sbfree(&so->so_rcv, m); 1292 so->so_rcv.sb_mb = m_free(m); 1293 m = so->so_rcv.sb_mb; 1294 sockbuf_pushsync(&so->so_rcv, nextrecord); 1295 } 1296 } 1297 1298 /* 1299 * Process one or more MT_CONTROL mbufs present before any data mbufs 1300 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we 1301 * just copy the data; if !MSG_PEEK, we call into the protocol to 1302 * perform externalization (or freeing if controlp == NULL). 1303 */ 1304 if (m != NULL && m->m_type == MT_CONTROL) { 1305 struct mbuf *cm = NULL, *cmn; 1306 struct mbuf **cme = &cm; 1307 1308 do { 1309 if (flags & MSG_PEEK) { 1310 if (controlp != NULL) { 1311 *controlp = m_copy(m, 0, m->m_len); 1312 controlp = &(*controlp)->m_next; 1313 } 1314 m = m->m_next; 1315 } else { 1316 sbfree(&so->so_rcv, m); 1317 so->so_rcv.sb_mb = m->m_next; 1318 m->m_next = NULL; 1319 *cme = m; 1320 cme = &(*cme)->m_next; 1321 m = so->so_rcv.sb_mb; 1322 } 1323 } while (m != NULL && m->m_type == MT_CONTROL); 1324 if ((flags & MSG_PEEK) == 0) 1325 sockbuf_pushsync(&so->so_rcv, nextrecord); 1326 while (cm != NULL) { 1327 cmn = cm->m_next; 1328 cm->m_next = NULL; 1329 if (pr->pr_domain->dom_externalize != NULL) { 1330 SOCKBUF_UNLOCK(&so->so_rcv); 1331 error = (*pr->pr_domain->dom_externalize) 1332 (cm, controlp); 1333 SOCKBUF_LOCK(&so->so_rcv); 1334 } else if (controlp != NULL) 1335 *controlp = cm; 1336 else 1337 m_freem(cm); 1338 if (controlp != NULL) { 1339 orig_resid = 0; 1340 while (*controlp != NULL) 1341 controlp = &(*controlp)->m_next; 1342 } 1343 cm = cmn; 1344 } 1345 if (so->so_rcv.sb_mb) 1346 nextrecord = so->so_rcv.sb_mb->m_nextpkt; 1347 else 1348 nextrecord = NULL; 1349 orig_resid = 0; 1350 } 1351 if (m != NULL) { 1352 if ((flags & MSG_PEEK) == 0) { 1353 KASSERT(m->m_nextpkt == nextrecord, 1354 ("soreceive: post-control, nextrecord !sync")); 1355 if (nextrecord == NULL) { 1356 KASSERT(so->so_rcv.sb_mb == m, 1357 ("soreceive: post-control, sb_mb!=m")); 1358 KASSERT(so->so_rcv.sb_lastrecord == m, 1359 ("soreceive: post-control, lastrecord!=m")); 1360 } 1361 } 1362 type = m->m_type; 1363 if (type == MT_OOBDATA) 1364 flags |= MSG_OOB; 1365 } else { 1366 if ((flags & MSG_PEEK) == 0) { 1367 KASSERT(so->so_rcv.sb_mb == nextrecord, 1368 ("soreceive: sb_mb != nextrecord")); 1369 if (so->so_rcv.sb_mb == NULL) { 1370 KASSERT(so->so_rcv.sb_lastrecord == NULL, 1371 ("soreceive: sb_lastercord != NULL")); 1372 } 1373 } 1374 } 1375 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1376 SBLASTRECORDCHK(&so->so_rcv); 1377 SBLASTMBUFCHK(&so->so_rcv); 1378 1379 /* 1380 * Now continue to read any data mbufs off of the head of the socket 1381 * buffer until the read request is satisfied. Note that 'type' is 1382 * used to store the type of any mbuf reads that have happened so far 1383 * such that soreceive() can stop reading if the type changes, which 1384 * causes soreceive() to return only one of regular data and inline 1385 * out-of-band data in a single socket receive operation. 1386 */ 1387 moff = 0; 1388 offset = 0; 1389 while (m != NULL && uio->uio_resid > 0 && error == 0) { 1390 /* 1391 * If the type of mbuf has changed since the last mbuf 1392 * examined ('type'), end the receive operation. 1393 */ 1394 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1395 if (m->m_type == MT_OOBDATA) { 1396 if (type != MT_OOBDATA) 1397 break; 1398 } else if (type == MT_OOBDATA) 1399 break; 1400 else 1401 KASSERT(m->m_type == MT_DATA, 1402 ("m->m_type == %d", m->m_type)); 1403 so->so_rcv.sb_state &= ~SBS_RCVATMARK; 1404 len = uio->uio_resid; 1405 if (so->so_oobmark && len > so->so_oobmark - offset) 1406 len = so->so_oobmark - offset; 1407 if (len > m->m_len - moff) 1408 len = m->m_len - moff; 1409 /* 1410 * If mp is set, just pass back the mbufs. 1411 * Otherwise copy them out via the uio, then free. 1412 * Sockbuf must be consistent here (points to current mbuf, 1413 * it points to next record) when we drop priority; 1414 * we must note any additions to the sockbuf when we 1415 * block interrupts again. 1416 */ 1417 if (mp == NULL) { 1418 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1419 SBLASTRECORDCHK(&so->so_rcv); 1420 SBLASTMBUFCHK(&so->so_rcv); 1421 SOCKBUF_UNLOCK(&so->so_rcv); 1422 #ifdef ZERO_COPY_SOCKETS 1423 if (so_zero_copy_receive) { 1424 int disposable; 1425 1426 if ((m->m_flags & M_EXT) 1427 && (m->m_ext.ext_type == EXT_DISPOSABLE)) 1428 disposable = 1; 1429 else 1430 disposable = 0; 1431 1432 error = uiomoveco(mtod(m, char *) + moff, 1433 (int)len, uio, 1434 disposable); 1435 } else 1436 #endif /* ZERO_COPY_SOCKETS */ 1437 error = uiomove(mtod(m, char *) + moff, (int)len, uio); 1438 SOCKBUF_LOCK(&so->so_rcv); 1439 if (error) 1440 goto release; 1441 } else 1442 uio->uio_resid -= len; 1443 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1444 if (len == m->m_len - moff) { 1445 if (m->m_flags & M_EOR) 1446 flags |= MSG_EOR; 1447 if (flags & MSG_PEEK) { 1448 m = m->m_next; 1449 moff = 0; 1450 } else { 1451 nextrecord = m->m_nextpkt; 1452 sbfree(&so->so_rcv, m); 1453 if (mp != NULL) { 1454 *mp = m; 1455 mp = &m->m_next; 1456 so->so_rcv.sb_mb = m = m->m_next; 1457 *mp = NULL; 1458 } else { 1459 so->so_rcv.sb_mb = m_free(m); 1460 m = so->so_rcv.sb_mb; 1461 } 1462 sockbuf_pushsync(&so->so_rcv, nextrecord); 1463 SBLASTRECORDCHK(&so->so_rcv); 1464 SBLASTMBUFCHK(&so->so_rcv); 1465 } 1466 } else { 1467 if (flags & MSG_PEEK) 1468 moff += len; 1469 else { 1470 if (mp != NULL) { 1471 int copy_flag; 1472 1473 if (flags & MSG_DONTWAIT) 1474 copy_flag = M_DONTWAIT; 1475 else 1476 copy_flag = M_TRYWAIT; 1477 if (copy_flag == M_TRYWAIT) 1478 SOCKBUF_UNLOCK(&so->so_rcv); 1479 *mp = m_copym(m, 0, len, copy_flag); 1480 if (copy_flag == M_TRYWAIT) 1481 SOCKBUF_LOCK(&so->so_rcv); 1482 if (*mp == NULL) { 1483 /* 1484 * m_copym() couldn't allocate an mbuf. 1485 * Adjust uio_resid back (it was adjusted 1486 * down by len bytes, which we didn't end 1487 * up "copying" over). 1488 */ 1489 uio->uio_resid += len; 1490 break; 1491 } 1492 } 1493 m->m_data += len; 1494 m->m_len -= len; 1495 so->so_rcv.sb_cc -= len; 1496 } 1497 } 1498 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1499 if (so->so_oobmark) { 1500 if ((flags & MSG_PEEK) == 0) { 1501 so->so_oobmark -= len; 1502 if (so->so_oobmark == 0) { 1503 so->so_rcv.sb_state |= SBS_RCVATMARK; 1504 break; 1505 } 1506 } else { 1507 offset += len; 1508 if (offset == so->so_oobmark) 1509 break; 1510 } 1511 } 1512 if (flags & MSG_EOR) 1513 break; 1514 /* 1515 * If the MSG_WAITALL flag is set (for non-atomic socket), 1516 * we must not quit until "uio->uio_resid == 0" or an error 1517 * termination. If a signal/timeout occurs, return 1518 * with a short count but without error. 1519 * Keep sockbuf locked against other readers. 1520 */ 1521 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 && 1522 !sosendallatonce(so) && nextrecord == NULL) { 1523 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1524 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE) 1525 break; 1526 /* 1527 * Notify the protocol that some data has been 1528 * drained before blocking. 1529 */ 1530 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb != NULL) { 1531 SOCKBUF_UNLOCK(&so->so_rcv); 1532 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1533 SOCKBUF_LOCK(&so->so_rcv); 1534 } 1535 SBLASTRECORDCHK(&so->so_rcv); 1536 SBLASTMBUFCHK(&so->so_rcv); 1537 error = sbwait(&so->so_rcv); 1538 if (error) 1539 goto release; 1540 m = so->so_rcv.sb_mb; 1541 if (m != NULL) 1542 nextrecord = m->m_nextpkt; 1543 } 1544 } 1545 1546 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1547 if (m != NULL && pr->pr_flags & PR_ATOMIC) { 1548 flags |= MSG_TRUNC; 1549 if ((flags & MSG_PEEK) == 0) 1550 (void) sbdroprecord_locked(&so->so_rcv); 1551 } 1552 if ((flags & MSG_PEEK) == 0) { 1553 if (m == NULL) { 1554 /* 1555 * First part is an inline SB_EMPTY_FIXUP(). Second 1556 * part makes sure sb_lastrecord is up-to-date if 1557 * there is still data in the socket buffer. 1558 */ 1559 so->so_rcv.sb_mb = nextrecord; 1560 if (so->so_rcv.sb_mb == NULL) { 1561 so->so_rcv.sb_mbtail = NULL; 1562 so->so_rcv.sb_lastrecord = NULL; 1563 } else if (nextrecord->m_nextpkt == NULL) 1564 so->so_rcv.sb_lastrecord = nextrecord; 1565 } 1566 SBLASTRECORDCHK(&so->so_rcv); 1567 SBLASTMBUFCHK(&so->so_rcv); 1568 /* 1569 * If soreceive() is being done from the socket callback, then 1570 * don't need to generate ACK to peer to update window, since 1571 * ACK will be generated on return to TCP. 1572 */ 1573 if (!(flags & MSG_SOCALLBCK) && 1574 (pr->pr_flags & PR_WANTRCVD) && so->so_pcb) { 1575 SOCKBUF_UNLOCK(&so->so_rcv); 1576 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1577 SOCKBUF_LOCK(&so->so_rcv); 1578 } 1579 } 1580 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1581 if (orig_resid == uio->uio_resid && orig_resid && 1582 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) { 1583 sbunlock(&so->so_rcv); 1584 goto restart; 1585 } 1586 1587 if (flagsp != NULL) 1588 *flagsp |= flags; 1589 release: 1590 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1591 sbunlock(&so->so_rcv); 1592 out: 1593 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1594 SOCKBUF_UNLOCK(&so->so_rcv); 1595 return (error); 1596 } 1597 1598 int 1599 soshutdown(so, how) 1600 struct socket *so; 1601 int how; 1602 { 1603 struct protosw *pr = so->so_proto; 1604 1605 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) 1606 return (EINVAL); 1607 1608 if (how != SHUT_WR) 1609 sorflush(so); 1610 if (how != SHUT_RD) 1611 return ((*pr->pr_usrreqs->pru_shutdown)(so)); 1612 return (0); 1613 } 1614 1615 void 1616 sorflush(so) 1617 struct socket *so; 1618 { 1619 struct sockbuf *sb = &so->so_rcv; 1620 struct protosw *pr = so->so_proto; 1621 struct sockbuf asb; 1622 1623 /* 1624 * XXXRW: This is quite ugly. Previously, this code made a copy of 1625 * the socket buffer, then zero'd the original to clear the buffer 1626 * fields. However, with mutexes in the socket buffer, this causes 1627 * problems. We only clear the zeroable bits of the original; 1628 * however, we have to initialize and destroy the mutex in the copy 1629 * so that dom_dispose() and sbrelease() can lock t as needed. 1630 */ 1631 SOCKBUF_LOCK(sb); 1632 sb->sb_flags |= SB_NOINTR; 1633 (void) sblock(sb, M_WAITOK); 1634 /* 1635 * socantrcvmore_locked() drops the socket buffer mutex so that it 1636 * can safely perform wakeups. Re-acquire the mutex before 1637 * continuing. 1638 */ 1639 socantrcvmore_locked(so); 1640 SOCKBUF_LOCK(sb); 1641 sbunlock(sb); 1642 /* 1643 * Invalidate/clear most of the sockbuf structure, but leave 1644 * selinfo and mutex data unchanged. 1645 */ 1646 bzero(&asb, offsetof(struct sockbuf, sb_startzero)); 1647 bcopy(&sb->sb_startzero, &asb.sb_startzero, 1648 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero)); 1649 bzero(&sb->sb_startzero, 1650 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero)); 1651 SOCKBUF_UNLOCK(sb); 1652 1653 SOCKBUF_LOCK_INIT(&asb, "so_rcv"); 1654 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL) 1655 (*pr->pr_domain->dom_dispose)(asb.sb_mb); 1656 sbrelease(&asb, so); 1657 SOCKBUF_LOCK_DESTROY(&asb); 1658 } 1659 1660 /* 1661 * Perhaps this routine, and sooptcopyout(), below, ought to come in 1662 * an additional variant to handle the case where the option value needs 1663 * to be some kind of integer, but not a specific size. 1664 * In addition to their use here, these functions are also called by the 1665 * protocol-level pr_ctloutput() routines. 1666 */ 1667 int 1668 sooptcopyin(sopt, buf, len, minlen) 1669 struct sockopt *sopt; 1670 void *buf; 1671 size_t len; 1672 size_t minlen; 1673 { 1674 size_t valsize; 1675 1676 /* 1677 * If the user gives us more than we wanted, we ignore it, 1678 * but if we don't get the minimum length the caller 1679 * wants, we return EINVAL. On success, sopt->sopt_valsize 1680 * is set to however much we actually retrieved. 1681 */ 1682 if ((valsize = sopt->sopt_valsize) < minlen) 1683 return EINVAL; 1684 if (valsize > len) 1685 sopt->sopt_valsize = valsize = len; 1686 1687 if (sopt->sopt_td != NULL) 1688 return (copyin(sopt->sopt_val, buf, valsize)); 1689 1690 bcopy(sopt->sopt_val, buf, valsize); 1691 return (0); 1692 } 1693 1694 /* 1695 * Kernel version of setsockopt(2)/ 1696 * XXX: optlen is size_t, not socklen_t 1697 */ 1698 int 1699 so_setsockopt(struct socket *so, int level, int optname, void *optval, 1700 size_t optlen) 1701 { 1702 struct sockopt sopt; 1703 1704 sopt.sopt_level = level; 1705 sopt.sopt_name = optname; 1706 sopt.sopt_dir = SOPT_SET; 1707 sopt.sopt_val = optval; 1708 sopt.sopt_valsize = optlen; 1709 sopt.sopt_td = NULL; 1710 return (sosetopt(so, &sopt)); 1711 } 1712 1713 int 1714 sosetopt(so, sopt) 1715 struct socket *so; 1716 struct sockopt *sopt; 1717 { 1718 int error, optval; 1719 struct linger l; 1720 struct timeval tv; 1721 u_long val; 1722 #ifdef MAC 1723 struct mac extmac; 1724 #endif 1725 1726 error = 0; 1727 if (sopt->sopt_level != SOL_SOCKET) { 1728 if (so->so_proto && so->so_proto->pr_ctloutput) 1729 return ((*so->so_proto->pr_ctloutput) 1730 (so, sopt)); 1731 error = ENOPROTOOPT; 1732 } else { 1733 switch (sopt->sopt_name) { 1734 #ifdef INET 1735 case SO_ACCEPTFILTER: 1736 error = do_setopt_accept_filter(so, sopt); 1737 if (error) 1738 goto bad; 1739 break; 1740 #endif 1741 case SO_LINGER: 1742 error = sooptcopyin(sopt, &l, sizeof l, sizeof l); 1743 if (error) 1744 goto bad; 1745 1746 SOCK_LOCK(so); 1747 so->so_linger = l.l_linger; 1748 if (l.l_onoff) 1749 so->so_options |= SO_LINGER; 1750 else 1751 so->so_options &= ~SO_LINGER; 1752 SOCK_UNLOCK(so); 1753 break; 1754 1755 case SO_DEBUG: 1756 case SO_KEEPALIVE: 1757 case SO_DONTROUTE: 1758 case SO_USELOOPBACK: 1759 case SO_BROADCAST: 1760 case SO_REUSEADDR: 1761 case SO_REUSEPORT: 1762 case SO_OOBINLINE: 1763 case SO_TIMESTAMP: 1764 case SO_BINTIME: 1765 case SO_NOSIGPIPE: 1766 error = sooptcopyin(sopt, &optval, sizeof optval, 1767 sizeof optval); 1768 if (error) 1769 goto bad; 1770 SOCK_LOCK(so); 1771 if (optval) 1772 so->so_options |= sopt->sopt_name; 1773 else 1774 so->so_options &= ~sopt->sopt_name; 1775 SOCK_UNLOCK(so); 1776 break; 1777 1778 case SO_SNDBUF: 1779 case SO_RCVBUF: 1780 case SO_SNDLOWAT: 1781 case SO_RCVLOWAT: 1782 error = sooptcopyin(sopt, &optval, sizeof optval, 1783 sizeof optval); 1784 if (error) 1785 goto bad; 1786 1787 /* 1788 * Values < 1 make no sense for any of these 1789 * options, so disallow them. 1790 */ 1791 if (optval < 1) { 1792 error = EINVAL; 1793 goto bad; 1794 } 1795 1796 switch (sopt->sopt_name) { 1797 case SO_SNDBUF: 1798 case SO_RCVBUF: 1799 if (sbreserve(sopt->sopt_name == SO_SNDBUF ? 1800 &so->so_snd : &so->so_rcv, (u_long)optval, 1801 so, curthread) == 0) { 1802 error = ENOBUFS; 1803 goto bad; 1804 } 1805 break; 1806 1807 /* 1808 * Make sure the low-water is never greater than 1809 * the high-water. 1810 */ 1811 case SO_SNDLOWAT: 1812 SOCKBUF_LOCK(&so->so_snd); 1813 so->so_snd.sb_lowat = 1814 (optval > so->so_snd.sb_hiwat) ? 1815 so->so_snd.sb_hiwat : optval; 1816 SOCKBUF_UNLOCK(&so->so_snd); 1817 break; 1818 case SO_RCVLOWAT: 1819 SOCKBUF_LOCK(&so->so_rcv); 1820 so->so_rcv.sb_lowat = 1821 (optval > so->so_rcv.sb_hiwat) ? 1822 so->so_rcv.sb_hiwat : optval; 1823 SOCKBUF_UNLOCK(&so->so_rcv); 1824 break; 1825 } 1826 break; 1827 1828 case SO_SNDTIMEO: 1829 case SO_RCVTIMEO: 1830 #ifdef COMPAT_IA32 1831 if (curthread->td_proc->p_sysent == &ia32_freebsd_sysvec) { 1832 struct timeval32 tv32; 1833 1834 error = sooptcopyin(sopt, &tv32, sizeof tv32, 1835 sizeof tv32); 1836 CP(tv32, tv, tv_sec); 1837 CP(tv32, tv, tv_usec); 1838 } else 1839 #endif 1840 error = sooptcopyin(sopt, &tv, sizeof tv, 1841 sizeof tv); 1842 if (error) 1843 goto bad; 1844 1845 /* assert(hz > 0); */ 1846 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz || 1847 tv.tv_usec < 0 || tv.tv_usec >= 1000000) { 1848 error = EDOM; 1849 goto bad; 1850 } 1851 /* assert(tick > 0); */ 1852 /* assert(ULONG_MAX - INT_MAX >= 1000000); */ 1853 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick; 1854 if (val > INT_MAX) { 1855 error = EDOM; 1856 goto bad; 1857 } 1858 if (val == 0 && tv.tv_usec != 0) 1859 val = 1; 1860 1861 switch (sopt->sopt_name) { 1862 case SO_SNDTIMEO: 1863 so->so_snd.sb_timeo = val; 1864 break; 1865 case SO_RCVTIMEO: 1866 so->so_rcv.sb_timeo = val; 1867 break; 1868 } 1869 break; 1870 1871 case SO_LABEL: 1872 #ifdef MAC 1873 error = sooptcopyin(sopt, &extmac, sizeof extmac, 1874 sizeof extmac); 1875 if (error) 1876 goto bad; 1877 error = mac_setsockopt_label(sopt->sopt_td->td_ucred, 1878 so, &extmac); 1879 #else 1880 error = EOPNOTSUPP; 1881 #endif 1882 break; 1883 1884 default: 1885 error = ENOPROTOOPT; 1886 break; 1887 } 1888 if (error == 0 && so->so_proto != NULL && 1889 so->so_proto->pr_ctloutput != NULL) { 1890 (void) ((*so->so_proto->pr_ctloutput) 1891 (so, sopt)); 1892 } 1893 } 1894 bad: 1895 return (error); 1896 } 1897 1898 /* Helper routine for getsockopt */ 1899 int 1900 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len) 1901 { 1902 int error; 1903 size_t valsize; 1904 1905 error = 0; 1906 1907 /* 1908 * Documented get behavior is that we always return a value, 1909 * possibly truncated to fit in the user's buffer. 1910 * Traditional behavior is that we always tell the user 1911 * precisely how much we copied, rather than something useful 1912 * like the total amount we had available for her. 1913 * Note that this interface is not idempotent; the entire answer must 1914 * generated ahead of time. 1915 */ 1916 valsize = min(len, sopt->sopt_valsize); 1917 sopt->sopt_valsize = valsize; 1918 if (sopt->sopt_val != NULL) { 1919 if (sopt->sopt_td != NULL) 1920 error = copyout(buf, sopt->sopt_val, valsize); 1921 else 1922 bcopy(buf, sopt->sopt_val, valsize); 1923 } 1924 return (error); 1925 } 1926 1927 int 1928 sogetopt(so, sopt) 1929 struct socket *so; 1930 struct sockopt *sopt; 1931 { 1932 int error, optval; 1933 struct linger l; 1934 struct timeval tv; 1935 #ifdef MAC 1936 struct mac extmac; 1937 #endif 1938 1939 error = 0; 1940 if (sopt->sopt_level != SOL_SOCKET) { 1941 if (so->so_proto && so->so_proto->pr_ctloutput) { 1942 return ((*so->so_proto->pr_ctloutput) 1943 (so, sopt)); 1944 } else 1945 return (ENOPROTOOPT); 1946 } else { 1947 switch (sopt->sopt_name) { 1948 #ifdef INET 1949 case SO_ACCEPTFILTER: 1950 error = do_getopt_accept_filter(so, sopt); 1951 break; 1952 #endif 1953 case SO_LINGER: 1954 SOCK_LOCK(so); 1955 l.l_onoff = so->so_options & SO_LINGER; 1956 l.l_linger = so->so_linger; 1957 SOCK_UNLOCK(so); 1958 error = sooptcopyout(sopt, &l, sizeof l); 1959 break; 1960 1961 case SO_USELOOPBACK: 1962 case SO_DONTROUTE: 1963 case SO_DEBUG: 1964 case SO_KEEPALIVE: 1965 case SO_REUSEADDR: 1966 case SO_REUSEPORT: 1967 case SO_BROADCAST: 1968 case SO_OOBINLINE: 1969 case SO_ACCEPTCONN: 1970 case SO_TIMESTAMP: 1971 case SO_BINTIME: 1972 case SO_NOSIGPIPE: 1973 optval = so->so_options & sopt->sopt_name; 1974 integer: 1975 error = sooptcopyout(sopt, &optval, sizeof optval); 1976 break; 1977 1978 case SO_TYPE: 1979 optval = so->so_type; 1980 goto integer; 1981 1982 case SO_ERROR: 1983 optval = so->so_error; 1984 so->so_error = 0; 1985 goto integer; 1986 1987 case SO_SNDBUF: 1988 optval = so->so_snd.sb_hiwat; 1989 goto integer; 1990 1991 case SO_RCVBUF: 1992 optval = so->so_rcv.sb_hiwat; 1993 goto integer; 1994 1995 case SO_SNDLOWAT: 1996 optval = so->so_snd.sb_lowat; 1997 goto integer; 1998 1999 case SO_RCVLOWAT: 2000 optval = so->so_rcv.sb_lowat; 2001 goto integer; 2002 2003 case SO_SNDTIMEO: 2004 case SO_RCVTIMEO: 2005 optval = (sopt->sopt_name == SO_SNDTIMEO ? 2006 so->so_snd.sb_timeo : so->so_rcv.sb_timeo); 2007 2008 tv.tv_sec = optval / hz; 2009 tv.tv_usec = (optval % hz) * tick; 2010 #ifdef COMPAT_IA32 2011 if (curthread->td_proc->p_sysent == &ia32_freebsd_sysvec) { 2012 struct timeval32 tv32; 2013 2014 CP(tv, tv32, tv_sec); 2015 CP(tv, tv32, tv_usec); 2016 error = sooptcopyout(sopt, &tv32, sizeof tv32); 2017 } else 2018 #endif 2019 error = sooptcopyout(sopt, &tv, sizeof tv); 2020 break; 2021 2022 case SO_LABEL: 2023 #ifdef MAC 2024 error = sooptcopyin(sopt, &extmac, sizeof(extmac), 2025 sizeof(extmac)); 2026 if (error) 2027 return (error); 2028 error = mac_getsockopt_label(sopt->sopt_td->td_ucred, 2029 so, &extmac); 2030 if (error) 2031 return (error); 2032 error = sooptcopyout(sopt, &extmac, sizeof extmac); 2033 #else 2034 error = EOPNOTSUPP; 2035 #endif 2036 break; 2037 2038 case SO_PEERLABEL: 2039 #ifdef MAC 2040 error = sooptcopyin(sopt, &extmac, sizeof(extmac), 2041 sizeof(extmac)); 2042 if (error) 2043 return (error); 2044 error = mac_getsockopt_peerlabel( 2045 sopt->sopt_td->td_ucred, so, &extmac); 2046 if (error) 2047 return (error); 2048 error = sooptcopyout(sopt, &extmac, sizeof extmac); 2049 #else 2050 error = EOPNOTSUPP; 2051 #endif 2052 break; 2053 2054 case SO_LISTENQLIMIT: 2055 optval = so->so_qlimit; 2056 goto integer; 2057 2058 case SO_LISTENQLEN: 2059 optval = so->so_qlen; 2060 goto integer; 2061 2062 case SO_LISTENINCQLEN: 2063 optval = so->so_incqlen; 2064 goto integer; 2065 2066 default: 2067 error = ENOPROTOOPT; 2068 break; 2069 } 2070 return (error); 2071 } 2072 } 2073 2074 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 2075 int 2076 soopt_getm(struct sockopt *sopt, struct mbuf **mp) 2077 { 2078 struct mbuf *m, *m_prev; 2079 int sopt_size = sopt->sopt_valsize; 2080 2081 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 2082 if (m == NULL) 2083 return ENOBUFS; 2084 if (sopt_size > MLEN) { 2085 MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT); 2086 if ((m->m_flags & M_EXT) == 0) { 2087 m_free(m); 2088 return ENOBUFS; 2089 } 2090 m->m_len = min(MCLBYTES, sopt_size); 2091 } else { 2092 m->m_len = min(MLEN, sopt_size); 2093 } 2094 sopt_size -= m->m_len; 2095 *mp = m; 2096 m_prev = m; 2097 2098 while (sopt_size) { 2099 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA); 2100 if (m == NULL) { 2101 m_freem(*mp); 2102 return ENOBUFS; 2103 } 2104 if (sopt_size > MLEN) { 2105 MCLGET(m, sopt->sopt_td != NULL ? M_TRYWAIT : 2106 M_DONTWAIT); 2107 if ((m->m_flags & M_EXT) == 0) { 2108 m_freem(m); 2109 m_freem(*mp); 2110 return ENOBUFS; 2111 } 2112 m->m_len = min(MCLBYTES, sopt_size); 2113 } else { 2114 m->m_len = min(MLEN, sopt_size); 2115 } 2116 sopt_size -= m->m_len; 2117 m_prev->m_next = m; 2118 m_prev = m; 2119 } 2120 return (0); 2121 } 2122 2123 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */ 2124 int 2125 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m) 2126 { 2127 struct mbuf *m0 = m; 2128 2129 if (sopt->sopt_val == NULL) 2130 return (0); 2131 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 2132 if (sopt->sopt_td != NULL) { 2133 int error; 2134 2135 error = copyin(sopt->sopt_val, mtod(m, char *), 2136 m->m_len); 2137 if (error != 0) { 2138 m_freem(m0); 2139 return(error); 2140 } 2141 } else 2142 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len); 2143 sopt->sopt_valsize -= m->m_len; 2144 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len; 2145 m = m->m_next; 2146 } 2147 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */ 2148 panic("ip6_sooptmcopyin"); 2149 return (0); 2150 } 2151 2152 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */ 2153 int 2154 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m) 2155 { 2156 struct mbuf *m0 = m; 2157 size_t valsize = 0; 2158 2159 if (sopt->sopt_val == NULL) 2160 return (0); 2161 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 2162 if (sopt->sopt_td != NULL) { 2163 int error; 2164 2165 error = copyout(mtod(m, char *), sopt->sopt_val, 2166 m->m_len); 2167 if (error != 0) { 2168 m_freem(m0); 2169 return(error); 2170 } 2171 } else 2172 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len); 2173 sopt->sopt_valsize -= m->m_len; 2174 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len; 2175 valsize += m->m_len; 2176 m = m->m_next; 2177 } 2178 if (m != NULL) { 2179 /* enough soopt buffer should be given from user-land */ 2180 m_freem(m0); 2181 return(EINVAL); 2182 } 2183 sopt->sopt_valsize = valsize; 2184 return (0); 2185 } 2186 2187 void 2188 sohasoutofband(so) 2189 struct socket *so; 2190 { 2191 if (so->so_sigio != NULL) 2192 pgsigio(&so->so_sigio, SIGURG, 0); 2193 selwakeuppri(&so->so_rcv.sb_sel, PSOCK); 2194 } 2195 2196 int 2197 sopoll(struct socket *so, int events, struct ucred *active_cred, 2198 struct thread *td) 2199 { 2200 int revents = 0; 2201 2202 SOCKBUF_LOCK(&so->so_snd); 2203 SOCKBUF_LOCK(&so->so_rcv); 2204 if (events & (POLLIN | POLLRDNORM)) 2205 if (soreadable(so)) 2206 revents |= events & (POLLIN | POLLRDNORM); 2207 2208 if (events & POLLINIGNEOF) 2209 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat || 2210 !TAILQ_EMPTY(&so->so_comp) || so->so_error) 2211 revents |= POLLINIGNEOF; 2212 2213 if (events & (POLLOUT | POLLWRNORM)) 2214 if (sowriteable(so)) 2215 revents |= events & (POLLOUT | POLLWRNORM); 2216 2217 if (events & (POLLPRI | POLLRDBAND)) 2218 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK)) 2219 revents |= events & (POLLPRI | POLLRDBAND); 2220 2221 if (revents == 0) { 2222 if (events & 2223 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | 2224 POLLRDBAND)) { 2225 selrecord(td, &so->so_rcv.sb_sel); 2226 so->so_rcv.sb_flags |= SB_SEL; 2227 } 2228 2229 if (events & (POLLOUT | POLLWRNORM)) { 2230 selrecord(td, &so->so_snd.sb_sel); 2231 so->so_snd.sb_flags |= SB_SEL; 2232 } 2233 } 2234 2235 SOCKBUF_UNLOCK(&so->so_rcv); 2236 SOCKBUF_UNLOCK(&so->so_snd); 2237 return (revents); 2238 } 2239 2240 int 2241 soo_kqfilter(struct file *fp, struct knote *kn) 2242 { 2243 struct socket *so = kn->kn_fp->f_data; 2244 struct sockbuf *sb; 2245 2246 switch (kn->kn_filter) { 2247 case EVFILT_READ: 2248 if (so->so_options & SO_ACCEPTCONN) 2249 kn->kn_fop = &solisten_filtops; 2250 else 2251 kn->kn_fop = &soread_filtops; 2252 sb = &so->so_rcv; 2253 break; 2254 case EVFILT_WRITE: 2255 kn->kn_fop = &sowrite_filtops; 2256 sb = &so->so_snd; 2257 break; 2258 default: 2259 return (EINVAL); 2260 } 2261 2262 SOCKBUF_LOCK(sb); 2263 knlist_add(&sb->sb_sel.si_note, kn, 1); 2264 sb->sb_flags |= SB_KNOTE; 2265 SOCKBUF_UNLOCK(sb); 2266 return (0); 2267 } 2268 2269 static void 2270 filt_sordetach(struct knote *kn) 2271 { 2272 struct socket *so = kn->kn_fp->f_data; 2273 2274 SOCKBUF_LOCK(&so->so_rcv); 2275 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1); 2276 if (knlist_empty(&so->so_rcv.sb_sel.si_note)) 2277 so->so_rcv.sb_flags &= ~SB_KNOTE; 2278 SOCKBUF_UNLOCK(&so->so_rcv); 2279 } 2280 2281 /*ARGSUSED*/ 2282 static int 2283 filt_soread(struct knote *kn, long hint) 2284 { 2285 struct socket *so; 2286 2287 so = kn->kn_fp->f_data; 2288 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2289 2290 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl; 2291 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 2292 kn->kn_flags |= EV_EOF; 2293 kn->kn_fflags = so->so_error; 2294 return (1); 2295 } else if (so->so_error) /* temporary udp error */ 2296 return (1); 2297 else if (kn->kn_sfflags & NOTE_LOWAT) 2298 return (kn->kn_data >= kn->kn_sdata); 2299 else 2300 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat); 2301 } 2302 2303 static void 2304 filt_sowdetach(struct knote *kn) 2305 { 2306 struct socket *so = kn->kn_fp->f_data; 2307 2308 SOCKBUF_LOCK(&so->so_snd); 2309 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1); 2310 if (knlist_empty(&so->so_snd.sb_sel.si_note)) 2311 so->so_snd.sb_flags &= ~SB_KNOTE; 2312 SOCKBUF_UNLOCK(&so->so_snd); 2313 } 2314 2315 /*ARGSUSED*/ 2316 static int 2317 filt_sowrite(struct knote *kn, long hint) 2318 { 2319 struct socket *so; 2320 2321 so = kn->kn_fp->f_data; 2322 SOCKBUF_LOCK_ASSERT(&so->so_snd); 2323 kn->kn_data = sbspace(&so->so_snd); 2324 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 2325 kn->kn_flags |= EV_EOF; 2326 kn->kn_fflags = so->so_error; 2327 return (1); 2328 } else if (so->so_error) /* temporary udp error */ 2329 return (1); 2330 else if (((so->so_state & SS_ISCONNECTED) == 0) && 2331 (so->so_proto->pr_flags & PR_CONNREQUIRED)) 2332 return (0); 2333 else if (kn->kn_sfflags & NOTE_LOWAT) 2334 return (kn->kn_data >= kn->kn_sdata); 2335 else 2336 return (kn->kn_data >= so->so_snd.sb_lowat); 2337 } 2338 2339 /*ARGSUSED*/ 2340 static int 2341 filt_solisten(struct knote *kn, long hint) 2342 { 2343 struct socket *so = kn->kn_fp->f_data; 2344 2345 kn->kn_data = so->so_qlen; 2346 return (! TAILQ_EMPTY(&so->so_comp)); 2347 } 2348 2349 int 2350 socheckuid(struct socket *so, uid_t uid) 2351 { 2352 2353 if (so == NULL) 2354 return (EPERM); 2355 if (so->so_cred->cr_uid != uid) 2356 return (EPERM); 2357 return (0); 2358 } 2359 2360 static int 2361 somaxconn_sysctl(SYSCTL_HANDLER_ARGS) 2362 { 2363 int error; 2364 int val; 2365 2366 val = somaxconn; 2367 error = sysctl_handle_int(oidp, &val, sizeof(int), req); 2368 if (error || !req->newptr ) 2369 return (error); 2370 2371 if (val < 1 || val > USHRT_MAX) 2372 return (EINVAL); 2373 2374 somaxconn = val; 2375 return (0); 2376 } 2377