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)
127 FILEDESC_LOCK_FAST(fdp);
128 fp = fget_locked(fdp, fd);
131 else if (fp->f_type != DTYPE_SOCKET) {
137 *fflagp = fp->f_flag;
140 FILEDESC_UNLOCK_FAST(fdp);
147 * System call interface to the socket abstraction.
149 #if defined(COMPAT_43)
150 #define COMPAT_OLDSOCK
159 register struct socket_args /* {
165 struct filedesc *fdp;
171 error = mac_check_socket_create(td->td_ucred, uap->domain, uap->type,
176 fdp = td->td_proc->p_fd;
177 error = falloc(td, &fp, &fd);
180 /* An extra reference on `fp' has been held for us by falloc(). */
182 error = socreate(uap->domain, &so, uap->type, uap->protocol,
186 fdclose(fdp, fp, fd, td);
188 FILEDESC_LOCK_FAST(fdp);
189 fp->f_data = so; /* already has ref count */
190 fp->f_flag = FREAD|FWRITE;
191 fp->f_ops = &socketops;
192 fp->f_type = DTYPE_SOCKET;
193 FILEDESC_UNLOCK_FAST(fdp);
194 td->td_retval[0] = fd;
207 register struct bind_args /* {
216 if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
219 error = kern_bind(td, uap->s, sa);
225 kern_bind(td, fd, sa)
235 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
241 error = mac_check_socket_bind(td->td_ucred, so, sa);
246 error = sobind(so, sa, td);
263 register struct listen_args /* {
273 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
278 error = mac_check_socket_listen(td->td_ucred, so);
283 error = solisten(so, uap->backlog, td);
298 accept1(td, uap, compat)
300 register struct accept_args /* {
302 struct sockaddr * __restrict name;
303 socklen_t * __restrict anamelen;
307 struct sockaddr *name;
312 if (uap->name == NULL)
313 return (kern_accept(td, uap->s, NULL, NULL, NULL));
315 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
319 error = kern_accept(td, uap->s, &name, &namelen, &fp);
322 * return a namelen of zero for older code which might
323 * ignore the return value from accept.
326 (void) copyout(&namelen,
327 uap->anamelen, sizeof(*uap->anamelen));
331 if (error == 0 && name != NULL) {
332 #ifdef COMPAT_OLDSOCK
334 ((struct osockaddr *)name)->sa_family =
337 error = copyout(name, uap->name, namelen);
340 error = copyout(&namelen, uap->anamelen,
343 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
345 free(name, M_SONAME);
350 kern_accept(struct thread *td, int s, struct sockaddr **name,
351 socklen_t *namelen, struct file **fp)
353 struct filedesc *fdp;
354 struct file *headfp, *nfp = NULL;
355 struct sockaddr *sa = NULL;
357 struct socket *head, *so;
369 fdp = td->td_proc->p_fd;
371 error = getsock(fdp, s, &headfp, &fflag);
374 head = headfp->f_data;
375 if ((head->so_options & SO_ACCEPTCONN) == 0) {
381 error = mac_check_socket_accept(td->td_ucred, head);
386 error = falloc(td, &nfp, &fd);
390 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
395 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
396 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
397 head->so_error = ECONNABORTED;
400 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
407 if (head->so_error) {
408 error = head->so_error;
413 so = TAILQ_FIRST(&head->so_comp);
414 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
415 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
418 * Before changing the flags on the socket, we have to bump the
419 * reference count. Otherwise, if the protocol calls sofree(),
420 * the socket will be released due to a zero refcount.
422 SOCK_LOCK(so); /* soref() and so_state update */
423 soref(so); /* file descriptor reference */
425 TAILQ_REMOVE(&head->so_comp, so, so_list);
427 so->so_state |= (head->so_state & SS_NBIO);
428 so->so_qstate &= ~SQ_COMP;
434 /* An extra reference on `nfp' has been held for us by falloc(). */
435 td->td_retval[0] = fd;
437 /* connection has been removed from the listen queue */
438 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
440 pgid = fgetown(&head->so_sigio);
442 fsetown(pgid, &so->so_sigio);
445 nfp->f_data = so; /* nfp has ref count from falloc */
447 nfp->f_ops = &socketops;
448 nfp->f_type = DTYPE_SOCKET;
450 /* Sync socket nonblocking/async state with file flags */
451 tmp = fflag & FNONBLOCK;
452 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
453 tmp = fflag & FASYNC;
454 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
456 error = soaccept(so, &sa);
459 * return a namelen of zero for older code which might
460 * ignore the return value from accept.
472 /* check sa_len before it is destroyed */
473 if (*namelen > sa->sa_len)
474 *namelen = sa->sa_len;
483 * close the new descriptor, assuming someone hasn't ripped it
487 fdclose(fdp, nfp, fd, td);
490 * Release explicitly held references before returning. We return
491 * a reference on nfp to the caller on success if they request it.
510 * MPSAFE (accept1() is MPSAFE)
515 struct accept_args *uap;
518 return (accept1(td, uap, 0));
521 #ifdef COMPAT_OLDSOCK
523 * MPSAFE (accept1() is MPSAFE)
528 struct accept_args *uap;
531 return (accept1(td, uap, 1));
533 #endif /* COMPAT_OLDSOCK */
542 register struct connect_args /* {
551 error = getsockaddr(&sa, uap->name, uap->namelen);
555 error = kern_connect(td, uap->s, sa);
562 kern_connect(td, fd, sa)
573 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
577 if (so->so_state & SS_ISCONNECTING) {
583 error = mac_check_socket_connect(td->td_ucred, so, sa);
588 error = soconnect(so, sa, td);
591 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
596 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
597 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
600 if (error == EINTR || error == ERESTART)
606 error = so->so_error;
612 so->so_state &= ~SS_ISCONNECTING;
613 if (error == ERESTART)
628 register struct socketpair_args /* {
635 register struct filedesc *fdp = td->td_proc->p_fd;
636 struct file *fp1, *fp2;
637 struct socket *so1, *so2;
638 int fd, error, sv[2];
641 /* We might want to have a separate check for socket pairs. */
642 error = mac_check_socket_create(td->td_ucred, uap->domain, uap->type,
649 error = socreate(uap->domain, &so1, uap->type, uap->protocol,
653 error = socreate(uap->domain, &so2, uap->type, uap->protocol,
657 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
658 error = falloc(td, &fp1, &fd);
662 fp1->f_data = so1; /* so1 already has ref count */
663 error = falloc(td, &fp2, &fd);
666 fp2->f_data = so2; /* so2 already has ref count */
668 error = soconnect2(so1, so2);
671 if (uap->type == SOCK_DGRAM) {
673 * Datagram socket connection is asymmetric.
675 error = soconnect2(so2, so1);
680 fp1->f_flag = FREAD|FWRITE;
681 fp1->f_ops = &socketops;
682 fp1->f_type = DTYPE_SOCKET;
685 fp2->f_flag = FREAD|FWRITE;
686 fp2->f_ops = &socketops;
687 fp2->f_type = DTYPE_SOCKET;
689 error = copyout(sv, uap->rsv, 2 * sizeof (int));
694 fdclose(fdp, fp2, sv[1], td);
697 fdclose(fdp, fp1, sv[0], td);
709 sendit(td, s, mp, flags)
710 register struct thread *td;
712 register struct msghdr *mp;
715 struct mbuf *control;
719 if (mp->msg_name != NULL) {
720 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
730 if (mp->msg_control) {
731 if (mp->msg_controllen < sizeof(struct cmsghdr)
732 #ifdef COMPAT_OLDSOCK
733 && mp->msg_flags != MSG_COMPAT
739 error = sockargs(&control, mp->msg_control,
740 mp->msg_controllen, MT_CONTROL);
743 #ifdef COMPAT_OLDSOCK
744 if (mp->msg_flags == MSG_COMPAT) {
745 register struct cmsghdr *cm;
747 M_PREPEND(control, sizeof(*cm), M_TRYWAIT);
752 cm = mtod(control, struct cmsghdr *);
753 cm->cmsg_len = control->m_len;
754 cm->cmsg_level = SOL_SOCKET;
755 cm->cmsg_type = SCM_RIGHTS;
763 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
772 kern_sendit(td, s, mp, flags, control, segflg)
777 struct mbuf *control;
787 struct uio *ktruio = NULL;
791 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
794 so = (struct socket *)fp->f_data;
798 error = mac_check_socket_send(td->td_ucred, so);
804 auio.uio_iov = mp->msg_iov;
805 auio.uio_iovcnt = mp->msg_iovlen;
806 auio.uio_segflg = segflg;
807 auio.uio_rw = UIO_WRITE;
809 auio.uio_offset = 0; /* XXX */
812 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
813 if ((auio.uio_resid += iov->iov_len) < 0) {
819 if (KTRPOINT(td, KTR_GENIO))
820 ktruio = cloneuio(&auio);
822 len = auio.uio_resid;
823 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
825 if (auio.uio_resid != len && (error == ERESTART ||
826 error == EINTR || error == EWOULDBLOCK))
828 /* Generation of SIGPIPE can be controlled per socket */
829 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
830 !(flags & MSG_NOSIGNAL)) {
831 PROC_LOCK(td->td_proc);
832 psignal(td->td_proc, SIGPIPE);
833 PROC_UNLOCK(td->td_proc);
837 td->td_retval[0] = len - auio.uio_resid;
839 if (ktruio != NULL) {
840 ktruio->uio_resid = td->td_retval[0];
841 ktrgenio(s, UIO_WRITE, ktruio, error);
857 register struct sendto_args /* {
870 msg.msg_name = uap->to;
871 msg.msg_namelen = uap->tolen;
875 #ifdef COMPAT_OLDSOCK
878 aiov.iov_base = uap->buf;
879 aiov.iov_len = uap->len;
880 error = sendit(td, uap->s, &msg, uap->flags);
884 #ifdef COMPAT_OLDSOCK
891 register struct osend_args /* {
906 aiov.iov_base = uap->buf;
907 aiov.iov_len = uap->len;
910 error = sendit(td, uap->s, &msg, uap->flags);
920 struct osendmsg_args /* {
930 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
933 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
937 msg.msg_flags = MSG_COMPAT;
938 error = sendit(td, uap->s, &msg, uap->flags);
950 struct sendmsg_args /* {
960 error = copyin(uap->msg, &msg, sizeof (msg));
963 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
967 #ifdef COMPAT_OLDSOCK
970 error = sendit(td, uap->s, &msg, uap->flags);
976 kern_recvit(td, s, mp, fromseg, controlp)
980 enum uio_seg fromseg;
981 struct mbuf **controlp;
988 struct mbuf *m, *control = 0;
992 struct sockaddr *fromsa = 0;
994 struct uio *ktruio = NULL;
1001 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1010 error = mac_check_socket_receive(td->td_ucred, so);
1019 auio.uio_iov = mp->msg_iov;
1020 auio.uio_iovcnt = mp->msg_iovlen;
1021 auio.uio_segflg = UIO_USERSPACE;
1022 auio.uio_rw = UIO_READ;
1024 auio.uio_offset = 0; /* XXX */
1027 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
1028 if ((auio.uio_resid += iov->iov_len) < 0) {
1035 if (KTRPOINT(td, KTR_GENIO))
1036 ktruio = cloneuio(&auio);
1038 len = auio.uio_resid;
1039 error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
1040 (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
1043 if (auio.uio_resid != (int)len && (error == ERESTART ||
1044 error == EINTR || error == EWOULDBLOCK))
1048 if (ktruio != NULL) {
1049 ktruio->uio_resid = (int)len - auio.uio_resid;
1050 ktrgenio(s, UIO_READ, ktruio, error);
1055 td->td_retval[0] = (int)len - auio.uio_resid;
1057 len = mp->msg_namelen;
1058 if (len <= 0 || fromsa == 0)
1061 /* save sa_len before it is destroyed by MSG_COMPAT */
1062 len = MIN(len, fromsa->sa_len);
1063 #ifdef COMPAT_OLDSOCK
1064 if (mp->msg_flags & MSG_COMPAT)
1065 ((struct osockaddr *)fromsa)->sa_family =
1068 if (fromseg == UIO_USERSPACE) {
1069 error = copyout(fromsa, mp->msg_name,
1074 bcopy(fromsa, mp->msg_name, len);
1076 mp->msg_namelen = len;
1078 if (mp->msg_control && controlp == NULL) {
1079 #ifdef COMPAT_OLDSOCK
1081 * We assume that old recvmsg calls won't receive access
1082 * rights and other control info, esp. as control info
1083 * is always optional and those options didn't exist in 4.3.
1084 * If we receive rights, trim the cmsghdr; anything else
1087 if (control && mp->msg_flags & MSG_COMPAT) {
1088 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1090 mtod(control, struct cmsghdr *)->cmsg_type !=
1092 mp->msg_controllen = 0;
1095 control->m_len -= sizeof (struct cmsghdr);
1096 control->m_data += sizeof (struct cmsghdr);
1099 len = mp->msg_controllen;
1101 mp->msg_controllen = 0;
1102 ctlbuf = mp->msg_control;
1104 while (m && len > 0) {
1105 unsigned int tocopy;
1107 if (len >= m->m_len)
1110 mp->msg_flags |= MSG_CTRUNC;
1114 if ((error = copyout(mtod(m, caddr_t),
1115 ctlbuf, tocopy)) != 0)
1122 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1128 FREE(fromsa, M_SONAME);
1130 if (error == 0 && controlp != NULL)
1131 *controlp = control;
1139 recvit(td, s, mp, namelenp)
1147 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1151 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1152 #ifdef COMPAT_OLDSOCK
1153 if (mp->msg_flags & MSG_COMPAT)
1154 error = 0; /* old recvfrom didn't check */
1166 register struct recvfrom_args /* {
1171 struct sockaddr * __restrict from;
1172 socklen_t * __restrict fromlenaddr;
1179 if (uap->fromlenaddr) {
1180 error = copyin(uap->fromlenaddr,
1181 &msg.msg_namelen, sizeof (msg.msg_namelen));
1185 msg.msg_namelen = 0;
1187 msg.msg_name = uap->from;
1188 msg.msg_iov = &aiov;
1190 aiov.iov_base = uap->buf;
1191 aiov.iov_len = uap->len;
1192 msg.msg_control = 0;
1193 msg.msg_flags = uap->flags;
1194 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1199 #ifdef COMPAT_OLDSOCK
1206 struct recvfrom_args *uap;
1209 uap->flags |= MSG_COMPAT;
1210 return (recvfrom(td, uap));
1215 #ifdef COMPAT_OLDSOCK
1222 register struct orecv_args /* {
1234 msg.msg_namelen = 0;
1235 msg.msg_iov = &aiov;
1237 aiov.iov_base = uap->buf;
1238 aiov.iov_len = uap->len;
1239 msg.msg_control = 0;
1240 msg.msg_flags = uap->flags;
1241 error = recvit(td, uap->s, &msg, NULL);
1246 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1247 * overlays the new one, missing only the flags, and with the (old) access
1248 * rights where the control fields are now.
1255 struct orecvmsg_args /* {
1257 struct omsghdr *msg;
1265 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1268 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1271 msg.msg_flags = uap->flags | MSG_COMPAT;
1273 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1274 if (msg.msg_controllen && error == 0)
1275 error = copyout(&msg.msg_controllen,
1276 &uap->msg->msg_accrightslen, sizeof (int));
1288 struct recvmsg_args /* {
1295 struct iovec *uiov, *iov;
1298 error = copyin(uap->msg, &msg, sizeof (msg));
1301 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1304 msg.msg_flags = uap->flags;
1305 #ifdef COMPAT_OLDSOCK
1306 msg.msg_flags &= ~MSG_COMPAT;
1310 error = recvit(td, uap->s, &msg, NULL);
1313 error = copyout(&msg, uap->msg, sizeof(msg));
1326 register struct shutdown_args /* {
1336 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
1339 error = soshutdown(so, uap->how);
1353 register struct setsockopt_args /* {
1362 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1363 uap->val, UIO_USERSPACE, uap->valsize));
1367 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1373 enum uio_seg valseg;
1379 struct sockopt sopt;
1381 if (val == NULL && valsize != 0)
1383 if ((int)valsize < 0)
1386 sopt.sopt_dir = SOPT_SET;
1387 sopt.sopt_level = level;
1388 sopt.sopt_name = name;
1389 sopt.sopt_val = val;
1390 sopt.sopt_valsize = valsize;
1396 sopt.sopt_td = NULL;
1399 panic("kern_setsockopt called with bad valseg");
1403 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1406 error = sosetopt(so, &sopt);
1420 register struct getsockopt_args /* {
1424 void * __restrict val;
1425 socklen_t * __restrict avalsize;
1432 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1437 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1438 uap->val, UIO_USERSPACE, &valsize);
1441 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1446 * Kernel version of getsockopt.
1447 * optval can be a userland or userspace. optlen is always a kernel pointer.
1450 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1456 enum uio_seg valseg;
1462 struct sockopt sopt;
1466 if ((int)*valsize < 0)
1469 sopt.sopt_dir = SOPT_GET;
1470 sopt.sopt_level = level;
1471 sopt.sopt_name = name;
1472 sopt.sopt_val = val;
1473 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1479 sopt.sopt_td = NULL;
1482 panic("kern_getsockopt called with bad valseg");
1486 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1489 error = sogetopt(so, &sopt);
1490 *valsize = sopt.sopt_valsize;
1498 * getsockname1() - Get socket name.
1504 getsockname1(td, uap, compat)
1506 register struct getsockname_args /* {
1508 struct sockaddr * __restrict asa;
1509 socklen_t * __restrict alen;
1513 struct sockaddr *sa;
1517 error = copyin(uap->alen, &len, sizeof(len));
1521 error = kern_getsockname(td, uap->fdes, &sa, &len);
1526 #ifdef COMPAT_OLDSOCK
1528 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1530 error = copyout(sa, uap->asa, (u_int)len);
1534 error = copyout(&len, uap->alen, sizeof(len));
1539 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1551 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1556 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1562 len = MIN(*alen, (*sa)->sa_len);
1567 free(*sa, M_SONAME);
1579 getsockname(td, uap)
1581 struct getsockname_args *uap;
1584 return (getsockname1(td, uap, 0));
1587 #ifdef COMPAT_OLDSOCK
1592 ogetsockname(td, uap)
1594 struct getsockname_args *uap;
1597 return (getsockname1(td, uap, 1));
1599 #endif /* COMPAT_OLDSOCK */
1602 * getpeername1() - Get name of peer for connected socket.
1608 getpeername1(td, uap, compat)
1610 register struct getpeername_args /* {
1612 struct sockaddr * __restrict asa;
1613 socklen_t * __restrict alen;
1617 struct sockaddr *sa;
1621 error = copyin(uap->alen, &len, sizeof (len));
1625 error = kern_getpeername(td, uap->fdes, &sa, &len);
1630 #ifdef COMPAT_OLDSOCK
1632 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1634 error = copyout(sa, uap->asa, (u_int)len);
1638 error = copyout(&len, uap->alen, sizeof(len));
1643 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1655 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1659 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1664 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1670 len = MIN(*alen, (*sa)->sa_len);
1674 free(*sa, M_SONAME);
1688 getpeername(td, uap)
1690 struct getpeername_args *uap;
1693 return (getpeername1(td, uap, 0));
1696 #ifdef COMPAT_OLDSOCK
1701 ogetpeername(td, uap)
1703 struct ogetpeername_args *uap;
1706 /* XXX uap should have type `getpeername_args *' to begin with. */
1707 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1709 #endif /* COMPAT_OLDSOCK */
1712 sockargs(mp, buf, buflen, type)
1717 register struct sockaddr *sa;
1718 register struct mbuf *m;
1721 if ((u_int)buflen > MLEN) {
1722 #ifdef COMPAT_OLDSOCK
1723 if (type == MT_SONAME && (u_int)buflen <= 112)
1724 buflen = MLEN; /* unix domain compat. hack */
1727 if ((u_int)buflen > MCLBYTES)
1730 m = m_get(M_TRYWAIT, type);
1733 if ((u_int)buflen > MLEN) {
1734 MCLGET(m, M_TRYWAIT);
1735 if ((m->m_flags & M_EXT) == 0) {
1741 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1746 if (type == MT_SONAME) {
1747 sa = mtod(m, struct sockaddr *);
1749 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1750 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1751 sa->sa_family = sa->sa_len;
1753 sa->sa_len = buflen;
1760 getsockaddr(namp, uaddr, len)
1761 struct sockaddr **namp;
1765 struct sockaddr *sa;
1768 if (len > SOCK_MAXADDRLEN)
1769 return (ENAMETOOLONG);
1770 if (len < offsetof(struct sockaddr, sa_data[0]))
1772 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1773 error = copyin(uaddr, sa, len);
1777 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1778 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1779 sa->sa_family = sa->sa_len;
1788 * Detach mapped page and release resources back to the system.
1791 sf_buf_mext(void *addr, void *args)
1795 m = sf_buf_page(args);
1797 vm_page_lock_queues();
1798 vm_page_unwire(m, 0);
1800 * Check for the object going away on us. This can
1801 * happen since we don't hold a reference to it.
1802 * If so, we're responsible for freeing the page.
1804 if (m->wire_count == 0 && m->object == NULL)
1806 vm_page_unlock_queues();
1814 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1815 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1817 * Send a file specified by 'fd' and starting at 'offset' to a socket
1818 * specified by 's'. Send only 'nbytes' of the file or until EOF if
1819 * nbytes == 0. Optionally add a header and/or trailer to the socket
1820 * output. If specified, write the total number of bytes sent into *sbytes.
1824 sendfile(struct thread *td, struct sendfile_args *uap)
1827 return (do_sendfile(td, uap, 0));
1831 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1833 struct sf_hdtr hdtr;
1834 struct uio *hdr_uio, *trl_uio;
1837 hdr_uio = trl_uio = NULL;
1839 if (uap->hdtr != NULL) {
1840 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1843 if (hdtr.headers != NULL) {
1844 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1848 if (hdtr.trailers != NULL) {
1849 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1856 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1859 free(hdr_uio, M_IOV);
1861 free(trl_uio, M_IOV);
1865 #ifdef COMPAT_FREEBSD4
1867 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1869 struct sendfile_args args;
1873 args.offset = uap->offset;
1874 args.nbytes = uap->nbytes;
1875 args.hdtr = uap->hdtr;
1876 args.sbytes = uap->sbytes;
1877 args.flags = uap->flags;
1879 return (do_sendfile(td, &args, 1));
1881 #endif /* COMPAT_FREEBSD4 */
1884 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1885 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1887 struct file *sock_fp;
1889 struct vm_object *obj = NULL;
1890 struct socket *so = NULL;
1891 struct mbuf *m = NULL;
1894 off_t off, xfsize, sbytes = 0, rem = 0;
1901 * The file descriptor must be a regular file and have a
1902 * backing VM object.
1903 * File offset must be positive. If it goes beyond EOF
1904 * we send only the header/trailer and no payload data.
1906 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1908 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1909 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1913 * Temporarily increase the backing VM object's reference
1914 * count so that a forced reclamation of its vnode does not
1915 * immediately destroy it.
1917 VM_OBJECT_LOCK(obj);
1918 if ((obj->flags & OBJ_DEAD) == 0) {
1919 vm_object_reference_locked(obj);
1920 VM_OBJECT_UNLOCK(obj);
1922 VM_OBJECT_UNLOCK(obj);
1926 VOP_UNLOCK(vp, 0, td);
1927 VFS_UNLOCK_GIANT(vfslocked);
1932 if (uap->offset < 0) {
1938 * The socket must be a stream socket and connected.
1939 * Remember if it a blocking or non-blocking socket.
1941 if ((error = getsock(td->td_proc->p_fd, uap->s, &sock_fp,
1944 so = sock_fp->f_data;
1945 if (so->so_type != SOCK_STREAM) {
1949 if ((so->so_state & SS_ISCONNECTED) == 0) {
1954 * Do not wait on memory allocations but return ENOMEM for
1955 * caller to retry later.
1956 * XXX: Experimental.
1958 if (uap->flags & SF_MNOWAIT)
1963 error = mac_check_socket_send(td->td_ucred, so);
1969 /* If headers are specified copy them into mbufs. */
1970 if (hdr_uio != NULL) {
1971 hdr_uio->uio_td = td;
1972 hdr_uio->uio_rw = UIO_WRITE;
1973 if (hdr_uio->uio_resid > 0) {
1975 * In FBSD < 5.0 the nbytes to send also included
1976 * the header. If compat is specified subtract the
1977 * header size from nbytes.
1980 if (uap->nbytes > hdr_uio->uio_resid)
1981 uap->nbytes -= hdr_uio->uio_resid;
1985 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1988 error = mnw ? EAGAIN : ENOBUFS;
1994 /* Protect against multiple writers to the socket. */
1995 SOCKBUF_LOCK(&so->so_snd);
1996 (void) sblock(&so->so_snd, M_WAITOK);
1997 SOCKBUF_UNLOCK(&so->so_snd);
2000 * Loop through the pages of the file, starting with the requested
2001 * offset. Get a file page (do I/O if necessary), map the file page
2002 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
2004 * This is done in two loops. The inner loop turns as many pages
2005 * as it can, up to available socket buffer space, without blocking
2006 * into mbufs to have it bulk delivered into the socket send buffer.
2007 * The outer loop checks the state and available space of the socket
2008 * and takes care of the overall progress.
2010 for (off = uap->offset; ; ) {
2016 * Check the socket state for ongoing connection,
2017 * no errors and space in socket buffer.
2018 * If space is low allow for the remainder of the
2019 * file to be processed if it fits the socket buffer.
2020 * Otherwise block in waiting for sufficient space
2021 * to proceed, or if the socket is nonblocking, return
2022 * to userland with EAGAIN while reporting how far
2024 * We wait until the socket buffer has significant free
2025 * space to do bulk sends. This makes good use of file
2026 * system read ahead and allows packet segmentation
2027 * offloading hardware to take over lots of work. If
2028 * we were not careful here we would send off only one
2031 SOCKBUF_LOCK(&so->so_snd);
2032 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
2033 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
2035 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2037 SOCKBUF_UNLOCK(&so->so_snd);
2039 } else if (so->so_error) {
2040 error = so->so_error;
2042 SOCKBUF_UNLOCK(&so->so_snd);
2045 space = sbspace(&so->so_snd);
2048 space < so->so_snd.sb_lowat)) {
2049 if (so->so_state & SS_NBIO) {
2050 SOCKBUF_UNLOCK(&so->so_snd);
2055 * sbwait drops the lock while sleeping.
2056 * When we loop back to retry_space the
2057 * state may have changed and we retest
2060 error = sbwait(&so->so_snd);
2062 * An error from sbwait usually indicates that we've
2063 * been interrupted by a signal. If we've sent anything
2064 * then return bytes sent, otherwise return the error.
2067 SOCKBUF_UNLOCK(&so->so_snd);
2072 SOCKBUF_UNLOCK(&so->so_snd);
2075 * Loop and construct maximum sized mbuf chain to be bulk
2076 * dumped into socket buffer.
2078 while(space > loopbytes) {
2083 VM_OBJECT_LOCK(obj);
2085 * Calculate the amount to transfer.
2086 * Not to exceed a page, the EOF,
2087 * or the passed in nbytes.
2089 pgoff = (vm_offset_t)(off & PAGE_MASK);
2090 xfsize = omin(PAGE_SIZE - pgoff,
2091 obj->un_pager.vnp.vnp_size - off -
2092 sbytes - loopbytes);
2094 rem = (uap->nbytes - sbytes - loopbytes);
2096 rem = obj->un_pager.vnp.vnp_size - off -
2098 xfsize = omin(rem, xfsize);
2100 VM_OBJECT_UNLOCK(obj);
2101 done = 1; /* all data sent */
2105 * Don't overflow the send buffer.
2106 * Stop here and send out what we've
2109 if (space < loopbytes + xfsize) {
2110 VM_OBJECT_UNLOCK(obj);
2115 * Attempt to look up the page.
2116 * Allocate if not found or
2117 * wait and loop if busy.
2119 pindex = OFF_TO_IDX(off);
2120 pg = vm_page_lookup(obj, pindex);
2122 pg = vm_page_alloc(obj, pindex,
2123 VM_ALLOC_NOBUSY | VM_ALLOC_NORMAL |
2126 VM_OBJECT_UNLOCK(obj);
2128 VM_OBJECT_LOCK(obj);
2131 } else if (vm_page_sleep_if_busy(pg, TRUE, "sfpbsy"))
2135 * Wire the page so it does not get
2136 * ripped out from under us.
2138 vm_page_lock_queues();
2140 vm_page_unlock_queues();
2144 * Check if page is valid for what we need,
2145 * otherwise initiate I/O.
2146 * If we already turned some pages into mbufs,
2147 * send them off before we come here again and
2150 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2151 VM_OBJECT_UNLOCK(obj);
2153 error = EAGAIN; /* send what we already got */
2154 else if (uap->flags & SF_NODISKIO)
2160 * Ensure that our page is still around
2161 * when the I/O completes.
2163 vm_page_io_start(pg);
2164 VM_OBJECT_UNLOCK(obj);
2167 * Get the page from backing store.
2169 bsize = vp->v_mount->mnt_stat.f_iosize;
2170 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2171 vn_lock(vp, LK_SHARED | LK_RETRY, td);
2174 * XXXMAC: Because we don't have fp->f_cred
2175 * here, we pass in NOCRED. This is probably
2176 * wrong, but is consistent with our original
2179 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2180 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2181 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2182 td->td_ucred, NOCRED, &resid, td);
2183 VOP_UNLOCK(vp, 0, td);
2184 VFS_UNLOCK_GIANT(vfslocked);
2185 VM_OBJECT_LOCK(obj);
2186 vm_page_io_finish(pg);
2188 VM_OBJECT_UNLOCK(obj);
2192 vm_page_lock_queues();
2193 vm_page_unwire(pg, 0);
2195 * See if anyone else might know about
2196 * this page. If not and it is not valid,
2199 if (pg->wire_count == 0 && pg->valid == 0 &&
2200 pg->busy == 0 && !(pg->oflags & VPO_BUSY) &&
2201 pg->hold_count == 0) {
2204 vm_page_unlock_queues();
2205 VM_OBJECT_UNLOCK(obj);
2206 if (error == EAGAIN)
2207 error = 0; /* not a real error */
2212 * Get a sendfile buf. We usually wait as long
2213 * as necessary, but this wait can be interrupted.
2215 if ((sf = sf_buf_alloc(pg,
2216 (mnw ? SFB_NOWAIT : SFB_CATCH))) == NULL) {
2217 mbstat.sf_allocfail++;
2218 vm_page_lock_queues();
2219 vm_page_unwire(pg, 0);
2221 * XXX: Not same check as above!?
2223 if (pg->wire_count == 0 && pg->object == NULL)
2225 vm_page_unlock_queues();
2226 error = (mnw ? EAGAIN : EINTR);
2231 * Get an mbuf and set it up as having
2234 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2236 error = (mnw ? EAGAIN : ENOBUFS);
2237 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2240 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2241 sf, M_RDONLY, EXT_SFBUF);
2242 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2245 /* Append to mbuf chain. */
2251 /* Keep track of bits processed. */
2252 loopbytes += xfsize;
2256 /* Add the buffer chain to the socket buffer. */
2260 mlen = m_length(m, NULL);
2261 SOCKBUF_LOCK(&so->so_snd);
2262 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2264 SOCKBUF_UNLOCK(&so->so_snd);
2267 SOCKBUF_UNLOCK(&so->so_snd);
2268 error = (*so->so_proto->pr_usrreqs->pru_send)
2269 (so, 0, m, NULL, NULL, td);
2272 m = NULL; /* pru_send always consumes */
2275 /* Quit outer loop on error or when we're done. */
2281 * Send trailers. Wimp out and use writev(2).
2283 if (trl_uio != NULL) {
2284 error = kern_writev(td, uap->s, trl_uio);
2287 sbytes += td->td_retval[0];
2291 SOCKBUF_LOCK(&so->so_snd);
2292 sbunlock(&so->so_snd);
2293 SOCKBUF_UNLOCK(&so->so_snd);
2296 * If there was no error we have to clear td->td_retval[0]
2297 * because it may have been set by writev.
2300 td->td_retval[0] = 0;
2302 if (uap->sbytes != NULL) {
2303 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2306 vm_object_deallocate(obj);
2308 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2310 VFS_UNLOCK_GIANT(vfslocked);
2319 if (error == ERESTART)
2327 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2328 * otherwise all return EOPNOTSUPP.
2329 * XXX: We should make this loadable one day.
2332 sctp_peeloff(td, uap)
2334 struct sctp_peeloff_args /* {
2340 struct filedesc *fdp;
2341 struct file *nfp = NULL;
2343 struct socket *head, *so;
2347 fdp = td->td_proc->p_fd;
2348 error = fgetsock(td, uap->sd, &head, &fflag);
2351 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2355 * At this point we know we do have a assoc to pull
2356 * we proceed to get the fd setup. This may block
2360 error = falloc(td, &nfp, &fd);
2363 td->td_retval[0] = fd;
2365 so = sonewconn(head, SS_ISCONNECTED);
2369 * Before changing the flags on the socket, we have to bump the
2370 * reference count. Otherwise, if the protocol calls sofree(),
2371 * the socket will be released due to a zero refcount.
2374 soref(so); /* file descriptor reference */
2379 TAILQ_REMOVE(&head->so_comp, so, so_list);
2381 so->so_state |= (head->so_state & SS_NBIO);
2382 so->so_state &= ~SS_NOFDREF;
2383 so->so_qstate &= ~SQ_COMP;
2388 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2391 if (head->so_sigio != NULL)
2392 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2396 nfp->f_flag = fflag;
2397 nfp->f_ops = &socketops;
2398 nfp->f_type = DTYPE_SOCKET;
2403 * close the new descriptor, assuming someone hasn't ripped it
2404 * out from under us.
2407 fdclose(fdp, nfp, fd, td);
2410 * Release explicitly held references before returning.
2419 return (EOPNOTSUPP);
2424 sctp_generic_sendmsg (td, uap)
2426 struct sctp_generic_sendmsg_args /* {
2432 struct sctp_sndrcvinfo *sinfo,
2437 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2440 int use_rcvinfo = 1;
2442 struct sockaddr *to = NULL;
2444 struct uio *ktruio = NULL;
2447 struct iovec iov[1];
2450 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2456 error = getsockaddr(&to, uap->to, uap->tolen);
2463 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2467 iov[0].iov_base = uap->msg;
2468 iov[0].iov_len = uap->mlen;
2470 so = (struct socket *)fp->f_data;
2473 error = mac_check_socket_send(td->td_ucred, so);
2480 auio.uio_iovcnt = 1;
2481 auio.uio_segflg = UIO_USERSPACE;
2482 auio.uio_rw = UIO_WRITE;
2484 auio.uio_offset = 0; /* XXX */
2486 len = auio.uio_resid = uap->mlen;
2487 error = sctp_lower_sosend(so, to, &auio,
2488 (struct mbuf *)NULL, (struct mbuf *)NULL,
2489 uap->flags, use_rcvinfo, u_sinfo, td);
2491 if (auio.uio_resid != len && (error == ERESTART ||
2492 error == EINTR || error == EWOULDBLOCK))
2494 /* Generation of SIGPIPE can be controlled per socket. */
2495 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2496 !(uap->flags & MSG_NOSIGNAL)) {
2497 PROC_LOCK(td->td_proc);
2498 psignal(td->td_proc, SIGPIPE);
2499 PROC_UNLOCK(td->td_proc);
2503 td->td_retval[0] = len - auio.uio_resid;
2505 if (ktruio != NULL) {
2506 ktruio->uio_resid = td->td_retval[0];
2507 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2517 return (EOPNOTSUPP);
2522 sctp_generic_sendmsg_iov(td, uap)
2524 struct sctp_generic_sendmsg_iov_args /* {
2530 struct sctp_sndrcvinfo *sinfo,
2535 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2538 int use_rcvinfo = 1;
2539 int error=0, len, i;
2540 struct sockaddr *to = NULL;
2542 struct uio *ktruio = NULL;
2545 struct iovec *iov, *tiov;
2548 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2554 error = getsockaddr(&to, uap->to, uap->tolen);
2561 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2565 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2569 so = (struct socket *)fp->f_data;
2572 error = mac_check_socket_send(td->td_ucred, so);
2579 auio.uio_iovcnt = uap->iovlen;
2580 auio.uio_segflg = UIO_USERSPACE;
2581 auio.uio_rw = UIO_WRITE;
2583 auio.uio_offset = 0; /* XXX */
2586 for (i = 0; i <uap->iovlen; i++, tiov++) {
2587 if ((auio.uio_resid += tiov->iov_len) < 0) {
2592 len = auio.uio_resid;
2593 error = sctp_lower_sosend(so, to, &auio,
2594 (struct mbuf *)NULL, (struct mbuf *)NULL,
2595 uap->flags, use_rcvinfo, u_sinfo, td);
2597 if (auio.uio_resid != len && (error == ERESTART ||
2598 error == EINTR || error == EWOULDBLOCK))
2600 /* Generation of SIGPIPE can be controlled per socket */
2601 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2602 !(uap->flags & MSG_NOSIGNAL)) {
2603 PROC_LOCK(td->td_proc);
2604 psignal(td->td_proc, SIGPIPE);
2605 PROC_UNLOCK(td->td_proc);
2609 td->td_retval[0] = len - auio.uio_resid;
2611 if (ktruio != NULL) {
2612 ktruio->uio_resid = td->td_retval[0];
2613 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2625 return (EOPNOTSUPP);
2630 sctp_generic_recvmsg(td, uap)
2632 struct sctp_generic_recvmsg_args /* {
2636 struct sockaddr *from,
2637 __socklen_t *fromlenaddr,
2638 struct sctp_sndrcvinfo *sinfo,
2643 u_int8_t sockbufstore[256];
2645 struct iovec *iov, *tiov;
2646 struct sctp_sndrcvinfo sinfo;
2649 struct sockaddr *fromsa;
2651 int len, i, msg_flags;
2654 struct uio *ktruio = NULL;
2656 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2660 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2668 error = mac_check_socket_receive(td->td_ucred, so);
2676 if (uap->fromlenaddr) {
2677 error = copyin(uap->fromlenaddr,
2678 &fromlen, sizeof (fromlen));
2685 if(uap->msg_flags) {
2686 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2694 auio.uio_iovcnt = uap->iovlen;
2695 auio.uio_segflg = UIO_USERSPACE;
2696 auio.uio_rw = UIO_READ;
2698 auio.uio_offset = 0; /* XXX */
2701 for (i = 0; i <uap->iovlen; i++, tiov++) {
2702 if ((auio.uio_resid += tiov->iov_len) < 0) {
2707 len = auio.uio_resid;
2708 fromsa = (struct sockaddr *)sockbufstore;
2711 if (KTRPOINT(td, KTR_GENIO))
2712 ktruio = cloneuio(&auio);
2714 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2715 fromsa, fromlen, &msg_flags,
2716 (struct sctp_sndrcvinfo *)&sinfo, 1);
2718 if (auio.uio_resid != (int)len && (error == ERESTART ||
2719 error == EINTR || error == EWOULDBLOCK))
2723 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2726 if (ktruio != NULL) {
2727 ktruio->uio_resid = (int)len - auio.uio_resid;
2728 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2733 td->td_retval[0] = (int)len - auio.uio_resid;
2735 if (fromlen && uap->from) {
2737 if (len <= 0 || fromsa == 0)
2740 len = MIN(len, fromsa->sa_len);
2741 error = copyout(fromsa, uap->from, (unsigned)len);
2745 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2750 if (uap->msg_flags) {
2751 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2762 return (EOPNOTSUPP);