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-2007 Robert N. M. Watson 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94 33 */ 34 35 /* 36 * Comments on the socket life cycle: 37 * 38 * soalloc() sets of socket layer state for a socket, called only by 39 * socreate() and sonewconn(). Socket layer private. 40 * 41 * sodealloc() tears down socket layer state for a socket, called only by 42 * sofree() and sonewconn(). Socket layer private. 43 * 44 * pru_attach() associates protocol layer state with an allocated socket; 45 * called only once, may fail, aborting socket allocation. This is called 46 * from socreate() and sonewconn(). Socket layer private. 47 * 48 * pru_detach() disassociates protocol layer state from an attached socket, 49 * and will be called exactly once for sockets in which pru_attach() has 50 * been successfully called. If pru_attach() returned an error, 51 * pru_detach() will not be called. Socket layer private. 52 * 53 * pru_abort() and pru_close() notify the protocol layer that the last 54 * consumer of a socket is starting to tear down the socket, and that the 55 * protocol should terminate the connection. Historically, pru_abort() also 56 * detached protocol state from the socket state, but this is no longer the 57 * case. 58 * 59 * socreate() creates a socket and attaches protocol state. This is a public 60 * interface that may be used by socket layer consumers to create new 61 * sockets. 62 * 63 * sonewconn() creates a socket and attaches protocol state. This is a 64 * public interface that may be used by protocols to create new sockets when 65 * a new connection is received and will be available for accept() on a 66 * listen socket. 67 * 68 * soclose() destroys a socket after possibly waiting for it to disconnect. 69 * This is a public interface that socket consumers should use to close and 70 * release a socket when done with it. 71 * 72 * soabort() destroys a socket without waiting for it to disconnect (used 73 * only for incoming connections that are already partially or fully 74 * connected). This is used internally by the socket layer when clearing 75 * listen socket queues (due to overflow or close on the listen socket), but 76 * is also a public interface protocols may use to abort connections in 77 * their incomplete listen queues should they no longer be required. Sockets 78 * placed in completed connection listen queues should not be aborted for 79 * reasons described in the comment above the soclose() implementation. This 80 * is not a general purpose close routine, and except in the specific 81 * circumstances described here, should not be used. 82 * 83 * sofree() will free a socket and its protocol state if all references on 84 * the socket have been released, and is the public interface to attempt to 85 * free a socket when a reference is removed. This is a socket layer private 86 * interface. 87 * 88 * NOTE: In addition to socreate() and soclose(), which provide a single 89 * socket reference to the consumer to be managed as required, there are two 90 * calls to explicitly manage socket references, soref(), and sorele(). 91 * Currently, these are generally required only when transitioning a socket 92 * from a listen queue to a file descriptor, in order to prevent garbage 93 * collection of the socket at an untimely moment. For a number of reasons, 94 * these interfaces are not preferred, and should be avoided. 95 */ 96 97 #include <sys/cdefs.h> 98 __FBSDID("$FreeBSD$"); 99 100 #include "opt_inet.h" 101 #include "opt_mac.h" 102 #include "opt_zero.h" 103 #include "opt_compat.h" 104 105 #include <sys/param.h> 106 #include <sys/systm.h> 107 #include <sys/fcntl.h> 108 #include <sys/limits.h> 109 #include <sys/lock.h> 110 #include <sys/mac.h> 111 #include <sys/malloc.h> 112 #include <sys/mbuf.h> 113 #include <sys/mutex.h> 114 #include <sys/domain.h> 115 #include <sys/file.h> /* for struct knote */ 116 #include <sys/kernel.h> 117 #include <sys/event.h> 118 #include <sys/eventhandler.h> 119 #include <sys/poll.h> 120 #include <sys/proc.h> 121 #include <sys/protosw.h> 122 #include <sys/socket.h> 123 #include <sys/socketvar.h> 124 #include <sys/resourcevar.h> 125 #include <sys/signalvar.h> 126 #include <sys/stat.h> 127 #include <sys/sx.h> 128 #include <sys/sysctl.h> 129 #include <sys/uio.h> 130 #include <sys/jail.h> 131 132 #include <security/mac/mac_framework.h> 133 134 #include <vm/uma.h> 135 136 #ifdef COMPAT_IA32 137 #include <sys/mount.h> 138 #include <compat/freebsd32/freebsd32.h> 139 140 extern struct sysentvec ia32_freebsd_sysvec; 141 #endif 142 143 static int soreceive_rcvoob(struct socket *so, struct uio *uio, 144 int flags); 145 146 static void filt_sordetach(struct knote *kn); 147 static int filt_soread(struct knote *kn, long hint); 148 static void filt_sowdetach(struct knote *kn); 149 static int filt_sowrite(struct knote *kn, long hint); 150 static int filt_solisten(struct knote *kn, long hint); 151 152 static struct filterops solisten_filtops = 153 { 1, NULL, filt_sordetach, filt_solisten }; 154 static struct filterops soread_filtops = 155 { 1, NULL, filt_sordetach, filt_soread }; 156 static struct filterops sowrite_filtops = 157 { 1, NULL, filt_sowdetach, filt_sowrite }; 158 159 uma_zone_t socket_zone; 160 so_gen_t so_gencnt; /* generation count for sockets */ 161 162 int maxsockets; 163 164 MALLOC_DEFINE(M_SONAME, "soname", "socket name"); 165 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block"); 166 167 static int somaxconn = SOMAXCONN; 168 static int sysctl_somaxconn(SYSCTL_HANDLER_ARGS); 169 /* XXX: we dont have SYSCTL_USHORT */ 170 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLTYPE_UINT | CTLFLAG_RW, 171 0, sizeof(int), sysctl_somaxconn, "I", "Maximum pending socket connection " 172 "queue size"); 173 static int numopensockets; 174 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD, 175 &numopensockets, 0, "Number of open sockets"); 176 #ifdef ZERO_COPY_SOCKETS 177 /* These aren't static because they're used in other files. */ 178 int so_zero_copy_send = 1; 179 int so_zero_copy_receive = 1; 180 SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0, 181 "Zero copy controls"); 182 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW, 183 &so_zero_copy_receive, 0, "Enable zero copy receive"); 184 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW, 185 &so_zero_copy_send, 0, "Enable zero copy send"); 186 #endif /* ZERO_COPY_SOCKETS */ 187 188 /* 189 * accept_mtx locks down per-socket fields relating to accept queues. See 190 * socketvar.h for an annotation of the protected fields of struct socket. 191 */ 192 struct mtx accept_mtx; 193 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF); 194 195 /* 196 * so_global_mtx protects so_gencnt, numopensockets, and the per-socket 197 * so_gencnt field. 198 */ 199 static struct mtx so_global_mtx; 200 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF); 201 202 /* 203 * General IPC sysctl name space, used by sockets and a variety of other IPC 204 * types. 205 */ 206 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC"); 207 208 /* 209 * Sysctl to get and set the maximum global sockets limit. Notify protocols 210 * of the change so that they can update their dependent limits as required. 211 */ 212 static int 213 sysctl_maxsockets(SYSCTL_HANDLER_ARGS) 214 { 215 int error, newmaxsockets; 216 217 newmaxsockets = maxsockets; 218 error = sysctl_handle_int(oidp, &newmaxsockets, 0, req); 219 if (error == 0 && req->newptr) { 220 if (newmaxsockets > maxsockets) { 221 maxsockets = newmaxsockets; 222 if (maxsockets > ((maxfiles / 4) * 3)) { 223 maxfiles = (maxsockets * 5) / 4; 224 maxfilesperproc = (maxfiles * 9) / 10; 225 } 226 EVENTHANDLER_INVOKE(maxsockets_change); 227 } else 228 error = EINVAL; 229 } 230 return (error); 231 } 232 233 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW, 234 &maxsockets, 0, sysctl_maxsockets, "IU", 235 "Maximum number of sockets avaliable"); 236 237 /* 238 * Initialise maxsockets. 239 */ 240 static void init_maxsockets(void *ignored) 241 { 242 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets); 243 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters)); 244 } 245 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL); 246 247 /* 248 * Socket operation routines. These routines are called by the routines in 249 * sys_socket.c or from a system process, and implement the semantics of 250 * socket operations by switching out to the protocol specific routines. 251 */ 252 253 /* 254 * Get a socket structure from our zone, and initialize it. Note that it 255 * would probably be better to allocate socket and PCB at the same time, but 256 * I'm not convinced that all the protocols can be easily modified to do 257 * this. 258 * 259 * soalloc() returns a socket with a ref count of 0. 260 */ 261 static struct socket * 262 soalloc(void) 263 { 264 struct socket *so; 265 266 so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO); 267 if (so == NULL) 268 return (NULL); 269 #ifdef MAC 270 if (mac_socket_init(so, M_NOWAIT) != 0) { 271 uma_zfree(socket_zone, so); 272 return (NULL); 273 } 274 #endif 275 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd"); 276 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv"); 277 sx_init(&so->so_snd.sb_sx, "so_snd_sx"); 278 sx_init(&so->so_rcv.sb_sx, "so_rcv_sx"); 279 TAILQ_INIT(&so->so_aiojobq); 280 mtx_lock(&so_global_mtx); 281 so->so_gencnt = ++so_gencnt; 282 ++numopensockets; 283 mtx_unlock(&so_global_mtx); 284 return (so); 285 } 286 287 /* 288 * Free the storage associated with a socket at the socket layer, tear down 289 * locks, labels, etc. All protocol state is assumed already to have been 290 * torn down (and possibly never set up) by the caller. 291 */ 292 static void 293 sodealloc(struct socket *so) 294 { 295 296 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count)); 297 KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL")); 298 299 mtx_lock(&so_global_mtx); 300 so->so_gencnt = ++so_gencnt; 301 --numopensockets; /* Could be below, but faster here. */ 302 mtx_unlock(&so_global_mtx); 303 if (so->so_rcv.sb_hiwat) 304 (void)chgsbsize(so->so_cred->cr_uidinfo, 305 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY); 306 if (so->so_snd.sb_hiwat) 307 (void)chgsbsize(so->so_cred->cr_uidinfo, 308 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY); 309 #ifdef INET 310 /* remove acccept filter if one is present. */ 311 if (so->so_accf != NULL) 312 do_setopt_accept_filter(so, NULL); 313 #endif 314 #ifdef MAC 315 mac_socket_destroy(so); 316 #endif 317 crfree(so->so_cred); 318 sx_destroy(&so->so_snd.sb_sx); 319 sx_destroy(&so->so_rcv.sb_sx); 320 SOCKBUF_LOCK_DESTROY(&so->so_snd); 321 SOCKBUF_LOCK_DESTROY(&so->so_rcv); 322 uma_zfree(socket_zone, so); 323 } 324 325 /* 326 * socreate returns a socket with a ref count of 1. The socket should be 327 * closed with soclose(). 328 */ 329 int 330 socreate(int dom, struct socket **aso, int type, int proto, 331 struct ucred *cred, struct thread *td) 332 { 333 struct protosw *prp; 334 struct socket *so; 335 int error; 336 337 if (proto) 338 prp = pffindproto(dom, proto, type); 339 else 340 prp = pffindtype(dom, type); 341 342 if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL || 343 prp->pr_usrreqs->pru_attach == pru_attach_notsupp) 344 return (EPROTONOSUPPORT); 345 346 if (jailed(cred) && jail_socket_unixiproute_only && 347 prp->pr_domain->dom_family != PF_LOCAL && 348 prp->pr_domain->dom_family != PF_INET && 349 prp->pr_domain->dom_family != PF_ROUTE) { 350 return (EPROTONOSUPPORT); 351 } 352 353 if (prp->pr_type != type) 354 return (EPROTOTYPE); 355 so = soalloc(); 356 if (so == NULL) 357 return (ENOBUFS); 358 359 TAILQ_INIT(&so->so_incomp); 360 TAILQ_INIT(&so->so_comp); 361 so->so_type = type; 362 so->so_cred = crhold(cred); 363 so->so_proto = prp; 364 #ifdef MAC 365 mac_socket_create(cred, so); 366 #endif 367 knlist_init(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv), 368 NULL, NULL, NULL); 369 knlist_init(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd), 370 NULL, NULL, NULL); 371 so->so_count = 1; 372 /* 373 * Auto-sizing of socket buffers is managed by the protocols and 374 * the appropriate flags must be set in the pru_attach function. 375 */ 376 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td); 377 if (error) { 378 KASSERT(so->so_count == 1, ("socreate: so_count %d", 379 so->so_count)); 380 so->so_count = 0; 381 sodealloc(so); 382 return (error); 383 } 384 *aso = so; 385 return (0); 386 } 387 388 #ifdef REGRESSION 389 static int regression_sonewconn_earlytest = 1; 390 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW, 391 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test"); 392 #endif 393 394 /* 395 * When an attempt at a new connection is noted on a socket which accepts 396 * connections, sonewconn is called. If the connection is possible (subject 397 * to space constraints, etc.) then we allocate a new structure, propoerly 398 * linked into the data structure of the original socket, and return this. 399 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. 400 * 401 * Note: the ref count on the socket is 0 on return. 402 */ 403 struct socket * 404 sonewconn(struct socket *head, int connstatus) 405 { 406 struct socket *so; 407 int over; 408 409 ACCEPT_LOCK(); 410 over = (head->so_qlen > 3 * head->so_qlimit / 2); 411 ACCEPT_UNLOCK(); 412 #ifdef REGRESSION 413 if (regression_sonewconn_earlytest && over) 414 #else 415 if (over) 416 #endif 417 return (NULL); 418 so = soalloc(); 419 if (so == NULL) 420 return (NULL); 421 if ((head->so_options & SO_ACCEPTFILTER) != 0) 422 connstatus = 0; 423 so->so_head = head; 424 so->so_type = head->so_type; 425 so->so_options = head->so_options &~ SO_ACCEPTCONN; 426 so->so_linger = head->so_linger; 427 so->so_state = head->so_state | SS_NOFDREF; 428 so->so_proto = head->so_proto; 429 so->so_cred = crhold(head->so_cred); 430 #ifdef MAC 431 SOCK_LOCK(head); 432 mac_socket_newconn(head, so); 433 SOCK_UNLOCK(head); 434 #endif 435 knlist_init(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv), 436 NULL, NULL, NULL); 437 knlist_init(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd), 438 NULL, NULL, NULL); 439 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) || 440 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) { 441 sodealloc(so); 442 return (NULL); 443 } 444 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat; 445 so->so_snd.sb_lowat = head->so_snd.sb_lowat; 446 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo; 447 so->so_snd.sb_timeo = head->so_snd.sb_timeo; 448 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE; 449 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE; 450 so->so_state |= connstatus; 451 ACCEPT_LOCK(); 452 if (connstatus) { 453 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 454 so->so_qstate |= SQ_COMP; 455 head->so_qlen++; 456 } else { 457 /* 458 * Keep removing sockets from the head until there's room for 459 * us to insert on the tail. In pre-locking revisions, this 460 * was a simple if(), but as we could be racing with other 461 * threads and soabort() requires dropping locks, we must 462 * loop waiting for the condition to be true. 463 */ 464 while (head->so_incqlen > head->so_qlimit) { 465 struct socket *sp; 466 sp = TAILQ_FIRST(&head->so_incomp); 467 TAILQ_REMOVE(&head->so_incomp, sp, so_list); 468 head->so_incqlen--; 469 sp->so_qstate &= ~SQ_INCOMP; 470 sp->so_head = NULL; 471 ACCEPT_UNLOCK(); 472 soabort(sp); 473 ACCEPT_LOCK(); 474 } 475 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); 476 so->so_qstate |= SQ_INCOMP; 477 head->so_incqlen++; 478 } 479 ACCEPT_UNLOCK(); 480 if (connstatus) { 481 sorwakeup(head); 482 wakeup_one(&head->so_timeo); 483 } 484 return (so); 485 } 486 487 int 488 sobind(struct socket *so, struct sockaddr *nam, struct thread *td) 489 { 490 491 return ((*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td)); 492 } 493 494 /* 495 * solisten() transitions a socket from a non-listening state to a listening 496 * state, but can also be used to update the listen queue depth on an 497 * existing listen socket. The protocol will call back into the sockets 498 * layer using solisten_proto_check() and solisten_proto() to check and set 499 * socket-layer listen state. Call backs are used so that the protocol can 500 * acquire both protocol and socket layer locks in whatever order is required 501 * by the protocol. 502 * 503 * Protocol implementors are advised to hold the socket lock across the 504 * socket-layer test and set to avoid races at the socket layer. 505 */ 506 int 507 solisten(struct socket *so, int backlog, struct thread *td) 508 { 509 510 return ((*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td)); 511 } 512 513 int 514 solisten_proto_check(struct socket *so) 515 { 516 517 SOCK_LOCK_ASSERT(so); 518 519 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | 520 SS_ISDISCONNECTING)) 521 return (EINVAL); 522 return (0); 523 } 524 525 void 526 solisten_proto(struct socket *so, int backlog) 527 { 528 529 SOCK_LOCK_ASSERT(so); 530 531 if (backlog < 0 || backlog > somaxconn) 532 backlog = somaxconn; 533 so->so_qlimit = backlog; 534 so->so_options |= SO_ACCEPTCONN; 535 } 536 537 /* 538 * Attempt to free a socket. This should really be sotryfree(). 539 * 540 * sofree() will succeed if: 541 * 542 * - There are no outstanding file descriptor references or related consumers 543 * (so_count == 0). 544 * 545 * - The socket has been closed by user space, if ever open (SS_NOFDREF). 546 * 547 * - The protocol does not have an outstanding strong reference on the socket 548 * (SS_PROTOREF). 549 * 550 * - The socket is not in a completed connection queue, so a process has been 551 * notified that it is present. If it is removed, the user process may 552 * block in accept() despite select() saying the socket was ready. 553 * 554 * Otherwise, it will quietly abort so that a future call to sofree(), when 555 * conditions are right, can succeed. 556 */ 557 void 558 sofree(struct socket *so) 559 { 560 struct protosw *pr = so->so_proto; 561 struct socket *head; 562 563 ACCEPT_LOCK_ASSERT(); 564 SOCK_LOCK_ASSERT(so); 565 566 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 || 567 (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) { 568 SOCK_UNLOCK(so); 569 ACCEPT_UNLOCK(); 570 return; 571 } 572 573 head = so->so_head; 574 if (head != NULL) { 575 KASSERT((so->so_qstate & SQ_COMP) != 0 || 576 (so->so_qstate & SQ_INCOMP) != 0, 577 ("sofree: so_head != NULL, but neither SQ_COMP nor " 578 "SQ_INCOMP")); 579 KASSERT((so->so_qstate & SQ_COMP) == 0 || 580 (so->so_qstate & SQ_INCOMP) == 0, 581 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP")); 582 TAILQ_REMOVE(&head->so_incomp, so, so_list); 583 head->so_incqlen--; 584 so->so_qstate &= ~SQ_INCOMP; 585 so->so_head = NULL; 586 } 587 KASSERT((so->so_qstate & SQ_COMP) == 0 && 588 (so->so_qstate & SQ_INCOMP) == 0, 589 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)", 590 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP)); 591 if (so->so_options & SO_ACCEPTCONN) { 592 KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated")); 593 KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated")); 594 } 595 SOCK_UNLOCK(so); 596 ACCEPT_UNLOCK(); 597 598 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL) 599 (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb); 600 if (pr->pr_usrreqs->pru_detach != NULL) 601 (*pr->pr_usrreqs->pru_detach)(so); 602 603 /* 604 * From this point on, we assume that no other references to this 605 * socket exist anywhere else in the stack. Therefore, no locks need 606 * to be acquired or held. 607 * 608 * We used to do a lot of socket buffer and socket locking here, as 609 * well as invoke sorflush() and perform wakeups. The direct call to 610 * dom_dispose() and sbrelease_internal() are an inlining of what was 611 * necessary from sorflush(). 612 * 613 * Notice that the socket buffer and kqueue state are torn down 614 * before calling pru_detach. This means that protocols shold not 615 * assume they can perform socket wakeups, etc, in their detach code. 616 */ 617 sbdestroy(&so->so_snd, so); 618 sbdestroy(&so->so_rcv, so); 619 knlist_destroy(&so->so_rcv.sb_sel.si_note); 620 knlist_destroy(&so->so_snd.sb_sel.si_note); 621 sodealloc(so); 622 } 623 624 /* 625 * Close a socket on last file table reference removal. Initiate disconnect 626 * if connected. Free socket when disconnect complete. 627 * 628 * This function will sorele() the socket. Note that soclose() may be called 629 * prior to the ref count reaching zero. The actual socket structure will 630 * not be freed until the ref count reaches zero. 631 */ 632 int 633 soclose(struct socket *so) 634 { 635 int error = 0; 636 637 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter")); 638 639 funsetown(&so->so_sigio); 640 if (so->so_state & SS_ISCONNECTED) { 641 if ((so->so_state & SS_ISDISCONNECTING) == 0) { 642 error = sodisconnect(so); 643 if (error) 644 goto drop; 645 } 646 if (so->so_options & SO_LINGER) { 647 if ((so->so_state & SS_ISDISCONNECTING) && 648 (so->so_state & SS_NBIO)) 649 goto drop; 650 while (so->so_state & SS_ISCONNECTED) { 651 error = tsleep(&so->so_timeo, 652 PSOCK | PCATCH, "soclos", so->so_linger * hz); 653 if (error) 654 break; 655 } 656 } 657 } 658 659 drop: 660 if (so->so_proto->pr_usrreqs->pru_close != NULL) 661 (*so->so_proto->pr_usrreqs->pru_close)(so); 662 if (so->so_options & SO_ACCEPTCONN) { 663 struct socket *sp; 664 ACCEPT_LOCK(); 665 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) { 666 TAILQ_REMOVE(&so->so_incomp, sp, so_list); 667 so->so_incqlen--; 668 sp->so_qstate &= ~SQ_INCOMP; 669 sp->so_head = NULL; 670 ACCEPT_UNLOCK(); 671 soabort(sp); 672 ACCEPT_LOCK(); 673 } 674 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) { 675 TAILQ_REMOVE(&so->so_comp, sp, so_list); 676 so->so_qlen--; 677 sp->so_qstate &= ~SQ_COMP; 678 sp->so_head = NULL; 679 ACCEPT_UNLOCK(); 680 soabort(sp); 681 ACCEPT_LOCK(); 682 } 683 ACCEPT_UNLOCK(); 684 } 685 ACCEPT_LOCK(); 686 SOCK_LOCK(so); 687 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF")); 688 so->so_state |= SS_NOFDREF; 689 sorele(so); 690 return (error); 691 } 692 693 /* 694 * soabort() is used to abruptly tear down a connection, such as when a 695 * resource limit is reached (listen queue depth exceeded), or if a listen 696 * socket is closed while there are sockets waiting to be accepted. 697 * 698 * This interface is tricky, because it is called on an unreferenced socket, 699 * and must be called only by a thread that has actually removed the socket 700 * from the listen queue it was on, or races with other threads are risked. 701 * 702 * This interface will call into the protocol code, so must not be called 703 * with any socket locks held. Protocols do call it while holding their own 704 * recursible protocol mutexes, but this is something that should be subject 705 * to review in the future. 706 */ 707 void 708 soabort(struct socket *so) 709 { 710 711 /* 712 * In as much as is possible, assert that no references to this 713 * socket are held. This is not quite the same as asserting that the 714 * current thread is responsible for arranging for no references, but 715 * is as close as we can get for now. 716 */ 717 KASSERT(so->so_count == 0, ("soabort: so_count")); 718 KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF")); 719 KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF")); 720 KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP")); 721 KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP")); 722 723 if (so->so_proto->pr_usrreqs->pru_abort != NULL) 724 (*so->so_proto->pr_usrreqs->pru_abort)(so); 725 ACCEPT_LOCK(); 726 SOCK_LOCK(so); 727 sofree(so); 728 } 729 730 int 731 soaccept(struct socket *so, struct sockaddr **nam) 732 { 733 int error; 734 735 SOCK_LOCK(so); 736 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF")); 737 so->so_state &= ~SS_NOFDREF; 738 SOCK_UNLOCK(so); 739 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam); 740 return (error); 741 } 742 743 int 744 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td) 745 { 746 int error; 747 748 if (so->so_options & SO_ACCEPTCONN) 749 return (EOPNOTSUPP); 750 /* 751 * If protocol is connection-based, can only connect once. 752 * Otherwise, if connected, try to disconnect first. This allows 753 * user to disconnect by connecting to, e.g., a null address. 754 */ 755 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && 756 ((so->so_proto->pr_flags & PR_CONNREQUIRED) || 757 (error = sodisconnect(so)))) { 758 error = EISCONN; 759 } else { 760 /* 761 * Prevent accumulated error from previous connection from 762 * biting us. 763 */ 764 so->so_error = 0; 765 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td); 766 } 767 768 return (error); 769 } 770 771 int 772 soconnect2(struct socket *so1, struct socket *so2) 773 { 774 775 return ((*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2)); 776 } 777 778 int 779 sodisconnect(struct socket *so) 780 { 781 int error; 782 783 if ((so->so_state & SS_ISCONNECTED) == 0) 784 return (ENOTCONN); 785 if (so->so_state & SS_ISDISCONNECTING) 786 return (EALREADY); 787 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so); 788 return (error); 789 } 790 791 #ifdef ZERO_COPY_SOCKETS 792 struct so_zerocopy_stats{ 793 int size_ok; 794 int align_ok; 795 int found_ifp; 796 }; 797 struct so_zerocopy_stats so_zerocp_stats = {0,0,0}; 798 #include <netinet/in.h> 799 #include <net/route.h> 800 #include <netinet/in_pcb.h> 801 #include <vm/vm.h> 802 #include <vm/vm_page.h> 803 #include <vm/vm_object.h> 804 805 /* 806 * sosend_copyin() is only used if zero copy sockets are enabled. Otherwise 807 * sosend_dgram() and sosend_generic() use m_uiotombuf(). 808 * 809 * sosend_copyin() accepts a uio and prepares an mbuf chain holding part or 810 * all of the data referenced by the uio. If desired, it uses zero-copy. 811 * *space will be updated to reflect data copied in. 812 * 813 * NB: If atomic I/O is requested, the caller must already have checked that 814 * space can hold resid bytes. 815 * 816 * NB: In the event of an error, the caller may need to free the partial 817 * chain pointed to by *mpp. The contents of both *uio and *space may be 818 * modified even in the case of an error. 819 */ 820 static int 821 sosend_copyin(struct uio *uio, struct mbuf **retmp, int atomic, long *space, 822 int flags) 823 { 824 struct mbuf *m, **mp, *top; 825 long len, resid; 826 int error; 827 #ifdef ZERO_COPY_SOCKETS 828 int cow_send; 829 #endif 830 831 *retmp = top = NULL; 832 mp = ⊤ 833 len = 0; 834 resid = uio->uio_resid; 835 error = 0; 836 do { 837 #ifdef ZERO_COPY_SOCKETS 838 cow_send = 0; 839 #endif /* ZERO_COPY_SOCKETS */ 840 if (resid >= MINCLSIZE) { 841 #ifdef ZERO_COPY_SOCKETS 842 if (top == NULL) { 843 m = m_gethdr(M_WAITOK, MT_DATA); 844 m->m_pkthdr.len = 0; 845 m->m_pkthdr.rcvif = NULL; 846 } else 847 m = m_get(M_WAITOK, MT_DATA); 848 if (so_zero_copy_send && 849 resid>=PAGE_SIZE && 850 *space>=PAGE_SIZE && 851 uio->uio_iov->iov_len>=PAGE_SIZE) { 852 so_zerocp_stats.size_ok++; 853 so_zerocp_stats.align_ok++; 854 cow_send = socow_setup(m, uio); 855 len = cow_send; 856 } 857 if (!cow_send) { 858 m_clget(m, M_WAITOK); 859 len = min(min(MCLBYTES, resid), *space); 860 } 861 #else /* ZERO_COPY_SOCKETS */ 862 if (top == NULL) { 863 m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR); 864 m->m_pkthdr.len = 0; 865 m->m_pkthdr.rcvif = NULL; 866 } else 867 m = m_getcl(M_WAIT, MT_DATA, 0); 868 len = min(min(MCLBYTES, resid), *space); 869 #endif /* ZERO_COPY_SOCKETS */ 870 } else { 871 if (top == NULL) { 872 m = m_gethdr(M_WAIT, MT_DATA); 873 m->m_pkthdr.len = 0; 874 m->m_pkthdr.rcvif = NULL; 875 876 len = min(min(MHLEN, resid), *space); 877 /* 878 * For datagram protocols, leave room 879 * for protocol headers in first mbuf. 880 */ 881 if (atomic && m && len < MHLEN) 882 MH_ALIGN(m, len); 883 } else { 884 m = m_get(M_WAIT, MT_DATA); 885 len = min(min(MLEN, resid), *space); 886 } 887 } 888 if (m == NULL) { 889 error = ENOBUFS; 890 goto out; 891 } 892 893 *space -= len; 894 #ifdef ZERO_COPY_SOCKETS 895 if (cow_send) 896 error = 0; 897 else 898 #endif /* ZERO_COPY_SOCKETS */ 899 error = uiomove(mtod(m, void *), (int)len, uio); 900 resid = uio->uio_resid; 901 m->m_len = len; 902 *mp = m; 903 top->m_pkthdr.len += len; 904 if (error) 905 goto out; 906 mp = &m->m_next; 907 if (resid <= 0) { 908 if (flags & MSG_EOR) 909 top->m_flags |= M_EOR; 910 break; 911 } 912 } while (*space > 0 && atomic); 913 out: 914 *retmp = top; 915 return (error); 916 } 917 #endif /*ZERO_COPY_SOCKETS*/ 918 919 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT) 920 921 int 922 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio, 923 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 924 { 925 long space, resid; 926 int clen = 0, error, dontroute; 927 #ifdef ZERO_COPY_SOCKETS 928 int atomic = sosendallatonce(so) || top; 929 #endif 930 931 KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM")); 932 KASSERT(so->so_proto->pr_flags & PR_ATOMIC, 933 ("sodgram_send: !PR_ATOMIC")); 934 935 if (uio != NULL) 936 resid = uio->uio_resid; 937 else 938 resid = top->m_pkthdr.len; 939 /* 940 * In theory resid should be unsigned. However, space must be 941 * signed, as it might be less than 0 if we over-committed, and we 942 * must use a signed comparison of space and resid. On the other 943 * hand, a negative resid causes us to loop sending 0-length 944 * segments to the protocol. 945 * 946 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 947 * type sockets since that's an error. 948 */ 949 if (resid < 0) { 950 error = EINVAL; 951 goto out; 952 } 953 954 dontroute = 955 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0; 956 if (td != NULL) 957 td->td_ru.ru_msgsnd++; 958 if (control != NULL) 959 clen = control->m_len; 960 961 SOCKBUF_LOCK(&so->so_snd); 962 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 963 SOCKBUF_UNLOCK(&so->so_snd); 964 error = EPIPE; 965 goto out; 966 } 967 if (so->so_error) { 968 error = so->so_error; 969 so->so_error = 0; 970 SOCKBUF_UNLOCK(&so->so_snd); 971 goto out; 972 } 973 if ((so->so_state & SS_ISCONNECTED) == 0) { 974 /* 975 * `sendto' and `sendmsg' is allowed on a connection-based 976 * socket if it supports implied connect. Return ENOTCONN if 977 * not connected and no address is supplied. 978 */ 979 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 980 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 981 if ((so->so_state & SS_ISCONFIRMING) == 0 && 982 !(resid == 0 && clen != 0)) { 983 SOCKBUF_UNLOCK(&so->so_snd); 984 error = ENOTCONN; 985 goto out; 986 } 987 } else if (addr == NULL) { 988 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 989 error = ENOTCONN; 990 else 991 error = EDESTADDRREQ; 992 SOCKBUF_UNLOCK(&so->so_snd); 993 goto out; 994 } 995 } 996 997 /* 998 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a 999 * problem and need fixing. 1000 */ 1001 space = sbspace(&so->so_snd); 1002 if (flags & MSG_OOB) 1003 space += 1024; 1004 space -= clen; 1005 SOCKBUF_UNLOCK(&so->so_snd); 1006 if (resid > space) { 1007 error = EMSGSIZE; 1008 goto out; 1009 } 1010 if (uio == NULL) { 1011 resid = 0; 1012 if (flags & MSG_EOR) 1013 top->m_flags |= M_EOR; 1014 } else { 1015 #ifdef ZERO_COPY_SOCKETS 1016 error = sosend_copyin(uio, &top, atomic, &space, flags); 1017 if (error) 1018 goto out; 1019 #else 1020 /* 1021 * Copy the data from userland into a mbuf chain. 1022 * If no data is to be copied in, a single empty mbuf 1023 * is returned. 1024 */ 1025 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr, 1026 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0))); 1027 if (top == NULL) { 1028 error = EFAULT; /* only possible error */ 1029 goto out; 1030 } 1031 space -= resid - uio->uio_resid; 1032 #endif 1033 resid = uio->uio_resid; 1034 } 1035 KASSERT(resid == 0, ("sosend_dgram: resid != 0")); 1036 /* 1037 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock 1038 * than with. 1039 */ 1040 if (dontroute) { 1041 SOCK_LOCK(so); 1042 so->so_options |= SO_DONTROUTE; 1043 SOCK_UNLOCK(so); 1044 } 1045 /* 1046 * XXX all the SBS_CANTSENDMORE checks previously done could be out 1047 * of date. We could have recieved a reset packet in an interrupt or 1048 * maybe we slept while doing page faults in uiomove() etc. We could 1049 * probably recheck again inside the locking protection here, but 1050 * there are probably other places that this also happens. We must 1051 * rethink this. 1052 */ 1053 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 1054 (flags & MSG_OOB) ? PRUS_OOB : 1055 /* 1056 * If the user set MSG_EOF, the protocol understands this flag and 1057 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND. 1058 */ 1059 ((flags & MSG_EOF) && 1060 (so->so_proto->pr_flags & PR_IMPLOPCL) && 1061 (resid <= 0)) ? 1062 PRUS_EOF : 1063 /* If there is more to send set PRUS_MORETOCOME */ 1064 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 1065 top, addr, control, td); 1066 if (dontroute) { 1067 SOCK_LOCK(so); 1068 so->so_options &= ~SO_DONTROUTE; 1069 SOCK_UNLOCK(so); 1070 } 1071 clen = 0; 1072 control = NULL; 1073 top = NULL; 1074 out: 1075 if (top != NULL) 1076 m_freem(top); 1077 if (control != NULL) 1078 m_freem(control); 1079 return (error); 1080 } 1081 1082 /* 1083 * Send on a socket. If send must go all at once and message is larger than 1084 * send buffering, then hard error. Lock against other senders. If must go 1085 * all at once and not enough room now, then inform user that this would 1086 * block and do nothing. Otherwise, if nonblocking, send as much as 1087 * possible. The data to be sent is described by "uio" if nonzero, otherwise 1088 * by the mbuf chain "top" (which must be null if uio is not). Data provided 1089 * in mbuf chain must be small enough to send all at once. 1090 * 1091 * Returns nonzero on error, timeout or signal; callers must check for short 1092 * counts if EINTR/ERESTART are returned. Data and control buffers are freed 1093 * on return. 1094 */ 1095 int 1096 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio, 1097 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 1098 { 1099 long space, resid; 1100 int clen = 0, error, dontroute; 1101 int atomic = sosendallatonce(so) || top; 1102 1103 if (uio != NULL) 1104 resid = uio->uio_resid; 1105 else 1106 resid = top->m_pkthdr.len; 1107 /* 1108 * In theory resid should be unsigned. However, space must be 1109 * signed, as it might be less than 0 if we over-committed, and we 1110 * must use a signed comparison of space and resid. On the other 1111 * hand, a negative resid causes us to loop sending 0-length 1112 * segments to the protocol. 1113 * 1114 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM 1115 * type sockets since that's an error. 1116 */ 1117 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) { 1118 error = EINVAL; 1119 goto out; 1120 } 1121 1122 dontroute = 1123 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && 1124 (so->so_proto->pr_flags & PR_ATOMIC); 1125 if (td != NULL) 1126 td->td_ru.ru_msgsnd++; 1127 if (control != NULL) 1128 clen = control->m_len; 1129 1130 error = sblock(&so->so_snd, SBLOCKWAIT(flags)); 1131 if (error) 1132 goto out; 1133 1134 restart: 1135 do { 1136 SOCKBUF_LOCK(&so->so_snd); 1137 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 1138 SOCKBUF_UNLOCK(&so->so_snd); 1139 error = EPIPE; 1140 goto release; 1141 } 1142 if (so->so_error) { 1143 error = so->so_error; 1144 so->so_error = 0; 1145 SOCKBUF_UNLOCK(&so->so_snd); 1146 goto release; 1147 } 1148 if ((so->so_state & SS_ISCONNECTED) == 0) { 1149 /* 1150 * `sendto' and `sendmsg' is allowed on a connection- 1151 * based socket if it supports implied connect. 1152 * Return ENOTCONN if not connected and no address is 1153 * supplied. 1154 */ 1155 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) && 1156 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) { 1157 if ((so->so_state & SS_ISCONFIRMING) == 0 && 1158 !(resid == 0 && clen != 0)) { 1159 SOCKBUF_UNLOCK(&so->so_snd); 1160 error = ENOTCONN; 1161 goto release; 1162 } 1163 } else if (addr == NULL) { 1164 SOCKBUF_UNLOCK(&so->so_snd); 1165 if (so->so_proto->pr_flags & PR_CONNREQUIRED) 1166 error = ENOTCONN; 1167 else 1168 error = EDESTADDRREQ; 1169 goto release; 1170 } 1171 } 1172 space = sbspace(&so->so_snd); 1173 if (flags & MSG_OOB) 1174 space += 1024; 1175 if ((atomic && resid > so->so_snd.sb_hiwat) || 1176 clen > so->so_snd.sb_hiwat) { 1177 SOCKBUF_UNLOCK(&so->so_snd); 1178 error = EMSGSIZE; 1179 goto release; 1180 } 1181 if (space < resid + clen && 1182 (atomic || space < so->so_snd.sb_lowat || space < clen)) { 1183 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) { 1184 SOCKBUF_UNLOCK(&so->so_snd); 1185 error = EWOULDBLOCK; 1186 goto release; 1187 } 1188 error = sbwait(&so->so_snd); 1189 SOCKBUF_UNLOCK(&so->so_snd); 1190 if (error) 1191 goto release; 1192 goto restart; 1193 } 1194 SOCKBUF_UNLOCK(&so->so_snd); 1195 space -= clen; 1196 do { 1197 if (uio == NULL) { 1198 resid = 0; 1199 if (flags & MSG_EOR) 1200 top->m_flags |= M_EOR; 1201 } else { 1202 #ifdef ZERO_COPY_SOCKETS 1203 error = sosend_copyin(uio, &top, atomic, 1204 &space, flags); 1205 if (error != 0) 1206 goto release; 1207 #else 1208 /* 1209 * Copy the data from userland into a mbuf 1210 * chain. If no data is to be copied in, 1211 * a single empty mbuf is returned. 1212 */ 1213 top = m_uiotombuf(uio, M_WAITOK, space, 1214 (atomic ? max_hdr : 0), 1215 (atomic ? M_PKTHDR : 0) | 1216 ((flags & MSG_EOR) ? M_EOR : 0)); 1217 if (top == NULL) { 1218 error = EFAULT; /* only possible error */ 1219 goto release; 1220 } 1221 space -= resid - uio->uio_resid; 1222 #endif 1223 resid = uio->uio_resid; 1224 } 1225 if (dontroute) { 1226 SOCK_LOCK(so); 1227 so->so_options |= SO_DONTROUTE; 1228 SOCK_UNLOCK(so); 1229 } 1230 /* 1231 * XXX all the SBS_CANTSENDMORE checks previously 1232 * done could be out of date. We could have recieved 1233 * a reset packet in an interrupt or maybe we slept 1234 * while doing page faults in uiomove() etc. We 1235 * could probably recheck again inside the locking 1236 * protection here, but there are probably other 1237 * places that this also happens. We must rethink 1238 * this. 1239 */ 1240 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 1241 (flags & MSG_OOB) ? PRUS_OOB : 1242 /* 1243 * If the user set MSG_EOF, the protocol understands 1244 * this flag and nothing left to send then use 1245 * PRU_SEND_EOF instead of PRU_SEND. 1246 */ 1247 ((flags & MSG_EOF) && 1248 (so->so_proto->pr_flags & PR_IMPLOPCL) && 1249 (resid <= 0)) ? 1250 PRUS_EOF : 1251 /* If there is more to send set PRUS_MORETOCOME. */ 1252 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0, 1253 top, addr, control, td); 1254 if (dontroute) { 1255 SOCK_LOCK(so); 1256 so->so_options &= ~SO_DONTROUTE; 1257 SOCK_UNLOCK(so); 1258 } 1259 clen = 0; 1260 control = NULL; 1261 top = NULL; 1262 if (error) 1263 goto release; 1264 } while (resid && space > 0); 1265 } while (resid); 1266 1267 release: 1268 sbunlock(&so->so_snd); 1269 out: 1270 if (top != NULL) 1271 m_freem(top); 1272 if (control != NULL) 1273 m_freem(control); 1274 return (error); 1275 } 1276 1277 int 1278 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, 1279 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 1280 { 1281 1282 /* XXXRW: Temporary debugging. */ 1283 KASSERT(so->so_proto->pr_usrreqs->pru_sosend != sosend, 1284 ("sosend: protocol calls sosend")); 1285 1286 return (so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top, 1287 control, flags, td)); 1288 } 1289 1290 /* 1291 * The part of soreceive() that implements reading non-inline out-of-band 1292 * data from a socket. For more complete comments, see soreceive(), from 1293 * which this code originated. 1294 * 1295 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is 1296 * unable to return an mbuf chain to the caller. 1297 */ 1298 static int 1299 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags) 1300 { 1301 struct protosw *pr = so->so_proto; 1302 struct mbuf *m; 1303 int error; 1304 1305 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0")); 1306 1307 m = m_get(M_WAIT, MT_DATA); 1308 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK); 1309 if (error) 1310 goto bad; 1311 do { 1312 #ifdef ZERO_COPY_SOCKETS 1313 if (so_zero_copy_receive) { 1314 int disposable; 1315 1316 if ((m->m_flags & M_EXT) 1317 && (m->m_ext.ext_type == EXT_DISPOSABLE)) 1318 disposable = 1; 1319 else 1320 disposable = 0; 1321 1322 error = uiomoveco(mtod(m, void *), 1323 min(uio->uio_resid, m->m_len), 1324 uio, disposable); 1325 } else 1326 #endif /* ZERO_COPY_SOCKETS */ 1327 error = uiomove(mtod(m, void *), 1328 (int) min(uio->uio_resid, m->m_len), uio); 1329 m = m_free(m); 1330 } while (uio->uio_resid && error == 0 && m); 1331 bad: 1332 if (m != NULL) 1333 m_freem(m); 1334 return (error); 1335 } 1336 1337 /* 1338 * Following replacement or removal of the first mbuf on the first mbuf chain 1339 * of a socket buffer, push necessary state changes back into the socket 1340 * buffer so that other consumers see the values consistently. 'nextrecord' 1341 * is the callers locally stored value of the original value of 1342 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes. 1343 * NOTE: 'nextrecord' may be NULL. 1344 */ 1345 static __inline void 1346 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord) 1347 { 1348 1349 SOCKBUF_LOCK_ASSERT(sb); 1350 /* 1351 * First, update for the new value of nextrecord. If necessary, make 1352 * it the first record. 1353 */ 1354 if (sb->sb_mb != NULL) 1355 sb->sb_mb->m_nextpkt = nextrecord; 1356 else 1357 sb->sb_mb = nextrecord; 1358 1359 /* 1360 * Now update any dependent socket buffer fields to reflect the new 1361 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the 1362 * addition of a second clause that takes care of the case where 1363 * sb_mb has been updated, but remains the last record. 1364 */ 1365 if (sb->sb_mb == NULL) { 1366 sb->sb_mbtail = NULL; 1367 sb->sb_lastrecord = NULL; 1368 } else if (sb->sb_mb->m_nextpkt == NULL) 1369 sb->sb_lastrecord = sb->sb_mb; 1370 } 1371 1372 1373 /* 1374 * Implement receive operations on a socket. We depend on the way that 1375 * records are added to the sockbuf by sbappend. In particular, each record 1376 * (mbufs linked through m_next) must begin with an address if the protocol 1377 * so specifies, followed by an optional mbuf or mbufs containing ancillary 1378 * data, and then zero or more mbufs of data. In order to allow parallelism 1379 * between network receive and copying to user space, as well as avoid 1380 * sleeping with a mutex held, we release the socket buffer mutex during the 1381 * user space copy. Although the sockbuf is locked, new data may still be 1382 * appended, and thus we must maintain consistency of the sockbuf during that 1383 * time. 1384 * 1385 * The caller may receive the data as a single mbuf chain by supplying an 1386 * mbuf **mp0 for use in returning the chain. The uio is then used only for 1387 * the count in uio_resid. 1388 */ 1389 int 1390 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio, 1391 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 1392 { 1393 struct mbuf *m, **mp; 1394 int flags, len, error, offset; 1395 struct protosw *pr = so->so_proto; 1396 struct mbuf *nextrecord; 1397 int moff, type = 0; 1398 int orig_resid = uio->uio_resid; 1399 1400 mp = mp0; 1401 if (psa != NULL) 1402 *psa = NULL; 1403 if (controlp != NULL) 1404 *controlp = NULL; 1405 if (flagsp != NULL) 1406 flags = *flagsp &~ MSG_EOR; 1407 else 1408 flags = 0; 1409 if (flags & MSG_OOB) 1410 return (soreceive_rcvoob(so, uio, flags)); 1411 if (mp != NULL) 1412 *mp = NULL; 1413 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING) 1414 && uio->uio_resid) 1415 (*pr->pr_usrreqs->pru_rcvd)(so, 0); 1416 1417 error = sblock(&so->so_rcv, SBLOCKWAIT(flags)); 1418 if (error) 1419 return (error); 1420 1421 restart: 1422 SOCKBUF_LOCK(&so->so_rcv); 1423 m = so->so_rcv.sb_mb; 1424 /* 1425 * If we have less data than requested, block awaiting more (subject 1426 * to any timeout) if: 1427 * 1. the current count is less than the low water mark, or 1428 * 2. MSG_WAITALL is set, and it is possible to do the entire 1429 * receive operation at once if we block (resid <= hiwat). 1430 * 3. MSG_DONTWAIT is not set 1431 * If MSG_WAITALL is set but resid is larger than the receive buffer, 1432 * we have to do the receive in sections, and thus risk returning a 1433 * short count if a timeout or signal occurs after we start. 1434 */ 1435 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 && 1436 so->so_rcv.sb_cc < uio->uio_resid) && 1437 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || 1438 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && 1439 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) { 1440 KASSERT(m != NULL || !so->so_rcv.sb_cc, 1441 ("receive: m == %p so->so_rcv.sb_cc == %u", 1442 m, so->so_rcv.sb_cc)); 1443 if (so->so_error) { 1444 if (m != NULL) 1445 goto dontblock; 1446 error = so->so_error; 1447 if ((flags & MSG_PEEK) == 0) 1448 so->so_error = 0; 1449 SOCKBUF_UNLOCK(&so->so_rcv); 1450 goto release; 1451 } 1452 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1453 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 1454 if (m == NULL) { 1455 SOCKBUF_UNLOCK(&so->so_rcv); 1456 goto release; 1457 } else 1458 goto dontblock; 1459 } 1460 for (; m != NULL; m = m->m_next) 1461 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { 1462 m = so->so_rcv.sb_mb; 1463 goto dontblock; 1464 } 1465 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 && 1466 (so->so_proto->pr_flags & PR_CONNREQUIRED)) { 1467 SOCKBUF_UNLOCK(&so->so_rcv); 1468 error = ENOTCONN; 1469 goto release; 1470 } 1471 if (uio->uio_resid == 0) { 1472 SOCKBUF_UNLOCK(&so->so_rcv); 1473 goto release; 1474 } 1475 if ((so->so_state & SS_NBIO) || 1476 (flags & (MSG_DONTWAIT|MSG_NBIO))) { 1477 SOCKBUF_UNLOCK(&so->so_rcv); 1478 error = EWOULDBLOCK; 1479 goto release; 1480 } 1481 SBLASTRECORDCHK(&so->so_rcv); 1482 SBLASTMBUFCHK(&so->so_rcv); 1483 error = sbwait(&so->so_rcv); 1484 SOCKBUF_UNLOCK(&so->so_rcv); 1485 if (error) 1486 goto release; 1487 goto restart; 1488 } 1489 dontblock: 1490 /* 1491 * From this point onward, we maintain 'nextrecord' as a cache of the 1492 * pointer to the next record in the socket buffer. We must keep the 1493 * various socket buffer pointers and local stack versions of the 1494 * pointers in sync, pushing out modifications before dropping the 1495 * socket buffer mutex, and re-reading them when picking it up. 1496 * 1497 * Otherwise, we will race with the network stack appending new data 1498 * or records onto the socket buffer by using inconsistent/stale 1499 * versions of the field, possibly resulting in socket buffer 1500 * corruption. 1501 * 1502 * By holding the high-level sblock(), we prevent simultaneous 1503 * readers from pulling off the front of the socket buffer. 1504 */ 1505 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1506 if (uio->uio_td) 1507 uio->uio_td->td_ru.ru_msgrcv++; 1508 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb")); 1509 SBLASTRECORDCHK(&so->so_rcv); 1510 SBLASTMBUFCHK(&so->so_rcv); 1511 nextrecord = m->m_nextpkt; 1512 if (pr->pr_flags & PR_ADDR) { 1513 KASSERT(m->m_type == MT_SONAME, 1514 ("m->m_type == %d", m->m_type)); 1515 orig_resid = 0; 1516 if (psa != NULL) 1517 *psa = sodupsockaddr(mtod(m, struct sockaddr *), 1518 M_NOWAIT); 1519 if (flags & MSG_PEEK) { 1520 m = m->m_next; 1521 } else { 1522 sbfree(&so->so_rcv, m); 1523 so->so_rcv.sb_mb = m_free(m); 1524 m = so->so_rcv.sb_mb; 1525 sockbuf_pushsync(&so->so_rcv, nextrecord); 1526 } 1527 } 1528 1529 /* 1530 * Process one or more MT_CONTROL mbufs present before any data mbufs 1531 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we 1532 * just copy the data; if !MSG_PEEK, we call into the protocol to 1533 * perform externalization (or freeing if controlp == NULL). 1534 */ 1535 if (m != NULL && m->m_type == MT_CONTROL) { 1536 struct mbuf *cm = NULL, *cmn; 1537 struct mbuf **cme = &cm; 1538 1539 do { 1540 if (flags & MSG_PEEK) { 1541 if (controlp != NULL) { 1542 *controlp = m_copy(m, 0, m->m_len); 1543 controlp = &(*controlp)->m_next; 1544 } 1545 m = m->m_next; 1546 } else { 1547 sbfree(&so->so_rcv, m); 1548 so->so_rcv.sb_mb = m->m_next; 1549 m->m_next = NULL; 1550 *cme = m; 1551 cme = &(*cme)->m_next; 1552 m = so->so_rcv.sb_mb; 1553 } 1554 } while (m != NULL && m->m_type == MT_CONTROL); 1555 if ((flags & MSG_PEEK) == 0) 1556 sockbuf_pushsync(&so->so_rcv, nextrecord); 1557 while (cm != NULL) { 1558 cmn = cm->m_next; 1559 cm->m_next = NULL; 1560 if (pr->pr_domain->dom_externalize != NULL) { 1561 SOCKBUF_UNLOCK(&so->so_rcv); 1562 error = (*pr->pr_domain->dom_externalize) 1563 (cm, controlp); 1564 SOCKBUF_LOCK(&so->so_rcv); 1565 } else if (controlp != NULL) 1566 *controlp = cm; 1567 else 1568 m_freem(cm); 1569 if (controlp != NULL) { 1570 orig_resid = 0; 1571 while (*controlp != NULL) 1572 controlp = &(*controlp)->m_next; 1573 } 1574 cm = cmn; 1575 } 1576 if (m != NULL) 1577 nextrecord = so->so_rcv.sb_mb->m_nextpkt; 1578 else 1579 nextrecord = so->so_rcv.sb_mb; 1580 orig_resid = 0; 1581 } 1582 if (m != NULL) { 1583 if ((flags & MSG_PEEK) == 0) { 1584 KASSERT(m->m_nextpkt == nextrecord, 1585 ("soreceive: post-control, nextrecord !sync")); 1586 if (nextrecord == NULL) { 1587 KASSERT(so->so_rcv.sb_mb == m, 1588 ("soreceive: post-control, sb_mb!=m")); 1589 KASSERT(so->so_rcv.sb_lastrecord == m, 1590 ("soreceive: post-control, lastrecord!=m")); 1591 } 1592 } 1593 type = m->m_type; 1594 if (type == MT_OOBDATA) 1595 flags |= MSG_OOB; 1596 } else { 1597 if ((flags & MSG_PEEK) == 0) { 1598 KASSERT(so->so_rcv.sb_mb == nextrecord, 1599 ("soreceive: sb_mb != nextrecord")); 1600 if (so->so_rcv.sb_mb == NULL) { 1601 KASSERT(so->so_rcv.sb_lastrecord == NULL, 1602 ("soreceive: sb_lastercord != NULL")); 1603 } 1604 } 1605 } 1606 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1607 SBLASTRECORDCHK(&so->so_rcv); 1608 SBLASTMBUFCHK(&so->so_rcv); 1609 1610 /* 1611 * Now continue to read any data mbufs off of the head of the socket 1612 * buffer until the read request is satisfied. Note that 'type' is 1613 * used to store the type of any mbuf reads that have happened so far 1614 * such that soreceive() can stop reading if the type changes, which 1615 * causes soreceive() to return only one of regular data and inline 1616 * out-of-band data in a single socket receive operation. 1617 */ 1618 moff = 0; 1619 offset = 0; 1620 while (m != NULL && uio->uio_resid > 0 && error == 0) { 1621 /* 1622 * If the type of mbuf has changed since the last mbuf 1623 * examined ('type'), end the receive operation. 1624 */ 1625 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1626 if (m->m_type == MT_OOBDATA) { 1627 if (type != MT_OOBDATA) 1628 break; 1629 } else if (type == MT_OOBDATA) 1630 break; 1631 else 1632 KASSERT(m->m_type == MT_DATA, 1633 ("m->m_type == %d", m->m_type)); 1634 so->so_rcv.sb_state &= ~SBS_RCVATMARK; 1635 len = uio->uio_resid; 1636 if (so->so_oobmark && len > so->so_oobmark - offset) 1637 len = so->so_oobmark - offset; 1638 if (len > m->m_len - moff) 1639 len = m->m_len - moff; 1640 /* 1641 * If mp is set, just pass back the mbufs. Otherwise copy 1642 * them out via the uio, then free. Sockbuf must be 1643 * consistent here (points to current mbuf, it points to next 1644 * record) when we drop priority; we must note any additions 1645 * to the sockbuf when we block interrupts again. 1646 */ 1647 if (mp == NULL) { 1648 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1649 SBLASTRECORDCHK(&so->so_rcv); 1650 SBLASTMBUFCHK(&so->so_rcv); 1651 SOCKBUF_UNLOCK(&so->so_rcv); 1652 #ifdef ZERO_COPY_SOCKETS 1653 if (so_zero_copy_receive) { 1654 int disposable; 1655 1656 if ((m->m_flags & M_EXT) 1657 && (m->m_ext.ext_type == EXT_DISPOSABLE)) 1658 disposable = 1; 1659 else 1660 disposable = 0; 1661 1662 error = uiomoveco(mtod(m, char *) + moff, 1663 (int)len, uio, 1664 disposable); 1665 } else 1666 #endif /* ZERO_COPY_SOCKETS */ 1667 error = uiomove(mtod(m, char *) + moff, (int)len, uio); 1668 SOCKBUF_LOCK(&so->so_rcv); 1669 if (error) { 1670 /* 1671 * The MT_SONAME mbuf has already been removed 1672 * from the record, so it is necessary to 1673 * remove the data mbufs, if any, to preserve 1674 * the invariant in the case of PR_ADDR that 1675 * requires MT_SONAME mbufs at the head of 1676 * each record. 1677 */ 1678 if (m && pr->pr_flags & PR_ATOMIC && 1679 ((flags & MSG_PEEK) == 0)) 1680 (void)sbdroprecord_locked(&so->so_rcv); 1681 SOCKBUF_UNLOCK(&so->so_rcv); 1682 goto release; 1683 } 1684 } else 1685 uio->uio_resid -= len; 1686 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1687 if (len == m->m_len - moff) { 1688 if (m->m_flags & M_EOR) 1689 flags |= MSG_EOR; 1690 if (flags & MSG_PEEK) { 1691 m = m->m_next; 1692 moff = 0; 1693 } else { 1694 nextrecord = m->m_nextpkt; 1695 sbfree(&so->so_rcv, m); 1696 if (mp != NULL) { 1697 *mp = m; 1698 mp = &m->m_next; 1699 so->so_rcv.sb_mb = m = m->m_next; 1700 *mp = NULL; 1701 } else { 1702 so->so_rcv.sb_mb = m_free(m); 1703 m = so->so_rcv.sb_mb; 1704 } 1705 sockbuf_pushsync(&so->so_rcv, nextrecord); 1706 SBLASTRECORDCHK(&so->so_rcv); 1707 SBLASTMBUFCHK(&so->so_rcv); 1708 } 1709 } else { 1710 if (flags & MSG_PEEK) 1711 moff += len; 1712 else { 1713 if (mp != NULL) { 1714 int copy_flag; 1715 1716 if (flags & MSG_DONTWAIT) 1717 copy_flag = M_DONTWAIT; 1718 else 1719 copy_flag = M_WAIT; 1720 if (copy_flag == M_WAIT) 1721 SOCKBUF_UNLOCK(&so->so_rcv); 1722 *mp = m_copym(m, 0, len, copy_flag); 1723 if (copy_flag == M_WAIT) 1724 SOCKBUF_LOCK(&so->so_rcv); 1725 if (*mp == NULL) { 1726 /* 1727 * m_copym() couldn't 1728 * allocate an mbuf. Adjust 1729 * uio_resid back (it was 1730 * adjusted down by len 1731 * bytes, which we didn't end 1732 * up "copying" over). 1733 */ 1734 uio->uio_resid += len; 1735 break; 1736 } 1737 } 1738 m->m_data += len; 1739 m->m_len -= len; 1740 so->so_rcv.sb_cc -= len; 1741 } 1742 } 1743 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1744 if (so->so_oobmark) { 1745 if ((flags & MSG_PEEK) == 0) { 1746 so->so_oobmark -= len; 1747 if (so->so_oobmark == 0) { 1748 so->so_rcv.sb_state |= SBS_RCVATMARK; 1749 break; 1750 } 1751 } else { 1752 offset += len; 1753 if (offset == so->so_oobmark) 1754 break; 1755 } 1756 } 1757 if (flags & MSG_EOR) 1758 break; 1759 /* 1760 * If the MSG_WAITALL flag is set (for non-atomic socket), we 1761 * must not quit until "uio->uio_resid == 0" or an error 1762 * termination. If a signal/timeout occurs, return with a 1763 * short count but without error. Keep sockbuf locked 1764 * against other readers. 1765 */ 1766 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 && 1767 !sosendallatonce(so) && nextrecord == NULL) { 1768 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1769 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE) 1770 break; 1771 /* 1772 * Notify the protocol that some data has been 1773 * drained before blocking. 1774 */ 1775 if (pr->pr_flags & PR_WANTRCVD) { 1776 SOCKBUF_UNLOCK(&so->so_rcv); 1777 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1778 SOCKBUF_LOCK(&so->so_rcv); 1779 } 1780 SBLASTRECORDCHK(&so->so_rcv); 1781 SBLASTMBUFCHK(&so->so_rcv); 1782 error = sbwait(&so->so_rcv); 1783 if (error) { 1784 SOCKBUF_UNLOCK(&so->so_rcv); 1785 goto release; 1786 } 1787 m = so->so_rcv.sb_mb; 1788 if (m != NULL) 1789 nextrecord = m->m_nextpkt; 1790 } 1791 } 1792 1793 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1794 if (m != NULL && pr->pr_flags & PR_ATOMIC) { 1795 flags |= MSG_TRUNC; 1796 if ((flags & MSG_PEEK) == 0) 1797 (void) sbdroprecord_locked(&so->so_rcv); 1798 } 1799 if ((flags & MSG_PEEK) == 0) { 1800 if (m == NULL) { 1801 /* 1802 * First part is an inline SB_EMPTY_FIXUP(). Second 1803 * part makes sure sb_lastrecord is up-to-date if 1804 * there is still data in the socket buffer. 1805 */ 1806 so->so_rcv.sb_mb = nextrecord; 1807 if (so->so_rcv.sb_mb == NULL) { 1808 so->so_rcv.sb_mbtail = NULL; 1809 so->so_rcv.sb_lastrecord = NULL; 1810 } else if (nextrecord->m_nextpkt == NULL) 1811 so->so_rcv.sb_lastrecord = nextrecord; 1812 } 1813 SBLASTRECORDCHK(&so->so_rcv); 1814 SBLASTMBUFCHK(&so->so_rcv); 1815 /* 1816 * If soreceive() is being done from the socket callback, 1817 * then don't need to generate ACK to peer to update window, 1818 * since ACK will be generated on return to TCP. 1819 */ 1820 if (!(flags & MSG_SOCALLBCK) && 1821 (pr->pr_flags & PR_WANTRCVD)) { 1822 SOCKBUF_UNLOCK(&so->so_rcv); 1823 (*pr->pr_usrreqs->pru_rcvd)(so, flags); 1824 SOCKBUF_LOCK(&so->so_rcv); 1825 } 1826 } 1827 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1828 if (orig_resid == uio->uio_resid && orig_resid && 1829 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) { 1830 SOCKBUF_UNLOCK(&so->so_rcv); 1831 goto restart; 1832 } 1833 SOCKBUF_UNLOCK(&so->so_rcv); 1834 1835 if (flagsp != NULL) 1836 *flagsp |= flags; 1837 release: 1838 sbunlock(&so->so_rcv); 1839 return (error); 1840 } 1841 1842 int 1843 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio, 1844 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 1845 { 1846 1847 /* XXXRW: Temporary debugging. */ 1848 KASSERT(so->so_proto->pr_usrreqs->pru_soreceive != soreceive, 1849 ("soreceive: protocol calls soreceive")); 1850 1851 return (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0, 1852 controlp, flagsp)); 1853 } 1854 1855 int 1856 soshutdown(struct socket *so, int how) 1857 { 1858 struct protosw *pr = so->so_proto; 1859 1860 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) 1861 return (EINVAL); 1862 1863 if (how != SHUT_WR) 1864 sorflush(so); 1865 if (how != SHUT_RD) 1866 return ((*pr->pr_usrreqs->pru_shutdown)(so)); 1867 return (0); 1868 } 1869 1870 void 1871 sorflush(struct socket *so) 1872 { 1873 struct sockbuf *sb = &so->so_rcv; 1874 struct protosw *pr = so->so_proto; 1875 struct sockbuf asb; 1876 1877 /* 1878 * In order to avoid calling dom_dispose with the socket buffer mutex 1879 * held, and in order to generally avoid holding the lock for a long 1880 * time, we make a copy of the socket buffer and clear the original 1881 * (except locks, state). The new socket buffer copy won't have 1882 * initialized locks so we can only call routines that won't use or 1883 * assert those locks. 1884 * 1885 * Dislodge threads currently blocked in receive and wait to acquire 1886 * a lock against other simultaneous readers before clearing the 1887 * socket buffer. Don't let our acquire be interrupted by a signal 1888 * despite any existing socket disposition on interruptable waiting. 1889 */ 1890 socantrcvmore(so); 1891 (void) sblock(sb, SBL_WAIT | SBL_NOINTR); 1892 1893 /* 1894 * Invalidate/clear most of the sockbuf structure, but leave selinfo 1895 * and mutex data unchanged. 1896 */ 1897 SOCKBUF_LOCK(sb); 1898 bzero(&asb, offsetof(struct sockbuf, sb_startzero)); 1899 bcopy(&sb->sb_startzero, &asb.sb_startzero, 1900 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero)); 1901 bzero(&sb->sb_startzero, 1902 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero)); 1903 SOCKBUF_UNLOCK(sb); 1904 sbunlock(sb); 1905 1906 /* 1907 * Dispose of special rights and flush the socket buffer. Don't call 1908 * any unsafe routines (that rely on locks being initialized) on asb. 1909 */ 1910 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL) 1911 (*pr->pr_domain->dom_dispose)(asb.sb_mb); 1912 sbrelease_internal(&asb, so); 1913 } 1914 1915 /* 1916 * Perhaps this routine, and sooptcopyout(), below, ought to come in an 1917 * additional variant to handle the case where the option value needs to be 1918 * some kind of integer, but not a specific size. In addition to their use 1919 * here, these functions are also called by the protocol-level pr_ctloutput() 1920 * routines. 1921 */ 1922 int 1923 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen) 1924 { 1925 size_t valsize; 1926 1927 /* 1928 * If the user gives us more than we wanted, we ignore it, but if we 1929 * don't get the minimum length the caller wants, we return EINVAL. 1930 * On success, sopt->sopt_valsize is set to however much we actually 1931 * retrieved. 1932 */ 1933 if ((valsize = sopt->sopt_valsize) < minlen) 1934 return EINVAL; 1935 if (valsize > len) 1936 sopt->sopt_valsize = valsize = len; 1937 1938 if (sopt->sopt_td != NULL) 1939 return (copyin(sopt->sopt_val, buf, valsize)); 1940 1941 bcopy(sopt->sopt_val, buf, valsize); 1942 return (0); 1943 } 1944 1945 /* 1946 * Kernel version of setsockopt(2). 1947 * 1948 * XXX: optlen is size_t, not socklen_t 1949 */ 1950 int 1951 so_setsockopt(struct socket *so, int level, int optname, void *optval, 1952 size_t optlen) 1953 { 1954 struct sockopt sopt; 1955 1956 sopt.sopt_level = level; 1957 sopt.sopt_name = optname; 1958 sopt.sopt_dir = SOPT_SET; 1959 sopt.sopt_val = optval; 1960 sopt.sopt_valsize = optlen; 1961 sopt.sopt_td = NULL; 1962 return (sosetopt(so, &sopt)); 1963 } 1964 1965 int 1966 sosetopt(struct socket *so, struct sockopt *sopt) 1967 { 1968 int error, optval; 1969 struct linger l; 1970 struct timeval tv; 1971 u_long val; 1972 #ifdef MAC 1973 struct mac extmac; 1974 #endif 1975 1976 error = 0; 1977 if (sopt->sopt_level != SOL_SOCKET) { 1978 if (so->so_proto && so->so_proto->pr_ctloutput) 1979 return ((*so->so_proto->pr_ctloutput) 1980 (so, sopt)); 1981 error = ENOPROTOOPT; 1982 } else { 1983 switch (sopt->sopt_name) { 1984 #ifdef INET 1985 case SO_ACCEPTFILTER: 1986 error = do_setopt_accept_filter(so, sopt); 1987 if (error) 1988 goto bad; 1989 break; 1990 #endif 1991 case SO_LINGER: 1992 error = sooptcopyin(sopt, &l, sizeof l, sizeof l); 1993 if (error) 1994 goto bad; 1995 1996 SOCK_LOCK(so); 1997 so->so_linger = l.l_linger; 1998 if (l.l_onoff) 1999 so->so_options |= SO_LINGER; 2000 else 2001 so->so_options &= ~SO_LINGER; 2002 SOCK_UNLOCK(so); 2003 break; 2004 2005 case SO_DEBUG: 2006 case SO_KEEPALIVE: 2007 case SO_DONTROUTE: 2008 case SO_USELOOPBACK: 2009 case SO_BROADCAST: 2010 case SO_REUSEADDR: 2011 case SO_REUSEPORT: 2012 case SO_OOBINLINE: 2013 case SO_TIMESTAMP: 2014 case SO_BINTIME: 2015 case SO_NOSIGPIPE: 2016 error = sooptcopyin(sopt, &optval, sizeof optval, 2017 sizeof optval); 2018 if (error) 2019 goto bad; 2020 SOCK_LOCK(so); 2021 if (optval) 2022 so->so_options |= sopt->sopt_name; 2023 else 2024 so->so_options &= ~sopt->sopt_name; 2025 SOCK_UNLOCK(so); 2026 break; 2027 2028 case SO_SNDBUF: 2029 case SO_RCVBUF: 2030 case SO_SNDLOWAT: 2031 case SO_RCVLOWAT: 2032 error = sooptcopyin(sopt, &optval, sizeof optval, 2033 sizeof optval); 2034 if (error) 2035 goto bad; 2036 2037 /* 2038 * Values < 1 make no sense for any of these options, 2039 * so disallow them. 2040 */ 2041 if (optval < 1) { 2042 error = EINVAL; 2043 goto bad; 2044 } 2045 2046 switch (sopt->sopt_name) { 2047 case SO_SNDBUF: 2048 case SO_RCVBUF: 2049 if (sbreserve(sopt->sopt_name == SO_SNDBUF ? 2050 &so->so_snd : &so->so_rcv, (u_long)optval, 2051 so, curthread) == 0) { 2052 error = ENOBUFS; 2053 goto bad; 2054 } 2055 (sopt->sopt_name == SO_SNDBUF ? &so->so_snd : 2056 &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE; 2057 break; 2058 2059 /* 2060 * Make sure the low-water is never greater than the 2061 * high-water. 2062 */ 2063 case SO_SNDLOWAT: 2064 SOCKBUF_LOCK(&so->so_snd); 2065 so->so_snd.sb_lowat = 2066 (optval > so->so_snd.sb_hiwat) ? 2067 so->so_snd.sb_hiwat : optval; 2068 SOCKBUF_UNLOCK(&so->so_snd); 2069 break; 2070 case SO_RCVLOWAT: 2071 SOCKBUF_LOCK(&so->so_rcv); 2072 so->so_rcv.sb_lowat = 2073 (optval > so->so_rcv.sb_hiwat) ? 2074 so->so_rcv.sb_hiwat : optval; 2075 SOCKBUF_UNLOCK(&so->so_rcv); 2076 break; 2077 } 2078 break; 2079 2080 case SO_SNDTIMEO: 2081 case SO_RCVTIMEO: 2082 #ifdef COMPAT_IA32 2083 if (curthread->td_proc->p_sysent == &ia32_freebsd_sysvec) { 2084 struct timeval32 tv32; 2085 2086 error = sooptcopyin(sopt, &tv32, sizeof tv32, 2087 sizeof tv32); 2088 CP(tv32, tv, tv_sec); 2089 CP(tv32, tv, tv_usec); 2090 } else 2091 #endif 2092 error = sooptcopyin(sopt, &tv, sizeof tv, 2093 sizeof tv); 2094 if (error) 2095 goto bad; 2096 2097 /* assert(hz > 0); */ 2098 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz || 2099 tv.tv_usec < 0 || tv.tv_usec >= 1000000) { 2100 error = EDOM; 2101 goto bad; 2102 } 2103 /* assert(tick > 0); */ 2104 /* assert(ULONG_MAX - INT_MAX >= 1000000); */ 2105 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick; 2106 if (val > INT_MAX) { 2107 error = EDOM; 2108 goto bad; 2109 } 2110 if (val == 0 && tv.tv_usec != 0) 2111 val = 1; 2112 2113 switch (sopt->sopt_name) { 2114 case SO_SNDTIMEO: 2115 so->so_snd.sb_timeo = val; 2116 break; 2117 case SO_RCVTIMEO: 2118 so->so_rcv.sb_timeo = val; 2119 break; 2120 } 2121 break; 2122 2123 case SO_LABEL: 2124 #ifdef MAC 2125 error = sooptcopyin(sopt, &extmac, sizeof extmac, 2126 sizeof extmac); 2127 if (error) 2128 goto bad; 2129 error = mac_setsockopt_label(sopt->sopt_td->td_ucred, 2130 so, &extmac); 2131 #else 2132 error = EOPNOTSUPP; 2133 #endif 2134 break; 2135 2136 default: 2137 error = ENOPROTOOPT; 2138 break; 2139 } 2140 if (error == 0 && so->so_proto != NULL && 2141 so->so_proto->pr_ctloutput != NULL) { 2142 (void) ((*so->so_proto->pr_ctloutput) 2143 (so, sopt)); 2144 } 2145 } 2146 bad: 2147 return (error); 2148 } 2149 2150 /* 2151 * Helper routine for getsockopt. 2152 */ 2153 int 2154 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len) 2155 { 2156 int error; 2157 size_t valsize; 2158 2159 error = 0; 2160 2161 /* 2162 * Documented get behavior is that we always return a value, possibly 2163 * truncated to fit in the user's buffer. Traditional behavior is 2164 * that we always tell the user precisely how much we copied, rather 2165 * than something useful like the total amount we had available for 2166 * her. Note that this interface is not idempotent; the entire 2167 * answer must generated ahead of time. 2168 */ 2169 valsize = min(len, sopt->sopt_valsize); 2170 sopt->sopt_valsize = valsize; 2171 if (sopt->sopt_val != NULL) { 2172 if (sopt->sopt_td != NULL) 2173 error = copyout(buf, sopt->sopt_val, valsize); 2174 else 2175 bcopy(buf, sopt->sopt_val, valsize); 2176 } 2177 return (error); 2178 } 2179 2180 int 2181 sogetopt(struct socket *so, struct sockopt *sopt) 2182 { 2183 int error, optval; 2184 struct linger l; 2185 struct timeval tv; 2186 #ifdef MAC 2187 struct mac extmac; 2188 #endif 2189 2190 error = 0; 2191 if (sopt->sopt_level != SOL_SOCKET) { 2192 if (so->so_proto && so->so_proto->pr_ctloutput) { 2193 return ((*so->so_proto->pr_ctloutput) 2194 (so, sopt)); 2195 } else 2196 return (ENOPROTOOPT); 2197 } else { 2198 switch (sopt->sopt_name) { 2199 #ifdef INET 2200 case SO_ACCEPTFILTER: 2201 error = do_getopt_accept_filter(so, sopt); 2202 break; 2203 #endif 2204 case SO_LINGER: 2205 SOCK_LOCK(so); 2206 l.l_onoff = so->so_options & SO_LINGER; 2207 l.l_linger = so->so_linger; 2208 SOCK_UNLOCK(so); 2209 error = sooptcopyout(sopt, &l, sizeof l); 2210 break; 2211 2212 case SO_USELOOPBACK: 2213 case SO_DONTROUTE: 2214 case SO_DEBUG: 2215 case SO_KEEPALIVE: 2216 case SO_REUSEADDR: 2217 case SO_REUSEPORT: 2218 case SO_BROADCAST: 2219 case SO_OOBINLINE: 2220 case SO_ACCEPTCONN: 2221 case SO_TIMESTAMP: 2222 case SO_BINTIME: 2223 case SO_NOSIGPIPE: 2224 optval = so->so_options & sopt->sopt_name; 2225 integer: 2226 error = sooptcopyout(sopt, &optval, sizeof optval); 2227 break; 2228 2229 case SO_TYPE: 2230 optval = so->so_type; 2231 goto integer; 2232 2233 case SO_ERROR: 2234 SOCK_LOCK(so); 2235 optval = so->so_error; 2236 so->so_error = 0; 2237 SOCK_UNLOCK(so); 2238 goto integer; 2239 2240 case SO_SNDBUF: 2241 optval = so->so_snd.sb_hiwat; 2242 goto integer; 2243 2244 case SO_RCVBUF: 2245 optval = so->so_rcv.sb_hiwat; 2246 goto integer; 2247 2248 case SO_SNDLOWAT: 2249 optval = so->so_snd.sb_lowat; 2250 goto integer; 2251 2252 case SO_RCVLOWAT: 2253 optval = so->so_rcv.sb_lowat; 2254 goto integer; 2255 2256 case SO_SNDTIMEO: 2257 case SO_RCVTIMEO: 2258 optval = (sopt->sopt_name == SO_SNDTIMEO ? 2259 so->so_snd.sb_timeo : so->so_rcv.sb_timeo); 2260 2261 tv.tv_sec = optval / hz; 2262 tv.tv_usec = (optval % hz) * tick; 2263 #ifdef COMPAT_IA32 2264 if (curthread->td_proc->p_sysent == &ia32_freebsd_sysvec) { 2265 struct timeval32 tv32; 2266 2267 CP(tv, tv32, tv_sec); 2268 CP(tv, tv32, tv_usec); 2269 error = sooptcopyout(sopt, &tv32, sizeof tv32); 2270 } else 2271 #endif 2272 error = sooptcopyout(sopt, &tv, sizeof tv); 2273 break; 2274 2275 case SO_LABEL: 2276 #ifdef MAC 2277 error = sooptcopyin(sopt, &extmac, sizeof(extmac), 2278 sizeof(extmac)); 2279 if (error) 2280 return (error); 2281 error = mac_getsockopt_label(sopt->sopt_td->td_ucred, 2282 so, &extmac); 2283 if (error) 2284 return (error); 2285 error = sooptcopyout(sopt, &extmac, sizeof extmac); 2286 #else 2287 error = EOPNOTSUPP; 2288 #endif 2289 break; 2290 2291 case SO_PEERLABEL: 2292 #ifdef MAC 2293 error = sooptcopyin(sopt, &extmac, sizeof(extmac), 2294 sizeof(extmac)); 2295 if (error) 2296 return (error); 2297 error = mac_getsockopt_peerlabel( 2298 sopt->sopt_td->td_ucred, so, &extmac); 2299 if (error) 2300 return (error); 2301 error = sooptcopyout(sopt, &extmac, sizeof extmac); 2302 #else 2303 error = EOPNOTSUPP; 2304 #endif 2305 break; 2306 2307 case SO_LISTENQLIMIT: 2308 optval = so->so_qlimit; 2309 goto integer; 2310 2311 case SO_LISTENQLEN: 2312 optval = so->so_qlen; 2313 goto integer; 2314 2315 case SO_LISTENINCQLEN: 2316 optval = so->so_incqlen; 2317 goto integer; 2318 2319 default: 2320 error = ENOPROTOOPT; 2321 break; 2322 } 2323 return (error); 2324 } 2325 } 2326 2327 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 2328 int 2329 soopt_getm(struct sockopt *sopt, struct mbuf **mp) 2330 { 2331 struct mbuf *m, *m_prev; 2332 int sopt_size = sopt->sopt_valsize; 2333 2334 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA); 2335 if (m == NULL) 2336 return ENOBUFS; 2337 if (sopt_size > MLEN) { 2338 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT); 2339 if ((m->m_flags & M_EXT) == 0) { 2340 m_free(m); 2341 return ENOBUFS; 2342 } 2343 m->m_len = min(MCLBYTES, sopt_size); 2344 } else { 2345 m->m_len = min(MLEN, sopt_size); 2346 } 2347 sopt_size -= m->m_len; 2348 *mp = m; 2349 m_prev = m; 2350 2351 while (sopt_size) { 2352 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA); 2353 if (m == NULL) { 2354 m_freem(*mp); 2355 return ENOBUFS; 2356 } 2357 if (sopt_size > MLEN) { 2358 MCLGET(m, sopt->sopt_td != NULL ? M_WAIT : 2359 M_DONTWAIT); 2360 if ((m->m_flags & M_EXT) == 0) { 2361 m_freem(m); 2362 m_freem(*mp); 2363 return ENOBUFS; 2364 } 2365 m->m_len = min(MCLBYTES, sopt_size); 2366 } else { 2367 m->m_len = min(MLEN, sopt_size); 2368 } 2369 sopt_size -= m->m_len; 2370 m_prev->m_next = m; 2371 m_prev = m; 2372 } 2373 return (0); 2374 } 2375 2376 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */ 2377 int 2378 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m) 2379 { 2380 struct mbuf *m0 = m; 2381 2382 if (sopt->sopt_val == NULL) 2383 return (0); 2384 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 2385 if (sopt->sopt_td != NULL) { 2386 int error; 2387 2388 error = copyin(sopt->sopt_val, mtod(m, char *), 2389 m->m_len); 2390 if (error != 0) { 2391 m_freem(m0); 2392 return(error); 2393 } 2394 } else 2395 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len); 2396 sopt->sopt_valsize -= m->m_len; 2397 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len; 2398 m = m->m_next; 2399 } 2400 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */ 2401 panic("ip6_sooptmcopyin"); 2402 return (0); 2403 } 2404 2405 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */ 2406 int 2407 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m) 2408 { 2409 struct mbuf *m0 = m; 2410 size_t valsize = 0; 2411 2412 if (sopt->sopt_val == NULL) 2413 return (0); 2414 while (m != NULL && sopt->sopt_valsize >= m->m_len) { 2415 if (sopt->sopt_td != NULL) { 2416 int error; 2417 2418 error = copyout(mtod(m, char *), sopt->sopt_val, 2419 m->m_len); 2420 if (error != 0) { 2421 m_freem(m0); 2422 return(error); 2423 } 2424 } else 2425 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len); 2426 sopt->sopt_valsize -= m->m_len; 2427 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len; 2428 valsize += m->m_len; 2429 m = m->m_next; 2430 } 2431 if (m != NULL) { 2432 /* enough soopt buffer should be given from user-land */ 2433 m_freem(m0); 2434 return(EINVAL); 2435 } 2436 sopt->sopt_valsize = valsize; 2437 return (0); 2438 } 2439 2440 /* 2441 * sohasoutofband(): protocol notifies socket layer of the arrival of new 2442 * out-of-band data, which will then notify socket consumers. 2443 */ 2444 void 2445 sohasoutofband(struct socket *so) 2446 { 2447 2448 if (so->so_sigio != NULL) 2449 pgsigio(&so->so_sigio, SIGURG, 0); 2450 selwakeuppri(&so->so_rcv.sb_sel, PSOCK); 2451 } 2452 2453 int 2454 sopoll(struct socket *so, int events, struct ucred *active_cred, 2455 struct thread *td) 2456 { 2457 2458 /* XXXRW: Temporary debugging. */ 2459 KASSERT(so->so_proto->pr_usrreqs->pru_sopoll != sopoll, 2460 ("sopoll: protocol calls sopoll")); 2461 2462 return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred, 2463 td)); 2464 } 2465 2466 int 2467 sopoll_generic(struct socket *so, int events, struct ucred *active_cred, 2468 struct thread *td) 2469 { 2470 int revents = 0; 2471 2472 SOCKBUF_LOCK(&so->so_snd); 2473 SOCKBUF_LOCK(&so->so_rcv); 2474 if (events & (POLLIN | POLLRDNORM)) 2475 if (soreadable(so)) 2476 revents |= events & (POLLIN | POLLRDNORM); 2477 2478 if (events & POLLINIGNEOF) 2479 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat || 2480 !TAILQ_EMPTY(&so->so_comp) || so->so_error) 2481 revents |= POLLINIGNEOF; 2482 2483 if (events & (POLLOUT | POLLWRNORM)) 2484 if (sowriteable(so)) 2485 revents |= events & (POLLOUT | POLLWRNORM); 2486 2487 if (events & (POLLPRI | POLLRDBAND)) 2488 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK)) 2489 revents |= events & (POLLPRI | POLLRDBAND); 2490 2491 if (revents == 0) { 2492 if (events & 2493 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM | 2494 POLLRDBAND)) { 2495 selrecord(td, &so->so_rcv.sb_sel); 2496 so->so_rcv.sb_flags |= SB_SEL; 2497 } 2498 2499 if (events & (POLLOUT | POLLWRNORM)) { 2500 selrecord(td, &so->so_snd.sb_sel); 2501 so->so_snd.sb_flags |= SB_SEL; 2502 } 2503 } 2504 2505 SOCKBUF_UNLOCK(&so->so_rcv); 2506 SOCKBUF_UNLOCK(&so->so_snd); 2507 return (revents); 2508 } 2509 2510 int 2511 soo_kqfilter(struct file *fp, struct knote *kn) 2512 { 2513 struct socket *so = kn->kn_fp->f_data; 2514 struct sockbuf *sb; 2515 2516 switch (kn->kn_filter) { 2517 case EVFILT_READ: 2518 if (so->so_options & SO_ACCEPTCONN) 2519 kn->kn_fop = &solisten_filtops; 2520 else 2521 kn->kn_fop = &soread_filtops; 2522 sb = &so->so_rcv; 2523 break; 2524 case EVFILT_WRITE: 2525 kn->kn_fop = &sowrite_filtops; 2526 sb = &so->so_snd; 2527 break; 2528 default: 2529 return (EINVAL); 2530 } 2531 2532 SOCKBUF_LOCK(sb); 2533 knlist_add(&sb->sb_sel.si_note, kn, 1); 2534 sb->sb_flags |= SB_KNOTE; 2535 SOCKBUF_UNLOCK(sb); 2536 return (0); 2537 } 2538 2539 /* 2540 * Some routines that return EOPNOTSUPP for entry points that are not 2541 * supported by a protocol. Fill in as needed. 2542 */ 2543 int 2544 pru_accept_notsupp(struct socket *so, struct sockaddr **nam) 2545 { 2546 2547 return EOPNOTSUPP; 2548 } 2549 2550 int 2551 pru_attach_notsupp(struct socket *so, int proto, struct thread *td) 2552 { 2553 2554 return EOPNOTSUPP; 2555 } 2556 2557 int 2558 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 2559 { 2560 2561 return EOPNOTSUPP; 2562 } 2563 2564 int 2565 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 2566 { 2567 2568 return EOPNOTSUPP; 2569 } 2570 2571 int 2572 pru_connect2_notsupp(struct socket *so1, struct socket *so2) 2573 { 2574 2575 return EOPNOTSUPP; 2576 } 2577 2578 int 2579 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, 2580 struct ifnet *ifp, struct thread *td) 2581 { 2582 2583 return EOPNOTSUPP; 2584 } 2585 2586 int 2587 pru_disconnect_notsupp(struct socket *so) 2588 { 2589 2590 return EOPNOTSUPP; 2591 } 2592 2593 int 2594 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td) 2595 { 2596 2597 return EOPNOTSUPP; 2598 } 2599 2600 int 2601 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam) 2602 { 2603 2604 return EOPNOTSUPP; 2605 } 2606 2607 int 2608 pru_rcvd_notsupp(struct socket *so, int flags) 2609 { 2610 2611 return EOPNOTSUPP; 2612 } 2613 2614 int 2615 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) 2616 { 2617 2618 return EOPNOTSUPP; 2619 } 2620 2621 int 2622 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m, 2623 struct sockaddr *addr, struct mbuf *control, struct thread *td) 2624 { 2625 2626 return EOPNOTSUPP; 2627 } 2628 2629 /* 2630 * This isn't really a ``null'' operation, but it's the default one and 2631 * doesn't do anything destructive. 2632 */ 2633 int 2634 pru_sense_null(struct socket *so, struct stat *sb) 2635 { 2636 2637 sb->st_blksize = so->so_snd.sb_hiwat; 2638 return 0; 2639 } 2640 2641 int 2642 pru_shutdown_notsupp(struct socket *so) 2643 { 2644 2645 return EOPNOTSUPP; 2646 } 2647 2648 int 2649 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam) 2650 { 2651 2652 return EOPNOTSUPP; 2653 } 2654 2655 int 2656 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio, 2657 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 2658 { 2659 2660 return EOPNOTSUPP; 2661 } 2662 2663 int 2664 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr, 2665 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp) 2666 { 2667 2668 return EOPNOTSUPP; 2669 } 2670 2671 int 2672 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred, 2673 struct thread *td) 2674 { 2675 2676 return EOPNOTSUPP; 2677 } 2678 2679 static void 2680 filt_sordetach(struct knote *kn) 2681 { 2682 struct socket *so = kn->kn_fp->f_data; 2683 2684 SOCKBUF_LOCK(&so->so_rcv); 2685 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1); 2686 if (knlist_empty(&so->so_rcv.sb_sel.si_note)) 2687 so->so_rcv.sb_flags &= ~SB_KNOTE; 2688 SOCKBUF_UNLOCK(&so->so_rcv); 2689 } 2690 2691 /*ARGSUSED*/ 2692 static int 2693 filt_soread(struct knote *kn, long hint) 2694 { 2695 struct socket *so; 2696 2697 so = kn->kn_fp->f_data; 2698 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 2699 2700 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl; 2701 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) { 2702 kn->kn_flags |= EV_EOF; 2703 kn->kn_fflags = so->so_error; 2704 return (1); 2705 } else if (so->so_error) /* temporary udp error */ 2706 return (1); 2707 else if (kn->kn_sfflags & NOTE_LOWAT) 2708 return (kn->kn_data >= kn->kn_sdata); 2709 else 2710 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat); 2711 } 2712 2713 static void 2714 filt_sowdetach(struct knote *kn) 2715 { 2716 struct socket *so = kn->kn_fp->f_data; 2717 2718 SOCKBUF_LOCK(&so->so_snd); 2719 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1); 2720 if (knlist_empty(&so->so_snd.sb_sel.si_note)) 2721 so->so_snd.sb_flags &= ~SB_KNOTE; 2722 SOCKBUF_UNLOCK(&so->so_snd); 2723 } 2724 2725 /*ARGSUSED*/ 2726 static int 2727 filt_sowrite(struct knote *kn, long hint) 2728 { 2729 struct socket *so; 2730 2731 so = kn->kn_fp->f_data; 2732 SOCKBUF_LOCK_ASSERT(&so->so_snd); 2733 kn->kn_data = sbspace(&so->so_snd); 2734 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 2735 kn->kn_flags |= EV_EOF; 2736 kn->kn_fflags = so->so_error; 2737 return (1); 2738 } else if (so->so_error) /* temporary udp error */ 2739 return (1); 2740 else if (((so->so_state & SS_ISCONNECTED) == 0) && 2741 (so->so_proto->pr_flags & PR_CONNREQUIRED)) 2742 return (0); 2743 else if (kn->kn_sfflags & NOTE_LOWAT) 2744 return (kn->kn_data >= kn->kn_sdata); 2745 else 2746 return (kn->kn_data >= so->so_snd.sb_lowat); 2747 } 2748 2749 /*ARGSUSED*/ 2750 static int 2751 filt_solisten(struct knote *kn, long hint) 2752 { 2753 struct socket *so = kn->kn_fp->f_data; 2754 2755 kn->kn_data = so->so_qlen; 2756 return (! TAILQ_EMPTY(&so->so_comp)); 2757 } 2758 2759 int 2760 socheckuid(struct socket *so, uid_t uid) 2761 { 2762 2763 if (so == NULL) 2764 return (EPERM); 2765 if (so->so_cred->cr_uid != uid) 2766 return (EPERM); 2767 return (0); 2768 } 2769 2770 static int 2771 sysctl_somaxconn(SYSCTL_HANDLER_ARGS) 2772 { 2773 int error; 2774 int val; 2775 2776 val = somaxconn; 2777 error = sysctl_handle_int(oidp, &val, 0, req); 2778 if (error || !req->newptr ) 2779 return (error); 2780 2781 if (val < 1 || val > USHRT_MAX) 2782 return (EINVAL); 2783 2784 somaxconn = val; 2785 return (0); 2786 } 2787 2788 /* 2789 * These functions are used by protocols to notify the socket layer (and its 2790 * consumers) of state changes in the sockets driven by protocol-side events. 2791 */ 2792 2793 /* 2794 * Procedures to manipulate state flags of socket and do appropriate wakeups. 2795 * 2796 * Normal sequence from the active (originating) side is that 2797 * soisconnecting() is called during processing of connect() call, resulting 2798 * in an eventual call to soisconnected() if/when the connection is 2799 * established. When the connection is torn down soisdisconnecting() is 2800 * called during processing of disconnect() call, and soisdisconnected() is 2801 * called when the connection to the peer is totally severed. The semantics 2802 * of these routines are such that connectionless protocols can call 2803 * soisconnected() and soisdisconnected() only, bypassing the in-progress 2804 * calls when setting up a ``connection'' takes no time. 2805 * 2806 * From the passive side, a socket is created with two queues of sockets: 2807 * so_incomp for connections in progress and so_comp for connections already 2808 * made and awaiting user acceptance. As a protocol is preparing incoming 2809 * connections, it creates a socket structure queued on so_incomp by calling 2810 * sonewconn(). When the connection is established, soisconnected() is 2811 * called, and transfers the socket structure to so_comp, making it available 2812 * to accept(). 2813 * 2814 * If a socket is closed with sockets on either so_incomp or so_comp, these 2815 * sockets are dropped. 2816 * 2817 * If higher-level protocols are implemented in the kernel, the wakeups done 2818 * here will sometimes cause software-interrupt process scheduling. 2819 */ 2820 void 2821 soisconnecting(struct socket *so) 2822 { 2823 2824 SOCK_LOCK(so); 2825 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 2826 so->so_state |= SS_ISCONNECTING; 2827 SOCK_UNLOCK(so); 2828 } 2829 2830 void 2831 soisconnected(struct socket *so) 2832 { 2833 struct socket *head; 2834 2835 ACCEPT_LOCK(); 2836 SOCK_LOCK(so); 2837 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 2838 so->so_state |= SS_ISCONNECTED; 2839 head = so->so_head; 2840 if (head != NULL && (so->so_qstate & SQ_INCOMP)) { 2841 if ((so->so_options & SO_ACCEPTFILTER) == 0) { 2842 SOCK_UNLOCK(so); 2843 TAILQ_REMOVE(&head->so_incomp, so, so_list); 2844 head->so_incqlen--; 2845 so->so_qstate &= ~SQ_INCOMP; 2846 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 2847 head->so_qlen++; 2848 so->so_qstate |= SQ_COMP; 2849 ACCEPT_UNLOCK(); 2850 sorwakeup(head); 2851 wakeup_one(&head->so_timeo); 2852 } else { 2853 ACCEPT_UNLOCK(); 2854 so->so_upcall = 2855 head->so_accf->so_accept_filter->accf_callback; 2856 so->so_upcallarg = head->so_accf->so_accept_filter_arg; 2857 so->so_rcv.sb_flags |= SB_UPCALL; 2858 so->so_options &= ~SO_ACCEPTFILTER; 2859 SOCK_UNLOCK(so); 2860 so->so_upcall(so, so->so_upcallarg, M_DONTWAIT); 2861 } 2862 return; 2863 } 2864 SOCK_UNLOCK(so); 2865 ACCEPT_UNLOCK(); 2866 wakeup(&so->so_timeo); 2867 sorwakeup(so); 2868 sowwakeup(so); 2869 } 2870 2871 void 2872 soisdisconnecting(struct socket *so) 2873 { 2874 2875 /* 2876 * Note: This code assumes that SOCK_LOCK(so) and 2877 * SOCKBUF_LOCK(&so->so_rcv) are the same. 2878 */ 2879 SOCKBUF_LOCK(&so->so_rcv); 2880 so->so_state &= ~SS_ISCONNECTING; 2881 so->so_state |= SS_ISDISCONNECTING; 2882 so->so_rcv.sb_state |= SBS_CANTRCVMORE; 2883 sorwakeup_locked(so); 2884 SOCKBUF_LOCK(&so->so_snd); 2885 so->so_snd.sb_state |= SBS_CANTSENDMORE; 2886 sowwakeup_locked(so); 2887 wakeup(&so->so_timeo); 2888 } 2889 2890 void 2891 soisdisconnected(struct socket *so) 2892 { 2893 2894 /* 2895 * Note: This code assumes that SOCK_LOCK(so) and 2896 * SOCKBUF_LOCK(&so->so_rcv) are the same. 2897 */ 2898 SOCKBUF_LOCK(&so->so_rcv); 2899 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 2900 so->so_state |= SS_ISDISCONNECTED; 2901 so->so_rcv.sb_state |= SBS_CANTRCVMORE; 2902 sorwakeup_locked(so); 2903 SOCKBUF_LOCK(&so->so_snd); 2904 so->so_snd.sb_state |= SBS_CANTSENDMORE; 2905 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc); 2906 sowwakeup_locked(so); 2907 wakeup(&so->so_timeo); 2908 } 2909 2910 /* 2911 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. 2912 */ 2913 struct sockaddr * 2914 sodupsockaddr(const struct sockaddr *sa, int mflags) 2915 { 2916 struct sockaddr *sa2; 2917 2918 sa2 = malloc(sa->sa_len, M_SONAME, mflags); 2919 if (sa2) 2920 bcopy(sa, sa2, sa->sa_len); 2921 return sa2; 2922 } 2923 2924 /* 2925 * Create an external-format (``xsocket'') structure using the information in 2926 * the kernel-format socket structure pointed to by so. This is done to 2927 * reduce the spew of irrelevant information over this interface, to isolate 2928 * user code from changes in the kernel structure, and potentially to provide 2929 * information-hiding if we decide that some of this information should be 2930 * hidden from users. 2931 */ 2932 void 2933 sotoxsocket(struct socket *so, struct xsocket *xso) 2934 { 2935 2936 xso->xso_len = sizeof *xso; 2937 xso->xso_so = so; 2938 xso->so_type = so->so_type; 2939 xso->so_options = so->so_options; 2940 xso->so_linger = so->so_linger; 2941 xso->so_state = so->so_state; 2942 xso->so_pcb = so->so_pcb; 2943 xso->xso_protocol = so->so_proto->pr_protocol; 2944 xso->xso_family = so->so_proto->pr_domain->dom_family; 2945 xso->so_qlen = so->so_qlen; 2946 xso->so_incqlen = so->so_incqlen; 2947 xso->so_qlimit = so->so_qlimit; 2948 xso->so_timeo = so->so_timeo; 2949 xso->so_error = so->so_error; 2950 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0; 2951 xso->so_oobmark = so->so_oobmark; 2952 sbtoxsockbuf(&so->so_snd, &xso->so_snd); 2953 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 2954 xso->so_uid = so->so_cred->cr_uid; 2955 } 2956