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