1/*-
2 * Copyright (c) 1982, 1986, 1989, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 *
29 */
30
31#include <sys/cdefs.h>
32#include "opt_capsicum.h"
33#include "opt_sctp.h"
34#include "opt_ktrace.h"
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/capsicum.h>
39#include <sys/kernel.h>
40#include <sys/lock.h>
41#include <sys/mutex.h>
42#include <sys/sysproto.h>
43#include <sys/malloc.h>
44#include <sys/filedesc.h>
45#include <sys/event.h>
46#include <sys/proc.h>
47#include <sys/fcntl.h>
48#include <sys/file.h>
49#include <sys/filio.h>
50#include <sys/jail.h>
51#include <sys/mount.h>
52#include <sys/mbuf.h>
53#include <sys/protosw.h>
54#include <sys/sf_buf.h>
55#include <sys/sysent.h>
56#include <sys/socket.h>
57#include <sys/socketvar.h>
58#include <sys/signalvar.h>
59#include <sys/syscall.h>
60#include <sys/syscallsubr.h>
61#include <sys/sysctl.h>
62#include <sys/uio.h>
63#include <sys/vnode.h>
64#ifdef KTRACE
65#include <sys/ktrace.h>
66#endif
67#ifdef COMPAT_FREEBSD32
68#include <compat/freebsd32/freebsd32.h>
69#include <compat/freebsd32/freebsd32_syscall.h>
70#include <compat/freebsd32/freebsd32_util.h>
71#endif
72
73#include <net/vnet.h>
74
75#include <security/audit/audit.h>
76#include <security/mac/mac_framework.h>
77
78#include <netinet/sctp.h>
79#include <netinet/sctp_os_bsd.h>
80#include <netinet/sctp_peeloff.h>
81
82static struct syscall_helper_data sctp_syscalls[] = {
83	SYSCALL_INIT_HELPER_F(sctp_peeloff, SYF_CAPENABLED),
84	SYSCALL_INIT_HELPER_F(sctp_generic_sendmsg, SYF_CAPENABLED),
85	SYSCALL_INIT_HELPER_F(sctp_generic_sendmsg_iov, SYF_CAPENABLED),
86	SYSCALL_INIT_HELPER_F(sctp_generic_recvmsg, SYF_CAPENABLED),
87	SYSCALL_INIT_LAST
88};
89
90#ifdef COMPAT_FREEBSD32
91static struct syscall_helper_data sctp32_syscalls[] = {
92	SYSCALL32_INIT_HELPER_COMPAT(sctp_peeloff),
93	SYSCALL32_INIT_HELPER_COMPAT(sctp_generic_sendmsg),
94	SYSCALL32_INIT_HELPER_COMPAT(sctp_generic_sendmsg_iov),
95	SYSCALL32_INIT_HELPER_COMPAT(sctp_generic_recvmsg),
96	SYSCALL_INIT_LAST
97};
98#endif
99
100int
101sctp_syscalls_init(void)
102{
103	int error;
104
105	error = syscall_helper_register(sctp_syscalls, SY_THR_STATIC_KLD);
106	if (error != 0)
107		return (error);
108#ifdef COMPAT_FREEBSD32
109	error = syscall32_helper_register(sctp32_syscalls, SY_THR_STATIC_KLD);
110	if (error != 0)
111		return (error);
112#endif
113	return (0);
114}
115
116#ifdef SCTP
117SYSINIT(sctp_syscalls, SI_SUB_SYSCALLS, SI_ORDER_ANY, sctp_syscalls_init, NULL);
118#endif
119
120int
121sctp_syscalls_uninit(void)
122{
123	int error;
124
125#ifdef COMPAT_FREEBSD32
126	error = syscall32_helper_unregister(sctp32_syscalls);
127	if (error != 0)
128		return (error);
129#endif
130	error = syscall_helper_unregister(sctp_syscalls);
131	if (error != 0)
132		return (error);
133	return (0);
134}
135
136/*
137 * SCTP syscalls.
138 */
139int
140sys_sctp_peeloff(struct thread *td, struct sctp_peeloff_args *uap)
141{
142	struct file *headfp, *nfp = NULL;
143	struct socket *head, *so;
144	cap_rights_t rights;
145	u_int fflag;
146	int error, fd;
147
148	AUDIT_ARG_FD(uap->sd);
149	error = getsock(td, uap->sd, cap_rights_init_one(&rights, CAP_PEELOFF),
150	    &headfp);
151	if (error != 0)
152		goto done2;
153	fflag = atomic_load_int(&headfp->f_flag);
154	head = headfp->f_data;
155	if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
156		error = EOPNOTSUPP;
157		goto done;
158	}
159	error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
160	if (error != 0)
161		goto done;
162	/*
163	 * At this point we know we do have a assoc to pull
164	 * we proceed to get the fd setup. This may block
165	 * but that is ok.
166	 */
167
168	error = falloc(td, &nfp, &fd, 0);
169	if (error != 0)
170		goto done;
171	td->td_retval[0] = fd;
172
173	CURVNET_SET(head->so_vnet);
174	so = sopeeloff(head);
175	if (so == NULL) {
176		error = ENOMEM;
177		goto noconnection;
178	}
179	finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
180	error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
181	if (error != 0)
182		goto noconnection;
183	if (head->so_sigio != NULL)
184		fsetown(fgetown(&head->so_sigio), &so->so_sigio);
185
186noconnection:
187	/*
188	 * close the new descriptor, assuming someone hasn't ripped it
189	 * out from under us.
190	 */
191	if (error != 0)
192		fdclose(td, nfp, fd);
193
194	/*
195	 * Release explicitly held references before returning.
196	 */
197	CURVNET_RESTORE();
198done:
199	if (nfp != NULL)
200		fdrop(nfp, td);
201	fdrop(headfp, td);
202done2:
203	return (error);
204}
205
206int
207sys_sctp_generic_sendmsg(struct thread *td, struct sctp_generic_sendmsg_args *uap)
208{
209	struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
210	struct socket *so;
211	struct file *fp = NULL;
212	struct sockaddr *to = NULL;
213#ifdef KTRACE
214	struct uio *ktruio = NULL;
215#endif
216	struct uio auio;
217	struct iovec iov[1];
218	cap_rights_t rights;
219	int error = 0, len;
220
221	if (uap->sinfo != NULL) {
222		error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
223		if (error != 0)
224			return (error);
225		u_sinfo = &sinfo;
226	}
227
228	cap_rights_init_one(&rights, CAP_SEND);
229	if (uap->tolen != 0) {
230		error = getsockaddr(&to, uap->to, uap->tolen);
231		if (error != 0) {
232			to = NULL;
233			goto sctp_bad2;
234		}
235		cap_rights_set_one(&rights, CAP_CONNECT);
236	}
237
238	AUDIT_ARG_FD(uap->sd);
239	error = getsock(td, uap->sd, &rights, &fp);
240	if (error != 0)
241		goto sctp_bad;
242#ifdef KTRACE
243	if (to && (KTRPOINT(td, KTR_STRUCT)))
244		ktrsockaddr(to);
245#endif
246
247	iov[0].iov_base = uap->msg;
248	iov[0].iov_len = uap->mlen;
249
250	so = (struct socket *)fp->f_data;
251	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
252		error = EOPNOTSUPP;
253		goto sctp_bad;
254	}
255#ifdef MAC
256	error = mac_socket_check_send(td->td_ucred, so);
257	if (error != 0)
258		goto sctp_bad;
259#endif /* MAC */
260
261	auio.uio_iov =  iov;
262	auio.uio_iovcnt = 1;
263	auio.uio_segflg = UIO_USERSPACE;
264	auio.uio_rw = UIO_WRITE;
265	auio.uio_td = td;
266	auio.uio_offset = 0;			/* XXX */
267	auio.uio_resid = 0;
268#ifdef KTRACE
269	if (KTRPOINT(td, KTR_GENIO))
270		ktruio = cloneuio(&auio);
271#endif /* KTRACE */
272	len = auio.uio_resid = uap->mlen;
273	CURVNET_SET(so->so_vnet);
274	error = sctp_lower_sosend(so, to, &auio, (struct mbuf *)NULL,
275	    (struct mbuf *)NULL, uap->flags, u_sinfo, td);
276	CURVNET_RESTORE();
277	if (error != 0) {
278		if (auio.uio_resid != len && (error == ERESTART ||
279		    error == EINTR || error == EWOULDBLOCK))
280			error = 0;
281		/* Generation of SIGPIPE can be controlled per socket. */
282		if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
283		    !(uap->flags & MSG_NOSIGNAL)) {
284			PROC_LOCK(td->td_proc);
285			tdsignal(td, SIGPIPE);
286			PROC_UNLOCK(td->td_proc);
287		}
288	}
289	if (error == 0)
290		td->td_retval[0] = len - auio.uio_resid;
291#ifdef KTRACE
292	if (ktruio != NULL) {
293		if (error == 0)
294			ktruio->uio_resid = td->td_retval[0];
295		ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
296	}
297#endif /* KTRACE */
298sctp_bad:
299	if (fp != NULL)
300		fdrop(fp, td);
301sctp_bad2:
302	free(to, M_SONAME);
303	return (error);
304}
305
306int
307sys_sctp_generic_sendmsg_iov(struct thread *td, struct sctp_generic_sendmsg_iov_args *uap)
308{
309	struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
310	struct socket *so;
311	struct file *fp = NULL;
312	struct sockaddr *to = NULL;
313#ifdef KTRACE
314	struct uio *ktruio = NULL;
315#endif
316	struct uio auio;
317	struct iovec *iov, *tiov;
318	cap_rights_t rights;
319	ssize_t len;
320	int error, i;
321
322	if (uap->sinfo != NULL) {
323		error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
324		if (error != 0)
325			return (error);
326		u_sinfo = &sinfo;
327	}
328	cap_rights_init_one(&rights, CAP_SEND);
329	if (uap->tolen != 0) {
330		error = getsockaddr(&to, uap->to, uap->tolen);
331		if (error != 0) {
332			to = NULL;
333			goto sctp_bad2;
334		}
335		cap_rights_set_one(&rights, CAP_CONNECT);
336	}
337
338	AUDIT_ARG_FD(uap->sd);
339	error = getsock(td, uap->sd, &rights, &fp);
340	if (error != 0)
341		goto sctp_bad1;
342
343#ifdef COMPAT_FREEBSD32
344	if (SV_CURPROC_FLAG(SV_ILP32))
345		error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
346		    uap->iovlen, &iov, EMSGSIZE);
347	else
348#endif
349		error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
350	if (error != 0)
351		goto sctp_bad1;
352#ifdef KTRACE
353	if (to && (KTRPOINT(td, KTR_STRUCT)))
354		ktrsockaddr(to);
355#endif
356
357	so = (struct socket *)fp->f_data;
358	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
359		error = EOPNOTSUPP;
360		goto sctp_bad;
361	}
362#ifdef MAC
363	error = mac_socket_check_send(td->td_ucred, so);
364	if (error != 0)
365		goto sctp_bad;
366#endif /* MAC */
367
368	auio.uio_iov = iov;
369	auio.uio_iovcnt = uap->iovlen;
370	auio.uio_segflg = UIO_USERSPACE;
371	auio.uio_rw = UIO_WRITE;
372	auio.uio_td = td;
373	auio.uio_offset = 0;			/* XXX */
374	auio.uio_resid = 0;
375	tiov = iov;
376	for (i = 0; i <uap->iovlen; i++, tiov++) {
377		if ((auio.uio_resid += tiov->iov_len) < 0) {
378			error = EINVAL;
379			goto sctp_bad;
380		}
381	}
382#ifdef KTRACE
383	if (KTRPOINT(td, KTR_GENIO))
384		ktruio = cloneuio(&auio);
385#endif /* KTRACE */
386	len = auio.uio_resid;
387	CURVNET_SET(so->so_vnet);
388	error = sctp_lower_sosend(so, to, &auio,
389		    (struct mbuf *)NULL, (struct mbuf *)NULL,
390		    uap->flags, u_sinfo, td);
391	CURVNET_RESTORE();
392	if (error != 0) {
393		if (auio.uio_resid != len && (error == ERESTART ||
394		    error == EINTR || error == EWOULDBLOCK))
395			error = 0;
396		/* Generation of SIGPIPE can be controlled per socket */
397		if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
398		    !(uap->flags & MSG_NOSIGNAL)) {
399			PROC_LOCK(td->td_proc);
400			tdsignal(td, SIGPIPE);
401			PROC_UNLOCK(td->td_proc);
402		}
403	}
404	if (error == 0)
405		td->td_retval[0] = len - auio.uio_resid;
406#ifdef KTRACE
407	if (ktruio != NULL) {
408		if (error == 0)
409			ktruio->uio_resid = td->td_retval[0];
410		ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
411	}
412#endif /* KTRACE */
413sctp_bad:
414	free(iov, M_IOV);
415sctp_bad1:
416	if (fp != NULL)
417		fdrop(fp, td);
418sctp_bad2:
419	free(to, M_SONAME);
420	return (error);
421}
422
423int
424sys_sctp_generic_recvmsg(struct thread *td, struct sctp_generic_recvmsg_args *uap)
425{
426	uint8_t sockbufstore[256];
427	struct uio auio;
428	struct iovec *iov, *tiov;
429	struct sctp_sndrcvinfo sinfo;
430	struct socket *so;
431	struct file *fp = NULL;
432	struct sockaddr *fromsa;
433	cap_rights_t rights;
434#ifdef KTRACE
435	struct uio *ktruio = NULL;
436#endif
437	ssize_t len;
438	int error, fromlen, i, msg_flags;
439
440	AUDIT_ARG_FD(uap->sd);
441	error = getsock(td, uap->sd, cap_rights_init_one(&rights, CAP_RECV),
442	    &fp);
443	if (error != 0)
444		return (error);
445#ifdef COMPAT_FREEBSD32
446	if (SV_CURPROC_FLAG(SV_ILP32))
447		error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
448		    uap->iovlen, &iov, EMSGSIZE);
449	else
450#endif
451		error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
452	if (error != 0)
453		goto out1;
454
455	so = fp->f_data;
456	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
457		error = EOPNOTSUPP;
458		goto out;
459	}
460#ifdef MAC
461	error = mac_socket_check_receive(td->td_ucred, so);
462	if (error != 0)
463		goto out;
464#endif /* MAC */
465
466	if (uap->fromlenaddr != NULL) {
467		error = copyin(uap->fromlenaddr, &fromlen, sizeof (fromlen));
468		if (error != 0)
469			goto out;
470	} else {
471		fromlen = 0;
472	}
473	if (uap->msg_flags) {
474		error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
475		if (error != 0)
476			goto out;
477	} else {
478		msg_flags = 0;
479	}
480	auio.uio_iov = iov;
481	auio.uio_iovcnt = uap->iovlen;
482	auio.uio_segflg = UIO_USERSPACE;
483	auio.uio_rw = UIO_READ;
484	auio.uio_td = td;
485	auio.uio_offset = 0;			/* XXX */
486	auio.uio_resid = 0;
487	tiov = iov;
488	for (i = 0; i <uap->iovlen; i++, tiov++) {
489		if ((auio.uio_resid += tiov->iov_len) < 0) {
490			error = EINVAL;
491			goto out;
492		}
493	}
494	len = auio.uio_resid;
495	fromsa = (struct sockaddr *)sockbufstore;
496
497#ifdef KTRACE
498	if (KTRPOINT(td, KTR_GENIO))
499		ktruio = cloneuio(&auio);
500#endif /* KTRACE */
501	memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
502	CURVNET_SET(so->so_vnet);
503	error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
504		    fromsa, fromlen, &msg_flags,
505		    (struct sctp_sndrcvinfo *)&sinfo, 1);
506	CURVNET_RESTORE();
507	if (error != 0) {
508		if (auio.uio_resid != len && (error == ERESTART ||
509		    error == EINTR || error == EWOULDBLOCK))
510			error = 0;
511	} else {
512		if (uap->sinfo)
513			error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
514	}
515#ifdef KTRACE
516	if (ktruio != NULL) {
517		ktruio->uio_resid = len - auio.uio_resid;
518		ktrgenio(uap->sd, UIO_READ, ktruio, error);
519	}
520#endif /* KTRACE */
521	if (error != 0)
522		goto out;
523	td->td_retval[0] = len - auio.uio_resid;
524
525	if (fromlen && uap->from) {
526		len = fromlen;
527		if (len <= 0 || fromsa == NULL)
528			len = 0;
529		else {
530			len = MIN(len, fromsa->sa_len);
531			error = copyout(fromsa, uap->from, (size_t)len);
532			if (error != 0)
533				goto out;
534		}
535		error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
536		if (error != 0)
537			goto out;
538	}
539#ifdef KTRACE
540	if (KTRPOINT(td, KTR_STRUCT))
541		ktrsockaddr(fromsa);
542#endif
543	if (uap->msg_flags) {
544		error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
545		if (error != 0)
546			goto out;
547	}
548out:
549	free(iov, M_IOV);
550out1:
551	if (fp != NULL)
552		fdrop(fp, td);
553
554	return (error);
555}
556