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
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 = zalloc(socket_zone);
115 /* XXX race condition for reentrant kernel */
116 bzero(so, sizeof *so);
117 so->so_gencnt = ++so_gencnt;
118 so->so_zone = socket_zone;
119 TAILQ_INIT(&so->so_aiojobq);
125 socreate(dom, aso, type, proto, p)
132 register struct protosw *prp;
133 register struct socket *so;
137 prp = pffindproto(dom, proto, type);
139 prp = pffindtype(dom, type);
141 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
142 return (EPROTONOSUPPORT);
144 if (p->p_prison && jail_socket_unixiproute_only &&
145 prp->pr_domain->dom_family != PF_LOCAL &&
146 prp->pr_domain->dom_family != PF_INET &&
147 prp->pr_domain->dom_family != PF_ROUTE) {
148 return (EPROTONOSUPPORT);
151 if (prp->pr_type != type)
153 so = soalloc(p != 0);
157 TAILQ_INIT(&so->so_incomp);
158 TAILQ_INIT(&so->so_comp);
160 so->so_cred = p->p_ucred;
163 error = (*prp->pr_usrreqs->pru_attach)(so, proto, p);
165 so->so_state |= SS_NOFDREF;
176 struct sockaddr *nam;
182 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
192 so->so_gencnt = ++so_gencnt;
193 if (so->so_rcv.sb_hiwat)
194 (void)chgsbsize(so->so_cred->cr_uidinfo,
195 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
196 if (so->so_snd.sb_hiwat)
197 (void)chgsbsize(so->so_cred->cr_uidinfo,
198 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
200 if (so->so_accf != NULL) {
201 if (so->so_accf->so_accept_filter != NULL &&
202 so->so_accf->so_accept_filter->accf_destroy != NULL) {
203 so->so_accf->so_accept_filter->accf_destroy(so);
205 if (so->so_accf->so_accept_filter_str != NULL)
206 FREE(so->so_accf->so_accept_filter_str, M_ACCF);
207 FREE(so->so_accf, M_ACCF);
211 zfree(so->so_zone, so);
215 solisten(so, backlog, p)
216 register struct socket *so;
223 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p);
228 if (TAILQ_EMPTY(&so->so_comp))
229 so->so_options |= SO_ACCEPTCONN;
230 if (backlog < 0 || backlog > somaxconn)
232 so->so_qlimit = backlog;
239 register struct socket *so;
241 struct socket *head = so->so_head;
243 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
246 if (so->so_state & SS_INCOMP) {
247 TAILQ_REMOVE(&head->so_incomp, so, so_list);
249 } else if (so->so_state & SS_COMP) {
251 * We must not decommission a socket that's
252 * on the accept(2) queue. If we do, then
253 * accept(2) may hang after select(2) indicated
254 * that the listening socket was ready.
258 panic("sofree: not queued");
261 so->so_state &= ~SS_INCOMP;
264 sbrelease(&so->so_snd, so);
270 * Close a socket on last file table reference removal.
271 * Initiate disconnect if connected.
272 * Free socket when disconnect complete.
276 register struct socket *so;
278 int s = splnet(); /* conservative */
281 funsetown(so->so_sigio);
282 if (so->so_options & SO_ACCEPTCONN) {
283 struct socket *sp, *sonext;
285 sp = TAILQ_FIRST(&so->so_incomp);
286 for (; sp != NULL; sp = sonext) {
287 sonext = TAILQ_NEXT(sp, so_list);
290 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
291 sonext = TAILQ_NEXT(sp, so_list);
292 /* Dequeue from so_comp since sofree() won't do it */
293 TAILQ_REMOVE(&so->so_comp, sp, so_list);
295 sp->so_state &= ~SS_COMP;
302 if (so->so_state & SS_ISCONNECTED) {
303 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
304 error = sodisconnect(so);
308 if (so->so_options & SO_LINGER) {
309 if ((so->so_state & SS_ISDISCONNECTING) &&
310 (so->so_state & SS_NBIO))
312 while (so->so_state & SS_ISCONNECTED) {
313 error = tsleep((caddr_t)&so->so_timeo,
314 PSOCK | PCATCH, "soclos", so->so_linger * hz);
322 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
327 if (so->so_state & SS_NOFDREF)
328 panic("soclose: NOFDREF");
329 so->so_state |= SS_NOFDREF;
336 * Must be called at splnet...
344 error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
354 register struct socket *so;
355 struct sockaddr **nam;
360 if ((so->so_state & SS_NOFDREF) == 0)
361 panic("soaccept: !NOFDREF");
362 so->so_state &= ~SS_NOFDREF;
363 if ((so->so_state & SS_ISDISCONNECTED) == 0)
364 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
366 error = ECONNABORTED;
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_TRYWAIT, MT_DATA);
562 m->m_pkthdr.rcvif = (struct ifnet *)0;
564 MGET(m, M_TRYWAIT, MT_DATA);
571 if (resid >= MINCLSIZE) {
572 MCLGET(m, M_TRYWAIT);
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_TRYWAIT, 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_TRYWAIT);
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);
993 do_setopt_accept_filter(so, sopt)
995 struct sockopt *sopt;
997 struct accept_filter_arg *afap = NULL;
998 struct accept_filter *afp;
999 struct so_accf *af = so->so_accf;
1002 /* do not set/remove accept filters on non listen sockets */
1003 if ((so->so_options & SO_ACCEPTCONN) == 0) {
1008 /* removing the filter */
1011 if (af->so_accept_filter != NULL &&
1012 af->so_accept_filter->accf_destroy != NULL) {
1013 af->so_accept_filter->accf_destroy(so);
1015 if (af->so_accept_filter_str != NULL) {
1016 FREE(af->so_accept_filter_str, M_ACCF);
1021 so->so_options &= ~SO_ACCEPTFILTER;
1024 /* adding a filter */
1025 /* must remove previous filter first */
1030 /* don't put large objects on the kernel stack */
1031 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1032 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1033 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1034 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1037 afp = accept_filt_get(afap->af_name);
1042 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK | M_ZERO);
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;
1070 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1071 * an additional variant to handle the case where the option value needs
1072 * to be some kind of integer, but not a specific size.
1073 * In addition to their use here, these functions are also called by the
1074 * protocol-level pr_ctloutput() routines.
1077 sooptcopyin(sopt, buf, len, minlen)
1078 struct sockopt *sopt;
1086 * If the user gives us more than we wanted, we ignore it,
1087 * but if we don't get the minimum length the caller
1088 * wants, we return EINVAL. On success, sopt->sopt_valsize
1089 * is set to however much we actually retrieved.
1091 if ((valsize = sopt->sopt_valsize) < minlen)
1094 sopt->sopt_valsize = valsize = len;
1096 if (sopt->sopt_p != 0)
1097 return (copyin(sopt->sopt_val, buf, valsize));
1099 bcopy(sopt->sopt_val, buf, valsize);
1106 struct sockopt *sopt;
1114 if (sopt->sopt_level != SOL_SOCKET) {
1115 if (so->so_proto && so->so_proto->pr_ctloutput)
1116 return ((*so->so_proto->pr_ctloutput)
1118 error = ENOPROTOOPT;
1120 switch (sopt->sopt_name) {
1122 case SO_ACCEPTFILTER:
1123 error = do_setopt_accept_filter(so, sopt);
1129 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1133 so->so_linger = l.l_linger;
1135 so->so_options |= SO_LINGER;
1137 so->so_options &= ~SO_LINGER;
1143 case SO_USELOOPBACK:
1149 error = sooptcopyin(sopt, &optval, sizeof optval,
1154 so->so_options |= sopt->sopt_name;
1156 so->so_options &= ~sopt->sopt_name;
1163 error = sooptcopyin(sopt, &optval, sizeof optval,
1169 * Values < 1 make no sense for any of these
1170 * options, so disallow them.
1177 switch (sopt->sopt_name) {
1180 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1181 &so->so_snd : &so->so_rcv, (u_long)optval,
1182 so, curproc) == 0) {
1189 * Make sure the low-water is never greater than
1193 so->so_snd.sb_lowat =
1194 (optval > so->so_snd.sb_hiwat) ?
1195 so->so_snd.sb_hiwat : optval;
1198 so->so_rcv.sb_lowat =
1199 (optval > so->so_rcv.sb_hiwat) ?
1200 so->so_rcv.sb_hiwat : optval;
1207 error = sooptcopyin(sopt, &tv, sizeof tv,
1212 /* assert(hz > 0); */
1213 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1214 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1218 /* assert(tick > 0); */
1219 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1220 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1221 if (val > SHRT_MAX) {
1226 switch (sopt->sopt_name) {
1228 so->so_snd.sb_timeo = val;
1231 so->so_rcv.sb_timeo = val;
1236 error = ENOPROTOOPT;
1239 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1240 (void) ((*so->so_proto->pr_ctloutput)
1248 /* Helper routine for getsockopt */
1250 sooptcopyout(sopt, buf, len)
1251 struct sockopt *sopt;
1261 * Documented get behavior is that we always return a value,
1262 * possibly truncated to fit in the user's buffer.
1263 * Traditional behavior is that we always tell the user
1264 * precisely how much we copied, rather than something useful
1265 * like the total amount we had available for her.
1266 * Note that this interface is not idempotent; the entire answer must
1267 * generated ahead of time.
1269 valsize = min(len, sopt->sopt_valsize);
1270 sopt->sopt_valsize = valsize;
1271 if (sopt->sopt_val != 0) {
1272 if (sopt->sopt_p != 0)
1273 error = copyout(buf, sopt->sopt_val, valsize);
1275 bcopy(buf, sopt->sopt_val, valsize);
1283 struct sockopt *sopt;
1289 struct accept_filter_arg *afap;
1293 if (sopt->sopt_level != SOL_SOCKET) {
1294 if (so->so_proto && so->so_proto->pr_ctloutput) {
1295 return ((*so->so_proto->pr_ctloutput)
1298 return (ENOPROTOOPT);
1300 switch (sopt->sopt_name) {
1302 case SO_ACCEPTFILTER:
1303 if ((so->so_options & SO_ACCEPTCONN) == 0)
1305 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1306 M_TEMP, M_WAITOK | M_ZERO);
1307 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1308 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1309 if (so->so_accf->so_accept_filter_str != NULL)
1310 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1312 error = sooptcopyout(sopt, afap, sizeof(*afap));
1318 l.l_onoff = so->so_options & SO_LINGER;
1319 l.l_linger = so->so_linger;
1320 error = sooptcopyout(sopt, &l, sizeof l);
1323 case SO_USELOOPBACK:
1332 optval = so->so_options & sopt->sopt_name;
1334 error = sooptcopyout(sopt, &optval, sizeof optval);
1338 optval = so->so_type;
1342 optval = so->so_error;
1347 optval = so->so_snd.sb_hiwat;
1351 optval = so->so_rcv.sb_hiwat;
1355 optval = so->so_snd.sb_lowat;
1359 optval = so->so_rcv.sb_lowat;
1364 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1365 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1367 tv.tv_sec = optval / hz;
1368 tv.tv_usec = (optval % hz) * tick;
1369 error = sooptcopyout(sopt, &tv, sizeof tv);
1373 error = ENOPROTOOPT;
1380 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1382 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1384 struct mbuf *m, *m_prev;
1385 int sopt_size = sopt->sopt_valsize;
1387 MGET(m, sopt->sopt_p ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
1390 if (sopt_size > MLEN) {
1391 MCLGET(m, sopt->sopt_p ? M_TRYWAIT : M_DONTWAIT);
1392 if ((m->m_flags & M_EXT) == 0) {
1396 m->m_len = min(MCLBYTES, sopt_size);
1398 m->m_len = min(MLEN, sopt_size);
1400 sopt_size -= m->m_len;
1405 MGET(m, sopt->sopt_p ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
1410 if (sopt_size > MLEN) {
1411 MCLGET(m, sopt->sopt_p ? M_TRYWAIT : M_DONTWAIT);
1412 if ((m->m_flags & M_EXT) == 0) {
1416 m->m_len = min(MCLBYTES, sopt_size);
1418 m->m_len = min(MLEN, sopt_size);
1420 sopt_size -= m->m_len;
1427 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1429 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1431 struct mbuf *m0 = m;
1433 if (sopt->sopt_val == NULL)
1435 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1436 if (sopt->sopt_p != NULL) {
1439 error = copyin(sopt->sopt_val, mtod(m, char *),
1446 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1447 sopt->sopt_valsize -= m->m_len;
1448 (caddr_t)sopt->sopt_val += m->m_len;
1451 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1452 panic("ip6_sooptmcopyin");
1456 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1458 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1460 struct mbuf *m0 = m;
1463 if (sopt->sopt_val == NULL)
1465 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1466 if (sopt->sopt_p != NULL) {
1469 error = copyout(mtod(m, char *), sopt->sopt_val,
1476 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1477 sopt->sopt_valsize -= m->m_len;
1478 (caddr_t)sopt->sopt_val += m->m_len;
1479 valsize += m->m_len;
1483 /* enough soopt buffer should be given from user-land */
1487 sopt->sopt_valsize = valsize;
1493 register struct socket *so;
1495 if (so->so_sigio != NULL)
1496 pgsigio(so->so_sigio, SIGURG, 0);
1497 selwakeup(&so->so_rcv.sb_sel);
1501 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p)
1506 if (events & (POLLIN | POLLRDNORM))
1508 revents |= events & (POLLIN | POLLRDNORM);
1510 if (events & (POLLOUT | POLLWRNORM))
1511 if (sowriteable(so))
1512 revents |= events & (POLLOUT | POLLWRNORM);
1514 if (events & (POLLPRI | POLLRDBAND))
1515 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1516 revents |= events & (POLLPRI | POLLRDBAND);
1519 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1520 selrecord(p, &so->so_rcv.sb_sel);
1521 so->so_rcv.sb_flags |= SB_SEL;
1524 if (events & (POLLOUT | POLLWRNORM)) {
1525 selrecord(p, &so->so_snd.sb_sel);
1526 so->so_snd.sb_flags |= SB_SEL;
1535 sokqfilter(struct file *fp, struct knote *kn)
1537 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1541 switch (kn->kn_filter) {
1543 if (so->so_options & SO_ACCEPTCONN)
1544 kn->kn_fop = &solisten_filtops;
1546 kn->kn_fop = &soread_filtops;
1550 kn->kn_fop = &sowrite_filtops;
1558 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext);
1559 sb->sb_flags |= SB_KNOTE;
1565 filt_sordetach(struct knote *kn)
1567 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1570 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1571 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1572 so->so_rcv.sb_flags &= ~SB_KNOTE;
1578 filt_soread(struct knote *kn, long hint)
1580 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1582 kn->kn_data = so->so_rcv.sb_cc;
1583 if (so->so_state & SS_CANTRCVMORE) {
1584 kn->kn_flags |= EV_EOF;
1587 if (so->so_error) /* temporary udp error */
1589 return (kn->kn_data >= so->so_rcv.sb_lowat);
1593 filt_sowdetach(struct knote *kn)
1595 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1598 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1599 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1600 so->so_snd.sb_flags &= ~SB_KNOTE;
1606 filt_sowrite(struct knote *kn, long hint)
1608 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1610 kn->kn_data = sbspace(&so->so_snd);
1611 if (so->so_state & SS_CANTSENDMORE) {
1612 kn->kn_flags |= EV_EOF;
1615 if (so->so_error) /* temporary udp error */
1617 if (((so->so_state & SS_ISCONNECTED) == 0) &&
1618 (so->so_proto->pr_flags & PR_CONNREQUIRED))
1620 return (kn->kn_data >= so->so_snd.sb_lowat);
1625 filt_solisten(struct knote *kn, long hint)
1627 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1629 kn->kn_data = so->so_qlen - so->so_incqlen;
1630 return (! TAILQ_EMPTY(&so->so_comp));