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