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