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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
33 * $FreeBSD: src/sys/kern/uipc_syscalls.c,v 1.65.2.17 2003/04/04 17:11:16 tegge Exp $
36 #include "opt_ktrace.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/sysproto.h>
43 #include <sys/malloc.h>
44 #include <sys/filedesc.h>
45 #include <sys/event.h>
47 #include <sys/fcntl.h>
49 #include <sys/filio.h>
50 #include <sys/kern_syscall.h>
52 #include <sys/protosw.h>
53 #include <sys/sfbuf.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/socketops.h>
58 #include <sys/vnode.h>
60 #include <sys/mount.h>
62 #include <sys/ktrace.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_page.h>
67 #include <vm/vm_pageout.h>
68 #include <vm/vm_kern.h>
69 #include <vm/vm_extern.h>
70 #include <sys/file2.h>
71 #include <sys/signalvar.h>
72 #include <sys/serialize.h>
74 #include <sys/thread2.h>
75 #include <sys/msgport2.h>
76 #include <sys/socketvar2.h>
77 #include <net/netmsg2.h>
80 #include <netinet/sctp_peeloff.h>
83 extern int use_soaccept_pred_fast;
84 extern int use_sendfile_async;
87 * System call interface to the socket abstraction.
90 extern struct fileops socketops;
93 * socket_args(int domain, int type, int protocol)
96 kern_socket(int domain, int type, int protocol, int *res)
98 struct thread *td = curthread;
99 struct filedesc *fdp = td->td_proc->p_fd;
104 KKASSERT(td->td_lwp);
106 error = falloc(td->td_lwp, &fp, &fd);
109 error = socreate(domain, &so, type, protocol, td);
111 fsetfd(fdp, NULL, fd);
113 fp->f_type = DTYPE_SOCKET;
114 fp->f_flag = FREAD | FWRITE;
115 fp->f_ops = &socketops;
128 sys_socket(struct socket_args *uap)
132 error = kern_socket(uap->domain, uap->type, uap->protocol,
133 &uap->sysmsg_iresult);
139 kern_bind(int s, struct sockaddr *sa)
141 struct thread *td = curthread;
142 struct proc *p = td->td_proc;
147 error = holdsock(p->p_fd, s, &fp);
150 error = sobind((struct socket *)fp->f_data, sa, td);
156 * bind_args(int s, caddr_t name, int namelen)
161 sys_bind(struct bind_args *uap)
166 error = getsockaddr(&sa, uap->name, uap->namelen);
169 error = kern_bind(uap->s, sa);
176 kern_listen(int s, int backlog)
178 struct thread *td = curthread;
179 struct proc *p = td->td_proc;
184 error = holdsock(p->p_fd, s, &fp);
187 error = solisten((struct socket *)fp->f_data, backlog, td);
193 * listen_args(int s, int backlog)
198 sys_listen(struct listen_args *uap)
202 error = kern_listen(uap->s, uap->backlog);
207 * Returns the accepted socket as well.
209 * NOTE! The sockets sitting on so_comp/so_incomp might have 0 refs, the
210 * pool token is absolutely required to avoid a sofree() race,
211 * as well as to avoid tailq handling races.
214 soaccept_predicate(struct netmsg_so_notify *msg)
216 struct socket *head = msg->base.nm_so;
219 if (head->so_error != 0) {
220 msg->base.lmsg.ms_error = head->so_error;
223 lwkt_getpooltoken(head);
224 if (!TAILQ_EMPTY(&head->so_comp)) {
225 /* Abuse nm_so field as copy in/copy out parameter. XXX JH */
226 so = TAILQ_FIRST(&head->so_comp);
227 TAILQ_REMOVE(&head->so_comp, so, so_list);
229 soclrstate(so, SS_COMP);
233 lwkt_relpooltoken(head);
235 msg->base.lmsg.ms_error = 0;
236 msg->base.nm_so = so;
239 lwkt_relpooltoken(head);
240 if (head->so_state & SS_CANTRCVMORE) {
241 msg->base.lmsg.ms_error = ECONNABORTED;
244 if (msg->nm_fflags & FNONBLOCK) {
245 msg->base.lmsg.ms_error = EWOULDBLOCK;
253 * The second argument to kern_accept() is a handle to a struct sockaddr.
254 * This allows kern_accept() to return a pointer to an allocated struct
255 * sockaddr which must be freed later with FREE(). The caller must
256 * initialize *name to NULL.
259 kern_accept(int s, int fflags, struct sockaddr **name, int *namelen, int *res)
261 struct thread *td = curthread;
262 struct filedesc *fdp = td->td_proc->p_fd;
263 struct file *lfp = NULL;
264 struct file *nfp = NULL;
266 struct socket *head, *so;
267 struct netmsg_so_notify msg;
269 u_int fflag; /* type must match fp->f_flag */
273 if (name && namelen && *namelen < 0)
276 error = holdsock(td->td_proc->p_fd, s, &lfp);
280 error = falloc(td->td_lwp, &nfp, &fd);
281 if (error) { /* Probably ran out of file descriptors. */
285 head = (struct socket *)lfp->f_data;
286 if ((head->so_options & SO_ACCEPTCONN) == 0) {
291 if (fflags & O_FBLOCKING)
292 fflags |= lfp->f_flag & ~FNONBLOCK;
293 else if (fflags & O_FNONBLOCKING)
294 fflags |= lfp->f_flag | FNONBLOCK;
296 fflags = lfp->f_flag;
298 if (use_soaccept_pred_fast) {
301 /* Initialize necessary parts for soaccept_predicate() */
302 netmsg_init(&msg.base, head, &netisr_apanic_rport, 0, NULL);
303 msg.nm_fflags = fflags;
305 lwkt_getpooltoken(head);
306 pred = soaccept_predicate(&msg);
307 lwkt_relpooltoken(head);
310 error = msg.base.lmsg.ms_error;
318 /* optimize for uniprocessor case later XXX JH */
319 netmsg_init_abortable(&msg.base, head, &curthread->td_msgport,
320 0, netmsg_so_notify, netmsg_so_notify_doabort);
321 msg.nm_predicate = soaccept_predicate;
322 msg.nm_fflags = fflags;
323 msg.nm_etype = NM_REVENT;
324 error = lwkt_domsg(head->so_port, &msg.base.lmsg, PCATCH);
330 * At this point we have the connection that's ready to be accepted.
332 * NOTE! soaccept_predicate() ref'd so for us, and soaccept() expects
333 * to eat the ref and turn it into a descriptor.
339 /* connection has been removed from the listen queue */
340 KNOTE(&head->so_rcv.ssb_kq.ki_note, 0);
342 if (head->so_sigio != NULL)
343 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
345 nfp->f_type = DTYPE_SOCKET;
347 nfp->f_ops = &socketops;
349 /* Sync socket nonblocking/async state with file flags */
350 tmp = fflag & FNONBLOCK;
351 fo_ioctl(nfp, FIONBIO, (caddr_t)&tmp, td->td_ucred, NULL);
352 tmp = fflag & FASYNC;
353 fo_ioctl(nfp, FIOASYNC, (caddr_t)&tmp, td->td_ucred, NULL);
356 if (so->so_faddr != NULL) {
360 soaccept_generic(so);
363 error = soaccept(so, &sa);
367 * Set the returned name and namelen as applicable. Set the returned
368 * namelen to 0 for older code which might ignore the return value
372 if (sa && name && namelen) {
373 if (*namelen > sa->sa_len)
374 *namelen = sa->sa_len;
384 * If an error occured clear the reserved descriptor, else associate
387 * Note that *res is normally ignored if an error is returned but
388 * a syscall message will still have access to the result code.
391 fsetfd(fdp, NULL, fd);
394 fsetfd(fdp, nfp, fd);
402 * accept(int s, caddr_t name, int *anamelen)
407 sys_accept(struct accept_args *uap)
409 struct sockaddr *sa = NULL;
414 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
418 error = kern_accept(uap->s, 0, &sa, &sa_len,
419 &uap->sysmsg_iresult);
422 error = copyout(sa, uap->name, sa_len);
424 error = copyout(&sa_len, uap->anamelen,
425 sizeof(*uap->anamelen));
430 error = kern_accept(uap->s, 0, NULL, 0,
431 &uap->sysmsg_iresult);
437 * extaccept(int s, int fflags, caddr_t name, int *anamelen)
442 sys_extaccept(struct extaccept_args *uap)
444 struct sockaddr *sa = NULL;
447 int fflags = uap->flags & O_FMASK;
450 error = copyin(uap->anamelen, &sa_len, sizeof(sa_len));
454 error = kern_accept(uap->s, fflags, &sa, &sa_len,
455 &uap->sysmsg_iresult);
458 error = copyout(sa, uap->name, sa_len);
460 error = copyout(&sa_len, uap->anamelen,
461 sizeof(*uap->anamelen));
466 error = kern_accept(uap->s, fflags, NULL, 0,
467 &uap->sysmsg_iresult);
474 * Returns TRUE if predicate satisfied.
477 soconnected_predicate(struct netmsg_so_notify *msg)
479 struct socket *so = msg->base.nm_so;
481 /* check predicate */
482 if (!(so->so_state & SS_ISCONNECTING) || so->so_error != 0) {
483 msg->base.lmsg.ms_error = so->so_error;
491 kern_connect(int s, int fflags, struct sockaddr *sa)
493 struct thread *td = curthread;
494 struct proc *p = td->td_proc;
497 int error, interrupted = 0;
499 error = holdsock(p->p_fd, s, &fp);
502 so = (struct socket *)fp->f_data;
504 if (fflags & O_FBLOCKING)
505 /* fflags &= ~FNONBLOCK; */;
506 else if (fflags & O_FNONBLOCKING)
511 if (so->so_state & SS_ISCONNECTING) {
515 error = soconnect(so, sa, td);
518 if ((fflags & FNONBLOCK) && (so->so_state & SS_ISCONNECTING)) {
522 if ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
523 struct netmsg_so_notify msg;
525 netmsg_init_abortable(&msg.base, so,
526 &curthread->td_msgport,
529 netmsg_so_notify_doabort);
530 msg.nm_predicate = soconnected_predicate;
531 msg.nm_etype = NM_REVENT;
532 error = lwkt_domsg(so->so_port, &msg.base.lmsg, PCATCH);
533 if (error == EINTR || error == ERESTART)
537 error = so->so_error;
542 soclrstate(so, SS_ISCONNECTING);
543 if (error == ERESTART)
551 * connect_args(int s, caddr_t name, int namelen)
556 sys_connect(struct connect_args *uap)
561 error = getsockaddr(&sa, uap->name, uap->namelen);
564 error = kern_connect(uap->s, 0, sa);
571 * connect_args(int s, int fflags, caddr_t name, int namelen)
576 sys_extconnect(struct extconnect_args *uap)
580 int fflags = uap->flags & O_FMASK;
582 error = getsockaddr(&sa, uap->name, uap->namelen);
585 error = kern_connect(uap->s, fflags, sa);
592 kern_socketpair(int domain, int type, int protocol, int *sv)
594 struct thread *td = curthread;
595 struct filedesc *fdp;
596 struct file *fp1, *fp2;
597 struct socket *so1, *so2;
600 fdp = td->td_proc->p_fd;
601 error = socreate(domain, &so1, type, protocol, td);
604 error = socreate(domain, &so2, type, protocol, td);
607 error = falloc(td->td_lwp, &fp1, &fd1);
612 error = falloc(td->td_lwp, &fp2, &fd2);
617 error = soconnect2(so1, so2);
620 if (type == SOCK_DGRAM) {
622 * Datagram socket connection is asymmetric.
624 error = soconnect2(so2, so1);
628 fp1->f_type = fp2->f_type = DTYPE_SOCKET;
629 fp1->f_flag = fp2->f_flag = FREAD|FWRITE;
630 fp1->f_ops = fp2->f_ops = &socketops;
631 fsetfd(fdp, fp1, fd1);
632 fsetfd(fdp, fp2, fd2);
637 fsetfd(fdp, NULL, fd2);
640 fsetfd(fdp, NULL, fd1);
643 (void)soclose(so2, 0);
645 (void)soclose(so1, 0);
650 * socketpair(int domain, int type, int protocol, int *rsv)
653 sys_socketpair(struct socketpair_args *uap)
657 error = kern_socketpair(uap->domain, uap->type, uap->protocol, sockv);
660 error = copyout(sockv, uap->rsv, sizeof(sockv));
663 kern_close(sockv[0]);
664 kern_close(sockv[1]);
672 kern_sendmsg(int s, struct sockaddr *sa, struct uio *auio,
673 struct mbuf *control, int flags, size_t *res)
675 struct thread *td = curthread;
676 struct lwp *lp = td->td_lwp;
677 struct proc *p = td->td_proc;
683 struct iovec *ktriov = NULL;
687 error = holdsock(p->p_fd, s, &fp);
691 if (KTRPOINT(td, KTR_GENIO)) {
692 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
694 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
695 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
699 len = auio->uio_resid;
700 so = (struct socket *)fp->f_data;
701 if ((flags & (MSG_FNONBLOCKING|MSG_FBLOCKING)) == 0) {
702 if (fp->f_flag & FNONBLOCK)
703 flags |= MSG_FNONBLOCKING;
705 error = so_pru_sosend(so, sa, auio, NULL, control, flags, td);
707 if (auio->uio_resid != len && (error == ERESTART ||
708 error == EINTR || error == EWOULDBLOCK))
710 if (error == EPIPE && !(flags & MSG_NOSIGNAL) &&
711 !(so->so_options & SO_NOSIGPIPE))
712 lwpsignal(p, lp, SIGPIPE);
715 if (ktriov != NULL) {
717 ktruio.uio_iov = ktriov;
718 ktruio.uio_resid = len - auio->uio_resid;
719 ktrgenio(lp, s, UIO_WRITE, &ktruio, error);
721 kfree(ktriov, M_TEMP);
725 *res = len - auio->uio_resid;
731 * sendto_args(int s, caddr_t buf, size_t len, int flags, caddr_t to, int tolen)
736 sys_sendto(struct sendto_args *uap)
738 struct thread *td = curthread;
741 struct sockaddr *sa = NULL;
745 error = getsockaddr(&sa, uap->to, uap->tolen);
749 aiov.iov_base = uap->buf;
750 aiov.iov_len = uap->len;
751 auio.uio_iov = &aiov;
754 auio.uio_resid = uap->len;
755 auio.uio_segflg = UIO_USERSPACE;
756 auio.uio_rw = UIO_WRITE;
759 error = kern_sendmsg(uap->s, sa, &auio, NULL, uap->flags,
760 &uap->sysmsg_szresult);
768 * sendmsg_args(int s, caddr_t msg, int flags)
773 sys_sendmsg(struct sendmsg_args *uap)
775 struct thread *td = curthread;
778 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
779 struct sockaddr *sa = NULL;
780 struct mbuf *control = NULL;
783 error = copyin(uap->msg, (caddr_t)&msg, sizeof(msg));
788 * Conditionally copyin msg.msg_name.
791 error = getsockaddr(&sa, msg.msg_name, msg.msg_namelen);
799 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
804 auio.uio_iovcnt = msg.msg_iovlen;
806 auio.uio_segflg = UIO_USERSPACE;
807 auio.uio_rw = UIO_WRITE;
811 * Conditionally copyin msg.msg_control.
813 if (msg.msg_control) {
814 if (msg.msg_controllen < sizeof(struct cmsghdr) ||
815 msg.msg_controllen > MLEN) {
819 control = m_get(MB_WAIT, MT_CONTROL);
820 if (control == NULL) {
824 control->m_len = msg.msg_controllen;
825 error = copyin(msg.msg_control, mtod(control, caddr_t),
833 error = kern_sendmsg(uap->s, sa, &auio, control, uap->flags,
834 &uap->sysmsg_szresult);
837 iovec_free(&iov, aiov);
845 * kern_recvmsg() takes a handle to sa and control. If the handle is non-
846 * null, it returns a dynamically allocated struct sockaddr and an mbuf.
847 * Don't forget to FREE() and m_free() these if they are returned.
850 kern_recvmsg(int s, struct sockaddr **sa, struct uio *auio,
851 struct mbuf **control, int *flags, size_t *res)
853 struct thread *td = curthread;
854 struct proc *p = td->td_proc;
861 struct iovec *ktriov = NULL;
865 error = holdsock(p->p_fd, s, &fp);
869 if (KTRPOINT(td, KTR_GENIO)) {
870 int iovlen = auio->uio_iovcnt * sizeof (struct iovec);
872 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
873 bcopy(auio->uio_iov, ktriov, iovlen);
877 len = auio->uio_resid;
878 so = (struct socket *)fp->f_data;
880 if (flags == NULL || (*flags & (MSG_FNONBLOCKING|MSG_FBLOCKING)) == 0) {
881 if (fp->f_flag & FNONBLOCK) {
883 *flags |= MSG_FNONBLOCKING;
885 lflags = MSG_FNONBLOCKING;
891 error = so_pru_soreceive(so, sa, auio, NULL, control, flags);
893 if (auio->uio_resid != len && (error == ERESTART ||
894 error == EINTR || error == EWOULDBLOCK))
898 if (ktriov != NULL) {
900 ktruio.uio_iov = ktriov;
901 ktruio.uio_resid = len - auio->uio_resid;
902 ktrgenio(td->td_lwp, s, UIO_READ, &ktruio, error);
904 kfree(ktriov, M_TEMP);
908 *res = len - auio->uio_resid;
914 * recvfrom_args(int s, caddr_t buf, size_t len, int flags,
915 * caddr_t from, int *fromlenaddr)
920 sys_recvfrom(struct recvfrom_args *uap)
922 struct thread *td = curthread;
925 struct sockaddr *sa = NULL;
928 if (uap->from && uap->fromlenaddr) {
929 error = copyin(uap->fromlenaddr, &fromlen, sizeof(fromlen));
937 aiov.iov_base = uap->buf;
938 aiov.iov_len = uap->len;
939 auio.uio_iov = &aiov;
942 auio.uio_resid = uap->len;
943 auio.uio_segflg = UIO_USERSPACE;
944 auio.uio_rw = UIO_READ;
947 error = kern_recvmsg(uap->s, uap->from ? &sa : NULL, &auio, NULL,
948 &uap->flags, &uap->sysmsg_szresult);
950 if (error == 0 && uap->from) {
951 /* note: sa may still be NULL */
953 fromlen = MIN(fromlen, sa->sa_len);
954 error = copyout(sa, uap->from, fromlen);
959 error = copyout(&fromlen, uap->fromlenaddr,
970 * recvmsg_args(int s, struct msghdr *msg, int flags)
975 sys_recvmsg(struct recvmsg_args *uap)
977 struct thread *td = curthread;
980 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
981 struct mbuf *m, *control = NULL;
982 struct sockaddr *sa = NULL;
984 socklen_t *ufromlenp, *ucontrollenp;
985 int error, fromlen, controllen, len, flags, *uflagsp;
988 * This copyin handles everything except the iovec.
990 error = copyin(uap->msg, &msg, sizeof(msg));
994 if (msg.msg_name && msg.msg_namelen < 0)
996 if (msg.msg_control && msg.msg_controllen < 0)
999 ufromlenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
1001 ucontrollenp = (socklen_t *)((caddr_t)uap->msg + offsetof(struct msghdr,
1003 uflagsp = (int *)((caddr_t)uap->msg + offsetof(struct msghdr,
1009 error = iovec_copyin(msg.msg_iov, &iov, aiov, msg.msg_iovlen,
1014 auio.uio_iovcnt = msg.msg_iovlen;
1015 auio.uio_offset = 0;
1016 auio.uio_segflg = UIO_USERSPACE;
1017 auio.uio_rw = UIO_READ;
1022 error = kern_recvmsg(uap->s,
1023 (msg.msg_name ? &sa : NULL), &auio,
1024 (msg.msg_control ? &control : NULL), &flags,
1025 &uap->sysmsg_szresult);
1028 * Conditionally copyout the name and populate the namelen field.
1030 if (error == 0 && msg.msg_name) {
1031 /* note: sa may still be NULL */
1033 fromlen = MIN(msg.msg_namelen, sa->sa_len);
1034 error = copyout(sa, msg.msg_name, fromlen);
1039 error = copyout(&fromlen, ufromlenp,
1040 sizeof(*ufromlenp));
1044 * Copyout msg.msg_control and msg.msg_controllen.
1046 if (error == 0 && msg.msg_control) {
1047 len = msg.msg_controllen;
1049 ctlbuf = (caddr_t)msg.msg_control;
1051 while(m && len > 0) {
1052 unsigned int tocopy;
1054 if (len >= m->m_len) {
1057 msg.msg_flags |= MSG_CTRUNC;
1061 error = copyout(mtod(m, caddr_t), ctlbuf, tocopy);
1069 controllen = ctlbuf - (caddr_t)msg.msg_control;
1070 error = copyout(&controllen, ucontrollenp,
1071 sizeof(*ucontrollenp));
1075 error = copyout(&flags, uflagsp, sizeof(*uflagsp));
1079 kfree(sa, M_SONAME);
1080 iovec_free(&iov, aiov);
1087 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
1088 * in kernel pointer instead of a userland pointer. This allows us
1089 * to manipulate socket options in the emulation code.
1092 kern_setsockopt(int s, struct sockopt *sopt)
1094 struct thread *td = curthread;
1095 struct proc *p = td->td_proc;
1099 if (sopt->sopt_val == NULL && sopt->sopt_valsize != 0)
1101 if (sopt->sopt_val != NULL && sopt->sopt_valsize == 0)
1103 if (sopt->sopt_valsize > SOMAXOPT_SIZE) /* unsigned */
1106 error = holdsock(p->p_fd, s, &fp);
1110 error = sosetopt((struct socket *)fp->f_data, sopt);
1116 * setsockopt_args(int s, int level, int name, caddr_t val, int valsize)
1121 sys_setsockopt(struct setsockopt_args *uap)
1123 struct thread *td = curthread;
1124 struct sockopt sopt;
1127 sopt.sopt_level = uap->level;
1128 sopt.sopt_name = uap->name;
1129 sopt.sopt_valsize = uap->valsize;
1131 sopt.sopt_val = NULL;
1133 if (sopt.sopt_valsize > SOMAXOPT_SIZE) /* unsigned */
1136 sopt.sopt_val = kmalloc(sopt.sopt_valsize, M_TEMP, M_WAITOK);
1137 error = copyin(uap->val, sopt.sopt_val, sopt.sopt_valsize);
1142 error = kern_setsockopt(uap->s, &sopt);
1145 kfree(sopt.sopt_val, M_TEMP);
1150 * If sopt->sopt_td == NULL, then sopt->sopt_val is treated as an
1151 * in kernel pointer instead of a userland pointer. This allows us
1152 * to manipulate socket options in the emulation code.
1155 kern_getsockopt(int s, struct sockopt *sopt)
1157 struct thread *td = curthread;
1158 struct proc *p = td->td_proc;
1162 if (sopt->sopt_val == NULL && sopt->sopt_valsize != 0)
1164 if (sopt->sopt_val != NULL && sopt->sopt_valsize == 0)
1166 if (sopt->sopt_valsize > SOMAXOPT_SIZE) /* unsigned */
1169 error = holdsock(p->p_fd, s, &fp);
1173 error = sogetopt((struct socket *)fp->f_data, sopt);
1179 * getsockopt_args(int s, int level, int name, caddr_t val, int *avalsize)
1184 sys_getsockopt(struct getsockopt_args *uap)
1186 struct thread *td = curthread;
1187 struct sockopt sopt;
1191 error = copyin(uap->avalsize, &valsize, sizeof(valsize));
1198 sopt.sopt_level = uap->level;
1199 sopt.sopt_name = uap->name;
1200 sopt.sopt_valsize = valsize;
1202 sopt.sopt_val = NULL;
1204 if (sopt.sopt_valsize > SOMAXOPT_SIZE) /* unsigned */
1207 sopt.sopt_val = kmalloc(sopt.sopt_valsize, M_TEMP, M_WAITOK);
1208 error = copyin(uap->val, sopt.sopt_val, sopt.sopt_valsize);
1213 error = kern_getsockopt(uap->s, &sopt);
1216 valsize = sopt.sopt_valsize;
1217 error = copyout(&valsize, uap->avalsize, sizeof(valsize));
1221 error = copyout(sopt.sopt_val, uap->val, sopt.sopt_valsize);
1224 kfree(sopt.sopt_val, M_TEMP);
1229 * The second argument to kern_getsockname() is a handle to a struct sockaddr.
1230 * This allows kern_getsockname() to return a pointer to an allocated struct
1231 * sockaddr which must be freed later with FREE(). The caller must
1232 * initialize *name to NULL.
1235 kern_getsockname(int s, struct sockaddr **name, int *namelen)
1237 struct thread *td = curthread;
1238 struct proc *p = td->td_proc;
1241 struct sockaddr *sa = NULL;
1244 error = holdsock(p->p_fd, s, &fp);
1251 so = (struct socket *)fp->f_data;
1252 error = so_pru_sockaddr(so, &sa);
1257 *namelen = MIN(*namelen, sa->sa_len);
1267 * getsockname_args(int fdes, caddr_t asa, int *alen)
1274 sys_getsockname(struct getsockname_args *uap)
1276 struct sockaddr *sa = NULL;
1279 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1283 error = kern_getsockname(uap->fdes, &sa, &sa_len);
1286 error = copyout(sa, uap->asa, sa_len);
1288 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1290 kfree(sa, M_SONAME);
1295 * The second argument to kern_getpeername() is a handle to a struct sockaddr.
1296 * This allows kern_getpeername() to return a pointer to an allocated struct
1297 * sockaddr which must be freed later with FREE(). The caller must
1298 * initialize *name to NULL.
1301 kern_getpeername(int s, struct sockaddr **name, int *namelen)
1303 struct thread *td = curthread;
1304 struct proc *p = td->td_proc;
1307 struct sockaddr *sa = NULL;
1310 error = holdsock(p->p_fd, s, &fp);
1317 so = (struct socket *)fp->f_data;
1318 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1322 error = so_pru_peeraddr(so, &sa);
1327 *namelen = MIN(*namelen, sa->sa_len);
1337 * getpeername_args(int fdes, caddr_t asa, int *alen)
1339 * Get name of peer for connected socket.
1344 sys_getpeername(struct getpeername_args *uap)
1346 struct sockaddr *sa = NULL;
1349 error = copyin(uap->alen, &sa_len, sizeof(sa_len));
1353 error = kern_getpeername(uap->fdes, &sa, &sa_len);
1356 error = copyout(sa, uap->asa, sa_len);
1358 error = copyout(&sa_len, uap->alen, sizeof(*uap->alen));
1360 kfree(sa, M_SONAME);
1365 getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len)
1367 struct sockaddr *sa;
1371 if (len > SOCK_MAXADDRLEN)
1372 return ENAMETOOLONG;
1373 if (len < offsetof(struct sockaddr, sa_data[0]))
1375 sa = kmalloc(len, M_SONAME, M_WAITOK);
1376 error = copyin(uaddr, sa, len);
1378 kfree(sa, M_SONAME);
1380 #if BYTE_ORDER != BIG_ENDIAN
1382 * The bind(), connect(), and sendto() syscalls were not
1383 * versioned for COMPAT_43. Thus, this check must stay.
1385 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1386 sa->sa_family = sa->sa_len;
1395 * Detach a mapped page and release resources back to the system.
1396 * We must release our wiring and if the object is ripped out
1397 * from under the vm_page we become responsible for freeing the
1403 sf_buf_mfree(void *arg)
1405 struct sf_buf *sf = arg;
1408 m = sf_buf_page(sf);
1409 if (sf_buf_free(sf)) {
1410 /* sf invalid now */
1411 vm_page_busy_wait(m, FALSE, "sockpgf");
1412 vm_page_unwire(m, 0);
1413 if (m->object == NULL &&
1414 m->wire_count == 0 &&
1415 (m->flags & PG_NEED_COMMIT) == 0) {
1425 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1426 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1428 * Send a file specified by 'fd' and starting at 'offset' to a socket
1429 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1430 * nbytes == 0. Optionally add a header and/or trailer to the socket
1431 * output. If specified, write the total number of bytes sent into *sbytes.
1433 * In FreeBSD kern/uipc_syscalls.c,v 1.103, a bug was fixed that caused
1434 * the headers to count against the remaining bytes to be sent from
1435 * the file descriptor. We may wish to implement a compatibility syscall
1441 sys_sendfile(struct sendfile_args *uap)
1443 struct thread *td = curthread;
1444 struct proc *p = td->td_proc;
1446 struct vnode *vp = NULL;
1447 struct sf_hdtr hdtr;
1448 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
1450 struct mbuf *mheader = NULL;
1453 off_t hdtr_size = 0;
1460 * Do argument checking. Must be a regular file in, stream
1461 * type and connected socket out, positive offset.
1463 fp = holdfp(p->p_fd, uap->fd, FREAD);
1467 if (fp->f_type != DTYPE_VNODE) {
1471 vp = (struct vnode *)fp->f_data;
1476 * If specified, get the pointer to the sf_hdtr struct for
1477 * any headers/trailers.
1480 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1487 error = iovec_copyin(hdtr.headers, &iov, aiov,
1488 hdtr.hdr_cnt, &hbytes);
1492 auio.uio_iovcnt = hdtr.hdr_cnt;
1493 auio.uio_offset = 0;
1494 auio.uio_segflg = UIO_USERSPACE;
1495 auio.uio_rw = UIO_WRITE;
1497 auio.uio_resid = hbytes;
1499 mheader = m_uiomove(&auio);
1501 iovec_free(&iov, aiov);
1502 if (mheader == NULL)
1507 error = kern_sendfile(vp, uap->s, uap->offset, uap->nbytes, mheader,
1508 &sbytes, uap->flags);
1513 * Send trailers. Wimp out and use writev(2).
1515 if (uap->hdtr != NULL && hdtr.trailers != NULL) {
1516 error = iovec_copyin(hdtr.trailers, &iov, aiov,
1517 hdtr.trl_cnt, &auio.uio_resid);
1521 auio.uio_iovcnt = hdtr.trl_cnt;
1522 auio.uio_offset = 0;
1523 auio.uio_segflg = UIO_USERSPACE;
1524 auio.uio_rw = UIO_WRITE;
1527 error = kern_sendmsg(uap->s, NULL, &auio, NULL, 0, &tbytes);
1529 iovec_free(&iov, aiov);
1532 hdtr_size += tbytes; /* trailer bytes successfully sent */
1538 if (uap->sbytes != NULL) {
1539 sbytes += hdtr_size;
1540 copyout(&sbytes, uap->sbytes, sizeof(off_t));
1546 kern_sendfile(struct vnode *vp, int sfd, off_t offset, size_t nbytes,
1547 struct mbuf *mheader, off_t *sbytes, int flags)
1549 struct thread *td = curthread;
1550 struct proc *p = td->td_proc;
1551 struct vm_object *obj;
1554 struct mbuf *m, *mp;
1561 if (vp->v_type != VREG) {
1565 if ((obj = vp->v_object) == NULL) {
1569 error = holdsock(p->p_fd, sfd, &fp);
1572 so = (struct socket *)fp->f_data;
1573 if (so->so_type != SOCK_STREAM) {
1577 if ((so->so_state & SS_ISCONNECTED) == 0) {
1588 * Protect against multiple writers to the socket.
1590 ssb_lock(&so->so_snd, M_WAITOK);
1593 * Loop through the pages in the file, starting with the requested
1594 * offset. Get a file page (do I/O if necessary), map the file page
1595 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1598 for (off = offset; ; off += xfsize, *sbytes += xfsize + hbytes) {
1603 pindex = OFF_TO_IDX(off);
1606 * Calculate the amount to transfer. Not to exceed a page,
1607 * the EOF, or the passed in nbytes.
1609 xfsize = vp->v_filesize - off;
1610 if (xfsize > PAGE_SIZE)
1612 pgoff = (vm_offset_t)(off & PAGE_MASK);
1613 if (PAGE_SIZE - pgoff < xfsize)
1614 xfsize = PAGE_SIZE - pgoff;
1615 if (nbytes && xfsize > (nbytes - *sbytes))
1616 xfsize = nbytes - *sbytes;
1620 * Optimize the non-blocking case by looking at the socket space
1621 * before going to the extra work of constituting the sf_buf.
1623 if ((fp->f_flag & FNONBLOCK) &&
1624 ssb_space_prealloc(&so->so_snd) <= 0) {
1625 if (so->so_state & SS_CANTSENDMORE)
1629 ssb_unlock(&so->so_snd);
1633 * Attempt to look up the page.
1635 * Allocate if not found, wait and loop if busy, then
1636 * wire the page. critical section protection is
1637 * required to maintain the object association (an
1638 * interrupt can free the page) through to the
1639 * vm_page_wire() call.
1641 vm_object_hold(obj);
1642 pg = vm_page_lookup_busy_try(obj, pindex, TRUE, &error);
1644 vm_page_sleep_busy(pg, TRUE, "sfpbsy");
1645 vm_object_drop(obj);
1649 pg = vm_page_alloc(obj, pindex, VM_ALLOC_NORMAL |
1653 vm_object_drop(obj);
1658 vm_object_drop(obj);
1661 * If page is not valid for what we need, initiate I/O
1664 if (!pg->valid || !vm_page_is_valid(pg, pgoff, xfsize)) {
1670 * Ensure that our page is still around when the I/O
1673 * Ensure that our page is not modified while part of
1674 * a mbuf as this could mess up tcp checksums, DMA,
1675 * etc (XXX NEEDS WORK). The softbusy is supposed to
1676 * help here but it actually doesn't.
1678 * XXX THIS HAS MULTIPLE PROBLEMS. The underlying
1679 * VM pages are not protected by the soft-busy
1680 * unless we vm_page_protect... READ them, and
1681 * they STILL aren't protected against
1682 * modification via the buffer cache (VOP_WRITE).
1684 * Fixing the second issue is particularly
1687 * XXX We also can't soft-busy anyway because it can
1688 * deadlock against the syncer doing a vfs_msync(),
1689 * vfs_msync->vmntvnodesca->vfs_msync_scan2->
1690 * vm_object_page_clean->(scan)-> ... page
1693 /*vm_page_io_start(pg);*/
1697 * Get the page from backing store.
1699 bsize = vp->v_mount->mnt_stat.f_iosize;
1700 auio.uio_iov = &aiov;
1701 auio.uio_iovcnt = 1;
1703 aiov.iov_len = MAXBSIZE;
1704 auio.uio_resid = MAXBSIZE;
1705 auio.uio_offset = trunc_page(off);
1706 auio.uio_segflg = UIO_NOCOPY;
1707 auio.uio_rw = UIO_READ;
1709 vn_lock(vp, LK_SHARED | LK_RETRY);
1710 error = VOP_READ(vp, &auio,
1711 IO_VMIO | ((MAXBSIZE / bsize) << 16),
1714 vm_page_flag_clear(pg, PG_ZERO);
1715 vm_page_busy_wait(pg, FALSE, "sockpg");
1716 /*vm_page_io_finish(pg);*/
1718 vm_page_unwire(pg, 0);
1720 vm_page_try_to_free(pg);
1721 ssb_unlock(&so->so_snd);
1728 * Get a sendfile buf. We usually wait as long as necessary,
1729 * but this wait can be interrupted.
1731 if ((sf = sf_buf_alloc(pg)) == NULL) {
1732 vm_page_unwire(pg, 0);
1734 vm_page_try_to_free(pg);
1735 ssb_unlock(&so->so_snd);
1742 * Get an mbuf header and set it up as having external storage.
1744 MGETHDR(m, MB_WAIT, MT_DATA);
1748 ssb_unlock(&so->so_snd);
1752 m->m_ext.ext_free = sf_buf_mfree;
1753 m->m_ext.ext_ref = sf_buf_ref;
1754 m->m_ext.ext_arg = sf;
1755 m->m_ext.ext_buf = (void *)sf_buf_kva(sf);
1756 m->m_ext.ext_size = PAGE_SIZE;
1757 m->m_data = (char *)sf_buf_kva(sf) + pgoff;
1758 m->m_flags |= M_EXT;
1759 m->m_pkthdr.len = m->m_len = xfsize;
1760 KKASSERT((m->m_flags & (M_EXT_CLUSTER)) == 0);
1762 if (mheader != NULL) {
1763 hbytes = mheader->m_pkthdr.len;
1764 mheader->m_pkthdr.len += m->m_pkthdr.len;
1772 * Add the buffer to the socket buffer chain.
1777 * Make sure that the socket is still able to take more data.
1778 * CANTSENDMORE being true usually means that the connection
1779 * was closed. so_error is true when an error was sensed after
1781 * The state is checked after the page mapping and buffer
1782 * allocation above since those operations may block and make
1783 * any socket checks stale. From this point forward, nothing
1784 * blocks before the pru_send (or more accurately, any blocking
1785 * results in a loop back to here to re-check).
1787 if ((so->so_state & SS_CANTSENDMORE) || so->so_error) {
1788 if (so->so_state & SS_CANTSENDMORE) {
1791 error = so->so_error;
1795 ssb_unlock(&so->so_snd);
1800 * Wait for socket space to become available. We do this just
1801 * after checking the connection state above in order to avoid
1802 * a race condition with ssb_wait().
1804 space = ssb_space_prealloc(&so->so_snd);
1805 if (space < m->m_pkthdr.len && space < so->so_snd.ssb_lowat) {
1806 if (fp->f_flag & FNONBLOCK) {
1808 ssb_unlock(&so->so_snd);
1813 error = ssb_wait(&so->so_snd);
1815 * An error from ssb_wait usually indicates that we've
1816 * been interrupted by a signal. If we've sent anything
1817 * then return bytes sent, otherwise return the error.
1821 ssb_unlock(&so->so_snd);
1828 for (mp = m; mp != NULL; mp = mp->m_next)
1829 ssb_preallocstream(&so->so_snd, mp);
1830 if (use_sendfile_async)
1831 error = so_pru_senda(so, 0, m, NULL, NULL, td);
1833 error = so_pru_send(so, 0, m, NULL, NULL, td);
1837 ssb_unlock(&so->so_snd);
1841 if (mheader != NULL) {
1842 *sbytes += mheader->m_pkthdr.len;
1844 for (mp = mheader; mp != NULL; mp = mp->m_next)
1845 ssb_preallocstream(&so->so_snd, mp);
1846 if (use_sendfile_async)
1847 error = so_pru_senda(so, 0, mheader, NULL, NULL, td);
1849 error = so_pru_send(so, 0, mheader, NULL, NULL, td);
1853 ssb_unlock(&so->so_snd);
1858 if (mheader != NULL)
1867 sys_sctp_peeloff(struct sctp_peeloff_args *uap)
1870 struct thread *td = curthread;
1871 struct filedesc *fdp = td->td_proc->p_fd;
1872 struct file *lfp = NULL;
1873 struct file *nfp = NULL;
1875 struct socket *head, *so;
1878 short fflag; /* type must match fp->f_flag */
1880 assoc_id = uap->name;
1881 error = holdsock(td->td_proc->p_fd, uap->sd, &lfp);
1886 head = (struct socket *)lfp->f_data;
1887 error = sctp_can_peel_off(head, assoc_id);
1893 * At this point we know we do have a assoc to pull
1894 * we proceed to get the fd setup. This may block
1898 fflag = lfp->f_flag;
1899 error = falloc(td->td_lwp, &nfp, &fd);
1902 * Probably ran out of file descriptors. Put the
1903 * unaccepted connection back onto the queue and
1904 * do another wakeup so some other process might
1905 * have a chance at it.
1910 uap->sysmsg_iresult = fd;
1912 so = sctp_get_peeloff(head, assoc_id, &error);
1915 * Either someone else peeled it off OR
1916 * we can't get a socket.
1920 soreference(so); /* reference needed */
1921 soclrstate(so, SS_NOFDREF | SS_COMP); /* when clearing NOFDREF */
1923 if (head->so_sigio != NULL)
1924 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
1926 nfp->f_type = DTYPE_SOCKET;
1927 nfp->f_flag = fflag;
1928 nfp->f_ops = &socketops;
1933 * Assign the file pointer to the reserved descriptor, or clear
1934 * the reserved descriptor if an error occured.
1937 fsetfd(fdp, NULL, fd);
1939 fsetfd(fdp, nfp, fd);
1942 * Release explicitly held references before returning.