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