1 //==========================================================================
3 // src/sys/kern/uipc_socket.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
55 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.16 2001/06/14 20:46:06 ume Exp $
58 #include <sys/param.h>
59 #include <sys/malloc.h>
61 #include <sys/domain.h>
62 #include <sys/malloc.h>
63 #include <sys/protosw.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
67 #include <cyg/io/file.h>
70 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
74 static void filt_sordetach(struct knote *kn);
75 static int filt_soread(struct knote *kn, long hint);
76 static void filt_sowdetach(struct knote *kn);
77 static int filt_sowrite(struct knote *kn, long hint);
78 static int filt_solisten(struct knote *kn, long hint);
80 static struct filterops solisten_filtops =
81 { 1, NULL, filt_sordetach, filt_solisten };
82 static struct filterops soread_filtops =
83 { 1, NULL, filt_sordetach, filt_soread };
84 static struct filterops sowrite_filtops =
85 { 1, NULL, filt_sowdetach, filt_sowrite };
88 struct vm_zone *socket_zone;
89 so_gen_t so_gencnt; /* generation count for sockets */
91 static int somaxconn = SOMAXCONN;
94 * Socket operation routines.
95 * These routines are called by the routines in
96 * sys_socket.c or from a system process, and
97 * implement the semantics of socket operations by
98 * switching out to the protocol specific routines.
102 * Get a socket structure from our zone, and initialize it.
103 * 'waitok' has been implemented for eCos, with [currently] some
104 * rather fixed strategy - it will retry some number of times (10)
105 * after at most 2 minutes. This seems sufficient for sockets which
106 * are tied up in the TCP close process.
111 struct socket *so = NULL;
112 int maxtries = waitok ? 10 : 1;
114 while (maxtries-- > 0) {
115 so = zalloci(socket_zone);
117 /* XXX race condition for reentrant kernel */
118 bzero(so, sizeof *so);
119 so->so_gencnt = ++so_gencnt;
120 so->so_zone = socket_zone;
121 TAILQ_INIT(&so->so_aiojobq);
125 diag_printf("DEBUG: Out of sockets - waiting\n");
126 tsleep(socket_zone, PVM|PCATCH, "soalloc", 120*100);
127 diag_printf("DEBUG: ... retry sockets\n");
134 socreate(dom, aso, type, proto, p)
141 register struct protosw *prp;
142 register struct socket *so;
146 prp = pffindproto(dom, proto, type);
148 prp = pffindtype(dom, type);
150 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
151 return (EPROTONOSUPPORT);
153 if (prp->pr_type != type)
155 so = soalloc(p != 0);
160 TAILQ_INIT(&so->so_incomp);
161 TAILQ_INIT(&so->so_comp);
164 error = (*prp->pr_usrreqs->pru_attach)(so, proto, p);
166 so->so_state |= SS_NOFDREF;
177 struct sockaddr *nam;
183 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
195 so->so_gencnt = ++so_gencnt;
197 if (so->so_accf != NULL) {
198 if (so->so_accf->so_accept_filter != NULL &&
199 so->so_accf->so_accept_filter->accf_destroy != NULL) {
200 so->so_accf->so_accept_filter->accf_destroy(so);
202 if (so->so_accf->so_accept_filter_str != NULL)
203 FREE(so->so_accf->so_accept_filter_str, M_ACCF);
204 FREE(so->so_accf, M_ACCF);
213 solisten(so, backlog, p)
214 register struct socket *so;
221 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p);
226 if (TAILQ_EMPTY(&so->so_comp))
227 so->so_options |= SO_ACCEPTCONN;
228 if (backlog < 0 || backlog > somaxconn)
230 so->so_qlimit = backlog;
237 register struct socket *so;
239 struct socket *head = so->so_head;
241 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
244 if (so->so_state & SS_INCOMP) {
245 TAILQ_REMOVE(&head->so_incomp, so, so_list);
247 } else if (so->so_state & SS_COMP) {
249 * We must not decommission a socket that's
250 * on the accept(2) queue. If we do, then
251 * accept(2) may hang after select(2) indicated
252 * that the listening socket was ready.
256 panic("sofree: not queued");
259 so->so_state &= ~SS_INCOMP;
262 sbrelease(&so->so_snd, so);
268 * Close a socket on last file table reference removal.
269 * Initiate disconnect if connected.
270 * Free socket when disconnect complete.
274 register struct socket *so;
276 int s = splnet(); /* conservative */
279 if (so->so_options & SO_ACCEPTCONN) {
280 struct socket *sp, *sonext;
282 sp = TAILQ_FIRST(&so->so_incomp);
283 for (; sp != NULL; sp = sonext) {
284 sonext = TAILQ_NEXT(sp, so_list);
287 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
288 sonext = TAILQ_NEXT(sp, so_list);
289 /* Dequeue from so_comp since sofree() won't do it */
290 TAILQ_REMOVE(&so->so_comp, sp, so_list);
292 sp->so_state &= ~SS_COMP;
299 if (so->so_state & SS_ISCONNECTED) {
300 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
301 error = sodisconnect(so);
305 if (so->so_options & SO_LINGER) {
306 if ((so->so_state & SS_ISDISCONNECTING) &&
307 (so->so_state & SS_NBIO))
309 while (so->so_state & SS_ISCONNECTED) {
310 error = tsleep((caddr_t)&so->so_timeo,
311 PSOCK | PCATCH, "soclos", so->so_linger * hz);
319 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
324 if (so->so_state & SS_NOFDREF)
325 panic("soclose: NOFDREF");
326 so->so_state |= SS_NOFDREF;
333 * Must be called at splnet...
341 error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
351 register struct socket *so;
352 struct sockaddr **nam;
357 if ((so->so_state & SS_NOFDREF) == 0)
358 panic("soaccept: !NOFDREF");
359 so->so_state &= ~SS_NOFDREF;
360 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
366 soconnect(so, nam, p)
367 register struct socket *so;
368 struct sockaddr *nam;
374 if (so->so_options & SO_ACCEPTCONN)
378 * If protocol is connection-based, can only connect once.
379 * Otherwise, if connected, try to disconnect first.
380 * This allows user to disconnect by connecting to, e.g.,
383 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
384 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
385 (error = sodisconnect(so))))
388 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p);
395 register struct socket *so1;
401 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
408 register struct socket *so;
413 if ((so->so_state & SS_ISCONNECTED) == 0) {
417 if (so->so_state & SS_ISDISCONNECTING) {
421 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
427 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
430 * If send must go all at once and message is larger than
431 * send buffering, then hard error.
432 * Lock against other senders.
433 * If must go all at once and not enough room now, then
434 * inform user that this would block and do nothing.
435 * Otherwise, if nonblocking, send as much as possible.
436 * The data to be sent is described by "uio" if nonzero,
437 * otherwise by the mbuf chain "top" (which must be null
438 * if uio is not). Data provided in mbuf chain must be small
439 * enough to send all at once.
441 * Returns nonzero on error, timeout or signal; callers
442 * must check for short counts if EINTR/ERESTART are returned.
443 * Data and control buffers are freed on return.
446 sosend(so, addr, uio, top, control, flags, p)
447 register struct socket *so;
448 struct sockaddr *addr;
451 struct mbuf *control;
456 register struct mbuf *m;
457 register long space, len, resid;
458 int clen = 0, error, s, dontroute, mlen;
459 int atomic = sosendallatonce(so) || top;
462 resid = uio->uio_resid;
464 resid = top->m_pkthdr.len;
466 * In theory resid should be unsigned.
467 * However, space must be signed, as it might be less than 0
468 * if we over-committed, and we must use a signed comparison
469 * of space and resid. On the other hand, a negative resid
470 * causes us to loop sending 0-length segments to the protocol.
472 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
473 * type sockets since that's an error.
475 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
481 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
482 (so->so_proto->pr_flags & PR_ATOMIC);
484 clen = control->m_len;
485 #define snderr(errno) { error = errno; splx(s); goto release; }
488 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
493 if (so->so_state & SS_CANTSENDMORE)
496 error = so->so_error;
501 if ((so->so_state & SS_ISCONNECTED) == 0) {
503 * `sendto' and `sendmsg' is allowed on a connection-
504 * based socket if it supports implied connect.
505 * Return ENOTCONN if not connected and no address is
508 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
509 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
510 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
511 !(resid == 0 && clen != 0))
513 } else if (addr == 0)
514 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
515 ENOTCONN : EDESTADDRREQ);
517 space = sbspace(&so->so_snd);
520 if ((atomic && resid > so->so_snd.sb_hiwat) ||
521 clen > so->so_snd.sb_hiwat)
523 if (space < resid + clen && uio &&
524 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
525 if (so->so_state & SS_NBIO)
527 sbunlock(&so->so_snd);
528 error = sbwait(&so->so_snd);
540 * Data is prepackaged in "top".
544 top->m_flags |= M_EOR;
547 MGETHDR(m, M_WAIT, MT_DATA);
554 m->m_pkthdr.rcvif = (struct ifnet *)0;
556 MGET(m, M_WAIT, MT_DATA);
563 if (resid >= MINCLSIZE) {
565 if ((m->m_flags & M_EXT) == 0)
568 len = min(min(mlen, resid), space);
571 len = min(min(mlen, resid), space);
573 * For datagram protocols, leave room
574 * for protocol headers in first mbuf.
576 if (atomic && top == 0 && len < mlen)
580 error = uiomove(mtod(m, caddr_t), (int)len, uio);
581 resid = uio->uio_resid;
584 top->m_pkthdr.len += len;
590 top->m_flags |= M_EOR;
593 } while (space > 0 && atomic);
595 so->so_options |= SO_DONTROUTE;
596 s = splnet(); /* XXX */
598 * XXX all the SS_CANTSENDMORE checks previously
599 * done could be out of date. We could have recieved
600 * a reset packet in an interrupt or maybe we slept
601 * while doing page faults in uiomove() etc. We could
602 * probably recheck again inside the splnet() protection
603 * here, but there are probably other places that this
604 * also happens. We must rethink this.
606 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
607 (flags & MSG_OOB) ? PRUS_OOB :
609 * If the user set MSG_EOF, the protocol
610 * understands this flag and nothing left to
611 * send then use PRU_SEND_EOF instead of PRU_SEND.
613 ((flags & MSG_EOF) &&
614 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
617 /* If there is more to send set PRUS_MORETOCOME */
618 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
619 top, addr, control, p);
622 so->so_options &= ~SO_DONTROUTE;
629 } while (resid && space > 0);
633 sbunlock(&so->so_snd);
643 * Implement receive operations on a socket.
644 * We depend on the way that records are added to the sockbuf
645 * by sbappend*. In particular, each record (mbufs linked through m_next)
646 * must begin with an address if the protocol so specifies,
647 * followed by an optional mbuf or mbufs containing ancillary data,
648 * and then zero or more mbufs of data.
649 * In order to avoid blocking network interrupts for the entire time here,
650 * we splx() while doing the actual copy to user space.
651 * Although the sockbuf is locked, new data may still be appended,
652 * and thus we must maintain consistency of the sockbuf during that time.
654 * The caller may receive the data as a single mbuf chain by supplying
655 * an mbuf **mp0 for use in returning the chain. The uio is then used
656 * only for the count in uio_resid.
659 soreceive(so, psa, uio, mp0, controlp, flagsp)
660 register struct socket *so;
661 struct sockaddr **psa;
664 struct mbuf **controlp;
667 register struct mbuf *m, **mp;
668 register int flags, len, error, s, offset;
669 struct protosw *pr = so->so_proto;
670 struct mbuf *nextrecord;
672 int orig_resid = uio->uio_resid;
680 flags = *flagsp &~ MSG_EOR;
683 if (flags & MSG_OOB) {
684 m = m_get(M_WAIT, MT_DATA);
688 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
692 error = uiomove(mtod(m, caddr_t),
693 (int) min(uio->uio_resid, m->m_len), uio);
695 } while (uio->uio_resid && error == 0 && m);
702 *mp = (struct mbuf *)0;
703 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
704 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
707 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
712 m = so->so_rcv.sb_mb;
714 * If we have less data than requested, block awaiting more
715 * (subject to any timeout) if:
716 * 1. the current count is less than the low water mark, or
717 * 2. MSG_WAITALL is set, and it is possible to do the entire
718 * receive operation at once if we block (resid <= hiwat).
719 * 3. MSG_DONTWAIT is not set
720 * If MSG_WAITALL is set but resid is larger than the receive buffer,
721 * we have to do the receive in sections, and thus risk returning
722 * a short count if a timeout or signal occurs after we start.
724 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
725 so->so_rcv.sb_cc < uio->uio_resid) &&
726 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
727 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
728 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
732 error = so->so_error;
733 if ((flags & MSG_PEEK) == 0)
737 if (so->so_state & SS_CANTRCVMORE) {
743 for (; m; m = m->m_next)
744 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
745 m = so->so_rcv.sb_mb;
748 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
749 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
753 if (uio->uio_resid == 0)
755 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
759 sbunlock(&so->so_rcv);
760 error = sbwait(&so->so_rcv);
767 nextrecord = m->m_nextpkt;
768 if (pr->pr_flags & PR_ADDR) {
771 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
773 if (flags & MSG_PEEK) {
776 sbfree(&so->so_rcv, m);
777 MFREE(m, so->so_rcv.sb_mb);
778 m = so->so_rcv.sb_mb;
781 while (m && m->m_type == MT_CONTROL && error == 0) {
782 if (flags & MSG_PEEK) {
784 *controlp = m_copy(m, 0, m->m_len);
787 sbfree(&so->so_rcv, m);
789 if (pr->pr_domain->dom_externalize &&
790 mtod(m, struct cmsghdr *)->cmsg_type ==
792 error = (*pr->pr_domain->dom_externalize)(m);
794 so->so_rcv.sb_mb = m->m_next;
796 m = so->so_rcv.sb_mb;
798 MFREE(m, so->so_rcv.sb_mb);
799 m = so->so_rcv.sb_mb;
804 controlp = &(*controlp)->m_next;
808 if ((flags & MSG_PEEK) == 0)
809 m->m_nextpkt = nextrecord;
811 if (type == MT_OOBDATA)
816 while (m && uio->uio_resid > 0 && error == 0) {
817 if (m->m_type == MT_OOBDATA) {
818 if (type != MT_OOBDATA)
820 } else if (type == MT_OOBDATA)
822 so->so_state &= ~SS_RCVATMARK;
823 len = uio->uio_resid;
824 if (so->so_oobmark && len > so->so_oobmark - offset)
825 len = so->so_oobmark - offset;
826 if (len > m->m_len - moff)
827 len = m->m_len - moff;
829 * If mp is set, just pass back the mbufs.
830 * Otherwise copy them out via the uio, then free.
831 * Sockbuf must be consistent here (points to current mbuf,
832 * it points to next record) when we drop priority;
833 * we must note any additions to the sockbuf when we
834 * block interrupts again.
838 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
843 uio->uio_resid -= len;
844 if (len == m->m_len - moff) {
845 if (m->m_flags & M_EOR)
847 if (flags & MSG_PEEK) {
851 nextrecord = m->m_nextpkt;
852 sbfree(&so->so_rcv, m);
856 so->so_rcv.sb_mb = m = m->m_next;
857 *mp = (struct mbuf *)0;
859 MFREE(m, so->so_rcv.sb_mb);
860 m = so->so_rcv.sb_mb;
863 m->m_nextpkt = nextrecord;
866 if (flags & MSG_PEEK)
870 *mp = m_copym(m, 0, len, M_WAIT);
873 so->so_rcv.sb_cc -= len;
876 if (so->so_oobmark) {
877 if ((flags & MSG_PEEK) == 0) {
878 so->so_oobmark -= len;
879 if (so->so_oobmark == 0) {
880 so->so_state |= SS_RCVATMARK;
885 if (offset == so->so_oobmark)
892 * If the MSG_WAITALL flag is set (for non-atomic socket),
893 * we must not quit until "uio->uio_resid == 0" or an error
894 * termination. If a signal/timeout occurs, return
895 * with a short count but without error.
896 * Keep sockbuf locked against other readers.
898 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
899 !sosendallatonce(so) && !nextrecord) {
900 if (so->so_error || so->so_state & SS_CANTRCVMORE)
902 error = sbwait(&so->so_rcv);
904 sbunlock(&so->so_rcv);
908 m = so->so_rcv.sb_mb;
910 nextrecord = m->m_nextpkt;
914 if (m && pr->pr_flags & PR_ATOMIC) {
916 if ((flags & MSG_PEEK) == 0)
917 (void) sbdroprecord(&so->so_rcv);
919 if ((flags & MSG_PEEK) == 0) {
921 so->so_rcv.sb_mb = nextrecord;
922 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
923 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
925 if (orig_resid == uio->uio_resid && orig_resid &&
926 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
927 sbunlock(&so->so_rcv);
935 sbunlock(&so->so_rcv);
942 register struct socket *so;
945 register struct protosw *pr = so->so_proto;
947 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
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);
981 do_setopt_accept_filter(so, sopt)
983 struct sockopt *sopt;
985 struct accept_filter_arg *afap = NULL;
986 struct accept_filter *afp;
987 struct so_accf *af = so->so_accf;
990 /* do not set/remove accept filters on non listen sockets */
991 if ((so->so_options & SO_ACCEPTCONN) == 0) {
996 /* removing the filter */
999 if (af->so_accept_filter != NULL &&
1000 af->so_accept_filter->accf_destroy != NULL) {
1001 af->so_accept_filter->accf_destroy(so);
1003 if (af->so_accept_filter_str != NULL) {
1004 FREE(af->so_accept_filter_str, M_ACCF);
1009 so->so_options &= ~SO_ACCEPTFILTER;
1012 /* adding a filter */
1013 /* must remove previous filter first */
1018 /* don't put large objects on the kernel stack */
1019 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1020 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1021 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1022 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1025 afp = accept_filt_get(afap->af_name);
1030 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK);
1031 bzero(af, sizeof(*af));
1032 if (afp->accf_create != NULL) {
1033 if (afap->af_name[0] != '\0') {
1034 int len = strlen(afap->af_name) + 1;
1036 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1037 strcpy(af->so_accept_filter_str, afap->af_name);
1039 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1040 if (af->so_accept_filter_arg == NULL) {
1041 FREE(af->so_accept_filter_str, M_ACCF);
1048 af->so_accept_filter = afp;
1050 so->so_options |= SO_ACCEPTFILTER;
1059 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1060 * an additional variant to handle the case where the option value needs
1061 * to be some kind of integer, but not a specific size.
1062 * In addition to their use here, these functions are also called by the
1063 * protocol-level pr_ctloutput() routines.
1066 sooptcopyin(sopt, buf, len, minlen)
1067 struct sockopt *sopt;
1075 * If the user gives us more than we wanted, we ignore it,
1076 * but if we don't get the minimum length the caller
1077 * wants, we return EINVAL. On success, sopt->sopt_valsize
1078 * is set to however much we actually retrieved.
1080 if ((valsize = sopt->sopt_valsize) < minlen)
1083 sopt->sopt_valsize = valsize = len;
1085 if (sopt->sopt_p != 0)
1086 return (copyin(sopt->sopt_val, buf, valsize));
1088 bcopy(sopt->sopt_val, buf, valsize);
1095 struct sockopt *sopt;
1103 if (sopt->sopt_level != SOL_SOCKET) {
1104 if (so->so_proto && so->so_proto->pr_ctloutput)
1105 return ((*so->so_proto->pr_ctloutput)
1107 error = ENOPROTOOPT;
1109 switch (sopt->sopt_name) {
1111 case SO_ACCEPTFILTER:
1112 error = do_setopt_accept_filter(so, sopt);
1118 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1122 so->so_linger = l.l_linger;
1124 so->so_options |= SO_LINGER;
1126 so->so_options &= ~SO_LINGER;
1132 case SO_USELOOPBACK:
1138 error = sooptcopyin(sopt, &optval, sizeof optval,
1143 so->so_options |= sopt->sopt_name;
1145 so->so_options &= ~sopt->sopt_name;
1152 error = sooptcopyin(sopt, &optval, sizeof optval,
1158 * Values < 1 make no sense for any of these
1159 * options, so disallow them.
1166 switch (sopt->sopt_name) {
1169 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1170 &so->so_snd : &so->so_rcv, (u_long)optval,
1171 so, curproc) == 0) {
1178 * Make sure the low-water is never greater than
1182 so->so_snd.sb_lowat =
1183 (optval > so->so_snd.sb_hiwat) ?
1184 so->so_snd.sb_hiwat : optval;
1187 so->so_rcv.sb_lowat =
1188 (optval > so->so_rcv.sb_hiwat) ?
1189 so->so_rcv.sb_hiwat : optval;
1196 error = sooptcopyin(sopt, &tv, sizeof tv,
1201 /* assert(hz > 0); */
1202 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1203 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1207 /* assert(tick > 0); */
1208 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1209 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1210 if (val > SHRT_MAX) {
1215 switch (sopt->sopt_name) {
1217 so->so_snd.sb_timeo = val;
1220 so->so_rcv.sb_timeo = val;
1225 error = ENOPROTOOPT;
1228 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1229 (void) ((*so->so_proto->pr_ctloutput)
1237 /* Helper routine for getsockopt */
1239 sooptcopyout(sopt, buf, len)
1240 struct sockopt *sopt;
1250 * Documented get behavior is that we always return a value,
1251 * possibly truncated to fit in the user's buffer.
1252 * Traditional behavior is that we always tell the user
1253 * precisely how much we copied, rather than something useful
1254 * like the total amount we had available for her.
1255 * Note that this interface is not idempotent; the entire answer must
1256 * generated ahead of time.
1258 valsize = min(len, sopt->sopt_valsize);
1259 sopt->sopt_valsize = valsize;
1260 if (sopt->sopt_val != 0) {
1261 if (sopt->sopt_p != 0)
1262 error = copyout(buf, sopt->sopt_val, valsize);
1264 bcopy(buf, sopt->sopt_val, valsize);
1272 struct sockopt *sopt;
1277 struct accept_filter_arg *afap;
1280 if (sopt->sopt_level != SOL_SOCKET) {
1281 if (so->so_proto && so->so_proto->pr_ctloutput) {
1282 return ((*so->so_proto->pr_ctloutput)
1285 return (ENOPROTOOPT);
1287 switch (sopt->sopt_name) {
1289 case SO_ACCEPTFILTER:
1290 if ((so->so_options & SO_ACCEPTCONN) == 0)
1292 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1294 bzero(afap, sizeof(*afap));
1295 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1296 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1297 if (so->so_accf->so_accept_filter_str != NULL)
1298 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1300 error = sooptcopyout(sopt, afap, sizeof(*afap));
1306 l.l_onoff = so->so_options & SO_LINGER;
1307 l.l_linger = so->so_linger;
1308 error = sooptcopyout(sopt, &l, sizeof l);
1311 case SO_USELOOPBACK:
1320 optval = so->so_options & sopt->sopt_name;
1322 error = sooptcopyout(sopt, &optval, sizeof optval);
1326 optval = so->so_type;
1330 optval = so->so_error;
1335 optval = so->so_snd.sb_hiwat;
1339 optval = so->so_rcv.sb_hiwat;
1343 optval = so->so_snd.sb_lowat;
1347 optval = so->so_rcv.sb_lowat;
1352 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1353 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1355 tv.tv_sec = optval / hz;
1356 tv.tv_usec = (optval % hz) * tick;
1357 error = sooptcopyout(sopt, &tv, sizeof tv);
1361 error = ENOPROTOOPT;
1368 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1370 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1372 struct mbuf *m, *m_prev;
1373 int sopt_size = sopt->sopt_valsize;
1375 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1379 if (sopt_size > MLEN) {
1380 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1381 if ((m->m_flags & M_EXT) == 0) {
1385 m->m_len = min(MCLBYTES, sopt_size);
1387 m->m_len = min(MLEN, sopt_size);
1389 sopt_size -= m->m_len;
1394 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1399 if (sopt_size > MLEN) {
1400 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1401 if ((m->m_flags & M_EXT) == 0) {
1405 m->m_len = min(MCLBYTES, sopt_size);
1407 m->m_len = min(MLEN, sopt_size);
1409 sopt_size -= m->m_len;
1416 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1418 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1420 struct mbuf *m0 = m;
1422 if (sopt->sopt_val == NULL)
1424 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1425 if (sopt->sopt_p != NULL) {
1428 error = copyin(sopt->sopt_val, mtod(m, char *),
1435 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1436 sopt->sopt_valsize -= m->m_len;
1437 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
1440 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1441 panic("ip6_sooptmcopyin");
1445 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1447 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1449 struct mbuf *m0 = m;
1452 if (sopt->sopt_val == NULL)
1454 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1455 if (sopt->sopt_p != NULL) {
1458 error = copyout(mtod(m, char *), sopt->sopt_val,
1465 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1466 sopt->sopt_valsize -= m->m_len;
1467 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
1468 valsize += m->m_len;
1472 /* enough soopt buffer should be given from user-land */
1476 sopt->sopt_valsize = valsize;
1482 register struct socket *so;
1484 selwakeup(&so->so_rcv.sb_sel);
1488 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p)
1490 panic("%s\n", __FUNCTION__);
1496 if (events & (POLLIN | POLLRDNORM))
1498 revents |= events & (POLLIN | POLLRDNORM);
1500 if (events & (POLLOUT | POLLWRNORM))
1501 if (sowriteable(so))
1502 revents |= events & (POLLOUT | POLLWRNORM);
1504 if (events & (POLLPRI | POLLRDBAND))
1505 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1506 revents |= events & (POLLPRI | POLLRDBAND);
1509 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1510 selrecord(p, &so->so_rcv.sb_sel);
1511 so->so_rcv.sb_flags |= SB_SEL;
1514 if (events & (POLLOUT | POLLWRNORM)) {
1515 selrecord(p, &so->so_snd.sb_sel);
1516 so->so_snd.sb_flags |= SB_SEL;