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
40 #include "opt_compat.h"
41 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
49 #include <sys/mutex.h>
50 #include <sys/sysproto.h>
51 #include <sys/malloc.h>
52 #include <sys/filedesc.h>
53 #include <sys/event.h>
55 #include <sys/fcntl.h>
58 #include <sys/mount.h>
60 #include <sys/protosw.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/signalvar.h>
65 #include <sys/vnode.h>
67 #include <sys/ktrace.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_pageout.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_extern.h>
77 static void sf_buf_init(void *arg);
78 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
80 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
81 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
83 static int accept1(struct thread *td, struct accept_args *uap, int compat);
84 static int do_sendfile(struct thread *td, struct sendfile_args *uap, int compat);
85 static int getsockname1(struct thread *td, struct getsockname_args *uap,
87 static int getpeername1(struct thread *td, struct getpeername_args *uap,
91 * Expanded sf_freelist head. Really an SLIST_HEAD() in disguise, with the
92 * sf_freelist head with the sf_lock mutex.
95 SLIST_HEAD(, sf_buf) sf_head;
100 struct sf_buf *sf_bufs;
101 u_int sf_buf_alloc_want;
104 * System call interface to the socket abstraction.
106 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
107 #define COMPAT_OLDSOCK
110 extern struct fileops socketops;
118 register struct socket_args /* {
124 struct filedesc *fdp;
130 fdp = td->td_proc->p_fd;
131 error = falloc(td, &fp, &fd);
135 error = socreate(uap->domain, &so, uap->type, uap->protocol,
139 if (fdp->fd_ofiles[fd] == fp) {
140 fdp->fd_ofiles[fd] = NULL;
141 FILEDESC_UNLOCK(fdp);
144 FILEDESC_UNLOCK(fdp);
146 fp->f_data = so; /* already has ref count */
147 fp->f_flag = FREAD|FWRITE;
148 fp->f_ops = &socketops;
149 fp->f_type = DTYPE_SOCKET;
150 FILEDESC_UNLOCK(fdp);
151 td->td_retval[0] = fd;
166 register struct bind_args /* {
177 if ((error = fgetsock(td, uap->s, &so, NULL)) != 0)
179 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
182 error = mac_check_socket_bind(td->td_ucred, so, sa);
188 error = sobind(so, sa, td);
204 register struct listen_args /* {
213 if ((error = fgetsock(td, uap->s, &so, NULL)) == 0) {
215 error = mac_check_socket_listen(td->td_ucred, so);
219 error = solisten(so, uap->backlog, td);
234 accept1(td, uap, compat)
236 register struct accept_args /* {
243 struct filedesc *fdp;
244 struct file *nfp = NULL;
246 int namelen, error, s;
247 struct socket *head, *so;
253 fdp = td->td_proc->p_fd;
255 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
263 error = fgetsock(td, uap->s, &head, &fflag);
267 if ((head->so_options & SO_ACCEPTCONN) == 0) {
272 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
273 if (head->so_state & SS_CANTRCVMORE) {
274 head->so_error = ECONNABORTED;
277 if ((head->so_state & SS_NBIO) != 0) {
278 head->so_error = EWOULDBLOCK;
281 error = tsleep(&head->so_timeo, PSOCK | PCATCH,
288 if (head->so_error) {
289 error = head->so_error;
296 * At this point we know that there is at least one connection
297 * ready to be accepted. Remove it from the queue prior to
298 * allocating the file descriptor for it since falloc() may
299 * block allowing another process to accept the connection
302 so = TAILQ_FIRST(&head->so_comp);
303 TAILQ_REMOVE(&head->so_comp, so, so_list);
306 error = falloc(td, &nfp, &fd);
309 * Probably ran out of file descriptors. Put the
310 * unaccepted connection back onto the queue and
311 * do another wakeup so some other process might
312 * have a chance at it.
314 TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
316 wakeup_one(&head->so_timeo);
321 td->td_retval[0] = fd;
323 /* connection has been removed from the listen queue */
324 KNOTE(&head->so_rcv.sb_sel.si_note, 0);
326 so->so_state &= ~SS_COMP;
328 pgid = fgetown(&head->so_sigio);
330 fsetown(pgid, &so->so_sigio);
333 soref(so); /* file descriptor reference */
334 nfp->f_data = so; /* nfp has ref count from falloc */
336 nfp->f_ops = &socketops;
337 nfp->f_type = DTYPE_SOCKET;
340 error = soaccept(so, &sa);
343 * return a namelen of zero for older code which might
344 * ignore the return value from accept.
346 if (uap->name != NULL) {
348 (void) copyout(&namelen,
349 uap->anamelen, sizeof(*uap->anamelen));
362 /* check sa_len before it is destroyed */
363 if (namelen > sa->sa_len)
364 namelen = sa->sa_len;
365 #ifdef COMPAT_OLDSOCK
367 ((struct osockaddr *)sa)->sa_family =
370 error = copyout(sa, uap->name, (u_int)namelen);
373 error = copyout(&namelen,
374 uap->anamelen, sizeof (*uap->anamelen));
381 * close the new descriptor, assuming someone hasn't ripped it
386 if (fdp->fd_ofiles[fd] == nfp) {
387 fdp->fd_ofiles[fd] = NULL;
388 FILEDESC_UNLOCK(fdp);
391 FILEDESC_UNLOCK(fdp);
397 * Release explicitly held references before returning.
409 * MPSAFE (accept1() is MPSAFE)
414 struct accept_args *uap;
417 return (accept1(td, uap, 0));
420 #ifdef COMPAT_OLDSOCK
422 * MPSAFE (accept1() is MPSAFE)
427 struct accept_args *uap;
430 return (accept1(td, uap, 1));
432 #endif /* COMPAT_OLDSOCK */
441 register struct connect_args /* {
452 if ((error = fgetsock(td, uap->s, &so, NULL)) != 0)
454 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
458 error = getsockaddr(&sa, uap->name, uap->namelen);
462 error = mac_check_socket_connect(td->td_ucred, so, sa);
466 error = soconnect(so, sa, td);
469 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
475 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
476 error = tsleep(&so->so_timeo, PSOCK | PCATCH, "connec", 0);
481 error = so->so_error;
486 so->so_state &= ~SS_ISCONNECTING;
488 if (error == ERESTART)
503 register struct socketpair_args /* {
510 register struct filedesc *fdp = td->td_proc->p_fd;
511 struct file *fp1, *fp2;
512 struct socket *so1, *so2;
513 int fd, error, sv[2];
516 error = socreate(uap->domain, &so1, uap->type, uap->protocol,
520 error = socreate(uap->domain, &so2, uap->type, uap->protocol,
524 error = falloc(td, &fp1, &fd);
529 fp1->f_data = so1; /* so1 already has ref count */
530 error = falloc(td, &fp2, &fd);
534 fp2->f_data = so2; /* so2 already has ref count */
536 error = soconnect2(so1, so2);
539 if (uap->type == SOCK_DGRAM) {
541 * Datagram socket connection is asymmetric.
543 error = soconnect2(so2, so1);
548 fp1->f_flag = FREAD|FWRITE;
549 fp1->f_ops = &socketops;
550 fp1->f_type = DTYPE_SOCKET;
553 fp2->f_flag = FREAD|FWRITE;
554 fp2->f_ops = &socketops;
555 fp2->f_type = DTYPE_SOCKET;
557 error = copyout(sv, uap->rsv, 2 * sizeof (int));
563 if (fdp->fd_ofiles[sv[1]] == fp2) {
564 fdp->fd_ofiles[sv[1]] = NULL;
565 FILEDESC_UNLOCK(fdp);
568 FILEDESC_UNLOCK(fdp);
572 if (fdp->fd_ofiles[sv[0]] == fp1) {
573 fdp->fd_ofiles[sv[0]] = NULL;
574 FILEDESC_UNLOCK(fdp);
577 FILEDESC_UNLOCK(fdp);
589 sendit(td, s, mp, flags)
590 register struct thread *td;
592 register struct msghdr *mp;
596 register struct iovec *iov;
598 struct mbuf *control;
599 struct sockaddr *to = NULL;
603 struct iovec *ktriov = NULL;
608 if ((error = fgetsock(td, s, &so, NULL)) != 0)
612 error = mac_check_socket_send(td->td_ucred, so);
617 auio.uio_iov = mp->msg_iov;
618 auio.uio_iovcnt = mp->msg_iovlen;
619 auio.uio_segflg = UIO_USERSPACE;
620 auio.uio_rw = UIO_WRITE;
622 auio.uio_offset = 0; /* XXX */
625 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
626 if ((auio.uio_resid += iov->iov_len) < 0) {
632 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
636 if (mp->msg_control) {
637 if (mp->msg_controllen < sizeof(struct cmsghdr)
638 #ifdef COMPAT_OLDSOCK
639 && mp->msg_flags != MSG_COMPAT
645 error = sockargs(&control, mp->msg_control,
646 mp->msg_controllen, MT_CONTROL);
649 #ifdef COMPAT_OLDSOCK
650 if (mp->msg_flags == MSG_COMPAT) {
651 register struct cmsghdr *cm;
653 M_PREPEND(control, sizeof(*cm), M_TRYWAIT);
658 cm = mtod(control, struct cmsghdr *);
659 cm->cmsg_len = control->m_len;
660 cm->cmsg_level = SOL_SOCKET;
661 cm->cmsg_type = SCM_RIGHTS;
669 if (KTRPOINT(td, KTR_GENIO)) {
670 iovlen = auio.uio_iovcnt * sizeof (struct iovec);
671 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
672 bcopy(auio.uio_iov, ktriov, iovlen);
676 len = auio.uio_resid;
677 error = so->so_proto->pr_usrreqs->pru_sosend(so, to, &auio, 0, control,
680 if (auio.uio_resid != len && (error == ERESTART ||
681 error == EINTR || error == EWOULDBLOCK))
683 /* Generation of SIGPIPE can be controlled per socket */
684 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE)) {
685 PROC_LOCK(td->td_proc);
686 psignal(td->td_proc, SIGPIPE);
687 PROC_UNLOCK(td->td_proc);
691 td->td_retval[0] = len - auio.uio_resid;
693 if (ktriov != NULL) {
695 ktruio.uio_iov = ktriov;
696 ktruio.uio_resid = td->td_retval[0];
697 ktrgenio(s, UIO_WRITE, &ktruio, error);
699 FREE(ktriov, M_TEMP);
715 register struct sendto_args /* {
728 msg.msg_name = uap->to;
729 msg.msg_namelen = uap->tolen;
733 #ifdef COMPAT_OLDSOCK
736 aiov.iov_base = uap->buf;
737 aiov.iov_len = uap->len;
739 error = sendit(td, uap->s, &msg, uap->flags);
744 #ifdef COMPAT_OLDSOCK
751 register struct osend_args /* {
766 aiov.iov_base = uap->buf;
767 aiov.iov_len = uap->len;
771 error = sendit(td, uap->s, &msg, uap->flags);
782 register struct osendmsg_args /* {
789 struct iovec aiov[UIO_SMALLIOV], *iov;
793 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
796 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
797 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV) {
801 MALLOC(iov, struct iovec *,
802 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
807 error = copyin(msg.msg_iov, iov,
808 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
811 msg.msg_flags = MSG_COMPAT;
813 error = sendit(td, uap->s, &msg, uap->flags);
829 register struct sendmsg_args /* {
836 struct iovec aiov[UIO_SMALLIOV], *iov;
840 error = copyin(uap->msg, &msg, sizeof (msg));
843 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
844 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV) {
848 MALLOC(iov, struct iovec *,
849 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
854 if (msg.msg_iovlen &&
855 (error = copyin(msg.msg_iov, iov,
856 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)))))
859 #ifdef COMPAT_OLDSOCK
862 error = sendit(td, uap->s, &msg, uap->flags);
872 recvit(td, s, mp, namelenp)
873 register struct thread *td;
875 register struct msghdr *mp;
879 register struct iovec *iov;
882 struct mbuf *m, *control = 0;
885 struct sockaddr *fromsa = 0;
887 struct iovec *ktriov = NULL;
892 if ((error = fgetsock(td, s, &so, NULL)) != 0)
896 error = mac_check_socket_receive(td->td_ucred, so);
903 auio.uio_iov = mp->msg_iov;
904 auio.uio_iovcnt = mp->msg_iovlen;
905 auio.uio_segflg = UIO_USERSPACE;
906 auio.uio_rw = UIO_READ;
908 auio.uio_offset = 0; /* XXX */
911 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
912 if ((auio.uio_resid += iov->iov_len) < 0) {
918 if (KTRPOINT(td, KTR_GENIO)) {
919 iovlen = auio.uio_iovcnt * sizeof (struct iovec);
920 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
921 bcopy(auio.uio_iov, ktriov, iovlen);
925 len = auio.uio_resid;
926 error = so->so_proto->pr_usrreqs->pru_soreceive(so, &fromsa, &auio,
927 (struct mbuf **)0, mp->msg_control ? &control : (struct mbuf **)0,
930 if (auio.uio_resid != len && (error == ERESTART ||
931 error == EINTR || error == EWOULDBLOCK))
935 if (ktriov != NULL) {
937 ktruio.uio_iov = ktriov;
938 ktruio.uio_resid = len - auio.uio_resid;
939 ktrgenio(s, UIO_READ, &ktruio, error);
941 FREE(ktriov, M_TEMP);
946 td->td_retval[0] = len - auio.uio_resid;
948 len = mp->msg_namelen;
949 if (len <= 0 || fromsa == 0)
953 #define MIN(a,b) ((a)>(b)?(b):(a))
955 /* save sa_len before it is destroyed by MSG_COMPAT */
956 len = MIN(len, fromsa->sa_len);
957 #ifdef COMPAT_OLDSOCK
958 if (mp->msg_flags & MSG_COMPAT)
959 ((struct osockaddr *)fromsa)->sa_family =
962 error = copyout(fromsa, mp->msg_name, (unsigned)len);
966 mp->msg_namelen = len;
968 (error = copyout(&len, namelenp, sizeof (int)))) {
969 #ifdef COMPAT_OLDSOCK
970 if (mp->msg_flags & MSG_COMPAT)
971 error = 0; /* old recvfrom didn't check */
977 if (mp->msg_control) {
978 #ifdef COMPAT_OLDSOCK
980 * We assume that old recvmsg calls won't receive access
981 * rights and other control info, esp. as control info
982 * is always optional and those options didn't exist in 4.3.
983 * If we receive rights, trim the cmsghdr; anything else
986 if (control && mp->msg_flags & MSG_COMPAT) {
987 if (mtod(control, struct cmsghdr *)->cmsg_level !=
989 mtod(control, struct cmsghdr *)->cmsg_type !=
991 mp->msg_controllen = 0;
994 control->m_len -= sizeof (struct cmsghdr);
995 control->m_data += sizeof (struct cmsghdr);
998 len = mp->msg_controllen;
1000 mp->msg_controllen = 0;
1001 ctlbuf = mp->msg_control;
1003 while (m && len > 0) {
1004 unsigned int tocopy;
1006 if (len >= m->m_len)
1009 mp->msg_flags |= MSG_CTRUNC;
1013 if ((error = copyout(mtod(m, caddr_t),
1014 ctlbuf, tocopy)) != 0)
1021 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1026 FREE(fromsa, M_SONAME);
1038 register struct recvfrom_args /* {
1052 if (uap->fromlenaddr) {
1053 error = copyin(uap->fromlenaddr,
1054 &msg.msg_namelen, sizeof (msg.msg_namelen));
1058 msg.msg_namelen = 0;
1060 msg.msg_name = uap->from;
1061 msg.msg_iov = &aiov;
1063 aiov.iov_base = uap->buf;
1064 aiov.iov_len = uap->len;
1065 msg.msg_control = 0;
1066 msg.msg_flags = uap->flags;
1067 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1073 #ifdef COMPAT_OLDSOCK
1080 struct recvfrom_args *uap;
1083 uap->flags |= MSG_COMPAT;
1084 return (recvfrom(td, uap));
1089 #ifdef COMPAT_OLDSOCK
1096 register struct orecv_args /* {
1109 msg.msg_namelen = 0;
1110 msg.msg_iov = &aiov;
1112 aiov.iov_base = uap->buf;
1113 aiov.iov_len = uap->len;
1114 msg.msg_control = 0;
1115 msg.msg_flags = uap->flags;
1116 error = recvit(td, uap->s, &msg, NULL);
1122 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1123 * overlays the new one, missing only the flags, and with the (old) access
1124 * rights where the control fields are now.
1131 register struct orecvmsg_args /* {
1133 struct omsghdr *msg;
1138 struct iovec aiov[UIO_SMALLIOV], *iov;
1141 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1146 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
1147 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV) {
1151 MALLOC(iov, struct iovec *,
1152 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
1157 msg.msg_flags = uap->flags | MSG_COMPAT;
1158 error = copyin(msg.msg_iov, iov,
1159 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
1163 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1165 if (msg.msg_controllen && error == 0)
1166 error = copyout(&msg.msg_controllen,
1167 &uap->msg->msg_accrightslen, sizeof (int));
1183 register struct recvmsg_args /* {
1190 struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
1194 error = copyin(uap->msg, &msg, sizeof (msg));
1197 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
1198 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV) {
1202 MALLOC(iov, struct iovec *,
1203 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
1208 #ifdef COMPAT_OLDSOCK
1209 msg.msg_flags = uap->flags &~ MSG_COMPAT;
1211 msg.msg_flags = uap->flags;
1215 error = copyin(uiov, iov,
1216 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
1219 error = recvit(td, uap->s, &msg, NULL);
1222 error = copyout(&msg, uap->msg, sizeof(msg));
1239 register struct shutdown_args /* {
1248 if ((error = fgetsock(td, uap->s, &so, NULL)) == 0) {
1249 error = soshutdown(so, uap->how);
1263 register struct setsockopt_args /* {
1272 struct sockopt sopt;
1275 if (uap->val == 0 && uap->valsize != 0)
1277 if (uap->valsize < 0)
1281 if ((error = fgetsock(td, uap->s, &so, NULL)) == 0) {
1282 sopt.sopt_dir = SOPT_SET;
1283 sopt.sopt_level = uap->level;
1284 sopt.sopt_name = uap->name;
1285 sopt.sopt_val = uap->val;
1286 sopt.sopt_valsize = uap->valsize;
1288 error = sosetopt(so, &sopt);
1302 register struct getsockopt_args /* {
1312 struct sockopt sopt;
1315 if ((error = fgetsock(td, uap->s, &so, NULL)) != 0)
1318 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1329 sopt.sopt_dir = SOPT_GET;
1330 sopt.sopt_level = uap->level;
1331 sopt.sopt_name = uap->name;
1332 sopt.sopt_val = uap->val;
1333 sopt.sopt_valsize = (size_t)valsize; /* checked non-negative above */
1336 error = sogetopt(so, &sopt);
1338 valsize = sopt.sopt_valsize;
1339 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1349 * getsockname1() - Get socket name.
1355 getsockname1(td, uap, compat)
1357 register struct getsockname_args /* {
1365 struct sockaddr *sa;
1369 if ((error = fgetsock(td, uap->fdes, &so, NULL)) != 0)
1371 error = copyin(uap->alen, &len, sizeof (len));
1379 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
1387 len = MIN(len, sa->sa_len);
1388 #ifdef COMPAT_OLDSOCK
1390 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1392 error = copyout(sa, uap->asa, (u_int)len);
1395 error = copyout(&len, uap->alen, sizeof (len));
1410 getsockname(td, uap)
1412 struct getsockname_args *uap;
1415 return (getsockname1(td, uap, 0));
1418 #ifdef COMPAT_OLDSOCK
1423 ogetsockname(td, uap)
1425 struct getsockname_args *uap;
1428 return (getsockname1(td, uap, 1));
1430 #endif /* COMPAT_OLDSOCK */
1433 * getpeername1() - Get name of peer for connected socket.
1439 getpeername1(td, uap, compat)
1441 register struct getpeername_args /* {
1449 struct sockaddr *sa;
1453 if ((error = fgetsock(td, uap->fdes, &so, NULL)) != 0)
1455 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1459 error = copyin(uap->alen, &len, sizeof (len));
1467 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa);
1474 len = MIN(len, sa->sa_len);
1475 #ifdef COMPAT_OLDSOCK
1477 ((struct osockaddr *)sa)->sa_family =
1480 error = copyout(sa, uap->asa, (u_int)len);
1484 error = copyout(&len, uap->alen, sizeof (len));
1499 getpeername(td, uap)
1501 struct getpeername_args *uap;
1504 return (getpeername1(td, uap, 0));
1507 #ifdef COMPAT_OLDSOCK
1512 ogetpeername(td, uap)
1514 struct ogetpeername_args *uap;
1517 /* XXX uap should have type `getpeername_args *' to begin with. */
1518 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1520 #endif /* COMPAT_OLDSOCK */
1523 sockargs(mp, buf, buflen, type)
1528 register struct sockaddr *sa;
1529 register struct mbuf *m;
1532 if ((u_int)buflen > MLEN) {
1533 #ifdef COMPAT_OLDSOCK
1534 if (type == MT_SONAME && (u_int)buflen <= 112)
1535 buflen = MLEN; /* unix domain compat. hack */
1540 m = m_get(M_TRYWAIT, type);
1544 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1549 if (type == MT_SONAME) {
1550 sa = mtod(m, struct sockaddr *);
1552 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1553 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1554 sa->sa_family = sa->sa_len;
1556 sa->sa_len = buflen;
1563 getsockaddr(namp, uaddr, len)
1564 struct sockaddr **namp;
1568 struct sockaddr *sa;
1571 if (len > SOCK_MAXADDRLEN)
1572 return ENAMETOOLONG;
1573 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1574 error = copyin(uaddr, sa, len);
1578 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1579 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1580 sa->sa_family = sa->sa_len;
1589 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1592 sf_buf_init(void *arg)
1596 mtx_init(&sf_freelist.sf_lock, "sf_bufs list lock", NULL, MTX_DEF);
1597 mtx_lock(&sf_freelist.sf_lock);
1598 SLIST_INIT(&sf_freelist.sf_head);
1599 sf_base = kmem_alloc_pageable(kernel_map, nsfbufs * PAGE_SIZE);
1600 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
1602 for (i = 0; i < nsfbufs; i++) {
1603 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
1604 SLIST_INSERT_HEAD(&sf_freelist.sf_head, &sf_bufs[i], free_list);
1606 sf_buf_alloc_want = 0;
1607 mtx_unlock(&sf_freelist.sf_lock);
1611 * Get an sf_buf from the freelist. Will block if none are available.
1619 mtx_lock(&sf_freelist.sf_lock);
1620 while ((sf = SLIST_FIRST(&sf_freelist.sf_head)) == NULL) {
1621 sf_buf_alloc_want++;
1622 error = msleep(&sf_freelist, &sf_freelist.sf_lock, PVM|PCATCH,
1624 sf_buf_alloc_want--;
1627 * If we got a signal, don't risk going back to sleep.
1633 SLIST_REMOVE_HEAD(&sf_freelist.sf_head, free_list);
1634 mtx_unlock(&sf_freelist.sf_lock);
1638 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1641 * Detatch mapped page and release resources back to the system.
1644 sf_buf_free(void *addr, void *args)
1652 pmap_qremove((vm_offset_t)addr, 1);
1654 vm_page_lock_queues();
1655 vm_page_unwire(m, 0);
1657 * Check for the object going away on us. This can
1658 * happen since we don't hold a reference to it.
1659 * If so, we're responsible for freeing the page.
1661 if (m->wire_count == 0 && m->object == NULL)
1663 vm_page_unlock_queues();
1665 mtx_lock(&sf_freelist.sf_lock);
1666 SLIST_INSERT_HEAD(&sf_freelist.sf_head, sf, free_list);
1667 if (sf_buf_alloc_want > 0)
1668 wakeup_one(&sf_freelist);
1669 mtx_unlock(&sf_freelist.sf_lock);
1677 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1678 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1680 * Send a file specified by 'fd' and starting at 'offset' to a socket
1681 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1682 * nbytes == 0. Optionally add a header and/or trailer to the socket
1683 * output. If specified, write the total number of bytes sent into *sbytes.
1687 sendfile(struct thread *td, struct sendfile_args *uap)
1690 return (do_sendfile(td, uap, 0));
1693 #ifdef COMPAT_FREEBSD4
1695 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1697 struct sendfile_args args;
1701 args.offset = uap->offset;
1702 args.nbytes = uap->nbytes;
1703 args.hdtr = uap->hdtr;
1704 args.sbytes = uap->sbytes;
1705 args.flags = uap->flags;
1707 return (do_sendfile(td, &args, 1));
1709 #endif /* COMPAT_FREEBSD4 */
1712 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1715 struct vm_object *obj;
1716 struct socket *so = NULL;
1720 struct writev_args nuap;
1721 struct sf_hdtr hdtr;
1722 off_t off, xfsize, hdtr_size, sbytes = 0;
1730 * The descriptor must be a regular file and have a backing VM object.
1732 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1734 if (vp->v_type != VREG || VOP_GETVOBJECT(vp, &obj) != 0) {
1738 if ((error = fgetsock(td, uap->s, &so, NULL)) != 0)
1740 if (so->so_type != SOCK_STREAM) {
1744 if ((so->so_state & SS_ISCONNECTED) == 0) {
1748 if (uap->offset < 0) {
1754 error = mac_check_socket_send(td->td_ucred, so);
1760 * If specified, get the pointer to the sf_hdtr struct for
1761 * any headers/trailers.
1763 if (uap->hdtr != NULL) {
1764 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1768 * Send any headers. Wimp out and use writev(2).
1770 if (hdtr.headers != NULL) {
1772 nuap.iovp = hdtr.headers;
1773 nuap.iovcnt = hdtr.hdr_cnt;
1774 error = writev(td, &nuap);
1778 sbytes += td->td_retval[0];
1780 hdtr_size += td->td_retval[0];
1785 * Protect against multiple writers to the socket.
1787 (void) sblock(&so->so_snd, M_WAITOK);
1790 * Loop through the pages in the file, starting with the requested
1791 * offset. Get a file page (do I/O if necessary), map the file page
1792 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1795 for (off = uap->offset; ; off += xfsize, sbytes += xfsize) {
1799 pindex = OFF_TO_IDX(off);
1802 * Calculate the amount to transfer. Not to exceed a page,
1803 * the EOF, or the passed in nbytes.
1805 xfsize = obj->un_pager.vnp.vnp_size - off;
1806 if (xfsize > PAGE_SIZE)
1808 pgoff = (vm_offset_t)(off & PAGE_MASK);
1809 if (PAGE_SIZE - pgoff < xfsize)
1810 xfsize = PAGE_SIZE - pgoff;
1811 if (uap->nbytes && xfsize > (uap->nbytes - sbytes))
1812 xfsize = uap->nbytes - sbytes;
1816 * Optimize the non-blocking case by looking at the socket space
1817 * before going to the extra work of constituting the sf_buf.
1819 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1820 if (so->so_state & SS_CANTSENDMORE)
1824 sbunlock(&so->so_snd);
1828 * Attempt to look up the page.
1830 * Allocate if not found
1832 * Wait and loop if busy.
1834 pg = vm_page_lookup(obj, pindex);
1837 pg = vm_page_alloc(obj, pindex,
1838 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
1843 vm_page_lock_queues();
1846 vm_page_lock_queues();
1847 if (vm_page_sleep_if_busy(pg, TRUE, "sfpbsy"))
1850 * Wire the page so it does not get ripped out from
1857 * If page is not valid for what we need, initiate I/O
1860 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1864 * Ensure that our page is still around when the I/O
1867 vm_page_io_start(pg);
1868 vm_page_unlock_queues();
1871 * Get the page from backing store.
1873 bsize = vp->v_mount->mnt_stat.f_iosize;
1874 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
1876 * XXXMAC: Because we don't have fp->f_cred here,
1877 * we pass in NOCRED. This is probably wrong, but
1878 * is consistent with our original implementation.
1880 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
1881 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
1882 IO_VMIO | ((MAXBSIZE / bsize) << 16),
1883 td->td_ucred, NOCRED, &resid, td);
1884 VOP_UNLOCK(vp, 0, td);
1885 vm_page_lock_queues();
1886 vm_page_flag_clear(pg, PG_ZERO);
1887 vm_page_io_finish(pg);
1889 vm_page_unwire(pg, 0);
1891 * See if anyone else might know about this page.
1892 * If not and it is not valid, then free it.
1894 if (pg->wire_count == 0 && pg->valid == 0 &&
1895 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1896 pg->hold_count == 0) {
1900 vm_page_unlock_queues();
1901 sbunlock(&so->so_snd);
1905 vm_page_unlock_queues();
1908 * Get a sendfile buf. We usually wait as long as necessary,
1909 * but this wait can be interrupted.
1911 if ((sf = sf_buf_alloc()) == NULL) {
1912 vm_page_lock_queues();
1913 vm_page_unwire(pg, 0);
1914 if (pg->wire_count == 0 && pg->object == NULL)
1916 vm_page_unlock_queues();
1917 sbunlock(&so->so_snd);
1923 * Allocate a kernel virtual page and insert the physical page
1927 pmap_qenter(sf->kva, &pg, 1);
1929 * Get an mbuf header and set it up as having external storage.
1931 MGETHDR(m, M_TRYWAIT, MT_DATA);
1934 sf_buf_free((void *)sf->kva, NULL);
1935 sbunlock(&so->so_snd);
1939 * Setup external storage for mbuf.
1941 MEXTADD(m, sf->kva, PAGE_SIZE, sf_buf_free, NULL, M_RDONLY,
1943 m->m_data = (char *) sf->kva + pgoff;
1944 m->m_pkthdr.len = m->m_len = xfsize;
1946 * Add the buffer to the socket buffer chain.
1951 * Make sure that the socket is still able to take more data.
1952 * CANTSENDMORE being true usually means that the connection
1953 * was closed. so_error is true when an error was sensed after
1955 * The state is checked after the page mapping and buffer
1956 * allocation above since those operations may block and make
1957 * any socket checks stale. From this point forward, nothing
1958 * blocks before the pru_send (or more accurately, any blocking
1959 * results in a loop back to here to re-check).
1961 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1962 if (so->so_state & SS_CANTSENDMORE) {
1965 error = so->so_error;
1969 sbunlock(&so->so_snd);
1974 * Wait for socket space to become available. We do this just
1975 * after checking the connection state above in order to avoid
1976 * a race condition with sbwait().
1978 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
1979 if (so->so_state & SS_NBIO) {
1981 sbunlock(&so->so_snd);
1986 error = sbwait(&so->so_snd);
1988 * An error from sbwait usually indicates that we've
1989 * been interrupted by a signal. If we've sent anything
1990 * then return bytes sent, otherwise return the error.
1994 sbunlock(&so->so_snd);
2000 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, td);
2003 sbunlock(&so->so_snd);
2007 sbunlock(&so->so_snd);
2010 * Send trailers. Wimp out and use writev(2).
2012 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
2014 nuap.iovp = hdtr.trailers;
2015 nuap.iovcnt = hdtr.trl_cnt;
2016 error = writev(td, &nuap);
2020 sbytes += td->td_retval[0];
2022 hdtr_size += td->td_retval[0];
2027 * If there was no error we have to clear td->td_retval[0]
2028 * because it may have been set by writev.
2031 td->td_retval[0] = 0;
2033 if (uap->sbytes != NULL) {
2035 sbytes += hdtr_size;
2036 copyout(&sbytes, uap->sbytes, sizeof(off_t));