dsynth - Conditionalize .txz -> other_compression_fmt for pkg repo
[dragonfly.git] / sys / kern / uipc_socket.c
CommitLineData
984263bc 1/*
6ea1e9b9 2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
66d6c637
JH
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
4 *
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
19 *
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
31 * SUCH DAMAGE.
32 */
33
66d6c637 34/*
984263bc
MD
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
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.
dc71b7ab 46 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
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
60 * SUCH DAMAGE.
61 *
62 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
7405c902 63 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.24 2003/11/11 17:18:18 silby Exp $
984263bc
MD
64 */
65
66#include "opt_inet.h"
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/fcntl.h>
71#include <sys/malloc.h>
72#include <sys/mbuf.h>
73#include <sys/domain.h>
74#include <sys/file.h> /* for struct knote */
75#include <sys/kernel.h>
984263bc 76#include <sys/event.h>
984263bc
MD
77#include <sys/proc.h>
78#include <sys/protosw.h>
79#include <sys/socket.h>
80#include <sys/socketvar.h>
6b6e0885 81#include <sys/socketops.h>
984263bc
MD
82#include <sys/resourcevar.h>
83#include <sys/signalvar.h>
84#include <sys/sysctl.h>
85#include <sys/uio.h>
86#include <sys/jail.h>
87#include <vm/vm_zone.h>
e71a125f 88#include <vm/pmap.h>
acd31a69 89#include <net/netmsg2.h>
5337421c 90#include <net/netisr2.h>
984263bc 91
d6cb521d 92#include <sys/socketvar2.h>
96c6eb29 93#include <sys/spinlock2.h>
e43a034f 94
984263bc
MD
95#include <machine/limits.h>
96
4d3eb9a9 97#ifdef INET
0df7608b
SZ
98extern int tcp_sosend_agglim;
99extern int tcp_sosend_async;
ef82c254 100extern int tcp_sosend_jcluster;
0df7608b 101extern int udp_sosend_async;
f5991d1f 102extern int udp_sosend_prepend;
c16aca65 103
984263bc
MD
104static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
105#endif /* INET */
106
107static void filt_sordetach(struct knote *kn);
108static int filt_soread(struct knote *kn, long hint);
109static void filt_sowdetach(struct knote *kn);
110static int filt_sowrite(struct knote *kn, long hint);
111static int filt_solisten(struct knote *kn, long hint);
112
acd31a69
SZ
113static int soclose_sync(struct socket *so, int fflag);
114static void soclose_fast(struct socket *so);
115
984263bc 116static struct filterops solisten_filtops =
8d1b9f93 117 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sordetach, filt_solisten };
984263bc 118static struct filterops soread_filtops =
8d1b9f93 119 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sordetach, filt_soread };
984263bc 120static struct filterops sowrite_filtops =
8d1b9f93 121 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sowdetach, filt_sowrite };
73c344d3 122static struct filterops soexcept_filtops =
8d1b9f93 123 { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_sordetach, filt_soread };
984263bc 124
69ea5b8d 125MALLOC_DEFINE(M_SOCKET, "socket", "socket struct");
984263bc
MD
126MALLOC_DEFINE(M_SONAME, "soname", "socket name");
127MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
128
984263bc
MD
129
130static int somaxconn = SOMAXCONN;
131SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
132 &somaxconn, 0, "Maximum pending socket connection queue size");
133
acd31a69
SZ
134static int use_soclose_fast = 1;
135SYSCTL_INT(_kern_ipc, OID_AUTO, soclose_fast, CTLFLAG_RW,
136 &use_soclose_fast, 0, "Fast socket close");
137
5e4b3994
SZ
138int use_soaccept_pred_fast = 1;
139SYSCTL_INT(_kern_ipc, OID_AUTO, soaccept_pred_fast, CTLFLAG_RW,
140 &use_soaccept_pred_fast, 0, "Fast socket accept predication");
141
f786a6ac 142int use_sendfile_async = 1;
828cf263
SZ
143SYSCTL_INT(_kern_ipc, OID_AUTO, sendfile_async, CTLFLAG_RW,
144 &use_sendfile_async, 0, "sendfile uses asynchronized pru_send");
145
e368a6e9
SZ
146int use_soconnect_async = 1;
147SYSCTL_INT(_kern_ipc, OID_AUTO, soconnect_async, CTLFLAG_RW,
148 &use_soconnect_async, 0, "soconnect uses asynchronized pru_connect");
149
c892825f
SZ
150static int use_socreate_fast = 1;
151SYSCTL_INT(_kern_ipc, OID_AUTO, socreate_fast, CTLFLAG_RW,
152 &use_socreate_fast, 0, "Fast socket creation");
153
65e531c6
SZ
154static int soavailconn = 32;
155SYSCTL_INT(_kern_ipc, OID_AUTO, soavailconn, CTLFLAG_RW,
156 &soavailconn, 0, "Maximum available socket connection queue size");
157
984263bc
MD
158/*
159 * Socket operation routines.
160 * These routines are called by the routines in
161 * sys_socket.c or from a system process, and
162 * implement the semantics of socket operations by
163 * switching out to the protocol specific routines.
164 */
165
166/*
69ea5b8d 167 * Get a socket structure, and initialize it.
984263bc
MD
168 * Note that it would probably be better to allocate socket
169 * and PCB at the same time, but I'm not convinced that all
170 * the protocols can be easily modified to do this.
171 */
172struct socket *
96c6eb29 173soalloc(int waitok, struct protosw *pr)
984263bc 174{
c6e47da6 175 globaldata_t gd = mycpu;
984263bc 176 struct socket *so;
69ea5b8d 177 unsigned waitmask;
984263bc 178
69ea5b8d
NT
179 waitmask = waitok ? M_WAITOK : M_NOWAIT;
180 so = kmalloc(sizeof(struct socket), M_SOCKET, M_ZERO|waitmask);
984263bc
MD
181 if (so) {
182 /* XXX race condition for reentrant kernel */
96c6eb29 183 so->so_proto = pr;
984263bc 184 TAILQ_INIT(&so->so_aiojobq);
20faa324
SZ
185 TAILQ_INIT(&so->so_rcv.ssb_mlist);
186 TAILQ_INIT(&so->so_snd.ssb_mlist);
a3c18566
MD
187 lwkt_token_init(&so->so_rcv.ssb_token, "rcvtok");
188 lwkt_token_init(&so->so_snd.ssb_token, "sndtok");
ba87a4ab 189 spin_init(&so->so_rcvd_spin, "soalloc");
96c6eb29 190 netmsg_init(&so->so_rcvd_msg.base, so, &netisr_adone_rport,
d6b70ca6
SZ
191 MSGF_DROPABLE | MSGF_PRIORITY,
192 so->so_proto->pr_usrreqs->pru_rcvd);
96c6eb29 193 so->so_rcvd_msg.nm_pru_flags |= PRUR_ASYNC;
6cef7136
MD
194 so->so_state = SS_NOFDREF;
195 so->so_refs = 1;
c6e47da6 196 so->so_inum = gd->gd_anoninum++ * ncpus + gd->gd_cpuid + 2;
984263bc
MD
197 }
198 return so;
199}
200
201int
dadab5e9
MD
202socreate(int dom, struct socket **aso, int type,
203 int proto, struct thread *td)
984263bc 204{
dadab5e9
MD
205 struct proc *p = td->td_proc;
206 struct protosw *prp;
207 struct socket *so;
e4700d00 208 struct pru_attach_info ai;
7104f312 209 struct prison *pr = p->p_ucred->cr_prison;
dadab5e9 210 int error;
984263bc
MD
211
212 if (proto)
213 prp = pffindproto(dom, proto, type);
214 else
215 prp = pffindtype(dom, type);
216
4090d6ff 217 if (prp == NULL || prp->pr_usrreqs->pru_attach == 0)
984263bc
MD
218 return (EPROTONOSUPPORT);
219
25e27214 220 if (pr && PRISON_CAP_ISSET(pr->pr_caps, PRISON_CAP_NET_UNIXIPROUTE) &&
984263bc
MD
221 prp->pr_domain->dom_family != PF_LOCAL &&
222 prp->pr_domain->dom_family != PF_INET &&
3e4150ef 223 prp->pr_domain->dom_family != PF_INET6 &&
984263bc
MD
224 prp->pr_domain->dom_family != PF_ROUTE) {
225 return (EPROTONOSUPPORT);
226 }
227
228 if (prp->pr_type != type)
229 return (EPROTOTYPE);
96c6eb29 230 so = soalloc(p != NULL, prp);
6cef7136 231 if (so == NULL)
984263bc
MD
232 return (ENOBUFS);
233
6cef7136
MD
234 /*
235 * Callers of socreate() presumably will connect up a descriptor
236 * and call soclose() if they cannot. This represents our so_refs
237 * (which should be 1) from soalloc().
238 */
239 soclrstate(so, SS_NOFDREF);
240
48e7b118
MD
241 /*
242 * Set a default port for protocol processing. No action will occur
243 * on the socket on this port until an inpcb is attached to it and
244 * is able to match incoming packets, or until the socket becomes
245 * available to userland.
002c1265 246 *
ee0be9ca
SZ
247 * We normally default the socket to the protocol thread on cpu 0,
248 * if protocol does not provide its own method to initialize the
249 * default port.
250 *
002c1265
MD
251 * If PR_SYNC_PORT is set (unix domain sockets) there is no protocol
252 * thread and all pr_*()/pru_*() calls are executed synchronously.
48e7b118 253 */
ee0be9ca 254 if (prp->pr_flags & PR_SYNC_PORT)
002c1265 255 so->so_port = &netisr_sync_port;
ee0be9ca
SZ
256 else if (prp->pr_initport != NULL)
257 so->so_port = prp->pr_initport();
258 else
ec7f7fc8 259 so->so_port = netisr_cpuport(0);
48e7b118 260
984263bc
MD
261 TAILQ_INIT(&so->so_incomp);
262 TAILQ_INIT(&so->so_comp);
263 so->so_type = type;
e9a372eb 264 so->so_cred = crhold(p->p_ucred);
e4700d00
JH
265 ai.sb_rlimit = &p->p_rlimit[RLIMIT_SBSIZE];
266 ai.p_ucred = p->p_ucred;
267 ai.fd_rdir = p->p_fd->fd_rdir;
48e7b118 268
5b0b9fa5
PA
269 /*
270 * Auto-sizing of socket buffers is managed by the protocols and
271 * the appropriate flags must be set in the pru_attach function.
272 */
c892825f
SZ
273 if (use_socreate_fast && prp->pr_usrreqs->pru_preattach)
274 error = so_pru_attach_fast(so, proto, &ai);
275 else
276 error = so_pru_attach(so, proto, &ai);
984263bc 277 if (error) {
6cef7136
MD
278 sosetstate(so, SS_NOFDREF);
279 sofree(so); /* from soalloc */
280 return error;
984263bc 281 }
48e7b118 282
6cef7136
MD
283 /*
284 * NOTE: Returns referenced socket.
285 */
984263bc
MD
286 *aso = so;
287 return (0);
288}
289
290int
dadab5e9 291sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
984263bc 292{
984263bc
MD
293 int error;
294
6b6e0885 295 error = so_pru_bind(so, nam, td);
984263bc
MD
296 return (error);
297}
298
6cef7136 299static void
dadab5e9 300sodealloc(struct socket *so)
984263bc 301{
ee39a18e 302 KKASSERT((so->so_state & (SS_INCOMP | SS_COMP)) == 0);
a6974ef8
SZ
303
304#ifdef INVARIANTS
305 if (so->so_options & SO_ACCEPTCONN) {
306 KASSERT(TAILQ_EMPTY(&so->so_comp), ("so_comp is not empty"));
307 KASSERT(TAILQ_EMPTY(&so->so_incomp),
308 ("so_incomp is not empty"));
309 }
310#endif
1bbcd2ba 311
6d49aa6f 312 if (so->so_rcv.ssb_hiwat)
984263bc 313 (void)chgsbsize(so->so_cred->cr_uidinfo,
6d49aa6f
MD
314 &so->so_rcv.ssb_hiwat, 0, RLIM_INFINITY);
315 if (so->so_snd.ssb_hiwat)
984263bc 316 (void)chgsbsize(so->so_cred->cr_uidinfo,
6d49aa6f 317 &so->so_snd.ssb_hiwat, 0, RLIM_INFINITY);
984263bc 318#ifdef INET
81d59d3d
HP
319 /* remove accept filter if present */
320 if (so->so_accf != NULL)
321 do_setopt_accept_filter(so, NULL);
984263bc
MD
322#endif /* INET */
323 crfree(so->so_cred);
88da6203
SZ
324 if (so->so_faddr != NULL)
325 kfree(so->so_faddr, M_SONAME);
69ea5b8d 326 kfree(so, M_SOCKET);
984263bc
MD
327}
328
329int
dadab5e9 330solisten(struct socket *so, int backlog, struct thread *td)
984263bc 331{
6cef7136 332 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING))
78812139 333 return (EINVAL);
78812139 334
6cef7136 335 lwkt_gettoken(&so->so_rcv.ssb_token);
984263bc
MD
336 if (TAILQ_EMPTY(&so->so_comp))
337 so->so_options |= SO_ACCEPTCONN;
6cef7136 338 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc
MD
339 if (backlog < 0 || backlog > somaxconn)
340 backlog = somaxconn;
341 so->so_qlimit = backlog;
60a260ad 342 return so_pru_listen(so, td);
984263bc
MD
343}
344
1bbcd2ba
SZ
345static void
346soqflush(struct socket *so)
347{
348 lwkt_getpooltoken(so);
349 if (so->so_options & SO_ACCEPTCONN) {
350 struct socket *sp;
351
352 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
353 KKASSERT((sp->so_state & (SS_INCOMP | SS_COMP)) ==
354 SS_INCOMP);
355 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
356 so->so_incqlen--;
357 soclrstate(sp, SS_INCOMP);
358 soabort_async(sp, TRUE);
359 }
360 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
361 KKASSERT((sp->so_state & (SS_INCOMP | SS_COMP)) ==
362 SS_COMP);
363 TAILQ_REMOVE(&so->so_comp, sp, so_list);
364 so->so_qlen--;
365 soclrstate(sp, SS_COMP);
366 soabort_async(sp, TRUE);
367 }
368 }
369 lwkt_relpooltoken(so);
370}
371
4402d8a2
MD
372/*
373 * Destroy a disconnected socket. This routine is a NOP if entities
374 * still have a reference on the socket:
375 *
376 * so_pcb - The protocol stack still has a reference
377 * SS_NOFDREF - There is no longer a file pointer reference
4402d8a2 378 */
984263bc 379void
dadab5e9 380sofree(struct socket *so)
984263bc 381{
5217bcbc
MD
382 struct socket *head;
383
384 /*
385 * This is a bit hackish at the moment. We need to interlock
386 * any accept queue we are on before we potentially lose the
387 * last reference to avoid races against a re-reference from
388 * someone operating on the queue.
389 */
390 while ((head = so->so_head) != NULL) {
391 lwkt_getpooltoken(head);
392 if (so->so_head == head)
393 break;
394 lwkt_relpooltoken(head);
395 }
984263bc 396
6cef7136
MD
397 /*
398 * Arbitrage the last free.
399 */
400 KKASSERT(so->so_refs > 0);
5217bcbc
MD
401 if (atomic_fetchadd_int(&so->so_refs, -1) != 1) {
402 if (head)
403 lwkt_relpooltoken(head);
4402d8a2 404 return;
5217bcbc 405 }
6cef7136
MD
406
407 KKASSERT(so->so_pcb == NULL && (so->so_state & SS_NOFDREF));
e28d8186 408 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
6cef7136 409
984263bc 410 if (head != NULL) {
1bbcd2ba
SZ
411 /*
412 * We're done, remove ourselves from the accept queue we are
413 * on, if we are on one.
414 */
984263bc 415 if (so->so_state & SS_INCOMP) {
ee39a18e
SZ
416 KKASSERT((so->so_state & (SS_INCOMP | SS_COMP)) ==
417 SS_INCOMP);
984263bc
MD
418 TAILQ_REMOVE(&head->so_incomp, so, so_list);
419 head->so_incqlen--;
420 } else if (so->so_state & SS_COMP) {
421 /*
422 * We must not decommission a socket that's
423 * on the accept(2) queue. If we do, then
424 * accept(2) may hang after select(2) indicated
425 * that the listening socket was ready.
426 */
ee39a18e
SZ
427 KKASSERT((so->so_state & (SS_INCOMP | SS_COMP)) ==
428 SS_COMP);
5217bcbc 429 lwkt_relpooltoken(head);
984263bc
MD
430 return;
431 } else {
432 panic("sofree: not queued");
433 }
6cef7136 434 soclrstate(so, SS_INCOMP);
984263bc 435 so->so_head = NULL;
5217bcbc 436 lwkt_relpooltoken(head);
1bbcd2ba
SZ
437 } else {
438 /* Flush accept queues, if we are accepting. */
439 soqflush(so);
984263bc 440 }
6d49aa6f 441 ssb_release(&so->so_snd, so);
984263bc
MD
442 sorflush(so);
443 sodealloc(so);
444}
445
446/*
447 * Close a socket on last file table reference removal.
448 * Initiate disconnect if connected.
449 * Free socket when disconnect complete.
450 */
451int
9ba76b73 452soclose(struct socket *so, int fflag)
984263bc 453{
acd31a69 454 int error;
984263bc 455
58c2553a 456 funsetown(&so->so_sigio);
be4519a2 457 sosetstate(so, SS_ISCLOSING);
acd31a69
SZ
458 if (!use_soclose_fast ||
459 (so->so_proto->pr_flags & PR_SYNC_PORT) ||
de5833ad 460 ((so->so_state & SS_ISCONNECTED) &&
27d2f9e2
SZ
461 (so->so_options & SO_LINGER) &&
462 so->so_linger != 0)) {
acd31a69
SZ
463 error = soclose_sync(so, fflag);
464 } else {
465 soclose_fast(so);
466 error = 0;
467 }
468 return error;
469}
470
be4519a2 471void
acd31a69
SZ
472sodiscard(struct socket *so)
473{
acd31a69
SZ
474 if (so->so_state & SS_NOFDREF)
475 panic("soclose: NOFDREF");
476 sosetstate(so, SS_NOFDREF); /* take ref */
477}
478
838d2682
SZ
479/*
480 * Append the completed queue of head to head_inh (inherting listen socket).
481 */
02ad2f0b 482void
838d2682 483soinherit(struct socket *head, struct socket *head_inh)
02ad2f0b 484{
838d2682 485 boolean_t do_wakeup = FALSE;
02ad2f0b 486
838d2682
SZ
487 KASSERT(head->so_options & SO_ACCEPTCONN,
488 ("head does not accept connection"));
489 KASSERT(head_inh->so_options & SO_ACCEPTCONN,
490 ("head_inh does not accept connection"));
02ad2f0b 491
838d2682
SZ
492 lwkt_getpooltoken(head);
493 lwkt_getpooltoken(head_inh);
02ad2f0b 494
838d2682
SZ
495 if (head->so_qlen > 0)
496 do_wakeup = TRUE;
02ad2f0b 497
838d2682
SZ
498 while (!TAILQ_EMPTY(&head->so_comp)) {
499 struct ucred *old_cr;
500 struct socket *sp;
02ad2f0b 501
838d2682 502 sp = TAILQ_FIRST(&head->so_comp);
ee39a18e 503 KKASSERT((sp->so_state & (SS_INCOMP | SS_COMP)) == SS_COMP);
02ad2f0b 504
838d2682
SZ
505 /*
506 * Remove this socket from the current listen socket
507 * completed queue.
508 */
509 TAILQ_REMOVE(&head->so_comp, sp, so_list);
510 head->so_qlen--;
02ad2f0b 511
838d2682
SZ
512 /* Save the old ucred for later free. */
513 old_cr = sp->so_cred;
514
515 /*
516 * Install this socket to the inheriting listen socket
517 * completed queue.
518 */
519 sp->so_cred = crhold(head_inh->so_cred); /* non-blocking */
520 sp->so_head = head_inh;
02ad2f0b 521
838d2682
SZ
522 TAILQ_INSERT_TAIL(&head_inh->so_comp, sp, so_list);
523 head_inh->so_qlen++;
02ad2f0b 524
838d2682
SZ
525 /*
526 * NOTE:
527 * crfree() may block and release the tokens temporarily.
528 * However, we are fine here, since the transition is done.
529 */
530 crfree(old_cr);
531 }
02ad2f0b 532
838d2682
SZ
533 lwkt_relpooltoken(head_inh);
534 lwkt_relpooltoken(head);
02ad2f0b 535
838d2682 536 if (do_wakeup) {
02ad2f0b
SZ
537 /*
538 * "New" connections have arrived
539 */
838d2682
SZ
540 sorwakeup(head_inh);
541 wakeup(&head_inh->so_timeo);
02ad2f0b
SZ
542 }
543}
544
acd31a69
SZ
545static int
546soclose_sync(struct socket *so, int fflag)
547{
548 int error = 0;
549
f5c5b724
SZ
550 if ((so->so_proto->pr_flags & PR_SYNC_PORT) == 0)
551 so_pru_sync(so); /* unpend async prus */
552
19be7d32 553 if (so->so_pcb == NULL)
984263bc 554 goto discard;
f5c5b724 555
984263bc
MD
556 if (so->so_state & SS_ISCONNECTED) {
557 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
558 error = sodisconnect(so);
559 if (error)
560 goto drop;
561 }
562 if (so->so_options & SO_LINGER) {
563 if ((so->so_state & SS_ISDISCONNECTING) &&
9ba76b73 564 (fflag & FNONBLOCK))
984263bc
MD
565 goto drop;
566 while (so->so_state & SS_ISCONNECTED) {
6cef7136
MD
567 error = tsleep(&so->so_timeo, PCATCH,
568 "soclos", so->so_linger * hz);
984263bc
MD
569 if (error)
570 break;
571 }
572 }
573 }
574drop:
575 if (so->so_pcb) {
6b6e0885
JH
576 int error2;
577
578 error2 = so_pru_detach(so);
be4519a2
SZ
579 if (error2 == EJUSTRETURN) {
580 /*
581 * Protocol will call sodiscard()
582 * and sofree() for us.
583 */
584 return error;
585 }
984263bc
MD
586 if (error == 0)
587 error = error2;
588 }
589discard:
acd31a69 590 sodiscard(so);
acd31a69 591 sofree(so); /* dispose of ref */
19be7d32 592
acd31a69
SZ
593 return (error);
594}
595
596static void
f5c5b724 597soclose_fast_handler(netmsg_t msg)
acd31a69
SZ
598{
599 struct socket *so = msg->base.nm_so;
600
f5c5b724
SZ
601 if (so->so_pcb == NULL)
602 goto discard;
603
acd31a69
SZ
604 if ((so->so_state & SS_ISCONNECTED) &&
605 (so->so_state & SS_ISDISCONNECTING) == 0)
606 so_pru_disconnect_direct(so);
607
be4519a2
SZ
608 if (so->so_pcb) {
609 int error;
610
611 error = so_pru_detach_direct(so);
612 if (error == EJUSTRETURN) {
613 /*
614 * Protocol will call sodiscard()
615 * and sofree() for us.
616 */
617 return;
618 }
619 }
f5c5b724 620discard:
acd31a69
SZ
621 sodiscard(so);
622 sofree(so);
623}
624
625static void
f5c5b724 626soclose_fast(struct socket *so)
acd31a69
SZ
627{
628 struct netmsg_base *base = &so->so_clomsg;
629
630 netmsg_init(base, so, &netisr_apanic_rport, 0,
f5c5b724 631 soclose_fast_handler);
0ebcb559
SZ
632 if (so->so_port == netisr_curport())
633 lwkt_sendmsg_oncpu(so->so_port, &base->lmsg);
634 else
635 lwkt_sendmsg(so->so_port, &base->lmsg);
acd31a69
SZ
636}
637
984263bc 638/*
9116be8e
MD
639 * Abort and destroy a socket. Only one abort can be in progress
640 * at any given moment.
984263bc 641 */
4402d8a2 642void
3735a885 643soabort_async(struct socket *so, boolean_t clr_head)
4402d8a2 644{
3735a885
SZ
645 /*
646 * Keep a reference before clearing the so_head
647 * to avoid racing socket close in netisr.
648 */
6cef7136 649 soreference(so);
3735a885
SZ
650 if (clr_head)
651 so->so_head = NULL;
fd27efb4 652 so_pru_abort_async(so);
984263bc
MD
653}
654
fd86a41c 655void
94aba184 656soabort_direct(struct socket *so)
fd86a41c 657{
6cef7136 658 soreference(so);
2deaa561 659 so_pru_abort_direct(so);
fd86a41c
SZ
660}
661
c19fdb0e
MD
662/*
663 * so is passed in ref'd, which becomes owned by
664 * the cleared SS_NOFDREF flag.
665 */
88da6203
SZ
666void
667soaccept_generic(struct socket *so)
668{
669 if ((so->so_state & SS_NOFDREF) == 0)
670 panic("soaccept: !NOFDREF");
671 soclrstate(so, SS_NOFDREF); /* owned by lack of SS_NOFDREF */
672}
673
984263bc 674int
dadab5e9 675soaccept(struct socket *so, struct sockaddr **nam)
984263bc 676{
984263bc
MD
677 int error;
678
88da6203 679 soaccept_generic(so);
f328adf5 680 error = so_pru_accept(so, nam);
984263bc
MD
681 return (error);
682}
683
684int
e368a6e9
SZ
685soconnect(struct socket *so, struct sockaddr *nam, struct thread *td,
686 boolean_t sync)
984263bc 687{
984263bc
MD
688 int error;
689
690 if (so->so_options & SO_ACCEPTCONN)
691 return (EOPNOTSUPP);
984263bc
MD
692 /*
693 * If protocol is connection-based, can only connect once.
694 * Otherwise, if connected, try to disconnect first.
695 * This allows user to disconnect by connecting to, e.g.,
696 * a null address.
697 */
698 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
699 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
59429d28 700 (error = sodisconnect(so)))) {
984263bc 701 error = EISCONN;
59429d28
MD
702 } else {
703 /*
704 * Prevent accumulated error from previous connection
705 * from biting us.
706 */
707 so->so_error = 0;
e368a6e9
SZ
708 if (!sync && so->so_proto->pr_usrreqs->pru_preconnect)
709 error = so_pru_connect_async(so, nam, td);
710 else
711 error = so_pru_connect(so, nam, td);
59429d28 712 }
984263bc
MD
713 return (error);
714}
715
716int
dadab5e9 717soconnect2(struct socket *so1, struct socket *so2)
984263bc 718{
984263bc
MD
719 int error;
720
6b6e0885 721 error = so_pru_connect2(so1, so2);
984263bc
MD
722 return (error);
723}
724
725int
dadab5e9 726sodisconnect(struct socket *so)
984263bc 727{
984263bc
MD
728 int error;
729
730 if ((so->so_state & SS_ISCONNECTED) == 0) {
731 error = ENOTCONN;
732 goto bad;
733 }
734 if (so->so_state & SS_ISDISCONNECTING) {
735 error = EALREADY;
736 goto bad;
737 }
6b6e0885 738 error = so_pru_disconnect(so);
984263bc 739bad:
984263bc
MD
740 return (error);
741}
742
743#define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
744/*
745 * Send on a socket.
746 * If send must go all at once and message is larger than
747 * send buffering, then hard error.
748 * Lock against other senders.
749 * If must go all at once and not enough room now, then
750 * inform user that this would block and do nothing.
751 * Otherwise, if nonblocking, send as much as possible.
752 * The data to be sent is described by "uio" if nonzero,
753 * otherwise by the mbuf chain "top" (which must be null
754 * if uio is not). Data provided in mbuf chain must be small
755 * enough to send all at once.
756 *
757 * Returns nonzero on error, timeout or signal; callers
758 * must check for short counts if EINTR/ERESTART are returned.
759 * Data and control buffers are freed on return.
760 */
761int
dadab5e9
MD
762sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
763 struct mbuf *top, struct mbuf *control, int flags,
764 struct thread *td)
984263bc
MD
765{
766 struct mbuf **mp;
dadab5e9 767 struct mbuf *m;
e54488bb
MD
768 size_t resid;
769 int space, len;
e43a034f 770 int clen = 0, error, dontroute, mlen;
984263bc 771 int atomic = sosendallatonce(so) || top;
6b6e0885 772 int pru_flags;
984263bc 773
5bd48c1d 774 if (uio) {
984263bc 775 resid = uio->uio_resid;
5bd48c1d 776 } else {
e54488bb 777 resid = (size_t)top->m_pkthdr.len;
5bd48c1d
MD
778#ifdef INVARIANTS
779 len = 0;
780 for (m = top; m; m = m->m_next)
781 len += m->m_len;
782 KKASSERT(top->m_pkthdr.len == len);
783#endif
784 }
48e7b118 785
984263bc 786 /*
e54488bb
MD
787 * WARNING! resid is unsigned, space and len are signed. space
788 * can wind up negative if the sockbuf is overcommitted.
984263bc
MD
789 *
790 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
791 * type sockets since that's an error.
792 */
e54488bb 793 if (so->so_type == SOCK_STREAM && (flags & MSG_EOR)) {
984263bc
MD
794 error = EINVAL;
795 goto out;
796 }
797
798 dontroute =
799 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
800 (so->so_proto->pr_flags & PR_ATOMIC);
fde7ac71
SS
801 if (td->td_lwp != NULL)
802 td->td_lwp->lwp_ru.ru_msgsnd++;
984263bc
MD
803 if (control)
804 clen = control->m_len;
6cef7136 805#define gotoerr(errcode) { error = errcode; goto release; }
984263bc
MD
806
807restart:
6d49aa6f 808 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
984263bc
MD
809 if (error)
810 goto out;
48e7b118 811
984263bc 812 do {
984263bc 813 if (so->so_state & SS_CANTSENDMORE)
6ea1e9b9 814 gotoerr(EPIPE);
984263bc
MD
815 if (so->so_error) {
816 error = so->so_error;
817 so->so_error = 0;
984263bc
MD
818 goto release;
819 }
820 if ((so->so_state & SS_ISCONNECTED) == 0) {
821 /*
822 * `sendto' and `sendmsg' is allowed on a connection-
823 * based socket if it supports implied connect.
824 * Return ENOTCONN if not connected and no address is
825 * supplied.
826 */
827 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
828 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
829 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
830 !(resid == 0 && clen != 0))
6ea1e9b9 831 gotoerr(ENOTCONN);
4090d6ff 832 } else if (addr == NULL)
6ea1e9b9 833 gotoerr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
984263bc
MD
834 ENOTCONN : EDESTADDRREQ);
835 }
3a6117bb
MD
836 if ((atomic && resid > so->so_snd.ssb_hiwat) ||
837 clen > so->so_snd.ssb_hiwat) {
838 gotoerr(EMSGSIZE);
839 }
6d49aa6f 840 space = ssb_space(&so->so_snd);
984263bc
MD
841 if (flags & MSG_OOB)
842 space += 1024;
e54488bb 843 if ((space < 0 || (size_t)space < resid + clen) && uio &&
6d49aa6f 844 (atomic || space < so->so_snd.ssb_lowat || space < clen)) {
9ba76b73 845 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
6ea1e9b9 846 gotoerr(EWOULDBLOCK);
6d49aa6f
MD
847 ssb_unlock(&so->so_snd);
848 error = ssb_wait(&so->so_snd);
984263bc
MD
849 if (error)
850 goto out;
851 goto restart;
852 }
984263bc
MD
853 mp = &top;
854 space -= clen;
855 do {
856 if (uio == NULL) {
857 /*
858 * Data is prepackaged in "top".
859 */
860 resid = 0;
861 if (flags & MSG_EOR)
862 top->m_flags |= M_EOR;
863 } else do {
e54488bb
MD
864 if (resid > INT_MAX)
865 resid = INT_MAX;
b5523eac 866 m = m_getl((int)resid, M_WAITOK, MT_DATA,
50503f0f
JH
867 top == NULL ? M_PKTHDR : 0, &mlen);
868 if (top == NULL) {
984263bc 869 m->m_pkthdr.len = 0;
60233e58 870 m->m_pkthdr.rcvif = NULL;
984263bc 871 }
e54488bb 872 len = imin((int)szmin(mlen, resid), space);
50503f0f 873 if (resid < MINCLSIZE) {
984263bc
MD
874 /*
875 * For datagram protocols, leave room
876 * for protocol headers in first mbuf.
877 */
4090d6ff 878 if (atomic && top == NULL && len < mlen)
984263bc
MD
879 MH_ALIGN(m, len);
880 }
881 space -= len;
e54488bb 882 error = uiomove(mtod(m, caddr_t), (size_t)len, uio);
984263bc
MD
883 resid = uio->uio_resid;
884 m->m_len = len;
885 *mp = m;
886 top->m_pkthdr.len += len;
887 if (error)
888 goto release;
889 mp = &m->m_next;
e54488bb 890 if (resid == 0) {
984263bc
MD
891 if (flags & MSG_EOR)
892 top->m_flags |= M_EOR;
893 break;
894 }
895 } while (space > 0 && atomic);
896 if (dontroute)
897 so->so_options |= SO_DONTROUTE;
6b6e0885
JH
898 if (flags & MSG_OOB) {
899 pru_flags = PRUS_OOB;
900 } else if ((flags & MSG_EOF) &&
901 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
e54488bb 902 (resid == 0)) {
6b6e0885
JH
903 /*
904 * If the user set MSG_EOF, the protocol
905 * understands this flag and nothing left to
906 * send then use PRU_SEND_EOF instead of PRU_SEND.
907 */
908 pru_flags = PRUS_EOF;
909 } else if (resid > 0 && space > 0) {
910 /* If there is more to send, set PRUS_MORETOCOME */
911 pru_flags = PRUS_MORETOCOME;
912 } else {
913 pru_flags = 0;
914 }
984263bc
MD
915 /*
916 * XXX all the SS_CANTSENDMORE checks previously
917 * done could be out of date. We could have recieved
918 * a reset packet in an interrupt or maybe we slept
919 * while doing page faults in uiomove() etc. We could
920 * probably recheck again inside the splnet() protection
921 * here, but there are probably other places that this
922 * also happens. We must rethink this.
923 */
6b6e0885 924 error = so_pru_send(so, pru_flags, top, addr, control, td);
984263bc
MD
925 if (dontroute)
926 so->so_options &= ~SO_DONTROUTE;
927 clen = 0;
4090d6ff 928 control = NULL;
e28d8186 929 top = NULL;
984263bc
MD
930 mp = &top;
931 if (error)
6b6e0885 932 goto release;
984263bc
MD
933 } while (resid && space > 0);
934 } while (resid);
935
936release:
6d49aa6f 937 ssb_unlock(&so->so_snd);
984263bc
MD
938out:
939 if (top)
940 m_freem(top);
941 if (control)
942 m_freem(control);
943 return (error);
944}
945
a6ae3d3b 946#ifdef INET
6ea1e9b9
JH
947/*
948 * A specialization of sosend() for UDP based on protocol-specific knowledge:
949 * so->so_proto->pr_flags has the PR_ATOMIC field set. This means that
950 * sosendallatonce() returns true,
951 * the "atomic" variable is true,
952 * and sosendudp() blocks until space is available for the entire send.
953 * so->so_proto->pr_flags does not have the PR_CONNREQUIRED or
954 * PR_IMPLOPCL flags set.
955 * UDP has no out-of-band data.
956 * UDP has no control data.
957 * UDP does not support MSG_EOR.
958 */
959int
960sosendudp(struct socket *so, struct sockaddr *addr, struct uio *uio,
961 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
962{
e54488bb 963 size_t resid;
47d61296 964 int error, pru_flags = 0;
e54488bb 965 int space;
6ea1e9b9 966
fde7ac71
SS
967 if (td->td_lwp != NULL)
968 td->td_lwp->lwp_ru.ru_msgsnd++;
6ea1e9b9
JH
969 if (control)
970 m_freem(control);
971
972 KASSERT((uio && !top) || (top && !uio), ("bad arguments to sosendudp"));
e54488bb 973 resid = uio ? uio->uio_resid : (size_t)top->m_pkthdr.len;
6ea1e9b9
JH
974
975restart:
6d49aa6f 976 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
6ea1e9b9
JH
977 if (error)
978 goto out;
979
6ea1e9b9
JH
980 if (so->so_state & SS_CANTSENDMORE)
981 gotoerr(EPIPE);
982 if (so->so_error) {
983 error = so->so_error;
984 so->so_error = 0;
6ea1e9b9
JH
985 goto release;
986 }
987 if (!(so->so_state & SS_ISCONNECTED) && addr == NULL)
988 gotoerr(EDESTADDRREQ);
6d49aa6f 989 if (resid > so->so_snd.ssb_hiwat)
6ea1e9b9 990 gotoerr(EMSGSIZE);
e54488bb
MD
991 space = ssb_space(&so->so_snd);
992 if (uio && (space < 0 || (size_t)space < resid)) {
9ba76b73 993 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
6ea1e9b9 994 gotoerr(EWOULDBLOCK);
6d49aa6f
MD
995 ssb_unlock(&so->so_snd);
996 error = ssb_wait(&so->so_snd);
6ea1e9b9
JH
997 if (error)
998 goto out;
999 goto restart;
1000 }
6ea1e9b9
JH
1001
1002 if (uio) {
f5991d1f
SZ
1003 int hdrlen = max_hdr;
1004
1005 /*
1006 * We try to optimize out the additional mbuf
1007 * allocations in M_PREPEND() on output path, e.g.
1008 * - udp_output(), when it tries to prepend protocol
1009 * headers.
1010 * - Link layer output function, when it tries to
1011 * prepend link layer header.
1012 *
1013 * This probably will not benefit any data that will
1014 * be fragmented, so this optimization is only performed
1015 * when the size of data and max size of protocol+link
1016 * headers fit into one mbuf cluster.
1017 */
1018 if (uio->uio_resid > MCLBYTES - hdrlen ||
1019 !udp_sosend_prepend) {
1020 top = m_uiomove(uio);
1021 if (top == NULL)
1022 goto release;
1023 } else {
1024 int nsize;
1025
b5523eac 1026 top = m_getl(uio->uio_resid + hdrlen, M_WAITOK,
f5991d1f
SZ
1027 MT_DATA, M_PKTHDR, &nsize);
1028 KASSERT(nsize >= uio->uio_resid + hdrlen,
1029 ("sosendudp invalid nsize %d, "
1030 "resid %zu, hdrlen %d",
1031 nsize, uio->uio_resid, hdrlen));
1032
1033 top->m_len = uio->uio_resid;
1034 top->m_pkthdr.len = uio->uio_resid;
1035 top->m_data += hdrlen;
1036
1037 error = uiomove(mtod(top, caddr_t), top->m_len, uio);
1038 if (error)
1039 goto out;
1040 }
6ea1e9b9
JH
1041 }
1042
47d61296
SZ
1043 if (flags & MSG_DONTROUTE)
1044 pru_flags |= PRUS_DONTROUTE;
6ea1e9b9 1045
0df7608b 1046 if (udp_sosend_async && (flags & MSG_SYNC) == 0) {
6ce03112
SZ
1047 so_pru_send_async(so, pru_flags, top, addr, NULL, td);
1048 error = 0;
1049 } else {
1050 error = so_pru_send(so, pru_flags, top, addr, NULL, td);
1051 }
6ea1e9b9
JH
1052 top = NULL; /* sent or freed in lower layer */
1053
6ea1e9b9 1054release:
6d49aa6f 1055 ssb_unlock(&so->so_snd);
6ea1e9b9
JH
1056out:
1057 if (top)
1058 m_freem(top);
1059 return (error);
1060}
1061
5bc42dd1
SZ
1062int
1063sosendtcp(struct socket *so, struct sockaddr *addr, struct uio *uio,
1064 struct mbuf *top, struct mbuf *control, int flags,
1065 struct thread *td)
1066{
1067 struct mbuf **mp;
1068 struct mbuf *m;
1069 size_t resid;
1070 int space, len;
1071 int error, mlen;
1072 int allatonce;
1073 int pru_flags;
1074
1075 if (uio) {
1076 KKASSERT(top == NULL);
1077 allatonce = 0;
1078 resid = uio->uio_resid;
1079 } else {
1080 allatonce = 1;
1081 resid = (size_t)top->m_pkthdr.len;
1082#ifdef INVARIANTS
1083 len = 0;
1084 for (m = top; m; m = m->m_next)
1085 len += m->m_len;
1086 KKASSERT(top->m_pkthdr.len == len);
1087#endif
1088 }
1089
1090 /*
1091 * WARNING! resid is unsigned, space and len are signed. space
1092 * can wind up negative if the sockbuf is overcommitted.
1093 *
1094 * Also check to make sure that MSG_EOR isn't used on TCP
1095 */
1096 if (flags & MSG_EOR) {
1097 error = EINVAL;
1098 goto out;
1099 }
1100
1101 if (control) {
1102 /* TCP doesn't do control messages (rights, creds, etc) */
1103 if (control->m_len) {
1104 error = EINVAL;
1105 goto out;
1106 }
1107 m_freem(control); /* empty control, just free it */
1108 control = NULL;
1109 }
1110
1111 if (td->td_lwp != NULL)
1112 td->td_lwp->lwp_ru.ru_msgsnd++;
1113
1114#define gotoerr(errcode) { error = errcode; goto release; }
1115
1116restart:
1117 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
1118 if (error)
1119 goto out;
1120
1121 do {
1122 if (so->so_state & SS_CANTSENDMORE)
1123 gotoerr(EPIPE);
1124 if (so->so_error) {
1125 error = so->so_error;
1126 so->so_error = 0;
1127 goto release;
1128 }
1129 if ((so->so_state & SS_ISCONNECTED) == 0 &&
1130 (so->so_state & SS_ISCONFIRMING) == 0)
1131 gotoerr(ENOTCONN);
1132 if (allatonce && resid > so->so_snd.ssb_hiwat)
1133 gotoerr(EMSGSIZE);
1134
f786a6ac 1135 space = ssb_space_prealloc(&so->so_snd);
5bc42dd1
SZ
1136 if (flags & MSG_OOB)
1137 space += 1024;
1138 if ((space < 0 || (size_t)space < resid) && !allatonce &&
1139 space < so->so_snd.ssb_lowat) {
1140 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
1141 gotoerr(EWOULDBLOCK);
1142 ssb_unlock(&so->so_snd);
1143 error = ssb_wait(&so->so_snd);
1144 if (error)
1145 goto out;
1146 goto restart;
1147 }
1148 mp = &top;
1149 do {
f2a3782e 1150 int cnt = 0, async = 0;
c16aca65 1151
5bc42dd1
SZ
1152 if (uio == NULL) {
1153 /*
1154 * Data is prepackaged in "top".
1155 */
1156 resid = 0;
1157 } else do {
1158 if (resid > INT_MAX)
1159 resid = INT_MAX;
ef82c254 1160 if (tcp_sosend_jcluster) {
b5523eac 1161 m = m_getlj((int)resid, M_WAITOK, MT_DATA,
ef82c254
SZ
1162 top == NULL ? M_PKTHDR : 0, &mlen);
1163 } else {
b5523eac 1164 m = m_getl((int)resid, M_WAITOK, MT_DATA,
ef82c254
SZ
1165 top == NULL ? M_PKTHDR : 0, &mlen);
1166 }
5bc42dd1
SZ
1167 if (top == NULL) {
1168 m->m_pkthdr.len = 0;
1169 m->m_pkthdr.rcvif = NULL;
1170 }
1171 len = imin((int)szmin(mlen, resid), space);
1172 space -= len;
1173 error = uiomove(mtod(m, caddr_t), (size_t)len, uio);
1174 resid = uio->uio_resid;
1175 m->m_len = len;
1176 *mp = m;
1177 top->m_pkthdr.len += len;
1178 if (error)
1179 goto release;
1180 mp = &m->m_next;
1181 if (resid == 0)
1182 break;
c16aca65 1183 ++cnt;
0df7608b 1184 } while (space > 0 && cnt < tcp_sosend_agglim);
5bc42dd1 1185
0df7608b 1186 if (tcp_sosend_async)
6d618102
SZ
1187 async = 1;
1188
5bc42dd1
SZ
1189 if (flags & MSG_OOB) {
1190 pru_flags = PRUS_OOB;
6d618102
SZ
1191 async = 0;
1192 } else if ((flags & MSG_EOF) && resid == 0) {
1193 pru_flags = PRUS_EOF;
5bc42dd1
SZ
1194 } else if (resid > 0 && space > 0) {
1195 /* If there is more to send, set PRUS_MORETOCOME */
1196 pru_flags = PRUS_MORETOCOME;
f2a3782e 1197 async = 1;
5bc42dd1
SZ
1198 } else {
1199 pru_flags = 0;
1200 }
1201
1af30d61 1202 if (flags & MSG_SYNC)
6d618102 1203 async = 0;
1af30d61 1204
5bc42dd1
SZ
1205 /*
1206 * XXX all the SS_CANTSENDMORE checks previously
1207 * done could be out of date. We could have recieved
1208 * a reset packet in an interrupt or maybe we slept
1209 * while doing page faults in uiomove() etc. We could
1210 * probably recheck again inside the splnet() protection
1211 * here, but there are probably other places that this
1212 * also happens. We must rethink this.
1213 */
f786a6ac
SZ
1214 for (m = top; m; m = m->m_next)
1215 ssb_preallocstream(&so->so_snd, m);
f2a3782e 1216 if (!async) {
0ad8e15e
SZ
1217 error = so_pru_send(so, pru_flags, top,
1218 NULL, NULL, td);
1219 } else {
1220 so_pru_send_async(so, pru_flags, top,
1221 NULL, NULL, td);
1222 error = 0;
1223 }
5bc42dd1
SZ
1224
1225 top = NULL;
1226 mp = &top;
1227 if (error)
1228 goto release;
1229 } while (resid && space > 0);
1230 } while (resid);
1231
1232release:
1233 ssb_unlock(&so->so_snd);
1234out:
1235 if (top)
1236 m_freem(top);
1237 if (control)
1238 m_freem(control);
1239 return (error);
1240}
a6ae3d3b 1241#endif
5bc42dd1 1242
984263bc
MD
1243/*
1244 * Implement receive operations on a socket.
6cef7136 1245 *
6d49aa6f 1246 * We depend on the way that records are added to the signalsockbuf
984263bc
MD
1247 * by sbappend*. In particular, each record (mbufs linked through m_next)
1248 * must begin with an address if the protocol so specifies,
1249 * followed by an optional mbuf or mbufs containing ancillary data,
1250 * and then zero or more mbufs of data.
6cef7136
MD
1251 *
1252 * Although the signalsockbuf is locked, new data may still be appended.
1253 * A token inside the ssb_lock deals with MP issues and still allows
1254 * the network to access the socket if we block in a uio.
984263bc
MD
1255 *
1256 * The caller may receive the data as a single mbuf chain by supplying
1257 * an mbuf **mp0 for use in returning the chain. The uio is then used
1258 * only for the count in uio_resid.
1259 */
1260int
c972a82f 1261soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
6d49aa6f 1262 struct sockbuf *sio, struct mbuf **controlp, int *flagsp)
984263bc 1263{
d8a9a23b 1264 struct mbuf *m, *n;
857caa4a 1265 struct mbuf *free_chain = NULL;
e43a034f 1266 int flags, len, error, offset;
984263bc 1267 struct protosw *pr = so->so_proto;
984263bc 1268 int moff, type = 0;
e54488bb 1269 size_t resid, orig_resid;
e62cfe62 1270 boolean_t free_rights = FALSE;
d8a9a23b
MD
1271
1272 if (uio)
1273 resid = uio->uio_resid;
1274 else
e54488bb 1275 resid = (size_t)(sio->sb_climit - sio->sb_cc);
d8a9a23b 1276 orig_resid = resid;
984263bc 1277
9510c423
SZ
1278 if (psa)
1279 *psa = NULL;
1280 if (controlp)
1281 *controlp = NULL;
1282 if (flagsp)
1283 flags = *flagsp &~ MSG_EOR;
1284 else
1285 flags = 0;
1286 if (flags & MSG_OOB) {
b5523eac 1287 m = m_get(M_WAITOK, MT_DATA);
9510c423
SZ
1288 if (m == NULL)
1289 return (ENOBUFS);
1290 error = so_pru_rcvoob(so, m, flags & MSG_PEEK);
1291 if (error)
1292 goto bad;
1293 if (sio) {
1294 do {
1295 sbappend(sio, m);
1296 KKASSERT(resid >= (size_t)m->m_len);
1297 resid -= (size_t)m->m_len;
1298 } while (resid > 0 && m);
1299 } else {
1300 do {
1301 uio->uio_resid = resid;
1302 error = uiomove(mtod(m, caddr_t),
1303 (int)szmin(resid, m->m_len),
1304 uio);
1305 resid = uio->uio_resid;
1306 m = m_free(m);
1307 } while (uio->uio_resid && error == 0 && m);
1308 }
1309bad:
1310 if (m)
1311 m_freem(m);
1312 return (error);
1313 }
1314 if ((so->so_state & SS_ISCONFIRMING) && resid)
1315 so_pru_rcvd(so, 0);
1316
1317 /*
1318 * The token interlocks against the protocol thread while
1319 * ssb_lock is a blocking lock against other userland entities.
1320 */
1321 lwkt_gettoken(&so->so_rcv.ssb_token);
1322restart:
1323 error = ssb_lock(&so->so_rcv, SBLOCKWAIT(flags));
1324 if (error)
1325 goto done;
1326
1327 m = so->so_rcv.ssb_mb;
1328 /*
1329 * If we have less data than requested, block awaiting more
1330 * (subject to any timeout) if:
1331 * 1. the current count is less than the low water mark, or
1332 * 2. MSG_WAITALL is set, and it is possible to do the entire
1333 * receive operation at once if we block (resid <= hiwat).
1334 * 3. MSG_DONTWAIT is not set
1335 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1336 * we have to do the receive in sections, and thus risk returning
1337 * a short count if a timeout or signal occurs after we start.
1338 */
1339 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
1340 (size_t)so->so_rcv.ssb_cc < resid) &&
1341 (so->so_rcv.ssb_cc < so->so_rcv.ssb_lowat ||
1342 ((flags & MSG_WAITALL) && resid <= (size_t)so->so_rcv.ssb_hiwat)) &&
1343 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
1344 KASSERT(m != NULL || !so->so_rcv.ssb_cc, ("receive 1"));
7eaeff3d 1345 if (so->so_error || so->so_rerror) {
9510c423
SZ
1346 if (m)
1347 goto dontblock;
7eaeff3d
RM
1348 if (so->so_error)
1349 error = so->so_error;
1350 else
1351 error = so->so_rerror;
1352 if ((flags & MSG_PEEK) == 0) {
1353 if (so->so_error)
1354 so->so_error = 0;
1355 else
1356 so->so_rerror = 0;
1357 }
9510c423
SZ
1358 goto release;
1359 }
1360 if (so->so_state & SS_CANTRCVMORE) {
1361 if (m)
1362 goto dontblock;
1363 else
1364 goto release;
1365 }
1366 for (; m; m = m->m_next) {
1367 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
1368 m = so->so_rcv.ssb_mb;
1369 goto dontblock;
1370 }
1371 }
1372 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
1373 (pr->pr_flags & PR_CONNREQUIRED)) {
1374 error = ENOTCONN;
1375 goto release;
1376 }
1377 if (resid == 0)
1378 goto release;
1379 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
1380 error = EWOULDBLOCK;
1381 goto release;
1382 }
1383 ssb_unlock(&so->so_rcv);
1384 error = ssb_wait(&so->so_rcv);
1385 if (error)
1386 goto done;
1387 goto restart;
1388 }
1389dontblock:
1390 if (uio && uio->uio_td && uio->uio_td->td_proc)
1391 uio->uio_td->td_lwp->lwp_ru.ru_msgrcv++;
1392
1393 /*
1394 * note: m should be == sb_mb here. Cache the next record while
1395 * cleaning up. Note that calling m_free*() will break out critical
1396 * section.
1397 */
1398 KKASSERT(m == so->so_rcv.ssb_mb);
1399
1400 /*
1401 * Skip any address mbufs prepending the record.
1402 */
1403 if (pr->pr_flags & PR_ADDR) {
1404 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
1405 orig_resid = 0;
1406 if (psa)
1407 *psa = dup_sockaddr(mtod(m, struct sockaddr *));
1408 if (flags & MSG_PEEK)
1409 m = m->m_next;
1410 else
1411 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1412 }
1413
1414 /*
1415 * Skip any control mbufs prepending the record.
1416 */
9510c423
SZ
1417 while (m && m->m_type == MT_CONTROL && error == 0) {
1418 if (flags & MSG_PEEK) {
1419 if (controlp)
1420 *controlp = m_copy(m, 0, m->m_len);
1421 m = m->m_next; /* XXX race */
1422 } else {
e62cfe62
SZ
1423 const struct cmsghdr *cm = mtod(m, struct cmsghdr *);
1424
9510c423
SZ
1425 if (controlp) {
1426 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
1427 if (pr->pr_domain->dom_externalize &&
e62cfe62
SZ
1428 cm->cmsg_level == SOL_SOCKET &&
1429 cm->cmsg_type == SCM_RIGHTS) {
1430 error = pr->pr_domain->dom_externalize
1431 (m, flags);
1432 }
9510c423
SZ
1433 *controlp = m;
1434 m = n;
1435 } else {
e62cfe62
SZ
1436 if (cm->cmsg_level == SOL_SOCKET &&
1437 cm->cmsg_type == SCM_RIGHTS)
1438 free_rights = TRUE;
9510c423
SZ
1439 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1440 }
1441 }
1442 if (controlp && *controlp) {
1443 orig_resid = 0;
1444 controlp = &(*controlp)->m_next;
1445 }
1446 }
1447
1448 /*
1449 * flag OOB data.
1450 */
1451 if (m) {
1452 type = m->m_type;
1453 if (type == MT_OOBDATA)
1454 flags |= MSG_OOB;
1455 }
1456
1457 /*
1458 * Copy to the UIO or mbuf return chain (*mp).
1459 */
1460 moff = 0;
1461 offset = 0;
1462 while (m && resid > 0 && error == 0) {
1463 if (m->m_type == MT_OOBDATA) {
1464 if (type != MT_OOBDATA)
1465 break;
1466 } else if (type == MT_OOBDATA)
1467 break;
1468 else
1469 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
1470 ("receive 3"));
1471 soclrstate(so, SS_RCVATMARK);
1472 len = (resid > INT_MAX) ? INT_MAX : resid;
1473 if (so->so_oobmark && len > so->so_oobmark - offset)
1474 len = so->so_oobmark - offset;
1475 if (len > m->m_len - moff)
1476 len = m->m_len - moff;
1477
1478 /*
1479 * Copy out to the UIO or pass the mbufs back to the SIO.
1480 * The SIO is dealt with when we eat the mbuf, but deal
1481 * with the resid here either way.
1482 */
1483 if (uio) {
1484 uio->uio_resid = resid;
1485 error = uiomove(mtod(m, caddr_t) + moff, len, uio);
1486 resid = uio->uio_resid;
1487 if (error)
1488 goto release;
1489 } else {
1490 resid -= (size_t)len;
1491 }
1492
1493 /*
1494 * Eat the entire mbuf or just a piece of it
1495 */
1496 if (len == m->m_len - moff) {
1497 if (m->m_flags & M_EOR)
1498 flags |= MSG_EOR;
9510c423
SZ
1499 if (flags & MSG_PEEK) {
1500 m = m->m_next;
1501 moff = 0;
1502 } else {
1503 if (sio) {
1504 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
1505 sbappend(sio, m);
1506 m = n;
1507 } else {
1508 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1509 }
1510 }
1511 } else {
1512 if (flags & MSG_PEEK) {
1513 moff += len;
1514 } else {
1515 if (sio) {
b5523eac 1516 n = m_copym(m, 0, len, M_WAITOK);
9510c423
SZ
1517 if (n)
1518 sbappend(sio, n);
1519 }
1520 m->m_data += len;
1521 m->m_len -= len;
1522 so->so_rcv.ssb_cc -= len;
1523 }
1524 }
1525 if (so->so_oobmark) {
1526 if ((flags & MSG_PEEK) == 0) {
1527 so->so_oobmark -= len;
1528 if (so->so_oobmark == 0) {
1529 sosetstate(so, SS_RCVATMARK);
1530 break;
1531 }
1532 } else {
1533 offset += len;
1534 if (offset == so->so_oobmark)
1535 break;
1536 }
1537 }
1538 if (flags & MSG_EOR)
1539 break;
1540 /*
1541 * If the MSG_WAITALL flag is set (for non-atomic socket),
1542 * we must not quit until resid == 0 or an error
1543 * termination. If a signal/timeout occurs, return
1544 * with a short count but without error.
1545 * Keep signalsockbuf locked against other readers.
1546 */
1547 while ((flags & MSG_WAITALL) && m == NULL &&
1548 resid > 0 && !sosendallatonce(so) &&
1549 so->so_rcv.ssb_mb == NULL) {
7eaeff3d
RM
1550 if (so->so_error || so->so_rerror ||
1551 so->so_state & SS_CANTRCVMORE)
9510c423
SZ
1552 break;
1553 /*
1554 * The window might have closed to zero, make
1555 * sure we send an ack now that we've drained
1556 * the buffer or we might end up blocking until
1557 * the idle takes over (5 seconds).
1558 */
1559 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1560 so_pru_rcvd(so, flags);
1561 error = ssb_wait(&so->so_rcv);
1562 if (error) {
1563 ssb_unlock(&so->so_rcv);
1564 error = 0;
1565 goto done;
1566 }
1567 m = so->so_rcv.ssb_mb;
1568 }
1569 }
1570
1571 /*
1572 * If an atomic read was requested but unread data still remains
1573 * in the record, set MSG_TRUNC.
1574 */
1575 if (m && pr->pr_flags & PR_ATOMIC)
1576 flags |= MSG_TRUNC;
1577
1578 /*
1579 * Cleanup. If an atomic read was requested drop any unread data.
1580 */
1581 if ((flags & MSG_PEEK) == 0) {
1582 if (m && (pr->pr_flags & PR_ATOMIC))
1583 sbdroprecord(&so->so_rcv.sb);
1584 if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
1585 so_pru_rcvd(so, flags);
1586 }
1587
1588 if (orig_resid == resid && orig_resid &&
1589 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
1590 ssb_unlock(&so->so_rcv);
1591 goto restart;
1592 }
1593
1594 if (flagsp)
1595 *flagsp |= flags;
1596release:
1597 ssb_unlock(&so->so_rcv);
1598done:
1599 lwkt_reltoken(&so->so_rcv.ssb_token);
e62cfe62
SZ
1600 if (free_chain) {
1601 if (free_rights && (pr->pr_flags & PR_RIGHTS) &&
1602 pr->pr_domain->dom_dispose)
1603 pr->pr_domain->dom_dispose(free_chain);
9510c423 1604 m_freem(free_chain);
e62cfe62 1605 }
9510c423
SZ
1606 return (error);
1607}
1608
1609int
1610sorecvtcp(struct socket *so, struct sockaddr **psa, struct uio *uio,
1611 struct sockbuf *sio, struct mbuf **controlp, int *flagsp)
1612{
1613 struct mbuf *m, *n;
1614 struct mbuf *free_chain = NULL;
1615 int flags, len, error, offset;
1616 struct protosw *pr = so->so_proto;
fd600585 1617 int moff;
11b81f5d
MD
1618 int didoob;
1619 size_t resid, orig_resid, restmp;
9510c423
SZ
1620
1621 if (uio)
1622 resid = uio->uio_resid;
1623 else
1624 resid = (size_t)(sio->sb_climit - sio->sb_cc);
1625 orig_resid = resid;
1626
984263bc 1627 if (psa)
857caa4a 1628 *psa = NULL;
984263bc 1629 if (controlp)
857caa4a 1630 *controlp = NULL;
984263bc
MD
1631 if (flagsp)
1632 flags = *flagsp &~ MSG_EOR;
1633 else
1634 flags = 0;
1635 if (flags & MSG_OOB) {
b5523eac 1636 m = m_get(M_WAITOK, MT_DATA);
984263bc
MD
1637 if (m == NULL)
1638 return (ENOBUFS);
6b6e0885 1639 error = so_pru_rcvoob(so, m, flags & MSG_PEEK);
984263bc
MD
1640 if (error)
1641 goto bad;
d8a9a23b
MD
1642 if (sio) {
1643 do {
6d49aa6f 1644 sbappend(sio, m);
e54488bb
MD
1645 KKASSERT(resid >= (size_t)m->m_len);
1646 resid -= (size_t)m->m_len;
d8a9a23b
MD
1647 } while (resid > 0 && m);
1648 } else {
1649 do {
1650 uio->uio_resid = resid;
1651 error = uiomove(mtod(m, caddr_t),
e54488bb
MD
1652 (int)szmin(resid, m->m_len),
1653 uio);
d8a9a23b
MD
1654 resid = uio->uio_resid;
1655 m = m_free(m);
1656 } while (uio->uio_resid && error == 0 && m);
1657 }
984263bc
MD
1658bad:
1659 if (m)
1660 m_freem(m);
1661 return (error);
1662 }
984263bc 1663
20156c7a
MD
1664 /*
1665 * The token interlocks against the protocol thread while
1666 * ssb_lock is a blocking lock against other userland entities.
11b81f5d
MD
1667 *
1668 * Lock a limited number of mbufs (not all, so sbcompress() still
1669 * works well). The token is used as an interlock for sbwait() so
1670 * release it afterwords.
20156c7a 1671 */
984263bc 1672restart:
6d49aa6f 1673 error = ssb_lock(&so->so_rcv, SBLOCKWAIT(flags));
984263bc 1674 if (error)
857caa4a 1675 goto done;
984263bc 1676
11b81f5d 1677 lwkt_gettoken(&so->so_rcv.ssb_token);
6d49aa6f 1678 m = so->so_rcv.ssb_mb;
11b81f5d 1679
984263bc
MD
1680 /*
1681 * If we have less data than requested, block awaiting more
1682 * (subject to any timeout) if:
1683 * 1. the current count is less than the low water mark, or
1684 * 2. MSG_WAITALL is set, and it is possible to do the entire
1685 * receive operation at once if we block (resid <= hiwat).
1686 * 3. MSG_DONTWAIT is not set
1687 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1688 * we have to do the receive in sections, and thus risk returning
1689 * a short count if a timeout or signal occurs after we start.
1690 */
857caa4a 1691 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
e54488bb 1692 (size_t)so->so_rcv.ssb_cc < resid) &&
6d49aa6f 1693 (so->so_rcv.ssb_cc < so->so_rcv.ssb_lowat ||
fd600585 1694 ((flags & MSG_WAITALL) && resid <= (size_t)so->so_rcv.ssb_hiwat)))) {
6d49aa6f 1695 KASSERT(m != NULL || !so->so_rcv.ssb_cc, ("receive 1"));
984263bc
MD
1696 if (so->so_error) {
1697 if (m)
1698 goto dontblock;
11b81f5d 1699 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc
MD
1700 error = so->so_error;
1701 if ((flags & MSG_PEEK) == 0)
1702 so->so_error = 0;
1703 goto release;
1704 }
1705 if (so->so_state & SS_CANTRCVMORE) {
1706 if (m)
1707 goto dontblock;
11b81f5d
MD
1708 lwkt_reltoken(&so->so_rcv.ssb_token);
1709 goto release;
984263bc 1710 }
984263bc 1711 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
6b6e0885 1712 (pr->pr_flags & PR_CONNREQUIRED)) {
11b81f5d 1713 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc
MD
1714 error = ENOTCONN;
1715 goto release;
1716 }
11b81f5d
MD
1717 if (resid == 0) {
1718 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc 1719 goto release;
11b81f5d 1720 }
9ba76b73 1721 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
11b81f5d 1722 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc
MD
1723 error = EWOULDBLOCK;
1724 goto release;
1725 }
6d49aa6f
MD
1726 ssb_unlock(&so->so_rcv);
1727 error = ssb_wait(&so->so_rcv);
11b81f5d 1728 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc 1729 if (error)
857caa4a 1730 goto done;
984263bc
MD
1731 goto restart;
1732 }
11b81f5d
MD
1733
1734 /*
1735 * Token still held
1736 */
984263bc 1737dontblock:
11b81f5d
MD
1738 n = m;
1739 restmp = 0;
1740 while (n && restmp < resid) {
1741 n->m_flags |= M_SOLOCKED;
1742 restmp += n->m_len;
1743 if (n->m_next == NULL)
1744 n = n->m_nextpkt;
1745 else
1746 n = n->m_next;
1747 }
1748
1749 /*
1750 * Release token for loop
1751 */
1752 lwkt_reltoken(&so->so_rcv.ssb_token);
d8a9a23b 1753 if (uio && uio->uio_td && uio->uio_td->td_proc)
fde7ac71 1754 uio->uio_td->td_lwp->lwp_ru.ru_msgrcv++;
857caa4a
MD
1755
1756 /*
1757 * note: m should be == sb_mb here. Cache the next record while
1758 * cleaning up. Note that calling m_free*() will break out critical
1759 * section.
1760 */
6d49aa6f 1761 KKASSERT(m == so->so_rcv.ssb_mb);
857caa4a 1762
857caa4a
MD
1763 /*
1764 * Copy to the UIO or mbuf return chain (*mp).
11b81f5d
MD
1765 *
1766 * NOTE: Token is not held for loop
857caa4a 1767 */
984263bc
MD
1768 moff = 0;
1769 offset = 0;
11b81f5d
MD
1770 didoob = 0;
1771
1772 while (m && (m->m_flags & M_SOLOCKED) && resid > 0 && error == 0) {
fd600585
SZ
1773 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
1774 ("receive 3"));
1775
6cef7136 1776 soclrstate(so, SS_RCVATMARK);
e54488bb 1777 len = (resid > INT_MAX) ? INT_MAX : resid;
984263bc
MD
1778 if (so->so_oobmark && len > so->so_oobmark - offset)
1779 len = so->so_oobmark - offset;
1780 if (len > m->m_len - moff)
1781 len = m->m_len - moff;
d8a9a23b 1782
984263bc 1783 /*
d8a9a23b
MD
1784 * Copy out to the UIO or pass the mbufs back to the SIO.
1785 * The SIO is dealt with when we eat the mbuf, but deal
1786 * with the resid here either way.
984263bc 1787 */
d8a9a23b 1788 if (uio) {
d8a9a23b
MD
1789 uio->uio_resid = resid;
1790 error = uiomove(mtod(m, caddr_t) + moff, len, uio);
1791 resid = uio->uio_resid;
984263bc
MD
1792 if (error)
1793 goto release;
857caa4a 1794 } else {
e54488bb 1795 resid -= (size_t)len;
857caa4a
MD
1796 }
1797
1798 /*
1799 * Eat the entire mbuf or just a piece of it
1800 */
11b81f5d 1801 offset += len;
984263bc 1802 if (len == m->m_len - moff) {
11b81f5d
MD
1803 m = m->m_next;
1804 moff = 0;
1805 } else {
1806 moff += len;
1807 }
1808
1809 /*
1810 * Check oobmark
1811 */
1812 if (so->so_oobmark && offset == so->so_oobmark) {
1813 didoob = 1;
1814 break;
1815 }
1816 }
1817
1818 /*
1819 * Synchronize sockbuf with data we read.
1820 *
1821 * NOTE: (m) is junk on entry (it could be left over from the
1822 * previous loop).
1823 */
1824 if ((flags & MSG_PEEK) == 0) {
1825 lwkt_gettoken(&so->so_rcv.ssb_token);
1826 m = so->so_rcv.ssb_mb;
1827 while (m && offset >= m->m_len) {
1828 if (so->so_oobmark) {
1829 so->so_oobmark -= m->m_len;
1830 if (so->so_oobmark == 0) {
1831 sosetstate(so, SS_RCVATMARK);
1832 didoob = 1;
984263bc 1833 }
984263bc 1834 }
11b81f5d
MD
1835 offset -= m->m_len;
1836 if (sio) {
1837 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
1838 sbappend(sio, m);
1839 m = n;
857caa4a 1840 } else {
11b81f5d
MD
1841 m = sbunlinkmbuf(&so->so_rcv.sb,
1842 m, &free_chain);
984263bc
MD
1843 }
1844 }
11b81f5d
MD
1845 if (offset) {
1846 KKASSERT(m);
1847 if (sio) {
b5523eac 1848 n = m_copym(m, 0, offset, M_WAITOK);
11b81f5d
MD
1849 if (n)
1850 sbappend(sio, n);
1851 }
1852 m->m_data += offset;
1853 m->m_len -= offset;
1854 so->so_rcv.ssb_cc -= offset;
1855 if (so->so_oobmark) {
1856 so->so_oobmark -= offset;
984263bc 1857 if (so->so_oobmark == 0) {
6cef7136 1858 sosetstate(so, SS_RCVATMARK);
11b81f5d 1859 didoob = 1;
984263bc 1860 }
984263bc 1861 }
11b81f5d 1862 offset = 0;
984263bc 1863 }
11b81f5d
MD
1864 lwkt_reltoken(&so->so_rcv.ssb_token);
1865 }
1866
1867 /*
1868 * If the MSG_WAITALL flag is set (for non-atomic socket),
1869 * we must not quit until resid == 0 or an error termination.
1870 *
1871 * If a signal/timeout occurs, return with a short count but without
1872 * error.
1873 *
1874 * Keep signalsockbuf locked against other readers.
1875 *
1876 * XXX if MSG_PEEK we currently do quit.
1877 */
1878 if ((flags & MSG_WAITALL) && !(flags & MSG_PEEK) &&
1879 didoob == 0 && resid > 0 &&
1880 !sosendallatonce(so)) {
1881 lwkt_gettoken(&so->so_rcv.ssb_token);
1882 error = 0;
1883 while ((m = so->so_rcv.ssb_mb) == NULL) {
1884 if (so->so_error || (so->so_state & SS_CANTRCVMORE)) {
1885 error = so->so_error;
984263bc 1886 break;
11b81f5d 1887 }
984263bc
MD
1888 /*
1889 * The window might have closed to zero, make
1890 * sure we send an ack now that we've drained
1891 * the buffer or we might end up blocking until
1892 * the idle takes over (5 seconds).
1893 */
fd600585 1894 if (so->so_pcb)
96c6eb29 1895 so_pru_rcvd_async(so);
11b81f5d
MD
1896 if (so->so_rcv.ssb_mb == NULL)
1897 error = ssb_wait(&so->so_rcv);
984263bc 1898 if (error) {
11b81f5d 1899 lwkt_reltoken(&so->so_rcv.ssb_token);
6d49aa6f 1900 ssb_unlock(&so->so_rcv);
857caa4a
MD
1901 error = 0;
1902 goto done;
984263bc 1903 }
984263bc 1904 }
11b81f5d
MD
1905 if (m && error == 0)
1906 goto dontblock;
1907 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc
MD
1908 }
1909
857caa4a 1910 /*
11b81f5d
MD
1911 * Token not held here.
1912 *
1913 * Cleanup. If an atomic read was requested drop any unread data XXX
857caa4a
MD
1914 */
1915 if ((flags & MSG_PEEK) == 0) {
fd600585 1916 if (so->so_pcb)
96c6eb29 1917 so_pru_rcvd_async(so);
984263bc 1918 }
bf8a9a6f 1919
d8a9a23b 1920 if (orig_resid == resid && orig_resid &&
fd600585 1921 (so->so_state & SS_CANTRCVMORE) == 0) {
6d49aa6f 1922 ssb_unlock(&so->so_rcv);
984263bc
MD
1923 goto restart;
1924 }
1925
1926 if (flagsp)
1927 *flagsp |= flags;
1928release:
6d49aa6f 1929 ssb_unlock(&so->so_rcv);
857caa4a 1930done:
857caa4a
MD
1931 if (free_chain)
1932 m_freem(free_chain);
984263bc
MD
1933 return (error);
1934}
1935
edf5c732
MD
1936/*
1937 * Shut a socket down. Note that we do not get a frontend lock as we
1938 * want to be able to shut the socket down even if another thread is
1939 * blocked in a read(), thus waking it up.
1940 */
984263bc 1941int
c972a82f 1942soshutdown(struct socket *so, int how)
984263bc 1943{
984263bc
MD
1944 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1945 return (EINVAL);
1946
ff518922 1947 if (how != SHUT_WR) {
edf5c732 1948 /*ssb_lock(&so->so_rcv, M_WAITOK);*/
984263bc 1949 sorflush(so);
edf5c732 1950 /*ssb_unlock(&so->so_rcv);*/
ff518922 1951 }
0cd6e642 1952 if (how != SHUT_RD)
6b6e0885 1953 return (so_pru_shutdown(so));
984263bc
MD
1954 return (0);
1955}
1956
1957void
c972a82f 1958sorflush(struct socket *so)
984263bc 1959{
6d49aa6f 1960 struct signalsockbuf *ssb = &so->so_rcv;
1fd87d54 1961 struct protosw *pr = so->so_proto;
6d49aa6f 1962 struct signalsockbuf asb;
984263bc 1963
14343ad3 1964 atomic_set_int(&ssb->ssb_flags, SSB_NOINTR);
e43a034f 1965
ff518922 1966 lwkt_gettoken(&ssb->ssb_token);
984263bc 1967 socantrcvmore(so);
6d49aa6f 1968 asb = *ssb;
14343ad3
MD
1969
1970 /*
1971 * Can't just blow up the ssb structure here
1972 */
ff518922 1973 bzero(&ssb->sb, sizeof(ssb->sb));
14343ad3 1974 ssb->ssb_timeo = 0;
14343ad3
MD
1975 ssb->ssb_lowat = 0;
1976 ssb->ssb_hiwat = 0;
1977 ssb->ssb_mbmax = 0;
1978 atomic_clear_int(&ssb->ssb_flags, SSB_CLEAR_MASK);
1979
edf5c732 1980 if ((pr->pr_flags & PR_RIGHTS) && pr->pr_domain->dom_dispose)
6d49aa6f
MD
1981 (*pr->pr_domain->dom_dispose)(asb.ssb_mb);
1982 ssb_release(&asb, so);
edf5c732
MD
1983
1984 lwkt_reltoken(&ssb->ssb_token);
984263bc
MD
1985}
1986
1987#ifdef INET
1988static int
c972a82f 1989do_setopt_accept_filter(struct socket *so, struct sockopt *sopt)
984263bc
MD
1990{
1991 struct accept_filter_arg *afap = NULL;
1992 struct accept_filter *afp;
1993 struct so_accf *af = so->so_accf;
1994 int error = 0;
1995
1996 /* do not set/remove accept filters on non listen sockets */
1997 if ((so->so_options & SO_ACCEPTCONN) == 0) {
1998 error = EINVAL;
1999 goto out;
2000 }
2001
2002 /* removing the filter */
2003 if (sopt == NULL) {
2004 if (af != NULL) {
2005 if (af->so_accept_filter != NULL &&
2006 af->so_accept_filter->accf_destroy != NULL) {
2007 af->so_accept_filter->accf_destroy(so);
2008 }
2009 if (af->so_accept_filter_str != NULL) {
884717e1 2010 kfree(af->so_accept_filter_str, M_ACCF);
984263bc 2011 }
884717e1 2012 kfree(af, M_ACCF);
984263bc
MD
2013 so->so_accf = NULL;
2014 }
2015 so->so_options &= ~SO_ACCEPTFILTER;
2016 return (0);
2017 }
2018 /* adding a filter */
2019 /* must remove previous filter first */
2020 if (af != NULL) {
2021 error = EINVAL;
2022 goto out;
2023 }
2024 /* don't put large objects on the kernel stack */
884717e1 2025 afap = kmalloc(sizeof(*afap), M_TEMP, M_WAITOK);
984263bc
MD
2026 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
2027 afap->af_name[sizeof(afap->af_name)-1] = '\0';
2028 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
2029 if (error)
2030 goto out;
2031 afp = accept_filt_get(afap->af_name);
2032 if (afp == NULL) {
2033 error = ENOENT;
2034 goto out;
2035 }
884717e1 2036 af = kmalloc(sizeof(*af), M_ACCF, M_WAITOK | M_ZERO);
984263bc
MD
2037 if (afp->accf_create != NULL) {
2038 if (afap->af_name[0] != '\0') {
2039 int len = strlen(afap->af_name) + 1;
2040
884717e1
SW
2041 af->so_accept_filter_str = kmalloc(len, M_ACCF,
2042 M_WAITOK);
984263bc
MD
2043 strcpy(af->so_accept_filter_str, afap->af_name);
2044 }
2045 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
2046 if (af->so_accept_filter_arg == NULL) {
884717e1
SW
2047 kfree(af->so_accept_filter_str, M_ACCF);
2048 kfree(af, M_ACCF);
984263bc
MD
2049 so->so_accf = NULL;
2050 error = EINVAL;
2051 goto out;
2052 }
2053 }
2054 af->so_accept_filter = afp;
2055 so->so_accf = af;
2056 so->so_options |= SO_ACCEPTFILTER;
2057out:
2058 if (afap != NULL)
884717e1 2059 kfree(afap, M_TEMP);
984263bc
MD
2060 return (error);
2061}
2062#endif /* INET */
2063
2064/*
2065 * Perhaps this routine, and sooptcopyout(), below, ought to come in
2066 * an additional variant to handle the case where the option value needs
2067 * to be some kind of integer, but not a specific size.
2068 * In addition to their use here, these functions are also called by the
2069 * protocol-level pr_ctloutput() routines.
2070 */
2071int
c972a82f 2072sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
de0003fe
AE
2073{
2074 return soopt_to_kbuf(sopt, buf, len, minlen);
2075}
2076
2077int
2078soopt_to_kbuf(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
984263bc
MD
2079{
2080 size_t valsize;
2081
792239df 2082 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
de0003fe
AE
2083 KKASSERT(kva_p(buf));
2084
984263bc
MD
2085 /*
2086 * If the user gives us more than we wanted, we ignore it,
2087 * but if we don't get the minimum length the caller
2088 * wants, we return EINVAL. On success, sopt->sopt_valsize
2089 * is set to however much we actually retrieved.
2090 */
2091 if ((valsize = sopt->sopt_valsize) < minlen)
2092 return EINVAL;
2093 if (valsize > len)
2094 sopt->sopt_valsize = valsize = len;
2095
984263bc
MD
2096 bcopy(sopt->sopt_val, buf, valsize);
2097 return 0;
2098}
2099
e71a125f 2100
984263bc 2101int
c972a82f 2102sosetopt(struct socket *so, struct sockopt *sopt)
984263bc
MD
2103{
2104 int error, optval;
2105 struct linger l;
2106 struct timeval tv;
2107 u_long val;
14343ad3 2108 struct signalsockbuf *sotmp;
984263bc
MD
2109
2110 error = 0;
e79d388f 2111 sopt->sopt_dir = SOPT_SET;
984263bc 2112 if (sopt->sopt_level != SOL_SOCKET) {
6b6e0885 2113 if (so->so_proto && so->so_proto->pr_ctloutput) {
002c1265 2114 return (so_pr_ctloutput(so, sopt));
6b6e0885 2115 }
984263bc
MD
2116 error = ENOPROTOOPT;
2117 } else {
2118 switch (sopt->sopt_name) {
2119#ifdef INET
2120 case SO_ACCEPTFILTER:
2121 error = do_setopt_accept_filter(so, sopt);
2122 if (error)
2123 goto bad;
2124 break;
2125#endif /* INET */
2126 case SO_LINGER:
2127 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
2128 if (error)
2129 goto bad;
2130
2131 so->so_linger = l.l_linger;
2132 if (l.l_onoff)
2133 so->so_options |= SO_LINGER;
2134 else
2135 so->so_options &= ~SO_LINGER;
2136 break;
2137
2138 case SO_DEBUG:
2139 case SO_KEEPALIVE:
2140 case SO_DONTROUTE:
2141 case SO_USELOOPBACK:
2142 case SO_BROADCAST:
2143 case SO_REUSEADDR:
2144 case SO_REUSEPORT:
2145 case SO_OOBINLINE:
2146 case SO_TIMESTAMP:
89233cfd 2147 case SO_NOSIGPIPE:
7eaeff3d 2148 case SO_RERROR:
984263bc
MD
2149 error = sooptcopyin(sopt, &optval, sizeof optval,
2150 sizeof optval);
2151 if (error)
2152 goto bad;
2153 if (optval)
2154 so->so_options |= sopt->sopt_name;
2155 else
2156 so->so_options &= ~sopt->sopt_name;
2157 break;
2158
2159 case SO_SNDBUF:
2160 case SO_RCVBUF:
2161 case SO_SNDLOWAT:
2162 case SO_RCVLOWAT:
2163 error = sooptcopyin(sopt, &optval, sizeof optval,
2164 sizeof optval);
2165 if (error)
2166 goto bad;
2167
2168 /*
2169 * Values < 1 make no sense for any of these
2170 * options, so disallow them.
2171 */
2172 if (optval < 1) {
2173 error = EINVAL;
2174 goto bad;
2175 }
2176
2177 switch (sopt->sopt_name) {
2178 case SO_SNDBUF:
2179 case SO_RCVBUF:
6d49aa6f 2180 if (ssb_reserve(sopt->sopt_name == SO_SNDBUF ?
984263bc 2181 &so->so_snd : &so->so_rcv, (u_long)optval,
e4700d00
JH
2182 so,
2183 &curproc->p_rlimit[RLIMIT_SBSIZE]) == 0) {
984263bc
MD
2184 error = ENOBUFS;
2185 goto bad;
2186 }
14343ad3
MD
2187 sotmp = (sopt->sopt_name == SO_SNDBUF) ?
2188 &so->so_snd : &so->so_rcv;
2189 atomic_clear_int(&sotmp->ssb_flags,
2190 SSB_AUTOSIZE);
984263bc
MD
2191 break;
2192
2193 /*
2194 * Make sure the low-water is never greater than
2195 * the high-water.
2196 */
2197 case SO_SNDLOWAT:
6d49aa6f
MD
2198 so->so_snd.ssb_lowat =
2199 (optval > so->so_snd.ssb_hiwat) ?
2200 so->so_snd.ssb_hiwat : optval;
14343ad3
MD
2201 atomic_clear_int(&so->so_snd.ssb_flags,
2202 SSB_AUTOLOWAT);
984263bc
MD
2203 break;
2204 case SO_RCVLOWAT:
6d49aa6f
MD
2205 so->so_rcv.ssb_lowat =
2206 (optval > so->so_rcv.ssb_hiwat) ?
2207 so->so_rcv.ssb_hiwat : optval;
14343ad3
MD
2208 atomic_clear_int(&so->so_rcv.ssb_flags,
2209 SSB_AUTOLOWAT);
984263bc
MD
2210 break;
2211 }
2212 break;
2213
2214 case SO_SNDTIMEO:
2215 case SO_RCVTIMEO:
2216 error = sooptcopyin(sopt, &tv, sizeof tv,
2217 sizeof tv);
2218 if (error)
2219 goto bad;
2220
2221 /* assert(hz > 0); */
45546849 2222 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
984263bc
MD
2223 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
2224 error = EDOM;
2225 goto bad;
2226 }
2227 /* assert(tick > 0); */
45546849 2228 /* assert(ULONG_MAX - INT_MAX >= 1000000); */
a591f597 2229 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / ustick;
45546849 2230 if (val > INT_MAX) {
984263bc
MD
2231 error = EDOM;
2232 goto bad;
2233 }
2234 if (val == 0 && tv.tv_usec != 0)
2235 val = 1;
2236
2237 switch (sopt->sopt_name) {
2238 case SO_SNDTIMEO:
6d49aa6f 2239 so->so_snd.ssb_timeo = val;
984263bc
MD
2240 break;
2241 case SO_RCVTIMEO:
6d49aa6f 2242 so->so_rcv.ssb_timeo = val;
984263bc
MD
2243 break;
2244 }
2245 break;
2246 default:
2247 error = ENOPROTOOPT;
2248 break;
2249 }
2250 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
002c1265 2251 (void) so_pr_ctloutput(so, sopt);
984263bc
MD
2252 }
2253 }
2254bad:
2255 return (error);
2256}
2257
2258/* Helper routine for getsockopt */
2259int
f1f552f6 2260sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
984263bc 2261{
de0003fe
AE
2262 soopt_from_kbuf(sopt, buf, len);
2263 return 0;
2264}
2265
2266void
2267soopt_from_kbuf(struct sockopt *sopt, const void *buf, size_t len)
2268{
984263bc
MD
2269 size_t valsize;
2270
565d9f6f
SZ
2271 if (len == 0) {
2272 sopt->sopt_valsize = 0;
2273 return;
2274 }
2275
792239df 2276 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
de0003fe 2277 KKASSERT(kva_p(buf));
984263bc
MD
2278
2279 /*
2280 * Documented get behavior is that we always return a value,
2281 * possibly truncated to fit in the user's buffer.
2282 * Traditional behavior is that we always tell the user
2283 * precisely how much we copied, rather than something useful
2284 * like the total amount we had available for her.
2285 * Note that this interface is not idempotent; the entire answer must
2286 * generated ahead of time.
2287 */
231d276b 2288 valsize = szmin(len, sopt->sopt_valsize);
984263bc
MD
2289 sopt->sopt_valsize = valsize;
2290 if (sopt->sopt_val != 0) {
de0003fe 2291 bcopy(buf, sopt->sopt_val, valsize);
984263bc 2292 }
e71a125f
AE
2293}
2294
984263bc 2295int
c972a82f 2296sogetopt(struct socket *so, struct sockopt *sopt)
984263bc
MD
2297{
2298 int error, optval;
755c519c 2299 long optval_l;
984263bc
MD
2300 struct linger l;
2301 struct timeval tv;
51f4ca92 2302#ifdef INET
984263bc 2303 struct accept_filter_arg *afap;
51f4ca92 2304#endif
984263bc
MD
2305
2306 error = 0;
e79d388f 2307 sopt->sopt_dir = SOPT_GET;
984263bc
MD
2308 if (sopt->sopt_level != SOL_SOCKET) {
2309 if (so->so_proto && so->so_proto->pr_ctloutput) {
002c1265 2310 return (so_pr_ctloutput(so, sopt));
984263bc
MD
2311 } else
2312 return (ENOPROTOOPT);
2313 } else {
2314 switch (sopt->sopt_name) {
2315#ifdef INET
2316 case SO_ACCEPTFILTER:
2317 if ((so->so_options & SO_ACCEPTCONN) == 0)
2318 return (EINVAL);
884717e1
SW
2319 afap = kmalloc(sizeof(*afap), M_TEMP,
2320 M_WAITOK | M_ZERO);
984263bc
MD
2321 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
2322 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
2323 if (so->so_accf->so_accept_filter_str != NULL)
2324 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
2325 }
2326 error = sooptcopyout(sopt, afap, sizeof(*afap));
884717e1 2327 kfree(afap, M_TEMP);
984263bc
MD
2328 break;
2329#endif /* INET */
2330
2331 case SO_LINGER:
2332 l.l_onoff = so->so_options & SO_LINGER;
2333 l.l_linger = so->so_linger;
2334 error = sooptcopyout(sopt, &l, sizeof l);
2335 break;
2336
2337 case SO_USELOOPBACK:
2338 case SO_DONTROUTE:
2339 case SO_DEBUG:
2340 case SO_KEEPALIVE:
2341 case SO_REUSEADDR:
2342 case SO_REUSEPORT:
2343 case SO_BROADCAST:
2344 case SO_OOBINLINE:
2345 case SO_TIMESTAMP:
89233cfd 2346 case SO_NOSIGPIPE:
7eaeff3d 2347 case SO_RERROR:
984263bc
MD
2348 optval = so->so_options & sopt->sopt_name;
2349integer:
2350 error = sooptcopyout(sopt, &optval, sizeof optval);
2351 break;
2352
2353 case SO_TYPE:
2354 optval = so->so_type;
2355 goto integer;
2356
2357 case SO_ERROR:
7eaeff3d
RM
2358 if (so->so_error) {
2359 optval = so->so_error;
2360 so->so_error = 0;
2361 } else {
2362 optval = so->so_rerror;
2363 so->so_rerror = 0;
2364 }
984263bc
MD
2365 goto integer;
2366
2367 case SO_SNDBUF:
6d49aa6f 2368 optval = so->so_snd.ssb_hiwat;
984263bc
MD
2369 goto integer;
2370
2371 case SO_RCVBUF:
6d49aa6f 2372 optval = so->so_rcv.ssb_hiwat;
984263bc
MD
2373 goto integer;
2374
2375 case SO_SNDLOWAT:
6d49aa6f 2376 optval = so->so_snd.ssb_lowat;
984263bc
MD
2377 goto integer;
2378
2379 case SO_RCVLOWAT:
6d49aa6f 2380 optval = so->so_rcv.ssb_lowat;
984263bc
MD
2381 goto integer;
2382
2383 case SO_SNDTIMEO:
2384 case SO_RCVTIMEO:
2385 optval = (sopt->sopt_name == SO_SNDTIMEO ?
6d49aa6f 2386 so->so_snd.ssb_timeo : so->so_rcv.ssb_timeo);
984263bc
MD
2387
2388 tv.tv_sec = optval / hz;
a591f597 2389 tv.tv_usec = (optval % hz) * ustick;
984263bc
MD
2390 error = sooptcopyout(sopt, &tv, sizeof tv);
2391 break;
2392
755c519c
SZ
2393 case SO_SNDSPACE:
2394 optval_l = ssb_space(&so->so_snd);
2395 error = sooptcopyout(sopt, &optval_l, sizeof(optval_l));
2396 break;
2397
8ba7dcb1
SZ
2398 case SO_CPUHINT:
2399 optval = -1; /* no hint */
2400 goto integer;
2401
984263bc
MD
2402 default:
2403 error = ENOPROTOOPT;
2404 break;
2405 }
8ba7dcb1
SZ
2406 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput)
2407 so_pr_ctloutput(so, sopt);
984263bc
MD
2408 return (error);
2409 }
2410}
2411
2412/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
2413int
2414soopt_getm(struct sockopt *sopt, struct mbuf **mp)
2415{
2416 struct mbuf *m, *m_prev;
bf6ac9fa
JH
2417 int sopt_size = sopt->sopt_valsize, msize;
2418
b5523eac 2419 m = m_getl(sopt_size, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA,
bf6ac9fa
JH
2420 0, &msize);
2421 if (m == NULL)
2422 return (ENOBUFS);
2423 m->m_len = min(msize, sopt_size);
984263bc
MD
2424 sopt_size -= m->m_len;
2425 *mp = m;
2426 m_prev = m;
2427
bf6ac9fa 2428 while (sopt_size > 0) {
b5523eac 2429 m = m_getl(sopt_size, sopt->sopt_td ? M_WAITOK : M_NOWAIT,
bf6ac9fa
JH
2430 MT_DATA, 0, &msize);
2431 if (m == NULL) {
984263bc 2432 m_freem(*mp);
bf6ac9fa 2433 return (ENOBUFS);
984263bc 2434 }
bf6ac9fa 2435 m->m_len = min(msize, sopt_size);
984263bc
MD
2436 sopt_size -= m->m_len;
2437 m_prev->m_next = m;
2438 m_prev = m;
2439 }
bf6ac9fa 2440 return (0);
984263bc
MD
2441}
2442
2443/* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
2444int
2445soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
de0003fe
AE
2446{
2447 soopt_to_mbuf(sopt, m);
2448 return 0;
2449}
2450
2451void
2452soopt_to_mbuf(struct sockopt *sopt, struct mbuf *m)
984263bc 2453{
c3e742f9
NT
2454 size_t valsize;
2455 void *val;
984263bc 2456
792239df 2457 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
de0003fe 2458 KKASSERT(kva_p(m));
984263bc 2459 if (sopt->sopt_val == NULL)
792239df 2460 return;
c3e742f9
NT
2461 val = sopt->sopt_val;
2462 valsize = sopt->sopt_valsize;
2463 while (m != NULL && valsize >= m->m_len) {
de0003fe 2464 bcopy(val, mtod(m, char *), m->m_len);
c3e742f9
NT
2465 valsize -= m->m_len;
2466 val = (caddr_t)val + m->m_len;
984263bc
MD
2467 m = m->m_next;
2468 }
2469 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
2470 panic("ip6_sooptmcopyin");
984263bc
MD
2471}
2472
de0003fe
AE
2473/* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
2474int
2475soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
e71a125f 2476{
de0003fe 2477 return soopt_from_mbuf(sopt, m);
e71a125f
AE
2478}
2479
984263bc 2480int
de0003fe 2481soopt_from_mbuf(struct sockopt *sopt, struct mbuf *m)
984263bc
MD
2482{
2483 struct mbuf *m0 = m;
2484 size_t valsize = 0;
c3e742f9
NT
2485 size_t maxsize;
2486 void *val;
984263bc 2487
792239df 2488 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
de0003fe 2489 KKASSERT(kva_p(m));
984263bc
MD
2490 if (sopt->sopt_val == NULL)
2491 return 0;
c3e742f9
NT
2492 val = sopt->sopt_val;
2493 maxsize = sopt->sopt_valsize;
2494 while (m != NULL && maxsize >= m->m_len) {
de0003fe 2495 bcopy(mtod(m, char *), val, m->m_len);
c3e742f9
NT
2496 maxsize -= m->m_len;
2497 val = (caddr_t)val + m->m_len;
984263bc
MD
2498 valsize += m->m_len;
2499 m = m->m_next;
2500 }
2501 if (m != NULL) {
2502 /* enough soopt buffer should be given from user-land */
2503 m_freem(m0);
bf6ac9fa 2504 return (EINVAL);
984263bc
MD
2505 }
2506 sopt->sopt_valsize = valsize;
2507 return 0;
2508}
2509
2510void
c972a82f 2511sohasoutofband(struct socket *so)
984263bc
MD
2512{
2513 if (so->so_sigio != NULL)
2514 pgsigio(so->so_sigio, SIGURG, 0);
5d83d150
SZ
2515 /*
2516 * NOTE:
2517 * There is no need to use NOTE_OOB as KNOTE hint here:
2518 * soread filter depends on so_oobmark and SS_RCVATMARK
2519 * so_state. NOTE_OOB would cause unnecessary penalty
2520 * in KNOTE, if there was knote processing contention.
2521 */
2522 KNOTE(&so->so_rcv.ssb_kq.ki_note, 0);
984263bc
MD
2523}
2524
984263bc
MD
2525int
2526sokqfilter(struct file *fp, struct knote *kn)
2527{
2528 struct socket *so = (struct socket *)kn->kn_fp->f_data;
6d49aa6f 2529 struct signalsockbuf *ssb;
984263bc
MD
2530
2531 switch (kn->kn_filter) {
2532 case EVFILT_READ:
2533 if (so->so_options & SO_ACCEPTCONN)
2534 kn->kn_fop = &solisten_filtops;
2535 else
2536 kn->kn_fop = &soread_filtops;
6d49aa6f 2537 ssb = &so->so_rcv;
984263bc
MD
2538 break;
2539 case EVFILT_WRITE:
2540 kn->kn_fop = &sowrite_filtops;
6d49aa6f 2541 ssb = &so->so_snd;
984263bc 2542 break;
73c344d3
SG
2543 case EVFILT_EXCEPT:
2544 kn->kn_fop = &soexcept_filtops;
2545 ssb = &so->so_rcv;
2546 break;
984263bc 2547 default:
b287d649 2548 return (EOPNOTSUPP);
984263bc
MD
2549 }
2550
5b22f1a7 2551 knote_insert(&ssb->ssb_kq.ki_note, kn);
14343ad3 2552 atomic_set_int(&ssb->ssb_flags, SSB_KNOTE);
984263bc
MD
2553 return (0);
2554}
2555
2556static void
2557filt_sordetach(struct knote *kn)
2558{
2559 struct socket *so = (struct socket *)kn->kn_fp->f_data;
984263bc 2560
5b22f1a7
SG
2561 knote_remove(&so->so_rcv.ssb_kq.ki_note, kn);
2562 if (SLIST_EMPTY(&so->so_rcv.ssb_kq.ki_note))
14343ad3 2563 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_KNOTE);
984263bc
MD
2564}
2565
2566/*ARGSUSED*/
2567static int
aea5d1bb 2568filt_soread(struct knote *kn, long hint __unused)
984263bc
MD
2569{
2570 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2571
73c344d3
SG
2572 if (kn->kn_sfflags & NOTE_OOB) {
2573 if ((so->so_oobmark || (so->so_state & SS_RCVATMARK))) {
2574 kn->kn_fflags |= NOTE_OOB;
2575 return (1);
2576 }
2577 return (0);
70a4a30f 2578 }
6d49aa6f 2579 kn->kn_data = so->so_rcv.ssb_cc;
8c4ed426 2580
3bcb6e5e
SZ
2581 if (so->so_state & SS_CANTRCVMORE) {
2582 /*
2583 * Only set NODATA if all data has been exhausted.
2584 */
2585 if (kn->kn_data == 0)
2586 kn->kn_flags |= EV_NODATA;
984263bc
MD
2587 kn->kn_flags |= EV_EOF;
2588 kn->kn_fflags = so->so_error;
2589 return (1);
2590 }
7eaeff3d 2591 if (so->so_error || so->so_rerror)
984263bc
MD
2592 return (1);
2593 if (kn->kn_sfflags & NOTE_LOWAT)
2594 return (kn->kn_data >= kn->kn_sdata);
e5857bf7 2595 return ((kn->kn_data >= so->so_rcv.ssb_lowat) ||
6cef7136 2596 !TAILQ_EMPTY(&so->so_comp));
984263bc
MD
2597}
2598
2599static void
2600filt_sowdetach(struct knote *kn)
2601{
2602 struct socket *so = (struct socket *)kn->kn_fp->f_data;
984263bc 2603
5b22f1a7
SG
2604 knote_remove(&so->so_snd.ssb_kq.ki_note, kn);
2605 if (SLIST_EMPTY(&so->so_snd.ssb_kq.ki_note))
14343ad3 2606 atomic_clear_int(&so->so_snd.ssb_flags, SSB_KNOTE);
984263bc
MD
2607}
2608
2609/*ARGSUSED*/
2610static int
aea5d1bb 2611filt_sowrite(struct knote *kn, long hint __unused)
984263bc
MD
2612{
2613 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2614
d54c9898
SZ
2615 if (so->so_snd.ssb_flags & SSB_PREALLOC)
2616 kn->kn_data = ssb_space_prealloc(&so->so_snd);
2617 else
2618 kn->kn_data = ssb_space(&so->so_snd);
2619
984263bc 2620 if (so->so_state & SS_CANTSENDMORE) {
3bcb6e5e 2621 kn->kn_flags |= (EV_EOF | EV_NODATA);
984263bc
MD
2622 kn->kn_fflags = so->so_error;
2623 return (1);
2624 }
2625 if (so->so_error) /* temporary udp error */
2626 return (1);
2627 if (((so->so_state & SS_ISCONNECTED) == 0) &&
2628 (so->so_proto->pr_flags & PR_CONNREQUIRED))
2629 return (0);
2630 if (kn->kn_sfflags & NOTE_LOWAT)
2631 return (kn->kn_data >= kn->kn_sdata);
6d49aa6f 2632 return (kn->kn_data >= so->so_snd.ssb_lowat);
984263bc
MD
2633}
2634
2635/*ARGSUSED*/
2636static int
aea5d1bb 2637filt_solisten(struct knote *kn, long hint __unused)
984263bc
MD
2638{
2639 struct socket *so = (struct socket *)kn->kn_fp->f_data;
65e531c6
SZ
2640 int qlen = so->so_qlen;
2641
2642 if (soavailconn > 0 && qlen > soavailconn)
2643 qlen = soavailconn;
2644 kn->kn_data = qlen;
984263bc 2645
65e531c6 2646 return (!TAILQ_EMPTY(&so->so_comp));
984263bc 2647}