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