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.35 2004/05/13 17:40:15 dillon Exp $
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/sysproto.h>
47 #include <sys/malloc.h>
48 #include <sys/filedesc.h>
49 #include <sys/event.h>
51 #include <sys/fcntl.h>
53 #include <sys/filio.h>
54 #include <sys/kern_syscall.h>
56 #include <sys/protosw.h>
57 #include <sys/sfbuf.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/socketops.h>
62 #include <sys/vnode.h>
64 #include <sys/mount.h>
66 #include <sys/ktrace.h>
69 #include <vm/vm_object.h>
70 #include <vm/vm_page.h>
71 #include <vm/vm_pageout.h>
72 #include <vm/vm_kern.h>
73 #include <vm/vm_extern.h>
74 #include <sys/file2.h>
75 #include <sys/signalvar.h>
77 #include <sys/thread2.h>
78 #include <sys/msgport2.h>
81 * System call interface to the socket abstraction.
84 extern struct fileops socketops;
87 * socket_args(int domain, int type, int protocol)
90 kern_socket(int domain, int type, int protocol, int *res)
92 struct thread *td = curthread;
93 struct proc *p = td->td_proc;
102 error = falloc(p, &fp, &fd);
106 error = socreate(domain, &so, type, protocol, td);
108 if (fdp->fd_ofiles[fd] == fp) {
109 fdp->fd_ofiles[fd] = NULL;
113 fp->f_data = (caddr_t)so;
114 fp->f_flag = FREAD|FWRITE;
115 fp->f_ops = &socketops;
116 fp->f_type = DTYPE_SOCKET;
124 socket(struct socket_args *uap)
128 error = kern_socket(uap->domain, uap->type, uap->protocol,
129 &uap->sysmsg_result);
135 kern_bind(int s, struct sockaddr *sa)
137 struct thread *td = curthread;
138 struct proc *p = td->td_proc;
143 error = holdsock(p->p_fd, s, &fp);
146 error = sobind((struct socket *)fp->f_data, sa, td);
152 * bind_args(int s, caddr_t name, int namelen)
155 bind(struct bind_args *uap)
160 error = getsockaddr(&sa, uap->name, uap->namelen);
163 error = kern_bind(uap->s, sa);
170 kern_listen(int s, int backlog)
172 struct thread *td = curthread;
173 struct proc *p = td->td_proc;
178 error = holdsock(p->p_fd, s, &fp);
181 error = solisten((struct socket *)fp->f_data, backlog, td);
187 * listen_args(int s, int backlog)
190 listen(struct listen_args *uap)
194 error = kern_listen(uap->s, uap->backlog);
199 * The second argument to kern_accept() is a handle to a struct sockaddr.
200 * This allows kern_accept() to return a pointer to an allocated struct
201 * sockaddr which must be freed later with FREE(). The caller must
202 * initialize *name to NULL.
205 kern_accept(int s, struct sockaddr **name, int *namelen, int *res)
207 struct thread *td = curthread;
208 struct proc *p = td->td_proc;
209 struct filedesc *fdp = p->p_fd;
210 struct file *lfp = NULL;
211 struct file *nfp = NULL;
214 struct socket *head, *so;
216 u_int fflag; /* type must match fp->f_flag */
219 if (name && namelen && *namelen < 0)
222 error = holdsock(fdp, s, &lfp);
226 head = (struct socket *)lfp->f_data;
227 if ((head->so_options & SO_ACCEPTCONN) == 0) {
232 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
233 if (head->so_state & SS_CANTRCVMORE) {
234 head->so_error = ECONNABORTED;
237 if ((head->so_state & SS_NBIO) != 0) {
238 head->so_error = EWOULDBLOCK;
241 error = tsleep((caddr_t)&head->so_timeo, PCATCH, "accept", 0);
247 if (head->so_error) {
248 error = head->so_error;
255 * At this point we know that there is at least one connection
256 * ready to be accepted. Remove it from the queue prior to
257 * allocating the file descriptor for it since falloc() may
258 * block allowing another process to accept the connection
261 so = TAILQ_FIRST(&head->so_comp);
262 TAILQ_REMOVE(&head->so_comp, so, so_list);
266 error = falloc(p, &nfp, &fd);
269 * Probably ran out of file descriptors. Put the
270 * unaccepted connection back onto the queue and
271 * do another wakeup so some other process might
272 * have a chance at it.
274 TAILQ_INSERT_HEAD(&head->so_comp, so, so_list);
276 wakeup_one(&head->so_timeo);
283 /* connection has been removed from the listen queue */
284 KNOTE(&head->so_rcv.sb_sel.si_note, 0);
286 so->so_state &= ~SS_COMP;
288 if (head->so_sigio != NULL)
289 fsetown(fgetown(head->so_sigio), &so->so_sigio);
291 nfp->f_data = (caddr_t)so;
293 nfp->f_ops = &socketops;
294 nfp->f_type = DTYPE_SOCKET;
295 /* Sync socket nonblocking/async state with file flags */
296 tmp = fflag & FNONBLOCK;
297 (void) fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, td);
298 tmp = fflag & FASYNC;
299 (void) fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, td);
302 error = soaccept(so, &sa);
305 * Set the returned name and namelen as applicable. Set the returned
306 * namelen to 0 for older code which might ignore the return value
310 if (sa && name && namelen) {
311 if (*namelen > sa->sa_len)
312 *namelen = sa->sa_len;
321 * close the new descriptor, assuming someone hasn't ripped it
322 * out from under us. Note that *res is normally ignored if an
323 * error is returned but a syscall message will still have access
324 * to the result code.
328 if (fdp->fd_ofiles[fd] == nfp) {
329 fdp->fd_ofiles[fd] = NULL;
336 * Release explicitly held references before returning.
346 * accept_args(int s, caddr_t name, int *anamelen)
349 accept(struct accept_args *uap)
351 struct sockaddr *sa = NULL;
356 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
360 error = kern_accept(uap->s, &sa, &sa_len, &uap->sysmsg_result);
363 error = copyout(sa, uap->name, sa_len);
365 error = copyout(&sa_len, uap->anamelen,
366 sizeof(*uap->anamelen));
371 error = kern_accept(uap->s, NULL, 0, &uap->sysmsg_result);
377 * Returns TRUE if predicate satisfied.
380 soconnected_predicate(struct netmsg *msg0)
382 struct netmsg_so_notify *msg = (struct netmsg_so_notify *)msg0;
383 struct socket *so = msg->nm_so;
385 /* check predicate */
386 if (!(so->so_state & SS_ISCONNECTING) || so->so_error != 0) {
387 msg->nm_lmsg.ms_error = so->so_error;
395 kern_connect(int s, struct sockaddr *sa)
397 struct thread *td = curthread;
398 struct proc *p = td->td_proc;
403 error = holdsock(p->p_fd, s, &fp);
406 so = (struct socket *)fp->f_data;
407 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
411 error = soconnect(so, sa, td);
414 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
418 if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
419 struct netmsg_so_notify msg;
422 port = so->so_proto->pr_mport(so, sa, PRU_PRED);
423 lwkt_initmsg(&msg.nm_lmsg,
424 &curthread->td_msgport,
425 MSGF_PCATCH | MSGF_ABORTABLE,
426 lwkt_cmd_func(netmsg_so_notify),
427 lwkt_cmd_func(netmsg_so_notify_abort));
428 msg.nm_predicate = soconnected_predicate;
430 msg.nm_etype = NM_REVENT;
431 error = lwkt_domsg(port, &msg.nm_lmsg);
434 error = so->so_error;
438 so->so_state &= ~SS_ISCONNECTING;
439 if (error == ERESTART)
447 * connect_args(int s, caddr_t name, int namelen)
450 connect(struct connect_args *uap)
455 error = getsockaddr(&sa, uap->name, uap->namelen);
458 error = kern_connect(uap->s, sa);
465 kern_socketpair(int domain, int type, int protocol, int *sv)
467 struct thread *td = curthread;
468 struct proc *p = td->td_proc;
469 struct filedesc *fdp;
470 struct file *fp1, *fp2;
471 struct socket *so1, *so2;
476 error = socreate(domain, &so1, type, protocol, td);
479 error = socreate(domain, &so2, type, protocol, td);
482 error = falloc(p, &fp1, &fd);
487 fp1->f_data = (caddr_t)so1;
488 error = falloc(p, &fp2, &fd);
492 fp2->f_data = (caddr_t)so2;
494 error = soconnect2(so1, so2);
497 if (type == SOCK_DGRAM) {
499 * Datagram socket connection is asymmetric.
501 error = soconnect2(so2, so1);
505 fp1->f_flag = fp2->f_flag = FREAD|FWRITE;
506 fp1->f_ops = fp2->f_ops = &socketops;
507 fp1->f_type = fp2->f_type = DTYPE_SOCKET;
512 if (fdp->fd_ofiles[sv[1]] == fp2) {
513 fdp->fd_ofiles[sv[1]] = NULL;
518 if (fdp->fd_ofiles[sv[0]] == fp1) {
519 fdp->fd_ofiles[sv[0]] = NULL;
531 * socketpair(int domain, int type, int protocol, int *rsv)
534 socketpair(struct socketpair_args *uap)
538 error = kern_socketpair(uap->domain, uap->type, uap->protocol, sockv);
541 error = copyout(sockv, uap->rsv, sizeof(sockv));
546 kern_sendmsg(int s, struct sockaddr *sa, struct uio *auio,
547 struct mbuf *control, int flags, int *res)
549 struct thread *td = curthread;
550 struct proc *p = td->td_proc;
555 struct iovec *ktriov = NULL;
559 error = holdsock(p->p_fd, s, &fp);
562 if (auio->uio_resid < 0) {
567 if (KTRPOINT(td, KTR_GENIO)) {
568 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
570 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
571 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
575 len = auio->uio_resid;
576 so = (struct socket *)fp->f_data;
577 error = so_pru_sosend(so, sa, auio, NULL, control, flags, td);
579 if (auio->uio_resid != len && (error == ERESTART ||
580 error == EINTR || error == EWOULDBLOCK))
586 if (ktriov != NULL) {
588 ktruio.uio_iov = ktriov;
589 ktruio.uio_resid = len - auio->uio_resid;
590 ktrgenio(p->p_tracep, s, UIO_WRITE, &ktruio, error);
592 FREE(ktriov, M_TEMP);
596 *res = len - auio->uio_resid;
603 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
606 sendto(struct sendto_args *uap)
608 struct thread *td = curthread;
611 struct sockaddr *sa = NULL;
615 error = getsockaddr(&sa, uap->to, uap->tolen);
619 aiov.iov_base = uap->buf;
620 aiov.iov_len = uap->len;
621 auio.uio_iov = &aiov;
624 auio.uio_resid = uap->len;
625 auio.uio_segflg = UIO_USERSPACE;
626 auio.uio_rw = UIO_WRITE;
629 error = kern_sendmsg(uap->s, sa, &auio, NULL, uap->flags,
630 &uap->sysmsg_result);
638 * sendmsg_args(int s, caddr_t msg, int flags)
641 sendmsg(struct sendmsg_args *uap)
643 struct thread *td = curthread;
646 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
647 struct sockaddr *sa = NULL;
648 struct mbuf *control = NULL;
651 error = copyin(uap->msg, (caddr_t)&msg, sizeof(msg));
656 * Conditionally copyin msg.msg_name.
659 error = getsockaddr(&sa, msg.msg_name, msg.msg_namelen);
667 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
672 auio.uio_iovcnt = msg.msg_iovlen;
674 auio.uio_segflg = UIO_USERSPACE;
675 auio.uio_rw = UIO_WRITE;
679 * Conditionally copyin msg.msg_control.
681 if (msg.msg_control) {
682 if (msg.msg_controllen < sizeof(struct cmsghdr) ||
683 msg.msg_controllen > MLEN) {
687 control = m_get(M_WAIT, MT_CONTROL);
688 if (control == NULL) {
692 control->m_len = msg.msg_controllen;
693 error = copyin(msg.msg_control, mtod(control, caddr_t),
701 error = kern_sendmsg(uap->s, sa, &auio, control, uap->flags,
702 &uap->sysmsg_result);
707 iovec_free(&iov, aiov);
712 * kern_recvmsg() takes a handle to sa and control. If the handle is non-
713 * null, it returns a dynamically allocated struct sockaddr and an mbuf.
714 * Don't forget to FREE() and m_free() these if they are returned.
717 kern_recvmsg(int s, struct sockaddr **sa, struct uio *auio,
718 struct mbuf **control, int *flags, int *res)
720 struct thread *td = curthread;
721 struct proc *p = td->td_proc;
726 struct iovec *ktriov = NULL;
730 error = holdsock(p->p_fd, s, &fp);
733 if (auio->uio_resid < 0) {
738 if (KTRPOINT(td, KTR_GENIO)) {
739 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
741 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
742 bcopy(auio->uio_iov, ktriov, iovlen);
746 len = auio->uio_resid;
747 so = (struct socket *)fp->f_data;
748 error = so_pru_soreceive(so, sa, auio, NULL, control, flags);
750 if (auio->uio_resid != len && (error == ERESTART ||
751 error == EINTR || error == EWOULDBLOCK))
755 if (ktriov != NULL) {
757 ktruio.uio_iov = ktriov;
758 ktruio.uio_resid = len - auio->uio_resid;
759 ktrgenio(p->p_tracep, s, UIO_READ, &ktruio, error);
761 FREE(ktriov, M_TEMP);
765 *res = len - auio->uio_resid;
772 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
773 * caddr_t from, int *fromlenaddr)
776 recvfrom(struct recvfrom_args *uap)
778 struct thread *td = curthread;
781 struct sockaddr *sa = NULL;
784 if (uap->from && uap->fromlenaddr) {
785 error = copyin(uap->fromlenaddr, &fromlen, sizeof(fromlen));
793 aiov.iov_base = uap->buf;
794 aiov.iov_len = uap->len;
795 auio.uio_iov = &aiov;
798 auio.uio_resid = uap->len;
799 auio.uio_segflg = UIO_USERSPACE;
800 auio.uio_rw = UIO_READ;
803 error = kern_recvmsg(uap->s, uap->from ? &sa : NULL, &auio, NULL,
804 &uap->flags, &uap->sysmsg_result);
806 if (error == 0 && uap->from) {
807 /* note: sa may still be NULL */
809 fromlen = MIN(fromlen, sa->sa_len);
810 error = copyout(sa, uap->from, fromlen);
815 error = copyout(&fromlen, uap->fromlenaddr,
826 * recvmsg_args(int s, struct msghdr *msg, int flags)
829 recvmsg(struct recvmsg_args *uap)
831 struct thread *td = curthread;
834 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
835 struct mbuf *m, *control = NULL;
836 struct sockaddr *sa = NULL;
838 socklen_t *ufromlenp, *ucontrollenp;
839 int error, fromlen, controllen, len, flags, *uflagsp;
842 * This copyin handles everything except the iovec.
844 error = copyin(uap->msg, &msg, sizeof(msg));
848 if (msg.msg_name && msg.msg_namelen < 0)
850 if (msg.msg_control && msg.msg_controllen < 0)
853 ufromlenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
855 ucontrollenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
857 uflagsp = (int *)((caddr_t)uap->msg + offsetof(struct msghdr,
863 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
868 auio.uio_iovcnt = msg.msg_iovlen;
870 auio.uio_segflg = UIO_USERSPACE;
871 auio.uio_rw = UIO_READ;
874 flags = msg.msg_flags;
876 error = kern_recvmsg(uap->s, msg.msg_name ? &sa : NULL, &auio,
877 msg.msg_control ? &control : NULL, &flags, &uap->sysmsg_result);
880 * Conditionally copyout the name and populate the namelen field.
882 if (error == 0 && msg.msg_name) {
883 fromlen = MIN(msg.msg_namelen, sa->sa_len);
884 error = copyout(sa, msg.msg_name, fromlen);
886 error = copyout(&fromlen, ufromlenp,
891 * Copyout msg.msg_control and msg.msg_controllen.
893 if (error == 0 && msg.msg_control) {
894 len = msg.msg_controllen;
896 ctlbuf = (caddr_t)msg.msg_control;
898 while(m && len > 0) {
901 if (len >= m->m_len) {
904 msg.msg_flags |= MSG_CTRUNC;
908 error = copyout(mtod(m, caddr_t), ctlbuf, tocopy);
916 controllen = ctlbuf - (caddr_t)msg.msg_control;
917 error = copyout(&controllen, ucontrollenp,
918 sizeof(*ucontrollenp));
922 error = copyout(&flags, uflagsp, sizeof(*uflagsp));
927 iovec_free(&iov, aiov);
934 * shutdown_args(int s, int how)
937 kern_shutdown(int s, int how)
939 struct thread *td = curthread;
940 struct proc *p = td->td_proc;
945 error = holdsock(p->p_fd, s, &fp);
948 error = soshutdown((struct socket *)fp->f_data, how);
954 shutdown(struct shutdown_args *uap)
958 error = kern_shutdown(uap->s, uap->how);
964 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
965 * in kernel pointer instead of a userland pointer. This allows us
966 * to manipulate socket options in the emulation code.
969 kern_setsockopt(int s, struct sockopt *sopt)
971 struct thread *td = curthread;
972 struct proc *p = td->td_proc;
976 if (sopt->sopt_val == 0 && sopt->sopt_valsize != 0)
978 if (sopt->sopt_valsize < 0)
981 error = holdsock(p->p_fd, s, &fp);
985 error = sosetopt((struct socket *)fp->f_data, sopt);
991 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
994 setsockopt(struct setsockopt_args *uap)
996 struct thread *td = curthread;
1000 sopt.sopt_dir = SOPT_SET;
1001 sopt.sopt_level = uap->level;
1002 sopt.sopt_name = uap->name;
1003 sopt.sopt_val = uap->val;
1004 sopt.sopt_valsize = uap->valsize;
1007 error = kern_setsockopt(uap->s, &sopt);
1012 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
1013 * in kernel pointer instead of a userland pointer. This allows us
1014 * to manipulate socket options in the emulation code.
1017 kern_getsockopt(int s, struct sockopt *sopt)
1019 struct thread *td = curthread;
1020 struct proc *p = td->td_proc;
1024 if (sopt->sopt_val == 0 && sopt->sopt_valsize != 0)
1026 if (sopt->sopt_valsize < 0)
1029 error = holdsock(p->p_fd, s, &fp);
1033 error = sogetopt((struct socket *)fp->f_data, sopt);
1039 * getsockopt_Args(int s, int level, int name, caddr_t val, int *avalsize)
1042 getsockopt(struct getsockopt_args *uap)
1044 struct thread *td = curthread;
1045 struct sockopt sopt;
1049 error = copyin(uap->avalsize, &valsize, sizeof(valsize));
1058 sopt.sopt_dir = SOPT_GET;
1059 sopt.sopt_level = uap->level;
1060 sopt.sopt_name = uap->name;
1061 sopt.sopt_val = uap->val;
1062 sopt.sopt_valsize = valsize;
1065 error = kern_getsockopt(uap->s, &sopt);
1067 valsize = sopt.sopt_valsize;
1068 error = copyout(&valsize, uap->avalsize, sizeof(valsize));
1074 * The second argument to kern_getsockname() is a handle to a struct sockaddr.
1075 * This allows kern_getsockname() to return a pointer to an allocated struct
1076 * sockaddr which must be freed later with FREE(). The caller must
1077 * initialize *name to NULL.
1080 kern_getsockname(int s, struct sockaddr **name, int *namelen)
1082 struct thread *td = curthread;
1083 struct proc *p = td->td_proc;
1086 struct sockaddr *sa = NULL;
1089 error = holdsock(p->p_fd, s, &fp);
1096 so = (struct socket *)fp->f_data;
1097 error = so_pru_sockaddr(so, &sa);
1102 *namelen = MIN(*namelen, sa->sa_len);
1112 * getsockname_args(int fdes, caddr_t asa, int *alen)
1117 getsockname(struct getsockname_args *uap)
1119 struct sockaddr *sa = NULL;
1122 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1126 error = kern_getsockname(uap->fdes, &sa, &sa_len);
1129 error = copyout(sa, uap->asa, sa_len);
1131 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1138 * The second argument to kern_getpeername() is a handle to a struct sockaddr.
1139 * This allows kern_getpeername() to return a pointer to an allocated struct
1140 * sockaddr which must be freed later with FREE(). The caller must
1141 * initialize *name to NULL.
1144 kern_getpeername(int s, struct sockaddr **name, int *namelen)
1146 struct thread *td = curthread;
1147 struct proc *p = td->td_proc;
1150 struct sockaddr *sa = NULL;
1153 error = holdsock(p->p_fd, s, &fp);
1160 so = (struct socket *)fp->f_data;
1161 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1165 error = so_pru_peeraddr(so, &sa);
1170 *namelen = MIN(*namelen, sa->sa_len);
1180 * getpeername_args(int fdes, caddr_t asa, int *alen)
1182 * Get name of peer for connected socket.
1185 getpeername(struct getpeername_args *uap)
1187 struct sockaddr *sa = NULL;
1190 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1194 error = kern_getpeername(uap->fdes, &sa, &sa_len);
1197 error = copyout(sa, uap->asa, sa_len);
1199 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1206 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1208 struct sockaddr *sa;
1212 if (len > SOCK_MAXADDRLEN)
1213 return ENAMETOOLONG;
1214 if (len < offsetof(struct sockaddr, sa_data[0]))
1216 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1217 error = copyin(uaddr, sa, len);
1221 #if BYTE_ORDER != BIG_ENDIAN
1223 * The bind(), connect(), and sendto() syscalls were not
1224 * versioned for COMPAT_43. Thus, this check must stay.
1226 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1227 sa->sa_family = sa->sa_len;
1236 * holdsock() - load the struct file pointer associated
1237 * with a socket into *fpp. If an error occurs, non-zero
1238 * will be returned and *fpp will be set to NULL.
1241 holdsock(fdp, fdes, fpp)
1242 struct filedesc *fdp;
1246 struct file *fp = NULL;
1249 if ((unsigned)fdes >= fdp->fd_nfiles ||
1250 (fp = fdp->fd_ofiles[fdes]) == NULL) {
1252 } else if (fp->f_type != DTYPE_SOCKET) {
1263 * Detach a mapped page and release resources back to the system.
1264 * We must release our wiring and if the object is ripped out
1265 * from under the vm_page we become responsible for freeing the
1268 * XXX HACK XXX TEMPORARY UNTIL WE IMPLEMENT EXT MBUF REFERENCE COUNTING
1271 sf_buf_mref(caddr_t addr, u_int size)
1275 sf = sf_buf_tosf(addr);
1280 sf_buf_mext(caddr_t addr, u_int size)
1287 sf = sf_buf_tosf(addr);
1288 KKASSERT(sf->aux2 > 0);
1289 if (--sf->aux2 == 0) {
1290 m = sf_buf_page(sf);
1297 vm_page_unwire(m, 0);
1299 if (m->wire_count == 0 && m->object == NULL)
1307 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1308 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1310 * Send a file specified by 'fd' and starting at 'offset' to a socket
1311 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1312 * nbytes == 0. Optionally add a header and/or trailer to the socket
1313 * output. If specified, write the total number of bytes sent into *sbytes.
1315 * In FreeBSD kern/uipc_syscalls.c,v 1.103, a bug was fixed that caused
1316 * the headers to count against the remaining bytes to be sent from
1317 * the file descriptor. We may wish to implement a compatibility syscall
1321 sendfile(struct sendfile_args *uap)
1323 struct thread *td = curthread;
1324 struct proc *p = td->td_proc;
1326 struct filedesc *fdp;
1327 struct vnode *vp = NULL;
1328 struct sf_hdtr hdtr;
1329 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
1331 struct mbuf *mheader = NULL;
1332 off_t hdtr_size = 0, sbytes;
1333 int error, hbytes = 0, tbytes;
1339 * Do argument checking. Must be a regular file in, stream
1340 * type and connected socket out, positive offset.
1342 fp = holdfp(fdp, uap->fd, FREAD);
1346 if (fp->f_type != DTYPE_VNODE) {
1350 vp = (struct vnode *)fp->f_data;
1355 * If specified, get the pointer to the sf_hdtr struct for
1356 * any headers/trailers.
1359 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1366 error = iovec_copyin(hdtr.headers, &iov, aiov,
1367 hdtr.hdr_cnt, &hbytes);
1371 auio.uio_iovcnt = hdtr.hdr_cnt;
1372 auio.uio_offset = 0;
1373 auio.uio_segflg = UIO_USERSPACE;
1374 auio.uio_rw = UIO_WRITE;
1376 auio.uio_resid = hbytes;
1378 mheader = m_uiomove(&auio, M_WAIT, 0);
1380 iovec_free(&iov, aiov);
1381 if (mheader == NULL)
1386 error = kern_sendfile(vp, uap->s, uap->offset, uap->nbytes, mheader,
1387 &sbytes, uap->flags);
1392 * Send trailers. Wimp out and use writev(2).
1394 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1395 error = iovec_copyin(hdtr.trailers, &iov, aiov,
1396 hdtr.trl_cnt, &auio.uio_resid);
1400 auio.uio_iovcnt = hdtr.trl_cnt;
1401 auio.uio_offset = 0;
1402 auio.uio_segflg = UIO_USERSPACE;
1403 auio.uio_rw = UIO_WRITE;
1406 error = kern_sendmsg(uap->s, NULL, &auio, NULL, 0, &tbytes);
1408 iovec_free(&iov, aiov);
1411 hdtr_size += tbytes; /* trailer bytes successfully sent */
1415 if (uap->sbytes != NULL) {
1416 sbytes += hdtr_size;
1417 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1425 kern_sendfile(struct vnode *vp, int sfd, off_t offset, size_t nbytes,
1426 struct mbuf *mheader, off_t *sbytes, int flags)
1428 struct thread *td = curthread;
1429 struct proc *p = td->td_proc;
1430 struct vm_object *obj;
1441 if (vp->v_type != VREG || VOP_GETVOBJECT(vp, &obj) != 0) {
1445 error = holdsock(p->p_fd, sfd, &fp);
1448 so = (struct socket *)fp->f_data;
1449 if (so->so_type != SOCK_STREAM) {
1453 if ((so->so_state & SS_ISCONNECTED) == 0) {
1464 * Protect against multiple writers to the socket.
1466 (void) sblock(&so->so_snd, M_WAITOK);
1469 * Loop through the pages in the file, starting with the requested
1470 * offset. Get a file page (do I/O if necessary), map the file page
1471 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1474 for (off = offset; ; off += xfsize, *sbytes += xfsize + hbytes) {
1478 pindex = OFF_TO_IDX(off);
1481 * Calculate the amount to transfer. Not to exceed a page,
1482 * the EOF, or the passed in nbytes.
1484 xfsize = obj->un_pager.vnp.vnp_size - off;
1485 if (xfsize > PAGE_SIZE)
1487 pgoff = (vm_offset_t)(off & PAGE_MASK);
1488 if (PAGE_SIZE - pgoff < xfsize)
1489 xfsize = PAGE_SIZE - pgoff;
1490 if (nbytes && xfsize > (nbytes - *sbytes))
1491 xfsize = nbytes - *sbytes;
1495 * Optimize the non-blocking case by looking at the socket space
1496 * before going to the extra work of constituting the sf_buf.
1498 if ((so->so_state & SS_NBIO) && sbspace(&so->so_snd) <= 0) {
1499 if (so->so_state & SS_CANTSENDMORE)
1503 sbunlock(&so->so_snd);
1507 * Attempt to look up the page.
1509 * Allocate if not found, wait and loop if busy, then
1510 * wire the page. splvm() protection is required to
1511 * maintain the object association (an interrupt can
1512 * free the page) through to the vm_page_wire() call.
1515 pg = vm_page_lookup(obj, pindex);
1517 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL);
1524 } else if (vm_page_sleep_busy(pg, TRUE, "sfpbsy")) {
1532 * If page is not valid for what we need, initiate I/O
1535 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1541 * Ensure that our page is still around when the I/O
1544 vm_page_io_start(pg);
1547 * Get the page from backing store.
1549 bsize = vp->v_mount->mnt_stat.f_iosize;
1550 auio.uio_iov = &aiov;
1551 auio.uio_iovcnt = 1;
1553 aiov.iov_len = MAXBSIZE;
1554 auio.uio_resid = MAXBSIZE;
1555 auio.uio_offset = trunc_page(off);
1556 auio.uio_segflg = UIO_NOCOPY;
1557 auio.uio_rw = UIO_READ;
1559 vn_lock(vp, NULL, LK_SHARED | LK_NOPAUSE | LK_RETRY, td);
1560 error = VOP_READ(vp, &auio,
1561 IO_VMIO | ((MAXBSIZE / bsize) << 16),
1563 VOP_UNLOCK(vp, NULL, 0, td);
1564 vm_page_flag_clear(pg, PG_ZERO);
1565 vm_page_io_finish(pg);
1567 vm_page_unwire(pg, 0);
1569 * See if anyone else might know about this page.
1570 * If not and it is not valid, then free it.
1572 if (pg->wire_count == 0 && pg->valid == 0 &&
1573 pg->busy == 0 && !(pg->flags & PG_BUSY) &&
1574 pg->hold_count == 0) {
1578 sbunlock(&so->so_snd);
1585 * Get a sendfile buf. We usually wait as long as necessary,
1586 * but this wait can be interrupted.
1588 if ((sf = sf_buf_alloc(pg, SFBA_PCATCH)) == NULL) {
1590 vm_page_unwire(pg, 0);
1591 if (pg->wire_count == 0 && pg->object == NULL)
1594 sbunlock(&so->so_snd);
1600 * Get an mbuf header and set it up as having external storage.
1602 MGETHDR(m, M_WAIT, MT_DATA);
1606 sbunlock(&so->so_snd);
1609 ++sf->aux1; /* wiring count */
1610 ++sf->aux2; /* initial reference */
1611 m->m_ext.ext_free = sf_buf_mext;
1612 m->m_ext.ext_ref = sf_buf_mref;
1613 m->m_ext.ext_buf = (void *)sf->kva;
1614 m->m_ext.ext_size = PAGE_SIZE;
1615 m->m_data = (char *) sf->kva + pgoff;
1616 m->m_flags |= M_EXT;
1617 m->m_pkthdr.len = m->m_len = xfsize;
1619 if (mheader != NULL) {
1620 hbytes = mheader->m_pkthdr.len;
1621 mheader->m_pkthdr.len += m->m_pkthdr.len;
1629 * Add the buffer to the socket buffer chain.
1634 * Make sure that the socket is still able to take more data.
1635 * CANTSENDMORE being true usually means that the connection
1636 * was closed. so_error is true when an error was sensed after
1638 * The state is checked after the page mapping and buffer
1639 * allocation above since those operations may block and make
1640 * any socket checks stale. From this point forward, nothing
1641 * blocks before the pru_send (or more accurately, any blocking
1642 * results in a loop back to here to re-check).
1644 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1645 if (so->so_state & SS_CANTSENDMORE) {
1648 error = so->so_error;
1652 sbunlock(&so->so_snd);
1657 * Wait for socket space to become available. We do this just
1658 * after checking the connection state above in order to avoid
1659 * a race condition with sbwait().
1661 if (sbspace(&so->so_snd) < so->so_snd.sb_lowat) {
1662 if (so->so_state & SS_NBIO) {
1664 sbunlock(&so->so_snd);
1669 error = sbwait(&so->so_snd);
1671 * An error from sbwait usually indicates that we've
1672 * been interrupted by a signal. If we've sent anything
1673 * then return bytes sent, otherwise return the error.
1677 sbunlock(&so->so_snd);
1683 error = so_pru_send(so, 0, m, NULL, NULL, td);
1686 sbunlock(&so->so_snd);
1690 if (mheader != NULL) {
1691 *sbytes += mheader->m_pkthdr.len;
1692 error = so_pru_send(so, 0, mheader, NULL, NULL, td);
1695 sbunlock(&so->so_snd);
1700 if (mheader != NULL)