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>
56 #include <vm/vm_zone.h>
58 #include <machine/limits.h>
60 static int filt_sorattach(struct knote *kn);
61 static void filt_sordetach(struct knote *kn);
62 static int filt_soread(struct knote *kn, long hint);
63 static int filt_sowattach(struct knote *kn);
64 static void filt_sowdetach(struct knote *kn);
65 static int filt_sowrite(struct knote *kn, long hint);
66 static int filt_solisten(struct knote *kn, long hint);
68 static struct filterops solisten_filtops =
69 { 1, filt_sorattach, filt_sordetach, filt_solisten };
71 struct filterops so_rwfiltops[] = {
72 { 1, filt_sorattach, filt_sordetach, filt_soread },
73 { 1, filt_sowattach, filt_sowdetach, filt_sowrite },
76 struct vm_zone *socket_zone;
77 so_gen_t so_gencnt; /* generation count for sockets */
79 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
80 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
82 SYSCTL_DECL(_kern_ipc);
84 static int somaxconn = SOMAXCONN;
85 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
86 &somaxconn, 0, "Maximum pending socket connection queue size");
89 * Socket operation routines.
90 * These routines are called by the routines in
91 * sys_socket.c or from a system process, and
92 * implement the semantics of socket operations by
93 * switching out to the protocol specific routines.
97 * Get a socket structure from our zone, and initialize it.
98 * We don't implement `waitok' yet (see comments in uipc_domain.c).
99 * Note that it would probably be better to allocate socket
100 * and PCB at the same time, but I'm not convinced that all
101 * the protocols can be easily modified to do this.
109 so = zalloci(socket_zone);
111 /* XXX race condition for reentrant kernel */
112 bzero(so, sizeof *so);
113 so->so_gencnt = ++so_gencnt;
114 so->so_zone = socket_zone;
115 TAILQ_INIT(&so->so_aiojobq);
121 socreate(dom, aso, type, proto, p)
128 register struct protosw *prp;
129 register struct socket *so;
133 prp = pffindproto(dom, proto, type);
135 prp = pffindtype(dom, type);
136 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
137 return (EPROTONOSUPPORT);
138 if (prp->pr_type != type)
140 so = soalloc(p != 0);
144 TAILQ_INIT(&so->so_incomp);
145 TAILQ_INIT(&so->so_comp);
147 so->so_cred = p->p_ucred;
150 error = (*prp->pr_usrreqs->pru_attach)(so, proto, p);
152 so->so_state |= SS_NOFDREF;
163 struct sockaddr *nam;
169 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
179 so->so_gencnt = ++so_gencnt;
180 if (so->so_rcv.sb_hiwat)
181 (void)chgsbsize(so->so_cred->cr_uid,
182 -(rlim_t)so->so_rcv.sb_hiwat);
183 if (so->so_snd.sb_hiwat)
184 (void)chgsbsize(so->so_cred->cr_uid,
185 -(rlim_t)so->so_snd.sb_hiwat);
187 zfreei(so->so_zone, so);
191 solisten(so, backlog, p)
192 register struct socket *so;
199 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p);
204 if (TAILQ_EMPTY(&so->so_comp))
205 so->so_options |= SO_ACCEPTCONN;
206 if (backlog < 0 || backlog > somaxconn)
208 so->so_qlimit = backlog;
215 register struct socket *so;
217 struct socket *head = so->so_head;
219 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
222 if (so->so_state & SS_INCOMP) {
223 TAILQ_REMOVE(&head->so_incomp, so, so_list);
225 } else if (so->so_state & SS_COMP) {
227 * We must not decommission a socket that's
228 * on the accept(2) queue. If we do, then
229 * accept(2) may hang after select(2) indicated
230 * that the listening socket was ready.
234 panic("sofree: not queued");
237 so->so_state &= ~SS_INCOMP;
240 sbrelease(&so->so_snd, so);
246 * Close a socket on last file table reference removal.
247 * Initiate disconnect if connected.
248 * Free socket when disconnect complete.
252 register struct socket *so;
254 int s = splnet(); /* conservative */
257 funsetown(so->so_sigio);
258 if (so->so_options & SO_ACCEPTCONN) {
259 struct socket *sp, *sonext;
261 sp = TAILQ_FIRST(&so->so_incomp);
262 for (; sp != NULL; sp = sonext) {
263 sonext = TAILQ_NEXT(sp, so_list);
266 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
267 sonext = TAILQ_NEXT(sp, so_list);
268 /* Dequeue from so_comp since sofree() won't do it */
269 TAILQ_REMOVE(&so->so_comp, sp, so_list);
271 sp->so_state &= ~SS_COMP;
278 if (so->so_state & SS_ISCONNECTED) {
279 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
280 error = sodisconnect(so);
284 if (so->so_options & SO_LINGER) {
285 if ((so->so_state & SS_ISDISCONNECTING) &&
286 (so->so_state & SS_NBIO))
288 while (so->so_state & SS_ISCONNECTED) {
289 error = tsleep((caddr_t)&so->so_timeo,
290 PSOCK | PCATCH, "soclos", so->so_linger * hz);
298 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
303 if (so->so_state & SS_NOFDREF)
304 panic("soclose: NOFDREF");
305 so->so_state |= SS_NOFDREF;
312 * Must be called at splnet...
320 error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
330 register struct socket *so;
331 struct sockaddr **nam;
336 if ((so->so_state & SS_NOFDREF) == 0)
337 panic("soaccept: !NOFDREF");
338 so->so_state &= ~SS_NOFDREF;
339 if ((so->so_state & SS_ISDISCONNECTED) == 0)
340 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
351 soconnect(so, nam, p)
352 register struct socket *so;
353 struct sockaddr *nam;
359 if (so->so_options & SO_ACCEPTCONN)
363 * If protocol is connection-based, can only connect once.
364 * Otherwise, if connected, try to disconnect first.
365 * This allows user to disconnect by connecting to, e.g.,
368 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
369 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
370 (error = sodisconnect(so))))
373 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p);
380 register struct socket *so1;
386 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
393 register struct socket *so;
398 if ((so->so_state & SS_ISCONNECTED) == 0) {
402 if (so->so_state & SS_ISDISCONNECTING) {
406 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
412 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
415 * If send must go all at once and message is larger than
416 * send buffering, then hard error.
417 * Lock against other senders.
418 * If must go all at once and not enough room now, then
419 * inform user that this would block and do nothing.
420 * Otherwise, if nonblocking, send as much as possible.
421 * The data to be sent is described by "uio" if nonzero,
422 * otherwise by the mbuf chain "top" (which must be null
423 * if uio is not). Data provided in mbuf chain must be small
424 * enough to send all at once.
426 * Returns nonzero on error, timeout or signal; callers
427 * must check for short counts if EINTR/ERESTART are returned.
428 * Data and control buffers are freed on return.
431 sosend(so, addr, uio, top, control, flags, p)
432 register struct socket *so;
433 struct sockaddr *addr;
436 struct mbuf *control;
441 register struct mbuf *m;
442 register long space, len, resid;
443 int clen = 0, error, s, dontroute, mlen;
444 int atomic = sosendallatonce(so) || top;
447 resid = uio->uio_resid;
449 resid = top->m_pkthdr.len;
451 * In theory resid should be unsigned.
452 * However, space must be signed, as it might be less than 0
453 * if we over-committed, and we must use a signed comparison
454 * of space and resid. On the other hand, a negative resid
455 * causes us to loop sending 0-length segments to the protocol.
457 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
458 * type sockets since that's an error.
460 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
466 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
467 (so->so_proto->pr_flags & PR_ATOMIC);
469 p->p_stats->p_ru.ru_msgsnd++;
471 clen = control->m_len;
472 #define snderr(errno) { error = errno; splx(s); goto release; }
475 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
480 if (so->so_state & SS_CANTSENDMORE)
483 error = so->so_error;
488 if ((so->so_state & SS_ISCONNECTED) == 0) {
490 * `sendto' and `sendmsg' is allowed on a connection-
491 * based socket if it supports implied connect.
492 * Return ENOTCONN if not connected and no address is
495 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
496 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
497 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
498 !(resid == 0 && clen != 0))
500 } else if (addr == 0)
501 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
502 ENOTCONN : EDESTADDRREQ);
504 space = sbspace(&so->so_snd);
507 if ((atomic && resid > so->so_snd.sb_hiwat) ||
508 clen > so->so_snd.sb_hiwat)
510 if (space < resid + clen && uio &&
511 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
512 if (so->so_state & SS_NBIO)
514 sbunlock(&so->so_snd);
515 error = sbwait(&so->so_snd);
527 * Data is prepackaged in "top".
531 top->m_flags |= M_EOR;
534 MGETHDR(m, M_WAIT, MT_DATA);
541 m->m_pkthdr.rcvif = (struct ifnet *)0;
543 MGET(m, M_WAIT, MT_DATA);
550 if (resid >= MINCLSIZE) {
552 if ((m->m_flags & M_EXT) == 0)
555 len = min(min(mlen, resid), space);
558 len = min(min(mlen, resid), space);
560 * For datagram protocols, leave room
561 * for protocol headers in first mbuf.
563 if (atomic && top == 0 && len < mlen)
567 error = uiomove(mtod(m, caddr_t), (int)len, uio);
568 resid = uio->uio_resid;
571 top->m_pkthdr.len += len;
577 top->m_flags |= M_EOR;
580 } while (space > 0 && atomic);
582 so->so_options |= SO_DONTROUTE;
583 s = splnet(); /* XXX */
585 * XXX all the SS_CANTSENDMORE checks previously
586 * done could be out of date. We could have recieved
587 * a reset packet in an interrupt or maybe we slept
588 * while doing page faults in uiomove() etc. We could
589 * probably recheck again inside the splnet() protection
590 * here, but there are probably other places that this
591 * also happens. We must rethink this.
593 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
594 (flags & MSG_OOB) ? PRUS_OOB :
596 * If the user set MSG_EOF, the protocol
597 * understands this flag and nothing left to
598 * send then use PRU_SEND_EOF instead of PRU_SEND.
600 ((flags & MSG_EOF) &&
601 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
604 /* If there is more to send set PRUS_MORETOCOME */
605 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
606 top, addr, control, p);
609 so->so_options &= ~SO_DONTROUTE;
616 } while (resid && space > 0);
620 sbunlock(&so->so_snd);
630 * Implement receive operations on a socket.
631 * We depend on the way that records are added to the sockbuf
632 * by sbappend*. In particular, each record (mbufs linked through m_next)
633 * must begin with an address if the protocol so specifies,
634 * followed by an optional mbuf or mbufs containing ancillary data,
635 * and then zero or more mbufs of data.
636 * In order to avoid blocking network interrupts for the entire time here,
637 * we splx() while doing the actual copy to user space.
638 * Although the sockbuf is locked, new data may still be appended,
639 * and thus we must maintain consistency of the sockbuf during that time.
641 * The caller may receive the data as a single mbuf chain by supplying
642 * an mbuf **mp0 for use in returning the chain. The uio is then used
643 * only for the count in uio_resid.
646 soreceive(so, psa, uio, mp0, controlp, flagsp)
647 register struct socket *so;
648 struct sockaddr **psa;
651 struct mbuf **controlp;
654 register struct mbuf *m, **mp;
655 register int flags, len, error, s, offset;
656 struct protosw *pr = so->so_proto;
657 struct mbuf *nextrecord;
659 int orig_resid = uio->uio_resid;
667 flags = *flagsp &~ MSG_EOR;
670 if (flags & MSG_OOB) {
671 m = m_get(M_WAIT, MT_DATA);
674 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
678 error = uiomove(mtod(m, caddr_t),
679 (int) min(uio->uio_resid, m->m_len), uio);
681 } while (uio->uio_resid && error == 0 && m);
688 *mp = (struct mbuf *)0;
689 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
690 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
693 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
698 m = so->so_rcv.sb_mb;
700 * If we have less data than requested, block awaiting more
701 * (subject to any timeout) if:
702 * 1. the current count is less than the low water mark, or
703 * 2. MSG_WAITALL is set, and it is possible to do the entire
704 * receive operation at once if we block (resid <= hiwat).
705 * 3. MSG_DONTWAIT is not set
706 * If MSG_WAITALL is set but resid is larger than the receive buffer,
707 * we have to do the receive in sections, and thus risk returning
708 * a short count if a timeout or signal occurs after we start.
710 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
711 so->so_rcv.sb_cc < uio->uio_resid) &&
712 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
713 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
714 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
715 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
719 error = so->so_error;
720 if ((flags & MSG_PEEK) == 0)
724 if (so->so_state & SS_CANTRCVMORE) {
730 for (; m; m = m->m_next)
731 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
732 m = so->so_rcv.sb_mb;
735 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
736 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
740 if (uio->uio_resid == 0)
742 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
746 sbunlock(&so->so_rcv);
747 error = sbwait(&so->so_rcv);
755 uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
756 nextrecord = m->m_nextpkt;
757 if (pr->pr_flags & PR_ADDR) {
758 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
761 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
763 if (flags & MSG_PEEK) {
766 sbfree(&so->so_rcv, m);
767 MFREE(m, so->so_rcv.sb_mb);
768 m = so->so_rcv.sb_mb;
771 while (m && m->m_type == MT_CONTROL && error == 0) {
772 if (flags & MSG_PEEK) {
774 *controlp = m_copy(m, 0, m->m_len);
777 sbfree(&so->so_rcv, m);
779 if (pr->pr_domain->dom_externalize &&
780 mtod(m, struct cmsghdr *)->cmsg_type ==
782 error = (*pr->pr_domain->dom_externalize)(m);
784 so->so_rcv.sb_mb = m->m_next;
786 m = so->so_rcv.sb_mb;
788 MFREE(m, so->so_rcv.sb_mb);
789 m = so->so_rcv.sb_mb;
794 controlp = &(*controlp)->m_next;
798 if ((flags & MSG_PEEK) == 0)
799 m->m_nextpkt = nextrecord;
801 if (type == MT_OOBDATA)
806 while (m && uio->uio_resid > 0 && error == 0) {
807 if (m->m_type == MT_OOBDATA) {
808 if (type != MT_OOBDATA)
810 } else if (type == MT_OOBDATA)
813 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
815 so->so_state &= ~SS_RCVATMARK;
816 len = uio->uio_resid;
817 if (so->so_oobmark && len > so->so_oobmark - offset)
818 len = so->so_oobmark - offset;
819 if (len > m->m_len - moff)
820 len = m->m_len - moff;
822 * If mp is set, just pass back the mbufs.
823 * Otherwise copy them out via the uio, then free.
824 * Sockbuf must be consistent here (points to current mbuf,
825 * it points to next record) when we drop priority;
826 * we must note any additions to the sockbuf when we
827 * block interrupts again.
831 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
836 uio->uio_resid -= len;
837 if (len == m->m_len - moff) {
838 if (m->m_flags & M_EOR)
840 if (flags & MSG_PEEK) {
844 nextrecord = m->m_nextpkt;
845 sbfree(&so->so_rcv, m);
849 so->so_rcv.sb_mb = m = m->m_next;
850 *mp = (struct mbuf *)0;
852 MFREE(m, so->so_rcv.sb_mb);
853 m = so->so_rcv.sb_mb;
856 m->m_nextpkt = nextrecord;
859 if (flags & MSG_PEEK)
863 *mp = m_copym(m, 0, len, M_WAIT);
866 so->so_rcv.sb_cc -= len;
869 if (so->so_oobmark) {
870 if ((flags & MSG_PEEK) == 0) {
871 so->so_oobmark -= len;
872 if (so->so_oobmark == 0) {
873 so->so_state |= SS_RCVATMARK;
878 if (offset == so->so_oobmark)
885 * If the MSG_WAITALL flag is set (for non-atomic socket),
886 * we must not quit until "uio->uio_resid == 0" or an error
887 * termination. If a signal/timeout occurs, return
888 * with a short count but without error.
889 * Keep sockbuf locked against other readers.
891 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
892 !sosendallatonce(so) && !nextrecord) {
893 if (so->so_error || so->so_state & SS_CANTRCVMORE)
895 error = sbwait(&so->so_rcv);
897 sbunlock(&so->so_rcv);
901 m = so->so_rcv.sb_mb;
903 nextrecord = m->m_nextpkt;
907 if (m && pr->pr_flags & PR_ATOMIC) {
909 if ((flags & MSG_PEEK) == 0)
910 (void) sbdroprecord(&so->so_rcv);
912 if ((flags & MSG_PEEK) == 0) {
914 so->so_rcv.sb_mb = nextrecord;
915 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
916 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
918 if (orig_resid == uio->uio_resid && orig_resid &&
919 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
920 sbunlock(&so->so_rcv);
928 sbunlock(&so->so_rcv);
935 register struct socket *so;
938 register struct protosw *pr = so->so_proto;
944 return ((*pr->pr_usrreqs->pru_shutdown)(so));
950 register struct socket *so;
952 register struct sockbuf *sb = &so->so_rcv;
953 register struct protosw *pr = so->so_proto;
957 sb->sb_flags |= SB_NOINTR;
958 (void) sblock(sb, M_WAITOK);
963 bzero((caddr_t)sb, sizeof (*sb));
965 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
966 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
971 * Perhaps this routine, and sooptcopyout(), below, ought to come in
972 * an additional variant to handle the case where the option value needs
973 * to be some kind of integer, but not a specific size.
974 * In addition to their use here, these functions are also called by the
975 * protocol-level pr_ctloutput() routines.
978 sooptcopyin(sopt, buf, len, minlen)
979 struct sockopt *sopt;
987 * If the user gives us more than we wanted, we ignore it,
988 * but if we don't get the minimum length the caller
989 * wants, we return EINVAL. On success, sopt->sopt_valsize
990 * is set to however much we actually retrieved.
992 if ((valsize = sopt->sopt_valsize) < minlen)
995 sopt->sopt_valsize = valsize = len;
997 if (sopt->sopt_p != 0)
998 return (copyin(sopt->sopt_val, buf, valsize));
1000 bcopy(sopt->sopt_val, buf, valsize);
1007 struct sockopt *sopt;
1015 if (sopt->sopt_level != SOL_SOCKET) {
1016 if (so->so_proto && so->so_proto->pr_ctloutput)
1017 return ((*so->so_proto->pr_ctloutput)
1019 error = ENOPROTOOPT;
1021 switch (sopt->sopt_name) {
1023 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1027 so->so_linger = l.l_linger;
1029 so->so_options |= SO_LINGER;
1031 so->so_options &= ~SO_LINGER;
1037 case SO_USELOOPBACK:
1043 error = sooptcopyin(sopt, &optval, sizeof optval,
1048 so->so_options |= sopt->sopt_name;
1050 so->so_options &= ~sopt->sopt_name;
1057 error = sooptcopyin(sopt, &optval, sizeof optval,
1063 * Values < 1 make no sense for any of these
1064 * options, so disallow them.
1071 switch (sopt->sopt_name) {
1074 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1075 &so->so_snd : &so->so_rcv, (u_long)optval,
1076 so, curproc) == 0) {
1083 * Make sure the low-water is never greater than
1087 so->so_snd.sb_lowat =
1088 (optval > so->so_snd.sb_hiwat) ?
1089 so->so_snd.sb_hiwat : optval;
1092 so->so_rcv.sb_lowat =
1093 (optval > so->so_rcv.sb_hiwat) ?
1094 so->so_rcv.sb_hiwat : optval;
1101 error = sooptcopyin(sopt, &tv, sizeof tv,
1106 /* assert(hz > 0); */
1107 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1108 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1112 /* assert(tick > 0); */
1113 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1114 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1115 if (val > SHRT_MAX) {
1120 switch (sopt->sopt_name) {
1122 so->so_snd.sb_timeo = val;
1125 so->so_rcv.sb_timeo = val;
1131 error = ENOPROTOOPT;
1134 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1135 (void) ((*so->so_proto->pr_ctloutput)
1143 /* Helper routine for getsockopt */
1145 sooptcopyout(sopt, buf, len)
1146 struct sockopt *sopt;
1156 * Documented get behavior is that we always return a value,
1157 * possibly truncated to fit in the user's buffer.
1158 * Traditional behavior is that we always tell the user
1159 * precisely how much we copied, rather than something useful
1160 * like the total amount we had available for her.
1161 * Note that this interface is not idempotent; the entire answer must
1162 * generated ahead of time.
1164 valsize = min(len, sopt->sopt_valsize);
1165 sopt->sopt_valsize = valsize;
1166 if (sopt->sopt_val != 0) {
1167 if (sopt->sopt_p != 0)
1168 error = copyout(buf, sopt->sopt_val, valsize);
1170 bcopy(buf, sopt->sopt_val, valsize);
1178 struct sockopt *sopt;
1185 if (sopt->sopt_level != SOL_SOCKET) {
1186 if (so->so_proto && so->so_proto->pr_ctloutput) {
1187 return ((*so->so_proto->pr_ctloutput)
1190 return (ENOPROTOOPT);
1192 switch (sopt->sopt_name) {
1194 l.l_onoff = so->so_options & SO_LINGER;
1195 l.l_linger = so->so_linger;
1196 error = sooptcopyout(sopt, &l, sizeof l);
1199 case SO_USELOOPBACK:
1208 optval = so->so_options & sopt->sopt_name;
1210 error = sooptcopyout(sopt, &optval, sizeof optval);
1214 optval = so->so_type;
1218 optval = so->so_error;
1223 optval = so->so_snd.sb_hiwat;
1227 optval = so->so_rcv.sb_hiwat;
1231 optval = so->so_snd.sb_lowat;
1235 optval = so->so_rcv.sb_lowat;
1240 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1241 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1243 tv.tv_sec = optval / hz;
1244 tv.tv_usec = (optval % hz) * tick;
1245 error = sooptcopyout(sopt, &tv, sizeof tv);
1249 error = ENOPROTOOPT;
1256 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1258 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1260 struct mbuf *m, *m_prev;
1261 int sopt_size = sopt->sopt_valsize;
1263 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1266 if (sopt_size > MLEN) {
1267 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1268 if ((m->m_flags & M_EXT) == 0) {
1272 m->m_len = min(MCLBYTES, sopt_size);
1274 m->m_len = min(MLEN, sopt_size);
1276 sopt_size -= m->m_len;
1281 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1286 if (sopt_size > MLEN) {
1287 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1288 if ((m->m_flags & M_EXT) == 0) {
1292 m->m_len = min(MCLBYTES, sopt_size);
1294 m->m_len = min(MLEN, sopt_size);
1296 sopt_size -= m->m_len;
1303 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1305 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1307 struct mbuf *m0 = m;
1309 if (sopt->sopt_val == NULL)
1311 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1312 if (sopt->sopt_p != NULL) {
1315 error = copyin(sopt->sopt_val, mtod(m, char *),
1322 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1323 sopt->sopt_valsize -= m->m_len;
1324 (caddr_t)sopt->sopt_val += m->m_len;
1327 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1328 panic("ip6_sooptmcopyin");
1332 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1334 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1336 struct mbuf *m0 = m;
1339 if (sopt->sopt_val == NULL)
1341 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1342 if (sopt->sopt_p != NULL) {
1345 error = copyout(mtod(m, char *), sopt->sopt_val,
1352 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1353 sopt->sopt_valsize -= m->m_len;
1354 (caddr_t)sopt->sopt_val += m->m_len;
1355 valsize += m->m_len;
1359 /* enough soopt buffer should be given from user-land */
1363 sopt->sopt_valsize = valsize;
1369 register struct socket *so;
1371 if (so->so_sigio != NULL)
1372 pgsigio(so->so_sigio, SIGURG, 0);
1373 selwakeup(&so->so_rcv.sb_sel);
1377 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p)
1382 if (events & (POLLIN | POLLRDNORM))
1384 revents |= events & (POLLIN | POLLRDNORM);
1386 if (events & (POLLOUT | POLLWRNORM))
1387 if (sowriteable(so))
1388 revents |= events & (POLLOUT | POLLWRNORM);
1390 if (events & (POLLPRI | POLLRDBAND))
1391 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1392 revents |= events & (POLLPRI | POLLRDBAND);
1395 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1396 selrecord(p, &so->so_rcv.sb_sel);
1397 so->so_rcv.sb_flags |= SB_SEL;
1400 if (events & (POLLOUT | POLLWRNORM)) {
1401 selrecord(p, &so->so_snd.sb_sel);
1402 so->so_snd.sb_flags |= SB_SEL;
1411 filt_sorattach(struct knote *kn)
1413 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1416 if (so->so_options & SO_ACCEPTCONN)
1417 kn->kn_fop = &solisten_filtops;
1418 SLIST_INSERT_HEAD(&so->so_rcv.sb_sel.si_note, kn, kn_selnext);
1419 so->so_rcv.sb_flags |= SB_KNOTE;
1425 filt_sordetach(struct knote *kn)
1427 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1430 SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1431 if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1432 so->so_rcv.sb_flags &= ~SB_KNOTE;
1438 filt_soread(struct knote *kn, long hint)
1440 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1442 kn->kn_data = so->so_rcv.sb_cc;
1443 if (so->so_state & SS_CANTRCVMORE) {
1444 kn->kn_flags |= EV_EOF;
1447 return (kn->kn_data > 0);
1451 filt_sowattach(struct knote *kn)
1453 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1456 SLIST_INSERT_HEAD(&so->so_snd.sb_sel.si_note, kn, kn_selnext);
1457 so->so_snd.sb_flags |= SB_KNOTE;
1463 filt_sowdetach(struct knote *kn)
1465 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1468 SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1469 if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1470 so->so_snd.sb_flags &= ~SB_KNOTE;
1476 filt_sowrite(struct knote *kn, long hint)
1478 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1480 kn->kn_data = sbspace(&so->so_snd);
1481 if (so->so_state & SS_CANTSENDMORE) {
1482 kn->kn_flags |= EV_EOF;
1485 if (((so->so_state & SS_ISCONNECTED) == 0) &&
1486 (so->so_proto->pr_flags & PR_CONNREQUIRED))
1488 return (kn->kn_data >= so->so_snd.sb_lowat);
1493 filt_solisten(struct knote *kn, long hint)
1495 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1497 kn->kn_data = so->so_qlen - so->so_incqlen;
1498 return (! TAILQ_EMPTY(&so->so_comp));