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_ktrace.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/capsicum.h>
41 #include <sys/kernel.h>
43 #include <sys/mutex.h>
44 #include <sys/sysproto.h>
45 #include <sys/malloc.h>
46 #include <sys/filedesc.h>
47 #include <sys/event.h>
49 #include <sys/fcntl.h>
51 #include <sys/filio.h>
53 #include <sys/mount.h>
55 #include <sys/protosw.h>
56 #include <sys/sf_buf.h>
57 #include <sys/sysent.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/signalvar.h>
61 #include <sys/syscall.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysctl.h>
65 #include <sys/vnode.h>
67 #include <sys/ktrace.h>
69 #ifdef COMPAT_FREEBSD32
70 #include <compat/freebsd32/freebsd32.h>
71 #include <compat/freebsd32/freebsd32_syscall.h>
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_os_bsd.h>
82 #include <netinet/sctp_peeloff.h>
84 static struct syscall_helper_data sctp_syscalls[] = {
85 SYSCALL_INIT_HELPER_F(sctp_peeloff, SYF_CAPENABLED),
86 SYSCALL_INIT_HELPER_F(sctp_generic_sendmsg, SYF_CAPENABLED),
87 SYSCALL_INIT_HELPER_F(sctp_generic_sendmsg_iov, SYF_CAPENABLED),
88 SYSCALL_INIT_HELPER_F(sctp_generic_recvmsg, SYF_CAPENABLED),
92 #ifdef COMPAT_FREEBSD32
93 static struct syscall_helper_data sctp32_syscalls[] = {
94 SYSCALL32_INIT_HELPER_COMPAT(sctp_peeloff),
95 SYSCALL32_INIT_HELPER_COMPAT(sctp_generic_sendmsg),
96 SYSCALL32_INIT_HELPER_COMPAT(sctp_generic_sendmsg_iov),
97 SYSCALL32_INIT_HELPER_COMPAT(sctp_generic_recvmsg),
103 sctp_syscalls_init(void)
107 error = syscall_helper_register(sctp_syscalls, SY_THR_STATIC_KLD);
110 #ifdef COMPAT_FREEBSD32
111 error = syscall32_helper_register(sctp32_syscalls, SY_THR_STATIC_KLD);
119 SYSINIT(sctp_syscalls, SI_SUB_SYSCALLS, SI_ORDER_ANY, sctp_syscalls_init, NULL);
123 sctp_syscalls_uninit(void)
127 #ifdef COMPAT_FREEBSD32
128 error = syscall32_helper_unregister(sctp32_syscalls);
132 error = syscall_helper_unregister(sctp_syscalls);
142 sys_sctp_peeloff(td, uap)
144 struct sctp_peeloff_args /* {
149 struct file *headfp, *nfp = NULL;
150 struct socket *head, *so;
155 AUDIT_ARG_FD(uap->sd);
156 error = getsock(td, uap->sd, cap_rights_init_one(&rights, CAP_PEELOFF),
160 fflag = atomic_load_int(&headfp->f_flag);
161 head = headfp->f_data;
162 if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
166 error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
170 * At this point we know we do have a assoc to pull
171 * we proceed to get the fd setup. This may block
175 error = falloc(td, &nfp, &fd, 0);
178 td->td_retval[0] = fd;
180 CURVNET_SET(head->so_vnet);
181 so = sopeeloff(head);
186 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
187 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
190 if (head->so_sigio != NULL)
191 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
195 * close the new descriptor, assuming someone hasn't ripped it
199 fdclose(td, nfp, fd);
202 * Release explicitly held references before returning.
214 sys_sctp_generic_sendmsg (td, uap)
216 struct sctp_generic_sendmsg_args /* {
222 struct sctp_sndrcvinfo *sinfo,
226 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
228 struct file *fp = NULL;
229 struct sockaddr *to = NULL;
231 struct uio *ktruio = NULL;
238 if (uap->sinfo != NULL) {
239 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
245 cap_rights_init_one(&rights, CAP_SEND);
246 if (uap->tolen != 0) {
247 error = getsockaddr(&to, uap->to, uap->tolen);
252 cap_rights_set_one(&rights, CAP_CONNECT);
255 AUDIT_ARG_FD(uap->sd);
256 error = getsock(td, uap->sd, &rights, &fp);
260 if (to && (KTRPOINT(td, KTR_STRUCT)))
264 iov[0].iov_base = uap->msg;
265 iov[0].iov_len = uap->mlen;
267 so = (struct socket *)fp->f_data;
268 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
273 error = mac_socket_check_send(td->td_ucred, so);
280 auio.uio_segflg = UIO_USERSPACE;
281 auio.uio_rw = UIO_WRITE;
283 auio.uio_offset = 0; /* XXX */
286 if (KTRPOINT(td, KTR_GENIO))
287 ktruio = cloneuio(&auio);
289 len = auio.uio_resid = uap->mlen;
290 CURVNET_SET(so->so_vnet);
291 error = sctp_lower_sosend(so, to, &auio, (struct mbuf *)NULL,
292 (struct mbuf *)NULL, uap->flags, u_sinfo, td);
295 if (auio.uio_resid != len && (error == ERESTART ||
296 error == EINTR || error == EWOULDBLOCK))
298 /* Generation of SIGPIPE can be controlled per socket. */
299 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
300 !(uap->flags & MSG_NOSIGNAL)) {
301 PROC_LOCK(td->td_proc);
302 tdsignal(td, SIGPIPE);
303 PROC_UNLOCK(td->td_proc);
307 td->td_retval[0] = len - auio.uio_resid;
309 if (ktruio != NULL) {
310 ktruio->uio_resid = td->td_retval[0];
311 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
323 sys_sctp_generic_sendmsg_iov(td, uap)
325 struct sctp_generic_sendmsg_iov_args /* {
331 struct sctp_sndrcvinfo *sinfo,
335 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
337 struct file *fp = NULL;
338 struct sockaddr *to = NULL;
340 struct uio *ktruio = NULL;
343 struct iovec *iov, *tiov;
348 if (uap->sinfo != NULL) {
349 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
354 cap_rights_init_one(&rights, CAP_SEND);
355 if (uap->tolen != 0) {
356 error = getsockaddr(&to, uap->to, uap->tolen);
361 cap_rights_set_one(&rights, CAP_CONNECT);
364 AUDIT_ARG_FD(uap->sd);
365 error = getsock(td, uap->sd, &rights, &fp);
369 #ifdef COMPAT_FREEBSD32
370 if (SV_CURPROC_FLAG(SV_ILP32))
371 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
372 uap->iovlen, &iov, EMSGSIZE);
375 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
379 if (to && (KTRPOINT(td, KTR_STRUCT)))
383 so = (struct socket *)fp->f_data;
384 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
389 error = mac_socket_check_send(td->td_ucred, so);
395 auio.uio_iovcnt = uap->iovlen;
396 auio.uio_segflg = UIO_USERSPACE;
397 auio.uio_rw = UIO_WRITE;
399 auio.uio_offset = 0; /* XXX */
402 for (i = 0; i <uap->iovlen; i++, tiov++) {
403 if ((auio.uio_resid += tiov->iov_len) < 0) {
409 if (KTRPOINT(td, KTR_GENIO))
410 ktruio = cloneuio(&auio);
412 len = auio.uio_resid;
413 CURVNET_SET(so->so_vnet);
414 error = sctp_lower_sosend(so, to, &auio,
415 (struct mbuf *)NULL, (struct mbuf *)NULL,
416 uap->flags, u_sinfo, td);
419 if (auio.uio_resid != len && (error == ERESTART ||
420 error == EINTR || error == EWOULDBLOCK))
422 /* Generation of SIGPIPE can be controlled per socket */
423 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
424 !(uap->flags & MSG_NOSIGNAL)) {
425 PROC_LOCK(td->td_proc);
426 tdsignal(td, SIGPIPE);
427 PROC_UNLOCK(td->td_proc);
431 td->td_retval[0] = len - auio.uio_resid;
433 if (ktruio != NULL) {
434 ktruio->uio_resid = td->td_retval[0];
435 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
449 sys_sctp_generic_recvmsg(td, uap)
451 struct sctp_generic_recvmsg_args /* {
455 struct sockaddr *from,
456 __socklen_t *fromlenaddr,
457 struct sctp_sndrcvinfo *sinfo,
461 uint8_t sockbufstore[256];
463 struct iovec *iov, *tiov;
464 struct sctp_sndrcvinfo sinfo;
466 struct file *fp = NULL;
467 struct sockaddr *fromsa;
470 struct uio *ktruio = NULL;
473 int error, fromlen, i, msg_flags;
475 AUDIT_ARG_FD(uap->sd);
476 error = getsock(td, uap->sd, cap_rights_init_one(&rights, CAP_RECV),
480 #ifdef COMPAT_FREEBSD32
481 if (SV_CURPROC_FLAG(SV_ILP32))
482 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
483 uap->iovlen, &iov, EMSGSIZE);
486 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
491 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
496 error = mac_socket_check_receive(td->td_ucred, so);
501 if (uap->fromlenaddr != NULL) {
502 error = copyin(uap->fromlenaddr, &fromlen, sizeof (fromlen));
508 if (uap->msg_flags) {
509 error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
516 auio.uio_iovcnt = uap->iovlen;
517 auio.uio_segflg = UIO_USERSPACE;
518 auio.uio_rw = UIO_READ;
520 auio.uio_offset = 0; /* XXX */
523 for (i = 0; i <uap->iovlen; i++, tiov++) {
524 if ((auio.uio_resid += tiov->iov_len) < 0) {
529 len = auio.uio_resid;
530 fromsa = (struct sockaddr *)sockbufstore;
533 if (KTRPOINT(td, KTR_GENIO))
534 ktruio = cloneuio(&auio);
536 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
537 CURVNET_SET(so->so_vnet);
538 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
539 fromsa, fromlen, &msg_flags,
540 (struct sctp_sndrcvinfo *)&sinfo, 1);
543 if (auio.uio_resid != len && (error == ERESTART ||
544 error == EINTR || error == EWOULDBLOCK))
548 error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
551 if (ktruio != NULL) {
552 ktruio->uio_resid = len - auio.uio_resid;
553 ktrgenio(uap->sd, UIO_READ, ktruio, error);
558 td->td_retval[0] = len - auio.uio_resid;
560 if (fromlen && uap->from) {
562 if (len <= 0 || fromsa == NULL)
565 len = MIN(len, fromsa->sa_len);
566 error = copyout(fromsa, uap->from, (size_t)len);
570 error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
575 if (KTRPOINT(td, KTR_STRUCT))
578 if (uap->msg_flags) {
579 error = copyout(&msg_flags, uap->msg_flags, sizeof (int));