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
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/fcntl.h>
40 #include <sys/malloc.h>
42 #include <sys/domain.h>
43 #include <sys/file.h> /* for struct knote */
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/event.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/resourcevar.h>
53 #include <sys/signalvar.h>
54 #include <sys/sysctl.h>
57 #include <vm/vm_zone.h>
59 #include <machine/limits.h>
61 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
63 static int filt_sorattach(struct knote *kn);
64 static void filt_sordetach(struct knote *kn);
65 static int filt_soread(struct knote *kn, long hint);
66 static int filt_sowattach(struct knote *kn);
67 static void filt_sowdetach(struct knote *kn);
68 static int filt_sowrite(struct knote *kn, long hint);
69 static int filt_solisten(struct knote *kn, long hint);
71 static struct filterops solisten_filtops =
72 { 1, filt_sorattach, filt_sordetach, filt_solisten };
74 struct filterops so_rwfiltops[] = {
75 { 1, filt_sorattach, filt_sordetach, filt_soread },
76 { 1, filt_sowattach, filt_sowdetach, filt_sowrite },
79 struct vm_zone *socket_zone;
80 so_gen_t so_gencnt; /* generation count for sockets */
82 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
83 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
85 SYSCTL_DECL(_kern_ipc);
87 static int somaxconn = SOMAXCONN;
88 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
89 &somaxconn, 0, "Maximum pending socket connection queue size");
92 * Socket operation routines.
93 * These routines are called by the routines in
94 * sys_socket.c or from a system process, and
95 * implement the semantics of socket operations by
96 * switching out to the protocol specific routines.
100 * Get a socket structure from our zone, and initialize it.
101 * We don't implement `waitok' yet (see comments in uipc_domain.c).
102 * Note that it would probably be better to allocate socket
103 * and PCB at the same time, but I'm not convinced that all
104 * the protocols can be easily modified to do this.
112 so = zalloci(socket_zone);
114 /* XXX race condition for reentrant kernel */
115 bzero(so, sizeof *so);
116 so->so_gencnt = ++so_gencnt;
117 so->so_zone = socket_zone;
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_uid,
194 -(rlim_t)so->so_rcv.sb_hiwat, RLIM_INFINITY);
195 if (so->so_snd.sb_hiwat)
196 (void)chgsbsize(so->so_cred->cr_uid,
197 -(rlim_t)so->so_snd.sb_hiwat, RLIM_INFINITY);
198 if (so->so_accf != NULL) {
199 if (so->so_accf->so_accept_filter != NULL &&
200 so->so_accf->so_accept_filter->accf_destroy != NULL) {
201 so->so_accf->so_accept_filter->accf_destroy(so);
203 if (so->so_accf->so_accept_filter_str != NULL)
204 FREE(so->so_accf->so_accept_filter_str, M_ACCF);
205 FREE(so->so_accf, M_ACCF);
208 zfreei(so->so_zone, so);
212 solisten(so, backlog, p)
213 register struct socket *so;
220 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p);
225 if (TAILQ_EMPTY(&so->so_comp))
226 so->so_options |= SO_ACCEPTCONN;
227 if (backlog < 0 || backlog > somaxconn)
229 so->so_qlimit = backlog;
236 register struct socket *so;
238 struct socket *head = so->so_head;
240 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
243 if (so->so_state & SS_INCOMP) {
244 TAILQ_REMOVE(&head->so_incomp, so, so_list);
246 } else if (so->so_state & SS_COMP) {
248 * We must not decommission a socket that's
249 * on the accept(2) queue. If we do, then
250 * accept(2) may hang after select(2) indicated
251 * that the listening socket was ready.
255 panic("sofree: not queued");
258 so->so_state &= ~SS_INCOMP;
261 sbrelease(&so->so_snd, so);
267 * Close a socket on last file table reference removal.
268 * Initiate disconnect if connected.
269 * Free socket when disconnect complete.
273 register struct socket *so;
275 int s = splnet(); /* conservative */
278 funsetown(so->so_sigio);
279 if (so->so_options & SO_ACCEPTCONN) {
280 struct socket *sp, *sonext;
282 sp = TAILQ_FIRST(&so->so_incomp);
283 for (; sp != NULL; sp = sonext) {
284 sonext = TAILQ_NEXT(sp, so_list);
287 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
288 sonext = TAILQ_NEXT(sp, so_list);
289 /* Dequeue from so_comp since sofree() won't do it */
290 TAILQ_REMOVE(&so->so_comp, sp, so_list);
292 sp->so_state &= ~SS_COMP;
299 if (so->so_state & SS_ISCONNECTED) {
300 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
301 error = sodisconnect(so);
305 if (so->so_options & SO_LINGER) {
306 if ((so->so_state & SS_ISDISCONNECTING) &&
307 (so->so_state & SS_NBIO))
309 while (so->so_state & SS_ISCONNECTED) {
310 error = tsleep((caddr_t)&so->so_timeo,
311 PSOCK | PCATCH, "soclos", so->so_linger * hz);
319 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
324 if (so->so_state & SS_NOFDREF)
325 panic("soclose: NOFDREF");
326 so->so_state |= SS_NOFDREF;
333 * Must be called at splnet...
341 error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
351 register struct socket *so;
352 struct sockaddr **nam;
357 if ((so->so_state & SS_NOFDREF) == 0)
358 panic("soaccept: !NOFDREF");
359 so->so_state &= ~SS_NOFDREF;
360 if ((so->so_state & SS_ISDISCONNECTED) == 0)
361 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
372 soconnect(so, nam, p)
373 register struct socket *so;
374 struct sockaddr *nam;
380 if (so->so_options & SO_ACCEPTCONN)
384 * If protocol is connection-based, can only connect once.
385 * Otherwise, if connected, try to disconnect first.
386 * This allows user to disconnect by connecting to, e.g.,
389 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
390 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
391 (error = sodisconnect(so))))
394 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p);
401 register struct socket *so1;
407 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
414 register struct socket *so;
419 if ((so->so_state & SS_ISCONNECTED) == 0) {
423 if (so->so_state & SS_ISDISCONNECTING) {
427 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
433 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
436 * If send must go all at once and message is larger than
437 * send buffering, then hard error.
438 * Lock against other senders.
439 * If must go all at once and not enough room now, then
440 * inform user that this would block and do nothing.
441 * Otherwise, if nonblocking, send as much as possible.
442 * The data to be sent is described by "uio" if nonzero,
443 * otherwise by the mbuf chain "top" (which must be null
444 * if uio is not). Data provided in mbuf chain must be small
445 * enough to send all at once.
447 * Returns nonzero on error, timeout or signal; callers
448 * must check for short counts if EINTR/ERESTART are returned.
449 * Data and control buffers are freed on return.
452 sosend(so, addr, uio, top, control, flags, p)
453 register struct socket *so;
454 struct sockaddr *addr;
457 struct mbuf *control;
462 register struct mbuf *m;
463 register long space, len, resid;
464 int clen = 0, error, s, dontroute, mlen;
465 int atomic = sosendallatonce(so) || top;
468 resid = uio->uio_resid;
470 resid = top->m_pkthdr.len;
472 * In theory resid should be unsigned.
473 * However, space must be signed, as it might be less than 0
474 * if we over-committed, and we must use a signed comparison
475 * of space and resid. On the other hand, a negative resid
476 * causes us to loop sending 0-length segments to the protocol.
478 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
479 * type sockets since that's an error.
481 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
487 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
488 (so->so_proto->pr_flags & PR_ATOMIC);
490 p->p_stats->p_ru.ru_msgsnd++;
492 clen = control->m_len;
493 #define snderr(errno) { error = errno; splx(s); goto release; }
496 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
501 if (so->so_state & SS_CANTSENDMORE)
504 error = so->so_error;
509 if ((so->so_state & SS_ISCONNECTED) == 0) {
511 * `sendto' and `sendmsg' is allowed on a connection-
512 * based socket if it supports implied connect.
513 * Return ENOTCONN if not connected and no address is
516 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
517 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
518 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
519 !(resid == 0 && clen != 0))
521 } else if (addr == 0)
522 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
523 ENOTCONN : EDESTADDRREQ);
525 space = sbspace(&so->so_snd);
528 if ((atomic && resid > so->so_snd.sb_hiwat) ||
529 clen > so->so_snd.sb_hiwat)
531 if (space < resid + clen && uio &&
532 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
533 if (so->so_state & SS_NBIO)
535 sbunlock(&so->so_snd);
536 error = sbwait(&so->so_snd);
548 * Data is prepackaged in "top".
552 top->m_flags |= M_EOR;
555 MGETHDR(m, M_WAIT, MT_DATA);
562 m->m_pkthdr.rcvif = (struct ifnet *)0;
564 MGET(m, M_WAIT, MT_DATA);
571 if (resid >= MINCLSIZE) {
573 if ((m->m_flags & M_EXT) == 0)
576 len = min(min(mlen, resid), space);
579 len = min(min(mlen, resid), space);
581 * For datagram protocols, leave room
582 * for protocol headers in first mbuf.
584 if (atomic && top == 0 && len < mlen)
588 error = uiomove(mtod(m, caddr_t), (int)len, uio);
589 resid = uio->uio_resid;
592 top->m_pkthdr.len += len;
598 top->m_flags |= M_EOR;
601 } while (space > 0 && atomic);
603 so->so_options |= SO_DONTROUTE;
604 s = splnet(); /* XXX */
606 * XXX all the SS_CANTSENDMORE checks previously
607 * done could be out of date. We could have recieved
608 * a reset packet in an interrupt or maybe we slept
609 * while doing page faults in uiomove() etc. We could
610 * probably recheck again inside the splnet() protection
611 * here, but there are probably other places that this
612 * also happens. We must rethink this.
614 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
615 (flags & MSG_OOB) ? PRUS_OOB :
617 * If the user set MSG_EOF, the protocol
618 * understands this flag and nothing left to
619 * send then use PRU_SEND_EOF instead of PRU_SEND.
621 ((flags & MSG_EOF) &&
622 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
625 /* If there is more to send set PRUS_MORETOCOME */
626 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
627 top, addr, control, p);
630 so->so_options &= ~SO_DONTROUTE;
637 } while (resid && space > 0);
641 sbunlock(&so->so_snd);
651 * Implement receive operations on a socket.
652 * We depend on the way that records are added to the sockbuf
653 * by sbappend*. In particular, each record (mbufs linked through m_next)
654 * must begin with an address if the protocol so specifies,
655 * followed by an optional mbuf or mbufs containing ancillary data,
656 * and then zero or more mbufs of data.
657 * In order to avoid blocking network interrupts for the entire time here,
658 * we splx() while doing the actual copy to user space.
659 * Although the sockbuf is locked, new data may still be appended,
660 * and thus we must maintain consistency of the sockbuf during that time.
662 * The caller may receive the data as a single mbuf chain by supplying
663 * an mbuf **mp0 for use in returning the chain. The uio is then used
664 * only for the count in uio_resid.
667 soreceive(so, psa, uio, mp0, controlp, flagsp)
668 register struct socket *so;
669 struct sockaddr **psa;
672 struct mbuf **controlp;
675 register struct mbuf *m, **mp;
676 register int flags, len, error, s, offset;
677 struct protosw *pr = so->so_proto;
678 struct mbuf *nextrecord;
680 int orig_resid = uio->uio_resid;
688 flags = *flagsp &~ MSG_EOR;
691 if (flags & MSG_OOB) {
692 m = m_get(M_WAIT, MT_DATA);
695 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
699 error = uiomove(mtod(m, caddr_t),
700 (int) min(uio->uio_resid, m->m_len), uio);
702 } while (uio->uio_resid && error == 0 && m);
709 *mp = (struct mbuf *)0;
710 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
711 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
714 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
719 m = so->so_rcv.sb_mb;
721 * If we have less data than requested, block awaiting more
722 * (subject to any timeout) if:
723 * 1. the current count is less than the low water mark, or
724 * 2. MSG_WAITALL is set, and it is possible to do the entire
725 * receive operation at once if we block (resid <= hiwat).
726 * 3. MSG_DONTWAIT is not set
727 * If MSG_WAITALL is set but resid is larger than the receive buffer,
728 * we have to do the receive in sections, and thus risk returning
729 * a short count if a timeout or signal occurs after we start.
731 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
732 so->so_rcv.sb_cc < uio->uio_resid) &&
733 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
734 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
735 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
736 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
740 error = so->so_error;
741 if ((flags & MSG_PEEK) == 0)
745 if (so->so_state & SS_CANTRCVMORE) {
751 for (; m; m = m->m_next)
752 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
753 m = so->so_rcv.sb_mb;
756 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
757 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
761 if (uio->uio_resid == 0)
763 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
767 sbunlock(&so->so_rcv);
768 error = sbwait(&so->so_rcv);
776 uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
777 nextrecord = m->m_nextpkt;
778 if (pr->pr_flags & PR_ADDR) {
779 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
782 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
784 if (flags & MSG_PEEK) {
787 sbfree(&so->so_rcv, m);
788 MFREE(m, so->so_rcv.sb_mb);
789 m = so->so_rcv.sb_mb;
792 while (m && m->m_type == MT_CONTROL && error == 0) {
793 if (flags & MSG_PEEK) {
795 *controlp = m_copy(m, 0, m->m_len);
798 sbfree(&so->so_rcv, m);
800 if (pr->pr_domain->dom_externalize &&
801 mtod(m, struct cmsghdr *)->cmsg_type ==
803 error = (*pr->pr_domain->dom_externalize)(m);
805 so->so_rcv.sb_mb = m->m_next;
807 m = so->so_rcv.sb_mb;
809 MFREE(m, so->so_rcv.sb_mb);
810 m = so->so_rcv.sb_mb;
815 controlp = &(*controlp)->m_next;
819 if ((flags & MSG_PEEK) == 0)
820 m->m_nextpkt = nextrecord;
822 if (type == MT_OOBDATA)
827 while (m && uio->uio_resid > 0 && error == 0) {
828 if (m->m_type == MT_OOBDATA) {
829 if (type != MT_OOBDATA)
831 } else if (type == MT_OOBDATA)
834 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
836 so->so_state &= ~SS_RCVATMARK;
837 len = uio->uio_resid;
838 if (so->so_oobmark && len > so->so_oobmark - offset)
839 len = so->so_oobmark - offset;
840 if (len > m->m_len - moff)
841 len = m->m_len - moff;
843 * If mp is set, just pass back the mbufs.
844 * Otherwise copy them out via the uio, then free.
845 * Sockbuf must be consistent here (points to current mbuf,
846 * it points to next record) when we drop priority;
847 * we must note any additions to the sockbuf when we
848 * block interrupts again.
852 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
857 uio->uio_resid -= len;
858 if (len == m->m_len - moff) {
859 if (m->m_flags & M_EOR)
861 if (flags & MSG_PEEK) {
865 nextrecord = m->m_nextpkt;
866 sbfree(&so->so_rcv, m);
870 so->so_rcv.sb_mb = m = m->m_next;
871 *mp = (struct mbuf *)0;
873 MFREE(m, so->so_rcv.sb_mb);
874 m = so->so_rcv.sb_mb;
877 m->m_nextpkt = nextrecord;
880 if (flags & MSG_PEEK)
884 *mp = m_copym(m, 0, len, M_WAIT);
887 so->so_rcv.sb_cc -= len;
890 if (so->so_oobmark) {
891 if ((flags & MSG_PEEK) == 0) {
892 so->so_oobmark -= len;
893 if (so->so_oobmark == 0) {
894 so->so_state |= SS_RCVATMARK;
899 if (offset == so->so_oobmark)
906 * If the MSG_WAITALL flag is set (for non-atomic socket),
907 * we must not quit until "uio->uio_resid == 0" or an error
908 * termination. If a signal/timeout occurs, return
909 * with a short count but without error.
910 * Keep sockbuf locked against other readers.
912 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
913 !sosendallatonce(so) && !nextrecord) {
914 if (so->so_error || so->so_state & SS_CANTRCVMORE)
916 error = sbwait(&so->so_rcv);
918 sbunlock(&so->so_rcv);
922 m = so->so_rcv.sb_mb;
924 nextrecord = m->m_nextpkt;
928 if (m && pr->pr_flags & PR_ATOMIC) {
930 if ((flags & MSG_PEEK) == 0)
931 (void) sbdroprecord(&so->so_rcv);
933 if ((flags & MSG_PEEK) == 0) {
935 so->so_rcv.sb_mb = nextrecord;
936 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
937 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
939 if (orig_resid == uio->uio_resid && orig_resid &&
940 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
941 sbunlock(&so->so_rcv);
949 sbunlock(&so->so_rcv);
956 register struct socket *so;
959 register struct protosw *pr = so->so_proto;
965 return ((*pr->pr_usrreqs->pru_shutdown)(so));
971 register struct socket *so;
973 register struct sockbuf *sb = &so->so_rcv;
974 register struct protosw *pr = so->so_proto;
978 sb->sb_flags |= SB_NOINTR;
979 (void) sblock(sb, M_WAITOK);
984 bzero((caddr_t)sb, sizeof (*sb));
986 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
987 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
992 do_setopt_accept_filter(so, sopt)
994 struct sockopt *sopt;
996 struct accept_filter_arg *afap = NULL;
997 struct accept_filter *afp;
998 struct so_accf *af = so->so_accf;
1001 /* do not set/remove accept filters on non listen sockets */
1002 if ((so->so_options & SO_ACCEPTCONN) == 0) {
1007 /* removing the filter */
1010 if (af->so_accept_filter != NULL &&
1011 af->so_accept_filter->accf_destroy != NULL) {
1012 af->so_accept_filter->accf_destroy(so);
1014 if (af->so_accept_filter_str != NULL) {
1015 FREE(af->so_accept_filter_str, M_ACCF);
1020 so->so_options &= ~SO_ACCEPTFILTER;
1023 /* adding a filter */
1024 /* must remove previous filter first */
1029 /* don't put large objects on the kernel stack */
1030 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1031 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1032 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1033 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1036 afp = accept_filt_get(afap->af_name);
1041 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK);
1042 bzero(af, sizeof(*af));
1043 if (afp->accf_create != NULL) {
1044 if (afap->af_name[0] != '\0') {
1045 int len = strlen(afap->af_name) + 1;
1047 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1048 strcpy(af->so_accept_filter_str, afap->af_name);
1050 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1051 if (af->so_accept_filter_arg == NULL) {
1052 FREE(af->so_accept_filter_str, M_ACCF);
1059 af->so_accept_filter = afp;
1061 so->so_options |= SO_ACCEPTFILTER;
1069 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1070 * an additional variant to handle the case where the option value needs
1071 * to be some kind of integer, but not a specific size.
1072 * In addition to their use here, these functions are also called by the
1073 * protocol-level pr_ctloutput() routines.
1076 sooptcopyin(sopt, buf, len, minlen)
1077 struct sockopt *sopt;
1085 * If the user gives us more than we wanted, we ignore it,
1086 * but if we don't get the minimum length the caller
1087 * wants, we return EINVAL. On success, sopt->sopt_valsize
1088 * is set to however much we actually retrieved.
1090 if ((valsize = sopt->sopt_valsize) < minlen)
1093 sopt->sopt_valsize = valsize = len;
1095 if (sopt->sopt_p != 0)
1096 return (copyin(sopt->sopt_val, buf, valsize));
1098 bcopy(sopt->sopt_val, buf, valsize);
1105 struct sockopt *sopt;
1113 if (sopt->sopt_level != SOL_SOCKET) {
1114 if (so->so_proto && so->so_proto->pr_ctloutput)
1115 return ((*so->so_proto->pr_ctloutput)
1117 error = ENOPROTOOPT;
1119 switch (sopt->sopt_name) {
1121 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1125 so->so_linger = l.l_linger;
1127 so->so_options |= SO_LINGER;
1129 so->so_options &= ~SO_LINGER;
1135 case SO_USELOOPBACK:
1141 error = sooptcopyin(sopt, &optval, sizeof optval,
1146 so->so_options |= sopt->sopt_name;
1148 so->so_options &= ~sopt->sopt_name;
1155 error = sooptcopyin(sopt, &optval, sizeof optval,
1161 * Values < 1 make no sense for any of these
1162 * options, so disallow them.
1169 switch (sopt->sopt_name) {
1172 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1173 &so->so_snd : &so->so_rcv, (u_long)optval,
1174 so, curproc) == 0) {
1181 * Make sure the low-water is never greater than
1185 so->so_snd.sb_lowat =
1186 (optval > so->so_snd.sb_hiwat) ?
1187 so->so_snd.sb_hiwat : optval;
1190 so->so_rcv.sb_lowat =
1191 (optval > so->so_rcv.sb_hiwat) ?
1192 so->so_rcv.sb_hiwat : optval;
1199 error = sooptcopyin(sopt, &tv, sizeof tv,
1204 /* assert(hz > 0); */
1205 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1206 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1210 /* assert(tick > 0); */
1211 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1212 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1213 if (val > SHRT_MAX) {
1218 switch (sopt->sopt_name) {
1220 so->so_snd.sb_timeo = val;
1223 so->so_rcv.sb_timeo = val;
1228 case SO_ACCEPTFILTER:
1229 error = do_setopt_accept_filter(so, sopt);
1234 error = ENOPROTOOPT;
1237 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1238 (void) ((*so->so_proto->pr_ctloutput)
1246 /* Helper routine for getsockopt */
1248 sooptcopyout(sopt, buf, len)
1249 struct sockopt *sopt;
1259 * Documented get behavior is that we always return a value,
1260 * possibly truncated to fit in the user's buffer.
1261 * Traditional behavior is that we always tell the user
1262 * precisely how much we copied, rather than something useful
1263 * like the total amount we had available for her.
1264 * Note that this interface is not idempotent; the entire answer must
1265 * generated ahead of time.
1267 valsize = min(len, sopt->sopt_valsize);
1268 sopt->sopt_valsize = valsize;
1269 if (sopt->sopt_val != 0) {
1270 if (sopt->sopt_p != 0)
1271 error = copyout(buf, sopt->sopt_val, valsize);
1273 bcopy(buf, sopt->sopt_val, valsize);
1281 struct sockopt *sopt;
1286 struct accept_filter_arg *afap;
1289 if (sopt->sopt_level != SOL_SOCKET) {
1290 if (so->so_proto && so->so_proto->pr_ctloutput) {
1291 return ((*so->so_proto->pr_ctloutput)
1294 return (ENOPROTOOPT);
1296 switch (sopt->sopt_name) {
1297 case SO_ACCEPTFILTER:
1298 if ((so->so_options & SO_ACCEPTCONN) == 0)
1300 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1302 bzero(afap, sizeof(*afap));
1303 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1304 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1305 if (so->so_accf->so_accept_filter_str != NULL)
1306 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1308 error = sooptcopyout(sopt, afap, sizeof(*afap));
1313 l.l_onoff = so->so_options & SO_LINGER;
1314 l.l_linger = so->so_linger;
1315 error = sooptcopyout(sopt, &l, sizeof l);
1318 case SO_USELOOPBACK:
1327 optval = so->so_options & sopt->sopt_name;
1329 error = sooptcopyout(sopt, &optval, sizeof optval);
1333 optval = so->so_type;
1337 optval = so->so_error;
1342 optval = so->so_snd.sb_hiwat;
1346 optval = so->so_rcv.sb_hiwat;
1350 optval = so->so_snd.sb_lowat;
1354 optval = so->so_rcv.sb_lowat;
1359 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1360 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1362 tv.tv_sec = optval / hz;
1363 tv.tv_usec = (optval % hz) * tick;
1364 error = sooptcopyout(sopt, &tv, sizeof tv);
1368 error = ENOPROTOOPT;
1375 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1377 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1379 struct mbuf *m, *m_prev;
1380 int sopt_size = sopt->sopt_valsize;
1382 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1385 if (sopt_size > MLEN) {
1386 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1387 if ((m->m_flags & M_EXT) == 0) {
1391 m->m_len = min(MCLBYTES, sopt_size);
1393 m->m_len = min(MLEN, sopt_size);
1395 sopt_size -= m->m_len;
1400 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1405 if (sopt_size > MLEN) {
1406 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1407 if ((m->m_flags & M_EXT) == 0) {
1411 m->m_len = min(MCLBYTES, sopt_size);
1413 m->m_len = min(MLEN, sopt_size);
1415 sopt_size -= m->m_len;
1422 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1424 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1426 struct mbuf *m0 = m;
1428 if (sopt->sopt_val == NULL)
1430 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1431 if (sopt->sopt_p != NULL) {
1434 error = copyin(sopt->sopt_val, mtod(m, char *),
1441 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1442 sopt->sopt_valsize -= m->m_len;
1443 (caddr_t)sopt->sopt_val += m->m_len;
1446 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1447 panic("ip6_sooptmcopyin");
1451 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1453 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1455 struct mbuf *m0 = m;
1458 if (sopt->sopt_val == NULL)
1460 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1461 if (sopt->sopt_p != NULL) {
1464 error = copyout(mtod(m, char *), sopt->sopt_val,
1471 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1472 sopt->sopt_valsize -= m->m_len;
1473 (caddr_t)sopt->sopt_val += m->m_len;
1474 valsize += m->m_len;
1478 /* enough soopt buffer should be given from user-land */
1482 sopt->sopt_valsize = valsize;
1488 register struct socket *so;
1490 if (so->so_sigio != NULL)
1491 pgsigio(so->so_sigio, SIGURG, 0);
1492 selwakeup(&so->so_rcv.sb_sel);
1496 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p)
1501 if (events & (POLLIN | POLLRDNORM))
1503 revents |= events & (POLLIN | POLLRDNORM);
1505 if (events & (POLLOUT | POLLWRNORM))
1506 if (sowriteable(so))
1507 revents |= events & (POLLOUT | POLLWRNORM);
1509 if (events & (POLLPRI | POLLRDBAND))
1510 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1511 revents |= events & (POLLPRI | POLLRDBAND);
1514 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1515 selrecord(p, &so->so_rcv.sb_sel);
1516 so->so_rcv.sb_flags |= SB_SEL;
1519 if (events & (POLLOUT | POLLWRNORM)) {
1520 selrecord(p, &so->so_snd.sb_sel);
1521 so->so_snd.sb_flags |= SB_SEL;
1530 filt_sorattach(struct knote *kn)
1532 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1535 if (so->so_options & SO_ACCEPTCONN)
1536 kn->kn_fop = &solisten_filtops;
1537 SLIST_INSERT_HEAD(&so->so_rcv.sb_sel.si_note, kn, kn_selnext);
1538 so->so_rcv.sb_flags |= SB_KNOTE;
1544 filt_sordetach(struct knote *kn)
1546 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1549 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1550 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1551 so->so_rcv.sb_flags &= ~SB_KNOTE;
1557 filt_soread(struct knote *kn, long hint)
1559 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1561 kn->kn_data = so->so_rcv.sb_cc;
1562 if (so->so_state & SS_CANTRCVMORE) {
1563 kn->kn_flags |= EV_EOF;
1566 return (kn->kn_data > 0);
1570 filt_sowattach(struct knote *kn)
1572 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1575 SLIST_INSERT_HEAD(&so->so_snd.sb_sel.si_note, kn, kn_selnext);
1576 so->so_snd.sb_flags |= SB_KNOTE;
1582 filt_sowdetach(struct knote *kn)
1584 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1587 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1588 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1589 so->so_snd.sb_flags &= ~SB_KNOTE;
1595 filt_sowrite(struct knote *kn, long hint)
1597 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1599 kn->kn_data = sbspace(&so->so_snd);
1600 if (so->so_state & SS_CANTSENDMORE) {
1601 kn->kn_flags |= EV_EOF;
1604 if (((so->so_state & SS_ISCONNECTED) == 0) &&
1605 (so->so_proto->pr_flags & PR_CONNREQUIRED))
1607 return (kn->kn_data >= so->so_snd.sb_lowat);
1612 filt_solisten(struct knote *kn, long hint)
1614 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1616 kn->kn_data = so->so_qlen - so->so_incqlen;
1617 return (! TAILQ_EMPTY(&so->so_comp));