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