1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2015 Joyent, Inc. All rights reserved.
25 * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
26 */
27
28 #include <sys/types.h>
29 #include <sys/t_lock.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/buf.h>
33 #include <sys/vfs.h>
34 #include <sys/vnode.h>
35 #include <sys/debug.h>
36 #include <sys/errno.h>
37 #include <sys/stropts.h>
38 #include <sys/cmn_err.h>
39 #include <sys/sysmacros.h>
40 #include <sys/filio.h>
41 #include <sys/policy.h>
42
43 #include <sys/project.h>
44 #include <sys/tihdr.h>
45 #include <sys/strsubr.h>
46 #include <sys/esunddi.h>
47 #include <sys/ddi.h>
48
49 #include <sys/sockio.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/strsun.h>
53
54 #include <netinet/sctp.h>
55 #include <inet/sctp_itf.h>
56 #include <fs/sockfs/sockcommon.h>
57 #include "socksctp.h"
58
59 /*
60 * SCTP sockfs sonode operations, 1-1 socket
61 */
62 static int sosctp_init(struct sonode *, struct sonode *, struct cred *, int);
63 static int sosctp_accept(struct sonode *, int, struct cred *, struct sonode **);
64 static int sosctp_bind(struct sonode *, struct sockaddr *, socklen_t, int,
65 struct cred *);
66 static int sosctp_listen(struct sonode *, int, struct cred *);
67 static int sosctp_connect(struct sonode *, struct sockaddr *, socklen_t,
68 int, int, struct cred *);
69 static int sosctp_recvmsg(struct sonode *, struct nmsghdr *, struct uio *,
70 struct cred *);
71 static int sosctp_sendmsg(struct sonode *, struct nmsghdr *, struct uio *,
72 struct cred *);
73 static int sosctp_getpeername(struct sonode *, struct sockaddr *, socklen_t *,
74 boolean_t, struct cred *);
75 static int sosctp_getsockname(struct sonode *, struct sockaddr *, socklen_t *,
76 struct cred *);
77 static int sosctp_shutdown(struct sonode *, int, struct cred *);
78 static int sosctp_getsockopt(struct sonode *, int, int, void *, socklen_t *,
79 int, struct cred *);
80 static int sosctp_setsockopt(struct sonode *, int, int, const void *,
81 socklen_t, struct cred *);
82 static int sosctp_ioctl(struct sonode *, int, intptr_t, int, struct cred *,
83 int32_t *);
84 static int sosctp_close(struct sonode *, int, struct cred *);
85 void sosctp_fini(struct sonode *, struct cred *);
86
87 /*
88 * SCTP sockfs sonode operations, 1-N socket
89 */
90 static int sosctp_seq_connect(struct sonode *, struct sockaddr *,
91 socklen_t, int, int, struct cred *);
92 static int sosctp_seq_sendmsg(struct sonode *, struct nmsghdr *, struct uio *,
93 struct cred *);
94
95 /*
96 * Socket association upcalls, 1-N socket connection
97 */
98 sock_upper_handle_t sctp_assoc_newconn(sock_upper_handle_t,
99 sock_lower_handle_t, sock_downcalls_t *, struct cred *, pid_t,
100 sock_upcalls_t **);
101 static void sctp_assoc_connected(sock_upper_handle_t, sock_connid_t,
102 struct cred *, pid_t);
103 static int sctp_assoc_disconnected(sock_upper_handle_t, sock_connid_t, int);
104 static void sctp_assoc_disconnecting(sock_upper_handle_t, sock_opctl_action_t,
105 uintptr_t arg);
106 static ssize_t sctp_assoc_recv(sock_upper_handle_t, mblk_t *, size_t, int,
107 int *, boolean_t *);
108 static void sctp_assoc_xmitted(sock_upper_handle_t, boolean_t);
109 static void sctp_assoc_properties(sock_upper_handle_t,
110 struct sock_proto_props *);
111 static vnode_t *sctp_assoc_get_vnode(sock_upper_handle_t);
112
113 sonodeops_t sosctp_sonodeops = {
114 sosctp_init, /* sop_init */
115 sosctp_accept, /* sop_accept */
116 sosctp_bind, /* sop_bind */
117 sosctp_listen, /* sop_listen */
118 sosctp_connect, /* sop_connect */
119 sosctp_recvmsg, /* sop_recvmsg */
120 sosctp_sendmsg, /* sop_sendmsg */
121 so_sendmblk_notsupp, /* sop_sendmblk */
122 sosctp_getpeername, /* sop_getpeername */
123 sosctp_getsockname, /* sop_getsockname */
124 sosctp_shutdown, /* sop_shutdown */
125 sosctp_getsockopt, /* sop_getsockopt */
126 sosctp_setsockopt, /* sop_setsockopt */
127 sosctp_ioctl, /* sop_ioctl */
128 so_poll, /* sop_poll */
129 sosctp_close, /* sop_close */
130 };
131
132 sonodeops_t sosctp_seq_sonodeops = {
133 sosctp_init, /* sop_init */
134 so_accept_notsupp, /* sop_accept */
135 sosctp_bind, /* sop_bind */
136 sosctp_listen, /* sop_listen */
137 sosctp_seq_connect, /* sop_connect */
138 sosctp_recvmsg, /* sop_recvmsg */
139 sosctp_seq_sendmsg, /* sop_sendmsg */
140 so_sendmblk_notsupp, /* sop_sendmblk */
141 so_getpeername_notsupp, /* sop_getpeername */
142 sosctp_getsockname, /* sop_getsockname */
143 so_shutdown_notsupp, /* sop_shutdown */
144 sosctp_getsockopt, /* sop_getsockopt */
145 sosctp_setsockopt, /* sop_setsockopt */
146 sosctp_ioctl, /* sop_ioctl */
147 so_poll, /* sop_poll */
148 sosctp_close, /* sop_close */
149 };
150
151 /* All the upcalls expect the upper handle to be sonode. */
152 sock_upcalls_t sosctp_sock_upcalls = {
153 so_newconn,
154 so_connected,
155 so_disconnected,
156 so_opctl,
157 so_queue_msg,
158 so_set_prop,
159 so_txq_full,
160 NULL, /* su_signal_oob */
161 NULL, /* su_signal_oob */
162 NULL, /* su_set_error */
163 NULL, /* su_closed */
164 so_get_vnode /* su_get_vnode */
165 };
166
167 /* All the upcalls expect the upper handle to be sctp_sonode/sctp_soassoc. */
168 sock_upcalls_t sosctp_assoc_upcalls = {
169 sctp_assoc_newconn,
170 sctp_assoc_connected,
171 sctp_assoc_disconnected,
172 sctp_assoc_disconnecting,
173 sctp_assoc_recv,
174 sctp_assoc_properties,
175 sctp_assoc_xmitted,
176 NULL, /* su_recv_space */
177 NULL, /* su_signal_oob */
178 NULL, /* su_set_error */
179 NULL, /* su_closed */
180 sctp_assoc_get_vnode
181 };
182
183 /* ARGSUSED */
184 static int
sosctp_init(struct sonode * so,struct sonode * pso,struct cred * cr,int flags)185 sosctp_init(struct sonode *so, struct sonode *pso, struct cred *cr, int flags)
186 {
187 struct sctp_sonode *ss;
188 struct sctp_sonode *pss;
189 sctp_sockbuf_limits_t sbl;
190 int err;
191
192 ss = SOTOSSO(so);
193
194 if (pso != NULL) {
195 /*
196 * Passive open, just inherit settings from parent. We should
197 * not end up here for SOCK_SEQPACKET type sockets, since no
198 * new sonode is created in that case.
199 */
200 ASSERT(so->so_type == SOCK_STREAM);
201 pss = SOTOSSO(pso);
202
203 mutex_enter(&pso->so_lock);
204 so->so_state |= (SS_ISBOUND | SS_ISCONNECTED |
205 (pso->so_state & SS_ASYNC));
206 sosctp_so_inherit(pss, ss);
207 so->so_proto_props = pso->so_proto_props;
208 so->so_mode = pso->so_mode;
209 mutex_exit(&pso->so_lock);
210
211 return (0);
212 }
213
214 if ((err = secpolicy_basic_net_access(cr)) != 0)
215 return (err);
216
217 if (so->so_type == SOCK_STREAM) {
218 so->so_proto_handle = (sock_lower_handle_t)sctp_create(so,
219 NULL, so->so_family, so->so_type, SCTP_CAN_BLOCK,
220 &sosctp_sock_upcalls, &sbl, cr);
221 so->so_mode = SM_CONNREQUIRED;
222 } else {
223 ASSERT(so->so_type == SOCK_SEQPACKET);
224 so->so_proto_handle = (sock_lower_handle_t)sctp_create(ss,
225 NULL, so->so_family, so->so_type, SCTP_CAN_BLOCK,
226 &sosctp_assoc_upcalls, &sbl, cr);
227 }
228
229 if (so->so_proto_handle == NULL)
230 return (ENOMEM);
231
232 so->so_rcvbuf = sbl.sbl_rxbuf;
233 so->so_rcvlowat = sbl.sbl_rxlowat;
234 so->so_sndbuf = sbl.sbl_txbuf;
235 so->so_sndlowat = sbl.sbl_txlowat;
236
237 return (0);
238 }
239
240 /*
241 * Accept incoming connection.
242 */
243 /*ARGSUSED*/
244 static int
sosctp_accept(struct sonode * so,int fflag,struct cred * cr,struct sonode ** nsop)245 sosctp_accept(struct sonode *so, int fflag, struct cred *cr,
246 struct sonode **nsop)
247 {
248 int error = 0;
249
250 if ((so->so_state & SS_ACCEPTCONN) == 0)
251 return (EINVAL);
252
253 error = so_acceptq_dequeue(so, (fflag & (FNONBLOCK|FNDELAY)), nsop);
254
255 return (error);
256 }
257
258 /*
259 * Bind local endpoint.
260 */
261 /*ARGSUSED*/
262 static int
sosctp_bind(struct sonode * so,struct sockaddr * name,socklen_t namelen,int flags,struct cred * cr)263 sosctp_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
264 int flags, struct cred *cr)
265 {
266 int error;
267
268 if (!(flags & _SOBIND_LOCK_HELD)) {
269 mutex_enter(&so->so_lock);
270 so_lock_single(so); /* Set SOLOCKED */
271 } else {
272 ASSERT(MUTEX_HELD(&so->so_lock));
273 }
274
275 /*
276 * X/Open requires this check
277 */
278 if (so->so_state & SS_CANTSENDMORE) {
279 error = EINVAL;
280 goto done;
281 }
282
283
284 /*
285 * Protocol module does address family checks.
286 */
287 mutex_exit(&so->so_lock);
288
289 error = sctp_bind((struct sctp_s *)so->so_proto_handle, name, namelen);
290
291 mutex_enter(&so->so_lock);
292 if (error == 0) {
293 so->so_state |= SS_ISBOUND;
294 } else {
295 eprintsoline(so, error);
296 }
297 done:
298 if (!(flags & _SOBIND_LOCK_HELD)) {
299 so_unlock_single(so, SOLOCKED);
300 mutex_exit(&so->so_lock);
301 } else {
302 /* If the caller held the lock don't release it here */
303 ASSERT(MUTEX_HELD(&so->so_lock));
304 ASSERT(so->so_flag & SOLOCKED);
305 }
306
307 return (error);
308 }
309
310 /*
311 * Turn socket into a listen socket.
312 */
313 /* ARGSUSED */
314 static int
sosctp_listen(struct sonode * so,int backlog,struct cred * cr)315 sosctp_listen(struct sonode *so, int backlog, struct cred *cr)
316 {
317 int error = 0;
318
319 mutex_enter(&so->so_lock);
320 so_lock_single(so);
321
322 /*
323 * If this socket is trying to do connect, or if it has
324 * been connected, disallow.
325 */
326 if (so->so_state & (SS_ISCONNECTING | SS_ISCONNECTED |
327 SS_ISDISCONNECTING | SS_CANTRCVMORE | SS_CANTSENDMORE)) {
328 error = EINVAL;
329 eprintsoline(so, error);
330 goto done;
331 }
332
333 if (backlog < 0) {
334 backlog = 0;
335 }
336
337 /*
338 * If listen() is only called to change backlog, we don't
339 * need to notify protocol module.
340 */
341 if (so->so_state & SS_ACCEPTCONN) {
342 so->so_backlog = backlog;
343 goto done;
344 }
345
346 mutex_exit(&so->so_lock);
347 error = sctp_listen((struct sctp_s *)so->so_proto_handle);
348 mutex_enter(&so->so_lock);
349 if (error == 0) {
350 so->so_state |= (SS_ACCEPTCONN|SS_ISBOUND);
351 so->so_backlog = backlog;
352 } else {
353 eprintsoline(so, error);
354 }
355 done:
356 so_unlock_single(so, SOLOCKED);
357 mutex_exit(&so->so_lock);
358
359 return (error);
360 }
361
362 /*
363 * Active open.
364 */
365 /*ARGSUSED*/
366 static int
sosctp_connect(struct sonode * so,struct sockaddr * name,socklen_t namelen,int fflag,int flags,struct cred * cr)367 sosctp_connect(struct sonode *so, struct sockaddr *name,
368 socklen_t namelen, int fflag, int flags, struct cred *cr)
369 {
370 int error = 0;
371 pid_t pid = curproc->p_pid;
372
373 ASSERT(so->so_type == SOCK_STREAM);
374
375 mutex_enter(&so->so_lock);
376 so_lock_single(so);
377
378 /*
379 * Can't connect() after listen(), or if the socket is already
380 * connected.
381 */
382 if (so->so_state & (SS_ACCEPTCONN|SS_ISCONNECTED|SS_ISCONNECTING)) {
383 if (so->so_state & SS_ISCONNECTED) {
384 error = EISCONN;
385 } else if (so->so_state & SS_ISCONNECTING) {
386 error = EALREADY;
387 } else {
388 error = EOPNOTSUPP;
389 }
390 eprintsoline(so, error);
391 goto done;
392 }
393
394 /*
395 * Check for failure of an earlier call
396 */
397 if (so->so_error != 0) {
398 error = sogeterr(so, B_TRUE);
399 eprintsoline(so, error);
400 goto done;
401 }
402
403 /*
404 * Connection is closing, or closed, don't allow reconnect.
405 * TCP allows this to proceed, but the socket remains unwriteable.
406 * BSD returns EINVAL.
407 */
408 if (so->so_state & (SS_ISDISCONNECTING|SS_CANTRCVMORE|
409 SS_CANTSENDMORE)) {
410 error = EINVAL;
411 eprintsoline(so, error);
412 goto done;
413 }
414
415 if (name == NULL || namelen == 0) {
416 error = EINVAL;
417 eprintsoline(so, error);
418 goto done;
419 }
420
421 soisconnecting(so);
422 mutex_exit(&so->so_lock);
423
424 error = sctp_connect((struct sctp_s *)so->so_proto_handle,
425 name, namelen, cr, pid);
426
427 mutex_enter(&so->so_lock);
428 if (error == 0) {
429 /*
430 * Allow other threads to access the socket
431 */
432 error = sowaitconnected(so, fflag, 0);
433 }
434 done:
435 so_unlock_single(so, SOLOCKED);
436 mutex_exit(&so->so_lock);
437 return (error);
438 }
439
440 /*
441 * Active open for 1-N sockets, create a new association and
442 * call connect on that.
443 * If there parent hasn't been bound yet (this is the first association),
444 * make it so.
445 */
446 static int
sosctp_seq_connect(struct sonode * so,struct sockaddr * name,socklen_t namelen,int fflag,int flags,struct cred * cr)447 sosctp_seq_connect(struct sonode *so, struct sockaddr *name,
448 socklen_t namelen, int fflag, int flags, struct cred *cr)
449 {
450 struct sctp_soassoc *ssa;
451 struct sctp_sonode *ss;
452 int error;
453
454 ASSERT(so->so_type == SOCK_SEQPACKET);
455
456 mutex_enter(&so->so_lock);
457 so_lock_single(so);
458
459 if (name == NULL || namelen == 0) {
460 error = EINVAL;
461 eprintsoline(so, error);
462 goto done;
463 }
464
465 ss = SOTOSSO(so);
466
467 error = sosctp_assoc_createconn(ss, name, namelen, NULL, 0, fflag,
468 cr, &ssa);
469 if (error != 0) {
470 if ((error == EHOSTUNREACH) && (flags & _SOCONNECT_XPG4_2)) {
471 error = ENETUNREACH;
472 }
473 }
474 if (ssa != NULL) {
475 SSA_REFRELE(ss, ssa);
476 }
477
478 done:
479 so_unlock_single(so, SOLOCKED);
480 mutex_exit(&so->so_lock);
481 return (error);
482 }
483
484 /*
485 * Receive data.
486 */
487 /* ARGSUSED */
488 static int
sosctp_recvmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop,struct cred * cr)489 sosctp_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
490 struct cred *cr)
491 {
492 struct sctp_sonode *ss = SOTOSSO(so);
493 struct sctp_soassoc *ssa = NULL;
494 int flags, error = 0;
495 struct T_unitdata_ind *tind;
496 ssize_t orig_resid = uiop->uio_resid;
497 int len, count, readcnt = 0;
498 socklen_t controllen, namelen;
499 void *opt;
500 mblk_t *mp;
501 rval_t rval;
502
503 controllen = msg->msg_controllen;
504 namelen = msg->msg_namelen;
505 flags = msg->msg_flags;
506 msg->msg_flags = 0;
507 msg->msg_controllen = 0;
508 msg->msg_namelen = 0;
509
510 if (so->so_type == SOCK_STREAM) {
511 if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|
512 SS_CANTRCVMORE))) {
513 return (ENOTCONN);
514 }
515 } else {
516 /* NOTE: Will come here from vop_read() as well */
517 /* For 1-N socket, recv() cannot be used. */
518 if (namelen == 0)
519 return (EOPNOTSUPP);
520 /*
521 * If there are no associations, and no new connections are
522 * coming in, there's not going to be new messages coming
523 * in either.
524 */
525 if (so->so_rcv_q_head == NULL && so->so_rcv_head == NULL &&
526 ss->ss_assoccnt == 0 && !(so->so_state & SS_ACCEPTCONN)) {
527 return (ENOTCONN);
528 }
529 }
530
531 /*
532 * out-of-band data not supported.
533 */
534 if (flags & MSG_OOB) {
535 return (EOPNOTSUPP);
536 }
537
538 /*
539 * flag possibilities:
540 *
541 * MSG_PEEK Don't consume data
542 * MSG_WAITALL Wait for full quantity of data (ignored if MSG_PEEK)
543 * MSG_DONTWAIT Non-blocking (same as FNDELAY | FNONBLOCK)
544 *
545 * MSG_WAITALL can return less than the full buffer if either
546 *
547 * 1. we would block and we are non-blocking
548 * 2. a full message cannot be delivered
549 *
550 * Given that we always get a full message from proto below,
551 * MSG_WAITALL is not meaningful.
552 */
553
554 mutex_enter(&so->so_lock);
555
556 /*
557 * Allow just one reader at a time.
558 */
559 error = so_lock_read_intr(so,
560 uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
561 if (error) {
562 mutex_exit(&so->so_lock);
563 return (error);
564 }
565 mutex_exit(&so->so_lock);
566
567 error = so_dequeue_msg(so, &mp, uiop, &rval, flags | MSG_DUPCTRL);
568 if (mp != NULL) {
569 if (so->so_type == SOCK_SEQPACKET) {
570 ssa = *(struct sctp_soassoc **)DB_BASE(mp);
571 }
572
573 tind = (struct T_unitdata_ind *)mp->b_rptr;
574
575 len = tind->SRC_length;
576
577 if (namelen > 0 && len > 0) {
578
579 opt = sogetoff(mp, tind->SRC_offset, len, 1);
580
581 ASSERT(opt != NULL);
582
583 msg->msg_name = kmem_alloc(len, KM_SLEEP);
584 msg->msg_namelen = len;
585
586 bcopy(opt, msg->msg_name, len);
587 }
588
589 len = tind->OPT_length;
590 if (controllen == 0) {
591 if (len > 0) {
592 msg->msg_flags |= MSG_CTRUNC;
593 }
594 } else if (len > 0) {
595 opt = sogetoff(mp, tind->OPT_offset, len,
596 __TPI_ALIGN_SIZE);
597
598 ASSERT(opt != NULL);
599 sosctp_pack_cmsg(opt, msg, len);
600 }
601
602 if (mp->b_flag & SCTP_NOTIFICATION) {
603 msg->msg_flags |= MSG_NOTIFICATION;
604 }
605
606 if (!(mp->b_flag & SCTP_PARTIAL_DATA) &&
607 !(rval.r_val1 & MOREDATA)) {
608 msg->msg_flags |= MSG_EOR;
609 }
610 freemsg(mp);
611 }
612
613 if (!(flags & MSG_PEEK))
614 readcnt = orig_resid - uiop->uio_resid;
615 /*
616 * Determine if we need to update SCTP about the buffer
617 * space. For performance reason, we cannot update SCTP
618 * every time a message is read. The socket buffer low
619 * watermark is used as the threshold.
620 */
621 if (ssa == NULL) {
622 mutex_enter(&so->so_lock);
623 count = so->so_rcvbuf - so->so_rcv_queued;
624
625 ASSERT(so->so_rcv_q_head != NULL ||
626 so->so_rcv_head != NULL ||
627 so->so_rcv_queued == 0);
628
629 so_unlock_read(so);
630
631 /*
632 * so_dequeue_msg() sets r_val2 to true if flow control was
633 * cleared and we need to update SCTP. so_flowctrld was
634 * cleared in so_dequeue_msg() via so_check_flow_control().
635 */
636 if (rval.r_val2) {
637 mutex_exit(&so->so_lock);
638 sctp_recvd((struct sctp_s *)so->so_proto_handle, count);
639 } else {
640 mutex_exit(&so->so_lock);
641 }
642 } else {
643 /*
644 * Each association keeps track of how much data it has
645 * queued; we need to update the value here. Note that this
646 * is slightly different from SOCK_STREAM type sockets, which
647 * does not need to update the byte count, as it is already
648 * done in so_dequeue_msg().
649 */
650 mutex_enter(&so->so_lock);
651 ssa->ssa_rcv_queued -= readcnt;
652 count = so->so_rcvbuf - ssa->ssa_rcv_queued;
653
654 so_unlock_read(so);
655
656 if (readcnt > 0 && ssa->ssa_flowctrld &&
657 ssa->ssa_rcv_queued < so->so_rcvlowat) {
658 /*
659 * Need to clear ssa_flowctrld, different from 1-1
660 * style.
661 */
662 ssa->ssa_flowctrld = B_FALSE;
663 mutex_exit(&so->so_lock);
664 sctp_recvd(ssa->ssa_conn, count);
665 mutex_enter(&so->so_lock);
666 }
667
668 /*
669 * MOREDATA flag is set if all data could not be copied
670 */
671 if (!(flags & MSG_PEEK) && !(rval.r_val1 & MOREDATA)) {
672 SSA_REFRELE(ss, ssa);
673 }
674 mutex_exit(&so->so_lock);
675 }
676
677 return (error);
678 }
679
680 int
sosctp_uiomove(mblk_t * hdr_mp,ssize_t count,ssize_t blk_size,int wroff,struct uio * uiop,int flags)681 sosctp_uiomove(mblk_t *hdr_mp, ssize_t count, ssize_t blk_size, int wroff,
682 struct uio *uiop, int flags)
683 {
684 ssize_t size;
685 int error;
686 mblk_t *mp;
687 dblk_t *dp;
688
689 if (blk_size == INFPSZ)
690 blk_size = count;
691
692 /*
693 * Loop until we have all data copied into mblk's.
694 */
695 while (count > 0) {
696 size = MIN(count, blk_size);
697
698 /*
699 * As a message can be splitted up and sent in different
700 * packets, each mblk will have the extra space before
701 * data to accommodate what SCTP wants to put in there.
702 */
703 while ((mp = allocb(size + wroff, BPRI_MED)) == NULL) {
704 if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) ||
705 (flags & MSG_DONTWAIT)) {
706 return (EAGAIN);
707 }
708 if ((error = strwaitbuf(size + wroff, BPRI_MED))) {
709 return (error);
710 }
711 }
712
713 dp = mp->b_datap;
714 dp->db_cpid = curproc->p_pid;
715 ASSERT(wroff <= dp->db_lim - mp->b_wptr);
716 mp->b_rptr += wroff;
717 error = uiomove(mp->b_rptr, size, UIO_WRITE, uiop);
718 if (error != 0) {
719 freeb(mp);
720 return (error);
721 }
722 mp->b_wptr = mp->b_rptr + size;
723 count -= size;
724 hdr_mp->b_cont = mp;
725 hdr_mp = mp;
726 }
727 return (0);
728 }
729
730 /*
731 * Send message.
732 */
733 static int
sosctp_sendmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop,struct cred * cr)734 sosctp_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
735 struct cred *cr)
736 {
737 mblk_t *mctl;
738 struct cmsghdr *cmsg;
739 struct sctp_sndrcvinfo *sinfo;
740 int optlen, flags, fflag;
741 ssize_t count, msglen;
742 int error;
743
744 ASSERT(so->so_type == SOCK_STREAM);
745
746 flags = msg->msg_flags;
747 if (flags & MSG_OOB) {
748 /*
749 * No out-of-band data support.
750 */
751 return (EOPNOTSUPP);
752 }
753
754 if (msg->msg_controllen != 0) {
755 optlen = msg->msg_controllen;
756 cmsg = sosctp_find_cmsg(msg->msg_control, optlen, SCTP_SNDRCV);
757 if (cmsg != NULL) {
758 if (cmsg->cmsg_len <
759 (sizeof (*sinfo) + sizeof (*cmsg))) {
760 eprintsoline(so, EINVAL);
761 return (EINVAL);
762 }
763 sinfo = (struct sctp_sndrcvinfo *)(cmsg + 1);
764
765 /* Both flags should not be set together. */
766 if ((sinfo->sinfo_flags & MSG_EOF) &&
767 (sinfo->sinfo_flags & MSG_ABORT)) {
768 eprintsoline(so, EINVAL);
769 return (EINVAL);
770 }
771
772 /* Initiate a graceful shutdown. */
773 if (sinfo->sinfo_flags & MSG_EOF) {
774 /* Can't include data in MSG_EOF message. */
775 if (uiop->uio_resid != 0) {
776 eprintsoline(so, EINVAL);
777 return (EINVAL);
778 }
779
780 /*
781 * This is the same sequence as done in
782 * shutdown(SHUT_WR).
783 */
784 mutex_enter(&so->so_lock);
785 so_lock_single(so);
786 socantsendmore(so);
787 cv_broadcast(&so->so_snd_cv);
788 so->so_state |= SS_ISDISCONNECTING;
789 mutex_exit(&so->so_lock);
790
791 pollwakeup(&so->so_poll_list, POLLOUT);
792 sctp_recvd((struct sctp_s *)so->so_proto_handle,
793 so->so_rcvbuf);
794 error = sctp_disconnect(
795 (struct sctp_s *)so->so_proto_handle);
796
797 mutex_enter(&so->so_lock);
798 so_unlock_single(so, SOLOCKED);
799 mutex_exit(&so->so_lock);
800 return (error);
801 }
802 }
803 } else {
804 optlen = 0;
805 }
806
807 mutex_enter(&so->so_lock);
808 for (;;) {
809 if (so->so_state & SS_CANTSENDMORE) {
810 mutex_exit(&so->so_lock);
811 return (EPIPE);
812 }
813
814 if (so->so_error != 0) {
815 error = sogeterr(so, B_TRUE);
816 mutex_exit(&so->so_lock);
817 return (error);
818 }
819
820 if (!so->so_snd_qfull)
821 break;
822
823 if (so->so_state & SS_CLOSING) {
824 mutex_exit(&so->so_lock);
825 return (EINTR);
826 }
827 /*
828 * Xmit window full in a blocking socket.
829 */
830 if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) ||
831 (flags & MSG_DONTWAIT)) {
832 mutex_exit(&so->so_lock);
833 return (EAGAIN);
834 } else {
835 /*
836 * Wait for space to become available and try again.
837 */
838 error = cv_wait_sig(&so->so_snd_cv, &so->so_lock);
839 if (!error) { /* signal */
840 mutex_exit(&so->so_lock);
841 return (EINTR);
842 }
843 }
844 }
845 msglen = count = uiop->uio_resid;
846
847 /* Don't allow sending a message larger than the send buffer size. */
848 /* XXX Transport module need to enforce this */
849 if (msglen > so->so_sndbuf) {
850 mutex_exit(&so->so_lock);
851 return (EMSGSIZE);
852 }
853
854 /*
855 * Allow piggybacking data on handshake messages (SS_ISCONNECTING).
856 */
857 if (!(so->so_state & (SS_ISCONNECTING | SS_ISCONNECTED))) {
858 /*
859 * We need to check here for listener so that the
860 * same error will be returned as with a TCP socket.
861 * In this case, sosctp_connect() returns EOPNOTSUPP
862 * while a TCP socket returns ENOTCONN instead. Catch it
863 * here to have the same behavior as a TCP socket.
864 *
865 * We also need to make sure that the peer address is
866 * provided before we attempt to do the connect.
867 */
868 if ((so->so_state & SS_ACCEPTCONN) ||
869 msg->msg_name == NULL) {
870 mutex_exit(&so->so_lock);
871 error = ENOTCONN;
872 goto error_nofree;
873 }
874 mutex_exit(&so->so_lock);
875 fflag = uiop->uio_fmode;
876 if (flags & MSG_DONTWAIT) {
877 fflag |= FNDELAY;
878 }
879 error = sosctp_connect(so, msg->msg_name, msg->msg_namelen,
880 fflag, (so->so_version == SOV_XPG4_2) * _SOCONNECT_XPG4_2,
881 cr);
882 if (error) {
883 /*
884 * Check for non-fatal errors, socket connected
885 * while the lock had been lifted.
886 */
887 if (error != EISCONN && error != EALREADY) {
888 goto error_nofree;
889 }
890 error = 0;
891 }
892 } else {
893 mutex_exit(&so->so_lock);
894 }
895
896 mctl = sctp_alloc_hdr(msg->msg_name, msg->msg_namelen,
897 msg->msg_control, optlen, SCTP_CAN_BLOCK);
898 if (mctl == NULL) {
899 error = EINTR;
900 goto error_nofree;
901 }
902
903 /* Copy in the message. */
904 if ((error = sosctp_uiomove(mctl, count, so->so_proto_props.sopp_maxblk,
905 so->so_proto_props.sopp_wroff, uiop, flags)) != 0) {
906 goto error_ret;
907 }
908 error = sctp_sendmsg((struct sctp_s *)so->so_proto_handle, mctl, 0);
909 if (error == 0)
910 return (0);
911
912 error_ret:
913 freemsg(mctl);
914 error_nofree:
915 mutex_enter(&so->so_lock);
916 if ((error == EPIPE) && (so->so_state & SS_CANTSENDMORE)) {
917 /*
918 * We received shutdown between the time lock was
919 * lifted and call to sctp_sendmsg().
920 */
921 mutex_exit(&so->so_lock);
922 return (EPIPE);
923 }
924 mutex_exit(&so->so_lock);
925 return (error);
926 }
927
928 /*
929 * Send message on 1-N socket. Connects automatically if there is
930 * no association.
931 */
932 static int
sosctp_seq_sendmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop,struct cred * cr)933 sosctp_seq_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
934 struct cred *cr)
935 {
936 struct sctp_sonode *ss;
937 struct sctp_soassoc *ssa;
938 struct cmsghdr *cmsg;
939 struct sctp_sndrcvinfo *sinfo;
940 int aid = 0;
941 mblk_t *mctl;
942 int namelen, optlen, flags;
943 ssize_t count, msglen;
944 int error;
945 uint16_t s_flags = 0;
946
947 ASSERT(so->so_type == SOCK_SEQPACKET);
948
949 /*
950 * There shouldn't be problems with alignment, as the memory for
951 * msg_control was alloced with kmem_alloc.
952 */
953 cmsg = sosctp_find_cmsg(msg->msg_control, msg->msg_controllen,
954 SCTP_SNDRCV);
955 if (cmsg != NULL) {
956 if (cmsg->cmsg_len < (sizeof (*sinfo) + sizeof (*cmsg))) {
957 eprintsoline(so, EINVAL);
958 return (EINVAL);
959 }
960 sinfo = (struct sctp_sndrcvinfo *)(cmsg + 1);
961 s_flags = sinfo->sinfo_flags;
962 aid = sinfo->sinfo_assoc_id;
963 }
964
965 ss = SOTOSSO(so);
966 namelen = msg->msg_namelen;
967
968 if (msg->msg_controllen > 0) {
969 optlen = msg->msg_controllen;
970 } else {
971 optlen = 0;
972 }
973
974 mutex_enter(&so->so_lock);
975
976 /*
977 * If there is no association id, connect to address specified
978 * in msg_name. Otherwise look up the association using the id.
979 */
980 if (aid == 0) {
981 /*
982 * Connect and shutdown cannot be done together, so check for
983 * MSG_EOF.
984 */
985 if (msg->msg_name == NULL || namelen == 0 ||
986 (s_flags & MSG_EOF)) {
987 error = EINVAL;
988 eprintsoline(so, error);
989 goto done;
990 }
991 flags = uiop->uio_fmode;
992 if (msg->msg_flags & MSG_DONTWAIT) {
993 flags |= FNDELAY;
994 }
995 so_lock_single(so);
996 error = sosctp_assoc_createconn(ss, msg->msg_name, namelen,
997 msg->msg_control, optlen, flags, cr, &ssa);
998 if (error) {
999 if ((so->so_version == SOV_XPG4_2) &&
1000 (error == EHOSTUNREACH)) {
1001 error = ENETUNREACH;
1002 }
1003 if (ssa == NULL) {
1004 /*
1005 * Fatal error during connect(). Bail out.
1006 * If ssa exists, it means that the handshake
1007 * is in progress.
1008 */
1009 eprintsoline(so, error);
1010 so_unlock_single(so, SOLOCKED);
1011 goto done;
1012 }
1013 /*
1014 * All the errors are non-fatal ones, don't return
1015 * e.g. EINPROGRESS from sendmsg().
1016 */
1017 error = 0;
1018 }
1019 so_unlock_single(so, SOLOCKED);
1020 } else {
1021 if ((error = sosctp_assoc(ss, aid, &ssa)) != 0) {
1022 eprintsoline(so, error);
1023 goto done;
1024 }
1025 }
1026
1027 /*
1028 * Now we have an association.
1029 */
1030 flags = msg->msg_flags;
1031
1032 /*
1033 * MSG_EOF initiates graceful shutdown.
1034 */
1035 if (s_flags & MSG_EOF) {
1036 if (uiop->uio_resid) {
1037 /*
1038 * Can't include data in MSG_EOF message.
1039 */
1040 error = EINVAL;
1041 } else {
1042 mutex_exit(&so->so_lock);
1043 ssa->ssa_state |= SS_ISDISCONNECTING;
1044 sctp_recvd(ssa->ssa_conn, so->so_rcvbuf);
1045 error = sctp_disconnect(ssa->ssa_conn);
1046 mutex_enter(&so->so_lock);
1047 }
1048 goto refrele;
1049 }
1050
1051 for (;;) {
1052 if (ssa->ssa_state & SS_CANTSENDMORE) {
1053 SSA_REFRELE(ss, ssa);
1054 mutex_exit(&so->so_lock);
1055 return (EPIPE);
1056 }
1057 if (ssa->ssa_error != 0) {
1058 error = ssa->ssa_error;
1059 ssa->ssa_error = 0;
1060 goto refrele;
1061 }
1062
1063 if (!ssa->ssa_snd_qfull)
1064 break;
1065
1066 if (so->so_state & SS_CLOSING) {
1067 error = EINTR;
1068 goto refrele;
1069 }
1070 if ((uiop->uio_fmode & (FNDELAY|FNONBLOCK)) ||
1071 (flags & MSG_DONTWAIT)) {
1072 error = EAGAIN;
1073 goto refrele;
1074 } else {
1075 /*
1076 * Wait for space to become available and try again.
1077 */
1078 error = cv_wait_sig(&so->so_snd_cv, &so->so_lock);
1079 if (!error) { /* signal */
1080 error = EINTR;
1081 goto refrele;
1082 }
1083 }
1084 }
1085
1086 msglen = count = uiop->uio_resid;
1087
1088 /* Don't allow sending a message larger than the send buffer size. */
1089 if (msglen > so->so_sndbuf) {
1090 error = EMSGSIZE;
1091 goto refrele;
1092 }
1093
1094 /*
1095 * Update TX buffer usage here so that we can lift the socket lock.
1096 */
1097 mutex_exit(&so->so_lock);
1098
1099 mctl = sctp_alloc_hdr(msg->msg_name, namelen, msg->msg_control,
1100 optlen, SCTP_CAN_BLOCK);
1101 if (mctl == NULL) {
1102 error = EINTR;
1103 goto lock_rele;
1104 }
1105
1106 /* Copy in the message. */
1107 if ((error = sosctp_uiomove(mctl, count, ssa->ssa_wrsize,
1108 ssa->ssa_wroff, uiop, flags)) != 0) {
1109 goto lock_rele;
1110 }
1111 error = sctp_sendmsg((struct sctp_s *)ssa->ssa_conn, mctl, 0);
1112 lock_rele:
1113 mutex_enter(&so->so_lock);
1114 if (error != 0) {
1115 freemsg(mctl);
1116 if ((error == EPIPE) && (ssa->ssa_state & SS_CANTSENDMORE)) {
1117 /*
1118 * We received shutdown between the time lock was
1119 * lifted and call to sctp_sendmsg().
1120 */
1121 SSA_REFRELE(ss, ssa);
1122 mutex_exit(&so->so_lock);
1123 return (EPIPE);
1124 }
1125 }
1126
1127 refrele:
1128 SSA_REFRELE(ss, ssa);
1129 done:
1130 mutex_exit(&so->so_lock);
1131 return (error);
1132 }
1133
1134 /*
1135 * Get address of remote node.
1136 */
1137 /* ARGSUSED */
1138 static int
sosctp_getpeername(struct sonode * so,struct sockaddr * addr,socklen_t * addrlen,boolean_t accept,struct cred * cr)1139 sosctp_getpeername(struct sonode *so, struct sockaddr *addr, socklen_t *addrlen,
1140 boolean_t accept, struct cred *cr)
1141 {
1142 return (sctp_getpeername((struct sctp_s *)so->so_proto_handle, addr,
1143 addrlen));
1144 }
1145
1146 /*
1147 * Get local address.
1148 */
1149 /* ARGSUSED */
1150 static int
sosctp_getsockname(struct sonode * so,struct sockaddr * addr,socklen_t * addrlen,struct cred * cr)1151 sosctp_getsockname(struct sonode *so, struct sockaddr *addr, socklen_t *addrlen,
1152 struct cred *cr)
1153 {
1154 return (sctp_getsockname((struct sctp_s *)so->so_proto_handle, addr,
1155 addrlen));
1156 }
1157
1158 /*
1159 * Called from shutdown().
1160 */
1161 /* ARGSUSED */
1162 static int
sosctp_shutdown(struct sonode * so,int how,struct cred * cr)1163 sosctp_shutdown(struct sonode *so, int how, struct cred *cr)
1164 {
1165 uint_t state_change;
1166 int wakesig = 0;
1167 int error = 0;
1168
1169 mutex_enter(&so->so_lock);
1170 /*
1171 * Record the current state and then perform any state changes.
1172 * Then use the difference between the old and new states to
1173 * determine which needs to be done.
1174 */
1175 state_change = so->so_state;
1176
1177 switch (how) {
1178 case SHUT_RD:
1179 socantrcvmore(so);
1180 break;
1181 case SHUT_WR:
1182 socantsendmore(so);
1183 break;
1184 case SHUT_RDWR:
1185 socantsendmore(so);
1186 socantrcvmore(so);
1187 break;
1188 default:
1189 mutex_exit(&so->so_lock);
1190 return (EINVAL);
1191 }
1192
1193 state_change = so->so_state & ~state_change;
1194
1195 if (state_change & SS_CANTRCVMORE) {
1196 if (so->so_rcv_q_head == NULL) {
1197 cv_signal(&so->so_rcv_cv);
1198 }
1199 wakesig = POLLIN|POLLRDNORM;
1200
1201 socket_sendsig(so, SOCKETSIG_READ);
1202 }
1203 if (state_change & SS_CANTSENDMORE) {
1204 cv_broadcast(&so->so_snd_cv);
1205 wakesig |= POLLOUT;
1206
1207 so->so_state |= SS_ISDISCONNECTING;
1208 }
1209 mutex_exit(&so->so_lock);
1210
1211 pollwakeup(&so->so_poll_list, wakesig);
1212
1213 if (state_change & SS_CANTSENDMORE) {
1214 sctp_recvd((struct sctp_s *)so->so_proto_handle, so->so_rcvbuf);
1215 error = sctp_disconnect((struct sctp_s *)so->so_proto_handle);
1216 }
1217
1218 /*
1219 * HACK: sctp_disconnect() may return EWOULDBLOCK. But this error is
1220 * not documented in standard socket API. Catch it here.
1221 */
1222 if (error == EWOULDBLOCK)
1223 error = 0;
1224 return (error);
1225 }
1226
1227 /*
1228 * Get socket options.
1229 */
1230 /*ARGSUSED5*/
1231 static int
sosctp_getsockopt(struct sonode * so,int level,int option_name,void * optval,socklen_t * optlenp,int flags,struct cred * cr)1232 sosctp_getsockopt(struct sonode *so, int level, int option_name,
1233 void *optval, socklen_t *optlenp, int flags, struct cred *cr)
1234 {
1235 socklen_t maxlen = *optlenp;
1236 socklen_t len;
1237 socklen_t optlen;
1238 uint8_t buffer[4];
1239 void *optbuf = &buffer;
1240 int error = 0;
1241
1242 if (level == SOL_SOCKET) {
1243 switch (option_name) {
1244 /* Not supported options */
1245 case SO_SNDTIMEO:
1246 case SO_RCVTIMEO:
1247 case SO_EXCLBIND:
1248 eprintsoline(so, ENOPROTOOPT);
1249 return (ENOPROTOOPT);
1250 default:
1251 error = socket_getopt_common(so, level, option_name,
1252 optval, optlenp, flags);
1253 if (error >= 0)
1254 return (error);
1255 /* Pass the request to the protocol */
1256 break;
1257 }
1258 }
1259
1260 if (level == IPPROTO_SCTP) {
1261 /*
1262 * Should go through ioctl().
1263 */
1264 return (EINVAL);
1265 }
1266
1267 if (maxlen > sizeof (buffer)) {
1268 optbuf = kmem_alloc(maxlen, KM_SLEEP);
1269 }
1270 optlen = maxlen;
1271
1272 /*
1273 * If the resulting optlen is greater than the provided maxlen, then
1274 * we sliently trucate.
1275 */
1276 error = sctp_get_opt((struct sctp_s *)so->so_proto_handle, level,
1277 option_name, optbuf, &optlen);
1278
1279 if (error != 0) {
1280 eprintsoline(so, error);
1281 goto free;
1282 }
1283
1284 len = MIN(optlen, maxlen);
1285 bcopy(optbuf, optval, len);
1286 *optlenp = optlen;
1287 free:
1288 if (optbuf != &buffer) {
1289 kmem_free(optbuf, maxlen);
1290 }
1291
1292 return (error);
1293 }
1294
1295 /*
1296 * Set socket options
1297 */
1298 /* ARGSUSED */
1299 static int
sosctp_setsockopt(struct sonode * so,int level,int option_name,const void * optval,t_uscalar_t optlen,struct cred * cr)1300 sosctp_setsockopt(struct sonode *so, int level, int option_name,
1301 const void *optval, t_uscalar_t optlen, struct cred *cr)
1302 {
1303 struct sctp_sonode *ss = SOTOSSO(so);
1304 struct sctp_soassoc *ssa = NULL;
1305 sctp_assoc_t id;
1306 int error, rc;
1307 void *conn = NULL;
1308
1309 mutex_enter(&so->so_lock);
1310
1311 /*
1312 * For some SCTP level options, one can select the association this
1313 * applies to.
1314 */
1315 if (so->so_type == SOCK_STREAM) {
1316 conn = so->so_proto_handle;
1317 } else {
1318 /*
1319 * SOCK_SEQPACKET only
1320 */
1321 id = 0;
1322 if (level == IPPROTO_SCTP) {
1323 switch (option_name) {
1324 case SCTP_RTOINFO:
1325 case SCTP_ASSOCINFO:
1326 case SCTP_SET_PEER_PRIMARY_ADDR:
1327 case SCTP_PRIMARY_ADDR:
1328 case SCTP_PEER_ADDR_PARAMS:
1329 /*
1330 * Association ID is the first element
1331 * params struct
1332 */
1333 if (optlen < sizeof (sctp_assoc_t)) {
1334 error = EINVAL;
1335 eprintsoline(so, error);
1336 goto done;
1337 }
1338 id = *(sctp_assoc_t *)optval;
1339 break;
1340 case SCTP_DEFAULT_SEND_PARAM:
1341 if (optlen != sizeof (struct sctp_sndrcvinfo)) {
1342 error = EINVAL;
1343 eprintsoline(so, error);
1344 goto done;
1345 }
1346 id = ((struct sctp_sndrcvinfo *)
1347 optval)->sinfo_assoc_id;
1348 break;
1349 case SCTP_INITMSG:
1350 /*
1351 * Only applies to future associations
1352 */
1353 conn = so->so_proto_handle;
1354 break;
1355 default:
1356 break;
1357 }
1358 } else if (level == SOL_SOCKET) {
1359 if (option_name == SO_LINGER) {
1360 error = EOPNOTSUPP;
1361 eprintsoline(so, error);
1362 goto done;
1363 }
1364 /*
1365 * These 2 options are applied to all associations.
1366 * The other socket level options are only applied
1367 * to the socket (not associations).
1368 */
1369 if ((option_name != SO_RCVBUF) &&
1370 (option_name != SO_SNDBUF)) {
1371 conn = so->so_proto_handle;
1372 }
1373 } else {
1374 conn = NULL;
1375 }
1376
1377 /*
1378 * If association ID was specified, do op on that assoc.
1379 * Otherwise set the default setting of a socket.
1380 */
1381 if (id != 0) {
1382 if ((error = sosctp_assoc(ss, id, &ssa)) != 0) {
1383 eprintsoline(so, error);
1384 goto done;
1385 }
1386 conn = ssa->ssa_conn;
1387 }
1388 }
1389 dprint(2, ("sosctp_setsockopt %p (%d) - conn %p %d %d id:%d\n",
1390 (void *)ss, so->so_type, (void *)conn, level, option_name, id));
1391
1392 ASSERT(ssa == NULL || (ssa != NULL && conn != NULL));
1393 if (conn != NULL) {
1394 mutex_exit(&so->so_lock);
1395 error = sctp_set_opt((struct sctp_s *)conn, level, option_name,
1396 optval, optlen);
1397 mutex_enter(&so->so_lock);
1398 if (ssa != NULL)
1399 SSA_REFRELE(ss, ssa);
1400 } else {
1401 /*
1402 * 1-N socket, and we have to apply the operation to ALL
1403 * associations. Like with anything of this sort, the
1404 * problem is what to do if the operation fails.
1405 * Just try to apply the setting to everyone, but store
1406 * error number if someone returns such. And since we are
1407 * looping through all possible aids, some of them can be
1408 * invalid. We just ignore this kind (sosctp_assoc()) of
1409 * errors.
1410 */
1411 sctp_assoc_t aid;
1412
1413 mutex_exit(&so->so_lock);
1414 error = sctp_set_opt((struct sctp_s *)so->so_proto_handle,
1415 level, option_name, optval, optlen);
1416 mutex_enter(&so->so_lock);
1417 for (aid = 1; aid < ss->ss_maxassoc; aid++) {
1418 if (sosctp_assoc(ss, aid, &ssa) != 0)
1419 continue;
1420 mutex_exit(&so->so_lock);
1421 rc = sctp_set_opt((struct sctp_s *)ssa->ssa_conn, level,
1422 option_name, optval, optlen);
1423 mutex_enter(&so->so_lock);
1424 SSA_REFRELE(ss, ssa);
1425 if (error == 0) {
1426 error = rc;
1427 }
1428 }
1429 }
1430 done:
1431 mutex_exit(&so->so_lock);
1432 return (error);
1433 }
1434
1435 /*ARGSUSED*/
1436 static int
sosctp_ioctl(struct sonode * so,int cmd,intptr_t arg,int mode,struct cred * cr,int32_t * rvalp)1437 sosctp_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode,
1438 struct cred *cr, int32_t *rvalp)
1439 {
1440 struct sctp_sonode *ss;
1441 int32_t value;
1442 int error;
1443 int intval;
1444 pid_t pid;
1445 struct sctp_soassoc *ssa;
1446 void *conn;
1447 void *buf;
1448 STRUCT_DECL(sctpopt, opt);
1449 uint32_t optlen;
1450 int buflen;
1451
1452 ss = SOTOSSO(so);
1453
1454 /* handle socket specific ioctls */
1455 switch (cmd) {
1456 case FIONBIO:
1457 if (so_copyin((void *)arg, &value, sizeof (int32_t),
1458 (mode & (int)FKIOCTL))) {
1459 return (EFAULT);
1460 }
1461 mutex_enter(&so->so_lock);
1462 if (value) {
1463 so->so_state |= SS_NDELAY;
1464 } else {
1465 so->so_state &= ~SS_NDELAY;
1466 }
1467 mutex_exit(&so->so_lock);
1468 return (0);
1469
1470 case FIOASYNC:
1471 if (so_copyin((void *)arg, &value, sizeof (int32_t),
1472 (mode & (int)FKIOCTL))) {
1473 return (EFAULT);
1474 }
1475 mutex_enter(&so->so_lock);
1476
1477 if (value) {
1478 /* Turn on SIGIO */
1479 so->so_state |= SS_ASYNC;
1480 } else {
1481 /* Turn off SIGIO */
1482 so->so_state &= ~SS_ASYNC;
1483 }
1484 mutex_exit(&so->so_lock);
1485 return (0);
1486
1487 case SIOCSPGRP:
1488 case FIOSETOWN:
1489 if (so_copyin((void *)arg, &pid, sizeof (pid_t),
1490 (mode & (int)FKIOCTL))) {
1491 return (EFAULT);
1492 }
1493 mutex_enter(&so->so_lock);
1494
1495 error = (pid != so->so_pgrp) ? socket_chgpgrp(so, pid) : 0;
1496 mutex_exit(&so->so_lock);
1497 return (error);
1498
1499 case SIOCGPGRP:
1500 case FIOGETOWN:
1501 if (so_copyout(&so->so_pgrp, (void *)arg,
1502 sizeof (pid_t), (mode & (int)FKIOCTL)))
1503 return (EFAULT);
1504 return (0);
1505
1506 case FIONREAD:
1507 /* XXX: Cannot be used unless standard buffer is used */
1508 /*
1509 * Return number of bytes of data in all data messages
1510 * in queue in "arg".
1511 * For stream socket, amount of available data.
1512 * For sock_dgram, # of available bytes + addresses.
1513 */
1514 intval = (so->so_state & SS_ACCEPTCONN) ? 0 :
1515 MIN(so->so_rcv_queued, INT_MAX);
1516 if (so_copyout(&intval, (void *)arg, sizeof (intval),
1517 (mode & (int)FKIOCTL)))
1518 return (EFAULT);
1519 return (0);
1520 case SIOCATMARK:
1521 /*
1522 * No support for urgent data.
1523 */
1524 intval = 0;
1525
1526 if (so_copyout(&intval, (void *)arg, sizeof (int),
1527 (mode & (int)FKIOCTL)))
1528 return (EFAULT);
1529 return (0);
1530 case _I_GETPEERCRED: {
1531 int error = 0;
1532
1533 if ((mode & FKIOCTL) == 0)
1534 return (EINVAL);
1535
1536 mutex_enter(&so->so_lock);
1537 if ((so->so_mode & SM_CONNREQUIRED) == 0) {
1538 error = ENOTSUP;
1539 } else if ((so->so_state & SS_ISCONNECTED) == 0) {
1540 error = ENOTCONN;
1541 } else if (so->so_peercred != NULL) {
1542 k_peercred_t *kp = (k_peercred_t *)arg;
1543 kp->pc_cr = so->so_peercred;
1544 kp->pc_cpid = so->so_cpid;
1545 crhold(so->so_peercred);
1546 } else {
1547 error = EINVAL;
1548 }
1549 mutex_exit(&so->so_lock);
1550 return (error);
1551 }
1552 case SIOCSCTPGOPT:
1553 STRUCT_INIT(opt, mode);
1554
1555 if (so_copyin((void *)arg, STRUCT_BUF(opt), STRUCT_SIZE(opt),
1556 (mode & (int)FKIOCTL))) {
1557 return (EFAULT);
1558 }
1559 if ((optlen = STRUCT_FGET(opt, sopt_len)) > SO_MAXARGSIZE)
1560 return (EINVAL);
1561
1562 /*
1563 * Find the correct sctp_t based on whether it is 1-N socket
1564 * or not.
1565 */
1566 intval = STRUCT_FGET(opt, sopt_aid);
1567 mutex_enter(&so->so_lock);
1568 if ((so->so_type == SOCK_SEQPACKET) && intval) {
1569 if ((error = sosctp_assoc(ss, intval, &ssa)) != 0) {
1570 mutex_exit(&so->so_lock);
1571 return (error);
1572 }
1573 conn = ssa->ssa_conn;
1574 ASSERT(conn != NULL);
1575 } else {
1576 conn = so->so_proto_handle;
1577 ssa = NULL;
1578 }
1579 mutex_exit(&so->so_lock);
1580
1581 /* Copyin the option buffer and then call sctp_get_opt(). */
1582 buflen = optlen;
1583 /* Let's allocate a buffer enough to hold an int */
1584 if (buflen < sizeof (uint32_t))
1585 buflen = sizeof (uint32_t);
1586 buf = kmem_alloc(buflen, KM_SLEEP);
1587 if (so_copyin(STRUCT_FGETP(opt, sopt_val), buf, optlen,
1588 (mode & (int)FKIOCTL))) {
1589 if (ssa != NULL) {
1590 mutex_enter(&so->so_lock);
1591 SSA_REFRELE(ss, ssa);
1592 mutex_exit(&so->so_lock);
1593 }
1594 kmem_free(buf, buflen);
1595 return (EFAULT);
1596 }
1597 /* The option level has to be IPPROTO_SCTP */
1598 error = sctp_get_opt((struct sctp_s *)conn, IPPROTO_SCTP,
1599 STRUCT_FGET(opt, sopt_name), buf, &optlen);
1600 if (ssa != NULL) {
1601 mutex_enter(&so->so_lock);
1602 SSA_REFRELE(ss, ssa);
1603 mutex_exit(&so->so_lock);
1604 }
1605 optlen = MIN(buflen, optlen);
1606 /* No error, copyout the result with the correct buf len. */
1607 if (error == 0) {
1608 STRUCT_FSET(opt, sopt_len, optlen);
1609 if (so_copyout(STRUCT_BUF(opt), (void *)arg,
1610 STRUCT_SIZE(opt), (mode & (int)FKIOCTL))) {
1611 error = EFAULT;
1612 } else if (so_copyout(buf, STRUCT_FGETP(opt, sopt_val),
1613 optlen, (mode & (int)FKIOCTL))) {
1614 error = EFAULT;
1615 }
1616 }
1617 kmem_free(buf, buflen);
1618 return (error);
1619
1620 case SIOCSCTPSOPT:
1621 STRUCT_INIT(opt, mode);
1622
1623 if (so_copyin((void *)arg, STRUCT_BUF(opt), STRUCT_SIZE(opt),
1624 (mode & (int)FKIOCTL))) {
1625 return (EFAULT);
1626 }
1627 if ((optlen = STRUCT_FGET(opt, sopt_len)) > SO_MAXARGSIZE)
1628 return (EINVAL);
1629
1630 /*
1631 * Find the correct sctp_t based on whether it is 1-N socket
1632 * or not.
1633 */
1634 intval = STRUCT_FGET(opt, sopt_aid);
1635 mutex_enter(&so->so_lock);
1636 if (intval != 0) {
1637 if ((error = sosctp_assoc(ss, intval, &ssa)) != 0) {
1638 mutex_exit(&so->so_lock);
1639 return (error);
1640 }
1641 conn = ssa->ssa_conn;
1642 ASSERT(conn != NULL);
1643 } else {
1644 conn = so->so_proto_handle;
1645 ssa = NULL;
1646 }
1647 mutex_exit(&so->so_lock);
1648
1649 /* Copyin the option buffer and then call sctp_set_opt(). */
1650 buf = kmem_alloc(optlen, KM_SLEEP);
1651 if (so_copyin(STRUCT_FGETP(opt, sopt_val), buf, optlen,
1652 (mode & (int)FKIOCTL))) {
1653 if (ssa != NULL) {
1654 mutex_enter(&so->so_lock);
1655 SSA_REFRELE(ss, ssa);
1656 mutex_exit(&so->so_lock);
1657 }
1658 kmem_free(buf, intval);
1659 return (EFAULT);
1660 }
1661 /* The option level has to be IPPROTO_SCTP */
1662 error = sctp_set_opt((struct sctp_s *)conn, IPPROTO_SCTP,
1663 STRUCT_FGET(opt, sopt_name), buf, optlen);
1664 if (ssa) {
1665 mutex_enter(&so->so_lock);
1666 SSA_REFRELE(ss, ssa);
1667 mutex_exit(&so->so_lock);
1668 }
1669 kmem_free(buf, optlen);
1670 return (error);
1671
1672 case SIOCSCTPPEELOFF: {
1673 struct sonode *nso;
1674 struct sctp_uc_swap us;
1675 int nfd;
1676 struct file *nfp;
1677 struct vnode *nvp = NULL;
1678 struct sockparams *sp;
1679
1680 dprint(2, ("sctppeeloff %p\n", (void *)ss));
1681
1682 if (so->so_type != SOCK_SEQPACKET) {
1683 return (EOPNOTSUPP);
1684 }
1685 if (so_copyin((void *)arg, &intval, sizeof (intval),
1686 (mode & (int)FKIOCTL))) {
1687 return (EFAULT);
1688 }
1689 if (intval == 0) {
1690 return (EINVAL);
1691 }
1692
1693 /*
1694 * Find sockparams. This is different from parent's entry,
1695 * as the socket type is different.
1696 */
1697 error = solookup(so->so_family, SOCK_STREAM, so->so_protocol,
1698 &sp);
1699 if (error != 0)
1700 return (error);
1701
1702 /*
1703 * Allocate the user fd.
1704 */
1705 if ((nfd = ufalloc(0)) == -1) {
1706 eprintsoline(so, EMFILE);
1707 SOCKPARAMS_DEC_REF(sp);
1708 return (EMFILE);
1709 }
1710
1711 /*
1712 * Copy the fd out.
1713 */
1714 if (so_copyout(&nfd, (void *)arg, sizeof (nfd),
1715 (mode & (int)FKIOCTL))) {
1716 error = EFAULT;
1717 goto err;
1718 }
1719 mutex_enter(&so->so_lock);
1720
1721 /*
1722 * Don't use sosctp_assoc() in order to peel off disconnected
1723 * associations.
1724 */
1725 ssa = ((uint32_t)intval >= ss->ss_maxassoc) ? NULL :
1726 ss->ss_assocs[intval].ssi_assoc;
1727 if (ssa == NULL) {
1728 mutex_exit(&so->so_lock);
1729 error = EINVAL;
1730 goto err;
1731 }
1732 SSA_REFHOLD(ssa);
1733
1734 nso = socksctp_create(sp, so->so_family, SOCK_STREAM,
1735 so->so_protocol, so->so_version, SOCKET_NOSLEEP,
1736 &error, cr);
1737 if (nso == NULL) {
1738 SSA_REFRELE(ss, ssa);
1739 mutex_exit(&so->so_lock);
1740 goto err;
1741 }
1742 nvp = SOTOV(nso);
1743 so_lock_single(so);
1744 mutex_exit(&so->so_lock);
1745
1746 /* cannot fail, only inheriting properties */
1747 (void) sosctp_init(nso, so, CRED(), 0);
1748
1749 /*
1750 * We have a single ref on the new socket. This is normally
1751 * handled by socket_{create,newconn}, but since they are not
1752 * used we have to do it here.
1753 */
1754 nso->so_count = 1;
1755
1756 us.sus_handle = nso;
1757 us.sus_upcalls = &sosctp_sock_upcalls;
1758
1759 /*
1760 * Upcalls to new socket are blocked for the duration of
1761 * downcall.
1762 */
1763 mutex_enter(&nso->so_lock);
1764
1765 error = sctp_set_opt((struct sctp_s *)ssa->ssa_conn,
1766 IPPROTO_SCTP, SCTP_UC_SWAP, &us, sizeof (us));
1767 if (error) {
1768 goto peelerr;
1769 }
1770 error = falloc(nvp, FWRITE|FREAD, &nfp, NULL);
1771 if (error) {
1772 goto peelerr;
1773 }
1774
1775 /*
1776 * fill in the entries that falloc reserved
1777 */
1778 nfp->f_vnode = nvp;
1779 mutex_exit(&nfp->f_tlock);
1780 setf(nfd, nfp);
1781
1782 mutex_enter(&so->so_lock);
1783
1784 sosctp_assoc_move(ss, SOTOSSO(nso), ssa);
1785
1786 mutex_exit(&nso->so_lock);
1787
1788 ssa->ssa_conn = NULL;
1789 sosctp_assoc_free(ss, ssa);
1790
1791 so_unlock_single(so, SOLOCKED);
1792 mutex_exit(&so->so_lock);
1793
1794 return (0);
1795
1796 err:
1797 SOCKPARAMS_DEC_REF(sp);
1798 setf(nfd, NULL);
1799 eprintsoline(so, error);
1800 return (error);
1801
1802 peelerr:
1803 mutex_exit(&nso->so_lock);
1804 mutex_enter(&so->so_lock);
1805 ASSERT(nso->so_count == 1);
1806 nso->so_count = 0;
1807 so_unlock_single(so, SOLOCKED);
1808 SSA_REFRELE(ss, ssa);
1809 mutex_exit(&so->so_lock);
1810
1811 setf(nfd, NULL);
1812 ASSERT(nvp->v_count == 1);
1813 socket_destroy(nso);
1814 eprintsoline(so, error);
1815 return (error);
1816 }
1817 default:
1818 return (EINVAL);
1819 }
1820 }
1821
1822 /*ARGSUSED*/
1823 static int
sosctp_close(struct sonode * so,int flag,struct cred * cr)1824 sosctp_close(struct sonode *so, int flag, struct cred *cr)
1825 {
1826 struct sctp_sonode *ss;
1827 struct sctp_sa_id *ssi;
1828 struct sctp_soassoc *ssa;
1829 int32_t i;
1830
1831 ss = SOTOSSO(so);
1832
1833 /*
1834 * Initiate connection shutdown. Tell SCTP if there is any data
1835 * left unread.
1836 */
1837 sctp_recvd((struct sctp_s *)so->so_proto_handle,
1838 so->so_rcvbuf - so->so_rcv_queued);
1839 (void) sctp_disconnect((struct sctp_s *)so->so_proto_handle);
1840
1841 /*
1842 * New associations can't come in, but old ones might get
1843 * closed in upcall. Protect against that by taking a reference
1844 * on the association.
1845 */
1846 mutex_enter(&so->so_lock);
1847 ssi = ss->ss_assocs;
1848 for (i = 0; i < ss->ss_maxassoc; i++, ssi++) {
1849 if ((ssa = ssi->ssi_assoc) != NULL) {
1850 SSA_REFHOLD(ssa);
1851 sosctp_assoc_isdisconnected(ssa, 0);
1852 mutex_exit(&so->so_lock);
1853
1854 sctp_recvd(ssa->ssa_conn, so->so_rcvbuf -
1855 ssa->ssa_rcv_queued);
1856 (void) sctp_disconnect(ssa->ssa_conn);
1857
1858 mutex_enter(&so->so_lock);
1859 SSA_REFRELE(ss, ssa);
1860 }
1861 }
1862 mutex_exit(&so->so_lock);
1863
1864 return (0);
1865 }
1866
1867 /*
1868 * Closes incoming connections which were never accepted, frees
1869 * resources.
1870 */
1871 /* ARGSUSED */
1872 void
sosctp_fini(struct sonode * so,struct cred * cr)1873 sosctp_fini(struct sonode *so, struct cred *cr)
1874 {
1875 struct sctp_sonode *ss;
1876 struct sctp_sa_id *ssi;
1877 struct sctp_soassoc *ssa;
1878 int32_t i;
1879
1880 ss = SOTOSSO(so);
1881
1882 ASSERT(so->so_ops == &sosctp_sonodeops ||
1883 so->so_ops == &sosctp_seq_sonodeops);
1884
1885 /* We are the sole owner of so now */
1886 mutex_enter(&so->so_lock);
1887
1888 /* Free all pending connections */
1889 so_acceptq_flush(so, B_TRUE);
1890
1891 ssi = ss->ss_assocs;
1892 for (i = 0; i < ss->ss_maxassoc; i++, ssi++) {
1893 if ((ssa = ssi->ssi_assoc) != NULL) {
1894 SSA_REFHOLD(ssa);
1895 mutex_exit(&so->so_lock);
1896
1897 sctp_close((struct sctp_s *)ssa->ssa_conn);
1898
1899 mutex_enter(&so->so_lock);
1900 ssa->ssa_conn = NULL;
1901 sosctp_assoc_free(ss, ssa);
1902 }
1903 }
1904 if (ss->ss_assocs != NULL) {
1905 ASSERT(ss->ss_assoccnt == 0);
1906 kmem_free(ss->ss_assocs,
1907 ss->ss_maxassoc * sizeof (struct sctp_sa_id));
1908 }
1909 mutex_exit(&so->so_lock);
1910
1911 if (so->so_proto_handle)
1912 sctp_close((struct sctp_s *)so->so_proto_handle);
1913 so->so_proto_handle = NULL;
1914
1915 /*
1916 * Note until sctp_close() is called, SCTP can still send up
1917 * messages, such as event notifications. So we should flush
1918 * the recevie buffer after calling sctp_close().
1919 */
1920 mutex_enter(&so->so_lock);
1921 so_rcv_flush(so);
1922 mutex_exit(&so->so_lock);
1923
1924 sonode_fini(so);
1925 }
1926
1927 /*
1928 * Upcalls from SCTP
1929 */
1930
1931 /*
1932 * This is the upcall function for 1-N (SOCK_SEQPACKET) socket when a new
1933 * association is created. Note that the first argument (handle) is of type
1934 * sctp_sonode *, which is the one changed to a listener for new
1935 * associations. All the other upcalls for 1-N socket take sctp_soassoc *
1936 * as handle. The only exception is the su_properties upcall, which
1937 * can take both types as handle.
1938 */
1939 /* ARGSUSED */
1940 sock_upper_handle_t
sctp_assoc_newconn(sock_upper_handle_t parenthandle,sock_lower_handle_t connind,sock_downcalls_t * dc,struct cred * peer_cred,pid_t peer_cpid,sock_upcalls_t ** ucp)1941 sctp_assoc_newconn(sock_upper_handle_t parenthandle,
1942 sock_lower_handle_t connind, sock_downcalls_t *dc,
1943 struct cred *peer_cred, pid_t peer_cpid, sock_upcalls_t **ucp)
1944 {
1945 struct sctp_sonode *lss = (struct sctp_sonode *)parenthandle;
1946 struct sonode *lso = &lss->ss_so;
1947 struct sctp_soassoc *ssa;
1948 sctp_assoc_t id;
1949
1950 ASSERT(lss->ss_type == SOSCTP_SOCKET);
1951 ASSERT(lso->so_state & SS_ACCEPTCONN);
1952 ASSERT(lso->so_proto_handle != NULL); /* closed conn */
1953 ASSERT(lso->so_type == SOCK_SEQPACKET);
1954
1955 mutex_enter(&lso->so_lock);
1956
1957 if ((id = sosctp_aid_get(lss)) == -1) {
1958 /*
1959 * Array not large enough; increase size.
1960 */
1961 if (sosctp_aid_grow(lss, lss->ss_maxassoc, KM_NOSLEEP) < 0) {
1962 mutex_exit(&lso->so_lock);
1963 return (NULL);
1964 }
1965 id = sosctp_aid_get(lss);
1966 ASSERT(id != -1);
1967 }
1968
1969 /*
1970 * Create soassoc for this connection
1971 */
1972 ssa = sosctp_assoc_create(lss, KM_NOSLEEP);
1973 if (ssa == NULL) {
1974 mutex_exit(&lso->so_lock);
1975 return (NULL);
1976 }
1977 sosctp_aid_reserve(lss, id, 1);
1978 lss->ss_assocs[id].ssi_assoc = ssa;
1979 ++lss->ss_assoccnt;
1980 ssa->ssa_id = id;
1981 ssa->ssa_conn = (struct sctp_s *)connind;
1982 ssa->ssa_state = (SS_ISBOUND | SS_ISCONNECTED);
1983 ssa->ssa_wroff = lss->ss_wroff;
1984 ssa->ssa_wrsize = lss->ss_wrsize;
1985
1986 mutex_exit(&lso->so_lock);
1987
1988 *ucp = &sosctp_assoc_upcalls;
1989
1990 return ((sock_upper_handle_t)ssa);
1991 }
1992
1993 /* ARGSUSED */
1994 static void
sctp_assoc_connected(sock_upper_handle_t handle,sock_connid_t id,struct cred * peer_cred,pid_t peer_cpid)1995 sctp_assoc_connected(sock_upper_handle_t handle, sock_connid_t id,
1996 struct cred *peer_cred, pid_t peer_cpid)
1997 {
1998 struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
1999 struct sonode *so = &ssa->ssa_sonode->ss_so;
2000
2001 ASSERT(so->so_type == SOCK_SEQPACKET);
2002 ASSERT(ssa->ssa_conn);
2003
2004 mutex_enter(&so->so_lock);
2005 sosctp_assoc_isconnected(ssa);
2006 mutex_exit(&so->so_lock);
2007 }
2008
2009 /* ARGSUSED */
2010 static int
sctp_assoc_disconnected(sock_upper_handle_t handle,sock_connid_t id,int error)2011 sctp_assoc_disconnected(sock_upper_handle_t handle, sock_connid_t id, int error)
2012 {
2013 struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2014 struct sonode *so = &ssa->ssa_sonode->ss_so;
2015 int ret;
2016
2017 ASSERT(so->so_type == SOCK_SEQPACKET);
2018 ASSERT(ssa->ssa_conn != NULL);
2019
2020 mutex_enter(&so->so_lock);
2021 sosctp_assoc_isdisconnected(ssa, error);
2022 if (ssa->ssa_refcnt == 1) {
2023 ret = 1;
2024 ssa->ssa_conn = NULL;
2025 } else {
2026 ret = 0;
2027 }
2028 SSA_REFRELE(SOTOSSO(so), ssa);
2029
2030 cv_broadcast(&so->so_snd_cv);
2031
2032 mutex_exit(&so->so_lock);
2033
2034 return (ret);
2035 }
2036
2037 /* ARGSUSED */
2038 static void
sctp_assoc_disconnecting(sock_upper_handle_t handle,sock_opctl_action_t action,uintptr_t arg)2039 sctp_assoc_disconnecting(sock_upper_handle_t handle, sock_opctl_action_t action,
2040 uintptr_t arg)
2041 {
2042 struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2043 struct sonode *so = &ssa->ssa_sonode->ss_so;
2044
2045 ASSERT(so->so_type == SOCK_SEQPACKET);
2046 ASSERT(ssa->ssa_conn != NULL);
2047 ASSERT(action == SOCK_OPCTL_SHUT_SEND);
2048
2049 mutex_enter(&so->so_lock);
2050 sosctp_assoc_isdisconnecting(ssa);
2051 mutex_exit(&so->so_lock);
2052 }
2053
2054 /* ARGSUSED */
2055 static ssize_t
sctp_assoc_recv(sock_upper_handle_t handle,mblk_t * mp,size_t len,int flags,int * errorp,boolean_t * forcepush)2056 sctp_assoc_recv(sock_upper_handle_t handle, mblk_t *mp, size_t len, int flags,
2057 int *errorp, boolean_t *forcepush)
2058 {
2059 struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2060 struct sctp_sonode *ss = ssa->ssa_sonode;
2061 struct sonode *so = &ss->ss_so;
2062 struct T_unitdata_ind *tind;
2063 mblk_t *mp2;
2064 union sctp_notification *sn;
2065 struct sctp_sndrcvinfo *sinfo;
2066 ssize_t space_available;
2067
2068 ASSERT(ssa->ssa_type == SOSCTP_ASSOC);
2069 ASSERT(so->so_type == SOCK_SEQPACKET);
2070 ASSERT(ssa->ssa_conn != NULL); /* closed conn */
2071 ASSERT(mp != NULL);
2072
2073 ASSERT(errorp != NULL);
2074 *errorp = 0;
2075
2076 /*
2077 * Should be getting T_unitdata_req's only.
2078 * Must have address as part of packet.
2079 */
2080 tind = (struct T_unitdata_ind *)mp->b_rptr;
2081 ASSERT((DB_TYPE(mp) == M_PROTO) &&
2082 (tind->PRIM_type == T_UNITDATA_IND));
2083 ASSERT(tind->SRC_length);
2084
2085 mutex_enter(&so->so_lock);
2086
2087 /*
2088 * For notify messages, need to fill in association id.
2089 * For data messages, sndrcvinfo could be in ancillary data.
2090 */
2091 if (mp->b_flag & SCTP_NOTIFICATION) {
2092 mp2 = mp->b_cont;
2093 sn = (union sctp_notification *)mp2->b_rptr;
2094 switch (sn->sn_header.sn_type) {
2095 case SCTP_ASSOC_CHANGE:
2096 sn->sn_assoc_change.sac_assoc_id = ssa->ssa_id;
2097 break;
2098 case SCTP_PEER_ADDR_CHANGE:
2099 sn->sn_paddr_change.spc_assoc_id = ssa->ssa_id;
2100 break;
2101 case SCTP_REMOTE_ERROR:
2102 sn->sn_remote_error.sre_assoc_id = ssa->ssa_id;
2103 break;
2104 case SCTP_SEND_FAILED:
2105 sn->sn_send_failed.ssf_assoc_id = ssa->ssa_id;
2106 break;
2107 case SCTP_SHUTDOWN_EVENT:
2108 sn->sn_shutdown_event.sse_assoc_id = ssa->ssa_id;
2109 break;
2110 case SCTP_ADAPTATION_INDICATION:
2111 sn->sn_adaptation_event.sai_assoc_id = ssa->ssa_id;
2112 break;
2113 case SCTP_PARTIAL_DELIVERY_EVENT:
2114 sn->sn_pdapi_event.pdapi_assoc_id = ssa->ssa_id;
2115 break;
2116 default:
2117 ASSERT(0);
2118 break;
2119 }
2120 } else {
2121 if (tind->OPT_length > 0) {
2122 struct cmsghdr *cmsg;
2123 char *cend;
2124
2125 cmsg = (struct cmsghdr *)
2126 ((uchar_t *)mp->b_rptr + tind->OPT_offset);
2127 cend = (char *)cmsg + tind->OPT_length;
2128 for (;;) {
2129 if ((char *)(cmsg + 1) > cend ||
2130 ((char *)cmsg + cmsg->cmsg_len) > cend) {
2131 break;
2132 }
2133 if ((cmsg->cmsg_level == IPPROTO_SCTP) &&
2134 (cmsg->cmsg_type == SCTP_SNDRCV)) {
2135 sinfo = (struct sctp_sndrcvinfo *)
2136 (cmsg + 1);
2137 sinfo->sinfo_assoc_id = ssa->ssa_id;
2138 break;
2139 }
2140 if (cmsg->cmsg_len > 0) {
2141 cmsg = (struct cmsghdr *)
2142 ((uchar_t *)cmsg + cmsg->cmsg_len);
2143 } else {
2144 break;
2145 }
2146 }
2147 }
2148 }
2149
2150 /*
2151 * SCTP has reserved space in the header for storing a pointer.
2152 * Put the pointer to assocation there, and queue the data.
2153 */
2154 SSA_REFHOLD(ssa);
2155 ASSERT((mp->b_rptr - DB_BASE(mp)) >= sizeof (ssa));
2156 *(struct sctp_soassoc **)DB_BASE(mp) = ssa;
2157
2158 ssa->ssa_rcv_queued += len;
2159 space_available = so->so_rcvbuf - ssa->ssa_rcv_queued;
2160 if (space_available <= 0)
2161 ssa->ssa_flowctrld = B_TRUE;
2162
2163 so_enqueue_msg(so, mp, len);
2164
2165 /* so_notify_data drops so_lock */
2166 so_notify_data(so, len);
2167
2168 return (space_available);
2169 }
2170
2171 static void
sctp_assoc_xmitted(sock_upper_handle_t handle,boolean_t qfull)2172 sctp_assoc_xmitted(sock_upper_handle_t handle, boolean_t qfull)
2173 {
2174 struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2175 struct sctp_sonode *ss = ssa->ssa_sonode;
2176
2177 ASSERT(ssa->ssa_type == SOSCTP_ASSOC);
2178 ASSERT(ss->ss_so.so_type == SOCK_SEQPACKET);
2179 ASSERT(ssa->ssa_conn != NULL);
2180
2181 mutex_enter(&ss->ss_so.so_lock);
2182
2183 ssa->ssa_snd_qfull = qfull;
2184
2185 /*
2186 * Wake blocked writers.
2187 */
2188 cv_broadcast(&ss->ss_so.so_snd_cv);
2189
2190 mutex_exit(&ss->ss_so.so_lock);
2191 }
2192
2193 static vnode_t *
sctp_assoc_get_vnode(sock_upper_handle_t handle)2194 sctp_assoc_get_vnode(sock_upper_handle_t handle)
2195 {
2196 struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2197 struct sonode *so;
2198
2199 if (ssa->ssa_type == SOSCTP_ASSOC)
2200 so = &ssa->ssa_sonode->ss_so;
2201 else
2202 so = &((struct sctp_sonode *)handle)->ss_so;
2203
2204 return (so_get_vnode((sock_upper_handle_t)so));
2205 }
2206
2207 static void
sctp_assoc_properties(sock_upper_handle_t handle,struct sock_proto_props * soppp)2208 sctp_assoc_properties(sock_upper_handle_t handle,
2209 struct sock_proto_props *soppp)
2210 {
2211 struct sctp_soassoc *ssa = (struct sctp_soassoc *)handle;
2212 struct sonode *so;
2213
2214 if (ssa->ssa_type == SOSCTP_ASSOC) {
2215 so = &ssa->ssa_sonode->ss_so;
2216
2217 mutex_enter(&so->so_lock);
2218
2219 /* Per assoc_id properties. */
2220 if (soppp->sopp_flags & SOCKOPT_WROFF)
2221 ssa->ssa_wroff = soppp->sopp_wroff;
2222 if (soppp->sopp_flags & SOCKOPT_MAXBLK)
2223 ssa->ssa_wrsize = soppp->sopp_maxblk;
2224 } else {
2225 so = &((struct sctp_sonode *)handle)->ss_so;
2226 mutex_enter(&so->so_lock);
2227
2228 if (soppp->sopp_flags & SOCKOPT_WROFF)
2229 so->so_proto_props.sopp_wroff = soppp->sopp_wroff;
2230 if (soppp->sopp_flags & SOCKOPT_MAXBLK)
2231 so->so_proto_props.sopp_maxblk = soppp->sopp_maxblk;
2232 if (soppp->sopp_flags & SOCKOPT_RCVHIWAT) {
2233 ssize_t lowat;
2234
2235 so->so_rcvbuf = soppp->sopp_rxhiwat;
2236 /*
2237 * The low water mark should be adjusted properly
2238 * if the high water mark is changed. It should
2239 * not be bigger than 1/4 of high water mark.
2240 */
2241 lowat = soppp->sopp_rxhiwat >> 2;
2242 if (so->so_rcvlowat > lowat) {
2243 /* Sanity check... */
2244 if (lowat == 0)
2245 so->so_rcvlowat = soppp->sopp_rxhiwat;
2246 else
2247 so->so_rcvlowat = lowat;
2248 }
2249 }
2250 }
2251 mutex_exit(&so->so_lock);
2252 }
2253