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