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 * $Id: uipc_socket.c,v 1.59 1999/06/04 02:27:02 peter Exp $
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/kernel.h>
44 #include <sys/malloc.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/resourcevar.h>
51 #include <sys/signalvar.h>
52 #include <sys/sysctl.h>
54 #include <vm/vm_zone.h>
56 #include <machine/limits.h>
58 struct vm_zone *socket_zone;
59 so_gen_t so_gencnt; /* generation count for sockets */
61 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
62 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
64 SYSCTL_DECL(_kern_ipc);
66 static int somaxconn = SOMAXCONN;
67 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
68 &somaxconn, 0, "Maximum pending socket connection queue size");
71 * Socket operation routines.
72 * These routines are called by the routines in
73 * sys_socket.c or from a system process, and
74 * implement the semantics of socket operations by
75 * switching out to the protocol specific routines.
79 * Get a socket structure from our zone, and initialize it.
80 * We don't implement `waitok' yet (see comments in uipc_domain.c).
81 * Note that it would probably be better to allocate socket
82 * and PCB at the same time, but I'm not convinced that all
83 * the protocols can be easily modified to do this.
91 so = zalloci(socket_zone);
93 /* XXX race condition for reentrant kernel */
94 bzero(so, sizeof *so);
95 so->so_gencnt = ++so_gencnt;
96 so->so_zone = socket_zone;
102 socreate(dom, aso, type, proto, p)
109 register struct protosw *prp;
110 register struct socket *so;
114 prp = pffindproto(dom, proto, type);
116 prp = pffindtype(dom, type);
117 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
118 return (EPROTONOSUPPORT);
119 if (prp->pr_type != type)
121 so = soalloc(p != 0);
125 TAILQ_INIT(&so->so_incomp);
126 TAILQ_INIT(&so->so_comp);
129 so->so_cred = p->p_cred;
130 so->so_cred->p_refcnt++;
131 } else so->so_cred = NULL;
133 error = (*prp->pr_usrreqs->pru_attach)(so, proto, p);
135 so->so_state |= SS_NOFDREF;
146 struct sockaddr *nam;
152 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
161 so->so_gencnt = ++so_gencnt;
162 if (so->so_cred && --so->so_cred->p_refcnt == 0) {
163 crfree(so->so_cred->pc_ucred);
164 FREE(so->so_cred, M_SUBPROC);
166 zfreei(so->so_zone, so);
170 solisten(so, backlog, p)
171 register struct socket *so;
178 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p);
183 if (so->so_comp.tqh_first == NULL)
184 so->so_options |= SO_ACCEPTCONN;
185 if (backlog < 0 || backlog > somaxconn)
187 so->so_qlimit = backlog;
194 register struct socket *so;
196 struct socket *head = so->so_head;
198 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
201 if (so->so_state & SS_INCOMP) {
202 TAILQ_REMOVE(&head->so_incomp, so, so_list);
204 } else if (so->so_state & SS_COMP) {
206 * We must not decommission a socket that's
207 * on the accept(2) queue. If we do, then
208 * accept(2) may hang after select(2) indicated
209 * that the listening socket was ready.
213 panic("sofree: not queued");
216 so->so_state &= ~SS_INCOMP;
219 sbrelease(&so->so_snd);
225 * Close a socket on last file table reference removal.
226 * Initiate disconnect if connected.
227 * Free socket when disconnect complete.
231 register struct socket *so;
233 int s = splnet(); /* conservative */
236 funsetown(so->so_sigio);
237 if (so->so_options & SO_ACCEPTCONN) {
238 struct socket *sp, *sonext;
240 for (sp = so->so_incomp.tqh_first; sp != NULL; sp = sonext) {
241 sonext = sp->so_list.tqe_next;
244 for (sp = so->so_comp.tqh_first; sp != NULL; sp = sonext) {
245 sonext = sp->so_list.tqe_next;
246 /* Dequeue from so_comp since sofree() won't do it */
247 TAILQ_REMOVE(&so->so_comp, sp, so_list);
249 sp->so_state &= ~SS_COMP;
256 if (so->so_state & SS_ISCONNECTED) {
257 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
258 error = sodisconnect(so);
262 if (so->so_options & SO_LINGER) {
263 if ((so->so_state & SS_ISDISCONNECTING) &&
264 (so->so_state & SS_NBIO))
266 while (so->so_state & SS_ISCONNECTED) {
267 error = tsleep((caddr_t)&so->so_timeo,
268 PSOCK | PCATCH, "soclos", so->so_linger * hz);
276 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
281 if (so->so_state & SS_NOFDREF)
282 panic("soclose: NOFDREF");
283 so->so_state |= SS_NOFDREF;
290 * Must be called at splnet...
297 return (*so->so_proto->pr_usrreqs->pru_abort)(so);
302 register struct socket *so;
303 struct sockaddr **nam;
308 if ((so->so_state & SS_NOFDREF) == 0)
309 panic("soaccept: !NOFDREF");
310 so->so_state &= ~SS_NOFDREF;
311 if ((so->so_state & SS_ISDISCONNECTED) == 0)
312 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
323 soconnect(so, nam, p)
324 register struct socket *so;
325 struct sockaddr *nam;
331 if (so->so_options & SO_ACCEPTCONN)
335 * If protocol is connection-based, can only connect once.
336 * Otherwise, if connected, try to disconnect first.
337 * This allows user to disconnect by connecting to, e.g.,
340 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
341 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
342 (error = sodisconnect(so))))
345 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p);
352 register struct socket *so1;
358 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
365 register struct socket *so;
370 if ((so->so_state & SS_ISCONNECTED) == 0) {
374 if (so->so_state & SS_ISDISCONNECTING) {
378 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
384 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
387 * If send must go all at once and message is larger than
388 * send buffering, then hard error.
389 * Lock against other senders.
390 * If must go all at once and not enough room now, then
391 * inform user that this would block and do nothing.
392 * Otherwise, if nonblocking, send as much as possible.
393 * The data to be sent is described by "uio" if nonzero,
394 * otherwise by the mbuf chain "top" (which must be null
395 * if uio is not). Data provided in mbuf chain must be small
396 * enough to send all at once.
398 * Returns nonzero on error, timeout or signal; callers
399 * must check for short counts if EINTR/ERESTART are returned.
400 * Data and control buffers are freed on return.
403 sosend(so, addr, uio, top, control, flags, p)
404 register struct socket *so;
405 struct sockaddr *addr;
408 struct mbuf *control;
413 register struct mbuf *m;
414 register long space, len, resid;
415 int clen = 0, error, s, dontroute, mlen;
416 int atomic = sosendallatonce(so) || top;
419 resid = uio->uio_resid;
421 resid = top->m_pkthdr.len;
423 * In theory resid should be unsigned.
424 * However, space must be signed, as it might be less than 0
425 * if we over-committed, and we must use a signed comparison
426 * of space and resid. On the other hand, a negative resid
427 * causes us to loop sending 0-length segments to the protocol.
429 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
430 * type sockets since that's an error.
432 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
438 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
439 (so->so_proto->pr_flags & PR_ATOMIC);
441 p->p_stats->p_ru.ru_msgsnd++;
443 clen = control->m_len;
444 #define snderr(errno) { error = errno; splx(s); goto release; }
447 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
452 if (so->so_state & SS_CANTSENDMORE)
455 error = so->so_error;
460 if ((so->so_state & SS_ISCONNECTED) == 0) {
462 * `sendto' and `sendmsg' is allowed on a connection-
463 * based socket if it supports implied connect.
464 * Return ENOTCONN if not connected and no address is
467 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
468 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
469 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
470 !(resid == 0 && clen != 0))
472 } else if (addr == 0)
473 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
474 ENOTCONN : EDESTADDRREQ);
476 space = sbspace(&so->so_snd);
479 if ((atomic && resid > so->so_snd.sb_hiwat) ||
480 clen > so->so_snd.sb_hiwat)
482 if (space < resid + clen && uio &&
483 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
484 if (so->so_state & SS_NBIO)
486 sbunlock(&so->so_snd);
487 error = sbwait(&so->so_snd);
499 * Data is prepackaged in "top".
503 top->m_flags |= M_EOR;
506 MGETHDR(m, M_WAIT, MT_DATA);
509 m->m_pkthdr.rcvif = (struct ifnet *)0;
511 MGET(m, M_WAIT, MT_DATA);
514 if (resid >= MINCLSIZE) {
516 if ((m->m_flags & M_EXT) == 0)
519 len = min(min(mlen, resid), space);
522 len = min(min(mlen, resid), space);
524 * For datagram protocols, leave room
525 * for protocol headers in first mbuf.
527 if (atomic && top == 0 && len < mlen)
531 error = uiomove(mtod(m, caddr_t), (int)len, uio);
532 resid = uio->uio_resid;
535 top->m_pkthdr.len += len;
541 top->m_flags |= M_EOR;
544 } while (space > 0 && atomic);
546 so->so_options |= SO_DONTROUTE;
547 s = splnet(); /* XXX */
549 * XXX all the SS_CANTSENDMORE checks previously
550 * done could be out of date. We could have recieved
551 * a reset packet in an interrupt or maybe we slept
552 * while doing page faults in uiomove() etc. We could
553 * probably recheck again inside the splnet() protection
554 * here, but there are probably other places that this
555 * also happens. We must rethink this.
557 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
558 (flags & MSG_OOB) ? PRUS_OOB :
560 * If the user set MSG_EOF, the protocol
561 * understands this flag and nothing left to
562 * send then use PRU_SEND_EOF instead of PRU_SEND.
564 ((flags & MSG_EOF) &&
565 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
568 /* If there is more to send set PRUS_MORETOCOME */
569 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
570 top, addr, control, p);
573 so->so_options &= ~SO_DONTROUTE;
580 } while (resid && space > 0);
584 sbunlock(&so->so_snd);
594 * Implement receive operations on a socket.
595 * We depend on the way that records are added to the sockbuf
596 * by sbappend*. In particular, each record (mbufs linked through m_next)
597 * must begin with an address if the protocol so specifies,
598 * followed by an optional mbuf or mbufs containing ancillary data,
599 * and then zero or more mbufs of data.
600 * In order to avoid blocking network interrupts for the entire time here,
601 * we splx() while doing the actual copy to user space.
602 * Although the sockbuf is locked, new data may still be appended,
603 * and thus we must maintain consistency of the sockbuf during that time.
605 * The caller may receive the data as a single mbuf chain by supplying
606 * an mbuf **mp0 for use in returning the chain. The uio is then used
607 * only for the count in uio_resid.
610 soreceive(so, psa, uio, mp0, controlp, flagsp)
611 register struct socket *so;
612 struct sockaddr **psa;
615 struct mbuf **controlp;
618 register struct mbuf *m, **mp;
619 register int flags, len, error, s, offset;
620 struct protosw *pr = so->so_proto;
621 struct mbuf *nextrecord;
623 int orig_resid = uio->uio_resid;
631 flags = *flagsp &~ MSG_EOR;
634 if (flags & MSG_OOB) {
635 m = m_get(M_WAIT, MT_DATA);
636 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
640 error = uiomove(mtod(m, caddr_t),
641 (int) min(uio->uio_resid, m->m_len), uio);
643 } while (uio->uio_resid && error == 0 && m);
650 *mp = (struct mbuf *)0;
651 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
652 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
655 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
660 m = so->so_rcv.sb_mb;
662 * If we have less data than requested, block awaiting more
663 * (subject to any timeout) if:
664 * 1. the current count is less than the low water mark, or
665 * 2. MSG_WAITALL is set, and it is possible to do the entire
666 * receive operation at once if we block (resid <= hiwat).
667 * 3. MSG_DONTWAIT is not set
668 * If MSG_WAITALL is set but resid is larger than the receive buffer,
669 * we have to do the receive in sections, and thus risk returning
670 * a short count if a timeout or signal occurs after we start.
672 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
673 so->so_rcv.sb_cc < uio->uio_resid) &&
674 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
675 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
676 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
677 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
681 error = so->so_error;
682 if ((flags & MSG_PEEK) == 0)
686 if (so->so_state & SS_CANTRCVMORE) {
692 for (; m; m = m->m_next)
693 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
694 m = so->so_rcv.sb_mb;
697 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
698 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
702 if (uio->uio_resid == 0)
704 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
708 sbunlock(&so->so_rcv);
709 error = sbwait(&so->so_rcv);
717 uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
718 nextrecord = m->m_nextpkt;
719 if (pr->pr_flags & PR_ADDR) {
720 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
723 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
725 if (flags & MSG_PEEK) {
728 sbfree(&so->so_rcv, m);
729 MFREE(m, so->so_rcv.sb_mb);
730 m = so->so_rcv.sb_mb;
733 while (m && m->m_type == MT_CONTROL && error == 0) {
734 if (flags & MSG_PEEK) {
736 *controlp = m_copy(m, 0, m->m_len);
739 sbfree(&so->so_rcv, m);
741 if (pr->pr_domain->dom_externalize &&
742 mtod(m, struct cmsghdr *)->cmsg_type ==
744 error = (*pr->pr_domain->dom_externalize)(m);
746 so->so_rcv.sb_mb = m->m_next;
748 m = so->so_rcv.sb_mb;
750 MFREE(m, so->so_rcv.sb_mb);
751 m = so->so_rcv.sb_mb;
756 controlp = &(*controlp)->m_next;
760 if ((flags & MSG_PEEK) == 0)
761 m->m_nextpkt = nextrecord;
763 if (type == MT_OOBDATA)
768 while (m && uio->uio_resid > 0 && error == 0) {
769 if (m->m_type == MT_OOBDATA) {
770 if (type != MT_OOBDATA)
772 } else if (type == MT_OOBDATA)
775 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
777 so->so_state &= ~SS_RCVATMARK;
778 len = uio->uio_resid;
779 if (so->so_oobmark && len > so->so_oobmark - offset)
780 len = so->so_oobmark - offset;
781 if (len > m->m_len - moff)
782 len = m->m_len - moff;
784 * If mp is set, just pass back the mbufs.
785 * Otherwise copy them out via the uio, then free.
786 * Sockbuf must be consistent here (points to current mbuf,
787 * it points to next record) when we drop priority;
788 * we must note any additions to the sockbuf when we
789 * block interrupts again.
793 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
798 uio->uio_resid -= len;
799 if (len == m->m_len - moff) {
800 if (m->m_flags & M_EOR)
802 if (flags & MSG_PEEK) {
806 nextrecord = m->m_nextpkt;
807 sbfree(&so->so_rcv, m);
811 so->so_rcv.sb_mb = m = m->m_next;
812 *mp = (struct mbuf *)0;
814 MFREE(m, so->so_rcv.sb_mb);
815 m = so->so_rcv.sb_mb;
818 m->m_nextpkt = nextrecord;
821 if (flags & MSG_PEEK)
825 *mp = m_copym(m, 0, len, M_WAIT);
828 so->so_rcv.sb_cc -= len;
831 if (so->so_oobmark) {
832 if ((flags & MSG_PEEK) == 0) {
833 so->so_oobmark -= len;
834 if (so->so_oobmark == 0) {
835 so->so_state |= SS_RCVATMARK;
840 if (offset == so->so_oobmark)
847 * If the MSG_WAITALL flag is set (for non-atomic socket),
848 * we must not quit until "uio->uio_resid == 0" or an error
849 * termination. If a signal/timeout occurs, return
850 * with a short count but without error.
851 * Keep sockbuf locked against other readers.
853 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
854 !sosendallatonce(so) && !nextrecord) {
855 if (so->so_error || so->so_state & SS_CANTRCVMORE)
857 error = sbwait(&so->so_rcv);
859 sbunlock(&so->so_rcv);
863 m = so->so_rcv.sb_mb;
865 nextrecord = m->m_nextpkt;
869 if (m && pr->pr_flags & PR_ATOMIC) {
871 if ((flags & MSG_PEEK) == 0)
872 (void) sbdroprecord(&so->so_rcv);
874 if ((flags & MSG_PEEK) == 0) {
876 so->so_rcv.sb_mb = nextrecord;
877 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
878 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
880 if (orig_resid == uio->uio_resid && orig_resid &&
881 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
882 sbunlock(&so->so_rcv);
890 sbunlock(&so->so_rcv);
897 register struct socket *so;
900 register struct protosw *pr = so->so_proto;
906 return ((*pr->pr_usrreqs->pru_shutdown)(so));
912 register struct socket *so;
914 register struct sockbuf *sb = &so->so_rcv;
915 register struct protosw *pr = so->so_proto;
919 sb->sb_flags |= SB_NOINTR;
920 (void) sblock(sb, M_WAITOK);
925 bzero((caddr_t)sb, sizeof (*sb));
927 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
928 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
933 * Perhaps this routine, and sooptcopyout(), below, ought to come in
934 * an additional variant to handle the case where the option value needs
935 * to be some kind of integer, but not a specific size.
936 * In addition to their use here, these functions are also called by the
937 * protocol-level pr_ctloutput() routines.
940 sooptcopyin(sopt, buf, len, minlen)
941 struct sockopt *sopt;
949 * If the user gives us more than we wanted, we ignore it,
950 * but if we don't get the minimum length the caller
951 * wants, we return EINVAL. On success, sopt->sopt_valsize
952 * is set to however much we actually retrieved.
954 if ((valsize = sopt->sopt_valsize) < minlen)
957 sopt->sopt_valsize = valsize = len;
959 if (sopt->sopt_p != 0)
960 return (copyin(sopt->sopt_val, buf, valsize));
962 bcopy(sopt->sopt_val, buf, valsize);
969 struct sockopt *sopt;
977 if (sopt->sopt_level != SOL_SOCKET) {
978 if (so->so_proto && so->so_proto->pr_ctloutput)
979 return ((*so->so_proto->pr_ctloutput)
983 switch (sopt->sopt_name) {
985 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
989 so->so_linger = l.l_linger;
991 so->so_options |= SO_LINGER;
993 so->so_options &= ~SO_LINGER;
1005 error = sooptcopyin(sopt, &optval, sizeof optval,
1010 so->so_options |= sopt->sopt_name;
1012 so->so_options &= ~sopt->sopt_name;
1019 error = sooptcopyin(sopt, &optval, sizeof optval,
1025 * Values < 1 make no sense for any of these
1026 * options, so disallow them.
1033 switch (sopt->sopt_name) {
1036 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1037 &so->so_snd : &so->so_rcv,
1038 (u_long) optval) == 0) {
1045 * Make sure the low-water is never greater than
1049 so->so_snd.sb_lowat =
1050 (optval > so->so_snd.sb_hiwat) ?
1051 so->so_snd.sb_hiwat : optval;
1054 so->so_rcv.sb_lowat =
1055 (optval > so->so_rcv.sb_hiwat) ?
1056 so->so_rcv.sb_hiwat : optval;
1063 error = sooptcopyin(sopt, &tv, sizeof tv,
1068 /* assert(hz > 0); */
1069 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1070 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1074 /* assert(tick > 0); */
1075 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1076 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1077 if (val > SHRT_MAX) {
1082 switch (sopt->sopt_name) {
1084 so->so_snd.sb_timeo = val;
1087 so->so_rcv.sb_timeo = val;
1093 error = ENOPROTOOPT;
1096 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1097 (void) ((*so->so_proto->pr_ctloutput)
1105 /* Helper routine for getsockopt */
1107 sooptcopyout(sopt, buf, len)
1108 struct sockopt *sopt;
1118 * Documented get behavior is that we always return a value,
1119 * possibly truncated to fit in the user's buffer.
1120 * Traditional behavior is that we always tell the user
1121 * precisely how much we copied, rather than something useful
1122 * like the total amount we had available for her.
1123 * Note that this interface is not idempotent; the entire answer must
1124 * generated ahead of time.
1126 valsize = min(len, sopt->sopt_valsize);
1127 sopt->sopt_valsize = valsize;
1128 if (sopt->sopt_val != 0) {
1129 if (sopt->sopt_p != 0)
1130 error = copyout(buf, sopt->sopt_val, valsize);
1132 bcopy(buf, sopt->sopt_val, valsize);
1140 struct sockopt *sopt;
1147 if (sopt->sopt_level != SOL_SOCKET) {
1148 if (so->so_proto && so->so_proto->pr_ctloutput) {
1149 return ((*so->so_proto->pr_ctloutput)
1152 return (ENOPROTOOPT);
1154 switch (sopt->sopt_name) {
1156 l.l_onoff = so->so_options & SO_LINGER;
1157 l.l_linger = so->so_linger;
1158 error = sooptcopyout(sopt, &l, sizeof l);
1161 case SO_USELOOPBACK:
1170 optval = so->so_options & sopt->sopt_name;
1172 error = sooptcopyout(sopt, &optval, sizeof optval);
1176 optval = so->so_type;
1180 optval = so->so_error;
1185 optval = so->so_snd.sb_hiwat;
1189 optval = so->so_rcv.sb_hiwat;
1193 optval = so->so_snd.sb_lowat;
1197 optval = so->so_rcv.sb_lowat;
1202 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1203 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1205 tv.tv_sec = optval / hz;
1206 tv.tv_usec = (optval % hz) * tick;
1207 error = sooptcopyout(sopt, &tv, sizeof tv);
1211 error = ENOPROTOOPT;
1220 register struct socket *so;
1222 if (so->so_sigio != NULL)
1223 pgsigio(so->so_sigio, SIGURG, 0);
1224 selwakeup(&so->so_rcv.sb_sel);
1228 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p)
1233 if (events & (POLLIN | POLLRDNORM))
1235 revents |= events & (POLLIN | POLLRDNORM);
1237 if (events & (POLLOUT | POLLWRNORM))
1238 if (sowriteable(so))
1239 revents |= events & (POLLOUT | POLLWRNORM);
1241 if (events & (POLLPRI | POLLRDBAND))
1242 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1243 revents |= events & (POLLPRI | POLLRDBAND);
1246 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1247 selrecord(p, &so->so_rcv.sb_sel);
1248 so->so_rcv.sb_flags |= SB_SEL;
1251 if (events & (POLLOUT | POLLWRNORM)) {
1252 selrecord(p, &so->so_snd.sb_sel);
1253 so->so_snd.sb_flags |= SB_SEL;