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
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
36 * Comments on the socket life cycle:
38 * soalloc() sets of socket layer state for a socket, called only by
39 * socreate() and sonewconn(). Socket layer private.
41 * sodealloc() tears down socket layer state for a socket, called only by
42 * sofree() and sonewconn(). Socket layer private.
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.
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.
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
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
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
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.
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.
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
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.
97 #include <sys/cdefs.h>
98 __FBSDID("$FreeBSD$");
100 #include "opt_inet.h"
101 #include "opt_inet6.h"
103 #include "opt_zero.h"
104 #include "opt_compat.h"
106 #include <sys/param.h>
107 #include <sys/systm.h>
108 #include <sys/fcntl.h>
109 #include <sys/limits.h>
110 #include <sys/lock.h>
112 #include <sys/malloc.h>
113 #include <sys/mbuf.h>
114 #include <sys/mutex.h>
115 #include <sys/domain.h>
116 #include <sys/file.h> /* for struct knote */
117 #include <sys/kernel.h>
118 #include <sys/event.h>
119 #include <sys/eventhandler.h>
120 #include <sys/poll.h>
121 #include <sys/proc.h>
122 #include <sys/protosw.h>
123 #include <sys/socket.h>
124 #include <sys/socketvar.h>
125 #include <sys/resourcevar.h>
126 #include <net/route.h>
127 #include <sys/signalvar.h>
128 #include <sys/stat.h>
130 #include <sys/sysctl.h>
132 #include <sys/jail.h>
134 #include <security/mac/mac_framework.h>
139 #include <sys/mount.h>
140 #include <sys/sysent.h>
141 #include <compat/freebsd32/freebsd32.h>
144 static int soreceive_rcvoob(struct socket *so, struct uio *uio,
147 static void filt_sordetach(struct knote *kn);
148 static int filt_soread(struct knote *kn, long hint);
149 static void filt_sowdetach(struct knote *kn);
150 static int filt_sowrite(struct knote *kn, long hint);
151 static int filt_solisten(struct knote *kn, long hint);
153 static struct filterops solisten_filtops =
154 { 1, NULL, filt_sordetach, filt_solisten };
155 static struct filterops soread_filtops =
156 { 1, NULL, filt_sordetach, filt_soread };
157 static struct filterops sowrite_filtops =
158 { 1, NULL, filt_sowdetach, filt_sowrite };
160 uma_zone_t socket_zone;
161 so_gen_t so_gencnt; /* generation count for sockets */
165 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
166 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
168 static int somaxconn = SOMAXCONN;
169 static int sysctl_somaxconn(SYSCTL_HANDLER_ARGS);
170 /* XXX: we dont have SYSCTL_USHORT */
171 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLTYPE_UINT | CTLFLAG_RW,
172 0, sizeof(int), sysctl_somaxconn, "I", "Maximum pending socket connection "
174 static int numopensockets;
175 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
176 &numopensockets, 0, "Number of open sockets");
177 #ifdef ZERO_COPY_SOCKETS
178 /* These aren't static because they're used in other files. */
179 int so_zero_copy_send = 1;
180 int so_zero_copy_receive = 1;
181 SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0,
182 "Zero copy controls");
183 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW,
184 &so_zero_copy_receive, 0, "Enable zero copy receive");
185 SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW,
186 &so_zero_copy_send, 0, "Enable zero copy send");
187 #endif /* ZERO_COPY_SOCKETS */
190 * accept_mtx locks down per-socket fields relating to accept queues. See
191 * socketvar.h for an annotation of the protected fields of struct socket.
193 struct mtx accept_mtx;
194 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
197 * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
200 static struct mtx so_global_mtx;
201 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
204 * General IPC sysctl name space, used by sockets and a variety of other IPC
207 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
210 * Sysctl to get and set the maximum global sockets limit. Notify protocols
211 * of the change so that they can update their dependent limits as required.
214 sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
216 int error, newmaxsockets;
218 newmaxsockets = maxsockets;
219 error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
220 if (error == 0 && req->newptr) {
221 if (newmaxsockets > maxsockets) {
222 maxsockets = newmaxsockets;
223 if (maxsockets > ((maxfiles / 4) * 3)) {
224 maxfiles = (maxsockets * 5) / 4;
225 maxfilesperproc = (maxfiles * 9) / 10;
227 EVENTHANDLER_INVOKE(maxsockets_change);
234 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW,
235 &maxsockets, 0, sysctl_maxsockets, "IU",
236 "Maximum number of sockets avaliable");
239 * Initialise maxsockets. This SYSINIT must be run after
243 init_maxsockets(void *ignored)
246 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
247 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
249 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
252 * Socket operation routines. These routines are called by the routines in
253 * sys_socket.c or from a system process, and implement the semantics of
254 * socket operations by switching out to the protocol specific routines.
258 * Get a socket structure from our zone, and initialize it. Note that it
259 * would probably be better to allocate socket and PCB at the same time, but
260 * I'm not convinced that all the protocols can be easily modified to do
263 * soalloc() returns a socket with a ref count of 0.
265 static struct socket *
270 so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO);
274 if (mac_socket_init(so, M_NOWAIT) != 0) {
275 uma_zfree(socket_zone, so);
279 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
280 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
281 sx_init(&so->so_snd.sb_sx, "so_snd_sx");
282 sx_init(&so->so_rcv.sb_sx, "so_rcv_sx");
283 TAILQ_INIT(&so->so_aiojobq);
284 mtx_lock(&so_global_mtx);
285 so->so_gencnt = ++so_gencnt;
287 mtx_unlock(&so_global_mtx);
292 * Free the storage associated with a socket at the socket layer, tear down
293 * locks, labels, etc. All protocol state is assumed already to have been
294 * torn down (and possibly never set up) by the caller.
297 sodealloc(struct socket *so)
300 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
301 KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
303 mtx_lock(&so_global_mtx);
304 so->so_gencnt = ++so_gencnt;
305 --numopensockets; /* Could be below, but faster here. */
306 mtx_unlock(&so_global_mtx);
307 if (so->so_rcv.sb_hiwat)
308 (void)chgsbsize(so->so_cred->cr_uidinfo,
309 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
310 if (so->so_snd.sb_hiwat)
311 (void)chgsbsize(so->so_cred->cr_uidinfo,
312 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
314 /* remove acccept filter if one is present. */
315 if (so->so_accf != NULL)
316 do_setopt_accept_filter(so, NULL);
319 mac_socket_destroy(so);
322 sx_destroy(&so->so_snd.sb_sx);
323 sx_destroy(&so->so_rcv.sb_sx);
324 SOCKBUF_LOCK_DESTROY(&so->so_snd);
325 SOCKBUF_LOCK_DESTROY(&so->so_rcv);
326 uma_zfree(socket_zone, so);
330 * socreate returns a socket with a ref count of 1. The socket should be
331 * closed with soclose().
334 socreate(int dom, struct socket **aso, int type, int proto,
335 struct ucred *cred, struct thread *td)
342 prp = pffindproto(dom, proto, type);
344 prp = pffindtype(dom, type);
346 if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL ||
347 prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
348 return (EPROTONOSUPPORT);
350 if (prison_check_af(cred, prp->pr_domain->dom_family) != 0)
351 return (EPROTONOSUPPORT);
353 if (prp->pr_type != type)
359 TAILQ_INIT(&so->so_incomp);
360 TAILQ_INIT(&so->so_comp);
362 so->so_cred = crhold(cred);
363 if ((prp->pr_domain->dom_family == PF_INET) ||
364 (prp->pr_domain->dom_family == PF_ROUTE))
365 so->so_fibnum = td->td_proc->p_fibnum;
370 mac_socket_create(cred, so);
372 knlist_init(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv),
374 knlist_init(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd),
378 * Auto-sizing of socket buffers is managed by the protocols and
379 * the appropriate flags must be set in the pru_attach function.
381 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
383 KASSERT(so->so_count == 1, ("socreate: so_count %d",
394 static int regression_sonewconn_earlytest = 1;
395 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
396 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
400 * When an attempt at a new connection is noted on a socket which accepts
401 * connections, sonewconn is called. If the connection is possible (subject
402 * to space constraints, etc.) then we allocate a new structure, propoerly
403 * linked into the data structure of the original socket, and return this.
404 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
406 * Note: the ref count on the socket is 0 on return.
409 sonewconn(struct socket *head, int connstatus)
415 over = (head->so_qlen > 3 * head->so_qlimit / 2);
418 if (regression_sonewconn_earlytest && over)
426 if ((head->so_options & SO_ACCEPTFILTER) != 0)
429 so->so_type = head->so_type;
430 so->so_options = head->so_options &~ SO_ACCEPTCONN;
431 so->so_linger = head->so_linger;
432 so->so_state = head->so_state | SS_NOFDREF;
433 so->so_proto = head->so_proto;
434 so->so_cred = crhold(head->so_cred);
437 mac_socket_newconn(head, so);
440 knlist_init(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv),
442 knlist_init(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd),
444 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
445 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
449 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
450 so->so_snd.sb_lowat = head->so_snd.sb_lowat;
451 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
452 so->so_snd.sb_timeo = head->so_snd.sb_timeo;
453 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
454 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
455 so->so_state |= connstatus;
458 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
459 so->so_qstate |= SQ_COMP;
463 * Keep removing sockets from the head until there's room for
464 * us to insert on the tail. In pre-locking revisions, this
465 * was a simple if(), but as we could be racing with other
466 * threads and soabort() requires dropping locks, we must
467 * loop waiting for the condition to be true.
469 while (head->so_incqlen > head->so_qlimit) {
471 sp = TAILQ_FIRST(&head->so_incomp);
472 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
474 sp->so_qstate &= ~SQ_INCOMP;
480 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
481 so->so_qstate |= SQ_INCOMP;
487 wakeup_one(&head->so_timeo);
493 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
496 return ((*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td));
500 * solisten() transitions a socket from a non-listening state to a listening
501 * state, but can also be used to update the listen queue depth on an
502 * existing listen socket. The protocol will call back into the sockets
503 * layer using solisten_proto_check() and solisten_proto() to check and set
504 * socket-layer listen state. Call backs are used so that the protocol can
505 * acquire both protocol and socket layer locks in whatever order is required
508 * Protocol implementors are advised to hold the socket lock across the
509 * socket-layer test and set to avoid races at the socket layer.
512 solisten(struct socket *so, int backlog, struct thread *td)
515 return ((*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td));
519 solisten_proto_check(struct socket *so)
522 SOCK_LOCK_ASSERT(so);
524 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
531 solisten_proto(struct socket *so, int backlog)
534 SOCK_LOCK_ASSERT(so);
536 if (backlog < 0 || backlog > somaxconn)
538 so->so_qlimit = backlog;
539 so->so_options |= SO_ACCEPTCONN;
543 * Attempt to free a socket. This should really be sotryfree().
545 * sofree() will succeed if:
547 * - There are no outstanding file descriptor references or related consumers
550 * - The socket has been closed by user space, if ever open (SS_NOFDREF).
552 * - The protocol does not have an outstanding strong reference on the socket
555 * - The socket is not in a completed connection queue, so a process has been
556 * notified that it is present. If it is removed, the user process may
557 * block in accept() despite select() saying the socket was ready.
559 * Otherwise, it will quietly abort so that a future call to sofree(), when
560 * conditions are right, can succeed.
563 sofree(struct socket *so)
565 struct protosw *pr = so->so_proto;
568 ACCEPT_LOCK_ASSERT();
569 SOCK_LOCK_ASSERT(so);
571 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
572 (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
580 KASSERT((so->so_qstate & SQ_COMP) != 0 ||
581 (so->so_qstate & SQ_INCOMP) != 0,
582 ("sofree: so_head != NULL, but neither SQ_COMP nor "
584 KASSERT((so->so_qstate & SQ_COMP) == 0 ||
585 (so->so_qstate & SQ_INCOMP) == 0,
586 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
587 TAILQ_REMOVE(&head->so_incomp, so, so_list);
589 so->so_qstate &= ~SQ_INCOMP;
592 KASSERT((so->so_qstate & SQ_COMP) == 0 &&
593 (so->so_qstate & SQ_INCOMP) == 0,
594 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
595 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
596 if (so->so_options & SO_ACCEPTCONN) {
597 KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
598 KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated"));
603 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
604 (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
605 if (pr->pr_usrreqs->pru_detach != NULL)
606 (*pr->pr_usrreqs->pru_detach)(so);
609 * From this point on, we assume that no other references to this
610 * socket exist anywhere else in the stack. Therefore, no locks need
611 * to be acquired or held.
613 * We used to do a lot of socket buffer and socket locking here, as
614 * well as invoke sorflush() and perform wakeups. The direct call to
615 * dom_dispose() and sbrelease_internal() are an inlining of what was
616 * necessary from sorflush().
618 * Notice that the socket buffer and kqueue state are torn down
619 * before calling pru_detach. This means that protocols shold not
620 * assume they can perform socket wakeups, etc, in their detach code.
622 sbdestroy(&so->so_snd, so);
623 sbdestroy(&so->so_rcv, so);
624 knlist_destroy(&so->so_rcv.sb_sel.si_note);
625 knlist_destroy(&so->so_snd.sb_sel.si_note);
630 * Close a socket on last file table reference removal. Initiate disconnect
631 * if connected. Free socket when disconnect complete.
633 * This function will sorele() the socket. Note that soclose() may be called
634 * prior to the ref count reaching zero. The actual socket structure will
635 * not be freed until the ref count reaches zero.
638 soclose(struct socket *so)
642 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
644 funsetown(&so->so_sigio);
645 if (so->so_state & SS_ISCONNECTED) {
646 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
647 error = sodisconnect(so);
651 if (so->so_options & SO_LINGER) {
652 if ((so->so_state & SS_ISDISCONNECTING) &&
653 (so->so_state & SS_NBIO))
655 while (so->so_state & SS_ISCONNECTED) {
656 error = tsleep(&so->so_timeo,
657 PSOCK | PCATCH, "soclos", so->so_linger * hz);
665 if (so->so_proto->pr_usrreqs->pru_close != NULL)
666 (*so->so_proto->pr_usrreqs->pru_close)(so);
667 if (so->so_options & SO_ACCEPTCONN) {
670 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
671 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
673 sp->so_qstate &= ~SQ_INCOMP;
679 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
680 TAILQ_REMOVE(&so->so_comp, sp, so_list);
682 sp->so_qstate &= ~SQ_COMP;
692 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
693 so->so_state |= SS_NOFDREF;
699 * soabort() is used to abruptly tear down a connection, such as when a
700 * resource limit is reached (listen queue depth exceeded), or if a listen
701 * socket is closed while there are sockets waiting to be accepted.
703 * This interface is tricky, because it is called on an unreferenced socket,
704 * and must be called only by a thread that has actually removed the socket
705 * from the listen queue it was on, or races with other threads are risked.
707 * This interface will call into the protocol code, so must not be called
708 * with any socket locks held. Protocols do call it while holding their own
709 * recursible protocol mutexes, but this is something that should be subject
710 * to review in the future.
713 soabort(struct socket *so)
717 * In as much as is possible, assert that no references to this
718 * socket are held. This is not quite the same as asserting that the
719 * current thread is responsible for arranging for no references, but
720 * is as close as we can get for now.
722 KASSERT(so->so_count == 0, ("soabort: so_count"));
723 KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
724 KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
725 KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP"));
726 KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP"));
728 if (so->so_proto->pr_usrreqs->pru_abort != NULL)
729 (*so->so_proto->pr_usrreqs->pru_abort)(so);
736 soaccept(struct socket *so, struct sockaddr **nam)
741 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
742 so->so_state &= ~SS_NOFDREF;
744 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
749 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
753 if (so->so_options & SO_ACCEPTCONN)
756 * If protocol is connection-based, can only connect once.
757 * Otherwise, if connected, try to disconnect first. This allows
758 * user to disconnect by connecting to, e.g., a null address.
760 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
761 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
762 (error = sodisconnect(so)))) {
766 * Prevent accumulated error from previous connection from
770 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
777 soconnect2(struct socket *so1, struct socket *so2)
780 return ((*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2));
784 sodisconnect(struct socket *so)
788 if ((so->so_state & SS_ISCONNECTED) == 0)
790 if (so->so_state & SS_ISDISCONNECTING)
792 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
796 #ifdef ZERO_COPY_SOCKETS
797 struct so_zerocopy_stats{
802 struct so_zerocopy_stats so_zerocp_stats = {0,0,0};
803 #include <netinet/in.h>
804 #include <net/route.h>
805 #include <netinet/in_pcb.h>
807 #include <vm/vm_page.h>
808 #include <vm/vm_object.h>
811 * sosend_copyin() is only used if zero copy sockets are enabled. Otherwise
812 * sosend_dgram() and sosend_generic() use m_uiotombuf().
814 * sosend_copyin() accepts a uio and prepares an mbuf chain holding part or
815 * all of the data referenced by the uio. If desired, it uses zero-copy.
816 * *space will be updated to reflect data copied in.
818 * NB: If atomic I/O is requested, the caller must already have checked that
819 * space can hold resid bytes.
821 * NB: In the event of an error, the caller may need to free the partial
822 * chain pointed to by *mpp. The contents of both *uio and *space may be
823 * modified even in the case of an error.
826 sosend_copyin(struct uio *uio, struct mbuf **retmp, int atomic, long *space,
829 struct mbuf *m, **mp, *top;
832 #ifdef ZERO_COPY_SOCKETS
839 resid = uio->uio_resid;
842 #ifdef ZERO_COPY_SOCKETS
844 #endif /* ZERO_COPY_SOCKETS */
845 if (resid >= MINCLSIZE) {
846 #ifdef ZERO_COPY_SOCKETS
848 m = m_gethdr(M_WAITOK, MT_DATA);
850 m->m_pkthdr.rcvif = NULL;
852 m = m_get(M_WAITOK, MT_DATA);
853 if (so_zero_copy_send &&
856 uio->uio_iov->iov_len>=PAGE_SIZE) {
857 so_zerocp_stats.size_ok++;
858 so_zerocp_stats.align_ok++;
859 cow_send = socow_setup(m, uio);
863 m_clget(m, M_WAITOK);
864 len = min(min(MCLBYTES, resid), *space);
866 #else /* ZERO_COPY_SOCKETS */
868 m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
870 m->m_pkthdr.rcvif = NULL;
872 m = m_getcl(M_WAIT, MT_DATA, 0);
873 len = min(min(MCLBYTES, resid), *space);
874 #endif /* ZERO_COPY_SOCKETS */
877 m = m_gethdr(M_WAIT, MT_DATA);
879 m->m_pkthdr.rcvif = NULL;
881 len = min(min(MHLEN, resid), *space);
883 * For datagram protocols, leave room
884 * for protocol headers in first mbuf.
886 if (atomic && m && len < MHLEN)
889 m = m_get(M_WAIT, MT_DATA);
890 len = min(min(MLEN, resid), *space);
899 #ifdef ZERO_COPY_SOCKETS
903 #endif /* ZERO_COPY_SOCKETS */
904 error = uiomove(mtod(m, void *), (int)len, uio);
905 resid = uio->uio_resid;
908 top->m_pkthdr.len += len;
914 top->m_flags |= M_EOR;
917 } while (*space > 0 && atomic);
922 #endif /*ZERO_COPY_SOCKETS*/
924 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
927 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
928 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
931 int clen = 0, error, dontroute;
932 #ifdef ZERO_COPY_SOCKETS
933 int atomic = sosendallatonce(so) || top;
936 KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM"));
937 KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
938 ("sodgram_send: !PR_ATOMIC"));
941 resid = uio->uio_resid;
943 resid = top->m_pkthdr.len;
945 * In theory resid should be unsigned. However, space must be
946 * signed, as it might be less than 0 if we over-committed, and we
947 * must use a signed comparison of space and resid. On the other
948 * hand, a negative resid causes us to loop sending 0-length
949 * segments to the protocol.
951 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
952 * type sockets since that's an error.
960 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
962 td->td_ru.ru_msgsnd++;
964 clen = control->m_len;
966 SOCKBUF_LOCK(&so->so_snd);
967 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
968 SOCKBUF_UNLOCK(&so->so_snd);
973 error = so->so_error;
975 SOCKBUF_UNLOCK(&so->so_snd);
978 if ((so->so_state & SS_ISCONNECTED) == 0) {
980 * `sendto' and `sendmsg' is allowed on a connection-based
981 * socket if it supports implied connect. Return ENOTCONN if
982 * not connected and no address is supplied.
984 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
985 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
986 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
987 !(resid == 0 && clen != 0)) {
988 SOCKBUF_UNLOCK(&so->so_snd);
992 } else if (addr == NULL) {
993 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
996 error = EDESTADDRREQ;
997 SOCKBUF_UNLOCK(&so->so_snd);
1003 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a
1004 * problem and need fixing.
1006 space = sbspace(&so->so_snd);
1007 if (flags & MSG_OOB)
1010 SOCKBUF_UNLOCK(&so->so_snd);
1011 if (resid > space) {
1017 if (flags & MSG_EOR)
1018 top->m_flags |= M_EOR;
1020 #ifdef ZERO_COPY_SOCKETS
1021 error = sosend_copyin(uio, &top, atomic, &space, flags);
1026 * Copy the data from userland into a mbuf chain.
1027 * If no data is to be copied in, a single empty mbuf
1030 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
1031 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
1033 error = EFAULT; /* only possible error */
1036 space -= resid - uio->uio_resid;
1038 resid = uio->uio_resid;
1040 KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
1042 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
1047 so->so_options |= SO_DONTROUTE;
1051 * XXX all the SBS_CANTSENDMORE checks previously done could be out
1052 * of date. We could have recieved a reset packet in an interrupt or
1053 * maybe we slept while doing page faults in uiomove() etc. We could
1054 * probably recheck again inside the locking protection here, but
1055 * there are probably other places that this also happens. We must
1058 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1059 (flags & MSG_OOB) ? PRUS_OOB :
1061 * If the user set MSG_EOF, the protocol understands this flag and
1062 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
1064 ((flags & MSG_EOF) &&
1065 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1068 /* If there is more to send set PRUS_MORETOCOME */
1069 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1070 top, addr, control, td);
1073 so->so_options &= ~SO_DONTROUTE;
1082 if (control != NULL)
1088 * Send on a socket. If send must go all at once and message is larger than
1089 * send buffering, then hard error. Lock against other senders. If must go
1090 * all at once and not enough room now, then inform user that this would
1091 * block and do nothing. Otherwise, if nonblocking, send as much as
1092 * possible. The data to be sent is described by "uio" if nonzero, otherwise
1093 * by the mbuf chain "top" (which must be null if uio is not). Data provided
1094 * in mbuf chain must be small enough to send all at once.
1096 * Returns nonzero on error, timeout or signal; callers must check for short
1097 * counts if EINTR/ERESTART are returned. Data and control buffers are freed
1101 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
1102 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1105 int clen = 0, error, dontroute;
1106 int atomic = sosendallatonce(so) || top;
1109 resid = uio->uio_resid;
1111 resid = top->m_pkthdr.len;
1113 * In theory resid should be unsigned. However, space must be
1114 * signed, as it might be less than 0 if we over-committed, and we
1115 * must use a signed comparison of space and resid. On the other
1116 * hand, a negative resid causes us to loop sending 0-length
1117 * segments to the protocol.
1119 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1120 * type sockets since that's an error.
1122 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
1128 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
1129 (so->so_proto->pr_flags & PR_ATOMIC);
1131 td->td_ru.ru_msgsnd++;
1132 if (control != NULL)
1133 clen = control->m_len;
1135 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
1141 SOCKBUF_LOCK(&so->so_snd);
1142 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1143 SOCKBUF_UNLOCK(&so->so_snd);
1148 error = so->so_error;
1150 SOCKBUF_UNLOCK(&so->so_snd);
1153 if ((so->so_state & SS_ISCONNECTED) == 0) {
1155 * `sendto' and `sendmsg' is allowed on a connection-
1156 * based socket if it supports implied connect.
1157 * Return ENOTCONN if not connected and no address is
1160 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1161 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1162 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1163 !(resid == 0 && clen != 0)) {
1164 SOCKBUF_UNLOCK(&so->so_snd);
1168 } else if (addr == NULL) {
1169 SOCKBUF_UNLOCK(&so->so_snd);
1170 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1173 error = EDESTADDRREQ;
1177 space = sbspace(&so->so_snd);
1178 if (flags & MSG_OOB)
1180 if ((atomic && resid > so->so_snd.sb_hiwat) ||
1181 clen > so->so_snd.sb_hiwat) {
1182 SOCKBUF_UNLOCK(&so->so_snd);
1186 if (space < resid + clen &&
1187 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1188 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
1189 SOCKBUF_UNLOCK(&so->so_snd);
1190 error = EWOULDBLOCK;
1193 error = sbwait(&so->so_snd);
1194 SOCKBUF_UNLOCK(&so->so_snd);
1199 SOCKBUF_UNLOCK(&so->so_snd);
1204 if (flags & MSG_EOR)
1205 top->m_flags |= M_EOR;
1207 #ifdef ZERO_COPY_SOCKETS
1208 error = sosend_copyin(uio, &top, atomic,
1214 * Copy the data from userland into a mbuf
1215 * chain. If no data is to be copied in,
1216 * a single empty mbuf is returned.
1218 top = m_uiotombuf(uio, M_WAITOK, space,
1219 (atomic ? max_hdr : 0),
1220 (atomic ? M_PKTHDR : 0) |
1221 ((flags & MSG_EOR) ? M_EOR : 0));
1223 error = EFAULT; /* only possible error */
1226 space -= resid - uio->uio_resid;
1228 resid = uio->uio_resid;
1232 so->so_options |= SO_DONTROUTE;
1236 * XXX all the SBS_CANTSENDMORE checks previously
1237 * done could be out of date. We could have recieved
1238 * a reset packet in an interrupt or maybe we slept
1239 * while doing page faults in uiomove() etc. We
1240 * could probably recheck again inside the locking
1241 * protection here, but there are probably other
1242 * places that this also happens. We must rethink
1245 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1246 (flags & MSG_OOB) ? PRUS_OOB :
1248 * If the user set MSG_EOF, the protocol understands
1249 * this flag and nothing left to send then use
1250 * PRU_SEND_EOF instead of PRU_SEND.
1252 ((flags & MSG_EOF) &&
1253 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1256 /* If there is more to send set PRUS_MORETOCOME. */
1257 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1258 top, addr, control, td);
1261 so->so_options &= ~SO_DONTROUTE;
1269 } while (resid && space > 0);
1273 sbunlock(&so->so_snd);
1277 if (control != NULL)
1283 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1284 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1287 return (so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
1288 control, flags, td));
1292 * The part of soreceive() that implements reading non-inline out-of-band
1293 * data from a socket. For more complete comments, see soreceive(), from
1294 * which this code originated.
1296 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1297 * unable to return an mbuf chain to the caller.
1300 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
1302 struct protosw *pr = so->so_proto;
1306 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1308 m = m_get(M_WAIT, MT_DATA);
1309 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
1313 #ifdef ZERO_COPY_SOCKETS
1314 if (so_zero_copy_receive) {
1317 if ((m->m_flags & M_EXT)
1318 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1323 error = uiomoveco(mtod(m, void *),
1324 min(uio->uio_resid, m->m_len),
1327 #endif /* ZERO_COPY_SOCKETS */
1328 error = uiomove(mtod(m, void *),
1329 (int) min(uio->uio_resid, m->m_len), uio);
1331 } while (uio->uio_resid && error == 0 && m);
1339 * Following replacement or removal of the first mbuf on the first mbuf chain
1340 * of a socket buffer, push necessary state changes back into the socket
1341 * buffer so that other consumers see the values consistently. 'nextrecord'
1342 * is the callers locally stored value of the original value of
1343 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
1344 * NOTE: 'nextrecord' may be NULL.
1346 static __inline void
1347 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
1350 SOCKBUF_LOCK_ASSERT(sb);
1352 * First, update for the new value of nextrecord. If necessary, make
1353 * it the first record.
1355 if (sb->sb_mb != NULL)
1356 sb->sb_mb->m_nextpkt = nextrecord;
1358 sb->sb_mb = nextrecord;
1361 * Now update any dependent socket buffer fields to reflect the new
1362 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the
1363 * addition of a second clause that takes care of the case where
1364 * sb_mb has been updated, but remains the last record.
1366 if (sb->sb_mb == NULL) {
1367 sb->sb_mbtail = NULL;
1368 sb->sb_lastrecord = NULL;
1369 } else if (sb->sb_mb->m_nextpkt == NULL)
1370 sb->sb_lastrecord = sb->sb_mb;
1375 * Implement receive operations on a socket. We depend on the way that
1376 * records are added to the sockbuf by sbappend. In particular, each record
1377 * (mbufs linked through m_next) must begin with an address if the protocol
1378 * so specifies, followed by an optional mbuf or mbufs containing ancillary
1379 * data, and then zero or more mbufs of data. In order to allow parallelism
1380 * between network receive and copying to user space, as well as avoid
1381 * sleeping with a mutex held, we release the socket buffer mutex during the
1382 * user space copy. Although the sockbuf is locked, new data may still be
1383 * appended, and thus we must maintain consistency of the sockbuf during that
1386 * The caller may receive the data as a single mbuf chain by supplying an
1387 * mbuf **mp0 for use in returning the chain. The uio is then used only for
1388 * the count in uio_resid.
1391 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
1392 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1394 struct mbuf *m, **mp;
1395 int flags, len, error, offset;
1396 struct protosw *pr = so->so_proto;
1397 struct mbuf *nextrecord;
1399 int orig_resid = uio->uio_resid;
1404 if (controlp != NULL)
1407 flags = *flagsp &~ MSG_EOR;
1410 if (flags & MSG_OOB)
1411 return (soreceive_rcvoob(so, uio, flags));
1414 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
1416 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
1418 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1423 SOCKBUF_LOCK(&so->so_rcv);
1424 m = so->so_rcv.sb_mb;
1426 * If we have less data than requested, block awaiting more (subject
1427 * to any timeout) if:
1428 * 1. the current count is less than the low water mark, or
1429 * 2. MSG_WAITALL is set, and it is possible to do the entire
1430 * receive operation at once if we block (resid <= hiwat).
1431 * 3. MSG_DONTWAIT is not set
1432 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1433 * we have to do the receive in sections, and thus risk returning a
1434 * short count if a timeout or signal occurs after we start.
1436 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1437 so->so_rcv.sb_cc < uio->uio_resid) &&
1438 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
1439 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
1440 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
1441 KASSERT(m != NULL || !so->so_rcv.sb_cc,
1442 ("receive: m == %p so->so_rcv.sb_cc == %u",
1443 m, so->so_rcv.sb_cc));
1447 error = so->so_error;
1448 if ((flags & MSG_PEEK) == 0)
1450 SOCKBUF_UNLOCK(&so->so_rcv);
1453 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1454 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1456 SOCKBUF_UNLOCK(&so->so_rcv);
1461 for (; m != NULL; m = m->m_next)
1462 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1463 m = so->so_rcv.sb_mb;
1466 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1467 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1468 SOCKBUF_UNLOCK(&so->so_rcv);
1472 if (uio->uio_resid == 0) {
1473 SOCKBUF_UNLOCK(&so->so_rcv);
1476 if ((so->so_state & SS_NBIO) ||
1477 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1478 SOCKBUF_UNLOCK(&so->so_rcv);
1479 error = EWOULDBLOCK;
1482 SBLASTRECORDCHK(&so->so_rcv);
1483 SBLASTMBUFCHK(&so->so_rcv);
1484 error = sbwait(&so->so_rcv);
1485 SOCKBUF_UNLOCK(&so->so_rcv);
1492 * From this point onward, we maintain 'nextrecord' as a cache of the
1493 * pointer to the next record in the socket buffer. We must keep the
1494 * various socket buffer pointers and local stack versions of the
1495 * pointers in sync, pushing out modifications before dropping the
1496 * socket buffer mutex, and re-reading them when picking it up.
1498 * Otherwise, we will race with the network stack appending new data
1499 * or records onto the socket buffer by using inconsistent/stale
1500 * versions of the field, possibly resulting in socket buffer
1503 * By holding the high-level sblock(), we prevent simultaneous
1504 * readers from pulling off the front of the socket buffer.
1506 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1508 uio->uio_td->td_ru.ru_msgrcv++;
1509 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
1510 SBLASTRECORDCHK(&so->so_rcv);
1511 SBLASTMBUFCHK(&so->so_rcv);
1512 nextrecord = m->m_nextpkt;
1513 if (pr->pr_flags & PR_ADDR) {
1514 KASSERT(m->m_type == MT_SONAME,
1515 ("m->m_type == %d", m->m_type));
1518 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
1520 if (flags & MSG_PEEK) {
1523 sbfree(&so->so_rcv, m);
1524 so->so_rcv.sb_mb = m_free(m);
1525 m = so->so_rcv.sb_mb;
1526 sockbuf_pushsync(&so->so_rcv, nextrecord);
1531 * Process one or more MT_CONTROL mbufs present before any data mbufs
1532 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1533 * just copy the data; if !MSG_PEEK, we call into the protocol to
1534 * perform externalization (or freeing if controlp == NULL).
1536 if (m != NULL && m->m_type == MT_CONTROL) {
1537 struct mbuf *cm = NULL, *cmn;
1538 struct mbuf **cme = &cm;
1541 if (flags & MSG_PEEK) {
1542 if (controlp != NULL) {
1543 *controlp = m_copy(m, 0, m->m_len);
1544 controlp = &(*controlp)->m_next;
1548 sbfree(&so->so_rcv, m);
1549 so->so_rcv.sb_mb = m->m_next;
1552 cme = &(*cme)->m_next;
1553 m = so->so_rcv.sb_mb;
1555 } while (m != NULL && m->m_type == MT_CONTROL);
1556 if ((flags & MSG_PEEK) == 0)
1557 sockbuf_pushsync(&so->so_rcv, nextrecord);
1558 while (cm != NULL) {
1561 if (pr->pr_domain->dom_externalize != NULL) {
1562 SOCKBUF_UNLOCK(&so->so_rcv);
1563 error = (*pr->pr_domain->dom_externalize)
1565 SOCKBUF_LOCK(&so->so_rcv);
1566 } else if (controlp != NULL)
1570 if (controlp != NULL) {
1572 while (*controlp != NULL)
1573 controlp = &(*controlp)->m_next;
1578 nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1580 nextrecord = so->so_rcv.sb_mb;
1584 if ((flags & MSG_PEEK) == 0) {
1585 KASSERT(m->m_nextpkt == nextrecord,
1586 ("soreceive: post-control, nextrecord !sync"));
1587 if (nextrecord == NULL) {
1588 KASSERT(so->so_rcv.sb_mb == m,
1589 ("soreceive: post-control, sb_mb!=m"));
1590 KASSERT(so->so_rcv.sb_lastrecord == m,
1591 ("soreceive: post-control, lastrecord!=m"));
1595 if (type == MT_OOBDATA)
1598 if ((flags & MSG_PEEK) == 0) {
1599 KASSERT(so->so_rcv.sb_mb == nextrecord,
1600 ("soreceive: sb_mb != nextrecord"));
1601 if (so->so_rcv.sb_mb == NULL) {
1602 KASSERT(so->so_rcv.sb_lastrecord == NULL,
1603 ("soreceive: sb_lastercord != NULL"));
1607 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1608 SBLASTRECORDCHK(&so->so_rcv);
1609 SBLASTMBUFCHK(&so->so_rcv);
1612 * Now continue to read any data mbufs off of the head of the socket
1613 * buffer until the read request is satisfied. Note that 'type' is
1614 * used to store the type of any mbuf reads that have happened so far
1615 * such that soreceive() can stop reading if the type changes, which
1616 * causes soreceive() to return only one of regular data and inline
1617 * out-of-band data in a single socket receive operation.
1621 while (m != NULL && uio->uio_resid > 0 && error == 0) {
1623 * If the type of mbuf has changed since the last mbuf
1624 * examined ('type'), end the receive operation.
1626 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1627 if (m->m_type == MT_OOBDATA) {
1628 if (type != MT_OOBDATA)
1630 } else if (type == MT_OOBDATA)
1633 KASSERT(m->m_type == MT_DATA,
1634 ("m->m_type == %d", m->m_type));
1635 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
1636 len = uio->uio_resid;
1637 if (so->so_oobmark && len > so->so_oobmark - offset)
1638 len = so->so_oobmark - offset;
1639 if (len > m->m_len - moff)
1640 len = m->m_len - moff;
1642 * If mp is set, just pass back the mbufs. Otherwise copy
1643 * them out via the uio, then free. Sockbuf must be
1644 * consistent here (points to current mbuf, it points to next
1645 * record) when we drop priority; we must note any additions
1646 * to the sockbuf when we block interrupts again.
1649 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1650 SBLASTRECORDCHK(&so->so_rcv);
1651 SBLASTMBUFCHK(&so->so_rcv);
1652 SOCKBUF_UNLOCK(&so->so_rcv);
1653 #ifdef ZERO_COPY_SOCKETS
1654 if (so_zero_copy_receive) {
1657 if ((m->m_flags & M_EXT)
1658 && (m->m_ext.ext_type == EXT_DISPOSABLE))
1663 error = uiomoveco(mtod(m, char *) + moff,
1667 #endif /* ZERO_COPY_SOCKETS */
1668 error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1669 SOCKBUF_LOCK(&so->so_rcv);
1672 * The MT_SONAME mbuf has already been removed
1673 * from the record, so it is necessary to
1674 * remove the data mbufs, if any, to preserve
1675 * the invariant in the case of PR_ADDR that
1676 * requires MT_SONAME mbufs at the head of
1679 if (m && pr->pr_flags & PR_ATOMIC &&
1680 ((flags & MSG_PEEK) == 0))
1681 (void)sbdroprecord_locked(&so->so_rcv);
1682 SOCKBUF_UNLOCK(&so->so_rcv);
1686 uio->uio_resid -= len;
1687 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1688 if (len == m->m_len - moff) {
1689 if (m->m_flags & M_EOR)
1691 if (flags & MSG_PEEK) {
1695 nextrecord = m->m_nextpkt;
1696 sbfree(&so->so_rcv, m);
1700 so->so_rcv.sb_mb = m = m->m_next;
1703 so->so_rcv.sb_mb = m_free(m);
1704 m = so->so_rcv.sb_mb;
1706 sockbuf_pushsync(&so->so_rcv, nextrecord);
1707 SBLASTRECORDCHK(&so->so_rcv);
1708 SBLASTMBUFCHK(&so->so_rcv);
1711 if (flags & MSG_PEEK)
1717 if (flags & MSG_DONTWAIT)
1718 copy_flag = M_DONTWAIT;
1721 if (copy_flag == M_WAIT)
1722 SOCKBUF_UNLOCK(&so->so_rcv);
1723 *mp = m_copym(m, 0, len, copy_flag);
1724 if (copy_flag == M_WAIT)
1725 SOCKBUF_LOCK(&so->so_rcv);
1728 * m_copym() couldn't
1729 * allocate an mbuf. Adjust
1730 * uio_resid back (it was
1731 * adjusted down by len
1732 * bytes, which we didn't end
1733 * up "copying" over).
1735 uio->uio_resid += len;
1741 so->so_rcv.sb_cc -= len;
1744 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1745 if (so->so_oobmark) {
1746 if ((flags & MSG_PEEK) == 0) {
1747 so->so_oobmark -= len;
1748 if (so->so_oobmark == 0) {
1749 so->so_rcv.sb_state |= SBS_RCVATMARK;
1754 if (offset == so->so_oobmark)
1758 if (flags & MSG_EOR)
1761 * If the MSG_WAITALL flag is set (for non-atomic socket), we
1762 * must not quit until "uio->uio_resid == 0" or an error
1763 * termination. If a signal/timeout occurs, return with a
1764 * short count but without error. Keep sockbuf locked
1765 * against other readers.
1767 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1768 !sosendallatonce(so) && nextrecord == NULL) {
1769 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1770 if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
1773 * Notify the protocol that some data has been
1774 * drained before blocking.
1776 if (pr->pr_flags & PR_WANTRCVD) {
1777 SOCKBUF_UNLOCK(&so->so_rcv);
1778 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1779 SOCKBUF_LOCK(&so->so_rcv);
1781 SBLASTRECORDCHK(&so->so_rcv);
1782 SBLASTMBUFCHK(&so->so_rcv);
1783 error = sbwait(&so->so_rcv);
1785 SOCKBUF_UNLOCK(&so->so_rcv);
1788 m = so->so_rcv.sb_mb;
1790 nextrecord = m->m_nextpkt;
1794 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1795 if (m != NULL && pr->pr_flags & PR_ATOMIC) {
1797 if ((flags & MSG_PEEK) == 0)
1798 (void) sbdroprecord_locked(&so->so_rcv);
1800 if ((flags & MSG_PEEK) == 0) {
1803 * First part is an inline SB_EMPTY_FIXUP(). Second
1804 * part makes sure sb_lastrecord is up-to-date if
1805 * there is still data in the socket buffer.
1807 so->so_rcv.sb_mb = nextrecord;
1808 if (so->so_rcv.sb_mb == NULL) {
1809 so->so_rcv.sb_mbtail = NULL;
1810 so->so_rcv.sb_lastrecord = NULL;
1811 } else if (nextrecord->m_nextpkt == NULL)
1812 so->so_rcv.sb_lastrecord = nextrecord;
1814 SBLASTRECORDCHK(&so->so_rcv);
1815 SBLASTMBUFCHK(&so->so_rcv);
1817 * If soreceive() is being done from the socket callback,
1818 * then don't need to generate ACK to peer to update window,
1819 * since ACK will be generated on return to TCP.
1821 if (!(flags & MSG_SOCALLBCK) &&
1822 (pr->pr_flags & PR_WANTRCVD)) {
1823 SOCKBUF_UNLOCK(&so->so_rcv);
1824 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1825 SOCKBUF_LOCK(&so->so_rcv);
1828 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1829 if (orig_resid == uio->uio_resid && orig_resid &&
1830 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
1831 SOCKBUF_UNLOCK(&so->so_rcv);
1834 SOCKBUF_UNLOCK(&so->so_rcv);
1839 sbunlock(&so->so_rcv);
1844 * Optimized version of soreceive() for simple datagram cases from userspace.
1845 * Unlike in the stream case, we're able to drop a datagram if copyout()
1846 * fails, and because we handle datagrams atomically, we don't need to use a
1847 * sleep lock to prevent I/O interlacing.
1850 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
1851 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1853 struct mbuf *m, *m2;
1854 int flags, len, error;
1855 struct protosw *pr = so->so_proto;
1856 struct mbuf *nextrecord;
1860 if (controlp != NULL)
1863 flags = *flagsp &~ MSG_EOR;
1868 * For any complicated cases, fall back to the full
1869 * soreceive_generic().
1871 if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
1872 return (soreceive_generic(so, psa, uio, mp0, controlp,
1876 * Enforce restrictions on use.
1878 KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
1879 ("soreceive_dgram: wantrcvd"));
1880 KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
1881 KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
1882 ("soreceive_dgram: SBS_RCVATMARK"));
1883 KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
1884 ("soreceive_dgram: P_CONNREQUIRED"));
1887 * Loop blocking while waiting for a datagram.
1889 SOCKBUF_LOCK(&so->so_rcv);
1890 while ((m = so->so_rcv.sb_mb) == NULL) {
1891 KASSERT(so->so_rcv.sb_cc == 0,
1892 ("soreceive_dgram: sb_mb NULL but sb_cc %u",
1895 error = so->so_error;
1897 SOCKBUF_UNLOCK(&so->so_rcv);
1900 if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
1901 uio->uio_resid == 0) {
1902 SOCKBUF_UNLOCK(&so->so_rcv);
1905 if ((so->so_state & SS_NBIO) ||
1906 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1907 SOCKBUF_UNLOCK(&so->so_rcv);
1908 return (EWOULDBLOCK);
1910 SBLASTRECORDCHK(&so->so_rcv);
1911 SBLASTMBUFCHK(&so->so_rcv);
1912 error = sbwait(&so->so_rcv);
1914 SOCKBUF_UNLOCK(&so->so_rcv);
1918 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1921 uio->uio_td->td_ru.ru_msgrcv++;
1922 SBLASTRECORDCHK(&so->so_rcv);
1923 SBLASTMBUFCHK(&so->so_rcv);
1924 nextrecord = m->m_nextpkt;
1925 if (nextrecord == NULL) {
1926 KASSERT(so->so_rcv.sb_lastrecord == m,
1927 ("soreceive_dgram: lastrecord != m"));
1930 KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
1931 ("soreceive_dgram: m_nextpkt != nextrecord"));
1934 * Pull 'm' and its chain off the front of the packet queue.
1936 so->so_rcv.sb_mb = NULL;
1937 sockbuf_pushsync(&so->so_rcv, nextrecord);
1940 * Walk 'm's chain and free that many bytes from the socket buffer.
1942 for (m2 = m; m2 != NULL; m2 = m2->m_next)
1943 sbfree(&so->so_rcv, m2);
1946 * Do a few last checks before we let go of the lock.
1948 SBLASTRECORDCHK(&so->so_rcv);
1949 SBLASTMBUFCHK(&so->so_rcv);
1950 SOCKBUF_UNLOCK(&so->so_rcv);
1952 if (pr->pr_flags & PR_ADDR) {
1953 KASSERT(m->m_type == MT_SONAME,
1954 ("m->m_type == %d", m->m_type));
1956 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
1961 /* XXXRW: Can this happen? */
1966 * Packet to copyout() is now in 'm' and it is disconnected from the
1969 * Process one or more MT_CONTROL mbufs present before any data mbufs
1970 * in the first mbuf chain on the socket buffer. We call into the
1971 * protocol to perform externalization (or freeing if controlp ==
1974 if (m->m_type == MT_CONTROL) {
1975 struct mbuf *cm = NULL, *cmn;
1976 struct mbuf **cme = &cm;
1982 cme = &(*cme)->m_next;
1984 } while (m != NULL && m->m_type == MT_CONTROL);
1985 while (cm != NULL) {
1988 if (pr->pr_domain->dom_externalize != NULL) {
1989 error = (*pr->pr_domain->dom_externalize)
1991 } else if (controlp != NULL)
1995 if (controlp != NULL) {
1996 while (*controlp != NULL)
1997 controlp = &(*controlp)->m_next;
2002 KASSERT(m->m_type == MT_DATA, ("soreceive_dgram: !data"));
2004 while (m != NULL && uio->uio_resid > 0) {
2005 len = uio->uio_resid;
2008 error = uiomove(mtod(m, char *), (int)len, uio);
2024 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
2025 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2028 return (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
2033 soshutdown(struct socket *so, int how)
2035 struct protosw *pr = so->so_proto;
2037 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
2039 if (pr->pr_usrreqs->pru_flush != NULL) {
2040 (*pr->pr_usrreqs->pru_flush)(so, how);
2045 return ((*pr->pr_usrreqs->pru_shutdown)(so));
2050 sorflush(struct socket *so)
2052 struct sockbuf *sb = &so->so_rcv;
2053 struct protosw *pr = so->so_proto;
2057 * In order to avoid calling dom_dispose with the socket buffer mutex
2058 * held, and in order to generally avoid holding the lock for a long
2059 * time, we make a copy of the socket buffer and clear the original
2060 * (except locks, state). The new socket buffer copy won't have
2061 * initialized locks so we can only call routines that won't use or
2062 * assert those locks.
2064 * Dislodge threads currently blocked in receive and wait to acquire
2065 * a lock against other simultaneous readers before clearing the
2066 * socket buffer. Don't let our acquire be interrupted by a signal
2067 * despite any existing socket disposition on interruptable waiting.
2070 (void) sblock(sb, SBL_WAIT | SBL_NOINTR);
2073 * Invalidate/clear most of the sockbuf structure, but leave selinfo
2074 * and mutex data unchanged.
2077 bzero(&asb, offsetof(struct sockbuf, sb_startzero));
2078 bcopy(&sb->sb_startzero, &asb.sb_startzero,
2079 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2080 bzero(&sb->sb_startzero,
2081 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2086 * Dispose of special rights and flush the socket buffer. Don't call
2087 * any unsafe routines (that rely on locks being initialized) on asb.
2089 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
2090 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
2091 sbrelease_internal(&asb, so);
2095 * Perhaps this routine, and sooptcopyout(), below, ought to come in an
2096 * additional variant to handle the case where the option value needs to be
2097 * some kind of integer, but not a specific size. In addition to their use
2098 * here, these functions are also called by the protocol-level pr_ctloutput()
2102 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2107 * If the user gives us more than we wanted, we ignore it, but if we
2108 * don't get the minimum length the caller wants, we return EINVAL.
2109 * On success, sopt->sopt_valsize is set to however much we actually
2112 if ((valsize = sopt->sopt_valsize) < minlen)
2115 sopt->sopt_valsize = valsize = len;
2117 if (sopt->sopt_td != NULL)
2118 return (copyin(sopt->sopt_val, buf, valsize));
2120 bcopy(sopt->sopt_val, buf, valsize);
2125 * Kernel version of setsockopt(2).
2127 * XXX: optlen is size_t, not socklen_t
2130 so_setsockopt(struct socket *so, int level, int optname, void *optval,
2133 struct sockopt sopt;
2135 sopt.sopt_level = level;
2136 sopt.sopt_name = optname;
2137 sopt.sopt_dir = SOPT_SET;
2138 sopt.sopt_val = optval;
2139 sopt.sopt_valsize = optlen;
2140 sopt.sopt_td = NULL;
2141 return (sosetopt(so, &sopt));
2145 sosetopt(struct socket *so, struct sockopt *sopt)
2156 if (sopt->sopt_level != SOL_SOCKET) {
2157 if (so->so_proto && so->so_proto->pr_ctloutput)
2158 return ((*so->so_proto->pr_ctloutput)
2160 error = ENOPROTOOPT;
2162 switch (sopt->sopt_name) {
2164 case SO_ACCEPTFILTER:
2165 error = do_setopt_accept_filter(so, sopt);
2171 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2176 so->so_linger = l.l_linger;
2178 so->so_options |= SO_LINGER;
2180 so->so_options &= ~SO_LINGER;
2187 case SO_USELOOPBACK:
2197 error = sooptcopyin(sopt, &optval, sizeof optval,
2203 so->so_options |= sopt->sopt_name;
2205 so->so_options &= ~sopt->sopt_name;
2210 error = sooptcopyin(sopt, &optval, sizeof optval,
2212 if (optval < 1 || optval > rt_numfibs) {
2216 if ((so->so_proto->pr_domain->dom_family == PF_INET) ||
2217 (so->so_proto->pr_domain->dom_family == PF_ROUTE)) {
2218 so->so_fibnum = optval;
2219 /* Note: ignore error */
2220 if (so->so_proto && so->so_proto->pr_ctloutput)
2221 (*so->so_proto->pr_ctloutput)(so, sopt);
2230 error = sooptcopyin(sopt, &optval, sizeof optval,
2236 * Values < 1 make no sense for any of these options,
2244 switch (sopt->sopt_name) {
2247 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
2248 &so->so_snd : &so->so_rcv, (u_long)optval,
2249 so, curthread) == 0) {
2253 (sopt->sopt_name == SO_SNDBUF ? &so->so_snd :
2254 &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE;
2258 * Make sure the low-water is never greater than the
2262 SOCKBUF_LOCK(&so->so_snd);
2263 so->so_snd.sb_lowat =
2264 (optval > so->so_snd.sb_hiwat) ?
2265 so->so_snd.sb_hiwat : optval;
2266 SOCKBUF_UNLOCK(&so->so_snd);
2269 SOCKBUF_LOCK(&so->so_rcv);
2270 so->so_rcv.sb_lowat =
2271 (optval > so->so_rcv.sb_hiwat) ?
2272 so->so_rcv.sb_hiwat : optval;
2273 SOCKBUF_UNLOCK(&so->so_rcv);
2281 if (SV_CURPROC_FLAG(SV_ILP32)) {
2282 struct timeval32 tv32;
2284 error = sooptcopyin(sopt, &tv32, sizeof tv32,
2286 CP(tv32, tv, tv_sec);
2287 CP(tv32, tv, tv_usec);
2290 error = sooptcopyin(sopt, &tv, sizeof tv,
2295 /* assert(hz > 0); */
2296 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
2297 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
2301 /* assert(tick > 0); */
2302 /* assert(ULONG_MAX - INT_MAX >= 1000000); */
2303 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
2304 if (val > INT_MAX) {
2308 if (val == 0 && tv.tv_usec != 0)
2311 switch (sopt->sopt_name) {
2313 so->so_snd.sb_timeo = val;
2316 so->so_rcv.sb_timeo = val;
2323 error = sooptcopyin(sopt, &extmac, sizeof extmac,
2327 error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
2335 error = ENOPROTOOPT;
2338 if (error == 0 && so->so_proto != NULL &&
2339 so->so_proto->pr_ctloutput != NULL) {
2340 (void) ((*so->so_proto->pr_ctloutput)
2349 * Helper routine for getsockopt.
2352 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
2360 * Documented get behavior is that we always return a value, possibly
2361 * truncated to fit in the user's buffer. Traditional behavior is
2362 * that we always tell the user precisely how much we copied, rather
2363 * than something useful like the total amount we had available for
2364 * her. Note that this interface is not idempotent; the entire
2365 * answer must generated ahead of time.
2367 valsize = min(len, sopt->sopt_valsize);
2368 sopt->sopt_valsize = valsize;
2369 if (sopt->sopt_val != NULL) {
2370 if (sopt->sopt_td != NULL)
2371 error = copyout(buf, sopt->sopt_val, valsize);
2373 bcopy(buf, sopt->sopt_val, valsize);
2379 sogetopt(struct socket *so, struct sockopt *sopt)
2389 if (sopt->sopt_level != SOL_SOCKET) {
2390 if (so->so_proto && so->so_proto->pr_ctloutput) {
2391 return ((*so->so_proto->pr_ctloutput)
2394 return (ENOPROTOOPT);
2396 switch (sopt->sopt_name) {
2398 case SO_ACCEPTFILTER:
2399 error = do_getopt_accept_filter(so, sopt);
2404 l.l_onoff = so->so_options & SO_LINGER;
2405 l.l_linger = so->so_linger;
2407 error = sooptcopyout(sopt, &l, sizeof l);
2410 case SO_USELOOPBACK:
2422 optval = so->so_options & sopt->sopt_name;
2424 error = sooptcopyout(sopt, &optval, sizeof optval);
2428 optval = so->so_type;
2433 optval = so->so_error;
2439 optval = so->so_snd.sb_hiwat;
2443 optval = so->so_rcv.sb_hiwat;
2447 optval = so->so_snd.sb_lowat;
2451 optval = so->so_rcv.sb_lowat;
2456 optval = (sopt->sopt_name == SO_SNDTIMEO ?
2457 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
2459 tv.tv_sec = optval / hz;
2460 tv.tv_usec = (optval % hz) * tick;
2462 if (SV_CURPROC_FLAG(SV_ILP32)) {
2463 struct timeval32 tv32;
2465 CP(tv, tv32, tv_sec);
2466 CP(tv, tv32, tv_usec);
2467 error = sooptcopyout(sopt, &tv32, sizeof tv32);
2470 error = sooptcopyout(sopt, &tv, sizeof tv);
2475 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2479 error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
2483 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2491 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2495 error = mac_getsockopt_peerlabel(
2496 sopt->sopt_td->td_ucred, so, &extmac);
2499 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2505 case SO_LISTENQLIMIT:
2506 optval = so->so_qlimit;
2510 optval = so->so_qlen;
2513 case SO_LISTENINCQLEN:
2514 optval = so->so_incqlen;
2518 error = ENOPROTOOPT;
2525 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2527 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2529 struct mbuf *m, *m_prev;
2530 int sopt_size = sopt->sopt_valsize;
2532 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2535 if (sopt_size > MLEN) {
2536 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
2537 if ((m->m_flags & M_EXT) == 0) {
2541 m->m_len = min(MCLBYTES, sopt_size);
2543 m->m_len = min(MLEN, sopt_size);
2545 sopt_size -= m->m_len;
2550 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
2555 if (sopt_size > MLEN) {
2556 MCLGET(m, sopt->sopt_td != NULL ? M_WAIT :
2558 if ((m->m_flags & M_EXT) == 0) {
2563 m->m_len = min(MCLBYTES, sopt_size);
2565 m->m_len = min(MLEN, sopt_size);
2567 sopt_size -= m->m_len;
2574 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2576 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
2578 struct mbuf *m0 = m;
2580 if (sopt->sopt_val == NULL)
2582 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2583 if (sopt->sopt_td != NULL) {
2586 error = copyin(sopt->sopt_val, mtod(m, char *),
2593 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
2594 sopt->sopt_valsize -= m->m_len;
2595 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2598 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2599 panic("ip6_sooptmcopyin");
2603 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2605 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
2607 struct mbuf *m0 = m;
2610 if (sopt->sopt_val == NULL)
2612 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2613 if (sopt->sopt_td != NULL) {
2616 error = copyout(mtod(m, char *), sopt->sopt_val,
2623 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
2624 sopt->sopt_valsize -= m->m_len;
2625 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2626 valsize += m->m_len;
2630 /* enough soopt buffer should be given from user-land */
2634 sopt->sopt_valsize = valsize;
2639 * sohasoutofband(): protocol notifies socket layer of the arrival of new
2640 * out-of-band data, which will then notify socket consumers.
2643 sohasoutofband(struct socket *so)
2646 if (so->so_sigio != NULL)
2647 pgsigio(&so->so_sigio, SIGURG, 0);
2648 selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
2652 sopoll(struct socket *so, int events, struct ucred *active_cred,
2656 return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
2661 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
2666 SOCKBUF_LOCK(&so->so_snd);
2667 SOCKBUF_LOCK(&so->so_rcv);
2668 if (events & (POLLIN | POLLRDNORM))
2670 revents |= events & (POLLIN | POLLRDNORM);
2672 if (events & POLLINIGNEOF)
2673 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
2674 !TAILQ_EMPTY(&so->so_comp) || so->so_error)
2675 revents |= POLLINIGNEOF;
2677 if (events & (POLLOUT | POLLWRNORM))
2678 if (sowriteable(so))
2679 revents |= events & (POLLOUT | POLLWRNORM);
2681 if (events & (POLLPRI | POLLRDBAND))
2682 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
2683 revents |= events & (POLLPRI | POLLRDBAND);
2687 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM |
2689 selrecord(td, &so->so_rcv.sb_sel);
2690 so->so_rcv.sb_flags |= SB_SEL;
2693 if (events & (POLLOUT | POLLWRNORM)) {
2694 selrecord(td, &so->so_snd.sb_sel);
2695 so->so_snd.sb_flags |= SB_SEL;
2699 SOCKBUF_UNLOCK(&so->so_rcv);
2700 SOCKBUF_UNLOCK(&so->so_snd);
2705 soo_kqfilter(struct file *fp, struct knote *kn)
2707 struct socket *so = kn->kn_fp->f_data;
2710 switch (kn->kn_filter) {
2712 if (so->so_options & SO_ACCEPTCONN)
2713 kn->kn_fop = &solisten_filtops;
2715 kn->kn_fop = &soread_filtops;
2719 kn->kn_fop = &sowrite_filtops;
2727 knlist_add(&sb->sb_sel.si_note, kn, 1);
2728 sb->sb_flags |= SB_KNOTE;
2734 * Some routines that return EOPNOTSUPP for entry points that are not
2735 * supported by a protocol. Fill in as needed.
2738 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
2745 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
2752 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
2759 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
2766 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
2773 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
2774 struct ifnet *ifp, struct thread *td)
2781 pru_disconnect_notsupp(struct socket *so)
2788 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
2795 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
2802 pru_rcvd_notsupp(struct socket *so, int flags)
2809 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
2816 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
2817 struct sockaddr *addr, struct mbuf *control, struct thread *td)
2824 * This isn't really a ``null'' operation, but it's the default one and
2825 * doesn't do anything destructive.
2828 pru_sense_null(struct socket *so, struct stat *sb)
2831 sb->st_blksize = so->so_snd.sb_hiwat;
2836 pru_shutdown_notsupp(struct socket *so)
2843 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
2850 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
2851 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
2858 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
2859 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2866 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
2874 filt_sordetach(struct knote *kn)
2876 struct socket *so = kn->kn_fp->f_data;
2878 SOCKBUF_LOCK(&so->so_rcv);
2879 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
2880 if (knlist_empty(&so->so_rcv.sb_sel.si_note))
2881 so->so_rcv.sb_flags &= ~SB_KNOTE;
2882 SOCKBUF_UNLOCK(&so->so_rcv);
2887 filt_soread(struct knote *kn, long hint)
2891 so = kn->kn_fp->f_data;
2892 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2894 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
2895 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2896 kn->kn_flags |= EV_EOF;
2897 kn->kn_fflags = so->so_error;
2899 } else if (so->so_error) /* temporary udp error */
2901 else if (kn->kn_sfflags & NOTE_LOWAT)
2902 return (kn->kn_data >= kn->kn_sdata);
2904 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
2908 filt_sowdetach(struct knote *kn)
2910 struct socket *so = kn->kn_fp->f_data;
2912 SOCKBUF_LOCK(&so->so_snd);
2913 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
2914 if (knlist_empty(&so->so_snd.sb_sel.si_note))
2915 so->so_snd.sb_flags &= ~SB_KNOTE;
2916 SOCKBUF_UNLOCK(&so->so_snd);
2921 filt_sowrite(struct knote *kn, long hint)
2925 so = kn->kn_fp->f_data;
2926 SOCKBUF_LOCK_ASSERT(&so->so_snd);
2927 kn->kn_data = sbspace(&so->so_snd);
2928 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2929 kn->kn_flags |= EV_EOF;
2930 kn->kn_fflags = so->so_error;
2932 } else if (so->so_error) /* temporary udp error */
2934 else if (((so->so_state & SS_ISCONNECTED) == 0) &&
2935 (so->so_proto->pr_flags & PR_CONNREQUIRED))
2937 else if (kn->kn_sfflags & NOTE_LOWAT)
2938 return (kn->kn_data >= kn->kn_sdata);
2940 return (kn->kn_data >= so->so_snd.sb_lowat);
2945 filt_solisten(struct knote *kn, long hint)
2947 struct socket *so = kn->kn_fp->f_data;
2949 kn->kn_data = so->so_qlen;
2950 return (! TAILQ_EMPTY(&so->so_comp));
2954 socheckuid(struct socket *so, uid_t uid)
2959 if (so->so_cred->cr_uid != uid)
2965 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
2971 error = sysctl_handle_int(oidp, &val, 0, req);
2972 if (error || !req->newptr )
2975 if (val < 1 || val > USHRT_MAX)
2983 * These functions are used by protocols to notify the socket layer (and its
2984 * consumers) of state changes in the sockets driven by protocol-side events.
2988 * Procedures to manipulate state flags of socket and do appropriate wakeups.
2990 * Normal sequence from the active (originating) side is that
2991 * soisconnecting() is called during processing of connect() call, resulting
2992 * in an eventual call to soisconnected() if/when the connection is
2993 * established. When the connection is torn down soisdisconnecting() is
2994 * called during processing of disconnect() call, and soisdisconnected() is
2995 * called when the connection to the peer is totally severed. The semantics
2996 * of these routines are such that connectionless protocols can call
2997 * soisconnected() and soisdisconnected() only, bypassing the in-progress
2998 * calls when setting up a ``connection'' takes no time.
3000 * From the passive side, a socket is created with two queues of sockets:
3001 * so_incomp for connections in progress and so_comp for connections already
3002 * made and awaiting user acceptance. As a protocol is preparing incoming
3003 * connections, it creates a socket structure queued on so_incomp by calling
3004 * sonewconn(). When the connection is established, soisconnected() is
3005 * called, and transfers the socket structure to so_comp, making it available
3008 * If a socket is closed with sockets on either so_incomp or so_comp, these
3009 * sockets are dropped.
3011 * If higher-level protocols are implemented in the kernel, the wakeups done
3012 * here will sometimes cause software-interrupt process scheduling.
3015 soisconnecting(struct socket *so)
3019 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
3020 so->so_state |= SS_ISCONNECTING;
3025 soisconnected(struct socket *so)
3027 struct socket *head;
3031 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
3032 so->so_state |= SS_ISCONNECTED;
3034 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
3035 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
3037 TAILQ_REMOVE(&head->so_incomp, so, so_list);
3039 so->so_qstate &= ~SQ_INCOMP;
3040 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
3042 so->so_qstate |= SQ_COMP;
3045 wakeup_one(&head->so_timeo);
3049 head->so_accf->so_accept_filter->accf_callback;
3050 so->so_upcallarg = head->so_accf->so_accept_filter_arg;
3051 so->so_rcv.sb_flags |= SB_UPCALL;
3052 so->so_options &= ~SO_ACCEPTFILTER;
3054 so->so_upcall(so, so->so_upcallarg, M_DONTWAIT);
3060 wakeup(&so->so_timeo);
3066 soisdisconnecting(struct socket *so)
3070 * Note: This code assumes that SOCK_LOCK(so) and
3071 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3073 SOCKBUF_LOCK(&so->so_rcv);
3074 so->so_state &= ~SS_ISCONNECTING;
3075 so->so_state |= SS_ISDISCONNECTING;
3076 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3077 sorwakeup_locked(so);
3078 SOCKBUF_LOCK(&so->so_snd);
3079 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3080 sowwakeup_locked(so);
3081 wakeup(&so->so_timeo);
3085 soisdisconnected(struct socket *so)
3089 * Note: This code assumes that SOCK_LOCK(so) and
3090 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3092 SOCKBUF_LOCK(&so->so_rcv);
3093 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
3094 so->so_state |= SS_ISDISCONNECTED;
3095 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3096 sorwakeup_locked(so);
3097 SOCKBUF_LOCK(&so->so_snd);
3098 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3099 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
3100 sowwakeup_locked(so);
3101 wakeup(&so->so_timeo);
3105 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
3108 sodupsockaddr(const struct sockaddr *sa, int mflags)
3110 struct sockaddr *sa2;
3112 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
3114 bcopy(sa, sa2, sa->sa_len);
3119 * Create an external-format (``xsocket'') structure using the information in
3120 * the kernel-format socket structure pointed to by so. This is done to
3121 * reduce the spew of irrelevant information over this interface, to isolate
3122 * user code from changes in the kernel structure, and potentially to provide
3123 * information-hiding if we decide that some of this information should be
3124 * hidden from users.
3127 sotoxsocket(struct socket *so, struct xsocket *xso)
3130 xso->xso_len = sizeof *xso;
3132 xso->so_type = so->so_type;
3133 xso->so_options = so->so_options;
3134 xso->so_linger = so->so_linger;
3135 xso->so_state = so->so_state;
3136 xso->so_pcb = so->so_pcb;
3137 xso->xso_protocol = so->so_proto->pr_protocol;
3138 xso->xso_family = so->so_proto->pr_domain->dom_family;
3139 xso->so_qlen = so->so_qlen;
3140 xso->so_incqlen = so->so_incqlen;
3141 xso->so_qlimit = so->so_qlimit;
3142 xso->so_timeo = so->so_timeo;
3143 xso->so_error = so->so_error;
3144 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
3145 xso->so_oobmark = so->so_oobmark;
3146 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
3147 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
3148 xso->so_uid = so->so_cred->cr_uid;
3153 * Socket accessor functions to provide external consumers with
3154 * a safe interface to socket state
3159 so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *), void *arg)
3162 TAILQ_FOREACH(so, &so->so_comp, so_list)
3167 so_sockbuf_rcv(struct socket *so)
3170 return (&so->so_rcv);
3174 so_sockbuf_snd(struct socket *so)
3177 return (&so->so_snd);
3181 so_state_get(const struct socket *so)
3184 return (so->so_state);
3188 so_state_set(struct socket *so, int val)
3195 so_options_get(const struct socket *so)
3198 return (so->so_options);
3202 so_options_set(struct socket *so, int val)
3205 so->so_options = val;
3209 so_error_get(const struct socket *so)
3212 return (so->so_error);
3216 so_error_set(struct socket *so, int val)
3223 so_linger_get(const struct socket *so)
3226 return (so->so_linger);
3230 so_linger_set(struct socket *so, int val)
3233 so->so_linger = val;
3237 so_protosw_get(const struct socket *so)
3240 return (so->so_proto);
3244 so_protosw_set(struct socket *so, struct protosw *val)
3251 so_sorwakeup(struct socket *so)
3258 so_sowwakeup(struct socket *so)
3265 so_sorwakeup_locked(struct socket *so)
3268 sorwakeup_locked(so);
3272 so_sowwakeup_locked(struct socket *so)
3275 sowwakeup_locked(so);
3279 so_lock(struct socket *so)
3285 so_unlock(struct socket *so)