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"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/sysproto.h>
42 #include <sys/malloc.h>
43 #include <sys/filedesc.h>
44 #include <sys/event.h>
46 #include <sys/fcntl.h>
48 #include <sys/filio.h>
49 #include <sys/kern_syscall.h>
51 #include <sys/protosw.h>
52 #include <sys/sfbuf.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/socketops.h>
57 #include <sys/vnode.h>
59 #include <sys/mount.h>
61 #include <sys/ktrace.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_pageout.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_extern.h>
69 #include <sys/file2.h>
70 #include <sys/signalvar.h>
71 #include <sys/serialize.h>
73 #include <sys/thread2.h>
74 #include <sys/msgport2.h>
75 #include <sys/socketvar2.h>
76 #include <net/netmsg2.h>
77 #include <vm/vm_page2.h>
79 extern int use_soaccept_pred_fast;
80 extern int use_sendfile_async;
81 extern int use_soconnect_async;
84 * System call interface to the socket abstraction.
87 extern struct fileops socketops;
90 * socket_args(int domain, int type, int protocol)
93 kern_socket(int domain, int type, int protocol, int *res)
95 struct thread *td = curthread;
96 struct filedesc *fdp = td->td_proc->p_fd;
101 KKASSERT(td->td_lwp);
103 error = falloc(td->td_lwp, &fp, &fd);
106 error = socreate(domain, &so, type, protocol, td);
108 fsetfd(fdp, NULL, fd);
110 fp->f_type = DTYPE_SOCKET;
111 fp->f_flag = FREAD | FWRITE;
112 fp->f_ops = &socketops;
125 sys_socket(struct socket_args *uap)
129 error = kern_socket(uap->domain, uap->type, uap->protocol,
130 &uap->sysmsg_iresult);
136 kern_bind(int s, struct sockaddr *sa)
138 struct thread *td = curthread;
139 struct proc *p = td->td_proc;
144 error = holdsock(p->p_fd, s, &fp);
147 error = sobind((struct socket *)fp->f_data, sa, td);
153 * bind_args(int s, caddr_t name, int namelen)
158 sys_bind(struct bind_args *uap)
163 error = getsockaddr(&sa, uap->name, uap->namelen);
166 error = kern_bind(uap->s, sa);
173 kern_listen(int s, int backlog)
175 struct thread *td = curthread;
176 struct proc *p = td->td_proc;
181 error = holdsock(p->p_fd, s, &fp);
184 error = solisten((struct socket *)fp->f_data, backlog, td);
190 * listen_args(int s, int backlog)
195 sys_listen(struct listen_args *uap)
199 error = kern_listen(uap->s, uap->backlog);
204 * Returns the accepted socket as well.
206 * NOTE! The sockets sitting on so_comp/so_incomp might have 0 refs, the
207 * pool token is absolutely required to avoid a sofree() race,
208 * as well as to avoid tailq handling races.
211 soaccept_predicate(struct netmsg_so_notify *msg)
213 struct socket *head = msg->base.nm_so;
216 if (head->so_error != 0) {
217 msg->base.lmsg.ms_error = head->so_error;
220 lwkt_getpooltoken(head);
221 if (!TAILQ_EMPTY(&head->so_comp)) {
222 /* Abuse nm_so field as copy in/copy out parameter. XXX JH */
223 so = TAILQ_FIRST(&head->so_comp);
224 TAILQ_REMOVE(&head->so_comp, so, so_list);
226 soclrstate(so, SS_COMP);
230 lwkt_relpooltoken(head);
232 msg->base.lmsg.ms_error = 0;
233 msg->base.nm_so = so;
236 lwkt_relpooltoken(head);
237 if (head->so_state & SS_CANTRCVMORE) {
238 msg->base.lmsg.ms_error = ECONNABORTED;
241 if (msg->nm_fflags & FNONBLOCK) {
242 msg->base.lmsg.ms_error = EWOULDBLOCK;
250 * The second argument to kern_accept() is a handle to a struct sockaddr.
251 * This allows kern_accept() to return a pointer to an allocated struct
252 * sockaddr which must be freed later with FREE(). The caller must
253 * initialize *name to NULL.
256 kern_accept(int s, int fflags, struct sockaddr **name, int *namelen, int *res)
258 struct thread *td = curthread;
259 struct filedesc *fdp = td->td_proc->p_fd;
260 struct file *lfp = NULL;
261 struct file *nfp = NULL;
263 struct socket *head, *so;
264 struct netmsg_so_notify msg;
266 u_int fflag; /* type must match fp->f_flag */
270 if (name && namelen && *namelen < 0)
273 error = holdsock(td->td_proc->p_fd, s, &lfp);
277 error = falloc(td->td_lwp, &nfp, &fd);
278 if (error) { /* Probably ran out of file descriptors. */
282 head = (struct socket *)lfp->f_data;
283 if ((head->so_options & SO_ACCEPTCONN) == 0) {
288 if (fflags & O_FBLOCKING)
289 fflags |= lfp->f_flag & ~FNONBLOCK;
290 else if (fflags & O_FNONBLOCKING)
291 fflags |= lfp->f_flag | FNONBLOCK;
293 fflags = lfp->f_flag;
295 if (use_soaccept_pred_fast) {
298 /* Initialize necessary parts for soaccept_predicate() */
299 netmsg_init(&msg.base, head, &netisr_apanic_rport, 0, NULL);
300 msg.nm_fflags = fflags;
302 lwkt_getpooltoken(head);
303 pred = soaccept_predicate(&msg);
304 lwkt_relpooltoken(head);
307 error = msg.base.lmsg.ms_error;
315 /* optimize for uniprocessor case later XXX JH */
316 netmsg_init_abortable(&msg.base, head, &curthread->td_msgport,
317 0, netmsg_so_notify, netmsg_so_notify_doabort);
318 msg.nm_predicate = soaccept_predicate;
319 msg.nm_fflags = fflags;
320 msg.nm_etype = NM_REVENT;
321 error = lwkt_domsg(head->so_port, &msg.base.lmsg, PCATCH);
327 * At this point we have the connection that's ready to be accepted.
329 * NOTE! soaccept_predicate() ref'd so for us, and soaccept() expects
330 * to eat the ref and turn it into a descriptor.
336 /* connection has been removed from the listen queue */
337 KNOTE(&head->so_rcv.ssb_kq.ki_note, 0);
339 if (head->so_sigio != NULL)
340 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
342 nfp->f_type = DTYPE_SOCKET;
344 nfp->f_ops = &socketops;
346 /* Sync socket nonblocking/async state with file flags */
347 tmp = fflag & FNONBLOCK;
348 fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, td->td_ucred, NULL);
349 tmp = fflag & FASYNC;
350 fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, td->td_ucred, NULL);
353 if (so->so_faddr != NULL) {
357 soaccept_generic(so);
360 error = soaccept(so, &sa);
364 * Set the returned name and namelen as applicable. Set the returned
365 * namelen to 0 for older code which might ignore the return value
369 if (sa && name && namelen) {
370 if (*namelen > sa->sa_len)
371 *namelen = sa->sa_len;
381 * If an error occured clear the reserved descriptor, else associate
384 * Note that *res is normally ignored if an error is returned but
385 * a syscall message will still have access to the result code.
388 fsetfd(fdp, NULL, fd);
391 fsetfd(fdp, nfp, fd);
399 * accept(int s, caddr_t name, int *anamelen)
404 sys_accept(struct accept_args *uap)
406 struct sockaddr *sa = NULL;
411 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
415 error = kern_accept(uap->s, 0, &sa, &sa_len,
416 &uap->sysmsg_iresult);
419 error = copyout(sa, uap->name, sa_len);
421 error = copyout(&sa_len, uap->anamelen,
422 sizeof(*uap->anamelen));
427 error = kern_accept(uap->s, 0, NULL, 0,
428 &uap->sysmsg_iresult);
434 * extaccept(int s, int fflags, caddr_t name, int *anamelen)
439 sys_extaccept(struct extaccept_args *uap)
441 struct sockaddr *sa = NULL;
444 int fflags = uap->flags & O_FMASK;
447 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
451 error = kern_accept(uap->s, fflags, &sa, &sa_len,
452 &uap->sysmsg_iresult);
455 error = copyout(sa, uap->name, sa_len);
457 error = copyout(&sa_len, uap->anamelen,
458 sizeof(*uap->anamelen));
463 error = kern_accept(uap->s, fflags, NULL, 0,
464 &uap->sysmsg_iresult);
471 * Returns TRUE if predicate satisfied.
474 soconnected_predicate(struct netmsg_so_notify *msg)
476 struct socket *so = msg->base.nm_so;
478 /* check predicate */
479 if (!(so->so_state & SS_ISCONNECTING) || so->so_error != 0) {
480 msg->base.lmsg.ms_error = so->so_error;
488 kern_connect(int s, int fflags, struct sockaddr *sa)
490 struct thread *td = curthread;
491 struct proc *p = td->td_proc;
494 int error, interrupted = 0;
496 error = holdsock(p->p_fd, s, &fp);
499 so = (struct socket *)fp->f_data;
501 if (fflags & O_FBLOCKING)
502 /* fflags &= ~FNONBLOCK; */;
503 else if (fflags & O_FNONBLOCKING)
508 if (so->so_state & SS_ISCONNECTING) {
512 error = soconnect(so, sa, td, use_soconnect_async ? FALSE : TRUE);
515 if ((fflags & FNONBLOCK) && (so->so_state & SS_ISCONNECTING)) {
519 if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
520 struct netmsg_so_notify msg;
522 netmsg_init_abortable(&msg.base, so,
523 &curthread->td_msgport,
526 netmsg_so_notify_doabort);
527 msg.nm_predicate = soconnected_predicate;
528 msg.nm_etype = NM_REVENT;
529 error = lwkt_domsg(so->so_port, &msg.base.lmsg, PCATCH);
530 if (error == EINTR || error == ERESTART)
534 error = so->so_error;
539 soclrstate(so, SS_ISCONNECTING);
540 if (error == ERESTART)
548 * connect_args(int s, caddr_t name, int namelen)
553 sys_connect(struct connect_args *uap)
558 error = getsockaddr(&sa, uap->name, uap->namelen);
561 error = kern_connect(uap->s, 0, sa);
568 * connect_args(int s, int fflags, caddr_t name, int namelen)
573 sys_extconnect(struct extconnect_args *uap)
577 int fflags = uap->flags & O_FMASK;
579 error = getsockaddr(&sa, uap->name, uap->namelen);
582 error = kern_connect(uap->s, fflags, sa);
589 kern_socketpair(int domain, int type, int protocol, int *sv)
591 struct thread *td = curthread;
592 struct filedesc *fdp;
593 struct file *fp1, *fp2;
594 struct socket *so1, *so2;
597 fdp = td->td_proc->p_fd;
598 error = socreate(domain, &so1, type, protocol, td);
601 error = socreate(domain, &so2, type, protocol, td);
604 error = falloc(td->td_lwp, &fp1, &fd1);
609 error = falloc(td->td_lwp, &fp2, &fd2);
614 error = soconnect2(so1, so2);
617 if (type == SOCK_DGRAM) {
619 * Datagram socket connection is asymmetric.
621 error = soconnect2(so2, so1);
625 fp1->f_type = fp2->f_type = DTYPE_SOCKET;
626 fp1->f_flag = fp2->f_flag = FREAD|FWRITE;
627 fp1->f_ops = fp2->f_ops = &socketops;
628 fsetfd(fdp, fp1, fd1);
629 fsetfd(fdp, fp2, fd2);
634 fsetfd(fdp, NULL, fd2);
637 fsetfd(fdp, NULL, fd1);
640 (void)soclose(so2, 0);
642 (void)soclose(so1, 0);
647 * socketpair(int domain, int type, int protocol, int *rsv)
650 sys_socketpair(struct socketpair_args *uap)
654 error = kern_socketpair(uap->domain, uap->type, uap->protocol, sockv);
657 error = copyout(sockv, uap->rsv, sizeof(sockv));
660 kern_close(sockv[0]);
661 kern_close(sockv[1]);
669 kern_sendmsg(int s, struct sockaddr *sa, struct uio *auio,
670 struct mbuf *control, int flags, size_t *res)
672 struct thread *td = curthread;
673 struct lwp *lp = td->td_lwp;
674 struct proc *p = td->td_proc;
680 struct iovec *ktriov = NULL;
684 error = holdsock(p->p_fd, s, &fp);
688 if (KTRPOINT(td, KTR_GENIO)) {
689 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
691 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
692 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
696 len = auio->uio_resid;
697 so = (struct socket *)fp->f_data;
698 if ((flags & (MSG_FNONBLOCKING|MSG_FBLOCKING)) == 0) {
699 if (fp->f_flag & FNONBLOCK)
700 flags |= MSG_FNONBLOCKING;
702 error = so_pru_sosend(so, sa, auio, NULL, control, flags, td);
704 if (auio->uio_resid != len && (error == ERESTART ||
705 error == EINTR || error == EWOULDBLOCK))
707 if (error == EPIPE && !(flags & MSG_NOSIGNAL) &&
708 !(so->so_options & SO_NOSIGPIPE))
709 lwpsignal(p, lp, SIGPIPE);
712 if (ktriov != NULL) {
714 ktruio.uio_iov = ktriov;
715 ktruio.uio_resid = len - auio->uio_resid;
716 ktrgenio(lp, s, UIO_WRITE, &ktruio, error);
718 kfree(ktriov, M_TEMP);
722 *res = len - auio->uio_resid;
728 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
733 sys_sendto(struct sendto_args *uap)
735 struct thread *td = curthread;
738 struct sockaddr *sa = NULL;
742 error = getsockaddr(&sa, uap->to, uap->tolen);
746 aiov.iov_base = uap->buf;
747 aiov.iov_len = uap->len;
748 auio.uio_iov = &aiov;
751 auio.uio_resid = uap->len;
752 auio.uio_segflg = UIO_USERSPACE;
753 auio.uio_rw = UIO_WRITE;
756 error = kern_sendmsg(uap->s, sa, &auio, NULL, uap->flags,
757 &uap->sysmsg_szresult);
765 * sendmsg_args(int s, caddr_t msg, int flags)
770 sys_sendmsg(struct sendmsg_args *uap)
772 struct thread *td = curthread;
775 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
776 struct sockaddr *sa = NULL;
777 struct mbuf *control = NULL;
780 error = copyin(uap->msg, (caddr_t)&msg, sizeof(msg));
785 * Conditionally copyin msg.msg_name.
788 error = getsockaddr(&sa, msg.msg_name, msg.msg_namelen);
796 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
801 auio.uio_iovcnt = msg.msg_iovlen;
803 auio.uio_segflg = UIO_USERSPACE;
804 auio.uio_rw = UIO_WRITE;
808 * Conditionally copyin msg.msg_control.
810 if (msg.msg_control) {
811 if (msg.msg_controllen < sizeof(struct cmsghdr) ||
812 msg.msg_controllen > MLEN) {
816 control = m_get(M_WAITOK, MT_CONTROL);
817 if (control == NULL) {
821 control->m_len = msg.msg_controllen;
822 error = copyin(msg.msg_control, mtod(control, caddr_t),
830 error = kern_sendmsg(uap->s, sa, &auio, control, uap->flags,
831 &uap->sysmsg_szresult);
834 iovec_free(&iov, aiov);
842 * kern_recvmsg() takes a handle to sa and control. If the handle is non-
843 * null, it returns a dynamically allocated struct sockaddr and an mbuf.
844 * Don't forget to FREE() and m_free() these if they are returned.
847 kern_recvmsg(int s, struct sockaddr **sa, struct uio *auio,
848 struct mbuf **control, int *flags, size_t *res)
850 struct thread *td = curthread;
851 struct proc *p = td->td_proc;
858 struct iovec *ktriov = NULL;
862 error = holdsock(p->p_fd, s, &fp);
866 if (KTRPOINT(td, KTR_GENIO)) {
867 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
869 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
870 bcopy(auio->uio_iov, ktriov, iovlen);
874 len = auio->uio_resid;
875 so = (struct socket *)fp->f_data;
877 if (flags == NULL || (*flags & (MSG_FNONBLOCKING|MSG_FBLOCKING)) == 0) {
878 if (fp->f_flag & FNONBLOCK) {
880 *flags |= MSG_FNONBLOCKING;
882 lflags = MSG_FNONBLOCKING;
888 error = so_pru_soreceive(so, sa, auio, NULL, control, flags);
890 if (auio->uio_resid != len && (error == ERESTART ||
891 error == EINTR || error == EWOULDBLOCK))
895 if (ktriov != NULL) {
897 ktruio.uio_iov = ktriov;
898 ktruio.uio_resid = len - auio->uio_resid;
899 ktrgenio(td->td_lwp, s, UIO_READ, &ktruio, error);
901 kfree(ktriov, M_TEMP);
905 *res = len - auio->uio_resid;
911 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
912 * caddr_t from, int *fromlenaddr)
917 sys_recvfrom(struct recvfrom_args *uap)
919 struct thread *td = curthread;
922 struct sockaddr *sa = NULL;
925 if (uap->from && uap->fromlenaddr) {
926 error = copyin(uap->fromlenaddr, &fromlen, sizeof(fromlen));
934 aiov.iov_base = uap->buf;
935 aiov.iov_len = uap->len;
936 auio.uio_iov = &aiov;
939 auio.uio_resid = uap->len;
940 auio.uio_segflg = UIO_USERSPACE;
941 auio.uio_rw = UIO_READ;
944 error = kern_recvmsg(uap->s, uap->from ? &sa : NULL, &auio, NULL,
945 &uap->flags, &uap->sysmsg_szresult);
947 if (error == 0 && uap->from) {
948 /* note: sa may still be NULL */
950 fromlen = MIN(fromlen, sa->sa_len);
951 error = copyout(sa, uap->from, fromlen);
956 error = copyout(&fromlen, uap->fromlenaddr,
967 * recvmsg_args(int s, struct msghdr *msg, int flags)
972 sys_recvmsg(struct recvmsg_args *uap)
974 struct thread *td = curthread;
977 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
978 struct mbuf *m, *control = NULL;
979 struct sockaddr *sa = NULL;
981 socklen_t *ufromlenp, *ucontrollenp;
982 int error, fromlen, controllen, len, flags, *uflagsp;
985 * This copyin handles everything except the iovec.
987 error = copyin(uap->msg, &msg, sizeof(msg));
991 if (msg.msg_name && msg.msg_namelen < 0)
993 if (msg.msg_control && msg.msg_controllen < 0)
996 ufromlenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
998 ucontrollenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
1000 uflagsp = (int *)((caddr_t)uap->msg + offsetof(struct msghdr,
1006 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
1011 auio.uio_iovcnt = msg.msg_iovlen;
1012 auio.uio_offset = 0;
1013 auio.uio_segflg = UIO_USERSPACE;
1014 auio.uio_rw = UIO_READ;
1019 error = kern_recvmsg(uap->s,
1020 (msg.msg_name ? &sa : NULL), &auio,
1021 (msg.msg_control ? &control : NULL), &flags,
1022 &uap->sysmsg_szresult);
1025 * Conditionally copyout the name and populate the namelen field.
1027 if (error == 0 && msg.msg_name) {
1028 /* note: sa may still be NULL */
1030 fromlen = MIN(msg.msg_namelen, sa->sa_len);
1031 error = copyout(sa, msg.msg_name, fromlen);
1036 error = copyout(&fromlen, ufromlenp,
1037 sizeof(*ufromlenp));
1041 * Copyout msg.msg_control and msg.msg_controllen.
1043 if (error == 0 && msg.msg_control) {
1044 len = msg.msg_controllen;
1046 ctlbuf = (caddr_t)msg.msg_control;
1048 while(m && len > 0) {
1049 unsigned int tocopy;
1051 if (len >= m->m_len) {
1054 msg.msg_flags |= MSG_CTRUNC;
1058 error = copyout(mtod(m, caddr_t), ctlbuf, tocopy);
1066 controllen = ctlbuf - (caddr_t)msg.msg_control;
1067 error = copyout(&controllen, ucontrollenp,
1068 sizeof(*ucontrollenp));
1072 error = copyout(&flags, uflagsp, sizeof(*uflagsp));
1076 kfree(sa, M_SONAME);
1077 iovec_free(&iov, aiov);
1084 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
1085 * in kernel pointer instead of a userland pointer. This allows us
1086 * to manipulate socket options in the emulation code.
1089 kern_setsockopt(int s, struct sockopt *sopt)
1091 struct thread *td = curthread;
1092 struct proc *p = td->td_proc;
1096 if (sopt->sopt_val == NULL && sopt->sopt_valsize != 0)
1098 if (sopt->sopt_val != NULL && sopt->sopt_valsize == 0)
1100 if (sopt->sopt_valsize > SOMAXOPT_SIZE) /* unsigned */
1103 error = holdsock(p->p_fd, s, &fp);
1107 error = sosetopt((struct socket *)fp->f_data, sopt);
1113 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
1118 sys_setsockopt(struct setsockopt_args *uap)
1120 struct thread *td = curthread;
1121 struct sockopt sopt;
1124 sopt.sopt_level = uap->level;
1125 sopt.sopt_name = uap->name;
1126 sopt.sopt_valsize = uap->valsize;
1128 sopt.sopt_val = NULL;
1130 if (sopt.sopt_valsize > SOMAXOPT_SIZE) /* unsigned */
1133 sopt.sopt_val = kmalloc(sopt.sopt_valsize, M_TEMP, M_WAITOK);
1134 error = copyin(uap->val, sopt.sopt_val, sopt.sopt_valsize);
1139 error = kern_setsockopt(uap->s, &sopt);
1142 kfree(sopt.sopt_val, M_TEMP);
1147 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
1148 * in kernel pointer instead of a userland pointer. This allows us
1149 * to manipulate socket options in the emulation code.
1152 kern_getsockopt(int s, struct sockopt *sopt)
1154 struct thread *td = curthread;
1155 struct proc *p = td->td_proc;
1159 if (sopt->sopt_val == NULL && sopt->sopt_valsize != 0)
1161 if (sopt->sopt_val != NULL && sopt->sopt_valsize == 0)
1163 if (sopt->sopt_valsize > SOMAXOPT_SIZE) /* unsigned */
1166 error = holdsock(p->p_fd, s, &fp);
1170 error = sogetopt((struct socket *)fp->f_data, sopt);
1176 * getsockopt_args(int s, int level, int name, caddr_t val, int *avalsize)
1181 sys_getsockopt(struct getsockopt_args *uap)
1183 struct thread *td = curthread;
1184 struct sockopt sopt;
1188 error = copyin(uap->avalsize, &valsize, sizeof(valsize));
1195 sopt.sopt_level = uap->level;
1196 sopt.sopt_name = uap->name;
1197 sopt.sopt_valsize = valsize;
1199 sopt.sopt_val = NULL;
1201 if (sopt.sopt_valsize > SOMAXOPT_SIZE) /* unsigned */
1204 sopt.sopt_val = kmalloc(sopt.sopt_valsize, M_TEMP, M_WAITOK);
1205 error = copyin(uap->val, sopt.sopt_val, sopt.sopt_valsize);
1210 error = kern_getsockopt(uap->s, &sopt);
1213 valsize = sopt.sopt_valsize;
1214 error = copyout(&valsize, uap->avalsize, sizeof(valsize));
1218 error = copyout(sopt.sopt_val, uap->val, sopt.sopt_valsize);
1221 kfree(sopt.sopt_val, M_TEMP);
1226 * The second argument to kern_getsockname() is a handle to a struct sockaddr.
1227 * This allows kern_getsockname() to return a pointer to an allocated struct
1228 * sockaddr which must be freed later with FREE(). The caller must
1229 * initialize *name to NULL.
1232 kern_getsockname(int s, struct sockaddr **name, int *namelen)
1234 struct thread *td = curthread;
1235 struct proc *p = td->td_proc;
1238 struct sockaddr *sa = NULL;
1241 error = holdsock(p->p_fd, s, &fp);
1248 so = (struct socket *)fp->f_data;
1249 error = so_pru_sockaddr(so, &sa);
1254 *namelen = MIN(*namelen, sa->sa_len);
1264 * getsockname_args(int fdes, caddr_t asa, int *alen)
1271 sys_getsockname(struct getsockname_args *uap)
1273 struct sockaddr *sa = NULL;
1276 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1280 error = kern_getsockname(uap->fdes, &sa, &sa_len);
1283 error = copyout(sa, uap->asa, sa_len);
1285 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1287 kfree(sa, M_SONAME);
1292 * The second argument to kern_getpeername() is a handle to a struct sockaddr.
1293 * This allows kern_getpeername() to return a pointer to an allocated struct
1294 * sockaddr which must be freed later with FREE(). The caller must
1295 * initialize *name to NULL.
1298 kern_getpeername(int s, struct sockaddr **name, int *namelen)
1300 struct thread *td = curthread;
1301 struct proc *p = td->td_proc;
1304 struct sockaddr *sa = NULL;
1307 error = holdsock(p->p_fd, s, &fp);
1314 so = (struct socket *)fp->f_data;
1315 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1319 error = so_pru_peeraddr(so, &sa);
1324 *namelen = MIN(*namelen, sa->sa_len);
1334 * getpeername_args(int fdes, caddr_t asa, int *alen)
1336 * Get name of peer for connected socket.
1341 sys_getpeername(struct getpeername_args *uap)
1343 struct sockaddr *sa = NULL;
1346 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1350 error = kern_getpeername(uap->fdes, &sa, &sa_len);
1353 error = copyout(sa, uap->asa, sa_len);
1355 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1357 kfree(sa, M_SONAME);
1362 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1364 struct sockaddr *sa;
1368 if (len > SOCK_MAXADDRLEN)
1369 return ENAMETOOLONG;
1370 if (len < offsetof(struct sockaddr, sa_data[0]))
1372 sa = kmalloc(len, M_SONAME, M_WAITOK);
1373 error = copyin(uaddr, sa, len);
1375 kfree(sa, M_SONAME);
1377 #if BYTE_ORDER != BIG_ENDIAN
1379 * The bind(), connect(), and sendto() syscalls were not
1380 * versioned for COMPAT_43. Thus, this check must stay.
1382 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1383 sa->sa_family = sa->sa_len;
1392 * Detach a mapped page and release resources back to the system.
1393 * We must release our wiring and if the object is ripped out
1394 * from under the vm_page we become responsible for freeing the
1400 sf_buf_mfree(void *arg)
1402 struct sf_buf *sf = arg;
1405 m = sf_buf_page(sf);
1406 if (sf_buf_free(sf)) {
1407 /* sf invalid now */
1409 vm_page_busy_wait(m, FALSE, "sockpgf");
1414 if (m->object == NULL &&
1415 m->wire_count == 0 &&
1416 (m->flags & PG_NEED_COMMIT) == 0) {
1427 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1428 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1430 * Send a file specified by 'fd' and starting at 'offset' to a socket
1431 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1432 * nbytes == 0. Optionally add a header and/or trailer to the socket
1433 * output. If specified, write the total number of bytes sent into *sbytes.
1435 * In FreeBSD kern/uipc_syscalls.c,v 1.103, a bug was fixed that caused
1436 * the headers to count against the remaining bytes to be sent from
1437 * the file descriptor. We may wish to implement a compatibility syscall
1443 sys_sendfile(struct sendfile_args *uap)
1445 struct thread *td = curthread;
1446 struct proc *p = td->td_proc;
1448 struct vnode *vp = NULL;
1449 struct sf_hdtr hdtr;
1450 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
1452 struct mbuf *mheader = NULL;
1455 off_t hdtr_size = 0;
1462 * Do argument checking. Must be a regular file in, stream
1463 * type and connected socket out, positive offset.
1465 fp = holdfp(p->p_fd, uap->fd, FREAD);
1469 if (fp->f_type != DTYPE_VNODE) {
1473 vp = (struct vnode *)fp->f_data;
1478 * If specified, get the pointer to the sf_hdtr struct for
1479 * any headers/trailers.
1482 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1489 error = iovec_copyin(hdtr.headers, &iov, aiov,
1490 hdtr.hdr_cnt, &hbytes);
1494 auio.uio_iovcnt = hdtr.hdr_cnt;
1495 auio.uio_offset = 0;
1496 auio.uio_segflg = UIO_USERSPACE;
1497 auio.uio_rw = UIO_WRITE;
1499 auio.uio_resid = hbytes;
1501 mheader = m_uiomove(&auio);
1503 iovec_free(&iov, aiov);
1504 if (mheader == NULL)
1509 error = kern_sendfile(vp, uap->s, uap->offset, uap->nbytes, mheader,
1510 &sbytes, uap->flags);
1515 * Send trailers. Wimp out and use writev(2).
1517 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1518 error = iovec_copyin(hdtr.trailers, &iov, aiov,
1519 hdtr.trl_cnt, &auio.uio_resid);
1523 auio.uio_iovcnt = hdtr.trl_cnt;
1524 auio.uio_offset = 0;
1525 auio.uio_segflg = UIO_USERSPACE;
1526 auio.uio_rw = UIO_WRITE;
1529 error = kern_sendmsg(uap->s, NULL, &auio, NULL, 0, &tbytes);
1531 iovec_free(&iov, aiov);
1534 hdtr_size += tbytes; /* trailer bytes successfully sent */
1540 if (uap->sbytes != NULL) {
1541 sbytes += hdtr_size;
1542 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1548 kern_sendfile(struct vnode *vp, int sfd, off_t offset, size_t nbytes,
1549 struct mbuf *mheader, off_t *sbytes, int flags)
1551 struct thread *td = curthread;
1552 struct proc *p = td->td_proc;
1553 struct vm_object *obj;
1556 struct mbuf *m, *mp;
1559 off_t off, xfsize, xbytes;
1563 if (vp->v_type != VREG) {
1567 if ((obj = vp->v_object) == NULL) {
1571 error = holdsock(p->p_fd, sfd, &fp);
1574 so = (struct socket *)fp->f_data;
1575 if (so->so_type != SOCK_STREAM) {
1579 if ((so->so_state & SS_ISCONNECTED) == 0) {
1589 * preallocation is required for asynchronous passing of mbufs,
1590 * otherwise we can wind up building up an infinite number of
1591 * mbufs during the asynchronous latency.
1593 if ((so->so_snd.ssb_flags & (SSB_PREALLOC | SSB_STOPSUPP)) == 0) {
1601 * Protect against multiple writers to the socket.
1603 ssb_lock(&so->so_snd, M_WAITOK);
1606 * Loop through the pages in the file, starting with the requested
1607 * offset. Get a file page (do I/O if necessary), map the file page
1608 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1611 for (off = offset; ; off += xfsize, *sbytes += xfsize + hbytes, xbytes += xfsize) {
1616 pindex = OFF_TO_IDX(off);
1619 * Calculate the amount to transfer. Not to exceed a page,
1620 * the EOF, or the passed in nbytes.
1622 xfsize = vp->v_filesize - off;
1623 if (xfsize > PAGE_SIZE)
1625 pgoff = (vm_offset_t)(off & PAGE_MASK);
1626 if (PAGE_SIZE - pgoff < xfsize)
1627 xfsize = PAGE_SIZE - pgoff;
1628 if (nbytes && xfsize > (nbytes - xbytes))
1629 xfsize = nbytes - xbytes;
1633 * Optimize the non-blocking case by looking at the socket space
1634 * before going to the extra work of constituting the sf_buf.
1636 if (so->so_snd.ssb_flags & SSB_PREALLOC)
1637 space = ssb_space_prealloc(&so->so_snd);
1639 space = ssb_space(&so->so_snd);
1641 if ((fp->f_flag & FNONBLOCK) && space <= 0) {
1642 if (so->so_state & SS_CANTSENDMORE)
1646 ssb_unlock(&so->so_snd);
1650 * Attempt to look up the page.
1652 * Allocate if not found, wait and loop if busy, then hold the page.
1653 * We hold rather than wire the page because we do not want to prevent
1654 * filesystem truncation operations from occuring on the file. This
1655 * can happen even under normal operation if the file being sent is
1656 * remove()d after the sendfile() call completes, because the socket buffer
1657 * may still be draining. tmpfs will crash if we try to use wire.
1659 vm_object_hold(obj);
1660 pg = vm_page_lookup_busy_try(obj, pindex, TRUE, &error);
1662 vm_page_sleep_busy(pg, TRUE, "sfpbsy");
1663 vm_object_drop(obj);
1667 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL |
1671 vm_object_drop(obj);
1676 vm_object_drop(obj);
1679 * If page is not valid for what we need, initiate I/O
1682 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1688 * Ensure that our page is still around when the I/O
1691 * Ensure that our page is not modified while part of
1692 * a mbuf as this could mess up tcp checksums, DMA,
1693 * etc (XXX NEEDS WORK). The softbusy is supposed to
1694 * help here but it actually doesn't.
1696 * XXX THIS HAS MULTIPLE PROBLEMS. The underlying
1697 * VM pages are not protected by the soft-busy
1698 * unless we vm_page_protect... READ them, and
1699 * they STILL aren't protected against
1700 * modification via the buffer cache (VOP_WRITE).
1702 * Fixing the second issue is particularly
1705 * XXX We also can't soft-busy anyway because it can
1706 * deadlock against the syncer doing a vfs_msync(),
1707 * vfs_msync->vmntvnodesca->vfs_msync_scan2->
1708 * vm_object_page_clean->(scan)-> ... page
1711 /*vm_page_io_start(pg);*/
1715 * Get the page from backing store.
1717 bsize = vp->v_mount->mnt_stat.f_iosize;
1718 auio.uio_iov = &aiov;
1719 auio.uio_iovcnt = 1;
1721 aiov.iov_len = MAXBSIZE;
1722 auio.uio_resid = MAXBSIZE;
1723 auio.uio_offset = trunc_page(off);
1724 auio.uio_segflg = UIO_NOCOPY;
1725 auio.uio_rw = UIO_READ;
1727 vn_lock(vp, LK_SHARED | LK_RETRY);
1728 error = VOP_READ(vp, &auio,
1729 IO_VMIO | ((MAXBSIZE / bsize) << 16),
1732 vm_page_flag_clear(pg, PG_ZERO);
1733 vm_page_busy_wait(pg, FALSE, "sockpg");
1734 /*vm_page_io_finish(pg);*/
1738 /* vm_page_try_to_free(pg); */
1739 ssb_unlock(&so->so_snd);
1746 * Get a sendfile buf. We usually wait as long as necessary,
1747 * but this wait can be interrupted.
1749 if ((sf = sf_buf_alloc(pg)) == NULL) {
1752 /* vm_page_try_to_free(pg); */
1753 ssb_unlock(&so->so_snd);
1759 * Get an mbuf header and set it up as having external storage.
1761 MGETHDR(m, M_WAITOK, MT_DATA);
1766 /* vm_page_try_to_free(pg); */
1768 ssb_unlock(&so->so_snd);
1774 m->m_ext.ext_free = sf_buf_mfree;
1775 m->m_ext.ext_ref = sf_buf_ref;
1776 m->m_ext.ext_arg = sf;
1777 m->m_ext.ext_buf = (void *)sf_buf_kva(sf);
1778 m->m_ext.ext_size = PAGE_SIZE;
1779 m->m_data = (char *)sf_buf_kva(sf) + pgoff;
1780 m->m_flags |= M_EXT;
1781 m->m_pkthdr.len = m->m_len = xfsize;
1782 KKASSERT((m->m_flags & (M_EXT_CLUSTER)) == 0);
1784 if (mheader != NULL) {
1785 hbytes = mheader->m_pkthdr.len;
1786 mheader->m_pkthdr.len += m->m_pkthdr.len;
1794 * Add the buffer to the socket buffer chain.
1799 * Make sure that the socket is still able to take more data.
1800 * CANTSENDMORE being true usually means that the connection
1801 * was closed. so_error is true when an error was sensed after
1803 * The state is checked after the page mapping and buffer
1804 * allocation above since those operations may block and make
1805 * any socket checks stale. From this point forward, nothing
1806 * blocks before the pru_send (or more accurately, any blocking
1807 * results in a loop back to here to re-check).
1809 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1810 if (so->so_state & SS_CANTSENDMORE) {
1813 error = so->so_error;
1817 ssb_unlock(&so->so_snd);
1822 * Wait for socket space to become available. We do this just
1823 * after checking the connection state above in order to avoid
1824 * a race condition with ssb_wait().
1826 if (so->so_snd.ssb_flags & SSB_PREALLOC)
1827 space = ssb_space_prealloc(&so->so_snd);
1829 space = ssb_space(&so->so_snd);
1831 if (space < m->m_pkthdr.len && space < so->so_snd.ssb_lowat) {
1832 if (fp->f_flag & FNONBLOCK) {
1834 ssb_unlock(&so->so_snd);
1839 error = ssb_wait(&so->so_snd);
1841 * An error from ssb_wait usually indicates that we've
1842 * been interrupted by a signal. If we've sent anything
1843 * then return bytes sent, otherwise return the error.
1847 ssb_unlock(&so->so_snd);
1854 if (so->so_snd.ssb_flags & SSB_PREALLOC) {
1855 for (mp = m; mp != NULL; mp = mp->m_next)
1856 ssb_preallocstream(&so->so_snd, mp);
1858 if (use_sendfile_async)
1859 error = so_pru_senda(so, 0, m, NULL, NULL, td);
1861 error = so_pru_send(so, 0, m, NULL, NULL, td);
1865 ssb_unlock(&so->so_snd);
1869 if (mheader != NULL) {
1870 *sbytes += mheader->m_pkthdr.len;
1872 if (so->so_snd.ssb_flags & SSB_PREALLOC) {
1873 for (mp = mheader; mp != NULL; mp = mp->m_next)
1874 ssb_preallocstream(&so->so_snd, mp);
1876 if (use_sendfile_async)
1877 error = so_pru_senda(so, 0, mheader, NULL, NULL, td);
1879 error = so_pru_send(so, 0, mheader, NULL, NULL, td);
1883 ssb_unlock(&so->so_snd);
1888 if (mheader != NULL)