1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/stream.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
35*7c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
36*7c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
40*7c478bd9Sstevel@tonic-gate #include <netinet/sctp.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <inet/common.h>
43*7c478bd9Sstevel@tonic-gate #include <inet/ip.h>
44*7c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static void
47*7c478bd9Sstevel@tonic-gate sctp_notify(sctp_t *sctp, mblk_t *emp, size_t len)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	struct T_unitdata_ind *tudi;
50*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
51*7c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
52*7c478bd9Sstevel@tonic-gate 	int32_t rwnd = 0;
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*tudi) + sizeof (void *) +
55*7c478bd9Sstevel@tonic-gate 		sizeof (struct sockaddr_in6), BPRI_HI)) == NULL) {
56*7c478bd9Sstevel@tonic-gate 		/* XXX trouble: don't want to drop events. should queue it. */
57*7c478bd9Sstevel@tonic-gate 		freemsg(emp);
58*7c478bd9Sstevel@tonic-gate 		return;
59*7c478bd9Sstevel@tonic-gate 	}
60*7c478bd9Sstevel@tonic-gate 	dprint(3, ("sctp_notify: event %d\n", (*(uint16_t *)emp->b_rptr)));
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
63*7c478bd9Sstevel@tonic-gate 	mp->b_flag |= MSGMARK;
64*7c478bd9Sstevel@tonic-gate 	mp->b_rptr += sizeof (void *); /* pointer worth of padding */
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	tudi = (struct T_unitdata_ind *)mp->b_rptr;
67*7c478bd9Sstevel@tonic-gate 	tudi->PRIM_type = T_UNITDATA_IND;
68*7c478bd9Sstevel@tonic-gate 	tudi->SRC_offset = sizeof (*tudi);
69*7c478bd9Sstevel@tonic-gate 	tudi->OPT_length = 0;
70*7c478bd9Sstevel@tonic-gate 	tudi->OPT_offset = 0;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	fp = sctp->sctp_primary;
73*7c478bd9Sstevel@tonic-gate 	ASSERT(fp);
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	/*
76*7c478bd9Sstevel@tonic-gate 	 * Fill in primary remote address.
77*7c478bd9Sstevel@tonic-gate 	 */
78*7c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&fp->faddr)) {
79*7c478bd9Sstevel@tonic-gate 		struct sockaddr_in *sin4;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 		tudi->SRC_length = sizeof (*sin4);
82*7c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)(tudi + 1);
83*7c478bd9Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
84*7c478bd9Sstevel@tonic-gate 		sin4->sin_port = sctp->sctp_fport;
85*7c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr, sin4->sin_addr.s_addr);
86*7c478bd9Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)(sin4 + 1);
87*7c478bd9Sstevel@tonic-gate 	} else {
88*7c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 		tudi->SRC_length = sizeof (*sin6);
91*7c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)(tudi + 1);
92*7c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
93*7c478bd9Sstevel@tonic-gate 		sin6->sin6_port = sctp->sctp_fport;
94*7c478bd9Sstevel@tonic-gate 		sin6->sin6_addr = fp->faddr;
95*7c478bd9Sstevel@tonic-gate 		mp->b_wptr = (uchar_t *)(sin6 + 1);
96*7c478bd9Sstevel@tonic-gate 	}
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	mp->b_cont = emp;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	/*
101*7c478bd9Sstevel@tonic-gate 	 * Notifications are queued regardless of socket rx space.  So
102*7c478bd9Sstevel@tonic-gate 	 * we do not decrement sctp_rwnd here as this will confuse the
103*7c478bd9Sstevel@tonic-gate 	 * other side.
104*7c478bd9Sstevel@tonic-gate 	 */
105*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
106*7c478bd9Sstevel@tonic-gate 	for (emp = mp->b_cont; emp; emp = emp->b_cont) {
107*7c478bd9Sstevel@tonic-gate 		rwnd += emp->b_wptr - emp->b_rptr;
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 	ASSERT(len == rwnd);
110*7c478bd9Sstevel@tonic-gate #endif
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	rwnd = sctp->sctp_ulp_recv(sctp->sctp_ulpd, mp, SCTP_NOTIFICATION);
113*7c478bd9Sstevel@tonic-gate 	if (rwnd > sctp->sctp_rwnd) {
114*7c478bd9Sstevel@tonic-gate 		sctp->sctp_rwnd = rwnd;
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate }
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate void
119*7c478bd9Sstevel@tonic-gate sctp_assoc_event(sctp_t *sctp, uint16_t state, uint16_t error,
120*7c478bd9Sstevel@tonic-gate     sctp_chunk_hdr_t *ch)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	struct sctp_assoc_change *sacp;
123*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
124*7c478bd9Sstevel@tonic-gate 	uint16_t ch_len;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvassocevnt) {
127*7c478bd9Sstevel@tonic-gate 		return;
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	ch_len = (ch != NULL) ? ntohs(ch->sch_len) : 0;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*sacp) + ch_len, BPRI_MED)) == NULL) {
133*7c478bd9Sstevel@tonic-gate 		return;
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	sacp = (struct sctp_assoc_change *)mp->b_rptr;
137*7c478bd9Sstevel@tonic-gate 	sacp->sac_type = SCTP_ASSOC_CHANGE;
138*7c478bd9Sstevel@tonic-gate 	sacp->sac_flags = sctp->sctp_prsctp_aware ? SCTP_PRSCTP_CAPABLE : 0;
139*7c478bd9Sstevel@tonic-gate 	sacp->sac_length = sizeof (*sacp) + ch_len;
140*7c478bd9Sstevel@tonic-gate 	sacp->sac_state = state;
141*7c478bd9Sstevel@tonic-gate 	sacp->sac_error = error;
142*7c478bd9Sstevel@tonic-gate 	sacp->sac_outbound_streams = sctp->sctp_num_ostr;
143*7c478bd9Sstevel@tonic-gate 	sacp->sac_inbound_streams = sctp->sctp_num_istr;
144*7c478bd9Sstevel@tonic-gate 	sacp->sac_assoc_id = 0;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	if (ch != NULL)
147*7c478bd9Sstevel@tonic-gate 		bcopy(ch, sacp + 1, ch_len);
148*7c478bd9Sstevel@tonic-gate 	mp->b_wptr += sacp->sac_length;
149*7c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, sacp->sac_length);
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate /*
153*7c478bd9Sstevel@tonic-gate  * Send failure event. Message is expected to have message header still
154*7c478bd9Sstevel@tonic-gate  * in place, data follows in subsequent mblk's.
155*7c478bd9Sstevel@tonic-gate  */
156*7c478bd9Sstevel@tonic-gate static void
157*7c478bd9Sstevel@tonic-gate sctp_sendfail(sctp_t *sctp, mblk_t *msghdr, uint16_t flags, int error)
158*7c478bd9Sstevel@tonic-gate {
159*7c478bd9Sstevel@tonic-gate 	struct sctp_send_failed *sfp;
160*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
161*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr_t *smh;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	/* Allocate a mblk for the notification header */
164*7c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*sfp), BPRI_MED)) == NULL) {
165*7c478bd9Sstevel@tonic-gate 		/* give up */
166*7c478bd9Sstevel@tonic-gate 		freemsg(msghdr);
167*7c478bd9Sstevel@tonic-gate 		return;
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate 	smh = (sctp_msg_hdr_t *)msghdr->b_rptr;
171*7c478bd9Sstevel@tonic-gate 	sfp = (struct sctp_send_failed *)mp->b_rptr;
172*7c478bd9Sstevel@tonic-gate 	sfp->ssf_type = SCTP_SEND_FAILED;
173*7c478bd9Sstevel@tonic-gate 	sfp->ssf_flags = flags;
174*7c478bd9Sstevel@tonic-gate 	sfp->ssf_length = smh->smh_msglen + sizeof (*sfp);
175*7c478bd9Sstevel@tonic-gate 	sfp->ssf_error = error;
176*7c478bd9Sstevel@tonic-gate 	sfp->ssf_assoc_id = 0;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	bzero(&sfp->ssf_info, sizeof (sfp->ssf_info));
179*7c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_stream = smh->smh_sid;
180*7c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_flags = smh->smh_flags;
181*7c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_ppid = smh->smh_ppid;
182*7c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_context = smh->smh_context;
183*7c478bd9Sstevel@tonic-gate 	sfp->ssf_info.sinfo_timetolive = TICK_TO_MSEC(smh->smh_ttl);
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(sfp + 1);
186*7c478bd9Sstevel@tonic-gate 	mp->b_cont = msghdr->b_cont;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	freeb(msghdr);
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, sfp->ssf_length);
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate }
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate /*
195*7c478bd9Sstevel@tonic-gate  * Send failure when the message has been fully chunkified.
196*7c478bd9Sstevel@tonic-gate  */
197*7c478bd9Sstevel@tonic-gate static void
198*7c478bd9Sstevel@tonic-gate sctp_sendfail_sent(sctp_t *sctp, mblk_t *meta, int error)
199*7c478bd9Sstevel@tonic-gate {
200*7c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
201*7c478bd9Sstevel@tonic-gate 	mblk_t		*nmp;
202*7c478bd9Sstevel@tonic-gate 	mblk_t		*tail;
203*7c478bd9Sstevel@tonic-gate 	uint16_t	flags = SCTP_DATA_SENT;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvsendfailevnt) {
206*7c478bd9Sstevel@tonic-gate 		sctp_free_msg(meta);
207*7c478bd9Sstevel@tonic-gate 		return;
208*7c478bd9Sstevel@tonic-gate 	}
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	/*
211*7c478bd9Sstevel@tonic-gate 	 * We need to remove all data_hdr's.
212*7c478bd9Sstevel@tonic-gate 	 */
213*7c478bd9Sstevel@tonic-gate 	nmp = meta->b_cont;
214*7c478bd9Sstevel@tonic-gate 	tail = meta;
215*7c478bd9Sstevel@tonic-gate 	do {
216*7c478bd9Sstevel@tonic-gate 		mp = nmp->b_next;
217*7c478bd9Sstevel@tonic-gate 		nmp->b_next = NULL;
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 		/*
220*7c478bd9Sstevel@tonic-gate 		 * If one of the chunks hasn't been sent yet, say that
221*7c478bd9Sstevel@tonic-gate 		 * the message hasn't been sent.
222*7c478bd9Sstevel@tonic-gate 		 */
223*7c478bd9Sstevel@tonic-gate 		if (!SCTP_CHUNK_ISSENT(nmp)) {
224*7c478bd9Sstevel@tonic-gate 			flags = SCTP_DATA_UNSENT;
225*7c478bd9Sstevel@tonic-gate 		}
226*7c478bd9Sstevel@tonic-gate 		nmp->b_rptr += sizeof (sctp_data_hdr_t);
227*7c478bd9Sstevel@tonic-gate 		if (nmp->b_rptr == nmp->b_wptr) {
228*7c478bd9Sstevel@tonic-gate 			tail->b_cont = nmp->b_cont;
229*7c478bd9Sstevel@tonic-gate 			freeb(nmp);
230*7c478bd9Sstevel@tonic-gate 		} else {
231*7c478bd9Sstevel@tonic-gate 			tail->b_cont = nmp;
232*7c478bd9Sstevel@tonic-gate 		}
233*7c478bd9Sstevel@tonic-gate 		while (tail->b_cont) {
234*7c478bd9Sstevel@tonic-gate 			tail = tail->b_cont;
235*7c478bd9Sstevel@tonic-gate 		}
236*7c478bd9Sstevel@tonic-gate 	} while ((nmp = mp) != NULL);
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	sctp_sendfail(sctp, meta, flags, error);
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate /*
242*7c478bd9Sstevel@tonic-gate  * Send failure when the message hasn't been fully chunkified.
243*7c478bd9Sstevel@tonic-gate  */
244*7c478bd9Sstevel@tonic-gate void
245*7c478bd9Sstevel@tonic-gate sctp_sendfail_event(sctp_t *sctp, mblk_t *meta, int error, boolean_t chunkified)
246*7c478bd9Sstevel@tonic-gate {
247*7c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
248*7c478bd9Sstevel@tonic-gate 	mblk_t	*nmp;
249*7c478bd9Sstevel@tonic-gate 	mblk_t	*tail;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	if (meta == NULL)
252*7c478bd9Sstevel@tonic-gate 		return;
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvsendfailevnt) {
255*7c478bd9Sstevel@tonic-gate 		sctp_free_msg(meta);
256*7c478bd9Sstevel@tonic-gate 		return;
257*7c478bd9Sstevel@tonic-gate 	}
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	/* If the message is fully chunkified */
260*7c478bd9Sstevel@tonic-gate 	if (chunkified) {
261*7c478bd9Sstevel@tonic-gate 		sctp_sendfail_sent(sctp, meta, error);
262*7c478bd9Sstevel@tonic-gate 		return;
263*7c478bd9Sstevel@tonic-gate 	}
264*7c478bd9Sstevel@tonic-gate 	/*
265*7c478bd9Sstevel@tonic-gate 	 * Message might be partially chunkified, we need to remove
266*7c478bd9Sstevel@tonic-gate 	 * all data_hdr's.
267*7c478bd9Sstevel@tonic-gate 	 */
268*7c478bd9Sstevel@tonic-gate 	mp = meta->b_cont;
269*7c478bd9Sstevel@tonic-gate 	tail = meta;
270*7c478bd9Sstevel@tonic-gate 	while ((nmp = mp->b_next) != NULL) {
271*7c478bd9Sstevel@tonic-gate 		mp->b_next = nmp->b_next;
272*7c478bd9Sstevel@tonic-gate 		nmp->b_next = NULL;
273*7c478bd9Sstevel@tonic-gate 		nmp->b_rptr += sizeof (sctp_data_hdr_t);
274*7c478bd9Sstevel@tonic-gate 		if (nmp->b_rptr == nmp->b_wptr) {
275*7c478bd9Sstevel@tonic-gate 			tail->b_cont = nmp->b_cont;
276*7c478bd9Sstevel@tonic-gate 			freeb(nmp);
277*7c478bd9Sstevel@tonic-gate 		} else {
278*7c478bd9Sstevel@tonic-gate 			tail->b_cont = nmp;
279*7c478bd9Sstevel@tonic-gate 		}
280*7c478bd9Sstevel@tonic-gate 		while (tail->b_cont) {
281*7c478bd9Sstevel@tonic-gate 			tail = tail->b_cont;
282*7c478bd9Sstevel@tonic-gate 		}
283*7c478bd9Sstevel@tonic-gate 	}
284*7c478bd9Sstevel@tonic-gate 	tail->b_cont = mp;
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 	sctp_sendfail(sctp, meta, SCTP_DATA_UNSENT, error);
287*7c478bd9Sstevel@tonic-gate }
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate void
290*7c478bd9Sstevel@tonic-gate sctp_regift_xmitlist(sctp_t *sctp)
291*7c478bd9Sstevel@tonic-gate {
292*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvsendfailevnt) {
295*7c478bd9Sstevel@tonic-gate 		return;
296*7c478bd9Sstevel@tonic-gate 	}
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_xmit_head) != NULL) {
299*7c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_head = mp->b_next;
300*7c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
301*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_xmit_head != NULL)
302*7c478bd9Sstevel@tonic-gate 			sctp->sctp_xmit_head->b_prev = NULL;
303*7c478bd9Sstevel@tonic-gate 		sctp_sendfail_event(sctp, mp, 0, B_TRUE);
304*7c478bd9Sstevel@tonic-gate 	}
305*7c478bd9Sstevel@tonic-gate 	while ((mp = sctp->sctp_xmit_unsent) != NULL) {
306*7c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_unsent = mp->b_next;
307*7c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
308*7c478bd9Sstevel@tonic-gate 		sctp_sendfail_event(sctp, mp, 0, B_FALSE);
309*7c478bd9Sstevel@tonic-gate 	}
310*7c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_tail = sctp->sctp_xmit_unsent_tail = NULL;
311*7c478bd9Sstevel@tonic-gate 	sctp->sctp_unacked = sctp->sctp_unsent = 0;
312*7c478bd9Sstevel@tonic-gate }
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate void
315*7c478bd9Sstevel@tonic-gate sctp_intf_event(sctp_t *sctp, in6_addr_t addr, int state, int error)
316*7c478bd9Sstevel@tonic-gate {
317*7c478bd9Sstevel@tonic-gate 	struct sctp_paddr_change *spc;
318*7c478bd9Sstevel@tonic-gate 	ipaddr_t addr4;
319*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
320*7c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
321*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvpathevnt) {
324*7c478bd9Sstevel@tonic-gate 		return;
325*7c478bd9Sstevel@tonic-gate 	}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*spc), BPRI_MED)) == NULL) {
328*7c478bd9Sstevel@tonic-gate 		return;
329*7c478bd9Sstevel@tonic-gate 	}
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	spc = (struct sctp_paddr_change *)mp->b_rptr;
332*7c478bd9Sstevel@tonic-gate 	spc->spc_type = SCTP_PEER_ADDR_CHANGE;
333*7c478bd9Sstevel@tonic-gate 	spc->spc_flags = 0;
334*7c478bd9Sstevel@tonic-gate 	spc->spc_length = sizeof (*spc);
335*7c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&addr)) {
336*7c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&addr, addr4);
337*7c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&spc->spc_aaddr;
338*7c478bd9Sstevel@tonic-gate 		sin->sin_family = AF_INET;
339*7c478bd9Sstevel@tonic-gate 		sin->sin_port = 0;
340*7c478bd9Sstevel@tonic-gate 		sin->sin_addr.s_addr = addr4;
341*7c478bd9Sstevel@tonic-gate 	} else {
342*7c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
343*7c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
344*7c478bd9Sstevel@tonic-gate 		sin6->sin6_port = 0;
345*7c478bd9Sstevel@tonic-gate 		sin6->sin6_addr = addr;
346*7c478bd9Sstevel@tonic-gate 	}
347*7c478bd9Sstevel@tonic-gate 	spc->spc_state = state;
348*7c478bd9Sstevel@tonic-gate 	spc->spc_error = error;
349*7c478bd9Sstevel@tonic-gate 	spc->spc_assoc_id = 0;
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(spc + 1);
352*7c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, spc->spc_length);
353*7c478bd9Sstevel@tonic-gate }
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate void
356*7c478bd9Sstevel@tonic-gate sctp_error_event(sctp_t *sctp, sctp_chunk_hdr_t *ch)
357*7c478bd9Sstevel@tonic-gate {
358*7c478bd9Sstevel@tonic-gate 	struct sctp_remote_error *sre;
359*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
360*7c478bd9Sstevel@tonic-gate 	size_t len;
361*7c478bd9Sstevel@tonic-gate 	sctp_parm_hdr_t *errh;
362*7c478bd9Sstevel@tonic-gate 	uint16_t dlen = 0;
363*7c478bd9Sstevel@tonic-gate 	uint16_t error = 0;
364*7c478bd9Sstevel@tonic-gate 	void *dtail = NULL;
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvpeererr) {
367*7c478bd9Sstevel@tonic-gate 		return;
368*7c478bd9Sstevel@tonic-gate 	}
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 	if (ntohs(ch->sch_len) > sizeof (*ch)) {
371*7c478bd9Sstevel@tonic-gate 		errh = (sctp_parm_hdr_t *)(ch + 1);
372*7c478bd9Sstevel@tonic-gate 		error = ntohs(errh->sph_type);
373*7c478bd9Sstevel@tonic-gate 		dlen = ntohs(errh->sph_len) - sizeof (*errh);
374*7c478bd9Sstevel@tonic-gate 		if (dlen > 0) {
375*7c478bd9Sstevel@tonic-gate 			dtail = errh + 1;
376*7c478bd9Sstevel@tonic-gate 		}
377*7c478bd9Sstevel@tonic-gate 	}
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	len = sizeof (*sre) + dlen;
380*7c478bd9Sstevel@tonic-gate 	if ((mp = allocb(len, BPRI_MED)) == NULL) {
381*7c478bd9Sstevel@tonic-gate 		return;
382*7c478bd9Sstevel@tonic-gate 	}
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 	sre = (struct sctp_remote_error *)mp->b_rptr;
385*7c478bd9Sstevel@tonic-gate 	sre->sre_type = SCTP_REMOTE_ERROR;
386*7c478bd9Sstevel@tonic-gate 	sre->sre_flags = 0;
387*7c478bd9Sstevel@tonic-gate 	sre->sre_length = len;
388*7c478bd9Sstevel@tonic-gate 	sre->sre_assoc_id = 0;
389*7c478bd9Sstevel@tonic-gate 	sre->sre_error = error;
390*7c478bd9Sstevel@tonic-gate 	if (dtail) {
391*7c478bd9Sstevel@tonic-gate 		bcopy(dtail, sre + 1, dlen);
392*7c478bd9Sstevel@tonic-gate 	}
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + len;
395*7c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, len);
396*7c478bd9Sstevel@tonic-gate }
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate void
399*7c478bd9Sstevel@tonic-gate sctp_shutdown_event(sctp_t *sctp)
400*7c478bd9Sstevel@tonic-gate {
401*7c478bd9Sstevel@tonic-gate 	struct sctp_shutdown_event *sse;
402*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvshutdownevnt) {
405*7c478bd9Sstevel@tonic-gate 		return;
406*7c478bd9Sstevel@tonic-gate 	}
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*sse), BPRI_MED)) == NULL) {
409*7c478bd9Sstevel@tonic-gate 		return;
410*7c478bd9Sstevel@tonic-gate 	}
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	sse = (struct sctp_shutdown_event *)mp->b_rptr;
413*7c478bd9Sstevel@tonic-gate 	sse->sse_type = SCTP_SHUTDOWN_EVENT;
414*7c478bd9Sstevel@tonic-gate 	sse->sse_flags = 0;
415*7c478bd9Sstevel@tonic-gate 	sse->sse_length = sizeof (*sse);
416*7c478bd9Sstevel@tonic-gate 	sse->sse_assoc_id = 0;
417*7c478bd9Sstevel@tonic-gate 
418*7c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(sse + 1);
419*7c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, sse->sse_length);
420*7c478bd9Sstevel@tonic-gate }
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate void
423*7c478bd9Sstevel@tonic-gate sctp_adaption_event(sctp_t *sctp)
424*7c478bd9Sstevel@tonic-gate {
425*7c478bd9Sstevel@tonic-gate 	struct sctp_adaption_event *sai;
426*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvalevnt || !sctp->sctp_recv_adaption) {
429*7c478bd9Sstevel@tonic-gate 		return;
430*7c478bd9Sstevel@tonic-gate 	}
431*7c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*sai), BPRI_MED)) == NULL) {
432*7c478bd9Sstevel@tonic-gate 		return;
433*7c478bd9Sstevel@tonic-gate 	}
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 	sai = (struct sctp_adaption_event *)mp->b_rptr;
436*7c478bd9Sstevel@tonic-gate 	sai->sai_type = SCTP_ADAPTION_INDICATION;
437*7c478bd9Sstevel@tonic-gate 	sai->sai_flags = 0;
438*7c478bd9Sstevel@tonic-gate 	sai->sai_length = sizeof (*sai);
439*7c478bd9Sstevel@tonic-gate 	sai->sai_assoc_id = 0;
440*7c478bd9Sstevel@tonic-gate 	/*
441*7c478bd9Sstevel@tonic-gate 	 * Adaptation code delivered in network byte order.
442*7c478bd9Sstevel@tonic-gate 	 */
443*7c478bd9Sstevel@tonic-gate 	sai->sai_adaption_ind = sctp->sctp_rx_adaption_code;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(sai + 1);
446*7c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, sai->sai_length);
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 	sctp->sctp_recv_adaption = 0; /* in case there's a restart later */
449*7c478bd9Sstevel@tonic-gate }
450*7c478bd9Sstevel@tonic-gate 
451*7c478bd9Sstevel@tonic-gate /* Send partial deliver event */
452*7c478bd9Sstevel@tonic-gate void
453*7c478bd9Sstevel@tonic-gate sctp_partial_delivery_event(sctp_t *sctp)
454*7c478bd9Sstevel@tonic-gate {
455*7c478bd9Sstevel@tonic-gate 	struct sctp_pdapi_event	*pdapi;
456*7c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_recvpdevnt)
459*7c478bd9Sstevel@tonic-gate 		return;
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate 	if ((mp = allocb(sizeof (*pdapi), BPRI_MED)) == NULL)
462*7c478bd9Sstevel@tonic-gate 		return;
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	pdapi = (struct sctp_pdapi_event *)mp->b_rptr;
465*7c478bd9Sstevel@tonic-gate 	pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
466*7c478bd9Sstevel@tonic-gate 	pdapi->pdapi_flags = 0;
467*7c478bd9Sstevel@tonic-gate 	pdapi->pdapi_length = sizeof (*pdapi);
468*7c478bd9Sstevel@tonic-gate 	pdapi->pdapi_indication = SCTP_PARTIAL_DELIVERY_ABORTED;
469*7c478bd9Sstevel@tonic-gate 	pdapi->pdapi_assoc_id = 0;
470*7c478bd9Sstevel@tonic-gate 	mp->b_wptr = (uchar_t *)(pdapi + 1);
471*7c478bd9Sstevel@tonic-gate 	sctp_notify(sctp, mp, pdapi->pdapi_length);
472*7c478bd9Sstevel@tonic-gate }
473