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_compat.h"
40 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/mutex.h>
48 #include <sys/sysproto.h>
49 #include <sys/malloc.h>
50 #include <sys/filedesc.h>
51 #include <sys/event.h>
53 #include <sys/fcntl.h>
55 #include <sys/filio.h>
56 #include <sys/mount.h>
58 #include <sys/protosw.h>
59 #include <sys/sf_buf.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/signalvar.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
68 #include <sys/ktrace.h>
71 #include <security/mac/mac_framework.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_pageout.h>
77 #include <vm/vm_kern.h>
78 #include <vm/vm_extern.h>
81 #include <netinet/sctp.h>
82 #include <netinet/sctp_peeloff.h>
85 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
86 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
88 static int accept1(struct thread *td, struct accept_args *uap, int compat);
89 static int do_sendfile(struct thread *td, struct sendfile_args *uap, int compat);
90 static int getsockname1(struct thread *td, struct getsockname_args *uap,
92 static int getpeername1(struct thread *td, struct getpeername_args *uap,
96 * NSFBUFS-related variables and associated sysctls
102 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
103 "Maximum number of sendfile(2) sf_bufs available");
104 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
105 "Number of sendfile(2) sf_bufs at peak usage");
106 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
107 "Number of sendfile(2) sf_bufs in use");
110 * Convert a user file descriptor to a kernel file entry. A reference on the
111 * file entry is held upon returning. This is lighter weight than
112 * fgetsock(), which bumps the socket reference drops the file reference
113 * count instead, as this approach avoids several additional mutex operations
114 * associated with the additional reference count. If requested, return the
118 getsock(struct filedesc *fdp, int fd, struct file **fpp, u_int *fflagp)
128 fp = fget_locked(fdp, fd);
131 else if (fp->f_type != DTYPE_SOCKET) {
137 *fflagp = fp->f_flag;
140 FILEDESC_SUNLOCK(fdp);
147 * System call interface to the socket abstraction.
149 #if defined(COMPAT_43)
150 #define COMPAT_OLDSOCK
156 struct socket_args /* {
162 struct filedesc *fdp;
168 error = mac_check_socket_create(td->td_ucred, uap->domain, uap->type,
173 fdp = td->td_proc->p_fd;
174 error = falloc(td, &fp, &fd);
177 /* An extra reference on `fp' has been held for us by falloc(). */
178 error = socreate(uap->domain, &so, uap->type, uap->protocol,
181 fdclose(fdp, fp, fd, td);
184 fp->f_data = so; /* already has ref count */
185 fp->f_flag = FREAD|FWRITE;
186 fp->f_type = DTYPE_SOCKET;
187 fp->f_ops = &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)
226 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
231 if (KTRPOINT(td, KTR_STRUCT))
236 error = mac_check_socket_bind(td->td_ucred, so, sa);
241 error = sobind(so, sa, td);
253 struct listen_args /* {
262 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
267 error = mac_check_socket_listen(td->td_ucred, so);
272 error = solisten(so, uap->backlog, td);
285 accept1(td, uap, compat)
287 struct accept_args /* {
289 struct sockaddr * __restrict name;
290 socklen_t * __restrict anamelen;
294 struct sockaddr *name;
299 if (uap->name == NULL)
300 return (kern_accept(td, uap->s, NULL, NULL, NULL));
302 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
306 error = kern_accept(td, uap->s, &name, &namelen, &fp);
309 * return a namelen of zero for older code which might
310 * ignore the return value from accept.
313 (void) copyout(&namelen,
314 uap->anamelen, sizeof(*uap->anamelen));
318 if (error == 0 && name != NULL) {
319 #ifdef COMPAT_OLDSOCK
321 ((struct osockaddr *)name)->sa_family =
324 error = copyout(name, uap->name, namelen);
327 error = copyout(&namelen, uap->anamelen,
330 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
332 free(name, M_SONAME);
337 kern_accept(struct thread *td, int s, struct sockaddr **name,
338 socklen_t *namelen, struct file **fp)
340 struct filedesc *fdp;
341 struct file *headfp, *nfp = NULL;
342 struct sockaddr *sa = NULL;
344 struct socket *head, *so;
356 fdp = td->td_proc->p_fd;
357 error = getsock(fdp, s, &headfp, &fflag);
360 head = headfp->f_data;
361 if ((head->so_options & SO_ACCEPTCONN) == 0) {
367 error = mac_check_socket_accept(td->td_ucred, head);
372 error = falloc(td, &nfp, &fd);
376 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
381 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
382 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
383 head->so_error = ECONNABORTED;
386 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
393 if (head->so_error) {
394 error = head->so_error;
399 so = TAILQ_FIRST(&head->so_comp);
400 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
401 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
404 * Before changing the flags on the socket, we have to bump the
405 * reference count. Otherwise, if the protocol calls sofree(),
406 * the socket will be released due to a zero refcount.
408 SOCK_LOCK(so); /* soref() and so_state update */
409 soref(so); /* file descriptor reference */
411 TAILQ_REMOVE(&head->so_comp, so, so_list);
413 so->so_state |= (head->so_state & SS_NBIO);
414 so->so_qstate &= ~SQ_COMP;
420 /* An extra reference on `nfp' has been held for us by falloc(). */
421 td->td_retval[0] = fd;
423 /* connection has been removed from the listen queue */
424 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
426 pgid = fgetown(&head->so_sigio);
428 fsetown(pgid, &so->so_sigio);
431 nfp->f_data = so; /* nfp has ref count from falloc */
433 nfp->f_type = DTYPE_SOCKET;
434 nfp->f_ops = &socketops;
436 /* Sync socket nonblocking/async state with file flags */
437 tmp = fflag & FNONBLOCK;
438 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
439 tmp = fflag & FASYNC;
440 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
442 error = soaccept(so, &sa);
445 * return a namelen of zero for older code which might
446 * ignore the return value from accept.
458 /* check sa_len before it is destroyed */
459 if (*namelen > sa->sa_len)
460 *namelen = sa->sa_len;
462 if (KTRPOINT(td, KTR_STRUCT))
473 * close the new descriptor, assuming someone hasn't ripped it
477 fdclose(fdp, nfp, fd, td);
480 * Release explicitly held references before returning. We return
481 * a reference on nfp to the caller on success if they request it.
500 struct accept_args *uap;
503 return (accept1(td, uap, 0));
506 #ifdef COMPAT_OLDSOCK
510 struct accept_args *uap;
513 return (accept1(td, uap, 1));
515 #endif /* COMPAT_OLDSOCK */
521 struct connect_args /* {
530 error = getsockaddr(&sa, uap->name, uap->namelen);
534 error = kern_connect(td, uap->s, sa);
541 kern_connect(td, fd, sa)
551 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
555 if (so->so_state & SS_ISCONNECTING) {
560 if (KTRPOINT(td, KTR_STRUCT))
565 error = mac_check_socket_connect(td->td_ucred, so, sa);
570 error = soconnect(so, sa, td);
573 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
578 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
579 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
582 if (error == EINTR || error == ERESTART)
588 error = so->so_error;
594 so->so_state &= ~SS_ISCONNECTING;
595 if (error == ERESTART)
605 struct socketpair_args /* {
612 struct filedesc *fdp = td->td_proc->p_fd;
613 struct file *fp1, *fp2;
614 struct socket *so1, *so2;
615 int fd, error, sv[2];
618 /* We might want to have a separate check for socket pairs. */
619 error = mac_check_socket_create(td->td_ucred, uap->domain, uap->type,
625 error = socreate(uap->domain, &so1, uap->type, uap->protocol,
629 error = socreate(uap->domain, &so2, uap->type, uap->protocol,
633 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
634 error = falloc(td, &fp1, &fd);
638 fp1->f_data = so1; /* so1 already has ref count */
639 error = falloc(td, &fp2, &fd);
642 fp2->f_data = so2; /* so2 already has ref count */
644 error = soconnect2(so1, so2);
647 if (uap->type == SOCK_DGRAM) {
649 * Datagram socket connection is asymmetric.
651 error = soconnect2(so2, so1);
656 fp1->f_flag = FREAD|FWRITE;
657 fp1->f_type = DTYPE_SOCKET;
658 fp1->f_ops = &socketops;
661 fp2->f_flag = FREAD|FWRITE;
662 fp2->f_type = DTYPE_SOCKET;
663 fp2->f_ops = &socketops;
666 error = copyout(sv, uap->rsv, 2 * sizeof (int));
673 fdclose(fdp, fp2, sv[1], td);
676 fdclose(fdp, fp1, sv[0], td);
688 sendit(td, s, mp, flags)
694 struct mbuf *control;
698 if (mp->msg_name != NULL) {
699 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
709 if (mp->msg_control) {
710 if (mp->msg_controllen < sizeof(struct cmsghdr)
711 #ifdef COMPAT_OLDSOCK
712 && mp->msg_flags != MSG_COMPAT
718 error = sockargs(&control, mp->msg_control,
719 mp->msg_controllen, MT_CONTROL);
722 #ifdef COMPAT_OLDSOCK
723 if (mp->msg_flags == MSG_COMPAT) {
726 M_PREPEND(control, sizeof(*cm), M_TRYWAIT);
731 cm = mtod(control, struct cmsghdr *);
732 cm->cmsg_len = control->m_len;
733 cm->cmsg_level = SOL_SOCKET;
734 cm->cmsg_type = SCM_RIGHTS;
742 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
751 kern_sendit(td, s, mp, flags, control, segflg)
756 struct mbuf *control;
766 struct uio *ktruio = NULL;
769 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
772 so = (struct socket *)fp->f_data;
776 if (mp->msg_name != NULL)
777 error = mac_check_socket_connect(td->td_ucred, so,
780 error = mac_check_socket_send(td->td_ucred, so);
786 auio.uio_iov = mp->msg_iov;
787 auio.uio_iovcnt = mp->msg_iovlen;
788 auio.uio_segflg = segflg;
789 auio.uio_rw = UIO_WRITE;
791 auio.uio_offset = 0; /* XXX */
794 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
795 if ((auio.uio_resid += iov->iov_len) < 0) {
801 if (KTRPOINT(td, KTR_GENIO))
802 ktruio = cloneuio(&auio);
804 len = auio.uio_resid;
805 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
807 if (auio.uio_resid != len && (error == ERESTART ||
808 error == EINTR || error == EWOULDBLOCK))
810 /* Generation of SIGPIPE can be controlled per socket */
811 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
812 !(flags & MSG_NOSIGNAL)) {
813 PROC_LOCK(td->td_proc);
814 psignal(td->td_proc, SIGPIPE);
815 PROC_UNLOCK(td->td_proc);
819 td->td_retval[0] = len - auio.uio_resid;
821 if (ktruio != NULL) {
822 ktruio->uio_resid = td->td_retval[0];
823 ktrgenio(s, UIO_WRITE, ktruio, error);
834 struct sendto_args /* {
847 msg.msg_name = uap->to;
848 msg.msg_namelen = uap->tolen;
852 #ifdef COMPAT_OLDSOCK
855 aiov.iov_base = uap->buf;
856 aiov.iov_len = uap->len;
857 error = sendit(td, uap->s, &msg, uap->flags);
861 #ifdef COMPAT_OLDSOCK
865 struct osend_args /* {
880 aiov.iov_base = uap->buf;
881 aiov.iov_len = uap->len;
884 error = sendit(td, uap->s, &msg, uap->flags);
891 struct osendmsg_args /* {
901 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
904 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
908 msg.msg_flags = MSG_COMPAT;
909 error = sendit(td, uap->s, &msg, uap->flags);
918 struct sendmsg_args /* {
928 error = copyin(uap->msg, &msg, sizeof (msg));
931 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
935 #ifdef COMPAT_OLDSOCK
938 error = sendit(td, uap->s, &msg, uap->flags);
944 kern_recvit(td, s, mp, fromseg, controlp)
948 enum uio_seg fromseg;
949 struct mbuf **controlp;
956 struct mbuf *m, *control = 0;
960 struct sockaddr *fromsa = 0;
962 struct uio *ktruio = NULL;
968 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
975 error = mac_check_socket_receive(td->td_ucred, so);
983 auio.uio_iov = mp->msg_iov;
984 auio.uio_iovcnt = mp->msg_iovlen;
985 auio.uio_segflg = UIO_USERSPACE;
986 auio.uio_rw = UIO_READ;
988 auio.uio_offset = 0; /* XXX */
991 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
992 if ((auio.uio_resid += iov->iov_len) < 0) {
998 if (KTRPOINT(td, KTR_GENIO))
999 ktruio = cloneuio(&auio);
1001 len = auio.uio_resid;
1002 error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
1003 (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
1006 if (auio.uio_resid != (int)len && (error == ERESTART ||
1007 error == EINTR || error == EWOULDBLOCK))
1011 if (ktruio != NULL) {
1012 ktruio->uio_resid = (int)len - auio.uio_resid;
1013 ktrgenio(s, UIO_READ, ktruio, error);
1018 td->td_retval[0] = (int)len - auio.uio_resid;
1020 len = mp->msg_namelen;
1021 if (len <= 0 || fromsa == 0)
1024 /* save sa_len before it is destroyed by MSG_COMPAT */
1025 len = MIN(len, fromsa->sa_len);
1026 #ifdef COMPAT_OLDSOCK
1027 if (mp->msg_flags & MSG_COMPAT)
1028 ((struct osockaddr *)fromsa)->sa_family =
1031 if (fromseg == UIO_USERSPACE) {
1032 error = copyout(fromsa, mp->msg_name,
1037 bcopy(fromsa, mp->msg_name, len);
1039 mp->msg_namelen = len;
1041 if (mp->msg_control && controlp == NULL) {
1042 #ifdef COMPAT_OLDSOCK
1044 * We assume that old recvmsg calls won't receive access
1045 * rights and other control info, esp. as control info
1046 * is always optional and those options didn't exist in 4.3.
1047 * If we receive rights, trim the cmsghdr; anything else
1050 if (control && mp->msg_flags & MSG_COMPAT) {
1051 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1053 mtod(control, struct cmsghdr *)->cmsg_type !=
1055 mp->msg_controllen = 0;
1058 control->m_len -= sizeof (struct cmsghdr);
1059 control->m_data += sizeof (struct cmsghdr);
1062 len = mp->msg_controllen;
1064 mp->msg_controllen = 0;
1065 ctlbuf = mp->msg_control;
1067 while (m && len > 0) {
1068 unsigned int tocopy;
1070 if (len >= m->m_len)
1073 mp->msg_flags |= MSG_CTRUNC;
1077 if ((error = copyout(mtod(m, caddr_t),
1078 ctlbuf, tocopy)) != 0)
1085 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1090 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1091 ktrsockaddr(fromsa);
1094 FREE(fromsa, M_SONAME);
1096 if (error == 0 && controlp != NULL)
1097 *controlp = control;
1105 recvit(td, s, mp, namelenp)
1113 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1117 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1118 #ifdef COMPAT_OLDSOCK
1119 if (mp->msg_flags & MSG_COMPAT)
1120 error = 0; /* old recvfrom didn't check */
1129 struct recvfrom_args /* {
1134 struct sockaddr * __restrict from;
1135 socklen_t * __restrict fromlenaddr;
1142 if (uap->fromlenaddr) {
1143 error = copyin(uap->fromlenaddr,
1144 &msg.msg_namelen, sizeof (msg.msg_namelen));
1148 msg.msg_namelen = 0;
1150 msg.msg_name = uap->from;
1151 msg.msg_iov = &aiov;
1153 aiov.iov_base = uap->buf;
1154 aiov.iov_len = uap->len;
1155 msg.msg_control = 0;
1156 msg.msg_flags = uap->flags;
1157 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1162 #ifdef COMPAT_OLDSOCK
1166 struct recvfrom_args *uap;
1169 uap->flags |= MSG_COMPAT;
1170 return (recvfrom(td, uap));
1174 #ifdef COMPAT_OLDSOCK
1178 struct orecv_args /* {
1190 msg.msg_namelen = 0;
1191 msg.msg_iov = &aiov;
1193 aiov.iov_base = uap->buf;
1194 aiov.iov_len = uap->len;
1195 msg.msg_control = 0;
1196 msg.msg_flags = uap->flags;
1197 error = recvit(td, uap->s, &msg, NULL);
1202 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1203 * overlays the new one, missing only the flags, and with the (old) access
1204 * rights where the control fields are now.
1209 struct orecvmsg_args /* {
1211 struct omsghdr *msg;
1219 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1222 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1225 msg.msg_flags = uap->flags | MSG_COMPAT;
1227 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1228 if (msg.msg_controllen && error == 0)
1229 error = copyout(&msg.msg_controllen,
1230 &uap->msg->msg_accrightslen, sizeof (int));
1239 struct recvmsg_args /* {
1246 struct iovec *uiov, *iov;
1249 error = copyin(uap->msg, &msg, sizeof (msg));
1252 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1255 msg.msg_flags = uap->flags;
1256 #ifdef COMPAT_OLDSOCK
1257 msg.msg_flags &= ~MSG_COMPAT;
1261 error = recvit(td, uap->s, &msg, NULL);
1264 error = copyout(&msg, uap->msg, sizeof(msg));
1274 struct shutdown_args /* {
1283 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
1286 error = soshutdown(so, uap->how);
1296 struct setsockopt_args /* {
1305 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1306 uap->val, UIO_USERSPACE, uap->valsize));
1310 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1316 enum uio_seg valseg;
1322 struct sockopt sopt;
1324 if (val == NULL && valsize != 0)
1326 if ((int)valsize < 0)
1329 sopt.sopt_dir = SOPT_SET;
1330 sopt.sopt_level = level;
1331 sopt.sopt_name = name;
1332 sopt.sopt_val = val;
1333 sopt.sopt_valsize = valsize;
1339 sopt.sopt_td = NULL;
1342 panic("kern_setsockopt called with bad valseg");
1345 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1348 error = sosetopt(so, &sopt);
1358 struct getsockopt_args /* {
1362 void * __restrict val;
1363 socklen_t * __restrict avalsize;
1370 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1375 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1376 uap->val, UIO_USERSPACE, &valsize);
1379 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1384 * Kernel version of getsockopt.
1385 * optval can be a userland or userspace. optlen is always a kernel pointer.
1388 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1394 enum uio_seg valseg;
1400 struct sockopt sopt;
1404 if ((int)*valsize < 0)
1407 sopt.sopt_dir = SOPT_GET;
1408 sopt.sopt_level = level;
1409 sopt.sopt_name = name;
1410 sopt.sopt_val = val;
1411 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1417 sopt.sopt_td = NULL;
1420 panic("kern_getsockopt called with bad valseg");
1423 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1426 error = sogetopt(so, &sopt);
1427 *valsize = sopt.sopt_valsize;
1434 * getsockname1() - Get socket name.
1438 getsockname1(td, uap, compat)
1440 struct getsockname_args /* {
1442 struct sockaddr * __restrict asa;
1443 socklen_t * __restrict alen;
1447 struct sockaddr *sa;
1451 error = copyin(uap->alen, &len, sizeof(len));
1455 error = kern_getsockname(td, uap->fdes, &sa, &len);
1460 #ifdef COMPAT_OLDSOCK
1462 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1464 error = copyout(sa, uap->asa, (u_int)len);
1468 error = copyout(&len, uap->alen, sizeof(len));
1473 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1484 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1489 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1495 len = MIN(*alen, (*sa)->sa_len);
1498 if (KTRPOINT(td, KTR_STRUCT))
1504 free(*sa, M_SONAME);
1511 getsockname(td, uap)
1513 struct getsockname_args *uap;
1516 return (getsockname1(td, uap, 0));
1519 #ifdef COMPAT_OLDSOCK
1521 ogetsockname(td, uap)
1523 struct getsockname_args *uap;
1526 return (getsockname1(td, uap, 1));
1528 #endif /* COMPAT_OLDSOCK */
1531 * getpeername1() - Get name of peer for connected socket.
1535 getpeername1(td, uap, compat)
1537 struct getpeername_args /* {
1539 struct sockaddr * __restrict asa;
1540 socklen_t * __restrict alen;
1544 struct sockaddr *sa;
1548 error = copyin(uap->alen, &len, sizeof (len));
1552 error = kern_getpeername(td, uap->fdes, &sa, &len);
1557 #ifdef COMPAT_OLDSOCK
1559 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1561 error = copyout(sa, uap->asa, (u_int)len);
1565 error = copyout(&len, uap->alen, sizeof(len));
1570 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1581 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1585 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1590 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1596 len = MIN(*alen, (*sa)->sa_len);
1599 if (KTRPOINT(td, KTR_STRUCT))
1604 free(*sa, M_SONAME);
1613 getpeername(td, uap)
1615 struct getpeername_args *uap;
1618 return (getpeername1(td, uap, 0));
1621 #ifdef COMPAT_OLDSOCK
1623 ogetpeername(td, uap)
1625 struct ogetpeername_args *uap;
1628 /* XXX uap should have type `getpeername_args *' to begin with. */
1629 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1631 #endif /* COMPAT_OLDSOCK */
1634 sockargs(mp, buf, buflen, type)
1639 struct sockaddr *sa;
1643 if ((u_int)buflen > MLEN) {
1644 #ifdef COMPAT_OLDSOCK
1645 if (type == MT_SONAME && (u_int)buflen <= 112)
1646 buflen = MLEN; /* unix domain compat. hack */
1649 if ((u_int)buflen > MCLBYTES)
1652 m = m_get(M_TRYWAIT, type);
1655 if ((u_int)buflen > MLEN) {
1656 MCLGET(m, M_TRYWAIT);
1657 if ((m->m_flags & M_EXT) == 0) {
1663 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1668 if (type == MT_SONAME) {
1669 sa = mtod(m, struct sockaddr *);
1671 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1672 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1673 sa->sa_family = sa->sa_len;
1675 sa->sa_len = buflen;
1682 getsockaddr(namp, uaddr, len)
1683 struct sockaddr **namp;
1687 struct sockaddr *sa;
1690 if (len > SOCK_MAXADDRLEN)
1691 return (ENAMETOOLONG);
1692 if (len < offsetof(struct sockaddr, sa_data[0]))
1694 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1695 error = copyin(uaddr, sa, len);
1699 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1700 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1701 sa->sa_family = sa->sa_len;
1710 * Detach mapped page and release resources back to the system.
1713 sf_buf_mext(void *addr, void *args)
1717 m = sf_buf_page(args);
1719 vm_page_lock_queues();
1720 vm_page_unwire(m, 0);
1722 * Check for the object going away on us. This can
1723 * happen since we don't hold a reference to it.
1724 * If so, we're responsible for freeing the page.
1726 if (m->wire_count == 0 && m->object == NULL)
1728 vm_page_unlock_queues();
1734 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1735 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1737 * Send a file specified by 'fd' and starting at 'offset' to a socket
1738 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1739 * 0. Optionally add a header and/or trailer to the socket output. If
1740 * specified, write the total number of bytes sent into *sbytes.
1743 sendfile(struct thread *td, struct sendfile_args *uap)
1746 return (do_sendfile(td, uap, 0));
1750 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1752 struct sf_hdtr hdtr;
1753 struct uio *hdr_uio, *trl_uio;
1756 hdr_uio = trl_uio = NULL;
1758 if (uap->hdtr != NULL) {
1759 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1762 if (hdtr.headers != NULL) {
1763 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1767 if (hdtr.trailers != NULL) {
1768 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1775 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1778 free(hdr_uio, M_IOV);
1780 free(trl_uio, M_IOV);
1784 #ifdef COMPAT_FREEBSD4
1786 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1788 struct sendfile_args args;
1792 args.offset = uap->offset;
1793 args.nbytes = uap->nbytes;
1794 args.hdtr = uap->hdtr;
1795 args.sbytes = uap->sbytes;
1796 args.flags = uap->flags;
1798 return (do_sendfile(td, &args, 1));
1800 #endif /* COMPAT_FREEBSD4 */
1803 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1804 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1806 struct file *sock_fp;
1808 struct vm_object *obj = NULL;
1809 struct socket *so = NULL;
1810 struct mbuf *m = NULL;
1813 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1814 int error, hdrlen = 0, mnw = 0;
1818 * The file descriptor must be a regular file and have a
1819 * backing VM object.
1820 * File offset must be positive. If it goes beyond EOF
1821 * we send only the header/trailer and no payload data.
1823 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1825 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1826 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
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);
1846 VOP_UNLOCK(vp, 0, td);
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)
1883 error = mac_check_socket_send(td->td_ucred, so);
1889 /* If headers are specified copy them into mbufs. */
1890 if (hdr_uio != NULL) {
1891 hdr_uio->uio_td = td;
1892 hdr_uio->uio_rw = UIO_WRITE;
1893 if (hdr_uio->uio_resid > 0) {
1895 * In FBSD < 5.0 the nbytes to send also included
1896 * the header. If compat is specified subtract the
1897 * header size from nbytes.
1900 if (uap->nbytes > hdr_uio->uio_resid)
1901 uap->nbytes -= hdr_uio->uio_resid;
1905 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1908 error = mnw ? EAGAIN : ENOBUFS;
1911 hdrlen = m_length(m, NULL);
1916 * Protect against multiple writers to the socket.
1918 * XXXRW: Historically this has assumed non-interruptibility, so now
1919 * we implement that, but possibly shouldn't.
1921 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1924 * Loop through the pages of the file, starting with the requested
1925 * offset. Get a file page (do I/O if necessary), map the file page
1926 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1928 * This is done in two loops. The inner loop turns as many pages
1929 * as it can, up to available socket buffer space, without blocking
1930 * into mbufs to have it bulk delivered into the socket send buffer.
1931 * The outer loop checks the state and available space of the socket
1932 * and takes care of the overall progress.
1934 for (off = uap->offset, rem = uap->nbytes; ; ) {
1940 * Check the socket state for ongoing connection,
1941 * no errors and space in socket buffer.
1942 * If space is low allow for the remainder of the
1943 * file to be processed if it fits the socket buffer.
1944 * Otherwise block in waiting for sufficient space
1945 * to proceed, or if the socket is nonblocking, return
1946 * to userland with EAGAIN while reporting how far
1948 * We wait until the socket buffer has significant free
1949 * space to do bulk sends. This makes good use of file
1950 * system read ahead and allows packet segmentation
1951 * offloading hardware to take over lots of work. If
1952 * we were not careful here we would send off only one
1955 SOCKBUF_LOCK(&so->so_snd);
1956 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1957 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1959 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1961 SOCKBUF_UNLOCK(&so->so_snd);
1963 } else if (so->so_error) {
1964 error = so->so_error;
1966 SOCKBUF_UNLOCK(&so->so_snd);
1969 space = sbspace(&so->so_snd);
1972 space < so->so_snd.sb_lowat)) {
1973 if (so->so_state & SS_NBIO) {
1974 SOCKBUF_UNLOCK(&so->so_snd);
1979 * sbwait drops the lock while sleeping.
1980 * When we loop back to retry_space the
1981 * state may have changed and we retest
1984 error = sbwait(&so->so_snd);
1986 * An error from sbwait usually indicates that we've
1987 * been interrupted by a signal. If we've sent anything
1988 * then return bytes sent, otherwise return the error.
1991 SOCKBUF_UNLOCK(&so->so_snd);
1996 SOCKBUF_UNLOCK(&so->so_snd);
1999 * Reduce space in the socket buffer by the size of
2000 * the header mbuf chain.
2001 * hdrlen is set to 0 after the first loop.
2006 * Loop and construct maximum sized mbuf chain to be bulk
2007 * dumped into socket buffer.
2009 while(space > loopbytes) {
2014 VM_OBJECT_LOCK(obj);
2016 * Calculate the amount to transfer.
2017 * Not to exceed a page, the EOF,
2018 * or the passed in nbytes.
2020 pgoff = (vm_offset_t)(off & PAGE_MASK);
2021 xfsize = omin(PAGE_SIZE - pgoff,
2022 obj->un_pager.vnp.vnp_size - uap->offset -
2023 fsbytes - loopbytes);
2025 rem = (uap->nbytes - fsbytes - loopbytes);
2027 rem = obj->un_pager.vnp.vnp_size -
2028 uap->offset - fsbytes - loopbytes;
2029 xfsize = omin(rem, xfsize);
2031 VM_OBJECT_UNLOCK(obj);
2032 done = 1; /* all data sent */
2036 * Don't overflow the send buffer.
2037 * Stop here and send out what we've
2040 if (space < loopbytes + xfsize) {
2041 VM_OBJECT_UNLOCK(obj);
2046 * Attempt to look up the page. Allocate
2047 * if not found or wait and loop if busy.
2049 pindex = OFF_TO_IDX(off);
2050 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2051 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2054 * Check if page is valid for what we need,
2055 * otherwise initiate I/O.
2056 * If we already turned some pages into mbufs,
2057 * send them off before we come here again and
2060 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2061 VM_OBJECT_UNLOCK(obj);
2063 error = EAGAIN; /* send what we already got */
2064 else if (uap->flags & SF_NODISKIO)
2070 * Ensure that our page is still around
2071 * when the I/O completes.
2073 vm_page_io_start(pg);
2074 VM_OBJECT_UNLOCK(obj);
2077 * Get the page from backing store.
2079 bsize = vp->v_mount->mnt_stat.f_iosize;
2080 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2081 vn_lock(vp, LK_SHARED | LK_RETRY, td);
2084 * XXXMAC: Because we don't have fp->f_cred
2085 * here, we pass in NOCRED. This is probably
2086 * wrong, but is consistent with our original
2089 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2090 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2091 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2092 td->td_ucred, NOCRED, &resid, td);
2093 VOP_UNLOCK(vp, 0, 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. We usually wait as long
2123 * as necessary, but this wait can be interrupted.
2125 if ((sf = sf_buf_alloc(pg,
2126 (mnw ? SFB_NOWAIT : SFB_CATCH))) == NULL) {
2127 mbstat.sf_allocfail++;
2128 vm_page_lock_queues();
2129 vm_page_unwire(pg, 0);
2131 * XXX: Not same check as above!?
2133 if (pg->wire_count == 0 && pg->object == NULL)
2135 vm_page_unlock_queues();
2136 error = (mnw ? EAGAIN : EINTR);
2141 * Get an mbuf and set it up as having
2144 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2146 error = (mnw ? EAGAIN : ENOBUFS);
2147 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2150 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2151 sf, M_RDONLY, EXT_SFBUF);
2152 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2155 /* Append to mbuf chain. */
2161 /* Keep track of bits processed. */
2162 loopbytes += xfsize;
2166 /* Add the buffer chain to the socket buffer. */
2170 mlen = m_length(m, NULL);
2171 SOCKBUF_LOCK(&so->so_snd);
2172 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2174 SOCKBUF_UNLOCK(&so->so_snd);
2177 SOCKBUF_UNLOCK(&so->so_snd);
2178 /* Avoid error aliasing. */
2179 err = (*so->so_proto->pr_usrreqs->pru_send)
2180 (so, 0, m, NULL, NULL, td);
2183 * We need two counters to get the
2184 * file offset and nbytes to send
2186 * - sbytes contains the total amount
2187 * of bytes sent, including headers.
2188 * - fsbytes contains the total amount
2189 * of bytes sent from the file.
2197 } else if (error == 0)
2199 m = NULL; /* pru_send always consumes */
2202 /* Quit outer loop on error or when we're done. */
2210 * Send trailers. Wimp out and use writev(2).
2212 if (trl_uio != NULL) {
2213 sbunlock(&so->so_snd);
2214 error = kern_writev(td, uap->s, trl_uio);
2216 sbytes += td->td_retval[0];
2221 sbunlock(&so->so_snd);
2224 * If there was no error we have to clear td->td_retval[0]
2225 * because it may have been set by writev.
2228 td->td_retval[0] = 0;
2230 if (uap->sbytes != NULL) {
2231 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2234 vm_object_deallocate(obj);
2236 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2238 VFS_UNLOCK_GIANT(vfslocked);
2245 if (error == ERESTART)
2253 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2254 * otherwise all return EOPNOTSUPP.
2255 * XXX: We should make this loadable one day.
2258 sctp_peeloff(td, uap)
2260 struct sctp_peeloff_args /* {
2266 struct filedesc *fdp;
2267 struct file *nfp = NULL;
2269 struct socket *head, *so;
2273 fdp = td->td_proc->p_fd;
2274 error = fgetsock(td, uap->sd, &head, &fflag);
2277 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2281 * At this point we know we do have a assoc to pull
2282 * we proceed to get the fd setup. This may block
2286 error = falloc(td, &nfp, &fd);
2289 td->td_retval[0] = fd;
2291 so = sonewconn(head, SS_ISCONNECTED);
2295 * Before changing the flags on the socket, we have to bump the
2296 * reference count. Otherwise, if the protocol calls sofree(),
2297 * the socket will be released due to a zero refcount.
2300 soref(so); /* file descriptor reference */
2305 TAILQ_REMOVE(&head->so_comp, so, so_list);
2307 so->so_state |= (head->so_state & SS_NBIO);
2308 so->so_state &= ~SS_NOFDREF;
2309 so->so_qstate &= ~SQ_COMP;
2314 nfp->f_flag = fflag;
2315 nfp->f_type = DTYPE_SOCKET;
2316 nfp->f_ops = &socketops;
2318 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2321 if (head->so_sigio != NULL)
2322 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2326 * close the new descriptor, assuming someone hasn't ripped it
2327 * out from under us.
2330 fdclose(fdp, nfp, fd, td);
2333 * Release explicitly held references before returning.
2342 return (EOPNOTSUPP);
2347 sctp_generic_sendmsg (td, uap)
2349 struct sctp_generic_sendmsg_args /* {
2355 struct sctp_sndrcvinfo *sinfo,
2360 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2362 struct file *fp = NULL;
2363 int use_rcvinfo = 1;
2365 struct sockaddr *to = NULL;
2367 struct uio *ktruio = NULL;
2370 struct iovec iov[1];
2373 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2379 error = getsockaddr(&to, uap->to, uap->tolen);
2386 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2390 if (KTRPOINT(td, KTR_STRUCT))
2394 iov[0].iov_base = uap->msg;
2395 iov[0].iov_len = uap->mlen;
2397 so = (struct socket *)fp->f_data;
2400 error = mac_check_socket_send(td->td_ucred, so);
2407 auio.uio_iovcnt = 1;
2408 auio.uio_segflg = UIO_USERSPACE;
2409 auio.uio_rw = UIO_WRITE;
2411 auio.uio_offset = 0; /* XXX */
2413 len = auio.uio_resid = uap->mlen;
2414 error = sctp_lower_sosend(so, to, &auio,
2415 (struct mbuf *)NULL, (struct mbuf *)NULL,
2416 uap->flags, use_rcvinfo, u_sinfo, td);
2418 if (auio.uio_resid != len && (error == ERESTART ||
2419 error == EINTR || error == EWOULDBLOCK))
2421 /* Generation of SIGPIPE can be controlled per socket. */
2422 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2423 !(uap->flags & MSG_NOSIGNAL)) {
2424 PROC_LOCK(td->td_proc);
2425 psignal(td->td_proc, SIGPIPE);
2426 PROC_UNLOCK(td->td_proc);
2430 td->td_retval[0] = len - auio.uio_resid;
2432 if (ktruio != NULL) {
2433 ktruio->uio_resid = td->td_retval[0];
2434 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2445 return (EOPNOTSUPP);
2450 sctp_generic_sendmsg_iov(td, uap)
2452 struct sctp_generic_sendmsg_iov_args /* {
2458 struct sctp_sndrcvinfo *sinfo,
2463 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2465 struct file *fp = NULL;
2466 int use_rcvinfo = 1;
2467 int error=0, len, i;
2468 struct sockaddr *to = NULL;
2470 struct uio *ktruio = NULL;
2473 struct iovec *iov, *tiov;
2476 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2482 error = getsockaddr(&to, uap->to, uap->tolen);
2489 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2493 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2497 if (KTRPOINT(td, KTR_STRUCT))
2501 so = (struct socket *)fp->f_data;
2504 error = mac_check_socket_send(td->td_ucred, so);
2511 auio.uio_iovcnt = uap->iovlen;
2512 auio.uio_segflg = UIO_USERSPACE;
2513 auio.uio_rw = UIO_WRITE;
2515 auio.uio_offset = 0; /* XXX */
2518 for (i = 0; i <uap->iovlen; i++, tiov++) {
2519 if ((auio.uio_resid += tiov->iov_len) < 0) {
2524 len = auio.uio_resid;
2525 error = sctp_lower_sosend(so, to, &auio,
2526 (struct mbuf *)NULL, (struct mbuf *)NULL,
2527 uap->flags, use_rcvinfo, u_sinfo, td);
2529 if (auio.uio_resid != len && (error == ERESTART ||
2530 error == EINTR || error == EWOULDBLOCK))
2532 /* Generation of SIGPIPE can be controlled per socket */
2533 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2534 !(uap->flags & MSG_NOSIGNAL)) {
2535 PROC_LOCK(td->td_proc);
2536 psignal(td->td_proc, SIGPIPE);
2537 PROC_UNLOCK(td->td_proc);
2541 td->td_retval[0] = len - auio.uio_resid;
2543 if (ktruio != NULL) {
2544 ktruio->uio_resid = td->td_retval[0];
2545 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2558 return (EOPNOTSUPP);
2563 sctp_generic_recvmsg(td, uap)
2565 struct sctp_generic_recvmsg_args /* {
2569 struct sockaddr *from,
2570 __socklen_t *fromlenaddr,
2571 struct sctp_sndrcvinfo *sinfo,
2576 u_int8_t sockbufstore[256];
2578 struct iovec *iov, *tiov;
2579 struct sctp_sndrcvinfo sinfo;
2581 struct file *fp = NULL;
2582 struct sockaddr *fromsa;
2584 int len, i, msg_flags;
2587 struct uio *ktruio = NULL;
2589 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2593 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2601 error = mac_check_socket_receive(td->td_ucred, so);
2609 if (uap->fromlenaddr) {
2610 error = copyin(uap->fromlenaddr,
2611 &fromlen, sizeof (fromlen));
2618 if(uap->msg_flags) {
2619 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2627 auio.uio_iovcnt = uap->iovlen;
2628 auio.uio_segflg = UIO_USERSPACE;
2629 auio.uio_rw = UIO_READ;
2631 auio.uio_offset = 0; /* XXX */
2634 for (i = 0; i <uap->iovlen; i++, tiov++) {
2635 if ((auio.uio_resid += tiov->iov_len) < 0) {
2640 len = auio.uio_resid;
2641 fromsa = (struct sockaddr *)sockbufstore;
2644 if (KTRPOINT(td, KTR_GENIO))
2645 ktruio = cloneuio(&auio);
2647 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2648 fromsa, fromlen, &msg_flags,
2649 (struct sctp_sndrcvinfo *)&sinfo, 1);
2651 if (auio.uio_resid != (int)len && (error == ERESTART ||
2652 error == EINTR || error == EWOULDBLOCK))
2656 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2659 if (ktruio != NULL) {
2660 ktruio->uio_resid = (int)len - auio.uio_resid;
2661 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2666 td->td_retval[0] = (int)len - auio.uio_resid;
2668 if (fromlen && uap->from) {
2670 if (len <= 0 || fromsa == 0)
2673 len = MIN(len, fromsa->sa_len);
2674 error = copyout(fromsa, uap->from, (unsigned)len);
2678 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2684 if (KTRPOINT(td, KTR_STRUCT))
2685 ktrsockaddr(fromsa);
2687 if (uap->msg_flags) {
2688 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2701 return (EOPNOTSUPP);