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