2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
63 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.24 2003/11/11 17:18:18 silby Exp $
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/fcntl.h>
72 #include <sys/malloc.h>
74 #include <sys/domain.h>
75 #include <sys/file.h> /* for struct knote */
76 #include <sys/kernel.h>
77 #include <sys/event.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/socketops.h>
83 #include <sys/resourcevar.h>
84 #include <sys/signalvar.h>
85 #include <sys/sysctl.h>
88 #include <vm/vm_zone.h>
90 #include <net/netmsg2.h>
91 #include <net/netisr2.h>
93 #include <sys/thread2.h>
94 #include <sys/socketvar2.h>
95 #include <sys/spinlock2.h>
97 #include <machine/limits.h>
100 extern int tcp_sosend_agglim;
101 extern int tcp_sosend_async;
102 extern int tcp_sosend_jcluster;
103 extern int udp_sosend_async;
104 extern int udp_sosend_prepend;
106 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
109 static void filt_sordetach(struct knote *kn);
110 static int filt_soread(struct knote *kn, long hint);
111 static void filt_sowdetach(struct knote *kn);
112 static int filt_sowrite(struct knote *kn, long hint);
113 static int filt_solisten(struct knote *kn, long hint);
115 static void sodiscard(struct socket *so);
116 static int soclose_sync(struct socket *so, int fflag);
117 static void soclose_fast(struct socket *so);
119 static struct filterops solisten_filtops =
120 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sordetach, filt_solisten };
121 static struct filterops soread_filtops =
122 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sordetach, filt_soread };
123 static struct filterops sowrite_filtops =
124 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sowdetach, filt_sowrite };
125 static struct filterops soexcept_filtops =
126 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sordetach, filt_soread };
128 MALLOC_DEFINE(M_SOCKET, "socket", "socket struct");
129 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
130 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
133 static int somaxconn = SOMAXCONN;
134 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
135 &somaxconn, 0, "Maximum pending socket connection queue size");
137 static int use_soclose_fast = 1;
138 SYSCTL_INT(_kern_ipc, OID_AUTO, soclose_fast, CTLFLAG_RW,
139 &use_soclose_fast, 0, "Fast socket close");
141 int use_soaccept_pred_fast = 1;
142 SYSCTL_INT(_kern_ipc, OID_AUTO, soaccept_pred_fast, CTLFLAG_RW,
143 &use_soaccept_pred_fast, 0, "Fast socket accept predication");
145 int use_sendfile_async = 1;
146 SYSCTL_INT(_kern_ipc, OID_AUTO, sendfile_async, CTLFLAG_RW,
147 &use_sendfile_async, 0, "sendfile uses asynchronized pru_send");
149 int use_soconnect_async = 1;
150 SYSCTL_INT(_kern_ipc, OID_AUTO, soconnect_async, CTLFLAG_RW,
151 &use_soconnect_async, 0, "soconnect uses asynchronized pru_connect");
154 * Socket operation routines.
155 * These routines are called by the routines in
156 * sys_socket.c or from a system process, and
157 * implement the semantics of socket operations by
158 * switching out to the protocol specific routines.
162 * Get a socket structure, and initialize it.
163 * Note that it would probably be better to allocate socket
164 * and PCB at the same time, but I'm not convinced that all
165 * the protocols can be easily modified to do this.
168 soalloc(int waitok, struct protosw *pr)
173 waitmask = waitok ? M_WAITOK : M_NOWAIT;
174 so = kmalloc(sizeof(struct socket), M_SOCKET, M_ZERO|waitmask);
176 /* XXX race condition for reentrant kernel */
178 TAILQ_INIT(&so->so_aiojobq);
179 TAILQ_INIT(&so->so_rcv.ssb_kq.ki_mlist);
180 TAILQ_INIT(&so->so_snd.ssb_kq.ki_mlist);
181 lwkt_token_init(&so->so_rcv.ssb_token, "rcvtok");
182 lwkt_token_init(&so->so_snd.ssb_token, "sndtok");
183 spin_init(&so->so_rcvd_spin);
184 netmsg_init(&so->so_rcvd_msg.base, so, &netisr_adone_rport,
185 MSGF_DROPABLE | MSGF_PRIORITY,
186 so->so_proto->pr_usrreqs->pru_rcvd);
187 so->so_rcvd_msg.nm_pru_flags |= PRUR_ASYNC;
188 so->so_state = SS_NOFDREF;
195 socreate(int dom, struct socket **aso, int type,
196 int proto, struct thread *td)
198 struct proc *p = td->td_proc;
201 struct pru_attach_info ai;
205 prp = pffindproto(dom, proto, type);
207 prp = pffindtype(dom, type);
209 if (prp == NULL || prp->pr_usrreqs->pru_attach == 0)
210 return (EPROTONOSUPPORT);
212 if (p->p_ucred->cr_prison && jail_socket_unixiproute_only &&
213 prp->pr_domain->dom_family != PF_LOCAL &&
214 prp->pr_domain->dom_family != PF_INET &&
215 prp->pr_domain->dom_family != PF_INET6 &&
216 prp->pr_domain->dom_family != PF_ROUTE) {
217 return (EPROTONOSUPPORT);
220 if (prp->pr_type != type)
222 so = soalloc(p != NULL, prp);
227 * Callers of socreate() presumably will connect up a descriptor
228 * and call soclose() if they cannot. This represents our so_refs
229 * (which should be 1) from soalloc().
231 soclrstate(so, SS_NOFDREF);
234 * Set a default port for protocol processing. No action will occur
235 * on the socket on this port until an inpcb is attached to it and
236 * is able to match incoming packets, or until the socket becomes
237 * available to userland.
239 * We normally default the socket to the protocol thread on cpu 0,
240 * if protocol does not provide its own method to initialize the
243 * If PR_SYNC_PORT is set (unix domain sockets) there is no protocol
244 * thread and all pr_*()/pru_*() calls are executed synchronously.
246 if (prp->pr_flags & PR_SYNC_PORT)
247 so->so_port = &netisr_sync_port;
248 else if (prp->pr_initport != NULL)
249 so->so_port = prp->pr_initport();
251 so->so_port = netisr_cpuport(0);
253 TAILQ_INIT(&so->so_incomp);
254 TAILQ_INIT(&so->so_comp);
256 so->so_cred = crhold(p->p_ucred);
257 ai.sb_rlimit = &p->p_rlimit[RLIMIT_SBSIZE];
258 ai.p_ucred = p->p_ucred;
259 ai.fd_rdir = p->p_fd->fd_rdir;
262 * Auto-sizing of socket buffers is managed by the protocols and
263 * the appropriate flags must be set in the pru_attach function.
265 error = so_pru_attach(so, proto, &ai);
267 sosetstate(so, SS_NOFDREF);
268 sofree(so); /* from soalloc */
273 * NOTE: Returns referenced socket.
280 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
284 error = so_pru_bind(so, nam, td);
289 sodealloc(struct socket *so)
291 if (so->so_rcv.ssb_hiwat)
292 (void)chgsbsize(so->so_cred->cr_uidinfo,
293 &so->so_rcv.ssb_hiwat, 0, RLIM_INFINITY);
294 if (so->so_snd.ssb_hiwat)
295 (void)chgsbsize(so->so_cred->cr_uidinfo,
296 &so->so_snd.ssb_hiwat, 0, RLIM_INFINITY);
298 /* remove accept filter if present */
299 if (so->so_accf != NULL)
300 do_setopt_accept_filter(so, NULL);
303 if (so->so_faddr != NULL)
304 kfree(so->so_faddr, M_SONAME);
309 solisten(struct socket *so, int backlog, struct thread *td)
313 short oldopt, oldqlimit;
316 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING))
320 oldopt = so->so_options;
321 oldqlimit = so->so_qlimit;
324 lwkt_gettoken(&so->so_rcv.ssb_token);
325 if (TAILQ_EMPTY(&so->so_comp))
326 so->so_options |= SO_ACCEPTCONN;
327 lwkt_reltoken(&so->so_rcv.ssb_token);
328 if (backlog < 0 || backlog > somaxconn)
330 so->so_qlimit = backlog;
331 /* SCTP needs to look at tweak both the inbound backlog parameter AND
332 * the so_options (UDP model both connect's and gets inbound
333 * connections .. implicitly).
335 error = so_pru_listen(so, td);
338 /* Restore the params */
339 so->so_options = oldopt;
340 so->so_qlimit = oldqlimit;
348 * Destroy a disconnected socket. This routine is a NOP if entities
349 * still have a reference on the socket:
351 * so_pcb - The protocol stack still has a reference
352 * SS_NOFDREF - There is no longer a file pointer reference
355 sofree(struct socket *so)
360 * This is a bit hackish at the moment. We need to interlock
361 * any accept queue we are on before we potentially lose the
362 * last reference to avoid races against a re-reference from
363 * someone operating on the queue.
365 while ((head = so->so_head) != NULL) {
366 lwkt_getpooltoken(head);
367 if (so->so_head == head)
369 lwkt_relpooltoken(head);
373 * Arbitrage the last free.
375 KKASSERT(so->so_refs > 0);
376 if (atomic_fetchadd_int(&so->so_refs, -1) != 1) {
378 lwkt_relpooltoken(head);
382 KKASSERT(so->so_pcb == NULL && (so->so_state & SS_NOFDREF));
383 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
386 * We're done, remove ourselves from the accept queue we are
387 * on, if we are on one.
390 if (so->so_state & SS_INCOMP) {
391 TAILQ_REMOVE(&head->so_incomp, so, so_list);
393 } else if (so->so_state & SS_COMP) {
395 * We must not decommission a socket that's
396 * on the accept(2) queue. If we do, then
397 * accept(2) may hang after select(2) indicated
398 * that the listening socket was ready.
400 lwkt_relpooltoken(head);
403 panic("sofree: not queued");
405 soclrstate(so, SS_INCOMP);
407 lwkt_relpooltoken(head);
409 ssb_release(&so->so_snd, so);
415 * Close a socket on last file table reference removal.
416 * Initiate disconnect if connected.
417 * Free socket when disconnect complete.
420 soclose(struct socket *so, int fflag)
424 funsetown(&so->so_sigio);
425 if (!use_soclose_fast ||
426 (so->so_proto->pr_flags & PR_SYNC_PORT) ||
427 ((so->so_state & SS_ISCONNECTED) &&
428 (so->so_options & SO_LINGER))) {
429 error = soclose_sync(so, fflag);
438 sodiscard(struct socket *so)
440 lwkt_getpooltoken(so);
441 if (so->so_options & SO_ACCEPTCONN) {
444 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
445 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
446 soclrstate(sp, SS_INCOMP);
451 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
452 TAILQ_REMOVE(&so->so_comp, sp, so_list);
453 soclrstate(sp, SS_COMP);
459 lwkt_relpooltoken(so);
461 if (so->so_state & SS_NOFDREF)
462 panic("soclose: NOFDREF");
463 sosetstate(so, SS_NOFDREF); /* take ref */
467 soinherit(struct socket *so, struct socket *so_inh)
469 TAILQ_HEAD(, socket) comp, incomp;
473 KASSERT(so->so_options & SO_ACCEPTCONN,
474 ("so does not accept connection"));
475 KASSERT(so_inh->so_options & SO_ACCEPTCONN,
476 ("so_inh does not accept connection"));
481 lwkt_getpooltoken(so);
482 lwkt_getpooltoken(so_inh);
485 * Save completed queue and incompleted queue
487 TAILQ_CONCAT(&comp, &so->so_comp, so_list);
491 TAILQ_CONCAT(&incomp, &so->so_incomp, so_list);
492 incqlen = so->so_incqlen;
496 * Append the saved completed queue and incompleted
497 * queue to the socket inherits them.
500 * This may temporarily break the inheriting socket's
503 TAILQ_FOREACH(sp, &comp, so_list) {
504 sp->so_head = so_inh;
506 sp->so_cred = crhold(so_inh->so_cred);
509 TAILQ_FOREACH(sp, &incomp, so_list) {
510 sp->so_head = so_inh;
512 sp->so_cred = crhold(so_inh->so_cred);
515 TAILQ_CONCAT(&so_inh->so_comp, &comp, so_list);
516 so_inh->so_qlen += qlen;
518 TAILQ_CONCAT(&so_inh->so_incomp, &incomp, so_list);
519 so_inh->so_incqlen += incqlen;
521 lwkt_relpooltoken(so_inh);
522 lwkt_relpooltoken(so);
526 * "New" connections have arrived
529 wakeup(&so_inh->so_timeo);
534 soclose_sync(struct socket *so, int fflag)
538 if (so->so_pcb == NULL)
540 if (so->so_state & SS_ISCONNECTED) {
541 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
542 error = sodisconnect(so);
546 if (so->so_options & SO_LINGER) {
547 if ((so->so_state & SS_ISDISCONNECTING) &&
550 while (so->so_state & SS_ISCONNECTED) {
551 error = tsleep(&so->so_timeo, PCATCH,
552 "soclos", so->so_linger * hz);
562 error2 = so_pru_detach(so);
568 so_pru_sync(so); /* unpend async sending */
569 sofree(so); /* dispose of ref */
575 soclose_sofree_async_handler(netmsg_t msg)
577 sofree(msg->base.nm_so);
581 soclose_sofree_async(struct socket *so)
583 struct netmsg_base *base = &so->so_clomsg;
585 netmsg_init(base, so, &netisr_apanic_rport, 0,
586 soclose_sofree_async_handler);
587 lwkt_sendmsg(so->so_port, &base->lmsg);
591 soclose_disconn_async_handler(netmsg_t msg)
593 struct socket *so = msg->base.nm_so;
595 if ((so->so_state & SS_ISCONNECTED) &&
596 (so->so_state & SS_ISDISCONNECTING) == 0)
597 so_pru_disconnect_direct(so);
600 so_pru_detach_direct(so);
607 soclose_disconn_async(struct socket *so)
609 struct netmsg_base *base = &so->so_clomsg;
611 netmsg_init(base, so, &netisr_apanic_rport, 0,
612 soclose_disconn_async_handler);
613 lwkt_sendmsg(so->so_port, &base->lmsg);
617 soclose_detach_async_handler(netmsg_t msg)
619 struct socket *so = msg->base.nm_so;
622 so_pru_detach_direct(so);
629 soclose_detach_async(struct socket *so)
631 struct netmsg_base *base = &so->so_clomsg;
633 netmsg_init(base, so, &netisr_apanic_rport, 0,
634 soclose_detach_async_handler);
635 lwkt_sendmsg(so->so_port, &base->lmsg);
639 soclose_fast(struct socket *so)
641 if (so->so_pcb == NULL)
644 if ((so->so_state & SS_ISCONNECTED) &&
645 (so->so_state & SS_ISDISCONNECTING) == 0) {
646 soclose_disconn_async(so);
651 soclose_detach_async(so);
657 soclose_sofree_async(so);
661 * Abort and destroy a socket. Only one abort can be in progress
662 * at any given moment.
665 soabort(struct socket *so)
672 soabort_async(struct socket *so)
675 so_pru_abort_async(so);
679 soabort_oncpu(struct socket *so)
682 so_pru_abort_direct(so);
686 * so is passed in ref'd, which becomes owned by
687 * the cleared SS_NOFDREF flag.
690 soaccept_generic(struct socket *so)
692 if ((so->so_state & SS_NOFDREF) == 0)
693 panic("soaccept: !NOFDREF");
694 soclrstate(so, SS_NOFDREF); /* owned by lack of SS_NOFDREF */
698 soaccept(struct socket *so, struct sockaddr **nam)
702 soaccept_generic(so);
703 error = so_pru_accept(so, nam);
708 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td,
713 if (so->so_options & SO_ACCEPTCONN)
716 * If protocol is connection-based, can only connect once.
717 * Otherwise, if connected, try to disconnect first.
718 * This allows user to disconnect by connecting to, e.g.,
721 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
722 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
723 (error = sodisconnect(so)))) {
727 * Prevent accumulated error from previous connection
731 if (!sync && so->so_proto->pr_usrreqs->pru_preconnect)
732 error = so_pru_connect_async(so, nam, td);
734 error = so_pru_connect(so, nam, td);
740 soconnect2(struct socket *so1, struct socket *so2)
744 error = so_pru_connect2(so1, so2);
749 sodisconnect(struct socket *so)
753 if ((so->so_state & SS_ISCONNECTED) == 0) {
757 if (so->so_state & SS_ISDISCONNECTING) {
761 error = so_pru_disconnect(so);
766 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
769 * If send must go all at once and message is larger than
770 * send buffering, then hard error.
771 * Lock against other senders.
772 * If must go all at once and not enough room now, then
773 * inform user that this would block and do nothing.
774 * Otherwise, if nonblocking, send as much as possible.
775 * The data to be sent is described by "uio" if nonzero,
776 * otherwise by the mbuf chain "top" (which must be null
777 * if uio is not). Data provided in mbuf chain must be small
778 * enough to send all at once.
780 * Returns nonzero on error, timeout or signal; callers
781 * must check for short counts if EINTR/ERESTART are returned.
782 * Data and control buffers are freed on return.
785 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
786 struct mbuf *top, struct mbuf *control, int flags,
793 int clen = 0, error, dontroute, mlen;
794 int atomic = sosendallatonce(so) || top;
798 resid = uio->uio_resid;
800 resid = (size_t)top->m_pkthdr.len;
803 for (m = top; m; m = m->m_next)
805 KKASSERT(top->m_pkthdr.len == len);
810 * WARNING! resid is unsigned, space and len are signed. space
811 * can wind up negative if the sockbuf is overcommitted.
813 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
814 * type sockets since that's an error.
816 if (so->so_type == SOCK_STREAM && (flags & MSG_EOR)) {
822 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
823 (so->so_proto->pr_flags & PR_ATOMIC);
824 if (td->td_lwp != NULL)
825 td->td_lwp->lwp_ru.ru_msgsnd++;
827 clen = control->m_len;
828 #define gotoerr(errcode) { error = errcode; goto release; }
831 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
836 if (so->so_state & SS_CANTSENDMORE)
839 error = so->so_error;
843 if ((so->so_state & SS_ISCONNECTED) == 0) {
845 * `sendto' and `sendmsg' is allowed on a connection-
846 * based socket if it supports implied connect.
847 * Return ENOTCONN if not connected and no address is
850 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
851 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
852 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
853 !(resid == 0 && clen != 0))
855 } else if (addr == NULL)
856 gotoerr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
857 ENOTCONN : EDESTADDRREQ);
859 if ((atomic && resid > so->so_snd.ssb_hiwat) ||
860 clen > so->so_snd.ssb_hiwat) {
863 space = ssb_space(&so->so_snd);
866 if ((space < 0 || (size_t)space < resid + clen) && uio &&
867 (atomic || space < so->so_snd.ssb_lowat || space < clen)) {
868 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
869 gotoerr(EWOULDBLOCK);
870 ssb_unlock(&so->so_snd);
871 error = ssb_wait(&so->so_snd);
881 * Data is prepackaged in "top".
885 top->m_flags |= M_EOR;
889 m = m_getl((int)resid, MB_WAIT, MT_DATA,
890 top == NULL ? M_PKTHDR : 0, &mlen);
893 m->m_pkthdr.rcvif = NULL;
895 len = imin((int)szmin(mlen, resid), space);
896 if (resid < MINCLSIZE) {
898 * For datagram protocols, leave room
899 * for protocol headers in first mbuf.
901 if (atomic && top == NULL && len < mlen)
905 error = uiomove(mtod(m, caddr_t), (size_t)len, uio);
906 resid = uio->uio_resid;
909 top->m_pkthdr.len += len;
915 top->m_flags |= M_EOR;
918 } while (space > 0 && atomic);
920 so->so_options |= SO_DONTROUTE;
921 if (flags & MSG_OOB) {
922 pru_flags = PRUS_OOB;
923 } else if ((flags & MSG_EOF) &&
924 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
927 * If the user set MSG_EOF, the protocol
928 * understands this flag and nothing left to
929 * send then use PRU_SEND_EOF instead of PRU_SEND.
931 pru_flags = PRUS_EOF;
932 } else if (resid > 0 && space > 0) {
933 /* If there is more to send, set PRUS_MORETOCOME */
934 pru_flags = PRUS_MORETOCOME;
939 * XXX all the SS_CANTSENDMORE checks previously
940 * done could be out of date. We could have recieved
941 * a reset packet in an interrupt or maybe we slept
942 * while doing page faults in uiomove() etc. We could
943 * probably recheck again inside the splnet() protection
944 * here, but there are probably other places that this
945 * also happens. We must rethink this.
947 error = so_pru_send(so, pru_flags, top, addr, control, td);
949 so->so_options &= ~SO_DONTROUTE;
956 } while (resid && space > 0);
960 ssb_unlock(&so->so_snd);
971 * A specialization of sosend() for UDP based on protocol-specific knowledge:
972 * so->so_proto->pr_flags has the PR_ATOMIC field set. This means that
973 * sosendallatonce() returns true,
974 * the "atomic" variable is true,
975 * and sosendudp() blocks until space is available for the entire send.
976 * so->so_proto->pr_flags does not have the PR_CONNREQUIRED or
977 * PR_IMPLOPCL flags set.
978 * UDP has no out-of-band data.
979 * UDP has no control data.
980 * UDP does not support MSG_EOR.
983 sosendudp(struct socket *so, struct sockaddr *addr, struct uio *uio,
984 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
987 int error, pru_flags = 0;
990 if (td->td_lwp != NULL)
991 td->td_lwp->lwp_ru.ru_msgsnd++;
995 KASSERT((uio && !top) || (top && !uio), ("bad arguments to sosendudp"));
996 resid = uio ? uio->uio_resid : (size_t)top->m_pkthdr.len;
999 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
1003 if (so->so_state & SS_CANTSENDMORE)
1006 error = so->so_error;
1010 if (!(so->so_state & SS_ISCONNECTED) && addr == NULL)
1011 gotoerr(EDESTADDRREQ);
1012 if (resid > so->so_snd.ssb_hiwat)
1014 space = ssb_space(&so->so_snd);
1015 if (uio && (space < 0 || (size_t)space < resid)) {
1016 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
1017 gotoerr(EWOULDBLOCK);
1018 ssb_unlock(&so->so_snd);
1019 error = ssb_wait(&so->so_snd);
1026 int hdrlen = max_hdr;
1029 * We try to optimize out the additional mbuf
1030 * allocations in M_PREPEND() on output path, e.g.
1031 * - udp_output(), when it tries to prepend protocol
1033 * - Link layer output function, when it tries to
1034 * prepend link layer header.
1036 * This probably will not benefit any data that will
1037 * be fragmented, so this optimization is only performed
1038 * when the size of data and max size of protocol+link
1039 * headers fit into one mbuf cluster.
1041 if (uio->uio_resid > MCLBYTES - hdrlen ||
1042 !udp_sosend_prepend) {
1043 top = m_uiomove(uio);
1049 top = m_getl(uio->uio_resid + hdrlen, MB_WAIT,
1050 MT_DATA, M_PKTHDR, &nsize);
1051 KASSERT(nsize >= uio->uio_resid + hdrlen,
1052 ("sosendudp invalid nsize %d, "
1053 "resid %zu, hdrlen %d",
1054 nsize, uio->uio_resid, hdrlen));
1056 top->m_len = uio->uio_resid;
1057 top->m_pkthdr.len = uio->uio_resid;
1058 top->m_data += hdrlen;
1060 error = uiomove(mtod(top, caddr_t), top->m_len, uio);
1066 if (flags & MSG_DONTROUTE)
1067 pru_flags |= PRUS_DONTROUTE;
1069 if (udp_sosend_async && (flags & MSG_SYNC) == 0) {
1070 so_pru_send_async(so, pru_flags, top, addr, NULL, td);
1073 error = so_pru_send(so, pru_flags, top, addr, NULL, td);
1075 top = NULL; /* sent or freed in lower layer */
1078 ssb_unlock(&so->so_snd);
1086 sosendtcp(struct socket *so, struct sockaddr *addr, struct uio *uio,
1087 struct mbuf *top, struct mbuf *control, int flags,
1099 KKASSERT(top == NULL);
1101 resid = uio->uio_resid;
1104 resid = (size_t)top->m_pkthdr.len;
1107 for (m = top; m; m = m->m_next)
1109 KKASSERT(top->m_pkthdr.len == len);
1114 * WARNING! resid is unsigned, space and len are signed. space
1115 * can wind up negative if the sockbuf is overcommitted.
1117 * Also check to make sure that MSG_EOR isn't used on TCP
1119 if (flags & MSG_EOR) {
1125 /* TCP doesn't do control messages (rights, creds, etc) */
1126 if (control->m_len) {
1130 m_freem(control); /* empty control, just free it */
1134 if (td->td_lwp != NULL)
1135 td->td_lwp->lwp_ru.ru_msgsnd++;
1137 #define gotoerr(errcode) { error = errcode; goto release; }
1140 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
1145 if (so->so_state & SS_CANTSENDMORE)
1148 error = so->so_error;
1152 if ((so->so_state & SS_ISCONNECTED) == 0 &&
1153 (so->so_state & SS_ISCONFIRMING) == 0)
1155 if (allatonce && resid > so->so_snd.ssb_hiwat)
1158 space = ssb_space_prealloc(&so->so_snd);
1159 if (flags & MSG_OOB)
1161 if ((space < 0 || (size_t)space < resid) && !allatonce &&
1162 space < so->so_snd.ssb_lowat) {
1163 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
1164 gotoerr(EWOULDBLOCK);
1165 ssb_unlock(&so->so_snd);
1166 error = ssb_wait(&so->so_snd);
1173 int cnt = 0, async = 0;
1177 * Data is prepackaged in "top".
1181 if (resid > INT_MAX)
1183 if (tcp_sosend_jcluster) {
1184 m = m_getlj((int)resid, MB_WAIT, MT_DATA,
1185 top == NULL ? M_PKTHDR : 0, &mlen);
1187 m = m_getl((int)resid, MB_WAIT, MT_DATA,
1188 top == NULL ? M_PKTHDR : 0, &mlen);
1191 m->m_pkthdr.len = 0;
1192 m->m_pkthdr.rcvif = NULL;
1194 len = imin((int)szmin(mlen, resid), space);
1196 error = uiomove(mtod(m, caddr_t), (size_t)len, uio);
1197 resid = uio->uio_resid;
1200 top->m_pkthdr.len += len;
1207 } while (space > 0 && cnt < tcp_sosend_agglim);
1209 if (tcp_sosend_async)
1212 if (flags & MSG_OOB) {
1213 pru_flags = PRUS_OOB;
1215 } else if ((flags & MSG_EOF) && resid == 0) {
1216 pru_flags = PRUS_EOF;
1217 } else if (resid > 0 && space > 0) {
1218 /* If there is more to send, set PRUS_MORETOCOME */
1219 pru_flags = PRUS_MORETOCOME;
1225 if (flags & MSG_SYNC)
1229 * XXX all the SS_CANTSENDMORE checks previously
1230 * done could be out of date. We could have recieved
1231 * a reset packet in an interrupt or maybe we slept
1232 * while doing page faults in uiomove() etc. We could
1233 * probably recheck again inside the splnet() protection
1234 * here, but there are probably other places that this
1235 * also happens. We must rethink this.
1237 for (m = top; m; m = m->m_next)
1238 ssb_preallocstream(&so->so_snd, m);
1240 error = so_pru_send(so, pru_flags, top,
1243 so_pru_send_async(so, pru_flags, top,
1252 } while (resid && space > 0);
1256 ssb_unlock(&so->so_snd);
1267 * Implement receive operations on a socket.
1269 * We depend on the way that records are added to the signalsockbuf
1270 * by sbappend*. In particular, each record (mbufs linked through m_next)
1271 * must begin with an address if the protocol so specifies,
1272 * followed by an optional mbuf or mbufs containing ancillary data,
1273 * and then zero or more mbufs of data.
1275 * Although the signalsockbuf is locked, new data may still be appended.
1276 * A token inside the ssb_lock deals with MP issues and still allows
1277 * the network to access the socket if we block in a uio.
1279 * The caller may receive the data as a single mbuf chain by supplying
1280 * an mbuf **mp0 for use in returning the chain. The uio is then used
1281 * only for the count in uio_resid.
1284 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
1285 struct sockbuf *sio, struct mbuf **controlp, int *flagsp)
1288 struct mbuf *free_chain = NULL;
1289 int flags, len, error, offset;
1290 struct protosw *pr = so->so_proto;
1292 size_t resid, orig_resid;
1295 resid = uio->uio_resid;
1297 resid = (size_t)(sio->sb_climit - sio->sb_cc);
1305 flags = *flagsp &~ MSG_EOR;
1308 if (flags & MSG_OOB) {
1309 m = m_get(MB_WAIT, MT_DATA);
1312 error = so_pru_rcvoob(so, m, flags & MSG_PEEK);
1318 KKASSERT(resid >= (size_t)m->m_len);
1319 resid -= (size_t)m->m_len;
1320 } while (resid > 0 && m);
1323 uio->uio_resid = resid;
1324 error = uiomove(mtod(m, caddr_t),
1325 (int)szmin(resid, m->m_len),
1327 resid = uio->uio_resid;
1329 } while (uio->uio_resid && error == 0 && m);
1336 if ((so->so_state & SS_ISCONFIRMING) && resid)
1340 * The token interlocks against the protocol thread while
1341 * ssb_lock is a blocking lock against other userland entities.
1343 lwkt_gettoken(&so->so_rcv.ssb_token);
1345 error = ssb_lock(&so->so_rcv, SBLOCKWAIT(flags));
1349 m = so->so_rcv.ssb_mb;
1351 * If we have less data than requested, block awaiting more
1352 * (subject to any timeout) if:
1353 * 1. the current count is less than the low water mark, or
1354 * 2. MSG_WAITALL is set, and it is possible to do the entire
1355 * receive operation at once if we block (resid <= hiwat).
1356 * 3. MSG_DONTWAIT is not set
1357 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1358 * we have to do the receive in sections, and thus risk returning
1359 * a short count if a timeout or signal occurs after we start.
1361 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1362 (size_t)so->so_rcv.ssb_cc < resid) &&
1363 (so->so_rcv.ssb_cc < so->so_rcv.ssb_lowat ||
1364 ((flags & MSG_WAITALL) && resid <= (size_t)so->so_rcv.ssb_hiwat)) &&
1365 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
1366 KASSERT(m != NULL || !so->so_rcv.ssb_cc, ("receive 1"));
1370 error = so->so_error;
1371 if ((flags & MSG_PEEK) == 0)
1375 if (so->so_state & SS_CANTRCVMORE) {
1381 for (; m; m = m->m_next) {
1382 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1383 m = so->so_rcv.ssb_mb;
1387 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1388 (pr->pr_flags & PR_CONNREQUIRED)) {
1394 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
1395 error = EWOULDBLOCK;
1398 ssb_unlock(&so->so_rcv);
1399 error = ssb_wait(&so->so_rcv);
1405 if (uio && uio->uio_td && uio->uio_td->td_proc)
1406 uio->uio_td->td_lwp->lwp_ru.ru_msgrcv++;
1409 * note: m should be == sb_mb here. Cache the next record while
1410 * cleaning up. Note that calling m_free*() will break out critical
1413 KKASSERT(m == so->so_rcv.ssb_mb);
1416 * Skip any address mbufs prepending the record.
1418 if (pr->pr_flags & PR_ADDR) {
1419 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
1422 *psa = dup_sockaddr(mtod(m, struct sockaddr *));
1423 if (flags & MSG_PEEK)
1426 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1430 * Skip any control mbufs prepending the record.
1433 if (pr->pr_flags & PR_ADDR_OPT) {
1435 * For SCTP we may be getting a
1436 * whole message OR a partial delivery.
1438 if (m && m->m_type == MT_SONAME) {
1441 *psa = dup_sockaddr(mtod(m, struct sockaddr *));
1442 if (flags & MSG_PEEK)
1445 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1449 while (m && m->m_type == MT_CONTROL && error == 0) {
1450 if (flags & MSG_PEEK) {
1452 *controlp = m_copy(m, 0, m->m_len);
1453 m = m->m_next; /* XXX race */
1456 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
1457 if (pr->pr_domain->dom_externalize &&
1458 mtod(m, struct cmsghdr *)->cmsg_type ==
1460 error = (*pr->pr_domain->dom_externalize)(m);
1464 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1467 if (controlp && *controlp) {
1469 controlp = &(*controlp)->m_next;
1478 if (type == MT_OOBDATA)
1483 * Copy to the UIO or mbuf return chain (*mp).
1487 while (m && resid > 0 && error == 0) {
1488 if (m->m_type == MT_OOBDATA) {
1489 if (type != MT_OOBDATA)
1491 } else if (type == MT_OOBDATA)
1494 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
1496 soclrstate(so, SS_RCVATMARK);
1497 len = (resid > INT_MAX) ? INT_MAX : resid;
1498 if (so->so_oobmark && len > so->so_oobmark - offset)
1499 len = so->so_oobmark - offset;
1500 if (len > m->m_len - moff)
1501 len = m->m_len - moff;
1504 * Copy out to the UIO or pass the mbufs back to the SIO.
1505 * The SIO is dealt with when we eat the mbuf, but deal
1506 * with the resid here either way.
1509 uio->uio_resid = resid;
1510 error = uiomove(mtod(m, caddr_t) + moff, len, uio);
1511 resid = uio->uio_resid;
1515 resid -= (size_t)len;
1519 * Eat the entire mbuf or just a piece of it
1521 if (len == m->m_len - moff) {
1522 if (m->m_flags & M_EOR)
1525 if (m->m_flags & M_NOTIFICATION)
1526 flags |= MSG_NOTIFICATION;
1528 if (flags & MSG_PEEK) {
1533 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
1537 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1541 if (flags & MSG_PEEK) {
1545 n = m_copym(m, 0, len, MB_WAIT);
1551 so->so_rcv.ssb_cc -= len;
1554 if (so->so_oobmark) {
1555 if ((flags & MSG_PEEK) == 0) {
1556 so->so_oobmark -= len;
1557 if (so->so_oobmark == 0) {
1558 sosetstate(so, SS_RCVATMARK);
1563 if (offset == so->so_oobmark)
1567 if (flags & MSG_EOR)
1570 * If the MSG_WAITALL flag is set (for non-atomic socket),
1571 * we must not quit until resid == 0 or an error
1572 * termination. If a signal/timeout occurs, return
1573 * with a short count but without error.
1574 * Keep signalsockbuf locked against other readers.
1576 while ((flags & MSG_WAITALL) && m == NULL &&
1577 resid > 0 && !sosendallatonce(so) &&
1578 so->so_rcv.ssb_mb == NULL) {
1579 if (so->so_error || so->so_state & SS_CANTRCVMORE)
1582 * The window might have closed to zero, make
1583 * sure we send an ack now that we've drained
1584 * the buffer or we might end up blocking until
1585 * the idle takes over (5 seconds).
1587 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1588 so_pru_rcvd(so, flags);
1589 error = ssb_wait(&so->so_rcv);
1591 ssb_unlock(&so->so_rcv);
1595 m = so->so_rcv.ssb_mb;
1600 * If an atomic read was requested but unread data still remains
1601 * in the record, set MSG_TRUNC.
1603 if (m && pr->pr_flags & PR_ATOMIC)
1607 * Cleanup. If an atomic read was requested drop any unread data.
1609 if ((flags & MSG_PEEK) == 0) {
1610 if (m && (pr->pr_flags & PR_ATOMIC))
1611 sbdroprecord(&so->so_rcv.sb);
1612 if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
1613 so_pru_rcvd(so, flags);
1616 if (orig_resid == resid && orig_resid &&
1617 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
1618 ssb_unlock(&so->so_rcv);
1625 ssb_unlock(&so->so_rcv);
1627 lwkt_reltoken(&so->so_rcv.ssb_token);
1629 m_freem(free_chain);
1634 sorecvtcp(struct socket *so, struct sockaddr **psa, struct uio *uio,
1635 struct sockbuf *sio, struct mbuf **controlp, int *flagsp)
1638 struct mbuf *free_chain = NULL;
1639 int flags, len, error, offset;
1640 struct protosw *pr = so->so_proto;
1643 size_t resid, orig_resid, restmp;
1646 resid = uio->uio_resid;
1648 resid = (size_t)(sio->sb_climit - sio->sb_cc);
1656 flags = *flagsp &~ MSG_EOR;
1659 if (flags & MSG_OOB) {
1660 m = m_get(MB_WAIT, MT_DATA);
1663 error = so_pru_rcvoob(so, m, flags & MSG_PEEK);
1669 KKASSERT(resid >= (size_t)m->m_len);
1670 resid -= (size_t)m->m_len;
1671 } while (resid > 0 && m);
1674 uio->uio_resid = resid;
1675 error = uiomove(mtod(m, caddr_t),
1676 (int)szmin(resid, m->m_len),
1678 resid = uio->uio_resid;
1680 } while (uio->uio_resid && error == 0 && m);
1689 * The token interlocks against the protocol thread while
1690 * ssb_lock is a blocking lock against other userland entities.
1692 * Lock a limited number of mbufs (not all, so sbcompress() still
1693 * works well). The token is used as an interlock for sbwait() so
1694 * release it afterwords.
1697 error = ssb_lock(&so->so_rcv, SBLOCKWAIT(flags));
1701 lwkt_gettoken(&so->so_rcv.ssb_token);
1702 m = so->so_rcv.ssb_mb;
1705 * If we have less data than requested, block awaiting more
1706 * (subject to any timeout) if:
1707 * 1. the current count is less than the low water mark, or
1708 * 2. MSG_WAITALL is set, and it is possible to do the entire
1709 * receive operation at once if we block (resid <= hiwat).
1710 * 3. MSG_DONTWAIT is not set
1711 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1712 * we have to do the receive in sections, and thus risk returning
1713 * a short count if a timeout or signal occurs after we start.
1715 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1716 (size_t)so->so_rcv.ssb_cc < resid) &&
1717 (so->so_rcv.ssb_cc < so->so_rcv.ssb_lowat ||
1718 ((flags & MSG_WAITALL) && resid <= (size_t)so->so_rcv.ssb_hiwat)))) {
1719 KASSERT(m != NULL || !so->so_rcv.ssb_cc, ("receive 1"));
1723 lwkt_reltoken(&so->so_rcv.ssb_token);
1724 error = so->so_error;
1725 if ((flags & MSG_PEEK) == 0)
1729 if (so->so_state & SS_CANTRCVMORE) {
1732 lwkt_reltoken(&so->so_rcv.ssb_token);
1735 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1736 (pr->pr_flags & PR_CONNREQUIRED)) {
1737 lwkt_reltoken(&so->so_rcv.ssb_token);
1742 lwkt_reltoken(&so->so_rcv.ssb_token);
1745 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
1746 lwkt_reltoken(&so->so_rcv.ssb_token);
1747 error = EWOULDBLOCK;
1750 ssb_unlock(&so->so_rcv);
1751 error = ssb_wait(&so->so_rcv);
1752 lwkt_reltoken(&so->so_rcv.ssb_token);
1764 while (n && restmp < resid) {
1765 n->m_flags |= M_SOLOCKED;
1767 if (n->m_next == NULL)
1774 * Release token for loop
1776 lwkt_reltoken(&so->so_rcv.ssb_token);
1777 if (uio && uio->uio_td && uio->uio_td->td_proc)
1778 uio->uio_td->td_lwp->lwp_ru.ru_msgrcv++;
1781 * note: m should be == sb_mb here. Cache the next record while
1782 * cleaning up. Note that calling m_free*() will break out critical
1785 KKASSERT(m == so->so_rcv.ssb_mb);
1788 * Copy to the UIO or mbuf return chain (*mp).
1790 * NOTE: Token is not held for loop
1796 while (m && (m->m_flags & M_SOLOCKED) && resid > 0 && error == 0) {
1797 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
1800 soclrstate(so, SS_RCVATMARK);
1801 len = (resid > INT_MAX) ? INT_MAX : resid;
1802 if (so->so_oobmark && len > so->so_oobmark - offset)
1803 len = so->so_oobmark - offset;
1804 if (len > m->m_len - moff)
1805 len = m->m_len - moff;
1808 * Copy out to the UIO or pass the mbufs back to the SIO.
1809 * The SIO is dealt with when we eat the mbuf, but deal
1810 * with the resid here either way.
1813 uio->uio_resid = resid;
1814 error = uiomove(mtod(m, caddr_t) + moff, len, uio);
1815 resid = uio->uio_resid;
1819 resid -= (size_t)len;
1823 * Eat the entire mbuf or just a piece of it
1826 if (len == m->m_len - moff) {
1836 if (so->so_oobmark && offset == so->so_oobmark) {
1843 * Synchronize sockbuf with data we read.
1845 * NOTE: (m) is junk on entry (it could be left over from the
1848 if ((flags & MSG_PEEK) == 0) {
1849 lwkt_gettoken(&so->so_rcv.ssb_token);
1850 m = so->so_rcv.ssb_mb;
1851 while (m && offset >= m->m_len) {
1852 if (so->so_oobmark) {
1853 so->so_oobmark -= m->m_len;
1854 if (so->so_oobmark == 0) {
1855 sosetstate(so, SS_RCVATMARK);
1861 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
1865 m = sbunlinkmbuf(&so->so_rcv.sb,
1872 n = m_copym(m, 0, offset, MB_WAIT);
1876 m->m_data += offset;
1878 so->so_rcv.ssb_cc -= offset;
1879 if (so->so_oobmark) {
1880 so->so_oobmark -= offset;
1881 if (so->so_oobmark == 0) {
1882 sosetstate(so, SS_RCVATMARK);
1888 lwkt_reltoken(&so->so_rcv.ssb_token);
1892 * If the MSG_WAITALL flag is set (for non-atomic socket),
1893 * we must not quit until resid == 0 or an error termination.
1895 * If a signal/timeout occurs, return with a short count but without
1898 * Keep signalsockbuf locked against other readers.
1900 * XXX if MSG_PEEK we currently do quit.
1902 if ((flags & MSG_WAITALL) && !(flags & MSG_PEEK) &&
1903 didoob == 0 && resid > 0 &&
1904 !sosendallatonce(so)) {
1905 lwkt_gettoken(&so->so_rcv.ssb_token);
1907 while ((m = so->so_rcv.ssb_mb) == NULL) {
1908 if (so->so_error || (so->so_state & SS_CANTRCVMORE)) {
1909 error = so->so_error;
1913 * The window might have closed to zero, make
1914 * sure we send an ack now that we've drained
1915 * the buffer or we might end up blocking until
1916 * the idle takes over (5 seconds).
1919 so_pru_rcvd_async(so);
1920 if (so->so_rcv.ssb_mb == NULL)
1921 error = ssb_wait(&so->so_rcv);
1923 lwkt_reltoken(&so->so_rcv.ssb_token);
1924 ssb_unlock(&so->so_rcv);
1929 if (m && error == 0)
1931 lwkt_reltoken(&so->so_rcv.ssb_token);
1935 * Token not held here.
1937 * Cleanup. If an atomic read was requested drop any unread data XXX
1939 if ((flags & MSG_PEEK) == 0) {
1941 so_pru_rcvd_async(so);
1944 if (orig_resid == resid && orig_resid &&
1945 (so->so_state & SS_CANTRCVMORE) == 0) {
1946 ssb_unlock(&so->so_rcv);
1953 ssb_unlock(&so->so_rcv);
1956 m_freem(free_chain);
1961 * Shut a socket down. Note that we do not get a frontend lock as we
1962 * want to be able to shut the socket down even if another thread is
1963 * blocked in a read(), thus waking it up.
1966 soshutdown(struct socket *so, int how)
1968 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1971 if (how != SHUT_WR) {
1972 /*ssb_lock(&so->so_rcv, M_WAITOK);*/
1974 /*ssb_unlock(&so->so_rcv);*/
1977 return (so_pru_shutdown(so));
1982 sorflush(struct socket *so)
1984 struct signalsockbuf *ssb = &so->so_rcv;
1985 struct protosw *pr = so->so_proto;
1986 struct signalsockbuf asb;
1988 atomic_set_int(&ssb->ssb_flags, SSB_NOINTR);
1990 lwkt_gettoken(&ssb->ssb_token);
1995 * Can't just blow up the ssb structure here
1997 bzero(&ssb->sb, sizeof(ssb->sb));
2002 atomic_clear_int(&ssb->ssb_flags, SSB_CLEAR_MASK);
2004 if ((pr->pr_flags & PR_RIGHTS) && pr->pr_domain->dom_dispose)
2005 (*pr->pr_domain->dom_dispose)(asb.ssb_mb);
2006 ssb_release(&asb, so);
2008 lwkt_reltoken(&ssb->ssb_token);
2013 do_setopt_accept_filter(struct socket *so, struct sockopt *sopt)
2015 struct accept_filter_arg *afap = NULL;
2016 struct accept_filter *afp;
2017 struct so_accf *af = so->so_accf;
2020 /* do not set/remove accept filters on non listen sockets */
2021 if ((so->so_options & SO_ACCEPTCONN) == 0) {
2026 /* removing the filter */
2029 if (af->so_accept_filter != NULL &&
2030 af->so_accept_filter->accf_destroy != NULL) {
2031 af->so_accept_filter->accf_destroy(so);
2033 if (af->so_accept_filter_str != NULL) {
2034 kfree(af->so_accept_filter_str, M_ACCF);
2039 so->so_options &= ~SO_ACCEPTFILTER;
2042 /* adding a filter */
2043 /* must remove previous filter first */
2048 /* don't put large objects on the kernel stack */
2049 afap = kmalloc(sizeof(*afap), M_TEMP, M_WAITOK);
2050 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
2051 afap->af_name[sizeof(afap->af_name)-1] = '\0';
2052 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
2055 afp = accept_filt_get(afap->af_name);
2060 af = kmalloc(sizeof(*af), M_ACCF, M_WAITOK | M_ZERO);
2061 if (afp->accf_create != NULL) {
2062 if (afap->af_name[0] != '\0') {
2063 int len = strlen(afap->af_name) + 1;
2065 af->so_accept_filter_str = kmalloc(len, M_ACCF,
2067 strcpy(af->so_accept_filter_str, afap->af_name);
2069 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
2070 if (af->so_accept_filter_arg == NULL) {
2071 kfree(af->so_accept_filter_str, M_ACCF);
2078 af->so_accept_filter = afp;
2080 so->so_options |= SO_ACCEPTFILTER;
2083 kfree(afap, M_TEMP);
2089 * Perhaps this routine, and sooptcopyout(), below, ought to come in
2090 * an additional variant to handle the case where the option value needs
2091 * to be some kind of integer, but not a specific size.
2092 * In addition to their use here, these functions are also called by the
2093 * protocol-level pr_ctloutput() routines.
2096 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2098 return soopt_to_kbuf(sopt, buf, len, minlen);
2102 soopt_to_kbuf(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
2106 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
2107 KKASSERT(kva_p(buf));
2110 * If the user gives us more than we wanted, we ignore it,
2111 * but if we don't get the minimum length the caller
2112 * wants, we return EINVAL. On success, sopt->sopt_valsize
2113 * is set to however much we actually retrieved.
2115 if ((valsize = sopt->sopt_valsize) < minlen)
2118 sopt->sopt_valsize = valsize = len;
2120 bcopy(sopt->sopt_val, buf, valsize);
2126 sosetopt(struct socket *so, struct sockopt *sopt)
2132 struct signalsockbuf *sotmp;
2135 sopt->sopt_dir = SOPT_SET;
2136 if (sopt->sopt_level != SOL_SOCKET) {
2137 if (so->so_proto && so->so_proto->pr_ctloutput) {
2138 return (so_pr_ctloutput(so, sopt));
2140 error = ENOPROTOOPT;
2142 switch (sopt->sopt_name) {
2144 case SO_ACCEPTFILTER:
2145 error = do_setopt_accept_filter(so, sopt);
2151 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2155 so->so_linger = l.l_linger;
2157 so->so_options |= SO_LINGER;
2159 so->so_options &= ~SO_LINGER;
2165 case SO_USELOOPBACK:
2172 error = sooptcopyin(sopt, &optval, sizeof optval,
2177 so->so_options |= sopt->sopt_name;
2179 so->so_options &= ~sopt->sopt_name;
2186 error = sooptcopyin(sopt, &optval, sizeof optval,
2192 * Values < 1 make no sense for any of these
2193 * options, so disallow them.
2200 switch (sopt->sopt_name) {
2203 if (ssb_reserve(sopt->sopt_name == SO_SNDBUF ?
2204 &so->so_snd : &so->so_rcv, (u_long)optval,
2206 &curproc->p_rlimit[RLIMIT_SBSIZE]) == 0) {
2210 sotmp = (sopt->sopt_name == SO_SNDBUF) ?
2211 &so->so_snd : &so->so_rcv;
2212 atomic_clear_int(&sotmp->ssb_flags,
2217 * Make sure the low-water is never greater than
2221 so->so_snd.ssb_lowat =
2222 (optval > so->so_snd.ssb_hiwat) ?
2223 so->so_snd.ssb_hiwat : optval;
2224 atomic_clear_int(&so->so_snd.ssb_flags,
2228 so->so_rcv.ssb_lowat =
2229 (optval > so->so_rcv.ssb_hiwat) ?
2230 so->so_rcv.ssb_hiwat : optval;
2231 atomic_clear_int(&so->so_rcv.ssb_flags,
2239 error = sooptcopyin(sopt, &tv, sizeof tv,
2244 /* assert(hz > 0); */
2245 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
2246 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
2250 /* assert(tick > 0); */
2251 /* assert(ULONG_MAX - INT_MAX >= 1000000); */
2252 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / ustick;
2253 if (val > INT_MAX) {
2257 if (val == 0 && tv.tv_usec != 0)
2260 switch (sopt->sopt_name) {
2262 so->so_snd.ssb_timeo = val;
2265 so->so_rcv.ssb_timeo = val;
2270 error = ENOPROTOOPT;
2273 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
2274 (void) so_pr_ctloutput(so, sopt);
2281 /* Helper routine for getsockopt */
2283 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
2285 soopt_from_kbuf(sopt, buf, len);
2290 soopt_from_kbuf(struct sockopt *sopt, const void *buf, size_t len)
2295 sopt->sopt_valsize = 0;
2299 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
2300 KKASSERT(kva_p(buf));
2303 * Documented get behavior is that we always return a value,
2304 * possibly truncated to fit in the user's buffer.
2305 * Traditional behavior is that we always tell the user
2306 * precisely how much we copied, rather than something useful
2307 * like the total amount we had available for her.
2308 * Note that this interface is not idempotent; the entire answer must
2309 * generated ahead of time.
2311 valsize = szmin(len, sopt->sopt_valsize);
2312 sopt->sopt_valsize = valsize;
2313 if (sopt->sopt_val != 0) {
2314 bcopy(buf, sopt->sopt_val, valsize);
2319 sogetopt(struct socket *so, struct sockopt *sopt)
2326 struct accept_filter_arg *afap;
2330 sopt->sopt_dir = SOPT_GET;
2331 if (sopt->sopt_level != SOL_SOCKET) {
2332 if (so->so_proto && so->so_proto->pr_ctloutput) {
2333 return (so_pr_ctloutput(so, sopt));
2335 return (ENOPROTOOPT);
2337 switch (sopt->sopt_name) {
2339 case SO_ACCEPTFILTER:
2340 if ((so->so_options & SO_ACCEPTCONN) == 0)
2342 afap = kmalloc(sizeof(*afap), M_TEMP,
2344 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
2345 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
2346 if (so->so_accf->so_accept_filter_str != NULL)
2347 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
2349 error = sooptcopyout(sopt, afap, sizeof(*afap));
2350 kfree(afap, M_TEMP);
2355 l.l_onoff = so->so_options & SO_LINGER;
2356 l.l_linger = so->so_linger;
2357 error = sooptcopyout(sopt, &l, sizeof l);
2360 case SO_USELOOPBACK:
2370 optval = so->so_options & sopt->sopt_name;
2372 error = sooptcopyout(sopt, &optval, sizeof optval);
2376 optval = so->so_type;
2380 optval = so->so_error;
2385 optval = so->so_snd.ssb_hiwat;
2389 optval = so->so_rcv.ssb_hiwat;
2393 optval = so->so_snd.ssb_lowat;
2397 optval = so->so_rcv.ssb_lowat;
2402 optval = (sopt->sopt_name == SO_SNDTIMEO ?
2403 so->so_snd.ssb_timeo : so->so_rcv.ssb_timeo);
2405 tv.tv_sec = optval / hz;
2406 tv.tv_usec = (optval % hz) * ustick;
2407 error = sooptcopyout(sopt, &tv, sizeof tv);
2411 optval_l = ssb_space(&so->so_snd);
2412 error = sooptcopyout(sopt, &optval_l, sizeof(optval_l));
2416 error = ENOPROTOOPT;
2423 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2425 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2427 struct mbuf *m, *m_prev;
2428 int sopt_size = sopt->sopt_valsize, msize;
2430 m = m_getl(sopt_size, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_DATA,
2434 m->m_len = min(msize, sopt_size);
2435 sopt_size -= m->m_len;
2439 while (sopt_size > 0) {
2440 m = m_getl(sopt_size, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT,
2441 MT_DATA, 0, &msize);
2446 m->m_len = min(msize, sopt_size);
2447 sopt_size -= m->m_len;
2454 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2456 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
2458 soopt_to_mbuf(sopt, m);
2463 soopt_to_mbuf(struct sockopt *sopt, struct mbuf *m)
2468 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
2470 if (sopt->sopt_val == NULL)
2472 val = sopt->sopt_val;
2473 valsize = sopt->sopt_valsize;
2474 while (m != NULL && valsize >= m->m_len) {
2475 bcopy(val, mtod(m, char *), m->m_len);
2476 valsize -= m->m_len;
2477 val = (caddr_t)val + m->m_len;
2480 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2481 panic("ip6_sooptmcopyin");
2484 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2486 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
2488 return soopt_from_mbuf(sopt, m);
2492 soopt_from_mbuf(struct sockopt *sopt, struct mbuf *m)
2494 struct mbuf *m0 = m;
2499 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
2501 if (sopt->sopt_val == NULL)
2503 val = sopt->sopt_val;
2504 maxsize = sopt->sopt_valsize;
2505 while (m != NULL && maxsize >= m->m_len) {
2506 bcopy(mtod(m, char *), val, m->m_len);
2507 maxsize -= m->m_len;
2508 val = (caddr_t)val + m->m_len;
2509 valsize += m->m_len;
2513 /* enough soopt buffer should be given from user-land */
2517 sopt->sopt_valsize = valsize;
2522 sohasoutofband(struct socket *so)
2524 if (so->so_sigio != NULL)
2525 pgsigio(so->so_sigio, SIGURG, 0);
2526 KNOTE(&so->so_rcv.ssb_kq.ki_note, NOTE_OOB);
2530 sokqfilter(struct file *fp, struct knote *kn)
2532 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2533 struct signalsockbuf *ssb;
2535 switch (kn->kn_filter) {
2537 if (so->so_options & SO_ACCEPTCONN)
2538 kn->kn_fop = &solisten_filtops;
2540 kn->kn_fop = &soread_filtops;
2544 kn->kn_fop = &sowrite_filtops;
2548 kn->kn_fop = &soexcept_filtops;
2552 return (EOPNOTSUPP);
2555 knote_insert(&ssb->ssb_kq.ki_note, kn);
2556 atomic_set_int(&ssb->ssb_flags, SSB_KNOTE);
2561 filt_sordetach(struct knote *kn)
2563 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2565 knote_remove(&so->so_rcv.ssb_kq.ki_note, kn);
2566 if (SLIST_EMPTY(&so->so_rcv.ssb_kq.ki_note))
2567 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_KNOTE);
2572 filt_soread(struct knote *kn, long hint)
2574 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2576 if (kn->kn_sfflags & NOTE_OOB) {
2577 if ((so->so_oobmark || (so->so_state & SS_RCVATMARK))) {
2578 kn->kn_fflags |= NOTE_OOB;
2583 kn->kn_data = so->so_rcv.ssb_cc;
2585 if (so->so_state & SS_CANTRCVMORE) {
2587 * Only set NODATA if all data has been exhausted.
2589 if (kn->kn_data == 0)
2590 kn->kn_flags |= EV_NODATA;
2591 kn->kn_flags |= EV_EOF;
2592 kn->kn_fflags = so->so_error;
2595 if (so->so_error) /* temporary udp error */
2597 if (kn->kn_sfflags & NOTE_LOWAT)
2598 return (kn->kn_data >= kn->kn_sdata);
2599 return ((kn->kn_data >= so->so_rcv.ssb_lowat) ||
2600 !TAILQ_EMPTY(&so->so_comp));
2604 filt_sowdetach(struct knote *kn)
2606 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2608 knote_remove(&so->so_snd.ssb_kq.ki_note, kn);
2609 if (SLIST_EMPTY(&so->so_snd.ssb_kq.ki_note))
2610 atomic_clear_int(&so->so_snd.ssb_flags, SSB_KNOTE);
2615 filt_sowrite(struct knote *kn, long hint)
2617 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2619 kn->kn_data = ssb_space(&so->so_snd);
2620 if (so->so_state & SS_CANTSENDMORE) {
2621 kn->kn_flags |= (EV_EOF | EV_NODATA);
2622 kn->kn_fflags = so->so_error;
2625 if (so->so_error) /* temporary udp error */
2627 if (((so->so_state & SS_ISCONNECTED) == 0) &&
2628 (so->so_proto->pr_flags & PR_CONNREQUIRED))
2630 if (kn->kn_sfflags & NOTE_LOWAT)
2631 return (kn->kn_data >= kn->kn_sdata);
2632 return (kn->kn_data >= so->so_snd.ssb_lowat);
2637 filt_solisten(struct knote *kn, long hint)
2639 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2641 kn->kn_data = so->so_qlen;
2642 return (! TAILQ_EMPTY(&so->so_comp));