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);
227 error = mac_socket_check_bind(td->td_ucred, so, sa);
232 error = sobind(so, sa, td);
244 struct listen_args /* {
253 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
258 error = mac_socket_check_listen(td->td_ucred, so);
263 error = solisten(so, uap->backlog, td);
276 accept1(td, uap, compat)
278 struct accept_args /* {
280 struct sockaddr * __restrict name;
281 socklen_t * __restrict anamelen;
285 struct sockaddr *name;
290 if (uap->name == NULL)
291 return (kern_accept(td, uap->s, NULL, NULL, NULL));
293 error = copyin(uap->anamelen, &namelen, sizeof (namelen));
297 error = kern_accept(td, uap->s, &name, &namelen, &fp);
300 * return a namelen of zero for older code which might
301 * ignore the return value from accept.
304 (void) copyout(&namelen,
305 uap->anamelen, sizeof(*uap->anamelen));
309 if (error == 0 && name != NULL) {
310 #ifdef COMPAT_OLDSOCK
312 ((struct osockaddr *)name)->sa_family =
315 error = copyout(name, uap->name, namelen);
318 error = copyout(&namelen, uap->anamelen,
321 fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
323 free(name, M_SONAME);
328 kern_accept(struct thread *td, int s, struct sockaddr **name,
329 socklen_t *namelen, struct file **fp)
331 struct filedesc *fdp;
332 struct file *headfp, *nfp = NULL;
333 struct sockaddr *sa = NULL;
335 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) {
358 error = mac_socket_check_accept(td->td_ucred, head);
363 error = falloc(td, &nfp, &fd);
367 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
372 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
373 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
374 head->so_error = ECONNABORTED;
377 error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
384 if (head->so_error) {
385 error = head->so_error;
390 so = TAILQ_FIRST(&head->so_comp);
391 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
392 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
395 * Before changing the flags on the socket, we have to bump the
396 * reference count. Otherwise, if the protocol calls sofree(),
397 * the socket will be released due to a zero refcount.
399 SOCK_LOCK(so); /* soref() and so_state update */
400 soref(so); /* file descriptor reference */
402 TAILQ_REMOVE(&head->so_comp, so, so_list);
404 so->so_state |= (head->so_state & SS_NBIO);
405 so->so_qstate &= ~SQ_COMP;
411 /* An extra reference on `nfp' has been held for us by falloc(). */
412 td->td_retval[0] = fd;
414 /* connection has been removed from the listen queue */
415 KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
417 pgid = fgetown(&head->so_sigio);
419 fsetown(pgid, &so->so_sigio);
421 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
422 /* Sync socket nonblocking/async state with file flags */
423 tmp = fflag & FNONBLOCK;
424 (void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
425 tmp = fflag & FASYNC;
426 (void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
428 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;
455 * close the new descriptor, assuming someone hasn't ripped it
459 fdclose(fdp, nfp, fd, td);
462 * Release explicitly held references before returning. We return
463 * a reference on nfp to the caller on success if they request it.
482 struct accept_args *uap;
485 return (accept1(td, uap, 0));
488 #ifdef COMPAT_OLDSOCK
492 struct accept_args *uap;
495 return (accept1(td, uap, 1));
497 #endif /* COMPAT_OLDSOCK */
503 struct connect_args /* {
512 error = getsockaddr(&sa, uap->name, uap->namelen);
516 error = kern_connect(td, uap->s, sa);
523 kern_connect(td, fd, sa)
533 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
537 if (so->so_state & SS_ISCONNECTING) {
543 error = mac_socket_check_connect(td->td_ucred, so, sa);
548 error = soconnect(so, sa, td);
551 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
556 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
557 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
560 if (error == EINTR || error == ERESTART)
566 error = so->so_error;
572 so->so_state &= ~SS_ISCONNECTING;
573 if (error == ERESTART)
583 struct socketpair_args /* {
590 struct filedesc *fdp = td->td_proc->p_fd;
591 struct file *fp1, *fp2;
592 struct socket *so1, *so2;
593 int fd, error, sv[2];
596 /* We might want to have a separate check for socket pairs. */
597 error = mac_socket_check_create(td->td_ucred, uap->domain, uap->type,
603 error = socreate(uap->domain, &so1, uap->type, uap->protocol,
607 error = socreate(uap->domain, &so2, uap->type, uap->protocol,
611 /* On success extra reference to `fp1' and 'fp2' is set by falloc. */
612 error = falloc(td, &fp1, &fd);
616 fp1->f_data = so1; /* so1 already has ref count */
617 error = falloc(td, &fp2, &fd);
620 fp2->f_data = so2; /* so2 already has ref count */
622 error = soconnect2(so1, so2);
625 if (uap->type == SOCK_DGRAM) {
627 * Datagram socket connection is asymmetric.
629 error = soconnect2(so2, so1);
633 finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops);
634 finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops);
636 error = copyout(sv, uap->rsv, 2 * sizeof (int));
643 fdclose(fdp, fp2, sv[1], td);
646 fdclose(fdp, fp1, sv[0], td);
658 sendit(td, s, mp, flags)
664 struct mbuf *control;
668 if (mp->msg_name != NULL) {
669 error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
679 if (mp->msg_control) {
680 if (mp->msg_controllen < sizeof(struct cmsghdr)
681 #ifdef COMPAT_OLDSOCK
682 && mp->msg_flags != MSG_COMPAT
688 error = sockargs(&control, mp->msg_control,
689 mp->msg_controllen, MT_CONTROL);
692 #ifdef COMPAT_OLDSOCK
693 if (mp->msg_flags == MSG_COMPAT) {
696 M_PREPEND(control, sizeof(*cm), M_TRYWAIT);
701 cm = mtod(control, struct cmsghdr *);
702 cm->cmsg_len = control->m_len;
703 cm->cmsg_level = SOL_SOCKET;
704 cm->cmsg_type = SCM_RIGHTS;
712 error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
721 kern_sendit(td, s, mp, flags, control, segflg)
726 struct mbuf *control;
736 struct uio *ktruio = NULL;
739 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
742 so = (struct socket *)fp->f_data;
746 error = mac_socket_check_send(td->td_ucred, so);
752 auio.uio_iov = mp->msg_iov;
753 auio.uio_iovcnt = mp->msg_iovlen;
754 auio.uio_segflg = segflg;
755 auio.uio_rw = UIO_WRITE;
757 auio.uio_offset = 0; /* XXX */
760 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
761 if ((auio.uio_resid += iov->iov_len) < 0) {
767 if (KTRPOINT(td, KTR_GENIO))
768 ktruio = cloneuio(&auio);
770 len = auio.uio_resid;
771 error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
773 if (auio.uio_resid != len && (error == ERESTART ||
774 error == EINTR || error == EWOULDBLOCK))
776 /* Generation of SIGPIPE can be controlled per socket */
777 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
778 !(flags & MSG_NOSIGNAL)) {
779 PROC_LOCK(td->td_proc);
780 psignal(td->td_proc, SIGPIPE);
781 PROC_UNLOCK(td->td_proc);
785 td->td_retval[0] = len - auio.uio_resid;
787 if (ktruio != NULL) {
788 ktruio->uio_resid = td->td_retval[0];
789 ktrgenio(s, UIO_WRITE, ktruio, error);
800 struct sendto_args /* {
813 msg.msg_name = uap->to;
814 msg.msg_namelen = uap->tolen;
818 #ifdef COMPAT_OLDSOCK
821 aiov.iov_base = uap->buf;
822 aiov.iov_len = uap->len;
823 error = sendit(td, uap->s, &msg, uap->flags);
827 #ifdef COMPAT_OLDSOCK
831 struct osend_args /* {
846 aiov.iov_base = uap->buf;
847 aiov.iov_len = uap->len;
850 error = sendit(td, uap->s, &msg, uap->flags);
857 struct osendmsg_args /* {
867 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
870 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
874 msg.msg_flags = MSG_COMPAT;
875 error = sendit(td, uap->s, &msg, uap->flags);
884 struct sendmsg_args /* {
894 error = copyin(uap->msg, &msg, sizeof (msg));
897 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
901 #ifdef COMPAT_OLDSOCK
904 error = sendit(td, uap->s, &msg, uap->flags);
910 kern_recvit(td, s, mp, fromseg, controlp)
914 enum uio_seg fromseg;
915 struct mbuf **controlp;
922 struct mbuf *m, *control = 0;
926 struct sockaddr *fromsa = 0;
928 struct uio *ktruio = NULL;
934 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
941 error = mac_socket_check_receive(td->td_ucred, so);
949 auio.uio_iov = mp->msg_iov;
950 auio.uio_iovcnt = mp->msg_iovlen;
951 auio.uio_segflg = UIO_USERSPACE;
952 auio.uio_rw = UIO_READ;
954 auio.uio_offset = 0; /* XXX */
957 for (i = 0; i < mp->msg_iovlen; i++, iov++) {
958 if ((auio.uio_resid += iov->iov_len) < 0) {
964 if (KTRPOINT(td, KTR_GENIO))
965 ktruio = cloneuio(&auio);
967 len = auio.uio_resid;
968 error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
969 (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
972 if (auio.uio_resid != (int)len && (error == ERESTART ||
973 error == EINTR || error == EWOULDBLOCK))
977 if (ktruio != NULL) {
978 ktruio->uio_resid = (int)len - auio.uio_resid;
979 ktrgenio(s, UIO_READ, ktruio, error);
984 td->td_retval[0] = (int)len - auio.uio_resid;
986 len = mp->msg_namelen;
987 if (len <= 0 || fromsa == 0)
990 /* save sa_len before it is destroyed by MSG_COMPAT */
991 len = MIN(len, fromsa->sa_len);
992 #ifdef COMPAT_OLDSOCK
993 if (mp->msg_flags & MSG_COMPAT)
994 ((struct osockaddr *)fromsa)->sa_family =
997 if (fromseg == UIO_USERSPACE) {
998 error = copyout(fromsa, mp->msg_name,
1003 bcopy(fromsa, mp->msg_name, len);
1005 mp->msg_namelen = len;
1007 if (mp->msg_control && controlp == NULL) {
1008 #ifdef COMPAT_OLDSOCK
1010 * We assume that old recvmsg calls won't receive access
1011 * rights and other control info, esp. as control info
1012 * is always optional and those options didn't exist in 4.3.
1013 * If we receive rights, trim the cmsghdr; anything else
1016 if (control && mp->msg_flags & MSG_COMPAT) {
1017 if (mtod(control, struct cmsghdr *)->cmsg_level !=
1019 mtod(control, struct cmsghdr *)->cmsg_type !=
1021 mp->msg_controllen = 0;
1024 control->m_len -= sizeof (struct cmsghdr);
1025 control->m_data += sizeof (struct cmsghdr);
1028 len = mp->msg_controllen;
1030 mp->msg_controllen = 0;
1031 ctlbuf = mp->msg_control;
1033 while (m && len > 0) {
1034 unsigned int tocopy;
1036 if (len >= m->m_len)
1039 mp->msg_flags |= MSG_CTRUNC;
1043 if ((error = copyout(mtod(m, caddr_t),
1044 ctlbuf, tocopy)) != 0)
1051 mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1056 FREE(fromsa, M_SONAME);
1058 if (error == 0 && controlp != NULL)
1059 *controlp = control;
1067 recvit(td, s, mp, namelenp)
1075 error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1079 error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1080 #ifdef COMPAT_OLDSOCK
1081 if (mp->msg_flags & MSG_COMPAT)
1082 error = 0; /* old recvfrom didn't check */
1091 struct recvfrom_args /* {
1096 struct sockaddr * __restrict from;
1097 socklen_t * __restrict fromlenaddr;
1104 if (uap->fromlenaddr) {
1105 error = copyin(uap->fromlenaddr,
1106 &msg.msg_namelen, sizeof (msg.msg_namelen));
1110 msg.msg_namelen = 0;
1112 msg.msg_name = uap->from;
1113 msg.msg_iov = &aiov;
1115 aiov.iov_base = uap->buf;
1116 aiov.iov_len = uap->len;
1117 msg.msg_control = 0;
1118 msg.msg_flags = uap->flags;
1119 error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1124 #ifdef COMPAT_OLDSOCK
1128 struct recvfrom_args *uap;
1131 uap->flags |= MSG_COMPAT;
1132 return (recvfrom(td, uap));
1136 #ifdef COMPAT_OLDSOCK
1140 struct orecv_args /* {
1152 msg.msg_namelen = 0;
1153 msg.msg_iov = &aiov;
1155 aiov.iov_base = uap->buf;
1156 aiov.iov_len = uap->len;
1157 msg.msg_control = 0;
1158 msg.msg_flags = uap->flags;
1159 error = recvit(td, uap->s, &msg, NULL);
1164 * Old recvmsg. This code takes advantage of the fact that the old msghdr
1165 * overlays the new one, missing only the flags, and with the (old) access
1166 * rights where the control fields are now.
1171 struct orecvmsg_args /* {
1173 struct omsghdr *msg;
1181 error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1184 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1187 msg.msg_flags = uap->flags | MSG_COMPAT;
1189 error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1190 if (msg.msg_controllen && error == 0)
1191 error = copyout(&msg.msg_controllen,
1192 &uap->msg->msg_accrightslen, sizeof (int));
1201 struct recvmsg_args /* {
1208 struct iovec *uiov, *iov;
1211 error = copyin(uap->msg, &msg, sizeof (msg));
1214 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1217 msg.msg_flags = uap->flags;
1218 #ifdef COMPAT_OLDSOCK
1219 msg.msg_flags &= ~MSG_COMPAT;
1223 error = recvit(td, uap->s, &msg, NULL);
1226 error = copyout(&msg, uap->msg, sizeof(msg));
1236 struct shutdown_args /* {
1245 error = getsock(td->td_proc->p_fd, uap->s, &fp, NULL);
1248 error = soshutdown(so, uap->how);
1258 struct setsockopt_args /* {
1267 return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1268 uap->val, UIO_USERSPACE, uap->valsize));
1272 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1278 enum uio_seg valseg;
1284 struct sockopt sopt;
1286 if (val == NULL && valsize != 0)
1288 if ((int)valsize < 0)
1291 sopt.sopt_dir = SOPT_SET;
1292 sopt.sopt_level = level;
1293 sopt.sopt_name = name;
1294 sopt.sopt_val = val;
1295 sopt.sopt_valsize = valsize;
1301 sopt.sopt_td = NULL;
1304 panic("kern_setsockopt called with bad valseg");
1307 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1310 error = sosetopt(so, &sopt);
1320 struct getsockopt_args /* {
1324 void * __restrict val;
1325 socklen_t * __restrict avalsize;
1332 error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1337 error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1338 uap->val, UIO_USERSPACE, &valsize);
1341 error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1346 * Kernel version of getsockopt.
1347 * optval can be a userland or userspace. optlen is always a kernel pointer.
1350 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1356 enum uio_seg valseg;
1362 struct sockopt sopt;
1366 if ((int)*valsize < 0)
1369 sopt.sopt_dir = SOPT_GET;
1370 sopt.sopt_level = level;
1371 sopt.sopt_name = name;
1372 sopt.sopt_val = val;
1373 sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1379 sopt.sopt_td = NULL;
1382 panic("kern_getsockopt called with bad valseg");
1385 error = getsock(td->td_proc->p_fd, s, &fp, NULL);
1388 error = sogetopt(so, &sopt);
1389 *valsize = sopt.sopt_valsize;
1396 * getsockname1() - Get socket name.
1400 getsockname1(td, uap, compat)
1402 struct getsockname_args /* {
1404 struct sockaddr * __restrict asa;
1405 socklen_t * __restrict alen;
1409 struct sockaddr *sa;
1413 error = copyin(uap->alen, &len, sizeof(len));
1417 error = kern_getsockname(td, uap->fdes, &sa, &len);
1422 #ifdef COMPAT_OLDSOCK
1424 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1426 error = copyout(sa, uap->asa, (u_int)len);
1430 error = copyout(&len, uap->alen, sizeof(len));
1435 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1446 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1451 error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1457 len = MIN(*alen, (*sa)->sa_len);
1462 free(*sa, M_SONAME);
1469 getsockname(td, uap)
1471 struct getsockname_args *uap;
1474 return (getsockname1(td, uap, 0));
1477 #ifdef COMPAT_OLDSOCK
1479 ogetsockname(td, uap)
1481 struct getsockname_args *uap;
1484 return (getsockname1(td, uap, 1));
1486 #endif /* COMPAT_OLDSOCK */
1489 * getpeername1() - Get name of peer for connected socket.
1493 getpeername1(td, uap, compat)
1495 struct getpeername_args /* {
1497 struct sockaddr * __restrict asa;
1498 socklen_t * __restrict alen;
1502 struct sockaddr *sa;
1506 error = copyin(uap->alen, &len, sizeof (len));
1510 error = kern_getpeername(td, uap->fdes, &sa, &len);
1515 #ifdef COMPAT_OLDSOCK
1517 ((struct osockaddr *)sa)->sa_family = sa->sa_family;
1519 error = copyout(sa, uap->asa, (u_int)len);
1523 error = copyout(&len, uap->alen, sizeof(len));
1528 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1539 error = getsock(td->td_proc->p_fd, fd, &fp, NULL);
1543 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1548 error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1554 len = MIN(*alen, (*sa)->sa_len);
1558 free(*sa, M_SONAME);
1567 getpeername(td, uap)
1569 struct getpeername_args *uap;
1572 return (getpeername1(td, uap, 0));
1575 #ifdef COMPAT_OLDSOCK
1577 ogetpeername(td, uap)
1579 struct ogetpeername_args *uap;
1582 /* XXX uap should have type `getpeername_args *' to begin with. */
1583 return (getpeername1(td, (struct getpeername_args *)uap, 1));
1585 #endif /* COMPAT_OLDSOCK */
1588 sockargs(mp, buf, buflen, type)
1593 struct sockaddr *sa;
1597 if ((u_int)buflen > MLEN) {
1598 #ifdef COMPAT_OLDSOCK
1599 if (type == MT_SONAME && (u_int)buflen <= 112)
1600 buflen = MLEN; /* unix domain compat. hack */
1603 if ((u_int)buflen > MCLBYTES)
1606 m = m_get(M_TRYWAIT, type);
1609 if ((u_int)buflen > MLEN) {
1610 MCLGET(m, M_TRYWAIT);
1611 if ((m->m_flags & M_EXT) == 0) {
1617 error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1622 if (type == MT_SONAME) {
1623 sa = mtod(m, struct sockaddr *);
1625 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1626 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1627 sa->sa_family = sa->sa_len;
1629 sa->sa_len = buflen;
1636 getsockaddr(namp, uaddr, len)
1637 struct sockaddr **namp;
1641 struct sockaddr *sa;
1644 if (len > SOCK_MAXADDRLEN)
1645 return (ENAMETOOLONG);
1646 if (len < offsetof(struct sockaddr, sa_data[0]))
1648 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
1649 error = copyin(uaddr, sa, len);
1653 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1654 if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1655 sa->sa_family = sa->sa_len;
1664 * Detach mapped page and release resources back to the system.
1667 sf_buf_mext(void *addr, void *args)
1671 m = sf_buf_page(args);
1673 vm_page_lock_queues();
1674 vm_page_unwire(m, 0);
1676 * Check for the object going away on us. This can
1677 * happen since we don't hold a reference to it.
1678 * If so, we're responsible for freeing the page.
1680 if (m->wire_count == 0 && m->object == NULL)
1682 vm_page_unlock_queues();
1688 * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1689 * struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1691 * Send a file specified by 'fd' and starting at 'offset' to a socket
1692 * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1693 * 0. Optionally add a header and/or trailer to the socket output. If
1694 * specified, write the total number of bytes sent into *sbytes.
1697 sendfile(struct thread *td, struct sendfile_args *uap)
1700 return (do_sendfile(td, uap, 0));
1704 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1706 struct sf_hdtr hdtr;
1707 struct uio *hdr_uio, *trl_uio;
1710 hdr_uio = trl_uio = NULL;
1712 if (uap->hdtr != NULL) {
1713 error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1716 if (hdtr.headers != NULL) {
1717 error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1721 if (hdtr.trailers != NULL) {
1722 error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1729 error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1732 free(hdr_uio, M_IOV);
1734 free(trl_uio, M_IOV);
1738 #ifdef COMPAT_FREEBSD4
1740 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1742 struct sendfile_args args;
1746 args.offset = uap->offset;
1747 args.nbytes = uap->nbytes;
1748 args.hdtr = uap->hdtr;
1749 args.sbytes = uap->sbytes;
1750 args.flags = uap->flags;
1752 return (do_sendfile(td, &args, 1));
1754 #endif /* COMPAT_FREEBSD4 */
1757 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1758 struct uio *hdr_uio, struct uio *trl_uio, int compat)
1760 struct file *sock_fp;
1762 struct vm_object *obj = NULL;
1763 struct socket *so = NULL;
1764 struct mbuf *m = NULL;
1767 off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1768 int error, hdrlen = 0, mnw = 0;
1772 * The file descriptor must be a regular file and have a
1773 * backing VM object.
1774 * File offset must be positive. If it goes beyond EOF
1775 * we send only the header/trailer and no payload data.
1777 if ((error = fgetvp_read(td, uap->fd, &vp)) != 0)
1779 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1780 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1784 * Temporarily increase the backing VM object's reference
1785 * count so that a forced reclamation of its vnode does not
1786 * immediately destroy it.
1788 VM_OBJECT_LOCK(obj);
1789 if ((obj->flags & OBJ_DEAD) == 0) {
1790 vm_object_reference_locked(obj);
1791 VM_OBJECT_UNLOCK(obj);
1793 VM_OBJECT_UNLOCK(obj);
1797 VOP_UNLOCK(vp, 0, td);
1798 VFS_UNLOCK_GIANT(vfslocked);
1803 if (uap->offset < 0) {
1809 * The socket must be a stream socket and connected.
1810 * Remember if it a blocking or non-blocking socket.
1812 if ((error = getsock(td->td_proc->p_fd, uap->s, &sock_fp,
1815 so = sock_fp->f_data;
1816 if (so->so_type != SOCK_STREAM) {
1820 if ((so->so_state & SS_ISCONNECTED) == 0) {
1825 * Do not wait on memory allocations but return ENOMEM for
1826 * caller to retry later.
1827 * XXX: Experimental.
1829 if (uap->flags & SF_MNOWAIT)
1834 error = mac_socket_check_send(td->td_ucred, so);
1840 /* If headers are specified copy them into mbufs. */
1841 if (hdr_uio != NULL) {
1842 hdr_uio->uio_td = td;
1843 hdr_uio->uio_rw = UIO_WRITE;
1844 if (hdr_uio->uio_resid > 0) {
1846 * In FBSD < 5.0 the nbytes to send also included
1847 * the header. If compat is specified subtract the
1848 * header size from nbytes.
1851 if (uap->nbytes > hdr_uio->uio_resid)
1852 uap->nbytes -= hdr_uio->uio_resid;
1856 m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1859 error = mnw ? EAGAIN : ENOBUFS;
1862 hdrlen = m_length(m, NULL);
1866 /* Protect against multiple writers to the socket. */
1867 (void) sblock(&so->so_snd, M_WAITOK);
1870 * Loop through the pages of the file, starting with the requested
1871 * offset. Get a file page (do I/O if necessary), map the file page
1872 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1874 * This is done in two loops. The inner loop turns as many pages
1875 * as it can, up to available socket buffer space, without blocking
1876 * into mbufs to have it bulk delivered into the socket send buffer.
1877 * The outer loop checks the state and available space of the socket
1878 * and takes care of the overall progress.
1880 for (off = uap->offset, rem = uap->nbytes; ; ) {
1886 * Check the socket state for ongoing connection,
1887 * no errors and space in socket buffer.
1888 * If space is low allow for the remainder of the
1889 * file to be processed if it fits the socket buffer.
1890 * Otherwise block in waiting for sufficient space
1891 * to proceed, or if the socket is nonblocking, return
1892 * to userland with EAGAIN while reporting how far
1894 * We wait until the socket buffer has significant free
1895 * space to do bulk sends. This makes good use of file
1896 * system read ahead and allows packet segmentation
1897 * offloading hardware to take over lots of work. If
1898 * we were not careful here we would send off only one
1901 SOCKBUF_LOCK(&so->so_snd);
1902 if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1903 so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1905 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1907 SOCKBUF_UNLOCK(&so->so_snd);
1909 } else if (so->so_error) {
1910 error = so->so_error;
1912 SOCKBUF_UNLOCK(&so->so_snd);
1915 space = sbspace(&so->so_snd);
1918 space < so->so_snd.sb_lowat)) {
1919 if (so->so_state & SS_NBIO) {
1920 SOCKBUF_UNLOCK(&so->so_snd);
1925 * sbwait drops the lock while sleeping.
1926 * When we loop back to retry_space the
1927 * state may have changed and we retest
1930 error = sbwait(&so->so_snd);
1932 * An error from sbwait usually indicates that we've
1933 * been interrupted by a signal. If we've sent anything
1934 * then return bytes sent, otherwise return the error.
1937 SOCKBUF_UNLOCK(&so->so_snd);
1942 SOCKBUF_UNLOCK(&so->so_snd);
1945 * Reduce space in the socket buffer by the size of
1946 * the header mbuf chain.
1947 * hdrlen is set to 0 after the first loop.
1952 * Loop and construct maximum sized mbuf chain to be bulk
1953 * dumped into socket buffer.
1955 while(space > loopbytes) {
1960 VM_OBJECT_LOCK(obj);
1962 * Calculate the amount to transfer.
1963 * Not to exceed a page, the EOF,
1964 * or the passed in nbytes.
1966 pgoff = (vm_offset_t)(off & PAGE_MASK);
1967 xfsize = omin(PAGE_SIZE - pgoff,
1968 obj->un_pager.vnp.vnp_size - uap->offset -
1969 fsbytes - loopbytes);
1971 rem = (uap->nbytes - fsbytes - loopbytes);
1973 rem = obj->un_pager.vnp.vnp_size -
1974 uap->offset - fsbytes - loopbytes;
1975 xfsize = omin(rem, xfsize);
1977 VM_OBJECT_UNLOCK(obj);
1978 done = 1; /* all data sent */
1982 * Don't overflow the send buffer.
1983 * Stop here and send out what we've
1986 if (space < loopbytes + xfsize) {
1987 VM_OBJECT_UNLOCK(obj);
1992 * Attempt to look up the page. Allocate
1993 * if not found or wait and loop if busy.
1995 pindex = OFF_TO_IDX(off);
1996 pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
1997 VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2000 * Check if page is valid for what we need,
2001 * otherwise initiate I/O.
2002 * If we already turned some pages into mbufs,
2003 * send them off before we come here again and
2006 if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2007 VM_OBJECT_UNLOCK(obj);
2009 error = EAGAIN; /* send what we already got */
2010 else if (uap->flags & SF_NODISKIO)
2016 * Ensure that our page is still around
2017 * when the I/O completes.
2019 vm_page_io_start(pg);
2020 VM_OBJECT_UNLOCK(obj);
2023 * Get the page from backing store.
2025 bsize = vp->v_mount->mnt_stat.f_iosize;
2026 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2027 vn_lock(vp, LK_SHARED | LK_RETRY, td);
2030 * XXXMAC: Because we don't have fp->f_cred
2031 * here, we pass in NOCRED. This is probably
2032 * wrong, but is consistent with our original
2035 error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2036 trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2037 IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2038 td->td_ucred, NOCRED, &resid, td);
2039 VOP_UNLOCK(vp, 0, td);
2040 VFS_UNLOCK_GIANT(vfslocked);
2041 VM_OBJECT_LOCK(obj);
2042 vm_page_io_finish(pg);
2044 VM_OBJECT_UNLOCK(obj);
2048 vm_page_lock_queues();
2049 vm_page_unwire(pg, 0);
2051 * See if anyone else might know about
2052 * this page. If not and it is not valid,
2055 if (pg->wire_count == 0 && pg->valid == 0 &&
2056 pg->busy == 0 && !(pg->oflags & VPO_BUSY) &&
2057 pg->hold_count == 0) {
2060 vm_page_unlock_queues();
2061 VM_OBJECT_UNLOCK(obj);
2062 if (error == EAGAIN)
2063 error = 0; /* not a real error */
2068 * Get a sendfile buf. We usually wait as long
2069 * as necessary, but this wait can be interrupted.
2071 if ((sf = sf_buf_alloc(pg,
2072 (mnw ? SFB_NOWAIT : SFB_CATCH))) == NULL) {
2073 mbstat.sf_allocfail++;
2074 vm_page_lock_queues();
2075 vm_page_unwire(pg, 0);
2077 * XXX: Not same check as above!?
2079 if (pg->wire_count == 0 && pg->object == NULL)
2081 vm_page_unlock_queues();
2082 error = (mnw ? EAGAIN : EINTR);
2087 * Get an mbuf and set it up as having
2090 m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2092 error = (mnw ? EAGAIN : ENOBUFS);
2093 sf_buf_mext((void *)sf_buf_kva(sf), sf);
2096 MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2097 sf, M_RDONLY, EXT_SFBUF);
2098 m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2101 /* Append to mbuf chain. */
2107 /* Keep track of bits processed. */
2108 loopbytes += xfsize;
2112 /* Add the buffer chain to the socket buffer. */
2116 mlen = m_length(m, NULL);
2117 SOCKBUF_LOCK(&so->so_snd);
2118 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2120 SOCKBUF_UNLOCK(&so->so_snd);
2123 SOCKBUF_UNLOCK(&so->so_snd);
2124 /* Avoid error aliasing. */
2125 err = (*so->so_proto->pr_usrreqs->pru_send)
2126 (so, 0, m, NULL, NULL, td);
2129 * We need two counters to get the
2130 * file offset and nbytes to send
2132 * - sbytes contains the total amount
2133 * of bytes sent, including headers.
2134 * - fsbytes contains the total amount
2135 * of bytes sent from the file.
2143 } else if (error == 0)
2145 m = NULL; /* pru_send always consumes */
2148 /* Quit outer loop on error or when we're done. */
2154 * Send trailers. Wimp out and use writev(2).
2156 if (trl_uio != NULL) {
2157 error = kern_writev(td, uap->s, trl_uio);
2160 sbytes += td->td_retval[0];
2164 sbunlock(&so->so_snd);
2167 * If there was no error we have to clear td->td_retval[0]
2168 * because it may have been set by writev.
2171 td->td_retval[0] = 0;
2173 if (uap->sbytes != NULL) {
2174 copyout(&sbytes, uap->sbytes, sizeof(off_t));
2177 vm_object_deallocate(obj);
2179 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2181 VFS_UNLOCK_GIANT(vfslocked);
2188 if (error == ERESTART)
2196 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2197 * otherwise all return EOPNOTSUPP.
2198 * XXX: We should make this loadable one day.
2201 sctp_peeloff(td, uap)
2203 struct sctp_peeloff_args /* {
2209 struct filedesc *fdp;
2210 struct file *nfp = NULL;
2212 struct socket *head, *so;
2216 fdp = td->td_proc->p_fd;
2217 error = fgetsock(td, uap->sd, &head, &fflag);
2220 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2224 * At this point we know we do have a assoc to pull
2225 * we proceed to get the fd setup. This may block
2229 error = falloc(td, &nfp, &fd);
2232 td->td_retval[0] = fd;
2234 so = sonewconn(head, SS_ISCONNECTED);
2238 * Before changing the flags on the socket, we have to bump the
2239 * reference count. Otherwise, if the protocol calls sofree(),
2240 * the socket will be released due to a zero refcount.
2243 soref(so); /* file descriptor reference */
2248 TAILQ_REMOVE(&head->so_comp, so, so_list);
2250 so->so_state |= (head->so_state & SS_NBIO);
2251 so->so_state &= ~SS_NOFDREF;
2252 so->so_qstate &= ~SQ_COMP;
2255 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2256 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2259 if (head->so_sigio != NULL)
2260 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2264 * close the new descriptor, assuming someone hasn't ripped it
2265 * out from under us.
2268 fdclose(fdp, nfp, fd, td);
2271 * Release explicitly held references before returning.
2280 return (EOPNOTSUPP);
2285 sctp_generic_sendmsg (td, uap)
2287 struct sctp_generic_sendmsg_args /* {
2293 struct sctp_sndrcvinfo *sinfo,
2298 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2300 struct file *fp = NULL;
2301 int use_rcvinfo = 1;
2303 struct sockaddr *to = NULL;
2305 struct uio *ktruio = NULL;
2308 struct iovec iov[1];
2311 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2317 error = getsockaddr(&to, uap->to, uap->tolen);
2324 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2328 iov[0].iov_base = uap->msg;
2329 iov[0].iov_len = uap->mlen;
2331 so = (struct socket *)fp->f_data;
2334 error = mac_socket_check_send(td->td_ucred, so);
2341 auio.uio_iovcnt = 1;
2342 auio.uio_segflg = UIO_USERSPACE;
2343 auio.uio_rw = UIO_WRITE;
2345 auio.uio_offset = 0; /* XXX */
2347 len = auio.uio_resid = uap->mlen;
2348 error = sctp_lower_sosend(so, to, &auio,
2349 (struct mbuf *)NULL, (struct mbuf *)NULL,
2350 uap->flags, use_rcvinfo, u_sinfo, td);
2352 if (auio.uio_resid != len && (error == ERESTART ||
2353 error == EINTR || error == EWOULDBLOCK))
2355 /* Generation of SIGPIPE can be controlled per socket. */
2356 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2357 !(uap->flags & MSG_NOSIGNAL)) {
2358 PROC_LOCK(td->td_proc);
2359 psignal(td->td_proc, SIGPIPE);
2360 PROC_UNLOCK(td->td_proc);
2364 td->td_retval[0] = len - auio.uio_resid;
2366 if (ktruio != NULL) {
2367 ktruio->uio_resid = td->td_retval[0];
2368 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2379 return (EOPNOTSUPP);
2384 sctp_generic_sendmsg_iov(td, uap)
2386 struct sctp_generic_sendmsg_iov_args /* {
2392 struct sctp_sndrcvinfo *sinfo,
2397 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2399 struct file *fp = NULL;
2400 int use_rcvinfo = 1;
2401 int error=0, len, i;
2402 struct sockaddr *to = NULL;
2404 struct uio *ktruio = NULL;
2407 struct iovec *iov, *tiov;
2410 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2416 error = getsockaddr(&to, uap->to, uap->tolen);
2423 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2427 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2431 so = (struct socket *)fp->f_data;
2434 error = mac_socket_check_send(td->td_ucred, so);
2441 auio.uio_iovcnt = uap->iovlen;
2442 auio.uio_segflg = UIO_USERSPACE;
2443 auio.uio_rw = UIO_WRITE;
2445 auio.uio_offset = 0; /* XXX */
2448 for (i = 0; i <uap->iovlen; i++, tiov++) {
2449 if ((auio.uio_resid += tiov->iov_len) < 0) {
2454 len = auio.uio_resid;
2455 error = sctp_lower_sosend(so, to, &auio,
2456 (struct mbuf *)NULL, (struct mbuf *)NULL,
2457 uap->flags, use_rcvinfo, u_sinfo, td);
2459 if (auio.uio_resid != len && (error == ERESTART ||
2460 error == EINTR || error == EWOULDBLOCK))
2462 /* Generation of SIGPIPE can be controlled per socket */
2463 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2464 !(uap->flags & MSG_NOSIGNAL)) {
2465 PROC_LOCK(td->td_proc);
2466 psignal(td->td_proc, SIGPIPE);
2467 PROC_UNLOCK(td->td_proc);
2471 td->td_retval[0] = len - auio.uio_resid;
2473 if (ktruio != NULL) {
2474 ktruio->uio_resid = td->td_retval[0];
2475 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2488 return (EOPNOTSUPP);
2493 sctp_generic_recvmsg(td, uap)
2495 struct sctp_generic_recvmsg_args /* {
2499 struct sockaddr *from,
2500 __socklen_t *fromlenaddr,
2501 struct sctp_sndrcvinfo *sinfo,
2506 u_int8_t sockbufstore[256];
2508 struct iovec *iov, *tiov;
2509 struct sctp_sndrcvinfo sinfo;
2511 struct file *fp = NULL;
2512 struct sockaddr *fromsa;
2514 int len, i, msg_flags;
2517 struct uio *ktruio = NULL;
2519 error = getsock(td->td_proc->p_fd, uap->sd, &fp, NULL);
2523 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2531 error = mac_socket_check_receive(td->td_ucred, so);
2539 if (uap->fromlenaddr) {
2540 error = copyin(uap->fromlenaddr,
2541 &fromlen, sizeof (fromlen));
2548 if(uap->msg_flags) {
2549 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2557 auio.uio_iovcnt = uap->iovlen;
2558 auio.uio_segflg = UIO_USERSPACE;
2559 auio.uio_rw = UIO_READ;
2561 auio.uio_offset = 0; /* XXX */
2564 for (i = 0; i <uap->iovlen; i++, tiov++) {
2565 if ((auio.uio_resid += tiov->iov_len) < 0) {
2570 len = auio.uio_resid;
2571 fromsa = (struct sockaddr *)sockbufstore;
2574 if (KTRPOINT(td, KTR_GENIO))
2575 ktruio = cloneuio(&auio);
2577 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2578 fromsa, fromlen, &msg_flags,
2579 (struct sctp_sndrcvinfo *)&sinfo, 1);
2581 if (auio.uio_resid != (int)len && (error == ERESTART ||
2582 error == EINTR || error == EWOULDBLOCK))
2586 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2589 if (ktruio != NULL) {
2590 ktruio->uio_resid = (int)len - auio.uio_resid;
2591 ktrgenio(uap->sd, UIO_READ, ktruio, error);
2596 td->td_retval[0] = (int)len - auio.uio_resid;
2598 if (fromlen && uap->from) {
2600 if (len <= 0 || fromsa == 0)
2603 len = MIN(len, fromsa->sa_len);
2604 error = copyout(fromsa, uap->from, (unsigned)len);
2608 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2613 if (uap->msg_flags) {
2614 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2627 return (EOPNOTSUPP);