uftdi(4) related usbdevs work:
[dragonfly.git] / sys / kern / uipc_socket.c
blob284348667a322937ec3a20397ea176ca1acb91dc
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.
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.
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
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.
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.
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.45 2007/04/22 01:13:10 dillon Exp $
71 #include "opt_inet.h"
72 #include "opt_sctp.h"
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/poll.h>
85 #include <sys/proc.h>
86 #include <sys/protosw.h>
87 #include <sys/socket.h>
88 #include <sys/socketvar.h>
89 #include <sys/socketops.h>
90 #include <sys/resourcevar.h>
91 #include <sys/signalvar.h>
92 #include <sys/sysctl.h>
93 #include <sys/uio.h>
94 #include <sys/jail.h>
95 #include <vm/vm_zone.h>
97 #include <sys/thread2.h>
99 #include <machine/limits.h>
101 #ifdef INET
102 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
103 #endif /* INET */
105 static void filt_sordetach(struct knote *kn);
106 static int filt_soread(struct knote *kn, long hint);
107 static void filt_sowdetach(struct knote *kn);
108 static int filt_sowrite(struct knote *kn, long hint);
109 static int filt_solisten(struct knote *kn, long hint);
111 static struct filterops solisten_filtops =
112 { 1, NULL, filt_sordetach, filt_solisten };
113 static struct filterops soread_filtops =
114 { 1, NULL, filt_sordetach, filt_soread };
115 static struct filterops sowrite_filtops =
116 { 1, NULL, filt_sowdetach, filt_sowrite };
118 struct vm_zone *socket_zone;
120 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
121 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
124 static int somaxconn = SOMAXCONN;
125 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
126 &somaxconn, 0, "Maximum pending socket connection queue size");
129 * Socket operation routines.
130 * These routines are called by the routines in
131 * sys_socket.c or from a system process, and
132 * implement the semantics of socket operations by
133 * switching out to the protocol specific routines.
137 * Get a socket structure from our zone, and initialize it.
138 * We don't implement `waitok' yet (see comments in uipc_domain.c).
139 * Note that it would probably be better to allocate socket
140 * and PCB at the same time, but I'm not convinced that all
141 * the protocols can be easily modified to do this.
143 struct socket *
144 soalloc(int waitok)
146 struct socket *so;
148 so = zalloc(socket_zone);
149 if (so) {
150 /* XXX race condition for reentrant kernel */
151 bzero(so, sizeof *so);
152 TAILQ_INIT(&so->so_aiojobq);
153 TAILQ_INIT(&so->so_rcv.ssb_sel.si_mlist);
154 TAILQ_INIT(&so->so_snd.ssb_sel.si_mlist);
156 return so;
160 socreate(int dom, struct socket **aso, int type,
161 int proto, struct thread *td)
163 struct proc *p = td->td_proc;
164 struct protosw *prp;
165 struct socket *so;
166 struct pru_attach_info ai;
167 int error;
169 if (proto)
170 prp = pffindproto(dom, proto, type);
171 else
172 prp = pffindtype(dom, type);
174 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
175 return (EPROTONOSUPPORT);
177 if (p->p_ucred->cr_prison && jail_socket_unixiproute_only &&
178 prp->pr_domain->dom_family != PF_LOCAL &&
179 prp->pr_domain->dom_family != PF_INET &&
180 prp->pr_domain->dom_family != PF_INET6 &&
181 prp->pr_domain->dom_family != PF_ROUTE) {
182 return (EPROTONOSUPPORT);
185 if (prp->pr_type != type)
186 return (EPROTOTYPE);
187 so = soalloc(p != 0);
188 if (so == 0)
189 return (ENOBUFS);
191 TAILQ_INIT(&so->so_incomp);
192 TAILQ_INIT(&so->so_comp);
193 so->so_type = type;
194 so->so_cred = crhold(p->p_ucred);
195 so->so_proto = prp;
196 ai.sb_rlimit = &p->p_rlimit[RLIMIT_SBSIZE];
197 ai.p_ucred = p->p_ucred;
198 ai.fd_rdir = p->p_fd->fd_rdir;
199 error = so_pru_attach(so, proto, &ai);
200 if (error) {
201 so->so_state |= SS_NOFDREF;
202 sofree(so);
203 return (error);
205 *aso = so;
206 return (0);
210 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
212 int error;
214 crit_enter();
215 error = so_pru_bind(so, nam, td);
216 crit_exit();
217 return (error);
220 void
221 sodealloc(struct socket *so)
223 if (so->so_rcv.ssb_hiwat)
224 (void)chgsbsize(so->so_cred->cr_uidinfo,
225 &so->so_rcv.ssb_hiwat, 0, RLIM_INFINITY);
226 if (so->so_snd.ssb_hiwat)
227 (void)chgsbsize(so->so_cred->cr_uidinfo,
228 &so->so_snd.ssb_hiwat, 0, RLIM_INFINITY);
229 #ifdef INET
230 /* remove accept filter if present */
231 if (so->so_accf != NULL)
232 do_setopt_accept_filter(so, NULL);
233 #endif /* INET */
234 crfree(so->so_cred);
235 zfree(socket_zone, so);
239 solisten(struct socket *so, int backlog, struct thread *td)
241 int error;
242 #ifdef SCTP
243 short oldopt, oldqlimit;
244 #endif /* SCTP */
246 crit_enter();
247 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING)) {
248 crit_exit();
249 return (EINVAL);
252 #ifdef SCTP
253 oldopt = so->so_options;
254 oldqlimit = so->so_qlimit;
255 #endif /* SCTP */
257 if (TAILQ_EMPTY(&so->so_comp))
258 so->so_options |= SO_ACCEPTCONN;
259 if (backlog < 0 || backlog > somaxconn)
260 backlog = somaxconn;
261 so->so_qlimit = backlog;
262 /* SCTP needs to look at tweak both the inbound backlog parameter AND
263 * the so_options (UDP model both connect's and gets inbound
264 * connections .. implicitly).
266 error = so_pru_listen(so, td);
267 if (error) {
268 #ifdef SCTP
269 /* Restore the params */
270 so->so_options = oldopt;
271 so->so_qlimit = oldqlimit;
272 #endif /* SCTP */
273 crit_exit();
274 return (error);
276 crit_exit();
277 return (0);
280 void
281 sofree(struct socket *so)
283 struct socket *head = so->so_head;
285 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
286 return;
287 if (head != NULL) {
288 if (so->so_state & SS_INCOMP) {
289 TAILQ_REMOVE(&head->so_incomp, so, so_list);
290 head->so_incqlen--;
291 } else if (so->so_state & SS_COMP) {
293 * We must not decommission a socket that's
294 * on the accept(2) queue. If we do, then
295 * accept(2) may hang after select(2) indicated
296 * that the listening socket was ready.
298 return;
299 } else {
300 panic("sofree: not queued");
302 so->so_state &= ~SS_INCOMP;
303 so->so_head = NULL;
305 ssb_release(&so->so_snd, so);
306 sorflush(so);
307 sodealloc(so);
311 * Close a socket on last file table reference removal.
312 * Initiate disconnect if connected.
313 * Free socket when disconnect complete.
316 soclose(struct socket *so, int fflag)
318 int error = 0;
320 crit_enter();
321 funsetown(so->so_sigio);
322 if (so->so_pcb == NULL)
323 goto discard;
324 if (so->so_state & SS_ISCONNECTED) {
325 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
326 error = sodisconnect(so);
327 if (error)
328 goto drop;
330 if (so->so_options & SO_LINGER) {
331 if ((so->so_state & SS_ISDISCONNECTING) &&
332 (fflag & FNONBLOCK))
333 goto drop;
334 while (so->so_state & SS_ISCONNECTED) {
335 error = tsleep((caddr_t)&so->so_timeo,
336 PCATCH, "soclos", so->so_linger * hz);
337 if (error)
338 break;
342 drop:
343 if (so->so_pcb) {
344 int error2;
346 error2 = so_pru_detach(so);
347 if (error == 0)
348 error = error2;
350 discard:
351 if (so->so_options & SO_ACCEPTCONN) {
352 struct socket *sp, *sonext;
354 sp = TAILQ_FIRST(&so->so_incomp);
355 for (; sp != NULL; sp = sonext) {
356 sonext = TAILQ_NEXT(sp, so_list);
357 (void) soabort(sp);
359 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
360 sonext = TAILQ_NEXT(sp, so_list);
361 /* Dequeue from so_comp since sofree() won't do it */
362 TAILQ_REMOVE(&so->so_comp, sp, so_list);
363 so->so_qlen--;
364 sp->so_state &= ~SS_COMP;
365 sp->so_head = NULL;
366 (void) soabort(sp);
369 if (so->so_state & SS_NOFDREF)
370 panic("soclose: NOFDREF");
371 so->so_state |= SS_NOFDREF;
372 sofree(so);
373 crit_exit();
374 return (error);
378 * Must be called from a critical section.
381 soabort(struct socket *so)
383 int error;
385 error = so_pru_abort(so);
386 if (error) {
387 sofree(so);
388 return error;
390 return (0);
394 soaccept(struct socket *so, struct sockaddr **nam)
396 int error;
398 crit_enter();
399 if ((so->so_state & SS_NOFDREF) == 0)
400 panic("soaccept: !NOFDREF");
401 so->so_state &= ~SS_NOFDREF;
402 error = so_pru_accept(so, nam);
403 crit_exit();
404 return (error);
408 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
410 int error;
412 if (so->so_options & SO_ACCEPTCONN)
413 return (EOPNOTSUPP);
414 crit_enter();
416 * If protocol is connection-based, can only connect once.
417 * Otherwise, if connected, try to disconnect first.
418 * This allows user to disconnect by connecting to, e.g.,
419 * a null address.
421 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
422 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
423 (error = sodisconnect(so)))) {
424 error = EISCONN;
425 } else {
427 * Prevent accumulated error from previous connection
428 * from biting us.
430 so->so_error = 0;
431 error = so_pru_connect(so, nam, td);
433 crit_exit();
434 return (error);
438 soconnect2(struct socket *so1, struct socket *so2)
440 int error;
442 crit_enter();
443 error = so_pru_connect2(so1, so2);
444 crit_exit();
445 return (error);
449 sodisconnect(struct socket *so)
451 int error;
453 crit_enter();
454 if ((so->so_state & SS_ISCONNECTED) == 0) {
455 error = ENOTCONN;
456 goto bad;
458 if (so->so_state & SS_ISDISCONNECTING) {
459 error = EALREADY;
460 goto bad;
462 error = so_pru_disconnect(so);
463 bad:
464 crit_exit();
465 return (error);
468 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
470 * Send on a socket.
471 * If send must go all at once and message is larger than
472 * send buffering, then hard error.
473 * Lock against other senders.
474 * If must go all at once and not enough room now, then
475 * inform user that this would block and do nothing.
476 * Otherwise, if nonblocking, send as much as possible.
477 * The data to be sent is described by "uio" if nonzero,
478 * otherwise by the mbuf chain "top" (which must be null
479 * if uio is not). Data provided in mbuf chain must be small
480 * enough to send all at once.
482 * Returns nonzero on error, timeout or signal; callers
483 * must check for short counts if EINTR/ERESTART are returned.
484 * Data and control buffers are freed on return.
487 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
488 struct mbuf *top, struct mbuf *control, int flags,
489 struct thread *td)
491 struct mbuf **mp;
492 struct mbuf *m;
493 long space, len, resid;
494 int clen = 0, error, dontroute, mlen;
495 int atomic = sosendallatonce(so) || top;
496 int pru_flags;
498 if (uio)
499 resid = uio->uio_resid;
500 else
501 resid = top->m_pkthdr.len;
503 * In theory resid should be unsigned.
504 * However, space must be signed, as it might be less than 0
505 * if we over-committed, and we must use a signed comparison
506 * of space and resid. On the other hand, a negative resid
507 * causes us to loop sending 0-length segments to the protocol.
509 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
510 * type sockets since that's an error.
512 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
513 error = EINVAL;
514 goto out;
517 dontroute =
518 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
519 (so->so_proto->pr_flags & PR_ATOMIC);
520 if (td->td_lwp != NULL)
521 td->td_lwp->lwp_ru.ru_msgsnd++;
522 if (control)
523 clen = control->m_len;
524 #define gotoerr(errcode) { error = errcode; crit_exit(); goto release; }
526 restart:
527 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
528 if (error)
529 goto out;
530 do {
531 crit_enter();
532 if (so->so_state & SS_CANTSENDMORE)
533 gotoerr(EPIPE);
534 if (so->so_error) {
535 error = so->so_error;
536 so->so_error = 0;
537 crit_exit();
538 goto release;
540 if ((so->so_state & SS_ISCONNECTED) == 0) {
542 * `sendto' and `sendmsg' is allowed on a connection-
543 * based socket if it supports implied connect.
544 * Return ENOTCONN if not connected and no address is
545 * supplied.
547 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
548 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
549 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
550 !(resid == 0 && clen != 0))
551 gotoerr(ENOTCONN);
552 } else if (addr == 0)
553 gotoerr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
554 ENOTCONN : EDESTADDRREQ);
556 space = ssb_space(&so->so_snd);
557 if (flags & MSG_OOB)
558 space += 1024;
559 if ((atomic && resid > so->so_snd.ssb_hiwat) ||
560 clen > so->so_snd.ssb_hiwat)
561 gotoerr(EMSGSIZE);
562 if (space < resid + clen && uio &&
563 (atomic || space < so->so_snd.ssb_lowat || space < clen)) {
564 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
565 gotoerr(EWOULDBLOCK);
566 ssb_unlock(&so->so_snd);
567 error = ssb_wait(&so->so_snd);
568 crit_exit();
569 if (error)
570 goto out;
571 goto restart;
573 crit_exit();
574 mp = &top;
575 space -= clen;
576 do {
577 if (uio == NULL) {
579 * Data is prepackaged in "top".
581 resid = 0;
582 if (flags & MSG_EOR)
583 top->m_flags |= M_EOR;
584 } else do {
585 m = m_getl(resid, MB_WAIT, MT_DATA,
586 top == NULL ? M_PKTHDR : 0, &mlen);
587 if (top == NULL) {
588 m->m_pkthdr.len = 0;
589 m->m_pkthdr.rcvif = (struct ifnet *)0;
591 len = min(min(mlen, resid), space);
592 if (resid < MINCLSIZE) {
594 * For datagram protocols, leave room
595 * for protocol headers in first mbuf.
597 if (atomic && top == 0 && len < mlen)
598 MH_ALIGN(m, len);
600 space -= len;
601 error = uiomove(mtod(m, caddr_t), (int)len, uio);
602 resid = uio->uio_resid;
603 m->m_len = len;
604 *mp = m;
605 top->m_pkthdr.len += len;
606 if (error)
607 goto release;
608 mp = &m->m_next;
609 if (resid <= 0) {
610 if (flags & MSG_EOR)
611 top->m_flags |= M_EOR;
612 break;
614 } while (space > 0 && atomic);
615 if (dontroute)
616 so->so_options |= SO_DONTROUTE;
617 if (flags & MSG_OOB) {
618 pru_flags = PRUS_OOB;
619 } else if ((flags & MSG_EOF) &&
620 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
621 (resid <= 0)) {
623 * If the user set MSG_EOF, the protocol
624 * understands this flag and nothing left to
625 * send then use PRU_SEND_EOF instead of PRU_SEND.
627 pru_flags = PRUS_EOF;
628 } else if (resid > 0 && space > 0) {
629 /* If there is more to send, set PRUS_MORETOCOME */
630 pru_flags = PRUS_MORETOCOME;
631 } else {
632 pru_flags = 0;
634 crit_enter();
636 * XXX all the SS_CANTSENDMORE checks previously
637 * done could be out of date. We could have recieved
638 * a reset packet in an interrupt or maybe we slept
639 * while doing page faults in uiomove() etc. We could
640 * probably recheck again inside the splnet() protection
641 * here, but there are probably other places that this
642 * also happens. We must rethink this.
644 error = so_pru_send(so, pru_flags, top, addr, control, td);
645 crit_exit();
646 if (dontroute)
647 so->so_options &= ~SO_DONTROUTE;
648 clen = 0;
649 control = 0;
650 top = 0;
651 mp = &top;
652 if (error)
653 goto release;
654 } while (resid && space > 0);
655 } while (resid);
657 release:
658 ssb_unlock(&so->so_snd);
659 out:
660 if (top)
661 m_freem(top);
662 if (control)
663 m_freem(control);
664 return (error);
668 * A specialization of sosend() for UDP based on protocol-specific knowledge:
669 * so->so_proto->pr_flags has the PR_ATOMIC field set. This means that
670 * sosendallatonce() returns true,
671 * the "atomic" variable is true,
672 * and sosendudp() blocks until space is available for the entire send.
673 * so->so_proto->pr_flags does not have the PR_CONNREQUIRED or
674 * PR_IMPLOPCL flags set.
675 * UDP has no out-of-band data.
676 * UDP has no control data.
677 * UDP does not support MSG_EOR.
680 sosendudp(struct socket *so, struct sockaddr *addr, struct uio *uio,
681 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
683 int resid, error;
684 boolean_t dontroute; /* temporary SO_DONTROUTE setting */
686 if (td->td_lwp != NULL)
687 td->td_lwp->lwp_ru.ru_msgsnd++;
688 if (control)
689 m_freem(control);
691 KASSERT((uio && !top) || (top && !uio), ("bad arguments to sosendudp"));
692 resid = uio ? uio->uio_resid : top->m_pkthdr.len;
694 restart:
695 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
696 if (error)
697 goto out;
699 crit_enter();
700 if (so->so_state & SS_CANTSENDMORE)
701 gotoerr(EPIPE);
702 if (so->so_error) {
703 error = so->so_error;
704 so->so_error = 0;
705 crit_exit();
706 goto release;
708 if (!(so->so_state & SS_ISCONNECTED) && addr == NULL)
709 gotoerr(EDESTADDRREQ);
710 if (resid > so->so_snd.ssb_hiwat)
711 gotoerr(EMSGSIZE);
712 if (uio && ssb_space(&so->so_snd) < resid) {
713 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
714 gotoerr(EWOULDBLOCK);
715 ssb_unlock(&so->so_snd);
716 error = ssb_wait(&so->so_snd);
717 crit_exit();
718 if (error)
719 goto out;
720 goto restart;
722 crit_exit();
724 if (uio) {
725 top = m_uiomove(uio);
726 if (top == NULL)
727 goto release;
730 dontroute = (flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE);
731 if (dontroute)
732 so->so_options |= SO_DONTROUTE;
734 error = so_pru_send(so, 0, top, addr, NULL, td);
735 top = NULL; /* sent or freed in lower layer */
737 if (dontroute)
738 so->so_options &= ~SO_DONTROUTE;
740 release:
741 ssb_unlock(&so->so_snd);
742 out:
743 if (top)
744 m_freem(top);
745 return (error);
749 * Implement receive operations on a socket.
750 * We depend on the way that records are added to the signalsockbuf
751 * by sbappend*. In particular, each record (mbufs linked through m_next)
752 * must begin with an address if the protocol so specifies,
753 * followed by an optional mbuf or mbufs containing ancillary data,
754 * and then zero or more mbufs of data.
755 * In order to avoid blocking network interrupts for the entire time here,
756 * we exit the critical section while doing the actual copy to user space.
757 * Although the signalsockbuf is locked, new data may still be appended,
758 * and thus we must maintain consistency of the signalsockbuf during that time.
760 * The caller may receive the data as a single mbuf chain by supplying
761 * an mbuf **mp0 for use in returning the chain. The uio is then used
762 * only for the count in uio_resid.
765 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
766 struct sockbuf *sio, struct mbuf **controlp, int *flagsp)
768 struct mbuf *m, *n;
769 struct mbuf *free_chain = NULL;
770 int flags, len, error, offset;
771 struct protosw *pr = so->so_proto;
772 int moff, type = 0;
773 int resid, orig_resid;
775 if (uio)
776 resid = uio->uio_resid;
777 else
778 resid = (int)(sio->sb_climit - sio->sb_cc);
779 orig_resid = resid;
781 if (psa)
782 *psa = NULL;
783 if (controlp)
784 *controlp = NULL;
785 if (flagsp)
786 flags = *flagsp &~ MSG_EOR;
787 else
788 flags = 0;
789 if (flags & MSG_OOB) {
790 m = m_get(MB_WAIT, MT_DATA);
791 if (m == NULL)
792 return (ENOBUFS);
793 error = so_pru_rcvoob(so, m, flags & MSG_PEEK);
794 if (error)
795 goto bad;
796 if (sio) {
797 do {
798 sbappend(sio, m);
799 resid -= m->m_len;
800 } while (resid > 0 && m);
801 } else {
802 do {
803 uio->uio_resid = resid;
804 error = uiomove(mtod(m, caddr_t),
805 (int)min(resid, m->m_len), uio);
806 resid = uio->uio_resid;
807 m = m_free(m);
808 } while (uio->uio_resid && error == 0 && m);
810 bad:
811 if (m)
812 m_freem(m);
813 return (error);
815 if (so->so_state & SS_ISCONFIRMING && resid)
816 so_pru_rcvd(so, 0);
818 restart:
819 crit_enter();
820 error = ssb_lock(&so->so_rcv, SBLOCKWAIT(flags));
821 if (error)
822 goto done;
824 m = so->so_rcv.ssb_mb;
826 * If we have less data than requested, block awaiting more
827 * (subject to any timeout) if:
828 * 1. the current count is less than the low water mark, or
829 * 2. MSG_WAITALL is set, and it is possible to do the entire
830 * receive operation at once if we block (resid <= hiwat).
831 * 3. MSG_DONTWAIT is not set
832 * If MSG_WAITALL is set but resid is larger than the receive buffer,
833 * we have to do the receive in sections, and thus risk returning
834 * a short count if a timeout or signal occurs after we start.
836 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
837 so->so_rcv.ssb_cc < resid) &&
838 (so->so_rcv.ssb_cc < so->so_rcv.ssb_lowat ||
839 ((flags & MSG_WAITALL) && resid <= so->so_rcv.ssb_hiwat)) &&
840 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
841 KASSERT(m != NULL || !so->so_rcv.ssb_cc, ("receive 1"));
842 if (so->so_error) {
843 if (m)
844 goto dontblock;
845 error = so->so_error;
846 if ((flags & MSG_PEEK) == 0)
847 so->so_error = 0;
848 goto release;
850 if (so->so_state & SS_CANTRCVMORE) {
851 if (m)
852 goto dontblock;
853 else
854 goto release;
856 for (; m; m = m->m_next) {
857 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
858 m = so->so_rcv.ssb_mb;
859 goto dontblock;
862 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
863 (pr->pr_flags & PR_CONNREQUIRED)) {
864 error = ENOTCONN;
865 goto release;
867 if (resid == 0)
868 goto release;
869 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
870 error = EWOULDBLOCK;
871 goto release;
873 ssb_unlock(&so->so_rcv);
874 error = ssb_wait(&so->so_rcv);
875 if (error)
876 goto done;
877 crit_exit();
878 goto restart;
880 dontblock:
881 if (uio && uio->uio_td && uio->uio_td->td_proc)
882 uio->uio_td->td_lwp->lwp_ru.ru_msgrcv++;
885 * note: m should be == sb_mb here. Cache the next record while
886 * cleaning up. Note that calling m_free*() will break out critical
887 * section.
889 KKASSERT(m == so->so_rcv.ssb_mb);
892 * Skip any address mbufs prepending the record.
894 if (pr->pr_flags & PR_ADDR) {
895 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
896 orig_resid = 0;
897 if (psa)
898 *psa = dup_sockaddr(mtod(m, struct sockaddr *));
899 if (flags & MSG_PEEK)
900 m = m->m_next;
901 else
902 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
906 * Skip any control mbufs prepending the record.
908 #ifdef SCTP
909 if (pr->pr_flags & PR_ADDR_OPT) {
911 * For SCTP we may be getting a
912 * whole message OR a partial delivery.
914 if (m && m->m_type == MT_SONAME) {
915 orig_resid = 0;
916 if (psa)
917 *psa = dup_sockaddr(mtod(m, struct sockaddr *));
918 if (flags & MSG_PEEK)
919 m = m->m_next;
920 else
921 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
924 #endif /* SCTP */
925 while (m && m->m_type == MT_CONTROL && error == 0) {
926 if (flags & MSG_PEEK) {
927 if (controlp)
928 *controlp = m_copy(m, 0, m->m_len);
929 m = m->m_next; /* XXX race */
930 } else {
931 if (controlp) {
932 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
933 if (pr->pr_domain->dom_externalize &&
934 mtod(m, struct cmsghdr *)->cmsg_type ==
935 SCM_RIGHTS)
936 error = (*pr->pr_domain->dom_externalize)(m);
937 *controlp = m;
938 m = n;
939 } else {
940 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
943 if (controlp && *controlp) {
944 orig_resid = 0;
945 controlp = &(*controlp)->m_next;
950 * flag OOB data.
952 if (m) {
953 type = m->m_type;
954 if (type == MT_OOBDATA)
955 flags |= MSG_OOB;
959 * Copy to the UIO or mbuf return chain (*mp).
961 moff = 0;
962 offset = 0;
963 while (m && resid > 0 && error == 0) {
964 if (m->m_type == MT_OOBDATA) {
965 if (type != MT_OOBDATA)
966 break;
967 } else if (type == MT_OOBDATA)
968 break;
969 else
970 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
971 ("receive 3"));
972 so->so_state &= ~SS_RCVATMARK;
973 len = resid;
974 if (so->so_oobmark && len > so->so_oobmark - offset)
975 len = so->so_oobmark - offset;
976 if (len > m->m_len - moff)
977 len = m->m_len - moff;
980 * Copy out to the UIO or pass the mbufs back to the SIO.
981 * The SIO is dealt with when we eat the mbuf, but deal
982 * with the resid here either way.
984 if (uio) {
985 crit_exit();
986 uio->uio_resid = resid;
987 error = uiomove(mtod(m, caddr_t) + moff, len, uio);
988 resid = uio->uio_resid;
989 crit_enter();
990 if (error)
991 goto release;
992 } else {
993 resid -= len;
997 * Eat the entire mbuf or just a piece of it
999 if (len == m->m_len - moff) {
1000 if (m->m_flags & M_EOR)
1001 flags |= MSG_EOR;
1002 #ifdef SCTP
1003 if (m->m_flags & M_NOTIFICATION)
1004 flags |= MSG_NOTIFICATION;
1005 #endif /* SCTP */
1006 if (flags & MSG_PEEK) {
1007 m = m->m_next;
1008 moff = 0;
1009 } else {
1010 if (sio) {
1011 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
1012 sbappend(sio, m);
1013 m = n;
1014 } else {
1015 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1018 } else {
1019 if (flags & MSG_PEEK) {
1020 moff += len;
1021 } else {
1022 if (sio) {
1023 n = m_copym(m, 0, len, MB_WAIT);
1024 if (n)
1025 sbappend(sio, n);
1027 m->m_data += len;
1028 m->m_len -= len;
1029 so->so_rcv.ssb_cc -= len;
1032 if (so->so_oobmark) {
1033 if ((flags & MSG_PEEK) == 0) {
1034 so->so_oobmark -= len;
1035 if (so->so_oobmark == 0) {
1036 so->so_state |= SS_RCVATMARK;
1037 break;
1039 } else {
1040 offset += len;
1041 if (offset == so->so_oobmark)
1042 break;
1045 if (flags & MSG_EOR)
1046 break;
1048 * If the MSG_WAITALL flag is set (for non-atomic socket),
1049 * we must not quit until resid == 0 or an error
1050 * termination. If a signal/timeout occurs, return
1051 * with a short count but without error.
1052 * Keep signalsockbuf locked against other readers.
1054 while ((flags & MSG_WAITALL) && m == NULL &&
1055 resid > 0 && !sosendallatonce(so) &&
1056 so->so_rcv.ssb_mb == NULL) {
1057 if (so->so_error || so->so_state & SS_CANTRCVMORE)
1058 break;
1060 * The window might have closed to zero, make
1061 * sure we send an ack now that we've drained
1062 * the buffer or we might end up blocking until
1063 * the idle takes over (5 seconds).
1065 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1066 so_pru_rcvd(so, flags);
1067 error = ssb_wait(&so->so_rcv);
1068 if (error) {
1069 ssb_unlock(&so->so_rcv);
1070 error = 0;
1071 goto done;
1073 m = so->so_rcv.ssb_mb;
1078 * If an atomic read was requested but unread data still remains
1079 * in the record, set MSG_TRUNC.
1081 if (m && pr->pr_flags & PR_ATOMIC)
1082 flags |= MSG_TRUNC;
1085 * Cleanup. If an atomic read was requested drop any unread data.
1087 if ((flags & MSG_PEEK) == 0) {
1088 if (m && (pr->pr_flags & PR_ATOMIC))
1089 sbdroprecord(&so->so_rcv.sb);
1090 if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
1091 so_pru_rcvd(so, flags);
1094 if (orig_resid == resid && orig_resid &&
1095 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
1096 ssb_unlock(&so->so_rcv);
1097 crit_exit();
1098 goto restart;
1101 if (flagsp)
1102 *flagsp |= flags;
1103 release:
1104 ssb_unlock(&so->so_rcv);
1105 done:
1106 crit_exit();
1107 if (free_chain)
1108 m_freem(free_chain);
1109 return (error);
1113 soshutdown(struct socket *so, int how)
1115 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1116 return (EINVAL);
1118 if (how != SHUT_WR)
1119 sorflush(so);
1120 if (how != SHUT_RD)
1121 return (so_pru_shutdown(so));
1122 return (0);
1125 void
1126 sorflush(struct socket *so)
1128 struct signalsockbuf *ssb = &so->so_rcv;
1129 struct protosw *pr = so->so_proto;
1130 struct signalsockbuf asb;
1132 ssb->ssb_flags |= SSB_NOINTR;
1133 (void) ssb_lock(ssb, M_WAITOK);
1135 crit_enter();
1136 socantrcvmore(so);
1137 ssb_unlock(ssb);
1138 asb = *ssb;
1139 bzero((caddr_t)ssb, sizeof (*ssb));
1140 if (asb.ssb_flags & SSB_KNOTE) {
1141 ssb->ssb_sel.si_note = asb.ssb_sel.si_note;
1142 ssb->ssb_flags = SSB_KNOTE;
1144 crit_exit();
1146 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
1147 (*pr->pr_domain->dom_dispose)(asb.ssb_mb);
1148 ssb_release(&asb, so);
1151 #ifdef INET
1152 static int
1153 do_setopt_accept_filter(struct socket *so, struct sockopt *sopt)
1155 struct accept_filter_arg *afap = NULL;
1156 struct accept_filter *afp;
1157 struct so_accf *af = so->so_accf;
1158 int error = 0;
1160 /* do not set/remove accept filters on non listen sockets */
1161 if ((so->so_options & SO_ACCEPTCONN) == 0) {
1162 error = EINVAL;
1163 goto out;
1166 /* removing the filter */
1167 if (sopt == NULL) {
1168 if (af != NULL) {
1169 if (af->so_accept_filter != NULL &&
1170 af->so_accept_filter->accf_destroy != NULL) {
1171 af->so_accept_filter->accf_destroy(so);
1173 if (af->so_accept_filter_str != NULL) {
1174 FREE(af->so_accept_filter_str, M_ACCF);
1176 FREE(af, M_ACCF);
1177 so->so_accf = NULL;
1179 so->so_options &= ~SO_ACCEPTFILTER;
1180 return (0);
1182 /* adding a filter */
1183 /* must remove previous filter first */
1184 if (af != NULL) {
1185 error = EINVAL;
1186 goto out;
1188 /* don't put large objects on the kernel stack */
1189 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1190 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1191 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1192 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1193 if (error)
1194 goto out;
1195 afp = accept_filt_get(afap->af_name);
1196 if (afp == NULL) {
1197 error = ENOENT;
1198 goto out;
1200 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK);
1201 bzero(af, sizeof(*af));
1202 if (afp->accf_create != NULL) {
1203 if (afap->af_name[0] != '\0') {
1204 int len = strlen(afap->af_name) + 1;
1206 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1207 strcpy(af->so_accept_filter_str, afap->af_name);
1209 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1210 if (af->so_accept_filter_arg == NULL) {
1211 FREE(af->so_accept_filter_str, M_ACCF);
1212 FREE(af, M_ACCF);
1213 so->so_accf = NULL;
1214 error = EINVAL;
1215 goto out;
1218 af->so_accept_filter = afp;
1219 so->so_accf = af;
1220 so->so_options |= SO_ACCEPTFILTER;
1221 out:
1222 if (afap != NULL)
1223 FREE(afap, M_TEMP);
1224 return (error);
1226 #endif /* INET */
1229 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1230 * an additional variant to handle the case where the option value needs
1231 * to be some kind of integer, but not a specific size.
1232 * In addition to their use here, these functions are also called by the
1233 * protocol-level pr_ctloutput() routines.
1236 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
1238 size_t valsize;
1241 * If the user gives us more than we wanted, we ignore it,
1242 * but if we don't get the minimum length the caller
1243 * wants, we return EINVAL. On success, sopt->sopt_valsize
1244 * is set to however much we actually retrieved.
1246 if ((valsize = sopt->sopt_valsize) < minlen)
1247 return EINVAL;
1248 if (valsize > len)
1249 sopt->sopt_valsize = valsize = len;
1251 if (sopt->sopt_td != NULL)
1252 return (copyin(sopt->sopt_val, buf, valsize));
1254 bcopy(sopt->sopt_val, buf, valsize);
1255 return 0;
1259 sosetopt(struct socket *so, struct sockopt *sopt)
1261 int error, optval;
1262 struct linger l;
1263 struct timeval tv;
1264 u_long val;
1266 error = 0;
1267 sopt->sopt_dir = SOPT_SET;
1268 if (sopt->sopt_level != SOL_SOCKET) {
1269 if (so->so_proto && so->so_proto->pr_ctloutput) {
1270 return (so_pr_ctloutput(so, sopt));
1272 error = ENOPROTOOPT;
1273 } else {
1274 switch (sopt->sopt_name) {
1275 #ifdef INET
1276 case SO_ACCEPTFILTER:
1277 error = do_setopt_accept_filter(so, sopt);
1278 if (error)
1279 goto bad;
1280 break;
1281 #endif /* INET */
1282 case SO_LINGER:
1283 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1284 if (error)
1285 goto bad;
1287 so->so_linger = l.l_linger;
1288 if (l.l_onoff)
1289 so->so_options |= SO_LINGER;
1290 else
1291 so->so_options &= ~SO_LINGER;
1292 break;
1294 case SO_DEBUG:
1295 case SO_KEEPALIVE:
1296 case SO_DONTROUTE:
1297 case SO_USELOOPBACK:
1298 case SO_BROADCAST:
1299 case SO_REUSEADDR:
1300 case SO_REUSEPORT:
1301 case SO_OOBINLINE:
1302 case SO_TIMESTAMP:
1303 error = sooptcopyin(sopt, &optval, sizeof optval,
1304 sizeof optval);
1305 if (error)
1306 goto bad;
1307 if (optval)
1308 so->so_options |= sopt->sopt_name;
1309 else
1310 so->so_options &= ~sopt->sopt_name;
1311 break;
1313 case SO_SNDBUF:
1314 case SO_RCVBUF:
1315 case SO_SNDLOWAT:
1316 case SO_RCVLOWAT:
1317 error = sooptcopyin(sopt, &optval, sizeof optval,
1318 sizeof optval);
1319 if (error)
1320 goto bad;
1323 * Values < 1 make no sense for any of these
1324 * options, so disallow them.
1326 if (optval < 1) {
1327 error = EINVAL;
1328 goto bad;
1331 switch (sopt->sopt_name) {
1332 case SO_SNDBUF:
1333 case SO_RCVBUF:
1334 if (ssb_reserve(sopt->sopt_name == SO_SNDBUF ?
1335 &so->so_snd : &so->so_rcv, (u_long)optval,
1337 &curproc->p_rlimit[RLIMIT_SBSIZE]) == 0) {
1338 error = ENOBUFS;
1339 goto bad;
1341 break;
1344 * Make sure the low-water is never greater than
1345 * the high-water.
1347 case SO_SNDLOWAT:
1348 so->so_snd.ssb_lowat =
1349 (optval > so->so_snd.ssb_hiwat) ?
1350 so->so_snd.ssb_hiwat : optval;
1351 break;
1352 case SO_RCVLOWAT:
1353 so->so_rcv.ssb_lowat =
1354 (optval > so->so_rcv.ssb_hiwat) ?
1355 so->so_rcv.ssb_hiwat : optval;
1356 break;
1358 break;
1360 case SO_SNDTIMEO:
1361 case SO_RCVTIMEO:
1362 error = sooptcopyin(sopt, &tv, sizeof tv,
1363 sizeof tv);
1364 if (error)
1365 goto bad;
1367 /* assert(hz > 0); */
1368 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1369 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1370 error = EDOM;
1371 goto bad;
1373 /* assert(tick > 0); */
1374 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1375 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1376 if (val > SHRT_MAX) {
1377 error = EDOM;
1378 goto bad;
1380 if (val == 0 && tv.tv_usec != 0)
1381 val = 1;
1383 switch (sopt->sopt_name) {
1384 case SO_SNDTIMEO:
1385 so->so_snd.ssb_timeo = val;
1386 break;
1387 case SO_RCVTIMEO:
1388 so->so_rcv.ssb_timeo = val;
1389 break;
1391 break;
1392 default:
1393 error = ENOPROTOOPT;
1394 break;
1396 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1397 (void) so_pr_ctloutput(so, sopt);
1400 bad:
1401 return (error);
1404 /* Helper routine for getsockopt */
1406 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
1408 int error;
1409 size_t valsize;
1411 error = 0;
1414 * Documented get behavior is that we always return a value,
1415 * possibly truncated to fit in the user's buffer.
1416 * Traditional behavior is that we always tell the user
1417 * precisely how much we copied, rather than something useful
1418 * like the total amount we had available for her.
1419 * Note that this interface is not idempotent; the entire answer must
1420 * generated ahead of time.
1422 valsize = min(len, sopt->sopt_valsize);
1423 sopt->sopt_valsize = valsize;
1424 if (sopt->sopt_val != 0) {
1425 if (sopt->sopt_td != NULL)
1426 error = copyout(buf, sopt->sopt_val, valsize);
1427 else
1428 bcopy(buf, sopt->sopt_val, valsize);
1430 return error;
1434 sogetopt(struct socket *so, struct sockopt *sopt)
1436 int error, optval;
1437 struct linger l;
1438 struct timeval tv;
1439 #ifdef INET
1440 struct accept_filter_arg *afap;
1441 #endif
1443 error = 0;
1444 sopt->sopt_dir = SOPT_GET;
1445 if (sopt->sopt_level != SOL_SOCKET) {
1446 if (so->so_proto && so->so_proto->pr_ctloutput) {
1447 return (so_pr_ctloutput(so, sopt));
1448 } else
1449 return (ENOPROTOOPT);
1450 } else {
1451 switch (sopt->sopt_name) {
1452 #ifdef INET
1453 case SO_ACCEPTFILTER:
1454 if ((so->so_options & SO_ACCEPTCONN) == 0)
1455 return (EINVAL);
1456 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1457 M_TEMP, M_WAITOK);
1458 bzero(afap, sizeof(*afap));
1459 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1460 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1461 if (so->so_accf->so_accept_filter_str != NULL)
1462 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1464 error = sooptcopyout(sopt, afap, sizeof(*afap));
1465 FREE(afap, M_TEMP);
1466 break;
1467 #endif /* INET */
1469 case SO_LINGER:
1470 l.l_onoff = so->so_options & SO_LINGER;
1471 l.l_linger = so->so_linger;
1472 error = sooptcopyout(sopt, &l, sizeof l);
1473 break;
1475 case SO_USELOOPBACK:
1476 case SO_DONTROUTE:
1477 case SO_DEBUG:
1478 case SO_KEEPALIVE:
1479 case SO_REUSEADDR:
1480 case SO_REUSEPORT:
1481 case SO_BROADCAST:
1482 case SO_OOBINLINE:
1483 case SO_TIMESTAMP:
1484 optval = so->so_options & sopt->sopt_name;
1485 integer:
1486 error = sooptcopyout(sopt, &optval, sizeof optval);
1487 break;
1489 case SO_TYPE:
1490 optval = so->so_type;
1491 goto integer;
1493 case SO_ERROR:
1494 optval = so->so_error;
1495 so->so_error = 0;
1496 goto integer;
1498 case SO_SNDBUF:
1499 optval = so->so_snd.ssb_hiwat;
1500 goto integer;
1502 case SO_RCVBUF:
1503 optval = so->so_rcv.ssb_hiwat;
1504 goto integer;
1506 case SO_SNDLOWAT:
1507 optval = so->so_snd.ssb_lowat;
1508 goto integer;
1510 case SO_RCVLOWAT:
1511 optval = so->so_rcv.ssb_lowat;
1512 goto integer;
1514 case SO_SNDTIMEO:
1515 case SO_RCVTIMEO:
1516 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1517 so->so_snd.ssb_timeo : so->so_rcv.ssb_timeo);
1519 tv.tv_sec = optval / hz;
1520 tv.tv_usec = (optval % hz) * tick;
1521 error = sooptcopyout(sopt, &tv, sizeof tv);
1522 break;
1524 default:
1525 error = ENOPROTOOPT;
1526 break;
1528 return (error);
1532 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1534 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1536 struct mbuf *m, *m_prev;
1537 int sopt_size = sopt->sopt_valsize, msize;
1539 m = m_getl(sopt_size, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_DATA,
1540 0, &msize);
1541 if (m == NULL)
1542 return (ENOBUFS);
1543 m->m_len = min(msize, sopt_size);
1544 sopt_size -= m->m_len;
1545 *mp = m;
1546 m_prev = m;
1548 while (sopt_size > 0) {
1549 m = m_getl(sopt_size, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT,
1550 MT_DATA, 0, &msize);
1551 if (m == NULL) {
1552 m_freem(*mp);
1553 return (ENOBUFS);
1555 m->m_len = min(msize, sopt_size);
1556 sopt_size -= m->m_len;
1557 m_prev->m_next = m;
1558 m_prev = m;
1560 return (0);
1563 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1565 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1567 struct mbuf *m0 = m;
1569 if (sopt->sopt_val == NULL)
1570 return 0;
1571 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1572 if (sopt->sopt_td != NULL) {
1573 int error;
1575 error = copyin(sopt->sopt_val, mtod(m, char *),
1576 m->m_len);
1577 if (error != 0) {
1578 m_freem(m0);
1579 return (error);
1581 } else
1582 bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1583 sopt->sopt_valsize -= m->m_len;
1584 sopt->sopt_val = (caddr_t)sopt->sopt_val + m->m_len;
1585 m = m->m_next;
1587 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1588 panic("ip6_sooptmcopyin");
1589 return 0;
1592 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1594 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1596 struct mbuf *m0 = m;
1597 size_t valsize = 0;
1599 if (sopt->sopt_val == NULL)
1600 return 0;
1601 while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1602 if (sopt->sopt_td != NULL) {
1603 int error;
1605 error = copyout(mtod(m, char *), sopt->sopt_val,
1606 m->m_len);
1607 if (error != 0) {
1608 m_freem(m0);
1609 return (error);
1611 } else
1612 bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1613 sopt->sopt_valsize -= m->m_len;
1614 sopt->sopt_val = (caddr_t)sopt->sopt_val + m->m_len;
1615 valsize += m->m_len;
1616 m = m->m_next;
1618 if (m != NULL) {
1619 /* enough soopt buffer should be given from user-land */
1620 m_freem(m0);
1621 return (EINVAL);
1623 sopt->sopt_valsize = valsize;
1624 return 0;
1627 void
1628 sohasoutofband(struct socket *so)
1630 if (so->so_sigio != NULL)
1631 pgsigio(so->so_sigio, SIGURG, 0);
1632 selwakeup(&so->so_rcv.ssb_sel);
1636 sopoll(struct socket *so, int events, struct ucred *cred, struct thread *td)
1638 int revents = 0;
1640 crit_enter();
1642 if (events & (POLLIN | POLLRDNORM))
1643 if (soreadable(so))
1644 revents |= events & (POLLIN | POLLRDNORM);
1646 if (events & POLLINIGNEOF)
1647 if (so->so_rcv.ssb_cc >= so->so_rcv.ssb_lowat ||
1648 !TAILQ_EMPTY(&so->so_comp) || so->so_error)
1649 revents |= POLLINIGNEOF;
1651 if (events & (POLLOUT | POLLWRNORM))
1652 if (sowriteable(so))
1653 revents |= events & (POLLOUT | POLLWRNORM);
1655 if (events & (POLLPRI | POLLRDBAND))
1656 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1657 revents |= events & (POLLPRI | POLLRDBAND);
1659 if (revents == 0) {
1660 if (events &
1661 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM |
1662 POLLRDBAND)) {
1663 selrecord(td, &so->so_rcv.ssb_sel);
1664 so->so_rcv.ssb_flags |= SSB_SEL;
1667 if (events & (POLLOUT | POLLWRNORM)) {
1668 selrecord(td, &so->so_snd.ssb_sel);
1669 so->so_snd.ssb_flags |= SSB_SEL;
1673 crit_exit();
1674 return (revents);
1678 sokqfilter(struct file *fp, struct knote *kn)
1680 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1681 struct signalsockbuf *ssb;
1683 switch (kn->kn_filter) {
1684 case EVFILT_READ:
1685 if (so->so_options & SO_ACCEPTCONN)
1686 kn->kn_fop = &solisten_filtops;
1687 else
1688 kn->kn_fop = &soread_filtops;
1689 ssb = &so->so_rcv;
1690 break;
1691 case EVFILT_WRITE:
1692 kn->kn_fop = &sowrite_filtops;
1693 ssb = &so->so_snd;
1694 break;
1695 default:
1696 return (1);
1699 crit_enter();
1700 SLIST_INSERT_HEAD(&ssb->ssb_sel.si_note, kn, kn_selnext);
1701 ssb->ssb_flags |= SSB_KNOTE;
1702 crit_exit();
1703 return (0);
1706 static void
1707 filt_sordetach(struct knote *kn)
1709 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1711 crit_enter();
1712 SLIST_REMOVE(&so->so_rcv.ssb_sel.si_note, kn, knote, kn_selnext);
1713 if (SLIST_EMPTY(&so->so_rcv.ssb_sel.si_note))
1714 so->so_rcv.ssb_flags &= ~SSB_KNOTE;
1715 crit_exit();
1718 /*ARGSUSED*/
1719 static int
1720 filt_soread(struct knote *kn, long hint)
1722 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1724 kn->kn_data = so->so_rcv.ssb_cc;
1725 if (so->so_state & SS_CANTRCVMORE) {
1726 kn->kn_flags |= EV_EOF;
1727 kn->kn_fflags = so->so_error;
1728 return (1);
1730 if (so->so_error) /* temporary udp error */
1731 return (1);
1732 if (kn->kn_sfflags & NOTE_LOWAT)
1733 return (kn->kn_data >= kn->kn_sdata);
1734 return (kn->kn_data >= so->so_rcv.ssb_lowat);
1737 static void
1738 filt_sowdetach(struct knote *kn)
1740 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1742 crit_enter();
1743 SLIST_REMOVE(&so->so_snd.ssb_sel.si_note, kn, knote, kn_selnext);
1744 if (SLIST_EMPTY(&so->so_snd.ssb_sel.si_note))
1745 so->so_snd.ssb_flags &= ~SSB_KNOTE;
1746 crit_exit();
1749 /*ARGSUSED*/
1750 static int
1751 filt_sowrite(struct knote *kn, long hint)
1753 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1755 kn->kn_data = ssb_space(&so->so_snd);
1756 if (so->so_state & SS_CANTSENDMORE) {
1757 kn->kn_flags |= EV_EOF;
1758 kn->kn_fflags = so->so_error;
1759 return (1);
1761 if (so->so_error) /* temporary udp error */
1762 return (1);
1763 if (((so->so_state & SS_ISCONNECTED) == 0) &&
1764 (so->so_proto->pr_flags & PR_CONNREQUIRED))
1765 return (0);
1766 if (kn->kn_sfflags & NOTE_LOWAT)
1767 return (kn->kn_data >= kn->kn_sdata);
1768 return (kn->kn_data >= so->so_snd.ssb_lowat);
1771 /*ARGSUSED*/
1772 static int
1773 filt_solisten(struct knote *kn, long hint)
1775 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1777 kn->kn_data = so->so_qlen;
1778 return (! TAILQ_EMPTY(&so->so_comp));