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.
96 * NOTE: With regard to VNETs the general rule is that callers do not set
97 * curvnet. Exceptions to this rule include soabort(), sodisconnect(),
98 * sofree() (and with that sorele(), sotryfree()), as well as sonewconn()
99 * and sorflush(), which are usually called from a pre-set VNET context.
100 * sopoll() currently does not need a VNET context to be set.
103 #include <sys/cdefs.h>
104 __FBSDID("$FreeBSD$");
106 #include "opt_inet.h"
107 #include "opt_inet6.h"
108 #include "opt_compat.h"
110 #include <sys/param.h>
111 #include <sys/systm.h>
112 #include <sys/fcntl.h>
113 #include <sys/limits.h>
114 #include <sys/lock.h>
116 #include <sys/malloc.h>
117 #include <sys/mbuf.h>
118 #include <sys/mutex.h>
119 #include <sys/domain.h>
120 #include <sys/file.h> /* for struct knote */
121 #include <sys/kernel.h>
122 #include <sys/event.h>
123 #include <sys/eventhandler.h>
124 #include <sys/poll.h>
125 #include <sys/proc.h>
126 #include <sys/protosw.h>
127 #include <sys/socket.h>
128 #include <sys/socketvar.h>
129 #include <sys/resourcevar.h>
130 #include <net/route.h>
131 #include <sys/signalvar.h>
132 #include <sys/stat.h>
134 #include <sys/sysctl.h>
136 #include <sys/jail.h>
137 #include <sys/syslog.h>
138 #include <netinet/in.h>
140 #include <net/vnet.h>
142 #include <security/mac/mac_framework.h>
146 #ifdef COMPAT_FREEBSD32
147 #include <sys/mount.h>
148 #include <sys/sysent.h>
149 #include <compat/freebsd32/freebsd32.h>
152 static int soreceive_rcvoob(struct socket *so, struct uio *uio,
155 static void filt_sordetach(struct knote *kn);
156 static int filt_soread(struct knote *kn, long hint);
157 static void filt_sowdetach(struct knote *kn);
158 static int filt_sowrite(struct knote *kn, long hint);
159 static int filt_solisten(struct knote *kn, long hint);
161 static struct filterops solisten_filtops = {
163 .f_detach = filt_sordetach,
164 .f_event = filt_solisten,
166 static struct filterops soread_filtops = {
168 .f_detach = filt_sordetach,
169 .f_event = filt_soread,
171 static struct filterops sowrite_filtops = {
173 .f_detach = filt_sowdetach,
174 .f_event = filt_sowrite,
177 so_gen_t so_gencnt; /* generation count for sockets */
179 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
180 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
182 #define VNET_SO_ASSERT(so) \
183 VNET_ASSERT(curvnet != NULL, \
184 ("%s:%d curvnet is NULL, so=%p", __func__, __LINE__, (so)));
187 * Limit on the number of connections in the listen queue waiting
189 * NB: The orginal sysctl somaxconn is still available but hidden
190 * to prevent confusion about the actual purpose of this number.
192 static int somaxconn = SOMAXCONN;
195 sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
201 error = sysctl_handle_int(oidp, &val, 0, req);
202 if (error || !req->newptr )
205 if (val < 1 || val > USHRT_MAX)
211 SYSCTL_PROC(_kern_ipc, OID_AUTO, soacceptqueue, CTLTYPE_UINT | CTLFLAG_RW,
212 0, sizeof(int), sysctl_somaxconn, "I",
213 "Maximum listen socket pending connection accept queue size");
214 SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn,
215 CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_SKIP,
216 0, sizeof(int), sysctl_somaxconn, "I",
217 "Maximum listen socket pending connection accept queue size (compat)");
219 static int numopensockets;
220 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
221 &numopensockets, 0, "Number of open sockets");
224 * accept_mtx locks down per-socket fields relating to accept queues. See
225 * socketvar.h for an annotation of the protected fields of struct socket.
227 struct mtx accept_mtx;
228 MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
231 * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
234 static struct mtx so_global_mtx;
235 MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
238 * General IPC sysctl name space, used by sockets and a variety of other IPC
241 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
244 * Initialize the socket subsystem and set up the socket
247 static uma_zone_t socket_zone;
251 socket_zone_change(void *tag)
254 maxsockets = uma_zone_set_max(socket_zone, maxsockets);
258 socket_init(void *tag)
261 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
262 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
263 maxsockets = uma_zone_set_max(socket_zone, maxsockets);
264 uma_zone_set_warning(socket_zone, "kern.ipc.maxsockets limit reached");
265 EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
266 EVENTHANDLER_PRI_FIRST);
268 SYSINIT(socket, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, socket_init, NULL);
271 * Initialise maxsockets. This SYSINIT must be run after
275 init_maxsockets(void *ignored)
278 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
279 maxsockets = imax(maxsockets, maxfiles);
281 SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
284 * Sysctl to get and set the maximum global sockets limit. Notify protocols
285 * of the change so that they can update their dependent limits as required.
288 sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
290 int error, newmaxsockets;
292 newmaxsockets = maxsockets;
293 error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
294 if (error == 0 && req->newptr) {
295 if (newmaxsockets > maxsockets &&
296 newmaxsockets <= maxfiles) {
297 maxsockets = newmaxsockets;
298 EVENTHANDLER_INVOKE(maxsockets_change);
304 SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW,
305 &maxsockets, 0, sysctl_maxsockets, "IU",
306 "Maximum number of sockets avaliable");
309 * Socket operation routines. These routines are called by the routines in
310 * sys_socket.c or from a system process, and implement the semantics of
311 * socket operations by switching out to the protocol specific routines.
315 * Get a socket structure from our zone, and initialize it. Note that it
316 * would probably be better to allocate socket and PCB at the same time, but
317 * I'm not convinced that all the protocols can be easily modified to do
320 * soalloc() returns a socket with a ref count of 0.
322 static struct socket *
323 soalloc(struct vnet *vnet)
327 so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO);
331 if (mac_socket_init(so, M_NOWAIT) != 0) {
332 uma_zfree(socket_zone, so);
336 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
337 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
338 sx_init(&so->so_snd.sb_sx, "so_snd_sx");
339 sx_init(&so->so_rcv.sb_sx, "so_rcv_sx");
340 TAILQ_INIT(&so->so_aiojobq);
341 mtx_lock(&so_global_mtx);
342 so->so_gencnt = ++so_gencnt;
345 VNET_ASSERT(vnet != NULL, ("%s:%d vnet is NULL, so=%p",
346 __func__, __LINE__, so));
347 vnet->vnet_sockcnt++;
350 mtx_unlock(&so_global_mtx);
355 * Free the storage associated with a socket at the socket layer, tear down
356 * locks, labels, etc. All protocol state is assumed already to have been
357 * torn down (and possibly never set up) by the caller.
360 sodealloc(struct socket *so)
363 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
364 KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
366 mtx_lock(&so_global_mtx);
367 so->so_gencnt = ++so_gencnt;
368 --numopensockets; /* Could be below, but faster here. */
370 VNET_ASSERT(so->so_vnet != NULL, ("%s:%d so_vnet is NULL, so=%p",
371 __func__, __LINE__, so));
372 so->so_vnet->vnet_sockcnt--;
374 mtx_unlock(&so_global_mtx);
375 if (so->so_rcv.sb_hiwat)
376 (void)chgsbsize(so->so_cred->cr_uidinfo,
377 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
378 if (so->so_snd.sb_hiwat)
379 (void)chgsbsize(so->so_cred->cr_uidinfo,
380 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
381 /* remove acccept filter if one is present. */
382 if (so->so_accf != NULL)
383 do_setopt_accept_filter(so, NULL);
385 mac_socket_destroy(so);
388 sx_destroy(&so->so_snd.sb_sx);
389 sx_destroy(&so->so_rcv.sb_sx);
390 SOCKBUF_LOCK_DESTROY(&so->so_snd);
391 SOCKBUF_LOCK_DESTROY(&so->so_rcv);
392 uma_zfree(socket_zone, so);
396 * socreate returns a socket with a ref count of 1. The socket should be
397 * closed with soclose().
400 socreate(int dom, struct socket **aso, int type, int proto,
401 struct ucred *cred, struct thread *td)
408 prp = pffindproto(dom, proto, type);
410 prp = pffindtype(dom, type);
413 /* No support for domain. */
414 if (pffinddomain(dom) == NULL)
415 return (EAFNOSUPPORT);
416 /* No support for socket type. */
417 if (proto == 0 && type != 0)
419 return (EPROTONOSUPPORT);
421 if (prp->pr_usrreqs->pru_attach == NULL ||
422 prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
423 return (EPROTONOSUPPORT);
425 if (prison_check_af(cred, prp->pr_domain->dom_family) != 0)
426 return (EPROTONOSUPPORT);
428 if (prp->pr_type != type)
430 so = soalloc(CRED_TO_VNET(cred));
434 TAILQ_INIT(&so->so_incomp);
435 TAILQ_INIT(&so->so_comp);
437 so->so_cred = crhold(cred);
438 if ((prp->pr_domain->dom_family == PF_INET) ||
439 (prp->pr_domain->dom_family == PF_INET6) ||
440 (prp->pr_domain->dom_family == PF_ROUTE))
441 so->so_fibnum = td->td_proc->p_fibnum;
446 mac_socket_create(cred, so);
448 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
449 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
452 * Auto-sizing of socket buffers is managed by the protocols and
453 * the appropriate flags must be set in the pru_attach function.
455 CURVNET_SET(so->so_vnet);
456 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
459 KASSERT(so->so_count == 1, ("socreate: so_count %d",
470 static int regression_sonewconn_earlytest = 1;
471 SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
472 ®ression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
476 * When an attempt at a new connection is noted on a socket which accepts
477 * connections, sonewconn is called. If the connection is possible (subject
478 * to space constraints, etc.) then we allocate a new structure, propoerly
479 * linked into the data structure of the original socket, and return this.
480 * Connstatus may be 0, or SS_ISCONFIRMING, or SS_ISCONNECTED.
482 * Note: the ref count on the socket is 0 on return.
485 sonewconn(struct socket *head, int connstatus)
487 static struct timeval lastover;
488 static struct timeval overinterval = { 60, 0 };
489 static int overcount;
495 over = (head->so_qlen > 3 * head->so_qlimit / 2);
498 if (regression_sonewconn_earlytest && over) {
504 if (ratecheck(&lastover, &overinterval)) {
505 log(LOG_DEBUG, "%s: pcb %p: Listen queue overflow: "
506 "%i already in queue awaiting acceptance "
507 "(%d occurrences)\n",
508 __func__, head->so_pcb, head->so_qlen, overcount);
515 VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p",
516 __func__, __LINE__, head));
517 so = soalloc(head->so_vnet);
519 log(LOG_DEBUG, "%s: pcb %p: New socket allocation failure: "
520 "limit reached or out of memory\n",
521 __func__, head->so_pcb);
524 if ((head->so_options & SO_ACCEPTFILTER) != 0)
527 so->so_type = head->so_type;
528 so->so_options = head->so_options &~ SO_ACCEPTCONN;
529 so->so_linger = head->so_linger;
530 so->so_state = head->so_state | SS_NOFDREF;
531 so->so_fibnum = head->so_fibnum;
532 so->so_proto = head->so_proto;
533 so->so_cred = crhold(head->so_cred);
535 mac_socket_newconn(head, so);
537 knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
538 knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
539 VNET_SO_ASSERT(head);
540 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
542 log(LOG_DEBUG, "%s: pcb %p: soreserve() failed\n",
543 __func__, head->so_pcb);
546 if ((*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
548 log(LOG_DEBUG, "%s: pcb %p: pru_attach() failed\n",
549 __func__, head->so_pcb);
552 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
553 so->so_snd.sb_lowat = head->so_snd.sb_lowat;
554 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
555 so->so_snd.sb_timeo = head->so_snd.sb_timeo;
556 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
557 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
558 so->so_state |= connstatus;
561 * The accept socket may be tearing down but we just
562 * won a race on the ACCEPT_LOCK.
563 * However, if sctp_peeloff() is called on a 1-to-many
564 * style socket, the SO_ACCEPTCONN doesn't need to be set.
566 if (!(head->so_options & SO_ACCEPTCONN) &&
567 ((head->so_proto->pr_protocol != IPPROTO_SCTP) ||
568 (head->so_type != SOCK_SEQPACKET))) {
571 sofree(so); /* NB: returns ACCEPT_UNLOCK'ed. */
575 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
576 so->so_qstate |= SQ_COMP;
580 * Keep removing sockets from the head until there's room for
581 * us to insert on the tail. In pre-locking revisions, this
582 * was a simple if(), but as we could be racing with other
583 * threads and soabort() requires dropping locks, we must
584 * loop waiting for the condition to be true.
586 while (head->so_incqlen > head->so_qlimit) {
588 sp = TAILQ_FIRST(&head->so_incomp);
589 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
591 sp->so_qstate &= ~SQ_INCOMP;
597 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
598 so->so_qstate |= SQ_INCOMP;
604 wakeup_one(&head->so_timeo);
610 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
614 CURVNET_SET(so->so_vnet);
615 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
621 sobindat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
625 CURVNET_SET(so->so_vnet);
626 error = (*so->so_proto->pr_usrreqs->pru_bindat)(fd, so, nam, td);
632 * solisten() transitions a socket from a non-listening state to a listening
633 * state, but can also be used to update the listen queue depth on an
634 * existing listen socket. The protocol will call back into the sockets
635 * layer using solisten_proto_check() and solisten_proto() to check and set
636 * socket-layer listen state. Call backs are used so that the protocol can
637 * acquire both protocol and socket layer locks in whatever order is required
640 * Protocol implementors are advised to hold the socket lock across the
641 * socket-layer test and set to avoid races at the socket layer.
644 solisten(struct socket *so, int backlog, struct thread *td)
648 CURVNET_SET(so->so_vnet);
649 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td);
655 solisten_proto_check(struct socket *so)
658 SOCK_LOCK_ASSERT(so);
660 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
667 solisten_proto(struct socket *so, int backlog)
670 SOCK_LOCK_ASSERT(so);
672 if (backlog < 0 || backlog > somaxconn)
674 so->so_qlimit = backlog;
675 so->so_options |= SO_ACCEPTCONN;
679 * Evaluate the reference count and named references on a socket; if no
680 * references remain, free it. This should be called whenever a reference is
681 * released, such as in sorele(), but also when named reference flags are
682 * cleared in socket or protocol code.
684 * sofree() will free the socket if:
686 * - There are no outstanding file descriptor references or related consumers
689 * - The socket has been closed by user space, if ever open (SS_NOFDREF).
691 * - The protocol does not have an outstanding strong reference on the socket
694 * - The socket is not in a completed connection queue, so a process has been
695 * notified that it is present. If it is removed, the user process may
696 * block in accept() despite select() saying the socket was ready.
699 sofree(struct socket *so)
701 struct protosw *pr = so->so_proto;
704 ACCEPT_LOCK_ASSERT();
705 SOCK_LOCK_ASSERT(so);
707 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
708 (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
716 KASSERT((so->so_qstate & SQ_COMP) != 0 ||
717 (so->so_qstate & SQ_INCOMP) != 0,
718 ("sofree: so_head != NULL, but neither SQ_COMP nor "
720 KASSERT((so->so_qstate & SQ_COMP) == 0 ||
721 (so->so_qstate & SQ_INCOMP) == 0,
722 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
723 TAILQ_REMOVE(&head->so_incomp, so, so_list);
725 so->so_qstate &= ~SQ_INCOMP;
728 KASSERT((so->so_qstate & SQ_COMP) == 0 &&
729 (so->so_qstate & SQ_INCOMP) == 0,
730 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
731 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
732 if (so->so_options & SO_ACCEPTCONN) {
733 KASSERT((TAILQ_EMPTY(&so->so_comp)),
734 ("sofree: so_comp populated"));
735 KASSERT((TAILQ_EMPTY(&so->so_incomp)),
736 ("sofree: so_incomp populated"));
742 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
743 (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
744 if (pr->pr_usrreqs->pru_detach != NULL)
745 (*pr->pr_usrreqs->pru_detach)(so);
748 * From this point on, we assume that no other references to this
749 * socket exist anywhere else in the stack. Therefore, no locks need
750 * to be acquired or held.
752 * We used to do a lot of socket buffer and socket locking here, as
753 * well as invoke sorflush() and perform wakeups. The direct call to
754 * dom_dispose() and sbrelease_internal() are an inlining of what was
755 * necessary from sorflush().
757 * Notice that the socket buffer and kqueue state are torn down
758 * before calling pru_detach. This means that protocols shold not
759 * assume they can perform socket wakeups, etc, in their detach code.
761 sbdestroy(&so->so_snd, so);
762 sbdestroy(&so->so_rcv, so);
763 seldrain(&so->so_snd.sb_sel);
764 seldrain(&so->so_rcv.sb_sel);
765 knlist_destroy(&so->so_rcv.sb_sel.si_note);
766 knlist_destroy(&so->so_snd.sb_sel.si_note);
771 * Close a socket on last file table reference removal. Initiate disconnect
772 * if connected. Free socket when disconnect complete.
774 * This function will sorele() the socket. Note that soclose() may be called
775 * prior to the ref count reaching zero. The actual socket structure will
776 * not be freed until the ref count reaches zero.
779 soclose(struct socket *so)
783 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
785 CURVNET_SET(so->so_vnet);
786 funsetown(&so->so_sigio);
787 if (so->so_state & SS_ISCONNECTED) {
788 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
789 error = sodisconnect(so);
791 if (error == ENOTCONN)
796 if (so->so_options & SO_LINGER) {
797 if ((so->so_state & SS_ISDISCONNECTING) &&
798 (so->so_state & SS_NBIO))
800 while (so->so_state & SS_ISCONNECTED) {
801 error = tsleep(&so->so_timeo,
802 PSOCK | PCATCH, "soclos",
811 if (so->so_proto->pr_usrreqs->pru_close != NULL)
812 (*so->so_proto->pr_usrreqs->pru_close)(so);
814 if (so->so_options & SO_ACCEPTCONN) {
817 * Prevent new additions to the accept queues due
818 * to ACCEPT_LOCK races while we are draining them.
820 so->so_options &= ~SO_ACCEPTCONN;
821 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
822 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
824 sp->so_qstate &= ~SQ_INCOMP;
830 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
831 TAILQ_REMOVE(&so->so_comp, sp, so_list);
833 sp->so_qstate &= ~SQ_COMP;
839 KASSERT((TAILQ_EMPTY(&so->so_comp)),
840 ("%s: so_comp populated", __func__));
841 KASSERT((TAILQ_EMPTY(&so->so_incomp)),
842 ("%s: so_incomp populated", __func__));
845 KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
846 so->so_state |= SS_NOFDREF;
847 sorele(so); /* NB: Returns with ACCEPT_UNLOCK(). */
853 * soabort() is used to abruptly tear down a connection, such as when a
854 * resource limit is reached (listen queue depth exceeded), or if a listen
855 * socket is closed while there are sockets waiting to be accepted.
857 * This interface is tricky, because it is called on an unreferenced socket,
858 * and must be called only by a thread that has actually removed the socket
859 * from the listen queue it was on, or races with other threads are risked.
861 * This interface will call into the protocol code, so must not be called
862 * with any socket locks held. Protocols do call it while holding their own
863 * recursible protocol mutexes, but this is something that should be subject
864 * to review in the future.
867 soabort(struct socket *so)
871 * In as much as is possible, assert that no references to this
872 * socket are held. This is not quite the same as asserting that the
873 * current thread is responsible for arranging for no references, but
874 * is as close as we can get for now.
876 KASSERT(so->so_count == 0, ("soabort: so_count"));
877 KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
878 KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
879 KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP"));
880 KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP"));
883 if (so->so_proto->pr_usrreqs->pru_abort != NULL)
884 (*so->so_proto->pr_usrreqs->pru_abort)(so);
891 soaccept(struct socket *so, struct sockaddr **nam)
896 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
897 so->so_state &= ~SS_NOFDREF;
900 CURVNET_SET(so->so_vnet);
901 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
907 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
910 return (soconnectat(AT_FDCWD, so, nam, td));
914 soconnectat(int fd, struct socket *so, struct sockaddr *nam, struct thread *td)
918 if (so->so_options & SO_ACCEPTCONN)
921 CURVNET_SET(so->so_vnet);
923 * If protocol is connection-based, can only connect once.
924 * Otherwise, if connected, try to disconnect first. This allows
925 * user to disconnect by connecting to, e.g., a null address.
927 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
928 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
929 (error = sodisconnect(so)))) {
933 * Prevent accumulated error from previous connection from
937 if (fd == AT_FDCWD) {
938 error = (*so->so_proto->pr_usrreqs->pru_connect)(so,
941 error = (*so->so_proto->pr_usrreqs->pru_connectat)(fd,
951 soconnect2(struct socket *so1, struct socket *so2)
955 CURVNET_SET(so1->so_vnet);
956 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
962 sodisconnect(struct socket *so)
966 if ((so->so_state & SS_ISCONNECTED) == 0)
968 if (so->so_state & SS_ISDISCONNECTING)
971 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
975 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
978 sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
979 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
983 int clen = 0, error, dontroute;
985 KASSERT(so->so_type == SOCK_DGRAM, ("sosend_dgram: !SOCK_DGRAM"));
986 KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
987 ("sosend_dgram: !PR_ATOMIC"));
990 resid = uio->uio_resid;
992 resid = top->m_pkthdr.len;
994 * In theory resid should be unsigned. However, space must be
995 * signed, as it might be less than 0 if we over-committed, and we
996 * must use a signed comparison of space and resid. On the other
997 * hand, a negative resid causes us to loop sending 0-length
998 * segments to the protocol.
1006 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
1008 td->td_ru.ru_msgsnd++;
1009 if (control != NULL)
1010 clen = control->m_len;
1012 SOCKBUF_LOCK(&so->so_snd);
1013 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1014 SOCKBUF_UNLOCK(&so->so_snd);
1019 error = so->so_error;
1021 SOCKBUF_UNLOCK(&so->so_snd);
1024 if ((so->so_state & SS_ISCONNECTED) == 0) {
1026 * `sendto' and `sendmsg' is allowed on a connection-based
1027 * socket if it supports implied connect. Return ENOTCONN if
1028 * not connected and no address is supplied.
1030 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1031 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1032 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1033 !(resid == 0 && clen != 0)) {
1034 SOCKBUF_UNLOCK(&so->so_snd);
1038 } else if (addr == NULL) {
1039 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1042 error = EDESTADDRREQ;
1043 SOCKBUF_UNLOCK(&so->so_snd);
1049 * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a
1050 * problem and need fixing.
1052 space = sbspace(&so->so_snd);
1053 if (flags & MSG_OOB)
1056 SOCKBUF_UNLOCK(&so->so_snd);
1057 if (resid > space) {
1063 if (flags & MSG_EOR)
1064 top->m_flags |= M_EOR;
1067 * Copy the data from userland into a mbuf chain.
1068 * If no data is to be copied in, a single empty mbuf
1071 top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
1072 (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
1074 error = EFAULT; /* only possible error */
1077 space -= resid - uio->uio_resid;
1078 resid = uio->uio_resid;
1080 KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
1082 * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
1087 so->so_options |= SO_DONTROUTE;
1091 * XXX all the SBS_CANTSENDMORE checks previously done could be out
1092 * of date. We could have recieved a reset packet in an interrupt or
1093 * maybe we slept while doing page faults in uiomove() etc. We could
1094 * probably recheck again inside the locking protection here, but
1095 * there are probably other places that this also happens. We must
1099 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1100 (flags & MSG_OOB) ? PRUS_OOB :
1102 * If the user set MSG_EOF, the protocol understands this flag and
1103 * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
1105 ((flags & MSG_EOF) &&
1106 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1109 /* If there is more to send set PRUS_MORETOCOME */
1110 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1111 top, addr, control, td);
1114 so->so_options &= ~SO_DONTROUTE;
1123 if (control != NULL)
1129 * Send on a socket. If send must go all at once and message is larger than
1130 * send buffering, then hard error. Lock against other senders. If must go
1131 * all at once and not enough room now, then inform user that this would
1132 * block and do nothing. Otherwise, if nonblocking, send as much as
1133 * possible. The data to be sent is described by "uio" if nonzero, otherwise
1134 * by the mbuf chain "top" (which must be null if uio is not). Data provided
1135 * in mbuf chain must be small enough to send all at once.
1137 * Returns nonzero on error, timeout or signal; callers must check for short
1138 * counts if EINTR/ERESTART are returned. Data and control buffers are freed
1142 sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
1143 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1147 int clen = 0, error, dontroute;
1148 int atomic = sosendallatonce(so) || top;
1151 resid = uio->uio_resid;
1153 resid = top->m_pkthdr.len;
1155 * In theory resid should be unsigned. However, space must be
1156 * signed, as it might be less than 0 if we over-committed, and we
1157 * must use a signed comparison of space and resid. On the other
1158 * hand, a negative resid causes us to loop sending 0-length
1159 * segments to the protocol.
1161 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
1162 * type sockets since that's an error.
1164 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
1170 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
1171 (so->so_proto->pr_flags & PR_ATOMIC);
1173 td->td_ru.ru_msgsnd++;
1174 if (control != NULL)
1175 clen = control->m_len;
1177 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
1183 SOCKBUF_LOCK(&so->so_snd);
1184 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1185 SOCKBUF_UNLOCK(&so->so_snd);
1190 error = so->so_error;
1192 SOCKBUF_UNLOCK(&so->so_snd);
1195 if ((so->so_state & SS_ISCONNECTED) == 0) {
1197 * `sendto' and `sendmsg' is allowed on a connection-
1198 * based socket if it supports implied connect.
1199 * Return ENOTCONN if not connected and no address is
1202 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
1203 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
1204 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
1205 !(resid == 0 && clen != 0)) {
1206 SOCKBUF_UNLOCK(&so->so_snd);
1210 } else if (addr == NULL) {
1211 SOCKBUF_UNLOCK(&so->so_snd);
1212 if (so->so_proto->pr_flags & PR_CONNREQUIRED)
1215 error = EDESTADDRREQ;
1219 space = sbspace(&so->so_snd);
1220 if (flags & MSG_OOB)
1222 if ((atomic && resid > so->so_snd.sb_hiwat) ||
1223 clen > so->so_snd.sb_hiwat) {
1224 SOCKBUF_UNLOCK(&so->so_snd);
1228 if (space < resid + clen &&
1229 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
1230 if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
1231 SOCKBUF_UNLOCK(&so->so_snd);
1232 error = EWOULDBLOCK;
1235 error = sbwait(&so->so_snd);
1236 SOCKBUF_UNLOCK(&so->so_snd);
1241 SOCKBUF_UNLOCK(&so->so_snd);
1246 if (flags & MSG_EOR)
1247 top->m_flags |= M_EOR;
1250 * Copy the data from userland into a mbuf
1251 * chain. If no data is to be copied in,
1252 * a single empty mbuf is returned.
1254 top = m_uiotombuf(uio, M_WAITOK, space,
1255 (atomic ? max_hdr : 0),
1256 (atomic ? M_PKTHDR : 0) |
1257 ((flags & MSG_EOR) ? M_EOR : 0));
1259 error = EFAULT; /* only possible error */
1262 space -= resid - uio->uio_resid;
1263 resid = uio->uio_resid;
1267 so->so_options |= SO_DONTROUTE;
1271 * XXX all the SBS_CANTSENDMORE checks previously
1272 * done could be out of date. We could have recieved
1273 * a reset packet in an interrupt or maybe we slept
1274 * while doing page faults in uiomove() etc. We
1275 * could probably recheck again inside the locking
1276 * protection here, but there are probably other
1277 * places that this also happens. We must rethink
1281 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
1282 (flags & MSG_OOB) ? PRUS_OOB :
1284 * If the user set MSG_EOF, the protocol understands
1285 * this flag and nothing left to send then use
1286 * PRU_SEND_EOF instead of PRU_SEND.
1288 ((flags & MSG_EOF) &&
1289 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
1292 /* If there is more to send set PRUS_MORETOCOME. */
1293 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
1294 top, addr, control, td);
1297 so->so_options &= ~SO_DONTROUTE;
1305 } while (resid && space > 0);
1309 sbunlock(&so->so_snd);
1313 if (control != NULL)
1319 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
1320 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
1324 CURVNET_SET(so->so_vnet);
1325 error = so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
1326 control, flags, td);
1332 * The part of soreceive() that implements reading non-inline out-of-band
1333 * data from a socket. For more complete comments, see soreceive(), from
1334 * which this code originated.
1336 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1337 * unable to return an mbuf chain to the caller.
1340 soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
1342 struct protosw *pr = so->so_proto;
1346 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1349 m = m_get(M_WAITOK, MT_DATA);
1350 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
1354 error = uiomove(mtod(m, void *),
1355 (int) min(uio->uio_resid, m->m_len), uio);
1357 } while (uio->uio_resid && error == 0 && m);
1365 * Following replacement or removal of the first mbuf on the first mbuf chain
1366 * of a socket buffer, push necessary state changes back into the socket
1367 * buffer so that other consumers see the values consistently. 'nextrecord'
1368 * is the callers locally stored value of the original value of
1369 * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
1370 * NOTE: 'nextrecord' may be NULL.
1372 static __inline void
1373 sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
1376 SOCKBUF_LOCK_ASSERT(sb);
1378 * First, update for the new value of nextrecord. If necessary, make
1379 * it the first record.
1381 if (sb->sb_mb != NULL)
1382 sb->sb_mb->m_nextpkt = nextrecord;
1384 sb->sb_mb = nextrecord;
1387 * Now update any dependent socket buffer fields to reflect the new
1388 * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the
1389 * addition of a second clause that takes care of the case where
1390 * sb_mb has been updated, but remains the last record.
1392 if (sb->sb_mb == NULL) {
1393 sb->sb_mbtail = NULL;
1394 sb->sb_lastrecord = NULL;
1395 } else if (sb->sb_mb->m_nextpkt == NULL)
1396 sb->sb_lastrecord = sb->sb_mb;
1400 * Implement receive operations on a socket. We depend on the way that
1401 * records are added to the sockbuf by sbappend. In particular, each record
1402 * (mbufs linked through m_next) must begin with an address if the protocol
1403 * so specifies, followed by an optional mbuf or mbufs containing ancillary
1404 * data, and then zero or more mbufs of data. In order to allow parallelism
1405 * between network receive and copying to user space, as well as avoid
1406 * sleeping with a mutex held, we release the socket buffer mutex during the
1407 * user space copy. Although the sockbuf is locked, new data may still be
1408 * appended, and thus we must maintain consistency of the sockbuf during that
1411 * The caller may receive the data as a single mbuf chain by supplying an
1412 * mbuf **mp0 for use in returning the chain. The uio is then used only for
1413 * the count in uio_resid.
1416 soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
1417 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1419 struct mbuf *m, **mp;
1420 int flags, error, offset;
1422 struct protosw *pr = so->so_proto;
1423 struct mbuf *nextrecord;
1425 ssize_t orig_resid = uio->uio_resid;
1430 if (controlp != NULL)
1433 flags = *flagsp &~ MSG_EOR;
1436 if (flags & MSG_OOB)
1437 return (soreceive_rcvoob(so, uio, flags));
1440 if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
1441 && uio->uio_resid) {
1443 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
1446 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
1451 SOCKBUF_LOCK(&so->so_rcv);
1452 m = so->so_rcv.sb_mb;
1454 * If we have less data than requested, block awaiting more (subject
1455 * to any timeout) if:
1456 * 1. the current count is less than the low water mark, or
1457 * 2. MSG_DONTWAIT is not set
1459 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1460 so->so_rcv.sb_cc < uio->uio_resid) &&
1461 so->so_rcv.sb_cc < so->so_rcv.sb_lowat &&
1462 m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
1463 KASSERT(m != NULL || !so->so_rcv.sb_cc,
1464 ("receive: m == %p so->so_rcv.sb_cc == %u",
1465 m, so->so_rcv.sb_cc));
1469 error = so->so_error;
1470 if ((flags & MSG_PEEK) == 0)
1472 SOCKBUF_UNLOCK(&so->so_rcv);
1475 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1476 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1478 SOCKBUF_UNLOCK(&so->so_rcv);
1483 for (; m != NULL; m = m->m_next)
1484 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1485 m = so->so_rcv.sb_mb;
1488 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1489 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
1490 SOCKBUF_UNLOCK(&so->so_rcv);
1494 if (uio->uio_resid == 0) {
1495 SOCKBUF_UNLOCK(&so->so_rcv);
1498 if ((so->so_state & SS_NBIO) ||
1499 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
1500 SOCKBUF_UNLOCK(&so->so_rcv);
1501 error = EWOULDBLOCK;
1504 SBLASTRECORDCHK(&so->so_rcv);
1505 SBLASTMBUFCHK(&so->so_rcv);
1506 error = sbwait(&so->so_rcv);
1507 SOCKBUF_UNLOCK(&so->so_rcv);
1514 * From this point onward, we maintain 'nextrecord' as a cache of the
1515 * pointer to the next record in the socket buffer. We must keep the
1516 * various socket buffer pointers and local stack versions of the
1517 * pointers in sync, pushing out modifications before dropping the
1518 * socket buffer mutex, and re-reading them when picking it up.
1520 * Otherwise, we will race with the network stack appending new data
1521 * or records onto the socket buffer by using inconsistent/stale
1522 * versions of the field, possibly resulting in socket buffer
1525 * By holding the high-level sblock(), we prevent simultaneous
1526 * readers from pulling off the front of the socket buffer.
1528 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1530 uio->uio_td->td_ru.ru_msgrcv++;
1531 KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
1532 SBLASTRECORDCHK(&so->so_rcv);
1533 SBLASTMBUFCHK(&so->so_rcv);
1534 nextrecord = m->m_nextpkt;
1535 if (pr->pr_flags & PR_ADDR) {
1536 KASSERT(m->m_type == MT_SONAME,
1537 ("m->m_type == %d", m->m_type));
1540 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
1542 if (flags & MSG_PEEK) {
1545 sbfree(&so->so_rcv, m);
1546 so->so_rcv.sb_mb = m_free(m);
1547 m = so->so_rcv.sb_mb;
1548 sockbuf_pushsync(&so->so_rcv, nextrecord);
1553 * Process one or more MT_CONTROL mbufs present before any data mbufs
1554 * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
1555 * just copy the data; if !MSG_PEEK, we call into the protocol to
1556 * perform externalization (or freeing if controlp == NULL).
1558 if (m != NULL && m->m_type == MT_CONTROL) {
1559 struct mbuf *cm = NULL, *cmn;
1560 struct mbuf **cme = &cm;
1563 if (flags & MSG_PEEK) {
1564 if (controlp != NULL) {
1565 *controlp = m_copy(m, 0, m->m_len);
1566 controlp = &(*controlp)->m_next;
1570 sbfree(&so->so_rcv, m);
1571 so->so_rcv.sb_mb = m->m_next;
1574 cme = &(*cme)->m_next;
1575 m = so->so_rcv.sb_mb;
1577 } while (m != NULL && m->m_type == MT_CONTROL);
1578 if ((flags & MSG_PEEK) == 0)
1579 sockbuf_pushsync(&so->so_rcv, nextrecord);
1580 while (cm != NULL) {
1583 if (pr->pr_domain->dom_externalize != NULL) {
1584 SOCKBUF_UNLOCK(&so->so_rcv);
1586 error = (*pr->pr_domain->dom_externalize)
1587 (cm, controlp, flags);
1588 SOCKBUF_LOCK(&so->so_rcv);
1589 } else if (controlp != NULL)
1593 if (controlp != NULL) {
1595 while (*controlp != NULL)
1596 controlp = &(*controlp)->m_next;
1601 nextrecord = so->so_rcv.sb_mb->m_nextpkt;
1603 nextrecord = so->so_rcv.sb_mb;
1607 if ((flags & MSG_PEEK) == 0) {
1608 KASSERT(m->m_nextpkt == nextrecord,
1609 ("soreceive: post-control, nextrecord !sync"));
1610 if (nextrecord == NULL) {
1611 KASSERT(so->so_rcv.sb_mb == m,
1612 ("soreceive: post-control, sb_mb!=m"));
1613 KASSERT(so->so_rcv.sb_lastrecord == m,
1614 ("soreceive: post-control, lastrecord!=m"));
1618 if (type == MT_OOBDATA)
1621 if ((flags & MSG_PEEK) == 0) {
1622 KASSERT(so->so_rcv.sb_mb == nextrecord,
1623 ("soreceive: sb_mb != nextrecord"));
1624 if (so->so_rcv.sb_mb == NULL) {
1625 KASSERT(so->so_rcv.sb_lastrecord == NULL,
1626 ("soreceive: sb_lastercord != NULL"));
1630 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1631 SBLASTRECORDCHK(&so->so_rcv);
1632 SBLASTMBUFCHK(&so->so_rcv);
1635 * Now continue to read any data mbufs off of the head of the socket
1636 * buffer until the read request is satisfied. Note that 'type' is
1637 * used to store the type of any mbuf reads that have happened so far
1638 * such that soreceive() can stop reading if the type changes, which
1639 * causes soreceive() to return only one of regular data and inline
1640 * out-of-band data in a single socket receive operation.
1644 while (m != NULL && uio->uio_resid > 0 && error == 0) {
1646 * If the type of mbuf has changed since the last mbuf
1647 * examined ('type'), end the receive operation.
1649 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1650 if (m->m_type == MT_OOBDATA || m->m_type == MT_CONTROL) {
1651 if (type != m->m_type)
1653 } else if (type == MT_OOBDATA)
1656 KASSERT(m->m_type == MT_DATA,
1657 ("m->m_type == %d", m->m_type));
1658 so->so_rcv.sb_state &= ~SBS_RCVATMARK;
1659 len = uio->uio_resid;
1660 if (so->so_oobmark && len > so->so_oobmark - offset)
1661 len = so->so_oobmark - offset;
1662 if (len > m->m_len - moff)
1663 len = m->m_len - moff;
1665 * If mp is set, just pass back the mbufs. Otherwise copy
1666 * them out via the uio, then free. Sockbuf must be
1667 * consistent here (points to current mbuf, it points to next
1668 * record) when we drop priority; we must note any additions
1669 * to the sockbuf when we block interrupts again.
1672 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1673 SBLASTRECORDCHK(&so->so_rcv);
1674 SBLASTMBUFCHK(&so->so_rcv);
1675 SOCKBUF_UNLOCK(&so->so_rcv);
1676 error = uiomove(mtod(m, char *) + moff, (int)len, uio);
1677 SOCKBUF_LOCK(&so->so_rcv);
1680 * The MT_SONAME mbuf has already been removed
1681 * from the record, so it is necessary to
1682 * remove the data mbufs, if any, to preserve
1683 * the invariant in the case of PR_ADDR that
1684 * requires MT_SONAME mbufs at the head of
1687 if (m && pr->pr_flags & PR_ATOMIC &&
1688 ((flags & MSG_PEEK) == 0))
1689 (void)sbdroprecord_locked(&so->so_rcv);
1690 SOCKBUF_UNLOCK(&so->so_rcv);
1694 uio->uio_resid -= len;
1695 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1696 if (len == m->m_len - moff) {
1697 if (m->m_flags & M_EOR)
1699 if (flags & MSG_PEEK) {
1703 nextrecord = m->m_nextpkt;
1704 sbfree(&so->so_rcv, m);
1706 m->m_nextpkt = NULL;
1709 so->so_rcv.sb_mb = m = m->m_next;
1712 so->so_rcv.sb_mb = m_free(m);
1713 m = so->so_rcv.sb_mb;
1715 sockbuf_pushsync(&so->so_rcv, nextrecord);
1716 SBLASTRECORDCHK(&so->so_rcv);
1717 SBLASTMBUFCHK(&so->so_rcv);
1720 if (flags & MSG_PEEK)
1724 if (flags & MSG_DONTWAIT) {
1725 *mp = m_copym(m, 0, len,
1729 * m_copym() couldn't
1731 * Adjust uio_resid back
1733 * down by len bytes,
1734 * which we didn't end
1735 * up "copying" over).
1737 uio->uio_resid += len;
1741 SOCKBUF_UNLOCK(&so->so_rcv);
1742 *mp = m_copym(m, 0, len,
1744 SOCKBUF_LOCK(&so->so_rcv);
1749 so->so_rcv.sb_cc -= len;
1752 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1753 if (so->so_oobmark) {
1754 if ((flags & MSG_PEEK) == 0) {
1755 so->so_oobmark -= len;
1756 if (so->so_oobmark == 0) {
1757 so->so_rcv.sb_state |= SBS_RCVATMARK;
1762 if (offset == so->so_oobmark)
1766 if (flags & MSG_EOR)
1769 * If the MSG_WAITALL flag is set (for non-atomic socket), we
1770 * must not quit until "uio->uio_resid == 0" or an error
1771 * termination. If a signal/timeout occurs, return with a
1772 * short count but without error. Keep sockbuf locked
1773 * against other readers.
1775 while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
1776 !sosendallatonce(so) && nextrecord == NULL) {
1777 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1779 so->so_rcv.sb_state & SBS_CANTRCVMORE)
1782 * Notify the protocol that some data has been
1783 * drained before blocking.
1785 if (pr->pr_flags & PR_WANTRCVD) {
1786 SOCKBUF_UNLOCK(&so->so_rcv);
1788 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1789 SOCKBUF_LOCK(&so->so_rcv);
1791 SBLASTRECORDCHK(&so->so_rcv);
1792 SBLASTMBUFCHK(&so->so_rcv);
1794 * We could receive some data while was notifying
1795 * the protocol. Skip blocking in this case.
1797 if (so->so_rcv.sb_mb == NULL) {
1798 error = sbwait(&so->so_rcv);
1800 SOCKBUF_UNLOCK(&so->so_rcv);
1804 m = so->so_rcv.sb_mb;
1806 nextrecord = m->m_nextpkt;
1810 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1811 if (m != NULL && pr->pr_flags & PR_ATOMIC) {
1813 if ((flags & MSG_PEEK) == 0)
1814 (void) sbdroprecord_locked(&so->so_rcv);
1816 if ((flags & MSG_PEEK) == 0) {
1819 * First part is an inline SB_EMPTY_FIXUP(). Second
1820 * part makes sure sb_lastrecord is up-to-date if
1821 * there is still data in the socket buffer.
1823 so->so_rcv.sb_mb = nextrecord;
1824 if (so->so_rcv.sb_mb == NULL) {
1825 so->so_rcv.sb_mbtail = NULL;
1826 so->so_rcv.sb_lastrecord = NULL;
1827 } else if (nextrecord->m_nextpkt == NULL)
1828 so->so_rcv.sb_lastrecord = nextrecord;
1830 SBLASTRECORDCHK(&so->so_rcv);
1831 SBLASTMBUFCHK(&so->so_rcv);
1833 * If soreceive() is being done from the socket callback,
1834 * then don't need to generate ACK to peer to update window,
1835 * since ACK will be generated on return to TCP.
1837 if (!(flags & MSG_SOCALLBCK) &&
1838 (pr->pr_flags & PR_WANTRCVD)) {
1839 SOCKBUF_UNLOCK(&so->so_rcv);
1841 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
1842 SOCKBUF_LOCK(&so->so_rcv);
1845 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1846 if (orig_resid == uio->uio_resid && orig_resid &&
1847 (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
1848 SOCKBUF_UNLOCK(&so->so_rcv);
1851 SOCKBUF_UNLOCK(&so->so_rcv);
1856 sbunlock(&so->so_rcv);
1861 * Optimized version of soreceive() for stream (TCP) sockets.
1862 * XXXAO: (MSG_WAITALL | MSG_PEEK) isn't properly handled.
1865 soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio,
1866 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
1868 int len = 0, error = 0, flags, oresid;
1870 struct mbuf *m, *n = NULL;
1872 /* We only do stream sockets. */
1873 if (so->so_type != SOCK_STREAM)
1877 if (controlp != NULL)
1880 flags = *flagsp &~ MSG_EOR;
1883 if (flags & MSG_OOB)
1884 return (soreceive_rcvoob(so, uio, flags));
1890 /* Prevent other readers from entering the socket. */
1891 error = sblock(sb, SBLOCKWAIT(flags));
1896 /* Easy one, no space to copyout anything. */
1897 if (uio->uio_resid == 0) {
1901 oresid = uio->uio_resid;
1903 /* We will never ever get anything unless we are or were connected. */
1904 if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
1910 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1912 /* Abort if socket has reported problems. */
1916 if (oresid > uio->uio_resid)
1918 error = so->so_error;
1919 if (!(flags & MSG_PEEK))
1924 /* Door is closed. Deliver what is left, if any. */
1925 if (sb->sb_state & SBS_CANTRCVMORE) {
1932 /* Socket buffer is empty and we shall not block. */
1933 if (sb->sb_cc == 0 &&
1934 ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
1939 /* Socket buffer got some data that we shall deliver now. */
1940 if (sb->sb_cc > 0 && !(flags & MSG_WAITALL) &&
1941 ((sb->sb_flags & SS_NBIO) ||
1942 (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
1943 sb->sb_cc >= sb->sb_lowat ||
1944 sb->sb_cc >= uio->uio_resid ||
1945 sb->sb_cc >= sb->sb_hiwat) ) {
1949 /* On MSG_WAITALL we must wait until all data or error arrives. */
1950 if ((flags & MSG_WAITALL) &&
1951 (sb->sb_cc >= uio->uio_resid || sb->sb_cc >= sb->sb_hiwat))
1955 * Wait and block until (more) data comes in.
1956 * NB: Drops the sockbuf lock during wait.
1964 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1965 KASSERT(sb->sb_cc > 0, ("%s: sockbuf empty", __func__));
1966 KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
1970 uio->uio_td->td_ru.ru_msgrcv++;
1972 /* Fill uio until full or current end of socket buffer is reached. */
1973 len = min(uio->uio_resid, sb->sb_cc);
1975 /* Dequeue as many mbufs as possible. */
1976 if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
1980 m_cat(*mp0, sb->sb_mb);
1982 m != NULL && m->m_len <= len;
1985 uio->uio_resid -= m->m_len;
1991 sb->sb_lastrecord = sb->sb_mb;
1992 if (sb->sb_mb == NULL)
1995 /* Copy the remainder. */
1997 KASSERT(sb->sb_mb != NULL,
1998 ("%s: len > 0 && sb->sb_mb empty", __func__));
2000 m = m_copym(sb->sb_mb, 0, len, M_NOWAIT);
2002 len = 0; /* Don't flush data from sockbuf. */
2004 uio->uio_resid -= len;
2015 /* NB: Must unlock socket buffer as uiomove may sleep. */
2017 error = m_mbuftouio(uio, sb->sb_mb, len);
2022 SBLASTRECORDCHK(sb);
2026 * Remove the delivered data from the socket buffer unless we
2027 * were only peeking.
2029 if (!(flags & MSG_PEEK)) {
2031 sbdrop_locked(sb, len);
2033 /* Notify protocol that we drained some data. */
2034 if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
2035 (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
2036 !(flags & MSG_SOCALLBCK))) {
2039 (*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags);
2045 * For MSG_WAITALL we may have to loop again and wait for
2046 * more data to come in.
2048 if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
2051 SOCKBUF_LOCK_ASSERT(sb);
2052 SBLASTRECORDCHK(sb);
2060 * Optimized version of soreceive() for simple datagram cases from userspace.
2061 * Unlike in the stream case, we're able to drop a datagram if copyout()
2062 * fails, and because we handle datagrams atomically, we don't need to use a
2063 * sleep lock to prevent I/O interlacing.
2066 soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
2067 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2069 struct mbuf *m, *m2;
2072 struct protosw *pr = so->so_proto;
2073 struct mbuf *nextrecord;
2077 if (controlp != NULL)
2080 flags = *flagsp &~ MSG_EOR;
2085 * For any complicated cases, fall back to the full
2086 * soreceive_generic().
2088 if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
2089 return (soreceive_generic(so, psa, uio, mp0, controlp,
2093 * Enforce restrictions on use.
2095 KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
2096 ("soreceive_dgram: wantrcvd"));
2097 KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
2098 KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
2099 ("soreceive_dgram: SBS_RCVATMARK"));
2100 KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
2101 ("soreceive_dgram: P_CONNREQUIRED"));
2104 * Loop blocking while waiting for a datagram.
2106 SOCKBUF_LOCK(&so->so_rcv);
2107 while ((m = so->so_rcv.sb_mb) == NULL) {
2108 KASSERT(so->so_rcv.sb_cc == 0,
2109 ("soreceive_dgram: sb_mb NULL but sb_cc %u",
2112 error = so->so_error;
2114 SOCKBUF_UNLOCK(&so->so_rcv);
2117 if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
2118 uio->uio_resid == 0) {
2119 SOCKBUF_UNLOCK(&so->so_rcv);
2122 if ((so->so_state & SS_NBIO) ||
2123 (flags & (MSG_DONTWAIT|MSG_NBIO))) {
2124 SOCKBUF_UNLOCK(&so->so_rcv);
2125 return (EWOULDBLOCK);
2127 SBLASTRECORDCHK(&so->so_rcv);
2128 SBLASTMBUFCHK(&so->so_rcv);
2129 error = sbwait(&so->so_rcv);
2131 SOCKBUF_UNLOCK(&so->so_rcv);
2135 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
2138 uio->uio_td->td_ru.ru_msgrcv++;
2139 SBLASTRECORDCHK(&so->so_rcv);
2140 SBLASTMBUFCHK(&so->so_rcv);
2141 nextrecord = m->m_nextpkt;
2142 if (nextrecord == NULL) {
2143 KASSERT(so->so_rcv.sb_lastrecord == m,
2144 ("soreceive_dgram: lastrecord != m"));
2147 KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
2148 ("soreceive_dgram: m_nextpkt != nextrecord"));
2151 * Pull 'm' and its chain off the front of the packet queue.
2153 so->so_rcv.sb_mb = NULL;
2154 sockbuf_pushsync(&so->so_rcv, nextrecord);
2157 * Walk 'm's chain and free that many bytes from the socket buffer.
2159 for (m2 = m; m2 != NULL; m2 = m2->m_next)
2160 sbfree(&so->so_rcv, m2);
2163 * Do a few last checks before we let go of the lock.
2165 SBLASTRECORDCHK(&so->so_rcv);
2166 SBLASTMBUFCHK(&so->so_rcv);
2167 SOCKBUF_UNLOCK(&so->so_rcv);
2169 if (pr->pr_flags & PR_ADDR) {
2170 KASSERT(m->m_type == MT_SONAME,
2171 ("m->m_type == %d", m->m_type));
2173 *psa = sodupsockaddr(mtod(m, struct sockaddr *),
2178 /* XXXRW: Can this happen? */
2183 * Packet to copyout() is now in 'm' and it is disconnected from the
2186 * Process one or more MT_CONTROL mbufs present before any data mbufs
2187 * in the first mbuf chain on the socket buffer. We call into the
2188 * protocol to perform externalization (or freeing if controlp ==
2191 if (m->m_type == MT_CONTROL) {
2192 struct mbuf *cm = NULL, *cmn;
2193 struct mbuf **cme = &cm;
2199 cme = &(*cme)->m_next;
2201 } while (m != NULL && m->m_type == MT_CONTROL);
2202 while (cm != NULL) {
2205 if (pr->pr_domain->dom_externalize != NULL) {
2206 error = (*pr->pr_domain->dom_externalize)
2207 (cm, controlp, flags);
2208 } else if (controlp != NULL)
2212 if (controlp != NULL) {
2213 while (*controlp != NULL)
2214 controlp = &(*controlp)->m_next;
2219 KASSERT(m->m_type == MT_DATA, ("soreceive_dgram: !data"));
2221 while (m != NULL && uio->uio_resid > 0) {
2222 len = uio->uio_resid;
2225 error = uiomove(mtod(m, char *), (int)len, uio);
2230 if (len == m->m_len)
2246 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
2247 struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
2251 CURVNET_SET(so->so_vnet);
2252 error = (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
2259 soshutdown(struct socket *so, int how)
2261 struct protosw *pr = so->so_proto;
2264 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
2267 CURVNET_SET(so->so_vnet);
2268 if (pr->pr_usrreqs->pru_flush != NULL)
2269 (*pr->pr_usrreqs->pru_flush)(so, how);
2272 if (how != SHUT_RD) {
2273 error = (*pr->pr_usrreqs->pru_shutdown)(so);
2274 wakeup(&so->so_timeo);
2278 wakeup(&so->so_timeo);
2284 sorflush(struct socket *so)
2286 struct sockbuf *sb = &so->so_rcv;
2287 struct protosw *pr = so->so_proto;
2293 * In order to avoid calling dom_dispose with the socket buffer mutex
2294 * held, and in order to generally avoid holding the lock for a long
2295 * time, we make a copy of the socket buffer and clear the original
2296 * (except locks, state). The new socket buffer copy won't have
2297 * initialized locks so we can only call routines that won't use or
2298 * assert those locks.
2300 * Dislodge threads currently blocked in receive and wait to acquire
2301 * a lock against other simultaneous readers before clearing the
2302 * socket buffer. Don't let our acquire be interrupted by a signal
2303 * despite any existing socket disposition on interruptable waiting.
2306 (void) sblock(sb, SBL_WAIT | SBL_NOINTR);
2309 * Invalidate/clear most of the sockbuf structure, but leave selinfo
2310 * and mutex data unchanged.
2313 bzero(&asb, offsetof(struct sockbuf, sb_startzero));
2314 bcopy(&sb->sb_startzero, &asb.sb_startzero,
2315 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2316 bzero(&sb->sb_startzero,
2317 sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
2322 * Dispose of special rights and flush the socket buffer. Don't call
2323 * any unsafe routines (that rely on locks being initialized) on asb.
2325 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
2326 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
2327 sbrelease_internal(&asb, so);
2331 * Perhaps this routine, and sooptcopyout(), below, ought to come in an
2332 * additional variant to handle the case where the option value needs to be
2333 * some kind of integer, but not a specific size. In addition to their use
2334 * here, these functions are also called by the protocol-level pr_ctloutput()
2338 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2343 * If the user gives us more than we wanted, we ignore it, but if we
2344 * don't get the minimum length the caller wants, we return EINVAL.
2345 * On success, sopt->sopt_valsize is set to however much we actually
2348 if ((valsize = sopt->sopt_valsize) < minlen)
2351 sopt->sopt_valsize = valsize = len;
2353 if (sopt->sopt_td != NULL)
2354 return (copyin(sopt->sopt_val, buf, valsize));
2356 bcopy(sopt->sopt_val, buf, valsize);
2361 * Kernel version of setsockopt(2).
2363 * XXX: optlen is size_t, not socklen_t
2366 so_setsockopt(struct socket *so, int level, int optname, void *optval,
2369 struct sockopt sopt;
2371 sopt.sopt_level = level;
2372 sopt.sopt_name = optname;
2373 sopt.sopt_dir = SOPT_SET;
2374 sopt.sopt_val = optval;
2375 sopt.sopt_valsize = optlen;
2376 sopt.sopt_td = NULL;
2377 return (sosetopt(so, &sopt));
2381 sosetopt(struct socket *so, struct sockopt *sopt)
2392 CURVNET_SET(so->so_vnet);
2394 if (sopt->sopt_level != SOL_SOCKET) {
2395 if (so->so_proto->pr_ctloutput != NULL) {
2396 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2400 error = ENOPROTOOPT;
2402 switch (sopt->sopt_name) {
2403 case SO_ACCEPTFILTER:
2404 error = do_setopt_accept_filter(so, sopt);
2410 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2415 so->so_linger = l.l_linger;
2417 so->so_options |= SO_LINGER;
2419 so->so_options &= ~SO_LINGER;
2426 case SO_USELOOPBACK:
2436 error = sooptcopyin(sopt, &optval, sizeof optval,
2442 so->so_options |= sopt->sopt_name;
2444 so->so_options &= ~sopt->sopt_name;
2449 error = sooptcopyin(sopt, &optval, sizeof optval,
2454 if (optval < 0 || optval >= rt_numfibs) {
2458 if (((so->so_proto->pr_domain->dom_family == PF_INET) ||
2459 (so->so_proto->pr_domain->dom_family == PF_INET6) ||
2460 (so->so_proto->pr_domain->dom_family == PF_ROUTE)))
2461 so->so_fibnum = optval;
2466 case SO_USER_COOKIE:
2467 error = sooptcopyin(sopt, &val32, sizeof val32,
2471 so->so_user_cookie = val32;
2478 error = sooptcopyin(sopt, &optval, sizeof optval,
2484 * Values < 1 make no sense for any of these options,
2492 switch (sopt->sopt_name) {
2495 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
2496 &so->so_snd : &so->so_rcv, (u_long)optval,
2497 so, curthread) == 0) {
2501 (sopt->sopt_name == SO_SNDBUF ? &so->so_snd :
2502 &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE;
2506 * Make sure the low-water is never greater than the
2510 SOCKBUF_LOCK(&so->so_snd);
2511 so->so_snd.sb_lowat =
2512 (optval > so->so_snd.sb_hiwat) ?
2513 so->so_snd.sb_hiwat : optval;
2514 SOCKBUF_UNLOCK(&so->so_snd);
2517 SOCKBUF_LOCK(&so->so_rcv);
2518 so->so_rcv.sb_lowat =
2519 (optval > so->so_rcv.sb_hiwat) ?
2520 so->so_rcv.sb_hiwat : optval;
2521 SOCKBUF_UNLOCK(&so->so_rcv);
2528 #ifdef COMPAT_FREEBSD32
2529 if (SV_CURPROC_FLAG(SV_ILP32)) {
2530 struct timeval32 tv32;
2532 error = sooptcopyin(sopt, &tv32, sizeof tv32,
2534 CP(tv32, tv, tv_sec);
2535 CP(tv32, tv, tv_usec);
2538 error = sooptcopyin(sopt, &tv, sizeof tv,
2542 if (tv.tv_sec < 0 || tv.tv_usec < 0 ||
2543 tv.tv_usec >= 1000000) {
2547 if (tv.tv_sec > INT32_MAX)
2551 switch (sopt->sopt_name) {
2553 so->so_snd.sb_timeo = val;
2556 so->so_rcv.sb_timeo = val;
2563 error = sooptcopyin(sopt, &extmac, sizeof extmac,
2567 error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
2575 error = ENOPROTOOPT;
2578 if (error == 0 && so->so_proto->pr_ctloutput != NULL)
2579 (void)(*so->so_proto->pr_ctloutput)(so, sopt);
2587 * Helper routine for getsockopt.
2590 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
2598 * Documented get behavior is that we always return a value, possibly
2599 * truncated to fit in the user's buffer. Traditional behavior is
2600 * that we always tell the user precisely how much we copied, rather
2601 * than something useful like the total amount we had available for
2602 * her. Note that this interface is not idempotent; the entire
2603 * answer must generated ahead of time.
2605 valsize = min(len, sopt->sopt_valsize);
2606 sopt->sopt_valsize = valsize;
2607 if (sopt->sopt_val != NULL) {
2608 if (sopt->sopt_td != NULL)
2609 error = copyout(buf, sopt->sopt_val, valsize);
2611 bcopy(buf, sopt->sopt_val, valsize);
2617 sogetopt(struct socket *so, struct sockopt *sopt)
2626 CURVNET_SET(so->so_vnet);
2628 if (sopt->sopt_level != SOL_SOCKET) {
2629 if (so->so_proto->pr_ctloutput != NULL)
2630 error = (*so->so_proto->pr_ctloutput)(so, sopt);
2632 error = ENOPROTOOPT;
2636 switch (sopt->sopt_name) {
2637 case SO_ACCEPTFILTER:
2638 error = do_getopt_accept_filter(so, sopt);
2643 l.l_onoff = so->so_options & SO_LINGER;
2644 l.l_linger = so->so_linger;
2646 error = sooptcopyout(sopt, &l, sizeof l);
2649 case SO_USELOOPBACK:
2661 optval = so->so_options & sopt->sopt_name;
2663 error = sooptcopyout(sopt, &optval, sizeof optval);
2667 optval = so->so_type;
2671 optval = so->so_proto->pr_protocol;
2676 optval = so->so_error;
2682 optval = so->so_snd.sb_hiwat;
2686 optval = so->so_rcv.sb_hiwat;
2690 optval = so->so_snd.sb_lowat;
2694 optval = so->so_rcv.sb_lowat;
2699 tv = sbttotv(sopt->sopt_name == SO_SNDTIMEO ?
2700 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
2701 #ifdef COMPAT_FREEBSD32
2702 if (SV_CURPROC_FLAG(SV_ILP32)) {
2703 struct timeval32 tv32;
2705 CP(tv, tv32, tv_sec);
2706 CP(tv, tv32, tv_usec);
2707 error = sooptcopyout(sopt, &tv32, sizeof tv32);
2710 error = sooptcopyout(sopt, &tv, sizeof tv);
2715 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2719 error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
2723 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2731 error = sooptcopyin(sopt, &extmac, sizeof(extmac),
2735 error = mac_getsockopt_peerlabel(
2736 sopt->sopt_td->td_ucred, so, &extmac);
2739 error = sooptcopyout(sopt, &extmac, sizeof extmac);
2745 case SO_LISTENQLIMIT:
2746 optval = so->so_qlimit;
2750 optval = so->so_qlen;
2753 case SO_LISTENINCQLEN:
2754 optval = so->so_incqlen;
2758 error = ENOPROTOOPT;
2770 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2772 struct mbuf *m, *m_prev;
2773 int sopt_size = sopt->sopt_valsize;
2775 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
2778 if (sopt_size > MLEN) {
2779 MCLGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT);
2780 if ((m->m_flags & M_EXT) == 0) {
2784 m->m_len = min(MCLBYTES, sopt_size);
2786 m->m_len = min(MLEN, sopt_size);
2788 sopt_size -= m->m_len;
2793 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
2798 if (sopt_size > MLEN) {
2799 MCLGET(m, sopt->sopt_td != NULL ? M_WAITOK :
2801 if ((m->m_flags & M_EXT) == 0) {
2806 m->m_len = min(MCLBYTES, sopt_size);
2808 m->m_len = min(MLEN, sopt_size);
2810 sopt_size -= m->m_len;
2818 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
2820 struct mbuf *m0 = m;
2822 if (sopt->sopt_val == NULL)
2824 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2825 if (sopt->sopt_td != NULL) {
2828 error = copyin(sopt->sopt_val, mtod(m, char *),
2835 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
2836 sopt->sopt_valsize -= m->m_len;
2837 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2840 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2841 panic("ip6_sooptmcopyin");
2846 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
2848 struct mbuf *m0 = m;
2851 if (sopt->sopt_val == NULL)
2853 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
2854 if (sopt->sopt_td != NULL) {
2857 error = copyout(mtod(m, char *), sopt->sopt_val,
2864 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
2865 sopt->sopt_valsize -= m->m_len;
2866 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
2867 valsize += m->m_len;
2871 /* enough soopt buffer should be given from user-land */
2875 sopt->sopt_valsize = valsize;
2880 * sohasoutofband(): protocol notifies socket layer of the arrival of new
2881 * out-of-band data, which will then notify socket consumers.
2884 sohasoutofband(struct socket *so)
2887 if (so->so_sigio != NULL)
2888 pgsigio(&so->so_sigio, SIGURG, 0);
2889 selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
2893 sopoll(struct socket *so, int events, struct ucred *active_cred,
2898 * We do not need to set or assert curvnet as long as everyone uses
2901 return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
2906 sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
2911 SOCKBUF_LOCK(&so->so_snd);
2912 SOCKBUF_LOCK(&so->so_rcv);
2913 if (events & (POLLIN | POLLRDNORM))
2914 if (soreadabledata(so))
2915 revents |= events & (POLLIN | POLLRDNORM);
2917 if (events & (POLLOUT | POLLWRNORM))
2918 if (sowriteable(so))
2919 revents |= events & (POLLOUT | POLLWRNORM);
2921 if (events & (POLLPRI | POLLRDBAND))
2922 if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
2923 revents |= events & (POLLPRI | POLLRDBAND);
2925 if ((events & POLLINIGNEOF) == 0) {
2926 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
2927 revents |= events & (POLLIN | POLLRDNORM);
2928 if (so->so_snd.sb_state & SBS_CANTSENDMORE)
2934 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
2935 selrecord(td, &so->so_rcv.sb_sel);
2936 so->so_rcv.sb_flags |= SB_SEL;
2939 if (events & (POLLOUT | POLLWRNORM)) {
2940 selrecord(td, &so->so_snd.sb_sel);
2941 so->so_snd.sb_flags |= SB_SEL;
2945 SOCKBUF_UNLOCK(&so->so_rcv);
2946 SOCKBUF_UNLOCK(&so->so_snd);
2951 soo_kqfilter(struct file *fp, struct knote *kn)
2953 struct socket *so = kn->kn_fp->f_data;
2956 switch (kn->kn_filter) {
2958 if (so->so_options & SO_ACCEPTCONN)
2959 kn->kn_fop = &solisten_filtops;
2961 kn->kn_fop = &soread_filtops;
2965 kn->kn_fop = &sowrite_filtops;
2973 knlist_add(&sb->sb_sel.si_note, kn, 1);
2974 sb->sb_flags |= SB_KNOTE;
2980 * Some routines that return EOPNOTSUPP for entry points that are not
2981 * supported by a protocol. Fill in as needed.
2984 pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
2991 pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
2998 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3005 pru_bindat_notsupp(int fd, struct socket *so, struct sockaddr *nam,
3013 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
3020 pru_connectat_notsupp(int fd, struct socket *so, struct sockaddr *nam,
3028 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
3035 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
3036 struct ifnet *ifp, struct thread *td)
3043 pru_disconnect_notsupp(struct socket *so)
3050 pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
3057 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
3064 pru_rcvd_notsupp(struct socket *so, int flags)
3071 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
3078 pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
3079 struct sockaddr *addr, struct mbuf *control, struct thread *td)
3086 * This isn't really a ``null'' operation, but it's the default one and
3087 * doesn't do anything destructive.
3090 pru_sense_null(struct socket *so, struct stat *sb)
3093 sb->st_blksize = so->so_snd.sb_hiwat;
3098 pru_shutdown_notsupp(struct socket *so)
3105 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
3112 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
3113 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
3120 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
3121 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
3128 pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
3136 filt_sordetach(struct knote *kn)
3138 struct socket *so = kn->kn_fp->f_data;
3140 SOCKBUF_LOCK(&so->so_rcv);
3141 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
3142 if (knlist_empty(&so->so_rcv.sb_sel.si_note))
3143 so->so_rcv.sb_flags &= ~SB_KNOTE;
3144 SOCKBUF_UNLOCK(&so->so_rcv);
3149 filt_soread(struct knote *kn, long hint)
3153 so = kn->kn_fp->f_data;
3154 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
3156 kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
3157 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
3158 kn->kn_flags |= EV_EOF;
3159 kn->kn_fflags = so->so_error;
3161 } else if (so->so_error) /* temporary udp error */
3163 else if (kn->kn_sfflags & NOTE_LOWAT)
3164 return (kn->kn_data >= kn->kn_sdata);
3166 return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
3170 filt_sowdetach(struct knote *kn)
3172 struct socket *so = kn->kn_fp->f_data;
3174 SOCKBUF_LOCK(&so->so_snd);
3175 knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
3176 if (knlist_empty(&so->so_snd.sb_sel.si_note))
3177 so->so_snd.sb_flags &= ~SB_KNOTE;
3178 SOCKBUF_UNLOCK(&so->so_snd);
3183 filt_sowrite(struct knote *kn, long hint)
3187 so = kn->kn_fp->f_data;
3188 SOCKBUF_LOCK_ASSERT(&so->so_snd);
3189 kn->kn_data = sbspace(&so->so_snd);
3190 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
3191 kn->kn_flags |= EV_EOF;
3192 kn->kn_fflags = so->so_error;
3194 } else if (so->so_error) /* temporary udp error */
3196 else if (((so->so_state & SS_ISCONNECTED) == 0) &&
3197 (so->so_proto->pr_flags & PR_CONNREQUIRED))
3199 else if (kn->kn_sfflags & NOTE_LOWAT)
3200 return (kn->kn_data >= kn->kn_sdata);
3202 return (kn->kn_data >= so->so_snd.sb_lowat);
3207 filt_solisten(struct knote *kn, long hint)
3209 struct socket *so = kn->kn_fp->f_data;
3211 kn->kn_data = so->so_qlen;
3212 return (!TAILQ_EMPTY(&so->so_comp));
3216 socheckuid(struct socket *so, uid_t uid)
3221 if (so->so_cred->cr_uid != uid)
3227 * These functions are used by protocols to notify the socket layer (and its
3228 * consumers) of state changes in the sockets driven by protocol-side events.
3232 * Procedures to manipulate state flags of socket and do appropriate wakeups.
3234 * Normal sequence from the active (originating) side is that
3235 * soisconnecting() is called during processing of connect() call, resulting
3236 * in an eventual call to soisconnected() if/when the connection is
3237 * established. When the connection is torn down soisdisconnecting() is
3238 * called during processing of disconnect() call, and soisdisconnected() is
3239 * called when the connection to the peer is totally severed. The semantics
3240 * of these routines are such that connectionless protocols can call
3241 * soisconnected() and soisdisconnected() only, bypassing the in-progress
3242 * calls when setting up a ``connection'' takes no time.
3244 * From the passive side, a socket is created with two queues of sockets:
3245 * so_incomp for connections in progress and so_comp for connections already
3246 * made and awaiting user acceptance. As a protocol is preparing incoming
3247 * connections, it creates a socket structure queued on so_incomp by calling
3248 * sonewconn(). When the connection is established, soisconnected() is
3249 * called, and transfers the socket structure to so_comp, making it available
3252 * If a socket is closed with sockets on either so_incomp or so_comp, these
3253 * sockets are dropped.
3255 * If higher-level protocols are implemented in the kernel, the wakeups done
3256 * here will sometimes cause software-interrupt process scheduling.
3259 soisconnecting(struct socket *so)
3263 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
3264 so->so_state |= SS_ISCONNECTING;
3269 soisconnected(struct socket *so)
3271 struct socket *head;
3277 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
3278 so->so_state |= SS_ISCONNECTED;
3280 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
3281 if ((so->so_options & SO_ACCEPTFILTER) == 0) {
3283 TAILQ_REMOVE(&head->so_incomp, so, so_list);
3285 so->so_qstate &= ~SQ_INCOMP;
3286 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
3288 so->so_qstate |= SQ_COMP;
3291 wakeup_one(&head->so_timeo);
3294 soupcall_set(so, SO_RCV,
3295 head->so_accf->so_accept_filter->accf_callback,
3296 head->so_accf->so_accept_filter_arg);
3297 so->so_options &= ~SO_ACCEPTFILTER;
3298 ret = head->so_accf->so_accept_filter->accf_callback(so,
3299 head->so_accf->so_accept_filter_arg, M_NOWAIT);
3300 if (ret == SU_ISCONNECTED)
3301 soupcall_clear(so, SO_RCV);
3303 if (ret == SU_ISCONNECTED)
3310 wakeup(&so->so_timeo);
3316 soisdisconnecting(struct socket *so)
3320 * Note: This code assumes that SOCK_LOCK(so) and
3321 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3323 SOCKBUF_LOCK(&so->so_rcv);
3324 so->so_state &= ~SS_ISCONNECTING;
3325 so->so_state |= SS_ISDISCONNECTING;
3326 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3327 sorwakeup_locked(so);
3328 SOCKBUF_LOCK(&so->so_snd);
3329 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3330 sowwakeup_locked(so);
3331 wakeup(&so->so_timeo);
3335 soisdisconnected(struct socket *so)
3339 * Note: This code assumes that SOCK_LOCK(so) and
3340 * SOCKBUF_LOCK(&so->so_rcv) are the same.
3342 SOCKBUF_LOCK(&so->so_rcv);
3343 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
3344 so->so_state |= SS_ISDISCONNECTED;
3345 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
3346 sorwakeup_locked(so);
3347 SOCKBUF_LOCK(&so->so_snd);
3348 so->so_snd.sb_state |= SBS_CANTSENDMORE;
3349 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
3350 sowwakeup_locked(so);
3351 wakeup(&so->so_timeo);
3355 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
3358 sodupsockaddr(const struct sockaddr *sa, int mflags)
3360 struct sockaddr *sa2;
3362 sa2 = malloc(sa->sa_len, M_SONAME, mflags);
3364 bcopy(sa, sa2, sa->sa_len);
3369 * Register per-socket buffer upcalls.
3372 soupcall_set(struct socket *so, int which,
3373 int (*func)(struct socket *, void *, int), void *arg)
3385 panic("soupcall_set: bad which");
3387 SOCKBUF_LOCK_ASSERT(sb);
3389 /* XXX: accf_http actually wants to do this on purpose. */
3390 KASSERT(sb->sb_upcall == NULL, ("soupcall_set: overwriting upcall"));
3392 sb->sb_upcall = func;
3393 sb->sb_upcallarg = arg;
3394 sb->sb_flags |= SB_UPCALL;
3398 soupcall_clear(struct socket *so, int which)
3410 panic("soupcall_clear: bad which");
3412 SOCKBUF_LOCK_ASSERT(sb);
3413 KASSERT(sb->sb_upcall != NULL, ("soupcall_clear: no upcall to clear"));
3414 sb->sb_upcall = NULL;
3415 sb->sb_upcallarg = NULL;
3416 sb->sb_flags &= ~SB_UPCALL;
3420 * Create an external-format (``xsocket'') structure using the information in
3421 * the kernel-format socket structure pointed to by so. This is done to
3422 * reduce the spew of irrelevant information over this interface, to isolate
3423 * user code from changes in the kernel structure, and potentially to provide
3424 * information-hiding if we decide that some of this information should be
3425 * hidden from users.
3428 sotoxsocket(struct socket *so, struct xsocket *xso)
3431 xso->xso_len = sizeof *xso;
3433 xso->so_type = so->so_type;
3434 xso->so_options = so->so_options;
3435 xso->so_linger = so->so_linger;
3436 xso->so_state = so->so_state;
3437 xso->so_pcb = so->so_pcb;
3438 xso->xso_protocol = so->so_proto->pr_protocol;
3439 xso->xso_family = so->so_proto->pr_domain->dom_family;
3440 xso->so_qlen = so->so_qlen;
3441 xso->so_incqlen = so->so_incqlen;
3442 xso->so_qlimit = so->so_qlimit;
3443 xso->so_timeo = so->so_timeo;
3444 xso->so_error = so->so_error;
3445 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
3446 xso->so_oobmark = so->so_oobmark;
3447 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
3448 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
3449 xso->so_uid = so->so_cred->cr_uid;
3454 * Socket accessor functions to provide external consumers with
3455 * a safe interface to socket state
3460 so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *),
3464 TAILQ_FOREACH(so, &so->so_comp, so_list)
3469 so_sockbuf_rcv(struct socket *so)
3472 return (&so->so_rcv);
3476 so_sockbuf_snd(struct socket *so)
3479 return (&so->so_snd);
3483 so_state_get(const struct socket *so)
3486 return (so->so_state);
3490 so_state_set(struct socket *so, int val)
3497 so_options_get(const struct socket *so)
3500 return (so->so_options);
3504 so_options_set(struct socket *so, int val)
3507 so->so_options = val;
3511 so_error_get(const struct socket *so)
3514 return (so->so_error);
3518 so_error_set(struct socket *so, int val)
3525 so_linger_get(const struct socket *so)
3528 return (so->so_linger);
3532 so_linger_set(struct socket *so, int val)
3535 so->so_linger = val;
3539 so_protosw_get(const struct socket *so)
3542 return (so->so_proto);
3546 so_protosw_set(struct socket *so, struct protosw *val)
3553 so_sorwakeup(struct socket *so)
3560 so_sowwakeup(struct socket *so)
3567 so_sorwakeup_locked(struct socket *so)
3570 sorwakeup_locked(so);
3574 so_sowwakeup_locked(struct socket *so)
3577 sowwakeup_locked(so);
3581 so_lock(struct socket *so)
3588 so_unlock(struct socket *so)