1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1988, 1990, 1993 5 * The Regents of the University of California. 6 * Copyright (c) 2004 The FreeBSD Foundation 7 * Copyright (c) 2004-2008 Robert N. M. Watson 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Comments on the socket life cycle: 37 * 38 * soalloc() sets of socket layer state for a socket, called only by 39 * socreate() and sonewconn(). Socket layer private. 40 * 41 * sodealloc() tears down socket layer state for a socket, called only by 42 * sofree() and sonewconn(). Socket layer private. 43 * 44 * pru_attach() associates protocol layer state with an allocated socket; 45 * called only once, may fail, aborting socket allocation. This is called 46 * from socreate() and sonewconn(). Socket layer private. 47 * 48 * pru_detach() disassociates protocol layer state from an attached socket, 49 * and will be called exactly once for sockets in which pru_attach() has 50 * been successfully called. If pru_attach() returned an error, 51 * pru_detach() will not be called. Socket layer private. 52 * 53 * pru_abort() and pru_close() notify the protocol layer that the last 54 * consumer of a socket is starting to tear down the socket, and that the 55 * protocol should terminate the connection. Historically, pru_abort() also 56 * detached protocol state from the socket state, but this is no longer the 57 * case. 58 * 59 * socreate() creates a socket and attaches protocol state. This is a public 60 * interface that may be used by socket layer consumers to create new 61 * sockets. 62 * 63 * sonewconn() creates a socket and attaches protocol state. This is a 64 * public interface that may be used by protocols to create new sockets when 65 * a new connection is received and will be available for accept() on a 66 * listen socket. 67 * 68 * soclose() destroys a socket after possibly waiting for it to disconnect. 69 * This is a public interface that socket consumers should use to close and 70 * release a socket when done with it. 71 * 72 * soabort() destroys a socket without waiting for it to disconnect (used 73 * only for incoming connections that are already partially or fully 74 * connected). This is used internally by the socket layer when clearing 75 * listen socket queues (due to overflow or close on the listen socket), but 76 * is also a public interface protocols may use to abort connections in 77 * their incomplete listen queues should they no longer be required. Sockets 78 * placed in completed connection listen queues should not be aborted for 79 * reasons described in the comment above the soclose() implementation. This 80 * is not a general purpose close routine, and except in the specific 81 * circumstances described here, should not be used. 82 * 83 * sofree() will free a socket and its protocol state if all references on 84 * the socket have been released, and is the public interface to attempt to 85 * free a socket when a reference is removed. This is a socket layer private 86 * interface. 87 * 88 * NOTE: In addition to socreate() and soclose(), which provide a single 89 * socket reference to the consumer to be managed as required, there are two 90 * calls to explicitly manage socket references, soref(), and sorele(). 91 * Currently, these are generally required only when transitioning a socket 92 * from a listen queue to a file descriptor, in order to prevent garbage 93 * collection of the socket at an untimely moment. For a number of reasons, 94 * these interfaces are not preferred, and should be avoided. 95 * 96 * NOTE: With regard to VNETs the general rule is that callers do not set 97 * curvnet. Exceptions to this rule include soabort(), sodisconnect(), 98 * sofree(), sorele(), sonewconn() and sorflush(), which are usually called 99 * from a pre-set VNET context. sopoll() currently does not need a VNET 100 * context to be set. 101 */ 102 103 #include <sys/cdefs.h> 104 #include "opt_inet.h" 105 #include "opt_inet6.h" 106 #include "opt_kern_tls.h" 107 #include "opt_ktrace.h" 108 #include "opt_sctp.h" 109 110 #include <sys/param.h> 111 #include <sys/systm.h> 112 #include <sys/capsicum.h> 113 #include <sys/fcntl.h> 114 #include <sys/limits.h> 115 #include <sys/lock.h> 116 #include <sys/mac.h> 117 #include <sys/malloc.h> 118 #include <sys/mbuf.h> 119 #include <sys/mutex.h> 120 #include <sys/domain.h> 121 #include <sys/file.h> /* for struct knote */ 122 #include <sys/hhook.h> 123 #include <sys/kernel.h> 124 #include <sys/khelp.h> 125 #include <sys/ktls.h> 126 #include <sys/event.h> 127 #include <sys/eventhandler.h> 128 #include <sys/poll.h> 129 #include <sys/proc.h> 130 #include <sys/protosw.h> 131 #include <sys/sbuf.h> 132 #include <sys/socket.h> 133 #include <sys/socketvar.h> 134 #include <sys/resourcevar.h> 135 #include <net/route.h> 136 #include <sys/signalvar.h> 137 #include <sys/stat.h> 138 #include <sys/sx.h> 139 #include <sys/sysctl.h> 140 #include <sys/taskqueue.h> 141 #include <sys/uio.h> 142 #include <sys/un.h> 143 #include <sys/unpcb.h> 144 #include <sys/jail.h> 145 #include <sys/syslog.h> 146 #include <netinet/in.h> 147 #include <netinet/in_pcb.h> 148 #include <netinet/tcp.h> 149 150 #include <net/vnet.h> 151 152 #include <security/mac/mac_framework.h> 153 154 #include <vm/uma.h> 155 156 #ifdef COMPAT_FREEBSD32 157 #include <sys/mount.h> 158 #include <sys/sysent.h> 159 #include <compat/freebsd32/freebsd32.h> 160 #endif 161 162 static int soreceive_rcvoob(struct socket *so, struct uio *uio, 163 int flags); 164 static void so_rdknl_lock(void *); 165 static void so_rdknl_unlock(void *); 166 static void so_rdknl_assert_lock(void *, int); 167 static void so_wrknl_lock(void *); 168 static void so_wrknl_unlock(void *); 169 static void so_wrknl_assert_lock(void *, int); 170 171 static void filt_sordetach(struct knote *kn); 172 static int filt_soread(struct knote *kn, long hint); 173 static void filt_sowdetach(struct knote *kn); 174 static int filt_sowrite(struct knote *kn, long hint); 175 static int filt_soempty(struct knote *kn, long hint); 176 static int inline hhook_run_socket(struct socket *so, void *hctx, int32_t h_id); 177 fo_kqfilter_t soo_kqfilter; 178 179 static struct filterops soread_filtops = { 180 .f_isfd = 1, 181 .f_detach = filt_sordetach, 182 .f_event = filt_soread, 183 }; 184 static struct filterops sowrite_filtops = { 185 .f_isfd = 1, 186 .f_detach = filt_sowdetach, 187 .f_event = filt_sowrite, 188 }; 189 static struct filterops soempty_filtops = { 190 .f_isfd = 1, 191 .f_detach = filt_sowdetach, 192 .f_event = filt_soempty, 193 }; 194 195 so_gen_t so_gencnt; /* generation count for sockets */ 196 197 MALLOC_DEFINE(M_SONAME, "soname", "socket name"); 198 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block"); 199 200 #define VNET_SO_ASSERT(so) \ 201 VNET_ASSERT(curvnet != NULL, \ 202 ("%s:%d curvnet is NULL, so=%p", __func__, __LINE__, (so))); 203 204 VNET_DEFINE(struct hhook_head *, socket_hhh[HHOOK_SOCKET_LAST + 1]); 205 #define V_socket_hhh VNET(socket_hhh) 206 207 /* 208 * Limit on the number of connections in the listen queue waiting 209 * for accept(2). 210 * NB: The original sysctl somaxconn is still available but hidden 211 * to prevent confusion about the actual purpose of this number. 212 */ 213 static u_int somaxconn = SOMAXCONN; 214 215 static int 216 sysctl_somaxconn(SYSCTL_HANDLER_ARGS) 217 { 218 int error; 219 int val; 220 221 val = somaxconn; 222 error = sysctl_handle_int(oidp, &val, 0, req); 223 if (error || !req->newptr ) 224 return (error); 225 226 /* 227 * The purpose of the UINT_MAX / 3 limit, is so that the formula 228 * 3 * so_qlimit / 2 229 * below, will not overflow. 230 */ 231 232 if (val < 1 || val > UINT_MAX / 3) 233 return (EINVAL); 234 235 somaxconn = val; 236 return (0); 237 } 238 SYSCTL_PROC(_kern_ipc, OID_AUTO, soacceptqueue, 239 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int), 240 sysctl_somaxconn, "I", 241 "Maximum listen socket pending connection accept queue size"); 242 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn, 243 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_SKIP | CTLFLAG_MPSAFE, 0, 244 sizeof(int), sysctl_somaxconn, "I", 245 "Maximum listen socket pending connection accept queue size (compat)"); 246 247 static int numopensockets; 248 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD, 249 &numopensockets, 0, "Number of open sockets"); 250 251 /* 252 * so_global_mtx protects so_gencnt, numopensockets, and the per-socket 253 * so_gencnt field. 254 */ 255 static struct mtx so_global_mtx; 256 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF); 257 258 /* 259 * General IPC sysctl name space, used by sockets and a variety of other IPC 260 * types. 261 */ 262 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 263 "IPC"); 264 265 /* 266 * Initialize the socket subsystem and set up the socket 267 * memory allocator. 268 */ 269 static uma_zone_t socket_zone; 270 int maxsockets; 271 272 static void 273 socket_zone_change(void *tag) 274 { 275 276 maxsockets = uma_zone_set_max(socket_zone, maxsockets); 277 } 278 279 static void 280 socket_hhook_register(int subtype) 281 { 282 283 if (hhook_head_register(HHOOK_TYPE_SOCKET, subtype, 284 &V_socket_hhh[subtype], 285 HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0) 286 printf("%s: WARNING: unable to register hook\n", __func__); 287 } 288 289 static void 290 socket_hhook_deregister(int subtype) 291 { 292 293 if (hhook_head_deregister(V_socket_hhh[subtype]) != 0) 294 printf("%s: WARNING: unable to deregister hook\n", __func__); 295 } 296 297 static void 298 socket_init(void *tag) 299 { 300 301 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL, 302 NULL, NULL, UMA_ALIGN_PTR, 0); 303 maxsockets = uma_zone_set_max(socket_zone, maxsockets); 304 uma_zone_set_warning(socket_zone, "kern.ipc.maxsockets limit reached"); 305 EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL, 306 EVENTHANDLER_PRI_FIRST); 307 } 308 SYSINIT(socket, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, socket_init, NULL); 309 310 static void 311 socket_vnet_init(const void *unused __unused) 312 { 313 int i; 314 315 /* We expect a contiguous range */ 316 for (i = 0; i <= HHOOK_SOCKET_LAST; i++) 317 socket_hhook_register(i); 318 } 319 VNET_SYSINIT(socket_vnet_init, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, 320 socket_vnet_init, NULL); 321 322 static void 323 socket_vnet_uninit(const void *unused __unused) 324 { 325 int i; 326 327 for (i = 0; i <= HHOOK_SOCKET_LAST; i++) 328 socket_hhook_deregister(i); 329 } 330 VNET_SYSUNINIT(socket_vnet_uninit, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, 331 socket_vnet_uninit, NULL); 332 333 /* 334 * Initialise maxsockets. This SYSINIT must be run after 335 * tunable_mbinit(). 336 */ 337 static void 338 init_maxsockets(void *ignored) 339 { 340 341 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets); 342 maxsockets = imax(maxsockets, maxfiles); 343 } 344 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL); 345 346 /* 347 * Sysctl to get and set the maximum global sockets limit. Notify protocols 348 * of the change so that they can update their dependent limits as required. 349 */ 350 static int 351 sysctl_maxsockets(SYSCTL_HANDLER_ARGS) 352 { 353 int error, newmaxsockets; 354 355 newmaxsockets = maxsockets; 356 error = sysctl_handle_int(oidp, &newmaxsockets, 0, req); 357 if (error == 0 && req->newptr && newmaxsockets != maxsockets) { 358 if (newmaxsockets > maxsockets && 359 newmaxsockets <= maxfiles) { 360 maxsockets = newmaxsockets; 361 EVENTHANDLER_INVOKE(maxsockets_change); 362 } else 363 error = EINVAL; 364 } 365 return (error); 366 } 367 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, 368 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NOFETCH | CTLFLAG_MPSAFE, 369 &maxsockets, 0, sysctl_maxsockets, "IU", 370 "Maximum number of sockets available"); 371 372 /* 373 * Socket operation routines. These routines are called by the routines in 374 * sys_socket.c or from a system process, and implement the semantics of 375 * socket operations by switching out to the protocol specific routines. 376 */ 377 378 /* 379 * Get a socket structure from our zone, and initialize it. Note that it 380 * would probably be better to allocate socket and PCB at the same time, but 381 * I'm not convinced that all the protocols can be easily modified to do 382 * this. 383 * 384 * soalloc() returns a socket with a ref count of 0. 385 */ 386 static struct socket * 387 soalloc(struct vnet *vnet) 388 { 389 struct socket *so; 390 391 so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO); 392 if (so == NULL) 393 return (NULL); 394 #ifdef MAC 395 if (mac_socket_init(so, M_NOWAIT) != 0) { 396 uma_zfree(socket_zone, so); 397 return (NULL); 398 } 399 #endif 400 if (khelp_init_osd(HELPER_CLASS_SOCKET, &so->osd)) { 401 uma_zfree(socket_zone, so); 402 return (NULL); 403 } 404 405 /* 406 * The socket locking protocol allows to lock 2 sockets at a time, 407 * however, the first one must be a listening socket. WITNESS lacks 408 * a feature to change class of an existing lock, so we use DUPOK. 409 */ 410 mtx_init(&so->so_lock, "socket", NULL, MTX_DEF | MTX_DUPOK); 411 mtx_init(&so->so_snd_mtx, "so_snd", NULL, MTX_DEF); 412 mtx_init(&so->so_rcv_mtx, "so_rcv", NULL, MTX_DEF); 413 so->so_rcv.sb_sel = &so->so_rdsel; 414 so->so_snd.sb_sel = &so->so_wrsel; 415 sx_init(&so->so_snd_sx, "so_snd_sx"); 416 sx_init(&so->so_rcv_sx, "so_rcv_sx"); 417 TAILQ_INIT(&so->so_snd.sb_aiojobq); 418 TAILQ_INIT(&so->so_rcv.sb_aiojobq); 419 TASK_INIT(&so->so_snd.sb_aiotask, 0, soaio_snd, so); 420 TASK_INIT(&so->so_rcv.sb_aiotask, 0, soaio_rcv, so); 421 #ifdef VIMAGE 422 VNET_ASSERT(vnet != NULL, ("%s:%d vnet is NULL, so=%p", 423 __func__, __LINE__, so)); 424 so->so_vnet = vnet; 425 #endif 426 /* We shouldn't need the so_global_mtx */ 427 if (hhook_run_socket(so, NULL, HHOOK_SOCKET_CREATE)) { 428 /* Do we need more comprehensive error returns? */ 429 uma_zfree(socket_zone, so); 430 return (NULL); 431 } 432 mtx_lock(&so_global_mtx); 433 so->so_gencnt = ++so_gencnt; 434 ++numopensockets; 435 #ifdef VIMAGE 436 vnet->vnet_sockcnt++; 437 #endif 438 mtx_unlock(&so_global_mtx); 439 440 return (so); 441 } 442 443 /* 444 * Free the storage associated with a socket at the socket layer, tear down 445 * locks, labels, etc. All protocol state is assumed already to have been 446 * torn down (and possibly never set up) by the caller. 447 */ 448 void 449 sodealloc(struct socket *so) 450 { 451 452 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count)); 453 KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL")); 454 455 mtx_lock(&so_global_mtx); 456 so->so_gencnt = ++so_gencnt; 457 --numopensockets; /* Could be below, but faster here. */ 458 #ifdef VIMAGE 459 VNET_ASSERT(so->so_vnet != NULL, ("%s:%d so_vnet is NULL, so=%p", 460 __func__, __LINE__, so)); 461 so->so_vnet->vnet_sockcnt--; 462 #endif 463 mtx_unlock(&so_global_mtx); 464 #ifdef MAC 465 mac_socket_destroy(so); 466 #endif 467 hhook_run_socket(so, NULL, HHOOK_SOCKET_CLOSE); 468 469 khelp_destroy_osd(&so->osd); 470 if (SOLISTENING(so)) { 471 if (so->sol_accept_filter != NULL) 472 accept_filt_setopt(so, NULL); 473 } else { 474 if (so->so_rcv.sb_hiwat) 475 (void)chgsbsize(so->so_cred->cr_uidinfo, 476 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY); 477 if (so->so_snd.sb_hiwat) 478 (void)chgsbsize(so->so_cred->cr_uidinfo, 479 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY); 480 sx_destroy(&so->so_snd_sx); 481 sx_destroy(&so->so_rcv_sx); 482 mtx_destroy(&so->so_snd_mtx); 483 mtx_destroy(&so->so_rcv_mtx); 484 } 485 crfree(so->so_cred); 486 mtx_destroy(&so->so_lock); 487 uma_zfree(socket_zone, so); 488 } 489 490 /* 491 * socreate returns a socket with a ref count of 1 and a file descriptor 492 * reference. The socket should be closed with soclose(). 493 */ 494 int 495 socreate(int dom, struct socket **aso, int type, int proto, 496 struct ucred *cred, struct thread *td) 497 { 498 struct protosw *prp; 499 struct socket *so; 500 int error; 501 502 /* 503 * XXX: divert(4) historically abused PF_INET. Keep this compatibility 504 * shim until all applications have been updated. 505 */ 506 if (__predict_false(dom == PF_INET && type == SOCK_RAW && 507 proto == IPPROTO_DIVERT)) { 508 dom = PF_DIVERT; 509 printf("%s uses obsolete way to create divert(4) socket\n", 510 td->td_proc->p_comm); 511 } 512 513 prp = pffindproto(dom, type, proto); 514 if (prp == NULL) { 515 /* No support for domain. */ 516 if (pffinddomain(dom) == NULL) 517 return (EAFNOSUPPORT); 518 /* No support for socket type. */ 519 if (proto == 0 && type != 0) 520 return (EPROTOTYPE); 521 return (EPROTONOSUPPORT); 522 } 523 524 MPASS(prp->pr_attach); 525 526 if ((prp->pr_flags & PR_CAPATTACH) == 0) { 527 if (CAP_TRACING(td)) 528 ktrcapfail(CAPFAIL_PROTO, &proto); 529 if (IN_CAPABILITY_MODE(td)) 530 return (ECAPMODE); 531 } 532 533 if (prison_check_af(cred, prp->pr_domain->dom_family) != 0) 534 return (EPROTONOSUPPORT); 535 536 so = soalloc(CRED_TO_VNET(cred)); 537 if (so == NULL) 538 return (ENOBUFS); 539 540 so->so_type = type; 541 so->so_cred = crhold(cred); 542 if ((prp->pr_domain->dom_family == PF_INET) || 543 (prp->pr_domain->dom_family == PF_INET6) || 544 (prp->pr_domain->dom_family == PF_ROUTE)) 545 so->so_fibnum = td->td_proc->p_fibnum; 546 else 547 so->so_fibnum = 0; 548 so->so_proto = prp; 549 #ifdef MAC 550 mac_socket_create(cred, so); 551 #endif 552 knlist_init(&so->so_rdsel.si_note, so, so_rdknl_lock, so_rdknl_unlock, 553 so_rdknl_assert_lock); 554 knlist_init(&so->so_wrsel.si_note, so, so_wrknl_lock, so_wrknl_unlock, 555 so_wrknl_assert_lock); 556 if ((prp->pr_flags & PR_SOCKBUF) == 0) { 557 so->so_snd.sb_mtx = &so->so_snd_mtx; 558 so->so_rcv.sb_mtx = &so->so_rcv_mtx; 559 } 560 /* 561 * Auto-sizing of socket buffers is managed by the protocols and 562 * the appropriate flags must be set in the pru_attach function. 563 */ 564 CURVNET_SET(so->so_vnet); 565 error = prp->pr_attach(so, proto, td); 566 CURVNET_RESTORE(); 567 if (error) { 568 sodealloc(so); 569 return (error); 570 } 571 soref(so); 572 *aso = so; 573 return (0); 574 } 575 576 #ifdef REGRESSION 577 static int regression_sonewconn_earlytest = 1; 578 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW, 579 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test"); 580 #endif 581 582 static int sooverprio = LOG_DEBUG; 583 SYSCTL_INT(_kern_ipc, OID_AUTO, sooverprio, CTLFLAG_RW, 584 &sooverprio, 0, "Log priority for listen socket overflows: 0..7 or -1 to disable"); 585 586 static struct timeval overinterval = { 60, 0 }; 587 SYSCTL_TIMEVAL_SEC(_kern_ipc, OID_AUTO, sooverinterval, CTLFLAG_RW, 588 &overinterval, 589 "Delay in seconds between warnings for listen socket overflows"); 590 591 /* 592 * When an attempt at a new connection is noted on a socket which supports 593 * accept(2), the protocol has two options: 594 * 1) Call legacy sonewconn() function, which would call protocol attach 595 * method, same as used for socket(2). 596 * 2) Call solisten_clone(), do attach that is specific to a cloned connection, 597 * and then call solisten_enqueue(). 598 * 599 * Note: the ref count on the socket is 0 on return. 600 */ 601 struct socket * 602 solisten_clone(struct socket *head) 603 { 604 struct sbuf descrsb; 605 struct socket *so; 606 int len, overcount; 607 u_int qlen; 608 const char localprefix[] = "local:"; 609 char descrbuf[SUNPATHLEN + sizeof(localprefix)]; 610 #if defined(INET6) 611 char addrbuf[INET6_ADDRSTRLEN]; 612 #elif defined(INET) 613 char addrbuf[INET_ADDRSTRLEN]; 614 #endif 615 bool dolog, over; 616 617 SOLISTEN_LOCK(head); 618 over = (head->sol_qlen > 3 * head->sol_qlimit / 2); 619 #ifdef REGRESSION 620 if (regression_sonewconn_earlytest && over) { 621 #else 622 if (over) { 623 #endif 624 head->sol_overcount++; 625 dolog = (sooverprio >= 0) && 626 !!ratecheck(&head->sol_lastover, &overinterval); 627 628 /* 629 * If we're going to log, copy the overflow count and queue 630 * length from the listen socket before dropping the lock. 631 * Also, reset the overflow count. 632 */ 633 if (dolog) { 634 overcount = head->sol_overcount; 635 head->sol_overcount = 0; 636 qlen = head->sol_qlen; 637 } 638 SOLISTEN_UNLOCK(head); 639 640 if (dolog) { 641 /* 642 * Try to print something descriptive about the 643 * socket for the error message. 644 */ 645 sbuf_new(&descrsb, descrbuf, sizeof(descrbuf), 646 SBUF_FIXEDLEN); 647 switch (head->so_proto->pr_domain->dom_family) { 648 #if defined(INET) || defined(INET6) 649 #ifdef INET 650 case AF_INET: 651 #endif 652 #ifdef INET6 653 case AF_INET6: 654 if (head->so_proto->pr_domain->dom_family == 655 AF_INET6 || 656 (sotoinpcb(head)->inp_inc.inc_flags & 657 INC_ISIPV6)) { 658 ip6_sprintf(addrbuf, 659 &sotoinpcb(head)->inp_inc.inc6_laddr); 660 sbuf_printf(&descrsb, "[%s]", addrbuf); 661 } else 662 #endif 663 { 664 #ifdef INET 665 inet_ntoa_r( 666 sotoinpcb(head)->inp_inc.inc_laddr, 667 addrbuf); 668 sbuf_cat(&descrsb, addrbuf); 669 #endif 670 } 671 sbuf_printf(&descrsb, ":%hu (proto %u)", 672 ntohs(sotoinpcb(head)->inp_inc.inc_lport), 673 head->so_proto->pr_protocol); 674 break; 675 #endif /* INET || INET6 */ 676 case AF_UNIX: 677 sbuf_cat(&descrsb, localprefix); 678 if (sotounpcb(head)->unp_addr != NULL) 679 len = 680 sotounpcb(head)->unp_addr->sun_len - 681 offsetof(struct sockaddr_un, 682 sun_path); 683 else 684 len = 0; 685 if (len > 0) 686 sbuf_bcat(&descrsb, 687 sotounpcb(head)->unp_addr->sun_path, 688 len); 689 else 690 sbuf_cat(&descrsb, "(unknown)"); 691 break; 692 } 693 694 /* 695 * If we can't print something more specific, at least 696 * print the domain name. 697 */ 698 if (sbuf_finish(&descrsb) != 0 || 699 sbuf_len(&descrsb) <= 0) { 700 sbuf_clear(&descrsb); 701 sbuf_cat(&descrsb, 702 head->so_proto->pr_domain->dom_name ?: 703 "unknown"); 704 sbuf_finish(&descrsb); 705 } 706 KASSERT(sbuf_len(&descrsb) > 0, 707 ("%s: sbuf creation failed", __func__)); 708 /* 709 * Preserve the historic listen queue overflow log 710 * message, that starts with "sonewconn:". It has 711 * been known to sysadmins for years and also test 712 * sys/kern/sonewconn_overflow checks for it. 713 */ 714 if (head->so_cred == 0) { 715 log(LOG_PRI(sooverprio), 716 "sonewconn: pcb %p (%s): " 717 "Listen queue overflow: %i already in " 718 "queue awaiting acceptance (%d " 719 "occurrences)\n", head->so_pcb, 720 sbuf_data(&descrsb), 721 qlen, overcount); 722 } else { 723 log(LOG_PRI(sooverprio), 724 "sonewconn: pcb %p (%s): " 725 "Listen queue overflow: " 726 "%i already in queue awaiting acceptance " 727 "(%d occurrences), euid %d, rgid %d, jail %s\n", 728 head->so_pcb, sbuf_data(&descrsb), qlen, 729 overcount, head->so_cred->cr_uid, 730 head->so_cred->cr_rgid, 731 head->so_cred->cr_prison ? 732 head->so_cred->cr_prison->pr_name : 733 "not_jailed"); 734 } 735 sbuf_delete(&descrsb); 736 737 overcount = 0; 738 } 739 740 return (NULL); 741 } 742 SOLISTEN_UNLOCK(head); 743 VNET_ASSERT(head->so_vnet != NULL, ("%s: so %p vnet is NULL", 744 __func__, head)); 745 so = soalloc(head->so_vnet); 746 if (so == NULL) { 747 log(LOG_DEBUG, "%s: pcb %p: New socket allocation failure: " 748 "limit reached or out of memory\n", 749 __func__, head->so_pcb); 750 return (NULL); 751 } 752 so->so_listen = head; 753 so->so_type = head->so_type; 754 /* 755 * POSIX is ambiguous on what options an accept(2)ed socket should 756 * inherit from the listener. Words "create a new socket" may be 757 * interpreted as not inheriting anything. Best programming practice 758 * for application developers is to not rely on such inheritance. 759 * FreeBSD had historically inherited all so_options excluding 760 * SO_ACCEPTCONN, which virtually means all SOL_SOCKET level options, 761 * including those completely irrelevant to a new born socket. For 762 * compatibility with older versions we will inherit a list of 763 * meaningful options. 764 * The crucial bit to inherit is SO_ACCEPTFILTER. We need it present 765 * in the child socket for soisconnected() promoting socket from the 766 * incomplete queue to complete. It will be cleared before the child 767 * gets available to accept(2). 768 */ 769 so->so_options = head->so_options & (SO_ACCEPTFILTER | SO_KEEPALIVE | 770 SO_DONTROUTE | SO_LINGER | SO_OOBINLINE | SO_NOSIGPIPE); 771 so->so_linger = head->so_linger; 772 so->so_state = head->so_state; 773 so->so_fibnum = head->so_fibnum; 774 so->so_proto = head->so_proto; 775 so->so_cred = crhold(head->so_cred); 776 if (V_socket_hhh[HHOOK_SOCKET_NEWCONN]->hhh_nhooks > 0) { 777 if (hhook_run_socket(so, head, HHOOK_SOCKET_NEWCONN)) { 778 sodealloc(so); 779 log(LOG_DEBUG, "%s: hhook run failed\n", __func__); 780 return (NULL); 781 } 782 } 783 #ifdef MAC 784 mac_socket_newconn(head, so); 785 #endif 786 knlist_init(&so->so_rdsel.si_note, so, so_rdknl_lock, so_rdknl_unlock, 787 so_rdknl_assert_lock); 788 knlist_init(&so->so_wrsel.si_note, so, so_wrknl_lock, so_wrknl_unlock, 789 so_wrknl_assert_lock); 790 VNET_SO_ASSERT(head); 791 if (soreserve(so, head->sol_sbsnd_hiwat, head->sol_sbrcv_hiwat)) { 792 sodealloc(so); 793 log(LOG_DEBUG, "%s: pcb %p: soreserve() failed\n", 794 __func__, head->so_pcb); 795 return (NULL); 796 } 797 so->so_rcv.sb_lowat = head->sol_sbrcv_lowat; 798 so->so_snd.sb_lowat = head->sol_sbsnd_lowat; 799 so->so_rcv.sb_timeo = head->sol_sbrcv_timeo; 800 so->so_snd.sb_timeo = head->sol_sbsnd_timeo; 801 so->so_rcv.sb_flags = head->sol_sbrcv_flags & SB_AUTOSIZE; 802 so->so_snd.sb_flags = head->sol_sbsnd_flags & SB_AUTOSIZE; 803 if ((so->so_proto->pr_flags & PR_SOCKBUF) == 0) { 804 so->so_snd.sb_mtx = &so->so_snd_mtx; 805 so->so_rcv.sb_mtx = &so->so_rcv_mtx; 806 } 807 808 return (so); 809 } 810 811 /* Connstatus may be 0 or SS_ISCONNECTED. */ 812 struct socket * 813 sonewconn(struct socket *head, int connstatus) 814 { 815 struct socket *so; 816 817 if ((so = solisten_clone(head)) == NULL) 818 return (NULL); 819 820 if (so->so_proto->pr_attach(so, 0, NULL) != 0) { 821 sodealloc(so); 822 log(LOG_DEBUG, "%s: pcb %p: pr_attach() failed\n", 823 __func__, head->so_pcb); 824 return (NULL); 825 } 826 827 (void)solisten_enqueue(so, connstatus); 828 829 return (so); 830 } 831 832 /* 833 * Enqueue socket cloned by solisten_clone() to the listen queue of the 834 * listener it has been cloned from. 835 * 836 * Return 'true' if socket landed on complete queue, otherwise 'false'. 837 */ 838 bool 839 solisten_enqueue(struct socket *so, int connstatus) 840 { 841 struct socket *head = so->so_listen; 842 843 MPASS(refcount_load(&so->so_count) == 0); 844 refcount_init(&so->so_count, 1); 845 846 SOLISTEN_LOCK(head); 847 if (head->sol_accept_filter != NULL) 848 connstatus = 0; 849 so->so_state |= connstatus; 850 soref(head); /* A socket on (in)complete queue refs head. */ 851 if (connstatus) { 852 TAILQ_INSERT_TAIL(&head->sol_comp, so, so_list); 853 so->so_qstate = SQ_COMP; 854 head->sol_qlen++; 855 solisten_wakeup(head); /* unlocks */ 856 return (true); 857 } else { 858 /* 859 * Keep removing sockets from the head until there's room for 860 * us to insert on the tail. In pre-locking revisions, this 861 * was a simple if(), but as we could be racing with other 862 * threads and soabort() requires dropping locks, we must 863 * loop waiting for the condition to be true. 864 */ 865 while (head->sol_incqlen > head->sol_qlimit) { 866 struct socket *sp; 867 868 sp = TAILQ_FIRST(&head->sol_incomp); 869 TAILQ_REMOVE(&head->sol_incomp, sp, so_list); 870 head->sol_incqlen--; 871 SOCK_LOCK(sp); 872 sp->so_qstate = SQ_NONE; 873 sp->so_listen = NULL; 874 SOCK_UNLOCK(sp); 875 sorele_locked(head); /* does SOLISTEN_UNLOCK, head stays */ 876 soabort(sp); 877 SOLISTEN_LOCK(head); 878 } 879 TAILQ_INSERT_TAIL(&head->sol_incomp, so, so_list); 880 so->so_qstate = SQ_INCOMP; 881 head->sol_incqlen++; 882 SOLISTEN_UNLOCK(head); 883 return (false); 884 } 885 } 886 887 #if defined(SCTP) || defined(SCTP_SUPPORT) 888 /* 889 * Socket part of sctp_peeloff(). Detach a new socket from an 890 * association. The new socket is returned with a reference. 891 * 892 * XXXGL: reduce copy-paste with solisten_clone(). 893 */ 894 struct socket * 895 sopeeloff(struct socket *head) 896 { 897 struct socket *so; 898 899 VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p", 900 __func__, __LINE__, head)); 901 so = soalloc(head->so_vnet); 902 if (so == NULL) { 903 log(LOG_DEBUG, "%s: pcb %p: New socket allocation failure: " 904 "limit reached or out of memory\n", 905 __func__, head->so_pcb); 906 return (NULL); 907 } 908 so->so_type = head->so_type; 909 so->so_options = head->so_options; 910 so->so_linger = head->so_linger; 911 so->so_state = (head->so_state & SS_NBIO) | SS_ISCONNECTED; 912 so->so_fibnum = head->so_fibnum; 913 so->so_proto = head->so_proto; 914 so->so_cred = crhold(head->so_cred); 915 #ifdef MAC 916 mac_socket_newconn(head, so); 917 #endif 918 knlist_init(&so->so_rdsel.si_note, so, so_rdknl_lock, so_rdknl_unlock, 919 so_rdknl_assert_lock); 920 knlist_init(&so->so_wrsel.si_note, so, so_wrknl_lock, so_wrknl_unlock, 921 so_wrknl_assert_lock); 922 VNET_SO_ASSERT(head); 923 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) { 924 sodealloc(so); 925 log(LOG_DEBUG, "%s: pcb %p: soreserve() failed\n", 926 __func__, head->so_pcb); 927 return (NULL); 928 } 929 if ((*so->so_proto->pr_attach)(so, 0, NULL)) { 930 sodealloc(so); 931 log(LOG_DEBUG, "%s: pcb %p: pru_attach() failed\n", 932 __func__, head->so_pcb); 933 return (NULL); 934 } 935 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat; 936 so->so_snd.sb_lowat = head->so_snd.sb_lowat; 937 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo; 938 so->so_snd.sb_timeo = head->so_snd.sb_timeo; 939 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE; 940 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE; 941 if ((so->so_proto->pr_flags & PR_SOCKBUF) == 0) { 942 so->so_snd.sb_mtx = &so->so_snd_mtx; 943 so->so_rcv.sb_mtx = &so->so_rcv_mtx; 944 } 945 946 soref(so); 947 948 return (so); 949 } 950 #endif /* SCTP */ 951 952 int 953 sobind(struct socket *so, struct sockaddr *nam, struct thread *td) 954 { 955 int error; 956 957 CURVNET_SET(so->so_vnet); 958 error = so->so_proto->pr_bind(so, nam, td); 959 CURVNET_RESTORE(); 960 return (error); 961 } 962 963 int 964 sobindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td) 965 { 966 int error; 967 968 CURVNET_SET(so->so_vnet); 969 error = so->so_proto->pr_bindat(fd, so, nam, td); 970 CURVNET_RESTORE(); 971 return (error); 972 } 973 974 /* 975 * solisten() transitions a socket from a non-listening state to a listening 976 * state, but can also be used to update the listen queue depth on an 977 * existing listen socket. The protocol will call back into the sockets 978 * layer using solisten_proto_check() and solisten_proto() to check and set 979 * socket-layer listen state. Call backs are used so that the protocol can 980 * acquire both protocol and socket layer locks in whatever order is required 981 * by the protocol. 982 * 983 * Protocol implementors are advised to hold the socket lock across the 984 * socket-layer test and set to avoid races at the socket layer. 985 */ 986 int 987 solisten(struct socket *so, int backlog, struct thread *td) 988 { 989 int error; 990 991 CURVNET_SET(so->so_vnet); 992 error = so->so_proto->pr_listen(so, backlog, td); 993 CURVNET_RESTORE(); 994 return (error); 995 } 996 997 /* 998 * Prepare for a call to solisten_proto(). Acquire all socket buffer locks in 999 * order to interlock with socket I/O. 1000 */ 1001 int 1002 solisten_proto_check(struct socket *so) 1003 { 1004 SOCK_LOCK_ASSERT(so); 1005 1006 if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | 1007 SS_ISDISCONNECTING)) != 0) 1008 return (EINVAL); 1009 1010 /* 1011 * Sleeping is not permitted here, so simply fail if userspace is 1012 * attempting to transmit or receive on the socket. This kind of 1013 * transient failure is not ideal, but it should occur only if userspace 1014 * is misusing the socket interfaces. 1015 */ 1016 if (!sx_try_xlock(&so->so_snd_sx)) 1017 return (EAGAIN); 1018 if (!sx_try_xlock(&so->so_rcv_sx)) { 1019 sx_xunlock(&so->so_snd_sx); 1020 return (EAGAIN); 1021 } 1022 mtx_lock(&so->so_snd_mtx); 1023 mtx_lock(&so->so_rcv_mtx); 1024 1025 /* Interlock with soo_aio_queue() and KTLS. */ 1026 if (!SOLISTENING(so)) { 1027 bool ktls; 1028 1029 #ifdef KERN_TLS 1030 ktls = so->so_snd.sb_tls_info != NULL || 1031 so->so_rcv.sb_tls_info != NULL; 1032 #else 1033 ktls = false; 1034 #endif 1035 if (ktls || 1036 (so->so_snd.sb_flags & (SB_AIO | SB_AIO_RUNNING)) != 0 || 1037 (so->so_rcv.sb_flags & (SB_AIO | SB_AIO_RUNNING)) != 0) { 1038 solisten_proto_abort(so); 1039 return (EINVAL); 1040 } 1041 } 1042 1043 return (0); 1044 } 1045 1046 /* 1047 * Undo the setup done by solisten_proto_check(). 1048 */ 1049 void 1050 solisten_proto_abort(struct socket *so) 1051 { 1052 mtx_unlock(&so->so_snd_mtx); 1053 mtx_unlock(&so->so_rcv_mtx); 1054 sx_xunlock(&so->so_snd_sx); 1055 sx_xunlock(&so->so_rcv_sx); 1056 } 1057 1058 void 1059 solisten_proto(struct socket *so, int backlog) 1060 { 1061 int sbrcv_lowat, sbsnd_lowat; 1062 u_int sbrcv_hiwat, sbsnd_hiwat; 1063 short sbrcv_flags, sbsnd_flags; 1064 sbintime_t sbrcv_timeo, sbsnd_timeo; 1065 1066 SOCK_LOCK_ASSERT(so); 1067 KASSERT((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | 1068 SS_ISDISCONNECTING)) == 0, 1069 ("%s: bad socket state %p", __func__, so)); 1070 1071 if (SOLISTENING(so)) 1072 goto listening; 1073 1074 /* 1075 * Change this socket to listening state. 1076 */ 1077 sbrcv_lowat = so->so_rcv.sb_lowat; 1078 sbsnd_lowat = so->so_snd.sb_lowat; 1079 sbrcv_hiwat = so->so_rcv.sb_hiwat; 1080 sbsnd_hiwat = so->so_snd.sb_hiwat; 1081 sbrcv_flags = so->so_rcv.sb_flags; 1082 sbsnd_flags = so->so_snd.sb_flags; 1083 sbrcv_timeo = so->so_rcv.sb_timeo; 1084 sbsnd_timeo = so->so_snd.sb_timeo; 1085 1086 if (!(so->so_proto->pr_flags & PR_SOCKBUF)) { 1087 sbdestroy(so, SO_SND); 1088 sbdestroy(so, SO_RCV); 1089 } 1090 1091 #ifdef INVARIANTS 1092 bzero(&so->so_rcv, 1093 sizeof(struct socket) - offsetof(struct socket, so_rcv)); 1094 #endif 1095 1096 so->sol_sbrcv_lowat = sbrcv_lowat; 1097 so->sol_sbsnd_lowat = sbsnd_lowat; 1098 so->sol_sbrcv_hiwat = sbrcv_hiwat; 1099 so->sol_sbsnd_hiwat = sbsnd_hiwat; 1100 so->sol_sbrcv_flags = sbrcv_flags; 1101 so->sol_sbsnd_flags = sbsnd_flags; 1102 so->sol_sbrcv_timeo = sbrcv_timeo; 1103 so->sol_sbsnd_timeo = sbsnd_timeo; 1104 1105 so->sol_qlen = so->sol_incqlen = 0; 1106 TAILQ_INIT(&so->sol_incomp); 1107 TAILQ_INIT(&so->sol_comp); 1108 1109 so->sol_accept_filter = NULL; 1110 so->sol_accept_filter_arg = NULL; 1111 so->sol_accept_filter_str = NULL; 1112 1113 so->sol_upcall = NULL; 1114 so->sol_upcallarg = NULL; 1115 1116 so->so_options |= SO_ACCEPTCONN; 1117 1118 listening: 1119 if (backlog < 0 || backlog > somaxconn) 1120 backlog = somaxconn; 1121 so->sol_qlimit = backlog; 1122 1123 mtx_unlock(&so->so_snd_mtx); 1124 mtx_unlock(&so->so_rcv_mtx); 1125 sx_xunlock(&so->so_snd_sx); 1126 sx_xunlock(&so->so_rcv_sx); 1127 } 1128 1129 /* 1130 * Wakeup listeners/subsystems once we have a complete connection. 1131 * Enters with lock, returns unlocked. 1132 */ 1133 void 1134 solisten_wakeup(struct socket *sol) 1135 { 1136 1137 if (sol->sol_upcall != NULL) 1138 (void )sol->sol_upcall(sol, sol->sol_upcallarg, M_NOWAIT); 1139 else { 1140 selwakeuppri(&sol->so_rdsel, PSOCK); 1141 KNOTE_LOCKED(&sol->so_rdsel.si_note, 0); 1142 } 1143 SOLISTEN_UNLOCK(sol); 1144 wakeup_one(&sol->sol_comp); 1145 if ((sol->so_state & SS_ASYNC) && sol->so_sigio != NULL) 1146 pgsigio(&sol->so_sigio, SIGIO, 0); 1147 } 1148 1149 /* 1150 * Return single connection off a listening socket queue. Main consumer of 1151 * the function is kern_accept4(). Some modules, that do their own accept 1152 * management also use the function. The socket reference held by the 1153 * listen queue is handed to the caller. 1154 * 1155 * Listening socket must be locked on entry and is returned unlocked on 1156 * return. 1157 * The flags argument is set of accept4(2) flags and ACCEPT4_INHERIT. 1158 */ 1159 int 1160 solisten_dequeue(struct socket *head, struct socket **ret, int flags) 1161 { 1162 struct socket *so; 1163 int error; 1164 1165 SOLISTEN_LOCK_ASSERT(head); 1166 1167 while (!(head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->sol_comp) && 1168 head->so_error == 0) { 1169 error = msleep(&head->sol_comp, SOCK_MTX(head), PSOCK | PCATCH, 1170 "accept", 0); 1171 if (error != 0) { 1172 SOLISTEN_UNLOCK(head); 1173 return (error); 1174 } 1175 } 1176 if (head->so_error) { 1177 error = head->so_error; 1178 head->so_error = 0; 1179 } else if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->sol_comp)) 1180 error = EWOULDBLOCK; 1181 else 1182 error = 0; 1183 if (error) { 1184 SOLISTEN_UNLOCK(head); 1185 return (error); 1186 } 1187 so = TAILQ_FIRST(&head->sol_comp); 1188 SOCK_LOCK(so); 1189 KASSERT(so->so_qstate == SQ_COMP, 1190 ("%s: so %p not SQ_COMP", __func__, so)); 1191 head->sol_qlen--; 1192 so->so_qstate = SQ_NONE; 1193 so->so_listen = NULL; 1194 TAILQ_REMOVE(&head->sol_comp, so, so_list); 1195 if (flags & ACCEPT4_INHERIT) 1196 so->so_state |= (head->so_state & SS_NBIO); 1197 else 1198 so->so_state |= (flags & SOCK_NONBLOCK) ? SS_NBIO : 0; 1199 SOCK_UNLOCK(so); 1200 sorele_locked(head); 1201 1202 *ret = so; 1203 return (0); 1204 } 1205 1206 /* 1207 * Free socket upon release of the very last reference. 1208 */ 1209 static void 1210 sofree(struct socket *so) 1211 { 1212 struct protosw *pr = so->so_proto; 1213 1214 SOCK_LOCK_ASSERT(so); 1215 KASSERT(refcount_load(&so->so_count) == 0, 1216 ("%s: so %p has references", __func__, so)); 1217 KASSERT(SOLISTENING(so) || so->so_qstate == SQ_NONE, 1218 ("%s: so %p is on listen queue", __func__, so)); 1219 1220 SOCK_UNLOCK(so); 1221 1222 if (so->so_dtor != NULL) 1223 so->so_dtor(so); 1224 1225 VNET_SO_ASSERT(so); 1226 if (pr->pr_detach != NULL) 1227 pr->pr_detach(so); 1228 1229 /* 1230 * From this point on, we assume that no other references to this 1231 * socket exist anywhere else in the stack. Therefore, no locks need 1232 * to be acquired or held. 1233 */ 1234 if (!(pr->pr_flags & PR_SOCKBUF) && !SOLISTENING(so)) { 1235 sbdestroy(so, SO_SND); 1236 sbdestroy(so, SO_RCV); 1237 } 1238 seldrain(&so->so_rdsel); 1239 seldrain(&so->so_wrsel); 1240 knlist_destroy(&so->so_rdsel.si_note); 1241 knlist_destroy(&so->so_wrsel.si_note); 1242 sodealloc(so); 1243 } 1244 1245 /* 1246 * Release a reference on a socket while holding the socket lock. 1247 * Unlocks the socket lock before returning. 1248 */ 1249 void 1250 sorele_locked(struct socket *so) 1251 { 1252 SOCK_LOCK_ASSERT(so); 1253 if (refcount_release(&so->so_count)) 1254 sofree(so); 1255 else 1256 SOCK_UNLOCK(so); 1257 } 1258 1259 /* 1260 * Close a socket on last file table reference removal. Initiate disconnect 1261 * if connected. Free socket when disconnect complete. 1262 * 1263 * This function will sorele() the socket. Note that soclose() may be called 1264 * prior to the ref count reaching zero. The actual socket structure will 1265 * not be freed until the ref count reaches zero. 1266 */ 1267 int 1268 soclose(struct socket *so) 1269 { 1270 struct accept_queue lqueue; 1271 int error = 0; 1272 bool listening, last __diagused; 1273 1274 CURVNET_SET(so->so_vnet); 1275 funsetown(&so->so_sigio); 1276 if (so->so_state & SS_ISCONNECTED) { 1277 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 1278 error = sodisconnect(so); 1279 if (error) { 1280 if (error == ENOTCONN) 1281 error = 0; 1282 goto drop; 1283 } 1284 } 1285 1286 if ((so->so_options & SO_LINGER) != 0 && so->so_linger != 0) { 1287 if ((so->so_state & SS_ISDISCONNECTING) && 1288 (so->so_state & SS_NBIO)) 1289 goto drop; 1290 while (so->so_state & SS_ISCONNECTED) { 1291 error = tsleep(&so->so_timeo, 1292 PSOCK | PCATCH, "soclos", 1293 so->so_linger * hz); 1294 if (error) 1295 break; 1296 } 1297 } 1298 } 1299 1300 drop: 1301 if (so->so_proto->pr_close != NULL) 1302 so->so_proto->pr_close(so); 1303 1304 SOCK_LOCK(so); 1305 if ((listening = SOLISTENING(so))) { 1306 struct socket *sp; 1307 1308 TAILQ_INIT(&lqueue); 1309 TAILQ_SWAP(&lqueue, &so->sol_incomp, socket, so_list); 1310 TAILQ_CONCAT(&lqueue, &so->sol_comp, so_list); 1311 1312 so->sol_qlen = so->sol_incqlen = 0; 1313 1314 TAILQ_FOREACH(sp, &lqueue, so_list) { 1315 SOCK_LOCK(sp); 1316 sp->so_qstate = SQ_NONE; 1317 sp->so_listen = NULL; 1318 SOCK_UNLOCK(sp); 1319 last = refcount_release(&so->so_count); 1320 KASSERT(!last, ("%s: released last reference for %p", 1321 __func__, so)); 1322 } 1323 } 1324 sorele_locked(so); 1325 if (listening) { 1326 struct socket *sp, *tsp; 1327 1328 TAILQ_FOREACH_SAFE(sp, &lqueue, so_list, tsp) 1329 soabort(sp); 1330 } 1331 CURVNET_RESTORE(); 1332 return (error); 1333 } 1334 1335 /* 1336 * soabort() is used to abruptly tear down a connection, such as when a 1337 * resource limit is reached (listen queue depth exceeded), or if a listen 1338 * socket is closed while there are sockets waiting to be accepted. 1339 * 1340 * This interface is tricky, because it is called on an unreferenced socket, 1341 * and must be called only by a thread that has actually removed the socket 1342 * from the listen queue it was on. Likely this thread holds the last 1343 * reference on the socket and soabort() will proceed with sofree(). But 1344 * it might be not the last, as the sockets on the listen queues are seen 1345 * from the protocol side. 1346 * 1347 * This interface will call into the protocol code, so must not be called 1348 * with any socket locks held. Protocols do call it while holding their own 1349 * recursible protocol mutexes, but this is something that should be subject 1350 * to review in the future. 1351 * 1352 * Usually socket should have a single reference left, but this is not a 1353 * requirement. In the past, when we have had named references for file 1354 * descriptor and protocol, we asserted that none of them are being held. 1355 */ 1356 void 1357 soabort(struct socket *so) 1358 { 1359 1360 VNET_SO_ASSERT(so); 1361 1362 if (so->so_proto->pr_abort != NULL) 1363 so->so_proto->pr_abort(so); 1364 SOCK_LOCK(so); 1365 sorele_locked(so); 1366 } 1367 1368 int 1369 soaccept(struct socket *so, struct sockaddr *sa) 1370 { 1371 #ifdef INVARIANTS 1372 u_char len = sa->sa_len; 1373 #endif 1374 int error; 1375 1376 CURVNET_SET(so->so_vnet); 1377 error = so->so_proto->pr_accept(so, sa); 1378 KASSERT(sa->sa_len <= len, 1379 ("%s: protocol %p sockaddr overflow", __func__, so->so_proto)); 1380 CURVNET_RESTORE(); 1381 return (error); 1382 } 1383 1384 int 1385 sopeeraddr(struct socket *so, struct sockaddr *sa) 1386 { 1387 #ifdef INVARIANTS 1388 u_char len = sa->sa_len; 1389 #endif 1390 int error; 1391 1392 CURVNET_SET(so->so_vnet); 1393 error = so->so_proto->pr_peeraddr(so, sa); 1394 KASSERT(sa->sa_len <= len, 1395 ("%s: protocol %p sockaddr overflow", __func__, so->so_proto)); 1396 CURVNET_RESTORE(); 1397 1398 return (error); 1399 } 1400 1401 int 1402 sosockaddr(struct socket *so, struct sockaddr *sa) 1403 { 1404 #ifdef INVARIANTS 1405 u_char len = sa->sa_len; 1406 #endif 1407 int error; 1408 1409 CURVNET_SET(so->so_vnet); 1410 error = so->so_proto->pr_sockaddr(so, sa); 1411 KASSERT(sa->sa_len <= len, 1412 ("%s: protocol %p sockaddr overflow", __func__, so->so_proto)); 1413 CURVNET_RESTORE(); 1414 1415 return (error); 1416 } 1417 1418 int 1419 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td) 1420 { 1421 1422 return (soconnectat(AT_FDCWD, so, nam, td)); 1423 } 1424 1425 int 1426 soconnectat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td) 1427 { 1428 int error; 1429 1430 CURVNET_SET(so->so_vnet); 1431 1432 /* 1433 * If protocol is connection-based, can only connect once. 1434 * Otherwise, if connected, try to disconnect first. This allows 1435 * user to disconnect by connecting to, e.g., a null address. 1436 * 1437 * Note, this check is racy and may need to be re-evaluated at the 1438 * protocol layer. 1439 */ 1440 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 1441 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 1442 (error = sodisconnect(so)))) { 1443 error = EISCONN; 1444 } else { 1445 /* 1446 * Prevent accumulated error from previous connection from 1447 * biting us. 1448 */ 1449 so->so_error = 0; 1450 if (fd == AT_FDCWD) { 1451 error = so->so_proto->pr_connect(so, nam, td); 1452 } else { 1453 error = so->so_proto->pr_connectat(fd, so, nam, td); 1454 } 1455 } 1456 CURVNET_RESTORE(); 1457 1458 return (error); 1459 } 1460 1461 int 1462 soconnect2(struct socket *so1, struct socket *so2) 1463 { 1464 int error; 1465 1466 CURVNET_SET(so1->so_vnet); 1467 error = so1->so_proto->pr_connect2(so1, so2); 1468 CURVNET_RESTORE(); 1469 return (error); 1470 } 1471 1472 int 1473 sodisconnect(struct socket *so) 1474 { 1475 int error; 1476 1477 if ((so->so_state & SS_ISCONNECTED) == 0) 1478 return (ENOTCONN); 1479 if (so->so_state & SS_ISDISCONNECTING) 1480 return (EALREADY); 1481 VNET_SO_ASSERT(so); 1482 error = so->so_proto->pr_disconnect(so); 1483 return (error); 1484 } 1485 1486 int 1487 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio, 1488 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 1489 { 1490 long space; 1491 ssize_t resid; 1492 int clen = 0, error, dontroute; 1493 1494 KASSERT(so->so_type == SOCK_DGRAM, ("sosend_dgram: !SOCK_DGRAM")); 1495 KASSERT(so->so_proto->pr_flags & PR_ATOMIC, 1496 ("sosend_dgram: !PR_ATOMIC")); 1497 1498 if (uio != NULL) 1499 resid = uio->uio_resid; 1500 else 1501 resid = top->m_pkthdr.len; 1502 /* 1503 * In theory resid should be unsigned. However, space must be 1504 * signed, as it might be less than 0 if we over-committed, and we 1505 * must use a signed comparison of space and resid. On the other 1506 * hand, a negative resid causes us to loop sending 0-length 1507 * segments to the protocol. 1508 */ 1509 if (resid < 0) { 1510 error = EINVAL; 1511 goto out; 1512 } 1513 1514 dontroute = 1515 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0; 1516 if (td != NULL) 1517 td->td_ru.ru_msgsnd++; 1518 if (control != NULL) 1519 clen = control->m_len; 1520 1521 SOCKBUF_LOCK(&so->so_snd); 1522 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 1523 SOCKBUF_UNLOCK(&so->so_snd); 1524 error = EPIPE; 1525 goto out; 1526 } 1527 if (so->so_error) { 1528 error = so->so_error; 1529 so->so_error = 0; 1530 SOCKBUF_UNLOCK(&so->so_snd); 1531 goto out; 1532 } 1533 if ((so->so_state & SS_ISCONNECTED) == 0) { 1534 /* 1535 * `sendto' and `sendmsg' is allowed on a connection-based 1536 * socket if it supports implied connect. Return ENOTCONN if 1537 * not connected and no address is supplied. 1538 */ 1539 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 1540 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 1541 if (!(resid == 0 && clen != 0)) { 1542 SOCKBUF_UNLOCK(&so->so_snd); 1543 error = ENOTCONN; 1544 goto out; 1545 } 1546 } else if (addr == NULL) { 1547 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 1548 error = ENOTCONN; 1549 else 1550 error = EDESTADDRREQ; 1551 SOCKBUF_UNLOCK(&so->so_snd); 1552 goto out; 1553 } 1554 } 1555 1556 /* 1557 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a 1558 * problem and need fixing. 1559 */ 1560 space = sbspace(&so->so_snd); 1561 if (flags & MSG_OOB) 1562 space += 1024; 1563 space -= clen; 1564 SOCKBUF_UNLOCK(&so->so_snd); 1565 if (resid > space) { 1566 error = EMSGSIZE; 1567 goto out; 1568 } 1569 if (uio == NULL) { 1570 resid = 0; 1571 if (flags & MSG_EOR) 1572 top->m_flags |= M_EOR; 1573 } else { 1574 /* 1575 * Copy the data from userland into a mbuf chain. 1576 * If no data is to be copied in, a single empty mbuf 1577 * is returned. 1578 */ 1579 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr, 1580 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0))); 1581 if (top == NULL) { 1582 error = EFAULT; /* only possible error */ 1583 goto out; 1584 } 1585 space -= resid - uio->uio_resid; 1586 resid = uio->uio_resid; 1587 } 1588 KASSERT(resid == 0, ("sosend_dgram: resid != 0")); 1589 /* 1590 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock 1591 * than with. 1592 */ 1593 if (dontroute) { 1594 SOCK_LOCK(so); 1595 so->so_options |= SO_DONTROUTE; 1596 SOCK_UNLOCK(so); 1597 } 1598 /* 1599 * XXX all the SBS_CANTSENDMORE checks previously done could be out 1600 * of date. We could have received a reset packet in an interrupt or 1601 * maybe we slept while doing page faults in uiomove() etc. We could 1602 * probably recheck again inside the locking protection here, but 1603 * there are probably other places that this also happens. We must 1604 * rethink this. 1605 */ 1606 VNET_SO_ASSERT(so); 1607 error = so->so_proto->pr_send(so, (flags & MSG_OOB) ? PRUS_OOB : 1608 /* 1609 * If the user set MSG_EOF, the protocol understands this flag and 1610 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND. 1611 */ 1612 ((flags & MSG_EOF) && 1613 (so->so_proto->pr_flags & PR_IMPLOPCL) && 1614 (resid <= 0)) ? 1615 PRUS_EOF : 1616 /* If there is more to send set PRUS_MORETOCOME */ 1617 (flags & MSG_MORETOCOME) || 1618 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 1619 top, addr, control, td); 1620 if (dontroute) { 1621 SOCK_LOCK(so); 1622 so->so_options &= ~SO_DONTROUTE; 1623 SOCK_UNLOCK(so); 1624 } 1625 clen = 0; 1626 control = NULL; 1627 top = NULL; 1628 out: 1629 if (top != NULL) 1630 m_freem(top); 1631 if (control != NULL) 1632 m_freem(control); 1633 return (error); 1634 } 1635 1636 /* 1637 * Send on a socket. If send must go all at once and message is larger than 1638 * send buffering, then hard error. Lock against other senders. If must go 1639 * all at once and not enough room now, then inform user that this would 1640 * block and do nothing. Otherwise, if nonblocking, send as much as 1641 * possible. The data to be sent is described by "uio" if nonzero, otherwise 1642 * by the mbuf chain "top" (which must be null if uio is not). Data provided 1643 * in mbuf chain must be small enough to send all at once. 1644 * 1645 * Returns nonzero on error, timeout or signal; callers must check for short 1646 * counts if EINTR/ERESTART are returned. Data and control buffers are freed 1647 * on return. 1648 */ 1649 int 1650 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio, 1651 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 1652 { 1653 long space; 1654 ssize_t resid; 1655 int clen = 0, error, dontroute; 1656 int atomic = sosendallatonce(so) || top; 1657 int pr_send_flag; 1658 #ifdef KERN_TLS 1659 struct ktls_session *tls; 1660 int tls_enq_cnt, tls_send_flag; 1661 uint8_t tls_rtype; 1662 1663 tls = NULL; 1664 tls_rtype = TLS_RLTYPE_APP; 1665 #endif 1666 if (uio != NULL) 1667 resid = uio->uio_resid; 1668 else if ((top->m_flags & M_PKTHDR) != 0) 1669 resid = top->m_pkthdr.len; 1670 else 1671 resid = m_length(top, NULL); 1672 /* 1673 * In theory resid should be unsigned. However, space must be 1674 * signed, as it might be less than 0 if we over-committed, and we 1675 * must use a signed comparison of space and resid. On the other 1676 * hand, a negative resid causes us to loop sending 0-length 1677 * segments to the protocol. 1678 * 1679 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 1680 * type sockets since that's an error. 1681 */ 1682 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) { 1683 error = EINVAL; 1684 goto out; 1685 } 1686 1687 dontroute = 1688 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 1689 (so->so_proto->pr_flags & PR_ATOMIC); 1690 if (td != NULL) 1691 td->td_ru.ru_msgsnd++; 1692 if (control != NULL) 1693 clen = control->m_len; 1694 1695 error = SOCK_IO_SEND_LOCK(so, SBLOCKWAIT(flags)); 1696 if (error) 1697 goto out; 1698 1699 #ifdef KERN_TLS 1700 tls_send_flag = 0; 1701 tls = ktls_hold(so->so_snd.sb_tls_info); 1702 if (tls != NULL) { 1703 if (tls->mode == TCP_TLS_MODE_SW) 1704 tls_send_flag = PRUS_NOTREADY; 1705 1706 if (control != NULL) { 1707 struct cmsghdr *cm = mtod(control, struct cmsghdr *); 1708 1709 if (clen >= sizeof(*cm) && 1710 cm->cmsg_type == TLS_SET_RECORD_TYPE) { 1711 tls_rtype = *((uint8_t *)CMSG_DATA(cm)); 1712 clen = 0; 1713 m_freem(control); 1714 control = NULL; 1715 atomic = 1; 1716 } 1717 } 1718 1719 if (resid == 0 && !ktls_permit_empty_frames(tls)) { 1720 error = EINVAL; 1721 goto release; 1722 } 1723 } 1724 #endif 1725 1726 restart: 1727 do { 1728 SOCKBUF_LOCK(&so->so_snd); 1729 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 1730 SOCKBUF_UNLOCK(&so->so_snd); 1731 error = EPIPE; 1732 goto release; 1733 } 1734 if (so->so_error) { 1735 error = so->so_error; 1736 so->so_error = 0; 1737 SOCKBUF_UNLOCK(&so->so_snd); 1738 goto release; 1739 } 1740 if ((so->so_state & SS_ISCONNECTED) == 0) { 1741 /* 1742 * `sendto' and `sendmsg' is allowed on a connection- 1743 * based socket if it supports implied connect. 1744 * Return ENOTCONN if not connected and no address is 1745 * supplied. 1746 */ 1747 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 1748 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 1749 if (!(resid == 0 && clen != 0)) { 1750 SOCKBUF_UNLOCK(&so->so_snd); 1751 error = ENOTCONN; 1752 goto release; 1753 } 1754 } else if (addr == NULL) { 1755 SOCKBUF_UNLOCK(&so->so_snd); 1756 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 1757 error = ENOTCONN; 1758 else 1759 error = EDESTADDRREQ; 1760 goto release; 1761 } 1762 } 1763 space = sbspace(&so->so_snd); 1764 if (flags & MSG_OOB) 1765 space += 1024; 1766 if ((atomic && resid > so->so_snd.sb_hiwat) || 1767 clen > so->so_snd.sb_hiwat) { 1768 SOCKBUF_UNLOCK(&so->so_snd); 1769 error = EMSGSIZE; 1770 goto release; 1771 } 1772 if (space < resid + clen && 1773 (atomic || space < so->so_snd.sb_lowat || space < clen)) { 1774 if ((so->so_state & SS_NBIO) || 1775 (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0) { 1776 SOCKBUF_UNLOCK(&so->so_snd); 1777 error = EWOULDBLOCK; 1778 goto release; 1779 } 1780 error = sbwait(so, SO_SND); 1781 SOCKBUF_UNLOCK(&so->so_snd); 1782 if (error) 1783 goto release; 1784 goto restart; 1785 } 1786 SOCKBUF_UNLOCK(&so->so_snd); 1787 space -= clen; 1788 do { 1789 if (uio == NULL) { 1790 resid = 0; 1791 if (flags & MSG_EOR) 1792 top->m_flags |= M_EOR; 1793 #ifdef KERN_TLS 1794 if (tls != NULL) { 1795 ktls_frame(top, tls, &tls_enq_cnt, 1796 tls_rtype); 1797 tls_rtype = TLS_RLTYPE_APP; 1798 } 1799 #endif 1800 } else { 1801 /* 1802 * Copy the data from userland into a mbuf 1803 * chain. If resid is 0, which can happen 1804 * only if we have control to send, then 1805 * a single empty mbuf is returned. This 1806 * is a workaround to prevent protocol send 1807 * methods to panic. 1808 */ 1809 #ifdef KERN_TLS 1810 if (tls != NULL) { 1811 top = m_uiotombuf(uio, M_WAITOK, space, 1812 tls->params.max_frame_len, 1813 M_EXTPG | 1814 ((flags & MSG_EOR) ? M_EOR : 0)); 1815 if (top != NULL) { 1816 ktls_frame(top, tls, 1817 &tls_enq_cnt, tls_rtype); 1818 } 1819 tls_rtype = TLS_RLTYPE_APP; 1820 } else 1821 #endif 1822 top = m_uiotombuf(uio, M_WAITOK, space, 1823 (atomic ? max_hdr : 0), 1824 (atomic ? M_PKTHDR : 0) | 1825 ((flags & MSG_EOR) ? M_EOR : 0)); 1826 if (top == NULL) { 1827 error = EFAULT; /* only possible error */ 1828 goto release; 1829 } 1830 space -= resid - uio->uio_resid; 1831 resid = uio->uio_resid; 1832 } 1833 if (dontroute) { 1834 SOCK_LOCK(so); 1835 so->so_options |= SO_DONTROUTE; 1836 SOCK_UNLOCK(so); 1837 } 1838 /* 1839 * XXX all the SBS_CANTSENDMORE checks previously 1840 * done could be out of date. We could have received 1841 * a reset packet in an interrupt or maybe we slept 1842 * while doing page faults in uiomove() etc. We 1843 * could probably recheck again inside the locking 1844 * protection here, but there are probably other 1845 * places that this also happens. We must rethink 1846 * this. 1847 */ 1848 VNET_SO_ASSERT(so); 1849 1850 pr_send_flag = (flags & MSG_OOB) ? PRUS_OOB : 1851 /* 1852 * If the user set MSG_EOF, the protocol understands 1853 * this flag and nothing left to send then use 1854 * PRU_SEND_EOF instead of PRU_SEND. 1855 */ 1856 ((flags & MSG_EOF) && 1857 (so->so_proto->pr_flags & PR_IMPLOPCL) && 1858 (resid <= 0)) ? 1859 PRUS_EOF : 1860 /* If there is more to send set PRUS_MORETOCOME. */ 1861 (flags & MSG_MORETOCOME) || 1862 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0; 1863 1864 #ifdef KERN_TLS 1865 pr_send_flag |= tls_send_flag; 1866 #endif 1867 1868 error = so->so_proto->pr_send(so, pr_send_flag, top, 1869 addr, control, td); 1870 1871 if (dontroute) { 1872 SOCK_LOCK(so); 1873 so->so_options &= ~SO_DONTROUTE; 1874 SOCK_UNLOCK(so); 1875 } 1876 1877 #ifdef KERN_TLS 1878 if (tls != NULL && tls->mode == TCP_TLS_MODE_SW) { 1879 if (error != 0) { 1880 m_freem(top); 1881 top = NULL; 1882 } else { 1883 soref(so); 1884 ktls_enqueue(top, so, tls_enq_cnt); 1885 } 1886 } 1887 #endif 1888 clen = 0; 1889 control = NULL; 1890 top = NULL; 1891 if (error) 1892 goto release; 1893 } while (resid && space > 0); 1894 } while (resid); 1895 1896 release: 1897 SOCK_IO_SEND_UNLOCK(so); 1898 out: 1899 #ifdef KERN_TLS 1900 if (tls != NULL) 1901 ktls_free(tls); 1902 #endif 1903 if (top != NULL) 1904 m_freem(top); 1905 if (control != NULL) 1906 m_freem(control); 1907 return (error); 1908 } 1909 1910 /* 1911 * Send to a socket from a kernel thread. 1912 * 1913 * XXXGL: in almost all cases uio is NULL and the mbuf is supplied. 1914 * Exception is nfs/bootp_subr.c. It is arguable that the VNET context needs 1915 * to be set at all. This function should just boil down to a static inline 1916 * calling the protocol method. 1917 */ 1918 int 1919 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, 1920 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 1921 { 1922 int error; 1923 1924 CURVNET_SET(so->so_vnet); 1925 error = so->so_proto->pr_sosend(so, addr, uio, 1926 top, control, flags, td); 1927 CURVNET_RESTORE(); 1928 return (error); 1929 } 1930 1931 /* 1932 * send(2), write(2) or aio_write(2) on a socket. 1933 */ 1934 int 1935 sousrsend(struct socket *so, struct sockaddr *addr, struct uio *uio, 1936 struct mbuf *control, int flags, struct proc *userproc) 1937 { 1938 struct thread *td; 1939 ssize_t len; 1940 int error; 1941 1942 td = uio->uio_td; 1943 len = uio->uio_resid; 1944 CURVNET_SET(so->so_vnet); 1945 error = so->so_proto->pr_sosend(so, addr, uio, NULL, control, flags, 1946 td); 1947 CURVNET_RESTORE(); 1948 if (error != 0) { 1949 /* 1950 * Clear transient errors for stream protocols if they made 1951 * some progress. Make exclusion for aio(4) that would 1952 * schedule a new write in case of EWOULDBLOCK and clear 1953 * error itself. See soaio_process_job(). 1954 */ 1955 if (uio->uio_resid != len && 1956 (so->so_proto->pr_flags & PR_ATOMIC) == 0 && 1957 userproc == NULL && 1958 (error == ERESTART || error == EINTR || 1959 error == EWOULDBLOCK)) 1960 error = 0; 1961 /* Generation of SIGPIPE can be controlled per socket. */ 1962 if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0 && 1963 (flags & MSG_NOSIGNAL) == 0) { 1964 if (userproc != NULL) { 1965 /* aio(4) job */ 1966 PROC_LOCK(userproc); 1967 kern_psignal(userproc, SIGPIPE); 1968 PROC_UNLOCK(userproc); 1969 } else { 1970 PROC_LOCK(td->td_proc); 1971 tdsignal(td, SIGPIPE); 1972 PROC_UNLOCK(td->td_proc); 1973 } 1974 } 1975 } 1976 return (error); 1977 } 1978 1979 /* 1980 * The part of soreceive() that implements reading non-inline out-of-band 1981 * data from a socket. For more complete comments, see soreceive(), from 1982 * which this code originated. 1983 * 1984 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is 1985 * unable to return an mbuf chain to the caller. 1986 */ 1987 static int 1988 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags) 1989 { 1990 struct protosw *pr = so->so_proto; 1991 struct mbuf *m; 1992 int error; 1993 1994 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0")); 1995 VNET_SO_ASSERT(so); 1996 1997 m = m_get(M_WAITOK, MT_DATA); 1998 error = pr->pr_rcvoob(so, m, flags & MSG_PEEK); 1999 if (error) 2000 goto bad; 2001 do { 2002 error = uiomove(mtod(m, void *), 2003 (int) min(uio->uio_resid, m->m_len), uio); 2004 m = m_free(m); 2005 } while (uio->uio_resid && error == 0 && m); 2006 bad: 2007 if (m != NULL) 2008 m_freem(m); 2009 return (error); 2010 } 2011 2012 /* 2013 * Following replacement or removal of the first mbuf on the first mbuf chain 2014 * of a socket buffer, push necessary state changes back into the socket 2015 * buffer so that other consumers see the values consistently. 'nextrecord' 2016 * is the callers locally stored value of the original value of 2017 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes. 2018 * NOTE: 'nextrecord' may be NULL. 2019 */ 2020 static __inline void 2021 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord) 2022 { 2023 2024 SOCKBUF_LOCK_ASSERT(sb); 2025 /* 2026 * First, update for the new value of nextrecord. If necessary, make 2027 * it the first record. 2028 */ 2029 if (sb->sb_mb != NULL) 2030 sb->sb_mb->m_nextpkt = nextrecord; 2031 else 2032 sb->sb_mb = nextrecord; 2033 2034 /* 2035 * Now update any dependent socket buffer fields to reflect the new 2036 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the 2037 * addition of a second clause that takes care of the case where 2038 * sb_mb has been updated, but remains the last record. 2039 */ 2040 if (sb->sb_mb == NULL) { 2041 sb->sb_mbtail = NULL; 2042 sb->sb_lastrecord = NULL; 2043 } else if (sb->sb_mb->m_nextpkt == NULL) 2044 sb->sb_lastrecord = sb->sb_mb; 2045 } 2046 2047 /* 2048 * Implement receive operations on a socket. We depend on the way that 2049 * records are added to the sockbuf by sbappend. In particular, each record 2050 * (mbufs linked through m_next) must begin with an address if the protocol 2051 * so specifies, followed by an optional mbuf or mbufs containing ancillary 2052 * data, and then zero or more mbufs of data. In order to allow parallelism 2053 * between network receive and copying to user space, as well as avoid 2054 * sleeping with a mutex held, we release the socket buffer mutex during the 2055 * user space copy. Although the sockbuf is locked, new data may still be 2056 * appended, and thus we must maintain consistency of the sockbuf during that 2057 * time. 2058 * 2059 * The caller may receive the data as a single mbuf chain by supplying an 2060 * mbuf **mp0 for use in returning the chain. The uio is then used only for 2061 * the count in uio_resid. 2062 */ 2063 int 2064 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio, 2065 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 2066 { 2067 struct mbuf *m, **mp; 2068 int flags, error, offset; 2069 ssize_t len; 2070 struct protosw *pr = so->so_proto; 2071 struct mbuf *nextrecord; 2072 int moff, type = 0; 2073 ssize_t orig_resid = uio->uio_resid; 2074 bool report_real_len = false; 2075 2076 mp = mp0; 2077 if (psa != NULL) 2078 *psa = NULL; 2079 if (controlp != NULL) 2080 *controlp = NULL; 2081 if (flagsp != NULL) { 2082 report_real_len = *flagsp & MSG_TRUNC; 2083 *flagsp &= ~MSG_TRUNC; 2084 flags = *flagsp &~ MSG_EOR; 2085 } else 2086 flags = 0; 2087 if (flags & MSG_OOB) 2088 return (soreceive_rcvoob(so, uio, flags)); 2089 if (mp != NULL) 2090 *mp = NULL; 2091 2092 error = SOCK_IO_RECV_LOCK(so, SBLOCKWAIT(flags)); 2093 if (error) 2094 return (error); 2095 2096 restart: 2097 SOCKBUF_LOCK(&so->so_rcv); 2098 m = so->so_rcv.sb_mb; 2099 /* 2100 * If we have less data than requested, block awaiting more (subject 2101 * to any timeout) if: 2102 * 1. the current count is less than the low water mark, or 2103 * 2. MSG_DONTWAIT is not set 2104 */ 2105 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && 2106 sbavail(&so->so_rcv) < uio->uio_resid) && 2107 sbavail(&so->so_rcv) < so->so_rcv.sb_lowat && 2108 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) { 2109 KASSERT(m != NULL || !sbavail(&so->so_rcv), 2110 ("receive: m == %p sbavail == %u", 2111 m, sbavail(&so->so_rcv))); 2112 if (so->so_error || so->so_rerror) { 2113 if (m != NULL) 2114 goto dontblock; 2115 if (so->so_error) 2116 error = so->so_error; 2117 else 2118 error = so->so_rerror; 2119 if ((flags & MSG_PEEK) == 0) { 2120 if (so->so_error) 2121 so->so_error = 0; 2122 else 2123 so->so_rerror = 0; 2124 } 2125 SOCKBUF_UNLOCK(&so->so_rcv); 2126 goto release; 2127 } 2128 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2129 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 2130 if (m != NULL) 2131 goto dontblock; 2132 #ifdef KERN_TLS 2133 else if (so->so_rcv.sb_tlsdcc == 0 && 2134 so->so_rcv.sb_tlscc == 0) { 2135 #else 2136 else { 2137 #endif 2138 SOCKBUF_UNLOCK(&so->so_rcv); 2139 goto release; 2140 } 2141 } 2142 for (; m != NULL; m = m->m_next) 2143 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 2144 m = so->so_rcv.sb_mb; 2145 goto dontblock; 2146 } 2147 if ((so->so_state & (SS_ISCONNECTING | SS_ISCONNECTED | 2148 SS_ISDISCONNECTING | SS_ISDISCONNECTED)) == 0 && 2149 (so->so_proto->pr_flags & PR_CONNREQUIRED) != 0) { 2150 SOCKBUF_UNLOCK(&so->so_rcv); 2151 error = ENOTCONN; 2152 goto release; 2153 } 2154 if (uio->uio_resid == 0 && !report_real_len) { 2155 SOCKBUF_UNLOCK(&so->so_rcv); 2156 goto release; 2157 } 2158 if ((so->so_state & SS_NBIO) || 2159 (flags & (MSG_DONTWAIT|MSG_NBIO))) { 2160 SOCKBUF_UNLOCK(&so->so_rcv); 2161 error = EWOULDBLOCK; 2162 goto release; 2163 } 2164 SBLASTRECORDCHK(&so->so_rcv); 2165 SBLASTMBUFCHK(&so->so_rcv); 2166 error = sbwait(so, SO_RCV); 2167 SOCKBUF_UNLOCK(&so->so_rcv); 2168 if (error) 2169 goto release; 2170 goto restart; 2171 } 2172 dontblock: 2173 /* 2174 * From this point onward, we maintain 'nextrecord' as a cache of the 2175 * pointer to the next record in the socket buffer. We must keep the 2176 * various socket buffer pointers and local stack versions of the 2177 * pointers in sync, pushing out modifications before dropping the 2178 * socket buffer mutex, and re-reading them when picking it up. 2179 * 2180 * Otherwise, we will race with the network stack appending new data 2181 * or records onto the socket buffer by using inconsistent/stale 2182 * versions of the field, possibly resulting in socket buffer 2183 * corruption. 2184 * 2185 * By holding the high-level sblock(), we prevent simultaneous 2186 * readers from pulling off the front of the socket buffer. 2187 */ 2188 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2189 if (uio->uio_td) 2190 uio->uio_td->td_ru.ru_msgrcv++; 2191 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb")); 2192 SBLASTRECORDCHK(&so->so_rcv); 2193 SBLASTMBUFCHK(&so->so_rcv); 2194 nextrecord = m->m_nextpkt; 2195 if (pr->pr_flags & PR_ADDR) { 2196 KASSERT(m->m_type == MT_SONAME, 2197 ("m->m_type == %d", m->m_type)); 2198 orig_resid = 0; 2199 if (psa != NULL) 2200 *psa = sodupsockaddr(mtod(m, struct sockaddr *), 2201 M_NOWAIT); 2202 if (flags & MSG_PEEK) { 2203 m = m->m_next; 2204 } else { 2205 sbfree(&so->so_rcv, m); 2206 so->so_rcv.sb_mb = m_free(m); 2207 m = so->so_rcv.sb_mb; 2208 sockbuf_pushsync(&so->so_rcv, nextrecord); 2209 } 2210 } 2211 2212 /* 2213 * Process one or more MT_CONTROL mbufs present before any data mbufs 2214 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we 2215 * just copy the data; if !MSG_PEEK, we call into the protocol to 2216 * perform externalization (or freeing if controlp == NULL). 2217 */ 2218 if (m != NULL && m->m_type == MT_CONTROL) { 2219 struct mbuf *cm = NULL, *cmn; 2220 struct mbuf **cme = &cm; 2221 #ifdef KERN_TLS 2222 struct cmsghdr *cmsg; 2223 struct tls_get_record tgr; 2224 2225 /* 2226 * For MSG_TLSAPPDATA, check for an alert record. 2227 * If found, return ENXIO without removing 2228 * it from the receive queue. This allows a subsequent 2229 * call without MSG_TLSAPPDATA to receive it. 2230 * Note that, for TLS, there should only be a single 2231 * control mbuf with the TLS_GET_RECORD message in it. 2232 */ 2233 if (flags & MSG_TLSAPPDATA) { 2234 cmsg = mtod(m, struct cmsghdr *); 2235 if (cmsg->cmsg_type == TLS_GET_RECORD && 2236 cmsg->cmsg_len == CMSG_LEN(sizeof(tgr))) { 2237 memcpy(&tgr, CMSG_DATA(cmsg), sizeof(tgr)); 2238 if (__predict_false(tgr.tls_type == 2239 TLS_RLTYPE_ALERT)) { 2240 SOCKBUF_UNLOCK(&so->so_rcv); 2241 error = ENXIO; 2242 goto release; 2243 } 2244 } 2245 } 2246 #endif 2247 2248 do { 2249 if (flags & MSG_PEEK) { 2250 if (controlp != NULL) { 2251 *controlp = m_copym(m, 0, m->m_len, 2252 M_NOWAIT); 2253 controlp = &(*controlp)->m_next; 2254 } 2255 m = m->m_next; 2256 } else { 2257 sbfree(&so->so_rcv, m); 2258 so->so_rcv.sb_mb = m->m_next; 2259 m->m_next = NULL; 2260 *cme = m; 2261 cme = &(*cme)->m_next; 2262 m = so->so_rcv.sb_mb; 2263 } 2264 } while (m != NULL && m->m_type == MT_CONTROL); 2265 if ((flags & MSG_PEEK) == 0) 2266 sockbuf_pushsync(&so->so_rcv, nextrecord); 2267 while (cm != NULL) { 2268 cmn = cm->m_next; 2269 cm->m_next = NULL; 2270 if (pr->pr_domain->dom_externalize != NULL) { 2271 SOCKBUF_UNLOCK(&so->so_rcv); 2272 VNET_SO_ASSERT(so); 2273 error = (*pr->pr_domain->dom_externalize) 2274 (cm, controlp, flags); 2275 SOCKBUF_LOCK(&so->so_rcv); 2276 } else if (controlp != NULL) 2277 *controlp = cm; 2278 else 2279 m_freem(cm); 2280 if (controlp != NULL) { 2281 while (*controlp != NULL) 2282 controlp = &(*controlp)->m_next; 2283 } 2284 cm = cmn; 2285 } 2286 if (m != NULL) 2287 nextrecord = so->so_rcv.sb_mb->m_nextpkt; 2288 else 2289 nextrecord = so->so_rcv.sb_mb; 2290 orig_resid = 0; 2291 } 2292 if (m != NULL) { 2293 if ((flags & MSG_PEEK) == 0) { 2294 KASSERT(m->m_nextpkt == nextrecord, 2295 ("soreceive: post-control, nextrecord !sync")); 2296 if (nextrecord == NULL) { 2297 KASSERT(so->so_rcv.sb_mb == m, 2298 ("soreceive: post-control, sb_mb!=m")); 2299 KASSERT(so->so_rcv.sb_lastrecord == m, 2300 ("soreceive: post-control, lastrecord!=m")); 2301 } 2302 } 2303 type = m->m_type; 2304 if (type == MT_OOBDATA) 2305 flags |= MSG_OOB; 2306 } else { 2307 if ((flags & MSG_PEEK) == 0) { 2308 KASSERT(so->so_rcv.sb_mb == nextrecord, 2309 ("soreceive: sb_mb != nextrecord")); 2310 if (so->so_rcv.sb_mb == NULL) { 2311 KASSERT(so->so_rcv.sb_lastrecord == NULL, 2312 ("soreceive: sb_lastercord != NULL")); 2313 } 2314 } 2315 } 2316 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2317 SBLASTRECORDCHK(&so->so_rcv); 2318 SBLASTMBUFCHK(&so->so_rcv); 2319 2320 /* 2321 * Now continue to read any data mbufs off of the head of the socket 2322 * buffer until the read request is satisfied. Note that 'type' is 2323 * used to store the type of any mbuf reads that have happened so far 2324 * such that soreceive() can stop reading if the type changes, which 2325 * causes soreceive() to return only one of regular data and inline 2326 * out-of-band data in a single socket receive operation. 2327 */ 2328 moff = 0; 2329 offset = 0; 2330 while (m != NULL && !(m->m_flags & M_NOTAVAIL) && uio->uio_resid > 0 2331 && error == 0) { 2332 /* 2333 * If the type of mbuf has changed since the last mbuf 2334 * examined ('type'), end the receive operation. 2335 */ 2336 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2337 if (m->m_type == MT_OOBDATA || m->m_type == MT_CONTROL) { 2338 if (type != m->m_type) 2339 break; 2340 } else if (type == MT_OOBDATA) 2341 break; 2342 else 2343 KASSERT(m->m_type == MT_DATA, 2344 ("m->m_type == %d", m->m_type)); 2345 so->so_rcv.sb_state &= ~SBS_RCVATMARK; 2346 len = uio->uio_resid; 2347 if (so->so_oobmark && len > so->so_oobmark - offset) 2348 len = so->so_oobmark - offset; 2349 if (len > m->m_len - moff) 2350 len = m->m_len - moff; 2351 /* 2352 * If mp is set, just pass back the mbufs. Otherwise copy 2353 * them out via the uio, then free. Sockbuf must be 2354 * consistent here (points to current mbuf, it points to next 2355 * record) when we drop priority; we must note any additions 2356 * to the sockbuf when we block interrupts again. 2357 */ 2358 if (mp == NULL) { 2359 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2360 SBLASTRECORDCHK(&so->so_rcv); 2361 SBLASTMBUFCHK(&so->so_rcv); 2362 SOCKBUF_UNLOCK(&so->so_rcv); 2363 if ((m->m_flags & M_EXTPG) != 0) 2364 error = m_unmapped_uiomove(m, moff, uio, 2365 (int)len); 2366 else 2367 error = uiomove(mtod(m, char *) + moff, 2368 (int)len, uio); 2369 SOCKBUF_LOCK(&so->so_rcv); 2370 if (error) { 2371 /* 2372 * The MT_SONAME mbuf has already been removed 2373 * from the record, so it is necessary to 2374 * remove the data mbufs, if any, to preserve 2375 * the invariant in the case of PR_ADDR that 2376 * requires MT_SONAME mbufs at the head of 2377 * each record. 2378 */ 2379 if (pr->pr_flags & PR_ATOMIC && 2380 ((flags & MSG_PEEK) == 0)) 2381 (void)sbdroprecord_locked(&so->so_rcv); 2382 SOCKBUF_UNLOCK(&so->so_rcv); 2383 goto release; 2384 } 2385 } else 2386 uio->uio_resid -= len; 2387 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2388 if (len == m->m_len - moff) { 2389 if (m->m_flags & M_EOR) 2390 flags |= MSG_EOR; 2391 if (flags & MSG_PEEK) { 2392 m = m->m_next; 2393 moff = 0; 2394 } else { 2395 nextrecord = m->m_nextpkt; 2396 sbfree(&so->so_rcv, m); 2397 if (mp != NULL) { 2398 m->m_nextpkt = NULL; 2399 *mp = m; 2400 mp = &m->m_next; 2401 so->so_rcv.sb_mb = m = m->m_next; 2402 *mp = NULL; 2403 } else { 2404 so->so_rcv.sb_mb = m_free(m); 2405 m = so->so_rcv.sb_mb; 2406 } 2407 sockbuf_pushsync(&so->so_rcv, nextrecord); 2408 SBLASTRECORDCHK(&so->so_rcv); 2409 SBLASTMBUFCHK(&so->so_rcv); 2410 } 2411 } else { 2412 if (flags & MSG_PEEK) 2413 moff += len; 2414 else { 2415 if (mp != NULL) { 2416 if (flags & MSG_DONTWAIT) { 2417 *mp = m_copym(m, 0, len, 2418 M_NOWAIT); 2419 if (*mp == NULL) { 2420 /* 2421 * m_copym() couldn't 2422 * allocate an mbuf. 2423 * Adjust uio_resid back 2424 * (it was adjusted 2425 * down by len bytes, 2426 * which we didn't end 2427 * up "copying" over). 2428 */ 2429 uio->uio_resid += len; 2430 break; 2431 } 2432 } else { 2433 SOCKBUF_UNLOCK(&so->so_rcv); 2434 *mp = m_copym(m, 0, len, 2435 M_WAITOK); 2436 SOCKBUF_LOCK(&so->so_rcv); 2437 } 2438 } 2439 sbcut_locked(&so->so_rcv, len); 2440 } 2441 } 2442 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2443 if (so->so_oobmark) { 2444 if ((flags & MSG_PEEK) == 0) { 2445 so->so_oobmark -= len; 2446 if (so->so_oobmark == 0) { 2447 so->so_rcv.sb_state |= SBS_RCVATMARK; 2448 break; 2449 } 2450 } else { 2451 offset += len; 2452 if (offset == so->so_oobmark) 2453 break; 2454 } 2455 } 2456 if (flags & MSG_EOR) 2457 break; 2458 /* 2459 * If the MSG_WAITALL flag is set (for non-atomic socket), we 2460 * must not quit until "uio->uio_resid == 0" or an error 2461 * termination. If a signal/timeout occurs, return with a 2462 * short count but without error. Keep sockbuf locked 2463 * against other readers. 2464 */ 2465 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 && 2466 !sosendallatonce(so) && nextrecord == NULL) { 2467 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2468 if (so->so_error || so->so_rerror || 2469 so->so_rcv.sb_state & SBS_CANTRCVMORE) 2470 break; 2471 /* 2472 * Notify the protocol that some data has been 2473 * drained before blocking. 2474 */ 2475 if (pr->pr_flags & PR_WANTRCVD) { 2476 SOCKBUF_UNLOCK(&so->so_rcv); 2477 VNET_SO_ASSERT(so); 2478 pr->pr_rcvd(so, flags); 2479 SOCKBUF_LOCK(&so->so_rcv); 2480 if (__predict_false(so->so_rcv.sb_mb == NULL && 2481 (so->so_error || so->so_rerror || 2482 so->so_rcv.sb_state & SBS_CANTRCVMORE))) 2483 break; 2484 } 2485 SBLASTRECORDCHK(&so->so_rcv); 2486 SBLASTMBUFCHK(&so->so_rcv); 2487 /* 2488 * We could receive some data while was notifying 2489 * the protocol. Skip blocking in this case. 2490 */ 2491 if (so->so_rcv.sb_mb == NULL) { 2492 error = sbwait(so, SO_RCV); 2493 if (error) { 2494 SOCKBUF_UNLOCK(&so->so_rcv); 2495 goto release; 2496 } 2497 } 2498 m = so->so_rcv.sb_mb; 2499 if (m != NULL) 2500 nextrecord = m->m_nextpkt; 2501 } 2502 } 2503 2504 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2505 if (m != NULL && pr->pr_flags & PR_ATOMIC) { 2506 if (report_real_len) 2507 uio->uio_resid -= m_length(m, NULL) - moff; 2508 flags |= MSG_TRUNC; 2509 if ((flags & MSG_PEEK) == 0) 2510 (void) sbdroprecord_locked(&so->so_rcv); 2511 } 2512 if ((flags & MSG_PEEK) == 0) { 2513 if (m == NULL) { 2514 /* 2515 * First part is an inline SB_EMPTY_FIXUP(). Second 2516 * part makes sure sb_lastrecord is up-to-date if 2517 * there is still data in the socket buffer. 2518 */ 2519 so->so_rcv.sb_mb = nextrecord; 2520 if (so->so_rcv.sb_mb == NULL) { 2521 so->so_rcv.sb_mbtail = NULL; 2522 so->so_rcv.sb_lastrecord = NULL; 2523 } else if (nextrecord->m_nextpkt == NULL) 2524 so->so_rcv.sb_lastrecord = nextrecord; 2525 } 2526 SBLASTRECORDCHK(&so->so_rcv); 2527 SBLASTMBUFCHK(&so->so_rcv); 2528 /* 2529 * If soreceive() is being done from the socket callback, 2530 * then don't need to generate ACK to peer to update window, 2531 * since ACK will be generated on return to TCP. 2532 */ 2533 if (!(flags & MSG_SOCALLBCK) && 2534 (pr->pr_flags & PR_WANTRCVD)) { 2535 SOCKBUF_UNLOCK(&so->so_rcv); 2536 VNET_SO_ASSERT(so); 2537 pr->pr_rcvd(so, flags); 2538 SOCKBUF_LOCK(&so->so_rcv); 2539 } 2540 } 2541 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2542 if (orig_resid == uio->uio_resid && orig_resid && 2543 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) { 2544 SOCKBUF_UNLOCK(&so->so_rcv); 2545 goto restart; 2546 } 2547 SOCKBUF_UNLOCK(&so->so_rcv); 2548 2549 if (flagsp != NULL) 2550 *flagsp |= flags; 2551 release: 2552 SOCK_IO_RECV_UNLOCK(so); 2553 return (error); 2554 } 2555 2556 /* 2557 * Optimized version of soreceive() for stream (TCP) sockets. 2558 */ 2559 int 2560 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio, 2561 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 2562 { 2563 int len = 0, error = 0, flags, oresid; 2564 struct sockbuf *sb; 2565 struct mbuf *m, *n = NULL; 2566 2567 /* We only do stream sockets. */ 2568 if (so->so_type != SOCK_STREAM) 2569 return (EINVAL); 2570 if (psa != NULL) 2571 *psa = NULL; 2572 if (flagsp != NULL) 2573 flags = *flagsp &~ MSG_EOR; 2574 else 2575 flags = 0; 2576 if (controlp != NULL) 2577 *controlp = NULL; 2578 if (flags & MSG_OOB) 2579 return (soreceive_rcvoob(so, uio, flags)); 2580 if (mp0 != NULL) 2581 *mp0 = NULL; 2582 2583 sb = &so->so_rcv; 2584 2585 #ifdef KERN_TLS 2586 /* 2587 * KTLS store TLS records as records with a control message to 2588 * describe the framing. 2589 * 2590 * We check once here before acquiring locks to optimize the 2591 * common case. 2592 */ 2593 if (sb->sb_tls_info != NULL) 2594 return (soreceive_generic(so, psa, uio, mp0, controlp, 2595 flagsp)); 2596 #endif 2597 2598 /* Prevent other readers from entering the socket. */ 2599 error = SOCK_IO_RECV_LOCK(so, SBLOCKWAIT(flags)); 2600 if (error) 2601 return (error); 2602 SOCKBUF_LOCK(sb); 2603 2604 #ifdef KERN_TLS 2605 if (sb->sb_tls_info != NULL) { 2606 SOCKBUF_UNLOCK(sb); 2607 SOCK_IO_RECV_UNLOCK(so); 2608 return (soreceive_generic(so, psa, uio, mp0, controlp, 2609 flagsp)); 2610 } 2611 #endif 2612 2613 /* Easy one, no space to copyout anything. */ 2614 if (uio->uio_resid == 0) { 2615 error = EINVAL; 2616 goto out; 2617 } 2618 oresid = uio->uio_resid; 2619 2620 /* We will never ever get anything unless we are or were connected. */ 2621 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) { 2622 error = ENOTCONN; 2623 goto out; 2624 } 2625 2626 restart: 2627 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2628 2629 /* Abort if socket has reported problems. */ 2630 if (so->so_error) { 2631 if (sbavail(sb) > 0) 2632 goto deliver; 2633 if (oresid > uio->uio_resid) 2634 goto out; 2635 error = so->so_error; 2636 if (!(flags & MSG_PEEK)) 2637 so->so_error = 0; 2638 goto out; 2639 } 2640 2641 /* Door is closed. Deliver what is left, if any. */ 2642 if (sb->sb_state & SBS_CANTRCVMORE) { 2643 if (sbavail(sb) > 0) 2644 goto deliver; 2645 else 2646 goto out; 2647 } 2648 2649 /* Socket buffer is empty and we shall not block. */ 2650 if (sbavail(sb) == 0 && 2651 ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) { 2652 error = EAGAIN; 2653 goto out; 2654 } 2655 2656 /* Socket buffer got some data that we shall deliver now. */ 2657 if (sbavail(sb) > 0 && !(flags & MSG_WAITALL) && 2658 ((so->so_state & SS_NBIO) || 2659 (flags & (MSG_DONTWAIT|MSG_NBIO)) || 2660 sbavail(sb) >= sb->sb_lowat || 2661 sbavail(sb) >= uio->uio_resid || 2662 sbavail(sb) >= sb->sb_hiwat) ) { 2663 goto deliver; 2664 } 2665 2666 /* On MSG_WAITALL we must wait until all data or error arrives. */ 2667 if ((flags & MSG_WAITALL) && 2668 (sbavail(sb) >= uio->uio_resid || sbavail(sb) >= sb->sb_hiwat)) 2669 goto deliver; 2670 2671 /* 2672 * Wait and block until (more) data comes in. 2673 * NB: Drops the sockbuf lock during wait. 2674 */ 2675 error = sbwait(so, SO_RCV); 2676 if (error) 2677 goto out; 2678 goto restart; 2679 2680 deliver: 2681 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2682 KASSERT(sbavail(sb) > 0, ("%s: sockbuf empty", __func__)); 2683 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__)); 2684 2685 /* Statistics. */ 2686 if (uio->uio_td) 2687 uio->uio_td->td_ru.ru_msgrcv++; 2688 2689 /* Fill uio until full or current end of socket buffer is reached. */ 2690 len = min(uio->uio_resid, sbavail(sb)); 2691 if (mp0 != NULL) { 2692 /* Dequeue as many mbufs as possible. */ 2693 if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) { 2694 if (*mp0 == NULL) 2695 *mp0 = sb->sb_mb; 2696 else 2697 m_cat(*mp0, sb->sb_mb); 2698 for (m = sb->sb_mb; 2699 m != NULL && m->m_len <= len; 2700 m = m->m_next) { 2701 KASSERT(!(m->m_flags & M_NOTAVAIL), 2702 ("%s: m %p not available", __func__, m)); 2703 len -= m->m_len; 2704 uio->uio_resid -= m->m_len; 2705 sbfree(sb, m); 2706 n = m; 2707 } 2708 n->m_next = NULL; 2709 sb->sb_mb = m; 2710 sb->sb_lastrecord = sb->sb_mb; 2711 if (sb->sb_mb == NULL) 2712 SB_EMPTY_FIXUP(sb); 2713 } 2714 /* Copy the remainder. */ 2715 if (len > 0) { 2716 KASSERT(sb->sb_mb != NULL, 2717 ("%s: len > 0 && sb->sb_mb empty", __func__)); 2718 2719 m = m_copym(sb->sb_mb, 0, len, M_NOWAIT); 2720 if (m == NULL) 2721 len = 0; /* Don't flush data from sockbuf. */ 2722 else 2723 uio->uio_resid -= len; 2724 if (*mp0 != NULL) 2725 m_cat(*mp0, m); 2726 else 2727 *mp0 = m; 2728 if (*mp0 == NULL) { 2729 error = ENOBUFS; 2730 goto out; 2731 } 2732 } 2733 } else { 2734 /* NB: Must unlock socket buffer as uiomove may sleep. */ 2735 SOCKBUF_UNLOCK(sb); 2736 error = m_mbuftouio(uio, sb->sb_mb, len); 2737 SOCKBUF_LOCK(sb); 2738 if (error) 2739 goto out; 2740 } 2741 SBLASTRECORDCHK(sb); 2742 SBLASTMBUFCHK(sb); 2743 2744 /* 2745 * Remove the delivered data from the socket buffer unless we 2746 * were only peeking. 2747 */ 2748 if (!(flags & MSG_PEEK)) { 2749 if (len > 0) 2750 sbdrop_locked(sb, len); 2751 2752 /* Notify protocol that we drained some data. */ 2753 if ((so->so_proto->pr_flags & PR_WANTRCVD) && 2754 (((flags & MSG_WAITALL) && uio->uio_resid > 0) || 2755 !(flags & MSG_SOCALLBCK))) { 2756 SOCKBUF_UNLOCK(sb); 2757 VNET_SO_ASSERT(so); 2758 so->so_proto->pr_rcvd(so, flags); 2759 SOCKBUF_LOCK(sb); 2760 } 2761 } 2762 2763 /* 2764 * For MSG_WAITALL we may have to loop again and wait for 2765 * more data to come in. 2766 */ 2767 if ((flags & MSG_WAITALL) && uio->uio_resid > 0) 2768 goto restart; 2769 out: 2770 SBLASTRECORDCHK(sb); 2771 SBLASTMBUFCHK(sb); 2772 SOCKBUF_UNLOCK(sb); 2773 SOCK_IO_RECV_UNLOCK(so); 2774 return (error); 2775 } 2776 2777 /* 2778 * Optimized version of soreceive() for simple datagram cases from userspace. 2779 * Unlike in the stream case, we're able to drop a datagram if copyout() 2780 * fails, and because we handle datagrams atomically, we don't need to use a 2781 * sleep lock to prevent I/O interlacing. 2782 */ 2783 int 2784 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio, 2785 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 2786 { 2787 struct mbuf *m, *m2; 2788 int flags, error; 2789 ssize_t len; 2790 struct protosw *pr = so->so_proto; 2791 struct mbuf *nextrecord; 2792 2793 if (psa != NULL) 2794 *psa = NULL; 2795 if (controlp != NULL) 2796 *controlp = NULL; 2797 if (flagsp != NULL) 2798 flags = *flagsp &~ MSG_EOR; 2799 else 2800 flags = 0; 2801 2802 /* 2803 * For any complicated cases, fall back to the full 2804 * soreceive_generic(). 2805 */ 2806 if (mp0 != NULL || (flags & (MSG_PEEK | MSG_OOB | MSG_TRUNC))) 2807 return (soreceive_generic(so, psa, uio, mp0, controlp, 2808 flagsp)); 2809 2810 /* 2811 * Enforce restrictions on use. 2812 */ 2813 KASSERT((pr->pr_flags & PR_WANTRCVD) == 0, 2814 ("soreceive_dgram: wantrcvd")); 2815 KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic")); 2816 KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0, 2817 ("soreceive_dgram: SBS_RCVATMARK")); 2818 KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0, 2819 ("soreceive_dgram: P_CONNREQUIRED")); 2820 2821 /* 2822 * Loop blocking while waiting for a datagram. 2823 */ 2824 SOCKBUF_LOCK(&so->so_rcv); 2825 while ((m = so->so_rcv.sb_mb) == NULL) { 2826 KASSERT(sbavail(&so->so_rcv) == 0, 2827 ("soreceive_dgram: sb_mb NULL but sbavail %u", 2828 sbavail(&so->so_rcv))); 2829 if (so->so_error) { 2830 error = so->so_error; 2831 so->so_error = 0; 2832 SOCKBUF_UNLOCK(&so->so_rcv); 2833 return (error); 2834 } 2835 if (so->so_rcv.sb_state & SBS_CANTRCVMORE || 2836 uio->uio_resid == 0) { 2837 SOCKBUF_UNLOCK(&so->so_rcv); 2838 return (0); 2839 } 2840 if ((so->so_state & SS_NBIO) || 2841 (flags & (MSG_DONTWAIT|MSG_NBIO))) { 2842 SOCKBUF_UNLOCK(&so->so_rcv); 2843 return (EWOULDBLOCK); 2844 } 2845 SBLASTRECORDCHK(&so->so_rcv); 2846 SBLASTMBUFCHK(&so->so_rcv); 2847 error = sbwait(so, SO_RCV); 2848 if (error) { 2849 SOCKBUF_UNLOCK(&so->so_rcv); 2850 return (error); 2851 } 2852 } 2853 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2854 2855 if (uio->uio_td) 2856 uio->uio_td->td_ru.ru_msgrcv++; 2857 SBLASTRECORDCHK(&so->so_rcv); 2858 SBLASTMBUFCHK(&so->so_rcv); 2859 nextrecord = m->m_nextpkt; 2860 if (nextrecord == NULL) { 2861 KASSERT(so->so_rcv.sb_lastrecord == m, 2862 ("soreceive_dgram: lastrecord != m")); 2863 } 2864 2865 KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord, 2866 ("soreceive_dgram: m_nextpkt != nextrecord")); 2867 2868 /* 2869 * Pull 'm' and its chain off the front of the packet queue. 2870 */ 2871 so->so_rcv.sb_mb = NULL; 2872 sockbuf_pushsync(&so->so_rcv, nextrecord); 2873 2874 /* 2875 * Walk 'm's chain and free that many bytes from the socket buffer. 2876 */ 2877 for (m2 = m; m2 != NULL; m2 = m2->m_next) 2878 sbfree(&so->so_rcv, m2); 2879 2880 /* 2881 * Do a few last checks before we let go of the lock. 2882 */ 2883 SBLASTRECORDCHK(&so->so_rcv); 2884 SBLASTMBUFCHK(&so->so_rcv); 2885 SOCKBUF_UNLOCK(&so->so_rcv); 2886 2887 if (pr->pr_flags & PR_ADDR) { 2888 KASSERT(m->m_type == MT_SONAME, 2889 ("m->m_type == %d", m->m_type)); 2890 if (psa != NULL) 2891 *psa = sodupsockaddr(mtod(m, struct sockaddr *), 2892 M_WAITOK); 2893 m = m_free(m); 2894 } 2895 KASSERT(m, ("%s: no data or control after soname", __func__)); 2896 2897 /* 2898 * Packet to copyout() is now in 'm' and it is disconnected from the 2899 * queue. 2900 * 2901 * Process one or more MT_CONTROL mbufs present before any data mbufs 2902 * in the first mbuf chain on the socket buffer. We call into the 2903 * protocol to perform externalization (or freeing if controlp == 2904 * NULL). In some cases there can be only MT_CONTROL mbufs without 2905 * MT_DATA mbufs. 2906 */ 2907 if (m->m_type == MT_CONTROL) { 2908 struct mbuf *cm = NULL, *cmn; 2909 struct mbuf **cme = &cm; 2910 2911 do { 2912 m2 = m->m_next; 2913 m->m_next = NULL; 2914 *cme = m; 2915 cme = &(*cme)->m_next; 2916 m = m2; 2917 } while (m != NULL && m->m_type == MT_CONTROL); 2918 while (cm != NULL) { 2919 cmn = cm->m_next; 2920 cm->m_next = NULL; 2921 if (pr->pr_domain->dom_externalize != NULL) { 2922 error = (*pr->pr_domain->dom_externalize) 2923 (cm, controlp, flags); 2924 } else if (controlp != NULL) 2925 *controlp = cm; 2926 else 2927 m_freem(cm); 2928 if (controlp != NULL) { 2929 while (*controlp != NULL) 2930 controlp = &(*controlp)->m_next; 2931 } 2932 cm = cmn; 2933 } 2934 } 2935 KASSERT(m == NULL || m->m_type == MT_DATA, 2936 ("soreceive_dgram: !data")); 2937 while (m != NULL && uio->uio_resid > 0) { 2938 len = uio->uio_resid; 2939 if (len > m->m_len) 2940 len = m->m_len; 2941 error = uiomove(mtod(m, char *), (int)len, uio); 2942 if (error) { 2943 m_freem(m); 2944 return (error); 2945 } 2946 if (len == m->m_len) 2947 m = m_free(m); 2948 else { 2949 m->m_data += len; 2950 m->m_len -= len; 2951 } 2952 } 2953 if (m != NULL) { 2954 flags |= MSG_TRUNC; 2955 m_freem(m); 2956 } 2957 if (flagsp != NULL) 2958 *flagsp |= flags; 2959 return (0); 2960 } 2961 2962 int 2963 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio, 2964 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 2965 { 2966 int error; 2967 2968 CURVNET_SET(so->so_vnet); 2969 error = so->so_proto->pr_soreceive(so, psa, uio, mp0, controlp, flagsp); 2970 CURVNET_RESTORE(); 2971 return (error); 2972 } 2973 2974 int 2975 soshutdown(struct socket *so, enum shutdown_how how) 2976 { 2977 int error; 2978 2979 CURVNET_SET(so->so_vnet); 2980 error = so->so_proto->pr_shutdown(so, how); 2981 CURVNET_RESTORE(); 2982 2983 return (error); 2984 } 2985 2986 /* 2987 * Used by several pr_shutdown implementations that use generic socket buffers. 2988 */ 2989 void 2990 sorflush(struct socket *so) 2991 { 2992 int error; 2993 2994 VNET_SO_ASSERT(so); 2995 2996 /* 2997 * Dislodge threads currently blocked in receive and wait to acquire 2998 * a lock against other simultaneous readers before clearing the 2999 * socket buffer. Don't let our acquire be interrupted by a signal 3000 * despite any existing socket disposition on interruptable waiting. 3001 * 3002 * The SOCK_IO_RECV_LOCK() is important here as there some pr_soreceive 3003 * methods that read the top of the socket buffer without acquisition 3004 * of the socket buffer mutex, assuming that top of the buffer 3005 * exclusively belongs to the read(2) syscall. This is handy when 3006 * performing MSG_PEEK. 3007 */ 3008 socantrcvmore(so); 3009 3010 error = SOCK_IO_RECV_LOCK(so, SBL_WAIT | SBL_NOINTR); 3011 if (error != 0) { 3012 KASSERT(SOLISTENING(so), 3013 ("%s: soiolock(%p) failed", __func__, so)); 3014 return; 3015 } 3016 3017 sbrelease(so, SO_RCV); 3018 SOCK_IO_RECV_UNLOCK(so); 3019 3020 } 3021 3022 /* 3023 * Wrapper for Socket established helper hook. 3024 * Parameters: socket, context of the hook point, hook id. 3025 */ 3026 static int inline 3027 hhook_run_socket(struct socket *so, void *hctx, int32_t h_id) 3028 { 3029 struct socket_hhook_data hhook_data = { 3030 .so = so, 3031 .hctx = hctx, 3032 .m = NULL, 3033 .status = 0 3034 }; 3035 3036 CURVNET_SET(so->so_vnet); 3037 HHOOKS_RUN_IF(V_socket_hhh[h_id], &hhook_data, &so->osd); 3038 CURVNET_RESTORE(); 3039 3040 /* Ugly but needed, since hhooks return void for now */ 3041 return (hhook_data.status); 3042 } 3043 3044 /* 3045 * Perhaps this routine, and sooptcopyout(), below, ought to come in an 3046 * additional variant to handle the case where the option value needs to be 3047 * some kind of integer, but not a specific size. In addition to their use 3048 * here, these functions are also called by the protocol-level pr_ctloutput() 3049 * routines. 3050 */ 3051 int 3052 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen) 3053 { 3054 size_t valsize; 3055 3056 /* 3057 * If the user gives us more than we wanted, we ignore it, but if we 3058 * don't get the minimum length the caller wants, we return EINVAL. 3059 * On success, sopt->sopt_valsize is set to however much we actually 3060 * retrieved. 3061 */ 3062 if ((valsize = sopt->sopt_valsize) < minlen) 3063 return EINVAL; 3064 if (valsize > len) 3065 sopt->sopt_valsize = valsize = len; 3066 3067 if (sopt->sopt_td != NULL) 3068 return (copyin(sopt->sopt_val, buf, valsize)); 3069 3070 bcopy(sopt->sopt_val, buf, valsize); 3071 return (0); 3072 } 3073 3074 /* 3075 * Kernel version of setsockopt(2). 3076 * 3077 * XXX: optlen is size_t, not socklen_t 3078 */ 3079 int 3080 so_setsockopt(struct socket *so, int level, int optname, void *optval, 3081 size_t optlen) 3082 { 3083 struct sockopt sopt; 3084 3085 sopt.sopt_level = level; 3086 sopt.sopt_name = optname; 3087 sopt.sopt_dir = SOPT_SET; 3088 sopt.sopt_val = optval; 3089 sopt.sopt_valsize = optlen; 3090 sopt.sopt_td = NULL; 3091 return (sosetopt(so, &sopt)); 3092 } 3093 3094 int 3095 sosetopt(struct socket *so, struct sockopt *sopt) 3096 { 3097 int error, optval; 3098 struct linger l; 3099 struct timeval tv; 3100 sbintime_t val, *valp; 3101 uint32_t val32; 3102 #ifdef MAC 3103 struct mac extmac; 3104 #endif 3105 3106 CURVNET_SET(so->so_vnet); 3107 error = 0; 3108 if (sopt->sopt_level != SOL_SOCKET) { 3109 if (so->so_proto->pr_ctloutput != NULL) 3110 error = (*so->so_proto->pr_ctloutput)(so, sopt); 3111 else 3112 error = ENOPROTOOPT; 3113 } else { 3114 switch (sopt->sopt_name) { 3115 case SO_ACCEPTFILTER: 3116 error = accept_filt_setopt(so, sopt); 3117 if (error) 3118 goto bad; 3119 break; 3120 3121 case SO_LINGER: 3122 error = sooptcopyin(sopt, &l, sizeof l, sizeof l); 3123 if (error) 3124 goto bad; 3125 if (l.l_linger < 0 || 3126 l.l_linger > USHRT_MAX || 3127 l.l_linger > (INT_MAX / hz)) { 3128 error = EDOM; 3129 goto bad; 3130 } 3131 SOCK_LOCK(so); 3132 so->so_linger = l.l_linger; 3133 if (l.l_onoff) 3134 so->so_options |= SO_LINGER; 3135 else 3136 so->so_options &= ~SO_LINGER; 3137 SOCK_UNLOCK(so); 3138 break; 3139 3140 case SO_DEBUG: 3141 case SO_KEEPALIVE: 3142 case SO_DONTROUTE: 3143 case SO_USELOOPBACK: 3144 case SO_BROADCAST: 3145 case SO_REUSEADDR: 3146 case SO_REUSEPORT: 3147 case SO_REUSEPORT_LB: 3148 case SO_OOBINLINE: 3149 case SO_TIMESTAMP: 3150 case SO_BINTIME: 3151 case SO_NOSIGPIPE: 3152 case SO_NO_DDP: 3153 case SO_NO_OFFLOAD: 3154 case SO_RERROR: 3155 error = sooptcopyin(sopt, &optval, sizeof optval, 3156 sizeof optval); 3157 if (error) 3158 goto bad; 3159 SOCK_LOCK(so); 3160 if (optval) 3161 so->so_options |= sopt->sopt_name; 3162 else 3163 so->so_options &= ~sopt->sopt_name; 3164 SOCK_UNLOCK(so); 3165 break; 3166 3167 case SO_SETFIB: 3168 error = sooptcopyin(sopt, &optval, sizeof optval, 3169 sizeof optval); 3170 if (error) 3171 goto bad; 3172 3173 if (optval < 0 || optval >= rt_numfibs) { 3174 error = EINVAL; 3175 goto bad; 3176 } 3177 if (((so->so_proto->pr_domain->dom_family == PF_INET) || 3178 (so->so_proto->pr_domain->dom_family == PF_INET6) || 3179 (so->so_proto->pr_domain->dom_family == PF_ROUTE))) 3180 so->so_fibnum = optval; 3181 else 3182 so->so_fibnum = 0; 3183 break; 3184 3185 case SO_USER_COOKIE: 3186 error = sooptcopyin(sopt, &val32, sizeof val32, 3187 sizeof val32); 3188 if (error) 3189 goto bad; 3190 so->so_user_cookie = val32; 3191 break; 3192 3193 case SO_SNDBUF: 3194 case SO_RCVBUF: 3195 case SO_SNDLOWAT: 3196 case SO_RCVLOWAT: 3197 error = so->so_proto->pr_setsbopt(so, sopt); 3198 if (error) 3199 goto bad; 3200 break; 3201 3202 case SO_SNDTIMEO: 3203 case SO_RCVTIMEO: 3204 #ifdef COMPAT_FREEBSD32 3205 if (SV_CURPROC_FLAG(SV_ILP32)) { 3206 struct timeval32 tv32; 3207 3208 error = sooptcopyin(sopt, &tv32, sizeof tv32, 3209 sizeof tv32); 3210 CP(tv32, tv, tv_sec); 3211 CP(tv32, tv, tv_usec); 3212 } else 3213 #endif 3214 error = sooptcopyin(sopt, &tv, sizeof tv, 3215 sizeof tv); 3216 if (error) 3217 goto bad; 3218 if (tv.tv_sec < 0 || tv.tv_usec < 0 || 3219 tv.tv_usec >= 1000000) { 3220 error = EDOM; 3221 goto bad; 3222 } 3223 if (tv.tv_sec > INT32_MAX) 3224 val = SBT_MAX; 3225 else 3226 val = tvtosbt(tv); 3227 SOCK_LOCK(so); 3228 valp = sopt->sopt_name == SO_SNDTIMEO ? 3229 (SOLISTENING(so) ? &so->sol_sbsnd_timeo : 3230 &so->so_snd.sb_timeo) : 3231 (SOLISTENING(so) ? &so->sol_sbrcv_timeo : 3232 &so->so_rcv.sb_timeo); 3233 *valp = val; 3234 SOCK_UNLOCK(so); 3235 break; 3236 3237 case SO_LABEL: 3238 #ifdef MAC 3239 error = sooptcopyin(sopt, &extmac, sizeof extmac, 3240 sizeof extmac); 3241 if (error) 3242 goto bad; 3243 error = mac_setsockopt_label(sopt->sopt_td->td_ucred, 3244 so, &extmac); 3245 #else 3246 error = EOPNOTSUPP; 3247 #endif 3248 break; 3249 3250 case SO_TS_CLOCK: 3251 error = sooptcopyin(sopt, &optval, sizeof optval, 3252 sizeof optval); 3253 if (error) 3254 goto bad; 3255 if (optval < 0 || optval > SO_TS_CLOCK_MAX) { 3256 error = EINVAL; 3257 goto bad; 3258 } 3259 so->so_ts_clock = optval; 3260 break; 3261 3262 case SO_MAX_PACING_RATE: 3263 error = sooptcopyin(sopt, &val32, sizeof(val32), 3264 sizeof(val32)); 3265 if (error) 3266 goto bad; 3267 so->so_max_pacing_rate = val32; 3268 break; 3269 3270 default: 3271 if (V_socket_hhh[HHOOK_SOCKET_OPT]->hhh_nhooks > 0) 3272 error = hhook_run_socket(so, sopt, 3273 HHOOK_SOCKET_OPT); 3274 else 3275 error = ENOPROTOOPT; 3276 break; 3277 } 3278 if (error == 0 && so->so_proto->pr_ctloutput != NULL) 3279 (void)(*so->so_proto->pr_ctloutput)(so, sopt); 3280 } 3281 bad: 3282 CURVNET_RESTORE(); 3283 return (error); 3284 } 3285 3286 /* 3287 * Helper routine for getsockopt. 3288 */ 3289 int 3290 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len) 3291 { 3292 int error; 3293 size_t valsize; 3294 3295 error = 0; 3296 3297 /* 3298 * Documented get behavior is that we always return a value, possibly 3299 * truncated to fit in the user's buffer. Traditional behavior is 3300 * that we always tell the user precisely how much we copied, rather 3301 * than something useful like the total amount we had available for 3302 * her. Note that this interface is not idempotent; the entire 3303 * answer must be generated ahead of time. 3304 */ 3305 valsize = min(len, sopt->sopt_valsize); 3306 sopt->sopt_valsize = valsize; 3307 if (sopt->sopt_val != NULL) { 3308 if (sopt->sopt_td != NULL) 3309 error = copyout(buf, sopt->sopt_val, valsize); 3310 else 3311 bcopy(buf, sopt->sopt_val, valsize); 3312 } 3313 return (error); 3314 } 3315 3316 int 3317 sogetopt(struct socket *so, struct sockopt *sopt) 3318 { 3319 int error, optval; 3320 struct linger l; 3321 struct timeval tv; 3322 #ifdef MAC 3323 struct mac extmac; 3324 #endif 3325 3326 CURVNET_SET(so->so_vnet); 3327 error = 0; 3328 if (sopt->sopt_level != SOL_SOCKET) { 3329 if (so->so_proto->pr_ctloutput != NULL) 3330 error = (*so->so_proto->pr_ctloutput)(so, sopt); 3331 else 3332 error = ENOPROTOOPT; 3333 CURVNET_RESTORE(); 3334 return (error); 3335 } else { 3336 switch (sopt->sopt_name) { 3337 case SO_ACCEPTFILTER: 3338 error = accept_filt_getopt(so, sopt); 3339 break; 3340 3341 case SO_LINGER: 3342 SOCK_LOCK(so); 3343 l.l_onoff = so->so_options & SO_LINGER; 3344 l.l_linger = so->so_linger; 3345 SOCK_UNLOCK(so); 3346 error = sooptcopyout(sopt, &l, sizeof l); 3347 break; 3348 3349 case SO_USELOOPBACK: 3350 case SO_DONTROUTE: 3351 case SO_DEBUG: 3352 case SO_KEEPALIVE: 3353 case SO_REUSEADDR: 3354 case SO_REUSEPORT: 3355 case SO_REUSEPORT_LB: 3356 case SO_BROADCAST: 3357 case SO_OOBINLINE: 3358 case SO_ACCEPTCONN: 3359 case SO_TIMESTAMP: 3360 case SO_BINTIME: 3361 case SO_NOSIGPIPE: 3362 case SO_NO_DDP: 3363 case SO_NO_OFFLOAD: 3364 case SO_RERROR: 3365 optval = so->so_options & sopt->sopt_name; 3366 integer: 3367 error = sooptcopyout(sopt, &optval, sizeof optval); 3368 break; 3369 3370 case SO_DOMAIN: 3371 optval = so->so_proto->pr_domain->dom_family; 3372 goto integer; 3373 3374 case SO_TYPE: 3375 optval = so->so_type; 3376 goto integer; 3377 3378 case SO_PROTOCOL: 3379 optval = so->so_proto->pr_protocol; 3380 goto integer; 3381 3382 case SO_ERROR: 3383 SOCK_LOCK(so); 3384 if (so->so_error) { 3385 optval = so->so_error; 3386 so->so_error = 0; 3387 } else { 3388 optval = so->so_rerror; 3389 so->so_rerror = 0; 3390 } 3391 SOCK_UNLOCK(so); 3392 goto integer; 3393 3394 case SO_SNDBUF: 3395 optval = SOLISTENING(so) ? so->sol_sbsnd_hiwat : 3396 so->so_snd.sb_hiwat; 3397 goto integer; 3398 3399 case SO_RCVBUF: 3400 optval = SOLISTENING(so) ? so->sol_sbrcv_hiwat : 3401 so->so_rcv.sb_hiwat; 3402 goto integer; 3403 3404 case SO_SNDLOWAT: 3405 optval = SOLISTENING(so) ? so->sol_sbsnd_lowat : 3406 so->so_snd.sb_lowat; 3407 goto integer; 3408 3409 case SO_RCVLOWAT: 3410 optval = SOLISTENING(so) ? so->sol_sbrcv_lowat : 3411 so->so_rcv.sb_lowat; 3412 goto integer; 3413 3414 case SO_SNDTIMEO: 3415 case SO_RCVTIMEO: 3416 SOCK_LOCK(so); 3417 tv = sbttotv(sopt->sopt_name == SO_SNDTIMEO ? 3418 (SOLISTENING(so) ? so->sol_sbsnd_timeo : 3419 so->so_snd.sb_timeo) : 3420 (SOLISTENING(so) ? so->sol_sbrcv_timeo : 3421 so->so_rcv.sb_timeo)); 3422 SOCK_UNLOCK(so); 3423 #ifdef COMPAT_FREEBSD32 3424 if (SV_CURPROC_FLAG(SV_ILP32)) { 3425 struct timeval32 tv32; 3426 3427 CP(tv, tv32, tv_sec); 3428 CP(tv, tv32, tv_usec); 3429 error = sooptcopyout(sopt, &tv32, sizeof tv32); 3430 } else 3431 #endif 3432 error = sooptcopyout(sopt, &tv, sizeof tv); 3433 break; 3434 3435 case SO_LABEL: 3436 #ifdef MAC 3437 error = sooptcopyin(sopt, &extmac, sizeof(extmac), 3438 sizeof(extmac)); 3439 if (error) 3440 goto bad; 3441 error = mac_getsockopt_label(sopt->sopt_td->td_ucred, 3442 so, &extmac); 3443 if (error) 3444 goto bad; 3445 /* Don't copy out extmac, it is unchanged. */ 3446 #else 3447 error = EOPNOTSUPP; 3448 #endif 3449 break; 3450 3451 case SO_PEERLABEL: 3452 #ifdef MAC 3453 error = sooptcopyin(sopt, &extmac, sizeof(extmac), 3454 sizeof(extmac)); 3455 if (error) 3456 goto bad; 3457 error = mac_getsockopt_peerlabel( 3458 sopt->sopt_td->td_ucred, so, &extmac); 3459 if (error) 3460 goto bad; 3461 /* Don't copy out extmac, it is unchanged. */ 3462 #else 3463 error = EOPNOTSUPP; 3464 #endif 3465 break; 3466 3467 case SO_LISTENQLIMIT: 3468 optval = SOLISTENING(so) ? so->sol_qlimit : 0; 3469 goto integer; 3470 3471 case SO_LISTENQLEN: 3472 optval = SOLISTENING(so) ? so->sol_qlen : 0; 3473 goto integer; 3474 3475 case SO_LISTENINCQLEN: 3476 optval = SOLISTENING(so) ? so->sol_incqlen : 0; 3477 goto integer; 3478 3479 case SO_TS_CLOCK: 3480 optval = so->so_ts_clock; 3481 goto integer; 3482 3483 case SO_MAX_PACING_RATE: 3484 optval = so->so_max_pacing_rate; 3485 goto integer; 3486 3487 default: 3488 if (V_socket_hhh[HHOOK_SOCKET_OPT]->hhh_nhooks > 0) 3489 error = hhook_run_socket(so, sopt, 3490 HHOOK_SOCKET_OPT); 3491 else 3492 error = ENOPROTOOPT; 3493 break; 3494 } 3495 } 3496 #ifdef MAC 3497 bad: 3498 #endif 3499 CURVNET_RESTORE(); 3500 return (error); 3501 } 3502 3503 int 3504 soopt_getm(struct sockopt *sopt, struct mbuf **mp) 3505 { 3506 struct mbuf *m, *m_prev; 3507 int sopt_size = sopt->sopt_valsize; 3508 3509 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA); 3510 if (m == NULL) 3511 return ENOBUFS; 3512 if (sopt_size > MLEN) { 3513 MCLGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT); 3514 if ((m->m_flags & M_EXT) == 0) { 3515 m_free(m); 3516 return ENOBUFS; 3517 } 3518 m->m_len = min(MCLBYTES, sopt_size); 3519 } else { 3520 m->m_len = min(MLEN, sopt_size); 3521 } 3522 sopt_size -= m->m_len; 3523 *mp = m; 3524 m_prev = m; 3525 3526 while (sopt_size) { 3527 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA); 3528 if (m == NULL) { 3529 m_freem(*mp); 3530 return ENOBUFS; 3531 } 3532 if (sopt_size > MLEN) { 3533 MCLGET(m, sopt->sopt_td != NULL ? M_WAITOK : 3534 M_NOWAIT); 3535 if ((m->m_flags & M_EXT) == 0) { 3536 m_freem(m); 3537 m_freem(*mp); 3538 return ENOBUFS; 3539 } 3540 m->m_len = min(MCLBYTES, sopt_size); 3541 } else { 3542 m->m_len = min(MLEN, sopt_size); 3543 } 3544 sopt_size -= m->m_len; 3545 m_prev->m_next = m; 3546 m_prev = m; 3547 } 3548 return (0); 3549 } 3550 3551 int 3552 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m) 3553 { 3554 struct mbuf *m0 = m; 3555 3556 if (sopt->sopt_val == NULL) 3557 return (0); 3558 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 3559 if (sopt->sopt_td != NULL) { 3560 int error; 3561 3562 error = copyin(sopt->sopt_val, mtod(m, char *), 3563 m->m_len); 3564 if (error != 0) { 3565 m_freem(m0); 3566 return(error); 3567 } 3568 } else 3569 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len); 3570 sopt->sopt_valsize -= m->m_len; 3571 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len; 3572 m = m->m_next; 3573 } 3574 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */ 3575 panic("ip6_sooptmcopyin"); 3576 return (0); 3577 } 3578 3579 int 3580 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m) 3581 { 3582 struct mbuf *m0 = m; 3583 size_t valsize = 0; 3584 3585 if (sopt->sopt_val == NULL) 3586 return (0); 3587 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 3588 if (sopt->sopt_td != NULL) { 3589 int error; 3590 3591 error = copyout(mtod(m, char *), sopt->sopt_val, 3592 m->m_len); 3593 if (error != 0) { 3594 m_freem(m0); 3595 return(error); 3596 } 3597 } else 3598 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len); 3599 sopt->sopt_valsize -= m->m_len; 3600 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len; 3601 valsize += m->m_len; 3602 m = m->m_next; 3603 } 3604 if (m != NULL) { 3605 /* enough soopt buffer should be given from user-land */ 3606 m_freem(m0); 3607 return(EINVAL); 3608 } 3609 sopt->sopt_valsize = valsize; 3610 return (0); 3611 } 3612 3613 /* 3614 * sohasoutofband(): protocol notifies socket layer of the arrival of new 3615 * out-of-band data, which will then notify socket consumers. 3616 */ 3617 void 3618 sohasoutofband(struct socket *so) 3619 { 3620 3621 if (so->so_sigio != NULL) 3622 pgsigio(&so->so_sigio, SIGURG, 0); 3623 selwakeuppri(&so->so_rdsel, PSOCK); 3624 } 3625 3626 int 3627 sopoll(struct socket *so, int events, struct ucred *active_cred, 3628 struct thread *td) 3629 { 3630 3631 /* 3632 * We do not need to set or assert curvnet as long as everyone uses 3633 * sopoll_generic(). 3634 */ 3635 return (so->so_proto->pr_sopoll(so, events, active_cred, td)); 3636 } 3637 3638 int 3639 sopoll_generic(struct socket *so, int events, struct ucred *active_cred, 3640 struct thread *td) 3641 { 3642 int revents; 3643 3644 SOCK_LOCK(so); 3645 if (SOLISTENING(so)) { 3646 if (!(events & (POLLIN | POLLRDNORM))) 3647 revents = 0; 3648 else if (!TAILQ_EMPTY(&so->sol_comp)) 3649 revents = events & (POLLIN | POLLRDNORM); 3650 else if ((events & POLLINIGNEOF) == 0 && so->so_error) 3651 revents = (events & (POLLIN | POLLRDNORM)) | POLLHUP; 3652 else { 3653 selrecord(td, &so->so_rdsel); 3654 revents = 0; 3655 } 3656 } else { 3657 revents = 0; 3658 SOCK_SENDBUF_LOCK(so); 3659 SOCK_RECVBUF_LOCK(so); 3660 if (events & (POLLIN | POLLRDNORM)) 3661 if (soreadabledata(so)) 3662 revents |= events & (POLLIN | POLLRDNORM); 3663 if (events & (POLLOUT | POLLWRNORM)) 3664 if (sowriteable(so)) 3665 revents |= events & (POLLOUT | POLLWRNORM); 3666 if (events & (POLLPRI | POLLRDBAND)) 3667 if (so->so_oobmark || 3668 (so->so_rcv.sb_state & SBS_RCVATMARK)) 3669 revents |= events & (POLLPRI | POLLRDBAND); 3670 if ((events & POLLINIGNEOF) == 0) { 3671 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 3672 revents |= events & (POLLIN | POLLRDNORM); 3673 if (so->so_snd.sb_state & SBS_CANTSENDMORE) 3674 revents |= POLLHUP; 3675 } 3676 } 3677 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 3678 revents |= events & POLLRDHUP; 3679 if (revents == 0) { 3680 if (events & 3681 (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND | POLLRDHUP)) { 3682 selrecord(td, &so->so_rdsel); 3683 so->so_rcv.sb_flags |= SB_SEL; 3684 } 3685 if (events & (POLLOUT | POLLWRNORM)) { 3686 selrecord(td, &so->so_wrsel); 3687 so->so_snd.sb_flags |= SB_SEL; 3688 } 3689 } 3690 SOCK_RECVBUF_UNLOCK(so); 3691 SOCK_SENDBUF_UNLOCK(so); 3692 } 3693 SOCK_UNLOCK(so); 3694 return (revents); 3695 } 3696 3697 int 3698 soo_kqfilter(struct file *fp, struct knote *kn) 3699 { 3700 struct socket *so = kn->kn_fp->f_data; 3701 struct sockbuf *sb; 3702 sb_which which; 3703 struct knlist *knl; 3704 3705 switch (kn->kn_filter) { 3706 case EVFILT_READ: 3707 kn->kn_fop = &soread_filtops; 3708 knl = &so->so_rdsel.si_note; 3709 sb = &so->so_rcv; 3710 which = SO_RCV; 3711 break; 3712 case EVFILT_WRITE: 3713 kn->kn_fop = &sowrite_filtops; 3714 knl = &so->so_wrsel.si_note; 3715 sb = &so->so_snd; 3716 which = SO_SND; 3717 break; 3718 case EVFILT_EMPTY: 3719 kn->kn_fop = &soempty_filtops; 3720 knl = &so->so_wrsel.si_note; 3721 sb = &so->so_snd; 3722 which = SO_SND; 3723 break; 3724 default: 3725 return (EINVAL); 3726 } 3727 3728 SOCK_LOCK(so); 3729 if (SOLISTENING(so)) { 3730 knlist_add(knl, kn, 1); 3731 } else { 3732 SOCK_BUF_LOCK(so, which); 3733 knlist_add(knl, kn, 1); 3734 sb->sb_flags |= SB_KNOTE; 3735 SOCK_BUF_UNLOCK(so, which); 3736 } 3737 SOCK_UNLOCK(so); 3738 return (0); 3739 } 3740 3741 static void 3742 filt_sordetach(struct knote *kn) 3743 { 3744 struct socket *so = kn->kn_fp->f_data; 3745 3746 so_rdknl_lock(so); 3747 knlist_remove(&so->so_rdsel.si_note, kn, 1); 3748 if (!SOLISTENING(so) && knlist_empty(&so->so_rdsel.si_note)) 3749 so->so_rcv.sb_flags &= ~SB_KNOTE; 3750 so_rdknl_unlock(so); 3751 } 3752 3753 /*ARGSUSED*/ 3754 static int 3755 filt_soread(struct knote *kn, long hint) 3756 { 3757 struct socket *so; 3758 3759 so = kn->kn_fp->f_data; 3760 3761 if (SOLISTENING(so)) { 3762 SOCK_LOCK_ASSERT(so); 3763 kn->kn_data = so->sol_qlen; 3764 if (so->so_error) { 3765 kn->kn_flags |= EV_EOF; 3766 kn->kn_fflags = so->so_error; 3767 return (1); 3768 } 3769 return (!TAILQ_EMPTY(&so->sol_comp)); 3770 } 3771 3772 SOCK_RECVBUF_LOCK_ASSERT(so); 3773 3774 kn->kn_data = sbavail(&so->so_rcv) - so->so_rcv.sb_ctl; 3775 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 3776 kn->kn_flags |= EV_EOF; 3777 kn->kn_fflags = so->so_error; 3778 return (1); 3779 } else if (so->so_error || so->so_rerror) 3780 return (1); 3781 3782 if (kn->kn_sfflags & NOTE_LOWAT) { 3783 if (kn->kn_data >= kn->kn_sdata) 3784 return (1); 3785 } else if (sbavail(&so->so_rcv) >= so->so_rcv.sb_lowat) 3786 return (1); 3787 3788 /* This hook returning non-zero indicates an event, not error */ 3789 return (hhook_run_socket(so, NULL, HHOOK_FILT_SOREAD)); 3790 } 3791 3792 static void 3793 filt_sowdetach(struct knote *kn) 3794 { 3795 struct socket *so = kn->kn_fp->f_data; 3796 3797 so_wrknl_lock(so); 3798 knlist_remove(&so->so_wrsel.si_note, kn, 1); 3799 if (!SOLISTENING(so) && knlist_empty(&so->so_wrsel.si_note)) 3800 so->so_snd.sb_flags &= ~SB_KNOTE; 3801 so_wrknl_unlock(so); 3802 } 3803 3804 /*ARGSUSED*/ 3805 static int 3806 filt_sowrite(struct knote *kn, long hint) 3807 { 3808 struct socket *so; 3809 3810 so = kn->kn_fp->f_data; 3811 3812 if (SOLISTENING(so)) 3813 return (0); 3814 3815 SOCK_SENDBUF_LOCK_ASSERT(so); 3816 kn->kn_data = sbspace(&so->so_snd); 3817 3818 hhook_run_socket(so, kn, HHOOK_FILT_SOWRITE); 3819 3820 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 3821 kn->kn_flags |= EV_EOF; 3822 kn->kn_fflags = so->so_error; 3823 return (1); 3824 } else if (so->so_error) /* temporary udp error */ 3825 return (1); 3826 else if (((so->so_state & SS_ISCONNECTED) == 0) && 3827 (so->so_proto->pr_flags & PR_CONNREQUIRED)) 3828 return (0); 3829 else if (kn->kn_sfflags & NOTE_LOWAT) 3830 return (kn->kn_data >= kn->kn_sdata); 3831 else 3832 return (kn->kn_data >= so->so_snd.sb_lowat); 3833 } 3834 3835 static int 3836 filt_soempty(struct knote *kn, long hint) 3837 { 3838 struct socket *so; 3839 3840 so = kn->kn_fp->f_data; 3841 3842 if (SOLISTENING(so)) 3843 return (1); 3844 3845 SOCK_SENDBUF_LOCK_ASSERT(so); 3846 kn->kn_data = sbused(&so->so_snd); 3847 3848 if (kn->kn_data == 0) 3849 return (1); 3850 else 3851 return (0); 3852 } 3853 3854 int 3855 socheckuid(struct socket *so, uid_t uid) 3856 { 3857 3858 if (so == NULL) 3859 return (EPERM); 3860 if (so->so_cred->cr_uid != uid) 3861 return (EPERM); 3862 return (0); 3863 } 3864 3865 /* 3866 * These functions are used by protocols to notify the socket layer (and its 3867 * consumers) of state changes in the sockets driven by protocol-side events. 3868 */ 3869 3870 /* 3871 * Procedures to manipulate state flags of socket and do appropriate wakeups. 3872 * 3873 * Normal sequence from the active (originating) side is that 3874 * soisconnecting() is called during processing of connect() call, resulting 3875 * in an eventual call to soisconnected() if/when the connection is 3876 * established. When the connection is torn down soisdisconnecting() is 3877 * called during processing of disconnect() call, and soisdisconnected() is 3878 * called when the connection to the peer is totally severed. The semantics 3879 * of these routines are such that connectionless protocols can call 3880 * soisconnected() and soisdisconnected() only, bypassing the in-progress 3881 * calls when setting up a ``connection'' takes no time. 3882 * 3883 * From the passive side, a socket is created with two queues of sockets: 3884 * so_incomp for connections in progress and so_comp for connections already 3885 * made and awaiting user acceptance. As a protocol is preparing incoming 3886 * connections, it creates a socket structure queued on so_incomp by calling 3887 * sonewconn(). When the connection is established, soisconnected() is 3888 * called, and transfers the socket structure to so_comp, making it available 3889 * to accept(). 3890 * 3891 * If a socket is closed with sockets on either so_incomp or so_comp, these 3892 * sockets are dropped. 3893 * 3894 * If higher-level protocols are implemented in the kernel, the wakeups done 3895 * here will sometimes cause software-interrupt process scheduling. 3896 */ 3897 void 3898 soisconnecting(struct socket *so) 3899 { 3900 3901 SOCK_LOCK(so); 3902 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 3903 so->so_state |= SS_ISCONNECTING; 3904 SOCK_UNLOCK(so); 3905 } 3906 3907 void 3908 soisconnected(struct socket *so) 3909 { 3910 bool last __diagused; 3911 3912 SOCK_LOCK(so); 3913 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING); 3914 so->so_state |= SS_ISCONNECTED; 3915 3916 if (so->so_qstate == SQ_INCOMP) { 3917 struct socket *head = so->so_listen; 3918 int ret; 3919 3920 KASSERT(head, ("%s: so %p on incomp of NULL", __func__, so)); 3921 /* 3922 * Promoting a socket from incomplete queue to complete, we 3923 * need to go through reverse order of locking. We first do 3924 * trylock, and if that doesn't succeed, we go the hard way 3925 * leaving a reference and rechecking consistency after proper 3926 * locking. 3927 */ 3928 if (__predict_false(SOLISTEN_TRYLOCK(head) == 0)) { 3929 soref(head); 3930 SOCK_UNLOCK(so); 3931 SOLISTEN_LOCK(head); 3932 SOCK_LOCK(so); 3933 if (__predict_false(head != so->so_listen)) { 3934 /* 3935 * The socket went off the listen queue, 3936 * should be lost race to close(2) of sol. 3937 * The socket is about to soabort(). 3938 */ 3939 SOCK_UNLOCK(so); 3940 sorele_locked(head); 3941 return; 3942 } 3943 last = refcount_release(&head->so_count); 3944 KASSERT(!last, ("%s: released last reference for %p", 3945 __func__, head)); 3946 } 3947 again: 3948 if ((so->so_options & SO_ACCEPTFILTER) == 0) { 3949 TAILQ_REMOVE(&head->sol_incomp, so, so_list); 3950 head->sol_incqlen--; 3951 TAILQ_INSERT_TAIL(&head->sol_comp, so, so_list); 3952 head->sol_qlen++; 3953 so->so_qstate = SQ_COMP; 3954 SOCK_UNLOCK(so); 3955 solisten_wakeup(head); /* unlocks */ 3956 } else { 3957 SOCK_RECVBUF_LOCK(so); 3958 soupcall_set(so, SO_RCV, 3959 head->sol_accept_filter->accf_callback, 3960 head->sol_accept_filter_arg); 3961 so->so_options &= ~SO_ACCEPTFILTER; 3962 ret = head->sol_accept_filter->accf_callback(so, 3963 head->sol_accept_filter_arg, M_NOWAIT); 3964 if (ret == SU_ISCONNECTED) { 3965 soupcall_clear(so, SO_RCV); 3966 SOCK_RECVBUF_UNLOCK(so); 3967 goto again; 3968 } 3969 SOCK_RECVBUF_UNLOCK(so); 3970 SOCK_UNLOCK(so); 3971 SOLISTEN_UNLOCK(head); 3972 } 3973 return; 3974 } 3975 SOCK_UNLOCK(so); 3976 wakeup(&so->so_timeo); 3977 sorwakeup(so); 3978 sowwakeup(so); 3979 } 3980 3981 void 3982 soisdisconnecting(struct socket *so) 3983 { 3984 3985 SOCK_LOCK(so); 3986 so->so_state &= ~SS_ISCONNECTING; 3987 so->so_state |= SS_ISDISCONNECTING; 3988 3989 if (!SOLISTENING(so)) { 3990 SOCK_RECVBUF_LOCK(so); 3991 socantrcvmore_locked(so); 3992 SOCK_SENDBUF_LOCK(so); 3993 socantsendmore_locked(so); 3994 } 3995 SOCK_UNLOCK(so); 3996 wakeup(&so->so_timeo); 3997 } 3998 3999 void 4000 soisdisconnected(struct socket *so) 4001 { 4002 4003 SOCK_LOCK(so); 4004 4005 /* 4006 * There is at least one reader of so_state that does not 4007 * acquire socket lock, namely soreceive_generic(). Ensure 4008 * that it never sees all flags that track connection status 4009 * cleared, by ordering the update with a barrier semantic of 4010 * our release thread fence. 4011 */ 4012 so->so_state |= SS_ISDISCONNECTED; 4013 atomic_thread_fence_rel(); 4014 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 4015 4016 if (!SOLISTENING(so)) { 4017 SOCK_UNLOCK(so); 4018 SOCK_RECVBUF_LOCK(so); 4019 socantrcvmore_locked(so); 4020 SOCK_SENDBUF_LOCK(so); 4021 sbdrop_locked(&so->so_snd, sbused(&so->so_snd)); 4022 socantsendmore_locked(so); 4023 } else 4024 SOCK_UNLOCK(so); 4025 wakeup(&so->so_timeo); 4026 } 4027 4028 int 4029 soiolock(struct socket *so, struct sx *sx, int flags) 4030 { 4031 int error; 4032 4033 KASSERT((flags & SBL_VALID) == flags, 4034 ("soiolock: invalid flags %#x", flags)); 4035 4036 if ((flags & SBL_WAIT) != 0) { 4037 if ((flags & SBL_NOINTR) != 0) { 4038 sx_xlock(sx); 4039 } else { 4040 error = sx_xlock_sig(sx); 4041 if (error != 0) 4042 return (error); 4043 } 4044 } else if (!sx_try_xlock(sx)) { 4045 return (EWOULDBLOCK); 4046 } 4047 4048 if (__predict_false(SOLISTENING(so))) { 4049 sx_xunlock(sx); 4050 return (ENOTCONN); 4051 } 4052 return (0); 4053 } 4054 4055 void 4056 soiounlock(struct sx *sx) 4057 { 4058 sx_xunlock(sx); 4059 } 4060 4061 /* 4062 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. 4063 */ 4064 struct sockaddr * 4065 sodupsockaddr(const struct sockaddr *sa, int mflags) 4066 { 4067 struct sockaddr *sa2; 4068 4069 sa2 = malloc(sa->sa_len, M_SONAME, mflags); 4070 if (sa2) 4071 bcopy(sa, sa2, sa->sa_len); 4072 return sa2; 4073 } 4074 4075 /* 4076 * Register per-socket destructor. 4077 */ 4078 void 4079 sodtor_set(struct socket *so, so_dtor_t *func) 4080 { 4081 4082 SOCK_LOCK_ASSERT(so); 4083 so->so_dtor = func; 4084 } 4085 4086 /* 4087 * Register per-socket buffer upcalls. 4088 */ 4089 void 4090 soupcall_set(struct socket *so, sb_which which, so_upcall_t func, void *arg) 4091 { 4092 struct sockbuf *sb; 4093 4094 KASSERT(!SOLISTENING(so), ("%s: so %p listening", __func__, so)); 4095 4096 switch (which) { 4097 case SO_RCV: 4098 sb = &so->so_rcv; 4099 break; 4100 case SO_SND: 4101 sb = &so->so_snd; 4102 break; 4103 } 4104 SOCK_BUF_LOCK_ASSERT(so, which); 4105 sb->sb_upcall = func; 4106 sb->sb_upcallarg = arg; 4107 sb->sb_flags |= SB_UPCALL; 4108 } 4109 4110 void 4111 soupcall_clear(struct socket *so, sb_which which) 4112 { 4113 struct sockbuf *sb; 4114 4115 KASSERT(!SOLISTENING(so), ("%s: so %p listening", __func__, so)); 4116 4117 switch (which) { 4118 case SO_RCV: 4119 sb = &so->so_rcv; 4120 break; 4121 case SO_SND: 4122 sb = &so->so_snd; 4123 break; 4124 } 4125 SOCK_BUF_LOCK_ASSERT(so, which); 4126 KASSERT(sb->sb_upcall != NULL, 4127 ("%s: so %p no upcall to clear", __func__, so)); 4128 sb->sb_upcall = NULL; 4129 sb->sb_upcallarg = NULL; 4130 sb->sb_flags &= ~SB_UPCALL; 4131 } 4132 4133 void 4134 solisten_upcall_set(struct socket *so, so_upcall_t func, void *arg) 4135 { 4136 4137 SOLISTEN_LOCK_ASSERT(so); 4138 so->sol_upcall = func; 4139 so->sol_upcallarg = arg; 4140 } 4141 4142 static void 4143 so_rdknl_lock(void *arg) 4144 { 4145 struct socket *so = arg; 4146 4147 retry: 4148 if (SOLISTENING(so)) { 4149 SOLISTEN_LOCK(so); 4150 } else { 4151 SOCK_RECVBUF_LOCK(so); 4152 if (__predict_false(SOLISTENING(so))) { 4153 SOCK_RECVBUF_UNLOCK(so); 4154 goto retry; 4155 } 4156 } 4157 } 4158 4159 static void 4160 so_rdknl_unlock(void *arg) 4161 { 4162 struct socket *so = arg; 4163 4164 if (SOLISTENING(so)) 4165 SOLISTEN_UNLOCK(so); 4166 else 4167 SOCK_RECVBUF_UNLOCK(so); 4168 } 4169 4170 static void 4171 so_rdknl_assert_lock(void *arg, int what) 4172 { 4173 struct socket *so = arg; 4174 4175 if (what == LA_LOCKED) { 4176 if (SOLISTENING(so)) 4177 SOLISTEN_LOCK_ASSERT(so); 4178 else 4179 SOCK_RECVBUF_LOCK_ASSERT(so); 4180 } else { 4181 if (SOLISTENING(so)) 4182 SOLISTEN_UNLOCK_ASSERT(so); 4183 else 4184 SOCK_RECVBUF_UNLOCK_ASSERT(so); 4185 } 4186 } 4187 4188 static void 4189 so_wrknl_lock(void *arg) 4190 { 4191 struct socket *so = arg; 4192 4193 retry: 4194 if (SOLISTENING(so)) { 4195 SOLISTEN_LOCK(so); 4196 } else { 4197 SOCK_SENDBUF_LOCK(so); 4198 if (__predict_false(SOLISTENING(so))) { 4199 SOCK_SENDBUF_UNLOCK(so); 4200 goto retry; 4201 } 4202 } 4203 } 4204 4205 static void 4206 so_wrknl_unlock(void *arg) 4207 { 4208 struct socket *so = arg; 4209 4210 if (SOLISTENING(so)) 4211 SOLISTEN_UNLOCK(so); 4212 else 4213 SOCK_SENDBUF_UNLOCK(so); 4214 } 4215 4216 static void 4217 so_wrknl_assert_lock(void *arg, int what) 4218 { 4219 struct socket *so = arg; 4220 4221 if (what == LA_LOCKED) { 4222 if (SOLISTENING(so)) 4223 SOLISTEN_LOCK_ASSERT(so); 4224 else 4225 SOCK_SENDBUF_LOCK_ASSERT(so); 4226 } else { 4227 if (SOLISTENING(so)) 4228 SOLISTEN_UNLOCK_ASSERT(so); 4229 else 4230 SOCK_SENDBUF_UNLOCK_ASSERT(so); 4231 } 4232 } 4233 4234 /* 4235 * Create an external-format (``xsocket'') structure using the information in 4236 * the kernel-format socket structure pointed to by so. This is done to 4237 * reduce the spew of irrelevant information over this interface, to isolate 4238 * user code from changes in the kernel structure, and potentially to provide 4239 * information-hiding if we decide that some of this information should be 4240 * hidden from users. 4241 */ 4242 void 4243 sotoxsocket(struct socket *so, struct xsocket *xso) 4244 { 4245 4246 bzero(xso, sizeof(*xso)); 4247 xso->xso_len = sizeof *xso; 4248 xso->xso_so = (uintptr_t)so; 4249 xso->so_type = so->so_type; 4250 xso->so_options = so->so_options; 4251 xso->so_linger = so->so_linger; 4252 xso->so_state = so->so_state; 4253 xso->so_pcb = (uintptr_t)so->so_pcb; 4254 xso->xso_protocol = so->so_proto->pr_protocol; 4255 xso->xso_family = so->so_proto->pr_domain->dom_family; 4256 xso->so_timeo = so->so_timeo; 4257 xso->so_error = so->so_error; 4258 xso->so_uid = so->so_cred->cr_uid; 4259 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0; 4260 if (SOLISTENING(so)) { 4261 xso->so_qlen = so->sol_qlen; 4262 xso->so_incqlen = so->sol_incqlen; 4263 xso->so_qlimit = so->sol_qlimit; 4264 xso->so_oobmark = 0; 4265 } else { 4266 xso->so_state |= so->so_qstate; 4267 xso->so_qlen = xso->so_incqlen = xso->so_qlimit = 0; 4268 xso->so_oobmark = so->so_oobmark; 4269 sbtoxsockbuf(&so->so_snd, &xso->so_snd); 4270 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 4271 } 4272 } 4273 4274 int 4275 so_options_get(const struct socket *so) 4276 { 4277 4278 return (so->so_options); 4279 } 4280 4281 void 4282 so_options_set(struct socket *so, int val) 4283 { 4284 4285 so->so_options = val; 4286 } 4287 4288 int 4289 so_error_get(const struct socket *so) 4290 { 4291 4292 return (so->so_error); 4293 } 4294 4295 void 4296 so_error_set(struct socket *so, int val) 4297 { 4298 4299 so->so_error = val; 4300 } 4301