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 2004 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 #define	_SUN_TPI_VERSION 2
34*7c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/socketvar.h>
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
42*7c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
43*7c478bd9Sstevel@tonic-gate #include <netinet/tcp_seq.h>
44*7c478bd9Sstevel@tonic-gate #include <netinet/sctp.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #include <inet/common.h>
47*7c478bd9Sstevel@tonic-gate #include <inet/mi.h>
48*7c478bd9Sstevel@tonic-gate #include <inet/ip.h>
49*7c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
50*7c478bd9Sstevel@tonic-gate #include <inet/ip_ire.h>
51*7c478bd9Sstevel@tonic-gate #include <inet/sctp_ip.h>
52*7c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate  * PR-SCTP comments.
56*7c478bd9Sstevel@tonic-gate  *
57*7c478bd9Sstevel@tonic-gate  * A message can expire before it gets to the transmit list (i.e. it is still
58*7c478bd9Sstevel@tonic-gate  * in the unsent list - unchunked), after it gets to the transmit list, but
59*7c478bd9Sstevel@tonic-gate  * before transmission has actually started, or after transmission has begun.
60*7c478bd9Sstevel@tonic-gate  * Accordingly, we check for the status of a message in sctp_chunkify() when
61*7c478bd9Sstevel@tonic-gate  * the message is being transferred from the unsent list to the transmit list;
62*7c478bd9Sstevel@tonic-gate  * in sctp_get_msg_to_send(), when we get the next chunk from the transmit
63*7c478bd9Sstevel@tonic-gate  * list and in sctp_rexmit() when we get the next chunk to be (re)transmitted.
64*7c478bd9Sstevel@tonic-gate  * When we nuke a message in sctp_chunkify(), all we need to do is take it
65*7c478bd9Sstevel@tonic-gate  * out of the unsent list and update sctp_unsent; when a message is deemed
66*7c478bd9Sstevel@tonic-gate  * timed-out in sctp_get_msg_to_send() we can just take it out of the transmit
67*7c478bd9Sstevel@tonic-gate  * list, update sctp_unsent IFF transmission for the message has not yet begun
68*7c478bd9Sstevel@tonic-gate  * (i.e. !SCTP_CHUNK_ISSENT(meta->b_cont)). However, if transmission for the
69*7c478bd9Sstevel@tonic-gate  * message has started, then we cannot just take it out of the list, we need
70*7c478bd9Sstevel@tonic-gate  * to send Forward TSN chunk to the peer so that the peer can clear its
71*7c478bd9Sstevel@tonic-gate  * fragment list for this message. However, we cannot just send the Forward
72*7c478bd9Sstevel@tonic-gate  * TSN in sctp_get_msg_to_send() because there might be unacked chunks for
73*7c478bd9Sstevel@tonic-gate  * messages preceeding this abandoned message. So, we send a Forward TSN
74*7c478bd9Sstevel@tonic-gate  * IFF all messages prior to this abandoned message has been SACKd, if not
75*7c478bd9Sstevel@tonic-gate  * we defer sending the Forward TSN to sctp_cumack(), which will check for
76*7c478bd9Sstevel@tonic-gate  * this condition and send the Forward TSN via sctp_check_abandoned_msg(). In
77*7c478bd9Sstevel@tonic-gate  * sctp_rexmit() when we check for retransmissions, we need to determine if
78*7c478bd9Sstevel@tonic-gate  * the advanced peer ack point can be moved ahead, and if so, send a Forward
79*7c478bd9Sstevel@tonic-gate  * TSN to the peer instead of retransmitting the chunk. Note that when
80*7c478bd9Sstevel@tonic-gate  * we send a Forward TSN for a message, there may be yet unsent chunks for
81*7c478bd9Sstevel@tonic-gate  * this message; we need to mark all such chunks as abandoned, so that
82*7c478bd9Sstevel@tonic-gate  * sctp_cumack() can take the message out of the transmit list, additionally
83*7c478bd9Sstevel@tonic-gate  * sctp_unsent need to be adjusted. Whenever sctp_unsent is updated (i.e.
84*7c478bd9Sstevel@tonic-gate  * decremented when a message/chunk is deemed abandoned), sockfs needs to
85*7c478bd9Sstevel@tonic-gate  * be notified so that it can adjust its idea of the queued message.
86*7c478bd9Sstevel@tonic-gate  */
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate static struct kmem_cache	*sctp_kmem_ftsn_set_cache;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate /* Padding mblk for SCTP chunks. */
93*7c478bd9Sstevel@tonic-gate mblk_t *sctp_pad_mp;
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
96*7c478bd9Sstevel@tonic-gate static boolean_t	sctp_verify_chain(mblk_t *, mblk_t *);
97*7c478bd9Sstevel@tonic-gate #endif
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * Called to allocate a header mblk when sending data to SCTP.
101*7c478bd9Sstevel@tonic-gate  * Data will follow in b_cont of this mblk.
102*7c478bd9Sstevel@tonic-gate  */
103*7c478bd9Sstevel@tonic-gate mblk_t *
104*7c478bd9Sstevel@tonic-gate sctp_alloc_hdr(const char *name, int nlen, const char *control, int clen,
105*7c478bd9Sstevel@tonic-gate     int flags)
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	mblk_t *mp;
108*7c478bd9Sstevel@tonic-gate 	struct T_unitdata_req *tudr;
109*7c478bd9Sstevel@tonic-gate 	size_t size;
110*7c478bd9Sstevel@tonic-gate 	int error;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	size = sizeof (*tudr) + _TPI_ALIGN_TOPT(nlen) + clen;
113*7c478bd9Sstevel@tonic-gate 	size = MAX(size, sizeof (sctp_msg_hdr_t));
114*7c478bd9Sstevel@tonic-gate 	if (flags & SCTP_CAN_BLOCK) {
115*7c478bd9Sstevel@tonic-gate 		mp = allocb_wait(size, BPRI_MED, 0, &error);
116*7c478bd9Sstevel@tonic-gate 	} else {
117*7c478bd9Sstevel@tonic-gate 		mp = allocb(size, BPRI_MED);
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 	if (mp) {
120*7c478bd9Sstevel@tonic-gate 		tudr = (struct T_unitdata_req *)mp->b_rptr;
121*7c478bd9Sstevel@tonic-gate 		tudr->PRIM_type = T_UNITDATA_REQ;
122*7c478bd9Sstevel@tonic-gate 		tudr->DEST_length = nlen;
123*7c478bd9Sstevel@tonic-gate 		tudr->DEST_offset = sizeof (*tudr);
124*7c478bd9Sstevel@tonic-gate 		tudr->OPT_length = clen;
125*7c478bd9Sstevel@tonic-gate 		tudr->OPT_offset = (t_scalar_t)(sizeof (*tudr) +
126*7c478bd9Sstevel@tonic-gate 		    _TPI_ALIGN_TOPT(nlen));
127*7c478bd9Sstevel@tonic-gate 		if (nlen > 0)
128*7c478bd9Sstevel@tonic-gate 			bcopy(name, tudr + 1, nlen);
129*7c478bd9Sstevel@tonic-gate 		if (clen > 0)
130*7c478bd9Sstevel@tonic-gate 			bcopy(control, (char *)tudr + tudr->OPT_offset, clen);
131*7c478bd9Sstevel@tonic-gate 		mp->b_wptr += (tudr ->OPT_offset + clen);
132*7c478bd9Sstevel@tonic-gate 		mp->b_datap->db_type = M_PROTO;
133*7c478bd9Sstevel@tonic-gate 	}
134*7c478bd9Sstevel@tonic-gate 	return (mp);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
138*7c478bd9Sstevel@tonic-gate int
139*7c478bd9Sstevel@tonic-gate sctp_sendmsg(sctp_t *sctp, mblk_t *mp, int flags)
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate 	sctp_faddr_t	*fp = NULL;
142*7c478bd9Sstevel@tonic-gate 	struct T_unitdata_req	*tudr;
143*7c478bd9Sstevel@tonic-gate 	int		error = 0;
144*7c478bd9Sstevel@tonic-gate 	mblk_t		*mproto = mp;
145*7c478bd9Sstevel@tonic-gate 	in6_addr_t	*addr;
146*7c478bd9Sstevel@tonic-gate 	in6_addr_t	tmpaddr;
147*7c478bd9Sstevel@tonic-gate 	uint16_t	sid = sctp->sctp_def_stream;
148*7c478bd9Sstevel@tonic-gate 	uint32_t	ppid = sctp->sctp_def_ppid;
149*7c478bd9Sstevel@tonic-gate 	uint32_t	context = sctp->sctp_def_context;
150*7c478bd9Sstevel@tonic-gate 	uint16_t	msg_flags = sctp->sctp_def_flags;
151*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr_t	*sctp_msg_hdr;
152*7c478bd9Sstevel@tonic-gate 	uint32_t	msg_len = 0;
153*7c478bd9Sstevel@tonic-gate 	uint32_t	timetolive = sctp->sctp_def_timetolive;
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	ASSERT(DB_TYPE(mproto) == M_PROTO);
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	mp = mp->b_cont;
158*7c478bd9Sstevel@tonic-gate 	ASSERT(mp == NULL || DB_TYPE(mp) == M_DATA);
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	tudr = (struct T_unitdata_req *)mproto->b_rptr;
161*7c478bd9Sstevel@tonic-gate 	ASSERT(tudr->PRIM_type == T_UNITDATA_REQ);
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	/* Get destination address, if specified */
164*7c478bd9Sstevel@tonic-gate 	if (tudr->DEST_length > 0) {
165*7c478bd9Sstevel@tonic-gate 		sin_t *sin;
166*7c478bd9Sstevel@tonic-gate 		sin6_t *sin6;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)
169*7c478bd9Sstevel@tonic-gate 		    (mproto->b_rptr + tudr->DEST_offset);
170*7c478bd9Sstevel@tonic-gate 		switch (sin->sin_family) {
171*7c478bd9Sstevel@tonic-gate 		case AF_INET:
172*7c478bd9Sstevel@tonic-gate 			if (tudr->DEST_length < sizeof (*sin)) {
173*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
174*7c478bd9Sstevel@tonic-gate 			}
175*7c478bd9Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &tmpaddr);
176*7c478bd9Sstevel@tonic-gate 			addr = &tmpaddr;
177*7c478bd9Sstevel@tonic-gate 			break;
178*7c478bd9Sstevel@tonic-gate 		case AF_INET6:
179*7c478bd9Sstevel@tonic-gate 			if (tudr->DEST_length < sizeof (*sin6)) {
180*7c478bd9Sstevel@tonic-gate 				return (EINVAL);
181*7c478bd9Sstevel@tonic-gate 			}
182*7c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)
183*7c478bd9Sstevel@tonic-gate 			    (mproto->b_rptr + tudr->DEST_offset);
184*7c478bd9Sstevel@tonic-gate 			addr = &sin6->sin6_addr;
185*7c478bd9Sstevel@tonic-gate 			break;
186*7c478bd9Sstevel@tonic-gate 		default:
187*7c478bd9Sstevel@tonic-gate 			return (EAFNOSUPPORT);
188*7c478bd9Sstevel@tonic-gate 		}
189*7c478bd9Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, addr);
190*7c478bd9Sstevel@tonic-gate 		if (fp == NULL) {
191*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
192*7c478bd9Sstevel@tonic-gate 		}
193*7c478bd9Sstevel@tonic-gate 	}
194*7c478bd9Sstevel@tonic-gate 	/* Ancillary Data? */
195*7c478bd9Sstevel@tonic-gate 	if (tudr->OPT_length > 0) {
196*7c478bd9Sstevel@tonic-gate 		struct cmsghdr		*cmsg;
197*7c478bd9Sstevel@tonic-gate 		char			*cend;
198*7c478bd9Sstevel@tonic-gate 		struct sctp_sndrcvinfo	*sndrcv;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)(mproto->b_rptr + tudr->OPT_offset);
201*7c478bd9Sstevel@tonic-gate 		cend = ((char *)cmsg + tudr->OPT_length);
202*7c478bd9Sstevel@tonic-gate 		ASSERT(cend <= (char *)mproto->b_wptr);
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 		for (;;) {
205*7c478bd9Sstevel@tonic-gate 			if ((char *)(cmsg + 1) > cend ||
206*7c478bd9Sstevel@tonic-gate 			    ((char *)cmsg + cmsg->cmsg_len) > cend) {
207*7c478bd9Sstevel@tonic-gate 				break;
208*7c478bd9Sstevel@tonic-gate 			}
209*7c478bd9Sstevel@tonic-gate 			if ((cmsg->cmsg_level == IPPROTO_SCTP) &&
210*7c478bd9Sstevel@tonic-gate 			    (cmsg->cmsg_type == SCTP_SNDRCV)) {
211*7c478bd9Sstevel@tonic-gate 				if (cmsg->cmsg_len <
212*7c478bd9Sstevel@tonic-gate 				    (sizeof (*sndrcv) + sizeof (*cmsg))) {
213*7c478bd9Sstevel@tonic-gate 					return (EINVAL);
214*7c478bd9Sstevel@tonic-gate 				}
215*7c478bd9Sstevel@tonic-gate 				sndrcv = (struct sctp_sndrcvinfo *)(cmsg + 1);
216*7c478bd9Sstevel@tonic-gate 				sid = sndrcv->sinfo_stream;
217*7c478bd9Sstevel@tonic-gate 				msg_flags = sndrcv->sinfo_flags;
218*7c478bd9Sstevel@tonic-gate 				ppid = sndrcv->sinfo_ppid;
219*7c478bd9Sstevel@tonic-gate 				context = sndrcv->sinfo_context;
220*7c478bd9Sstevel@tonic-gate 				timetolive = sndrcv->sinfo_timetolive;
221*7c478bd9Sstevel@tonic-gate 				break;
222*7c478bd9Sstevel@tonic-gate 			}
223*7c478bd9Sstevel@tonic-gate 			if (cmsg->cmsg_len > 0)
224*7c478bd9Sstevel@tonic-gate 				cmsg = CMSG_NEXT(cmsg);
225*7c478bd9Sstevel@tonic-gate 			else
226*7c478bd9Sstevel@tonic-gate 				break;
227*7c478bd9Sstevel@tonic-gate 		}
228*7c478bd9Sstevel@tonic-gate 	}
229*7c478bd9Sstevel@tonic-gate 	if (msg_flags & MSG_ABORT) {
230*7c478bd9Sstevel@tonic-gate 		if (mp && mp->b_cont) {
231*7c478bd9Sstevel@tonic-gate 			mblk_t *pump = msgpullup(mp, -1);
232*7c478bd9Sstevel@tonic-gate 			if (!pump) {
233*7c478bd9Sstevel@tonic-gate 				return (ENOMEM);
234*7c478bd9Sstevel@tonic-gate 			}
235*7c478bd9Sstevel@tonic-gate 			freemsg(mp);
236*7c478bd9Sstevel@tonic-gate 			mp = pump;
237*7c478bd9Sstevel@tonic-gate 			mproto->b_cont = mp;
238*7c478bd9Sstevel@tonic-gate 		}
239*7c478bd9Sstevel@tonic-gate 		RUN_SCTP(sctp);
240*7c478bd9Sstevel@tonic-gate 		sctp_user_abort(sctp, mp, B_TRUE);
241*7c478bd9Sstevel@tonic-gate 		sctp_clean_death(sctp, ECONNRESET);
242*7c478bd9Sstevel@tonic-gate 		freemsg(mproto);
243*7c478bd9Sstevel@tonic-gate 		goto process_sendq;
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 	if (mp == NULL)
246*7c478bd9Sstevel@tonic-gate 		goto done;
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	/* Reject any new data requests if we are shutting down */
251*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_ESTABLISHED) {
252*7c478bd9Sstevel@tonic-gate 		error = EPIPE;
253*7c478bd9Sstevel@tonic-gate 		goto unlock_done;
254*7c478bd9Sstevel@tonic-gate 	}
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	/* Re-use the mproto to store relevant info. */
257*7c478bd9Sstevel@tonic-gate 	ASSERT(MBLKSIZE(mproto) >= sizeof (*sctp_msg_hdr));
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	mproto->b_rptr = mproto->b_datap->db_base;
260*7c478bd9Sstevel@tonic-gate 	mproto->b_wptr = mproto->b_rptr + sizeof (*sctp_msg_hdr);
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr = (sctp_msg_hdr_t *)mproto->b_rptr;
263*7c478bd9Sstevel@tonic-gate 	bzero(sctp_msg_hdr, sizeof (*sctp_msg_hdr));
264*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr->smh_context = context;
265*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr->smh_sid = sid;
266*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr->smh_ppid = ppid;
267*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr->smh_flags = msg_flags;
268*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr->smh_ttl = MSEC_TO_TICK(timetolive);
269*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr->smh_tob = lbolt64;
270*7c478bd9Sstevel@tonic-gate 	for (; mp != NULL; mp = mp->b_cont)
271*7c478bd9Sstevel@tonic-gate 		msg_len += MBLKL(mp);
272*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr->smh_msglen = msg_len;
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	/* User requested specific destination */
275*7c478bd9Sstevel@tonic-gate 	SCTP_SET_CHUNK_DEST(mproto, fp);
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_COOKIE_ECHOED &&
278*7c478bd9Sstevel@tonic-gate 	    sid >= sctp->sctp_num_ostr) {
279*7c478bd9Sstevel@tonic-gate 		/* Send sendfail event */
280*7c478bd9Sstevel@tonic-gate 		sctp_sendfail_event(sctp, dupmsg(mproto), SCTP_ERR_BAD_SID,
281*7c478bd9Sstevel@tonic-gate 		    B_FALSE);
282*7c478bd9Sstevel@tonic-gate 		error = EINVAL;
283*7c478bd9Sstevel@tonic-gate 		goto unlock_done;
284*7c478bd9Sstevel@tonic-gate 	}
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 	/* no data */
287*7c478bd9Sstevel@tonic-gate 	if (msg_len == 0) {
288*7c478bd9Sstevel@tonic-gate 		sctp_sendfail_event(sctp, dupmsg(mproto),
289*7c478bd9Sstevel@tonic-gate 		    SCTP_ERR_NO_USR_DATA, B_FALSE);
290*7c478bd9Sstevel@tonic-gate 		error = EINVAL;
291*7c478bd9Sstevel@tonic-gate 		goto unlock_done;
292*7c478bd9Sstevel@tonic-gate 	}
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	/* Add it to the unsent list */
295*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_xmit_unsent == NULL) {
296*7c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = mproto;
297*7c478bd9Sstevel@tonic-gate 	} else {
298*7c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_unsent_tail->b_next = mproto;
299*7c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_unsent_tail = mproto;
300*7c478bd9Sstevel@tonic-gate 	}
301*7c478bd9Sstevel@tonic-gate 	sctp->sctp_unsent += msg_len;
302*7c478bd9Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_msgcount);
303*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state == SCTPS_ESTABLISHED)
304*7c478bd9Sstevel@tonic-gate 		sctp_output(sctp);
305*7c478bd9Sstevel@tonic-gate process_sendq:
306*7c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
307*7c478bd9Sstevel@tonic-gate 	sctp_process_sendq(sctp);
308*7c478bd9Sstevel@tonic-gate 	return (0);
309*7c478bd9Sstevel@tonic-gate unlock_done:
310*7c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
311*7c478bd9Sstevel@tonic-gate done:
312*7c478bd9Sstevel@tonic-gate 	return (error);
313*7c478bd9Sstevel@tonic-gate }
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate void
316*7c478bd9Sstevel@tonic-gate sctp_chunkify(sctp_t *sctp, int first_len, int bytes_to_send)
317*7c478bd9Sstevel@tonic-gate {
318*7c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
319*7c478bd9Sstevel@tonic-gate 	mblk_t			*chunk_mp;
320*7c478bd9Sstevel@tonic-gate 	mblk_t			*chunk_head;
321*7c478bd9Sstevel@tonic-gate 	mblk_t			*chunk_hdr;
322*7c478bd9Sstevel@tonic-gate 	mblk_t			*chunk_tail = NULL;
323*7c478bd9Sstevel@tonic-gate 	int			count;
324*7c478bd9Sstevel@tonic-gate 	int			chunksize;
325*7c478bd9Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
326*7c478bd9Sstevel@tonic-gate 	mblk_t			*mdblk = sctp->sctp_xmit_unsent;
327*7c478bd9Sstevel@tonic-gate 	sctp_faddr_t		*fp;
328*7c478bd9Sstevel@tonic-gate 	sctp_faddr_t		*fp1;
329*7c478bd9Sstevel@tonic-gate 	size_t			xtralen;
330*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr_t		*msg_hdr;
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 	fp = SCTP_CHUNK_DEST(mdblk);
333*7c478bd9Sstevel@tonic-gate 	if (fp == NULL)
334*7c478bd9Sstevel@tonic-gate 		fp = sctp->sctp_current;
335*7c478bd9Sstevel@tonic-gate 	if (fp->isv4)
336*7c478bd9Sstevel@tonic-gate 		xtralen = sctp->sctp_hdr_len + sctp_wroff_xtra + sizeof (*sdc);
337*7c478bd9Sstevel@tonic-gate 	else
338*7c478bd9Sstevel@tonic-gate 		xtralen = sctp->sctp_hdr6_len + sctp_wroff_xtra + sizeof (*sdc);
339*7c478bd9Sstevel@tonic-gate 	count = chunksize = first_len - sizeof (*sdc);
340*7c478bd9Sstevel@tonic-gate nextmsg:
341*7c478bd9Sstevel@tonic-gate 	chunk_mp = mdblk->b_cont;
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	/*
344*7c478bd9Sstevel@tonic-gate 	 * If this partially chunked, we ignore the first_len for now
345*7c478bd9Sstevel@tonic-gate 	 * and use the one already present. For the unchunked bits, we
346*7c478bd9Sstevel@tonic-gate 	 * use the length of the last chunk.
347*7c478bd9Sstevel@tonic-gate 	 */
348*7c478bd9Sstevel@tonic-gate 	if (SCTP_IS_MSG_CHUNKED(mdblk)) {
349*7c478bd9Sstevel@tonic-gate 		int	chunk_len;
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 		ASSERT(chunk_mp->b_next != NULL);
352*7c478bd9Sstevel@tonic-gate 		mdblk->b_cont = chunk_mp->b_next;
353*7c478bd9Sstevel@tonic-gate 		chunk_mp->b_next = NULL;
354*7c478bd9Sstevel@tonic-gate 		SCTP_MSG_CLEAR_CHUNKED(mdblk);
355*7c478bd9Sstevel@tonic-gate 		mp = mdblk->b_cont;
356*7c478bd9Sstevel@tonic-gate 		while (mp->b_next != NULL)
357*7c478bd9Sstevel@tonic-gate 			mp = mp->b_next;
358*7c478bd9Sstevel@tonic-gate 		chunk_len = ntohs(((sctp_data_hdr_t *)mp->b_rptr)->sdh_len);
359*7c478bd9Sstevel@tonic-gate 		if (fp->sfa_pmss - chunk_len > sizeof (*sdc))
360*7c478bd9Sstevel@tonic-gate 			count = chunksize = fp->sfa_pmss - chunk_len;
361*7c478bd9Sstevel@tonic-gate 		else
362*7c478bd9Sstevel@tonic-gate 			count = chunksize = fp->sfa_pmss;
363*7c478bd9Sstevel@tonic-gate 		count = chunksize = count - sizeof (*sdc);
364*7c478bd9Sstevel@tonic-gate 	} else {
365*7c478bd9Sstevel@tonic-gate 		msg_hdr = (sctp_msg_hdr_t *)mdblk->b_rptr;
366*7c478bd9Sstevel@tonic-gate 		if (SCTP_MSG_TO_BE_ABANDONED(mdblk, msg_hdr, sctp)) {
367*7c478bd9Sstevel@tonic-gate 			sctp->sctp_xmit_unsent = mdblk->b_next;
368*7c478bd9Sstevel@tonic-gate 			if (sctp->sctp_xmit_unsent == NULL)
369*7c478bd9Sstevel@tonic-gate 				sctp->sctp_xmit_unsent_tail = NULL;
370*7c478bd9Sstevel@tonic-gate 			ASSERT(sctp->sctp_unsent >= msg_hdr->smh_msglen);
371*7c478bd9Sstevel@tonic-gate 			sctp->sctp_unsent -= msg_hdr->smh_msglen;
372*7c478bd9Sstevel@tonic-gate 			mdblk->b_next = NULL;
373*7c478bd9Sstevel@tonic-gate 			BUMP_LOCAL(sctp->sctp_prsctpdrop);
374*7c478bd9Sstevel@tonic-gate 			/*
375*7c478bd9Sstevel@tonic-gate 			 * Update ULP the amount of queued data, which is
376*7c478bd9Sstevel@tonic-gate 			 * sent-unack'ed + unsent.
377*7c478bd9Sstevel@tonic-gate 			 */
378*7c478bd9Sstevel@tonic-gate 			if (!SCTP_IS_DETACHED(sctp)) {
379*7c478bd9Sstevel@tonic-gate 				sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
380*7c478bd9Sstevel@tonic-gate 				    sctp->sctp_unacked + sctp->sctp_unsent);
381*7c478bd9Sstevel@tonic-gate 			}
382*7c478bd9Sstevel@tonic-gate 			sctp_sendfail_event(sctp, mdblk, 0, B_FALSE);
383*7c478bd9Sstevel@tonic-gate 			goto try_next;
384*7c478bd9Sstevel@tonic-gate 		}
385*7c478bd9Sstevel@tonic-gate 		mdblk->b_cont = NULL;
386*7c478bd9Sstevel@tonic-gate 	}
387*7c478bd9Sstevel@tonic-gate 	msg_hdr = (sctp_msg_hdr_t *)mdblk->b_rptr;
388*7c478bd9Sstevel@tonic-gate nextchunk:
389*7c478bd9Sstevel@tonic-gate 	chunk_head = chunk_mp;
390*7c478bd9Sstevel@tonic-gate 	chunk_tail = NULL;
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	/* Skip as many mblk's as we need */
393*7c478bd9Sstevel@tonic-gate 	while (chunk_mp != NULL && ((count - MBLKL(chunk_mp)) >= 0)) {
394*7c478bd9Sstevel@tonic-gate 		count -= MBLKL(chunk_mp);
395*7c478bd9Sstevel@tonic-gate 		chunk_tail = chunk_mp;
396*7c478bd9Sstevel@tonic-gate 		chunk_mp = chunk_mp->b_cont;
397*7c478bd9Sstevel@tonic-gate 	}
398*7c478bd9Sstevel@tonic-gate 	/* Split the chain, if needed */
399*7c478bd9Sstevel@tonic-gate 	if (chunk_mp != NULL) {
400*7c478bd9Sstevel@tonic-gate 		if (count > 0) {
401*7c478bd9Sstevel@tonic-gate 			mblk_t	*split_mp = dupb(chunk_mp);
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 			if (split_mp == NULL) {
404*7c478bd9Sstevel@tonic-gate 				if (mdblk->b_cont == NULL) {
405*7c478bd9Sstevel@tonic-gate 					mdblk->b_cont = chunk_head;
406*7c478bd9Sstevel@tonic-gate 				} else  {
407*7c478bd9Sstevel@tonic-gate 					SCTP_MSG_SET_CHUNKED(mdblk);
408*7c478bd9Sstevel@tonic-gate 					ASSERT(chunk_head->b_next == NULL);
409*7c478bd9Sstevel@tonic-gate 					chunk_head->b_next = mdblk->b_cont;
410*7c478bd9Sstevel@tonic-gate 					mdblk->b_cont = chunk_head;
411*7c478bd9Sstevel@tonic-gate 				}
412*7c478bd9Sstevel@tonic-gate 				return;
413*7c478bd9Sstevel@tonic-gate 			}
414*7c478bd9Sstevel@tonic-gate 			if (chunk_tail != NULL) {
415*7c478bd9Sstevel@tonic-gate 				chunk_tail->b_cont = split_mp;
416*7c478bd9Sstevel@tonic-gate 				chunk_tail = chunk_tail->b_cont;
417*7c478bd9Sstevel@tonic-gate 			} else {
418*7c478bd9Sstevel@tonic-gate 				chunk_head = chunk_tail = split_mp;
419*7c478bd9Sstevel@tonic-gate 			}
420*7c478bd9Sstevel@tonic-gate 			chunk_tail->b_wptr = chunk_tail->b_rptr + count;
421*7c478bd9Sstevel@tonic-gate 			chunk_mp->b_rptr = chunk_tail->b_wptr;
422*7c478bd9Sstevel@tonic-gate 			count = 0;
423*7c478bd9Sstevel@tonic-gate 		} else if (chunk_tail == NULL) {
424*7c478bd9Sstevel@tonic-gate 			goto next;
425*7c478bd9Sstevel@tonic-gate 		} else {
426*7c478bd9Sstevel@tonic-gate 			chunk_tail->b_cont = NULL;
427*7c478bd9Sstevel@tonic-gate 		}
428*7c478bd9Sstevel@tonic-gate 	}
429*7c478bd9Sstevel@tonic-gate 	/* Alloc chunk hdr, if needed */
430*7c478bd9Sstevel@tonic-gate 	if (DB_REF(chunk_head) > 1 ||
431*7c478bd9Sstevel@tonic-gate 	    ((intptr_t)chunk_head->b_rptr) & (SCTP_ALIGN - 1) ||
432*7c478bd9Sstevel@tonic-gate 	    MBLKHEAD(chunk_head) < sizeof (*sdc)) {
433*7c478bd9Sstevel@tonic-gate 		if ((chunk_hdr = allocb(xtralen, BPRI_MED)) == NULL) {
434*7c478bd9Sstevel@tonic-gate 			if (mdblk->b_cont == NULL) {
435*7c478bd9Sstevel@tonic-gate 				if (chunk_mp != NULL)
436*7c478bd9Sstevel@tonic-gate 					linkb(chunk_head, chunk_mp);
437*7c478bd9Sstevel@tonic-gate 				mdblk->b_cont = chunk_head;
438*7c478bd9Sstevel@tonic-gate 			} else {
439*7c478bd9Sstevel@tonic-gate 				SCTP_MSG_SET_CHUNKED(mdblk);
440*7c478bd9Sstevel@tonic-gate 				if (chunk_mp != NULL)
441*7c478bd9Sstevel@tonic-gate 					linkb(chunk_head, chunk_mp);
442*7c478bd9Sstevel@tonic-gate 				ASSERT(chunk_head->b_next == NULL);
443*7c478bd9Sstevel@tonic-gate 				chunk_head->b_next = mdblk->b_cont;
444*7c478bd9Sstevel@tonic-gate 				mdblk->b_cont = chunk_head;
445*7c478bd9Sstevel@tonic-gate 			}
446*7c478bd9Sstevel@tonic-gate 			return;
447*7c478bd9Sstevel@tonic-gate 		}
448*7c478bd9Sstevel@tonic-gate 		chunk_hdr->b_rptr += xtralen - sizeof (*sdc);
449*7c478bd9Sstevel@tonic-gate 		chunk_hdr->b_wptr = chunk_hdr->b_rptr + sizeof (*sdc);
450*7c478bd9Sstevel@tonic-gate 		chunk_hdr->b_cont = chunk_head;
451*7c478bd9Sstevel@tonic-gate 	} else {
452*7c478bd9Sstevel@tonic-gate 		chunk_hdr = chunk_head;
453*7c478bd9Sstevel@tonic-gate 		chunk_hdr->b_rptr -= sizeof (*sdc);
454*7c478bd9Sstevel@tonic-gate 	}
455*7c478bd9Sstevel@tonic-gate 	ASSERT(chunk_hdr->b_datap->db_ref == 1);
456*7c478bd9Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)chunk_hdr->b_rptr;
457*7c478bd9Sstevel@tonic-gate 	sdc->sdh_id = CHUNK_DATA;
458*7c478bd9Sstevel@tonic-gate 	sdc->sdh_flags = 0;
459*7c478bd9Sstevel@tonic-gate 	sdc->sdh_len = htons(sizeof (*sdc) + chunksize - count);
460*7c478bd9Sstevel@tonic-gate 	ASSERT(sdc->sdh_len);
461*7c478bd9Sstevel@tonic-gate 	sdc->sdh_sid = htons(msg_hdr->smh_sid);
462*7c478bd9Sstevel@tonic-gate 	/*
463*7c478bd9Sstevel@tonic-gate 	 * We defer assigning the SSN just before sending the chunk, else
464*7c478bd9Sstevel@tonic-gate 	 * if we drop the chunk in sctp_get_msg_to_send(), we would need
465*7c478bd9Sstevel@tonic-gate 	 * to send a Forward TSN to let the peer know. Some more comments
466*7c478bd9Sstevel@tonic-gate 	 * about this in sctp_impl.h for SCTP_CHUNK_SENT.
467*7c478bd9Sstevel@tonic-gate 	 */
468*7c478bd9Sstevel@tonic-gate 	sdc->sdh_payload_id = msg_hdr->smh_ppid;
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 	if (mdblk->b_cont == NULL) {
471*7c478bd9Sstevel@tonic-gate 		mdblk->b_cont = chunk_hdr;
472*7c478bd9Sstevel@tonic-gate 		SCTP_DATA_SET_BBIT(sdc);
473*7c478bd9Sstevel@tonic-gate 	} else {
474*7c478bd9Sstevel@tonic-gate 		mp = mdblk->b_cont;
475*7c478bd9Sstevel@tonic-gate 		while (mp->b_next != NULL)
476*7c478bd9Sstevel@tonic-gate 			mp = mp->b_next;
477*7c478bd9Sstevel@tonic-gate 		mp->b_next = chunk_hdr;
478*7c478bd9Sstevel@tonic-gate 	}
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate 	bytes_to_send -= (chunksize - count);
481*7c478bd9Sstevel@tonic-gate 	if (chunk_mp != NULL) {
482*7c478bd9Sstevel@tonic-gate next:
483*7c478bd9Sstevel@tonic-gate 		count = chunksize = fp->sfa_pmss - sizeof (*sdc);
484*7c478bd9Sstevel@tonic-gate 		goto nextchunk;
485*7c478bd9Sstevel@tonic-gate 	}
486*7c478bd9Sstevel@tonic-gate 	SCTP_DATA_SET_EBIT(sdc);
487*7c478bd9Sstevel@tonic-gate 	sctp->sctp_xmit_unsent = mdblk->b_next;
488*7c478bd9Sstevel@tonic-gate 	if (mdblk->b_next == NULL) {
489*7c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_unsent_tail = NULL;
490*7c478bd9Sstevel@tonic-gate 	}
491*7c478bd9Sstevel@tonic-gate 	mdblk->b_next = NULL;
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_xmit_tail == NULL) {
494*7c478bd9Sstevel@tonic-gate 		sctp->sctp_xmit_head = sctp->sctp_xmit_tail = mdblk;
495*7c478bd9Sstevel@tonic-gate 	} else {
496*7c478bd9Sstevel@tonic-gate 		mp = sctp->sctp_xmit_tail;
497*7c478bd9Sstevel@tonic-gate 		while (mp->b_next != NULL)
498*7c478bd9Sstevel@tonic-gate 			mp = mp->b_next;
499*7c478bd9Sstevel@tonic-gate 		mp->b_next = mdblk;
500*7c478bd9Sstevel@tonic-gate 		mdblk->b_prev = mp;
501*7c478bd9Sstevel@tonic-gate 	}
502*7c478bd9Sstevel@tonic-gate try_next:
503*7c478bd9Sstevel@tonic-gate 	if (bytes_to_send > 0 && sctp->sctp_xmit_unsent != NULL) {
504*7c478bd9Sstevel@tonic-gate 		mdblk = sctp->sctp_xmit_unsent;
505*7c478bd9Sstevel@tonic-gate 		fp1 = SCTP_CHUNK_DEST(mdblk);
506*7c478bd9Sstevel@tonic-gate 		if (fp1 == NULL)
507*7c478bd9Sstevel@tonic-gate 			fp1 = sctp->sctp_current;
508*7c478bd9Sstevel@tonic-gate 		if (fp == fp1) {
509*7c478bd9Sstevel@tonic-gate 			size_t len = MBLKL(mdblk->b_cont);
510*7c478bd9Sstevel@tonic-gate 			if ((count > 0) &&
511*7c478bd9Sstevel@tonic-gate 			    ((len > fp->sfa_pmss - sizeof (*sdc)) ||
512*7c478bd9Sstevel@tonic-gate 				(len <= count))) {
513*7c478bd9Sstevel@tonic-gate 				count -= sizeof (*sdc);
514*7c478bd9Sstevel@tonic-gate 				count = chunksize = count - (count & 0x3);
515*7c478bd9Sstevel@tonic-gate 			} else {
516*7c478bd9Sstevel@tonic-gate 				count = chunksize = fp->sfa_pmss -
517*7c478bd9Sstevel@tonic-gate 				    sizeof (*sdc);
518*7c478bd9Sstevel@tonic-gate 			}
519*7c478bd9Sstevel@tonic-gate 		} else {
520*7c478bd9Sstevel@tonic-gate 			if (fp1->isv4)
521*7c478bd9Sstevel@tonic-gate 				xtralen = sctp->sctp_hdr_len;
522*7c478bd9Sstevel@tonic-gate 			else
523*7c478bd9Sstevel@tonic-gate 				xtralen = sctp->sctp_hdr6_len;
524*7c478bd9Sstevel@tonic-gate 			xtralen += sctp_wroff_xtra + sizeof (*sdc);
525*7c478bd9Sstevel@tonic-gate 			count = chunksize = fp1->sfa_pmss - sizeof (*sdc);
526*7c478bd9Sstevel@tonic-gate 			fp = fp1;
527*7c478bd9Sstevel@tonic-gate 		}
528*7c478bd9Sstevel@tonic-gate 		goto nextmsg;
529*7c478bd9Sstevel@tonic-gate 	}
530*7c478bd9Sstevel@tonic-gate }
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate void
533*7c478bd9Sstevel@tonic-gate sctp_free_msg(mblk_t *ump)
534*7c478bd9Sstevel@tonic-gate {
535*7c478bd9Sstevel@tonic-gate 	mblk_t *mp, *nmp;
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 	for (mp = ump->b_cont; mp; mp = nmp) {
538*7c478bd9Sstevel@tonic-gate 		nmp = mp->b_next;
539*7c478bd9Sstevel@tonic-gate 		mp->b_next = mp->b_prev = NULL;
540*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
541*7c478bd9Sstevel@tonic-gate 	}
542*7c478bd9Sstevel@tonic-gate 	ASSERT(!ump->b_prev);
543*7c478bd9Sstevel@tonic-gate 	ump->b_next = NULL;
544*7c478bd9Sstevel@tonic-gate 	freeb(ump);
545*7c478bd9Sstevel@tonic-gate }
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate mblk_t *
548*7c478bd9Sstevel@tonic-gate sctp_add_proto_hdr(sctp_t *sctp, sctp_faddr_t *fp, mblk_t *mp, int sacklen)
549*7c478bd9Sstevel@tonic-gate {
550*7c478bd9Sstevel@tonic-gate 	int hdrlen;
551*7c478bd9Sstevel@tonic-gate 	char *hdr;
552*7c478bd9Sstevel@tonic-gate 	int isv4 = fp->isv4;
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate 	if (isv4) {
555*7c478bd9Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
556*7c478bd9Sstevel@tonic-gate 		hdr = sctp->sctp_iphc;
557*7c478bd9Sstevel@tonic-gate 	} else {
558*7c478bd9Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
559*7c478bd9Sstevel@tonic-gate 		hdr = sctp->sctp_iphc6;
560*7c478bd9Sstevel@tonic-gate 	}
561*7c478bd9Sstevel@tonic-gate 	if (SCTP_IS_ADDR_UNSPEC(fp->isv4, fp->saddr)) {
562*7c478bd9Sstevel@tonic-gate 		sctp_ire2faddr(sctp, fp);
563*7c478bd9Sstevel@tonic-gate 	} else if (fp->ire == NULL) {
564*7c478bd9Sstevel@tonic-gate 		ipaddr_t addr4;
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate 		if (isv4) {
567*7c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr, addr4);
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate 			fp->ire = ire_cache_lookup(addr4, sctp->sctp_zoneid);
570*7c478bd9Sstevel@tonic-gate 		} else {
571*7c478bd9Sstevel@tonic-gate 			fp->ire = ire_cache_lookup_v6(&fp->faddr,
572*7c478bd9Sstevel@tonic-gate 			    sctp->sctp_zoneid);
573*7c478bd9Sstevel@tonic-gate 		}
574*7c478bd9Sstevel@tonic-gate 		if (fp->ire != NULL) {
575*7c478bd9Sstevel@tonic-gate 			IRE_REFHOLD_NOTR(fp->ire);
576*7c478bd9Sstevel@tonic-gate 			IRE_REFRELE(fp->ire);
577*7c478bd9Sstevel@tonic-gate 		}
578*7c478bd9Sstevel@tonic-gate 		if (fp->ire != NULL && fp->ire->ire_type == IRE_LOOPBACK &&
579*7c478bd9Sstevel@tonic-gate 		    !sctp->sctp_loopback) {
580*7c478bd9Sstevel@tonic-gate 			sctp->sctp_loopback = 1;
581*7c478bd9Sstevel@tonic-gate 		}
582*7c478bd9Sstevel@tonic-gate 	}
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 	/* Copy in IP header. */
585*7c478bd9Sstevel@tonic-gate 	if ((mp->b_rptr - mp->b_datap->db_base) <
586*7c478bd9Sstevel@tonic-gate 	    (sctp_wroff_xtra + hdrlen + sacklen) || DB_REF(mp) > 2) {
587*7c478bd9Sstevel@tonic-gate 		mblk_t *nmp;
588*7c478bd9Sstevel@tonic-gate 		/*
589*7c478bd9Sstevel@tonic-gate 		 * This can happen if IP headers are adjusted after
590*7c478bd9Sstevel@tonic-gate 		 * data was moved into chunks, or during retransmission,
591*7c478bd9Sstevel@tonic-gate 		 * or things like snoop is running.
592*7c478bd9Sstevel@tonic-gate 		 */
593*7c478bd9Sstevel@tonic-gate 		nmp = allocb(sctp_wroff_xtra + hdrlen + sacklen, BPRI_MED);
594*7c478bd9Sstevel@tonic-gate 		if (nmp == NULL) {
595*7c478bd9Sstevel@tonic-gate 			return (NULL);
596*7c478bd9Sstevel@tonic-gate 		}
597*7c478bd9Sstevel@tonic-gate 		nmp->b_rptr += sctp_wroff_xtra;
598*7c478bd9Sstevel@tonic-gate 		nmp->b_wptr = nmp->b_rptr + hdrlen + sacklen;
599*7c478bd9Sstevel@tonic-gate 		nmp->b_cont = mp;
600*7c478bd9Sstevel@tonic-gate 		mp = nmp;
601*7c478bd9Sstevel@tonic-gate 	} else {
602*7c478bd9Sstevel@tonic-gate 		mp->b_rptr -= (hdrlen + sacklen);
603*7c478bd9Sstevel@tonic-gate 	}
604*7c478bd9Sstevel@tonic-gate 	bcopy(hdr, mp->b_rptr, hdrlen);
605*7c478bd9Sstevel@tonic-gate 	if (sacklen) {
606*7c478bd9Sstevel@tonic-gate 		sctp_fill_sack(sctp, mp->b_rptr + hdrlen, sacklen);
607*7c478bd9Sstevel@tonic-gate 	}
608*7c478bd9Sstevel@tonic-gate 	if (fp != sctp->sctp_current) {
609*7c478bd9Sstevel@tonic-gate 		/* change addresses in header */
610*7c478bd9Sstevel@tonic-gate 		if (isv4) {
611*7c478bd9Sstevel@tonic-gate 			ipha_t *iph = (ipha_t *)mp->b_rptr;
612*7c478bd9Sstevel@tonic-gate 
613*7c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst);
614*7c478bd9Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr)) {
615*7c478bd9Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&fp->saddr,
616*7c478bd9Sstevel@tonic-gate 				    iph->ipha_src);
617*7c478bd9Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
618*7c478bd9Sstevel@tonic-gate 				iph->ipha_src = INADDR_ANY;
619*7c478bd9Sstevel@tonic-gate 			}
620*7c478bd9Sstevel@tonic-gate 		} else {
621*7c478bd9Sstevel@tonic-gate 			((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr;
622*7c478bd9Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&fp->saddr)) {
623*7c478bd9Sstevel@tonic-gate 				((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr;
624*7c478bd9Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
625*7c478bd9Sstevel@tonic-gate 				V6_SET_ZERO(((ip6_t *)(mp->b_rptr))->ip6_src);
626*7c478bd9Sstevel@tonic-gate 			}
627*7c478bd9Sstevel@tonic-gate 		}
628*7c478bd9Sstevel@tonic-gate 	}
629*7c478bd9Sstevel@tonic-gate 	/*
630*7c478bd9Sstevel@tonic-gate 	 * IP will not free this IRE if it is condemned.  SCTP needs to
631*7c478bd9Sstevel@tonic-gate 	 * free it.
632*7c478bd9Sstevel@tonic-gate 	 */
633*7c478bd9Sstevel@tonic-gate 	if ((fp->ire != NULL) && (fp->ire->ire_marks & IRE_MARK_CONDEMNED)) {
634*7c478bd9Sstevel@tonic-gate 		IRE_REFRELE_NOTR(fp->ire);
635*7c478bd9Sstevel@tonic-gate 		fp->ire = NULL;
636*7c478bd9Sstevel@tonic-gate 	}
637*7c478bd9Sstevel@tonic-gate 
638*7c478bd9Sstevel@tonic-gate 	/* Stash the conn and ire ptr info for IP */
639*7c478bd9Sstevel@tonic-gate 	SCTP_STASH_IPINFO(mp, fp->ire);
640*7c478bd9Sstevel@tonic-gate 
641*7c478bd9Sstevel@tonic-gate 	return (mp);
642*7c478bd9Sstevel@tonic-gate }
643*7c478bd9Sstevel@tonic-gate 
644*7c478bd9Sstevel@tonic-gate /*
645*7c478bd9Sstevel@tonic-gate  * SCTP requires every chunk to be padded so that the total length
646*7c478bd9Sstevel@tonic-gate  * is a multiple of SCTP_ALIGN.  This function returns a mblk with
647*7c478bd9Sstevel@tonic-gate  * the specified pad length.
648*7c478bd9Sstevel@tonic-gate  */
649*7c478bd9Sstevel@tonic-gate static mblk_t *
650*7c478bd9Sstevel@tonic-gate sctp_get_padding(int pad)
651*7c478bd9Sstevel@tonic-gate {
652*7c478bd9Sstevel@tonic-gate 	mblk_t *fill;
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate 	ASSERT(pad < SCTP_ALIGN);
655*7c478bd9Sstevel@tonic-gate 	if ((fill = dupb(sctp_pad_mp)) != NULL) {
656*7c478bd9Sstevel@tonic-gate 		fill->b_wptr += pad;
657*7c478bd9Sstevel@tonic-gate 		return (fill);
658*7c478bd9Sstevel@tonic-gate 	}
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate 	/*
661*7c478bd9Sstevel@tonic-gate 	 * The memory saving path of reusing the sctp_pad_mp
662*7c478bd9Sstevel@tonic-gate 	 * fails may be because it has been dupb() too
663*7c478bd9Sstevel@tonic-gate 	 * many times (DBLK_REFMAX).  Use the memory consuming
664*7c478bd9Sstevel@tonic-gate 	 * path of allocating the pad mblk.
665*7c478bd9Sstevel@tonic-gate 	 */
666*7c478bd9Sstevel@tonic-gate 	if ((fill = allocb(SCTP_ALIGN, BPRI_MED)) != NULL) {
667*7c478bd9Sstevel@tonic-gate 		/* Zero it out.  SCTP_ALIGN is sizeof (int32_t) */
668*7c478bd9Sstevel@tonic-gate 		*(int32_t *)fill->b_rptr = 0;
669*7c478bd9Sstevel@tonic-gate 		fill->b_wptr += pad;
670*7c478bd9Sstevel@tonic-gate 	}
671*7c478bd9Sstevel@tonic-gate 	return (fill);
672*7c478bd9Sstevel@tonic-gate }
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate static mblk_t *
675*7c478bd9Sstevel@tonic-gate sctp_find_fast_rexmit_mblks(sctp_t *sctp, int *total, sctp_faddr_t **fp)
676*7c478bd9Sstevel@tonic-gate {
677*7c478bd9Sstevel@tonic-gate 	mblk_t		*meta;
678*7c478bd9Sstevel@tonic-gate 	mblk_t		*start_mp = NULL;
679*7c478bd9Sstevel@tonic-gate 	mblk_t		*end_mp = NULL;
680*7c478bd9Sstevel@tonic-gate 	mblk_t		*mp, *nmp;
681*7c478bd9Sstevel@tonic-gate 	mblk_t		*fill;
682*7c478bd9Sstevel@tonic-gate 	sctp_data_hdr_t	*sdh;
683*7c478bd9Sstevel@tonic-gate 	int		msglen;
684*7c478bd9Sstevel@tonic-gate 	int		extra;
685*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr_t	*msg_hdr;
686*7c478bd9Sstevel@tonic-gate 
687*7c478bd9Sstevel@tonic-gate 	for (meta = sctp->sctp_xmit_head; meta != NULL; meta = meta->b_next) {
688*7c478bd9Sstevel@tonic-gate 		msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
689*7c478bd9Sstevel@tonic-gate 		if (SCTP_IS_MSG_ABANDONED(meta) ||
690*7c478bd9Sstevel@tonic-gate 		    SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
691*7c478bd9Sstevel@tonic-gate 			continue;
692*7c478bd9Sstevel@tonic-gate 		}
693*7c478bd9Sstevel@tonic-gate 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
694*7c478bd9Sstevel@tonic-gate 			if (SCTP_CHUNK_WANT_REXMIT(mp)) {
695*7c478bd9Sstevel@tonic-gate 				/*
696*7c478bd9Sstevel@tonic-gate 				 * Use the same peer address to do fast
697*7c478bd9Sstevel@tonic-gate 				 * retransmission.
698*7c478bd9Sstevel@tonic-gate 				 */
699*7c478bd9Sstevel@tonic-gate 				if (*fp == NULL) {
700*7c478bd9Sstevel@tonic-gate 					*fp = SCTP_CHUNK_DEST(mp);
701*7c478bd9Sstevel@tonic-gate 					if ((*fp)->state != SCTP_FADDRS_ALIVE)
702*7c478bd9Sstevel@tonic-gate 						*fp = sctp->sctp_current;
703*7c478bd9Sstevel@tonic-gate 				} else if (*fp != SCTP_CHUNK_DEST(mp)) {
704*7c478bd9Sstevel@tonic-gate 					continue;
705*7c478bd9Sstevel@tonic-gate 				}
706*7c478bd9Sstevel@tonic-gate 
707*7c478bd9Sstevel@tonic-gate 				sdh = (sctp_data_hdr_t *)mp->b_rptr;
708*7c478bd9Sstevel@tonic-gate 				msglen = ntohs(sdh->sdh_len);
709*7c478bd9Sstevel@tonic-gate 				if ((extra = msglen & (SCTP_ALIGN - 1)) != 0) {
710*7c478bd9Sstevel@tonic-gate 					extra = SCTP_ALIGN - extra;
711*7c478bd9Sstevel@tonic-gate 				}
712*7c478bd9Sstevel@tonic-gate 
713*7c478bd9Sstevel@tonic-gate 				/*
714*7c478bd9Sstevel@tonic-gate 				 * We still return at least the first message
715*7c478bd9Sstevel@tonic-gate 				 * even if that message cannot fit in as
716*7c478bd9Sstevel@tonic-gate 				 * PMTU may have changed.
717*7c478bd9Sstevel@tonic-gate 				 */
718*7c478bd9Sstevel@tonic-gate 				if (*total + msglen + extra >
719*7c478bd9Sstevel@tonic-gate 				    (*fp)->sfa_pmss && start_mp != NULL) {
720*7c478bd9Sstevel@tonic-gate 					return (start_mp);
721*7c478bd9Sstevel@tonic-gate 				}
722*7c478bd9Sstevel@tonic-gate 				if ((nmp = dupmsg(mp)) == NULL)
723*7c478bd9Sstevel@tonic-gate 					return (start_mp);
724*7c478bd9Sstevel@tonic-gate 				if (extra > 0) {
725*7c478bd9Sstevel@tonic-gate 					fill = sctp_get_padding(extra);
726*7c478bd9Sstevel@tonic-gate 					if (fill != NULL) {
727*7c478bd9Sstevel@tonic-gate 						linkb(nmp, fill);
728*7c478bd9Sstevel@tonic-gate 					} else {
729*7c478bd9Sstevel@tonic-gate 						return (start_mp);
730*7c478bd9Sstevel@tonic-gate 					}
731*7c478bd9Sstevel@tonic-gate 				}
732*7c478bd9Sstevel@tonic-gate 				SCTP_CHUNK_CLEAR_REXMIT(mp);
733*7c478bd9Sstevel@tonic-gate 				if (start_mp == NULL) {
734*7c478bd9Sstevel@tonic-gate 					start_mp = nmp;
735*7c478bd9Sstevel@tonic-gate 				} else {
736*7c478bd9Sstevel@tonic-gate 					linkb(end_mp, nmp);
737*7c478bd9Sstevel@tonic-gate 				}
738*7c478bd9Sstevel@tonic-gate 				end_mp = nmp;
739*7c478bd9Sstevel@tonic-gate 				*total += msglen + extra;
740*7c478bd9Sstevel@tonic-gate 				dprint(2, ("sctp_find_fast_rexmit_mblks: "
741*7c478bd9Sstevel@tonic-gate 				    "tsn %x\n", sdh->sdh_tsn));
742*7c478bd9Sstevel@tonic-gate 			}
743*7c478bd9Sstevel@tonic-gate 		}
744*7c478bd9Sstevel@tonic-gate 	}
745*7c478bd9Sstevel@tonic-gate 	/* Clear the flag as there is no more message to be fast rexmitted. */
746*7c478bd9Sstevel@tonic-gate 	sctp->sctp_chk_fast_rexmit = B_FALSE;
747*7c478bd9Sstevel@tonic-gate 	return (start_mp);
748*7c478bd9Sstevel@tonic-gate }
749*7c478bd9Sstevel@tonic-gate 
750*7c478bd9Sstevel@tonic-gate /* A debug function just to make sure that a mblk chain is not broken */
751*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
752*7c478bd9Sstevel@tonic-gate static boolean_t
753*7c478bd9Sstevel@tonic-gate sctp_verify_chain(mblk_t *head, mblk_t *tail)
754*7c478bd9Sstevel@tonic-gate {
755*7c478bd9Sstevel@tonic-gate 	mblk_t	*mp = head;
756*7c478bd9Sstevel@tonic-gate 
757*7c478bd9Sstevel@tonic-gate 	if (head == NULL || tail == NULL)
758*7c478bd9Sstevel@tonic-gate 		return (B_TRUE);
759*7c478bd9Sstevel@tonic-gate 	while (mp != NULL) {
760*7c478bd9Sstevel@tonic-gate 		if (mp == tail)
761*7c478bd9Sstevel@tonic-gate 			return (B_TRUE);
762*7c478bd9Sstevel@tonic-gate 		mp = mp->b_next;
763*7c478bd9Sstevel@tonic-gate 	}
764*7c478bd9Sstevel@tonic-gate 	return (B_FALSE);
765*7c478bd9Sstevel@tonic-gate }
766*7c478bd9Sstevel@tonic-gate #endif
767*7c478bd9Sstevel@tonic-gate 
768*7c478bd9Sstevel@tonic-gate /*
769*7c478bd9Sstevel@tonic-gate  * Gets the next unsent chunk to transmit. Messages that are abandoned are
770*7c478bd9Sstevel@tonic-gate  * skipped. A message can be abandoned if it has a non-zero timetolive and
771*7c478bd9Sstevel@tonic-gate  * transmission has not yet started or if it is a partially reliable
772*7c478bd9Sstevel@tonic-gate  * message and its time is up (assuming we are PR-SCTP aware).
773*7c478bd9Sstevel@tonic-gate  * 'cansend' is used to determine if need to try and chunkify messages from
774*7c478bd9Sstevel@tonic-gate  * the unsent list, if any, and also as an input to sctp_chunkify() if so.
775*7c478bd9Sstevel@tonic-gate  * When called from sctp_rexmit(), we don't want to chunkify, so 'cansend'
776*7c478bd9Sstevel@tonic-gate  * will be set to 0.
777*7c478bd9Sstevel@tonic-gate  */
778*7c478bd9Sstevel@tonic-gate mblk_t *
779*7c478bd9Sstevel@tonic-gate sctp_get_msg_to_send(sctp_t *sctp, mblk_t **mp, mblk_t *meta, int  *error,
780*7c478bd9Sstevel@tonic-gate     int32_t firstseg, uint32_t cansend, sctp_faddr_t *fp)
781*7c478bd9Sstevel@tonic-gate {
782*7c478bd9Sstevel@tonic-gate 	mblk_t		*mp1;
783*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr_t	*msg_hdr;
784*7c478bd9Sstevel@tonic-gate 	mblk_t		*tmp_meta;
785*7c478bd9Sstevel@tonic-gate 	sctp_faddr_t	*fp1;
786*7c478bd9Sstevel@tonic-gate 
787*7c478bd9Sstevel@tonic-gate 	ASSERT(error != NULL && mp != NULL);
788*7c478bd9Sstevel@tonic-gate 	*error = 0;
789*7c478bd9Sstevel@tonic-gate 
790*7c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_current != NULL);
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate chunkified:
793*7c478bd9Sstevel@tonic-gate 	while (meta != NULL) {
794*7c478bd9Sstevel@tonic-gate 		tmp_meta = meta->b_next;
795*7c478bd9Sstevel@tonic-gate 		msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
796*7c478bd9Sstevel@tonic-gate 		mp1 = meta->b_cont;
797*7c478bd9Sstevel@tonic-gate 		if (SCTP_IS_MSG_ABANDONED(meta))
798*7c478bd9Sstevel@tonic-gate 			goto next_msg;
799*7c478bd9Sstevel@tonic-gate 		if (!SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
800*7c478bd9Sstevel@tonic-gate 			while (mp1 != NULL) {
801*7c478bd9Sstevel@tonic-gate 				if (SCTP_CHUNK_CANSEND(mp1)) {
802*7c478bd9Sstevel@tonic-gate 					*mp = mp1;
803*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
804*7c478bd9Sstevel@tonic-gate 					ASSERT(sctp_verify_chain(
805*7c478bd9Sstevel@tonic-gate 					    sctp->sctp_xmit_head, meta));
806*7c478bd9Sstevel@tonic-gate #endif
807*7c478bd9Sstevel@tonic-gate 					return (meta);
808*7c478bd9Sstevel@tonic-gate 				}
809*7c478bd9Sstevel@tonic-gate 				mp1 = mp1->b_next;
810*7c478bd9Sstevel@tonic-gate 			}
811*7c478bd9Sstevel@tonic-gate 			goto next_msg;
812*7c478bd9Sstevel@tonic-gate 		}
813*7c478bd9Sstevel@tonic-gate 		/*
814*7c478bd9Sstevel@tonic-gate 		 * If we come here and the first chunk is sent, then we
815*7c478bd9Sstevel@tonic-gate 		 * we are PR-SCTP aware, in which case if the cumulative
816*7c478bd9Sstevel@tonic-gate 		 * TSN has moved upto or beyond the first chunk (which
817*7c478bd9Sstevel@tonic-gate 		 * means all the previous messages have been cumulative
818*7c478bd9Sstevel@tonic-gate 		 * SACK'd), then we send a Forward TSN with the last
819*7c478bd9Sstevel@tonic-gate 		 * chunk that was sent in this message. If we can't send
820*7c478bd9Sstevel@tonic-gate 		 * a Forward TSN because previous non-abandoned messages
821*7c478bd9Sstevel@tonic-gate 		 * have not been acked then we will defer the Forward TSN
822*7c478bd9Sstevel@tonic-gate 		 * to sctp_rexmit() or sctp_cumack().
823*7c478bd9Sstevel@tonic-gate 		 */
824*7c478bd9Sstevel@tonic-gate 		if (SCTP_CHUNK_ISSENT(mp1)) {
825*7c478bd9Sstevel@tonic-gate 			*error = sctp_check_abandoned_msg(sctp, meta);
826*7c478bd9Sstevel@tonic-gate 			if (*error != 0) {
827*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
828*7c478bd9Sstevel@tonic-gate 				ASSERT(sctp_verify_chain(sctp->sctp_xmit_head,
829*7c478bd9Sstevel@tonic-gate 				    sctp->sctp_xmit_tail));
830*7c478bd9Sstevel@tonic-gate #endif
831*7c478bd9Sstevel@tonic-gate 				return (NULL);
832*7c478bd9Sstevel@tonic-gate 			}
833*7c478bd9Sstevel@tonic-gate 			goto next_msg;
834*7c478bd9Sstevel@tonic-gate 		}
835*7c478bd9Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_prsctpdrop);
836*7c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent >= msg_hdr->smh_msglen);
837*7c478bd9Sstevel@tonic-gate 		if (meta->b_prev == NULL) {
838*7c478bd9Sstevel@tonic-gate 			ASSERT(sctp->sctp_xmit_head == meta);
839*7c478bd9Sstevel@tonic-gate 			sctp->sctp_xmit_head = tmp_meta;
840*7c478bd9Sstevel@tonic-gate 			if (sctp->sctp_xmit_tail == meta)
841*7c478bd9Sstevel@tonic-gate 				sctp->sctp_xmit_tail = tmp_meta;
842*7c478bd9Sstevel@tonic-gate 			meta->b_next = NULL;
843*7c478bd9Sstevel@tonic-gate 			if (tmp_meta != NULL)
844*7c478bd9Sstevel@tonic-gate 				tmp_meta->b_prev = NULL;
845*7c478bd9Sstevel@tonic-gate 		} else if (meta->b_next == NULL) {
846*7c478bd9Sstevel@tonic-gate 			if (sctp->sctp_xmit_tail == meta)
847*7c478bd9Sstevel@tonic-gate 				sctp->sctp_xmit_tail = meta->b_prev;
848*7c478bd9Sstevel@tonic-gate 			meta->b_prev->b_next = NULL;
849*7c478bd9Sstevel@tonic-gate 			meta->b_prev = NULL;
850*7c478bd9Sstevel@tonic-gate 		} else {
851*7c478bd9Sstevel@tonic-gate 			meta->b_prev->b_next = tmp_meta;
852*7c478bd9Sstevel@tonic-gate 			tmp_meta->b_prev = meta->b_prev;
853*7c478bd9Sstevel@tonic-gate 			if (sctp->sctp_xmit_tail == meta)
854*7c478bd9Sstevel@tonic-gate 				sctp->sctp_xmit_tail = tmp_meta;
855*7c478bd9Sstevel@tonic-gate 			meta->b_prev = NULL;
856*7c478bd9Sstevel@tonic-gate 			meta->b_next = NULL;
857*7c478bd9Sstevel@tonic-gate 		}
858*7c478bd9Sstevel@tonic-gate 		sctp->sctp_unsent -= msg_hdr->smh_msglen;
859*7c478bd9Sstevel@tonic-gate 		/*
860*7c478bd9Sstevel@tonic-gate 		 * Update ULP the amount of queued data, which is
861*7c478bd9Sstevel@tonic-gate 		 * sent-unack'ed + unsent.
862*7c478bd9Sstevel@tonic-gate 		 */
863*7c478bd9Sstevel@tonic-gate 		if (!SCTP_IS_DETACHED(sctp)) {
864*7c478bd9Sstevel@tonic-gate 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
865*7c478bd9Sstevel@tonic-gate 			    sctp->sctp_unacked + sctp->sctp_unsent);
866*7c478bd9Sstevel@tonic-gate 		}
867*7c478bd9Sstevel@tonic-gate 		sctp_sendfail_event(sctp, meta, 0, B_TRUE);
868*7c478bd9Sstevel@tonic-gate next_msg:
869*7c478bd9Sstevel@tonic-gate 		meta = tmp_meta;
870*7c478bd9Sstevel@tonic-gate 	}
871*7c478bd9Sstevel@tonic-gate 	/* chunkify, if needed */
872*7c478bd9Sstevel@tonic-gate 	if (cansend > 0 && sctp->sctp_xmit_unsent != NULL) {
873*7c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent > 0);
874*7c478bd9Sstevel@tonic-gate 		if (fp == NULL) {
875*7c478bd9Sstevel@tonic-gate 			fp = SCTP_CHUNK_DEST(sctp->sctp_xmit_unsent);
876*7c478bd9Sstevel@tonic-gate 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
877*7c478bd9Sstevel@tonic-gate 				fp = sctp->sctp_current;
878*7c478bd9Sstevel@tonic-gate 		} else {
879*7c478bd9Sstevel@tonic-gate 			/*
880*7c478bd9Sstevel@tonic-gate 			 * If user specified destination, try to honor that.
881*7c478bd9Sstevel@tonic-gate 			 */
882*7c478bd9Sstevel@tonic-gate 			fp1 = SCTP_CHUNK_DEST(sctp->sctp_xmit_unsent);
883*7c478bd9Sstevel@tonic-gate 			if (fp1 != NULL && fp1->state == SCTP_FADDRS_ALIVE &&
884*7c478bd9Sstevel@tonic-gate 			    fp1 != fp) {
885*7c478bd9Sstevel@tonic-gate 				goto chunk_done;
886*7c478bd9Sstevel@tonic-gate 			}
887*7c478bd9Sstevel@tonic-gate 		}
888*7c478bd9Sstevel@tonic-gate 		sctp_chunkify(sctp, fp->sfa_pmss - firstseg, cansend);
889*7c478bd9Sstevel@tonic-gate 		if ((meta = sctp->sctp_xmit_tail) == NULL)
890*7c478bd9Sstevel@tonic-gate 			goto chunk_done;
891*7c478bd9Sstevel@tonic-gate 		/*
892*7c478bd9Sstevel@tonic-gate 		 * sctp_chunkify() won't advance sctp_xmit_tail if it adds
893*7c478bd9Sstevel@tonic-gate 		 * new chunk(s) to the tail, so we need to skip the
894*7c478bd9Sstevel@tonic-gate 		 * sctp_xmit_tail, which would have already been processed.
895*7c478bd9Sstevel@tonic-gate 		 * This could happen when there is unacked chunks, but
896*7c478bd9Sstevel@tonic-gate 		 * nothing new to send.
897*7c478bd9Sstevel@tonic-gate 		 * When sctp_chunkify() is called when the transmit queue
898*7c478bd9Sstevel@tonic-gate 		 * is empty then we need to start from sctp_xmit_tail.
899*7c478bd9Sstevel@tonic-gate 		 */
900*7c478bd9Sstevel@tonic-gate 		if (SCTP_CHUNK_ISSENT(sctp->sctp_xmit_tail->b_cont)) {
901*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
902*7c478bd9Sstevel@tonic-gate 			mp1 = sctp->sctp_xmit_tail->b_cont;
903*7c478bd9Sstevel@tonic-gate 			while (mp1 != NULL) {
904*7c478bd9Sstevel@tonic-gate 				ASSERT(!SCTP_CHUNK_CANSEND(mp1));
905*7c478bd9Sstevel@tonic-gate 				mp1 = mp1->b_next;
906*7c478bd9Sstevel@tonic-gate 			}
907*7c478bd9Sstevel@tonic-gate #endif
908*7c478bd9Sstevel@tonic-gate 			if ((meta = sctp->sctp_xmit_tail->b_next) == NULL)
909*7c478bd9Sstevel@tonic-gate 				goto chunk_done;
910*7c478bd9Sstevel@tonic-gate 		}
911*7c478bd9Sstevel@tonic-gate 		goto chunkified;
912*7c478bd9Sstevel@tonic-gate 	}
913*7c478bd9Sstevel@tonic-gate chunk_done:
914*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
915*7c478bd9Sstevel@tonic-gate 	ASSERT(sctp_verify_chain(sctp->sctp_xmit_head, sctp->sctp_xmit_tail));
916*7c478bd9Sstevel@tonic-gate #endif
917*7c478bd9Sstevel@tonic-gate 	return (NULL);
918*7c478bd9Sstevel@tonic-gate }
919*7c478bd9Sstevel@tonic-gate 
920*7c478bd9Sstevel@tonic-gate void
921*7c478bd9Sstevel@tonic-gate sctp_fast_rexmit(sctp_t *sctp)
922*7c478bd9Sstevel@tonic-gate {
923*7c478bd9Sstevel@tonic-gate 	mblk_t		*mp, *head;
924*7c478bd9Sstevel@tonic-gate 	int		pktlen = 0;
925*7c478bd9Sstevel@tonic-gate 	sctp_faddr_t	*fp = NULL;
926*7c478bd9Sstevel@tonic-gate 
927*7c478bd9Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head != NULL);
928*7c478bd9Sstevel@tonic-gate 	mp = sctp_find_fast_rexmit_mblks(sctp, &pktlen, &fp);
929*7c478bd9Sstevel@tonic-gate 	if (mp == NULL)
930*7c478bd9Sstevel@tonic-gate 		return;
931*7c478bd9Sstevel@tonic-gate 	if ((head = sctp_add_proto_hdr(sctp, fp, mp, 0)) == NULL) {
932*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
933*7c478bd9Sstevel@tonic-gate 		return;
934*7c478bd9Sstevel@tonic-gate 	}
935*7c478bd9Sstevel@tonic-gate 	if ((pktlen > fp->sfa_pmss) && fp->isv4) {
936*7c478bd9Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)head->b_rptr;
937*7c478bd9Sstevel@tonic-gate 
938*7c478bd9Sstevel@tonic-gate 		iph->ipha_fragment_offset_and_flags = 0;
939*7c478bd9Sstevel@tonic-gate 	}
940*7c478bd9Sstevel@tonic-gate 
941*7c478bd9Sstevel@tonic-gate 	sctp_set_iplen(sctp, head);
942*7c478bd9Sstevel@tonic-gate 	sctp_add_sendq(sctp, head);
943*7c478bd9Sstevel@tonic-gate 	sctp->sctp_active = fp->lastactive = lbolt64;
944*7c478bd9Sstevel@tonic-gate }
945*7c478bd9Sstevel@tonic-gate 
946*7c478bd9Sstevel@tonic-gate void
947*7c478bd9Sstevel@tonic-gate sctp_output(sctp_t *sctp)
948*7c478bd9Sstevel@tonic-gate {
949*7c478bd9Sstevel@tonic-gate 	mblk_t			*mp = NULL;
950*7c478bd9Sstevel@tonic-gate 	mblk_t			*nmp;
951*7c478bd9Sstevel@tonic-gate 	mblk_t			*head;
952*7c478bd9Sstevel@tonic-gate 	mblk_t			*meta = sctp->sctp_xmit_tail;
953*7c478bd9Sstevel@tonic-gate 	mblk_t			*fill = NULL;
954*7c478bd9Sstevel@tonic-gate 	uint16_t 		chunklen;
955*7c478bd9Sstevel@tonic-gate 	uint32_t 		cansend;
956*7c478bd9Sstevel@tonic-gate 	int32_t			seglen;
957*7c478bd9Sstevel@tonic-gate 	int32_t			xtralen;
958*7c478bd9Sstevel@tonic-gate 	int32_t			sacklen;
959*7c478bd9Sstevel@tonic-gate 	int32_t			pad = 0;
960*7c478bd9Sstevel@tonic-gate 	int32_t			pathmax;
961*7c478bd9Sstevel@tonic-gate 	int			extra;
962*7c478bd9Sstevel@tonic-gate 	int64_t			now = lbolt64;
963*7c478bd9Sstevel@tonic-gate 	sctp_faddr_t		*fp;
964*7c478bd9Sstevel@tonic-gate 	sctp_faddr_t		*lfp;
965*7c478bd9Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
966*7c478bd9Sstevel@tonic-gate 	int			error;
967*7c478bd9Sstevel@tonic-gate 
968*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
969*7c478bd9Sstevel@tonic-gate 		sacklen = 0;
970*7c478bd9Sstevel@tonic-gate 	} else {
971*7c478bd9Sstevel@tonic-gate 		/* send a SACK chunk */
972*7c478bd9Sstevel@tonic-gate 		sacklen = sizeof (sctp_chunk_hdr_t) +
973*7c478bd9Sstevel@tonic-gate 		    sizeof (sctp_sack_chunk_t) +
974*7c478bd9Sstevel@tonic-gate 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
975*7c478bd9Sstevel@tonic-gate 		lfp = sctp->sctp_lastdata;
976*7c478bd9Sstevel@tonic-gate 		ASSERT(lfp != NULL);
977*7c478bd9Sstevel@tonic-gate 		if (lfp->state != SCTP_FADDRS_ALIVE)
978*7c478bd9Sstevel@tonic-gate 			lfp = sctp->sctp_current;
979*7c478bd9Sstevel@tonic-gate 	}
980*7c478bd9Sstevel@tonic-gate 
981*7c478bd9Sstevel@tonic-gate 	cansend = sctp->sctp_frwnd;
982*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_unsent < cansend)
983*7c478bd9Sstevel@tonic-gate 		cansend = sctp->sctp_unsent;
984*7c478bd9Sstevel@tonic-gate 	if ((cansend < sctp->sctp_current->sfa_pmss / 2) &&
985*7c478bd9Sstevel@tonic-gate 	    sctp->sctp_unacked &&
986*7c478bd9Sstevel@tonic-gate 	    (sctp->sctp_unacked < sctp->sctp_current->sfa_pmss) &&
987*7c478bd9Sstevel@tonic-gate 	    !sctp->sctp_ndelay) {
988*7c478bd9Sstevel@tonic-gate 		head = NULL;
989*7c478bd9Sstevel@tonic-gate 		fp = sctp->sctp_current;
990*7c478bd9Sstevel@tonic-gate 		goto unsent_data;
991*7c478bd9Sstevel@tonic-gate 	}
992*7c478bd9Sstevel@tonic-gate 	if (meta != NULL)
993*7c478bd9Sstevel@tonic-gate 		mp = meta->b_cont;
994*7c478bd9Sstevel@tonic-gate 	while (cansend > 0) {
995*7c478bd9Sstevel@tonic-gate 		pad = 0;
996*7c478bd9Sstevel@tonic-gate 
997*7c478bd9Sstevel@tonic-gate 		/*
998*7c478bd9Sstevel@tonic-gate 		 * Find first segment eligible for transmit.
999*7c478bd9Sstevel@tonic-gate 		 */
1000*7c478bd9Sstevel@tonic-gate 		while (mp != NULL) {
1001*7c478bd9Sstevel@tonic-gate 			if (SCTP_CHUNK_CANSEND(mp))
1002*7c478bd9Sstevel@tonic-gate 				break;
1003*7c478bd9Sstevel@tonic-gate 			mp = mp->b_next;
1004*7c478bd9Sstevel@tonic-gate 		}
1005*7c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
1006*7c478bd9Sstevel@tonic-gate 			meta = sctp_get_msg_to_send(sctp, &mp,
1007*7c478bd9Sstevel@tonic-gate 			    meta == NULL ? NULL : meta->b_next, &error, sacklen,
1008*7c478bd9Sstevel@tonic-gate 			    cansend, NULL);
1009*7c478bd9Sstevel@tonic-gate 			if (error != 0 || meta == NULL) {
1010*7c478bd9Sstevel@tonic-gate 				head = NULL;
1011*7c478bd9Sstevel@tonic-gate 				fp = sctp->sctp_current;
1012*7c478bd9Sstevel@tonic-gate 				goto unsent_data;
1013*7c478bd9Sstevel@tonic-gate 			}
1014*7c478bd9Sstevel@tonic-gate 			sctp->sctp_xmit_tail =  meta;
1015*7c478bd9Sstevel@tonic-gate 		}
1016*7c478bd9Sstevel@tonic-gate 
1017*7c478bd9Sstevel@tonic-gate 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
1018*7c478bd9Sstevel@tonic-gate 		seglen = ntohs(sdc->sdh_len);
1019*7c478bd9Sstevel@tonic-gate 		xtralen = sizeof (*sdc);
1020*7c478bd9Sstevel@tonic-gate 		chunklen = seglen - xtralen;
1021*7c478bd9Sstevel@tonic-gate 
1022*7c478bd9Sstevel@tonic-gate 		/*
1023*7c478bd9Sstevel@tonic-gate 		 * Check rwnd.
1024*7c478bd9Sstevel@tonic-gate 		 */
1025*7c478bd9Sstevel@tonic-gate 		if (chunklen > cansend) {
1026*7c478bd9Sstevel@tonic-gate 			head = NULL;
1027*7c478bd9Sstevel@tonic-gate 			fp = SCTP_CHUNK_DEST(meta);
1028*7c478bd9Sstevel@tonic-gate 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
1029*7c478bd9Sstevel@tonic-gate 				fp = sctp->sctp_current;
1030*7c478bd9Sstevel@tonic-gate 			goto unsent_data;
1031*7c478bd9Sstevel@tonic-gate 		}
1032*7c478bd9Sstevel@tonic-gate 		if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
1033*7c478bd9Sstevel@tonic-gate 			extra = SCTP_ALIGN - extra;
1034*7c478bd9Sstevel@tonic-gate 
1035*7c478bd9Sstevel@tonic-gate 		/*
1036*7c478bd9Sstevel@tonic-gate 		 * Pick destination address, and check cwnd.
1037*7c478bd9Sstevel@tonic-gate 		 */
1038*7c478bd9Sstevel@tonic-gate 		if (sacklen > 0 && (seglen + extra <= lfp->cwnd - lfp->suna) &&
1039*7c478bd9Sstevel@tonic-gate 		    (seglen + sacklen + extra <= lfp->sfa_pmss)) {
1040*7c478bd9Sstevel@tonic-gate 			/*
1041*7c478bd9Sstevel@tonic-gate 			 * Only include SACK chunk if it can be bundled
1042*7c478bd9Sstevel@tonic-gate 			 * with a data chunk, and sent to sctp_lastdata.
1043*7c478bd9Sstevel@tonic-gate 			 */
1044*7c478bd9Sstevel@tonic-gate 			pathmax = lfp->cwnd - lfp->suna;
1045*7c478bd9Sstevel@tonic-gate 
1046*7c478bd9Sstevel@tonic-gate 			fp = lfp;
1047*7c478bd9Sstevel@tonic-gate 			if ((nmp = dupmsg(mp)) == NULL) {
1048*7c478bd9Sstevel@tonic-gate 				head = NULL;
1049*7c478bd9Sstevel@tonic-gate 				goto unsent_data;
1050*7c478bd9Sstevel@tonic-gate 			}
1051*7c478bd9Sstevel@tonic-gate 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1052*7c478bd9Sstevel@tonic-gate 			head = sctp_add_proto_hdr(sctp, fp, nmp, sacklen);
1053*7c478bd9Sstevel@tonic-gate 			if (head == NULL) {
1054*7c478bd9Sstevel@tonic-gate 				freemsg(nmp);
1055*7c478bd9Sstevel@tonic-gate 				goto unsent_data;
1056*7c478bd9Sstevel@tonic-gate 			}
1057*7c478bd9Sstevel@tonic-gate 			seglen += sacklen;
1058*7c478bd9Sstevel@tonic-gate 			xtralen += sacklen;
1059*7c478bd9Sstevel@tonic-gate 			sacklen = 0;
1060*7c478bd9Sstevel@tonic-gate 		} else {
1061*7c478bd9Sstevel@tonic-gate 			fp = SCTP_CHUNK_DEST(meta);
1062*7c478bd9Sstevel@tonic-gate 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
1063*7c478bd9Sstevel@tonic-gate 				fp = sctp->sctp_current;
1064*7c478bd9Sstevel@tonic-gate 			/*
1065*7c478bd9Sstevel@tonic-gate 			 * If we haven't sent data to this destination for
1066*7c478bd9Sstevel@tonic-gate 			 * a while, do slow start again.
1067*7c478bd9Sstevel@tonic-gate 			 */
1068*7c478bd9Sstevel@tonic-gate 			if (now - fp->lastactive > fp->rto) {
1069*7c478bd9Sstevel@tonic-gate 				fp->cwnd = sctp_slow_start_after_idle *
1070*7c478bd9Sstevel@tonic-gate 				    fp->sfa_pmss;
1071*7c478bd9Sstevel@tonic-gate 			}
1072*7c478bd9Sstevel@tonic-gate 			fp->lastactive = now;
1073*7c478bd9Sstevel@tonic-gate 
1074*7c478bd9Sstevel@tonic-gate 			pathmax = fp->cwnd - fp->suna;
1075*7c478bd9Sstevel@tonic-gate 			if (seglen + extra > pathmax) {
1076*7c478bd9Sstevel@tonic-gate 				head = NULL;
1077*7c478bd9Sstevel@tonic-gate 				goto unsent_data;
1078*7c478bd9Sstevel@tonic-gate 			}
1079*7c478bd9Sstevel@tonic-gate 			if ((nmp = dupmsg(mp)) == NULL) {
1080*7c478bd9Sstevel@tonic-gate 				head = NULL;
1081*7c478bd9Sstevel@tonic-gate 				goto unsent_data;
1082*7c478bd9Sstevel@tonic-gate 			}
1083*7c478bd9Sstevel@tonic-gate 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1084*7c478bd9Sstevel@tonic-gate 			head = sctp_add_proto_hdr(sctp, fp, nmp, 0);
1085*7c478bd9Sstevel@tonic-gate 			if (head == NULL) {
1086*7c478bd9Sstevel@tonic-gate 				freemsg(nmp);
1087*7c478bd9Sstevel@tonic-gate 				goto unsent_data;
1088*7c478bd9Sstevel@tonic-gate 			}
1089*7c478bd9Sstevel@tonic-gate 		}
1090*7c478bd9Sstevel@tonic-gate 		if (pathmax > fp->sfa_pmss)
1091*7c478bd9Sstevel@tonic-gate 			pathmax = fp->sfa_pmss;
1092*7c478bd9Sstevel@tonic-gate 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
1093*7c478bd9Sstevel@tonic-gate 		mp = mp->b_next;
1094*7c478bd9Sstevel@tonic-gate 
1095*7c478bd9Sstevel@tonic-gate 		/* Use this chunk to measure RTT? */
1096*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_out_time == 0) {
1097*7c478bd9Sstevel@tonic-gate 			sctp->sctp_out_time = now;
1098*7c478bd9Sstevel@tonic-gate 			sctp->sctp_rtt_tsn = sctp->sctp_ltsn - 1;
1099*7c478bd9Sstevel@tonic-gate 		}
1100*7c478bd9Sstevel@tonic-gate 		if (extra > 0) {
1101*7c478bd9Sstevel@tonic-gate 			fill = sctp_get_padding(extra);
1102*7c478bd9Sstevel@tonic-gate 			if (fill != NULL) {
1103*7c478bd9Sstevel@tonic-gate 				linkb(head, fill);
1104*7c478bd9Sstevel@tonic-gate 				pad = extra;
1105*7c478bd9Sstevel@tonic-gate 				seglen += extra;
1106*7c478bd9Sstevel@tonic-gate 			} else {
1107*7c478bd9Sstevel@tonic-gate 				goto unsent_data;
1108*7c478bd9Sstevel@tonic-gate 			}
1109*7c478bd9Sstevel@tonic-gate 		}
1110*7c478bd9Sstevel@tonic-gate 		/* See if we can bundle more. */
1111*7c478bd9Sstevel@tonic-gate 		while (seglen < pathmax) {
1112*7c478bd9Sstevel@tonic-gate 			int32_t		new_len;
1113*7c478bd9Sstevel@tonic-gate 			int32_t		new_xtralen;
1114*7c478bd9Sstevel@tonic-gate 
1115*7c478bd9Sstevel@tonic-gate 			while (mp != NULL) {
1116*7c478bd9Sstevel@tonic-gate 				if (SCTP_CHUNK_CANSEND(mp))
1117*7c478bd9Sstevel@tonic-gate 					break;
1118*7c478bd9Sstevel@tonic-gate 				mp = mp->b_next;
1119*7c478bd9Sstevel@tonic-gate 			}
1120*7c478bd9Sstevel@tonic-gate 			if (mp == NULL) {
1121*7c478bd9Sstevel@tonic-gate 				meta = sctp_get_msg_to_send(sctp, &mp,
1122*7c478bd9Sstevel@tonic-gate 				    meta->b_next, &error, seglen,
1123*7c478bd9Sstevel@tonic-gate 				    (seglen - xtralen) >= cansend ? 0 :
1124*7c478bd9Sstevel@tonic-gate 				    cansend - seglen, fp);
1125*7c478bd9Sstevel@tonic-gate 				if (error != 0 || meta == NULL)
1126*7c478bd9Sstevel@tonic-gate 					break;
1127*7c478bd9Sstevel@tonic-gate 				sctp->sctp_xmit_tail =  meta;
1128*7c478bd9Sstevel@tonic-gate 			}
1129*7c478bd9Sstevel@tonic-gate 			ASSERT(mp != NULL);
1130*7c478bd9Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp) && SCTP_CHUNK_DEST(meta) &&
1131*7c478bd9Sstevel@tonic-gate 			    fp != SCTP_CHUNK_DEST(meta)) {
1132*7c478bd9Sstevel@tonic-gate 				break;
1133*7c478bd9Sstevel@tonic-gate 			}
1134*7c478bd9Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
1135*7c478bd9Sstevel@tonic-gate 			chunklen = ntohs(sdc->sdh_len);
1136*7c478bd9Sstevel@tonic-gate 			if ((extra = chunklen  & (SCTP_ALIGN - 1)) != 0)
1137*7c478bd9Sstevel@tonic-gate 				extra = SCTP_ALIGN - extra;
1138*7c478bd9Sstevel@tonic-gate 
1139*7c478bd9Sstevel@tonic-gate 			new_len = seglen + chunklen;
1140*7c478bd9Sstevel@tonic-gate 			new_xtralen = xtralen + sizeof (*sdc);
1141*7c478bd9Sstevel@tonic-gate 			chunklen -= sizeof (*sdc);
1142*7c478bd9Sstevel@tonic-gate 
1143*7c478bd9Sstevel@tonic-gate 			if (new_len - new_xtralen > cansend ||
1144*7c478bd9Sstevel@tonic-gate 			    new_len + extra > pathmax) {
1145*7c478bd9Sstevel@tonic-gate 				break;
1146*7c478bd9Sstevel@tonic-gate 			}
1147*7c478bd9Sstevel@tonic-gate 			if ((nmp = dupmsg(mp)) == NULL)
1148*7c478bd9Sstevel@tonic-gate 				break;
1149*7c478bd9Sstevel@tonic-gate 			if (extra > 0) {
1150*7c478bd9Sstevel@tonic-gate 				fill = sctp_get_padding(extra);
1151*7c478bd9Sstevel@tonic-gate 				if (fill != NULL) {
1152*7c478bd9Sstevel@tonic-gate 					pad += extra;
1153*7c478bd9Sstevel@tonic-gate 					new_len += extra;
1154*7c478bd9Sstevel@tonic-gate 					linkb(nmp, fill);
1155*7c478bd9Sstevel@tonic-gate 				} else {
1156*7c478bd9Sstevel@tonic-gate 					freemsg(nmp);
1157*7c478bd9Sstevel@tonic-gate 					break;
1158*7c478bd9Sstevel@tonic-gate 				}
1159*7c478bd9Sstevel@tonic-gate 			}
1160*7c478bd9Sstevel@tonic-gate 			seglen = new_len;
1161*7c478bd9Sstevel@tonic-gate 			xtralen = new_xtralen;
1162*7c478bd9Sstevel@tonic-gate 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1163*7c478bd9Sstevel@tonic-gate 			SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
1164*7c478bd9Sstevel@tonic-gate 			linkb(head, nmp);
1165*7c478bd9Sstevel@tonic-gate 			mp = mp->b_next;
1166*7c478bd9Sstevel@tonic-gate 		}
1167*7c478bd9Sstevel@tonic-gate 		if ((seglen > fp->sfa_pmss) && fp->isv4) {
1168*7c478bd9Sstevel@tonic-gate 			ipha_t *iph = (ipha_t *)head->b_rptr;
1169*7c478bd9Sstevel@tonic-gate 
1170*7c478bd9Sstevel@tonic-gate 			/*
1171*7c478bd9Sstevel@tonic-gate 			 * Path MTU is different from what we thought it would
1172*7c478bd9Sstevel@tonic-gate 			 * be when we created chunks, or IP headers have grown.
1173*7c478bd9Sstevel@tonic-gate 			 * Need to clear the DF bit.
1174*7c478bd9Sstevel@tonic-gate 			 */
1175*7c478bd9Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = 0;
1176*7c478bd9Sstevel@tonic-gate 		}
1177*7c478bd9Sstevel@tonic-gate 		/* xmit segment */
1178*7c478bd9Sstevel@tonic-gate 		ASSERT(cansend >= seglen - pad - xtralen);
1179*7c478bd9Sstevel@tonic-gate 		cansend -= (seglen - pad - xtralen);
1180*7c478bd9Sstevel@tonic-gate 		dprint(2, ("sctp_output: Sending packet %d bytes, tsn %x "
1181*7c478bd9Sstevel@tonic-gate 			"ssn %d to %p (rwnd %d, cansend %d, lastack_rxd %x)\n",
1182*7c478bd9Sstevel@tonic-gate 			seglen - xtralen, ntohl(sdc->sdh_tsn),
1183*7c478bd9Sstevel@tonic-gate 			ntohs(sdc->sdh_ssn), fp, sctp->sctp_frwnd, cansend,
1184*7c478bd9Sstevel@tonic-gate 			sctp->sctp_lastack_rxd));
1185*7c478bd9Sstevel@tonic-gate 		sctp_set_iplen(sctp, head);
1186*7c478bd9Sstevel@tonic-gate 		sctp_add_sendq(sctp, head);
1187*7c478bd9Sstevel@tonic-gate 		/* arm rto timer (if not set) */
1188*7c478bd9Sstevel@tonic-gate 		if (!fp->timer_running)
1189*7c478bd9Sstevel@tonic-gate 			SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1190*7c478bd9Sstevel@tonic-gate 	}
1191*7c478bd9Sstevel@tonic-gate 	sctp->sctp_active = now;
1192*7c478bd9Sstevel@tonic-gate 	return;
1193*7c478bd9Sstevel@tonic-gate unsent_data:
1194*7c478bd9Sstevel@tonic-gate 	/* arm persist timer (if rto timer not set) */
1195*7c478bd9Sstevel@tonic-gate 	if (!fp->timer_running)
1196*7c478bd9Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1197*7c478bd9Sstevel@tonic-gate 	if (head != NULL)
1198*7c478bd9Sstevel@tonic-gate 		freemsg(head);
1199*7c478bd9Sstevel@tonic-gate }
1200*7c478bd9Sstevel@tonic-gate 
1201*7c478bd9Sstevel@tonic-gate /*
1202*7c478bd9Sstevel@tonic-gate  * The following two functions initialize and destroy the cache
1203*7c478bd9Sstevel@tonic-gate  * associated with the sets used for PR-SCTP.
1204*7c478bd9Sstevel@tonic-gate  */
1205*7c478bd9Sstevel@tonic-gate void
1206*7c478bd9Sstevel@tonic-gate sctp_ftsn_sets_init(void)
1207*7c478bd9Sstevel@tonic-gate {
1208*7c478bd9Sstevel@tonic-gate 	sctp_kmem_ftsn_set_cache = kmem_cache_create("sctp_ftsn_set_cache",
1209*7c478bd9Sstevel@tonic-gate 	    sizeof (sctp_ftsn_set_t), 0, NULL, NULL, NULL, NULL,
1210*7c478bd9Sstevel@tonic-gate 	    NULL, 0);
1211*7c478bd9Sstevel@tonic-gate }
1212*7c478bd9Sstevel@tonic-gate 
1213*7c478bd9Sstevel@tonic-gate void
1214*7c478bd9Sstevel@tonic-gate sctp_ftsn_sets_fini(void)
1215*7c478bd9Sstevel@tonic-gate {
1216*7c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_ftsn_set_cache);
1217*7c478bd9Sstevel@tonic-gate }
1218*7c478bd9Sstevel@tonic-gate 
1219*7c478bd9Sstevel@tonic-gate 
1220*7c478bd9Sstevel@tonic-gate /* Free PR-SCTP sets */
1221*7c478bd9Sstevel@tonic-gate void
1222*7c478bd9Sstevel@tonic-gate sctp_free_ftsn_set(sctp_ftsn_set_t *s)
1223*7c478bd9Sstevel@tonic-gate {
1224*7c478bd9Sstevel@tonic-gate 	sctp_ftsn_set_t *p;
1225*7c478bd9Sstevel@tonic-gate 
1226*7c478bd9Sstevel@tonic-gate 	while (s != NULL) {
1227*7c478bd9Sstevel@tonic-gate 		p = s->next;
1228*7c478bd9Sstevel@tonic-gate 		s->next = NULL;
1229*7c478bd9Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_ftsn_set_cache, s);
1230*7c478bd9Sstevel@tonic-gate 		s = p;
1231*7c478bd9Sstevel@tonic-gate 	}
1232*7c478bd9Sstevel@tonic-gate }
1233*7c478bd9Sstevel@tonic-gate 
1234*7c478bd9Sstevel@tonic-gate /*
1235*7c478bd9Sstevel@tonic-gate  * Given a message meta block, meta, this routine creates or modifies
1236*7c478bd9Sstevel@tonic-gate  * the set that will be used to generate a Forward TSN chunk. If the
1237*7c478bd9Sstevel@tonic-gate  * entry for stream id, sid, for this message already exists, the
1238*7c478bd9Sstevel@tonic-gate  * sequence number, ssn, is updated if it is greater than the existing
1239*7c478bd9Sstevel@tonic-gate  * one. If an entry for this sid does not exist, one is created if
1240*7c478bd9Sstevel@tonic-gate  * the size does not exceed fp->sfa_pmss. We return false in case
1241*7c478bd9Sstevel@tonic-gate  * or an error.
1242*7c478bd9Sstevel@tonic-gate  */
1243*7c478bd9Sstevel@tonic-gate boolean_t
1244*7c478bd9Sstevel@tonic-gate sctp_add_ftsn_set(sctp_ftsn_set_t **s, sctp_faddr_t *fp, mblk_t *meta,
1245*7c478bd9Sstevel@tonic-gate     uint_t *nsets, uint32_t *slen)
1246*7c478bd9Sstevel@tonic-gate {
1247*7c478bd9Sstevel@tonic-gate 	sctp_ftsn_set_t		*p;
1248*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr_t		*msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
1249*7c478bd9Sstevel@tonic-gate 	uint16_t		sid = htons(msg_hdr->smh_sid);
1250*7c478bd9Sstevel@tonic-gate 	/* msg_hdr->smh_ssn is already in NBO */
1251*7c478bd9Sstevel@tonic-gate 	uint16_t		ssn = msg_hdr->smh_ssn;
1252*7c478bd9Sstevel@tonic-gate 
1253*7c478bd9Sstevel@tonic-gate 	ASSERT(s != NULL && nsets != NULL);
1254*7c478bd9Sstevel@tonic-gate 	ASSERT((*nsets == 0 && *s == NULL) || (*nsets > 0 && *s != NULL));
1255*7c478bd9Sstevel@tonic-gate 
1256*7c478bd9Sstevel@tonic-gate 	if (*s == NULL) {
1257*7c478bd9Sstevel@tonic-gate 		ASSERT((*slen + sizeof (uint32_t)) <= fp->sfa_pmss);
1258*7c478bd9Sstevel@tonic-gate 		*s = kmem_cache_alloc(sctp_kmem_ftsn_set_cache, KM_NOSLEEP);
1259*7c478bd9Sstevel@tonic-gate 		if (*s == NULL)
1260*7c478bd9Sstevel@tonic-gate 			return (B_FALSE);
1261*7c478bd9Sstevel@tonic-gate 		(*s)->ftsn_entries.ftsn_sid = sid;
1262*7c478bd9Sstevel@tonic-gate 		(*s)->ftsn_entries.ftsn_ssn = ssn;
1263*7c478bd9Sstevel@tonic-gate 		(*s)->next = NULL;
1264*7c478bd9Sstevel@tonic-gate 		*nsets = 1;
1265*7c478bd9Sstevel@tonic-gate 		*slen += sizeof (uint32_t);
1266*7c478bd9Sstevel@tonic-gate 		return (B_TRUE);
1267*7c478bd9Sstevel@tonic-gate 	}
1268*7c478bd9Sstevel@tonic-gate 	for (p = *s; p->next != NULL; p = p->next) {
1269*7c478bd9Sstevel@tonic-gate 		if (p->ftsn_entries.ftsn_sid == sid) {
1270*7c478bd9Sstevel@tonic-gate 			if (SSN_GT(ssn, p->ftsn_entries.ftsn_ssn))
1271*7c478bd9Sstevel@tonic-gate 				p->ftsn_entries.ftsn_ssn = ssn;
1272*7c478bd9Sstevel@tonic-gate 			return (B_TRUE);
1273*7c478bd9Sstevel@tonic-gate 		}
1274*7c478bd9Sstevel@tonic-gate 	}
1275*7c478bd9Sstevel@tonic-gate 	/* the last one */
1276*7c478bd9Sstevel@tonic-gate 	if (p->ftsn_entries.ftsn_sid == sid) {
1277*7c478bd9Sstevel@tonic-gate 		if (SSN_GT(ssn, p->ftsn_entries.ftsn_ssn))
1278*7c478bd9Sstevel@tonic-gate 			p->ftsn_entries.ftsn_ssn = ssn;
1279*7c478bd9Sstevel@tonic-gate 	} else {
1280*7c478bd9Sstevel@tonic-gate 		if ((*slen + sizeof (uint32_t)) > fp->sfa_pmss)
1281*7c478bd9Sstevel@tonic-gate 			return (B_FALSE);
1282*7c478bd9Sstevel@tonic-gate 		p->next = kmem_cache_alloc(sctp_kmem_ftsn_set_cache,
1283*7c478bd9Sstevel@tonic-gate 		    KM_NOSLEEP);
1284*7c478bd9Sstevel@tonic-gate 		if (p->next == NULL)
1285*7c478bd9Sstevel@tonic-gate 			return (B_FALSE);
1286*7c478bd9Sstevel@tonic-gate 		p = p->next;
1287*7c478bd9Sstevel@tonic-gate 		p->ftsn_entries.ftsn_sid = sid;
1288*7c478bd9Sstevel@tonic-gate 		p->ftsn_entries.ftsn_ssn = ssn;
1289*7c478bd9Sstevel@tonic-gate 		p->next = NULL;
1290*7c478bd9Sstevel@tonic-gate 		(*nsets)++;
1291*7c478bd9Sstevel@tonic-gate 		*slen += sizeof (uint32_t);
1292*7c478bd9Sstevel@tonic-gate 	}
1293*7c478bd9Sstevel@tonic-gate 	return (B_TRUE);
1294*7c478bd9Sstevel@tonic-gate }
1295*7c478bd9Sstevel@tonic-gate 
1296*7c478bd9Sstevel@tonic-gate /*
1297*7c478bd9Sstevel@tonic-gate  * Given a set of stream id - sequence number pairs, this routing creates
1298*7c478bd9Sstevel@tonic-gate  * a Forward TSN chunk. The cumulative TSN (advanced peer ack point)
1299*7c478bd9Sstevel@tonic-gate  * for the chunk is obtained from sctp->sctp_adv_pap. The caller
1300*7c478bd9Sstevel@tonic-gate  * will add the IP/SCTP header.
1301*7c478bd9Sstevel@tonic-gate  */
1302*7c478bd9Sstevel@tonic-gate mblk_t *
1303*7c478bd9Sstevel@tonic-gate sctp_make_ftsn_chunk(sctp_t *sctp, sctp_faddr_t *fp, sctp_ftsn_set_t *sets,
1304*7c478bd9Sstevel@tonic-gate     uint_t nsets, uint32_t seglen)
1305*7c478bd9Sstevel@tonic-gate {
1306*7c478bd9Sstevel@tonic-gate 	mblk_t			*ftsn_mp;
1307*7c478bd9Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ch_hdr;
1308*7c478bd9Sstevel@tonic-gate 	uint32_t		*advtsn;
1309*7c478bd9Sstevel@tonic-gate 	uint16_t		schlen;
1310*7c478bd9Sstevel@tonic-gate 	size_t			xtralen;
1311*7c478bd9Sstevel@tonic-gate 	ftsn_entry_t		*ftsn_entry;
1312*7c478bd9Sstevel@tonic-gate 
1313*7c478bd9Sstevel@tonic-gate 	seglen += sizeof (sctp_chunk_hdr_t);
1314*7c478bd9Sstevel@tonic-gate 	if (fp->isv4)
1315*7c478bd9Sstevel@tonic-gate 		xtralen = sctp->sctp_hdr_len + sctp_wroff_xtra;
1316*7c478bd9Sstevel@tonic-gate 	else
1317*7c478bd9Sstevel@tonic-gate 		xtralen = sctp->sctp_hdr6_len + sctp_wroff_xtra;
1318*7c478bd9Sstevel@tonic-gate 	ftsn_mp = allocb(xtralen + seglen, BPRI_MED);
1319*7c478bd9Sstevel@tonic-gate 	if (ftsn_mp == NULL)
1320*7c478bd9Sstevel@tonic-gate 		return (NULL);
1321*7c478bd9Sstevel@tonic-gate 	ftsn_mp->b_rptr += xtralen;
1322*7c478bd9Sstevel@tonic-gate 	ftsn_mp->b_wptr = ftsn_mp->b_rptr + seglen;
1323*7c478bd9Sstevel@tonic-gate 
1324*7c478bd9Sstevel@tonic-gate 	ch_hdr = (sctp_chunk_hdr_t *)ftsn_mp->b_rptr;
1325*7c478bd9Sstevel@tonic-gate 	ch_hdr->sch_id = CHUNK_FORWARD_TSN;
1326*7c478bd9Sstevel@tonic-gate 	ch_hdr->sch_flags = 0;
1327*7c478bd9Sstevel@tonic-gate 	/*
1328*7c478bd9Sstevel@tonic-gate 	 * The cast here should not be an issue since seglen is
1329*7c478bd9Sstevel@tonic-gate 	 * the length of the Forward TSN chunk.
1330*7c478bd9Sstevel@tonic-gate 	 */
1331*7c478bd9Sstevel@tonic-gate 	schlen = (uint16_t)seglen;
1332*7c478bd9Sstevel@tonic-gate 	U16_TO_ABE16(schlen, &(ch_hdr->sch_len));
1333*7c478bd9Sstevel@tonic-gate 
1334*7c478bd9Sstevel@tonic-gate 	advtsn = (uint32_t *)(ch_hdr + 1);
1335*7c478bd9Sstevel@tonic-gate 	U32_TO_ABE32(sctp->sctp_adv_pap, advtsn);
1336*7c478bd9Sstevel@tonic-gate 	ftsn_entry = (ftsn_entry_t *)(advtsn + 1);
1337*7c478bd9Sstevel@tonic-gate 	while (nsets > 0) {
1338*7c478bd9Sstevel@tonic-gate 		ASSERT((uchar_t *)&ftsn_entry[1] <= ftsn_mp->b_wptr);
1339*7c478bd9Sstevel@tonic-gate 		ftsn_entry->ftsn_sid = sets->ftsn_entries.ftsn_sid;
1340*7c478bd9Sstevel@tonic-gate 		ftsn_entry->ftsn_ssn = sets->ftsn_entries.ftsn_ssn;
1341*7c478bd9Sstevel@tonic-gate 		ftsn_entry++;
1342*7c478bd9Sstevel@tonic-gate 		sets = sets->next;
1343*7c478bd9Sstevel@tonic-gate 		nsets--;
1344*7c478bd9Sstevel@tonic-gate 	}
1345*7c478bd9Sstevel@tonic-gate 	return (ftsn_mp);
1346*7c478bd9Sstevel@tonic-gate }
1347*7c478bd9Sstevel@tonic-gate 
1348*7c478bd9Sstevel@tonic-gate /*
1349*7c478bd9Sstevel@tonic-gate  * Given a starting message, the routine steps through all the
1350*7c478bd9Sstevel@tonic-gate  * messages whose TSN is less than sctp->sctp_adv_pap and creates
1351*7c478bd9Sstevel@tonic-gate  * ftsn sets. The ftsn sets is then used to create an Forward TSN
1352*7c478bd9Sstevel@tonic-gate  * chunk. All the messages, that have chunks that are included in the
1353*7c478bd9Sstevel@tonic-gate  * ftsn sets, are flagged abandonded. If a message is partially sent
1354*7c478bd9Sstevel@tonic-gate  * and is deemed abandoned, all remaining unsent chunks are marked
1355*7c478bd9Sstevel@tonic-gate  * abandoned and are deducted from sctp_unsent.
1356*7c478bd9Sstevel@tonic-gate  */
1357*7c478bd9Sstevel@tonic-gate void
1358*7c478bd9Sstevel@tonic-gate sctp_make_ftsns(sctp_t *sctp, mblk_t *meta, mblk_t *mp, mblk_t **nmp,
1359*7c478bd9Sstevel@tonic-gate     sctp_faddr_t *fp, uint32_t *seglen)
1360*7c478bd9Sstevel@tonic-gate {
1361*7c478bd9Sstevel@tonic-gate 	mblk_t		*mp1 = mp;
1362*7c478bd9Sstevel@tonic-gate 	mblk_t		*mp_head = mp;
1363*7c478bd9Sstevel@tonic-gate 	mblk_t		*meta_head = meta;
1364*7c478bd9Sstevel@tonic-gate 	mblk_t		*head;
1365*7c478bd9Sstevel@tonic-gate 	sctp_ftsn_set_t	*sets = NULL;
1366*7c478bd9Sstevel@tonic-gate 	uint_t		nsets = 0;
1367*7c478bd9Sstevel@tonic-gate 	uint16_t	clen;
1368*7c478bd9Sstevel@tonic-gate 	sctp_data_hdr_t	*sdc;
1369*7c478bd9Sstevel@tonic-gate 	uint32_t	sacklen;
1370*7c478bd9Sstevel@tonic-gate 	uint32_t	adv_pap = sctp->sctp_adv_pap;
1371*7c478bd9Sstevel@tonic-gate 	uint32_t	unsent = 0;
1372*7c478bd9Sstevel@tonic-gate 	boolean_t	ubit;
1373*7c478bd9Sstevel@tonic-gate 
1374*7c478bd9Sstevel@tonic-gate 	*seglen = sizeof (uint32_t);
1375*7c478bd9Sstevel@tonic-gate 
1376*7c478bd9Sstevel@tonic-gate 	sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
1377*7c478bd9Sstevel@tonic-gate 	while (meta != NULL &&
1378*7c478bd9Sstevel@tonic-gate 	    SEQ_GEQ(sctp->sctp_adv_pap, ntohl(sdc->sdh_tsn))) {
1379*7c478bd9Sstevel@tonic-gate 		/*
1380*7c478bd9Sstevel@tonic-gate 		 * Skip adding FTSN sets for un-ordered messages as they do
1381*7c478bd9Sstevel@tonic-gate 		 * not have SSNs.
1382*7c478bd9Sstevel@tonic-gate 		 */
1383*7c478bd9Sstevel@tonic-gate 		ubit = SCTP_DATA_GET_UBIT(sdc);
1384*7c478bd9Sstevel@tonic-gate 		if (!ubit &&
1385*7c478bd9Sstevel@tonic-gate 		    !sctp_add_ftsn_set(&sets, fp, meta, &nsets, seglen)) {
1386*7c478bd9Sstevel@tonic-gate 			meta = NULL;
1387*7c478bd9Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
1388*7c478bd9Sstevel@tonic-gate 			goto ftsn_done;
1389*7c478bd9Sstevel@tonic-gate 		}
1390*7c478bd9Sstevel@tonic-gate 		while (mp1 != NULL && SCTP_CHUNK_ISSENT(mp1)) {
1391*7c478bd9Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
1392*7c478bd9Sstevel@tonic-gate 			adv_pap = ntohl(sdc->sdh_tsn);
1393*7c478bd9Sstevel@tonic-gate 			mp1 = mp1->b_next;
1394*7c478bd9Sstevel@tonic-gate 		}
1395*7c478bd9Sstevel@tonic-gate 		meta = meta->b_next;
1396*7c478bd9Sstevel@tonic-gate 		if (meta != NULL) {
1397*7c478bd9Sstevel@tonic-gate 			mp1 = meta->b_cont;
1398*7c478bd9Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp1))
1399*7c478bd9Sstevel@tonic-gate 				break;
1400*7c478bd9Sstevel@tonic-gate 			sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
1401*7c478bd9Sstevel@tonic-gate 		}
1402*7c478bd9Sstevel@tonic-gate 	}
1403*7c478bd9Sstevel@tonic-gate ftsn_done:
1404*7c478bd9Sstevel@tonic-gate 	/*
1405*7c478bd9Sstevel@tonic-gate 	 * Can't compare with sets == NULL, since we don't add any
1406*7c478bd9Sstevel@tonic-gate 	 * sets for un-ordered messages.
1407*7c478bd9Sstevel@tonic-gate 	 */
1408*7c478bd9Sstevel@tonic-gate 	if (meta == meta_head)
1409*7c478bd9Sstevel@tonic-gate 		return;
1410*7c478bd9Sstevel@tonic-gate 	*nmp = sctp_make_ftsn_chunk(sctp, fp, sets, nsets, *seglen);
1411*7c478bd9Sstevel@tonic-gate 	sctp_free_ftsn_set(sets);
1412*7c478bd9Sstevel@tonic-gate 	if (*nmp == NULL)
1413*7c478bd9Sstevel@tonic-gate 		return;
1414*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
1415*7c478bd9Sstevel@tonic-gate 		sacklen = 0;
1416*7c478bd9Sstevel@tonic-gate 	} else {
1417*7c478bd9Sstevel@tonic-gate 		sacklen = sizeof (sctp_chunk_hdr_t) +
1418*7c478bd9Sstevel@tonic-gate 		    sizeof (sctp_sack_chunk_t) +
1419*7c478bd9Sstevel@tonic-gate 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
1420*7c478bd9Sstevel@tonic-gate 		if (*seglen + sacklen > sctp->sctp_lastdata->sfa_pmss) {
1421*7c478bd9Sstevel@tonic-gate 			/* piggybacked SACK doesn't fit */
1422*7c478bd9Sstevel@tonic-gate 			sacklen = 0;
1423*7c478bd9Sstevel@tonic-gate 		} else {
1424*7c478bd9Sstevel@tonic-gate 			fp = sctp->sctp_lastdata;
1425*7c478bd9Sstevel@tonic-gate 		}
1426*7c478bd9Sstevel@tonic-gate 	}
1427*7c478bd9Sstevel@tonic-gate 	head = sctp_add_proto_hdr(sctp, fp, *nmp, sacklen);
1428*7c478bd9Sstevel@tonic-gate 	if (head == NULL) {
1429*7c478bd9Sstevel@tonic-gate 		freemsg(*nmp);
1430*7c478bd9Sstevel@tonic-gate 		*nmp = NULL;
1431*7c478bd9Sstevel@tonic-gate 		return;
1432*7c478bd9Sstevel@tonic-gate 	}
1433*7c478bd9Sstevel@tonic-gate 	*seglen += sacklen;
1434*7c478bd9Sstevel@tonic-gate 	*nmp = head;
1435*7c478bd9Sstevel@tonic-gate 
1436*7c478bd9Sstevel@tonic-gate 	/*
1437*7c478bd9Sstevel@tonic-gate 	 * XXXNeed to optimise this, the reason it is done here is so
1438*7c478bd9Sstevel@tonic-gate 	 * that we don't have to undo in case of failure.
1439*7c478bd9Sstevel@tonic-gate 	 */
1440*7c478bd9Sstevel@tonic-gate 	mp1 = mp_head;
1441*7c478bd9Sstevel@tonic-gate 	sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
1442*7c478bd9Sstevel@tonic-gate 	while (meta_head != NULL &&
1443*7c478bd9Sstevel@tonic-gate 	    SEQ_GEQ(sctp->sctp_adv_pap, ntohl(sdc->sdh_tsn))) {
1444*7c478bd9Sstevel@tonic-gate 		if (!SCTP_IS_MSG_ABANDONED(meta_head))
1445*7c478bd9Sstevel@tonic-gate 			SCTP_MSG_SET_ABANDONED(meta_head);
1446*7c478bd9Sstevel@tonic-gate 		while (mp1 != NULL && SCTP_CHUNK_ISSENT(mp1)) {
1447*7c478bd9Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
1448*7c478bd9Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISACKED(mp1)) {
1449*7c478bd9Sstevel@tonic-gate 				clen = ntohs(sdc->sdh_len) - sizeof (*sdc);
1450*7c478bd9Sstevel@tonic-gate 				SCTP_CHUNK_SENT(sctp, mp1, sdc, fp, clen,
1451*7c478bd9Sstevel@tonic-gate 				    meta_head);
1452*7c478bd9Sstevel@tonic-gate 			}
1453*7c478bd9Sstevel@tonic-gate 			mp1 = mp1->b_next;
1454*7c478bd9Sstevel@tonic-gate 		}
1455*7c478bd9Sstevel@tonic-gate 		while (mp1 != NULL) {
1456*7c478bd9Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
1457*7c478bd9Sstevel@tonic-gate 			if (!SCTP_CHUNK_ABANDONED(mp1)) {
1458*7c478bd9Sstevel@tonic-gate 				ASSERT(!SCTP_CHUNK_ISSENT(mp1));
1459*7c478bd9Sstevel@tonic-gate 				unsent += ntohs(sdc->sdh_len) - sizeof (*sdc);
1460*7c478bd9Sstevel@tonic-gate 				SCTP_ABANDON_CHUNK(mp1);
1461*7c478bd9Sstevel@tonic-gate 			}
1462*7c478bd9Sstevel@tonic-gate 			mp1 = mp1->b_next;
1463*7c478bd9Sstevel@tonic-gate 		}
1464*7c478bd9Sstevel@tonic-gate 		meta_head = meta_head->b_next;
1465*7c478bd9Sstevel@tonic-gate 		if (meta_head != NULL) {
1466*7c478bd9Sstevel@tonic-gate 			mp1 = meta_head->b_cont;
1467*7c478bd9Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp1))
1468*7c478bd9Sstevel@tonic-gate 				break;
1469*7c478bd9Sstevel@tonic-gate 			sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
1470*7c478bd9Sstevel@tonic-gate 		}
1471*7c478bd9Sstevel@tonic-gate 	}
1472*7c478bd9Sstevel@tonic-gate 	if (unsent > 0) {
1473*7c478bd9Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent >= unsent);
1474*7c478bd9Sstevel@tonic-gate 		sctp->sctp_unsent -= unsent;
1475*7c478bd9Sstevel@tonic-gate 		/*
1476*7c478bd9Sstevel@tonic-gate 		 * Update ULP the amount of queued data, which is
1477*7c478bd9Sstevel@tonic-gate 		 * sent-unack'ed + unsent.
1478*7c478bd9Sstevel@tonic-gate 		 */
1479*7c478bd9Sstevel@tonic-gate 		if (!SCTP_IS_DETACHED(sctp)) {
1480*7c478bd9Sstevel@tonic-gate 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
1481*7c478bd9Sstevel@tonic-gate 			    sctp->sctp_unacked + sctp->sctp_unsent);
1482*7c478bd9Sstevel@tonic-gate 		}
1483*7c478bd9Sstevel@tonic-gate 	}
1484*7c478bd9Sstevel@tonic-gate }
1485*7c478bd9Sstevel@tonic-gate 
1486*7c478bd9Sstevel@tonic-gate /*
1487*7c478bd9Sstevel@tonic-gate  * This function steps through messages starting at meta and checks if
1488*7c478bd9Sstevel@tonic-gate  * the message is abandoned. It stops when it hits an unsent chunk or
1489*7c478bd9Sstevel@tonic-gate  * a message that has all its chunk acked. This is the only place
1490*7c478bd9Sstevel@tonic-gate  * where the sctp_adv_pap is moved forward to indicated abandoned
1491*7c478bd9Sstevel@tonic-gate  * messages.
1492*7c478bd9Sstevel@tonic-gate  */
1493*7c478bd9Sstevel@tonic-gate void
1494*7c478bd9Sstevel@tonic-gate sctp_check_adv_ack_pt(sctp_t *sctp, mblk_t *meta, mblk_t *mp)
1495*7c478bd9Sstevel@tonic-gate {
1496*7c478bd9Sstevel@tonic-gate 	uint32_t	tsn = sctp->sctp_adv_pap;
1497*7c478bd9Sstevel@tonic-gate 	sctp_data_hdr_t	*sdc;
1498*7c478bd9Sstevel@tonic-gate 	sctp_msg_hdr_t	*msg_hdr;
1499*7c478bd9Sstevel@tonic-gate 
1500*7c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL);
1501*7c478bd9Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
1502*7c478bd9Sstevel@tonic-gate 	ASSERT(SEQ_GT(ntohl(sdc->sdh_tsn), sctp->sctp_lastack_rxd));
1503*7c478bd9Sstevel@tonic-gate 	msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
1504*7c478bd9Sstevel@tonic-gate 	if (!SCTP_IS_MSG_ABANDONED(meta) &&
1505*7c478bd9Sstevel@tonic-gate 	    !SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
1506*7c478bd9Sstevel@tonic-gate 		return;
1507*7c478bd9Sstevel@tonic-gate 	}
1508*7c478bd9Sstevel@tonic-gate 	while (meta != NULL) {
1509*7c478bd9Sstevel@tonic-gate 		while (mp != NULL && SCTP_CHUNK_ISSENT(mp)) {
1510*7c478bd9Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
1511*7c478bd9Sstevel@tonic-gate 			tsn = ntohl(sdc->sdh_tsn);
1512*7c478bd9Sstevel@tonic-gate 			mp = mp->b_next;
1513*7c478bd9Sstevel@tonic-gate 		}
1514*7c478bd9Sstevel@tonic-gate 		if (mp != NULL)
1515*7c478bd9Sstevel@tonic-gate 			break;
1516*7c478bd9Sstevel@tonic-gate 		/*
1517*7c478bd9Sstevel@tonic-gate 		 * We continue checking for successive messages only if there
1518*7c478bd9Sstevel@tonic-gate 		 * is a chunk marked for retransmission. Else, we might
1519*7c478bd9Sstevel@tonic-gate 		 * end up sending FTSN prematurely for chunks that have been
1520*7c478bd9Sstevel@tonic-gate 		 * sent, but not yet acked.
1521*7c478bd9Sstevel@tonic-gate 		 */
1522*7c478bd9Sstevel@tonic-gate 		if ((meta = meta->b_next) != NULL) {
1523*7c478bd9Sstevel@tonic-gate 			msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
1524*7c478bd9Sstevel@tonic-gate 			if (!SCTP_IS_MSG_ABANDONED(meta) &&
1525*7c478bd9Sstevel@tonic-gate 			    !SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
1526*7c478bd9Sstevel@tonic-gate 				break;
1527*7c478bd9Sstevel@tonic-gate 			}
1528*7c478bd9Sstevel@tonic-gate 			for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
1529*7c478bd9Sstevel@tonic-gate 				if (!SCTP_CHUNK_ISSENT(mp)) {
1530*7c478bd9Sstevel@tonic-gate 					sctp->sctp_adv_pap = tsn;
1531*7c478bd9Sstevel@tonic-gate 					return;
1532*7c478bd9Sstevel@tonic-gate 				}
1533*7c478bd9Sstevel@tonic-gate 				if (SCTP_CHUNK_WANT_REXMIT(mp))
1534*7c478bd9Sstevel@tonic-gate 					break;
1535*7c478bd9Sstevel@tonic-gate 			}
1536*7c478bd9Sstevel@tonic-gate 			if (mp == NULL)
1537*7c478bd9Sstevel@tonic-gate 				break;
1538*7c478bd9Sstevel@tonic-gate 		}
1539*7c478bd9Sstevel@tonic-gate 	}
1540*7c478bd9Sstevel@tonic-gate 	sctp->sctp_adv_pap = tsn;
1541*7c478bd9Sstevel@tonic-gate }
1542*7c478bd9Sstevel@tonic-gate 
1543*7c478bd9Sstevel@tonic-gate /*
1544*7c478bd9Sstevel@tonic-gate  * Retransmit first segment which hasn't been acked with cumtsn or send
1545*7c478bd9Sstevel@tonic-gate  * a Forward TSN chunk, if appropriate.
1546*7c478bd9Sstevel@tonic-gate  */
1547*7c478bd9Sstevel@tonic-gate void
1548*7c478bd9Sstevel@tonic-gate sctp_rexmit(sctp_t *sctp, sctp_faddr_t *oldfp)
1549*7c478bd9Sstevel@tonic-gate {
1550*7c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
1551*7c478bd9Sstevel@tonic-gate 	mblk_t		*nmp = NULL;
1552*7c478bd9Sstevel@tonic-gate 	mblk_t		*head;
1553*7c478bd9Sstevel@tonic-gate 	mblk_t		*meta = sctp->sctp_xmit_head;
1554*7c478bd9Sstevel@tonic-gate 	mblk_t		*fill;
1555*7c478bd9Sstevel@tonic-gate 	uint32_t	seglen = 0;
1556*7c478bd9Sstevel@tonic-gate 	uint32_t	sacklen;
1557*7c478bd9Sstevel@tonic-gate 	uint16_t	chunklen;
1558*7c478bd9Sstevel@tonic-gate 	int		extra;
1559*7c478bd9Sstevel@tonic-gate 	sctp_data_hdr_t	*sdc;
1560*7c478bd9Sstevel@tonic-gate 	sctp_faddr_t	*fp;
1561*7c478bd9Sstevel@tonic-gate 	int		error;
1562*7c478bd9Sstevel@tonic-gate 	uint32_t	adv_pap = sctp->sctp_adv_pap;
1563*7c478bd9Sstevel@tonic-gate 	boolean_t	do_ftsn = B_FALSE;
1564*7c478bd9Sstevel@tonic-gate 	boolean_t	ftsn_check = B_TRUE;
1565*7c478bd9Sstevel@tonic-gate 
1566*7c478bd9Sstevel@tonic-gate 	while (meta != NULL) {
1567*7c478bd9Sstevel@tonic-gate 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
1568*7c478bd9Sstevel@tonic-gate 			uint32_t	tsn;
1569*7c478bd9Sstevel@tonic-gate 
1570*7c478bd9Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp))
1571*7c478bd9Sstevel@tonic-gate 				goto window_probe;
1572*7c478bd9Sstevel@tonic-gate 			/*
1573*7c478bd9Sstevel@tonic-gate 			 * We break in the following cases -
1574*7c478bd9Sstevel@tonic-gate 			 *
1575*7c478bd9Sstevel@tonic-gate 			 *	if the advanced peer ack point includes the next
1576*7c478bd9Sstevel@tonic-gate 			 *	chunk to be retransmited - possibly the Forward
1577*7c478bd9Sstevel@tonic-gate 			 * 	TSN was lost.
1578*7c478bd9Sstevel@tonic-gate 			 *
1579*7c478bd9Sstevel@tonic-gate 			 *	if we are PRSCTP aware and the next chunk to be
1580*7c478bd9Sstevel@tonic-gate 			 *	retransmitted is now abandoned
1581*7c478bd9Sstevel@tonic-gate 			 *
1582*7c478bd9Sstevel@tonic-gate 			 *	if the next chunk to be retransmitted is for
1583*7c478bd9Sstevel@tonic-gate 			 *	the dest on which the timer went off. (this
1584*7c478bd9Sstevel@tonic-gate 			 *	message is not abandoned).
1585*7c478bd9Sstevel@tonic-gate 			 *
1586*7c478bd9Sstevel@tonic-gate 			 * We check for Forward TSN only for the first
1587*7c478bd9Sstevel@tonic-gate 			 * eligible chunk to be retransmitted. The reason
1588*7c478bd9Sstevel@tonic-gate 			 * being if the first eligible chunk is skipped (say
1589*7c478bd9Sstevel@tonic-gate 			 * it was sent to a destination other than oldfp)
1590*7c478bd9Sstevel@tonic-gate 			 * then we cannot advance the cum TSN via Forward
1591*7c478bd9Sstevel@tonic-gate 			 * TSN chunk.
1592*7c478bd9Sstevel@tonic-gate 			 *
1593*7c478bd9Sstevel@tonic-gate 			 * Also, ftsn_check is B_TRUE only for the first
1594*7c478bd9Sstevel@tonic-gate 			 * eligible chunk, it  will be B_FALSE for all
1595*7c478bd9Sstevel@tonic-gate 			 * subsequent candidate messages for retransmission.
1596*7c478bd9Sstevel@tonic-gate 			 */
1597*7c478bd9Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
1598*7c478bd9Sstevel@tonic-gate 			tsn = ntohl(sdc->sdh_tsn);
1599*7c478bd9Sstevel@tonic-gate 			if (SEQ_GT(tsn, sctp->sctp_lastack_rxd)) {
1600*7c478bd9Sstevel@tonic-gate 				if (sctp->sctp_prsctp_aware && ftsn_check) {
1601*7c478bd9Sstevel@tonic-gate 					if (SEQ_GEQ(sctp->sctp_adv_pap, tsn)) {
1602*7c478bd9Sstevel@tonic-gate 						ASSERT(sctp->sctp_prsctp_aware);
1603*7c478bd9Sstevel@tonic-gate 						do_ftsn = B_TRUE;
1604*7c478bd9Sstevel@tonic-gate 						goto out;
1605*7c478bd9Sstevel@tonic-gate 					} else {
1606*7c478bd9Sstevel@tonic-gate 						sctp_check_adv_ack_pt(sctp,
1607*7c478bd9Sstevel@tonic-gate 						    meta, mp);
1608*7c478bd9Sstevel@tonic-gate 						if (SEQ_GT(sctp->sctp_adv_pap,
1609*7c478bd9Sstevel@tonic-gate 						    adv_pap)) {
1610*7c478bd9Sstevel@tonic-gate 							do_ftsn = B_TRUE;
1611*7c478bd9Sstevel@tonic-gate 							goto out;
1612*7c478bd9Sstevel@tonic-gate 						}
1613*7c478bd9Sstevel@tonic-gate 					}
1614*7c478bd9Sstevel@tonic-gate 					ftsn_check = B_FALSE;
1615*7c478bd9Sstevel@tonic-gate 				}
1616*7c478bd9Sstevel@tonic-gate 				if (SCTP_CHUNK_DEST(mp) == oldfp)
1617*7c478bd9Sstevel@tonic-gate 					goto out;
1618*7c478bd9Sstevel@tonic-gate 			}
1619*7c478bd9Sstevel@tonic-gate 		}
1620*7c478bd9Sstevel@tonic-gate 		meta = meta->b_next;
1621*7c478bd9Sstevel@tonic-gate 		if (meta != NULL && sctp->sctp_prsctp_aware) {
1622*7c478bd9Sstevel@tonic-gate 			sctp_msg_hdr_t	*mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
1623*7c478bd9Sstevel@tonic-gate 
1624*7c478bd9Sstevel@tonic-gate 			while (meta != NULL && (SCTP_IS_MSG_ABANDONED(meta) ||
1625*7c478bd9Sstevel@tonic-gate 			    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr, sctp))) {
1626*7c478bd9Sstevel@tonic-gate 				meta = meta->b_next;
1627*7c478bd9Sstevel@tonic-gate 			}
1628*7c478bd9Sstevel@tonic-gate 		}
1629*7c478bd9Sstevel@tonic-gate 	}
1630*7c478bd9Sstevel@tonic-gate window_probe:
1631*7c478bd9Sstevel@tonic-gate 	/*
1632*7c478bd9Sstevel@tonic-gate 	 * Retransmit fired for a destination which didn't have
1633*7c478bd9Sstevel@tonic-gate 	 * any unacked data pending.
1634*7c478bd9Sstevel@tonic-gate 	 */
1635*7c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_unacked && sctp->sctp_unsent) {
1636*7c478bd9Sstevel@tonic-gate 		/*
1637*7c478bd9Sstevel@tonic-gate 		 * Send a window probe. Inflate frwnd to allow
1638*7c478bd9Sstevel@tonic-gate 		 * sending one segment.
1639*7c478bd9Sstevel@tonic-gate 		 */
1640*7c478bd9Sstevel@tonic-gate 		if (sctp->sctp_frwnd < (oldfp->sfa_pmss - sizeof (*sdc))) {
1641*7c478bd9Sstevel@tonic-gate 			sctp->sctp_frwnd = oldfp->sfa_pmss - sizeof (*sdc);
1642*7c478bd9Sstevel@tonic-gate 		}
1643*7c478bd9Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpOutWinProbe);
1644*7c478bd9Sstevel@tonic-gate 		sctp_output(sctp);
1645*7c478bd9Sstevel@tonic-gate 	}
1646*7c478bd9Sstevel@tonic-gate 	return;
1647*7c478bd9Sstevel@tonic-gate out:
1648*7c478bd9Sstevel@tonic-gate 	/*
1649*7c478bd9Sstevel@tonic-gate 	 * Enter slowstart for this destination
1650*7c478bd9Sstevel@tonic-gate 	 */
1651*7c478bd9Sstevel@tonic-gate 	oldfp->ssthresh = oldfp->cwnd / 2;
1652*7c478bd9Sstevel@tonic-gate 	if (oldfp->ssthresh < 2 * oldfp->sfa_pmss)
1653*7c478bd9Sstevel@tonic-gate 		oldfp->ssthresh = 2 * oldfp->sfa_pmss;
1654*7c478bd9Sstevel@tonic-gate 	oldfp->cwnd = oldfp->sfa_pmss;
1655*7c478bd9Sstevel@tonic-gate 	oldfp->pba = 0;
1656*7c478bd9Sstevel@tonic-gate 	fp = sctp_rotate_faddr(sctp, oldfp);
1657*7c478bd9Sstevel@tonic-gate 	ASSERT(fp != NULL);
1658*7c478bd9Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
1659*7c478bd9Sstevel@tonic-gate 
1660*7c478bd9Sstevel@tonic-gate 	if (do_ftsn) {
1661*7c478bd9Sstevel@tonic-gate 		sctp_make_ftsns(sctp, meta, mp, &nmp, fp, &seglen);
1662*7c478bd9Sstevel@tonic-gate 		if (nmp == NULL) {
1663*7c478bd9Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
1664*7c478bd9Sstevel@tonic-gate 			goto restart_timer;
1665*7c478bd9Sstevel@tonic-gate 		}
1666*7c478bd9Sstevel@tonic-gate 		head = nmp;
1667*7c478bd9Sstevel@tonic-gate 		mp = NULL;
1668*7c478bd9Sstevel@tonic-gate 		meta = sctp->sctp_xmit_tail;
1669*7c478bd9Sstevel@tonic-gate 		if (meta != NULL)
1670*7c478bd9Sstevel@tonic-gate 			mp = meta->b_cont;
1671*7c478bd9Sstevel@tonic-gate 		goto try_bundle;
1672*7c478bd9Sstevel@tonic-gate 	}
1673*7c478bd9Sstevel@tonic-gate 	seglen = ntohs(sdc->sdh_len);
1674*7c478bd9Sstevel@tonic-gate 	chunklen = seglen - sizeof (*sdc);
1675*7c478bd9Sstevel@tonic-gate 	if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
1676*7c478bd9Sstevel@tonic-gate 		extra = SCTP_ALIGN - extra;
1677*7c478bd9Sstevel@tonic-gate 
1678*7c478bd9Sstevel@tonic-gate 	/*
1679*7c478bd9Sstevel@tonic-gate 	 * Cancel RTT measurement if the retransmitted TSN is before the
1680*7c478bd9Sstevel@tonic-gate 	 * TSN used for timimg.
1681*7c478bd9Sstevel@tonic-gate 	 */
1682*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_out_time != 0 &&
1683*7c478bd9Sstevel@tonic-gate 	    SEQ_GEQ(sctp->sctp_rtt_tsn, sdc->sdh_tsn)) {
1684*7c478bd9Sstevel@tonic-gate 		sctp->sctp_out_time = 0;
1685*7c478bd9Sstevel@tonic-gate 	}
1686*7c478bd9Sstevel@tonic-gate 	/* Clear the counter as the RTT calculation may be off. */
1687*7c478bd9Sstevel@tonic-gate 	fp->rtt_updates = 0;
1688*7c478bd9Sstevel@tonic-gate 
1689*7c478bd9Sstevel@tonic-gate 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
1690*7c478bd9Sstevel@tonic-gate 		sacklen = 0;
1691*7c478bd9Sstevel@tonic-gate 	} else {
1692*7c478bd9Sstevel@tonic-gate 		sacklen = sizeof (sctp_chunk_hdr_t) +
1693*7c478bd9Sstevel@tonic-gate 		    sizeof (sctp_sack_chunk_t) +
1694*7c478bd9Sstevel@tonic-gate 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
1695*7c478bd9Sstevel@tonic-gate 		if (seglen + sacklen > sctp->sctp_lastdata->sfa_pmss) {
1696*7c478bd9Sstevel@tonic-gate 			/* piggybacked SACK doesn't fit */
1697*7c478bd9Sstevel@tonic-gate 			sacklen = 0;
1698*7c478bd9Sstevel@tonic-gate 		} else {
1699*7c478bd9Sstevel@tonic-gate 			fp = sctp->sctp_lastdata;
1700*7c478bd9Sstevel@tonic-gate 		}
1701*7c478bd9Sstevel@tonic-gate 	}
1702*7c478bd9Sstevel@tonic-gate 
1703*7c478bd9Sstevel@tonic-gate 	nmp = dupmsg(mp);
1704*7c478bd9Sstevel@tonic-gate 	if (nmp == NULL)
1705*7c478bd9Sstevel@tonic-gate 		goto restart_timer;
1706*7c478bd9Sstevel@tonic-gate 	if (extra > 0) {
1707*7c478bd9Sstevel@tonic-gate 		fill = sctp_get_padding(extra);
1708*7c478bd9Sstevel@tonic-gate 		if (fill != NULL) {
1709*7c478bd9Sstevel@tonic-gate 			linkb(nmp, fill);
1710*7c478bd9Sstevel@tonic-gate 			seglen += extra;
1711*7c478bd9Sstevel@tonic-gate 		} else {
1712*7c478bd9Sstevel@tonic-gate 			freemsg(nmp);
1713*7c478bd9Sstevel@tonic-gate 			goto restart_timer;
1714*7c478bd9Sstevel@tonic-gate 		}
1715*7c478bd9Sstevel@tonic-gate 	}
1716*7c478bd9Sstevel@tonic-gate 	SCTP_CHUNK_CLEAR_FLAGS(nmp);
1717*7c478bd9Sstevel@tonic-gate 	head = sctp_add_proto_hdr(sctp, fp, nmp, sacklen);
1718*7c478bd9Sstevel@tonic-gate 	if (head == NULL) {
1719*7c478bd9Sstevel@tonic-gate 		freemsg(nmp);
1720*7c478bd9Sstevel@tonic-gate 		goto restart_timer;
1721*7c478bd9Sstevel@tonic-gate 	}
1722*7c478bd9Sstevel@tonic-gate 	seglen += sacklen;
1723*7c478bd9Sstevel@tonic-gate 
1724*7c478bd9Sstevel@tonic-gate 	SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
1725*7c478bd9Sstevel@tonic-gate 
1726*7c478bd9Sstevel@tonic-gate 	mp = mp->b_next;
1727*7c478bd9Sstevel@tonic-gate try_bundle:
1728*7c478bd9Sstevel@tonic-gate 	while (seglen < fp->sfa_pmss) {
1729*7c478bd9Sstevel@tonic-gate 		int32_t new_len;
1730*7c478bd9Sstevel@tonic-gate 
1731*7c478bd9Sstevel@tonic-gate 		while (mp != NULL) {
1732*7c478bd9Sstevel@tonic-gate 			if (SCTP_CHUNK_CANSEND(mp))
1733*7c478bd9Sstevel@tonic-gate 				break;
1734*7c478bd9Sstevel@tonic-gate 			mp = mp->b_next;
1735*7c478bd9Sstevel@tonic-gate 		}
1736*7c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
1737*7c478bd9Sstevel@tonic-gate 			meta = sctp_get_msg_to_send(sctp, &mp, meta->b_next,
1738*7c478bd9Sstevel@tonic-gate 			    &error, 0, 0, oldfp);
1739*7c478bd9Sstevel@tonic-gate 			if (error != 0 || meta == NULL)
1740*7c478bd9Sstevel@tonic-gate 				break;
1741*7c478bd9Sstevel@tonic-gate 			ASSERT(mp != NULL);
1742*7c478bd9Sstevel@tonic-gate 			sctp->sctp_xmit_tail = meta;
1743*7c478bd9Sstevel@tonic-gate 		}
1744*7c478bd9Sstevel@tonic-gate 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
1745*7c478bd9Sstevel@tonic-gate 		chunklen = ntohs(sdc->sdh_len) - sizeof (*sdc);
1746*7c478bd9Sstevel@tonic-gate 		new_len = seglen + ntohs(sdc->sdh_len);
1747*7c478bd9Sstevel@tonic-gate 
1748*7c478bd9Sstevel@tonic-gate 		if (seglen & (SCTP_ALIGN - 1)) {
1749*7c478bd9Sstevel@tonic-gate 			extra = SCTP_ALIGN - (seglen & (SCTP_ALIGN - 1));
1750*7c478bd9Sstevel@tonic-gate 
1751*7c478bd9Sstevel@tonic-gate 			if (new_len + extra > fp->sfa_pmss) {
1752*7c478bd9Sstevel@tonic-gate 				break;
1753*7c478bd9Sstevel@tonic-gate 			}
1754*7c478bd9Sstevel@tonic-gate 			fill = sctp_get_padding(extra);
1755*7c478bd9Sstevel@tonic-gate 			if (fill != NULL) {
1756*7c478bd9Sstevel@tonic-gate 				new_len += extra;
1757*7c478bd9Sstevel@tonic-gate 				linkb(head, fill);
1758*7c478bd9Sstevel@tonic-gate 			} else {
1759*7c478bd9Sstevel@tonic-gate 				break;
1760*7c478bd9Sstevel@tonic-gate 			}
1761*7c478bd9Sstevel@tonic-gate 		} else {
1762*7c478bd9Sstevel@tonic-gate 			if (new_len > fp->sfa_pmss) {
1763*7c478bd9Sstevel@tonic-gate 				break;
1764*7c478bd9Sstevel@tonic-gate 			}
1765*7c478bd9Sstevel@tonic-gate 		}
1766*7c478bd9Sstevel@tonic-gate 		if ((nmp = dupmsg(mp)) == NULL) {
1767*7c478bd9Sstevel@tonic-gate 			break;
1768*7c478bd9Sstevel@tonic-gate 		}
1769*7c478bd9Sstevel@tonic-gate 		seglen = new_len;
1770*7c478bd9Sstevel@tonic-gate 
1771*7c478bd9Sstevel@tonic-gate 		SCTP_CHUNK_CLEAR_FLAGS(nmp);
1772*7c478bd9Sstevel@tonic-gate 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
1773*7c478bd9Sstevel@tonic-gate 		linkb(head, nmp);
1774*7c478bd9Sstevel@tonic-gate 		mp = mp->b_next;
1775*7c478bd9Sstevel@tonic-gate 	}
1776*7c478bd9Sstevel@tonic-gate 	if ((seglen > fp->sfa_pmss) && fp->isv4) {
1777*7c478bd9Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)head->b_rptr;
1778*7c478bd9Sstevel@tonic-gate 
1779*7c478bd9Sstevel@tonic-gate 		/*
1780*7c478bd9Sstevel@tonic-gate 		 * Path MTU is different from path we thought it would
1781*7c478bd9Sstevel@tonic-gate 		 * be when we created chunks, or IP headers have grown.
1782*7c478bd9Sstevel@tonic-gate 		 * Need to clear the DF bit.
1783*7c478bd9Sstevel@tonic-gate 		 */
1784*7c478bd9Sstevel@tonic-gate 		iph->ipha_fragment_offset_and_flags = 0;
1785*7c478bd9Sstevel@tonic-gate 	}
1786*7c478bd9Sstevel@tonic-gate 	dprint(2, ("sctp_rexmit: Sending packet %d bytes, tsn %x "
1787*7c478bd9Sstevel@tonic-gate 	    "ssn %d to %p (rwnd %d, lastack_rxd %x)\n",
1788*7c478bd9Sstevel@tonic-gate 	    seglen, ntohl(sdc->sdh_tsn), ntohs(sdc->sdh_ssn), fp,
1789*7c478bd9Sstevel@tonic-gate 	    sctp->sctp_frwnd, sctp->sctp_lastack_rxd));
1790*7c478bd9Sstevel@tonic-gate 
1791*7c478bd9Sstevel@tonic-gate 	sctp_set_iplen(sctp, head);
1792*7c478bd9Sstevel@tonic-gate 	sctp_add_sendq(sctp, head);
1793*7c478bd9Sstevel@tonic-gate 
1794*7c478bd9Sstevel@tonic-gate 	/*
1795*7c478bd9Sstevel@tonic-gate 	 * Restart timer with exponential backoff
1796*7c478bd9Sstevel@tonic-gate 	 */
1797*7c478bd9Sstevel@tonic-gate restart_timer:
1798*7c478bd9Sstevel@tonic-gate 	oldfp->strikes++;
1799*7c478bd9Sstevel@tonic-gate 	sctp->sctp_strikes++;
1800*7c478bd9Sstevel@tonic-gate 	SCTP_CALC_RXT(oldfp, sctp->sctp_rto_max);
1801*7c478bd9Sstevel@tonic-gate 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1802*7c478bd9Sstevel@tonic-gate 	if (oldfp->suna != 0)
1803*7c478bd9Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, oldfp, oldfp->rto);
1804*7c478bd9Sstevel@tonic-gate 	sctp->sctp_active = lbolt64;
1805*7c478bd9Sstevel@tonic-gate }
1806*7c478bd9Sstevel@tonic-gate 
1807*7c478bd9Sstevel@tonic-gate /*
1808*7c478bd9Sstevel@tonic-gate  * The SCTP write put procedure called from IP.
1809*7c478bd9Sstevel@tonic-gate  */
1810*7c478bd9Sstevel@tonic-gate void
1811*7c478bd9Sstevel@tonic-gate sctp_wput(queue_t *q, mblk_t *mp)
1812*7c478bd9Sstevel@tonic-gate {
1813*7c478bd9Sstevel@tonic-gate 	uchar_t		*rptr;
1814*7c478bd9Sstevel@tonic-gate 	t_scalar_t	type;
1815*7c478bd9Sstevel@tonic-gate 
1816*7c478bd9Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
1817*7c478bd9Sstevel@tonic-gate 	case M_IOCTL:
1818*7c478bd9Sstevel@tonic-gate 		sctp_wput_ioctl(q, mp);
1819*7c478bd9Sstevel@tonic-gate 		break;
1820*7c478bd9Sstevel@tonic-gate 	case M_DATA:
1821*7c478bd9Sstevel@tonic-gate 		/* Should be handled in sctp_output() */
1822*7c478bd9Sstevel@tonic-gate 		ASSERT(0);
1823*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
1824*7c478bd9Sstevel@tonic-gate 		break;
1825*7c478bd9Sstevel@tonic-gate 	case M_PROTO:
1826*7c478bd9Sstevel@tonic-gate 	case M_PCPROTO:
1827*7c478bd9Sstevel@tonic-gate 		rptr = mp->b_rptr;
1828*7c478bd9Sstevel@tonic-gate 		if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) {
1829*7c478bd9Sstevel@tonic-gate 			type = ((union T_primitives *)rptr)->type;
1830*7c478bd9Sstevel@tonic-gate 			/*
1831*7c478bd9Sstevel@tonic-gate 			 * There is no "standard" way on how to respond
1832*7c478bd9Sstevel@tonic-gate 			 * to T_CAPABILITY_REQ if a module does not
1833*7c478bd9Sstevel@tonic-gate 			 * understand it.  And the current TI mod
1834*7c478bd9Sstevel@tonic-gate 			 * has problems handling an error ack.  So we
1835*7c478bd9Sstevel@tonic-gate 			 * catch the request here and reply with a response
1836*7c478bd9Sstevel@tonic-gate 			 * which the TI mod knows how to respond to.
1837*7c478bd9Sstevel@tonic-gate 			 */
1838*7c478bd9Sstevel@tonic-gate 			switch (type) {
1839*7c478bd9Sstevel@tonic-gate 			case T_CAPABILITY_REQ:
1840*7c478bd9Sstevel@tonic-gate 				(void) putnextctl1(RD(q), M_ERROR, EPROTO);
1841*7c478bd9Sstevel@tonic-gate 				break;
1842*7c478bd9Sstevel@tonic-gate 			default:
1843*7c478bd9Sstevel@tonic-gate 				if ((mp = mi_tpi_err_ack_alloc(mp,
1844*7c478bd9Sstevel@tonic-gate 				    TNOTSUPPORT, 0)) != NULL) {
1845*7c478bd9Sstevel@tonic-gate 					qreply(q, mp);
1846*7c478bd9Sstevel@tonic-gate 					return;
1847*7c478bd9Sstevel@tonic-gate 				}
1848*7c478bd9Sstevel@tonic-gate 			}
1849*7c478bd9Sstevel@tonic-gate 		}
1850*7c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
1851*7c478bd9Sstevel@tonic-gate 	default:
1852*7c478bd9Sstevel@tonic-gate 		freemsg(mp);
1853*7c478bd9Sstevel@tonic-gate 		return;
1854*7c478bd9Sstevel@tonic-gate 	}
1855*7c478bd9Sstevel@tonic-gate }
1856