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