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.2 2003/06/17 04:28:41 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>
74 static void sf_buf_init(void *arg);
75 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
77 static int sendit __P((struct proc *p, int s, struct msghdr *mp, int flags));
78 static int recvit __P((struct proc *p, int s, struct msghdr *mp,
81 static int accept1 __P((struct proc *p, struct accept_args *uap, int compat));
82 static int do_sendfile __P((struct proc *p, struct sendfile_args *uap,
84 static int getsockname1 __P((struct proc *p, struct getsockname_args *uap,
86 static int getpeername1 __P((struct proc *p, struct getpeername_args *uap,
89 static SLIST_HEAD(, sf_buf) sf_freelist;
90 static vm_offset_t sf_base;
91 static struct sf_buf *sf_bufs;
92 static int sf_buf_alloc_want;
95 * System call interface to the socket abstraction.
97 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
98 #define COMPAT_OLDSOCK
101 extern struct fileops socketops;
106 register struct socket_args /* {
112 struct filedesc *fdp = p->p_fd;
117 error = falloc(p, &fp, &fd);
121 error = socreate(uap->domain, &so, uap->type, uap->protocol, p);
123 if (fdp->fd_ofiles[fd] == fp) {
124 fdp->fd_ofiles[fd] = NULL;
128 fp->f_data = (caddr_t)so;
129 fp->f_flag = FREAD|FWRITE;
130 fp->f_ops = &socketops;
131 fp->f_type = DTYPE_SOCKET;
142 register struct bind_args /* {
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, p);
170 register struct listen_args /* {
178 error = holdsock(p->p_fd, uap->s, &fp);
181 error = solisten((struct socket *)fp->f_data, uap->backlog, p);
187 accept1(p, uap, compat)
189 register struct accept_args /* {
196 struct filedesc *fdp = p->p_fd;
197 struct file *lfp = NULL;
198 struct file *nfp = NULL;
200 int namelen, error, s;
201 struct socket *head, *so;
203 u_int fflag; /* type must match fp->f_flag */
207 error = copyin((caddr_t)uap->anamelen, (caddr_t)&namelen,
214 error = holdsock(fdp, uap->s, &lfp);
218 head = (struct socket *)lfp->f_data;
219 if ((head->so_options & SO_ACCEPTCONN) == 0) {
224 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
225 if (head->so_state & SS_CANTRCVMORE) {
226 head->so_error = ECONNABORTED;
229 if ((head->so_state & SS_NBIO) != 0) {
230 head->so_error = EWOULDBLOCK;
233 error = tsleep((caddr_t)&head->so_timeo, PSOCK | PCATCH,
240 if (head->so_error) {
241 error = head->so_error;
248 * At this point we know that there is at least one connection
249 * ready to be accepted. Remove it from the queue prior to
250 * allocating the file descriptor for it since falloc() may
251 * block allowing another process to accept the connection
254 so = TAILQ_FIRST(&head->so_comp);
255 TAILQ_REMOVE(&head->so_comp, so, so_list);
259 error = falloc(p, &nfp, &fd);
262 * Probably ran out of file descriptors. Put the
263 * unaccepted connection back onto the queue and
264 * do another wakeup so some other process might
265 * have a chance at it.
267 TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
269 wakeup_one(&head->so_timeo);
276 /* connection has been removed from the listen queue */
277 KNOTE(&head->so_rcv.sb_sel.si_note, 0);
279 so->so_state &= ~SS_COMP;
281 if (head->so_sigio != NULL)
282 fsetown(fgetown(head->so_sigio), &so->so_sigio);
284 nfp->f_data = (caddr_t)so;
286 nfp->f_ops = &socketops;
287 nfp->f_type = DTYPE_SOCKET;
288 /* Sync socket nonblocking/async state with file flags */
289 tmp = fflag & FNONBLOCK;
290 (void) fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, p);
291 tmp = fflag & FASYNC;
292 (void) fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, p);
294 error = soaccept(so, &sa);
297 * return a namelen of zero for older code which might
298 * ignore the return value from accept.
300 if (uap->name != NULL) {
302 (void) copyout((caddr_t)&namelen,
303 (caddr_t)uap->anamelen, sizeof(*uap->anamelen));
316 /* check sa_len before it is destroyed */
317 if (namelen > sa->sa_len)
318 namelen = sa->sa_len;
319 #ifdef COMPAT_OLDSOCK
321 ((struct osockaddr *)sa)->sa_family =
324 error = copyout(sa, (caddr_t)uap->name, (u_int)namelen);
327 error = copyout((caddr_t)&namelen,
328 (caddr_t)uap->anamelen, sizeof (*uap->anamelen));
335 * close the new descriptor, assuming someone hasn't ripped it
339 if (fdp->fd_ofiles[fd] == nfp) {
340 fdp->fd_ofiles[fd] = NULL;
347 * Release explicitly held references before returning.
359 struct accept_args *uap;
362 return (accept1(p, uap, 0));
365 #ifdef COMPAT_OLDSOCK
369 struct accept_args *uap;
372 return (accept1(p, uap, 1));
374 #endif /* COMPAT_OLDSOCK */
380 register struct connect_args /* {
387 register struct socket *so;
391 error = holdsock(p->p_fd, uap->s, &fp);
394 so = (struct socket *)fp->f_data;
395 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
399 error = getsockaddr(&sa, uap->name, uap->namelen);
402 error = soconnect(so, sa, p);
405 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
411 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
412 error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
418 error = so->so_error;
423 so->so_state &= ~SS_ISCONNECTING;
425 if (error == ERESTART)
435 register struct socketpair_args /* {
442 register struct filedesc *fdp = p->p_fd;
443 struct file *fp1, *fp2;
444 struct socket *so1, *so2;
445 int fd, error, sv[2];
447 error = socreate(uap->domain, &so1, uap->type, uap->protocol, p);
450 error = socreate(uap->domain, &so2, uap->type, uap->protocol, p);
453 error = falloc(p, &fp1, &fd);
458 fp1->f_data = (caddr_t)so1;
459 error = falloc(p, &fp2, &fd);
463 fp2->f_data = (caddr_t)so2;
465 error = soconnect2(so1, so2);
468 if (uap->type == SOCK_DGRAM) {
470 * Datagram socket connection is asymmetric.
472 error = soconnect2(so2, so1);
476 fp1->f_flag = fp2->f_flag = FREAD|FWRITE;
477 fp1->f_ops = fp2->f_ops = &socketops;
478 fp1->f_type = fp2->f_type = DTYPE_SOCKET;
479 error = copyout((caddr_t)sv, (caddr_t)uap->rsv, 2 * sizeof (int));
484 if (fdp->fd_ofiles[sv[1]] == fp2) {
485 fdp->fd_ofiles[sv[1]] = NULL;
490 if (fdp->fd_ofiles[sv[0]] == fp1) {
491 fdp->fd_ofiles[sv[0]] = NULL;
503 sendit(p, s, mp, flags)
504 register struct proc *p;
506 register struct msghdr *mp;
511 register struct iovec *iov;
513 struct mbuf *control;
518 struct iovec *ktriov = NULL;
522 error = holdsock(p->p_fd, s, &fp);
525 auio.uio_iov = mp->msg_iov;
526 auio.uio_iovcnt = mp->msg_iovlen;
527 auio.uio_segflg = UIO_USERSPACE;
528 auio.uio_rw = UIO_WRITE;
530 auio.uio_offset = 0; /* XXX */
533 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
534 if ((auio.uio_resid += iov->iov_len) < 0) {
540 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
548 if (mp->msg_control) {
549 if (mp->msg_controllen < sizeof(struct cmsghdr)
550 #ifdef COMPAT_OLDSOCK
551 && mp->msg_flags != MSG_COMPAT
557 error = sockargs(&control, mp->msg_control,
558 mp->msg_controllen, MT_CONTROL);
561 #ifdef COMPAT_OLDSOCK
562 if (mp->msg_flags == MSG_COMPAT) {
563 register struct cmsghdr *cm;
565 M_PREPEND(control, sizeof(*cm), M_WAIT);
570 cm = mtod(control, struct cmsghdr *);
571 cm->cmsg_len = control->m_len;
572 cm->cmsg_level = SOL_SOCKET;
573 cm->cmsg_type = SCM_RIGHTS;
581 if (KTRPOINT(p, KTR_GENIO)) {
582 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
584 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
585 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
589 len = auio.uio_resid;
590 so = (struct socket *)fp->f_data;
591 error = so->so_proto->pr_usrreqs->pru_sosend(so, to, &auio, 0, control,
594 if (auio.uio_resid != len && (error == ERESTART ||
595 error == EINTR || error == EWOULDBLOCK))
601 p->p_retval[0] = len - auio.uio_resid;
603 if (ktriov != NULL) {
605 ktruio.uio_iov = ktriov;
606 ktruio.uio_resid = p->p_retval[0];
607 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error);
609 FREE(ktriov, M_TEMP);
622 register struct sendto_args /* {
634 msg.msg_name = uap->to;
635 msg.msg_namelen = uap->tolen;
639 #ifdef COMPAT_OLDSOCK
642 aiov.iov_base = uap->buf;
643 aiov.iov_len = uap->len;
644 return (sendit(p, uap->s, &msg, uap->flags));
647 #ifdef COMPAT_OLDSOCK
651 register struct osend_args /* {
665 aiov.iov_base = uap->buf;
666 aiov.iov_len = uap->len;
669 return (sendit(p, uap->s, &msg, uap->flags));
675 register struct osendmsg_args /* {
682 struct iovec aiov[UIO_SMALLIOV], *iov;
685 error = copyin(uap->msg, (caddr_t)&msg, sizeof (struct omsghdr));
688 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
689 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
691 MALLOC(iov, struct iovec *,
692 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
696 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
697 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
700 msg.msg_flags = MSG_COMPAT;
702 error = sendit(p, uap->s, &msg, uap->flags);
713 register struct sendmsg_args /* {
720 struct iovec aiov[UIO_SMALLIOV], *iov;
723 error = copyin(uap->msg, (caddr_t)&msg, sizeof (msg));
726 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
727 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
729 MALLOC(iov, struct iovec *,
730 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
734 if (msg.msg_iovlen &&
735 (error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
736 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)))))
739 #ifdef COMPAT_OLDSOCK
742 error = sendit(p, uap->s, &msg, uap->flags);
750 recvit(p, s, mp, namelenp)
751 register struct proc *p;
753 register struct msghdr *mp;
758 register struct iovec *iov;
761 struct mbuf *m, *control = 0;
764 struct sockaddr *fromsa = 0;
766 struct iovec *ktriov = NULL;
770 error = holdsock(p->p_fd, s, &fp);
773 auio.uio_iov = mp->msg_iov;
774 auio.uio_iovcnt = mp->msg_iovlen;
775 auio.uio_segflg = UIO_USERSPACE;
776 auio.uio_rw = UIO_READ;
778 auio.uio_offset = 0; /* XXX */
781 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
782 if ((auio.uio_resid += iov->iov_len) < 0) {
788 if (KTRPOINT(p, KTR_GENIO)) {
789 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
791 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
792 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
796 len = auio.uio_resid;
797 so = (struct socket *)fp->f_data;
798 error = so->so_proto->pr_usrreqs->pru_soreceive(so, &fromsa, &auio,
799 (struct mbuf **)0, mp->msg_control ? &control : (struct mbuf **)0,
802 if (auio.uio_resid != len && (error == ERESTART ||
803 error == EINTR || error == EWOULDBLOCK))
807 if (ktriov != NULL) {
809 ktruio.uio_iov = ktriov;
810 ktruio.uio_resid = len - auio.uio_resid;
811 ktrgenio(p->p_tracep, s, UIO_READ, &ktruio, error);
813 FREE(ktriov, M_TEMP);
818 p->p_retval[0] = len - auio.uio_resid;
820 len = mp->msg_namelen;
821 if (len <= 0 || fromsa == 0)
825 #define MIN(a,b) ((a)>(b)?(b):(a))
827 /* save sa_len before it is destroyed by MSG_COMPAT */
828 len = MIN(len, fromsa->sa_len);
829 #ifdef COMPAT_OLDSOCK
830 if (mp->msg_flags & MSG_COMPAT)
831 ((struct osockaddr *)fromsa)->sa_family =
834 error = copyout(fromsa,
835 (caddr_t)mp->msg_name, (unsigned)len);
839 mp->msg_namelen = len;
841 (error = copyout((caddr_t)&len, namelenp, sizeof (int)))) {
842 #ifdef COMPAT_OLDSOCK
843 if (mp->msg_flags & MSG_COMPAT)
844 error = 0; /* old recvfrom didn't check */
850 if (mp->msg_control) {
851 #ifdef COMPAT_OLDSOCK
853 * We assume that old recvmsg calls won't receive access
854 * rights and other control info, esp. as control info
855 * is always optional and those options didn't exist in 4.3.
856 * If we receive rights, trim the cmsghdr; anything else
859 if (control && mp->msg_flags & MSG_COMPAT) {
860 if (mtod(control, struct cmsghdr *)->cmsg_level !=
862 mtod(control, struct cmsghdr *)->cmsg_type !=
864 mp->msg_controllen = 0;
867 control->m_len -= sizeof (struct cmsghdr);
868 control->m_data += sizeof (struct cmsghdr);
871 len = mp->msg_controllen;
873 mp->msg_controllen = 0;
874 ctlbuf = (caddr_t) mp->msg_control;
876 while (m && len > 0) {
882 mp->msg_flags |= MSG_CTRUNC;
886 if ((error = copyout((caddr_t)mtod(m, caddr_t),
887 ctlbuf, tocopy)) != 0)
894 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
899 FREE(fromsa, M_SONAME);
908 register struct recvfrom_args /* {
921 if (uap->fromlenaddr) {
922 error = copyin((caddr_t)uap->fromlenaddr,
923 (caddr_t)&msg.msg_namelen, sizeof (msg.msg_namelen));
928 msg.msg_name = uap->from;
931 aiov.iov_base = uap->buf;
932 aiov.iov_len = uap->len;
934 msg.msg_flags = uap->flags;
935 return (recvit(p, uap->s, &msg, (caddr_t)uap->fromlenaddr));
938 #ifdef COMPAT_OLDSOCK
942 struct recvfrom_args *uap;
945 uap->flags |= MSG_COMPAT;
946 return (recvfrom(p, uap));
951 #ifdef COMPAT_OLDSOCK
955 register struct orecv_args /* {
969 aiov.iov_base = uap->buf;
970 aiov.iov_len = uap->len;
972 msg.msg_flags = uap->flags;
973 return (recvit(p, uap->s, &msg, (caddr_t)0));
977 * Old recvmsg. This code takes advantage of the fact that the old msghdr
978 * overlays the new one, missing only the flags, and with the (old) access
979 * rights where the control fields are now.
984 register struct orecvmsg_args /* {
991 struct iovec aiov[UIO_SMALLIOV], *iov;
994 error = copyin((caddr_t)uap->msg, (caddr_t)&msg,
995 sizeof (struct omsghdr));
998 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
999 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
1001 MALLOC(iov, struct iovec *,
1002 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
1006 msg.msg_flags = uap->flags | MSG_COMPAT;
1007 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
1008 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
1012 error = recvit(p, uap->s, &msg, (caddr_t)&uap->msg->msg_namelen);
1014 if (msg.msg_controllen && error == 0)
1015 error = copyout((caddr_t)&msg.msg_controllen,
1016 (caddr_t)&uap->msg->msg_accrightslen, sizeof (int));
1027 register struct recvmsg_args /* {
1034 struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
1037 error = copyin((caddr_t)uap->msg, (caddr_t)&msg, sizeof (msg));
1040 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
1041 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
1043 MALLOC(iov, struct iovec *,
1044 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
1048 #ifdef COMPAT_OLDSOCK
1049 msg.msg_flags = uap->flags &~ MSG_COMPAT;
1051 msg.msg_flags = uap->flags;
1055 error = copyin((caddr_t)uiov, (caddr_t)iov,
1056 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
1059 error = recvit(p, uap->s, &msg, (caddr_t)0);
1062 error = copyout((caddr_t)&msg, (caddr_t)uap->msg, sizeof(msg));
1074 register struct shutdown_args /* {
1082 error = holdsock(p->p_fd, uap->s, &fp);
1085 error = soshutdown((struct socket *)fp->f_data, uap->how);
1094 register struct setsockopt_args /* {
1103 struct sockopt sopt;
1106 if (uap->val == 0 && uap->valsize != 0)
1108 if (uap->valsize < 0)
1111 error = holdsock(p->p_fd, uap->s, &fp);
1115 sopt.sopt_dir = SOPT_SET;
1116 sopt.sopt_level = uap->level;
1117 sopt.sopt_name = uap->name;
1118 sopt.sopt_val = uap->val;
1119 sopt.sopt_valsize = uap->valsize;
1121 error = sosetopt((struct socket *)fp->f_data, &sopt);
1130 register struct getsockopt_args /* {
1140 struct sockopt sopt;
1142 error = holdsock(p->p_fd, uap->s, &fp);
1146 error = copyin((caddr_t)uap->avalsize, (caddr_t)&valsize,
1160 sopt.sopt_dir = SOPT_GET;
1161 sopt.sopt_level = uap->level;
1162 sopt.sopt_name = uap->name;
1163 sopt.sopt_val = uap->val;
1164 sopt.sopt_valsize = (size_t)valsize; /* checked non-negative above */
1167 error = sogetopt((struct socket *)fp->f_data, &sopt);
1169 valsize = sopt.sopt_valsize;
1170 error = copyout((caddr_t)&valsize,
1171 (caddr_t)uap->avalsize, sizeof (valsize));
1182 getsockname1(p, uap, compat)
1184 register struct getsockname_args /* {
1192 register struct socket *so;
1193 struct sockaddr *sa;
1196 error = holdsock(p->p_fd, uap->fdes, &fp);
1199 error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len));
1208 so = (struct socket *)fp->f_data;
1210 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
1218 len = MIN(len, sa->sa_len);
1219 #ifdef COMPAT_OLDSOCK
1221 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1223 error = copyout(sa, (caddr_t)uap->asa, (u_int)len);
1226 error = copyout((caddr_t)&len, (caddr_t)uap->alen,
1238 struct getsockname_args *uap;
1241 return (getsockname1(p, uap, 0));
1244 #ifdef COMPAT_OLDSOCK
1246 ogetsockname(p, uap)
1248 struct getsockname_args *uap;
1251 return (getsockname1(p, uap, 1));
1253 #endif /* COMPAT_OLDSOCK */
1256 * Get name of peer for connected socket.
1260 getpeername1(p, uap, compat)
1262 register struct getpeername_args /* {
1270 register struct socket *so;
1271 struct sockaddr *sa;
1274 error = holdsock(p->p_fd, uap->fdes, &fp);
1277 so = (struct socket *)fp->f_data;
1278 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1282 error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len));
1292 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa);
1299 len = MIN(len, sa->sa_len);
1300 #ifdef COMPAT_OLDSOCK
1302 ((struct osockaddr *)sa)->sa_family =
1305 error = copyout(sa, (caddr_t)uap->asa, (u_int)len);
1309 error = copyout((caddr_t)&len, (caddr_t)uap->alen, sizeof (len));
1320 struct getpeername_args *uap;
1323 return (getpeername1(p, uap, 0));
1326 #ifdef COMPAT_OLDSOCK
1328 ogetpeername(p, uap)
1330 struct ogetpeername_args *uap;
1333 /* XXX uap should have type `getpeername_args *' to begin with. */
1334 return (getpeername1(p, (struct getpeername_args *)uap, 1));
1336 #endif /* COMPAT_OLDSOCK */
1339 sockargs(mp, buf, buflen, type)
1344 register struct sockaddr *sa;
1345 register struct mbuf *m;
1348 if ((u_int)buflen > MLEN) {
1349 #ifdef COMPAT_OLDSOCK
1350 if (type == MT_SONAME && (u_int)buflen <= 112)
1351 buflen = MLEN; /* unix domain compat. hack */
1356 m = m_get(M_WAIT, type);
1360 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1365 if (type == MT_SONAME) {
1366 sa = mtod(m, struct sockaddr *);
1368 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1369 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1370 sa->sa_family = sa->sa_len;
1372 sa->sa_len = buflen;
1379 getsockaddr(namp, uaddr, len)
1380 struct sockaddr **namp;
1384 struct sockaddr *sa;
1387 if (len > SOCK_MAXADDRLEN)
1388 return ENAMETOOLONG;
1389 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1390 error = copyin(uaddr, sa, len);
1394 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1395 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1396 sa->sa_family = sa->sa_len;
1405 * holdsock() - load the struct file pointer associated
1406 * with a socket into *fpp. If an error occurs, non-zero
1407 * will be returned and *fpp will be set to NULL.
1410 holdsock(fdp, fdes, fpp)
1411 struct filedesc *fdp;
1415 register struct file *fp = NULL;
1418 if ((unsigned)fdes >= fdp->fd_nfiles ||
1419 (fp = fdp->fd_ofiles[fdes]) == NULL) {
1421 } else if (fp->f_type != DTYPE_SOCKET) {
1432 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1435 sf_buf_init(void *arg)
1439 SLIST_INIT(&sf_freelist);
1440 sf_base = kmem_alloc_pageable(kernel_map, nsfbufs * PAGE_SIZE);
1441 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, M_NOWAIT);
1442 bzero(sf_bufs, nsfbufs * sizeof(struct sf_buf));
1443 for (i = 0; i < nsfbufs; i++) {
1444 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
1445 SLIST_INSERT_HEAD(&sf_freelist, &sf_bufs[i], free_list);
1450 * Get an sf_buf from the freelist. Will block if none are available.
1460 while ((sf = SLIST_FIRST(&sf_freelist)) == NULL) {
1461 sf_buf_alloc_want = 1;
1462 error = tsleep(&sf_freelist, PVM|PCATCH, "sfbufa", 0);
1467 SLIST_REMOVE_HEAD(&sf_freelist, free_list);
1474 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1476 sf_buf_ref(caddr_t addr, u_int size)
1481 if (sf->refcnt == 0)
1482 panic("sf_buf_ref: referencing a free sf_buf");
1487 * Lose a reference to an sf_buf. When none left, detach mapped page
1488 * and release resources back to the system.
1490 * Must be called at splimp.
1493 sf_buf_free(caddr_t addr, u_int size)
1500 if (sf->refcnt == 0)
1501 panic("sf_buf_free: freeing free sf_buf");
1503 if (sf->refcnt == 0) {
1504 pmap_qremove((vm_offset_t)addr, 1);
1507 vm_page_unwire(m, 0);
1509 * Check for the object going away on us. This can
1510 * happen since we don't hold a reference to it.
1511 * If so, we're responsible for freeing the page.
1513 if (m->wire_count == 0 && m->object == NULL)
1517 SLIST_INSERT_HEAD(&sf_freelist, sf, free_list);
1518 if (sf_buf_alloc_want) {
1519 sf_buf_alloc_want = 0;
1520 wakeup(&sf_freelist);
1527 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1528 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1530 * Send a file specified by 'fd' and starting at 'offset' to a socket
1531 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1532 * nbytes == 0. Optionally add a header and/or trailer to the socket
1533 * output. If specified, write the total number of bytes sent into *sbytes.
1536 sendfile(struct proc *p, struct sendfile_args *uap)
1539 return (do_sendfile(p, uap, 0));
1544 osendfile(struct proc *p, struct osendfile_args *uap)
1546 struct sendfile_args args;
1550 args.offset = uap->offset;
1551 args.nbytes = uap->nbytes;
1552 args.hdtr = uap->hdtr;
1553 args.sbytes = uap->sbytes;
1554 args.flags = uap->flags;
1556 return (do_sendfile(p, &args, 1));
1561 do_sendfile(struct proc *p, struct sendfile_args *uap, int compat)
1564 struct filedesc *fdp = p->p_fd;
1566 struct vm_object *obj;
1571 struct writev_args nuap;
1572 struct sf_hdtr hdtr;
1573 off_t off, xfsize, hdtr_size, sbytes = 0;
1579 * Do argument checking. Must be a regular file in, stream
1580 * type and connected socket out, positive offset.
1582 fp = holdfp(fdp, uap->fd, FREAD);
1587 if (fp->f_type != DTYPE_VNODE) {
1591 vp = (struct vnode *)fp->f_data;
1593 if (vp->v_type != VREG || VOP_GETVOBJECT(vp, &obj) != 0) {
1598 error = holdsock(p->p_fd, uap->s, &fp);
1601 so = (struct socket *)fp->f_data;
1602 if (so->so_type != SOCK_STREAM) {
1606 if ((so->so_state & SS_ISCONNECTED) == 0) {
1610 if (uap->offset < 0) {
1616 * If specified, get the pointer to the sf_hdtr struct for
1617 * any headers/trailers.
1619 if (uap->hdtr != NULL) {
1620 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1624 * Send any headers. Wimp out and use writev(2).
1626 if (hdtr.headers != NULL) {
1628 nuap.iovp = hdtr.headers;
1629 nuap.iovcnt = hdtr.hdr_cnt;
1630 error = writev(p, &nuap);
1634 sbytes += p->p_retval[0];
1636 hdtr_size += p->p_retval[0];
1641 * Protect against multiple writers to the socket.
1643 (void) sblock(&so->so_snd, M_WAITOK);
1646 * Loop through the pages in the file, starting with the requested
1647 * offset. Get a file page (do I/O if necessary), map the file page
1648 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1651 for (off = uap->offset; ; off += xfsize, sbytes += xfsize) {
1655 pindex = OFF_TO_IDX(off);
1658 * Calculate the amount to transfer. Not to exceed a page,
1659 * the EOF, or the passed in nbytes.
1661 xfsize = obj->un_pager.vnp.vnp_size - off;
1662 if (xfsize > PAGE_SIZE)
1664 pgoff = (vm_offset_t)(off & PAGE_MASK);
1665 if (PAGE_SIZE - pgoff < xfsize)
1666 xfsize = PAGE_SIZE - pgoff;
1667 if (uap->nbytes && xfsize > (uap->nbytes - sbytes))
1668 xfsize = uap->nbytes - sbytes;
1672 * Optimize the non-blocking case by looking at the socket space
1673 * before going to the extra work of constituting the sf_buf.
1675 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1676 if (so->so_state & SS_CANTSENDMORE)
1680 sbunlock(&so->so_snd);
1684 * Attempt to look up the page.
1686 * Allocate if not found
1688 * Wait and loop if busy.
1690 pg = vm_page_lookup(obj, pindex);
1693 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL);
1699 } else if (vm_page_sleep_busy(pg, TRUE, "sfpbsy")) {
1704 * Wire the page so it does not get ripped out from under
1711 * If page is not valid for what we need, initiate I/O
1714 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1720 * Ensure that our page is still around when the I/O
1723 vm_page_io_start(pg);
1726 * Get the page from backing store.
1728 bsize = vp->v_mount->mnt_stat.f_iosize;
1729 auio.uio_iov = &aiov;
1730 auio.uio_iovcnt = 1;
1732 aiov.iov_len = MAXBSIZE;
1733 auio.uio_resid = MAXBSIZE;
1734 auio.uio_offset = trunc_page(off);
1735 auio.uio_segflg = UIO_NOCOPY;
1736 auio.uio_rw = UIO_READ;
1738 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p);
1739 error = VOP_READ(vp, &auio, IO_VMIO | ((MAXBSIZE / bsize) << 16),
1741 VOP_UNLOCK(vp, 0, p);
1742 vm_page_flag_clear(pg, PG_ZERO);
1743 vm_page_io_finish(pg);
1745 vm_page_unwire(pg, 0);
1747 * See if anyone else might know about this page.
1748 * If not and it is not valid, then free it.
1750 if (pg->wire_count == 0 && pg->valid == 0 &&
1751 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1752 pg->hold_count == 0) {
1756 sbunlock(&so->so_snd);
1763 * Get a sendfile buf. We usually wait as long as necessary,
1764 * but this wait can be interrupted.
1766 if ((sf = sf_buf_alloc()) == NULL) {
1768 vm_page_unwire(pg, 0);
1769 if (pg->wire_count == 0 && pg->object == NULL)
1772 sbunlock(&so->so_snd);
1779 * Allocate a kernel virtual page and insert the physical page
1784 pmap_qenter(sf->kva, &pg, 1);
1786 * Get an mbuf header and set it up as having external storage.
1788 MGETHDR(m, M_WAIT, MT_DATA);
1791 sf_buf_free((void *)sf->kva, PAGE_SIZE);
1792 sbunlock(&so->so_snd);
1795 m->m_ext.ext_free = sf_buf_free;
1796 m->m_ext.ext_ref = sf_buf_ref;
1797 m->m_ext.ext_buf = (void *)sf->kva;
1798 m->m_ext.ext_size = PAGE_SIZE;
1799 m->m_data = (char *) sf->kva + pgoff;
1800 m->m_flags |= M_EXT;
1801 m->m_pkthdr.len = m->m_len = xfsize;
1803 * Add the buffer to the socket buffer chain.
1808 * Make sure that the socket is still able to take more data.
1809 * CANTSENDMORE being true usually means that the connection
1810 * was closed. so_error is true when an error was sensed after
1812 * The state is checked after the page mapping and buffer
1813 * allocation above since those operations may block and make
1814 * any socket checks stale. From this point forward, nothing
1815 * blocks before the pru_send (or more accurately, any blocking
1816 * results in a loop back to here to re-check).
1818 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1819 if (so->so_state & SS_CANTSENDMORE) {
1822 error = so->so_error;
1826 sbunlock(&so->so_snd);
1831 * Wait for socket space to become available. We do this just
1832 * after checking the connection state above in order to avoid
1833 * a race condition with sbwait().
1835 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
1836 if (so->so_state & SS_NBIO) {
1838 sbunlock(&so->so_snd);
1843 error = sbwait(&so->so_snd);
1845 * An error from sbwait usually indicates that we've
1846 * been interrupted by a signal. If we've sent anything
1847 * then return bytes sent, otherwise return the error.
1851 sbunlock(&so->so_snd);
1857 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, p);
1860 sbunlock(&so->so_snd);
1864 sbunlock(&so->so_snd);
1867 * Send trailers. Wimp out and use writev(2).
1869 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1871 nuap.iovp = hdtr.trailers;
1872 nuap.iovcnt = hdtr.trl_cnt;
1873 error = writev(p, &nuap);
1877 sbytes += p->p_retval[0];
1879 hdtr_size += p->p_retval[0];
1883 if (uap->sbytes != NULL) {
1885 sbytes += hdtr_size;
1886 copyout(&sbytes, uap->sbytes, sizeof(off_t));