installer: Re-add the TEST_DEV command which was removed accidentally.
[dragonfly.git] / sys / kern / uipc_socket.c
blobcd2d27c55cab0c232bc00e95546d59748cece3e3
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.55 2008/09/02 16:17:52 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>
96 #include <vm/pmap.h>
98 #include <sys/thread2.h>
99 #include <sys/socketvar2.h>
101 #include <machine/limits.h>
103 #ifdef INET
104 static int do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
105 #endif /* INET */
107 static void filt_sordetach(struct knote *kn);
108 static int filt_soread(struct knote *kn, long hint);
109 static void filt_sowdetach(struct knote *kn);
110 static int filt_sowrite(struct knote *kn, long hint);
111 static int filt_solisten(struct knote *kn, long hint);
113 static struct filterops solisten_filtops =
114 { 1, NULL, filt_sordetach, filt_solisten };
115 static struct filterops soread_filtops =
116 { 1, NULL, filt_sordetach, filt_soread };
117 static struct filterops sowrite_filtops =
118 { 1, NULL, filt_sowdetach, filt_sowrite };
120 MALLOC_DEFINE(M_SOCKET, "socket", "socket struct");
121 MALLOC_DEFINE(M_SONAME, "soname", "socket name");
122 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
125 static int somaxconn = SOMAXCONN;
126 SYSCTL_INT(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLFLAG_RW,
127 &somaxconn, 0, "Maximum pending socket connection queue size");
130 * Socket operation routines.
131 * These routines are called by the routines in
132 * sys_socket.c or from a system process, and
133 * implement the semantics of socket operations by
134 * switching out to the protocol specific routines.
138 * Get a socket structure, and initialize it.
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;
147 unsigned waitmask;
149 waitmask = waitok ? M_WAITOK : M_NOWAIT;
150 so = kmalloc(sizeof(struct socket), M_SOCKET, M_ZERO|waitmask);
151 if (so) {
152 /* XXX race condition for reentrant kernel */
153 TAILQ_INIT(&so->so_aiojobq);
154 TAILQ_INIT(&so->so_rcv.ssb_sel.si_mlist);
155 TAILQ_INIT(&so->so_snd.ssb_sel.si_mlist);
157 return so;
161 socreate(int dom, struct socket **aso, int type,
162 int proto, struct thread *td)
164 struct proc *p = td->td_proc;
165 struct protosw *prp;
166 struct socket *so;
167 struct pru_attach_info ai;
168 int error;
170 if (proto)
171 prp = pffindproto(dom, proto, type);
172 else
173 prp = pffindtype(dom, type);
175 if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
176 return (EPROTONOSUPPORT);
178 if (p->p_ucred->cr_prison && jail_socket_unixiproute_only &&
179 prp->pr_domain->dom_family != PF_LOCAL &&
180 prp->pr_domain->dom_family != PF_INET &&
181 prp->pr_domain->dom_family != PF_INET6 &&
182 prp->pr_domain->dom_family != PF_ROUTE) {
183 return (EPROTONOSUPPORT);
186 if (prp->pr_type != type)
187 return (EPROTOTYPE);
188 so = soalloc(p != 0);
189 if (so == 0)
190 return (ENOBUFS);
192 TAILQ_INIT(&so->so_incomp);
193 TAILQ_INIT(&so->so_comp);
194 so->so_type = type;
195 so->so_cred = crhold(p->p_ucred);
196 so->so_proto = prp;
197 ai.sb_rlimit = &p->p_rlimit[RLIMIT_SBSIZE];
198 ai.p_ucred = p->p_ucred;
199 ai.fd_rdir = p->p_fd->fd_rdir;
200 error = so_pru_attach(so, proto, &ai);
201 if (error) {
202 so->so_state |= SS_NOFDREF;
203 sofree(so);
204 return (error);
206 *aso = so;
207 return (0);
211 sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
213 int error;
215 crit_enter();
216 error = so_pru_bind(so, nam, td);
217 crit_exit();
218 return (error);
221 void
222 sodealloc(struct socket *so)
224 if (so->so_rcv.ssb_hiwat)
225 (void)chgsbsize(so->so_cred->cr_uidinfo,
226 &so->so_rcv.ssb_hiwat, 0, RLIM_INFINITY);
227 if (so->so_snd.ssb_hiwat)
228 (void)chgsbsize(so->so_cred->cr_uidinfo,
229 &so->so_snd.ssb_hiwat, 0, RLIM_INFINITY);
230 #ifdef INET
231 /* remove accept filter if present */
232 if (so->so_accf != NULL)
233 do_setopt_accept_filter(so, NULL);
234 #endif /* INET */
235 crfree(so->so_cred);
236 kfree(so, M_SOCKET);
240 solisten(struct socket *so, int backlog, struct thread *td)
242 int error;
243 #ifdef SCTP
244 short oldopt, oldqlimit;
245 #endif /* SCTP */
247 crit_enter();
248 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING)) {
249 crit_exit();
250 return (EINVAL);
253 #ifdef SCTP
254 oldopt = so->so_options;
255 oldqlimit = so->so_qlimit;
256 #endif /* SCTP */
258 if (TAILQ_EMPTY(&so->so_comp))
259 so->so_options |= SO_ACCEPTCONN;
260 if (backlog < 0 || backlog > somaxconn)
261 backlog = somaxconn;
262 so->so_qlimit = backlog;
263 /* SCTP needs to look at tweak both the inbound backlog parameter AND
264 * the so_options (UDP model both connect's and gets inbound
265 * connections .. implicitly).
267 error = so_pru_listen(so, td);
268 if (error) {
269 #ifdef SCTP
270 /* Restore the params */
271 so->so_options = oldopt;
272 so->so_qlimit = oldqlimit;
273 #endif /* SCTP */
274 crit_exit();
275 return (error);
277 crit_exit();
278 return (0);
282 * Destroy a disconnected socket. This routine is a NOP if entities
283 * still have a reference on the socket:
285 * so_pcb - The protocol stack still has a reference
286 * SS_NOFDREF - There is no longer a file pointer reference
287 * SS_ABORTING - An abort netmsg is in-flight
289 void
290 sofree(struct socket *so)
292 struct socket *head = so->so_head;
294 if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
295 return;
296 if (so->so_state & SS_ABORTING)
297 return;
298 if (head != NULL) {
299 if (so->so_state & SS_INCOMP) {
300 TAILQ_REMOVE(&head->so_incomp, so, so_list);
301 head->so_incqlen--;
302 } else if (so->so_state & SS_COMP) {
304 * We must not decommission a socket that's
305 * on the accept(2) queue. If we do, then
306 * accept(2) may hang after select(2) indicated
307 * that the listening socket was ready.
309 return;
310 } else {
311 panic("sofree: not queued");
313 so->so_state &= ~SS_INCOMP;
314 so->so_head = NULL;
316 ssb_release(&so->so_snd, so);
317 sorflush(so);
318 sodealloc(so);
322 * Close a socket on last file table reference removal.
323 * Initiate disconnect if connected.
324 * Free socket when disconnect complete.
327 soclose(struct socket *so, int fflag)
329 int error = 0;
331 crit_enter();
332 funsetown(so->so_sigio);
333 if (so->so_pcb == NULL)
334 goto discard;
335 if (so->so_state & SS_ISCONNECTED) {
336 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
337 error = sodisconnect(so);
338 if (error)
339 goto drop;
341 if (so->so_options & SO_LINGER) {
342 if ((so->so_state & SS_ISDISCONNECTING) &&
343 (fflag & FNONBLOCK))
344 goto drop;
345 while (so->so_state & SS_ISCONNECTED) {
346 error = tsleep((caddr_t)&so->so_timeo,
347 PCATCH, "soclos", so->so_linger * hz);
348 if (error)
349 break;
353 drop:
354 if (so->so_pcb) {
355 int error2;
357 error2 = so_pru_detach(so);
358 if (error == 0)
359 error = error2;
361 discard:
362 if (so->so_options & SO_ACCEPTCONN) {
363 struct socket *sp;
365 while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
366 TAILQ_REMOVE(&so->so_incomp, sp, so_list);
367 sp->so_state &= ~SS_INCOMP;
368 sp->so_head = NULL;
369 so->so_incqlen--;
370 soaborta(sp);
372 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
373 TAILQ_REMOVE(&so->so_comp, sp, so_list);
374 sp->so_state &= ~SS_COMP;
375 sp->so_head = NULL;
376 so->so_qlen--;
377 soaborta(sp);
380 if (so->so_state & SS_NOFDREF)
381 panic("soclose: NOFDREF");
382 so->so_state |= SS_NOFDREF;
383 sofree(so);
384 crit_exit();
385 return (error);
389 * Abort and destroy a socket. Only one abort can be in progress
390 * at any given moment.
392 void
393 soabort(struct socket *so)
395 if ((so->so_state & SS_ABORTING) == 0) {
396 so->so_state |= SS_ABORTING;
397 so_pru_abort(so);
401 void
402 soaborta(struct socket *so)
404 if ((so->so_state & SS_ABORTING) == 0) {
405 so->so_state |= SS_ABORTING;
406 so_pru_aborta(so);
410 void
411 soabort_oncpu(struct socket *so)
413 if ((so->so_state & SS_ABORTING) == 0) {
414 so->so_state |= SS_ABORTING;
415 so_pru_abort_oncpu(so);
420 soaccept(struct socket *so, struct sockaddr **nam)
422 int error;
424 crit_enter();
425 if ((so->so_state & SS_NOFDREF) == 0)
426 panic("soaccept: !NOFDREF");
427 so->so_state &= ~SS_NOFDREF;
428 error = so_pru_accept(so, nam);
429 crit_exit();
430 return (error);
434 soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
436 int error;
438 if (so->so_options & SO_ACCEPTCONN)
439 return (EOPNOTSUPP);
440 crit_enter();
442 * If protocol is connection-based, can only connect once.
443 * Otherwise, if connected, try to disconnect first.
444 * This allows user to disconnect by connecting to, e.g.,
445 * a null address.
447 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
448 ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
449 (error = sodisconnect(so)))) {
450 error = EISCONN;
451 } else {
453 * Prevent accumulated error from previous connection
454 * from biting us.
456 so->so_error = 0;
457 error = so_pru_connect(so, nam, td);
459 crit_exit();
460 return (error);
464 soconnect2(struct socket *so1, struct socket *so2)
466 int error;
468 crit_enter();
469 error = so_pru_connect2(so1, so2);
470 crit_exit();
471 return (error);
475 sodisconnect(struct socket *so)
477 int error;
479 crit_enter();
480 if ((so->so_state & SS_ISCONNECTED) == 0) {
481 error = ENOTCONN;
482 goto bad;
484 if (so->so_state & SS_ISDISCONNECTING) {
485 error = EALREADY;
486 goto bad;
488 error = so_pru_disconnect(so);
489 bad:
490 crit_exit();
491 return (error);
494 #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
496 * Send on a socket.
497 * If send must go all at once and message is larger than
498 * send buffering, then hard error.
499 * Lock against other senders.
500 * If must go all at once and not enough room now, then
501 * inform user that this would block and do nothing.
502 * Otherwise, if nonblocking, send as much as possible.
503 * The data to be sent is described by "uio" if nonzero,
504 * otherwise by the mbuf chain "top" (which must be null
505 * if uio is not). Data provided in mbuf chain must be small
506 * enough to send all at once.
508 * Returns nonzero on error, timeout or signal; callers
509 * must check for short counts if EINTR/ERESTART are returned.
510 * Data and control buffers are freed on return.
513 sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
514 struct mbuf *top, struct mbuf *control, int flags,
515 struct thread *td)
517 struct mbuf **mp;
518 struct mbuf *m;
519 long space, len, resid;
520 int clen = 0, error, dontroute, mlen;
521 int atomic = sosendallatonce(so) || top;
522 int pru_flags;
524 if (uio)
525 resid = uio->uio_resid;
526 else
527 resid = top->m_pkthdr.len;
529 * In theory resid should be unsigned.
530 * However, space must be signed, as it might be less than 0
531 * if we over-committed, and we must use a signed comparison
532 * of space and resid. On the other hand, a negative resid
533 * causes us to loop sending 0-length segments to the protocol.
535 * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
536 * type sockets since that's an error.
538 if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
539 error = EINVAL;
540 goto out;
543 dontroute =
544 (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
545 (so->so_proto->pr_flags & PR_ATOMIC);
546 if (td->td_lwp != NULL)
547 td->td_lwp->lwp_ru.ru_msgsnd++;
548 if (control)
549 clen = control->m_len;
550 #define gotoerr(errcode) { error = errcode; crit_exit(); goto release; }
552 restart:
553 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
554 if (error)
555 goto out;
556 do {
557 crit_enter();
558 if (so->so_state & SS_CANTSENDMORE)
559 gotoerr(EPIPE);
560 if (so->so_error) {
561 error = so->so_error;
562 so->so_error = 0;
563 crit_exit();
564 goto release;
566 if ((so->so_state & SS_ISCONNECTED) == 0) {
568 * `sendto' and `sendmsg' is allowed on a connection-
569 * based socket if it supports implied connect.
570 * Return ENOTCONN if not connected and no address is
571 * supplied.
573 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
574 (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
575 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
576 !(resid == 0 && clen != 0))
577 gotoerr(ENOTCONN);
578 } else if (addr == 0)
579 gotoerr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
580 ENOTCONN : EDESTADDRREQ);
582 if ((atomic && resid > so->so_snd.ssb_hiwat) ||
583 clen > so->so_snd.ssb_hiwat) {
584 gotoerr(EMSGSIZE);
586 space = ssb_space(&so->so_snd);
587 if (flags & MSG_OOB)
588 space += 1024;
589 if (space < resid + clen && uio &&
590 (atomic || space < so->so_snd.ssb_lowat || space < clen)) {
591 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
592 gotoerr(EWOULDBLOCK);
593 ssb_unlock(&so->so_snd);
594 error = ssb_wait(&so->so_snd);
595 crit_exit();
596 if (error)
597 goto out;
598 goto restart;
600 crit_exit();
601 mp = &top;
602 space -= clen;
603 do {
604 if (uio == NULL) {
606 * Data is prepackaged in "top".
608 resid = 0;
609 if (flags & MSG_EOR)
610 top->m_flags |= M_EOR;
611 } else do {
612 m = m_getl(resid, MB_WAIT, MT_DATA,
613 top == NULL ? M_PKTHDR : 0, &mlen);
614 if (top == NULL) {
615 m->m_pkthdr.len = 0;
616 m->m_pkthdr.rcvif = (struct ifnet *)0;
618 len = min(min(mlen, resid), space);
619 if (resid < MINCLSIZE) {
621 * For datagram protocols, leave room
622 * for protocol headers in first mbuf.
624 if (atomic && top == 0 && len < mlen)
625 MH_ALIGN(m, len);
627 space -= len;
628 error = uiomove(mtod(m, caddr_t), (int)len, uio);
629 resid = uio->uio_resid;
630 m->m_len = len;
631 *mp = m;
632 top->m_pkthdr.len += len;
633 if (error)
634 goto release;
635 mp = &m->m_next;
636 if (resid <= 0) {
637 if (flags & MSG_EOR)
638 top->m_flags |= M_EOR;
639 break;
641 } while (space > 0 && atomic);
642 if (dontroute)
643 so->so_options |= SO_DONTROUTE;
644 if (flags & MSG_OOB) {
645 pru_flags = PRUS_OOB;
646 } else if ((flags & MSG_EOF) &&
647 (so->so_proto->pr_flags & PR_IMPLOPCL) &&
648 (resid <= 0)) {
650 * If the user set MSG_EOF, the protocol
651 * understands this flag and nothing left to
652 * send then use PRU_SEND_EOF instead of PRU_SEND.
654 pru_flags = PRUS_EOF;
655 } else if (resid > 0 && space > 0) {
656 /* If there is more to send, set PRUS_MORETOCOME */
657 pru_flags = PRUS_MORETOCOME;
658 } else {
659 pru_flags = 0;
661 crit_enter();
663 * XXX all the SS_CANTSENDMORE checks previously
664 * done could be out of date. We could have recieved
665 * a reset packet in an interrupt or maybe we slept
666 * while doing page faults in uiomove() etc. We could
667 * probably recheck again inside the splnet() protection
668 * here, but there are probably other places that this
669 * also happens. We must rethink this.
671 error = so_pru_send(so, pru_flags, top, addr, control, td);
672 crit_exit();
673 if (dontroute)
674 so->so_options &= ~SO_DONTROUTE;
675 clen = 0;
676 control = 0;
677 top = 0;
678 mp = &top;
679 if (error)
680 goto release;
681 } while (resid && space > 0);
682 } while (resid);
684 release:
685 ssb_unlock(&so->so_snd);
686 out:
687 if (top)
688 m_freem(top);
689 if (control)
690 m_freem(control);
691 return (error);
695 * A specialization of sosend() for UDP based on protocol-specific knowledge:
696 * so->so_proto->pr_flags has the PR_ATOMIC field set. This means that
697 * sosendallatonce() returns true,
698 * the "atomic" variable is true,
699 * and sosendudp() blocks until space is available for the entire send.
700 * so->so_proto->pr_flags does not have the PR_CONNREQUIRED or
701 * PR_IMPLOPCL flags set.
702 * UDP has no out-of-band data.
703 * UDP has no control data.
704 * UDP does not support MSG_EOR.
707 sosendudp(struct socket *so, struct sockaddr *addr, struct uio *uio,
708 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
710 int resid, error;
711 boolean_t dontroute; /* temporary SO_DONTROUTE setting */
713 if (td->td_lwp != NULL)
714 td->td_lwp->lwp_ru.ru_msgsnd++;
715 if (control)
716 m_freem(control);
718 KASSERT((uio && !top) || (top && !uio), ("bad arguments to sosendudp"));
719 resid = uio ? uio->uio_resid : top->m_pkthdr.len;
721 restart:
722 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
723 if (error)
724 goto out;
726 crit_enter();
727 if (so->so_state & SS_CANTSENDMORE)
728 gotoerr(EPIPE);
729 if (so->so_error) {
730 error = so->so_error;
731 so->so_error = 0;
732 crit_exit();
733 goto release;
735 if (!(so->so_state & SS_ISCONNECTED) && addr == NULL)
736 gotoerr(EDESTADDRREQ);
737 if (resid > so->so_snd.ssb_hiwat)
738 gotoerr(EMSGSIZE);
739 if (uio && ssb_space(&so->so_snd) < resid) {
740 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT))
741 gotoerr(EWOULDBLOCK);
742 ssb_unlock(&so->so_snd);
743 error = ssb_wait(&so->so_snd);
744 crit_exit();
745 if (error)
746 goto out;
747 goto restart;
749 crit_exit();
751 if (uio) {
752 top = m_uiomove(uio);
753 if (top == NULL)
754 goto release;
757 dontroute = (flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE);
758 if (dontroute)
759 so->so_options |= SO_DONTROUTE;
761 error = so_pru_send(so, 0, top, addr, NULL, td);
762 top = NULL; /* sent or freed in lower layer */
764 if (dontroute)
765 so->so_options &= ~SO_DONTROUTE;
767 release:
768 ssb_unlock(&so->so_snd);
769 out:
770 if (top)
771 m_freem(top);
772 return (error);
776 * Implement receive operations on a socket.
777 * We depend on the way that records are added to the signalsockbuf
778 * by sbappend*. In particular, each record (mbufs linked through m_next)
779 * must begin with an address if the protocol so specifies,
780 * followed by an optional mbuf or mbufs containing ancillary data,
781 * and then zero or more mbufs of data.
782 * In order to avoid blocking network interrupts for the entire time here,
783 * we exit the critical section while doing the actual copy to user space.
784 * Although the signalsockbuf is locked, new data may still be appended,
785 * and thus we must maintain consistency of the signalsockbuf during that time.
787 * The caller may receive the data as a single mbuf chain by supplying
788 * an mbuf **mp0 for use in returning the chain. The uio is then used
789 * only for the count in uio_resid.
792 soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
793 struct sockbuf *sio, struct mbuf **controlp, int *flagsp)
795 struct mbuf *m, *n;
796 struct mbuf *free_chain = NULL;
797 int flags, len, error, offset;
798 struct protosw *pr = so->so_proto;
799 int moff, type = 0;
800 int resid, orig_resid;
802 if (uio)
803 resid = uio->uio_resid;
804 else
805 resid = (int)(sio->sb_climit - sio->sb_cc);
806 orig_resid = resid;
808 if (psa)
809 *psa = NULL;
810 if (controlp)
811 *controlp = NULL;
812 if (flagsp)
813 flags = *flagsp &~ MSG_EOR;
814 else
815 flags = 0;
816 if (flags & MSG_OOB) {
817 m = m_get(MB_WAIT, MT_DATA);
818 if (m == NULL)
819 return (ENOBUFS);
820 error = so_pru_rcvoob(so, m, flags & MSG_PEEK);
821 if (error)
822 goto bad;
823 if (sio) {
824 do {
825 sbappend(sio, m);
826 resid -= m->m_len;
827 } while (resid > 0 && m);
828 } else {
829 do {
830 uio->uio_resid = resid;
831 error = uiomove(mtod(m, caddr_t),
832 (int)min(resid, m->m_len), uio);
833 resid = uio->uio_resid;
834 m = m_free(m);
835 } while (uio->uio_resid && error == 0 && m);
837 bad:
838 if (m)
839 m_freem(m);
840 return (error);
842 if (so->so_state & SS_ISCONFIRMING && resid)
843 so_pru_rcvd(so, 0);
845 restart:
846 crit_enter();
847 error = ssb_lock(&so->so_rcv, SBLOCKWAIT(flags));
848 if (error)
849 goto done;
851 m = so->so_rcv.ssb_mb;
853 * If we have less data than requested, block awaiting more
854 * (subject to any timeout) if:
855 * 1. the current count is less than the low water mark, or
856 * 2. MSG_WAITALL is set, and it is possible to do the entire
857 * receive operation at once if we block (resid <= hiwat).
858 * 3. MSG_DONTWAIT is not set
859 * If MSG_WAITALL is set but resid is larger than the receive buffer,
860 * we have to do the receive in sections, and thus risk returning
861 * a short count if a timeout or signal occurs after we start.
863 if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
864 so->so_rcv.ssb_cc < resid) &&
865 (so->so_rcv.ssb_cc < so->so_rcv.ssb_lowat ||
866 ((flags & MSG_WAITALL) && resid <= so->so_rcv.ssb_hiwat)) &&
867 m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
868 KASSERT(m != NULL || !so->so_rcv.ssb_cc, ("receive 1"));
869 if (so->so_error) {
870 if (m)
871 goto dontblock;
872 error = so->so_error;
873 if ((flags & MSG_PEEK) == 0)
874 so->so_error = 0;
875 goto release;
877 if (so->so_state & SS_CANTRCVMORE) {
878 if (m)
879 goto dontblock;
880 else
881 goto release;
883 for (; m; m = m->m_next) {
884 if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
885 m = so->so_rcv.ssb_mb;
886 goto dontblock;
889 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
890 (pr->pr_flags & PR_CONNREQUIRED)) {
891 error = ENOTCONN;
892 goto release;
894 if (resid == 0)
895 goto release;
896 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
897 error = EWOULDBLOCK;
898 goto release;
900 ssb_unlock(&so->so_rcv);
901 error = ssb_wait(&so->so_rcv);
902 if (error)
903 goto done;
904 crit_exit();
905 goto restart;
907 dontblock:
908 if (uio && uio->uio_td && uio->uio_td->td_proc)
909 uio->uio_td->td_lwp->lwp_ru.ru_msgrcv++;
912 * note: m should be == sb_mb here. Cache the next record while
913 * cleaning up. Note that calling m_free*() will break out critical
914 * section.
916 KKASSERT(m == so->so_rcv.ssb_mb);
919 * Skip any address mbufs prepending the record.
921 if (pr->pr_flags & PR_ADDR) {
922 KASSERT(m->m_type == MT_SONAME, ("receive 1a"));
923 orig_resid = 0;
924 if (psa)
925 *psa = dup_sockaddr(mtod(m, struct sockaddr *));
926 if (flags & MSG_PEEK)
927 m = m->m_next;
928 else
929 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
933 * Skip any control mbufs prepending the record.
935 #ifdef SCTP
936 if (pr->pr_flags & PR_ADDR_OPT) {
938 * For SCTP we may be getting a
939 * whole message OR a partial delivery.
941 if (m && m->m_type == MT_SONAME) {
942 orig_resid = 0;
943 if (psa)
944 *psa = dup_sockaddr(mtod(m, struct sockaddr *));
945 if (flags & MSG_PEEK)
946 m = m->m_next;
947 else
948 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
951 #endif /* SCTP */
952 while (m && m->m_type == MT_CONTROL && error == 0) {
953 if (flags & MSG_PEEK) {
954 if (controlp)
955 *controlp = m_copy(m, 0, m->m_len);
956 m = m->m_next; /* XXX race */
957 } else {
958 if (controlp) {
959 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
960 if (pr->pr_domain->dom_externalize &&
961 mtod(m, struct cmsghdr *)->cmsg_type ==
962 SCM_RIGHTS)
963 error = (*pr->pr_domain->dom_externalize)(m);
964 *controlp = m;
965 m = n;
966 } else {
967 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
970 if (controlp && *controlp) {
971 orig_resid = 0;
972 controlp = &(*controlp)->m_next;
977 * flag OOB data.
979 if (m) {
980 type = m->m_type;
981 if (type == MT_OOBDATA)
982 flags |= MSG_OOB;
986 * Copy to the UIO or mbuf return chain (*mp).
988 moff = 0;
989 offset = 0;
990 while (m && resid > 0 && error == 0) {
991 if (m->m_type == MT_OOBDATA) {
992 if (type != MT_OOBDATA)
993 break;
994 } else if (type == MT_OOBDATA)
995 break;
996 else
997 KASSERT(m->m_type == MT_DATA || m->m_type == MT_HEADER,
998 ("receive 3"));
999 so->so_state &= ~SS_RCVATMARK;
1000 len = resid;
1001 if (so->so_oobmark && len > so->so_oobmark - offset)
1002 len = so->so_oobmark - offset;
1003 if (len > m->m_len - moff)
1004 len = m->m_len - moff;
1007 * Copy out to the UIO or pass the mbufs back to the SIO.
1008 * The SIO is dealt with when we eat the mbuf, but deal
1009 * with the resid here either way.
1011 if (uio) {
1012 crit_exit();
1013 uio->uio_resid = resid;
1014 error = uiomove(mtod(m, caddr_t) + moff, len, uio);
1015 resid = uio->uio_resid;
1016 crit_enter();
1017 if (error)
1018 goto release;
1019 } else {
1020 resid -= len;
1024 * Eat the entire mbuf or just a piece of it
1026 if (len == m->m_len - moff) {
1027 if (m->m_flags & M_EOR)
1028 flags |= MSG_EOR;
1029 #ifdef SCTP
1030 if (m->m_flags & M_NOTIFICATION)
1031 flags |= MSG_NOTIFICATION;
1032 #endif /* SCTP */
1033 if (flags & MSG_PEEK) {
1034 m = m->m_next;
1035 moff = 0;
1036 } else {
1037 if (sio) {
1038 n = sbunlinkmbuf(&so->so_rcv.sb, m, NULL);
1039 sbappend(sio, m);
1040 m = n;
1041 } else {
1042 m = sbunlinkmbuf(&so->so_rcv.sb, m, &free_chain);
1045 } else {
1046 if (flags & MSG_PEEK) {
1047 moff += len;
1048 } else {
1049 if (sio) {
1050 n = m_copym(m, 0, len, MB_WAIT);
1051 if (n)
1052 sbappend(sio, n);
1054 m->m_data += len;
1055 m->m_len -= len;
1056 so->so_rcv.ssb_cc -= len;
1059 if (so->so_oobmark) {
1060 if ((flags & MSG_PEEK) == 0) {
1061 so->so_oobmark -= len;
1062 if (so->so_oobmark == 0) {
1063 so->so_state |= SS_RCVATMARK;
1064 break;
1066 } else {
1067 offset += len;
1068 if (offset == so->so_oobmark)
1069 break;
1072 if (flags & MSG_EOR)
1073 break;
1075 * If the MSG_WAITALL flag is set (for non-atomic socket),
1076 * we must not quit until resid == 0 or an error
1077 * termination. If a signal/timeout occurs, return
1078 * with a short count but without error.
1079 * Keep signalsockbuf locked against other readers.
1081 while ((flags & MSG_WAITALL) && m == NULL &&
1082 resid > 0 && !sosendallatonce(so) &&
1083 so->so_rcv.ssb_mb == NULL) {
1084 if (so->so_error || so->so_state & SS_CANTRCVMORE)
1085 break;
1087 * The window might have closed to zero, make
1088 * sure we send an ack now that we've drained
1089 * the buffer or we might end up blocking until
1090 * the idle takes over (5 seconds).
1092 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
1093 so_pru_rcvd(so, flags);
1094 error = ssb_wait(&so->so_rcv);
1095 if (error) {
1096 ssb_unlock(&so->so_rcv);
1097 error = 0;
1098 goto done;
1100 m = so->so_rcv.ssb_mb;
1105 * If an atomic read was requested but unread data still remains
1106 * in the record, set MSG_TRUNC.
1108 if (m && pr->pr_flags & PR_ATOMIC)
1109 flags |= MSG_TRUNC;
1112 * Cleanup. If an atomic read was requested drop any unread data.
1114 if ((flags & MSG_PEEK) == 0) {
1115 if (m && (pr->pr_flags & PR_ATOMIC))
1116 sbdroprecord(&so->so_rcv.sb);
1117 if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
1118 so_pru_rcvd(so, flags);
1121 if (orig_resid == resid && orig_resid &&
1122 (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
1123 ssb_unlock(&so->so_rcv);
1124 crit_exit();
1125 goto restart;
1128 if (flagsp)
1129 *flagsp |= flags;
1130 release:
1131 ssb_unlock(&so->so_rcv);
1132 done:
1133 crit_exit();
1134 if (free_chain)
1135 m_freem(free_chain);
1136 return (error);
1140 soshutdown(struct socket *so, int how)
1142 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1143 return (EINVAL);
1145 if (how != SHUT_WR)
1146 sorflush(so);
1147 if (how != SHUT_RD)
1148 return (so_pru_shutdown(so));
1149 return (0);
1152 void
1153 sorflush(struct socket *so)
1155 struct signalsockbuf *ssb = &so->so_rcv;
1156 struct protosw *pr = so->so_proto;
1157 struct signalsockbuf asb;
1159 ssb->ssb_flags |= SSB_NOINTR;
1160 (void) ssb_lock(ssb, M_WAITOK);
1162 crit_enter();
1163 socantrcvmore(so);
1164 ssb_unlock(ssb);
1165 asb = *ssb;
1166 bzero((caddr_t)ssb, sizeof (*ssb));
1167 if (asb.ssb_flags & SSB_KNOTE) {
1168 ssb->ssb_sel.si_note = asb.ssb_sel.si_note;
1169 ssb->ssb_flags = SSB_KNOTE;
1171 crit_exit();
1173 if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
1174 (*pr->pr_domain->dom_dispose)(asb.ssb_mb);
1175 ssb_release(&asb, so);
1178 #ifdef INET
1179 static int
1180 do_setopt_accept_filter(struct socket *so, struct sockopt *sopt)
1182 struct accept_filter_arg *afap = NULL;
1183 struct accept_filter *afp;
1184 struct so_accf *af = so->so_accf;
1185 int error = 0;
1187 /* do not set/remove accept filters on non listen sockets */
1188 if ((so->so_options & SO_ACCEPTCONN) == 0) {
1189 error = EINVAL;
1190 goto out;
1193 /* removing the filter */
1194 if (sopt == NULL) {
1195 if (af != NULL) {
1196 if (af->so_accept_filter != NULL &&
1197 af->so_accept_filter->accf_destroy != NULL) {
1198 af->so_accept_filter->accf_destroy(so);
1200 if (af->so_accept_filter_str != NULL) {
1201 FREE(af->so_accept_filter_str, M_ACCF);
1203 FREE(af, M_ACCF);
1204 so->so_accf = NULL;
1206 so->so_options &= ~SO_ACCEPTFILTER;
1207 return (0);
1209 /* adding a filter */
1210 /* must remove previous filter first */
1211 if (af != NULL) {
1212 error = EINVAL;
1213 goto out;
1215 /* don't put large objects on the kernel stack */
1216 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1217 error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1218 afap->af_name[sizeof(afap->af_name)-1] = '\0';
1219 afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1220 if (error)
1221 goto out;
1222 afp = accept_filt_get(afap->af_name);
1223 if (afp == NULL) {
1224 error = ENOENT;
1225 goto out;
1227 MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK | M_ZERO);
1228 if (afp->accf_create != NULL) {
1229 if (afap->af_name[0] != '\0') {
1230 int len = strlen(afap->af_name) + 1;
1232 MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1233 strcpy(af->so_accept_filter_str, afap->af_name);
1235 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1236 if (af->so_accept_filter_arg == NULL) {
1237 FREE(af->so_accept_filter_str, M_ACCF);
1238 FREE(af, M_ACCF);
1239 so->so_accf = NULL;
1240 error = EINVAL;
1241 goto out;
1244 af->so_accept_filter = afp;
1245 so->so_accf = af;
1246 so->so_options |= SO_ACCEPTFILTER;
1247 out:
1248 if (afap != NULL)
1249 FREE(afap, M_TEMP);
1250 return (error);
1252 #endif /* INET */
1255 * Perhaps this routine, and sooptcopyout(), below, ought to come in
1256 * an additional variant to handle the case where the option value needs
1257 * to be some kind of integer, but not a specific size.
1258 * In addition to their use here, these functions are also called by the
1259 * protocol-level pr_ctloutput() routines.
1262 sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
1264 return soopt_to_kbuf(sopt, buf, len, minlen);
1268 soopt_to_kbuf(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
1270 size_t valsize;
1272 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
1273 KKASSERT(kva_p(buf));
1276 * If the user gives us more than we wanted, we ignore it,
1277 * but if we don't get the minimum length the caller
1278 * wants, we return EINVAL. On success, sopt->sopt_valsize
1279 * is set to however much we actually retrieved.
1281 if ((valsize = sopt->sopt_valsize) < minlen)
1282 return EINVAL;
1283 if (valsize > len)
1284 sopt->sopt_valsize = valsize = len;
1286 bcopy(sopt->sopt_val, buf, valsize);
1287 return 0;
1292 sosetopt(struct socket *so, struct sockopt *sopt)
1294 int error, optval;
1295 struct linger l;
1296 struct timeval tv;
1297 u_long val;
1299 error = 0;
1300 sopt->sopt_dir = SOPT_SET;
1301 if (sopt->sopt_level != SOL_SOCKET) {
1302 if (so->so_proto && so->so_proto->pr_ctloutput) {
1303 return (so_pru_ctloutput(so, sopt));
1305 error = ENOPROTOOPT;
1306 } else {
1307 switch (sopt->sopt_name) {
1308 #ifdef INET
1309 case SO_ACCEPTFILTER:
1310 error = do_setopt_accept_filter(so, sopt);
1311 if (error)
1312 goto bad;
1313 break;
1314 #endif /* INET */
1315 case SO_LINGER:
1316 error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1317 if (error)
1318 goto bad;
1320 so->so_linger = l.l_linger;
1321 if (l.l_onoff)
1322 so->so_options |= SO_LINGER;
1323 else
1324 so->so_options &= ~SO_LINGER;
1325 break;
1327 case SO_DEBUG:
1328 case SO_KEEPALIVE:
1329 case SO_DONTROUTE:
1330 case SO_USELOOPBACK:
1331 case SO_BROADCAST:
1332 case SO_REUSEADDR:
1333 case SO_REUSEPORT:
1334 case SO_OOBINLINE:
1335 case SO_TIMESTAMP:
1336 error = sooptcopyin(sopt, &optval, sizeof optval,
1337 sizeof optval);
1338 if (error)
1339 goto bad;
1340 if (optval)
1341 so->so_options |= sopt->sopt_name;
1342 else
1343 so->so_options &= ~sopt->sopt_name;
1344 break;
1346 case SO_SNDBUF:
1347 case SO_RCVBUF:
1348 case SO_SNDLOWAT:
1349 case SO_RCVLOWAT:
1350 error = sooptcopyin(sopt, &optval, sizeof optval,
1351 sizeof optval);
1352 if (error)
1353 goto bad;
1356 * Values < 1 make no sense for any of these
1357 * options, so disallow them.
1359 if (optval < 1) {
1360 error = EINVAL;
1361 goto bad;
1364 switch (sopt->sopt_name) {
1365 case SO_SNDBUF:
1366 case SO_RCVBUF:
1367 if (ssb_reserve(sopt->sopt_name == SO_SNDBUF ?
1368 &so->so_snd : &so->so_rcv, (u_long)optval,
1370 &curproc->p_rlimit[RLIMIT_SBSIZE]) == 0) {
1371 error = ENOBUFS;
1372 goto bad;
1374 break;
1377 * Make sure the low-water is never greater than
1378 * the high-water.
1380 case SO_SNDLOWAT:
1381 so->so_snd.ssb_lowat =
1382 (optval > so->so_snd.ssb_hiwat) ?
1383 so->so_snd.ssb_hiwat : optval;
1384 break;
1385 case SO_RCVLOWAT:
1386 so->so_rcv.ssb_lowat =
1387 (optval > so->so_rcv.ssb_hiwat) ?
1388 so->so_rcv.ssb_hiwat : optval;
1389 break;
1391 break;
1393 case SO_SNDTIMEO:
1394 case SO_RCVTIMEO:
1395 error = sooptcopyin(sopt, &tv, sizeof tv,
1396 sizeof tv);
1397 if (error)
1398 goto bad;
1400 /* assert(hz > 0); */
1401 if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1402 tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1403 error = EDOM;
1404 goto bad;
1406 /* assert(tick > 0); */
1407 /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1408 val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1409 if (val > SHRT_MAX) {
1410 error = EDOM;
1411 goto bad;
1413 if (val == 0 && tv.tv_usec != 0)
1414 val = 1;
1416 switch (sopt->sopt_name) {
1417 case SO_SNDTIMEO:
1418 so->so_snd.ssb_timeo = val;
1419 break;
1420 case SO_RCVTIMEO:
1421 so->so_rcv.ssb_timeo = val;
1422 break;
1424 break;
1425 default:
1426 error = ENOPROTOOPT;
1427 break;
1429 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1430 (void) so_pru_ctloutput(so, sopt);
1433 bad:
1434 return (error);
1437 /* Helper routine for getsockopt */
1439 sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
1441 soopt_from_kbuf(sopt, buf, len);
1442 return 0;
1445 void
1446 soopt_from_kbuf(struct sockopt *sopt, const void *buf, size_t len)
1448 size_t valsize;
1450 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
1451 KKASSERT(kva_p(buf));
1454 * Documented get behavior is that we always return a value,
1455 * possibly truncated to fit in the user's buffer.
1456 * Traditional behavior is that we always tell the user
1457 * precisely how much we copied, rather than something useful
1458 * like the total amount we had available for her.
1459 * Note that this interface is not idempotent; the entire answer must
1460 * generated ahead of time.
1462 valsize = min(len, sopt->sopt_valsize);
1463 sopt->sopt_valsize = valsize;
1464 if (sopt->sopt_val != 0) {
1465 bcopy(buf, sopt->sopt_val, valsize);
1470 sogetopt(struct socket *so, struct sockopt *sopt)
1472 int error, optval;
1473 struct linger l;
1474 struct timeval tv;
1475 #ifdef INET
1476 struct accept_filter_arg *afap;
1477 #endif
1479 error = 0;
1480 sopt->sopt_dir = SOPT_GET;
1481 if (sopt->sopt_level != SOL_SOCKET) {
1482 if (so->so_proto && so->so_proto->pr_ctloutput) {
1483 return (so_pru_ctloutput(so, sopt));
1484 } else
1485 return (ENOPROTOOPT);
1486 } else {
1487 switch (sopt->sopt_name) {
1488 #ifdef INET
1489 case SO_ACCEPTFILTER:
1490 if ((so->so_options & SO_ACCEPTCONN) == 0)
1491 return (EINVAL);
1492 MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1493 M_TEMP, M_WAITOK | M_ZERO);
1494 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1495 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1496 if (so->so_accf->so_accept_filter_str != NULL)
1497 strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1499 error = sooptcopyout(sopt, afap, sizeof(*afap));
1500 FREE(afap, M_TEMP);
1501 break;
1502 #endif /* INET */
1504 case SO_LINGER:
1505 l.l_onoff = so->so_options & SO_LINGER;
1506 l.l_linger = so->so_linger;
1507 error = sooptcopyout(sopt, &l, sizeof l);
1508 break;
1510 case SO_USELOOPBACK:
1511 case SO_DONTROUTE:
1512 case SO_DEBUG:
1513 case SO_KEEPALIVE:
1514 case SO_REUSEADDR:
1515 case SO_REUSEPORT:
1516 case SO_BROADCAST:
1517 case SO_OOBINLINE:
1518 case SO_TIMESTAMP:
1519 optval = so->so_options & sopt->sopt_name;
1520 integer:
1521 error = sooptcopyout(sopt, &optval, sizeof optval);
1522 break;
1524 case SO_TYPE:
1525 optval = so->so_type;
1526 goto integer;
1528 case SO_ERROR:
1529 optval = so->so_error;
1530 so->so_error = 0;
1531 goto integer;
1533 case SO_SNDBUF:
1534 optval = so->so_snd.ssb_hiwat;
1535 goto integer;
1537 case SO_RCVBUF:
1538 optval = so->so_rcv.ssb_hiwat;
1539 goto integer;
1541 case SO_SNDLOWAT:
1542 optval = so->so_snd.ssb_lowat;
1543 goto integer;
1545 case SO_RCVLOWAT:
1546 optval = so->so_rcv.ssb_lowat;
1547 goto integer;
1549 case SO_SNDTIMEO:
1550 case SO_RCVTIMEO:
1551 optval = (sopt->sopt_name == SO_SNDTIMEO ?
1552 so->so_snd.ssb_timeo : so->so_rcv.ssb_timeo);
1554 tv.tv_sec = optval / hz;
1555 tv.tv_usec = (optval % hz) * tick;
1556 error = sooptcopyout(sopt, &tv, sizeof tv);
1557 break;
1559 default:
1560 error = ENOPROTOOPT;
1561 break;
1563 return (error);
1567 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1569 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1571 struct mbuf *m, *m_prev;
1572 int sopt_size = sopt->sopt_valsize, msize;
1574 m = m_getl(sopt_size, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_DATA,
1575 0, &msize);
1576 if (m == NULL)
1577 return (ENOBUFS);
1578 m->m_len = min(msize, sopt_size);
1579 sopt_size -= m->m_len;
1580 *mp = m;
1581 m_prev = m;
1583 while (sopt_size > 0) {
1584 m = m_getl(sopt_size, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT,
1585 MT_DATA, 0, &msize);
1586 if (m == NULL) {
1587 m_freem(*mp);
1588 return (ENOBUFS);
1590 m->m_len = min(msize, sopt_size);
1591 sopt_size -= m->m_len;
1592 m_prev->m_next = m;
1593 m_prev = m;
1595 return (0);
1598 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1600 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1602 soopt_to_mbuf(sopt, m);
1603 return 0;
1606 void
1607 soopt_to_mbuf(struct sockopt *sopt, struct mbuf *m)
1609 size_t valsize;
1610 void *val;
1612 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
1613 KKASSERT(kva_p(m));
1614 if (sopt->sopt_val == NULL)
1615 return;
1616 val = sopt->sopt_val;
1617 valsize = sopt->sopt_valsize;
1618 while (m != NULL && valsize >= m->m_len) {
1619 bcopy(val, mtod(m, char *), m->m_len);
1620 valsize -= m->m_len;
1621 val = (caddr_t)val + m->m_len;
1622 m = m->m_next;
1624 if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1625 panic("ip6_sooptmcopyin");
1628 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1630 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1632 return soopt_from_mbuf(sopt, m);
1636 soopt_from_mbuf(struct sockopt *sopt, struct mbuf *m)
1638 struct mbuf *m0 = m;
1639 size_t valsize = 0;
1640 size_t maxsize;
1641 void *val;
1643 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
1644 KKASSERT(kva_p(m));
1645 if (sopt->sopt_val == NULL)
1646 return 0;
1647 val = sopt->sopt_val;
1648 maxsize = sopt->sopt_valsize;
1649 while (m != NULL && maxsize >= m->m_len) {
1650 bcopy(mtod(m, char *), val, m->m_len);
1651 maxsize -= m->m_len;
1652 val = (caddr_t)val + m->m_len;
1653 valsize += m->m_len;
1654 m = m->m_next;
1656 if (m != NULL) {
1657 /* enough soopt buffer should be given from user-land */
1658 m_freem(m0);
1659 return (EINVAL);
1661 sopt->sopt_valsize = valsize;
1662 return 0;
1665 void
1666 sohasoutofband(struct socket *so)
1668 if (so->so_sigio != NULL)
1669 pgsigio(so->so_sigio, SIGURG, 0);
1670 selwakeup(&so->so_rcv.ssb_sel);
1674 sopoll(struct socket *so, int events, struct ucred *cred, struct thread *td)
1676 int revents = 0;
1678 crit_enter();
1680 if (events & (POLLIN | POLLRDNORM))
1681 if (soreadable(so))
1682 revents |= events & (POLLIN | POLLRDNORM);
1684 if (events & POLLINIGNEOF)
1685 if (so->so_rcv.ssb_cc >= so->so_rcv.ssb_lowat ||
1686 !TAILQ_EMPTY(&so->so_comp) || so->so_error)
1687 revents |= POLLINIGNEOF;
1689 if (events & (POLLOUT | POLLWRNORM))
1690 if (sowriteable(so))
1691 revents |= events & (POLLOUT | POLLWRNORM);
1693 if (events & (POLLPRI | POLLRDBAND))
1694 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1695 revents |= events & (POLLPRI | POLLRDBAND);
1697 if (revents == 0) {
1698 if (events &
1699 (POLLIN | POLLINIGNEOF | POLLPRI | POLLRDNORM |
1700 POLLRDBAND)) {
1701 selrecord(td, &so->so_rcv.ssb_sel);
1702 so->so_rcv.ssb_flags |= SSB_SEL;
1705 if (events & (POLLOUT | POLLWRNORM)) {
1706 selrecord(td, &so->so_snd.ssb_sel);
1707 so->so_snd.ssb_flags |= SSB_SEL;
1711 crit_exit();
1712 return (revents);
1716 sokqfilter(struct file *fp, struct knote *kn)
1718 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1719 struct signalsockbuf *ssb;
1721 switch (kn->kn_filter) {
1722 case EVFILT_READ:
1723 if (so->so_options & SO_ACCEPTCONN)
1724 kn->kn_fop = &solisten_filtops;
1725 else
1726 kn->kn_fop = &soread_filtops;
1727 ssb = &so->so_rcv;
1728 break;
1729 case EVFILT_WRITE:
1730 kn->kn_fop = &sowrite_filtops;
1731 ssb = &so->so_snd;
1732 break;
1733 default:
1734 return (1);
1737 crit_enter();
1738 SLIST_INSERT_HEAD(&ssb->ssb_sel.si_note, kn, kn_selnext);
1739 ssb->ssb_flags |= SSB_KNOTE;
1740 crit_exit();
1741 return (0);
1744 static void
1745 filt_sordetach(struct knote *kn)
1747 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1749 crit_enter();
1750 SLIST_REMOVE(&so->so_rcv.ssb_sel.si_note, kn, knote, kn_selnext);
1751 if (SLIST_EMPTY(&so->so_rcv.ssb_sel.si_note))
1752 so->so_rcv.ssb_flags &= ~SSB_KNOTE;
1753 crit_exit();
1756 /*ARGSUSED*/
1757 static int
1758 filt_soread(struct knote *kn, long hint)
1760 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1762 kn->kn_data = so->so_rcv.ssb_cc;
1763 if (so->so_state & SS_CANTRCVMORE) {
1764 kn->kn_flags |= EV_EOF;
1765 kn->kn_fflags = so->so_error;
1766 return (1);
1768 if (so->so_error) /* temporary udp error */
1769 return (1);
1770 if (kn->kn_sfflags & NOTE_LOWAT)
1771 return (kn->kn_data >= kn->kn_sdata);
1772 return (kn->kn_data >= so->so_rcv.ssb_lowat);
1775 static void
1776 filt_sowdetach(struct knote *kn)
1778 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1780 crit_enter();
1781 SLIST_REMOVE(&so->so_snd.ssb_sel.si_note, kn, knote, kn_selnext);
1782 if (SLIST_EMPTY(&so->so_snd.ssb_sel.si_note))
1783 so->so_snd.ssb_flags &= ~SSB_KNOTE;
1784 crit_exit();
1787 /*ARGSUSED*/
1788 static int
1789 filt_sowrite(struct knote *kn, long hint)
1791 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1793 kn->kn_data = ssb_space(&so->so_snd);
1794 if (so->so_state & SS_CANTSENDMORE) {
1795 kn->kn_flags |= EV_EOF;
1796 kn->kn_fflags = so->so_error;
1797 return (1);
1799 if (so->so_error) /* temporary udp error */
1800 return (1);
1801 if (((so->so_state & SS_ISCONNECTED) == 0) &&
1802 (so->so_proto->pr_flags & PR_CONNREQUIRED))
1803 return (0);
1804 if (kn->kn_sfflags & NOTE_LOWAT)
1805 return (kn->kn_data >= kn->kn_sdata);
1806 return (kn->kn_data >= so->so_snd.ssb_lowat);
1809 /*ARGSUSED*/
1810 static int
1811 filt_solisten(struct knote *kn, long hint)
1813 struct socket *so = (struct socket *)kn->kn_fp->f_data;
1815 kn->kn_data = so->so_qlen;
1816 return (! TAILQ_EMPTY(&so->so_comp));