2 * Copyright (c) 1982, 1986, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * sendfile(2) and related extensions:
6 * Copyright (c) 1998, David Greenman. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
37 * $FreeBSD: src/sys/kern/uipc_syscalls.c,v 1.65.2.17 2003/04/04 17:11:16 tegge Exp $
38 * $DragonFly: src/sys/kern/uipc_syscalls.c,v 1.5 2003/07/19 21:14:39 dillon Exp $
41 #include "opt_compat.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/sysproto.h>
48 #include <sys/malloc.h>
49 #include <sys/filedesc.h>
50 #include <sys/event.h>
52 #include <sys/fcntl.h>
54 #include <sys/filio.h>
56 #include <sys/protosw.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/signalvar.h>
61 #include <sys/vnode.h>
63 #include <sys/mount.h>
65 #include <sys/ktrace.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_pageout.h>
71 #include <vm/vm_kern.h>
72 #include <vm/vm_extern.h>
73 #include <sys/file2.h>
75 static void sf_buf_init(void *arg);
76 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
78 static int sendit __P((int s, struct msghdr *mp, int flags));
79 static int recvit __P((int s, struct msghdr *mp,
82 static int accept1 __P((struct accept_args *uap, int compat));
83 static int do_sendfile __P((struct sendfile_args *uap, int compat));
84 static int getsockname1 __P((struct getsockname_args *uap, int compat));
85 static int getpeername1 __P((struct getpeername_args *uap, int compat));
87 static SLIST_HEAD(, sf_buf) sf_freelist;
88 static vm_offset_t sf_base;
89 static struct sf_buf *sf_bufs;
90 static int sf_buf_alloc_want;
93 * System call interface to the socket abstraction.
95 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
96 #define COMPAT_OLDSOCK
99 extern struct fileops socketops;
102 * socket_args(int domain, int type, int protocol)
105 socket(struct socket_args *uap)
107 struct thread *td = curthread;
108 struct proc *p = td->td_proc;
109 struct filedesc *fdp;
117 error = falloc(p, &fp, &fd);
121 error = socreate(uap->domain, &so, uap->type, uap->protocol, td);
123 if (fdp->fd_ofiles[fd] == fp) {
124 fdp->fd_ofiles[fd] = NULL;
128 fp->f_data = (caddr_t)so;
129 fp->f_flag = FREAD|FWRITE;
130 fp->f_ops = &socketops;
131 fp->f_type = DTYPE_SOCKET;
139 * bind_args(int s, caddr_t name, int namelen)
144 bind(struct bind_args *uap)
146 struct thread *td = curthread;
147 struct proc *p = td->td_proc;
153 error = holdsock(p->p_fd, uap->s, &fp);
156 error = getsockaddr(&sa, uap->name, uap->namelen);
161 error = sobind((struct socket *)fp->f_data, sa, td);
168 * listen_args(int s, int backlog)
172 listen(struct listen_args *uap)
174 struct thread *td = curthread;
175 struct proc *p = td->td_proc;
180 error = holdsock(p->p_fd, uap->s, &fp);
183 error = solisten((struct socket *)fp->f_data, uap->backlog, td);
189 * accept_args(int s, caddr_t name, int *anamelen)
192 accept1(struct accept_args *uap, int compat)
194 struct thread *td = curthread;
195 struct proc *p = td->td_proc;
196 struct filedesc *fdp = p->p_fd;
197 struct file *lfp = NULL;
198 struct file *nfp = NULL;
200 int namelen, error, s;
201 struct socket *head, *so;
203 u_int fflag; /* type must match fp->f_flag */
207 error = copyin((caddr_t)uap->anamelen, (caddr_t)&namelen,
214 error = holdsock(fdp, uap->s, &lfp);
218 head = (struct socket *)lfp->f_data;
219 if ((head->so_options & SO_ACCEPTCONN) == 0) {
224 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
225 if (head->so_state & SS_CANTRCVMORE) {
226 head->so_error = ECONNABORTED;
229 if ((head->so_state & SS_NBIO) != 0) {
230 head->so_error = EWOULDBLOCK;
233 error = tsleep((caddr_t)&head->so_timeo, PCATCH, "accept", 0);
239 if (head->so_error) {
240 error = head->so_error;
247 * At this point we know that there is at least one connection
248 * ready to be accepted. Remove it from the queue prior to
249 * allocating the file descriptor for it since falloc() may
250 * block allowing another process to accept the connection
253 so = TAILQ_FIRST(&head->so_comp);
254 TAILQ_REMOVE(&head->so_comp, so, so_list);
258 error = falloc(p, &nfp, &fd);
261 * Probably ran out of file descriptors. Put the
262 * unaccepted connection back onto the queue and
263 * do another wakeup so some other process might
264 * have a chance at it.
266 TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
268 wakeup_one(&head->so_timeo);
275 /* connection has been removed from the listen queue */
276 KNOTE(&head->so_rcv.sb_sel.si_note, 0);
278 so->so_state &= ~SS_COMP;
280 if (head->so_sigio != NULL)
281 fsetown(fgetown(head->so_sigio), &so->so_sigio);
283 nfp->f_data = (caddr_t)so;
285 nfp->f_ops = &socketops;
286 nfp->f_type = DTYPE_SOCKET;
287 /* Sync socket nonblocking/async state with file flags */
288 tmp = fflag & FNONBLOCK;
289 (void) fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, td);
290 tmp = fflag & FASYNC;
291 (void) fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, td);
293 error = soaccept(so, &sa);
296 * return a namelen of zero for older code which might
297 * ignore the return value from accept.
299 if (uap->name != NULL) {
301 (void) copyout((caddr_t)&namelen,
302 (caddr_t)uap->anamelen, sizeof(*uap->anamelen));
315 /* check sa_len before it is destroyed */
316 if (namelen > sa->sa_len)
317 namelen = sa->sa_len;
318 #ifdef COMPAT_OLDSOCK
320 ((struct osockaddr *)sa)->sa_family =
323 error = copyout(sa, (caddr_t)uap->name, (u_int)namelen);
326 error = copyout((caddr_t)&namelen,
327 (caddr_t)uap->anamelen, sizeof (*uap->anamelen));
334 * close the new descriptor, assuming someone hasn't ripped it
338 if (fdp->fd_ofiles[fd] == nfp) {
339 fdp->fd_ofiles[fd] = NULL;
346 * Release explicitly held references before returning.
356 accept(struct accept_args *uap)
358 return (accept1(uap, 0));
361 #ifdef COMPAT_OLDSOCK
363 oaccept(struct accept_args *uap)
366 return (accept1(uap, 1));
368 #endif /* COMPAT_OLDSOCK */
371 * connect_args(int s, caddr_t name, int namelen)
375 connect(struct connect_args *uap)
377 struct thread *td = curthread;
378 struct proc *p = td->td_proc;
384 error = holdsock(p->p_fd, uap->s, &fp);
387 so = (struct socket *)fp->f_data;
388 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
392 error = getsockaddr(&sa, uap->name, uap->namelen);
395 error = soconnect(so, sa, td);
398 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
404 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
405 error = tsleep((caddr_t)&so->so_timeo, PCATCH, "connec", 0);
410 error = so->so_error;
415 so->so_state &= ~SS_ISCONNECTING;
417 if (error == ERESTART)
425 * socketpair(int domain, int type, int protocol, int *rsv)
428 socketpair(struct socketpair_args *uap)
430 struct thread *td = curthread;
431 struct proc *p = td->td_proc;
432 struct filedesc *fdp;
433 struct file *fp1, *fp2;
434 struct socket *so1, *so2;
435 int fd, error, sv[2];
439 error = socreate(uap->domain, &so1, uap->type, uap->protocol, td);
442 error = socreate(uap->domain, &so2, uap->type, uap->protocol, td);
445 error = falloc(p, &fp1, &fd);
450 fp1->f_data = (caddr_t)so1;
451 error = falloc(p, &fp2, &fd);
455 fp2->f_data = (caddr_t)so2;
457 error = soconnect2(so1, so2);
460 if (uap->type == SOCK_DGRAM) {
462 * Datagram socket connection is asymmetric.
464 error = soconnect2(so2, so1);
468 fp1->f_flag = fp2->f_flag = FREAD|FWRITE;
469 fp1->f_ops = fp2->f_ops = &socketops;
470 fp1->f_type = fp2->f_type = DTYPE_SOCKET;
471 error = copyout((caddr_t)sv, (caddr_t)uap->rsv, 2 * sizeof (int));
476 if (fdp->fd_ofiles[sv[1]] == fp2) {
477 fdp->fd_ofiles[sv[1]] = NULL;
482 if (fdp->fd_ofiles[sv[0]] == fp1) {
483 fdp->fd_ofiles[sv[0]] = NULL;
495 sendit(int s, struct msghdr *mp, int flags)
497 struct thread *td = curthread;
498 struct proc *p = td->td_proc;
501 register struct iovec *iov;
503 struct mbuf *control;
508 struct iovec *ktriov = NULL;
512 error = holdsock(p->p_fd, s, &fp);
515 auio.uio_iov = mp->msg_iov;
516 auio.uio_iovcnt = mp->msg_iovlen;
517 auio.uio_segflg = UIO_USERSPACE;
518 auio.uio_rw = UIO_WRITE;
520 auio.uio_offset = 0; /* XXX */
523 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
524 if ((auio.uio_resid += iov->iov_len) < 0) {
530 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
538 if (mp->msg_control) {
539 if (mp->msg_controllen < sizeof(struct cmsghdr)
540 #ifdef COMPAT_OLDSOCK
541 && mp->msg_flags != MSG_COMPAT
547 error = sockargs(&control, mp->msg_control,
548 mp->msg_controllen, MT_CONTROL);
551 #ifdef COMPAT_OLDSOCK
552 if (mp->msg_flags == MSG_COMPAT) {
553 register struct cmsghdr *cm;
555 M_PREPEND(control, sizeof(*cm), M_WAIT);
560 cm = mtod(control, struct cmsghdr *);
561 cm->cmsg_len = control->m_len;
562 cm->cmsg_level = SOL_SOCKET;
563 cm->cmsg_type = SCM_RIGHTS;
571 if (KTRPOINT(td, KTR_GENIO)) {
572 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
574 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
575 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
579 len = auio.uio_resid;
580 so = (struct socket *)fp->f_data;
581 error = so->so_proto->pr_usrreqs->pru_sosend(so, to, &auio, 0, control,
584 if (auio.uio_resid != len && (error == ERESTART ||
585 error == EINTR || error == EWOULDBLOCK))
591 p->p_retval[0] = len - auio.uio_resid;
593 if (ktriov != NULL) {
595 ktruio.uio_iov = ktriov;
596 ktruio.uio_resid = p->p_retval[0];
597 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error);
599 FREE(ktriov, M_TEMP);
610 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
613 sendto(struct sendto_args *uap)
618 msg.msg_name = uap->to;
619 msg.msg_namelen = uap->tolen;
623 #ifdef COMPAT_OLDSOCK
626 aiov.iov_base = uap->buf;
627 aiov.iov_len = uap->len;
628 return (sendit(uap->s, &msg, uap->flags));
631 #ifdef COMPAT_OLDSOCK
633 * osend_args(int s, caddr_t buf, int len, int flags)
636 osend(struct osend_args *uap)
645 aiov.iov_base = uap->buf;
646 aiov.iov_len = uap->len;
649 return (sendit(uap->s, &msg, uap->flags));
653 * osendmsg_args(int s, caddr_t msg, int flags)
656 osendmsg(struct osendmsg_args *uap)
659 struct iovec aiov[UIO_SMALLIOV], *iov;
662 error = copyin(uap->msg, (caddr_t)&msg, sizeof (struct omsghdr));
665 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
666 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
668 MALLOC(iov, struct iovec *,
669 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
673 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
674 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
677 msg.msg_flags = MSG_COMPAT;
679 error = sendit(uap->s, &msg, uap->flags);
688 * sendmsg_args(int s, caddr_t msg, int flags)
691 sendmsg(struct sendmsg_args *uap)
694 struct iovec aiov[UIO_SMALLIOV], *iov;
697 error = copyin(uap->msg, (caddr_t)&msg, sizeof (msg));
700 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
701 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
703 MALLOC(iov, struct iovec *,
704 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
708 if (msg.msg_iovlen &&
709 (error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
710 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)))))
713 #ifdef COMPAT_OLDSOCK
716 error = sendit(uap->s, &msg, uap->flags);
724 recvit(int s, struct msghdr *mp, caddr_t namelenp)
726 struct thread *td = curthread;
727 struct proc *p = td->td_proc;
730 register struct iovec *iov;
733 struct mbuf *m, *control = 0;
736 struct sockaddr *fromsa = 0;
738 struct iovec *ktriov = NULL;
742 error = holdsock(p->p_fd, s, &fp);
745 auio.uio_iov = mp->msg_iov;
746 auio.uio_iovcnt = mp->msg_iovlen;
747 auio.uio_segflg = UIO_USERSPACE;
748 auio.uio_rw = UIO_READ;
750 auio.uio_offset = 0; /* XXX */
753 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
754 if ((auio.uio_resid += iov->iov_len) < 0) {
760 if (KTRPOINT(td, KTR_GENIO)) {
761 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
763 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
764 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
768 len = auio.uio_resid;
769 so = (struct socket *)fp->f_data;
770 error = so->so_proto->pr_usrreqs->pru_soreceive(so, &fromsa, &auio,
771 (struct mbuf **)0, mp->msg_control ? &control : (struct mbuf **)0,
774 if (auio.uio_resid != len && (error == ERESTART ||
775 error == EINTR || error == EWOULDBLOCK))
779 if (ktriov != NULL) {
781 ktruio.uio_iov = ktriov;
782 ktruio.uio_resid = len - auio.uio_resid;
783 ktrgenio(p->p_tracep, s, UIO_READ, &ktruio, error);
785 FREE(ktriov, M_TEMP);
790 p->p_retval[0] = len - auio.uio_resid;
792 len = mp->msg_namelen;
793 if (len <= 0 || fromsa == 0)
797 #define MIN(a,b) ((a)>(b)?(b):(a))
799 /* save sa_len before it is destroyed by MSG_COMPAT */
800 len = MIN(len, fromsa->sa_len);
801 #ifdef COMPAT_OLDSOCK
802 if (mp->msg_flags & MSG_COMPAT)
803 ((struct osockaddr *)fromsa)->sa_family =
806 error = copyout(fromsa,
807 (caddr_t)mp->msg_name, (unsigned)len);
811 mp->msg_namelen = len;
813 (error = copyout((caddr_t)&len, namelenp, sizeof (int)))) {
814 #ifdef COMPAT_OLDSOCK
815 if (mp->msg_flags & MSG_COMPAT)
816 error = 0; /* old recvfrom didn't check */
822 if (mp->msg_control) {
823 #ifdef COMPAT_OLDSOCK
825 * We assume that old recvmsg calls won't receive access
826 * rights and other control info, esp. as control info
827 * is always optional and those options didn't exist in 4.3.
828 * If we receive rights, trim the cmsghdr; anything else
831 if (control && mp->msg_flags & MSG_COMPAT) {
832 if (mtod(control, struct cmsghdr *)->cmsg_level !=
834 mtod(control, struct cmsghdr *)->cmsg_type !=
836 mp->msg_controllen = 0;
839 control->m_len -= sizeof (struct cmsghdr);
840 control->m_data += sizeof (struct cmsghdr);
843 len = mp->msg_controllen;
845 mp->msg_controllen = 0;
846 ctlbuf = (caddr_t) mp->msg_control;
848 while (m && len > 0) {
854 mp->msg_flags |= MSG_CTRUNC;
858 if ((error = copyout((caddr_t)mtod(m, caddr_t),
859 ctlbuf, tocopy)) != 0)
866 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
871 FREE(fromsa, M_SONAME);
878 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
879 * caddr_t from, int *fromlenaddr)
882 recvfrom(struct recvfrom_args *uap)
888 if (uap->fromlenaddr) {
889 error = copyin((caddr_t)uap->fromlenaddr,
890 (caddr_t)&msg.msg_namelen, sizeof (msg.msg_namelen));
895 msg.msg_name = uap->from;
898 aiov.iov_base = uap->buf;
899 aiov.iov_len = uap->len;
901 msg.msg_flags = uap->flags;
902 return (recvit(uap->s, &msg, (caddr_t)uap->fromlenaddr));
905 #ifdef COMPAT_OLDSOCK
907 orecvfrom(struct recvfrom_args *uap)
909 uap->flags |= MSG_COMPAT;
910 return (recvfrom(uap));
914 #ifdef COMPAT_OLDSOCK
916 * struct orecv_args(int s, caddr_t buf, int len, int flags)
919 orecv(struct orecv_args *uap)
928 aiov.iov_base = uap->buf;
929 aiov.iov_len = uap->len;
931 msg.msg_flags = uap->flags;
932 return (recvit(uap->s, &msg, (caddr_t)0));
936 * Old recvmsg. This code takes advantage of the fact that the old msghdr
937 * overlays the new one, missing only the flags, and with the (old) access
938 * rights where the control fields are now.
940 * orecvmsg_args(int s, struct omsghdr *msg, int flags)
943 orecvmsg(struct orecvmsg_args *uap)
946 struct iovec aiov[UIO_SMALLIOV], *iov;
949 error = copyin((caddr_t)uap->msg, (caddr_t)&msg,
950 sizeof (struct omsghdr));
953 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
954 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
956 MALLOC(iov, struct iovec *,
957 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
961 msg.msg_flags = uap->flags | MSG_COMPAT;
962 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
963 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
967 error = recvit(uap->s, &msg, (caddr_t)&uap->msg->msg_namelen);
969 if (msg.msg_controllen && error == 0)
970 error = copyout((caddr_t)&msg.msg_controllen,
971 (caddr_t)&uap->msg->msg_accrightslen, sizeof (int));
980 * recvmsg_args(int s, struct msghdr *msg, int flags)
983 recvmsg(struct recvmsg_args *uap)
986 struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
989 error = copyin((caddr_t)uap->msg, (caddr_t)&msg, sizeof (msg));
992 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
993 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
995 MALLOC(iov, struct iovec *,
996 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
1000 #ifdef COMPAT_OLDSOCK
1001 msg.msg_flags = uap->flags &~ MSG_COMPAT;
1003 msg.msg_flags = uap->flags;
1007 error = copyin((caddr_t)uiov, (caddr_t)iov,
1008 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
1011 error = recvit(uap->s, &msg, (caddr_t)0);
1014 error = copyout((caddr_t)&msg, (caddr_t)uap->msg, sizeof(msg));
1023 * shutdown_args(int s, int how)
1027 shutdown(struct shutdown_args *uap)
1029 struct thread *td = curthread;
1030 struct proc *p = td->td_proc;
1035 error = holdsock(p->p_fd, uap->s, &fp);
1038 error = soshutdown((struct socket *)fp->f_data, uap->how);
1044 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
1048 setsockopt(struct setsockopt_args *uap)
1050 struct thread *td = curthread;
1051 struct proc *p = td->td_proc;
1053 struct sockopt sopt;
1056 if (uap->val == 0 && uap->valsize != 0)
1058 if (uap->valsize < 0)
1061 error = holdsock(p->p_fd, uap->s, &fp);
1065 sopt.sopt_dir = SOPT_SET;
1066 sopt.sopt_level = uap->level;
1067 sopt.sopt_name = uap->name;
1068 sopt.sopt_val = uap->val;
1069 sopt.sopt_valsize = uap->valsize;
1071 error = sosetopt((struct socket *)fp->f_data, &sopt);
1077 * getsockopt_Args(int s, int level, int name, caddr_t val, int *avalsize)
1081 getsockopt(struct getsockopt_args *uap)
1083 struct thread *td = curthread;
1084 struct proc *p = td->td_proc;
1087 struct sockopt sopt;
1089 error = holdsock(p->p_fd, uap->s, &fp);
1093 error = copyin((caddr_t)uap->avalsize, (caddr_t)&valsize,
1107 sopt.sopt_dir = SOPT_GET;
1108 sopt.sopt_level = uap->level;
1109 sopt.sopt_name = uap->name;
1110 sopt.sopt_val = uap->val;
1111 sopt.sopt_valsize = (size_t)valsize; /* checked non-negative above */
1114 error = sogetopt((struct socket *)fp->f_data, &sopt);
1116 valsize = sopt.sopt_valsize;
1117 error = copyout((caddr_t)&valsize,
1118 (caddr_t)uap->avalsize, sizeof (valsize));
1125 * getsockname_args(int fdes, caddr_t asa, int *alen)
1131 getsockname1(struct getsockname_args *uap, int compat)
1133 struct thread *td = curthread;
1134 struct proc *p = td->td_proc;
1137 struct sockaddr *sa;
1140 error = holdsock(p->p_fd, uap->fdes, &fp);
1143 error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len));
1152 so = (struct socket *)fp->f_data;
1154 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
1162 len = MIN(len, sa->sa_len);
1163 #ifdef COMPAT_OLDSOCK
1165 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1167 error = copyout(sa, (caddr_t)uap->asa, (u_int)len);
1170 error = copyout((caddr_t)&len, (caddr_t)uap->alen,
1180 getsockname(struct getsockname_args *uap)
1183 return (getsockname1(uap, 0));
1186 #ifdef COMPAT_OLDSOCK
1188 ogetsockname(struct getsockname_args *uap)
1191 return (getsockname1(uap, 1));
1193 #endif /* COMPAT_OLDSOCK */
1196 * getpeername_args(int fdes, caddr_t asa, int *alen)
1198 * Get name of peer for connected socket.
1202 getpeername1(struct getpeername_args *uap, int compat)
1204 struct thread *td = curthread;
1205 struct proc *p = td->td_proc;
1208 struct sockaddr *sa;
1211 error = holdsock(p->p_fd, uap->fdes, &fp);
1214 so = (struct socket *)fp->f_data;
1215 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1219 error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len));
1229 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa);
1236 len = MIN(len, sa->sa_len);
1237 #ifdef COMPAT_OLDSOCK
1239 ((struct osockaddr *)sa)->sa_family =
1242 error = copyout(sa, (caddr_t)uap->asa, (u_int)len);
1246 error = copyout((caddr_t)&len, (caddr_t)uap->alen, sizeof (len));
1255 getpeername(struct getpeername_args *uap)
1257 return (getpeername1(uap, 0));
1260 #ifdef COMPAT_OLDSOCK
1262 ogetpeername(struct ogetpeername_args *uap)
1264 /* XXX uap should have type `getpeername_args *' to begin with. */
1265 return (getpeername1((struct getpeername_args *)uap, 1));
1267 #endif /* COMPAT_OLDSOCK */
1270 sockargs(mp, buf, buflen, type)
1275 register struct sockaddr *sa;
1276 register struct mbuf *m;
1279 if ((u_int)buflen > MLEN) {
1280 #ifdef COMPAT_OLDSOCK
1281 if (type == MT_SONAME && (u_int)buflen <= 112)
1282 buflen = MLEN; /* unix domain compat. hack */
1287 m = m_get(M_WAIT, type);
1291 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1296 if (type == MT_SONAME) {
1297 sa = mtod(m, struct sockaddr *);
1299 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1300 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1301 sa->sa_family = sa->sa_len;
1303 sa->sa_len = buflen;
1310 getsockaddr(namp, uaddr, len)
1311 struct sockaddr **namp;
1315 struct sockaddr *sa;
1318 if (len > SOCK_MAXADDRLEN)
1319 return ENAMETOOLONG;
1320 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1321 error = copyin(uaddr, sa, len);
1325 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1326 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1327 sa->sa_family = sa->sa_len;
1336 * holdsock() - load the struct file pointer associated
1337 * with a socket into *fpp. If an error occurs, non-zero
1338 * will be returned and *fpp will be set to NULL.
1341 holdsock(fdp, fdes, fpp)
1342 struct filedesc *fdp;
1346 register struct file *fp = NULL;
1349 if ((unsigned)fdes >= fdp->fd_nfiles ||
1350 (fp = fdp->fd_ofiles[fdes]) == NULL) {
1352 } else if (fp->f_type != DTYPE_SOCKET) {
1363 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1366 sf_buf_init(void *arg)
1370 SLIST_INIT(&sf_freelist);
1371 sf_base = kmem_alloc_pageable(kernel_map, nsfbufs * PAGE_SIZE);
1372 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, M_NOWAIT);
1373 bzero(sf_bufs, nsfbufs * sizeof(struct sf_buf));
1374 for (i = 0; i < nsfbufs; i++) {
1375 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
1376 SLIST_INSERT_HEAD(&sf_freelist, &sf_bufs[i], free_list);
1381 * Get an sf_buf from the freelist. Will block if none are available.
1391 while ((sf = SLIST_FIRST(&sf_freelist)) == NULL) {
1392 sf_buf_alloc_want = 1;
1393 error = tsleep(&sf_freelist, PCATCH, "sfbufa", 0);
1398 SLIST_REMOVE_HEAD(&sf_freelist, free_list);
1405 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1407 sf_buf_ref(caddr_t addr, u_int size)
1412 if (sf->refcnt == 0)
1413 panic("sf_buf_ref: referencing a free sf_buf");
1418 * Lose a reference to an sf_buf. When none left, detach mapped page
1419 * and release resources back to the system.
1421 * Must be called at splimp.
1424 sf_buf_free(caddr_t addr, u_int size)
1431 if (sf->refcnt == 0)
1432 panic("sf_buf_free: freeing free sf_buf");
1434 if (sf->refcnt == 0) {
1435 pmap_qremove((vm_offset_t)addr, 1);
1438 vm_page_unwire(m, 0);
1440 * Check for the object going away on us. This can
1441 * happen since we don't hold a reference to it.
1442 * If so, we're responsible for freeing the page.
1444 if (m->wire_count == 0 && m->object == NULL)
1448 SLIST_INSERT_HEAD(&sf_freelist, sf, free_list);
1449 if (sf_buf_alloc_want) {
1450 sf_buf_alloc_want = 0;
1451 wakeup(&sf_freelist);
1458 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1459 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1461 * Send a file specified by 'fd' and starting at 'offset' to a socket
1462 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1463 * nbytes == 0. Optionally add a header and/or trailer to the socket
1464 * output. If specified, write the total number of bytes sent into *sbytes.
1467 sendfile(struct sendfile_args *uap)
1469 return (do_sendfile(uap, 0));
1474 osendfile(struct osendfile_args *uap)
1476 struct sendfile_args args;
1480 args.offset = uap->offset;
1481 args.nbytes = uap->nbytes;
1482 args.hdtr = uap->hdtr;
1483 args.sbytes = uap->sbytes;
1484 args.flags = uap->flags;
1486 return (do_sendfile(&args, 1));
1491 do_sendfile(struct sendfile_args *uap, int compat)
1493 struct thread *td = curthread;
1494 struct proc *p = td->td_proc;
1496 struct filedesc *fdp;
1498 struct vm_object *obj;
1503 struct writev_args nuap;
1504 struct sf_hdtr hdtr;
1505 off_t off, xfsize, hdtr_size, sbytes = 0;
1514 * Do argument checking. Must be a regular file in, stream
1515 * type and connected socket out, positive offset.
1517 fp = holdfp(fdp, uap->fd, FREAD);
1522 if (fp->f_type != DTYPE_VNODE) {
1526 vp = (struct vnode *)fp->f_data;
1528 if (vp->v_type != VREG || VOP_GETVOBJECT(vp, &obj) != 0) {
1533 error = holdsock(p->p_fd, uap->s, &fp);
1536 so = (struct socket *)fp->f_data;
1537 if (so->so_type != SOCK_STREAM) {
1541 if ((so->so_state & SS_ISCONNECTED) == 0) {
1545 if (uap->offset < 0) {
1551 * If specified, get the pointer to the sf_hdtr struct for
1552 * any headers/trailers.
1554 if (uap->hdtr != NULL) {
1555 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1559 * Send any headers. Wimp out and use writev(2).
1561 if (hdtr.headers != NULL) {
1563 nuap.iovp = hdtr.headers;
1564 nuap.iovcnt = hdtr.hdr_cnt;
1565 error = writev(&nuap);
1569 sbytes += p->p_retval[0];
1571 hdtr_size += p->p_retval[0];
1576 * Protect against multiple writers to the socket.
1578 (void) sblock(&so->so_snd, M_WAITOK);
1581 * Loop through the pages in the file, starting with the requested
1582 * offset. Get a file page (do I/O if necessary), map the file page
1583 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1586 for (off = uap->offset; ; off += xfsize, sbytes += xfsize) {
1590 pindex = OFF_TO_IDX(off);
1593 * Calculate the amount to transfer. Not to exceed a page,
1594 * the EOF, or the passed in nbytes.
1596 xfsize = obj->un_pager.vnp.vnp_size - off;
1597 if (xfsize > PAGE_SIZE)
1599 pgoff = (vm_offset_t)(off & PAGE_MASK);
1600 if (PAGE_SIZE - pgoff < xfsize)
1601 xfsize = PAGE_SIZE - pgoff;
1602 if (uap->nbytes && xfsize > (uap->nbytes - sbytes))
1603 xfsize = uap->nbytes - sbytes;
1607 * Optimize the non-blocking case by looking at the socket space
1608 * before going to the extra work of constituting the sf_buf.
1610 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1611 if (so->so_state & SS_CANTSENDMORE)
1615 sbunlock(&so->so_snd);
1619 * Attempt to look up the page.
1621 * Allocate if not found
1623 * Wait and loop if busy.
1625 pg = vm_page_lookup(obj, pindex);
1628 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL);
1634 } else if (vm_page_sleep_busy(pg, TRUE, "sfpbsy")) {
1639 * Wire the page so it does not get ripped out from under
1646 * If page is not valid for what we need, initiate I/O
1649 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1655 * Ensure that our page is still around when the I/O
1658 vm_page_io_start(pg);
1661 * Get the page from backing store.
1663 bsize = vp->v_mount->mnt_stat.f_iosize;
1664 auio.uio_iov = &aiov;
1665 auio.uio_iovcnt = 1;
1667 aiov.iov_len = MAXBSIZE;
1668 auio.uio_resid = MAXBSIZE;
1669 auio.uio_offset = trunc_page(off);
1670 auio.uio_segflg = UIO_NOCOPY;
1671 auio.uio_rw = UIO_READ;
1673 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
1674 error = VOP_READ(vp, &auio,
1675 IO_VMIO | ((MAXBSIZE / bsize) << 16),
1677 VOP_UNLOCK(vp, 0, td);
1678 vm_page_flag_clear(pg, PG_ZERO);
1679 vm_page_io_finish(pg);
1681 vm_page_unwire(pg, 0);
1683 * See if anyone else might know about this page.
1684 * If not and it is not valid, then free it.
1686 if (pg->wire_count == 0 && pg->valid == 0 &&
1687 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1688 pg->hold_count == 0) {
1692 sbunlock(&so->so_snd);
1699 * Get a sendfile buf. We usually wait as long as necessary,
1700 * but this wait can be interrupted.
1702 if ((sf = sf_buf_alloc()) == NULL) {
1704 vm_page_unwire(pg, 0);
1705 if (pg->wire_count == 0 && pg->object == NULL)
1708 sbunlock(&so->so_snd);
1715 * Allocate a kernel virtual page and insert the physical page
1720 pmap_qenter(sf->kva, &pg, 1);
1722 * Get an mbuf header and set it up as having external storage.
1724 MGETHDR(m, M_WAIT, MT_DATA);
1727 sf_buf_free((void *)sf->kva, PAGE_SIZE);
1728 sbunlock(&so->so_snd);
1731 m->m_ext.ext_free = sf_buf_free;
1732 m->m_ext.ext_ref = sf_buf_ref;
1733 m->m_ext.ext_buf = (void *)sf->kva;
1734 m->m_ext.ext_size = PAGE_SIZE;
1735 m->m_data = (char *) sf->kva + pgoff;
1736 m->m_flags |= M_EXT;
1737 m->m_pkthdr.len = m->m_len = xfsize;
1739 * Add the buffer to the socket buffer chain.
1744 * Make sure that the socket is still able to take more data.
1745 * CANTSENDMORE being true usually means that the connection
1746 * was closed. so_error is true when an error was sensed after
1748 * The state is checked after the page mapping and buffer
1749 * allocation above since those operations may block and make
1750 * any socket checks stale. From this point forward, nothing
1751 * blocks before the pru_send (or more accurately, any blocking
1752 * results in a loop back to here to re-check).
1754 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1755 if (so->so_state & SS_CANTSENDMORE) {
1758 error = so->so_error;
1762 sbunlock(&so->so_snd);
1767 * Wait for socket space to become available. We do this just
1768 * after checking the connection state above in order to avoid
1769 * a race condition with sbwait().
1771 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
1772 if (so->so_state & SS_NBIO) {
1774 sbunlock(&so->so_snd);
1779 error = sbwait(&so->so_snd);
1781 * An error from sbwait usually indicates that we've
1782 * been interrupted by a signal. If we've sent anything
1783 * then return bytes sent, otherwise return the error.
1787 sbunlock(&so->so_snd);
1794 (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, td);
1797 sbunlock(&so->so_snd);
1801 sbunlock(&so->so_snd);
1804 * Send trailers. Wimp out and use writev(2).
1806 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1808 nuap.iovp = hdtr.trailers;
1809 nuap.iovcnt = hdtr.trl_cnt;
1810 error = writev(&nuap);
1814 sbytes += p->p_retval[0];
1816 hdtr_size += p->p_retval[0];
1820 if (uap->sbytes != NULL) {
1822 sbytes += hdtr_size;
1823 copyout(&sbytes, uap->sbytes, sizeof(off_t));