]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/uipc_socket.c
This commit was generated by cvs2svn to compensate for changes in r61521,
[FreeBSD/FreeBSD.git] / sys / kern / uipc_socket.c
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)uipc_socket.c       8.3 (Berkeley) 4/15/94
34  * $FreeBSD$
35  */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/fcntl.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/domain.h>
43 #include <sys/file.h>                   /* for struct knote */
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/event.h>
47 #include <sys/poll.h>
48 #include <sys/proc.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/resourcevar.h>
53 #include <sys/signalvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/uio.h>
56 #include <sys/jail.h>
57 #include <vm/vm_zone.h>
58
59 #include <machine/limits.h>
60
61 static int      filt_sorattach(struct knote *kn);
62 static void     filt_sordetach(struct knote *kn);
63 static int      filt_soread(struct knote *kn, long hint);
64 static int      filt_sowattach(struct knote *kn);
65 static void     filt_sowdetach(struct knote *kn);
66 static int      filt_sowrite(struct knote *kn, long hint);
67 static int      filt_solisten(struct knote *kn, long hint);
68
69 static struct filterops solisten_filtops = 
70         { 1, filt_sorattach, filt_sordetach, filt_solisten };
71
72 struct filterops so_rwfiltops[] = {
73         { 1, filt_sorattach, filt_sordetach, filt_soread },
74         { 1, filt_sowattach, filt_sowdetach, filt_sowrite },
75 };
76
77 struct  vm_zone *socket_zone;
78 so_gen_t        so_gencnt;      /* generation count for sockets */
79
80 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
81 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
82
83 SYSCTL_DECL(_kern_ipc);
84
85 static int somaxconn = SOMAXCONN;
86 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
87     &somaxconn, 0, "Maximum pending socket connection queue size");
88
89 /*
90  * Socket operation routines.
91  * These routines are called by the routines in
92  * sys_socket.c or from a system process, and
93  * implement the semantics of socket operations by
94  * switching out to the protocol specific routines.
95  */
96
97 /*
98  * Get a socket structure from our zone, and initialize it.
99  * We don't implement `waitok' yet (see comments in uipc_domain.c).
100  * Note that it would probably be better to allocate socket
101  * and PCB at the same time, but I'm not convinced that all
102  * the protocols can be easily modified to do this.
103  */
104 struct socket *
105 soalloc(waitok)
106         int waitok;
107 {
108         struct socket *so;
109
110         so = zalloci(socket_zone);
111         if (so) {
112                 /* XXX race condition for reentrant kernel */
113                 bzero(so, sizeof *so);
114                 so->so_gencnt = ++so_gencnt;
115                 so->so_zone = socket_zone;
116                 TAILQ_INIT(&so->so_aiojobq);
117         }
118         return so;
119 }
120
121 int
122 socreate(dom, aso, type, proto, p)
123         int dom;
124         struct socket **aso;
125         register int type;
126         int proto;
127         struct proc *p;
128 {
129         register struct protosw *prp;
130         register struct socket *so;
131         register int error;
132
133         if (proto)
134                 prp = pffindproto(dom, proto, type);
135         else
136                 prp = pffindtype(dom, type);
137
138         if (p->p_prison && jail_socket_unixiproute_only &&
139             prp->pr_domain->dom_family != PF_LOCAL &&
140             prp->pr_domain->dom_family != PF_INET &&
141             prp->pr_domain->dom_family != PF_ROUTE) {
142                 return (EPROTONOSUPPORT);
143         }
144
145         if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
146                 return (EPROTONOSUPPORT);
147         if (prp->pr_type != type)
148                 return (EPROTOTYPE);
149         so = soalloc(p != 0);
150         if (so == 0)
151                 return (ENOBUFS);
152
153         TAILQ_INIT(&so->so_incomp);
154         TAILQ_INIT(&so->so_comp);
155         so->so_type = type;
156         so->so_cred = p->p_ucred;
157         crhold(so->so_cred);
158         so->so_proto = prp;
159         error = (*prp->pr_usrreqs->pru_attach)(so, proto, p);
160         if (error) {
161                 so->so_state |= SS_NOFDREF;
162                 sofree(so);
163                 return (error);
164         }
165         *aso = so;
166         return (0);
167 }
168
169 int
170 sobind(so, nam, p)
171         struct socket *so;
172         struct sockaddr *nam;
173         struct proc *p;
174 {
175         int s = splnet();
176         int error;
177
178         error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
179         splx(s);
180         return (error);
181 }
182
183 void
184 sodealloc(so)
185         struct socket *so;
186 {
187
188         so->so_gencnt = ++so_gencnt;
189         if (so->so_rcv.sb_hiwat)
190                 (void)chgsbsize(so->so_cred->cr_uid,
191                     -(rlim_t)so->so_rcv.sb_hiwat);
192         if (so->so_snd.sb_hiwat)
193                 (void)chgsbsize(so->so_cred->cr_uid,
194                     -(rlim_t)so->so_snd.sb_hiwat);
195         crfree(so->so_cred);
196         zfreei(so->so_zone, so);
197 }
198
199 int
200 solisten(so, backlog, p)
201         register struct socket *so;
202         int backlog;
203         struct proc *p;
204 {
205         int s, error;
206
207         s = splnet();
208         error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p);
209         if (error) {
210                 splx(s);
211                 return (error);
212         }
213         if (TAILQ_EMPTY(&so->so_comp))
214                 so->so_options |= SO_ACCEPTCONN;
215         if (backlog < 0 || backlog > somaxconn)
216                 backlog = somaxconn;
217         so->so_qlimit = backlog;
218         splx(s);
219         return (0);
220 }
221
222 void
223 sofree(so)
224         register struct socket *so;
225 {
226         struct socket *head = so->so_head;
227
228         if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
229                 return;
230         if (head != NULL) {
231                 if (so->so_state & SS_INCOMP) {
232                         TAILQ_REMOVE(&head->so_incomp, so, so_list);
233                         head->so_incqlen--;
234                 } else if (so->so_state & SS_COMP) {
235                         /*
236                          * We must not decommission a socket that's
237                          * on the accept(2) queue.  If we do, then
238                          * accept(2) may hang after select(2) indicated
239                          * that the listening socket was ready.
240                          */
241                         return;
242                 } else {
243                         panic("sofree: not queued");
244                 }
245                 head->so_qlen--;
246                 so->so_state &= ~SS_INCOMP;
247                 so->so_head = NULL;
248         }
249         sbrelease(&so->so_snd, so);
250         sorflush(so);
251         sodealloc(so);
252 }
253
254 /*
255  * Close a socket on last file table reference removal.
256  * Initiate disconnect if connected.
257  * Free socket when disconnect complete.
258  */
259 int
260 soclose(so)
261         register struct socket *so;
262 {
263         int s = splnet();               /* conservative */
264         int error = 0;
265
266         funsetown(so->so_sigio);
267         if (so->so_options & SO_ACCEPTCONN) {
268                 struct socket *sp, *sonext;
269
270                 sp = TAILQ_FIRST(&so->so_incomp);
271                 for (; sp != NULL; sp = sonext) {
272                         sonext = TAILQ_NEXT(sp, so_list);
273                         (void) soabort(sp);
274                 }
275                 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
276                         sonext = TAILQ_NEXT(sp, so_list);
277                         /* Dequeue from so_comp since sofree() won't do it */
278                         TAILQ_REMOVE(&so->so_comp, sp, so_list);
279                         so->so_qlen--;
280                         sp->so_state &= ~SS_COMP;
281                         sp->so_head = NULL;
282                         (void) soabort(sp);
283                 }
284         }
285         if (so->so_pcb == 0)
286                 goto discard;
287         if (so->so_state & SS_ISCONNECTED) {
288                 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
289                         error = sodisconnect(so);
290                         if (error)
291                                 goto drop;
292                 }
293                 if (so->so_options & SO_LINGER) {
294                         if ((so->so_state & SS_ISDISCONNECTING) &&
295                             (so->so_state & SS_NBIO))
296                                 goto drop;
297                         while (so->so_state & SS_ISCONNECTED) {
298                                 error = tsleep((caddr_t)&so->so_timeo,
299                                     PSOCK | PCATCH, "soclos", so->so_linger * hz);
300                                 if (error)
301                                         break;
302                         }
303                 }
304         }
305 drop:
306         if (so->so_pcb) {
307                 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
308                 if (error == 0)
309                         error = error2;
310         }
311 discard:
312         if (so->so_state & SS_NOFDREF)
313                 panic("soclose: NOFDREF");
314         so->so_state |= SS_NOFDREF;
315         sofree(so);
316         splx(s);
317         return (error);
318 }
319
320 /*
321  * Must be called at splnet...
322  */
323 int
324 soabort(so)
325         struct socket *so;
326 {
327         int error;
328
329         error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
330         if (error) {
331                 sofree(so);
332                 return error;
333         }
334         return (0);
335 }
336
337 int
338 soaccept(so, nam)
339         register struct socket *so;
340         struct sockaddr **nam;
341 {
342         int s = splnet();
343         int error;
344
345         if ((so->so_state & SS_NOFDREF) == 0)
346                 panic("soaccept: !NOFDREF");
347         so->so_state &= ~SS_NOFDREF;
348         if ((so->so_state & SS_ISDISCONNECTED) == 0)
349                 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
350         else {
351                 if (nam)
352                         *nam = 0;
353                 error = 0;
354         }
355         splx(s);
356         return (error);
357 }
358
359 int
360 soconnect(so, nam, p)
361         register struct socket *so;
362         struct sockaddr *nam;
363         struct proc *p;
364 {
365         int s;
366         int error;
367
368         if (so->so_options & SO_ACCEPTCONN)
369                 return (EOPNOTSUPP);
370         s = splnet();
371         /*
372          * If protocol is connection-based, can only connect once.
373          * Otherwise, if connected, try to disconnect first.
374          * This allows user to disconnect by connecting to, e.g.,
375          * a null address.
376          */
377         if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
378             ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
379             (error = sodisconnect(so))))
380                 error = EISCONN;
381         else
382                 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p);
383         splx(s);
384         return (error);
385 }
386
387 int
388 soconnect2(so1, so2)
389         register struct socket *so1;
390         struct socket *so2;
391 {
392         int s = splnet();
393         int error;
394
395         error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
396         splx(s);
397         return (error);
398 }
399
400 int
401 sodisconnect(so)
402         register struct socket *so;
403 {
404         int s = splnet();
405         int error;
406
407         if ((so->so_state & SS_ISCONNECTED) == 0) {
408                 error = ENOTCONN;
409                 goto bad;
410         }
411         if (so->so_state & SS_ISDISCONNECTING) {
412                 error = EALREADY;
413                 goto bad;
414         }
415         error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
416 bad:
417         splx(s);
418         return (error);
419 }
420
421 #define SBLOCKWAIT(f)   (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
422 /*
423  * Send on a socket.
424  * If send must go all at once and message is larger than
425  * send buffering, then hard error.
426  * Lock against other senders.
427  * If must go all at once and not enough room now, then
428  * inform user that this would block and do nothing.
429  * Otherwise, if nonblocking, send as much as possible.
430  * The data to be sent is described by "uio" if nonzero,
431  * otherwise by the mbuf chain "top" (which must be null
432  * if uio is not).  Data provided in mbuf chain must be small
433  * enough to send all at once.
434  *
435  * Returns nonzero on error, timeout or signal; callers
436  * must check for short counts if EINTR/ERESTART are returned.
437  * Data and control buffers are freed on return.
438  */
439 int
440 sosend(so, addr, uio, top, control, flags, p)
441         register struct socket *so;
442         struct sockaddr *addr;
443         struct uio *uio;
444         struct mbuf *top;
445         struct mbuf *control;
446         int flags;
447         struct proc *p;
448 {
449         struct mbuf **mp;
450         register struct mbuf *m;
451         register long space, len, resid;
452         int clen = 0, error, s, dontroute, mlen;
453         int atomic = sosendallatonce(so) || top;
454
455         if (uio)
456                 resid = uio->uio_resid;
457         else
458                 resid = top->m_pkthdr.len;
459         /*
460          * In theory resid should be unsigned.
461          * However, space must be signed, as it might be less than 0
462          * if we over-committed, and we must use a signed comparison
463          * of space and resid.  On the other hand, a negative resid
464          * causes us to loop sending 0-length segments to the protocol.
465          *
466          * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
467          * type sockets since that's an error.
468          */
469         if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
470                 error = EINVAL;
471                 goto out;
472         }
473
474         dontroute =
475             (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
476             (so->so_proto->pr_flags & PR_ATOMIC);
477         if (p)
478                 p->p_stats->p_ru.ru_msgsnd++;
479         if (control)
480                 clen = control->m_len;
481 #define snderr(errno)   { error = errno; splx(s); goto release; }
482
483 restart:
484         error = sblock(&so->so_snd, SBLOCKWAIT(flags));
485         if (error)
486                 goto out;
487         do {
488                 s = splnet();
489                 if (so->so_state & SS_CANTSENDMORE)
490                         snderr(EPIPE);
491                 if (so->so_error) {
492                         error = so->so_error;
493                         so->so_error = 0;
494                         splx(s);
495                         goto release;
496                 }
497                 if ((so->so_state & SS_ISCONNECTED) == 0) {
498                         /*
499                          * `sendto' and `sendmsg' is allowed on a connection-
500                          * based socket if it supports implied connect.
501                          * Return ENOTCONN if not connected and no address is
502                          * supplied.
503                          */
504                         if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
505                             (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
506                                 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
507                                     !(resid == 0 && clen != 0))
508                                         snderr(ENOTCONN);
509                         } else if (addr == 0)
510                             snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
511                                    ENOTCONN : EDESTADDRREQ);
512                 }
513                 space = sbspace(&so->so_snd);
514                 if (flags & MSG_OOB)
515                         space += 1024;
516                 if ((atomic && resid > so->so_snd.sb_hiwat) ||
517                     clen > so->so_snd.sb_hiwat)
518                         snderr(EMSGSIZE);
519                 if (space < resid + clen && uio &&
520                     (atomic || space < so->so_snd.sb_lowat || space < clen)) {
521                         if (so->so_state & SS_NBIO)
522                                 snderr(EWOULDBLOCK);
523                         sbunlock(&so->so_snd);
524                         error = sbwait(&so->so_snd);
525                         splx(s);
526                         if (error)
527                                 goto out;
528                         goto restart;
529                 }
530                 splx(s);
531                 mp = &top;
532                 space -= clen;
533                 do {
534                     if (uio == NULL) {
535                         /*
536                          * Data is prepackaged in "top".
537                          */
538                         resid = 0;
539                         if (flags & MSG_EOR)
540                                 top->m_flags |= M_EOR;
541                     } else do {
542                         if (top == 0) {
543                                 MGETHDR(m, M_WAIT, MT_DATA);
544                                 if (m == NULL) {
545                                         error = ENOBUFS;
546                                         goto release;
547                                 }
548                                 mlen = MHLEN;
549                                 m->m_pkthdr.len = 0;
550                                 m->m_pkthdr.rcvif = (struct ifnet *)0;
551                         } else {
552                                 MGET(m, M_WAIT, MT_DATA);
553                                 if (m == NULL) {
554                                         error = ENOBUFS;
555                                         goto release;
556                                 }
557                                 mlen = MLEN;
558                         }
559                         if (resid >= MINCLSIZE) {
560                                 MCLGET(m, M_WAIT);
561                                 if ((m->m_flags & M_EXT) == 0)
562                                         goto nopages;
563                                 mlen = MCLBYTES;
564                                 len = min(min(mlen, resid), space);
565                         } else {
566 nopages:
567                                 len = min(min(mlen, resid), space);
568                                 /*
569                                  * For datagram protocols, leave room
570                                  * for protocol headers in first mbuf.
571                                  */
572                                 if (atomic && top == 0 && len < mlen)
573                                         MH_ALIGN(m, len);
574                         }
575                         space -= len;
576                         error = uiomove(mtod(m, caddr_t), (int)len, uio);
577                         resid = uio->uio_resid;
578                         m->m_len = len;
579                         *mp = m;
580                         top->m_pkthdr.len += len;
581                         if (error)
582                                 goto release;
583                         mp = &m->m_next;
584                         if (resid <= 0) {
585                                 if (flags & MSG_EOR)
586                                         top->m_flags |= M_EOR;
587                                 break;
588                         }
589                     } while (space > 0 && atomic);
590                     if (dontroute)
591                             so->so_options |= SO_DONTROUTE;
592                     s = splnet();                               /* XXX */
593                     /*
594                      * XXX all the SS_CANTSENDMORE checks previously
595                      * done could be out of date.  We could have recieved
596                      * a reset packet in an interrupt or maybe we slept
597                      * while doing page faults in uiomove() etc. We could
598                      * probably recheck again inside the splnet() protection
599                      * here, but there are probably other places that this
600                      * also happens.  We must rethink this.
601                      */
602                     error = (*so->so_proto->pr_usrreqs->pru_send)(so,
603                         (flags & MSG_OOB) ? PRUS_OOB :
604                         /*
605                          * If the user set MSG_EOF, the protocol
606                          * understands this flag and nothing left to
607                          * send then use PRU_SEND_EOF instead of PRU_SEND.
608                          */
609                         ((flags & MSG_EOF) &&
610                          (so->so_proto->pr_flags & PR_IMPLOPCL) &&
611                          (resid <= 0)) ?
612                                 PRUS_EOF :
613                         /* If there is more to send set PRUS_MORETOCOME */
614                         (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
615                         top, addr, control, p);
616                     splx(s);
617                     if (dontroute)
618                             so->so_options &= ~SO_DONTROUTE;
619                     clen = 0;
620                     control = 0;
621                     top = 0;
622                     mp = &top;
623                     if (error)
624                         goto release;
625                 } while (resid && space > 0);
626         } while (resid);
627
628 release:
629         sbunlock(&so->so_snd);
630 out:
631         if (top)
632                 m_freem(top);
633         if (control)
634                 m_freem(control);
635         return (error);
636 }
637
638 /*
639  * Implement receive operations on a socket.
640  * We depend on the way that records are added to the sockbuf
641  * by sbappend*.  In particular, each record (mbufs linked through m_next)
642  * must begin with an address if the protocol so specifies,
643  * followed by an optional mbuf or mbufs containing ancillary data,
644  * and then zero or more mbufs of data.
645  * In order to avoid blocking network interrupts for the entire time here,
646  * we splx() while doing the actual copy to user space.
647  * Although the sockbuf is locked, new data may still be appended,
648  * and thus we must maintain consistency of the sockbuf during that time.
649  *
650  * The caller may receive the data as a single mbuf chain by supplying
651  * an mbuf **mp0 for use in returning the chain.  The uio is then used
652  * only for the count in uio_resid.
653  */
654 int
655 soreceive(so, psa, uio, mp0, controlp, flagsp)
656         register struct socket *so;
657         struct sockaddr **psa;
658         struct uio *uio;
659         struct mbuf **mp0;
660         struct mbuf **controlp;
661         int *flagsp;
662 {
663         register struct mbuf *m, **mp;
664         register int flags, len, error, s, offset;
665         struct protosw *pr = so->so_proto;
666         struct mbuf *nextrecord;
667         int moff, type = 0;
668         int orig_resid = uio->uio_resid;
669
670         mp = mp0;
671         if (psa)
672                 *psa = 0;
673         if (controlp)
674                 *controlp = 0;
675         if (flagsp)
676                 flags = *flagsp &~ MSG_EOR;
677         else
678                 flags = 0;
679         if (flags & MSG_OOB) {
680                 m = m_get(M_WAIT, MT_DATA);
681                 if (m == NULL)
682                         return (ENOBUFS);
683                 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
684                 if (error)
685                         goto bad;
686                 do {
687                         error = uiomove(mtod(m, caddr_t),
688                             (int) min(uio->uio_resid, m->m_len), uio);
689                         m = m_free(m);
690                 } while (uio->uio_resid && error == 0 && m);
691 bad:
692                 if (m)
693                         m_freem(m);
694                 return (error);
695         }
696         if (mp)
697                 *mp = (struct mbuf *)0;
698         if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
699                 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
700
701 restart:
702         error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
703         if (error)
704                 return (error);
705         s = splnet();
706
707         m = so->so_rcv.sb_mb;
708         /*
709          * If we have less data than requested, block awaiting more
710          * (subject to any timeout) if:
711          *   1. the current count is less than the low water mark, or
712          *   2. MSG_WAITALL is set, and it is possible to do the entire
713          *      receive operation at once if we block (resid <= hiwat).
714          *   3. MSG_DONTWAIT is not set
715          * If MSG_WAITALL is set but resid is larger than the receive buffer,
716          * we have to do the receive in sections, and thus risk returning
717          * a short count if a timeout or signal occurs after we start.
718          */
719         if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
720             so->so_rcv.sb_cc < uio->uio_resid) &&
721             (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
722             ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
723             m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
724                 KASSERT(m != 0 || !so->so_rcv.sb_cc, ("receive 1"));
725                 if (so->so_error) {
726                         if (m)
727                                 goto dontblock;
728                         error = so->so_error;
729                         if ((flags & MSG_PEEK) == 0)
730                                 so->so_error = 0;
731                         goto release;
732                 }
733                 if (so->so_state & SS_CANTRCVMORE) {
734                         if (m)
735                                 goto dontblock;
736                         else
737                                 goto release;
738                 }
739                 for (; m; m = m->m_next)
740                         if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
741                                 m = so->so_rcv.sb_mb;
742                                 goto dontblock;
743                         }
744                 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
745                     (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
746                         error = ENOTCONN;
747                         goto release;
748                 }
749                 if (uio->uio_resid == 0)
750                         goto release;
751                 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
752                         error = EWOULDBLOCK;
753                         goto release;
754                 }
755                 sbunlock(&so->so_rcv);
756                 error = sbwait(&so->so_rcv);
757                 splx(s);
758                 if (error)
759                         return (error);
760                 goto restart;
761         }
762 dontblock:
763         if (uio->uio_procp)
764                 uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
765         nextrecord = m->m_nextpkt;
766         if (pr->pr_flags & PR_ADDR) {
767                 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
768                 orig_resid = 0;
769                 if (psa)
770                         *psa = dup_sockaddr(mtod(m, struct sockaddr *),
771                                             mp0 == 0);
772                 if (flags & MSG_PEEK) {
773                         m = m->m_next;
774                 } else {
775                         sbfree(&so->so_rcv, m);
776                         MFREE(m, so->so_rcv.sb_mb);
777                         m = so->so_rcv.sb_mb;
778                 }
779         }
780         while (m && m->m_type == MT_CONTROL && error == 0) {
781                 if (flags & MSG_PEEK) {
782                         if (controlp)
783                                 *controlp = m_copy(m, 0, m->m_len);
784                         m = m->m_next;
785                 } else {
786                         sbfree(&so->so_rcv, m);
787                         if (controlp) {
788                                 if (pr->pr_domain->dom_externalize &&
789                                     mtod(m, struct cmsghdr *)->cmsg_type ==
790                                     SCM_RIGHTS)
791                                    error = (*pr->pr_domain->dom_externalize)(m);
792                                 *controlp = m;
793                                 so->so_rcv.sb_mb = m->m_next;
794                                 m->m_next = 0;
795                                 m = so->so_rcv.sb_mb;
796                         } else {
797                                 MFREE(m, so->so_rcv.sb_mb);
798                                 m = so->so_rcv.sb_mb;
799                         }
800                 }
801                 if (controlp) {
802                         orig_resid = 0;
803                         controlp = &(*controlp)->m_next;
804                 }
805         }
806         if (m) {
807                 if ((flags & MSG_PEEK) == 0)
808                         m->m_nextpkt = nextrecord;
809                 type = m->m_type;
810                 if (type == MT_OOBDATA)
811                         flags |= MSG_OOB;
812         }
813         moff = 0;
814         offset = 0;
815         while (m && uio->uio_resid > 0 && error == 0) {
816                 if (m->m_type == MT_OOBDATA) {
817                         if (type != MT_OOBDATA)
818                                 break;
819                 } else if (type == MT_OOBDATA)
820                         break;
821                 else
822                     KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
823                         ("receive 3"));
824                 so->so_state &= ~SS_RCVATMARK;
825                 len = uio->uio_resid;
826                 if (so->so_oobmark && len > so->so_oobmark - offset)
827                         len = so->so_oobmark - offset;
828                 if (len > m->m_len - moff)
829                         len = m->m_len - moff;
830                 /*
831                  * If mp is set, just pass back the mbufs.
832                  * Otherwise copy them out via the uio, then free.
833                  * Sockbuf must be consistent here (points to current mbuf,
834                  * it points to next record) when we drop priority;
835                  * we must note any additions to the sockbuf when we
836                  * block interrupts again.
837                  */
838                 if (mp == 0) {
839                         splx(s);
840                         error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
841                         s = splnet();
842                         if (error)
843                                 goto release;
844                 } else
845                         uio->uio_resid -= len;
846                 if (len == m->m_len - moff) {
847                         if (m->m_flags & M_EOR)
848                                 flags |= MSG_EOR;
849                         if (flags & MSG_PEEK) {
850                                 m = m->m_next;
851                                 moff = 0;
852                         } else {
853                                 nextrecord = m->m_nextpkt;
854                                 sbfree(&so->so_rcv, m);
855                                 if (mp) {
856                                         *mp = m;
857                                         mp = &m->m_next;
858                                         so->so_rcv.sb_mb = m = m->m_next;
859                                         *mp = (struct mbuf *)0;
860                                 } else {
861                                         MFREE(m, so->so_rcv.sb_mb);
862                                         m = so->so_rcv.sb_mb;
863                                 }
864                                 if (m)
865                                         m->m_nextpkt = nextrecord;
866                         }
867                 } else {
868                         if (flags & MSG_PEEK)
869                                 moff += len;
870                         else {
871                                 if (mp)
872                                         *mp = m_copym(m, 0, len, M_WAIT);
873                                 m->m_data += len;
874                                 m->m_len -= len;
875                                 so->so_rcv.sb_cc -= len;
876                         }
877                 }
878                 if (so->so_oobmark) {
879                         if ((flags & MSG_PEEK) == 0) {
880                                 so->so_oobmark -= len;
881                                 if (so->so_oobmark == 0) {
882                                         so->so_state |= SS_RCVATMARK;
883                                         break;
884                                 }
885                         } else {
886                                 offset += len;
887                                 if (offset == so->so_oobmark)
888                                         break;
889                         }
890                 }
891                 if (flags & MSG_EOR)
892                         break;
893                 /*
894                  * If the MSG_WAITALL flag is set (for non-atomic socket),
895                  * we must not quit until "uio->uio_resid == 0" or an error
896                  * termination.  If a signal/timeout occurs, return
897                  * with a short count but without error.
898                  * Keep sockbuf locked against other readers.
899                  */
900                 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
901                     !sosendallatonce(so) && !nextrecord) {
902                         if (so->so_error || so->so_state & SS_CANTRCVMORE)
903                                 break;
904                         error = sbwait(&so->so_rcv);
905                         if (error) {
906                                 sbunlock(&so->so_rcv);
907                                 splx(s);
908                                 return (0);
909                         }
910                         m = so->so_rcv.sb_mb;
911                         if (m)
912                                 nextrecord = m->m_nextpkt;
913                 }
914         }
915
916         if (m && pr->pr_flags & PR_ATOMIC) {
917                 flags |= MSG_TRUNC;
918                 if ((flags & MSG_PEEK) == 0)
919                         (void) sbdroprecord(&so->so_rcv);
920         }
921         if ((flags & MSG_PEEK) == 0) {
922                 if (m == 0)
923                         so->so_rcv.sb_mb = nextrecord;
924                 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
925                         (*pr->pr_usrreqs->pru_rcvd)(so, flags);
926         }
927         if (orig_resid == uio->uio_resid && orig_resid &&
928             (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
929                 sbunlock(&so->so_rcv);
930                 splx(s);
931                 goto restart;
932         }
933
934         if (flagsp)
935                 *flagsp |= flags;
936 release:
937         sbunlock(&so->so_rcv);
938         splx(s);
939         return (error);
940 }
941
942 int
943 soshutdown(so, how)
944         register struct socket *so;
945         register int how;
946 {
947         register struct protosw *pr = so->so_proto;
948
949         how++;
950         if (how & FREAD)
951                 sorflush(so);
952         if (how & FWRITE)
953                 return ((*pr->pr_usrreqs->pru_shutdown)(so));
954         return (0);
955 }
956
957 void
958 sorflush(so)
959         register struct socket *so;
960 {
961         register struct sockbuf *sb = &so->so_rcv;
962         register struct protosw *pr = so->so_proto;
963         register int s;
964         struct sockbuf asb;
965
966         sb->sb_flags |= SB_NOINTR;
967         (void) sblock(sb, M_WAITOK);
968         s = splimp();
969         socantrcvmore(so);
970         sbunlock(sb);
971         asb = *sb;
972         bzero((caddr_t)sb, sizeof (*sb));
973         splx(s);
974         if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
975                 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
976         sbrelease(&asb, so);
977 }
978
979 /*
980  * Perhaps this routine, and sooptcopyout(), below, ought to come in
981  * an additional variant to handle the case where the option value needs
982  * to be some kind of integer, but not a specific size.
983  * In addition to their use here, these functions are also called by the
984  * protocol-level pr_ctloutput() routines.
985  */
986 int
987 sooptcopyin(sopt, buf, len, minlen)
988         struct  sockopt *sopt;
989         void    *buf;
990         size_t  len;
991         size_t  minlen;
992 {
993         size_t  valsize;
994
995         /*
996          * If the user gives us more than we wanted, we ignore it,
997          * but if we don't get the minimum length the caller
998          * wants, we return EINVAL.  On success, sopt->sopt_valsize
999          * is set to however much we actually retrieved.
1000          */
1001         if ((valsize = sopt->sopt_valsize) < minlen)
1002                 return EINVAL;
1003         if (valsize > len)
1004                 sopt->sopt_valsize = valsize = len;
1005
1006         if (sopt->sopt_p != 0)
1007                 return (copyin(sopt->sopt_val, buf, valsize));
1008
1009         bcopy(sopt->sopt_val, buf, valsize);
1010         return 0;
1011 }
1012
1013 int
1014 sosetopt(so, sopt)
1015         struct socket *so;
1016         struct sockopt *sopt;
1017 {
1018         int     error, optval;
1019         struct  linger l;
1020         struct  timeval tv;
1021         u_long  val;
1022
1023         error = 0;
1024         if (sopt->sopt_level != SOL_SOCKET) {
1025                 if (so->so_proto && so->so_proto->pr_ctloutput)
1026                         return ((*so->so_proto->pr_ctloutput)
1027                                   (so, sopt));
1028                 error = ENOPROTOOPT;
1029         } else {
1030                 switch (sopt->sopt_name) {
1031                 case SO_LINGER:
1032                         error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1033                         if (error)
1034                                 goto bad;
1035
1036                         so->so_linger = l.l_linger;
1037                         if (l.l_onoff)
1038                                 so->so_options |= SO_LINGER;
1039                         else
1040                                 so->so_options &= ~SO_LINGER;
1041                         break;
1042
1043                 case SO_DEBUG:
1044                 case SO_KEEPALIVE:
1045                 case SO_DONTROUTE:
1046                 case SO_USELOOPBACK:
1047                 case SO_BROADCAST:
1048                 case SO_REUSEADDR:
1049                 case SO_REUSEPORT:
1050                 case SO_OOBINLINE:
1051                 case SO_TIMESTAMP:
1052                         error = sooptcopyin(sopt, &optval, sizeof optval,
1053                                             sizeof optval);
1054                         if (error)
1055                                 goto bad;
1056                         if (optval)
1057                                 so->so_options |= sopt->sopt_name;
1058                         else
1059                                 so->so_options &= ~sopt->sopt_name;
1060                         break;
1061
1062                 case SO_SNDBUF:
1063                 case SO_RCVBUF:
1064                 case SO_SNDLOWAT:
1065                 case SO_RCVLOWAT:
1066                         error = sooptcopyin(sopt, &optval, sizeof optval,
1067                                             sizeof optval);
1068                         if (error)
1069                                 goto bad;
1070
1071                         /*
1072                          * Values < 1 make no sense for any of these
1073                          * options, so disallow them.
1074                          */
1075                         if (optval < 1) {
1076                                 error = EINVAL;
1077                                 goto bad;
1078                         }
1079
1080                         switch (sopt->sopt_name) {
1081                         case SO_SNDBUF:
1082                         case SO_RCVBUF:
1083                                 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1084                                     &so->so_snd : &so->so_rcv, (u_long)optval,
1085                                     so, curproc) == 0) {
1086                                         error = ENOBUFS;
1087                                         goto bad;
1088                                 }
1089                                 break;
1090
1091                         /*
1092                          * Make sure the low-water is never greater than
1093                          * the high-water.
1094                          */
1095                         case SO_SNDLOWAT:
1096                                 so->so_snd.sb_lowat =
1097                                     (optval > so->so_snd.sb_hiwat) ?
1098                                     so->so_snd.sb_hiwat : optval;
1099                                 break;
1100                         case SO_RCVLOWAT:
1101                                 so->so_rcv.sb_lowat =
1102                                     (optval > so->so_rcv.sb_hiwat) ?
1103                                     so->so_rcv.sb_hiwat : optval;
1104                                 break;
1105                         }
1106                         break;
1107
1108                 case SO_SNDTIMEO:
1109                 case SO_RCVTIMEO:
1110                         error = sooptcopyin(sopt, &tv, sizeof tv,
1111                                             sizeof tv);
1112                         if (error)
1113                                 goto bad;
1114
1115                         /* assert(hz > 0); */
1116                         if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1117                             tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1118                                 error = EDOM;
1119                                 goto bad;
1120                         }
1121                         /* assert(tick > 0); */
1122                         /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1123                         val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1124                         if (val > SHRT_MAX) {
1125                                 error = EDOM;
1126                                 goto bad;
1127                         }
1128
1129                         switch (sopt->sopt_name) {
1130                         case SO_SNDTIMEO:
1131                                 so->so_snd.sb_timeo = val;
1132                                 break;
1133                         case SO_RCVTIMEO:
1134                                 so->so_rcv.sb_timeo = val;
1135                                 break;
1136                         }
1137                         break;
1138
1139                 default:
1140                         error = ENOPROTOOPT;
1141                         break;
1142                 }
1143                 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1144                         (void) ((*so->so_proto->pr_ctloutput)
1145                                   (so, sopt));
1146                 }
1147         }
1148 bad:
1149         return (error);
1150 }
1151
1152 /* Helper routine for getsockopt */
1153 int
1154 sooptcopyout(sopt, buf, len)
1155         struct  sockopt *sopt;
1156         void    *buf;
1157         size_t  len;
1158 {
1159         int     error;
1160         size_t  valsize;
1161
1162         error = 0;
1163
1164         /*
1165          * Documented get behavior is that we always return a value,
1166          * possibly truncated to fit in the user's buffer.
1167          * Traditional behavior is that we always tell the user
1168          * precisely how much we copied, rather than something useful
1169          * like the total amount we had available for her.
1170          * Note that this interface is not idempotent; the entire answer must
1171          * generated ahead of time.
1172          */
1173         valsize = min(len, sopt->sopt_valsize);
1174         sopt->sopt_valsize = valsize;
1175         if (sopt->sopt_val != 0) {
1176                 if (sopt->sopt_p != 0)
1177                         error = copyout(buf, sopt->sopt_val, valsize);
1178                 else
1179                         bcopy(buf, sopt->sopt_val, valsize);
1180         }
1181         return error;
1182 }
1183
1184 int
1185 sogetopt(so, sopt)
1186         struct socket *so;
1187         struct sockopt *sopt;
1188 {
1189         int     error, optval;
1190         struct  linger l;
1191         struct  timeval tv;
1192
1193         error = 0;
1194         if (sopt->sopt_level != SOL_SOCKET) {
1195                 if (so->so_proto && so->so_proto->pr_ctloutput) {
1196                         return ((*so->so_proto->pr_ctloutput)
1197                                   (so, sopt));
1198                 } else
1199                         return (ENOPROTOOPT);
1200         } else {
1201                 switch (sopt->sopt_name) {
1202                 case SO_LINGER:
1203                         l.l_onoff = so->so_options & SO_LINGER;
1204                         l.l_linger = so->so_linger;
1205                         error = sooptcopyout(sopt, &l, sizeof l);
1206                         break;
1207
1208                 case SO_USELOOPBACK:
1209                 case SO_DONTROUTE:
1210                 case SO_DEBUG:
1211                 case SO_KEEPALIVE:
1212                 case SO_REUSEADDR:
1213                 case SO_REUSEPORT:
1214                 case SO_BROADCAST:
1215                 case SO_OOBINLINE:
1216                 case SO_TIMESTAMP:
1217                         optval = so->so_options & sopt->sopt_name;
1218 integer:
1219                         error = sooptcopyout(sopt, &optval, sizeof optval);
1220                         break;
1221
1222                 case SO_TYPE:
1223                         optval = so->so_type;
1224                         goto integer;
1225
1226                 case SO_ERROR:
1227                         optval = so->so_error;
1228                         so->so_error = 0;
1229                         goto integer;
1230
1231                 case SO_SNDBUF:
1232                         optval = so->so_snd.sb_hiwat;
1233                         goto integer;
1234
1235                 case SO_RCVBUF:
1236                         optval = so->so_rcv.sb_hiwat;
1237                         goto integer;
1238
1239                 case SO_SNDLOWAT:
1240                         optval = so->so_snd.sb_lowat;
1241                         goto integer;
1242
1243                 case SO_RCVLOWAT:
1244                         optval = so->so_rcv.sb_lowat;
1245                         goto integer;
1246
1247                 case SO_SNDTIMEO:
1248                 case SO_RCVTIMEO:
1249                         optval = (sopt->sopt_name == SO_SNDTIMEO ?
1250                                   so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1251
1252                         tv.tv_sec = optval / hz;
1253                         tv.tv_usec = (optval % hz) * tick;
1254                         error = sooptcopyout(sopt, &tv, sizeof tv);
1255                         break;                  
1256
1257                 default:
1258                         error = ENOPROTOOPT;
1259                         break;
1260                 }
1261                 return (error);
1262         }
1263 }
1264
1265 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1266 int
1267 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1268 {
1269         struct mbuf *m, *m_prev;
1270         int sopt_size = sopt->sopt_valsize;
1271
1272         MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1273         if (m == 0)
1274                 return ENOBUFS;
1275         if (sopt_size > MLEN) {
1276                 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1277                 if ((m->m_flags & M_EXT) == 0) {
1278                         m_free(m);
1279                         return ENOBUFS;
1280                 }
1281                 m->m_len = min(MCLBYTES, sopt_size);
1282         } else {
1283                 m->m_len = min(MLEN, sopt_size);
1284         }
1285         sopt_size -= m->m_len;
1286         *mp = m;
1287         m_prev = m;
1288
1289         while (sopt_size) {
1290                 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1291                 if (m == 0) {
1292                         m_freem(*mp);
1293                         return ENOBUFS;
1294                 }
1295                 if (sopt_size > MLEN) {
1296                         MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1297                         if ((m->m_flags & M_EXT) == 0) {
1298                                 m_freem(*mp);
1299                                 return ENOBUFS;
1300                         }
1301                         m->m_len = min(MCLBYTES, sopt_size);
1302                 } else {
1303                         m->m_len = min(MLEN, sopt_size);
1304                 }
1305                 sopt_size -= m->m_len;
1306                 m_prev->m_next = m;
1307                 m_prev = m;
1308         }
1309         return 0;
1310 }
1311
1312 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1313 int
1314 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1315 {
1316         struct mbuf *m0 = m;
1317
1318         if (sopt->sopt_val == NULL)
1319                 return 0;
1320         while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1321                 if (sopt->sopt_p != NULL) {
1322                         int error;
1323
1324                         error = copyin(sopt->sopt_val, mtod(m, char *),
1325                                        m->m_len);
1326                         if (error != 0) {
1327                                 m_freem(m0);
1328                                 return(error);
1329                         }
1330                 } else
1331                         bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1332                 sopt->sopt_valsize -= m->m_len;
1333                 (caddr_t)sopt->sopt_val += m->m_len;
1334                 m = m->m_next;
1335         }
1336         if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1337                 panic("ip6_sooptmcopyin");
1338         return 0;
1339 }
1340
1341 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1342 int
1343 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1344 {
1345         struct mbuf *m0 = m;
1346         size_t valsize = 0;
1347
1348         if (sopt->sopt_val == NULL)
1349                 return 0;
1350         while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1351                 if (sopt->sopt_p != NULL) {
1352                         int error;
1353
1354                         error = copyout(mtod(m, char *), sopt->sopt_val,
1355                                        m->m_len);
1356                         if (error != 0) {
1357                                 m_freem(m0);
1358                                 return(error);
1359                         }
1360                 } else
1361                         bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1362                sopt->sopt_valsize -= m->m_len;
1363                (caddr_t)sopt->sopt_val += m->m_len;
1364                valsize += m->m_len;
1365                m = m->m_next;
1366         }
1367         if (m != NULL) {
1368                 /* enough soopt buffer should be given from user-land */
1369                 m_freem(m0);
1370                 return(EINVAL);
1371         }
1372         sopt->sopt_valsize = valsize;
1373         return 0;
1374 }
1375
1376 void
1377 sohasoutofband(so)
1378         register struct socket *so;
1379 {
1380         if (so->so_sigio != NULL)
1381                 pgsigio(so->so_sigio, SIGURG, 0);
1382         selwakeup(&so->so_rcv.sb_sel);
1383 }
1384
1385 int
1386 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p)
1387 {
1388         int revents = 0;
1389         int s = splnet();
1390
1391         if (events & (POLLIN | POLLRDNORM))
1392                 if (soreadable(so))
1393                         revents |= events & (POLLIN | POLLRDNORM);
1394
1395         if (events & (POLLOUT | POLLWRNORM))
1396                 if (sowriteable(so))
1397                         revents |= events & (POLLOUT | POLLWRNORM);
1398
1399         if (events & (POLLPRI | POLLRDBAND))
1400                 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1401                         revents |= events & (POLLPRI | POLLRDBAND);
1402
1403         if (revents == 0) {
1404                 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1405                         selrecord(p, &so->so_rcv.sb_sel);
1406                         so->so_rcv.sb_flags |= SB_SEL;
1407                 }
1408
1409                 if (events & (POLLOUT | POLLWRNORM)) {
1410                         selrecord(p, &so->so_snd.sb_sel);
1411                         so->so_snd.sb_flags |= SB_SEL;
1412                 }
1413         }
1414
1415         splx(s);
1416         return (revents);
1417 }
1418
1419 static int
1420 filt_sorattach(struct knote *kn)
1421 {
1422         struct socket *so = (struct socket *)kn->kn_fp->f_data;
1423         int s = splnet();
1424
1425         if (so->so_options & SO_ACCEPTCONN)
1426                 kn->kn_fop = &solisten_filtops;
1427         SLIST_INSERT_HEAD(&so->so_rcv.sb_sel.si_note, kn, kn_selnext);
1428         so->so_rcv.sb_flags |= SB_KNOTE;
1429         splx(s);
1430         return (0);
1431 }
1432
1433 static void
1434 filt_sordetach(struct knote *kn)
1435 {
1436         struct socket *so = (struct socket *)kn->kn_fp->f_data;
1437         int s = splnet();
1438
1439         SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
1440         if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
1441                 so->so_rcv.sb_flags &= ~SB_KNOTE;
1442         splx(s);
1443 }
1444
1445 /*ARGSUSED*/
1446 static int
1447 filt_soread(struct knote *kn, long hint)
1448 {
1449         struct socket *so = (struct socket *)kn->kn_fp->f_data;
1450
1451         kn->kn_data = so->so_rcv.sb_cc;
1452         if (so->so_state & SS_CANTRCVMORE) {
1453                 kn->kn_flags |= EV_EOF; 
1454                 return (1);
1455         }
1456         return (kn->kn_data > 0);
1457 }
1458
1459 static int
1460 filt_sowattach(struct knote *kn)
1461 {
1462         struct socket *so = (struct socket *)kn->kn_fp->f_data;
1463         int s = splnet();
1464
1465         SLIST_INSERT_HEAD(&so->so_snd.sb_sel.si_note, kn, kn_selnext);
1466         so->so_snd.sb_flags |= SB_KNOTE;
1467         splx(s);
1468         return (0);
1469 }
1470
1471 static void
1472 filt_sowdetach(struct knote *kn)
1473 {
1474         struct socket *so = (struct socket *)kn->kn_fp->f_data;
1475         int s = splnet();
1476
1477         SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
1478         if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
1479                 so->so_snd.sb_flags &= ~SB_KNOTE;
1480         splx(s);
1481 }
1482
1483 /*ARGSUSED*/
1484 static int
1485 filt_sowrite(struct knote *kn, long hint)
1486 {
1487         struct socket *so = (struct socket *)kn->kn_fp->f_data;
1488
1489         kn->kn_data = sbspace(&so->so_snd);
1490         if (so->so_state & SS_CANTSENDMORE) {
1491                 kn->kn_flags |= EV_EOF; 
1492                 return (1);
1493         }
1494         if (((so->so_state & SS_ISCONNECTED) == 0) &&
1495             (so->so_proto->pr_flags & PR_CONNREQUIRED))
1496                 return (0);
1497         return (kn->kn_data >= so->so_snd.sb_lowat);
1498 }
1499
1500 /*ARGSUSED*/
1501 static int
1502 filt_solisten(struct knote *kn, long hint)
1503 {
1504         struct socket *so = (struct socket *)kn->kn_fp->f_data;
1505
1506         kn->kn_data = so->so_qlen - so->so_incqlen;
1507         return (! TAILQ_EMPTY(&so->so_comp));
1508 }