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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
37 * $FreeBSD: src/sys/kern/uipc_syscalls.c,v 1.65.2.17 2003/04/04 17:11:16 tegge Exp $
38 * $DragonFly: src/sys/kern/uipc_syscalls.c,v 1.19 2003/10/08 03:21:26 daver Exp $
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/sysproto.h>
47 #include <sys/malloc.h>
48 #include <sys/filedesc.h>
49 #include <sys/event.h>
51 #include <sys/fcntl.h>
53 #include <sys/filio.h>
54 #include <sys/kern_syscall.h>
56 #include <sys/protosw.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/signalvar.h>
61 #include <sys/vnode.h>
63 #include <sys/mount.h>
65 #include <sys/ktrace.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_pageout.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_extern.h>
73 #include <sys/file2.h>
75 static void sf_buf_init(void *arg);
76 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
77 static SLIST_HEAD(, sf_buf) sf_freelist;
78 static vm_offset_t sf_base;
79 static struct sf_buf *sf_bufs;
80 static int sf_buf_alloc_want;
83 * System call interface to the socket abstraction.
86 extern struct fileops socketops;
89 * socket_args(int domain, int type, int protocol)
92 kern_socket(int domain, int type, int protocol, int *res)
94 struct thread *td = curthread;
95 struct proc *p = td->td_proc;
104 error = falloc(p, &fp, &fd);
108 error = socreate(domain, &so, type, protocol, td);
110 if (fdp->fd_ofiles[fd] == fp) {
111 fdp->fd_ofiles[fd] = NULL;
115 fp->f_data = (caddr_t)so;
116 fp->f_flag = FREAD|FWRITE;
117 fp->f_ops = &socketops;
118 fp->f_type = DTYPE_SOCKET;
126 socket(struct socket_args *uap)
130 error = kern_socket(uap->domain, uap->type, uap->protocol,
131 &uap->sysmsg_result);
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)
156 bind(struct bind_args *uap)
161 error = getsockaddr(&sa, uap->name, uap->namelen);
164 error = kern_bind(uap->s, sa);
171 kern_listen(int s, int backlog)
173 struct thread *td = curthread;
174 struct proc *p = td->td_proc;
179 error = holdsock(p->p_fd, s, &fp);
182 error = solisten((struct socket *)fp->f_data, backlog, td);
188 * listen_args(int s, int backlog)
191 listen(struct listen_args *uap)
195 error = kern_listen(uap->s, uap->backlog);
200 * The second argument to kern_accept() is a handle to a struct sockaddr.
201 * This allows kern_accept() to return a pointer to an allocated struct
202 * sockaddr which must be freed later with FREE(). The caller must
203 * initialize *name to NULL.
206 kern_accept(int s, struct sockaddr **name, int *namelen, int *res)
208 struct thread *td = curthread;
209 struct proc *p = td->td_proc;
210 struct filedesc *fdp = p->p_fd;
211 struct file *lfp = NULL;
212 struct file *nfp = NULL;
215 struct socket *head, *so;
217 u_int fflag; /* type must match fp->f_flag */
220 if (name && namelen && *namelen < 0)
223 error = holdsock(fdp, s, &lfp);
227 head = (struct socket *)lfp->f_data;
228 if ((head->so_options & SO_ACCEPTCONN) == 0) {
233 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
234 if (head->so_state & SS_CANTRCVMORE) {
235 head->so_error = ECONNABORTED;
238 if ((head->so_state & SS_NBIO) != 0) {
239 head->so_error = EWOULDBLOCK;
242 error = tsleep((caddr_t)&head->so_timeo, PCATCH, "accept", 0);
248 if (head->so_error) {
249 error = head->so_error;
256 * At this point we know that there is at least one connection
257 * ready to be accepted. Remove it from the queue prior to
258 * allocating the file descriptor for it since falloc() may
259 * block allowing another process to accept the connection
262 so = TAILQ_FIRST(&head->so_comp);
263 TAILQ_REMOVE(&head->so_comp, so, so_list);
267 error = falloc(p, &nfp, &fd);
270 * Probably ran out of file descriptors. Put the
271 * unaccepted connection back onto the queue and
272 * do another wakeup so some other process might
273 * have a chance at it.
275 TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
277 wakeup_one(&head->so_timeo);
284 /* connection has been removed from the listen queue */
285 KNOTE(&head->so_rcv.sb_sel.si_note, 0);
287 so->so_state &= ~SS_COMP;
289 if (head->so_sigio != NULL)
290 fsetown(fgetown(head->so_sigio), &so->so_sigio);
292 nfp->f_data = (caddr_t)so;
294 nfp->f_ops = &socketops;
295 nfp->f_type = DTYPE_SOCKET;
296 /* Sync socket nonblocking/async state with file flags */
297 tmp = fflag & FNONBLOCK;
298 (void) fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, td);
299 tmp = fflag & FASYNC;
300 (void) fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, td);
303 error = soaccept(so, &sa);
306 * Set the returned name and namelen as applicable. Set the returned
307 * namelen to 0 for older code which might ignore the return value
311 if (sa && name && namelen) {
312 if (*namelen > sa->sa_len)
313 *namelen = sa->sa_len;
322 * close the new descriptor, assuming someone hasn't ripped it
323 * out from under us. Note that *res is normally ignored if an
324 * error is returned but a syscall message will still have access
325 * to the result code.
329 if (fdp->fd_ofiles[fd] == nfp) {
330 fdp->fd_ofiles[fd] = NULL;
337 * Release explicitly held references before returning.
347 * accept_args(int s, caddr_t name, int *anamelen)
350 accept(struct accept_args *uap)
352 struct sockaddr *sa = NULL;
357 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
361 error = kern_accept(uap->s, &sa, &sa_len, &uap->sysmsg_result);
364 error = copyout(sa, uap->name, sa_len);
366 error = copyout(&sa_len, uap->anamelen,
367 sizeof(*uap->anamelen));
372 error = kern_accept(uap->s, NULL, 0, &uap->sysmsg_result);
378 kern_connect(int s, struct sockaddr *sa)
380 struct thread *td = curthread;
381 struct proc *p = td->td_proc;
386 error = holdsock(p->p_fd, s, &fp);
389 so = (struct socket *)fp->f_data;
390 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
394 error = soconnect(so, sa, td);
397 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
402 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
403 error = tsleep((caddr_t)&so->so_timeo, PCATCH, "connec", 0);
408 error = so->so_error;
413 so->so_state &= ~SS_ISCONNECTING;
414 if (error == ERESTART)
422 * connect_args(int s, caddr_t name, int namelen)
425 connect(struct connect_args *uap)
430 error = getsockaddr(&sa, uap->name, uap->namelen);
433 error = kern_connect(uap->s, sa);
440 kern_socketpair(int domain, int type, int protocol, int *sv)
442 struct thread *td = curthread;
443 struct proc *p = td->td_proc;
444 struct filedesc *fdp;
445 struct file *fp1, *fp2;
446 struct socket *so1, *so2;
451 error = socreate(domain, &so1, type, protocol, td);
454 error = socreate(domain, &so2, type, protocol, td);
457 error = falloc(p, &fp1, &fd);
462 fp1->f_data = (caddr_t)so1;
463 error = falloc(p, &fp2, &fd);
467 fp2->f_data = (caddr_t)so2;
469 error = soconnect2(so1, so2);
472 if (type == SOCK_DGRAM) {
474 * Datagram socket connection is asymmetric.
476 error = soconnect2(so2, so1);
480 fp1->f_flag = fp2->f_flag = FREAD|FWRITE;
481 fp1->f_ops = fp2->f_ops = &socketops;
482 fp1->f_type = fp2->f_type = DTYPE_SOCKET;
487 if (fdp->fd_ofiles[sv[1]] == fp2) {
488 fdp->fd_ofiles[sv[1]] = NULL;
493 if (fdp->fd_ofiles[sv[0]] == fp1) {
494 fdp->fd_ofiles[sv[0]] = NULL;
506 * socketpair(int domain, int type, int protocol, int *rsv)
509 socketpair(struct socketpair_args *uap)
513 error = kern_socketpair(uap->domain, uap->type, uap->protocol, sockv);
516 error = copyout(sockv, uap->rsv, sizeof(sockv));
521 kern_sendmsg(int s, struct sockaddr *sa, struct uio *auio,
522 struct mbuf *control, int flags, int *res)
524 struct thread *td = curthread;
525 struct proc *p = td->td_proc;
530 struct iovec *ktriov = NULL;
534 error = holdsock(p->p_fd, s, &fp);
538 if (KTRPOINT(td, KTR_GENIO)) {
539 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
541 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
542 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
546 len = auio->uio_resid;
547 so = (struct socket *)fp->f_data;
548 error = so->so_proto->pr_usrreqs->pru_sosend(so, sa, auio, NULL,
551 if (auio->uio_resid != len && (error == ERESTART ||
552 error == EINTR || error == EWOULDBLOCK))
558 if (ktriov != NULL) {
560 ktruio.uio_iov = ktriov;
561 ktruio.uio_resid = len - auio->uio_resid;
562 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error);
564 FREE(ktriov, M_TEMP);
568 *res = len - auio->uio_resid;
574 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
577 sendto(struct sendto_args *uap)
579 struct thread *td = curthread;
582 struct sockaddr *sa = NULL;
586 error = getsockaddr(&sa, uap->to, uap->tolen);
590 aiov.iov_base = uap->buf;
591 aiov.iov_len = uap->len;
592 auio.uio_iov = &aiov;
595 auio.uio_resid = uap->len;
596 auio.uio_segflg = UIO_USERSPACE;
597 auio.uio_rw = UIO_WRITE;
600 error = kern_sendmsg(uap->s, sa, &auio, NULL, uap->flags,
601 &uap->sysmsg_result);
609 * sendmsg_args(int s, caddr_t msg, int flags)
612 sendmsg(struct sendmsg_args *uap)
614 struct thread *td = curthread;
617 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
618 struct sockaddr *sa = NULL;
619 struct mbuf *control = NULL;
622 error = copyin(uap->msg, (caddr_t)&msg, sizeof(msg));
627 * Conditionally copyin msg.msg_name.
630 error = getsockaddr(&sa, msg.msg_name, msg.msg_namelen);
638 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
643 auio.uio_iovcnt = msg.msg_iovlen;
645 auio.uio_segflg = UIO_USERSPACE;
646 auio.uio_rw = UIO_WRITE;
650 * Conditionally copyin msg.msg_control.
652 if (msg.msg_control) {
653 if (msg.msg_controllen < sizeof(struct cmsghdr) ||
654 msg.msg_controllen > MLEN) {
658 control = m_get(M_WAIT, MT_CONTROL);
659 if (control == NULL) {
663 control->m_len = msg.msg_controllen;
664 error = copyin(msg.msg_control, mtod(control, caddr_t),
672 error = kern_sendmsg(uap->s, sa, &auio, control, uap->flags,
673 &uap->sysmsg_result);
678 iovec_free(&iov, aiov);
683 * kern_recvmsg() takes a handle to sa and control. If the handle is non-
684 * null, it returns a dynamically allocated struct sockaddr and an mbuf.
685 * Don't forget to FREE() and m_free() these if they are returned.
688 kern_recvmsg(int s, struct sockaddr **sa, struct uio *auio,
689 struct mbuf **control, int *flags, int *res)
691 struct thread *td = curthread;
692 struct proc *p = td->td_proc;
697 struct iovec *ktriov = NULL;
701 error = holdsock(p->p_fd, s, &fp);
705 if (KTRPOINT(td, KTR_GENIO)) {
706 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
708 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
709 bcopy(auio->uio_iov, ktriov, iovlen);
713 len = auio->uio_resid;
714 so = (struct socket *)fp->f_data;
715 error = so->so_proto->pr_usrreqs->pru_soreceive(so, sa, auio, NULL,
718 if (auio->uio_resid != len && (error == ERESTART ||
719 error == EINTR || error == EWOULDBLOCK))
723 if (ktriov != NULL) {
725 ktruio.uio_iov = ktriov;
726 ktruio.uio_resid = len - auio->uio_resid;
727 ktrgenio(p->p_tracep, s, UIO_READ, &ktruio, error);
729 FREE(ktriov, M_TEMP);
733 *res = len - auio->uio_resid;
739 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
740 * caddr_t from, int *fromlenaddr)
743 recvfrom(struct recvfrom_args *uap)
745 struct thread *td = curthread;
748 struct sockaddr *sa = NULL;
751 if (uap->from && uap->fromlenaddr) {
752 error = copyin(uap->fromlenaddr, &fromlen, sizeof(fromlen));
760 aiov.iov_base = uap->buf;
761 aiov.iov_len = uap->len;
762 auio.uio_iov = &aiov;
765 auio.uio_resid = uap->len;
766 auio.uio_segflg = UIO_USERSPACE;
767 auio.uio_rw = UIO_READ;
770 error = kern_recvmsg(uap->s, uap->from ? &sa : NULL, &auio, NULL,
771 &uap->flags, &uap->sysmsg_result);
773 if (error == 0 && uap->from) {
774 fromlen = MIN(fromlen, sa->sa_len);
775 error = copyout(sa, uap->from, fromlen);
777 error = copyout(&fromlen, uap->fromlenaddr,
787 * recvmsg_args(int s, struct msghdr *msg, int flags)
790 recvmsg(struct recvmsg_args *uap)
792 struct thread *td = curthread;
795 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
796 struct mbuf *m, *control = NULL;
797 struct sockaddr *sa = NULL;
799 socklen_t *ufromlenp, *ucontrollenp;
800 int error, fromlen, controllen, len, flags, *uflagsp;
803 * This copyin handles everything except the iovec.
805 error = copyin(uap->msg, &msg, sizeof(msg));
809 if (msg.msg_name && msg.msg_namelen < 0)
811 if (msg.msg_control && msg.msg_controllen < 0)
814 ufromlenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
816 ucontrollenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
818 uflagsp = (int *)((caddr_t)uap->msg + offsetof(struct msghdr,
824 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
829 auio.uio_iovcnt = msg.msg_iovlen;
831 auio.uio_segflg = UIO_USERSPACE;
832 auio.uio_rw = UIO_READ;
835 flags = msg.msg_flags;
837 error = kern_recvmsg(uap->s, msg.msg_name ? &sa : NULL, &auio,
838 msg.msg_control ? &control : NULL, &flags, &uap->sysmsg_result);
841 * Conditionally copyout the name and populate the namelen field.
843 if (error == 0 && msg.msg_name) {
844 fromlen = MIN(msg.msg_namelen, sa->sa_len);
845 error = copyout(sa, msg.msg_name, fromlen);
847 error = copyout(&fromlen, ufromlenp,
852 * Copyout msg.msg_control and msg.msg_controllen.
854 if (error == 0 && msg.msg_control) {
855 len = msg.msg_controllen;
857 ctlbuf = (caddr_t)msg.msg_control;
859 while(m && len > 0) {
862 if (len >= m->m_len) {
865 msg.msg_flags |= MSG_CTRUNC;
869 error = copyout(mtod(m, caddr_t), ctlbuf, tocopy);
877 controllen = ctlbuf - (caddr_t)msg.msg_control;
878 error = copyout(&controllen, ucontrollenp,
879 sizeof(*ucontrollenp));
883 error = copyout(&flags, uflagsp, sizeof(*uflagsp));
888 iovec_free(&iov, aiov);
895 * shutdown_args(int s, int how)
898 kern_shutdown(int s, int how)
900 struct thread *td = curthread;
901 struct proc *p = td->td_proc;
906 error = holdsock(p->p_fd, s, &fp);
909 error = soshutdown((struct socket *)fp->f_data, how);
915 shutdown(struct shutdown_args *uap)
919 error = kern_shutdown(uap->s, uap->how);
925 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
926 * in kernel pointer instead of a userland pointer. This allows us
927 * to manipulate socket options in the emulation code.
930 kern_setsockopt(int s, struct sockopt *sopt)
932 struct thread *td = curthread;
933 struct proc *p = td->td_proc;
937 if (sopt->sopt_val == 0 && sopt->sopt_valsize != 0)
939 if (sopt->sopt_valsize < 0)
942 error = holdsock(p->p_fd, s, &fp);
946 error = sosetopt((struct socket *)fp->f_data, sopt);
952 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
955 setsockopt(struct setsockopt_args *uap)
957 struct thread *td = curthread;
961 sopt.sopt_dir = SOPT_SET;
962 sopt.sopt_level = uap->level;
963 sopt.sopt_name = uap->name;
964 sopt.sopt_val = uap->val;
965 sopt.sopt_valsize = uap->valsize;
968 error = kern_setsockopt(uap->s, &sopt);
973 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
974 * in kernel pointer instead of a userland pointer. This allows us
975 * to manipulate socket options in the emulation code.
978 kern_getsockopt(int s, struct sockopt *sopt)
980 struct thread *td = curthread;
981 struct proc *p = td->td_proc;
985 if (sopt->sopt_val == 0 && sopt->sopt_valsize != 0)
987 if (sopt->sopt_valsize < 0)
990 error = holdsock(p->p_fd, s, &fp);
994 error = sogetopt((struct socket *)fp->f_data, sopt);
1000 * getsockopt_Args(int s, int level, int name, caddr_t val, int *avalsize)
1003 getsockopt(struct getsockopt_args *uap)
1005 struct thread *td = curthread;
1006 struct sockopt sopt;
1010 error = copyin(uap->avalsize, &valsize, sizeof(valsize));
1019 sopt.sopt_dir = SOPT_GET;
1020 sopt.sopt_level = uap->level;
1021 sopt.sopt_name = uap->name;
1022 sopt.sopt_val = uap->val;
1023 sopt.sopt_valsize = valsize;
1026 error = kern_getsockopt(uap->s, &sopt);
1028 valsize = sopt.sopt_valsize;
1029 error = copyout(&valsize, uap->avalsize, sizeof(valsize));
1035 * The second argument to kern_getsockname() is a handle to a struct sockaddr.
1036 * This allows kern_getsockname() to return a pointer to an allocated struct
1037 * sockaddr which must be freed later with FREE(). The caller must
1038 * initialize *name to NULL.
1041 kern_getsockname(int s, struct sockaddr **name, int *namelen)
1043 struct thread *td = curthread;
1044 struct proc *p = td->td_proc;
1047 struct sockaddr *sa = NULL;
1050 error = holdsock(p->p_fd, s, &fp);
1057 so = (struct socket *)fp->f_data;
1058 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
1063 *namelen = MIN(*namelen, sa->sa_len);
1073 * getsockname_args(int fdes, caddr_t asa, int *alen)
1078 getsockname(struct getsockname_args *uap)
1080 struct sockaddr *sa = NULL;
1083 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1087 error = kern_getsockname(uap->fdes, &sa, &sa_len);
1090 error = copyout(sa, uap->asa, sa_len);
1092 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1099 * The second argument to kern_getpeername() is a handle to a struct sockaddr.
1100 * This allows kern_getpeername() to return a pointer to an allocated struct
1101 * sockaddr which must be freed later with FREE(). The caller must
1102 * initialize *name to NULL.
1105 kern_getpeername(int s, struct sockaddr **name, int *namelen)
1107 struct thread *td = curthread;
1108 struct proc *p = td->td_proc;
1111 struct sockaddr *sa = NULL;
1114 error = holdsock(p->p_fd, s, &fp);
1121 so = (struct socket *)fp->f_data;
1122 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1126 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa);
1131 *namelen = MIN(*namelen, sa->sa_len);
1141 * getpeername_args(int fdes, caddr_t asa, int *alen)
1143 * Get name of peer for connected socket.
1146 getpeername(struct getpeername_args *uap)
1148 struct sockaddr *sa = NULL;
1151 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1155 error = kern_getpeername(uap->fdes, &sa, &sa_len);
1158 error = copyout(sa, uap->asa, sa_len);
1160 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1167 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1169 struct sockaddr *sa;
1173 if (len > SOCK_MAXADDRLEN)
1174 return ENAMETOOLONG;
1175 if (len < offsetof(struct sockaddr, sa_data[0]))
1177 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1178 error = copyin(uaddr, sa, len);
1182 #if BYTE_ORDER != BIG_ENDIAN
1184 * The bind(), connect(), and sendto() syscalls were not
1185 * versioned for COMPAT_43. Thus, this check must stay.
1187 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1188 sa->sa_family = sa->sa_len;
1197 * holdsock() - load the struct file pointer associated
1198 * with a socket into *fpp. If an error occurs, non-zero
1199 * will be returned and *fpp will be set to NULL.
1202 holdsock(fdp, fdes, fpp)
1203 struct filedesc *fdp;
1207 struct file *fp = NULL;
1210 if ((unsigned)fdes >= fdp->fd_nfiles ||
1211 (fp = fdp->fd_ofiles[fdes]) == NULL) {
1213 } else if (fp->f_type != DTYPE_SOCKET) {
1224 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1227 sf_buf_init(void *arg)
1231 SLIST_INIT(&sf_freelist);
1232 sf_base = kmem_alloc_pageable(kernel_map, nsfbufs * PAGE_SIZE);
1233 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, M_NOWAIT);
1234 bzero(sf_bufs, nsfbufs * sizeof(struct sf_buf));
1235 for (i = 0; i < nsfbufs; i++) {
1236 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
1237 SLIST_INSERT_HEAD(&sf_freelist, &sf_bufs[i], free_list);
1242 * Get an sf_buf from the freelist. Will block if none are available.
1252 while ((sf = SLIST_FIRST(&sf_freelist)) == NULL) {
1253 sf_buf_alloc_want = 1;
1254 error = tsleep(&sf_freelist, PCATCH, "sfbufa", 0);
1259 SLIST_REMOVE_HEAD(&sf_freelist, free_list);
1266 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1268 sf_buf_ref(caddr_t addr, u_int size)
1273 if (sf->refcnt == 0)
1274 panic("sf_buf_ref: referencing a free sf_buf");
1279 * Lose a reference to an sf_buf. When none left, detach mapped page
1280 * and release resources back to the system.
1282 * Must be called at splimp.
1285 sf_buf_free(caddr_t addr, u_int size)
1292 if (sf->refcnt == 0)
1293 panic("sf_buf_free: freeing free sf_buf");
1295 if (sf->refcnt == 0) {
1296 pmap_qremove((vm_offset_t)addr, 1);
1299 vm_page_unwire(m, 0);
1301 * Check for the object going away on us. This can
1302 * happen since we don't hold a reference to it.
1303 * If so, we're responsible for freeing the page.
1305 if (m->wire_count == 0 && m->object == NULL)
1309 SLIST_INSERT_HEAD(&sf_freelist, sf, free_list);
1310 if (sf_buf_alloc_want) {
1311 sf_buf_alloc_want = 0;
1312 wakeup(&sf_freelist);
1319 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1320 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1322 * Send a file specified by 'fd' and starting at 'offset' to a socket
1323 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1324 * nbytes == 0. Optionally add a header and/or trailer to the socket
1325 * output. If specified, write the total number of bytes sent into *sbytes.
1327 * In FreeBSD kern/uipc_syscalls.c,v 1.103, a bug was fixed that caused
1328 * the headers to count against the remaining bytes to be sent from
1329 * the file descriptor. We may wish to implement a compatibility syscall
1333 sendfile(struct sendfile_args *uap)
1335 struct thread *td = curthread;
1336 struct proc *p = td->td_proc;
1338 struct filedesc *fdp;
1339 struct vnode *vp = NULL;
1340 struct sf_hdtr hdtr;
1341 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
1343 off_t hdtr_size = 0, sbytes;
1350 * Do argument checking. Must be a regular file in, stream
1351 * type and connected socket out, positive offset.
1353 fp = holdfp(fdp, uap->fd, FREAD);
1357 if (fp->f_type != DTYPE_VNODE) {
1361 vp = (struct vnode *)fp->f_data;
1366 * If specified, get the pointer to the sf_hdtr struct for
1367 * any headers/trailers.
1370 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1377 error = iovec_copyin(hdtr.headers, &iov, aiov,
1378 hdtr.hdr_cnt, &auio.uio_resid);
1382 auio.uio_iovcnt = hdtr.hdr_cnt;
1383 auio.uio_offset = 0;
1384 auio.uio_segflg = UIO_USERSPACE;
1385 auio.uio_rw = UIO_WRITE;
1388 error = kern_sendmsg(uap->s, NULL, &auio, NULL, 0,
1391 iovec_free(&iov, aiov);
1398 error = kern_sendfile(vp, uap->s, uap->offset, uap->nbytes,
1399 &sbytes, uap->flags);
1404 * Send trailers. Wimp out and use writev(2).
1406 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1407 error = iovec_copyin(hdtr.trailers, &iov, aiov,
1408 hdtr.trl_cnt, &auio.uio_resid);
1412 auio.uio_iovcnt = hdtr.trl_cnt;
1413 auio.uio_offset = 0;
1414 auio.uio_segflg = UIO_USERSPACE;
1415 auio.uio_rw = UIO_WRITE;
1418 error = kern_sendmsg(uap->s, NULL, &auio, NULL, 0, &res);
1420 iovec_free(&iov, aiov);
1427 if (uap->sbytes != NULL) {
1428 sbytes += hdtr_size;
1429 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1437 kern_sendfile(struct vnode *vp, int s, off_t offset, size_t nbytes,
1438 off_t *sbytes, int flags)
1440 struct thread *td = curthread;
1441 struct proc *p = td->td_proc;
1442 struct vm_object *obj;
1451 if (vp->v_type != VREG || VOP_GETVOBJECT(vp, &obj) != 0) {
1455 error = holdsock(p->p_fd, s, &fp);
1458 so = (struct socket *)fp->f_data;
1459 if (so->so_type != SOCK_STREAM) {
1463 if ((so->so_state & SS_ISCONNECTED) == 0) {
1474 * Protect against multiple writers to the socket.
1476 (void) sblock(&so->so_snd, M_WAITOK);
1479 * Loop through the pages in the file, starting with the requested
1480 * offset. Get a file page (do I/O if necessary), map the file page
1481 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1484 for (off = offset; ; off += xfsize, *sbytes += xfsize) {
1488 pindex = OFF_TO_IDX(off);
1491 * Calculate the amount to transfer. Not to exceed a page,
1492 * the EOF, or the passed in nbytes.
1494 xfsize = obj->un_pager.vnp.vnp_size - off;
1495 if (xfsize > PAGE_SIZE)
1497 pgoff = (vm_offset_t)(off & PAGE_MASK);
1498 if (PAGE_SIZE - pgoff < xfsize)
1499 xfsize = PAGE_SIZE - pgoff;
1500 if (nbytes && xfsize > (nbytes - *sbytes))
1501 xfsize = nbytes - *sbytes;
1505 * Optimize the non-blocking case by looking at the socket space
1506 * before going to the extra work of constituting the sf_buf.
1508 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1509 if (so->so_state & SS_CANTSENDMORE)
1513 sbunlock(&so->so_snd);
1517 * Attempt to look up the page.
1519 * Allocate if not found
1521 * Wait and loop if busy.
1523 pg = vm_page_lookup(obj, pindex);
1526 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL);
1532 } else if (vm_page_sleep_busy(pg, TRUE, "sfpbsy")) {
1537 * Wire the page so it does not get ripped out from under
1544 * If page is not valid for what we need, initiate I/O
1547 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1553 * Ensure that our page is still around when the I/O
1556 vm_page_io_start(pg);
1559 * Get the page from backing store.
1561 bsize = vp->v_mount->mnt_stat.f_iosize;
1562 auio.uio_iov = &aiov;
1563 auio.uio_iovcnt = 1;
1565 aiov.iov_len = MAXBSIZE;
1566 auio.uio_resid = MAXBSIZE;
1567 auio.uio_offset = trunc_page(off);
1568 auio.uio_segflg = UIO_NOCOPY;
1569 auio.uio_rw = UIO_READ;
1571 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
1572 error = VOP_READ(vp, &auio,
1573 IO_VMIO | ((MAXBSIZE / bsize) << 16),
1575 VOP_UNLOCK(vp, 0, td);
1576 vm_page_flag_clear(pg, PG_ZERO);
1577 vm_page_io_finish(pg);
1579 vm_page_unwire(pg, 0);
1581 * See if anyone else might know about this page.
1582 * If not and it is not valid, then free it.
1584 if (pg->wire_count == 0 && pg->valid == 0 &&
1585 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1586 pg->hold_count == 0) {
1590 sbunlock(&so->so_snd);
1597 * Get a sendfile buf. We usually wait as long as necessary,
1598 * but this wait can be interrupted.
1600 if ((sf = sf_buf_alloc()) == NULL) {
1602 vm_page_unwire(pg, 0);
1603 if (pg->wire_count == 0 && pg->object == NULL)
1606 sbunlock(&so->so_snd);
1613 * Allocate a kernel virtual page and insert the physical page
1618 pmap_qenter(sf->kva, &pg, 1);
1620 * Get an mbuf header and set it up as having external storage.
1622 MGETHDR(m, M_WAIT, MT_DATA);
1625 sf_buf_free((void *)sf->kva, PAGE_SIZE);
1626 sbunlock(&so->so_snd);
1629 m->m_ext.ext_free = sf_buf_free;
1630 m->m_ext.ext_ref = sf_buf_ref;
1631 m->m_ext.ext_buf = (void *)sf->kva;
1632 m->m_ext.ext_size = PAGE_SIZE;
1633 m->m_data = (char *) sf->kva + pgoff;
1634 m->m_flags |= M_EXT;
1635 m->m_pkthdr.len = m->m_len = xfsize;
1637 * Add the buffer to the socket buffer chain.
1642 * Make sure that the socket is still able to take more data.
1643 * CANTSENDMORE being true usually means that the connection
1644 * was closed. so_error is true when an error was sensed after
1646 * The state is checked after the page mapping and buffer
1647 * allocation above since those operations may block and make
1648 * any socket checks stale. From this point forward, nothing
1649 * blocks before the pru_send (or more accurately, any blocking
1650 * results in a loop back to here to re-check).
1652 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1653 if (so->so_state & SS_CANTSENDMORE) {
1656 error = so->so_error;
1660 sbunlock(&so->so_snd);
1665 * Wait for socket space to become available. We do this just
1666 * after checking the connection state above in order to avoid
1667 * a race condition with sbwait().
1669 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
1670 if (so->so_state & SS_NBIO) {
1672 sbunlock(&so->so_snd);
1677 error = sbwait(&so->so_snd);
1679 * An error from sbwait usually indicates that we've
1680 * been interrupted by a signal. If we've sent anything
1681 * then return bytes sent, otherwise return the error.
1685 sbunlock(&so->so_snd);
1692 (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, td);
1695 sbunlock(&so->so_snd);
1699 sbunlock(&so->so_snd);