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*6be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/types.h>
267c478bd9Sstevel@tonic-gate #include <sys/systm.h>
277c478bd9Sstevel@tonic-gate #include <sys/stream.h>
287c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
297c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
307c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
317c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
327c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
337c478bd9Sstevel@tonic-gate #include <sys/socket.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <netinet/in.h>
367c478bd9Sstevel@tonic-gate #include <netinet/sctp.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <inet/common.h>
390f1702c5SYu Xiangning #include <inet/ipclassifier.h>
407c478bd9Sstevel@tonic-gate #include <inet/ip.h>
410f1702c5SYu Xiangning 
427c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
437c478bd9Sstevel@tonic-gate 
440f1702c5SYu Xiangning /* ARGSUSED */
457c478bd9Sstevel@tonic-gate static void
467c478bd9Sstevel@tonic-gate sctp_notify(sctp_t *sctp, mblk_t *emp, size_t len)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate 	struct T_unitdata_ind *tudi;
497c478bd9Sstevel@tonic-gate 	mblk_t *mp;
507c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
517c478bd9Sstevel@tonic-gate 	int32_t rwnd = 0;
520f1702c5SYu Xiangning 	int error;
53bd670b35SErik Nordmark 	conn_t *connp = sctp->sctp_connp;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*tudi) + sizeof (void *) +
567c478bd9Sstevel@tonic-gate 		sizeof (struct sockaddr_in6), BPRI_HI)) == NULL) {
577c478bd9Sstevel@tonic-gate 		/* XXX trouble: don't want to drop events. should queue it. */
587c478bd9Sstevel@tonic-gate 		freemsg(emp);
597c478bd9Sstevel@tonic-gate 		return;
607c478bd9Sstevel@tonic-gate 	}
617c478bd9Sstevel@tonic-gate 	dprint(3, ("sctp_notify: event %d\n", (*(uint16_t *)emp->b_rptr)));
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
647c478bd9Sstevel@tonic-gate 	mp->b_flag |= MSGMARK;
657c478bd9Sstevel@tonic-gate 	mp->b_rptr += sizeof (void *); /* pointer worth of padding */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
687c478bd9Sstevel@tonic-gate 	tudi->PRIM_type = T_UNITDATA_IND;
697c478bd9Sstevel@tonic-gate 	tudi->SRC_offset = sizeof (*tudi);
707c478bd9Sstevel@tonic-gate 	tudi->OPT_length = 0;
717c478bd9Sstevel@tonic-gate 	tudi->OPT_offset = 0;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	fp = sctp->sctp_primary;
747c478bd9Sstevel@tonic-gate 	ASSERT(fp);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/*
777c478bd9Sstevel@tonic-gate 	 * Fill in primary remote address.
787c478bd9Sstevel@tonic-gate 	 */
79*6be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 	if (IN6_IS_ADDR_V4MAPPED(&fp->sf_faddr)) {
807c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 		tudi->SRC_length = sizeof (*sin4);
837c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)(tudi + 1);
847c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
85bd670b35SErik Nordmark 		sin4->sin_port = connp->conn_fport;
86*6be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		IN6_V4MAPPED_TO_IPADDR(&fp->sf_faddr, sin4->sin_addr.s_addr);
877c478bd9Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)(sin4 + 1);
887c478bd9Sstevel@tonic-gate 	} else {
897c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 		tudi->SRC_length = sizeof (*sin6);
927c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)(tudi + 1);
937c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
94bd670b35SErik Nordmark 		sin6->sin6_port = connp->conn_fport;
95*6be61d4eSchandrasekar marimuthu - Sun Microsystems - Bangalore India 		sin6->sin6_addr = fp->sf_faddr;
967c478bd9Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)(sin6 + 1);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	mp->b_cont = emp;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/*
1027c478bd9Sstevel@tonic-gate 	 * Notifications are queued regardless of socket rx space.  So
1037c478bd9Sstevel@tonic-gate 	 * we do not decrement sctp_rwnd here as this will confuse the
1047c478bd9Sstevel@tonic-gate 	 * other side.
1057c478bd9Sstevel@tonic-gate 	 */
1067c478bd9Sstevel@tonic-gate #ifdef DEBUG
1077c478bd9Sstevel@tonic-gate 	for (emp = mp->b_cont; emp; emp = emp->b_cont) {
1087c478bd9Sstevel@tonic-gate 		rwnd += emp->b_wptr - emp->b_rptr;
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 	ASSERT(len == rwnd);
1117c478bd9Sstevel@tonic-gate #endif
1127c478bd9Sstevel@tonic-gate 
1130f1702c5SYu Xiangning 	/*
1140f1702c5SYu Xiangning 	 * Override b_flag for SCTP sockfs internal use
1150f1702c5SYu Xiangning 	 */
1160f1702c5SYu Xiangning 	mp->b_flag = (short)SCTP_NOTIFICATION;
1170f1702c5SYu Xiangning 
1180f1702c5SYu Xiangning 	rwnd = sctp->sctp_ulp_recv(sctp->sctp_ulpd, mp, msgdsize(mp), 0,
1190f1702c5SYu Xiangning 	    &error, NULL);
1207c478bd9Sstevel@tonic-gate 	if (rwnd > sctp->sctp_rwnd) {
1217c478bd9Sstevel@tonic-gate 		sctp->sctp_rwnd = rwnd;
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate void
1267c478bd9Sstevel@tonic-gate sctp_assoc_event(sctp_t *sctp, uint16_t state, uint16_t error,
1277c478bd9Sstevel@tonic-gate     sctp_chunk_hdr_t *ch)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	struct sctp_assoc_change *sacp;
1307c478bd9Sstevel@tonic-gate 	mblk_t *mp;
1317c478bd9Sstevel@tonic-gate 	uint16_t ch_len;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvassocevnt) {
1347c478bd9Sstevel@tonic-gate 		return;
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	ch_len = (ch != NULL) ? ntohs(ch->sch_len) : 0;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*sacp) + ch_len, BPRI_MED)) == NULL) {
1407c478bd9Sstevel@tonic-gate 		return;
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	sacp = (struct sctp_assoc_change *)mp->b_rptr;
1447c478bd9Sstevel@tonic-gate 	sacp->sac_type = SCTP_ASSOC_CHANGE;
1457c478bd9Sstevel@tonic-gate 	sacp->sac_flags = sctp->sctp_prsctp_aware ? SCTP_PRSCTP_CAPABLE : 0;
1467c478bd9Sstevel@tonic-gate 	sacp->sac_length = sizeof (*sacp) + ch_len;
1477c478bd9Sstevel@tonic-gate 	sacp->sac_state = state;
1487c478bd9Sstevel@tonic-gate 	sacp->sac_error = error;
1497c478bd9Sstevel@tonic-gate 	sacp->sac_outbound_streams = sctp->sctp_num_ostr;
1507c478bd9Sstevel@tonic-gate 	sacp->sac_inbound_streams = sctp->sctp_num_istr;
1517c478bd9Sstevel@tonic-gate 	sacp->sac_assoc_id = 0;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (ch != NULL)
1547c478bd9Sstevel@tonic-gate 		bcopy(ch, sacp + 1, ch_len);
1557c478bd9Sstevel@tonic-gate 	mp->b_wptr += sacp->sac_length;
1567c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, sacp->sac_length);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * Send failure event. Message is expected to have message header still
1617c478bd9Sstevel@tonic-gate  * in place, data follows in subsequent mblk's.
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate static void
1647c478bd9Sstevel@tonic-gate sctp_sendfail(sctp_t *sctp, mblk_t *msghdr, uint16_t flags, int error)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	struct sctp_send_failed *sfp;
1677c478bd9Sstevel@tonic-gate 	mblk_t *mp;
1687c478bd9Sstevel@tonic-gate 	sctp_msg_hdr_t *smh;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/* Allocate a mblk for the notification header */
1717c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*sfp), BPRI_MED)) == NULL) {
1727c478bd9Sstevel@tonic-gate 		/* give up */
1737c478bd9Sstevel@tonic-gate 		freemsg(msghdr);
1747c478bd9Sstevel@tonic-gate 		return;
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	smh = (sctp_msg_hdr_t *)msghdr->b_rptr;
1787c478bd9Sstevel@tonic-gate 	sfp = (struct sctp_send_failed *)mp->b_rptr;
1797c478bd9Sstevel@tonic-gate 	sfp->ssf_type = SCTP_SEND_FAILED;
1807c478bd9Sstevel@tonic-gate 	sfp->ssf_flags = flags;
1817c478bd9Sstevel@tonic-gate 	sfp->ssf_length = smh->smh_msglen + sizeof (*sfp);
1827c478bd9Sstevel@tonic-gate 	sfp->ssf_error = error;
1837c478bd9Sstevel@tonic-gate 	sfp->ssf_assoc_id = 0;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	bzero(&sfp->ssf_info, sizeof (sfp->ssf_info));
1867c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_stream = smh->smh_sid;
1877c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_flags = smh->smh_flags;
1887c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_ppid = smh->smh_ppid;
1897c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_context = smh->smh_context;
1907c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_timetolive = TICK_TO_MSEC(smh->smh_ttl);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(sfp + 1);
1937c478bd9Sstevel@tonic-gate 	mp->b_cont = msghdr->b_cont;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	freeb(msghdr);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, sfp->ssf_length);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * Send failure when the message has been fully chunkified.
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate static void
2057c478bd9Sstevel@tonic-gate sctp_sendfail_sent(sctp_t *sctp, mblk_t *meta, int error)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
2087c478bd9Sstevel@tonic-gate 	mblk_t		*nmp;
2097c478bd9Sstevel@tonic-gate 	mblk_t		*tail;
2107c478bd9Sstevel@tonic-gate 	uint16_t	flags = SCTP_DATA_SENT;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvsendfailevnt) {
2137c478bd9Sstevel@tonic-gate 		sctp_free_msg(meta);
2147c478bd9Sstevel@tonic-gate 		return;
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/*
2187c478bd9Sstevel@tonic-gate 	 * We need to remove all data_hdr's.
2197c478bd9Sstevel@tonic-gate 	 */
2207c478bd9Sstevel@tonic-gate 	nmp = meta->b_cont;
2217c478bd9Sstevel@tonic-gate 	tail = meta;
2227c478bd9Sstevel@tonic-gate 	do {
2237c478bd9Sstevel@tonic-gate 		mp = nmp->b_next;
2247c478bd9Sstevel@tonic-gate 		nmp->b_next = NULL;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		/*
2277c478bd9Sstevel@tonic-gate 		 * If one of the chunks hasn't been sent yet, say that
2287c478bd9Sstevel@tonic-gate 		 * the message hasn't been sent.
2297c478bd9Sstevel@tonic-gate 		 */
2307c478bd9Sstevel@tonic-gate 		if (!SCTP_CHUNK_ISSENT(nmp)) {
2317c478bd9Sstevel@tonic-gate 			flags = SCTP_DATA_UNSENT;
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 		nmp->b_rptr += sizeof (sctp_data_hdr_t);
2347c478bd9Sstevel@tonic-gate 		if (nmp->b_rptr == nmp->b_wptr) {
2357c478bd9Sstevel@tonic-gate 			tail->b_cont = nmp->b_cont;
2367c478bd9Sstevel@tonic-gate 			freeb(nmp);
2377c478bd9Sstevel@tonic-gate 		} else {
2387c478bd9Sstevel@tonic-gate 			tail->b_cont = nmp;
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 		while (tail->b_cont) {
2417c478bd9Sstevel@tonic-gate 			tail = tail->b_cont;
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 	} while ((nmp = mp) != NULL);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	sctp_sendfail(sctp, meta, flags, error);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate  * Send failure when the message hasn't been fully chunkified.
2507c478bd9Sstevel@tonic-gate  */
2517c478bd9Sstevel@tonic-gate void
2527c478bd9Sstevel@tonic-gate sctp_sendfail_event(sctp_t *sctp, mblk_t *meta, int error, boolean_t chunkified)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
2557c478bd9Sstevel@tonic-gate 	mblk_t	*nmp;
2567c478bd9Sstevel@tonic-gate 	mblk_t	*tail;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (meta == NULL)
2597c478bd9Sstevel@tonic-gate 		return;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvsendfailevnt) {
2627c478bd9Sstevel@tonic-gate 		sctp_free_msg(meta);
2637c478bd9Sstevel@tonic-gate 		return;
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/* If the message is fully chunkified */
2677c478bd9Sstevel@tonic-gate 	if (chunkified) {
2687c478bd9Sstevel@tonic-gate 		sctp_sendfail_sent(sctp, meta, error);
2697c478bd9Sstevel@tonic-gate 		return;
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * Message might be partially chunkified, we need to remove
2737c478bd9Sstevel@tonic-gate 	 * all data_hdr's.
2747c478bd9Sstevel@tonic-gate 	 */
2757c478bd9Sstevel@tonic-gate 	mp = meta->b_cont;
2767c478bd9Sstevel@tonic-gate 	tail = meta;
2777c478bd9Sstevel@tonic-gate 	while ((nmp = mp->b_next) != NULL) {
2787c478bd9Sstevel@tonic-gate 		mp->b_next = nmp->b_next;
2797c478bd9Sstevel@tonic-gate 		nmp->b_next = NULL;
2807c478bd9Sstevel@tonic-gate 		nmp->b_rptr += sizeof (sctp_data_hdr_t);
2817c478bd9Sstevel@tonic-gate 		if (nmp->b_rptr == nmp->b_wptr) {
2827c478bd9Sstevel@tonic-gate 			tail->b_cont = nmp->b_cont;
2837c478bd9Sstevel@tonic-gate 			freeb(nmp);
2847c478bd9Sstevel@tonic-gate 		} else {
2857c478bd9Sstevel@tonic-gate 			tail->b_cont = nmp;
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 		while (tail->b_cont) {
2887c478bd9Sstevel@tonic-gate 			tail = tail->b_cont;
2897c478bd9Sstevel@tonic-gate 		}
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	tail->b_cont = mp;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	sctp_sendfail(sctp, meta, SCTP_DATA_UNSENT, error);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate void
2977c478bd9Sstevel@tonic-gate sctp_regift_xmitlist(sctp_t *sctp)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	mblk_t *mp;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvsendfailevnt) {
3027c478bd9Sstevel@tonic-gate 		return;
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_xmit_head) != NULL) {
3067c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_head = mp->b_next;
3077c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
3087c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_head != NULL)
3097c478bd9Sstevel@tonic-gate 			sctp->sctp_xmit_head->b_prev = NULL;
3107c478bd9Sstevel@tonic-gate 		sctp_sendfail_event(sctp, mp, 0, B_TRUE);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_xmit_unsent) != NULL) {
3137c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_unsent = mp->b_next;
3147c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
3157c478bd9Sstevel@tonic-gate 		sctp_sendfail_event(sctp, mp, 0, B_FALSE);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_tail = sctp->sctp_xmit_unsent_tail = NULL;
3187c478bd9Sstevel@tonic-gate 	sctp->sctp_unacked = sctp->sctp_unsent = 0;
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate void
3227c478bd9Sstevel@tonic-gate sctp_intf_event(sctp_t *sctp, in6_addr_t addr, int state, int error)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	struct sctp_paddr_change *spc;
3257c478bd9Sstevel@tonic-gate 	ipaddr_t addr4;
3267c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
3277c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
3287c478bd9Sstevel@tonic-gate 	mblk_t *mp;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvpathevnt) {
3317c478bd9Sstevel@tonic-gate 		return;
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*spc), BPRI_MED)) == NULL) {
3357c478bd9Sstevel@tonic-gate 		return;
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	spc = (struct sctp_paddr_change *)mp->b_rptr;
3397c478bd9Sstevel@tonic-gate 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
3407c478bd9Sstevel@tonic-gate 	spc->spc_flags = 0;
3417c478bd9Sstevel@tonic-gate 	spc->spc_length = sizeof (*spc);
3427c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&addr)) {
3437c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&addr, addr4);
3447c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&spc->spc_aaddr;
3457c478bd9Sstevel@tonic-gate 		sin->sin_family = AF_INET;
3467c478bd9Sstevel@tonic-gate 		sin->sin_port = 0;
3477c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = addr4;
3487c478bd9Sstevel@tonic-gate 	} else {
3497c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
3507c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
3517c478bd9Sstevel@tonic-gate 		sin6->sin6_port = 0;
3527c478bd9Sstevel@tonic-gate 		sin6->sin6_addr = addr;
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 	spc->spc_state = state;
3557c478bd9Sstevel@tonic-gate 	spc->spc_error = error;
3567c478bd9Sstevel@tonic-gate 	spc->spc_assoc_id = 0;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(spc + 1);
3597c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, spc->spc_length);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate void
3638dfac042SAnil udupa sctp_error_event(sctp_t *sctp, sctp_chunk_hdr_t *ch, boolean_t is_asconf)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	struct sctp_remote_error *sre;
3667c478bd9Sstevel@tonic-gate 	mblk_t *mp;
3677c478bd9Sstevel@tonic-gate 	size_t len;
3688dfac042SAnil udupa 	sctp_parm_hdr_t *errh = NULL;
3697c478bd9Sstevel@tonic-gate 	uint16_t dlen = 0;
3707c478bd9Sstevel@tonic-gate 	uint16_t error = 0;
3717c478bd9Sstevel@tonic-gate 	void *dtail = NULL;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvpeererr) {
3747c478bd9Sstevel@tonic-gate 		return;
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3778dfac042SAnil udupa 	/*
3788dfac042SAnil udupa 	 * ASCONF PARM error chunks :
3798dfac042SAnil udupa 	 * PARM_ERROR_IND parm type is always followed by correlation id.
3808dfac042SAnil udupa 	 * See sctp_asconf_adderr() and sctp_asconf_prepend_errwrap() as to
3818dfac042SAnil udupa 	 * how these error chunks are build.
3828dfac042SAnil udupa 	 * error cause wrapper (PARM_ERROR_IND) + correlation id +
3838dfac042SAnil udupa 	 * error cause + error cause details.
3848dfac042SAnil udupa 	 *
3858dfac042SAnil udupa 	 * Regular error chunks :
3868dfac042SAnil udupa 	 * See sctp_make_err() as to how these error chunks are build.
3878dfac042SAnil udupa 	 * error chunk header (CHUNK_ERROR) + error cause + error cause details.
3888dfac042SAnil udupa 	 */
3898dfac042SAnil udupa 	if (is_asconf) {
3908dfac042SAnil udupa 		if (ntohs(ch->sch_len) >
3918dfac042SAnil udupa 		    (sizeof (*ch) + sizeof (uint32_t))) {
3928dfac042SAnil udupa 			errh = (sctp_parm_hdr_t *)((char *)ch +
3938dfac042SAnil udupa 			    sizeof (uint32_t) + sizeof (sctp_parm_hdr_t));
3948dfac042SAnil udupa 		}
3958dfac042SAnil udupa 	} else {
3968dfac042SAnil udupa 		if (ntohs(ch->sch_len) > sizeof (*ch)) {
3978dfac042SAnil udupa 			errh = (sctp_parm_hdr_t *)(ch + 1);
3988dfac042SAnil udupa 		}
3998dfac042SAnil udupa 	}
4008dfac042SAnil udupa 
4018dfac042SAnil udupa 	if (errh != NULL) {
4027c478bd9Sstevel@tonic-gate 		error = ntohs(errh->sph_type);
4037c478bd9Sstevel@tonic-gate 		dlen = ntohs(errh->sph_len) - sizeof (*errh);
4047c478bd9Sstevel@tonic-gate 		if (dlen > 0) {
4057c478bd9Sstevel@tonic-gate 			dtail = errh + 1;
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	len = sizeof (*sre) + dlen;
4107c478bd9Sstevel@tonic-gate 	if ((mp = allocb(len, BPRI_MED)) == NULL) {
4117c478bd9Sstevel@tonic-gate 		return;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	sre = (struct sctp_remote_error *)mp->b_rptr;
4157c478bd9Sstevel@tonic-gate 	sre->sre_type = SCTP_REMOTE_ERROR;
4167c478bd9Sstevel@tonic-gate 	sre->sre_flags = 0;
4177c478bd9Sstevel@tonic-gate 	sre->sre_length = len;
4187c478bd9Sstevel@tonic-gate 	sre->sre_assoc_id = 0;
4197c478bd9Sstevel@tonic-gate 	sre->sre_error = error;
4208dfac042SAnil udupa 	if (dtail != NULL) {
4217c478bd9Sstevel@tonic-gate 		bcopy(dtail, sre + 1, dlen);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + len;
4257c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, len);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate void
4297c478bd9Sstevel@tonic-gate sctp_shutdown_event(sctp_t *sctp)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	struct sctp_shutdown_event *sse;
4327c478bd9Sstevel@tonic-gate 	mblk_t *mp;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvshutdownevnt) {
4357c478bd9Sstevel@tonic-gate 		return;
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*sse), BPRI_MED)) == NULL) {
4397c478bd9Sstevel@tonic-gate 		return;
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	sse = (struct sctp_shutdown_event *)mp->b_rptr;
4437c478bd9Sstevel@tonic-gate 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
4447c478bd9Sstevel@tonic-gate 	sse->sse_flags = 0;
4457c478bd9Sstevel@tonic-gate 	sse->sse_length = sizeof (*sse);
4467c478bd9Sstevel@tonic-gate 	sse->sse_assoc_id = 0;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(sse + 1);
4497c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, sse->sse_length);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate void
453558fbd03Skcpoon sctp_adaptation_event(sctp_t *sctp)
4547c478bd9Sstevel@tonic-gate {
455558fbd03Skcpoon 	struct sctp_adaptation_event *sai;
4567c478bd9Sstevel@tonic-gate 	mblk_t *mp;
4577c478bd9Sstevel@tonic-gate 
458558fbd03Skcpoon 	if (!sctp->sctp_recvalevnt || !sctp->sctp_recv_adaptation) {
4597c478bd9Sstevel@tonic-gate 		return;
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*sai), BPRI_MED)) == NULL) {
4627c478bd9Sstevel@tonic-gate 		return;
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
465558fbd03Skcpoon 	sai = (struct sctp_adaptation_event *)mp->b_rptr;
466558fbd03Skcpoon 	sai->sai_type = SCTP_ADAPTATION_INDICATION;
4677c478bd9Sstevel@tonic-gate 	sai->sai_flags = 0;
4687c478bd9Sstevel@tonic-gate 	sai->sai_length = sizeof (*sai);
4697c478bd9Sstevel@tonic-gate 	sai->sai_assoc_id = 0;
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * Adaptation code delivered in network byte order.
4727c478bd9Sstevel@tonic-gate 	 */
473558fbd03Skcpoon 	sai->sai_adaptation_ind = sctp->sctp_rx_adaptation_code;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(sai + 1);
4767c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, sai->sai_length);
4777c478bd9Sstevel@tonic-gate 
478558fbd03Skcpoon 	sctp->sctp_recv_adaptation = 0; /* in case there's a restart later */
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate /* Send partial deliver event */
4827c478bd9Sstevel@tonic-gate void
4837c478bd9Sstevel@tonic-gate sctp_partial_delivery_event(sctp_t *sctp)
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	struct sctp_pdapi_event	*pdapi;
4867c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvpdevnt)
4897c478bd9Sstevel@tonic-gate 		return;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*pdapi), BPRI_MED)) == NULL)
4927c478bd9Sstevel@tonic-gate 		return;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	pdapi = (struct sctp_pdapi_event *)mp->b_rptr;
4957c478bd9Sstevel@tonic-gate 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
4967c478bd9Sstevel@tonic-gate 	pdapi->pdapi_flags = 0;
4977c478bd9Sstevel@tonic-gate 	pdapi->pdapi_length = sizeof (*pdapi);
4987c478bd9Sstevel@tonic-gate 	pdapi->pdapi_indication = SCTP_PARTIAL_DELIVERY_ABORTED;
4997c478bd9Sstevel@tonic-gate 	pdapi->pdapi_assoc_id = 0;
5007c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(pdapi + 1);
5017c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, pdapi->pdapi_length);
5027c478bd9Sstevel@tonic-gate }
503