xref: /freebsd/sys/netinet/sctp_syscalls.c (revision 642870485c089b57000fe538d3485e272b038d59)
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 = sonewconn(head, SS_ISCONNECTED);
156 	if (so == NULL) {
157 		error = ENOMEM;
158 		goto noconnection;
159 	}
160 	/*
161 	 * Before changing the flags on the socket, we have to bump the
162 	 * reference count.  Otherwise, if the protocol calls sofree(),
163 	 * the socket will be released due to a zero refcount.
164 	 */
165         SOCK_LOCK(so);
166         soref(so);                      /* file descriptor reference */
167         SOCK_UNLOCK(so);
168 
169 	ACCEPT_LOCK();
170 
171 	TAILQ_REMOVE(&head->so_comp, so, so_list);
172 	head->so_qlen--;
173 	so->so_state |= (head->so_state & SS_NBIO);
174 	so->so_state &= ~SS_NOFDREF;
175 	so->so_qstate &= ~SQ_COMP;
176 	so->so_head = NULL;
177 	ACCEPT_UNLOCK();
178 	finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
179 	error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
180 	if (error != 0)
181 		goto noconnection;
182 	if (head->so_sigio != NULL)
183 		fsetown(fgetown(&head->so_sigio), &so->so_sigio);
184 
185 noconnection:
186 	/*
187 	 * close the new descriptor, assuming someone hasn't ripped it
188 	 * out from under us.
189 	 */
190 	if (error != 0)
191 		fdclose(td, nfp, fd);
192 
193 	/*
194 	 * Release explicitly held references before returning.
195 	 */
196 	CURVNET_RESTORE();
197 done:
198 	if (nfp != NULL)
199 		fdrop(nfp, td);
200 	fdrop(headfp, td);
201 done2:
202 	return (error);
203 #else  /* SCTP */
204 	return (EOPNOTSUPP);
205 #endif /* SCTP */
206 }
207 
208 int
209 sys_sctp_generic_sendmsg (td, uap)
210 	struct thread *td;
211 	struct sctp_generic_sendmsg_args /* {
212 		int sd,
213 		caddr_t msg,
214 		int mlen,
215 		caddr_t to,
216 		__socklen_t tolen,
217 		struct sctp_sndrcvinfo *sinfo,
218 		int flags
219 	} */ *uap;
220 {
221 #if (defined(INET) || defined(INET6)) && defined(SCTP)
222 	struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
223 	struct socket *so;
224 	struct file *fp = NULL;
225 	struct sockaddr *to = NULL;
226 #ifdef KTRACE
227 	struct uio *ktruio = NULL;
228 #endif
229 	struct uio auio;
230 	struct iovec iov[1];
231 	cap_rights_t rights;
232 	int error = 0, len;
233 
234 	if (uap->sinfo != NULL) {
235 		error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
236 		if (error != 0)
237 			return (error);
238 		u_sinfo = &sinfo;
239 	}
240 
241 	cap_rights_init(&rights, CAP_SEND);
242 	if (uap->tolen != 0) {
243 		error = getsockaddr(&to, uap->to, uap->tolen);
244 		if (error != 0) {
245 			to = NULL;
246 			goto sctp_bad2;
247 		}
248 		cap_rights_set(&rights, CAP_CONNECT);
249 	}
250 
251 	AUDIT_ARG_FD(uap->sd);
252 	error = getsock_cap(td, uap->sd, &rights, &fp, NULL, NULL);
253 	if (error != 0)
254 		goto sctp_bad;
255 #ifdef KTRACE
256 	if (to && (KTRPOINT(td, KTR_STRUCT)))
257 		ktrsockaddr(to);
258 #endif
259 
260 	iov[0].iov_base = uap->msg;
261 	iov[0].iov_len = uap->mlen;
262 
263 	so = (struct socket *)fp->f_data;
264 	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
265 		error = EOPNOTSUPP;
266 		goto sctp_bad;
267 	}
268 #ifdef MAC
269 	error = mac_socket_check_send(td->td_ucred, so);
270 	if (error != 0)
271 		goto sctp_bad;
272 #endif /* MAC */
273 
274 	auio.uio_iov =  iov;
275 	auio.uio_iovcnt = 1;
276 	auio.uio_segflg = UIO_USERSPACE;
277 	auio.uio_rw = UIO_WRITE;
278 	auio.uio_td = td;
279 	auio.uio_offset = 0;			/* XXX */
280 	auio.uio_resid = 0;
281 #ifdef KTRACE
282 	if (KTRPOINT(td, KTR_GENIO))
283 		ktruio = cloneuio(&auio);
284 #endif /* KTRACE */
285 	len = auio.uio_resid = uap->mlen;
286 	CURVNET_SET(so->so_vnet);
287 	error = sctp_lower_sosend(so, to, &auio, (struct mbuf *)NULL,
288 	    (struct mbuf *)NULL, uap->flags, u_sinfo, td);
289 	CURVNET_RESTORE();
290 	if (error != 0) {
291 		if (auio.uio_resid != len && (error == ERESTART ||
292 		    error == EINTR || error == EWOULDBLOCK))
293 			error = 0;
294 		/* Generation of SIGPIPE can be controlled per socket. */
295 		if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
296 		    !(uap->flags & MSG_NOSIGNAL)) {
297 			PROC_LOCK(td->td_proc);
298 			tdsignal(td, SIGPIPE);
299 			PROC_UNLOCK(td->td_proc);
300 		}
301 	}
302 	if (error == 0)
303 		td->td_retval[0] = len - auio.uio_resid;
304 #ifdef KTRACE
305 	if (ktruio != NULL) {
306 		ktruio->uio_resid = td->td_retval[0];
307 		ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
308 	}
309 #endif /* KTRACE */
310 sctp_bad:
311 	if (fp != NULL)
312 		fdrop(fp, td);
313 sctp_bad2:
314 	free(to, M_SONAME);
315 	return (error);
316 #else  /* SCTP */
317 	return (EOPNOTSUPP);
318 #endif /* SCTP */
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 #if (defined(INET) || defined(INET6)) && defined(SCTP)
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(&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(&rights, CAP_CONNECT);
362 	}
363 
364 	AUDIT_ARG_FD(uap->sd);
365 	error = getsock_cap(td, uap->sd, &rights, &fp, NULL, NULL);
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 #else  /* SCTP */
447 	return (EOPNOTSUPP);
448 #endif /* SCTP */
449 }
450 
451 int
452 sys_sctp_generic_recvmsg(td, uap)
453 	struct thread *td;
454 	struct sctp_generic_recvmsg_args /* {
455 		int sd,
456 		struct iovec *iov,
457 		int iovlen,
458 		struct sockaddr *from,
459 		__socklen_t *fromlenaddr,
460 		struct sctp_sndrcvinfo *sinfo,
461 		int *msg_flags
462 	} */ *uap;
463 {
464 #if (defined(INET) || defined(INET6)) && defined(SCTP)
465 	uint8_t sockbufstore[256];
466 	struct uio auio;
467 	struct iovec *iov, *tiov;
468 	struct sctp_sndrcvinfo sinfo;
469 	struct socket *so;
470 	struct file *fp = NULL;
471 	struct sockaddr *fromsa;
472 	cap_rights_t rights;
473 #ifdef KTRACE
474 	struct uio *ktruio = NULL;
475 #endif
476 	ssize_t len;
477 	int error, fromlen, i, msg_flags;
478 
479 	AUDIT_ARG_FD(uap->sd);
480 	error = getsock_cap(td, uap->sd, cap_rights_init(&rights, CAP_RECV),
481 	    &fp, NULL, NULL);
482 	if (error != 0)
483 		return (error);
484 #ifdef COMPAT_FREEBSD32
485 	if (SV_CURPROC_FLAG(SV_ILP32))
486 		error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
487 		    uap->iovlen, &iov, EMSGSIZE);
488 	else
489 #endif
490 		error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
491 	if (error != 0)
492 		goto out1;
493 
494 	so = fp->f_data;
495 	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
496 		error = EOPNOTSUPP;
497 		goto out;
498 	}
499 #ifdef MAC
500 	error = mac_socket_check_receive(td->td_ucred, so);
501 	if (error != 0)
502 		goto out;
503 #endif /* MAC */
504 
505 	if (uap->fromlenaddr != NULL) {
506 		error = copyin(uap->fromlenaddr, &fromlen, sizeof (fromlen));
507 		if (error != 0)
508 			goto out;
509 	} else {
510 		fromlen = 0;
511 	}
512 	if (uap->msg_flags) {
513 		error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
514 		if (error != 0)
515 			goto out;
516 	} else {
517 		msg_flags = 0;
518 	}
519 	auio.uio_iov = iov;
520 	auio.uio_iovcnt = uap->iovlen;
521 	auio.uio_segflg = UIO_USERSPACE;
522 	auio.uio_rw = UIO_READ;
523 	auio.uio_td = td;
524 	auio.uio_offset = 0;			/* XXX */
525 	auio.uio_resid = 0;
526 	tiov = iov;
527 	for (i = 0; i <uap->iovlen; i++, tiov++) {
528 		if ((auio.uio_resid += tiov->iov_len) < 0) {
529 			error = EINVAL;
530 			goto out;
531 		}
532 	}
533 	len = auio.uio_resid;
534 	fromsa = (struct sockaddr *)sockbufstore;
535 
536 #ifdef KTRACE
537 	if (KTRPOINT(td, KTR_GENIO))
538 		ktruio = cloneuio(&auio);
539 #endif /* KTRACE */
540 	memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
541 	CURVNET_SET(so->so_vnet);
542 	error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
543 		    fromsa, fromlen, &msg_flags,
544 		    (struct sctp_sndrcvinfo *)&sinfo, 1);
545 	CURVNET_RESTORE();
546 	if (error != 0) {
547 		if (auio.uio_resid != len && (error == ERESTART ||
548 		    error == EINTR || error == EWOULDBLOCK))
549 			error = 0;
550 	} else {
551 		if (uap->sinfo)
552 			error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
553 	}
554 #ifdef KTRACE
555 	if (ktruio != NULL) {
556 		ktruio->uio_resid = len - auio.uio_resid;
557 		ktrgenio(uap->sd, UIO_READ, ktruio, error);
558 	}
559 #endif /* KTRACE */
560 	if (error != 0)
561 		goto out;
562 	td->td_retval[0] = len - auio.uio_resid;
563 
564 	if (fromlen && uap->from) {
565 		len = fromlen;
566 		if (len <= 0 || fromsa == NULL)
567 			len = 0;
568 		else {
569 			len = MIN(len, fromsa->sa_len);
570 			error = copyout(fromsa, uap->from, (size_t)len);
571 			if (error != 0)
572 				goto out;
573 		}
574 		error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
575 		if (error != 0)
576 			goto out;
577 	}
578 #ifdef KTRACE
579 	if (KTRPOINT(td, KTR_STRUCT))
580 		ktrsockaddr(fromsa);
581 #endif
582 	if (uap->msg_flags) {
583 		error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
584 		if (error != 0)
585 			goto out;
586 	}
587 out:
588 	free(iov, M_IOV);
589 out1:
590 	if (fp != NULL)
591 		fdrop(fp, td);
592 
593 	return (error);
594 #else  /* SCTP */
595 	return (EOPNOTSUPP);
596 #endif /* SCTP */
597 }
598