xref: /freebsd/sys/netinet/sctp_syscalls.c (revision 2b833162)
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(struct thread *td, struct sctp_peeloff_args *uap)
143 {
144 	struct file *headfp, *nfp = NULL;
145 	struct socket *head, *so;
146 	cap_rights_t rights;
147 	u_int fflag;
148 	int error, fd;
149 
150 	AUDIT_ARG_FD(uap->sd);
151 	error = getsock(td, uap->sd, cap_rights_init_one(&rights, CAP_PEELOFF),
152 	    &headfp);
153 	if (error != 0)
154 		goto done2;
155 	fflag = atomic_load_int(&headfp->f_flag);
156 	head = headfp->f_data;
157 	if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
158 		error = EOPNOTSUPP;
159 		goto done;
160 	}
161 	error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
162 	if (error != 0)
163 		goto done;
164 	/*
165 	 * At this point we know we do have a assoc to pull
166 	 * we proceed to get the fd setup. This may block
167 	 * but that is ok.
168 	 */
169 
170 	error = falloc(td, &nfp, &fd, 0);
171 	if (error != 0)
172 		goto done;
173 	td->td_retval[0] = fd;
174 
175 	CURVNET_SET(head->so_vnet);
176 	so = sopeeloff(head);
177 	if (so == NULL) {
178 		error = ENOMEM;
179 		goto noconnection;
180 	}
181 	finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
182 	error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
183 	if (error != 0)
184 		goto noconnection;
185 	if (head->so_sigio != NULL)
186 		fsetown(fgetown(&head->so_sigio), &so->so_sigio);
187 
188 noconnection:
189 	/*
190 	 * close the new descriptor, assuming someone hasn't ripped it
191 	 * out from under us.
192 	 */
193 	if (error != 0)
194 		fdclose(td, nfp, fd);
195 
196 	/*
197 	 * Release explicitly held references before returning.
198 	 */
199 	CURVNET_RESTORE();
200 done:
201 	if (nfp != NULL)
202 		fdrop(nfp, td);
203 	fdrop(headfp, td);
204 done2:
205 	return (error);
206 }
207 
208 int
209 sys_sctp_generic_sendmsg(struct thread *td, struct sctp_generic_sendmsg_args *uap)
210 {
211 	struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
212 	struct socket *so;
213 	struct file *fp = NULL;
214 	struct sockaddr *to = NULL;
215 #ifdef KTRACE
216 	struct uio *ktruio = NULL;
217 #endif
218 	struct uio auio;
219 	struct iovec iov[1];
220 	cap_rights_t rights;
221 	int error = 0, len;
222 
223 	if (uap->sinfo != NULL) {
224 		error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
225 		if (error != 0)
226 			return (error);
227 		u_sinfo = &sinfo;
228 	}
229 
230 	cap_rights_init_one(&rights, CAP_SEND);
231 	if (uap->tolen != 0) {
232 		error = getsockaddr(&to, uap->to, uap->tolen);
233 		if (error != 0) {
234 			to = NULL;
235 			goto sctp_bad2;
236 		}
237 		cap_rights_set_one(&rights, CAP_CONNECT);
238 	}
239 
240 	AUDIT_ARG_FD(uap->sd);
241 	error = getsock(td, uap->sd, &rights, &fp);
242 	if (error != 0)
243 		goto sctp_bad;
244 #ifdef KTRACE
245 	if (to && (KTRPOINT(td, KTR_STRUCT)))
246 		ktrsockaddr(to);
247 #endif
248 
249 	iov[0].iov_base = uap->msg;
250 	iov[0].iov_len = uap->mlen;
251 
252 	so = (struct socket *)fp->f_data;
253 	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
254 		error = EOPNOTSUPP;
255 		goto sctp_bad;
256 	}
257 #ifdef MAC
258 	error = mac_socket_check_send(td->td_ucred, so);
259 	if (error != 0)
260 		goto sctp_bad;
261 #endif /* MAC */
262 
263 	auio.uio_iov =  iov;
264 	auio.uio_iovcnt = 1;
265 	auio.uio_segflg = UIO_USERSPACE;
266 	auio.uio_rw = UIO_WRITE;
267 	auio.uio_td = td;
268 	auio.uio_offset = 0;			/* XXX */
269 	auio.uio_resid = 0;
270 #ifdef KTRACE
271 	if (KTRPOINT(td, KTR_GENIO))
272 		ktruio = cloneuio(&auio);
273 #endif /* KTRACE */
274 	len = auio.uio_resid = uap->mlen;
275 	CURVNET_SET(so->so_vnet);
276 	error = sctp_lower_sosend(so, to, &auio, (struct mbuf *)NULL,
277 	    (struct mbuf *)NULL, uap->flags, u_sinfo, td);
278 	CURVNET_RESTORE();
279 	if (error != 0) {
280 		if (auio.uio_resid != len && (error == ERESTART ||
281 		    error == EINTR || error == EWOULDBLOCK))
282 			error = 0;
283 		/* Generation of SIGPIPE can be controlled per socket. */
284 		if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
285 		    !(uap->flags & MSG_NOSIGNAL)) {
286 			PROC_LOCK(td->td_proc);
287 			tdsignal(td, SIGPIPE);
288 			PROC_UNLOCK(td->td_proc);
289 		}
290 	}
291 	if (error == 0)
292 		td->td_retval[0] = len - auio.uio_resid;
293 #ifdef KTRACE
294 	if (ktruio != NULL) {
295 		ktruio->uio_resid = td->td_retval[0];
296 		ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
297 	}
298 #endif /* KTRACE */
299 sctp_bad:
300 	if (fp != NULL)
301 		fdrop(fp, td);
302 sctp_bad2:
303 	free(to, M_SONAME);
304 	return (error);
305 }
306 
307 int
308 sys_sctp_generic_sendmsg_iov(struct thread *td, struct sctp_generic_sendmsg_iov_args *uap)
309 {
310 	struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
311 	struct socket *so;
312 	struct file *fp = NULL;
313 	struct sockaddr *to = NULL;
314 #ifdef KTRACE
315 	struct uio *ktruio = NULL;
316 #endif
317 	struct uio auio;
318 	struct iovec *iov, *tiov;
319 	cap_rights_t rights;
320 	ssize_t len;
321 	int error, i;
322 
323 	if (uap->sinfo != NULL) {
324 		error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
325 		if (error != 0)
326 			return (error);
327 		u_sinfo = &sinfo;
328 	}
329 	cap_rights_init_one(&rights, CAP_SEND);
330 	if (uap->tolen != 0) {
331 		error = getsockaddr(&to, uap->to, uap->tolen);
332 		if (error != 0) {
333 			to = NULL;
334 			goto sctp_bad2;
335 		}
336 		cap_rights_set_one(&rights, CAP_CONNECT);
337 	}
338 
339 	AUDIT_ARG_FD(uap->sd);
340 	error = getsock(td, uap->sd, &rights, &fp);
341 	if (error != 0)
342 		goto sctp_bad1;
343 
344 #ifdef COMPAT_FREEBSD32
345 	if (SV_CURPROC_FLAG(SV_ILP32))
346 		error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
347 		    uap->iovlen, &iov, EMSGSIZE);
348 	else
349 #endif
350 		error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
351 	if (error != 0)
352 		goto sctp_bad1;
353 #ifdef KTRACE
354 	if (to && (KTRPOINT(td, KTR_STRUCT)))
355 		ktrsockaddr(to);
356 #endif
357 
358 	so = (struct socket *)fp->f_data;
359 	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
360 		error = EOPNOTSUPP;
361 		goto sctp_bad;
362 	}
363 #ifdef MAC
364 	error = mac_socket_check_send(td->td_ucred, so);
365 	if (error != 0)
366 		goto sctp_bad;
367 #endif /* MAC */
368 
369 	auio.uio_iov = iov;
370 	auio.uio_iovcnt = uap->iovlen;
371 	auio.uio_segflg = UIO_USERSPACE;
372 	auio.uio_rw = UIO_WRITE;
373 	auio.uio_td = td;
374 	auio.uio_offset = 0;			/* XXX */
375 	auio.uio_resid = 0;
376 	tiov = iov;
377 	for (i = 0; i <uap->iovlen; i++, tiov++) {
378 		if ((auio.uio_resid += tiov->iov_len) < 0) {
379 			error = EINVAL;
380 			goto sctp_bad;
381 		}
382 	}
383 #ifdef KTRACE
384 	if (KTRPOINT(td, KTR_GENIO))
385 		ktruio = cloneuio(&auio);
386 #endif /* KTRACE */
387 	len = auio.uio_resid;
388 	CURVNET_SET(so->so_vnet);
389 	error = sctp_lower_sosend(so, to, &auio,
390 		    (struct mbuf *)NULL, (struct mbuf *)NULL,
391 		    uap->flags, u_sinfo, td);
392 	CURVNET_RESTORE();
393 	if (error != 0) {
394 		if (auio.uio_resid != len && (error == ERESTART ||
395 		    error == EINTR || error == EWOULDBLOCK))
396 			error = 0;
397 		/* Generation of SIGPIPE can be controlled per socket */
398 		if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
399 		    !(uap->flags & MSG_NOSIGNAL)) {
400 			PROC_LOCK(td->td_proc);
401 			tdsignal(td, SIGPIPE);
402 			PROC_UNLOCK(td->td_proc);
403 		}
404 	}
405 	if (error == 0)
406 		td->td_retval[0] = len - auio.uio_resid;
407 #ifdef KTRACE
408 	if (ktruio != NULL) {
409 		ktruio->uio_resid = td->td_retval[0];
410 		ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
411 	}
412 #endif /* KTRACE */
413 sctp_bad:
414 	free(iov, M_IOV);
415 sctp_bad1:
416 	if (fp != NULL)
417 		fdrop(fp, td);
418 sctp_bad2:
419 	free(to, M_SONAME);
420 	return (error);
421 }
422 
423 int
424 sys_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 	}
548 out:
549 	free(iov, M_IOV);
550 out1:
551 	if (fp != NULL)
552 		fdrop(fp, td);
553 
554 	return (error);
555 }
556