1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993 3 * The Regents of the University of California. 4 * Copyright (c) 2004 The FreeBSD Foundation 5 * Copyright (c) 2004-2008 Robert N. M. Watson 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94 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 * and sorflush(), which are usually called from a pre-set VNET context. 100 * sopoll() currently does not need a VNET context to be set. 101 */ 102 103 #include <sys/cdefs.h> 104 __FBSDID("$FreeBSD$"); 105 106 #include "opt_inet.h" 107 #include "opt_inet6.h" 108 #include "opt_compat.h" 109 110 #include <sys/param.h> 111 #include <sys/systm.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/event.h> 125 #include <sys/eventhandler.h> 126 #include <sys/poll.h> 127 #include <sys/proc.h> 128 #include <sys/protosw.h> 129 #include <sys/socket.h> 130 #include <sys/socketvar.h> 131 #include <sys/resourcevar.h> 132 #include <net/route.h> 133 #include <sys/signalvar.h> 134 #include <sys/stat.h> 135 #include <sys/sx.h> 136 #include <sys/sysctl.h> 137 #include <sys/uio.h> 138 #include <sys/jail.h> 139 #include <sys/syslog.h> 140 #include <netinet/in.h> 141 142 #include <net/vnet.h> 143 144 #include <security/mac/mac_framework.h> 145 146 #include <vm/uma.h> 147 148 #ifdef COMPAT_FREEBSD32 149 #include <sys/mount.h> 150 #include <sys/sysent.h> 151 #include <compat/freebsd32/freebsd32.h> 152 #endif 153 154 static int soreceive_rcvoob(struct socket *so, struct uio *uio, 155 int flags); 156 157 static void filt_sordetach(struct knote *kn); 158 static int filt_soread(struct knote *kn, long hint); 159 static void filt_sowdetach(struct knote *kn); 160 static int filt_sowrite(struct knote *kn, long hint); 161 static int filt_solisten(struct knote *kn, long hint); 162 static int inline hhook_run_socket(struct socket *so, void *hctx, int32_t h_id); 163 fo_kqfilter_t soo_kqfilter; 164 165 static struct filterops solisten_filtops = { 166 .f_isfd = 1, 167 .f_detach = filt_sordetach, 168 .f_event = filt_solisten, 169 }; 170 static struct filterops soread_filtops = { 171 .f_isfd = 1, 172 .f_detach = filt_sordetach, 173 .f_event = filt_soread, 174 }; 175 static struct filterops sowrite_filtops = { 176 .f_isfd = 1, 177 .f_detach = filt_sowdetach, 178 .f_event = filt_sowrite, 179 }; 180 181 so_gen_t so_gencnt; /* generation count for sockets */ 182 183 MALLOC_DEFINE(M_SONAME, "soname", "socket name"); 184 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block"); 185 186 #define VNET_SO_ASSERT(so) \ 187 VNET_ASSERT(curvnet != NULL, \ 188 ("%s:%d curvnet is NULL, so=%p", __func__, __LINE__, (so))); 189 190 VNET_DEFINE(struct hhook_head *, socket_hhh[HHOOK_SOCKET_LAST + 1]); 191 #define V_socket_hhh VNET(socket_hhh) 192 193 /* 194 * Limit on the number of connections in the listen queue waiting 195 * for accept(2). 196 * NB: The orginal sysctl somaxconn is still available but hidden 197 * to prevent confusion about the actual purpose of this number. 198 */ 199 static int somaxconn = SOMAXCONN; 200 201 static int 202 sysctl_somaxconn(SYSCTL_HANDLER_ARGS) 203 { 204 int error; 205 int val; 206 207 val = somaxconn; 208 error = sysctl_handle_int(oidp, &val, 0, req); 209 if (error || !req->newptr ) 210 return (error); 211 212 if (val < 1 || val > USHRT_MAX) 213 return (EINVAL); 214 215 somaxconn = val; 216 return (0); 217 } 218 SYSCTL_PROC(_kern_ipc, OID_AUTO, soacceptqueue, CTLTYPE_UINT | CTLFLAG_RW, 219 0, sizeof(int), sysctl_somaxconn, "I", 220 "Maximum listen socket pending connection accept queue size"); 221 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn, 222 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_SKIP, 223 0, sizeof(int), sysctl_somaxconn, "I", 224 "Maximum listen socket pending connection accept queue size (compat)"); 225 226 static int numopensockets; 227 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD, 228 &numopensockets, 0, "Number of open sockets"); 229 230 /* 231 * accept_mtx locks down per-socket fields relating to accept queues. See 232 * socketvar.h for an annotation of the protected fields of struct socket. 233 */ 234 struct mtx accept_mtx; 235 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF); 236 237 /* 238 * so_global_mtx protects so_gencnt, numopensockets, and the per-socket 239 * so_gencnt field. 240 */ 241 static struct mtx so_global_mtx; 242 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF); 243 244 /* 245 * General IPC sysctl name space, used by sockets and a variety of other IPC 246 * types. 247 */ 248 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC"); 249 250 /* 251 * Initialize the socket subsystem and set up the socket 252 * memory allocator. 253 */ 254 static uma_zone_t socket_zone; 255 int maxsockets; 256 257 static void 258 socket_zone_change(void *tag) 259 { 260 261 maxsockets = uma_zone_set_max(socket_zone, maxsockets); 262 } 263 264 static void 265 socket_hhook_register(int subtype) 266 { 267 268 if (hhook_head_register(HHOOK_TYPE_SOCKET, subtype, 269 &V_socket_hhh[subtype], 270 HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0) 271 printf("%s: WARNING: unable to register hook\n", __func__); 272 } 273 274 static void 275 socket_hhook_deregister(int subtype) 276 { 277 278 if (hhook_head_deregister(V_socket_hhh[subtype]) != 0) 279 printf("%s: WARNING: unable to deregister hook\n", __func__); 280 } 281 282 static void 283 socket_init(void *tag) 284 { 285 286 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL, 287 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 288 maxsockets = uma_zone_set_max(socket_zone, maxsockets); 289 uma_zone_set_warning(socket_zone, "kern.ipc.maxsockets limit reached"); 290 EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL, 291 EVENTHANDLER_PRI_FIRST); 292 } 293 SYSINIT(socket, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, socket_init, NULL); 294 295 static void 296 socket_vnet_init(const void *unused __unused) 297 { 298 int i; 299 300 /* We expect a contiguous range */ 301 for (i = 0; i <= HHOOK_SOCKET_LAST; i++) 302 socket_hhook_register(i); 303 } 304 VNET_SYSINIT(socket_vnet_init, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, 305 socket_vnet_init, NULL); 306 307 static void 308 socket_vnet_uninit(const void *unused __unused) 309 { 310 int i; 311 312 for (i = 0; i <= HHOOK_SOCKET_LAST; i++) 313 socket_hhook_deregister(i); 314 } 315 VNET_SYSUNINIT(socket_vnet_uninit, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, 316 socket_vnet_uninit, NULL); 317 318 /* 319 * Initialise maxsockets. This SYSINIT must be run after 320 * tunable_mbinit(). 321 */ 322 static void 323 init_maxsockets(void *ignored) 324 { 325 326 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets); 327 maxsockets = imax(maxsockets, maxfiles); 328 } 329 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL); 330 331 /* 332 * Sysctl to get and set the maximum global sockets limit. Notify protocols 333 * of the change so that they can update their dependent limits as required. 334 */ 335 static int 336 sysctl_maxsockets(SYSCTL_HANDLER_ARGS) 337 { 338 int error, newmaxsockets; 339 340 newmaxsockets = maxsockets; 341 error = sysctl_handle_int(oidp, &newmaxsockets, 0, req); 342 if (error == 0 && req->newptr) { 343 if (newmaxsockets > maxsockets && 344 newmaxsockets <= maxfiles) { 345 maxsockets = newmaxsockets; 346 EVENTHANDLER_INVOKE(maxsockets_change); 347 } else 348 error = EINVAL; 349 } 350 return (error); 351 } 352 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW, 353 &maxsockets, 0, sysctl_maxsockets, "IU", 354 "Maximum number of sockets avaliable"); 355 356 /* 357 * Socket operation routines. These routines are called by the routines in 358 * sys_socket.c or from a system process, and implement the semantics of 359 * socket operations by switching out to the protocol specific routines. 360 */ 361 362 /* 363 * Get a socket structure from our zone, and initialize it. Note that it 364 * would probably be better to allocate socket and PCB at the same time, but 365 * I'm not convinced that all the protocols can be easily modified to do 366 * this. 367 * 368 * soalloc() returns a socket with a ref count of 0. 369 */ 370 static struct socket * 371 soalloc(struct vnet *vnet) 372 { 373 struct socket *so; 374 375 so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO); 376 if (so == NULL) 377 return (NULL); 378 #ifdef MAC 379 if (mac_socket_init(so, M_NOWAIT) != 0) { 380 uma_zfree(socket_zone, so); 381 return (NULL); 382 } 383 #endif 384 if (khelp_init_osd(HELPER_CLASS_SOCKET, &so->osd)) { 385 uma_zfree(socket_zone, so); 386 return (NULL); 387 } 388 389 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd"); 390 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv"); 391 sx_init(&so->so_snd.sb_sx, "so_snd_sx"); 392 sx_init(&so->so_rcv.sb_sx, "so_rcv_sx"); 393 TAILQ_INIT(&so->so_aiojobq); 394 mtx_lock(&so_global_mtx); 395 so->so_gencnt = ++so_gencnt; 396 ++numopensockets; 397 #ifdef VIMAGE 398 VNET_ASSERT(vnet != NULL, ("%s:%d vnet is NULL, so=%p", 399 __func__, __LINE__, so)); 400 vnet->vnet_sockcnt++; 401 so->so_vnet = vnet; 402 #endif 403 mtx_unlock(&so_global_mtx); 404 405 CURVNET_SET(vnet); 406 /* We shouldn't need the so_global_mtx */ 407 if (V_socket_hhh[HHOOK_SOCKET_CREATE]->hhh_nhooks > 0) { 408 if (hhook_run_socket(so, NULL, HHOOK_SOCKET_CREATE)) 409 /* Do we need more comprehensive error returns? */ 410 so = NULL; 411 } 412 CURVNET_RESTORE(); 413 414 return (so); 415 } 416 417 /* 418 * Free the storage associated with a socket at the socket layer, tear down 419 * locks, labels, etc. All protocol state is assumed already to have been 420 * torn down (and possibly never set up) by the caller. 421 */ 422 static void 423 sodealloc(struct socket *so) 424 { 425 426 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count)); 427 KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL")); 428 429 mtx_lock(&so_global_mtx); 430 so->so_gencnt = ++so_gencnt; 431 --numopensockets; /* Could be below, but faster here. */ 432 #ifdef VIMAGE 433 VNET_ASSERT(so->so_vnet != NULL, ("%s:%d so_vnet is NULL, so=%p", 434 __func__, __LINE__, so)); 435 so->so_vnet->vnet_sockcnt--; 436 #endif 437 mtx_unlock(&so_global_mtx); 438 if (so->so_rcv.sb_hiwat) 439 (void)chgsbsize(so->so_cred->cr_uidinfo, 440 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY); 441 if (so->so_snd.sb_hiwat) 442 (void)chgsbsize(so->so_cred->cr_uidinfo, 443 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY); 444 /* remove acccept filter if one is present. */ 445 if (so->so_accf != NULL) 446 do_setopt_accept_filter(so, NULL); 447 #ifdef MAC 448 mac_socket_destroy(so); 449 #endif 450 CURVNET_SET(so->so_vnet); 451 if (V_socket_hhh[HHOOK_SOCKET_CLOSE]->hhh_nhooks > 0) 452 hhook_run_socket(so, NULL, HHOOK_SOCKET_CLOSE); 453 CURVNET_RESTORE(); 454 455 crfree(so->so_cred); 456 khelp_destroy_osd(&so->osd); 457 sx_destroy(&so->so_snd.sb_sx); 458 sx_destroy(&so->so_rcv.sb_sx); 459 SOCKBUF_LOCK_DESTROY(&so->so_snd); 460 SOCKBUF_LOCK_DESTROY(&so->so_rcv); 461 uma_zfree(socket_zone, so); 462 } 463 464 /* 465 * socreate returns a socket with a ref count of 1. The socket should be 466 * closed with soclose(). 467 */ 468 int 469 socreate(int dom, struct socket **aso, int type, int proto, 470 struct ucred *cred, struct thread *td) 471 { 472 struct protosw *prp; 473 struct socket *so; 474 int error; 475 476 if (proto) 477 prp = pffindproto(dom, proto, type); 478 else 479 prp = pffindtype(dom, type); 480 481 if (prp == NULL) { 482 /* No support for domain. */ 483 if (pffinddomain(dom) == NULL) 484 return (EAFNOSUPPORT); 485 /* No support for socket type. */ 486 if (proto == 0 && type != 0) 487 return (EPROTOTYPE); 488 return (EPROTONOSUPPORT); 489 } 490 if (prp->pr_usrreqs->pru_attach == NULL || 491 prp->pr_usrreqs->pru_attach == pru_attach_notsupp) 492 return (EPROTONOSUPPORT); 493 494 if (prison_check_af(cred, prp->pr_domain->dom_family) != 0) 495 return (EPROTONOSUPPORT); 496 497 if (prp->pr_type != type) 498 return (EPROTOTYPE); 499 so = soalloc(CRED_TO_VNET(cred)); 500 if (so == NULL) 501 return (ENOBUFS); 502 503 TAILQ_INIT(&so->so_incomp); 504 TAILQ_INIT(&so->so_comp); 505 so->so_type = type; 506 so->so_cred = crhold(cred); 507 if ((prp->pr_domain->dom_family == PF_INET) || 508 (prp->pr_domain->dom_family == PF_INET6) || 509 (prp->pr_domain->dom_family == PF_ROUTE)) 510 so->so_fibnum = td->td_proc->p_fibnum; 511 else 512 so->so_fibnum = 0; 513 so->so_proto = prp; 514 #ifdef MAC 515 mac_socket_create(cred, so); 516 #endif 517 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv)); 518 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd)); 519 so->so_count = 1; 520 /* 521 * Auto-sizing of socket buffers is managed by the protocols and 522 * the appropriate flags must be set in the pru_attach function. 523 */ 524 CURVNET_SET(so->so_vnet); 525 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td); 526 CURVNET_RESTORE(); 527 if (error) { 528 KASSERT(so->so_count == 1, ("socreate: so_count %d", 529 so->so_count)); 530 so->so_count = 0; 531 sodealloc(so); 532 return (error); 533 } 534 *aso = so; 535 return (0); 536 } 537 538 #ifdef REGRESSION 539 static int regression_sonewconn_earlytest = 1; 540 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW, 541 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test"); 542 #endif 543 544 /* 545 * When an attempt at a new connection is noted on a socket which accepts 546 * connections, sonewconn is called. If the connection is possible (subject 547 * to space constraints, etc.) then we allocate a new structure, propoerly 548 * linked into the data structure of the original socket, and return this. 549 * Connstatus may be 0, or SS_ISCONFIRMING, or SS_ISCONNECTED. 550 * 551 * Note: the ref count on the socket is 0 on return. 552 */ 553 struct socket * 554 sonewconn(struct socket *head, int connstatus) 555 { 556 static struct timeval lastover; 557 static struct timeval overinterval = { 60, 0 }; 558 static int overcount; 559 560 struct socket *so; 561 int over; 562 563 ACCEPT_LOCK(); 564 over = (head->so_qlen > 3 * head->so_qlimit / 2); 565 ACCEPT_UNLOCK(); 566 #ifdef REGRESSION 567 if (regression_sonewconn_earlytest && over) { 568 #else 569 if (over) { 570 #endif 571 overcount++; 572 573 if (ratecheck(&lastover, &overinterval)) { 574 log(LOG_DEBUG, "%s: pcb %p: Listen queue overflow: " 575 "%i already in queue awaiting acceptance " 576 "(%d occurrences)\n", 577 __func__, head->so_pcb, head->so_qlen, overcount); 578 579 overcount = 0; 580 } 581 582 return (NULL); 583 } 584 VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p", 585 __func__, __LINE__, head)); 586 so = soalloc(head->so_vnet); 587 if (so == NULL) { 588 log(LOG_DEBUG, "%s: pcb %p: New socket allocation failure: " 589 "limit reached or out of memory\n", 590 __func__, head->so_pcb); 591 return (NULL); 592 } 593 if ((head->so_options & SO_ACCEPTFILTER) != 0) 594 connstatus = 0; 595 so->so_head = head; 596 so->so_type = head->so_type; 597 so->so_options = head->so_options &~ SO_ACCEPTCONN; 598 so->so_linger = head->so_linger; 599 so->so_state = head->so_state | SS_NOFDREF; 600 so->so_fibnum = head->so_fibnum; 601 so->so_proto = head->so_proto; 602 so->so_cred = crhold(head->so_cred); 603 #ifdef MAC 604 mac_socket_newconn(head, so); 605 #endif 606 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv)); 607 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd)); 608 VNET_SO_ASSERT(head); 609 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) { 610 sodealloc(so); 611 log(LOG_DEBUG, "%s: pcb %p: soreserve() failed\n", 612 __func__, head->so_pcb); 613 return (NULL); 614 } 615 if ((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) { 616 sodealloc(so); 617 log(LOG_DEBUG, "%s: pcb %p: pru_attach() failed\n", 618 __func__, head->so_pcb); 619 return (NULL); 620 } 621 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat; 622 so->so_snd.sb_lowat = head->so_snd.sb_lowat; 623 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo; 624 so->so_snd.sb_timeo = head->so_snd.sb_timeo; 625 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE; 626 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE; 627 so->so_state |= connstatus; 628 ACCEPT_LOCK(); 629 /* 630 * The accept socket may be tearing down but we just 631 * won a race on the ACCEPT_LOCK. 632 * However, if sctp_peeloff() is called on a 1-to-many 633 * style socket, the SO_ACCEPTCONN doesn't need to be set. 634 */ 635 if (!(head->so_options & SO_ACCEPTCONN) && 636 ((head->so_proto->pr_protocol != IPPROTO_SCTP) || 637 (head->so_type != SOCK_SEQPACKET))) { 638 SOCK_LOCK(so); 639 so->so_head = NULL; 640 sofree(so); /* NB: returns ACCEPT_UNLOCK'ed. */ 641 return (NULL); 642 } 643 if (connstatus) { 644 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 645 so->so_qstate |= SQ_COMP; 646 head->so_qlen++; 647 } else { 648 /* 649 * Keep removing sockets from the head until there's room for 650 * us to insert on the tail. In pre-locking revisions, this 651 * was a simple if(), but as we could be racing with other 652 * threads and soabort() requires dropping locks, we must 653 * loop waiting for the condition to be true. 654 */ 655 while (head->so_incqlen > head->so_qlimit) { 656 struct socket *sp; 657 sp = TAILQ_FIRST(&head->so_incomp); 658 TAILQ_REMOVE(&head->so_incomp, sp, so_list); 659 head->so_incqlen--; 660 sp->so_qstate &= ~SQ_INCOMP; 661 sp->so_head = NULL; 662 ACCEPT_UNLOCK(); 663 soabort(sp); 664 ACCEPT_LOCK(); 665 } 666 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); 667 so->so_qstate |= SQ_INCOMP; 668 head->so_incqlen++; 669 } 670 ACCEPT_UNLOCK(); 671 if (connstatus) { 672 sorwakeup(head); 673 wakeup_one(&head->so_timeo); 674 } 675 return (so); 676 } 677 678 int 679 sobind(struct socket *so, struct sockaddr *nam, struct thread *td) 680 { 681 int error; 682 683 CURVNET_SET(so->so_vnet); 684 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td); 685 CURVNET_RESTORE(); 686 return (error); 687 } 688 689 int 690 sobindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td) 691 { 692 int error; 693 694 CURVNET_SET(so->so_vnet); 695 error = (*so->so_proto->pr_usrreqs->pru_bindat)(fd, so, nam, td); 696 CURVNET_RESTORE(); 697 return (error); 698 } 699 700 /* 701 * solisten() transitions a socket from a non-listening state to a listening 702 * state, but can also be used to update the listen queue depth on an 703 * existing listen socket. The protocol will call back into the sockets 704 * layer using solisten_proto_check() and solisten_proto() to check and set 705 * socket-layer listen state. Call backs are used so that the protocol can 706 * acquire both protocol and socket layer locks in whatever order is required 707 * by the protocol. 708 * 709 * Protocol implementors are advised to hold the socket lock across the 710 * socket-layer test and set to avoid races at the socket layer. 711 */ 712 int 713 solisten(struct socket *so, int backlog, struct thread *td) 714 { 715 int error; 716 717 CURVNET_SET(so->so_vnet); 718 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td); 719 CURVNET_RESTORE(); 720 return (error); 721 } 722 723 int 724 solisten_proto_check(struct socket *so) 725 { 726 727 SOCK_LOCK_ASSERT(so); 728 729 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | 730 SS_ISDISCONNECTING)) 731 return (EINVAL); 732 return (0); 733 } 734 735 void 736 solisten_proto(struct socket *so, int backlog) 737 { 738 739 SOCK_LOCK_ASSERT(so); 740 741 if (backlog < 0 || backlog > somaxconn) 742 backlog = somaxconn; 743 so->so_qlimit = backlog; 744 so->so_options |= SO_ACCEPTCONN; 745 } 746 747 /* 748 * Evaluate the reference count and named references on a socket; if no 749 * references remain, free it. This should be called whenever a reference is 750 * released, such as in sorele(), but also when named reference flags are 751 * cleared in socket or protocol code. 752 * 753 * sofree() will free the socket if: 754 * 755 * - There are no outstanding file descriptor references or related consumers 756 * (so_count == 0). 757 * 758 * - The socket has been closed by user space, if ever open (SS_NOFDREF). 759 * 760 * - The protocol does not have an outstanding strong reference on the socket 761 * (SS_PROTOREF). 762 * 763 * - The socket is not in a completed connection queue, so a process has been 764 * notified that it is present. If it is removed, the user process may 765 * block in accept() despite select() saying the socket was ready. 766 */ 767 void 768 sofree(struct socket *so) 769 { 770 struct protosw *pr = so->so_proto; 771 struct socket *head; 772 773 ACCEPT_LOCK_ASSERT(); 774 SOCK_LOCK_ASSERT(so); 775 776 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 || 777 (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) { 778 SOCK_UNLOCK(so); 779 ACCEPT_UNLOCK(); 780 return; 781 } 782 783 head = so->so_head; 784 if (head != NULL) { 785 KASSERT((so->so_qstate & SQ_COMP) != 0 || 786 (so->so_qstate & SQ_INCOMP) != 0, 787 ("sofree: so_head != NULL, but neither SQ_COMP nor " 788 "SQ_INCOMP")); 789 KASSERT((so->so_qstate & SQ_COMP) == 0 || 790 (so->so_qstate & SQ_INCOMP) == 0, 791 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP")); 792 TAILQ_REMOVE(&head->so_incomp, so, so_list); 793 head->so_incqlen--; 794 so->so_qstate &= ~SQ_INCOMP; 795 so->so_head = NULL; 796 } 797 KASSERT((so->so_qstate & SQ_COMP) == 0 && 798 (so->so_qstate & SQ_INCOMP) == 0, 799 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)", 800 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP)); 801 if (so->so_options & SO_ACCEPTCONN) { 802 KASSERT((TAILQ_EMPTY(&so->so_comp)), 803 ("sofree: so_comp populated")); 804 KASSERT((TAILQ_EMPTY(&so->so_incomp)), 805 ("sofree: so_incomp populated")); 806 } 807 SOCK_UNLOCK(so); 808 ACCEPT_UNLOCK(); 809 810 VNET_SO_ASSERT(so); 811 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL) 812 (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb); 813 if (pr->pr_usrreqs->pru_detach != NULL) 814 (*pr->pr_usrreqs->pru_detach)(so); 815 816 /* 817 * From this point on, we assume that no other references to this 818 * socket exist anywhere else in the stack. Therefore, no locks need 819 * to be acquired or held. 820 * 821 * We used to do a lot of socket buffer and socket locking here, as 822 * well as invoke sorflush() and perform wakeups. The direct call to 823 * dom_dispose() and sbrelease_internal() are an inlining of what was 824 * necessary from sorflush(). 825 * 826 * Notice that the socket buffer and kqueue state are torn down 827 * before calling pru_detach. This means that protocols shold not 828 * assume they can perform socket wakeups, etc, in their detach code. 829 */ 830 sbdestroy(&so->so_snd, so); 831 sbdestroy(&so->so_rcv, so); 832 seldrain(&so->so_snd.sb_sel); 833 seldrain(&so->so_rcv.sb_sel); 834 knlist_destroy(&so->so_rcv.sb_sel.si_note); 835 knlist_destroy(&so->so_snd.sb_sel.si_note); 836 sodealloc(so); 837 } 838 839 /* 840 * Close a socket on last file table reference removal. Initiate disconnect 841 * if connected. Free socket when disconnect complete. 842 * 843 * This function will sorele() the socket. Note that soclose() may be called 844 * prior to the ref count reaching zero. The actual socket structure will 845 * not be freed until the ref count reaches zero. 846 */ 847 int 848 soclose(struct socket *so) 849 { 850 int error = 0; 851 852 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter")); 853 854 CURVNET_SET(so->so_vnet); 855 funsetown(&so->so_sigio); 856 if (so->so_state & SS_ISCONNECTED) { 857 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 858 error = sodisconnect(so); 859 if (error) { 860 if (error == ENOTCONN) 861 error = 0; 862 goto drop; 863 } 864 } 865 if (so->so_options & SO_LINGER) { 866 if ((so->so_state & SS_ISDISCONNECTING) && 867 (so->so_state & SS_NBIO)) 868 goto drop; 869 while (so->so_state & SS_ISCONNECTED) { 870 error = tsleep(&so->so_timeo, 871 PSOCK | PCATCH, "soclos", 872 so->so_linger * hz); 873 if (error) 874 break; 875 } 876 } 877 } 878 879 drop: 880 if (so->so_proto->pr_usrreqs->pru_close != NULL) 881 (*so->so_proto->pr_usrreqs->pru_close)(so); 882 ACCEPT_LOCK(); 883 if (so->so_options & SO_ACCEPTCONN) { 884 struct socket *sp; 885 /* 886 * Prevent new additions to the accept queues due 887 * to ACCEPT_LOCK races while we are draining them. 888 */ 889 so->so_options &= ~SO_ACCEPTCONN; 890 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) { 891 TAILQ_REMOVE(&so->so_incomp, sp, so_list); 892 so->so_incqlen--; 893 sp->so_qstate &= ~SQ_INCOMP; 894 sp->so_head = NULL; 895 ACCEPT_UNLOCK(); 896 soabort(sp); 897 ACCEPT_LOCK(); 898 } 899 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) { 900 TAILQ_REMOVE(&so->so_comp, sp, so_list); 901 so->so_qlen--; 902 sp->so_qstate &= ~SQ_COMP; 903 sp->so_head = NULL; 904 ACCEPT_UNLOCK(); 905 soabort(sp); 906 ACCEPT_LOCK(); 907 } 908 KASSERT((TAILQ_EMPTY(&so->so_comp)), 909 ("%s: so_comp populated", __func__)); 910 KASSERT((TAILQ_EMPTY(&so->so_incomp)), 911 ("%s: so_incomp populated", __func__)); 912 } 913 SOCK_LOCK(so); 914 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF")); 915 so->so_state |= SS_NOFDREF; 916 sorele(so); /* NB: Returns with ACCEPT_UNLOCK(). */ 917 CURVNET_RESTORE(); 918 return (error); 919 } 920 921 /* 922 * soabort() is used to abruptly tear down a connection, such as when a 923 * resource limit is reached (listen queue depth exceeded), or if a listen 924 * socket is closed while there are sockets waiting to be accepted. 925 * 926 * This interface is tricky, because it is called on an unreferenced socket, 927 * and must be called only by a thread that has actually removed the socket 928 * from the listen queue it was on, or races with other threads are risked. 929 * 930 * This interface will call into the protocol code, so must not be called 931 * with any socket locks held. Protocols do call it while holding their own 932 * recursible protocol mutexes, but this is something that should be subject 933 * to review in the future. 934 */ 935 void 936 soabort(struct socket *so) 937 { 938 939 /* 940 * In as much as is possible, assert that no references to this 941 * socket are held. This is not quite the same as asserting that the 942 * current thread is responsible for arranging for no references, but 943 * is as close as we can get for now. 944 */ 945 KASSERT(so->so_count == 0, ("soabort: so_count")); 946 KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF")); 947 KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF")); 948 KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP")); 949 KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP")); 950 VNET_SO_ASSERT(so); 951 952 if (so->so_proto->pr_usrreqs->pru_abort != NULL) 953 (*so->so_proto->pr_usrreqs->pru_abort)(so); 954 ACCEPT_LOCK(); 955 SOCK_LOCK(so); 956 sofree(so); 957 } 958 959 int 960 soaccept(struct socket *so, struct sockaddr **nam) 961 { 962 int error; 963 964 SOCK_LOCK(so); 965 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF")); 966 so->so_state &= ~SS_NOFDREF; 967 SOCK_UNLOCK(so); 968 969 CURVNET_SET(so->so_vnet); 970 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam); 971 CURVNET_RESTORE(); 972 return (error); 973 } 974 975 int 976 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td) 977 { 978 979 return (soconnectat(AT_FDCWD, so, nam, td)); 980 } 981 982 int 983 soconnectat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td) 984 { 985 int error; 986 987 if (so->so_options & SO_ACCEPTCONN) 988 return (EOPNOTSUPP); 989 990 CURVNET_SET(so->so_vnet); 991 /* 992 * If protocol is connection-based, can only connect once. 993 * Otherwise, if connected, try to disconnect first. This allows 994 * user to disconnect by connecting to, e.g., a null address. 995 */ 996 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 997 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 998 (error = sodisconnect(so)))) { 999 error = EISCONN; 1000 } else { 1001 /* 1002 * Prevent accumulated error from previous connection from 1003 * biting us. 1004 */ 1005 so->so_error = 0; 1006 if (fd == AT_FDCWD) { 1007 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, 1008 nam, td); 1009 } else { 1010 error = (*so->so_proto->pr_usrreqs->pru_connectat)(fd, 1011 so, nam, td); 1012 } 1013 } 1014 CURVNET_RESTORE(); 1015 1016 return (error); 1017 } 1018 1019 int 1020 soconnect2(struct socket *so1, struct socket *so2) 1021 { 1022 int error; 1023 1024 CURVNET_SET(so1->so_vnet); 1025 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2); 1026 CURVNET_RESTORE(); 1027 return (error); 1028 } 1029 1030 int 1031 sodisconnect(struct socket *so) 1032 { 1033 int error; 1034 1035 if ((so->so_state & SS_ISCONNECTED) == 0) 1036 return (ENOTCONN); 1037 if (so->so_state & SS_ISDISCONNECTING) 1038 return (EALREADY); 1039 VNET_SO_ASSERT(so); 1040 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so); 1041 return (error); 1042 } 1043 1044 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT) 1045 1046 int 1047 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio, 1048 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 1049 { 1050 long space; 1051 ssize_t resid; 1052 int clen = 0, error, dontroute; 1053 1054 KASSERT(so->so_type == SOCK_DGRAM, ("sosend_dgram: !SOCK_DGRAM")); 1055 KASSERT(so->so_proto->pr_flags & PR_ATOMIC, 1056 ("sosend_dgram: !PR_ATOMIC")); 1057 1058 if (uio != NULL) 1059 resid = uio->uio_resid; 1060 else 1061 resid = top->m_pkthdr.len; 1062 /* 1063 * In theory resid should be unsigned. However, space must be 1064 * signed, as it might be less than 0 if we over-committed, and we 1065 * must use a signed comparison of space and resid. On the other 1066 * hand, a negative resid causes us to loop sending 0-length 1067 * segments to the protocol. 1068 */ 1069 if (resid < 0) { 1070 error = EINVAL; 1071 goto out; 1072 } 1073 1074 dontroute = 1075 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0; 1076 if (td != NULL) 1077 td->td_ru.ru_msgsnd++; 1078 if (control != NULL) 1079 clen = control->m_len; 1080 1081 SOCKBUF_LOCK(&so->so_snd); 1082 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 1083 SOCKBUF_UNLOCK(&so->so_snd); 1084 error = EPIPE; 1085 goto out; 1086 } 1087 if (so->so_error) { 1088 error = so->so_error; 1089 so->so_error = 0; 1090 SOCKBUF_UNLOCK(&so->so_snd); 1091 goto out; 1092 } 1093 if ((so->so_state & SS_ISCONNECTED) == 0) { 1094 /* 1095 * `sendto' and `sendmsg' is allowed on a connection-based 1096 * socket if it supports implied connect. Return ENOTCONN if 1097 * not connected and no address is supplied. 1098 */ 1099 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 1100 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 1101 if ((so->so_state & SS_ISCONFIRMING) == 0 && 1102 !(resid == 0 && clen != 0)) { 1103 SOCKBUF_UNLOCK(&so->so_snd); 1104 error = ENOTCONN; 1105 goto out; 1106 } 1107 } else if (addr == NULL) { 1108 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 1109 error = ENOTCONN; 1110 else 1111 error = EDESTADDRREQ; 1112 SOCKBUF_UNLOCK(&so->so_snd); 1113 goto out; 1114 } 1115 } 1116 1117 /* 1118 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a 1119 * problem and need fixing. 1120 */ 1121 space = sbspace(&so->so_snd); 1122 if (flags & MSG_OOB) 1123 space += 1024; 1124 space -= clen; 1125 SOCKBUF_UNLOCK(&so->so_snd); 1126 if (resid > space) { 1127 error = EMSGSIZE; 1128 goto out; 1129 } 1130 if (uio == NULL) { 1131 resid = 0; 1132 if (flags & MSG_EOR) 1133 top->m_flags |= M_EOR; 1134 } else { 1135 /* 1136 * Copy the data from userland into a mbuf chain. 1137 * If no data is to be copied in, a single empty mbuf 1138 * is returned. 1139 */ 1140 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr, 1141 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0))); 1142 if (top == NULL) { 1143 error = EFAULT; /* only possible error */ 1144 goto out; 1145 } 1146 space -= resid - uio->uio_resid; 1147 resid = uio->uio_resid; 1148 } 1149 KASSERT(resid == 0, ("sosend_dgram: resid != 0")); 1150 /* 1151 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock 1152 * than with. 1153 */ 1154 if (dontroute) { 1155 SOCK_LOCK(so); 1156 so->so_options |= SO_DONTROUTE; 1157 SOCK_UNLOCK(so); 1158 } 1159 /* 1160 * XXX all the SBS_CANTSENDMORE checks previously done could be out 1161 * of date. We could have recieved a reset packet in an interrupt or 1162 * maybe we slept while doing page faults in uiomove() etc. We could 1163 * probably recheck again inside the locking protection here, but 1164 * there are probably other places that this also happens. We must 1165 * rethink this. 1166 */ 1167 VNET_SO_ASSERT(so); 1168 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 1169 (flags & MSG_OOB) ? PRUS_OOB : 1170 /* 1171 * If the user set MSG_EOF, the protocol understands this flag and 1172 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND. 1173 */ 1174 ((flags & MSG_EOF) && 1175 (so->so_proto->pr_flags & PR_IMPLOPCL) && 1176 (resid <= 0)) ? 1177 PRUS_EOF : 1178 /* If there is more to send set PRUS_MORETOCOME */ 1179 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 1180 top, addr, control, td); 1181 if (dontroute) { 1182 SOCK_LOCK(so); 1183 so->so_options &= ~SO_DONTROUTE; 1184 SOCK_UNLOCK(so); 1185 } 1186 clen = 0; 1187 control = NULL; 1188 top = NULL; 1189 out: 1190 if (top != NULL) 1191 m_freem(top); 1192 if (control != NULL) 1193 m_freem(control); 1194 return (error); 1195 } 1196 1197 /* 1198 * Send on a socket. If send must go all at once and message is larger than 1199 * send buffering, then hard error. Lock against other senders. If must go 1200 * all at once and not enough room now, then inform user that this would 1201 * block and do nothing. Otherwise, if nonblocking, send as much as 1202 * possible. The data to be sent is described by "uio" if nonzero, otherwise 1203 * by the mbuf chain "top" (which must be null if uio is not). Data provided 1204 * in mbuf chain must be small enough to send all at once. 1205 * 1206 * Returns nonzero on error, timeout or signal; callers must check for short 1207 * counts if EINTR/ERESTART are returned. Data and control buffers are freed 1208 * on return. 1209 */ 1210 int 1211 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio, 1212 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 1213 { 1214 long space; 1215 ssize_t resid; 1216 int clen = 0, error, dontroute; 1217 int atomic = sosendallatonce(so) || top; 1218 1219 if (uio != NULL) 1220 resid = uio->uio_resid; 1221 else 1222 resid = top->m_pkthdr.len; 1223 /* 1224 * In theory resid should be unsigned. However, space must be 1225 * signed, as it might be less than 0 if we over-committed, and we 1226 * must use a signed comparison of space and resid. On the other 1227 * hand, a negative resid causes us to loop sending 0-length 1228 * segments to the protocol. 1229 * 1230 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 1231 * type sockets since that's an error. 1232 */ 1233 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) { 1234 error = EINVAL; 1235 goto out; 1236 } 1237 1238 dontroute = 1239 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 1240 (so->so_proto->pr_flags & PR_ATOMIC); 1241 if (td != NULL) 1242 td->td_ru.ru_msgsnd++; 1243 if (control != NULL) 1244 clen = control->m_len; 1245 1246 error = sblock(&so->so_snd, SBLOCKWAIT(flags)); 1247 if (error) 1248 goto out; 1249 1250 restart: 1251 do { 1252 SOCKBUF_LOCK(&so->so_snd); 1253 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 1254 SOCKBUF_UNLOCK(&so->so_snd); 1255 error = EPIPE; 1256 goto release; 1257 } 1258 if (so->so_error) { 1259 error = so->so_error; 1260 so->so_error = 0; 1261 SOCKBUF_UNLOCK(&so->so_snd); 1262 goto release; 1263 } 1264 if ((so->so_state & SS_ISCONNECTED) == 0) { 1265 /* 1266 * `sendto' and `sendmsg' is allowed on a connection- 1267 * based socket if it supports implied connect. 1268 * Return ENOTCONN if not connected and no address is 1269 * supplied. 1270 */ 1271 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 1272 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 1273 if ((so->so_state & SS_ISCONFIRMING) == 0 && 1274 !(resid == 0 && clen != 0)) { 1275 SOCKBUF_UNLOCK(&so->so_snd); 1276 error = ENOTCONN; 1277 goto release; 1278 } 1279 } else if (addr == NULL) { 1280 SOCKBUF_UNLOCK(&so->so_snd); 1281 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 1282 error = ENOTCONN; 1283 else 1284 error = EDESTADDRREQ; 1285 goto release; 1286 } 1287 } 1288 space = sbspace(&so->so_snd); 1289 if (flags & MSG_OOB) 1290 space += 1024; 1291 if ((atomic && resid > so->so_snd.sb_hiwat) || 1292 clen > so->so_snd.sb_hiwat) { 1293 SOCKBUF_UNLOCK(&so->so_snd); 1294 error = EMSGSIZE; 1295 goto release; 1296 } 1297 if (space < resid + clen && 1298 (atomic || space < so->so_snd.sb_lowat || space < clen)) { 1299 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) { 1300 SOCKBUF_UNLOCK(&so->so_snd); 1301 error = EWOULDBLOCK; 1302 goto release; 1303 } 1304 error = sbwait(&so->so_snd); 1305 SOCKBUF_UNLOCK(&so->so_snd); 1306 if (error) 1307 goto release; 1308 goto restart; 1309 } 1310 SOCKBUF_UNLOCK(&so->so_snd); 1311 space -= clen; 1312 do { 1313 if (uio == NULL) { 1314 resid = 0; 1315 if (flags & MSG_EOR) 1316 top->m_flags |= M_EOR; 1317 } else { 1318 /* 1319 * Copy the data from userland into a mbuf 1320 * chain. If no data is to be copied in, 1321 * a single empty mbuf is returned. 1322 */ 1323 top = m_uiotombuf(uio, M_WAITOK, space, 1324 (atomic ? max_hdr : 0), 1325 (atomic ? M_PKTHDR : 0) | 1326 ((flags & MSG_EOR) ? M_EOR : 0)); 1327 if (top == NULL) { 1328 error = EFAULT; /* only possible error */ 1329 goto release; 1330 } 1331 space -= resid - uio->uio_resid; 1332 resid = uio->uio_resid; 1333 } 1334 if (dontroute) { 1335 SOCK_LOCK(so); 1336 so->so_options |= SO_DONTROUTE; 1337 SOCK_UNLOCK(so); 1338 } 1339 /* 1340 * XXX all the SBS_CANTSENDMORE checks previously 1341 * done could be out of date. We could have recieved 1342 * a reset packet in an interrupt or maybe we slept 1343 * while doing page faults in uiomove() etc. We 1344 * could probably recheck again inside the locking 1345 * protection here, but there are probably other 1346 * places that this also happens. We must rethink 1347 * this. 1348 */ 1349 VNET_SO_ASSERT(so); 1350 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 1351 (flags & MSG_OOB) ? PRUS_OOB : 1352 /* 1353 * If the user set MSG_EOF, the protocol understands 1354 * this flag and nothing left to send then use 1355 * PRU_SEND_EOF instead of PRU_SEND. 1356 */ 1357 ((flags & MSG_EOF) && 1358 (so->so_proto->pr_flags & PR_IMPLOPCL) && 1359 (resid <= 0)) ? 1360 PRUS_EOF : 1361 /* If there is more to send set PRUS_MORETOCOME. */ 1362 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 1363 top, addr, control, td); 1364 if (dontroute) { 1365 SOCK_LOCK(so); 1366 so->so_options &= ~SO_DONTROUTE; 1367 SOCK_UNLOCK(so); 1368 } 1369 clen = 0; 1370 control = NULL; 1371 top = NULL; 1372 if (error) 1373 goto release; 1374 } while (resid && space > 0); 1375 } while (resid); 1376 1377 release: 1378 sbunlock(&so->so_snd); 1379 out: 1380 if (top != NULL) 1381 m_freem(top); 1382 if (control != NULL) 1383 m_freem(control); 1384 return (error); 1385 } 1386 1387 int 1388 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, 1389 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 1390 { 1391 int error; 1392 1393 CURVNET_SET(so->so_vnet); 1394 error = so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top, 1395 control, flags, td); 1396 CURVNET_RESTORE(); 1397 return (error); 1398 } 1399 1400 /* 1401 * The part of soreceive() that implements reading non-inline out-of-band 1402 * data from a socket. For more complete comments, see soreceive(), from 1403 * which this code originated. 1404 * 1405 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is 1406 * unable to return an mbuf chain to the caller. 1407 */ 1408 static int 1409 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags) 1410 { 1411 struct protosw *pr = so->so_proto; 1412 struct mbuf *m; 1413 int error; 1414 1415 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0")); 1416 VNET_SO_ASSERT(so); 1417 1418 m = m_get(M_WAITOK, MT_DATA); 1419 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK); 1420 if (error) 1421 goto bad; 1422 do { 1423 error = uiomove(mtod(m, void *), 1424 (int) min(uio->uio_resid, m->m_len), uio); 1425 m = m_free(m); 1426 } while (uio->uio_resid && error == 0 && m); 1427 bad: 1428 if (m != NULL) 1429 m_freem(m); 1430 return (error); 1431 } 1432 1433 /* 1434 * Following replacement or removal of the first mbuf on the first mbuf chain 1435 * of a socket buffer, push necessary state changes back into the socket 1436 * buffer so that other consumers see the values consistently. 'nextrecord' 1437 * is the callers locally stored value of the original value of 1438 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes. 1439 * NOTE: 'nextrecord' may be NULL. 1440 */ 1441 static __inline void 1442 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord) 1443 { 1444 1445 SOCKBUF_LOCK_ASSERT(sb); 1446 /* 1447 * First, update for the new value of nextrecord. If necessary, make 1448 * it the first record. 1449 */ 1450 if (sb->sb_mb != NULL) 1451 sb->sb_mb->m_nextpkt = nextrecord; 1452 else 1453 sb->sb_mb = nextrecord; 1454 1455 /* 1456 * Now update any dependent socket buffer fields to reflect the new 1457 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the 1458 * addition of a second clause that takes care of the case where 1459 * sb_mb has been updated, but remains the last record. 1460 */ 1461 if (sb->sb_mb == NULL) { 1462 sb->sb_mbtail = NULL; 1463 sb->sb_lastrecord = NULL; 1464 } else if (sb->sb_mb->m_nextpkt == NULL) 1465 sb->sb_lastrecord = sb->sb_mb; 1466 } 1467 1468 /* 1469 * Implement receive operations on a socket. We depend on the way that 1470 * records are added to the sockbuf by sbappend. In particular, each record 1471 * (mbufs linked through m_next) must begin with an address if the protocol 1472 * so specifies, followed by an optional mbuf or mbufs containing ancillary 1473 * data, and then zero or more mbufs of data. In order to allow parallelism 1474 * between network receive and copying to user space, as well as avoid 1475 * sleeping with a mutex held, we release the socket buffer mutex during the 1476 * user space copy. Although the sockbuf is locked, new data may still be 1477 * appended, and thus we must maintain consistency of the sockbuf during that 1478 * time. 1479 * 1480 * The caller may receive the data as a single mbuf chain by supplying an 1481 * mbuf **mp0 for use in returning the chain. The uio is then used only for 1482 * the count in uio_resid. 1483 */ 1484 int 1485 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio, 1486 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 1487 { 1488 struct mbuf *m, **mp; 1489 int flags, error, offset; 1490 ssize_t len; 1491 struct protosw *pr = so->so_proto; 1492 struct mbuf *nextrecord; 1493 int moff, type = 0; 1494 ssize_t orig_resid = uio->uio_resid; 1495 1496 mp = mp0; 1497 if (psa != NULL) 1498 *psa = NULL; 1499 if (controlp != NULL) 1500 *controlp = NULL; 1501 if (flagsp != NULL) 1502 flags = *flagsp &~ MSG_EOR; 1503 else 1504 flags = 0; 1505 if (flags & MSG_OOB) 1506 return (soreceive_rcvoob(so, uio, flags)); 1507 if (mp != NULL) 1508 *mp = NULL; 1509 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING) 1510 && uio->uio_resid) { 1511 VNET_SO_ASSERT(so); 1512 (*pr->pr_usrreqs->pru_rcvd)(so, 0); 1513 } 1514 1515 error = sblock(&so->so_rcv, SBLOCKWAIT(flags)); 1516 if (error) 1517 return (error); 1518 1519 restart: 1520 SOCKBUF_LOCK(&so->so_rcv); 1521 m = so->so_rcv.sb_mb; 1522 /* 1523 * If we have less data than requested, block awaiting more (subject 1524 * to any timeout) if: 1525 * 1. the current count is less than the low water mark, or 1526 * 2. MSG_DONTWAIT is not set 1527 */ 1528 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && 1529 so->so_rcv.sb_cc < uio->uio_resid) && 1530 so->so_rcv.sb_cc < so->so_rcv.sb_lowat && 1531 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) { 1532 KASSERT(m != NULL || !so->so_rcv.sb_cc, 1533 ("receive: m == %p so->so_rcv.sb_cc == %u", 1534 m, so->so_rcv.sb_cc)); 1535 if (so->so_error) { 1536 if (m != NULL) 1537 goto dontblock; 1538 error = so->so_error; 1539 if ((flags & MSG_PEEK) == 0) 1540 so->so_error = 0; 1541 SOCKBUF_UNLOCK(&so->so_rcv); 1542 goto release; 1543 } 1544 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1545 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1546 if (m == NULL) { 1547 SOCKBUF_UNLOCK(&so->so_rcv); 1548 goto release; 1549 } else 1550 goto dontblock; 1551 } 1552 for (; m != NULL; m = m->m_next) 1553 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 1554 m = so->so_rcv.sb_mb; 1555 goto dontblock; 1556 } 1557 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 1558 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 1559 SOCKBUF_UNLOCK(&so->so_rcv); 1560 error = ENOTCONN; 1561 goto release; 1562 } 1563 if (uio->uio_resid == 0) { 1564 SOCKBUF_UNLOCK(&so->so_rcv); 1565 goto release; 1566 } 1567 if ((so->so_state & SS_NBIO) || 1568 (flags & (MSG_DONTWAIT|MSG_NBIO))) { 1569 SOCKBUF_UNLOCK(&so->so_rcv); 1570 error = EWOULDBLOCK; 1571 goto release; 1572 } 1573 SBLASTRECORDCHK(&so->so_rcv); 1574 SBLASTMBUFCHK(&so->so_rcv); 1575 error = sbwait(&so->so_rcv); 1576 SOCKBUF_UNLOCK(&so->so_rcv); 1577 if (error) 1578 goto release; 1579 goto restart; 1580 } 1581 dontblock: 1582 /* 1583 * From this point onward, we maintain 'nextrecord' as a cache of the 1584 * pointer to the next record in the socket buffer. We must keep the 1585 * various socket buffer pointers and local stack versions of the 1586 * pointers in sync, pushing out modifications before dropping the 1587 * socket buffer mutex, and re-reading them when picking it up. 1588 * 1589 * Otherwise, we will race with the network stack appending new data 1590 * or records onto the socket buffer by using inconsistent/stale 1591 * versions of the field, possibly resulting in socket buffer 1592 * corruption. 1593 * 1594 * By holding the high-level sblock(), we prevent simultaneous 1595 * readers from pulling off the front of the socket buffer. 1596 */ 1597 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1598 if (uio->uio_td) 1599 uio->uio_td->td_ru.ru_msgrcv++; 1600 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb")); 1601 SBLASTRECORDCHK(&so->so_rcv); 1602 SBLASTMBUFCHK(&so->so_rcv); 1603 nextrecord = m->m_nextpkt; 1604 if (pr->pr_flags & PR_ADDR) { 1605 KASSERT(m->m_type == MT_SONAME, 1606 ("m->m_type == %d", m->m_type)); 1607 orig_resid = 0; 1608 if (psa != NULL) 1609 *psa = sodupsockaddr(mtod(m, struct sockaddr *), 1610 M_NOWAIT); 1611 if (flags & MSG_PEEK) { 1612 m = m->m_next; 1613 } else { 1614 sbfree(&so->so_rcv, m); 1615 so->so_rcv.sb_mb = m_free(m); 1616 m = so->so_rcv.sb_mb; 1617 sockbuf_pushsync(&so->so_rcv, nextrecord); 1618 } 1619 } 1620 1621 /* 1622 * Process one or more MT_CONTROL mbufs present before any data mbufs 1623 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we 1624 * just copy the data; if !MSG_PEEK, we call into the protocol to 1625 * perform externalization (or freeing if controlp == NULL). 1626 */ 1627 if (m != NULL && m->m_type == MT_CONTROL) { 1628 struct mbuf *cm = NULL, *cmn; 1629 struct mbuf **cme = &cm; 1630 1631 do { 1632 if (flags & MSG_PEEK) { 1633 if (controlp != NULL) { 1634 *controlp = m_copy(m, 0, m->m_len); 1635 controlp = &(*controlp)->m_next; 1636 } 1637 m = m->m_next; 1638 } else { 1639 sbfree(&so->so_rcv, m); 1640 so->so_rcv.sb_mb = m->m_next; 1641 m->m_next = NULL; 1642 *cme = m; 1643 cme = &(*cme)->m_next; 1644 m = so->so_rcv.sb_mb; 1645 } 1646 } while (m != NULL && m->m_type == MT_CONTROL); 1647 if ((flags & MSG_PEEK) == 0) 1648 sockbuf_pushsync(&so->so_rcv, nextrecord); 1649 while (cm != NULL) { 1650 cmn = cm->m_next; 1651 cm->m_next = NULL; 1652 if (pr->pr_domain->dom_externalize != NULL) { 1653 SOCKBUF_UNLOCK(&so->so_rcv); 1654 VNET_SO_ASSERT(so); 1655 error = (*pr->pr_domain->dom_externalize) 1656 (cm, controlp, flags); 1657 SOCKBUF_LOCK(&so->so_rcv); 1658 } else if (controlp != NULL) 1659 *controlp = cm; 1660 else 1661 m_freem(cm); 1662 if (controlp != NULL) { 1663 orig_resid = 0; 1664 while (*controlp != NULL) 1665 controlp = &(*controlp)->m_next; 1666 } 1667 cm = cmn; 1668 } 1669 if (m != NULL) 1670 nextrecord = so->so_rcv.sb_mb->m_nextpkt; 1671 else 1672 nextrecord = so->so_rcv.sb_mb; 1673 orig_resid = 0; 1674 } 1675 if (m != NULL) { 1676 if ((flags & MSG_PEEK) == 0) { 1677 KASSERT(m->m_nextpkt == nextrecord, 1678 ("soreceive: post-control, nextrecord !sync")); 1679 if (nextrecord == NULL) { 1680 KASSERT(so->so_rcv.sb_mb == m, 1681 ("soreceive: post-control, sb_mb!=m")); 1682 KASSERT(so->so_rcv.sb_lastrecord == m, 1683 ("soreceive: post-control, lastrecord!=m")); 1684 } 1685 } 1686 type = m->m_type; 1687 if (type == MT_OOBDATA) 1688 flags |= MSG_OOB; 1689 } else { 1690 if ((flags & MSG_PEEK) == 0) { 1691 KASSERT(so->so_rcv.sb_mb == nextrecord, 1692 ("soreceive: sb_mb != nextrecord")); 1693 if (so->so_rcv.sb_mb == NULL) { 1694 KASSERT(so->so_rcv.sb_lastrecord == NULL, 1695 ("soreceive: sb_lastercord != NULL")); 1696 } 1697 } 1698 } 1699 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1700 SBLASTRECORDCHK(&so->so_rcv); 1701 SBLASTMBUFCHK(&so->so_rcv); 1702 1703 /* 1704 * Now continue to read any data mbufs off of the head of the socket 1705 * buffer until the read request is satisfied. Note that 'type' is 1706 * used to store the type of any mbuf reads that have happened so far 1707 * such that soreceive() can stop reading if the type changes, which 1708 * causes soreceive() to return only one of regular data and inline 1709 * out-of-band data in a single socket receive operation. 1710 */ 1711 moff = 0; 1712 offset = 0; 1713 while (m != NULL && uio->uio_resid > 0 && error == 0) { 1714 /* 1715 * If the type of mbuf has changed since the last mbuf 1716 * examined ('type'), end the receive operation. 1717 */ 1718 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1719 if (m->m_type == MT_OOBDATA || m->m_type == MT_CONTROL) { 1720 if (type != m->m_type) 1721 break; 1722 } else if (type == MT_OOBDATA) 1723 break; 1724 else 1725 KASSERT(m->m_type == MT_DATA, 1726 ("m->m_type == %d", m->m_type)); 1727 so->so_rcv.sb_state &= ~SBS_RCVATMARK; 1728 len = uio->uio_resid; 1729 if (so->so_oobmark && len > so->so_oobmark - offset) 1730 len = so->so_oobmark - offset; 1731 if (len > m->m_len - moff) 1732 len = m->m_len - moff; 1733 /* 1734 * If mp is set, just pass back the mbufs. Otherwise copy 1735 * them out via the uio, then free. Sockbuf must be 1736 * consistent here (points to current mbuf, it points to next 1737 * record) when we drop priority; we must note any additions 1738 * to the sockbuf when we block interrupts again. 1739 */ 1740 if (mp == NULL) { 1741 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1742 SBLASTRECORDCHK(&so->so_rcv); 1743 SBLASTMBUFCHK(&so->so_rcv); 1744 SOCKBUF_UNLOCK(&so->so_rcv); 1745 error = uiomove(mtod(m, char *) + moff, (int)len, uio); 1746 SOCKBUF_LOCK(&so->so_rcv); 1747 if (error) { 1748 /* 1749 * The MT_SONAME mbuf has already been removed 1750 * from the record, so it is necessary to 1751 * remove the data mbufs, if any, to preserve 1752 * the invariant in the case of PR_ADDR that 1753 * requires MT_SONAME mbufs at the head of 1754 * each record. 1755 */ 1756 if (m && pr->pr_flags & PR_ATOMIC && 1757 ((flags & MSG_PEEK) == 0)) 1758 (void)sbdroprecord_locked(&so->so_rcv); 1759 SOCKBUF_UNLOCK(&so->so_rcv); 1760 goto release; 1761 } 1762 } else 1763 uio->uio_resid -= len; 1764 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1765 if (len == m->m_len - moff) { 1766 if (m->m_flags & M_EOR) 1767 flags |= MSG_EOR; 1768 if (flags & MSG_PEEK) { 1769 m = m->m_next; 1770 moff = 0; 1771 } else { 1772 nextrecord = m->m_nextpkt; 1773 sbfree(&so->so_rcv, m); 1774 if (mp != NULL) { 1775 m->m_nextpkt = NULL; 1776 *mp = m; 1777 mp = &m->m_next; 1778 so->so_rcv.sb_mb = m = m->m_next; 1779 *mp = NULL; 1780 } else { 1781 so->so_rcv.sb_mb = m_free(m); 1782 m = so->so_rcv.sb_mb; 1783 } 1784 sockbuf_pushsync(&so->so_rcv, nextrecord); 1785 SBLASTRECORDCHK(&so->so_rcv); 1786 SBLASTMBUFCHK(&so->so_rcv); 1787 } 1788 } else { 1789 if (flags & MSG_PEEK) 1790 moff += len; 1791 else { 1792 if (mp != NULL) { 1793 if (flags & MSG_DONTWAIT) { 1794 *mp = m_copym(m, 0, len, 1795 M_NOWAIT); 1796 if (*mp == NULL) { 1797 /* 1798 * m_copym() couldn't 1799 * allocate an mbuf. 1800 * Adjust uio_resid back 1801 * (it was adjusted 1802 * down by len bytes, 1803 * which we didn't end 1804 * up "copying" over). 1805 */ 1806 uio->uio_resid += len; 1807 break; 1808 } 1809 } else { 1810 SOCKBUF_UNLOCK(&so->so_rcv); 1811 *mp = m_copym(m, 0, len, 1812 M_WAITOK); 1813 SOCKBUF_LOCK(&so->so_rcv); 1814 } 1815 } 1816 m->m_data += len; 1817 m->m_len -= len; 1818 so->so_rcv.sb_cc -= len; 1819 } 1820 } 1821 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1822 if (so->so_oobmark) { 1823 if ((flags & MSG_PEEK) == 0) { 1824 so->so_oobmark -= len; 1825 if (so->so_oobmark == 0) { 1826 so->so_rcv.sb_state |= SBS_RCVATMARK; 1827 break; 1828 } 1829 } else { 1830 offset += len; 1831 if (offset == so->so_oobmark) 1832 break; 1833 } 1834 } 1835 if (flags & MSG_EOR) 1836 break; 1837 /* 1838 * If the MSG_WAITALL flag is set (for non-atomic socket), we 1839 * must not quit until "uio->uio_resid == 0" or an error 1840 * termination. If a signal/timeout occurs, return with a 1841 * short count but without error. Keep sockbuf locked 1842 * against other readers. 1843 */ 1844 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 && 1845 !sosendallatonce(so) && nextrecord == NULL) { 1846 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1847 if (so->so_error || 1848 so->so_rcv.sb_state & SBS_CANTRCVMORE) 1849 break; 1850 /* 1851 * Notify the protocol that some data has been 1852 * drained before blocking. 1853 */ 1854 if (pr->pr_flags & PR_WANTRCVD) { 1855 SOCKBUF_UNLOCK(&so->so_rcv); 1856 VNET_SO_ASSERT(so); 1857 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1858 SOCKBUF_LOCK(&so->so_rcv); 1859 } 1860 SBLASTRECORDCHK(&so->so_rcv); 1861 SBLASTMBUFCHK(&so->so_rcv); 1862 /* 1863 * We could receive some data while was notifying 1864 * the protocol. Skip blocking in this case. 1865 */ 1866 if (so->so_rcv.sb_mb == NULL) { 1867 error = sbwait(&so->so_rcv); 1868 if (error) { 1869 SOCKBUF_UNLOCK(&so->so_rcv); 1870 goto release; 1871 } 1872 } 1873 m = so->so_rcv.sb_mb; 1874 if (m != NULL) 1875 nextrecord = m->m_nextpkt; 1876 } 1877 } 1878 1879 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1880 if (m != NULL && pr->pr_flags & PR_ATOMIC) { 1881 flags |= MSG_TRUNC; 1882 if ((flags & MSG_PEEK) == 0) 1883 (void) sbdroprecord_locked(&so->so_rcv); 1884 } 1885 if ((flags & MSG_PEEK) == 0) { 1886 if (m == NULL) { 1887 /* 1888 * First part is an inline SB_EMPTY_FIXUP(). Second 1889 * part makes sure sb_lastrecord is up-to-date if 1890 * there is still data in the socket buffer. 1891 */ 1892 so->so_rcv.sb_mb = nextrecord; 1893 if (so->so_rcv.sb_mb == NULL) { 1894 so->so_rcv.sb_mbtail = NULL; 1895 so->so_rcv.sb_lastrecord = NULL; 1896 } else if (nextrecord->m_nextpkt == NULL) 1897 so->so_rcv.sb_lastrecord = nextrecord; 1898 } 1899 SBLASTRECORDCHK(&so->so_rcv); 1900 SBLASTMBUFCHK(&so->so_rcv); 1901 /* 1902 * If soreceive() is being done from the socket callback, 1903 * then don't need to generate ACK to peer to update window, 1904 * since ACK will be generated on return to TCP. 1905 */ 1906 if (!(flags & MSG_SOCALLBCK) && 1907 (pr->pr_flags & PR_WANTRCVD)) { 1908 SOCKBUF_UNLOCK(&so->so_rcv); 1909 VNET_SO_ASSERT(so); 1910 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1911 SOCKBUF_LOCK(&so->so_rcv); 1912 } 1913 } 1914 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1915 if (orig_resid == uio->uio_resid && orig_resid && 1916 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) { 1917 SOCKBUF_UNLOCK(&so->so_rcv); 1918 goto restart; 1919 } 1920 SOCKBUF_UNLOCK(&so->so_rcv); 1921 1922 if (flagsp != NULL) 1923 *flagsp |= flags; 1924 release: 1925 sbunlock(&so->so_rcv); 1926 return (error); 1927 } 1928 1929 /* 1930 * Optimized version of soreceive() for stream (TCP) sockets. 1931 * XXXAO: (MSG_WAITALL | MSG_PEEK) isn't properly handled. 1932 */ 1933 int 1934 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio, 1935 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 1936 { 1937 int len = 0, error = 0, flags, oresid; 1938 struct sockbuf *sb; 1939 struct mbuf *m, *n = NULL; 1940 1941 /* We only do stream sockets. */ 1942 if (so->so_type != SOCK_STREAM) 1943 return (EINVAL); 1944 if (psa != NULL) 1945 *psa = NULL; 1946 if (controlp != NULL) 1947 return (EINVAL); 1948 if (flagsp != NULL) 1949 flags = *flagsp &~ MSG_EOR; 1950 else 1951 flags = 0; 1952 if (flags & MSG_OOB) 1953 return (soreceive_rcvoob(so, uio, flags)); 1954 if (mp0 != NULL) 1955 *mp0 = NULL; 1956 1957 sb = &so->so_rcv; 1958 1959 /* Prevent other readers from entering the socket. */ 1960 error = sblock(sb, SBLOCKWAIT(flags)); 1961 if (error) 1962 goto out; 1963 SOCKBUF_LOCK(sb); 1964 1965 /* Easy one, no space to copyout anything. */ 1966 if (uio->uio_resid == 0) { 1967 error = EINVAL; 1968 goto out; 1969 } 1970 oresid = uio->uio_resid; 1971 1972 /* We will never ever get anything unless we are or were connected. */ 1973 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) { 1974 error = ENOTCONN; 1975 goto out; 1976 } 1977 1978 restart: 1979 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1980 1981 /* Abort if socket has reported problems. */ 1982 if (so->so_error) { 1983 if (sb->sb_cc > 0) 1984 goto deliver; 1985 if (oresid > uio->uio_resid) 1986 goto out; 1987 error = so->so_error; 1988 if (!(flags & MSG_PEEK)) 1989 so->so_error = 0; 1990 goto out; 1991 } 1992 1993 /* Door is closed. Deliver what is left, if any. */ 1994 if (sb->sb_state & SBS_CANTRCVMORE) { 1995 if (sb->sb_cc > 0) 1996 goto deliver; 1997 else 1998 goto out; 1999 } 2000 2001 /* Socket buffer is empty and we shall not block. */ 2002 if (sb->sb_cc == 0 && 2003 ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) { 2004 error = EAGAIN; 2005 goto out; 2006 } 2007 2008 /* Socket buffer got some data that we shall deliver now. */ 2009 if (sb->sb_cc > 0 && !(flags & MSG_WAITALL) && 2010 ((sb->sb_flags & SS_NBIO) || 2011 (flags & (MSG_DONTWAIT|MSG_NBIO)) || 2012 sb->sb_cc >= sb->sb_lowat || 2013 sb->sb_cc >= uio->uio_resid || 2014 sb->sb_cc >= sb->sb_hiwat) ) { 2015 goto deliver; 2016 } 2017 2018 /* On MSG_WAITALL we must wait until all data or error arrives. */ 2019 if ((flags & MSG_WAITALL) && 2020 (sb->sb_cc >= uio->uio_resid || sb->sb_cc >= sb->sb_hiwat)) 2021 goto deliver; 2022 2023 /* 2024 * Wait and block until (more) data comes in. 2025 * NB: Drops the sockbuf lock during wait. 2026 */ 2027 error = sbwait(sb); 2028 if (error) 2029 goto out; 2030 goto restart; 2031 2032 deliver: 2033 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2034 KASSERT(sb->sb_cc > 0, ("%s: sockbuf empty", __func__)); 2035 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__)); 2036 2037 /* Statistics. */ 2038 if (uio->uio_td) 2039 uio->uio_td->td_ru.ru_msgrcv++; 2040 2041 /* Fill uio until full or current end of socket buffer is reached. */ 2042 len = min(uio->uio_resid, sb->sb_cc); 2043 if (mp0 != NULL) { 2044 /* Dequeue as many mbufs as possible. */ 2045 if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) { 2046 if (*mp0 == NULL) 2047 *mp0 = sb->sb_mb; 2048 else 2049 m_cat(*mp0, sb->sb_mb); 2050 for (m = sb->sb_mb; 2051 m != NULL && m->m_len <= len; 2052 m = m->m_next) { 2053 len -= m->m_len; 2054 uio->uio_resid -= m->m_len; 2055 sbfree(sb, m); 2056 n = m; 2057 } 2058 n->m_next = NULL; 2059 sb->sb_mb = m; 2060 sb->sb_lastrecord = sb->sb_mb; 2061 if (sb->sb_mb == NULL) 2062 SB_EMPTY_FIXUP(sb); 2063 } 2064 /* Copy the remainder. */ 2065 if (len > 0) { 2066 KASSERT(sb->sb_mb != NULL, 2067 ("%s: len > 0 && sb->sb_mb empty", __func__)); 2068 2069 m = m_copym(sb->sb_mb, 0, len, M_NOWAIT); 2070 if (m == NULL) 2071 len = 0; /* Don't flush data from sockbuf. */ 2072 else 2073 uio->uio_resid -= len; 2074 if (*mp0 != NULL) 2075 m_cat(*mp0, m); 2076 else 2077 *mp0 = m; 2078 if (*mp0 == NULL) { 2079 error = ENOBUFS; 2080 goto out; 2081 } 2082 } 2083 } else { 2084 /* NB: Must unlock socket buffer as uiomove may sleep. */ 2085 SOCKBUF_UNLOCK(sb); 2086 error = m_mbuftouio(uio, sb->sb_mb, len); 2087 SOCKBUF_LOCK(sb); 2088 if (error) 2089 goto out; 2090 } 2091 SBLASTRECORDCHK(sb); 2092 SBLASTMBUFCHK(sb); 2093 2094 /* 2095 * Remove the delivered data from the socket buffer unless we 2096 * were only peeking. 2097 */ 2098 if (!(flags & MSG_PEEK)) { 2099 if (len > 0) 2100 sbdrop_locked(sb, len); 2101 2102 /* Notify protocol that we drained some data. */ 2103 if ((so->so_proto->pr_flags & PR_WANTRCVD) && 2104 (((flags & MSG_WAITALL) && uio->uio_resid > 0) || 2105 !(flags & MSG_SOCALLBCK))) { 2106 SOCKBUF_UNLOCK(sb); 2107 VNET_SO_ASSERT(so); 2108 (*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags); 2109 SOCKBUF_LOCK(sb); 2110 } 2111 } 2112 2113 /* 2114 * For MSG_WAITALL we may have to loop again and wait for 2115 * more data to come in. 2116 */ 2117 if ((flags & MSG_WAITALL) && uio->uio_resid > 0) 2118 goto restart; 2119 out: 2120 SOCKBUF_LOCK_ASSERT(sb); 2121 SBLASTRECORDCHK(sb); 2122 SBLASTMBUFCHK(sb); 2123 SOCKBUF_UNLOCK(sb); 2124 sbunlock(sb); 2125 return (error); 2126 } 2127 2128 /* 2129 * Optimized version of soreceive() for simple datagram cases from userspace. 2130 * Unlike in the stream case, we're able to drop a datagram if copyout() 2131 * fails, and because we handle datagrams atomically, we don't need to use a 2132 * sleep lock to prevent I/O interlacing. 2133 */ 2134 int 2135 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio, 2136 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 2137 { 2138 struct mbuf *m, *m2; 2139 int flags, error; 2140 ssize_t len; 2141 struct protosw *pr = so->so_proto; 2142 struct mbuf *nextrecord; 2143 2144 if (psa != NULL) 2145 *psa = NULL; 2146 if (controlp != NULL) 2147 *controlp = NULL; 2148 if (flagsp != NULL) 2149 flags = *flagsp &~ MSG_EOR; 2150 else 2151 flags = 0; 2152 2153 /* 2154 * For any complicated cases, fall back to the full 2155 * soreceive_generic(). 2156 */ 2157 if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB)) 2158 return (soreceive_generic(so, psa, uio, mp0, controlp, 2159 flagsp)); 2160 2161 /* 2162 * Enforce restrictions on use. 2163 */ 2164 KASSERT((pr->pr_flags & PR_WANTRCVD) == 0, 2165 ("soreceive_dgram: wantrcvd")); 2166 KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic")); 2167 KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0, 2168 ("soreceive_dgram: SBS_RCVATMARK")); 2169 KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0, 2170 ("soreceive_dgram: P_CONNREQUIRED")); 2171 2172 /* 2173 * Loop blocking while waiting for a datagram. 2174 */ 2175 SOCKBUF_LOCK(&so->so_rcv); 2176 while ((m = so->so_rcv.sb_mb) == NULL) { 2177 KASSERT(so->so_rcv.sb_cc == 0, 2178 ("soreceive_dgram: sb_mb NULL but sb_cc %u", 2179 so->so_rcv.sb_cc)); 2180 if (so->so_error) { 2181 error = so->so_error; 2182 so->so_error = 0; 2183 SOCKBUF_UNLOCK(&so->so_rcv); 2184 return (error); 2185 } 2186 if (so->so_rcv.sb_state & SBS_CANTRCVMORE || 2187 uio->uio_resid == 0) { 2188 SOCKBUF_UNLOCK(&so->so_rcv); 2189 return (0); 2190 } 2191 if ((so->so_state & SS_NBIO) || 2192 (flags & (MSG_DONTWAIT|MSG_NBIO))) { 2193 SOCKBUF_UNLOCK(&so->so_rcv); 2194 return (EWOULDBLOCK); 2195 } 2196 SBLASTRECORDCHK(&so->so_rcv); 2197 SBLASTMBUFCHK(&so->so_rcv); 2198 error = sbwait(&so->so_rcv); 2199 if (error) { 2200 SOCKBUF_UNLOCK(&so->so_rcv); 2201 return (error); 2202 } 2203 } 2204 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2205 2206 if (uio->uio_td) 2207 uio->uio_td->td_ru.ru_msgrcv++; 2208 SBLASTRECORDCHK(&so->so_rcv); 2209 SBLASTMBUFCHK(&so->so_rcv); 2210 nextrecord = m->m_nextpkt; 2211 if (nextrecord == NULL) { 2212 KASSERT(so->so_rcv.sb_lastrecord == m, 2213 ("soreceive_dgram: lastrecord != m")); 2214 } 2215 2216 KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord, 2217 ("soreceive_dgram: m_nextpkt != nextrecord")); 2218 2219 /* 2220 * Pull 'm' and its chain off the front of the packet queue. 2221 */ 2222 so->so_rcv.sb_mb = NULL; 2223 sockbuf_pushsync(&so->so_rcv, nextrecord); 2224 2225 /* 2226 * Walk 'm's chain and free that many bytes from the socket buffer. 2227 */ 2228 for (m2 = m; m2 != NULL; m2 = m2->m_next) 2229 sbfree(&so->so_rcv, m2); 2230 2231 /* 2232 * Do a few last checks before we let go of the lock. 2233 */ 2234 SBLASTRECORDCHK(&so->so_rcv); 2235 SBLASTMBUFCHK(&so->so_rcv); 2236 SOCKBUF_UNLOCK(&so->so_rcv); 2237 2238 if (pr->pr_flags & PR_ADDR) { 2239 KASSERT(m->m_type == MT_SONAME, 2240 ("m->m_type == %d", m->m_type)); 2241 if (psa != NULL) 2242 *psa = sodupsockaddr(mtod(m, struct sockaddr *), 2243 M_NOWAIT); 2244 m = m_free(m); 2245 } 2246 if (m == NULL) { 2247 /* XXXRW: Can this happen? */ 2248 return (0); 2249 } 2250 2251 /* 2252 * Packet to copyout() is now in 'm' and it is disconnected from the 2253 * queue. 2254 * 2255 * Process one or more MT_CONTROL mbufs present before any data mbufs 2256 * in the first mbuf chain on the socket buffer. We call into the 2257 * protocol to perform externalization (or freeing if controlp == 2258 * NULL). 2259 */ 2260 if (m->m_type == MT_CONTROL) { 2261 struct mbuf *cm = NULL, *cmn; 2262 struct mbuf **cme = &cm; 2263 2264 do { 2265 m2 = m->m_next; 2266 m->m_next = NULL; 2267 *cme = m; 2268 cme = &(*cme)->m_next; 2269 m = m2; 2270 } while (m != NULL && m->m_type == MT_CONTROL); 2271 while (cm != NULL) { 2272 cmn = cm->m_next; 2273 cm->m_next = NULL; 2274 if (pr->pr_domain->dom_externalize != NULL) { 2275 error = (*pr->pr_domain->dom_externalize) 2276 (cm, controlp, flags); 2277 } else if (controlp != NULL) 2278 *controlp = cm; 2279 else 2280 m_freem(cm); 2281 if (controlp != NULL) { 2282 while (*controlp != NULL) 2283 controlp = &(*controlp)->m_next; 2284 } 2285 cm = cmn; 2286 } 2287 } 2288 KASSERT(m->m_type == MT_DATA, ("soreceive_dgram: !data")); 2289 2290 while (m != NULL && uio->uio_resid > 0) { 2291 len = uio->uio_resid; 2292 if (len > m->m_len) 2293 len = m->m_len; 2294 error = uiomove(mtod(m, char *), (int)len, uio); 2295 if (error) { 2296 m_freem(m); 2297 return (error); 2298 } 2299 if (len == m->m_len) 2300 m = m_free(m); 2301 else { 2302 m->m_data += len; 2303 m->m_len -= len; 2304 } 2305 } 2306 if (m != NULL) 2307 flags |= MSG_TRUNC; 2308 m_freem(m); 2309 if (flagsp != NULL) 2310 *flagsp |= flags; 2311 return (0); 2312 } 2313 2314 int 2315 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio, 2316 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 2317 { 2318 int error; 2319 2320 CURVNET_SET(so->so_vnet); 2321 error = (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0, 2322 controlp, flagsp)); 2323 CURVNET_RESTORE(); 2324 return (error); 2325 } 2326 2327 int 2328 soshutdown(struct socket *so, int how) 2329 { 2330 struct protosw *pr = so->so_proto; 2331 int error; 2332 2333 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) 2334 return (EINVAL); 2335 2336 CURVNET_SET(so->so_vnet); 2337 if (pr->pr_usrreqs->pru_flush != NULL) 2338 (*pr->pr_usrreqs->pru_flush)(so, how); 2339 if (how != SHUT_WR) 2340 sorflush(so); 2341 if (how != SHUT_RD) { 2342 error = (*pr->pr_usrreqs->pru_shutdown)(so); 2343 wakeup(&so->so_timeo); 2344 CURVNET_RESTORE(); 2345 return (error); 2346 } 2347 wakeup(&so->so_timeo); 2348 CURVNET_RESTORE(); 2349 return (0); 2350 } 2351 2352 void 2353 sorflush(struct socket *so) 2354 { 2355 struct sockbuf *sb = &so->so_rcv; 2356 struct protosw *pr = so->so_proto; 2357 struct sockbuf asb; 2358 2359 VNET_SO_ASSERT(so); 2360 2361 /* 2362 * In order to avoid calling dom_dispose with the socket buffer mutex 2363 * held, and in order to generally avoid holding the lock for a long 2364 * time, we make a copy of the socket buffer and clear the original 2365 * (except locks, state). The new socket buffer copy won't have 2366 * initialized locks so we can only call routines that won't use or 2367 * assert those locks. 2368 * 2369 * Dislodge threads currently blocked in receive and wait to acquire 2370 * a lock against other simultaneous readers before clearing the 2371 * socket buffer. Don't let our acquire be interrupted by a signal 2372 * despite any existing socket disposition on interruptable waiting. 2373 */ 2374 socantrcvmore(so); 2375 (void) sblock(sb, SBL_WAIT | SBL_NOINTR); 2376 2377 /* 2378 * Invalidate/clear most of the sockbuf structure, but leave selinfo 2379 * and mutex data unchanged. 2380 */ 2381 SOCKBUF_LOCK(sb); 2382 bzero(&asb, offsetof(struct sockbuf, sb_startzero)); 2383 bcopy(&sb->sb_startzero, &asb.sb_startzero, 2384 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero)); 2385 bzero(&sb->sb_startzero, 2386 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero)); 2387 SOCKBUF_UNLOCK(sb); 2388 sbunlock(sb); 2389 2390 /* 2391 * Dispose of special rights and flush the socket buffer. Don't call 2392 * any unsafe routines (that rely on locks being initialized) on asb. 2393 */ 2394 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL) 2395 (*pr->pr_domain->dom_dispose)(asb.sb_mb); 2396 sbrelease_internal(&asb, so); 2397 } 2398 2399 /* 2400 * Wrapper for Socket established helper hook. 2401 * Parameters: socket, context of the hook point, hook id. 2402 */ 2403 static int inline 2404 hhook_run_socket(struct socket *so, void *hctx, int32_t h_id) 2405 { 2406 struct socket_hhook_data hhook_data = { 2407 .so = so, 2408 .hctx = hctx, 2409 .m = NULL 2410 }; 2411 2412 hhook_run_hooks(V_socket_hhh[h_id], &hhook_data, &so->osd); 2413 2414 /* Ugly but needed, since hhooks return void for now */ 2415 return (hhook_data.status); 2416 } 2417 2418 /* 2419 * Perhaps this routine, and sooptcopyout(), below, ought to come in an 2420 * additional variant to handle the case where the option value needs to be 2421 * some kind of integer, but not a specific size. In addition to their use 2422 * here, these functions are also called by the protocol-level pr_ctloutput() 2423 * routines. 2424 */ 2425 int 2426 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen) 2427 { 2428 size_t valsize; 2429 2430 /* 2431 * If the user gives us more than we wanted, we ignore it, but if we 2432 * don't get the minimum length the caller wants, we return EINVAL. 2433 * On success, sopt->sopt_valsize is set to however much we actually 2434 * retrieved. 2435 */ 2436 if ((valsize = sopt->sopt_valsize) < minlen) 2437 return EINVAL; 2438 if (valsize > len) 2439 sopt->sopt_valsize = valsize = len; 2440 2441 if (sopt->sopt_td != NULL) 2442 return (copyin(sopt->sopt_val, buf, valsize)); 2443 2444 bcopy(sopt->sopt_val, buf, valsize); 2445 return (0); 2446 } 2447 2448 /* 2449 * Kernel version of setsockopt(2). 2450 * 2451 * XXX: optlen is size_t, not socklen_t 2452 */ 2453 int 2454 so_setsockopt(struct socket *so, int level, int optname, void *optval, 2455 size_t optlen) 2456 { 2457 struct sockopt sopt; 2458 2459 sopt.sopt_level = level; 2460 sopt.sopt_name = optname; 2461 sopt.sopt_dir = SOPT_SET; 2462 sopt.sopt_val = optval; 2463 sopt.sopt_valsize = optlen; 2464 sopt.sopt_td = NULL; 2465 return (sosetopt(so, &sopt)); 2466 } 2467 2468 int 2469 sosetopt(struct socket *so, struct sockopt *sopt) 2470 { 2471 int error, optval; 2472 struct linger l; 2473 struct timeval tv; 2474 sbintime_t val; 2475 uint32_t val32; 2476 #ifdef MAC 2477 struct mac extmac; 2478 #endif 2479 2480 CURVNET_SET(so->so_vnet); 2481 error = 0; 2482 if (sopt->sopt_level != SOL_SOCKET) { 2483 if (so->so_proto->pr_ctloutput != NULL) { 2484 error = (*so->so_proto->pr_ctloutput)(so, sopt); 2485 CURVNET_RESTORE(); 2486 return (error); 2487 } 2488 error = ENOPROTOOPT; 2489 } else { 2490 switch (sopt->sopt_name) { 2491 case SO_ACCEPTFILTER: 2492 error = do_setopt_accept_filter(so, sopt); 2493 if (error) 2494 goto bad; 2495 break; 2496 2497 case SO_LINGER: 2498 error = sooptcopyin(sopt, &l, sizeof l, sizeof l); 2499 if (error) 2500 goto bad; 2501 2502 SOCK_LOCK(so); 2503 so->so_linger = l.l_linger; 2504 if (l.l_onoff) 2505 so->so_options |= SO_LINGER; 2506 else 2507 so->so_options &= ~SO_LINGER; 2508 SOCK_UNLOCK(so); 2509 break; 2510 2511 case SO_DEBUG: 2512 case SO_KEEPALIVE: 2513 case SO_DONTROUTE: 2514 case SO_USELOOPBACK: 2515 case SO_BROADCAST: 2516 case SO_REUSEADDR: 2517 case SO_REUSEPORT: 2518 case SO_OOBINLINE: 2519 case SO_TIMESTAMP: 2520 case SO_BINTIME: 2521 case SO_NOSIGPIPE: 2522 case SO_NO_DDP: 2523 case SO_NO_OFFLOAD: 2524 error = sooptcopyin(sopt, &optval, sizeof optval, 2525 sizeof optval); 2526 if (error) 2527 goto bad; 2528 SOCK_LOCK(so); 2529 if (optval) 2530 so->so_options |= sopt->sopt_name; 2531 else 2532 so->so_options &= ~sopt->sopt_name; 2533 SOCK_UNLOCK(so); 2534 break; 2535 2536 case SO_SETFIB: 2537 error = sooptcopyin(sopt, &optval, sizeof optval, 2538 sizeof optval); 2539 if (error) 2540 goto bad; 2541 2542 if (optval < 0 || optval >= rt_numfibs) { 2543 error = EINVAL; 2544 goto bad; 2545 } 2546 if (((so->so_proto->pr_domain->dom_family == PF_INET) || 2547 (so->so_proto->pr_domain->dom_family == PF_INET6) || 2548 (so->so_proto->pr_domain->dom_family == PF_ROUTE))) 2549 so->so_fibnum = optval; 2550 else 2551 so->so_fibnum = 0; 2552 break; 2553 2554 case SO_USER_COOKIE: 2555 error = sooptcopyin(sopt, &val32, sizeof val32, 2556 sizeof val32); 2557 if (error) 2558 goto bad; 2559 so->so_user_cookie = val32; 2560 break; 2561 2562 case SO_SNDBUF: 2563 case SO_RCVBUF: 2564 case SO_SNDLOWAT: 2565 case SO_RCVLOWAT: 2566 error = sooptcopyin(sopt, &optval, sizeof optval, 2567 sizeof optval); 2568 if (error) 2569 goto bad; 2570 2571 /* 2572 * Values < 1 make no sense for any of these options, 2573 * so disallow them. 2574 */ 2575 if (optval < 1) { 2576 error = EINVAL; 2577 goto bad; 2578 } 2579 2580 switch (sopt->sopt_name) { 2581 case SO_SNDBUF: 2582 case SO_RCVBUF: 2583 if (sbreserve(sopt->sopt_name == SO_SNDBUF ? 2584 &so->so_snd : &so->so_rcv, (u_long)optval, 2585 so, curthread) == 0) { 2586 error = ENOBUFS; 2587 goto bad; 2588 } 2589 (sopt->sopt_name == SO_SNDBUF ? &so->so_snd : 2590 &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE; 2591 break; 2592 2593 /* 2594 * Make sure the low-water is never greater than the 2595 * high-water. 2596 */ 2597 case SO_SNDLOWAT: 2598 SOCKBUF_LOCK(&so->so_snd); 2599 so->so_snd.sb_lowat = 2600 (optval > so->so_snd.sb_hiwat) ? 2601 so->so_snd.sb_hiwat : optval; 2602 SOCKBUF_UNLOCK(&so->so_snd); 2603 break; 2604 case SO_RCVLOWAT: 2605 SOCKBUF_LOCK(&so->so_rcv); 2606 so->so_rcv.sb_lowat = 2607 (optval > so->so_rcv.sb_hiwat) ? 2608 so->so_rcv.sb_hiwat : optval; 2609 SOCKBUF_UNLOCK(&so->so_rcv); 2610 break; 2611 } 2612 break; 2613 2614 case SO_SNDTIMEO: 2615 case SO_RCVTIMEO: 2616 #ifdef COMPAT_FREEBSD32 2617 if (SV_CURPROC_FLAG(SV_ILP32)) { 2618 struct timeval32 tv32; 2619 2620 error = sooptcopyin(sopt, &tv32, sizeof tv32, 2621 sizeof tv32); 2622 CP(tv32, tv, tv_sec); 2623 CP(tv32, tv, tv_usec); 2624 } else 2625 #endif 2626 error = sooptcopyin(sopt, &tv, sizeof tv, 2627 sizeof tv); 2628 if (error) 2629 goto bad; 2630 if (tv.tv_sec < 0 || tv.tv_usec < 0 || 2631 tv.tv_usec >= 1000000) { 2632 error = EDOM; 2633 goto bad; 2634 } 2635 if (tv.tv_sec > INT32_MAX) 2636 val = SBT_MAX; 2637 else 2638 val = tvtosbt(tv); 2639 switch (sopt->sopt_name) { 2640 case SO_SNDTIMEO: 2641 so->so_snd.sb_timeo = val; 2642 break; 2643 case SO_RCVTIMEO: 2644 so->so_rcv.sb_timeo = val; 2645 break; 2646 } 2647 break; 2648 2649 case SO_LABEL: 2650 #ifdef MAC 2651 error = sooptcopyin(sopt, &extmac, sizeof extmac, 2652 sizeof extmac); 2653 if (error) 2654 goto bad; 2655 error = mac_setsockopt_label(sopt->sopt_td->td_ucred, 2656 so, &extmac); 2657 #else 2658 error = EOPNOTSUPP; 2659 #endif 2660 break; 2661 2662 default: 2663 if (V_socket_hhh[HHOOK_SOCKET_OPT]->hhh_nhooks > 0) 2664 error = hhook_run_socket(so, sopt, 2665 HHOOK_SOCKET_OPT); 2666 else 2667 error = ENOPROTOOPT; 2668 break; 2669 } 2670 if (error == 0 && so->so_proto->pr_ctloutput != NULL) 2671 (void)(*so->so_proto->pr_ctloutput)(so, sopt); 2672 } 2673 bad: 2674 CURVNET_RESTORE(); 2675 return (error); 2676 } 2677 2678 /* 2679 * Helper routine for getsockopt. 2680 */ 2681 int 2682 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len) 2683 { 2684 int error; 2685 size_t valsize; 2686 2687 error = 0; 2688 2689 /* 2690 * Documented get behavior is that we always return a value, possibly 2691 * truncated to fit in the user's buffer. Traditional behavior is 2692 * that we always tell the user precisely how much we copied, rather 2693 * than something useful like the total amount we had available for 2694 * her. Note that this interface is not idempotent; the entire 2695 * answer must generated ahead of time. 2696 */ 2697 valsize = min(len, sopt->sopt_valsize); 2698 sopt->sopt_valsize = valsize; 2699 if (sopt->sopt_val != NULL) { 2700 if (sopt->sopt_td != NULL) 2701 error = copyout(buf, sopt->sopt_val, valsize); 2702 else 2703 bcopy(buf, sopt->sopt_val, valsize); 2704 } 2705 return (error); 2706 } 2707 2708 int 2709 sogetopt(struct socket *so, struct sockopt *sopt) 2710 { 2711 int error, optval; 2712 struct linger l; 2713 struct timeval tv; 2714 #ifdef MAC 2715 struct mac extmac; 2716 #endif 2717 2718 CURVNET_SET(so->so_vnet); 2719 error = 0; 2720 if (sopt->sopt_level != SOL_SOCKET) { 2721 if (so->so_proto->pr_ctloutput != NULL) 2722 error = (*so->so_proto->pr_ctloutput)(so, sopt); 2723 else 2724 error = ENOPROTOOPT; 2725 CURVNET_RESTORE(); 2726 return (error); 2727 } else { 2728 switch (sopt->sopt_name) { 2729 case SO_ACCEPTFILTER: 2730 error = do_getopt_accept_filter(so, sopt); 2731 break; 2732 2733 case SO_LINGER: 2734 SOCK_LOCK(so); 2735 l.l_onoff = so->so_options & SO_LINGER; 2736 l.l_linger = so->so_linger; 2737 SOCK_UNLOCK(so); 2738 error = sooptcopyout(sopt, &l, sizeof l); 2739 break; 2740 2741 case SO_USELOOPBACK: 2742 case SO_DONTROUTE: 2743 case SO_DEBUG: 2744 case SO_KEEPALIVE: 2745 case SO_REUSEADDR: 2746 case SO_REUSEPORT: 2747 case SO_BROADCAST: 2748 case SO_OOBINLINE: 2749 case SO_ACCEPTCONN: 2750 case SO_TIMESTAMP: 2751 case SO_BINTIME: 2752 case SO_NOSIGPIPE: 2753 optval = so->so_options & sopt->sopt_name; 2754 integer: 2755 error = sooptcopyout(sopt, &optval, sizeof optval); 2756 break; 2757 2758 case SO_TYPE: 2759 optval = so->so_type; 2760 goto integer; 2761 2762 case SO_PROTOCOL: 2763 optval = so->so_proto->pr_protocol; 2764 goto integer; 2765 2766 case SO_ERROR: 2767 SOCK_LOCK(so); 2768 optval = so->so_error; 2769 so->so_error = 0; 2770 SOCK_UNLOCK(so); 2771 goto integer; 2772 2773 case SO_SNDBUF: 2774 optval = so->so_snd.sb_hiwat; 2775 goto integer; 2776 2777 case SO_RCVBUF: 2778 optval = so->so_rcv.sb_hiwat; 2779 goto integer; 2780 2781 case SO_SNDLOWAT: 2782 optval = so->so_snd.sb_lowat; 2783 goto integer; 2784 2785 case SO_RCVLOWAT: 2786 optval = so->so_rcv.sb_lowat; 2787 goto integer; 2788 2789 case SO_SNDTIMEO: 2790 case SO_RCVTIMEO: 2791 tv = sbttotv(sopt->sopt_name == SO_SNDTIMEO ? 2792 so->so_snd.sb_timeo : so->so_rcv.sb_timeo); 2793 #ifdef COMPAT_FREEBSD32 2794 if (SV_CURPROC_FLAG(SV_ILP32)) { 2795 struct timeval32 tv32; 2796 2797 CP(tv, tv32, tv_sec); 2798 CP(tv, tv32, tv_usec); 2799 error = sooptcopyout(sopt, &tv32, sizeof tv32); 2800 } else 2801 #endif 2802 error = sooptcopyout(sopt, &tv, sizeof tv); 2803 break; 2804 2805 case SO_LABEL: 2806 #ifdef MAC 2807 error = sooptcopyin(sopt, &extmac, sizeof(extmac), 2808 sizeof(extmac)); 2809 if (error) 2810 goto bad; 2811 error = mac_getsockopt_label(sopt->sopt_td->td_ucred, 2812 so, &extmac); 2813 if (error) 2814 goto bad; 2815 error = sooptcopyout(sopt, &extmac, sizeof extmac); 2816 #else 2817 error = EOPNOTSUPP; 2818 #endif 2819 break; 2820 2821 case SO_PEERLABEL: 2822 #ifdef MAC 2823 error = sooptcopyin(sopt, &extmac, sizeof(extmac), 2824 sizeof(extmac)); 2825 if (error) 2826 goto bad; 2827 error = mac_getsockopt_peerlabel( 2828 sopt->sopt_td->td_ucred, so, &extmac); 2829 if (error) 2830 goto bad; 2831 error = sooptcopyout(sopt, &extmac, sizeof extmac); 2832 #else 2833 error = EOPNOTSUPP; 2834 #endif 2835 break; 2836 2837 case SO_LISTENQLIMIT: 2838 optval = so->so_qlimit; 2839 goto integer; 2840 2841 case SO_LISTENQLEN: 2842 optval = so->so_qlen; 2843 goto integer; 2844 2845 case SO_LISTENINCQLEN: 2846 optval = so->so_incqlen; 2847 goto integer; 2848 2849 default: 2850 if (V_socket_hhh[HHOOK_SOCKET_OPT]->hhh_nhooks > 0) 2851 error = hhook_run_socket(so, sopt, 2852 HHOOK_SOCKET_OPT); 2853 else 2854 error = ENOPROTOOPT; 2855 break; 2856 } 2857 } 2858 #ifdef MAC 2859 bad: 2860 #endif 2861 CURVNET_RESTORE(); 2862 return (error); 2863 } 2864 2865 int 2866 soopt_getm(struct sockopt *sopt, struct mbuf **mp) 2867 { 2868 struct mbuf *m, *m_prev; 2869 int sopt_size = sopt->sopt_valsize; 2870 2871 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA); 2872 if (m == NULL) 2873 return ENOBUFS; 2874 if (sopt_size > MLEN) { 2875 MCLGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT); 2876 if ((m->m_flags & M_EXT) == 0) { 2877 m_free(m); 2878 return ENOBUFS; 2879 } 2880 m->m_len = min(MCLBYTES, sopt_size); 2881 } else { 2882 m->m_len = min(MLEN, sopt_size); 2883 } 2884 sopt_size -= m->m_len; 2885 *mp = m; 2886 m_prev = m; 2887 2888 while (sopt_size) { 2889 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA); 2890 if (m == NULL) { 2891 m_freem(*mp); 2892 return ENOBUFS; 2893 } 2894 if (sopt_size > MLEN) { 2895 MCLGET(m, sopt->sopt_td != NULL ? M_WAITOK : 2896 M_NOWAIT); 2897 if ((m->m_flags & M_EXT) == 0) { 2898 m_freem(m); 2899 m_freem(*mp); 2900 return ENOBUFS; 2901 } 2902 m->m_len = min(MCLBYTES, sopt_size); 2903 } else { 2904 m->m_len = min(MLEN, sopt_size); 2905 } 2906 sopt_size -= m->m_len; 2907 m_prev->m_next = m; 2908 m_prev = m; 2909 } 2910 return (0); 2911 } 2912 2913 int 2914 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m) 2915 { 2916 struct mbuf *m0 = m; 2917 2918 if (sopt->sopt_val == NULL) 2919 return (0); 2920 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 2921 if (sopt->sopt_td != NULL) { 2922 int error; 2923 2924 error = copyin(sopt->sopt_val, mtod(m, char *), 2925 m->m_len); 2926 if (error != 0) { 2927 m_freem(m0); 2928 return(error); 2929 } 2930 } else 2931 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len); 2932 sopt->sopt_valsize -= m->m_len; 2933 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len; 2934 m = m->m_next; 2935 } 2936 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */ 2937 panic("ip6_sooptmcopyin"); 2938 return (0); 2939 } 2940 2941 int 2942 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m) 2943 { 2944 struct mbuf *m0 = m; 2945 size_t valsize = 0; 2946 2947 if (sopt->sopt_val == NULL) 2948 return (0); 2949 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 2950 if (sopt->sopt_td != NULL) { 2951 int error; 2952 2953 error = copyout(mtod(m, char *), sopt->sopt_val, 2954 m->m_len); 2955 if (error != 0) { 2956 m_freem(m0); 2957 return(error); 2958 } 2959 } else 2960 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len); 2961 sopt->sopt_valsize -= m->m_len; 2962 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len; 2963 valsize += m->m_len; 2964 m = m->m_next; 2965 } 2966 if (m != NULL) { 2967 /* enough soopt buffer should be given from user-land */ 2968 m_freem(m0); 2969 return(EINVAL); 2970 } 2971 sopt->sopt_valsize = valsize; 2972 return (0); 2973 } 2974 2975 /* 2976 * sohasoutofband(): protocol notifies socket layer of the arrival of new 2977 * out-of-band data, which will then notify socket consumers. 2978 */ 2979 void 2980 sohasoutofband(struct socket *so) 2981 { 2982 2983 if (so->so_sigio != NULL) 2984 pgsigio(&so->so_sigio, SIGURG, 0); 2985 selwakeuppri(&so->so_rcv.sb_sel, PSOCK); 2986 } 2987 2988 int 2989 sopoll(struct socket *so, int events, struct ucred *active_cred, 2990 struct thread *td) 2991 { 2992 2993 /* 2994 * We do not need to set or assert curvnet as long as everyone uses 2995 * sopoll_generic(). 2996 */ 2997 return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred, 2998 td)); 2999 } 3000 3001 int 3002 sopoll_generic(struct socket *so, int events, struct ucred *active_cred, 3003 struct thread *td) 3004 { 3005 int revents = 0; 3006 3007 SOCKBUF_LOCK(&so->so_snd); 3008 SOCKBUF_LOCK(&so->so_rcv); 3009 if (events & (POLLIN | POLLRDNORM)) 3010 if (soreadabledata(so)) 3011 revents |= events & (POLLIN | POLLRDNORM); 3012 3013 if (events & (POLLOUT | POLLWRNORM)) 3014 if (sowriteable(so)) 3015 revents |= events & (POLLOUT | POLLWRNORM); 3016 3017 if (events & (POLLPRI | POLLRDBAND)) 3018 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK)) 3019 revents |= events & (POLLPRI | POLLRDBAND); 3020 3021 if ((events & POLLINIGNEOF) == 0) { 3022 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 3023 revents |= events & (POLLIN | POLLRDNORM); 3024 if (so->so_snd.sb_state & SBS_CANTSENDMORE) 3025 revents |= POLLHUP; 3026 } 3027 } 3028 3029 if (revents == 0) { 3030 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) { 3031 selrecord(td, &so->so_rcv.sb_sel); 3032 so->so_rcv.sb_flags |= SB_SEL; 3033 } 3034 3035 if (events & (POLLOUT | POLLWRNORM)) { 3036 selrecord(td, &so->so_snd.sb_sel); 3037 so->so_snd.sb_flags |= SB_SEL; 3038 } 3039 } 3040 3041 SOCKBUF_UNLOCK(&so->so_rcv); 3042 SOCKBUF_UNLOCK(&so->so_snd); 3043 return (revents); 3044 } 3045 3046 int 3047 soo_kqfilter(struct file *fp, struct knote *kn) 3048 { 3049 struct socket *so = kn->kn_fp->f_data; 3050 struct sockbuf *sb; 3051 3052 switch (kn->kn_filter) { 3053 case EVFILT_READ: 3054 if (so->so_options & SO_ACCEPTCONN) 3055 kn->kn_fop = &solisten_filtops; 3056 else 3057 kn->kn_fop = &soread_filtops; 3058 sb = &so->so_rcv; 3059 break; 3060 case EVFILT_WRITE: 3061 kn->kn_fop = &sowrite_filtops; 3062 sb = &so->so_snd; 3063 break; 3064 default: 3065 return (EINVAL); 3066 } 3067 3068 SOCKBUF_LOCK(sb); 3069 knlist_add(&sb->sb_sel.si_note, kn, 1); 3070 sb->sb_flags |= SB_KNOTE; 3071 SOCKBUF_UNLOCK(sb); 3072 return (0); 3073 } 3074 3075 /* 3076 * Some routines that return EOPNOTSUPP for entry points that are not 3077 * supported by a protocol. Fill in as needed. 3078 */ 3079 int 3080 pru_accept_notsupp(struct socket *so, struct sockaddr **nam) 3081 { 3082 3083 return EOPNOTSUPP; 3084 } 3085 3086 int 3087 pru_attach_notsupp(struct socket *so, int proto, struct thread *td) 3088 { 3089 3090 return EOPNOTSUPP; 3091 } 3092 3093 int 3094 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 3095 { 3096 3097 return EOPNOTSUPP; 3098 } 3099 3100 int 3101 pru_bindat_notsupp(int fd, struct socket *so, struct sockaddr *nam, 3102 struct thread *td) 3103 { 3104 3105 return EOPNOTSUPP; 3106 } 3107 3108 int 3109 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 3110 { 3111 3112 return EOPNOTSUPP; 3113 } 3114 3115 int 3116 pru_connectat_notsupp(int fd, struct socket *so, struct sockaddr *nam, 3117 struct thread *td) 3118 { 3119 3120 return EOPNOTSUPP; 3121 } 3122 3123 int 3124 pru_connect2_notsupp(struct socket *so1, struct socket *so2) 3125 { 3126 3127 return EOPNOTSUPP; 3128 } 3129 3130 int 3131 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, 3132 struct ifnet *ifp, struct thread *td) 3133 { 3134 3135 return EOPNOTSUPP; 3136 } 3137 3138 int 3139 pru_disconnect_notsupp(struct socket *so) 3140 { 3141 3142 return EOPNOTSUPP; 3143 } 3144 3145 int 3146 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td) 3147 { 3148 3149 return EOPNOTSUPP; 3150 } 3151 3152 int 3153 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam) 3154 { 3155 3156 return EOPNOTSUPP; 3157 } 3158 3159 int 3160 pru_rcvd_notsupp(struct socket *so, int flags) 3161 { 3162 3163 return EOPNOTSUPP; 3164 } 3165 3166 int 3167 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) 3168 { 3169 3170 return EOPNOTSUPP; 3171 } 3172 3173 int 3174 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m, 3175 struct sockaddr *addr, struct mbuf *control, struct thread *td) 3176 { 3177 3178 return EOPNOTSUPP; 3179 } 3180 3181 /* 3182 * This isn't really a ``null'' operation, but it's the default one and 3183 * doesn't do anything destructive. 3184 */ 3185 int 3186 pru_sense_null(struct socket *so, struct stat *sb) 3187 { 3188 3189 sb->st_blksize = so->so_snd.sb_hiwat; 3190 return 0; 3191 } 3192 3193 int 3194 pru_shutdown_notsupp(struct socket *so) 3195 { 3196 3197 return EOPNOTSUPP; 3198 } 3199 3200 int 3201 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam) 3202 { 3203 3204 return EOPNOTSUPP; 3205 } 3206 3207 int 3208 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio, 3209 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 3210 { 3211 3212 return EOPNOTSUPP; 3213 } 3214 3215 int 3216 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr, 3217 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 3218 { 3219 3220 return EOPNOTSUPP; 3221 } 3222 3223 int 3224 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred, 3225 struct thread *td) 3226 { 3227 3228 return EOPNOTSUPP; 3229 } 3230 3231 static void 3232 filt_sordetach(struct knote *kn) 3233 { 3234 struct socket *so = kn->kn_fp->f_data; 3235 3236 SOCKBUF_LOCK(&so->so_rcv); 3237 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1); 3238 if (knlist_empty(&so->so_rcv.sb_sel.si_note)) 3239 so->so_rcv.sb_flags &= ~SB_KNOTE; 3240 SOCKBUF_UNLOCK(&so->so_rcv); 3241 } 3242 3243 /*ARGSUSED*/ 3244 static int 3245 filt_soread(struct knote *kn, long hint) 3246 { 3247 struct socket *so; 3248 3249 so = kn->kn_fp->f_data; 3250 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 3251 3252 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl; 3253 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 3254 kn->kn_flags |= EV_EOF; 3255 kn->kn_fflags = so->so_error; 3256 return (1); 3257 } else if (so->so_error) /* temporary udp error */ 3258 return (1); 3259 3260 if (kn->kn_sfflags & NOTE_LOWAT) { 3261 if (kn->kn_data >= kn->kn_sdata) 3262 return 1; 3263 } else { 3264 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat) 3265 return 1; 3266 } 3267 3268 if (V_socket_hhh[HHOOK_FILT_SOREAD]->hhh_nhooks > 0) 3269 /* This hook returning non-zero indicates an event, not error */ 3270 return (hhook_run_socket(so, NULL, HHOOK_FILT_SOREAD)); 3271 3272 return (0); 3273 } 3274 3275 static void 3276 filt_sowdetach(struct knote *kn) 3277 { 3278 struct socket *so = kn->kn_fp->f_data; 3279 3280 SOCKBUF_LOCK(&so->so_snd); 3281 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1); 3282 if (knlist_empty(&so->so_snd.sb_sel.si_note)) 3283 so->so_snd.sb_flags &= ~SB_KNOTE; 3284 SOCKBUF_UNLOCK(&so->so_snd); 3285 } 3286 3287 /*ARGSUSED*/ 3288 static int 3289 filt_sowrite(struct knote *kn, long hint) 3290 { 3291 struct socket *so; 3292 3293 so = kn->kn_fp->f_data; 3294 SOCKBUF_LOCK_ASSERT(&so->so_snd); 3295 kn->kn_data = sbspace(&so->so_snd); 3296 3297 if (V_socket_hhh[HHOOK_FILT_SOWRITE]->hhh_nhooks > 0) 3298 hhook_run_socket(so, kn, HHOOK_FILT_SOWRITE); 3299 3300 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 3301 kn->kn_flags |= EV_EOF; 3302 kn->kn_fflags = so->so_error; 3303 return (1); 3304 } else if (so->so_error) /* temporary udp error */ 3305 return (1); 3306 else if (((so->so_state & SS_ISCONNECTED) == 0) && 3307 (so->so_proto->pr_flags & PR_CONNREQUIRED)) 3308 return (0); 3309 else if (kn->kn_sfflags & NOTE_LOWAT) 3310 return (kn->kn_data >= kn->kn_sdata); 3311 else 3312 return (kn->kn_data >= so->so_snd.sb_lowat); 3313 } 3314 3315 /*ARGSUSED*/ 3316 static int 3317 filt_solisten(struct knote *kn, long hint) 3318 { 3319 struct socket *so = kn->kn_fp->f_data; 3320 3321 kn->kn_data = so->so_qlen; 3322 return (!TAILQ_EMPTY(&so->so_comp)); 3323 } 3324 3325 int 3326 socheckuid(struct socket *so, uid_t uid) 3327 { 3328 3329 if (so == NULL) 3330 return (EPERM); 3331 if (so->so_cred->cr_uid != uid) 3332 return (EPERM); 3333 return (0); 3334 } 3335 3336 /* 3337 * These functions are used by protocols to notify the socket layer (and its 3338 * consumers) of state changes in the sockets driven by protocol-side events. 3339 */ 3340 3341 /* 3342 * Procedures to manipulate state flags of socket and do appropriate wakeups. 3343 * 3344 * Normal sequence from the active (originating) side is that 3345 * soisconnecting() is called during processing of connect() call, resulting 3346 * in an eventual call to soisconnected() if/when the connection is 3347 * established. When the connection is torn down soisdisconnecting() is 3348 * called during processing of disconnect() call, and soisdisconnected() is 3349 * called when the connection to the peer is totally severed. The semantics 3350 * of these routines are such that connectionless protocols can call 3351 * soisconnected() and soisdisconnected() only, bypassing the in-progress 3352 * calls when setting up a ``connection'' takes no time. 3353 * 3354 * From the passive side, a socket is created with two queues of sockets: 3355 * so_incomp for connections in progress and so_comp for connections already 3356 * made and awaiting user acceptance. As a protocol is preparing incoming 3357 * connections, it creates a socket structure queued on so_incomp by calling 3358 * sonewconn(). When the connection is established, soisconnected() is 3359 * called, and transfers the socket structure to so_comp, making it available 3360 * to accept(). 3361 * 3362 * If a socket is closed with sockets on either so_incomp or so_comp, these 3363 * sockets are dropped. 3364 * 3365 * If higher-level protocols are implemented in the kernel, the wakeups done 3366 * here will sometimes cause software-interrupt process scheduling. 3367 */ 3368 void 3369 soisconnecting(struct socket *so) 3370 { 3371 3372 SOCK_LOCK(so); 3373 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 3374 so->so_state |= SS_ISCONNECTING; 3375 SOCK_UNLOCK(so); 3376 } 3377 3378 void 3379 soisconnected(struct socket *so) 3380 { 3381 struct socket *head; 3382 int ret; 3383 3384 restart: 3385 ACCEPT_LOCK(); 3386 SOCK_LOCK(so); 3387 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 3388 so->so_state |= SS_ISCONNECTED; 3389 head = so->so_head; 3390 if (head != NULL && (so->so_qstate & SQ_INCOMP)) { 3391 if ((so->so_options & SO_ACCEPTFILTER) == 0) { 3392 SOCK_UNLOCK(so); 3393 TAILQ_REMOVE(&head->so_incomp, so, so_list); 3394 head->so_incqlen--; 3395 so->so_qstate &= ~SQ_INCOMP; 3396 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 3397 head->so_qlen++; 3398 so->so_qstate |= SQ_COMP; 3399 ACCEPT_UNLOCK(); 3400 sorwakeup(head); 3401 wakeup_one(&head->so_timeo); 3402 } else { 3403 ACCEPT_UNLOCK(); 3404 soupcall_set(so, SO_RCV, 3405 head->so_accf->so_accept_filter->accf_callback, 3406 head->so_accf->so_accept_filter_arg); 3407 so->so_options &= ~SO_ACCEPTFILTER; 3408 ret = head->so_accf->so_accept_filter->accf_callback(so, 3409 head->so_accf->so_accept_filter_arg, M_NOWAIT); 3410 if (ret == SU_ISCONNECTED) 3411 soupcall_clear(so, SO_RCV); 3412 SOCK_UNLOCK(so); 3413 if (ret == SU_ISCONNECTED) 3414 goto restart; 3415 } 3416 return; 3417 } 3418 SOCK_UNLOCK(so); 3419 ACCEPT_UNLOCK(); 3420 wakeup(&so->so_timeo); 3421 sorwakeup(so); 3422 sowwakeup(so); 3423 } 3424 3425 void 3426 soisdisconnecting(struct socket *so) 3427 { 3428 3429 /* 3430 * Note: This code assumes that SOCK_LOCK(so) and 3431 * SOCKBUF_LOCK(&so->so_rcv) are the same. 3432 */ 3433 SOCKBUF_LOCK(&so->so_rcv); 3434 so->so_state &= ~SS_ISCONNECTING; 3435 so->so_state |= SS_ISDISCONNECTING; 3436 so->so_rcv.sb_state |= SBS_CANTRCVMORE; 3437 sorwakeup_locked(so); 3438 SOCKBUF_LOCK(&so->so_snd); 3439 so->so_snd.sb_state |= SBS_CANTSENDMORE; 3440 sowwakeup_locked(so); 3441 wakeup(&so->so_timeo); 3442 } 3443 3444 void 3445 soisdisconnected(struct socket *so) 3446 { 3447 3448 /* 3449 * Note: This code assumes that SOCK_LOCK(so) and 3450 * SOCKBUF_LOCK(&so->so_rcv) are the same. 3451 */ 3452 SOCKBUF_LOCK(&so->so_rcv); 3453 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 3454 so->so_state |= SS_ISDISCONNECTED; 3455 so->so_rcv.sb_state |= SBS_CANTRCVMORE; 3456 sorwakeup_locked(so); 3457 SOCKBUF_LOCK(&so->so_snd); 3458 so->so_snd.sb_state |= SBS_CANTSENDMORE; 3459 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc); 3460 sowwakeup_locked(so); 3461 wakeup(&so->so_timeo); 3462 } 3463 3464 /* 3465 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. 3466 */ 3467 struct sockaddr * 3468 sodupsockaddr(const struct sockaddr *sa, int mflags) 3469 { 3470 struct sockaddr *sa2; 3471 3472 sa2 = malloc(sa->sa_len, M_SONAME, mflags); 3473 if (sa2) 3474 bcopy(sa, sa2, sa->sa_len); 3475 return sa2; 3476 } 3477 3478 /* 3479 * Register per-socket buffer upcalls. 3480 */ 3481 void 3482 soupcall_set(struct socket *so, int which, 3483 int (*func)(struct socket *, void *, int), void *arg) 3484 { 3485 struct sockbuf *sb; 3486 3487 switch (which) { 3488 case SO_RCV: 3489 sb = &so->so_rcv; 3490 break; 3491 case SO_SND: 3492 sb = &so->so_snd; 3493 break; 3494 default: 3495 panic("soupcall_set: bad which"); 3496 } 3497 SOCKBUF_LOCK_ASSERT(sb); 3498 #if 0 3499 /* XXX: accf_http actually wants to do this on purpose. */ 3500 KASSERT(sb->sb_upcall == NULL, ("soupcall_set: overwriting upcall")); 3501 #endif 3502 sb->sb_upcall = func; 3503 sb->sb_upcallarg = arg; 3504 sb->sb_flags |= SB_UPCALL; 3505 } 3506 3507 void 3508 soupcall_clear(struct socket *so, int which) 3509 { 3510 struct sockbuf *sb; 3511 3512 switch (which) { 3513 case SO_RCV: 3514 sb = &so->so_rcv; 3515 break; 3516 case SO_SND: 3517 sb = &so->so_snd; 3518 break; 3519 default: 3520 panic("soupcall_clear: bad which"); 3521 } 3522 SOCKBUF_LOCK_ASSERT(sb); 3523 KASSERT(sb->sb_upcall != NULL, ("soupcall_clear: no upcall to clear")); 3524 sb->sb_upcall = NULL; 3525 sb->sb_upcallarg = NULL; 3526 sb->sb_flags &= ~SB_UPCALL; 3527 } 3528 3529 /* 3530 * Create an external-format (``xsocket'') structure using the information in 3531 * the kernel-format socket structure pointed to by so. This is done to 3532 * reduce the spew of irrelevant information over this interface, to isolate 3533 * user code from changes in the kernel structure, and potentially to provide 3534 * information-hiding if we decide that some of this information should be 3535 * hidden from users. 3536 */ 3537 void 3538 sotoxsocket(struct socket *so, struct xsocket *xso) 3539 { 3540 3541 xso->xso_len = sizeof *xso; 3542 xso->xso_so = so; 3543 xso->so_type = so->so_type; 3544 xso->so_options = so->so_options; 3545 xso->so_linger = so->so_linger; 3546 xso->so_state = so->so_state; 3547 xso->so_pcb = so->so_pcb; 3548 xso->xso_protocol = so->so_proto->pr_protocol; 3549 xso->xso_family = so->so_proto->pr_domain->dom_family; 3550 xso->so_qlen = so->so_qlen; 3551 xso->so_incqlen = so->so_incqlen; 3552 xso->so_qlimit = so->so_qlimit; 3553 xso->so_timeo = so->so_timeo; 3554 xso->so_error = so->so_error; 3555 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0; 3556 xso->so_oobmark = so->so_oobmark; 3557 sbtoxsockbuf(&so->so_snd, &xso->so_snd); 3558 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 3559 xso->so_uid = so->so_cred->cr_uid; 3560 } 3561 3562 3563 /* 3564 * Socket accessor functions to provide external consumers with 3565 * a safe interface to socket state 3566 * 3567 */ 3568 3569 void 3570 so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *), 3571 void *arg) 3572 { 3573 3574 TAILQ_FOREACH(so, &so->so_comp, so_list) 3575 func(so, arg); 3576 } 3577 3578 struct sockbuf * 3579 so_sockbuf_rcv(struct socket *so) 3580 { 3581 3582 return (&so->so_rcv); 3583 } 3584 3585 struct sockbuf * 3586 so_sockbuf_snd(struct socket *so) 3587 { 3588 3589 return (&so->so_snd); 3590 } 3591 3592 int 3593 so_state_get(const struct socket *so) 3594 { 3595 3596 return (so->so_state); 3597 } 3598 3599 void 3600 so_state_set(struct socket *so, int val) 3601 { 3602 3603 so->so_state = val; 3604 } 3605 3606 int 3607 so_options_get(const struct socket *so) 3608 { 3609 3610 return (so->so_options); 3611 } 3612 3613 void 3614 so_options_set(struct socket *so, int val) 3615 { 3616 3617 so->so_options = val; 3618 } 3619 3620 int 3621 so_error_get(const struct socket *so) 3622 { 3623 3624 return (so->so_error); 3625 } 3626 3627 void 3628 so_error_set(struct socket *so, int val) 3629 { 3630 3631 so->so_error = val; 3632 } 3633 3634 int 3635 so_linger_get(const struct socket *so) 3636 { 3637 3638 return (so->so_linger); 3639 } 3640 3641 void 3642 so_linger_set(struct socket *so, int val) 3643 { 3644 3645 so->so_linger = val; 3646 } 3647 3648 struct protosw * 3649 so_protosw_get(const struct socket *so) 3650 { 3651 3652 return (so->so_proto); 3653 } 3654 3655 void 3656 so_protosw_set(struct socket *so, struct protosw *val) 3657 { 3658 3659 so->so_proto = val; 3660 } 3661 3662 void 3663 so_sorwakeup(struct socket *so) 3664 { 3665 3666 sorwakeup(so); 3667 } 3668 3669 void 3670 so_sowwakeup(struct socket *so) 3671 { 3672 3673 sowwakeup(so); 3674 } 3675 3676 void 3677 so_sorwakeup_locked(struct socket *so) 3678 { 3679 3680 sorwakeup_locked(so); 3681 } 3682 3683 void 3684 so_sowwakeup_locked(struct socket *so) 3685 { 3686 3687 sowwakeup_locked(so); 3688 } 3689 3690 void 3691 so_lock(struct socket *so) 3692 { 3693 3694 SOCK_LOCK(so); 3695 } 3696 3697 void 3698 so_unlock(struct socket *so) 3699 { 3700 3701 SOCK_UNLOCK(so); 3702 } 3703