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.9 2003/07/30 00:19:14 dillon 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>
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)
78 static int sendit __P((int s, struct msghdr *mp, int flags, int *res));
79 static int recvit __P((int s, struct msghdr *mp, caddr_t namelenp, int *res));
81 static int accept1 __P((struct accept_args *uap, int compat));
82 static int do_sendfile __P((struct sendfile_args *uap, int compat));
83 static int getsockname1 __P((struct getsockname_args *uap, int compat));
84 static int getpeername1 __P((struct getpeername_args *uap, int compat));
86 static SLIST_HEAD(, sf_buf) sf_freelist;
87 static vm_offset_t sf_base;
88 static struct sf_buf *sf_bufs;
89 static int sf_buf_alloc_want;
92 * System call interface to the socket abstraction.
94 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
95 #define COMPAT_OLDSOCK
98 extern struct fileops socketops;
101 * socket_args(int domain, int type, int protocol)
104 socket(struct socket_args *uap)
106 struct thread *td = curthread;
107 struct proc *p = td->td_proc;
108 struct filedesc *fdp;
116 error = falloc(p, &fp, &fd);
120 error = socreate(uap->domain, &so, uap->type, uap->protocol, td);
122 if (fdp->fd_ofiles[fd] == fp) {
123 fdp->fd_ofiles[fd] = NULL;
127 fp->f_data = (caddr_t)so;
128 fp->f_flag = FREAD|FWRITE;
129 fp->f_ops = &socketops;
130 fp->f_type = DTYPE_SOCKET;
131 uap->sysmsg_result = fd;
138 * bind_args(int s, caddr_t name, int namelen)
143 bind(struct bind_args *uap)
145 struct thread *td = curthread;
146 struct proc *p = td->td_proc;
152 error = holdsock(p->p_fd, uap->s, &fp);
155 error = getsockaddr(&sa, uap->name, uap->namelen);
160 error = sobind((struct socket *)fp->f_data, sa, td);
167 * listen_args(int s, int backlog)
171 listen(struct listen_args *uap)
173 struct thread *td = curthread;
174 struct proc *p = td->td_proc;
179 error = holdsock(p->p_fd, uap->s, &fp);
182 error = solisten((struct socket *)fp->f_data, uap->backlog, td);
188 * accept_args(int s, caddr_t name, int *anamelen)
191 accept1(struct accept_args *uap, int compat)
193 struct thread *td = curthread;
194 struct proc *p = td->td_proc;
195 struct filedesc *fdp = p->p_fd;
196 struct file *lfp = NULL;
197 struct file *nfp = NULL;
199 int namelen, error, s;
200 struct socket *head, *so;
202 u_int fflag; /* type must match fp->f_flag */
206 error = copyin((caddr_t)uap->anamelen, (caddr_t)&namelen,
213 error = holdsock(fdp, uap->s, &lfp);
217 head = (struct socket *)lfp->f_data;
218 if ((head->so_options & SO_ACCEPTCONN) == 0) {
223 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
224 if (head->so_state & SS_CANTRCVMORE) {
225 head->so_error = ECONNABORTED;
228 if ((head->so_state & SS_NBIO) != 0) {
229 head->so_error = EWOULDBLOCK;
232 error = tsleep((caddr_t)&head->so_timeo, PCATCH, "accept", 0);
238 if (head->so_error) {
239 error = head->so_error;
246 * At this point we know that there is at least one connection
247 * ready to be accepted. Remove it from the queue prior to
248 * allocating the file descriptor for it since falloc() may
249 * block allowing another process to accept the connection
252 so = TAILQ_FIRST(&head->so_comp);
253 TAILQ_REMOVE(&head->so_comp, so, so_list);
257 error = falloc(p, &nfp, &fd);
260 * Probably ran out of file descriptors. Put the
261 * unaccepted connection back onto the queue and
262 * do another wakeup so some other process might
263 * have a chance at it.
265 TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
267 wakeup_one(&head->so_timeo);
272 uap->sysmsg_result = fd;
274 /* connection has been removed from the listen queue */
275 KNOTE(&head->so_rcv.sb_sel.si_note, 0);
277 so->so_state &= ~SS_COMP;
279 if (head->so_sigio != NULL)
280 fsetown(fgetown(head->so_sigio), &so->so_sigio);
282 nfp->f_data = (caddr_t)so;
284 nfp->f_ops = &socketops;
285 nfp->f_type = DTYPE_SOCKET;
286 /* Sync socket nonblocking/async state with file flags */
287 tmp = fflag & FNONBLOCK;
288 (void) fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, td);
289 tmp = fflag & FASYNC;
290 (void) fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, td);
292 error = soaccept(so, &sa);
295 * return a namelen of zero for older code which might
296 * ignore the return value from accept.
298 if (uap->name != NULL) {
300 (void) copyout((caddr_t)&namelen,
301 (caddr_t)uap->anamelen, sizeof(*uap->anamelen));
314 /* check sa_len before it is destroyed */
315 if (namelen > sa->sa_len)
316 namelen = sa->sa_len;
317 #ifdef COMPAT_OLDSOCK
319 ((struct osockaddr *)sa)->sa_family =
322 error = copyout(sa, (caddr_t)uap->name, (u_int)namelen);
325 error = copyout((caddr_t)&namelen,
326 (caddr_t)uap->anamelen, sizeof (*uap->anamelen));
333 * close the new descriptor, assuming someone hasn't ripped it
337 if (fdp->fd_ofiles[fd] == nfp) {
338 fdp->fd_ofiles[fd] = NULL;
345 * Release explicitly held references before returning.
355 accept(struct accept_args *uap)
357 return (accept1(uap, 0));
360 #ifdef COMPAT_OLDSOCK
362 oaccept(struct accept_args *uap)
365 return (accept1(uap, 1));
367 #endif /* COMPAT_OLDSOCK */
370 * connect_args(int s, caddr_t name, int namelen)
374 connect(struct connect_args *uap)
376 struct thread *td = curthread;
377 struct proc *p = td->td_proc;
383 error = holdsock(p->p_fd, uap->s, &fp);
386 so = (struct socket *)fp->f_data;
387 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
391 error = getsockaddr(&sa, uap->name, uap->namelen);
394 error = soconnect(so, sa, td);
397 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;
416 if (error == ERESTART)
424 * socketpair(int domain, int type, int protocol, int *rsv)
427 socketpair(struct socketpair_args *uap)
429 struct thread *td = curthread;
430 struct proc *p = td->td_proc;
431 struct filedesc *fdp;
432 struct file *fp1, *fp2;
433 struct socket *so1, *so2;
434 int fd, error, sv[2];
438 error = socreate(uap->domain, &so1, uap->type, uap->protocol, td);
441 error = socreate(uap->domain, &so2, uap->type, uap->protocol, td);
444 error = falloc(p, &fp1, &fd);
449 fp1->f_data = (caddr_t)so1;
450 error = falloc(p, &fp2, &fd);
454 fp2->f_data = (caddr_t)so2;
456 error = soconnect2(so1, so2);
459 if (uap->type == SOCK_DGRAM) {
461 * Datagram socket connection is asymmetric.
463 error = soconnect2(so2, so1);
467 fp1->f_flag = fp2->f_flag = FREAD|FWRITE;
468 fp1->f_ops = fp2->f_ops = &socketops;
469 fp1->f_type = fp2->f_type = DTYPE_SOCKET;
470 error = copyout((caddr_t)sv, (caddr_t)uap->rsv, 2 * sizeof (int));
475 if (fdp->fd_ofiles[sv[1]] == fp2) {
476 fdp->fd_ofiles[sv[1]] = NULL;
481 if (fdp->fd_ofiles[sv[0]] == fp1) {
482 fdp->fd_ofiles[sv[0]] = NULL;
494 sendit(int s, struct msghdr *mp, int flags, int *res)
496 struct thread *td = curthread;
497 struct proc *p = td->td_proc;
502 struct mbuf *control;
507 struct iovec *ktriov = NULL;
511 error = holdsock(p->p_fd, s, &fp);
514 auio.uio_iov = mp->msg_iov;
515 auio.uio_iovcnt = mp->msg_iovlen;
516 auio.uio_segflg = UIO_USERSPACE;
517 auio.uio_rw = UIO_WRITE;
519 auio.uio_offset = 0; /* XXX */
522 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
523 if ((auio.uio_resid += iov->iov_len) < 0) {
529 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
537 if (mp->msg_control) {
538 if (mp->msg_controllen < sizeof(struct cmsghdr)
539 #ifdef COMPAT_OLDSOCK
540 && mp->msg_flags != MSG_COMPAT
546 error = sockargs(&control, mp->msg_control,
547 mp->msg_controllen, MT_CONTROL);
550 #ifdef COMPAT_OLDSOCK
551 if (mp->msg_flags == MSG_COMPAT) {
554 M_PREPEND(control, sizeof(*cm), M_WAIT);
559 cm = mtod(control, struct cmsghdr *);
560 cm->cmsg_len = control->m_len;
561 cm->cmsg_level = SOL_SOCKET;
562 cm->cmsg_type = SCM_RIGHTS;
570 if (KTRPOINT(td, KTR_GENIO)) {
571 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
573 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
574 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
578 len = auio.uio_resid;
579 so = (struct socket *)fp->f_data;
580 error = so->so_proto->pr_usrreqs->pru_sosend(so, to, &auio, 0, control,
583 if (auio.uio_resid != len && (error == ERESTART ||
584 error == EINTR || error == EWOULDBLOCK))
590 *res = len - auio.uio_resid;
592 if (ktriov != NULL) {
594 ktruio.uio_iov = ktriov;
595 ktruio.uio_resid = *res;
596 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error);
598 FREE(ktriov, M_TEMP);
609 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
612 sendto(struct sendto_args *uap)
617 msg.msg_name = uap->to;
618 msg.msg_namelen = uap->tolen;
622 #ifdef COMPAT_OLDSOCK
625 aiov.iov_base = uap->buf;
626 aiov.iov_len = uap->len;
627 return (sendit(uap->s, &msg, uap->flags, &uap->sysmsg_result));
630 #ifdef COMPAT_OLDSOCK
632 * osend_args(int s, caddr_t buf, int len, int flags)
635 osend(struct osend_args *uap)
644 aiov.iov_base = uap->buf;
645 aiov.iov_len = uap->len;
648 return (sendit(uap->s, &msg, uap->flags, &uap->sysmsg_result));
652 * osendmsg_args(int s, caddr_t msg, int flags)
655 osendmsg(struct osendmsg_args *uap)
658 struct iovec aiov[UIO_SMALLIOV], *iov;
661 error = copyin(uap->msg, (caddr_t)&msg, sizeof (struct omsghdr));
664 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
665 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
667 MALLOC(iov, struct iovec *,
668 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
672 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
673 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
676 msg.msg_flags = MSG_COMPAT;
678 error = sendit(uap->s, &msg, uap->flags, &uap->sysmsg_result);
687 * sendmsg_args(int s, caddr_t msg, int flags)
690 sendmsg(struct sendmsg_args *uap)
693 struct iovec aiov[UIO_SMALLIOV], *iov;
696 error = copyin(uap->msg, (caddr_t)&msg, sizeof (msg));
699 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
700 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
702 MALLOC(iov, struct iovec *,
703 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
707 if (msg.msg_iovlen &&
708 (error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
709 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)))))
712 #ifdef COMPAT_OLDSOCK
715 error = sendit(uap->s, &msg, uap->flags, &uap->sysmsg_result);
723 recvit(int s, struct msghdr *mp, caddr_t namelenp, int *res)
725 struct thread *td = curthread;
726 struct proc *p = td->td_proc;
732 struct mbuf *m, *control = 0;
735 struct sockaddr *fromsa = 0;
737 struct iovec *ktriov = NULL;
741 error = holdsock(p->p_fd, s, &fp);
744 auio.uio_iov = mp->msg_iov;
745 auio.uio_iovcnt = mp->msg_iovlen;
746 auio.uio_segflg = UIO_USERSPACE;
747 auio.uio_rw = UIO_READ;
749 auio.uio_offset = 0; /* XXX */
752 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
753 if ((auio.uio_resid += iov->iov_len) < 0) {
759 if (KTRPOINT(td, KTR_GENIO)) {
760 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
762 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
763 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
767 len = auio.uio_resid;
768 so = (struct socket *)fp->f_data;
769 error = so->so_proto->pr_usrreqs->pru_soreceive(so, &fromsa, &auio,
770 (struct mbuf **)0, mp->msg_control ? &control : (struct mbuf **)0,
773 if (auio.uio_resid != len && (error == ERESTART ||
774 error == EINTR || error == EWOULDBLOCK))
778 if (ktriov != NULL) {
780 ktruio.uio_iov = ktriov;
781 ktruio.uio_resid = len - auio.uio_resid;
782 ktrgenio(p->p_tracep, s, UIO_READ, &ktruio, error);
784 FREE(ktriov, M_TEMP);
789 *res = len - auio.uio_resid;
791 len = mp->msg_namelen;
792 if (len <= 0 || fromsa == 0)
795 /* save sa_len before it is destroyed by MSG_COMPAT */
796 len = MIN(len, fromsa->sa_len);
797 #ifdef COMPAT_OLDSOCK
798 if (mp->msg_flags & MSG_COMPAT)
799 ((struct osockaddr *)fromsa)->sa_family =
802 error = copyout(fromsa,
803 (caddr_t)mp->msg_name, (unsigned)len);
807 mp->msg_namelen = len;
809 (error = copyout((caddr_t)&len, namelenp, sizeof (int)))) {
810 #ifdef COMPAT_OLDSOCK
811 if (mp->msg_flags & MSG_COMPAT)
812 error = 0; /* old recvfrom didn't check */
818 if (mp->msg_control) {
819 #ifdef COMPAT_OLDSOCK
821 * We assume that old recvmsg calls won't receive access
822 * rights and other control info, esp. as control info
823 * is always optional and those options didn't exist in 4.3.
824 * If we receive rights, trim the cmsghdr; anything else
827 if (control && mp->msg_flags & MSG_COMPAT) {
828 if (mtod(control, struct cmsghdr *)->cmsg_level !=
830 mtod(control, struct cmsghdr *)->cmsg_type !=
832 mp->msg_controllen = 0;
835 control->m_len -= sizeof (struct cmsghdr);
836 control->m_data += sizeof (struct cmsghdr);
839 len = mp->msg_controllen;
841 mp->msg_controllen = 0;
842 ctlbuf = (caddr_t) mp->msg_control;
844 while (m && len > 0) {
850 mp->msg_flags |= MSG_CTRUNC;
854 if ((error = copyout((caddr_t)mtod(m, caddr_t),
855 ctlbuf, tocopy)) != 0)
862 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
867 FREE(fromsa, M_SONAME);
874 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
875 * caddr_t from, int *fromlenaddr)
878 recvfrom(struct recvfrom_args *uap)
884 if (uap->fromlenaddr) {
885 error = copyin((caddr_t)uap->fromlenaddr,
886 (caddr_t)&msg.msg_namelen, sizeof (msg.msg_namelen));
891 msg.msg_name = uap->from;
894 aiov.iov_base = uap->buf;
895 aiov.iov_len = uap->len;
897 msg.msg_flags = uap->flags;
898 return (recvit(uap->s, &msg, (caddr_t)uap->fromlenaddr, &uap->sysmsg_result));
901 #ifdef COMPAT_OLDSOCK
903 orecvfrom(struct recvfrom_args *uap)
905 uap->flags |= MSG_COMPAT;
906 return (recvfrom(uap));
910 #ifdef COMPAT_OLDSOCK
912 * struct orecv_args(int s, caddr_t buf, int len, int flags)
915 orecv(struct orecv_args *uap)
924 aiov.iov_base = uap->buf;
925 aiov.iov_len = uap->len;
927 msg.msg_flags = uap->flags;
928 return (recvit(uap->s, &msg, (caddr_t)0, &uap->sysmsg_result));
932 * Old recvmsg. This code takes advantage of the fact that the old msghdr
933 * overlays the new one, missing only the flags, and with the (old) access
934 * rights where the control fields are now.
936 * orecvmsg_args(int s, struct omsghdr *msg, int flags)
939 orecvmsg(struct orecvmsg_args *uap)
942 struct iovec aiov[UIO_SMALLIOV], *iov;
945 error = copyin((caddr_t)uap->msg, (caddr_t)&msg,
946 sizeof (struct omsghdr));
949 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
950 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
952 MALLOC(iov, struct iovec *,
953 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
957 msg.msg_flags = uap->flags | MSG_COMPAT;
958 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
959 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
963 error = recvit(uap->s, &msg, (caddr_t)&uap->msg->msg_namelen, &uap->sysmsg_result);
965 if (msg.msg_controllen && error == 0)
966 error = copyout((caddr_t)&msg.msg_controllen,
967 (caddr_t)&uap->msg->msg_accrightslen, sizeof (int));
976 * recvmsg_args(int s, struct msghdr *msg, int flags)
979 recvmsg(struct recvmsg_args *uap)
982 struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
985 error = copyin((caddr_t)uap->msg, (caddr_t)&msg, sizeof (msg));
988 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
989 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
991 MALLOC(iov, struct iovec *,
992 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
996 #ifdef COMPAT_OLDSOCK
997 msg.msg_flags = uap->flags &~ MSG_COMPAT;
999 msg.msg_flags = uap->flags;
1003 error = copyin((caddr_t)uiov, (caddr_t)iov,
1004 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
1007 error = recvit(uap->s, &msg, (caddr_t)0, &uap->sysmsg_result);
1010 error = copyout((caddr_t)&msg, (caddr_t)uap->msg, sizeof(msg));
1019 * shutdown_args(int s, int how)
1023 shutdown(struct shutdown_args *uap)
1025 struct thread *td = curthread;
1026 struct proc *p = td->td_proc;
1031 error = holdsock(p->p_fd, uap->s, &fp);
1034 error = soshutdown((struct socket *)fp->f_data, uap->how);
1040 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
1044 setsockopt(struct setsockopt_args *uap)
1046 struct thread *td = curthread;
1047 struct proc *p = td->td_proc;
1049 struct sockopt sopt;
1052 if (uap->val == 0 && uap->valsize != 0)
1054 if (uap->valsize < 0)
1057 error = holdsock(p->p_fd, uap->s, &fp);
1061 sopt.sopt_dir = SOPT_SET;
1062 sopt.sopt_level = uap->level;
1063 sopt.sopt_name = uap->name;
1064 sopt.sopt_val = uap->val;
1065 sopt.sopt_valsize = uap->valsize;
1067 error = sosetopt((struct socket *)fp->f_data, &sopt);
1073 * getsockopt_Args(int s, int level, int name, caddr_t val, int *avalsize)
1077 getsockopt(struct getsockopt_args *uap)
1079 struct thread *td = curthread;
1080 struct proc *p = td->td_proc;
1083 struct sockopt sopt;
1085 error = holdsock(p->p_fd, uap->s, &fp);
1089 error = copyin((caddr_t)uap->avalsize, (caddr_t)&valsize,
1103 sopt.sopt_dir = SOPT_GET;
1104 sopt.sopt_level = uap->level;
1105 sopt.sopt_name = uap->name;
1106 sopt.sopt_val = uap->val;
1107 sopt.sopt_valsize = (size_t)valsize; /* checked non-negative above */
1110 error = sogetopt((struct socket *)fp->f_data, &sopt);
1112 valsize = sopt.sopt_valsize;
1113 error = copyout((caddr_t)&valsize,
1114 (caddr_t)uap->avalsize, sizeof (valsize));
1121 * getsockname_args(int fdes, caddr_t asa, int *alen)
1127 getsockname1(struct getsockname_args *uap, int compat)
1129 struct thread *td = curthread;
1130 struct proc *p = td->td_proc;
1133 struct sockaddr *sa;
1136 error = holdsock(p->p_fd, uap->fdes, &fp);
1139 error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len));
1148 so = (struct socket *)fp->f_data;
1150 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
1158 len = MIN(len, sa->sa_len);
1159 #ifdef COMPAT_OLDSOCK
1161 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1163 error = copyout(sa, (caddr_t)uap->asa, (u_int)len);
1166 error = copyout((caddr_t)&len, (caddr_t)uap->alen,
1176 getsockname(struct getsockname_args *uap)
1179 return (getsockname1(uap, 0));
1182 #ifdef COMPAT_OLDSOCK
1184 ogetsockname(struct getsockname_args *uap)
1187 return (getsockname1(uap, 1));
1189 #endif /* COMPAT_OLDSOCK */
1192 * getpeername_args(int fdes, caddr_t asa, int *alen)
1194 * Get name of peer for connected socket.
1198 getpeername1(struct getpeername_args *uap, int compat)
1200 struct thread *td = curthread;
1201 struct proc *p = td->td_proc;
1204 struct sockaddr *sa;
1207 error = holdsock(p->p_fd, uap->fdes, &fp);
1210 so = (struct socket *)fp->f_data;
1211 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1215 error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len));
1225 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa);
1232 len = MIN(len, sa->sa_len);
1233 #ifdef COMPAT_OLDSOCK
1235 ((struct osockaddr *)sa)->sa_family =
1238 error = copyout(sa, (caddr_t)uap->asa, (u_int)len);
1242 error = copyout((caddr_t)&len, (caddr_t)uap->alen, sizeof (len));
1251 getpeername(struct getpeername_args *uap)
1253 return (getpeername1(uap, 0));
1256 #ifdef COMPAT_OLDSOCK
1258 ogetpeername(struct ogetpeername_args *uap)
1260 /* XXX uap should have type `getpeername_args *' to begin with. */
1261 return (getpeername1((struct getpeername_args *)uap, 1));
1263 #endif /* COMPAT_OLDSOCK */
1266 sockargs(mp, buf, buflen, type)
1271 struct sockaddr *sa;
1275 if ((u_int)buflen > MLEN) {
1276 #ifdef COMPAT_OLDSOCK
1277 if (type == MT_SONAME && (u_int)buflen <= 112)
1278 buflen = MLEN; /* unix domain compat. hack */
1283 m = m_get(M_WAIT, type);
1287 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1292 if (type == MT_SONAME) {
1293 sa = mtod(m, struct sockaddr *);
1295 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1296 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1297 sa->sa_family = sa->sa_len;
1299 sa->sa_len = buflen;
1306 getsockaddr(namp, uaddr, len)
1307 struct sockaddr **namp;
1311 struct sockaddr *sa;
1314 if (len > SOCK_MAXADDRLEN)
1315 return ENAMETOOLONG;
1316 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1317 error = copyin(uaddr, sa, len);
1321 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1322 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1323 sa->sa_family = sa->sa_len;
1332 * holdsock() - load the struct file pointer associated
1333 * with a socket into *fpp. If an error occurs, non-zero
1334 * will be returned and *fpp will be set to NULL.
1337 holdsock(fdp, fdes, fpp)
1338 struct filedesc *fdp;
1342 struct file *fp = NULL;
1345 if ((unsigned)fdes >= fdp->fd_nfiles ||
1346 (fp = fdp->fd_ofiles[fdes]) == NULL) {
1348 } else if (fp->f_type != DTYPE_SOCKET) {
1359 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1362 sf_buf_init(void *arg)
1366 SLIST_INIT(&sf_freelist);
1367 sf_base = kmem_alloc_pageable(kernel_map, nsfbufs * PAGE_SIZE);
1368 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, M_NOWAIT);
1369 bzero(sf_bufs, nsfbufs * sizeof(struct sf_buf));
1370 for (i = 0; i < nsfbufs; i++) {
1371 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
1372 SLIST_INSERT_HEAD(&sf_freelist, &sf_bufs[i], free_list);
1377 * Get an sf_buf from the freelist. Will block if none are available.
1387 while ((sf = SLIST_FIRST(&sf_freelist)) == NULL) {
1388 sf_buf_alloc_want = 1;
1389 error = tsleep(&sf_freelist, PCATCH, "sfbufa", 0);
1394 SLIST_REMOVE_HEAD(&sf_freelist, free_list);
1401 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1403 sf_buf_ref(caddr_t addr, u_int size)
1408 if (sf->refcnt == 0)
1409 panic("sf_buf_ref: referencing a free sf_buf");
1414 * Lose a reference to an sf_buf. When none left, detach mapped page
1415 * and release resources back to the system.
1417 * Must be called at splimp.
1420 sf_buf_free(caddr_t addr, u_int size)
1427 if (sf->refcnt == 0)
1428 panic("sf_buf_free: freeing free sf_buf");
1430 if (sf->refcnt == 0) {
1431 pmap_qremove((vm_offset_t)addr, 1);
1434 vm_page_unwire(m, 0);
1436 * Check for the object going away on us. This can
1437 * happen since we don't hold a reference to it.
1438 * If so, we're responsible for freeing the page.
1440 if (m->wire_count == 0 && m->object == NULL)
1444 SLIST_INSERT_HEAD(&sf_freelist, sf, free_list);
1445 if (sf_buf_alloc_want) {
1446 sf_buf_alloc_want = 0;
1447 wakeup(&sf_freelist);
1454 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1455 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1457 * Send a file specified by 'fd' and starting at 'offset' to a socket
1458 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1459 * nbytes == 0. Optionally add a header and/or trailer to the socket
1460 * output. If specified, write the total number of bytes sent into *sbytes.
1463 sendfile(struct sendfile_args *uap)
1465 return (do_sendfile(uap, 0));
1470 osendfile(struct osendfile_args *uap)
1472 struct sendfile_args args;
1476 args.offset = uap->offset;
1477 args.nbytes = uap->nbytes;
1478 args.hdtr = uap->hdtr;
1479 args.sbytes = uap->sbytes;
1480 args.flags = uap->flags;
1482 return (do_sendfile(&args, 1));
1487 do_sendfile(struct sendfile_args *uap, int compat)
1489 struct thread *td = curthread;
1490 struct proc *p = td->td_proc;
1492 struct filedesc *fdp;
1494 struct vm_object *obj;
1499 struct writev_args nuap;
1500 struct sf_hdtr hdtr;
1501 off_t off, xfsize, hdtr_size, sbytes = 0;
1510 * Do argument checking. Must be a regular file in, stream
1511 * type and connected socket out, positive offset.
1513 fp = holdfp(fdp, uap->fd, FREAD);
1518 if (fp->f_type != DTYPE_VNODE) {
1522 vp = (struct vnode *)fp->f_data;
1524 if (vp->v_type != VREG || VOP_GETVOBJECT(vp, &obj) != 0) {
1529 error = holdsock(p->p_fd, uap->s, &fp);
1532 so = (struct socket *)fp->f_data;
1533 if (so->so_type != SOCK_STREAM) {
1537 if ((so->so_state & SS_ISCONNECTED) == 0) {
1541 if (uap->offset < 0) {
1547 * If specified, get the pointer to the sf_hdtr struct for
1548 * any headers/trailers.
1550 if (uap->hdtr != NULL) {
1551 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1555 * Send any headers. Wimp out and use writev(2).
1557 if (hdtr.headers != NULL) {
1559 nuap.iovp = hdtr.headers;
1560 nuap.iovcnt = hdtr.hdr_cnt;
1561 error = writev(&nuap);
1565 sbytes += uap->sysmsg_result;
1567 hdtr_size += uap->sysmsg_result;
1572 * Protect against multiple writers to the socket.
1574 (void) sblock(&so->so_snd, M_WAITOK);
1577 * Loop through the pages in the file, starting with the requested
1578 * offset. Get a file page (do I/O if necessary), map the file page
1579 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1582 for (off = uap->offset; ; off += xfsize, sbytes += xfsize) {
1586 pindex = OFF_TO_IDX(off);
1589 * Calculate the amount to transfer. Not to exceed a page,
1590 * the EOF, or the passed in nbytes.
1592 xfsize = obj->un_pager.vnp.vnp_size - off;
1593 if (xfsize > PAGE_SIZE)
1595 pgoff = (vm_offset_t)(off & PAGE_MASK);
1596 if (PAGE_SIZE - pgoff < xfsize)
1597 xfsize = PAGE_SIZE - pgoff;
1598 if (uap->nbytes && xfsize > (uap->nbytes - sbytes))
1599 xfsize = uap->nbytes - sbytes;
1603 * Optimize the non-blocking case by looking at the socket space
1604 * before going to the extra work of constituting the sf_buf.
1606 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1607 if (so->so_state & SS_CANTSENDMORE)
1611 sbunlock(&so->so_snd);
1615 * Attempt to look up the page.
1617 * Allocate if not found
1619 * Wait and loop if busy.
1621 pg = vm_page_lookup(obj, pindex);
1624 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL);
1630 } else if (vm_page_sleep_busy(pg, TRUE, "sfpbsy")) {
1635 * Wire the page so it does not get ripped out from under
1642 * If page is not valid for what we need, initiate I/O
1645 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1651 * Ensure that our page is still around when the I/O
1654 vm_page_io_start(pg);
1657 * Get the page from backing store.
1659 bsize = vp->v_mount->mnt_stat.f_iosize;
1660 auio.uio_iov = &aiov;
1661 auio.uio_iovcnt = 1;
1663 aiov.iov_len = MAXBSIZE;
1664 auio.uio_resid = MAXBSIZE;
1665 auio.uio_offset = trunc_page(off);
1666 auio.uio_segflg = UIO_NOCOPY;
1667 auio.uio_rw = UIO_READ;
1669 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
1670 error = VOP_READ(vp, &auio,
1671 IO_VMIO | ((MAXBSIZE / bsize) << 16),
1673 VOP_UNLOCK(vp, 0, td);
1674 vm_page_flag_clear(pg, PG_ZERO);
1675 vm_page_io_finish(pg);
1677 vm_page_unwire(pg, 0);
1679 * See if anyone else might know about this page.
1680 * If not and it is not valid, then free it.
1682 if (pg->wire_count == 0 && pg->valid == 0 &&
1683 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1684 pg->hold_count == 0) {
1688 sbunlock(&so->so_snd);
1695 * Get a sendfile buf. We usually wait as long as necessary,
1696 * but this wait can be interrupted.
1698 if ((sf = sf_buf_alloc()) == NULL) {
1700 vm_page_unwire(pg, 0);
1701 if (pg->wire_count == 0 && pg->object == NULL)
1704 sbunlock(&so->so_snd);
1711 * Allocate a kernel virtual page and insert the physical page
1716 pmap_qenter(sf->kva, &pg, 1);
1718 * Get an mbuf header and set it up as having external storage.
1720 MGETHDR(m, M_WAIT, MT_DATA);
1723 sf_buf_free((void *)sf->kva, PAGE_SIZE);
1724 sbunlock(&so->so_snd);
1727 m->m_ext.ext_free = sf_buf_free;
1728 m->m_ext.ext_ref = sf_buf_ref;
1729 m->m_ext.ext_buf = (void *)sf->kva;
1730 m->m_ext.ext_size = PAGE_SIZE;
1731 m->m_data = (char *) sf->kva + pgoff;
1732 m->m_flags |= M_EXT;
1733 m->m_pkthdr.len = m->m_len = xfsize;
1735 * Add the buffer to the socket buffer chain.
1740 * Make sure that the socket is still able to take more data.
1741 * CANTSENDMORE being true usually means that the connection
1742 * was closed. so_error is true when an error was sensed after
1744 * The state is checked after the page mapping and buffer
1745 * allocation above since those operations may block and make
1746 * any socket checks stale. From this point forward, nothing
1747 * blocks before the pru_send (or more accurately, any blocking
1748 * results in a loop back to here to re-check).
1750 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1751 if (so->so_state & SS_CANTSENDMORE) {
1754 error = so->so_error;
1758 sbunlock(&so->so_snd);
1763 * Wait for socket space to become available. We do this just
1764 * after checking the connection state above in order to avoid
1765 * a race condition with sbwait().
1767 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
1768 if (so->so_state & SS_NBIO) {
1770 sbunlock(&so->so_snd);
1775 error = sbwait(&so->so_snd);
1777 * An error from sbwait usually indicates that we've
1778 * been interrupted by a signal. If we've sent anything
1779 * then return bytes sent, otherwise return the error.
1783 sbunlock(&so->so_snd);
1790 (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, td);
1793 sbunlock(&so->so_snd);
1797 sbunlock(&so->so_snd);
1800 * Send trailers. Wimp out and use writev(2).
1802 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1804 nuap.iovp = hdtr.trailers;
1805 nuap.iovcnt = hdtr.trl_cnt;
1806 error = writev(&nuap);
1810 sbytes += uap->sysmsg_result;
1812 hdtr_size += uap->sysmsg_result;
1816 if (uap->sbytes != NULL) {
1818 sbytes += hdtr_size;
1819 copyout(&sbytes, uap->sbytes, sizeof(off_t));