xref: /freebsd/sys/netinet/sctp_syscalls.c (revision 38a52bd3)
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(td, uap->sd, cap_rights_init_one(&rights, CAP_PEELOFF),
157 	    &headfp);
158 	if (error != 0)
159 		goto done2;
160 	fflag = atomic_load_int(&headfp->f_flag);
161 	head = headfp->f_data;
162 	if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
163 		error = EOPNOTSUPP;
164 		goto done;
165 	}
166 	error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
167 	if (error != 0)
168 		goto done;
169 	/*
170 	 * At this point we know we do have a assoc to pull
171 	 * we proceed to get the fd setup. This may block
172 	 * but that is ok.
173 	 */
174 
175 	error = falloc(td, &nfp, &fd, 0);
176 	if (error != 0)
177 		goto done;
178 	td->td_retval[0] = fd;
179 
180 	CURVNET_SET(head->so_vnet);
181 	so = sopeeloff(head);
182 	if (so == NULL) {
183 		error = ENOMEM;
184 		goto noconnection;
185 	}
186 	finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
187 	error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
188 	if (error != 0)
189 		goto noconnection;
190 	if (head->so_sigio != NULL)
191 		fsetown(fgetown(&head->so_sigio), &so->so_sigio);
192 
193 noconnection:
194 	/*
195 	 * close the new descriptor, assuming someone hasn't ripped it
196 	 * out from under us.
197 	 */
198 	if (error != 0)
199 		fdclose(td, nfp, fd);
200 
201 	/*
202 	 * Release explicitly held references before returning.
203 	 */
204 	CURVNET_RESTORE();
205 done:
206 	if (nfp != NULL)
207 		fdrop(nfp, td);
208 	fdrop(headfp, td);
209 done2:
210 	return (error);
211 }
212 
213 int
214 sys_sctp_generic_sendmsg (td, uap)
215 	struct thread *td;
216 	struct sctp_generic_sendmsg_args /* {
217 		int sd,
218 		caddr_t msg,
219 		int mlen,
220 		caddr_t to,
221 		__socklen_t tolen,
222 		struct sctp_sndrcvinfo *sinfo,
223 		int flags
224 	} */ *uap;
225 {
226 	struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
227 	struct socket *so;
228 	struct file *fp = NULL;
229 	struct sockaddr *to = NULL;
230 #ifdef KTRACE
231 	struct uio *ktruio = NULL;
232 #endif
233 	struct uio auio;
234 	struct iovec iov[1];
235 	cap_rights_t rights;
236 	int error = 0, len;
237 
238 	if (uap->sinfo != NULL) {
239 		error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
240 		if (error != 0)
241 			return (error);
242 		u_sinfo = &sinfo;
243 	}
244 
245 	cap_rights_init_one(&rights, CAP_SEND);
246 	if (uap->tolen != 0) {
247 		error = getsockaddr(&to, uap->to, uap->tolen);
248 		if (error != 0) {
249 			to = NULL;
250 			goto sctp_bad2;
251 		}
252 		cap_rights_set_one(&rights, CAP_CONNECT);
253 	}
254 
255 	AUDIT_ARG_FD(uap->sd);
256 	error = getsock(td, uap->sd, &rights, &fp);
257 	if (error != 0)
258 		goto sctp_bad;
259 #ifdef KTRACE
260 	if (to && (KTRPOINT(td, KTR_STRUCT)))
261 		ktrsockaddr(to);
262 #endif
263 
264 	iov[0].iov_base = uap->msg;
265 	iov[0].iov_len = uap->mlen;
266 
267 	so = (struct socket *)fp->f_data;
268 	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
269 		error = EOPNOTSUPP;
270 		goto sctp_bad;
271 	}
272 #ifdef MAC
273 	error = mac_socket_check_send(td->td_ucred, so);
274 	if (error != 0)
275 		goto sctp_bad;
276 #endif /* MAC */
277 
278 	auio.uio_iov =  iov;
279 	auio.uio_iovcnt = 1;
280 	auio.uio_segflg = UIO_USERSPACE;
281 	auio.uio_rw = UIO_WRITE;
282 	auio.uio_td = td;
283 	auio.uio_offset = 0;			/* XXX */
284 	auio.uio_resid = 0;
285 #ifdef KTRACE
286 	if (KTRPOINT(td, KTR_GENIO))
287 		ktruio = cloneuio(&auio);
288 #endif /* KTRACE */
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);
293 	CURVNET_RESTORE();
294 	if (error != 0) {
295 		if (auio.uio_resid != len && (error == ERESTART ||
296 		    error == EINTR || error == EWOULDBLOCK))
297 			error = 0;
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);
304 		}
305 	}
306 	if (error == 0)
307 		td->td_retval[0] = len - auio.uio_resid;
308 #ifdef KTRACE
309 	if (ktruio != NULL) {
310 		ktruio->uio_resid = td->td_retval[0];
311 		ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
312 	}
313 #endif /* KTRACE */
314 sctp_bad:
315 	if (fp != NULL)
316 		fdrop(fp, td);
317 sctp_bad2:
318 	free(to, M_SONAME);
319 	return (error);
320 }
321 
322 int
323 sys_sctp_generic_sendmsg_iov(td, uap)
324 	struct thread *td;
325 	struct sctp_generic_sendmsg_iov_args /* {
326 		int sd,
327 		struct iovec *iov,
328 		int iovlen,
329 		caddr_t to,
330 		__socklen_t tolen,
331 		struct sctp_sndrcvinfo *sinfo,
332 		int flags
333 	} */ *uap;
334 {
335 	struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
336 	struct socket *so;
337 	struct file *fp = NULL;
338 	struct sockaddr *to = NULL;
339 #ifdef KTRACE
340 	struct uio *ktruio = NULL;
341 #endif
342 	struct uio auio;
343 	struct iovec *iov, *tiov;
344 	cap_rights_t rights;
345 	ssize_t len;
346 	int error, i;
347 
348 	if (uap->sinfo != NULL) {
349 		error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
350 		if (error != 0)
351 			return (error);
352 		u_sinfo = &sinfo;
353 	}
354 	cap_rights_init_one(&rights, CAP_SEND);
355 	if (uap->tolen != 0) {
356 		error = getsockaddr(&to, uap->to, uap->tolen);
357 		if (error != 0) {
358 			to = NULL;
359 			goto sctp_bad2;
360 		}
361 		cap_rights_set_one(&rights, CAP_CONNECT);
362 	}
363 
364 	AUDIT_ARG_FD(uap->sd);
365 	error = getsock(td, uap->sd, &rights, &fp);
366 	if (error != 0)
367 		goto sctp_bad1;
368 
369 #ifdef COMPAT_FREEBSD32
370 	if (SV_CURPROC_FLAG(SV_ILP32))
371 		error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
372 		    uap->iovlen, &iov, EMSGSIZE);
373 	else
374 #endif
375 		error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
376 	if (error != 0)
377 		goto sctp_bad1;
378 #ifdef KTRACE
379 	if (to && (KTRPOINT(td, KTR_STRUCT)))
380 		ktrsockaddr(to);
381 #endif
382 
383 	so = (struct socket *)fp->f_data;
384 	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
385 		error = EOPNOTSUPP;
386 		goto sctp_bad;
387 	}
388 #ifdef MAC
389 	error = mac_socket_check_send(td->td_ucred, so);
390 	if (error != 0)
391 		goto sctp_bad;
392 #endif /* MAC */
393 
394 	auio.uio_iov = iov;
395 	auio.uio_iovcnt = uap->iovlen;
396 	auio.uio_segflg = UIO_USERSPACE;
397 	auio.uio_rw = UIO_WRITE;
398 	auio.uio_td = td;
399 	auio.uio_offset = 0;			/* XXX */
400 	auio.uio_resid = 0;
401 	tiov = iov;
402 	for (i = 0; i <uap->iovlen; i++, tiov++) {
403 		if ((auio.uio_resid += tiov->iov_len) < 0) {
404 			error = EINVAL;
405 			goto sctp_bad;
406 		}
407 	}
408 #ifdef KTRACE
409 	if (KTRPOINT(td, KTR_GENIO))
410 		ktruio = cloneuio(&auio);
411 #endif /* KTRACE */
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);
417 	CURVNET_RESTORE();
418 	if (error != 0) {
419 		if (auio.uio_resid != len && (error == ERESTART ||
420 		    error == EINTR || error == EWOULDBLOCK))
421 			error = 0;
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);
428 		}
429 	}
430 	if (error == 0)
431 		td->td_retval[0] = len - auio.uio_resid;
432 #ifdef KTRACE
433 	if (ktruio != NULL) {
434 		ktruio->uio_resid = td->td_retval[0];
435 		ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
436 	}
437 #endif /* KTRACE */
438 sctp_bad:
439 	free(iov, M_IOV);
440 sctp_bad1:
441 	if (fp != NULL)
442 		fdrop(fp, td);
443 sctp_bad2:
444 	free(to, M_SONAME);
445 	return (error);
446 }
447 
448 int
449 sys_sctp_generic_recvmsg(td, uap)
450 	struct thread *td;
451 	struct sctp_generic_recvmsg_args /* {
452 		int sd,
453 		struct iovec *iov,
454 		int iovlen,
455 		struct sockaddr *from,
456 		__socklen_t *fromlenaddr,
457 		struct sctp_sndrcvinfo *sinfo,
458 		int *msg_flags
459 	} */ *uap;
460 {
461 	uint8_t sockbufstore[256];
462 	struct uio auio;
463 	struct iovec *iov, *tiov;
464 	struct sctp_sndrcvinfo sinfo;
465 	struct socket *so;
466 	struct file *fp = NULL;
467 	struct sockaddr *fromsa;
468 	cap_rights_t rights;
469 #ifdef KTRACE
470 	struct uio *ktruio = NULL;
471 #endif
472 	ssize_t len;
473 	int error, fromlen, i, msg_flags;
474 
475 	AUDIT_ARG_FD(uap->sd);
476 	error = getsock(td, uap->sd, cap_rights_init_one(&rights, CAP_RECV),
477 	    &fp);
478 	if (error != 0)
479 		return (error);
480 #ifdef COMPAT_FREEBSD32
481 	if (SV_CURPROC_FLAG(SV_ILP32))
482 		error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
483 		    uap->iovlen, &iov, EMSGSIZE);
484 	else
485 #endif
486 		error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
487 	if (error != 0)
488 		goto out1;
489 
490 	so = fp->f_data;
491 	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
492 		error = EOPNOTSUPP;
493 		goto out;
494 	}
495 #ifdef MAC
496 	error = mac_socket_check_receive(td->td_ucred, so);
497 	if (error != 0)
498 		goto out;
499 #endif /* MAC */
500 
501 	if (uap->fromlenaddr != NULL) {
502 		error = copyin(uap->fromlenaddr, &fromlen, sizeof (fromlen));
503 		if (error != 0)
504 			goto out;
505 	} else {
506 		fromlen = 0;
507 	}
508 	if (uap->msg_flags) {
509 		error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
510 		if (error != 0)
511 			goto out;
512 	} else {
513 		msg_flags = 0;
514 	}
515 	auio.uio_iov = iov;
516 	auio.uio_iovcnt = uap->iovlen;
517 	auio.uio_segflg = UIO_USERSPACE;
518 	auio.uio_rw = UIO_READ;
519 	auio.uio_td = td;
520 	auio.uio_offset = 0;			/* XXX */
521 	auio.uio_resid = 0;
522 	tiov = iov;
523 	for (i = 0; i <uap->iovlen; i++, tiov++) {
524 		if ((auio.uio_resid += tiov->iov_len) < 0) {
525 			error = EINVAL;
526 			goto out;
527 		}
528 	}
529 	len = auio.uio_resid;
530 	fromsa = (struct sockaddr *)sockbufstore;
531 
532 #ifdef KTRACE
533 	if (KTRPOINT(td, KTR_GENIO))
534 		ktruio = cloneuio(&auio);
535 #endif /* KTRACE */
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);
541 	CURVNET_RESTORE();
542 	if (error != 0) {
543 		if (auio.uio_resid != len && (error == ERESTART ||
544 		    error == EINTR || error == EWOULDBLOCK))
545 			error = 0;
546 	} else {
547 		if (uap->sinfo)
548 			error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
549 	}
550 #ifdef KTRACE
551 	if (ktruio != NULL) {
552 		ktruio->uio_resid = len - auio.uio_resid;
553 		ktrgenio(uap->sd, UIO_READ, ktruio, error);
554 	}
555 #endif /* KTRACE */
556 	if (error != 0)
557 		goto out;
558 	td->td_retval[0] = len - auio.uio_resid;
559 
560 	if (fromlen && uap->from) {
561 		len = fromlen;
562 		if (len <= 0 || fromsa == NULL)
563 			len = 0;
564 		else {
565 			len = MIN(len, fromsa->sa_len);
566 			error = copyout(fromsa, uap->from, (size_t)len);
567 			if (error != 0)
568 				goto out;
569 		}
570 		error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
571 		if (error != 0)
572 			goto out;
573 	}
574 #ifdef KTRACE
575 	if (KTRPOINT(td, KTR_STRUCT))
576 		ktrsockaddr(fromsa);
577 #endif
578 	if (uap->msg_flags) {
579 		error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
580 		if (error != 0)
581 			goto out;
582 	}
583 out:
584 	free(iov, M_IOV);
585 out1:
586 	if (fp != NULL)
587 		fdrop(fp, td);
588 
589 	return (error);
590 }
591