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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "opt_compat.h"
39 #include "opt_ktrace.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
47 #include <sys/mutex.h>
48 #include <sys/sysproto.h>
49 #include <sys/malloc.h>
50 #include <sys/filedesc.h>
51 #include <sys/event.h>
53 #include <sys/fcntl.h>
55 #include <sys/filio.h>
56 #include <sys/mount.h>
58 #include <sys/protosw.h>
59 #include <sys/sf_buf.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/signalvar.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
68 #include <sys/ktrace.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_page.h>
74 #include <vm/vm_pageout.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_extern.h>
78 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
79 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
81 static int accept1(struct thread *td, struct accept_args *uap, int compat);
82 static int do_sendfile(struct thread *td, struct sendfile_args *uap, int compat);
83 static int getsockname1(struct thread *td, struct getsockname_args *uap,
85 static int getpeername1(struct thread *td, struct getpeername_args *uap,
89 * NSFBUFS-related variables and associated sysctls
95 SYSCTL_DECL(_kern_ipc);
96 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
97 "Maximum number of sendfile(2) sf_bufs available");
98 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
99 "Number of sendfile(2) sf_bufs at peak usage");
100 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
101 "Number of sendfile(2) sf_bufs in use");
104 * Convert a user file descriptor to a kernel file entry. A reference on the
105 * file entry is held upon returning. This is lighter weight than
106 * fgetsock(), which bumps the socket reference drops the file reference
107 * count instead, as this approach avoids several additional mutex operations
108 * associated with the additional reference count.
111 getsock(struct filedesc *fdp, int fd, struct file **fpp)
120 FILEDESC_LOCK_FAST(fdp);
121 fp = fget_locked(fdp, fd);
124 else if (fp->f_type != DTYPE_SOCKET) {
131 FILEDESC_UNLOCK_FAST(fdp);
138 * System call interface to the socket abstraction.
140 #if defined(COMPAT_43)
141 #define COMPAT_OLDSOCK
150 register struct socket_args /* {
156 struct filedesc *fdp;
161 fdp = td->td_proc->p_fd;
162 error = falloc(td, &fp, &fd);
165 /* An extra reference on `fp' has been held for us by falloc(). */
167 error = socreate(uap->domain, &so, uap->type, uap->protocol,
171 fdclose(fdp, fp, fd, td);
173 FILEDESC_LOCK_FAST(fdp);
174 fp->f_data = so; /* already has ref count */
175 fp->f_flag = FREAD|FWRITE;
176 fp->f_ops = &socketops;
177 fp->f_type = DTYPE_SOCKET;
178 FILEDESC_UNLOCK_FAST(fdp);
179 td->td_retval[0] = fd;
192 register struct bind_args /* {
201 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
204 return (kern_bind(td, uap->s, sa));
208 kern_bind(td, fd, sa)
218 error = getsock(td->td_proc->p_fd, fd, &fp);
224 error = mac_check_socket_bind(td->td_ucred, so, sa);
229 error = sobind(so, sa, td);
247 register struct listen_args /* {
257 error = getsock(td->td_proc->p_fd, uap->s, &fp);
262 error = mac_check_socket_listen(td->td_ucred, so);
267 error = solisten(so, uap->backlog, td);
282 accept1(td, uap, compat)
284 register struct accept_args /* {
286 struct sockaddr * __restrict name;
287 socklen_t * __restrict anamelen;
291 struct filedesc *fdp;
292 struct file *nfp = NULL;
293 struct sockaddr *sa = NULL;
296 struct socket *head, *so;
302 fdp = td->td_proc->p_fd;
304 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
311 error = fgetsock(td, uap->s, &head, &fflag);
314 if ((head->so_options & SO_ACCEPTCONN) == 0) {
320 error = mac_check_socket_accept(td->td_ucred, head);
325 error = falloc(td, &nfp, &fd);
329 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
334 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
335 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
336 head->so_error = ECONNABORTED;
339 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
346 if (head->so_error) {
347 error = head->so_error;
352 so = TAILQ_FIRST(&head->so_comp);
353 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
354 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
357 * Before changing the flags on the socket, we have to bump the
358 * reference count. Otherwise, if the protocol calls sofree(),
359 * the socket will be released due to a zero refcount.
361 SOCK_LOCK(so); /* soref() and so_state update */
362 soref(so); /* file descriptor reference */
364 TAILQ_REMOVE(&head->so_comp, so, so_list);
366 so->so_state |= (head->so_state & SS_NBIO);
367 so->so_qstate &= ~SQ_COMP;
373 /* An extra reference on `nfp' has been held for us by falloc(). */
374 td->td_retval[0] = fd;
376 /* connection has been removed from the listen queue */
377 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
379 pgid = fgetown(&head->so_sigio);
381 fsetown(pgid, &so->so_sigio);
384 nfp->f_data = so; /* nfp has ref count from falloc */
386 nfp->f_ops = &socketops;
387 nfp->f_type = DTYPE_SOCKET;
389 /* Sync socket nonblocking/async state with file flags */
390 tmp = fflag & FNONBLOCK;
391 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
392 tmp = fflag & FASYNC;
393 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
395 error = soaccept(so, &sa);
398 * return a namelen of zero for older code which might
399 * ignore the return value from accept.
401 if (uap->name != NULL) {
403 (void) copyout(&namelen,
404 uap->anamelen, sizeof(*uap->anamelen));
416 /* check sa_len before it is destroyed */
417 if (namelen > sa->sa_len)
418 namelen = sa->sa_len;
419 #ifdef COMPAT_OLDSOCK
421 ((struct osockaddr *)sa)->sa_family =
424 error = copyout(sa, uap->name, (u_int)namelen);
427 error = copyout(&namelen,
428 uap->anamelen, sizeof (*uap->anamelen));
435 * close the new descriptor, assuming someone hasn't ripped it
439 fdclose(fdp, nfp, fd, td);
442 * Release explicitly held references before returning.
454 * MPSAFE (accept1() is MPSAFE)
459 struct accept_args *uap;
462 return (accept1(td, uap, 0));
465 #ifdef COMPAT_OLDSOCK
467 * MPSAFE (accept1() is MPSAFE)
472 struct accept_args *uap;
475 return (accept1(td, uap, 1));
477 #endif /* COMPAT_OLDSOCK */
486 register struct connect_args /* {
495 error = getsockaddr(&sa, uap->name, uap->namelen);
499 return (kern_connect(td, uap->s, sa));
504 kern_connect(td, fd, sa)
515 error = getsock(td->td_proc->p_fd, fd, &fp);
519 if (so->so_state & SS_ISCONNECTING) {
525 error = mac_check_socket_connect(td->td_ucred, so, sa);
530 error = soconnect(so, sa, td);
533 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
538 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
539 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
542 if (error == EINTR || error == ERESTART)
548 error = so->so_error;
554 so->so_state &= ~SS_ISCONNECTING;
555 if (error == ERESTART)
571 register struct socketpair_args /* {
578 register struct filedesc *fdp = td->td_proc->p_fd;
579 struct file *fp1, *fp2;
580 struct socket *so1, *so2;
581 int fd, error, sv[2];
584 error = socreate(uap->domain, &so1, uap->type, uap->protocol,
588 error = socreate(uap->domain, &so2, uap->type, uap->protocol,
592 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
593 error = falloc(td, &fp1, &fd);
597 fp1->f_data = so1; /* so1 already has ref count */
598 error = falloc(td, &fp2, &fd);
601 fp2->f_data = so2; /* so2 already has ref count */
603 error = soconnect2(so1, so2);
606 if (uap->type == SOCK_DGRAM) {
608 * Datagram socket connection is asymmetric.
610 error = soconnect2(so2, so1);
615 fp1->f_flag = FREAD|FWRITE;
616 fp1->f_ops = &socketops;
617 fp1->f_type = DTYPE_SOCKET;
620 fp2->f_flag = FREAD|FWRITE;
621 fp2->f_ops = &socketops;
622 fp2->f_type = DTYPE_SOCKET;
624 error = copyout(sv, uap->rsv, 2 * sizeof (int));
629 fdclose(fdp, fp2, sv[1], td);
632 fdclose(fdp, fp1, sv[0], td);
644 sendit(td, s, mp, flags)
645 register struct thread *td;
647 register struct msghdr *mp;
650 struct mbuf *control;
654 if (mp->msg_name != NULL) {
655 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
665 if (mp->msg_control) {
666 if (mp->msg_controllen < sizeof(struct cmsghdr)
667 #ifdef COMPAT_OLDSOCK
668 && mp->msg_flags != MSG_COMPAT
674 error = sockargs(&control, mp->msg_control,
675 mp->msg_controllen, MT_CONTROL);
678 #ifdef COMPAT_OLDSOCK
679 if (mp->msg_flags == MSG_COMPAT) {
680 register struct cmsghdr *cm;
682 M_PREPEND(control, sizeof(*cm), M_TRYWAIT);
687 cm = mtod(control, struct cmsghdr *);
688 cm->cmsg_len = control->m_len;
689 cm->cmsg_level = SOL_SOCKET;
690 cm->cmsg_type = SCM_RIGHTS;
698 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
707 kern_sendit(td, s, mp, flags, control, segflg)
712 struct mbuf *control;
722 struct uio *ktruio = NULL;
726 error = getsock(td->td_proc->p_fd, s, &fp);
729 so = (struct socket *)fp->f_data;
733 error = mac_check_socket_send(td->td_ucred, so);
739 auio.uio_iov = mp->msg_iov;
740 auio.uio_iovcnt = mp->msg_iovlen;
741 auio.uio_segflg = segflg;
742 auio.uio_rw = UIO_WRITE;
744 auio.uio_offset = 0; /* XXX */
747 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
748 if ((auio.uio_resid += iov->iov_len) < 0) {
754 if (KTRPOINT(td, KTR_GENIO))
755 ktruio = cloneuio(&auio);
757 len = auio.uio_resid;
758 error = so->so_proto->pr_usrreqs->pru_sosend(so, mp->msg_name, &auio,
759 0, control, flags, td);
761 if (auio.uio_resid != len && (error == ERESTART ||
762 error == EINTR || error == EWOULDBLOCK))
764 /* Generation of SIGPIPE can be controlled per socket */
765 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
766 !(flags & MSG_NOSIGNAL)) {
767 PROC_LOCK(td->td_proc);
768 psignal(td->td_proc, SIGPIPE);
769 PROC_UNLOCK(td->td_proc);
773 td->td_retval[0] = len - auio.uio_resid;
775 if (ktruio != NULL) {
776 ktruio->uio_resid = td->td_retval[0];
777 ktrgenio(s, UIO_WRITE, ktruio, error);
793 register struct sendto_args /* {
806 msg.msg_name = uap->to;
807 msg.msg_namelen = uap->tolen;
811 #ifdef COMPAT_OLDSOCK
814 aiov.iov_base = uap->buf;
815 aiov.iov_len = uap->len;
816 error = sendit(td, uap->s, &msg, uap->flags);
820 #ifdef COMPAT_OLDSOCK
827 register struct osend_args /* {
842 aiov.iov_base = uap->buf;
843 aiov.iov_len = uap->len;
846 error = sendit(td, uap->s, &msg, uap->flags);
856 struct osendmsg_args /* {
866 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
869 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
873 msg.msg_flags = MSG_COMPAT;
874 error = sendit(td, uap->s, &msg, uap->flags);
886 struct sendmsg_args /* {
896 error = copyin(uap->msg, &msg, sizeof (msg));
899 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
903 #ifdef COMPAT_OLDSOCK
906 error = sendit(td, uap->s, &msg, uap->flags);
912 recvit(td, s, mp, namelenp)
923 struct mbuf *m, *control = 0;
927 struct sockaddr *fromsa = 0;
929 struct uio *ktruio = NULL;
933 error = getsock(td->td_proc->p_fd, s, &fp);
942 error = mac_check_socket_receive(td->td_ucred, so);
951 auio.uio_iov = mp->msg_iov;
952 auio.uio_iovcnt = mp->msg_iovlen;
953 auio.uio_segflg = UIO_USERSPACE;
954 auio.uio_rw = UIO_READ;
956 auio.uio_offset = 0; /* XXX */
959 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
960 if ((auio.uio_resid += iov->iov_len) < 0) {
967 if (KTRPOINT(td, KTR_GENIO))
968 ktruio = cloneuio(&auio);
970 len = auio.uio_resid;
971 error = so->so_proto->pr_usrreqs->pru_soreceive(so, &fromsa, &auio,
972 (struct mbuf **)0, mp->msg_control ? &control : (struct mbuf **)0,
975 if (auio.uio_resid != (int)len && (error == ERESTART ||
976 error == EINTR || error == EWOULDBLOCK))
980 if (ktruio != NULL) {
981 ktruio->uio_resid = (int)len - auio.uio_resid;
982 ktrgenio(s, UIO_READ, ktruio, error);
987 td->td_retval[0] = (int)len - auio.uio_resid;
989 len = mp->msg_namelen;
990 if (len <= 0 || fromsa == 0)
993 /* save sa_len before it is destroyed by MSG_COMPAT */
994 len = MIN(len, fromsa->sa_len);
995 #ifdef COMPAT_OLDSOCK
996 if (mp->msg_flags & MSG_COMPAT)
997 ((struct osockaddr *)fromsa)->sa_family =
1000 error = copyout(fromsa, mp->msg_name, (unsigned)len);
1004 mp->msg_namelen = len;
1006 (error = copyout(&len, namelenp, sizeof (socklen_t)))) {
1007 #ifdef COMPAT_OLDSOCK
1008 if (mp->msg_flags & MSG_COMPAT)
1009 error = 0; /* old recvfrom didn't check */
1015 if (mp->msg_control) {
1016 #ifdef COMPAT_OLDSOCK
1018 * We assume that old recvmsg calls won't receive access
1019 * rights and other control info, esp. as control info
1020 * is always optional and those options didn't exist in 4.3.
1021 * If we receive rights, trim the cmsghdr; anything else
1024 if (control && mp->msg_flags & MSG_COMPAT) {
1025 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1027 mtod(control, struct cmsghdr *)->cmsg_type !=
1029 mp->msg_controllen = 0;
1032 control->m_len -= sizeof (struct cmsghdr);
1033 control->m_data += sizeof (struct cmsghdr);
1036 len = mp->msg_controllen;
1038 mp->msg_controllen = 0;
1039 ctlbuf = mp->msg_control;
1041 while (m && len > 0) {
1042 unsigned int tocopy;
1044 if (len >= m->m_len)
1047 mp->msg_flags |= MSG_CTRUNC;
1051 if ((error = copyout(mtod(m, caddr_t),
1052 ctlbuf, tocopy)) != 0)
1059 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1065 FREE(fromsa, M_SONAME);
1077 register struct recvfrom_args /* {
1082 struct sockaddr * __restrict from;
1083 socklen_t * __restrict fromlenaddr;
1090 if (uap->fromlenaddr) {
1091 error = copyin(uap->fromlenaddr,
1092 &msg.msg_namelen, sizeof (msg.msg_namelen));
1096 msg.msg_namelen = 0;
1098 msg.msg_name = uap->from;
1099 msg.msg_iov = &aiov;
1101 aiov.iov_base = uap->buf;
1102 aiov.iov_len = uap->len;
1103 msg.msg_control = 0;
1104 msg.msg_flags = uap->flags;
1105 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1110 #ifdef COMPAT_OLDSOCK
1117 struct recvfrom_args *uap;
1120 uap->flags |= MSG_COMPAT;
1121 return (recvfrom(td, uap));
1126 #ifdef COMPAT_OLDSOCK
1133 register struct orecv_args /* {
1145 msg.msg_namelen = 0;
1146 msg.msg_iov = &aiov;
1148 aiov.iov_base = uap->buf;
1149 aiov.iov_len = uap->len;
1150 msg.msg_control = 0;
1151 msg.msg_flags = uap->flags;
1152 error = recvit(td, uap->s, &msg, NULL);
1157 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1158 * overlays the new one, missing only the flags, and with the (old) access
1159 * rights where the control fields are now.
1166 struct orecvmsg_args /* {
1168 struct omsghdr *msg;
1176 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1179 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1182 msg.msg_flags = uap->flags | MSG_COMPAT;
1184 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1185 if (msg.msg_controllen && error == 0)
1186 error = copyout(&msg.msg_controllen,
1187 &uap->msg->msg_accrightslen, sizeof (int));
1199 struct recvmsg_args /* {
1206 struct iovec *uiov, *iov;
1209 error = copyin(uap->msg, &msg, sizeof (msg));
1212 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1215 msg.msg_flags = uap->flags;
1216 #ifdef COMPAT_OLDSOCK
1217 msg.msg_flags &= ~MSG_COMPAT;
1221 error = recvit(td, uap->s, &msg, NULL);
1224 error = copyout(&msg, uap->msg, sizeof(msg));
1237 register struct shutdown_args /* {
1247 error = getsock(td->td_proc->p_fd, uap->s, &fp);
1250 error = soshutdown(so, uap->how);
1264 register struct setsockopt_args /* {
1273 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1274 uap->val, UIO_USERSPACE, uap->valsize));
1278 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1284 enum uio_seg valseg;
1290 struct sockopt sopt;
1292 if (val == NULL && valsize != 0)
1297 sopt.sopt_dir = SOPT_SET;
1298 sopt.sopt_level = level;
1299 sopt.sopt_name = name;
1300 sopt.sopt_val = val;
1301 sopt.sopt_valsize = valsize;
1307 sopt.sopt_td = NULL;
1310 panic("kern_setsockopt called with bad valseg");
1314 error = getsock(td->td_proc->p_fd, s, &fp);
1317 error = sosetopt(so, &sopt);
1331 register struct getsockopt_args /* {
1335 void * __restrict val;
1336 socklen_t * __restrict avalsize;
1343 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1348 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1349 uap->val, UIO_USERSPACE, &valsize);
1352 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1357 * Kernel version of getsockopt.
1358 * optval can be a userland or userspace. optlen is always a kernel pointer.
1361 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1367 enum uio_seg valseg;
1373 struct sockopt sopt;
1380 sopt.sopt_dir = SOPT_GET;
1381 sopt.sopt_level = level;
1382 sopt.sopt_name = name;
1383 sopt.sopt_val = val;
1384 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1390 sopt.sopt_td = NULL;
1393 panic("kern_getsockopt called with bad valseg");
1397 error = getsock(td->td_proc->p_fd, s, &fp);
1400 error = sogetopt(so, &sopt);
1401 *valsize = sopt.sopt_valsize;
1409 * getsockname1() - Get socket name.
1415 getsockname1(td, uap, compat)
1417 register struct getsockname_args /* {
1419 struct sockaddr * __restrict asa;
1420 socklen_t * __restrict alen;
1425 struct sockaddr *sa;
1431 error = getsock(td->td_proc->p_fd, uap->fdes, &fp);
1435 error = copyin(uap->alen, &len, sizeof (len));
1443 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
1451 len = MIN(len, sa->sa_len);
1452 #ifdef COMPAT_OLDSOCK
1454 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1456 error = copyout(sa, uap->asa, (u_int)len);
1459 error = copyout(&len, uap->alen, sizeof (len));
1474 getsockname(td, uap)
1476 struct getsockname_args *uap;
1479 return (getsockname1(td, uap, 0));
1482 #ifdef COMPAT_OLDSOCK
1487 ogetsockname(td, uap)
1489 struct getsockname_args *uap;
1492 return (getsockname1(td, uap, 1));
1494 #endif /* COMPAT_OLDSOCK */
1497 * getpeername1() - Get name of peer for connected socket.
1503 getpeername1(td, uap, compat)
1505 register struct getpeername_args /* {
1507 struct sockaddr * __restrict asa;
1508 socklen_t * __restrict alen;
1513 struct sockaddr *sa;
1519 error = getsock(td->td_proc->p_fd, uap->fdes, &fp);
1523 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1527 error = copyin(uap->alen, &len, sizeof (len));
1535 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa);
1542 len = MIN(len, sa->sa_len);
1543 #ifdef COMPAT_OLDSOCK
1545 ((struct osockaddr *)sa)->sa_family =
1548 error = copyout(sa, uap->asa, (u_int)len);
1552 error = copyout(&len, uap->alen, sizeof (len));
1567 getpeername(td, uap)
1569 struct getpeername_args *uap;
1572 return (getpeername1(td, uap, 0));
1575 #ifdef COMPAT_OLDSOCK
1580 ogetpeername(td, uap)
1582 struct ogetpeername_args *uap;
1585 /* XXX uap should have type `getpeername_args *' to begin with. */
1586 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1588 #endif /* COMPAT_OLDSOCK */
1591 sockargs(mp, buf, buflen, type)
1596 register struct sockaddr *sa;
1597 register struct mbuf *m;
1600 if ((u_int)buflen > MLEN) {
1601 #ifdef COMPAT_OLDSOCK
1602 if (type == MT_SONAME && (u_int)buflen <= 112)
1603 buflen = MLEN; /* unix domain compat. hack */
1606 if ((u_int)buflen > MCLBYTES)
1609 m = m_get(M_TRYWAIT, type);
1612 if ((u_int)buflen > MLEN) {
1613 MCLGET(m, M_TRYWAIT);
1614 if ((m->m_flags & M_EXT) == 0) {
1620 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1625 if (type == MT_SONAME) {
1626 sa = mtod(m, struct sockaddr *);
1628 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1629 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1630 sa->sa_family = sa->sa_len;
1632 sa->sa_len = buflen;
1639 getsockaddr(namp, uaddr, len)
1640 struct sockaddr **namp;
1644 struct sockaddr *sa;
1647 if (len > SOCK_MAXADDRLEN)
1648 return (ENAMETOOLONG);
1649 if (len < offsetof(struct sockaddr, sa_data[0]))
1651 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1652 error = copyin(uaddr, sa, len);
1656 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1657 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1658 sa->sa_family = sa->sa_len;
1667 * Detach mapped page and release resources back to the system.
1670 sf_buf_mext(void *addr, void *args)
1674 m = sf_buf_page(args);
1676 vm_page_lock_queues();
1677 vm_page_unwire(m, 0);
1679 * Check for the object going away on us. This can
1680 * happen since we don't hold a reference to it.
1681 * If so, we're responsible for freeing the page.
1683 if (m->wire_count == 0 && m->object == NULL)
1685 vm_page_unlock_queues();
1693 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1694 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1696 * Send a file specified by 'fd' and starting at 'offset' to a socket
1697 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1698 * nbytes == 0. Optionally add a header and/or trailer to the socket
1699 * output. If specified, write the total number of bytes sent into *sbytes.
1703 sendfile(struct thread *td, struct sendfile_args *uap)
1706 return (do_sendfile(td, uap, 0));
1709 #ifdef COMPAT_FREEBSD4
1711 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1713 struct sendfile_args args;
1717 args.offset = uap->offset;
1718 args.nbytes = uap->nbytes;
1719 args.hdtr = uap->hdtr;
1720 args.sbytes = uap->sbytes;
1721 args.flags = uap->flags;
1723 return (do_sendfile(td, &args, 1));
1725 #endif /* COMPAT_FREEBSD4 */
1728 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1731 struct vm_object *obj;
1732 struct socket *so = NULL;
1733 struct mbuf *m, *m_header = NULL;
1736 struct writev_args nuap;
1737 struct sf_hdtr hdtr;
1738 struct uio *hdr_uio = NULL;
1739 off_t off, xfsize, hdtr_size, sbytes = 0;
1740 int error, headersize = 0, headersent = 0;
1747 * The descriptor must be a regular file and have a backing VM object.
1749 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1751 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1753 VOP_UNLOCK(vp, 0, td);
1758 if ((error = fgetsock(td, uap->s, &so, NULL)) != 0)
1760 if (so->so_type != SOCK_STREAM) {
1764 if ((so->so_state & SS_ISCONNECTED) == 0) {
1768 if (uap->offset < 0) {
1775 error = mac_check_socket_send(td->td_ucred, so);
1782 * If specified, get the pointer to the sf_hdtr struct for
1783 * any headers/trailers.
1785 if (uap->hdtr != NULL) {
1786 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1792 if (hdtr.headers != NULL) {
1793 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1796 hdr_uio->uio_td = td;
1797 hdr_uio->uio_rw = UIO_WRITE;
1798 if (hdr_uio->uio_resid > 0) {
1799 m_header = m_uiotombuf(hdr_uio, M_DONTWAIT, 0, 0);
1800 if (m_header == NULL)
1802 headersize = m_header->m_pkthdr.len;
1804 sbytes += headersize;
1810 * Protect against multiple writers to the socket.
1812 SOCKBUF_LOCK(&so->so_snd);
1813 (void) sblock(&so->so_snd, M_WAITOK);
1814 SOCKBUF_UNLOCK(&so->so_snd);
1817 * Loop through the pages in the file, starting with the requested
1818 * offset. Get a file page (do I/O if necessary), map the file page
1819 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1822 for (off = uap->offset; ; off += xfsize, sbytes += xfsize) {
1826 pindex = OFF_TO_IDX(off);
1827 VM_OBJECT_LOCK(obj);
1830 * Calculate the amount to transfer. Not to exceed a page,
1831 * the EOF, or the passed in nbytes.
1833 xfsize = obj->un_pager.vnp.vnp_size - off;
1834 VM_OBJECT_UNLOCK(obj);
1835 if (xfsize > PAGE_SIZE)
1837 pgoff = (vm_offset_t)(off & PAGE_MASK);
1838 if (PAGE_SIZE - pgoff < xfsize)
1839 xfsize = PAGE_SIZE - pgoff;
1840 if (uap->nbytes && xfsize > (uap->nbytes - sbytes))
1841 xfsize = uap->nbytes - sbytes;
1843 if (m_header != NULL) {
1846 SOCKBUF_LOCK(&so->so_snd);
1852 * Optimize the non-blocking case by looking at the socket space
1853 * before going to the extra work of constituting the sf_buf.
1855 SOCKBUF_LOCK(&so->so_snd);
1856 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1857 if (so->so_snd.sb_state & SBS_CANTSENDMORE)
1861 sbunlock(&so->so_snd);
1862 SOCKBUF_UNLOCK(&so->so_snd);
1865 SOCKBUF_UNLOCK(&so->so_snd);
1866 VM_OBJECT_LOCK(obj);
1868 * Attempt to look up the page.
1870 * Allocate if not found
1872 * Wait and loop if busy.
1874 pg = vm_page_lookup(obj, pindex);
1877 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NOBUSY |
1878 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
1880 VM_OBJECT_UNLOCK(obj);
1882 VM_OBJECT_LOCK(obj);
1885 vm_page_lock_queues();
1887 vm_page_lock_queues();
1888 if (vm_page_sleep_if_busy(pg, TRUE, "sfpbsy"))
1891 * Wire the page so it does not get ripped out from
1898 * If page is not valid for what we need, initiate I/O
1901 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize)) {
1902 VM_OBJECT_UNLOCK(obj);
1903 } else if (uap->flags & SF_NODISKIO) {
1909 * Ensure that our page is still around when the I/O
1912 vm_page_io_start(pg);
1913 vm_page_unlock_queues();
1914 VM_OBJECT_UNLOCK(obj);
1917 * Get the page from backing store.
1919 bsize = vp->v_mount->mnt_stat.f_iosize;
1920 vn_lock(vp, LK_SHARED | LK_RETRY, td);
1922 * XXXMAC: Because we don't have fp->f_cred here,
1923 * we pass in NOCRED. This is probably wrong, but
1924 * is consistent with our original implementation.
1926 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
1927 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
1928 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
1929 td->td_ucred, NOCRED, &resid, td);
1930 VOP_UNLOCK(vp, 0, td);
1931 VM_OBJECT_LOCK(obj);
1932 vm_page_lock_queues();
1933 vm_page_io_finish(pg);
1935 VM_OBJECT_UNLOCK(obj);
1940 vm_page_unwire(pg, 0);
1942 * See if anyone else might know about this page.
1943 * If not and it is not valid, then free it.
1945 if (pg->wire_count == 0 && pg->valid == 0 &&
1946 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1947 pg->hold_count == 0) {
1950 vm_page_unlock_queues();
1951 VM_OBJECT_UNLOCK(obj);
1952 SOCKBUF_LOCK(&so->so_snd);
1953 sbunlock(&so->so_snd);
1954 SOCKBUF_UNLOCK(&so->so_snd);
1957 vm_page_unlock_queues();
1960 * Get a sendfile buf. We usually wait as long as necessary,
1961 * but this wait can be interrupted.
1963 if ((sf = sf_buf_alloc(pg, SFB_CATCH)) == NULL) {
1964 mbstat.sf_allocfail++;
1965 vm_page_lock_queues();
1966 vm_page_unwire(pg, 0);
1967 if (pg->wire_count == 0 && pg->object == NULL)
1969 vm_page_unlock_queues();
1970 SOCKBUF_LOCK(&so->so_snd);
1971 sbunlock(&so->so_snd);
1972 SOCKBUF_UNLOCK(&so->so_snd);
1978 * Get an mbuf header and set it up as having external storage.
1981 MGET(m, M_TRYWAIT, MT_DATA);
1983 MGETHDR(m, M_TRYWAIT, MT_DATA);
1986 sf_buf_mext((void *)sf_buf_kva(sf), sf);
1987 SOCKBUF_LOCK(&so->so_snd);
1988 sbunlock(&so->so_snd);
1989 SOCKBUF_UNLOCK(&so->so_snd);
1993 * Setup external storage for mbuf.
1995 MEXTADD(m, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext, sf, M_RDONLY,
1997 m->m_data = (char *)sf_buf_kva(sf) + pgoff;
1998 m->m_pkthdr.len = m->m_len = xfsize;
2008 * Add the buffer to the socket buffer chain.
2010 SOCKBUF_LOCK(&so->so_snd);
2013 * Make sure that the socket is still able to take more data.
2014 * CANTSENDMORE being true usually means that the connection
2015 * was closed. so_error is true when an error was sensed after
2017 * The state is checked after the page mapping and buffer
2018 * allocation above since those operations may block and make
2019 * any socket checks stale. From this point forward, nothing
2020 * blocks before the pru_send (or more accurately, any blocking
2021 * results in a loop back to here to re-check).
2023 SOCKBUF_LOCK_ASSERT(&so->so_snd);
2024 if ((so->so_snd.sb_state & SBS_CANTSENDMORE) || so->so_error) {
2025 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2028 error = so->so_error;
2032 sbunlock(&so->so_snd);
2033 SOCKBUF_UNLOCK(&so->so_snd);
2037 * Wait for socket space to become available. We do this just
2038 * after checking the connection state above in order to avoid
2039 * a race condition with sbwait().
2041 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
2042 if (so->so_state & SS_NBIO) {
2044 sbunlock(&so->so_snd);
2045 SOCKBUF_UNLOCK(&so->so_snd);
2049 error = sbwait(&so->so_snd);
2051 * An error from sbwait usually indicates that we've
2052 * been interrupted by a signal. If we've sent anything
2053 * then return bytes sent, otherwise return the error.
2057 sbunlock(&so->so_snd);
2058 SOCKBUF_UNLOCK(&so->so_snd);
2063 SOCKBUF_UNLOCK(&so->so_snd);
2064 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, td);
2066 SOCKBUF_LOCK(&so->so_snd);
2067 sbunlock(&so->so_snd);
2068 SOCKBUF_UNLOCK(&so->so_snd);
2073 SOCKBUF_LOCK(&so->so_snd);
2074 sbunlock(&so->so_snd);
2075 SOCKBUF_UNLOCK(&so->so_snd);
2078 * Send trailers. Wimp out and use writev(2).
2080 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
2082 nuap.iovp = hdtr.trailers;
2083 nuap.iovcnt = hdtr.trl_cnt;
2084 error = writev(td, &nuap);
2088 sbytes += td->td_retval[0];
2090 hdtr_size += td->td_retval[0];
2096 hdtr_size += headersize;
2099 sbytes -= headersize;
2102 * If there was no error we have to clear td->td_retval[0]
2103 * because it may have been set by writev.
2106 td->td_retval[0] = 0;
2108 if (uap->sbytes != NULL) {
2110 sbytes += hdtr_size;
2111 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2117 if (hdr_uio != NULL)
2118 free(hdr_uio, M_IOV);
2124 if (error == ERESTART)