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