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