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