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 $
68 * $DragonFly: src/sys/kern/uipc_socket.c,v 1.54 2008/08/28 23:15:43 dillon Exp $
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/fcntl.h>
77 #include <sys/malloc.h>
79 #include <sys/domain.h>
80 #include <sys/file.h> /* for struct knote */
81 #include <sys/kernel.h>
82 #include <sys/malloc.h>
83 #include <sys/event.h>
86 #include <sys/protosw.h>
87 #include <sys/socket.h>
88 #include <sys/socketvar.h>
89 #include <sys/socketops.h>
90 #include <sys/resourcevar.h>
91 #include <sys/signalvar.h>
92 #include <sys/sysctl.h>
95 #include <vm/vm_zone.h>
98 #include <sys/thread2.h>
99 #include <sys/socketvar2.h>
101 #include <machine/limits.h>
104 static int do_setopt_accept_filter(struct socket
*so
, struct sockopt
*sopt
);
107 static void filt_sordetach(struct knote
*kn
);
108 static int filt_soread(struct knote
*kn
, long hint
);
109 static void filt_sowdetach(struct knote
*kn
);
110 static int filt_sowrite(struct knote
*kn
, long hint
);
111 static int filt_solisten(struct knote
*kn
, long hint
);
113 static struct filterops solisten_filtops
=
114 { 1, NULL
, filt_sordetach
, filt_solisten
};
115 static struct filterops soread_filtops
=
116 { 1, NULL
, filt_sordetach
, filt_soread
};
117 static struct filterops sowrite_filtops
=
118 { 1, NULL
, filt_sowdetach
, filt_sowrite
};
120 MALLOC_DEFINE(M_SOCKET
, "socket", "socket struct");
121 MALLOC_DEFINE(M_SONAME
, "soname", "socket name");
122 MALLOC_DEFINE(M_PCB
, "pcb", "protocol control block");
125 static int somaxconn
= SOMAXCONN
;
126 SYSCTL_INT(_kern_ipc
, KIPC_SOMAXCONN
, somaxconn
, CTLFLAG_RW
,
127 &somaxconn
, 0, "Maximum pending socket connection queue size");
130 * Socket operation routines.
131 * These routines are called by the routines in
132 * sys_socket.c or from a system process, and
133 * implement the semantics of socket operations by
134 * switching out to the protocol specific routines.
138 * Get a socket structure, and initialize it.
139 * Note that it would probably be better to allocate socket
140 * and PCB at the same time, but I'm not convinced that all
141 * the protocols can be easily modified to do this.
149 waitmask
= waitok
? M_WAITOK
: M_NOWAIT
;
150 so
= kmalloc(sizeof(struct socket
), M_SOCKET
, M_ZERO
|waitmask
);
152 /* XXX race condition for reentrant kernel */
153 TAILQ_INIT(&so
->so_aiojobq
);
154 TAILQ_INIT(&so
->so_rcv
.ssb_sel
.si_mlist
);
155 TAILQ_INIT(&so
->so_snd
.ssb_sel
.si_mlist
);
161 socreate(int dom
, struct socket
**aso
, int type
,
162 int proto
, struct thread
*td
)
164 struct proc
*p
= td
->td_proc
;
167 struct pru_attach_info ai
;
171 prp
= pffindproto(dom
, proto
, type
);
173 prp
= pffindtype(dom
, type
);
175 if (prp
== 0 || prp
->pr_usrreqs
->pru_attach
== 0)
176 return (EPROTONOSUPPORT
);
178 if (p
->p_ucred
->cr_prison
&& jail_socket_unixiproute_only
&&
179 prp
->pr_domain
->dom_family
!= PF_LOCAL
&&
180 prp
->pr_domain
->dom_family
!= PF_INET
&&
181 prp
->pr_domain
->dom_family
!= PF_INET6
&&
182 prp
->pr_domain
->dom_family
!= PF_ROUTE
) {
183 return (EPROTONOSUPPORT
);
186 if (prp
->pr_type
!= type
)
188 so
= soalloc(p
!= 0);
192 TAILQ_INIT(&so
->so_incomp
);
193 TAILQ_INIT(&so
->so_comp
);
195 so
->so_cred
= crhold(p
->p_ucred
);
197 ai
.sb_rlimit
= &p
->p_rlimit
[RLIMIT_SBSIZE
];
198 ai
.p_ucred
= p
->p_ucred
;
199 ai
.fd_rdir
= p
->p_fd
->fd_rdir
;
200 error
= so_pru_attach(so
, proto
, &ai
);
202 so
->so_state
|= SS_NOFDREF
;
211 sobind(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
216 error
= so_pru_bind(so
, nam
, td
);
222 sodealloc(struct socket
*so
)
224 if (so
->so_rcv
.ssb_hiwat
)
225 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
226 &so
->so_rcv
.ssb_hiwat
, 0, RLIM_INFINITY
);
227 if (so
->so_snd
.ssb_hiwat
)
228 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
229 &so
->so_snd
.ssb_hiwat
, 0, RLIM_INFINITY
);
231 /* remove accept filter if present */
232 if (so
->so_accf
!= NULL
)
233 do_setopt_accept_filter(so
, NULL
);
240 solisten(struct socket
*so
, int backlog
, struct thread
*td
)
244 short oldopt
, oldqlimit
;
248 if (so
->so_state
& (SS_ISCONNECTED
| SS_ISCONNECTING
)) {
254 oldopt
= so
->so_options
;
255 oldqlimit
= so
->so_qlimit
;
258 if (TAILQ_EMPTY(&so
->so_comp
))
259 so
->so_options
|= SO_ACCEPTCONN
;
260 if (backlog
< 0 || backlog
> somaxconn
)
262 so
->so_qlimit
= backlog
;
263 /* SCTP needs to look at tweak both the inbound backlog parameter AND
264 * the so_options (UDP model both connect's and gets inbound
265 * connections .. implicitly).
267 error
= so_pru_listen(so
, td
);
270 /* Restore the params */
271 so
->so_options
= oldopt
;
272 so
->so_qlimit
= oldqlimit
;
282 * Destroy a disconnected socket. This routine is a NOP if entities
283 * still have a reference on the socket:
285 * so_pcb - The protocol stack still has a reference
286 * SS_NOFDREF - There is no longer a file pointer reference
287 * SS_ABORTING - An abort netmsg is in-flight
290 sofree(struct socket
*so
)
292 struct socket
*head
= so
->so_head
;
294 if (so
->so_pcb
|| (so
->so_state
& SS_NOFDREF
) == 0)
296 if (so
->so_state
& SS_ABORTING
)
299 if (so
->so_state
& SS_INCOMP
) {
300 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
302 } else if (so
->so_state
& SS_COMP
) {
304 * We must not decommission a socket that's
305 * on the accept(2) queue. If we do, then
306 * accept(2) may hang after select(2) indicated
307 * that the listening socket was ready.
311 panic("sofree: not queued");
313 so
->so_state
&= ~SS_INCOMP
;
316 ssb_release(&so
->so_snd
, so
);
322 * Close a socket on last file table reference removal.
323 * Initiate disconnect if connected.
324 * Free socket when disconnect complete.
327 soclose(struct socket
*so
, int fflag
)
332 funsetown(so
->so_sigio
);
333 if (so
->so_pcb
== NULL
)
335 if (so
->so_state
& SS_ISCONNECTED
) {
336 if ((so
->so_state
& SS_ISDISCONNECTING
) == 0) {
337 error
= sodisconnect(so
);
341 if (so
->so_options
& SO_LINGER
) {
342 if ((so
->so_state
& SS_ISDISCONNECTING
) &&
345 while (so
->so_state
& SS_ISCONNECTED
) {
346 error
= tsleep((caddr_t
)&so
->so_timeo
,
347 PCATCH
, "soclos", so
->so_linger
* hz
);
357 error2
= so_pru_detach(so
);
362 if (so
->so_options
& SO_ACCEPTCONN
) {
365 while ((sp
= TAILQ_FIRST(&so
->so_incomp
)) != NULL
) {
366 TAILQ_REMOVE(&so
->so_incomp
, sp
, so_list
);
367 sp
->so_state
&= ~SS_INCOMP
;
370 if ((sp
->so_state
& SS_ABORTING
) == 0) {
371 sp
->so_state
|= SS_ABORTING
;
375 while ((sp
= TAILQ_FIRST(&so
->so_comp
)) != NULL
) {
376 TAILQ_REMOVE(&so
->so_comp
, sp
, so_list
);
377 sp
->so_state
&= ~SS_COMP
;
380 if ((sp
->so_state
& SS_ABORTING
) == 0) {
381 sp
->so_state
|= SS_ABORTING
;
386 if (so
->so_state
& SS_NOFDREF
)
387 panic("soclose: NOFDREF");
388 so
->so_state
|= SS_NOFDREF
;
395 * Abort and destroy a socket.
398 soabort(struct socket
*so
)
404 soaborta(struct socket
*so
)
410 soaccept(struct socket
*so
, struct sockaddr
**nam
)
415 if ((so
->so_state
& SS_NOFDREF
) == 0)
416 panic("soaccept: !NOFDREF");
417 so
->so_state
&= ~SS_NOFDREF
;
418 error
= so_pru_accept(so
, nam
);
424 soconnect(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
428 if (so
->so_options
& SO_ACCEPTCONN
)
432 * If protocol is connection-based, can only connect once.
433 * Otherwise, if connected, try to disconnect first.
434 * This allows user to disconnect by connecting to, e.g.,
437 if (so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
) &&
438 ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ||
439 (error
= sodisconnect(so
)))) {
443 * Prevent accumulated error from previous connection
447 error
= so_pru_connect(so
, nam
, td
);
454 soconnect2(struct socket
*so1
, struct socket
*so2
)
459 error
= so_pru_connect2(so1
, so2
);
465 sodisconnect(struct socket
*so
)
470 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
474 if (so
->so_state
& SS_ISDISCONNECTING
) {
478 error
= so_pru_disconnect(so
);
484 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
487 * If send must go all at once and message is larger than
488 * send buffering, then hard error.
489 * Lock against other senders.
490 * If must go all at once and not enough room now, then
491 * inform user that this would block and do nothing.
492 * Otherwise, if nonblocking, send as much as possible.
493 * The data to be sent is described by "uio" if nonzero,
494 * otherwise by the mbuf chain "top" (which must be null
495 * if uio is not). Data provided in mbuf chain must be small
496 * enough to send all at once.
498 * Returns nonzero on error, timeout or signal; callers
499 * must check for short counts if EINTR/ERESTART are returned.
500 * Data and control buffers are freed on return.
503 sosend(struct socket
*so
, struct sockaddr
*addr
, struct uio
*uio
,
504 struct mbuf
*top
, struct mbuf
*control
, int flags
,
509 long space
, len
, resid
;
510 int clen
= 0, error
, dontroute
, mlen
;
511 int atomic
= sosendallatonce(so
) || top
;
515 resid
= uio
->uio_resid
;
517 resid
= top
->m_pkthdr
.len
;
519 * In theory resid should be unsigned.
520 * However, space must be signed, as it might be less than 0
521 * if we over-committed, and we must use a signed comparison
522 * of space and resid. On the other hand, a negative resid
523 * causes us to loop sending 0-length segments to the protocol.
525 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
526 * type sockets since that's an error.
528 if (resid
< 0 || (so
->so_type
== SOCK_STREAM
&& (flags
& MSG_EOR
))) {
534 (flags
& MSG_DONTROUTE
) && (so
->so_options
& SO_DONTROUTE
) == 0 &&
535 (so
->so_proto
->pr_flags
& PR_ATOMIC
);
536 if (td
->td_lwp
!= NULL
)
537 td
->td_lwp
->lwp_ru
.ru_msgsnd
++;
539 clen
= control
->m_len
;
540 #define gotoerr(errcode) { error = errcode; crit_exit(); goto release; }
543 error
= ssb_lock(&so
->so_snd
, SBLOCKWAIT(flags
));
548 if (so
->so_state
& SS_CANTSENDMORE
)
551 error
= so
->so_error
;
556 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
558 * `sendto' and `sendmsg' is allowed on a connection-
559 * based socket if it supports implied connect.
560 * Return ENOTCONN if not connected and no address is
563 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) &&
564 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) == 0) {
565 if ((so
->so_state
& SS_ISCONFIRMING
) == 0 &&
566 !(resid
== 0 && clen
!= 0))
568 } else if (addr
== 0)
569 gotoerr(so
->so_proto
->pr_flags
& PR_CONNREQUIRED
?
570 ENOTCONN
: EDESTADDRREQ
);
572 if ((atomic
&& resid
> so
->so_snd
.ssb_hiwat
) ||
573 clen
> so
->so_snd
.ssb_hiwat
) {
576 space
= ssb_space(&so
->so_snd
);
579 if (space
< resid
+ clen
&& uio
&&
580 (atomic
|| space
< so
->so_snd
.ssb_lowat
|| space
< clen
)) {
581 if (flags
& (MSG_FNONBLOCKING
|MSG_DONTWAIT
))
582 gotoerr(EWOULDBLOCK
);
583 ssb_unlock(&so
->so_snd
);
584 error
= ssb_wait(&so
->so_snd
);
596 * Data is prepackaged in "top".
600 top
->m_flags
|= M_EOR
;
602 m
= m_getl(resid
, MB_WAIT
, MT_DATA
,
603 top
== NULL
? M_PKTHDR
: 0, &mlen
);
606 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
608 len
= min(min(mlen
, resid
), space
);
609 if (resid
< MINCLSIZE
) {
611 * For datagram protocols, leave room
612 * for protocol headers in first mbuf.
614 if (atomic
&& top
== 0 && len
< mlen
)
618 error
= uiomove(mtod(m
, caddr_t
), (int)len
, uio
);
619 resid
= uio
->uio_resid
;
622 top
->m_pkthdr
.len
+= len
;
628 top
->m_flags
|= M_EOR
;
631 } while (space
> 0 && atomic
);
633 so
->so_options
|= SO_DONTROUTE
;
634 if (flags
& MSG_OOB
) {
635 pru_flags
= PRUS_OOB
;
636 } else if ((flags
& MSG_EOF
) &&
637 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) &&
640 * If the user set MSG_EOF, the protocol
641 * understands this flag and nothing left to
642 * send then use PRU_SEND_EOF instead of PRU_SEND.
644 pru_flags
= PRUS_EOF
;
645 } else if (resid
> 0 && space
> 0) {
646 /* If there is more to send, set PRUS_MORETOCOME */
647 pru_flags
= PRUS_MORETOCOME
;
653 * XXX all the SS_CANTSENDMORE checks previously
654 * done could be out of date. We could have recieved
655 * a reset packet in an interrupt or maybe we slept
656 * while doing page faults in uiomove() etc. We could
657 * probably recheck again inside the splnet() protection
658 * here, but there are probably other places that this
659 * also happens. We must rethink this.
661 error
= so_pru_send(so
, pru_flags
, top
, addr
, control
, td
);
664 so
->so_options
&= ~SO_DONTROUTE
;
671 } while (resid
&& space
> 0);
675 ssb_unlock(&so
->so_snd
);
685 * A specialization of sosend() for UDP based on protocol-specific knowledge:
686 * so->so_proto->pr_flags has the PR_ATOMIC field set. This means that
687 * sosendallatonce() returns true,
688 * the "atomic" variable is true,
689 * and sosendudp() blocks until space is available for the entire send.
690 * so->so_proto->pr_flags does not have the PR_CONNREQUIRED or
691 * PR_IMPLOPCL flags set.
692 * UDP has no out-of-band data.
693 * UDP has no control data.
694 * UDP does not support MSG_EOR.
697 sosendudp(struct socket
*so
, struct sockaddr
*addr
, struct uio
*uio
,
698 struct mbuf
*top
, struct mbuf
*control
, int flags
, struct thread
*td
)
701 boolean_t dontroute
; /* temporary SO_DONTROUTE setting */
703 if (td
->td_lwp
!= NULL
)
704 td
->td_lwp
->lwp_ru
.ru_msgsnd
++;
708 KASSERT((uio
&& !top
) || (top
&& !uio
), ("bad arguments to sosendudp"));
709 resid
= uio
? uio
->uio_resid
: top
->m_pkthdr
.len
;
712 error
= ssb_lock(&so
->so_snd
, SBLOCKWAIT(flags
));
717 if (so
->so_state
& SS_CANTSENDMORE
)
720 error
= so
->so_error
;
725 if (!(so
->so_state
& SS_ISCONNECTED
) && addr
== NULL
)
726 gotoerr(EDESTADDRREQ
);
727 if (resid
> so
->so_snd
.ssb_hiwat
)
729 if (uio
&& ssb_space(&so
->so_snd
) < resid
) {
730 if (flags
& (MSG_FNONBLOCKING
|MSG_DONTWAIT
))
731 gotoerr(EWOULDBLOCK
);
732 ssb_unlock(&so
->so_snd
);
733 error
= ssb_wait(&so
->so_snd
);
742 top
= m_uiomove(uio
);
747 dontroute
= (flags
& MSG_DONTROUTE
) && !(so
->so_options
& SO_DONTROUTE
);
749 so
->so_options
|= SO_DONTROUTE
;
751 error
= so_pru_send(so
, 0, top
, addr
, NULL
, td
);
752 top
= NULL
; /* sent or freed in lower layer */
755 so
->so_options
&= ~SO_DONTROUTE
;
758 ssb_unlock(&so
->so_snd
);
766 * Implement receive operations on a socket.
767 * We depend on the way that records are added to the signalsockbuf
768 * by sbappend*. In particular, each record (mbufs linked through m_next)
769 * must begin with an address if the protocol so specifies,
770 * followed by an optional mbuf or mbufs containing ancillary data,
771 * and then zero or more mbufs of data.
772 * In order to avoid blocking network interrupts for the entire time here,
773 * we exit the critical section while doing the actual copy to user space.
774 * Although the signalsockbuf is locked, new data may still be appended,
775 * and thus we must maintain consistency of the signalsockbuf during that time.
777 * The caller may receive the data as a single mbuf chain by supplying
778 * an mbuf **mp0 for use in returning the chain. The uio is then used
779 * only for the count in uio_resid.
782 soreceive(struct socket
*so
, struct sockaddr
**psa
, struct uio
*uio
,
783 struct sockbuf
*sio
, struct mbuf
**controlp
, int *flagsp
)
786 struct mbuf
*free_chain
= NULL
;
787 int flags
, len
, error
, offset
;
788 struct protosw
*pr
= so
->so_proto
;
790 int resid
, orig_resid
;
793 resid
= uio
->uio_resid
;
795 resid
= (int)(sio
->sb_climit
- sio
->sb_cc
);
803 flags
= *flagsp
&~ MSG_EOR
;
806 if (flags
& MSG_OOB
) {
807 m
= m_get(MB_WAIT
, MT_DATA
);
810 error
= so_pru_rcvoob(so
, m
, flags
& MSG_PEEK
);
817 } while (resid
> 0 && m
);
820 uio
->uio_resid
= resid
;
821 error
= uiomove(mtod(m
, caddr_t
),
822 (int)min(resid
, m
->m_len
), uio
);
823 resid
= uio
->uio_resid
;
825 } while (uio
->uio_resid
&& error
== 0 && m
);
832 if (so
->so_state
& SS_ISCONFIRMING
&& resid
)
837 error
= ssb_lock(&so
->so_rcv
, SBLOCKWAIT(flags
));
841 m
= so
->so_rcv
.ssb_mb
;
843 * If we have less data than requested, block awaiting more
844 * (subject to any timeout) if:
845 * 1. the current count is less than the low water mark, or
846 * 2. MSG_WAITALL is set, and it is possible to do the entire
847 * receive operation at once if we block (resid <= hiwat).
848 * 3. MSG_DONTWAIT is not set
849 * If MSG_WAITALL is set but resid is larger than the receive buffer,
850 * we have to do the receive in sections, and thus risk returning
851 * a short count if a timeout or signal occurs after we start.
853 if (m
== NULL
|| (((flags
& MSG_DONTWAIT
) == 0 &&
854 so
->so_rcv
.ssb_cc
< resid
) &&
855 (so
->so_rcv
.ssb_cc
< so
->so_rcv
.ssb_lowat
||
856 ((flags
& MSG_WAITALL
) && resid
<= so
->so_rcv
.ssb_hiwat
)) &&
857 m
->m_nextpkt
== 0 && (pr
->pr_flags
& PR_ATOMIC
) == 0)) {
858 KASSERT(m
!= NULL
|| !so
->so_rcv
.ssb_cc
, ("receive 1"));
862 error
= so
->so_error
;
863 if ((flags
& MSG_PEEK
) == 0)
867 if (so
->so_state
& SS_CANTRCVMORE
) {
873 for (; m
; m
= m
->m_next
) {
874 if (m
->m_type
== MT_OOBDATA
|| (m
->m_flags
& M_EOR
)) {
875 m
= so
->so_rcv
.ssb_mb
;
879 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
)) == 0 &&
880 (pr
->pr_flags
& PR_CONNREQUIRED
)) {
886 if (flags
& (MSG_FNONBLOCKING
|MSG_DONTWAIT
)) {
890 ssb_unlock(&so
->so_rcv
);
891 error
= ssb_wait(&so
->so_rcv
);
898 if (uio
&& uio
->uio_td
&& uio
->uio_td
->td_proc
)
899 uio
->uio_td
->td_lwp
->lwp_ru
.ru_msgrcv
++;
902 * note: m should be == sb_mb here. Cache the next record while
903 * cleaning up. Note that calling m_free*() will break out critical
906 KKASSERT(m
== so
->so_rcv
.ssb_mb
);
909 * Skip any address mbufs prepending the record.
911 if (pr
->pr_flags
& PR_ADDR
) {
912 KASSERT(m
->m_type
== MT_SONAME
, ("receive 1a"));
915 *psa
= dup_sockaddr(mtod(m
, struct sockaddr
*));
916 if (flags
& MSG_PEEK
)
919 m
= sbunlinkmbuf(&so
->so_rcv
.sb
, m
, &free_chain
);
923 * Skip any control mbufs prepending the record.
926 if (pr
->pr_flags
& PR_ADDR_OPT
) {
928 * For SCTP we may be getting a
929 * whole message OR a partial delivery.
931 if (m
&& m
->m_type
== MT_SONAME
) {
934 *psa
= dup_sockaddr(mtod(m
, struct sockaddr
*));
935 if (flags
& MSG_PEEK
)
938 m
= sbunlinkmbuf(&so
->so_rcv
.sb
, m
, &free_chain
);
942 while (m
&& m
->m_type
== MT_CONTROL
&& error
== 0) {
943 if (flags
& MSG_PEEK
) {
945 *controlp
= m_copy(m
, 0, m
->m_len
);
946 m
= m
->m_next
; /* XXX race */
949 n
= sbunlinkmbuf(&so
->so_rcv
.sb
, m
, NULL
);
950 if (pr
->pr_domain
->dom_externalize
&&
951 mtod(m
, struct cmsghdr
*)->cmsg_type
==
953 error
= (*pr
->pr_domain
->dom_externalize
)(m
);
957 m
= sbunlinkmbuf(&so
->so_rcv
.sb
, m
, &free_chain
);
960 if (controlp
&& *controlp
) {
962 controlp
= &(*controlp
)->m_next
;
971 if (type
== MT_OOBDATA
)
976 * Copy to the UIO or mbuf return chain (*mp).
980 while (m
&& resid
> 0 && error
== 0) {
981 if (m
->m_type
== MT_OOBDATA
) {
982 if (type
!= MT_OOBDATA
)
984 } else if (type
== MT_OOBDATA
)
987 KASSERT(m
->m_type
== MT_DATA
|| m
->m_type
== MT_HEADER
,
989 so
->so_state
&= ~SS_RCVATMARK
;
991 if (so
->so_oobmark
&& len
> so
->so_oobmark
- offset
)
992 len
= so
->so_oobmark
- offset
;
993 if (len
> m
->m_len
- moff
)
994 len
= m
->m_len
- moff
;
997 * Copy out to the UIO or pass the mbufs back to the SIO.
998 * The SIO is dealt with when we eat the mbuf, but deal
999 * with the resid here either way.
1003 uio
->uio_resid
= resid
;
1004 error
= uiomove(mtod(m
, caddr_t
) + moff
, len
, uio
);
1005 resid
= uio
->uio_resid
;
1014 * Eat the entire mbuf or just a piece of it
1016 if (len
== m
->m_len
- moff
) {
1017 if (m
->m_flags
& M_EOR
)
1020 if (m
->m_flags
& M_NOTIFICATION
)
1021 flags
|= MSG_NOTIFICATION
;
1023 if (flags
& MSG_PEEK
) {
1028 n
= sbunlinkmbuf(&so
->so_rcv
.sb
, m
, NULL
);
1032 m
= sbunlinkmbuf(&so
->so_rcv
.sb
, m
, &free_chain
);
1036 if (flags
& MSG_PEEK
) {
1040 n
= m_copym(m
, 0, len
, MB_WAIT
);
1046 so
->so_rcv
.ssb_cc
-= len
;
1049 if (so
->so_oobmark
) {
1050 if ((flags
& MSG_PEEK
) == 0) {
1051 so
->so_oobmark
-= len
;
1052 if (so
->so_oobmark
== 0) {
1053 so
->so_state
|= SS_RCVATMARK
;
1058 if (offset
== so
->so_oobmark
)
1062 if (flags
& MSG_EOR
)
1065 * If the MSG_WAITALL flag is set (for non-atomic socket),
1066 * we must not quit until resid == 0 or an error
1067 * termination. If a signal/timeout occurs, return
1068 * with a short count but without error.
1069 * Keep signalsockbuf locked against other readers.
1071 while ((flags
& MSG_WAITALL
) && m
== NULL
&&
1072 resid
> 0 && !sosendallatonce(so
) &&
1073 so
->so_rcv
.ssb_mb
== NULL
) {
1074 if (so
->so_error
|| so
->so_state
& SS_CANTRCVMORE
)
1077 * The window might have closed to zero, make
1078 * sure we send an ack now that we've drained
1079 * the buffer or we might end up blocking until
1080 * the idle takes over (5 seconds).
1082 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
)
1083 so_pru_rcvd(so
, flags
);
1084 error
= ssb_wait(&so
->so_rcv
);
1086 ssb_unlock(&so
->so_rcv
);
1090 m
= so
->so_rcv
.ssb_mb
;
1095 * If an atomic read was requested but unread data still remains
1096 * in the record, set MSG_TRUNC.
1098 if (m
&& pr
->pr_flags
& PR_ATOMIC
)
1102 * Cleanup. If an atomic read was requested drop any unread data.
1104 if ((flags
& MSG_PEEK
) == 0) {
1105 if (m
&& (pr
->pr_flags
& PR_ATOMIC
))
1106 sbdroprecord(&so
->so_rcv
.sb
);
1107 if ((pr
->pr_flags
& PR_WANTRCVD
) && so
->so_pcb
)
1108 so_pru_rcvd(so
, flags
);
1111 if (orig_resid
== resid
&& orig_resid
&&
1112 (flags
& MSG_EOR
) == 0 && (so
->so_state
& SS_CANTRCVMORE
) == 0) {
1113 ssb_unlock(&so
->so_rcv
);
1121 ssb_unlock(&so
->so_rcv
);
1125 m_freem(free_chain
);
1130 soshutdown(struct socket
*so
, int how
)
1132 if (!(how
== SHUT_RD
|| how
== SHUT_WR
|| how
== SHUT_RDWR
))
1138 return (so_pru_shutdown(so
));
1143 sorflush(struct socket
*so
)
1145 struct signalsockbuf
*ssb
= &so
->so_rcv
;
1146 struct protosw
*pr
= so
->so_proto
;
1147 struct signalsockbuf asb
;
1149 ssb
->ssb_flags
|= SSB_NOINTR
;
1150 (void) ssb_lock(ssb
, M_WAITOK
);
1156 bzero((caddr_t
)ssb
, sizeof (*ssb
));
1157 if (asb
.ssb_flags
& SSB_KNOTE
) {
1158 ssb
->ssb_sel
.si_note
= asb
.ssb_sel
.si_note
;
1159 ssb
->ssb_flags
= SSB_KNOTE
;
1163 if (pr
->pr_flags
& PR_RIGHTS
&& pr
->pr_domain
->dom_dispose
)
1164 (*pr
->pr_domain
->dom_dispose
)(asb
.ssb_mb
);
1165 ssb_release(&asb
, so
);
1170 do_setopt_accept_filter(struct socket
*so
, struct sockopt
*sopt
)
1172 struct accept_filter_arg
*afap
= NULL
;
1173 struct accept_filter
*afp
;
1174 struct so_accf
*af
= so
->so_accf
;
1177 /* do not set/remove accept filters on non listen sockets */
1178 if ((so
->so_options
& SO_ACCEPTCONN
) == 0) {
1183 /* removing the filter */
1186 if (af
->so_accept_filter
!= NULL
&&
1187 af
->so_accept_filter
->accf_destroy
!= NULL
) {
1188 af
->so_accept_filter
->accf_destroy(so
);
1190 if (af
->so_accept_filter_str
!= NULL
) {
1191 FREE(af
->so_accept_filter_str
, M_ACCF
);
1196 so
->so_options
&= ~SO_ACCEPTFILTER
;
1199 /* adding a filter */
1200 /* must remove previous filter first */
1205 /* don't put large objects on the kernel stack */
1206 MALLOC(afap
, struct accept_filter_arg
*, sizeof(*afap
), M_TEMP
, M_WAITOK
);
1207 error
= sooptcopyin(sopt
, afap
, sizeof *afap
, sizeof *afap
);
1208 afap
->af_name
[sizeof(afap
->af_name
)-1] = '\0';
1209 afap
->af_arg
[sizeof(afap
->af_arg
)-1] = '\0';
1212 afp
= accept_filt_get(afap
->af_name
);
1217 MALLOC(af
, struct so_accf
*, sizeof(*af
), M_ACCF
, M_WAITOK
| M_ZERO
);
1218 if (afp
->accf_create
!= NULL
) {
1219 if (afap
->af_name
[0] != '\0') {
1220 int len
= strlen(afap
->af_name
) + 1;
1222 MALLOC(af
->so_accept_filter_str
, char *, len
, M_ACCF
, M_WAITOK
);
1223 strcpy(af
->so_accept_filter_str
, afap
->af_name
);
1225 af
->so_accept_filter_arg
= afp
->accf_create(so
, afap
->af_arg
);
1226 if (af
->so_accept_filter_arg
== NULL
) {
1227 FREE(af
->so_accept_filter_str
, M_ACCF
);
1234 af
->so_accept_filter
= afp
;
1236 so
->so_options
|= SO_ACCEPTFILTER
;
1245 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1246 * an additional variant to handle the case where the option value needs
1247 * to be some kind of integer, but not a specific size.
1248 * In addition to their use here, these functions are also called by the
1249 * protocol-level pr_ctloutput() routines.
1252 sooptcopyin(struct sockopt
*sopt
, void *buf
, size_t len
, size_t minlen
)
1254 return soopt_to_kbuf(sopt
, buf
, len
, minlen
);
1258 soopt_to_kbuf(struct sockopt
*sopt
, void *buf
, size_t len
, size_t minlen
)
1262 KKASSERT(!sopt
->sopt_val
|| kva_p(sopt
->sopt_val
));
1263 KKASSERT(kva_p(buf
));
1266 * If the user gives us more than we wanted, we ignore it,
1267 * but if we don't get the minimum length the caller
1268 * wants, we return EINVAL. On success, sopt->sopt_valsize
1269 * is set to however much we actually retrieved.
1271 if ((valsize
= sopt
->sopt_valsize
) < minlen
)
1274 sopt
->sopt_valsize
= valsize
= len
;
1276 bcopy(sopt
->sopt_val
, buf
, valsize
);
1282 sosetopt(struct socket
*so
, struct sockopt
*sopt
)
1290 sopt
->sopt_dir
= SOPT_SET
;
1291 if (sopt
->sopt_level
!= SOL_SOCKET
) {
1292 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
1293 return (so_pru_ctloutput(so
, sopt
));
1295 error
= ENOPROTOOPT
;
1297 switch (sopt
->sopt_name
) {
1299 case SO_ACCEPTFILTER
:
1300 error
= do_setopt_accept_filter(so
, sopt
);
1306 error
= sooptcopyin(sopt
, &l
, sizeof l
, sizeof l
);
1310 so
->so_linger
= l
.l_linger
;
1312 so
->so_options
|= SO_LINGER
;
1314 so
->so_options
&= ~SO_LINGER
;
1320 case SO_USELOOPBACK
:
1326 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1331 so
->so_options
|= sopt
->sopt_name
;
1333 so
->so_options
&= ~sopt
->sopt_name
;
1340 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1346 * Values < 1 make no sense for any of these
1347 * options, so disallow them.
1354 switch (sopt
->sopt_name
) {
1357 if (ssb_reserve(sopt
->sopt_name
== SO_SNDBUF
?
1358 &so
->so_snd
: &so
->so_rcv
, (u_long
)optval
,
1360 &curproc
->p_rlimit
[RLIMIT_SBSIZE
]) == 0) {
1367 * Make sure the low-water is never greater than
1371 so
->so_snd
.ssb_lowat
=
1372 (optval
> so
->so_snd
.ssb_hiwat
) ?
1373 so
->so_snd
.ssb_hiwat
: optval
;
1376 so
->so_rcv
.ssb_lowat
=
1377 (optval
> so
->so_rcv
.ssb_hiwat
) ?
1378 so
->so_rcv
.ssb_hiwat
: optval
;
1385 error
= sooptcopyin(sopt
, &tv
, sizeof tv
,
1390 /* assert(hz > 0); */
1391 if (tv
.tv_sec
< 0 || tv
.tv_sec
> SHRT_MAX
/ hz
||
1392 tv
.tv_usec
< 0 || tv
.tv_usec
>= 1000000) {
1396 /* assert(tick > 0); */
1397 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1398 val
= (u_long
)(tv
.tv_sec
* hz
) + tv
.tv_usec
/ tick
;
1399 if (val
> SHRT_MAX
) {
1403 if (val
== 0 && tv
.tv_usec
!= 0)
1406 switch (sopt
->sopt_name
) {
1408 so
->so_snd
.ssb_timeo
= val
;
1411 so
->so_rcv
.ssb_timeo
= val
;
1416 error
= ENOPROTOOPT
;
1419 if (error
== 0 && so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
1420 (void) so_pru_ctloutput(so
, sopt
);
1427 /* Helper routine for getsockopt */
1429 sooptcopyout(struct sockopt
*sopt
, const void *buf
, size_t len
)
1431 soopt_from_kbuf(sopt
, buf
, len
);
1436 soopt_from_kbuf(struct sockopt
*sopt
, const void *buf
, size_t len
)
1440 KKASSERT(!sopt
->sopt_val
|| kva_p(sopt
->sopt_val
));
1441 KKASSERT(kva_p(buf
));
1444 * Documented get behavior is that we always return a value,
1445 * possibly truncated to fit in the user's buffer.
1446 * Traditional behavior is that we always tell the user
1447 * precisely how much we copied, rather than something useful
1448 * like the total amount we had available for her.
1449 * Note that this interface is not idempotent; the entire answer must
1450 * generated ahead of time.
1452 valsize
= min(len
, sopt
->sopt_valsize
);
1453 sopt
->sopt_valsize
= valsize
;
1454 if (sopt
->sopt_val
!= 0) {
1455 bcopy(buf
, sopt
->sopt_val
, valsize
);
1460 sogetopt(struct socket
*so
, struct sockopt
*sopt
)
1466 struct accept_filter_arg
*afap
;
1470 sopt
->sopt_dir
= SOPT_GET
;
1471 if (sopt
->sopt_level
!= SOL_SOCKET
) {
1472 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
1473 return (so_pru_ctloutput(so
, sopt
));
1475 return (ENOPROTOOPT
);
1477 switch (sopt
->sopt_name
) {
1479 case SO_ACCEPTFILTER
:
1480 if ((so
->so_options
& SO_ACCEPTCONN
) == 0)
1482 MALLOC(afap
, struct accept_filter_arg
*, sizeof(*afap
),
1483 M_TEMP
, M_WAITOK
| M_ZERO
);
1484 if ((so
->so_options
& SO_ACCEPTFILTER
) != 0) {
1485 strcpy(afap
->af_name
, so
->so_accf
->so_accept_filter
->accf_name
);
1486 if (so
->so_accf
->so_accept_filter_str
!= NULL
)
1487 strcpy(afap
->af_arg
, so
->so_accf
->so_accept_filter_str
);
1489 error
= sooptcopyout(sopt
, afap
, sizeof(*afap
));
1495 l
.l_onoff
= so
->so_options
& SO_LINGER
;
1496 l
.l_linger
= so
->so_linger
;
1497 error
= sooptcopyout(sopt
, &l
, sizeof l
);
1500 case SO_USELOOPBACK
:
1509 optval
= so
->so_options
& sopt
->sopt_name
;
1511 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1515 optval
= so
->so_type
;
1519 optval
= so
->so_error
;
1524 optval
= so
->so_snd
.ssb_hiwat
;
1528 optval
= so
->so_rcv
.ssb_hiwat
;
1532 optval
= so
->so_snd
.ssb_lowat
;
1536 optval
= so
->so_rcv
.ssb_lowat
;
1541 optval
= (sopt
->sopt_name
== SO_SNDTIMEO
?
1542 so
->so_snd
.ssb_timeo
: so
->so_rcv
.ssb_timeo
);
1544 tv
.tv_sec
= optval
/ hz
;
1545 tv
.tv_usec
= (optval
% hz
) * tick
;
1546 error
= sooptcopyout(sopt
, &tv
, sizeof tv
);
1550 error
= ENOPROTOOPT
;
1557 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1559 soopt_getm(struct sockopt
*sopt
, struct mbuf
**mp
)
1561 struct mbuf
*m
, *m_prev
;
1562 int sopt_size
= sopt
->sopt_valsize
, msize
;
1564 m
= m_getl(sopt_size
, sopt
->sopt_td
? MB_WAIT
: MB_DONTWAIT
, MT_DATA
,
1568 m
->m_len
= min(msize
, sopt_size
);
1569 sopt_size
-= m
->m_len
;
1573 while (sopt_size
> 0) {
1574 m
= m_getl(sopt_size
, sopt
->sopt_td
? MB_WAIT
: MB_DONTWAIT
,
1575 MT_DATA
, 0, &msize
);
1580 m
->m_len
= min(msize
, sopt_size
);
1581 sopt_size
-= m
->m_len
;
1588 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1590 soopt_mcopyin(struct sockopt
*sopt
, struct mbuf
*m
)
1592 soopt_to_mbuf(sopt
, m
);
1597 soopt_to_mbuf(struct sockopt
*sopt
, struct mbuf
*m
)
1602 KKASSERT(!sopt
->sopt_val
|| kva_p(sopt
->sopt_val
));
1604 if (sopt
->sopt_val
== NULL
)
1606 val
= sopt
->sopt_val
;
1607 valsize
= sopt
->sopt_valsize
;
1608 while (m
!= NULL
&& valsize
>= m
->m_len
) {
1609 bcopy(val
, mtod(m
, char *), m
->m_len
);
1610 valsize
-= m
->m_len
;
1611 val
= (caddr_t
)val
+ m
->m_len
;
1614 if (m
!= NULL
) /* should be allocated enoughly at ip6_sooptmcopyin() */
1615 panic("ip6_sooptmcopyin");
1618 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1620 soopt_mcopyout(struct sockopt
*sopt
, struct mbuf
*m
)
1622 return soopt_from_mbuf(sopt
, m
);
1626 soopt_from_mbuf(struct sockopt
*sopt
, struct mbuf
*m
)
1628 struct mbuf
*m0
= m
;
1633 KKASSERT(!sopt
->sopt_val
|| kva_p(sopt
->sopt_val
));
1635 if (sopt
->sopt_val
== NULL
)
1637 val
= sopt
->sopt_val
;
1638 maxsize
= sopt
->sopt_valsize
;
1639 while (m
!= NULL
&& maxsize
>= m
->m_len
) {
1640 bcopy(mtod(m
, char *), val
, m
->m_len
);
1641 maxsize
-= m
->m_len
;
1642 val
= (caddr_t
)val
+ m
->m_len
;
1643 valsize
+= m
->m_len
;
1647 /* enough soopt buffer should be given from user-land */
1651 sopt
->sopt_valsize
= valsize
;
1656 sohasoutofband(struct socket
*so
)
1658 if (so
->so_sigio
!= NULL
)
1659 pgsigio(so
->so_sigio
, SIGURG
, 0);
1660 selwakeup(&so
->so_rcv
.ssb_sel
);
1664 sopoll(struct socket
*so
, int events
, struct ucred
*cred
, struct thread
*td
)
1670 if (events
& (POLLIN
| POLLRDNORM
))
1672 revents
|= events
& (POLLIN
| POLLRDNORM
);
1674 if (events
& POLLINIGNEOF
)
1675 if (so
->so_rcv
.ssb_cc
>= so
->so_rcv
.ssb_lowat
||
1676 !TAILQ_EMPTY(&so
->so_comp
) || so
->so_error
)
1677 revents
|= POLLINIGNEOF
;
1679 if (events
& (POLLOUT
| POLLWRNORM
))
1680 if (sowriteable(so
))
1681 revents
|= events
& (POLLOUT
| POLLWRNORM
);
1683 if (events
& (POLLPRI
| POLLRDBAND
))
1684 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
))
1685 revents
|= events
& (POLLPRI
| POLLRDBAND
);
1689 (POLLIN
| POLLINIGNEOF
| POLLPRI
| POLLRDNORM
|
1691 selrecord(td
, &so
->so_rcv
.ssb_sel
);
1692 so
->so_rcv
.ssb_flags
|= SSB_SEL
;
1695 if (events
& (POLLOUT
| POLLWRNORM
)) {
1696 selrecord(td
, &so
->so_snd
.ssb_sel
);
1697 so
->so_snd
.ssb_flags
|= SSB_SEL
;
1706 sokqfilter(struct file
*fp
, struct knote
*kn
)
1708 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
1709 struct signalsockbuf
*ssb
;
1711 switch (kn
->kn_filter
) {
1713 if (so
->so_options
& SO_ACCEPTCONN
)
1714 kn
->kn_fop
= &solisten_filtops
;
1716 kn
->kn_fop
= &soread_filtops
;
1720 kn
->kn_fop
= &sowrite_filtops
;
1728 SLIST_INSERT_HEAD(&ssb
->ssb_sel
.si_note
, kn
, kn_selnext
);
1729 ssb
->ssb_flags
|= SSB_KNOTE
;
1735 filt_sordetach(struct knote
*kn
)
1737 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
1740 SLIST_REMOVE(&so
->so_rcv
.ssb_sel
.si_note
, kn
, knote
, kn_selnext
);
1741 if (SLIST_EMPTY(&so
->so_rcv
.ssb_sel
.si_note
))
1742 so
->so_rcv
.ssb_flags
&= ~SSB_KNOTE
;
1748 filt_soread(struct knote
*kn
, long hint
)
1750 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
1752 kn
->kn_data
= so
->so_rcv
.ssb_cc
;
1753 if (so
->so_state
& SS_CANTRCVMORE
) {
1754 kn
->kn_flags
|= EV_EOF
;
1755 kn
->kn_fflags
= so
->so_error
;
1758 if (so
->so_error
) /* temporary udp error */
1760 if (kn
->kn_sfflags
& NOTE_LOWAT
)
1761 return (kn
->kn_data
>= kn
->kn_sdata
);
1762 return (kn
->kn_data
>= so
->so_rcv
.ssb_lowat
);
1766 filt_sowdetach(struct knote
*kn
)
1768 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
1771 SLIST_REMOVE(&so
->so_snd
.ssb_sel
.si_note
, kn
, knote
, kn_selnext
);
1772 if (SLIST_EMPTY(&so
->so_snd
.ssb_sel
.si_note
))
1773 so
->so_snd
.ssb_flags
&= ~SSB_KNOTE
;
1779 filt_sowrite(struct knote
*kn
, long hint
)
1781 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
1783 kn
->kn_data
= ssb_space(&so
->so_snd
);
1784 if (so
->so_state
& SS_CANTSENDMORE
) {
1785 kn
->kn_flags
|= EV_EOF
;
1786 kn
->kn_fflags
= so
->so_error
;
1789 if (so
->so_error
) /* temporary udp error */
1791 if (((so
->so_state
& SS_ISCONNECTED
) == 0) &&
1792 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
))
1794 if (kn
->kn_sfflags
& NOTE_LOWAT
)
1795 return (kn
->kn_data
>= kn
->kn_sdata
);
1796 return (kn
->kn_data
>= so
->so_snd
.ssb_lowat
);
1801 filt_solisten(struct knote
*kn
, long hint
)
1803 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
1805 kn
->kn_data
= so
->so_qlen
;
1806 return (! TAILQ_EMPTY(&so
->so_comp
));