10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211735Skcpoon 
220Sstevel@tonic-gate /*
237480SKacheong.Poon@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/stream.h>
300Sstevel@tonic-gate #include <sys/cmn_err.h>
310Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
320Sstevel@tonic-gate #include <sys/tihdr.h>
330Sstevel@tonic-gate #include <sys/socket.h>
340Sstevel@tonic-gate #include <sys/stropts.h>
350Sstevel@tonic-gate #include <sys/strsun.h>
360Sstevel@tonic-gate #include <sys/strsubr.h>
370Sstevel@tonic-gate #include <sys/socketvar.h>
380Sstevel@tonic-gate #include <inet/common.h>
390Sstevel@tonic-gate #include <inet/mi.h>
400Sstevel@tonic-gate #include <inet/ip.h>
410Sstevel@tonic-gate #include <inet/ip6.h>
420Sstevel@tonic-gate #include <inet/sctp_ip.h>
430Sstevel@tonic-gate #include <inet/ipclassifier.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * PR-SCTP comments.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * A message can expire before it gets to the transmit list (i.e. it is still
490Sstevel@tonic-gate  * in the unsent list - unchunked), after it gets to the transmit list, but
500Sstevel@tonic-gate  * before transmission has actually started, or after transmission has begun.
510Sstevel@tonic-gate  * Accordingly, we check for the status of a message in sctp_chunkify() when
520Sstevel@tonic-gate  * the message is being transferred from the unsent list to the transmit list;
530Sstevel@tonic-gate  * in sctp_get_msg_to_send(), when we get the next chunk from the transmit
540Sstevel@tonic-gate  * list and in sctp_rexmit() when we get the next chunk to be (re)transmitted.
550Sstevel@tonic-gate  * When we nuke a message in sctp_chunkify(), all we need to do is take it
560Sstevel@tonic-gate  * out of the unsent list and update sctp_unsent; when a message is deemed
570Sstevel@tonic-gate  * timed-out in sctp_get_msg_to_send() we can just take it out of the transmit
580Sstevel@tonic-gate  * list, update sctp_unsent IFF transmission for the message has not yet begun
590Sstevel@tonic-gate  * (i.e. !SCTP_CHUNK_ISSENT(meta->b_cont)). However, if transmission for the
600Sstevel@tonic-gate  * message has started, then we cannot just take it out of the list, we need
610Sstevel@tonic-gate  * to send Forward TSN chunk to the peer so that the peer can clear its
620Sstevel@tonic-gate  * fragment list for this message. However, we cannot just send the Forward
630Sstevel@tonic-gate  * TSN in sctp_get_msg_to_send() because there might be unacked chunks for
640Sstevel@tonic-gate  * messages preceeding this abandoned message. So, we send a Forward TSN
650Sstevel@tonic-gate  * IFF all messages prior to this abandoned message has been SACKd, if not
660Sstevel@tonic-gate  * we defer sending the Forward TSN to sctp_cumack(), which will check for
670Sstevel@tonic-gate  * this condition and send the Forward TSN via sctp_check_abandoned_msg(). In
680Sstevel@tonic-gate  * sctp_rexmit() when we check for retransmissions, we need to determine if
690Sstevel@tonic-gate  * the advanced peer ack point can be moved ahead, and if so, send a Forward
700Sstevel@tonic-gate  * TSN to the peer instead of retransmitting the chunk. Note that when
710Sstevel@tonic-gate  * we send a Forward TSN for a message, there may be yet unsent chunks for
720Sstevel@tonic-gate  * this message; we need to mark all such chunks as abandoned, so that
730Sstevel@tonic-gate  * sctp_cumack() can take the message out of the transmit list, additionally
740Sstevel@tonic-gate  * sctp_unsent need to be adjusted. Whenever sctp_unsent is updated (i.e.
750Sstevel@tonic-gate  * decremented when a message/chunk is deemed abandoned), sockfs needs to
760Sstevel@tonic-gate  * be notified so that it can adjust its idea of the queued message.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #include "sctp_impl.h"
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static struct kmem_cache	*sctp_kmem_ftsn_set_cache;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #ifdef	DEBUG
840Sstevel@tonic-gate static boolean_t	sctp_verify_chain(mblk_t *, mblk_t *);
850Sstevel@tonic-gate #endif
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  * Called to allocate a header mblk when sending data to SCTP.
890Sstevel@tonic-gate  * Data will follow in b_cont of this mblk.
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate mblk_t *
920Sstevel@tonic-gate sctp_alloc_hdr(const char *name, int nlen, const char *control, int clen,
930Sstevel@tonic-gate     int flags)
940Sstevel@tonic-gate {
950Sstevel@tonic-gate 	mblk_t *mp;
960Sstevel@tonic-gate 	struct T_unitdata_req *tudr;
970Sstevel@tonic-gate 	size_t size;
980Sstevel@tonic-gate 	int error;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	size = sizeof (*tudr) + _TPI_ALIGN_TOPT(nlen) + clen;
1010Sstevel@tonic-gate 	size = MAX(size, sizeof (sctp_msg_hdr_t));
1020Sstevel@tonic-gate 	if (flags & SCTP_CAN_BLOCK) {
1030Sstevel@tonic-gate 		mp = allocb_wait(size, BPRI_MED, 0, &error);
1040Sstevel@tonic-gate 	} else {
1050Sstevel@tonic-gate 		mp = allocb(size, BPRI_MED);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 	if (mp) {
1080Sstevel@tonic-gate 		tudr = (struct T_unitdata_req *)mp->b_rptr;
1090Sstevel@tonic-gate 		tudr->PRIM_type = T_UNITDATA_REQ;
1100Sstevel@tonic-gate 		tudr->DEST_length = nlen;
1110Sstevel@tonic-gate 		tudr->DEST_offset = sizeof (*tudr);
1120Sstevel@tonic-gate 		tudr->OPT_length = clen;
1130Sstevel@tonic-gate 		tudr->OPT_offset = (t_scalar_t)(sizeof (*tudr) +
1140Sstevel@tonic-gate 		    _TPI_ALIGN_TOPT(nlen));
1150Sstevel@tonic-gate 		if (nlen > 0)
1160Sstevel@tonic-gate 			bcopy(name, tudr + 1, nlen);
1170Sstevel@tonic-gate 		if (clen > 0)
1180Sstevel@tonic-gate 			bcopy(control, (char *)tudr + tudr->OPT_offset, clen);
1190Sstevel@tonic-gate 		mp->b_wptr += (tudr ->OPT_offset + clen);
1200Sstevel@tonic-gate 		mp->b_datap->db_type = M_PROTO;
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 	return (mp);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*ARGSUSED2*/
1260Sstevel@tonic-gate int
1270Sstevel@tonic-gate sctp_sendmsg(sctp_t *sctp, mblk_t *mp, int flags)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	sctp_faddr_t	*fp = NULL;
1300Sstevel@tonic-gate 	struct T_unitdata_req	*tudr;
1310Sstevel@tonic-gate 	int		error = 0;
1320Sstevel@tonic-gate 	mblk_t		*mproto = mp;
1330Sstevel@tonic-gate 	in6_addr_t	*addr;
1340Sstevel@tonic-gate 	in6_addr_t	tmpaddr;
1350Sstevel@tonic-gate 	uint16_t	sid = sctp->sctp_def_stream;
1360Sstevel@tonic-gate 	uint32_t	ppid = sctp->sctp_def_ppid;
1370Sstevel@tonic-gate 	uint32_t	context = sctp->sctp_def_context;
1380Sstevel@tonic-gate 	uint16_t	msg_flags = sctp->sctp_def_flags;
1390Sstevel@tonic-gate 	sctp_msg_hdr_t	*sctp_msg_hdr;
1400Sstevel@tonic-gate 	uint32_t	msg_len = 0;
1410Sstevel@tonic-gate 	uint32_t	timetolive = sctp->sctp_def_timetolive;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	ASSERT(DB_TYPE(mproto) == M_PROTO);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	mp = mp->b_cont;
1460Sstevel@tonic-gate 	ASSERT(mp == NULL || DB_TYPE(mp) == M_DATA);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	tudr = (struct T_unitdata_req *)mproto->b_rptr;
1490Sstevel@tonic-gate 	ASSERT(tudr->PRIM_type == T_UNITDATA_REQ);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/* Get destination address, if specified */
1520Sstevel@tonic-gate 	if (tudr->DEST_length > 0) {
1530Sstevel@tonic-gate 		sin_t *sin;
1540Sstevel@tonic-gate 		sin6_t *sin6;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 		sin = (struct sockaddr_in *)
1570Sstevel@tonic-gate 		    (mproto->b_rptr + tudr->DEST_offset);
1580Sstevel@tonic-gate 		switch (sin->sin_family) {
1590Sstevel@tonic-gate 		case AF_INET:
1600Sstevel@tonic-gate 			if (tudr->DEST_length < sizeof (*sin)) {
1610Sstevel@tonic-gate 				return (EINVAL);
1620Sstevel@tonic-gate 			}
1630Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &tmpaddr);
1640Sstevel@tonic-gate 			addr = &tmpaddr;
1650Sstevel@tonic-gate 			break;
1660Sstevel@tonic-gate 		case AF_INET6:
1670Sstevel@tonic-gate 			if (tudr->DEST_length < sizeof (*sin6)) {
1680Sstevel@tonic-gate 				return (EINVAL);
1690Sstevel@tonic-gate 			}
1700Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)
1710Sstevel@tonic-gate 			    (mproto->b_rptr + tudr->DEST_offset);
1720Sstevel@tonic-gate 			addr = &sin6->sin6_addr;
1730Sstevel@tonic-gate 			break;
1740Sstevel@tonic-gate 		default:
1750Sstevel@tonic-gate 			return (EAFNOSUPPORT);
1760Sstevel@tonic-gate 		}
1770Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, addr);
1780Sstevel@tonic-gate 		if (fp == NULL) {
1790Sstevel@tonic-gate 			return (EINVAL);
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 	/* Ancillary Data? */
1830Sstevel@tonic-gate 	if (tudr->OPT_length > 0) {
1840Sstevel@tonic-gate 		struct cmsghdr		*cmsg;
1850Sstevel@tonic-gate 		char			*cend;
1860Sstevel@tonic-gate 		struct sctp_sndrcvinfo	*sndrcv;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)(mproto->b_rptr + tudr->OPT_offset);
1890Sstevel@tonic-gate 		cend = ((char *)cmsg + tudr->OPT_length);
1900Sstevel@tonic-gate 		ASSERT(cend <= (char *)mproto->b_wptr);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		for (;;) {
1930Sstevel@tonic-gate 			if ((char *)(cmsg + 1) > cend ||
1940Sstevel@tonic-gate 			    ((char *)cmsg + cmsg->cmsg_len) > cend) {
1950Sstevel@tonic-gate 				break;
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 			if ((cmsg->cmsg_level == IPPROTO_SCTP) &&
1980Sstevel@tonic-gate 			    (cmsg->cmsg_type == SCTP_SNDRCV)) {
1990Sstevel@tonic-gate 				if (cmsg->cmsg_len <
2000Sstevel@tonic-gate 				    (sizeof (*sndrcv) + sizeof (*cmsg))) {
2010Sstevel@tonic-gate 					return (EINVAL);
2020Sstevel@tonic-gate 				}
2030Sstevel@tonic-gate 				sndrcv = (struct sctp_sndrcvinfo *)(cmsg + 1);
2040Sstevel@tonic-gate 				sid = sndrcv->sinfo_stream;
2050Sstevel@tonic-gate 				msg_flags = sndrcv->sinfo_flags;
2060Sstevel@tonic-gate 				ppid = sndrcv->sinfo_ppid;
2070Sstevel@tonic-gate 				context = sndrcv->sinfo_context;
2080Sstevel@tonic-gate 				timetolive = sndrcv->sinfo_timetolive;
2090Sstevel@tonic-gate 				break;
2100Sstevel@tonic-gate 			}
2110Sstevel@tonic-gate 			if (cmsg->cmsg_len > 0)
2120Sstevel@tonic-gate 				cmsg = CMSG_NEXT(cmsg);
2130Sstevel@tonic-gate 			else
2140Sstevel@tonic-gate 				break;
2150Sstevel@tonic-gate 		}
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 	if (msg_flags & MSG_ABORT) {
2180Sstevel@tonic-gate 		if (mp && mp->b_cont) {
2190Sstevel@tonic-gate 			mblk_t *pump = msgpullup(mp, -1);
2200Sstevel@tonic-gate 			if (!pump) {
2210Sstevel@tonic-gate 				return (ENOMEM);
2220Sstevel@tonic-gate 			}
2230Sstevel@tonic-gate 			freemsg(mp);
2240Sstevel@tonic-gate 			mp = pump;
2250Sstevel@tonic-gate 			mproto->b_cont = mp;
2260Sstevel@tonic-gate 		}
2270Sstevel@tonic-gate 		RUN_SCTP(sctp);
2287480SKacheong.Poon@Sun.COM 		sctp_user_abort(sctp, mp);
2290Sstevel@tonic-gate 		freemsg(mproto);
2300Sstevel@tonic-gate 		goto process_sendq;
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 	if (mp == NULL)
2330Sstevel@tonic-gate 		goto done;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	RUN_SCTP(sctp);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	/* Reject any new data requests if we are shutting down */
2384505Skcpoon 	if (sctp->sctp_state > SCTPS_ESTABLISHED ||
2394505Skcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
2400Sstevel@tonic-gate 		error = EPIPE;
2410Sstevel@tonic-gate 		goto unlock_done;
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	/* Re-use the mproto to store relevant info. */
2450Sstevel@tonic-gate 	ASSERT(MBLKSIZE(mproto) >= sizeof (*sctp_msg_hdr));
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	mproto->b_rptr = mproto->b_datap->db_base;
2480Sstevel@tonic-gate 	mproto->b_wptr = mproto->b_rptr + sizeof (*sctp_msg_hdr);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	sctp_msg_hdr = (sctp_msg_hdr_t *)mproto->b_rptr;
2510Sstevel@tonic-gate 	bzero(sctp_msg_hdr, sizeof (*sctp_msg_hdr));
2520Sstevel@tonic-gate 	sctp_msg_hdr->smh_context = context;
2530Sstevel@tonic-gate 	sctp_msg_hdr->smh_sid = sid;
2540Sstevel@tonic-gate 	sctp_msg_hdr->smh_ppid = ppid;
2550Sstevel@tonic-gate 	sctp_msg_hdr->smh_flags = msg_flags;
2560Sstevel@tonic-gate 	sctp_msg_hdr->smh_ttl = MSEC_TO_TICK(timetolive);
2570Sstevel@tonic-gate 	sctp_msg_hdr->smh_tob = lbolt64;
2580Sstevel@tonic-gate 	for (; mp != NULL; mp = mp->b_cont)
2590Sstevel@tonic-gate 		msg_len += MBLKL(mp);
2600Sstevel@tonic-gate 	sctp_msg_hdr->smh_msglen = msg_len;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/* User requested specific destination */
2630Sstevel@tonic-gate 	SCTP_SET_CHUNK_DEST(mproto, fp);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_COOKIE_ECHOED &&
2660Sstevel@tonic-gate 	    sid >= sctp->sctp_num_ostr) {
2670Sstevel@tonic-gate 		/* Send sendfail event */
2680Sstevel@tonic-gate 		sctp_sendfail_event(sctp, dupmsg(mproto), SCTP_ERR_BAD_SID,
2690Sstevel@tonic-gate 		    B_FALSE);
2700Sstevel@tonic-gate 		error = EINVAL;
2710Sstevel@tonic-gate 		goto unlock_done;
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/* no data */
2750Sstevel@tonic-gate 	if (msg_len == 0) {
2760Sstevel@tonic-gate 		sctp_sendfail_event(sctp, dupmsg(mproto),
2770Sstevel@tonic-gate 		    SCTP_ERR_NO_USR_DATA, B_FALSE);
2780Sstevel@tonic-gate 		error = EINVAL;
2790Sstevel@tonic-gate 		goto unlock_done;
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/* Add it to the unsent list */
2830Sstevel@tonic-gate 	if (sctp->sctp_xmit_unsent == NULL) {
2840Sstevel@tonic-gate 		sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = mproto;
2850Sstevel@tonic-gate 	} else {
2860Sstevel@tonic-gate 		sctp->sctp_xmit_unsent_tail->b_next = mproto;
2870Sstevel@tonic-gate 		sctp->sctp_xmit_unsent_tail = mproto;
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 	sctp->sctp_unsent += msg_len;
2900Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_msgcount);
2910Sstevel@tonic-gate 	if (sctp->sctp_state == SCTPS_ESTABLISHED)
2923795Skcpoon 		sctp_output(sctp, UINT_MAX);
2930Sstevel@tonic-gate process_sendq:
2940Sstevel@tonic-gate 	WAKE_SCTP(sctp);
2950Sstevel@tonic-gate 	sctp_process_sendq(sctp);
2960Sstevel@tonic-gate 	return (0);
2970Sstevel@tonic-gate unlock_done:
2980Sstevel@tonic-gate 	WAKE_SCTP(sctp);
2990Sstevel@tonic-gate done:
3000Sstevel@tonic-gate 	return (error);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate void
3040Sstevel@tonic-gate sctp_chunkify(sctp_t *sctp, int first_len, int bytes_to_send)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	mblk_t			*mp;
3070Sstevel@tonic-gate 	mblk_t			*chunk_mp;
3080Sstevel@tonic-gate 	mblk_t			*chunk_head;
3090Sstevel@tonic-gate 	mblk_t			*chunk_hdr;
3100Sstevel@tonic-gate 	mblk_t			*chunk_tail = NULL;
3110Sstevel@tonic-gate 	int			count;
3120Sstevel@tonic-gate 	int			chunksize;
3130Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
3140Sstevel@tonic-gate 	mblk_t			*mdblk = sctp->sctp_xmit_unsent;
3150Sstevel@tonic-gate 	sctp_faddr_t		*fp;
3160Sstevel@tonic-gate 	sctp_faddr_t		*fp1;
3170Sstevel@tonic-gate 	size_t			xtralen;
3180Sstevel@tonic-gate 	sctp_msg_hdr_t		*msg_hdr;
3193448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	fp = SCTP_CHUNK_DEST(mdblk);
3220Sstevel@tonic-gate 	if (fp == NULL)
3230Sstevel@tonic-gate 		fp = sctp->sctp_current;
3240Sstevel@tonic-gate 	if (fp->isv4)
3253448Sdh155122 		xtralen = sctp->sctp_hdr_len + sctps->sctps_wroff_xtra +
3263448Sdh155122 		    sizeof (*sdc);
3270Sstevel@tonic-gate 	else
3283448Sdh155122 		xtralen = sctp->sctp_hdr6_len + sctps->sctps_wroff_xtra +
3293448Sdh155122 		    sizeof (*sdc);
3300Sstevel@tonic-gate 	count = chunksize = first_len - sizeof (*sdc);
3310Sstevel@tonic-gate nextmsg:
3320Sstevel@tonic-gate 	chunk_mp = mdblk->b_cont;
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	/*
3350Sstevel@tonic-gate 	 * If this partially chunked, we ignore the first_len for now
3360Sstevel@tonic-gate 	 * and use the one already present. For the unchunked bits, we
3370Sstevel@tonic-gate 	 * use the length of the last chunk.
3380Sstevel@tonic-gate 	 */
3390Sstevel@tonic-gate 	if (SCTP_IS_MSG_CHUNKED(mdblk)) {
3400Sstevel@tonic-gate 		int	chunk_len;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		ASSERT(chunk_mp->b_next != NULL);
3430Sstevel@tonic-gate 		mdblk->b_cont = chunk_mp->b_next;
3440Sstevel@tonic-gate 		chunk_mp->b_next = NULL;
3450Sstevel@tonic-gate 		SCTP_MSG_CLEAR_CHUNKED(mdblk);
3460Sstevel@tonic-gate 		mp = mdblk->b_cont;
3470Sstevel@tonic-gate 		while (mp->b_next != NULL)
3480Sstevel@tonic-gate 			mp = mp->b_next;
3490Sstevel@tonic-gate 		chunk_len = ntohs(((sctp_data_hdr_t *)mp->b_rptr)->sdh_len);
3500Sstevel@tonic-gate 		if (fp->sfa_pmss - chunk_len > sizeof (*sdc))
3510Sstevel@tonic-gate 			count = chunksize = fp->sfa_pmss - chunk_len;
3520Sstevel@tonic-gate 		else
3530Sstevel@tonic-gate 			count = chunksize = fp->sfa_pmss;
3540Sstevel@tonic-gate 		count = chunksize = count - sizeof (*sdc);
3550Sstevel@tonic-gate 	} else {
3560Sstevel@tonic-gate 		msg_hdr = (sctp_msg_hdr_t *)mdblk->b_rptr;
3570Sstevel@tonic-gate 		if (SCTP_MSG_TO_BE_ABANDONED(mdblk, msg_hdr, sctp)) {
3580Sstevel@tonic-gate 			sctp->sctp_xmit_unsent = mdblk->b_next;
3590Sstevel@tonic-gate 			if (sctp->sctp_xmit_unsent == NULL)
3600Sstevel@tonic-gate 				sctp->sctp_xmit_unsent_tail = NULL;
3610Sstevel@tonic-gate 			ASSERT(sctp->sctp_unsent >= msg_hdr->smh_msglen);
3620Sstevel@tonic-gate 			sctp->sctp_unsent -= msg_hdr->smh_msglen;
3630Sstevel@tonic-gate 			mdblk->b_next = NULL;
3640Sstevel@tonic-gate 			BUMP_LOCAL(sctp->sctp_prsctpdrop);
3650Sstevel@tonic-gate 			/*
3660Sstevel@tonic-gate 			 * Update ULP the amount of queued data, which is
3670Sstevel@tonic-gate 			 * sent-unack'ed + unsent.
3680Sstevel@tonic-gate 			 */
3690Sstevel@tonic-gate 			if (!SCTP_IS_DETACHED(sctp)) {
3700Sstevel@tonic-gate 				sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
3710Sstevel@tonic-gate 				    sctp->sctp_unacked + sctp->sctp_unsent);
3720Sstevel@tonic-gate 			}
3730Sstevel@tonic-gate 			sctp_sendfail_event(sctp, mdblk, 0, B_FALSE);
3740Sstevel@tonic-gate 			goto try_next;
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 		mdblk->b_cont = NULL;
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 	msg_hdr = (sctp_msg_hdr_t *)mdblk->b_rptr;
3790Sstevel@tonic-gate nextchunk:
3800Sstevel@tonic-gate 	chunk_head = chunk_mp;
3810Sstevel@tonic-gate 	chunk_tail = NULL;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	/* Skip as many mblk's as we need */
3840Sstevel@tonic-gate 	while (chunk_mp != NULL && ((count - MBLKL(chunk_mp)) >= 0)) {
3850Sstevel@tonic-gate 		count -= MBLKL(chunk_mp);
3860Sstevel@tonic-gate 		chunk_tail = chunk_mp;
3870Sstevel@tonic-gate 		chunk_mp = chunk_mp->b_cont;
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	/* Split the chain, if needed */
3900Sstevel@tonic-gate 	if (chunk_mp != NULL) {
3910Sstevel@tonic-gate 		if (count > 0) {
3920Sstevel@tonic-gate 			mblk_t	*split_mp = dupb(chunk_mp);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 			if (split_mp == NULL) {
3950Sstevel@tonic-gate 				if (mdblk->b_cont == NULL) {
3960Sstevel@tonic-gate 					mdblk->b_cont = chunk_head;
3970Sstevel@tonic-gate 				} else  {
3980Sstevel@tonic-gate 					SCTP_MSG_SET_CHUNKED(mdblk);
3990Sstevel@tonic-gate 					ASSERT(chunk_head->b_next == NULL);
4000Sstevel@tonic-gate 					chunk_head->b_next = mdblk->b_cont;
4010Sstevel@tonic-gate 					mdblk->b_cont = chunk_head;
4020Sstevel@tonic-gate 				}
4030Sstevel@tonic-gate 				return;
4040Sstevel@tonic-gate 			}
4050Sstevel@tonic-gate 			if (chunk_tail != NULL) {
4060Sstevel@tonic-gate 				chunk_tail->b_cont = split_mp;
4070Sstevel@tonic-gate 				chunk_tail = chunk_tail->b_cont;
4080Sstevel@tonic-gate 			} else {
4090Sstevel@tonic-gate 				chunk_head = chunk_tail = split_mp;
4100Sstevel@tonic-gate 			}
4110Sstevel@tonic-gate 			chunk_tail->b_wptr = chunk_tail->b_rptr + count;
4120Sstevel@tonic-gate 			chunk_mp->b_rptr = chunk_tail->b_wptr;
4130Sstevel@tonic-gate 			count = 0;
4140Sstevel@tonic-gate 		} else if (chunk_tail == NULL) {
4150Sstevel@tonic-gate 			goto next;
4160Sstevel@tonic-gate 		} else {
4170Sstevel@tonic-gate 			chunk_tail->b_cont = NULL;
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 	/* Alloc chunk hdr, if needed */
4210Sstevel@tonic-gate 	if (DB_REF(chunk_head) > 1 ||
4220Sstevel@tonic-gate 	    ((intptr_t)chunk_head->b_rptr) & (SCTP_ALIGN - 1) ||
4230Sstevel@tonic-gate 	    MBLKHEAD(chunk_head) < sizeof (*sdc)) {
4240Sstevel@tonic-gate 		if ((chunk_hdr = allocb(xtralen, BPRI_MED)) == NULL) {
4250Sstevel@tonic-gate 			if (mdblk->b_cont == NULL) {
4260Sstevel@tonic-gate 				if (chunk_mp != NULL)
4270Sstevel@tonic-gate 					linkb(chunk_head, chunk_mp);
4280Sstevel@tonic-gate 				mdblk->b_cont = chunk_head;
4290Sstevel@tonic-gate 			} else {
4300Sstevel@tonic-gate 				SCTP_MSG_SET_CHUNKED(mdblk);
4310Sstevel@tonic-gate 				if (chunk_mp != NULL)
4320Sstevel@tonic-gate 					linkb(chunk_head, chunk_mp);
4330Sstevel@tonic-gate 				ASSERT(chunk_head->b_next == NULL);
4340Sstevel@tonic-gate 				chunk_head->b_next = mdblk->b_cont;
4350Sstevel@tonic-gate 				mdblk->b_cont = chunk_head;
4360Sstevel@tonic-gate 			}
4370Sstevel@tonic-gate 			return;
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 		chunk_hdr->b_rptr += xtralen - sizeof (*sdc);
4400Sstevel@tonic-gate 		chunk_hdr->b_wptr = chunk_hdr->b_rptr + sizeof (*sdc);
4410Sstevel@tonic-gate 		chunk_hdr->b_cont = chunk_head;
4420Sstevel@tonic-gate 	} else {
4430Sstevel@tonic-gate 		chunk_hdr = chunk_head;
4440Sstevel@tonic-gate 		chunk_hdr->b_rptr -= sizeof (*sdc);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 	ASSERT(chunk_hdr->b_datap->db_ref == 1);
4470Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)chunk_hdr->b_rptr;
4480Sstevel@tonic-gate 	sdc->sdh_id = CHUNK_DATA;
4490Sstevel@tonic-gate 	sdc->sdh_flags = 0;
4500Sstevel@tonic-gate 	sdc->sdh_len = htons(sizeof (*sdc) + chunksize - count);
4510Sstevel@tonic-gate 	ASSERT(sdc->sdh_len);
4520Sstevel@tonic-gate 	sdc->sdh_sid = htons(msg_hdr->smh_sid);
4530Sstevel@tonic-gate 	/*
4540Sstevel@tonic-gate 	 * We defer assigning the SSN just before sending the chunk, else
4550Sstevel@tonic-gate 	 * if we drop the chunk in sctp_get_msg_to_send(), we would need
4560Sstevel@tonic-gate 	 * to send a Forward TSN to let the peer know. Some more comments
4570Sstevel@tonic-gate 	 * about this in sctp_impl.h for SCTP_CHUNK_SENT.
4580Sstevel@tonic-gate 	 */
4590Sstevel@tonic-gate 	sdc->sdh_payload_id = msg_hdr->smh_ppid;
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	if (mdblk->b_cont == NULL) {
4620Sstevel@tonic-gate 		mdblk->b_cont = chunk_hdr;
4630Sstevel@tonic-gate 		SCTP_DATA_SET_BBIT(sdc);
4640Sstevel@tonic-gate 	} else {
4650Sstevel@tonic-gate 		mp = mdblk->b_cont;
4660Sstevel@tonic-gate 		while (mp->b_next != NULL)
4670Sstevel@tonic-gate 			mp = mp->b_next;
4680Sstevel@tonic-gate 		mp->b_next = chunk_hdr;
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	bytes_to_send -= (chunksize - count);
4720Sstevel@tonic-gate 	if (chunk_mp != NULL) {
4730Sstevel@tonic-gate next:
4740Sstevel@tonic-gate 		count = chunksize = fp->sfa_pmss - sizeof (*sdc);
4750Sstevel@tonic-gate 		goto nextchunk;
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 	SCTP_DATA_SET_EBIT(sdc);
4780Sstevel@tonic-gate 	sctp->sctp_xmit_unsent = mdblk->b_next;
4790Sstevel@tonic-gate 	if (mdblk->b_next == NULL) {
4800Sstevel@tonic-gate 		sctp->sctp_xmit_unsent_tail = NULL;
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 	mdblk->b_next = NULL;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	if (sctp->sctp_xmit_tail == NULL) {
4850Sstevel@tonic-gate 		sctp->sctp_xmit_head = sctp->sctp_xmit_tail = mdblk;
4860Sstevel@tonic-gate 	} else {
4870Sstevel@tonic-gate 		mp = sctp->sctp_xmit_tail;
4880Sstevel@tonic-gate 		while (mp->b_next != NULL)
4890Sstevel@tonic-gate 			mp = mp->b_next;
4900Sstevel@tonic-gate 		mp->b_next = mdblk;
4910Sstevel@tonic-gate 		mdblk->b_prev = mp;
4920Sstevel@tonic-gate 	}
4930Sstevel@tonic-gate try_next:
4940Sstevel@tonic-gate 	if (bytes_to_send > 0 && sctp->sctp_xmit_unsent != NULL) {
4950Sstevel@tonic-gate 		mdblk = sctp->sctp_xmit_unsent;
4960Sstevel@tonic-gate 		fp1 = SCTP_CHUNK_DEST(mdblk);
4970Sstevel@tonic-gate 		if (fp1 == NULL)
4980Sstevel@tonic-gate 			fp1 = sctp->sctp_current;
4990Sstevel@tonic-gate 		if (fp == fp1) {
5000Sstevel@tonic-gate 			size_t len = MBLKL(mdblk->b_cont);
5010Sstevel@tonic-gate 			if ((count > 0) &&
5020Sstevel@tonic-gate 			    ((len > fp->sfa_pmss - sizeof (*sdc)) ||
5034505Skcpoon 			    (len <= count))) {
5040Sstevel@tonic-gate 				count -= sizeof (*sdc);
5050Sstevel@tonic-gate 				count = chunksize = count - (count & 0x3);
5060Sstevel@tonic-gate 			} else {
5070Sstevel@tonic-gate 				count = chunksize = fp->sfa_pmss -
5080Sstevel@tonic-gate 				    sizeof (*sdc);
5090Sstevel@tonic-gate 			}
5100Sstevel@tonic-gate 		} else {
5110Sstevel@tonic-gate 			if (fp1->isv4)
5120Sstevel@tonic-gate 				xtralen = sctp->sctp_hdr_len;
5130Sstevel@tonic-gate 			else
5140Sstevel@tonic-gate 				xtralen = sctp->sctp_hdr6_len;
5153448Sdh155122 			xtralen += sctps->sctps_wroff_xtra + sizeof (*sdc);
5160Sstevel@tonic-gate 			count = chunksize = fp1->sfa_pmss - sizeof (*sdc);
5170Sstevel@tonic-gate 			fp = fp1;
5180Sstevel@tonic-gate 		}
5190Sstevel@tonic-gate 		goto nextmsg;
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate void
5240Sstevel@tonic-gate sctp_free_msg(mblk_t *ump)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	mblk_t *mp, *nmp;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	for (mp = ump->b_cont; mp; mp = nmp) {
5290Sstevel@tonic-gate 		nmp = mp->b_next;
5300Sstevel@tonic-gate 		mp->b_next = mp->b_prev = NULL;
5310Sstevel@tonic-gate 		freemsg(mp);
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 	ASSERT(!ump->b_prev);
5340Sstevel@tonic-gate 	ump->b_next = NULL;
5350Sstevel@tonic-gate 	freeb(ump);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate mblk_t *
539252Svi117747 sctp_add_proto_hdr(sctp_t *sctp, sctp_faddr_t *fp, mblk_t *mp, int sacklen,
540252Svi117747     int *error)
5410Sstevel@tonic-gate {
5420Sstevel@tonic-gate 	int hdrlen;
5430Sstevel@tonic-gate 	char *hdr;
5440Sstevel@tonic-gate 	int isv4 = fp->isv4;
5453448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
5460Sstevel@tonic-gate 
547252Svi117747 	if (error != NULL)
548252Svi117747 		*error = 0;
549252Svi117747 
5500Sstevel@tonic-gate 	if (isv4) {
5510Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
5520Sstevel@tonic-gate 		hdr = sctp->sctp_iphc;
5530Sstevel@tonic-gate 	} else {
5540Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
5550Sstevel@tonic-gate 		hdr = sctp->sctp_iphc6;
5560Sstevel@tonic-gate 	}
557252Svi117747 	/*
558252Svi117747 	 * A null fp->ire could mean that the address is 'down'. Similarly,
559252Svi117747 	 * it is possible that the address went down, we tried to send an
560252Svi117747 	 * heartbeat and ended up setting fp->saddr as unspec because we
5611735Skcpoon 	 * didn't have any usable source address.  In either case
5621735Skcpoon 	 * sctp_get_ire() will try find an IRE, if available, and set
5631735Skcpoon 	 * the source address, if needed.  If we still don't have any
564252Svi117747 	 * usable source address, fp->state will be SCTP_FADDRS_UNREACH and
565252Svi117747 	 * we return EHOSTUNREACH.
566252Svi117747 	 */
567252Svi117747 	if (fp->ire == NULL || SCTP_IS_ADDR_UNSPEC(fp->isv4, fp->saddr)) {
5681735Skcpoon 		sctp_get_ire(sctp, fp);
569252Svi117747 		if (fp->state == SCTP_FADDRS_UNREACH) {
570252Svi117747 			if (error != NULL)
571252Svi117747 				*error = EHOSTUNREACH;
572252Svi117747 			return (NULL);
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 	/* Copy in IP header. */
5760Sstevel@tonic-gate 	if ((mp->b_rptr - mp->b_datap->db_base) <
5773448Sdh155122 	    (sctps->sctps_wroff_xtra + hdrlen + sacklen) || DB_REF(mp) > 2 ||
5781735Skcpoon 	    !IS_P2ALIGNED(DB_BASE(mp), sizeof (ire_t *))) {
5790Sstevel@tonic-gate 		mblk_t *nmp;
5801735Skcpoon 
5810Sstevel@tonic-gate 		/*
5820Sstevel@tonic-gate 		 * This can happen if IP headers are adjusted after
5830Sstevel@tonic-gate 		 * data was moved into chunks, or during retransmission,
5840Sstevel@tonic-gate 		 * or things like snoop is running.
5850Sstevel@tonic-gate 		 */
5863448Sdh155122 		nmp = allocb_cred(sctps->sctps_wroff_xtra + hdrlen + sacklen,
5871676Sjpk 		    CONN_CRED(sctp->sctp_connp));
5880Sstevel@tonic-gate 		if (nmp == NULL) {
589252Svi117747 			if (error !=  NULL)
590252Svi117747 				*error = ENOMEM;
5910Sstevel@tonic-gate 			return (NULL);
5920Sstevel@tonic-gate 		}
5933448Sdh155122 		nmp->b_rptr += sctps->sctps_wroff_xtra;
5940Sstevel@tonic-gate 		nmp->b_wptr = nmp->b_rptr + hdrlen + sacklen;
5950Sstevel@tonic-gate 		nmp->b_cont = mp;
5960Sstevel@tonic-gate 		mp = nmp;
5970Sstevel@tonic-gate 	} else {
5980Sstevel@tonic-gate 		mp->b_rptr -= (hdrlen + sacklen);
5991676Sjpk 		mblk_setcred(mp, CONN_CRED(sctp->sctp_connp));
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 	bcopy(hdr, mp->b_rptr, hdrlen);
6020Sstevel@tonic-gate 	if (sacklen) {
6030Sstevel@tonic-gate 		sctp_fill_sack(sctp, mp->b_rptr + hdrlen, sacklen);
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 	if (fp != sctp->sctp_current) {
6060Sstevel@tonic-gate 		/* change addresses in header */
6070Sstevel@tonic-gate 		if (isv4) {
6080Sstevel@tonic-gate 			ipha_t *iph = (ipha_t *)mp->b_rptr;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst);
6110Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr)) {
6120Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&fp->saddr,
6130Sstevel@tonic-gate 				    iph->ipha_src);
6140Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
6150Sstevel@tonic-gate 				iph->ipha_src = INADDR_ANY;
6160Sstevel@tonic-gate 			}
6170Sstevel@tonic-gate 		} else {
6180Sstevel@tonic-gate 			((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr;
6190Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&fp->saddr)) {
6200Sstevel@tonic-gate 				((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr;
6210Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
6220Sstevel@tonic-gate 				V6_SET_ZERO(((ip6_t *)(mp->b_rptr))->ip6_src);
6230Sstevel@tonic-gate 			}
6240Sstevel@tonic-gate 		}
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 	/*
6270Sstevel@tonic-gate 	 * IP will not free this IRE if it is condemned.  SCTP needs to
6280Sstevel@tonic-gate 	 * free it.
6290Sstevel@tonic-gate 	 */
6300Sstevel@tonic-gate 	if ((fp->ire != NULL) && (fp->ire->ire_marks & IRE_MARK_CONDEMNED)) {
6310Sstevel@tonic-gate 		IRE_REFRELE_NOTR(fp->ire);
6320Sstevel@tonic-gate 		fp->ire = NULL;
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	/* Stash the conn and ire ptr info for IP */
6360Sstevel@tonic-gate 	SCTP_STASH_IPINFO(mp, fp->ire);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	return (mp);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate  * SCTP requires every chunk to be padded so that the total length
6430Sstevel@tonic-gate  * is a multiple of SCTP_ALIGN.  This function returns a mblk with
6440Sstevel@tonic-gate  * the specified pad length.
6450Sstevel@tonic-gate  */
6460Sstevel@tonic-gate static mblk_t *
6474691Skcpoon sctp_get_padding(sctp_t *sctp, int pad)
6480Sstevel@tonic-gate {
6490Sstevel@tonic-gate 	mblk_t *fill;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	ASSERT(pad < SCTP_ALIGN);
6524691Skcpoon 	ASSERT(sctp->sctp_pad_mp != NULL);
6534691Skcpoon 	if ((fill = dupb(sctp->sctp_pad_mp)) != NULL) {
6540Sstevel@tonic-gate 		fill->b_wptr += pad;
6550Sstevel@tonic-gate 		return (fill);
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	/*
6590Sstevel@tonic-gate 	 * The memory saving path of reusing the sctp_pad_mp
6600Sstevel@tonic-gate 	 * fails may be because it has been dupb() too
6610Sstevel@tonic-gate 	 * many times (DBLK_REFMAX).  Use the memory consuming
6620Sstevel@tonic-gate 	 * path of allocating the pad mblk.
6630Sstevel@tonic-gate 	 */
6640Sstevel@tonic-gate 	if ((fill = allocb(SCTP_ALIGN, BPRI_MED)) != NULL) {
6650Sstevel@tonic-gate 		/* Zero it out.  SCTP_ALIGN is sizeof (int32_t) */
6660Sstevel@tonic-gate 		*(int32_t *)fill->b_rptr = 0;
6670Sstevel@tonic-gate 		fill->b_wptr += pad;
6680Sstevel@tonic-gate 	}
6690Sstevel@tonic-gate 	return (fill);
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate static mblk_t *
6730Sstevel@tonic-gate sctp_find_fast_rexmit_mblks(sctp_t *sctp, int *total, sctp_faddr_t **fp)
6740Sstevel@tonic-gate {
6750Sstevel@tonic-gate 	mblk_t		*meta;
6760Sstevel@tonic-gate 	mblk_t		*start_mp = NULL;
6770Sstevel@tonic-gate 	mblk_t		*end_mp = NULL;
6780Sstevel@tonic-gate 	mblk_t		*mp, *nmp;
6790Sstevel@tonic-gate 	mblk_t		*fill;
6800Sstevel@tonic-gate 	sctp_data_hdr_t	*sdh;
6810Sstevel@tonic-gate 	int		msglen;
6820Sstevel@tonic-gate 	int		extra;
6830Sstevel@tonic-gate 	sctp_msg_hdr_t	*msg_hdr;
6841735Skcpoon 	sctp_faddr_t	*old_fp = NULL;
6851735Skcpoon 	sctp_faddr_t	*chunk_fp;
6863448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	for (meta = sctp->sctp_xmit_head; meta != NULL; meta = meta->b_next) {
6890Sstevel@tonic-gate 		msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
6900Sstevel@tonic-gate 		if (SCTP_IS_MSG_ABANDONED(meta) ||
6910Sstevel@tonic-gate 		    SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
6920Sstevel@tonic-gate 			continue;
6930Sstevel@tonic-gate 		}
6940Sstevel@tonic-gate 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
6950Sstevel@tonic-gate 			if (SCTP_CHUNK_WANT_REXMIT(mp)) {
6960Sstevel@tonic-gate 				/*
6970Sstevel@tonic-gate 				 * Use the same peer address to do fast
6981735Skcpoon 				 * retransmission.  If the original peer
6991735Skcpoon 				 * address is dead, switch to the current
7001735Skcpoon 				 * one.  Record the old one so that we
7011735Skcpoon 				 * will pick the chunks sent to the old
7021735Skcpoon 				 * one for fast retransmission.
7030Sstevel@tonic-gate 				 */
7041735Skcpoon 				chunk_fp = SCTP_CHUNK_DEST(mp);
7050Sstevel@tonic-gate 				if (*fp == NULL) {
7061735Skcpoon 					*fp = chunk_fp;
7071735Skcpoon 					if ((*fp)->state != SCTP_FADDRS_ALIVE) {
7081735Skcpoon 						old_fp = *fp;
7090Sstevel@tonic-gate 						*fp = sctp->sctp_current;
7101735Skcpoon 					}
7111735Skcpoon 				} else if (old_fp == NULL && *fp != chunk_fp) {
7121735Skcpoon 					continue;
7131735Skcpoon 				} else if (old_fp != NULL &&
7141735Skcpoon 				    old_fp != chunk_fp) {
7150Sstevel@tonic-gate 					continue;
7160Sstevel@tonic-gate 				}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 				sdh = (sctp_data_hdr_t *)mp->b_rptr;
7190Sstevel@tonic-gate 				msglen = ntohs(sdh->sdh_len);
7200Sstevel@tonic-gate 				if ((extra = msglen & (SCTP_ALIGN - 1)) != 0) {
7210Sstevel@tonic-gate 					extra = SCTP_ALIGN - extra;
7220Sstevel@tonic-gate 				}
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 				/*
7250Sstevel@tonic-gate 				 * We still return at least the first message
7260Sstevel@tonic-gate 				 * even if that message cannot fit in as
7270Sstevel@tonic-gate 				 * PMTU may have changed.
7280Sstevel@tonic-gate 				 */
7290Sstevel@tonic-gate 				if (*total + msglen + extra >
7300Sstevel@tonic-gate 				    (*fp)->sfa_pmss && start_mp != NULL) {
7310Sstevel@tonic-gate 					return (start_mp);
7320Sstevel@tonic-gate 				}
7330Sstevel@tonic-gate 				if ((nmp = dupmsg(mp)) == NULL)
7340Sstevel@tonic-gate 					return (start_mp);
7350Sstevel@tonic-gate 				if (extra > 0) {
7364691Skcpoon 					fill = sctp_get_padding(sctp, extra);
7370Sstevel@tonic-gate 					if (fill != NULL) {
7380Sstevel@tonic-gate 						linkb(nmp, fill);
7390Sstevel@tonic-gate 					} else {
7400Sstevel@tonic-gate 						return (start_mp);
7410Sstevel@tonic-gate 					}
7420Sstevel@tonic-gate 				}
7433448Sdh155122 				BUMP_MIB(&sctps->sctps_mib, sctpOutFastRetrans);
7441735Skcpoon 				BUMP_LOCAL(sctp->sctp_rxtchunks);
7450Sstevel@tonic-gate 				SCTP_CHUNK_CLEAR_REXMIT(mp);
7460Sstevel@tonic-gate 				if (start_mp == NULL) {
7470Sstevel@tonic-gate 					start_mp = nmp;
7480Sstevel@tonic-gate 				} else {
7490Sstevel@tonic-gate 					linkb(end_mp, nmp);
7500Sstevel@tonic-gate 				}
7510Sstevel@tonic-gate 				end_mp = nmp;
7520Sstevel@tonic-gate 				*total += msglen + extra;
7530Sstevel@tonic-gate 				dprint(2, ("sctp_find_fast_rexmit_mblks: "
7540Sstevel@tonic-gate 				    "tsn %x\n", sdh->sdh_tsn));
7550Sstevel@tonic-gate 			}
7560Sstevel@tonic-gate 		}
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 	/* Clear the flag as there is no more message to be fast rexmitted. */
7590Sstevel@tonic-gate 	sctp->sctp_chk_fast_rexmit = B_FALSE;
7600Sstevel@tonic-gate 	return (start_mp);
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate /* A debug function just to make sure that a mblk chain is not broken */
7640Sstevel@tonic-gate #ifdef	DEBUG
7650Sstevel@tonic-gate static boolean_t
7660Sstevel@tonic-gate sctp_verify_chain(mblk_t *head, mblk_t *tail)
7670Sstevel@tonic-gate {
7680Sstevel@tonic-gate 	mblk_t	*mp = head;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	if (head == NULL || tail == NULL)
7710Sstevel@tonic-gate 		return (B_TRUE);
7720Sstevel@tonic-gate 	while (mp != NULL) {
7730Sstevel@tonic-gate 		if (mp == tail)
7740Sstevel@tonic-gate 			return (B_TRUE);
7750Sstevel@tonic-gate 		mp = mp->b_next;
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate 	return (B_FALSE);
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate #endif
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate /*
7820Sstevel@tonic-gate  * Gets the next unsent chunk to transmit. Messages that are abandoned are
7830Sstevel@tonic-gate  * skipped. A message can be abandoned if it has a non-zero timetolive and
7840Sstevel@tonic-gate  * transmission has not yet started or if it is a partially reliable
7850Sstevel@tonic-gate  * message and its time is up (assuming we are PR-SCTP aware).
7860Sstevel@tonic-gate  * 'cansend' is used to determine if need to try and chunkify messages from
7870Sstevel@tonic-gate  * the unsent list, if any, and also as an input to sctp_chunkify() if so.
7888154SGeorge.Shepherd@Sun.COM  *
7898154SGeorge.Shepherd@Sun.COM  * firstseg indicates the space already used, cansend represents remaining
7908154SGeorge.Shepherd@Sun.COM  * space in the window, ((sfa_pmss - firstseg) can therefore reasonably
7918154SGeorge.Shepherd@Sun.COM  * be used to compute the cansend arg).
7920Sstevel@tonic-gate  */
7930Sstevel@tonic-gate mblk_t *
7940Sstevel@tonic-gate sctp_get_msg_to_send(sctp_t *sctp, mblk_t **mp, mblk_t *meta, int  *error,
7950Sstevel@tonic-gate     int32_t firstseg, uint32_t cansend, sctp_faddr_t *fp)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate 	mblk_t		*mp1;
7980Sstevel@tonic-gate 	sctp_msg_hdr_t	*msg_hdr;
7990Sstevel@tonic-gate 	mblk_t		*tmp_meta;
8000Sstevel@tonic-gate 	sctp_faddr_t	*fp1;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	ASSERT(error != NULL && mp != NULL);
8030Sstevel@tonic-gate 	*error = 0;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	ASSERT(sctp->sctp_current != NULL);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate chunkified:
8080Sstevel@tonic-gate 	while (meta != NULL) {
8090Sstevel@tonic-gate 		tmp_meta = meta->b_next;
8100Sstevel@tonic-gate 		msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
8110Sstevel@tonic-gate 		mp1 = meta->b_cont;
8120Sstevel@tonic-gate 		if (SCTP_IS_MSG_ABANDONED(meta))
8130Sstevel@tonic-gate 			goto next_msg;
8140Sstevel@tonic-gate 		if (!SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
8150Sstevel@tonic-gate 			while (mp1 != NULL) {
8160Sstevel@tonic-gate 				if (SCTP_CHUNK_CANSEND(mp1)) {
8170Sstevel@tonic-gate 					*mp = mp1;
8180Sstevel@tonic-gate #ifdef	DEBUG
8190Sstevel@tonic-gate 					ASSERT(sctp_verify_chain(
8200Sstevel@tonic-gate 					    sctp->sctp_xmit_head, meta));
8210Sstevel@tonic-gate #endif
8220Sstevel@tonic-gate 					return (meta);
8230Sstevel@tonic-gate 				}
8240Sstevel@tonic-gate 				mp1 = mp1->b_next;
8250Sstevel@tonic-gate 			}
8260Sstevel@tonic-gate 			goto next_msg;
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 		/*
8290Sstevel@tonic-gate 		 * If we come here and the first chunk is sent, then we
8300Sstevel@tonic-gate 		 * we are PR-SCTP aware, in which case if the cumulative
8310Sstevel@tonic-gate 		 * TSN has moved upto or beyond the first chunk (which
8320Sstevel@tonic-gate 		 * means all the previous messages have been cumulative
8330Sstevel@tonic-gate 		 * SACK'd), then we send a Forward TSN with the last
8340Sstevel@tonic-gate 		 * chunk that was sent in this message. If we can't send
8350Sstevel@tonic-gate 		 * a Forward TSN because previous non-abandoned messages
8360Sstevel@tonic-gate 		 * have not been acked then we will defer the Forward TSN
8370Sstevel@tonic-gate 		 * to sctp_rexmit() or sctp_cumack().
8380Sstevel@tonic-gate 		 */
8390Sstevel@tonic-gate 		if (SCTP_CHUNK_ISSENT(mp1)) {
8400Sstevel@tonic-gate 			*error = sctp_check_abandoned_msg(sctp, meta);
8410Sstevel@tonic-gate 			if (*error != 0) {
8420Sstevel@tonic-gate #ifdef	DEBUG
8430Sstevel@tonic-gate 				ASSERT(sctp_verify_chain(sctp->sctp_xmit_head,
8440Sstevel@tonic-gate 				    sctp->sctp_xmit_tail));
8450Sstevel@tonic-gate #endif
8460Sstevel@tonic-gate 				return (NULL);
8470Sstevel@tonic-gate 			}
8480Sstevel@tonic-gate 			goto next_msg;
8490Sstevel@tonic-gate 		}
8500Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_prsctpdrop);
8510Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent >= msg_hdr->smh_msglen);
8520Sstevel@tonic-gate 		if (meta->b_prev == NULL) {
8530Sstevel@tonic-gate 			ASSERT(sctp->sctp_xmit_head == meta);
8540Sstevel@tonic-gate 			sctp->sctp_xmit_head = tmp_meta;
8550Sstevel@tonic-gate 			if (sctp->sctp_xmit_tail == meta)
8560Sstevel@tonic-gate 				sctp->sctp_xmit_tail = tmp_meta;
8570Sstevel@tonic-gate 			meta->b_next = NULL;
8580Sstevel@tonic-gate 			if (tmp_meta != NULL)
8590Sstevel@tonic-gate 				tmp_meta->b_prev = NULL;
8600Sstevel@tonic-gate 		} else if (meta->b_next == NULL) {
8610Sstevel@tonic-gate 			if (sctp->sctp_xmit_tail == meta)
8620Sstevel@tonic-gate 				sctp->sctp_xmit_tail = meta->b_prev;
8630Sstevel@tonic-gate 			meta->b_prev->b_next = NULL;
8640Sstevel@tonic-gate 			meta->b_prev = NULL;
8650Sstevel@tonic-gate 		} else {
8660Sstevel@tonic-gate 			meta->b_prev->b_next = tmp_meta;
8670Sstevel@tonic-gate 			tmp_meta->b_prev = meta->b_prev;
8680Sstevel@tonic-gate 			if (sctp->sctp_xmit_tail == meta)
8690Sstevel@tonic-gate 				sctp->sctp_xmit_tail = tmp_meta;
8700Sstevel@tonic-gate 			meta->b_prev = NULL;
8710Sstevel@tonic-gate 			meta->b_next = NULL;
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 		sctp->sctp_unsent -= msg_hdr->smh_msglen;
8740Sstevel@tonic-gate 		/*
8750Sstevel@tonic-gate 		 * Update ULP the amount of queued data, which is
8760Sstevel@tonic-gate 		 * sent-unack'ed + unsent.
8770Sstevel@tonic-gate 		 */
8780Sstevel@tonic-gate 		if (!SCTP_IS_DETACHED(sctp)) {
8790Sstevel@tonic-gate 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
8800Sstevel@tonic-gate 			    sctp->sctp_unacked + sctp->sctp_unsent);
8810Sstevel@tonic-gate 		}
8820Sstevel@tonic-gate 		sctp_sendfail_event(sctp, meta, 0, B_TRUE);
8830Sstevel@tonic-gate next_msg:
8840Sstevel@tonic-gate 		meta = tmp_meta;
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 	/* chunkify, if needed */
8870Sstevel@tonic-gate 	if (cansend > 0 && sctp->sctp_xmit_unsent != NULL) {
8880Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent > 0);
8890Sstevel@tonic-gate 		if (fp == NULL) {
8900Sstevel@tonic-gate 			fp = SCTP_CHUNK_DEST(sctp->sctp_xmit_unsent);
8910Sstevel@tonic-gate 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
8920Sstevel@tonic-gate 				fp = sctp->sctp_current;
8930Sstevel@tonic-gate 		} else {
8940Sstevel@tonic-gate 			/*
8950Sstevel@tonic-gate 			 * If user specified destination, try to honor that.
8960Sstevel@tonic-gate 			 */
8970Sstevel@tonic-gate 			fp1 = SCTP_CHUNK_DEST(sctp->sctp_xmit_unsent);
8980Sstevel@tonic-gate 			if (fp1 != NULL && fp1->state == SCTP_FADDRS_ALIVE &&
8990Sstevel@tonic-gate 			    fp1 != fp) {
9000Sstevel@tonic-gate 				goto chunk_done;
9010Sstevel@tonic-gate 			}
9020Sstevel@tonic-gate 		}
9030Sstevel@tonic-gate 		sctp_chunkify(sctp, fp->sfa_pmss - firstseg, cansend);
9040Sstevel@tonic-gate 		if ((meta = sctp->sctp_xmit_tail) == NULL)
9050Sstevel@tonic-gate 			goto chunk_done;
9060Sstevel@tonic-gate 		/*
9070Sstevel@tonic-gate 		 * sctp_chunkify() won't advance sctp_xmit_tail if it adds
9080Sstevel@tonic-gate 		 * new chunk(s) to the tail, so we need to skip the
9090Sstevel@tonic-gate 		 * sctp_xmit_tail, which would have already been processed.
9100Sstevel@tonic-gate 		 * This could happen when there is unacked chunks, but
9110Sstevel@tonic-gate 		 * nothing new to send.
9120Sstevel@tonic-gate 		 * When sctp_chunkify() is called when the transmit queue
9130Sstevel@tonic-gate 		 * is empty then we need to start from sctp_xmit_tail.
9140Sstevel@tonic-gate 		 */
9150Sstevel@tonic-gate 		if (SCTP_CHUNK_ISSENT(sctp->sctp_xmit_tail->b_cont)) {
9160Sstevel@tonic-gate #ifdef	DEBUG
9170Sstevel@tonic-gate 			mp1 = sctp->sctp_xmit_tail->b_cont;
9180Sstevel@tonic-gate 			while (mp1 != NULL) {
9190Sstevel@tonic-gate 				ASSERT(!SCTP_CHUNK_CANSEND(mp1));
9200Sstevel@tonic-gate 				mp1 = mp1->b_next;
9210Sstevel@tonic-gate 			}
9220Sstevel@tonic-gate #endif
9230Sstevel@tonic-gate 			if ((meta = sctp->sctp_xmit_tail->b_next) == NULL)
9240Sstevel@tonic-gate 				goto chunk_done;
9250Sstevel@tonic-gate 		}
9260Sstevel@tonic-gate 		goto chunkified;
9270Sstevel@tonic-gate 	}
9280Sstevel@tonic-gate chunk_done:
9290Sstevel@tonic-gate #ifdef	DEBUG
9300Sstevel@tonic-gate 	ASSERT(sctp_verify_chain(sctp->sctp_xmit_head, sctp->sctp_xmit_tail));
9310Sstevel@tonic-gate #endif
9320Sstevel@tonic-gate 	return (NULL);
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate void
9360Sstevel@tonic-gate sctp_fast_rexmit(sctp_t *sctp)
9370Sstevel@tonic-gate {
9380Sstevel@tonic-gate 	mblk_t		*mp, *head;
9390Sstevel@tonic-gate 	int		pktlen = 0;
9400Sstevel@tonic-gate 	sctp_faddr_t	*fp = NULL;
9413448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head != NULL);
9440Sstevel@tonic-gate 	mp = sctp_find_fast_rexmit_mblks(sctp, &pktlen, &fp);
9451735Skcpoon 	if (mp == NULL) {
9463448Sdh155122 		SCTP_KSTAT(sctps, sctp_fr_not_found);
9470Sstevel@tonic-gate 		return;
9481735Skcpoon 	}
949252Svi117747 	if ((head = sctp_add_proto_hdr(sctp, fp, mp, 0, NULL)) == NULL) {
9500Sstevel@tonic-gate 		freemsg(mp);
9513448Sdh155122 		SCTP_KSTAT(sctps, sctp_fr_add_hdr);
9520Sstevel@tonic-gate 		return;
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 	if ((pktlen > fp->sfa_pmss) && fp->isv4) {
9550Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)head->b_rptr;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 		iph->ipha_fragment_offset_and_flags = 0;
9580Sstevel@tonic-gate 	}
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	sctp_set_iplen(sctp, head);
9610Sstevel@tonic-gate 	sctp_add_sendq(sctp, head);
9620Sstevel@tonic-gate 	sctp->sctp_active = fp->lastactive = lbolt64;
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate void
9663795Skcpoon sctp_output(sctp_t *sctp, uint_t num_pkt)
9670Sstevel@tonic-gate {
9680Sstevel@tonic-gate 	mblk_t			*mp = NULL;
9690Sstevel@tonic-gate 	mblk_t			*nmp;
9700Sstevel@tonic-gate 	mblk_t			*head;
9710Sstevel@tonic-gate 	mblk_t			*meta = sctp->sctp_xmit_tail;
9720Sstevel@tonic-gate 	mblk_t			*fill = NULL;
9730Sstevel@tonic-gate 	uint16_t 		chunklen;
9740Sstevel@tonic-gate 	uint32_t 		cansend;
9750Sstevel@tonic-gate 	int32_t			seglen;
9760Sstevel@tonic-gate 	int32_t			xtralen;
9770Sstevel@tonic-gate 	int32_t			sacklen;
9780Sstevel@tonic-gate 	int32_t			pad = 0;
9790Sstevel@tonic-gate 	int32_t			pathmax;
9800Sstevel@tonic-gate 	int			extra;
9810Sstevel@tonic-gate 	int64_t			now = lbolt64;
9820Sstevel@tonic-gate 	sctp_faddr_t		*fp;
9830Sstevel@tonic-gate 	sctp_faddr_t		*lfp;
9840Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
9850Sstevel@tonic-gate 	int			error;
986252Svi117747 	boolean_t		notsent = B_TRUE;
9873795Skcpoon 	sctp_stack_t		*sctps = sctp->sctp_sctps;
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
9900Sstevel@tonic-gate 		sacklen = 0;
9910Sstevel@tonic-gate 	} else {
9920Sstevel@tonic-gate 		/* send a SACK chunk */
9930Sstevel@tonic-gate 		sacklen = sizeof (sctp_chunk_hdr_t) +
9940Sstevel@tonic-gate 		    sizeof (sctp_sack_chunk_t) +
9950Sstevel@tonic-gate 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
9960Sstevel@tonic-gate 		lfp = sctp->sctp_lastdata;
9970Sstevel@tonic-gate 		ASSERT(lfp != NULL);
9980Sstevel@tonic-gate 		if (lfp->state != SCTP_FADDRS_ALIVE)
9990Sstevel@tonic-gate 			lfp = sctp->sctp_current;
10000Sstevel@tonic-gate 	}
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	cansend = sctp->sctp_frwnd;
10030Sstevel@tonic-gate 	if (sctp->sctp_unsent < cansend)
10040Sstevel@tonic-gate 		cansend = sctp->sctp_unsent;
1005*8222SGeorge.Shepherd@Sun.COM 
1006*8222SGeorge.Shepherd@Sun.COM 	/*
1007*8222SGeorge.Shepherd@Sun.COM 	 * Start persist timer if unable to send or when
1008*8222SGeorge.Shepherd@Sun.COM 	 * trying to send into a zero window. This timer
1009*8222SGeorge.Shepherd@Sun.COM 	 * ensures the blocked send attempt is retried.
1010*8222SGeorge.Shepherd@Sun.COM 	 */
10110Sstevel@tonic-gate 	if ((cansend < sctp->sctp_current->sfa_pmss / 2) &&
1012*8222SGeorge.Shepherd@Sun.COM 	    (sctp->sctp_unacked != 0) &&
10130Sstevel@tonic-gate 	    (sctp->sctp_unacked < sctp->sctp_current->sfa_pmss) &&
1014*8222SGeorge.Shepherd@Sun.COM 	    !sctp->sctp_ndelay ||
1015*8222SGeorge.Shepherd@Sun.COM 	    (cansend == 0 && sctp->sctp_unacked == 0 &&
1016*8222SGeorge.Shepherd@Sun.COM 	    sctp->sctp_unsent != 0)) {
10170Sstevel@tonic-gate 		head = NULL;
10180Sstevel@tonic-gate 		fp = sctp->sctp_current;
10190Sstevel@tonic-gate 		goto unsent_data;
10200Sstevel@tonic-gate 	}
10210Sstevel@tonic-gate 	if (meta != NULL)
10220Sstevel@tonic-gate 		mp = meta->b_cont;
10233795Skcpoon 	while (cansend > 0 && num_pkt-- != 0) {
10240Sstevel@tonic-gate 		pad = 0;
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 		/*
10270Sstevel@tonic-gate 		 * Find first segment eligible for transmit.
10280Sstevel@tonic-gate 		 */
10290Sstevel@tonic-gate 		while (mp != NULL) {
10300Sstevel@tonic-gate 			if (SCTP_CHUNK_CANSEND(mp))
10310Sstevel@tonic-gate 				break;
10320Sstevel@tonic-gate 			mp = mp->b_next;
10330Sstevel@tonic-gate 		}
10340Sstevel@tonic-gate 		if (mp == NULL) {
10350Sstevel@tonic-gate 			meta = sctp_get_msg_to_send(sctp, &mp,
10360Sstevel@tonic-gate 			    meta == NULL ? NULL : meta->b_next, &error, sacklen,
10370Sstevel@tonic-gate 			    cansend, NULL);
10380Sstevel@tonic-gate 			if (error != 0 || meta == NULL) {
10390Sstevel@tonic-gate 				head = NULL;
10400Sstevel@tonic-gate 				fp = sctp->sctp_current;
10410Sstevel@tonic-gate 				goto unsent_data;
10420Sstevel@tonic-gate 			}
10430Sstevel@tonic-gate 			sctp->sctp_xmit_tail =  meta;
10440Sstevel@tonic-gate 		}
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
10470Sstevel@tonic-gate 		seglen = ntohs(sdc->sdh_len);
10480Sstevel@tonic-gate 		xtralen = sizeof (*sdc);
10490Sstevel@tonic-gate 		chunklen = seglen - xtralen;
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 		/*
10520Sstevel@tonic-gate 		 * Check rwnd.
10530Sstevel@tonic-gate 		 */
10540Sstevel@tonic-gate 		if (chunklen > cansend) {
10550Sstevel@tonic-gate 			head = NULL;
10560Sstevel@tonic-gate 			fp = SCTP_CHUNK_DEST(meta);
10570Sstevel@tonic-gate 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
10580Sstevel@tonic-gate 				fp = sctp->sctp_current;
10590Sstevel@tonic-gate 			goto unsent_data;
10600Sstevel@tonic-gate 		}
10610Sstevel@tonic-gate 		if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
10620Sstevel@tonic-gate 			extra = SCTP_ALIGN - extra;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 		/*
10650Sstevel@tonic-gate 		 * Pick destination address, and check cwnd.
10660Sstevel@tonic-gate 		 */
10670Sstevel@tonic-gate 		if (sacklen > 0 && (seglen + extra <= lfp->cwnd - lfp->suna) &&
10680Sstevel@tonic-gate 		    (seglen + sacklen + extra <= lfp->sfa_pmss)) {
10690Sstevel@tonic-gate 			/*
10700Sstevel@tonic-gate 			 * Only include SACK chunk if it can be bundled
10710Sstevel@tonic-gate 			 * with a data chunk, and sent to sctp_lastdata.
10720Sstevel@tonic-gate 			 */
10730Sstevel@tonic-gate 			pathmax = lfp->cwnd - lfp->suna;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 			fp = lfp;
10760Sstevel@tonic-gate 			if ((nmp = dupmsg(mp)) == NULL) {
10770Sstevel@tonic-gate 				head = NULL;
10780Sstevel@tonic-gate 				goto unsent_data;
10790Sstevel@tonic-gate 			}
10800Sstevel@tonic-gate 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1081252Svi117747 			head = sctp_add_proto_hdr(sctp, fp, nmp, sacklen,
1082252Svi117747 			    &error);
10830Sstevel@tonic-gate 			if (head == NULL) {
1084252Svi117747 				/*
1085252Svi117747 				 * If none of the source addresses are
1086252Svi117747 				 * available (i.e error == EHOSTUNREACH),
1087252Svi117747 				 * pretend we have sent the data. We will
1088252Svi117747 				 * eventually time out trying to retramsmit
1089252Svi117747 				 * the data if the interface never comes up.
1090252Svi117747 				 * If we have already sent some stuff (i.e.,
1091252Svi117747 				 * notsent is B_FALSE) then we are fine, else
1092252Svi117747 				 * just mark this packet as sent.
1093252Svi117747 				 */
1094252Svi117747 				if (notsent && error == EHOSTUNREACH) {
1095252Svi117747 					SCTP_CHUNK_SENT(sctp, mp, sdc,
1096252Svi117747 					    fp, chunklen, meta);
1097252Svi117747 				}
10980Sstevel@tonic-gate 				freemsg(nmp);
10993448Sdh155122 				SCTP_KSTAT(sctps, sctp_output_failed);
11000Sstevel@tonic-gate 				goto unsent_data;
11010Sstevel@tonic-gate 			}
11020Sstevel@tonic-gate 			seglen += sacklen;
11030Sstevel@tonic-gate 			xtralen += sacklen;
11040Sstevel@tonic-gate 			sacklen = 0;
11050Sstevel@tonic-gate 		} else {
11060Sstevel@tonic-gate 			fp = SCTP_CHUNK_DEST(meta);
11070Sstevel@tonic-gate 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
11080Sstevel@tonic-gate 				fp = sctp->sctp_current;
11090Sstevel@tonic-gate 			/*
11100Sstevel@tonic-gate 			 * If we haven't sent data to this destination for
11110Sstevel@tonic-gate 			 * a while, do slow start again.
11120Sstevel@tonic-gate 			 */
11130Sstevel@tonic-gate 			if (now - fp->lastactive > fp->rto) {
11143795Skcpoon 				SET_CWND(fp, fp->sfa_pmss,
11153795Skcpoon 				    sctps->sctps_slow_start_after_idle);
11160Sstevel@tonic-gate 			}
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 			pathmax = fp->cwnd - fp->suna;
11190Sstevel@tonic-gate 			if (seglen + extra > pathmax) {
11200Sstevel@tonic-gate 				head = NULL;
11210Sstevel@tonic-gate 				goto unsent_data;
11220Sstevel@tonic-gate 			}
11230Sstevel@tonic-gate 			if ((nmp = dupmsg(mp)) == NULL) {
11240Sstevel@tonic-gate 				head = NULL;
11250Sstevel@tonic-gate 				goto unsent_data;
11260Sstevel@tonic-gate 			}
11270Sstevel@tonic-gate 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1128252Svi117747 			head = sctp_add_proto_hdr(sctp, fp, nmp, 0, &error);
11290Sstevel@tonic-gate 			if (head == NULL) {
1130252Svi117747 				/*
1131252Svi117747 				 * If none of the source addresses are
1132252Svi117747 				 * available (i.e error == EHOSTUNREACH),
1133252Svi117747 				 * pretend we have sent the data. We will
1134252Svi117747 				 * eventually time out trying to retramsmit
1135252Svi117747 				 * the data if the interface never comes up.
1136252Svi117747 				 * If we have already sent some stuff (i.e.,
1137252Svi117747 				 * notsent is B_FALSE) then we are fine, else
1138252Svi117747 				 * just mark this packet as sent.
1139252Svi117747 				 */
1140252Svi117747 				if (notsent && error == EHOSTUNREACH) {
1141252Svi117747 					SCTP_CHUNK_SENT(sctp, mp, sdc,
1142252Svi117747 					    fp, chunklen, meta);
1143252Svi117747 				}
11440Sstevel@tonic-gate 				freemsg(nmp);
11453448Sdh155122 				SCTP_KSTAT(sctps, sctp_output_failed);
11460Sstevel@tonic-gate 				goto unsent_data;
11470Sstevel@tonic-gate 			}
11480Sstevel@tonic-gate 		}
11491735Skcpoon 		fp->lastactive = now;
11500Sstevel@tonic-gate 		if (pathmax > fp->sfa_pmss)
11510Sstevel@tonic-gate 			pathmax = fp->sfa_pmss;
11520Sstevel@tonic-gate 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
11530Sstevel@tonic-gate 		mp = mp->b_next;
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 		/* Use this chunk to measure RTT? */
11560Sstevel@tonic-gate 		if (sctp->sctp_out_time == 0) {
11570Sstevel@tonic-gate 			sctp->sctp_out_time = now;
11580Sstevel@tonic-gate 			sctp->sctp_rtt_tsn = sctp->sctp_ltsn - 1;
11591735Skcpoon 			ASSERT(sctp->sctp_rtt_tsn == ntohl(sdc->sdh_tsn));
11600Sstevel@tonic-gate 		}
11610Sstevel@tonic-gate 		if (extra > 0) {
11624691Skcpoon 			fill = sctp_get_padding(sctp, extra);
11630Sstevel@tonic-gate 			if (fill != NULL) {
11640Sstevel@tonic-gate 				linkb(head, fill);
11650Sstevel@tonic-gate 				pad = extra;
11660Sstevel@tonic-gate 				seglen += extra;
11670Sstevel@tonic-gate 			} else {
11680Sstevel@tonic-gate 				goto unsent_data;
11690Sstevel@tonic-gate 			}
11700Sstevel@tonic-gate 		}
11710Sstevel@tonic-gate 		/* See if we can bundle more. */
11720Sstevel@tonic-gate 		while (seglen < pathmax) {
11730Sstevel@tonic-gate 			int32_t		new_len;
11740Sstevel@tonic-gate 			int32_t		new_xtralen;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 			while (mp != NULL) {
11770Sstevel@tonic-gate 				if (SCTP_CHUNK_CANSEND(mp))
11780Sstevel@tonic-gate 					break;
11790Sstevel@tonic-gate 				mp = mp->b_next;
11800Sstevel@tonic-gate 			}
11810Sstevel@tonic-gate 			if (mp == NULL) {
11820Sstevel@tonic-gate 				meta = sctp_get_msg_to_send(sctp, &mp,
11830Sstevel@tonic-gate 				    meta->b_next, &error, seglen,
11840Sstevel@tonic-gate 				    (seglen - xtralen) >= cansend ? 0 :
11850Sstevel@tonic-gate 				    cansend - seglen, fp);
11860Sstevel@tonic-gate 				if (error != 0 || meta == NULL)
11870Sstevel@tonic-gate 					break;
11880Sstevel@tonic-gate 				sctp->sctp_xmit_tail =  meta;
11890Sstevel@tonic-gate 			}
11900Sstevel@tonic-gate 			ASSERT(mp != NULL);
11910Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp) && SCTP_CHUNK_DEST(meta) &&
11920Sstevel@tonic-gate 			    fp != SCTP_CHUNK_DEST(meta)) {
11930Sstevel@tonic-gate 				break;
11940Sstevel@tonic-gate 			}
11950Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
11960Sstevel@tonic-gate 			chunklen = ntohs(sdc->sdh_len);
11970Sstevel@tonic-gate 			if ((extra = chunklen  & (SCTP_ALIGN - 1)) != 0)
11980Sstevel@tonic-gate 				extra = SCTP_ALIGN - extra;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 			new_len = seglen + chunklen;
12010Sstevel@tonic-gate 			new_xtralen = xtralen + sizeof (*sdc);
12020Sstevel@tonic-gate 			chunklen -= sizeof (*sdc);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 			if (new_len - new_xtralen > cansend ||
12050Sstevel@tonic-gate 			    new_len + extra > pathmax) {
12060Sstevel@tonic-gate 				break;
12070Sstevel@tonic-gate 			}
12080Sstevel@tonic-gate 			if ((nmp = dupmsg(mp)) == NULL)
12090Sstevel@tonic-gate 				break;
12100Sstevel@tonic-gate 			if (extra > 0) {
12114691Skcpoon 				fill = sctp_get_padding(sctp, extra);
12120Sstevel@tonic-gate 				if (fill != NULL) {
12130Sstevel@tonic-gate 					pad += extra;
12140Sstevel@tonic-gate 					new_len += extra;
12150Sstevel@tonic-gate 					linkb(nmp, fill);
12160Sstevel@tonic-gate 				} else {
12170Sstevel@tonic-gate 					freemsg(nmp);
12180Sstevel@tonic-gate 					break;
12190Sstevel@tonic-gate 				}
12200Sstevel@tonic-gate 			}
12210Sstevel@tonic-gate 			seglen = new_len;
12220Sstevel@tonic-gate 			xtralen = new_xtralen;
12230Sstevel@tonic-gate 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
12240Sstevel@tonic-gate 			SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
12250Sstevel@tonic-gate 			linkb(head, nmp);
12260Sstevel@tonic-gate 			mp = mp->b_next;
12270Sstevel@tonic-gate 		}
12280Sstevel@tonic-gate 		if ((seglen > fp->sfa_pmss) && fp->isv4) {
12290Sstevel@tonic-gate 			ipha_t *iph = (ipha_t *)head->b_rptr;
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 			/*
12320Sstevel@tonic-gate 			 * Path MTU is different from what we thought it would
12330Sstevel@tonic-gate 			 * be when we created chunks, or IP headers have grown.
12340Sstevel@tonic-gate 			 * Need to clear the DF bit.
12350Sstevel@tonic-gate 			 */
12360Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = 0;
12370Sstevel@tonic-gate 		}
12380Sstevel@tonic-gate 		/* xmit segment */
12390Sstevel@tonic-gate 		ASSERT(cansend >= seglen - pad - xtralen);
12400Sstevel@tonic-gate 		cansend -= (seglen - pad - xtralen);
12410Sstevel@tonic-gate 		dprint(2, ("sctp_output: Sending packet %d bytes, tsn %x "
12421676Sjpk 		    "ssn %d to %p (rwnd %d, cansend %d, lastack_rxd %x)\n",
12431676Sjpk 		    seglen - xtralen, ntohl(sdc->sdh_tsn),
12441676Sjpk 		    ntohs(sdc->sdh_ssn), (void *)fp, sctp->sctp_frwnd,
12451676Sjpk 		    cansend, sctp->sctp_lastack_rxd));
12460Sstevel@tonic-gate 		sctp_set_iplen(sctp, head);
12470Sstevel@tonic-gate 		sctp_add_sendq(sctp, head);
12480Sstevel@tonic-gate 		/* arm rto timer (if not set) */
12490Sstevel@tonic-gate 		if (!fp->timer_running)
12500Sstevel@tonic-gate 			SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1251252Svi117747 		notsent = B_FALSE;
12520Sstevel@tonic-gate 	}
12530Sstevel@tonic-gate 	sctp->sctp_active = now;
12540Sstevel@tonic-gate 	return;
12550Sstevel@tonic-gate unsent_data:
12560Sstevel@tonic-gate 	/* arm persist timer (if rto timer not set) */
12570Sstevel@tonic-gate 	if (!fp->timer_running)
12580Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
12590Sstevel@tonic-gate 	if (head != NULL)
12600Sstevel@tonic-gate 		freemsg(head);
12610Sstevel@tonic-gate }
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate /*
12640Sstevel@tonic-gate  * The following two functions initialize and destroy the cache
12650Sstevel@tonic-gate  * associated with the sets used for PR-SCTP.
12660Sstevel@tonic-gate  */
12670Sstevel@tonic-gate void
12680Sstevel@tonic-gate sctp_ftsn_sets_init(void)
12690Sstevel@tonic-gate {
12700Sstevel@tonic-gate 	sctp_kmem_ftsn_set_cache = kmem_cache_create("sctp_ftsn_set_cache",
12710Sstevel@tonic-gate 	    sizeof (sctp_ftsn_set_t), 0, NULL, NULL, NULL, NULL,
12720Sstevel@tonic-gate 	    NULL, 0);
12730Sstevel@tonic-gate }
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate void
12760Sstevel@tonic-gate sctp_ftsn_sets_fini(void)
12770Sstevel@tonic-gate {
12780Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_ftsn_set_cache);
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate /* Free PR-SCTP sets */
12830Sstevel@tonic-gate void
12840Sstevel@tonic-gate sctp_free_ftsn_set(sctp_ftsn_set_t *s)
12850Sstevel@tonic-gate {
12860Sstevel@tonic-gate 	sctp_ftsn_set_t *p;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	while (s != NULL) {
12890Sstevel@tonic-gate 		p = s->next;
12900Sstevel@tonic-gate 		s->next = NULL;
12910Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_ftsn_set_cache, s);
12920Sstevel@tonic-gate 		s = p;
12930Sstevel@tonic-gate 	}
12940Sstevel@tonic-gate }
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate /*
12970Sstevel@tonic-gate  * Given a message meta block, meta, this routine creates or modifies
12980Sstevel@tonic-gate  * the set that will be used to generate a Forward TSN chunk. If the
12990Sstevel@tonic-gate  * entry for stream id, sid, for this message already exists, the
13000Sstevel@tonic-gate  * sequence number, ssn, is updated if it is greater than the existing
13010Sstevel@tonic-gate  * one. If an entry for this sid does not exist, one is created if
13020Sstevel@tonic-gate  * the size does not exceed fp->sfa_pmss. We return false in case
13030Sstevel@tonic-gate  * or an error.
13040Sstevel@tonic-gate  */
13050Sstevel@tonic-gate boolean_t
13060Sstevel@tonic-gate sctp_add_ftsn_set(sctp_ftsn_set_t **s, sctp_faddr_t *fp, mblk_t *meta,
13070Sstevel@tonic-gate     uint_t *nsets, uint32_t *slen)
13080Sstevel@tonic-gate {
13090Sstevel@tonic-gate 	sctp_ftsn_set_t		*p;
13100Sstevel@tonic-gate 	sctp_msg_hdr_t		*msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
13110Sstevel@tonic-gate 	uint16_t		sid = htons(msg_hdr->smh_sid);
13120Sstevel@tonic-gate 	/* msg_hdr->smh_ssn is already in NBO */
13130Sstevel@tonic-gate 	uint16_t		ssn = msg_hdr->smh_ssn;
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	ASSERT(s != NULL && nsets != NULL);
13160Sstevel@tonic-gate 	ASSERT((*nsets == 0 && *s == NULL) || (*nsets > 0 && *s != NULL));
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	if (*s == NULL) {
13190Sstevel@tonic-gate 		ASSERT((*slen + sizeof (uint32_t)) <= fp->sfa_pmss);
13200Sstevel@tonic-gate 		*s = kmem_cache_alloc(sctp_kmem_ftsn_set_cache, KM_NOSLEEP);
13210Sstevel@tonic-gate 		if (*s == NULL)
13220Sstevel@tonic-gate 			return (B_FALSE);
13230Sstevel@tonic-gate 		(*s)->ftsn_entries.ftsn_sid = sid;
13240Sstevel@tonic-gate 		(*s)->ftsn_entries.ftsn_ssn = ssn;
13250Sstevel@tonic-gate 		(*s)->next = NULL;
13260Sstevel@tonic-gate 		*nsets = 1;
13270Sstevel@tonic-gate 		*slen += sizeof (uint32_t);
13280Sstevel@tonic-gate 		return (B_TRUE);
13290Sstevel@tonic-gate 	}
13300Sstevel@tonic-gate 	for (p = *s; p->next != NULL; p = p->next) {
13310Sstevel@tonic-gate 		if (p->ftsn_entries.ftsn_sid == sid) {
13320Sstevel@tonic-gate 			if (SSN_GT(ssn, p->ftsn_entries.ftsn_ssn))
13330Sstevel@tonic-gate 				p->ftsn_entries.ftsn_ssn = ssn;
13340Sstevel@tonic-gate 			return (B_TRUE);
13350Sstevel@tonic-gate 		}
13360Sstevel@tonic-gate 	}
13370Sstevel@tonic-gate 	/* the last one */
13380Sstevel@tonic-gate 	if (p->ftsn_entries.ftsn_sid == sid) {
13390Sstevel@tonic-gate 		if (SSN_GT(ssn, p->ftsn_entries.ftsn_ssn))
13400Sstevel@tonic-gate 			p->ftsn_entries.ftsn_ssn = ssn;
13410Sstevel@tonic-gate 	} else {
13420Sstevel@tonic-gate 		if ((*slen + sizeof (uint32_t)) > fp->sfa_pmss)
13430Sstevel@tonic-gate 			return (B_FALSE);
13440Sstevel@tonic-gate 		p->next = kmem_cache_alloc(sctp_kmem_ftsn_set_cache,
13450Sstevel@tonic-gate 		    KM_NOSLEEP);
13460Sstevel@tonic-gate 		if (p->next == NULL)
13470Sstevel@tonic-gate 			return (B_FALSE);
13480Sstevel@tonic-gate 		p = p->next;
13490Sstevel@tonic-gate 		p->ftsn_entries.ftsn_sid = sid;
13500Sstevel@tonic-gate 		p->ftsn_entries.ftsn_ssn = ssn;
13510Sstevel@tonic-gate 		p->next = NULL;
13520Sstevel@tonic-gate 		(*nsets)++;
13530Sstevel@tonic-gate 		*slen += sizeof (uint32_t);
13540Sstevel@tonic-gate 	}
13550Sstevel@tonic-gate 	return (B_TRUE);
13560Sstevel@tonic-gate }
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate /*
13590Sstevel@tonic-gate  * Given a set of stream id - sequence number pairs, this routing creates
13600Sstevel@tonic-gate  * a Forward TSN chunk. The cumulative TSN (advanced peer ack point)
13610Sstevel@tonic-gate  * for the chunk is obtained from sctp->sctp_adv_pap. The caller
13620Sstevel@tonic-gate  * will add the IP/SCTP header.
13630Sstevel@tonic-gate  */
13640Sstevel@tonic-gate mblk_t *
13650Sstevel@tonic-gate sctp_make_ftsn_chunk(sctp_t *sctp, sctp_faddr_t *fp, sctp_ftsn_set_t *sets,
13660Sstevel@tonic-gate     uint_t nsets, uint32_t seglen)
13670Sstevel@tonic-gate {
13680Sstevel@tonic-gate 	mblk_t			*ftsn_mp;
13690Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ch_hdr;
13700Sstevel@tonic-gate 	uint32_t		*advtsn;
13710Sstevel@tonic-gate 	uint16_t		schlen;
13720Sstevel@tonic-gate 	size_t			xtralen;
13730Sstevel@tonic-gate 	ftsn_entry_t		*ftsn_entry;
13743448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	seglen += sizeof (sctp_chunk_hdr_t);
13770Sstevel@tonic-gate 	if (fp->isv4)
13783448Sdh155122 		xtralen = sctp->sctp_hdr_len + sctps->sctps_wroff_xtra;
13790Sstevel@tonic-gate 	else
13803448Sdh155122 		xtralen = sctp->sctp_hdr6_len + sctps->sctps_wroff_xtra;
13811676Sjpk 	ftsn_mp = allocb_cred(xtralen + seglen, CONN_CRED(sctp->sctp_connp));
13820Sstevel@tonic-gate 	if (ftsn_mp == NULL)
13830Sstevel@tonic-gate 		return (NULL);
13840Sstevel@tonic-gate 	ftsn_mp->b_rptr += xtralen;
13850Sstevel@tonic-gate 	ftsn_mp->b_wptr = ftsn_mp->b_rptr + seglen;
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	ch_hdr = (sctp_chunk_hdr_t *)ftsn_mp->b_rptr;
13880Sstevel@tonic-gate 	ch_hdr->sch_id = CHUNK_FORWARD_TSN;
13890Sstevel@tonic-gate 	ch_hdr->sch_flags = 0;
13900Sstevel@tonic-gate 	/*
13910Sstevel@tonic-gate 	 * The cast here should not be an issue since seglen is
13920Sstevel@tonic-gate 	 * the length of the Forward TSN chunk.
13930Sstevel@tonic-gate 	 */
13940Sstevel@tonic-gate 	schlen = (uint16_t)seglen;
13950Sstevel@tonic-gate 	U16_TO_ABE16(schlen, &(ch_hdr->sch_len));
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	advtsn = (uint32_t *)(ch_hdr + 1);
13980Sstevel@tonic-gate 	U32_TO_ABE32(sctp->sctp_adv_pap, advtsn);
13990Sstevel@tonic-gate 	ftsn_entry = (ftsn_entry_t *)(advtsn + 1);
14000Sstevel@tonic-gate 	while (nsets > 0) {
14010Sstevel@tonic-gate 		ASSERT((uchar_t *)&ftsn_entry[1] <= ftsn_mp->b_wptr);
14020Sstevel@tonic-gate 		ftsn_entry->ftsn_sid = sets->ftsn_entries.ftsn_sid;
14030Sstevel@tonic-gate 		ftsn_entry->ftsn_ssn = sets->ftsn_entries.ftsn_ssn;
14040Sstevel@tonic-gate 		ftsn_entry++;
14050Sstevel@tonic-gate 		sets = sets->next;
14060Sstevel@tonic-gate 		nsets--;
14070Sstevel@tonic-gate 	}
14080Sstevel@tonic-gate 	return (ftsn_mp);
14090Sstevel@tonic-gate }
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate /*
14120Sstevel@tonic-gate  * Given a starting message, the routine steps through all the
14130Sstevel@tonic-gate  * messages whose TSN is less than sctp->sctp_adv_pap and creates
14140Sstevel@tonic-gate  * ftsn sets. The ftsn sets is then used to create an Forward TSN
14150Sstevel@tonic-gate  * chunk. All the messages, that have chunks that are included in the
14160Sstevel@tonic-gate  * ftsn sets, are flagged abandonded. If a message is partially sent
14170Sstevel@tonic-gate  * and is deemed abandoned, all remaining unsent chunks are marked
14180Sstevel@tonic-gate  * abandoned and are deducted from sctp_unsent.
14190Sstevel@tonic-gate  */
14200Sstevel@tonic-gate void
14210Sstevel@tonic-gate sctp_make_ftsns(sctp_t *sctp, mblk_t *meta, mblk_t *mp, mblk_t **nmp,
14220Sstevel@tonic-gate     sctp_faddr_t *fp, uint32_t *seglen)
14230Sstevel@tonic-gate {
14240Sstevel@tonic-gate 	mblk_t		*mp1 = mp;
14250Sstevel@tonic-gate 	mblk_t		*mp_head = mp;
14260Sstevel@tonic-gate 	mblk_t		*meta_head = meta;
14270Sstevel@tonic-gate 	mblk_t		*head;
14280Sstevel@tonic-gate 	sctp_ftsn_set_t	*sets = NULL;
14290Sstevel@tonic-gate 	uint_t		nsets = 0;
14300Sstevel@tonic-gate 	uint16_t	clen;
14310Sstevel@tonic-gate 	sctp_data_hdr_t	*sdc;
14320Sstevel@tonic-gate 	uint32_t	sacklen;
14330Sstevel@tonic-gate 	uint32_t	adv_pap = sctp->sctp_adv_pap;
14340Sstevel@tonic-gate 	uint32_t	unsent = 0;
14350Sstevel@tonic-gate 	boolean_t	ubit;
14363448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	*seglen = sizeof (uint32_t);
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
14410Sstevel@tonic-gate 	while (meta != NULL &&
14420Sstevel@tonic-gate 	    SEQ_GEQ(sctp->sctp_adv_pap, ntohl(sdc->sdh_tsn))) {
14430Sstevel@tonic-gate 		/*
14440Sstevel@tonic-gate 		 * Skip adding FTSN sets for un-ordered messages as they do
14450Sstevel@tonic-gate 		 * not have SSNs.
14460Sstevel@tonic-gate 		 */
14470Sstevel@tonic-gate 		ubit = SCTP_DATA_GET_UBIT(sdc);
14480Sstevel@tonic-gate 		if (!ubit &&
14490Sstevel@tonic-gate 		    !sctp_add_ftsn_set(&sets, fp, meta, &nsets, seglen)) {
14500Sstevel@tonic-gate 			meta = NULL;
14510Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
14520Sstevel@tonic-gate 			goto ftsn_done;
14530Sstevel@tonic-gate 		}
14540Sstevel@tonic-gate 		while (mp1 != NULL && SCTP_CHUNK_ISSENT(mp1)) {
14550Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
14560Sstevel@tonic-gate 			adv_pap = ntohl(sdc->sdh_tsn);
14570Sstevel@tonic-gate 			mp1 = mp1->b_next;
14580Sstevel@tonic-gate 		}
14590Sstevel@tonic-gate 		meta = meta->b_next;
14600Sstevel@tonic-gate 		if (meta != NULL) {
14610Sstevel@tonic-gate 			mp1 = meta->b_cont;
14620Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp1))
14630Sstevel@tonic-gate 				break;
14640Sstevel@tonic-gate 			sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
14650Sstevel@tonic-gate 		}
14660Sstevel@tonic-gate 	}
14670Sstevel@tonic-gate ftsn_done:
14680Sstevel@tonic-gate 	/*
14690Sstevel@tonic-gate 	 * Can't compare with sets == NULL, since we don't add any
14700Sstevel@tonic-gate 	 * sets for un-ordered messages.
14710Sstevel@tonic-gate 	 */
14720Sstevel@tonic-gate 	if (meta == meta_head)
14730Sstevel@tonic-gate 		return;
14740Sstevel@tonic-gate 	*nmp = sctp_make_ftsn_chunk(sctp, fp, sets, nsets, *seglen);
14750Sstevel@tonic-gate 	sctp_free_ftsn_set(sets);
14760Sstevel@tonic-gate 	if (*nmp == NULL)
14770Sstevel@tonic-gate 		return;
14780Sstevel@tonic-gate 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
14790Sstevel@tonic-gate 		sacklen = 0;
14800Sstevel@tonic-gate 	} else {
14810Sstevel@tonic-gate 		sacklen = sizeof (sctp_chunk_hdr_t) +
14820Sstevel@tonic-gate 		    sizeof (sctp_sack_chunk_t) +
14830Sstevel@tonic-gate 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
14840Sstevel@tonic-gate 		if (*seglen + sacklen > sctp->sctp_lastdata->sfa_pmss) {
14850Sstevel@tonic-gate 			/* piggybacked SACK doesn't fit */
14860Sstevel@tonic-gate 			sacklen = 0;
14870Sstevel@tonic-gate 		} else {
14880Sstevel@tonic-gate 			fp = sctp->sctp_lastdata;
14890Sstevel@tonic-gate 		}
14900Sstevel@tonic-gate 	}
1491252Svi117747 	head = sctp_add_proto_hdr(sctp, fp, *nmp, sacklen, NULL);
14920Sstevel@tonic-gate 	if (head == NULL) {
14930Sstevel@tonic-gate 		freemsg(*nmp);
14940Sstevel@tonic-gate 		*nmp = NULL;
14953448Sdh155122 		SCTP_KSTAT(sctps, sctp_send_ftsn_failed);
14960Sstevel@tonic-gate 		return;
14970Sstevel@tonic-gate 	}
14980Sstevel@tonic-gate 	*seglen += sacklen;
14990Sstevel@tonic-gate 	*nmp = head;
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	/*
15020Sstevel@tonic-gate 	 * XXXNeed to optimise this, the reason it is done here is so
15030Sstevel@tonic-gate 	 * that we don't have to undo in case of failure.
15040Sstevel@tonic-gate 	 */
15050Sstevel@tonic-gate 	mp1 = mp_head;
15060Sstevel@tonic-gate 	sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
15070Sstevel@tonic-gate 	while (meta_head != NULL &&
15080Sstevel@tonic-gate 	    SEQ_GEQ(sctp->sctp_adv_pap, ntohl(sdc->sdh_tsn))) {
15090Sstevel@tonic-gate 		if (!SCTP_IS_MSG_ABANDONED(meta_head))
15100Sstevel@tonic-gate 			SCTP_MSG_SET_ABANDONED(meta_head);
15110Sstevel@tonic-gate 		while (mp1 != NULL && SCTP_CHUNK_ISSENT(mp1)) {
15120Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
15130Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISACKED(mp1)) {
15140Sstevel@tonic-gate 				clen = ntohs(sdc->sdh_len) - sizeof (*sdc);
15150Sstevel@tonic-gate 				SCTP_CHUNK_SENT(sctp, mp1, sdc, fp, clen,
15160Sstevel@tonic-gate 				    meta_head);
15170Sstevel@tonic-gate 			}
15180Sstevel@tonic-gate 			mp1 = mp1->b_next;
15190Sstevel@tonic-gate 		}
15200Sstevel@tonic-gate 		while (mp1 != NULL) {
15210Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
15220Sstevel@tonic-gate 			if (!SCTP_CHUNK_ABANDONED(mp1)) {
15230Sstevel@tonic-gate 				ASSERT(!SCTP_CHUNK_ISSENT(mp1));
15240Sstevel@tonic-gate 				unsent += ntohs(sdc->sdh_len) - sizeof (*sdc);
15250Sstevel@tonic-gate 				SCTP_ABANDON_CHUNK(mp1);
15260Sstevel@tonic-gate 			}
15270Sstevel@tonic-gate 			mp1 = mp1->b_next;
15280Sstevel@tonic-gate 		}
15290Sstevel@tonic-gate 		meta_head = meta_head->b_next;
15300Sstevel@tonic-gate 		if (meta_head != NULL) {
15310Sstevel@tonic-gate 			mp1 = meta_head->b_cont;
15320Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp1))
15330Sstevel@tonic-gate 				break;
15340Sstevel@tonic-gate 			sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
15350Sstevel@tonic-gate 		}
15360Sstevel@tonic-gate 	}
15370Sstevel@tonic-gate 	if (unsent > 0) {
15380Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent >= unsent);
15390Sstevel@tonic-gate 		sctp->sctp_unsent -= unsent;
15400Sstevel@tonic-gate 		/*
15410Sstevel@tonic-gate 		 * Update ULP the amount of queued data, which is
15420Sstevel@tonic-gate 		 * sent-unack'ed + unsent.
15430Sstevel@tonic-gate 		 */
15440Sstevel@tonic-gate 		if (!SCTP_IS_DETACHED(sctp)) {
15450Sstevel@tonic-gate 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
15460Sstevel@tonic-gate 			    sctp->sctp_unacked + sctp->sctp_unsent);
15470Sstevel@tonic-gate 		}
15480Sstevel@tonic-gate 	}
15490Sstevel@tonic-gate }
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate /*
15520Sstevel@tonic-gate  * This function steps through messages starting at meta and checks if
15530Sstevel@tonic-gate  * the message is abandoned. It stops when it hits an unsent chunk or
15540Sstevel@tonic-gate  * a message that has all its chunk acked. This is the only place
15550Sstevel@tonic-gate  * where the sctp_adv_pap is moved forward to indicated abandoned
15560Sstevel@tonic-gate  * messages.
15570Sstevel@tonic-gate  */
15580Sstevel@tonic-gate void
15590Sstevel@tonic-gate sctp_check_adv_ack_pt(sctp_t *sctp, mblk_t *meta, mblk_t *mp)
15600Sstevel@tonic-gate {
15610Sstevel@tonic-gate 	uint32_t	tsn = sctp->sctp_adv_pap;
15620Sstevel@tonic-gate 	sctp_data_hdr_t	*sdc;
15630Sstevel@tonic-gate 	sctp_msg_hdr_t	*msg_hdr;
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	ASSERT(mp != NULL);
15660Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
15670Sstevel@tonic-gate 	ASSERT(SEQ_GT(ntohl(sdc->sdh_tsn), sctp->sctp_lastack_rxd));
15680Sstevel@tonic-gate 	msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
15690Sstevel@tonic-gate 	if (!SCTP_IS_MSG_ABANDONED(meta) &&
15700Sstevel@tonic-gate 	    !SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
15710Sstevel@tonic-gate 		return;
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate 	while (meta != NULL) {
15740Sstevel@tonic-gate 		while (mp != NULL && SCTP_CHUNK_ISSENT(mp)) {
15750Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
15760Sstevel@tonic-gate 			tsn = ntohl(sdc->sdh_tsn);
15770Sstevel@tonic-gate 			mp = mp->b_next;
15780Sstevel@tonic-gate 		}
15790Sstevel@tonic-gate 		if (mp != NULL)
15800Sstevel@tonic-gate 			break;
15810Sstevel@tonic-gate 		/*
15820Sstevel@tonic-gate 		 * We continue checking for successive messages only if there
15830Sstevel@tonic-gate 		 * is a chunk marked for retransmission. Else, we might
15840Sstevel@tonic-gate 		 * end up sending FTSN prematurely for chunks that have been
15850Sstevel@tonic-gate 		 * sent, but not yet acked.
15860Sstevel@tonic-gate 		 */
15870Sstevel@tonic-gate 		if ((meta = meta->b_next) != NULL) {
15880Sstevel@tonic-gate 			msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
15890Sstevel@tonic-gate 			if (!SCTP_IS_MSG_ABANDONED(meta) &&
15900Sstevel@tonic-gate 			    !SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
15910Sstevel@tonic-gate 				break;
15920Sstevel@tonic-gate 			}
15930Sstevel@tonic-gate 			for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
15940Sstevel@tonic-gate 				if (!SCTP_CHUNK_ISSENT(mp)) {
15950Sstevel@tonic-gate 					sctp->sctp_adv_pap = tsn;
15960Sstevel@tonic-gate 					return;
15970Sstevel@tonic-gate 				}
15980Sstevel@tonic-gate 				if (SCTP_CHUNK_WANT_REXMIT(mp))
15990Sstevel@tonic-gate 					break;
16000Sstevel@tonic-gate 			}
16010Sstevel@tonic-gate 			if (mp == NULL)
16020Sstevel@tonic-gate 				break;
16030Sstevel@tonic-gate 		}
16040Sstevel@tonic-gate 	}
16050Sstevel@tonic-gate 	sctp->sctp_adv_pap = tsn;
16060Sstevel@tonic-gate }
16070Sstevel@tonic-gate 
16081735Skcpoon 
16091735Skcpoon /*
16101735Skcpoon  * Determine if we should bundle a data chunk with the chunk being
16111735Skcpoon  * retransmitted.  We bundle if
16121735Skcpoon  *
16131735Skcpoon  * - the chunk is sent to the same destination and unack'ed.
16141735Skcpoon  *
16151735Skcpoon  * OR
16161735Skcpoon  *
16171735Skcpoon  * - the chunk is unsent, i.e. new data.
16181735Skcpoon  */
16191735Skcpoon #define	SCTP_CHUNK_RX_CANBUNDLE(mp, fp)					\
16201735Skcpoon 	(!SCTP_CHUNK_ABANDONED((mp)) && 				\
16211735Skcpoon 	((SCTP_CHUNK_ISSENT((mp)) && (SCTP_CHUNK_DEST(mp) == (fp) &&	\
16221735Skcpoon 	!SCTP_CHUNK_ISACKED(mp))) ||					\
16231735Skcpoon 	(((mp)->b_flag & (SCTP_CHUNK_FLAG_REXMIT|SCTP_CHUNK_FLAG_SENT)) != \
16241735Skcpoon 	SCTP_CHUNK_FLAG_SENT)))
16251735Skcpoon 
16260Sstevel@tonic-gate /*
16270Sstevel@tonic-gate  * Retransmit first segment which hasn't been acked with cumtsn or send
16280Sstevel@tonic-gate  * a Forward TSN chunk, if appropriate.
16290Sstevel@tonic-gate  */
16300Sstevel@tonic-gate void
16310Sstevel@tonic-gate sctp_rexmit(sctp_t *sctp, sctp_faddr_t *oldfp)
16320Sstevel@tonic-gate {
16330Sstevel@tonic-gate 	mblk_t		*mp;
16340Sstevel@tonic-gate 	mblk_t		*nmp = NULL;
16350Sstevel@tonic-gate 	mblk_t		*head;
16360Sstevel@tonic-gate 	mblk_t		*meta = sctp->sctp_xmit_head;
16370Sstevel@tonic-gate 	mblk_t		*fill;
16380Sstevel@tonic-gate 	uint32_t	seglen = 0;
16390Sstevel@tonic-gate 	uint32_t	sacklen;
16400Sstevel@tonic-gate 	uint16_t	chunklen;
16410Sstevel@tonic-gate 	int		extra;
16420Sstevel@tonic-gate 	sctp_data_hdr_t	*sdc;
16430Sstevel@tonic-gate 	sctp_faddr_t	*fp;
16440Sstevel@tonic-gate 	uint32_t	adv_pap = sctp->sctp_adv_pap;
16450Sstevel@tonic-gate 	boolean_t	do_ftsn = B_FALSE;
16460Sstevel@tonic-gate 	boolean_t	ftsn_check = B_TRUE;
16471735Skcpoon 	uint32_t	first_ua_tsn;
16481735Skcpoon 	sctp_msg_hdr_t	*mhdr;
16493448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
16508154SGeorge.Shepherd@Sun.COM 	int		error;
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 	while (meta != NULL) {
16530Sstevel@tonic-gate 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
16540Sstevel@tonic-gate 			uint32_t	tsn;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp))
16570Sstevel@tonic-gate 				goto window_probe;
16580Sstevel@tonic-gate 			/*
16590Sstevel@tonic-gate 			 * We break in the following cases -
16600Sstevel@tonic-gate 			 *
16610Sstevel@tonic-gate 			 *	if the advanced peer ack point includes the next
16620Sstevel@tonic-gate 			 *	chunk to be retransmited - possibly the Forward
16630Sstevel@tonic-gate 			 * 	TSN was lost.
16640Sstevel@tonic-gate 			 *
16650Sstevel@tonic-gate 			 *	if we are PRSCTP aware and the next chunk to be
16660Sstevel@tonic-gate 			 *	retransmitted is now abandoned
16670Sstevel@tonic-gate 			 *
16680Sstevel@tonic-gate 			 *	if the next chunk to be retransmitted is for
16690Sstevel@tonic-gate 			 *	the dest on which the timer went off. (this
16700Sstevel@tonic-gate 			 *	message is not abandoned).
16710Sstevel@tonic-gate 			 *
16720Sstevel@tonic-gate 			 * We check for Forward TSN only for the first
16730Sstevel@tonic-gate 			 * eligible chunk to be retransmitted. The reason
16740Sstevel@tonic-gate 			 * being if the first eligible chunk is skipped (say
16750Sstevel@tonic-gate 			 * it was sent to a destination other than oldfp)
16760Sstevel@tonic-gate 			 * then we cannot advance the cum TSN via Forward
16770Sstevel@tonic-gate 			 * TSN chunk.
16780Sstevel@tonic-gate 			 *
16790Sstevel@tonic-gate 			 * Also, ftsn_check is B_TRUE only for the first
16800Sstevel@tonic-gate 			 * eligible chunk, it  will be B_FALSE for all
16810Sstevel@tonic-gate 			 * subsequent candidate messages for retransmission.
16820Sstevel@tonic-gate 			 */
16830Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
16840Sstevel@tonic-gate 			tsn = ntohl(sdc->sdh_tsn);
16850Sstevel@tonic-gate 			if (SEQ_GT(tsn, sctp->sctp_lastack_rxd)) {
16860Sstevel@tonic-gate 				if (sctp->sctp_prsctp_aware && ftsn_check) {
16870Sstevel@tonic-gate 					if (SEQ_GEQ(sctp->sctp_adv_pap, tsn)) {
16880Sstevel@tonic-gate 						ASSERT(sctp->sctp_prsctp_aware);
16890Sstevel@tonic-gate 						do_ftsn = B_TRUE;
16900Sstevel@tonic-gate 						goto out;
16910Sstevel@tonic-gate 					} else {
16920Sstevel@tonic-gate 						sctp_check_adv_ack_pt(sctp,
16930Sstevel@tonic-gate 						    meta, mp);
16940Sstevel@tonic-gate 						if (SEQ_GT(sctp->sctp_adv_pap,
16950Sstevel@tonic-gate 						    adv_pap)) {
16960Sstevel@tonic-gate 							do_ftsn = B_TRUE;
16970Sstevel@tonic-gate 							goto out;
16980Sstevel@tonic-gate 						}
16990Sstevel@tonic-gate 					}
17000Sstevel@tonic-gate 					ftsn_check = B_FALSE;
17010Sstevel@tonic-gate 				}
17020Sstevel@tonic-gate 				if (SCTP_CHUNK_DEST(mp) == oldfp)
17030Sstevel@tonic-gate 					goto out;
17040Sstevel@tonic-gate 			}
17050Sstevel@tonic-gate 		}
17060Sstevel@tonic-gate 		meta = meta->b_next;
17070Sstevel@tonic-gate 		if (meta != NULL && sctp->sctp_prsctp_aware) {
17081735Skcpoon 			mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 			while (meta != NULL && (SCTP_IS_MSG_ABANDONED(meta) ||
17110Sstevel@tonic-gate 			    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr, sctp))) {
17120Sstevel@tonic-gate 				meta = meta->b_next;
17130Sstevel@tonic-gate 			}
17140Sstevel@tonic-gate 		}
17150Sstevel@tonic-gate 	}
17160Sstevel@tonic-gate window_probe:
17170Sstevel@tonic-gate 	/*
17180Sstevel@tonic-gate 	 * Retransmit fired for a destination which didn't have
17190Sstevel@tonic-gate 	 * any unacked data pending.
17200Sstevel@tonic-gate 	 */
17211932Svi117747 	if (sctp->sctp_unacked == 0 && sctp->sctp_unsent != 0) {
17220Sstevel@tonic-gate 		/*
17230Sstevel@tonic-gate 		 * Send a window probe. Inflate frwnd to allow
17240Sstevel@tonic-gate 		 * sending one segment.
17250Sstevel@tonic-gate 		 */
17261932Svi117747 		if (sctp->sctp_frwnd < (oldfp->sfa_pmss - sizeof (*sdc)))
17270Sstevel@tonic-gate 			sctp->sctp_frwnd = oldfp->sfa_pmss - sizeof (*sdc);
17283795Skcpoon 
17291932Svi117747 		/* next TSN to send */
17301932Svi117747 		sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn;
17313795Skcpoon 
17323795Skcpoon 		/*
17333795Skcpoon 		 * The above sctp_frwnd adjustment is coarse.  The "changed"
17343795Skcpoon 		 * sctp_frwnd may allow us to send more than 1 packet.  So
17353795Skcpoon 		 * tell sctp_output() to send only 1 packet.
17363795Skcpoon 		 */
17373795Skcpoon 		sctp_output(sctp, 1);
17383795Skcpoon 
17391932Svi117747 		/* Last sent TSN */
17401932Svi117747 		sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn - 1;
17411932Svi117747 		ASSERT(sctp->sctp_rxt_maxtsn >= sctp->sctp_rxt_nxttsn);
17421932Svi117747 		sctp->sctp_zero_win_probe = B_TRUE;
17433448Sdh155122 		BUMP_MIB(&sctps->sctps_mib, sctpOutWinProbe);
17440Sstevel@tonic-gate 	}
17450Sstevel@tonic-gate 	return;
17460Sstevel@tonic-gate out:
17470Sstevel@tonic-gate 	/*
17483795Skcpoon 	 * After a time out, assume that everything has left the network.  So
17493795Skcpoon 	 * we can clear rxt_unacked for the original peer address.
17503795Skcpoon 	 */
17513795Skcpoon 	oldfp->rxt_unacked = 0;
17523795Skcpoon 
17533795Skcpoon 	/*
17543795Skcpoon 	 * If we were probing for zero window, don't adjust retransmission
17551932Svi117747 	 * variables, but the timer is still backed off.
17561932Svi117747 	 */
17571932Svi117747 	if (sctp->sctp_zero_win_probe) {
17581932Svi117747 		mblk_t	*pkt;
17591932Svi117747 		uint_t	pkt_len;
17601932Svi117747 
17611932Svi117747 		/*
17621932Svi117747 		 * Get the Zero Win Probe for retrasmission, sctp_rxt_nxttsn
17631932Svi117747 		 * and sctp_rxt_maxtsn will specify the ZWP packet.
17641932Svi117747 		 */
17651932Svi117747 		fp = oldfp;
17661932Svi117747 		if (oldfp->state != SCTP_FADDRS_ALIVE)
17671932Svi117747 			fp = sctp_rotate_faddr(sctp, oldfp);
17681932Svi117747 		pkt = sctp_rexmit_packet(sctp, &meta, &mp, fp, &pkt_len);
17691932Svi117747 		if (pkt != NULL) {
17701932Svi117747 			ASSERT(pkt_len <= fp->sfa_pmss);
17711932Svi117747 			sctp_set_iplen(sctp, pkt);
17721932Svi117747 			sctp_add_sendq(sctp, pkt);
17731932Svi117747 		} else {
17743448Sdh155122 			SCTP_KSTAT(sctps, sctp_ss_rexmit_failed);
17751932Svi117747 		}
17763795Skcpoon 
17773795Skcpoon 		/*
17783795Skcpoon 		 * The strikes will be clear by sctp_faddr_alive() when the
17793795Skcpoon 		 * other side sends us an ack.
17803795Skcpoon 		 */
17811932Svi117747 		oldfp->strikes++;
17821932Svi117747 		sctp->sctp_strikes++;
17833795Skcpoon 
17841932Svi117747 		SCTP_CALC_RXT(oldfp, sctp->sctp_rto_max);
17851932Svi117747 		if (oldfp != fp && oldfp->suna != 0)
17861932Svi117747 			SCTP_FADDR_TIMER_RESTART(sctp, oldfp, fp->rto);
17871932Svi117747 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
17883448Sdh155122 		BUMP_MIB(&sctps->sctps_mib, sctpOutWinProbe);
17891932Svi117747 		return;
17901932Svi117747 	}
17911932Svi117747 
17921932Svi117747 	/*
17930Sstevel@tonic-gate 	 * Enter slowstart for this destination
17940Sstevel@tonic-gate 	 */
17950Sstevel@tonic-gate 	oldfp->ssthresh = oldfp->cwnd / 2;
17960Sstevel@tonic-gate 	if (oldfp->ssthresh < 2 * oldfp->sfa_pmss)
17970Sstevel@tonic-gate 		oldfp->ssthresh = 2 * oldfp->sfa_pmss;
17980Sstevel@tonic-gate 	oldfp->cwnd = oldfp->sfa_pmss;
17990Sstevel@tonic-gate 	oldfp->pba = 0;
18000Sstevel@tonic-gate 	fp = sctp_rotate_faddr(sctp, oldfp);
18010Sstevel@tonic-gate 	ASSERT(fp != NULL);
18020Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
18030Sstevel@tonic-gate 
18041735Skcpoon 	first_ua_tsn = ntohl(sdc->sdh_tsn);
18050Sstevel@tonic-gate 	if (do_ftsn) {
18060Sstevel@tonic-gate 		sctp_make_ftsns(sctp, meta, mp, &nmp, fp, &seglen);
18070Sstevel@tonic-gate 		if (nmp == NULL) {
18080Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
18090Sstevel@tonic-gate 			goto restart_timer;
18100Sstevel@tonic-gate 		}
18110Sstevel@tonic-gate 		head = nmp;
18121735Skcpoon 		/*
18131735Skcpoon 		 * Move to the next unabandoned chunk. XXXCheck if meta will
18141735Skcpoon 		 * always be marked abandoned.
18151735Skcpoon 		 */
18161735Skcpoon 		while (meta != NULL && SCTP_IS_MSG_ABANDONED(meta))
18171735Skcpoon 			meta = meta->b_next;
18180Sstevel@tonic-gate 		if (meta != NULL)
18191735Skcpoon 			mp = mp->b_cont;
18201735Skcpoon 		else
18211735Skcpoon 			mp = NULL;
18220Sstevel@tonic-gate 		goto try_bundle;
18230Sstevel@tonic-gate 	}
18240Sstevel@tonic-gate 	seglen = ntohs(sdc->sdh_len);
18250Sstevel@tonic-gate 	chunklen = seglen - sizeof (*sdc);
18260Sstevel@tonic-gate 	if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
18270Sstevel@tonic-gate 		extra = SCTP_ALIGN - extra;
18280Sstevel@tonic-gate 
18291735Skcpoon 	/* Find out if we need to piggyback SACK. */
18301735Skcpoon 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
18311735Skcpoon 		sacklen = 0;
18321735Skcpoon 	} else {
18331735Skcpoon 		sacklen = sizeof (sctp_chunk_hdr_t) +
18341735Skcpoon 		    sizeof (sctp_sack_chunk_t) +
18351735Skcpoon 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
18361735Skcpoon 		if (seglen + sacklen > sctp->sctp_lastdata->sfa_pmss) {
18371735Skcpoon 			/* piggybacked SACK doesn't fit */
18381735Skcpoon 			sacklen = 0;
18391735Skcpoon 		} else {
18401735Skcpoon 			/*
18411735Skcpoon 			 * OK, we have room to send SACK back.  But we
18421735Skcpoon 			 * should send it back to the last fp where we
18431735Skcpoon 			 * receive data from, unless sctp_lastdata equals
18441735Skcpoon 			 * oldfp, then we should probably not send it
18451735Skcpoon 			 * back to that fp.  Also we should check that
18461735Skcpoon 			 * the fp is alive.
18471735Skcpoon 			 */
18481735Skcpoon 			if (sctp->sctp_lastdata != oldfp &&
18491735Skcpoon 			    sctp->sctp_lastdata->state == SCTP_FADDRS_ALIVE) {
18501735Skcpoon 				fp = sctp->sctp_lastdata;
18511735Skcpoon 			}
18521735Skcpoon 		}
18531735Skcpoon 	}
18541735Skcpoon 
18550Sstevel@tonic-gate 	/*
18560Sstevel@tonic-gate 	 * Cancel RTT measurement if the retransmitted TSN is before the
18570Sstevel@tonic-gate 	 * TSN used for timimg.
18580Sstevel@tonic-gate 	 */
18590Sstevel@tonic-gate 	if (sctp->sctp_out_time != 0 &&
18600Sstevel@tonic-gate 	    SEQ_GEQ(sctp->sctp_rtt_tsn, sdc->sdh_tsn)) {
18610Sstevel@tonic-gate 		sctp->sctp_out_time = 0;
18620Sstevel@tonic-gate 	}
18630Sstevel@tonic-gate 	/* Clear the counter as the RTT calculation may be off. */
18640Sstevel@tonic-gate 	fp->rtt_updates = 0;
18651735Skcpoon 	oldfp->rtt_updates = 0;
18660Sstevel@tonic-gate 
18671735Skcpoon 	/*
18681735Skcpoon 	 * After a timeout, we should change the current faddr so that
18691735Skcpoon 	 * new chunks will be sent to the alternate address.
18701735Skcpoon 	 */
18711735Skcpoon 	sctp_set_faddr_current(sctp, fp);
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 	nmp = dupmsg(mp);
18740Sstevel@tonic-gate 	if (nmp == NULL)
18750Sstevel@tonic-gate 		goto restart_timer;
18760Sstevel@tonic-gate 	if (extra > 0) {
18774691Skcpoon 		fill = sctp_get_padding(sctp, extra);
18780Sstevel@tonic-gate 		if (fill != NULL) {
18790Sstevel@tonic-gate 			linkb(nmp, fill);
18800Sstevel@tonic-gate 			seglen += extra;
18810Sstevel@tonic-gate 		} else {
18820Sstevel@tonic-gate 			freemsg(nmp);
18830Sstevel@tonic-gate 			goto restart_timer;
18840Sstevel@tonic-gate 		}
18850Sstevel@tonic-gate 	}
18860Sstevel@tonic-gate 	SCTP_CHUNK_CLEAR_FLAGS(nmp);
1887252Svi117747 	head = sctp_add_proto_hdr(sctp, fp, nmp, sacklen, NULL);
18880Sstevel@tonic-gate 	if (head == NULL) {
18890Sstevel@tonic-gate 		freemsg(nmp);
18903448Sdh155122 		SCTP_KSTAT(sctps, sctp_rexmit_failed);
18910Sstevel@tonic-gate 		goto restart_timer;
18920Sstevel@tonic-gate 	}
18930Sstevel@tonic-gate 	seglen += sacklen;
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 	SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate 	mp = mp->b_next;
18981735Skcpoon 
18990Sstevel@tonic-gate try_bundle:
19003795Skcpoon 	/* We can at least and at most send 1 packet at timeout. */
19010Sstevel@tonic-gate 	while (seglen < fp->sfa_pmss) {
19020Sstevel@tonic-gate 		int32_t new_len;
19030Sstevel@tonic-gate 
19041735Skcpoon 		/* Go through the list to find more chunks to be bundled. */
19050Sstevel@tonic-gate 		while (mp != NULL) {
19061735Skcpoon 			/* Check if the chunk can be bundled. */
19071735Skcpoon 			if (SCTP_CHUNK_RX_CANBUNDLE(mp, oldfp))
19080Sstevel@tonic-gate 				break;
19090Sstevel@tonic-gate 			mp = mp->b_next;
19100Sstevel@tonic-gate 		}
19111735Skcpoon 		/* Go to the next message. */
19120Sstevel@tonic-gate 		if (mp == NULL) {
19131735Skcpoon 			for (meta = meta->b_next; meta != NULL;
19141735Skcpoon 			    meta = meta->b_next) {
19151735Skcpoon 				mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
19161735Skcpoon 
19171735Skcpoon 				if (SCTP_IS_MSG_ABANDONED(meta) ||
19181735Skcpoon 				    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr,
19191735Skcpoon 				    sctp)) {
19201735Skcpoon 					continue;
19211735Skcpoon 				}
19221735Skcpoon 
19231735Skcpoon 				mp = meta->b_cont;
19241735Skcpoon 				goto try_bundle;
19251735Skcpoon 			}
19268154SGeorge.Shepherd@Sun.COM 			/*
19278154SGeorge.Shepherd@Sun.COM 			 * Check if there is a new message which potentially
19288154SGeorge.Shepherd@Sun.COM 			 * could be bundled with this retransmission.
19298154SGeorge.Shepherd@Sun.COM 			 */
19308154SGeorge.Shepherd@Sun.COM 			meta = sctp_get_msg_to_send(sctp, &mp, NULL, &error,
19318154SGeorge.Shepherd@Sun.COM 			    seglen, fp->sfa_pmss - seglen, NULL);
19328154SGeorge.Shepherd@Sun.COM 			if (error != 0 || meta == NULL) {
19338154SGeorge.Shepherd@Sun.COM 				/* No more chunk to be bundled. */
19348154SGeorge.Shepherd@Sun.COM 				break;
19358154SGeorge.Shepherd@Sun.COM 			} else {
19368154SGeorge.Shepherd@Sun.COM 				goto try_bundle;
19378154SGeorge.Shepherd@Sun.COM 			}
19380Sstevel@tonic-gate 		}
19391735Skcpoon 
19400Sstevel@tonic-gate 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
19411735Skcpoon 		new_len = ntohs(sdc->sdh_len);
19421735Skcpoon 		chunklen = new_len - sizeof (*sdc);
19430Sstevel@tonic-gate 
19441735Skcpoon 		if ((extra = new_len & (SCTP_ALIGN - 1)) != 0)
19451735Skcpoon 			extra = SCTP_ALIGN - extra;
19461735Skcpoon 		if ((new_len = seglen + new_len + extra) > fp->sfa_pmss)
19471735Skcpoon 			break;
19481735Skcpoon 		if ((nmp = dupmsg(mp)) == NULL)
19491735Skcpoon 			break;
19500Sstevel@tonic-gate 
19511735Skcpoon 		if (extra > 0) {
19524691Skcpoon 			fill = sctp_get_padding(sctp, extra);
19530Sstevel@tonic-gate 			if (fill != NULL) {
19541735Skcpoon 				linkb(nmp, fill);
19550Sstevel@tonic-gate 			} else {
19561735Skcpoon 				freemsg(nmp);
19570Sstevel@tonic-gate 				break;
19580Sstevel@tonic-gate 			}
19590Sstevel@tonic-gate 		}
19601735Skcpoon 		linkb(head, nmp);
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 		SCTP_CHUNK_CLEAR_FLAGS(nmp);
19630Sstevel@tonic-gate 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
19641735Skcpoon 
19651735Skcpoon 		seglen = new_len;
19660Sstevel@tonic-gate 		mp = mp->b_next;
19670Sstevel@tonic-gate 	}
19681735Skcpoon done_bundle:
19690Sstevel@tonic-gate 	if ((seglen > fp->sfa_pmss) && fp->isv4) {
19700Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)head->b_rptr;
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 		/*
19730Sstevel@tonic-gate 		 * Path MTU is different from path we thought it would
19740Sstevel@tonic-gate 		 * be when we created chunks, or IP headers have grown.
19750Sstevel@tonic-gate 		 * Need to clear the DF bit.
19760Sstevel@tonic-gate 		 */
19770Sstevel@tonic-gate 		iph->ipha_fragment_offset_and_flags = 0;
19780Sstevel@tonic-gate 	}
19793795Skcpoon 	fp->rxt_unacked += seglen;
19803795Skcpoon 
19810Sstevel@tonic-gate 	dprint(2, ("sctp_rexmit: Sending packet %d bytes, tsn %x "
19820Sstevel@tonic-gate 	    "ssn %d to %p (rwnd %d, lastack_rxd %x)\n",
19831676Sjpk 	    seglen, ntohl(sdc->sdh_tsn), ntohs(sdc->sdh_ssn),
19841676Sjpk 	    (void *)fp, sctp->sctp_frwnd, sctp->sctp_lastack_rxd));
19850Sstevel@tonic-gate 
19861735Skcpoon 	sctp->sctp_rexmitting = B_TRUE;
19871735Skcpoon 	sctp->sctp_rxt_nxttsn = first_ua_tsn;
19881735Skcpoon 	sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn - 1;
19890Sstevel@tonic-gate 	sctp_set_iplen(sctp, head);
19900Sstevel@tonic-gate 	sctp_add_sendq(sctp, head);
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 	/*
19931735Skcpoon 	 * Restart the oldfp timer with exponential backoff and
19941735Skcpoon 	 * the new fp timer for the retransmitted chunks.
19950Sstevel@tonic-gate 	 */
19960Sstevel@tonic-gate restart_timer:
19970Sstevel@tonic-gate 	oldfp->strikes++;
19980Sstevel@tonic-gate 	sctp->sctp_strikes++;
19990Sstevel@tonic-gate 	SCTP_CALC_RXT(oldfp, sctp->sctp_rto_max);
20004818Skcpoon 	/*
20014818Skcpoon 	 * If there is still some data in the oldfp, restart the
20024818Skcpoon 	 * retransmission timer.  If there is no data, the heartbeat will
20034818Skcpoon 	 * continue to run so it will do its job in checking the reachability
20044818Skcpoon 	 * of the oldfp.
20054818Skcpoon 	 */
20064818Skcpoon 	if (oldfp != fp && oldfp->suna != 0)
20070Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, oldfp, oldfp->rto);
20081735Skcpoon 
20091735Skcpoon 	/*
20101735Skcpoon 	 * Should we restart the timer of the new fp?  If there is
20111735Skcpoon 	 * outstanding data to the new fp, the timer should be
20121735Skcpoon 	 * running already.  So restarting it means that the timer
20131735Skcpoon 	 * will fire later for those outstanding data.  But if
20141735Skcpoon 	 * we don't restart it, the timer will fire too early for the
20151735Skcpoon 	 * just retransmitted chunks to the new fp.  The reason is that we
20161735Skcpoon 	 * don't keep a timestamp on when a chunk is retransmitted.
20171735Skcpoon 	 * So when the timer fires, it will just search for the
20181735Skcpoon 	 * chunk with the earliest TSN sent to new fp.  This probably
20191735Skcpoon 	 * is the chunk we just retransmitted.  So for now, let's
20201735Skcpoon 	 * be conservative and restart the timer of the new fp.
20211735Skcpoon 	 */
20221735Skcpoon 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
20234818Skcpoon 
20244818Skcpoon 	sctp->sctp_active = lbolt64;
20250Sstevel@tonic-gate }
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate /*
20281735Skcpoon  * This function is called by sctp_ss_rexmit() to create a packet
20291735Skcpoon  * to be retransmitted to the given fp.  The given meta and mp
20301735Skcpoon  * parameters are respectively the sctp_msg_hdr_t and the mblk of the
20313795Skcpoon  * first chunk to be retransmitted.  This is also called when we want
20321932Svi117747  * to retransmit a zero window probe from sctp_rexmit() or when we
20331932Svi117747  * want to retransmit the zero window probe after the window has
20341932Svi117747  * opened from sctp_got_sack().
20351735Skcpoon  */
20361932Svi117747 mblk_t *
20371735Skcpoon sctp_rexmit_packet(sctp_t *sctp, mblk_t **meta, mblk_t **mp, sctp_faddr_t *fp,
20381735Skcpoon     uint_t *packet_len)
20391735Skcpoon {
20401735Skcpoon 	uint32_t	seglen = 0;
20411735Skcpoon 	uint16_t	chunklen;
20421735Skcpoon 	int		extra;
20431735Skcpoon 	mblk_t		*nmp;
20441735Skcpoon 	mblk_t		*head;
20451735Skcpoon 	mblk_t		*fill;
20461735Skcpoon 	sctp_data_hdr_t	*sdc;
20471735Skcpoon 	sctp_msg_hdr_t	*mhdr;
20481735Skcpoon 
20491735Skcpoon 	sdc = (sctp_data_hdr_t *)(*mp)->b_rptr;
20501735Skcpoon 	seglen = ntohs(sdc->sdh_len);
20511735Skcpoon 	chunklen = seglen - sizeof (*sdc);
20521735Skcpoon 	if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
20531735Skcpoon 		extra = SCTP_ALIGN - extra;
20541735Skcpoon 
20551735Skcpoon 	nmp = dupmsg(*mp);
20561735Skcpoon 	if (nmp == NULL)
20571735Skcpoon 		return (NULL);
20581735Skcpoon 	if (extra > 0) {
20594691Skcpoon 		fill = sctp_get_padding(sctp, extra);
20601735Skcpoon 		if (fill != NULL) {
20611735Skcpoon 			linkb(nmp, fill);
20621735Skcpoon 			seglen += extra;
20631735Skcpoon 		} else {
20641735Skcpoon 			freemsg(nmp);
20651735Skcpoon 			return (NULL);
20661735Skcpoon 		}
20671735Skcpoon 	}
20681735Skcpoon 	SCTP_CHUNK_CLEAR_FLAGS(nmp);
20691735Skcpoon 	head = sctp_add_proto_hdr(sctp, fp, nmp, 0, NULL);
20701735Skcpoon 	if (head == NULL) {
20711735Skcpoon 		freemsg(nmp);
20721735Skcpoon 		return (NULL);
20731735Skcpoon 	}
20741735Skcpoon 	SCTP_CHUNK_SENT(sctp, *mp, sdc, fp, chunklen, *meta);
20751932Svi117747 	/*
20761932Svi117747 	 * Don't update the TSN if we are doing a Zero Win Probe.
20771932Svi117747 	 */
20781932Svi117747 	if (!sctp->sctp_zero_win_probe)
20791932Svi117747 		sctp->sctp_rxt_nxttsn = ntohl(sdc->sdh_tsn);
20801735Skcpoon 	*mp = (*mp)->b_next;
20811735Skcpoon 
20821735Skcpoon try_bundle:
20831735Skcpoon 	while (seglen < fp->sfa_pmss) {
20841735Skcpoon 		int32_t new_len;
20851735Skcpoon 
20861735Skcpoon 		/*
20871735Skcpoon 		 * Go through the list to find more chunks to be bundled.
20881735Skcpoon 		 * We should only retransmit sent by unack'ed chunks.  Since
20891735Skcpoon 		 * they were sent before, the peer's receive window should
20901735Skcpoon 		 * be able to receive them.
20911735Skcpoon 		 */
20921735Skcpoon 		while (*mp != NULL) {
20931735Skcpoon 			/* Check if the chunk can be bundled. */
20941735Skcpoon 			if (SCTP_CHUNK_ISSENT(*mp) && !SCTP_CHUNK_ISACKED(*mp))
20951735Skcpoon 				break;
20961735Skcpoon 			*mp = (*mp)->b_next;
20971735Skcpoon 		}
20981735Skcpoon 		/* Go to the next message. */
20991735Skcpoon 		if (*mp == NULL) {
21001735Skcpoon 			for (*meta = (*meta)->b_next; *meta != NULL;
21011735Skcpoon 			    *meta = (*meta)->b_next) {
21021735Skcpoon 				mhdr = (sctp_msg_hdr_t *)(*meta)->b_rptr;
21031735Skcpoon 
21041735Skcpoon 				if (SCTP_IS_MSG_ABANDONED(*meta) ||
21051735Skcpoon 				    SCTP_MSG_TO_BE_ABANDONED(*meta, mhdr,
21061735Skcpoon 				    sctp)) {
21071735Skcpoon 					continue;
21081735Skcpoon 				}
21091735Skcpoon 
21101735Skcpoon 				*mp = (*meta)->b_cont;
21111735Skcpoon 				goto try_bundle;
21121735Skcpoon 			}
21131735Skcpoon 			/* No more chunk to be bundled. */
21141735Skcpoon 			break;
21151735Skcpoon 		}
21161735Skcpoon 
21171735Skcpoon 		sdc = (sctp_data_hdr_t *)(*mp)->b_rptr;
21181735Skcpoon 		/* Don't bundle chunks beyond sctp_rxt_maxtsn. */
21191735Skcpoon 		if (SEQ_GT(ntohl(sdc->sdh_tsn), sctp->sctp_rxt_maxtsn))
21201735Skcpoon 			break;
21211735Skcpoon 		new_len = ntohs(sdc->sdh_len);
21221735Skcpoon 		chunklen = new_len - sizeof (*sdc);
21231735Skcpoon 
21241735Skcpoon 		if ((extra = new_len & (SCTP_ALIGN - 1)) != 0)
21251735Skcpoon 			extra = SCTP_ALIGN - extra;
21261735Skcpoon 		if ((new_len = seglen + new_len + extra) > fp->sfa_pmss)
21271735Skcpoon 			break;
21281735Skcpoon 		if ((nmp = dupmsg(*mp)) == NULL)
21291735Skcpoon 			break;
21301735Skcpoon 
21311735Skcpoon 		if (extra > 0) {
21324691Skcpoon 			fill = sctp_get_padding(sctp, extra);
21331735Skcpoon 			if (fill != NULL) {
21341735Skcpoon 				linkb(nmp, fill);
21351735Skcpoon 			} else {
21361735Skcpoon 				freemsg(nmp);
21371735Skcpoon 				break;
21381735Skcpoon 			}
21391735Skcpoon 		}
21401735Skcpoon 		linkb(head, nmp);
21411735Skcpoon 
21421735Skcpoon 		SCTP_CHUNK_CLEAR_FLAGS(nmp);
21431735Skcpoon 		SCTP_CHUNK_SENT(sctp, *mp, sdc, fp, chunklen, *meta);
21441932Svi117747 		/*
21451932Svi117747 		 * Don't update the TSN if we are doing a Zero Win Probe.
21461932Svi117747 		 */
21471932Svi117747 		if (!sctp->sctp_zero_win_probe)
21481932Svi117747 			sctp->sctp_rxt_nxttsn = ntohl(sdc->sdh_tsn);
21491735Skcpoon 
21501735Skcpoon 		seglen = new_len;
21511735Skcpoon 		*mp = (*mp)->b_next;
21521735Skcpoon 	}
21531735Skcpoon 	*packet_len = seglen;
21543795Skcpoon 	fp->rxt_unacked += seglen;
21551735Skcpoon 	return (head);
21561735Skcpoon }
21571735Skcpoon 
21581735Skcpoon /*
21591735Skcpoon  * sctp_ss_rexmit() is called when we get a SACK after a timeout which
21601735Skcpoon  * advances the cum_tsn but the cum_tsn is still less than what we have sent
21611735Skcpoon  * (sctp_rxt_maxtsn) at the time of the timeout.  This SACK is a "partial"
21621735Skcpoon  * SACK.  We retransmit unacked chunks without having to wait for another
21631735Skcpoon  * timeout.  The rationale is that the SACK should not be "partial" if all the
21641735Skcpoon  * lost chunks have been retransmitted.  Since the SACK is "partial,"
21651735Skcpoon  * the chunks between the cum_tsn and the sctp_rxt_maxtsn should still
21661735Skcpoon  * be missing.  It is better for us to retransmit them now instead
21671735Skcpoon  * of waiting for a timeout.
21681735Skcpoon  */
21691735Skcpoon void
21701735Skcpoon sctp_ss_rexmit(sctp_t *sctp)
21711735Skcpoon {
21721735Skcpoon 	mblk_t		*meta;
21731735Skcpoon 	mblk_t		*mp;
21741735Skcpoon 	mblk_t		*pkt;
21751735Skcpoon 	sctp_faddr_t	*fp;
21761735Skcpoon 	uint_t		pkt_len;
21771735Skcpoon 	uint32_t	tot_wnd;
21781735Skcpoon 	sctp_data_hdr_t	*sdc;
21791735Skcpoon 	int		burst;
21803448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
21811735Skcpoon 
21821932Svi117747 	ASSERT(!sctp->sctp_zero_win_probe);
21831932Svi117747 
21841735Skcpoon 	/*
21851735Skcpoon 	 * If the last cum ack is smaller than what we have just
21861735Skcpoon 	 * retransmitted, simply return.
21871735Skcpoon 	 */
21881735Skcpoon 	if (SEQ_GEQ(sctp->sctp_lastack_rxd, sctp->sctp_rxt_nxttsn))
21891735Skcpoon 		sctp->sctp_rxt_nxttsn = sctp->sctp_lastack_rxd + 1;
21901735Skcpoon 	else
21911735Skcpoon 		return;
21921735Skcpoon 	ASSERT(SEQ_LEQ(sctp->sctp_rxt_nxttsn, sctp->sctp_rxt_maxtsn));
21931735Skcpoon 
21941735Skcpoon 	/*
21951735Skcpoon 	 * After a timer fires, sctp_current should be set to the new
21961735Skcpoon 	 * fp where the retransmitted chunks are sent.
21971735Skcpoon 	 */
21981735Skcpoon 	fp = sctp->sctp_current;
21991735Skcpoon 
22001735Skcpoon 	/*
22013795Skcpoon 	 * Since we are retransmitting, we only need to use cwnd to determine
22023795Skcpoon 	 * how much we can send as we were allowed (by peer's receive window)
22033795Skcpoon 	 * to send those retransmitted chunks previously when they are first
22043795Skcpoon 	 * sent.  If we record how much we have retransmitted but
22053795Skcpoon 	 * unacknowledged using rxt_unacked, then the amount we can now send
22063795Skcpoon 	 * is equal to cwnd minus rxt_unacked.
22073795Skcpoon 	 *
22083795Skcpoon 	 * The field rxt_unacked is incremented when we retransmit a packet
22093795Skcpoon 	 * and decremented when we got a SACK acknowledging something.  And
22103795Skcpoon 	 * it is reset when the retransmission timer fires as we assume that
22113795Skcpoon 	 * all packets have left the network after a timeout.  If this
22123795Skcpoon 	 * assumption is not true, it means that after a timeout, we can
22133795Skcpoon 	 * get a SACK acknowledging more than rxt_unacked (its value only
22143795Skcpoon 	 * contains what is retransmitted when the timer fires).  So
22153795Skcpoon 	 * rxt_unacked will become very big (it is an unsiged int so going
22163795Skcpoon 	 * negative means that the value is huge).  This is the reason we
22173795Skcpoon 	 * always send at least 1 MSS bytes.
22183795Skcpoon 	 *
22193795Skcpoon 	 * The reason why we do not have an accurate count is that we
22203795Skcpoon 	 * only know how many packets are outstanding (using the TSN numbers).
22213795Skcpoon 	 * But we do not know how many bytes those packets contain.  To
22223795Skcpoon 	 * have an accurate count, we need to walk through the send list.
22233795Skcpoon 	 * As it is not really important to have an accurate count during
22243795Skcpoon 	 * retransmission, we skip this walk to save some time.  This should
22253795Skcpoon 	 * not make the retransmission too aggressive to cause congestion.
22261735Skcpoon 	 */
22273795Skcpoon 	if (fp->cwnd <= fp->rxt_unacked)
22283795Skcpoon 		tot_wnd = fp->sfa_pmss;
22291735Skcpoon 	else
22303795Skcpoon 		tot_wnd = fp->cwnd - fp->rxt_unacked;
22311735Skcpoon 
22321735Skcpoon 	/* Find the first unack'ed chunk */
22331735Skcpoon 	for (meta = sctp->sctp_xmit_head; meta != NULL; meta = meta->b_next) {
22341735Skcpoon 		sctp_msg_hdr_t	*mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
22351735Skcpoon 
22361735Skcpoon 		if (SCTP_IS_MSG_ABANDONED(meta) ||
22371735Skcpoon 		    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr, sctp)) {
22381735Skcpoon 			continue;
22391735Skcpoon 		}
22401735Skcpoon 
22411735Skcpoon 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
22421735Skcpoon 			/* Again, this may not be possible */
22431735Skcpoon 			if (!SCTP_CHUNK_ISSENT(mp))
22441735Skcpoon 				return;
22451735Skcpoon 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
22461735Skcpoon 			if (ntohl(sdc->sdh_tsn) == sctp->sctp_rxt_nxttsn)
22471735Skcpoon 				goto found_msg;
22481735Skcpoon 		}
22491735Skcpoon 	}
22501735Skcpoon 
22511735Skcpoon 	/* Everything is abandoned... */
22521735Skcpoon 	return;
22531735Skcpoon 
22541735Skcpoon found_msg:
22551735Skcpoon 	if (!fp->timer_running)
22561735Skcpoon 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
22571735Skcpoon 	pkt = sctp_rexmit_packet(sctp, &meta, &mp, fp, &pkt_len);
22581735Skcpoon 	if (pkt == NULL) {
22593448Sdh155122 		SCTP_KSTAT(sctps, sctp_ss_rexmit_failed);
22601735Skcpoon 		return;
22611735Skcpoon 	}
22621735Skcpoon 	if ((pkt_len > fp->sfa_pmss) && fp->isv4) {
22631735Skcpoon 		ipha_t	*iph = (ipha_t *)pkt->b_rptr;
22641735Skcpoon 
22651735Skcpoon 		/*
22661735Skcpoon 		 * Path MTU is different from path we thought it would
22671735Skcpoon 		 * be when we created chunks, or IP headers have grown.
22681735Skcpoon 		 *  Need to clear the DF bit.
22691735Skcpoon 		 */
22701735Skcpoon 		iph->ipha_fragment_offset_and_flags = 0;
22711735Skcpoon 	}
22721735Skcpoon 	sctp_set_iplen(sctp, pkt);
22731735Skcpoon 	sctp_add_sendq(sctp, pkt);
22741735Skcpoon 
22751735Skcpoon 	/* Check and see if there is more chunk to be retransmitted. */
22761735Skcpoon 	if (tot_wnd <= pkt_len || tot_wnd - pkt_len < fp->sfa_pmss ||
22771735Skcpoon 	    meta == NULL)
22781735Skcpoon 		return;
22791735Skcpoon 	if (mp == NULL)
22801735Skcpoon 		meta = meta->b_next;
22811735Skcpoon 	if (meta == NULL)
22821735Skcpoon 		return;
22831735Skcpoon 
22841735Skcpoon 	/* Retransmit another packet if the window allows. */
22853448Sdh155122 	for (tot_wnd -= pkt_len, burst = sctps->sctps_maxburst - 1;
22861735Skcpoon 	    meta != NULL && burst > 0; meta = meta->b_next, burst--) {
22871735Skcpoon 		if (mp == NULL)
22881735Skcpoon 			mp = meta->b_cont;
22891735Skcpoon 		for (; mp != NULL; mp = mp->b_next) {
22901735Skcpoon 			/* Again, this may not be possible */
22911735Skcpoon 			if (!SCTP_CHUNK_ISSENT(mp))
22921735Skcpoon 				return;
22931735Skcpoon 			if (!SCTP_CHUNK_ISACKED(mp))
22941735Skcpoon 				goto found_msg;
22951735Skcpoon 		}
22961735Skcpoon 	}
22971735Skcpoon }
2298