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>
57 #include <sys/mount.h>
59 #include <sys/protosw.h>
60 #include <sys/sf_buf.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/signalvar.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysctl.h>
67 #include <sys/vimage.h>
68 #include <sys/vnode.h>
70 #include <sys/ktrace.h>
73 #include <security/audit/audit.h>
74 #include <security/mac/mac_framework.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_pageout.h>
80 #include <vm/vm_kern.h>
81 #include <vm/vm_extern.h>
83 #if defined(INET) || defined(INET6)
85 #include <netinet/sctp.h>
86 #include <netinet/sctp_peeloff.h>
88 #endif /* INET || INET6 */
90 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
91 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
93 static int accept1(struct thread *td, struct accept_args *uap, int compat);
94 static int do_sendfile(struct thread *td, struct sendfile_args *uap, int compat);
95 static int getsockname1(struct thread *td, struct getsockname_args *uap,
97 static int getpeername1(struct thread *td, struct getpeername_args *uap,
101 * NSFBUFS-related variables and associated sysctls
107 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
108 "Maximum number of sendfile(2) sf_bufs available");
109 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
110 "Number of sendfile(2) sf_bufs at peak usage");
111 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
112 "Number of sendfile(2) sf_bufs in use");
115 * Convert a user file descriptor to a kernel file entry. A reference on the
116 * file entry is held upon returning. This is lighter weight than
117 * fgetsock(), which bumps the socket reference drops the file reference
118 * count instead, as this approach avoids several additional mutex operations
119 * associated with the additional reference count. If requested, return the
123 getsock(struct filedesc *fdp, int fd, struct file **fpp, u_int *fflagp)
129 if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL) {
131 } else if (fp->f_type != DTYPE_SOCKET) {
132 fdrop(fp, curthread);
137 *fflagp = fp->f_flag;
145 * System call interface to the socket abstraction.
147 #if defined(COMPAT_43)
148 #define COMPAT_OLDSOCK
154 struct socket_args /* {
160 struct filedesc *fdp;
165 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
167 error = mac_socket_check_create(td->td_ucred, uap->domain, uap->type,
172 fdp = td->td_proc->p_fd;
173 error = falloc(td, &fp, &fd);
176 /* An extra reference on `fp' has been held for us by falloc(). */
177 error = socreate(uap->domain, &so, uap->type, uap->protocol,
180 fdclose(fdp, fp, fd, td);
182 finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &socketops);
183 td->td_retval[0] = fd;
193 struct bind_args /* {
202 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
205 error = kern_bind(td, uap->s, sa);
211 kern_bind(td, fd, sa)
221 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
226 if (KTRPOINT(td, KTR_STRUCT))
230 error = mac_socket_check_bind(td->td_ucred, so, sa);
233 error = sobind(so, sa, td);
242 struct listen_args /* {
251 AUDIT_ARG_FD(uap->s);
252 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
256 error = mac_socket_check_listen(td->td_ucred, so);
259 CURVNET_SET(so->so_vnet);
260 error = solisten(so, uap->backlog, td);
274 accept1(td, uap, compat)
276 struct accept_args /* {
278 struct sockaddr * __restrict name;
279 socklen_t * __restrict anamelen;
283 struct sockaddr *name;
288 if (uap->name == NULL)
289 return (kern_accept(td, uap->s, NULL, NULL, NULL));
291 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
295 error = kern_accept(td, uap->s, &name, &namelen, &fp);
298 * return a namelen of zero for older code which might
299 * ignore the return value from accept.
302 (void) copyout(&namelen,
303 uap->anamelen, sizeof(*uap->anamelen));
307 if (error == 0 && name != NULL) {
308 #ifdef COMPAT_OLDSOCK
310 ((struct osockaddr *)name)->sa_family =
313 error = copyout(name, uap->name, namelen);
316 error = copyout(&namelen, uap->anamelen,
319 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
321 free(name, M_SONAME);
326 kern_accept(struct thread *td, int s, struct sockaddr **name,
327 socklen_t *namelen, struct file **fp)
329 struct filedesc *fdp;
330 struct file *headfp, *nfp = NULL;
331 struct sockaddr *sa = NULL;
333 struct socket *head, *so;
346 fdp = td->td_proc->p_fd;
347 error = getsock(fdp, s, &headfp, &fflag);
350 head = headfp->f_data;
351 if ((head->so_options & SO_ACCEPTCONN) == 0) {
356 error = mac_socket_check_accept(td->td_ucred, head);
360 error = falloc(td, &nfp, &fd);
364 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
369 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
370 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
371 head->so_error = ECONNABORTED;
374 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
381 if (head->so_error) {
382 error = head->so_error;
387 so = TAILQ_FIRST(&head->so_comp);
388 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
389 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
392 * Before changing the flags on the socket, we have to bump the
393 * reference count. Otherwise, if the protocol calls sofree(),
394 * the socket will be released due to a zero refcount.
396 SOCK_LOCK(so); /* soref() and so_state update */
397 soref(so); /* file descriptor reference */
399 TAILQ_REMOVE(&head->so_comp, so, so_list);
401 so->so_state |= (head->so_state & SS_NBIO);
402 so->so_qstate &= ~SQ_COMP;
408 /* An extra reference on `nfp' has been held for us by falloc(). */
409 td->td_retval[0] = fd;
411 /* connection has been removed from the listen queue */
412 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
414 pgid = fgetown(&head->so_sigio);
416 fsetown(pgid, &so->so_sigio);
418 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
419 /* Sync socket nonblocking/async state with file flags */
420 tmp = fflag & FNONBLOCK;
421 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
422 tmp = fflag & FASYNC;
423 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
425 CURVNET_SET(so->so_vnet);
426 error = soaccept(so, &sa);
430 * return a namelen of zero for older code which might
431 * ignore the return value from accept.
443 /* check sa_len before it is destroyed */
444 if (*namelen > sa->sa_len)
445 *namelen = sa->sa_len;
447 if (KTRPOINT(td, KTR_STRUCT))
458 * close the new descriptor, assuming someone hasn't ripped it
462 fdclose(fdp, nfp, fd, td);
465 * Release explicitly held references before returning. We return
466 * a reference on nfp to the caller on success if they request it.
485 struct accept_args *uap;
488 return (accept1(td, uap, 0));
491 #ifdef COMPAT_OLDSOCK
495 struct accept_args *uap;
498 return (accept1(td, uap, 1));
500 #endif /* COMPAT_OLDSOCK */
506 struct connect_args /* {
515 error = getsockaddr(&sa, uap->name, uap->namelen);
519 error = kern_connect(td, uap->s, sa);
526 kern_connect(td, fd, sa)
537 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
541 if (so->so_state & SS_ISCONNECTING) {
546 if (KTRPOINT(td, KTR_STRUCT))
550 error = mac_socket_check_connect(td->td_ucred, so, sa);
554 error = soconnect(so, sa, td);
557 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
562 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
563 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
566 if (error == EINTR || error == ERESTART)
572 error = so->so_error;
578 so->so_state &= ~SS_ISCONNECTING;
579 if (error == ERESTART)
587 kern_socketpair(struct thread *td, int domain, int type, int protocol,
590 struct filedesc *fdp = td->td_proc->p_fd;
591 struct file *fp1, *fp2;
592 struct socket *so1, *so2;
595 AUDIT_ARG_SOCKET(domain, type, protocol);
597 /* We might want to have a separate check for socket pairs. */
598 error = mac_socket_check_create(td->td_ucred, domain, type,
603 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
606 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
609 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
610 error = falloc(td, &fp1, &fd);
614 fp1->f_data = so1; /* so1 already has ref count */
615 error = falloc(td, &fp2, &fd);
618 fp2->f_data = so2; /* so2 already has ref count */
620 error = soconnect2(so1, so2);
623 if (type == SOCK_DGRAM) {
625 * Datagram socket connection is asymmetric.
627 error = soconnect2(so2, so1);
631 finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops);
632 finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops);
637 fdclose(fdp, fp2, rsv[1], td);
640 fdclose(fdp, fp1, rsv[0], td);
652 socketpair(struct thread *td, struct socketpair_args *uap)
656 error = kern_socketpair(td, uap->domain, uap->type,
660 error = copyout(sv, uap->rsv, 2 * sizeof(int));
662 (void)kern_close(td, sv[0]);
663 (void)kern_close(td, sv[1]);
669 sendit(td, s, mp, flags)
675 struct mbuf *control;
679 if (mp->msg_name != NULL) {
680 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
690 if (mp->msg_control) {
691 if (mp->msg_controllen < sizeof(struct cmsghdr)
692 #ifdef COMPAT_OLDSOCK
693 && mp->msg_flags != MSG_COMPAT
699 error = sockargs(&control, mp->msg_control,
700 mp->msg_controllen, MT_CONTROL);
703 #ifdef COMPAT_OLDSOCK
704 if (mp->msg_flags == MSG_COMPAT) {
707 M_PREPEND(control, sizeof(*cm), M_WAIT);
708 cm = mtod(control, struct cmsghdr *);
709 cm->cmsg_len = control->m_len;
710 cm->cmsg_level = SOL_SOCKET;
711 cm->cmsg_type = SCM_RIGHTS;
718 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
727 kern_sendit(td, s, mp, flags, control, segflg)
732 struct mbuf *control;
742 struct uio *ktruio = NULL;
746 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
749 so = (struct socket *)fp->f_data;
752 if (mp->msg_name != NULL) {
753 error = mac_socket_check_connect(td->td_ucred, so,
758 error = mac_socket_check_send(td->td_ucred, so);
763 auio.uio_iov = mp->msg_iov;
764 auio.uio_iovcnt = mp->msg_iovlen;
765 auio.uio_segflg = segflg;
766 auio.uio_rw = UIO_WRITE;
768 auio.uio_offset = 0; /* XXX */
771 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
772 if ((auio.uio_resid += iov->iov_len) < 0) {
778 if (KTRPOINT(td, KTR_GENIO))
779 ktruio = cloneuio(&auio);
781 len = auio.uio_resid;
782 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
784 if (auio.uio_resid != len && (error == ERESTART ||
785 error == EINTR || error == EWOULDBLOCK))
787 /* Generation of SIGPIPE can be controlled per socket */
788 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
789 !(flags & MSG_NOSIGNAL)) {
790 PROC_LOCK(td->td_proc);
791 psignal(td->td_proc, SIGPIPE);
792 PROC_UNLOCK(td->td_proc);
796 td->td_retval[0] = len - auio.uio_resid;
798 if (ktruio != NULL) {
799 ktruio->uio_resid = td->td_retval[0];
800 ktrgenio(s, UIO_WRITE, ktruio, error);
811 struct sendto_args /* {
824 msg.msg_name = uap->to;
825 msg.msg_namelen = uap->tolen;
829 #ifdef COMPAT_OLDSOCK
832 aiov.iov_base = uap->buf;
833 aiov.iov_len = uap->len;
834 error = sendit(td, uap->s, &msg, uap->flags);
838 #ifdef COMPAT_OLDSOCK
842 struct osend_args /* {
857 aiov.iov_base = uap->buf;
858 aiov.iov_len = uap->len;
861 error = sendit(td, uap->s, &msg, uap->flags);
868 struct osendmsg_args /* {
878 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
881 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
885 msg.msg_flags = MSG_COMPAT;
886 error = sendit(td, uap->s, &msg, uap->flags);
895 struct sendmsg_args /* {
905 error = copyin(uap->msg, &msg, sizeof (msg));
908 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
912 #ifdef COMPAT_OLDSOCK
915 error = sendit(td, uap->s, &msg, uap->flags);
921 kern_recvit(td, s, mp, fromseg, controlp)
925 enum uio_seg fromseg;
926 struct mbuf **controlp;
933 struct mbuf *m, *control = 0;
937 struct sockaddr *fromsa = 0;
939 struct uio *ktruio = NULL;
946 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
952 error = mac_socket_check_receive(td->td_ucred, so);
959 auio.uio_iov = mp->msg_iov;
960 auio.uio_iovcnt = mp->msg_iovlen;
961 auio.uio_segflg = UIO_USERSPACE;
962 auio.uio_rw = UIO_READ;
964 auio.uio_offset = 0; /* XXX */
967 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
968 if ((auio.uio_resid += iov->iov_len) < 0) {
974 if (KTRPOINT(td, KTR_GENIO))
975 ktruio = cloneuio(&auio);
977 len = auio.uio_resid;
978 CURVNET_SET(so->so_vnet);
979 error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
980 (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
984 if (auio.uio_resid != (int)len && (error == ERESTART ||
985 error == EINTR || error == EWOULDBLOCK))
989 if (ktruio != NULL) {
990 ktruio->uio_resid = (int)len - auio.uio_resid;
991 ktrgenio(s, UIO_READ, ktruio, error);
996 td->td_retval[0] = (int)len - auio.uio_resid;
998 len = mp->msg_namelen;
999 if (len <= 0 || fromsa == 0)
1002 /* save sa_len before it is destroyed by MSG_COMPAT */
1003 len = MIN(len, fromsa->sa_len);
1004 #ifdef COMPAT_OLDSOCK
1005 if (mp->msg_flags & MSG_COMPAT)
1006 ((struct osockaddr *)fromsa)->sa_family =
1009 if (fromseg == UIO_USERSPACE) {
1010 error = copyout(fromsa, mp->msg_name,
1015 bcopy(fromsa, mp->msg_name, len);
1017 mp->msg_namelen = len;
1019 if (mp->msg_control && controlp == NULL) {
1020 #ifdef COMPAT_OLDSOCK
1022 * We assume that old recvmsg calls won't receive access
1023 * rights and other control info, esp. as control info
1024 * is always optional and those options didn't exist in 4.3.
1025 * If we receive rights, trim the cmsghdr; anything else
1028 if (control && mp->msg_flags & MSG_COMPAT) {
1029 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1031 mtod(control, struct cmsghdr *)->cmsg_type !=
1033 mp->msg_controllen = 0;
1036 control->m_len -= sizeof (struct cmsghdr);
1037 control->m_data += sizeof (struct cmsghdr);
1040 len = mp->msg_controllen;
1042 mp->msg_controllen = 0;
1043 ctlbuf = mp->msg_control;
1045 while (m && len > 0) {
1046 unsigned int tocopy;
1048 if (len >= m->m_len)
1051 mp->msg_flags |= MSG_CTRUNC;
1055 if ((error = copyout(mtod(m, caddr_t),
1056 ctlbuf, tocopy)) != 0)
1063 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1068 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1069 ktrsockaddr(fromsa);
1072 free(fromsa, M_SONAME);
1074 if (error == 0 && controlp != NULL)
1075 *controlp = control;
1083 recvit(td, s, mp, namelenp)
1091 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1095 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1096 #ifdef COMPAT_OLDSOCK
1097 if (mp->msg_flags & MSG_COMPAT)
1098 error = 0; /* old recvfrom didn't check */
1107 struct recvfrom_args /* {
1112 struct sockaddr * __restrict from;
1113 socklen_t * __restrict fromlenaddr;
1120 if (uap->fromlenaddr) {
1121 error = copyin(uap->fromlenaddr,
1122 &msg.msg_namelen, sizeof (msg.msg_namelen));
1126 msg.msg_namelen = 0;
1128 msg.msg_name = uap->from;
1129 msg.msg_iov = &aiov;
1131 aiov.iov_base = uap->buf;
1132 aiov.iov_len = uap->len;
1133 msg.msg_control = 0;
1134 msg.msg_flags = uap->flags;
1135 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1140 #ifdef COMPAT_OLDSOCK
1144 struct recvfrom_args *uap;
1147 uap->flags |= MSG_COMPAT;
1148 return (recvfrom(td, uap));
1152 #ifdef COMPAT_OLDSOCK
1156 struct orecv_args /* {
1168 msg.msg_namelen = 0;
1169 msg.msg_iov = &aiov;
1171 aiov.iov_base = uap->buf;
1172 aiov.iov_len = uap->len;
1173 msg.msg_control = 0;
1174 msg.msg_flags = uap->flags;
1175 error = recvit(td, uap->s, &msg, NULL);
1180 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1181 * overlays the new one, missing only the flags, and with the (old) access
1182 * rights where the control fields are now.
1187 struct orecvmsg_args /* {
1189 struct omsghdr *msg;
1197 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1200 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1203 msg.msg_flags = uap->flags | MSG_COMPAT;
1205 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1206 if (msg.msg_controllen && error == 0)
1207 error = copyout(&msg.msg_controllen,
1208 &uap->msg->msg_accrightslen, sizeof (int));
1217 struct recvmsg_args /* {
1224 struct iovec *uiov, *iov;
1227 error = copyin(uap->msg, &msg, sizeof (msg));
1230 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1233 msg.msg_flags = uap->flags;
1234 #ifdef COMPAT_OLDSOCK
1235 msg.msg_flags &= ~MSG_COMPAT;
1239 error = recvit(td, uap->s, &msg, NULL);
1242 error = copyout(&msg, uap->msg, sizeof(msg));
1252 struct shutdown_args /* {
1261 AUDIT_ARG_FD(uap->s);
1262 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
1265 error = soshutdown(so, uap->how);
1275 struct setsockopt_args /* {
1284 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1285 uap->val, UIO_USERSPACE, uap->valsize));
1289 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1295 enum uio_seg valseg;
1301 struct sockopt sopt;
1303 if (val == NULL && valsize != 0)
1305 if ((int)valsize < 0)
1308 sopt.sopt_dir = SOPT_SET;
1309 sopt.sopt_level = level;
1310 sopt.sopt_name = name;
1311 sopt.sopt_val = val;
1312 sopt.sopt_valsize = valsize;
1318 sopt.sopt_td = NULL;
1321 panic("kern_setsockopt called with bad valseg");
1325 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1328 CURVNET_SET(so->so_vnet);
1329 error = sosetopt(so, &sopt);
1340 struct getsockopt_args /* {
1344 void * __restrict val;
1345 socklen_t * __restrict avalsize;
1352 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1357 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1358 uap->val, UIO_USERSPACE, &valsize);
1361 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1366 * Kernel version of getsockopt.
1367 * optval can be a userland or userspace. optlen is always a kernel pointer.
1370 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1376 enum uio_seg valseg;
1382 struct sockopt sopt;
1386 if ((int)*valsize < 0)
1389 sopt.sopt_dir = SOPT_GET;
1390 sopt.sopt_level = level;
1391 sopt.sopt_name = name;
1392 sopt.sopt_val = val;
1393 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1399 sopt.sopt_td = NULL;
1402 panic("kern_getsockopt called with bad valseg");
1406 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1409 CURVNET_SET(so->so_vnet);
1410 error = sogetopt(so, &sopt);
1412 *valsize = sopt.sopt_valsize;
1419 * getsockname1() - Get socket name.
1423 getsockname1(td, uap, compat)
1425 struct getsockname_args /* {
1427 struct sockaddr * __restrict asa;
1428 socklen_t * __restrict alen;
1432 struct sockaddr *sa;
1436 error = copyin(uap->alen, &len, sizeof(len));
1440 error = kern_getsockname(td, uap->fdes, &sa, &len);
1445 #ifdef COMPAT_OLDSOCK
1447 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1449 error = copyout(sa, uap->asa, (u_int)len);
1453 error = copyout(&len, uap->alen, sizeof(len));
1458 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1470 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1475 CURVNET_SET(so->so_vnet);
1476 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1483 len = MIN(*alen, (*sa)->sa_len);
1486 if (KTRPOINT(td, KTR_STRUCT))
1492 free(*sa, M_SONAME);
1499 getsockname(td, uap)
1501 struct getsockname_args *uap;
1504 return (getsockname1(td, uap, 0));
1507 #ifdef COMPAT_OLDSOCK
1509 ogetsockname(td, uap)
1511 struct getsockname_args *uap;
1514 return (getsockname1(td, uap, 1));
1516 #endif /* COMPAT_OLDSOCK */
1519 * getpeername1() - Get name of peer for connected socket.
1523 getpeername1(td, uap, compat)
1525 struct getpeername_args /* {
1527 struct sockaddr * __restrict asa;
1528 socklen_t * __restrict alen;
1532 struct sockaddr *sa;
1536 error = copyin(uap->alen, &len, sizeof (len));
1540 error = kern_getpeername(td, uap->fdes, &sa, &len);
1545 #ifdef COMPAT_OLDSOCK
1547 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1549 error = copyout(sa, uap->asa, (u_int)len);
1553 error = copyout(&len, uap->alen, sizeof(len));
1558 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1570 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1574 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1579 CURVNET_SET(so->so_vnet);
1580 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1587 len = MIN(*alen, (*sa)->sa_len);
1590 if (KTRPOINT(td, KTR_STRUCT))
1595 free(*sa, M_SONAME);
1604 getpeername(td, uap)
1606 struct getpeername_args *uap;
1609 return (getpeername1(td, uap, 0));
1612 #ifdef COMPAT_OLDSOCK
1614 ogetpeername(td, uap)
1616 struct ogetpeername_args *uap;
1619 /* XXX uap should have type `getpeername_args *' to begin with. */
1620 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1622 #endif /* COMPAT_OLDSOCK */
1625 sockargs(mp, buf, buflen, type)
1630 struct sockaddr *sa;
1634 if ((u_int)buflen > MLEN) {
1635 #ifdef COMPAT_OLDSOCK
1636 if (type == MT_SONAME && (u_int)buflen <= 112)
1637 buflen = MLEN; /* unix domain compat. hack */
1640 if ((u_int)buflen > MCLBYTES)
1643 m = m_get(M_WAIT, type);
1644 if ((u_int)buflen > MLEN)
1647 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1652 if (type == MT_SONAME) {
1653 sa = mtod(m, struct sockaddr *);
1655 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1656 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1657 sa->sa_family = sa->sa_len;
1659 sa->sa_len = buflen;
1666 getsockaddr(namp, uaddr, len)
1667 struct sockaddr **namp;
1671 struct sockaddr *sa;
1674 if (len > SOCK_MAXADDRLEN)
1675 return (ENAMETOOLONG);
1676 if (len < offsetof(struct sockaddr, sa_data[0]))
1678 sa = malloc(len, M_SONAME, M_WAITOK);
1679 error = copyin(uaddr, sa, len);
1683 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1684 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1685 sa->sa_family = sa->sa_len;
1693 #include <sys/condvar.h>
1695 struct sendfile_sync {
1702 * Detach mapped page and release resources back to the system.
1705 sf_buf_mext(void *addr, void *args)
1708 struct sendfile_sync *sfs;
1710 m = sf_buf_page(args);
1712 vm_page_lock_queues();
1713 vm_page_unwire(m, 0);
1715 * Check for the object going away on us. This can
1716 * happen since we don't hold a reference to it.
1717 * If so, we're responsible for freeing the page.
1719 if (m->wire_count == 0 && m->object == NULL)
1721 vm_page_unlock_queues();
1725 mtx_lock(&sfs->mtx);
1726 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1727 if (--sfs->count == 0)
1728 cv_signal(&sfs->cv);
1729 mtx_unlock(&sfs->mtx);
1735 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1736 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1738 * Send a file specified by 'fd' and starting at 'offset' to a socket
1739 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1740 * 0. Optionally add a header and/or trailer to the socket output. If
1741 * specified, write the total number of bytes sent into *sbytes.
1744 sendfile(struct thread *td, struct sendfile_args *uap)
1747 return (do_sendfile(td, uap, 0));
1751 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1753 struct sf_hdtr hdtr;
1754 struct uio *hdr_uio, *trl_uio;
1757 hdr_uio = trl_uio = NULL;
1759 if (uap->hdtr != NULL) {
1760 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1763 if (hdtr.headers != NULL) {
1764 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1768 if (hdtr.trailers != NULL) {
1769 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1776 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1779 free(hdr_uio, M_IOV);
1781 free(trl_uio, M_IOV);
1785 #ifdef COMPAT_FREEBSD4
1787 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1789 struct sendfile_args args;
1793 args.offset = uap->offset;
1794 args.nbytes = uap->nbytes;
1795 args.hdtr = uap->hdtr;
1796 args.sbytes = uap->sbytes;
1797 args.flags = uap->flags;
1799 return (do_sendfile(td, &args, 1));
1801 #endif /* COMPAT_FREEBSD4 */
1804 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1805 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1807 struct file *sock_fp;
1809 struct vm_object *obj = NULL;
1810 struct socket *so = NULL;
1811 struct mbuf *m = NULL;
1814 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1815 int error, hdrlen = 0, mnw = 0;
1817 struct sendfile_sync *sfs = NULL;
1820 * The file descriptor must be a regular file and have a
1821 * backing VM object.
1822 * File offset must be positive. If it goes beyond EOF
1823 * we send only the header/trailer and no payload data.
1825 AUDIT_ARG_FD(uap->fd);
1826 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1828 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1829 vn_lock(vp, LK_SHARED | LK_RETRY);
1830 if (vp->v_type == VREG) {
1834 * Temporarily increase the backing VM
1835 * object's reference count so that a forced
1836 * reclamation of its vnode does not
1837 * immediately destroy it.
1839 VM_OBJECT_LOCK(obj);
1840 if ((obj->flags & OBJ_DEAD) == 0) {
1841 vm_object_reference_locked(obj);
1842 VM_OBJECT_UNLOCK(obj);
1844 VM_OBJECT_UNLOCK(obj);
1850 VFS_UNLOCK_GIANT(vfslocked);
1855 if (uap->offset < 0) {
1861 * The socket must be a stream socket and connected.
1862 * Remember if it a blocking or non-blocking socket.
1864 if ((error = getsock(td->td_proc->p_fd, uap->s, &sock_fp,
1867 so = sock_fp->f_data;
1868 if (so->so_type != SOCK_STREAM) {
1872 if ((so->so_state & SS_ISCONNECTED) == 0) {
1877 * Do not wait on memory allocations but return ENOMEM for
1878 * caller to retry later.
1879 * XXX: Experimental.
1881 if (uap->flags & SF_MNOWAIT)
1884 if (uap->flags & SF_SYNC) {
1885 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK);
1886 memset(sfs, 0, sizeof *sfs);
1887 mtx_init(&sfs->mtx, "sendfile", MTX_DEF, 0);
1888 cv_init(&sfs->cv, "sendfile");
1892 error = mac_socket_check_send(td->td_ucred, so);
1897 /* If headers are specified copy them into mbufs. */
1898 if (hdr_uio != NULL) {
1899 hdr_uio->uio_td = td;
1900 hdr_uio->uio_rw = UIO_WRITE;
1901 if (hdr_uio->uio_resid > 0) {
1903 * In FBSD < 5.0 the nbytes to send also included
1904 * the header. If compat is specified subtract the
1905 * header size from nbytes.
1908 if (uap->nbytes > hdr_uio->uio_resid)
1909 uap->nbytes -= hdr_uio->uio_resid;
1913 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1916 error = mnw ? EAGAIN : ENOBUFS;
1919 hdrlen = m_length(m, NULL);
1924 * Protect against multiple writers to the socket.
1926 * XXXRW: Historically this has assumed non-interruptibility, so now
1927 * we implement that, but possibly shouldn't.
1929 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1932 * Loop through the pages of the file, starting with the requested
1933 * offset. Get a file page (do I/O if necessary), map the file page
1934 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1936 * This is done in two loops. The inner loop turns as many pages
1937 * as it can, up to available socket buffer space, without blocking
1938 * into mbufs to have it bulk delivered into the socket send buffer.
1939 * The outer loop checks the state and available space of the socket
1940 * and takes care of the overall progress.
1942 for (off = uap->offset, rem = uap->nbytes; ; ) {
1948 * Check the socket state for ongoing connection,
1949 * no errors and space in socket buffer.
1950 * If space is low allow for the remainder of the
1951 * file to be processed if it fits the socket buffer.
1952 * Otherwise block in waiting for sufficient space
1953 * to proceed, or if the socket is nonblocking, return
1954 * to userland with EAGAIN while reporting how far
1956 * We wait until the socket buffer has significant free
1957 * space to do bulk sends. This makes good use of file
1958 * system read ahead and allows packet segmentation
1959 * offloading hardware to take over lots of work. If
1960 * we were not careful here we would send off only one
1963 SOCKBUF_LOCK(&so->so_snd);
1964 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1965 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1967 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1969 SOCKBUF_UNLOCK(&so->so_snd);
1971 } else if (so->so_error) {
1972 error = so->so_error;
1974 SOCKBUF_UNLOCK(&so->so_snd);
1977 space = sbspace(&so->so_snd);
1980 space < so->so_snd.sb_lowat)) {
1981 if (so->so_state & SS_NBIO) {
1982 SOCKBUF_UNLOCK(&so->so_snd);
1987 * sbwait drops the lock while sleeping.
1988 * When we loop back to retry_space the
1989 * state may have changed and we retest
1992 error = sbwait(&so->so_snd);
1994 * An error from sbwait usually indicates that we've
1995 * been interrupted by a signal. If we've sent anything
1996 * then return bytes sent, otherwise return the error.
1999 SOCKBUF_UNLOCK(&so->so_snd);
2004 SOCKBUF_UNLOCK(&so->so_snd);
2007 * Reduce space in the socket buffer by the size of
2008 * the header mbuf chain.
2009 * hdrlen is set to 0 after the first loop.
2014 * Loop and construct maximum sized mbuf chain to be bulk
2015 * dumped into socket buffer.
2017 while(space > loopbytes) {
2022 VM_OBJECT_LOCK(obj);
2024 * Calculate the amount to transfer.
2025 * Not to exceed a page, the EOF,
2026 * or the passed in nbytes.
2028 pgoff = (vm_offset_t)(off & PAGE_MASK);
2029 xfsize = omin(PAGE_SIZE - pgoff,
2030 obj->un_pager.vnp.vnp_size - uap->offset -
2031 fsbytes - loopbytes);
2033 rem = (uap->nbytes - fsbytes - loopbytes);
2035 rem = obj->un_pager.vnp.vnp_size -
2036 uap->offset - fsbytes - loopbytes;
2037 xfsize = omin(rem, xfsize);
2039 VM_OBJECT_UNLOCK(obj);
2040 done = 1; /* all data sent */
2044 * Don't overflow the send buffer.
2045 * Stop here and send out what we've
2048 if (space < loopbytes + xfsize) {
2049 VM_OBJECT_UNLOCK(obj);
2054 * Attempt to look up the page. Allocate
2055 * if not found or wait and loop if busy.
2057 pindex = OFF_TO_IDX(off);
2058 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2059 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2062 * Check if page is valid for what we need,
2063 * otherwise initiate I/O.
2064 * If we already turned some pages into mbufs,
2065 * send them off before we come here again and
2068 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2069 VM_OBJECT_UNLOCK(obj);
2071 error = EAGAIN; /* send what we already got */
2072 else if (uap->flags & SF_NODISKIO)
2078 * Ensure that our page is still around
2079 * when the I/O completes.
2081 vm_page_io_start(pg);
2082 VM_OBJECT_UNLOCK(obj);
2085 * Get the page from backing store.
2087 bsize = vp->v_mount->mnt_stat.f_iosize;
2088 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2089 vn_lock(vp, LK_SHARED | LK_RETRY);
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);
2102 VFS_UNLOCK_GIANT(vfslocked);
2103 VM_OBJECT_LOCK(obj);
2104 vm_page_io_finish(pg);
2106 VM_OBJECT_UNLOCK(obj);
2110 vm_page_lock_queues();
2111 vm_page_unwire(pg, 0);
2113 * See if anyone else might know about
2114 * this page. If not and it is not valid,
2117 if (pg->wire_count == 0 && pg->valid == 0 &&
2118 pg->busy == 0 && !(pg->oflags & VPO_BUSY) &&
2119 pg->hold_count == 0) {
2122 vm_page_unlock_queues();
2123 VM_OBJECT_UNLOCK(obj);
2124 if (error == EAGAIN)
2125 error = 0; /* not a real error */
2130 * Get a sendfile buf. We usually wait as long
2131 * as necessary, but this wait can be interrupted.
2133 if ((sf = sf_buf_alloc(pg,
2134 (mnw ? SFB_NOWAIT : SFB_CATCH))) == NULL) {
2135 mbstat.sf_allocfail++;
2136 vm_page_lock_queues();
2137 vm_page_unwire(pg, 0);
2139 * XXX: Not same check as above!?
2141 if (pg->wire_count == 0 && pg->object == NULL)
2143 vm_page_unlock_queues();
2144 error = (mnw ? EAGAIN : EINTR);
2149 * Get an mbuf and set it up as having
2152 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2154 error = (mnw ? EAGAIN : ENOBUFS);
2155 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2158 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2159 sfs, sf, M_RDONLY, EXT_SFBUF);
2160 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2163 /* Append to mbuf chain. */
2169 /* Keep track of bits processed. */
2170 loopbytes += xfsize;
2174 mtx_lock(&sfs->mtx);
2176 mtx_unlock(&sfs->mtx);
2180 /* Add the buffer chain to the socket buffer. */
2184 mlen = m_length(m, NULL);
2185 SOCKBUF_LOCK(&so->so_snd);
2186 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2188 SOCKBUF_UNLOCK(&so->so_snd);
2191 SOCKBUF_UNLOCK(&so->so_snd);
2192 CURVNET_SET(so->so_vnet);
2193 /* Avoid error aliasing. */
2194 err = (*so->so_proto->pr_usrreqs->pru_send)
2195 (so, 0, m, NULL, NULL, td);
2199 * We need two counters to get the
2200 * file offset and nbytes to send
2202 * - sbytes contains the total amount
2203 * of bytes sent, including headers.
2204 * - fsbytes contains the total amount
2205 * of bytes sent from the file.
2213 } else if (error == 0)
2215 m = NULL; /* pru_send always consumes */
2218 /* Quit outer loop on error or when we're done. */
2226 * Send trailers. Wimp out and use writev(2).
2228 if (trl_uio != NULL) {
2229 sbunlock(&so->so_snd);
2230 error = kern_writev(td, uap->s, trl_uio);
2232 sbytes += td->td_retval[0];
2237 sbunlock(&so->so_snd);
2240 * If there was no error we have to clear td->td_retval[0]
2241 * because it may have been set by writev.
2244 td->td_retval[0] = 0;
2246 if (uap->sbytes != NULL) {
2247 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2250 vm_object_deallocate(obj);
2252 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2254 VFS_UNLOCK_GIANT(vfslocked);
2262 mtx_lock(&sfs->mtx);
2263 if (sfs->count != 0)
2264 cv_wait(&sfs->cv, &sfs->mtx);
2265 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2266 cv_destroy(&sfs->cv);
2267 mtx_destroy(&sfs->mtx);
2271 if (error == ERESTART)
2279 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2280 * otherwise all return EOPNOTSUPP.
2281 * XXX: We should make this loadable one day.
2284 sctp_peeloff(td, uap)
2286 struct sctp_peeloff_args /* {
2291 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2292 struct filedesc *fdp;
2293 struct file *nfp = NULL;
2295 struct socket *head, *so;
2299 fdp = td->td_proc->p_fd;
2300 AUDIT_ARG_FD(uap->sd);
2301 error = fgetsock(td, uap->sd, &head, &fflag);
2304 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2308 * At this point we know we do have a assoc to pull
2309 * we proceed to get the fd setup. This may block
2313 error = falloc(td, &nfp, &fd);
2316 td->td_retval[0] = fd;
2318 so = sonewconn(head, SS_ISCONNECTED);
2322 * Before changing the flags on the socket, we have to bump the
2323 * reference count. Otherwise, if the protocol calls sofree(),
2324 * the socket will be released due to a zero refcount.
2327 soref(so); /* file descriptor reference */
2332 TAILQ_REMOVE(&head->so_comp, so, so_list);
2334 so->so_state |= (head->so_state & SS_NBIO);
2335 so->so_state &= ~SS_NOFDREF;
2336 so->so_qstate &= ~SQ_COMP;
2339 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2340 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2343 if (head->so_sigio != NULL)
2344 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2348 * close the new descriptor, assuming someone hasn't ripped it
2349 * out from under us.
2352 fdclose(fdp, nfp, fd, td);
2355 * Release explicitly held references before returning.
2364 return (EOPNOTSUPP);
2369 sctp_generic_sendmsg (td, uap)
2371 struct sctp_generic_sendmsg_args /* {
2377 struct sctp_sndrcvinfo *sinfo,
2381 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2382 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2384 struct file *fp = NULL;
2385 int use_rcvinfo = 1;
2387 struct sockaddr *to = NULL;
2389 struct uio *ktruio = NULL;
2392 struct iovec iov[1];
2395 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2401 error = getsockaddr(&to, uap->to, uap->tolen);
2408 AUDIT_ARG_FD(uap->sd);
2409 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2413 if (KTRPOINT(td, KTR_STRUCT))
2417 iov[0].iov_base = uap->msg;
2418 iov[0].iov_len = uap->mlen;
2420 so = (struct socket *)fp->f_data;
2422 error = mac_socket_check_send(td->td_ucred, so);
2428 auio.uio_iovcnt = 1;
2429 auio.uio_segflg = UIO_USERSPACE;
2430 auio.uio_rw = UIO_WRITE;
2432 auio.uio_offset = 0; /* XXX */
2434 len = auio.uio_resid = uap->mlen;
2435 error = sctp_lower_sosend(so, to, &auio,
2436 (struct mbuf *)NULL, (struct mbuf *)NULL,
2437 uap->flags, use_rcvinfo, u_sinfo, td);
2439 if (auio.uio_resid != len && (error == ERESTART ||
2440 error == EINTR || error == EWOULDBLOCK))
2442 /* Generation of SIGPIPE can be controlled per socket. */
2443 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2444 !(uap->flags & MSG_NOSIGNAL)) {
2445 PROC_LOCK(td->td_proc);
2446 psignal(td->td_proc, SIGPIPE);
2447 PROC_UNLOCK(td->td_proc);
2451 td->td_retval[0] = len - auio.uio_resid;
2453 if (ktruio != NULL) {
2454 ktruio->uio_resid = td->td_retval[0];
2455 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2466 return (EOPNOTSUPP);
2471 sctp_generic_sendmsg_iov(td, uap)
2473 struct sctp_generic_sendmsg_iov_args /* {
2479 struct sctp_sndrcvinfo *sinfo,
2483 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2484 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2486 struct file *fp = NULL;
2487 int use_rcvinfo = 1;
2488 int error=0, len, i;
2489 struct sockaddr *to = NULL;
2491 struct uio *ktruio = NULL;
2494 struct iovec *iov, *tiov;
2497 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2503 error = getsockaddr(&to, uap->to, uap->tolen);
2510 AUDIT_ARG_FD(uap->sd);
2511 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2515 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2519 if (KTRPOINT(td, KTR_STRUCT))
2523 so = (struct socket *)fp->f_data;
2525 error = mac_socket_check_send(td->td_ucred, so);
2531 auio.uio_iovcnt = uap->iovlen;
2532 auio.uio_segflg = UIO_USERSPACE;
2533 auio.uio_rw = UIO_WRITE;
2535 auio.uio_offset = 0; /* XXX */
2538 for (i = 0; i <uap->iovlen; i++, tiov++) {
2539 if ((auio.uio_resid += tiov->iov_len) < 0) {
2544 len = auio.uio_resid;
2545 error = sctp_lower_sosend(so, to, &auio,
2546 (struct mbuf *)NULL, (struct mbuf *)NULL,
2547 uap->flags, use_rcvinfo, u_sinfo, td);
2549 if (auio.uio_resid != len && (error == ERESTART ||
2550 error == EINTR || error == EWOULDBLOCK))
2552 /* Generation of SIGPIPE can be controlled per socket */
2553 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2554 !(uap->flags & MSG_NOSIGNAL)) {
2555 PROC_LOCK(td->td_proc);
2556 psignal(td->td_proc, SIGPIPE);
2557 PROC_UNLOCK(td->td_proc);
2561 td->td_retval[0] = len - auio.uio_resid;
2563 if (ktruio != NULL) {
2564 ktruio->uio_resid = td->td_retval[0];
2565 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2578 return (EOPNOTSUPP);
2583 sctp_generic_recvmsg(td, uap)
2585 struct sctp_generic_recvmsg_args /* {
2589 struct sockaddr *from,
2590 __socklen_t *fromlenaddr,
2591 struct sctp_sndrcvinfo *sinfo,
2595 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2596 u_int8_t sockbufstore[256];
2598 struct iovec *iov, *tiov;
2599 struct sctp_sndrcvinfo sinfo;
2601 struct file *fp = NULL;
2602 struct sockaddr *fromsa;
2604 int len, i, msg_flags;
2607 struct uio *ktruio = NULL;
2610 AUDIT_ARG_FD(uap->sd);
2611 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2615 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2622 error = mac_socket_check_receive(td->td_ucred, so);
2629 if (uap->fromlenaddr) {
2630 error = copyin(uap->fromlenaddr,
2631 &fromlen, sizeof (fromlen));
2638 if(uap->msg_flags) {
2639 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2647 auio.uio_iovcnt = uap->iovlen;
2648 auio.uio_segflg = UIO_USERSPACE;
2649 auio.uio_rw = UIO_READ;
2651 auio.uio_offset = 0; /* XXX */
2654 for (i = 0; i <uap->iovlen; i++, tiov++) {
2655 if ((auio.uio_resid += tiov->iov_len) < 0) {
2660 len = auio.uio_resid;
2661 fromsa = (struct sockaddr *)sockbufstore;
2664 if (KTRPOINT(td, KTR_GENIO))
2665 ktruio = cloneuio(&auio);
2667 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2668 fromsa, fromlen, &msg_flags,
2669 (struct sctp_sndrcvinfo *)&sinfo, 1);
2671 if (auio.uio_resid != (int)len && (error == ERESTART ||
2672 error == EINTR || error == EWOULDBLOCK))
2676 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2679 if (ktruio != NULL) {
2680 ktruio->uio_resid = (int)len - auio.uio_resid;
2681 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2686 td->td_retval[0] = (int)len - auio.uio_resid;
2688 if (fromlen && uap->from) {
2690 if (len <= 0 || fromsa == 0)
2693 len = MIN(len, fromsa->sa_len);
2694 error = copyout(fromsa, uap->from, (unsigned)len);
2698 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2704 if (KTRPOINT(td, KTR_STRUCT))
2705 ktrsockaddr(fromsa);
2707 if (uap->msg_flags) {
2708 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2721 return (EOPNOTSUPP);