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