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