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 $
35 * $DragonFly: src/sys/kern/uipc_socket.c,v 1.2 2003/06/17 04:28:41 dillon Exp $
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
45 #include <sys/domain.h>
46 #include <sys/file.h> /* for struct knote */
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/event.h>
52 #include <sys/protosw.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/resourcevar.h>
56 #include <sys/signalvar.h>
57 #include <sys/sysctl.h>
60 #include <vm/vm_zone.h>
62 #include <machine/limits.h>
65 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
68 static void filt_sordetach(struct knote *kn);
69 static int filt_soread(struct knote *kn, long hint);
70 static void filt_sowdetach(struct knote *kn);
71 static int filt_sowrite(struct knote *kn, long hint);
72 static int filt_solisten(struct knote *kn, long hint);
74 static struct filterops solisten_filtops =
75 { 1, NULL, filt_sordetach, filt_solisten };
76 static struct filterops soread_filtops =
77 { 1, NULL, filt_sordetach, filt_soread };
78 static struct filterops sowrite_filtops =
79 { 1, NULL, filt_sowdetach, filt_sowrite };
81 struct vm_zone *socket_zone;
82 so_gen_t so_gencnt; /* generation count for sockets */
84 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
85 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
87 SYSCTL_DECL(_kern_ipc);
89 static int somaxconn = SOMAXCONN;
90 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
91 &somaxconn, 0, "Maximum pending socket connection queue size");
94 * Socket operation routines.
95 * These routines are called by the routines in
96 * sys_socket.c or from a system process, and
97 * implement the semantics of socket operations by
98 * switching out to the protocol specific routines.
102 * Get a socket structure from our zone, and initialize it.
103 * We don't implement `waitok' yet (see comments in uipc_domain.c).
104 * Note that it would probably be better to allocate socket
105 * and PCB at the same time, but I'm not convinced that all
106 * the protocols can be easily modified to do this.
114 so = zalloci(socket_zone);
116 /* XXX race condition for reentrant kernel */
117 bzero(so, sizeof *so);
118 so->so_gencnt = ++so_gencnt;
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 zfreei(socket_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");
260 so->so_state &= ~SS_INCOMP;
263 sbrelease(&so->so_snd, so);
269 * Close a socket on last file table reference removal.
270 * Initiate disconnect if connected.
271 * Free socket when disconnect complete.
275 register struct socket *so;
277 int s = splnet(); /* conservative */
280 funsetown(so->so_sigio);
281 if (so->so_options & SO_ACCEPTCONN) {
282 struct socket *sp, *sonext;
284 sp = TAILQ_FIRST(&so->so_incomp);
285 for (; sp != NULL; sp = sonext) {
286 sonext = TAILQ_NEXT(sp, so_list);
289 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
290 sonext = TAILQ_NEXT(sp, so_list);
291 /* Dequeue from so_comp since sofree() won't do it */
292 TAILQ_REMOVE(&so->so_comp, sp, so_list);
294 sp->so_state &= ~SS_COMP;
301 if (so->so_state & SS_ISCONNECTED) {
302 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
303 error = sodisconnect(so);
307 if (so->so_options & SO_LINGER) {
308 if ((so->so_state & SS_ISDISCONNECTING) &&
309 (so->so_state & SS_NBIO))
311 while (so->so_state & SS_ISCONNECTED) {
312 error = tsleep((caddr_t)&so->so_timeo,
313 PSOCK | PCATCH, "soclos", so->so_linger * hz);
321 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
326 if (so->so_state & SS_NOFDREF)
327 panic("soclose: NOFDREF");
328 so->so_state |= SS_NOFDREF;
335 * Must be called at splnet...
343 error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
353 register struct socket *so;
354 struct sockaddr **nam;
359 if ((so->so_state & SS_NOFDREF) == 0)
360 panic("soaccept: !NOFDREF");
361 so->so_state &= ~SS_NOFDREF;
362 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
368 soconnect(so, nam, p)
369 register struct socket *so;
370 struct sockaddr *nam;
376 if (so->so_options & SO_ACCEPTCONN)
380 * If protocol is connection-based, can only connect once.
381 * Otherwise, if connected, try to disconnect first.
382 * This allows user to disconnect by connecting to, e.g.,
385 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
386 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
387 (error = sodisconnect(so))))
390 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p);
397 register struct socket *so1;
403 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
410 register struct socket *so;
415 if ((so->so_state & SS_ISCONNECTED) == 0) {
419 if (so->so_state & SS_ISDISCONNECTING) {
423 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
429 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
432 * If send must go all at once and message is larger than
433 * send buffering, then hard error.
434 * Lock against other senders.
435 * If must go all at once and not enough room now, then
436 * inform user that this would block and do nothing.
437 * Otherwise, if nonblocking, send as much as possible.
438 * The data to be sent is described by "uio" if nonzero,
439 * otherwise by the mbuf chain "top" (which must be null
440 * if uio is not). Data provided in mbuf chain must be small
441 * enough to send all at once.
443 * Returns nonzero on error, timeout or signal; callers
444 * must check for short counts if EINTR/ERESTART are returned.
445 * Data and control buffers are freed on return.
448 sosend(so, addr, uio, top, control, flags, p)
449 register struct socket *so;
450 struct sockaddr *addr;
453 struct mbuf *control;
458 register struct mbuf *m;
459 register long space, len, resid;
460 int clen = 0, error, s, dontroute, mlen;
461 int atomic = sosendallatonce(so) || top;
464 resid = uio->uio_resid;
466 resid = top->m_pkthdr.len;
468 * In theory resid should be unsigned.
469 * However, space must be signed, as it might be less than 0
470 * if we over-committed, and we must use a signed comparison
471 * of space and resid. On the other hand, a negative resid
472 * causes us to loop sending 0-length segments to the protocol.
474 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
475 * type sockets since that's an error.
477 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
483 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
484 (so->so_proto->pr_flags & PR_ATOMIC);
486 p->p_stats->p_ru.ru_msgsnd++;
488 clen = control->m_len;
489 #define snderr(errno) { error = errno; splx(s); goto release; }
492 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
497 if (so->so_state & SS_CANTSENDMORE)
500 error = so->so_error;
505 if ((so->so_state & SS_ISCONNECTED) == 0) {
507 * `sendto' and `sendmsg' is allowed on a connection-
508 * based socket if it supports implied connect.
509 * Return ENOTCONN if not connected and no address is
512 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
513 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
514 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
515 !(resid == 0 && clen != 0))
517 } else if (addr == 0)
518 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
519 ENOTCONN : EDESTADDRREQ);
521 space = sbspace(&so->so_snd);
524 if ((atomic && resid > so->so_snd.sb_hiwat) ||
525 clen > so->so_snd.sb_hiwat)
527 if (space < resid + clen &&
528 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
529 if (so->so_state & SS_NBIO)
531 sbunlock(&so->so_snd);
532 error = sbwait(&so->so_snd);
544 * Data is prepackaged in "top".
548 top->m_flags |= M_EOR;
551 MGETHDR(m, M_WAIT, MT_DATA);
558 m->m_pkthdr.rcvif = (struct ifnet *)0;
560 MGET(m, M_WAIT, MT_DATA);
567 if (resid >= MINCLSIZE) {
569 if ((m->m_flags & M_EXT) == 0)
572 len = min(min(mlen, resid), space);
575 len = min(min(mlen, resid), space);
577 * For datagram protocols, leave room
578 * for protocol headers in first mbuf.
580 if (atomic && top == 0 && len < mlen)
584 error = uiomove(mtod(m, caddr_t), (int)len, uio);
585 resid = uio->uio_resid;
588 top->m_pkthdr.len += len;
594 top->m_flags |= M_EOR;
597 } while (space > 0 && atomic);
599 so->so_options |= SO_DONTROUTE;
600 s = splnet(); /* XXX */
602 * XXX all the SS_CANTSENDMORE checks previously
603 * done could be out of date. We could have recieved
604 * a reset packet in an interrupt or maybe we slept
605 * while doing page faults in uiomove() etc. We could
606 * probably recheck again inside the splnet() protection
607 * here, but there are probably other places that this
608 * also happens. We must rethink this.
610 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
611 (flags & MSG_OOB) ? PRUS_OOB :
613 * If the user set MSG_EOF, the protocol
614 * understands this flag and nothing left to
615 * send then use PRU_SEND_EOF instead of PRU_SEND.
617 ((flags & MSG_EOF) &&
618 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
621 /* If there is more to send set PRUS_MORETOCOME */
622 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
623 top, addr, control, p);
626 so->so_options &= ~SO_DONTROUTE;
633 } while (resid && space > 0);
637 sbunlock(&so->so_snd);
647 * Implement receive operations on a socket.
648 * We depend on the way that records are added to the sockbuf
649 * by sbappend*. In particular, each record (mbufs linked through m_next)
650 * must begin with an address if the protocol so specifies,
651 * followed by an optional mbuf or mbufs containing ancillary data,
652 * and then zero or more mbufs of data.
653 * In order to avoid blocking network interrupts for the entire time here,
654 * we splx() while doing the actual copy to user space.
655 * Although the sockbuf is locked, new data may still be appended,
656 * and thus we must maintain consistency of the sockbuf during that time.
658 * The caller may receive the data as a single mbuf chain by supplying
659 * an mbuf **mp0 for use in returning the chain. The uio is then used
660 * only for the count in uio_resid.
663 soreceive(so, psa, uio, mp0, controlp, flagsp)
664 register struct socket *so;
665 struct sockaddr **psa;
668 struct mbuf **controlp;
671 register struct mbuf *m, **mp;
672 register int flags, len, error, s, offset;
673 struct protosw *pr = so->so_proto;
674 struct mbuf *nextrecord;
676 int orig_resid = uio->uio_resid;
684 flags = *flagsp &~ MSG_EOR;
687 if (flags & MSG_OOB) {
688 m = m_get(M_WAIT, MT_DATA);
691 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
695 error = uiomove(mtod(m, caddr_t),
696 (int) min(uio->uio_resid, m->m_len), uio);
698 } while (uio->uio_resid && error == 0 && m);
705 *mp = (struct mbuf *)0;
706 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
707 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
710 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
715 m = so->so_rcv.sb_mb;
717 * If we have less data than requested, block awaiting more
718 * (subject to any timeout) if:
719 * 1. the current count is less than the low water mark, or
720 * 2. MSG_WAITALL is set, and it is possible to do the entire
721 * receive operation at once if we block (resid <= hiwat).
722 * 3. MSG_DONTWAIT is not set
723 * If MSG_WAITALL is set but resid is larger than the receive buffer,
724 * we have to do the receive in sections, and thus risk returning
725 * a short count if a timeout or signal occurs after we start.
727 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
728 so->so_rcv.sb_cc < uio->uio_resid) &&
729 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
730 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
731 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
732 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
736 error = so->so_error;
737 if ((flags & MSG_PEEK) == 0)
741 if (so->so_state & SS_CANTRCVMORE) {
747 for (; m; m = m->m_next)
748 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
749 m = so->so_rcv.sb_mb;
752 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
753 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
757 if (uio->uio_resid == 0)
759 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
763 sbunlock(&so->so_rcv);
764 error = sbwait(&so->so_rcv);
772 uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
773 nextrecord = m->m_nextpkt;
774 if (pr->pr_flags & PR_ADDR) {
775 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
778 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
780 if (flags & MSG_PEEK) {
783 sbfree(&so->so_rcv, m);
784 so->so_rcv.sb_mb = m_free(m);
785 m = so->so_rcv.sb_mb;
788 while (m && m->m_type == MT_CONTROL && error == 0) {
789 if (flags & MSG_PEEK) {
791 *controlp = m_copy(m, 0, m->m_len);
794 sbfree(&so->so_rcv, m);
796 if (pr->pr_domain->dom_externalize &&
797 mtod(m, struct cmsghdr *)->cmsg_type ==
799 error = (*pr->pr_domain->dom_externalize)(m);
801 so->so_rcv.sb_mb = m->m_next;
803 m = so->so_rcv.sb_mb;
805 so->so_rcv.sb_mb = m_free(m);
806 m = so->so_rcv.sb_mb;
811 controlp = &(*controlp)->m_next;
815 if ((flags & MSG_PEEK) == 0)
816 m->m_nextpkt = nextrecord;
818 if (type == MT_OOBDATA)
823 while (m && uio->uio_resid > 0 && error == 0) {
824 if (m->m_type == MT_OOBDATA) {
825 if (type != MT_OOBDATA)
827 } else if (type == MT_OOBDATA)
830 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
832 so->so_state &= ~SS_RCVATMARK;
833 len = uio->uio_resid;
834 if (so->so_oobmark && len > so->so_oobmark - offset)
835 len = so->so_oobmark - offset;
836 if (len > m->m_len - moff)
837 len = m->m_len - moff;
839 * If mp is set, just pass back the mbufs.
840 * Otherwise copy them out via the uio, then free.
841 * Sockbuf must be consistent here (points to current mbuf,
842 * it points to next record) when we drop priority;
843 * we must note any additions to the sockbuf when we
844 * block interrupts again.
848 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
853 uio->uio_resid -= len;
854 if (len == m->m_len - moff) {
855 if (m->m_flags & M_EOR)
857 if (flags & MSG_PEEK) {
861 nextrecord = m->m_nextpkt;
862 sbfree(&so->so_rcv, m);
866 so->so_rcv.sb_mb = m = m->m_next;
867 *mp = (struct mbuf *)0;
869 so->so_rcv.sb_mb = m = m_free(m);
872 m->m_nextpkt = nextrecord;
875 if (flags & MSG_PEEK)
879 *mp = m_copym(m, 0, len, M_WAIT);
882 so->so_rcv.sb_cc -= len;
885 if (so->so_oobmark) {
886 if ((flags & MSG_PEEK) == 0) {
887 so->so_oobmark -= len;
888 if (so->so_oobmark == 0) {
889 so->so_state |= SS_RCVATMARK;
894 if (offset == so->so_oobmark)
901 * If the MSG_WAITALL flag is set (for non-atomic socket),
902 * we must not quit until "uio->uio_resid == 0" or an error
903 * termination. If a signal/timeout occurs, return
904 * with a short count but without error.
905 * Keep sockbuf locked against other readers.
907 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
908 !sosendallatonce(so) && !nextrecord) {
909 if (so->so_error || so->so_state & SS_CANTRCVMORE)
912 * The window might have closed to zero, make
913 * sure we send an ack now that we've drained
914 * the buffer or we might end up blocking until
915 * the idle takes over (5 seconds).
917 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
918 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
919 error = sbwait(&so->so_rcv);
921 sbunlock(&so->so_rcv);
925 m = so->so_rcv.sb_mb;
927 nextrecord = m->m_nextpkt;
931 if (m && pr->pr_flags & PR_ATOMIC) {
933 if ((flags & MSG_PEEK) == 0)
934 (void) sbdroprecord(&so->so_rcv);
936 if ((flags & MSG_PEEK) == 0) {
938 so->so_rcv.sb_mb = nextrecord;
939 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
940 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
942 if (orig_resid == uio->uio_resid && orig_resid &&
943 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
944 sbunlock(&so->so_rcv);
952 sbunlock(&so->so_rcv);
959 register struct socket *so;
962 register struct protosw *pr = so->so_proto;
964 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
970 return ((*pr->pr_usrreqs->pru_shutdown)(so));
976 register struct socket *so;
978 register struct sockbuf *sb = &so->so_rcv;
979 register struct protosw *pr = so->so_proto;
983 sb->sb_flags |= SB_NOINTR;
984 (void) sblock(sb, M_WAITOK);
989 bzero((caddr_t)sb, sizeof (*sb));
990 if (asb.sb_flags & SB_KNOTE) {
991 sb->sb_sel.si_note = asb.sb_sel.si_note;
992 sb->sb_flags = SB_KNOTE;
995 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
996 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
1002 do_setopt_accept_filter(so, sopt)
1004 struct sockopt *sopt;
1006 struct accept_filter_arg *afap = NULL;
1007 struct accept_filter *afp;
1008 struct so_accf *af = so->so_accf;
1011 /* do not set/remove accept filters on non listen sockets */
1012 if ((so->so_options & SO_ACCEPTCONN) == 0) {
1017 /* removing the filter */
1020 if (af->so_accept_filter != NULL &&
1021 af->so_accept_filter->accf_destroy != NULL) {
1022 af->so_accept_filter->accf_destroy(so);
1024 if (af->so_accept_filter_str != NULL) {
1025 FREE(af->so_accept_filter_str, M_ACCF);
1030 so->so_options &= ~SO_ACCEPTFILTER;
1033 /* adding a filter */
1034 /* must remove previous filter first */
1039 /* don't put large objects on the kernel stack */
1040 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1041 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1042 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1043 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1046 afp = accept_filt_get(afap->af_name);
1051 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK);
1052 bzero(af, sizeof(*af));
1053 if (afp->accf_create != NULL) {
1054 if (afap->af_name[0] != '\0') {
1055 int len = strlen(afap->af_name) + 1;
1057 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1058 strcpy(af->so_accept_filter_str, afap->af_name);
1060 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1061 if (af->so_accept_filter_arg == NULL) {
1062 FREE(af->so_accept_filter_str, M_ACCF);
1069 af->so_accept_filter = afp;
1071 so->so_options |= SO_ACCEPTFILTER;
1080 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1081 * an additional variant to handle the case where the option value needs
1082 * to be some kind of integer, but not a specific size.
1083 * In addition to their use here, these functions are also called by the
1084 * protocol-level pr_ctloutput() routines.
1087 sooptcopyin(sopt, buf, len, minlen)
1088 struct sockopt *sopt;
1096 * If the user gives us more than we wanted, we ignore it,
1097 * but if we don't get the minimum length the caller
1098 * wants, we return EINVAL. On success, sopt->sopt_valsize
1099 * is set to however much we actually retrieved.
1101 if ((valsize = sopt->sopt_valsize) < minlen)
1104 sopt->sopt_valsize = valsize = len;
1106 if (sopt->sopt_p != 0)
1107 return (copyin(sopt->sopt_val, buf, valsize));
1109 bcopy(sopt->sopt_val, buf, valsize);
1116 struct sockopt *sopt;
1124 if (sopt->sopt_level != SOL_SOCKET) {
1125 if (so->so_proto && so->so_proto->pr_ctloutput)
1126 return ((*so->so_proto->pr_ctloutput)
1128 error = ENOPROTOOPT;
1130 switch (sopt->sopt_name) {
1132 case SO_ACCEPTFILTER:
1133 error = do_setopt_accept_filter(so, sopt);
1139 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1143 so->so_linger = l.l_linger;
1145 so->so_options |= SO_LINGER;
1147 so->so_options &= ~SO_LINGER;
1153 case SO_USELOOPBACK:
1159 error = sooptcopyin(sopt, &optval, sizeof optval,
1164 so->so_options |= sopt->sopt_name;
1166 so->so_options &= ~sopt->sopt_name;
1173 error = sooptcopyin(sopt, &optval, sizeof optval,
1179 * Values < 1 make no sense for any of these
1180 * options, so disallow them.
1187 switch (sopt->sopt_name) {
1190 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1191 &so->so_snd : &so->so_rcv, (u_long)optval,
1192 so, curproc) == 0) {
1199 * Make sure the low-water is never greater than
1203 so->so_snd.sb_lowat =
1204 (optval > so->so_snd.sb_hiwat) ?
1205 so->so_snd.sb_hiwat : optval;
1208 so->so_rcv.sb_lowat =
1209 (optval > so->so_rcv.sb_hiwat) ?
1210 so->so_rcv.sb_hiwat : optval;
1217 error = sooptcopyin(sopt, &tv, sizeof tv,
1222 /* assert(hz > 0); */
1223 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1224 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1228 /* assert(tick > 0); */
1229 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1230 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1231 if (val > SHRT_MAX) {
1235 if (val == 0 && tv.tv_usec != 0)
1238 switch (sopt->sopt_name) {
1240 so->so_snd.sb_timeo = val;
1243 so->so_rcv.sb_timeo = val;
1248 error = ENOPROTOOPT;
1251 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1252 (void) ((*so->so_proto->pr_ctloutput)
1260 /* Helper routine for getsockopt */
1262 sooptcopyout(sopt, buf, len)
1263 struct sockopt *sopt;
1273 * Documented get behavior is that we always return a value,
1274 * possibly truncated to fit in the user's buffer.
1275 * Traditional behavior is that we always tell the user
1276 * precisely how much we copied, rather than something useful
1277 * like the total amount we had available for her.
1278 * Note that this interface is not idempotent; the entire answer must
1279 * generated ahead of time.
1281 valsize = min(len, sopt->sopt_valsize);
1282 sopt->sopt_valsize = valsize;
1283 if (sopt->sopt_val != 0) {
1284 if (sopt->sopt_p != 0)
1285 error = copyout(buf, sopt->sopt_val, valsize);
1287 bcopy(buf, sopt->sopt_val, valsize);
1295 struct sockopt *sopt;
1300 struct accept_filter_arg *afap;
1303 if (sopt->sopt_level != SOL_SOCKET) {
1304 if (so->so_proto && so->so_proto->pr_ctloutput) {
1305 return ((*so->so_proto->pr_ctloutput)
1308 return (ENOPROTOOPT);
1310 switch (sopt->sopt_name) {
1312 case SO_ACCEPTFILTER:
1313 if ((so->so_options & SO_ACCEPTCONN) == 0)
1315 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1317 bzero(afap, sizeof(*afap));
1318 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1319 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1320 if (so->so_accf->so_accept_filter_str != NULL)
1321 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1323 error = sooptcopyout(sopt, afap, sizeof(*afap));
1329 l.l_onoff = so->so_options & SO_LINGER;
1330 l.l_linger = so->so_linger;
1331 error = sooptcopyout(sopt, &l, sizeof l);
1334 case SO_USELOOPBACK:
1343 optval = so->so_options & sopt->sopt_name;
1345 error = sooptcopyout(sopt, &optval, sizeof optval);
1349 optval = so->so_type;
1353 optval = so->so_error;
1358 optval = so->so_snd.sb_hiwat;
1362 optval = so->so_rcv.sb_hiwat;
1366 optval = so->so_snd.sb_lowat;
1370 optval = so->so_rcv.sb_lowat;
1375 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1376 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1378 tv.tv_sec = optval / hz;
1379 tv.tv_usec = (optval % hz) * tick;
1380 error = sooptcopyout(sopt, &tv, sizeof tv);
1384 error = ENOPROTOOPT;
1391 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1393 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1395 struct mbuf *m, *m_prev;
1396 int sopt_size = sopt->sopt_valsize;
1398 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1401 if (sopt_size > MLEN) {
1402 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1403 if ((m->m_flags & M_EXT) == 0) {
1407 m->m_len = min(MCLBYTES, sopt_size);
1409 m->m_len = min(MLEN, sopt_size);
1411 sopt_size -= m->m_len;
1416 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1421 if (sopt_size > MLEN) {
1422 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1423 if ((m->m_flags & M_EXT) == 0) {
1427 m->m_len = min(MCLBYTES, sopt_size);
1429 m->m_len = min(MLEN, sopt_size);
1431 sopt_size -= m->m_len;
1438 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1440 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1442 struct mbuf *m0 = m;
1444 if (sopt->sopt_val == NULL)
1446 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1447 if (sopt->sopt_p != NULL) {
1450 error = copyin(sopt->sopt_val, mtod(m, char *),
1457 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1458 sopt->sopt_valsize -= m->m_len;
1459 (caddr_t)sopt->sopt_val += m->m_len;
1462 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1463 panic("ip6_sooptmcopyin");
1467 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1469 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1471 struct mbuf *m0 = m;
1474 if (sopt->sopt_val == NULL)
1476 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1477 if (sopt->sopt_p != NULL) {
1480 error = copyout(mtod(m, char *), sopt->sopt_val,
1487 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1488 sopt->sopt_valsize -= m->m_len;
1489 (caddr_t)sopt->sopt_val += m->m_len;
1490 valsize += m->m_len;
1494 /* enough soopt buffer should be given from user-land */
1498 sopt->sopt_valsize = valsize;
1504 register struct socket *so;
1506 if (so->so_sigio != NULL)
1507 pgsigio(so->so_sigio, SIGURG, 0);
1508 selwakeup(&so->so_rcv.sb_sel);
1512 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p)
1517 if (events & (POLLIN | POLLRDNORM))
1519 revents |= events & (POLLIN | POLLRDNORM);
1521 if (events & (POLLOUT | POLLWRNORM))
1522 if (sowriteable(so))
1523 revents |= events & (POLLOUT | POLLWRNORM);
1525 if (events & (POLLPRI | POLLRDBAND))
1526 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1527 revents |= events & (POLLPRI | POLLRDBAND);
1530 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1531 selrecord(p, &so->so_rcv.sb_sel);
1532 so->so_rcv.sb_flags |= SB_SEL;
1535 if (events & (POLLOUT | POLLWRNORM)) {
1536 selrecord(p, &so->so_snd.sb_sel);
1537 so->so_snd.sb_flags |= SB_SEL;
1546 sokqfilter(struct file *fp, struct knote *kn)
1548 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1552 switch (kn->kn_filter) {
1554 if (so->so_options & SO_ACCEPTCONN)
1555 kn->kn_fop = &solisten_filtops;
1557 kn->kn_fop = &soread_filtops;
1561 kn->kn_fop = &sowrite_filtops;
1569 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext);
1570 sb->sb_flags |= SB_KNOTE;
1576 filt_sordetach(struct knote *kn)
1578 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1581 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1582 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1583 so->so_rcv.sb_flags &= ~SB_KNOTE;
1589 filt_soread(struct knote *kn, long hint)
1591 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1593 kn->kn_data = so->so_rcv.sb_cc;
1594 if (so->so_state & SS_CANTRCVMORE) {
1595 kn->kn_flags |= EV_EOF;
1596 kn->kn_fflags = so->so_error;
1599 if (so->so_error) /* temporary udp error */
1601 if (kn->kn_sfflags & NOTE_LOWAT)
1602 return (kn->kn_data >= kn->kn_sdata);
1603 return (kn->kn_data >= so->so_rcv.sb_lowat);
1607 filt_sowdetach(struct knote *kn)
1609 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1612 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1613 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1614 so->so_snd.sb_flags &= ~SB_KNOTE;
1620 filt_sowrite(struct knote *kn, long hint)
1622 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1624 kn->kn_data = sbspace(&so->so_snd);
1625 if (so->so_state & SS_CANTSENDMORE) {
1626 kn->kn_flags |= EV_EOF;
1627 kn->kn_fflags = so->so_error;
1630 if (so->so_error) /* temporary udp error */
1632 if (((so->so_state & SS_ISCONNECTED) == 0) &&
1633 (so->so_proto->pr_flags & PR_CONNREQUIRED))
1635 if (kn->kn_sfflags & NOTE_LOWAT)
1636 return (kn->kn_data >= kn->kn_sdata);
1637 return (kn->kn_data >= so->so_snd.sb_lowat);
1642 filt_solisten(struct knote *kn, long hint)
1644 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1646 kn->kn_data = so->so_qlen;
1647 return (! TAILQ_EMPTY(&so->so_comp));