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
5*1676Sjpk  * Common Development and Distribution License (the "License").
6*1676Sjpk  * 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  */
210Sstevel@tonic-gate /*
22*1676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/stream.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
320Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
330Sstevel@tonic-gate #include <sys/tihdr.h>
340Sstevel@tonic-gate #include <sys/socket.h>
350Sstevel@tonic-gate #include <sys/stropts.h>
360Sstevel@tonic-gate #include <sys/strsun.h>
370Sstevel@tonic-gate #include <sys/strsubr.h>
380Sstevel@tonic-gate #include <sys/socketvar.h>
39*1676Sjpk /* swilly code in sys/socketvar.h turns off DEBUG */
40*1676Sjpk #ifdef __lint
41*1676Sjpk #define	DEBUG
42*1676Sjpk #endif
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <inet/common.h>
450Sstevel@tonic-gate #include <inet/mi.h>
460Sstevel@tonic-gate #include <inet/ip.h>
470Sstevel@tonic-gate #include <inet/ip6.h>
480Sstevel@tonic-gate #include <inet/sctp_ip.h>
490Sstevel@tonic-gate #include <inet/ipclassifier.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * PR-SCTP comments.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * A message can expire before it gets to the transmit list (i.e. it is still
550Sstevel@tonic-gate  * in the unsent list - unchunked), after it gets to the transmit list, but
560Sstevel@tonic-gate  * before transmission has actually started, or after transmission has begun.
570Sstevel@tonic-gate  * Accordingly, we check for the status of a message in sctp_chunkify() when
580Sstevel@tonic-gate  * the message is being transferred from the unsent list to the transmit list;
590Sstevel@tonic-gate  * in sctp_get_msg_to_send(), when we get the next chunk from the transmit
600Sstevel@tonic-gate  * list and in sctp_rexmit() when we get the next chunk to be (re)transmitted.
610Sstevel@tonic-gate  * When we nuke a message in sctp_chunkify(), all we need to do is take it
620Sstevel@tonic-gate  * out of the unsent list and update sctp_unsent; when a message is deemed
630Sstevel@tonic-gate  * timed-out in sctp_get_msg_to_send() we can just take it out of the transmit
640Sstevel@tonic-gate  * list, update sctp_unsent IFF transmission for the message has not yet begun
650Sstevel@tonic-gate  * (i.e. !SCTP_CHUNK_ISSENT(meta->b_cont)). However, if transmission for the
660Sstevel@tonic-gate  * message has started, then we cannot just take it out of the list, we need
670Sstevel@tonic-gate  * to send Forward TSN chunk to the peer so that the peer can clear its
680Sstevel@tonic-gate  * fragment list for this message. However, we cannot just send the Forward
690Sstevel@tonic-gate  * TSN in sctp_get_msg_to_send() because there might be unacked chunks for
700Sstevel@tonic-gate  * messages preceeding this abandoned message. So, we send a Forward TSN
710Sstevel@tonic-gate  * IFF all messages prior to this abandoned message has been SACKd, if not
720Sstevel@tonic-gate  * we defer sending the Forward TSN to sctp_cumack(), which will check for
730Sstevel@tonic-gate  * this condition and send the Forward TSN via sctp_check_abandoned_msg(). In
740Sstevel@tonic-gate  * sctp_rexmit() when we check for retransmissions, we need to determine if
750Sstevel@tonic-gate  * the advanced peer ack point can be moved ahead, and if so, send a Forward
760Sstevel@tonic-gate  * TSN to the peer instead of retransmitting the chunk. Note that when
770Sstevel@tonic-gate  * we send a Forward TSN for a message, there may be yet unsent chunks for
780Sstevel@tonic-gate  * this message; we need to mark all such chunks as abandoned, so that
790Sstevel@tonic-gate  * sctp_cumack() can take the message out of the transmit list, additionally
800Sstevel@tonic-gate  * sctp_unsent need to be adjusted. Whenever sctp_unsent is updated (i.e.
810Sstevel@tonic-gate  * decremented when a message/chunk is deemed abandoned), sockfs needs to
820Sstevel@tonic-gate  * be notified so that it can adjust its idea of the queued message.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #include "sctp_impl.h"
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static struct kmem_cache	*sctp_kmem_ftsn_set_cache;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /* Padding mblk for SCTP chunks. */
900Sstevel@tonic-gate mblk_t *sctp_pad_mp;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #ifdef	DEBUG
930Sstevel@tonic-gate static boolean_t	sctp_verify_chain(mblk_t *, mblk_t *);
940Sstevel@tonic-gate #endif
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Called to allocate a header mblk when sending data to SCTP.
980Sstevel@tonic-gate  * Data will follow in b_cont of this mblk.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate mblk_t *
1010Sstevel@tonic-gate sctp_alloc_hdr(const char *name, int nlen, const char *control, int clen,
1020Sstevel@tonic-gate     int flags)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	mblk_t *mp;
1050Sstevel@tonic-gate 	struct T_unitdata_req *tudr;
1060Sstevel@tonic-gate 	size_t size;
1070Sstevel@tonic-gate 	int error;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	size = sizeof (*tudr) + _TPI_ALIGN_TOPT(nlen) + clen;
1100Sstevel@tonic-gate 	size = MAX(size, sizeof (sctp_msg_hdr_t));
1110Sstevel@tonic-gate 	if (flags & SCTP_CAN_BLOCK) {
1120Sstevel@tonic-gate 		mp = allocb_wait(size, BPRI_MED, 0, &error);
1130Sstevel@tonic-gate 	} else {
1140Sstevel@tonic-gate 		mp = allocb(size, BPRI_MED);
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 	if (mp) {
1170Sstevel@tonic-gate 		tudr = (struct T_unitdata_req *)mp->b_rptr;
1180Sstevel@tonic-gate 		tudr->PRIM_type = T_UNITDATA_REQ;
1190Sstevel@tonic-gate 		tudr->DEST_length = nlen;
1200Sstevel@tonic-gate 		tudr->DEST_offset = sizeof (*tudr);
1210Sstevel@tonic-gate 		tudr->OPT_length = clen;
1220Sstevel@tonic-gate 		tudr->OPT_offset = (t_scalar_t)(sizeof (*tudr) +
1230Sstevel@tonic-gate 		    _TPI_ALIGN_TOPT(nlen));
1240Sstevel@tonic-gate 		if (nlen > 0)
1250Sstevel@tonic-gate 			bcopy(name, tudr + 1, nlen);
1260Sstevel@tonic-gate 		if (clen > 0)
1270Sstevel@tonic-gate 			bcopy(control, (char *)tudr + tudr->OPT_offset, clen);
1280Sstevel@tonic-gate 		mp->b_wptr += (tudr ->OPT_offset + clen);
1290Sstevel@tonic-gate 		mp->b_datap->db_type = M_PROTO;
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 	return (mp);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*ARGSUSED2*/
1350Sstevel@tonic-gate int
1360Sstevel@tonic-gate sctp_sendmsg(sctp_t *sctp, mblk_t *mp, int flags)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate 	sctp_faddr_t	*fp = NULL;
1390Sstevel@tonic-gate 	struct T_unitdata_req	*tudr;
1400Sstevel@tonic-gate 	int		error = 0;
1410Sstevel@tonic-gate 	mblk_t		*mproto = mp;
1420Sstevel@tonic-gate 	in6_addr_t	*addr;
1430Sstevel@tonic-gate 	in6_addr_t	tmpaddr;
1440Sstevel@tonic-gate 	uint16_t	sid = sctp->sctp_def_stream;
1450Sstevel@tonic-gate 	uint32_t	ppid = sctp->sctp_def_ppid;
1460Sstevel@tonic-gate 	uint32_t	context = sctp->sctp_def_context;
1470Sstevel@tonic-gate 	uint16_t	msg_flags = sctp->sctp_def_flags;
1480Sstevel@tonic-gate 	sctp_msg_hdr_t	*sctp_msg_hdr;
1490Sstevel@tonic-gate 	uint32_t	msg_len = 0;
1500Sstevel@tonic-gate 	uint32_t	timetolive = sctp->sctp_def_timetolive;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	ASSERT(DB_TYPE(mproto) == M_PROTO);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	mp = mp->b_cont;
1550Sstevel@tonic-gate 	ASSERT(mp == NULL || DB_TYPE(mp) == M_DATA);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	tudr = (struct T_unitdata_req *)mproto->b_rptr;
1580Sstevel@tonic-gate 	ASSERT(tudr->PRIM_type == T_UNITDATA_REQ);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/* Get destination address, if specified */
1610Sstevel@tonic-gate 	if (tudr->DEST_length > 0) {
1620Sstevel@tonic-gate 		sin_t *sin;
1630Sstevel@tonic-gate 		sin6_t *sin6;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 		sin = (struct sockaddr_in *)
1660Sstevel@tonic-gate 		    (mproto->b_rptr + tudr->DEST_offset);
1670Sstevel@tonic-gate 		switch (sin->sin_family) {
1680Sstevel@tonic-gate 		case AF_INET:
1690Sstevel@tonic-gate 			if (tudr->DEST_length < sizeof (*sin)) {
1700Sstevel@tonic-gate 				return (EINVAL);
1710Sstevel@tonic-gate 			}
1720Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &tmpaddr);
1730Sstevel@tonic-gate 			addr = &tmpaddr;
1740Sstevel@tonic-gate 			break;
1750Sstevel@tonic-gate 		case AF_INET6:
1760Sstevel@tonic-gate 			if (tudr->DEST_length < sizeof (*sin6)) {
1770Sstevel@tonic-gate 				return (EINVAL);
1780Sstevel@tonic-gate 			}
1790Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)
1800Sstevel@tonic-gate 			    (mproto->b_rptr + tudr->DEST_offset);
1810Sstevel@tonic-gate 			addr = &sin6->sin6_addr;
1820Sstevel@tonic-gate 			break;
1830Sstevel@tonic-gate 		default:
1840Sstevel@tonic-gate 			return (EAFNOSUPPORT);
1850Sstevel@tonic-gate 		}
1860Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, addr);
1870Sstevel@tonic-gate 		if (fp == NULL) {
1880Sstevel@tonic-gate 			return (EINVAL);
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 	/* Ancillary Data? */
1920Sstevel@tonic-gate 	if (tudr->OPT_length > 0) {
1930Sstevel@tonic-gate 		struct cmsghdr		*cmsg;
1940Sstevel@tonic-gate 		char			*cend;
1950Sstevel@tonic-gate 		struct sctp_sndrcvinfo	*sndrcv;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)(mproto->b_rptr + tudr->OPT_offset);
1980Sstevel@tonic-gate 		cend = ((char *)cmsg + tudr->OPT_length);
1990Sstevel@tonic-gate 		ASSERT(cend <= (char *)mproto->b_wptr);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		for (;;) {
2020Sstevel@tonic-gate 			if ((char *)(cmsg + 1) > cend ||
2030Sstevel@tonic-gate 			    ((char *)cmsg + cmsg->cmsg_len) > cend) {
2040Sstevel@tonic-gate 				break;
2050Sstevel@tonic-gate 			}
2060Sstevel@tonic-gate 			if ((cmsg->cmsg_level == IPPROTO_SCTP) &&
2070Sstevel@tonic-gate 			    (cmsg->cmsg_type == SCTP_SNDRCV)) {
2080Sstevel@tonic-gate 				if (cmsg->cmsg_len <
2090Sstevel@tonic-gate 				    (sizeof (*sndrcv) + sizeof (*cmsg))) {
2100Sstevel@tonic-gate 					return (EINVAL);
2110Sstevel@tonic-gate 				}
2120Sstevel@tonic-gate 				sndrcv = (struct sctp_sndrcvinfo *)(cmsg + 1);
2130Sstevel@tonic-gate 				sid = sndrcv->sinfo_stream;
2140Sstevel@tonic-gate 				msg_flags = sndrcv->sinfo_flags;
2150Sstevel@tonic-gate 				ppid = sndrcv->sinfo_ppid;
2160Sstevel@tonic-gate 				context = sndrcv->sinfo_context;
2170Sstevel@tonic-gate 				timetolive = sndrcv->sinfo_timetolive;
2180Sstevel@tonic-gate 				break;
2190Sstevel@tonic-gate 			}
2200Sstevel@tonic-gate 			if (cmsg->cmsg_len > 0)
2210Sstevel@tonic-gate 				cmsg = CMSG_NEXT(cmsg);
2220Sstevel@tonic-gate 			else
2230Sstevel@tonic-gate 				break;
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 	if (msg_flags & MSG_ABORT) {
2270Sstevel@tonic-gate 		if (mp && mp->b_cont) {
2280Sstevel@tonic-gate 			mblk_t *pump = msgpullup(mp, -1);
2290Sstevel@tonic-gate 			if (!pump) {
2300Sstevel@tonic-gate 				return (ENOMEM);
2310Sstevel@tonic-gate 			}
2320Sstevel@tonic-gate 			freemsg(mp);
2330Sstevel@tonic-gate 			mp = pump;
2340Sstevel@tonic-gate 			mproto->b_cont = mp;
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate 		RUN_SCTP(sctp);
2370Sstevel@tonic-gate 		sctp_user_abort(sctp, mp, B_TRUE);
2380Sstevel@tonic-gate 		sctp_clean_death(sctp, ECONNRESET);
2390Sstevel@tonic-gate 		freemsg(mproto);
2400Sstevel@tonic-gate 		goto process_sendq;
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 	if (mp == NULL)
2430Sstevel@tonic-gate 		goto done;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	RUN_SCTP(sctp);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	/* Reject any new data requests if we are shutting down */
2480Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_ESTABLISHED) {
2490Sstevel@tonic-gate 		error = EPIPE;
2500Sstevel@tonic-gate 		goto unlock_done;
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/* Re-use the mproto to store relevant info. */
2540Sstevel@tonic-gate 	ASSERT(MBLKSIZE(mproto) >= sizeof (*sctp_msg_hdr));
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	mproto->b_rptr = mproto->b_datap->db_base;
2570Sstevel@tonic-gate 	mproto->b_wptr = mproto->b_rptr + sizeof (*sctp_msg_hdr);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	sctp_msg_hdr = (sctp_msg_hdr_t *)mproto->b_rptr;
2600Sstevel@tonic-gate 	bzero(sctp_msg_hdr, sizeof (*sctp_msg_hdr));
2610Sstevel@tonic-gate 	sctp_msg_hdr->smh_context = context;
2620Sstevel@tonic-gate 	sctp_msg_hdr->smh_sid = sid;
2630Sstevel@tonic-gate 	sctp_msg_hdr->smh_ppid = ppid;
2640Sstevel@tonic-gate 	sctp_msg_hdr->smh_flags = msg_flags;
2650Sstevel@tonic-gate 	sctp_msg_hdr->smh_ttl = MSEC_TO_TICK(timetolive);
2660Sstevel@tonic-gate 	sctp_msg_hdr->smh_tob = lbolt64;
2670Sstevel@tonic-gate 	for (; mp != NULL; mp = mp->b_cont)
2680Sstevel@tonic-gate 		msg_len += MBLKL(mp);
2690Sstevel@tonic-gate 	sctp_msg_hdr->smh_msglen = msg_len;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	/* User requested specific destination */
2720Sstevel@tonic-gate 	SCTP_SET_CHUNK_DEST(mproto, fp);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_COOKIE_ECHOED &&
2750Sstevel@tonic-gate 	    sid >= sctp->sctp_num_ostr) {
2760Sstevel@tonic-gate 		/* Send sendfail event */
2770Sstevel@tonic-gate 		sctp_sendfail_event(sctp, dupmsg(mproto), SCTP_ERR_BAD_SID,
2780Sstevel@tonic-gate 		    B_FALSE);
2790Sstevel@tonic-gate 		error = EINVAL;
2800Sstevel@tonic-gate 		goto unlock_done;
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	/* no data */
2840Sstevel@tonic-gate 	if (msg_len == 0) {
2850Sstevel@tonic-gate 		sctp_sendfail_event(sctp, dupmsg(mproto),
2860Sstevel@tonic-gate 		    SCTP_ERR_NO_USR_DATA, B_FALSE);
2870Sstevel@tonic-gate 		error = EINVAL;
2880Sstevel@tonic-gate 		goto unlock_done;
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	/* Add it to the unsent list */
2920Sstevel@tonic-gate 	if (sctp->sctp_xmit_unsent == NULL) {
2930Sstevel@tonic-gate 		sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = mproto;
2940Sstevel@tonic-gate 	} else {
2950Sstevel@tonic-gate 		sctp->sctp_xmit_unsent_tail->b_next = mproto;
2960Sstevel@tonic-gate 		sctp->sctp_xmit_unsent_tail = mproto;
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 	sctp->sctp_unsent += msg_len;
2990Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_msgcount);
3000Sstevel@tonic-gate 	if (sctp->sctp_state == SCTPS_ESTABLISHED)
3010Sstevel@tonic-gate 		sctp_output(sctp);
3020Sstevel@tonic-gate process_sendq:
3030Sstevel@tonic-gate 	WAKE_SCTP(sctp);
3040Sstevel@tonic-gate 	sctp_process_sendq(sctp);
3050Sstevel@tonic-gate 	return (0);
3060Sstevel@tonic-gate unlock_done:
3070Sstevel@tonic-gate 	WAKE_SCTP(sctp);
3080Sstevel@tonic-gate done:
3090Sstevel@tonic-gate 	return (error);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate void
3130Sstevel@tonic-gate sctp_chunkify(sctp_t *sctp, int first_len, int bytes_to_send)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	mblk_t			*mp;
3160Sstevel@tonic-gate 	mblk_t			*chunk_mp;
3170Sstevel@tonic-gate 	mblk_t			*chunk_head;
3180Sstevel@tonic-gate 	mblk_t			*chunk_hdr;
3190Sstevel@tonic-gate 	mblk_t			*chunk_tail = NULL;
3200Sstevel@tonic-gate 	int			count;
3210Sstevel@tonic-gate 	int			chunksize;
3220Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
3230Sstevel@tonic-gate 	mblk_t			*mdblk = sctp->sctp_xmit_unsent;
3240Sstevel@tonic-gate 	sctp_faddr_t		*fp;
3250Sstevel@tonic-gate 	sctp_faddr_t		*fp1;
3260Sstevel@tonic-gate 	size_t			xtralen;
3270Sstevel@tonic-gate 	sctp_msg_hdr_t		*msg_hdr;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	fp = SCTP_CHUNK_DEST(mdblk);
3300Sstevel@tonic-gate 	if (fp == NULL)
3310Sstevel@tonic-gate 		fp = sctp->sctp_current;
3320Sstevel@tonic-gate 	if (fp->isv4)
3330Sstevel@tonic-gate 		xtralen = sctp->sctp_hdr_len + sctp_wroff_xtra + sizeof (*sdc);
3340Sstevel@tonic-gate 	else
3350Sstevel@tonic-gate 		xtralen = sctp->sctp_hdr6_len + sctp_wroff_xtra + sizeof (*sdc);
3360Sstevel@tonic-gate 	count = chunksize = first_len - sizeof (*sdc);
3370Sstevel@tonic-gate nextmsg:
3380Sstevel@tonic-gate 	chunk_mp = mdblk->b_cont;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	/*
3410Sstevel@tonic-gate 	 * If this partially chunked, we ignore the first_len for now
3420Sstevel@tonic-gate 	 * and use the one already present. For the unchunked bits, we
3430Sstevel@tonic-gate 	 * use the length of the last chunk.
3440Sstevel@tonic-gate 	 */
3450Sstevel@tonic-gate 	if (SCTP_IS_MSG_CHUNKED(mdblk)) {
3460Sstevel@tonic-gate 		int	chunk_len;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 		ASSERT(chunk_mp->b_next != NULL);
3490Sstevel@tonic-gate 		mdblk->b_cont = chunk_mp->b_next;
3500Sstevel@tonic-gate 		chunk_mp->b_next = NULL;
3510Sstevel@tonic-gate 		SCTP_MSG_CLEAR_CHUNKED(mdblk);
3520Sstevel@tonic-gate 		mp = mdblk->b_cont;
3530Sstevel@tonic-gate 		while (mp->b_next != NULL)
3540Sstevel@tonic-gate 			mp = mp->b_next;
3550Sstevel@tonic-gate 		chunk_len = ntohs(((sctp_data_hdr_t *)mp->b_rptr)->sdh_len);
3560Sstevel@tonic-gate 		if (fp->sfa_pmss - chunk_len > sizeof (*sdc))
3570Sstevel@tonic-gate 			count = chunksize = fp->sfa_pmss - chunk_len;
3580Sstevel@tonic-gate 		else
3590Sstevel@tonic-gate 			count = chunksize = fp->sfa_pmss;
3600Sstevel@tonic-gate 		count = chunksize = count - sizeof (*sdc);
3610Sstevel@tonic-gate 	} else {
3620Sstevel@tonic-gate 		msg_hdr = (sctp_msg_hdr_t *)mdblk->b_rptr;
3630Sstevel@tonic-gate 		if (SCTP_MSG_TO_BE_ABANDONED(mdblk, msg_hdr, sctp)) {
3640Sstevel@tonic-gate 			sctp->sctp_xmit_unsent = mdblk->b_next;
3650Sstevel@tonic-gate 			if (sctp->sctp_xmit_unsent == NULL)
3660Sstevel@tonic-gate 				sctp->sctp_xmit_unsent_tail = NULL;
3670Sstevel@tonic-gate 			ASSERT(sctp->sctp_unsent >= msg_hdr->smh_msglen);
3680Sstevel@tonic-gate 			sctp->sctp_unsent -= msg_hdr->smh_msglen;
3690Sstevel@tonic-gate 			mdblk->b_next = NULL;
3700Sstevel@tonic-gate 			BUMP_LOCAL(sctp->sctp_prsctpdrop);
3710Sstevel@tonic-gate 			/*
3720Sstevel@tonic-gate 			 * Update ULP the amount of queued data, which is
3730Sstevel@tonic-gate 			 * sent-unack'ed + unsent.
3740Sstevel@tonic-gate 			 */
3750Sstevel@tonic-gate 			if (!SCTP_IS_DETACHED(sctp)) {
3760Sstevel@tonic-gate 				sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
3770Sstevel@tonic-gate 				    sctp->sctp_unacked + sctp->sctp_unsent);
3780Sstevel@tonic-gate 			}
3790Sstevel@tonic-gate 			sctp_sendfail_event(sctp, mdblk, 0, B_FALSE);
3800Sstevel@tonic-gate 			goto try_next;
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 		mdblk->b_cont = NULL;
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 	msg_hdr = (sctp_msg_hdr_t *)mdblk->b_rptr;
3850Sstevel@tonic-gate nextchunk:
3860Sstevel@tonic-gate 	chunk_head = chunk_mp;
3870Sstevel@tonic-gate 	chunk_tail = NULL;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	/* Skip as many mblk's as we need */
3900Sstevel@tonic-gate 	while (chunk_mp != NULL && ((count - MBLKL(chunk_mp)) >= 0)) {
3910Sstevel@tonic-gate 		count -= MBLKL(chunk_mp);
3920Sstevel@tonic-gate 		chunk_tail = chunk_mp;
3930Sstevel@tonic-gate 		chunk_mp = chunk_mp->b_cont;
3940Sstevel@tonic-gate 	}
3950Sstevel@tonic-gate 	/* Split the chain, if needed */
3960Sstevel@tonic-gate 	if (chunk_mp != NULL) {
3970Sstevel@tonic-gate 		if (count > 0) {
3980Sstevel@tonic-gate 			mblk_t	*split_mp = dupb(chunk_mp);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 			if (split_mp == NULL) {
4010Sstevel@tonic-gate 				if (mdblk->b_cont == NULL) {
4020Sstevel@tonic-gate 					mdblk->b_cont = chunk_head;
4030Sstevel@tonic-gate 				} else  {
4040Sstevel@tonic-gate 					SCTP_MSG_SET_CHUNKED(mdblk);
4050Sstevel@tonic-gate 					ASSERT(chunk_head->b_next == NULL);
4060Sstevel@tonic-gate 					chunk_head->b_next = mdblk->b_cont;
4070Sstevel@tonic-gate 					mdblk->b_cont = chunk_head;
4080Sstevel@tonic-gate 				}
4090Sstevel@tonic-gate 				return;
4100Sstevel@tonic-gate 			}
4110Sstevel@tonic-gate 			if (chunk_tail != NULL) {
4120Sstevel@tonic-gate 				chunk_tail->b_cont = split_mp;
4130Sstevel@tonic-gate 				chunk_tail = chunk_tail->b_cont;
4140Sstevel@tonic-gate 			} else {
4150Sstevel@tonic-gate 				chunk_head = chunk_tail = split_mp;
4160Sstevel@tonic-gate 			}
4170Sstevel@tonic-gate 			chunk_tail->b_wptr = chunk_tail->b_rptr + count;
4180Sstevel@tonic-gate 			chunk_mp->b_rptr = chunk_tail->b_wptr;
4190Sstevel@tonic-gate 			count = 0;
4200Sstevel@tonic-gate 		} else if (chunk_tail == NULL) {
4210Sstevel@tonic-gate 			goto next;
4220Sstevel@tonic-gate 		} else {
4230Sstevel@tonic-gate 			chunk_tail->b_cont = NULL;
4240Sstevel@tonic-gate 		}
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 	/* Alloc chunk hdr, if needed */
4270Sstevel@tonic-gate 	if (DB_REF(chunk_head) > 1 ||
4280Sstevel@tonic-gate 	    ((intptr_t)chunk_head->b_rptr) & (SCTP_ALIGN - 1) ||
4290Sstevel@tonic-gate 	    MBLKHEAD(chunk_head) < sizeof (*sdc)) {
4300Sstevel@tonic-gate 		if ((chunk_hdr = allocb(xtralen, BPRI_MED)) == NULL) {
4310Sstevel@tonic-gate 			if (mdblk->b_cont == NULL) {
4320Sstevel@tonic-gate 				if (chunk_mp != NULL)
4330Sstevel@tonic-gate 					linkb(chunk_head, chunk_mp);
4340Sstevel@tonic-gate 				mdblk->b_cont = chunk_head;
4350Sstevel@tonic-gate 			} else {
4360Sstevel@tonic-gate 				SCTP_MSG_SET_CHUNKED(mdblk);
4370Sstevel@tonic-gate 				if (chunk_mp != NULL)
4380Sstevel@tonic-gate 					linkb(chunk_head, chunk_mp);
4390Sstevel@tonic-gate 				ASSERT(chunk_head->b_next == NULL);
4400Sstevel@tonic-gate 				chunk_head->b_next = mdblk->b_cont;
4410Sstevel@tonic-gate 				mdblk->b_cont = chunk_head;
4420Sstevel@tonic-gate 			}
4430Sstevel@tonic-gate 			return;
4440Sstevel@tonic-gate 		}
4450Sstevel@tonic-gate 		chunk_hdr->b_rptr += xtralen - sizeof (*sdc);
4460Sstevel@tonic-gate 		chunk_hdr->b_wptr = chunk_hdr->b_rptr + sizeof (*sdc);
4470Sstevel@tonic-gate 		chunk_hdr->b_cont = chunk_head;
4480Sstevel@tonic-gate 	} else {
4490Sstevel@tonic-gate 		chunk_hdr = chunk_head;
4500Sstevel@tonic-gate 		chunk_hdr->b_rptr -= sizeof (*sdc);
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 	ASSERT(chunk_hdr->b_datap->db_ref == 1);
4530Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)chunk_hdr->b_rptr;
4540Sstevel@tonic-gate 	sdc->sdh_id = CHUNK_DATA;
4550Sstevel@tonic-gate 	sdc->sdh_flags = 0;
4560Sstevel@tonic-gate 	sdc->sdh_len = htons(sizeof (*sdc) + chunksize - count);
4570Sstevel@tonic-gate 	ASSERT(sdc->sdh_len);
4580Sstevel@tonic-gate 	sdc->sdh_sid = htons(msg_hdr->smh_sid);
4590Sstevel@tonic-gate 	/*
4600Sstevel@tonic-gate 	 * We defer assigning the SSN just before sending the chunk, else
4610Sstevel@tonic-gate 	 * if we drop the chunk in sctp_get_msg_to_send(), we would need
4620Sstevel@tonic-gate 	 * to send a Forward TSN to let the peer know. Some more comments
4630Sstevel@tonic-gate 	 * about this in sctp_impl.h for SCTP_CHUNK_SENT.
4640Sstevel@tonic-gate 	 */
4650Sstevel@tonic-gate 	sdc->sdh_payload_id = msg_hdr->smh_ppid;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if (mdblk->b_cont == NULL) {
4680Sstevel@tonic-gate 		mdblk->b_cont = chunk_hdr;
4690Sstevel@tonic-gate 		SCTP_DATA_SET_BBIT(sdc);
4700Sstevel@tonic-gate 	} else {
4710Sstevel@tonic-gate 		mp = mdblk->b_cont;
4720Sstevel@tonic-gate 		while (mp->b_next != NULL)
4730Sstevel@tonic-gate 			mp = mp->b_next;
4740Sstevel@tonic-gate 		mp->b_next = chunk_hdr;
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	bytes_to_send -= (chunksize - count);
4780Sstevel@tonic-gate 	if (chunk_mp != NULL) {
4790Sstevel@tonic-gate next:
4800Sstevel@tonic-gate 		count = chunksize = fp->sfa_pmss - sizeof (*sdc);
4810Sstevel@tonic-gate 		goto nextchunk;
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 	SCTP_DATA_SET_EBIT(sdc);
4840Sstevel@tonic-gate 	sctp->sctp_xmit_unsent = mdblk->b_next;
4850Sstevel@tonic-gate 	if (mdblk->b_next == NULL) {
4860Sstevel@tonic-gate 		sctp->sctp_xmit_unsent_tail = NULL;
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 	mdblk->b_next = NULL;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	if (sctp->sctp_xmit_tail == NULL) {
4910Sstevel@tonic-gate 		sctp->sctp_xmit_head = sctp->sctp_xmit_tail = mdblk;
4920Sstevel@tonic-gate 	} else {
4930Sstevel@tonic-gate 		mp = sctp->sctp_xmit_tail;
4940Sstevel@tonic-gate 		while (mp->b_next != NULL)
4950Sstevel@tonic-gate 			mp = mp->b_next;
4960Sstevel@tonic-gate 		mp->b_next = mdblk;
4970Sstevel@tonic-gate 		mdblk->b_prev = mp;
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate try_next:
5000Sstevel@tonic-gate 	if (bytes_to_send > 0 && sctp->sctp_xmit_unsent != NULL) {
5010Sstevel@tonic-gate 		mdblk = sctp->sctp_xmit_unsent;
5020Sstevel@tonic-gate 		fp1 = SCTP_CHUNK_DEST(mdblk);
5030Sstevel@tonic-gate 		if (fp1 == NULL)
5040Sstevel@tonic-gate 			fp1 = sctp->sctp_current;
5050Sstevel@tonic-gate 		if (fp == fp1) {
5060Sstevel@tonic-gate 			size_t len = MBLKL(mdblk->b_cont);
5070Sstevel@tonic-gate 			if ((count > 0) &&
5080Sstevel@tonic-gate 			    ((len > fp->sfa_pmss - sizeof (*sdc)) ||
5090Sstevel@tonic-gate 				(len <= count))) {
5100Sstevel@tonic-gate 				count -= sizeof (*sdc);
5110Sstevel@tonic-gate 				count = chunksize = count - (count & 0x3);
5120Sstevel@tonic-gate 			} else {
5130Sstevel@tonic-gate 				count = chunksize = fp->sfa_pmss -
5140Sstevel@tonic-gate 				    sizeof (*sdc);
5150Sstevel@tonic-gate 			}
5160Sstevel@tonic-gate 		} else {
5170Sstevel@tonic-gate 			if (fp1->isv4)
5180Sstevel@tonic-gate 				xtralen = sctp->sctp_hdr_len;
5190Sstevel@tonic-gate 			else
5200Sstevel@tonic-gate 				xtralen = sctp->sctp_hdr6_len;
5210Sstevel@tonic-gate 			xtralen += sctp_wroff_xtra + sizeof (*sdc);
5220Sstevel@tonic-gate 			count = chunksize = fp1->sfa_pmss - sizeof (*sdc);
5230Sstevel@tonic-gate 			fp = fp1;
5240Sstevel@tonic-gate 		}
5250Sstevel@tonic-gate 		goto nextmsg;
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate void
5300Sstevel@tonic-gate sctp_free_msg(mblk_t *ump)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	mblk_t *mp, *nmp;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	for (mp = ump->b_cont; mp; mp = nmp) {
5350Sstevel@tonic-gate 		nmp = mp->b_next;
5360Sstevel@tonic-gate 		mp->b_next = mp->b_prev = NULL;
5370Sstevel@tonic-gate 		freemsg(mp);
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 	ASSERT(!ump->b_prev);
5400Sstevel@tonic-gate 	ump->b_next = NULL;
5410Sstevel@tonic-gate 	freeb(ump);
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate mblk_t *
545252Svi117747 sctp_add_proto_hdr(sctp_t *sctp, sctp_faddr_t *fp, mblk_t *mp, int sacklen,
546252Svi117747     int *error)
5470Sstevel@tonic-gate {
5480Sstevel@tonic-gate 	int hdrlen;
5490Sstevel@tonic-gate 	char *hdr;
5500Sstevel@tonic-gate 	int isv4 = fp->isv4;
5510Sstevel@tonic-gate 
552252Svi117747 	if (error != NULL)
553252Svi117747 		*error = 0;
554252Svi117747 
5550Sstevel@tonic-gate 	if (isv4) {
5560Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
5570Sstevel@tonic-gate 		hdr = sctp->sctp_iphc;
5580Sstevel@tonic-gate 	} else {
5590Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
5600Sstevel@tonic-gate 		hdr = sctp->sctp_iphc6;
5610Sstevel@tonic-gate 	}
562252Svi117747 	/*
563252Svi117747 	 * A null fp->ire could mean that the address is 'down'. Similarly,
564252Svi117747 	 * it is possible that the address went down, we tried to send an
565252Svi117747 	 * heartbeat and ended up setting fp->saddr as unspec because we
566252Svi117747 	 * didn't have any usable source address. In either case
567252Svi117747 	 * sctp_ire2faddr() will try find an IRE, if available, and set
568252Svi117747 	 * the source address, if needed. If we still don't have any
569252Svi117747 	 * usable source address, fp->state will be SCTP_FADDRS_UNREACH and
570252Svi117747 	 * we return EHOSTUNREACH.
571252Svi117747 	 */
572252Svi117747 	if (fp->ire == NULL || SCTP_IS_ADDR_UNSPEC(fp->isv4, fp->saddr)) {
5730Sstevel@tonic-gate 		sctp_ire2faddr(sctp, fp);
574252Svi117747 		if (fp->state == SCTP_FADDRS_UNREACH) {
575252Svi117747 			if (error != NULL)
576252Svi117747 				*error = EHOSTUNREACH;
577252Svi117747 			return (NULL);
5780Sstevel@tonic-gate 		}
5790Sstevel@tonic-gate 	}
5800Sstevel@tonic-gate 	/* Copy in IP header. */
5810Sstevel@tonic-gate 	if ((mp->b_rptr - mp->b_datap->db_base) <
5820Sstevel@tonic-gate 	    (sctp_wroff_xtra + hdrlen + sacklen) || DB_REF(mp) > 2) {
5830Sstevel@tonic-gate 		mblk_t *nmp;
5840Sstevel@tonic-gate 		/*
5850Sstevel@tonic-gate 		 * This can happen if IP headers are adjusted after
5860Sstevel@tonic-gate 		 * data was moved into chunks, or during retransmission,
5870Sstevel@tonic-gate 		 * or things like snoop is running.
5880Sstevel@tonic-gate 		 */
589*1676Sjpk 		nmp = allocb_cred(sctp_wroff_xtra + hdrlen + sacklen,
590*1676Sjpk 		    CONN_CRED(sctp->sctp_connp));
5910Sstevel@tonic-gate 		if (nmp == NULL) {
592252Svi117747 			if (error !=  NULL)
593252Svi117747 				*error = ENOMEM;
5940Sstevel@tonic-gate 			return (NULL);
5950Sstevel@tonic-gate 		}
5960Sstevel@tonic-gate 		nmp->b_rptr += sctp_wroff_xtra;
5970Sstevel@tonic-gate 		nmp->b_wptr = nmp->b_rptr + hdrlen + sacklen;
5980Sstevel@tonic-gate 		nmp->b_cont = mp;
5990Sstevel@tonic-gate 		mp = nmp;
6000Sstevel@tonic-gate 	} else {
6010Sstevel@tonic-gate 		mp->b_rptr -= (hdrlen + sacklen);
602*1676Sjpk 		mblk_setcred(mp, CONN_CRED(sctp->sctp_connp));
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 	bcopy(hdr, mp->b_rptr, hdrlen);
6050Sstevel@tonic-gate 	if (sacklen) {
6060Sstevel@tonic-gate 		sctp_fill_sack(sctp, mp->b_rptr + hdrlen, sacklen);
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 	if (fp != sctp->sctp_current) {
6090Sstevel@tonic-gate 		/* change addresses in header */
6100Sstevel@tonic-gate 		if (isv4) {
6110Sstevel@tonic-gate 			ipha_t *iph = (ipha_t *)mp->b_rptr;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR(&fp->faddr, iph->ipha_dst);
6140Sstevel@tonic-gate 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&fp->saddr)) {
6150Sstevel@tonic-gate 				IN6_V4MAPPED_TO_IPADDR(&fp->saddr,
6160Sstevel@tonic-gate 				    iph->ipha_src);
6170Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
6180Sstevel@tonic-gate 				iph->ipha_src = INADDR_ANY;
6190Sstevel@tonic-gate 			}
6200Sstevel@tonic-gate 		} else {
6210Sstevel@tonic-gate 			((ip6_t *)(mp->b_rptr))->ip6_dst = fp->faddr;
6220Sstevel@tonic-gate 			if (!IN6_IS_ADDR_UNSPECIFIED(&fp->saddr)) {
6230Sstevel@tonic-gate 				((ip6_t *)(mp->b_rptr))->ip6_src = fp->saddr;
6240Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
6250Sstevel@tonic-gate 				V6_SET_ZERO(((ip6_t *)(mp->b_rptr))->ip6_src);
6260Sstevel@tonic-gate 			}
6270Sstevel@tonic-gate 		}
6280Sstevel@tonic-gate 	}
6290Sstevel@tonic-gate 	/*
6300Sstevel@tonic-gate 	 * IP will not free this IRE if it is condemned.  SCTP needs to
6310Sstevel@tonic-gate 	 * free it.
6320Sstevel@tonic-gate 	 */
6330Sstevel@tonic-gate 	if ((fp->ire != NULL) && (fp->ire->ire_marks & IRE_MARK_CONDEMNED)) {
6340Sstevel@tonic-gate 		IRE_REFRELE_NOTR(fp->ire);
6350Sstevel@tonic-gate 		fp->ire = NULL;
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/* Stash the conn and ire ptr info for IP */
6390Sstevel@tonic-gate 	SCTP_STASH_IPINFO(mp, fp->ire);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	return (mp);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate /*
6450Sstevel@tonic-gate  * SCTP requires every chunk to be padded so that the total length
6460Sstevel@tonic-gate  * is a multiple of SCTP_ALIGN.  This function returns a mblk with
6470Sstevel@tonic-gate  * the specified pad length.
6480Sstevel@tonic-gate  */
6490Sstevel@tonic-gate static mblk_t *
6500Sstevel@tonic-gate sctp_get_padding(int pad)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	mblk_t *fill;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	ASSERT(pad < SCTP_ALIGN);
6550Sstevel@tonic-gate 	if ((fill = dupb(sctp_pad_mp)) != NULL) {
6560Sstevel@tonic-gate 		fill->b_wptr += pad;
6570Sstevel@tonic-gate 		return (fill);
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	/*
6610Sstevel@tonic-gate 	 * The memory saving path of reusing the sctp_pad_mp
6620Sstevel@tonic-gate 	 * fails may be because it has been dupb() too
6630Sstevel@tonic-gate 	 * many times (DBLK_REFMAX).  Use the memory consuming
6640Sstevel@tonic-gate 	 * path of allocating the pad mblk.
6650Sstevel@tonic-gate 	 */
6660Sstevel@tonic-gate 	if ((fill = allocb(SCTP_ALIGN, BPRI_MED)) != NULL) {
6670Sstevel@tonic-gate 		/* Zero it out.  SCTP_ALIGN is sizeof (int32_t) */
6680Sstevel@tonic-gate 		*(int32_t *)fill->b_rptr = 0;
6690Sstevel@tonic-gate 		fill->b_wptr += pad;
6700Sstevel@tonic-gate 	}
6710Sstevel@tonic-gate 	return (fill);
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate static mblk_t *
6750Sstevel@tonic-gate sctp_find_fast_rexmit_mblks(sctp_t *sctp, int *total, sctp_faddr_t **fp)
6760Sstevel@tonic-gate {
6770Sstevel@tonic-gate 	mblk_t		*meta;
6780Sstevel@tonic-gate 	mblk_t		*start_mp = NULL;
6790Sstevel@tonic-gate 	mblk_t		*end_mp = NULL;
6800Sstevel@tonic-gate 	mblk_t		*mp, *nmp;
6810Sstevel@tonic-gate 	mblk_t		*fill;
6820Sstevel@tonic-gate 	sctp_data_hdr_t	*sdh;
6830Sstevel@tonic-gate 	int		msglen;
6840Sstevel@tonic-gate 	int		extra;
6850Sstevel@tonic-gate 	sctp_msg_hdr_t	*msg_hdr;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	for (meta = sctp->sctp_xmit_head; meta != NULL; meta = meta->b_next) {
6880Sstevel@tonic-gate 		msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
6890Sstevel@tonic-gate 		if (SCTP_IS_MSG_ABANDONED(meta) ||
6900Sstevel@tonic-gate 		    SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
6910Sstevel@tonic-gate 			continue;
6920Sstevel@tonic-gate 		}
6930Sstevel@tonic-gate 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
6940Sstevel@tonic-gate 			if (SCTP_CHUNK_WANT_REXMIT(mp)) {
6950Sstevel@tonic-gate 				/*
6960Sstevel@tonic-gate 				 * Use the same peer address to do fast
6970Sstevel@tonic-gate 				 * retransmission.
6980Sstevel@tonic-gate 				 */
6990Sstevel@tonic-gate 				if (*fp == NULL) {
7000Sstevel@tonic-gate 					*fp = SCTP_CHUNK_DEST(mp);
7010Sstevel@tonic-gate 					if ((*fp)->state != SCTP_FADDRS_ALIVE)
7020Sstevel@tonic-gate 						*fp = sctp->sctp_current;
7030Sstevel@tonic-gate 				} else if (*fp != SCTP_CHUNK_DEST(mp)) {
7040Sstevel@tonic-gate 					continue;
7050Sstevel@tonic-gate 				}
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 				sdh = (sctp_data_hdr_t *)mp->b_rptr;
7080Sstevel@tonic-gate 				msglen = ntohs(sdh->sdh_len);
7090Sstevel@tonic-gate 				if ((extra = msglen & (SCTP_ALIGN - 1)) != 0) {
7100Sstevel@tonic-gate 					extra = SCTP_ALIGN - extra;
7110Sstevel@tonic-gate 				}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 				/*
7140Sstevel@tonic-gate 				 * We still return at least the first message
7150Sstevel@tonic-gate 				 * even if that message cannot fit in as
7160Sstevel@tonic-gate 				 * PMTU may have changed.
7170Sstevel@tonic-gate 				 */
7180Sstevel@tonic-gate 				if (*total + msglen + extra >
7190Sstevel@tonic-gate 				    (*fp)->sfa_pmss && start_mp != NULL) {
7200Sstevel@tonic-gate 					return (start_mp);
7210Sstevel@tonic-gate 				}
7220Sstevel@tonic-gate 				if ((nmp = dupmsg(mp)) == NULL)
7230Sstevel@tonic-gate 					return (start_mp);
7240Sstevel@tonic-gate 				if (extra > 0) {
7250Sstevel@tonic-gate 					fill = sctp_get_padding(extra);
7260Sstevel@tonic-gate 					if (fill != NULL) {
7270Sstevel@tonic-gate 						linkb(nmp, fill);
7280Sstevel@tonic-gate 					} else {
7290Sstevel@tonic-gate 						return (start_mp);
7300Sstevel@tonic-gate 					}
7310Sstevel@tonic-gate 				}
732852Svi117747 				BUMP_MIB(&sctp_mib, sctpOutFastRetrans);
7330Sstevel@tonic-gate 				SCTP_CHUNK_CLEAR_REXMIT(mp);
7340Sstevel@tonic-gate 				if (start_mp == NULL) {
7350Sstevel@tonic-gate 					start_mp = nmp;
7360Sstevel@tonic-gate 				} else {
7370Sstevel@tonic-gate 					linkb(end_mp, nmp);
7380Sstevel@tonic-gate 				}
7390Sstevel@tonic-gate 				end_mp = nmp;
7400Sstevel@tonic-gate 				*total += msglen + extra;
7410Sstevel@tonic-gate 				dprint(2, ("sctp_find_fast_rexmit_mblks: "
7420Sstevel@tonic-gate 				    "tsn %x\n", sdh->sdh_tsn));
7430Sstevel@tonic-gate 			}
7440Sstevel@tonic-gate 		}
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 	/* Clear the flag as there is no more message to be fast rexmitted. */
7470Sstevel@tonic-gate 	sctp->sctp_chk_fast_rexmit = B_FALSE;
7480Sstevel@tonic-gate 	return (start_mp);
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate /* A debug function just to make sure that a mblk chain is not broken */
7520Sstevel@tonic-gate #ifdef	DEBUG
7530Sstevel@tonic-gate static boolean_t
7540Sstevel@tonic-gate sctp_verify_chain(mblk_t *head, mblk_t *tail)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate 	mblk_t	*mp = head;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	if (head == NULL || tail == NULL)
7590Sstevel@tonic-gate 		return (B_TRUE);
7600Sstevel@tonic-gate 	while (mp != NULL) {
7610Sstevel@tonic-gate 		if (mp == tail)
7620Sstevel@tonic-gate 			return (B_TRUE);
7630Sstevel@tonic-gate 		mp = mp->b_next;
7640Sstevel@tonic-gate 	}
7650Sstevel@tonic-gate 	return (B_FALSE);
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate #endif
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate /*
7700Sstevel@tonic-gate  * Gets the next unsent chunk to transmit. Messages that are abandoned are
7710Sstevel@tonic-gate  * skipped. A message can be abandoned if it has a non-zero timetolive and
7720Sstevel@tonic-gate  * transmission has not yet started or if it is a partially reliable
7730Sstevel@tonic-gate  * message and its time is up (assuming we are PR-SCTP aware).
7740Sstevel@tonic-gate  * 'cansend' is used to determine if need to try and chunkify messages from
7750Sstevel@tonic-gate  * the unsent list, if any, and also as an input to sctp_chunkify() if so.
7760Sstevel@tonic-gate  * When called from sctp_rexmit(), we don't want to chunkify, so 'cansend'
7770Sstevel@tonic-gate  * will be set to 0.
7780Sstevel@tonic-gate  */
7790Sstevel@tonic-gate mblk_t *
7800Sstevel@tonic-gate sctp_get_msg_to_send(sctp_t *sctp, mblk_t **mp, mblk_t *meta, int  *error,
7810Sstevel@tonic-gate     int32_t firstseg, uint32_t cansend, sctp_faddr_t *fp)
7820Sstevel@tonic-gate {
7830Sstevel@tonic-gate 	mblk_t		*mp1;
7840Sstevel@tonic-gate 	sctp_msg_hdr_t	*msg_hdr;
7850Sstevel@tonic-gate 	mblk_t		*tmp_meta;
7860Sstevel@tonic-gate 	sctp_faddr_t	*fp1;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	ASSERT(error != NULL && mp != NULL);
7890Sstevel@tonic-gate 	*error = 0;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	ASSERT(sctp->sctp_current != NULL);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate chunkified:
7940Sstevel@tonic-gate 	while (meta != NULL) {
7950Sstevel@tonic-gate 		tmp_meta = meta->b_next;
7960Sstevel@tonic-gate 		msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
7970Sstevel@tonic-gate 		mp1 = meta->b_cont;
7980Sstevel@tonic-gate 		if (SCTP_IS_MSG_ABANDONED(meta))
7990Sstevel@tonic-gate 			goto next_msg;
8000Sstevel@tonic-gate 		if (!SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
8010Sstevel@tonic-gate 			while (mp1 != NULL) {
8020Sstevel@tonic-gate 				if (SCTP_CHUNK_CANSEND(mp1)) {
8030Sstevel@tonic-gate 					*mp = mp1;
8040Sstevel@tonic-gate #ifdef	DEBUG
8050Sstevel@tonic-gate 					ASSERT(sctp_verify_chain(
8060Sstevel@tonic-gate 					    sctp->sctp_xmit_head, meta));
8070Sstevel@tonic-gate #endif
8080Sstevel@tonic-gate 					return (meta);
8090Sstevel@tonic-gate 				}
8100Sstevel@tonic-gate 				mp1 = mp1->b_next;
8110Sstevel@tonic-gate 			}
8120Sstevel@tonic-gate 			goto next_msg;
8130Sstevel@tonic-gate 		}
8140Sstevel@tonic-gate 		/*
8150Sstevel@tonic-gate 		 * If we come here and the first chunk is sent, then we
8160Sstevel@tonic-gate 		 * we are PR-SCTP aware, in which case if the cumulative
8170Sstevel@tonic-gate 		 * TSN has moved upto or beyond the first chunk (which
8180Sstevel@tonic-gate 		 * means all the previous messages have been cumulative
8190Sstevel@tonic-gate 		 * SACK'd), then we send a Forward TSN with the last
8200Sstevel@tonic-gate 		 * chunk that was sent in this message. If we can't send
8210Sstevel@tonic-gate 		 * a Forward TSN because previous non-abandoned messages
8220Sstevel@tonic-gate 		 * have not been acked then we will defer the Forward TSN
8230Sstevel@tonic-gate 		 * to sctp_rexmit() or sctp_cumack().
8240Sstevel@tonic-gate 		 */
8250Sstevel@tonic-gate 		if (SCTP_CHUNK_ISSENT(mp1)) {
8260Sstevel@tonic-gate 			*error = sctp_check_abandoned_msg(sctp, meta);
8270Sstevel@tonic-gate 			if (*error != 0) {
8280Sstevel@tonic-gate #ifdef	DEBUG
8290Sstevel@tonic-gate 				ASSERT(sctp_verify_chain(sctp->sctp_xmit_head,
8300Sstevel@tonic-gate 				    sctp->sctp_xmit_tail));
8310Sstevel@tonic-gate #endif
8320Sstevel@tonic-gate 				return (NULL);
8330Sstevel@tonic-gate 			}
8340Sstevel@tonic-gate 			goto next_msg;
8350Sstevel@tonic-gate 		}
8360Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_prsctpdrop);
8370Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent >= msg_hdr->smh_msglen);
8380Sstevel@tonic-gate 		if (meta->b_prev == NULL) {
8390Sstevel@tonic-gate 			ASSERT(sctp->sctp_xmit_head == meta);
8400Sstevel@tonic-gate 			sctp->sctp_xmit_head = tmp_meta;
8410Sstevel@tonic-gate 			if (sctp->sctp_xmit_tail == meta)
8420Sstevel@tonic-gate 				sctp->sctp_xmit_tail = tmp_meta;
8430Sstevel@tonic-gate 			meta->b_next = NULL;
8440Sstevel@tonic-gate 			if (tmp_meta != NULL)
8450Sstevel@tonic-gate 				tmp_meta->b_prev = NULL;
8460Sstevel@tonic-gate 		} else if (meta->b_next == NULL) {
8470Sstevel@tonic-gate 			if (sctp->sctp_xmit_tail == meta)
8480Sstevel@tonic-gate 				sctp->sctp_xmit_tail = meta->b_prev;
8490Sstevel@tonic-gate 			meta->b_prev->b_next = NULL;
8500Sstevel@tonic-gate 			meta->b_prev = NULL;
8510Sstevel@tonic-gate 		} else {
8520Sstevel@tonic-gate 			meta->b_prev->b_next = tmp_meta;
8530Sstevel@tonic-gate 			tmp_meta->b_prev = meta->b_prev;
8540Sstevel@tonic-gate 			if (sctp->sctp_xmit_tail == meta)
8550Sstevel@tonic-gate 				sctp->sctp_xmit_tail = tmp_meta;
8560Sstevel@tonic-gate 			meta->b_prev = NULL;
8570Sstevel@tonic-gate 			meta->b_next = NULL;
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 		sctp->sctp_unsent -= msg_hdr->smh_msglen;
8600Sstevel@tonic-gate 		/*
8610Sstevel@tonic-gate 		 * Update ULP the amount of queued data, which is
8620Sstevel@tonic-gate 		 * sent-unack'ed + unsent.
8630Sstevel@tonic-gate 		 */
8640Sstevel@tonic-gate 		if (!SCTP_IS_DETACHED(sctp)) {
8650Sstevel@tonic-gate 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
8660Sstevel@tonic-gate 			    sctp->sctp_unacked + sctp->sctp_unsent);
8670Sstevel@tonic-gate 		}
8680Sstevel@tonic-gate 		sctp_sendfail_event(sctp, meta, 0, B_TRUE);
8690Sstevel@tonic-gate next_msg:
8700Sstevel@tonic-gate 		meta = tmp_meta;
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate 	/* chunkify, if needed */
8730Sstevel@tonic-gate 	if (cansend > 0 && sctp->sctp_xmit_unsent != NULL) {
8740Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent > 0);
8750Sstevel@tonic-gate 		if (fp == NULL) {
8760Sstevel@tonic-gate 			fp = SCTP_CHUNK_DEST(sctp->sctp_xmit_unsent);
8770Sstevel@tonic-gate 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
8780Sstevel@tonic-gate 				fp = sctp->sctp_current;
8790Sstevel@tonic-gate 		} else {
8800Sstevel@tonic-gate 			/*
8810Sstevel@tonic-gate 			 * If user specified destination, try to honor that.
8820Sstevel@tonic-gate 			 */
8830Sstevel@tonic-gate 			fp1 = SCTP_CHUNK_DEST(sctp->sctp_xmit_unsent);
8840Sstevel@tonic-gate 			if (fp1 != NULL && fp1->state == SCTP_FADDRS_ALIVE &&
8850Sstevel@tonic-gate 			    fp1 != fp) {
8860Sstevel@tonic-gate 				goto chunk_done;
8870Sstevel@tonic-gate 			}
8880Sstevel@tonic-gate 		}
8890Sstevel@tonic-gate 		sctp_chunkify(sctp, fp->sfa_pmss - firstseg, cansend);
8900Sstevel@tonic-gate 		if ((meta = sctp->sctp_xmit_tail) == NULL)
8910Sstevel@tonic-gate 			goto chunk_done;
8920Sstevel@tonic-gate 		/*
8930Sstevel@tonic-gate 		 * sctp_chunkify() won't advance sctp_xmit_tail if it adds
8940Sstevel@tonic-gate 		 * new chunk(s) to the tail, so we need to skip the
8950Sstevel@tonic-gate 		 * sctp_xmit_tail, which would have already been processed.
8960Sstevel@tonic-gate 		 * This could happen when there is unacked chunks, but
8970Sstevel@tonic-gate 		 * nothing new to send.
8980Sstevel@tonic-gate 		 * When sctp_chunkify() is called when the transmit queue
8990Sstevel@tonic-gate 		 * is empty then we need to start from sctp_xmit_tail.
9000Sstevel@tonic-gate 		 */
9010Sstevel@tonic-gate 		if (SCTP_CHUNK_ISSENT(sctp->sctp_xmit_tail->b_cont)) {
9020Sstevel@tonic-gate #ifdef	DEBUG
9030Sstevel@tonic-gate 			mp1 = sctp->sctp_xmit_tail->b_cont;
9040Sstevel@tonic-gate 			while (mp1 != NULL) {
9050Sstevel@tonic-gate 				ASSERT(!SCTP_CHUNK_CANSEND(mp1));
9060Sstevel@tonic-gate 				mp1 = mp1->b_next;
9070Sstevel@tonic-gate 			}
9080Sstevel@tonic-gate #endif
9090Sstevel@tonic-gate 			if ((meta = sctp->sctp_xmit_tail->b_next) == NULL)
9100Sstevel@tonic-gate 				goto chunk_done;
9110Sstevel@tonic-gate 		}
9120Sstevel@tonic-gate 		goto chunkified;
9130Sstevel@tonic-gate 	}
9140Sstevel@tonic-gate chunk_done:
9150Sstevel@tonic-gate #ifdef	DEBUG
9160Sstevel@tonic-gate 	ASSERT(sctp_verify_chain(sctp->sctp_xmit_head, sctp->sctp_xmit_tail));
9170Sstevel@tonic-gate #endif
9180Sstevel@tonic-gate 	return (NULL);
9190Sstevel@tonic-gate }
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate void
9220Sstevel@tonic-gate sctp_fast_rexmit(sctp_t *sctp)
9230Sstevel@tonic-gate {
9240Sstevel@tonic-gate 	mblk_t		*mp, *head;
9250Sstevel@tonic-gate 	int		pktlen = 0;
9260Sstevel@tonic-gate 	sctp_faddr_t	*fp = NULL;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head != NULL);
9290Sstevel@tonic-gate 	mp = sctp_find_fast_rexmit_mblks(sctp, &pktlen, &fp);
9300Sstevel@tonic-gate 	if (mp == NULL)
9310Sstevel@tonic-gate 		return;
932252Svi117747 	if ((head = sctp_add_proto_hdr(sctp, fp, mp, 0, NULL)) == NULL) {
9330Sstevel@tonic-gate 		freemsg(mp);
9340Sstevel@tonic-gate 		return;
9350Sstevel@tonic-gate 	}
9360Sstevel@tonic-gate 	if ((pktlen > fp->sfa_pmss) && fp->isv4) {
9370Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)head->b_rptr;
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 		iph->ipha_fragment_offset_and_flags = 0;
9400Sstevel@tonic-gate 	}
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	sctp_set_iplen(sctp, head);
9430Sstevel@tonic-gate 	sctp_add_sendq(sctp, head);
9440Sstevel@tonic-gate 	sctp->sctp_active = fp->lastactive = lbolt64;
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate void
9480Sstevel@tonic-gate sctp_output(sctp_t *sctp)
9490Sstevel@tonic-gate {
9500Sstevel@tonic-gate 	mblk_t			*mp = NULL;
9510Sstevel@tonic-gate 	mblk_t			*nmp;
9520Sstevel@tonic-gate 	mblk_t			*head;
9530Sstevel@tonic-gate 	mblk_t			*meta = sctp->sctp_xmit_tail;
9540Sstevel@tonic-gate 	mblk_t			*fill = NULL;
9550Sstevel@tonic-gate 	uint16_t 		chunklen;
9560Sstevel@tonic-gate 	uint32_t 		cansend;
9570Sstevel@tonic-gate 	int32_t			seglen;
9580Sstevel@tonic-gate 	int32_t			xtralen;
9590Sstevel@tonic-gate 	int32_t			sacklen;
9600Sstevel@tonic-gate 	int32_t			pad = 0;
9610Sstevel@tonic-gate 	int32_t			pathmax;
9620Sstevel@tonic-gate 	int			extra;
9630Sstevel@tonic-gate 	int64_t			now = lbolt64;
9640Sstevel@tonic-gate 	sctp_faddr_t		*fp;
9650Sstevel@tonic-gate 	sctp_faddr_t		*lfp;
9660Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
9670Sstevel@tonic-gate 	int			error;
968252Svi117747 	boolean_t		notsent = B_TRUE;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
9710Sstevel@tonic-gate 		sacklen = 0;
9720Sstevel@tonic-gate 	} else {
9730Sstevel@tonic-gate 		/* send a SACK chunk */
9740Sstevel@tonic-gate 		sacklen = sizeof (sctp_chunk_hdr_t) +
9750Sstevel@tonic-gate 		    sizeof (sctp_sack_chunk_t) +
9760Sstevel@tonic-gate 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
9770Sstevel@tonic-gate 		lfp = sctp->sctp_lastdata;
9780Sstevel@tonic-gate 		ASSERT(lfp != NULL);
9790Sstevel@tonic-gate 		if (lfp->state != SCTP_FADDRS_ALIVE)
9800Sstevel@tonic-gate 			lfp = sctp->sctp_current;
9810Sstevel@tonic-gate 	}
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	cansend = sctp->sctp_frwnd;
9840Sstevel@tonic-gate 	if (sctp->sctp_unsent < cansend)
9850Sstevel@tonic-gate 		cansend = sctp->sctp_unsent;
9860Sstevel@tonic-gate 	if ((cansend < sctp->sctp_current->sfa_pmss / 2) &&
9870Sstevel@tonic-gate 	    sctp->sctp_unacked &&
9880Sstevel@tonic-gate 	    (sctp->sctp_unacked < sctp->sctp_current->sfa_pmss) &&
9890Sstevel@tonic-gate 	    !sctp->sctp_ndelay) {
9900Sstevel@tonic-gate 		head = NULL;
9910Sstevel@tonic-gate 		fp = sctp->sctp_current;
9920Sstevel@tonic-gate 		goto unsent_data;
9930Sstevel@tonic-gate 	}
9940Sstevel@tonic-gate 	if (meta != NULL)
9950Sstevel@tonic-gate 		mp = meta->b_cont;
9960Sstevel@tonic-gate 	while (cansend > 0) {
9970Sstevel@tonic-gate 		pad = 0;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 		/*
10000Sstevel@tonic-gate 		 * Find first segment eligible for transmit.
10010Sstevel@tonic-gate 		 */
10020Sstevel@tonic-gate 		while (mp != NULL) {
10030Sstevel@tonic-gate 			if (SCTP_CHUNK_CANSEND(mp))
10040Sstevel@tonic-gate 				break;
10050Sstevel@tonic-gate 			mp = mp->b_next;
10060Sstevel@tonic-gate 		}
10070Sstevel@tonic-gate 		if (mp == NULL) {
10080Sstevel@tonic-gate 			meta = sctp_get_msg_to_send(sctp, &mp,
10090Sstevel@tonic-gate 			    meta == NULL ? NULL : meta->b_next, &error, sacklen,
10100Sstevel@tonic-gate 			    cansend, NULL);
10110Sstevel@tonic-gate 			if (error != 0 || meta == NULL) {
10120Sstevel@tonic-gate 				head = NULL;
10130Sstevel@tonic-gate 				fp = sctp->sctp_current;
10140Sstevel@tonic-gate 				goto unsent_data;
10150Sstevel@tonic-gate 			}
10160Sstevel@tonic-gate 			sctp->sctp_xmit_tail =  meta;
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
10200Sstevel@tonic-gate 		seglen = ntohs(sdc->sdh_len);
10210Sstevel@tonic-gate 		xtralen = sizeof (*sdc);
10220Sstevel@tonic-gate 		chunklen = seglen - xtralen;
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 		/*
10250Sstevel@tonic-gate 		 * Check rwnd.
10260Sstevel@tonic-gate 		 */
10270Sstevel@tonic-gate 		if (chunklen > cansend) {
10280Sstevel@tonic-gate 			head = NULL;
10290Sstevel@tonic-gate 			fp = SCTP_CHUNK_DEST(meta);
10300Sstevel@tonic-gate 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
10310Sstevel@tonic-gate 				fp = sctp->sctp_current;
10320Sstevel@tonic-gate 			goto unsent_data;
10330Sstevel@tonic-gate 		}
10340Sstevel@tonic-gate 		if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
10350Sstevel@tonic-gate 			extra = SCTP_ALIGN - extra;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		/*
10380Sstevel@tonic-gate 		 * Pick destination address, and check cwnd.
10390Sstevel@tonic-gate 		 */
10400Sstevel@tonic-gate 		if (sacklen > 0 && (seglen + extra <= lfp->cwnd - lfp->suna) &&
10410Sstevel@tonic-gate 		    (seglen + sacklen + extra <= lfp->sfa_pmss)) {
10420Sstevel@tonic-gate 			/*
10430Sstevel@tonic-gate 			 * Only include SACK chunk if it can be bundled
10440Sstevel@tonic-gate 			 * with a data chunk, and sent to sctp_lastdata.
10450Sstevel@tonic-gate 			 */
10460Sstevel@tonic-gate 			pathmax = lfp->cwnd - lfp->suna;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 			fp = lfp;
10490Sstevel@tonic-gate 			if ((nmp = dupmsg(mp)) == NULL) {
10500Sstevel@tonic-gate 				head = NULL;
10510Sstevel@tonic-gate 				goto unsent_data;
10520Sstevel@tonic-gate 			}
10530Sstevel@tonic-gate 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1054252Svi117747 			head = sctp_add_proto_hdr(sctp, fp, nmp, sacklen,
1055252Svi117747 			    &error);
10560Sstevel@tonic-gate 			if (head == NULL) {
1057252Svi117747 				/*
1058252Svi117747 				 * If none of the source addresses are
1059252Svi117747 				 * available (i.e error == EHOSTUNREACH),
1060252Svi117747 				 * pretend we have sent the data. We will
1061252Svi117747 				 * eventually time out trying to retramsmit
1062252Svi117747 				 * the data if the interface never comes up.
1063252Svi117747 				 * If we have already sent some stuff (i.e.,
1064252Svi117747 				 * notsent is B_FALSE) then we are fine, else
1065252Svi117747 				 * just mark this packet as sent.
1066252Svi117747 				 */
1067252Svi117747 				if (notsent && error == EHOSTUNREACH) {
1068252Svi117747 					SCTP_CHUNK_SENT(sctp, mp, sdc,
1069252Svi117747 					    fp, chunklen, meta);
1070252Svi117747 				}
10710Sstevel@tonic-gate 				freemsg(nmp);
10720Sstevel@tonic-gate 				goto unsent_data;
10730Sstevel@tonic-gate 			}
10740Sstevel@tonic-gate 			seglen += sacklen;
10750Sstevel@tonic-gate 			xtralen += sacklen;
10760Sstevel@tonic-gate 			sacklen = 0;
10770Sstevel@tonic-gate 		} else {
10780Sstevel@tonic-gate 			fp = SCTP_CHUNK_DEST(meta);
10790Sstevel@tonic-gate 			if (fp == NULL || fp->state != SCTP_FADDRS_ALIVE)
10800Sstevel@tonic-gate 				fp = sctp->sctp_current;
10810Sstevel@tonic-gate 			/*
10820Sstevel@tonic-gate 			 * If we haven't sent data to this destination for
10830Sstevel@tonic-gate 			 * a while, do slow start again.
10840Sstevel@tonic-gate 			 */
10850Sstevel@tonic-gate 			if (now - fp->lastactive > fp->rto) {
10860Sstevel@tonic-gate 				fp->cwnd = sctp_slow_start_after_idle *
10870Sstevel@tonic-gate 				    fp->sfa_pmss;
10880Sstevel@tonic-gate 			}
10890Sstevel@tonic-gate 			fp->lastactive = now;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 			pathmax = fp->cwnd - fp->suna;
10920Sstevel@tonic-gate 			if (seglen + extra > pathmax) {
10930Sstevel@tonic-gate 				head = NULL;
10940Sstevel@tonic-gate 				goto unsent_data;
10950Sstevel@tonic-gate 			}
10960Sstevel@tonic-gate 			if ((nmp = dupmsg(mp)) == NULL) {
10970Sstevel@tonic-gate 				head = NULL;
10980Sstevel@tonic-gate 				goto unsent_data;
10990Sstevel@tonic-gate 			}
11000Sstevel@tonic-gate 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1101252Svi117747 			head = sctp_add_proto_hdr(sctp, fp, nmp, 0, &error);
11020Sstevel@tonic-gate 			if (head == NULL) {
1103252Svi117747 				/*
1104252Svi117747 				 * If none of the source addresses are
1105252Svi117747 				 * available (i.e error == EHOSTUNREACH),
1106252Svi117747 				 * pretend we have sent the data. We will
1107252Svi117747 				 * eventually time out trying to retramsmit
1108252Svi117747 				 * the data if the interface never comes up.
1109252Svi117747 				 * If we have already sent some stuff (i.e.,
1110252Svi117747 				 * notsent is B_FALSE) then we are fine, else
1111252Svi117747 				 * just mark this packet as sent.
1112252Svi117747 				 */
1113252Svi117747 				if (notsent && error == EHOSTUNREACH) {
1114252Svi117747 					SCTP_CHUNK_SENT(sctp, mp, sdc,
1115252Svi117747 					    fp, chunklen, meta);
1116252Svi117747 				}
11170Sstevel@tonic-gate 				freemsg(nmp);
11180Sstevel@tonic-gate 				goto unsent_data;
11190Sstevel@tonic-gate 			}
11200Sstevel@tonic-gate 		}
11210Sstevel@tonic-gate 		if (pathmax > fp->sfa_pmss)
11220Sstevel@tonic-gate 			pathmax = fp->sfa_pmss;
11230Sstevel@tonic-gate 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
11240Sstevel@tonic-gate 		mp = mp->b_next;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 		/* Use this chunk to measure RTT? */
11270Sstevel@tonic-gate 		if (sctp->sctp_out_time == 0) {
11280Sstevel@tonic-gate 			sctp->sctp_out_time = now;
11290Sstevel@tonic-gate 			sctp->sctp_rtt_tsn = sctp->sctp_ltsn - 1;
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 		if (extra > 0) {
11320Sstevel@tonic-gate 			fill = sctp_get_padding(extra);
11330Sstevel@tonic-gate 			if (fill != NULL) {
11340Sstevel@tonic-gate 				linkb(head, fill);
11350Sstevel@tonic-gate 				pad = extra;
11360Sstevel@tonic-gate 				seglen += extra;
11370Sstevel@tonic-gate 			} else {
11380Sstevel@tonic-gate 				goto unsent_data;
11390Sstevel@tonic-gate 			}
11400Sstevel@tonic-gate 		}
11410Sstevel@tonic-gate 		/* See if we can bundle more. */
11420Sstevel@tonic-gate 		while (seglen < pathmax) {
11430Sstevel@tonic-gate 			int32_t		new_len;
11440Sstevel@tonic-gate 			int32_t		new_xtralen;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 			while (mp != NULL) {
11470Sstevel@tonic-gate 				if (SCTP_CHUNK_CANSEND(mp))
11480Sstevel@tonic-gate 					break;
11490Sstevel@tonic-gate 				mp = mp->b_next;
11500Sstevel@tonic-gate 			}
11510Sstevel@tonic-gate 			if (mp == NULL) {
11520Sstevel@tonic-gate 				meta = sctp_get_msg_to_send(sctp, &mp,
11530Sstevel@tonic-gate 				    meta->b_next, &error, seglen,
11540Sstevel@tonic-gate 				    (seglen - xtralen) >= cansend ? 0 :
11550Sstevel@tonic-gate 				    cansend - seglen, fp);
11560Sstevel@tonic-gate 				if (error != 0 || meta == NULL)
11570Sstevel@tonic-gate 					break;
11580Sstevel@tonic-gate 				sctp->sctp_xmit_tail =  meta;
11590Sstevel@tonic-gate 			}
11600Sstevel@tonic-gate 			ASSERT(mp != NULL);
11610Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp) && SCTP_CHUNK_DEST(meta) &&
11620Sstevel@tonic-gate 			    fp != SCTP_CHUNK_DEST(meta)) {
11630Sstevel@tonic-gate 				break;
11640Sstevel@tonic-gate 			}
11650Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
11660Sstevel@tonic-gate 			chunklen = ntohs(sdc->sdh_len);
11670Sstevel@tonic-gate 			if ((extra = chunklen  & (SCTP_ALIGN - 1)) != 0)
11680Sstevel@tonic-gate 				extra = SCTP_ALIGN - extra;
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 			new_len = seglen + chunklen;
11710Sstevel@tonic-gate 			new_xtralen = xtralen + sizeof (*sdc);
11720Sstevel@tonic-gate 			chunklen -= sizeof (*sdc);
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 			if (new_len - new_xtralen > cansend ||
11750Sstevel@tonic-gate 			    new_len + extra > pathmax) {
11760Sstevel@tonic-gate 				break;
11770Sstevel@tonic-gate 			}
11780Sstevel@tonic-gate 			if ((nmp = dupmsg(mp)) == NULL)
11790Sstevel@tonic-gate 				break;
11800Sstevel@tonic-gate 			if (extra > 0) {
11810Sstevel@tonic-gate 				fill = sctp_get_padding(extra);
11820Sstevel@tonic-gate 				if (fill != NULL) {
11830Sstevel@tonic-gate 					pad += extra;
11840Sstevel@tonic-gate 					new_len += extra;
11850Sstevel@tonic-gate 					linkb(nmp, fill);
11860Sstevel@tonic-gate 				} else {
11870Sstevel@tonic-gate 					freemsg(nmp);
11880Sstevel@tonic-gate 					break;
11890Sstevel@tonic-gate 				}
11900Sstevel@tonic-gate 			}
11910Sstevel@tonic-gate 			seglen = new_len;
11920Sstevel@tonic-gate 			xtralen = new_xtralen;
11930Sstevel@tonic-gate 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
11940Sstevel@tonic-gate 			SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
11950Sstevel@tonic-gate 			linkb(head, nmp);
11960Sstevel@tonic-gate 			mp = mp->b_next;
11970Sstevel@tonic-gate 		}
11980Sstevel@tonic-gate 		if ((seglen > fp->sfa_pmss) && fp->isv4) {
11990Sstevel@tonic-gate 			ipha_t *iph = (ipha_t *)head->b_rptr;
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 			/*
12020Sstevel@tonic-gate 			 * Path MTU is different from what we thought it would
12030Sstevel@tonic-gate 			 * be when we created chunks, or IP headers have grown.
12040Sstevel@tonic-gate 			 * Need to clear the DF bit.
12050Sstevel@tonic-gate 			 */
12060Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = 0;
12070Sstevel@tonic-gate 		}
12080Sstevel@tonic-gate 		/* xmit segment */
12090Sstevel@tonic-gate 		ASSERT(cansend >= seglen - pad - xtralen);
12100Sstevel@tonic-gate 		cansend -= (seglen - pad - xtralen);
12110Sstevel@tonic-gate 		dprint(2, ("sctp_output: Sending packet %d bytes, tsn %x "
1212*1676Sjpk 		    "ssn %d to %p (rwnd %d, cansend %d, lastack_rxd %x)\n",
1213*1676Sjpk 		    seglen - xtralen, ntohl(sdc->sdh_tsn),
1214*1676Sjpk 		    ntohs(sdc->sdh_ssn), (void *)fp, sctp->sctp_frwnd,
1215*1676Sjpk 		    cansend, sctp->sctp_lastack_rxd));
12160Sstevel@tonic-gate 		sctp_set_iplen(sctp, head);
12170Sstevel@tonic-gate 		sctp_add_sendq(sctp, head);
12180Sstevel@tonic-gate 		/* arm rto timer (if not set) */
12190Sstevel@tonic-gate 		if (!fp->timer_running)
12200Sstevel@tonic-gate 			SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1221252Svi117747 		notsent = B_FALSE;
12220Sstevel@tonic-gate 	}
12230Sstevel@tonic-gate 	sctp->sctp_active = now;
12240Sstevel@tonic-gate 	return;
12250Sstevel@tonic-gate unsent_data:
12260Sstevel@tonic-gate 	/* arm persist timer (if rto timer not set) */
12270Sstevel@tonic-gate 	if (!fp->timer_running)
12280Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
12290Sstevel@tonic-gate 	if (head != NULL)
12300Sstevel@tonic-gate 		freemsg(head);
12310Sstevel@tonic-gate }
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate /*
12340Sstevel@tonic-gate  * The following two functions initialize and destroy the cache
12350Sstevel@tonic-gate  * associated with the sets used for PR-SCTP.
12360Sstevel@tonic-gate  */
12370Sstevel@tonic-gate void
12380Sstevel@tonic-gate sctp_ftsn_sets_init(void)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate 	sctp_kmem_ftsn_set_cache = kmem_cache_create("sctp_ftsn_set_cache",
12410Sstevel@tonic-gate 	    sizeof (sctp_ftsn_set_t), 0, NULL, NULL, NULL, NULL,
12420Sstevel@tonic-gate 	    NULL, 0);
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate void
12460Sstevel@tonic-gate sctp_ftsn_sets_fini(void)
12470Sstevel@tonic-gate {
12480Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_ftsn_set_cache);
12490Sstevel@tonic-gate }
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate /* Free PR-SCTP sets */
12530Sstevel@tonic-gate void
12540Sstevel@tonic-gate sctp_free_ftsn_set(sctp_ftsn_set_t *s)
12550Sstevel@tonic-gate {
12560Sstevel@tonic-gate 	sctp_ftsn_set_t *p;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	while (s != NULL) {
12590Sstevel@tonic-gate 		p = s->next;
12600Sstevel@tonic-gate 		s->next = NULL;
12610Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_ftsn_set_cache, s);
12620Sstevel@tonic-gate 		s = p;
12630Sstevel@tonic-gate 	}
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate /*
12670Sstevel@tonic-gate  * Given a message meta block, meta, this routine creates or modifies
12680Sstevel@tonic-gate  * the set that will be used to generate a Forward TSN chunk. If the
12690Sstevel@tonic-gate  * entry for stream id, sid, for this message already exists, the
12700Sstevel@tonic-gate  * sequence number, ssn, is updated if it is greater than the existing
12710Sstevel@tonic-gate  * one. If an entry for this sid does not exist, one is created if
12720Sstevel@tonic-gate  * the size does not exceed fp->sfa_pmss. We return false in case
12730Sstevel@tonic-gate  * or an error.
12740Sstevel@tonic-gate  */
12750Sstevel@tonic-gate boolean_t
12760Sstevel@tonic-gate sctp_add_ftsn_set(sctp_ftsn_set_t **s, sctp_faddr_t *fp, mblk_t *meta,
12770Sstevel@tonic-gate     uint_t *nsets, uint32_t *slen)
12780Sstevel@tonic-gate {
12790Sstevel@tonic-gate 	sctp_ftsn_set_t		*p;
12800Sstevel@tonic-gate 	sctp_msg_hdr_t		*msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
12810Sstevel@tonic-gate 	uint16_t		sid = htons(msg_hdr->smh_sid);
12820Sstevel@tonic-gate 	/* msg_hdr->smh_ssn is already in NBO */
12830Sstevel@tonic-gate 	uint16_t		ssn = msg_hdr->smh_ssn;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	ASSERT(s != NULL && nsets != NULL);
12860Sstevel@tonic-gate 	ASSERT((*nsets == 0 && *s == NULL) || (*nsets > 0 && *s != NULL));
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	if (*s == NULL) {
12890Sstevel@tonic-gate 		ASSERT((*slen + sizeof (uint32_t)) <= fp->sfa_pmss);
12900Sstevel@tonic-gate 		*s = kmem_cache_alloc(sctp_kmem_ftsn_set_cache, KM_NOSLEEP);
12910Sstevel@tonic-gate 		if (*s == NULL)
12920Sstevel@tonic-gate 			return (B_FALSE);
12930Sstevel@tonic-gate 		(*s)->ftsn_entries.ftsn_sid = sid;
12940Sstevel@tonic-gate 		(*s)->ftsn_entries.ftsn_ssn = ssn;
12950Sstevel@tonic-gate 		(*s)->next = NULL;
12960Sstevel@tonic-gate 		*nsets = 1;
12970Sstevel@tonic-gate 		*slen += sizeof (uint32_t);
12980Sstevel@tonic-gate 		return (B_TRUE);
12990Sstevel@tonic-gate 	}
13000Sstevel@tonic-gate 	for (p = *s; p->next != NULL; p = p->next) {
13010Sstevel@tonic-gate 		if (p->ftsn_entries.ftsn_sid == sid) {
13020Sstevel@tonic-gate 			if (SSN_GT(ssn, p->ftsn_entries.ftsn_ssn))
13030Sstevel@tonic-gate 				p->ftsn_entries.ftsn_ssn = ssn;
13040Sstevel@tonic-gate 			return (B_TRUE);
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate 	/* the last one */
13080Sstevel@tonic-gate 	if (p->ftsn_entries.ftsn_sid == sid) {
13090Sstevel@tonic-gate 		if (SSN_GT(ssn, p->ftsn_entries.ftsn_ssn))
13100Sstevel@tonic-gate 			p->ftsn_entries.ftsn_ssn = ssn;
13110Sstevel@tonic-gate 	} else {
13120Sstevel@tonic-gate 		if ((*slen + sizeof (uint32_t)) > fp->sfa_pmss)
13130Sstevel@tonic-gate 			return (B_FALSE);
13140Sstevel@tonic-gate 		p->next = kmem_cache_alloc(sctp_kmem_ftsn_set_cache,
13150Sstevel@tonic-gate 		    KM_NOSLEEP);
13160Sstevel@tonic-gate 		if (p->next == NULL)
13170Sstevel@tonic-gate 			return (B_FALSE);
13180Sstevel@tonic-gate 		p = p->next;
13190Sstevel@tonic-gate 		p->ftsn_entries.ftsn_sid = sid;
13200Sstevel@tonic-gate 		p->ftsn_entries.ftsn_ssn = ssn;
13210Sstevel@tonic-gate 		p->next = NULL;
13220Sstevel@tonic-gate 		(*nsets)++;
13230Sstevel@tonic-gate 		*slen += sizeof (uint32_t);
13240Sstevel@tonic-gate 	}
13250Sstevel@tonic-gate 	return (B_TRUE);
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate /*
13290Sstevel@tonic-gate  * Given a set of stream id - sequence number pairs, this routing creates
13300Sstevel@tonic-gate  * a Forward TSN chunk. The cumulative TSN (advanced peer ack point)
13310Sstevel@tonic-gate  * for the chunk is obtained from sctp->sctp_adv_pap. The caller
13320Sstevel@tonic-gate  * will add the IP/SCTP header.
13330Sstevel@tonic-gate  */
13340Sstevel@tonic-gate mblk_t *
13350Sstevel@tonic-gate sctp_make_ftsn_chunk(sctp_t *sctp, sctp_faddr_t *fp, sctp_ftsn_set_t *sets,
13360Sstevel@tonic-gate     uint_t nsets, uint32_t seglen)
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate 	mblk_t			*ftsn_mp;
13390Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ch_hdr;
13400Sstevel@tonic-gate 	uint32_t		*advtsn;
13410Sstevel@tonic-gate 	uint16_t		schlen;
13420Sstevel@tonic-gate 	size_t			xtralen;
13430Sstevel@tonic-gate 	ftsn_entry_t		*ftsn_entry;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	seglen += sizeof (sctp_chunk_hdr_t);
13460Sstevel@tonic-gate 	if (fp->isv4)
13470Sstevel@tonic-gate 		xtralen = sctp->sctp_hdr_len + sctp_wroff_xtra;
13480Sstevel@tonic-gate 	else
13490Sstevel@tonic-gate 		xtralen = sctp->sctp_hdr6_len + sctp_wroff_xtra;
1350*1676Sjpk 	ftsn_mp = allocb_cred(xtralen + seglen, CONN_CRED(sctp->sctp_connp));
13510Sstevel@tonic-gate 	if (ftsn_mp == NULL)
13520Sstevel@tonic-gate 		return (NULL);
13530Sstevel@tonic-gate 	ftsn_mp->b_rptr += xtralen;
13540Sstevel@tonic-gate 	ftsn_mp->b_wptr = ftsn_mp->b_rptr + seglen;
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	ch_hdr = (sctp_chunk_hdr_t *)ftsn_mp->b_rptr;
13570Sstevel@tonic-gate 	ch_hdr->sch_id = CHUNK_FORWARD_TSN;
13580Sstevel@tonic-gate 	ch_hdr->sch_flags = 0;
13590Sstevel@tonic-gate 	/*
13600Sstevel@tonic-gate 	 * The cast here should not be an issue since seglen is
13610Sstevel@tonic-gate 	 * the length of the Forward TSN chunk.
13620Sstevel@tonic-gate 	 */
13630Sstevel@tonic-gate 	schlen = (uint16_t)seglen;
13640Sstevel@tonic-gate 	U16_TO_ABE16(schlen, &(ch_hdr->sch_len));
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	advtsn = (uint32_t *)(ch_hdr + 1);
13670Sstevel@tonic-gate 	U32_TO_ABE32(sctp->sctp_adv_pap, advtsn);
13680Sstevel@tonic-gate 	ftsn_entry = (ftsn_entry_t *)(advtsn + 1);
13690Sstevel@tonic-gate 	while (nsets > 0) {
13700Sstevel@tonic-gate 		ASSERT((uchar_t *)&ftsn_entry[1] <= ftsn_mp->b_wptr);
13710Sstevel@tonic-gate 		ftsn_entry->ftsn_sid = sets->ftsn_entries.ftsn_sid;
13720Sstevel@tonic-gate 		ftsn_entry->ftsn_ssn = sets->ftsn_entries.ftsn_ssn;
13730Sstevel@tonic-gate 		ftsn_entry++;
13740Sstevel@tonic-gate 		sets = sets->next;
13750Sstevel@tonic-gate 		nsets--;
13760Sstevel@tonic-gate 	}
13770Sstevel@tonic-gate 	return (ftsn_mp);
13780Sstevel@tonic-gate }
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate /*
13810Sstevel@tonic-gate  * Given a starting message, the routine steps through all the
13820Sstevel@tonic-gate  * messages whose TSN is less than sctp->sctp_adv_pap and creates
13830Sstevel@tonic-gate  * ftsn sets. The ftsn sets is then used to create an Forward TSN
13840Sstevel@tonic-gate  * chunk. All the messages, that have chunks that are included in the
13850Sstevel@tonic-gate  * ftsn sets, are flagged abandonded. If a message is partially sent
13860Sstevel@tonic-gate  * and is deemed abandoned, all remaining unsent chunks are marked
13870Sstevel@tonic-gate  * abandoned and are deducted from sctp_unsent.
13880Sstevel@tonic-gate  */
13890Sstevel@tonic-gate void
13900Sstevel@tonic-gate sctp_make_ftsns(sctp_t *sctp, mblk_t *meta, mblk_t *mp, mblk_t **nmp,
13910Sstevel@tonic-gate     sctp_faddr_t *fp, uint32_t *seglen)
13920Sstevel@tonic-gate {
13930Sstevel@tonic-gate 	mblk_t		*mp1 = mp;
13940Sstevel@tonic-gate 	mblk_t		*mp_head = mp;
13950Sstevel@tonic-gate 	mblk_t		*meta_head = meta;
13960Sstevel@tonic-gate 	mblk_t		*head;
13970Sstevel@tonic-gate 	sctp_ftsn_set_t	*sets = NULL;
13980Sstevel@tonic-gate 	uint_t		nsets = 0;
13990Sstevel@tonic-gate 	uint16_t	clen;
14000Sstevel@tonic-gate 	sctp_data_hdr_t	*sdc;
14010Sstevel@tonic-gate 	uint32_t	sacklen;
14020Sstevel@tonic-gate 	uint32_t	adv_pap = sctp->sctp_adv_pap;
14030Sstevel@tonic-gate 	uint32_t	unsent = 0;
14040Sstevel@tonic-gate 	boolean_t	ubit;
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	*seglen = sizeof (uint32_t);
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
14090Sstevel@tonic-gate 	while (meta != NULL &&
14100Sstevel@tonic-gate 	    SEQ_GEQ(sctp->sctp_adv_pap, ntohl(sdc->sdh_tsn))) {
14110Sstevel@tonic-gate 		/*
14120Sstevel@tonic-gate 		 * Skip adding FTSN sets for un-ordered messages as they do
14130Sstevel@tonic-gate 		 * not have SSNs.
14140Sstevel@tonic-gate 		 */
14150Sstevel@tonic-gate 		ubit = SCTP_DATA_GET_UBIT(sdc);
14160Sstevel@tonic-gate 		if (!ubit &&
14170Sstevel@tonic-gate 		    !sctp_add_ftsn_set(&sets, fp, meta, &nsets, seglen)) {
14180Sstevel@tonic-gate 			meta = NULL;
14190Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
14200Sstevel@tonic-gate 			goto ftsn_done;
14210Sstevel@tonic-gate 		}
14220Sstevel@tonic-gate 		while (mp1 != NULL && SCTP_CHUNK_ISSENT(mp1)) {
14230Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
14240Sstevel@tonic-gate 			adv_pap = ntohl(sdc->sdh_tsn);
14250Sstevel@tonic-gate 			mp1 = mp1->b_next;
14260Sstevel@tonic-gate 		}
14270Sstevel@tonic-gate 		meta = meta->b_next;
14280Sstevel@tonic-gate 		if (meta != NULL) {
14290Sstevel@tonic-gate 			mp1 = meta->b_cont;
14300Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp1))
14310Sstevel@tonic-gate 				break;
14320Sstevel@tonic-gate 			sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
14330Sstevel@tonic-gate 		}
14340Sstevel@tonic-gate 	}
14350Sstevel@tonic-gate ftsn_done:
14360Sstevel@tonic-gate 	/*
14370Sstevel@tonic-gate 	 * Can't compare with sets == NULL, since we don't add any
14380Sstevel@tonic-gate 	 * sets for un-ordered messages.
14390Sstevel@tonic-gate 	 */
14400Sstevel@tonic-gate 	if (meta == meta_head)
14410Sstevel@tonic-gate 		return;
14420Sstevel@tonic-gate 	*nmp = sctp_make_ftsn_chunk(sctp, fp, sets, nsets, *seglen);
14430Sstevel@tonic-gate 	sctp_free_ftsn_set(sets);
14440Sstevel@tonic-gate 	if (*nmp == NULL)
14450Sstevel@tonic-gate 		return;
14460Sstevel@tonic-gate 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
14470Sstevel@tonic-gate 		sacklen = 0;
14480Sstevel@tonic-gate 	} else {
14490Sstevel@tonic-gate 		sacklen = sizeof (sctp_chunk_hdr_t) +
14500Sstevel@tonic-gate 		    sizeof (sctp_sack_chunk_t) +
14510Sstevel@tonic-gate 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
14520Sstevel@tonic-gate 		if (*seglen + sacklen > sctp->sctp_lastdata->sfa_pmss) {
14530Sstevel@tonic-gate 			/* piggybacked SACK doesn't fit */
14540Sstevel@tonic-gate 			sacklen = 0;
14550Sstevel@tonic-gate 		} else {
14560Sstevel@tonic-gate 			fp = sctp->sctp_lastdata;
14570Sstevel@tonic-gate 		}
14580Sstevel@tonic-gate 	}
1459252Svi117747 	head = sctp_add_proto_hdr(sctp, fp, *nmp, sacklen, NULL);
14600Sstevel@tonic-gate 	if (head == NULL) {
14610Sstevel@tonic-gate 		freemsg(*nmp);
14620Sstevel@tonic-gate 		*nmp = NULL;
14630Sstevel@tonic-gate 		return;
14640Sstevel@tonic-gate 	}
14650Sstevel@tonic-gate 	*seglen += sacklen;
14660Sstevel@tonic-gate 	*nmp = head;
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	/*
14690Sstevel@tonic-gate 	 * XXXNeed to optimise this, the reason it is done here is so
14700Sstevel@tonic-gate 	 * that we don't have to undo in case of failure.
14710Sstevel@tonic-gate 	 */
14720Sstevel@tonic-gate 	mp1 = mp_head;
14730Sstevel@tonic-gate 	sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
14740Sstevel@tonic-gate 	while (meta_head != NULL &&
14750Sstevel@tonic-gate 	    SEQ_GEQ(sctp->sctp_adv_pap, ntohl(sdc->sdh_tsn))) {
14760Sstevel@tonic-gate 		if (!SCTP_IS_MSG_ABANDONED(meta_head))
14770Sstevel@tonic-gate 			SCTP_MSG_SET_ABANDONED(meta_head);
14780Sstevel@tonic-gate 		while (mp1 != NULL && SCTP_CHUNK_ISSENT(mp1)) {
14790Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
14800Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISACKED(mp1)) {
14810Sstevel@tonic-gate 				clen = ntohs(sdc->sdh_len) - sizeof (*sdc);
14820Sstevel@tonic-gate 				SCTP_CHUNK_SENT(sctp, mp1, sdc, fp, clen,
14830Sstevel@tonic-gate 				    meta_head);
14840Sstevel@tonic-gate 			}
14850Sstevel@tonic-gate 			mp1 = mp1->b_next;
14860Sstevel@tonic-gate 		}
14870Sstevel@tonic-gate 		while (mp1 != NULL) {
14880Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
14890Sstevel@tonic-gate 			if (!SCTP_CHUNK_ABANDONED(mp1)) {
14900Sstevel@tonic-gate 				ASSERT(!SCTP_CHUNK_ISSENT(mp1));
14910Sstevel@tonic-gate 				unsent += ntohs(sdc->sdh_len) - sizeof (*sdc);
14920Sstevel@tonic-gate 				SCTP_ABANDON_CHUNK(mp1);
14930Sstevel@tonic-gate 			}
14940Sstevel@tonic-gate 			mp1 = mp1->b_next;
14950Sstevel@tonic-gate 		}
14960Sstevel@tonic-gate 		meta_head = meta_head->b_next;
14970Sstevel@tonic-gate 		if (meta_head != NULL) {
14980Sstevel@tonic-gate 			mp1 = meta_head->b_cont;
14990Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp1))
15000Sstevel@tonic-gate 				break;
15010Sstevel@tonic-gate 			sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
15020Sstevel@tonic-gate 		}
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate 	if (unsent > 0) {
15050Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent >= unsent);
15060Sstevel@tonic-gate 		sctp->sctp_unsent -= unsent;
15070Sstevel@tonic-gate 		/*
15080Sstevel@tonic-gate 		 * Update ULP the amount of queued data, which is
15090Sstevel@tonic-gate 		 * sent-unack'ed + unsent.
15100Sstevel@tonic-gate 		 */
15110Sstevel@tonic-gate 		if (!SCTP_IS_DETACHED(sctp)) {
15120Sstevel@tonic-gate 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
15130Sstevel@tonic-gate 			    sctp->sctp_unacked + sctp->sctp_unsent);
15140Sstevel@tonic-gate 		}
15150Sstevel@tonic-gate 	}
15160Sstevel@tonic-gate }
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate /*
15190Sstevel@tonic-gate  * This function steps through messages starting at meta and checks if
15200Sstevel@tonic-gate  * the message is abandoned. It stops when it hits an unsent chunk or
15210Sstevel@tonic-gate  * a message that has all its chunk acked. This is the only place
15220Sstevel@tonic-gate  * where the sctp_adv_pap is moved forward to indicated abandoned
15230Sstevel@tonic-gate  * messages.
15240Sstevel@tonic-gate  */
15250Sstevel@tonic-gate void
15260Sstevel@tonic-gate sctp_check_adv_ack_pt(sctp_t *sctp, mblk_t *meta, mblk_t *mp)
15270Sstevel@tonic-gate {
15280Sstevel@tonic-gate 	uint32_t	tsn = sctp->sctp_adv_pap;
15290Sstevel@tonic-gate 	sctp_data_hdr_t	*sdc;
15300Sstevel@tonic-gate 	sctp_msg_hdr_t	*msg_hdr;
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	ASSERT(mp != NULL);
15330Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
15340Sstevel@tonic-gate 	ASSERT(SEQ_GT(ntohl(sdc->sdh_tsn), sctp->sctp_lastack_rxd));
15350Sstevel@tonic-gate 	msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
15360Sstevel@tonic-gate 	if (!SCTP_IS_MSG_ABANDONED(meta) &&
15370Sstevel@tonic-gate 	    !SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
15380Sstevel@tonic-gate 		return;
15390Sstevel@tonic-gate 	}
15400Sstevel@tonic-gate 	while (meta != NULL) {
15410Sstevel@tonic-gate 		while (mp != NULL && SCTP_CHUNK_ISSENT(mp)) {
15420Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
15430Sstevel@tonic-gate 			tsn = ntohl(sdc->sdh_tsn);
15440Sstevel@tonic-gate 			mp = mp->b_next;
15450Sstevel@tonic-gate 		}
15460Sstevel@tonic-gate 		if (mp != NULL)
15470Sstevel@tonic-gate 			break;
15480Sstevel@tonic-gate 		/*
15490Sstevel@tonic-gate 		 * We continue checking for successive messages only if there
15500Sstevel@tonic-gate 		 * is a chunk marked for retransmission. Else, we might
15510Sstevel@tonic-gate 		 * end up sending FTSN prematurely for chunks that have been
15520Sstevel@tonic-gate 		 * sent, but not yet acked.
15530Sstevel@tonic-gate 		 */
15540Sstevel@tonic-gate 		if ((meta = meta->b_next) != NULL) {
15550Sstevel@tonic-gate 			msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
15560Sstevel@tonic-gate 			if (!SCTP_IS_MSG_ABANDONED(meta) &&
15570Sstevel@tonic-gate 			    !SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
15580Sstevel@tonic-gate 				break;
15590Sstevel@tonic-gate 			}
15600Sstevel@tonic-gate 			for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
15610Sstevel@tonic-gate 				if (!SCTP_CHUNK_ISSENT(mp)) {
15620Sstevel@tonic-gate 					sctp->sctp_adv_pap = tsn;
15630Sstevel@tonic-gate 					return;
15640Sstevel@tonic-gate 				}
15650Sstevel@tonic-gate 				if (SCTP_CHUNK_WANT_REXMIT(mp))
15660Sstevel@tonic-gate 					break;
15670Sstevel@tonic-gate 			}
15680Sstevel@tonic-gate 			if (mp == NULL)
15690Sstevel@tonic-gate 				break;
15700Sstevel@tonic-gate 		}
15710Sstevel@tonic-gate 	}
15720Sstevel@tonic-gate 	sctp->sctp_adv_pap = tsn;
15730Sstevel@tonic-gate }
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate /*
15760Sstevel@tonic-gate  * Retransmit first segment which hasn't been acked with cumtsn or send
15770Sstevel@tonic-gate  * a Forward TSN chunk, if appropriate.
15780Sstevel@tonic-gate  */
15790Sstevel@tonic-gate void
15800Sstevel@tonic-gate sctp_rexmit(sctp_t *sctp, sctp_faddr_t *oldfp)
15810Sstevel@tonic-gate {
15820Sstevel@tonic-gate 	mblk_t		*mp;
15830Sstevel@tonic-gate 	mblk_t		*nmp = NULL;
15840Sstevel@tonic-gate 	mblk_t		*head;
15850Sstevel@tonic-gate 	mblk_t		*meta = sctp->sctp_xmit_head;
15860Sstevel@tonic-gate 	mblk_t		*fill;
15870Sstevel@tonic-gate 	uint32_t	seglen = 0;
15880Sstevel@tonic-gate 	uint32_t	sacklen;
15890Sstevel@tonic-gate 	uint16_t	chunklen;
15900Sstevel@tonic-gate 	int		extra;
15910Sstevel@tonic-gate 	sctp_data_hdr_t	*sdc;
15920Sstevel@tonic-gate 	sctp_faddr_t	*fp;
15930Sstevel@tonic-gate 	int		error;
15940Sstevel@tonic-gate 	uint32_t	adv_pap = sctp->sctp_adv_pap;
15950Sstevel@tonic-gate 	boolean_t	do_ftsn = B_FALSE;
15960Sstevel@tonic-gate 	boolean_t	ftsn_check = B_TRUE;
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	while (meta != NULL) {
15990Sstevel@tonic-gate 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
16000Sstevel@tonic-gate 			uint32_t	tsn;
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp))
16030Sstevel@tonic-gate 				goto window_probe;
16040Sstevel@tonic-gate 			/*
16050Sstevel@tonic-gate 			 * We break in the following cases -
16060Sstevel@tonic-gate 			 *
16070Sstevel@tonic-gate 			 *	if the advanced peer ack point includes the next
16080Sstevel@tonic-gate 			 *	chunk to be retransmited - possibly the Forward
16090Sstevel@tonic-gate 			 * 	TSN was lost.
16100Sstevel@tonic-gate 			 *
16110Sstevel@tonic-gate 			 *	if we are PRSCTP aware and the next chunk to be
16120Sstevel@tonic-gate 			 *	retransmitted is now abandoned
16130Sstevel@tonic-gate 			 *
16140Sstevel@tonic-gate 			 *	if the next chunk to be retransmitted is for
16150Sstevel@tonic-gate 			 *	the dest on which the timer went off. (this
16160Sstevel@tonic-gate 			 *	message is not abandoned).
16170Sstevel@tonic-gate 			 *
16180Sstevel@tonic-gate 			 * We check for Forward TSN only for the first
16190Sstevel@tonic-gate 			 * eligible chunk to be retransmitted. The reason
16200Sstevel@tonic-gate 			 * being if the first eligible chunk is skipped (say
16210Sstevel@tonic-gate 			 * it was sent to a destination other than oldfp)
16220Sstevel@tonic-gate 			 * then we cannot advance the cum TSN via Forward
16230Sstevel@tonic-gate 			 * TSN chunk.
16240Sstevel@tonic-gate 			 *
16250Sstevel@tonic-gate 			 * Also, ftsn_check is B_TRUE only for the first
16260Sstevel@tonic-gate 			 * eligible chunk, it  will be B_FALSE for all
16270Sstevel@tonic-gate 			 * subsequent candidate messages for retransmission.
16280Sstevel@tonic-gate 			 */
16290Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
16300Sstevel@tonic-gate 			tsn = ntohl(sdc->sdh_tsn);
16310Sstevel@tonic-gate 			if (SEQ_GT(tsn, sctp->sctp_lastack_rxd)) {
16320Sstevel@tonic-gate 				if (sctp->sctp_prsctp_aware && ftsn_check) {
16330Sstevel@tonic-gate 					if (SEQ_GEQ(sctp->sctp_adv_pap, tsn)) {
16340Sstevel@tonic-gate 						ASSERT(sctp->sctp_prsctp_aware);
16350Sstevel@tonic-gate 						do_ftsn = B_TRUE;
16360Sstevel@tonic-gate 						goto out;
16370Sstevel@tonic-gate 					} else {
16380Sstevel@tonic-gate 						sctp_check_adv_ack_pt(sctp,
16390Sstevel@tonic-gate 						    meta, mp);
16400Sstevel@tonic-gate 						if (SEQ_GT(sctp->sctp_adv_pap,
16410Sstevel@tonic-gate 						    adv_pap)) {
16420Sstevel@tonic-gate 							do_ftsn = B_TRUE;
16430Sstevel@tonic-gate 							goto out;
16440Sstevel@tonic-gate 						}
16450Sstevel@tonic-gate 					}
16460Sstevel@tonic-gate 					ftsn_check = B_FALSE;
16470Sstevel@tonic-gate 				}
16480Sstevel@tonic-gate 				if (SCTP_CHUNK_DEST(mp) == oldfp)
16490Sstevel@tonic-gate 					goto out;
16500Sstevel@tonic-gate 			}
16510Sstevel@tonic-gate 		}
16520Sstevel@tonic-gate 		meta = meta->b_next;
16530Sstevel@tonic-gate 		if (meta != NULL && sctp->sctp_prsctp_aware) {
16540Sstevel@tonic-gate 			sctp_msg_hdr_t	*mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 			while (meta != NULL && (SCTP_IS_MSG_ABANDONED(meta) ||
16570Sstevel@tonic-gate 			    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr, sctp))) {
16580Sstevel@tonic-gate 				meta = meta->b_next;
16590Sstevel@tonic-gate 			}
16600Sstevel@tonic-gate 		}
16610Sstevel@tonic-gate 	}
16620Sstevel@tonic-gate window_probe:
16630Sstevel@tonic-gate 	/*
16640Sstevel@tonic-gate 	 * Retransmit fired for a destination which didn't have
16650Sstevel@tonic-gate 	 * any unacked data pending.
16660Sstevel@tonic-gate 	 */
16670Sstevel@tonic-gate 	if (!sctp->sctp_unacked && sctp->sctp_unsent) {
16680Sstevel@tonic-gate 		/*
16690Sstevel@tonic-gate 		 * Send a window probe. Inflate frwnd to allow
16700Sstevel@tonic-gate 		 * sending one segment.
16710Sstevel@tonic-gate 		 */
16720Sstevel@tonic-gate 		if (sctp->sctp_frwnd < (oldfp->sfa_pmss - sizeof (*sdc))) {
16730Sstevel@tonic-gate 			sctp->sctp_frwnd = oldfp->sfa_pmss - sizeof (*sdc);
16740Sstevel@tonic-gate 		}
16750Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpOutWinProbe);
16760Sstevel@tonic-gate 		sctp_output(sctp);
16770Sstevel@tonic-gate 	}
16780Sstevel@tonic-gate 	return;
16790Sstevel@tonic-gate out:
16800Sstevel@tonic-gate 	/*
16810Sstevel@tonic-gate 	 * Enter slowstart for this destination
16820Sstevel@tonic-gate 	 */
16830Sstevel@tonic-gate 	oldfp->ssthresh = oldfp->cwnd / 2;
16840Sstevel@tonic-gate 	if (oldfp->ssthresh < 2 * oldfp->sfa_pmss)
16850Sstevel@tonic-gate 		oldfp->ssthresh = 2 * oldfp->sfa_pmss;
16860Sstevel@tonic-gate 	oldfp->cwnd = oldfp->sfa_pmss;
16870Sstevel@tonic-gate 	oldfp->pba = 0;
16880Sstevel@tonic-gate 	fp = sctp_rotate_faddr(sctp, oldfp);
16890Sstevel@tonic-gate 	ASSERT(fp != NULL);
16900Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	if (do_ftsn) {
16930Sstevel@tonic-gate 		sctp_make_ftsns(sctp, meta, mp, &nmp, fp, &seglen);
16940Sstevel@tonic-gate 		if (nmp == NULL) {
16950Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
16960Sstevel@tonic-gate 			goto restart_timer;
16970Sstevel@tonic-gate 		}
16980Sstevel@tonic-gate 		head = nmp;
16990Sstevel@tonic-gate 		mp = NULL;
17000Sstevel@tonic-gate 		meta = sctp->sctp_xmit_tail;
17010Sstevel@tonic-gate 		if (meta != NULL)
17020Sstevel@tonic-gate 			mp = meta->b_cont;
17030Sstevel@tonic-gate 		goto try_bundle;
17040Sstevel@tonic-gate 	}
17050Sstevel@tonic-gate 	seglen = ntohs(sdc->sdh_len);
17060Sstevel@tonic-gate 	chunklen = seglen - sizeof (*sdc);
17070Sstevel@tonic-gate 	if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
17080Sstevel@tonic-gate 		extra = SCTP_ALIGN - extra;
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 	/*
17110Sstevel@tonic-gate 	 * Cancel RTT measurement if the retransmitted TSN is before the
17120Sstevel@tonic-gate 	 * TSN used for timimg.
17130Sstevel@tonic-gate 	 */
17140Sstevel@tonic-gate 	if (sctp->sctp_out_time != 0 &&
17150Sstevel@tonic-gate 	    SEQ_GEQ(sctp->sctp_rtt_tsn, sdc->sdh_tsn)) {
17160Sstevel@tonic-gate 		sctp->sctp_out_time = 0;
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate 	/* Clear the counter as the RTT calculation may be off. */
17190Sstevel@tonic-gate 	fp->rtt_updates = 0;
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
17220Sstevel@tonic-gate 		sacklen = 0;
17230Sstevel@tonic-gate 	} else {
17240Sstevel@tonic-gate 		sacklen = sizeof (sctp_chunk_hdr_t) +
17250Sstevel@tonic-gate 		    sizeof (sctp_sack_chunk_t) +
17260Sstevel@tonic-gate 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
17270Sstevel@tonic-gate 		if (seglen + sacklen > sctp->sctp_lastdata->sfa_pmss) {
17280Sstevel@tonic-gate 			/* piggybacked SACK doesn't fit */
17290Sstevel@tonic-gate 			sacklen = 0;
17300Sstevel@tonic-gate 		} else {
17310Sstevel@tonic-gate 			fp = sctp->sctp_lastdata;
17320Sstevel@tonic-gate 		}
17330Sstevel@tonic-gate 	}
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	nmp = dupmsg(mp);
17360Sstevel@tonic-gate 	if (nmp == NULL)
17370Sstevel@tonic-gate 		goto restart_timer;
17380Sstevel@tonic-gate 	if (extra > 0) {
17390Sstevel@tonic-gate 		fill = sctp_get_padding(extra);
17400Sstevel@tonic-gate 		if (fill != NULL) {
17410Sstevel@tonic-gate 			linkb(nmp, fill);
17420Sstevel@tonic-gate 			seglen += extra;
17430Sstevel@tonic-gate 		} else {
17440Sstevel@tonic-gate 			freemsg(nmp);
17450Sstevel@tonic-gate 			goto restart_timer;
17460Sstevel@tonic-gate 		}
17470Sstevel@tonic-gate 	}
17480Sstevel@tonic-gate 	SCTP_CHUNK_CLEAR_FLAGS(nmp);
1749252Svi117747 	head = sctp_add_proto_hdr(sctp, fp, nmp, sacklen, NULL);
17500Sstevel@tonic-gate 	if (head == NULL) {
17510Sstevel@tonic-gate 		freemsg(nmp);
17520Sstevel@tonic-gate 		goto restart_timer;
17530Sstevel@tonic-gate 	}
17540Sstevel@tonic-gate 	seglen += sacklen;
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 	mp = mp->b_next;
17590Sstevel@tonic-gate try_bundle:
17600Sstevel@tonic-gate 	while (seglen < fp->sfa_pmss) {
17610Sstevel@tonic-gate 		int32_t new_len;
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 		while (mp != NULL) {
17640Sstevel@tonic-gate 			if (SCTP_CHUNK_CANSEND(mp))
17650Sstevel@tonic-gate 				break;
17660Sstevel@tonic-gate 			mp = mp->b_next;
17670Sstevel@tonic-gate 		}
17680Sstevel@tonic-gate 		if (mp == NULL) {
17690Sstevel@tonic-gate 			meta = sctp_get_msg_to_send(sctp, &mp, meta->b_next,
17700Sstevel@tonic-gate 			    &error, 0, 0, oldfp);
17710Sstevel@tonic-gate 			if (error != 0 || meta == NULL)
17720Sstevel@tonic-gate 				break;
17730Sstevel@tonic-gate 			ASSERT(mp != NULL);
17740Sstevel@tonic-gate 			sctp->sctp_xmit_tail = meta;
17750Sstevel@tonic-gate 		}
17760Sstevel@tonic-gate 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
17770Sstevel@tonic-gate 		chunklen = ntohs(sdc->sdh_len) - sizeof (*sdc);
17780Sstevel@tonic-gate 		new_len = seglen + ntohs(sdc->sdh_len);
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 		if (seglen & (SCTP_ALIGN - 1)) {
17810Sstevel@tonic-gate 			extra = SCTP_ALIGN - (seglen & (SCTP_ALIGN - 1));
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 			if (new_len + extra > fp->sfa_pmss) {
17840Sstevel@tonic-gate 				break;
17850Sstevel@tonic-gate 			}
17860Sstevel@tonic-gate 			fill = sctp_get_padding(extra);
17870Sstevel@tonic-gate 			if (fill != NULL) {
17880Sstevel@tonic-gate 				new_len += extra;
17890Sstevel@tonic-gate 				linkb(head, fill);
17900Sstevel@tonic-gate 			} else {
17910Sstevel@tonic-gate 				break;
17920Sstevel@tonic-gate 			}
17930Sstevel@tonic-gate 		} else {
17940Sstevel@tonic-gate 			if (new_len > fp->sfa_pmss) {
17950Sstevel@tonic-gate 				break;
17960Sstevel@tonic-gate 			}
17970Sstevel@tonic-gate 		}
17980Sstevel@tonic-gate 		if ((nmp = dupmsg(mp)) == NULL) {
17990Sstevel@tonic-gate 			break;
18000Sstevel@tonic-gate 		}
18010Sstevel@tonic-gate 		seglen = new_len;
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 		SCTP_CHUNK_CLEAR_FLAGS(nmp);
18040Sstevel@tonic-gate 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
18050Sstevel@tonic-gate 		linkb(head, nmp);
18060Sstevel@tonic-gate 		mp = mp->b_next;
18070Sstevel@tonic-gate 	}
18080Sstevel@tonic-gate 	if ((seglen > fp->sfa_pmss) && fp->isv4) {
18090Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)head->b_rptr;
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 		/*
18120Sstevel@tonic-gate 		 * Path MTU is different from path we thought it would
18130Sstevel@tonic-gate 		 * be when we created chunks, or IP headers have grown.
18140Sstevel@tonic-gate 		 * Need to clear the DF bit.
18150Sstevel@tonic-gate 		 */
18160Sstevel@tonic-gate 		iph->ipha_fragment_offset_and_flags = 0;
18170Sstevel@tonic-gate 	}
18180Sstevel@tonic-gate 	dprint(2, ("sctp_rexmit: Sending packet %d bytes, tsn %x "
18190Sstevel@tonic-gate 	    "ssn %d to %p (rwnd %d, lastack_rxd %x)\n",
1820*1676Sjpk 	    seglen, ntohl(sdc->sdh_tsn), ntohs(sdc->sdh_ssn),
1821*1676Sjpk 	    (void *)fp, sctp->sctp_frwnd, sctp->sctp_lastack_rxd));
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 	sctp_set_iplen(sctp, head);
18240Sstevel@tonic-gate 	sctp_add_sendq(sctp, head);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	/*
18270Sstevel@tonic-gate 	 * Restart timer with exponential backoff
18280Sstevel@tonic-gate 	 */
18290Sstevel@tonic-gate restart_timer:
18300Sstevel@tonic-gate 	oldfp->strikes++;
18310Sstevel@tonic-gate 	sctp->sctp_strikes++;
18320Sstevel@tonic-gate 	SCTP_CALC_RXT(oldfp, sctp->sctp_rto_max);
18330Sstevel@tonic-gate 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
18340Sstevel@tonic-gate 	if (oldfp->suna != 0)
18350Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, oldfp, oldfp->rto);
18360Sstevel@tonic-gate 	sctp->sctp_active = lbolt64;
18370Sstevel@tonic-gate }
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate /*
18400Sstevel@tonic-gate  * The SCTP write put procedure called from IP.
18410Sstevel@tonic-gate  */
18420Sstevel@tonic-gate void
18430Sstevel@tonic-gate sctp_wput(queue_t *q, mblk_t *mp)
18440Sstevel@tonic-gate {
18450Sstevel@tonic-gate 	uchar_t		*rptr;
18460Sstevel@tonic-gate 	t_scalar_t	type;
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 	switch (mp->b_datap->db_type) {
18490Sstevel@tonic-gate 	case M_IOCTL:
18500Sstevel@tonic-gate 		sctp_wput_ioctl(q, mp);
18510Sstevel@tonic-gate 		break;
18520Sstevel@tonic-gate 	case M_DATA:
18530Sstevel@tonic-gate 		/* Should be handled in sctp_output() */
18540Sstevel@tonic-gate 		ASSERT(0);
18550Sstevel@tonic-gate 		freemsg(mp);
18560Sstevel@tonic-gate 		break;
18570Sstevel@tonic-gate 	case M_PROTO:
18580Sstevel@tonic-gate 	case M_PCPROTO:
18590Sstevel@tonic-gate 		rptr = mp->b_rptr;
18600Sstevel@tonic-gate 		if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) {
18610Sstevel@tonic-gate 			type = ((union T_primitives *)rptr)->type;
18620Sstevel@tonic-gate 			/*
18630Sstevel@tonic-gate 			 * There is no "standard" way on how to respond
18640Sstevel@tonic-gate 			 * to T_CAPABILITY_REQ if a module does not
18650Sstevel@tonic-gate 			 * understand it.  And the current TI mod
18660Sstevel@tonic-gate 			 * has problems handling an error ack.  So we
18670Sstevel@tonic-gate 			 * catch the request here and reply with a response
18680Sstevel@tonic-gate 			 * which the TI mod knows how to respond to.
18690Sstevel@tonic-gate 			 */
18700Sstevel@tonic-gate 			switch (type) {
18710Sstevel@tonic-gate 			case T_CAPABILITY_REQ:
18720Sstevel@tonic-gate 				(void) putnextctl1(RD(q), M_ERROR, EPROTO);
18730Sstevel@tonic-gate 				break;
18740Sstevel@tonic-gate 			default:
18750Sstevel@tonic-gate 				if ((mp = mi_tpi_err_ack_alloc(mp,
18760Sstevel@tonic-gate 				    TNOTSUPPORT, 0)) != NULL) {
18770Sstevel@tonic-gate 					qreply(q, mp);
18780Sstevel@tonic-gate 					return;
18790Sstevel@tonic-gate 				}
18800Sstevel@tonic-gate 			}
18810Sstevel@tonic-gate 		}
18820Sstevel@tonic-gate 		/* FALLTHRU */
18830Sstevel@tonic-gate 	default:
18840Sstevel@tonic-gate 		freemsg(mp);
18850Sstevel@tonic-gate 		return;
18860Sstevel@tonic-gate 	}
18870Sstevel@tonic-gate }
1888