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.16 2003/09/29 05:34:08 daver Exp $
41 #include "opt_compat.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/sysproto.h>
48 #include <sys/malloc.h>
49 #include <sys/filedesc.h>
50 #include <sys/event.h>
52 #include <sys/fcntl.h>
54 #include <sys/filio.h>
55 #include <sys/kern_syscall.h>
57 #include <sys/protosw.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/signalvar.h>
62 #include <sys/vnode.h>
64 #include <sys/mount.h>
66 #include <sys/ktrace.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_page.h>
71 #include <vm/vm_pageout.h>
72 #include <vm/vm_kern.h>
73 #include <vm/vm_extern.h>
74 #include <sys/file2.h>
76 #if defined(COMPAT_43)
77 #include <emulation/43bsd/43bsd_socket.h>
78 #endif /* COMPAT_43 */
80 static void sf_buf_init(void *arg);
81 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
83 static int do_sendfile(struct sendfile_args *uap, int compat);
85 static SLIST_HEAD(, sf_buf) sf_freelist;
86 static vm_offset_t sf_base;
87 static struct sf_buf *sf_bufs;
88 static int sf_buf_alloc_want;
91 * System call interface to the socket abstraction.
93 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
94 #define COMPAT_OLDSOCK
97 extern struct fileops socketops;
100 * socket_args(int domain, int type, int protocol)
103 socket(struct socket_args *uap)
105 struct thread *td = curthread;
106 struct proc *p = td->td_proc;
107 struct filedesc *fdp;
115 error = falloc(p, &fp, &fd);
119 error = socreate(uap->domain, &so, uap->type, uap->protocol, td);
121 if (fdp->fd_ofiles[fd] == fp) {
122 fdp->fd_ofiles[fd] = NULL;
126 fp->f_data = (caddr_t)so;
127 fp->f_flag = FREAD|FWRITE;
128 fp->f_ops = &socketops;
129 fp->f_type = DTYPE_SOCKET;
130 uap->sysmsg_result = fd;
137 kern_bind(int s, struct sockaddr *sa)
139 struct thread *td = curthread;
140 struct proc *p = td->td_proc;
145 error = holdsock(p->p_fd, s, &fp);
148 error = sobind((struct socket *)fp->f_data, sa, td);
154 * bind_args(int s, caddr_t name, int namelen)
157 bind(struct bind_args *uap)
162 error = getsockaddr(&sa, uap->name, uap->namelen);
165 error = kern_bind(uap->s, sa);
172 kern_listen(int s, int backlog)
174 struct thread *td = curthread;
175 struct proc *p = td->td_proc;
180 error = holdsock(p->p_fd, s, &fp);
183 error = solisten((struct socket *)fp->f_data, backlog, td);
189 * listen_args(int s, int backlog)
192 listen(struct listen_args *uap)
196 error = kern_listen(uap->s, uap->backlog);
201 * The second argument to kern_accept() is a handle to a struct sockaddr.
202 * This allows kern_accept() to return a pointer to an allocated struct
203 * sockaddr which must be freed later with FREE(). The caller must
204 * initialize *name to NULL.
207 kern_accept(int s, struct sockaddr **name, int *namelen, int *res)
209 struct thread *td = curthread;
210 struct proc *p = td->td_proc;
211 struct filedesc *fdp = p->p_fd;
212 struct file *lfp = NULL;
213 struct file *nfp = NULL;
216 struct socket *head, *so;
218 u_int fflag; /* type must match fp->f_flag */
221 if (name && namelen && *namelen < 0)
224 error = holdsock(fdp, s, &lfp);
228 head = (struct socket *)lfp->f_data;
229 if ((head->so_options & SO_ACCEPTCONN) == 0) {
234 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
235 if (head->so_state & SS_CANTRCVMORE) {
236 head->so_error = ECONNABORTED;
239 if ((head->so_state & SS_NBIO) != 0) {
240 head->so_error = EWOULDBLOCK;
243 error = tsleep((caddr_t)&head->so_timeo, PCATCH, "accept", 0);
249 if (head->so_error) {
250 error = head->so_error;
257 * At this point we know that there is at least one connection
258 * ready to be accepted. Remove it from the queue prior to
259 * allocating the file descriptor for it since falloc() may
260 * block allowing another process to accept the connection
263 so = TAILQ_FIRST(&head->so_comp);
264 TAILQ_REMOVE(&head->so_comp, so, so_list);
268 error = falloc(p, &nfp, &fd);
271 * Probably ran out of file descriptors. Put the
272 * unaccepted connection back onto the queue and
273 * do another wakeup so some other process might
274 * have a chance at it.
276 TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
278 wakeup_one(&head->so_timeo);
285 /* connection has been removed from the listen queue */
286 KNOTE(&head->so_rcv.sb_sel.si_note, 0);
288 so->so_state &= ~SS_COMP;
290 if (head->so_sigio != NULL)
291 fsetown(fgetown(head->so_sigio), &so->so_sigio);
293 nfp->f_data = (caddr_t)so;
295 nfp->f_ops = &socketops;
296 nfp->f_type = DTYPE_SOCKET;
297 /* Sync socket nonblocking/async state with file flags */
298 tmp = fflag & FNONBLOCK;
299 (void) fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, td);
300 tmp = fflag & FASYNC;
301 (void) fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, td);
304 error = soaccept(so, &sa);
307 * Set the returned name and namelen as applicable. Set the returned
308 * namelen to 0 for older code which might ignore the return value
312 if (sa && name && namelen) {
313 if (*namelen > sa->sa_len)
314 *namelen = sa->sa_len;
323 * close the new descriptor, assuming someone hasn't ripped it
324 * out from under us. Note that *res is normally ignored if an
325 * error is returned but a syscall message will still have access
326 * to the result code.
330 if (fdp->fd_ofiles[fd] == nfp) {
331 fdp->fd_ofiles[fd] = NULL;
338 * Release explicitly held references before returning.
348 * accept_args(int s, caddr_t name, int *anamelen)
351 accept(struct accept_args *uap)
353 struct sockaddr *sa = NULL;
358 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
362 error = kern_accept(uap->s, &sa, &sa_len, &uap->sysmsg_result);
365 error = copyout(sa, uap->name, sa_len);
367 error = copyout(&sa_len, uap->anamelen,
368 sizeof(*uap->anamelen));
373 error = kern_accept(uap->s, NULL, 0, &uap->sysmsg_result);
379 kern_connect(int s, struct sockaddr *sa)
381 struct thread *td = curthread;
382 struct proc *p = td->td_proc;
387 error = holdsock(p->p_fd, s, &fp);
390 so = (struct socket *)fp->f_data;
391 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
395 error = soconnect(so, sa, td);
398 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
403 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
404 error = tsleep((caddr_t)&so->so_timeo, PCATCH, "connec", 0);
409 error = so->so_error;
414 so->so_state &= ~SS_ISCONNECTING;
415 if (error == ERESTART)
423 * connect_args(int s, caddr_t name, int namelen)
426 connect(struct connect_args *uap)
431 error = getsockaddr(&sa, uap->name, uap->namelen);
434 error = kern_connect(uap->s, sa);
441 kern_socketpair(int domain, int type, int protocol, int *sv)
443 struct thread *td = curthread;
444 struct proc *p = td->td_proc;
445 struct filedesc *fdp;
446 struct file *fp1, *fp2;
447 struct socket *so1, *so2;
452 error = socreate(domain, &so1, type, protocol, td);
455 error = socreate(domain, &so2, type, protocol, td);
458 error = falloc(p, &fp1, &fd);
463 fp1->f_data = (caddr_t)so1;
464 error = falloc(p, &fp2, &fd);
468 fp2->f_data = (caddr_t)so2;
470 error = soconnect2(so1, so2);
473 if (type == SOCK_DGRAM) {
475 * Datagram socket connection is asymmetric.
477 error = soconnect2(so2, so1);
481 fp1->f_flag = fp2->f_flag = FREAD|FWRITE;
482 fp1->f_ops = fp2->f_ops = &socketops;
483 fp1->f_type = fp2->f_type = DTYPE_SOCKET;
488 if (fdp->fd_ofiles[sv[1]] == fp2) {
489 fdp->fd_ofiles[sv[1]] = NULL;
494 if (fdp->fd_ofiles[sv[0]] == fp1) {
495 fdp->fd_ofiles[sv[0]] = NULL;
507 * socketpair(int domain, int type, int protocol, int *rsv)
510 socketpair(struct socketpair_args *uap)
514 error = kern_socketpair(uap->domain, uap->type, uap->protocol, sockv);
517 error = copyout(sockv, uap->rsv, sizeof(sockv));
522 * This function never touches mp->msg_namelen.
525 kern_sendmsg(int s, struct msghdr *mp, int *res)
527 struct thread *td = curthread;
528 struct proc *p = td->td_proc;
536 struct iovec *ktriov = NULL;
540 error = holdsock(p->p_fd, s, &fp);
543 auio.uio_iov = mp->msg_iov;
544 auio.uio_iovcnt = mp->msg_iovlen;
545 auio.uio_segflg = UIO_USERSPACE;
546 auio.uio_rw = UIO_WRITE;
548 auio.uio_offset = 0; /* XXX */
551 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
552 if ((auio.uio_resid += iov->iov_len) < 0) {
558 if (KTRPOINT(td, KTR_GENIO)) {
559 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
561 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
562 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
566 len = auio.uio_resid;
567 so = (struct socket *)fp->f_data;
568 error = so->so_proto->pr_usrreqs->pru_sosend(so, mp->msg_name,
569 &auio, NULL, mp->msg_control, mp->msg_flags, td);
571 if (auio.uio_resid != len && (error == ERESTART ||
572 error == EINTR || error == EWOULDBLOCK))
578 if (ktriov != NULL) {
580 ktruio.uio_iov = ktriov;
581 ktruio.uio_resid = len - auio.uio_resid;
582 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error);
584 FREE(ktriov, M_TEMP);
588 *res = len - auio.uio_resid;
594 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
597 sendto(struct sendto_args *uap)
601 struct sockaddr *sa = NULL;
605 error = getsockaddr(&sa, uap->to, uap->tolen);
612 /* msg.msg_namelen is ignored by kern_sendmsg() */
615 msg.msg_control = NULL;
616 msg.msg_flags = uap->flags;
617 aiov.iov_base = uap->buf;
618 aiov.iov_len = uap->len;
620 error = kern_sendmsg(uap->s, &msg, &uap->sysmsg_result);
628 * sendmsg_args(int s, caddr_t msg, int flags)
630 * We must copyin the msghdr and copyin a bunch of it's fields. We
631 * explicitly copyin msg.msg_iov and conditionally copyin the
632 * msg.msg_control and msg.msg_name fields.
635 sendmsg(struct sendmsg_args *uap)
638 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
639 struct sockaddr *sa = NULL;
640 struct mbuf *control = NULL;
643 error = copyin(uap->msg, (caddr_t)&msg, sizeof(msg));
648 * Conditionally copyin msg.msg_name.
651 error = getsockaddr(&sa, msg.msg_name, msg.msg_namelen);
658 * We always copyin msg.msg_iov.
660 if (msg.msg_iovlen >= UIO_MAXIOV) {
664 if (msg.msg_iovlen >= UIO_SMALLIOV) {
665 MALLOC(iov, struct iovec *,
666 sizeof(struct iovec) * msg.msg_iovlen, M_IOV, M_WAITOK);
670 error = copyin(msg.msg_iov, iov,
671 msg.msg_iovlen * sizeof(struct iovec));
677 * Conditionally copyin msg.msg_control.
679 if (msg.msg_control) {
680 if (msg.msg_controllen < sizeof(struct cmsghdr)) {
684 error = sockargs(&control, msg.msg_control,
685 msg.msg_controllen, MT_CONTROL);
688 msg.msg_control = control;
691 /* Don't forget the flags. */
692 msg.msg_flags = uap->flags;
694 error = kern_sendmsg(uap->s, &msg, &uap->sysmsg_result);
705 * If mp->msg_namelen is non-zero, then when we return mp->msg_name is
706 * a pointer to the address that we recieved from. If it is zero,
707 * the address is freed before return. Don't forget to FREE()
708 * mp->msg_name if mp->msg_namelen is non-zero.
711 kern_recvmsg(int s, struct msghdr *mp, int *res)
713 struct thread *td = curthread;
714 struct proc *p = td->td_proc;
722 struct iovec *ktriov = NULL;
726 error = holdsock(p->p_fd, s, &fp);
729 auio.uio_iov = mp->msg_iov;
730 auio.uio_iovcnt = mp->msg_iovlen;
731 auio.uio_segflg = UIO_USERSPACE;
732 auio.uio_rw = UIO_READ;
734 auio.uio_offset = 0; /* XXX */
737 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
738 if ((auio.uio_resid += iov->iov_len) < 0) {
744 if (KTRPOINT(td, KTR_GENIO)) {
745 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
747 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
748 bcopy(auio.uio_iov, ktriov, iovlen);
752 len = auio.uio_resid;
753 so = (struct socket *)fp->f_data;
754 error = so->so_proto->pr_usrreqs->pru_soreceive(so,
755 mp->msg_namelen ? (struct sockaddr **)&mp->msg_name : NULL,
757 mp->msg_controllen ? (struct mbuf **)&mp->msg_control : NULL,
760 if (auio.uio_resid != len && (error == ERESTART ||
761 error == EINTR || error == EWOULDBLOCK))
765 if (ktriov != NULL) {
767 ktruio.uio_iov = ktriov;
768 ktruio.uio_resid = len - auio.uio_resid;
769 ktrgenio(p->p_tracep, s, UIO_READ, &ktruio, error);
771 FREE(ktriov, M_TEMP);
775 *res = len - auio.uio_resid;
781 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
782 * caddr_t from, int *fromlenaddr)
785 recvfrom(struct recvfrom_args *uap)
791 if (uap->fromlenaddr) {
792 error = copyin(uap->fromlenaddr, &fromlen, sizeof(fromlen));
800 msg.msg_namelen = fromlen;
803 msg.msg_control = NULL;
804 msg.msg_flags = uap->flags;
805 aiov.iov_base = uap->buf;
806 aiov.iov_len = uap->len;
808 error = kern_recvmsg(uap->s, &msg, &uap->sysmsg_result);
810 fromlen = MIN(msg.msg_namelen, fromlen);
813 error = copyout(msg.msg_name, uap->from, fromlen);
815 error = copyout(&fromlen, uap->fromlenaddr,
819 FREE(msg.msg_name, M_SONAME);
825 * recvmsg_args(int s, struct msghdr *msg, int flags)
828 recvmsg(struct recvmsg_args *uap)
831 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
832 struct mbuf *m, *ucontrol;
833 struct sockaddr *uname;
835 socklen_t *unamelenp, *ucontrollenp;
836 int error, fromlen, len;
839 * This copyin handles everything except the iovec.
841 error = copyin(uap->msg, &msg, sizeof(msg));
846 * Save some userland pointers for the copyouts.
848 uname = msg.msg_name;
849 unamelenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
851 ucontrol = msg.msg_control;
852 ucontrollenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
855 fromlen = msg.msg_namelen;
858 * Copyin msg.msg_iov.
860 if (msg.msg_iovlen >= UIO_MAXIOV)
862 if (msg.msg_iovlen >= UIO_SMALLIOV) {
863 MALLOC(iov, struct iovec *,
864 sizeof(struct iovec) * msg.msg_iovlen, M_IOV, M_WAITOK);
868 error = copyin(msg.msg_iov, iov, msg.msg_iovlen * sizeof(struct iovec));
873 /* Don't forget the flags. */
874 msg.msg_flags = uap->flags;
876 error = kern_recvmsg(uap->s, &msg, &uap->sysmsg_result);
879 * Copyout msg.msg_name and msg.msg_namelen.
881 if (error == 0 && uname) {
882 fromlen = MIN(msg.msg_namelen, fromlen);
883 error = copyout(msg.msg_name, uname, fromlen);
885 error = copyout(&fromlen, unamelenp,
890 * Copyout msg.msg_control and msg.msg_controllen.
892 if (error == 0 && ucontrol) {
893 len = msg.msg_controllen;
894 msg.msg_controllen = 0;
896 ctlbuf = (caddr_t)ucontrol;
898 while(m && len > 0) {
901 if (len >= m->m_len) {
904 msg.msg_flags |= MSG_CTRUNC;
908 error = copyout(mtod(m, caddr_t), ctlbuf, tocopy);
916 msg.msg_controllen = ctlbuf - (caddr_t)ucontrol;
917 error = copyout(&msg.msg_controllen, ucontrollenp,
918 sizeof(*ucontrollenp));
923 FREE(msg.msg_name, M_SONAME);
927 m_freem(msg.msg_control);
932 * shutdown_args(int s, int how)
936 shutdown(struct shutdown_args *uap)
938 struct thread *td = curthread;
939 struct proc *p = td->td_proc;
944 error = holdsock(p->p_fd, uap->s, &fp);
947 error = soshutdown((struct socket *)fp->f_data, uap->how);
953 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
954 * in kernel pointer instead of a userland pointer. This allows us
955 * to manipulate socket options in the emulation code.
958 kern_setsockopt(int s, struct sockopt *sopt)
960 struct thread *td = curthread;
961 struct proc *p = td->td_proc;
965 if (sopt->sopt_val == 0 && sopt->sopt_valsize != 0)
967 if (sopt->sopt_valsize < 0)
970 error = holdsock(p->p_fd, s, &fp);
974 error = sosetopt((struct socket *)fp->f_data, sopt);
980 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
983 setsockopt(struct setsockopt_args *uap)
985 struct thread *td = curthread;
989 sopt.sopt_dir = SOPT_SET;
990 sopt.sopt_level = uap->level;
991 sopt.sopt_name = uap->name;
992 sopt.sopt_val = uap->val;
993 sopt.sopt_valsize = uap->valsize;
996 error = kern_setsockopt(uap->s, &sopt);
1001 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
1002 * in kernel pointer instead of a userland pointer. This allows us
1003 * to manipulate socket options in the emulation code.
1006 kern_getsockopt(int s, struct sockopt *sopt)
1008 struct thread *td = curthread;
1009 struct proc *p = td->td_proc;
1013 if (sopt->sopt_val == 0 && sopt->sopt_valsize != 0)
1015 if (sopt->sopt_valsize < 0)
1018 error = holdsock(p->p_fd, s, &fp);
1022 error = sogetopt((struct socket *)fp->f_data, sopt);
1028 * getsockopt_Args(int s, int level, int name, caddr_t val, int *avalsize)
1031 getsockopt(struct getsockopt_args *uap)
1033 struct thread *td = curthread;
1034 struct sockopt sopt;
1038 error = copyin(uap->avalsize, &valsize, sizeof(valsize));
1047 sopt.sopt_dir = SOPT_GET;
1048 sopt.sopt_level = uap->level;
1049 sopt.sopt_name = uap->name;
1050 sopt.sopt_val = uap->val;
1051 sopt.sopt_valsize = valsize;
1054 error = kern_getsockopt(uap->s, &sopt);
1056 valsize = sopt.sopt_valsize;
1057 error = copyout(&valsize, uap->avalsize, sizeof(valsize));
1063 * The second argument to kern_getsockname() is a handle to a struct sockaddr.
1064 * This allows kern_getsockname() to return a pointer to an allocated struct
1065 * sockaddr which must be freed later with FREE(). The caller must
1066 * initialize *name to NULL.
1069 kern_getsockname(int s, struct sockaddr **name, int *namelen)
1071 struct thread *td = curthread;
1072 struct proc *p = td->td_proc;
1075 struct sockaddr *sa = NULL;
1078 error = holdsock(p->p_fd, s, &fp);
1085 so = (struct socket *)fp->f_data;
1086 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
1091 *namelen = MIN(*namelen, sa->sa_len);
1101 * getsockname_args(int fdes, caddr_t asa, int *alen)
1106 getsockname(struct getsockname_args *uap)
1108 struct sockaddr *sa = NULL;
1111 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1115 error = kern_getsockname(uap->fdes, &sa, &sa_len);
1118 error = copyout(sa, uap->asa, sa_len);
1120 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1127 * The second argument to kern_getpeername() is a handle to a struct sockaddr.
1128 * This allows kern_getpeername() to return a pointer to an allocated struct
1129 * sockaddr which must be freed later with FREE(). The caller must
1130 * initialize *name to NULL.
1133 kern_getpeername(int s, struct sockaddr **name, int *namelen)
1135 struct thread *td = curthread;
1136 struct proc *p = td->td_proc;
1139 struct sockaddr *sa = NULL;
1142 error = holdsock(p->p_fd, s, &fp);
1149 so = (struct socket *)fp->f_data;
1150 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1154 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa);
1159 *namelen = MIN(*namelen, sa->sa_len);
1169 * getpeername_args(int fdes, caddr_t asa, int *alen)
1171 * Get name of peer for connected socket.
1174 getpeername(struct getpeername_args *uap)
1176 struct sockaddr *sa = NULL;
1179 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1183 error = kern_getpeername(uap->fdes, &sa, &sa_len);
1186 error = copyout(sa, uap->asa, sa_len);
1188 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1195 sockargs(mp, buf, buflen, type)
1200 struct sockaddr *sa;
1204 if ((u_int)buflen > MLEN) {
1205 #ifdef COMPAT_OLDSOCK
1206 if (type == MT_SONAME && (u_int)buflen <= 112)
1207 buflen = MLEN; /* unix domain compat. hack */
1212 m = m_get(M_WAIT, type);
1216 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1221 if (type == MT_SONAME) {
1222 sa = mtod(m, struct sockaddr *);
1224 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1225 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1226 sa->sa_family = sa->sa_len;
1228 sa->sa_len = buflen;
1235 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1237 struct sockaddr *sa;
1241 if (len > SOCK_MAXADDRLEN)
1242 return ENAMETOOLONG;
1243 if (len < offsetof(struct sockaddr, sa_data[0]))
1245 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1246 error = copyin(uaddr, sa, len);
1250 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1251 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1252 sa->sa_family = sa->sa_len;
1261 * holdsock() - load the struct file pointer associated
1262 * with a socket into *fpp. If an error occurs, non-zero
1263 * will be returned and *fpp will be set to NULL.
1266 holdsock(fdp, fdes, fpp)
1267 struct filedesc *fdp;
1271 struct file *fp = NULL;
1274 if ((unsigned)fdes >= fdp->fd_nfiles ||
1275 (fp = fdp->fd_ofiles[fdes]) == NULL) {
1277 } else if (fp->f_type != DTYPE_SOCKET) {
1288 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1291 sf_buf_init(void *arg)
1295 SLIST_INIT(&sf_freelist);
1296 sf_base = kmem_alloc_pageable(kernel_map, nsfbufs * PAGE_SIZE);
1297 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, M_NOWAIT);
1298 bzero(sf_bufs, nsfbufs * sizeof(struct sf_buf));
1299 for (i = 0; i < nsfbufs; i++) {
1300 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
1301 SLIST_INSERT_HEAD(&sf_freelist, &sf_bufs[i], free_list);
1306 * Get an sf_buf from the freelist. Will block if none are available.
1316 while ((sf = SLIST_FIRST(&sf_freelist)) == NULL) {
1317 sf_buf_alloc_want = 1;
1318 error = tsleep(&sf_freelist, PCATCH, "sfbufa", 0);
1323 SLIST_REMOVE_HEAD(&sf_freelist, free_list);
1330 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1332 sf_buf_ref(caddr_t addr, u_int size)
1337 if (sf->refcnt == 0)
1338 panic("sf_buf_ref: referencing a free sf_buf");
1343 * Lose a reference to an sf_buf. When none left, detach mapped page
1344 * and release resources back to the system.
1346 * Must be called at splimp.
1349 sf_buf_free(caddr_t addr, u_int size)
1356 if (sf->refcnt == 0)
1357 panic("sf_buf_free: freeing free sf_buf");
1359 if (sf->refcnt == 0) {
1360 pmap_qremove((vm_offset_t)addr, 1);
1363 vm_page_unwire(m, 0);
1365 * Check for the object going away on us. This can
1366 * happen since we don't hold a reference to it.
1367 * If so, we're responsible for freeing the page.
1369 if (m->wire_count == 0 && m->object == NULL)
1373 SLIST_INSERT_HEAD(&sf_freelist, sf, free_list);
1374 if (sf_buf_alloc_want) {
1375 sf_buf_alloc_want = 0;
1376 wakeup(&sf_freelist);
1383 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1384 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1386 * Send a file specified by 'fd' and starting at 'offset' to a socket
1387 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1388 * nbytes == 0. Optionally add a header and/or trailer to the socket
1389 * output. If specified, write the total number of bytes sent into *sbytes.
1392 sendfile(struct sendfile_args *uap)
1394 return (do_sendfile(uap, 0));
1399 osendfile(struct osendfile_args *uap)
1401 struct sendfile_args args;
1405 args.offset = uap->offset;
1406 args.nbytes = uap->nbytes;
1407 args.hdtr = uap->hdtr;
1408 args.sbytes = uap->sbytes;
1409 args.flags = uap->flags;
1411 return (do_sendfile(&args, 1));
1416 do_sendfile(struct sendfile_args *uap, int compat)
1418 struct thread *td = curthread;
1419 struct proc *p = td->td_proc;
1421 struct filedesc *fdp;
1423 struct vm_object *obj;
1428 struct writev_args nuap;
1429 struct sf_hdtr hdtr;
1430 off_t off, xfsize, hdtr_size, sbytes = 0;
1439 * Do argument checking. Must be a regular file in, stream
1440 * type and connected socket out, positive offset.
1442 fp = holdfp(fdp, uap->fd, FREAD);
1447 if (fp->f_type != DTYPE_VNODE) {
1451 vp = (struct vnode *)fp->f_data;
1453 if (vp->v_type != VREG || VOP_GETVOBJECT(vp, &obj) != 0) {
1458 error = holdsock(p->p_fd, uap->s, &fp);
1461 so = (struct socket *)fp->f_data;
1462 if (so->so_type != SOCK_STREAM) {
1466 if ((so->so_state & SS_ISCONNECTED) == 0) {
1470 if (uap->offset < 0) {
1476 * If specified, get the pointer to the sf_hdtr struct for
1477 * any headers/trailers.
1479 if (uap->hdtr != NULL) {
1480 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1484 * Send any headers. Wimp out and use writev(2).
1486 if (hdtr.headers != NULL) {
1488 nuap.iovp = hdtr.headers;
1489 nuap.iovcnt = hdtr.hdr_cnt;
1490 error = writev(&nuap);
1494 sbytes += nuap.sysmsg_result;
1496 hdtr_size += nuap.sysmsg_result;
1501 * Protect against multiple writers to the socket.
1503 (void) sblock(&so->so_snd, M_WAITOK);
1506 * Loop through the pages in the file, starting with the requested
1507 * offset. Get a file page (do I/O if necessary), map the file page
1508 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1511 for (off = uap->offset; ; off += xfsize, sbytes += xfsize) {
1515 pindex = OFF_TO_IDX(off);
1518 * Calculate the amount to transfer. Not to exceed a page,
1519 * the EOF, or the passed in nbytes.
1521 xfsize = obj->un_pager.vnp.vnp_size - off;
1522 if (xfsize > PAGE_SIZE)
1524 pgoff = (vm_offset_t)(off & PAGE_MASK);
1525 if (PAGE_SIZE - pgoff < xfsize)
1526 xfsize = PAGE_SIZE - pgoff;
1527 if (uap->nbytes && xfsize > (uap->nbytes - sbytes))
1528 xfsize = uap->nbytes - sbytes;
1532 * Optimize the non-blocking case by looking at the socket space
1533 * before going to the extra work of constituting the sf_buf.
1535 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1536 if (so->so_state & SS_CANTSENDMORE)
1540 sbunlock(&so->so_snd);
1544 * Attempt to look up the page.
1546 * Allocate if not found
1548 * Wait and loop if busy.
1550 pg = vm_page_lookup(obj, pindex);
1553 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL);
1559 } else if (vm_page_sleep_busy(pg, TRUE, "sfpbsy")) {
1564 * Wire the page so it does not get ripped out from under
1571 * If page is not valid for what we need, initiate I/O
1574 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1580 * Ensure that our page is still around when the I/O
1583 vm_page_io_start(pg);
1586 * Get the page from backing store.
1588 bsize = vp->v_mount->mnt_stat.f_iosize;
1589 auio.uio_iov = &aiov;
1590 auio.uio_iovcnt = 1;
1592 aiov.iov_len = MAXBSIZE;
1593 auio.uio_resid = MAXBSIZE;
1594 auio.uio_offset = trunc_page(off);
1595 auio.uio_segflg = UIO_NOCOPY;
1596 auio.uio_rw = UIO_READ;
1598 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
1599 error = VOP_READ(vp, &auio,
1600 IO_VMIO | ((MAXBSIZE / bsize) << 16),
1602 VOP_UNLOCK(vp, 0, td);
1603 vm_page_flag_clear(pg, PG_ZERO);
1604 vm_page_io_finish(pg);
1606 vm_page_unwire(pg, 0);
1608 * See if anyone else might know about this page.
1609 * If not and it is not valid, then free it.
1611 if (pg->wire_count == 0 && pg->valid == 0 &&
1612 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1613 pg->hold_count == 0) {
1617 sbunlock(&so->so_snd);
1624 * Get a sendfile buf. We usually wait as long as necessary,
1625 * but this wait can be interrupted.
1627 if ((sf = sf_buf_alloc()) == NULL) {
1629 vm_page_unwire(pg, 0);
1630 if (pg->wire_count == 0 && pg->object == NULL)
1633 sbunlock(&so->so_snd);
1640 * Allocate a kernel virtual page and insert the physical page
1645 pmap_qenter(sf->kva, &pg, 1);
1647 * Get an mbuf header and set it up as having external storage.
1649 MGETHDR(m, M_WAIT, MT_DATA);
1652 sf_buf_free((void *)sf->kva, PAGE_SIZE);
1653 sbunlock(&so->so_snd);
1656 m->m_ext.ext_free = sf_buf_free;
1657 m->m_ext.ext_ref = sf_buf_ref;
1658 m->m_ext.ext_buf = (void *)sf->kva;
1659 m->m_ext.ext_size = PAGE_SIZE;
1660 m->m_data = (char *) sf->kva + pgoff;
1661 m->m_flags |= M_EXT;
1662 m->m_pkthdr.len = m->m_len = xfsize;
1664 * Add the buffer to the socket buffer chain.
1669 * Make sure that the socket is still able to take more data.
1670 * CANTSENDMORE being true usually means that the connection
1671 * was closed. so_error is true when an error was sensed after
1673 * The state is checked after the page mapping and buffer
1674 * allocation above since those operations may block and make
1675 * any socket checks stale. From this point forward, nothing
1676 * blocks before the pru_send (or more accurately, any blocking
1677 * results in a loop back to here to re-check).
1679 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1680 if (so->so_state & SS_CANTSENDMORE) {
1683 error = so->so_error;
1687 sbunlock(&so->so_snd);
1692 * Wait for socket space to become available. We do this just
1693 * after checking the connection state above in order to avoid
1694 * a race condition with sbwait().
1696 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
1697 if (so->so_state & SS_NBIO) {
1699 sbunlock(&so->so_snd);
1704 error = sbwait(&so->so_snd);
1706 * An error from sbwait usually indicates that we've
1707 * been interrupted by a signal. If we've sent anything
1708 * then return bytes sent, otherwise return the error.
1712 sbunlock(&so->so_snd);
1719 (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, td);
1722 sbunlock(&so->so_snd);
1726 sbunlock(&so->so_snd);
1729 * Send trailers. Wimp out and use writev(2).
1731 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1733 nuap.iovp = hdtr.trailers;
1734 nuap.iovcnt = hdtr.trl_cnt;
1735 error = writev(&nuap);
1739 sbytes += nuap.sysmsg_result;
1741 hdtr_size += nuap.sysmsg_result;
1745 if (uap->sbytes != NULL) {
1747 sbytes += hdtr_size;
1748 copyout(&sbytes, uap->sbytes, sizeof(off_t));