xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp_output.c (revision f6f4cb8ada400367a1921f6b93fb9e02f53ac5e6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/systm.h>
29 #include <sys/stream.h>
30 #include <sys/cmn_err.h>
31 #define	_SUN_TPI_VERSION 2
32 #include <sys/tihdr.h>
33 #include <sys/socket.h>
34 #include <sys/stropts.h>
35 #include <sys/strsun.h>
36 #include <sys/strsubr.h>
37 #include <sys/socketvar.h>
38 /* swilly code in sys/socketvar.h turns off DEBUG */
39 #ifdef __lint
40 #define	DEBUG
41 #endif
42 
43 #include <inet/common.h>
44 #include <inet/mi.h>
45 #include <inet/ip.h>
46 #include <inet/ip6.h>
47 #include <inet/sctp_ip.h>
48 #include <inet/ipclassifier.h>
49 
50 /*
51  * PR-SCTP comments.
52  *
53  * A message can expire before it gets to the transmit list (i.e. it is still
54  * in the unsent list - unchunked), after it gets to the transmit list, but
55  * before transmission has actually started, or after transmission has begun.
56  * Accordingly, we check for the status of a message in sctp_chunkify() when
57  * the message is being transferred from the unsent list to the transmit list;
58  * in sctp_get_msg_to_send(), when we get the next chunk from the transmit
59  * list and in sctp_rexmit() when we get the next chunk to be (re)transmitted.
60  * When we nuke a message in sctp_chunkify(), all we need to do is take it
61  * out of the unsent list and update sctp_unsent; when a message is deemed
62  * timed-out in sctp_get_msg_to_send() we can just take it out of the transmit
63  * list, update sctp_unsent IFF transmission for the message has not yet begun
64  * (i.e. !SCTP_CHUNK_ISSENT(meta->b_cont)). However, if transmission for the
65  * message has started, then we cannot just take it out of the list, we need
66  * to send Forward TSN chunk to the peer so that the peer can clear its
67  * fragment list for this message. However, we cannot just send the Forward
68  * TSN in sctp_get_msg_to_send() because there might be unacked chunks for
69  * messages preceeding this abandoned message. So, we send a Forward TSN
70  * IFF all messages prior to this abandoned message has been SACKd, if not
71  * we defer sending the Forward TSN to sctp_cumack(), which will check for
72  * this condition and send the Forward TSN via sctp_check_abandoned_msg(). In
73  * sctp_rexmit() when we check for retransmissions, we need to determine if
74  * the advanced peer ack point can be moved ahead, and if so, send a Forward
75  * TSN to the peer instead of retransmitting the chunk. Note that when
76  * we send a Forward TSN for a message, there may be yet unsent chunks for
77  * this message; we need to mark all such chunks as abandoned, so that
78  * sctp_cumack() can take the message out of the transmit list, additionally
79  * sctp_unsent need to be adjusted. Whenever sctp_unsent is updated (i.e.
80  * decremented when a message/chunk is deemed abandoned), sockfs needs to
81  * be notified so that it can adjust its idea of the queued message.
82  */
83 
84 #include "sctp_impl.h"
85 
86 static struct kmem_cache	*sctp_kmem_ftsn_set_cache;
87 
88 #ifdef	DEBUG
89 static boolean_t	sctp_verify_chain(mblk_t *, mblk_t *);
90 #endif
91 
92 /*
93  * Called to allocate a header mblk when sending data to SCTP.
94  * Data will follow in b_cont of this mblk.
95  */
96 mblk_t *
97 sctp_alloc_hdr(const char *name, int nlen, const char *control, int clen,
98     int flags)
99 {
100 	mblk_t *mp;
101 	struct T_unitdata_req *tudr;
102 	size_t size;
103 	int error;
104 
105 	size = sizeof (*tudr) + _TPI_ALIGN_TOPT(nlen) + clen;
106 	size = MAX(size, sizeof (sctp_msg_hdr_t));
107 	if (flags & SCTP_CAN_BLOCK) {
108 		mp = allocb_wait(size, BPRI_MED, 0, &error);
109 	} else {
110 		mp = allocb(size, BPRI_MED);
111 	}
112 	if (mp) {
113 		tudr = (struct T_unitdata_req *)mp->b_rptr;
114 		tudr->PRIM_type = T_UNITDATA_REQ;
115 		tudr->DEST_length = nlen;
116 		tudr->DEST_offset = sizeof (*tudr);
117 		tudr->OPT_length = clen;
118 		tudr->OPT_offset = (t_scalar_t)(sizeof (*tudr) +
119 		    _TPI_ALIGN_TOPT(nlen));
120 		if (nlen > 0)
121 			bcopy(name, tudr + 1, nlen);
122 		if (clen > 0)
123 			bcopy(control, (char *)tudr + tudr->OPT_offset, clen);
124 		mp->b_wptr += (tudr ->OPT_offset + clen);
125 		mp->b_datap->db_type = M_PROTO;
126 	}
127 	return (mp);
128 }
129 
130 /*ARGSUSED2*/
131 int
132 sctp_sendmsg(sctp_t *sctp, mblk_t *mp, int flags)
133 {
134 	sctp_faddr_t	*fp = NULL;
135 	struct T_unitdata_req	*tudr;
136 	int		error = 0;
137 	mblk_t		*mproto = mp;
138 	in6_addr_t	*addr;
139 	in6_addr_t	tmpaddr;
140 	uint16_t	sid = sctp->sctp_def_stream;
141 	uint32_t	ppid = sctp->sctp_def_ppid;
142 	uint32_t	context = sctp->sctp_def_context;
143 	uint16_t	msg_flags = sctp->sctp_def_flags;
144 	sctp_msg_hdr_t	*sctp_msg_hdr;
145 	uint32_t	msg_len = 0;
146 	uint32_t	timetolive = sctp->sctp_def_timetolive;
147 
148 	ASSERT(DB_TYPE(mproto) == M_PROTO);
149 
150 	mp = mp->b_cont;
151 	ASSERT(mp == NULL || DB_TYPE(mp) == M_DATA);
152 
153 	tudr = (struct T_unitdata_req *)mproto->b_rptr;
154 	ASSERT(tudr->PRIM_type == T_UNITDATA_REQ);
155 
156 	/* Get destination address, if specified */
157 	if (tudr->DEST_length > 0) {
158 		sin_t *sin;
159 		sin6_t *sin6;
160 
161 		sin = (struct sockaddr_in *)
162 		    (mproto->b_rptr + tudr->DEST_offset);
163 		switch (sin->sin_family) {
164 		case AF_INET:
165 			if (tudr->DEST_length < sizeof (*sin)) {
166 				return (EINVAL);
167 			}
168 			IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &tmpaddr);
169 			addr = &tmpaddr;
170 			break;
171 		case AF_INET6:
172 			if (tudr->DEST_length < sizeof (*sin6)) {
173 				return (EINVAL);
174 			}
175 			sin6 = (struct sockaddr_in6 *)
176 			    (mproto->b_rptr + tudr->DEST_offset);
177 			addr = &sin6->sin6_addr;
178 			break;
179 		default:
180 			return (EAFNOSUPPORT);
181 		}
182 		fp = sctp_lookup_faddr(sctp, addr);
183 		if (fp == NULL) {
184 			return (EINVAL);
185 		}
186 	}
187 	/* Ancillary Data? */
188 	if (tudr->OPT_length > 0) {
189 		struct cmsghdr		*cmsg;
190 		char			*cend;
191 		struct sctp_sndrcvinfo	*sndrcv;
192 
193 		cmsg = (struct cmsghdr *)(mproto->b_rptr + tudr->OPT_offset);
194 		cend = ((char *)cmsg + tudr->OPT_length);
195 		ASSERT(cend <= (char *)mproto->b_wptr);
196 
197 		for (;;) {
198 			if ((char *)(cmsg + 1) > cend ||
199 			    ((char *)cmsg + cmsg->cmsg_len) > cend) {
200 				break;
201 			}
202 			if ((cmsg->cmsg_level == IPPROTO_SCTP) &&
203 			    (cmsg->cmsg_type == SCTP_SNDRCV)) {
204 				if (cmsg->cmsg_len <
205 				    (sizeof (*sndrcv) + sizeof (*cmsg))) {
206 					return (EINVAL);
207 				}
208 				sndrcv = (struct sctp_sndrcvinfo *)(cmsg + 1);
209 				sid = sndrcv->sinfo_stream;
210 				msg_flags = sndrcv->sinfo_flags;
211 				ppid = sndrcv->sinfo_ppid;
212 				context = sndrcv->sinfo_context;
213 				timetolive = sndrcv->sinfo_timetolive;
214 				break;
215 			}
216 			if (cmsg->cmsg_len > 0)
217 				cmsg = CMSG_NEXT(cmsg);
218 			else
219 				break;
220 		}
221 	}
222 	if (msg_flags & MSG_ABORT) {
223 		if (mp && mp->b_cont) {
224 			mblk_t *pump = msgpullup(mp, -1);
225 			if (!pump) {
226 				return (ENOMEM);
227 			}
228 			freemsg(mp);
229 			mp = pump;
230 			mproto->b_cont = mp;
231 		}
232 		RUN_SCTP(sctp);
233 		sctp_user_abort(sctp, mp);
234 		freemsg(mproto);
235 		goto process_sendq;
236 	}
237 	if (mp == NULL)
238 		goto done;
239 
240 	RUN_SCTP(sctp);
241 
242 	/* Reject any new data requests if we are shutting down */
243 	if (sctp->sctp_state > SCTPS_ESTABLISHED ||
244 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
245 		error = EPIPE;
246 		goto unlock_done;
247 	}
248 
249 	/* Re-use the mproto to store relevant info. */
250 	ASSERT(MBLKSIZE(mproto) >= sizeof (*sctp_msg_hdr));
251 
252 	mproto->b_rptr = mproto->b_datap->db_base;
253 	mproto->b_wptr = mproto->b_rptr + sizeof (*sctp_msg_hdr);
254 
255 	sctp_msg_hdr = (sctp_msg_hdr_t *)mproto->b_rptr;
256 	bzero(sctp_msg_hdr, sizeof (*sctp_msg_hdr));
257 	sctp_msg_hdr->smh_context = context;
258 	sctp_msg_hdr->smh_sid = sid;
259 	sctp_msg_hdr->smh_ppid = ppid;
260 	sctp_msg_hdr->smh_flags = msg_flags;
261 	sctp_msg_hdr->smh_ttl = MSEC_TO_TICK(timetolive);
262 	sctp_msg_hdr->smh_tob = lbolt64;
263 	for (; mp != NULL; mp = mp->b_cont)
264 		msg_len += MBLKL(mp);
265 	sctp_msg_hdr->smh_msglen = msg_len;
266 
267 	/* User requested specific destination */
268 	SCTP_SET_CHUNK_DEST(mproto, fp);
269 
270 	if (sctp->sctp_state >= SCTPS_COOKIE_ECHOED &&
271 	    sid >= sctp->sctp_num_ostr) {
272 		/* Send sendfail event */
273 		sctp_sendfail_event(sctp, dupmsg(mproto), SCTP_ERR_BAD_SID,
274 		    B_FALSE);
275 		error = EINVAL;
276 		goto unlock_done;
277 	}
278 
279 	/* no data */
280 	if (msg_len == 0) {
281 		sctp_sendfail_event(sctp, dupmsg(mproto),
282 		    SCTP_ERR_NO_USR_DATA, B_FALSE);
283 		error = EINVAL;
284 		goto unlock_done;
285 	}
286 
287 	/* Add it to the unsent list */
288 	if (sctp->sctp_xmit_unsent == NULL) {
289 		sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = mproto;
290 	} else {
291 		sctp->sctp_xmit_unsent_tail->b_next = mproto;
292 		sctp->sctp_xmit_unsent_tail = mproto;
293 	}
294 	sctp->sctp_unsent += msg_len;
295 	BUMP_LOCAL(sctp->sctp_msgcount);
296 	if (sctp->sctp_state == SCTPS_ESTABLISHED)
297 		sctp_output(sctp, UINT_MAX);
298 process_sendq:
299 	WAKE_SCTP(sctp);
300 	sctp_process_sendq(sctp);
301 	return (0);
302 unlock_done:
303 	WAKE_SCTP(sctp);
304 done:
305 	return (error);
306 }
307 
308 void
309 sctp_chunkify(sctp_t *sctp, int first_len, int bytes_to_send)
310 {
311 	mblk_t			*mp;
312 	mblk_t			*chunk_mp;
313 	mblk_t			*chunk_head;
314 	mblk_t			*chunk_hdr;
315 	mblk_t			*chunk_tail = NULL;
316 	int			count;
317 	int			chunksize;
318 	sctp_data_hdr_t		*sdc;
319 	mblk_t			*mdblk = sctp->sctp_xmit_unsent;
320 	sctp_faddr_t		*fp;
321 	sctp_faddr_t		*fp1;
322 	size_t			xtralen;
323 	sctp_msg_hdr_t		*msg_hdr;
324 	sctp_stack_t	*sctps = sctp->sctp_sctps;
325 
326 	fp = SCTP_CHUNK_DEST(mdblk);
327 	if (fp == NULL)
328 		fp = sctp->sctp_current;
329 	if (fp->isv4)
330 		xtralen = sctp->sctp_hdr_len + sctps->sctps_wroff_xtra +
331 		    sizeof (*sdc);
332 	else
333 		xtralen = sctp->sctp_hdr6_len + sctps->sctps_wroff_xtra +
334 		    sizeof (*sdc);
335 	count = chunksize = first_len - sizeof (*sdc);
336 nextmsg:
337 	chunk_mp = mdblk->b_cont;
338 
339 	/*
340 	 * If this partially chunked, we ignore the first_len for now
341 	 * and use the one already present. For the unchunked bits, we
342 	 * use the length of the last chunk.
343 	 */
344 	if (SCTP_IS_MSG_CHUNKED(mdblk)) {
345 		int	chunk_len;
346 
347 		ASSERT(chunk_mp->b_next != NULL);
348 		mdblk->b_cont = chunk_mp->b_next;
349 		chunk_mp->b_next = NULL;
350 		SCTP_MSG_CLEAR_CHUNKED(mdblk);
351 		mp = mdblk->b_cont;
352 		while (mp->b_next != NULL)
353 			mp = mp->b_next;
354 		chunk_len = ntohs(((sctp_data_hdr_t *)mp->b_rptr)->sdh_len);
355 		if (fp->sfa_pmss - chunk_len > sizeof (*sdc))
356 			count = chunksize = fp->sfa_pmss - chunk_len;
357 		else
358 			count = chunksize = fp->sfa_pmss;
359 		count = chunksize = count - sizeof (*sdc);
360 	} else {
361 		msg_hdr = (sctp_msg_hdr_t *)mdblk->b_rptr;
362 		if (SCTP_MSG_TO_BE_ABANDONED(mdblk, msg_hdr, sctp)) {
363 			sctp->sctp_xmit_unsent = mdblk->b_next;
364 			if (sctp->sctp_xmit_unsent == NULL)
365 				sctp->sctp_xmit_unsent_tail = NULL;
366 			ASSERT(sctp->sctp_unsent >= msg_hdr->smh_msglen);
367 			sctp->sctp_unsent -= msg_hdr->smh_msglen;
368 			mdblk->b_next = NULL;
369 			BUMP_LOCAL(sctp->sctp_prsctpdrop);
370 			/*
371 			 * Update ULP the amount of queued data, which is
372 			 * sent-unack'ed + unsent.
373 			 */
374 			if (!SCTP_IS_DETACHED(sctp)) {
375 				sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
376 				    sctp->sctp_unacked + sctp->sctp_unsent);
377 			}
378 			sctp_sendfail_event(sctp, mdblk, 0, B_FALSE);
379 			goto try_next;
380 		}
381 		mdblk->b_cont = NULL;
382 	}
383 	msg_hdr = (sctp_msg_hdr_t *)mdblk->b_rptr;
384 nextchunk:
385 	chunk_head = chunk_mp;
386 	chunk_tail = NULL;
387 
388 	/* Skip as many mblk's as we need */
389 	while (chunk_mp != NULL && ((count - MBLKL(chunk_mp)) >= 0)) {
390 		count -= MBLKL(chunk_mp);
391 		chunk_tail = chunk_mp;
392 		chunk_mp = chunk_mp->b_cont;
393 	}
394 	/* Split the chain, if needed */
395 	if (chunk_mp != NULL) {
396 		if (count > 0) {
397 			mblk_t	*split_mp = dupb(chunk_mp);
398 
399 			if (split_mp == NULL) {
400 				if (mdblk->b_cont == NULL) {
401 					mdblk->b_cont = chunk_head;
402 				} else  {
403 					SCTP_MSG_SET_CHUNKED(mdblk);
404 					ASSERT(chunk_head->b_next == NULL);
405 					chunk_head->b_next = mdblk->b_cont;
406 					mdblk->b_cont = chunk_head;
407 				}
408 				return;
409 			}
410 			if (chunk_tail != NULL) {
411 				chunk_tail->b_cont = split_mp;
412 				chunk_tail = chunk_tail->b_cont;
413 			} else {
414 				chunk_head = chunk_tail = split_mp;
415 			}
416 			chunk_tail->b_wptr = chunk_tail->b_rptr + count;
417 			chunk_mp->b_rptr = chunk_tail->b_wptr;
418 			count = 0;
419 		} else if (chunk_tail == NULL) {
420 			goto next;
421 		} else {
422 			chunk_tail->b_cont = NULL;
423 		}
424 	}
425 	/* Alloc chunk hdr, if needed */
426 	if (DB_REF(chunk_head) > 1 ||
427 	    ((intptr_t)chunk_head->b_rptr) & (SCTP_ALIGN - 1) ||
428 	    MBLKHEAD(chunk_head) < sizeof (*sdc)) {
429 		if ((chunk_hdr = allocb(xtralen, BPRI_MED)) == NULL) {
430 			if (mdblk->b_cont == NULL) {
431 				if (chunk_mp != NULL)
432 					linkb(chunk_head, chunk_mp);
433 				mdblk->b_cont = chunk_head;
434 			} else {
435 				SCTP_MSG_SET_CHUNKED(mdblk);
436 				if (chunk_mp != NULL)
437 					linkb(chunk_head, chunk_mp);
438 				ASSERT(chunk_head->b_next == NULL);
439 				chunk_head->b_next = mdblk->b_cont;
440 				mdblk->b_cont = chunk_head;
441 			}
442 			return;
443 		}
444 		chunk_hdr->b_rptr += xtralen - sizeof (*sdc);
445 		chunk_hdr->b_wptr = chunk_hdr->b_rptr + sizeof (*sdc);
446 		chunk_hdr->b_cont = chunk_head;
447 	} else {
448 		chunk_hdr = chunk_head;
449 		chunk_hdr->b_rptr -= sizeof (*sdc);
450 	}
451 	ASSERT(chunk_hdr->b_datap->db_ref == 1);
452 	sdc = (sctp_data_hdr_t *)chunk_hdr->b_rptr;
453 	sdc->sdh_id = CHUNK_DATA;
454 	sdc->sdh_flags = 0;
455 	sdc->sdh_len = htons(sizeof (*sdc) + chunksize - count);
456 	ASSERT(sdc->sdh_len);
457 	sdc->sdh_sid = htons(msg_hdr->smh_sid);
458 	/*
459 	 * We defer assigning the SSN just before sending the chunk, else
460 	 * if we drop the chunk in sctp_get_msg_to_send(), we would need
461 	 * to send a Forward TSN to let the peer know. Some more comments
462 	 * about this in sctp_impl.h for SCTP_CHUNK_SENT.
463 	 */
464 	sdc->sdh_payload_id = msg_hdr->smh_ppid;
465 
466 	if (mdblk->b_cont == NULL) {
467 		mdblk->b_cont = chunk_hdr;
468 		SCTP_DATA_SET_BBIT(sdc);
469 	} else {
470 		mp = mdblk->b_cont;
471 		while (mp->b_next != NULL)
472 			mp = mp->b_next;
473 		mp->b_next = chunk_hdr;
474 	}
475 
476 	bytes_to_send -= (chunksize - count);
477 	if (chunk_mp != NULL) {
478 next:
479 		count = chunksize = fp->sfa_pmss - sizeof (*sdc);
480 		goto nextchunk;
481 	}
482 	SCTP_DATA_SET_EBIT(sdc);
483 	sctp->sctp_xmit_unsent = mdblk->b_next;
484 	if (mdblk->b_next == NULL) {
485 		sctp->sctp_xmit_unsent_tail = NULL;
486 	}
487 	mdblk->b_next = NULL;
488 
489 	if (sctp->sctp_xmit_tail == NULL) {
490 		sctp->sctp_xmit_head = sctp->sctp_xmit_tail = mdblk;
491 	} else {
492 		mp = sctp->sctp_xmit_tail;
493 		while (mp->b_next != NULL)
494 			mp = mp->b_next;
495 		mp->b_next = mdblk;
496 		mdblk->b_prev = mp;
497 	}
498 try_next:
499 	if (bytes_to_send > 0 && sctp->sctp_xmit_unsent != NULL) {
500 		mdblk = sctp->sctp_xmit_unsent;
501 		fp1 = SCTP_CHUNK_DEST(mdblk);
502 		if (fp1 == NULL)
503 			fp1 = sctp->sctp_current;
504 		if (fp == fp1) {
505 			size_t len = MBLKL(mdblk->b_cont);
506 			if ((count > 0) &&
507 			    ((len > fp->sfa_pmss - sizeof (*sdc)) ||
508 			    (len <= count))) {
509 				count -= sizeof (*sdc);
510 				count = chunksize = count - (count & 0x3);
511 			} else {
512 				count = chunksize = fp->sfa_pmss -
513 				    sizeof (*sdc);
514 			}
515 		} else {
516 			if (fp1->isv4)
517 				xtralen = sctp->sctp_hdr_len;
518 			else
519 				xtralen = sctp->sctp_hdr6_len;
520 			xtralen += sctps->sctps_wroff_xtra + sizeof (*sdc);
521 			count = chunksize = fp1->sfa_pmss - sizeof (*sdc);
522 			fp = fp1;
523 		}
524 		goto nextmsg;
525 	}
526 }
527 
528 void
529 sctp_free_msg(mblk_t *ump)
530 {
531 	mblk_t *mp, *nmp;
532 
533 	for (mp = ump->b_cont; mp; mp = nmp) {
534 		nmp = mp->b_next;
535 		mp->b_next = mp->b_prev = NULL;
536 		freemsg(mp);
537 	}
538 	ASSERT(!ump->b_prev);
539 	ump->b_next = NULL;
540 	freeb(ump);
541 }
542 
543 mblk_t *
544 sctp_add_proto_hdr(sctp_t *sctp, sctp_faddr_t *fp, mblk_t *mp, int sacklen,
545     int *error)
546 {
547 	int hdrlen;
548 	char *hdr;
549 	int isv4 = fp->isv4;
550 	sctp_stack_t	*sctps = sctp->sctp_sctps;
551 
552 	if (error != NULL)
553 		*error = 0;
554 
555 	if (isv4) {
556 		hdrlen = sctp->sctp_hdr_len;
557 		hdr = sctp->sctp_iphc;
558 	} else {
559 		hdrlen = sctp->sctp_hdr6_len;
560 		hdr = sctp->sctp_iphc6;
561 	}
562 	/*
563 	 * A null fp->ire could mean that the address is 'down'. Similarly,
564 	 * it is possible that the address went down, we tried to send an
565 	 * heartbeat and ended up setting fp->saddr as unspec because we
566 	 * didn't have any usable source address.  In either case
567 	 * sctp_get_ire() will try find an IRE, if available, and set
568 	 * the source address, if needed.  If we still don't have any
569 	 * usable source address, fp->state will be SCTP_FADDRS_UNREACH and
570 	 * we return EHOSTUNREACH.
571 	 */
572 	if (fp->ire == NULL || SCTP_IS_ADDR_UNSPEC(fp->isv4, fp->saddr)) {
573 		sctp_get_ire(sctp, fp);
574 		if (fp->state == SCTP_FADDRS_UNREACH) {
575 			if (error != NULL)
576 				*error = EHOSTUNREACH;
577 			return (NULL);
578 		}
579 	}
580 	/* Copy in IP header. */
581 	if ((mp->b_rptr - mp->b_datap->db_base) <
582 	    (sctps->sctps_wroff_xtra + hdrlen + sacklen) || DB_REF(mp) > 2 ||
583 	    !IS_P2ALIGNED(DB_BASE(mp), sizeof (ire_t *))) {
584 		mblk_t *nmp;
585 
586 		/*
587 		 * This can happen if IP headers are adjusted after
588 		 * data was moved into chunks, or during retransmission,
589 		 * or things like snoop is running.
590 		 */
591 		nmp = allocb_cred(sctps->sctps_wroff_xtra + hdrlen + sacklen,
592 		    CONN_CRED(sctp->sctp_connp));
593 		if (nmp == NULL) {
594 			if (error !=  NULL)
595 				*error = ENOMEM;
596 			return (NULL);
597 		}
598 		nmp->b_rptr += sctps->sctps_wroff_xtra;
599 		nmp->b_wptr = nmp->b_rptr + hdrlen + sacklen;
600 		nmp->b_cont = mp;
601 		mp = nmp;
602 	} else {
603 		mp->b_rptr -= (hdrlen + sacklen);
604 		mblk_setcred(mp, CONN_CRED(sctp->sctp_connp));
605 	}
606 	bcopy(hdr, mp->b_rptr, hdrlen);
607 	if (sacklen) {
608 		sctp_fill_sack(sctp, mp->b_rptr + hdrlen, sacklen);
609 	}
610 	if (fp != sctp->sctp_current) {
611 		/* change addresses in header */
612 		if (isv4) {
613 			ipha_t *iph = (ipha_t *)mp->b_rptr;
614 
615 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst);
616 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr)) {
617 				IN6_V4MAPPED_TO_IPADDR(&fp->saddr,
618 				    iph->ipha_src);
619 			} else if (sctp->sctp_bound_to_all) {
620 				iph->ipha_src = INADDR_ANY;
621 			}
622 		} else {
623 			((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr;
624 			if (!IN6_IS_ADDR_UNSPECIFIED(&fp->saddr)) {
625 				((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr;
626 			} else if (sctp->sctp_bound_to_all) {
627 				V6_SET_ZERO(((ip6_t *)(mp->b_rptr))->ip6_src);
628 			}
629 		}
630 	}
631 	/*
632 	 * IP will not free this IRE if it is condemned.  SCTP needs to
633 	 * free it.
634 	 */
635 	if ((fp->ire != NULL) && (fp->ire->ire_marks & IRE_MARK_CONDEMNED)) {
636 		IRE_REFRELE_NOTR(fp->ire);
637 		fp->ire = NULL;
638 	}
639 
640 	/* Stash the conn and ire ptr info for IP */
641 	SCTP_STASH_IPINFO(mp, fp->ire);
642 
643 	return (mp);
644 }
645 
646 /*
647  * SCTP requires every chunk to be padded so that the total length
648  * is a multiple of SCTP_ALIGN.  This function returns a mblk with
649  * the specified pad length.
650  */
651 static mblk_t *
652 sctp_get_padding(sctp_t *sctp, int pad)
653 {
654 	mblk_t *fill;
655 
656 	ASSERT(pad < SCTP_ALIGN);
657 	ASSERT(sctp->sctp_pad_mp != NULL);
658 	if ((fill = dupb(sctp->sctp_pad_mp)) != NULL) {
659 		fill->b_wptr += pad;
660 		return (fill);
661 	}
662 
663 	/*
664 	 * The memory saving path of reusing the sctp_pad_mp
665 	 * fails may be because it has been dupb() too
666 	 * many times (DBLK_REFMAX).  Use the memory consuming
667 	 * path of allocating the pad mblk.
668 	 */
669 	if ((fill = allocb(SCTP_ALIGN, BPRI_MED)) != NULL) {
670 		/* Zero it out.  SCTP_ALIGN is sizeof (int32_t) */
671 		*(int32_t *)fill->b_rptr = 0;
672 		fill->b_wptr += pad;
673 	}
674 	return (fill);
675 }
676 
677 static mblk_t *
678 sctp_find_fast_rexmit_mblks(sctp_t *sctp, int *total, sctp_faddr_t **fp)
679 {
680 	mblk_t		*meta;
681 	mblk_t		*start_mp = NULL;
682 	mblk_t		*end_mp = NULL;
683 	mblk_t		*mp, *nmp;
684 	mblk_t		*fill;
685 	sctp_data_hdr_t	*sdh;
686 	int		msglen;
687 	int		extra;
688 	sctp_msg_hdr_t	*msg_hdr;
689 	sctp_faddr_t	*old_fp = NULL;
690 	sctp_faddr_t	*chunk_fp;
691 	sctp_stack_t	*sctps = sctp->sctp_sctps;
692 
693 	for (meta = sctp->sctp_xmit_head; meta != NULL; meta = meta->b_next) {
694 		msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
695 		if (SCTP_IS_MSG_ABANDONED(meta) ||
696 		    SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
697 			continue;
698 		}
699 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
700 			if (SCTP_CHUNK_WANT_REXMIT(mp)) {
701 				/*
702 				 * Use the same peer address to do fast
703 				 * retransmission.  If the original peer
704 				 * address is dead, switch to the current
705 				 * one.  Record the old one so that we
706 				 * will pick the chunks sent to the old
707 				 * one for fast retransmission.
708 				 */
709 				chunk_fp = SCTP_CHUNK_DEST(mp);
710 				if (*fp == NULL) {
711 					*fp = chunk_fp;
712 					if ((*fp)->state != SCTP_FADDRS_ALIVE) {
713 						old_fp = *fp;
714 						*fp = sctp->sctp_current;
715 					}
716 				} else if (old_fp == NULL && *fp != chunk_fp) {
717 					continue;
718 				} else if (old_fp != NULL &&
719 				    old_fp != chunk_fp) {
720 					continue;
721 				}
722 
723 				sdh = (sctp_data_hdr_t *)mp->b_rptr;
724 				msglen = ntohs(sdh->sdh_len);
725 				if ((extra = msglen & (SCTP_ALIGN - 1)) != 0) {
726 					extra = SCTP_ALIGN - extra;
727 				}
728 
729 				/*
730 				 * We still return at least the first message
731 				 * even if that message cannot fit in as
732 				 * PMTU may have changed.
733 				 */
734 				if (*total + msglen + extra >
735 				    (*fp)->sfa_pmss && start_mp != NULL) {
736 					return (start_mp);
737 				}
738 				if ((nmp = dupmsg(mp)) == NULL)
739 					return (start_mp);
740 				if (extra > 0) {
741 					fill = sctp_get_padding(sctp, extra);
742 					if (fill != NULL) {
743 						linkb(nmp, fill);
744 					} else {
745 						return (start_mp);
746 					}
747 				}
748 				BUMP_MIB(&sctps->sctps_mib, sctpOutFastRetrans);
749 				BUMP_LOCAL(sctp->sctp_rxtchunks);
750 				SCTP_CHUNK_CLEAR_REXMIT(mp);
751 				if (start_mp == NULL) {
752 					start_mp = nmp;
753 				} else {
754 					linkb(end_mp, nmp);
755 				}
756 				end_mp = nmp;
757 				*total += msglen + extra;
758 				dprint(2, ("sctp_find_fast_rexmit_mblks: "
759 				    "tsn %x\n", sdh->sdh_tsn));
760 			}
761 		}
762 	}
763 	/* Clear the flag as there is no more message to be fast rexmitted. */
764 	sctp->sctp_chk_fast_rexmit = B_FALSE;
765 	return (start_mp);
766 }
767 
768 /* A debug function just to make sure that a mblk chain is not broken */
769 #ifdef	DEBUG
770 static boolean_t
771 sctp_verify_chain(mblk_t *head, mblk_t *tail)
772 {
773 	mblk_t	*mp = head;
774 
775 	if (head == NULL || tail == NULL)
776 		return (B_TRUE);
777 	while (mp != NULL) {
778 		if (mp == tail)
779 			return (B_TRUE);
780 		mp = mp->b_next;
781 	}
782 	return (B_FALSE);
783 }
784 #endif
785 
786 /*
787  * Gets the next unsent chunk to transmit. Messages that are abandoned are
788  * skipped. A message can be abandoned if it has a non-zero timetolive and
789  * transmission has not yet started or if it is a partially reliable
790  * message and its time is up (assuming we are PR-SCTP aware).
791  * 'cansend' is used to determine if need to try and chunkify messages from
792  * the unsent list, if any, and also as an input to sctp_chunkify() if so.
793  * When called from sctp_rexmit(), we don't want to chunkify, so 'cansend'
794  * will be set to 0.
795  */
796 mblk_t *
797 sctp_get_msg_to_send(sctp_t *sctp, mblk_t **mp, mblk_t *meta, int  *error,
798     int32_t firstseg, uint32_t cansend, sctp_faddr_t *fp)
799 {
800 	mblk_t		*mp1;
801 	sctp_msg_hdr_t	*msg_hdr;
802 	mblk_t		*tmp_meta;
803 	sctp_faddr_t	*fp1;
804 
805 	ASSERT(error != NULL && mp != NULL);
806 	*error = 0;
807 
808 	ASSERT(sctp->sctp_current != NULL);
809 
810 chunkified:
811 	while (meta != NULL) {
812 		tmp_meta = meta->b_next;
813 		msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
814 		mp1 = meta->b_cont;
815 		if (SCTP_IS_MSG_ABANDONED(meta))
816 			goto next_msg;
817 		if (!SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
818 			while (mp1 != NULL) {
819 				if (SCTP_CHUNK_CANSEND(mp1)) {
820 					*mp = mp1;
821 #ifdef	DEBUG
822 					ASSERT(sctp_verify_chain(
823 					    sctp->sctp_xmit_head, meta));
824 #endif
825 					return (meta);
826 				}
827 				mp1 = mp1->b_next;
828 			}
829 			goto next_msg;
830 		}
831 		/*
832 		 * If we come here and the first chunk is sent, then we
833 		 * we are PR-SCTP aware, in which case if the cumulative
834 		 * TSN has moved upto or beyond the first chunk (which
835 		 * means all the previous messages have been cumulative
836 		 * SACK'd), then we send a Forward TSN with the last
837 		 * chunk that was sent in this message. If we can't send
838 		 * a Forward TSN because previous non-abandoned messages
839 		 * have not been acked then we will defer the Forward TSN
840 		 * to sctp_rexmit() or sctp_cumack().
841 		 */
842 		if (SCTP_CHUNK_ISSENT(mp1)) {
843 			*error = sctp_check_abandoned_msg(sctp, meta);
844 			if (*error != 0) {
845 #ifdef	DEBUG
846 				ASSERT(sctp_verify_chain(sctp->sctp_xmit_head,
847 				    sctp->sctp_xmit_tail));
848 #endif
849 				return (NULL);
850 			}
851 			goto next_msg;
852 		}
853 		BUMP_LOCAL(sctp->sctp_prsctpdrop);
854 		ASSERT(sctp->sctp_unsent >= msg_hdr->smh_msglen);
855 		if (meta->b_prev == NULL) {
856 			ASSERT(sctp->sctp_xmit_head == meta);
857 			sctp->sctp_xmit_head = tmp_meta;
858 			if (sctp->sctp_xmit_tail == meta)
859 				sctp->sctp_xmit_tail = tmp_meta;
860 			meta->b_next = NULL;
861 			if (tmp_meta != NULL)
862 				tmp_meta->b_prev = NULL;
863 		} else if (meta->b_next == NULL) {
864 			if (sctp->sctp_xmit_tail == meta)
865 				sctp->sctp_xmit_tail = meta->b_prev;
866 			meta->b_prev->b_next = NULL;
867 			meta->b_prev = NULL;
868 		} else {
869 			meta->b_prev->b_next = tmp_meta;
870 			tmp_meta->b_prev = meta->b_prev;
871 			if (sctp->sctp_xmit_tail == meta)
872 				sctp->sctp_xmit_tail = tmp_meta;
873 			meta->b_prev = NULL;
874 			meta->b_next = NULL;
875 		}
876 		sctp->sctp_unsent -= msg_hdr->smh_msglen;
877 		/*
878 		 * Update ULP the amount of queued data, which is
879 		 * sent-unack'ed + unsent.
880 		 */
881 		if (!SCTP_IS_DETACHED(sctp)) {
882 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
883 			    sctp->sctp_unacked + sctp->sctp_unsent);
884 		}
885 		sctp_sendfail_event(sctp, meta, 0, B_TRUE);
886 next_msg:
887 		meta = tmp_meta;
888 	}
889 	/* chunkify, if needed */
890 	if (cansend > 0 && sctp->sctp_xmit_unsent != NULL) {
891 		ASSERT(sctp->sctp_unsent > 0);
892 		if (fp == NULL) {
893 			fp = SCTP_CHUNK_DEST(sctp->sctp_xmit_unsent);
894 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
895 				fp = sctp->sctp_current;
896 		} else {
897 			/*
898 			 * If user specified destination, try to honor that.
899 			 */
900 			fp1 = SCTP_CHUNK_DEST(sctp->sctp_xmit_unsent);
901 			if (fp1 != NULL && fp1->state == SCTP_FADDRS_ALIVE &&
902 			    fp1 != fp) {
903 				goto chunk_done;
904 			}
905 		}
906 		sctp_chunkify(sctp, fp->sfa_pmss - firstseg, cansend);
907 		if ((meta = sctp->sctp_xmit_tail) == NULL)
908 			goto chunk_done;
909 		/*
910 		 * sctp_chunkify() won't advance sctp_xmit_tail if it adds
911 		 * new chunk(s) to the tail, so we need to skip the
912 		 * sctp_xmit_tail, which would have already been processed.
913 		 * This could happen when there is unacked chunks, but
914 		 * nothing new to send.
915 		 * When sctp_chunkify() is called when the transmit queue
916 		 * is empty then we need to start from sctp_xmit_tail.
917 		 */
918 		if (SCTP_CHUNK_ISSENT(sctp->sctp_xmit_tail->b_cont)) {
919 #ifdef	DEBUG
920 			mp1 = sctp->sctp_xmit_tail->b_cont;
921 			while (mp1 != NULL) {
922 				ASSERT(!SCTP_CHUNK_CANSEND(mp1));
923 				mp1 = mp1->b_next;
924 			}
925 #endif
926 			if ((meta = sctp->sctp_xmit_tail->b_next) == NULL)
927 				goto chunk_done;
928 		}
929 		goto chunkified;
930 	}
931 chunk_done:
932 #ifdef	DEBUG
933 	ASSERT(sctp_verify_chain(sctp->sctp_xmit_head, sctp->sctp_xmit_tail));
934 #endif
935 	return (NULL);
936 }
937 
938 void
939 sctp_fast_rexmit(sctp_t *sctp)
940 {
941 	mblk_t		*mp, *head;
942 	int		pktlen = 0;
943 	sctp_faddr_t	*fp = NULL;
944 	sctp_stack_t	*sctps = sctp->sctp_sctps;
945 
946 	ASSERT(sctp->sctp_xmit_head != NULL);
947 	mp = sctp_find_fast_rexmit_mblks(sctp, &pktlen, &fp);
948 	if (mp == NULL) {
949 		SCTP_KSTAT(sctps, sctp_fr_not_found);
950 		return;
951 	}
952 	if ((head = sctp_add_proto_hdr(sctp, fp, mp, 0, NULL)) == NULL) {
953 		freemsg(mp);
954 		SCTP_KSTAT(sctps, sctp_fr_add_hdr);
955 		return;
956 	}
957 	if ((pktlen > fp->sfa_pmss) && fp->isv4) {
958 		ipha_t *iph = (ipha_t *)head->b_rptr;
959 
960 		iph->ipha_fragment_offset_and_flags = 0;
961 	}
962 
963 	sctp_set_iplen(sctp, head);
964 	sctp_add_sendq(sctp, head);
965 	sctp->sctp_active = fp->lastactive = lbolt64;
966 }
967 
968 void
969 sctp_output(sctp_t *sctp, uint_t num_pkt)
970 {
971 	mblk_t			*mp = NULL;
972 	mblk_t			*nmp;
973 	mblk_t			*head;
974 	mblk_t			*meta = sctp->sctp_xmit_tail;
975 	mblk_t			*fill = NULL;
976 	uint16_t 		chunklen;
977 	uint32_t 		cansend;
978 	int32_t			seglen;
979 	int32_t			xtralen;
980 	int32_t			sacklen;
981 	int32_t			pad = 0;
982 	int32_t			pathmax;
983 	int			extra;
984 	int64_t			now = lbolt64;
985 	sctp_faddr_t		*fp;
986 	sctp_faddr_t		*lfp;
987 	sctp_data_hdr_t		*sdc;
988 	int			error;
989 	boolean_t		notsent = B_TRUE;
990 	sctp_stack_t		*sctps = sctp->sctp_sctps;
991 
992 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
993 		sacklen = 0;
994 	} else {
995 		/* send a SACK chunk */
996 		sacklen = sizeof (sctp_chunk_hdr_t) +
997 		    sizeof (sctp_sack_chunk_t) +
998 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
999 		lfp = sctp->sctp_lastdata;
1000 		ASSERT(lfp != NULL);
1001 		if (lfp->state != SCTP_FADDRS_ALIVE)
1002 			lfp = sctp->sctp_current;
1003 	}
1004 
1005 	cansend = sctp->sctp_frwnd;
1006 	if (sctp->sctp_unsent < cansend)
1007 		cansend = sctp->sctp_unsent;
1008 	if ((cansend < sctp->sctp_current->sfa_pmss / 2) &&
1009 	    sctp->sctp_unacked &&
1010 	    (sctp->sctp_unacked < sctp->sctp_current->sfa_pmss) &&
1011 	    !sctp->sctp_ndelay) {
1012 		head = NULL;
1013 		fp = sctp->sctp_current;
1014 		goto unsent_data;
1015 	}
1016 	if (meta != NULL)
1017 		mp = meta->b_cont;
1018 	while (cansend > 0 && num_pkt-- != 0) {
1019 		pad = 0;
1020 
1021 		/*
1022 		 * Find first segment eligible for transmit.
1023 		 */
1024 		while (mp != NULL) {
1025 			if (SCTP_CHUNK_CANSEND(mp))
1026 				break;
1027 			mp = mp->b_next;
1028 		}
1029 		if (mp == NULL) {
1030 			meta = sctp_get_msg_to_send(sctp, &mp,
1031 			    meta == NULL ? NULL : meta->b_next, &error, sacklen,
1032 			    cansend, NULL);
1033 			if (error != 0 || meta == NULL) {
1034 				head = NULL;
1035 				fp = sctp->sctp_current;
1036 				goto unsent_data;
1037 			}
1038 			sctp->sctp_xmit_tail =  meta;
1039 		}
1040 
1041 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
1042 		seglen = ntohs(sdc->sdh_len);
1043 		xtralen = sizeof (*sdc);
1044 		chunklen = seglen - xtralen;
1045 
1046 		/*
1047 		 * Check rwnd.
1048 		 */
1049 		if (chunklen > cansend) {
1050 			head = NULL;
1051 			fp = SCTP_CHUNK_DEST(meta);
1052 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
1053 				fp = sctp->sctp_current;
1054 			goto unsent_data;
1055 		}
1056 		if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
1057 			extra = SCTP_ALIGN - extra;
1058 
1059 		/*
1060 		 * Pick destination address, and check cwnd.
1061 		 */
1062 		if (sacklen > 0 && (seglen + extra <= lfp->cwnd - lfp->suna) &&
1063 		    (seglen + sacklen + extra <= lfp->sfa_pmss)) {
1064 			/*
1065 			 * Only include SACK chunk if it can be bundled
1066 			 * with a data chunk, and sent to sctp_lastdata.
1067 			 */
1068 			pathmax = lfp->cwnd - lfp->suna;
1069 
1070 			fp = lfp;
1071 			if ((nmp = dupmsg(mp)) == NULL) {
1072 				head = NULL;
1073 				goto unsent_data;
1074 			}
1075 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1076 			head = sctp_add_proto_hdr(sctp, fp, nmp, sacklen,
1077 			    &error);
1078 			if (head == NULL) {
1079 				/*
1080 				 * If none of the source addresses are
1081 				 * available (i.e error == EHOSTUNREACH),
1082 				 * pretend we have sent the data. We will
1083 				 * eventually time out trying to retramsmit
1084 				 * the data if the interface never comes up.
1085 				 * If we have already sent some stuff (i.e.,
1086 				 * notsent is B_FALSE) then we are fine, else
1087 				 * just mark this packet as sent.
1088 				 */
1089 				if (notsent && error == EHOSTUNREACH) {
1090 					SCTP_CHUNK_SENT(sctp, mp, sdc,
1091 					    fp, chunklen, meta);
1092 				}
1093 				freemsg(nmp);
1094 				SCTP_KSTAT(sctps, sctp_output_failed);
1095 				goto unsent_data;
1096 			}
1097 			seglen += sacklen;
1098 			xtralen += sacklen;
1099 			sacklen = 0;
1100 		} else {
1101 			fp = SCTP_CHUNK_DEST(meta);
1102 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
1103 				fp = sctp->sctp_current;
1104 			/*
1105 			 * If we haven't sent data to this destination for
1106 			 * a while, do slow start again.
1107 			 */
1108 			if (now - fp->lastactive > fp->rto) {
1109 				SET_CWND(fp, fp->sfa_pmss,
1110 				    sctps->sctps_slow_start_after_idle);
1111 			}
1112 
1113 			pathmax = fp->cwnd - fp->suna;
1114 			if (seglen + extra > pathmax) {
1115 				head = NULL;
1116 				goto unsent_data;
1117 			}
1118 			if ((nmp = dupmsg(mp)) == NULL) {
1119 				head = NULL;
1120 				goto unsent_data;
1121 			}
1122 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1123 			head = sctp_add_proto_hdr(sctp, fp, nmp, 0, &error);
1124 			if (head == NULL) {
1125 				/*
1126 				 * If none of the source addresses are
1127 				 * available (i.e error == EHOSTUNREACH),
1128 				 * pretend we have sent the data. We will
1129 				 * eventually time out trying to retramsmit
1130 				 * the data if the interface never comes up.
1131 				 * If we have already sent some stuff (i.e.,
1132 				 * notsent is B_FALSE) then we are fine, else
1133 				 * just mark this packet as sent.
1134 				 */
1135 				if (notsent && error == EHOSTUNREACH) {
1136 					SCTP_CHUNK_SENT(sctp, mp, sdc,
1137 					    fp, chunklen, meta);
1138 				}
1139 				freemsg(nmp);
1140 				SCTP_KSTAT(sctps, sctp_output_failed);
1141 				goto unsent_data;
1142 			}
1143 		}
1144 		fp->lastactive = now;
1145 		if (pathmax > fp->sfa_pmss)
1146 			pathmax = fp->sfa_pmss;
1147 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
1148 		mp = mp->b_next;
1149 
1150 		/* Use this chunk to measure RTT? */
1151 		if (sctp->sctp_out_time == 0) {
1152 			sctp->sctp_out_time = now;
1153 			sctp->sctp_rtt_tsn = sctp->sctp_ltsn - 1;
1154 			ASSERT(sctp->sctp_rtt_tsn == ntohl(sdc->sdh_tsn));
1155 		}
1156 		if (extra > 0) {
1157 			fill = sctp_get_padding(sctp, extra);
1158 			if (fill != NULL) {
1159 				linkb(head, fill);
1160 				pad = extra;
1161 				seglen += extra;
1162 			} else {
1163 				goto unsent_data;
1164 			}
1165 		}
1166 		/* See if we can bundle more. */
1167 		while (seglen < pathmax) {
1168 			int32_t		new_len;
1169 			int32_t		new_xtralen;
1170 
1171 			while (mp != NULL) {
1172 				if (SCTP_CHUNK_CANSEND(mp))
1173 					break;
1174 				mp = mp->b_next;
1175 			}
1176 			if (mp == NULL) {
1177 				meta = sctp_get_msg_to_send(sctp, &mp,
1178 				    meta->b_next, &error, seglen,
1179 				    (seglen - xtralen) >= cansend ? 0 :
1180 				    cansend - seglen, fp);
1181 				if (error != 0 || meta == NULL)
1182 					break;
1183 				sctp->sctp_xmit_tail =  meta;
1184 			}
1185 			ASSERT(mp != NULL);
1186 			if (!SCTP_CHUNK_ISSENT(mp) && SCTP_CHUNK_DEST(meta) &&
1187 			    fp != SCTP_CHUNK_DEST(meta)) {
1188 				break;
1189 			}
1190 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
1191 			chunklen = ntohs(sdc->sdh_len);
1192 			if ((extra = chunklen  & (SCTP_ALIGN - 1)) != 0)
1193 				extra = SCTP_ALIGN - extra;
1194 
1195 			new_len = seglen + chunklen;
1196 			new_xtralen = xtralen + sizeof (*sdc);
1197 			chunklen -= sizeof (*sdc);
1198 
1199 			if (new_len - new_xtralen > cansend ||
1200 			    new_len + extra > pathmax) {
1201 				break;
1202 			}
1203 			if ((nmp = dupmsg(mp)) == NULL)
1204 				break;
1205 			if (extra > 0) {
1206 				fill = sctp_get_padding(sctp, extra);
1207 				if (fill != NULL) {
1208 					pad += extra;
1209 					new_len += extra;
1210 					linkb(nmp, fill);
1211 				} else {
1212 					freemsg(nmp);
1213 					break;
1214 				}
1215 			}
1216 			seglen = new_len;
1217 			xtralen = new_xtralen;
1218 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1219 			SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
1220 			linkb(head, nmp);
1221 			mp = mp->b_next;
1222 		}
1223 		if ((seglen > fp->sfa_pmss) && fp->isv4) {
1224 			ipha_t *iph = (ipha_t *)head->b_rptr;
1225 
1226 			/*
1227 			 * Path MTU is different from what we thought it would
1228 			 * be when we created chunks, or IP headers have grown.
1229 			 * Need to clear the DF bit.
1230 			 */
1231 			iph->ipha_fragment_offset_and_flags = 0;
1232 		}
1233 		/* xmit segment */
1234 		ASSERT(cansend >= seglen - pad - xtralen);
1235 		cansend -= (seglen - pad - xtralen);
1236 		dprint(2, ("sctp_output: Sending packet %d bytes, tsn %x "
1237 		    "ssn %d to %p (rwnd %d, cansend %d, lastack_rxd %x)\n",
1238 		    seglen - xtralen, ntohl(sdc->sdh_tsn),
1239 		    ntohs(sdc->sdh_ssn), (void *)fp, sctp->sctp_frwnd,
1240 		    cansend, sctp->sctp_lastack_rxd));
1241 		sctp_set_iplen(sctp, head);
1242 		sctp_add_sendq(sctp, head);
1243 		/* arm rto timer (if not set) */
1244 		if (!fp->timer_running)
1245 			SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1246 		notsent = B_FALSE;
1247 	}
1248 	sctp->sctp_active = now;
1249 	return;
1250 unsent_data:
1251 	/* arm persist timer (if rto timer not set) */
1252 	if (!fp->timer_running)
1253 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1254 	if (head != NULL)
1255 		freemsg(head);
1256 }
1257 
1258 /*
1259  * The following two functions initialize and destroy the cache
1260  * associated with the sets used for PR-SCTP.
1261  */
1262 void
1263 sctp_ftsn_sets_init(void)
1264 {
1265 	sctp_kmem_ftsn_set_cache = kmem_cache_create("sctp_ftsn_set_cache",
1266 	    sizeof (sctp_ftsn_set_t), 0, NULL, NULL, NULL, NULL,
1267 	    NULL, 0);
1268 }
1269 
1270 void
1271 sctp_ftsn_sets_fini(void)
1272 {
1273 	kmem_cache_destroy(sctp_kmem_ftsn_set_cache);
1274 }
1275 
1276 
1277 /* Free PR-SCTP sets */
1278 void
1279 sctp_free_ftsn_set(sctp_ftsn_set_t *s)
1280 {
1281 	sctp_ftsn_set_t *p;
1282 
1283 	while (s != NULL) {
1284 		p = s->next;
1285 		s->next = NULL;
1286 		kmem_cache_free(sctp_kmem_ftsn_set_cache, s);
1287 		s = p;
1288 	}
1289 }
1290 
1291 /*
1292  * Given a message meta block, meta, this routine creates or modifies
1293  * the set that will be used to generate a Forward TSN chunk. If the
1294  * entry for stream id, sid, for this message already exists, the
1295  * sequence number, ssn, is updated if it is greater than the existing
1296  * one. If an entry for this sid does not exist, one is created if
1297  * the size does not exceed fp->sfa_pmss. We return false in case
1298  * or an error.
1299  */
1300 boolean_t
1301 sctp_add_ftsn_set(sctp_ftsn_set_t **s, sctp_faddr_t *fp, mblk_t *meta,
1302     uint_t *nsets, uint32_t *slen)
1303 {
1304 	sctp_ftsn_set_t		*p;
1305 	sctp_msg_hdr_t		*msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
1306 	uint16_t		sid = htons(msg_hdr->smh_sid);
1307 	/* msg_hdr->smh_ssn is already in NBO */
1308 	uint16_t		ssn = msg_hdr->smh_ssn;
1309 
1310 	ASSERT(s != NULL && nsets != NULL);
1311 	ASSERT((*nsets == 0 && *s == NULL) || (*nsets > 0 && *s != NULL));
1312 
1313 	if (*s == NULL) {
1314 		ASSERT((*slen + sizeof (uint32_t)) <= fp->sfa_pmss);
1315 		*s = kmem_cache_alloc(sctp_kmem_ftsn_set_cache, KM_NOSLEEP);
1316 		if (*s == NULL)
1317 			return (B_FALSE);
1318 		(*s)->ftsn_entries.ftsn_sid = sid;
1319 		(*s)->ftsn_entries.ftsn_ssn = ssn;
1320 		(*s)->next = NULL;
1321 		*nsets = 1;
1322 		*slen += sizeof (uint32_t);
1323 		return (B_TRUE);
1324 	}
1325 	for (p = *s; p->next != NULL; p = p->next) {
1326 		if (p->ftsn_entries.ftsn_sid == sid) {
1327 			if (SSN_GT(ssn, p->ftsn_entries.ftsn_ssn))
1328 				p->ftsn_entries.ftsn_ssn = ssn;
1329 			return (B_TRUE);
1330 		}
1331 	}
1332 	/* the last one */
1333 	if (p->ftsn_entries.ftsn_sid == sid) {
1334 		if (SSN_GT(ssn, p->ftsn_entries.ftsn_ssn))
1335 			p->ftsn_entries.ftsn_ssn = ssn;
1336 	} else {
1337 		if ((*slen + sizeof (uint32_t)) > fp->sfa_pmss)
1338 			return (B_FALSE);
1339 		p->next = kmem_cache_alloc(sctp_kmem_ftsn_set_cache,
1340 		    KM_NOSLEEP);
1341 		if (p->next == NULL)
1342 			return (B_FALSE);
1343 		p = p->next;
1344 		p->ftsn_entries.ftsn_sid = sid;
1345 		p->ftsn_entries.ftsn_ssn = ssn;
1346 		p->next = NULL;
1347 		(*nsets)++;
1348 		*slen += sizeof (uint32_t);
1349 	}
1350 	return (B_TRUE);
1351 }
1352 
1353 /*
1354  * Given a set of stream id - sequence number pairs, this routing creates
1355  * a Forward TSN chunk. The cumulative TSN (advanced peer ack point)
1356  * for the chunk is obtained from sctp->sctp_adv_pap. The caller
1357  * will add the IP/SCTP header.
1358  */
1359 mblk_t *
1360 sctp_make_ftsn_chunk(sctp_t *sctp, sctp_faddr_t *fp, sctp_ftsn_set_t *sets,
1361     uint_t nsets, uint32_t seglen)
1362 {
1363 	mblk_t			*ftsn_mp;
1364 	sctp_chunk_hdr_t	*ch_hdr;
1365 	uint32_t		*advtsn;
1366 	uint16_t		schlen;
1367 	size_t			xtralen;
1368 	ftsn_entry_t		*ftsn_entry;
1369 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1370 
1371 	seglen += sizeof (sctp_chunk_hdr_t);
1372 	if (fp->isv4)
1373 		xtralen = sctp->sctp_hdr_len + sctps->sctps_wroff_xtra;
1374 	else
1375 		xtralen = sctp->sctp_hdr6_len + sctps->sctps_wroff_xtra;
1376 	ftsn_mp = allocb_cred(xtralen + seglen, CONN_CRED(sctp->sctp_connp));
1377 	if (ftsn_mp == NULL)
1378 		return (NULL);
1379 	ftsn_mp->b_rptr += xtralen;
1380 	ftsn_mp->b_wptr = ftsn_mp->b_rptr + seglen;
1381 
1382 	ch_hdr = (sctp_chunk_hdr_t *)ftsn_mp->b_rptr;
1383 	ch_hdr->sch_id = CHUNK_FORWARD_TSN;
1384 	ch_hdr->sch_flags = 0;
1385 	/*
1386 	 * The cast here should not be an issue since seglen is
1387 	 * the length of the Forward TSN chunk.
1388 	 */
1389 	schlen = (uint16_t)seglen;
1390 	U16_TO_ABE16(schlen, &(ch_hdr->sch_len));
1391 
1392 	advtsn = (uint32_t *)(ch_hdr + 1);
1393 	U32_TO_ABE32(sctp->sctp_adv_pap, advtsn);
1394 	ftsn_entry = (ftsn_entry_t *)(advtsn + 1);
1395 	while (nsets > 0) {
1396 		ASSERT((uchar_t *)&ftsn_entry[1] <= ftsn_mp->b_wptr);
1397 		ftsn_entry->ftsn_sid = sets->ftsn_entries.ftsn_sid;
1398 		ftsn_entry->ftsn_ssn = sets->ftsn_entries.ftsn_ssn;
1399 		ftsn_entry++;
1400 		sets = sets->next;
1401 		nsets--;
1402 	}
1403 	return (ftsn_mp);
1404 }
1405 
1406 /*
1407  * Given a starting message, the routine steps through all the
1408  * messages whose TSN is less than sctp->sctp_adv_pap and creates
1409  * ftsn sets. The ftsn sets is then used to create an Forward TSN
1410  * chunk. All the messages, that have chunks that are included in the
1411  * ftsn sets, are flagged abandonded. If a message is partially sent
1412  * and is deemed abandoned, all remaining unsent chunks are marked
1413  * abandoned and are deducted from sctp_unsent.
1414  */
1415 void
1416 sctp_make_ftsns(sctp_t *sctp, mblk_t *meta, mblk_t *mp, mblk_t **nmp,
1417     sctp_faddr_t *fp, uint32_t *seglen)
1418 {
1419 	mblk_t		*mp1 = mp;
1420 	mblk_t		*mp_head = mp;
1421 	mblk_t		*meta_head = meta;
1422 	mblk_t		*head;
1423 	sctp_ftsn_set_t	*sets = NULL;
1424 	uint_t		nsets = 0;
1425 	uint16_t	clen;
1426 	sctp_data_hdr_t	*sdc;
1427 	uint32_t	sacklen;
1428 	uint32_t	adv_pap = sctp->sctp_adv_pap;
1429 	uint32_t	unsent = 0;
1430 	boolean_t	ubit;
1431 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1432 
1433 	*seglen = sizeof (uint32_t);
1434 
1435 	sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
1436 	while (meta != NULL &&
1437 	    SEQ_GEQ(sctp->sctp_adv_pap, ntohl(sdc->sdh_tsn))) {
1438 		/*
1439 		 * Skip adding FTSN sets for un-ordered messages as they do
1440 		 * not have SSNs.
1441 		 */
1442 		ubit = SCTP_DATA_GET_UBIT(sdc);
1443 		if (!ubit &&
1444 		    !sctp_add_ftsn_set(&sets, fp, meta, &nsets, seglen)) {
1445 			meta = NULL;
1446 			sctp->sctp_adv_pap = adv_pap;
1447 			goto ftsn_done;
1448 		}
1449 		while (mp1 != NULL && SCTP_CHUNK_ISSENT(mp1)) {
1450 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
1451 			adv_pap = ntohl(sdc->sdh_tsn);
1452 			mp1 = mp1->b_next;
1453 		}
1454 		meta = meta->b_next;
1455 		if (meta != NULL) {
1456 			mp1 = meta->b_cont;
1457 			if (!SCTP_CHUNK_ISSENT(mp1))
1458 				break;
1459 			sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
1460 		}
1461 	}
1462 ftsn_done:
1463 	/*
1464 	 * Can't compare with sets == NULL, since we don't add any
1465 	 * sets for un-ordered messages.
1466 	 */
1467 	if (meta == meta_head)
1468 		return;
1469 	*nmp = sctp_make_ftsn_chunk(sctp, fp, sets, nsets, *seglen);
1470 	sctp_free_ftsn_set(sets);
1471 	if (*nmp == NULL)
1472 		return;
1473 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
1474 		sacklen = 0;
1475 	} else {
1476 		sacklen = sizeof (sctp_chunk_hdr_t) +
1477 		    sizeof (sctp_sack_chunk_t) +
1478 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
1479 		if (*seglen + sacklen > sctp->sctp_lastdata->sfa_pmss) {
1480 			/* piggybacked SACK doesn't fit */
1481 			sacklen = 0;
1482 		} else {
1483 			fp = sctp->sctp_lastdata;
1484 		}
1485 	}
1486 	head = sctp_add_proto_hdr(sctp, fp, *nmp, sacklen, NULL);
1487 	if (head == NULL) {
1488 		freemsg(*nmp);
1489 		*nmp = NULL;
1490 		SCTP_KSTAT(sctps, sctp_send_ftsn_failed);
1491 		return;
1492 	}
1493 	*seglen += sacklen;
1494 	*nmp = head;
1495 
1496 	/*
1497 	 * XXXNeed to optimise this, the reason it is done here is so
1498 	 * that we don't have to undo in case of failure.
1499 	 */
1500 	mp1 = mp_head;
1501 	sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
1502 	while (meta_head != NULL &&
1503 	    SEQ_GEQ(sctp->sctp_adv_pap, ntohl(sdc->sdh_tsn))) {
1504 		if (!SCTP_IS_MSG_ABANDONED(meta_head))
1505 			SCTP_MSG_SET_ABANDONED(meta_head);
1506 		while (mp1 != NULL && SCTP_CHUNK_ISSENT(mp1)) {
1507 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
1508 			if (!SCTP_CHUNK_ISACKED(mp1)) {
1509 				clen = ntohs(sdc->sdh_len) - sizeof (*sdc);
1510 				SCTP_CHUNK_SENT(sctp, mp1, sdc, fp, clen,
1511 				    meta_head);
1512 			}
1513 			mp1 = mp1->b_next;
1514 		}
1515 		while (mp1 != NULL) {
1516 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
1517 			if (!SCTP_CHUNK_ABANDONED(mp1)) {
1518 				ASSERT(!SCTP_CHUNK_ISSENT(mp1));
1519 				unsent += ntohs(sdc->sdh_len) - sizeof (*sdc);
1520 				SCTP_ABANDON_CHUNK(mp1);
1521 			}
1522 			mp1 = mp1->b_next;
1523 		}
1524 		meta_head = meta_head->b_next;
1525 		if (meta_head != NULL) {
1526 			mp1 = meta_head->b_cont;
1527 			if (!SCTP_CHUNK_ISSENT(mp1))
1528 				break;
1529 			sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
1530 		}
1531 	}
1532 	if (unsent > 0) {
1533 		ASSERT(sctp->sctp_unsent >= unsent);
1534 		sctp->sctp_unsent -= unsent;
1535 		/*
1536 		 * Update ULP the amount of queued data, which is
1537 		 * sent-unack'ed + unsent.
1538 		 */
1539 		if (!SCTP_IS_DETACHED(sctp)) {
1540 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
1541 			    sctp->sctp_unacked + sctp->sctp_unsent);
1542 		}
1543 	}
1544 }
1545 
1546 /*
1547  * This function steps through messages starting at meta and checks if
1548  * the message is abandoned. It stops when it hits an unsent chunk or
1549  * a message that has all its chunk acked. This is the only place
1550  * where the sctp_adv_pap is moved forward to indicated abandoned
1551  * messages.
1552  */
1553 void
1554 sctp_check_adv_ack_pt(sctp_t *sctp, mblk_t *meta, mblk_t *mp)
1555 {
1556 	uint32_t	tsn = sctp->sctp_adv_pap;
1557 	sctp_data_hdr_t	*sdc;
1558 	sctp_msg_hdr_t	*msg_hdr;
1559 
1560 	ASSERT(mp != NULL);
1561 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
1562 	ASSERT(SEQ_GT(ntohl(sdc->sdh_tsn), sctp->sctp_lastack_rxd));
1563 	msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
1564 	if (!SCTP_IS_MSG_ABANDONED(meta) &&
1565 	    !SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
1566 		return;
1567 	}
1568 	while (meta != NULL) {
1569 		while (mp != NULL && SCTP_CHUNK_ISSENT(mp)) {
1570 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
1571 			tsn = ntohl(sdc->sdh_tsn);
1572 			mp = mp->b_next;
1573 		}
1574 		if (mp != NULL)
1575 			break;
1576 		/*
1577 		 * We continue checking for successive messages only if there
1578 		 * is a chunk marked for retransmission. Else, we might
1579 		 * end up sending FTSN prematurely for chunks that have been
1580 		 * sent, but not yet acked.
1581 		 */
1582 		if ((meta = meta->b_next) != NULL) {
1583 			msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
1584 			if (!SCTP_IS_MSG_ABANDONED(meta) &&
1585 			    !SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
1586 				break;
1587 			}
1588 			for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
1589 				if (!SCTP_CHUNK_ISSENT(mp)) {
1590 					sctp->sctp_adv_pap = tsn;
1591 					return;
1592 				}
1593 				if (SCTP_CHUNK_WANT_REXMIT(mp))
1594 					break;
1595 			}
1596 			if (mp == NULL)
1597 				break;
1598 		}
1599 	}
1600 	sctp->sctp_adv_pap = tsn;
1601 }
1602 
1603 
1604 /*
1605  * Determine if we should bundle a data chunk with the chunk being
1606  * retransmitted.  We bundle if
1607  *
1608  * - the chunk is sent to the same destination and unack'ed.
1609  *
1610  * OR
1611  *
1612  * - the chunk is unsent, i.e. new data.
1613  */
1614 #define	SCTP_CHUNK_RX_CANBUNDLE(mp, fp)					\
1615 	(!SCTP_CHUNK_ABANDONED((mp)) && 				\
1616 	((SCTP_CHUNK_ISSENT((mp)) && (SCTP_CHUNK_DEST(mp) == (fp) &&	\
1617 	!SCTP_CHUNK_ISACKED(mp))) ||					\
1618 	(((mp)->b_flag & (SCTP_CHUNK_FLAG_REXMIT|SCTP_CHUNK_FLAG_SENT)) != \
1619 	SCTP_CHUNK_FLAG_SENT)))
1620 
1621 /*
1622  * Retransmit first segment which hasn't been acked with cumtsn or send
1623  * a Forward TSN chunk, if appropriate.
1624  */
1625 void
1626 sctp_rexmit(sctp_t *sctp, sctp_faddr_t *oldfp)
1627 {
1628 	mblk_t		*mp;
1629 	mblk_t		*nmp = NULL;
1630 	mblk_t		*head;
1631 	mblk_t		*meta = sctp->sctp_xmit_head;
1632 	mblk_t		*fill;
1633 	uint32_t	seglen = 0;
1634 	uint32_t	sacklen;
1635 	uint16_t	chunklen;
1636 	int		extra;
1637 	sctp_data_hdr_t	*sdc;
1638 	sctp_faddr_t	*fp;
1639 	uint32_t	adv_pap = sctp->sctp_adv_pap;
1640 	boolean_t	do_ftsn = B_FALSE;
1641 	boolean_t	ftsn_check = B_TRUE;
1642 	uint32_t	first_ua_tsn;
1643 	sctp_msg_hdr_t	*mhdr;
1644 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1645 
1646 	while (meta != NULL) {
1647 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
1648 			uint32_t	tsn;
1649 
1650 			if (!SCTP_CHUNK_ISSENT(mp))
1651 				goto window_probe;
1652 			/*
1653 			 * We break in the following cases -
1654 			 *
1655 			 *	if the advanced peer ack point includes the next
1656 			 *	chunk to be retransmited - possibly the Forward
1657 			 * 	TSN was lost.
1658 			 *
1659 			 *	if we are PRSCTP aware and the next chunk to be
1660 			 *	retransmitted is now abandoned
1661 			 *
1662 			 *	if the next chunk to be retransmitted is for
1663 			 *	the dest on which the timer went off. (this
1664 			 *	message is not abandoned).
1665 			 *
1666 			 * We check for Forward TSN only for the first
1667 			 * eligible chunk to be retransmitted. The reason
1668 			 * being if the first eligible chunk is skipped (say
1669 			 * it was sent to a destination other than oldfp)
1670 			 * then we cannot advance the cum TSN via Forward
1671 			 * TSN chunk.
1672 			 *
1673 			 * Also, ftsn_check is B_TRUE only for the first
1674 			 * eligible chunk, it  will be B_FALSE for all
1675 			 * subsequent candidate messages for retransmission.
1676 			 */
1677 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
1678 			tsn = ntohl(sdc->sdh_tsn);
1679 			if (SEQ_GT(tsn, sctp->sctp_lastack_rxd)) {
1680 				if (sctp->sctp_prsctp_aware && ftsn_check) {
1681 					if (SEQ_GEQ(sctp->sctp_adv_pap, tsn)) {
1682 						ASSERT(sctp->sctp_prsctp_aware);
1683 						do_ftsn = B_TRUE;
1684 						goto out;
1685 					} else {
1686 						sctp_check_adv_ack_pt(sctp,
1687 						    meta, mp);
1688 						if (SEQ_GT(sctp->sctp_adv_pap,
1689 						    adv_pap)) {
1690 							do_ftsn = B_TRUE;
1691 							goto out;
1692 						}
1693 					}
1694 					ftsn_check = B_FALSE;
1695 				}
1696 				if (SCTP_CHUNK_DEST(mp) == oldfp)
1697 					goto out;
1698 			}
1699 		}
1700 		meta = meta->b_next;
1701 		if (meta != NULL && sctp->sctp_prsctp_aware) {
1702 			mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
1703 
1704 			while (meta != NULL && (SCTP_IS_MSG_ABANDONED(meta) ||
1705 			    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr, sctp))) {
1706 				meta = meta->b_next;
1707 			}
1708 		}
1709 	}
1710 window_probe:
1711 	/*
1712 	 * Retransmit fired for a destination which didn't have
1713 	 * any unacked data pending.
1714 	 */
1715 	if (sctp->sctp_unacked == 0 && sctp->sctp_unsent != 0) {
1716 		/*
1717 		 * Send a window probe. Inflate frwnd to allow
1718 		 * sending one segment.
1719 		 */
1720 		if (sctp->sctp_frwnd < (oldfp->sfa_pmss - sizeof (*sdc)))
1721 			sctp->sctp_frwnd = oldfp->sfa_pmss - sizeof (*sdc);
1722 
1723 		/* next TSN to send */
1724 		sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn;
1725 
1726 		/*
1727 		 * The above sctp_frwnd adjustment is coarse.  The "changed"
1728 		 * sctp_frwnd may allow us to send more than 1 packet.  So
1729 		 * tell sctp_output() to send only 1 packet.
1730 		 */
1731 		sctp_output(sctp, 1);
1732 
1733 		/* Last sent TSN */
1734 		sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn - 1;
1735 		ASSERT(sctp->sctp_rxt_maxtsn >= sctp->sctp_rxt_nxttsn);
1736 		sctp->sctp_zero_win_probe = B_TRUE;
1737 		BUMP_MIB(&sctps->sctps_mib, sctpOutWinProbe);
1738 	}
1739 	return;
1740 out:
1741 	/*
1742 	 * After a time out, assume that everything has left the network.  So
1743 	 * we can clear rxt_unacked for the original peer address.
1744 	 */
1745 	oldfp->rxt_unacked = 0;
1746 
1747 	/*
1748 	 * If we were probing for zero window, don't adjust retransmission
1749 	 * variables, but the timer is still backed off.
1750 	 */
1751 	if (sctp->sctp_zero_win_probe) {
1752 		mblk_t	*pkt;
1753 		uint_t	pkt_len;
1754 
1755 		/*
1756 		 * Get the Zero Win Probe for retrasmission, sctp_rxt_nxttsn
1757 		 * and sctp_rxt_maxtsn will specify the ZWP packet.
1758 		 */
1759 		fp = oldfp;
1760 		if (oldfp->state != SCTP_FADDRS_ALIVE)
1761 			fp = sctp_rotate_faddr(sctp, oldfp);
1762 		pkt = sctp_rexmit_packet(sctp, &meta, &mp, fp, &pkt_len);
1763 		if (pkt != NULL) {
1764 			ASSERT(pkt_len <= fp->sfa_pmss);
1765 			sctp_set_iplen(sctp, pkt);
1766 			sctp_add_sendq(sctp, pkt);
1767 		} else {
1768 			SCTP_KSTAT(sctps, sctp_ss_rexmit_failed);
1769 		}
1770 
1771 		/*
1772 		 * The strikes will be clear by sctp_faddr_alive() when the
1773 		 * other side sends us an ack.
1774 		 */
1775 		oldfp->strikes++;
1776 		sctp->sctp_strikes++;
1777 
1778 		SCTP_CALC_RXT(oldfp, sctp->sctp_rto_max);
1779 		if (oldfp != fp && oldfp->suna != 0)
1780 			SCTP_FADDR_TIMER_RESTART(sctp, oldfp, fp->rto);
1781 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1782 		BUMP_MIB(&sctps->sctps_mib, sctpOutWinProbe);
1783 		return;
1784 	}
1785 
1786 	/*
1787 	 * Enter slowstart for this destination
1788 	 */
1789 	oldfp->ssthresh = oldfp->cwnd / 2;
1790 	if (oldfp->ssthresh < 2 * oldfp->sfa_pmss)
1791 		oldfp->ssthresh = 2 * oldfp->sfa_pmss;
1792 	oldfp->cwnd = oldfp->sfa_pmss;
1793 	oldfp->pba = 0;
1794 	fp = sctp_rotate_faddr(sctp, oldfp);
1795 	ASSERT(fp != NULL);
1796 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
1797 
1798 	first_ua_tsn = ntohl(sdc->sdh_tsn);
1799 	if (do_ftsn) {
1800 		sctp_make_ftsns(sctp, meta, mp, &nmp, fp, &seglen);
1801 		if (nmp == NULL) {
1802 			sctp->sctp_adv_pap = adv_pap;
1803 			goto restart_timer;
1804 		}
1805 		head = nmp;
1806 		/*
1807 		 * Move to the next unabandoned chunk. XXXCheck if meta will
1808 		 * always be marked abandoned.
1809 		 */
1810 		while (meta != NULL && SCTP_IS_MSG_ABANDONED(meta))
1811 			meta = meta->b_next;
1812 		if (meta != NULL)
1813 			mp = mp->b_cont;
1814 		else
1815 			mp = NULL;
1816 		goto try_bundle;
1817 	}
1818 	seglen = ntohs(sdc->sdh_len);
1819 	chunklen = seglen - sizeof (*sdc);
1820 	if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
1821 		extra = SCTP_ALIGN - extra;
1822 
1823 	/* Find out if we need to piggyback SACK. */
1824 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
1825 		sacklen = 0;
1826 	} else {
1827 		sacklen = sizeof (sctp_chunk_hdr_t) +
1828 		    sizeof (sctp_sack_chunk_t) +
1829 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
1830 		if (seglen + sacklen > sctp->sctp_lastdata->sfa_pmss) {
1831 			/* piggybacked SACK doesn't fit */
1832 			sacklen = 0;
1833 		} else {
1834 			/*
1835 			 * OK, we have room to send SACK back.  But we
1836 			 * should send it back to the last fp where we
1837 			 * receive data from, unless sctp_lastdata equals
1838 			 * oldfp, then we should probably not send it
1839 			 * back to that fp.  Also we should check that
1840 			 * the fp is alive.
1841 			 */
1842 			if (sctp->sctp_lastdata != oldfp &&
1843 			    sctp->sctp_lastdata->state == SCTP_FADDRS_ALIVE) {
1844 				fp = sctp->sctp_lastdata;
1845 			}
1846 		}
1847 	}
1848 
1849 	/*
1850 	 * Cancel RTT measurement if the retransmitted TSN is before the
1851 	 * TSN used for timimg.
1852 	 */
1853 	if (sctp->sctp_out_time != 0 &&
1854 	    SEQ_GEQ(sctp->sctp_rtt_tsn, sdc->sdh_tsn)) {
1855 		sctp->sctp_out_time = 0;
1856 	}
1857 	/* Clear the counter as the RTT calculation may be off. */
1858 	fp->rtt_updates = 0;
1859 	oldfp->rtt_updates = 0;
1860 
1861 	/*
1862 	 * After a timeout, we should change the current faddr so that
1863 	 * new chunks will be sent to the alternate address.
1864 	 */
1865 	sctp_set_faddr_current(sctp, fp);
1866 
1867 	nmp = dupmsg(mp);
1868 	if (nmp == NULL)
1869 		goto restart_timer;
1870 	if (extra > 0) {
1871 		fill = sctp_get_padding(sctp, extra);
1872 		if (fill != NULL) {
1873 			linkb(nmp, fill);
1874 			seglen += extra;
1875 		} else {
1876 			freemsg(nmp);
1877 			goto restart_timer;
1878 		}
1879 	}
1880 	SCTP_CHUNK_CLEAR_FLAGS(nmp);
1881 	head = sctp_add_proto_hdr(sctp, fp, nmp, sacklen, NULL);
1882 	if (head == NULL) {
1883 		freemsg(nmp);
1884 		SCTP_KSTAT(sctps, sctp_rexmit_failed);
1885 		goto restart_timer;
1886 	}
1887 	seglen += sacklen;
1888 
1889 	SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
1890 
1891 	mp = mp->b_next;
1892 
1893 try_bundle:
1894 	/* We can at least and at most send 1 packet at timeout. */
1895 	while (seglen < fp->sfa_pmss) {
1896 		int32_t new_len;
1897 
1898 		/* Go through the list to find more chunks to be bundled. */
1899 		while (mp != NULL) {
1900 			/* Check if the chunk can be bundled. */
1901 			if (SCTP_CHUNK_RX_CANBUNDLE(mp, oldfp))
1902 				break;
1903 			mp = mp->b_next;
1904 		}
1905 		/* Go to the next message. */
1906 		if (mp == NULL) {
1907 			for (meta = meta->b_next; meta != NULL;
1908 			    meta = meta->b_next) {
1909 				mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
1910 
1911 				if (SCTP_IS_MSG_ABANDONED(meta) ||
1912 				    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr,
1913 				    sctp)) {
1914 					continue;
1915 				}
1916 
1917 				mp = meta->b_cont;
1918 				goto try_bundle;
1919 			}
1920 			/* No more chunk to be bundled. */
1921 			break;
1922 		}
1923 
1924 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
1925 		new_len = ntohs(sdc->sdh_len);
1926 		chunklen = new_len - sizeof (*sdc);
1927 
1928 		if ((extra = new_len & (SCTP_ALIGN - 1)) != 0)
1929 			extra = SCTP_ALIGN - extra;
1930 		if ((new_len = seglen + new_len + extra) > fp->sfa_pmss)
1931 			break;
1932 		if ((nmp = dupmsg(mp)) == NULL)
1933 			break;
1934 
1935 		if (extra > 0) {
1936 			fill = sctp_get_padding(sctp, extra);
1937 			if (fill != NULL) {
1938 				linkb(nmp, fill);
1939 			} else {
1940 				freemsg(nmp);
1941 				break;
1942 			}
1943 		}
1944 		linkb(head, nmp);
1945 
1946 		SCTP_CHUNK_CLEAR_FLAGS(nmp);
1947 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
1948 
1949 		seglen = new_len;
1950 		mp = mp->b_next;
1951 	}
1952 done_bundle:
1953 	if ((seglen > fp->sfa_pmss) && fp->isv4) {
1954 		ipha_t *iph = (ipha_t *)head->b_rptr;
1955 
1956 		/*
1957 		 * Path MTU is different from path we thought it would
1958 		 * be when we created chunks, or IP headers have grown.
1959 		 * Need to clear the DF bit.
1960 		 */
1961 		iph->ipha_fragment_offset_and_flags = 0;
1962 	}
1963 	fp->rxt_unacked += seglen;
1964 
1965 	dprint(2, ("sctp_rexmit: Sending packet %d bytes, tsn %x "
1966 	    "ssn %d to %p (rwnd %d, lastack_rxd %x)\n",
1967 	    seglen, ntohl(sdc->sdh_tsn), ntohs(sdc->sdh_ssn),
1968 	    (void *)fp, sctp->sctp_frwnd, sctp->sctp_lastack_rxd));
1969 
1970 	sctp->sctp_rexmitting = B_TRUE;
1971 	sctp->sctp_rxt_nxttsn = first_ua_tsn;
1972 	sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn - 1;
1973 	sctp_set_iplen(sctp, head);
1974 	sctp_add_sendq(sctp, head);
1975 
1976 	/*
1977 	 * Restart the oldfp timer with exponential backoff and
1978 	 * the new fp timer for the retransmitted chunks.
1979 	 */
1980 restart_timer:
1981 	oldfp->strikes++;
1982 	sctp->sctp_strikes++;
1983 	SCTP_CALC_RXT(oldfp, sctp->sctp_rto_max);
1984 	/*
1985 	 * If there is still some data in the oldfp, restart the
1986 	 * retransmission timer.  If there is no data, the heartbeat will
1987 	 * continue to run so it will do its job in checking the reachability
1988 	 * of the oldfp.
1989 	 */
1990 	if (oldfp != fp && oldfp->suna != 0)
1991 		SCTP_FADDR_TIMER_RESTART(sctp, oldfp, oldfp->rto);
1992 
1993 	/*
1994 	 * Should we restart the timer of the new fp?  If there is
1995 	 * outstanding data to the new fp, the timer should be
1996 	 * running already.  So restarting it means that the timer
1997 	 * will fire later for those outstanding data.  But if
1998 	 * we don't restart it, the timer will fire too early for the
1999 	 * just retransmitted chunks to the new fp.  The reason is that we
2000 	 * don't keep a timestamp on when a chunk is retransmitted.
2001 	 * So when the timer fires, it will just search for the
2002 	 * chunk with the earliest TSN sent to new fp.  This probably
2003 	 * is the chunk we just retransmitted.  So for now, let's
2004 	 * be conservative and restart the timer of the new fp.
2005 	 */
2006 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
2007 
2008 	sctp->sctp_active = lbolt64;
2009 }
2010 
2011 /*
2012  * This function is called by sctp_ss_rexmit() to create a packet
2013  * to be retransmitted to the given fp.  The given meta and mp
2014  * parameters are respectively the sctp_msg_hdr_t and the mblk of the
2015  * first chunk to be retransmitted.  This is also called when we want
2016  * to retransmit a zero window probe from sctp_rexmit() or when we
2017  * want to retransmit the zero window probe after the window has
2018  * opened from sctp_got_sack().
2019  */
2020 mblk_t *
2021 sctp_rexmit_packet(sctp_t *sctp, mblk_t **meta, mblk_t **mp, sctp_faddr_t *fp,
2022     uint_t *packet_len)
2023 {
2024 	uint32_t	seglen = 0;
2025 	uint16_t	chunklen;
2026 	int		extra;
2027 	mblk_t		*nmp;
2028 	mblk_t		*head;
2029 	mblk_t		*fill;
2030 	sctp_data_hdr_t	*sdc;
2031 	sctp_msg_hdr_t	*mhdr;
2032 
2033 	sdc = (sctp_data_hdr_t *)(*mp)->b_rptr;
2034 	seglen = ntohs(sdc->sdh_len);
2035 	chunklen = seglen - sizeof (*sdc);
2036 	if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
2037 		extra = SCTP_ALIGN - extra;
2038 
2039 	nmp = dupmsg(*mp);
2040 	if (nmp == NULL)
2041 		return (NULL);
2042 	if (extra > 0) {
2043 		fill = sctp_get_padding(sctp, extra);
2044 		if (fill != NULL) {
2045 			linkb(nmp, fill);
2046 			seglen += extra;
2047 		} else {
2048 			freemsg(nmp);
2049 			return (NULL);
2050 		}
2051 	}
2052 	SCTP_CHUNK_CLEAR_FLAGS(nmp);
2053 	head = sctp_add_proto_hdr(sctp, fp, nmp, 0, NULL);
2054 	if (head == NULL) {
2055 		freemsg(nmp);
2056 		return (NULL);
2057 	}
2058 	SCTP_CHUNK_SENT(sctp, *mp, sdc, fp, chunklen, *meta);
2059 	/*
2060 	 * Don't update the TSN if we are doing a Zero Win Probe.
2061 	 */
2062 	if (!sctp->sctp_zero_win_probe)
2063 		sctp->sctp_rxt_nxttsn = ntohl(sdc->sdh_tsn);
2064 	*mp = (*mp)->b_next;
2065 
2066 try_bundle:
2067 	while (seglen < fp->sfa_pmss) {
2068 		int32_t new_len;
2069 
2070 		/*
2071 		 * Go through the list to find more chunks to be bundled.
2072 		 * We should only retransmit sent by unack'ed chunks.  Since
2073 		 * they were sent before, the peer's receive window should
2074 		 * be able to receive them.
2075 		 */
2076 		while (*mp != NULL) {
2077 			/* Check if the chunk can be bundled. */
2078 			if (SCTP_CHUNK_ISSENT(*mp) && !SCTP_CHUNK_ISACKED(*mp))
2079 				break;
2080 			*mp = (*mp)->b_next;
2081 		}
2082 		/* Go to the next message. */
2083 		if (*mp == NULL) {
2084 			for (*meta = (*meta)->b_next; *meta != NULL;
2085 			    *meta = (*meta)->b_next) {
2086 				mhdr = (sctp_msg_hdr_t *)(*meta)->b_rptr;
2087 
2088 				if (SCTP_IS_MSG_ABANDONED(*meta) ||
2089 				    SCTP_MSG_TO_BE_ABANDONED(*meta, mhdr,
2090 				    sctp)) {
2091 					continue;
2092 				}
2093 
2094 				*mp = (*meta)->b_cont;
2095 				goto try_bundle;
2096 			}
2097 			/* No more chunk to be bundled. */
2098 			break;
2099 		}
2100 
2101 		sdc = (sctp_data_hdr_t *)(*mp)->b_rptr;
2102 		/* Don't bundle chunks beyond sctp_rxt_maxtsn. */
2103 		if (SEQ_GT(ntohl(sdc->sdh_tsn), sctp->sctp_rxt_maxtsn))
2104 			break;
2105 		new_len = ntohs(sdc->sdh_len);
2106 		chunklen = new_len - sizeof (*sdc);
2107 
2108 		if ((extra = new_len & (SCTP_ALIGN - 1)) != 0)
2109 			extra = SCTP_ALIGN - extra;
2110 		if ((new_len = seglen + new_len + extra) > fp->sfa_pmss)
2111 			break;
2112 		if ((nmp = dupmsg(*mp)) == NULL)
2113 			break;
2114 
2115 		if (extra > 0) {
2116 			fill = sctp_get_padding(sctp, extra);
2117 			if (fill != NULL) {
2118 				linkb(nmp, fill);
2119 			} else {
2120 				freemsg(nmp);
2121 				break;
2122 			}
2123 		}
2124 		linkb(head, nmp);
2125 
2126 		SCTP_CHUNK_CLEAR_FLAGS(nmp);
2127 		SCTP_CHUNK_SENT(sctp, *mp, sdc, fp, chunklen, *meta);
2128 		/*
2129 		 * Don't update the TSN if we are doing a Zero Win Probe.
2130 		 */
2131 		if (!sctp->sctp_zero_win_probe)
2132 			sctp->sctp_rxt_nxttsn = ntohl(sdc->sdh_tsn);
2133 
2134 		seglen = new_len;
2135 		*mp = (*mp)->b_next;
2136 	}
2137 	*packet_len = seglen;
2138 	fp->rxt_unacked += seglen;
2139 	return (head);
2140 }
2141 
2142 /*
2143  * sctp_ss_rexmit() is called when we get a SACK after a timeout which
2144  * advances the cum_tsn but the cum_tsn is still less than what we have sent
2145  * (sctp_rxt_maxtsn) at the time of the timeout.  This SACK is a "partial"
2146  * SACK.  We retransmit unacked chunks without having to wait for another
2147  * timeout.  The rationale is that the SACK should not be "partial" if all the
2148  * lost chunks have been retransmitted.  Since the SACK is "partial,"
2149  * the chunks between the cum_tsn and the sctp_rxt_maxtsn should still
2150  * be missing.  It is better for us to retransmit them now instead
2151  * of waiting for a timeout.
2152  */
2153 void
2154 sctp_ss_rexmit(sctp_t *sctp)
2155 {
2156 	mblk_t		*meta;
2157 	mblk_t		*mp;
2158 	mblk_t		*pkt;
2159 	sctp_faddr_t	*fp;
2160 	uint_t		pkt_len;
2161 	uint32_t	tot_wnd;
2162 	sctp_data_hdr_t	*sdc;
2163 	int		burst;
2164 	sctp_stack_t	*sctps = sctp->sctp_sctps;
2165 
2166 	ASSERT(!sctp->sctp_zero_win_probe);
2167 
2168 	/*
2169 	 * If the last cum ack is smaller than what we have just
2170 	 * retransmitted, simply return.
2171 	 */
2172 	if (SEQ_GEQ(sctp->sctp_lastack_rxd, sctp->sctp_rxt_nxttsn))
2173 		sctp->sctp_rxt_nxttsn = sctp->sctp_lastack_rxd + 1;
2174 	else
2175 		return;
2176 	ASSERT(SEQ_LEQ(sctp->sctp_rxt_nxttsn, sctp->sctp_rxt_maxtsn));
2177 
2178 	/*
2179 	 * After a timer fires, sctp_current should be set to the new
2180 	 * fp where the retransmitted chunks are sent.
2181 	 */
2182 	fp = sctp->sctp_current;
2183 
2184 	/*
2185 	 * Since we are retransmitting, we only need to use cwnd to determine
2186 	 * how much we can send as we were allowed (by peer's receive window)
2187 	 * to send those retransmitted chunks previously when they are first
2188 	 * sent.  If we record how much we have retransmitted but
2189 	 * unacknowledged using rxt_unacked, then the amount we can now send
2190 	 * is equal to cwnd minus rxt_unacked.
2191 	 *
2192 	 * The field rxt_unacked is incremented when we retransmit a packet
2193 	 * and decremented when we got a SACK acknowledging something.  And
2194 	 * it is reset when the retransmission timer fires as we assume that
2195 	 * all packets have left the network after a timeout.  If this
2196 	 * assumption is not true, it means that after a timeout, we can
2197 	 * get a SACK acknowledging more than rxt_unacked (its value only
2198 	 * contains what is retransmitted when the timer fires).  So
2199 	 * rxt_unacked will become very big (it is an unsiged int so going
2200 	 * negative means that the value is huge).  This is the reason we
2201 	 * always send at least 1 MSS bytes.
2202 	 *
2203 	 * The reason why we do not have an accurate count is that we
2204 	 * only know how many packets are outstanding (using the TSN numbers).
2205 	 * But we do not know how many bytes those packets contain.  To
2206 	 * have an accurate count, we need to walk through the send list.
2207 	 * As it is not really important to have an accurate count during
2208 	 * retransmission, we skip this walk to save some time.  This should
2209 	 * not make the retransmission too aggressive to cause congestion.
2210 	 */
2211 	if (fp->cwnd <= fp->rxt_unacked)
2212 		tot_wnd = fp->sfa_pmss;
2213 	else
2214 		tot_wnd = fp->cwnd - fp->rxt_unacked;
2215 
2216 	/* Find the first unack'ed chunk */
2217 	for (meta = sctp->sctp_xmit_head; meta != NULL; meta = meta->b_next) {
2218 		sctp_msg_hdr_t	*mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
2219 
2220 		if (SCTP_IS_MSG_ABANDONED(meta) ||
2221 		    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr, sctp)) {
2222 			continue;
2223 		}
2224 
2225 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
2226 			/* Again, this may not be possible */
2227 			if (!SCTP_CHUNK_ISSENT(mp))
2228 				return;
2229 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
2230 			if (ntohl(sdc->sdh_tsn) == sctp->sctp_rxt_nxttsn)
2231 				goto found_msg;
2232 		}
2233 	}
2234 
2235 	/* Everything is abandoned... */
2236 	return;
2237 
2238 found_msg:
2239 	if (!fp->timer_running)
2240 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
2241 	pkt = sctp_rexmit_packet(sctp, &meta, &mp, fp, &pkt_len);
2242 	if (pkt == NULL) {
2243 		SCTP_KSTAT(sctps, sctp_ss_rexmit_failed);
2244 		return;
2245 	}
2246 	if ((pkt_len > fp->sfa_pmss) && fp->isv4) {
2247 		ipha_t	*iph = (ipha_t *)pkt->b_rptr;
2248 
2249 		/*
2250 		 * Path MTU is different from path we thought it would
2251 		 * be when we created chunks, or IP headers have grown.
2252 		 *  Need to clear the DF bit.
2253 		 */
2254 		iph->ipha_fragment_offset_and_flags = 0;
2255 	}
2256 	sctp_set_iplen(sctp, pkt);
2257 	sctp_add_sendq(sctp, pkt);
2258 
2259 	/* Check and see if there is more chunk to be retransmitted. */
2260 	if (tot_wnd <= pkt_len || tot_wnd - pkt_len < fp->sfa_pmss ||
2261 	    meta == NULL)
2262 		return;
2263 	if (mp == NULL)
2264 		meta = meta->b_next;
2265 	if (meta == NULL)
2266 		return;
2267 
2268 	/* Retransmit another packet if the window allows. */
2269 	for (tot_wnd -= pkt_len, burst = sctps->sctps_maxburst - 1;
2270 	    meta != NULL && burst > 0; meta = meta->b_next, burst--) {
2271 		if (mp == NULL)
2272 			mp = meta->b_cont;
2273 		for (; mp != NULL; mp = mp->b_next) {
2274 			/* Again, this may not be possible */
2275 			if (!SCTP_CHUNK_ISSENT(mp))
2276 				return;
2277 			if (!SCTP_CHUNK_ISACKED(mp))
2278 				goto found_msg;
2279 		}
2280 	}
2281 }
2282