2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
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 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
67 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.24 2003/11/11 17:18:18 silby Exp $
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/fcntl.h>
76 #include <sys/malloc.h>
78 #include <sys/domain.h>
79 #include <sys/file.h> /* for struct knote */
80 #include <sys/kernel.h>
81 #include <sys/event.h>
83 #include <sys/protosw.h>
84 #include <sys/socket.h>
85 #include <sys/socketvar.h>
86 #include <sys/socketops.h>
87 #include <sys/resourcevar.h>
88 #include <sys/signalvar.h>
89 #include <sys/sysctl.h>
92 #include <vm/vm_zone.h>
95 #include <sys/thread2.h>
96 #include <sys/socketvar2.h>
98 #include <machine/limits.h>
100 extern int tcp_sosnd_agglim;
103 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
106 static void filt_sordetach(struct knote *kn);
107 static int filt_soread(struct knote *kn, long hint);
108 static void filt_sowdetach(struct knote *kn);
109 static int filt_sowrite(struct knote *kn, long hint);
110 static int filt_solisten(struct knote *kn, long hint);
112 static struct filterops solisten_filtops =
113 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sordetach, filt_solisten };
114 static struct filterops soread_filtops =
115 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sordetach, filt_soread };
116 static struct filterops sowrite_filtops =
117 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sowdetach, filt_sowrite };
118 static struct filterops soexcept_filtops =
119 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sordetach, filt_soread };
121 MALLOC_DEFINE(M_SOCKET, "socket", "socket struct");
122 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
123 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
126 static int somaxconn = SOMAXCONN;
127 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
128 &somaxconn, 0, "Maximum pending socket connection queue size");
131 * Socket operation routines.
132 * These routines are called by the routines in
133 * sys_socket.c or from a system process, and
134 * implement the semantics of socket operations by
135 * switching out to the protocol specific routines.
139 * Get a socket structure, and initialize it.
140 * Note that it would probably be better to allocate socket
141 * and PCB at the same time, but I'm not convinced that all
142 * the protocols can be easily modified to do this.
150 waitmask = waitok ? M_WAITOK : M_NOWAIT;
151 so = kmalloc(sizeof(struct socket), M_SOCKET, M_ZERO|waitmask);
153 /* XXX race condition for reentrant kernel */
154 TAILQ_INIT(&so->so_aiojobq);
155 TAILQ_INIT(&so->so_rcv.ssb_kq.ki_mlist);
156 TAILQ_INIT(&so->so_snd.ssb_kq.ki_mlist);
157 lwkt_token_init(&so->so_rcv.ssb_token, "rcvtok");
158 lwkt_token_init(&so->so_snd.ssb_token, "sndtok");
159 so->so_state = SS_NOFDREF;
166 socreate(int dom, struct socket **aso, int type,
167 int proto, struct thread *td)
169 struct proc *p = td->td_proc;
172 struct pru_attach_info ai;
176 prp = pffindproto(dom, proto, type);
178 prp = pffindtype(dom, type);
180 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
181 return (EPROTONOSUPPORT);
183 if (p->p_ucred->cr_prison && jail_socket_unixiproute_only &&
184 prp->pr_domain->dom_family != PF_LOCAL &&
185 prp->pr_domain->dom_family != PF_INET &&
186 prp->pr_domain->dom_family != PF_INET6 &&
187 prp->pr_domain->dom_family != PF_ROUTE) {
188 return (EPROTONOSUPPORT);
191 if (prp->pr_type != type)
193 so = soalloc(p != 0);
198 * Callers of socreate() presumably will connect up a descriptor
199 * and call soclose() if they cannot. This represents our so_refs
200 * (which should be 1) from soalloc().
202 soclrstate(so, SS_NOFDREF);
205 * Set a default port for protocol processing. No action will occur
206 * on the socket on this port until an inpcb is attached to it and
207 * is able to match incoming packets, or until the socket becomes
208 * available to userland.
210 * We normally default the socket to the protocol thread on cpu 0.
211 * If PR_SYNC_PORT is set (unix domain sockets) there is no protocol
212 * thread and all pr_*()/pru_*() calls are executed synchronously.
214 if (prp->pr_flags & PR_SYNC_PORT)
215 so->so_port = &netisr_sync_port;
217 so->so_port = cpu_portfn(0);
219 TAILQ_INIT(&so->so_incomp);
220 TAILQ_INIT(&so->so_comp);
222 so->so_cred = crhold(p->p_ucred);
224 ai.sb_rlimit = &p->p_rlimit[RLIMIT_SBSIZE];
225 ai.p_ucred = p->p_ucred;
226 ai.fd_rdir = p->p_fd->fd_rdir;
229 * Auto-sizing of socket buffers is managed by the protocols and
230 * the appropriate flags must be set in the pru_attach function.
232 error = so_pru_attach(so, proto, &ai);
234 sosetstate(so, SS_NOFDREF);
235 sofree(so); /* from soalloc */
240 * NOTE: Returns referenced socket.
247 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
251 error = so_pru_bind(so, nam, td);
256 sodealloc(struct socket *so)
258 if (so->so_rcv.ssb_hiwat)
259 (void)chgsbsize(so->so_cred->cr_uidinfo,
260 &so->so_rcv.ssb_hiwat, 0, RLIM_INFINITY);
261 if (so->so_snd.ssb_hiwat)
262 (void)chgsbsize(so->so_cred->cr_uidinfo,
263 &so->so_snd.ssb_hiwat, 0, RLIM_INFINITY);
265 /* remove accept filter if present */
266 if (so->so_accf != NULL)
267 do_setopt_accept_filter(so, NULL);
274 solisten(struct socket *so, int backlog, struct thread *td)
278 short oldopt, oldqlimit;
281 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING))
285 oldopt = so->so_options;
286 oldqlimit = so->so_qlimit;
289 lwkt_gettoken(&so->so_rcv.ssb_token);
290 if (TAILQ_EMPTY(&so->so_comp))
291 so->so_options |= SO_ACCEPTCONN;
292 lwkt_reltoken(&so->so_rcv.ssb_token);
293 if (backlog < 0 || backlog > somaxconn)
295 so->so_qlimit = backlog;
296 /* SCTP needs to look at tweak both the inbound backlog parameter AND
297 * the so_options (UDP model both connect's and gets inbound
298 * connections .. implicitly).
300 error = so_pru_listen(so, td);
303 /* Restore the params */
304 so->so_options = oldopt;
305 so->so_qlimit = oldqlimit;
313 * Destroy a disconnected socket. This routine is a NOP if entities
314 * still have a reference on the socket:
316 * so_pcb - The protocol stack still has a reference
317 * SS_NOFDREF - There is no longer a file pointer reference
320 sofree(struct socket *so)
325 * This is a bit hackish at the moment. We need to interlock
326 * any accept queue we are on before we potentially lose the
327 * last reference to avoid races against a re-reference from
328 * someone operating on the queue.
330 while ((head = so->so_head) != NULL) {
331 lwkt_getpooltoken(head);
332 if (so->so_head == head)
334 lwkt_relpooltoken(head);
338 * Arbitrage the last free.
340 KKASSERT(so->so_refs > 0);
341 if (atomic_fetchadd_int(&so->so_refs, -1) != 1) {
343 lwkt_relpooltoken(head);
347 KKASSERT(so->so_pcb == NULL && (so->so_state & SS_NOFDREF));
348 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
351 * We're done, remove ourselves from the accept queue we are
352 * on, if we are on one.
355 if (so->so_state & SS_INCOMP) {
356 TAILQ_REMOVE(&head->so_incomp, so, so_list);
358 } else if (so->so_state & SS_COMP) {
360 * We must not decommission a socket that's
361 * on the accept(2) queue. If we do, then
362 * accept(2) may hang after select(2) indicated
363 * that the listening socket was ready.
365 lwkt_relpooltoken(head);
368 panic("sofree: not queued");
370 soclrstate(so, SS_INCOMP);
372 lwkt_relpooltoken(head);
374 ssb_release(&so->so_snd, so);
380 * Close a socket on last file table reference removal.
381 * Initiate disconnect if connected.
382 * Free socket when disconnect complete.
385 soclose(struct socket *so, int fflag)
389 funsetown(&so->so_sigio);
390 if (so->so_pcb == NULL)
392 if (so->so_state & SS_ISCONNECTED) {
393 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
394 error = sodisconnect(so);
398 if (so->so_options & SO_LINGER) {
399 if ((so->so_state & SS_ISDISCONNECTING) &&
402 while (so->so_state & SS_ISCONNECTED) {
403 error = tsleep(&so->so_timeo, PCATCH,
404 "soclos", so->so_linger * hz);
414 error2 = so_pru_detach(so);
419 lwkt_getpooltoken(so);
420 if (so->so_options & SO_ACCEPTCONN) {
423 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
424 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
425 soclrstate(sp, SS_INCOMP);
430 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
431 TAILQ_REMOVE(&so->so_comp, sp, so_list);
432 soclrstate(sp, SS_COMP);
438 lwkt_relpooltoken(so);
439 if (so->so_state & SS_NOFDREF)
440 panic("soclose: NOFDREF");
441 sosetstate(so, SS_NOFDREF); /* take ref */
442 sofree(so); /* dispose of ref */
447 * Abort and destroy a socket. Only one abort can be in progress
448 * at any given moment.
451 soabort(struct socket *so)
458 soaborta(struct socket *so)
465 soabort_oncpu(struct socket *so)
468 so_pru_abort_oncpu(so);
472 * so is passed in ref'd, which becomes owned by
473 * the cleared SS_NOFDREF flag.
476 soaccept(struct socket *so, struct sockaddr **nam)
480 if ((so->so_state & SS_NOFDREF) == 0)
481 panic("soaccept: !NOFDREF");
482 soclrstate(so, SS_NOFDREF); /* owned by lack of SS_NOFDREF */
483 error = so_pru_accept_direct(so, nam);
488 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
492 if (so->so_options & SO_ACCEPTCONN)
495 * If protocol is connection-based, can only connect once.
496 * Otherwise, if connected, try to disconnect first.
497 * This allows user to disconnect by connecting to, e.g.,
500 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
501 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
502 (error = sodisconnect(so)))) {
506 * Prevent accumulated error from previous connection
510 error = so_pru_connect(so, nam, td);
516 soconnect2(struct socket *so1, struct socket *so2)
520 error = so_pru_connect2(so1, so2);
525 sodisconnect(struct socket *so)
529 if ((so->so_state & SS_ISCONNECTED) == 0) {
533 if (so->so_state & SS_ISDISCONNECTING) {
537 error = so_pru_disconnect(so);
542 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
545 * If send must go all at once and message is larger than
546 * send buffering, then hard error.
547 * Lock against other senders.
548 * If must go all at once and not enough room now, then
549 * inform user that this would block and do nothing.
550 * Otherwise, if nonblocking, send as much as possible.
551 * The data to be sent is described by "uio" if nonzero,
552 * otherwise by the mbuf chain "top" (which must be null
553 * if uio is not). Data provided in mbuf chain must be small
554 * enough to send all at once.
556 * Returns nonzero on error, timeout or signal; callers
557 * must check for short counts if EINTR/ERESTART are returned.
558 * Data and control buffers are freed on return.
561 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
562 struct mbuf *top, struct mbuf *control, int flags,
569 int clen = 0, error, dontroute, mlen;
570 int atomic = sosendallatonce(so) || top;
574 resid = uio->uio_resid;
576 resid = (size_t)top->m_pkthdr.len;
579 for (m = top; m; m = m->m_next)
581 KKASSERT(top->m_pkthdr.len == len);
586 * WARNING! resid is unsigned, space and len are signed. space
587 * can wind up negative if the sockbuf is overcommitted.
589 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
590 * type sockets since that's an error.
592 if (so->so_type == SOCK_STREAM && (flags & MSG_EOR)) {
598 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
599 (so->so_proto->pr_flags & PR_ATOMIC);
600 if (td->td_lwp != NULL)
601 td->td_lwp->lwp_ru.ru_msgsnd++;
603 clen = control->m_len;
604 #define gotoerr(errcode) { error = errcode; goto release; }
607 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
612 if (so->so_state & SS_CANTSENDMORE)
615 error = so->so_error;
619 if ((so->so_state & SS_ISCONNECTED) == 0) {
621 * `sendto' and `sendmsg' is allowed on a connection-
622 * based socket if it supports implied connect.
623 * Return ENOTCONN if not connected and no address is
626 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
627 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
628 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
629 !(resid == 0 && clen != 0))
631 } else if (addr == 0)
632 gotoerr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
633 ENOTCONN : EDESTADDRREQ);
635 if ((atomic && resid > so->so_snd.ssb_hiwat) ||
636 clen > so->so_snd.ssb_hiwat) {
639 space = ssb_space(&so->so_snd);
642 if ((space < 0 || (size_t)space < resid + clen) && uio &&
643 (atomic || space < so->so_snd.ssb_lowat || space < clen)) {
644 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
645 gotoerr(EWOULDBLOCK);
646 ssb_unlock(&so->so_snd);
647 error = ssb_wait(&so->so_snd);
657 * Data is prepackaged in "top".
661 top->m_flags |= M_EOR;
665 m = m_getl((int)resid, MB_WAIT, MT_DATA,
666 top == NULL ? M_PKTHDR : 0, &mlen);
669 m->m_pkthdr.rcvif = NULL;
671 len = imin((int)szmin(mlen, resid), space);
672 if (resid < MINCLSIZE) {
674 * For datagram protocols, leave room
675 * for protocol headers in first mbuf.
677 if (atomic && top == 0 && len < mlen)
681 error = uiomove(mtod(m, caddr_t), (size_t)len, uio);
682 resid = uio->uio_resid;
685 top->m_pkthdr.len += len;
691 top->m_flags |= M_EOR;
694 } while (space > 0 && atomic);
696 so->so_options |= SO_DONTROUTE;
697 if (flags & MSG_OOB) {
698 pru_flags = PRUS_OOB;
699 } else if ((flags & MSG_EOF) &&
700 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
703 * If the user set MSG_EOF, the protocol
704 * understands this flag and nothing left to
705 * send then use PRU_SEND_EOF instead of PRU_SEND.
707 pru_flags = PRUS_EOF;
708 } else if (resid > 0 && space > 0) {
709 /* If there is more to send, set PRUS_MORETOCOME */
710 pru_flags = PRUS_MORETOCOME;
715 * XXX all the SS_CANTSENDMORE checks previously
716 * done could be out of date. We could have recieved
717 * a reset packet in an interrupt or maybe we slept
718 * while doing page faults in uiomove() etc. We could
719 * probably recheck again inside the splnet() protection
720 * here, but there are probably other places that this
721 * also happens. We must rethink this.
723 error = so_pru_send(so, pru_flags, top, addr, control, td);
725 so->so_options &= ~SO_DONTROUTE;
732 } while (resid && space > 0);
736 ssb_unlock(&so->so_snd);
746 * A specialization of sosend() for UDP based on protocol-specific knowledge:
747 * so->so_proto->pr_flags has the PR_ATOMIC field set. This means that
748 * sosendallatonce() returns true,
749 * the "atomic" variable is true,
750 * and sosendudp() blocks until space is available for the entire send.
751 * so->so_proto->pr_flags does not have the PR_CONNREQUIRED or
752 * PR_IMPLOPCL flags set.
753 * UDP has no out-of-band data.
754 * UDP has no control data.
755 * UDP does not support MSG_EOR.
758 sosendudp(struct socket *so, struct sockaddr *addr, struct uio *uio,
759 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
761 boolean_t dontroute; /* temporary SO_DONTROUTE setting */
766 if (td->td_lwp != NULL)
767 td->td_lwp->lwp_ru.ru_msgsnd++;
771 KASSERT((uio && !top) || (top && !uio), ("bad arguments to sosendudp"));
772 resid = uio ? uio->uio_resid : (size_t)top->m_pkthdr.len;
775 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
779 if (so->so_state & SS_CANTSENDMORE)
782 error = so->so_error;
786 if (!(so->so_state & SS_ISCONNECTED) && addr == NULL)
787 gotoerr(EDESTADDRREQ);
788 if (resid > so->so_snd.ssb_hiwat)
790 space = ssb_space(&so->so_snd);
791 if (uio && (space < 0 || (size_t)space < resid)) {
792 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
793 gotoerr(EWOULDBLOCK);
794 ssb_unlock(&so->so_snd);
795 error = ssb_wait(&so->so_snd);
802 top = m_uiomove(uio);
807 dontroute = (flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE);
809 so->so_options |= SO_DONTROUTE;
811 error = so_pru_send(so, 0, top, addr, NULL, td);
812 top = NULL; /* sent or freed in lower layer */
815 so->so_options &= ~SO_DONTROUTE;
818 ssb_unlock(&so->so_snd);
826 sosendtcp(struct socket *so, struct sockaddr *addr, struct uio *uio,
827 struct mbuf *top, struct mbuf *control, int flags,
839 KKASSERT(top == NULL);
841 resid = uio->uio_resid;
844 resid = (size_t)top->m_pkthdr.len;
847 for (m = top; m; m = m->m_next)
849 KKASSERT(top->m_pkthdr.len == len);
854 * WARNING! resid is unsigned, space and len are signed. space
855 * can wind up negative if the sockbuf is overcommitted.
857 * Also check to make sure that MSG_EOR isn't used on TCP
859 if (flags & MSG_EOR) {
865 /* TCP doesn't do control messages (rights, creds, etc) */
866 if (control->m_len) {
870 m_freem(control); /* empty control, just free it */
874 if (td->td_lwp != NULL)
875 td->td_lwp->lwp_ru.ru_msgsnd++;
877 #define gotoerr(errcode) { error = errcode; goto release; }
880 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
885 if (so->so_state & SS_CANTSENDMORE)
888 error = so->so_error;
892 if ((so->so_state & SS_ISCONNECTED) == 0 &&
893 (so->so_state & SS_ISCONFIRMING) == 0)
895 if (allatonce && resid > so->so_snd.ssb_hiwat)
898 space = ssb_space(&so->so_snd);
901 if ((space < 0 || (size_t)space < resid) && !allatonce &&
902 space < so->so_snd.ssb_lowat) {
903 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
904 gotoerr(EWOULDBLOCK);
905 ssb_unlock(&so->so_snd);
906 error = ssb_wait(&so->so_snd);
917 * Data is prepackaged in "top".
923 m = m_getl((int)resid, MB_WAIT, MT_DATA,
924 top == NULL ? M_PKTHDR : 0, &mlen);
927 m->m_pkthdr.rcvif = NULL;
929 len = imin((int)szmin(mlen, resid), space);
931 error = uiomove(mtod(m, caddr_t), (size_t)len, uio);
932 resid = uio->uio_resid;
935 top->m_pkthdr.len += len;
942 } while (space > 0 && cnt < tcp_sosnd_agglim);
944 if (flags & MSG_OOB) {
945 pru_flags = PRUS_OOB;
946 } else if (resid > 0 && space > 0) {
947 /* If there is more to send, set PRUS_MORETOCOME */
948 pru_flags = PRUS_MORETOCOME;
954 * XXX all the SS_CANTSENDMORE checks previously
955 * done could be out of date. We could have recieved
956 * a reset packet in an interrupt or maybe we slept
957 * while doing page faults in uiomove() etc. We could
958 * probably recheck again inside the splnet() protection
959 * here, but there are probably other places that this
960 * also happens. We must rethink this.
962 if ((pru_flags & PRUS_OOB) ||
963 (pru_flags & PRUS_MORETOCOME) == 0) {
964 error = so_pru_send(so, pru_flags, top,
967 so_pru_send_async(so, pru_flags, top,
976 } while (resid && space > 0);
980 ssb_unlock(&so->so_snd);
990 * Implement receive operations on a socket.
992 * We depend on the way that records are added to the signalsockbuf
993 * by sbappend*. In particular, each record (mbufs linked through m_next)
994 * must begin with an address if the protocol so specifies,
995 * followed by an optional mbuf or mbufs containing ancillary data,
996 * and then zero or more mbufs of data.
998 * Although the signalsockbuf is locked, new data may still be appended.
999 * A token inside the ssb_lock deals with MP issues and still allows
1000 * the network to access the socket if we block in a uio.
1002 * The caller may receive the data as a single mbuf chain by supplying
1003 * an mbuf **mp0 for use in returning the chain. The uio is then used
1004 * only for the count in uio_resid.
1007 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
1008 struct sockbuf *sio, struct mbuf **controlp, int *flagsp)
1011 struct mbuf *free_chain = NULL;
1012 int flags, len, error, offset;
1013 struct protosw *pr = so->so_proto;
1015 size_t resid, orig_resid;
1018 resid = uio->uio_resid;
1020 resid = (size_t)(sio->sb_climit - sio->sb_cc);
1028 flags = *flagsp &~ MSG_EOR;
1031 if (flags & MSG_OOB) {
1032 m = m_get(MB_WAIT, MT_DATA);
1035 error = so_pru_rcvoob(so, m, flags & MSG_PEEK);
1041 KKASSERT(resid >= (size_t)m->m_len);
1042 resid -= (size_t)m->m_len;
1043 } while (resid > 0 && m);
1046 uio->uio_resid = resid;
1047 error = uiomove(mtod(m, caddr_t),
1048 (int)szmin(resid, m->m_len),
1050 resid = uio->uio_resid;
1052 } while (uio->uio_resid && error == 0 && m);
1059 if ((so->so_state & SS_ISCONFIRMING) && resid)
1063 * The token interlocks against the protocol thread while
1064 * ssb_lock is a blocking lock against other userland entities.
1066 lwkt_gettoken(&so->so_rcv.ssb_token);
1068 error = ssb_lock(&so->so_rcv, SBLOCKWAIT(flags));
1072 m = so->so_rcv.ssb_mb;
1074 * If we have less data than requested, block awaiting more
1075 * (subject to any timeout) if:
1076 * 1. the current count is less than the low water mark, or
1077 * 2. MSG_WAITALL is set, and it is possible to do the entire
1078 * receive operation at once if we block (resid <= hiwat).
1079 * 3. MSG_DONTWAIT is not set
1080 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1081 * we have to do the receive in sections, and thus risk returning
1082 * a short count if a timeout or signal occurs after we start.
1084 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1085 (size_t)so->so_rcv.ssb_cc < resid) &&
1086 (so->so_rcv.ssb_cc < so->so_rcv.ssb_lowat ||
1087 ((flags & MSG_WAITALL) && resid <= (size_t)so->so_rcv.ssb_hiwat)) &&
1088 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
1089 KASSERT(m != NULL || !so->so_rcv.ssb_cc, ("receive 1"));
1093 error = so->so_error;
1094 if ((flags & MSG_PEEK) == 0)
1098 if (so->so_state & SS_CANTRCVMORE) {
1104 for (; m; m = m->m_next) {
1105 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1106 m = so->so_rcv.ssb_mb;
1110 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1111 (pr->pr_flags & PR_CONNREQUIRED)) {
1117 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
1118 error = EWOULDBLOCK;
1121 ssb_unlock(&so->so_rcv);
1122 error = ssb_wait(&so->so_rcv);
1128 if (uio && uio->uio_td && uio->uio_td->td_proc)
1129 uio->uio_td->td_lwp->lwp_ru.ru_msgrcv++;
1132 * note: m should be == sb_mb here. Cache the next record while
1133 * cleaning up. Note that calling m_free*() will break out critical
1136 KKASSERT(m == so->so_rcv.ssb_mb);
1139 * Skip any address mbufs prepending the record.
1141 if (pr->pr_flags & PR_ADDR) {
1142 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
1145 *psa = dup_sockaddr(mtod(m, struct sockaddr *));
1146 if (flags & MSG_PEEK)
1149 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1153 * Skip any control mbufs prepending the record.
1156 if (pr->pr_flags & PR_ADDR_OPT) {
1158 * For SCTP we may be getting a
1159 * whole message OR a partial delivery.
1161 if (m && m->m_type == MT_SONAME) {
1164 *psa = dup_sockaddr(mtod(m, struct sockaddr *));
1165 if (flags & MSG_PEEK)
1168 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1172 while (m && m->m_type == MT_CONTROL && error == 0) {
1173 if (flags & MSG_PEEK) {
1175 *controlp = m_copy(m, 0, m->m_len);
1176 m = m->m_next; /* XXX race */
1179 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
1180 if (pr->pr_domain->dom_externalize &&
1181 mtod(m, struct cmsghdr *)->cmsg_type ==
1183 error = (*pr->pr_domain->dom_externalize)(m);
1187 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1190 if (controlp && *controlp) {
1192 controlp = &(*controlp)->m_next;
1201 if (type == MT_OOBDATA)
1206 * Copy to the UIO or mbuf return chain (*mp).
1210 while (m && resid > 0 && error == 0) {
1211 if (m->m_type == MT_OOBDATA) {
1212 if (type != MT_OOBDATA)
1214 } else if (type == MT_OOBDATA)
1217 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
1219 soclrstate(so, SS_RCVATMARK);
1220 len = (resid > INT_MAX) ? INT_MAX : resid;
1221 if (so->so_oobmark && len > so->so_oobmark - offset)
1222 len = so->so_oobmark - offset;
1223 if (len > m->m_len - moff)
1224 len = m->m_len - moff;
1227 * Copy out to the UIO or pass the mbufs back to the SIO.
1228 * The SIO is dealt with when we eat the mbuf, but deal
1229 * with the resid here either way.
1232 uio->uio_resid = resid;
1233 error = uiomove(mtod(m, caddr_t) + moff, len, uio);
1234 resid = uio->uio_resid;
1238 resid -= (size_t)len;
1242 * Eat the entire mbuf or just a piece of it
1244 if (len == m->m_len - moff) {
1245 if (m->m_flags & M_EOR)
1248 if (m->m_flags & M_NOTIFICATION)
1249 flags |= MSG_NOTIFICATION;
1251 if (flags & MSG_PEEK) {
1256 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
1260 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1264 if (flags & MSG_PEEK) {
1268 n = m_copym(m, 0, len, MB_WAIT);
1274 so->so_rcv.ssb_cc -= len;
1277 if (so->so_oobmark) {
1278 if ((flags & MSG_PEEK) == 0) {
1279 so->so_oobmark -= len;
1280 if (so->so_oobmark == 0) {
1281 sosetstate(so, SS_RCVATMARK);
1286 if (offset == so->so_oobmark)
1290 if (flags & MSG_EOR)
1293 * If the MSG_WAITALL flag is set (for non-atomic socket),
1294 * we must not quit until resid == 0 or an error
1295 * termination. If a signal/timeout occurs, return
1296 * with a short count but without error.
1297 * Keep signalsockbuf locked against other readers.
1299 while ((flags & MSG_WAITALL) && m == NULL &&
1300 resid > 0 && !sosendallatonce(so) &&
1301 so->so_rcv.ssb_mb == NULL) {
1302 if (so->so_error || so->so_state & SS_CANTRCVMORE)
1305 * The window might have closed to zero, make
1306 * sure we send an ack now that we've drained
1307 * the buffer or we might end up blocking until
1308 * the idle takes over (5 seconds).
1310 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1311 so_pru_rcvd(so, flags);
1312 error = ssb_wait(&so->so_rcv);
1314 ssb_unlock(&so->so_rcv);
1318 m = so->so_rcv.ssb_mb;
1323 * If an atomic read was requested but unread data still remains
1324 * in the record, set MSG_TRUNC.
1326 if (m && pr->pr_flags & PR_ATOMIC)
1330 * Cleanup. If an atomic read was requested drop any unread data.
1332 if ((flags & MSG_PEEK) == 0) {
1333 if (m && (pr->pr_flags & PR_ATOMIC))
1334 sbdroprecord(&so->so_rcv.sb);
1335 if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
1336 so_pru_rcvd(so, flags);
1339 if (orig_resid == resid && orig_resid &&
1340 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
1341 ssb_unlock(&so->so_rcv);
1348 ssb_unlock(&so->so_rcv);
1350 lwkt_reltoken(&so->so_rcv.ssb_token);
1352 m_freem(free_chain);
1357 * Shut a socket down. Note that we do not get a frontend lock as we
1358 * want to be able to shut the socket down even if another thread is
1359 * blocked in a read(), thus waking it up.
1362 soshutdown(struct socket *so, int how)
1364 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1367 if (how != SHUT_WR) {
1368 /*ssb_lock(&so->so_rcv, M_WAITOK);*/
1370 /*ssb_unlock(&so->so_rcv);*/
1373 return (so_pru_shutdown(so));
1378 sorflush(struct socket *so)
1380 struct signalsockbuf *ssb = &so->so_rcv;
1381 struct protosw *pr = so->so_proto;
1382 struct signalsockbuf asb;
1384 atomic_set_int(&ssb->ssb_flags, SSB_NOINTR);
1386 lwkt_gettoken(&ssb->ssb_token);
1391 * Can't just blow up the ssb structure here
1393 bzero(&ssb->sb, sizeof(ssb->sb));
1398 atomic_clear_int(&ssb->ssb_flags, SSB_CLEAR_MASK);
1400 if ((pr->pr_flags & PR_RIGHTS) && pr->pr_domain->dom_dispose)
1401 (*pr->pr_domain->dom_dispose)(asb.ssb_mb);
1402 ssb_release(&asb, so);
1404 lwkt_reltoken(&ssb->ssb_token);
1409 do_setopt_accept_filter(struct socket *so, struct sockopt *sopt)
1411 struct accept_filter_arg *afap = NULL;
1412 struct accept_filter *afp;
1413 struct so_accf *af = so->so_accf;
1416 /* do not set/remove accept filters on non listen sockets */
1417 if ((so->so_options & SO_ACCEPTCONN) == 0) {
1422 /* removing the filter */
1425 if (af->so_accept_filter != NULL &&
1426 af->so_accept_filter->accf_destroy != NULL) {
1427 af->so_accept_filter->accf_destroy(so);
1429 if (af->so_accept_filter_str != NULL) {
1430 FREE(af->so_accept_filter_str, M_ACCF);
1435 so->so_options &= ~SO_ACCEPTFILTER;
1438 /* adding a filter */
1439 /* must remove previous filter first */
1444 /* don't put large objects on the kernel stack */
1445 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1446 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1447 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1448 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1451 afp = accept_filt_get(afap->af_name);
1456 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK | M_ZERO);
1457 if (afp->accf_create != NULL) {
1458 if (afap->af_name[0] != '\0') {
1459 int len = strlen(afap->af_name) + 1;
1461 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1462 strcpy(af->so_accept_filter_str, afap->af_name);
1464 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1465 if (af->so_accept_filter_arg == NULL) {
1466 FREE(af->so_accept_filter_str, M_ACCF);
1473 af->so_accept_filter = afp;
1475 so->so_options |= SO_ACCEPTFILTER;
1484 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1485 * an additional variant to handle the case where the option value needs
1486 * to be some kind of integer, but not a specific size.
1487 * In addition to their use here, these functions are also called by the
1488 * protocol-level pr_ctloutput() routines.
1491 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
1493 return soopt_to_kbuf(sopt, buf, len, minlen);
1497 soopt_to_kbuf(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
1501 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
1502 KKASSERT(kva_p(buf));
1505 * If the user gives us more than we wanted, we ignore it,
1506 * but if we don't get the minimum length the caller
1507 * wants, we return EINVAL. On success, sopt->sopt_valsize
1508 * is set to however much we actually retrieved.
1510 if ((valsize = sopt->sopt_valsize) < minlen)
1513 sopt->sopt_valsize = valsize = len;
1515 bcopy(sopt->sopt_val, buf, valsize);
1521 sosetopt(struct socket *so, struct sockopt *sopt)
1527 struct signalsockbuf *sotmp;
1530 sopt->sopt_dir = SOPT_SET;
1531 if (sopt->sopt_level != SOL_SOCKET) {
1532 if (so->so_proto && so->so_proto->pr_ctloutput) {
1533 return (so_pr_ctloutput(so, sopt));
1535 error = ENOPROTOOPT;
1537 switch (sopt->sopt_name) {
1539 case SO_ACCEPTFILTER:
1540 error = do_setopt_accept_filter(so, sopt);
1546 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1550 so->so_linger = l.l_linger;
1552 so->so_options |= SO_LINGER;
1554 so->so_options &= ~SO_LINGER;
1560 case SO_USELOOPBACK:
1566 error = sooptcopyin(sopt, &optval, sizeof optval,
1571 so->so_options |= sopt->sopt_name;
1573 so->so_options &= ~sopt->sopt_name;
1580 error = sooptcopyin(sopt, &optval, sizeof optval,
1586 * Values < 1 make no sense for any of these
1587 * options, so disallow them.
1594 switch (sopt->sopt_name) {
1597 if (ssb_reserve(sopt->sopt_name == SO_SNDBUF ?
1598 &so->so_snd : &so->so_rcv, (u_long)optval,
1600 &curproc->p_rlimit[RLIMIT_SBSIZE]) == 0) {
1604 sotmp = (sopt->sopt_name == SO_SNDBUF) ?
1605 &so->so_snd : &so->so_rcv;
1606 atomic_clear_int(&sotmp->ssb_flags,
1611 * Make sure the low-water is never greater than
1615 so->so_snd.ssb_lowat =
1616 (optval > so->so_snd.ssb_hiwat) ?
1617 so->so_snd.ssb_hiwat : optval;
1618 atomic_clear_int(&so->so_snd.ssb_flags,
1622 so->so_rcv.ssb_lowat =
1623 (optval > so->so_rcv.ssb_hiwat) ?
1624 so->so_rcv.ssb_hiwat : optval;
1625 atomic_clear_int(&so->so_rcv.ssb_flags,
1633 error = sooptcopyin(sopt, &tv, sizeof tv,
1638 /* assert(hz > 0); */
1639 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
1640 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1644 /* assert(tick > 0); */
1645 /* assert(ULONG_MAX - INT_MAX >= 1000000); */
1646 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / ustick;
1647 if (val > INT_MAX) {
1651 if (val == 0 && tv.tv_usec != 0)
1654 switch (sopt->sopt_name) {
1656 so->so_snd.ssb_timeo = val;
1659 so->so_rcv.ssb_timeo = val;
1664 error = ENOPROTOOPT;
1667 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1668 (void) so_pr_ctloutput(so, sopt);
1675 /* Helper routine for getsockopt */
1677 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
1679 soopt_from_kbuf(sopt, buf, len);
1684 soopt_from_kbuf(struct sockopt *sopt, const void *buf, size_t len)
1689 sopt->sopt_valsize = 0;
1693 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
1694 KKASSERT(kva_p(buf));
1697 * Documented get behavior is that we always return a value,
1698 * possibly truncated to fit in the user's buffer.
1699 * Traditional behavior is that we always tell the user
1700 * precisely how much we copied, rather than something useful
1701 * like the total amount we had available for her.
1702 * Note that this interface is not idempotent; the entire answer must
1703 * generated ahead of time.
1705 valsize = szmin(len, sopt->sopt_valsize);
1706 sopt->sopt_valsize = valsize;
1707 if (sopt->sopt_val != 0) {
1708 bcopy(buf, sopt->sopt_val, valsize);
1713 sogetopt(struct socket *so, struct sockopt *sopt)
1720 struct accept_filter_arg *afap;
1724 sopt->sopt_dir = SOPT_GET;
1725 if (sopt->sopt_level != SOL_SOCKET) {
1726 if (so->so_proto && so->so_proto->pr_ctloutput) {
1727 return (so_pr_ctloutput(so, sopt));
1729 return (ENOPROTOOPT);
1731 switch (sopt->sopt_name) {
1733 case SO_ACCEPTFILTER:
1734 if ((so->so_options & SO_ACCEPTCONN) == 0)
1736 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1737 M_TEMP, M_WAITOK | M_ZERO);
1738 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1739 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1740 if (so->so_accf->so_accept_filter_str != NULL)
1741 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1743 error = sooptcopyout(sopt, afap, sizeof(*afap));
1749 l.l_onoff = so->so_options & SO_LINGER;
1750 l.l_linger = so->so_linger;
1751 error = sooptcopyout(sopt, &l, sizeof l);
1754 case SO_USELOOPBACK:
1763 optval = so->so_options & sopt->sopt_name;
1765 error = sooptcopyout(sopt, &optval, sizeof optval);
1769 optval = so->so_type;
1773 optval = so->so_error;
1778 optval = so->so_snd.ssb_hiwat;
1782 optval = so->so_rcv.ssb_hiwat;
1786 optval = so->so_snd.ssb_lowat;
1790 optval = so->so_rcv.ssb_lowat;
1795 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1796 so->so_snd.ssb_timeo : so->so_rcv.ssb_timeo);
1798 tv.tv_sec = optval / hz;
1799 tv.tv_usec = (optval % hz) * ustick;
1800 error = sooptcopyout(sopt, &tv, sizeof tv);
1804 optval_l = ssb_space(&so->so_snd);
1805 error = sooptcopyout(sopt, &optval_l, sizeof(optval_l));
1809 error = ENOPROTOOPT;
1816 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1818 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1820 struct mbuf *m, *m_prev;
1821 int sopt_size = sopt->sopt_valsize, msize;
1823 m = m_getl(sopt_size, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_DATA,
1827 m->m_len = min(msize, sopt_size);
1828 sopt_size -= m->m_len;
1832 while (sopt_size > 0) {
1833 m = m_getl(sopt_size, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT,
1834 MT_DATA, 0, &msize);
1839 m->m_len = min(msize, sopt_size);
1840 sopt_size -= m->m_len;
1847 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1849 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1851 soopt_to_mbuf(sopt, m);
1856 soopt_to_mbuf(struct sockopt *sopt, struct mbuf *m)
1861 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
1863 if (sopt->sopt_val == NULL)
1865 val = sopt->sopt_val;
1866 valsize = sopt->sopt_valsize;
1867 while (m != NULL && valsize >= m->m_len) {
1868 bcopy(val, mtod(m, char *), m->m_len);
1869 valsize -= m->m_len;
1870 val = (caddr_t)val + m->m_len;
1873 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1874 panic("ip6_sooptmcopyin");
1877 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1879 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1881 return soopt_from_mbuf(sopt, m);
1885 soopt_from_mbuf(struct sockopt *sopt, struct mbuf *m)
1887 struct mbuf *m0 = m;
1892 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
1894 if (sopt->sopt_val == NULL)
1896 val = sopt->sopt_val;
1897 maxsize = sopt->sopt_valsize;
1898 while (m != NULL && maxsize >= m->m_len) {
1899 bcopy(mtod(m, char *), val, m->m_len);
1900 maxsize -= m->m_len;
1901 val = (caddr_t)val + m->m_len;
1902 valsize += m->m_len;
1906 /* enough soopt buffer should be given from user-land */
1910 sopt->sopt_valsize = valsize;
1915 sohasoutofband(struct socket *so)
1917 if (so->so_sigio != NULL)
1918 pgsigio(so->so_sigio, SIGURG, 0);
1919 KNOTE(&so->so_rcv.ssb_kq.ki_note, NOTE_OOB);
1923 sokqfilter(struct file *fp, struct knote *kn)
1925 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1926 struct signalsockbuf *ssb;
1928 switch (kn->kn_filter) {
1930 if (so->so_options & SO_ACCEPTCONN)
1931 kn->kn_fop = &solisten_filtops;
1933 kn->kn_fop = &soread_filtops;
1937 kn->kn_fop = &sowrite_filtops;
1941 kn->kn_fop = &soexcept_filtops;
1945 return (EOPNOTSUPP);
1948 knote_insert(&ssb->ssb_kq.ki_note, kn);
1949 atomic_set_int(&ssb->ssb_flags, SSB_KNOTE);
1954 filt_sordetach(struct knote *kn)
1956 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1958 knote_remove(&so->so_rcv.ssb_kq.ki_note, kn);
1959 if (SLIST_EMPTY(&so->so_rcv.ssb_kq.ki_note))
1960 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_KNOTE);
1965 filt_soread(struct knote *kn, long hint)
1967 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1969 if (kn->kn_sfflags & NOTE_OOB) {
1970 if ((so->so_oobmark || (so->so_state & SS_RCVATMARK))) {
1971 kn->kn_fflags |= NOTE_OOB;
1976 kn->kn_data = so->so_rcv.ssb_cc;
1978 if (so->so_state & SS_CANTRCVMORE) {
1980 * Only set NODATA if all data has been exhausted.
1982 if (kn->kn_data == 0)
1983 kn->kn_flags |= EV_NODATA;
1984 kn->kn_flags |= EV_EOF;
1985 kn->kn_fflags = so->so_error;
1988 if (so->so_error) /* temporary udp error */
1990 if (kn->kn_sfflags & NOTE_LOWAT)
1991 return (kn->kn_data >= kn->kn_sdata);
1992 return ((kn->kn_data >= so->so_rcv.ssb_lowat) ||
1993 !TAILQ_EMPTY(&so->so_comp));
1997 filt_sowdetach(struct knote *kn)
1999 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2001 knote_remove(&so->so_snd.ssb_kq.ki_note, kn);
2002 if (SLIST_EMPTY(&so->so_snd.ssb_kq.ki_note))
2003 atomic_clear_int(&so->so_snd.ssb_flags, SSB_KNOTE);
2008 filt_sowrite(struct knote *kn, long hint)
2010 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2012 kn->kn_data = ssb_space(&so->so_snd);
2013 if (so->so_state & SS_CANTSENDMORE) {
2014 kn->kn_flags |= (EV_EOF | EV_NODATA);
2015 kn->kn_fflags = so->so_error;
2018 if (so->so_error) /* temporary udp error */
2020 if (((so->so_state & SS_ISCONNECTED) == 0) &&
2021 (so->so_proto->pr_flags & PR_CONNREQUIRED))
2023 if (kn->kn_sfflags & NOTE_LOWAT)
2024 return (kn->kn_data >= kn->kn_sdata);
2025 return (kn->kn_data >= so->so_snd.ssb_lowat);
2030 filt_solisten(struct knote *kn, long hint)
2032 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2034 kn->kn_data = so->so_qlen;
2035 return (! TAILQ_EMPTY(&so->so_comp));