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/vnode.h>
69 #include <sys/ktrace.h>
74 #include <security/audit/audit.h>
75 #include <security/mac/mac_framework.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_pageout.h>
81 #include <vm/vm_kern.h>
82 #include <vm/vm_extern.h>
84 #if defined(INET) || defined(INET6)
86 #include <netinet/sctp.h>
87 #include <netinet/sctp_peeloff.h>
89 #endif /* INET || INET6 */
91 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
92 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
94 static int accept1(struct thread *td, struct accept_args *uap, int compat);
95 static int do_sendfile(struct thread *td, struct sendfile_args *uap, int compat);
96 static int getsockname1(struct thread *td, struct getsockname_args *uap,
98 static int getpeername1(struct thread *td, struct getpeername_args *uap,
102 * NSFBUFS-related variables and associated sysctls
108 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
109 "Maximum number of sendfile(2) sf_bufs available");
110 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
111 "Number of sendfile(2) sf_bufs at peak usage");
112 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
113 "Number of sendfile(2) sf_bufs in use");
116 * Convert a user file descriptor to a kernel file entry. A reference on the
117 * file entry is held upon returning. This is lighter weight than
118 * fgetsock(), which bumps the socket reference drops the file reference
119 * count instead, as this approach avoids several additional mutex operations
120 * associated with the additional reference count. If requested, return the
124 getsock(struct filedesc *fdp, int fd, struct file **fpp, u_int *fflagp)
130 if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL) {
132 } else if (fp->f_type != DTYPE_SOCKET) {
133 fdrop(fp, curthread);
138 *fflagp = fp->f_flag;
146 * System call interface to the socket abstraction.
148 #if defined(COMPAT_43)
149 #define COMPAT_OLDSOCK
155 struct socket_args /* {
161 struct filedesc *fdp;
166 AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
168 error = mac_socket_check_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);
183 finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &socketops);
184 td->td_retval[0] = fd;
194 struct bind_args /* {
203 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
206 error = kern_bind(td, uap->s, sa);
212 kern_bind(td, fd, sa)
222 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
227 if (KTRPOINT(td, KTR_STRUCT))
231 error = mac_socket_check_bind(td->td_ucred, so, sa);
234 error = sobind(so, sa, td);
243 struct listen_args /* {
252 AUDIT_ARG_FD(uap->s);
253 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
257 error = mac_socket_check_listen(td->td_ucred, so);
260 CURVNET_SET(so->so_vnet);
261 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 CURVNET_SET(so->so_vnet);
427 error = soaccept(so, &sa);
431 * return a namelen of zero for older code which might
432 * ignore the return value from accept.
444 /* check sa_len before it is destroyed */
445 if (*namelen > sa->sa_len)
446 *namelen = sa->sa_len;
448 if (KTRPOINT(td, KTR_STRUCT))
459 * close the new descriptor, assuming someone hasn't ripped it
463 fdclose(fdp, nfp, fd, td);
466 * Release explicitly held references before returning. We return
467 * a reference on nfp to the caller on success if they request it.
486 struct accept_args *uap;
489 return (accept1(td, uap, 0));
492 #ifdef COMPAT_OLDSOCK
496 struct accept_args *uap;
499 return (accept1(td, uap, 1));
501 #endif /* COMPAT_OLDSOCK */
507 struct connect_args /* {
516 error = getsockaddr(&sa, uap->name, uap->namelen);
520 error = kern_connect(td, uap->s, sa);
527 kern_connect(td, fd, sa)
538 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
542 if (so->so_state & SS_ISCONNECTING) {
547 if (KTRPOINT(td, KTR_STRUCT))
551 error = mac_socket_check_connect(td->td_ucred, so, sa);
555 error = soconnect(so, sa, td);
558 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
563 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
564 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
567 if (error == EINTR || error == ERESTART)
573 error = so->so_error;
579 so->so_state &= ~SS_ISCONNECTING;
580 if (error == ERESTART)
588 kern_socketpair(struct thread *td, int domain, int type, int protocol,
591 struct filedesc *fdp = td->td_proc->p_fd;
592 struct file *fp1, *fp2;
593 struct socket *so1, *so2;
596 AUDIT_ARG_SOCKET(domain, type, protocol);
598 /* We might want to have a separate check for socket pairs. */
599 error = mac_socket_check_create(td->td_ucred, domain, type,
604 error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
607 error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
610 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
611 error = falloc(td, &fp1, &fd);
615 fp1->f_data = so1; /* so1 already has ref count */
616 error = falloc(td, &fp2, &fd);
619 fp2->f_data = so2; /* so2 already has ref count */
621 error = soconnect2(so1, so2);
624 if (type == SOCK_DGRAM) {
626 * Datagram socket connection is asymmetric.
628 error = soconnect2(so2, so1);
632 finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops);
633 finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops);
638 fdclose(fdp, fp2, rsv[1], td);
641 fdclose(fdp, fp1, rsv[0], td);
653 socketpair(struct thread *td, struct socketpair_args *uap)
657 error = kern_socketpair(td, uap->domain, uap->type,
661 error = copyout(sv, uap->rsv, 2 * sizeof(int));
663 (void)kern_close(td, sv[0]);
664 (void)kern_close(td, sv[1]);
670 sendit(td, s, mp, flags)
676 struct mbuf *control;
680 if (mp->msg_name != NULL) {
681 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
691 if (mp->msg_control) {
692 if (mp->msg_controllen < sizeof(struct cmsghdr)
693 #ifdef COMPAT_OLDSOCK
694 && mp->msg_flags != MSG_COMPAT
700 error = sockargs(&control, mp->msg_control,
701 mp->msg_controllen, MT_CONTROL);
704 #ifdef COMPAT_OLDSOCK
705 if (mp->msg_flags == MSG_COMPAT) {
708 M_PREPEND(control, sizeof(*cm), M_WAIT);
709 cm = mtod(control, struct cmsghdr *);
710 cm->cmsg_len = control->m_len;
711 cm->cmsg_level = SOL_SOCKET;
712 cm->cmsg_type = SCM_RIGHTS;
719 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
728 kern_sendit(td, s, mp, flags, control, segflg)
733 struct mbuf *control;
743 struct uio *ktruio = NULL;
747 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
750 so = (struct socket *)fp->f_data;
753 if (mp->msg_name != NULL) {
754 error = mac_socket_check_connect(td->td_ucred, so,
759 error = mac_socket_check_send(td->td_ucred, so);
764 auio.uio_iov = mp->msg_iov;
765 auio.uio_iovcnt = mp->msg_iovlen;
766 auio.uio_segflg = segflg;
767 auio.uio_rw = UIO_WRITE;
769 auio.uio_offset = 0; /* XXX */
772 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
773 if ((auio.uio_resid += iov->iov_len) < 0) {
779 if (KTRPOINT(td, KTR_GENIO))
780 ktruio = cloneuio(&auio);
782 len = auio.uio_resid;
783 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
785 if (auio.uio_resid != len && (error == ERESTART ||
786 error == EINTR || error == EWOULDBLOCK))
788 /* Generation of SIGPIPE can be controlled per socket */
789 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
790 !(flags & MSG_NOSIGNAL)) {
791 PROC_LOCK(td->td_proc);
792 psignal(td->td_proc, SIGPIPE);
793 PROC_UNLOCK(td->td_proc);
797 td->td_retval[0] = len - auio.uio_resid;
799 if (ktruio != NULL) {
800 ktruio->uio_resid = td->td_retval[0];
801 ktrgenio(s, UIO_WRITE, ktruio, error);
812 struct sendto_args /* {
825 msg.msg_name = uap->to;
826 msg.msg_namelen = uap->tolen;
830 #ifdef COMPAT_OLDSOCK
833 aiov.iov_base = uap->buf;
834 aiov.iov_len = uap->len;
835 error = sendit(td, uap->s, &msg, uap->flags);
839 #ifdef COMPAT_OLDSOCK
843 struct osend_args /* {
858 aiov.iov_base = uap->buf;
859 aiov.iov_len = uap->len;
862 error = sendit(td, uap->s, &msg, uap->flags);
869 struct osendmsg_args /* {
879 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
882 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
886 msg.msg_flags = MSG_COMPAT;
887 error = sendit(td, uap->s, &msg, uap->flags);
896 struct sendmsg_args /* {
906 error = copyin(uap->msg, &msg, sizeof (msg));
909 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
913 #ifdef COMPAT_OLDSOCK
916 error = sendit(td, uap->s, &msg, uap->flags);
922 kern_recvit(td, s, mp, fromseg, controlp)
926 enum uio_seg fromseg;
927 struct mbuf **controlp;
934 struct mbuf *m, *control = 0;
938 struct sockaddr *fromsa = 0;
940 struct uio *ktruio = NULL;
947 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
953 error = mac_socket_check_receive(td->td_ucred, so);
960 auio.uio_iov = mp->msg_iov;
961 auio.uio_iovcnt = mp->msg_iovlen;
962 auio.uio_segflg = UIO_USERSPACE;
963 auio.uio_rw = UIO_READ;
965 auio.uio_offset = 0; /* XXX */
968 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
969 if ((auio.uio_resid += iov->iov_len) < 0) {
975 if (KTRPOINT(td, KTR_GENIO))
976 ktruio = cloneuio(&auio);
978 len = auio.uio_resid;
979 CURVNET_SET(so->so_vnet);
980 error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
981 (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 CURVNET_SET(so->so_vnet);
1330 error = sosetopt(so, &sopt);
1341 struct getsockopt_args /* {
1345 void * __restrict val;
1346 socklen_t * __restrict avalsize;
1353 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1358 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1359 uap->val, UIO_USERSPACE, &valsize);
1362 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1367 * Kernel version of getsockopt.
1368 * optval can be a userland or userspace. optlen is always a kernel pointer.
1371 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1377 enum uio_seg valseg;
1383 struct sockopt sopt;
1387 if ((int)*valsize < 0)
1390 sopt.sopt_dir = SOPT_GET;
1391 sopt.sopt_level = level;
1392 sopt.sopt_name = name;
1393 sopt.sopt_val = val;
1394 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1400 sopt.sopt_td = NULL;
1403 panic("kern_getsockopt called with bad valseg");
1407 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1410 CURVNET_SET(so->so_vnet);
1411 error = sogetopt(so, &sopt);
1413 *valsize = sopt.sopt_valsize;
1420 * getsockname1() - Get socket name.
1424 getsockname1(td, uap, compat)
1426 struct getsockname_args /* {
1428 struct sockaddr * __restrict asa;
1429 socklen_t * __restrict alen;
1433 struct sockaddr *sa;
1437 error = copyin(uap->alen, &len, sizeof(len));
1441 error = kern_getsockname(td, uap->fdes, &sa, &len);
1446 #ifdef COMPAT_OLDSOCK
1448 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1450 error = copyout(sa, uap->asa, (u_int)len);
1454 error = copyout(&len, uap->alen, sizeof(len));
1459 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1471 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1476 CURVNET_SET(so->so_vnet);
1477 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1484 len = MIN(*alen, (*sa)->sa_len);
1487 if (KTRPOINT(td, KTR_STRUCT))
1493 free(*sa, M_SONAME);
1500 getsockname(td, uap)
1502 struct getsockname_args *uap;
1505 return (getsockname1(td, uap, 0));
1508 #ifdef COMPAT_OLDSOCK
1510 ogetsockname(td, uap)
1512 struct getsockname_args *uap;
1515 return (getsockname1(td, uap, 1));
1517 #endif /* COMPAT_OLDSOCK */
1520 * getpeername1() - Get name of peer for connected socket.
1524 getpeername1(td, uap, compat)
1526 struct getpeername_args /* {
1528 struct sockaddr * __restrict asa;
1529 socklen_t * __restrict alen;
1533 struct sockaddr *sa;
1537 error = copyin(uap->alen, &len, sizeof (len));
1541 error = kern_getpeername(td, uap->fdes, &sa, &len);
1546 #ifdef COMPAT_OLDSOCK
1548 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1550 error = copyout(sa, uap->asa, (u_int)len);
1554 error = copyout(&len, uap->alen, sizeof(len));
1559 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1571 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1575 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1580 CURVNET_SET(so->so_vnet);
1581 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1588 len = MIN(*alen, (*sa)->sa_len);
1591 if (KTRPOINT(td, KTR_STRUCT))
1596 free(*sa, M_SONAME);
1605 getpeername(td, uap)
1607 struct getpeername_args *uap;
1610 return (getpeername1(td, uap, 0));
1613 #ifdef COMPAT_OLDSOCK
1615 ogetpeername(td, uap)
1617 struct ogetpeername_args *uap;
1620 /* XXX uap should have type `getpeername_args *' to begin with. */
1621 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1623 #endif /* COMPAT_OLDSOCK */
1626 sockargs(mp, buf, buflen, type)
1631 struct sockaddr *sa;
1635 if ((u_int)buflen > MLEN) {
1636 #ifdef COMPAT_OLDSOCK
1637 if (type == MT_SONAME && (u_int)buflen <= 112)
1638 buflen = MLEN; /* unix domain compat. hack */
1641 if ((u_int)buflen > MCLBYTES)
1644 m = m_get(M_WAIT, type);
1645 if ((u_int)buflen > MLEN)
1648 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1653 if (type == MT_SONAME) {
1654 sa = mtod(m, struct sockaddr *);
1656 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1657 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1658 sa->sa_family = sa->sa_len;
1660 sa->sa_len = buflen;
1667 getsockaddr(namp, uaddr, len)
1668 struct sockaddr **namp;
1672 struct sockaddr *sa;
1675 if (len > SOCK_MAXADDRLEN)
1676 return (ENAMETOOLONG);
1677 if (len < offsetof(struct sockaddr, sa_data[0]))
1679 sa = malloc(len, M_SONAME, M_WAITOK);
1680 error = copyin(uaddr, sa, len);
1684 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1685 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1686 sa->sa_family = sa->sa_len;
1694 #include <sys/condvar.h>
1696 struct sendfile_sync {
1703 * Detach mapped page and release resources back to the system.
1706 sf_buf_mext(void *addr, void *args)
1709 struct sendfile_sync *sfs;
1711 m = sf_buf_page(args);
1713 vm_page_lock_queues();
1714 vm_page_unwire(m, 0);
1716 * Check for the object going away on us. This can
1717 * happen since we don't hold a reference to it.
1718 * If so, we're responsible for freeing the page.
1720 if (m->wire_count == 0 && m->object == NULL)
1722 vm_page_unlock_queues();
1726 mtx_lock(&sfs->mtx);
1727 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1728 if (--sfs->count == 0)
1729 cv_signal(&sfs->cv);
1730 mtx_unlock(&sfs->mtx);
1736 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1737 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1739 * Send a file specified by 'fd' and starting at 'offset' to a socket
1740 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1741 * 0. Optionally add a header and/or trailer to the socket output. If
1742 * specified, write the total number of bytes sent into *sbytes.
1745 sendfile(struct thread *td, struct sendfile_args *uap)
1748 return (do_sendfile(td, uap, 0));
1752 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1754 struct sf_hdtr hdtr;
1755 struct uio *hdr_uio, *trl_uio;
1758 hdr_uio = trl_uio = NULL;
1760 if (uap->hdtr != NULL) {
1761 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1764 if (hdtr.headers != NULL) {
1765 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1769 if (hdtr.trailers != NULL) {
1770 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1777 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1780 free(hdr_uio, M_IOV);
1782 free(trl_uio, M_IOV);
1786 #ifdef COMPAT_FREEBSD4
1788 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1790 struct sendfile_args args;
1794 args.offset = uap->offset;
1795 args.nbytes = uap->nbytes;
1796 args.hdtr = uap->hdtr;
1797 args.sbytes = uap->sbytes;
1798 args.flags = uap->flags;
1800 return (do_sendfile(td, &args, 1));
1802 #endif /* COMPAT_FREEBSD4 */
1805 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1806 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1808 struct file *sock_fp;
1810 struct vm_object *obj = NULL;
1811 struct socket *so = NULL;
1812 struct mbuf *m = NULL;
1815 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1816 int error, hdrlen = 0, mnw = 0;
1818 struct sendfile_sync *sfs = NULL;
1821 * The file descriptor must be a regular file and have a
1822 * backing VM object.
1823 * File offset must be positive. If it goes beyond EOF
1824 * we send only the header/trailer and no payload data.
1826 AUDIT_ARG_FD(uap->fd);
1827 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1829 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1830 vn_lock(vp, LK_SHARED | LK_RETRY);
1831 if (vp->v_type == VREG) {
1835 * Temporarily increase the backing VM
1836 * object's reference count so that a forced
1837 * reclamation of its vnode does not
1838 * immediately destroy it.
1840 VM_OBJECT_LOCK(obj);
1841 if ((obj->flags & OBJ_DEAD) == 0) {
1842 vm_object_reference_locked(obj);
1843 VM_OBJECT_UNLOCK(obj);
1845 VM_OBJECT_UNLOCK(obj);
1851 VFS_UNLOCK_GIANT(vfslocked);
1856 if (uap->offset < 0) {
1862 * The socket must be a stream socket and connected.
1863 * Remember if it a blocking or non-blocking socket.
1865 if ((error = getsock(td->td_proc->p_fd, uap->s, &sock_fp,
1868 so = sock_fp->f_data;
1869 if (so->so_type != SOCK_STREAM) {
1873 if ((so->so_state & SS_ISCONNECTED) == 0) {
1878 * Do not wait on memory allocations but return ENOMEM for
1879 * caller to retry later.
1880 * XXX: Experimental.
1882 if (uap->flags & SF_MNOWAIT)
1885 if (uap->flags & SF_SYNC) {
1886 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK);
1887 memset(sfs, 0, sizeof *sfs);
1888 mtx_init(&sfs->mtx, "sendfile", MTX_DEF, 0);
1889 cv_init(&sfs->cv, "sendfile");
1893 error = mac_socket_check_send(td->td_ucred, so);
1898 /* If headers are specified copy them into mbufs. */
1899 if (hdr_uio != NULL) {
1900 hdr_uio->uio_td = td;
1901 hdr_uio->uio_rw = UIO_WRITE;
1902 if (hdr_uio->uio_resid > 0) {
1904 * In FBSD < 5.0 the nbytes to send also included
1905 * the header. If compat is specified subtract the
1906 * header size from nbytes.
1909 if (uap->nbytes > hdr_uio->uio_resid)
1910 uap->nbytes -= hdr_uio->uio_resid;
1914 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1917 error = mnw ? EAGAIN : ENOBUFS;
1920 hdrlen = m_length(m, NULL);
1925 * Protect against multiple writers to the socket.
1927 * XXXRW: Historically this has assumed non-interruptibility, so now
1928 * we implement that, but possibly shouldn't.
1930 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1933 * Loop through the pages of the file, starting with the requested
1934 * offset. Get a file page (do I/O if necessary), map the file page
1935 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1937 * This is done in two loops. The inner loop turns as many pages
1938 * as it can, up to available socket buffer space, without blocking
1939 * into mbufs to have it bulk delivered into the socket send buffer.
1940 * The outer loop checks the state and available space of the socket
1941 * and takes care of the overall progress.
1943 for (off = uap->offset, rem = uap->nbytes; ; ) {
1949 * Check the socket state for ongoing connection,
1950 * no errors and space in socket buffer.
1951 * If space is low allow for the remainder of the
1952 * file to be processed if it fits the socket buffer.
1953 * Otherwise block in waiting for sufficient space
1954 * to proceed, or if the socket is nonblocking, return
1955 * to userland with EAGAIN while reporting how far
1957 * We wait until the socket buffer has significant free
1958 * space to do bulk sends. This makes good use of file
1959 * system read ahead and allows packet segmentation
1960 * offloading hardware to take over lots of work. If
1961 * we were not careful here we would send off only one
1964 SOCKBUF_LOCK(&so->so_snd);
1965 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1966 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1968 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1970 SOCKBUF_UNLOCK(&so->so_snd);
1972 } else if (so->so_error) {
1973 error = so->so_error;
1975 SOCKBUF_UNLOCK(&so->so_snd);
1978 space = sbspace(&so->so_snd);
1981 space < so->so_snd.sb_lowat)) {
1982 if (so->so_state & SS_NBIO) {
1983 SOCKBUF_UNLOCK(&so->so_snd);
1988 * sbwait drops the lock while sleeping.
1989 * When we loop back to retry_space the
1990 * state may have changed and we retest
1993 error = sbwait(&so->so_snd);
1995 * An error from sbwait usually indicates that we've
1996 * been interrupted by a signal. If we've sent anything
1997 * then return bytes sent, otherwise return the error.
2000 SOCKBUF_UNLOCK(&so->so_snd);
2005 SOCKBUF_UNLOCK(&so->so_snd);
2008 * Reduce space in the socket buffer by the size of
2009 * the header mbuf chain.
2010 * hdrlen is set to 0 after the first loop.
2015 * Loop and construct maximum sized mbuf chain to be bulk
2016 * dumped into socket buffer.
2018 while(space > loopbytes) {
2023 VM_OBJECT_LOCK(obj);
2025 * Calculate the amount to transfer.
2026 * Not to exceed a page, the EOF,
2027 * or the passed in nbytes.
2029 pgoff = (vm_offset_t)(off & PAGE_MASK);
2030 xfsize = omin(PAGE_SIZE - pgoff,
2031 obj->un_pager.vnp.vnp_size - uap->offset -
2032 fsbytes - loopbytes);
2034 rem = (uap->nbytes - fsbytes - loopbytes);
2036 rem = obj->un_pager.vnp.vnp_size -
2037 uap->offset - fsbytes - loopbytes;
2038 xfsize = omin(rem, xfsize);
2040 VM_OBJECT_UNLOCK(obj);
2041 done = 1; /* all data sent */
2045 * Don't overflow the send buffer.
2046 * Stop here and send out what we've
2049 if (space < loopbytes + xfsize) {
2050 VM_OBJECT_UNLOCK(obj);
2055 * Attempt to look up the page. Allocate
2056 * if not found or wait and loop if busy.
2058 pindex = OFF_TO_IDX(off);
2059 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2060 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2063 * Check if page is valid for what we need,
2064 * otherwise initiate I/O.
2065 * If we already turned some pages into mbufs,
2066 * send them off before we come here again and
2069 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2070 VM_OBJECT_UNLOCK(obj);
2072 error = EAGAIN; /* send what we already got */
2073 else if (uap->flags & SF_NODISKIO)
2079 * Ensure that our page is still around
2080 * when the I/O completes.
2082 vm_page_io_start(pg);
2083 VM_OBJECT_UNLOCK(obj);
2086 * Get the page from backing store.
2088 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2089 error = vn_lock(vp, LK_SHARED);
2092 bsize = vp->v_mount->mnt_stat.f_iosize;
2095 * XXXMAC: Because we don't have fp->f_cred
2096 * here, we pass in NOCRED. This is probably
2097 * wrong, but is consistent with our original
2100 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2101 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2102 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2103 td->td_ucred, NOCRED, &resid, td);
2106 VFS_UNLOCK_GIANT(vfslocked);
2107 VM_OBJECT_LOCK(obj);
2108 vm_page_io_finish(pg);
2110 VM_OBJECT_UNLOCK(obj);
2114 vm_page_lock_queues();
2115 vm_page_unwire(pg, 0);
2117 * See if anyone else might know about
2118 * this page. If not and it is not valid,
2121 if (pg->wire_count == 0 && pg->valid == 0 &&
2122 pg->busy == 0 && !(pg->oflags & VPO_BUSY) &&
2123 pg->hold_count == 0) {
2126 vm_page_unlock_queues();
2127 VM_OBJECT_UNLOCK(obj);
2128 if (error == EAGAIN)
2129 error = 0; /* not a real error */
2134 * Get a sendfile buf. We usually wait as long
2135 * as necessary, but this wait can be interrupted.
2137 if ((sf = sf_buf_alloc(pg,
2138 (mnw ? SFB_NOWAIT : SFB_CATCH))) == NULL) {
2139 mbstat.sf_allocfail++;
2140 vm_page_lock_queues();
2141 vm_page_unwire(pg, 0);
2143 * XXX: Not same check as above!?
2145 if (pg->wire_count == 0 && pg->object == NULL)
2147 vm_page_unlock_queues();
2148 error = (mnw ? EAGAIN : EINTR);
2153 * Get an mbuf and set it up as having
2156 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2158 error = (mnw ? EAGAIN : ENOBUFS);
2159 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2162 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2163 sfs, sf, M_RDONLY, EXT_SFBUF);
2164 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2167 /* Append to mbuf chain. */
2173 /* Keep track of bits processed. */
2174 loopbytes += xfsize;
2178 mtx_lock(&sfs->mtx);
2180 mtx_unlock(&sfs->mtx);
2184 /* Add the buffer chain to the socket buffer. */
2188 mlen = m_length(m, NULL);
2189 SOCKBUF_LOCK(&so->so_snd);
2190 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2192 SOCKBUF_UNLOCK(&so->so_snd);
2195 SOCKBUF_UNLOCK(&so->so_snd);
2196 CURVNET_SET(so->so_vnet);
2197 /* Avoid error aliasing. */
2198 err = (*so->so_proto->pr_usrreqs->pru_send)
2199 (so, 0, m, NULL, NULL, td);
2203 * We need two counters to get the
2204 * file offset and nbytes to send
2206 * - sbytes contains the total amount
2207 * of bytes sent, including headers.
2208 * - fsbytes contains the total amount
2209 * of bytes sent from the file.
2217 } else if (error == 0)
2219 m = NULL; /* pru_send always consumes */
2222 /* Quit outer loop on error or when we're done. */
2230 * Send trailers. Wimp out and use writev(2).
2232 if (trl_uio != NULL) {
2233 sbunlock(&so->so_snd);
2234 error = kern_writev(td, uap->s, trl_uio);
2236 sbytes += td->td_retval[0];
2241 sbunlock(&so->so_snd);
2244 * If there was no error we have to clear td->td_retval[0]
2245 * because it may have been set by writev.
2248 td->td_retval[0] = 0;
2250 if (uap->sbytes != NULL) {
2251 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2254 vm_object_deallocate(obj);
2256 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2258 VFS_UNLOCK_GIANT(vfslocked);
2266 mtx_lock(&sfs->mtx);
2267 if (sfs->count != 0)
2268 cv_wait(&sfs->cv, &sfs->mtx);
2269 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2270 cv_destroy(&sfs->cv);
2271 mtx_destroy(&sfs->mtx);
2275 if (error == ERESTART)
2283 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2284 * otherwise all return EOPNOTSUPP.
2285 * XXX: We should make this loadable one day.
2288 sctp_peeloff(td, uap)
2290 struct sctp_peeloff_args /* {
2295 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2296 struct filedesc *fdp;
2297 struct file *nfp = NULL;
2299 struct socket *head, *so;
2303 fdp = td->td_proc->p_fd;
2304 AUDIT_ARG_FD(uap->sd);
2305 error = fgetsock(td, uap->sd, &head, &fflag);
2308 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2312 * At this point we know we do have a assoc to pull
2313 * we proceed to get the fd setup. This may block
2317 error = falloc(td, &nfp, &fd);
2320 td->td_retval[0] = fd;
2322 so = sonewconn(head, SS_ISCONNECTED);
2326 * Before changing the flags on the socket, we have to bump the
2327 * reference count. Otherwise, if the protocol calls sofree(),
2328 * the socket will be released due to a zero refcount.
2331 soref(so); /* file descriptor reference */
2336 TAILQ_REMOVE(&head->so_comp, so, so_list);
2338 so->so_state |= (head->so_state & SS_NBIO);
2339 so->so_state &= ~SS_NOFDREF;
2340 so->so_qstate &= ~SQ_COMP;
2343 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2344 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2347 if (head->so_sigio != NULL)
2348 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2352 * close the new descriptor, assuming someone hasn't ripped it
2353 * out from under us.
2356 fdclose(fdp, nfp, fd, td);
2359 * Release explicitly held references before returning.
2368 return (EOPNOTSUPP);
2373 sctp_generic_sendmsg (td, uap)
2375 struct sctp_generic_sendmsg_args /* {
2381 struct sctp_sndrcvinfo *sinfo,
2385 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2386 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2388 struct file *fp = NULL;
2389 int use_rcvinfo = 1;
2391 struct sockaddr *to = NULL;
2393 struct uio *ktruio = NULL;
2396 struct iovec iov[1];
2399 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2405 error = getsockaddr(&to, uap->to, uap->tolen);
2412 AUDIT_ARG_FD(uap->sd);
2413 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2417 if (KTRPOINT(td, KTR_STRUCT))
2421 iov[0].iov_base = uap->msg;
2422 iov[0].iov_len = uap->mlen;
2424 so = (struct socket *)fp->f_data;
2426 error = mac_socket_check_send(td->td_ucred, so);
2432 auio.uio_iovcnt = 1;
2433 auio.uio_segflg = UIO_USERSPACE;
2434 auio.uio_rw = UIO_WRITE;
2436 auio.uio_offset = 0; /* XXX */
2438 len = auio.uio_resid = uap->mlen;
2439 error = sctp_lower_sosend(so, to, &auio,
2440 (struct mbuf *)NULL, (struct mbuf *)NULL,
2441 uap->flags, use_rcvinfo, u_sinfo, td);
2443 if (auio.uio_resid != len && (error == ERESTART ||
2444 error == EINTR || error == EWOULDBLOCK))
2446 /* Generation of SIGPIPE can be controlled per socket. */
2447 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2448 !(uap->flags & MSG_NOSIGNAL)) {
2449 PROC_LOCK(td->td_proc);
2450 psignal(td->td_proc, SIGPIPE);
2451 PROC_UNLOCK(td->td_proc);
2455 td->td_retval[0] = len - auio.uio_resid;
2457 if (ktruio != NULL) {
2458 ktruio->uio_resid = td->td_retval[0];
2459 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2470 return (EOPNOTSUPP);
2475 sctp_generic_sendmsg_iov(td, uap)
2477 struct sctp_generic_sendmsg_iov_args /* {
2483 struct sctp_sndrcvinfo *sinfo,
2487 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2488 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2490 struct file *fp = NULL;
2491 int use_rcvinfo = 1;
2492 int error=0, len, i;
2493 struct sockaddr *to = NULL;
2495 struct uio *ktruio = NULL;
2498 struct iovec *iov, *tiov;
2501 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2507 error = getsockaddr(&to, uap->to, uap->tolen);
2514 AUDIT_ARG_FD(uap->sd);
2515 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2519 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2523 if (KTRPOINT(td, KTR_STRUCT))
2527 so = (struct socket *)fp->f_data;
2529 error = mac_socket_check_send(td->td_ucred, so);
2535 auio.uio_iovcnt = uap->iovlen;
2536 auio.uio_segflg = UIO_USERSPACE;
2537 auio.uio_rw = UIO_WRITE;
2539 auio.uio_offset = 0; /* XXX */
2542 for (i = 0; i <uap->iovlen; i++, tiov++) {
2543 if ((auio.uio_resid += tiov->iov_len) < 0) {
2548 len = auio.uio_resid;
2549 error = sctp_lower_sosend(so, to, &auio,
2550 (struct mbuf *)NULL, (struct mbuf *)NULL,
2551 uap->flags, use_rcvinfo, u_sinfo, td);
2553 if (auio.uio_resid != len && (error == ERESTART ||
2554 error == EINTR || error == EWOULDBLOCK))
2556 /* Generation of SIGPIPE can be controlled per socket */
2557 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2558 !(uap->flags & MSG_NOSIGNAL)) {
2559 PROC_LOCK(td->td_proc);
2560 psignal(td->td_proc, SIGPIPE);
2561 PROC_UNLOCK(td->td_proc);
2565 td->td_retval[0] = len - auio.uio_resid;
2567 if (ktruio != NULL) {
2568 ktruio->uio_resid = td->td_retval[0];
2569 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2582 return (EOPNOTSUPP);
2587 sctp_generic_recvmsg(td, uap)
2589 struct sctp_generic_recvmsg_args /* {
2593 struct sockaddr *from,
2594 __socklen_t *fromlenaddr,
2595 struct sctp_sndrcvinfo *sinfo,
2599 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2600 u_int8_t sockbufstore[256];
2602 struct iovec *iov, *tiov;
2603 struct sctp_sndrcvinfo sinfo;
2605 struct file *fp = NULL;
2606 struct sockaddr *fromsa;
2608 int len, i, msg_flags;
2611 struct uio *ktruio = NULL;
2614 AUDIT_ARG_FD(uap->sd);
2615 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2619 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2626 error = mac_socket_check_receive(td->td_ucred, so);
2633 if (uap->fromlenaddr) {
2634 error = copyin(uap->fromlenaddr,
2635 &fromlen, sizeof (fromlen));
2642 if(uap->msg_flags) {
2643 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2651 auio.uio_iovcnt = uap->iovlen;
2652 auio.uio_segflg = UIO_USERSPACE;
2653 auio.uio_rw = UIO_READ;
2655 auio.uio_offset = 0; /* XXX */
2658 for (i = 0; i <uap->iovlen; i++, tiov++) {
2659 if ((auio.uio_resid += tiov->iov_len) < 0) {
2664 len = auio.uio_resid;
2665 fromsa = (struct sockaddr *)sockbufstore;
2668 if (KTRPOINT(td, KTR_GENIO))
2669 ktruio = cloneuio(&auio);
2671 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2672 fromsa, fromlen, &msg_flags,
2673 (struct sctp_sndrcvinfo *)&sinfo, 1);
2675 if (auio.uio_resid != (int)len && (error == ERESTART ||
2676 error == EINTR || error == EWOULDBLOCK))
2680 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2683 if (ktruio != NULL) {
2684 ktruio->uio_resid = (int)len - auio.uio_resid;
2685 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2690 td->td_retval[0] = (int)len - auio.uio_resid;
2692 if (fromlen && uap->from) {
2694 if (len <= 0 || fromsa == 0)
2697 len = MIN(len, fromsa->sa_len);
2698 error = copyout(fromsa, uap->from, (unsigned)len);
2702 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2708 if (KTRPOINT(td, KTR_STRUCT))
2709 ktrsockaddr(fromsa);
2711 if (uap->msg_flags) {
2712 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2725 return (EOPNOTSUPP);