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 error = solisten(so, uap->backlog, td);
275 accept1(td, uap, compat)
277 struct accept_args /* {
279 struct sockaddr * __restrict name;
280 socklen_t * __restrict anamelen;
284 struct sockaddr *name;
289 if (uap->name == NULL)
290 return (kern_accept(td, uap->s, NULL, NULL, NULL));
292 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
296 error = kern_accept(td, uap->s, &name, &namelen, &fp);
299 * return a namelen of zero for older code which might
300 * ignore the return value from accept.
303 (void) copyout(&namelen,
304 uap->anamelen, sizeof(*uap->anamelen));
308 if (error == 0 && name != NULL) {
309 #ifdef COMPAT_OLDSOCK
311 ((struct osockaddr *)name)->sa_family =
314 error = copyout(name, uap->name, namelen);
317 error = copyout(&namelen, uap->anamelen,
320 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
322 free(name, M_SONAME);
327 kern_accept(struct thread *td, int s, struct sockaddr **name,
328 socklen_t *namelen, struct file **fp)
330 struct filedesc *fdp;
331 struct file *headfp, *nfp = NULL;
332 struct sockaddr *sa = NULL;
334 struct socket *head, *so;
347 fdp = td->td_proc->p_fd;
348 error = getsock(fdp, s, &headfp, &fflag);
351 head = headfp->f_data;
352 if ((head->so_options & SO_ACCEPTCONN) == 0) {
357 error = mac_socket_check_accept(td->td_ucred, head);
361 error = falloc(td, &nfp, &fd);
365 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
370 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
371 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
372 head->so_error = ECONNABORTED;
375 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
382 if (head->so_error) {
383 error = head->so_error;
388 so = TAILQ_FIRST(&head->so_comp);
389 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
390 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
393 * Before changing the flags on the socket, we have to bump the
394 * reference count. Otherwise, if the protocol calls sofree(),
395 * the socket will be released due to a zero refcount.
397 SOCK_LOCK(so); /* soref() and so_state update */
398 soref(so); /* file descriptor reference */
400 TAILQ_REMOVE(&head->so_comp, so, so_list);
402 so->so_state |= (head->so_state & SS_NBIO);
403 so->so_qstate &= ~SQ_COMP;
409 /* An extra reference on `nfp' has been held for us by falloc(). */
410 td->td_retval[0] = fd;
412 /* connection has been removed from the listen queue */
413 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
415 pgid = fgetown(&head->so_sigio);
417 fsetown(pgid, &so->so_sigio);
419 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
420 /* Sync socket nonblocking/async state with file flags */
421 tmp = fflag & FNONBLOCK;
422 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
423 tmp = fflag & FASYNC;
424 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
426 error = soaccept(so, &sa);
429 * return a namelen of zero for older code which might
430 * ignore the return value from accept.
442 /* check sa_len before it is destroyed */
443 if (*namelen > sa->sa_len)
444 *namelen = sa->sa_len;
446 if (KTRPOINT(td, KTR_STRUCT))
457 * close the new descriptor, assuming someone hasn't ripped it
461 fdclose(fdp, nfp, fd, td);
464 * Release explicitly held references before returning. We return
465 * a reference on nfp to the caller on success if they request it.
484 struct accept_args *uap;
487 return (accept1(td, uap, 0));
490 #ifdef COMPAT_OLDSOCK
494 struct accept_args *uap;
497 return (accept1(td, uap, 1));
499 #endif /* COMPAT_OLDSOCK */
505 struct connect_args /* {
514 error = getsockaddr(&sa, uap->name, uap->namelen);
518 error = kern_connect(td, uap->s, sa);
525 kern_connect(td, fd, sa)
536 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
540 if (so->so_state & SS_ISCONNECTING) {
545 if (KTRPOINT(td, KTR_STRUCT))
549 error = mac_socket_check_connect(td->td_ucred, so, sa);
553 error = soconnect(so, sa, td);
556 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
561 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
562 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
565 if (error == EINTR || error == ERESTART)
571 error = so->so_error;
577 so->so_state &= ~SS_ISCONNECTING;
578 if (error == ERESTART)
586 kern_socketpair(struct thread *td, int domain, int type, int protocol,
589 struct filedesc *fdp = td->td_proc->p_fd;
590 struct file *fp1, *fp2;
591 struct socket *so1, *so2;
594 AUDIT_ARG_SOCKET(domain, type, protocol);
596 /* We might want to have a separate check for socket pairs. */
597 error = mac_socket_check_create(td->td_ucred, domain, type,
602 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
605 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
608 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
609 error = falloc(td, &fp1, &fd);
613 fp1->f_data = so1; /* so1 already has ref count */
614 error = falloc(td, &fp2, &fd);
617 fp2->f_data = so2; /* so2 already has ref count */
619 error = soconnect2(so1, so2);
622 if (type == SOCK_DGRAM) {
624 * Datagram socket connection is asymmetric.
626 error = soconnect2(so2, so1);
630 finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops);
631 finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops);
636 fdclose(fdp, fp2, rsv[1], td);
639 fdclose(fdp, fp1, rsv[0], td);
651 socketpair(struct thread *td, struct socketpair_args *uap)
655 error = kern_socketpair(td, uap->domain, uap->type,
659 error = copyout(sv, uap->rsv, 2 * sizeof(int));
661 (void)kern_close(td, sv[0]);
662 (void)kern_close(td, sv[1]);
668 sendit(td, s, mp, flags)
674 struct mbuf *control;
678 if (mp->msg_name != NULL) {
679 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
689 if (mp->msg_control) {
690 if (mp->msg_controllen < sizeof(struct cmsghdr)
691 #ifdef COMPAT_OLDSOCK
692 && mp->msg_flags != MSG_COMPAT
698 error = sockargs(&control, mp->msg_control,
699 mp->msg_controllen, MT_CONTROL);
702 #ifdef COMPAT_OLDSOCK
703 if (mp->msg_flags == MSG_COMPAT) {
706 M_PREPEND(control, sizeof(*cm), M_WAIT);
707 cm = mtod(control, struct cmsghdr *);
708 cm->cmsg_len = control->m_len;
709 cm->cmsg_level = SOL_SOCKET;
710 cm->cmsg_type = SCM_RIGHTS;
717 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
726 kern_sendit(td, s, mp, flags, control, segflg)
731 struct mbuf *control;
741 struct uio *ktruio = NULL;
745 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
748 so = (struct socket *)fp->f_data;
751 if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
752 ktrsockaddr(mp->msg_name);
755 if (mp->msg_name != NULL) {
756 error = mac_socket_check_connect(td->td_ucred, so,
761 error = mac_socket_check_send(td->td_ucred, so);
766 auio.uio_iov = mp->msg_iov;
767 auio.uio_iovcnt = mp->msg_iovlen;
768 auio.uio_segflg = segflg;
769 auio.uio_rw = UIO_WRITE;
771 auio.uio_offset = 0; /* XXX */
774 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
775 if ((auio.uio_resid += iov->iov_len) < 0) {
781 if (KTRPOINT(td, KTR_GENIO))
782 ktruio = cloneuio(&auio);
784 len = auio.uio_resid;
785 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
787 if (auio.uio_resid != len && (error == ERESTART ||
788 error == EINTR || error == EWOULDBLOCK))
790 /* Generation of SIGPIPE can be controlled per socket */
791 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
792 !(flags & MSG_NOSIGNAL)) {
793 PROC_LOCK(td->td_proc);
794 tdksignal(td, SIGPIPE, NULL);
795 PROC_UNLOCK(td->td_proc);
799 td->td_retval[0] = len - auio.uio_resid;
801 if (ktruio != NULL) {
802 ktruio->uio_resid = td->td_retval[0];
803 ktrgenio(s, UIO_WRITE, ktruio, error);
814 struct sendto_args /* {
827 msg.msg_name = uap->to;
828 msg.msg_namelen = uap->tolen;
832 #ifdef COMPAT_OLDSOCK
835 aiov.iov_base = uap->buf;
836 aiov.iov_len = uap->len;
837 error = sendit(td, uap->s, &msg, uap->flags);
841 #ifdef COMPAT_OLDSOCK
845 struct osend_args /* {
860 aiov.iov_base = uap->buf;
861 aiov.iov_len = uap->len;
864 error = sendit(td, uap->s, &msg, uap->flags);
871 struct osendmsg_args /* {
881 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
884 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
888 msg.msg_flags = MSG_COMPAT;
889 error = sendit(td, uap->s, &msg, uap->flags);
898 struct sendmsg_args /* {
908 error = copyin(uap->msg, &msg, sizeof (msg));
911 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
915 #ifdef COMPAT_OLDSOCK
918 error = sendit(td, uap->s, &msg, uap->flags);
924 kern_recvit(td, s, mp, fromseg, controlp)
928 enum uio_seg fromseg;
929 struct mbuf **controlp;
936 struct mbuf *m, *control = 0;
940 struct sockaddr *fromsa = 0;
942 struct uio *ktruio = NULL;
949 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
955 error = mac_socket_check_receive(td->td_ucred, so);
962 auio.uio_iov = mp->msg_iov;
963 auio.uio_iovcnt = mp->msg_iovlen;
964 auio.uio_segflg = UIO_USERSPACE;
965 auio.uio_rw = UIO_READ;
967 auio.uio_offset = 0; /* XXX */
970 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
971 if ((auio.uio_resid += iov->iov_len) < 0) {
977 if (KTRPOINT(td, KTR_GENIO))
978 ktruio = cloneuio(&auio);
980 len = auio.uio_resid;
981 error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
982 (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
985 if (auio.uio_resid != (int)len && (error == ERESTART ||
986 error == EINTR || error == EWOULDBLOCK))
990 if (ktruio != NULL) {
991 ktruio->uio_resid = (int)len - auio.uio_resid;
992 ktrgenio(s, UIO_READ, ktruio, error);
997 td->td_retval[0] = (int)len - auio.uio_resid;
999 len = mp->msg_namelen;
1000 if (len <= 0 || fromsa == 0)
1003 /* save sa_len before it is destroyed by MSG_COMPAT */
1004 len = MIN(len, fromsa->sa_len);
1005 #ifdef COMPAT_OLDSOCK
1006 if (mp->msg_flags & MSG_COMPAT)
1007 ((struct osockaddr *)fromsa)->sa_family =
1010 if (fromseg == UIO_USERSPACE) {
1011 error = copyout(fromsa, mp->msg_name,
1016 bcopy(fromsa, mp->msg_name, len);
1018 mp->msg_namelen = len;
1020 if (mp->msg_control && controlp == NULL) {
1021 #ifdef COMPAT_OLDSOCK
1023 * We assume that old recvmsg calls won't receive access
1024 * rights and other control info, esp. as control info
1025 * is always optional and those options didn't exist in 4.3.
1026 * If we receive rights, trim the cmsghdr; anything else
1029 if (control && mp->msg_flags & MSG_COMPAT) {
1030 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1032 mtod(control, struct cmsghdr *)->cmsg_type !=
1034 mp->msg_controllen = 0;
1037 control->m_len -= sizeof (struct cmsghdr);
1038 control->m_data += sizeof (struct cmsghdr);
1041 len = mp->msg_controllen;
1043 mp->msg_controllen = 0;
1044 ctlbuf = mp->msg_control;
1046 while (m && len > 0) {
1047 unsigned int tocopy;
1049 if (len >= m->m_len)
1052 mp->msg_flags |= MSG_CTRUNC;
1056 if ((error = copyout(mtod(m, caddr_t),
1057 ctlbuf, tocopy)) != 0)
1064 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1069 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1070 ktrsockaddr(fromsa);
1073 free(fromsa, M_SONAME);
1075 if (error == 0 && controlp != NULL)
1076 *controlp = control;
1084 recvit(td, s, mp, namelenp)
1092 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1096 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1097 #ifdef COMPAT_OLDSOCK
1098 if (mp->msg_flags & MSG_COMPAT)
1099 error = 0; /* old recvfrom didn't check */
1108 struct recvfrom_args /* {
1113 struct sockaddr * __restrict from;
1114 socklen_t * __restrict fromlenaddr;
1121 if (uap->fromlenaddr) {
1122 error = copyin(uap->fromlenaddr,
1123 &msg.msg_namelen, sizeof (msg.msg_namelen));
1127 msg.msg_namelen = 0;
1129 msg.msg_name = uap->from;
1130 msg.msg_iov = &aiov;
1132 aiov.iov_base = uap->buf;
1133 aiov.iov_len = uap->len;
1134 msg.msg_control = 0;
1135 msg.msg_flags = uap->flags;
1136 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1141 #ifdef COMPAT_OLDSOCK
1145 struct recvfrom_args *uap;
1148 uap->flags |= MSG_COMPAT;
1149 return (recvfrom(td, uap));
1153 #ifdef COMPAT_OLDSOCK
1157 struct orecv_args /* {
1169 msg.msg_namelen = 0;
1170 msg.msg_iov = &aiov;
1172 aiov.iov_base = uap->buf;
1173 aiov.iov_len = uap->len;
1174 msg.msg_control = 0;
1175 msg.msg_flags = uap->flags;
1176 error = recvit(td, uap->s, &msg, NULL);
1181 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1182 * overlays the new one, missing only the flags, and with the (old) access
1183 * rights where the control fields are now.
1188 struct orecvmsg_args /* {
1190 struct omsghdr *msg;
1198 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1201 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1204 msg.msg_flags = uap->flags | MSG_COMPAT;
1206 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1207 if (msg.msg_controllen && error == 0)
1208 error = copyout(&msg.msg_controllen,
1209 &uap->msg->msg_accrightslen, sizeof (int));
1218 struct recvmsg_args /* {
1225 struct iovec *uiov, *iov;
1228 error = copyin(uap->msg, &msg, sizeof (msg));
1231 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1234 msg.msg_flags = uap->flags;
1235 #ifdef COMPAT_OLDSOCK
1236 msg.msg_flags &= ~MSG_COMPAT;
1240 error = recvit(td, uap->s, &msg, NULL);
1243 error = copyout(&msg, uap->msg, sizeof(msg));
1253 struct shutdown_args /* {
1262 AUDIT_ARG_FD(uap->s);
1263 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
1266 error = soshutdown(so, uap->how);
1276 struct setsockopt_args /* {
1285 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1286 uap->val, UIO_USERSPACE, uap->valsize));
1290 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1296 enum uio_seg valseg;
1302 struct sockopt sopt;
1304 if (val == NULL && valsize != 0)
1306 if ((int)valsize < 0)
1309 sopt.sopt_dir = SOPT_SET;
1310 sopt.sopt_level = level;
1311 sopt.sopt_name = name;
1312 sopt.sopt_val = val;
1313 sopt.sopt_valsize = valsize;
1319 sopt.sopt_td = NULL;
1322 panic("kern_setsockopt called with bad valseg");
1326 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1329 error = sosetopt(so, &sopt);
1339 struct getsockopt_args /* {
1343 void * __restrict val;
1344 socklen_t * __restrict avalsize;
1351 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1356 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1357 uap->val, UIO_USERSPACE, &valsize);
1360 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1365 * Kernel version of getsockopt.
1366 * optval can be a userland or userspace. optlen is always a kernel pointer.
1369 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1375 enum uio_seg valseg;
1381 struct sockopt sopt;
1385 if ((int)*valsize < 0)
1388 sopt.sopt_dir = SOPT_GET;
1389 sopt.sopt_level = level;
1390 sopt.sopt_name = name;
1391 sopt.sopt_val = val;
1392 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1398 sopt.sopt_td = NULL;
1401 panic("kern_getsockopt called with bad valseg");
1405 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1408 error = sogetopt(so, &sopt);
1409 *valsize = sopt.sopt_valsize;
1416 * getsockname1() - Get socket name.
1420 getsockname1(td, uap, compat)
1422 struct getsockname_args /* {
1424 struct sockaddr * __restrict asa;
1425 socklen_t * __restrict alen;
1429 struct sockaddr *sa;
1433 error = copyin(uap->alen, &len, sizeof(len));
1437 error = kern_getsockname(td, uap->fdes, &sa, &len);
1442 #ifdef COMPAT_OLDSOCK
1444 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1446 error = copyout(sa, uap->asa, (u_int)len);
1450 error = copyout(&len, uap->alen, sizeof(len));
1455 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1467 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1472 CURVNET_SET(so->so_vnet);
1473 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1480 len = MIN(*alen, (*sa)->sa_len);
1483 if (KTRPOINT(td, KTR_STRUCT))
1489 free(*sa, M_SONAME);
1496 getsockname(td, uap)
1498 struct getsockname_args *uap;
1501 return (getsockname1(td, uap, 0));
1504 #ifdef COMPAT_OLDSOCK
1506 ogetsockname(td, uap)
1508 struct getsockname_args *uap;
1511 return (getsockname1(td, uap, 1));
1513 #endif /* COMPAT_OLDSOCK */
1516 * getpeername1() - Get name of peer for connected socket.
1520 getpeername1(td, uap, compat)
1522 struct getpeername_args /* {
1524 struct sockaddr * __restrict asa;
1525 socklen_t * __restrict alen;
1529 struct sockaddr *sa;
1533 error = copyin(uap->alen, &len, sizeof (len));
1537 error = kern_getpeername(td, uap->fdes, &sa, &len);
1542 #ifdef COMPAT_OLDSOCK
1544 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1546 error = copyout(sa, uap->asa, (u_int)len);
1550 error = copyout(&len, uap->alen, sizeof(len));
1555 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1567 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1571 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1576 CURVNET_SET(so->so_vnet);
1577 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1584 len = MIN(*alen, (*sa)->sa_len);
1587 if (KTRPOINT(td, KTR_STRUCT))
1592 free(*sa, M_SONAME);
1601 getpeername(td, uap)
1603 struct getpeername_args *uap;
1606 return (getpeername1(td, uap, 0));
1609 #ifdef COMPAT_OLDSOCK
1611 ogetpeername(td, uap)
1613 struct ogetpeername_args *uap;
1616 /* XXX uap should have type `getpeername_args *' to begin with. */
1617 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1619 #endif /* COMPAT_OLDSOCK */
1622 sockargs(mp, buf, buflen, type)
1627 struct sockaddr *sa;
1631 if ((u_int)buflen > MLEN) {
1632 #ifdef COMPAT_OLDSOCK
1633 if (type == MT_SONAME && (u_int)buflen <= 112)
1634 buflen = MLEN; /* unix domain compat. hack */
1637 if ((u_int)buflen > MCLBYTES)
1640 m = m_get(M_WAIT, type);
1641 if ((u_int)buflen > MLEN)
1644 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1649 if (type == MT_SONAME) {
1650 sa = mtod(m, struct sockaddr *);
1652 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1653 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1654 sa->sa_family = sa->sa_len;
1656 sa->sa_len = buflen;
1663 getsockaddr(namp, uaddr, len)
1664 struct sockaddr **namp;
1668 struct sockaddr *sa;
1671 if (len > SOCK_MAXADDRLEN)
1672 return (ENAMETOOLONG);
1673 if (len < offsetof(struct sockaddr, sa_data[0]))
1675 sa = malloc(len, M_SONAME, M_WAITOK);
1676 error = copyin(uaddr, sa, len);
1680 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1681 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1682 sa->sa_family = sa->sa_len;
1690 #include <sys/condvar.h>
1692 struct sendfile_sync {
1699 * Detach mapped page and release resources back to the system.
1702 sf_buf_mext(void *addr, void *args)
1705 struct sendfile_sync *sfs;
1707 m = sf_buf_page(args);
1709 vm_page_lock_queues();
1710 vm_page_unwire(m, 0);
1712 * Check for the object going away on us. This can
1713 * happen since we don't hold a reference to it.
1714 * If so, we're responsible for freeing the page.
1716 if (m->wire_count == 0 && m->object == NULL)
1718 vm_page_unlock_queues();
1722 mtx_lock(&sfs->mtx);
1723 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1724 if (--sfs->count == 0)
1725 cv_signal(&sfs->cv);
1726 mtx_unlock(&sfs->mtx);
1732 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1733 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1735 * Send a file specified by 'fd' and starting at 'offset' to a socket
1736 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1737 * 0. Optionally add a header and/or trailer to the socket output. If
1738 * specified, write the total number of bytes sent into *sbytes.
1741 sendfile(struct thread *td, struct sendfile_args *uap)
1744 return (do_sendfile(td, uap, 0));
1748 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1750 struct sf_hdtr hdtr;
1751 struct uio *hdr_uio, *trl_uio;
1754 hdr_uio = trl_uio = NULL;
1756 if (uap->hdtr != NULL) {
1757 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1760 if (hdtr.headers != NULL) {
1761 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1765 if (hdtr.trailers != NULL) {
1766 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1773 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1776 free(hdr_uio, M_IOV);
1778 free(trl_uio, M_IOV);
1782 #ifdef COMPAT_FREEBSD4
1784 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1786 struct sendfile_args args;
1790 args.offset = uap->offset;
1791 args.nbytes = uap->nbytes;
1792 args.hdtr = uap->hdtr;
1793 args.sbytes = uap->sbytes;
1794 args.flags = uap->flags;
1796 return (do_sendfile(td, &args, 1));
1798 #endif /* COMPAT_FREEBSD4 */
1801 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1802 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1804 struct file *sock_fp;
1806 struct vm_object *obj = NULL;
1807 struct socket *so = NULL;
1808 struct mbuf *m = NULL;
1811 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1812 int error, hdrlen = 0, mnw = 0;
1814 struct sendfile_sync *sfs = NULL;
1817 * The file descriptor must be a regular file and have a
1818 * backing VM object.
1819 * File offset must be positive. If it goes beyond EOF
1820 * we send only the header/trailer and no payload data.
1822 AUDIT_ARG_FD(uap->fd);
1823 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1825 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1826 vn_lock(vp, LK_SHARED | LK_RETRY);
1827 if (vp->v_type == VREG) {
1831 * Temporarily increase the backing VM
1832 * object's reference count so that a forced
1833 * reclamation of its vnode does not
1834 * immediately destroy it.
1836 VM_OBJECT_LOCK(obj);
1837 if ((obj->flags & OBJ_DEAD) == 0) {
1838 vm_object_reference_locked(obj);
1839 VM_OBJECT_UNLOCK(obj);
1841 VM_OBJECT_UNLOCK(obj);
1847 VFS_UNLOCK_GIANT(vfslocked);
1852 if (uap->offset < 0) {
1858 * The socket must be a stream socket and connected.
1859 * Remember if it a blocking or non-blocking socket.
1861 if ((error = getsock(td->td_proc->p_fd, uap->s, &sock_fp,
1864 so = sock_fp->f_data;
1865 if (so->so_type != SOCK_STREAM) {
1869 if ((so->so_state & SS_ISCONNECTED) == 0) {
1874 * Do not wait on memory allocations but return ENOMEM for
1875 * caller to retry later.
1876 * XXX: Experimental.
1878 if (uap->flags & SF_MNOWAIT)
1881 if (uap->flags & SF_SYNC) {
1882 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK);
1883 memset(sfs, 0, sizeof *sfs);
1884 mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
1885 cv_init(&sfs->cv, "sendfile");
1889 error = mac_socket_check_send(td->td_ucred, so);
1894 /* If headers are specified copy them into mbufs. */
1895 if (hdr_uio != NULL) {
1896 hdr_uio->uio_td = td;
1897 hdr_uio->uio_rw = UIO_WRITE;
1898 if (hdr_uio->uio_resid > 0) {
1900 * In FBSD < 5.0 the nbytes to send also included
1901 * the header. If compat is specified subtract the
1902 * header size from nbytes.
1905 if (uap->nbytes > hdr_uio->uio_resid)
1906 uap->nbytes -= hdr_uio->uio_resid;
1910 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1913 error = mnw ? EAGAIN : ENOBUFS;
1916 hdrlen = m_length(m, NULL);
1921 * Protect against multiple writers to the socket.
1923 * XXXRW: Historically this has assumed non-interruptibility, so now
1924 * we implement that, but possibly shouldn't.
1926 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1929 * Loop through the pages of the file, starting with the requested
1930 * offset. Get a file page (do I/O if necessary), map the file page
1931 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1933 * This is done in two loops. The inner loop turns as many pages
1934 * as it can, up to available socket buffer space, without blocking
1935 * into mbufs to have it bulk delivered into the socket send buffer.
1936 * The outer loop checks the state and available space of the socket
1937 * and takes care of the overall progress.
1939 for (off = uap->offset, rem = uap->nbytes; ; ) {
1945 * Check the socket state for ongoing connection,
1946 * no errors and space in socket buffer.
1947 * If space is low allow for the remainder of the
1948 * file to be processed if it fits the socket buffer.
1949 * Otherwise block in waiting for sufficient space
1950 * to proceed, or if the socket is nonblocking, return
1951 * to userland with EAGAIN while reporting how far
1953 * We wait until the socket buffer has significant free
1954 * space to do bulk sends. This makes good use of file
1955 * system read ahead and allows packet segmentation
1956 * offloading hardware to take over lots of work. If
1957 * we were not careful here we would send off only one
1960 SOCKBUF_LOCK(&so->so_snd);
1961 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1962 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1964 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1966 SOCKBUF_UNLOCK(&so->so_snd);
1968 } else if (so->so_error) {
1969 error = so->so_error;
1971 SOCKBUF_UNLOCK(&so->so_snd);
1974 space = sbspace(&so->so_snd);
1977 space < so->so_snd.sb_lowat)) {
1978 if (so->so_state & SS_NBIO) {
1979 SOCKBUF_UNLOCK(&so->so_snd);
1984 * sbwait drops the lock while sleeping.
1985 * When we loop back to retry_space the
1986 * state may have changed and we retest
1989 error = sbwait(&so->so_snd);
1991 * An error from sbwait usually indicates that we've
1992 * been interrupted by a signal. If we've sent anything
1993 * then return bytes sent, otherwise return the error.
1996 SOCKBUF_UNLOCK(&so->so_snd);
2001 SOCKBUF_UNLOCK(&so->so_snd);
2004 * Reduce space in the socket buffer by the size of
2005 * the header mbuf chain.
2006 * hdrlen is set to 0 after the first loop.
2011 * Loop and construct maximum sized mbuf chain to be bulk
2012 * dumped into socket buffer.
2014 while(space > loopbytes) {
2019 VM_OBJECT_LOCK(obj);
2021 * Calculate the amount to transfer.
2022 * Not to exceed a page, the EOF,
2023 * or the passed in nbytes.
2025 pgoff = (vm_offset_t)(off & PAGE_MASK);
2026 xfsize = omin(PAGE_SIZE - pgoff,
2027 obj->un_pager.vnp.vnp_size - uap->offset -
2028 fsbytes - loopbytes);
2030 rem = (uap->nbytes - fsbytes - loopbytes);
2032 rem = obj->un_pager.vnp.vnp_size -
2033 uap->offset - fsbytes - loopbytes;
2034 xfsize = omin(rem, xfsize);
2035 xfsize = omin(space - loopbytes, xfsize);
2037 VM_OBJECT_UNLOCK(obj);
2038 done = 1; /* all data sent */
2043 * Attempt to look up the page. Allocate
2044 * if not found or wait and loop if busy.
2046 pindex = OFF_TO_IDX(off);
2047 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2048 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2051 * Check if page is valid for what we need,
2052 * otherwise initiate I/O.
2053 * If we already turned some pages into mbufs,
2054 * send them off before we come here again and
2057 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2058 VM_OBJECT_UNLOCK(obj);
2060 error = EAGAIN; /* send what we already got */
2061 else if (uap->flags & SF_NODISKIO)
2067 * Ensure that our page is still around
2068 * when the I/O completes.
2070 vm_page_io_start(pg);
2071 VM_OBJECT_UNLOCK(obj);
2074 * Get the page from backing store.
2076 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2077 error = vn_lock(vp, LK_SHARED);
2080 bsize = vp->v_mount->mnt_stat.f_iosize;
2083 * XXXMAC: Because we don't have fp->f_cred
2084 * here, we pass in NOCRED. This is probably
2085 * wrong, but is consistent with our original
2088 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2089 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2090 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2091 td->td_ucred, NOCRED, &resid, td);
2094 VFS_UNLOCK_GIANT(vfslocked);
2095 VM_OBJECT_LOCK(obj);
2096 vm_page_io_finish(pg);
2098 VM_OBJECT_UNLOCK(obj);
2102 vm_page_lock_queues();
2103 vm_page_unwire(pg, 0);
2105 * See if anyone else might know about
2106 * this page. If not and it is not valid,
2109 if (pg->wire_count == 0 && pg->valid == 0 &&
2110 pg->busy == 0 && !(pg->oflags & VPO_BUSY) &&
2111 pg->hold_count == 0) {
2114 vm_page_unlock_queues();
2115 VM_OBJECT_UNLOCK(obj);
2116 if (error == EAGAIN)
2117 error = 0; /* not a real error */
2122 * Get a sendfile buf. When allocating the
2123 * first buffer for mbuf chain, we usually
2124 * wait as long as necessary, but this wait
2125 * can be interrupted. For consequent
2126 * buffers, do not sleep, since several
2127 * threads might exhaust the buffers and then
2130 sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2133 mbstat.sf_allocfail++;
2134 vm_page_lock_queues();
2135 vm_page_unwire(pg, 0);
2137 * XXX: Not same check as above!?
2139 if (pg->wire_count == 0 && pg->object == NULL)
2141 vm_page_unlock_queues();
2143 error = (mnw ? EAGAIN : EINTR);
2148 * Get an mbuf and set it up as having
2151 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2153 error = (mnw ? EAGAIN : ENOBUFS);
2154 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2157 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2158 sfs, sf, M_RDONLY, EXT_SFBUF);
2159 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2162 /* Append to mbuf chain. */
2168 /* Keep track of bits processed. */
2169 loopbytes += xfsize;
2173 mtx_lock(&sfs->mtx);
2175 mtx_unlock(&sfs->mtx);
2179 /* Add the buffer chain to the socket buffer. */
2183 mlen = m_length(m, NULL);
2184 SOCKBUF_LOCK(&so->so_snd);
2185 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2187 SOCKBUF_UNLOCK(&so->so_snd);
2190 SOCKBUF_UNLOCK(&so->so_snd);
2191 CURVNET_SET(so->so_vnet);
2192 /* Avoid error aliasing. */
2193 err = (*so->so_proto->pr_usrreqs->pru_send)
2194 (so, 0, m, NULL, NULL, td);
2198 * We need two counters to get the
2199 * file offset and nbytes to send
2201 * - sbytes contains the total amount
2202 * of bytes sent, including headers.
2203 * - fsbytes contains the total amount
2204 * of bytes sent from the file.
2212 } else if (error == 0)
2214 m = NULL; /* pru_send always consumes */
2217 /* Quit outer loop on error or when we're done. */
2225 * Send trailers. Wimp out and use writev(2).
2227 if (trl_uio != NULL) {
2228 sbunlock(&so->so_snd);
2229 error = kern_writev(td, uap->s, trl_uio);
2231 sbytes += td->td_retval[0];
2236 sbunlock(&so->so_snd);
2239 * If there was no error we have to clear td->td_retval[0]
2240 * because it may have been set by writev.
2243 td->td_retval[0] = 0;
2245 if (uap->sbytes != NULL) {
2246 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2249 vm_object_deallocate(obj);
2251 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2253 VFS_UNLOCK_GIANT(vfslocked);
2261 mtx_lock(&sfs->mtx);
2262 if (sfs->count != 0)
2263 cv_wait(&sfs->cv, &sfs->mtx);
2264 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2265 cv_destroy(&sfs->cv);
2266 mtx_destroy(&sfs->mtx);
2270 if (error == ERESTART)
2278 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2279 * otherwise all return EOPNOTSUPP.
2280 * XXX: We should make this loadable one day.
2283 sctp_peeloff(td, uap)
2285 struct sctp_peeloff_args /* {
2290 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2291 struct filedesc *fdp;
2292 struct file *nfp = NULL;
2294 struct socket *head, *so;
2298 fdp = td->td_proc->p_fd;
2299 AUDIT_ARG_FD(uap->sd);
2300 error = fgetsock(td, uap->sd, &head, &fflag);
2303 if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
2307 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2311 * At this point we know we do have a assoc to pull
2312 * we proceed to get the fd setup. This may block
2316 error = falloc(td, &nfp, &fd);
2319 td->td_retval[0] = fd;
2321 CURVNET_SET(head->so_vnet);
2322 so = sonewconn(head, SS_ISCONNECTED);
2328 * Before changing the flags on the socket, we have to bump the
2329 * reference count. Otherwise, if the protocol calls sofree(),
2330 * the socket will be released due to a zero refcount.
2333 soref(so); /* file descriptor reference */
2338 TAILQ_REMOVE(&head->so_comp, so, so_list);
2340 so->so_state |= (head->so_state & SS_NBIO);
2341 so->so_state &= ~SS_NOFDREF;
2342 so->so_qstate &= ~SQ_COMP;
2345 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2346 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2349 if (head->so_sigio != NULL)
2350 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2354 * close the new descriptor, assuming someone hasn't ripped it
2355 * out from under us.
2358 fdclose(fdp, nfp, fd, td);
2361 * Release explicitly held references before returning.
2371 return (EOPNOTSUPP);
2376 sctp_generic_sendmsg (td, uap)
2378 struct sctp_generic_sendmsg_args /* {
2384 struct sctp_sndrcvinfo *sinfo,
2388 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2389 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2391 struct file *fp = NULL;
2393 struct sockaddr *to = NULL;
2395 struct uio *ktruio = NULL;
2398 struct iovec iov[1];
2401 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2407 error = getsockaddr(&to, uap->to, uap->tolen);
2414 AUDIT_ARG_FD(uap->sd);
2415 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2419 if (to && (KTRPOINT(td, KTR_STRUCT)))
2423 iov[0].iov_base = uap->msg;
2424 iov[0].iov_len = uap->mlen;
2426 so = (struct socket *)fp->f_data;
2427 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2432 error = mac_socket_check_send(td->td_ucred, so);
2438 auio.uio_iovcnt = 1;
2439 auio.uio_segflg = UIO_USERSPACE;
2440 auio.uio_rw = UIO_WRITE;
2442 auio.uio_offset = 0; /* XXX */
2444 len = auio.uio_resid = uap->mlen;
2445 CURVNET_SET(so->so_vnet);
2446 error = sctp_lower_sosend(so, to, &auio,
2447 (struct mbuf *)NULL, (struct mbuf *)NULL,
2448 uap->flags, u_sinfo, td);
2451 if (auio.uio_resid != len && (error == ERESTART ||
2452 error == EINTR || error == EWOULDBLOCK))
2454 /* Generation of SIGPIPE can be controlled per socket. */
2455 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2456 !(uap->flags & MSG_NOSIGNAL)) {
2457 PROC_LOCK(td->td_proc);
2458 tdksignal(td, SIGPIPE, NULL);
2459 PROC_UNLOCK(td->td_proc);
2463 td->td_retval[0] = len - auio.uio_resid;
2465 if (ktruio != NULL) {
2466 ktruio->uio_resid = td->td_retval[0];
2467 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2478 return (EOPNOTSUPP);
2483 sctp_generic_sendmsg_iov(td, uap)
2485 struct sctp_generic_sendmsg_iov_args /* {
2491 struct sctp_sndrcvinfo *sinfo,
2495 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2496 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2498 struct file *fp = NULL;
2499 int error=0, len, i;
2500 struct sockaddr *to = NULL;
2502 struct uio *ktruio = NULL;
2505 struct iovec *iov, *tiov;
2508 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2514 error = getsockaddr(&to, uap->to, uap->tolen);
2521 AUDIT_ARG_FD(uap->sd);
2522 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2526 #ifdef COMPAT_FREEBSD32
2527 if (SV_CURPROC_FLAG(SV_ILP32))
2528 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2529 uap->iovlen, &iov, EMSGSIZE);
2532 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2536 if (to && (KTRPOINT(td, KTR_STRUCT)))
2540 so = (struct socket *)fp->f_data;
2541 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2546 error = mac_socket_check_send(td->td_ucred, so);
2552 auio.uio_iovcnt = uap->iovlen;
2553 auio.uio_segflg = UIO_USERSPACE;
2554 auio.uio_rw = UIO_WRITE;
2556 auio.uio_offset = 0; /* XXX */
2559 for (i = 0; i <uap->iovlen; i++, tiov++) {
2560 if ((auio.uio_resid += tiov->iov_len) < 0) {
2565 len = auio.uio_resid;
2566 CURVNET_SET(so->so_vnet);
2567 error = sctp_lower_sosend(so, to, &auio,
2568 (struct mbuf *)NULL, (struct mbuf *)NULL,
2569 uap->flags, u_sinfo, td);
2572 if (auio.uio_resid != len && (error == ERESTART ||
2573 error == EINTR || error == EWOULDBLOCK))
2575 /* Generation of SIGPIPE can be controlled per socket */
2576 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2577 !(uap->flags & MSG_NOSIGNAL)) {
2578 PROC_LOCK(td->td_proc);
2579 tdksignal(td, SIGPIPE, NULL);
2580 PROC_UNLOCK(td->td_proc);
2584 td->td_retval[0] = len - auio.uio_resid;
2586 if (ktruio != NULL) {
2587 ktruio->uio_resid = td->td_retval[0];
2588 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2601 return (EOPNOTSUPP);
2606 sctp_generic_recvmsg(td, uap)
2608 struct sctp_generic_recvmsg_args /* {
2612 struct sockaddr *from,
2613 __socklen_t *fromlenaddr,
2614 struct sctp_sndrcvinfo *sinfo,
2618 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2619 u_int8_t sockbufstore[256];
2621 struct iovec *iov, *tiov;
2622 struct sctp_sndrcvinfo sinfo;
2624 struct file *fp = NULL;
2625 struct sockaddr *fromsa;
2627 int len, i, msg_flags;
2630 struct uio *ktruio = NULL;
2633 AUDIT_ARG_FD(uap->sd);
2634 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2638 #ifdef COMPAT_FREEBSD32
2639 if (SV_CURPROC_FLAG(SV_ILP32))
2640 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2641 uap->iovlen, &iov, EMSGSIZE);
2644 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2649 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2654 error = mac_socket_check_receive(td->td_ucred, so);
2660 if (uap->fromlenaddr) {
2661 error = copyin(uap->fromlenaddr,
2662 &fromlen, sizeof (fromlen));
2669 if (uap->msg_flags) {
2670 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2678 auio.uio_iovcnt = uap->iovlen;
2679 auio.uio_segflg = UIO_USERSPACE;
2680 auio.uio_rw = UIO_READ;
2682 auio.uio_offset = 0; /* XXX */
2685 for (i = 0; i <uap->iovlen; i++, tiov++) {
2686 if ((auio.uio_resid += tiov->iov_len) < 0) {
2691 len = auio.uio_resid;
2692 fromsa = (struct sockaddr *)sockbufstore;
2695 if (KTRPOINT(td, KTR_GENIO))
2696 ktruio = cloneuio(&auio);
2698 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
2699 CURVNET_SET(so->so_vnet);
2700 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2701 fromsa, fromlen, &msg_flags,
2702 (struct sctp_sndrcvinfo *)&sinfo, 1);
2705 if (auio.uio_resid != (int)len && (error == ERESTART ||
2706 error == EINTR || error == EWOULDBLOCK))
2710 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2713 if (ktruio != NULL) {
2714 ktruio->uio_resid = (int)len - auio.uio_resid;
2715 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2720 td->td_retval[0] = (int)len - auio.uio_resid;
2722 if (fromlen && uap->from) {
2724 if (len <= 0 || fromsa == 0)
2727 len = MIN(len, fromsa->sa_len);
2728 error = copyout(fromsa, uap->from, (unsigned)len);
2732 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2738 if (KTRPOINT(td, KTR_STRUCT))
2739 ktrsockaddr(fromsa);
2741 if (uap->msg_flags) {
2742 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2755 return (EOPNOTSUPP);