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.20 2003/10/15 08:43:37 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);
537 if (auio->uio_resid < 0) {
542 if (KTRPOINT(td, KTR_GENIO)) {
543 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
545 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
546 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
550 len = auio->uio_resid;
551 so = (struct socket *)fp->f_data;
552 error = so->so_proto->pr_usrreqs->pru_sosend(so, sa, auio, NULL,
555 if (auio->uio_resid != len && (error == ERESTART ||
556 error == EINTR || error == EWOULDBLOCK))
562 if (ktriov != NULL) {
564 ktruio.uio_iov = ktriov;
565 ktruio.uio_resid = len - auio->uio_resid;
566 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error);
568 FREE(ktriov, M_TEMP);
572 *res = len - auio->uio_resid;
579 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
582 sendto(struct sendto_args *uap)
584 struct thread *td = curthread;
587 struct sockaddr *sa = NULL;
591 error = getsockaddr(&sa, uap->to, uap->tolen);
595 aiov.iov_base = uap->buf;
596 aiov.iov_len = uap->len;
597 auio.uio_iov = &aiov;
600 auio.uio_resid = uap->len;
601 auio.uio_segflg = UIO_USERSPACE;
602 auio.uio_rw = UIO_WRITE;
605 error = kern_sendmsg(uap->s, sa, &auio, NULL, uap->flags,
606 &uap->sysmsg_result);
614 * sendmsg_args(int s, caddr_t msg, int flags)
617 sendmsg(struct sendmsg_args *uap)
619 struct thread *td = curthread;
622 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
623 struct sockaddr *sa = NULL;
624 struct mbuf *control = NULL;
627 error = copyin(uap->msg, (caddr_t)&msg, sizeof(msg));
632 * Conditionally copyin msg.msg_name.
635 error = getsockaddr(&sa, msg.msg_name, msg.msg_namelen);
643 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
648 auio.uio_iovcnt = msg.msg_iovlen;
650 auio.uio_segflg = UIO_USERSPACE;
651 auio.uio_rw = UIO_WRITE;
655 * Conditionally copyin msg.msg_control.
657 if (msg.msg_control) {
658 if (msg.msg_controllen < sizeof(struct cmsghdr) ||
659 msg.msg_controllen > MLEN) {
663 control = m_get(M_WAIT, MT_CONTROL);
664 if (control == NULL) {
668 control->m_len = msg.msg_controllen;
669 error = copyin(msg.msg_control, mtod(control, caddr_t),
677 error = kern_sendmsg(uap->s, sa, &auio, control, uap->flags,
678 &uap->sysmsg_result);
683 iovec_free(&iov, aiov);
688 * kern_recvmsg() takes a handle to sa and control. If the handle is non-
689 * null, it returns a dynamically allocated struct sockaddr and an mbuf.
690 * Don't forget to FREE() and m_free() these if they are returned.
693 kern_recvmsg(int s, struct sockaddr **sa, struct uio *auio,
694 struct mbuf **control, int *flags, int *res)
696 struct thread *td = curthread;
697 struct proc *p = td->td_proc;
702 struct iovec *ktriov = NULL;
706 error = holdsock(p->p_fd, s, &fp);
709 if (auio->uio_resid < 0) {
714 if (KTRPOINT(td, KTR_GENIO)) {
715 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
717 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
718 bcopy(auio->uio_iov, ktriov, iovlen);
722 len = auio->uio_resid;
723 so = (struct socket *)fp->f_data;
724 error = so->so_proto->pr_usrreqs->pru_soreceive(so, sa, auio, NULL,
727 if (auio->uio_resid != len && (error == ERESTART ||
728 error == EINTR || error == EWOULDBLOCK))
732 if (ktriov != NULL) {
734 ktruio.uio_iov = ktriov;
735 ktruio.uio_resid = len - auio->uio_resid;
736 ktrgenio(p->p_tracep, s, UIO_READ, &ktruio, error);
738 FREE(ktriov, M_TEMP);
742 *res = len - auio->uio_resid;
749 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
750 * caddr_t from, int *fromlenaddr)
753 recvfrom(struct recvfrom_args *uap)
755 struct thread *td = curthread;
758 struct sockaddr *sa = NULL;
761 if (uap->from && uap->fromlenaddr) {
762 error = copyin(uap->fromlenaddr, &fromlen, sizeof(fromlen));
770 aiov.iov_base = uap->buf;
771 aiov.iov_len = uap->len;
772 auio.uio_iov = &aiov;
775 auio.uio_resid = uap->len;
776 auio.uio_segflg = UIO_USERSPACE;
777 auio.uio_rw = UIO_READ;
780 error = kern_recvmsg(uap->s, uap->from ? &sa : NULL, &auio, NULL,
781 &uap->flags, &uap->sysmsg_result);
783 if (error == 0 && uap->from) {
784 fromlen = MIN(fromlen, sa->sa_len);
785 error = copyout(sa, uap->from, fromlen);
787 error = copyout(&fromlen, uap->fromlenaddr,
797 * recvmsg_args(int s, struct msghdr *msg, int flags)
800 recvmsg(struct recvmsg_args *uap)
802 struct thread *td = curthread;
805 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
806 struct mbuf *m, *control = NULL;
807 struct sockaddr *sa = NULL;
809 socklen_t *ufromlenp, *ucontrollenp;
810 int error, fromlen, controllen, len, flags, *uflagsp;
813 * This copyin handles everything except the iovec.
815 error = copyin(uap->msg, &msg, sizeof(msg));
819 if (msg.msg_name && msg.msg_namelen < 0)
821 if (msg.msg_control && msg.msg_controllen < 0)
824 ufromlenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
826 ucontrollenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
828 uflagsp = (int *)((caddr_t)uap->msg + offsetof(struct msghdr,
834 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
839 auio.uio_iovcnt = msg.msg_iovlen;
841 auio.uio_segflg = UIO_USERSPACE;
842 auio.uio_rw = UIO_READ;
845 flags = msg.msg_flags;
847 error = kern_recvmsg(uap->s, msg.msg_name ? &sa : NULL, &auio,
848 msg.msg_control ? &control : NULL, &flags, &uap->sysmsg_result);
851 * Conditionally copyout the name and populate the namelen field.
853 if (error == 0 && msg.msg_name) {
854 fromlen = MIN(msg.msg_namelen, sa->sa_len);
855 error = copyout(sa, msg.msg_name, fromlen);
857 error = copyout(&fromlen, ufromlenp,
862 * Copyout msg.msg_control and msg.msg_controllen.
864 if (error == 0 && msg.msg_control) {
865 len = msg.msg_controllen;
867 ctlbuf = (caddr_t)msg.msg_control;
869 while(m && len > 0) {
872 if (len >= m->m_len) {
875 msg.msg_flags |= MSG_CTRUNC;
879 error = copyout(mtod(m, caddr_t), ctlbuf, tocopy);
887 controllen = ctlbuf - (caddr_t)msg.msg_control;
888 error = copyout(&controllen, ucontrollenp,
889 sizeof(*ucontrollenp));
893 error = copyout(&flags, uflagsp, sizeof(*uflagsp));
898 iovec_free(&iov, aiov);
905 * shutdown_args(int s, int how)
908 kern_shutdown(int s, int how)
910 struct thread *td = curthread;
911 struct proc *p = td->td_proc;
916 error = holdsock(p->p_fd, s, &fp);
919 error = soshutdown((struct socket *)fp->f_data, how);
925 shutdown(struct shutdown_args *uap)
929 error = kern_shutdown(uap->s, uap->how);
935 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
936 * in kernel pointer instead of a userland pointer. This allows us
937 * to manipulate socket options in the emulation code.
940 kern_setsockopt(int s, struct sockopt *sopt)
942 struct thread *td = curthread;
943 struct proc *p = td->td_proc;
947 if (sopt->sopt_val == 0 && sopt->sopt_valsize != 0)
949 if (sopt->sopt_valsize < 0)
952 error = holdsock(p->p_fd, s, &fp);
956 error = sosetopt((struct socket *)fp->f_data, sopt);
962 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
965 setsockopt(struct setsockopt_args *uap)
967 struct thread *td = curthread;
971 sopt.sopt_dir = SOPT_SET;
972 sopt.sopt_level = uap->level;
973 sopt.sopt_name = uap->name;
974 sopt.sopt_val = uap->val;
975 sopt.sopt_valsize = uap->valsize;
978 error = kern_setsockopt(uap->s, &sopt);
983 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
984 * in kernel pointer instead of a userland pointer. This allows us
985 * to manipulate socket options in the emulation code.
988 kern_getsockopt(int s, struct sockopt *sopt)
990 struct thread *td = curthread;
991 struct proc *p = td->td_proc;
995 if (sopt->sopt_val == 0 && sopt->sopt_valsize != 0)
997 if (sopt->sopt_valsize < 0)
1000 error = holdsock(p->p_fd, s, &fp);
1004 error = sogetopt((struct socket *)fp->f_data, sopt);
1010 * getsockopt_Args(int s, int level, int name, caddr_t val, int *avalsize)
1013 getsockopt(struct getsockopt_args *uap)
1015 struct thread *td = curthread;
1016 struct sockopt sopt;
1020 error = copyin(uap->avalsize, &valsize, sizeof(valsize));
1029 sopt.sopt_dir = SOPT_GET;
1030 sopt.sopt_level = uap->level;
1031 sopt.sopt_name = uap->name;
1032 sopt.sopt_val = uap->val;
1033 sopt.sopt_valsize = valsize;
1036 error = kern_getsockopt(uap->s, &sopt);
1038 valsize = sopt.sopt_valsize;
1039 error = copyout(&valsize, uap->avalsize, sizeof(valsize));
1045 * The second argument to kern_getsockname() is a handle to a struct sockaddr.
1046 * This allows kern_getsockname() to return a pointer to an allocated struct
1047 * sockaddr which must be freed later with FREE(). The caller must
1048 * initialize *name to NULL.
1051 kern_getsockname(int s, struct sockaddr **name, int *namelen)
1053 struct thread *td = curthread;
1054 struct proc *p = td->td_proc;
1057 struct sockaddr *sa = NULL;
1060 error = holdsock(p->p_fd, s, &fp);
1067 so = (struct socket *)fp->f_data;
1068 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
1073 *namelen = MIN(*namelen, sa->sa_len);
1083 * getsockname_args(int fdes, caddr_t asa, int *alen)
1088 getsockname(struct getsockname_args *uap)
1090 struct sockaddr *sa = NULL;
1093 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1097 error = kern_getsockname(uap->fdes, &sa, &sa_len);
1100 error = copyout(sa, uap->asa, sa_len);
1102 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1109 * The second argument to kern_getpeername() is a handle to a struct sockaddr.
1110 * This allows kern_getpeername() to return a pointer to an allocated struct
1111 * sockaddr which must be freed later with FREE(). The caller must
1112 * initialize *name to NULL.
1115 kern_getpeername(int s, struct sockaddr **name, int *namelen)
1117 struct thread *td = curthread;
1118 struct proc *p = td->td_proc;
1121 struct sockaddr *sa = NULL;
1124 error = holdsock(p->p_fd, s, &fp);
1131 so = (struct socket *)fp->f_data;
1132 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1136 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa);
1141 *namelen = MIN(*namelen, sa->sa_len);
1151 * getpeername_args(int fdes, caddr_t asa, int *alen)
1153 * Get name of peer for connected socket.
1156 getpeername(struct getpeername_args *uap)
1158 struct sockaddr *sa = NULL;
1161 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1165 error = kern_getpeername(uap->fdes, &sa, &sa_len);
1168 error = copyout(sa, uap->asa, sa_len);
1170 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1177 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1179 struct sockaddr *sa;
1183 if (len > SOCK_MAXADDRLEN)
1184 return ENAMETOOLONG;
1185 if (len < offsetof(struct sockaddr, sa_data[0]))
1187 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1188 error = copyin(uaddr, sa, len);
1192 #if BYTE_ORDER != BIG_ENDIAN
1194 * The bind(), connect(), and sendto() syscalls were not
1195 * versioned for COMPAT_43. Thus, this check must stay.
1197 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1198 sa->sa_family = sa->sa_len;
1207 * holdsock() - load the struct file pointer associated
1208 * with a socket into *fpp. If an error occurs, non-zero
1209 * will be returned and *fpp will be set to NULL.
1212 holdsock(fdp, fdes, fpp)
1213 struct filedesc *fdp;
1217 struct file *fp = NULL;
1220 if ((unsigned)fdes >= fdp->fd_nfiles ||
1221 (fp = fdp->fd_ofiles[fdes]) == NULL) {
1223 } else if (fp->f_type != DTYPE_SOCKET) {
1234 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1237 sf_buf_init(void *arg)
1241 SLIST_INIT(&sf_freelist);
1242 sf_base = kmem_alloc_pageable(kernel_map, nsfbufs * PAGE_SIZE);
1243 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, M_NOWAIT);
1244 bzero(sf_bufs, nsfbufs * sizeof(struct sf_buf));
1245 for (i = 0; i < nsfbufs; i++) {
1246 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
1247 SLIST_INSERT_HEAD(&sf_freelist, &sf_bufs[i], free_list);
1252 * Get an sf_buf from the freelist. Will block if none are available.
1262 while ((sf = SLIST_FIRST(&sf_freelist)) == NULL) {
1263 sf_buf_alloc_want = 1;
1264 error = tsleep(&sf_freelist, PCATCH, "sfbufa", 0);
1269 SLIST_REMOVE_HEAD(&sf_freelist, free_list);
1276 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1278 sf_buf_ref(caddr_t addr, u_int size)
1283 if (sf->refcnt == 0)
1284 panic("sf_buf_ref: referencing a free sf_buf");
1289 * Lose a reference to an sf_buf. When none left, detach mapped page
1290 * and release resources back to the system.
1292 * Must be called at splimp.
1295 sf_buf_free(caddr_t addr, u_int size)
1302 if (sf->refcnt == 0)
1303 panic("sf_buf_free: freeing free sf_buf");
1305 if (sf->refcnt == 0) {
1306 pmap_qremove((vm_offset_t)addr, 1);
1309 vm_page_unwire(m, 0);
1311 * Check for the object going away on us. This can
1312 * happen since we don't hold a reference to it.
1313 * If so, we're responsible for freeing the page.
1315 if (m->wire_count == 0 && m->object == NULL)
1319 SLIST_INSERT_HEAD(&sf_freelist, sf, free_list);
1320 if (sf_buf_alloc_want) {
1321 sf_buf_alloc_want = 0;
1322 wakeup(&sf_freelist);
1329 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1330 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1332 * Send a file specified by 'fd' and starting at 'offset' to a socket
1333 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1334 * nbytes == 0. Optionally add a header and/or trailer to the socket
1335 * output. If specified, write the total number of bytes sent into *sbytes.
1337 * In FreeBSD kern/uipc_syscalls.c,v 1.103, a bug was fixed that caused
1338 * the headers to count against the remaining bytes to be sent from
1339 * the file descriptor. We may wish to implement a compatibility syscall
1343 sendfile(struct sendfile_args *uap)
1345 struct thread *td = curthread;
1346 struct proc *p = td->td_proc;
1348 struct filedesc *fdp;
1349 struct vnode *vp = NULL;
1350 struct sf_hdtr hdtr;
1351 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
1353 off_t hdtr_size = 0, sbytes;
1360 * Do argument checking. Must be a regular file in, stream
1361 * type and connected socket out, positive offset.
1363 fp = holdfp(fdp, uap->fd, FREAD);
1367 if (fp->f_type != DTYPE_VNODE) {
1371 vp = (struct vnode *)fp->f_data;
1376 * If specified, get the pointer to the sf_hdtr struct for
1377 * any headers/trailers.
1380 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1387 error = iovec_copyin(hdtr.headers, &iov, aiov,
1388 hdtr.hdr_cnt, &auio.uio_resid);
1392 auio.uio_iovcnt = hdtr.hdr_cnt;
1393 auio.uio_offset = 0;
1394 auio.uio_segflg = UIO_USERSPACE;
1395 auio.uio_rw = UIO_WRITE;
1398 error = kern_sendmsg(uap->s, NULL, &auio, NULL, 0,
1401 iovec_free(&iov, aiov);
1408 error = kern_sendfile(vp, uap->s, uap->offset, uap->nbytes,
1409 &sbytes, uap->flags);
1414 * Send trailers. Wimp out and use writev(2).
1416 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1417 error = iovec_copyin(hdtr.trailers, &iov, aiov,
1418 hdtr.trl_cnt, &auio.uio_resid);
1422 auio.uio_iovcnt = hdtr.trl_cnt;
1423 auio.uio_offset = 0;
1424 auio.uio_segflg = UIO_USERSPACE;
1425 auio.uio_rw = UIO_WRITE;
1428 error = kern_sendmsg(uap->s, NULL, &auio, NULL, 0, &res);
1430 iovec_free(&iov, aiov);
1437 if (uap->sbytes != NULL) {
1438 sbytes += hdtr_size;
1439 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1447 kern_sendfile(struct vnode *vp, int s, off_t offset, size_t nbytes,
1448 off_t *sbytes, int flags)
1450 struct thread *td = curthread;
1451 struct proc *p = td->td_proc;
1452 struct vm_object *obj;
1461 if (vp->v_type != VREG || VOP_GETVOBJECT(vp, &obj) != 0) {
1465 error = holdsock(p->p_fd, s, &fp);
1468 so = (struct socket *)fp->f_data;
1469 if (so->so_type != SOCK_STREAM) {
1473 if ((so->so_state & SS_ISCONNECTED) == 0) {
1484 * Protect against multiple writers to the socket.
1486 (void) sblock(&so->so_snd, M_WAITOK);
1489 * Loop through the pages in the file, starting with the requested
1490 * offset. Get a file page (do I/O if necessary), map the file page
1491 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1494 for (off = offset; ; off += xfsize, *sbytes += xfsize) {
1498 pindex = OFF_TO_IDX(off);
1501 * Calculate the amount to transfer. Not to exceed a page,
1502 * the EOF, or the passed in nbytes.
1504 xfsize = obj->un_pager.vnp.vnp_size - off;
1505 if (xfsize > PAGE_SIZE)
1507 pgoff = (vm_offset_t)(off & PAGE_MASK);
1508 if (PAGE_SIZE - pgoff < xfsize)
1509 xfsize = PAGE_SIZE - pgoff;
1510 if (nbytes && xfsize > (nbytes - *sbytes))
1511 xfsize = nbytes - *sbytes;
1515 * Optimize the non-blocking case by looking at the socket space
1516 * before going to the extra work of constituting the sf_buf.
1518 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1519 if (so->so_state & SS_CANTSENDMORE)
1523 sbunlock(&so->so_snd);
1527 * Attempt to look up the page.
1529 * Allocate if not found
1531 * Wait and loop if busy.
1533 pg = vm_page_lookup(obj, pindex);
1536 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL);
1542 } else if (vm_page_sleep_busy(pg, TRUE, "sfpbsy")) {
1547 * Wire the page so it does not get ripped out from under
1554 * If page is not valid for what we need, initiate I/O
1557 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1563 * Ensure that our page is still around when the I/O
1566 vm_page_io_start(pg);
1569 * Get the page from backing store.
1571 bsize = vp->v_mount->mnt_stat.f_iosize;
1572 auio.uio_iov = &aiov;
1573 auio.uio_iovcnt = 1;
1575 aiov.iov_len = MAXBSIZE;
1576 auio.uio_resid = MAXBSIZE;
1577 auio.uio_offset = trunc_page(off);
1578 auio.uio_segflg = UIO_NOCOPY;
1579 auio.uio_rw = UIO_READ;
1581 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
1582 error = VOP_READ(vp, &auio,
1583 IO_VMIO | ((MAXBSIZE / bsize) << 16),
1585 VOP_UNLOCK(vp, 0, td);
1586 vm_page_flag_clear(pg, PG_ZERO);
1587 vm_page_io_finish(pg);
1589 vm_page_unwire(pg, 0);
1591 * See if anyone else might know about this page.
1592 * If not and it is not valid, then free it.
1594 if (pg->wire_count == 0 && pg->valid == 0 &&
1595 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1596 pg->hold_count == 0) {
1600 sbunlock(&so->so_snd);
1607 * Get a sendfile buf. We usually wait as long as necessary,
1608 * but this wait can be interrupted.
1610 if ((sf = sf_buf_alloc()) == NULL) {
1612 vm_page_unwire(pg, 0);
1613 if (pg->wire_count == 0 && pg->object == NULL)
1616 sbunlock(&so->so_snd);
1623 * Allocate a kernel virtual page and insert the physical page
1628 pmap_qenter(sf->kva, &pg, 1);
1630 * Get an mbuf header and set it up as having external storage.
1632 MGETHDR(m, M_WAIT, MT_DATA);
1635 sf_buf_free((void *)sf->kva, PAGE_SIZE);
1636 sbunlock(&so->so_snd);
1639 m->m_ext.ext_free = sf_buf_free;
1640 m->m_ext.ext_ref = sf_buf_ref;
1641 m->m_ext.ext_buf = (void *)sf->kva;
1642 m->m_ext.ext_size = PAGE_SIZE;
1643 m->m_data = (char *) sf->kva + pgoff;
1644 m->m_flags |= M_EXT;
1645 m->m_pkthdr.len = m->m_len = xfsize;
1647 * Add the buffer to the socket buffer chain.
1652 * Make sure that the socket is still able to take more data.
1653 * CANTSENDMORE being true usually means that the connection
1654 * was closed. so_error is true when an error was sensed after
1656 * The state is checked after the page mapping and buffer
1657 * allocation above since those operations may block and make
1658 * any socket checks stale. From this point forward, nothing
1659 * blocks before the pru_send (or more accurately, any blocking
1660 * results in a loop back to here to re-check).
1662 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1663 if (so->so_state & SS_CANTSENDMORE) {
1666 error = so->so_error;
1670 sbunlock(&so->so_snd);
1675 * Wait for socket space to become available. We do this just
1676 * after checking the connection state above in order to avoid
1677 * a race condition with sbwait().
1679 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
1680 if (so->so_state & SS_NBIO) {
1682 sbunlock(&so->so_snd);
1687 error = sbwait(&so->so_snd);
1689 * An error from sbwait usually indicates that we've
1690 * been interrupted by a signal. If we've sent anything
1691 * then return bytes sent, otherwise return the error.
1695 sbunlock(&so->so_snd);
1702 (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, td);
1705 sbunlock(&so->so_snd);
1709 sbunlock(&so->so_snd);