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.24 2003/11/11 17:18:18 silby Exp $
35 * $DragonFly: src/sys/kern/uipc_socket.c,v 1.15 2004/03/05 16:57:15 hsu 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/socketops.h>
56 #include <sys/resourcevar.h>
57 #include <sys/signalvar.h>
58 #include <sys/sysctl.h>
61 #include <vm/vm_zone.h>
63 #include <machine/limits.h>
66 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
69 static void filt_sordetach(struct knote *kn);
70 static int filt_soread(struct knote *kn, long hint);
71 static void filt_sowdetach(struct knote *kn);
72 static int filt_sowrite(struct knote *kn, long hint);
73 static int filt_solisten(struct knote *kn, long hint);
75 static struct filterops solisten_filtops =
76 { 1, NULL, filt_sordetach, filt_solisten };
77 static struct filterops soread_filtops =
78 { 1, NULL, filt_sordetach, filt_soread };
79 static struct filterops sowrite_filtops =
80 { 1, NULL, filt_sowdetach, filt_sowrite };
82 struct vm_zone *socket_zone;
83 so_gen_t so_gencnt; /* generation count for sockets */
85 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
86 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
88 SYSCTL_DECL(_kern_ipc);
90 static int somaxconn = SOMAXCONN;
91 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
92 &somaxconn, 0, "Maximum pending socket connection queue size");
95 * Socket operation routines.
96 * These routines are called by the routines in
97 * sys_socket.c or from a system process, and
98 * implement the semantics of socket operations by
99 * switching out to the protocol specific routines.
103 * Get a socket structure from our zone, and initialize it.
104 * We don't implement `waitok' yet (see comments in uipc_domain.c).
105 * Note that it would probably be better to allocate socket
106 * and PCB at the same time, but I'm not convinced that all
107 * the protocols can be easily modified to do this.
115 so = zalloc(socket_zone);
117 /* XXX race condition for reentrant kernel */
118 bzero(so, sizeof *so);
119 so->so_gencnt = ++so_gencnt;
120 TAILQ_INIT(&so->so_aiojobq);
126 socreate(int dom, struct socket **aso, int type,
127 int proto, struct thread *td)
129 struct proc *p = td->td_proc;
132 struct pru_attach_info ai;
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_ucred->cr_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 = crhold(p->p_ucred);
161 ai.sb_rlimit = &p->p_rlimit[RLIMIT_SBSIZE];
162 ai.p_ucred = p->p_ucred;
163 ai.fd_rdir = p->p_fd->fd_rdir;
164 error = so_pru_attach(so, proto, &ai);
166 so->so_state |= SS_NOFDREF;
175 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
180 error = so_pru_bind(so, nam, td);
186 sodealloc(struct socket *so)
189 so->so_gencnt = ++so_gencnt;
190 if (so->so_rcv.sb_hiwat)
191 (void)chgsbsize(so->so_cred->cr_uidinfo,
192 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
193 if (so->so_snd.sb_hiwat)
194 (void)chgsbsize(so->so_cred->cr_uidinfo,
195 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
197 if (so->so_accf != NULL) {
198 if (so->so_accf->so_accept_filter != NULL &&
199 so->so_accf->so_accept_filter->accf_destroy != NULL) {
200 so->so_accf->so_accept_filter->accf_destroy(so);
202 if (so->so_accf->so_accept_filter_str != NULL)
203 FREE(so->so_accf->so_accept_filter_str, M_ACCF);
204 FREE(so->so_accf, M_ACCF);
208 zfree(socket_zone, so);
212 solisten(struct socket *so, int backlog, struct thread *td)
217 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING)) {
222 error = so_pru_listen(so, td);
227 if (TAILQ_EMPTY(&so->so_comp))
228 so->so_options |= SO_ACCEPTCONN;
229 if (backlog < 0 || backlog > somaxconn)
231 so->so_qlimit = backlog;
237 sofree(struct socket *so)
239 struct socket *head = so->so_head;
241 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
244 if (so->so_state & SS_INCOMP) {
245 TAILQ_REMOVE(&head->so_incomp, so, so_list);
247 } else if (so->so_state & SS_COMP) {
249 * We must not decommission a socket that's
250 * on the accept(2) queue. If we do, then
251 * accept(2) may hang after select(2) indicated
252 * that the listening socket was ready.
256 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.
272 soclose(struct socket *so)
274 int s = splnet(); /* conservative */
277 funsetown(so->so_sigio);
278 if (so->so_options & SO_ACCEPTCONN) {
279 struct socket *sp, *sonext;
281 sp = TAILQ_FIRST(&so->so_incomp);
282 for (; sp != NULL; sp = sonext) {
283 sonext = TAILQ_NEXT(sp, so_list);
286 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
287 sonext = TAILQ_NEXT(sp, so_list);
288 /* Dequeue from so_comp since sofree() won't do it */
289 TAILQ_REMOVE(&so->so_comp, sp, so_list);
291 sp->so_state &= ~SS_COMP;
298 if (so->so_state & SS_ISCONNECTED) {
299 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
300 error = sodisconnect(so);
304 if (so->so_options & SO_LINGER) {
305 if ((so->so_state & SS_ISDISCONNECTING) &&
306 (so->so_state & SS_NBIO))
308 while (so->so_state & SS_ISCONNECTED) {
309 error = tsleep((caddr_t)&so->so_timeo,
310 PCATCH, "soclos", so->so_linger * hz);
320 error2 = so_pru_detach(so);
325 if (so->so_state & SS_NOFDREF)
326 panic("soclose: NOFDREF");
327 so->so_state |= SS_NOFDREF;
334 * Must be called at splnet...
342 error = so_pru_abort(so);
351 soaccept(struct socket *so, struct sockaddr **nam)
356 if ((so->so_state & SS_NOFDREF) == 0)
357 panic("soaccept: !NOFDREF");
358 so->so_state &= ~SS_NOFDREF;
359 error = so_pru_accept(so, nam);
365 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
370 if (so->so_options & SO_ACCEPTCONN)
374 * If protocol is connection-based, can only connect once.
375 * Otherwise, if connected, try to disconnect first.
376 * This allows user to disconnect by connecting to, e.g.,
379 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
380 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
381 (error = sodisconnect(so))))
384 error = so_pru_connect(so, nam, td);
390 soconnect2(struct socket *so1, struct socket *so2)
395 error = so_pru_connect2(so1, so2);
401 sodisconnect(struct socket *so)
406 if ((so->so_state & SS_ISCONNECTED) == 0) {
410 if (so->so_state & SS_ISDISCONNECTING) {
414 error = so_pru_disconnect(so);
420 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
423 * If send must go all at once and message is larger than
424 * send buffering, then hard error.
425 * Lock against other senders.
426 * If must go all at once and not enough room now, then
427 * inform user that this would block and do nothing.
428 * Otherwise, if nonblocking, send as much as possible.
429 * The data to be sent is described by "uio" if nonzero,
430 * otherwise by the mbuf chain "top" (which must be null
431 * if uio is not). Data provided in mbuf chain must be small
432 * enough to send all at once.
434 * Returns nonzero on error, timeout or signal; callers
435 * must check for short counts if EINTR/ERESTART are returned.
436 * Data and control buffers are freed on return.
439 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
440 struct mbuf *top, struct mbuf *control, int flags,
445 long space, len, resid;
446 int clen = 0, error, s, dontroute, mlen;
447 int atomic = sosendallatonce(so) || top;
451 resid = uio->uio_resid;
453 resid = top->m_pkthdr.len;
455 * In theory resid should be unsigned.
456 * However, space must be signed, as it might be less than 0
457 * if we over-committed, and we must use a signed comparison
458 * of space and resid. On the other hand, a negative resid
459 * causes us to loop sending 0-length segments to the protocol.
461 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
462 * type sockets since that's an error.
464 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
470 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
471 (so->so_proto->pr_flags & PR_ATOMIC);
472 if (td->td_proc && td->td_proc->p_stats)
473 td->td_proc->p_stats->p_ru.ru_msgsnd++;
475 clen = control->m_len;
476 #define snderr(errno) { error = errno; splx(s); goto release; }
479 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
484 if (so->so_state & SS_CANTSENDMORE)
487 error = so->so_error;
492 if ((so->so_state & SS_ISCONNECTED) == 0) {
494 * `sendto' and `sendmsg' is allowed on a connection-
495 * based socket if it supports implied connect.
496 * Return ENOTCONN if not connected and no address is
499 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
500 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
501 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
502 !(resid == 0 && clen != 0))
504 } else if (addr == 0)
505 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
506 ENOTCONN : EDESTADDRREQ);
508 space = sbspace(&so->so_snd);
511 if ((atomic && resid > so->so_snd.sb_hiwat) ||
512 clen > so->so_snd.sb_hiwat)
514 if (space < resid + clen && uio &&
515 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
516 if (so->so_state & SS_NBIO)
518 sbunlock(&so->so_snd);
519 error = sbwait(&so->so_snd);
531 * Data is prepackaged in "top".
535 top->m_flags |= M_EOR;
538 MGETHDR(m, M_WAIT, MT_DATA);
545 m->m_pkthdr.rcvif = (struct ifnet *)0;
547 MGET(m, M_WAIT, MT_DATA);
554 if (resid >= MINCLSIZE) {
556 if ((m->m_flags & M_EXT) == 0)
559 len = min(min(mlen, resid), space);
562 len = min(min(mlen, resid), space);
564 * For datagram protocols, leave room
565 * for protocol headers in first mbuf.
567 if (atomic && top == 0 && len < mlen)
571 error = uiomove(mtod(m, caddr_t), (int)len, uio);
572 resid = uio->uio_resid;
575 top->m_pkthdr.len += len;
581 top->m_flags |= M_EOR;
584 } while (space > 0 && atomic);
586 so->so_options |= SO_DONTROUTE;
587 if (flags & MSG_OOB) {
588 pru_flags = PRUS_OOB;
589 } else if ((flags & MSG_EOF) &&
590 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
593 * If the user set MSG_EOF, the protocol
594 * understands this flag and nothing left to
595 * send then use PRU_SEND_EOF instead of PRU_SEND.
597 pru_flags = PRUS_EOF;
598 } else if (resid > 0 && space > 0) {
599 /* If there is more to send, set PRUS_MORETOCOME */
600 pru_flags = PRUS_MORETOCOME;
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_pru_send(so, pru_flags, top, addr, control, td);
617 so->so_options &= ~SO_DONTROUTE;
624 } while (resid && space > 0);
628 sbunlock(&so->so_snd);
638 * Implement receive operations on a socket.
639 * We depend on the way that records are added to the sockbuf
640 * by sbappend*. In particular, each record (mbufs linked through m_next)
641 * must begin with an address if the protocol so specifies,
642 * followed by an optional mbuf or mbufs containing ancillary data,
643 * and then zero or more mbufs of data.
644 * In order to avoid blocking network interrupts for the entire time here,
645 * we splx() while doing the actual copy to user space.
646 * Although the sockbuf is locked, new data may still be appended,
647 * and thus we must maintain consistency of the sockbuf during that time.
649 * The caller may receive the data as a single mbuf chain by supplying
650 * an mbuf **mp0 for use in returning the chain. The uio is then used
651 * only for the count in uio_resid.
654 soreceive(so, psa, uio, mp0, controlp, flagsp)
656 struct sockaddr **psa;
659 struct mbuf **controlp;
662 struct mbuf *m, **mp;
663 int flags, len, error, s, offset;
664 struct protosw *pr = so->so_proto;
665 struct mbuf *nextrecord;
667 int orig_resid = uio->uio_resid;
675 flags = *flagsp &~ MSG_EOR;
678 if (flags & MSG_OOB) {
679 m = m_get(M_WAIT, MT_DATA);
682 error = so_pru_rcvoob(so, m, flags & MSG_PEEK);
686 error = uiomove(mtod(m, caddr_t),
687 (int) min(uio->uio_resid, m->m_len), uio);
689 } while (uio->uio_resid && error == 0 && m);
696 *mp = (struct mbuf *)0;
697 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
701 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
706 m = so->so_rcv.sb_mb;
708 * If we have less data than requested, block awaiting more
709 * (subject to any timeout) if:
710 * 1. the current count is less than the low water mark, or
711 * 2. MSG_WAITALL is set, and it is possible to do the entire
712 * receive operation at once if we block (resid <= hiwat).
713 * 3. MSG_DONTWAIT is not set
714 * If MSG_WAITALL is set but resid is larger than the receive buffer,
715 * we have to do the receive in sections, and thus risk returning
716 * a short count if a timeout or signal occurs after we start.
718 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
719 so->so_rcv.sb_cc < uio->uio_resid) &&
720 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
721 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
722 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
723 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
727 error = so->so_error;
728 if ((flags & MSG_PEEK) == 0)
732 if (so->so_state & SS_CANTRCVMORE) {
738 for (; m; m = m->m_next)
739 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
740 m = so->so_rcv.sb_mb;
743 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
744 (pr->pr_flags & PR_CONNREQUIRED)) {
748 if (uio->uio_resid == 0)
750 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
754 sbunlock(&so->so_rcv);
755 error = sbwait(&so->so_rcv);
762 if (uio->uio_td && uio->uio_td->td_proc)
763 uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++;
764 nextrecord = m->m_nextpkt;
765 if (pr->pr_flags & PR_ADDR) {
766 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
769 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
771 if (flags & MSG_PEEK) {
774 sbfree(&so->so_rcv, m);
775 so->so_rcv.sb_mb = m_free(m);
776 m = so->so_rcv.sb_mb;
779 while (m && m->m_type == MT_CONTROL && error == 0) {
780 if (flags & MSG_PEEK) {
782 *controlp = m_copy(m, 0, m->m_len);
785 sbfree(&so->so_rcv, m);
787 if (pr->pr_domain->dom_externalize &&
788 mtod(m, struct cmsghdr *)->cmsg_type ==
790 error = (*pr->pr_domain->dom_externalize)(m);
792 so->so_rcv.sb_mb = m->m_next;
794 m = so->so_rcv.sb_mb;
796 so->so_rcv.sb_mb = m_free(m);
797 m = so->so_rcv.sb_mb;
802 controlp = &(*controlp)->m_next;
806 if ((flags & MSG_PEEK) == 0)
807 m->m_nextpkt = nextrecord;
809 if (type == MT_OOBDATA)
814 while (m && uio->uio_resid > 0 && error == 0) {
815 if (m->m_type == MT_OOBDATA) {
816 if (type != MT_OOBDATA)
818 } else if (type == MT_OOBDATA)
821 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
823 so->so_state &= ~SS_RCVATMARK;
824 len = uio->uio_resid;
825 if (so->so_oobmark && len > so->so_oobmark - offset)
826 len = so->so_oobmark - offset;
827 if (len > m->m_len - moff)
828 len = m->m_len - moff;
830 * If mp is set, just pass back the mbufs.
831 * Otherwise copy them out via the uio, then free.
832 * Sockbuf must be consistent here (points to current mbuf,
833 * it points to next record) when we drop priority;
834 * we must note any additions to the sockbuf when we
835 * block interrupts again.
839 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
844 uio->uio_resid -= len;
845 if (len == m->m_len - moff) {
846 if (m->m_flags & M_EOR)
848 if (flags & MSG_PEEK) {
852 nextrecord = m->m_nextpkt;
853 sbfree(&so->so_rcv, m);
857 so->so_rcv.sb_mb = m = m->m_next;
858 *mp = (struct mbuf *)0;
860 so->so_rcv.sb_mb = m = m_free(m);
863 m->m_nextpkt = nextrecord;
866 if (flags & MSG_PEEK)
870 *mp = m_copym(m, 0, len, M_WAIT);
873 so->so_rcv.sb_cc -= len;
876 if (so->so_oobmark) {
877 if ((flags & MSG_PEEK) == 0) {
878 so->so_oobmark -= len;
879 if (so->so_oobmark == 0) {
880 so->so_state |= SS_RCVATMARK;
885 if (offset == so->so_oobmark)
892 * If the MSG_WAITALL flag is set (for non-atomic socket),
893 * we must not quit until "uio->uio_resid == 0" or an error
894 * termination. If a signal/timeout occurs, return
895 * with a short count but without error.
896 * Keep sockbuf locked against other readers.
898 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
899 !sosendallatonce(so) && !nextrecord) {
900 if (so->so_error || so->so_state & SS_CANTRCVMORE)
903 * The window might have closed to zero, make
904 * sure we send an ack now that we've drained
905 * the buffer or we might end up blocking until
906 * the idle takes over (5 seconds).
908 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
909 so_pru_rcvd(so, flags);
910 error = sbwait(&so->so_rcv);
912 sbunlock(&so->so_rcv);
916 m = so->so_rcv.sb_mb;
918 nextrecord = m->m_nextpkt;
922 if (m && pr->pr_flags & PR_ATOMIC) {
924 if ((flags & MSG_PEEK) == 0)
925 (void) sbdroprecord(&so->so_rcv);
927 if ((flags & MSG_PEEK) == 0) {
929 so->so_rcv.sb_mb = nextrecord;
930 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
931 so_pru_rcvd(so, flags);
933 if (orig_resid == uio->uio_resid && orig_resid &&
934 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
935 sbunlock(&so->so_rcv);
943 sbunlock(&so->so_rcv);
953 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
959 return (so_pru_shutdown(so));
967 struct sockbuf *sb = &so->so_rcv;
968 struct protosw *pr = so->so_proto;
972 sb->sb_flags |= SB_NOINTR;
973 (void) sblock(sb, M_WAITOK);
978 bzero((caddr_t)sb, sizeof (*sb));
979 if (asb.sb_flags & SB_KNOTE) {
980 sb->sb_sel.si_note = asb.sb_sel.si_note;
981 sb->sb_flags = SB_KNOTE;
984 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
985 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
991 do_setopt_accept_filter(so, sopt)
993 struct sockopt *sopt;
995 struct accept_filter_arg *afap = NULL;
996 struct accept_filter *afp;
997 struct so_accf *af = so->so_accf;
1000 /* do not set/remove accept filters on non listen sockets */
1001 if ((so->so_options & SO_ACCEPTCONN) == 0) {
1006 /* removing the filter */
1009 if (af->so_accept_filter != NULL &&
1010 af->so_accept_filter->accf_destroy != NULL) {
1011 af->so_accept_filter->accf_destroy(so);
1013 if (af->so_accept_filter_str != NULL) {
1014 FREE(af->so_accept_filter_str, M_ACCF);
1019 so->so_options &= ~SO_ACCEPTFILTER;
1022 /* adding a filter */
1023 /* must remove previous filter first */
1028 /* don't put large objects on the kernel stack */
1029 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1030 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1031 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1032 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1035 afp = accept_filt_get(afap->af_name);
1040 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK);
1041 bzero(af, sizeof(*af));
1042 if (afp->accf_create != NULL) {
1043 if (afap->af_name[0] != '\0') {
1044 int len = strlen(afap->af_name) + 1;
1046 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1047 strcpy(af->so_accept_filter_str, afap->af_name);
1049 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1050 if (af->so_accept_filter_arg == NULL) {
1051 FREE(af->so_accept_filter_str, M_ACCF);
1058 af->so_accept_filter = afp;
1060 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_td != NULL)
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_pr_ctloutput(so, sopt));
1117 error = ENOPROTOOPT;
1119 switch (sopt->sopt_name) {
1121 case SO_ACCEPTFILTER:
1122 error = do_setopt_accept_filter(so, sopt);
1128 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1132 so->so_linger = l.l_linger;
1134 so->so_options |= SO_LINGER;
1136 so->so_options &= ~SO_LINGER;
1142 case SO_USELOOPBACK:
1148 error = sooptcopyin(sopt, &optval, sizeof optval,
1153 so->so_options |= sopt->sopt_name;
1155 so->so_options &= ~sopt->sopt_name;
1162 error = sooptcopyin(sopt, &optval, sizeof optval,
1168 * Values < 1 make no sense for any of these
1169 * options, so disallow them.
1176 switch (sopt->sopt_name) {
1179 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1180 &so->so_snd : &so->so_rcv, (u_long)optval,
1182 &curproc->p_rlimit[RLIMIT_SBSIZE]) == 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) {
1225 if (val == 0 && tv.tv_usec != 0)
1228 switch (sopt->sopt_name) {
1230 so->so_snd.sb_timeo = val;
1233 so->so_rcv.sb_timeo = val;
1238 error = ENOPROTOOPT;
1241 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1242 (void) so_pr_ctloutput(so, sopt);
1249 /* Helper routine for getsockopt */
1251 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
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_td != NULL)
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_pr_ctloutput(so, sopt));
1293 return (ENOPROTOOPT);
1295 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));
1314 l.l_onoff = so->so_options & SO_LINGER;
1315 l.l_linger = so->so_linger;
1316 error = sooptcopyout(sopt, &l, sizeof l);
1319 case SO_USELOOPBACK:
1328 optval = so->so_options & sopt->sopt_name;
1330 error = sooptcopyout(sopt, &optval, sizeof optval);
1334 optval = so->so_type;
1338 optval = so->so_error;
1343 optval = so->so_snd.sb_hiwat;
1347 optval = so->so_rcv.sb_hiwat;
1351 optval = so->so_snd.sb_lowat;
1355 optval = so->so_rcv.sb_lowat;
1360 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1361 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1363 tv.tv_sec = optval / hz;
1364 tv.tv_usec = (optval % hz) * tick;
1365 error = sooptcopyout(sopt, &tv, sizeof tv);
1369 error = ENOPROTOOPT;
1376 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1378 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1380 struct mbuf *m, *m_prev;
1381 int sopt_size = sopt->sopt_valsize;
1383 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
1386 if (sopt_size > MLEN) {
1387 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1388 if ((m->m_flags & M_EXT) == 0) {
1392 m->m_len = min(MCLBYTES, sopt_size);
1394 m->m_len = min(MLEN, sopt_size);
1396 sopt_size -= m->m_len;
1401 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
1406 if (sopt_size > MLEN) {
1407 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1408 if ((m->m_flags & M_EXT) == 0) {
1412 m->m_len = min(MCLBYTES, sopt_size);
1414 m->m_len = min(MLEN, sopt_size);
1416 sopt_size -= m->m_len;
1423 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1425 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1427 struct mbuf *m0 = m;
1429 if (sopt->sopt_val == NULL)
1431 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1432 if (sopt->sopt_td != NULL) {
1435 error = copyin(sopt->sopt_val, mtod(m, char *),
1442 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1443 sopt->sopt_valsize -= m->m_len;
1444 (caddr_t)sopt->sopt_val += m->m_len;
1447 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1448 panic("ip6_sooptmcopyin");
1452 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1454 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1456 struct mbuf *m0 = m;
1459 if (sopt->sopt_val == NULL)
1461 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1462 if (sopt->sopt_td != NULL) {
1465 error = copyout(mtod(m, char *), sopt->sopt_val,
1472 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1473 sopt->sopt_valsize -= m->m_len;
1474 (caddr_t)sopt->sopt_val += m->m_len;
1475 valsize += m->m_len;
1479 /* enough soopt buffer should be given from user-land */
1483 sopt->sopt_valsize = valsize;
1491 if (so->so_sigio != NULL)
1492 pgsigio(so->so_sigio, SIGURG, 0);
1493 selwakeup(&so->so_rcv.sb_sel);
1497 sopoll(struct socket *so, int events, struct ucred *cred, struct thread *td)
1502 if (events & (POLLIN | POLLRDNORM))
1504 revents |= events & (POLLIN | POLLRDNORM);
1506 if (events & POLLINIGNEOF)
1507 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
1508 !TAILQ_EMPTY(&so->so_comp) || so->so_error)
1509 revents |= POLLINIGNEOF;
1511 if (events & (POLLOUT | POLLWRNORM))
1512 if (sowriteable(so))
1513 revents |= events & (POLLOUT | POLLWRNORM);
1515 if (events & (POLLPRI | POLLRDBAND))
1516 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1517 revents |= events & (POLLPRI | POLLRDBAND);
1521 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM |
1523 selrecord(td, &so->so_rcv.sb_sel);
1524 so->so_rcv.sb_flags |= SB_SEL;
1527 if (events & (POLLOUT | POLLWRNORM)) {
1528 selrecord(td, &so->so_snd.sb_sel);
1529 so->so_snd.sb_flags |= SB_SEL;
1538 sokqfilter(struct file *fp, struct knote *kn)
1540 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1544 switch (kn->kn_filter) {
1546 if (so->so_options & SO_ACCEPTCONN)
1547 kn->kn_fop = &solisten_filtops;
1549 kn->kn_fop = &soread_filtops;
1553 kn->kn_fop = &sowrite_filtops;
1561 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext);
1562 sb->sb_flags |= SB_KNOTE;
1568 filt_sordetach(struct knote *kn)
1570 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1573 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1574 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1575 so->so_rcv.sb_flags &= ~SB_KNOTE;
1581 filt_soread(struct knote *kn, long hint)
1583 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1585 kn->kn_data = so->so_rcv.sb_cc;
1586 if (so->so_state & SS_CANTRCVMORE) {
1587 kn->kn_flags |= EV_EOF;
1588 kn->kn_fflags = so->so_error;
1591 if (so->so_error) /* temporary udp error */
1593 if (kn->kn_sfflags & NOTE_LOWAT)
1594 return (kn->kn_data >= kn->kn_sdata);
1595 return (kn->kn_data >= so->so_rcv.sb_lowat);
1599 filt_sowdetach(struct knote *kn)
1601 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1604 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1605 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1606 so->so_snd.sb_flags &= ~SB_KNOTE;
1612 filt_sowrite(struct knote *kn, long hint)
1614 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1616 kn->kn_data = sbspace(&so->so_snd);
1617 if (so->so_state & SS_CANTSENDMORE) {
1618 kn->kn_flags |= EV_EOF;
1619 kn->kn_fflags = so->so_error;
1622 if (so->so_error) /* temporary udp error */
1624 if (((so->so_state & SS_ISCONNECTED) == 0) &&
1625 (so->so_proto->pr_flags & PR_CONNREQUIRED))
1627 if (kn->kn_sfflags & NOTE_LOWAT)
1628 return (kn->kn_data >= kn->kn_sdata);
1629 return (kn->kn_data >= so->so_snd.sb_lowat);
1634 filt_solisten(struct knote *kn, long hint)
1636 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1638 kn->kn_data = so->so_qlen;
1639 return (! TAILQ_EMPTY(&so->so_comp));