2 * Copyright (c) 1982, 1986, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * sendfile(2) and related extensions:
6 * Copyright (c) 1998, David Greenman. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 #include "opt_inet6.h"
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>
48 #include <sys/mutex.h>
49 #include <sys/sysproto.h>
50 #include <sys/malloc.h>
51 #include <sys/filedesc.h>
52 #include <sys/event.h>
54 #include <sys/fcntl.h>
56 #include <sys/filio.h>
58 #include <sys/mount.h>
60 #include <sys/protosw.h>
61 #include <sys/sf_buf.h>
62 #include <sys/sysent.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/signalvar.h>
66 #include <sys/syscallsubr.h>
67 #include <sys/sysctl.h>
69 #include <sys/vnode.h>
71 #include <sys/ktrace.h>
73 #ifdef COMPAT_FREEBSD32
74 #include <compat/freebsd32/freebsd32_util.h>
79 #include <security/audit/audit.h>
80 #include <security/mac/mac_framework.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_pageout.h>
86 #include <vm/vm_kern.h>
87 #include <vm/vm_extern.h>
89 #if defined(INET) || defined(INET6)
91 #include <netinet/sctp.h>
92 #include <netinet/sctp_peeloff.h>
94 #endif /* INET || INET6 */
96 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
97 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
99 static int accept1(struct thread *td, struct accept_args *uap, int compat);
100 static int do_sendfile(struct thread *td, struct sendfile_args *uap, int compat);
101 static int getsockname1(struct thread *td, struct getsockname_args *uap,
103 static int getpeername1(struct thread *td, struct getpeername_args *uap,
107 * NSFBUFS-related variables and associated sysctls
113 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
114 "Maximum number of sendfile(2) sf_bufs available");
115 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
116 "Number of sendfile(2) sf_bufs at peak usage");
117 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
118 "Number of sendfile(2) sf_bufs in use");
121 * Convert a user file descriptor to a kernel file entry. A reference on the
122 * file entry is held upon returning. This is lighter weight than
123 * fgetsock(), which bumps the socket reference drops the file reference
124 * count instead, as this approach avoids several additional mutex operations
125 * associated with the additional reference count. If requested, return the
129 getsock(struct filedesc *fdp, int fd, struct file **fpp, u_int *fflagp)
135 if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL) {
137 } else if (fp->f_type != DTYPE_SOCKET) {
138 fdrop(fp, curthread);
143 *fflagp = fp->f_flag;
151 * System call interface to the socket abstraction.
153 #if defined(COMPAT_43)
154 #define COMPAT_OLDSOCK
160 struct socket_args /* {
166 struct filedesc *fdp;
171 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
173 error = mac_socket_check_create(td->td_ucred, uap->domain, uap->type,
178 fdp = td->td_proc->p_fd;
179 error = falloc(td, &fp, &fd);
182 /* An extra reference on `fp' has been held for us by falloc(). */
183 error = socreate(uap->domain, &so, uap->type, uap->protocol,
186 fdclose(fdp, fp, fd, td);
188 finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &socketops);
189 td->td_retval[0] = fd;
199 struct bind_args /* {
208 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
211 error = kern_bind(td, uap->s, sa);
217 kern_bind(td, fd, sa)
227 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
232 if (KTRPOINT(td, KTR_STRUCT))
236 error = mac_socket_check_bind(td->td_ucred, so, sa);
239 error = sobind(so, sa, td);
248 struct listen_args /* {
257 AUDIT_ARG_FD(uap->s);
258 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
262 error = mac_socket_check_listen(td->td_ucred, so);
265 CURVNET_SET(so->so_vnet);
266 error = solisten(so, uap->backlog, td);
280 accept1(td, uap, compat)
282 struct accept_args /* {
284 struct sockaddr * __restrict name;
285 socklen_t * __restrict anamelen;
289 struct sockaddr *name;
294 if (uap->name == NULL)
295 return (kern_accept(td, uap->s, NULL, NULL, NULL));
297 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
301 error = kern_accept(td, uap->s, &name, &namelen, &fp);
304 * return a namelen of zero for older code which might
305 * ignore the return value from accept.
308 (void) copyout(&namelen,
309 uap->anamelen, sizeof(*uap->anamelen));
313 if (error == 0 && name != NULL) {
314 #ifdef COMPAT_OLDSOCK
316 ((struct osockaddr *)name)->sa_family =
319 error = copyout(name, uap->name, namelen);
322 error = copyout(&namelen, uap->anamelen,
325 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
327 free(name, M_SONAME);
332 kern_accept(struct thread *td, int s, struct sockaddr **name,
333 socklen_t *namelen, struct file **fp)
335 struct filedesc *fdp;
336 struct file *headfp, *nfp = NULL;
337 struct sockaddr *sa = NULL;
339 struct socket *head, *so;
352 fdp = td->td_proc->p_fd;
353 error = getsock(fdp, s, &headfp, &fflag);
356 head = headfp->f_data;
357 if ((head->so_options & SO_ACCEPTCONN) == 0) {
362 error = mac_socket_check_accept(td->td_ucred, head);
366 error = falloc(td, &nfp, &fd);
370 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
375 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
376 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
377 head->so_error = ECONNABORTED;
380 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
387 if (head->so_error) {
388 error = head->so_error;
393 so = TAILQ_FIRST(&head->so_comp);
394 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
395 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
398 * Before changing the flags on the socket, we have to bump the
399 * reference count. Otherwise, if the protocol calls sofree(),
400 * the socket will be released due to a zero refcount.
402 SOCK_LOCK(so); /* soref() and so_state update */
403 soref(so); /* file descriptor reference */
405 TAILQ_REMOVE(&head->so_comp, so, so_list);
407 so->so_state |= (head->so_state & SS_NBIO);
408 so->so_qstate &= ~SQ_COMP;
414 /* An extra reference on `nfp' has been held for us by falloc(). */
415 td->td_retval[0] = fd;
417 /* connection has been removed from the listen queue */
418 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
420 pgid = fgetown(&head->so_sigio);
422 fsetown(pgid, &so->so_sigio);
424 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
425 /* Sync socket nonblocking/async state with file flags */
426 tmp = fflag & FNONBLOCK;
427 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
428 tmp = fflag & FASYNC;
429 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
431 CURVNET_SET(so->so_vnet);
432 error = soaccept(so, &sa);
436 * return a namelen of zero for older code which might
437 * ignore the return value from accept.
449 /* check sa_len before it is destroyed */
450 if (*namelen > sa->sa_len)
451 *namelen = sa->sa_len;
453 if (KTRPOINT(td, KTR_STRUCT))
464 * close the new descriptor, assuming someone hasn't ripped it
468 fdclose(fdp, nfp, fd, td);
471 * Release explicitly held references before returning. We return
472 * a reference on nfp to the caller on success if they request it.
491 struct accept_args *uap;
494 return (accept1(td, uap, 0));
497 #ifdef COMPAT_OLDSOCK
501 struct accept_args *uap;
504 return (accept1(td, uap, 1));
506 #endif /* COMPAT_OLDSOCK */
512 struct connect_args /* {
521 error = getsockaddr(&sa, uap->name, uap->namelen);
525 error = kern_connect(td, uap->s, sa);
532 kern_connect(td, fd, sa)
543 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
547 if (so->so_state & SS_ISCONNECTING) {
552 if (KTRPOINT(td, KTR_STRUCT))
556 error = mac_socket_check_connect(td->td_ucred, so, sa);
560 error = soconnect(so, sa, td);
563 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
568 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
569 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
572 if (error == EINTR || error == ERESTART)
578 error = so->so_error;
584 so->so_state &= ~SS_ISCONNECTING;
585 if (error == ERESTART)
593 kern_socketpair(struct thread *td, int domain, int type, int protocol,
596 struct filedesc *fdp = td->td_proc->p_fd;
597 struct file *fp1, *fp2;
598 struct socket *so1, *so2;
601 AUDIT_ARG_SOCKET(domain, type, protocol);
603 /* We might want to have a separate check for socket pairs. */
604 error = mac_socket_check_create(td->td_ucred, domain, type,
609 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
612 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
615 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
616 error = falloc(td, &fp1, &fd);
620 fp1->f_data = so1; /* so1 already has ref count */
621 error = falloc(td, &fp2, &fd);
624 fp2->f_data = so2; /* so2 already has ref count */
626 error = soconnect2(so1, so2);
629 if (type == SOCK_DGRAM) {
631 * Datagram socket connection is asymmetric.
633 error = soconnect2(so2, so1);
637 finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops);
638 finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops);
643 fdclose(fdp, fp2, rsv[1], td);
646 fdclose(fdp, fp1, rsv[0], td);
658 socketpair(struct thread *td, struct socketpair_args *uap)
662 error = kern_socketpair(td, uap->domain, uap->type,
666 error = copyout(sv, uap->rsv, 2 * sizeof(int));
668 (void)kern_close(td, sv[0]);
669 (void)kern_close(td, sv[1]);
675 sendit(td, s, mp, flags)
681 struct mbuf *control;
685 if (mp->msg_name != NULL) {
686 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
696 if (mp->msg_control) {
697 if (mp->msg_controllen < sizeof(struct cmsghdr)
698 #ifdef COMPAT_OLDSOCK
699 && mp->msg_flags != MSG_COMPAT
705 error = sockargs(&control, mp->msg_control,
706 mp->msg_controllen, MT_CONTROL);
709 #ifdef COMPAT_OLDSOCK
710 if (mp->msg_flags == MSG_COMPAT) {
713 M_PREPEND(control, sizeof(*cm), M_WAIT);
714 cm = mtod(control, struct cmsghdr *);
715 cm->cmsg_len = control->m_len;
716 cm->cmsg_level = SOL_SOCKET;
717 cm->cmsg_type = SCM_RIGHTS;
724 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
733 kern_sendit(td, s, mp, flags, control, segflg)
738 struct mbuf *control;
748 struct uio *ktruio = NULL;
752 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
755 so = (struct socket *)fp->f_data;
758 if (mp->msg_name != NULL) {
759 error = mac_socket_check_connect(td->td_ucred, so,
764 error = mac_socket_check_send(td->td_ucred, so);
769 auio.uio_iov = mp->msg_iov;
770 auio.uio_iovcnt = mp->msg_iovlen;
771 auio.uio_segflg = segflg;
772 auio.uio_rw = UIO_WRITE;
774 auio.uio_offset = 0; /* XXX */
777 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
778 if ((auio.uio_resid += iov->iov_len) < 0) {
784 if (KTRPOINT(td, KTR_GENIO))
785 ktruio = cloneuio(&auio);
787 len = auio.uio_resid;
788 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
790 if (auio.uio_resid != len && (error == ERESTART ||
791 error == EINTR || error == EWOULDBLOCK))
793 /* Generation of SIGPIPE can be controlled per socket */
794 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
795 !(flags & MSG_NOSIGNAL)) {
796 PROC_LOCK(td->td_proc);
797 psignal(td->td_proc, SIGPIPE);
798 PROC_UNLOCK(td->td_proc);
802 td->td_retval[0] = len - auio.uio_resid;
804 if (ktruio != NULL) {
805 ktruio->uio_resid = td->td_retval[0];
806 ktrgenio(s, UIO_WRITE, ktruio, error);
817 struct sendto_args /* {
830 msg.msg_name = uap->to;
831 msg.msg_namelen = uap->tolen;
835 #ifdef COMPAT_OLDSOCK
838 aiov.iov_base = uap->buf;
839 aiov.iov_len = uap->len;
840 error = sendit(td, uap->s, &msg, uap->flags);
844 #ifdef COMPAT_OLDSOCK
848 struct osend_args /* {
863 aiov.iov_base = uap->buf;
864 aiov.iov_len = uap->len;
867 error = sendit(td, uap->s, &msg, uap->flags);
874 struct osendmsg_args /* {
884 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
887 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
891 msg.msg_flags = MSG_COMPAT;
892 error = sendit(td, uap->s, &msg, uap->flags);
901 struct sendmsg_args /* {
911 error = copyin(uap->msg, &msg, sizeof (msg));
914 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
918 #ifdef COMPAT_OLDSOCK
921 error = sendit(td, uap->s, &msg, uap->flags);
927 kern_recvit(td, s, mp, fromseg, controlp)
931 enum uio_seg fromseg;
932 struct mbuf **controlp;
939 struct mbuf *m, *control = 0;
943 struct sockaddr *fromsa = 0;
945 struct uio *ktruio = NULL;
952 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
958 error = mac_socket_check_receive(td->td_ucred, so);
965 auio.uio_iov = mp->msg_iov;
966 auio.uio_iovcnt = mp->msg_iovlen;
967 auio.uio_segflg = UIO_USERSPACE;
968 auio.uio_rw = UIO_READ;
970 auio.uio_offset = 0; /* XXX */
973 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
974 if ((auio.uio_resid += iov->iov_len) < 0) {
980 if (KTRPOINT(td, KTR_GENIO))
981 ktruio = cloneuio(&auio);
983 len = auio.uio_resid;
984 CURVNET_SET(so->so_vnet);
985 error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
986 (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
990 if (auio.uio_resid != (int)len && (error == ERESTART ||
991 error == EINTR || error == EWOULDBLOCK))
995 if (ktruio != NULL) {
996 ktruio->uio_resid = (int)len - auio.uio_resid;
997 ktrgenio(s, UIO_READ, ktruio, error);
1002 td->td_retval[0] = (int)len - auio.uio_resid;
1004 len = mp->msg_namelen;
1005 if (len <= 0 || fromsa == 0)
1008 /* save sa_len before it is destroyed by MSG_COMPAT */
1009 len = MIN(len, fromsa->sa_len);
1010 #ifdef COMPAT_OLDSOCK
1011 if (mp->msg_flags & MSG_COMPAT)
1012 ((struct osockaddr *)fromsa)->sa_family =
1015 if (fromseg == UIO_USERSPACE) {
1016 error = copyout(fromsa, mp->msg_name,
1021 bcopy(fromsa, mp->msg_name, len);
1023 mp->msg_namelen = len;
1025 if (mp->msg_control && controlp == NULL) {
1026 #ifdef COMPAT_OLDSOCK
1028 * We assume that old recvmsg calls won't receive access
1029 * rights and other control info, esp. as control info
1030 * is always optional and those options didn't exist in 4.3.
1031 * If we receive rights, trim the cmsghdr; anything else
1034 if (control && mp->msg_flags & MSG_COMPAT) {
1035 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1037 mtod(control, struct cmsghdr *)->cmsg_type !=
1039 mp->msg_controllen = 0;
1042 control->m_len -= sizeof (struct cmsghdr);
1043 control->m_data += sizeof (struct cmsghdr);
1046 len = mp->msg_controllen;
1048 mp->msg_controllen = 0;
1049 ctlbuf = mp->msg_control;
1051 while (m && len > 0) {
1052 unsigned int tocopy;
1054 if (len >= m->m_len)
1057 mp->msg_flags |= MSG_CTRUNC;
1061 if ((error = copyout(mtod(m, caddr_t),
1062 ctlbuf, tocopy)) != 0)
1069 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1074 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1075 ktrsockaddr(fromsa);
1078 free(fromsa, M_SONAME);
1080 if (error == 0 && controlp != NULL)
1081 *controlp = control;
1089 recvit(td, s, mp, namelenp)
1097 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1101 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1102 #ifdef COMPAT_OLDSOCK
1103 if (mp->msg_flags & MSG_COMPAT)
1104 error = 0; /* old recvfrom didn't check */
1113 struct recvfrom_args /* {
1118 struct sockaddr * __restrict from;
1119 socklen_t * __restrict fromlenaddr;
1126 if (uap->fromlenaddr) {
1127 error = copyin(uap->fromlenaddr,
1128 &msg.msg_namelen, sizeof (msg.msg_namelen));
1132 msg.msg_namelen = 0;
1134 msg.msg_name = uap->from;
1135 msg.msg_iov = &aiov;
1137 aiov.iov_base = uap->buf;
1138 aiov.iov_len = uap->len;
1139 msg.msg_control = 0;
1140 msg.msg_flags = uap->flags;
1141 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1146 #ifdef COMPAT_OLDSOCK
1150 struct recvfrom_args *uap;
1153 uap->flags |= MSG_COMPAT;
1154 return (recvfrom(td, uap));
1158 #ifdef COMPAT_OLDSOCK
1162 struct orecv_args /* {
1174 msg.msg_namelen = 0;
1175 msg.msg_iov = &aiov;
1177 aiov.iov_base = uap->buf;
1178 aiov.iov_len = uap->len;
1179 msg.msg_control = 0;
1180 msg.msg_flags = uap->flags;
1181 error = recvit(td, uap->s, &msg, NULL);
1186 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1187 * overlays the new one, missing only the flags, and with the (old) access
1188 * rights where the control fields are now.
1193 struct orecvmsg_args /* {
1195 struct omsghdr *msg;
1203 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1206 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1209 msg.msg_flags = uap->flags | MSG_COMPAT;
1211 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1212 if (msg.msg_controllen && error == 0)
1213 error = copyout(&msg.msg_controllen,
1214 &uap->msg->msg_accrightslen, sizeof (int));
1223 struct recvmsg_args /* {
1230 struct iovec *uiov, *iov;
1233 error = copyin(uap->msg, &msg, sizeof (msg));
1236 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1239 msg.msg_flags = uap->flags;
1240 #ifdef COMPAT_OLDSOCK
1241 msg.msg_flags &= ~MSG_COMPAT;
1245 error = recvit(td, uap->s, &msg, NULL);
1248 error = copyout(&msg, uap->msg, sizeof(msg));
1258 struct shutdown_args /* {
1267 AUDIT_ARG_FD(uap->s);
1268 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
1271 error = soshutdown(so, uap->how);
1281 struct setsockopt_args /* {
1290 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1291 uap->val, UIO_USERSPACE, uap->valsize));
1295 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1301 enum uio_seg valseg;
1307 struct sockopt sopt;
1309 if (val == NULL && valsize != 0)
1311 if ((int)valsize < 0)
1314 sopt.sopt_dir = SOPT_SET;
1315 sopt.sopt_level = level;
1316 sopt.sopt_name = name;
1317 sopt.sopt_val = val;
1318 sopt.sopt_valsize = valsize;
1324 sopt.sopt_td = NULL;
1327 panic("kern_setsockopt called with bad valseg");
1331 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1334 CURVNET_SET(so->so_vnet);
1335 error = sosetopt(so, &sopt);
1346 struct getsockopt_args /* {
1350 void * __restrict val;
1351 socklen_t * __restrict avalsize;
1358 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1363 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1364 uap->val, UIO_USERSPACE, &valsize);
1367 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1372 * Kernel version of getsockopt.
1373 * optval can be a userland or userspace. optlen is always a kernel pointer.
1376 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1382 enum uio_seg valseg;
1388 struct sockopt sopt;
1392 if ((int)*valsize < 0)
1395 sopt.sopt_dir = SOPT_GET;
1396 sopt.sopt_level = level;
1397 sopt.sopt_name = name;
1398 sopt.sopt_val = val;
1399 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1405 sopt.sopt_td = NULL;
1408 panic("kern_getsockopt called with bad valseg");
1412 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1415 CURVNET_SET(so->so_vnet);
1416 error = sogetopt(so, &sopt);
1418 *valsize = sopt.sopt_valsize;
1425 * getsockname1() - Get socket name.
1429 getsockname1(td, uap, compat)
1431 struct getsockname_args /* {
1433 struct sockaddr * __restrict asa;
1434 socklen_t * __restrict alen;
1438 struct sockaddr *sa;
1442 error = copyin(uap->alen, &len, sizeof(len));
1446 error = kern_getsockname(td, uap->fdes, &sa, &len);
1451 #ifdef COMPAT_OLDSOCK
1453 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1455 error = copyout(sa, uap->asa, (u_int)len);
1459 error = copyout(&len, uap->alen, sizeof(len));
1464 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1476 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1481 CURVNET_SET(so->so_vnet);
1482 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1489 len = MIN(*alen, (*sa)->sa_len);
1492 if (KTRPOINT(td, KTR_STRUCT))
1498 free(*sa, M_SONAME);
1505 getsockname(td, uap)
1507 struct getsockname_args *uap;
1510 return (getsockname1(td, uap, 0));
1513 #ifdef COMPAT_OLDSOCK
1515 ogetsockname(td, uap)
1517 struct getsockname_args *uap;
1520 return (getsockname1(td, uap, 1));
1522 #endif /* COMPAT_OLDSOCK */
1525 * getpeername1() - Get name of peer for connected socket.
1529 getpeername1(td, uap, compat)
1531 struct getpeername_args /* {
1533 struct sockaddr * __restrict asa;
1534 socklen_t * __restrict alen;
1538 struct sockaddr *sa;
1542 error = copyin(uap->alen, &len, sizeof (len));
1546 error = kern_getpeername(td, uap->fdes, &sa, &len);
1551 #ifdef COMPAT_OLDSOCK
1553 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1555 error = copyout(sa, uap->asa, (u_int)len);
1559 error = copyout(&len, uap->alen, sizeof(len));
1564 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1576 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1580 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1585 CURVNET_SET(so->so_vnet);
1586 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1593 len = MIN(*alen, (*sa)->sa_len);
1596 if (KTRPOINT(td, KTR_STRUCT))
1601 free(*sa, M_SONAME);
1610 getpeername(td, uap)
1612 struct getpeername_args *uap;
1615 return (getpeername1(td, uap, 0));
1618 #ifdef COMPAT_OLDSOCK
1620 ogetpeername(td, uap)
1622 struct ogetpeername_args *uap;
1625 /* XXX uap should have type `getpeername_args *' to begin with. */
1626 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1628 #endif /* COMPAT_OLDSOCK */
1631 sockargs(mp, buf, buflen, type)
1636 struct sockaddr *sa;
1640 if ((u_int)buflen > MLEN) {
1641 #ifdef COMPAT_OLDSOCK
1642 if (type == MT_SONAME && (u_int)buflen <= 112)
1643 buflen = MLEN; /* unix domain compat. hack */
1646 if ((u_int)buflen > MCLBYTES)
1649 m = m_get(M_WAIT, type);
1650 if ((u_int)buflen > MLEN)
1653 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1658 if (type == MT_SONAME) {
1659 sa = mtod(m, struct sockaddr *);
1661 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1662 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1663 sa->sa_family = sa->sa_len;
1665 sa->sa_len = buflen;
1672 getsockaddr(namp, uaddr, len)
1673 struct sockaddr **namp;
1677 struct sockaddr *sa;
1680 if (len > SOCK_MAXADDRLEN)
1681 return (ENAMETOOLONG);
1682 if (len < offsetof(struct sockaddr, sa_data[0]))
1684 sa = malloc(len, M_SONAME, M_WAITOK);
1685 error = copyin(uaddr, sa, len);
1689 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1690 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1691 sa->sa_family = sa->sa_len;
1699 #include <sys/condvar.h>
1701 struct sendfile_sync {
1708 * Detach mapped page and release resources back to the system.
1711 sf_buf_mext(void *addr, void *args)
1714 struct sendfile_sync *sfs;
1716 m = sf_buf_page(args);
1718 vm_page_lock_queues();
1719 vm_page_unwire(m, 0);
1721 * Check for the object going away on us. This can
1722 * happen since we don't hold a reference to it.
1723 * If so, we're responsible for freeing the page.
1725 if (m->wire_count == 0 && m->object == NULL)
1727 vm_page_unlock_queues();
1731 mtx_lock(&sfs->mtx);
1732 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1733 if (--sfs->count == 0)
1734 cv_signal(&sfs->cv);
1735 mtx_unlock(&sfs->mtx);
1741 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1742 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1744 * Send a file specified by 'fd' and starting at 'offset' to a socket
1745 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1746 * 0. Optionally add a header and/or trailer to the socket output. If
1747 * specified, write the total number of bytes sent into *sbytes.
1750 sendfile(struct thread *td, struct sendfile_args *uap)
1753 return (do_sendfile(td, uap, 0));
1757 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1759 struct sf_hdtr hdtr;
1760 struct uio *hdr_uio, *trl_uio;
1763 hdr_uio = trl_uio = NULL;
1765 if (uap->hdtr != NULL) {
1766 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1769 if (hdtr.headers != NULL) {
1770 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1774 if (hdtr.trailers != NULL) {
1775 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1782 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1785 free(hdr_uio, M_IOV);
1787 free(trl_uio, M_IOV);
1791 #ifdef COMPAT_FREEBSD4
1793 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1795 struct sendfile_args args;
1799 args.offset = uap->offset;
1800 args.nbytes = uap->nbytes;
1801 args.hdtr = uap->hdtr;
1802 args.sbytes = uap->sbytes;
1803 args.flags = uap->flags;
1805 return (do_sendfile(td, &args, 1));
1807 #endif /* COMPAT_FREEBSD4 */
1810 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1811 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1813 struct file *sock_fp;
1815 struct vm_object *obj = NULL;
1816 struct socket *so = NULL;
1817 struct mbuf *m = NULL;
1820 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1821 int error, hdrlen = 0, mnw = 0;
1823 struct sendfile_sync *sfs = NULL;
1826 * The file descriptor must be a regular file and have a
1827 * backing VM object.
1828 * File offset must be positive. If it goes beyond EOF
1829 * we send only the header/trailer and no payload data.
1831 AUDIT_ARG_FD(uap->fd);
1832 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1834 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1835 vn_lock(vp, LK_SHARED | LK_RETRY);
1836 if (vp->v_type == VREG) {
1840 * Temporarily increase the backing VM
1841 * object's reference count so that a forced
1842 * reclamation of its vnode does not
1843 * immediately destroy it.
1845 VM_OBJECT_LOCK(obj);
1846 if ((obj->flags & OBJ_DEAD) == 0) {
1847 vm_object_reference_locked(obj);
1848 VM_OBJECT_UNLOCK(obj);
1850 VM_OBJECT_UNLOCK(obj);
1856 VFS_UNLOCK_GIANT(vfslocked);
1861 if (uap->offset < 0) {
1867 * The socket must be a stream socket and connected.
1868 * Remember if it a blocking or non-blocking socket.
1870 if ((error = getsock(td->td_proc->p_fd, uap->s, &sock_fp,
1873 so = sock_fp->f_data;
1874 if (so->so_type != SOCK_STREAM) {
1878 if ((so->so_state & SS_ISCONNECTED) == 0) {
1883 * Do not wait on memory allocations but return ENOMEM for
1884 * caller to retry later.
1885 * XXX: Experimental.
1887 if (uap->flags & SF_MNOWAIT)
1890 if (uap->flags & SF_SYNC) {
1891 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK);
1892 memset(sfs, 0, sizeof *sfs);
1893 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
1894 cv_init(&sfs->cv, "sendfile");
1898 error = mac_socket_check_send(td->td_ucred, so);
1903 /* If headers are specified copy them into mbufs. */
1904 if (hdr_uio != NULL) {
1905 hdr_uio->uio_td = td;
1906 hdr_uio->uio_rw = UIO_WRITE;
1907 if (hdr_uio->uio_resid > 0) {
1909 * In FBSD < 5.0 the nbytes to send also included
1910 * the header. If compat is specified subtract the
1911 * header size from nbytes.
1914 if (uap->nbytes > hdr_uio->uio_resid)
1915 uap->nbytes -= hdr_uio->uio_resid;
1919 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1922 error = mnw ? EAGAIN : ENOBUFS;
1925 hdrlen = m_length(m, NULL);
1930 * Protect against multiple writers to the socket.
1932 * XXXRW: Historically this has assumed non-interruptibility, so now
1933 * we implement that, but possibly shouldn't.
1935 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1938 * Loop through the pages of the file, starting with the requested
1939 * offset. Get a file page (do I/O if necessary), map the file page
1940 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1942 * This is done in two loops. The inner loop turns as many pages
1943 * as it can, up to available socket buffer space, without blocking
1944 * into mbufs to have it bulk delivered into the socket send buffer.
1945 * The outer loop checks the state and available space of the socket
1946 * and takes care of the overall progress.
1948 for (off = uap->offset, rem = uap->nbytes; ; ) {
1954 * Check the socket state for ongoing connection,
1955 * no errors and space in socket buffer.
1956 * If space is low allow for the remainder of the
1957 * file to be processed if it fits the socket buffer.
1958 * Otherwise block in waiting for sufficient space
1959 * to proceed, or if the socket is nonblocking, return
1960 * to userland with EAGAIN while reporting how far
1962 * We wait until the socket buffer has significant free
1963 * space to do bulk sends. This makes good use of file
1964 * system read ahead and allows packet segmentation
1965 * offloading hardware to take over lots of work. If
1966 * we were not careful here we would send off only one
1969 SOCKBUF_LOCK(&so->so_snd);
1970 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1971 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1973 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1975 SOCKBUF_UNLOCK(&so->so_snd);
1977 } else if (so->so_error) {
1978 error = so->so_error;
1980 SOCKBUF_UNLOCK(&so->so_snd);
1983 space = sbspace(&so->so_snd);
1986 space < so->so_snd.sb_lowat)) {
1987 if (so->so_state & SS_NBIO) {
1988 SOCKBUF_UNLOCK(&so->so_snd);
1993 * sbwait drops the lock while sleeping.
1994 * When we loop back to retry_space the
1995 * state may have changed and we retest
1998 error = sbwait(&so->so_snd);
2000 * An error from sbwait usually indicates that we've
2001 * been interrupted by a signal. If we've sent anything
2002 * then return bytes sent, otherwise return the error.
2005 SOCKBUF_UNLOCK(&so->so_snd);
2010 SOCKBUF_UNLOCK(&so->so_snd);
2013 * Reduce space in the socket buffer by the size of
2014 * the header mbuf chain.
2015 * hdrlen is set to 0 after the first loop.
2020 * Loop and construct maximum sized mbuf chain to be bulk
2021 * dumped into socket buffer.
2023 while(space > loopbytes) {
2028 VM_OBJECT_LOCK(obj);
2030 * Calculate the amount to transfer.
2031 * Not to exceed a page, the EOF,
2032 * or the passed in nbytes.
2034 pgoff = (vm_offset_t)(off & PAGE_MASK);
2035 xfsize = omin(PAGE_SIZE - pgoff,
2036 obj->un_pager.vnp.vnp_size - uap->offset -
2037 fsbytes - loopbytes);
2039 rem = (uap->nbytes - fsbytes - loopbytes);
2041 rem = obj->un_pager.vnp.vnp_size -
2042 uap->offset - fsbytes - loopbytes;
2043 xfsize = omin(rem, xfsize);
2044 xfsize = omin(space - loopbytes, xfsize);
2046 VM_OBJECT_UNLOCK(obj);
2047 done = 1; /* all data sent */
2052 * Attempt to look up the page. Allocate
2053 * if not found or wait and loop if busy.
2055 pindex = OFF_TO_IDX(off);
2056 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2057 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2060 * Check if page is valid for what we need,
2061 * otherwise initiate I/O.
2062 * If we already turned some pages into mbufs,
2063 * send them off before we come here again and
2066 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2067 VM_OBJECT_UNLOCK(obj);
2069 error = EAGAIN; /* send what we already got */
2070 else if (uap->flags & SF_NODISKIO)
2076 * Ensure that our page is still around
2077 * when the I/O completes.
2079 vm_page_io_start(pg);
2080 VM_OBJECT_UNLOCK(obj);
2083 * Get the page from backing store.
2085 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2086 error = vn_lock(vp, LK_SHARED);
2089 bsize = vp->v_mount->mnt_stat.f_iosize;
2092 * XXXMAC: Because we don't have fp->f_cred
2093 * here, we pass in NOCRED. This is probably
2094 * wrong, but is consistent with our original
2097 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2098 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2099 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2100 td->td_ucred, NOCRED, &resid, td);
2103 VFS_UNLOCK_GIANT(vfslocked);
2104 VM_OBJECT_LOCK(obj);
2105 vm_page_io_finish(pg);
2107 VM_OBJECT_UNLOCK(obj);
2111 vm_page_lock_queues();
2112 vm_page_unwire(pg, 0);
2114 * See if anyone else might know about
2115 * this page. If not and it is not valid,
2118 if (pg->wire_count == 0 && pg->valid == 0 &&
2119 pg->busy == 0 && !(pg->oflags & VPO_BUSY) &&
2120 pg->hold_count == 0) {
2123 vm_page_unlock_queues();
2124 VM_OBJECT_UNLOCK(obj);
2125 if (error == EAGAIN)
2126 error = 0; /* not a real error */
2131 * Get a sendfile buf. We usually wait as long
2132 * as necessary, but this wait can be interrupted.
2134 if ((sf = sf_buf_alloc(pg,
2135 (mnw ? SFB_NOWAIT : SFB_CATCH))) == NULL) {
2136 mbstat.sf_allocfail++;
2137 vm_page_lock_queues();
2138 vm_page_unwire(pg, 0);
2140 * XXX: Not same check as above!?
2142 if (pg->wire_count == 0 && pg->object == NULL)
2144 vm_page_unlock_queues();
2145 error = (mnw ? EAGAIN : EINTR);
2150 * Get an mbuf and set it up as having
2153 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2155 error = (mnw ? EAGAIN : ENOBUFS);
2156 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2159 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2160 sfs, sf, M_RDONLY, EXT_SFBUF);
2161 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2164 /* Append to mbuf chain. */
2170 /* Keep track of bits processed. */
2171 loopbytes += xfsize;
2175 mtx_lock(&sfs->mtx);
2177 mtx_unlock(&sfs->mtx);
2181 /* Add the buffer chain to the socket buffer. */
2185 mlen = m_length(m, NULL);
2186 SOCKBUF_LOCK(&so->so_snd);
2187 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2189 SOCKBUF_UNLOCK(&so->so_snd);
2192 SOCKBUF_UNLOCK(&so->so_snd);
2193 CURVNET_SET(so->so_vnet);
2194 /* Avoid error aliasing. */
2195 err = (*so->so_proto->pr_usrreqs->pru_send)
2196 (so, 0, m, NULL, NULL, td);
2200 * We need two counters to get the
2201 * file offset and nbytes to send
2203 * - sbytes contains the total amount
2204 * of bytes sent, including headers.
2205 * - fsbytes contains the total amount
2206 * of bytes sent from the file.
2214 } else if (error == 0)
2216 m = NULL; /* pru_send always consumes */
2219 /* Quit outer loop on error or when we're done. */
2227 * Send trailers. Wimp out and use writev(2).
2229 if (trl_uio != NULL) {
2230 sbunlock(&so->so_snd);
2231 error = kern_writev(td, uap->s, trl_uio);
2233 sbytes += td->td_retval[0];
2238 sbunlock(&so->so_snd);
2241 * If there was no error we have to clear td->td_retval[0]
2242 * because it may have been set by writev.
2245 td->td_retval[0] = 0;
2247 if (uap->sbytes != NULL) {
2248 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2251 vm_object_deallocate(obj);
2253 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2255 VFS_UNLOCK_GIANT(vfslocked);
2263 mtx_lock(&sfs->mtx);
2264 if (sfs->count != 0)
2265 cv_wait(&sfs->cv, &sfs->mtx);
2266 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2267 cv_destroy(&sfs->cv);
2268 mtx_destroy(&sfs->mtx);
2272 if (error == ERESTART)
2280 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2281 * otherwise all return EOPNOTSUPP.
2282 * XXX: We should make this loadable one day.
2285 sctp_peeloff(td, uap)
2287 struct sctp_peeloff_args /* {
2292 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2293 struct filedesc *fdp;
2294 struct file *nfp = NULL;
2296 struct socket *head, *so;
2300 fdp = td->td_proc->p_fd;
2301 AUDIT_ARG_FD(uap->sd);
2302 error = fgetsock(td, uap->sd, &head, &fflag);
2305 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2309 * At this point we know we do have a assoc to pull
2310 * we proceed to get the fd setup. This may block
2314 error = falloc(td, &nfp, &fd);
2317 td->td_retval[0] = fd;
2319 CURVNET_SET(head->so_vnet);
2320 so = sonewconn(head, SS_ISCONNECTED);
2324 * Before changing the flags on the socket, we have to bump the
2325 * reference count. Otherwise, if the protocol calls sofree(),
2326 * the socket will be released due to a zero refcount.
2329 soref(so); /* file descriptor reference */
2334 TAILQ_REMOVE(&head->so_comp, so, so_list);
2336 so->so_state |= (head->so_state & SS_NBIO);
2337 so->so_state &= ~SS_NOFDREF;
2338 so->so_qstate &= ~SQ_COMP;
2341 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2342 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2345 if (head->so_sigio != NULL)
2346 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2350 * close the new descriptor, assuming someone hasn't ripped it
2351 * out from under us.
2354 fdclose(fdp, nfp, fd, td);
2357 * Release explicitly held references before returning.
2367 return (EOPNOTSUPP);
2372 sctp_generic_sendmsg (td, uap)
2374 struct sctp_generic_sendmsg_args /* {
2380 struct sctp_sndrcvinfo *sinfo,
2384 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2385 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2387 struct file *fp = NULL;
2388 int use_rcvinfo = 1;
2390 struct sockaddr *to = NULL;
2392 struct uio *ktruio = NULL;
2395 struct iovec iov[1];
2398 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2404 error = getsockaddr(&to, uap->to, uap->tolen);
2411 AUDIT_ARG_FD(uap->sd);
2412 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2416 if (KTRPOINT(td, KTR_STRUCT))
2420 iov[0].iov_base = uap->msg;
2421 iov[0].iov_len = uap->mlen;
2423 so = (struct socket *)fp->f_data;
2425 error = mac_socket_check_send(td->td_ucred, so);
2431 auio.uio_iovcnt = 1;
2432 auio.uio_segflg = UIO_USERSPACE;
2433 auio.uio_rw = UIO_WRITE;
2435 auio.uio_offset = 0; /* XXX */
2437 len = auio.uio_resid = uap->mlen;
2438 CURVNET_SET(so->so_vnet);
2439 error = sctp_lower_sosend(so, to, &auio,
2440 (struct mbuf *)NULL, (struct mbuf *)NULL,
2441 uap->flags, use_rcvinfo, u_sinfo, td);
2444 if (auio.uio_resid != len && (error == ERESTART ||
2445 error == EINTR || error == EWOULDBLOCK))
2447 /* Generation of SIGPIPE can be controlled per socket. */
2448 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2449 !(uap->flags & MSG_NOSIGNAL)) {
2450 PROC_LOCK(td->td_proc);
2451 psignal(td->td_proc, SIGPIPE);
2452 PROC_UNLOCK(td->td_proc);
2456 td->td_retval[0] = len - auio.uio_resid;
2458 if (ktruio != NULL) {
2459 ktruio->uio_resid = td->td_retval[0];
2460 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2471 return (EOPNOTSUPP);
2476 sctp_generic_sendmsg_iov(td, uap)
2478 struct sctp_generic_sendmsg_iov_args /* {
2484 struct sctp_sndrcvinfo *sinfo,
2488 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2489 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2491 struct file *fp = NULL;
2492 int use_rcvinfo = 1;
2493 int error=0, len, i;
2494 struct sockaddr *to = NULL;
2496 struct uio *ktruio = NULL;
2499 struct iovec *iov, *tiov;
2502 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2508 error = getsockaddr(&to, uap->to, uap->tolen);
2515 AUDIT_ARG_FD(uap->sd);
2516 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2520 #ifdef COMPAT_FREEBSD32
2521 if (SV_CURPROC_FLAG(SV_ILP32))
2522 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2523 uap->iovlen, &iov, EMSGSIZE);
2526 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2530 if (KTRPOINT(td, KTR_STRUCT))
2534 so = (struct socket *)fp->f_data;
2536 error = mac_socket_check_send(td->td_ucred, so);
2542 auio.uio_iovcnt = uap->iovlen;
2543 auio.uio_segflg = UIO_USERSPACE;
2544 auio.uio_rw = UIO_WRITE;
2546 auio.uio_offset = 0; /* XXX */
2549 for (i = 0; i <uap->iovlen; i++, tiov++) {
2550 if ((auio.uio_resid += tiov->iov_len) < 0) {
2555 len = auio.uio_resid;
2556 CURVNET_SET(so->so_vnet);
2557 error = sctp_lower_sosend(so, to, &auio,
2558 (struct mbuf *)NULL, (struct mbuf *)NULL,
2559 uap->flags, use_rcvinfo, u_sinfo, td);
2562 if (auio.uio_resid != len && (error == ERESTART ||
2563 error == EINTR || error == EWOULDBLOCK))
2565 /* Generation of SIGPIPE can be controlled per socket */
2566 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2567 !(uap->flags & MSG_NOSIGNAL)) {
2568 PROC_LOCK(td->td_proc);
2569 psignal(td->td_proc, SIGPIPE);
2570 PROC_UNLOCK(td->td_proc);
2574 td->td_retval[0] = len - auio.uio_resid;
2576 if (ktruio != NULL) {
2577 ktruio->uio_resid = td->td_retval[0];
2578 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2591 return (EOPNOTSUPP);
2596 sctp_generic_recvmsg(td, uap)
2598 struct sctp_generic_recvmsg_args /* {
2602 struct sockaddr *from,
2603 __socklen_t *fromlenaddr,
2604 struct sctp_sndrcvinfo *sinfo,
2608 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2609 u_int8_t sockbufstore[256];
2611 struct iovec *iov, *tiov;
2612 struct sctp_sndrcvinfo sinfo;
2614 struct file *fp = NULL;
2615 struct sockaddr *fromsa;
2617 int len, i, msg_flags;
2620 struct uio *ktruio = NULL;
2623 AUDIT_ARG_FD(uap->sd);
2624 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2628 #ifdef COMPAT_FREEBSD32
2629 if (SV_CURPROC_FLAG(SV_ILP32))
2630 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2631 uap->iovlen, &iov, EMSGSIZE);
2634 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2640 error = mac_socket_check_receive(td->td_ucred, so);
2646 if (uap->fromlenaddr) {
2647 error = copyin(uap->fromlenaddr,
2648 &fromlen, sizeof (fromlen));
2655 if (uap->msg_flags) {
2656 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2664 auio.uio_iovcnt = uap->iovlen;
2665 auio.uio_segflg = UIO_USERSPACE;
2666 auio.uio_rw = UIO_READ;
2668 auio.uio_offset = 0; /* XXX */
2671 for (i = 0; i <uap->iovlen; i++, tiov++) {
2672 if ((auio.uio_resid += tiov->iov_len) < 0) {
2677 len = auio.uio_resid;
2678 fromsa = (struct sockaddr *)sockbufstore;
2681 if (KTRPOINT(td, KTR_GENIO))
2682 ktruio = cloneuio(&auio);
2684 CURVNET_SET(so->so_vnet);
2685 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2686 fromsa, fromlen, &msg_flags,
2687 (struct sctp_sndrcvinfo *)&sinfo, 1);
2690 if (auio.uio_resid != (int)len && (error == ERESTART ||
2691 error == EINTR || error == EWOULDBLOCK))
2695 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2698 if (ktruio != NULL) {
2699 ktruio->uio_resid = (int)len - auio.uio_resid;
2700 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2705 td->td_retval[0] = (int)len - auio.uio_resid;
2707 if (fromlen && uap->from) {
2709 if (len <= 0 || fromsa == 0)
2712 len = MIN(len, fromsa->sa_len);
2713 error = copyout(fromsa, uap->from, (unsigned)len);
2717 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2723 if (KTRPOINT(td, KTR_STRUCT))
2724 ktrsockaddr(fromsa);
2726 if (uap->msg_flags) {
2727 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2740 return (EOPNOTSUPP);