2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
34 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.22 2002/12/15 09:24:23 maxim Exp $
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/fcntl.h>
42 #include <sys/malloc.h>
44 #include <sys/domain.h>
45 #include <sys/file.h> /* for struct knote */
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/event.h>
51 #include <sys/protosw.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/resourcevar.h>
55 #include <sys/signalvar.h>
56 #include <sys/sysctl.h>
59 #include <vm/vm_zone.h>
61 #include <machine/limits.h>
64 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
67 static void filt_sordetach(struct knote *kn);
68 static int filt_soread(struct knote *kn, long hint);
69 static void filt_sowdetach(struct knote *kn);
70 static int filt_sowrite(struct knote *kn, long hint);
71 static int filt_solisten(struct knote *kn, long hint);
73 static struct filterops solisten_filtops =
74 { 1, NULL, filt_sordetach, filt_solisten };
75 static struct filterops soread_filtops =
76 { 1, NULL, filt_sordetach, filt_soread };
77 static struct filterops sowrite_filtops =
78 { 1, NULL, filt_sowdetach, filt_sowrite };
80 struct vm_zone *socket_zone;
81 so_gen_t so_gencnt; /* generation count for sockets */
83 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
84 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
86 SYSCTL_DECL(_kern_ipc);
88 static int somaxconn = SOMAXCONN;
89 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
90 &somaxconn, 0, "Maximum pending socket connection queue size");
93 * Socket operation routines.
94 * These routines are called by the routines in
95 * sys_socket.c or from a system process, and
96 * implement the semantics of socket operations by
97 * switching out to the protocol specific routines.
101 * Get a socket structure from our zone, and initialize it.
102 * We don't implement `waitok' yet (see comments in uipc_domain.c).
103 * Note that it would probably be better to allocate socket
104 * and PCB at the same time, but I'm not convinced that all
105 * the protocols can be easily modified to do this.
113 so = zalloci(socket_zone);
115 /* XXX race condition for reentrant kernel */
116 bzero(so, sizeof *so);
117 so->so_gencnt = ++so_gencnt;
118 TAILQ_INIT(&so->so_aiojobq);
124 socreate(dom, aso, type, proto, p)
131 register struct protosw *prp;
132 register struct socket *so;
136 prp = pffindproto(dom, proto, type);
138 prp = pffindtype(dom, type);
140 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
141 return (EPROTONOSUPPORT);
143 if (p->p_prison && jail_socket_unixiproute_only &&
144 prp->pr_domain->dom_family != PF_LOCAL &&
145 prp->pr_domain->dom_family != PF_INET &&
146 prp->pr_domain->dom_family != PF_ROUTE) {
147 return (EPROTONOSUPPORT);
150 if (prp->pr_type != type)
152 so = soalloc(p != 0);
156 TAILQ_INIT(&so->so_incomp);
157 TAILQ_INIT(&so->so_comp);
159 so->so_cred = p->p_ucred;
162 error = (*prp->pr_usrreqs->pru_attach)(so, proto, p);
164 so->so_state |= SS_NOFDREF;
175 struct sockaddr *nam;
181 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
191 so->so_gencnt = ++so_gencnt;
192 if (so->so_rcv.sb_hiwat)
193 (void)chgsbsize(so->so_cred->cr_uidinfo,
194 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
195 if (so->so_snd.sb_hiwat)
196 (void)chgsbsize(so->so_cred->cr_uidinfo,
197 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
199 if (so->so_accf != NULL) {
200 if (so->so_accf->so_accept_filter != NULL &&
201 so->so_accf->so_accept_filter->accf_destroy != NULL) {
202 so->so_accf->so_accept_filter->accf_destroy(so);
204 if (so->so_accf->so_accept_filter_str != NULL)
205 FREE(so->so_accf->so_accept_filter_str, M_ACCF);
206 FREE(so->so_accf, M_ACCF);
210 zfreei(socket_zone, so);
214 solisten(so, backlog, p)
215 register struct socket *so;
222 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p);
227 if (TAILQ_EMPTY(&so->so_comp))
228 so->so_options |= SO_ACCEPTCONN;
229 if (backlog < 0 || backlog > somaxconn)
231 so->so_qlimit = backlog;
238 register struct socket *so;
240 struct socket *head = so->so_head;
242 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
245 if (so->so_state & SS_INCOMP) {
246 TAILQ_REMOVE(&head->so_incomp, so, so_list);
248 } else if (so->so_state & SS_COMP) {
250 * We must not decommission a socket that's
251 * on the accept(2) queue. If we do, then
252 * accept(2) may hang after select(2) indicated
253 * that the listening socket was ready.
257 panic("sofree: not queued");
259 so->so_state &= ~SS_INCOMP;
262 sbrelease(&so->so_snd, so);
268 * Close a socket on last file table reference removal.
269 * Initiate disconnect if connected.
270 * Free socket when disconnect complete.
274 register struct socket *so;
276 int s = splnet(); /* conservative */
279 funsetown(so->so_sigio);
280 if (so->so_options & SO_ACCEPTCONN) {
281 struct socket *sp, *sonext;
283 sp = TAILQ_FIRST(&so->so_incomp);
284 for (; sp != NULL; sp = sonext) {
285 sonext = TAILQ_NEXT(sp, so_list);
288 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
289 sonext = TAILQ_NEXT(sp, so_list);
290 /* Dequeue from so_comp since sofree() won't do it */
291 TAILQ_REMOVE(&so->so_comp, sp, so_list);
293 sp->so_state &= ~SS_COMP;
300 if (so->so_state & SS_ISCONNECTED) {
301 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
302 error = sodisconnect(so);
306 if (so->so_options & SO_LINGER) {
307 if ((so->so_state & SS_ISDISCONNECTING) &&
308 (so->so_state & SS_NBIO))
310 while (so->so_state & SS_ISCONNECTED) {
311 error = tsleep((caddr_t)&so->so_timeo,
312 PSOCK | PCATCH, "soclos", so->so_linger * hz);
320 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
325 if (so->so_state & SS_NOFDREF)
326 panic("soclose: NOFDREF");
327 so->so_state |= SS_NOFDREF;
334 * Must be called at splnet...
342 error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
352 register struct socket *so;
353 struct sockaddr **nam;
358 if ((so->so_state & SS_NOFDREF) == 0)
359 panic("soaccept: !NOFDREF");
360 so->so_state &= ~SS_NOFDREF;
361 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
367 soconnect(so, nam, p)
368 register struct socket *so;
369 struct sockaddr *nam;
375 if (so->so_options & SO_ACCEPTCONN)
379 * If protocol is connection-based, can only connect once.
380 * Otherwise, if connected, try to disconnect first.
381 * This allows user to disconnect by connecting to, e.g.,
384 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
385 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
386 (error = sodisconnect(so))))
389 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p);
396 register struct socket *so1;
402 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
409 register struct socket *so;
414 if ((so->so_state & SS_ISCONNECTED) == 0) {
418 if (so->so_state & SS_ISDISCONNECTING) {
422 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
428 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
431 * If send must go all at once and message is larger than
432 * send buffering, then hard error.
433 * Lock against other senders.
434 * If must go all at once and not enough room now, then
435 * inform user that this would block and do nothing.
436 * Otherwise, if nonblocking, send as much as possible.
437 * The data to be sent is described by "uio" if nonzero,
438 * otherwise by the mbuf chain "top" (which must be null
439 * if uio is not). Data provided in mbuf chain must be small
440 * enough to send all at once.
442 * Returns nonzero on error, timeout or signal; callers
443 * must check for short counts if EINTR/ERESTART are returned.
444 * Data and control buffers are freed on return.
447 sosend(so, addr, uio, top, control, flags, p)
448 register struct socket *so;
449 struct sockaddr *addr;
452 struct mbuf *control;
457 register struct mbuf *m;
458 register long space, len, resid;
459 int clen = 0, error, s, dontroute, mlen;
460 int atomic = sosendallatonce(so) || top;
463 resid = uio->uio_resid;
465 resid = top->m_pkthdr.len;
467 * In theory resid should be unsigned.
468 * However, space must be signed, as it might be less than 0
469 * if we over-committed, and we must use a signed comparison
470 * of space and resid. On the other hand, a negative resid
471 * causes us to loop sending 0-length segments to the protocol.
473 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
474 * type sockets since that's an error.
476 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
482 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
483 (so->so_proto->pr_flags & PR_ATOMIC);
485 p->p_stats->p_ru.ru_msgsnd++;
487 clen = control->m_len;
488 #define snderr(errno) { error = errno; splx(s); goto release; }
491 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
496 if (so->so_state & SS_CANTSENDMORE)
499 error = so->so_error;
504 if ((so->so_state & SS_ISCONNECTED) == 0) {
506 * `sendto' and `sendmsg' is allowed on a connection-
507 * based socket if it supports implied connect.
508 * Return ENOTCONN if not connected and no address is
511 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
512 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
513 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
514 !(resid == 0 && clen != 0))
516 } else if (addr == 0)
517 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
518 ENOTCONN : EDESTADDRREQ);
520 space = sbspace(&so->so_snd);
523 if ((atomic && resid > so->so_snd.sb_hiwat) ||
524 clen > so->so_snd.sb_hiwat)
526 if (space < resid + clen &&
527 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
528 if (so->so_state & SS_NBIO)
530 sbunlock(&so->so_snd);
531 error = sbwait(&so->so_snd);
543 * Data is prepackaged in "top".
547 top->m_flags |= M_EOR;
550 MGETHDR(m, M_WAIT, MT_DATA);
557 m->m_pkthdr.rcvif = (struct ifnet *)0;
559 MGET(m, M_WAIT, MT_DATA);
566 if (resid >= MINCLSIZE) {
568 if ((m->m_flags & M_EXT) == 0)
571 len = min(min(mlen, resid), space);
574 len = min(min(mlen, resid), space);
576 * For datagram protocols, leave room
577 * for protocol headers in first mbuf.
579 if (atomic && top == 0 && len < mlen)
583 error = uiomove(mtod(m, caddr_t), (int)len, uio);
584 resid = uio->uio_resid;
587 top->m_pkthdr.len += len;
593 top->m_flags |= M_EOR;
596 } while (space > 0 && atomic);
598 so->so_options |= SO_DONTROUTE;
599 s = splnet(); /* XXX */
601 * XXX all the SS_CANTSENDMORE checks previously
602 * done could be out of date. We could have recieved
603 * a reset packet in an interrupt or maybe we slept
604 * while doing page faults in uiomove() etc. We could
605 * probably recheck again inside the splnet() protection
606 * here, but there are probably other places that this
607 * also happens. We must rethink this.
609 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
610 (flags & MSG_OOB) ? PRUS_OOB :
612 * If the user set MSG_EOF, the protocol
613 * understands this flag and nothing left to
614 * send then use PRU_SEND_EOF instead of PRU_SEND.
616 ((flags & MSG_EOF) &&
617 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
620 /* If there is more to send set PRUS_MORETOCOME */
621 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
622 top, addr, control, p);
625 so->so_options &= ~SO_DONTROUTE;
632 } while (resid && space > 0);
636 sbunlock(&so->so_snd);
646 * Implement receive operations on a socket.
647 * We depend on the way that records are added to the sockbuf
648 * by sbappend*. In particular, each record (mbufs linked through m_next)
649 * must begin with an address if the protocol so specifies,
650 * followed by an optional mbuf or mbufs containing ancillary data,
651 * and then zero or more mbufs of data.
652 * In order to avoid blocking network interrupts for the entire time here,
653 * we splx() while doing the actual copy to user space.
654 * Although the sockbuf is locked, new data may still be appended,
655 * and thus we must maintain consistency of the sockbuf during that time.
657 * The caller may receive the data as a single mbuf chain by supplying
658 * an mbuf **mp0 for use in returning the chain. The uio is then used
659 * only for the count in uio_resid.
662 soreceive(so, psa, uio, mp0, controlp, flagsp)
663 register struct socket *so;
664 struct sockaddr **psa;
667 struct mbuf **controlp;
670 register struct mbuf *m, **mp;
671 register int flags, len, error, s, offset;
672 struct protosw *pr = so->so_proto;
673 struct mbuf *nextrecord;
675 int orig_resid = uio->uio_resid;
683 flags = *flagsp &~ MSG_EOR;
686 if (flags & MSG_OOB) {
687 m = m_get(M_WAIT, MT_DATA);
690 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
694 error = uiomove(mtod(m, caddr_t),
695 (int) min(uio->uio_resid, m->m_len), uio);
697 } while (uio->uio_resid && error == 0 && m);
704 *mp = (struct mbuf *)0;
705 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
706 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
709 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
714 m = so->so_rcv.sb_mb;
716 * If we have less data than requested, block awaiting more
717 * (subject to any timeout) if:
718 * 1. the current count is less than the low water mark, or
719 * 2. MSG_WAITALL is set, and it is possible to do the entire
720 * receive operation at once if we block (resid <= hiwat).
721 * 3. MSG_DONTWAIT is not set
722 * If MSG_WAITALL is set but resid is larger than the receive buffer,
723 * we have to do the receive in sections, and thus risk returning
724 * a short count if a timeout or signal occurs after we start.
726 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
727 so->so_rcv.sb_cc < uio->uio_resid) &&
728 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
729 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
730 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
731 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
735 error = so->so_error;
736 if ((flags & MSG_PEEK) == 0)
740 if (so->so_state & SS_CANTRCVMORE) {
746 for (; m; m = m->m_next)
747 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
748 m = so->so_rcv.sb_mb;
751 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
752 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
756 if (uio->uio_resid == 0)
758 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
762 sbunlock(&so->so_rcv);
763 error = sbwait(&so->so_rcv);
771 uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
772 nextrecord = m->m_nextpkt;
773 if (pr->pr_flags & PR_ADDR) {
774 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
777 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
779 if (flags & MSG_PEEK) {
782 sbfree(&so->so_rcv, m);
783 so->so_rcv.sb_mb = m_free(m);
784 m = so->so_rcv.sb_mb;
787 while (m && m->m_type == MT_CONTROL && error == 0) {
788 if (flags & MSG_PEEK) {
790 *controlp = m_copy(m, 0, m->m_len);
793 sbfree(&so->so_rcv, m);
795 if (pr->pr_domain->dom_externalize &&
796 mtod(m, struct cmsghdr *)->cmsg_type ==
798 error = (*pr->pr_domain->dom_externalize)(m);
800 so->so_rcv.sb_mb = m->m_next;
802 m = so->so_rcv.sb_mb;
804 so->so_rcv.sb_mb = m_free(m);
805 m = so->so_rcv.sb_mb;
810 controlp = &(*controlp)->m_next;
814 if ((flags & MSG_PEEK) == 0)
815 m->m_nextpkt = nextrecord;
817 if (type == MT_OOBDATA)
822 while (m && uio->uio_resid > 0 && error == 0) {
823 if (m->m_type == MT_OOBDATA) {
824 if (type != MT_OOBDATA)
826 } else if (type == MT_OOBDATA)
829 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
831 so->so_state &= ~SS_RCVATMARK;
832 len = uio->uio_resid;
833 if (so->so_oobmark && len > so->so_oobmark - offset)
834 len = so->so_oobmark - offset;
835 if (len > m->m_len - moff)
836 len = m->m_len - moff;
838 * If mp is set, just pass back the mbufs.
839 * Otherwise copy them out via the uio, then free.
840 * Sockbuf must be consistent here (points to current mbuf,
841 * it points to next record) when we drop priority;
842 * we must note any additions to the sockbuf when we
843 * block interrupts again.
847 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
852 uio->uio_resid -= len;
853 if (len == m->m_len - moff) {
854 if (m->m_flags & M_EOR)
856 if (flags & MSG_PEEK) {
860 nextrecord = m->m_nextpkt;
861 sbfree(&so->so_rcv, m);
865 so->so_rcv.sb_mb = m = m->m_next;
866 *mp = (struct mbuf *)0;
868 so->so_rcv.sb_mb = m = m_free(m);
871 m->m_nextpkt = nextrecord;
874 if (flags & MSG_PEEK)
878 *mp = m_copym(m, 0, len, M_WAIT);
881 so->so_rcv.sb_cc -= len;
884 if (so->so_oobmark) {
885 if ((flags & MSG_PEEK) == 0) {
886 so->so_oobmark -= len;
887 if (so->so_oobmark == 0) {
888 so->so_state |= SS_RCVATMARK;
893 if (offset == so->so_oobmark)
900 * If the MSG_WAITALL flag is set (for non-atomic socket),
901 * we must not quit until "uio->uio_resid == 0" or an error
902 * termination. If a signal/timeout occurs, return
903 * with a short count but without error.
904 * Keep sockbuf locked against other readers.
906 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
907 !sosendallatonce(so) && !nextrecord) {
908 if (so->so_error || so->so_state & SS_CANTRCVMORE)
911 * The window might have closed to zero, make
912 * sure we send an ack now that we've drained
913 * the buffer or we might end up blocking until
914 * the idle takes over (5 seconds).
916 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
917 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
918 error = sbwait(&so->so_rcv);
920 sbunlock(&so->so_rcv);
924 m = so->so_rcv.sb_mb;
926 nextrecord = m->m_nextpkt;
930 if (m && pr->pr_flags & PR_ATOMIC) {
932 if ((flags & MSG_PEEK) == 0)
933 (void) sbdroprecord(&so->so_rcv);
935 if ((flags & MSG_PEEK) == 0) {
937 so->so_rcv.sb_mb = nextrecord;
938 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
939 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
941 if (orig_resid == uio->uio_resid && orig_resid &&
942 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
943 sbunlock(&so->so_rcv);
951 sbunlock(&so->so_rcv);
958 register struct socket *so;
961 register struct protosw *pr = so->so_proto;
963 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
969 return ((*pr->pr_usrreqs->pru_shutdown)(so));
975 register struct socket *so;
977 register struct sockbuf *sb = &so->so_rcv;
978 register struct protosw *pr = so->so_proto;
982 sb->sb_flags |= SB_NOINTR;
983 (void) sblock(sb, M_WAITOK);
988 bzero((caddr_t)sb, sizeof (*sb));
989 if (asb.sb_flags & SB_KNOTE) {
990 sb->sb_sel.si_note = asb.sb_sel.si_note;
991 sb->sb_flags = SB_KNOTE;
994 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
995 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
1001 do_setopt_accept_filter(so, sopt)
1003 struct sockopt *sopt;
1005 struct accept_filter_arg *afap = NULL;
1006 struct accept_filter *afp;
1007 struct so_accf *af = so->so_accf;
1010 /* do not set/remove accept filters on non listen sockets */
1011 if ((so->so_options & SO_ACCEPTCONN) == 0) {
1016 /* removing the filter */
1019 if (af->so_accept_filter != NULL &&
1020 af->so_accept_filter->accf_destroy != NULL) {
1021 af->so_accept_filter->accf_destroy(so);
1023 if (af->so_accept_filter_str != NULL) {
1024 FREE(af->so_accept_filter_str, M_ACCF);
1029 so->so_options &= ~SO_ACCEPTFILTER;
1032 /* adding a filter */
1033 /* must remove previous filter first */
1038 /* don't put large objects on the kernel stack */
1039 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1040 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1041 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1042 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1045 afp = accept_filt_get(afap->af_name);
1050 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK);
1051 bzero(af, sizeof(*af));
1052 if (afp->accf_create != NULL) {
1053 if (afap->af_name[0] != '\0') {
1054 int len = strlen(afap->af_name) + 1;
1056 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1057 strcpy(af->so_accept_filter_str, afap->af_name);
1059 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1060 if (af->so_accept_filter_arg == NULL) {
1061 FREE(af->so_accept_filter_str, M_ACCF);
1068 af->so_accept_filter = afp;
1070 so->so_options |= SO_ACCEPTFILTER;
1079 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1080 * an additional variant to handle the case where the option value needs
1081 * to be some kind of integer, but not a specific size.
1082 * In addition to their use here, these functions are also called by the
1083 * protocol-level pr_ctloutput() routines.
1086 sooptcopyin(sopt, buf, len, minlen)
1087 struct sockopt *sopt;
1095 * If the user gives us more than we wanted, we ignore it,
1096 * but if we don't get the minimum length the caller
1097 * wants, we return EINVAL. On success, sopt->sopt_valsize
1098 * is set to however much we actually retrieved.
1100 if ((valsize = sopt->sopt_valsize) < minlen)
1103 sopt->sopt_valsize = valsize = len;
1105 if (sopt->sopt_p != 0)
1106 return (copyin(sopt->sopt_val, buf, valsize));
1108 bcopy(sopt->sopt_val, buf, valsize);
1115 struct sockopt *sopt;
1123 if (sopt->sopt_level != SOL_SOCKET) {
1124 if (so->so_proto && so->so_proto->pr_ctloutput)
1125 return ((*so->so_proto->pr_ctloutput)
1127 error = ENOPROTOOPT;
1129 switch (sopt->sopt_name) {
1131 case SO_ACCEPTFILTER:
1132 error = do_setopt_accept_filter(so, sopt);
1138 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1142 so->so_linger = l.l_linger;
1144 so->so_options |= SO_LINGER;
1146 so->so_options &= ~SO_LINGER;
1152 case SO_USELOOPBACK:
1158 error = sooptcopyin(sopt, &optval, sizeof optval,
1163 so->so_options |= sopt->sopt_name;
1165 so->so_options &= ~sopt->sopt_name;
1172 error = sooptcopyin(sopt, &optval, sizeof optval,
1178 * Values < 1 make no sense for any of these
1179 * options, so disallow them.
1186 switch (sopt->sopt_name) {
1189 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1190 &so->so_snd : &so->so_rcv, (u_long)optval,
1191 so, curproc) == 0) {
1198 * Make sure the low-water is never greater than
1202 so->so_snd.sb_lowat =
1203 (optval > so->so_snd.sb_hiwat) ?
1204 so->so_snd.sb_hiwat : optval;
1207 so->so_rcv.sb_lowat =
1208 (optval > so->so_rcv.sb_hiwat) ?
1209 so->so_rcv.sb_hiwat : optval;
1216 error = sooptcopyin(sopt, &tv, sizeof tv,
1221 /* assert(hz > 0); */
1222 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1223 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1227 /* assert(tick > 0); */
1228 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1229 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1230 if (val > SHRT_MAX) {
1234 if (val == 0 && tv.tv_usec != 0)
1237 switch (sopt->sopt_name) {
1239 so->so_snd.sb_timeo = val;
1242 so->so_rcv.sb_timeo = val;
1247 error = ENOPROTOOPT;
1250 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1251 (void) ((*so->so_proto->pr_ctloutput)
1259 /* Helper routine for getsockopt */
1261 sooptcopyout(sopt, buf, len)
1262 struct sockopt *sopt;
1272 * Documented get behavior is that we always return a value,
1273 * possibly truncated to fit in the user's buffer.
1274 * Traditional behavior is that we always tell the user
1275 * precisely how much we copied, rather than something useful
1276 * like the total amount we had available for her.
1277 * Note that this interface is not idempotent; the entire answer must
1278 * generated ahead of time.
1280 valsize = min(len, sopt->sopt_valsize);
1281 sopt->sopt_valsize = valsize;
1282 if (sopt->sopt_val != 0) {
1283 if (sopt->sopt_p != 0)
1284 error = copyout(buf, sopt->sopt_val, valsize);
1286 bcopy(buf, sopt->sopt_val, valsize);
1294 struct sockopt *sopt;
1299 struct accept_filter_arg *afap;
1302 if (sopt->sopt_level != SOL_SOCKET) {
1303 if (so->so_proto && so->so_proto->pr_ctloutput) {
1304 return ((*so->so_proto->pr_ctloutput)
1307 return (ENOPROTOOPT);
1309 switch (sopt->sopt_name) {
1311 case SO_ACCEPTFILTER:
1312 if ((so->so_options & SO_ACCEPTCONN) == 0)
1314 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1316 bzero(afap, sizeof(*afap));
1317 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1318 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1319 if (so->so_accf->so_accept_filter_str != NULL)
1320 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1322 error = sooptcopyout(sopt, afap, sizeof(*afap));
1328 l.l_onoff = so->so_options & SO_LINGER;
1329 l.l_linger = so->so_linger;
1330 error = sooptcopyout(sopt, &l, sizeof l);
1333 case SO_USELOOPBACK:
1342 optval = so->so_options & sopt->sopt_name;
1344 error = sooptcopyout(sopt, &optval, sizeof optval);
1348 optval = so->so_type;
1352 optval = so->so_error;
1357 optval = so->so_snd.sb_hiwat;
1361 optval = so->so_rcv.sb_hiwat;
1365 optval = so->so_snd.sb_lowat;
1369 optval = so->so_rcv.sb_lowat;
1374 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1375 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1377 tv.tv_sec = optval / hz;
1378 tv.tv_usec = (optval % hz) * tick;
1379 error = sooptcopyout(sopt, &tv, sizeof tv);
1383 error = ENOPROTOOPT;
1390 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1392 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1394 struct mbuf *m, *m_prev;
1395 int sopt_size = sopt->sopt_valsize;
1397 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1400 if (sopt_size > MLEN) {
1401 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1402 if ((m->m_flags & M_EXT) == 0) {
1406 m->m_len = min(MCLBYTES, sopt_size);
1408 m->m_len = min(MLEN, sopt_size);
1410 sopt_size -= m->m_len;
1415 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1420 if (sopt_size > MLEN) {
1421 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1422 if ((m->m_flags & M_EXT) == 0) {
1426 m->m_len = min(MCLBYTES, sopt_size);
1428 m->m_len = min(MLEN, sopt_size);
1430 sopt_size -= m->m_len;
1437 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1439 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1441 struct mbuf *m0 = m;
1443 if (sopt->sopt_val == NULL)
1445 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1446 if (sopt->sopt_p != NULL) {
1449 error = copyin(sopt->sopt_val, mtod(m, char *),
1456 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1457 sopt->sopt_valsize -= m->m_len;
1458 (caddr_t)sopt->sopt_val += m->m_len;
1461 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1462 panic("ip6_sooptmcopyin");
1466 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1468 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1470 struct mbuf *m0 = m;
1473 if (sopt->sopt_val == NULL)
1475 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1476 if (sopt->sopt_p != NULL) {
1479 error = copyout(mtod(m, char *), sopt->sopt_val,
1486 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1487 sopt->sopt_valsize -= m->m_len;
1488 (caddr_t)sopt->sopt_val += m->m_len;
1489 valsize += m->m_len;
1493 /* enough soopt buffer should be given from user-land */
1497 sopt->sopt_valsize = valsize;
1503 register struct socket *so;
1505 if (so->so_sigio != NULL)
1506 pgsigio(so->so_sigio, SIGURG, 0);
1507 selwakeup(&so->so_rcv.sb_sel);
1511 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p)
1516 if (events & (POLLIN | POLLRDNORM))
1518 revents |= events & (POLLIN | POLLRDNORM);
1520 if (events & (POLLOUT | POLLWRNORM))
1521 if (sowriteable(so))
1522 revents |= events & (POLLOUT | POLLWRNORM);
1524 if (events & (POLLPRI | POLLRDBAND))
1525 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1526 revents |= events & (POLLPRI | POLLRDBAND);
1529 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1530 selrecord(p, &so->so_rcv.sb_sel);
1531 so->so_rcv.sb_flags |= SB_SEL;
1534 if (events & (POLLOUT | POLLWRNORM)) {
1535 selrecord(p, &so->so_snd.sb_sel);
1536 so->so_snd.sb_flags |= SB_SEL;
1545 sokqfilter(struct file *fp, struct knote *kn)
1547 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1551 switch (kn->kn_filter) {
1553 if (so->so_options & SO_ACCEPTCONN)
1554 kn->kn_fop = &solisten_filtops;
1556 kn->kn_fop = &soread_filtops;
1560 kn->kn_fop = &sowrite_filtops;
1568 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext);
1569 sb->sb_flags |= SB_KNOTE;
1575 filt_sordetach(struct knote *kn)
1577 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1580 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1581 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1582 so->so_rcv.sb_flags &= ~SB_KNOTE;
1588 filt_soread(struct knote *kn, long hint)
1590 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1592 kn->kn_data = so->so_rcv.sb_cc;
1593 if (so->so_state & SS_CANTRCVMORE) {
1594 kn->kn_flags |= EV_EOF;
1595 kn->kn_fflags = so->so_error;
1598 if (so->so_error) /* temporary udp error */
1600 if (kn->kn_sfflags & NOTE_LOWAT)
1601 return (kn->kn_data >= kn->kn_sdata);
1602 return (kn->kn_data >= so->so_rcv.sb_lowat);
1606 filt_sowdetach(struct knote *kn)
1608 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1611 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1612 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1613 so->so_snd.sb_flags &= ~SB_KNOTE;
1619 filt_sowrite(struct knote *kn, long hint)
1621 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1623 kn->kn_data = sbspace(&so->so_snd);
1624 if (so->so_state & SS_CANTSENDMORE) {
1625 kn->kn_flags |= EV_EOF;
1626 kn->kn_fflags = so->so_error;
1629 if (so->so_error) /* temporary udp error */
1631 if (((so->so_state & SS_ISCONNECTED) == 0) &&
1632 (so->so_proto->pr_flags & PR_CONNREQUIRED))
1634 if (kn->kn_sfflags & NOTE_LOWAT)
1635 return (kn->kn_data >= kn->kn_sdata);
1636 return (kn->kn_data >= so->so_snd.sb_lowat);
1641 filt_solisten(struct knote *kn, long hint)
1643 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1645 kn->kn_data = so->so_qlen;
1646 return (! TAILQ_EMPTY(&so->so_comp));