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