2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
37 * License terms: all terms for the DragonFly license above plus the following:
39 * 4. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
42 * This product includes software developed by Jeffrey M. Hsu
43 * for the DragonFly Project.
45 * This requirement may be waived with permission from Jeffrey Hsu.
46 * This requirement will sunset and may be removed on July 8 2005,
47 * after which the standard DragonFly license (as shown above) will
52 * Copyright (c) 1982, 1986, 1988, 1990, 1993
53 * The Regents of the University of California. All rights reserved.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
84 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.24 2003/11/11 17:18:18 silby Exp $
85 * $DragonFly: src/sys/kern/uipc_socket.c,v 1.25 2004/08/11 02:24:16 dillon Exp $
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/fcntl.h>
93 #include <sys/malloc.h>
95 #include <sys/domain.h>
96 #include <sys/file.h> /* for struct knote */
97 #include <sys/kernel.h>
98 #include <sys/malloc.h>
99 #include <sys/event.h>
100 #include <sys/poll.h>
101 #include <sys/proc.h>
102 #include <sys/protosw.h>
103 #include <sys/socket.h>
104 #include <sys/socketvar.h>
105 #include <sys/socketops.h>
106 #include <sys/resourcevar.h>
107 #include <sys/signalvar.h>
108 #include <sys/sysctl.h>
110 #include <sys/jail.h>
111 #include <vm/vm_zone.h>
113 #include <machine/limits.h>
116 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
119 static void filt_sordetach(struct knote *kn);
120 static int filt_soread(struct knote *kn, long hint);
121 static void filt_sowdetach(struct knote *kn);
122 static int filt_sowrite(struct knote *kn, long hint);
123 static int filt_solisten(struct knote *kn, long hint);
125 static struct filterops solisten_filtops =
126 { 1, NULL, filt_sordetach, filt_solisten };
127 static struct filterops soread_filtops =
128 { 1, NULL, filt_sordetach, filt_soread };
129 static struct filterops sowrite_filtops =
130 { 1, NULL, filt_sowdetach, filt_sowrite };
132 struct vm_zone *socket_zone;
133 so_gen_t so_gencnt; /* generation count for sockets */
135 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
136 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
139 static int somaxconn = SOMAXCONN;
140 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
141 &somaxconn, 0, "Maximum pending socket connection queue size");
144 * Socket operation routines.
145 * These routines are called by the routines in
146 * sys_socket.c or from a system process, and
147 * implement the semantics of socket operations by
148 * switching out to the protocol specific routines.
152 * Get a socket structure from our zone, and initialize it.
153 * We don't implement `waitok' yet (see comments in uipc_domain.c).
154 * Note that it would probably be better to allocate socket
155 * and PCB at the same time, but I'm not convinced that all
156 * the protocols can be easily modified to do this.
164 so = zalloc(socket_zone);
166 /* XXX race condition for reentrant kernel */
167 bzero(so, sizeof *so);
168 so->so_gencnt = ++so_gencnt;
169 TAILQ_INIT(&so->so_aiojobq);
170 TAILQ_INIT(&so->so_rcv.sb_sel.si_mlist);
171 TAILQ_INIT(&so->so_snd.sb_sel.si_mlist);
177 socreate(int dom, struct socket **aso, int type,
178 int proto, struct thread *td)
180 struct proc *p = td->td_proc;
183 struct pru_attach_info ai;
187 prp = pffindproto(dom, proto, type);
189 prp = pffindtype(dom, type);
191 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
192 return (EPROTONOSUPPORT);
194 if (p->p_ucred->cr_prison && jail_socket_unixiproute_only &&
195 prp->pr_domain->dom_family != PF_LOCAL &&
196 prp->pr_domain->dom_family != PF_INET &&
197 prp->pr_domain->dom_family != PF_ROUTE) {
198 return (EPROTONOSUPPORT);
201 if (prp->pr_type != type)
203 so = soalloc(p != 0);
207 TAILQ_INIT(&so->so_incomp);
208 TAILQ_INIT(&so->so_comp);
210 so->so_cred = crhold(p->p_ucred);
212 ai.sb_rlimit = &p->p_rlimit[RLIMIT_SBSIZE];
213 ai.p_ucred = p->p_ucred;
214 ai.fd_rdir = p->p_fd->fd_rdir;
215 error = so_pru_attach(so, proto, &ai);
217 so->so_state |= SS_NOFDREF;
226 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
231 error = so_pru_bind(so, nam, td);
237 sodealloc(struct socket *so)
240 so->so_gencnt = ++so_gencnt;
241 if (so->so_rcv.sb_hiwat)
242 (void)chgsbsize(so->so_cred->cr_uidinfo,
243 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
244 if (so->so_snd.sb_hiwat)
245 (void)chgsbsize(so->so_cred->cr_uidinfo,
246 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
248 /* remove accept filter if present */
249 if (so->so_accf != NULL)
250 do_setopt_accept_filter(so, NULL);
253 zfree(socket_zone, so);
257 solisten(struct socket *so, int backlog, struct thread *td)
262 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING)) {
267 error = so_pru_listen(so, td);
272 if (TAILQ_EMPTY(&so->so_comp))
273 so->so_options |= SO_ACCEPTCONN;
274 if (backlog < 0 || backlog > somaxconn)
276 so->so_qlimit = backlog;
282 sofree(struct socket *so)
284 struct socket *head = so->so_head;
286 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
289 if (so->so_state & SS_INCOMP) {
290 TAILQ_REMOVE(&head->so_incomp, so, so_list);
292 } else if (so->so_state & SS_COMP) {
294 * We must not decommission a socket that's
295 * on the accept(2) queue. If we do, then
296 * accept(2) may hang after select(2) indicated
297 * that the listening socket was ready.
301 panic("sofree: not queued");
303 so->so_state &= ~SS_INCOMP;
306 sbrelease(&so->so_snd, so);
312 * Close a socket on last file table reference removal.
313 * Initiate disconnect if connected.
314 * Free socket when disconnect complete.
317 soclose(struct socket *so)
319 int s = splnet(); /* conservative */
322 funsetown(so->so_sigio);
323 if (so->so_pcb == NULL)
325 if (so->so_state & SS_ISCONNECTED) {
326 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
327 error = sodisconnect(so);
331 if (so->so_options & SO_LINGER) {
332 if ((so->so_state & SS_ISDISCONNECTING) &&
333 (so->so_state & SS_NBIO))
335 while (so->so_state & SS_ISCONNECTED) {
336 error = tsleep((caddr_t)&so->so_timeo,
337 PCATCH, "soclos", so->so_linger * hz);
347 error2 = so_pru_detach(so);
352 if (so->so_options & SO_ACCEPTCONN) {
353 struct socket *sp, *sonext;
355 sp = TAILQ_FIRST(&so->so_incomp);
356 for (; sp != NULL; sp = sonext) {
357 sonext = TAILQ_NEXT(sp, so_list);
360 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
361 sonext = TAILQ_NEXT(sp, so_list);
362 /* Dequeue from so_comp since sofree() won't do it */
363 TAILQ_REMOVE(&so->so_comp, sp, so_list);
365 sp->so_state &= ~SS_COMP;
370 if (so->so_state & SS_NOFDREF)
371 panic("soclose: NOFDREF");
372 so->so_state |= SS_NOFDREF;
379 * Must be called at splnet...
387 error = so_pru_abort(so);
396 soaccept(struct socket *so, struct sockaddr **nam)
401 if ((so->so_state & SS_NOFDREF) == 0)
402 panic("soaccept: !NOFDREF");
403 so->so_state &= ~SS_NOFDREF;
404 error = so_pru_accept(so, nam);
410 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
415 if (so->so_options & SO_ACCEPTCONN)
419 * If protocol is connection-based, can only connect once.
420 * Otherwise, if connected, try to disconnect first.
421 * This allows user to disconnect by connecting to, e.g.,
424 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
425 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
426 (error = sodisconnect(so))))
429 error = so_pru_connect(so, nam, td);
435 soconnect2(struct socket *so1, struct socket *so2)
440 error = so_pru_connect2(so1, so2);
446 sodisconnect(struct socket *so)
451 if ((so->so_state & SS_ISCONNECTED) == 0) {
455 if (so->so_state & SS_ISDISCONNECTING) {
459 error = so_pru_disconnect(so);
465 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
468 * If send must go all at once and message is larger than
469 * send buffering, then hard error.
470 * Lock against other senders.
471 * If must go all at once and not enough room now, then
472 * inform user that this would block and do nothing.
473 * Otherwise, if nonblocking, send as much as possible.
474 * The data to be sent is described by "uio" if nonzero,
475 * otherwise by the mbuf chain "top" (which must be null
476 * if uio is not). Data provided in mbuf chain must be small
477 * enough to send all at once.
479 * Returns nonzero on error, timeout or signal; callers
480 * must check for short counts if EINTR/ERESTART are returned.
481 * Data and control buffers are freed on return.
484 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
485 struct mbuf *top, struct mbuf *control, int flags,
490 long space, len, resid;
491 int clen = 0, error, s, dontroute, mlen;
492 int atomic = sosendallatonce(so) || top;
496 resid = uio->uio_resid;
498 resid = top->m_pkthdr.len;
500 * In theory resid should be unsigned.
501 * However, space must be signed, as it might be less than 0
502 * if we over-committed, and we must use a signed comparison
503 * of space and resid. On the other hand, a negative resid
504 * causes us to loop sending 0-length segments to the protocol.
506 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
507 * type sockets since that's an error.
509 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
515 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
516 (so->so_proto->pr_flags & PR_ATOMIC);
517 if (td->td_proc && td->td_proc->p_stats)
518 td->td_proc->p_stats->p_ru.ru_msgsnd++;
520 clen = control->m_len;
521 #define gotoerr(errno) { error = errno; splx(s); goto release; }
524 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
529 if (so->so_state & SS_CANTSENDMORE)
532 error = so->so_error;
537 if ((so->so_state & SS_ISCONNECTED) == 0) {
539 * `sendto' and `sendmsg' is allowed on a connection-
540 * based socket if it supports implied connect.
541 * Return ENOTCONN if not connected and no address is
544 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
545 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
546 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
547 !(resid == 0 && clen != 0))
549 } else if (addr == 0)
550 gotoerr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
551 ENOTCONN : EDESTADDRREQ);
553 space = sbspace(&so->so_snd);
556 if ((atomic && resid > so->so_snd.sb_hiwat) ||
557 clen > so->so_snd.sb_hiwat)
559 if (space < resid + clen && uio &&
560 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
561 if (so->so_state & SS_NBIO)
562 gotoerr(EWOULDBLOCK);
563 sbunlock(&so->so_snd);
564 error = sbwait(&so->so_snd);
576 * Data is prepackaged in "top".
580 top->m_flags |= M_EOR;
583 MGETHDR(m, MB_WAIT, MT_DATA);
590 m->m_pkthdr.rcvif = (struct ifnet *)0;
592 MGET(m, MB_WAIT, MT_DATA);
599 if (resid >= MINCLSIZE) {
601 if ((m->m_flags & M_EXT) == 0)
604 len = min(min(mlen, resid), space);
607 len = min(min(mlen, resid), space);
609 * For datagram protocols, leave room
610 * for protocol headers in first mbuf.
612 if (atomic && top == 0 && len < mlen)
616 error = uiomove(mtod(m, caddr_t), (int)len, uio);
617 resid = uio->uio_resid;
620 top->m_pkthdr.len += len;
626 top->m_flags |= M_EOR;
629 } while (space > 0 && atomic);
631 so->so_options |= SO_DONTROUTE;
632 if (flags & MSG_OOB) {
633 pru_flags = PRUS_OOB;
634 } else if ((flags & MSG_EOF) &&
635 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
638 * If the user set MSG_EOF, the protocol
639 * understands this flag and nothing left to
640 * send then use PRU_SEND_EOF instead of PRU_SEND.
642 pru_flags = PRUS_EOF;
643 } else if (resid > 0 && space > 0) {
644 /* If there is more to send, set PRUS_MORETOCOME */
645 pru_flags = PRUS_MORETOCOME;
649 s = splnet(); /* XXX */
651 * XXX all the SS_CANTSENDMORE checks previously
652 * done could be out of date. We could have recieved
653 * a reset packet in an interrupt or maybe we slept
654 * while doing page faults in uiomove() etc. We could
655 * probably recheck again inside the splnet() protection
656 * here, but there are probably other places that this
657 * also happens. We must rethink this.
659 error = so_pru_send(so, pru_flags, top, addr, control, td);
662 so->so_options &= ~SO_DONTROUTE;
669 } while (resid && space > 0);
673 sbunlock(&so->so_snd);
683 * A specialization of sosend() for UDP based on protocol-specific knowledge:
684 * so->so_proto->pr_flags has the PR_ATOMIC field set. This means that
685 * sosendallatonce() returns true,
686 * the "atomic" variable is true,
687 * and sosendudp() blocks until space is available for the entire send.
688 * so->so_proto->pr_flags does not have the PR_CONNREQUIRED or
689 * PR_IMPLOPCL flags set.
690 * UDP has no out-of-band data.
691 * UDP has no control data.
692 * UDP does not support MSG_EOR.
695 sosendudp(struct socket *so, struct sockaddr *addr, struct uio *uio,
696 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
699 boolean_t dontroute; /* temporary SO_DONTROUTE setting */
701 if (td->td_proc && td->td_proc->p_stats)
702 td->td_proc->p_stats->p_ru.ru_msgsnd++;
706 KASSERT((uio && !top) || (top && !uio), ("bad arguments to sosendudp"));
707 resid = uio ? uio->uio_resid : top->m_pkthdr.len;
710 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
715 if (so->so_state & SS_CANTSENDMORE)
718 error = so->so_error;
723 if (!(so->so_state & SS_ISCONNECTED) && addr == NULL)
724 gotoerr(EDESTADDRREQ);
725 if (resid > so->so_snd.sb_hiwat)
727 if (uio && sbspace(&so->so_snd) < resid) {
728 if (so->so_state & SS_NBIO)
729 gotoerr(EWOULDBLOCK);
730 sbunlock(&so->so_snd);
731 error = sbwait(&so->so_snd);
740 top = m_uiomove(uio, MB_WAIT, 0);
745 dontroute = (flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE);
747 so->so_options |= SO_DONTROUTE;
749 error = so_pru_send(so, 0, top, addr, NULL, td);
750 top = NULL; /* sent or freed in lower layer */
753 so->so_options &= ~SO_DONTROUTE;
756 sbunlock(&so->so_snd);
764 * Implement receive operations on a socket.
765 * We depend on the way that records are added to the sockbuf
766 * by sbappend*. In particular, each record (mbufs linked through m_next)
767 * must begin with an address if the protocol so specifies,
768 * followed by an optional mbuf or mbufs containing ancillary data,
769 * and then zero or more mbufs of data.
770 * In order to avoid blocking network interrupts for the entire time here,
771 * we splx() while doing the actual copy to user space.
772 * Although the sockbuf is locked, new data may still be appended,
773 * and thus we must maintain consistency of the sockbuf during that time.
775 * The caller may receive the data as a single mbuf chain by supplying
776 * an mbuf **mp0 for use in returning the chain. The uio is then used
777 * only for the count in uio_resid.
780 soreceive(so, psa, uio, mp0, controlp, flagsp)
782 struct sockaddr **psa;
785 struct mbuf **controlp;
788 struct mbuf *m, **mp;
789 int flags, len, error, s, offset;
790 struct protosw *pr = so->so_proto;
791 struct mbuf *nextrecord;
793 int orig_resid = uio->uio_resid;
801 flags = *flagsp &~ MSG_EOR;
804 if (flags & MSG_OOB) {
805 m = m_get(MB_WAIT, MT_DATA);
808 error = so_pru_rcvoob(so, m, flags & MSG_PEEK);
812 error = uiomove(mtod(m, caddr_t),
813 (int) min(uio->uio_resid, m->m_len), uio);
815 } while (uio->uio_resid && error == 0 && m);
822 *mp = (struct mbuf *)0;
823 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
827 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
832 m = so->so_rcv.sb_mb;
834 * If we have less data than requested, block awaiting more
835 * (subject to any timeout) if:
836 * 1. the current count is less than the low water mark, or
837 * 2. MSG_WAITALL is set, and it is possible to do the entire
838 * receive operation at once if we block (resid <= hiwat).
839 * 3. MSG_DONTWAIT is not set
840 * If MSG_WAITALL is set but resid is larger than the receive buffer,
841 * we have to do the receive in sections, and thus risk returning
842 * a short count if a timeout or signal occurs after we start.
844 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
845 so->so_rcv.sb_cc < uio->uio_resid) &&
846 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
847 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
848 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
849 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
853 error = so->so_error;
854 if ((flags & MSG_PEEK) == 0)
858 if (so->so_state & SS_CANTRCVMORE) {
864 for (; m; m = m->m_next)
865 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
866 m = so->so_rcv.sb_mb;
869 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
870 (pr->pr_flags & PR_CONNREQUIRED)) {
874 if (uio->uio_resid == 0)
876 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
880 sbunlock(&so->so_rcv);
881 error = sbwait(&so->so_rcv);
888 if (uio->uio_td && uio->uio_td->td_proc)
889 uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++;
890 nextrecord = m->m_nextpkt;
891 if (pr->pr_flags & PR_ADDR) {
892 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
895 *psa = dup_sockaddr(mtod(m, struct sockaddr *));
896 if (flags & MSG_PEEK) {
899 sbfree(&so->so_rcv, m);
900 so->so_rcv.sb_mb = m_free(m);
901 m = so->so_rcv.sb_mb;
904 while (m && m->m_type == MT_CONTROL && error == 0) {
905 if (flags & MSG_PEEK) {
907 *controlp = m_copy(m, 0, m->m_len);
910 sbfree(&so->so_rcv, m);
912 if (pr->pr_domain->dom_externalize &&
913 mtod(m, struct cmsghdr *)->cmsg_type ==
915 error = (*pr->pr_domain->dom_externalize)(m);
917 so->so_rcv.sb_mb = m->m_next;
919 m = so->so_rcv.sb_mb;
921 so->so_rcv.sb_mb = m_free(m);
922 m = so->so_rcv.sb_mb;
927 controlp = &(*controlp)->m_next;
931 if ((flags & MSG_PEEK) == 0)
932 m->m_nextpkt = nextrecord;
934 if (type == MT_OOBDATA)
939 while (m && uio->uio_resid > 0 && error == 0) {
940 if (m->m_type == MT_OOBDATA) {
941 if (type != MT_OOBDATA)
943 } else if (type == MT_OOBDATA)
946 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
948 so->so_state &= ~SS_RCVATMARK;
949 len = uio->uio_resid;
950 if (so->so_oobmark && len > so->so_oobmark - offset)
951 len = so->so_oobmark - offset;
952 if (len > m->m_len - moff)
953 len = m->m_len - moff;
955 * If mp is set, just pass back the mbufs.
956 * Otherwise copy them out via the uio, then free.
957 * Sockbuf must be consistent here (points to current mbuf,
958 * it points to next record) when we drop priority;
959 * we must note any additions to the sockbuf when we
960 * block interrupts again.
964 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
969 uio->uio_resid -= len;
970 if (len == m->m_len - moff) {
971 if (m->m_flags & M_EOR)
973 if (flags & MSG_PEEK) {
977 nextrecord = m->m_nextpkt;
978 sbfree(&so->so_rcv, m);
982 so->so_rcv.sb_mb = m = m->m_next;
983 *mp = (struct mbuf *)0;
985 so->so_rcv.sb_mb = m = m_free(m);
988 m->m_nextpkt = nextrecord;
991 if (flags & MSG_PEEK)
995 *mp = m_copym(m, 0, len, MB_WAIT);
998 so->so_rcv.sb_cc -= len;
1001 if (so->so_oobmark) {
1002 if ((flags & MSG_PEEK) == 0) {
1003 so->so_oobmark -= len;
1004 if (so->so_oobmark == 0) {
1005 so->so_state |= SS_RCVATMARK;
1010 if (offset == so->so_oobmark)
1014 if (flags & MSG_EOR)
1017 * If the MSG_WAITALL flag is set (for non-atomic socket),
1018 * we must not quit until "uio->uio_resid == 0" or an error
1019 * termination. If a signal/timeout occurs, return
1020 * with a short count but without error.
1021 * Keep sockbuf locked against other readers.
1023 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
1024 !sosendallatonce(so) && !nextrecord) {
1025 if (so->so_error || so->so_state & SS_CANTRCVMORE)
1028 * The window might have closed to zero, make
1029 * sure we send an ack now that we've drained
1030 * the buffer or we might end up blocking until
1031 * the idle takes over (5 seconds).
1033 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1034 so_pru_rcvd(so, flags);
1035 error = sbwait(&so->so_rcv);
1037 sbunlock(&so->so_rcv);
1041 m = so->so_rcv.sb_mb;
1043 nextrecord = m->m_nextpkt;
1047 if (m && pr->pr_flags & PR_ATOMIC) {
1049 if ((flags & MSG_PEEK) == 0)
1050 (void) sbdroprecord(&so->so_rcv);
1052 if ((flags & MSG_PEEK) == 0) {
1054 so->so_rcv.sb_mb = nextrecord;
1055 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1056 so_pru_rcvd(so, flags);
1058 if (orig_resid == uio->uio_resid && orig_resid &&
1059 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
1060 sbunlock(&so->so_rcv);
1068 sbunlock(&so->so_rcv);
1078 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1084 return (so_pru_shutdown(so));
1092 struct sockbuf *sb = &so->so_rcv;
1093 struct protosw *pr = so->so_proto;
1097 sb->sb_flags |= SB_NOINTR;
1098 (void) sblock(sb, M_WAITOK);
1103 bzero((caddr_t)sb, sizeof (*sb));
1104 if (asb.sb_flags & SB_KNOTE) {
1105 sb->sb_sel.si_note = asb.sb_sel.si_note;
1106 sb->sb_flags = SB_KNOTE;
1109 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
1110 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
1111 sbrelease(&asb, so);
1116 do_setopt_accept_filter(so, sopt)
1118 struct sockopt *sopt;
1120 struct accept_filter_arg *afap = NULL;
1121 struct accept_filter *afp;
1122 struct so_accf *af = so->so_accf;
1125 /* do not set/remove accept filters on non listen sockets */
1126 if ((so->so_options & SO_ACCEPTCONN) == 0) {
1131 /* removing the filter */
1134 if (af->so_accept_filter != NULL &&
1135 af->so_accept_filter->accf_destroy != NULL) {
1136 af->so_accept_filter->accf_destroy(so);
1138 if (af->so_accept_filter_str != NULL) {
1139 FREE(af->so_accept_filter_str, M_ACCF);
1144 so->so_options &= ~SO_ACCEPTFILTER;
1147 /* adding a filter */
1148 /* must remove previous filter first */
1153 /* don't put large objects on the kernel stack */
1154 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1155 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1156 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1157 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1160 afp = accept_filt_get(afap->af_name);
1165 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK);
1166 bzero(af, sizeof(*af));
1167 if (afp->accf_create != NULL) {
1168 if (afap->af_name[0] != '\0') {
1169 int len = strlen(afap->af_name) + 1;
1171 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1172 strcpy(af->so_accept_filter_str, afap->af_name);
1174 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1175 if (af->so_accept_filter_arg == NULL) {
1176 FREE(af->so_accept_filter_str, M_ACCF);
1183 af->so_accept_filter = afp;
1185 so->so_options |= SO_ACCEPTFILTER;
1194 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1195 * an additional variant to handle the case where the option value needs
1196 * to be some kind of integer, but not a specific size.
1197 * In addition to their use here, these functions are also called by the
1198 * protocol-level pr_ctloutput() routines.
1201 sooptcopyin(sopt, buf, len, minlen)
1202 struct sockopt *sopt;
1210 * If the user gives us more than we wanted, we ignore it,
1211 * but if we don't get the minimum length the caller
1212 * wants, we return EINVAL. On success, sopt->sopt_valsize
1213 * is set to however much we actually retrieved.
1215 if ((valsize = sopt->sopt_valsize) < minlen)
1218 sopt->sopt_valsize = valsize = len;
1220 if (sopt->sopt_td != NULL)
1221 return (copyin(sopt->sopt_val, buf, valsize));
1223 bcopy(sopt->sopt_val, buf, valsize);
1230 struct sockopt *sopt;
1238 if (sopt->sopt_level != SOL_SOCKET) {
1239 if (so->so_proto && so->so_proto->pr_ctloutput) {
1240 return (so_pr_ctloutput(so, sopt));
1242 error = ENOPROTOOPT;
1244 switch (sopt->sopt_name) {
1246 case SO_ACCEPTFILTER:
1247 error = do_setopt_accept_filter(so, sopt);
1253 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1257 so->so_linger = l.l_linger;
1259 so->so_options |= SO_LINGER;
1261 so->so_options &= ~SO_LINGER;
1267 case SO_USELOOPBACK:
1273 error = sooptcopyin(sopt, &optval, sizeof optval,
1278 so->so_options |= sopt->sopt_name;
1280 so->so_options &= ~sopt->sopt_name;
1287 error = sooptcopyin(sopt, &optval, sizeof optval,
1293 * Values < 1 make no sense for any of these
1294 * options, so disallow them.
1301 switch (sopt->sopt_name) {
1304 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1305 &so->so_snd : &so->so_rcv, (u_long)optval,
1307 &curproc->p_rlimit[RLIMIT_SBSIZE]) == 0) {
1314 * Make sure the low-water is never greater than
1318 so->so_snd.sb_lowat =
1319 (optval > so->so_snd.sb_hiwat) ?
1320 so->so_snd.sb_hiwat : optval;
1323 so->so_rcv.sb_lowat =
1324 (optval > so->so_rcv.sb_hiwat) ?
1325 so->so_rcv.sb_hiwat : optval;
1332 error = sooptcopyin(sopt, &tv, sizeof tv,
1337 /* assert(hz > 0); */
1338 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1339 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1343 /* assert(tick > 0); */
1344 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1345 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1346 if (val > SHRT_MAX) {
1350 if (val == 0 && tv.tv_usec != 0)
1353 switch (sopt->sopt_name) {
1355 so->so_snd.sb_timeo = val;
1358 so->so_rcv.sb_timeo = val;
1363 error = ENOPROTOOPT;
1366 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1367 (void) so_pr_ctloutput(so, sopt);
1374 /* Helper routine for getsockopt */
1376 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
1384 * Documented get behavior is that we always return a value,
1385 * possibly truncated to fit in the user's buffer.
1386 * Traditional behavior is that we always tell the user
1387 * precisely how much we copied, rather than something useful
1388 * like the total amount we had available for her.
1389 * Note that this interface is not idempotent; the entire answer must
1390 * generated ahead of time.
1392 valsize = min(len, sopt->sopt_valsize);
1393 sopt->sopt_valsize = valsize;
1394 if (sopt->sopt_val != 0) {
1395 if (sopt->sopt_td != NULL)
1396 error = copyout(buf, sopt->sopt_val, valsize);
1398 bcopy(buf, sopt->sopt_val, valsize);
1406 struct sockopt *sopt;
1412 struct accept_filter_arg *afap;
1416 if (sopt->sopt_level != SOL_SOCKET) {
1417 if (so->so_proto && so->so_proto->pr_ctloutput) {
1418 return (so_pr_ctloutput(so, sopt));
1420 return (ENOPROTOOPT);
1422 switch (sopt->sopt_name) {
1424 case SO_ACCEPTFILTER:
1425 if ((so->so_options & SO_ACCEPTCONN) == 0)
1427 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1429 bzero(afap, sizeof(*afap));
1430 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1431 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1432 if (so->so_accf->so_accept_filter_str != NULL)
1433 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1435 error = sooptcopyout(sopt, afap, sizeof(*afap));
1441 l.l_onoff = so->so_options & SO_LINGER;
1442 l.l_linger = so->so_linger;
1443 error = sooptcopyout(sopt, &l, sizeof l);
1446 case SO_USELOOPBACK:
1455 optval = so->so_options & sopt->sopt_name;
1457 error = sooptcopyout(sopt, &optval, sizeof optval);
1461 optval = so->so_type;
1465 optval = so->so_error;
1470 optval = so->so_snd.sb_hiwat;
1474 optval = so->so_rcv.sb_hiwat;
1478 optval = so->so_snd.sb_lowat;
1482 optval = so->so_rcv.sb_lowat;
1487 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1488 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1490 tv.tv_sec = optval / hz;
1491 tv.tv_usec = (optval % hz) * tick;
1492 error = sooptcopyout(sopt, &tv, sizeof tv);
1496 error = ENOPROTOOPT;
1503 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1505 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1507 struct mbuf *m, *m_prev;
1508 int sopt_size = sopt->sopt_valsize;
1510 MGET(m, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_DATA);
1513 if (sopt_size > MLEN) {
1514 MCLGET(m, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT);
1515 if ((m->m_flags & M_EXT) == 0) {
1519 m->m_len = min(MCLBYTES, sopt_size);
1521 m->m_len = min(MLEN, sopt_size);
1523 sopt_size -= m->m_len;
1528 MGET(m, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_DATA);
1533 if (sopt_size > MLEN) {
1534 MCLGET(m, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT);
1535 if ((m->m_flags & M_EXT) == 0) {
1539 m->m_len = min(MCLBYTES, sopt_size);
1541 m->m_len = min(MLEN, sopt_size);
1543 sopt_size -= m->m_len;
1550 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1552 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1554 struct mbuf *m0 = m;
1556 if (sopt->sopt_val == NULL)
1558 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1559 if (sopt->sopt_td != NULL) {
1562 error = copyin(sopt->sopt_val, mtod(m, char *),
1569 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1570 sopt->sopt_valsize -= m->m_len;
1571 sopt->sopt_val = (caddr_t)sopt->sopt_val + m->m_len;
1574 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1575 panic("ip6_sooptmcopyin");
1579 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1581 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1583 struct mbuf *m0 = m;
1586 if (sopt->sopt_val == NULL)
1588 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1589 if (sopt->sopt_td != NULL) {
1592 error = copyout(mtod(m, char *), sopt->sopt_val,
1599 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1600 sopt->sopt_valsize -= m->m_len;
1601 sopt->sopt_val = (caddr_t)sopt->sopt_val + m->m_len;
1602 valsize += m->m_len;
1606 /* enough soopt buffer should be given from user-land */
1610 sopt->sopt_valsize = valsize;
1618 if (so->so_sigio != NULL)
1619 pgsigio(so->so_sigio, SIGURG, 0);
1620 selwakeup(&so->so_rcv.sb_sel);
1624 sopoll(struct socket *so, int events, struct ucred *cred, struct thread *td)
1629 if (events & (POLLIN | POLLRDNORM))
1631 revents |= events & (POLLIN | POLLRDNORM);
1633 if (events & POLLINIGNEOF)
1634 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
1635 !TAILQ_EMPTY(&so->so_comp) || so->so_error)
1636 revents |= POLLINIGNEOF;
1638 if (events & (POLLOUT | POLLWRNORM))
1639 if (sowriteable(so))
1640 revents |= events & (POLLOUT | POLLWRNORM);
1642 if (events & (POLLPRI | POLLRDBAND))
1643 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1644 revents |= events & (POLLPRI | POLLRDBAND);
1648 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM |
1650 selrecord(td, &so->so_rcv.sb_sel);
1651 so->so_rcv.sb_flags |= SB_SEL;
1654 if (events & (POLLOUT | POLLWRNORM)) {
1655 selrecord(td, &so->so_snd.sb_sel);
1656 so->so_snd.sb_flags |= SB_SEL;
1665 sokqfilter(struct file *fp, struct knote *kn)
1667 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1671 switch (kn->kn_filter) {
1673 if (so->so_options & SO_ACCEPTCONN)
1674 kn->kn_fop = &solisten_filtops;
1676 kn->kn_fop = &soread_filtops;
1680 kn->kn_fop = &sowrite_filtops;
1688 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext);
1689 sb->sb_flags |= SB_KNOTE;
1695 filt_sordetach(struct knote *kn)
1697 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1700 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1701 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1702 so->so_rcv.sb_flags &= ~SB_KNOTE;
1708 filt_soread(struct knote *kn, long hint)
1710 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1712 kn->kn_data = so->so_rcv.sb_cc;
1713 if (so->so_state & SS_CANTRCVMORE) {
1714 kn->kn_flags |= EV_EOF;
1715 kn->kn_fflags = so->so_error;
1718 if (so->so_error) /* temporary udp error */
1720 if (kn->kn_sfflags & NOTE_LOWAT)
1721 return (kn->kn_data >= kn->kn_sdata);
1722 return (kn->kn_data >= so->so_rcv.sb_lowat);
1726 filt_sowdetach(struct knote *kn)
1728 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1731 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1732 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1733 so->so_snd.sb_flags &= ~SB_KNOTE;
1739 filt_sowrite(struct knote *kn, long hint)
1741 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1743 kn->kn_data = sbspace(&so->so_snd);
1744 if (so->so_state & SS_CANTSENDMORE) {
1745 kn->kn_flags |= EV_EOF;
1746 kn->kn_fflags = so->so_error;
1749 if (so->so_error) /* temporary udp error */
1751 if (((so->so_state & SS_ISCONNECTED) == 0) &&
1752 (so->so_proto->pr_flags & PR_CONNREQUIRED))
1754 if (kn->kn_sfflags & NOTE_LOWAT)
1755 return (kn->kn_data >= kn->kn_sdata);
1756 return (kn->kn_data >= so->so_snd.sb_lowat);
1761 filt_solisten(struct knote *kn, long hint)
1763 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1765 kn->kn_data = so->so_qlen;
1766 return (! TAILQ_EMPTY(&so->so_comp));