2 * Copyright (c) 1982, 1986, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_capsicum.h"
36 #include "opt_inet6.h"
38 #include "opt_ktrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/capsicum.h>
43 #include <sys/kernel.h>
45 #include <sys/mutex.h>
46 #include <sys/sysproto.h>
47 #include <sys/malloc.h>
48 #include <sys/filedesc.h>
49 #include <sys/event.h>
51 #include <sys/fcntl.h>
53 #include <sys/filio.h>
55 #include <sys/mount.h>
57 #include <sys/protosw.h>
58 #include <sys/sf_buf.h>
59 #include <sys/sysent.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/signalvar.h>
63 #include <sys/syscall.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysctl.h>
67 #include <sys/vnode.h>
69 #include <sys/ktrace.h>
71 #ifdef COMPAT_FREEBSD32
72 #include <compat/freebsd32/freebsd32_util.h>
77 #include <security/audit/audit.h>
78 #include <security/mac/mac_framework.h>
80 #include <netinet/sctp.h>
81 #include <netinet/sctp_peeloff.h>
83 static struct syscall_helper_data sctp_syscalls[] = {
84 SYSCALL_INIT_HELPER_F(sctp_peeloff, SYF_CAPENABLED),
85 SYSCALL_INIT_HELPER_F(sctp_generic_sendmsg, SYF_CAPENABLED),
86 SYSCALL_INIT_HELPER_F(sctp_generic_sendmsg_iov, SYF_CAPENABLED),
87 SYSCALL_INIT_HELPER_F(sctp_generic_recvmsg, SYF_CAPENABLED),
92 sctp_syscalls_init(void *unused __unused)
96 error = syscall_helper_register(sctp_syscalls, SY_THR_STATIC);
98 ("%s: syscall_helper_register failed for sctp syscalls", __func__));
99 #ifdef COMPAT_FREEBSD32
100 error = syscall32_helper_register(sctp_syscalls, SY_THR_STATIC);
101 KASSERT((error == 0),
102 ("%s: syscall32_helper_register failed for sctp syscalls",
106 SYSINIT(sctp_syscalls, SI_SUB_SYSCALLS, SI_ORDER_ANY, sctp_syscalls_init, NULL);
110 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
111 * otherwise all return EOPNOTSUPP.
112 * XXX: We should make this loadable one day.
115 sys_sctp_peeloff(td, uap)
117 struct sctp_peeloff_args /* {
122 #if (defined(INET) || defined(INET6)) && defined(SCTP)
123 struct file *headfp, *nfp = NULL;
124 struct socket *head, *so;
129 AUDIT_ARG_FD(uap->sd);
130 error = getsock_cap(td, uap->sd, cap_rights_init(&rights, CAP_PEELOFF),
131 &headfp, &fflag, NULL);
134 head = headfp->f_data;
135 if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
139 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
143 * At this point we know we do have a assoc to pull
144 * we proceed to get the fd setup. This may block
148 error = falloc(td, &nfp, &fd, 0);
151 td->td_retval[0] = fd;
153 CURVNET_SET(head->so_vnet);
154 so = sopeeloff(head);
159 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
160 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
163 if (head->so_sigio != NULL)
164 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
168 * close the new descriptor, assuming someone hasn't ripped it
172 fdclose(td, nfp, fd);
175 * Release explicitly held references before returning.
190 sys_sctp_generic_sendmsg (td, uap)
192 struct sctp_generic_sendmsg_args /* {
198 struct sctp_sndrcvinfo *sinfo,
202 #if (defined(INET) || defined(INET6)) && defined(SCTP)
203 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
205 struct file *fp = NULL;
206 struct sockaddr *to = NULL;
208 struct uio *ktruio = NULL;
215 if (uap->sinfo != NULL) {
216 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
222 cap_rights_init(&rights, CAP_SEND);
223 if (uap->tolen != 0) {
224 error = getsockaddr(&to, uap->to, uap->tolen);
229 cap_rights_set(&rights, CAP_CONNECT);
232 AUDIT_ARG_FD(uap->sd);
233 error = getsock_cap(td, uap->sd, &rights, &fp, NULL, NULL);
237 if (to && (KTRPOINT(td, KTR_STRUCT)))
241 iov[0].iov_base = uap->msg;
242 iov[0].iov_len = uap->mlen;
244 so = (struct socket *)fp->f_data;
245 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
250 error = mac_socket_check_send(td->td_ucred, so);
257 auio.uio_segflg = UIO_USERSPACE;
258 auio.uio_rw = UIO_WRITE;
260 auio.uio_offset = 0; /* XXX */
263 if (KTRPOINT(td, KTR_GENIO))
264 ktruio = cloneuio(&auio);
266 len = auio.uio_resid = uap->mlen;
267 CURVNET_SET(so->so_vnet);
268 error = sctp_lower_sosend(so, to, &auio, (struct mbuf *)NULL,
269 (struct mbuf *)NULL, uap->flags, u_sinfo, td);
272 if (auio.uio_resid != len && (error == ERESTART ||
273 error == EINTR || error == EWOULDBLOCK))
275 /* Generation of SIGPIPE can be controlled per socket. */
276 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
277 !(uap->flags & MSG_NOSIGNAL)) {
278 PROC_LOCK(td->td_proc);
279 tdsignal(td, SIGPIPE);
280 PROC_UNLOCK(td->td_proc);
284 td->td_retval[0] = len - auio.uio_resid;
286 if (ktruio != NULL) {
287 ktruio->uio_resid = td->td_retval[0];
288 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
303 sys_sctp_generic_sendmsg_iov(td, uap)
305 struct sctp_generic_sendmsg_iov_args /* {
311 struct sctp_sndrcvinfo *sinfo,
315 #if (defined(INET) || defined(INET6)) && defined(SCTP)
316 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
318 struct file *fp = NULL;
319 struct sockaddr *to = NULL;
321 struct uio *ktruio = NULL;
324 struct iovec *iov, *tiov;
329 if (uap->sinfo != NULL) {
330 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
335 cap_rights_init(&rights, CAP_SEND);
336 if (uap->tolen != 0) {
337 error = getsockaddr(&to, uap->to, uap->tolen);
342 cap_rights_set(&rights, CAP_CONNECT);
345 AUDIT_ARG_FD(uap->sd);
346 error = getsock_cap(td, uap->sd, &rights, &fp, NULL, NULL);
350 #ifdef COMPAT_FREEBSD32
351 if (SV_CURPROC_FLAG(SV_ILP32))
352 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
353 uap->iovlen, &iov, EMSGSIZE);
356 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
360 if (to && (KTRPOINT(td, KTR_STRUCT)))
364 so = (struct socket *)fp->f_data;
365 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
370 error = mac_socket_check_send(td->td_ucred, so);
376 auio.uio_iovcnt = uap->iovlen;
377 auio.uio_segflg = UIO_USERSPACE;
378 auio.uio_rw = UIO_WRITE;
380 auio.uio_offset = 0; /* XXX */
383 for (i = 0; i <uap->iovlen; i++, tiov++) {
384 if ((auio.uio_resid += tiov->iov_len) < 0) {
390 if (KTRPOINT(td, KTR_GENIO))
391 ktruio = cloneuio(&auio);
393 len = auio.uio_resid;
394 CURVNET_SET(so->so_vnet);
395 error = sctp_lower_sosend(so, to, &auio,
396 (struct mbuf *)NULL, (struct mbuf *)NULL,
397 uap->flags, u_sinfo, td);
400 if (auio.uio_resid != len && (error == ERESTART ||
401 error == EINTR || error == EWOULDBLOCK))
403 /* Generation of SIGPIPE can be controlled per socket */
404 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
405 !(uap->flags & MSG_NOSIGNAL)) {
406 PROC_LOCK(td->td_proc);
407 tdsignal(td, SIGPIPE);
408 PROC_UNLOCK(td->td_proc);
412 td->td_retval[0] = len - auio.uio_resid;
414 if (ktruio != NULL) {
415 ktruio->uio_resid = td->td_retval[0];
416 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
433 sys_sctp_generic_recvmsg(td, uap)
435 struct sctp_generic_recvmsg_args /* {
439 struct sockaddr *from,
440 __socklen_t *fromlenaddr,
441 struct sctp_sndrcvinfo *sinfo,
445 #if (defined(INET) || defined(INET6)) && defined(SCTP)
446 uint8_t sockbufstore[256];
448 struct iovec *iov, *tiov;
449 struct sctp_sndrcvinfo sinfo;
451 struct file *fp = NULL;
452 struct sockaddr *fromsa;
455 struct uio *ktruio = NULL;
458 int error, fromlen, i, msg_flags;
460 AUDIT_ARG_FD(uap->sd);
461 error = getsock_cap(td, uap->sd, cap_rights_init(&rights, CAP_RECV),
465 #ifdef COMPAT_FREEBSD32
466 if (SV_CURPROC_FLAG(SV_ILP32))
467 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
468 uap->iovlen, &iov, EMSGSIZE);
471 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
476 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
481 error = mac_socket_check_receive(td->td_ucred, so);
486 if (uap->fromlenaddr != NULL) {
487 error = copyin(uap->fromlenaddr, &fromlen, sizeof (fromlen));
493 if (uap->msg_flags) {
494 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
501 auio.uio_iovcnt = uap->iovlen;
502 auio.uio_segflg = UIO_USERSPACE;
503 auio.uio_rw = UIO_READ;
505 auio.uio_offset = 0; /* XXX */
508 for (i = 0; i <uap->iovlen; i++, tiov++) {
509 if ((auio.uio_resid += tiov->iov_len) < 0) {
514 len = auio.uio_resid;
515 fromsa = (struct sockaddr *)sockbufstore;
518 if (KTRPOINT(td, KTR_GENIO))
519 ktruio = cloneuio(&auio);
521 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
522 CURVNET_SET(so->so_vnet);
523 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
524 fromsa, fromlen, &msg_flags,
525 (struct sctp_sndrcvinfo *)&sinfo, 1);
528 if (auio.uio_resid != len && (error == ERESTART ||
529 error == EINTR || error == EWOULDBLOCK))
533 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
536 if (ktruio != NULL) {
537 ktruio->uio_resid = len - auio.uio_resid;
538 ktrgenio(uap->sd, UIO_READ, ktruio, error);
543 td->td_retval[0] = len - auio.uio_resid;
545 if (fromlen && uap->from) {
547 if (len <= 0 || fromsa == NULL)
550 len = MIN(len, fromsa->sa_len);
551 error = copyout(fromsa, uap->from, (size_t)len);
555 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
560 if (KTRPOINT(td, KTR_STRUCT))
563 if (uap->msg_flags) {
564 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));