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