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/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);
128 so->so_cred = p->p_ucred;
131 error = (*prp->pr_usrreqs->pru_attach)(so, proto, p);
133 so->so_state |= SS_NOFDREF;
144 struct sockaddr *nam;
150 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
160 so->so_gencnt = ++so_gencnt;
161 if (so->so_rcv.sb_hiwat)
162 (void)chgsbsize(so->so_cred->cr_uid,
163 -(rlim_t)so->so_rcv.sb_hiwat);
164 if (so->so_snd.sb_hiwat)
165 (void)chgsbsize(so->so_cred->cr_uid,
166 -(rlim_t)so->so_snd.sb_hiwat);
168 zfreei(so->so_zone, so);
172 solisten(so, backlog, p)
173 register struct socket *so;
180 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p);
185 if (TAILQ_EMPTY(&so->so_comp))
186 so->so_options |= SO_ACCEPTCONN;
187 if (backlog < 0 || backlog > somaxconn)
189 so->so_qlimit = backlog;
196 register struct socket *so;
198 struct socket *head = so->so_head;
200 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
203 if (so->so_state & SS_INCOMP) {
204 TAILQ_REMOVE(&head->so_incomp, so, so_list);
206 } else if (so->so_state & SS_COMP) {
208 * We must not decommission a socket that's
209 * on the accept(2) queue. If we do, then
210 * accept(2) may hang after select(2) indicated
211 * that the listening socket was ready.
215 panic("sofree: not queued");
218 so->so_state &= ~SS_INCOMP;
221 sbrelease(&so->so_snd, so);
227 * Close a socket on last file table reference removal.
228 * Initiate disconnect if connected.
229 * Free socket when disconnect complete.
233 register struct socket *so;
235 int s = splnet(); /* conservative */
238 funsetown(so->so_sigio);
239 if (so->so_options & SO_ACCEPTCONN) {
240 struct socket *sp, *sonext;
242 sp = TAILQ_FIRST(&so->so_incomp);
243 for (; sp != NULL; sp = sonext) {
244 sonext = TAILQ_NEXT(sp, so_list);
247 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
248 sonext = TAILQ_NEXT(sp, so_list);
249 /* Dequeue from so_comp since sofree() won't do it */
250 TAILQ_REMOVE(&so->so_comp, sp, so_list);
252 sp->so_state &= ~SS_COMP;
259 if (so->so_state & SS_ISCONNECTED) {
260 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
261 error = sodisconnect(so);
265 if (so->so_options & SO_LINGER) {
266 if ((so->so_state & SS_ISDISCONNECTING) &&
267 (so->so_state & SS_NBIO))
269 while (so->so_state & SS_ISCONNECTED) {
270 error = tsleep((caddr_t)&so->so_timeo,
271 PSOCK | PCATCH, "soclos", so->so_linger * hz);
279 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
284 if (so->so_state & SS_NOFDREF)
285 panic("soclose: NOFDREF");
286 so->so_state |= SS_NOFDREF;
293 * Must be called at splnet...
300 return (*so->so_proto->pr_usrreqs->pru_abort)(so);
305 register struct socket *so;
306 struct sockaddr **nam;
311 if ((so->so_state & SS_NOFDREF) == 0)
312 panic("soaccept: !NOFDREF");
313 so->so_state &= ~SS_NOFDREF;
314 if ((so->so_state & SS_ISDISCONNECTED) == 0)
315 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
326 soconnect(so, nam, p)
327 register struct socket *so;
328 struct sockaddr *nam;
334 if (so->so_options & SO_ACCEPTCONN)
338 * If protocol is connection-based, can only connect once.
339 * Otherwise, if connected, try to disconnect first.
340 * This allows user to disconnect by connecting to, e.g.,
343 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
344 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
345 (error = sodisconnect(so))))
348 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p);
355 register struct socket *so1;
361 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
368 register struct socket *so;
373 if ((so->so_state & SS_ISCONNECTED) == 0) {
377 if (so->so_state & SS_ISDISCONNECTING) {
381 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
387 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
390 * If send must go all at once and message is larger than
391 * send buffering, then hard error.
392 * Lock against other senders.
393 * If must go all at once and not enough room now, then
394 * inform user that this would block and do nothing.
395 * Otherwise, if nonblocking, send as much as possible.
396 * The data to be sent is described by "uio" if nonzero,
397 * otherwise by the mbuf chain "top" (which must be null
398 * if uio is not). Data provided in mbuf chain must be small
399 * enough to send all at once.
401 * Returns nonzero on error, timeout or signal; callers
402 * must check for short counts if EINTR/ERESTART are returned.
403 * Data and control buffers are freed on return.
406 sosend(so, addr, uio, top, control, flags, p)
407 register struct socket *so;
408 struct sockaddr *addr;
411 struct mbuf *control;
416 register struct mbuf *m;
417 register long space, len, resid;
418 int clen = 0, error, s, dontroute, mlen;
419 int atomic = sosendallatonce(so) || top;
422 resid = uio->uio_resid;
424 resid = top->m_pkthdr.len;
426 * In theory resid should be unsigned.
427 * However, space must be signed, as it might be less than 0
428 * if we over-committed, and we must use a signed comparison
429 * of space and resid. On the other hand, a negative resid
430 * causes us to loop sending 0-length segments to the protocol.
432 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
433 * type sockets since that's an error.
435 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
441 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
442 (so->so_proto->pr_flags & PR_ATOMIC);
444 p->p_stats->p_ru.ru_msgsnd++;
446 clen = control->m_len;
447 #define snderr(errno) { error = errno; splx(s); goto release; }
450 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
455 if (so->so_state & SS_CANTSENDMORE)
458 error = so->so_error;
463 if ((so->so_state & SS_ISCONNECTED) == 0) {
465 * `sendto' and `sendmsg' is allowed on a connection-
466 * based socket if it supports implied connect.
467 * Return ENOTCONN if not connected and no address is
470 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
471 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
472 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
473 !(resid == 0 && clen != 0))
475 } else if (addr == 0)
476 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
477 ENOTCONN : EDESTADDRREQ);
479 space = sbspace(&so->so_snd);
482 if ((atomic && resid > so->so_snd.sb_hiwat) ||
483 clen > so->so_snd.sb_hiwat)
485 if (space < resid + clen && uio &&
486 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
487 if (so->so_state & SS_NBIO)
489 sbunlock(&so->so_snd);
490 error = sbwait(&so->so_snd);
502 * Data is prepackaged in "top".
506 top->m_flags |= M_EOR;
509 MGETHDR(m, M_WAIT, MT_DATA);
516 m->m_pkthdr.rcvif = (struct ifnet *)0;
518 MGET(m, M_WAIT, MT_DATA);
525 if (resid >= MINCLSIZE) {
527 if ((m->m_flags & M_EXT) == 0)
530 len = min(min(mlen, resid), space);
533 len = min(min(mlen, resid), space);
535 * For datagram protocols, leave room
536 * for protocol headers in first mbuf.
538 if (atomic && top == 0 && len < mlen)
542 error = uiomove(mtod(m, caddr_t), (int)len, uio);
543 resid = uio->uio_resid;
546 top->m_pkthdr.len += len;
552 top->m_flags |= M_EOR;
555 } while (space > 0 && atomic);
557 so->so_options |= SO_DONTROUTE;
558 s = splnet(); /* XXX */
560 * XXX all the SS_CANTSENDMORE checks previously
561 * done could be out of date. We could have recieved
562 * a reset packet in an interrupt or maybe we slept
563 * while doing page faults in uiomove() etc. We could
564 * probably recheck again inside the splnet() protection
565 * here, but there are probably other places that this
566 * also happens. We must rethink this.
568 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
569 (flags & MSG_OOB) ? PRUS_OOB :
571 * If the user set MSG_EOF, the protocol
572 * understands this flag and nothing left to
573 * send then use PRU_SEND_EOF instead of PRU_SEND.
575 ((flags & MSG_EOF) &&
576 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
579 /* If there is more to send set PRUS_MORETOCOME */
580 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
581 top, addr, control, p);
584 so->so_options &= ~SO_DONTROUTE;
591 } while (resid && space > 0);
595 sbunlock(&so->so_snd);
605 * Implement receive operations on a socket.
606 * We depend on the way that records are added to the sockbuf
607 * by sbappend*. In particular, each record (mbufs linked through m_next)
608 * must begin with an address if the protocol so specifies,
609 * followed by an optional mbuf or mbufs containing ancillary data,
610 * and then zero or more mbufs of data.
611 * In order to avoid blocking network interrupts for the entire time here,
612 * we splx() while doing the actual copy to user space.
613 * Although the sockbuf is locked, new data may still be appended,
614 * and thus we must maintain consistency of the sockbuf during that time.
616 * The caller may receive the data as a single mbuf chain by supplying
617 * an mbuf **mp0 for use in returning the chain. The uio is then used
618 * only for the count in uio_resid.
621 soreceive(so, psa, uio, mp0, controlp, flagsp)
622 register struct socket *so;
623 struct sockaddr **psa;
626 struct mbuf **controlp;
629 register struct mbuf *m, **mp;
630 register int flags, len, error, s, offset;
631 struct protosw *pr = so->so_proto;
632 struct mbuf *nextrecord;
634 int orig_resid = uio->uio_resid;
642 flags = *flagsp &~ MSG_EOR;
645 if (flags & MSG_OOB) {
646 m = m_get(M_WAIT, MT_DATA);
649 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
653 error = uiomove(mtod(m, caddr_t),
654 (int) min(uio->uio_resid, m->m_len), uio);
656 } while (uio->uio_resid && error == 0 && m);
663 *mp = (struct mbuf *)0;
664 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
665 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
668 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
673 m = so->so_rcv.sb_mb;
675 * If we have less data than requested, block awaiting more
676 * (subject to any timeout) if:
677 * 1. the current count is less than the low water mark, or
678 * 2. MSG_WAITALL is set, and it is possible to do the entire
679 * receive operation at once if we block (resid <= hiwat).
680 * 3. MSG_DONTWAIT is not set
681 * If MSG_WAITALL is set but resid is larger than the receive buffer,
682 * we have to do the receive in sections, and thus risk returning
683 * a short count if a timeout or signal occurs after we start.
685 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
686 so->so_rcv.sb_cc < uio->uio_resid) &&
687 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
688 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
689 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
690 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
694 error = so->so_error;
695 if ((flags & MSG_PEEK) == 0)
699 if (so->so_state & SS_CANTRCVMORE) {
705 for (; m; m = m->m_next)
706 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
707 m = so->so_rcv.sb_mb;
710 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
711 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
715 if (uio->uio_resid == 0)
717 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
721 sbunlock(&so->so_rcv);
722 error = sbwait(&so->so_rcv);
730 uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
731 nextrecord = m->m_nextpkt;
732 if (pr->pr_flags & PR_ADDR) {
733 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
736 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
738 if (flags & MSG_PEEK) {
741 sbfree(&so->so_rcv, m);
742 MFREE(m, so->so_rcv.sb_mb);
743 m = so->so_rcv.sb_mb;
746 while (m && m->m_type == MT_CONTROL && error == 0) {
747 if (flags & MSG_PEEK) {
749 *controlp = m_copy(m, 0, m->m_len);
752 sbfree(&so->so_rcv, m);
754 if (pr->pr_domain->dom_externalize &&
755 mtod(m, struct cmsghdr *)->cmsg_type ==
757 error = (*pr->pr_domain->dom_externalize)(m);
759 so->so_rcv.sb_mb = m->m_next;
761 m = so->so_rcv.sb_mb;
763 MFREE(m, so->so_rcv.sb_mb);
764 m = so->so_rcv.sb_mb;
769 controlp = &(*controlp)->m_next;
773 if ((flags & MSG_PEEK) == 0)
774 m->m_nextpkt = nextrecord;
776 if (type == MT_OOBDATA)
781 while (m && uio->uio_resid > 0 && error == 0) {
782 if (m->m_type == MT_OOBDATA) {
783 if (type != MT_OOBDATA)
785 } else if (type == MT_OOBDATA)
788 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
790 so->so_state &= ~SS_RCVATMARK;
791 len = uio->uio_resid;
792 if (so->so_oobmark && len > so->so_oobmark - offset)
793 len = so->so_oobmark - offset;
794 if (len > m->m_len - moff)
795 len = m->m_len - moff;
797 * If mp is set, just pass back the mbufs.
798 * Otherwise copy them out via the uio, then free.
799 * Sockbuf must be consistent here (points to current mbuf,
800 * it points to next record) when we drop priority;
801 * we must note any additions to the sockbuf when we
802 * block interrupts again.
806 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
811 uio->uio_resid -= len;
812 if (len == m->m_len - moff) {
813 if (m->m_flags & M_EOR)
815 if (flags & MSG_PEEK) {
819 nextrecord = m->m_nextpkt;
820 sbfree(&so->so_rcv, m);
824 so->so_rcv.sb_mb = m = m->m_next;
825 *mp = (struct mbuf *)0;
827 MFREE(m, so->so_rcv.sb_mb);
828 m = so->so_rcv.sb_mb;
831 m->m_nextpkt = nextrecord;
834 if (flags & MSG_PEEK)
838 *mp = m_copym(m, 0, len, M_WAIT);
841 so->so_rcv.sb_cc -= len;
844 if (so->so_oobmark) {
845 if ((flags & MSG_PEEK) == 0) {
846 so->so_oobmark -= len;
847 if (so->so_oobmark == 0) {
848 so->so_state |= SS_RCVATMARK;
853 if (offset == so->so_oobmark)
860 * If the MSG_WAITALL flag is set (for non-atomic socket),
861 * we must not quit until "uio->uio_resid == 0" or an error
862 * termination. If a signal/timeout occurs, return
863 * with a short count but without error.
864 * Keep sockbuf locked against other readers.
866 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
867 !sosendallatonce(so) && !nextrecord) {
868 if (so->so_error || so->so_state & SS_CANTRCVMORE)
870 error = sbwait(&so->so_rcv);
872 sbunlock(&so->so_rcv);
876 m = so->so_rcv.sb_mb;
878 nextrecord = m->m_nextpkt;
882 if (m && pr->pr_flags & PR_ATOMIC) {
884 if ((flags & MSG_PEEK) == 0)
885 (void) sbdroprecord(&so->so_rcv);
887 if ((flags & MSG_PEEK) == 0) {
889 so->so_rcv.sb_mb = nextrecord;
890 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
891 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
893 if (orig_resid == uio->uio_resid && orig_resid &&
894 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
895 sbunlock(&so->so_rcv);
903 sbunlock(&so->so_rcv);
910 register struct socket *so;
913 register struct protosw *pr = so->so_proto;
919 return ((*pr->pr_usrreqs->pru_shutdown)(so));
925 register struct socket *so;
927 register struct sockbuf *sb = &so->so_rcv;
928 register struct protosw *pr = so->so_proto;
932 sb->sb_flags |= SB_NOINTR;
933 (void) sblock(sb, M_WAITOK);
938 bzero((caddr_t)sb, sizeof (*sb));
940 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
941 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
946 * Perhaps this routine, and sooptcopyout(), below, ought to come in
947 * an additional variant to handle the case where the option value needs
948 * to be some kind of integer, but not a specific size.
949 * In addition to their use here, these functions are also called by the
950 * protocol-level pr_ctloutput() routines.
953 sooptcopyin(sopt, buf, len, minlen)
954 struct sockopt *sopt;
962 * If the user gives us more than we wanted, we ignore it,
963 * but if we don't get the minimum length the caller
964 * wants, we return EINVAL. On success, sopt->sopt_valsize
965 * is set to however much we actually retrieved.
967 if ((valsize = sopt->sopt_valsize) < minlen)
970 sopt->sopt_valsize = valsize = len;
972 if (sopt->sopt_p != 0)
973 return (copyin(sopt->sopt_val, buf, valsize));
975 bcopy(sopt->sopt_val, buf, valsize);
982 struct sockopt *sopt;
990 if (sopt->sopt_level != SOL_SOCKET) {
991 if (so->so_proto && so->so_proto->pr_ctloutput)
992 return ((*so->so_proto->pr_ctloutput)
996 switch (sopt->sopt_name) {
998 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1002 so->so_linger = l.l_linger;
1004 so->so_options |= SO_LINGER;
1006 so->so_options &= ~SO_LINGER;
1012 case SO_USELOOPBACK:
1018 error = sooptcopyin(sopt, &optval, sizeof optval,
1023 so->so_options |= sopt->sopt_name;
1025 so->so_options &= ~sopt->sopt_name;
1032 error = sooptcopyin(sopt, &optval, sizeof optval,
1038 * Values < 1 make no sense for any of these
1039 * options, so disallow them.
1046 switch (sopt->sopt_name) {
1049 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1050 &so->so_snd : &so->so_rcv, (u_long)optval,
1051 so, curproc) == 0) {
1058 * Make sure the low-water is never greater than
1062 so->so_snd.sb_lowat =
1063 (optval > so->so_snd.sb_hiwat) ?
1064 so->so_snd.sb_hiwat : optval;
1067 so->so_rcv.sb_lowat =
1068 (optval > so->so_rcv.sb_hiwat) ?
1069 so->so_rcv.sb_hiwat : optval;
1076 error = sooptcopyin(sopt, &tv, sizeof tv,
1081 /* assert(hz > 0); */
1082 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1083 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1087 /* assert(tick > 0); */
1088 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1089 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1090 if (val > SHRT_MAX) {
1095 switch (sopt->sopt_name) {
1097 so->so_snd.sb_timeo = val;
1100 so->so_rcv.sb_timeo = val;
1106 error = ENOPROTOOPT;
1109 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1110 (void) ((*so->so_proto->pr_ctloutput)
1118 /* Helper routine for getsockopt */
1120 sooptcopyout(sopt, buf, len)
1121 struct sockopt *sopt;
1131 * Documented get behavior is that we always return a value,
1132 * possibly truncated to fit in the user's buffer.
1133 * Traditional behavior is that we always tell the user
1134 * precisely how much we copied, rather than something useful
1135 * like the total amount we had available for her.
1136 * Note that this interface is not idempotent; the entire answer must
1137 * generated ahead of time.
1139 valsize = min(len, sopt->sopt_valsize);
1140 sopt->sopt_valsize = valsize;
1141 if (sopt->sopt_val != 0) {
1142 if (sopt->sopt_p != 0)
1143 error = copyout(buf, sopt->sopt_val, valsize);
1145 bcopy(buf, sopt->sopt_val, valsize);
1153 struct sockopt *sopt;
1160 if (sopt->sopt_level != SOL_SOCKET) {
1161 if (so->so_proto && so->so_proto->pr_ctloutput) {
1162 return ((*so->so_proto->pr_ctloutput)
1165 return (ENOPROTOOPT);
1167 switch (sopt->sopt_name) {
1169 l.l_onoff = so->so_options & SO_LINGER;
1170 l.l_linger = so->so_linger;
1171 error = sooptcopyout(sopt, &l, sizeof l);
1174 case SO_USELOOPBACK:
1183 optval = so->so_options & sopt->sopt_name;
1185 error = sooptcopyout(sopt, &optval, sizeof optval);
1189 optval = so->so_type;
1193 optval = so->so_error;
1198 optval = so->so_snd.sb_hiwat;
1202 optval = so->so_rcv.sb_hiwat;
1206 optval = so->so_snd.sb_lowat;
1210 optval = so->so_rcv.sb_lowat;
1215 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1216 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1218 tv.tv_sec = optval / hz;
1219 tv.tv_usec = (optval % hz) * tick;
1220 error = sooptcopyout(sopt, &tv, sizeof tv);
1224 error = ENOPROTOOPT;
1231 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1233 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1235 struct mbuf *m, *m_prev;
1236 int sopt_size = sopt->sopt_valsize;
1238 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1241 if (sopt_size > MLEN) {
1242 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1243 if ((m->m_flags & M_EXT) == 0) {
1247 m->m_len = min(MCLBYTES, sopt_size);
1249 m->m_len = min(MLEN, sopt_size);
1251 sopt_size -= m->m_len;
1256 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1261 if (sopt_size > MLEN) {
1262 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1263 if ((m->m_flags & M_EXT) == 0) {
1267 m->m_len = min(MCLBYTES, sopt_size);
1269 m->m_len = min(MLEN, sopt_size);
1271 sopt_size -= m->m_len;
1278 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1280 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1282 struct mbuf *m0 = m;
1284 if (sopt->sopt_val == NULL)
1286 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1287 if (sopt->sopt_p != NULL) {
1290 error = copyin(sopt->sopt_val, mtod(m, char *),
1297 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1298 sopt->sopt_valsize -= m->m_len;
1299 (caddr_t)sopt->sopt_val += m->m_len;
1302 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1303 panic("ip6_sooptmcopyin");
1307 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1309 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1311 struct mbuf *m0 = m;
1314 if (sopt->sopt_val == NULL)
1316 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1317 if (sopt->sopt_p != NULL) {
1320 error = copyout(mtod(m, char *), sopt->sopt_val,
1327 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1328 sopt->sopt_valsize -= m->m_len;
1329 (caddr_t)sopt->sopt_val += m->m_len;
1330 valsize += m->m_len;
1334 /* enough soopt buffer should be given from user-land */
1338 sopt->sopt_valsize = valsize;
1344 register struct socket *so;
1346 if (so->so_sigio != NULL)
1347 pgsigio(so->so_sigio, SIGURG, 0);
1348 selwakeup(&so->so_rcv.sb_sel);
1352 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p)
1357 if (events & (POLLIN | POLLRDNORM))
1359 revents |= events & (POLLIN | POLLRDNORM);
1361 if (events & (POLLOUT | POLLWRNORM))
1362 if (sowriteable(so))
1363 revents |= events & (POLLOUT | POLLWRNORM);
1365 if (events & (POLLPRI | POLLRDBAND))
1366 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1367 revents |= events & (POLLPRI | POLLRDBAND);
1370 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1371 selrecord(p, &so->so_rcv.sb_sel);
1372 so->so_rcv.sb_flags |= SB_SEL;
1375 if (events & (POLLOUT | POLLWRNORM)) {
1376 selrecord(p, &so->so_snd.sb_sel);
1377 so->so_snd.sb_flags |= SB_SEL;