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