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.3 2003/06/23 17:55:41 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>
74 static void sf_buf_init(void *arg);
75 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
77 static int sendit __P((int s, struct msghdr *mp, int flags));
78 static int recvit __P((int s, struct msghdr *mp,
81 static int accept1 __P((struct accept_args *uap, int compat));
82 static int do_sendfile __P((struct sendfile_args *uap, int compat));
83 static int getsockname1 __P((struct getsockname_args *uap, int compat));
84 static int getpeername1 __P((struct getpeername_args *uap, int compat));
86 static SLIST_HEAD(, sf_buf) sf_freelist;
87 static vm_offset_t sf_base;
88 static struct sf_buf *sf_bufs;
89 static int sf_buf_alloc_want;
92 * System call interface to the socket abstraction.
94 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
95 #define COMPAT_OLDSOCK
98 extern struct fileops socketops;
101 * socket_args(int domain, int type, int protocol)
104 socket(struct socket_args *uap)
106 struct proc *p = curproc;
107 struct filedesc *fdp = p->p_fd;
112 error = falloc(p, &fp, &fd);
116 error = socreate(uap->domain, &so, uap->type, uap->protocol, p);
118 if (fdp->fd_ofiles[fd] == fp) {
119 fdp->fd_ofiles[fd] = NULL;
123 fp->f_data = (caddr_t)so;
124 fp->f_flag = FREAD|FWRITE;
125 fp->f_ops = &socketops;
126 fp->f_type = DTYPE_SOCKET;
134 * bind_args(int s, caddr_t name, int namelen)
139 bind(struct bind_args *uap)
141 struct proc *p = curproc;
146 error = holdsock(p->p_fd, uap->s, &fp);
149 error = getsockaddr(&sa, uap->name, uap->namelen);
154 error = sobind((struct socket *)fp->f_data, sa, p);
161 * listen_args(int s, int backlog)
165 listen(struct listen_args *uap)
167 struct proc *p = curproc;
171 error = holdsock(p->p_fd, uap->s, &fp);
174 error = solisten((struct socket *)fp->f_data, uap->backlog, p);
180 * accept_args(int s, caddr_t name, int *anamelen)
183 accept1(struct accept_args *uap, int compat)
185 struct proc *p = curproc;
186 struct filedesc *fdp = p->p_fd;
187 struct file *lfp = NULL;
188 struct file *nfp = NULL;
190 int namelen, error, s;
191 struct socket *head, *so;
193 u_int fflag; /* type must match fp->f_flag */
197 error = copyin((caddr_t)uap->anamelen, (caddr_t)&namelen,
204 error = holdsock(fdp, uap->s, &lfp);
208 head = (struct socket *)lfp->f_data;
209 if ((head->so_options & SO_ACCEPTCONN) == 0) {
214 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
215 if (head->so_state & SS_CANTRCVMORE) {
216 head->so_error = ECONNABORTED;
219 if ((head->so_state & SS_NBIO) != 0) {
220 head->so_error = EWOULDBLOCK;
223 error = tsleep((caddr_t)&head->so_timeo, PSOCK | PCATCH,
230 if (head->so_error) {
231 error = head->so_error;
238 * At this point we know that there is at least one connection
239 * ready to be accepted. Remove it from the queue prior to
240 * allocating the file descriptor for it since falloc() may
241 * block allowing another process to accept the connection
244 so = TAILQ_FIRST(&head->so_comp);
245 TAILQ_REMOVE(&head->so_comp, so, so_list);
249 error = falloc(p, &nfp, &fd);
252 * Probably ran out of file descriptors. Put the
253 * unaccepted connection back onto the queue and
254 * do another wakeup so some other process might
255 * have a chance at it.
257 TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
259 wakeup_one(&head->so_timeo);
266 /* connection has been removed from the listen queue */
267 KNOTE(&head->so_rcv.sb_sel.si_note, 0);
269 so->so_state &= ~SS_COMP;
271 if (head->so_sigio != NULL)
272 fsetown(fgetown(head->so_sigio), &so->so_sigio);
274 nfp->f_data = (caddr_t)so;
276 nfp->f_ops = &socketops;
277 nfp->f_type = DTYPE_SOCKET;
278 /* Sync socket nonblocking/async state with file flags */
279 tmp = fflag & FNONBLOCK;
280 (void) fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, p);
281 tmp = fflag & FASYNC;
282 (void) fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, p);
284 error = soaccept(so, &sa);
287 * return a namelen of zero for older code which might
288 * ignore the return value from accept.
290 if (uap->name != NULL) {
292 (void) copyout((caddr_t)&namelen,
293 (caddr_t)uap->anamelen, sizeof(*uap->anamelen));
306 /* check sa_len before it is destroyed */
307 if (namelen > sa->sa_len)
308 namelen = sa->sa_len;
309 #ifdef COMPAT_OLDSOCK
311 ((struct osockaddr *)sa)->sa_family =
314 error = copyout(sa, (caddr_t)uap->name, (u_int)namelen);
317 error = copyout((caddr_t)&namelen,
318 (caddr_t)uap->anamelen, sizeof (*uap->anamelen));
325 * close the new descriptor, assuming someone hasn't ripped it
329 if (fdp->fd_ofiles[fd] == nfp) {
330 fdp->fd_ofiles[fd] = NULL;
337 * Release explicitly held references before returning.
347 accept(struct accept_args *uap)
349 return (accept1(uap, 0));
352 #ifdef COMPAT_OLDSOCK
354 oaccept(struct accept_args *uap)
357 return (accept1(uap, 1));
359 #endif /* COMPAT_OLDSOCK */
362 * connect_args(int s, caddr_t name, int namelen)
366 connect(struct connect_args *uap)
368 struct proc *p = curproc;
370 register struct socket *so;
374 error = holdsock(p->p_fd, uap->s, &fp);
377 so = (struct socket *)fp->f_data;
378 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
382 error = getsockaddr(&sa, uap->name, uap->namelen);
385 error = soconnect(so, sa, p);
388 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
394 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
395 error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
401 error = so->so_error;
406 so->so_state &= ~SS_ISCONNECTING;
408 if (error == ERESTART)
416 * socketpair(int domain, int type, int protocol, int *rsv)
419 socketpair(struct socketpair_args *uap)
421 struct proc *p = curproc;
422 struct filedesc *fdp = p->p_fd;
423 struct file *fp1, *fp2;
424 struct socket *so1, *so2;
425 int fd, error, sv[2];
427 error = socreate(uap->domain, &so1, uap->type, uap->protocol, p);
430 error = socreate(uap->domain, &so2, uap->type, uap->protocol, p);
433 error = falloc(p, &fp1, &fd);
438 fp1->f_data = (caddr_t)so1;
439 error = falloc(p, &fp2, &fd);
443 fp2->f_data = (caddr_t)so2;
445 error = soconnect2(so1, so2);
448 if (uap->type == SOCK_DGRAM) {
450 * Datagram socket connection is asymmetric.
452 error = soconnect2(so2, so1);
456 fp1->f_flag = fp2->f_flag = FREAD|FWRITE;
457 fp1->f_ops = fp2->f_ops = &socketops;
458 fp1->f_type = fp2->f_type = DTYPE_SOCKET;
459 error = copyout((caddr_t)sv, (caddr_t)uap->rsv, 2 * sizeof (int));
464 if (fdp->fd_ofiles[sv[1]] == fp2) {
465 fdp->fd_ofiles[sv[1]] = NULL;
470 if (fdp->fd_ofiles[sv[0]] == fp1) {
471 fdp->fd_ofiles[sv[0]] = NULL;
488 struct proc *p = curproc;
491 register struct iovec *iov;
493 struct mbuf *control;
498 struct iovec *ktriov = NULL;
502 error = holdsock(p->p_fd, s, &fp);
505 auio.uio_iov = mp->msg_iov;
506 auio.uio_iovcnt = mp->msg_iovlen;
507 auio.uio_segflg = UIO_USERSPACE;
508 auio.uio_rw = UIO_WRITE;
510 auio.uio_offset = 0; /* XXX */
513 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
514 if ((auio.uio_resid += iov->iov_len) < 0) {
520 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
528 if (mp->msg_control) {
529 if (mp->msg_controllen < sizeof(struct cmsghdr)
530 #ifdef COMPAT_OLDSOCK
531 && mp->msg_flags != MSG_COMPAT
537 error = sockargs(&control, mp->msg_control,
538 mp->msg_controllen, MT_CONTROL);
541 #ifdef COMPAT_OLDSOCK
542 if (mp->msg_flags == MSG_COMPAT) {
543 register struct cmsghdr *cm;
545 M_PREPEND(control, sizeof(*cm), M_WAIT);
550 cm = mtod(control, struct cmsghdr *);
551 cm->cmsg_len = control->m_len;
552 cm->cmsg_level = SOL_SOCKET;
553 cm->cmsg_type = SCM_RIGHTS;
561 if (KTRPOINT(p, KTR_GENIO)) {
562 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
564 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
565 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
569 len = auio.uio_resid;
570 so = (struct socket *)fp->f_data;
571 error = so->so_proto->pr_usrreqs->pru_sosend(so, to, &auio, 0, control,
574 if (auio.uio_resid != len && (error == ERESTART ||
575 error == EINTR || error == EWOULDBLOCK))
581 p->p_retval[0] = len - auio.uio_resid;
583 if (ktriov != NULL) {
585 ktruio.uio_iov = ktriov;
586 ktruio.uio_resid = p->p_retval[0];
587 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error);
589 FREE(ktriov, M_TEMP);
600 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
603 sendto(struct sendto_args *uap)
608 msg.msg_name = uap->to;
609 msg.msg_namelen = uap->tolen;
613 #ifdef COMPAT_OLDSOCK
616 aiov.iov_base = uap->buf;
617 aiov.iov_len = uap->len;
618 return (sendit(uap->s, &msg, uap->flags));
621 #ifdef COMPAT_OLDSOCK
623 * osend_args(int s, caddr_t buf, int len, int flags)
626 osend(struct osend_args *uap)
635 aiov.iov_base = uap->buf;
636 aiov.iov_len = uap->len;
639 return (sendit(uap->s, &msg, uap->flags));
643 * osendmsg_args(int s, caddr_t msg, int flags)
646 osendmsg(struct osendmsg_args *uap)
649 struct iovec aiov[UIO_SMALLIOV], *iov;
652 error = copyin(uap->msg, (caddr_t)&msg, sizeof (struct omsghdr));
655 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
656 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
658 MALLOC(iov, struct iovec *,
659 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
663 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
664 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
667 msg.msg_flags = MSG_COMPAT;
669 error = sendit(uap->s, &msg, uap->flags);
678 * sendmsg_args(int s, caddr_t msg, int flags)
681 sendmsg(struct sendmsg_args *uap)
684 struct iovec aiov[UIO_SMALLIOV], *iov;
687 error = copyin(uap->msg, (caddr_t)&msg, sizeof (msg));
690 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
691 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
693 MALLOC(iov, struct iovec *,
694 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
698 if (msg.msg_iovlen &&
699 (error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
700 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)))))
703 #ifdef COMPAT_OLDSOCK
706 error = sendit(uap->s, &msg, uap->flags);
714 recvit(s, mp, namelenp)
719 struct proc *p = curproc;
722 register struct iovec *iov;
725 struct mbuf *m, *control = 0;
728 struct sockaddr *fromsa = 0;
730 struct iovec *ktriov = NULL;
734 error = holdsock(p->p_fd, s, &fp);
737 auio.uio_iov = mp->msg_iov;
738 auio.uio_iovcnt = mp->msg_iovlen;
739 auio.uio_segflg = UIO_USERSPACE;
740 auio.uio_rw = UIO_READ;
742 auio.uio_offset = 0; /* XXX */
745 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
746 if ((auio.uio_resid += iov->iov_len) < 0) {
752 if (KTRPOINT(p, KTR_GENIO)) {
753 int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
755 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
756 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
760 len = auio.uio_resid;
761 so = (struct socket *)fp->f_data;
762 error = so->so_proto->pr_usrreqs->pru_soreceive(so, &fromsa, &auio,
763 (struct mbuf **)0, mp->msg_control ? &control : (struct mbuf **)0,
766 if (auio.uio_resid != len && (error == ERESTART ||
767 error == EINTR || error == EWOULDBLOCK))
771 if (ktriov != NULL) {
773 ktruio.uio_iov = ktriov;
774 ktruio.uio_resid = len - auio.uio_resid;
775 ktrgenio(p->p_tracep, s, UIO_READ, &ktruio, error);
777 FREE(ktriov, M_TEMP);
782 p->p_retval[0] = len - auio.uio_resid;
784 len = mp->msg_namelen;
785 if (len <= 0 || fromsa == 0)
789 #define MIN(a,b) ((a)>(b)?(b):(a))
791 /* save sa_len before it is destroyed by MSG_COMPAT */
792 len = MIN(len, fromsa->sa_len);
793 #ifdef COMPAT_OLDSOCK
794 if (mp->msg_flags & MSG_COMPAT)
795 ((struct osockaddr *)fromsa)->sa_family =
798 error = copyout(fromsa,
799 (caddr_t)mp->msg_name, (unsigned)len);
803 mp->msg_namelen = len;
805 (error = copyout((caddr_t)&len, namelenp, sizeof (int)))) {
806 #ifdef COMPAT_OLDSOCK
807 if (mp->msg_flags & MSG_COMPAT)
808 error = 0; /* old recvfrom didn't check */
814 if (mp->msg_control) {
815 #ifdef COMPAT_OLDSOCK
817 * We assume that old recvmsg calls won't receive access
818 * rights and other control info, esp. as control info
819 * is always optional and those options didn't exist in 4.3.
820 * If we receive rights, trim the cmsghdr; anything else
823 if (control && mp->msg_flags & MSG_COMPAT) {
824 if (mtod(control, struct cmsghdr *)->cmsg_level !=
826 mtod(control, struct cmsghdr *)->cmsg_type !=
828 mp->msg_controllen = 0;
831 control->m_len -= sizeof (struct cmsghdr);
832 control->m_data += sizeof (struct cmsghdr);
835 len = mp->msg_controllen;
837 mp->msg_controllen = 0;
838 ctlbuf = (caddr_t) mp->msg_control;
840 while (m && len > 0) {
846 mp->msg_flags |= MSG_CTRUNC;
850 if ((error = copyout((caddr_t)mtod(m, caddr_t),
851 ctlbuf, tocopy)) != 0)
858 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
863 FREE(fromsa, M_SONAME);
870 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
871 * caddr_t from, int *fromlenaddr)
874 recvfrom(struct recvfrom_args *uap)
880 if (uap->fromlenaddr) {
881 error = copyin((caddr_t)uap->fromlenaddr,
882 (caddr_t)&msg.msg_namelen, sizeof (msg.msg_namelen));
887 msg.msg_name = uap->from;
890 aiov.iov_base = uap->buf;
891 aiov.iov_len = uap->len;
893 msg.msg_flags = uap->flags;
894 return (recvit(uap->s, &msg, (caddr_t)uap->fromlenaddr));
897 #ifdef COMPAT_OLDSOCK
899 orecvfrom(struct recvfrom_args *uap)
901 uap->flags |= MSG_COMPAT;
902 return (recvfrom(uap));
906 #ifdef COMPAT_OLDSOCK
908 * struct orecv_args(int s, caddr_t buf, int len, int flags)
911 orecv(struct orecv_args *uap)
920 aiov.iov_base = uap->buf;
921 aiov.iov_len = uap->len;
923 msg.msg_flags = uap->flags;
924 return (recvit(uap->s, &msg, (caddr_t)0));
928 * Old recvmsg. This code takes advantage of the fact that the old msghdr
929 * overlays the new one, missing only the flags, and with the (old) access
930 * rights where the control fields are now.
932 * orecvmsg_args(int s, struct omsghdr *msg, int flags)
935 orecvmsg(struct orecvmsg_args *uap)
938 struct iovec aiov[UIO_SMALLIOV], *iov;
941 error = copyin((caddr_t)uap->msg, (caddr_t)&msg,
942 sizeof (struct omsghdr));
945 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
946 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
948 MALLOC(iov, struct iovec *,
949 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
953 msg.msg_flags = uap->flags | MSG_COMPAT;
954 error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
955 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
959 error = recvit(uap->s, &msg, (caddr_t)&uap->msg->msg_namelen);
961 if (msg.msg_controllen && error == 0)
962 error = copyout((caddr_t)&msg.msg_controllen,
963 (caddr_t)&uap->msg->msg_accrightslen, sizeof (int));
972 * recvmsg_args(int s, struct msghdr *msg, int flags)
975 recvmsg(struct recvmsg_args *uap)
978 struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
981 error = copyin((caddr_t)uap->msg, (caddr_t)&msg, sizeof (msg));
984 if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
985 if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
987 MALLOC(iov, struct iovec *,
988 sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
992 #ifdef COMPAT_OLDSOCK
993 msg.msg_flags = uap->flags &~ MSG_COMPAT;
995 msg.msg_flags = uap->flags;
999 error = copyin((caddr_t)uiov, (caddr_t)iov,
1000 (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
1003 error = recvit(uap->s, &msg, (caddr_t)0);
1006 error = copyout((caddr_t)&msg, (caddr_t)uap->msg, sizeof(msg));
1015 * shutdown_args(int s, int how)
1019 shutdown(struct shutdown_args *uap)
1021 struct proc *p = curproc;
1025 error = holdsock(p->p_fd, uap->s, &fp);
1028 error = soshutdown((struct socket *)fp->f_data, uap->how);
1034 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
1038 setsockopt(struct setsockopt_args *uap)
1040 struct proc *p = curproc;
1042 struct sockopt sopt;
1045 if (uap->val == 0 && uap->valsize != 0)
1047 if (uap->valsize < 0)
1050 error = holdsock(p->p_fd, uap->s, &fp);
1054 sopt.sopt_dir = SOPT_SET;
1055 sopt.sopt_level = uap->level;
1056 sopt.sopt_name = uap->name;
1057 sopt.sopt_val = uap->val;
1058 sopt.sopt_valsize = uap->valsize;
1060 error = sosetopt((struct socket *)fp->f_data, &sopt);
1066 * getsockopt_Args(int s, int level, int name, caddr_t val, int *avalsize)
1070 getsockopt(struct getsockopt_args *uap)
1072 struct proc *p = curproc;
1075 struct sockopt sopt;
1077 error = holdsock(p->p_fd, uap->s, &fp);
1081 error = copyin((caddr_t)uap->avalsize, (caddr_t)&valsize,
1095 sopt.sopt_dir = SOPT_GET;
1096 sopt.sopt_level = uap->level;
1097 sopt.sopt_name = uap->name;
1098 sopt.sopt_val = uap->val;
1099 sopt.sopt_valsize = (size_t)valsize; /* checked non-negative above */
1102 error = sogetopt((struct socket *)fp->f_data, &sopt);
1104 valsize = sopt.sopt_valsize;
1105 error = copyout((caddr_t)&valsize,
1106 (caddr_t)uap->avalsize, sizeof (valsize));
1113 * getsockname_args(int fdes, caddr_t asa, int *alen)
1119 getsockname1(struct getsockname_args *uap, int compat)
1121 struct proc *p = curproc;
1123 register struct socket *so;
1124 struct sockaddr *sa;
1127 error = holdsock(p->p_fd, uap->fdes, &fp);
1130 error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len));
1139 so = (struct socket *)fp->f_data;
1141 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, &sa);
1149 len = MIN(len, sa->sa_len);
1150 #ifdef COMPAT_OLDSOCK
1152 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1154 error = copyout(sa, (caddr_t)uap->asa, (u_int)len);
1157 error = copyout((caddr_t)&len, (caddr_t)uap->alen,
1167 getsockname(struct getsockname_args *uap)
1170 return (getsockname1(uap, 0));
1173 #ifdef COMPAT_OLDSOCK
1175 ogetsockname(struct getsockname_args *uap)
1178 return (getsockname1(uap, 1));
1180 #endif /* COMPAT_OLDSOCK */
1183 * getpeername_args(int fdes, caddr_t asa, int *alen)
1185 * Get name of peer for connected socket.
1189 getpeername1(struct getpeername_args *uap, int compat)
1191 struct proc *p = curproc;
1193 register struct socket *so;
1194 struct sockaddr *sa;
1197 error = holdsock(p->p_fd, uap->fdes, &fp);
1200 so = (struct socket *)fp->f_data;
1201 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1205 error = copyin((caddr_t)uap->alen, (caddr_t)&len, sizeof (len));
1215 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, &sa);
1222 len = MIN(len, sa->sa_len);
1223 #ifdef COMPAT_OLDSOCK
1225 ((struct osockaddr *)sa)->sa_family =
1228 error = copyout(sa, (caddr_t)uap->asa, (u_int)len);
1232 error = copyout((caddr_t)&len, (caddr_t)uap->alen, sizeof (len));
1241 getpeername(struct getpeername_args *uap)
1243 return (getpeername1(uap, 0));
1246 #ifdef COMPAT_OLDSOCK
1248 ogetpeername(struct ogetpeername_args *uap)
1250 /* XXX uap should have type `getpeername_args *' to begin with. */
1251 return (getpeername1((struct getpeername_args *)uap, 1));
1253 #endif /* COMPAT_OLDSOCK */
1256 sockargs(mp, buf, buflen, type)
1261 register struct sockaddr *sa;
1262 register struct mbuf *m;
1265 if ((u_int)buflen > MLEN) {
1266 #ifdef COMPAT_OLDSOCK
1267 if (type == MT_SONAME && (u_int)buflen <= 112)
1268 buflen = MLEN; /* unix domain compat. hack */
1273 m = m_get(M_WAIT, type);
1277 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1282 if (type == MT_SONAME) {
1283 sa = mtod(m, struct sockaddr *);
1285 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1286 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1287 sa->sa_family = sa->sa_len;
1289 sa->sa_len = buflen;
1296 getsockaddr(namp, uaddr, len)
1297 struct sockaddr **namp;
1301 struct sockaddr *sa;
1304 if (len > SOCK_MAXADDRLEN)
1305 return ENAMETOOLONG;
1306 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1307 error = copyin(uaddr, sa, len);
1311 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1312 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1313 sa->sa_family = sa->sa_len;
1322 * holdsock() - load the struct file pointer associated
1323 * with a socket into *fpp. If an error occurs, non-zero
1324 * will be returned and *fpp will be set to NULL.
1327 holdsock(fdp, fdes, fpp)
1328 struct filedesc *fdp;
1332 register struct file *fp = NULL;
1335 if ((unsigned)fdes >= fdp->fd_nfiles ||
1336 (fp = fdp->fd_ofiles[fdes]) == NULL) {
1338 } else if (fp->f_type != DTYPE_SOCKET) {
1349 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
1352 sf_buf_init(void *arg)
1356 SLIST_INIT(&sf_freelist);
1357 sf_base = kmem_alloc_pageable(kernel_map, nsfbufs * PAGE_SIZE);
1358 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, M_NOWAIT);
1359 bzero(sf_bufs, nsfbufs * sizeof(struct sf_buf));
1360 for (i = 0; i < nsfbufs; i++) {
1361 sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
1362 SLIST_INSERT_HEAD(&sf_freelist, &sf_bufs[i], free_list);
1367 * Get an sf_buf from the freelist. Will block if none are available.
1377 while ((sf = SLIST_FIRST(&sf_freelist)) == NULL) {
1378 sf_buf_alloc_want = 1;
1379 error = tsleep(&sf_freelist, PVM|PCATCH, "sfbufa", 0);
1384 SLIST_REMOVE_HEAD(&sf_freelist, free_list);
1391 #define dtosf(x) (&sf_bufs[((uintptr_t)(x) - (uintptr_t)sf_base) >> PAGE_SHIFT])
1393 sf_buf_ref(caddr_t addr, u_int size)
1398 if (sf->refcnt == 0)
1399 panic("sf_buf_ref: referencing a free sf_buf");
1404 * Lose a reference to an sf_buf. When none left, detach mapped page
1405 * and release resources back to the system.
1407 * Must be called at splimp.
1410 sf_buf_free(caddr_t addr, u_int size)
1417 if (sf->refcnt == 0)
1418 panic("sf_buf_free: freeing free sf_buf");
1420 if (sf->refcnt == 0) {
1421 pmap_qremove((vm_offset_t)addr, 1);
1424 vm_page_unwire(m, 0);
1426 * Check for the object going away on us. This can
1427 * happen since we don't hold a reference to it.
1428 * If so, we're responsible for freeing the page.
1430 if (m->wire_count == 0 && m->object == NULL)
1434 SLIST_INSERT_HEAD(&sf_freelist, sf, free_list);
1435 if (sf_buf_alloc_want) {
1436 sf_buf_alloc_want = 0;
1437 wakeup(&sf_freelist);
1444 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1445 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1447 * Send a file specified by 'fd' and starting at 'offset' to a socket
1448 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1449 * nbytes == 0. Optionally add a header and/or trailer to the socket
1450 * output. If specified, write the total number of bytes sent into *sbytes.
1453 sendfile(struct sendfile_args *uap)
1455 return (do_sendfile(uap, 0));
1460 osendfile(struct osendfile_args *uap)
1462 struct sendfile_args args;
1466 args.offset = uap->offset;
1467 args.nbytes = uap->nbytes;
1468 args.hdtr = uap->hdtr;
1469 args.sbytes = uap->sbytes;
1470 args.flags = uap->flags;
1472 return (do_sendfile(&args, 1));
1477 do_sendfile(struct sendfile_args *uap, int compat)
1479 struct proc *p = curproc;
1481 struct filedesc *fdp = p->p_fd;
1483 struct vm_object *obj;
1488 struct writev_args nuap;
1489 struct sf_hdtr hdtr;
1490 off_t off, xfsize, hdtr_size, sbytes = 0;
1496 * Do argument checking. Must be a regular file in, stream
1497 * type and connected socket out, positive offset.
1499 fp = holdfp(fdp, uap->fd, FREAD);
1504 if (fp->f_type != DTYPE_VNODE) {
1508 vp = (struct vnode *)fp->f_data;
1510 if (vp->v_type != VREG || VOP_GETVOBJECT(vp, &obj) != 0) {
1515 error = holdsock(p->p_fd, uap->s, &fp);
1518 so = (struct socket *)fp->f_data;
1519 if (so->so_type != SOCK_STREAM) {
1523 if ((so->so_state & SS_ISCONNECTED) == 0) {
1527 if (uap->offset < 0) {
1533 * If specified, get the pointer to the sf_hdtr struct for
1534 * any headers/trailers.
1536 if (uap->hdtr != NULL) {
1537 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1541 * Send any headers. Wimp out and use writev(2).
1543 if (hdtr.headers != NULL) {
1545 nuap.iovp = hdtr.headers;
1546 nuap.iovcnt = hdtr.hdr_cnt;
1547 error = writev(&nuap);
1551 sbytes += p->p_retval[0];
1553 hdtr_size += p->p_retval[0];
1558 * Protect against multiple writers to the socket.
1560 (void) sblock(&so->so_snd, M_WAITOK);
1563 * Loop through the pages in the file, starting with the requested
1564 * offset. Get a file page (do I/O if necessary), map the file page
1565 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1568 for (off = uap->offset; ; off += xfsize, sbytes += xfsize) {
1572 pindex = OFF_TO_IDX(off);
1575 * Calculate the amount to transfer. Not to exceed a page,
1576 * the EOF, or the passed in nbytes.
1578 xfsize = obj->un_pager.vnp.vnp_size - off;
1579 if (xfsize > PAGE_SIZE)
1581 pgoff = (vm_offset_t)(off & PAGE_MASK);
1582 if (PAGE_SIZE - pgoff < xfsize)
1583 xfsize = PAGE_SIZE - pgoff;
1584 if (uap->nbytes && xfsize > (uap->nbytes - sbytes))
1585 xfsize = uap->nbytes - sbytes;
1589 * Optimize the non-blocking case by looking at the socket space
1590 * before going to the extra work of constituting the sf_buf.
1592 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1593 if (so->so_state & SS_CANTSENDMORE)
1597 sbunlock(&so->so_snd);
1601 * Attempt to look up the page.
1603 * Allocate if not found
1605 * Wait and loop if busy.
1607 pg = vm_page_lookup(obj, pindex);
1610 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL);
1616 } else if (vm_page_sleep_busy(pg, TRUE, "sfpbsy")) {
1621 * Wire the page so it does not get ripped out from under
1628 * If page is not valid for what we need, initiate I/O
1631 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1637 * Ensure that our page is still around when the I/O
1640 vm_page_io_start(pg);
1643 * Get the page from backing store.
1645 bsize = vp->v_mount->mnt_stat.f_iosize;
1646 auio.uio_iov = &aiov;
1647 auio.uio_iovcnt = 1;
1649 aiov.iov_len = MAXBSIZE;
1650 auio.uio_resid = MAXBSIZE;
1651 auio.uio_offset = trunc_page(off);
1652 auio.uio_segflg = UIO_NOCOPY;
1653 auio.uio_rw = UIO_READ;
1655 vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p);
1656 error = VOP_READ(vp, &auio, IO_VMIO | ((MAXBSIZE / bsize) << 16),
1658 VOP_UNLOCK(vp, 0, p);
1659 vm_page_flag_clear(pg, PG_ZERO);
1660 vm_page_io_finish(pg);
1662 vm_page_unwire(pg, 0);
1664 * See if anyone else might know about this page.
1665 * If not and it is not valid, then free it.
1667 if (pg->wire_count == 0 && pg->valid == 0 &&
1668 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1669 pg->hold_count == 0) {
1673 sbunlock(&so->so_snd);
1680 * Get a sendfile buf. We usually wait as long as necessary,
1681 * but this wait can be interrupted.
1683 if ((sf = sf_buf_alloc()) == NULL) {
1685 vm_page_unwire(pg, 0);
1686 if (pg->wire_count == 0 && pg->object == NULL)
1689 sbunlock(&so->so_snd);
1696 * Allocate a kernel virtual page and insert the physical page
1701 pmap_qenter(sf->kva, &pg, 1);
1703 * Get an mbuf header and set it up as having external storage.
1705 MGETHDR(m, M_WAIT, MT_DATA);
1708 sf_buf_free((void *)sf->kva, PAGE_SIZE);
1709 sbunlock(&so->so_snd);
1712 m->m_ext.ext_free = sf_buf_free;
1713 m->m_ext.ext_ref = sf_buf_ref;
1714 m->m_ext.ext_buf = (void *)sf->kva;
1715 m->m_ext.ext_size = PAGE_SIZE;
1716 m->m_data = (char *) sf->kva + pgoff;
1717 m->m_flags |= M_EXT;
1718 m->m_pkthdr.len = m->m_len = xfsize;
1720 * Add the buffer to the socket buffer chain.
1725 * Make sure that the socket is still able to take more data.
1726 * CANTSENDMORE being true usually means that the connection
1727 * was closed. so_error is true when an error was sensed after
1729 * The state is checked after the page mapping and buffer
1730 * allocation above since those operations may block and make
1731 * any socket checks stale. From this point forward, nothing
1732 * blocks before the pru_send (or more accurately, any blocking
1733 * results in a loop back to here to re-check).
1735 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1736 if (so->so_state & SS_CANTSENDMORE) {
1739 error = so->so_error;
1743 sbunlock(&so->so_snd);
1748 * Wait for socket space to become available. We do this just
1749 * after checking the connection state above in order to avoid
1750 * a race condition with sbwait().
1752 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
1753 if (so->so_state & SS_NBIO) {
1755 sbunlock(&so->so_snd);
1760 error = sbwait(&so->so_snd);
1762 * An error from sbwait usually indicates that we've
1763 * been interrupted by a signal. If we've sent anything
1764 * then return bytes sent, otherwise return the error.
1768 sbunlock(&so->so_snd);
1774 error = (*so->so_proto->pr_usrreqs->pru_send)(so, 0, m, 0, 0, p);
1777 sbunlock(&so->so_snd);
1781 sbunlock(&so->so_snd);
1784 * Send trailers. Wimp out and use writev(2).
1786 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1788 nuap.iovp = hdtr.trailers;
1789 nuap.iovcnt = hdtr.trl_cnt;
1790 error = writev(&nuap);
1794 sbytes += p->p_retval[0];
1796 hdtr_size += p->p_retval[0];
1800 if (uap->sbytes != NULL) {
1802 sbytes += hdtr_size;
1803 copyout(&sbytes, uap->sbytes, sizeof(off_t));