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) {
318 error = falloc(td, &nfp, &fd);
322 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
327 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
328 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
329 head->so_error = ECONNABORTED;
332 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
339 if (head->so_error) {
340 error = head->so_error;
345 so = TAILQ_FIRST(&head->so_comp);
346 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
347 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
350 * Before changing the flags on the socket, we have to bump the
351 * reference count. Otherwise, if the protocol calls sofree(),
352 * the socket will be released due to a zero refcount.
354 SOCK_LOCK(so); /* soref() and so_state update */
355 soref(so); /* file descriptor reference */
357 TAILQ_REMOVE(&head->so_comp, so, so_list);
359 so->so_state |= (head->so_state & SS_NBIO);
360 so->so_qstate &= ~SQ_COMP;
366 /* An extra reference on `nfp' has been held for us by falloc(). */
367 td->td_retval[0] = fd;
369 /* connection has been removed from the listen queue */
370 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
372 pgid = fgetown(&head->so_sigio);
374 fsetown(pgid, &so->so_sigio);
377 nfp->f_data = so; /* nfp has ref count from falloc */
379 nfp->f_ops = &socketops;
380 nfp->f_type = DTYPE_SOCKET;
382 /* Sync socket nonblocking/async state with file flags */
383 tmp = fflag & FNONBLOCK;
384 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
385 tmp = fflag & FASYNC;
386 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
388 error = soaccept(so, &sa);
391 * return a namelen of zero for older code which might
392 * ignore the return value from accept.
394 if (uap->name != NULL) {
396 (void) copyout(&namelen,
397 uap->anamelen, sizeof(*uap->anamelen));
409 /* check sa_len before it is destroyed */
410 if (namelen > sa->sa_len)
411 namelen = sa->sa_len;
412 #ifdef COMPAT_OLDSOCK
414 ((struct osockaddr *)sa)->sa_family =
417 error = copyout(sa, uap->name, (u_int)namelen);
420 error = copyout(&namelen,
421 uap->anamelen, sizeof (*uap->anamelen));
428 * close the new descriptor, assuming someone hasn't ripped it
432 fdclose(fdp, nfp, fd, td);
435 * Release explicitly held references before returning.
447 * MPSAFE (accept1() is MPSAFE)
452 struct accept_args *uap;
455 return (accept1(td, uap, 0));
458 #ifdef COMPAT_OLDSOCK
460 * MPSAFE (accept1() is MPSAFE)
465 struct accept_args *uap;
468 return (accept1(td, uap, 1));
470 #endif /* COMPAT_OLDSOCK */
479 register struct connect_args /* {
488 error = getsockaddr(&sa, uap->name, uap->namelen);
492 return (kern_connect(td, uap->s, sa));
497 kern_connect(td, fd, sa)
508 error = getsock(td->td_proc->p_fd, fd, &fp);
512 if (so->so_state & SS_ISCONNECTING) {
518 error = mac_check_socket_connect(td->td_ucred, so, sa);
523 error = soconnect(so, sa, td);
526 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
531 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
532 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
535 if (error == EINTR || error == ERESTART)
541 error = so->so_error;
547 so->so_state &= ~SS_ISCONNECTING;
548 if (error == ERESTART)
564 register struct socketpair_args /* {
571 register struct filedesc *fdp = td->td_proc->p_fd;
572 struct file *fp1, *fp2;
573 struct socket *so1, *so2;
574 int fd, error, sv[2];
577 error = socreate(uap->domain, &so1, uap->type, uap->protocol,
581 error = socreate(uap->domain, &so2, uap->type, uap->protocol,
585 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
586 error = falloc(td, &fp1, &fd);
590 fp1->f_data = so1; /* so1 already has ref count */
591 error = falloc(td, &fp2, &fd);
594 fp2->f_data = so2; /* so2 already has ref count */
596 error = soconnect2(so1, so2);
599 if (uap->type == SOCK_DGRAM) {
601 * Datagram socket connection is asymmetric.
603 error = soconnect2(so2, so1);
608 fp1->f_flag = FREAD|FWRITE;
609 fp1->f_ops = &socketops;
610 fp1->f_type = DTYPE_SOCKET;
613 fp2->f_flag = FREAD|FWRITE;
614 fp2->f_ops = &socketops;
615 fp2->f_type = DTYPE_SOCKET;
617 error = copyout(sv, uap->rsv, 2 * sizeof (int));
622 fdclose(fdp, fp2, sv[1], td);
625 fdclose(fdp, fp1, sv[0], td);
637 sendit(td, s, mp, flags)
638 register struct thread *td;
640 register struct msghdr *mp;
643 struct mbuf *control;
647 if (mp->msg_name != NULL) {
648 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
658 if (mp->msg_control) {
659 if (mp->msg_controllen < sizeof(struct cmsghdr)
660 #ifdef COMPAT_OLDSOCK
661 && mp->msg_flags != MSG_COMPAT
667 error = sockargs(&control, mp->msg_control,
668 mp->msg_controllen, MT_CONTROL);
671 #ifdef COMPAT_OLDSOCK
672 if (mp->msg_flags == MSG_COMPAT) {
673 register struct cmsghdr *cm;
675 M_PREPEND(control, sizeof(*cm), M_TRYWAIT);
680 cm = mtod(control, struct cmsghdr *);
681 cm->cmsg_len = control->m_len;
682 cm->cmsg_level = SOL_SOCKET;
683 cm->cmsg_type = SCM_RIGHTS;
691 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
700 kern_sendit(td, s, mp, flags, control, segflg)
705 struct mbuf *control;
715 struct uio *ktruio = NULL;
719 error = getsock(td->td_proc->p_fd, s, &fp);
722 so = (struct socket *)fp->f_data;
726 error = mac_check_socket_send(td->td_ucred, so);
732 auio.uio_iov = mp->msg_iov;
733 auio.uio_iovcnt = mp->msg_iovlen;
734 auio.uio_segflg = segflg;
735 auio.uio_rw = UIO_WRITE;
737 auio.uio_offset = 0; /* XXX */
740 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
741 if ((auio.uio_resid += iov->iov_len) < 0) {
747 if (KTRPOINT(td, KTR_GENIO))
748 ktruio = cloneuio(&auio);
750 len = auio.uio_resid;
751 error = so->so_proto->pr_usrreqs->pru_sosend(so, mp->msg_name, &auio,
752 0, control, flags, td);
754 if (auio.uio_resid != len && (error == ERESTART ||
755 error == EINTR || error == EWOULDBLOCK))
757 /* Generation of SIGPIPE can be controlled per socket */
758 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
759 !(flags & MSG_NOSIGNAL)) {
760 PROC_LOCK(td->td_proc);
761 psignal(td->td_proc, SIGPIPE);
762 PROC_UNLOCK(td->td_proc);
766 td->td_retval[0] = len - auio.uio_resid;
768 if (ktruio != NULL) {
769 ktruio->uio_resid = td->td_retval[0];
770 ktrgenio(s, UIO_WRITE, ktruio, error);
786 register struct sendto_args /* {
799 msg.msg_name = uap->to;
800 msg.msg_namelen = uap->tolen;
804 #ifdef COMPAT_OLDSOCK
807 aiov.iov_base = uap->buf;
808 aiov.iov_len = uap->len;
809 error = sendit(td, uap->s, &msg, uap->flags);
813 #ifdef COMPAT_OLDSOCK
820 register struct osend_args /* {
835 aiov.iov_base = uap->buf;
836 aiov.iov_len = uap->len;
839 error = sendit(td, uap->s, &msg, uap->flags);
849 struct osendmsg_args /* {
859 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
862 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
866 msg.msg_flags = MSG_COMPAT;
867 error = sendit(td, uap->s, &msg, uap->flags);
879 struct sendmsg_args /* {
889 error = copyin(uap->msg, &msg, sizeof (msg));
892 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
896 #ifdef COMPAT_OLDSOCK
899 error = sendit(td, uap->s, &msg, uap->flags);
905 recvit(td, s, mp, namelenp)
916 struct mbuf *m, *control = 0;
920 struct sockaddr *fromsa = 0;
922 struct uio *ktruio = NULL;
926 error = getsock(td->td_proc->p_fd, s, &fp);
935 error = mac_check_socket_receive(td->td_ucred, so);
944 auio.uio_iov = mp->msg_iov;
945 auio.uio_iovcnt = mp->msg_iovlen;
946 auio.uio_segflg = UIO_USERSPACE;
947 auio.uio_rw = UIO_READ;
949 auio.uio_offset = 0; /* XXX */
952 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
953 if ((auio.uio_resid += iov->iov_len) < 0) {
960 if (KTRPOINT(td, KTR_GENIO))
961 ktruio = cloneuio(&auio);
963 len = auio.uio_resid;
964 error = so->so_proto->pr_usrreqs->pru_soreceive(so, &fromsa, &auio,
965 (struct mbuf **)0, mp->msg_control ? &control : (struct mbuf **)0,
968 if (auio.uio_resid != (int)len && (error == ERESTART ||
969 error == EINTR || error == EWOULDBLOCK))
973 if (ktruio != NULL) {
974 ktruio->uio_resid = (int)len - auio.uio_resid;
975 ktrgenio(s, UIO_READ, ktruio, error);
980 td->td_retval[0] = (int)len - auio.uio_resid;
982 len = mp->msg_namelen;
983 if (len <= 0 || fromsa == 0)
986 /* save sa_len before it is destroyed by MSG_COMPAT */
987 len = MIN(len, fromsa->sa_len);
988 #ifdef COMPAT_OLDSOCK
989 if (mp->msg_flags & MSG_COMPAT)
990 ((struct osockaddr *)fromsa)->sa_family =
993 error = copyout(fromsa, mp->msg_name, (unsigned)len);
997 mp->msg_namelen = len;
999 (error = copyout(&len, namelenp, sizeof (socklen_t)))) {
1000 #ifdef COMPAT_OLDSOCK
1001 if (mp->msg_flags & MSG_COMPAT)
1002 error = 0; /* old recvfrom didn't check */
1008 if (mp->msg_control) {
1009 #ifdef COMPAT_OLDSOCK
1011 * We assume that old recvmsg calls won't receive access
1012 * rights and other control info, esp. as control info
1013 * is always optional and those options didn't exist in 4.3.
1014 * If we receive rights, trim the cmsghdr; anything else
1017 if (control && mp->msg_flags & MSG_COMPAT) {
1018 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1020 mtod(control, struct cmsghdr *)->cmsg_type !=
1022 mp->msg_controllen = 0;
1025 control->m_len -= sizeof (struct cmsghdr);
1026 control->m_data += sizeof (struct cmsghdr);
1029 len = mp->msg_controllen;
1031 mp->msg_controllen = 0;
1032 ctlbuf = mp->msg_control;
1034 while (m && len > 0) {
1035 unsigned int tocopy;
1037 if (len >= m->m_len)
1040 mp->msg_flags |= MSG_CTRUNC;
1044 if ((error = copyout(mtod(m, caddr_t),
1045 ctlbuf, tocopy)) != 0)
1052 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1058 FREE(fromsa, M_SONAME);
1070 register struct recvfrom_args /* {
1075 struct sockaddr * __restrict from;
1076 socklen_t * __restrict fromlenaddr;
1083 if (uap->fromlenaddr) {
1084 error = copyin(uap->fromlenaddr,
1085 &msg.msg_namelen, sizeof (msg.msg_namelen));
1089 msg.msg_namelen = 0;
1091 msg.msg_name = uap->from;
1092 msg.msg_iov = &aiov;
1094 aiov.iov_base = uap->buf;
1095 aiov.iov_len = uap->len;
1096 msg.msg_control = 0;
1097 msg.msg_flags = uap->flags;
1098 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1103 #ifdef COMPAT_OLDSOCK
1110 struct recvfrom_args *uap;
1113 uap->flags |= MSG_COMPAT;
1114 return (recvfrom(td, uap));
1119 #ifdef COMPAT_OLDSOCK
1126 register struct orecv_args /* {
1138 msg.msg_namelen = 0;
1139 msg.msg_iov = &aiov;
1141 aiov.iov_base = uap->buf;
1142 aiov.iov_len = uap->len;
1143 msg.msg_control = 0;
1144 msg.msg_flags = uap->flags;
1145 error = recvit(td, uap->s, &msg, NULL);
1150 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1151 * overlays the new one, missing only the flags, and with the (old) access
1152 * rights where the control fields are now.
1159 struct orecvmsg_args /* {
1161 struct omsghdr *msg;
1169 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1172 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1175 msg.msg_flags = uap->flags | MSG_COMPAT;
1177 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1178 if (msg.msg_controllen && error == 0)
1179 error = copyout(&msg.msg_controllen,
1180 &uap->msg->msg_accrightslen, sizeof (int));
1192 struct recvmsg_args /* {
1199 struct iovec *uiov, *iov;
1202 error = copyin(uap->msg, &msg, sizeof (msg));
1205 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1208 msg.msg_flags = uap->flags;
1209 #ifdef COMPAT_OLDSOCK
1210 msg.msg_flags &= ~MSG_COMPAT;
1214 error = recvit(td, uap->s, &msg, NULL);
1217 error = copyout(&msg, uap->msg, sizeof(msg));
1230 register struct shutdown_args /* {
1240 error = getsock(td->td_proc->p_fd, uap->s, &fp);
1243 error = soshutdown(so, uap->how);
1257 register struct setsockopt_args /* {
1266 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1267 uap->val, UIO_USERSPACE, uap->valsize));
1271 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1277 enum uio_seg valseg;
1283 struct sockopt sopt;
1285 if (val == NULL && valsize != 0)
1290 sopt.sopt_dir = SOPT_SET;
1291 sopt.sopt_level = level;
1292 sopt.sopt_name = name;
1293 sopt.sopt_val = val;
1294 sopt.sopt_valsize = valsize;
1300 sopt.sopt_td = NULL;
1303 panic("kern_setsockopt called with bad valseg");
1307 error = getsock(td->td_proc->p_fd, s, &fp);
1310 error = sosetopt(so, &sopt);
1324 register struct getsockopt_args /* {
1328 void * __restrict val;
1329 socklen_t * __restrict avalsize;
1336 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1341 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1342 uap->val, UIO_USERSPACE, &valsize);
1345 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1350 * Kernel version of getsockopt.
1351 * optval can be a userland or userspace. optlen is always a kernel pointer.
1354 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1360 enum uio_seg valseg;
1366 struct sockopt sopt;
1373 sopt.sopt_dir = SOPT_GET;
1374 sopt.sopt_level = level;
1375 sopt.sopt_name = name;
1376 sopt.sopt_val = val;
1377 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1383 sopt.sopt_td = NULL;
1386 panic("kern_getsockopt called with bad valseg");
1390 error = getsock(td->td_proc->p_fd, s, &fp);
1393 error = sogetopt(so, &sopt);
1394 *valsize = sopt.sopt_valsize;
1402 * getsockname1() - Get socket name.
1408 getsockname1(td, uap, compat)
1410 register struct getsockname_args /* {
1412 struct sockaddr * __restrict asa;
1413 socklen_t * __restrict alen;
1418 struct sockaddr *sa;
1424 error = getsock(td->td_proc->p_fd, uap->fdes, &fp);
1428 error = copyin(uap->alen, &len, sizeof (len));
1436 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
1444 len = MIN(len, sa->sa_len);
1445 #ifdef COMPAT_OLDSOCK
1447 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1449 error = copyout(sa, uap->asa, (u_int)len);
1452 error = copyout(&len, uap->alen, sizeof (len));
1467 getsockname(td, uap)
1469 struct getsockname_args *uap;
1472 return (getsockname1(td, uap, 0));
1475 #ifdef COMPAT_OLDSOCK
1480 ogetsockname(td, uap)
1482 struct getsockname_args *uap;
1485 return (getsockname1(td, uap, 1));
1487 #endif /* COMPAT_OLDSOCK */
1490 * getpeername1() - Get name of peer for connected socket.
1496 getpeername1(td, uap, compat)
1498 register struct getpeername_args /* {
1500 struct sockaddr * __restrict asa;
1501 socklen_t * __restrict alen;
1506 struct sockaddr *sa;
1512 error = getsock(td->td_proc->p_fd, uap->fdes, &fp);
1516 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1520 error = copyin(uap->alen, &len, sizeof (len));
1528 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa);
1535 len = MIN(len, sa->sa_len);
1536 #ifdef COMPAT_OLDSOCK
1538 ((struct osockaddr *)sa)->sa_family =
1541 error = copyout(sa, uap->asa, (u_int)len);
1545 error = copyout(&len, uap->alen, sizeof (len));
1560 getpeername(td, uap)
1562 struct getpeername_args *uap;
1565 return (getpeername1(td, uap, 0));
1568 #ifdef COMPAT_OLDSOCK
1573 ogetpeername(td, uap)
1575 struct ogetpeername_args *uap;
1578 /* XXX uap should have type `getpeername_args *' to begin with. */
1579 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1581 #endif /* COMPAT_OLDSOCK */
1584 sockargs(mp, buf, buflen, type)
1589 register struct sockaddr *sa;
1590 register struct mbuf *m;
1593 if ((u_int)buflen > MLEN) {
1594 #ifdef COMPAT_OLDSOCK
1595 if (type == MT_SONAME && (u_int)buflen <= 112)
1596 buflen = MLEN; /* unix domain compat. hack */
1599 if ((u_int)buflen > MCLBYTES)
1602 m = m_get(M_TRYWAIT, type);
1605 if ((u_int)buflen > MLEN) {
1606 MCLGET(m, M_TRYWAIT);
1607 if ((m->m_flags & M_EXT) == 0) {
1613 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1618 if (type == MT_SONAME) {
1619 sa = mtod(m, struct sockaddr *);
1621 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1622 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1623 sa->sa_family = sa->sa_len;
1625 sa->sa_len = buflen;
1632 getsockaddr(namp, uaddr, len)
1633 struct sockaddr **namp;
1637 struct sockaddr *sa;
1640 if (len > SOCK_MAXADDRLEN)
1641 return (ENAMETOOLONG);
1642 if (len < offsetof(struct sockaddr, sa_data[0]))
1644 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1645 error = copyin(uaddr, sa, len);
1649 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1650 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1651 sa->sa_family = sa->sa_len;
1660 * Detach mapped page and release resources back to the system.
1663 sf_buf_mext(void *addr, void *args)
1667 m = sf_buf_page(args);
1669 vm_page_lock_queues();
1670 vm_page_unwire(m, 0);
1672 * Check for the object going away on us. This can
1673 * happen since we don't hold a reference to it.
1674 * If so, we're responsible for freeing the page.
1676 if (m->wire_count == 0 && m->object == NULL)
1678 vm_page_unlock_queues();
1686 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1687 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1689 * Send a file specified by 'fd' and starting at 'offset' to a socket
1690 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1691 * nbytes == 0. Optionally add a header and/or trailer to the socket
1692 * output. If specified, write the total number of bytes sent into *sbytes.
1696 sendfile(struct thread *td, struct sendfile_args *uap)
1699 return (do_sendfile(td, uap, 0));
1702 #ifdef COMPAT_FREEBSD4
1704 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1706 struct sendfile_args args;
1710 args.offset = uap->offset;
1711 args.nbytes = uap->nbytes;
1712 args.hdtr = uap->hdtr;
1713 args.sbytes = uap->sbytes;
1714 args.flags = uap->flags;
1716 return (do_sendfile(td, &args, 1));
1718 #endif /* COMPAT_FREEBSD4 */
1721 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1724 struct vm_object *obj;
1725 struct socket *so = NULL;
1726 struct mbuf *m, *m_header = NULL;
1729 struct writev_args nuap;
1730 struct sf_hdtr hdtr;
1731 struct uio *hdr_uio = NULL;
1732 off_t off, xfsize, hdtr_size, sbytes = 0;
1733 int error, headersize = 0, headersent = 0;
1740 * The descriptor must be a regular file and have a backing VM object.
1742 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1744 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1746 VOP_UNLOCK(vp, 0, td);
1751 if ((error = fgetsock(td, uap->s, &so, NULL)) != 0)
1753 if (so->so_type != SOCK_STREAM) {
1757 if ((so->so_state & SS_ISCONNECTED) == 0) {
1761 if (uap->offset < 0) {
1768 error = mac_check_socket_send(td->td_ucred, so);
1775 * If specified, get the pointer to the sf_hdtr struct for
1776 * any headers/trailers.
1778 if (uap->hdtr != NULL) {
1779 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1785 if (hdtr.headers != NULL) {
1786 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1789 hdr_uio->uio_td = td;
1790 hdr_uio->uio_rw = UIO_WRITE;
1791 if (hdr_uio->uio_resid > 0) {
1792 m_header = m_uiotombuf(hdr_uio, M_DONTWAIT, 0);
1793 if (m_header == NULL)
1795 headersize = m_header->m_pkthdr.len;
1797 sbytes += headersize;
1803 * Protect against multiple writers to the socket.
1805 SOCKBUF_LOCK(&so->so_snd);
1806 (void) sblock(&so->so_snd, M_WAITOK);
1807 SOCKBUF_UNLOCK(&so->so_snd);
1810 * Loop through the pages in the file, starting with the requested
1811 * offset. Get a file page (do I/O if necessary), map the file page
1812 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1815 for (off = uap->offset; ; off += xfsize, sbytes += xfsize) {
1819 pindex = OFF_TO_IDX(off);
1820 VM_OBJECT_LOCK(obj);
1823 * Calculate the amount to transfer. Not to exceed a page,
1824 * the EOF, or the passed in nbytes.
1826 xfsize = obj->un_pager.vnp.vnp_size - off;
1827 VM_OBJECT_UNLOCK(obj);
1828 if (xfsize > PAGE_SIZE)
1830 pgoff = (vm_offset_t)(off & PAGE_MASK);
1831 if (PAGE_SIZE - pgoff < xfsize)
1832 xfsize = PAGE_SIZE - pgoff;
1833 if (uap->nbytes && xfsize > (uap->nbytes - sbytes))
1834 xfsize = uap->nbytes - sbytes;
1836 if (m_header != NULL) {
1839 SOCKBUF_LOCK(&so->so_snd);
1845 * Optimize the non-blocking case by looking at the socket space
1846 * before going to the extra work of constituting the sf_buf.
1848 SOCKBUF_LOCK(&so->so_snd);
1849 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1850 if (so->so_snd.sb_state & SBS_CANTSENDMORE)
1854 sbunlock(&so->so_snd);
1855 SOCKBUF_UNLOCK(&so->so_snd);
1858 SOCKBUF_UNLOCK(&so->so_snd);
1859 VM_OBJECT_LOCK(obj);
1861 * Attempt to look up the page.
1863 * Allocate if not found
1865 * Wait and loop if busy.
1867 pg = vm_page_lookup(obj, pindex);
1870 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NOBUSY |
1871 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
1873 VM_OBJECT_UNLOCK(obj);
1875 VM_OBJECT_LOCK(obj);
1878 vm_page_lock_queues();
1880 vm_page_lock_queues();
1881 if (vm_page_sleep_if_busy(pg, TRUE, "sfpbsy"))
1884 * Wire the page so it does not get ripped out from
1891 * If page is not valid for what we need, initiate I/O
1894 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize)) {
1895 VM_OBJECT_UNLOCK(obj);
1896 } else if (uap->flags & SF_NODISKIO) {
1902 * Ensure that our page is still around when the I/O
1905 vm_page_io_start(pg);
1906 vm_page_unlock_queues();
1907 VM_OBJECT_UNLOCK(obj);
1910 * Get the page from backing store.
1912 bsize = vp->v_mount->mnt_stat.f_iosize;
1913 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
1915 * XXXMAC: Because we don't have fp->f_cred here,
1916 * we pass in NOCRED. This is probably wrong, but
1917 * is consistent with our original implementation.
1919 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
1920 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
1921 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
1922 td->td_ucred, NOCRED, &resid, td);
1923 VOP_UNLOCK(vp, 0, td);
1924 VM_OBJECT_LOCK(obj);
1925 vm_page_lock_queues();
1926 vm_page_io_finish(pg);
1928 VM_OBJECT_UNLOCK(obj);
1933 vm_page_unwire(pg, 0);
1935 * See if anyone else might know about this page.
1936 * If not and it is not valid, then free it.
1938 if (pg->wire_count == 0 && pg->valid == 0 &&
1939 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1940 pg->hold_count == 0) {
1943 vm_page_unlock_queues();
1944 VM_OBJECT_UNLOCK(obj);
1945 SOCKBUF_LOCK(&so->so_snd);
1946 sbunlock(&so->so_snd);
1947 SOCKBUF_UNLOCK(&so->so_snd);
1950 vm_page_unlock_queues();
1953 * Get a sendfile buf. We usually wait as long as necessary,
1954 * but this wait can be interrupted.
1956 if ((sf = sf_buf_alloc(pg, SFB_CATCH)) == NULL) {
1957 mbstat.sf_allocfail++;
1958 vm_page_lock_queues();
1959 vm_page_unwire(pg, 0);
1960 if (pg->wire_count == 0 && pg->object == NULL)
1962 vm_page_unlock_queues();
1963 SOCKBUF_LOCK(&so->so_snd);
1964 sbunlock(&so->so_snd);
1965 SOCKBUF_UNLOCK(&so->so_snd);
1971 * Get an mbuf header and set it up as having external storage.
1974 MGET(m, M_TRYWAIT, MT_DATA);
1976 MGETHDR(m, M_TRYWAIT, MT_DATA);
1979 sf_buf_mext((void *)sf_buf_kva(sf), sf);
1980 SOCKBUF_LOCK(&so->so_snd);
1981 sbunlock(&so->so_snd);
1982 SOCKBUF_UNLOCK(&so->so_snd);
1986 * Setup external storage for mbuf.
1988 MEXTADD(m, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext, sf, M_RDONLY,
1990 m->m_data = (char *)sf_buf_kva(sf) + pgoff;
1991 m->m_pkthdr.len = m->m_len = xfsize;
2001 * Add the buffer to the socket buffer chain.
2003 SOCKBUF_LOCK(&so->so_snd);
2006 * Make sure that the socket is still able to take more data.
2007 * CANTSENDMORE being true usually means that the connection
2008 * was closed. so_error is true when an error was sensed after
2010 * The state is checked after the page mapping and buffer
2011 * allocation above since those operations may block and make
2012 * any socket checks stale. From this point forward, nothing
2013 * blocks before the pru_send (or more accurately, any blocking
2014 * results in a loop back to here to re-check).
2016 SOCKBUF_LOCK_ASSERT(&so->so_snd);
2017 if ((so->so_snd.sb_state & SBS_CANTSENDMORE) || so->so_error) {
2018 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2021 error = so->so_error;
2025 sbunlock(&so->so_snd);
2026 SOCKBUF_UNLOCK(&so->so_snd);
2030 * Wait for socket space to become available. We do this just
2031 * after checking the connection state above in order to avoid
2032 * a race condition with sbwait().
2034 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
2035 if (so->so_state & SS_NBIO) {
2037 sbunlock(&so->so_snd);
2038 SOCKBUF_UNLOCK(&so->so_snd);
2042 error = sbwait(&so->so_snd);
2044 * An error from sbwait usually indicates that we've
2045 * been interrupted by a signal. If we've sent anything
2046 * then return bytes sent, otherwise return the error.
2050 sbunlock(&so->so_snd);
2051 SOCKBUF_UNLOCK(&so->so_snd);
2056 SOCKBUF_UNLOCK(&so->so_snd);
2057 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, td);
2059 SOCKBUF_LOCK(&so->so_snd);
2060 sbunlock(&so->so_snd);
2061 SOCKBUF_UNLOCK(&so->so_snd);
2066 SOCKBUF_LOCK(&so->so_snd);
2067 sbunlock(&so->so_snd);
2068 SOCKBUF_UNLOCK(&so->so_snd);
2071 * Send trailers. Wimp out and use writev(2).
2073 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
2075 nuap.iovp = hdtr.trailers;
2076 nuap.iovcnt = hdtr.trl_cnt;
2077 error = writev(td, &nuap);
2081 sbytes += td->td_retval[0];
2083 hdtr_size += td->td_retval[0];
2089 hdtr_size += headersize;
2092 sbytes -= headersize;
2095 * If there was no error we have to clear td->td_retval[0]
2096 * because it may have been set by writev.
2099 td->td_retval[0] = 0;
2101 if (uap->sbytes != NULL) {
2103 sbytes += hdtr_size;
2104 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2110 if (hdr_uio != NULL)
2111 free(hdr_uio, M_IOV);
2117 if (error == ERESTART)