tcp: Aggregate the mbuf in sosendtcp() a little bit
[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.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
7405c902 67 * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.24 2003/11/11 17:18:18 silby Exp $
9116be8e 68 * $DragonFly: src/sys/kern/uipc_socket.c,v 1.55 2008/09/02 16:17:52 dillon Exp $
984263bc
MD
69 */
70
71#include "opt_inet.h"
78812139 72#include "opt_sctp.h"
984263bc
MD
73
74#include <sys/param.h>
75#include <sys/systm.h>
76#include <sys/fcntl.h>
77#include <sys/malloc.h>
78#include <sys/mbuf.h>
79#include <sys/domain.h>
80#include <sys/file.h> /* for struct knote */
81#include <sys/kernel.h>
82#include <sys/malloc.h>
83#include <sys/event.h>
984263bc
MD
84#include <sys/proc.h>
85#include <sys/protosw.h>
86#include <sys/socket.h>
87#include <sys/socketvar.h>
6b6e0885 88#include <sys/socketops.h>
984263bc
MD
89#include <sys/resourcevar.h>
90#include <sys/signalvar.h>
91#include <sys/sysctl.h>
92#include <sys/uio.h>
93#include <sys/jail.h>
94#include <vm/vm_zone.h>
e71a125f 95#include <vm/pmap.h>
984263bc 96
e43a034f 97#include <sys/thread2.h>
d6cb521d 98#include <sys/socketvar2.h>
e43a034f 99
984263bc
MD
100#include <machine/limits.h>
101
c16aca65
SZ
102extern int tcp_sosnd_agglim;
103
984263bc
MD
104#ifdef INET
105static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
106#endif /* INET */
107
108static void filt_sordetach(struct knote *kn);
109static int filt_soread(struct knote *kn, long hint);
110static void filt_sowdetach(struct knote *kn);
111static int filt_sowrite(struct knote *kn, long hint);
112static int filt_solisten(struct knote *kn, long hint);
113
114static struct filterops solisten_filtops =
4c91dbc9 115 { FILTEROP_ISFD, NULL, filt_sordetach, filt_solisten };
984263bc 116static struct filterops soread_filtops =
4c91dbc9 117 { FILTEROP_ISFD, NULL, filt_sordetach, filt_soread };
984263bc 118static struct filterops sowrite_filtops =
4c91dbc9 119 { FILTEROP_ISFD, NULL, filt_sowdetach, filt_sowrite };
73c344d3 120static struct filterops soexcept_filtops =
4c91dbc9 121 { FILTEROP_ISFD, NULL, filt_sordetach, filt_soread };
984263bc 122
69ea5b8d 123MALLOC_DEFINE(M_SOCKET, "socket", "socket struct");
984263bc
MD
124MALLOC_DEFINE(M_SONAME, "soname", "socket name");
125MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
126
984263bc
MD
127
128static int somaxconn = SOMAXCONN;
129SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
130 &somaxconn, 0, "Maximum pending socket connection queue size");
131
132/*
133 * Socket operation routines.
134 * These routines are called by the routines in
135 * sys_socket.c or from a system process, and
136 * implement the semantics of socket operations by
137 * switching out to the protocol specific routines.
138 */
139
140/*
69ea5b8d 141 * Get a socket structure, and initialize it.
984263bc
MD
142 * Note that it would probably be better to allocate socket
143 * and PCB at the same time, but I'm not convinced that all
144 * the protocols can be easily modified to do this.
145 */
146struct socket *
c972a82f 147soalloc(int waitok)
984263bc
MD
148{
149 struct socket *so;
69ea5b8d 150 unsigned waitmask;
984263bc 151
69ea5b8d
NT
152 waitmask = waitok ? M_WAITOK : M_NOWAIT;
153 so = kmalloc(sizeof(struct socket), M_SOCKET, M_ZERO|waitmask);
984263bc
MD
154 if (so) {
155 /* XXX race condition for reentrant kernel */
984263bc 156 TAILQ_INIT(&so->so_aiojobq);
5b22f1a7
SG
157 TAILQ_INIT(&so->so_rcv.ssb_kq.ki_mlist);
158 TAILQ_INIT(&so->so_snd.ssb_kq.ki_mlist);
a3c18566
MD
159 lwkt_token_init(&so->so_rcv.ssb_token, "rcvtok");
160 lwkt_token_init(&so->so_snd.ssb_token, "sndtok");
6cef7136
MD
161 so->so_state = SS_NOFDREF;
162 so->so_refs = 1;
984263bc
MD
163 }
164 return so;
165}
166
167int
dadab5e9
MD
168socreate(int dom, struct socket **aso, int type,
169 int proto, struct thread *td)
984263bc 170{
dadab5e9
MD
171 struct proc *p = td->td_proc;
172 struct protosw *prp;
173 struct socket *so;
e4700d00 174 struct pru_attach_info ai;
dadab5e9 175 int error;
984263bc
MD
176
177 if (proto)
178 prp = pffindproto(dom, proto, type);
179 else
180 prp = pffindtype(dom, type);
181
182 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
183 return (EPROTONOSUPPORT);
184
41c20dac 185 if (p->p_ucred->cr_prison && jail_socket_unixiproute_only &&
984263bc
MD
186 prp->pr_domain->dom_family != PF_LOCAL &&
187 prp->pr_domain->dom_family != PF_INET &&
3e4150ef 188 prp->pr_domain->dom_family != PF_INET6 &&
984263bc
MD
189 prp->pr_domain->dom_family != PF_ROUTE) {
190 return (EPROTONOSUPPORT);
191 }
192
193 if (prp->pr_type != type)
194 return (EPROTOTYPE);
195 so = soalloc(p != 0);
6cef7136 196 if (so == NULL)
984263bc
MD
197 return (ENOBUFS);
198
6cef7136
MD
199 /*
200 * Callers of socreate() presumably will connect up a descriptor
201 * and call soclose() if they cannot. This represents our so_refs
202 * (which should be 1) from soalloc().
203 */
204 soclrstate(so, SS_NOFDREF);
205
48e7b118
MD
206 /*
207 * Set a default port for protocol processing. No action will occur
208 * on the socket on this port until an inpcb is attached to it and
209 * is able to match incoming packets, or until the socket becomes
210 * available to userland.
002c1265
MD
211 *
212 * We normally default the socket to the protocol thread on cpu 0.
213 * If PR_SYNC_PORT is set (unix domain sockets) there is no protocol
214 * thread and all pr_*()/pru_*() calls are executed synchronously.
48e7b118 215 */
002c1265
MD
216 if (prp->pr_flags & PR_SYNC_PORT)
217 so->so_port = &netisr_sync_port;
218 else
219 so->so_port = cpu_portfn(0);
48e7b118 220
984263bc
MD
221 TAILQ_INIT(&so->so_incomp);
222 TAILQ_INIT(&so->so_comp);
223 so->so_type = type;
e9a372eb 224 so->so_cred = crhold(p->p_ucred);
984263bc 225 so->so_proto = prp;
e4700d00
JH
226 ai.sb_rlimit = &p->p_rlimit[RLIMIT_SBSIZE];
227 ai.p_ucred = p->p_ucred;
228 ai.fd_rdir = p->p_fd->fd_rdir;
48e7b118 229
5b0b9fa5
PA
230 /*
231 * Auto-sizing of socket buffers is managed by the protocols and
232 * the appropriate flags must be set in the pru_attach function.
233 */
e4700d00 234 error = so_pru_attach(so, proto, &ai);
984263bc 235 if (error) {
6cef7136
MD
236 sosetstate(so, SS_NOFDREF);
237 sofree(so); /* from soalloc */
238 return error;
984263bc 239 }
48e7b118 240
6cef7136
MD
241 /*
242 * NOTE: Returns referenced socket.
243 */
984263bc
MD
244 *aso = so;
245 return (0);
246}
247
248int
dadab5e9 249sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
984263bc 250{
984263bc
MD
251 int error;
252
6b6e0885 253 error = so_pru_bind(so, nam, td);
984263bc
MD
254 return (error);
255}
256
6cef7136 257static void
dadab5e9 258sodealloc(struct socket *so)
984263bc 259{
6d49aa6f 260 if (so->so_rcv.ssb_hiwat)
984263bc 261 (void)chgsbsize(so->so_cred->cr_uidinfo,
6d49aa6f
MD
262 &so->so_rcv.ssb_hiwat, 0, RLIM_INFINITY);
263 if (so->so_snd.ssb_hiwat)
984263bc 264 (void)chgsbsize(so->so_cred->cr_uidinfo,
6d49aa6f 265 &so->so_snd.ssb_hiwat, 0, RLIM_INFINITY);
984263bc 266#ifdef INET
81d59d3d
HP
267 /* remove accept filter if present */
268 if (so->so_accf != NULL)
269 do_setopt_accept_filter(so, NULL);
984263bc
MD
270#endif /* INET */
271 crfree(so->so_cred);
69ea5b8d 272 kfree(so, M_SOCKET);
984263bc
MD
273}
274
275int
dadab5e9 276solisten(struct socket *so, int backlog, struct thread *td)
984263bc 277{
e43a034f 278 int error;
78812139
EN
279#ifdef SCTP
280 short oldopt, oldqlimit;
281#endif /* SCTP */
984263bc 282
6cef7136 283 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING))
78812139 284 return (EINVAL);
78812139
EN
285
286#ifdef SCTP
287 oldopt = so->so_options;
288 oldqlimit = so->so_qlimit;
289#endif /* SCTP */
290
6cef7136 291 lwkt_gettoken(&so->so_rcv.ssb_token);
984263bc
MD
292 if (TAILQ_EMPTY(&so->so_comp))
293 so->so_options |= SO_ACCEPTCONN;
6cef7136 294 lwkt_reltoken(&so->so_rcv.ssb_token);
984263bc
MD
295 if (backlog < 0 || backlog > somaxconn)
296 backlog = somaxconn;
297 so->so_qlimit = backlog;
78812139
EN
298 /* SCTP needs to look at tweak both the inbound backlog parameter AND
299 * the so_options (UDP model both connect's and gets inbound
300 * connections .. implicitly).
301 */
302 error = so_pru_listen(so, td);
303 if (error) {
304#ifdef SCTP
305 /* Restore the params */
306 so->so_options = oldopt;
307 so->so_qlimit = oldqlimit;
308#endif /* SCTP */
78812139
EN
309 return (error);
310 }
984263bc
MD
311 return (0);
312}
313
4402d8a2
MD
314/*
315 * Destroy a disconnected socket. This routine is a NOP if entities
316 * still have a reference on the socket:
317 *
318 * so_pcb - The protocol stack still has a reference
319 * SS_NOFDREF - There is no longer a file pointer reference
4402d8a2 320 */
984263bc 321void
dadab5e9 322sofree(struct socket *so)
984263bc 323{
5217bcbc
MD
324 struct socket *head;
325
326 /*
327 * This is a bit hackish at the moment. We need to interlock
328 * any accept queue we are on before we potentially lose the
329 * last reference to avoid races against a re-reference from
330 * someone operating on the queue.
331 */
332 while ((head = so->so_head) != NULL) {
333 lwkt_getpooltoken(head);
334 if (so->so_head == head)
335 break;
336 lwkt_relpooltoken(head);
337 }
984263bc 338
6cef7136
MD
339 /*
340 * Arbitrage the last free.
341 */
342 KKASSERT(so->so_refs > 0);
5217bcbc
MD
343 if (atomic_fetchadd_int(&so->so_refs, -1) != 1) {
344 if (head)
345 lwkt_relpooltoken(head);
4402d8a2 346 return;
5217bcbc 347 }
6cef7136
MD
348
349 KKASSERT(so->so_pcb == NULL && (so->so_state & SS_NOFDREF));
e28d8186 350 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
6cef7136
MD
351
352 /*
5217bcbc
MD
353 * We're done, remove ourselves from the accept queue we are
354 * on, if we are on one.
6cef7136 355 */
984263bc
MD
356 if (head != NULL) {
357 if (so->so_state & SS_INCOMP) {
358 TAILQ_REMOVE(&head->so_incomp, so, so_list);
359 head->so_incqlen--;
360 } else if (so->so_state & SS_COMP) {
361 /*
362 * We must not decommission a socket that's
363 * on the accept(2) queue. If we do, then
364 * accept(2) may hang after select(2) indicated
365 * that the listening socket was ready.
366 */
5217bcbc 367 lwkt_relpooltoken(head);
984263bc
MD
368 return;
369 } else {
370 panic("sofree: not queued");
371 }
6cef7136 372 soclrstate(so, SS_INCOMP);
984263bc 373 so->so_head = NULL;
5217bcbc 374 lwkt_relpooltoken(head);
984263bc 375 }
6d49aa6f 376 ssb_release(&so->so_snd, so);
984263bc
MD
377 sorflush(so);
378 sodealloc(so);
379}
380
381/*
382 * Close a socket on last file table reference removal.
383 * Initiate disconnect if connected.
384 * Free socket when disconnect complete.
385 */
386int
9ba76b73 387soclose(struct socket *so, int fflag)
984263bc 388{
984263bc
MD
389 int error = 0;
390
58c2553a 391 funsetown(&so->so_sigio);
19be7d32 392 if (so->so_pcb == NULL)
984263bc
MD
393 goto discard;
394 if (so->so_state & SS_ISCONNECTED) {
395 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
396 error = sodisconnect(so);
397 if (error)
398 goto drop;
399 }
400 if (so->so_options & SO_LINGER) {
401 if ((so->so_state & SS_ISDISCONNECTING) &&
9ba76b73 402 (fflag & FNONBLOCK))
984263bc
MD
403 goto drop;
404 while (so->so_state & SS_ISCONNECTED) {
6cef7136
MD
405 error = tsleep(&so->so_timeo, PCATCH,
406 "soclos", so->so_linger * hz);
984263bc
MD
407 if (error)
408 break;
409 }
410 }
411 }
412drop:
413 if (so->so_pcb) {
6b6e0885
JH
414 int error2;
415
416 error2 = so_pru_detach(so);
984263bc
MD
417 if (error == 0)
418 error = error2;
419 }
420discard:
5217bcbc 421 lwkt_getpooltoken(so);
19be7d32 422 if (so->so_options & SO_ACCEPTCONN) {
4402d8a2 423 struct socket *sp;
19be7d32 424
4402d8a2
MD
425 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
426 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
6cef7136 427 soclrstate(sp, SS_INCOMP);
4402d8a2
MD
428 sp->so_head = NULL;
429 so->so_incqlen--;
9116be8e 430 soaborta(sp);
19be7d32 431 }
4402d8a2 432 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
19be7d32 433 TAILQ_REMOVE(&so->so_comp, sp, so_list);
6cef7136 434 soclrstate(sp, SS_COMP);
19be7d32 435 sp->so_head = NULL;
4402d8a2 436 so->so_qlen--;
9116be8e 437 soaborta(sp);
19be7d32
MD
438 }
439 }
5217bcbc 440 lwkt_relpooltoken(so);
984263bc
MD
441 if (so->so_state & SS_NOFDREF)
442 panic("soclose: NOFDREF");
6cef7136
MD
443 sosetstate(so, SS_NOFDREF); /* take ref */
444 sofree(so); /* dispose of ref */
984263bc
MD
445 return (error);
446}
447
448/*
9116be8e
MD
449 * Abort and destroy a socket. Only one abort can be in progress
450 * at any given moment.
984263bc 451 */
4402d8a2 452void
c972a82f 453soabort(struct socket *so)
984263bc 454{
6cef7136
MD
455 soreference(so);
456 so_pru_abort(so);
4402d8a2 457}
984263bc 458
4402d8a2
MD
459void
460soaborta(struct socket *so)
461{
6cef7136
MD
462 soreference(so);
463 so_pru_aborta(so);
984263bc
MD
464}
465
fd86a41c
SZ
466void
467soabort_oncpu(struct socket *so)
468{
6cef7136
MD
469 soreference(so);
470 so_pru_abort_oncpu(so);
fd86a41c
SZ
471}
472
c19fdb0e
MD
473/*
474 * so is passed in ref'd, which becomes owned by
475 * the cleared SS_NOFDREF flag.
476 */
984263bc 477int
dadab5e9 478soaccept(struct socket *so, struct sockaddr **nam)
984263bc 479{
984263bc
MD
480 int error;
481
482 if ((so->so_state & SS_NOFDREF) == 0)
483 panic("soaccept: !NOFDREF");
6cef7136 484 soclrstate(so, SS_NOFDREF); /* owned by lack of SS_NOFDREF */
002c1265 485 error = so_pru_accept_direct(so, nam);
984263bc
MD
486 return (error);
487}
488
489int
dadab5e9 490soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
984263bc 491{
984263bc
MD
492 int error;
493
494 if (so->so_options & SO_ACCEPTCONN)
495 return (EOPNOTSUPP);
984263bc
MD
496 /*
497 * If protocol is connection-based, can only connect once.
498 * Otherwise, if connected, try to disconnect first.
499 * This allows user to disconnect by connecting to, e.g.,
500 * a null address.
501 */
502 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
503 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
59429d28 504 (error = sodisconnect(so)))) {
984263bc 505 error = EISCONN;
59429d28
MD
506 } else {
507 /*
508 * Prevent accumulated error from previous connection
509 * from biting us.
510 */
511 so->so_error = 0;
6b6e0885 512 error = so_pru_connect(so, nam, td);
59429d28 513 }
984263bc
MD
514 return (error);
515}
516
517int
dadab5e9 518soconnect2(struct socket *so1, struct socket *so2)
984263bc 519{
984263bc
MD
520 int error;
521
6b6e0885 522 error = so_pru_connect2(so1, so2);
984263bc
MD
523 return (error);
524}
525
526int
dadab5e9 527sodisconnect(struct socket *so)
984263bc 528{
984263bc
MD
529 int error;
530
531 if ((so->so_state & SS_ISCONNECTED) == 0) {
532 error = ENOTCONN;
533 goto bad;
534 }
535 if (so->so_state & SS_ISDISCONNECTING) {
536 error = EALREADY;
537 goto bad;
538 }
6b6e0885 539 error = so_pru_disconnect(so);
984263bc 540bad:
984263bc
MD
541 return (error);
542}
543
544#define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
545/*
546 * Send on a socket.
547 * If send must go all at once and message is larger than
548 * send buffering, then hard error.
549 * Lock against other senders.
550 * If must go all at once and not enough room now, then
551 * inform user that this would block and do nothing.
552 * Otherwise, if nonblocking, send as much as possible.
553 * The data to be sent is described by "uio" if nonzero,
554 * otherwise by the mbuf chain "top" (which must be null
555 * if uio is not). Data provided in mbuf chain must be small
556 * enough to send all at once.
557 *
558 * Returns nonzero on error, timeout or signal; callers
559 * must check for short counts if EINTR/ERESTART are returned.
560 * Data and control buffers are freed on return.
561 */
562int
dadab5e9
MD
563sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
564 struct mbuf *top, struct mbuf *control, int flags,
565 struct thread *td)
984263bc
MD
566{
567 struct mbuf **mp;
dadab5e9 568 struct mbuf *m;
e54488bb
MD
569 size_t resid;
570 int space, len;
e43a034f 571 int clen = 0, error, dontroute, mlen;
984263bc 572 int atomic = sosendallatonce(so) || top;
6b6e0885 573 int pru_flags;
984263bc 574
5bd48c1d 575 if (uio) {
984263bc 576 resid = uio->uio_resid;
5bd48c1d 577 } else {
e54488bb 578 resid = (size_t)top->m_pkthdr.len;
5bd48c1d
MD
579#ifdef INVARIANTS
580 len = 0;
581 for (m = top; m; m = m->m_next)
582 len += m->m_len;
583 KKASSERT(top->m_pkthdr.len == len);
584#endif
585 }
48e7b118 586
984263bc 587 /*
e54488bb
MD
588 * WARNING! resid is unsigned, space and len are signed. space
589 * can wind up negative if the sockbuf is overcommitted.
984263bc
MD
590 *
591 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
592 * type sockets since that's an error.
593 */
e54488bb 594 if (so->so_type == SOCK_STREAM && (flags & MSG_EOR)) {
984263bc
MD
595 error = EINVAL;
596 goto out;
597 }
598
599 dontroute =
600 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
601 (so->so_proto->pr_flags & PR_ATOMIC);
fde7ac71
SS
602 if (td->td_lwp != NULL)
603 td->td_lwp->lwp_ru.ru_msgsnd++;
984263bc
MD
604 if (control)
605 clen = control->m_len;
6cef7136 606#define gotoerr(errcode) { error = errcode; goto release; }
984263bc
MD
607
608restart:
6d49aa6f 609 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
984263bc
MD
610 if (error)
611 goto out;
48e7b118 612
984263bc 613 do {
984263bc 614 if (so->so_state & SS_CANTSENDMORE)
6ea1e9b9 615 gotoerr(EPIPE);
984263bc
MD
616 if (so->so_error) {
617 error = so->so_error;
618 so->so_error = 0;
984263bc
MD
619 goto release;
620 }
621 if ((so->so_state & SS_ISCONNECTED) == 0) {
622 /*
623 * `sendto' and `sendmsg' is allowed on a connection-
624 * based socket if it supports implied connect.
625 * Return ENOTCONN if not connected and no address is
626 * supplied.
627 */
628 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
629 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
630 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
631 !(resid == 0 && clen != 0))
6ea1e9b9 632 gotoerr(ENOTCONN);
984263bc 633 } else if (addr == 0)
6ea1e9b9 634 gotoerr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
984263bc
MD
635 ENOTCONN : EDESTADDRREQ);
636 }
3a6117bb
MD
637 if ((atomic && resid > so->so_snd.ssb_hiwat) ||
638 clen > so->so_snd.ssb_hiwat) {
639 gotoerr(EMSGSIZE);
640 }
6d49aa6f 641 space = ssb_space(&so->so_snd);
984263bc
MD
642 if (flags & MSG_OOB)
643 space += 1024;
e54488bb 644 if ((space < 0 || (size_t)space < resid + clen) && uio &&
6d49aa6f 645 (atomic || space < so->so_snd.ssb_lowat || space < clen)) {
9ba76b73 646 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
6ea1e9b9 647 gotoerr(EWOULDBLOCK);
6d49aa6f
MD
648 ssb_unlock(&so->so_snd);
649 error = ssb_wait(&so->so_snd);
984263bc
MD
650 if (error)
651 goto out;
652 goto restart;
653 }
984263bc
MD
654 mp = &top;
655 space -= clen;
656 do {
657 if (uio == NULL) {
658 /*
659 * Data is prepackaged in "top".
660 */
661 resid = 0;
662 if (flags & MSG_EOR)
663 top->m_flags |= M_EOR;
664 } else do {
e54488bb
MD
665 if (resid > INT_MAX)
666 resid = INT_MAX;
667 m = m_getl((int)resid, MB_WAIT, MT_DATA,
50503f0f
JH
668 top == NULL ? M_PKTHDR : 0, &mlen);
669 if (top == NULL) {
984263bc 670 m->m_pkthdr.len = 0;
60233e58 671 m->m_pkthdr.rcvif = NULL;
984263bc 672 }
e54488bb 673 len = imin((int)szmin(mlen, resid), space);
50503f0f 674 if (resid < MINCLSIZE) {
984263bc
MD
675 /*
676 * For datagram protocols, leave room
677 * for protocol headers in first mbuf.
678 */
679 if (atomic && top == 0 && len < mlen)
680 MH_ALIGN(m, len);
681 }
682 space -= len;
e54488bb 683 error = uiomove(mtod(m, caddr_t), (size_t)len, uio);
984263bc
MD
684 resid = uio->uio_resid;
685 m->m_len = len;
686 *mp = m;
687 top->m_pkthdr.len += len;
688 if (error)
689 goto release;
690 mp = &m->m_next;
e54488bb 691 if (resid == 0) {
984263bc
MD
692 if (flags & MSG_EOR)
693 top->m_flags |= M_EOR;
694 break;
695 }
696 } while (space > 0 && atomic);
697 if (dontroute)
698 so->so_options |= SO_DONTROUTE;
6b6e0885
JH
699 if (flags & MSG_OOB) {
700 pru_flags = PRUS_OOB;
701 } else if ((flags & MSG_EOF) &&
702 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
e54488bb 703 (resid == 0)) {
6b6e0885
JH
704 /*
705 * If the user set MSG_EOF, the protocol
706 * understands this flag and nothing left to
707 * send then use PRU_SEND_EOF instead of PRU_SEND.
708 */
709 pru_flags = PRUS_EOF;
710 } else if (resid > 0 && space > 0) {
711 /* If there is more to send, set PRUS_MORETOCOME */
712 pru_flags = PRUS_MORETOCOME;
713 } else {
714 pru_flags = 0;
715 }
984263bc
MD
716 /*
717 * XXX all the SS_CANTSENDMORE checks previously
718 * done could be out of date. We could have recieved
719 * a reset packet in an interrupt or maybe we slept
720 * while doing page faults in uiomove() etc. We could
721 * probably recheck again inside the splnet() protection
722 * here, but there are probably other places that this
723 * also happens. We must rethink this.
724 */
6b6e0885 725 error = so_pru_send(so, pru_flags, top, addr, control, td);
984263bc
MD
726 if (dontroute)
727 so->so_options &= ~SO_DONTROUTE;
728 clen = 0;
729 control = 0;
e28d8186 730 top = NULL;
984263bc
MD
731 mp = &top;
732 if (error)
6b6e0885 733 goto release;
984263bc
MD
734 } while (resid && space > 0);
735 } while (resid);
736
737release:
6d49aa6f 738 ssb_unlock(&so->so_snd);
984263bc
MD
739out:
740 if (top)
741 m_freem(top);
742 if (control)
743 m_freem(control);
744 return (error);
745}
746
6ea1e9b9
JH
747/*
748 * A specialization of sosend() for UDP based on protocol-specific knowledge:
749 * so->so_proto->pr_flags has the PR_ATOMIC field set. This means that
750 * sosendallatonce() returns true,
751 * the "atomic" variable is true,
752 * and sosendudp() blocks until space is available for the entire send.
753 * so->so_proto->pr_flags does not have the PR_CONNREQUIRED or
754 * PR_IMPLOPCL flags set.
755 * UDP has no out-of-band data.
756 * UDP has no control data.
757 * UDP does not support MSG_EOR.
758 */
759int
760sosendudp(struct socket *so, struct sockaddr *addr, struct uio *uio,
761 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
762{
6ea1e9b9 763 boolean_t dontroute; /* temporary SO_DONTROUTE setting */
e54488bb
MD
764 size_t resid;
765 int error;
766 int space;
6ea1e9b9 767
fde7ac71
SS
768 if (td->td_lwp != NULL)
769 td->td_lwp->lwp_ru.ru_msgsnd++;
6ea1e9b9
JH
770 if (control)
771 m_freem(control);
772
773 KASSERT((uio && !top) || (top && !uio), ("bad arguments to sosendudp"));
e54488bb 774 resid = uio ? uio->uio_resid : (size_t)top->m_pkthdr.len;
6ea1e9b9
JH
775
776restart:
6d49aa6f 777 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
6ea1e9b9
JH
778 if (error)
779 goto out;
780
6ea1e9b9
JH
781 if (so->so_state & SS_CANTSENDMORE)
782 gotoerr(EPIPE);
783 if (so->so_error) {
784 error = so->so_error;
785 so->so_error = 0;
6ea1e9b9
JH
786 goto release;
787 }
788 if (!(so->so_state & SS_ISCONNECTED) && addr == NULL)
789 gotoerr(EDESTADDRREQ);
6d49aa6f 790 if (resid > so->so_snd.ssb_hiwat)
6ea1e9b9 791 gotoerr(EMSGSIZE);
e54488bb
MD
792 space = ssb_space(&so->so_snd);
793 if (uio && (space < 0 || (size_t)space < resid)) {
9ba76b73 794 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
6ea1e9b9 795 gotoerr(EWOULDBLOCK);
6d49aa6f
MD
796 ssb_unlock(&so->so_snd);
797 error = ssb_wait(&so->so_snd);
6ea1e9b9
JH
798 if (error)
799 goto out;
800 goto restart;
801 }
6ea1e9b9
JH
802
803 if (uio) {
e12241e1 804 top = m_uiomove(uio);
6ea1e9b9
JH
805 if (top == NULL)
806 goto release;
807 }
808
809 dontroute = (flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE);
810 if (dontroute)
811 so->so_options |= SO_DONTROUTE;
812
813 error = so_pru_send(so, 0, top, addr, NULL, td);
814 top = NULL; /* sent or freed in lower layer */
815
816 if (dontroute)
817 so->so_options &= ~SO_DONTROUTE;
818
819release:
6d49aa6f 820 ssb_unlock(&so->so_snd);
6ea1e9b9
JH
821out:
822 if (top)
823 m_freem(top);
824 return (error);
825}
826
5bc42dd1
SZ
827int
828sosendtcp(struct socket *so, struct sockaddr *addr, struct uio *uio,
829 struct mbuf *top, struct mbuf *control, int flags,
830 struct thread *td)
831{
832 struct mbuf **mp;
833 struct mbuf *m;
834 size_t resid;
835 int space, len;
836 int error, mlen;
837 int allatonce;
838 int pru_flags;
839
840 if (uio) {
841 KKASSERT(top == NULL);
842 allatonce = 0;
843 resid = uio->uio_resid;
844 } else {
845 allatonce = 1;
846 resid = (size_t)top->m_pkthdr.len;
847#ifdef INVARIANTS
848 len = 0;
849 for (m = top; m; m = m->m_next)
850 len += m->m_len;
851 KKASSERT(top->m_pkthdr.len == len);
852#endif
853 }
854
855 /*
856 * WARNING! resid is unsigned, space and len are signed. space
857 * can wind up negative if the sockbuf is overcommitted.
858 *
859 * Also check to make sure that MSG_EOR isn't used on TCP
860 */
861 if (flags & MSG_EOR) {
862 error = EINVAL;
863 goto out;
864 }
865
866 if (control) {
867 /* TCP doesn't do control messages (rights, creds, etc) */
868 if (control->m_len) {
869 error = EINVAL;
870 goto out;
871 }
872 m_freem(control); /* empty control, just free it */
873 control = NULL;
874 }
875
876 if (td->td_lwp != NULL)
877 td->td_lwp->lwp_ru.ru_msgsnd++;
878
879#define gotoerr(errcode) { error = errcode; goto release; }
880
881restart:
882 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
883 if (error)
884 goto out;
885
886 do {
887 if (so->so_state & SS_CANTSENDMORE)
888 gotoerr(EPIPE);
889 if (so->so_error) {
890 error = so->so_error;
891 so->so_error = 0;
892 goto release;
893 }
894 if ((so->so_state & SS_ISCONNECTED) == 0 &&
895 (so->so_state & SS_ISCONFIRMING) == 0)
896 gotoerr(ENOTCONN);
897 if (allatonce && resid > so->so_snd.ssb_hiwat)
898 gotoerr(EMSGSIZE);
899
900 space = ssb_space(&so->so_snd);
901 if (flags & MSG_OOB)
902 space += 1024;
903 if ((space < 0 || (size_t)space < resid) && !allatonce &&
904 space < so->so_snd.ssb_lowat) {
905 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
906 gotoerr(EWOULDBLOCK);
907 ssb_unlock(&so->so_snd);
908 error = ssb_wait(&so->so_snd);
909 if (error)
910 goto out;
911 goto restart;
912 }
913 mp = &top;
914 do {
c16aca65
SZ
915 int cnt = 0;
916
5bc42dd1
SZ
917 if (uio == NULL) {
918 /*
919 * Data is prepackaged in "top".
920 */
921 resid = 0;
922 } else do {
923 if (resid > INT_MAX)
924 resid = INT_MAX;
925 m = m_getl((int)resid, MB_WAIT, MT_DATA,
926 top == NULL ? M_PKTHDR : 0, &mlen);
927 if (top == NULL) {
928 m->m_pkthdr.len = 0;
929 m->m_pkthdr.rcvif = NULL;
930 }
931 len = imin((int)szmin(mlen, resid), space);
932 space -= len;
933 error = uiomove(mtod(m, caddr_t), (size_t)len, uio);
934 resid = uio->uio_resid;
935 m->m_len = len;
936 *mp = m;
937 top->m_pkthdr.len += len;
938 if (error)
939 goto release;
940 mp = &m->m_next;
941 if (resid == 0)
942 break;
c16aca65
SZ
943 ++cnt;
944 } while (space > 0 && cnt < tcp_sosnd_agglim);
5bc42dd1
SZ
945
946 if (flags & MSG_OOB) {
947 pru_flags = PRUS_OOB;
948 } else if (resid > 0 && space > 0) {
949 /* If there is more to send, set PRUS_MORETOCOME */
950 pru_flags = PRUS_MORETOCOME;
951 } else {
952 pru_flags = 0;
953 }
954
955 /*
956 * XXX all the SS_CANTSENDMORE checks previously
957 * done could be out of date. We could have recieved
958 * a reset packet in an interrupt or maybe we slept
959 * while doing page faults in uiomove() etc. We could
960 * probably recheck again inside the splnet() protection
961 * here, but there are probably other places that this
962 * also happens. We must rethink this.
963 */
0ad8e15e
SZ
964 if ((pru_flags & PRUS_OOB) ||
965 (pru_flags & PRUS_MORETOCOME) == 0) {
966 error = so_pru_send(so, pru_flags, top,
967 NULL, NULL, td);
968 } else {
969 so_pru_send_async(so, pru_flags, top,
970 NULL, NULL, td);
971 error = 0;
972 }
5bc42dd1
SZ
973
974 top = NULL;
975 mp = &top;
976 if (error)
977 goto release;
978 } while (resid && space > 0);
979 } while (resid);
980
981release:
982 ssb_unlock(&so->so_snd);
983out:
984 if (top)
985 m_freem(top);
986 if (control)
987 m_freem(control);
988 return (error);
989}
990
984263bc
MD
991/*
992 * Implement receive operations on a socket.
6cef7136 993 *
6d49aa6f 994 * We depend on the way that records are added to the signalsockbuf
984263bc
MD
995 * by sbappend*. In particular, each record (mbufs linked through m_next)
996 * must begin with an address if the protocol so specifies,
997 * followed by an optional mbuf or mbufs containing ancillary data,
998 * and then zero or more mbufs of data.
6cef7136
MD
999 *
1000 * Although the signalsockbuf is locked, new data may still be appended.
1001 * A token inside the ssb_lock deals with MP issues and still allows
1002 * the network to access the socket if we block in a uio.
984263bc
MD
1003 *
1004 * The caller may receive the data as a single mbuf chain by supplying
1005 * an mbuf **mp0 for use in returning the chain. The uio is then used
1006 * only for the count in uio_resid.
1007 */
1008int
c972a82f 1009soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
6d49aa6f 1010 struct sockbuf *sio, struct mbuf **controlp, int *flagsp)
984263bc 1011{
d8a9a23b 1012 struct mbuf *m, *n;
857caa4a 1013 struct mbuf *free_chain = NULL;
e43a034f 1014 int flags, len, error, offset;
984263bc 1015 struct protosw *pr = so->so_proto;
984263bc 1016 int moff, type = 0;
e54488bb 1017 size_t resid, orig_resid;
d8a9a23b
MD
1018
1019 if (uio)
1020 resid = uio->uio_resid;
1021 else
e54488bb 1022 resid = (size_t)(sio->sb_climit - sio->sb_cc);
d8a9a23b 1023 orig_resid = resid;
984263bc 1024
984263bc 1025 if (psa)
857caa4a 1026 *psa = NULL;
984263bc 1027 if (controlp)
857caa4a 1028 *controlp = NULL;
984263bc
MD
1029 if (flagsp)
1030 flags = *flagsp &~ MSG_EOR;
1031 else
1032 flags = 0;
1033 if (flags & MSG_OOB) {
74f1caca 1034 m = m_get(MB_WAIT, MT_DATA);
984263bc
MD
1035 if (m == NULL)
1036 return (ENOBUFS);
6b6e0885 1037 error = so_pru_rcvoob(so, m, flags & MSG_PEEK);
984263bc
MD
1038 if (error)
1039 goto bad;
d8a9a23b
MD
1040 if (sio) {
1041 do {
6d49aa6f 1042 sbappend(sio, m);
e54488bb
MD
1043 KKASSERT(resid >= (size_t)m->m_len);
1044 resid -= (size_t)m->m_len;
d8a9a23b
MD
1045 } while (resid > 0 && m);
1046 } else {
1047 do {
1048 uio->uio_resid = resid;
1049 error = uiomove(mtod(m, caddr_t),
e54488bb
MD
1050 (int)szmin(resid, m->m_len),
1051 uio);
d8a9a23b
MD
1052 resid = uio->uio_resid;
1053 m = m_free(m);
1054 } while (uio->uio_resid && error == 0 && m);
1055 }
984263bc
MD
1056bad:
1057 if (m)
1058 m_freem(m);
1059 return (error);
1060 }
e54488bb 1061 if ((so->so_state & SS_ISCONFIRMING) && resid)
6b6e0885 1062 so_pru_rcvd(so, 0);
984263bc 1063
20156c7a
MD
1064 /*
1065 * The token interlocks against the protocol thread while
1066 * ssb_lock is a blocking lock against other userland entities.
1067 */
1068 lwkt_gettoken(&so->so_rcv.ssb_token);
984263bc 1069restart:
6d49aa6f 1070 error = ssb_lock(&so->so_rcv, SBLOCKWAIT(flags));
984263bc 1071 if (error)
857caa4a 1072 goto done;
984263bc 1073
6d49aa6f 1074 m = so->so_rcv.ssb_mb;
984263bc
MD
1075 /*
1076 * If we have less data than requested, block awaiting more
1077 * (subject to any timeout) if:
1078 * 1. the current count is less than the low water mark, or
1079 * 2. MSG_WAITALL is set, and it is possible to do the entire
1080 * receive operation at once if we block (resid <= hiwat).
1081 * 3. MSG_DONTWAIT is not set
1082 * If MSG_WAITALL is set but resid is larger than the receive buffer,
1083 * we have to do the receive in sections, and thus risk returning
1084 * a short count if a timeout or signal occurs after we start.
1085 */
857caa4a 1086 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
e54488bb 1087 (size_t)so->so_rcv.ssb_cc < resid) &&
6d49aa6f 1088 (so->so_rcv.ssb_cc < so->so_rcv.ssb_lowat ||
e54488bb 1089 ((flags & MSG_WAITALL) && resid <= (size_t)so->so_rcv.ssb_hiwat)) &&
984263bc 1090 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
6d49aa6f 1091 KASSERT(m != NULL || !so->so_rcv.ssb_cc, ("receive 1"));
984263bc
MD
1092 if (so->so_error) {
1093 if (m)
1094 goto dontblock;
1095 error = so->so_error;
1096 if ((flags & MSG_PEEK) == 0)
1097 so->so_error = 0;
1098 goto release;
1099 }
1100 if (so->so_state & SS_CANTRCVMORE) {
1101 if (m)
1102 goto dontblock;
1103 else
1104 goto release;
1105 }
857caa4a 1106 for (; m; m = m->m_next) {
984263bc 1107 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
6d49aa6f 1108 m = so->so_rcv.ssb_mb;
984263bc
MD
1109 goto dontblock;
1110 }
857caa4a 1111 }
984263bc 1112 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
6b6e0885 1113 (pr->pr_flags & PR_CONNREQUIRED)) {
984263bc
MD
1114 error = ENOTCONN;
1115 goto release;
1116 }
d8a9a23b 1117 if (resid == 0)
984263bc 1118 goto release;
9ba76b73 1119 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
984263bc
MD
1120 error = EWOULDBLOCK;
1121 goto release;
1122 }
6d49aa6f
MD
1123 ssb_unlock(&so->so_rcv);
1124 error = ssb_wait(&so->so_rcv);
984263bc 1125 if (error)
857caa4a 1126 goto done;
984263bc
MD
1127 goto restart;
1128 }
1129dontblock:
d8a9a23b 1130 if (uio && uio->uio_td && uio->uio_td->td_proc)
fde7ac71 1131 uio->uio_td->td_lwp->lwp_ru.ru_msgrcv++;
857caa4a
MD
1132
1133 /*
1134 * note: m should be == sb_mb here. Cache the next record while
1135 * cleaning up. Note that calling m_free*() will break out critical
1136 * section.
1137 */
6d49aa6f 1138 KKASSERT(m == so->so_rcv.ssb_mb);
857caa4a
MD
1139
1140 /*
1141 * Skip any address mbufs prepending the record.
1142 */
984263bc
MD
1143 if (pr->pr_flags & PR_ADDR) {
1144 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
1145 orig_resid = 0;
1146 if (psa)
cfa2ba21 1147 *psa = dup_sockaddr(mtod(m, struct sockaddr *));
857caa4a 1148 if (flags & MSG_PEEK)
984263bc 1149 m = m->m_next;
857caa4a 1150 else
6d49aa6f 1151 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
984263bc 1152 }
857caa4a
MD
1153
1154 /*
1155 * Skip any control mbufs prepending the record.
1156 */
78812139
EN
1157#ifdef SCTP
1158 if (pr->pr_flags & PR_ADDR_OPT) {
1159 /*
1160 * For SCTP we may be getting a
1161 * whole message OR a partial delivery.
1162 */
857caa4a 1163 if (m && m->m_type == MT_SONAME) {
78812139
EN
1164 orig_resid = 0;
1165 if (psa)
1166 *psa = dup_sockaddr(mtod(m, struct sockaddr *));
857caa4a 1167 if (flags & MSG_PEEK)
78812139 1168 m = m->m_next;
857caa4a 1169 else
6d49aa6f 1170 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
78812139
EN
1171 }
1172 }
1173#endif /* SCTP */
984263bc
MD
1174 while (m && m->m_type == MT_CONTROL && error == 0) {
1175 if (flags & MSG_PEEK) {
1176 if (controlp)
1177 *controlp = m_copy(m, 0, m->m_len);
857caa4a 1178 m = m->m_next; /* XXX race */
984263bc 1179 } else {
984263bc 1180 if (controlp) {
6d49aa6f 1181 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
984263bc
MD
1182 if (pr->pr_domain->dom_externalize &&
1183 mtod(m, struct cmsghdr *)->cmsg_type ==
1184 SCM_RIGHTS)
1185 error = (*pr->pr_domain->dom_externalize)(m);
1186 *controlp = m;
857caa4a 1187 m = n;
984263bc 1188 } else {
6d49aa6f 1189 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
984263bc
MD
1190 }
1191 }
857caa4a 1192 if (controlp && *controlp) {
984263bc
MD
1193 orig_resid = 0;
1194 controlp = &(*controlp)->m_next;
1195 }
1196 }
857caa4a
MD
1197
1198 /*
1199 * flag OOB data.
1200 */
984263bc 1201 if (m) {
984263bc
MD
1202 type = m->m_type;
1203 if (type == MT_OOBDATA)
1204 flags |= MSG_OOB;
1205 }
857caa4a
MD
1206
1207 /*
1208 * Copy to the UIO or mbuf return chain (*mp).
1209 */
984263bc
MD
1210 moff = 0;
1211 offset = 0;
d8a9a23b 1212 while (m && resid > 0 && error == 0) {
984263bc
MD
1213 if (m->m_type == MT_OOBDATA) {
1214 if (type != MT_OOBDATA)
1215 break;
1216 } else if (type == MT_OOBDATA)
1217 break;
1218 else
1219 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
1220 ("receive 3"));
6cef7136 1221 soclrstate(so, SS_RCVATMARK);
e54488bb 1222 len = (resid > INT_MAX) ? INT_MAX : resid;
984263bc
MD
1223 if (so->so_oobmark && len > so->so_oobmark - offset)
1224 len = so->so_oobmark - offset;
1225 if (len > m->m_len - moff)
1226 len = m->m_len - moff;
d8a9a23b 1227
984263bc 1228 /*
d8a9a23b
MD
1229 * Copy out to the UIO or pass the mbufs back to the SIO.
1230 * The SIO is dealt with when we eat the mbuf, but deal
1231 * with the resid here either way.
984263bc 1232 */
d8a9a23b 1233 if (uio) {
d8a9a23b
MD
1234 uio->uio_resid = resid;
1235 error = uiomove(mtod(m, caddr_t) + moff, len, uio);
1236 resid = uio->uio_resid;
984263bc
MD
1237 if (error)
1238 goto release;
857caa4a 1239 } else {
e54488bb 1240 resid -= (size_t)len;
857caa4a
MD
1241 }
1242
1243 /*
1244 * Eat the entire mbuf or just a piece of it
1245 */
984263bc
MD
1246 if (len == m->m_len - moff) {
1247 if (m->m_flags & M_EOR)
1248 flags |= MSG_EOR;
78812139
EN
1249#ifdef SCTP
1250 if (m->m_flags & M_NOTIFICATION)
1251 flags |= MSG_NOTIFICATION;
1252#endif /* SCTP */
984263bc
MD
1253 if (flags & MSG_PEEK) {
1254 m = m->m_next;
1255 moff = 0;
1256 } else {
d8a9a23b 1257 if (sio) {
6d49aa6f
MD
1258 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
1259 sbappend(sio, m);
857caa4a 1260 m = n;
984263bc 1261 } else {
6d49aa6f 1262 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
984263bc 1263 }
984263bc
MD
1264 }
1265 } else {
857caa4a 1266 if (flags & MSG_PEEK) {
984263bc 1267 moff += len;
857caa4a 1268 } else {
d8a9a23b 1269 if (sio) {
6d49aa6f
MD
1270 n = m_copym(m, 0, len, MB_WAIT);
1271 if (n)
1272 sbappend(sio, n);
d8a9a23b 1273 }
984263bc
MD
1274 m->m_data += len;
1275 m->m_len -= len;
6d49aa6f 1276 so->so_rcv.ssb_cc -= len;
984263bc
MD
1277 }
1278 }
1279 if (so->so_oobmark) {
1280 if ((flags & MSG_PEEK) == 0) {
1281 so->so_oobmark -= len;
1282 if (so->so_oobmark == 0) {
6cef7136 1283 sosetstate(so, SS_RCVATMARK);
984263bc
MD
1284 break;
1285 }
1286 } else {
1287 offset += len;
1288 if (offset == so->so_oobmark)
1289 break;
1290 }
1291 }
1292 if (flags & MSG_EOR)
1293 break;
1294 /*
1295 * If the MSG_WAITALL flag is set (for non-atomic socket),
d8a9a23b 1296 * we must not quit until resid == 0 or an error
984263bc
MD
1297 * termination. If a signal/timeout occurs, return
1298 * with a short count but without error.
6d49aa6f 1299 * Keep signalsockbuf locked against other readers.
984263bc 1300 */
d8a9a23b
MD
1301 while ((flags & MSG_WAITALL) && m == NULL &&
1302 resid > 0 && !sosendallatonce(so) &&
6d49aa6f 1303 so->so_rcv.ssb_mb == NULL) {
984263bc
MD
1304 if (so->so_error || so->so_state & SS_CANTRCVMORE)
1305 break;
1306 /*
1307 * The window might have closed to zero, make
1308 * sure we send an ack now that we've drained
1309 * the buffer or we might end up blocking until
1310 * the idle takes over (5 seconds).
1311 */
1312 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
6b6e0885 1313 so_pru_rcvd(so, flags);
6d49aa6f 1314 error = ssb_wait(&so->so_rcv);
984263bc 1315 if (error) {
6d49aa6f 1316 ssb_unlock(&so->so_rcv);
857caa4a
MD
1317 error = 0;
1318 goto done;
984263bc 1319 }
6d49aa6f 1320 m = so->so_rcv.ssb_mb;
984263bc
MD
1321 }
1322 }
1323
857caa4a
MD
1324 /*
1325 * If an atomic read was requested but unread data still remains
1326 * in the record, set MSG_TRUNC.
1327 */
bf8a9a6f 1328 if (m && pr->pr_flags & PR_ATOMIC)
984263bc 1329 flags |= MSG_TRUNC;
857caa4a
MD
1330
1331 /*
1332 * Cleanup. If an atomic read was requested drop any unread data.
1333 */
1334 if ((flags & MSG_PEEK) == 0) {
1335 if (m && (pr->pr_flags & PR_ATOMIC))
6d49aa6f 1336 sbdroprecord(&so->so_rcv.sb);
857caa4a 1337 if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
6b6e0885 1338 so_pru_rcvd(so, flags);
984263bc 1339 }
bf8a9a6f 1340
d8a9a23b 1341 if (orig_resid == resid && orig_resid &&
984263bc 1342 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
6d49aa6f 1343 ssb_unlock(&so->so_rcv);
984263bc
MD
1344 goto restart;
1345 }
1346
1347 if (flagsp)
1348 *flagsp |= flags;
1349release:
6d49aa6f 1350 ssb_unlock(&so->so_rcv);
857caa4a 1351done:
20156c7a 1352 lwkt_reltoken(&so->so_rcv.ssb_token);
857caa4a
MD
1353 if (free_chain)
1354 m_freem(free_chain);
984263bc
MD
1355 return (error);
1356}
1357
edf5c732
MD
1358/*
1359 * Shut a socket down. Note that we do not get a frontend lock as we
1360 * want to be able to shut the socket down even if another thread is
1361 * blocked in a read(), thus waking it up.
1362 */
984263bc 1363int
c972a82f 1364soshutdown(struct socket *so, int how)
984263bc 1365{
984263bc
MD
1366 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1367 return (EINVAL);
1368
ff518922 1369 if (how != SHUT_WR) {
edf5c732 1370 /*ssb_lock(&so->so_rcv, M_WAITOK);*/
984263bc 1371 sorflush(so);
edf5c732 1372 /*ssb_unlock(&so->so_rcv);*/
ff518922 1373 }
984263bc 1374 if (how != SHUT_RD)
6b6e0885 1375 return (so_pru_shutdown(so));
984263bc
MD
1376 return (0);
1377}
1378
1379void
c972a82f 1380sorflush(struct socket *so)
984263bc 1381{
6d49aa6f 1382 struct signalsockbuf *ssb = &so->so_rcv;
1fd87d54 1383 struct protosw *pr = so->so_proto;
6d49aa6f 1384 struct signalsockbuf asb;
984263bc 1385
14343ad3 1386 atomic_set_int(&ssb->ssb_flags, SSB_NOINTR);
e43a034f 1387
ff518922 1388 lwkt_gettoken(&ssb->ssb_token);
984263bc 1389 socantrcvmore(so);
6d49aa6f 1390 asb = *ssb;
14343ad3
MD
1391
1392 /*
1393 * Can't just blow up the ssb structure here
1394 */
ff518922 1395 bzero(&ssb->sb, sizeof(ssb->sb));
14343ad3 1396 ssb->ssb_timeo = 0;
14343ad3
MD
1397 ssb->ssb_lowat = 0;
1398 ssb->ssb_hiwat = 0;
1399 ssb->ssb_mbmax = 0;
1400 atomic_clear_int(&ssb->ssb_flags, SSB_CLEAR_MASK);
1401
edf5c732 1402 if ((pr->pr_flags & PR_RIGHTS) && pr->pr_domain->dom_dispose)
6d49aa6f
MD
1403 (*pr->pr_domain->dom_dispose)(asb.ssb_mb);
1404 ssb_release(&asb, so);
edf5c732
MD
1405
1406 lwkt_reltoken(&ssb->ssb_token);
984263bc
MD
1407}
1408
1409#ifdef INET
1410static int
c972a82f 1411do_setopt_accept_filter(struct socket *so, struct sockopt *sopt)
984263bc
MD
1412{
1413 struct accept_filter_arg *afap = NULL;
1414 struct accept_filter *afp;
1415 struct so_accf *af = so->so_accf;
1416 int error = 0;
1417
1418 /* do not set/remove accept filters on non listen sockets */
1419 if ((so->so_options & SO_ACCEPTCONN) == 0) {
1420 error = EINVAL;
1421 goto out;
1422 }
1423
1424 /* removing the filter */
1425 if (sopt == NULL) {
1426 if (af != NULL) {
1427 if (af->so_accept_filter != NULL &&
1428 af->so_accept_filter->accf_destroy != NULL) {
1429 af->so_accept_filter->accf_destroy(so);
1430 }
1431 if (af->so_accept_filter_str != NULL) {
1432 FREE(af->so_accept_filter_str, M_ACCF);
1433 }
1434 FREE(af, M_ACCF);
1435 so->so_accf = NULL;
1436 }
1437 so->so_options &= ~SO_ACCEPTFILTER;
1438 return (0);
1439 }
1440 /* adding a filter */
1441 /* must remove previous filter first */
1442 if (af != NULL) {
1443 error = EINVAL;
1444 goto out;
1445 }
1446 /* don't put large objects on the kernel stack */
1447 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1448 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1449 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1450 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1451 if (error)
1452 goto out;
1453 afp = accept_filt_get(afap->af_name);
1454 if (afp == NULL) {
1455 error = ENOENT;
1456 goto out;
1457 }
e7b4468c 1458 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK | M_ZERO);
984263bc
MD
1459 if (afp->accf_create != NULL) {
1460 if (afap->af_name[0] != '\0') {
1461 int len = strlen(afap->af_name) + 1;
1462
1463 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1464 strcpy(af->so_accept_filter_str, afap->af_name);
1465 }
1466 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1467 if (af->so_accept_filter_arg == NULL) {
1468 FREE(af->so_accept_filter_str, M_ACCF);
1469 FREE(af, M_ACCF);
1470 so->so_accf = NULL;
1471 error = EINVAL;
1472 goto out;
1473 }
1474 }
1475 af->so_accept_filter = afp;
1476 so->so_accf = af;
1477 so->so_options |= SO_ACCEPTFILTER;
1478out:
1479 if (afap != NULL)
1480 FREE(afap, M_TEMP);
1481 return (error);
1482}
1483#endif /* INET */
1484
1485/*
1486 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1487 * an additional variant to handle the case where the option value needs
1488 * to be some kind of integer, but not a specific size.
1489 * In addition to their use here, these functions are also called by the
1490 * protocol-level pr_ctloutput() routines.
1491 */
1492int
c972a82f 1493sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
de0003fe
AE
1494{
1495 return soopt_to_kbuf(sopt, buf, len, minlen);
1496}
1497
1498int
1499soopt_to_kbuf(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
984263bc
MD
1500{
1501 size_t valsize;
1502
792239df 1503 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
de0003fe
AE
1504 KKASSERT(kva_p(buf));
1505
984263bc
MD
1506 /*
1507 * If the user gives us more than we wanted, we ignore it,
1508 * but if we don't get the minimum length the caller
1509 * wants, we return EINVAL. On success, sopt->sopt_valsize
1510 * is set to however much we actually retrieved.
1511 */
1512 if ((valsize = sopt->sopt_valsize) < minlen)
1513 return EINVAL;
1514 if (valsize > len)
1515 sopt->sopt_valsize = valsize = len;
1516
984263bc
MD
1517 bcopy(sopt->sopt_val, buf, valsize);
1518 return 0;
1519}
1520
e71a125f 1521
984263bc 1522int
c972a82f 1523sosetopt(struct socket *so, struct sockopt *sopt)
984263bc
MD
1524{
1525 int error, optval;
1526 struct linger l;
1527 struct timeval tv;
1528 u_long val;
14343ad3 1529 struct signalsockbuf *sotmp;
984263bc
MD
1530
1531 error = 0;
e79d388f 1532 sopt->sopt_dir = SOPT_SET;
984263bc 1533 if (sopt->sopt_level != SOL_SOCKET) {
6b6e0885 1534 if (so->so_proto && so->so_proto->pr_ctloutput) {
002c1265 1535 return (so_pr_ctloutput(so, sopt));
6b6e0885 1536 }
984263bc
MD
1537 error = ENOPROTOOPT;
1538 } else {
1539 switch (sopt->sopt_name) {
1540#ifdef INET
1541 case SO_ACCEPTFILTER:
1542 error = do_setopt_accept_filter(so, sopt);
1543 if (error)
1544 goto bad;
1545 break;
1546#endif /* INET */
1547 case SO_LINGER:
1548 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1549 if (error)
1550 goto bad;
1551
1552 so->so_linger = l.l_linger;
1553 if (l.l_onoff)
1554 so->so_options |= SO_LINGER;
1555 else
1556 so->so_options &= ~SO_LINGER;
1557 break;
1558
1559 case SO_DEBUG:
1560 case SO_KEEPALIVE:
1561 case SO_DONTROUTE:
1562 case SO_USELOOPBACK:
1563 case SO_BROADCAST:
1564 case SO_REUSEADDR:
1565 case SO_REUSEPORT:
1566 case SO_OOBINLINE:
1567 case SO_TIMESTAMP:
1568 error = sooptcopyin(sopt, &optval, sizeof optval,
1569 sizeof optval);
1570 if (error)
1571 goto bad;
1572 if (optval)
1573 so->so_options |= sopt->sopt_name;
1574 else
1575 so->so_options &= ~sopt->sopt_name;
1576 break;
1577
1578 case SO_SNDBUF:
1579 case SO_RCVBUF:
1580 case SO_SNDLOWAT:
1581 case SO_RCVLOWAT:
1582 error = sooptcopyin(sopt, &optval, sizeof optval,
1583 sizeof optval);
1584 if (error)
1585 goto bad;
1586
1587 /*
1588 * Values < 1 make no sense for any of these
1589 * options, so disallow them.
1590 */
1591 if (optval < 1) {
1592 error = EINVAL;
1593 goto bad;
1594 }
1595
1596 switch (sopt->sopt_name) {
1597 case SO_SNDBUF:
1598 case SO_RCVBUF:
6d49aa6f 1599 if (ssb_reserve(sopt->sopt_name == SO_SNDBUF ?
984263bc 1600 &so->so_snd : &so->so_rcv, (u_long)optval,
e4700d00
JH
1601 so,
1602 &curproc->p_rlimit[RLIMIT_SBSIZE]) == 0) {
984263bc
MD
1603 error = ENOBUFS;
1604 goto bad;
1605 }
14343ad3
MD
1606 sotmp = (sopt->sopt_name == SO_SNDBUF) ?
1607 &so->so_snd : &so->so_rcv;
1608 atomic_clear_int(&sotmp->ssb_flags,
1609 SSB_AUTOSIZE);
984263bc
MD
1610 break;
1611
1612 /*
1613 * Make sure the low-water is never greater than
1614 * the high-water.
1615 */
1616 case SO_SNDLOWAT:
6d49aa6f
MD
1617 so->so_snd.ssb_lowat =
1618 (optval > so->so_snd.ssb_hiwat) ?
1619 so->so_snd.ssb_hiwat : optval;
14343ad3
MD
1620 atomic_clear_int(&so->so_snd.ssb_flags,
1621 SSB_AUTOLOWAT);
984263bc
MD
1622 break;
1623 case SO_RCVLOWAT:
6d49aa6f
MD
1624 so->so_rcv.ssb_lowat =
1625 (optval > so->so_rcv.ssb_hiwat) ?
1626 so->so_rcv.ssb_hiwat : optval;
14343ad3
MD
1627 atomic_clear_int(&so->so_rcv.ssb_flags,
1628 SSB_AUTOLOWAT);
984263bc
MD
1629 break;
1630 }
1631 break;
1632
1633 case SO_SNDTIMEO:
1634 case SO_RCVTIMEO:
1635 error = sooptcopyin(sopt, &tv, sizeof tv,
1636 sizeof tv);
1637 if (error)
1638 goto bad;
1639
1640 /* assert(hz > 0); */
45546849 1641 if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
984263bc
MD
1642 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1643 error = EDOM;
1644 goto bad;
1645 }
1646 /* assert(tick > 0); */
45546849 1647 /* assert(ULONG_MAX - INT_MAX >= 1000000); */
a591f597 1648 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / ustick;
45546849 1649 if (val > INT_MAX) {
984263bc
MD
1650 error = EDOM;
1651 goto bad;
1652 }
1653 if (val == 0 && tv.tv_usec != 0)
1654 val = 1;
1655
1656 switch (sopt->sopt_name) {
1657 case SO_SNDTIMEO:
6d49aa6f 1658 so->so_snd.ssb_timeo = val;
984263bc
MD
1659 break;
1660 case SO_RCVTIMEO:
6d49aa6f 1661 so->so_rcv.ssb_timeo = val;
984263bc
MD
1662 break;
1663 }
1664 break;
1665 default:
1666 error = ENOPROTOOPT;
1667 break;
1668 }
1669 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
002c1265 1670 (void) so_pr_ctloutput(so, sopt);
984263bc
MD
1671 }
1672 }
1673bad:
1674 return (error);
1675}
1676
1677/* Helper routine for getsockopt */
1678int
f1f552f6 1679sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
984263bc 1680{
de0003fe
AE
1681 soopt_from_kbuf(sopt, buf, len);
1682 return 0;
1683}
1684
1685void
1686soopt_from_kbuf(struct sockopt *sopt, const void *buf, size_t len)
1687{
984263bc
MD
1688 size_t valsize;
1689
565d9f6f
SZ
1690 if (len == 0) {
1691 sopt->sopt_valsize = 0;
1692 return;
1693 }
1694
792239df 1695 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
de0003fe 1696 KKASSERT(kva_p(buf));
984263bc
MD
1697
1698 /*
1699 * Documented get behavior is that we always return a value,
1700 * possibly truncated to fit in the user's buffer.
1701 * Traditional behavior is that we always tell the user
1702 * precisely how much we copied, rather than something useful
1703 * like the total amount we had available for her.
1704 * Note that this interface is not idempotent; the entire answer must
1705 * generated ahead of time.
1706 */
231d276b 1707 valsize = szmin(len, sopt->sopt_valsize);
984263bc
MD
1708 sopt->sopt_valsize = valsize;
1709 if (sopt->sopt_val != 0) {
de0003fe 1710 bcopy(buf, sopt->sopt_val, valsize);
984263bc 1711 }
e71a125f
AE
1712}
1713
984263bc 1714int
c972a82f 1715sogetopt(struct socket *so, struct sockopt *sopt)
984263bc
MD
1716{
1717 int error, optval;
755c519c 1718 long optval_l;
984263bc
MD
1719 struct linger l;
1720 struct timeval tv;
51f4ca92 1721#ifdef INET
984263bc 1722 struct accept_filter_arg *afap;
51f4ca92 1723#endif
984263bc
MD
1724
1725 error = 0;
e79d388f 1726 sopt->sopt_dir = SOPT_GET;
984263bc
MD
1727 if (sopt->sopt_level != SOL_SOCKET) {
1728 if (so->so_proto && so->so_proto->pr_ctloutput) {
002c1265 1729 return (so_pr_ctloutput(so, sopt));
984263bc
MD
1730 } else
1731 return (ENOPROTOOPT);
1732 } else {
1733 switch (sopt->sopt_name) {
1734#ifdef INET
1735 case SO_ACCEPTFILTER:
1736 if ((so->so_options & SO_ACCEPTCONN) == 0)
1737 return (EINVAL);
1738 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
e7b4468c 1739 M_TEMP, M_WAITOK | M_ZERO);
984263bc
MD
1740 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1741 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1742 if (so->so_accf->so_accept_filter_str != NULL)
1743 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1744 }
1745 error = sooptcopyout(sopt, afap, sizeof(*afap));
1746 FREE(afap, M_TEMP);
1747 break;
1748#endif /* INET */
1749
1750 case SO_LINGER:
1751 l.l_onoff = so->so_options & SO_LINGER;
1752 l.l_linger = so->so_linger;
1753 error = sooptcopyout(sopt, &l, sizeof l);
1754 break;
1755
1756 case SO_USELOOPBACK:
1757 case SO_DONTROUTE:
1758 case SO_DEBUG:
1759 case SO_KEEPALIVE:
1760 case SO_REUSEADDR:
1761 case SO_REUSEPORT:
1762 case SO_BROADCAST:
1763 case SO_OOBINLINE:
1764 case SO_TIMESTAMP:
1765 optval = so->so_options & sopt->sopt_name;
1766integer:
1767 error = sooptcopyout(sopt, &optval, sizeof optval);
1768 break;
1769
1770 case SO_TYPE:
1771 optval = so->so_type;
1772 goto integer;
1773
1774 case SO_ERROR:
1775 optval = so->so_error;
1776 so->so_error = 0;
1777 goto integer;
1778
1779 case SO_SNDBUF:
6d49aa6f 1780 optval = so->so_snd.ssb_hiwat;
984263bc
MD
1781 goto integer;
1782
1783 case SO_RCVBUF:
6d49aa6f 1784 optval = so->so_rcv.ssb_hiwat;
984263bc
MD
1785 goto integer;
1786
1787 case SO_SNDLOWAT:
6d49aa6f 1788 optval = so->so_snd.ssb_lowat;
984263bc
MD
1789 goto integer;
1790
1791 case SO_RCVLOWAT:
6d49aa6f 1792 optval = so->so_rcv.ssb_lowat;
984263bc
MD
1793 goto integer;
1794
1795 case SO_SNDTIMEO:
1796 case SO_RCVTIMEO:
1797 optval = (sopt->sopt_name == SO_SNDTIMEO ?
6d49aa6f 1798 so->so_snd.ssb_timeo : so->so_rcv.ssb_timeo);
984263bc
MD
1799
1800 tv.tv_sec = optval / hz;
a591f597 1801 tv.tv_usec = (optval % hz) * ustick;
984263bc
MD
1802 error = sooptcopyout(sopt, &tv, sizeof tv);
1803 break;
1804
755c519c
SZ
1805 case SO_SNDSPACE:
1806 optval_l = ssb_space(&so->so_snd);
1807 error = sooptcopyout(sopt, &optval_l, sizeof(optval_l));
1808 break;
1809
984263bc
MD
1810 default:
1811 error = ENOPROTOOPT;
1812 break;
1813 }
1814 return (error);
1815 }
1816}
1817
1818/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1819int
1820soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1821{
1822 struct mbuf *m, *m_prev;
bf6ac9fa
JH
1823 int sopt_size = sopt->sopt_valsize, msize;
1824
1825 m = m_getl(sopt_size, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_DATA,
1826 0, &msize);
1827 if (m == NULL)
1828 return (ENOBUFS);
1829 m->m_len = min(msize, sopt_size);
984263bc
MD
1830 sopt_size -= m->m_len;
1831 *mp = m;
1832 m_prev = m;
1833
bf6ac9fa
JH
1834 while (sopt_size > 0) {
1835 m = m_getl(sopt_size, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT,
1836 MT_DATA, 0, &msize);
1837 if (m == NULL) {
984263bc 1838 m_freem(*mp);
bf6ac9fa 1839 return (ENOBUFS);
984263bc 1840 }
bf6ac9fa 1841 m->m_len = min(msize, sopt_size);
984263bc
MD
1842 sopt_size -= m->m_len;
1843 m_prev->m_next = m;
1844 m_prev = m;
1845 }
bf6ac9fa 1846 return (0);
984263bc
MD
1847}
1848
1849/* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1850int
1851soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
de0003fe
AE
1852{
1853 soopt_to_mbuf(sopt, m);
1854 return 0;
1855}
1856
1857void
1858soopt_to_mbuf(struct sockopt *sopt, struct mbuf *m)
984263bc 1859{
c3e742f9
NT
1860 size_t valsize;
1861 void *val;
984263bc 1862
792239df 1863 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
de0003fe 1864 KKASSERT(kva_p(m));
984263bc 1865 if (sopt->sopt_val == NULL)
792239df 1866 return;
c3e742f9
NT
1867 val = sopt->sopt_val;
1868 valsize = sopt->sopt_valsize;
1869 while (m != NULL && valsize >= m->m_len) {
de0003fe 1870 bcopy(val, mtod(m, char *), m->m_len);
c3e742f9
NT
1871 valsize -= m->m_len;
1872 val = (caddr_t)val + m->m_len;
984263bc
MD
1873 m = m->m_next;
1874 }
1875 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1876 panic("ip6_sooptmcopyin");
984263bc
MD
1877}
1878
de0003fe
AE
1879/* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1880int
1881soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
e71a125f 1882{
de0003fe 1883 return soopt_from_mbuf(sopt, m);
e71a125f
AE
1884}
1885
984263bc 1886int
de0003fe 1887soopt_from_mbuf(struct sockopt *sopt, struct mbuf *m)
984263bc
MD
1888{
1889 struct mbuf *m0 = m;
1890 size_t valsize = 0;
c3e742f9
NT
1891 size_t maxsize;
1892 void *val;
984263bc 1893
792239df 1894 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
de0003fe 1895 KKASSERT(kva_p(m));
984263bc
MD
1896 if (sopt->sopt_val == NULL)
1897 return 0;
c3e742f9
NT
1898 val = sopt->sopt_val;
1899 maxsize = sopt->sopt_valsize;
1900 while (m != NULL && maxsize >= m->m_len) {
de0003fe 1901 bcopy(mtod(m, char *), val, m->m_len);
c3e742f9
NT
1902 maxsize -= m->m_len;
1903 val = (caddr_t)val + m->m_len;
984263bc
MD
1904 valsize += m->m_len;
1905 m = m->m_next;
1906 }
1907 if (m != NULL) {
1908 /* enough soopt buffer should be given from user-land */
1909 m_freem(m0);
bf6ac9fa 1910 return (EINVAL);
984263bc
MD
1911 }
1912 sopt->sopt_valsize = valsize;
1913 return 0;
1914}
1915
1916void
c972a82f 1917sohasoutofband(struct socket *so)
984263bc
MD
1918{
1919 if (so->so_sigio != NULL)
1920 pgsigio(so->so_sigio, SIGURG, 0);
5b22f1a7 1921 KNOTE(&so->so_rcv.ssb_kq.ki_note, NOTE_OOB);
984263bc
MD
1922}
1923
984263bc
MD
1924int
1925sokqfilter(struct file *fp, struct knote *kn)
1926{
1927 struct socket *so = (struct socket *)kn->kn_fp->f_data;
6d49aa6f 1928 struct signalsockbuf *ssb;
984263bc
MD
1929
1930 switch (kn->kn_filter) {
1931 case EVFILT_READ:
1932 if (so->so_options & SO_ACCEPTCONN)
1933 kn->kn_fop = &solisten_filtops;
1934 else
1935 kn->kn_fop = &soread_filtops;
6d49aa6f 1936 ssb = &so->so_rcv;
984263bc
MD
1937 break;
1938 case EVFILT_WRITE:
1939 kn->kn_fop = &sowrite_filtops;
6d49aa6f 1940 ssb = &so->so_snd;
984263bc 1941 break;
73c344d3
SG
1942 case EVFILT_EXCEPT:
1943 kn->kn_fop = &soexcept_filtops;
1944 ssb = &so->so_rcv;
1945 break;
984263bc 1946 default:
b287d649 1947 return (EOPNOTSUPP);
984263bc
MD
1948 }
1949
5b22f1a7 1950 knote_insert(&ssb->ssb_kq.ki_note, kn);
14343ad3 1951 atomic_set_int(&ssb->ssb_flags, SSB_KNOTE);
984263bc
MD
1952 return (0);
1953}
1954
1955static void
1956filt_sordetach(struct knote *kn)
1957{
1958 struct socket *so = (struct socket *)kn->kn_fp->f_data;
984263bc 1959
5b22f1a7
SG
1960 knote_remove(&so->so_rcv.ssb_kq.ki_note, kn);
1961 if (SLIST_EMPTY(&so->so_rcv.ssb_kq.ki_note))
14343ad3 1962 atomic_clear_int(&so->so_rcv.ssb_flags, SSB_KNOTE);
984263bc
MD
1963}
1964
1965/*ARGSUSED*/
1966static int
1967filt_soread(struct knote *kn, long hint)
1968{
1969 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1970
73c344d3
SG
1971 if (kn->kn_sfflags & NOTE_OOB) {
1972 if ((so->so_oobmark || (so->so_state & SS_RCVATMARK))) {
1973 kn->kn_fflags |= NOTE_OOB;
1974 return (1);
1975 }
1976 return (0);
70a4a30f 1977 }
6d49aa6f 1978 kn->kn_data = so->so_rcv.ssb_cc;
8c4ed426 1979
3bcb6e5e
SZ
1980 if (so->so_state & SS_CANTRCVMORE) {
1981 /*
1982 * Only set NODATA if all data has been exhausted.
1983 */
1984 if (kn->kn_data == 0)
1985 kn->kn_flags |= EV_NODATA;
984263bc
MD
1986 kn->kn_flags |= EV_EOF;
1987 kn->kn_fflags = so->so_error;
1988 return (1);
1989 }
1990 if (so->so_error) /* temporary udp error */
1991 return (1);
1992 if (kn->kn_sfflags & NOTE_LOWAT)
1993 return (kn->kn_data >= kn->kn_sdata);
e5857bf7 1994 return ((kn->kn_data >= so->so_rcv.ssb_lowat) ||
6cef7136 1995 !TAILQ_EMPTY(&so->so_comp));
984263bc
MD
1996}
1997
1998static void
1999filt_sowdetach(struct knote *kn)
2000{
2001 struct socket *so = (struct socket *)kn->kn_fp->f_data;
984263bc 2002
5b22f1a7
SG
2003 knote_remove(&so->so_snd.ssb_kq.ki_note, kn);
2004 if (SLIST_EMPTY(&so->so_snd.ssb_kq.ki_note))
14343ad3 2005 atomic_clear_int(&so->so_snd.ssb_flags, SSB_KNOTE);
984263bc
MD
2006}
2007
2008/*ARGSUSED*/
2009static int
2010filt_sowrite(struct knote *kn, long hint)
2011{
2012 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2013
6d49aa6f 2014 kn->kn_data = ssb_space(&so->so_snd);
984263bc 2015 if (so->so_state & SS_CANTSENDMORE) {
3bcb6e5e 2016 kn->kn_flags |= (EV_EOF | EV_NODATA);
984263bc
MD
2017 kn->kn_fflags = so->so_error;
2018 return (1);
2019 }
2020 if (so->so_error) /* temporary udp error */
2021 return (1);
2022 if (((so->so_state & SS_ISCONNECTED) == 0) &&
2023 (so->so_proto->pr_flags & PR_CONNREQUIRED))
2024 return (0);
2025 if (kn->kn_sfflags & NOTE_LOWAT)
2026 return (kn->kn_data >= kn->kn_sdata);
6d49aa6f 2027 return (kn->kn_data >= so->so_snd.ssb_lowat);
984263bc
MD
2028}
2029
2030/*ARGSUSED*/
2031static int
2032filt_solisten(struct knote *kn, long hint)
2033{
2034 struct socket *so = (struct socket *)kn->kn_fp->f_data;
2035
2036 kn->kn_data = so->so_qlen;
2037 return (! TAILQ_EMPTY(&so->so_comp));
2038}