2 * Copyright (c) 1982, 1986, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * sendfile(2) and related extensions:
6 * Copyright (c) 1998, David Greenman. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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 University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
33 * $FreeBSD: src/sys/kern/uipc_syscalls.c,v 1.65.2.17 2003/04/04 17:11:16 tegge Exp $
36 #include "opt_ktrace.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/sysproto.h>
43 #include <sys/malloc.h>
44 #include <sys/filedesc.h>
45 #include <sys/event.h>
47 #include <sys/fcntl.h>
49 #include <sys/filio.h>
50 #include <sys/kern_syscall.h>
52 #include <sys/protosw.h>
53 #include <sys/sfbuf.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/socketops.h>
58 #include <sys/vnode.h>
60 #include <sys/mount.h>
62 #include <sys/ktrace.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_page.h>
67 #include <vm/vm_pageout.h>
68 #include <vm/vm_kern.h>
69 #include <vm/vm_extern.h>
70 #include <sys/file2.h>
71 #include <sys/signalvar.h>
72 #include <sys/serialize.h>
74 #include <sys/thread2.h>
75 #include <sys/msgport2.h>
76 #include <sys/socketvar2.h>
77 #include <net/netmsg2.h>
80 #include <netinet/sctp_peeloff.h>
83 extern int use_soaccept_pred_fast;
84 extern int use_sendfile_async;
85 extern int use_soconnect_async;
88 * System call interface to the socket abstraction.
91 extern struct fileops socketops;
94 * socket_args(int domain, int type, int protocol)
97 kern_socket(int domain, int type, int protocol, int *res)
99 struct thread *td = curthread;
100 struct filedesc *fdp = td->td_proc->p_fd;
105 KKASSERT(td->td_lwp);
107 error = falloc(td->td_lwp, &fp, &fd);
110 error = socreate(domain, &so, type, protocol, td);
112 fsetfd(fdp, NULL, fd);
114 fp->f_type = DTYPE_SOCKET;
115 fp->f_flag = FREAD | FWRITE;
116 fp->f_ops = &socketops;
129 sys_socket(struct socket_args *uap)
133 error = kern_socket(uap->domain, uap->type, uap->protocol,
134 &uap->sysmsg_iresult);
140 kern_bind(int s, struct sockaddr *sa)
142 struct thread *td = curthread;
143 struct proc *p = td->td_proc;
148 error = holdsock(p->p_fd, s, &fp);
151 error = sobind((struct socket *)fp->f_data, sa, td);
157 * bind_args(int s, caddr_t name, int namelen)
162 sys_bind(struct bind_args *uap)
167 error = getsockaddr(&sa, uap->name, uap->namelen);
170 error = kern_bind(uap->s, sa);
177 kern_listen(int s, int backlog)
179 struct thread *td = curthread;
180 struct proc *p = td->td_proc;
185 error = holdsock(p->p_fd, s, &fp);
188 error = solisten((struct socket *)fp->f_data, backlog, td);
194 * listen_args(int s, int backlog)
199 sys_listen(struct listen_args *uap)
203 error = kern_listen(uap->s, uap->backlog);
208 * Returns the accepted socket as well.
210 * NOTE! The sockets sitting on so_comp/so_incomp might have 0 refs, the
211 * pool token is absolutely required to avoid a sofree() race,
212 * as well as to avoid tailq handling races.
215 soaccept_predicate(struct netmsg_so_notify *msg)
217 struct socket *head = msg->base.nm_so;
220 if (head->so_error != 0) {
221 msg->base.lmsg.ms_error = head->so_error;
224 lwkt_getpooltoken(head);
225 if (!TAILQ_EMPTY(&head->so_comp)) {
226 /* Abuse nm_so field as copy in/copy out parameter. XXX JH */
227 so = TAILQ_FIRST(&head->so_comp);
228 TAILQ_REMOVE(&head->so_comp, so, so_list);
230 soclrstate(so, SS_COMP);
234 lwkt_relpooltoken(head);
236 msg->base.lmsg.ms_error = 0;
237 msg->base.nm_so = so;
240 lwkt_relpooltoken(head);
241 if (head->so_state & SS_CANTRCVMORE) {
242 msg->base.lmsg.ms_error = ECONNABORTED;
245 if (msg->nm_fflags & FNONBLOCK) {
246 msg->base.lmsg.ms_error = EWOULDBLOCK;
254 * The second argument to kern_accept() is a handle to a struct sockaddr.
255 * This allows kern_accept() to return a pointer to an allocated struct
256 * sockaddr which must be freed later with FREE(). The caller must
257 * initialize *name to NULL.
260 kern_accept(int s, int fflags, struct sockaddr **name, int *namelen, int *res)
262 struct thread *td = curthread;
263 struct filedesc *fdp = td->td_proc->p_fd;
264 struct file *lfp = NULL;
265 struct file *nfp = NULL;
267 struct socket *head, *so;
268 struct netmsg_so_notify msg;
270 u_int fflag; /* type must match fp->f_flag */
274 if (name && namelen && *namelen < 0)
277 error = holdsock(td->td_proc->p_fd, s, &lfp);
281 error = falloc(td->td_lwp, &nfp, &fd);
282 if (error) { /* Probably ran out of file descriptors. */
286 head = (struct socket *)lfp->f_data;
287 if ((head->so_options & SO_ACCEPTCONN) == 0) {
292 if (fflags & O_FBLOCKING)
293 fflags |= lfp->f_flag & ~FNONBLOCK;
294 else if (fflags & O_FNONBLOCKING)
295 fflags |= lfp->f_flag | FNONBLOCK;
297 fflags = lfp->f_flag;
299 if (use_soaccept_pred_fast) {
302 /* Initialize necessary parts for soaccept_predicate() */
303 netmsg_init(&msg.base, head, &netisr_apanic_rport, 0, NULL);
304 msg.nm_fflags = fflags;
306 lwkt_getpooltoken(head);
307 pred = soaccept_predicate(&msg);
308 lwkt_relpooltoken(head);
311 error = msg.base.lmsg.ms_error;
319 /* optimize for uniprocessor case later XXX JH */
320 netmsg_init_abortable(&msg.base, head, &curthread->td_msgport,
321 0, netmsg_so_notify, netmsg_so_notify_doabort);
322 msg.nm_predicate = soaccept_predicate;
323 msg.nm_fflags = fflags;
324 msg.nm_etype = NM_REVENT;
325 error = lwkt_domsg(head->so_port, &msg.base.lmsg, PCATCH);
331 * At this point we have the connection that's ready to be accepted.
333 * NOTE! soaccept_predicate() ref'd so for us, and soaccept() expects
334 * to eat the ref and turn it into a descriptor.
340 /* connection has been removed from the listen queue */
341 KNOTE(&head->so_rcv.ssb_kq.ki_note, 0);
343 if (head->so_sigio != NULL)
344 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
346 nfp->f_type = DTYPE_SOCKET;
348 nfp->f_ops = &socketops;
350 /* Sync socket nonblocking/async state with file flags */
351 tmp = fflag & FNONBLOCK;
352 fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, td->td_ucred, NULL);
353 tmp = fflag & FASYNC;
354 fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, td->td_ucred, NULL);
357 if (so->so_faddr != NULL) {
361 soaccept_generic(so);
364 error = soaccept(so, &sa);
368 * Set the returned name and namelen as applicable. Set the returned
369 * namelen to 0 for older code which might ignore the return value
373 if (sa && name && namelen) {
374 if (*namelen > sa->sa_len)
375 *namelen = sa->sa_len;
385 * If an error occured clear the reserved descriptor, else associate
388 * Note that *res is normally ignored if an error is returned but
389 * a syscall message will still have access to the result code.
392 fsetfd(fdp, NULL, fd);
395 fsetfd(fdp, nfp, fd);
403 * accept(int s, caddr_t name, int *anamelen)
408 sys_accept(struct accept_args *uap)
410 struct sockaddr *sa = NULL;
415 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
419 error = kern_accept(uap->s, 0, &sa, &sa_len,
420 &uap->sysmsg_iresult);
423 error = copyout(sa, uap->name, sa_len);
425 error = copyout(&sa_len, uap->anamelen,
426 sizeof(*uap->anamelen));
431 error = kern_accept(uap->s, 0, NULL, 0,
432 &uap->sysmsg_iresult);
438 * extaccept(int s, int fflags, caddr_t name, int *anamelen)
443 sys_extaccept(struct extaccept_args *uap)
445 struct sockaddr *sa = NULL;
448 int fflags = uap->flags & O_FMASK;
451 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
455 error = kern_accept(uap->s, fflags, &sa, &sa_len,
456 &uap->sysmsg_iresult);
459 error = copyout(sa, uap->name, sa_len);
461 error = copyout(&sa_len, uap->anamelen,
462 sizeof(*uap->anamelen));
467 error = kern_accept(uap->s, fflags, NULL, 0,
468 &uap->sysmsg_iresult);
475 * Returns TRUE if predicate satisfied.
478 soconnected_predicate(struct netmsg_so_notify *msg)
480 struct socket *so = msg->base.nm_so;
482 /* check predicate */
483 if (!(so->so_state & SS_ISCONNECTING) || so->so_error != 0) {
484 msg->base.lmsg.ms_error = so->so_error;
492 kern_connect(int s, int fflags, struct sockaddr *sa)
494 struct thread *td = curthread;
495 struct proc *p = td->td_proc;
498 int error, interrupted = 0;
500 error = holdsock(p->p_fd, s, &fp);
503 so = (struct socket *)fp->f_data;
505 if (fflags & O_FBLOCKING)
506 /* fflags &= ~FNONBLOCK; */;
507 else if (fflags & O_FNONBLOCKING)
512 if (so->so_state & SS_ISCONNECTING) {
516 error = soconnect(so, sa, td, use_soconnect_async ? FALSE : TRUE);
519 if ((fflags & FNONBLOCK) && (so->so_state & SS_ISCONNECTING)) {
523 if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
524 struct netmsg_so_notify msg;
526 netmsg_init_abortable(&msg.base, so,
527 &curthread->td_msgport,
530 netmsg_so_notify_doabort);
531 msg.nm_predicate = soconnected_predicate;
532 msg.nm_etype = NM_REVENT;
533 error = lwkt_domsg(so->so_port, &msg.base.lmsg, PCATCH);
534 if (error == EINTR || error == ERESTART)
538 error = so->so_error;
543 soclrstate(so, SS_ISCONNECTING);
544 if (error == ERESTART)
552 * connect_args(int s, caddr_t name, int namelen)
557 sys_connect(struct connect_args *uap)
562 error = getsockaddr(&sa, uap->name, uap->namelen);
565 error = kern_connect(uap->s, 0, sa);
572 * connect_args(int s, int fflags, caddr_t name, int namelen)
577 sys_extconnect(struct extconnect_args *uap)
581 int fflags = uap->flags & O_FMASK;
583 error = getsockaddr(&sa, uap->name, uap->namelen);
586 error = kern_connect(uap->s, fflags, sa);
593 kern_socketpair(int domain, int type, int protocol, int *sv)
595 struct thread *td = curthread;
596 struct filedesc *fdp;
597 struct file *fp1, *fp2;
598 struct socket *so1, *so2;
601 fdp = td->td_proc->p_fd;
602 error = socreate(domain, &so1, type, protocol, td);
605 error = socreate(domain, &so2, type, protocol, td);
608 error = falloc(td->td_lwp, &fp1, &fd1);
613 error = falloc(td->td_lwp, &fp2, &fd2);
618 error = soconnect2(so1, so2);
621 if (type == SOCK_DGRAM) {
623 * Datagram socket connection is asymmetric.
625 error = soconnect2(so2, so1);
629 fp1->f_type = fp2->f_type = DTYPE_SOCKET;
630 fp1->f_flag = fp2->f_flag = FREAD|FWRITE;
631 fp1->f_ops = fp2->f_ops = &socketops;
632 fsetfd(fdp, fp1, fd1);
633 fsetfd(fdp, fp2, fd2);
638 fsetfd(fdp, NULL, fd2);
641 fsetfd(fdp, NULL, fd1);
644 (void)soclose(so2, 0);
646 (void)soclose(so1, 0);
651 * socketpair(int domain, int type, int protocol, int *rsv)
654 sys_socketpair(struct socketpair_args *uap)
658 error = kern_socketpair(uap->domain, uap->type, uap->protocol, sockv);
661 error = copyout(sockv, uap->rsv, sizeof(sockv));
664 kern_close(sockv[0]);
665 kern_close(sockv[1]);
673 kern_sendmsg(int s, struct sockaddr *sa, struct uio *auio,
674 struct mbuf *control, int flags, size_t *res)
676 struct thread *td = curthread;
677 struct lwp *lp = td->td_lwp;
678 struct proc *p = td->td_proc;
684 struct iovec *ktriov = NULL;
688 error = holdsock(p->p_fd, s, &fp);
692 if (KTRPOINT(td, KTR_GENIO)) {
693 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
695 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
696 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
700 len = auio->uio_resid;
701 so = (struct socket *)fp->f_data;
702 if ((flags & (MSG_FNONBLOCKING|MSG_FBLOCKING)) == 0) {
703 if (fp->f_flag & FNONBLOCK)
704 flags |= MSG_FNONBLOCKING;
706 error = so_pru_sosend(so, sa, auio, NULL, control, flags, td);
708 if (auio->uio_resid != len && (error == ERESTART ||
709 error == EINTR || error == EWOULDBLOCK))
711 if (error == EPIPE && !(flags & MSG_NOSIGNAL) &&
712 !(so->so_options & SO_NOSIGPIPE))
713 lwpsignal(p, lp, SIGPIPE);
716 if (ktriov != NULL) {
718 ktruio.uio_iov = ktriov;
719 ktruio.uio_resid = len - auio->uio_resid;
720 ktrgenio(lp, s, UIO_WRITE, &ktruio, error);
722 kfree(ktriov, M_TEMP);
726 *res = len - auio->uio_resid;
732 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
737 sys_sendto(struct sendto_args *uap)
739 struct thread *td = curthread;
742 struct sockaddr *sa = NULL;
746 error = getsockaddr(&sa, uap->to, uap->tolen);
750 aiov.iov_base = uap->buf;
751 aiov.iov_len = uap->len;
752 auio.uio_iov = &aiov;
755 auio.uio_resid = uap->len;
756 auio.uio_segflg = UIO_USERSPACE;
757 auio.uio_rw = UIO_WRITE;
760 error = kern_sendmsg(uap->s, sa, &auio, NULL, uap->flags,
761 &uap->sysmsg_szresult);
769 * sendmsg_args(int s, caddr_t msg, int flags)
774 sys_sendmsg(struct sendmsg_args *uap)
776 struct thread *td = curthread;
779 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
780 struct sockaddr *sa = NULL;
781 struct mbuf *control = NULL;
784 error = copyin(uap->msg, (caddr_t)&msg, sizeof(msg));
789 * Conditionally copyin msg.msg_name.
792 error = getsockaddr(&sa, msg.msg_name, msg.msg_namelen);
800 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
805 auio.uio_iovcnt = msg.msg_iovlen;
807 auio.uio_segflg = UIO_USERSPACE;
808 auio.uio_rw = UIO_WRITE;
812 * Conditionally copyin msg.msg_control.
814 if (msg.msg_control) {
815 if (msg.msg_controllen < sizeof(struct cmsghdr) ||
816 msg.msg_controllen > MLEN) {
820 control = m_get(MB_WAIT, MT_CONTROL);
821 if (control == NULL) {
825 control->m_len = msg.msg_controllen;
826 error = copyin(msg.msg_control, mtod(control, caddr_t),
834 error = kern_sendmsg(uap->s, sa, &auio, control, uap->flags,
835 &uap->sysmsg_szresult);
838 iovec_free(&iov, aiov);
846 * kern_recvmsg() takes a handle to sa and control. If the handle is non-
847 * null, it returns a dynamically allocated struct sockaddr and an mbuf.
848 * Don't forget to FREE() and m_free() these if they are returned.
851 kern_recvmsg(int s, struct sockaddr **sa, struct uio *auio,
852 struct mbuf **control, int *flags, size_t *res)
854 struct thread *td = curthread;
855 struct proc *p = td->td_proc;
862 struct iovec *ktriov = NULL;
866 error = holdsock(p->p_fd, s, &fp);
870 if (KTRPOINT(td, KTR_GENIO)) {
871 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
873 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
874 bcopy(auio->uio_iov, ktriov, iovlen);
878 len = auio->uio_resid;
879 so = (struct socket *)fp->f_data;
881 if (flags == NULL || (*flags & (MSG_FNONBLOCKING|MSG_FBLOCKING)) == 0) {
882 if (fp->f_flag & FNONBLOCK) {
884 *flags |= MSG_FNONBLOCKING;
886 lflags = MSG_FNONBLOCKING;
892 error = so_pru_soreceive(so, sa, auio, NULL, control, flags);
894 if (auio->uio_resid != len && (error == ERESTART ||
895 error == EINTR || error == EWOULDBLOCK))
899 if (ktriov != NULL) {
901 ktruio.uio_iov = ktriov;
902 ktruio.uio_resid = len - auio->uio_resid;
903 ktrgenio(td->td_lwp, s, UIO_READ, &ktruio, error);
905 kfree(ktriov, M_TEMP);
909 *res = len - auio->uio_resid;
915 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
916 * caddr_t from, int *fromlenaddr)
921 sys_recvfrom(struct recvfrom_args *uap)
923 struct thread *td = curthread;
926 struct sockaddr *sa = NULL;
929 if (uap->from && uap->fromlenaddr) {
930 error = copyin(uap->fromlenaddr, &fromlen, sizeof(fromlen));
938 aiov.iov_base = uap->buf;
939 aiov.iov_len = uap->len;
940 auio.uio_iov = &aiov;
943 auio.uio_resid = uap->len;
944 auio.uio_segflg = UIO_USERSPACE;
945 auio.uio_rw = UIO_READ;
948 error = kern_recvmsg(uap->s, uap->from ? &sa : NULL, &auio, NULL,
949 &uap->flags, &uap->sysmsg_szresult);
951 if (error == 0 && uap->from) {
952 /* note: sa may still be NULL */
954 fromlen = MIN(fromlen, sa->sa_len);
955 error = copyout(sa, uap->from, fromlen);
960 error = copyout(&fromlen, uap->fromlenaddr,
971 * recvmsg_args(int s, struct msghdr *msg, int flags)
976 sys_recvmsg(struct recvmsg_args *uap)
978 struct thread *td = curthread;
981 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
982 struct mbuf *m, *control = NULL;
983 struct sockaddr *sa = NULL;
985 socklen_t *ufromlenp, *ucontrollenp;
986 int error, fromlen, controllen, len, flags, *uflagsp;
989 * This copyin handles everything except the iovec.
991 error = copyin(uap->msg, &msg, sizeof(msg));
995 if (msg.msg_name && msg.msg_namelen < 0)
997 if (msg.msg_control && msg.msg_controllen < 0)
1000 ufromlenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
1002 ucontrollenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
1004 uflagsp = (int *)((caddr_t)uap->msg + offsetof(struct msghdr,
1010 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
1015 auio.uio_iovcnt = msg.msg_iovlen;
1016 auio.uio_offset = 0;
1017 auio.uio_segflg = UIO_USERSPACE;
1018 auio.uio_rw = UIO_READ;
1023 error = kern_recvmsg(uap->s,
1024 (msg.msg_name ? &sa : NULL), &auio,
1025 (msg.msg_control ? &control : NULL), &flags,
1026 &uap->sysmsg_szresult);
1029 * Conditionally copyout the name and populate the namelen field.
1031 if (error == 0 && msg.msg_name) {
1032 /* note: sa may still be NULL */
1034 fromlen = MIN(msg.msg_namelen, sa->sa_len);
1035 error = copyout(sa, msg.msg_name, fromlen);
1040 error = copyout(&fromlen, ufromlenp,
1041 sizeof(*ufromlenp));
1045 * Copyout msg.msg_control and msg.msg_controllen.
1047 if (error == 0 && msg.msg_control) {
1048 len = msg.msg_controllen;
1050 ctlbuf = (caddr_t)msg.msg_control;
1052 while(m && len > 0) {
1053 unsigned int tocopy;
1055 if (len >= m->m_len) {
1058 msg.msg_flags |= MSG_CTRUNC;
1062 error = copyout(mtod(m, caddr_t), ctlbuf, tocopy);
1070 controllen = ctlbuf - (caddr_t)msg.msg_control;
1071 error = copyout(&controllen, ucontrollenp,
1072 sizeof(*ucontrollenp));
1076 error = copyout(&flags, uflagsp, sizeof(*uflagsp));
1080 kfree(sa, M_SONAME);
1081 iovec_free(&iov, aiov);
1088 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
1089 * in kernel pointer instead of a userland pointer. This allows us
1090 * to manipulate socket options in the emulation code.
1093 kern_setsockopt(int s, struct sockopt *sopt)
1095 struct thread *td = curthread;
1096 struct proc *p = td->td_proc;
1100 if (sopt->sopt_val == NULL && sopt->sopt_valsize != 0)
1102 if (sopt->sopt_val != NULL && sopt->sopt_valsize == 0)
1104 if (sopt->sopt_valsize > SOMAXOPT_SIZE) /* unsigned */
1107 error = holdsock(p->p_fd, s, &fp);
1111 error = sosetopt((struct socket *)fp->f_data, sopt);
1117 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
1122 sys_setsockopt(struct setsockopt_args *uap)
1124 struct thread *td = curthread;
1125 struct sockopt sopt;
1128 sopt.sopt_level = uap->level;
1129 sopt.sopt_name = uap->name;
1130 sopt.sopt_valsize = uap->valsize;
1132 sopt.sopt_val = NULL;
1134 if (sopt.sopt_valsize > SOMAXOPT_SIZE) /* unsigned */
1137 sopt.sopt_val = kmalloc(sopt.sopt_valsize, M_TEMP, M_WAITOK);
1138 error = copyin(uap->val, sopt.sopt_val, sopt.sopt_valsize);
1143 error = kern_setsockopt(uap->s, &sopt);
1146 kfree(sopt.sopt_val, M_TEMP);
1151 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
1152 * in kernel pointer instead of a userland pointer. This allows us
1153 * to manipulate socket options in the emulation code.
1156 kern_getsockopt(int s, struct sockopt *sopt)
1158 struct thread *td = curthread;
1159 struct proc *p = td->td_proc;
1163 if (sopt->sopt_val == NULL && sopt->sopt_valsize != 0)
1165 if (sopt->sopt_val != NULL && sopt->sopt_valsize == 0)
1167 if (sopt->sopt_valsize > SOMAXOPT_SIZE) /* unsigned */
1170 error = holdsock(p->p_fd, s, &fp);
1174 error = sogetopt((struct socket *)fp->f_data, sopt);
1180 * getsockopt_args(int s, int level, int name, caddr_t val, int *avalsize)
1185 sys_getsockopt(struct getsockopt_args *uap)
1187 struct thread *td = curthread;
1188 struct sockopt sopt;
1192 error = copyin(uap->avalsize, &valsize, sizeof(valsize));
1199 sopt.sopt_level = uap->level;
1200 sopt.sopt_name = uap->name;
1201 sopt.sopt_valsize = valsize;
1203 sopt.sopt_val = NULL;
1205 if (sopt.sopt_valsize > SOMAXOPT_SIZE) /* unsigned */
1208 sopt.sopt_val = kmalloc(sopt.sopt_valsize, M_TEMP, M_WAITOK);
1209 error = copyin(uap->val, sopt.sopt_val, sopt.sopt_valsize);
1214 error = kern_getsockopt(uap->s, &sopt);
1217 valsize = sopt.sopt_valsize;
1218 error = copyout(&valsize, uap->avalsize, sizeof(valsize));
1222 error = copyout(sopt.sopt_val, uap->val, sopt.sopt_valsize);
1225 kfree(sopt.sopt_val, M_TEMP);
1230 * The second argument to kern_getsockname() is a handle to a struct sockaddr.
1231 * This allows kern_getsockname() to return a pointer to an allocated struct
1232 * sockaddr which must be freed later with FREE(). The caller must
1233 * initialize *name to NULL.
1236 kern_getsockname(int s, struct sockaddr **name, int *namelen)
1238 struct thread *td = curthread;
1239 struct proc *p = td->td_proc;
1242 struct sockaddr *sa = NULL;
1245 error = holdsock(p->p_fd, s, &fp);
1252 so = (struct socket *)fp->f_data;
1253 error = so_pru_sockaddr(so, &sa);
1258 *namelen = MIN(*namelen, sa->sa_len);
1268 * getsockname_args(int fdes, caddr_t asa, int *alen)
1275 sys_getsockname(struct getsockname_args *uap)
1277 struct sockaddr *sa = NULL;
1280 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1284 error = kern_getsockname(uap->fdes, &sa, &sa_len);
1287 error = copyout(sa, uap->asa, sa_len);
1289 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1291 kfree(sa, M_SONAME);
1296 * The second argument to kern_getpeername() is a handle to a struct sockaddr.
1297 * This allows kern_getpeername() to return a pointer to an allocated struct
1298 * sockaddr which must be freed later with FREE(). The caller must
1299 * initialize *name to NULL.
1302 kern_getpeername(int s, struct sockaddr **name, int *namelen)
1304 struct thread *td = curthread;
1305 struct proc *p = td->td_proc;
1308 struct sockaddr *sa = NULL;
1311 error = holdsock(p->p_fd, s, &fp);
1318 so = (struct socket *)fp->f_data;
1319 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1323 error = so_pru_peeraddr(so, &sa);
1328 *namelen = MIN(*namelen, sa->sa_len);
1338 * getpeername_args(int fdes, caddr_t asa, int *alen)
1340 * Get name of peer for connected socket.
1345 sys_getpeername(struct getpeername_args *uap)
1347 struct sockaddr *sa = NULL;
1350 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1354 error = kern_getpeername(uap->fdes, &sa, &sa_len);
1357 error = copyout(sa, uap->asa, sa_len);
1359 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1361 kfree(sa, M_SONAME);
1366 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1368 struct sockaddr *sa;
1372 if (len > SOCK_MAXADDRLEN)
1373 return ENAMETOOLONG;
1374 if (len < offsetof(struct sockaddr, sa_data[0]))
1376 sa = kmalloc(len, M_SONAME, M_WAITOK);
1377 error = copyin(uaddr, sa, len);
1379 kfree(sa, M_SONAME);
1381 #if BYTE_ORDER != BIG_ENDIAN
1383 * The bind(), connect(), and sendto() syscalls were not
1384 * versioned for COMPAT_43. Thus, this check must stay.
1386 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1387 sa->sa_family = sa->sa_len;
1396 * Detach a mapped page and release resources back to the system.
1397 * We must release our wiring and if the object is ripped out
1398 * from under the vm_page we become responsible for freeing the
1404 sf_buf_mfree(void *arg)
1406 struct sf_buf *sf = arg;
1409 m = sf_buf_page(sf);
1410 if (sf_buf_free(sf)) {
1411 /* sf invalid now */
1412 vm_page_busy_wait(m, FALSE, "sockpgf");
1413 vm_page_unwire(m, 0);
1414 if (m->object == NULL &&
1415 m->wire_count == 0 &&
1416 (m->flags & PG_NEED_COMMIT) == 0) {
1426 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1427 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1429 * Send a file specified by 'fd' and starting at 'offset' to a socket
1430 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1431 * nbytes == 0. Optionally add a header and/or trailer to the socket
1432 * output. If specified, write the total number of bytes sent into *sbytes.
1434 * In FreeBSD kern/uipc_syscalls.c,v 1.103, a bug was fixed that caused
1435 * the headers to count against the remaining bytes to be sent from
1436 * the file descriptor. We may wish to implement a compatibility syscall
1442 sys_sendfile(struct sendfile_args *uap)
1444 struct thread *td = curthread;
1445 struct proc *p = td->td_proc;
1447 struct vnode *vp = NULL;
1448 struct sf_hdtr hdtr;
1449 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
1451 struct mbuf *mheader = NULL;
1454 off_t hdtr_size = 0;
1461 * Do argument checking. Must be a regular file in, stream
1462 * type and connected socket out, positive offset.
1464 fp = holdfp(p->p_fd, uap->fd, FREAD);
1468 if (fp->f_type != DTYPE_VNODE) {
1472 vp = (struct vnode *)fp->f_data;
1477 * If specified, get the pointer to the sf_hdtr struct for
1478 * any headers/trailers.
1481 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1488 error = iovec_copyin(hdtr.headers, &iov, aiov,
1489 hdtr.hdr_cnt, &hbytes);
1493 auio.uio_iovcnt = hdtr.hdr_cnt;
1494 auio.uio_offset = 0;
1495 auio.uio_segflg = UIO_USERSPACE;
1496 auio.uio_rw = UIO_WRITE;
1498 auio.uio_resid = hbytes;
1500 mheader = m_uiomove(&auio);
1502 iovec_free(&iov, aiov);
1503 if (mheader == NULL)
1508 error = kern_sendfile(vp, uap->s, uap->offset, uap->nbytes, mheader,
1509 &sbytes, uap->flags);
1514 * Send trailers. Wimp out and use writev(2).
1516 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1517 error = iovec_copyin(hdtr.trailers, &iov, aiov,
1518 hdtr.trl_cnt, &auio.uio_resid);
1522 auio.uio_iovcnt = hdtr.trl_cnt;
1523 auio.uio_offset = 0;
1524 auio.uio_segflg = UIO_USERSPACE;
1525 auio.uio_rw = UIO_WRITE;
1528 error = kern_sendmsg(uap->s, NULL, &auio, NULL, 0, &tbytes);
1530 iovec_free(&iov, aiov);
1533 hdtr_size += tbytes; /* trailer bytes successfully sent */
1539 if (uap->sbytes != NULL) {
1540 sbytes += hdtr_size;
1541 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1547 kern_sendfile(struct vnode *vp, int sfd, off_t offset, size_t nbytes,
1548 struct mbuf *mheader, off_t *sbytes, int flags)
1550 struct thread *td = curthread;
1551 struct proc *p = td->td_proc;
1552 struct vm_object *obj;
1555 struct mbuf *m, *mp;
1562 if (vp->v_type != VREG) {
1566 if ((obj = vp->v_object) == NULL) {
1570 error = holdsock(p->p_fd, sfd, &fp);
1573 so = (struct socket *)fp->f_data;
1574 if (so->so_type != SOCK_STREAM) {
1578 if ((so->so_state & SS_ISCONNECTED) == 0) {
1589 * Protect against multiple writers to the socket.
1591 ssb_lock(&so->so_snd, M_WAITOK);
1594 * Loop through the pages in the file, starting with the requested
1595 * offset. Get a file page (do I/O if necessary), map the file page
1596 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1599 for (off = offset; ; off += xfsize, *sbytes += xfsize + hbytes) {
1604 pindex = OFF_TO_IDX(off);
1607 * Calculate the amount to transfer. Not to exceed a page,
1608 * the EOF, or the passed in nbytes.
1610 xfsize = vp->v_filesize - off;
1611 if (xfsize > PAGE_SIZE)
1613 pgoff = (vm_offset_t)(off & PAGE_MASK);
1614 if (PAGE_SIZE - pgoff < xfsize)
1615 xfsize = PAGE_SIZE - pgoff;
1616 if (nbytes && xfsize > (nbytes - *sbytes))
1617 xfsize = nbytes - *sbytes;
1621 * Optimize the non-blocking case by looking at the socket space
1622 * before going to the extra work of constituting the sf_buf.
1624 if ((fp->f_flag & FNONBLOCK) &&
1625 ssb_space_prealloc(&so->so_snd) <= 0) {
1626 if (so->so_state & SS_CANTSENDMORE)
1630 ssb_unlock(&so->so_snd);
1634 * Attempt to look up the page.
1636 * Allocate if not found, wait and loop if busy, then
1637 * wire the page. critical section protection is
1638 * required to maintain the object association (an
1639 * interrupt can free the page) through to the
1640 * vm_page_wire() call.
1642 vm_object_hold(obj);
1643 pg = vm_page_lookup_busy_try(obj, pindex, TRUE, &error);
1645 vm_page_sleep_busy(pg, TRUE, "sfpbsy");
1646 vm_object_drop(obj);
1650 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL |
1654 vm_object_drop(obj);
1659 vm_object_drop(obj);
1662 * If page is not valid for what we need, initiate I/O
1665 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1671 * Ensure that our page is still around when the I/O
1674 * Ensure that our page is not modified while part of
1675 * a mbuf as this could mess up tcp checksums, DMA,
1676 * etc (XXX NEEDS WORK). The softbusy is supposed to
1677 * help here but it actually doesn't.
1679 * XXX THIS HAS MULTIPLE PROBLEMS. The underlying
1680 * VM pages are not protected by the soft-busy
1681 * unless we vm_page_protect... READ them, and
1682 * they STILL aren't protected against
1683 * modification via the buffer cache (VOP_WRITE).
1685 * Fixing the second issue is particularly
1688 * XXX We also can't soft-busy anyway because it can
1689 * deadlock against the syncer doing a vfs_msync(),
1690 * vfs_msync->vmntvnodesca->vfs_msync_scan2->
1691 * vm_object_page_clean->(scan)-> ... page
1694 /*vm_page_io_start(pg);*/
1698 * Get the page from backing store.
1700 bsize = vp->v_mount->mnt_stat.f_iosize;
1701 auio.uio_iov = &aiov;
1702 auio.uio_iovcnt = 1;
1704 aiov.iov_len = MAXBSIZE;
1705 auio.uio_resid = MAXBSIZE;
1706 auio.uio_offset = trunc_page(off);
1707 auio.uio_segflg = UIO_NOCOPY;
1708 auio.uio_rw = UIO_READ;
1710 vn_lock(vp, LK_SHARED | LK_RETRY);
1711 error = VOP_READ(vp, &auio,
1712 IO_VMIO | ((MAXBSIZE / bsize) << 16),
1715 vm_page_flag_clear(pg, PG_ZERO);
1716 vm_page_busy_wait(pg, FALSE, "sockpg");
1717 /*vm_page_io_finish(pg);*/
1719 vm_page_unwire(pg, 0);
1721 vm_page_try_to_free(pg);
1722 ssb_unlock(&so->so_snd);
1729 * Get a sendfile buf. We usually wait as long as necessary,
1730 * but this wait can be interrupted.
1732 if ((sf = sf_buf_alloc(pg)) == NULL) {
1733 vm_page_unwire(pg, 0);
1735 vm_page_try_to_free(pg);
1736 ssb_unlock(&so->so_snd);
1743 * Get an mbuf header and set it up as having external storage.
1745 MGETHDR(m, MB_WAIT, MT_DATA);
1749 ssb_unlock(&so->so_snd);
1753 m->m_ext.ext_free = sf_buf_mfree;
1754 m->m_ext.ext_ref = sf_buf_ref;
1755 m->m_ext.ext_arg = sf;
1756 m->m_ext.ext_buf = (void *)sf_buf_kva(sf);
1757 m->m_ext.ext_size = PAGE_SIZE;
1758 m->m_data = (char *)sf_buf_kva(sf) + pgoff;
1759 m->m_flags |= M_EXT;
1760 m->m_pkthdr.len = m->m_len = xfsize;
1761 KKASSERT((m->m_flags & (M_EXT_CLUSTER)) == 0);
1763 if (mheader != NULL) {
1764 hbytes = mheader->m_pkthdr.len;
1765 mheader->m_pkthdr.len += m->m_pkthdr.len;
1773 * Add the buffer to the socket buffer chain.
1778 * Make sure that the socket is still able to take more data.
1779 * CANTSENDMORE being true usually means that the connection
1780 * was closed. so_error is true when an error was sensed after
1782 * The state is checked after the page mapping and buffer
1783 * allocation above since those operations may block and make
1784 * any socket checks stale. From this point forward, nothing
1785 * blocks before the pru_send (or more accurately, any blocking
1786 * results in a loop back to here to re-check).
1788 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1789 if (so->so_state & SS_CANTSENDMORE) {
1792 error = so->so_error;
1796 ssb_unlock(&so->so_snd);
1801 * Wait for socket space to become available. We do this just
1802 * after checking the connection state above in order to avoid
1803 * a race condition with ssb_wait().
1805 space = ssb_space_prealloc(&so->so_snd);
1806 if (space < m->m_pkthdr.len && space < so->so_snd.ssb_lowat) {
1807 if (fp->f_flag & FNONBLOCK) {
1809 ssb_unlock(&so->so_snd);
1814 error = ssb_wait(&so->so_snd);
1816 * An error from ssb_wait usually indicates that we've
1817 * been interrupted by a signal. If we've sent anything
1818 * then return bytes sent, otherwise return the error.
1822 ssb_unlock(&so->so_snd);
1829 for (mp = m; mp != NULL; mp = mp->m_next)
1830 ssb_preallocstream(&so->so_snd, mp);
1831 if (use_sendfile_async)
1832 error = so_pru_senda(so, 0, m, NULL, NULL, td);
1834 error = so_pru_send(so, 0, m, NULL, NULL, td);
1838 ssb_unlock(&so->so_snd);
1842 if (mheader != NULL) {
1843 *sbytes += mheader->m_pkthdr.len;
1845 for (mp = mheader; mp != NULL; mp = mp->m_next)
1846 ssb_preallocstream(&so->so_snd, mp);
1847 if (use_sendfile_async)
1848 error = so_pru_senda(so, 0, mheader, NULL, NULL, td);
1850 error = so_pru_send(so, 0, mheader, NULL, NULL, td);
1854 ssb_unlock(&so->so_snd);
1859 if (mheader != NULL)
1868 sys_sctp_peeloff(struct sctp_peeloff_args *uap)
1871 struct thread *td = curthread;
1872 struct filedesc *fdp = td->td_proc->p_fd;
1873 struct file *lfp = NULL;
1874 struct file *nfp = NULL;
1876 struct socket *head, *so;
1879 short fflag; /* type must match fp->f_flag */
1881 assoc_id = uap->name;
1882 error = holdsock(td->td_proc->p_fd, uap->sd, &lfp);
1887 head = (struct socket *)lfp->f_data;
1888 error = sctp_can_peel_off(head, assoc_id);
1894 * At this point we know we do have a assoc to pull
1895 * we proceed to get the fd setup. This may block
1899 fflag = lfp->f_flag;
1900 error = falloc(td->td_lwp, &nfp, &fd);
1903 * Probably ran out of file descriptors. Put the
1904 * unaccepted connection back onto the queue and
1905 * do another wakeup so some other process might
1906 * have a chance at it.
1911 uap->sysmsg_iresult = fd;
1913 so = sctp_get_peeloff(head, assoc_id, &error);
1916 * Either someone else peeled it off OR
1917 * we can't get a socket.
1921 soreference(so); /* reference needed */
1922 soclrstate(so, SS_NOFDREF | SS_COMP); /* when clearing NOFDREF */
1924 if (head->so_sigio != NULL)
1925 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
1927 nfp->f_type = DTYPE_SOCKET;
1928 nfp->f_flag = fflag;
1929 nfp->f_ops = &socketops;
1934 * Assign the file pointer to the reserved descriptor, or clear
1935 * the reserved descriptor if an error occured.
1938 fsetfd(fdp, NULL, fd);
1940 fsetfd(fdp, nfp, fd);
1943 * Release explicitly held references before returning.