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>
71 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
75 static void filt_sordetach(struct knote *kn);
76 static int filt_soread(struct knote *kn, long hint);
77 static void filt_sowdetach(struct knote *kn);
78 static int filt_sowrite(struct knote *kn, long hint);
79 static int filt_solisten(struct knote *kn, long hint);
81 static struct filterops solisten_filtops =
82 { 1, NULL, filt_sordetach, filt_solisten };
83 static struct filterops soread_filtops =
84 { 1, NULL, filt_sordetach, filt_soread };
85 static struct filterops sowrite_filtops =
86 { 1, NULL, filt_sowdetach, filt_sowrite };
89 struct vm_zone *socket_zone;
90 so_gen_t so_gencnt; /* generation count for sockets */
92 static int somaxconn = SOMAXCONN;
95 * Socket operation routines.
96 * These routines are called by the routines in
97 * sys_socket.c or from a system process, and
98 * implement the semantics of socket operations by
99 * switching out to the protocol specific routines.
103 * Get a socket structure from our zone, and initialize it.
104 * 'waitok' has been implemented for eCos, with [currently] some
105 * rather fixed strategy - it will retry some number of times (10)
106 * after at most 2 minutes. This seems sufficient for sockets which
107 * are tied up in the TCP close process.
112 struct socket *so = NULL;
113 int maxtries = waitok ? 10 : 1;
115 while (maxtries-- > 0) {
116 so = zalloci(socket_zone);
118 /* XXX race condition for reentrant kernel */
119 bzero(so, sizeof *so);
120 so->so_gencnt = ++so_gencnt;
121 so->so_zone = socket_zone;
122 TAILQ_INIT(&so->so_aiojobq);
126 diag_printf("DEBUG: Out of sockets - waiting\n");
127 tsleep(socket_zone, PVM|PCATCH, "soalloc", 120*100);
128 diag_printf("DEBUG: ... retry sockets\n");
135 socreate(dom, aso, type, proto, p)
142 register struct protosw *prp;
143 register struct socket *so;
147 prp = pffindproto(dom, proto, type);
149 prp = pffindtype(dom, type);
151 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
152 return (EPROTONOSUPPORT);
154 if (prp->pr_type != type)
156 so = soalloc(p != 0);
161 TAILQ_INIT(&so->so_incomp);
162 TAILQ_INIT(&so->so_comp);
165 error = (*prp->pr_usrreqs->pru_attach)(so, proto, p);
167 so->so_state |= SS_NOFDREF;
178 struct sockaddr *nam;
184 error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
196 so->so_gencnt = ++so_gencnt;
198 if (so->so_accf != NULL) {
199 if (so->so_accf->so_accept_filter != NULL &&
200 so->so_accf->so_accept_filter->accf_destroy != NULL) {
201 so->so_accf->so_accept_filter->accf_destroy(so);
203 if (so->so_accf->so_accept_filter_str != NULL)
204 FREE(so->so_accf->so_accept_filter_str, M_ACCF);
205 FREE(so->so_accf, M_ACCF);
214 solisten(so, backlog, p)
215 register struct socket *so;
222 error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p);
227 if (TAILQ_EMPTY(&so->so_comp))
228 so->so_options |= SO_ACCEPTCONN;
229 if (backlog < 0 || backlog > somaxconn)
231 so->so_qlimit = backlog;
238 register struct socket *so;
240 struct socket *head = so->so_head;
242 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
245 if (so->so_state & SS_INCOMP) {
246 TAILQ_REMOVE(&head->so_incomp, so, so_list);
248 } else if (so->so_state & SS_COMP) {
250 * We must not decommission a socket that's
251 * on the accept(2) queue. If we do, then
252 * accept(2) may hang after select(2) indicated
253 * that the listening socket was ready.
257 panic("sofree: not queued");
260 so->so_state &= ~SS_INCOMP;
263 sbrelease(&so->so_snd, so);
269 * Close a socket on last file table reference removal.
270 * Initiate disconnect if connected.
271 * Free socket when disconnect complete.
275 register struct socket *so;
277 int s = splnet(); /* conservative */
280 if (so->so_options & SO_ACCEPTCONN) {
281 struct socket *sp, *sonext;
283 sp = TAILQ_FIRST(&so->so_incomp);
284 for (; sp != NULL; sp = sonext) {
285 sonext = TAILQ_NEXT(sp, so_list);
288 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
289 sonext = TAILQ_NEXT(sp, so_list);
290 /* Dequeue from so_comp since sofree() won't do it */
291 TAILQ_REMOVE(&so->so_comp, sp, so_list);
293 sp->so_state &= ~SS_COMP;
300 if (so->so_state & SS_ISCONNECTED) {
301 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
302 error = sodisconnect(so);
306 if (so->so_options & SO_LINGER) {
307 if ((so->so_state & SS_ISDISCONNECTING) &&
308 (so->so_state & SS_NBIO))
310 while (so->so_state & SS_ISCONNECTED) {
311 error = tsleep((caddr_t)&so->so_timeo,
312 PSOCK | PCATCH, "soclos", so->so_linger * hz);
320 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
325 if (so->so_state & SS_NOFDREF)
326 panic("soclose: NOFDREF");
327 so->so_state |= SS_NOFDREF;
334 * Must be called at splnet...
342 error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
352 register struct socket *so;
353 struct sockaddr **nam;
358 if ((so->so_state & SS_NOFDREF) == 0)
359 panic("soaccept: !NOFDREF");
360 so->so_state &= ~SS_NOFDREF;
361 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
367 soconnect(so, nam, p)
368 register struct socket *so;
369 struct sockaddr *nam;
375 if (so->so_options & SO_ACCEPTCONN)
379 * If protocol is connection-based, can only connect once.
380 * Otherwise, if connected, try to disconnect first.
381 * This allows user to disconnect by connecting to, e.g.,
384 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
385 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
386 (error = sodisconnect(so))))
389 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p);
396 register struct socket *so1;
402 error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
409 register struct socket *so;
414 if ((so->so_state & SS_ISCONNECTED) == 0) {
418 if (so->so_state & SS_ISDISCONNECTING) {
422 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
428 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
431 * If send must go all at once and message is larger than
432 * send buffering, then hard error.
433 * Lock against other senders.
434 * If must go all at once and not enough room now, then
435 * inform user that this would block and do nothing.
436 * Otherwise, if nonblocking, send as much as possible.
437 * The data to be sent is described by "uio" if nonzero,
438 * otherwise by the mbuf chain "top" (which must be null
439 * if uio is not). Data provided in mbuf chain must be small
440 * enough to send all at once.
442 * Returns nonzero on error, timeout or signal; callers
443 * must check for short counts if EINTR/ERESTART are returned.
444 * Data and control buffers are freed on return.
447 sosend(so, addr, uio, top, control, flags, p)
448 register struct socket *so;
449 struct sockaddr *addr;
452 struct mbuf *control;
457 register struct mbuf *m;
458 register long space, len, resid;
459 int clen = 0, error, s, dontroute, mlen;
460 int atomic = sosendallatonce(so) || top;
463 resid = uio->uio_resid;
465 resid = top->m_pkthdr.len;
467 * In theory resid should be unsigned.
468 * However, space must be signed, as it might be less than 0
469 * if we over-committed, and we must use a signed comparison
470 * of space and resid. On the other hand, a negative resid
471 * causes us to loop sending 0-length segments to the protocol.
473 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
474 * type sockets since that's an error.
476 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
482 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
483 (so->so_proto->pr_flags & PR_ATOMIC);
485 clen = control->m_len;
486 #define snderr(errno) { error = errno; splx(s); goto release; }
489 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
494 if (so->so_state & SS_CANTSENDMORE)
497 error = so->so_error;
502 if ((so->so_state & SS_ISCONNECTED) == 0) {
504 * `sendto' and `sendmsg' is allowed on a connection-
505 * based socket if it supports implied connect.
506 * Return ENOTCONN if not connected and no address is
509 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
510 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
511 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
512 !(resid == 0 && clen != 0))
514 } else if (addr == 0)
515 snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
516 ENOTCONN : EDESTADDRREQ);
518 space = sbspace(&so->so_snd);
521 if ((atomic && resid > so->so_snd.sb_hiwat) ||
522 clen > so->so_snd.sb_hiwat)
524 if (space < resid + clen && uio &&
525 (atomic || space < so->so_snd.sb_lowat || space < clen)) {
526 if (so->so_state & SS_NBIO)
528 sbunlock(&so->so_snd);
529 error = sbwait(&so->so_snd);
541 * Data is prepackaged in "top".
545 top->m_flags |= M_EOR;
548 MGETHDR(m, M_WAIT, MT_DATA);
555 m->m_pkthdr.rcvif = (struct ifnet *)0;
557 MGET(m, M_WAIT, MT_DATA);
564 if (resid >= MINCLSIZE) {
566 if ((m->m_flags & M_EXT) == 0)
569 len = min(min(mlen, resid), space);
572 len = min(min(mlen, resid), space);
574 * For datagram protocols, leave room
575 * for protocol headers in first mbuf.
577 if (atomic && top == 0 && len < mlen)
581 error = uiomove(mtod(m, caddr_t), (int)len, uio);
582 resid = uio->uio_resid;
585 top->m_pkthdr.len += len;
591 top->m_flags |= M_EOR;
594 } while (space > 0 && atomic);
596 so->so_options |= SO_DONTROUTE;
597 s = splnet(); /* XXX */
599 * XXX all the SS_CANTSENDMORE checks previously
600 * done could be out of date. We could have recieved
601 * a reset packet in an interrupt or maybe we slept
602 * while doing page faults in uiomove() etc. We could
603 * probably recheck again inside the splnet() protection
604 * here, but there are probably other places that this
605 * also happens. We must rethink this.
607 error = (*so->so_proto->pr_usrreqs->pru_send)(so,
608 (flags & MSG_OOB) ? PRUS_OOB :
610 * If the user set MSG_EOF, the protocol
611 * understands this flag and nothing left to
612 * send then use PRU_SEND_EOF instead of PRU_SEND.
614 ((flags & MSG_EOF) &&
615 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
618 /* If there is more to send set PRUS_MORETOCOME */
619 (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
620 top, addr, control, p);
623 so->so_options &= ~SO_DONTROUTE;
630 } while (resid && space > 0);
634 sbunlock(&so->so_snd);
644 * Implement receive operations on a socket.
645 * We depend on the way that records are added to the sockbuf
646 * by sbappend*. In particular, each record (mbufs linked through m_next)
647 * must begin with an address if the protocol so specifies,
648 * followed by an optional mbuf or mbufs containing ancillary data,
649 * and then zero or more mbufs of data.
650 * In order to avoid blocking network interrupts for the entire time here,
651 * we splx() while doing the actual copy to user space.
652 * Although the sockbuf is locked, new data may still be appended,
653 * and thus we must maintain consistency of the sockbuf during that time.
655 * The caller may receive the data as a single mbuf chain by supplying
656 * an mbuf **mp0 for use in returning the chain. The uio is then used
657 * only for the count in uio_resid.
660 soreceive(so, psa, uio, mp0, controlp, flagsp)
661 register struct socket *so;
662 struct sockaddr **psa;
665 struct mbuf **controlp;
668 register struct mbuf *m, **mp;
669 register int flags, len, error, s, offset;
670 struct protosw *pr = so->so_proto;
671 struct mbuf *nextrecord;
673 int orig_resid = uio->uio_resid;
681 flags = *flagsp &~ MSG_EOR;
684 if (flags & MSG_OOB) {
685 m = m_get(M_WAIT, MT_DATA);
689 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
693 error = uiomove(mtod(m, caddr_t),
694 (int) min(uio->uio_resid, m->m_len), uio);
696 } while (uio->uio_resid && error == 0 && m);
703 *mp = (struct mbuf *)0;
704 if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
705 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
708 error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
713 m = so->so_rcv.sb_mb;
715 * If we have less data than requested, block awaiting more
716 * (subject to any timeout) if:
717 * 1. the current count is less than the low water mark, or
718 * 2. MSG_WAITALL is set, and it is possible to do the entire
719 * receive operation at once if we block (resid <= hiwat).
720 * 3. MSG_DONTWAIT is not set
721 * If MSG_WAITALL is set but resid is larger than the receive buffer,
722 * we have to do the receive in sections, and thus risk returning
723 * a short count if a timeout or signal occurs after we start.
725 if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
726 so->so_rcv.sb_cc < uio->uio_resid) &&
727 (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
728 ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
729 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
733 error = so->so_error;
734 if ((flags & MSG_PEEK) == 0)
738 if (so->so_state & SS_CANTRCVMORE) {
744 for (; m; m = m->m_next)
745 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
746 m = so->so_rcv.sb_mb;
749 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
750 (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
754 if (uio->uio_resid == 0)
756 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
760 sbunlock(&so->so_rcv);
761 error = sbwait(&so->so_rcv);
768 nextrecord = m->m_nextpkt;
769 if (pr->pr_flags & PR_ADDR) {
772 *psa = dup_sockaddr(mtod(m, struct sockaddr *),
774 if (flags & MSG_PEEK) {
777 sbfree(&so->so_rcv, m);
778 MFREE(m, so->so_rcv.sb_mb);
779 m = so->so_rcv.sb_mb;
782 while (m && m->m_type == MT_CONTROL && error == 0) {
783 if (flags & MSG_PEEK) {
785 *controlp = m_copy(m, 0, m->m_len);
788 sbfree(&so->so_rcv, m);
790 if (pr->pr_domain->dom_externalize &&
791 mtod(m, struct cmsghdr *)->cmsg_type ==
793 error = (*pr->pr_domain->dom_externalize)(m);
795 so->so_rcv.sb_mb = m->m_next;
797 m = so->so_rcv.sb_mb;
799 MFREE(m, so->so_rcv.sb_mb);
800 m = so->so_rcv.sb_mb;
805 controlp = &(*controlp)->m_next;
809 if ((flags & MSG_PEEK) == 0)
810 m->m_nextpkt = nextrecord;
812 if (type == MT_OOBDATA)
817 while (m && uio->uio_resid > 0 && error == 0) {
818 if (m->m_type == MT_OOBDATA) {
819 if (type != MT_OOBDATA)
821 } else if (type == MT_OOBDATA)
823 so->so_state &= ~SS_RCVATMARK;
824 len = uio->uio_resid;
825 if (so->so_oobmark && len > so->so_oobmark - offset)
826 len = so->so_oobmark - offset;
827 if (len > m->m_len - moff)
828 len = m->m_len - moff;
830 * If mp is set, just pass back the mbufs.
831 * Otherwise copy them out via the uio, then free.
832 * Sockbuf must be consistent here (points to current mbuf,
833 * it points to next record) when we drop priority;
834 * we must note any additions to the sockbuf when we
835 * block interrupts again.
839 error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
844 uio->uio_resid -= len;
845 if (len == m->m_len - moff) {
846 if (m->m_flags & M_EOR)
848 if (flags & MSG_PEEK) {
852 nextrecord = m->m_nextpkt;
853 sbfree(&so->so_rcv, m);
857 so->so_rcv.sb_mb = m = m->m_next;
858 *mp = (struct mbuf *)0;
860 MFREE(m, so->so_rcv.sb_mb);
861 m = so->so_rcv.sb_mb;
864 m->m_nextpkt = nextrecord;
867 if (flags & MSG_PEEK)
871 *mp = m_copym(m, 0, len, M_WAIT);
874 so->so_rcv.sb_cc -= len;
877 if (so->so_oobmark) {
878 if ((flags & MSG_PEEK) == 0) {
879 so->so_oobmark -= len;
880 if (so->so_oobmark == 0) {
881 so->so_state |= SS_RCVATMARK;
886 if (offset == so->so_oobmark)
893 * If the MSG_WAITALL flag is set (for non-atomic socket),
894 * we must not quit until "uio->uio_resid == 0" or an error
895 * termination. If a signal/timeout occurs, return
896 * with a short count but without error.
897 * Keep sockbuf locked against other readers.
899 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
900 !sosendallatonce(so) && !nextrecord) {
901 if (so->so_error || so->so_state & SS_CANTRCVMORE)
903 error = sbwait(&so->so_rcv);
905 sbunlock(&so->so_rcv);
909 m = so->so_rcv.sb_mb;
911 nextrecord = m->m_nextpkt;
915 if (m && pr->pr_flags & PR_ATOMIC) {
917 if ((flags & MSG_PEEK) == 0)
918 (void) sbdroprecord(&so->so_rcv);
920 if ((flags & MSG_PEEK) == 0) {
922 so->so_rcv.sb_mb = nextrecord;
923 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
924 (*pr->pr_usrreqs->pru_rcvd)(so, flags);
926 if (orig_resid == uio->uio_resid && orig_resid &&
927 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
928 sbunlock(&so->so_rcv);
936 sbunlock(&so->so_rcv);
943 register struct socket *so;
946 register struct protosw *pr = so->so_proto;
948 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
954 return ((*pr->pr_usrreqs->pru_shutdown)(so));
960 register struct socket *so;
962 register struct sockbuf *sb = &so->so_rcv;
963 register struct protosw *pr = so->so_proto;
967 sb->sb_flags |= SB_NOINTR;
968 (void) sblock(sb, M_WAITOK);
973 bzero((caddr_t)sb, sizeof (*sb));
975 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
976 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
982 do_setopt_accept_filter(so, sopt)
984 struct sockopt *sopt;
986 struct accept_filter_arg *afap = NULL;
987 struct accept_filter *afp;
988 struct so_accf *af = so->so_accf;
991 /* do not set/remove accept filters on non listen sockets */
992 if ((so->so_options & SO_ACCEPTCONN) == 0) {
997 /* removing the filter */
1000 if (af->so_accept_filter != NULL &&
1001 af->so_accept_filter->accf_destroy != NULL) {
1002 af->so_accept_filter->accf_destroy(so);
1004 if (af->so_accept_filter_str != NULL) {
1005 FREE(af->so_accept_filter_str, M_ACCF);
1010 so->so_options &= ~SO_ACCEPTFILTER;
1013 /* adding a filter */
1014 /* must remove previous filter first */
1019 /* don't put large objects on the kernel stack */
1020 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1021 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1022 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1023 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1026 afp = accept_filt_get(afap->af_name);
1031 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK);
1032 bzero(af, sizeof(*af));
1033 if (afp->accf_create != NULL) {
1034 if (afap->af_name[0] != '\0') {
1035 int len = strlen(afap->af_name) + 1;
1037 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1038 strcpy(af->so_accept_filter_str, afap->af_name);
1040 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1041 if (af->so_accept_filter_arg == NULL) {
1042 FREE(af->so_accept_filter_str, M_ACCF);
1049 af->so_accept_filter = afp;
1051 so->so_options |= SO_ACCEPTFILTER;
1060 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1061 * an additional variant to handle the case where the option value needs
1062 * to be some kind of integer, but not a specific size.
1063 * In addition to their use here, these functions are also called by the
1064 * protocol-level pr_ctloutput() routines.
1067 sooptcopyin(sopt, buf, len, minlen)
1068 struct sockopt *sopt;
1076 * If the user gives us more than we wanted, we ignore it,
1077 * but if we don't get the minimum length the caller
1078 * wants, we return EINVAL. On success, sopt->sopt_valsize
1079 * is set to however much we actually retrieved.
1081 if ((valsize = sopt->sopt_valsize) < minlen)
1084 sopt->sopt_valsize = valsize = len;
1086 if (sopt->sopt_p != 0)
1087 return (copyin(sopt->sopt_val, buf, valsize));
1089 bcopy(sopt->sopt_val, buf, valsize);
1096 struct sockopt *sopt;
1104 if (sopt->sopt_level != SOL_SOCKET) {
1105 if (so->so_proto && so->so_proto->pr_ctloutput)
1106 return ((*so->so_proto->pr_ctloutput)
1108 error = ENOPROTOOPT;
1110 switch (sopt->sopt_name) {
1112 case SO_ACCEPTFILTER:
1113 error = do_setopt_accept_filter(so, sopt);
1119 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1123 so->so_linger = l.l_linger;
1125 so->so_options |= SO_LINGER;
1127 so->so_options &= ~SO_LINGER;
1133 case SO_USELOOPBACK:
1139 error = sooptcopyin(sopt, &optval, sizeof optval,
1144 so->so_options |= sopt->sopt_name;
1146 so->so_options &= ~sopt->sopt_name;
1153 error = sooptcopyin(sopt, &optval, sizeof optval,
1159 * Values < 1 make no sense for any of these
1160 * options, so disallow them.
1167 switch (sopt->sopt_name) {
1170 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1171 &so->so_snd : &so->so_rcv, (u_long)optval,
1172 so, curproc) == 0) {
1179 * Make sure the low-water is never greater than
1183 so->so_snd.sb_lowat =
1184 (optval > so->so_snd.sb_hiwat) ?
1185 so->so_snd.sb_hiwat : optval;
1188 so->so_rcv.sb_lowat =
1189 (optval > so->so_rcv.sb_hiwat) ?
1190 so->so_rcv.sb_hiwat : optval;
1197 error = sooptcopyin(sopt, &tv, sizeof tv,
1202 /* assert(hz > 0); */
1203 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1204 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1208 /* assert(tick > 0); */
1209 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1210 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1211 if (val > SHRT_MAX) {
1216 switch (sopt->sopt_name) {
1218 so->so_snd.sb_timeo = val;
1221 so->so_rcv.sb_timeo = val;
1226 error = ENOPROTOOPT;
1229 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1230 (void) ((*so->so_proto->pr_ctloutput)
1238 /* Helper routine for getsockopt */
1240 sooptcopyout(sopt, buf, len)
1241 struct sockopt *sopt;
1251 * Documented get behavior is that we always return a value,
1252 * possibly truncated to fit in the user's buffer.
1253 * Traditional behavior is that we always tell the user
1254 * precisely how much we copied, rather than something useful
1255 * like the total amount we had available for her.
1256 * Note that this interface is not idempotent; the entire answer must
1257 * generated ahead of time.
1259 valsize = min(len, sopt->sopt_valsize);
1260 sopt->sopt_valsize = valsize;
1261 if (sopt->sopt_val != 0) {
1262 if (sopt->sopt_p != 0)
1263 error = copyout(buf, sopt->sopt_val, valsize);
1265 bcopy(buf, sopt->sopt_val, valsize);
1273 struct sockopt *sopt;
1278 struct accept_filter_arg *afap;
1281 if (sopt->sopt_level != SOL_SOCKET) {
1282 if (so->so_proto && so->so_proto->pr_ctloutput) {
1283 return ((*so->so_proto->pr_ctloutput)
1286 return (ENOPROTOOPT);
1288 switch (sopt->sopt_name) {
1290 case SO_ACCEPTFILTER:
1291 if ((so->so_options & SO_ACCEPTCONN) == 0)
1293 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1295 bzero(afap, sizeof(*afap));
1296 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1297 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1298 if (so->so_accf->so_accept_filter_str != NULL)
1299 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1301 error = sooptcopyout(sopt, afap, sizeof(*afap));
1307 l.l_onoff = so->so_options & SO_LINGER;
1308 l.l_linger = so->so_linger;
1309 error = sooptcopyout(sopt, &l, sizeof l);
1312 case SO_USELOOPBACK:
1321 optval = so->so_options & sopt->sopt_name;
1323 error = sooptcopyout(sopt, &optval, sizeof optval);
1327 optval = so->so_type;
1331 optval = so->so_error;
1336 optval = so->so_snd.sb_hiwat;
1340 optval = so->so_rcv.sb_hiwat;
1344 optval = so->so_snd.sb_lowat;
1348 optval = so->so_rcv.sb_lowat;
1353 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1354 so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1356 tv.tv_sec = optval / hz;
1357 tv.tv_usec = (optval % hz) * tick;
1358 error = sooptcopyout(sopt, &tv, sizeof tv);
1362 error = ENOPROTOOPT;
1369 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1371 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1373 struct mbuf *m, *m_prev;
1374 int sopt_size = sopt->sopt_valsize;
1376 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1380 if (sopt_size > MLEN) {
1381 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1382 if ((m->m_flags & M_EXT) == 0) {
1386 m->m_len = min(MCLBYTES, sopt_size);
1388 m->m_len = min(MLEN, sopt_size);
1390 sopt_size -= m->m_len;
1395 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1400 if (sopt_size > MLEN) {
1401 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1402 if ((m->m_flags & M_EXT) == 0) {
1406 m->m_len = min(MCLBYTES, sopt_size);
1408 m->m_len = min(MLEN, sopt_size);
1410 sopt_size -= m->m_len;
1417 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1419 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1421 struct mbuf *m0 = m;
1423 if (sopt->sopt_val == NULL)
1425 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1426 if (sopt->sopt_p != NULL) {
1429 error = copyin(sopt->sopt_val, mtod(m, char *),
1436 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1437 sopt->sopt_valsize -= m->m_len;
1438 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
1441 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1442 panic("ip6_sooptmcopyin");
1446 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1448 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1450 struct mbuf *m0 = m;
1453 if (sopt->sopt_val == NULL)
1455 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1456 if (sopt->sopt_p != NULL) {
1459 error = copyout(mtod(m, char *), sopt->sopt_val,
1466 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1467 sopt->sopt_valsize -= m->m_len;
1468 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
1469 valsize += m->m_len;
1473 /* enough soopt buffer should be given from user-land */
1477 sopt->sopt_valsize = valsize;
1483 register struct socket *so;
1485 selwakeup(&so->so_rcv.sb_sel);
1489 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p)
1491 panic("%s\n", __FUNCTION__);
1497 if (events & (POLLIN | POLLRDNORM))
1499 revents |= events & (POLLIN | POLLRDNORM);
1501 if (events & (POLLOUT | POLLWRNORM))
1502 if (sowriteable(so))
1503 revents |= events & (POLLOUT | POLLWRNORM);
1505 if (events & (POLLPRI | POLLRDBAND))
1506 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1507 revents |= events & (POLLPRI | POLLRDBAND);
1510 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1511 selrecord(p, &so->so_rcv.sb_sel);
1512 so->so_rcv.sb_flags |= SB_SEL;
1515 if (events & (POLLOUT | POLLWRNORM)) {
1516 selrecord(p, &so->so_snd.sb_sel);
1517 so->so_snd.sb_flags |= SB_SEL;