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) 2004 Jeffrey M. Hsu. All rights reserved.
37 * License terms: all terms for the DragonFly license above plus the following:
39 * 4. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
42 * This product includes software developed by Jeffrey M. Hsu
43 * for the DragonFly Project.
45 * This requirement may be waived with permission from Jeffrey Hsu.
46 * This requirement will sunset and may be removed on July 8 2005,
47 * after which the standard DragonFly license (as shown above) will
52 * Copyright (c) 1982, 1986, 1988, 1990, 1993
53 * The Regents of the University of California. All rights reserved.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
84 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.24 2003/11/11 17:18:18 silby Exp $
85 * $DragonFly: src/sys/kern/uipc_socket.c,v 1.36 2005/07/23 07:28:34 dillon Exp $
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/fcntl.h>
94 #include <sys/malloc.h>
96 #include <sys/domain.h>
97 #include <sys/file.h> /* for struct knote */
98 #include <sys/kernel.h>
99 #include <sys/malloc.h>
100 #include <sys/event.h>
101 #include <sys/poll.h>
102 #include <sys/proc.h>
103 #include <sys/protosw.h>
104 #include <sys/socket.h>
105 #include <sys/socketvar.h>
106 #include <sys/socketops.h>
107 #include <sys/resourcevar.h>
108 #include <sys/signalvar.h>
109 #include <sys/sysctl.h>
111 #include <sys/jail.h>
112 #include <vm/vm_zone.h>
114 #include <sys/thread2.h>
116 #include <machine/limits.h>
119 static int do_setopt_accept_filter(struct socket
*so
, struct sockopt
*sopt
);
122 static void filt_sordetach(struct knote
*kn
);
123 static int filt_soread(struct knote
*kn
, long hint
);
124 static void filt_sowdetach(struct knote
*kn
);
125 static int filt_sowrite(struct knote
*kn
, long hint
);
126 static int filt_solisten(struct knote
*kn
, long hint
);
128 static struct filterops solisten_filtops
=
129 { 1, NULL
, filt_sordetach
, filt_solisten
};
130 static struct filterops soread_filtops
=
131 { 1, NULL
, filt_sordetach
, filt_soread
};
132 static struct filterops sowrite_filtops
=
133 { 1, NULL
, filt_sowdetach
, filt_sowrite
};
135 struct vm_zone
*socket_zone
;
136 so_gen_t so_gencnt
; /* generation count for sockets */
138 MALLOC_DEFINE(M_SONAME
, "soname", "socket name");
139 MALLOC_DEFINE(M_PCB
, "pcb", "protocol control block");
142 static int somaxconn
= SOMAXCONN
;
143 SYSCTL_INT(_kern_ipc
, KIPC_SOMAXCONN
, somaxconn
, CTLFLAG_RW
,
144 &somaxconn
, 0, "Maximum pending socket connection queue size");
147 * Socket operation routines.
148 * These routines are called by the routines in
149 * sys_socket.c or from a system process, and
150 * implement the semantics of socket operations by
151 * switching out to the protocol specific routines.
155 * Get a socket structure from our zone, and initialize it.
156 * We don't implement `waitok' yet (see comments in uipc_domain.c).
157 * Note that it would probably be better to allocate socket
158 * and PCB at the same time, but I'm not convinced that all
159 * the protocols can be easily modified to do this.
167 so
= zalloc(socket_zone
);
169 /* XXX race condition for reentrant kernel */
170 bzero(so
, sizeof *so
);
171 so
->so_gencnt
= ++so_gencnt
;
172 TAILQ_INIT(&so
->so_aiojobq
);
173 TAILQ_INIT(&so
->so_rcv
.sb_sel
.si_mlist
);
174 TAILQ_INIT(&so
->so_snd
.sb_sel
.si_mlist
);
180 socreate(int dom
, struct socket
**aso
, int type
,
181 int proto
, struct thread
*td
)
183 struct proc
*p
= td
->td_proc
;
186 struct pru_attach_info ai
;
190 prp
= pffindproto(dom
, proto
, type
);
192 prp
= pffindtype(dom
, type
);
194 if (prp
== 0 || prp
->pr_usrreqs
->pru_attach
== 0)
195 return (EPROTONOSUPPORT
);
197 if (p
->p_ucred
->cr_prison
&& jail_socket_unixiproute_only
&&
198 prp
->pr_domain
->dom_family
!= PF_LOCAL
&&
199 prp
->pr_domain
->dom_family
!= PF_INET
&&
200 prp
->pr_domain
->dom_family
!= PF_ROUTE
) {
201 return (EPROTONOSUPPORT
);
204 if (prp
->pr_type
!= type
)
206 so
= soalloc(p
!= 0);
210 TAILQ_INIT(&so
->so_incomp
);
211 TAILQ_INIT(&so
->so_comp
);
213 so
->so_cred
= crhold(p
->p_ucred
);
215 ai
.sb_rlimit
= &p
->p_rlimit
[RLIMIT_SBSIZE
];
216 ai
.p_ucred
= p
->p_ucred
;
217 ai
.fd_rdir
= p
->p_fd
->fd_rdir
;
218 error
= so_pru_attach(so
, proto
, &ai
);
220 so
->so_state
|= SS_NOFDREF
;
229 sobind(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
234 error
= so_pru_bind(so
, nam
, td
);
240 sodealloc(struct socket
*so
)
243 so
->so_gencnt
= ++so_gencnt
;
244 if (so
->so_rcv
.sb_hiwat
)
245 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
246 &so
->so_rcv
.sb_hiwat
, 0, RLIM_INFINITY
);
247 if (so
->so_snd
.sb_hiwat
)
248 (void)chgsbsize(so
->so_cred
->cr_uidinfo
,
249 &so
->so_snd
.sb_hiwat
, 0, RLIM_INFINITY
);
251 /* remove accept filter if present */
252 if (so
->so_accf
!= NULL
)
253 do_setopt_accept_filter(so
, NULL
);
256 zfree(socket_zone
, so
);
260 solisten(struct socket
*so
, int backlog
, struct thread
*td
)
264 short oldopt
, oldqlimit
;
268 if (so
->so_state
& (SS_ISCONNECTED
| SS_ISCONNECTING
)) {
274 oldopt
= so
->so_options
;
275 oldqlimit
= so
->so_qlimit
;
278 if (TAILQ_EMPTY(&so
->so_comp
))
279 so
->so_options
|= SO_ACCEPTCONN
;
280 if (backlog
< 0 || backlog
> somaxconn
)
282 so
->so_qlimit
= backlog
;
283 /* SCTP needs to look at tweak both the inbound backlog parameter AND
284 * the so_options (UDP model both connect's and gets inbound
285 * connections .. implicitly).
287 error
= so_pru_listen(so
, td
);
290 /* Restore the params */
291 so
->so_options
= oldopt
;
292 so
->so_qlimit
= oldqlimit
;
302 sofree(struct socket
*so
)
304 struct socket
*head
= so
->so_head
;
306 if (so
->so_pcb
|| (so
->so_state
& SS_NOFDREF
) == 0)
309 if (so
->so_state
& SS_INCOMP
) {
310 TAILQ_REMOVE(&head
->so_incomp
, so
, so_list
);
312 } else if (so
->so_state
& SS_COMP
) {
314 * We must not decommission a socket that's
315 * on the accept(2) queue. If we do, then
316 * accept(2) may hang after select(2) indicated
317 * that the listening socket was ready.
321 panic("sofree: not queued");
323 so
->so_state
&= ~SS_INCOMP
;
326 sbrelease(&so
->so_snd
, so
);
332 * Close a socket on last file table reference removal.
333 * Initiate disconnect if connected.
334 * Free socket when disconnect complete.
337 soclose(struct socket
*so
)
342 funsetown(so
->so_sigio
);
343 if (so
->so_pcb
== NULL
)
345 if (so
->so_state
& SS_ISCONNECTED
) {
346 if ((so
->so_state
& SS_ISDISCONNECTING
) == 0) {
347 error
= sodisconnect(so
);
351 if (so
->so_options
& SO_LINGER
) {
352 if ((so
->so_state
& SS_ISDISCONNECTING
) &&
353 (so
->so_state
& SS_NBIO
))
355 while (so
->so_state
& SS_ISCONNECTED
) {
356 error
= tsleep((caddr_t
)&so
->so_timeo
,
357 PCATCH
, "soclos", so
->so_linger
* hz
);
367 error2
= so_pru_detach(so
);
372 if (so
->so_options
& SO_ACCEPTCONN
) {
373 struct socket
*sp
, *sonext
;
375 sp
= TAILQ_FIRST(&so
->so_incomp
);
376 for (; sp
!= NULL
; sp
= sonext
) {
377 sonext
= TAILQ_NEXT(sp
, so_list
);
380 for (sp
= TAILQ_FIRST(&so
->so_comp
); sp
!= NULL
; sp
= sonext
) {
381 sonext
= TAILQ_NEXT(sp
, so_list
);
382 /* Dequeue from so_comp since sofree() won't do it */
383 TAILQ_REMOVE(&so
->so_comp
, sp
, so_list
);
385 sp
->so_state
&= ~SS_COMP
;
390 if (so
->so_state
& SS_NOFDREF
)
391 panic("soclose: NOFDREF");
392 so
->so_state
|= SS_NOFDREF
;
399 * Must be called from a critical section.
407 error
= so_pru_abort(so
);
416 soaccept(struct socket
*so
, struct sockaddr
**nam
)
421 if ((so
->so_state
& SS_NOFDREF
) == 0)
422 panic("soaccept: !NOFDREF");
423 so
->so_state
&= ~SS_NOFDREF
;
424 error
= so_pru_accept(so
, nam
);
430 soconnect(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
434 if (so
->so_options
& SO_ACCEPTCONN
)
438 * If protocol is connection-based, can only connect once.
439 * Otherwise, if connected, try to disconnect first.
440 * This allows user to disconnect by connecting to, e.g.,
443 if (so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
) &&
444 ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) ||
445 (error
= sodisconnect(so
)))) {
449 * Prevent accumulated error from previous connection
453 error
= so_pru_connect(so
, nam
, td
);
460 soconnect2(struct socket
*so1
, struct socket
*so2
)
465 error
= so_pru_connect2(so1
, so2
);
471 sodisconnect(struct socket
*so
)
476 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
480 if (so
->so_state
& SS_ISDISCONNECTING
) {
484 error
= so_pru_disconnect(so
);
490 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
493 * If send must go all at once and message is larger than
494 * send buffering, then hard error.
495 * Lock against other senders.
496 * If must go all at once and not enough room now, then
497 * inform user that this would block and do nothing.
498 * Otherwise, if nonblocking, send as much as possible.
499 * The data to be sent is described by "uio" if nonzero,
500 * otherwise by the mbuf chain "top" (which must be null
501 * if uio is not). Data provided in mbuf chain must be small
502 * enough to send all at once.
504 * Returns nonzero on error, timeout or signal; callers
505 * must check for short counts if EINTR/ERESTART are returned.
506 * Data and control buffers are freed on return.
509 sosend(struct socket
*so
, struct sockaddr
*addr
, struct uio
*uio
,
510 struct mbuf
*top
, struct mbuf
*control
, int flags
,
515 long space
, len
, resid
;
516 int clen
= 0, error
, dontroute
, mlen
;
517 int atomic
= sosendallatonce(so
) || top
;
521 resid
= uio
->uio_resid
;
523 resid
= top
->m_pkthdr
.len
;
525 * In theory resid should be unsigned.
526 * However, space must be signed, as it might be less than 0
527 * if we over-committed, and we must use a signed comparison
528 * of space and resid. On the other hand, a negative resid
529 * causes us to loop sending 0-length segments to the protocol.
531 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
532 * type sockets since that's an error.
534 if (resid
< 0 || (so
->so_type
== SOCK_STREAM
&& (flags
& MSG_EOR
))) {
540 (flags
& MSG_DONTROUTE
) && (so
->so_options
& SO_DONTROUTE
) == 0 &&
541 (so
->so_proto
->pr_flags
& PR_ATOMIC
);
542 if (td
->td_proc
&& td
->td_proc
->p_stats
)
543 td
->td_proc
->p_stats
->p_ru
.ru_msgsnd
++;
545 clen
= control
->m_len
;
546 #define gotoerr(errno) { error = errno; crit_exit(); goto release; }
549 error
= sblock(&so
->so_snd
, SBLOCKWAIT(flags
));
554 if (so
->so_state
& SS_CANTSENDMORE
)
557 error
= so
->so_error
;
562 if ((so
->so_state
& SS_ISCONNECTED
) == 0) {
564 * `sendto' and `sendmsg' is allowed on a connection-
565 * based socket if it supports implied connect.
566 * Return ENOTCONN if not connected and no address is
569 if ((so
->so_proto
->pr_flags
& PR_CONNREQUIRED
) &&
570 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) == 0) {
571 if ((so
->so_state
& SS_ISCONFIRMING
) == 0 &&
572 !(resid
== 0 && clen
!= 0))
574 } else if (addr
== 0)
575 gotoerr(so
->so_proto
->pr_flags
& PR_CONNREQUIRED
?
576 ENOTCONN
: EDESTADDRREQ
);
578 space
= sbspace(&so
->so_snd
);
581 if ((atomic
&& resid
> so
->so_snd
.sb_hiwat
) ||
582 clen
> so
->so_snd
.sb_hiwat
)
584 if (space
< resid
+ clen
&& uio
&&
585 (atomic
|| space
< so
->so_snd
.sb_lowat
|| space
< clen
)) {
586 if (so
->so_state
& SS_NBIO
)
587 gotoerr(EWOULDBLOCK
);
588 sbunlock(&so
->so_snd
);
589 error
= sbwait(&so
->so_snd
);
601 * Data is prepackaged in "top".
605 top
->m_flags
|= M_EOR
;
607 m
= m_getl(resid
, MB_WAIT
, MT_DATA
,
608 top
== NULL
? M_PKTHDR
: 0, &mlen
);
611 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
613 len
= min(min(mlen
, resid
), space
);
614 if (resid
< MINCLSIZE
) {
616 * For datagram protocols, leave room
617 * for protocol headers in first mbuf.
619 if (atomic
&& top
== 0 && len
< mlen
)
623 error
= uiomove(mtod(m
, caddr_t
), (int)len
, uio
);
624 resid
= uio
->uio_resid
;
627 top
->m_pkthdr
.len
+= len
;
633 top
->m_flags
|= M_EOR
;
636 } while (space
> 0 && atomic
);
638 so
->so_options
|= SO_DONTROUTE
;
639 if (flags
& MSG_OOB
) {
640 pru_flags
= PRUS_OOB
;
641 } else if ((flags
& MSG_EOF
) &&
642 (so
->so_proto
->pr_flags
& PR_IMPLOPCL
) &&
645 * If the user set MSG_EOF, the protocol
646 * understands this flag and nothing left to
647 * send then use PRU_SEND_EOF instead of PRU_SEND.
649 pru_flags
= PRUS_EOF
;
650 } else if (resid
> 0 && space
> 0) {
651 /* If there is more to send, set PRUS_MORETOCOME */
652 pru_flags
= PRUS_MORETOCOME
;
658 * XXX all the SS_CANTSENDMORE checks previously
659 * done could be out of date. We could have recieved
660 * a reset packet in an interrupt or maybe we slept
661 * while doing page faults in uiomove() etc. We could
662 * probably recheck again inside the splnet() protection
663 * here, but there are probably other places that this
664 * also happens. We must rethink this.
666 error
= so_pru_send(so
, pru_flags
, top
, addr
, control
, td
);
669 so
->so_options
&= ~SO_DONTROUTE
;
676 } while (resid
&& space
> 0);
680 sbunlock(&so
->so_snd
);
690 * A specialization of sosend() for UDP based on protocol-specific knowledge:
691 * so->so_proto->pr_flags has the PR_ATOMIC field set. This means that
692 * sosendallatonce() returns true,
693 * the "atomic" variable is true,
694 * and sosendudp() blocks until space is available for the entire send.
695 * so->so_proto->pr_flags does not have the PR_CONNREQUIRED or
696 * PR_IMPLOPCL flags set.
697 * UDP has no out-of-band data.
698 * UDP has no control data.
699 * UDP does not support MSG_EOR.
702 sosendudp(struct socket
*so
, struct sockaddr
*addr
, struct uio
*uio
,
703 struct mbuf
*top
, struct mbuf
*control
, int flags
, struct thread
*td
)
706 boolean_t dontroute
; /* temporary SO_DONTROUTE setting */
708 if (td
->td_proc
&& td
->td_proc
->p_stats
)
709 td
->td_proc
->p_stats
->p_ru
.ru_msgsnd
++;
713 KASSERT((uio
&& !top
) || (top
&& !uio
), ("bad arguments to sosendudp"));
714 resid
= uio
? uio
->uio_resid
: top
->m_pkthdr
.len
;
717 error
= sblock(&so
->so_snd
, SBLOCKWAIT(flags
));
722 if (so
->so_state
& SS_CANTSENDMORE
)
725 error
= so
->so_error
;
730 if (!(so
->so_state
& SS_ISCONNECTED
) && addr
== NULL
)
731 gotoerr(EDESTADDRREQ
);
732 if (resid
> so
->so_snd
.sb_hiwat
)
734 if (uio
&& sbspace(&so
->so_snd
) < resid
) {
735 if (so
->so_state
& SS_NBIO
)
736 gotoerr(EWOULDBLOCK
);
737 sbunlock(&so
->so_snd
);
738 error
= sbwait(&so
->so_snd
);
747 top
= m_uiomove(uio
);
752 dontroute
= (flags
& MSG_DONTROUTE
) && !(so
->so_options
& SO_DONTROUTE
);
754 so
->so_options
|= SO_DONTROUTE
;
756 error
= so_pru_send(so
, 0, top
, addr
, NULL
, td
);
757 top
= NULL
; /* sent or freed in lower layer */
760 so
->so_options
&= ~SO_DONTROUTE
;
763 sbunlock(&so
->so_snd
);
771 * Implement receive operations on a socket.
772 * We depend on the way that records are added to the sockbuf
773 * by sbappend*. In particular, each record (mbufs linked through m_next)
774 * must begin with an address if the protocol so specifies,
775 * followed by an optional mbuf or mbufs containing ancillary data,
776 * and then zero or more mbufs of data.
777 * In order to avoid blocking network interrupts for the entire time here,
778 * we exit the critical section while doing the actual copy to user space.
779 * Although the sockbuf is locked, new data may still be appended,
780 * and thus we must maintain consistency of the sockbuf during that time.
782 * The caller may receive the data as a single mbuf chain by supplying
783 * an mbuf **mp0 for use in returning the chain. The uio is then used
784 * only for the count in uio_resid.
787 soreceive(so
, psa
, uio
, mp0
, controlp
, flagsp
)
789 struct sockaddr
**psa
;
792 struct mbuf
**controlp
;
795 struct mbuf
*m
, *n
, **mp
;
796 struct mbuf
*free_chain
= NULL
;
797 int flags
, len
, error
, offset
;
798 struct protosw
*pr
= so
->so_proto
;
800 int orig_resid
= uio
->uio_resid
;
808 flags
= *flagsp
&~ MSG_EOR
;
811 if (flags
& MSG_OOB
) {
812 m
= m_get(MB_WAIT
, MT_DATA
);
815 error
= so_pru_rcvoob(so
, m
, flags
& MSG_PEEK
);
819 error
= uiomove(mtod(m
, caddr_t
),
820 (int) min(uio
->uio_resid
, m
->m_len
), uio
);
822 } while (uio
->uio_resid
&& error
== 0 && m
);
830 if (so
->so_state
& SS_ISCONFIRMING
&& uio
->uio_resid
)
835 error
= sblock(&so
->so_rcv
, SBLOCKWAIT(flags
));
839 m
= so
->so_rcv
.sb_mb
;
841 * If we have less data than requested, block awaiting more
842 * (subject to any timeout) if:
843 * 1. the current count is less than the low water mark, or
844 * 2. MSG_WAITALL is set, and it is possible to do the entire
845 * receive operation at once if we block (resid <= hiwat).
846 * 3. MSG_DONTWAIT is not set
847 * If MSG_WAITALL is set but resid is larger than the receive buffer,
848 * we have to do the receive in sections, and thus risk returning
849 * a short count if a timeout or signal occurs after we start.
851 if (m
== NULL
|| (((flags
& MSG_DONTWAIT
) == 0 &&
852 so
->so_rcv
.sb_cc
< uio
->uio_resid
) &&
853 (so
->so_rcv
.sb_cc
< so
->so_rcv
.sb_lowat
||
854 ((flags
& MSG_WAITALL
) && uio
->uio_resid
<= so
->so_rcv
.sb_hiwat
)) &&
855 m
->m_nextpkt
== 0 && (pr
->pr_flags
& PR_ATOMIC
) == 0)) {
856 KASSERT(m
!= NULL
|| !so
->so_rcv
.sb_cc
, ("receive 1"));
860 error
= so
->so_error
;
861 if ((flags
& MSG_PEEK
) == 0)
865 if (so
->so_state
& SS_CANTRCVMORE
) {
871 for (; m
; m
= m
->m_next
) {
872 if (m
->m_type
== MT_OOBDATA
|| (m
->m_flags
& M_EOR
)) {
873 m
= so
->so_rcv
.sb_mb
;
877 if ((so
->so_state
& (SS_ISCONNECTED
|SS_ISCONNECTING
)) == 0 &&
878 (pr
->pr_flags
& PR_CONNREQUIRED
)) {
882 if (uio
->uio_resid
== 0)
884 if ((so
->so_state
& SS_NBIO
) || (flags
& MSG_DONTWAIT
)) {
888 sbunlock(&so
->so_rcv
);
889 error
= sbwait(&so
->so_rcv
);
896 if (uio
->uio_td
&& uio
->uio_td
->td_proc
)
897 uio
->uio_td
->td_proc
->p_stats
->p_ru
.ru_msgrcv
++;
900 * note: m should be == sb_mb here. Cache the next record while
901 * cleaning up. Note that calling m_free*() will break out critical
904 KKASSERT(m
== so
->so_rcv
.sb_mb
);
907 * Skip any address mbufs prepending the record.
909 if (pr
->pr_flags
& PR_ADDR
) {
910 KASSERT(m
->m_type
== MT_SONAME
, ("receive 1a"));
913 *psa
= dup_sockaddr(mtod(m
, struct sockaddr
*));
914 if (flags
& MSG_PEEK
)
917 m
= sbunlinkmbuf(&so
->so_rcv
, m
, &free_chain
);
921 * Skip any control mbufs prepending the record.
924 if (pr
->pr_flags
& PR_ADDR_OPT
) {
926 * For SCTP we may be getting a
927 * whole message OR a partial delivery.
929 if (m
&& m
->m_type
== MT_SONAME
) {
932 *psa
= dup_sockaddr(mtod(m
, struct sockaddr
*));
933 if (flags
& MSG_PEEK
)
936 m
= sbunlinkmbuf(&so
->so_rcv
, m
, &free_chain
);
940 while (m
&& m
->m_type
== MT_CONTROL
&& error
== 0) {
941 if (flags
& MSG_PEEK
) {
943 *controlp
= m_copy(m
, 0, m
->m_len
);
944 m
= m
->m_next
; /* XXX race */
947 n
= sbunlinkmbuf(&so
->so_rcv
, m
, NULL
);
948 if (pr
->pr_domain
->dom_externalize
&&
949 mtod(m
, struct cmsghdr
*)->cmsg_type
==
951 error
= (*pr
->pr_domain
->dom_externalize
)(m
);
955 m
= sbunlinkmbuf(&so
->so_rcv
, m
, &free_chain
);
958 if (controlp
&& *controlp
) {
960 controlp
= &(*controlp
)->m_next
;
969 if (type
== MT_OOBDATA
)
974 * Copy to the UIO or mbuf return chain (*mp).
978 while (m
&& uio
->uio_resid
> 0 && error
== 0) {
979 if (m
->m_type
== MT_OOBDATA
) {
980 if (type
!= MT_OOBDATA
)
982 } else if (type
== MT_OOBDATA
)
985 KASSERT(m
->m_type
== MT_DATA
|| m
->m_type
== MT_HEADER
,
987 so
->so_state
&= ~SS_RCVATMARK
;
988 len
= uio
->uio_resid
;
989 if (so
->so_oobmark
&& len
> so
->so_oobmark
- offset
)
990 len
= so
->so_oobmark
- offset
;
991 if (len
> m
->m_len
- moff
)
992 len
= m
->m_len
- moff
;
994 * If mp is set, just pass back the mbufs.
995 * Otherwise copy them out via the uio, then free.
996 * Sockbuf must be consistent here (points to current mbuf,
997 * it points to next record) when we drop priority;
998 * we must note any additions to the sockbuf when we
999 * block interrupts again.
1003 error
= uiomove(mtod(m
, caddr_t
) + moff
, (int)len
, uio
);
1008 uio
->uio_resid
-= len
;
1012 * Eat the entire mbuf or just a piece of it
1014 if (len
== m
->m_len
- moff
) {
1015 if (m
->m_flags
& M_EOR
)
1018 if (m
->m_flags
& M_NOTIFICATION
)
1019 flags
|= MSG_NOTIFICATION
;
1021 if (flags
& MSG_PEEK
) {
1026 n
= sbunlinkmbuf(&so
->so_rcv
, m
, NULL
);
1031 m
= sbunlinkmbuf(&so
->so_rcv
, m
, &free_chain
);
1035 if (flags
& MSG_PEEK
) {
1039 *mp
= m_copym(m
, 0, len
, MB_WAIT
);
1042 so
->so_rcv
.sb_cc
-= len
;
1045 if (so
->so_oobmark
) {
1046 if ((flags
& MSG_PEEK
) == 0) {
1047 so
->so_oobmark
-= len
;
1048 if (so
->so_oobmark
== 0) {
1049 so
->so_state
|= SS_RCVATMARK
;
1054 if (offset
== so
->so_oobmark
)
1058 if (flags
& MSG_EOR
)
1061 * If the MSG_WAITALL flag is set (for non-atomic socket),
1062 * we must not quit until "uio->uio_resid == 0" or an error
1063 * termination. If a signal/timeout occurs, return
1064 * with a short count but without error.
1065 * Keep sockbuf locked against other readers.
1067 while (flags
& MSG_WAITALL
&& m
== NULL
&&
1068 uio
->uio_resid
> 0 && !sosendallatonce(so
) &&
1069 so
->so_rcv
.sb_mb
== NULL
) {
1070 if (so
->so_error
|| so
->so_state
& SS_CANTRCVMORE
)
1073 * The window might have closed to zero, make
1074 * sure we send an ack now that we've drained
1075 * the buffer or we might end up blocking until
1076 * the idle takes over (5 seconds).
1078 if (pr
->pr_flags
& PR_WANTRCVD
&& so
->so_pcb
)
1079 so_pru_rcvd(so
, flags
);
1080 error
= sbwait(&so
->so_rcv
);
1082 sbunlock(&so
->so_rcv
);
1086 m
= so
->so_rcv
.sb_mb
;
1091 * If an atomic read was requested but unread data still remains
1092 * in the record, set MSG_TRUNC.
1094 if (m
&& pr
->pr_flags
& PR_ATOMIC
)
1098 * Cleanup. If an atomic read was requested drop any unread data.
1100 if ((flags
& MSG_PEEK
) == 0) {
1101 if (m
&& (pr
->pr_flags
& PR_ATOMIC
))
1102 sbdroprecord(&so
->so_rcv
);
1103 if ((pr
->pr_flags
& PR_WANTRCVD
) && so
->so_pcb
)
1104 so_pru_rcvd(so
, flags
);
1107 if (orig_resid
== uio
->uio_resid
&& orig_resid
&&
1108 (flags
& MSG_EOR
) == 0 && (so
->so_state
& SS_CANTRCVMORE
) == 0) {
1109 sbunlock(&so
->so_rcv
);
1117 sbunlock(&so
->so_rcv
);
1121 m_freem(free_chain
);
1130 if (!(how
== SHUT_RD
|| how
== SHUT_WR
|| how
== SHUT_RDWR
))
1136 return (so_pru_shutdown(so
));
1144 struct sockbuf
*sb
= &so
->so_rcv
;
1145 struct protosw
*pr
= so
->so_proto
;
1148 sb
->sb_flags
|= SB_NOINTR
;
1149 (void) sblock(sb
, M_WAITOK
);
1155 bzero((caddr_t
)sb
, sizeof (*sb
));
1156 if (asb
.sb_flags
& SB_KNOTE
) {
1157 sb
->sb_sel
.si_note
= asb
.sb_sel
.si_note
;
1158 sb
->sb_flags
= SB_KNOTE
;
1162 if (pr
->pr_flags
& PR_RIGHTS
&& pr
->pr_domain
->dom_dispose
)
1163 (*pr
->pr_domain
->dom_dispose
)(asb
.sb_mb
);
1164 sbrelease(&asb
, so
);
1169 do_setopt_accept_filter(so
, sopt
)
1171 struct sockopt
*sopt
;
1173 struct accept_filter_arg
*afap
= NULL
;
1174 struct accept_filter
*afp
;
1175 struct so_accf
*af
= so
->so_accf
;
1178 /* do not set/remove accept filters on non listen sockets */
1179 if ((so
->so_options
& SO_ACCEPTCONN
) == 0) {
1184 /* removing the filter */
1187 if (af
->so_accept_filter
!= NULL
&&
1188 af
->so_accept_filter
->accf_destroy
!= NULL
) {
1189 af
->so_accept_filter
->accf_destroy(so
);
1191 if (af
->so_accept_filter_str
!= NULL
) {
1192 FREE(af
->so_accept_filter_str
, M_ACCF
);
1197 so
->so_options
&= ~SO_ACCEPTFILTER
;
1200 /* adding a filter */
1201 /* must remove previous filter first */
1206 /* don't put large objects on the kernel stack */
1207 MALLOC(afap
, struct accept_filter_arg
*, sizeof(*afap
), M_TEMP
, M_WAITOK
);
1208 error
= sooptcopyin(sopt
, afap
, sizeof *afap
, sizeof *afap
);
1209 afap
->af_name
[sizeof(afap
->af_name
)-1] = '\0';
1210 afap
->af_arg
[sizeof(afap
->af_arg
)-1] = '\0';
1213 afp
= accept_filt_get(afap
->af_name
);
1218 MALLOC(af
, struct so_accf
*, sizeof(*af
), M_ACCF
, M_WAITOK
);
1219 bzero(af
, sizeof(*af
));
1220 if (afp
->accf_create
!= NULL
) {
1221 if (afap
->af_name
[0] != '\0') {
1222 int len
= strlen(afap
->af_name
) + 1;
1224 MALLOC(af
->so_accept_filter_str
, char *, len
, M_ACCF
, M_WAITOK
);
1225 strcpy(af
->so_accept_filter_str
, afap
->af_name
);
1227 af
->so_accept_filter_arg
= afp
->accf_create(so
, afap
->af_arg
);
1228 if (af
->so_accept_filter_arg
== NULL
) {
1229 FREE(af
->so_accept_filter_str
, M_ACCF
);
1236 af
->so_accept_filter
= afp
;
1238 so
->so_options
|= SO_ACCEPTFILTER
;
1247 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1248 * an additional variant to handle the case where the option value needs
1249 * to be some kind of integer, but not a specific size.
1250 * In addition to their use here, these functions are also called by the
1251 * protocol-level pr_ctloutput() routines.
1254 sooptcopyin(sopt
, buf
, len
, minlen
)
1255 struct sockopt
*sopt
;
1263 * If the user gives us more than we wanted, we ignore it,
1264 * but if we don't get the minimum length the caller
1265 * wants, we return EINVAL. On success, sopt->sopt_valsize
1266 * is set to however much we actually retrieved.
1268 if ((valsize
= sopt
->sopt_valsize
) < minlen
)
1271 sopt
->sopt_valsize
= valsize
= len
;
1273 if (sopt
->sopt_td
!= NULL
)
1274 return (copyin(sopt
->sopt_val
, buf
, valsize
));
1276 bcopy(sopt
->sopt_val
, buf
, valsize
);
1283 struct sockopt
*sopt
;
1291 sopt
->sopt_dir
= SOPT_SET
;
1292 if (sopt
->sopt_level
!= SOL_SOCKET
) {
1293 if (so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
1294 return (so_pr_ctloutput(so
, sopt
));
1296 error
= ENOPROTOOPT
;
1298 switch (sopt
->sopt_name
) {
1300 case SO_ACCEPTFILTER
:
1301 error
= do_setopt_accept_filter(so
, sopt
);
1307 error
= sooptcopyin(sopt
, &l
, sizeof l
, sizeof l
);
1311 so
->so_linger
= l
.l_linger
;
1313 so
->so_options
|= SO_LINGER
;
1315 so
->so_options
&= ~SO_LINGER
;
1321 case SO_USELOOPBACK
:
1327 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1332 so
->so_options
|= sopt
->sopt_name
;
1334 so
->so_options
&= ~sopt
->sopt_name
;
1341 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1347 * Values < 1 make no sense for any of these
1348 * options, so disallow them.
1355 switch (sopt
->sopt_name
) {
1358 if (sbreserve(sopt
->sopt_name
== SO_SNDBUF
?
1359 &so
->so_snd
: &so
->so_rcv
, (u_long
)optval
,
1361 &curproc
->p_rlimit
[RLIMIT_SBSIZE
]) == 0) {
1368 * Make sure the low-water is never greater than
1372 so
->so_snd
.sb_lowat
=
1373 (optval
> so
->so_snd
.sb_hiwat
) ?
1374 so
->so_snd
.sb_hiwat
: optval
;
1377 so
->so_rcv
.sb_lowat
=
1378 (optval
> so
->so_rcv
.sb_hiwat
) ?
1379 so
->so_rcv
.sb_hiwat
: optval
;
1386 error
= sooptcopyin(sopt
, &tv
, sizeof tv
,
1391 /* assert(hz > 0); */
1392 if (tv
.tv_sec
< 0 || tv
.tv_sec
> SHRT_MAX
/ hz
||
1393 tv
.tv_usec
< 0 || tv
.tv_usec
>= 1000000) {
1397 /* assert(tick > 0); */
1398 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1399 val
= (u_long
)(tv
.tv_sec
* hz
) + tv
.tv_usec
/ tick
;
1400 if (val
> SHRT_MAX
) {
1404 if (val
== 0 && tv
.tv_usec
!= 0)
1407 switch (sopt
->sopt_name
) {
1409 so
->so_snd
.sb_timeo
= val
;
1412 so
->so_rcv
.sb_timeo
= val
;
1417 error
= ENOPROTOOPT
;
1420 if (error
== 0 && so
->so_proto
&& so
->so_proto
->pr_ctloutput
) {
1421 (void) so_pr_ctloutput(so
, sopt
);
1428 /* Helper routine for getsockopt */
1430 sooptcopyout(struct sockopt
*sopt
, const void *buf
, size_t len
)
1438 * Documented get behavior is that we always return a value,
1439 * possibly truncated to fit in the user's buffer.
1440 * Traditional behavior is that we always tell the user
1441 * precisely how much we copied, rather than something useful
1442 * like the total amount we had available for her.
1443 * Note that this interface is not idempotent; the entire answer must
1444 * generated ahead of time.
1446 valsize
= min(len
, sopt
->sopt_valsize
);
1447 sopt
->sopt_valsize
= valsize
;
1448 if (sopt
->sopt_val
!= 0) {
1449 if (sopt
->sopt_td
!= NULL
)
1450 error
= copyout(buf
, sopt
->sopt_val
, valsize
);
1452 bcopy(buf
, sopt
->sopt_val
, valsize
);
1460 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_pr_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
),
1484 bzero(afap
, sizeof(*afap
));
1485 if ((so
->so_options
& SO_ACCEPTFILTER
) != 0) {
1486 strcpy(afap
->af_name
, so
->so_accf
->so_accept_filter
->accf_name
);
1487 if (so
->so_accf
->so_accept_filter_str
!= NULL
)
1488 strcpy(afap
->af_arg
, so
->so_accf
->so_accept_filter_str
);
1490 error
= sooptcopyout(sopt
, afap
, sizeof(*afap
));
1496 l
.l_onoff
= so
->so_options
& SO_LINGER
;
1497 l
.l_linger
= so
->so_linger
;
1498 error
= sooptcopyout(sopt
, &l
, sizeof l
);
1501 case SO_USELOOPBACK
:
1510 optval
= so
->so_options
& sopt
->sopt_name
;
1512 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1516 optval
= so
->so_type
;
1520 optval
= so
->so_error
;
1525 optval
= so
->so_snd
.sb_hiwat
;
1529 optval
= so
->so_rcv
.sb_hiwat
;
1533 optval
= so
->so_snd
.sb_lowat
;
1537 optval
= so
->so_rcv
.sb_lowat
;
1542 optval
= (sopt
->sopt_name
== SO_SNDTIMEO
?
1543 so
->so_snd
.sb_timeo
: so
->so_rcv
.sb_timeo
);
1545 tv
.tv_sec
= optval
/ hz
;
1546 tv
.tv_usec
= (optval
% hz
) * tick
;
1547 error
= sooptcopyout(sopt
, &tv
, sizeof tv
);
1551 error
= ENOPROTOOPT
;
1558 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1560 soopt_getm(struct sockopt
*sopt
, struct mbuf
**mp
)
1562 struct mbuf
*m
, *m_prev
;
1563 int sopt_size
= sopt
->sopt_valsize
, msize
;
1565 m
= m_getl(sopt_size
, sopt
->sopt_td
? MB_WAIT
: MB_DONTWAIT
, MT_DATA
,
1569 m
->m_len
= min(msize
, sopt_size
);
1570 sopt_size
-= m
->m_len
;
1574 while (sopt_size
> 0) {
1575 m
= m_getl(sopt_size
, sopt
->sopt_td
? MB_WAIT
: MB_DONTWAIT
,
1576 MT_DATA
, 0, &msize
);
1581 m
->m_len
= min(msize
, sopt_size
);
1582 sopt_size
-= m
->m_len
;
1589 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1591 soopt_mcopyin(struct sockopt
*sopt
, struct mbuf
*m
)
1593 struct mbuf
*m0
= m
;
1595 if (sopt
->sopt_val
== NULL
)
1597 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
1598 if (sopt
->sopt_td
!= NULL
) {
1601 error
= copyin(sopt
->sopt_val
, mtod(m
, char *),
1608 bcopy(sopt
->sopt_val
, mtod(m
, char *), m
->m_len
);
1609 sopt
->sopt_valsize
-= m
->m_len
;
1610 sopt
->sopt_val
= (caddr_t
)sopt
->sopt_val
+ m
->m_len
;
1613 if (m
!= NULL
) /* should be allocated enoughly at ip6_sooptmcopyin() */
1614 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 struct mbuf
*m0
= m
;
1625 if (sopt
->sopt_val
== NULL
)
1627 while (m
!= NULL
&& sopt
->sopt_valsize
>= m
->m_len
) {
1628 if (sopt
->sopt_td
!= NULL
) {
1631 error
= copyout(mtod(m
, char *), sopt
->sopt_val
,
1638 bcopy(mtod(m
, char *), sopt
->sopt_val
, m
->m_len
);
1639 sopt
->sopt_valsize
-= m
->m_len
;
1640 sopt
->sopt_val
= (caddr_t
)sopt
->sopt_val
+ m
->m_len
;
1641 valsize
+= m
->m_len
;
1645 /* enough soopt buffer should be given from user-land */
1649 sopt
->sopt_valsize
= valsize
;
1657 if (so
->so_sigio
!= NULL
)
1658 pgsigio(so
->so_sigio
, SIGURG
, 0);
1659 selwakeup(&so
->so_rcv
.sb_sel
);
1663 sopoll(struct socket
*so
, int events
, struct ucred
*cred
, struct thread
*td
)
1669 if (events
& (POLLIN
| POLLRDNORM
))
1671 revents
|= events
& (POLLIN
| POLLRDNORM
);
1673 if (events
& POLLINIGNEOF
)
1674 if (so
->so_rcv
.sb_cc
>= so
->so_rcv
.sb_lowat
||
1675 !TAILQ_EMPTY(&so
->so_comp
) || so
->so_error
)
1676 revents
|= POLLINIGNEOF
;
1678 if (events
& (POLLOUT
| POLLWRNORM
))
1679 if (sowriteable(so
))
1680 revents
|= events
& (POLLOUT
| POLLWRNORM
);
1682 if (events
& (POLLPRI
| POLLRDBAND
))
1683 if (so
->so_oobmark
|| (so
->so_state
& SS_RCVATMARK
))
1684 revents
|= events
& (POLLPRI
| POLLRDBAND
);
1688 (POLLIN
| POLLINIGNEOF
| POLLPRI
| POLLRDNORM
|
1690 selrecord(td
, &so
->so_rcv
.sb_sel
);
1691 so
->so_rcv
.sb_flags
|= SB_SEL
;
1694 if (events
& (POLLOUT
| POLLWRNORM
)) {
1695 selrecord(td
, &so
->so_snd
.sb_sel
);
1696 so
->so_snd
.sb_flags
|= SB_SEL
;
1705 sokqfilter(struct file
*fp
, struct knote
*kn
)
1707 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
1710 switch (kn
->kn_filter
) {
1712 if (so
->so_options
& SO_ACCEPTCONN
)
1713 kn
->kn_fop
= &solisten_filtops
;
1715 kn
->kn_fop
= &soread_filtops
;
1719 kn
->kn_fop
= &sowrite_filtops
;
1727 SLIST_INSERT_HEAD(&sb
->sb_sel
.si_note
, kn
, kn_selnext
);
1728 sb
->sb_flags
|= SB_KNOTE
;
1734 filt_sordetach(struct knote
*kn
)
1736 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
1739 SLIST_REMOVE(&so
->so_rcv
.sb_sel
.si_note
, kn
, knote
, kn_selnext
);
1740 if (SLIST_EMPTY(&so
->so_rcv
.sb_sel
.si_note
))
1741 so
->so_rcv
.sb_flags
&= ~SB_KNOTE
;
1747 filt_soread(struct knote
*kn
, long hint
)
1749 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
1751 kn
->kn_data
= so
->so_rcv
.sb_cc
;
1752 if (so
->so_state
& SS_CANTRCVMORE
) {
1753 kn
->kn_flags
|= EV_EOF
;
1754 kn
->kn_fflags
= so
->so_error
;
1757 if (so
->so_error
) /* temporary udp error */
1759 if (kn
->kn_sfflags
& NOTE_LOWAT
)
1760 return (kn
->kn_data
>= kn
->kn_sdata
);
1761 return (kn
->kn_data
>= so
->so_rcv
.sb_lowat
);
1765 filt_sowdetach(struct knote
*kn
)
1767 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
1770 SLIST_REMOVE(&so
->so_snd
.sb_sel
.si_note
, kn
, knote
, kn_selnext
);
1771 if (SLIST_EMPTY(&so
->so_snd
.sb_sel
.si_note
))
1772 so
->so_snd
.sb_flags
&= ~SB_KNOTE
;
1778 filt_sowrite(struct knote
*kn
, long hint
)
1780 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
1782 kn
->kn_data
= sbspace(&so
->so_snd
);
1783 if (so
->so_state
& SS_CANTSENDMORE
) {
1784 kn
->kn_flags
|= EV_EOF
;
1785 kn
->kn_fflags
= so
->so_error
;
1788 if (so
->so_error
) /* temporary udp error */
1790 if (((so
->so_state
& SS_ISCONNECTED
) == 0) &&
1791 (so
->so_proto
->pr_flags
& PR_CONNREQUIRED
))
1793 if (kn
->kn_sfflags
& NOTE_LOWAT
)
1794 return (kn
->kn_data
>= kn
->kn_sdata
);
1795 return (kn
->kn_data
>= so
->so_snd
.sb_lowat
);
1800 filt_solisten(struct knote
*kn
, long hint
)
1802 struct socket
*so
= (struct socket
*)kn
->kn_fp
->f_data
;
1804 kn
->kn_data
= so
->so_qlen
;
1805 return (! TAILQ_EMPTY(&so
->so_comp
));