2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
45 #include <sys/mutex.h>
46 #include <sys/domain.h>
47 #include <sys/file.h> /* for struct knote */
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/event.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/resourcevar.h>
57 #include <sys/signalvar.h>
58 #include <sys/sysctl.h>
62 #include <vm/vm_zone.h>
64 #include <machine/limits.h>
67 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
70 static void filt_sordetach(struct knote *kn);
71 static int filt_soread(struct knote *kn, long hint);
72 static void filt_sowdetach(struct knote *kn);
73 static int filt_sowrite(struct knote *kn, long hint);
74 static int filt_solisten(struct knote *kn, long hint);
76 static struct filterops solisten_filtops =
77 { 1, NULL, filt_sordetach, filt_solisten };
78 static struct filterops soread_filtops =
79 { 1, NULL, filt_sordetach, filt_soread };
80 static struct filterops sowrite_filtops =
81 { 1, NULL, filt_sowdetach, filt_sowrite };
83 vm_zone_t socket_zone;
84 so_gen_t so_gencnt; /* generation count for sockets */
86 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
87 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
89 SYSCTL_DECL(_kern_ipc);
91 static int somaxconn = SOMAXCONN;
92 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
93 &somaxconn, 0, "Maximum pending socket connection queue size");
94 static int numopensockets;
95 SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
96 &numopensockets, 0, "Number of open sockets");
100 * Socket operation routines.
101 * These routines are called by the routines in
102 * sys_socket.c or from a system process, and
103 * implement the semantics of socket operations by
104 * switching out to the protocol specific routines.
108 * Get a socket structure from our zone, and initialize it.
109 * We don't implement `waitok' yet (see comments in uipc_domain.c).
110 * Note that it would probably be better to allocate socket
111 * and PCB at the same time, but I'm not convinced that all
112 * the protocols can be easily modified to do this.
114 * soalloc() returns a socket with a ref count of 0.
122 so = zalloc(socket_zone);
124 /* XXX race condition for reentrant kernel */
125 bzero(so, sizeof *so);
126 so->so_gencnt = ++so_gencnt;
127 so->so_zone = socket_zone;
128 /* sx_init(&so->so_sxlock, "socket sxlock"); */
129 TAILQ_INIT(&so->so_aiojobq);
136 * socreate returns a socket with a ref count of 1. The socket should be
137 * closed with soclose().
140 socreate(dom, aso, type, proto, cred, td)
148 register struct protosw *prp;
149 register struct socket *so;
153 prp = pffindproto(dom, proto, type);
155 prp = pffindtype(dom, type);
157 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
158 return (EPROTONOSUPPORT);
160 if (jailed(td->td_ucred) && jail_socket_unixiproute_only &&
161 prp->pr_domain->dom_family != PF_LOCAL &&
162 prp->pr_domain->dom_family != PF_INET &&
163 prp->pr_domain->dom_family != PF_ROUTE) {
164 return (EPROTONOSUPPORT);
167 if (prp->pr_type != type)
169 so = soalloc(td != 0);
173 TAILQ_INIT(&so->so_incomp);
174 TAILQ_INIT(&so->so_comp);
176 so->so_cred = crhold(cred);
179 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
181 so->so_state |= SS_NOFDREF;
192 struct sockaddr *nam;
198 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
204 sodealloc(struct socket *so)
207 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
208 so->so_gencnt = ++so_gencnt;
209 if (so->so_rcv.sb_hiwat)
210 (void)chgsbsize(so->so_cred->cr_uidinfo,
211 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
212 if (so->so_snd.sb_hiwat)
213 (void)chgsbsize(so->so_cred->cr_uidinfo,
214 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
216 if (so->so_accf != NULL) {
217 if (so->so_accf->so_accept_filter != NULL &&
218 so->so_accf->so_accept_filter->accf_destroy != NULL) {
219 so->so_accf->so_accept_filter->accf_destroy(so);
221 if (so->so_accf->so_accept_filter_str != NULL)
222 FREE(so->so_accf->so_accept_filter_str, M_ACCF);
223 FREE(so->so_accf, M_ACCF);
227 /* sx_destroy(&so->so_sxlock); */
228 zfree(so->so_zone, so);
233 solisten(so, backlog, td)
234 register struct socket *so;
241 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, td);
246 if (TAILQ_EMPTY(&so->so_comp))
247 so->so_options |= SO_ACCEPTCONN;
248 if (backlog < 0 || backlog > somaxconn)
250 so->so_qlimit = backlog;
257 register struct socket *so;
259 struct socket *head = so->so_head;
261 KASSERT(so->so_count == 0, ("socket %p so_count not 0", so));
263 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
266 if (so->so_state & SS_INCOMP) {
267 TAILQ_REMOVE(&head->so_incomp, so, so_list);
269 } else if (so->so_state & SS_COMP) {
271 * We must not decommission a socket that's
272 * on the accept(2) queue. If we do, then
273 * accept(2) may hang after select(2) indicated
274 * that the listening socket was ready.
278 panic("sofree: not queued");
281 so->so_state &= ~SS_INCOMP;
284 sbrelease(&so->so_snd, so);
290 * Close a socket on last file table reference removal.
291 * Initiate disconnect if connected.
292 * Free socket when disconnect complete.
294 * This function will sorele() the socket. Note that soclose() may be
295 * called prior to the ref count reaching zero. The actual socket
296 * structure will not be freed until the ref count reaches zero.
300 register struct socket *so;
302 int s = splnet(); /* conservative */
305 funsetown(so->so_sigio);
306 if (so->so_options & SO_ACCEPTCONN) {
307 struct socket *sp, *sonext;
309 sp = TAILQ_FIRST(&so->so_incomp);
310 for (; sp != NULL; sp = sonext) {
311 sonext = TAILQ_NEXT(sp, so_list);
314 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
315 sonext = TAILQ_NEXT(sp, so_list);
316 /* Dequeue from so_comp since sofree() won't do it */
317 TAILQ_REMOVE(&so->so_comp, sp, so_list);
319 sp->so_state &= ~SS_COMP;
326 if (so->so_state & SS_ISCONNECTED) {
327 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
328 error = sodisconnect(so);
332 if (so->so_options & SO_LINGER) {
333 if ((so->so_state & SS_ISDISCONNECTING) &&
334 (so->so_state & SS_NBIO))
336 while (so->so_state & SS_ISCONNECTED) {
337 error = tsleep((caddr_t)&so->so_timeo,
338 PSOCK | PCATCH, "soclos", so->so_linger * hz);
346 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
351 if (so->so_state & SS_NOFDREF)
352 panic("soclose: NOFDREF");
353 so->so_state |= SS_NOFDREF;
360 * Must be called at splnet...
368 error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
370 sotryfree(so); /* note: does not decrement the ref count */
378 register struct socket *so;
379 struct sockaddr **nam;
384 if ((so->so_state & SS_NOFDREF) == 0)
385 panic("soaccept: !NOFDREF");
386 so->so_state &= ~SS_NOFDREF;
387 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
393 soconnect(so, nam, td)
394 register struct socket *so;
395 struct sockaddr *nam;
401 if (so->so_options & SO_ACCEPTCONN)
405 * If protocol is connection-based, can only connect once.
406 * Otherwise, if connected, try to disconnect first.
407 * This allows user to disconnect by connecting to, e.g.,
410 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
411 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
412 (error = sodisconnect(so))))
415 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
422 register struct socket *so1;
428 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
435 register struct socket *so;
440 if ((so->so_state & SS_ISCONNECTED) == 0) {
444 if (so->so_state & SS_ISDISCONNECTING) {
448 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
454 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
457 * If send must go all at once and message is larger than
458 * send buffering, then hard error.
459 * Lock against other senders.
460 * If must go all at once and not enough room now, then
461 * inform user that this would block and do nothing.
462 * Otherwise, if nonblocking, send as much as possible.
463 * The data to be sent is described by "uio" if nonzero,
464 * otherwise by the mbuf chain "top" (which must be null
465 * if uio is not). Data provided in mbuf chain must be small
466 * enough to send all at once.
468 * Returns nonzero on error, timeout or signal; callers
469 * must check for short counts if EINTR/ERESTART are returned.
470 * Data and control buffers are freed on return.
473 sosend(so, addr, uio, top, control, flags, td)
474 register struct socket *so;
475 struct sockaddr *addr;
478 struct mbuf *control;
483 register struct mbuf *m;
484 register long space, len, resid;
485 int clen = 0, error, s, dontroute, mlen;
486 int atomic = sosendallatonce(so) || top;
489 resid = uio->uio_resid;
491 resid = top->m_pkthdr.len;
493 * In theory resid should be unsigned.
494 * However, space must be signed, as it might be less than 0
495 * if we over-committed, and we must use a signed comparison
496 * of space and resid. On the other hand, a negative resid
497 * causes us to loop sending 0-length segments to the protocol.
499 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
500 * type sockets since that's an error.
502 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
508 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
509 (so->so_proto->pr_flags & PR_ATOMIC);
511 td->td_proc->p_stats->p_ru.ru_msgsnd++;
513 clen = control->m_len;
514 #define snderr(errno) { error = errno; splx(s); goto release; }
517 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
522 if (so->so_state & SS_CANTSENDMORE)
525 error = so->so_error;
530 if ((so->so_state & SS_ISCONNECTED) == 0) {
532 * `sendto' and `sendmsg' is allowed on a connection-
533 * based socket if it supports implied connect.
534 * Return ENOTCONN if not connected and no address is
537 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
538 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
539 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
540 !(resid == 0 && clen != 0))
542 } else if (addr == 0)
543 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
544 ENOTCONN : EDESTADDRREQ);
546 space = sbspace(&so->so_snd);
549 if ((atomic && resid > so->so_snd.sb_hiwat) ||
550 clen > so->so_snd.sb_hiwat)
552 if (space < resid + clen &&
553 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
554 if (so->so_state & SS_NBIO)
556 sbunlock(&so->so_snd);
557 error = sbwait(&so->so_snd);
569 * Data is prepackaged in "top".
573 top->m_flags |= M_EOR;
576 MGETHDR(m, M_TRYWAIT, MT_DATA);
583 m->m_pkthdr.rcvif = (struct ifnet *)0;
585 MGET(m, M_TRYWAIT, MT_DATA);
592 if (resid >= MINCLSIZE) {
593 MCLGET(m, M_TRYWAIT);
594 if ((m->m_flags & M_EXT) == 0)
597 len = min(min(mlen, resid), space);
600 len = min(min(mlen, resid), space);
602 * For datagram protocols, leave room
603 * for protocol headers in first mbuf.
605 if (atomic && top == 0 && len < mlen)
609 error = uiomove(mtod(m, caddr_t), (int)len, uio);
610 resid = uio->uio_resid;
613 top->m_pkthdr.len += len;
619 top->m_flags |= M_EOR;
622 } while (space > 0 && atomic);
624 so->so_options |= SO_DONTROUTE;
625 s = splnet(); /* XXX */
627 * XXX all the SS_CANTSENDMORE checks previously
628 * done could be out of date. We could have recieved
629 * a reset packet in an interrupt or maybe we slept
630 * while doing page faults in uiomove() etc. We could
631 * probably recheck again inside the splnet() protection
632 * here, but there are probably other places that this
633 * also happens. We must rethink this.
635 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
636 (flags & MSG_OOB) ? PRUS_OOB :
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 ((flags & MSG_EOF) &&
643 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
646 /* If there is more to send set PRUS_MORETOCOME */
647 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
648 top, addr, control, td);
651 so->so_options &= ~SO_DONTROUTE;
658 } while (resid && space > 0);
662 sbunlock(&so->so_snd);
672 * Implement receive operations on a socket.
673 * We depend on the way that records are added to the sockbuf
674 * by sbappend*. In particular, each record (mbufs linked through m_next)
675 * must begin with an address if the protocol so specifies,
676 * followed by an optional mbuf or mbufs containing ancillary data,
677 * and then zero or more mbufs of data.
678 * In order to avoid blocking network interrupts for the entire time here,
679 * we splx() while doing the actual copy to user space.
680 * Although the sockbuf is locked, new data may still be appended,
681 * and thus we must maintain consistency of the sockbuf during that time.
683 * The caller may receive the data as a single mbuf chain by supplying
684 * an mbuf **mp0 for use in returning the chain. The uio is then used
685 * only for the count in uio_resid.
688 soreceive(so, psa, uio, mp0, controlp, flagsp)
689 register struct socket *so;
690 struct sockaddr **psa;
693 struct mbuf **controlp;
696 struct mbuf *m, **mp;
697 register int flags, len, error, s, offset;
698 struct protosw *pr = so->so_proto;
699 struct mbuf *nextrecord;
701 int orig_resid = uio->uio_resid;
709 flags = *flagsp &~ MSG_EOR;
712 if (flags & MSG_OOB) {
713 m = m_get(M_TRYWAIT, MT_DATA);
716 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
720 error = uiomove(mtod(m, caddr_t),
721 (int) min(uio->uio_resid, m->m_len), uio);
723 } while (uio->uio_resid && error == 0 && m);
730 *mp = (struct mbuf *)0;
731 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
732 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
735 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
740 m = so->so_rcv.sb_mb;
742 * If we have less data than requested, block awaiting more
743 * (subject to any timeout) if:
744 * 1. the current count is less than the low water mark, or
745 * 2. MSG_WAITALL is set, and it is possible to do the entire
746 * receive operation at once if we block (resid <= hiwat).
747 * 3. MSG_DONTWAIT is not set
748 * If MSG_WAITALL is set but resid is larger than the receive buffer,
749 * we have to do the receive in sections, and thus risk returning
750 * a short count if a timeout or signal occurs after we start.
752 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
753 so->so_rcv.sb_cc < uio->uio_resid) &&
754 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
755 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
756 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
757 KASSERT(m != 0 || !so->so_rcv.sb_cc,
758 ("receive: m == %p so->so_rcv.sb_cc == %lu",
759 m, so->so_rcv.sb_cc));
763 error = so->so_error;
764 if ((flags & MSG_PEEK) == 0)
768 if (so->so_state & SS_CANTRCVMORE) {
774 for (; m; m = m->m_next)
775 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
776 m = so->so_rcv.sb_mb;
779 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
780 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
784 if (uio->uio_resid == 0)
786 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
790 sbunlock(&so->so_rcv);
791 error = sbwait(&so->so_rcv);
799 uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++;
800 nextrecord = m->m_nextpkt;
801 if (pr->pr_flags & PR_ADDR) {
802 KASSERT(m->m_type == MT_SONAME,
803 ("m->m_type == %d", m->m_type));
806 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
808 if (flags & MSG_PEEK) {
811 sbfree(&so->so_rcv, m);
812 so->so_rcv.sb_mb = m_free(m);
813 m = so->so_rcv.sb_mb;
816 while (m && m->m_type == MT_CONTROL && error == 0) {
817 if (flags & MSG_PEEK) {
819 *controlp = m_copy(m, 0, m->m_len);
822 sbfree(&so->so_rcv, m);
823 so->so_rcv.sb_mb = m->m_next;
825 if (pr->pr_domain->dom_externalize)
827 (*pr->pr_domain->dom_externalize)(m, controlp);
832 m = so->so_rcv.sb_mb;
837 controlp = &(*controlp)->m_next;
838 while (*controlp != NULL);
842 if ((flags & MSG_PEEK) == 0)
843 m->m_nextpkt = nextrecord;
845 if (type == MT_OOBDATA)
850 while (m && uio->uio_resid > 0 && error == 0) {
851 if (m->m_type == MT_OOBDATA) {
852 if (type != MT_OOBDATA)
854 } else if (type == MT_OOBDATA)
857 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
858 ("m->m_type == %d", m->m_type));
859 so->so_state &= ~SS_RCVATMARK;
860 len = uio->uio_resid;
861 if (so->so_oobmark && len > so->so_oobmark - offset)
862 len = so->so_oobmark - offset;
863 if (len > m->m_len - moff)
864 len = m->m_len - moff;
866 * If mp is set, just pass back the mbufs.
867 * Otherwise copy them out via the uio, then free.
868 * Sockbuf must be consistent here (points to current mbuf,
869 * it points to next record) when we drop priority;
870 * we must note any additions to the sockbuf when we
871 * block interrupts again.
875 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
880 uio->uio_resid -= len;
881 if (len == m->m_len - moff) {
882 if (m->m_flags & M_EOR)
884 if (flags & MSG_PEEK) {
888 nextrecord = m->m_nextpkt;
889 sbfree(&so->so_rcv, m);
893 so->so_rcv.sb_mb = m = m->m_next;
894 *mp = (struct mbuf *)0;
896 so->so_rcv.sb_mb = m_free(m);
897 m = so->so_rcv.sb_mb;
900 m->m_nextpkt = nextrecord;
903 if (flags & MSG_PEEK)
907 *mp = m_copym(m, 0, len, M_TRYWAIT);
910 so->so_rcv.sb_cc -= len;
913 if (so->so_oobmark) {
914 if ((flags & MSG_PEEK) == 0) {
915 so->so_oobmark -= len;
916 if (so->so_oobmark == 0) {
917 so->so_state |= SS_RCVATMARK;
922 if (offset == so->so_oobmark)
929 * If the MSG_WAITALL flag is set (for non-atomic socket),
930 * we must not quit until "uio->uio_resid == 0" or an error
931 * termination. If a signal/timeout occurs, return
932 * with a short count but without error.
933 * Keep sockbuf locked against other readers.
935 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
936 !sosendallatonce(so) && !nextrecord) {
937 if (so->so_error || so->so_state & SS_CANTRCVMORE)
940 * Notify the protocol that some data has been
941 * drained before blocking.
943 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
944 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
945 error = sbwait(&so->so_rcv);
947 sbunlock(&so->so_rcv);
951 m = so->so_rcv.sb_mb;
953 nextrecord = m->m_nextpkt;
957 if (m && pr->pr_flags & PR_ATOMIC) {
959 if ((flags & MSG_PEEK) == 0)
960 (void) sbdroprecord(&so->so_rcv);
962 if ((flags & MSG_PEEK) == 0) {
964 so->so_rcv.sb_mb = nextrecord;
965 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
966 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
968 if (orig_resid == uio->uio_resid && orig_resid &&
969 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
970 sbunlock(&so->so_rcv);
978 sbunlock(&so->so_rcv);
985 register struct socket *so;
988 register struct protosw *pr = so->so_proto;
990 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
996 return ((*pr->pr_usrreqs->pru_shutdown)(so));
1002 register struct socket *so;
1004 register struct sockbuf *sb = &so->so_rcv;
1005 register struct protosw *pr = so->so_proto;
1009 sb->sb_flags |= SB_NOINTR;
1010 (void) sblock(sb, M_WAITOK);
1015 bzero((caddr_t)sb, sizeof (*sb));
1017 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
1018 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
1019 sbrelease(&asb, so);
1024 do_setopt_accept_filter(so, sopt)
1026 struct sockopt *sopt;
1028 struct accept_filter_arg *afap = NULL;
1029 struct accept_filter *afp;
1030 struct so_accf *af = so->so_accf;
1033 /* do not set/remove accept filters on non listen sockets */
1034 if ((so->so_options & SO_ACCEPTCONN) == 0) {
1039 /* removing the filter */
1042 if (af->so_accept_filter != NULL &&
1043 af->so_accept_filter->accf_destroy != NULL) {
1044 af->so_accept_filter->accf_destroy(so);
1046 if (af->so_accept_filter_str != NULL) {
1047 FREE(af->so_accept_filter_str, M_ACCF);
1052 so->so_options &= ~SO_ACCEPTFILTER;
1055 /* adding a filter */
1056 /* must remove previous filter first */
1061 /* don't put large objects on the kernel stack */
1062 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1063 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1064 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1065 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1068 afp = accept_filt_get(afap->af_name);
1073 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK | M_ZERO);
1074 if (afp->accf_create != NULL) {
1075 if (afap->af_name[0] != '\0') {
1076 int len = strlen(afap->af_name) + 1;
1078 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1079 strcpy(af->so_accept_filter_str, afap->af_name);
1081 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1082 if (af->so_accept_filter_arg == NULL) {
1083 FREE(af->so_accept_filter_str, M_ACCF);
1090 af->so_accept_filter = afp;
1092 so->so_options |= SO_ACCEPTFILTER;
1101 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1102 * an additional variant to handle the case where the option value needs
1103 * to be some kind of integer, but not a specific size.
1104 * In addition to their use here, these functions are also called by the
1105 * protocol-level pr_ctloutput() routines.
1108 sooptcopyin(sopt, buf, len, minlen)
1109 struct sockopt *sopt;
1117 * If the user gives us more than we wanted, we ignore it,
1118 * but if we don't get the minimum length the caller
1119 * wants, we return EINVAL. On success, sopt->sopt_valsize
1120 * is set to however much we actually retrieved.
1122 if ((valsize = sopt->sopt_valsize) < minlen)
1125 sopt->sopt_valsize = valsize = len;
1127 if (sopt->sopt_td != 0)
1128 return (copyin(sopt->sopt_val, buf, valsize));
1130 bcopy(sopt->sopt_val, buf, valsize);
1137 struct sockopt *sopt;
1145 if (sopt->sopt_level != SOL_SOCKET) {
1146 if (so->so_proto && so->so_proto->pr_ctloutput)
1147 return ((*so->so_proto->pr_ctloutput)
1149 error = ENOPROTOOPT;
1151 switch (sopt->sopt_name) {
1153 case SO_ACCEPTFILTER:
1154 error = do_setopt_accept_filter(so, sopt);
1160 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1164 so->so_linger = l.l_linger;
1166 so->so_options |= SO_LINGER;
1168 so->so_options &= ~SO_LINGER;
1174 case SO_USELOOPBACK:
1180 error = sooptcopyin(sopt, &optval, sizeof optval,
1185 so->so_options |= sopt->sopt_name;
1187 so->so_options &= ~sopt->sopt_name;
1194 error = sooptcopyin(sopt, &optval, sizeof optval,
1200 * Values < 1 make no sense for any of these
1201 * options, so disallow them.
1208 switch (sopt->sopt_name) {
1211 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1212 &so->so_snd : &so->so_rcv, (u_long)optval,
1213 so, curthread) == 0) {
1220 * Make sure the low-water is never greater than
1224 so->so_snd.sb_lowat =
1225 (optval > so->so_snd.sb_hiwat) ?
1226 so->so_snd.sb_hiwat : optval;
1229 so->so_rcv.sb_lowat =
1230 (optval > so->so_rcv.sb_hiwat) ?
1231 so->so_rcv.sb_hiwat : optval;
1238 error = sooptcopyin(sopt, &tv, sizeof tv,
1243 /* assert(hz > 0); */
1244 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1245 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1249 /* assert(tick > 0); */
1250 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1251 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1252 if (val > SHRT_MAX) {
1257 switch (sopt->sopt_name) {
1259 so->so_snd.sb_timeo = val;
1262 so->so_rcv.sb_timeo = val;
1267 error = ENOPROTOOPT;
1270 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1271 (void) ((*so->so_proto->pr_ctloutput)
1279 /* Helper routine for getsockopt */
1281 sooptcopyout(sopt, buf, len)
1282 struct sockopt *sopt;
1292 * Documented get behavior is that we always return a value,
1293 * possibly truncated to fit in the user's buffer.
1294 * Traditional behavior is that we always tell the user
1295 * precisely how much we copied, rather than something useful
1296 * like the total amount we had available for her.
1297 * Note that this interface is not idempotent; the entire answer must
1298 * generated ahead of time.
1300 valsize = min(len, sopt->sopt_valsize);
1301 sopt->sopt_valsize = valsize;
1302 if (sopt->sopt_val != 0) {
1303 if (sopt->sopt_td != 0)
1304 error = copyout(buf, sopt->sopt_val, valsize);
1306 bcopy(buf, sopt->sopt_val, valsize);
1314 struct sockopt *sopt;
1320 struct accept_filter_arg *afap;
1324 if (sopt->sopt_level != SOL_SOCKET) {
1325 if (so->so_proto && so->so_proto->pr_ctloutput) {
1326 return ((*so->so_proto->pr_ctloutput)
1329 return (ENOPROTOOPT);
1331 switch (sopt->sopt_name) {
1333 case SO_ACCEPTFILTER:
1334 if ((so->so_options & SO_ACCEPTCONN) == 0)
1336 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1337 M_TEMP, M_WAITOK | M_ZERO);
1338 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1339 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1340 if (so->so_accf->so_accept_filter_str != NULL)
1341 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1343 error = sooptcopyout(sopt, afap, sizeof(*afap));
1349 l.l_onoff = so->so_options & SO_LINGER;
1350 l.l_linger = so->so_linger;
1351 error = sooptcopyout(sopt, &l, sizeof l);
1354 case SO_USELOOPBACK:
1363 optval = so->so_options & sopt->sopt_name;
1365 error = sooptcopyout(sopt, &optval, sizeof optval);
1369 optval = so->so_type;
1373 optval = so->so_error;
1378 optval = so->so_snd.sb_hiwat;
1382 optval = so->so_rcv.sb_hiwat;
1386 optval = so->so_snd.sb_lowat;
1390 optval = so->so_rcv.sb_lowat;
1395 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1396 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1398 tv.tv_sec = optval / hz;
1399 tv.tv_usec = (optval % hz) * tick;
1400 error = sooptcopyout(sopt, &tv, sizeof tv);
1404 error = ENOPROTOOPT;
1411 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1413 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1415 struct mbuf *m, *m_prev;
1416 int sopt_size = sopt->sopt_valsize;
1418 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
1421 if (sopt_size > MLEN) {
1422 MCLGET(m, sopt->sopt_td ? M_TRYWAIT : 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;
1436 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
1441 if (sopt_size > MLEN) {
1442 MCLGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT);
1443 if ((m->m_flags & M_EXT) == 0) {
1447 m->m_len = min(MCLBYTES, sopt_size);
1449 m->m_len = min(MLEN, sopt_size);
1451 sopt_size -= m->m_len;
1458 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1460 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1462 struct mbuf *m0 = m;
1464 if (sopt->sopt_val == NULL)
1466 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1467 if (sopt->sopt_td != NULL) {
1470 error = copyin(sopt->sopt_val, mtod(m, char *),
1477 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1478 sopt->sopt_valsize -= m->m_len;
1479 (caddr_t)sopt->sopt_val += m->m_len;
1482 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1483 panic("ip6_sooptmcopyin");
1487 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1489 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1491 struct mbuf *m0 = m;
1494 if (sopt->sopt_val == NULL)
1496 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1497 if (sopt->sopt_td != NULL) {
1500 error = copyout(mtod(m, char *), sopt->sopt_val,
1507 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1508 sopt->sopt_valsize -= m->m_len;
1509 (caddr_t)sopt->sopt_val += m->m_len;
1510 valsize += m->m_len;
1514 /* enough soopt buffer should be given from user-land */
1518 sopt->sopt_valsize = valsize;
1524 register struct socket *so;
1526 if (so->so_sigio != NULL)
1527 pgsigio(so->so_sigio, SIGURG, 0);
1528 selwakeup(&so->so_rcv.sb_sel);
1532 sopoll(struct socket *so, int events, struct ucred *cred, struct thread *td)
1537 if (events & (POLLIN | POLLRDNORM))
1539 revents |= events & (POLLIN | POLLRDNORM);
1541 if (events & POLLINIGNEOF)
1542 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
1543 !TAILQ_EMPTY(&so->so_comp) || so->so_error)
1544 revents |= POLLINIGNEOF;
1546 if (events & (POLLOUT | POLLWRNORM))
1547 if (sowriteable(so))
1548 revents |= events & (POLLOUT | POLLWRNORM);
1550 if (events & (POLLPRI | POLLRDBAND))
1551 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1552 revents |= events & (POLLPRI | POLLRDBAND);
1556 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM |
1558 selrecord(td, &so->so_rcv.sb_sel);
1559 so->so_rcv.sb_flags |= SB_SEL;
1562 if (events & (POLLOUT | POLLWRNORM)) {
1563 selrecord(td, &so->so_snd.sb_sel);
1564 so->so_snd.sb_flags |= SB_SEL;
1573 sokqfilter(struct file *fp, struct knote *kn)
1575 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1579 switch (kn->kn_filter) {
1581 if (so->so_options & SO_ACCEPTCONN)
1582 kn->kn_fop = &solisten_filtops;
1584 kn->kn_fop = &soread_filtops;
1588 kn->kn_fop = &sowrite_filtops;
1596 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext);
1597 sb->sb_flags |= SB_KNOTE;
1603 filt_sordetach(struct knote *kn)
1605 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1608 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1609 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1610 so->so_rcv.sb_flags &= ~SB_KNOTE;
1616 filt_soread(struct knote *kn, long hint)
1618 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1620 kn->kn_data = so->so_rcv.sb_cc;
1621 if (so->so_state & SS_CANTRCVMORE) {
1622 kn->kn_flags |= EV_EOF;
1623 kn->kn_fflags = so->so_error;
1626 if (so->so_error) /* temporary udp error */
1628 if (kn->kn_sfflags & NOTE_LOWAT)
1629 return (kn->kn_data >= kn->kn_sdata);
1630 return (kn->kn_data >= so->so_rcv.sb_lowat);
1634 filt_sowdetach(struct knote *kn)
1636 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1639 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1640 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1641 so->so_snd.sb_flags &= ~SB_KNOTE;
1647 filt_sowrite(struct knote *kn, long hint)
1649 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1651 kn->kn_data = sbspace(&so->so_snd);
1652 if (so->so_state & SS_CANTSENDMORE) {
1653 kn->kn_flags |= EV_EOF;
1654 kn->kn_fflags = so->so_error;
1657 if (so->so_error) /* temporary udp error */
1659 if (((so->so_state & SS_ISCONNECTED) == 0) &&
1660 (so->so_proto->pr_flags & PR_CONNREQUIRED))
1662 if (kn->kn_sfflags & NOTE_LOWAT)
1663 return (kn->kn_data >= kn->kn_sdata);
1664 return (kn->kn_data >= so->so_snd.sb_lowat);
1669 filt_solisten(struct knote *kn, long hint)
1671 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1673 kn->kn_data = so->so_qlen - so->so_incqlen;
1674 return (! TAILQ_EMPTY(&so->so_comp));
1678 socheckuid(struct socket *so, uid_t uid)
1683 if (so->so_cred->cr_uid == uid)