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