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