xref: /titanic_41/usr/src/uts/common/inet/sctp/sctp_output.c (revision ed5289f91b9bf164dccd6c75398362be77a4478d)
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  * When called from sctp_rexmit(), we don't want to chunkify, so 'cansend'
789  * will be set to 0.
790  */
791 mblk_t *
792 sctp_get_msg_to_send(sctp_t *sctp, mblk_t **mp, mblk_t *meta, int  *error,
793     int32_t firstseg, uint32_t cansend, sctp_faddr_t *fp)
794 {
795 	mblk_t		*mp1;
796 	sctp_msg_hdr_t	*msg_hdr;
797 	mblk_t		*tmp_meta;
798 	sctp_faddr_t	*fp1;
799 
800 	ASSERT(error != NULL && mp != NULL);
801 	*error = 0;
802 
803 	ASSERT(sctp->sctp_current != NULL);
804 
805 chunkified:
806 	while (meta != NULL) {
807 		tmp_meta = meta->b_next;
808 		msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
809 		mp1 = meta->b_cont;
810 		if (SCTP_IS_MSG_ABANDONED(meta))
811 			goto next_msg;
812 		if (!SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
813 			while (mp1 != NULL) {
814 				if (SCTP_CHUNK_CANSEND(mp1)) {
815 					*mp = mp1;
816 #ifdef	DEBUG
817 					ASSERT(sctp_verify_chain(
818 					    sctp->sctp_xmit_head, meta));
819 #endif
820 					return (meta);
821 				}
822 				mp1 = mp1->b_next;
823 			}
824 			goto next_msg;
825 		}
826 		/*
827 		 * If we come here and the first chunk is sent, then we
828 		 * we are PR-SCTP aware, in which case if the cumulative
829 		 * TSN has moved upto or beyond the first chunk (which
830 		 * means all the previous messages have been cumulative
831 		 * SACK'd), then we send a Forward TSN with the last
832 		 * chunk that was sent in this message. If we can't send
833 		 * a Forward TSN because previous non-abandoned messages
834 		 * have not been acked then we will defer the Forward TSN
835 		 * to sctp_rexmit() or sctp_cumack().
836 		 */
837 		if (SCTP_CHUNK_ISSENT(mp1)) {
838 			*error = sctp_check_abandoned_msg(sctp, meta);
839 			if (*error != 0) {
840 #ifdef	DEBUG
841 				ASSERT(sctp_verify_chain(sctp->sctp_xmit_head,
842 				    sctp->sctp_xmit_tail));
843 #endif
844 				return (NULL);
845 			}
846 			goto next_msg;
847 		}
848 		BUMP_LOCAL(sctp->sctp_prsctpdrop);
849 		ASSERT(sctp->sctp_unsent >= msg_hdr->smh_msglen);
850 		if (meta->b_prev == NULL) {
851 			ASSERT(sctp->sctp_xmit_head == meta);
852 			sctp->sctp_xmit_head = tmp_meta;
853 			if (sctp->sctp_xmit_tail == meta)
854 				sctp->sctp_xmit_tail = tmp_meta;
855 			meta->b_next = NULL;
856 			if (tmp_meta != NULL)
857 				tmp_meta->b_prev = NULL;
858 		} else if (meta->b_next == NULL) {
859 			if (sctp->sctp_xmit_tail == meta)
860 				sctp->sctp_xmit_tail = meta->b_prev;
861 			meta->b_prev->b_next = NULL;
862 			meta->b_prev = NULL;
863 		} else {
864 			meta->b_prev->b_next = tmp_meta;
865 			tmp_meta->b_prev = meta->b_prev;
866 			if (sctp->sctp_xmit_tail == meta)
867 				sctp->sctp_xmit_tail = tmp_meta;
868 			meta->b_prev = NULL;
869 			meta->b_next = NULL;
870 		}
871 		sctp->sctp_unsent -= msg_hdr->smh_msglen;
872 		/*
873 		 * Update ULP the amount of queued data, which is
874 		 * sent-unack'ed + unsent.
875 		 */
876 		if (!SCTP_IS_DETACHED(sctp)) {
877 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
878 			    sctp->sctp_unacked + sctp->sctp_unsent);
879 		}
880 		sctp_sendfail_event(sctp, meta, 0, B_TRUE);
881 next_msg:
882 		meta = tmp_meta;
883 	}
884 	/* chunkify, if needed */
885 	if (cansend > 0 && sctp->sctp_xmit_unsent != NULL) {
886 		ASSERT(sctp->sctp_unsent > 0);
887 		if (fp == NULL) {
888 			fp = SCTP_CHUNK_DEST(sctp->sctp_xmit_unsent);
889 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
890 				fp = sctp->sctp_current;
891 		} else {
892 			/*
893 			 * If user specified destination, try to honor that.
894 			 */
895 			fp1 = SCTP_CHUNK_DEST(sctp->sctp_xmit_unsent);
896 			if (fp1 != NULL && fp1->state == SCTP_FADDRS_ALIVE &&
897 			    fp1 != fp) {
898 				goto chunk_done;
899 			}
900 		}
901 		sctp_chunkify(sctp, fp->sfa_pmss - firstseg, cansend);
902 		if ((meta = sctp->sctp_xmit_tail) == NULL)
903 			goto chunk_done;
904 		/*
905 		 * sctp_chunkify() won't advance sctp_xmit_tail if it adds
906 		 * new chunk(s) to the tail, so we need to skip the
907 		 * sctp_xmit_tail, which would have already been processed.
908 		 * This could happen when there is unacked chunks, but
909 		 * nothing new to send.
910 		 * When sctp_chunkify() is called when the transmit queue
911 		 * is empty then we need to start from sctp_xmit_tail.
912 		 */
913 		if (SCTP_CHUNK_ISSENT(sctp->sctp_xmit_tail->b_cont)) {
914 #ifdef	DEBUG
915 			mp1 = sctp->sctp_xmit_tail->b_cont;
916 			while (mp1 != NULL) {
917 				ASSERT(!SCTP_CHUNK_CANSEND(mp1));
918 				mp1 = mp1->b_next;
919 			}
920 #endif
921 			if ((meta = sctp->sctp_xmit_tail->b_next) == NULL)
922 				goto chunk_done;
923 		}
924 		goto chunkified;
925 	}
926 chunk_done:
927 #ifdef	DEBUG
928 	ASSERT(sctp_verify_chain(sctp->sctp_xmit_head, sctp->sctp_xmit_tail));
929 #endif
930 	return (NULL);
931 }
932 
933 void
934 sctp_fast_rexmit(sctp_t *sctp)
935 {
936 	mblk_t		*mp, *head;
937 	int		pktlen = 0;
938 	sctp_faddr_t	*fp = NULL;
939 	sctp_stack_t	*sctps = sctp->sctp_sctps;
940 
941 	ASSERT(sctp->sctp_xmit_head != NULL);
942 	mp = sctp_find_fast_rexmit_mblks(sctp, &pktlen, &fp);
943 	if (mp == NULL) {
944 		SCTP_KSTAT(sctps, sctp_fr_not_found);
945 		return;
946 	}
947 	if ((head = sctp_add_proto_hdr(sctp, fp, mp, 0, NULL)) == NULL) {
948 		freemsg(mp);
949 		SCTP_KSTAT(sctps, sctp_fr_add_hdr);
950 		return;
951 	}
952 	if ((pktlen > fp->sfa_pmss) && fp->isv4) {
953 		ipha_t *iph = (ipha_t *)head->b_rptr;
954 
955 		iph->ipha_fragment_offset_and_flags = 0;
956 	}
957 
958 	sctp_set_iplen(sctp, head);
959 	sctp_add_sendq(sctp, head);
960 	sctp->sctp_active = fp->lastactive = lbolt64;
961 }
962 
963 void
964 sctp_output(sctp_t *sctp, uint_t num_pkt)
965 {
966 	mblk_t			*mp = NULL;
967 	mblk_t			*nmp;
968 	mblk_t			*head;
969 	mblk_t			*meta = sctp->sctp_xmit_tail;
970 	mblk_t			*fill = NULL;
971 	uint16_t 		chunklen;
972 	uint32_t 		cansend;
973 	int32_t			seglen;
974 	int32_t			xtralen;
975 	int32_t			sacklen;
976 	int32_t			pad = 0;
977 	int32_t			pathmax;
978 	int			extra;
979 	int64_t			now = lbolt64;
980 	sctp_faddr_t		*fp;
981 	sctp_faddr_t		*lfp;
982 	sctp_data_hdr_t		*sdc;
983 	int			error;
984 	boolean_t		notsent = B_TRUE;
985 	sctp_stack_t		*sctps = sctp->sctp_sctps;
986 
987 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
988 		sacklen = 0;
989 	} else {
990 		/* send a SACK chunk */
991 		sacklen = sizeof (sctp_chunk_hdr_t) +
992 		    sizeof (sctp_sack_chunk_t) +
993 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
994 		lfp = sctp->sctp_lastdata;
995 		ASSERT(lfp != NULL);
996 		if (lfp->state != SCTP_FADDRS_ALIVE)
997 			lfp = sctp->sctp_current;
998 	}
999 
1000 	cansend = sctp->sctp_frwnd;
1001 	if (sctp->sctp_unsent < cansend)
1002 		cansend = sctp->sctp_unsent;
1003 	if ((cansend < sctp->sctp_current->sfa_pmss / 2) &&
1004 	    sctp->sctp_unacked &&
1005 	    (sctp->sctp_unacked < sctp->sctp_current->sfa_pmss) &&
1006 	    !sctp->sctp_ndelay) {
1007 		head = NULL;
1008 		fp = sctp->sctp_current;
1009 		goto unsent_data;
1010 	}
1011 	if (meta != NULL)
1012 		mp = meta->b_cont;
1013 	while (cansend > 0 && num_pkt-- != 0) {
1014 		pad = 0;
1015 
1016 		/*
1017 		 * Find first segment eligible for transmit.
1018 		 */
1019 		while (mp != NULL) {
1020 			if (SCTP_CHUNK_CANSEND(mp))
1021 				break;
1022 			mp = mp->b_next;
1023 		}
1024 		if (mp == NULL) {
1025 			meta = sctp_get_msg_to_send(sctp, &mp,
1026 			    meta == NULL ? NULL : meta->b_next, &error, sacklen,
1027 			    cansend, NULL);
1028 			if (error != 0 || meta == NULL) {
1029 				head = NULL;
1030 				fp = sctp->sctp_current;
1031 				goto unsent_data;
1032 			}
1033 			sctp->sctp_xmit_tail =  meta;
1034 		}
1035 
1036 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
1037 		seglen = ntohs(sdc->sdh_len);
1038 		xtralen = sizeof (*sdc);
1039 		chunklen = seglen - xtralen;
1040 
1041 		/*
1042 		 * Check rwnd.
1043 		 */
1044 		if (chunklen > cansend) {
1045 			head = NULL;
1046 			fp = SCTP_CHUNK_DEST(meta);
1047 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
1048 				fp = sctp->sctp_current;
1049 			goto unsent_data;
1050 		}
1051 		if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
1052 			extra = SCTP_ALIGN - extra;
1053 
1054 		/*
1055 		 * Pick destination address, and check cwnd.
1056 		 */
1057 		if (sacklen > 0 && (seglen + extra <= lfp->cwnd - lfp->suna) &&
1058 		    (seglen + sacklen + extra <= lfp->sfa_pmss)) {
1059 			/*
1060 			 * Only include SACK chunk if it can be bundled
1061 			 * with a data chunk, and sent to sctp_lastdata.
1062 			 */
1063 			pathmax = lfp->cwnd - lfp->suna;
1064 
1065 			fp = lfp;
1066 			if ((nmp = dupmsg(mp)) == NULL) {
1067 				head = NULL;
1068 				goto unsent_data;
1069 			}
1070 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1071 			head = sctp_add_proto_hdr(sctp, fp, nmp, sacklen,
1072 			    &error);
1073 			if (head == NULL) {
1074 				/*
1075 				 * If none of the source addresses are
1076 				 * available (i.e error == EHOSTUNREACH),
1077 				 * pretend we have sent the data. We will
1078 				 * eventually time out trying to retramsmit
1079 				 * the data if the interface never comes up.
1080 				 * If we have already sent some stuff (i.e.,
1081 				 * notsent is B_FALSE) then we are fine, else
1082 				 * just mark this packet as sent.
1083 				 */
1084 				if (notsent && error == EHOSTUNREACH) {
1085 					SCTP_CHUNK_SENT(sctp, mp, sdc,
1086 					    fp, chunklen, meta);
1087 				}
1088 				freemsg(nmp);
1089 				SCTP_KSTAT(sctps, sctp_output_failed);
1090 				goto unsent_data;
1091 			}
1092 			seglen += sacklen;
1093 			xtralen += sacklen;
1094 			sacklen = 0;
1095 		} else {
1096 			fp = SCTP_CHUNK_DEST(meta);
1097 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
1098 				fp = sctp->sctp_current;
1099 			/*
1100 			 * If we haven't sent data to this destination for
1101 			 * a while, do slow start again.
1102 			 */
1103 			if (now - fp->lastactive > fp->rto) {
1104 				SET_CWND(fp, fp->sfa_pmss,
1105 				    sctps->sctps_slow_start_after_idle);
1106 			}
1107 
1108 			pathmax = fp->cwnd - fp->suna;
1109 			if (seglen + extra > pathmax) {
1110 				head = NULL;
1111 				goto unsent_data;
1112 			}
1113 			if ((nmp = dupmsg(mp)) == NULL) {
1114 				head = NULL;
1115 				goto unsent_data;
1116 			}
1117 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1118 			head = sctp_add_proto_hdr(sctp, fp, nmp, 0, &error);
1119 			if (head == NULL) {
1120 				/*
1121 				 * If none of the source addresses are
1122 				 * available (i.e error == EHOSTUNREACH),
1123 				 * pretend we have sent the data. We will
1124 				 * eventually time out trying to retramsmit
1125 				 * the data if the interface never comes up.
1126 				 * If we have already sent some stuff (i.e.,
1127 				 * notsent is B_FALSE) then we are fine, else
1128 				 * just mark this packet as sent.
1129 				 */
1130 				if (notsent && error == EHOSTUNREACH) {
1131 					SCTP_CHUNK_SENT(sctp, mp, sdc,
1132 					    fp, chunklen, meta);
1133 				}
1134 				freemsg(nmp);
1135 				SCTP_KSTAT(sctps, sctp_output_failed);
1136 				goto unsent_data;
1137 			}
1138 		}
1139 		fp->lastactive = now;
1140 		if (pathmax > fp->sfa_pmss)
1141 			pathmax = fp->sfa_pmss;
1142 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
1143 		mp = mp->b_next;
1144 
1145 		/* Use this chunk to measure RTT? */
1146 		if (sctp->sctp_out_time == 0) {
1147 			sctp->sctp_out_time = now;
1148 			sctp->sctp_rtt_tsn = sctp->sctp_ltsn - 1;
1149 			ASSERT(sctp->sctp_rtt_tsn == ntohl(sdc->sdh_tsn));
1150 		}
1151 		if (extra > 0) {
1152 			fill = sctp_get_padding(sctp, extra);
1153 			if (fill != NULL) {
1154 				linkb(head, fill);
1155 				pad = extra;
1156 				seglen += extra;
1157 			} else {
1158 				goto unsent_data;
1159 			}
1160 		}
1161 		/* See if we can bundle more. */
1162 		while (seglen < pathmax) {
1163 			int32_t		new_len;
1164 			int32_t		new_xtralen;
1165 
1166 			while (mp != NULL) {
1167 				if (SCTP_CHUNK_CANSEND(mp))
1168 					break;
1169 				mp = mp->b_next;
1170 			}
1171 			if (mp == NULL) {
1172 				meta = sctp_get_msg_to_send(sctp, &mp,
1173 				    meta->b_next, &error, seglen,
1174 				    (seglen - xtralen) >= cansend ? 0 :
1175 				    cansend - seglen, fp);
1176 				if (error != 0 || meta == NULL)
1177 					break;
1178 				sctp->sctp_xmit_tail =  meta;
1179 			}
1180 			ASSERT(mp != NULL);
1181 			if (!SCTP_CHUNK_ISSENT(mp) && SCTP_CHUNK_DEST(meta) &&
1182 			    fp != SCTP_CHUNK_DEST(meta)) {
1183 				break;
1184 			}
1185 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
1186 			chunklen = ntohs(sdc->sdh_len);
1187 			if ((extra = chunklen  & (SCTP_ALIGN - 1)) != 0)
1188 				extra = SCTP_ALIGN - extra;
1189 
1190 			new_len = seglen + chunklen;
1191 			new_xtralen = xtralen + sizeof (*sdc);
1192 			chunklen -= sizeof (*sdc);
1193 
1194 			if (new_len - new_xtralen > cansend ||
1195 			    new_len + extra > pathmax) {
1196 				break;
1197 			}
1198 			if ((nmp = dupmsg(mp)) == NULL)
1199 				break;
1200 			if (extra > 0) {
1201 				fill = sctp_get_padding(sctp, extra);
1202 				if (fill != NULL) {
1203 					pad += extra;
1204 					new_len += extra;
1205 					linkb(nmp, fill);
1206 				} else {
1207 					freemsg(nmp);
1208 					break;
1209 				}
1210 			}
1211 			seglen = new_len;
1212 			xtralen = new_xtralen;
1213 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1214 			SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
1215 			linkb(head, nmp);
1216 			mp = mp->b_next;
1217 		}
1218 		if ((seglen > fp->sfa_pmss) && fp->isv4) {
1219 			ipha_t *iph = (ipha_t *)head->b_rptr;
1220 
1221 			/*
1222 			 * Path MTU is different from what we thought it would
1223 			 * be when we created chunks, or IP headers have grown.
1224 			 * Need to clear the DF bit.
1225 			 */
1226 			iph->ipha_fragment_offset_and_flags = 0;
1227 		}
1228 		/* xmit segment */
1229 		ASSERT(cansend >= seglen - pad - xtralen);
1230 		cansend -= (seglen - pad - xtralen);
1231 		dprint(2, ("sctp_output: Sending packet %d bytes, tsn %x "
1232 		    "ssn %d to %p (rwnd %d, cansend %d, lastack_rxd %x)\n",
1233 		    seglen - xtralen, ntohl(sdc->sdh_tsn),
1234 		    ntohs(sdc->sdh_ssn), (void *)fp, sctp->sctp_frwnd,
1235 		    cansend, sctp->sctp_lastack_rxd));
1236 		sctp_set_iplen(sctp, head);
1237 		sctp_add_sendq(sctp, head);
1238 		/* arm rto timer (if not set) */
1239 		if (!fp->timer_running)
1240 			SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1241 		notsent = B_FALSE;
1242 	}
1243 	sctp->sctp_active = now;
1244 	return;
1245 unsent_data:
1246 	/* arm persist timer (if rto timer not set) */
1247 	if (!fp->timer_running)
1248 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1249 	if (head != NULL)
1250 		freemsg(head);
1251 }
1252 
1253 /*
1254  * The following two functions initialize and destroy the cache
1255  * associated with the sets used for PR-SCTP.
1256  */
1257 void
1258 sctp_ftsn_sets_init(void)
1259 {
1260 	sctp_kmem_ftsn_set_cache = kmem_cache_create("sctp_ftsn_set_cache",
1261 	    sizeof (sctp_ftsn_set_t), 0, NULL, NULL, NULL, NULL,
1262 	    NULL, 0);
1263 }
1264 
1265 void
1266 sctp_ftsn_sets_fini(void)
1267 {
1268 	kmem_cache_destroy(sctp_kmem_ftsn_set_cache);
1269 }
1270 
1271 
1272 /* Free PR-SCTP sets */
1273 void
1274 sctp_free_ftsn_set(sctp_ftsn_set_t *s)
1275 {
1276 	sctp_ftsn_set_t *p;
1277 
1278 	while (s != NULL) {
1279 		p = s->next;
1280 		s->next = NULL;
1281 		kmem_cache_free(sctp_kmem_ftsn_set_cache, s);
1282 		s = p;
1283 	}
1284 }
1285 
1286 /*
1287  * Given a message meta block, meta, this routine creates or modifies
1288  * the set that will be used to generate a Forward TSN chunk. If the
1289  * entry for stream id, sid, for this message already exists, the
1290  * sequence number, ssn, is updated if it is greater than the existing
1291  * one. If an entry for this sid does not exist, one is created if
1292  * the size does not exceed fp->sfa_pmss. We return false in case
1293  * or an error.
1294  */
1295 boolean_t
1296 sctp_add_ftsn_set(sctp_ftsn_set_t **s, sctp_faddr_t *fp, mblk_t *meta,
1297     uint_t *nsets, uint32_t *slen)
1298 {
1299 	sctp_ftsn_set_t		*p;
1300 	sctp_msg_hdr_t		*msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
1301 	uint16_t		sid = htons(msg_hdr->smh_sid);
1302 	/* msg_hdr->smh_ssn is already in NBO */
1303 	uint16_t		ssn = msg_hdr->smh_ssn;
1304 
1305 	ASSERT(s != NULL && nsets != NULL);
1306 	ASSERT((*nsets == 0 && *s == NULL) || (*nsets > 0 && *s != NULL));
1307 
1308 	if (*s == NULL) {
1309 		ASSERT((*slen + sizeof (uint32_t)) <= fp->sfa_pmss);
1310 		*s = kmem_cache_alloc(sctp_kmem_ftsn_set_cache, KM_NOSLEEP);
1311 		if (*s == NULL)
1312 			return (B_FALSE);
1313 		(*s)->ftsn_entries.ftsn_sid = sid;
1314 		(*s)->ftsn_entries.ftsn_ssn = ssn;
1315 		(*s)->next = NULL;
1316 		*nsets = 1;
1317 		*slen += sizeof (uint32_t);
1318 		return (B_TRUE);
1319 	}
1320 	for (p = *s; p->next != NULL; p = p->next) {
1321 		if (p->ftsn_entries.ftsn_sid == sid) {
1322 			if (SSN_GT(ssn, p->ftsn_entries.ftsn_ssn))
1323 				p->ftsn_entries.ftsn_ssn = ssn;
1324 			return (B_TRUE);
1325 		}
1326 	}
1327 	/* the last one */
1328 	if (p->ftsn_entries.ftsn_sid == sid) {
1329 		if (SSN_GT(ssn, p->ftsn_entries.ftsn_ssn))
1330 			p->ftsn_entries.ftsn_ssn = ssn;
1331 	} else {
1332 		if ((*slen + sizeof (uint32_t)) > fp->sfa_pmss)
1333 			return (B_FALSE);
1334 		p->next = kmem_cache_alloc(sctp_kmem_ftsn_set_cache,
1335 		    KM_NOSLEEP);
1336 		if (p->next == NULL)
1337 			return (B_FALSE);
1338 		p = p->next;
1339 		p->ftsn_entries.ftsn_sid = sid;
1340 		p->ftsn_entries.ftsn_ssn = ssn;
1341 		p->next = NULL;
1342 		(*nsets)++;
1343 		*slen += sizeof (uint32_t);
1344 	}
1345 	return (B_TRUE);
1346 }
1347 
1348 /*
1349  * Given a set of stream id - sequence number pairs, this routing creates
1350  * a Forward TSN chunk. The cumulative TSN (advanced peer ack point)
1351  * for the chunk is obtained from sctp->sctp_adv_pap. The caller
1352  * will add the IP/SCTP header.
1353  */
1354 mblk_t *
1355 sctp_make_ftsn_chunk(sctp_t *sctp, sctp_faddr_t *fp, sctp_ftsn_set_t *sets,
1356     uint_t nsets, uint32_t seglen)
1357 {
1358 	mblk_t			*ftsn_mp;
1359 	sctp_chunk_hdr_t	*ch_hdr;
1360 	uint32_t		*advtsn;
1361 	uint16_t		schlen;
1362 	size_t			xtralen;
1363 	ftsn_entry_t		*ftsn_entry;
1364 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1365 
1366 	seglen += sizeof (sctp_chunk_hdr_t);
1367 	if (fp->isv4)
1368 		xtralen = sctp->sctp_hdr_len + sctps->sctps_wroff_xtra;
1369 	else
1370 		xtralen = sctp->sctp_hdr6_len + sctps->sctps_wroff_xtra;
1371 	ftsn_mp = allocb_cred(xtralen + seglen, CONN_CRED(sctp->sctp_connp));
1372 	if (ftsn_mp == NULL)
1373 		return (NULL);
1374 	ftsn_mp->b_rptr += xtralen;
1375 	ftsn_mp->b_wptr = ftsn_mp->b_rptr + seglen;
1376 
1377 	ch_hdr = (sctp_chunk_hdr_t *)ftsn_mp->b_rptr;
1378 	ch_hdr->sch_id = CHUNK_FORWARD_TSN;
1379 	ch_hdr->sch_flags = 0;
1380 	/*
1381 	 * The cast here should not be an issue since seglen is
1382 	 * the length of the Forward TSN chunk.
1383 	 */
1384 	schlen = (uint16_t)seglen;
1385 	U16_TO_ABE16(schlen, &(ch_hdr->sch_len));
1386 
1387 	advtsn = (uint32_t *)(ch_hdr + 1);
1388 	U32_TO_ABE32(sctp->sctp_adv_pap, advtsn);
1389 	ftsn_entry = (ftsn_entry_t *)(advtsn + 1);
1390 	while (nsets > 0) {
1391 		ASSERT((uchar_t *)&ftsn_entry[1] <= ftsn_mp->b_wptr);
1392 		ftsn_entry->ftsn_sid = sets->ftsn_entries.ftsn_sid;
1393 		ftsn_entry->ftsn_ssn = sets->ftsn_entries.ftsn_ssn;
1394 		ftsn_entry++;
1395 		sets = sets->next;
1396 		nsets--;
1397 	}
1398 	return (ftsn_mp);
1399 }
1400 
1401 /*
1402  * Given a starting message, the routine steps through all the
1403  * messages whose TSN is less than sctp->sctp_adv_pap and creates
1404  * ftsn sets. The ftsn sets is then used to create an Forward TSN
1405  * chunk. All the messages, that have chunks that are included in the
1406  * ftsn sets, are flagged abandonded. If a message is partially sent
1407  * and is deemed abandoned, all remaining unsent chunks are marked
1408  * abandoned and are deducted from sctp_unsent.
1409  */
1410 void
1411 sctp_make_ftsns(sctp_t *sctp, mblk_t *meta, mblk_t *mp, mblk_t **nmp,
1412     sctp_faddr_t *fp, uint32_t *seglen)
1413 {
1414 	mblk_t		*mp1 = mp;
1415 	mblk_t		*mp_head = mp;
1416 	mblk_t		*meta_head = meta;
1417 	mblk_t		*head;
1418 	sctp_ftsn_set_t	*sets = NULL;
1419 	uint_t		nsets = 0;
1420 	uint16_t	clen;
1421 	sctp_data_hdr_t	*sdc;
1422 	uint32_t	sacklen;
1423 	uint32_t	adv_pap = sctp->sctp_adv_pap;
1424 	uint32_t	unsent = 0;
1425 	boolean_t	ubit;
1426 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1427 
1428 	*seglen = sizeof (uint32_t);
1429 
1430 	sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
1431 	while (meta != NULL &&
1432 	    SEQ_GEQ(sctp->sctp_adv_pap, ntohl(sdc->sdh_tsn))) {
1433 		/*
1434 		 * Skip adding FTSN sets for un-ordered messages as they do
1435 		 * not have SSNs.
1436 		 */
1437 		ubit = SCTP_DATA_GET_UBIT(sdc);
1438 		if (!ubit &&
1439 		    !sctp_add_ftsn_set(&sets, fp, meta, &nsets, seglen)) {
1440 			meta = NULL;
1441 			sctp->sctp_adv_pap = adv_pap;
1442 			goto ftsn_done;
1443 		}
1444 		while (mp1 != NULL && SCTP_CHUNK_ISSENT(mp1)) {
1445 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
1446 			adv_pap = ntohl(sdc->sdh_tsn);
1447 			mp1 = mp1->b_next;
1448 		}
1449 		meta = meta->b_next;
1450 		if (meta != NULL) {
1451 			mp1 = meta->b_cont;
1452 			if (!SCTP_CHUNK_ISSENT(mp1))
1453 				break;
1454 			sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
1455 		}
1456 	}
1457 ftsn_done:
1458 	/*
1459 	 * Can't compare with sets == NULL, since we don't add any
1460 	 * sets for un-ordered messages.
1461 	 */
1462 	if (meta == meta_head)
1463 		return;
1464 	*nmp = sctp_make_ftsn_chunk(sctp, fp, sets, nsets, *seglen);
1465 	sctp_free_ftsn_set(sets);
1466 	if (*nmp == NULL)
1467 		return;
1468 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
1469 		sacklen = 0;
1470 	} else {
1471 		sacklen = sizeof (sctp_chunk_hdr_t) +
1472 		    sizeof (sctp_sack_chunk_t) +
1473 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
1474 		if (*seglen + sacklen > sctp->sctp_lastdata->sfa_pmss) {
1475 			/* piggybacked SACK doesn't fit */
1476 			sacklen = 0;
1477 		} else {
1478 			fp = sctp->sctp_lastdata;
1479 		}
1480 	}
1481 	head = sctp_add_proto_hdr(sctp, fp, *nmp, sacklen, NULL);
1482 	if (head == NULL) {
1483 		freemsg(*nmp);
1484 		*nmp = NULL;
1485 		SCTP_KSTAT(sctps, sctp_send_ftsn_failed);
1486 		return;
1487 	}
1488 	*seglen += sacklen;
1489 	*nmp = head;
1490 
1491 	/*
1492 	 * XXXNeed to optimise this, the reason it is done here is so
1493 	 * that we don't have to undo in case of failure.
1494 	 */
1495 	mp1 = mp_head;
1496 	sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
1497 	while (meta_head != NULL &&
1498 	    SEQ_GEQ(sctp->sctp_adv_pap, ntohl(sdc->sdh_tsn))) {
1499 		if (!SCTP_IS_MSG_ABANDONED(meta_head))
1500 			SCTP_MSG_SET_ABANDONED(meta_head);
1501 		while (mp1 != NULL && SCTP_CHUNK_ISSENT(mp1)) {
1502 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
1503 			if (!SCTP_CHUNK_ISACKED(mp1)) {
1504 				clen = ntohs(sdc->sdh_len) - sizeof (*sdc);
1505 				SCTP_CHUNK_SENT(sctp, mp1, sdc, fp, clen,
1506 				    meta_head);
1507 			}
1508 			mp1 = mp1->b_next;
1509 		}
1510 		while (mp1 != NULL) {
1511 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
1512 			if (!SCTP_CHUNK_ABANDONED(mp1)) {
1513 				ASSERT(!SCTP_CHUNK_ISSENT(mp1));
1514 				unsent += ntohs(sdc->sdh_len) - sizeof (*sdc);
1515 				SCTP_ABANDON_CHUNK(mp1);
1516 			}
1517 			mp1 = mp1->b_next;
1518 		}
1519 		meta_head = meta_head->b_next;
1520 		if (meta_head != NULL) {
1521 			mp1 = meta_head->b_cont;
1522 			if (!SCTP_CHUNK_ISSENT(mp1))
1523 				break;
1524 			sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
1525 		}
1526 	}
1527 	if (unsent > 0) {
1528 		ASSERT(sctp->sctp_unsent >= unsent);
1529 		sctp->sctp_unsent -= unsent;
1530 		/*
1531 		 * Update ULP the amount of queued data, which is
1532 		 * sent-unack'ed + unsent.
1533 		 */
1534 		if (!SCTP_IS_DETACHED(sctp)) {
1535 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
1536 			    sctp->sctp_unacked + sctp->sctp_unsent);
1537 		}
1538 	}
1539 }
1540 
1541 /*
1542  * This function steps through messages starting at meta and checks if
1543  * the message is abandoned. It stops when it hits an unsent chunk or
1544  * a message that has all its chunk acked. This is the only place
1545  * where the sctp_adv_pap is moved forward to indicated abandoned
1546  * messages.
1547  */
1548 void
1549 sctp_check_adv_ack_pt(sctp_t *sctp, mblk_t *meta, mblk_t *mp)
1550 {
1551 	uint32_t	tsn = sctp->sctp_adv_pap;
1552 	sctp_data_hdr_t	*sdc;
1553 	sctp_msg_hdr_t	*msg_hdr;
1554 
1555 	ASSERT(mp != NULL);
1556 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
1557 	ASSERT(SEQ_GT(ntohl(sdc->sdh_tsn), sctp->sctp_lastack_rxd));
1558 	msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
1559 	if (!SCTP_IS_MSG_ABANDONED(meta) &&
1560 	    !SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
1561 		return;
1562 	}
1563 	while (meta != NULL) {
1564 		while (mp != NULL && SCTP_CHUNK_ISSENT(mp)) {
1565 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
1566 			tsn = ntohl(sdc->sdh_tsn);
1567 			mp = mp->b_next;
1568 		}
1569 		if (mp != NULL)
1570 			break;
1571 		/*
1572 		 * We continue checking for successive messages only if there
1573 		 * is a chunk marked for retransmission. Else, we might
1574 		 * end up sending FTSN prematurely for chunks that have been
1575 		 * sent, but not yet acked.
1576 		 */
1577 		if ((meta = meta->b_next) != NULL) {
1578 			msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
1579 			if (!SCTP_IS_MSG_ABANDONED(meta) &&
1580 			    !SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
1581 				break;
1582 			}
1583 			for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
1584 				if (!SCTP_CHUNK_ISSENT(mp)) {
1585 					sctp->sctp_adv_pap = tsn;
1586 					return;
1587 				}
1588 				if (SCTP_CHUNK_WANT_REXMIT(mp))
1589 					break;
1590 			}
1591 			if (mp == NULL)
1592 				break;
1593 		}
1594 	}
1595 	sctp->sctp_adv_pap = tsn;
1596 }
1597 
1598 
1599 /*
1600  * Determine if we should bundle a data chunk with the chunk being
1601  * retransmitted.  We bundle if
1602  *
1603  * - the chunk is sent to the same destination and unack'ed.
1604  *
1605  * OR
1606  *
1607  * - the chunk is unsent, i.e. new data.
1608  */
1609 #define	SCTP_CHUNK_RX_CANBUNDLE(mp, fp)					\
1610 	(!SCTP_CHUNK_ABANDONED((mp)) && 				\
1611 	((SCTP_CHUNK_ISSENT((mp)) && (SCTP_CHUNK_DEST(mp) == (fp) &&	\
1612 	!SCTP_CHUNK_ISACKED(mp))) ||					\
1613 	(((mp)->b_flag & (SCTP_CHUNK_FLAG_REXMIT|SCTP_CHUNK_FLAG_SENT)) != \
1614 	SCTP_CHUNK_FLAG_SENT)))
1615 
1616 /*
1617  * Retransmit first segment which hasn't been acked with cumtsn or send
1618  * a Forward TSN chunk, if appropriate.
1619  */
1620 void
1621 sctp_rexmit(sctp_t *sctp, sctp_faddr_t *oldfp)
1622 {
1623 	mblk_t		*mp;
1624 	mblk_t		*nmp = NULL;
1625 	mblk_t		*head;
1626 	mblk_t		*meta = sctp->sctp_xmit_head;
1627 	mblk_t		*fill;
1628 	uint32_t	seglen = 0;
1629 	uint32_t	sacklen;
1630 	uint16_t	chunklen;
1631 	int		extra;
1632 	sctp_data_hdr_t	*sdc;
1633 	sctp_faddr_t	*fp;
1634 	uint32_t	adv_pap = sctp->sctp_adv_pap;
1635 	boolean_t	do_ftsn = B_FALSE;
1636 	boolean_t	ftsn_check = B_TRUE;
1637 	uint32_t	first_ua_tsn;
1638 	sctp_msg_hdr_t	*mhdr;
1639 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1640 
1641 	while (meta != NULL) {
1642 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
1643 			uint32_t	tsn;
1644 
1645 			if (!SCTP_CHUNK_ISSENT(mp))
1646 				goto window_probe;
1647 			/*
1648 			 * We break in the following cases -
1649 			 *
1650 			 *	if the advanced peer ack point includes the next
1651 			 *	chunk to be retransmited - possibly the Forward
1652 			 * 	TSN was lost.
1653 			 *
1654 			 *	if we are PRSCTP aware and the next chunk to be
1655 			 *	retransmitted is now abandoned
1656 			 *
1657 			 *	if the next chunk to be retransmitted is for
1658 			 *	the dest on which the timer went off. (this
1659 			 *	message is not abandoned).
1660 			 *
1661 			 * We check for Forward TSN only for the first
1662 			 * eligible chunk to be retransmitted. The reason
1663 			 * being if the first eligible chunk is skipped (say
1664 			 * it was sent to a destination other than oldfp)
1665 			 * then we cannot advance the cum TSN via Forward
1666 			 * TSN chunk.
1667 			 *
1668 			 * Also, ftsn_check is B_TRUE only for the first
1669 			 * eligible chunk, it  will be B_FALSE for all
1670 			 * subsequent candidate messages for retransmission.
1671 			 */
1672 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
1673 			tsn = ntohl(sdc->sdh_tsn);
1674 			if (SEQ_GT(tsn, sctp->sctp_lastack_rxd)) {
1675 				if (sctp->sctp_prsctp_aware && ftsn_check) {
1676 					if (SEQ_GEQ(sctp->sctp_adv_pap, tsn)) {
1677 						ASSERT(sctp->sctp_prsctp_aware);
1678 						do_ftsn = B_TRUE;
1679 						goto out;
1680 					} else {
1681 						sctp_check_adv_ack_pt(sctp,
1682 						    meta, mp);
1683 						if (SEQ_GT(sctp->sctp_adv_pap,
1684 						    adv_pap)) {
1685 							do_ftsn = B_TRUE;
1686 							goto out;
1687 						}
1688 					}
1689 					ftsn_check = B_FALSE;
1690 				}
1691 				if (SCTP_CHUNK_DEST(mp) == oldfp)
1692 					goto out;
1693 			}
1694 		}
1695 		meta = meta->b_next;
1696 		if (meta != NULL && sctp->sctp_prsctp_aware) {
1697 			mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
1698 
1699 			while (meta != NULL && (SCTP_IS_MSG_ABANDONED(meta) ||
1700 			    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr, sctp))) {
1701 				meta = meta->b_next;
1702 			}
1703 		}
1704 	}
1705 window_probe:
1706 	/*
1707 	 * Retransmit fired for a destination which didn't have
1708 	 * any unacked data pending.
1709 	 */
1710 	if (sctp->sctp_unacked == 0 && sctp->sctp_unsent != 0) {
1711 		/*
1712 		 * Send a window probe. Inflate frwnd to allow
1713 		 * sending one segment.
1714 		 */
1715 		if (sctp->sctp_frwnd < (oldfp->sfa_pmss - sizeof (*sdc)))
1716 			sctp->sctp_frwnd = oldfp->sfa_pmss - sizeof (*sdc);
1717 
1718 		/* next TSN to send */
1719 		sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn;
1720 
1721 		/*
1722 		 * The above sctp_frwnd adjustment is coarse.  The "changed"
1723 		 * sctp_frwnd may allow us to send more than 1 packet.  So
1724 		 * tell sctp_output() to send only 1 packet.
1725 		 */
1726 		sctp_output(sctp, 1);
1727 
1728 		/* Last sent TSN */
1729 		sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn - 1;
1730 		ASSERT(sctp->sctp_rxt_maxtsn >= sctp->sctp_rxt_nxttsn);
1731 		sctp->sctp_zero_win_probe = B_TRUE;
1732 		BUMP_MIB(&sctps->sctps_mib, sctpOutWinProbe);
1733 	}
1734 	return;
1735 out:
1736 	/*
1737 	 * After a time out, assume that everything has left the network.  So
1738 	 * we can clear rxt_unacked for the original peer address.
1739 	 */
1740 	oldfp->rxt_unacked = 0;
1741 
1742 	/*
1743 	 * If we were probing for zero window, don't adjust retransmission
1744 	 * variables, but the timer is still backed off.
1745 	 */
1746 	if (sctp->sctp_zero_win_probe) {
1747 		mblk_t	*pkt;
1748 		uint_t	pkt_len;
1749 
1750 		/*
1751 		 * Get the Zero Win Probe for retrasmission, sctp_rxt_nxttsn
1752 		 * and sctp_rxt_maxtsn will specify the ZWP packet.
1753 		 */
1754 		fp = oldfp;
1755 		if (oldfp->state != SCTP_FADDRS_ALIVE)
1756 			fp = sctp_rotate_faddr(sctp, oldfp);
1757 		pkt = sctp_rexmit_packet(sctp, &meta, &mp, fp, &pkt_len);
1758 		if (pkt != NULL) {
1759 			ASSERT(pkt_len <= fp->sfa_pmss);
1760 			sctp_set_iplen(sctp, pkt);
1761 			sctp_add_sendq(sctp, pkt);
1762 		} else {
1763 			SCTP_KSTAT(sctps, sctp_ss_rexmit_failed);
1764 		}
1765 
1766 		/*
1767 		 * The strikes will be clear by sctp_faddr_alive() when the
1768 		 * other side sends us an ack.
1769 		 */
1770 		oldfp->strikes++;
1771 		sctp->sctp_strikes++;
1772 
1773 		SCTP_CALC_RXT(oldfp, sctp->sctp_rto_max);
1774 		if (oldfp != fp && oldfp->suna != 0)
1775 			SCTP_FADDR_TIMER_RESTART(sctp, oldfp, fp->rto);
1776 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1777 		BUMP_MIB(&sctps->sctps_mib, sctpOutWinProbe);
1778 		return;
1779 	}
1780 
1781 	/*
1782 	 * Enter slowstart for this destination
1783 	 */
1784 	oldfp->ssthresh = oldfp->cwnd / 2;
1785 	if (oldfp->ssthresh < 2 * oldfp->sfa_pmss)
1786 		oldfp->ssthresh = 2 * oldfp->sfa_pmss;
1787 	oldfp->cwnd = oldfp->sfa_pmss;
1788 	oldfp->pba = 0;
1789 	fp = sctp_rotate_faddr(sctp, oldfp);
1790 	ASSERT(fp != NULL);
1791 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
1792 
1793 	first_ua_tsn = ntohl(sdc->sdh_tsn);
1794 	if (do_ftsn) {
1795 		sctp_make_ftsns(sctp, meta, mp, &nmp, fp, &seglen);
1796 		if (nmp == NULL) {
1797 			sctp->sctp_adv_pap = adv_pap;
1798 			goto restart_timer;
1799 		}
1800 		head = nmp;
1801 		/*
1802 		 * Move to the next unabandoned chunk. XXXCheck if meta will
1803 		 * always be marked abandoned.
1804 		 */
1805 		while (meta != NULL && SCTP_IS_MSG_ABANDONED(meta))
1806 			meta = meta->b_next;
1807 		if (meta != NULL)
1808 			mp = mp->b_cont;
1809 		else
1810 			mp = NULL;
1811 		goto try_bundle;
1812 	}
1813 	seglen = ntohs(sdc->sdh_len);
1814 	chunklen = seglen - sizeof (*sdc);
1815 	if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
1816 		extra = SCTP_ALIGN - extra;
1817 
1818 	/* Find out if we need to piggyback SACK. */
1819 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
1820 		sacklen = 0;
1821 	} else {
1822 		sacklen = sizeof (sctp_chunk_hdr_t) +
1823 		    sizeof (sctp_sack_chunk_t) +
1824 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
1825 		if (seglen + sacklen > sctp->sctp_lastdata->sfa_pmss) {
1826 			/* piggybacked SACK doesn't fit */
1827 			sacklen = 0;
1828 		} else {
1829 			/*
1830 			 * OK, we have room to send SACK back.  But we
1831 			 * should send it back to the last fp where we
1832 			 * receive data from, unless sctp_lastdata equals
1833 			 * oldfp, then we should probably not send it
1834 			 * back to that fp.  Also we should check that
1835 			 * the fp is alive.
1836 			 */
1837 			if (sctp->sctp_lastdata != oldfp &&
1838 			    sctp->sctp_lastdata->state == SCTP_FADDRS_ALIVE) {
1839 				fp = sctp->sctp_lastdata;
1840 			}
1841 		}
1842 	}
1843 
1844 	/*
1845 	 * Cancel RTT measurement if the retransmitted TSN is before the
1846 	 * TSN used for timimg.
1847 	 */
1848 	if (sctp->sctp_out_time != 0 &&
1849 	    SEQ_GEQ(sctp->sctp_rtt_tsn, sdc->sdh_tsn)) {
1850 		sctp->sctp_out_time = 0;
1851 	}
1852 	/* Clear the counter as the RTT calculation may be off. */
1853 	fp->rtt_updates = 0;
1854 	oldfp->rtt_updates = 0;
1855 
1856 	/*
1857 	 * After a timeout, we should change the current faddr so that
1858 	 * new chunks will be sent to the alternate address.
1859 	 */
1860 	sctp_set_faddr_current(sctp, fp);
1861 
1862 	nmp = dupmsg(mp);
1863 	if (nmp == NULL)
1864 		goto restart_timer;
1865 	if (extra > 0) {
1866 		fill = sctp_get_padding(sctp, extra);
1867 		if (fill != NULL) {
1868 			linkb(nmp, fill);
1869 			seglen += extra;
1870 		} else {
1871 			freemsg(nmp);
1872 			goto restart_timer;
1873 		}
1874 	}
1875 	SCTP_CHUNK_CLEAR_FLAGS(nmp);
1876 	head = sctp_add_proto_hdr(sctp, fp, nmp, sacklen, NULL);
1877 	if (head == NULL) {
1878 		freemsg(nmp);
1879 		SCTP_KSTAT(sctps, sctp_rexmit_failed);
1880 		goto restart_timer;
1881 	}
1882 	seglen += sacklen;
1883 
1884 	SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
1885 
1886 	mp = mp->b_next;
1887 
1888 try_bundle:
1889 	/* We can at least and at most send 1 packet at timeout. */
1890 	while (seglen < fp->sfa_pmss) {
1891 		int32_t new_len;
1892 
1893 		/* Go through the list to find more chunks to be bundled. */
1894 		while (mp != NULL) {
1895 			/* Check if the chunk can be bundled. */
1896 			if (SCTP_CHUNK_RX_CANBUNDLE(mp, oldfp))
1897 				break;
1898 			mp = mp->b_next;
1899 		}
1900 		/* Go to the next message. */
1901 		if (mp == NULL) {
1902 			for (meta = meta->b_next; meta != NULL;
1903 			    meta = meta->b_next) {
1904 				mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
1905 
1906 				if (SCTP_IS_MSG_ABANDONED(meta) ||
1907 				    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr,
1908 				    sctp)) {
1909 					continue;
1910 				}
1911 
1912 				mp = meta->b_cont;
1913 				goto try_bundle;
1914 			}
1915 			/* No more chunk to be bundled. */
1916 			break;
1917 		}
1918 
1919 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
1920 		new_len = ntohs(sdc->sdh_len);
1921 		chunklen = new_len - sizeof (*sdc);
1922 
1923 		if ((extra = new_len & (SCTP_ALIGN - 1)) != 0)
1924 			extra = SCTP_ALIGN - extra;
1925 		if ((new_len = seglen + new_len + extra) > fp->sfa_pmss)
1926 			break;
1927 		if ((nmp = dupmsg(mp)) == NULL)
1928 			break;
1929 
1930 		if (extra > 0) {
1931 			fill = sctp_get_padding(sctp, extra);
1932 			if (fill != NULL) {
1933 				linkb(nmp, fill);
1934 			} else {
1935 				freemsg(nmp);
1936 				break;
1937 			}
1938 		}
1939 		linkb(head, nmp);
1940 
1941 		SCTP_CHUNK_CLEAR_FLAGS(nmp);
1942 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
1943 
1944 		seglen = new_len;
1945 		mp = mp->b_next;
1946 	}
1947 done_bundle:
1948 	if ((seglen > fp->sfa_pmss) && fp->isv4) {
1949 		ipha_t *iph = (ipha_t *)head->b_rptr;
1950 
1951 		/*
1952 		 * Path MTU is different from path we thought it would
1953 		 * be when we created chunks, or IP headers have grown.
1954 		 * Need to clear the DF bit.
1955 		 */
1956 		iph->ipha_fragment_offset_and_flags = 0;
1957 	}
1958 	fp->rxt_unacked += seglen;
1959 
1960 	dprint(2, ("sctp_rexmit: Sending packet %d bytes, tsn %x "
1961 	    "ssn %d to %p (rwnd %d, lastack_rxd %x)\n",
1962 	    seglen, ntohl(sdc->sdh_tsn), ntohs(sdc->sdh_ssn),
1963 	    (void *)fp, sctp->sctp_frwnd, sctp->sctp_lastack_rxd));
1964 
1965 	sctp->sctp_rexmitting = B_TRUE;
1966 	sctp->sctp_rxt_nxttsn = first_ua_tsn;
1967 	sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn - 1;
1968 	sctp_set_iplen(sctp, head);
1969 	sctp_add_sendq(sctp, head);
1970 
1971 	/*
1972 	 * Restart the oldfp timer with exponential backoff and
1973 	 * the new fp timer for the retransmitted chunks.
1974 	 */
1975 restart_timer:
1976 	oldfp->strikes++;
1977 	sctp->sctp_strikes++;
1978 	SCTP_CALC_RXT(oldfp, sctp->sctp_rto_max);
1979 	/*
1980 	 * If there is still some data in the oldfp, restart the
1981 	 * retransmission timer.  If there is no data, the heartbeat will
1982 	 * continue to run so it will do its job in checking the reachability
1983 	 * of the oldfp.
1984 	 */
1985 	if (oldfp != fp && oldfp->suna != 0)
1986 		SCTP_FADDR_TIMER_RESTART(sctp, oldfp, oldfp->rto);
1987 
1988 	/*
1989 	 * Should we restart the timer of the new fp?  If there is
1990 	 * outstanding data to the new fp, the timer should be
1991 	 * running already.  So restarting it means that the timer
1992 	 * will fire later for those outstanding data.  But if
1993 	 * we don't restart it, the timer will fire too early for the
1994 	 * just retransmitted chunks to the new fp.  The reason is that we
1995 	 * don't keep a timestamp on when a chunk is retransmitted.
1996 	 * So when the timer fires, it will just search for the
1997 	 * chunk with the earliest TSN sent to new fp.  This probably
1998 	 * is the chunk we just retransmitted.  So for now, let's
1999 	 * be conservative and restart the timer of the new fp.
2000 	 */
2001 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
2002 
2003 	sctp->sctp_active = lbolt64;
2004 }
2005 
2006 /*
2007  * This function is called by sctp_ss_rexmit() to create a packet
2008  * to be retransmitted to the given fp.  The given meta and mp
2009  * parameters are respectively the sctp_msg_hdr_t and the mblk of the
2010  * first chunk to be retransmitted.  This is also called when we want
2011  * to retransmit a zero window probe from sctp_rexmit() or when we
2012  * want to retransmit the zero window probe after the window has
2013  * opened from sctp_got_sack().
2014  */
2015 mblk_t *
2016 sctp_rexmit_packet(sctp_t *sctp, mblk_t **meta, mblk_t **mp, sctp_faddr_t *fp,
2017     uint_t *packet_len)
2018 {
2019 	uint32_t	seglen = 0;
2020 	uint16_t	chunklen;
2021 	int		extra;
2022 	mblk_t		*nmp;
2023 	mblk_t		*head;
2024 	mblk_t		*fill;
2025 	sctp_data_hdr_t	*sdc;
2026 	sctp_msg_hdr_t	*mhdr;
2027 
2028 	sdc = (sctp_data_hdr_t *)(*mp)->b_rptr;
2029 	seglen = ntohs(sdc->sdh_len);
2030 	chunklen = seglen - sizeof (*sdc);
2031 	if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
2032 		extra = SCTP_ALIGN - extra;
2033 
2034 	nmp = dupmsg(*mp);
2035 	if (nmp == NULL)
2036 		return (NULL);
2037 	if (extra > 0) {
2038 		fill = sctp_get_padding(sctp, extra);
2039 		if (fill != NULL) {
2040 			linkb(nmp, fill);
2041 			seglen += extra;
2042 		} else {
2043 			freemsg(nmp);
2044 			return (NULL);
2045 		}
2046 	}
2047 	SCTP_CHUNK_CLEAR_FLAGS(nmp);
2048 	head = sctp_add_proto_hdr(sctp, fp, nmp, 0, NULL);
2049 	if (head == NULL) {
2050 		freemsg(nmp);
2051 		return (NULL);
2052 	}
2053 	SCTP_CHUNK_SENT(sctp, *mp, sdc, fp, chunklen, *meta);
2054 	/*
2055 	 * Don't update the TSN if we are doing a Zero Win Probe.
2056 	 */
2057 	if (!sctp->sctp_zero_win_probe)
2058 		sctp->sctp_rxt_nxttsn = ntohl(sdc->sdh_tsn);
2059 	*mp = (*mp)->b_next;
2060 
2061 try_bundle:
2062 	while (seglen < fp->sfa_pmss) {
2063 		int32_t new_len;
2064 
2065 		/*
2066 		 * Go through the list to find more chunks to be bundled.
2067 		 * We should only retransmit sent by unack'ed chunks.  Since
2068 		 * they were sent before, the peer's receive window should
2069 		 * be able to receive them.
2070 		 */
2071 		while (*mp != NULL) {
2072 			/* Check if the chunk can be bundled. */
2073 			if (SCTP_CHUNK_ISSENT(*mp) && !SCTP_CHUNK_ISACKED(*mp))
2074 				break;
2075 			*mp = (*mp)->b_next;
2076 		}
2077 		/* Go to the next message. */
2078 		if (*mp == NULL) {
2079 			for (*meta = (*meta)->b_next; *meta != NULL;
2080 			    *meta = (*meta)->b_next) {
2081 				mhdr = (sctp_msg_hdr_t *)(*meta)->b_rptr;
2082 
2083 				if (SCTP_IS_MSG_ABANDONED(*meta) ||
2084 				    SCTP_MSG_TO_BE_ABANDONED(*meta, mhdr,
2085 				    sctp)) {
2086 					continue;
2087 				}
2088 
2089 				*mp = (*meta)->b_cont;
2090 				goto try_bundle;
2091 			}
2092 			/* No more chunk to be bundled. */
2093 			break;
2094 		}
2095 
2096 		sdc = (sctp_data_hdr_t *)(*mp)->b_rptr;
2097 		/* Don't bundle chunks beyond sctp_rxt_maxtsn. */
2098 		if (SEQ_GT(ntohl(sdc->sdh_tsn), sctp->sctp_rxt_maxtsn))
2099 			break;
2100 		new_len = ntohs(sdc->sdh_len);
2101 		chunklen = new_len - sizeof (*sdc);
2102 
2103 		if ((extra = new_len & (SCTP_ALIGN - 1)) != 0)
2104 			extra = SCTP_ALIGN - extra;
2105 		if ((new_len = seglen + new_len + extra) > fp->sfa_pmss)
2106 			break;
2107 		if ((nmp = dupmsg(*mp)) == NULL)
2108 			break;
2109 
2110 		if (extra > 0) {
2111 			fill = sctp_get_padding(sctp, extra);
2112 			if (fill != NULL) {
2113 				linkb(nmp, fill);
2114 			} else {
2115 				freemsg(nmp);
2116 				break;
2117 			}
2118 		}
2119 		linkb(head, nmp);
2120 
2121 		SCTP_CHUNK_CLEAR_FLAGS(nmp);
2122 		SCTP_CHUNK_SENT(sctp, *mp, sdc, fp, chunklen, *meta);
2123 		/*
2124 		 * Don't update the TSN if we are doing a Zero Win Probe.
2125 		 */
2126 		if (!sctp->sctp_zero_win_probe)
2127 			sctp->sctp_rxt_nxttsn = ntohl(sdc->sdh_tsn);
2128 
2129 		seglen = new_len;
2130 		*mp = (*mp)->b_next;
2131 	}
2132 	*packet_len = seglen;
2133 	fp->rxt_unacked += seglen;
2134 	return (head);
2135 }
2136 
2137 /*
2138  * sctp_ss_rexmit() is called when we get a SACK after a timeout which
2139  * advances the cum_tsn but the cum_tsn is still less than what we have sent
2140  * (sctp_rxt_maxtsn) at the time of the timeout.  This SACK is a "partial"
2141  * SACK.  We retransmit unacked chunks without having to wait for another
2142  * timeout.  The rationale is that the SACK should not be "partial" if all the
2143  * lost chunks have been retransmitted.  Since the SACK is "partial,"
2144  * the chunks between the cum_tsn and the sctp_rxt_maxtsn should still
2145  * be missing.  It is better for us to retransmit them now instead
2146  * of waiting for a timeout.
2147  */
2148 void
2149 sctp_ss_rexmit(sctp_t *sctp)
2150 {
2151 	mblk_t		*meta;
2152 	mblk_t		*mp;
2153 	mblk_t		*pkt;
2154 	sctp_faddr_t	*fp;
2155 	uint_t		pkt_len;
2156 	uint32_t	tot_wnd;
2157 	sctp_data_hdr_t	*sdc;
2158 	int		burst;
2159 	sctp_stack_t	*sctps = sctp->sctp_sctps;
2160 
2161 	ASSERT(!sctp->sctp_zero_win_probe);
2162 
2163 	/*
2164 	 * If the last cum ack is smaller than what we have just
2165 	 * retransmitted, simply return.
2166 	 */
2167 	if (SEQ_GEQ(sctp->sctp_lastack_rxd, sctp->sctp_rxt_nxttsn))
2168 		sctp->sctp_rxt_nxttsn = sctp->sctp_lastack_rxd + 1;
2169 	else
2170 		return;
2171 	ASSERT(SEQ_LEQ(sctp->sctp_rxt_nxttsn, sctp->sctp_rxt_maxtsn));
2172 
2173 	/*
2174 	 * After a timer fires, sctp_current should be set to the new
2175 	 * fp where the retransmitted chunks are sent.
2176 	 */
2177 	fp = sctp->sctp_current;
2178 
2179 	/*
2180 	 * Since we are retransmitting, we only need to use cwnd to determine
2181 	 * how much we can send as we were allowed (by peer's receive window)
2182 	 * to send those retransmitted chunks previously when they are first
2183 	 * sent.  If we record how much we have retransmitted but
2184 	 * unacknowledged using rxt_unacked, then the amount we can now send
2185 	 * is equal to cwnd minus rxt_unacked.
2186 	 *
2187 	 * The field rxt_unacked is incremented when we retransmit a packet
2188 	 * and decremented when we got a SACK acknowledging something.  And
2189 	 * it is reset when the retransmission timer fires as we assume that
2190 	 * all packets have left the network after a timeout.  If this
2191 	 * assumption is not true, it means that after a timeout, we can
2192 	 * get a SACK acknowledging more than rxt_unacked (its value only
2193 	 * contains what is retransmitted when the timer fires).  So
2194 	 * rxt_unacked will become very big (it is an unsiged int so going
2195 	 * negative means that the value is huge).  This is the reason we
2196 	 * always send at least 1 MSS bytes.
2197 	 *
2198 	 * The reason why we do not have an accurate count is that we
2199 	 * only know how many packets are outstanding (using the TSN numbers).
2200 	 * But we do not know how many bytes those packets contain.  To
2201 	 * have an accurate count, we need to walk through the send list.
2202 	 * As it is not really important to have an accurate count during
2203 	 * retransmission, we skip this walk to save some time.  This should
2204 	 * not make the retransmission too aggressive to cause congestion.
2205 	 */
2206 	if (fp->cwnd <= fp->rxt_unacked)
2207 		tot_wnd = fp->sfa_pmss;
2208 	else
2209 		tot_wnd = fp->cwnd - fp->rxt_unacked;
2210 
2211 	/* Find the first unack'ed chunk */
2212 	for (meta = sctp->sctp_xmit_head; meta != NULL; meta = meta->b_next) {
2213 		sctp_msg_hdr_t	*mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
2214 
2215 		if (SCTP_IS_MSG_ABANDONED(meta) ||
2216 		    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr, sctp)) {
2217 			continue;
2218 		}
2219 
2220 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
2221 			/* Again, this may not be possible */
2222 			if (!SCTP_CHUNK_ISSENT(mp))
2223 				return;
2224 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
2225 			if (ntohl(sdc->sdh_tsn) == sctp->sctp_rxt_nxttsn)
2226 				goto found_msg;
2227 		}
2228 	}
2229 
2230 	/* Everything is abandoned... */
2231 	return;
2232 
2233 found_msg:
2234 	if (!fp->timer_running)
2235 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
2236 	pkt = sctp_rexmit_packet(sctp, &meta, &mp, fp, &pkt_len);
2237 	if (pkt == NULL) {
2238 		SCTP_KSTAT(sctps, sctp_ss_rexmit_failed);
2239 		return;
2240 	}
2241 	if ((pkt_len > fp->sfa_pmss) && fp->isv4) {
2242 		ipha_t	*iph = (ipha_t *)pkt->b_rptr;
2243 
2244 		/*
2245 		 * Path MTU is different from path we thought it would
2246 		 * be when we created chunks, or IP headers have grown.
2247 		 *  Need to clear the DF bit.
2248 		 */
2249 		iph->ipha_fragment_offset_and_flags = 0;
2250 	}
2251 	sctp_set_iplen(sctp, pkt);
2252 	sctp_add_sendq(sctp, pkt);
2253 
2254 	/* Check and see if there is more chunk to be retransmitted. */
2255 	if (tot_wnd <= pkt_len || tot_wnd - pkt_len < fp->sfa_pmss ||
2256 	    meta == NULL)
2257 		return;
2258 	if (mp == NULL)
2259 		meta = meta->b_next;
2260 	if (meta == NULL)
2261 		return;
2262 
2263 	/* Retransmit another packet if the window allows. */
2264 	for (tot_wnd -= pkt_len, burst = sctps->sctps_maxburst - 1;
2265 	    meta != NULL && burst > 0; meta = meta->b_next, burst--) {
2266 		if (mp == NULL)
2267 			mp = meta->b_cont;
2268 		for (; mp != NULL; mp = mp->b_next) {
2269 			/* Again, this may not be possible */
2270 			if (!SCTP_CHUNK_ISSENT(mp))
2271 				return;
2272 			if (!SCTP_CHUNK_ISACKED(mp))
2273 				goto found_msg;
2274 		}
2275 	}
2276 }
2277