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
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/fcntl.h>
40 #include <sys/malloc.h>
42 #include <sys/domain.h>
43 #include <sys/file.h> /* for struct knote */
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/event.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/resourcevar.h>
53 #include <sys/signalvar.h>
54 #include <sys/sysctl.h>
57 #include <vm/vm_zone.h>
59 #include <machine/limits.h>
61 static int filt_sorattach(struct knote *kn);
62 static void filt_sordetach(struct knote *kn);
63 static int filt_soread(struct knote *kn, long hint);
64 static int filt_sowattach(struct knote *kn);
65 static void filt_sowdetach(struct knote *kn);
66 static int filt_sowrite(struct knote *kn, long hint);
67 static int filt_solisten(struct knote *kn, long hint);
69 static struct filterops solisten_filtops =
70 { 1, filt_sorattach, filt_sordetach, filt_solisten };
72 struct filterops so_rwfiltops[] = {
73 { 1, filt_sorattach, filt_sordetach, filt_soread },
74 { 1, filt_sowattach, filt_sowdetach, filt_sowrite },
77 struct vm_zone *socket_zone;
78 so_gen_t so_gencnt; /* generation count for sockets */
80 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
81 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
83 SYSCTL_DECL(_kern_ipc);
85 static int somaxconn = SOMAXCONN;
86 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
87 &somaxconn, 0, "Maximum pending socket connection queue size");
90 * Socket operation routines.
91 * These routines are called by the routines in
92 * sys_socket.c or from a system process, and
93 * implement the semantics of socket operations by
94 * switching out to the protocol specific routines.
98 * Get a socket structure from our zone, and initialize it.
99 * We don't implement `waitok' yet (see comments in uipc_domain.c).
100 * Note that it would probably be better to allocate socket
101 * and PCB at the same time, but I'm not convinced that all
102 * the protocols can be easily modified to do this.
110 so = zalloci(socket_zone);
112 /* XXX race condition for reentrant kernel */
113 bzero(so, sizeof *so);
114 so->so_gencnt = ++so_gencnt;
115 so->so_zone = socket_zone;
116 TAILQ_INIT(&so->so_aiojobq);
122 socreate(dom, aso, type, proto, p)
129 register struct protosw *prp;
130 register struct socket *so;
134 prp = pffindproto(dom, proto, type);
136 prp = pffindtype(dom, type);
138 if (p->p_prison && jail_socket_unixiproute_only &&
139 prp->pr_domain->dom_family != PF_LOCAL &&
140 prp->pr_domain->dom_family != PF_INET &&
141 prp->pr_domain->dom_family != PF_ROUTE) {
142 return (EPROTONOSUPPORT);
145 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
146 return (EPROTONOSUPPORT);
147 if (prp->pr_type != type)
149 so = soalloc(p != 0);
153 TAILQ_INIT(&so->so_incomp);
154 TAILQ_INIT(&so->so_comp);
156 so->so_cred = p->p_ucred;
159 error = (*prp->pr_usrreqs->pru_attach)(so, proto, p);
161 so->so_state |= SS_NOFDREF;
172 struct sockaddr *nam;
178 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
188 so->so_gencnt = ++so_gencnt;
189 if (so->so_rcv.sb_hiwat)
190 (void)chgsbsize(so->so_cred->cr_uid,
191 -(rlim_t)so->so_rcv.sb_hiwat);
192 if (so->so_snd.sb_hiwat)
193 (void)chgsbsize(so->so_cred->cr_uid,
194 -(rlim_t)so->so_snd.sb_hiwat);
196 zfreei(so->so_zone, so);
200 solisten(so, backlog, p)
201 register struct socket *so;
208 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p);
213 if (TAILQ_EMPTY(&so->so_comp))
214 so->so_options |= SO_ACCEPTCONN;
215 if (backlog < 0 || backlog > somaxconn)
217 so->so_qlimit = backlog;
224 register struct socket *so;
226 struct socket *head = so->so_head;
228 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
231 if (so->so_state & SS_INCOMP) {
232 TAILQ_REMOVE(&head->so_incomp, so, so_list);
234 } else if (so->so_state & SS_COMP) {
236 * We must not decommission a socket that's
237 * on the accept(2) queue. If we do, then
238 * accept(2) may hang after select(2) indicated
239 * that the listening socket was ready.
243 panic("sofree: not queued");
246 so->so_state &= ~SS_INCOMP;
249 sbrelease(&so->so_snd, so);
255 * Close a socket on last file table reference removal.
256 * Initiate disconnect if connected.
257 * Free socket when disconnect complete.
261 register struct socket *so;
263 int s = splnet(); /* conservative */
266 funsetown(so->so_sigio);
267 if (so->so_options & SO_ACCEPTCONN) {
268 struct socket *sp, *sonext;
270 sp = TAILQ_FIRST(&so->so_incomp);
271 for (; sp != NULL; sp = sonext) {
272 sonext = TAILQ_NEXT(sp, so_list);
275 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
276 sonext = TAILQ_NEXT(sp, so_list);
277 /* Dequeue from so_comp since sofree() won't do it */
278 TAILQ_REMOVE(&so->so_comp, sp, so_list);
280 sp->so_state &= ~SS_COMP;
287 if (so->so_state & SS_ISCONNECTED) {
288 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
289 error = sodisconnect(so);
293 if (so->so_options & SO_LINGER) {
294 if ((so->so_state & SS_ISDISCONNECTING) &&
295 (so->so_state & SS_NBIO))
297 while (so->so_state & SS_ISCONNECTED) {
298 error = tsleep((caddr_t)&so->so_timeo,
299 PSOCK | PCATCH, "soclos", so->so_linger * hz);
307 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
312 if (so->so_state & SS_NOFDREF)
313 panic("soclose: NOFDREF");
314 so->so_state |= SS_NOFDREF;
321 * Must be called at splnet...
329 error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
339 register struct socket *so;
340 struct sockaddr **nam;
345 if ((so->so_state & SS_NOFDREF) == 0)
346 panic("soaccept: !NOFDREF");
347 so->so_state &= ~SS_NOFDREF;
348 if ((so->so_state & SS_ISDISCONNECTED) == 0)
349 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
360 soconnect(so, nam, p)
361 register struct socket *so;
362 struct sockaddr *nam;
368 if (so->so_options & SO_ACCEPTCONN)
372 * If protocol is connection-based, can only connect once.
373 * Otherwise, if connected, try to disconnect first.
374 * This allows user to disconnect by connecting to, e.g.,
377 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
378 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
379 (error = sodisconnect(so))))
382 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p);
389 register struct socket *so1;
395 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
402 register struct socket *so;
407 if ((so->so_state & SS_ISCONNECTED) == 0) {
411 if (so->so_state & SS_ISDISCONNECTING) {
415 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
421 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
424 * If send must go all at once and message is larger than
425 * send buffering, then hard error.
426 * Lock against other senders.
427 * If must go all at once and not enough room now, then
428 * inform user that this would block and do nothing.
429 * Otherwise, if nonblocking, send as much as possible.
430 * The data to be sent is described by "uio" if nonzero,
431 * otherwise by the mbuf chain "top" (which must be null
432 * if uio is not). Data provided in mbuf chain must be small
433 * enough to send all at once.
435 * Returns nonzero on error, timeout or signal; callers
436 * must check for short counts if EINTR/ERESTART are returned.
437 * Data and control buffers are freed on return.
440 sosend(so, addr, uio, top, control, flags, p)
441 register struct socket *so;
442 struct sockaddr *addr;
445 struct mbuf *control;
450 register struct mbuf *m;
451 register long space, len, resid;
452 int clen = 0, error, s, dontroute, mlen;
453 int atomic = sosendallatonce(so) || top;
456 resid = uio->uio_resid;
458 resid = top->m_pkthdr.len;
460 * In theory resid should be unsigned.
461 * However, space must be signed, as it might be less than 0
462 * if we over-committed, and we must use a signed comparison
463 * of space and resid. On the other hand, a negative resid
464 * causes us to loop sending 0-length segments to the protocol.
466 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
467 * type sockets since that's an error.
469 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
475 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
476 (so->so_proto->pr_flags & PR_ATOMIC);
478 p->p_stats->p_ru.ru_msgsnd++;
480 clen = control->m_len;
481 #define snderr(errno) { error = errno; splx(s); goto release; }
484 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
489 if (so->so_state & SS_CANTSENDMORE)
492 error = so->so_error;
497 if ((so->so_state & SS_ISCONNECTED) == 0) {
499 * `sendto' and `sendmsg' is allowed on a connection-
500 * based socket if it supports implied connect.
501 * Return ENOTCONN if not connected and no address is
504 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
505 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
506 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
507 !(resid == 0 && clen != 0))
509 } else if (addr == 0)
510 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
511 ENOTCONN : EDESTADDRREQ);
513 space = sbspace(&so->so_snd);
516 if ((atomic && resid > so->so_snd.sb_hiwat) ||
517 clen > so->so_snd.sb_hiwat)
519 if (space < resid + clen && uio &&
520 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
521 if (so->so_state & SS_NBIO)
523 sbunlock(&so->so_snd);
524 error = sbwait(&so->so_snd);
536 * Data is prepackaged in "top".
540 top->m_flags |= M_EOR;
543 MGETHDR(m, M_WAIT, MT_DATA);
550 m->m_pkthdr.rcvif = (struct ifnet *)0;
552 MGET(m, M_WAIT, MT_DATA);
559 if (resid >= MINCLSIZE) {
561 if ((m->m_flags & M_EXT) == 0)
564 len = min(min(mlen, resid), space);
567 len = min(min(mlen, resid), space);
569 * For datagram protocols, leave room
570 * for protocol headers in first mbuf.
572 if (atomic && top == 0 && len < mlen)
576 error = uiomove(mtod(m, caddr_t), (int)len, uio);
577 resid = uio->uio_resid;
580 top->m_pkthdr.len += len;
586 top->m_flags |= M_EOR;
589 } while (space > 0 && atomic);
591 so->so_options |= SO_DONTROUTE;
592 s = splnet(); /* XXX */
594 * XXX all the SS_CANTSENDMORE checks previously
595 * done could be out of date. We could have recieved
596 * a reset packet in an interrupt or maybe we slept
597 * while doing page faults in uiomove() etc. We could
598 * probably recheck again inside the splnet() protection
599 * here, but there are probably other places that this
600 * also happens. We must rethink this.
602 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
603 (flags & MSG_OOB) ? PRUS_OOB :
605 * If the user set MSG_EOF, the protocol
606 * understands this flag and nothing left to
607 * send then use PRU_SEND_EOF instead of PRU_SEND.
609 ((flags & MSG_EOF) &&
610 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
613 /* If there is more to send set PRUS_MORETOCOME */
614 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
615 top, addr, control, p);
618 so->so_options &= ~SO_DONTROUTE;
625 } while (resid && space > 0);
629 sbunlock(&so->so_snd);
639 * Implement receive operations on a socket.
640 * We depend on the way that records are added to the sockbuf
641 * by sbappend*. In particular, each record (mbufs linked through m_next)
642 * must begin with an address if the protocol so specifies,
643 * followed by an optional mbuf or mbufs containing ancillary data,
644 * and then zero or more mbufs of data.
645 * In order to avoid blocking network interrupts for the entire time here,
646 * we splx() while doing the actual copy to user space.
647 * Although the sockbuf is locked, new data may still be appended,
648 * and thus we must maintain consistency of the sockbuf during that time.
650 * The caller may receive the data as a single mbuf chain by supplying
651 * an mbuf **mp0 for use in returning the chain. The uio is then used
652 * only for the count in uio_resid.
655 soreceive(so, psa, uio, mp0, controlp, flagsp)
656 register struct socket *so;
657 struct sockaddr **psa;
660 struct mbuf **controlp;
663 register struct mbuf *m, **mp;
664 register int flags, len, error, s, offset;
665 struct protosw *pr = so->so_proto;
666 struct mbuf *nextrecord;
668 int orig_resid = uio->uio_resid;
676 flags = *flagsp &~ MSG_EOR;
679 if (flags & MSG_OOB) {
680 m = m_get(M_WAIT, MT_DATA);
683 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
687 error = uiomove(mtod(m, caddr_t),
688 (int) min(uio->uio_resid, m->m_len), uio);
690 } while (uio->uio_resid && error == 0 && m);
697 *mp = (struct mbuf *)0;
698 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
699 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
702 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
707 m = so->so_rcv.sb_mb;
709 * If we have less data than requested, block awaiting more
710 * (subject to any timeout) if:
711 * 1. the current count is less than the low water mark, or
712 * 2. MSG_WAITALL is set, and it is possible to do the entire
713 * receive operation at once if we block (resid <= hiwat).
714 * 3. MSG_DONTWAIT is not set
715 * If MSG_WAITALL is set but resid is larger than the receive buffer,
716 * we have to do the receive in sections, and thus risk returning
717 * a short count if a timeout or signal occurs after we start.
719 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
720 so->so_rcv.sb_cc < uio->uio_resid) &&
721 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
722 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
723 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
724 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
728 error = so->so_error;
729 if ((flags & MSG_PEEK) == 0)
733 if (so->so_state & SS_CANTRCVMORE) {
739 for (; m; m = m->m_next)
740 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
741 m = so->so_rcv.sb_mb;
744 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
745 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
749 if (uio->uio_resid == 0)
751 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
755 sbunlock(&so->so_rcv);
756 error = sbwait(&so->so_rcv);
764 uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
765 nextrecord = m->m_nextpkt;
766 if (pr->pr_flags & PR_ADDR) {
767 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
770 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
772 if (flags & MSG_PEEK) {
775 sbfree(&so->so_rcv, m);
776 MFREE(m, so->so_rcv.sb_mb);
777 m = so->so_rcv.sb_mb;
780 while (m && m->m_type == MT_CONTROL && error == 0) {
781 if (flags & MSG_PEEK) {
783 *controlp = m_copy(m, 0, m->m_len);
786 sbfree(&so->so_rcv, m);
788 if (pr->pr_domain->dom_externalize &&
789 mtod(m, struct cmsghdr *)->cmsg_type ==
791 error = (*pr->pr_domain->dom_externalize)(m);
793 so->so_rcv.sb_mb = m->m_next;
795 m = so->so_rcv.sb_mb;
797 MFREE(m, so->so_rcv.sb_mb);
798 m = so->so_rcv.sb_mb;
803 controlp = &(*controlp)->m_next;
807 if ((flags & MSG_PEEK) == 0)
808 m->m_nextpkt = nextrecord;
810 if (type == MT_OOBDATA)
815 while (m && uio->uio_resid > 0 && error == 0) {
816 if (m->m_type == MT_OOBDATA) {
817 if (type != MT_OOBDATA)
819 } else if (type == MT_OOBDATA)
822 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
824 so->so_state &= ~SS_RCVATMARK;
825 len = uio->uio_resid;
826 if (so->so_oobmark && len > so->so_oobmark - offset)
827 len = so->so_oobmark - offset;
828 if (len > m->m_len - moff)
829 len = m->m_len - moff;
831 * If mp is set, just pass back the mbufs.
832 * Otherwise copy them out via the uio, then free.
833 * Sockbuf must be consistent here (points to current mbuf,
834 * it points to next record) when we drop priority;
835 * we must note any additions to the sockbuf when we
836 * block interrupts again.
840 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
845 uio->uio_resid -= len;
846 if (len == m->m_len - moff) {
847 if (m->m_flags & M_EOR)
849 if (flags & MSG_PEEK) {
853 nextrecord = m->m_nextpkt;
854 sbfree(&so->so_rcv, m);
858 so->so_rcv.sb_mb = m = m->m_next;
859 *mp = (struct mbuf *)0;
861 MFREE(m, so->so_rcv.sb_mb);
862 m = so->so_rcv.sb_mb;
865 m->m_nextpkt = nextrecord;
868 if (flags & MSG_PEEK)
872 *mp = m_copym(m, 0, len, M_WAIT);
875 so->so_rcv.sb_cc -= len;
878 if (so->so_oobmark) {
879 if ((flags & MSG_PEEK) == 0) {
880 so->so_oobmark -= len;
881 if (so->so_oobmark == 0) {
882 so->so_state |= SS_RCVATMARK;
887 if (offset == so->so_oobmark)
894 * If the MSG_WAITALL flag is set (for non-atomic socket),
895 * we must not quit until "uio->uio_resid == 0" or an error
896 * termination. If a signal/timeout occurs, return
897 * with a short count but without error.
898 * Keep sockbuf locked against other readers.
900 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
901 !sosendallatonce(so) && !nextrecord) {
902 if (so->so_error || so->so_state & SS_CANTRCVMORE)
904 error = sbwait(&so->so_rcv);
906 sbunlock(&so->so_rcv);
910 m = so->so_rcv.sb_mb;
912 nextrecord = m->m_nextpkt;
916 if (m && pr->pr_flags & PR_ATOMIC) {
918 if ((flags & MSG_PEEK) == 0)
919 (void) sbdroprecord(&so->so_rcv);
921 if ((flags & MSG_PEEK) == 0) {
923 so->so_rcv.sb_mb = nextrecord;
924 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
925 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
927 if (orig_resid == uio->uio_resid && orig_resid &&
928 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
929 sbunlock(&so->so_rcv);
937 sbunlock(&so->so_rcv);
944 register struct socket *so;
947 register struct protosw *pr = so->so_proto;
953 return ((*pr->pr_usrreqs->pru_shutdown)(so));
959 register struct socket *so;
961 register struct sockbuf *sb = &so->so_rcv;
962 register struct protosw *pr = so->so_proto;
966 sb->sb_flags |= SB_NOINTR;
967 (void) sblock(sb, M_WAITOK);
972 bzero((caddr_t)sb, sizeof (*sb));
974 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
975 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
980 * Perhaps this routine, and sooptcopyout(), below, ought to come in
981 * an additional variant to handle the case where the option value needs
982 * to be some kind of integer, but not a specific size.
983 * In addition to their use here, these functions are also called by the
984 * protocol-level pr_ctloutput() routines.
987 sooptcopyin(sopt, buf, len, minlen)
988 struct sockopt *sopt;
996 * If the user gives us more than we wanted, we ignore it,
997 * but if we don't get the minimum length the caller
998 * wants, we return EINVAL. On success, sopt->sopt_valsize
999 * is set to however much we actually retrieved.
1001 if ((valsize = sopt->sopt_valsize) < minlen)
1004 sopt->sopt_valsize = valsize = len;
1006 if (sopt->sopt_p != 0)
1007 return (copyin(sopt->sopt_val, buf, valsize));
1009 bcopy(sopt->sopt_val, buf, valsize);
1016 struct sockopt *sopt;
1024 if (sopt->sopt_level != SOL_SOCKET) {
1025 if (so->so_proto && so->so_proto->pr_ctloutput)
1026 return ((*so->so_proto->pr_ctloutput)
1028 error = ENOPROTOOPT;
1030 switch (sopt->sopt_name) {
1032 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1036 so->so_linger = l.l_linger;
1038 so->so_options |= SO_LINGER;
1040 so->so_options &= ~SO_LINGER;
1046 case SO_USELOOPBACK:
1052 error = sooptcopyin(sopt, &optval, sizeof optval,
1057 so->so_options |= sopt->sopt_name;
1059 so->so_options &= ~sopt->sopt_name;
1066 error = sooptcopyin(sopt, &optval, sizeof optval,
1072 * Values < 1 make no sense for any of these
1073 * options, so disallow them.
1080 switch (sopt->sopt_name) {
1083 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1084 &so->so_snd : &so->so_rcv, (u_long)optval,
1085 so, curproc) == 0) {
1092 * Make sure the low-water is never greater than
1096 so->so_snd.sb_lowat =
1097 (optval > so->so_snd.sb_hiwat) ?
1098 so->so_snd.sb_hiwat : optval;
1101 so->so_rcv.sb_lowat =
1102 (optval > so->so_rcv.sb_hiwat) ?
1103 so->so_rcv.sb_hiwat : optval;
1110 error = sooptcopyin(sopt, &tv, sizeof tv,
1115 /* assert(hz > 0); */
1116 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1117 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1121 /* assert(tick > 0); */
1122 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1123 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1124 if (val > SHRT_MAX) {
1129 switch (sopt->sopt_name) {
1131 so->so_snd.sb_timeo = val;
1134 so->so_rcv.sb_timeo = val;
1140 error = ENOPROTOOPT;
1143 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1144 (void) ((*so->so_proto->pr_ctloutput)
1152 /* Helper routine for getsockopt */
1154 sooptcopyout(sopt, buf, len)
1155 struct sockopt *sopt;
1165 * Documented get behavior is that we always return a value,
1166 * possibly truncated to fit in the user's buffer.
1167 * Traditional behavior is that we always tell the user
1168 * precisely how much we copied, rather than something useful
1169 * like the total amount we had available for her.
1170 * Note that this interface is not idempotent; the entire answer must
1171 * generated ahead of time.
1173 valsize = min(len, sopt->sopt_valsize);
1174 sopt->sopt_valsize = valsize;
1175 if (sopt->sopt_val != 0) {
1176 if (sopt->sopt_p != 0)
1177 error = copyout(buf, sopt->sopt_val, valsize);
1179 bcopy(buf, sopt->sopt_val, valsize);
1187 struct sockopt *sopt;
1194 if (sopt->sopt_level != SOL_SOCKET) {
1195 if (so->so_proto && so->so_proto->pr_ctloutput) {
1196 return ((*so->so_proto->pr_ctloutput)
1199 return (ENOPROTOOPT);
1201 switch (sopt->sopt_name) {
1203 l.l_onoff = so->so_options & SO_LINGER;
1204 l.l_linger = so->so_linger;
1205 error = sooptcopyout(sopt, &l, sizeof l);
1208 case SO_USELOOPBACK:
1217 optval = so->so_options & sopt->sopt_name;
1219 error = sooptcopyout(sopt, &optval, sizeof optval);
1223 optval = so->so_type;
1227 optval = so->so_error;
1232 optval = so->so_snd.sb_hiwat;
1236 optval = so->so_rcv.sb_hiwat;
1240 optval = so->so_snd.sb_lowat;
1244 optval = so->so_rcv.sb_lowat;
1249 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1250 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1252 tv.tv_sec = optval / hz;
1253 tv.tv_usec = (optval % hz) * tick;
1254 error = sooptcopyout(sopt, &tv, sizeof tv);
1258 error = ENOPROTOOPT;
1265 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1267 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1269 struct mbuf *m, *m_prev;
1270 int sopt_size = sopt->sopt_valsize;
1272 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1275 if (sopt_size > MLEN) {
1276 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1277 if ((m->m_flags & M_EXT) == 0) {
1281 m->m_len = min(MCLBYTES, sopt_size);
1283 m->m_len = min(MLEN, sopt_size);
1285 sopt_size -= m->m_len;
1290 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1295 if (sopt_size > MLEN) {
1296 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1297 if ((m->m_flags & M_EXT) == 0) {
1301 m->m_len = min(MCLBYTES, sopt_size);
1303 m->m_len = min(MLEN, sopt_size);
1305 sopt_size -= m->m_len;
1312 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1314 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1316 struct mbuf *m0 = m;
1318 if (sopt->sopt_val == NULL)
1320 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1321 if (sopt->sopt_p != NULL) {
1324 error = copyin(sopt->sopt_val, mtod(m, char *),
1331 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1332 sopt->sopt_valsize -= m->m_len;
1333 (caddr_t)sopt->sopt_val += m->m_len;
1336 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1337 panic("ip6_sooptmcopyin");
1341 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1343 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1345 struct mbuf *m0 = m;
1348 if (sopt->sopt_val == NULL)
1350 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1351 if (sopt->sopt_p != NULL) {
1354 error = copyout(mtod(m, char *), sopt->sopt_val,
1361 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1362 sopt->sopt_valsize -= m->m_len;
1363 (caddr_t)sopt->sopt_val += m->m_len;
1364 valsize += m->m_len;
1368 /* enough soopt buffer should be given from user-land */
1372 sopt->sopt_valsize = valsize;
1378 register struct socket *so;
1380 if (so->so_sigio != NULL)
1381 pgsigio(so->so_sigio, SIGURG, 0);
1382 selwakeup(&so->so_rcv.sb_sel);
1386 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p)
1391 if (events & (POLLIN | POLLRDNORM))
1393 revents |= events & (POLLIN | POLLRDNORM);
1395 if (events & (POLLOUT | POLLWRNORM))
1396 if (sowriteable(so))
1397 revents |= events & (POLLOUT | POLLWRNORM);
1399 if (events & (POLLPRI | POLLRDBAND))
1400 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1401 revents |= events & (POLLPRI | POLLRDBAND);
1404 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1405 selrecord(p, &so->so_rcv.sb_sel);
1406 so->so_rcv.sb_flags |= SB_SEL;
1409 if (events & (POLLOUT | POLLWRNORM)) {
1410 selrecord(p, &so->so_snd.sb_sel);
1411 so->so_snd.sb_flags |= SB_SEL;
1420 filt_sorattach(struct knote *kn)
1422 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1425 if (so->so_options & SO_ACCEPTCONN)
1426 kn->kn_fop = &solisten_filtops;
1427 SLIST_INSERT_HEAD(&so->so_rcv.sb_sel.si_note, kn, kn_selnext);
1428 so->so_rcv.sb_flags |= SB_KNOTE;
1434 filt_sordetach(struct knote *kn)
1436 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1439 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1440 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1441 so->so_rcv.sb_flags &= ~SB_KNOTE;
1447 filt_soread(struct knote *kn, long hint)
1449 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1451 kn->kn_data = so->so_rcv.sb_cc;
1452 if (so->so_state & SS_CANTRCVMORE) {
1453 kn->kn_flags |= EV_EOF;
1456 return (kn->kn_data > 0);
1460 filt_sowattach(struct knote *kn)
1462 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1465 SLIST_INSERT_HEAD(&so->so_snd.sb_sel.si_note, kn, kn_selnext);
1466 so->so_snd.sb_flags |= SB_KNOTE;
1472 filt_sowdetach(struct knote *kn)
1474 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1477 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1478 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1479 so->so_snd.sb_flags &= ~SB_KNOTE;
1485 filt_sowrite(struct knote *kn, long hint)
1487 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1489 kn->kn_data = sbspace(&so->so_snd);
1490 if (so->so_state & SS_CANTSENDMORE) {
1491 kn->kn_flags |= EV_EOF;
1494 if (((so->so_state & SS_ISCONNECTED) == 0) &&
1495 (so->so_proto->pr_flags & PR_CONNREQUIRED))
1497 return (kn->kn_data >= so->so_snd.sb_lowat);
1502 filt_solisten(struct knote *kn, long hint)
1504 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1506 kn->kn_data = so->so_qlen - so->so_incqlen;
1507 return (! TAILQ_EMPTY(&so->so_comp));