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 * 4. 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_compat.h"
39 #include "opt_ktrace.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/capsicum.h>
44 #include <sys/kernel.h>
46 #include <sys/mutex.h>
47 #include <sys/sysproto.h>
48 #include <sys/malloc.h>
49 #include <sys/filedesc.h>
50 #include <sys/event.h>
52 #include <sys/fcntl.h>
54 #include <sys/filio.h>
56 #include <sys/mount.h>
58 #include <sys/protosw.h>
59 #include <sys/sf_buf.h>
60 #include <sys/sysent.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/signalvar.h>
64 #include <sys/syscall.h>
65 #include <sys/syscallsubr.h>
66 #include <sys/sysctl.h>
68 #include <sys/vnode.h>
70 #include <sys/ktrace.h>
72 #ifdef COMPAT_FREEBSD32
73 #include <compat/freebsd32/freebsd32_util.h>
78 #include <security/audit/audit.h>
79 #include <security/mac/mac_framework.h>
81 #include <netinet/sctp.h>
82 #include <netinet/sctp_peeloff.h>
84 static struct syscall_helper_data sctp_syscalls[] = {
85 SYSCALL_INIT_HELPER(sctp_peeloff),
86 SYSCALL_INIT_HELPER(sctp_generic_sendmsg),
87 SYSCALL_INIT_HELPER(sctp_generic_sendmsg_iov),
88 SYSCALL_INIT_HELPER(sctp_generic_recvmsg),
93 sctp_syscalls_init(void *unused __unused)
97 error = syscall_helper_register(sctp_syscalls);
99 ("%s: syscall_helper_register failed for sctp syscalls", __func__));
100 #ifdef COMPAT_FREEBSD32
101 error = syscall32_helper_register(sctp_syscalls);
102 KASSERT((error == 0),
103 ("%s: syscall32_helper_register failed for sctp syscalls",
107 SYSINIT(sctp_syscalls, SI_SUB_SYSCALLS, SI_ORDER_ANY, sctp_syscalls_init, NULL);
111 * Functionality only compiled in if SCTP is defined in the kernel Makefile,
112 * otherwise all return EOPNOTSUPP.
113 * XXX: We should make this loadable one day.
116 sys_sctp_peeloff(td, uap)
118 struct sctp_peeloff_args /* {
123 #if (defined(INET) || defined(INET6)) && defined(SCTP)
124 struct file *nfp = NULL;
125 struct socket *head, *so;
130 AUDIT_ARG_FD(uap->sd);
131 error = fgetsock(td, uap->sd, cap_rights_init(&rights, CAP_PEELOFF),
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 = sonewconn(head, SS_ISCONNECTED);
160 * Before changing the flags on the socket, we have to bump the
161 * reference count. Otherwise, if the protocol calls sofree(),
162 * the socket will be released due to a zero refcount.
165 soref(so); /* file descriptor reference */
170 TAILQ_REMOVE(&head->so_comp, so, so_list);
172 so->so_state |= (head->so_state & SS_NBIO);
173 so->so_state &= ~SS_NOFDREF;
174 so->so_qstate &= ~SQ_COMP;
177 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
178 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
181 if (head->so_sigio != NULL)
182 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
186 * close the new descriptor, assuming someone hasn't ripped it
190 fdclose(td, nfp, fd);
193 * Release explicitly held references before returning.
208 sys_sctp_generic_sendmsg (td, uap)
210 struct sctp_generic_sendmsg_args /* {
216 struct sctp_sndrcvinfo *sinfo,
220 #if (defined(INET) || defined(INET6)) && defined(SCTP)
221 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
223 struct file *fp = NULL;
224 struct sockaddr *to = NULL;
226 struct uio *ktruio = NULL;
233 if (uap->sinfo != NULL) {
234 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
240 cap_rights_init(&rights, CAP_SEND);
241 if (uap->tolen != 0) {
242 error = getsockaddr(&to, uap->to, uap->tolen);
247 cap_rights_set(&rights, CAP_CONNECT);
250 AUDIT_ARG_FD(uap->sd);
251 error = getsock_cap(td, uap->sd, &rights, &fp, NULL);
255 if (to && (KTRPOINT(td, KTR_STRUCT)))
259 iov[0].iov_base = uap->msg;
260 iov[0].iov_len = uap->mlen;
262 so = (struct socket *)fp->f_data;
263 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
268 error = mac_socket_check_send(td->td_ucred, so);
275 auio.uio_segflg = UIO_USERSPACE;
276 auio.uio_rw = UIO_WRITE;
278 auio.uio_offset = 0; /* XXX */
281 if (KTRPOINT(td, KTR_GENIO))
282 ktruio = cloneuio(&auio);
284 len = auio.uio_resid = uap->mlen;
285 CURVNET_SET(so->so_vnet);
286 error = sctp_lower_sosend(so, to, &auio, (struct mbuf *)NULL,
287 (struct mbuf *)NULL, uap->flags, u_sinfo, td);
290 if (auio.uio_resid != len && (error == ERESTART ||
291 error == EINTR || error == EWOULDBLOCK))
293 /* Generation of SIGPIPE can be controlled per socket. */
294 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
295 !(uap->flags & MSG_NOSIGNAL)) {
296 PROC_LOCK(td->td_proc);
297 tdsignal(td, SIGPIPE);
298 PROC_UNLOCK(td->td_proc);
302 td->td_retval[0] = len - auio.uio_resid;
304 if (ktruio != NULL) {
305 ktruio->uio_resid = td->td_retval[0];
306 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
321 sys_sctp_generic_sendmsg_iov(td, uap)
323 struct sctp_generic_sendmsg_iov_args /* {
329 struct sctp_sndrcvinfo *sinfo,
333 #if (defined(INET) || defined(INET6)) && defined(SCTP)
334 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
336 struct file *fp = NULL;
337 struct sockaddr *to = NULL;
339 struct uio *ktruio = NULL;
342 struct iovec *iov, *tiov;
347 if (uap->sinfo != NULL) {
348 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
353 cap_rights_init(&rights, CAP_SEND);
354 if (uap->tolen != 0) {
355 error = getsockaddr(&to, uap->to, uap->tolen);
360 cap_rights_set(&rights, CAP_CONNECT);
363 AUDIT_ARG_FD(uap->sd);
364 error = getsock_cap(td, uap->sd, &rights, &fp, NULL);
368 #ifdef COMPAT_FREEBSD32
369 if (SV_CURPROC_FLAG(SV_ILP32))
370 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
371 uap->iovlen, &iov, EMSGSIZE);
374 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
378 if (to && (KTRPOINT(td, KTR_STRUCT)))
382 so = (struct socket *)fp->f_data;
383 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
388 error = mac_socket_check_send(td->td_ucred, so);
394 auio.uio_iovcnt = uap->iovlen;
395 auio.uio_segflg = UIO_USERSPACE;
396 auio.uio_rw = UIO_WRITE;
398 auio.uio_offset = 0; /* XXX */
401 for (i = 0; i <uap->iovlen; i++, tiov++) {
402 if ((auio.uio_resid += tiov->iov_len) < 0) {
408 if (KTRPOINT(td, KTR_GENIO))
409 ktruio = cloneuio(&auio);
411 len = auio.uio_resid;
412 CURVNET_SET(so->so_vnet);
413 error = sctp_lower_sosend(so, to, &auio,
414 (struct mbuf *)NULL, (struct mbuf *)NULL,
415 uap->flags, u_sinfo, td);
418 if (auio.uio_resid != len && (error == ERESTART ||
419 error == EINTR || error == EWOULDBLOCK))
421 /* Generation of SIGPIPE can be controlled per socket */
422 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
423 !(uap->flags & MSG_NOSIGNAL)) {
424 PROC_LOCK(td->td_proc);
425 tdsignal(td, SIGPIPE);
426 PROC_UNLOCK(td->td_proc);
430 td->td_retval[0] = len - auio.uio_resid;
432 if (ktruio != NULL) {
433 ktruio->uio_resid = td->td_retval[0];
434 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
451 sys_sctp_generic_recvmsg(td, uap)
453 struct sctp_generic_recvmsg_args /* {
457 struct sockaddr *from,
458 __socklen_t *fromlenaddr,
459 struct sctp_sndrcvinfo *sinfo,
463 #if (defined(INET) || defined(INET6)) && defined(SCTP)
464 uint8_t sockbufstore[256];
466 struct iovec *iov, *tiov;
467 struct sctp_sndrcvinfo sinfo;
469 struct file *fp = NULL;
470 struct sockaddr *fromsa;
473 struct uio *ktruio = NULL;
476 int error, fromlen, i, msg_flags;
478 AUDIT_ARG_FD(uap->sd);
479 error = getsock_cap(td, uap->sd, cap_rights_init(&rights, CAP_RECV),
483 #ifdef COMPAT_FREEBSD32
484 if (SV_CURPROC_FLAG(SV_ILP32))
485 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
486 uap->iovlen, &iov, EMSGSIZE);
489 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
494 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
499 error = mac_socket_check_receive(td->td_ucred, so);
504 if (uap->fromlenaddr != NULL) {
505 error = copyin(uap->fromlenaddr, &fromlen, sizeof (fromlen));
511 if (uap->msg_flags) {
512 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
519 auio.uio_iovcnt = uap->iovlen;
520 auio.uio_segflg = UIO_USERSPACE;
521 auio.uio_rw = UIO_READ;
523 auio.uio_offset = 0; /* XXX */
526 for (i = 0; i <uap->iovlen; i++, tiov++) {
527 if ((auio.uio_resid += tiov->iov_len) < 0) {
532 len = auio.uio_resid;
533 fromsa = (struct sockaddr *)sockbufstore;
536 if (KTRPOINT(td, KTR_GENIO))
537 ktruio = cloneuio(&auio);
539 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
540 CURVNET_SET(so->so_vnet);
541 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
542 fromsa, fromlen, &msg_flags,
543 (struct sctp_sndrcvinfo *)&sinfo, 1);
546 if (auio.uio_resid != len && (error == ERESTART ||
547 error == EINTR || error == EWOULDBLOCK))
551 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
554 if (ktruio != NULL) {
555 ktruio->uio_resid = len - auio.uio_resid;
556 ktrgenio(uap->sd, UIO_READ, ktruio, error);
561 td->td_retval[0] = len - auio.uio_resid;
563 if (fromlen && uap->from) {
565 if (len <= 0 || fromsa == 0)
568 len = MIN(len, fromsa->sa_len);
569 error = copyout(fromsa, uap->from, (size_t)len);
573 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
578 if (KTRPOINT(td, KTR_STRUCT))
581 if (uap->msg_flags) {
582 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));