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.13 2004/02/10 15:45:43 hmp Exp $
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/malloc.h>
45 #include <sys/domain.h>
46 #include <sys/file.h> /* for struct knote */
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/event.h>
52 #include <sys/protosw.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/resourcevar.h>
56 #include <sys/signalvar.h>
57 #include <sys/sysctl.h>
60 #include <vm/vm_zone.h>
62 #include <machine/limits.h>
65 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
68 static void filt_sordetach(struct knote *kn);
69 static int filt_soread(struct knote *kn, long hint);
70 static void filt_sowdetach(struct knote *kn);
71 static int filt_sowrite(struct knote *kn, long hint);
72 static int filt_solisten(struct knote *kn, long hint);
74 static struct filterops solisten_filtops =
75 { 1, NULL, filt_sordetach, filt_solisten };
76 static struct filterops soread_filtops =
77 { 1, NULL, filt_sordetach, filt_soread };
78 static struct filterops sowrite_filtops =
79 { 1, NULL, filt_sowdetach, filt_sowrite };
81 struct vm_zone *socket_zone;
82 so_gen_t so_gencnt; /* generation count for sockets */
84 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
85 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
87 SYSCTL_DECL(_kern_ipc);
89 static int somaxconn = SOMAXCONN;
90 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
91 &somaxconn, 0, "Maximum pending socket connection queue size");
94 * Socket operation routines.
95 * These routines are called by the routines in
96 * sys_socket.c or from a system process, and
97 * implement the semantics of socket operations by
98 * switching out to the protocol specific routines.
102 * Get a socket structure from our zone, and initialize it.
103 * We don't implement `waitok' yet (see comments in uipc_domain.c).
104 * Note that it would probably be better to allocate socket
105 * and PCB at the same time, but I'm not convinced that all
106 * the protocols can be easily modified to do this.
114 so = zalloc(socket_zone);
116 /* XXX race condition for reentrant kernel */
117 bzero(so, sizeof *so);
118 so->so_gencnt = ++so_gencnt;
119 TAILQ_INIT(&so->so_aiojobq);
125 socreate(int dom, struct socket **aso, int type,
126 int proto, struct thread *td)
128 struct proc *p = td->td_proc;
134 prp = pffindproto(dom, proto, type);
136 prp = pffindtype(dom, type);
138 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
139 return (EPROTONOSUPPORT);
141 if (p->p_ucred->cr_prison && jail_socket_unixiproute_only &&
142 prp->pr_domain->dom_family != PF_LOCAL &&
143 prp->pr_domain->dom_family != PF_INET &&
144 prp->pr_domain->dom_family != PF_ROUTE) {
145 return (EPROTONOSUPPORT);
148 if (prp->pr_type != type)
150 so = soalloc(p != 0);
154 TAILQ_INIT(&so->so_incomp);
155 TAILQ_INIT(&so->so_comp);
157 so->so_cred = crhold(p->p_ucred);
159 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
161 so->so_state |= SS_NOFDREF;
170 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
175 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
181 sodealloc(struct socket *so)
184 so->so_gencnt = ++so_gencnt;
185 if (so->so_rcv.sb_hiwat)
186 (void)chgsbsize(so->so_cred->cr_uidinfo,
187 &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
188 if (so->so_snd.sb_hiwat)
189 (void)chgsbsize(so->so_cred->cr_uidinfo,
190 &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
192 if (so->so_accf != NULL) {
193 if (so->so_accf->so_accept_filter != NULL &&
194 so->so_accf->so_accept_filter->accf_destroy != NULL) {
195 so->so_accf->so_accept_filter->accf_destroy(so);
197 if (so->so_accf->so_accept_filter_str != NULL)
198 FREE(so->so_accf->so_accept_filter_str, M_ACCF);
199 FREE(so->so_accf, M_ACCF);
203 zfree(socket_zone, so);
207 solisten(struct socket *so, int backlog, struct thread *td)
213 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING)) {
218 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, td);
223 if (TAILQ_EMPTY(&so->so_comp))
224 so->so_options |= SO_ACCEPTCONN;
225 if (backlog < 0 || backlog > somaxconn)
227 so->so_qlimit = backlog;
233 sofree(struct socket *so)
235 struct socket *head = so->so_head;
237 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
240 if (so->so_state & SS_INCOMP) {
241 TAILQ_REMOVE(&head->so_incomp, so, so_list);
243 } else if (so->so_state & SS_COMP) {
245 * We must not decommission a socket that's
246 * on the accept(2) queue. If we do, then
247 * accept(2) may hang after select(2) indicated
248 * that the listening socket was ready.
252 panic("sofree: not queued");
254 so->so_state &= ~SS_INCOMP;
257 sbrelease(&so->so_snd, so);
263 * Close a socket on last file table reference removal.
264 * Initiate disconnect if connected.
265 * Free socket when disconnect complete.
268 soclose(struct socket *so)
270 int s = splnet(); /* conservative */
273 funsetown(so->so_sigio);
274 if (so->so_options & SO_ACCEPTCONN) {
275 struct socket *sp, *sonext;
277 sp = TAILQ_FIRST(&so->so_incomp);
278 for (; sp != NULL; sp = sonext) {
279 sonext = TAILQ_NEXT(sp, so_list);
282 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
283 sonext = TAILQ_NEXT(sp, so_list);
284 /* Dequeue from so_comp since sofree() won't do it */
285 TAILQ_REMOVE(&so->so_comp, sp, so_list);
287 sp->so_state &= ~SS_COMP;
294 if (so->so_state & SS_ISCONNECTED) {
295 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
296 error = sodisconnect(so);
300 if (so->so_options & SO_LINGER) {
301 if ((so->so_state & SS_ISDISCONNECTING) &&
302 (so->so_state & SS_NBIO))
304 while (so->so_state & SS_ISCONNECTED) {
305 error = tsleep((caddr_t)&so->so_timeo,
306 PCATCH, "soclos", so->so_linger * hz);
314 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
319 if (so->so_state & SS_NOFDREF)
320 panic("soclose: NOFDREF");
321 so->so_state |= SS_NOFDREF;
328 * Must be called at splnet...
336 error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
345 soaccept(struct socket *so, struct sockaddr **nam)
350 if ((so->so_state & SS_NOFDREF) == 0)
351 panic("soaccept: !NOFDREF");
352 so->so_state &= ~SS_NOFDREF;
353 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
359 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
364 if (so->so_options & SO_ACCEPTCONN)
368 * If protocol is connection-based, can only connect once.
369 * Otherwise, if connected, try to disconnect first.
370 * This allows user to disconnect by connecting to, e.g.,
373 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
374 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
375 (error = sodisconnect(so))))
378 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
384 soconnect2(struct socket *so1, struct socket *so2)
389 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
395 sodisconnect(struct socket *so)
400 if ((so->so_state & SS_ISCONNECTED) == 0) {
404 if (so->so_state & SS_ISDISCONNECTING) {
408 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
414 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
417 * If send must go all at once and message is larger than
418 * send buffering, then hard error.
419 * Lock against other senders.
420 * If must go all at once and not enough room now, then
421 * inform user that this would block and do nothing.
422 * Otherwise, if nonblocking, send as much as possible.
423 * The data to be sent is described by "uio" if nonzero,
424 * otherwise by the mbuf chain "top" (which must be null
425 * if uio is not). Data provided in mbuf chain must be small
426 * enough to send all at once.
428 * Returns nonzero on error, timeout or signal; callers
429 * must check for short counts if EINTR/ERESTART are returned.
430 * Data and control buffers are freed on return.
433 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
434 struct mbuf *top, struct mbuf *control, int flags,
439 long space, len, resid;
440 int clen = 0, error, s, dontroute, mlen;
441 int atomic = sosendallatonce(so) || top;
444 resid = uio->uio_resid;
446 resid = top->m_pkthdr.len;
448 * In theory resid should be unsigned.
449 * However, space must be signed, as it might be less than 0
450 * if we over-committed, and we must use a signed comparison
451 * of space and resid. On the other hand, a negative resid
452 * causes us to loop sending 0-length segments to the protocol.
454 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
455 * type sockets since that's an error.
457 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
463 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
464 (so->so_proto->pr_flags & PR_ATOMIC);
465 if (td->td_proc && td->td_proc->p_stats)
466 td->td_proc->p_stats->p_ru.ru_msgsnd++;
468 clen = control->m_len;
469 #define snderr(errno) { error = errno; splx(s); goto release; }
472 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
477 if (so->so_state & SS_CANTSENDMORE)
480 error = so->so_error;
485 if ((so->so_state & SS_ISCONNECTED) == 0) {
487 * `sendto' and `sendmsg' is allowed on a connection-
488 * based socket if it supports implied connect.
489 * Return ENOTCONN if not connected and no address is
492 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
493 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
494 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
495 !(resid == 0 && clen != 0))
497 } else if (addr == 0)
498 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
499 ENOTCONN : EDESTADDRREQ);
501 space = sbspace(&so->so_snd);
504 if ((atomic && resid > so->so_snd.sb_hiwat) ||
505 clen > so->so_snd.sb_hiwat)
507 if (space < resid + clen && uio &&
508 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
509 if (so->so_state & SS_NBIO)
511 sbunlock(&so->so_snd);
512 error = sbwait(&so->so_snd);
524 * Data is prepackaged in "top".
528 top->m_flags |= M_EOR;
531 MGETHDR(m, M_WAIT, MT_DATA);
538 m->m_pkthdr.rcvif = (struct ifnet *)0;
540 MGET(m, M_WAIT, MT_DATA);
547 if (resid >= MINCLSIZE) {
549 if ((m->m_flags & M_EXT) == 0)
552 len = min(min(mlen, resid), space);
555 len = min(min(mlen, resid), space);
557 * For datagram protocols, leave room
558 * for protocol headers in first mbuf.
560 if (atomic && top == 0 && len < mlen)
564 error = uiomove(mtod(m, caddr_t), (int)len, uio);
565 resid = uio->uio_resid;
568 top->m_pkthdr.len += len;
574 top->m_flags |= M_EOR;
577 } while (space > 0 && atomic);
579 so->so_options |= SO_DONTROUTE;
580 s = splnet(); /* XXX */
582 * XXX all the SS_CANTSENDMORE checks previously
583 * done could be out of date. We could have recieved
584 * a reset packet in an interrupt or maybe we slept
585 * while doing page faults in uiomove() etc. We could
586 * probably recheck again inside the splnet() protection
587 * here, but there are probably other places that this
588 * also happens. We must rethink this.
590 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
591 (flags & MSG_OOB) ? PRUS_OOB :
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 ((flags & MSG_EOF) &&
598 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
601 /* If there is more to send set PRUS_MORETOCOME */
602 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
603 top, addr, control, td);
606 so->so_options &= ~SO_DONTROUTE;
613 } while (resid && space > 0);
617 sbunlock(&so->so_snd);
627 * Implement receive operations on a socket.
628 * We depend on the way that records are added to the sockbuf
629 * by sbappend*. In particular, each record (mbufs linked through m_next)
630 * must begin with an address if the protocol so specifies,
631 * followed by an optional mbuf or mbufs containing ancillary data,
632 * and then zero or more mbufs of data.
633 * In order to avoid blocking network interrupts for the entire time here,
634 * we splx() while doing the actual copy to user space.
635 * Although the sockbuf is locked, new data may still be appended,
636 * and thus we must maintain consistency of the sockbuf during that time.
638 * The caller may receive the data as a single mbuf chain by supplying
639 * an mbuf **mp0 for use in returning the chain. The uio is then used
640 * only for the count in uio_resid.
643 soreceive(so, psa, uio, mp0, controlp, flagsp)
645 struct sockaddr **psa;
648 struct mbuf **controlp;
651 struct mbuf *m, **mp;
652 int flags, len, error, s, offset;
653 struct protosw *pr = so->so_proto;
654 struct mbuf *nextrecord;
656 int orig_resid = uio->uio_resid;
664 flags = *flagsp &~ MSG_EOR;
667 if (flags & MSG_OOB) {
668 m = m_get(M_WAIT, MT_DATA);
671 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
675 error = uiomove(mtod(m, caddr_t),
676 (int) min(uio->uio_resid, m->m_len), uio);
678 } while (uio->uio_resid && error == 0 && m);
685 *mp = (struct mbuf *)0;
686 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
687 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
690 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
695 m = so->so_rcv.sb_mb;
697 * If we have less data than requested, block awaiting more
698 * (subject to any timeout) if:
699 * 1. the current count is less than the low water mark, or
700 * 2. MSG_WAITALL is set, and it is possible to do the entire
701 * receive operation at once if we block (resid <= hiwat).
702 * 3. MSG_DONTWAIT is not set
703 * If MSG_WAITALL is set but resid is larger than the receive buffer,
704 * we have to do the receive in sections, and thus risk returning
705 * a short count if a timeout or signal occurs after we start.
707 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
708 so->so_rcv.sb_cc < uio->uio_resid) &&
709 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
710 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
711 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
712 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
716 error = so->so_error;
717 if ((flags & MSG_PEEK) == 0)
721 if (so->so_state & SS_CANTRCVMORE) {
727 for (; m; m = m->m_next)
728 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
729 m = so->so_rcv.sb_mb;
732 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
733 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
737 if (uio->uio_resid == 0)
739 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
743 sbunlock(&so->so_rcv);
744 error = sbwait(&so->so_rcv);
751 if (uio->uio_td && uio->uio_td->td_proc)
752 uio->uio_td->td_proc->p_stats->p_ru.ru_msgrcv++;
753 nextrecord = m->m_nextpkt;
754 if (pr->pr_flags & PR_ADDR) {
755 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
758 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
760 if (flags & MSG_PEEK) {
763 sbfree(&so->so_rcv, m);
764 so->so_rcv.sb_mb = m_free(m);
765 m = so->so_rcv.sb_mb;
768 while (m && m->m_type == MT_CONTROL && error == 0) {
769 if (flags & MSG_PEEK) {
771 *controlp = m_copy(m, 0, m->m_len);
774 sbfree(&so->so_rcv, m);
776 if (pr->pr_domain->dom_externalize &&
777 mtod(m, struct cmsghdr *)->cmsg_type ==
779 error = (*pr->pr_domain->dom_externalize)(m);
781 so->so_rcv.sb_mb = m->m_next;
783 m = so->so_rcv.sb_mb;
785 so->so_rcv.sb_mb = m_free(m);
786 m = so->so_rcv.sb_mb;
791 controlp = &(*controlp)->m_next;
795 if ((flags & MSG_PEEK) == 0)
796 m->m_nextpkt = nextrecord;
798 if (type == MT_OOBDATA)
803 while (m && uio->uio_resid > 0 && error == 0) {
804 if (m->m_type == MT_OOBDATA) {
805 if (type != MT_OOBDATA)
807 } else if (type == MT_OOBDATA)
810 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
812 so->so_state &= ~SS_RCVATMARK;
813 len = uio->uio_resid;
814 if (so->so_oobmark && len > so->so_oobmark - offset)
815 len = so->so_oobmark - offset;
816 if (len > m->m_len - moff)
817 len = m->m_len - moff;
819 * If mp is set, just pass back the mbufs.
820 * Otherwise copy them out via the uio, then free.
821 * Sockbuf must be consistent here (points to current mbuf,
822 * it points to next record) when we drop priority;
823 * we must note any additions to the sockbuf when we
824 * block interrupts again.
828 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
833 uio->uio_resid -= len;
834 if (len == m->m_len - moff) {
835 if (m->m_flags & M_EOR)
837 if (flags & MSG_PEEK) {
841 nextrecord = m->m_nextpkt;
842 sbfree(&so->so_rcv, m);
846 so->so_rcv.sb_mb = m = m->m_next;
847 *mp = (struct mbuf *)0;
849 so->so_rcv.sb_mb = m = m_free(m);
852 m->m_nextpkt = nextrecord;
855 if (flags & MSG_PEEK)
859 *mp = m_copym(m, 0, len, M_WAIT);
862 so->so_rcv.sb_cc -= len;
865 if (so->so_oobmark) {
866 if ((flags & MSG_PEEK) == 0) {
867 so->so_oobmark -= len;
868 if (so->so_oobmark == 0) {
869 so->so_state |= SS_RCVATMARK;
874 if (offset == so->so_oobmark)
881 * If the MSG_WAITALL flag is set (for non-atomic socket),
882 * we must not quit until "uio->uio_resid == 0" or an error
883 * termination. If a signal/timeout occurs, return
884 * with a short count but without error.
885 * Keep sockbuf locked against other readers.
887 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
888 !sosendallatonce(so) && !nextrecord) {
889 if (so->so_error || so->so_state & SS_CANTRCVMORE)
892 * The window might have closed to zero, make
893 * sure we send an ack now that we've drained
894 * the buffer or we might end up blocking until
895 * the idle takes over (5 seconds).
897 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
898 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
899 error = sbwait(&so->so_rcv);
901 sbunlock(&so->so_rcv);
905 m = so->so_rcv.sb_mb;
907 nextrecord = m->m_nextpkt;
911 if (m && pr->pr_flags & PR_ATOMIC) {
913 if ((flags & MSG_PEEK) == 0)
914 (void) sbdroprecord(&so->so_rcv);
916 if ((flags & MSG_PEEK) == 0) {
918 so->so_rcv.sb_mb = nextrecord;
919 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
920 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
922 if (orig_resid == uio->uio_resid && orig_resid &&
923 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
924 sbunlock(&so->so_rcv);
932 sbunlock(&so->so_rcv);
942 struct protosw *pr = so->so_proto;
944 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
950 return ((*pr->pr_usrreqs->pru_shutdown)(so));
958 struct sockbuf *sb = &so->so_rcv;
959 struct protosw *pr = so->so_proto;
963 sb->sb_flags |= SB_NOINTR;
964 (void) sblock(sb, M_WAITOK);
969 bzero((caddr_t)sb, sizeof (*sb));
970 if (asb.sb_flags & SB_KNOTE) {
971 sb->sb_sel.si_note = asb.sb_sel.si_note;
972 sb->sb_flags = SB_KNOTE;
975 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
976 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
982 do_setopt_accept_filter(so, sopt)
984 struct sockopt *sopt;
986 struct accept_filter_arg *afap = NULL;
987 struct accept_filter *afp;
988 struct so_accf *af = so->so_accf;
991 /* do not set/remove accept filters on non listen sockets */
992 if ((so->so_options & SO_ACCEPTCONN) == 0) {
997 /* removing the filter */
1000 if (af->so_accept_filter != NULL &&
1001 af->so_accept_filter->accf_destroy != NULL) {
1002 af->so_accept_filter->accf_destroy(so);
1004 if (af->so_accept_filter_str != NULL) {
1005 FREE(af->so_accept_filter_str, M_ACCF);
1010 so->so_options &= ~SO_ACCEPTFILTER;
1013 /* adding a filter */
1014 /* must remove previous filter first */
1019 /* don't put large objects on the kernel stack */
1020 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1021 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1022 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1023 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1026 afp = accept_filt_get(afap->af_name);
1031 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK);
1032 bzero(af, sizeof(*af));
1033 if (afp->accf_create != NULL) {
1034 if (afap->af_name[0] != '\0') {
1035 int len = strlen(afap->af_name) + 1;
1037 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1038 strcpy(af->so_accept_filter_str, afap->af_name);
1040 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1041 if (af->so_accept_filter_arg == NULL) {
1042 FREE(af->so_accept_filter_str, M_ACCF);
1049 af->so_accept_filter = afp;
1051 so->so_options |= SO_ACCEPTFILTER;
1060 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1061 * an additional variant to handle the case where the option value needs
1062 * to be some kind of integer, but not a specific size.
1063 * In addition to their use here, these functions are also called by the
1064 * protocol-level pr_ctloutput() routines.
1067 sooptcopyin(sopt, buf, len, minlen)
1068 struct sockopt *sopt;
1076 * If the user gives us more than we wanted, we ignore it,
1077 * but if we don't get the minimum length the caller
1078 * wants, we return EINVAL. On success, sopt->sopt_valsize
1079 * is set to however much we actually retrieved.
1081 if ((valsize = sopt->sopt_valsize) < minlen)
1084 sopt->sopt_valsize = valsize = len;
1086 if (sopt->sopt_td != NULL)
1087 return (copyin(sopt->sopt_val, buf, valsize));
1089 bcopy(sopt->sopt_val, buf, valsize);
1096 struct sockopt *sopt;
1104 if (sopt->sopt_level != SOL_SOCKET) {
1105 if (so->so_proto && so->so_proto->pr_ctloutput)
1106 return ((*so->so_proto->pr_ctloutput)
1108 error = ENOPROTOOPT;
1110 switch (sopt->sopt_name) {
1112 case SO_ACCEPTFILTER:
1113 error = do_setopt_accept_filter(so, sopt);
1119 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1123 so->so_linger = l.l_linger;
1125 so->so_options |= SO_LINGER;
1127 so->so_options &= ~SO_LINGER;
1133 case SO_USELOOPBACK:
1139 error = sooptcopyin(sopt, &optval, sizeof optval,
1144 so->so_options |= sopt->sopt_name;
1146 so->so_options &= ~sopt->sopt_name;
1153 error = sooptcopyin(sopt, &optval, sizeof optval,
1159 * Values < 1 make no sense for any of these
1160 * options, so disallow them.
1167 switch (sopt->sopt_name) {
1170 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1171 &so->so_snd : &so->so_rcv, (u_long)optval,
1172 so, curproc) == 0) {
1179 * Make sure the low-water is never greater than
1183 so->so_snd.sb_lowat =
1184 (optval > so->so_snd.sb_hiwat) ?
1185 so->so_snd.sb_hiwat : optval;
1188 so->so_rcv.sb_lowat =
1189 (optval > so->so_rcv.sb_hiwat) ?
1190 so->so_rcv.sb_hiwat : optval;
1197 error = sooptcopyin(sopt, &tv, sizeof tv,
1202 /* assert(hz > 0); */
1203 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1204 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1208 /* assert(tick > 0); */
1209 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1210 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1211 if (val > SHRT_MAX) {
1215 if (val == 0 && tv.tv_usec != 0)
1218 switch (sopt->sopt_name) {
1220 so->so_snd.sb_timeo = val;
1223 so->so_rcv.sb_timeo = val;
1228 error = ENOPROTOOPT;
1231 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1232 (void) ((*so->so_proto->pr_ctloutput)
1240 /* Helper routine for getsockopt */
1242 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
1250 * Documented get behavior is that we always return a value,
1251 * possibly truncated to fit in the user's buffer.
1252 * Traditional behavior is that we always tell the user
1253 * precisely how much we copied, rather than something useful
1254 * like the total amount we had available for her.
1255 * Note that this interface is not idempotent; the entire answer must
1256 * generated ahead of time.
1258 valsize = min(len, sopt->sopt_valsize);
1259 sopt->sopt_valsize = valsize;
1260 if (sopt->sopt_val != 0) {
1261 if (sopt->sopt_td != NULL)
1262 error = copyout(buf, sopt->sopt_val, valsize);
1264 bcopy(buf, sopt->sopt_val, valsize);
1272 struct sockopt *sopt;
1277 struct accept_filter_arg *afap;
1280 if (sopt->sopt_level != SOL_SOCKET) {
1281 if (so->so_proto && so->so_proto->pr_ctloutput) {
1282 return ((*so->so_proto->pr_ctloutput)
1285 return (ENOPROTOOPT);
1287 switch (sopt->sopt_name) {
1289 case SO_ACCEPTFILTER:
1290 if ((so->so_options & SO_ACCEPTCONN) == 0)
1292 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1294 bzero(afap, sizeof(*afap));
1295 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1296 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1297 if (so->so_accf->so_accept_filter_str != NULL)
1298 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1300 error = sooptcopyout(sopt, afap, sizeof(*afap));
1306 l.l_onoff = so->so_options & SO_LINGER;
1307 l.l_linger = so->so_linger;
1308 error = sooptcopyout(sopt, &l, sizeof l);
1311 case SO_USELOOPBACK:
1320 optval = so->so_options & sopt->sopt_name;
1322 error = sooptcopyout(sopt, &optval, sizeof optval);
1326 optval = so->so_type;
1330 optval = so->so_error;
1335 optval = so->so_snd.sb_hiwat;
1339 optval = so->so_rcv.sb_hiwat;
1343 optval = so->so_snd.sb_lowat;
1347 optval = so->so_rcv.sb_lowat;
1352 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1353 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1355 tv.tv_sec = optval / hz;
1356 tv.tv_usec = (optval % hz) * tick;
1357 error = sooptcopyout(sopt, &tv, sizeof tv);
1361 error = ENOPROTOOPT;
1368 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1370 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1372 struct mbuf *m, *m_prev;
1373 int sopt_size = sopt->sopt_valsize;
1375 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
1378 if (sopt_size > MLEN) {
1379 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1380 if ((m->m_flags & M_EXT) == 0) {
1384 m->m_len = min(MCLBYTES, sopt_size);
1386 m->m_len = min(MLEN, sopt_size);
1388 sopt_size -= m->m_len;
1393 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
1398 if (sopt_size > MLEN) {
1399 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1400 if ((m->m_flags & M_EXT) == 0) {
1404 m->m_len = min(MCLBYTES, sopt_size);
1406 m->m_len = min(MLEN, sopt_size);
1408 sopt_size -= m->m_len;
1415 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1417 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1419 struct mbuf *m0 = m;
1421 if (sopt->sopt_val == NULL)
1423 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1424 if (sopt->sopt_td != NULL) {
1427 error = copyin(sopt->sopt_val, mtod(m, char *),
1434 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1435 sopt->sopt_valsize -= m->m_len;
1436 (caddr_t)sopt->sopt_val += m->m_len;
1439 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1440 panic("ip6_sooptmcopyin");
1444 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1446 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1448 struct mbuf *m0 = m;
1451 if (sopt->sopt_val == NULL)
1453 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1454 if (sopt->sopt_td != NULL) {
1457 error = copyout(mtod(m, char *), sopt->sopt_val,
1464 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1465 sopt->sopt_valsize -= m->m_len;
1466 (caddr_t)sopt->sopt_val += m->m_len;
1467 valsize += m->m_len;
1471 /* enough soopt buffer should be given from user-land */
1475 sopt->sopt_valsize = valsize;
1483 if (so->so_sigio != NULL)
1484 pgsigio(so->so_sigio, SIGURG, 0);
1485 selwakeup(&so->so_rcv.sb_sel);
1489 sopoll(struct socket *so, int events, struct ucred *cred, struct thread *td)
1494 if (events & (POLLIN | POLLRDNORM))
1496 revents |= events & (POLLIN | POLLRDNORM);
1498 if (events & POLLINIGNEOF)
1499 if (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
1500 !TAILQ_EMPTY(&so->so_comp) || so->so_error)
1501 revents |= POLLINIGNEOF;
1503 if (events & (POLLOUT | POLLWRNORM))
1504 if (sowriteable(so))
1505 revents |= events & (POLLOUT | POLLWRNORM);
1507 if (events & (POLLPRI | POLLRDBAND))
1508 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1509 revents |= events & (POLLPRI | POLLRDBAND);
1513 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM |
1515 selrecord(td, &so->so_rcv.sb_sel);
1516 so->so_rcv.sb_flags |= SB_SEL;
1519 if (events & (POLLOUT | POLLWRNORM)) {
1520 selrecord(td, &so->so_snd.sb_sel);
1521 so->so_snd.sb_flags |= SB_SEL;
1530 sokqfilter(struct file *fp, struct knote *kn)
1532 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1536 switch (kn->kn_filter) {
1538 if (so->so_options & SO_ACCEPTCONN)
1539 kn->kn_fop = &solisten_filtops;
1541 kn->kn_fop = &soread_filtops;
1545 kn->kn_fop = &sowrite_filtops;
1553 SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext);
1554 sb->sb_flags |= SB_KNOTE;
1560 filt_sordetach(struct knote *kn)
1562 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1565 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1566 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1567 so->so_rcv.sb_flags &= ~SB_KNOTE;
1573 filt_soread(struct knote *kn, long hint)
1575 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1577 kn->kn_data = so->so_rcv.sb_cc;
1578 if (so->so_state & SS_CANTRCVMORE) {
1579 kn->kn_flags |= EV_EOF;
1580 kn->kn_fflags = so->so_error;
1583 if (so->so_error) /* temporary udp error */
1585 if (kn->kn_sfflags & NOTE_LOWAT)
1586 return (kn->kn_data >= kn->kn_sdata);
1587 return (kn->kn_data >= so->so_rcv.sb_lowat);
1591 filt_sowdetach(struct knote *kn)
1593 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1596 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1597 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1598 so->so_snd.sb_flags &= ~SB_KNOTE;
1604 filt_sowrite(struct knote *kn, long hint)
1606 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1608 kn->kn_data = sbspace(&so->so_snd);
1609 if (so->so_state & SS_CANTSENDMORE) {
1610 kn->kn_flags |= EV_EOF;
1611 kn->kn_fflags = so->so_error;
1614 if (so->so_error) /* temporary udp error */
1616 if (((so->so_state & SS_ISCONNECTED) == 0) &&
1617 (so->so_proto->pr_flags & PR_CONNREQUIRED))
1619 if (kn->kn_sfflags & NOTE_LOWAT)
1620 return (kn->kn_data >= kn->kn_sdata);
1621 return (kn->kn_data >= so->so_snd.sb_lowat);
1626 filt_solisten(struct knote *kn, long hint)
1628 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1630 kn->kn_data = so->so_qlen;
1631 return (! TAILQ_EMPTY(&so->so_comp));