17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5558fbd03Skcpoon  * Common Development and Distribution License (the "License").
6558fbd03Skcpoon  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*bd670b35SErik Nordmark  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/systm.h>
287c478bd9Sstevel@tonic-gate #include <sys/stream.h>
297c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
307c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
337c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
347c478bd9Sstevel@tonic-gate #include <sys/socket.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <netinet/in.h>
377c478bd9Sstevel@tonic-gate #include <netinet/sctp.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <inet/common.h>
400f1702c5SYu Xiangning #include <inet/ipclassifier.h>
417c478bd9Sstevel@tonic-gate #include <inet/ip.h>
420f1702c5SYu Xiangning 
437c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
447c478bd9Sstevel@tonic-gate 
450f1702c5SYu Xiangning /* ARGSUSED */
467c478bd9Sstevel@tonic-gate static void
477c478bd9Sstevel@tonic-gate sctp_notify(sctp_t *sctp, mblk_t *emp, size_t len)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	struct T_unitdata_ind *tudi;
507c478bd9Sstevel@tonic-gate 	mblk_t *mp;
517c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
527c478bd9Sstevel@tonic-gate 	int32_t rwnd = 0;
530f1702c5SYu Xiangning 	int error;
54*bd670b35SErik Nordmark 	conn_t *connp = sctp->sctp_connp;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*tudi) + sizeof (void *) +
577c478bd9Sstevel@tonic-gate 		sizeof (struct sockaddr_in6), BPRI_HI)) == NULL) {
587c478bd9Sstevel@tonic-gate 		/* XXX trouble: don't want to drop events. should queue it. */
597c478bd9Sstevel@tonic-gate 		freemsg(emp);
607c478bd9Sstevel@tonic-gate 		return;
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 	dprint(3, ("sctp_notify: event %d\n", (*(uint16_t *)emp->b_rptr)));
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
657c478bd9Sstevel@tonic-gate 	mp->b_flag |= MSGMARK;
667c478bd9Sstevel@tonic-gate 	mp->b_rptr += sizeof (void *); /* pointer worth of padding */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
697c478bd9Sstevel@tonic-gate 	tudi->PRIM_type = T_UNITDATA_IND;
707c478bd9Sstevel@tonic-gate 	tudi->SRC_offset = sizeof (*tudi);
717c478bd9Sstevel@tonic-gate 	tudi->OPT_length = 0;
727c478bd9Sstevel@tonic-gate 	tudi->OPT_offset = 0;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	fp = sctp->sctp_primary;
757c478bd9Sstevel@tonic-gate 	ASSERT(fp);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	/*
787c478bd9Sstevel@tonic-gate 	 * Fill in primary remote address.
797c478bd9Sstevel@tonic-gate 	 */
807c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
817c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 		tudi->SRC_length = sizeof (*sin4);
847c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)(tudi + 1);
857c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
86*bd670b35SErik Nordmark 		sin4->sin_port = connp->conn_fport;
877c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr, sin4->sin_addr.s_addr);
887c478bd9Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)(sin4 + 1);
897c478bd9Sstevel@tonic-gate 	} else {
907c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 		tudi->SRC_length = sizeof (*sin6);
937c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)(tudi + 1);
947c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
95*bd670b35SErik Nordmark 		sin6->sin6_port = connp->conn_fport;
967c478bd9Sstevel@tonic-gate 		sin6->sin6_addr = fp->faddr;
977c478bd9Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)(sin6 + 1);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	mp->b_cont = emp;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	/*
1037c478bd9Sstevel@tonic-gate 	 * Notifications are queued regardless of socket rx space.  So
1047c478bd9Sstevel@tonic-gate 	 * we do not decrement sctp_rwnd here as this will confuse the
1057c478bd9Sstevel@tonic-gate 	 * other side.
1067c478bd9Sstevel@tonic-gate 	 */
1077c478bd9Sstevel@tonic-gate #ifdef DEBUG
1087c478bd9Sstevel@tonic-gate 	for (emp = mp->b_cont; emp; emp = emp->b_cont) {
1097c478bd9Sstevel@tonic-gate 		rwnd += emp->b_wptr - emp->b_rptr;
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 	ASSERT(len == rwnd);
1127c478bd9Sstevel@tonic-gate #endif
1137c478bd9Sstevel@tonic-gate 
1140f1702c5SYu Xiangning 	/*
1150f1702c5SYu Xiangning 	 * Override b_flag for SCTP sockfs internal use
1160f1702c5SYu Xiangning 	 */
1170f1702c5SYu Xiangning 	mp->b_flag = (short)SCTP_NOTIFICATION;
1180f1702c5SYu Xiangning 
1190f1702c5SYu Xiangning 	rwnd = sctp->sctp_ulp_recv(sctp->sctp_ulpd, mp, msgdsize(mp), 0,
1200f1702c5SYu Xiangning 	    &error, NULL);
1217c478bd9Sstevel@tonic-gate 	if (rwnd > sctp->sctp_rwnd) {
1227c478bd9Sstevel@tonic-gate 		sctp->sctp_rwnd = rwnd;
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate void
1277c478bd9Sstevel@tonic-gate sctp_assoc_event(sctp_t *sctp, uint16_t state, uint16_t error,
1287c478bd9Sstevel@tonic-gate     sctp_chunk_hdr_t *ch)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	struct sctp_assoc_change *sacp;
1317c478bd9Sstevel@tonic-gate 	mblk_t *mp;
1327c478bd9Sstevel@tonic-gate 	uint16_t ch_len;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvassocevnt) {
1357c478bd9Sstevel@tonic-gate 		return;
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	ch_len = (ch != NULL) ? ntohs(ch->sch_len) : 0;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*sacp) + ch_len, BPRI_MED)) == NULL) {
1417c478bd9Sstevel@tonic-gate 		return;
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	sacp = (struct sctp_assoc_change *)mp->b_rptr;
1457c478bd9Sstevel@tonic-gate 	sacp->sac_type = SCTP_ASSOC_CHANGE;
1467c478bd9Sstevel@tonic-gate 	sacp->sac_flags = sctp->sctp_prsctp_aware ? SCTP_PRSCTP_CAPABLE : 0;
1477c478bd9Sstevel@tonic-gate 	sacp->sac_length = sizeof (*sacp) + ch_len;
1487c478bd9Sstevel@tonic-gate 	sacp->sac_state = state;
1497c478bd9Sstevel@tonic-gate 	sacp->sac_error = error;
1507c478bd9Sstevel@tonic-gate 	sacp->sac_outbound_streams = sctp->sctp_num_ostr;
1517c478bd9Sstevel@tonic-gate 	sacp->sac_inbound_streams = sctp->sctp_num_istr;
1527c478bd9Sstevel@tonic-gate 	sacp->sac_assoc_id = 0;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (ch != NULL)
1557c478bd9Sstevel@tonic-gate 		bcopy(ch, sacp + 1, ch_len);
1567c478bd9Sstevel@tonic-gate 	mp->b_wptr += sacp->sac_length;
1577c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, sacp->sac_length);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Send failure event. Message is expected to have message header still
1627c478bd9Sstevel@tonic-gate  * in place, data follows in subsequent mblk's.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate static void
1657c478bd9Sstevel@tonic-gate sctp_sendfail(sctp_t *sctp, mblk_t *msghdr, uint16_t flags, int error)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	struct sctp_send_failed *sfp;
1687c478bd9Sstevel@tonic-gate 	mblk_t *mp;
1697c478bd9Sstevel@tonic-gate 	sctp_msg_hdr_t *smh;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/* Allocate a mblk for the notification header */
1727c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*sfp), BPRI_MED)) == NULL) {
1737c478bd9Sstevel@tonic-gate 		/* give up */
1747c478bd9Sstevel@tonic-gate 		freemsg(msghdr);
1757c478bd9Sstevel@tonic-gate 		return;
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	smh = (sctp_msg_hdr_t *)msghdr->b_rptr;
1797c478bd9Sstevel@tonic-gate 	sfp = (struct sctp_send_failed *)mp->b_rptr;
1807c478bd9Sstevel@tonic-gate 	sfp->ssf_type = SCTP_SEND_FAILED;
1817c478bd9Sstevel@tonic-gate 	sfp->ssf_flags = flags;
1827c478bd9Sstevel@tonic-gate 	sfp->ssf_length = smh->smh_msglen + sizeof (*sfp);
1837c478bd9Sstevel@tonic-gate 	sfp->ssf_error = error;
1847c478bd9Sstevel@tonic-gate 	sfp->ssf_assoc_id = 0;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	bzero(&sfp->ssf_info, sizeof (sfp->ssf_info));
1877c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_stream = smh->smh_sid;
1887c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_flags = smh->smh_flags;
1897c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_ppid = smh->smh_ppid;
1907c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_context = smh->smh_context;
1917c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_timetolive = TICK_TO_MSEC(smh->smh_ttl);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(sfp + 1);
1947c478bd9Sstevel@tonic-gate 	mp->b_cont = msghdr->b_cont;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	freeb(msghdr);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, sfp->ssf_length);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * Send failure when the message has been fully chunkified.
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate static void
2067c478bd9Sstevel@tonic-gate sctp_sendfail_sent(sctp_t *sctp, mblk_t *meta, int error)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
2097c478bd9Sstevel@tonic-gate 	mblk_t		*nmp;
2107c478bd9Sstevel@tonic-gate 	mblk_t		*tail;
2117c478bd9Sstevel@tonic-gate 	uint16_t	flags = SCTP_DATA_SENT;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvsendfailevnt) {
2147c478bd9Sstevel@tonic-gate 		sctp_free_msg(meta);
2157c478bd9Sstevel@tonic-gate 		return;
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * We need to remove all data_hdr's.
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 	nmp = meta->b_cont;
2227c478bd9Sstevel@tonic-gate 	tail = meta;
2237c478bd9Sstevel@tonic-gate 	do {
2247c478bd9Sstevel@tonic-gate 		mp = nmp->b_next;
2257c478bd9Sstevel@tonic-gate 		nmp->b_next = NULL;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		/*
2287c478bd9Sstevel@tonic-gate 		 * If one of the chunks hasn't been sent yet, say that
2297c478bd9Sstevel@tonic-gate 		 * the message hasn't been sent.
2307c478bd9Sstevel@tonic-gate 		 */
2317c478bd9Sstevel@tonic-gate 		if (!SCTP_CHUNK_ISSENT(nmp)) {
2327c478bd9Sstevel@tonic-gate 			flags = SCTP_DATA_UNSENT;
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		nmp->b_rptr += sizeof (sctp_data_hdr_t);
2357c478bd9Sstevel@tonic-gate 		if (nmp->b_rptr == nmp->b_wptr) {
2367c478bd9Sstevel@tonic-gate 			tail->b_cont = nmp->b_cont;
2377c478bd9Sstevel@tonic-gate 			freeb(nmp);
2387c478bd9Sstevel@tonic-gate 		} else {
2397c478bd9Sstevel@tonic-gate 			tail->b_cont = nmp;
2407c478bd9Sstevel@tonic-gate 		}
2417c478bd9Sstevel@tonic-gate 		while (tail->b_cont) {
2427c478bd9Sstevel@tonic-gate 			tail = tail->b_cont;
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 	} while ((nmp = mp) != NULL);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	sctp_sendfail(sctp, meta, flags, error);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * Send failure when the message hasn't been fully chunkified.
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate void
2537c478bd9Sstevel@tonic-gate sctp_sendfail_event(sctp_t *sctp, mblk_t *meta, int error, boolean_t chunkified)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
2567c478bd9Sstevel@tonic-gate 	mblk_t	*nmp;
2577c478bd9Sstevel@tonic-gate 	mblk_t	*tail;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if (meta == NULL)
2607c478bd9Sstevel@tonic-gate 		return;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvsendfailevnt) {
2637c478bd9Sstevel@tonic-gate 		sctp_free_msg(meta);
2647c478bd9Sstevel@tonic-gate 		return;
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/* If the message is fully chunkified */
2687c478bd9Sstevel@tonic-gate 	if (chunkified) {
2697c478bd9Sstevel@tonic-gate 		sctp_sendfail_sent(sctp, meta, error);
2707c478bd9Sstevel@tonic-gate 		return;
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate 	 * Message might be partially chunkified, we need to remove
2747c478bd9Sstevel@tonic-gate 	 * all data_hdr's.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	mp = meta->b_cont;
2777c478bd9Sstevel@tonic-gate 	tail = meta;
2787c478bd9Sstevel@tonic-gate 	while ((nmp = mp->b_next) != NULL) {
2797c478bd9Sstevel@tonic-gate 		mp->b_next = nmp->b_next;
2807c478bd9Sstevel@tonic-gate 		nmp->b_next = NULL;
2817c478bd9Sstevel@tonic-gate 		nmp->b_rptr += sizeof (sctp_data_hdr_t);
2827c478bd9Sstevel@tonic-gate 		if (nmp->b_rptr == nmp->b_wptr) {
2837c478bd9Sstevel@tonic-gate 			tail->b_cont = nmp->b_cont;
2847c478bd9Sstevel@tonic-gate 			freeb(nmp);
2857c478bd9Sstevel@tonic-gate 		} else {
2867c478bd9Sstevel@tonic-gate 			tail->b_cont = nmp;
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 		while (tail->b_cont) {
2897c478bd9Sstevel@tonic-gate 			tail = tail->b_cont;
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 	tail->b_cont = mp;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	sctp_sendfail(sctp, meta, SCTP_DATA_UNSENT, error);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate void
2987c478bd9Sstevel@tonic-gate sctp_regift_xmitlist(sctp_t *sctp)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	mblk_t *mp;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvsendfailevnt) {
3037c478bd9Sstevel@tonic-gate 		return;
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_xmit_head) != NULL) {
3077c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_head = mp->b_next;
3087c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
3097c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_head != NULL)
3107c478bd9Sstevel@tonic-gate 			sctp->sctp_xmit_head->b_prev = NULL;
3117c478bd9Sstevel@tonic-gate 		sctp_sendfail_event(sctp, mp, 0, B_TRUE);
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_xmit_unsent) != NULL) {
3147c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_unsent = mp->b_next;
3157c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
3167c478bd9Sstevel@tonic-gate 		sctp_sendfail_event(sctp, mp, 0, B_FALSE);
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_tail = sctp->sctp_xmit_unsent_tail = NULL;
3197c478bd9Sstevel@tonic-gate 	sctp->sctp_unacked = sctp->sctp_unsent = 0;
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate void
3237c478bd9Sstevel@tonic-gate sctp_intf_event(sctp_t *sctp, in6_addr_t addr, int state, int error)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	struct sctp_paddr_change *spc;
3267c478bd9Sstevel@tonic-gate 	ipaddr_t addr4;
3277c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
3287c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
3297c478bd9Sstevel@tonic-gate 	mblk_t *mp;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvpathevnt) {
3327c478bd9Sstevel@tonic-gate 		return;
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*spc), BPRI_MED)) == NULL) {
3367c478bd9Sstevel@tonic-gate 		return;
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	spc = (struct sctp_paddr_change *)mp->b_rptr;
3407c478bd9Sstevel@tonic-gate 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
3417c478bd9Sstevel@tonic-gate 	spc->spc_flags = 0;
3427c478bd9Sstevel@tonic-gate 	spc->spc_length = sizeof (*spc);
3437c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&addr)) {
3447c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&addr, addr4);
3457c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&spc->spc_aaddr;
3467c478bd9Sstevel@tonic-gate 		sin->sin_family = AF_INET;
3477c478bd9Sstevel@tonic-gate 		sin->sin_port = 0;
3487c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = addr4;
3497c478bd9Sstevel@tonic-gate 	} else {
3507c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
3517c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
3527c478bd9Sstevel@tonic-gate 		sin6->sin6_port = 0;
3537c478bd9Sstevel@tonic-gate 		sin6->sin6_addr = addr;
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 	spc->spc_state = state;
3567c478bd9Sstevel@tonic-gate 	spc->spc_error = error;
3577c478bd9Sstevel@tonic-gate 	spc->spc_assoc_id = 0;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(spc + 1);
3607c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, spc->spc_length);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate void
3647c478bd9Sstevel@tonic-gate sctp_error_event(sctp_t *sctp, sctp_chunk_hdr_t *ch)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	struct sctp_remote_error *sre;
3677c478bd9Sstevel@tonic-gate 	mblk_t *mp;
3687c478bd9Sstevel@tonic-gate 	size_t len;
3697c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t *errh;
3707c478bd9Sstevel@tonic-gate 	uint16_t dlen = 0;
3717c478bd9Sstevel@tonic-gate 	uint16_t error = 0;
3727c478bd9Sstevel@tonic-gate 	void *dtail = NULL;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvpeererr) {
3757c478bd9Sstevel@tonic-gate 		return;
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	if (ntohs(ch->sch_len) > sizeof (*ch)) {
3797c478bd9Sstevel@tonic-gate 		errh = (sctp_parm_hdr_t *)(ch + 1);
3807c478bd9Sstevel@tonic-gate 		error = ntohs(errh->sph_type);
3817c478bd9Sstevel@tonic-gate 		dlen = ntohs(errh->sph_len) - sizeof (*errh);
3827c478bd9Sstevel@tonic-gate 		if (dlen > 0) {
3837c478bd9Sstevel@tonic-gate 			dtail = errh + 1;
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	len = sizeof (*sre) + dlen;
3887c478bd9Sstevel@tonic-gate 	if ((mp = allocb(len, BPRI_MED)) == NULL) {
3897c478bd9Sstevel@tonic-gate 		return;
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	sre = (struct sctp_remote_error *)mp->b_rptr;
3937c478bd9Sstevel@tonic-gate 	sre->sre_type = SCTP_REMOTE_ERROR;
3947c478bd9Sstevel@tonic-gate 	sre->sre_flags = 0;
3957c478bd9Sstevel@tonic-gate 	sre->sre_length = len;
3967c478bd9Sstevel@tonic-gate 	sre->sre_assoc_id = 0;
3977c478bd9Sstevel@tonic-gate 	sre->sre_error = error;
3987c478bd9Sstevel@tonic-gate 	if (dtail) {
3997c478bd9Sstevel@tonic-gate 		bcopy(dtail, sre + 1, dlen);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + len;
4037c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, len);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate void
4077c478bd9Sstevel@tonic-gate sctp_shutdown_event(sctp_t *sctp)
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	struct sctp_shutdown_event *sse;
4107c478bd9Sstevel@tonic-gate 	mblk_t *mp;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvshutdownevnt) {
4137c478bd9Sstevel@tonic-gate 		return;
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*sse), BPRI_MED)) == NULL) {
4177c478bd9Sstevel@tonic-gate 		return;
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	sse = (struct sctp_shutdown_event *)mp->b_rptr;
4217c478bd9Sstevel@tonic-gate 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
4227c478bd9Sstevel@tonic-gate 	sse->sse_flags = 0;
4237c478bd9Sstevel@tonic-gate 	sse->sse_length = sizeof (*sse);
4247c478bd9Sstevel@tonic-gate 	sse->sse_assoc_id = 0;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(sse + 1);
4277c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, sse->sse_length);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate void
431558fbd03Skcpoon sctp_adaptation_event(sctp_t *sctp)
4327c478bd9Sstevel@tonic-gate {
433558fbd03Skcpoon 	struct sctp_adaptation_event *sai;
4347c478bd9Sstevel@tonic-gate 	mblk_t *mp;
4357c478bd9Sstevel@tonic-gate 
436558fbd03Skcpoon 	if (!sctp->sctp_recvalevnt || !sctp->sctp_recv_adaptation) {
4377c478bd9Sstevel@tonic-gate 		return;
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*sai), BPRI_MED)) == NULL) {
4407c478bd9Sstevel@tonic-gate 		return;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
443558fbd03Skcpoon 	sai = (struct sctp_adaptation_event *)mp->b_rptr;
444558fbd03Skcpoon 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
4457c478bd9Sstevel@tonic-gate 	sai->sai_flags = 0;
4467c478bd9Sstevel@tonic-gate 	sai->sai_length = sizeof (*sai);
4477c478bd9Sstevel@tonic-gate 	sai->sai_assoc_id = 0;
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * Adaptation code delivered in network byte order.
4507c478bd9Sstevel@tonic-gate 	 */
451558fbd03Skcpoon 	sai->sai_adaptation_ind = sctp->sctp_rx_adaptation_code;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(sai + 1);
4547c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, sai->sai_length);
4557c478bd9Sstevel@tonic-gate 
456558fbd03Skcpoon 	sctp->sctp_recv_adaptation = 0; /* in case there's a restart later */
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate /* Send partial deliver event */
4607c478bd9Sstevel@tonic-gate void
4617c478bd9Sstevel@tonic-gate sctp_partial_delivery_event(sctp_t *sctp)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	struct sctp_pdapi_event	*pdapi;
4647c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvpdevnt)
4677c478bd9Sstevel@tonic-gate 		return;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*pdapi), BPRI_MED)) == NULL)
4707c478bd9Sstevel@tonic-gate 		return;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	pdapi = (struct sctp_pdapi_event *)mp->b_rptr;
4737c478bd9Sstevel@tonic-gate 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
4747c478bd9Sstevel@tonic-gate 	pdapi->pdapi_flags = 0;
4757c478bd9Sstevel@tonic-gate 	pdapi->pdapi_length = sizeof (*pdapi);
4767c478bd9Sstevel@tonic-gate 	pdapi->pdapi_indication = SCTP_PARTIAL_DELIVERY_ABORTED;
4777c478bd9Sstevel@tonic-gate 	pdapi->pdapi_assoc_id = 0;
4787c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(pdapi + 1);
4797c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, pdapi->pdapi_length);
4807c478bd9Sstevel@tonic-gate }
481