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 "opt_capsicum.h"
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34 #include "opt_sctp.h"
35 #include "opt_ktrace.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/capsicum.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/sysproto.h>
44 #include <sys/malloc.h>
45 #include <sys/filedesc.h>
46 #include <sys/event.h>
47 #include <sys/proc.h>
48 #include <sys/fcntl.h>
49 #include <sys/file.h>
50 #include <sys/filio.h>
51 #include <sys/jail.h>
52 #include <sys/mount.h>
53 #include <sys/mbuf.h>
54 #include <sys/protosw.h>
55 #include <sys/sf_buf.h>
56 #include <sys/sysent.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/signalvar.h>
60 #include <sys/syscall.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/sysctl.h>
63 #include <sys/uio.h>
64 #include <sys/vnode.h>
65 #ifdef KTRACE
66 #include <sys/ktrace.h>
67 #endif
68 #ifdef COMPAT_FREEBSD32
69 #include <compat/freebsd32/freebsd32.h>
70 #include <compat/freebsd32/freebsd32_syscall.h>
71 #include <compat/freebsd32/freebsd32_util.h>
72 #endif
73
74 #include <net/vnet.h>
75
76 #include <security/audit/audit.h>
77 #include <security/mac/mac_framework.h>
78
79 #include <netinet/sctp.h>
80 #include <netinet/sctp_pcb.h>
81 #include <netinet/sctp_var.h>
82 #ifdef INET6
83 #include <netinet6/sctp6_var.h>
84 #endif
85 #include <netinet/sctp_os_bsd.h>
86 #include <netinet/sctp_peeloff.h>
87
88 static struct syscall_helper_data sctp_syscalls[] = {
89 SYSCALL_INIT_HELPER_F(sctp_peeloff, SYF_CAPENABLED),
90 SYSCALL_INIT_HELPER_F(sctp_generic_sendmsg, SYF_CAPENABLED),
91 SYSCALL_INIT_HELPER_F(sctp_generic_sendmsg_iov, SYF_CAPENABLED),
92 SYSCALL_INIT_HELPER_F(sctp_generic_recvmsg, SYF_CAPENABLED),
93 SYSCALL_INIT_LAST
94 };
95
96 #ifdef COMPAT_FREEBSD32
97 static struct syscall_helper_data sctp32_syscalls[] = {
98 SYSCALL32_INIT_HELPER_COMPAT(sctp_peeloff),
99 SYSCALL32_INIT_HELPER_COMPAT(sctp_generic_sendmsg),
100 SYSCALL32_INIT_HELPER_COMPAT(sctp_generic_sendmsg_iov),
101 SYSCALL32_INIT_HELPER_COMPAT(sctp_generic_recvmsg),
102 SYSCALL_INIT_LAST
103 };
104 #endif
105
106 int
sctp_syscalls_init(void)107 sctp_syscalls_init(void)
108 {
109 int error;
110
111 error = syscall_helper_register(sctp_syscalls, SY_THR_STATIC_KLD);
112 if (error != 0)
113 return (error);
114 #ifdef COMPAT_FREEBSD32
115 error = syscall32_helper_register(sctp32_syscalls, SY_THR_STATIC_KLD);
116 if (error != 0)
117 return (error);
118 #endif
119 return (0);
120 }
121
122 #ifdef SCTP
123 SYSINIT(sctp_syscalls, SI_SUB_SYSCALLS, SI_ORDER_ANY, sctp_syscalls_init, NULL);
124 #endif
125
126 int
sctp_syscalls_uninit(void)127 sctp_syscalls_uninit(void)
128 {
129 int error;
130
131 #ifdef COMPAT_FREEBSD32
132 error = syscall32_helper_unregister(sctp32_syscalls);
133 if (error != 0)
134 return (error);
135 #endif
136 error = syscall_helper_unregister(sctp_syscalls);
137 if (error != 0)
138 return (error);
139 return (0);
140 }
141
142 /*
143 * SCTP syscalls.
144 */
145 int
sys_sctp_peeloff(struct thread * td,struct sctp_peeloff_args * uap)146 sys_sctp_peeloff(struct thread *td, struct sctp_peeloff_args *uap)
147 {
148 struct file *headfp, *nfp = NULL;
149 struct socket *head, *so;
150 struct filecaps fcaps;
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,
157 cap_rights_init_one(&rights, CAP_PEELOFF), &headfp, &fcaps);
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_caps(td, &nfp, &fd, 0, &fcaps);
176 if (error != 0)
177 goto done;
178 td->td_retval[0] = fd;
179
180 CURVNET_SET(head->so_vnet);
181 switch (head->so_proto->pr_domain->dom_family) {
182 #ifdef INET
183 case AF_INET:
184 so = sopeeloff(head, &sctp_stream_protosw);
185 break;
186 #endif
187 #ifdef INET6
188 case AF_INET6:
189 so = sopeeloff(head, &sctp6_stream_protosw);
190 break;
191 #endif
192 default:
193 error = EOPNOTSUPP;
194 goto noconnection;
195 }
196 if (so == NULL) {
197 error = ENOMEM;
198 goto noconnection;
199 }
200 finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
201 error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
202 if (error != 0)
203 goto noconnection;
204 if (head->so_sigio != NULL)
205 fsetown(fgetown(&head->so_sigio), &so->so_sigio);
206
207 noconnection:
208 /*
209 * close the new descriptor, assuming someone hasn't ripped it
210 * out from under us.
211 */
212 if (error != 0)
213 fdclose(td, nfp, fd);
214
215 /*
216 * Release explicitly held references before returning.
217 */
218 CURVNET_RESTORE();
219 done:
220 if (nfp != NULL)
221 fdrop(nfp, td);
222 fdrop(headfp, td);
223 done2:
224 return (error);
225 }
226
227 int
sys_sctp_generic_sendmsg(struct thread * td,struct sctp_generic_sendmsg_args * uap)228 sys_sctp_generic_sendmsg(struct thread *td, struct sctp_generic_sendmsg_args *uap)
229 {
230 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
231 struct socket *so;
232 struct file *fp = NULL;
233 struct sockaddr *to = NULL;
234 #ifdef KTRACE
235 struct uio *ktruio = NULL;
236 #endif
237 struct uio auio;
238 struct iovec iov[1];
239 cap_rights_t rights;
240 int error = 0, len;
241
242 if (uap->sinfo != NULL) {
243 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
244 if (error != 0)
245 return (error);
246 u_sinfo = &sinfo;
247 }
248
249 cap_rights_init_one(&rights, CAP_SEND);
250 if (uap->tolen != 0) {
251 error = getsockaddr(&to, uap->to, uap->tolen);
252 if (error != 0) {
253 to = NULL;
254 goto sctp_bad2;
255 }
256 cap_rights_set_one(&rights, CAP_CONNECT);
257 }
258
259 AUDIT_ARG_FD(uap->sd);
260 error = getsock(td, uap->sd, &rights, &fp);
261 if (error != 0)
262 goto sctp_bad;
263 #ifdef KTRACE
264 if (to && (KTRPOINT(td, KTR_STRUCT)))
265 ktrsockaddr(to);
266 #endif
267
268 iov[0].iov_base = uap->msg;
269 iov[0].iov_len = uap->mlen;
270
271 so = (struct socket *)fp->f_data;
272 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
273 error = EOPNOTSUPP;
274 goto sctp_bad;
275 }
276 #ifdef MAC
277 error = mac_socket_check_send(td->td_ucred, so);
278 if (error != 0)
279 goto sctp_bad;
280 #endif /* MAC */
281
282 auio.uio_iov = iov;
283 auio.uio_iovcnt = 1;
284 auio.uio_segflg = UIO_USERSPACE;
285 auio.uio_rw = UIO_WRITE;
286 auio.uio_td = td;
287 auio.uio_offset = 0; /* XXX */
288 auio.uio_resid = 0;
289 #ifdef KTRACE
290 if (KTRPOINT(td, KTR_GENIO))
291 ktruio = cloneuio(&auio);
292 #endif /* KTRACE */
293 len = auio.uio_resid = uap->mlen;
294 CURVNET_SET(so->so_vnet);
295 error = sctp_lower_sosend(so, to, &auio, (struct mbuf *)NULL,
296 (struct mbuf *)NULL, uap->flags, u_sinfo, td);
297 CURVNET_RESTORE();
298 if (error != 0) {
299 if (auio.uio_resid != len && (error == ERESTART ||
300 error == EINTR || error == EWOULDBLOCK))
301 error = 0;
302 /* Generation of SIGPIPE can be controlled per socket. */
303 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
304 !(uap->flags & MSG_NOSIGNAL)) {
305 PROC_LOCK(td->td_proc);
306 tdsignal(td, SIGPIPE);
307 PROC_UNLOCK(td->td_proc);
308 }
309 }
310 if (error == 0)
311 td->td_retval[0] = len - auio.uio_resid;
312 #ifdef KTRACE
313 if (ktruio != NULL) {
314 if (error == 0)
315 ktruio->uio_resid = td->td_retval[0];
316 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
317 }
318 #endif /* KTRACE */
319 sctp_bad:
320 if (fp != NULL)
321 fdrop(fp, td);
322 sctp_bad2:
323 free(to, M_SONAME);
324 return (error);
325 }
326
327 int
sys_sctp_generic_sendmsg_iov(struct thread * td,struct sctp_generic_sendmsg_iov_args * uap)328 sys_sctp_generic_sendmsg_iov(struct thread *td, struct sctp_generic_sendmsg_iov_args *uap)
329 {
330 struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
331 struct socket *so;
332 struct file *fp = NULL;
333 struct sockaddr *to = NULL;
334 #ifdef KTRACE
335 struct uio *ktruio = NULL;
336 #endif
337 struct uio auio;
338 struct iovec *iov, *tiov;
339 cap_rights_t rights;
340 ssize_t len;
341 int error, i;
342
343 if (uap->sinfo != NULL) {
344 error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
345 if (error != 0)
346 return (error);
347 u_sinfo = &sinfo;
348 }
349 cap_rights_init_one(&rights, CAP_SEND);
350 if (uap->tolen != 0) {
351 error = getsockaddr(&to, uap->to, uap->tolen);
352 if (error != 0) {
353 to = NULL;
354 goto sctp_bad2;
355 }
356 cap_rights_set_one(&rights, CAP_CONNECT);
357 }
358
359 AUDIT_ARG_FD(uap->sd);
360 error = getsock(td, uap->sd, &rights, &fp);
361 if (error != 0)
362 goto sctp_bad1;
363
364 #ifdef COMPAT_FREEBSD32
365 if (SV_CURPROC_FLAG(SV_ILP32))
366 error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
367 uap->iovlen, &iov, EMSGSIZE);
368 else
369 #endif
370 error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
371 if (error != 0)
372 goto sctp_bad1;
373 #ifdef KTRACE
374 if (to && (KTRPOINT(td, KTR_STRUCT)))
375 ktrsockaddr(to);
376 #endif
377
378 so = (struct socket *)fp->f_data;
379 if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
380 error = EOPNOTSUPP;
381 goto sctp_bad;
382 }
383 #ifdef MAC
384 error = mac_socket_check_send(td->td_ucred, so);
385 if (error != 0)
386 goto sctp_bad;
387 #endif /* MAC */
388
389 auio.uio_iov = iov;
390 auio.uio_iovcnt = uap->iovlen;
391 auio.uio_segflg = UIO_USERSPACE;
392 auio.uio_rw = UIO_WRITE;
393 auio.uio_td = td;
394 auio.uio_offset = 0; /* XXX */
395 auio.uio_resid = 0;
396 tiov = iov;
397 for (i = 0; i <uap->iovlen; i++, tiov++) {
398 if ((auio.uio_resid += tiov->iov_len) < 0) {
399 error = EINVAL;
400 goto sctp_bad;
401 }
402 }
403 #ifdef KTRACE
404 if (KTRPOINT(td, KTR_GENIO))
405 ktruio = cloneuio(&auio);
406 #endif /* KTRACE */
407 len = auio.uio_resid;
408 CURVNET_SET(so->so_vnet);
409 error = sctp_lower_sosend(so, to, &auio,
410 (struct mbuf *)NULL, (struct mbuf *)NULL,
411 uap->flags, u_sinfo, td);
412 CURVNET_RESTORE();
413 if (error != 0) {
414 if (auio.uio_resid != len && (error == ERESTART ||
415 error == EINTR || error == EWOULDBLOCK))
416 error = 0;
417 /* Generation of SIGPIPE can be controlled per socket */
418 if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
419 !(uap->flags & MSG_NOSIGNAL)) {
420 PROC_LOCK(td->td_proc);
421 tdsignal(td, SIGPIPE);
422 PROC_UNLOCK(td->td_proc);
423 }
424 }
425 if (error == 0)
426 td->td_retval[0] = len - auio.uio_resid;
427 #ifdef KTRACE
428 if (ktruio != NULL) {
429 if (error == 0)
430 ktruio->uio_resid = td->td_retval[0];
431 ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
432 }
433 #endif /* KTRACE */
434 sctp_bad:
435 free(iov, M_IOV);
436 sctp_bad1:
437 if (fp != NULL)
438 fdrop(fp, td);
439 sctp_bad2:
440 free(to, M_SONAME);
441 return (error);
442 }
443
444 int
sys_sctp_generic_recvmsg(struct thread * td,struct sctp_generic_recvmsg_args * uap)445 sys_sctp_generic_recvmsg(struct thread *td, struct sctp_generic_recvmsg_args *uap)
446 {
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(td, uap->sd, cap_rights_init_one(&rights, CAP_RECV),
463 &fp);
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 }
577