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;
162 error = mac_check_socket_create(td->td_ucred, uap->domain, uap->type,
167 fdp = td->td_proc->p_fd;
168 error = falloc(td, &fp, &fd);
171 /* An extra reference on `fp' has been held for us by falloc(). */
173 error = socreate(uap->domain, &so, uap->type, uap->protocol,
177 fdclose(fdp, fp, fd, td);
179 FILEDESC_LOCK_FAST(fdp);
180 fp->f_data = so; /* already has ref count */
181 fp->f_flag = FREAD|FWRITE;
182 fp->f_ops = &socketops;
183 fp->f_type = DTYPE_SOCKET;
184 FILEDESC_UNLOCK_FAST(fdp);
185 td->td_retval[0] = fd;
198 register struct bind_args /* {
207 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
210 return (kern_bind(td, uap->s, sa));
214 kern_bind(td, fd, sa)
224 error = getsock(td->td_proc->p_fd, fd, &fp);
230 error = mac_check_socket_bind(td->td_ucred, so, sa);
235 error = sobind(so, sa, td);
253 register struct listen_args /* {
263 error = getsock(td->td_proc->p_fd, uap->s, &fp);
268 error = mac_check_socket_listen(td->td_ucred, so);
273 error = solisten(so, uap->backlog, td);
288 accept1(td, uap, compat)
290 register struct accept_args /* {
292 struct sockaddr * __restrict name;
293 socklen_t * __restrict anamelen;
297 struct filedesc *fdp;
298 struct file *nfp = NULL;
299 struct sockaddr *sa = NULL;
302 struct socket *head, *so;
308 fdp = td->td_proc->p_fd;
310 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
317 error = fgetsock(td, uap->s, &head, &fflag);
320 if ((head->so_options & SO_ACCEPTCONN) == 0) {
326 error = mac_check_socket_accept(td->td_ucred, head);
331 error = falloc(td, &nfp, &fd);
335 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
340 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
341 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
342 head->so_error = ECONNABORTED;
345 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
352 if (head->so_error) {
353 error = head->so_error;
358 so = TAILQ_FIRST(&head->so_comp);
359 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
360 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
363 * Before changing the flags on the socket, we have to bump the
364 * reference count. Otherwise, if the protocol calls sofree(),
365 * the socket will be released due to a zero refcount.
367 SOCK_LOCK(so); /* soref() and so_state update */
368 soref(so); /* file descriptor reference */
370 TAILQ_REMOVE(&head->so_comp, so, so_list);
372 so->so_state |= (head->so_state & SS_NBIO);
373 so->so_qstate &= ~SQ_COMP;
379 /* An extra reference on `nfp' has been held for us by falloc(). */
380 td->td_retval[0] = fd;
382 /* connection has been removed from the listen queue */
383 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
385 pgid = fgetown(&head->so_sigio);
387 fsetown(pgid, &so->so_sigio);
390 nfp->f_data = so; /* nfp has ref count from falloc */
392 nfp->f_ops = &socketops;
393 nfp->f_type = DTYPE_SOCKET;
395 /* Sync socket nonblocking/async state with file flags */
396 tmp = fflag & FNONBLOCK;
397 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
398 tmp = fflag & FASYNC;
399 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
401 error = soaccept(so, &sa);
404 * return a namelen of zero for older code which might
405 * ignore the return value from accept.
407 if (uap->name != NULL) {
409 (void) copyout(&namelen,
410 uap->anamelen, sizeof(*uap->anamelen));
422 /* check sa_len before it is destroyed */
423 if (namelen > sa->sa_len)
424 namelen = sa->sa_len;
425 #ifdef COMPAT_OLDSOCK
427 ((struct osockaddr *)sa)->sa_family =
430 error = copyout(sa, uap->name, (u_int)namelen);
433 error = copyout(&namelen,
434 uap->anamelen, sizeof (*uap->anamelen));
441 * close the new descriptor, assuming someone hasn't ripped it
445 fdclose(fdp, nfp, fd, td);
448 * Release explicitly held references before returning.
460 * MPSAFE (accept1() is MPSAFE)
465 struct accept_args *uap;
468 return (accept1(td, uap, 0));
471 #ifdef COMPAT_OLDSOCK
473 * MPSAFE (accept1() is MPSAFE)
478 struct accept_args *uap;
481 return (accept1(td, uap, 1));
483 #endif /* COMPAT_OLDSOCK */
492 register struct connect_args /* {
501 error = getsockaddr(&sa, uap->name, uap->namelen);
505 return (kern_connect(td, uap->s, sa));
510 kern_connect(td, fd, sa)
521 error = getsock(td->td_proc->p_fd, fd, &fp);
525 if (so->so_state & SS_ISCONNECTING) {
531 error = mac_check_socket_connect(td->td_ucred, so, sa);
536 error = soconnect(so, sa, td);
539 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
544 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
545 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
548 if (error == EINTR || error == ERESTART)
554 error = so->so_error;
560 so->so_state &= ~SS_ISCONNECTING;
561 if (error == ERESTART)
577 register struct socketpair_args /* {
584 register struct filedesc *fdp = td->td_proc->p_fd;
585 struct file *fp1, *fp2;
586 struct socket *so1, *so2;
587 int fd, error, sv[2];
590 /* We might want to have a separate check for socket pairs. */
591 error = mac_check_socket_create(td->td_ucred, uap->domain, uap->type,
598 error = socreate(uap->domain, &so1, uap->type, uap->protocol,
602 error = socreate(uap->domain, &so2, uap->type, uap->protocol,
606 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
607 error = falloc(td, &fp1, &fd);
611 fp1->f_data = so1; /* so1 already has ref count */
612 error = falloc(td, &fp2, &fd);
615 fp2->f_data = so2; /* so2 already has ref count */
617 error = soconnect2(so1, so2);
620 if (uap->type == SOCK_DGRAM) {
622 * Datagram socket connection is asymmetric.
624 error = soconnect2(so2, so1);
629 fp1->f_flag = FREAD|FWRITE;
630 fp1->f_ops = &socketops;
631 fp1->f_type = DTYPE_SOCKET;
634 fp2->f_flag = FREAD|FWRITE;
635 fp2->f_ops = &socketops;
636 fp2->f_type = DTYPE_SOCKET;
638 error = copyout(sv, uap->rsv, 2 * sizeof (int));
643 fdclose(fdp, fp2, sv[1], td);
646 fdclose(fdp, fp1, sv[0], td);
658 sendit(td, s, mp, flags)
659 register struct thread *td;
661 register struct msghdr *mp;
664 struct mbuf *control;
668 if (mp->msg_name != NULL) {
669 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
679 if (mp->msg_control) {
680 if (mp->msg_controllen < sizeof(struct cmsghdr)
681 #ifdef COMPAT_OLDSOCK
682 && mp->msg_flags != MSG_COMPAT
688 error = sockargs(&control, mp->msg_control,
689 mp->msg_controllen, MT_CONTROL);
692 #ifdef COMPAT_OLDSOCK
693 if (mp->msg_flags == MSG_COMPAT) {
694 register struct cmsghdr *cm;
696 M_PREPEND(control, sizeof(*cm), M_TRYWAIT);
701 cm = mtod(control, struct cmsghdr *);
702 cm->cmsg_len = control->m_len;
703 cm->cmsg_level = SOL_SOCKET;
704 cm->cmsg_type = SCM_RIGHTS;
712 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
721 kern_sendit(td, s, mp, flags, control, segflg)
726 struct mbuf *control;
736 struct uio *ktruio = NULL;
740 error = getsock(td->td_proc->p_fd, s, &fp);
743 so = (struct socket *)fp->f_data;
747 error = mac_check_socket_send(td->td_ucred, so);
753 auio.uio_iov = mp->msg_iov;
754 auio.uio_iovcnt = mp->msg_iovlen;
755 auio.uio_segflg = segflg;
756 auio.uio_rw = UIO_WRITE;
758 auio.uio_offset = 0; /* XXX */
761 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
762 if ((auio.uio_resid += iov->iov_len) < 0) {
768 if (KTRPOINT(td, KTR_GENIO))
769 ktruio = cloneuio(&auio);
771 len = auio.uio_resid;
772 error = so->so_proto->pr_usrreqs->pru_sosend(so, mp->msg_name, &auio,
773 0, control, flags, td);
775 if (auio.uio_resid != len && (error == ERESTART ||
776 error == EINTR || error == EWOULDBLOCK))
778 /* Generation of SIGPIPE can be controlled per socket */
779 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
780 !(flags & MSG_NOSIGNAL)) {
781 PROC_LOCK(td->td_proc);
782 psignal(td->td_proc, SIGPIPE);
783 PROC_UNLOCK(td->td_proc);
787 td->td_retval[0] = len - auio.uio_resid;
789 if (ktruio != NULL) {
790 ktruio->uio_resid = td->td_retval[0];
791 ktrgenio(s, UIO_WRITE, ktruio, error);
807 register struct sendto_args /* {
820 msg.msg_name = uap->to;
821 msg.msg_namelen = uap->tolen;
825 #ifdef COMPAT_OLDSOCK
828 aiov.iov_base = uap->buf;
829 aiov.iov_len = uap->len;
830 error = sendit(td, uap->s, &msg, uap->flags);
834 #ifdef COMPAT_OLDSOCK
841 register struct osend_args /* {
856 aiov.iov_base = uap->buf;
857 aiov.iov_len = uap->len;
860 error = sendit(td, uap->s, &msg, uap->flags);
870 struct osendmsg_args /* {
880 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
883 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
887 msg.msg_flags = MSG_COMPAT;
888 error = sendit(td, uap->s, &msg, uap->flags);
900 struct sendmsg_args /* {
910 error = copyin(uap->msg, &msg, sizeof (msg));
913 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
917 #ifdef COMPAT_OLDSOCK
920 error = sendit(td, uap->s, &msg, uap->flags);
926 kern_recvit(td, s, mp, namelenp, segflg, controlp)
932 struct mbuf **controlp;
939 struct mbuf *m, *control = 0;
943 struct sockaddr *fromsa = 0;
945 struct uio *ktruio = NULL;
952 error = getsock(td->td_proc->p_fd, s, &fp);
961 error = mac_check_socket_receive(td->td_ucred, so);
970 auio.uio_iov = mp->msg_iov;
971 auio.uio_iovcnt = mp->msg_iovlen;
972 auio.uio_segflg = segflg;
973 auio.uio_rw = UIO_READ;
975 auio.uio_offset = 0; /* XXX */
978 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
979 if ((auio.uio_resid += iov->iov_len) < 0) {
986 if (KTRPOINT(td, KTR_GENIO))
987 ktruio = cloneuio(&auio);
989 len = auio.uio_resid;
990 error = so->so_proto->pr_usrreqs->pru_soreceive(so, &fromsa, &auio,
992 (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
995 if (auio.uio_resid != (int)len && (error == ERESTART ||
996 error == EINTR || error == EWOULDBLOCK))
1000 if (ktruio != NULL) {
1001 ktruio->uio_resid = (int)len - auio.uio_resid;
1002 ktrgenio(s, UIO_READ, ktruio, error);
1007 td->td_retval[0] = (int)len - auio.uio_resid;
1009 len = mp->msg_namelen;
1010 if (len <= 0 || fromsa == 0)
1013 /* save sa_len before it is destroyed by MSG_COMPAT */
1014 len = MIN(len, fromsa->sa_len);
1015 #ifdef COMPAT_OLDSOCK
1016 if (mp->msg_flags & MSG_COMPAT)
1017 ((struct osockaddr *)fromsa)->sa_family =
1020 error = copyout(fromsa, mp->msg_name, (unsigned)len);
1024 mp->msg_namelen = len;
1026 (error = copyout(&len, namelenp, sizeof (socklen_t)))) {
1027 #ifdef COMPAT_OLDSOCK
1028 if (mp->msg_flags & MSG_COMPAT)
1029 error = 0; /* old recvfrom didn't check */
1035 if (mp->msg_control && controlp == NULL) {
1036 #ifdef COMPAT_OLDSOCK
1038 * We assume that old recvmsg calls won't receive access
1039 * rights and other control info, esp. as control info
1040 * is always optional and those options didn't exist in 4.3.
1041 * If we receive rights, trim the cmsghdr; anything else
1044 if (control && mp->msg_flags & MSG_COMPAT) {
1045 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1047 mtod(control, struct cmsghdr *)->cmsg_type !=
1049 mp->msg_controllen = 0;
1052 control->m_len -= sizeof (struct cmsghdr);
1053 control->m_data += sizeof (struct cmsghdr);
1056 len = mp->msg_controllen;
1058 mp->msg_controllen = 0;
1059 ctlbuf = mp->msg_control;
1061 while (m && len > 0) {
1062 unsigned int tocopy;
1064 if (len >= m->m_len)
1067 mp->msg_flags |= MSG_CTRUNC;
1071 if ((error = copyout(mtod(m, caddr_t),
1072 ctlbuf, tocopy)) != 0)
1079 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1085 FREE(fromsa, M_SONAME);
1087 if (error == 0 && controlp != NULL)
1088 *controlp = control;
1096 recvit(td, s, mp, namelenp)
1103 return (kern_recvit(td, s, mp, namelenp, UIO_USERSPACE, NULL));
1112 register struct recvfrom_args /* {
1117 struct sockaddr * __restrict from;
1118 socklen_t * __restrict fromlenaddr;
1125 if (uap->fromlenaddr) {
1126 error = copyin(uap->fromlenaddr,
1127 &msg.msg_namelen, sizeof (msg.msg_namelen));
1131 msg.msg_namelen = 0;
1133 msg.msg_name = uap->from;
1134 msg.msg_iov = &aiov;
1136 aiov.iov_base = uap->buf;
1137 aiov.iov_len = uap->len;
1138 msg.msg_control = 0;
1139 msg.msg_flags = uap->flags;
1140 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1145 #ifdef COMPAT_OLDSOCK
1152 struct recvfrom_args *uap;
1155 uap->flags |= MSG_COMPAT;
1156 return (recvfrom(td, uap));
1161 #ifdef COMPAT_OLDSOCK
1168 register struct orecv_args /* {
1180 msg.msg_namelen = 0;
1181 msg.msg_iov = &aiov;
1183 aiov.iov_base = uap->buf;
1184 aiov.iov_len = uap->len;
1185 msg.msg_control = 0;
1186 msg.msg_flags = uap->flags;
1187 error = recvit(td, uap->s, &msg, NULL);
1192 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1193 * overlays the new one, missing only the flags, and with the (old) access
1194 * rights where the control fields are now.
1201 struct orecvmsg_args /* {
1203 struct omsghdr *msg;
1211 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1214 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1217 msg.msg_flags = uap->flags | MSG_COMPAT;
1219 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1220 if (msg.msg_controllen && error == 0)
1221 error = copyout(&msg.msg_controllen,
1222 &uap->msg->msg_accrightslen, sizeof (int));
1234 struct recvmsg_args /* {
1241 struct iovec *uiov, *iov;
1244 error = copyin(uap->msg, &msg, sizeof (msg));
1247 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1250 msg.msg_flags = uap->flags;
1251 #ifdef COMPAT_OLDSOCK
1252 msg.msg_flags &= ~MSG_COMPAT;
1256 error = recvit(td, uap->s, &msg, NULL);
1259 error = copyout(&msg, uap->msg, sizeof(msg));
1272 register struct shutdown_args /* {
1282 error = getsock(td->td_proc->p_fd, uap->s, &fp);
1285 error = soshutdown(so, uap->how);
1299 register struct setsockopt_args /* {
1308 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1309 uap->val, UIO_USERSPACE, uap->valsize));
1313 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1319 enum uio_seg valseg;
1325 struct sockopt sopt;
1327 if (val == NULL && valsize != 0)
1332 sopt.sopt_dir = SOPT_SET;
1333 sopt.sopt_level = level;
1334 sopt.sopt_name = name;
1335 sopt.sopt_val = val;
1336 sopt.sopt_valsize = valsize;
1342 sopt.sopt_td = NULL;
1345 panic("kern_setsockopt called with bad valseg");
1349 error = getsock(td->td_proc->p_fd, s, &fp);
1352 error = sosetopt(so, &sopt);
1366 register struct getsockopt_args /* {
1370 void * __restrict val;
1371 socklen_t * __restrict avalsize;
1378 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1383 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1384 uap->val, UIO_USERSPACE, &valsize);
1387 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1392 * Kernel version of getsockopt.
1393 * optval can be a userland or userspace. optlen is always a kernel pointer.
1396 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1402 enum uio_seg valseg;
1408 struct sockopt sopt;
1415 sopt.sopt_dir = SOPT_GET;
1416 sopt.sopt_level = level;
1417 sopt.sopt_name = name;
1418 sopt.sopt_val = val;
1419 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1425 sopt.sopt_td = NULL;
1428 panic("kern_getsockopt called with bad valseg");
1432 error = getsock(td->td_proc->p_fd, s, &fp);
1435 error = sogetopt(so, &sopt);
1436 *valsize = sopt.sopt_valsize;
1444 * getsockname1() - Get socket name.
1450 getsockname1(td, uap, compat)
1452 register struct getsockname_args /* {
1454 struct sockaddr * __restrict asa;
1455 socklen_t * __restrict alen;
1460 struct sockaddr *sa;
1466 error = getsock(td->td_proc->p_fd, uap->fdes, &fp);
1470 error = copyin(uap->alen, &len, sizeof (len));
1478 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
1486 len = MIN(len, sa->sa_len);
1487 #ifdef COMPAT_OLDSOCK
1489 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1491 error = copyout(sa, uap->asa, (u_int)len);
1494 error = copyout(&len, uap->alen, sizeof (len));
1509 getsockname(td, uap)
1511 struct getsockname_args *uap;
1514 return (getsockname1(td, uap, 0));
1517 #ifdef COMPAT_OLDSOCK
1522 ogetsockname(td, uap)
1524 struct getsockname_args *uap;
1527 return (getsockname1(td, uap, 1));
1529 #endif /* COMPAT_OLDSOCK */
1532 * getpeername1() - Get name of peer for connected socket.
1538 getpeername1(td, uap, compat)
1540 register struct getpeername_args /* {
1542 struct sockaddr * __restrict asa;
1543 socklen_t * __restrict alen;
1548 struct sockaddr *sa;
1554 error = getsock(td->td_proc->p_fd, uap->fdes, &fp);
1558 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1562 error = copyin(uap->alen, &len, sizeof (len));
1570 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa);
1577 len = MIN(len, sa->sa_len);
1578 #ifdef COMPAT_OLDSOCK
1580 ((struct osockaddr *)sa)->sa_family =
1583 error = copyout(sa, uap->asa, (u_int)len);
1587 error = copyout(&len, uap->alen, sizeof (len));
1602 getpeername(td, uap)
1604 struct getpeername_args *uap;
1607 return (getpeername1(td, uap, 0));
1610 #ifdef COMPAT_OLDSOCK
1615 ogetpeername(td, uap)
1617 struct ogetpeername_args *uap;
1620 /* XXX uap should have type `getpeername_args *' to begin with. */
1621 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1623 #endif /* COMPAT_OLDSOCK */
1626 sockargs(mp, buf, buflen, type)
1631 register struct sockaddr *sa;
1632 register struct mbuf *m;
1635 if ((u_int)buflen > MLEN) {
1636 #ifdef COMPAT_OLDSOCK
1637 if (type == MT_SONAME && (u_int)buflen <= 112)
1638 buflen = MLEN; /* unix domain compat. hack */
1641 if ((u_int)buflen > MCLBYTES)
1644 m = m_get(M_TRYWAIT, type);
1647 if ((u_int)buflen > MLEN) {
1648 MCLGET(m, M_TRYWAIT);
1649 if ((m->m_flags & M_EXT) == 0) {
1655 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1660 if (type == MT_SONAME) {
1661 sa = mtod(m, struct sockaddr *);
1663 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1664 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1665 sa->sa_family = sa->sa_len;
1667 sa->sa_len = buflen;
1674 getsockaddr(namp, uaddr, len)
1675 struct sockaddr **namp;
1679 struct sockaddr *sa;
1682 if (len > SOCK_MAXADDRLEN)
1683 return (ENAMETOOLONG);
1684 if (len < offsetof(struct sockaddr, sa_data[0]))
1686 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1687 error = copyin(uaddr, sa, len);
1691 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1692 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1693 sa->sa_family = sa->sa_len;
1702 * Detach mapped page and release resources back to the system.
1705 sf_buf_mext(void *addr, void *args)
1709 m = sf_buf_page(args);
1711 vm_page_lock_queues();
1712 vm_page_unwire(m, 0);
1714 * Check for the object going away on us. This can
1715 * happen since we don't hold a reference to it.
1716 * If so, we're responsible for freeing the page.
1718 if (m->wire_count == 0 && m->object == NULL)
1720 vm_page_unlock_queues();
1728 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1729 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1731 * Send a file specified by 'fd' and starting at 'offset' to a socket
1732 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1733 * nbytes == 0. Optionally add a header and/or trailer to the socket
1734 * output. If specified, write the total number of bytes sent into *sbytes.
1738 sendfile(struct thread *td, struct sendfile_args *uap)
1741 return (do_sendfile(td, uap, 0));
1745 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1747 struct sf_hdtr hdtr;
1748 struct uio *hdr_uio, *trl_uio;
1751 hdr_uio = trl_uio = NULL;
1753 if (uap->hdtr != NULL) {
1754 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1757 if (hdtr.headers != NULL) {
1758 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1762 if (hdtr.trailers != NULL) {
1763 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1770 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1773 free(hdr_uio, M_IOV);
1775 free(trl_uio, M_IOV);
1779 #ifdef COMPAT_FREEBSD4
1781 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1783 struct sendfile_args args;
1787 args.offset = uap->offset;
1788 args.nbytes = uap->nbytes;
1789 args.hdtr = uap->hdtr;
1790 args.sbytes = uap->sbytes;
1791 args.flags = uap->flags;
1793 return (do_sendfile(td, &args, 1));
1795 #endif /* COMPAT_FREEBSD4 */
1798 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1799 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1802 struct vm_object *obj;
1803 struct socket *so = NULL;
1804 struct mbuf *m, *m_header = NULL;
1807 off_t off, xfsize, hdtr_size, sbytes = 0;
1808 int error, headersize = 0, headersent = 0;
1815 * The descriptor must be a regular file and have a backing VM object.
1817 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1819 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1821 VOP_UNLOCK(vp, 0, td);
1826 if ((error = fgetsock(td, uap->s, &so, NULL)) != 0)
1828 if (so->so_type != SOCK_STREAM) {
1832 if ((so->so_state & SS_ISCONNECTED) == 0) {
1836 if (uap->offset < 0) {
1843 error = mac_check_socket_send(td->td_ucred, so);
1850 * If specified, get the pointer to the sf_hdtr struct for
1851 * any headers/trailers.
1853 if (hdr_uio != NULL) {
1854 hdr_uio->uio_td = td;
1855 hdr_uio->uio_rw = UIO_WRITE;
1856 if (hdr_uio->uio_resid > 0) {
1857 m_header = m_uiotombuf(hdr_uio, M_DONTWAIT, 0, 0);
1858 if (m_header == NULL)
1860 headersize = m_header->m_pkthdr.len;
1862 sbytes += headersize;
1867 * Protect against multiple writers to the socket.
1869 SOCKBUF_LOCK(&so->so_snd);
1870 (void) sblock(&so->so_snd, M_WAITOK);
1871 SOCKBUF_UNLOCK(&so->so_snd);
1874 * Loop through the pages in the file, starting with the requested
1875 * offset. Get a file page (do I/O if necessary), map the file page
1876 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1879 for (off = uap->offset; ; off += xfsize, sbytes += xfsize) {
1883 pindex = OFF_TO_IDX(off);
1884 VM_OBJECT_LOCK(obj);
1887 * Calculate the amount to transfer. Not to exceed a page,
1888 * the EOF, or the passed in nbytes.
1890 xfsize = obj->un_pager.vnp.vnp_size - off;
1891 VM_OBJECT_UNLOCK(obj);
1892 if (xfsize > PAGE_SIZE)
1894 pgoff = (vm_offset_t)(off & PAGE_MASK);
1895 if (PAGE_SIZE - pgoff < xfsize)
1896 xfsize = PAGE_SIZE - pgoff;
1897 if (uap->nbytes && xfsize > (uap->nbytes - sbytes))
1898 xfsize = uap->nbytes - sbytes;
1900 if (m_header != NULL) {
1903 SOCKBUF_LOCK(&so->so_snd);
1909 * Optimize the non-blocking case by looking at the socket space
1910 * before going to the extra work of constituting the sf_buf.
1912 SOCKBUF_LOCK(&so->so_snd);
1913 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1914 if (so->so_snd.sb_state & SBS_CANTSENDMORE)
1918 sbunlock(&so->so_snd);
1919 SOCKBUF_UNLOCK(&so->so_snd);
1922 SOCKBUF_UNLOCK(&so->so_snd);
1923 VM_OBJECT_LOCK(obj);
1925 * Attempt to look up the page.
1927 * Allocate if not found
1929 * Wait and loop if busy.
1931 pg = vm_page_lookup(obj, pindex);
1934 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NOBUSY |
1935 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
1937 VM_OBJECT_UNLOCK(obj);
1939 VM_OBJECT_LOCK(obj);
1942 vm_page_lock_queues();
1944 vm_page_lock_queues();
1945 if (vm_page_sleep_if_busy(pg, TRUE, "sfpbsy"))
1948 * Wire the page so it does not get ripped out from
1955 * If page is not valid for what we need, initiate I/O
1958 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize)) {
1959 VM_OBJECT_UNLOCK(obj);
1960 } else if (uap->flags & SF_NODISKIO) {
1966 * Ensure that our page is still around when the I/O
1969 vm_page_io_start(pg);
1970 vm_page_unlock_queues();
1971 VM_OBJECT_UNLOCK(obj);
1974 * Get the page from backing store.
1976 bsize = vp->v_mount->mnt_stat.f_iosize;
1977 vn_lock(vp, LK_SHARED | LK_RETRY, td);
1979 * XXXMAC: Because we don't have fp->f_cred here,
1980 * we pass in NOCRED. This is probably wrong, but
1981 * is consistent with our original implementation.
1983 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
1984 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
1985 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
1986 td->td_ucred, NOCRED, &resid, td);
1987 VOP_UNLOCK(vp, 0, td);
1988 VM_OBJECT_LOCK(obj);
1989 vm_page_lock_queues();
1990 vm_page_io_finish(pg);
1992 VM_OBJECT_UNLOCK(obj);
1997 vm_page_unwire(pg, 0);
1999 * See if anyone else might know about this page.
2000 * If not and it is not valid, then free it.
2002 if (pg->wire_count == 0 && pg->valid == 0 &&
2003 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
2004 pg->hold_count == 0) {
2007 vm_page_unlock_queues();
2008 VM_OBJECT_UNLOCK(obj);
2009 SOCKBUF_LOCK(&so->so_snd);
2010 sbunlock(&so->so_snd);
2011 SOCKBUF_UNLOCK(&so->so_snd);
2014 vm_page_unlock_queues();
2017 * Get a sendfile buf. We usually wait as long as necessary,
2018 * but this wait can be interrupted.
2020 if ((sf = sf_buf_alloc(pg, SFB_CATCH)) == NULL) {
2021 mbstat.sf_allocfail++;
2022 vm_page_lock_queues();
2023 vm_page_unwire(pg, 0);
2024 if (pg->wire_count == 0 && pg->object == NULL)
2026 vm_page_unlock_queues();
2027 SOCKBUF_LOCK(&so->so_snd);
2028 sbunlock(&so->so_snd);
2029 SOCKBUF_UNLOCK(&so->so_snd);
2035 * Get an mbuf header and set it up as having external storage.
2038 MGET(m, M_TRYWAIT, MT_DATA);
2040 MGETHDR(m, M_TRYWAIT, MT_DATA);
2043 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2044 SOCKBUF_LOCK(&so->so_snd);
2045 sbunlock(&so->so_snd);
2046 SOCKBUF_UNLOCK(&so->so_snd);
2050 * Setup external storage for mbuf.
2052 MEXTADD(m, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext, sf, M_RDONLY,
2054 m->m_data = (char *)sf_buf_kva(sf) + pgoff;
2055 m->m_pkthdr.len = m->m_len = xfsize;
2065 * Add the buffer to the socket buffer chain.
2067 SOCKBUF_LOCK(&so->so_snd);
2070 * Make sure that the socket is still able to take more data.
2071 * CANTSENDMORE being true usually means that the connection
2072 * was closed. so_error is true when an error was sensed after
2074 * The state is checked after the page mapping and buffer
2075 * allocation above since those operations may block and make
2076 * any socket checks stale. From this point forward, nothing
2077 * blocks before the pru_send (or more accurately, any blocking
2078 * results in a loop back to here to re-check).
2080 SOCKBUF_LOCK_ASSERT(&so->so_snd);
2081 if ((so->so_snd.sb_state & SBS_CANTSENDMORE) || so->so_error) {
2082 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2085 error = so->so_error;
2089 sbunlock(&so->so_snd);
2090 SOCKBUF_UNLOCK(&so->so_snd);
2094 * Wait for socket space to become available. We do this just
2095 * after checking the connection state above in order to avoid
2096 * a race condition with sbwait().
2098 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
2099 if (so->so_state & SS_NBIO) {
2101 sbunlock(&so->so_snd);
2102 SOCKBUF_UNLOCK(&so->so_snd);
2106 error = sbwait(&so->so_snd);
2108 * An error from sbwait usually indicates that we've
2109 * been interrupted by a signal. If we've sent anything
2110 * then return bytes sent, otherwise return the error.
2114 sbunlock(&so->so_snd);
2115 SOCKBUF_UNLOCK(&so->so_snd);
2120 SOCKBUF_UNLOCK(&so->so_snd);
2121 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, td);
2123 SOCKBUF_LOCK(&so->so_snd);
2124 sbunlock(&so->so_snd);
2125 SOCKBUF_UNLOCK(&so->so_snd);
2130 SOCKBUF_LOCK(&so->so_snd);
2131 sbunlock(&so->so_snd);
2132 SOCKBUF_UNLOCK(&so->so_snd);
2135 * Send trailers. Wimp out and use writev(2).
2137 if (trl_uio != NULL) {
2138 error = kern_writev(td, uap->s, trl_uio);
2142 sbytes += td->td_retval[0];
2144 hdtr_size += td->td_retval[0];
2150 hdtr_size += headersize;
2153 sbytes -= headersize;
2156 * If there was no error we have to clear td->td_retval[0]
2157 * because it may have been set by writev.
2160 td->td_retval[0] = 0;
2162 if (uap->sbytes != NULL) {
2164 sbytes += hdtr_size;
2165 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2176 if (error == ERESTART)