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_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)
221 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
226 if (KTRPOINT(td, KTR_STRUCT))
231 error = mac_socket_check_bind(td->td_ucred, so, sa);
236 error = sobind(so, sa, td);
248 struct listen_args /* {
257 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
262 error = mac_socket_check_listen(td->td_ucred, so);
267 error = solisten(so, uap->backlog, td);
280 accept1(td, uap, compat)
282 struct accept_args /* {
284 struct sockaddr * __restrict name;
285 socklen_t * __restrict anamelen;
289 struct sockaddr *name;
294 if (uap->name == NULL)
295 return (kern_accept(td, uap->s, NULL, NULL, NULL));
297 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
301 error = kern_accept(td, uap->s, &name, &namelen, &fp);
304 * return a namelen of zero for older code which might
305 * ignore the return value from accept.
308 (void) copyout(&namelen,
309 uap->anamelen, sizeof(*uap->anamelen));
313 if (error == 0 && name != NULL) {
314 #ifdef COMPAT_OLDSOCK
316 ((struct osockaddr *)name)->sa_family =
319 error = copyout(name, uap->name, namelen);
322 error = copyout(&namelen, uap->anamelen,
325 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
327 free(name, M_SONAME);
332 kern_accept(struct thread *td, int s, struct sockaddr **name,
333 socklen_t *namelen, struct file **fp)
335 struct filedesc *fdp;
336 struct file *headfp, *nfp = NULL;
337 struct sockaddr *sa = NULL;
339 struct socket *head, *so;
351 fdp = td->td_proc->p_fd;
352 error = getsock(fdp, s, &headfp, &fflag);
355 head = headfp->f_data;
356 if ((head->so_options & SO_ACCEPTCONN) == 0) {
362 error = mac_socket_check_accept(td->td_ucred, head);
367 error = falloc(td, &nfp, &fd);
371 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
376 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
377 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
378 head->so_error = ECONNABORTED;
381 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
388 if (head->so_error) {
389 error = head->so_error;
394 so = TAILQ_FIRST(&head->so_comp);
395 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
396 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
399 * Before changing the flags on the socket, we have to bump the
400 * reference count. Otherwise, if the protocol calls sofree(),
401 * the socket will be released due to a zero refcount.
403 SOCK_LOCK(so); /* soref() and so_state update */
404 soref(so); /* file descriptor reference */
406 TAILQ_REMOVE(&head->so_comp, so, so_list);
408 so->so_state |= (head->so_state & SS_NBIO);
409 so->so_qstate &= ~SQ_COMP;
415 /* An extra reference on `nfp' has been held for us by falloc(). */
416 td->td_retval[0] = fd;
418 /* connection has been removed from the listen queue */
419 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
421 pgid = fgetown(&head->so_sigio);
423 fsetown(pgid, &so->so_sigio);
425 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
426 /* Sync socket nonblocking/async state with file flags */
427 tmp = fflag & FNONBLOCK;
428 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
429 tmp = fflag & FASYNC;
430 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
432 error = soaccept(so, &sa);
435 * return a namelen of zero for older code which might
436 * ignore the return value from accept.
448 /* check sa_len before it is destroyed */
449 if (*namelen > sa->sa_len)
450 *namelen = sa->sa_len;
452 if (KTRPOINT(td, KTR_STRUCT))
463 * close the new descriptor, assuming someone hasn't ripped it
467 fdclose(fdp, nfp, fd, td);
470 * Release explicitly held references before returning. We return
471 * a reference on nfp to the caller on success if they request it.
490 struct accept_args *uap;
493 return (accept1(td, uap, 0));
496 #ifdef COMPAT_OLDSOCK
500 struct accept_args *uap;
503 return (accept1(td, uap, 1));
505 #endif /* COMPAT_OLDSOCK */
511 struct connect_args /* {
520 error = getsockaddr(&sa, uap->name, uap->namelen);
524 error = kern_connect(td, uap->s, sa);
531 kern_connect(td, fd, sa)
541 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
545 if (so->so_state & SS_ISCONNECTING) {
550 if (KTRPOINT(td, KTR_STRUCT))
555 error = mac_socket_check_connect(td->td_ucred, so, sa);
560 error = soconnect(so, sa, td);
563 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
568 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
569 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
572 if (error == EINTR || error == ERESTART)
578 error = so->so_error;
584 so->so_state &= ~SS_ISCONNECTING;
585 if (error == ERESTART)
595 struct socketpair_args /* {
602 struct filedesc *fdp = td->td_proc->p_fd;
603 struct file *fp1, *fp2;
604 struct socket *so1, *so2;
605 int fd, error, sv[2];
608 /* We might want to have a separate check for socket pairs. */
609 error = mac_socket_check_create(td->td_ucred, uap->domain, uap->type,
615 error = socreate(uap->domain, &so1, uap->type, uap->protocol,
619 error = socreate(uap->domain, &so2, uap->type, uap->protocol,
623 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
624 error = falloc(td, &fp1, &fd);
628 fp1->f_data = so1; /* so1 already has ref count */
629 error = falloc(td, &fp2, &fd);
632 fp2->f_data = so2; /* so2 already has ref count */
634 error = soconnect2(so1, so2);
637 if (uap->type == SOCK_DGRAM) {
639 * Datagram socket connection is asymmetric.
641 error = soconnect2(so2, so1);
645 finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops);
646 finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops);
648 error = copyout(sv, uap->rsv, 2 * sizeof (int));
655 fdclose(fdp, fp2, sv[1], td);
658 fdclose(fdp, fp1, sv[0], td);
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;
746 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
749 so = (struct socket *)fp->f_data;
753 error = mac_socket_check_send(td->td_ucred, so);
759 auio.uio_iov = mp->msg_iov;
760 auio.uio_iovcnt = mp->msg_iovlen;
761 auio.uio_segflg = segflg;
762 auio.uio_rw = UIO_WRITE;
764 auio.uio_offset = 0; /* XXX */
767 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
768 if ((auio.uio_resid += iov->iov_len) < 0) {
774 if (KTRPOINT(td, KTR_GENIO))
775 ktruio = cloneuio(&auio);
777 len = auio.uio_resid;
778 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
780 if (auio.uio_resid != len && (error == ERESTART ||
781 error == EINTR || error == EWOULDBLOCK))
783 /* Generation of SIGPIPE can be controlled per socket */
784 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
785 !(flags & MSG_NOSIGNAL)) {
786 PROC_LOCK(td->td_proc);
787 psignal(td->td_proc, SIGPIPE);
788 PROC_UNLOCK(td->td_proc);
792 td->td_retval[0] = len - auio.uio_resid;
794 if (ktruio != NULL) {
795 ktruio->uio_resid = td->td_retval[0];
796 ktrgenio(s, UIO_WRITE, ktruio, error);
807 struct sendto_args /* {
820 msg.msg_name = uap->to;
821 msg.msg_namelen = uap->tolen;
825 #ifdef COMPAT_OLDSOCK
828 aiov.iov_base = uap->buf;
829 aiov.iov_len = uap->len;
830 error = sendit(td, uap->s, &msg, uap->flags);
834 #ifdef COMPAT_OLDSOCK
838 struct osend_args /* {
853 aiov.iov_base = uap->buf;
854 aiov.iov_len = uap->len;
857 error = sendit(td, uap->s, &msg, uap->flags);
864 struct osendmsg_args /* {
874 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
877 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
881 msg.msg_flags = MSG_COMPAT;
882 error = sendit(td, uap->s, &msg, uap->flags);
891 struct sendmsg_args /* {
901 error = copyin(uap->msg, &msg, sizeof (msg));
904 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
908 #ifdef COMPAT_OLDSOCK
911 error = sendit(td, uap->s, &msg, uap->flags);
917 kern_recvit(td, s, mp, fromseg, controlp)
921 enum uio_seg fromseg;
922 struct mbuf **controlp;
929 struct mbuf *m, *control = 0;
933 struct sockaddr *fromsa = 0;
935 struct uio *ktruio = NULL;
941 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
948 error = mac_socket_check_receive(td->td_ucred, so);
956 auio.uio_iov = mp->msg_iov;
957 auio.uio_iovcnt = mp->msg_iovlen;
958 auio.uio_segflg = UIO_USERSPACE;
959 auio.uio_rw = UIO_READ;
961 auio.uio_offset = 0; /* XXX */
964 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
965 if ((auio.uio_resid += iov->iov_len) < 0) {
971 if (KTRPOINT(td, KTR_GENIO))
972 ktruio = cloneuio(&auio);
974 len = auio.uio_resid;
975 error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
976 (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
979 if (auio.uio_resid != (int)len && (error == ERESTART ||
980 error == EINTR || error == EWOULDBLOCK))
984 if (ktruio != NULL) {
985 ktruio->uio_resid = (int)len - auio.uio_resid;
986 ktrgenio(s, UIO_READ, ktruio, error);
991 td->td_retval[0] = (int)len - auio.uio_resid;
993 len = mp->msg_namelen;
994 if (len <= 0 || fromsa == 0)
997 /* save sa_len before it is destroyed by MSG_COMPAT */
998 len = MIN(len, fromsa->sa_len);
999 #ifdef COMPAT_OLDSOCK
1000 if (mp->msg_flags & MSG_COMPAT)
1001 ((struct osockaddr *)fromsa)->sa_family =
1004 if (fromseg == UIO_USERSPACE) {
1005 error = copyout(fromsa, mp->msg_name,
1010 bcopy(fromsa, mp->msg_name, len);
1012 mp->msg_namelen = len;
1014 if (mp->msg_control && controlp == NULL) {
1015 #ifdef COMPAT_OLDSOCK
1017 * We assume that old recvmsg calls won't receive access
1018 * rights and other control info, esp. as control info
1019 * is always optional and those options didn't exist in 4.3.
1020 * If we receive rights, trim the cmsghdr; anything else
1023 if (control && mp->msg_flags & MSG_COMPAT) {
1024 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1026 mtod(control, struct cmsghdr *)->cmsg_type !=
1028 mp->msg_controllen = 0;
1031 control->m_len -= sizeof (struct cmsghdr);
1032 control->m_data += sizeof (struct cmsghdr);
1035 len = mp->msg_controllen;
1037 mp->msg_controllen = 0;
1038 ctlbuf = mp->msg_control;
1040 while (m && len > 0) {
1041 unsigned int tocopy;
1043 if (len >= m->m_len)
1046 mp->msg_flags |= MSG_CTRUNC;
1050 if ((error = copyout(mtod(m, caddr_t),
1051 ctlbuf, tocopy)) != 0)
1058 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1063 if (fromsa && KTRPOINT(td, KTR_STRUCT))
1064 ktrsockaddr(fromsa);
1067 FREE(fromsa, M_SONAME);
1069 if (error == 0 && controlp != NULL)
1070 *controlp = control;
1078 recvit(td, s, mp, namelenp)
1086 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1090 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1091 #ifdef COMPAT_OLDSOCK
1092 if (mp->msg_flags & MSG_COMPAT)
1093 error = 0; /* old recvfrom didn't check */
1102 struct recvfrom_args /* {
1107 struct sockaddr * __restrict from;
1108 socklen_t * __restrict fromlenaddr;
1115 if (uap->fromlenaddr) {
1116 error = copyin(uap->fromlenaddr,
1117 &msg.msg_namelen, sizeof (msg.msg_namelen));
1121 msg.msg_namelen = 0;
1123 msg.msg_name = uap->from;
1124 msg.msg_iov = &aiov;
1126 aiov.iov_base = uap->buf;
1127 aiov.iov_len = uap->len;
1128 msg.msg_control = 0;
1129 msg.msg_flags = uap->flags;
1130 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1135 #ifdef COMPAT_OLDSOCK
1139 struct recvfrom_args *uap;
1142 uap->flags |= MSG_COMPAT;
1143 return (recvfrom(td, uap));
1147 #ifdef COMPAT_OLDSOCK
1151 struct orecv_args /* {
1163 msg.msg_namelen = 0;
1164 msg.msg_iov = &aiov;
1166 aiov.iov_base = uap->buf;
1167 aiov.iov_len = uap->len;
1168 msg.msg_control = 0;
1169 msg.msg_flags = uap->flags;
1170 error = recvit(td, uap->s, &msg, NULL);
1175 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1176 * overlays the new one, missing only the flags, and with the (old) access
1177 * rights where the control fields are now.
1182 struct orecvmsg_args /* {
1184 struct omsghdr *msg;
1192 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1195 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1198 msg.msg_flags = uap->flags | MSG_COMPAT;
1200 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1201 if (msg.msg_controllen && error == 0)
1202 error = copyout(&msg.msg_controllen,
1203 &uap->msg->msg_accrightslen, sizeof (int));
1212 struct recvmsg_args /* {
1219 struct iovec *uiov, *iov;
1222 error = copyin(uap->msg, &msg, sizeof (msg));
1225 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1228 msg.msg_flags = uap->flags;
1229 #ifdef COMPAT_OLDSOCK
1230 msg.msg_flags &= ~MSG_COMPAT;
1234 error = recvit(td, uap->s, &msg, NULL);
1237 error = copyout(&msg, uap->msg, sizeof(msg));
1247 struct shutdown_args /* {
1256 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
1259 error = soshutdown(so, uap->how);
1269 struct setsockopt_args /* {
1278 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1279 uap->val, UIO_USERSPACE, uap->valsize));
1283 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1289 enum uio_seg valseg;
1295 struct sockopt sopt;
1297 if (val == NULL && valsize != 0)
1299 if ((int)valsize < 0)
1302 sopt.sopt_dir = SOPT_SET;
1303 sopt.sopt_level = level;
1304 sopt.sopt_name = name;
1305 sopt.sopt_val = val;
1306 sopt.sopt_valsize = valsize;
1312 sopt.sopt_td = NULL;
1315 panic("kern_setsockopt called with bad valseg");
1318 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1321 error = sosetopt(so, &sopt);
1331 struct getsockopt_args /* {
1335 void * __restrict val;
1336 socklen_t * __restrict avalsize;
1343 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1348 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1349 uap->val, UIO_USERSPACE, &valsize);
1352 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1357 * Kernel version of getsockopt.
1358 * optval can be a userland or userspace. optlen is always a kernel pointer.
1361 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1367 enum uio_seg valseg;
1373 struct sockopt sopt;
1377 if ((int)*valsize < 0)
1380 sopt.sopt_dir = SOPT_GET;
1381 sopt.sopt_level = level;
1382 sopt.sopt_name = name;
1383 sopt.sopt_val = val;
1384 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1390 sopt.sopt_td = NULL;
1393 panic("kern_getsockopt called with bad valseg");
1396 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1399 error = sogetopt(so, &sopt);
1400 *valsize = sopt.sopt_valsize;
1407 * getsockname1() - Get socket name.
1411 getsockname1(td, uap, compat)
1413 struct getsockname_args /* {
1415 struct sockaddr * __restrict asa;
1416 socklen_t * __restrict alen;
1420 struct sockaddr *sa;
1424 error = copyin(uap->alen, &len, sizeof(len));
1428 error = kern_getsockname(td, uap->fdes, &sa, &len);
1433 #ifdef COMPAT_OLDSOCK
1435 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1437 error = copyout(sa, uap->asa, (u_int)len);
1441 error = copyout(&len, uap->alen, sizeof(len));
1446 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1457 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1462 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1468 len = MIN(*alen, (*sa)->sa_len);
1471 if (KTRPOINT(td, KTR_STRUCT))
1477 free(*sa, M_SONAME);
1484 getsockname(td, uap)
1486 struct getsockname_args *uap;
1489 return (getsockname1(td, uap, 0));
1492 #ifdef COMPAT_OLDSOCK
1494 ogetsockname(td, uap)
1496 struct getsockname_args *uap;
1499 return (getsockname1(td, uap, 1));
1501 #endif /* COMPAT_OLDSOCK */
1504 * getpeername1() - Get name of peer for connected socket.
1508 getpeername1(td, uap, compat)
1510 struct getpeername_args /* {
1512 struct sockaddr * __restrict asa;
1513 socklen_t * __restrict alen;
1517 struct sockaddr *sa;
1521 error = copyin(uap->alen, &len, sizeof (len));
1525 error = kern_getpeername(td, uap->fdes, &sa, &len);
1530 #ifdef COMPAT_OLDSOCK
1532 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1534 error = copyout(sa, uap->asa, (u_int)len);
1538 error = copyout(&len, uap->alen, sizeof(len));
1543 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1554 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1558 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1563 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1569 len = MIN(*alen, (*sa)->sa_len);
1572 if (KTRPOINT(td, KTR_STRUCT))
1577 free(*sa, M_SONAME);
1586 getpeername(td, uap)
1588 struct getpeername_args *uap;
1591 return (getpeername1(td, uap, 0));
1594 #ifdef COMPAT_OLDSOCK
1596 ogetpeername(td, uap)
1598 struct ogetpeername_args *uap;
1601 /* XXX uap should have type `getpeername_args *' to begin with. */
1602 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1604 #endif /* COMPAT_OLDSOCK */
1607 sockargs(mp, buf, buflen, type)
1612 struct sockaddr *sa;
1616 if ((u_int)buflen > MLEN) {
1617 #ifdef COMPAT_OLDSOCK
1618 if (type == MT_SONAME && (u_int)buflen <= 112)
1619 buflen = MLEN; /* unix domain compat. hack */
1622 if ((u_int)buflen > MCLBYTES)
1625 m = m_get(M_WAIT, type);
1626 if ((u_int)buflen > MLEN)
1629 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1634 if (type == MT_SONAME) {
1635 sa = mtod(m, struct sockaddr *);
1637 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1638 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1639 sa->sa_family = sa->sa_len;
1641 sa->sa_len = buflen;
1648 getsockaddr(namp, uaddr, len)
1649 struct sockaddr **namp;
1653 struct sockaddr *sa;
1656 if (len > SOCK_MAXADDRLEN)
1657 return (ENAMETOOLONG);
1658 if (len < offsetof(struct sockaddr, sa_data[0]))
1660 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1661 error = copyin(uaddr, sa, len);
1665 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1666 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1667 sa->sa_family = sa->sa_len;
1675 #include <sys/condvar.h>
1677 struct sendfile_sync {
1684 * Detach mapped page and release resources back to the system.
1687 sf_buf_mext(void *addr, void *args)
1690 struct sendfile_sync *sfs;
1692 m = sf_buf_page(args);
1694 vm_page_lock_queues();
1695 vm_page_unwire(m, 0);
1697 * Check for the object going away on us. This can
1698 * happen since we don't hold a reference to it.
1699 * If so, we're responsible for freeing the page.
1701 if (m->wire_count == 0 && m->object == NULL)
1703 vm_page_unlock_queues();
1707 mtx_lock(&sfs->mtx);
1708 KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1709 if (--sfs->count == 0)
1710 cv_signal(&sfs->cv);
1711 mtx_unlock(&sfs->mtx);
1717 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1718 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1720 * Send a file specified by 'fd' and starting at 'offset' to a socket
1721 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1722 * 0. Optionally add a header and/or trailer to the socket output. If
1723 * specified, write the total number of bytes sent into *sbytes.
1726 sendfile(struct thread *td, struct sendfile_args *uap)
1729 return (do_sendfile(td, uap, 0));
1733 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1735 struct sf_hdtr hdtr;
1736 struct uio *hdr_uio, *trl_uio;
1739 hdr_uio = trl_uio = NULL;
1741 if (uap->hdtr != NULL) {
1742 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1745 if (hdtr.headers != NULL) {
1746 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1750 if (hdtr.trailers != NULL) {
1751 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1758 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1761 free(hdr_uio, M_IOV);
1763 free(trl_uio, M_IOV);
1767 #ifdef COMPAT_FREEBSD4
1769 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1771 struct sendfile_args args;
1775 args.offset = uap->offset;
1776 args.nbytes = uap->nbytes;
1777 args.hdtr = uap->hdtr;
1778 args.sbytes = uap->sbytes;
1779 args.flags = uap->flags;
1781 return (do_sendfile(td, &args, 1));
1783 #endif /* COMPAT_FREEBSD4 */
1786 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1787 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1789 struct file *sock_fp;
1791 struct vm_object *obj = NULL;
1792 struct socket *so = NULL;
1793 struct mbuf *m = NULL;
1796 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1797 int error, hdrlen = 0, mnw = 0;
1799 struct sendfile_sync *sfs = NULL;
1802 * The file descriptor must be a regular file and have a
1803 * backing VM object.
1804 * File offset must be positive. If it goes beyond EOF
1805 * we send only the header/trailer and no payload data.
1807 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1809 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1810 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1811 if (vp->v_type == VREG) {
1815 * Temporarily increase the backing VM
1816 * object's reference count so that a forced
1817 * reclamation of its vnode does not
1818 * immediately destroy it.
1820 VM_OBJECT_LOCK(obj);
1821 if ((obj->flags & OBJ_DEAD) == 0) {
1822 vm_object_reference_locked(obj);
1823 VM_OBJECT_UNLOCK(obj);
1825 VM_OBJECT_UNLOCK(obj);
1831 VFS_UNLOCK_GIANT(vfslocked);
1836 if (uap->offset < 0) {
1842 * The socket must be a stream socket and connected.
1843 * Remember if it a blocking or non-blocking socket.
1845 if ((error = getsock(td->td_proc->p_fd, uap->s, &sock_fp,
1848 so = sock_fp->f_data;
1849 if (so->so_type != SOCK_STREAM) {
1853 if ((so->so_state & SS_ISCONNECTED) == 0) {
1858 * Do not wait on memory allocations but return ENOMEM for
1859 * caller to retry later.
1860 * XXX: Experimental.
1862 if (uap->flags & SF_MNOWAIT)
1865 if (uap->flags & SF_SYNC) {
1866 sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK);
1867 memset(sfs, 0, sizeof *sfs);
1868 mtx_init(&sfs->mtx, "sendfile", MTX_DEF, 0);
1869 cv_init(&sfs->cv, "sendfile");
1874 error = mac_socket_check_send(td->td_ucred, so);
1880 /* If headers are specified copy them into mbufs. */
1881 if (hdr_uio != NULL) {
1882 hdr_uio->uio_td = td;
1883 hdr_uio->uio_rw = UIO_WRITE;
1884 if (hdr_uio->uio_resid > 0) {
1886 * In FBSD < 5.0 the nbytes to send also included
1887 * the header. If compat is specified subtract the
1888 * header size from nbytes.
1891 if (uap->nbytes > hdr_uio->uio_resid)
1892 uap->nbytes -= hdr_uio->uio_resid;
1896 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1899 error = mnw ? EAGAIN : ENOBUFS;
1902 hdrlen = m_length(m, NULL);
1907 * Protect against multiple writers to the socket.
1909 * XXXRW: Historically this has assumed non-interruptibility, so now
1910 * we implement that, but possibly shouldn't.
1912 (void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1915 * Loop through the pages of the file, starting with the requested
1916 * offset. Get a file page (do I/O if necessary), map the file page
1917 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1919 * This is done in two loops. The inner loop turns as many pages
1920 * as it can, up to available socket buffer space, without blocking
1921 * into mbufs to have it bulk delivered into the socket send buffer.
1922 * The outer loop checks the state and available space of the socket
1923 * and takes care of the overall progress.
1925 for (off = uap->offset, rem = uap->nbytes; ; ) {
1931 * Check the socket state for ongoing connection,
1932 * no errors and space in socket buffer.
1933 * If space is low allow for the remainder of the
1934 * file to be processed if it fits the socket buffer.
1935 * Otherwise block in waiting for sufficient space
1936 * to proceed, or if the socket is nonblocking, return
1937 * to userland with EAGAIN while reporting how far
1939 * We wait until the socket buffer has significant free
1940 * space to do bulk sends. This makes good use of file
1941 * system read ahead and allows packet segmentation
1942 * offloading hardware to take over lots of work. If
1943 * we were not careful here we would send off only one
1946 SOCKBUF_LOCK(&so->so_snd);
1947 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1948 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1950 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1952 SOCKBUF_UNLOCK(&so->so_snd);
1954 } else if (so->so_error) {
1955 error = so->so_error;
1957 SOCKBUF_UNLOCK(&so->so_snd);
1960 space = sbspace(&so->so_snd);
1963 space < so->so_snd.sb_lowat)) {
1964 if (so->so_state & SS_NBIO) {
1965 SOCKBUF_UNLOCK(&so->so_snd);
1970 * sbwait drops the lock while sleeping.
1971 * When we loop back to retry_space the
1972 * state may have changed and we retest
1975 error = sbwait(&so->so_snd);
1977 * An error from sbwait usually indicates that we've
1978 * been interrupted by a signal. If we've sent anything
1979 * then return bytes sent, otherwise return the error.
1982 SOCKBUF_UNLOCK(&so->so_snd);
1987 SOCKBUF_UNLOCK(&so->so_snd);
1990 * Reduce space in the socket buffer by the size of
1991 * the header mbuf chain.
1992 * hdrlen is set to 0 after the first loop.
1997 * Loop and construct maximum sized mbuf chain to be bulk
1998 * dumped into socket buffer.
2000 while(space > loopbytes) {
2005 VM_OBJECT_LOCK(obj);
2007 * Calculate the amount to transfer.
2008 * Not to exceed a page, the EOF,
2009 * or the passed in nbytes.
2011 pgoff = (vm_offset_t)(off & PAGE_MASK);
2012 xfsize = omin(PAGE_SIZE - pgoff,
2013 obj->un_pager.vnp.vnp_size - uap->offset -
2014 fsbytes - loopbytes);
2016 rem = (uap->nbytes - fsbytes - loopbytes);
2018 rem = obj->un_pager.vnp.vnp_size -
2019 uap->offset - fsbytes - loopbytes;
2020 xfsize = omin(rem, xfsize);
2022 VM_OBJECT_UNLOCK(obj);
2023 done = 1; /* all data sent */
2027 * Don't overflow the send buffer.
2028 * Stop here and send out what we've
2031 if (space < loopbytes + xfsize) {
2032 VM_OBJECT_UNLOCK(obj);
2037 * Attempt to look up the page. Allocate
2038 * if not found or wait and loop if busy.
2040 pindex = OFF_TO_IDX(off);
2041 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2042 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2045 * Check if page is valid for what we need,
2046 * otherwise initiate I/O.
2047 * If we already turned some pages into mbufs,
2048 * send them off before we come here again and
2051 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2052 VM_OBJECT_UNLOCK(obj);
2054 error = EAGAIN; /* send what we already got */
2055 else if (uap->flags & SF_NODISKIO)
2061 * Ensure that our page is still around
2062 * when the I/O completes.
2064 vm_page_io_start(pg);
2065 VM_OBJECT_UNLOCK(obj);
2068 * Get the page from backing store.
2070 bsize = vp->v_mount->mnt_stat.f_iosize;
2071 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2072 vn_lock(vp, LK_SHARED | LK_RETRY);
2075 * XXXMAC: Because we don't have fp->f_cred
2076 * here, we pass in NOCRED. This is probably
2077 * wrong, but is consistent with our original
2080 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2081 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2082 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2083 td->td_ucred, NOCRED, &resid, td);
2085 VFS_UNLOCK_GIANT(vfslocked);
2086 VM_OBJECT_LOCK(obj);
2087 vm_page_io_finish(pg);
2089 VM_OBJECT_UNLOCK(obj);
2093 vm_page_lock_queues();
2094 vm_page_unwire(pg, 0);
2096 * See if anyone else might know about
2097 * this page. If not and it is not valid,
2100 if (pg->wire_count == 0 && pg->valid == 0 &&
2101 pg->busy == 0 && !(pg->oflags & VPO_BUSY) &&
2102 pg->hold_count == 0) {
2105 vm_page_unlock_queues();
2106 VM_OBJECT_UNLOCK(obj);
2107 if (error == EAGAIN)
2108 error = 0; /* not a real error */
2113 * Get a sendfile buf. We usually wait as long
2114 * as necessary, but this wait can be interrupted.
2116 if ((sf = sf_buf_alloc(pg,
2117 (mnw ? SFB_NOWAIT : SFB_CATCH))) == NULL) {
2118 mbstat.sf_allocfail++;
2119 vm_page_lock_queues();
2120 vm_page_unwire(pg, 0);
2122 * XXX: Not same check as above!?
2124 if (pg->wire_count == 0 && pg->object == NULL)
2126 vm_page_unlock_queues();
2127 error = (mnw ? EAGAIN : EINTR);
2132 * Get an mbuf and set it up as having
2135 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2137 error = (mnw ? EAGAIN : ENOBUFS);
2138 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2141 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2142 sfs, sf, M_RDONLY, EXT_SFBUF);
2143 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2146 /* Append to mbuf chain. */
2152 /* Keep track of bits processed. */
2153 loopbytes += xfsize;
2157 mtx_lock(&sfs->mtx);
2159 mtx_unlock(&sfs->mtx);
2163 /* Add the buffer chain to the socket buffer. */
2167 mlen = m_length(m, NULL);
2168 SOCKBUF_LOCK(&so->so_snd);
2169 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2171 SOCKBUF_UNLOCK(&so->so_snd);
2174 SOCKBUF_UNLOCK(&so->so_snd);
2175 /* Avoid error aliasing. */
2176 err = (*so->so_proto->pr_usrreqs->pru_send)
2177 (so, 0, m, NULL, NULL, td);
2180 * We need two counters to get the
2181 * file offset and nbytes to send
2183 * - sbytes contains the total amount
2184 * of bytes sent, including headers.
2185 * - fsbytes contains the total amount
2186 * of bytes sent from the file.
2194 } else if (error == 0)
2196 m = NULL; /* pru_send always consumes */
2199 /* Quit outer loop on error or when we're done. */
2207 * Send trailers. Wimp out and use writev(2).
2209 if (trl_uio != NULL) {
2210 error = kern_writev(td, uap->s, trl_uio);
2213 sbytes += td->td_retval[0];
2217 sbunlock(&so->so_snd);
2220 * If there was no error we have to clear td->td_retval[0]
2221 * because it may have been set by writev.
2224 td->td_retval[0] = 0;
2226 if (uap->sbytes != NULL) {
2227 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2230 vm_object_deallocate(obj);
2232 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2234 VFS_UNLOCK_GIANT(vfslocked);
2242 mtx_lock(&sfs->mtx);
2243 if (sfs->count != 0)
2244 cv_wait(&sfs->cv, &sfs->mtx);
2245 KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2246 cv_destroy(&sfs->cv);
2247 mtx_destroy(&sfs->mtx);
2251 if (error == ERESTART)
2259 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2260 * otherwise all return EOPNOTSUPP.
2261 * XXX: We should make this loadable one day.
2264 sctp_peeloff(td, uap)
2266 struct sctp_peeloff_args /* {
2272 struct filedesc *fdp;
2273 struct file *nfp = NULL;
2275 struct socket *head, *so;
2279 fdp = td->td_proc->p_fd;
2280 error = fgetsock(td, uap->sd, &head, &fflag);
2283 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2287 * At this point we know we do have a assoc to pull
2288 * we proceed to get the fd setup. This may block
2292 error = falloc(td, &nfp, &fd);
2295 td->td_retval[0] = fd;
2297 so = sonewconn(head, SS_ISCONNECTED);
2301 * Before changing the flags on the socket, we have to bump the
2302 * reference count. Otherwise, if the protocol calls sofree(),
2303 * the socket will be released due to a zero refcount.
2306 soref(so); /* file descriptor reference */
2311 TAILQ_REMOVE(&head->so_comp, so, so_list);
2313 so->so_state |= (head->so_state & SS_NBIO);
2314 so->so_state &= ~SS_NOFDREF;
2315 so->so_qstate &= ~SQ_COMP;
2318 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2319 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2322 if (head->so_sigio != NULL)
2323 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2327 * close the new descriptor, assuming someone hasn't ripped it
2328 * out from under us.
2331 fdclose(fdp, nfp, fd, td);
2334 * Release explicitly held references before returning.
2343 return (EOPNOTSUPP);
2348 sctp_generic_sendmsg (td, uap)
2350 struct sctp_generic_sendmsg_args /* {
2356 struct sctp_sndrcvinfo *sinfo,
2361 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2363 struct file *fp = NULL;
2364 int use_rcvinfo = 1;
2366 struct sockaddr *to = NULL;
2368 struct uio *ktruio = NULL;
2371 struct iovec iov[1];
2374 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2380 error = getsockaddr(&to, uap->to, uap->tolen);
2387 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2391 if (KTRPOINT(td, KTR_STRUCT))
2395 iov[0].iov_base = uap->msg;
2396 iov[0].iov_len = uap->mlen;
2398 so = (struct socket *)fp->f_data;
2401 error = mac_socket_check_send(td->td_ucred, so);
2408 auio.uio_iovcnt = 1;
2409 auio.uio_segflg = UIO_USERSPACE;
2410 auio.uio_rw = UIO_WRITE;
2412 auio.uio_offset = 0; /* XXX */
2414 len = auio.uio_resid = uap->mlen;
2415 error = sctp_lower_sosend(so, to, &auio,
2416 (struct mbuf *)NULL, (struct mbuf *)NULL,
2417 uap->flags, use_rcvinfo, u_sinfo, td);
2419 if (auio.uio_resid != len && (error == ERESTART ||
2420 error == EINTR || error == EWOULDBLOCK))
2422 /* Generation of SIGPIPE can be controlled per socket. */
2423 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2424 !(uap->flags & MSG_NOSIGNAL)) {
2425 PROC_LOCK(td->td_proc);
2426 psignal(td->td_proc, SIGPIPE);
2427 PROC_UNLOCK(td->td_proc);
2431 td->td_retval[0] = len - auio.uio_resid;
2433 if (ktruio != NULL) {
2434 ktruio->uio_resid = td->td_retval[0];
2435 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2446 return (EOPNOTSUPP);
2451 sctp_generic_sendmsg_iov(td, uap)
2453 struct sctp_generic_sendmsg_iov_args /* {
2459 struct sctp_sndrcvinfo *sinfo,
2464 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2466 struct file *fp = NULL;
2467 int use_rcvinfo = 1;
2468 int error=0, len, i;
2469 struct sockaddr *to = NULL;
2471 struct uio *ktruio = NULL;
2474 struct iovec *iov, *tiov;
2477 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2483 error = getsockaddr(&to, uap->to, uap->tolen);
2490 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2494 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2498 if (KTRPOINT(td, KTR_STRUCT))
2502 so = (struct socket *)fp->f_data;
2505 error = mac_socket_check_send(td->td_ucred, so);
2512 auio.uio_iovcnt = uap->iovlen;
2513 auio.uio_segflg = UIO_USERSPACE;
2514 auio.uio_rw = UIO_WRITE;
2516 auio.uio_offset = 0; /* XXX */
2519 for (i = 0; i <uap->iovlen; i++, tiov++) {
2520 if ((auio.uio_resid += tiov->iov_len) < 0) {
2525 len = auio.uio_resid;
2526 error = sctp_lower_sosend(so, to, &auio,
2527 (struct mbuf *)NULL, (struct mbuf *)NULL,
2528 uap->flags, use_rcvinfo, u_sinfo, td);
2530 if (auio.uio_resid != len && (error == ERESTART ||
2531 error == EINTR || error == EWOULDBLOCK))
2533 /* Generation of SIGPIPE can be controlled per socket */
2534 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2535 !(uap->flags & MSG_NOSIGNAL)) {
2536 PROC_LOCK(td->td_proc);
2537 psignal(td->td_proc, SIGPIPE);
2538 PROC_UNLOCK(td->td_proc);
2542 td->td_retval[0] = len - auio.uio_resid;
2544 if (ktruio != NULL) {
2545 ktruio->uio_resid = td->td_retval[0];
2546 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2559 return (EOPNOTSUPP);
2564 sctp_generic_recvmsg(td, uap)
2566 struct sctp_generic_recvmsg_args /* {
2570 struct sockaddr *from,
2571 __socklen_t *fromlenaddr,
2572 struct sctp_sndrcvinfo *sinfo,
2577 u_int8_t sockbufstore[256];
2579 struct iovec *iov, *tiov;
2580 struct sctp_sndrcvinfo sinfo;
2582 struct file *fp = NULL;
2583 struct sockaddr *fromsa;
2585 int len, i, msg_flags;
2588 struct uio *ktruio = NULL;
2590 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2594 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2602 error = mac_socket_check_receive(td->td_ucred, so);
2610 if (uap->fromlenaddr) {
2611 error = copyin(uap->fromlenaddr,
2612 &fromlen, sizeof (fromlen));
2619 if(uap->msg_flags) {
2620 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2628 auio.uio_iovcnt = uap->iovlen;
2629 auio.uio_segflg = UIO_USERSPACE;
2630 auio.uio_rw = UIO_READ;
2632 auio.uio_offset = 0; /* XXX */
2635 for (i = 0; i <uap->iovlen; i++, tiov++) {
2636 if ((auio.uio_resid += tiov->iov_len) < 0) {
2641 len = auio.uio_resid;
2642 fromsa = (struct sockaddr *)sockbufstore;
2645 if (KTRPOINT(td, KTR_GENIO))
2646 ktruio = cloneuio(&auio);
2648 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2649 fromsa, fromlen, &msg_flags,
2650 (struct sctp_sndrcvinfo *)&sinfo, 1);
2652 if (auio.uio_resid != (int)len && (error == ERESTART ||
2653 error == EINTR || error == EWOULDBLOCK))
2657 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2660 if (ktruio != NULL) {
2661 ktruio->uio_resid = (int)len - auio.uio_resid;
2662 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2667 td->td_retval[0] = (int)len - auio.uio_resid;
2669 if (fromlen && uap->from) {
2671 if (len <= 0 || fromsa == 0)
2674 len = MIN(len, fromsa->sa_len);
2675 error = copyout(fromsa, uap->from, (unsigned)len);
2679 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2685 if (KTRPOINT(td, KTR_STRUCT))
2686 ktrsockaddr(fromsa);
2688 if (uap->msg_flags) {
2689 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2702 return (EOPNOTSUPP);