xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_output.c (revision 13054:feaeaa778d1c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211735Skcpoon 
220Sstevel@tonic-gate /*
2312474SGeorge.Shepherd@Sun.COM  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/systm.h>
280Sstevel@tonic-gate #include <sys/stream.h>
290Sstevel@tonic-gate #include <sys/cmn_err.h>
300Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
310Sstevel@tonic-gate #include <sys/tihdr.h>
320Sstevel@tonic-gate #include <sys/socket.h>
330Sstevel@tonic-gate #include <sys/stropts.h>
340Sstevel@tonic-gate #include <sys/strsun.h>
350Sstevel@tonic-gate #include <sys/strsubr.h>
360Sstevel@tonic-gate #include <sys/socketvar.h>
370Sstevel@tonic-gate #include <inet/common.h>
380Sstevel@tonic-gate #include <inet/mi.h>
390Sstevel@tonic-gate #include <inet/ip.h>
4011042SErik.Nordmark@Sun.COM #include <inet/ip_ire.h>
410Sstevel@tonic-gate #include <inet/ip6.h>
420Sstevel@tonic-gate #include <inet/sctp_ip.h>
430Sstevel@tonic-gate #include <inet/ipclassifier.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * PR-SCTP comments.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * A message can expire before it gets to the transmit list (i.e. it is still
490Sstevel@tonic-gate  * in the unsent list - unchunked), after it gets to the transmit list, but
500Sstevel@tonic-gate  * before transmission has actually started, or after transmission has begun.
510Sstevel@tonic-gate  * Accordingly, we check for the status of a message in sctp_chunkify() when
520Sstevel@tonic-gate  * the message is being transferred from the unsent list to the transmit list;
530Sstevel@tonic-gate  * in sctp_get_msg_to_send(), when we get the next chunk from the transmit
540Sstevel@tonic-gate  * list and in sctp_rexmit() when we get the next chunk to be (re)transmitted.
550Sstevel@tonic-gate  * When we nuke a message in sctp_chunkify(), all we need to do is take it
560Sstevel@tonic-gate  * out of the unsent list and update sctp_unsent; when a message is deemed
570Sstevel@tonic-gate  * timed-out in sctp_get_msg_to_send() we can just take it out of the transmit
580Sstevel@tonic-gate  * list, update sctp_unsent IFF transmission for the message has not yet begun
590Sstevel@tonic-gate  * (i.e. !SCTP_CHUNK_ISSENT(meta->b_cont)). However, if transmission for the
600Sstevel@tonic-gate  * message has started, then we cannot just take it out of the list, we need
610Sstevel@tonic-gate  * to send Forward TSN chunk to the peer so that the peer can clear its
620Sstevel@tonic-gate  * fragment list for this message. However, we cannot just send the Forward
630Sstevel@tonic-gate  * TSN in sctp_get_msg_to_send() because there might be unacked chunks for
640Sstevel@tonic-gate  * messages preceeding this abandoned message. So, we send a Forward TSN
650Sstevel@tonic-gate  * IFF all messages prior to this abandoned message has been SACKd, if not
660Sstevel@tonic-gate  * we defer sending the Forward TSN to sctp_cumack(), which will check for
670Sstevel@tonic-gate  * this condition and send the Forward TSN via sctp_check_abandoned_msg(). In
680Sstevel@tonic-gate  * sctp_rexmit() when we check for retransmissions, we need to determine if
690Sstevel@tonic-gate  * the advanced peer ack point can be moved ahead, and if so, send a Forward
700Sstevel@tonic-gate  * TSN to the peer instead of retransmitting the chunk. Note that when
710Sstevel@tonic-gate  * we send a Forward TSN for a message, there may be yet unsent chunks for
720Sstevel@tonic-gate  * this message; we need to mark all such chunks as abandoned, so that
730Sstevel@tonic-gate  * sctp_cumack() can take the message out of the transmit list, additionally
740Sstevel@tonic-gate  * sctp_unsent need to be adjusted. Whenever sctp_unsent is updated (i.e.
750Sstevel@tonic-gate  * decremented when a message/chunk is deemed abandoned), sockfs needs to
760Sstevel@tonic-gate  * be notified so that it can adjust its idea of the queued message.
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #include "sctp_impl.h"
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static struct kmem_cache	*sctp_kmem_ftsn_set_cache;
8210037SNick.Street@Sun.COM static mblk_t			*sctp_chunkify(sctp_t *, int, int, int);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #ifdef	DEBUG
850Sstevel@tonic-gate static boolean_t	sctp_verify_chain(mblk_t *, mblk_t *);
860Sstevel@tonic-gate #endif
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * Called to allocate a header mblk when sending data to SCTP.
900Sstevel@tonic-gate  * Data will follow in b_cont of this mblk.
910Sstevel@tonic-gate  */
920Sstevel@tonic-gate mblk_t *
sctp_alloc_hdr(const char * name,int nlen,const char * control,int clen,int flags)930Sstevel@tonic-gate sctp_alloc_hdr(const char *name, int nlen, const char *control, int clen,
940Sstevel@tonic-gate     int flags)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate 	mblk_t *mp;
970Sstevel@tonic-gate 	struct T_unitdata_req *tudr;
980Sstevel@tonic-gate 	size_t size;
990Sstevel@tonic-gate 	int error;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	size = sizeof (*tudr) + _TPI_ALIGN_TOPT(nlen) + clen;
1020Sstevel@tonic-gate 	size = MAX(size, sizeof (sctp_msg_hdr_t));
1030Sstevel@tonic-gate 	if (flags & SCTP_CAN_BLOCK) {
1040Sstevel@tonic-gate 		mp = allocb_wait(size, BPRI_MED, 0, &error);
1050Sstevel@tonic-gate 	} else {
1060Sstevel@tonic-gate 		mp = allocb(size, BPRI_MED);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 	if (mp) {
1090Sstevel@tonic-gate 		tudr = (struct T_unitdata_req *)mp->b_rptr;
1100Sstevel@tonic-gate 		tudr->PRIM_type = T_UNITDATA_REQ;
1110Sstevel@tonic-gate 		tudr->DEST_length = nlen;
1120Sstevel@tonic-gate 		tudr->DEST_offset = sizeof (*tudr);
1130Sstevel@tonic-gate 		tudr->OPT_length = clen;
1140Sstevel@tonic-gate 		tudr->OPT_offset = (t_scalar_t)(sizeof (*tudr) +
1150Sstevel@tonic-gate 		    _TPI_ALIGN_TOPT(nlen));
1160Sstevel@tonic-gate 		if (nlen > 0)
1170Sstevel@tonic-gate 			bcopy(name, tudr + 1, nlen);
1180Sstevel@tonic-gate 		if (clen > 0)
1190Sstevel@tonic-gate 			bcopy(control, (char *)tudr + tudr->OPT_offset, clen);
1200Sstevel@tonic-gate 		mp->b_wptr += (tudr ->OPT_offset + clen);
1210Sstevel@tonic-gate 		mp->b_datap->db_type = M_PROTO;
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 	return (mp);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /*ARGSUSED2*/
1270Sstevel@tonic-gate int
sctp_sendmsg(sctp_t * sctp,mblk_t * mp,int flags)1280Sstevel@tonic-gate sctp_sendmsg(sctp_t *sctp, mblk_t *mp, int flags)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	sctp_faddr_t	*fp = NULL;
1310Sstevel@tonic-gate 	struct T_unitdata_req	*tudr;
1320Sstevel@tonic-gate 	int		error = 0;
1330Sstevel@tonic-gate 	mblk_t		*mproto = mp;
1340Sstevel@tonic-gate 	in6_addr_t	*addr;
1350Sstevel@tonic-gate 	in6_addr_t	tmpaddr;
1360Sstevel@tonic-gate 	uint16_t	sid = sctp->sctp_def_stream;
1370Sstevel@tonic-gate 	uint32_t	ppid = sctp->sctp_def_ppid;
1380Sstevel@tonic-gate 	uint32_t	context = sctp->sctp_def_context;
1390Sstevel@tonic-gate 	uint16_t	msg_flags = sctp->sctp_def_flags;
1400Sstevel@tonic-gate 	sctp_msg_hdr_t	*sctp_msg_hdr;
1410Sstevel@tonic-gate 	uint32_t	msg_len = 0;
1420Sstevel@tonic-gate 	uint32_t	timetolive = sctp->sctp_def_timetolive;
14311042SErik.Nordmark@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	ASSERT(DB_TYPE(mproto) == M_PROTO);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	mp = mp->b_cont;
1480Sstevel@tonic-gate 	ASSERT(mp == NULL || DB_TYPE(mp) == M_DATA);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	tudr = (struct T_unitdata_req *)mproto->b_rptr;
1510Sstevel@tonic-gate 	ASSERT(tudr->PRIM_type == T_UNITDATA_REQ);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/* Get destination address, if specified */
1540Sstevel@tonic-gate 	if (tudr->DEST_length > 0) {
1550Sstevel@tonic-gate 		sin_t *sin;
1560Sstevel@tonic-gate 		sin6_t *sin6;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		sin = (struct sockaddr_in *)
1590Sstevel@tonic-gate 		    (mproto->b_rptr + tudr->DEST_offset);
1600Sstevel@tonic-gate 		switch (sin->sin_family) {
1610Sstevel@tonic-gate 		case AF_INET:
1620Sstevel@tonic-gate 			if (tudr->DEST_length < sizeof (*sin)) {
1630Sstevel@tonic-gate 				return (EINVAL);
1640Sstevel@tonic-gate 			}
1650Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &tmpaddr);
1660Sstevel@tonic-gate 			addr = &tmpaddr;
1670Sstevel@tonic-gate 			break;
1680Sstevel@tonic-gate 		case AF_INET6:
1690Sstevel@tonic-gate 			if (tudr->DEST_length < sizeof (*sin6)) {
1700Sstevel@tonic-gate 				return (EINVAL);
1710Sstevel@tonic-gate 			}
1720Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)
1730Sstevel@tonic-gate 			    (mproto->b_rptr + tudr->DEST_offset);
1740Sstevel@tonic-gate 			addr = &sin6->sin6_addr;
1750Sstevel@tonic-gate 			break;
1760Sstevel@tonic-gate 		default:
1770Sstevel@tonic-gate 			return (EAFNOSUPPORT);
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 		fp = sctp_lookup_faddr(sctp, addr);
1800Sstevel@tonic-gate 		if (fp == NULL) {
1810Sstevel@tonic-gate 			return (EINVAL);
1820Sstevel@tonic-gate 		}
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 	/* Ancillary Data? */
1850Sstevel@tonic-gate 	if (tudr->OPT_length > 0) {
1860Sstevel@tonic-gate 		struct cmsghdr		*cmsg;
1870Sstevel@tonic-gate 		char			*cend;
1880Sstevel@tonic-gate 		struct sctp_sndrcvinfo	*sndrcv;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)(mproto->b_rptr + tudr->OPT_offset);
1910Sstevel@tonic-gate 		cend = ((char *)cmsg + tudr->OPT_length);
1920Sstevel@tonic-gate 		ASSERT(cend <= (char *)mproto->b_wptr);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 		for (;;) {
1950Sstevel@tonic-gate 			if ((char *)(cmsg + 1) > cend ||
1960Sstevel@tonic-gate 			    ((char *)cmsg + cmsg->cmsg_len) > cend) {
1970Sstevel@tonic-gate 				break;
1980Sstevel@tonic-gate 			}
1990Sstevel@tonic-gate 			if ((cmsg->cmsg_level == IPPROTO_SCTP) &&
2000Sstevel@tonic-gate 			    (cmsg->cmsg_type == SCTP_SNDRCV)) {
2010Sstevel@tonic-gate 				if (cmsg->cmsg_len <
2020Sstevel@tonic-gate 				    (sizeof (*sndrcv) + sizeof (*cmsg))) {
2030Sstevel@tonic-gate 					return (EINVAL);
2040Sstevel@tonic-gate 				}
2050Sstevel@tonic-gate 				sndrcv = (struct sctp_sndrcvinfo *)(cmsg + 1);
2060Sstevel@tonic-gate 				sid = sndrcv->sinfo_stream;
2070Sstevel@tonic-gate 				msg_flags = sndrcv->sinfo_flags;
2080Sstevel@tonic-gate 				ppid = sndrcv->sinfo_ppid;
2090Sstevel@tonic-gate 				context = sndrcv->sinfo_context;
2100Sstevel@tonic-gate 				timetolive = sndrcv->sinfo_timetolive;
2110Sstevel@tonic-gate 				break;
2120Sstevel@tonic-gate 			}
2130Sstevel@tonic-gate 			if (cmsg->cmsg_len > 0)
2140Sstevel@tonic-gate 				cmsg = CMSG_NEXT(cmsg);
2150Sstevel@tonic-gate 			else
2160Sstevel@tonic-gate 				break;
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 	if (msg_flags & MSG_ABORT) {
2200Sstevel@tonic-gate 		if (mp && mp->b_cont) {
2210Sstevel@tonic-gate 			mblk_t *pump = msgpullup(mp, -1);
2220Sstevel@tonic-gate 			if (!pump) {
2230Sstevel@tonic-gate 				return (ENOMEM);
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 			freemsg(mp);
2260Sstevel@tonic-gate 			mp = pump;
2270Sstevel@tonic-gate 			mproto->b_cont = mp;
2280Sstevel@tonic-gate 		}
2290Sstevel@tonic-gate 		RUN_SCTP(sctp);
2307480SKacheong.Poon@Sun.COM 		sctp_user_abort(sctp, mp);
2310Sstevel@tonic-gate 		freemsg(mproto);
23211042SErik.Nordmark@Sun.COM 		goto done2;
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 	if (mp == NULL)
2350Sstevel@tonic-gate 		goto done;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	RUN_SCTP(sctp);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/* Reject any new data requests if we are shutting down */
2404505Skcpoon 	if (sctp->sctp_state > SCTPS_ESTABLISHED ||
2414505Skcpoon 	    (sctp->sctp_connp->conn_state_flags & CONN_CLOSING)) {
2420Sstevel@tonic-gate 		error = EPIPE;
2430Sstevel@tonic-gate 		goto unlock_done;
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/* Re-use the mproto to store relevant info. */
2470Sstevel@tonic-gate 	ASSERT(MBLKSIZE(mproto) >= sizeof (*sctp_msg_hdr));
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	mproto->b_rptr = mproto->b_datap->db_base;
2500Sstevel@tonic-gate 	mproto->b_wptr = mproto->b_rptr + sizeof (*sctp_msg_hdr);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	sctp_msg_hdr = (sctp_msg_hdr_t *)mproto->b_rptr;
2530Sstevel@tonic-gate 	bzero(sctp_msg_hdr, sizeof (*sctp_msg_hdr));
2540Sstevel@tonic-gate 	sctp_msg_hdr->smh_context = context;
2550Sstevel@tonic-gate 	sctp_msg_hdr->smh_sid = sid;
2560Sstevel@tonic-gate 	sctp_msg_hdr->smh_ppid = ppid;
2570Sstevel@tonic-gate 	sctp_msg_hdr->smh_flags = msg_flags;
2580Sstevel@tonic-gate 	sctp_msg_hdr->smh_ttl = MSEC_TO_TICK(timetolive);
25911066Srafael.vanoni@sun.com 	sctp_msg_hdr->smh_tob = ddi_get_lbolt64();
2600Sstevel@tonic-gate 	for (; mp != NULL; mp = mp->b_cont)
2610Sstevel@tonic-gate 		msg_len += MBLKL(mp);
2620Sstevel@tonic-gate 	sctp_msg_hdr->smh_msglen = msg_len;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/* User requested specific destination */
2650Sstevel@tonic-gate 	SCTP_SET_CHUNK_DEST(mproto, fp);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_COOKIE_ECHOED &&
2680Sstevel@tonic-gate 	    sid >= sctp->sctp_num_ostr) {
2690Sstevel@tonic-gate 		/* Send sendfail event */
2700Sstevel@tonic-gate 		sctp_sendfail_event(sctp, dupmsg(mproto), SCTP_ERR_BAD_SID,
2710Sstevel@tonic-gate 		    B_FALSE);
2720Sstevel@tonic-gate 		error = EINVAL;
2730Sstevel@tonic-gate 		goto unlock_done;
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/* no data */
2770Sstevel@tonic-gate 	if (msg_len == 0) {
2780Sstevel@tonic-gate 		sctp_sendfail_event(sctp, dupmsg(mproto),
2790Sstevel@tonic-gate 		    SCTP_ERR_NO_USR_DATA, B_FALSE);
2800Sstevel@tonic-gate 		error = EINVAL;
2810Sstevel@tonic-gate 		goto unlock_done;
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	/* Add it to the unsent list */
2850Sstevel@tonic-gate 	if (sctp->sctp_xmit_unsent == NULL) {
2860Sstevel@tonic-gate 		sctp->sctp_xmit_unsent = sctp->sctp_xmit_unsent_tail = mproto;
2870Sstevel@tonic-gate 	} else {
2880Sstevel@tonic-gate 		sctp->sctp_xmit_unsent_tail->b_next = mproto;
2890Sstevel@tonic-gate 		sctp->sctp_xmit_unsent_tail = mproto;
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 	sctp->sctp_unsent += msg_len;
2920Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_msgcount);
2938348SEric.Yu@Sun.COM 	/*
2948348SEric.Yu@Sun.COM 	 * Notify sockfs if the tx queue is full.
2958348SEric.Yu@Sun.COM 	 */
29611042SErik.Nordmark@Sun.COM 	if (SCTP_TXQ_LEN(sctp) >= connp->conn_sndbuf) {
2978348SEric.Yu@Sun.COM 		sctp->sctp_txq_full = 1;
298*13054SKacheong.Poon@Sun.COM 		sctp->sctp_ulp_txq_full(sctp->sctp_ulpd, B_TRUE);
2998348SEric.Yu@Sun.COM 	}
3000Sstevel@tonic-gate 	if (sctp->sctp_state == SCTPS_ESTABLISHED)
3013795Skcpoon 		sctp_output(sctp, UINT_MAX);
30211042SErik.Nordmark@Sun.COM done2:
3030Sstevel@tonic-gate 	WAKE_SCTP(sctp);
3040Sstevel@tonic-gate 	return (0);
3050Sstevel@tonic-gate unlock_done:
3060Sstevel@tonic-gate 	WAKE_SCTP(sctp);
3070Sstevel@tonic-gate done:
3080Sstevel@tonic-gate 	return (error);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
31110037SNick.Street@Sun.COM /*
31210037SNick.Street@Sun.COM  * While there are messages on sctp_xmit_unsent, detach each one. For each:
31310037SNick.Street@Sun.COM  * allocate space for the chunk header, fill in the data chunk, and fill in
31410037SNick.Street@Sun.COM  * the chunk header. Then append it to sctp_xmit_tail.
31510037SNick.Street@Sun.COM  * Return after appending as many bytes as required (bytes_to_send).
31610037SNick.Street@Sun.COM  * We also return if we've appended one or more chunks, and find a subsequent
31710037SNick.Street@Sun.COM  * unsent message is too big to fit in the segment.
31810037SNick.Street@Sun.COM  */
31910037SNick.Street@Sun.COM mblk_t *
sctp_chunkify(sctp_t * sctp,int mss,int firstseg_len,int bytes_to_send)32010037SNick.Street@Sun.COM sctp_chunkify(sctp_t *sctp, int mss, int firstseg_len, int bytes_to_send)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	mblk_t			*mp;
3230Sstevel@tonic-gate 	mblk_t			*chunk_mp;
3240Sstevel@tonic-gate 	mblk_t			*chunk_head;
3250Sstevel@tonic-gate 	mblk_t			*chunk_hdr;
3260Sstevel@tonic-gate 	mblk_t			*chunk_tail = NULL;
3270Sstevel@tonic-gate 	int			count;
3280Sstevel@tonic-gate 	int			chunksize;
3290Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
3300Sstevel@tonic-gate 	mblk_t			*mdblk = sctp->sctp_xmit_unsent;
3310Sstevel@tonic-gate 	sctp_faddr_t		*fp;
3320Sstevel@tonic-gate 	sctp_faddr_t		*fp1;
3330Sstevel@tonic-gate 	size_t			xtralen;
3340Sstevel@tonic-gate 	sctp_msg_hdr_t		*msg_hdr;
33510037SNick.Street@Sun.COM 	sctp_stack_t		*sctps = sctp->sctp_sctps;
33610037SNick.Street@Sun.COM 	sctp_msg_hdr_t		*next_msg_hdr;
33710037SNick.Street@Sun.COM 	size_t			nextlen;
33810037SNick.Street@Sun.COM 	int			remaining_len = mss - firstseg_len;
33910037SNick.Street@Sun.COM 
34010037SNick.Street@Sun.COM 	ASSERT(remaining_len >= 0);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	fp = SCTP_CHUNK_DEST(mdblk);
3430Sstevel@tonic-gate 	if (fp == NULL)
3440Sstevel@tonic-gate 		fp = sctp->sctp_current;
34513009SChandrasekar.Marimuthu@Sun.COM 	if (fp->sf_isv4)
3463448Sdh155122 		xtralen = sctp->sctp_hdr_len + sctps->sctps_wroff_xtra +
3473448Sdh155122 		    sizeof (*sdc);
3480Sstevel@tonic-gate 	else
3493448Sdh155122 		xtralen = sctp->sctp_hdr6_len + sctps->sctps_wroff_xtra +
3503448Sdh155122 		    sizeof (*sdc);
35110037SNick.Street@Sun.COM 	count = chunksize = remaining_len - sizeof (*sdc);
3520Sstevel@tonic-gate nextmsg:
35310037SNick.Street@Sun.COM 	next_msg_hdr = (sctp_msg_hdr_t *)sctp->sctp_xmit_unsent->b_rptr;
35410037SNick.Street@Sun.COM 	nextlen = next_msg_hdr->smh_msglen;
35510037SNick.Street@Sun.COM 	/*
35610037SNick.Street@Sun.COM 	 * Will the entire next message fit in the current packet ?
35710037SNick.Street@Sun.COM 	 * if not, leave it on the unsent list.
35810037SNick.Street@Sun.COM 	 */
35910037SNick.Street@Sun.COM 	if ((firstseg_len != 0) && (nextlen > remaining_len))
36010037SNick.Street@Sun.COM 		return (NULL);
36110037SNick.Street@Sun.COM 
3620Sstevel@tonic-gate 	chunk_mp = mdblk->b_cont;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/*
36510037SNick.Street@Sun.COM 	 * If this partially chunked, we ignore the next one for now and
36610037SNick.Street@Sun.COM 	 * use the one already present. For the unchunked bits, we use the
36710037SNick.Street@Sun.COM 	 * length of the last chunk.
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate 	if (SCTP_IS_MSG_CHUNKED(mdblk)) {
3700Sstevel@tonic-gate 		int	chunk_len;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 		ASSERT(chunk_mp->b_next != NULL);
3730Sstevel@tonic-gate 		mdblk->b_cont = chunk_mp->b_next;
3740Sstevel@tonic-gate 		chunk_mp->b_next = NULL;
3750Sstevel@tonic-gate 		SCTP_MSG_CLEAR_CHUNKED(mdblk);
3760Sstevel@tonic-gate 		mp = mdblk->b_cont;
3770Sstevel@tonic-gate 		while (mp->b_next != NULL)
3780Sstevel@tonic-gate 			mp = mp->b_next;
3790Sstevel@tonic-gate 		chunk_len = ntohs(((sctp_data_hdr_t *)mp->b_rptr)->sdh_len);
38013009SChandrasekar.Marimuthu@Sun.COM 		if (fp->sf_pmss - chunk_len > sizeof (*sdc))
38113009SChandrasekar.Marimuthu@Sun.COM 			count = chunksize = fp->sf_pmss - chunk_len;
3820Sstevel@tonic-gate 		else
38313009SChandrasekar.Marimuthu@Sun.COM 			count = chunksize = fp->sf_pmss;
3840Sstevel@tonic-gate 		count = chunksize = count - sizeof (*sdc);
3850Sstevel@tonic-gate 	} else {
3860Sstevel@tonic-gate 		msg_hdr = (sctp_msg_hdr_t *)mdblk->b_rptr;
3870Sstevel@tonic-gate 		if (SCTP_MSG_TO_BE_ABANDONED(mdblk, msg_hdr, sctp)) {
3880Sstevel@tonic-gate 			sctp->sctp_xmit_unsent = mdblk->b_next;
3890Sstevel@tonic-gate 			if (sctp->sctp_xmit_unsent == NULL)
3900Sstevel@tonic-gate 				sctp->sctp_xmit_unsent_tail = NULL;
3910Sstevel@tonic-gate 			ASSERT(sctp->sctp_unsent >= msg_hdr->smh_msglen);
3920Sstevel@tonic-gate 			sctp->sctp_unsent -= msg_hdr->smh_msglen;
3930Sstevel@tonic-gate 			mdblk->b_next = NULL;
3940Sstevel@tonic-gate 			BUMP_LOCAL(sctp->sctp_prsctpdrop);
3950Sstevel@tonic-gate 			/*
3960Sstevel@tonic-gate 			 * Update ULP the amount of queued data, which is
3970Sstevel@tonic-gate 			 * sent-unack'ed + unsent.
3980Sstevel@tonic-gate 			 */
3998348SEric.Yu@Sun.COM 			if (!SCTP_IS_DETACHED(sctp))
4008348SEric.Yu@Sun.COM 				SCTP_TXQ_UPDATE(sctp);
4010Sstevel@tonic-gate 			sctp_sendfail_event(sctp, mdblk, 0, B_FALSE);
4020Sstevel@tonic-gate 			goto try_next;
4030Sstevel@tonic-gate 		}
4040Sstevel@tonic-gate 		mdblk->b_cont = NULL;
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 	msg_hdr = (sctp_msg_hdr_t *)mdblk->b_rptr;
4070Sstevel@tonic-gate nextchunk:
4080Sstevel@tonic-gate 	chunk_head = chunk_mp;
4090Sstevel@tonic-gate 	chunk_tail = NULL;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	/* Skip as many mblk's as we need */
4120Sstevel@tonic-gate 	while (chunk_mp != NULL && ((count - MBLKL(chunk_mp)) >= 0)) {
4130Sstevel@tonic-gate 		count -= MBLKL(chunk_mp);
4140Sstevel@tonic-gate 		chunk_tail = chunk_mp;
4150Sstevel@tonic-gate 		chunk_mp = chunk_mp->b_cont;
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 	/* Split the chain, if needed */
4180Sstevel@tonic-gate 	if (chunk_mp != NULL) {
4190Sstevel@tonic-gate 		if (count > 0) {
4200Sstevel@tonic-gate 			mblk_t	*split_mp = dupb(chunk_mp);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 			if (split_mp == NULL) {
4230Sstevel@tonic-gate 				if (mdblk->b_cont == NULL) {
4240Sstevel@tonic-gate 					mdblk->b_cont = chunk_head;
4250Sstevel@tonic-gate 				} else  {
4260Sstevel@tonic-gate 					SCTP_MSG_SET_CHUNKED(mdblk);
4270Sstevel@tonic-gate 					ASSERT(chunk_head->b_next == NULL);
4280Sstevel@tonic-gate 					chunk_head->b_next = mdblk->b_cont;
4290Sstevel@tonic-gate 					mdblk->b_cont = chunk_head;
4300Sstevel@tonic-gate 				}
43110037SNick.Street@Sun.COM 				return (sctp->sctp_xmit_tail);
4320Sstevel@tonic-gate 			}
4330Sstevel@tonic-gate 			if (chunk_tail != NULL) {
4340Sstevel@tonic-gate 				chunk_tail->b_cont = split_mp;
4350Sstevel@tonic-gate 				chunk_tail = chunk_tail->b_cont;
4360Sstevel@tonic-gate 			} else {
4370Sstevel@tonic-gate 				chunk_head = chunk_tail = split_mp;
4380Sstevel@tonic-gate 			}
4390Sstevel@tonic-gate 			chunk_tail->b_wptr = chunk_tail->b_rptr + count;
4400Sstevel@tonic-gate 			chunk_mp->b_rptr = chunk_tail->b_wptr;
4410Sstevel@tonic-gate 			count = 0;
4420Sstevel@tonic-gate 		} else if (chunk_tail == NULL) {
4430Sstevel@tonic-gate 			goto next;
4440Sstevel@tonic-gate 		} else {
4450Sstevel@tonic-gate 			chunk_tail->b_cont = NULL;
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 	}
4480Sstevel@tonic-gate 	/* Alloc chunk hdr, if needed */
4490Sstevel@tonic-gate 	if (DB_REF(chunk_head) > 1 ||
4500Sstevel@tonic-gate 	    ((intptr_t)chunk_head->b_rptr) & (SCTP_ALIGN - 1) ||
4510Sstevel@tonic-gate 	    MBLKHEAD(chunk_head) < sizeof (*sdc)) {
4520Sstevel@tonic-gate 		if ((chunk_hdr = allocb(xtralen, BPRI_MED)) == NULL) {
4530Sstevel@tonic-gate 			if (mdblk->b_cont == NULL) {
4540Sstevel@tonic-gate 				if (chunk_mp != NULL)
4550Sstevel@tonic-gate 					linkb(chunk_head, chunk_mp);
4560Sstevel@tonic-gate 				mdblk->b_cont = chunk_head;
4570Sstevel@tonic-gate 			} else {
4580Sstevel@tonic-gate 				SCTP_MSG_SET_CHUNKED(mdblk);
4590Sstevel@tonic-gate 				if (chunk_mp != NULL)
4600Sstevel@tonic-gate 					linkb(chunk_head, chunk_mp);
4610Sstevel@tonic-gate 				ASSERT(chunk_head->b_next == NULL);
4620Sstevel@tonic-gate 				chunk_head->b_next = mdblk->b_cont;
4630Sstevel@tonic-gate 				mdblk->b_cont = chunk_head;
4640Sstevel@tonic-gate 			}
46510037SNick.Street@Sun.COM 			return (sctp->sctp_xmit_tail);
4660Sstevel@tonic-gate 		}
4670Sstevel@tonic-gate 		chunk_hdr->b_rptr += xtralen - sizeof (*sdc);
4680Sstevel@tonic-gate 		chunk_hdr->b_wptr = chunk_hdr->b_rptr + sizeof (*sdc);
4690Sstevel@tonic-gate 		chunk_hdr->b_cont = chunk_head;
4700Sstevel@tonic-gate 	} else {
4710Sstevel@tonic-gate 		chunk_hdr = chunk_head;
4720Sstevel@tonic-gate 		chunk_hdr->b_rptr -= sizeof (*sdc);
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 	ASSERT(chunk_hdr->b_datap->db_ref == 1);
4750Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)chunk_hdr->b_rptr;
4760Sstevel@tonic-gate 	sdc->sdh_id = CHUNK_DATA;
4770Sstevel@tonic-gate 	sdc->sdh_flags = 0;
4780Sstevel@tonic-gate 	sdc->sdh_len = htons(sizeof (*sdc) + chunksize - count);
4790Sstevel@tonic-gate 	ASSERT(sdc->sdh_len);
4800Sstevel@tonic-gate 	sdc->sdh_sid = htons(msg_hdr->smh_sid);
4810Sstevel@tonic-gate 	/*
4820Sstevel@tonic-gate 	 * We defer assigning the SSN just before sending the chunk, else
4830Sstevel@tonic-gate 	 * if we drop the chunk in sctp_get_msg_to_send(), we would need
4840Sstevel@tonic-gate 	 * to send a Forward TSN to let the peer know. Some more comments
4850Sstevel@tonic-gate 	 * about this in sctp_impl.h for SCTP_CHUNK_SENT.
4860Sstevel@tonic-gate 	 */
4870Sstevel@tonic-gate 	sdc->sdh_payload_id = msg_hdr->smh_ppid;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if (mdblk->b_cont == NULL) {
4900Sstevel@tonic-gate 		mdblk->b_cont = chunk_hdr;
4910Sstevel@tonic-gate 		SCTP_DATA_SET_BBIT(sdc);
4920Sstevel@tonic-gate 	} else {
4930Sstevel@tonic-gate 		mp = mdblk->b_cont;
4940Sstevel@tonic-gate 		while (mp->b_next != NULL)
4950Sstevel@tonic-gate 			mp = mp->b_next;
4960Sstevel@tonic-gate 		mp->b_next = chunk_hdr;
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	bytes_to_send -= (chunksize - count);
5000Sstevel@tonic-gate 	if (chunk_mp != NULL) {
5010Sstevel@tonic-gate next:
50213009SChandrasekar.Marimuthu@Sun.COM 		count = chunksize = fp->sf_pmss - sizeof (*sdc);
5030Sstevel@tonic-gate 		goto nextchunk;
5040Sstevel@tonic-gate 	}
5050Sstevel@tonic-gate 	SCTP_DATA_SET_EBIT(sdc);
5060Sstevel@tonic-gate 	sctp->sctp_xmit_unsent = mdblk->b_next;
5070Sstevel@tonic-gate 	if (mdblk->b_next == NULL) {
5080Sstevel@tonic-gate 		sctp->sctp_xmit_unsent_tail = NULL;
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 	mdblk->b_next = NULL;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	if (sctp->sctp_xmit_tail == NULL) {
5130Sstevel@tonic-gate 		sctp->sctp_xmit_head = sctp->sctp_xmit_tail = mdblk;
5140Sstevel@tonic-gate 	} else {
5150Sstevel@tonic-gate 		mp = sctp->sctp_xmit_tail;
5160Sstevel@tonic-gate 		while (mp->b_next != NULL)
5170Sstevel@tonic-gate 			mp = mp->b_next;
5180Sstevel@tonic-gate 		mp->b_next = mdblk;
5190Sstevel@tonic-gate 		mdblk->b_prev = mp;
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate try_next:
5220Sstevel@tonic-gate 	if (bytes_to_send > 0 && sctp->sctp_xmit_unsent != NULL) {
5230Sstevel@tonic-gate 		mdblk = sctp->sctp_xmit_unsent;
5240Sstevel@tonic-gate 		fp1 = SCTP_CHUNK_DEST(mdblk);
5250Sstevel@tonic-gate 		if (fp1 == NULL)
5260Sstevel@tonic-gate 			fp1 = sctp->sctp_current;
5270Sstevel@tonic-gate 		if (fp == fp1) {
5280Sstevel@tonic-gate 			size_t len = MBLKL(mdblk->b_cont);
5290Sstevel@tonic-gate 			if ((count > 0) &&
53013009SChandrasekar.Marimuthu@Sun.COM 			    ((len > fp->sf_pmss - sizeof (*sdc)) ||
5314505Skcpoon 			    (len <= count))) {
5320Sstevel@tonic-gate 				count -= sizeof (*sdc);
5330Sstevel@tonic-gate 				count = chunksize = count - (count & 0x3);
5340Sstevel@tonic-gate 			} else {
53513009SChandrasekar.Marimuthu@Sun.COM 				count = chunksize = fp->sf_pmss -
5360Sstevel@tonic-gate 				    sizeof (*sdc);
5370Sstevel@tonic-gate 			}
5380Sstevel@tonic-gate 		} else {
53913009SChandrasekar.Marimuthu@Sun.COM 			if (fp1->sf_isv4)
5400Sstevel@tonic-gate 				xtralen = sctp->sctp_hdr_len;
5410Sstevel@tonic-gate 			else
5420Sstevel@tonic-gate 				xtralen = sctp->sctp_hdr6_len;
5433448Sdh155122 			xtralen += sctps->sctps_wroff_xtra + sizeof (*sdc);
54413009SChandrasekar.Marimuthu@Sun.COM 			count = chunksize = fp1->sf_pmss - sizeof (*sdc);
5450Sstevel@tonic-gate 			fp = fp1;
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 		goto nextmsg;
5480Sstevel@tonic-gate 	}
54910037SNick.Street@Sun.COM 	return (sctp->sctp_xmit_tail);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate void
sctp_free_msg(mblk_t * ump)5530Sstevel@tonic-gate sctp_free_msg(mblk_t *ump)
5540Sstevel@tonic-gate {
5550Sstevel@tonic-gate 	mblk_t *mp, *nmp;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	for (mp = ump->b_cont; mp; mp = nmp) {
5580Sstevel@tonic-gate 		nmp = mp->b_next;
5590Sstevel@tonic-gate 		mp->b_next = mp->b_prev = NULL;
5600Sstevel@tonic-gate 		freemsg(mp);
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 	ASSERT(!ump->b_prev);
5630Sstevel@tonic-gate 	ump->b_next = NULL;
5640Sstevel@tonic-gate 	freeb(ump);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate mblk_t *
sctp_add_proto_hdr(sctp_t * sctp,sctp_faddr_t * fp,mblk_t * mp,int sacklen,int * error)568252Svi117747 sctp_add_proto_hdr(sctp_t *sctp, sctp_faddr_t *fp, mblk_t *mp, int sacklen,
569252Svi117747     int *error)
5700Sstevel@tonic-gate {
5710Sstevel@tonic-gate 	int hdrlen;
57211042SErik.Nordmark@Sun.COM 	uchar_t *hdr;
57313009SChandrasekar.Marimuthu@Sun.COM 	int isv4 = fp->sf_isv4;
5743448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
5750Sstevel@tonic-gate 
576252Svi117747 	if (error != NULL)
577252Svi117747 		*error = 0;
578252Svi117747 
5790Sstevel@tonic-gate 	if (isv4) {
5800Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr_len;
5810Sstevel@tonic-gate 		hdr = sctp->sctp_iphc;
5820Sstevel@tonic-gate 	} else {
5830Sstevel@tonic-gate 		hdrlen = sctp->sctp_hdr6_len;
5840Sstevel@tonic-gate 		hdr = sctp->sctp_iphc6;
5850Sstevel@tonic-gate 	}
586252Svi117747 	/*
58711042SErik.Nordmark@Sun.COM 	 * A reject|blackhole could mean that the address is 'down'. Similarly,
588252Svi117747 	 * it is possible that the address went down, we tried to send an
58913009SChandrasekar.Marimuthu@Sun.COM 	 * heartbeat and ended up setting fp->sf_saddr as unspec because we
5901735Skcpoon 	 * didn't have any usable source address.  In either case
59111042SErik.Nordmark@Sun.COM 	 * sctp_get_dest() will try find an IRE, if available, and set
5921735Skcpoon 	 * the source address, if needed.  If we still don't have any
59313009SChandrasekar.Marimuthu@Sun.COM 	 * usable source address, fp->sf_state will be SCTP_FADDRS_UNREACH and
594252Svi117747 	 * we return EHOSTUNREACH.
595252Svi117747 	 */
59613009SChandrasekar.Marimuthu@Sun.COM 	ASSERT(fp->sf_ixa->ixa_ire != NULL);
59713009SChandrasekar.Marimuthu@Sun.COM 	if ((fp->sf_ixa->ixa_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
59813009SChandrasekar.Marimuthu@Sun.COM 	    SCTP_IS_ADDR_UNSPEC(fp->sf_isv4, fp->sf_saddr)) {
59911042SErik.Nordmark@Sun.COM 		sctp_get_dest(sctp, fp);
60013009SChandrasekar.Marimuthu@Sun.COM 		if (fp->sf_state == SCTP_FADDRS_UNREACH) {
601252Svi117747 			if (error != NULL)
602252Svi117747 				*error = EHOSTUNREACH;
603252Svi117747 			return (NULL);
6040Sstevel@tonic-gate 		}
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 	/* Copy in IP header. */
6070Sstevel@tonic-gate 	if ((mp->b_rptr - mp->b_datap->db_base) <
60811042SErik.Nordmark@Sun.COM 	    (sctps->sctps_wroff_xtra + hdrlen + sacklen) || DB_REF(mp) > 2) {
6090Sstevel@tonic-gate 		mblk_t *nmp;
6101735Skcpoon 
6110Sstevel@tonic-gate 		/*
6120Sstevel@tonic-gate 		 * This can happen if IP headers are adjusted after
6130Sstevel@tonic-gate 		 * data was moved into chunks, or during retransmission,
6140Sstevel@tonic-gate 		 * or things like snoop is running.
6150Sstevel@tonic-gate 		 */
61611042SErik.Nordmark@Sun.COM 		nmp = allocb(sctps->sctps_wroff_xtra + hdrlen + sacklen,
61711042SErik.Nordmark@Sun.COM 		    BPRI_MED);
6180Sstevel@tonic-gate 		if (nmp == NULL) {
619252Svi117747 			if (error !=  NULL)
620252Svi117747 				*error = ENOMEM;
6210Sstevel@tonic-gate 			return (NULL);
6220Sstevel@tonic-gate 		}
6233448Sdh155122 		nmp->b_rptr += sctps->sctps_wroff_xtra;
6240Sstevel@tonic-gate 		nmp->b_wptr = nmp->b_rptr + hdrlen + sacklen;
6250Sstevel@tonic-gate 		nmp->b_cont = mp;
6260Sstevel@tonic-gate 		mp = nmp;
6270Sstevel@tonic-gate 	} else {
6280Sstevel@tonic-gate 		mp->b_rptr -= (hdrlen + sacklen);
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate 	bcopy(hdr, mp->b_rptr, hdrlen);
6310Sstevel@tonic-gate 	if (sacklen) {
6320Sstevel@tonic-gate 		sctp_fill_sack(sctp, mp->b_rptr + hdrlen, sacklen);
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate 	if (fp != sctp->sctp_current) {
6350Sstevel@tonic-gate 		/* change addresses in header */
6360Sstevel@tonic-gate 		if (isv4) {
6370Sstevel@tonic-gate 			ipha_t *iph = (ipha_t *)mp->b_rptr;
6380Sstevel@tonic-gate 
63913009SChandrasekar.Marimuthu@Sun.COM 			IN6_V4MAPPED_TO_IPADDR(&fp->sf_faddr, iph->ipha_dst);
64013009SChandrasekar.Marimuthu@Sun.COM 			if (!IN6_IS_ADDR_V4MAPPED_ANY(&fp->sf_saddr)) {
64113009SChandrasekar.Marimuthu@Sun.COM 				IN6_V4MAPPED_TO_IPADDR(&fp->sf_saddr,
6420Sstevel@tonic-gate 				    iph->ipha_src);
6430Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
6440Sstevel@tonic-gate 				iph->ipha_src = INADDR_ANY;
6450Sstevel@tonic-gate 			}
6460Sstevel@tonic-gate 		} else {
64711042SErik.Nordmark@Sun.COM 			ip6_t *ip6h = (ip6_t *)mp->b_rptr;
64811042SErik.Nordmark@Sun.COM 
64913009SChandrasekar.Marimuthu@Sun.COM 			ip6h->ip6_dst = fp->sf_faddr;
65013009SChandrasekar.Marimuthu@Sun.COM 			if (!IN6_IS_ADDR_UNSPECIFIED(&fp->sf_saddr)) {
65113009SChandrasekar.Marimuthu@Sun.COM 				ip6h->ip6_src = fp->sf_saddr;
6520Sstevel@tonic-gate 			} else if (sctp->sctp_bound_to_all) {
65311042SErik.Nordmark@Sun.COM 				ip6h->ip6_src = ipv6_all_zeros;
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 	return (mp);
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate /*
6610Sstevel@tonic-gate  * SCTP requires every chunk to be padded so that the total length
6620Sstevel@tonic-gate  * is a multiple of SCTP_ALIGN.  This function returns a mblk with
6630Sstevel@tonic-gate  * the specified pad length.
6640Sstevel@tonic-gate  */
6650Sstevel@tonic-gate static mblk_t *
sctp_get_padding(sctp_t * sctp,int pad)6664691Skcpoon sctp_get_padding(sctp_t *sctp, int pad)
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	mblk_t *fill;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	ASSERT(pad < SCTP_ALIGN);
6714691Skcpoon 	ASSERT(sctp->sctp_pad_mp != NULL);
6724691Skcpoon 	if ((fill = dupb(sctp->sctp_pad_mp)) != NULL) {
6730Sstevel@tonic-gate 		fill->b_wptr += pad;
6740Sstevel@tonic-gate 		return (fill);
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	/*
6780Sstevel@tonic-gate 	 * The memory saving path of reusing the sctp_pad_mp
6790Sstevel@tonic-gate 	 * fails may be because it has been dupb() too
6800Sstevel@tonic-gate 	 * many times (DBLK_REFMAX).  Use the memory consuming
6810Sstevel@tonic-gate 	 * path of allocating the pad mblk.
6820Sstevel@tonic-gate 	 */
6830Sstevel@tonic-gate 	if ((fill = allocb(SCTP_ALIGN, BPRI_MED)) != NULL) {
6840Sstevel@tonic-gate 		/* Zero it out.  SCTP_ALIGN is sizeof (int32_t) */
6850Sstevel@tonic-gate 		*(int32_t *)fill->b_rptr = 0;
6860Sstevel@tonic-gate 		fill->b_wptr += pad;
6870Sstevel@tonic-gate 	}
6880Sstevel@tonic-gate 	return (fill);
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate static mblk_t *
sctp_find_fast_rexmit_mblks(sctp_t * sctp,int * total,sctp_faddr_t ** fp)6920Sstevel@tonic-gate sctp_find_fast_rexmit_mblks(sctp_t *sctp, int *total, sctp_faddr_t **fp)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate 	mblk_t		*meta;
6950Sstevel@tonic-gate 	mblk_t		*start_mp = NULL;
6960Sstevel@tonic-gate 	mblk_t		*end_mp = NULL;
6970Sstevel@tonic-gate 	mblk_t		*mp, *nmp;
6980Sstevel@tonic-gate 	mblk_t		*fill;
6990Sstevel@tonic-gate 	sctp_data_hdr_t	*sdh;
7000Sstevel@tonic-gate 	int		msglen;
7010Sstevel@tonic-gate 	int		extra;
7020Sstevel@tonic-gate 	sctp_msg_hdr_t	*msg_hdr;
7031735Skcpoon 	sctp_faddr_t	*old_fp = NULL;
7041735Skcpoon 	sctp_faddr_t	*chunk_fp;
7053448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	for (meta = sctp->sctp_xmit_head; meta != NULL; meta = meta->b_next) {
7080Sstevel@tonic-gate 		msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
7090Sstevel@tonic-gate 		if (SCTP_IS_MSG_ABANDONED(meta) ||
7100Sstevel@tonic-gate 		    SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
7110Sstevel@tonic-gate 			continue;
7120Sstevel@tonic-gate 		}
7130Sstevel@tonic-gate 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
7140Sstevel@tonic-gate 			if (SCTP_CHUNK_WANT_REXMIT(mp)) {
7150Sstevel@tonic-gate 				/*
7160Sstevel@tonic-gate 				 * Use the same peer address to do fast
7171735Skcpoon 				 * retransmission.  If the original peer
7181735Skcpoon 				 * address is dead, switch to the current
7191735Skcpoon 				 * one.  Record the old one so that we
7201735Skcpoon 				 * will pick the chunks sent to the old
7211735Skcpoon 				 * one for fast retransmission.
7220Sstevel@tonic-gate 				 */
7231735Skcpoon 				chunk_fp = SCTP_CHUNK_DEST(mp);
7240Sstevel@tonic-gate 				if (*fp == NULL) {
7251735Skcpoon 					*fp = chunk_fp;
72613009SChandrasekar.Marimuthu@Sun.COM 					if ((*fp)->sf_state !=
72713009SChandrasekar.Marimuthu@Sun.COM 					    SCTP_FADDRS_ALIVE) {
7281735Skcpoon 						old_fp = *fp;
7290Sstevel@tonic-gate 						*fp = sctp->sctp_current;
7301735Skcpoon 					}
7311735Skcpoon 				} else if (old_fp == NULL && *fp != chunk_fp) {
7321735Skcpoon 					continue;
7331735Skcpoon 				} else if (old_fp != NULL &&
7341735Skcpoon 				    old_fp != chunk_fp) {
7350Sstevel@tonic-gate 					continue;
7360Sstevel@tonic-gate 				}
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 				sdh = (sctp_data_hdr_t *)mp->b_rptr;
7390Sstevel@tonic-gate 				msglen = ntohs(sdh->sdh_len);
7400Sstevel@tonic-gate 				if ((extra = msglen & (SCTP_ALIGN - 1)) != 0) {
7410Sstevel@tonic-gate 					extra = SCTP_ALIGN - extra;
7420Sstevel@tonic-gate 				}
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 				/*
7450Sstevel@tonic-gate 				 * We still return at least the first message
7460Sstevel@tonic-gate 				 * even if that message cannot fit in as
7470Sstevel@tonic-gate 				 * PMTU may have changed.
7480Sstevel@tonic-gate 				 */
7490Sstevel@tonic-gate 				if (*total + msglen + extra >
75013009SChandrasekar.Marimuthu@Sun.COM 				    (*fp)->sf_pmss && start_mp != NULL) {
7510Sstevel@tonic-gate 					return (start_mp);
7520Sstevel@tonic-gate 				}
7530Sstevel@tonic-gate 				if ((nmp = dupmsg(mp)) == NULL)
7540Sstevel@tonic-gate 					return (start_mp);
7550Sstevel@tonic-gate 				if (extra > 0) {
7564691Skcpoon 					fill = sctp_get_padding(sctp, extra);
7570Sstevel@tonic-gate 					if (fill != NULL) {
7580Sstevel@tonic-gate 						linkb(nmp, fill);
7590Sstevel@tonic-gate 					} else {
7600Sstevel@tonic-gate 						return (start_mp);
7610Sstevel@tonic-gate 					}
7620Sstevel@tonic-gate 				}
76312869SKacheong.Poon@Sun.COM 				SCTPS_BUMP_MIB(sctps, sctpOutFastRetrans);
7641735Skcpoon 				BUMP_LOCAL(sctp->sctp_rxtchunks);
7650Sstevel@tonic-gate 				SCTP_CHUNK_CLEAR_REXMIT(mp);
7660Sstevel@tonic-gate 				if (start_mp == NULL) {
7670Sstevel@tonic-gate 					start_mp = nmp;
7680Sstevel@tonic-gate 				} else {
7690Sstevel@tonic-gate 					linkb(end_mp, nmp);
7700Sstevel@tonic-gate 				}
7710Sstevel@tonic-gate 				end_mp = nmp;
7720Sstevel@tonic-gate 				*total += msglen + extra;
7730Sstevel@tonic-gate 				dprint(2, ("sctp_find_fast_rexmit_mblks: "
7740Sstevel@tonic-gate 				    "tsn %x\n", sdh->sdh_tsn));
7750Sstevel@tonic-gate 			}
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 	/* Clear the flag as there is no more message to be fast rexmitted. */
7790Sstevel@tonic-gate 	sctp->sctp_chk_fast_rexmit = B_FALSE;
7800Sstevel@tonic-gate 	return (start_mp);
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate /* A debug function just to make sure that a mblk chain is not broken */
7840Sstevel@tonic-gate #ifdef	DEBUG
7850Sstevel@tonic-gate static boolean_t
sctp_verify_chain(mblk_t * head,mblk_t * tail)7860Sstevel@tonic-gate sctp_verify_chain(mblk_t *head, mblk_t *tail)
7870Sstevel@tonic-gate {
7880Sstevel@tonic-gate 	mblk_t	*mp = head;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	if (head == NULL || tail == NULL)
7910Sstevel@tonic-gate 		return (B_TRUE);
7920Sstevel@tonic-gate 	while (mp != NULL) {
7930Sstevel@tonic-gate 		if (mp == tail)
7940Sstevel@tonic-gate 			return (B_TRUE);
7950Sstevel@tonic-gate 		mp = mp->b_next;
7960Sstevel@tonic-gate 	}
7970Sstevel@tonic-gate 	return (B_FALSE);
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate #endif
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate /*
8020Sstevel@tonic-gate  * Gets the next unsent chunk to transmit. Messages that are abandoned are
8030Sstevel@tonic-gate  * skipped. A message can be abandoned if it has a non-zero timetolive and
8040Sstevel@tonic-gate  * transmission has not yet started or if it is a partially reliable
8050Sstevel@tonic-gate  * message and its time is up (assuming we are PR-SCTP aware).
80610037SNick.Street@Sun.COM  * We only return a chunk if it will fit entirely in the current packet.
8070Sstevel@tonic-gate  * 'cansend' is used to determine if need to try and chunkify messages from
8080Sstevel@tonic-gate  * the unsent list, if any, and also as an input to sctp_chunkify() if so.
8098154SGeorge.Shepherd@Sun.COM  *
81010037SNick.Street@Sun.COM  * firstseg_len indicates the space already used, cansend represents remaining
81113009SChandrasekar.Marimuthu@Sun.COM  * space in the window, ((sf_pmss - firstseg_len) can therefore reasonably
8128154SGeorge.Shepherd@Sun.COM  * be used to compute the cansend arg).
8130Sstevel@tonic-gate  */
8140Sstevel@tonic-gate mblk_t *
sctp_get_msg_to_send(sctp_t * sctp,mblk_t ** mp,mblk_t * meta,int * error,int32_t firstseg_len,uint32_t cansend,sctp_faddr_t * fp)8150Sstevel@tonic-gate sctp_get_msg_to_send(sctp_t *sctp, mblk_t **mp, mblk_t *meta, int  *error,
81610037SNick.Street@Sun.COM     int32_t firstseg_len, uint32_t cansend, sctp_faddr_t *fp)
8170Sstevel@tonic-gate {
8180Sstevel@tonic-gate 	mblk_t		*mp1;
8190Sstevel@tonic-gate 	sctp_msg_hdr_t	*msg_hdr;
8200Sstevel@tonic-gate 	mblk_t		*tmp_meta;
8210Sstevel@tonic-gate 	sctp_faddr_t	*fp1;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	ASSERT(error != NULL && mp != NULL);
8240Sstevel@tonic-gate 	*error = 0;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	ASSERT(sctp->sctp_current != NULL);
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate chunkified:
8290Sstevel@tonic-gate 	while (meta != NULL) {
8300Sstevel@tonic-gate 		tmp_meta = meta->b_next;
8310Sstevel@tonic-gate 		msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
8320Sstevel@tonic-gate 		mp1 = meta->b_cont;
8330Sstevel@tonic-gate 		if (SCTP_IS_MSG_ABANDONED(meta))
8340Sstevel@tonic-gate 			goto next_msg;
8350Sstevel@tonic-gate 		if (!SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
8360Sstevel@tonic-gate 			while (mp1 != NULL) {
8370Sstevel@tonic-gate 				if (SCTP_CHUNK_CANSEND(mp1)) {
8380Sstevel@tonic-gate 					*mp = mp1;
8390Sstevel@tonic-gate #ifdef	DEBUG
8400Sstevel@tonic-gate 					ASSERT(sctp_verify_chain(
8410Sstevel@tonic-gate 					    sctp->sctp_xmit_head, meta));
8420Sstevel@tonic-gate #endif
8430Sstevel@tonic-gate 					return (meta);
8440Sstevel@tonic-gate 				}
8450Sstevel@tonic-gate 				mp1 = mp1->b_next;
8460Sstevel@tonic-gate 			}
8470Sstevel@tonic-gate 			goto next_msg;
8480Sstevel@tonic-gate 		}
8490Sstevel@tonic-gate 		/*
8500Sstevel@tonic-gate 		 * If we come here and the first chunk is sent, then we
8510Sstevel@tonic-gate 		 * we are PR-SCTP aware, in which case if the cumulative
8520Sstevel@tonic-gate 		 * TSN has moved upto or beyond the first chunk (which
8530Sstevel@tonic-gate 		 * means all the previous messages have been cumulative
8540Sstevel@tonic-gate 		 * SACK'd), then we send a Forward TSN with the last
8550Sstevel@tonic-gate 		 * chunk that was sent in this message. If we can't send
8560Sstevel@tonic-gate 		 * a Forward TSN because previous non-abandoned messages
8570Sstevel@tonic-gate 		 * have not been acked then we will defer the Forward TSN
8580Sstevel@tonic-gate 		 * to sctp_rexmit() or sctp_cumack().
8590Sstevel@tonic-gate 		 */
8600Sstevel@tonic-gate 		if (SCTP_CHUNK_ISSENT(mp1)) {
8610Sstevel@tonic-gate 			*error = sctp_check_abandoned_msg(sctp, meta);
8620Sstevel@tonic-gate 			if (*error != 0) {
8630Sstevel@tonic-gate #ifdef	DEBUG
8640Sstevel@tonic-gate 				ASSERT(sctp_verify_chain(sctp->sctp_xmit_head,
8650Sstevel@tonic-gate 				    sctp->sctp_xmit_tail));
8660Sstevel@tonic-gate #endif
8670Sstevel@tonic-gate 				return (NULL);
8680Sstevel@tonic-gate 			}
8690Sstevel@tonic-gate 			goto next_msg;
8700Sstevel@tonic-gate 		}
8710Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_prsctpdrop);
8720Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent >= msg_hdr->smh_msglen);
8730Sstevel@tonic-gate 		if (meta->b_prev == NULL) {
8740Sstevel@tonic-gate 			ASSERT(sctp->sctp_xmit_head == meta);
8750Sstevel@tonic-gate 			sctp->sctp_xmit_head = tmp_meta;
8760Sstevel@tonic-gate 			if (sctp->sctp_xmit_tail == meta)
8770Sstevel@tonic-gate 				sctp->sctp_xmit_tail = tmp_meta;
8780Sstevel@tonic-gate 			meta->b_next = NULL;
8790Sstevel@tonic-gate 			if (tmp_meta != NULL)
8800Sstevel@tonic-gate 				tmp_meta->b_prev = NULL;
8810Sstevel@tonic-gate 		} else if (meta->b_next == NULL) {
8820Sstevel@tonic-gate 			if (sctp->sctp_xmit_tail == meta)
8830Sstevel@tonic-gate 				sctp->sctp_xmit_tail = meta->b_prev;
8840Sstevel@tonic-gate 			meta->b_prev->b_next = NULL;
8850Sstevel@tonic-gate 			meta->b_prev = NULL;
8860Sstevel@tonic-gate 		} else {
8870Sstevel@tonic-gate 			meta->b_prev->b_next = tmp_meta;
8880Sstevel@tonic-gate 			tmp_meta->b_prev = meta->b_prev;
8890Sstevel@tonic-gate 			if (sctp->sctp_xmit_tail == meta)
8900Sstevel@tonic-gate 				sctp->sctp_xmit_tail = tmp_meta;
8910Sstevel@tonic-gate 			meta->b_prev = NULL;
8920Sstevel@tonic-gate 			meta->b_next = NULL;
8930Sstevel@tonic-gate 		}
8940Sstevel@tonic-gate 		sctp->sctp_unsent -= msg_hdr->smh_msglen;
8950Sstevel@tonic-gate 		/*
8960Sstevel@tonic-gate 		 * Update ULP the amount of queued data, which is
8970Sstevel@tonic-gate 		 * sent-unack'ed + unsent.
8980Sstevel@tonic-gate 		 */
8998348SEric.Yu@Sun.COM 		if (!SCTP_IS_DETACHED(sctp))
9008348SEric.Yu@Sun.COM 			SCTP_TXQ_UPDATE(sctp);
9010Sstevel@tonic-gate 		sctp_sendfail_event(sctp, meta, 0, B_TRUE);
9020Sstevel@tonic-gate next_msg:
9030Sstevel@tonic-gate 		meta = tmp_meta;
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate 	/* chunkify, if needed */
9060Sstevel@tonic-gate 	if (cansend > 0 && sctp->sctp_xmit_unsent != NULL) {
9070Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent > 0);
9080Sstevel@tonic-gate 		if (fp == NULL) {
9090Sstevel@tonic-gate 			fp = SCTP_CHUNK_DEST(sctp->sctp_xmit_unsent);
91013009SChandrasekar.Marimuthu@Sun.COM 			if (fp == NULL || fp->sf_state != SCTP_FADDRS_ALIVE)
9110Sstevel@tonic-gate 				fp = sctp->sctp_current;
9120Sstevel@tonic-gate 		} else {
9130Sstevel@tonic-gate 			/*
9140Sstevel@tonic-gate 			 * If user specified destination, try to honor that.
9150Sstevel@tonic-gate 			 */
9160Sstevel@tonic-gate 			fp1 = SCTP_CHUNK_DEST(sctp->sctp_xmit_unsent);
91713009SChandrasekar.Marimuthu@Sun.COM 			if (fp1 != NULL && fp1->sf_state == SCTP_FADDRS_ALIVE &&
9180Sstevel@tonic-gate 			    fp1 != fp) {
9190Sstevel@tonic-gate 				goto chunk_done;
9200Sstevel@tonic-gate 			}
9210Sstevel@tonic-gate 		}
92213009SChandrasekar.Marimuthu@Sun.COM 		meta = sctp_chunkify(sctp, fp->sf_pmss, firstseg_len, cansend);
92310037SNick.Street@Sun.COM 		if (meta == NULL)
9240Sstevel@tonic-gate 			goto chunk_done;
9250Sstevel@tonic-gate 		/*
9260Sstevel@tonic-gate 		 * sctp_chunkify() won't advance sctp_xmit_tail if it adds
9270Sstevel@tonic-gate 		 * new chunk(s) to the tail, so we need to skip the
9280Sstevel@tonic-gate 		 * sctp_xmit_tail, which would have already been processed.
9290Sstevel@tonic-gate 		 * This could happen when there is unacked chunks, but
9300Sstevel@tonic-gate 		 * nothing new to send.
9310Sstevel@tonic-gate 		 * When sctp_chunkify() is called when the transmit queue
9320Sstevel@tonic-gate 		 * is empty then we need to start from sctp_xmit_tail.
9330Sstevel@tonic-gate 		 */
9340Sstevel@tonic-gate 		if (SCTP_CHUNK_ISSENT(sctp->sctp_xmit_tail->b_cont)) {
9350Sstevel@tonic-gate #ifdef	DEBUG
9360Sstevel@tonic-gate 			mp1 = sctp->sctp_xmit_tail->b_cont;
9370Sstevel@tonic-gate 			while (mp1 != NULL) {
9380Sstevel@tonic-gate 				ASSERT(!SCTP_CHUNK_CANSEND(mp1));
9390Sstevel@tonic-gate 				mp1 = mp1->b_next;
9400Sstevel@tonic-gate 			}
9410Sstevel@tonic-gate #endif
9420Sstevel@tonic-gate 			if ((meta = sctp->sctp_xmit_tail->b_next) == NULL)
9430Sstevel@tonic-gate 				goto chunk_done;
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate 		goto chunkified;
9460Sstevel@tonic-gate 	}
9470Sstevel@tonic-gate chunk_done:
9480Sstevel@tonic-gate #ifdef	DEBUG
9490Sstevel@tonic-gate 	ASSERT(sctp_verify_chain(sctp->sctp_xmit_head, sctp->sctp_xmit_tail));
9500Sstevel@tonic-gate #endif
9510Sstevel@tonic-gate 	return (NULL);
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate void
sctp_fast_rexmit(sctp_t * sctp)9550Sstevel@tonic-gate sctp_fast_rexmit(sctp_t *sctp)
9560Sstevel@tonic-gate {
9570Sstevel@tonic-gate 	mblk_t		*mp, *head;
9580Sstevel@tonic-gate 	int		pktlen = 0;
9590Sstevel@tonic-gate 	sctp_faddr_t	*fp = NULL;
9603448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head != NULL);
9630Sstevel@tonic-gate 	mp = sctp_find_fast_rexmit_mblks(sctp, &pktlen, &fp);
9641735Skcpoon 	if (mp == NULL) {
9653448Sdh155122 		SCTP_KSTAT(sctps, sctp_fr_not_found);
9660Sstevel@tonic-gate 		return;
9671735Skcpoon 	}
968252Svi117747 	if ((head = sctp_add_proto_hdr(sctp, fp, mp, 0, NULL)) == NULL) {
9690Sstevel@tonic-gate 		freemsg(mp);
9703448Sdh155122 		SCTP_KSTAT(sctps, sctp_fr_add_hdr);
9710Sstevel@tonic-gate 		return;
9720Sstevel@tonic-gate 	}
97313009SChandrasekar.Marimuthu@Sun.COM 	if ((pktlen > fp->sf_pmss) && fp->sf_isv4) {
9740Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)head->b_rptr;
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 		iph->ipha_fragment_offset_and_flags = 0;
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 
97913009SChandrasekar.Marimuthu@Sun.COM 	sctp_set_iplen(sctp, head, fp->sf_ixa);
98013009SChandrasekar.Marimuthu@Sun.COM 	(void) conn_ip_output(head, fp->sf_ixa);
98111042SErik.Nordmark@Sun.COM 	BUMP_LOCAL(sctp->sctp_opkts);
98213009SChandrasekar.Marimuthu@Sun.COM 	sctp->sctp_active = fp->sf_lastactive = ddi_get_lbolt64();
9830Sstevel@tonic-gate }
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate void
sctp_output(sctp_t * sctp,uint_t num_pkt)9863795Skcpoon sctp_output(sctp_t *sctp, uint_t num_pkt)
9870Sstevel@tonic-gate {
9880Sstevel@tonic-gate 	mblk_t			*mp = NULL;
9890Sstevel@tonic-gate 	mblk_t			*nmp;
9900Sstevel@tonic-gate 	mblk_t			*head;
9910Sstevel@tonic-gate 	mblk_t			*meta = sctp->sctp_xmit_tail;
9920Sstevel@tonic-gate 	mblk_t			*fill = NULL;
9930Sstevel@tonic-gate 	uint16_t 		chunklen;
9940Sstevel@tonic-gate 	uint32_t 		cansend;
9950Sstevel@tonic-gate 	int32_t			seglen;
9960Sstevel@tonic-gate 	int32_t			xtralen;
9970Sstevel@tonic-gate 	int32_t			sacklen;
9980Sstevel@tonic-gate 	int32_t			pad = 0;
9990Sstevel@tonic-gate 	int32_t			pathmax;
10000Sstevel@tonic-gate 	int			extra;
100112869SKacheong.Poon@Sun.COM 	int64_t			now = LBOLT_FASTPATH64;
10020Sstevel@tonic-gate 	sctp_faddr_t		*fp;
10030Sstevel@tonic-gate 	sctp_faddr_t		*lfp;
10040Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
10050Sstevel@tonic-gate 	int			error;
1006252Svi117747 	boolean_t		notsent = B_TRUE;
10073795Skcpoon 	sctp_stack_t		*sctps = sctp->sctp_sctps;
100812534SGeorge.Shepherd@Sun.COM 	uint32_t		tsn;
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
10110Sstevel@tonic-gate 		sacklen = 0;
10120Sstevel@tonic-gate 	} else {
10130Sstevel@tonic-gate 		/* send a SACK chunk */
10140Sstevel@tonic-gate 		sacklen = sizeof (sctp_chunk_hdr_t) +
10150Sstevel@tonic-gate 		    sizeof (sctp_sack_chunk_t) +
10160Sstevel@tonic-gate 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
10170Sstevel@tonic-gate 		lfp = sctp->sctp_lastdata;
10180Sstevel@tonic-gate 		ASSERT(lfp != NULL);
101913009SChandrasekar.Marimuthu@Sun.COM 		if (lfp->sf_state != SCTP_FADDRS_ALIVE)
10200Sstevel@tonic-gate 			lfp = sctp->sctp_current;
10210Sstevel@tonic-gate 	}
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	cansend = sctp->sctp_frwnd;
10240Sstevel@tonic-gate 	if (sctp->sctp_unsent < cansend)
10250Sstevel@tonic-gate 		cansend = sctp->sctp_unsent;
10268222SGeorge.Shepherd@Sun.COM 
10278222SGeorge.Shepherd@Sun.COM 	/*
10288222SGeorge.Shepherd@Sun.COM 	 * Start persist timer if unable to send or when
10298222SGeorge.Shepherd@Sun.COM 	 * trying to send into a zero window. This timer
10308222SGeorge.Shepherd@Sun.COM 	 * ensures the blocked send attempt is retried.
10318222SGeorge.Shepherd@Sun.COM 	 */
103213009SChandrasekar.Marimuthu@Sun.COM 	if ((cansend < sctp->sctp_current->sf_pmss / 2) &&
10338222SGeorge.Shepherd@Sun.COM 	    (sctp->sctp_unacked != 0) &&
103413009SChandrasekar.Marimuthu@Sun.COM 	    (sctp->sctp_unacked < sctp->sctp_current->sf_pmss) &&
10358222SGeorge.Shepherd@Sun.COM 	    !sctp->sctp_ndelay ||
10368222SGeorge.Shepherd@Sun.COM 	    (cansend == 0 && sctp->sctp_unacked == 0 &&
10378222SGeorge.Shepherd@Sun.COM 	    sctp->sctp_unsent != 0)) {
10380Sstevel@tonic-gate 		head = NULL;
10390Sstevel@tonic-gate 		fp = sctp->sctp_current;
10400Sstevel@tonic-gate 		goto unsent_data;
10410Sstevel@tonic-gate 	}
10420Sstevel@tonic-gate 	if (meta != NULL)
10430Sstevel@tonic-gate 		mp = meta->b_cont;
10443795Skcpoon 	while (cansend > 0 && num_pkt-- != 0) {
10450Sstevel@tonic-gate 		pad = 0;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 		/*
10480Sstevel@tonic-gate 		 * Find first segment eligible for transmit.
10490Sstevel@tonic-gate 		 */
10500Sstevel@tonic-gate 		while (mp != NULL) {
10510Sstevel@tonic-gate 			if (SCTP_CHUNK_CANSEND(mp))
10520Sstevel@tonic-gate 				break;
10530Sstevel@tonic-gate 			mp = mp->b_next;
10540Sstevel@tonic-gate 		}
10550Sstevel@tonic-gate 		if (mp == NULL) {
10560Sstevel@tonic-gate 			meta = sctp_get_msg_to_send(sctp, &mp,
10570Sstevel@tonic-gate 			    meta == NULL ? NULL : meta->b_next, &error, sacklen,
10580Sstevel@tonic-gate 			    cansend, NULL);
10590Sstevel@tonic-gate 			if (error != 0 || meta == NULL) {
10600Sstevel@tonic-gate 				head = NULL;
10610Sstevel@tonic-gate 				fp = sctp->sctp_current;
10620Sstevel@tonic-gate 				goto unsent_data;
10630Sstevel@tonic-gate 			}
10640Sstevel@tonic-gate 			sctp->sctp_xmit_tail =  meta;
10650Sstevel@tonic-gate 		}
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
10680Sstevel@tonic-gate 		seglen = ntohs(sdc->sdh_len);
10690Sstevel@tonic-gate 		xtralen = sizeof (*sdc);
10700Sstevel@tonic-gate 		chunklen = seglen - xtralen;
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 		/*
10730Sstevel@tonic-gate 		 * Check rwnd.
10740Sstevel@tonic-gate 		 */
10750Sstevel@tonic-gate 		if (chunklen > cansend) {
10760Sstevel@tonic-gate 			head = NULL;
10770Sstevel@tonic-gate 			fp = SCTP_CHUNK_DEST(meta);
107813009SChandrasekar.Marimuthu@Sun.COM 			if (fp == NULL || fp->sf_state != SCTP_FADDRS_ALIVE)
10790Sstevel@tonic-gate 				fp = sctp->sctp_current;
10800Sstevel@tonic-gate 			goto unsent_data;
10810Sstevel@tonic-gate 		}
10820Sstevel@tonic-gate 		if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
10830Sstevel@tonic-gate 			extra = SCTP_ALIGN - extra;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 		/*
10860Sstevel@tonic-gate 		 * Pick destination address, and check cwnd.
10870Sstevel@tonic-gate 		 */
108813009SChandrasekar.Marimuthu@Sun.COM 		if (sacklen > 0 && (seglen + extra <= lfp->sf_cwnd -
108913009SChandrasekar.Marimuthu@Sun.COM 		    lfp->sf_suna) &&
109013009SChandrasekar.Marimuthu@Sun.COM 		    (seglen + sacklen + extra <= lfp->sf_pmss)) {
10910Sstevel@tonic-gate 			/*
10920Sstevel@tonic-gate 			 * Only include SACK chunk if it can be bundled
10930Sstevel@tonic-gate 			 * with a data chunk, and sent to sctp_lastdata.
10940Sstevel@tonic-gate 			 */
109513009SChandrasekar.Marimuthu@Sun.COM 			pathmax = lfp->sf_cwnd - lfp->sf_suna;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 			fp = lfp;
10980Sstevel@tonic-gate 			if ((nmp = dupmsg(mp)) == NULL) {
10990Sstevel@tonic-gate 				head = NULL;
11000Sstevel@tonic-gate 				goto unsent_data;
11010Sstevel@tonic-gate 			}
11020Sstevel@tonic-gate 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1103252Svi117747 			head = sctp_add_proto_hdr(sctp, fp, nmp, sacklen,
1104252Svi117747 			    &error);
11050Sstevel@tonic-gate 			if (head == NULL) {
1106252Svi117747 				/*
1107252Svi117747 				 * If none of the source addresses are
1108252Svi117747 				 * available (i.e error == EHOSTUNREACH),
1109252Svi117747 				 * pretend we have sent the data. We will
1110252Svi117747 				 * eventually time out trying to retramsmit
1111252Svi117747 				 * the data if the interface never comes up.
1112252Svi117747 				 * If we have already sent some stuff (i.e.,
1113252Svi117747 				 * notsent is B_FALSE) then we are fine, else
1114252Svi117747 				 * just mark this packet as sent.
1115252Svi117747 				 */
1116252Svi117747 				if (notsent && error == EHOSTUNREACH) {
1117252Svi117747 					SCTP_CHUNK_SENT(sctp, mp, sdc,
1118252Svi117747 					    fp, chunklen, meta);
1119252Svi117747 				}
11200Sstevel@tonic-gate 				freemsg(nmp);
11213448Sdh155122 				SCTP_KSTAT(sctps, sctp_output_failed);
11220Sstevel@tonic-gate 				goto unsent_data;
11230Sstevel@tonic-gate 			}
11240Sstevel@tonic-gate 			seglen += sacklen;
11250Sstevel@tonic-gate 			xtralen += sacklen;
11260Sstevel@tonic-gate 			sacklen = 0;
11270Sstevel@tonic-gate 		} else {
11280Sstevel@tonic-gate 			fp = SCTP_CHUNK_DEST(meta);
112913009SChandrasekar.Marimuthu@Sun.COM 			if (fp == NULL || fp->sf_state != SCTP_FADDRS_ALIVE)
11300Sstevel@tonic-gate 				fp = sctp->sctp_current;
11310Sstevel@tonic-gate 			/*
11320Sstevel@tonic-gate 			 * If we haven't sent data to this destination for
11330Sstevel@tonic-gate 			 * a while, do slow start again.
11340Sstevel@tonic-gate 			 */
113513009SChandrasekar.Marimuthu@Sun.COM 			if (now - fp->sf_lastactive > fp->sf_rto) {
113613009SChandrasekar.Marimuthu@Sun.COM 				SET_CWND(fp, fp->sf_pmss,
11373795Skcpoon 				    sctps->sctps_slow_start_after_idle);
11380Sstevel@tonic-gate 			}
11390Sstevel@tonic-gate 
114013009SChandrasekar.Marimuthu@Sun.COM 			pathmax = fp->sf_cwnd - fp->sf_suna;
11410Sstevel@tonic-gate 			if (seglen + extra > pathmax) {
11420Sstevel@tonic-gate 				head = NULL;
11430Sstevel@tonic-gate 				goto unsent_data;
11440Sstevel@tonic-gate 			}
11450Sstevel@tonic-gate 			if ((nmp = dupmsg(mp)) == NULL) {
11460Sstevel@tonic-gate 				head = NULL;
11470Sstevel@tonic-gate 				goto unsent_data;
11480Sstevel@tonic-gate 			}
11490Sstevel@tonic-gate 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
1150252Svi117747 			head = sctp_add_proto_hdr(sctp, fp, nmp, 0, &error);
11510Sstevel@tonic-gate 			if (head == NULL) {
1152252Svi117747 				/*
1153252Svi117747 				 * If none of the source addresses are
1154252Svi117747 				 * available (i.e error == EHOSTUNREACH),
1155252Svi117747 				 * pretend we have sent the data. We will
1156252Svi117747 				 * eventually time out trying to retramsmit
1157252Svi117747 				 * the data if the interface never comes up.
1158252Svi117747 				 * If we have already sent some stuff (i.e.,
1159252Svi117747 				 * notsent is B_FALSE) then we are fine, else
1160252Svi117747 				 * just mark this packet as sent.
1161252Svi117747 				 */
1162252Svi117747 				if (notsent && error == EHOSTUNREACH) {
1163252Svi117747 					SCTP_CHUNK_SENT(sctp, mp, sdc,
1164252Svi117747 					    fp, chunklen, meta);
1165252Svi117747 				}
11660Sstevel@tonic-gate 				freemsg(nmp);
11673448Sdh155122 				SCTP_KSTAT(sctps, sctp_output_failed);
11680Sstevel@tonic-gate 				goto unsent_data;
11690Sstevel@tonic-gate 			}
11700Sstevel@tonic-gate 		}
117113009SChandrasekar.Marimuthu@Sun.COM 		fp->sf_lastactive = now;
117213009SChandrasekar.Marimuthu@Sun.COM 		if (pathmax > fp->sf_pmss)
117313009SChandrasekar.Marimuthu@Sun.COM 			pathmax = fp->sf_pmss;
11740Sstevel@tonic-gate 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
11750Sstevel@tonic-gate 		mp = mp->b_next;
11760Sstevel@tonic-gate 
117712534SGeorge.Shepherd@Sun.COM 		/*
117812534SGeorge.Shepherd@Sun.COM 		 * Use this chunk to measure RTT?
117912534SGeorge.Shepherd@Sun.COM 		 * Must not be a retransmision of an earlier chunk,
118012534SGeorge.Shepherd@Sun.COM 		 * ensure the tsn is current.
118112534SGeorge.Shepherd@Sun.COM 		 */
118212534SGeorge.Shepherd@Sun.COM 		tsn = ntohl(sdc->sdh_tsn);
118312534SGeorge.Shepherd@Sun.COM 		if (sctp->sctp_out_time == 0 && tsn == (sctp->sctp_ltsn - 1)) {
11840Sstevel@tonic-gate 			sctp->sctp_out_time = now;
118512534SGeorge.Shepherd@Sun.COM 			sctp->sctp_rtt_tsn = tsn;
11860Sstevel@tonic-gate 		}
11870Sstevel@tonic-gate 		if (extra > 0) {
11884691Skcpoon 			fill = sctp_get_padding(sctp, extra);
11890Sstevel@tonic-gate 			if (fill != NULL) {
11900Sstevel@tonic-gate 				linkb(head, fill);
11910Sstevel@tonic-gate 				pad = extra;
11920Sstevel@tonic-gate 				seglen += extra;
11930Sstevel@tonic-gate 			} else {
11940Sstevel@tonic-gate 				goto unsent_data;
11950Sstevel@tonic-gate 			}
11960Sstevel@tonic-gate 		}
119710037SNick.Street@Sun.COM 		/*
119810037SNick.Street@Sun.COM 		 * Bundle chunks. We linkb() the chunks together to send
119910037SNick.Street@Sun.COM 		 * downstream in a single packet.
120010037SNick.Street@Sun.COM 		 * Partial chunks MUST NOT be bundled with full chunks, so we
120110037SNick.Street@Sun.COM 		 * rely on sctp_get_msg_to_send() to only return messages that
120210037SNick.Street@Sun.COM 		 * will fit entirely in the current packet.
120310037SNick.Street@Sun.COM 		 */
12040Sstevel@tonic-gate 		while (seglen < pathmax) {
12050Sstevel@tonic-gate 			int32_t		new_len;
12060Sstevel@tonic-gate 			int32_t		new_xtralen;
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 			while (mp != NULL) {
12090Sstevel@tonic-gate 				if (SCTP_CHUNK_CANSEND(mp))
12100Sstevel@tonic-gate 					break;
12110Sstevel@tonic-gate 				mp = mp->b_next;
12120Sstevel@tonic-gate 			}
12130Sstevel@tonic-gate 			if (mp == NULL) {
12140Sstevel@tonic-gate 				meta = sctp_get_msg_to_send(sctp, &mp,
12150Sstevel@tonic-gate 				    meta->b_next, &error, seglen,
12160Sstevel@tonic-gate 				    (seglen - xtralen) >= cansend ? 0 :
12170Sstevel@tonic-gate 				    cansend - seglen, fp);
121810037SNick.Street@Sun.COM 				if (error != 0)
121910037SNick.Street@Sun.COM 					break;
122010037SNick.Street@Sun.COM 				/* If no more eligible chunks, cease bundling */
122110037SNick.Street@Sun.COM 				if (meta == NULL)
12220Sstevel@tonic-gate 					break;
12230Sstevel@tonic-gate 				sctp->sctp_xmit_tail =  meta;
12240Sstevel@tonic-gate 			}
12250Sstevel@tonic-gate 			ASSERT(mp != NULL);
12260Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp) && SCTP_CHUNK_DEST(meta) &&
12270Sstevel@tonic-gate 			    fp != SCTP_CHUNK_DEST(meta)) {
12280Sstevel@tonic-gate 				break;
12290Sstevel@tonic-gate 			}
12300Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
12310Sstevel@tonic-gate 			chunklen = ntohs(sdc->sdh_len);
12320Sstevel@tonic-gate 			if ((extra = chunklen  & (SCTP_ALIGN - 1)) != 0)
12330Sstevel@tonic-gate 				extra = SCTP_ALIGN - extra;
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 			new_len = seglen + chunklen;
12360Sstevel@tonic-gate 			new_xtralen = xtralen + sizeof (*sdc);
12370Sstevel@tonic-gate 			chunklen -= sizeof (*sdc);
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 			if (new_len - new_xtralen > cansend ||
12400Sstevel@tonic-gate 			    new_len + extra > pathmax) {
12410Sstevel@tonic-gate 				break;
12420Sstevel@tonic-gate 			}
12430Sstevel@tonic-gate 			if ((nmp = dupmsg(mp)) == NULL)
12440Sstevel@tonic-gate 				break;
12450Sstevel@tonic-gate 			if (extra > 0) {
12464691Skcpoon 				fill = sctp_get_padding(sctp, extra);
12470Sstevel@tonic-gate 				if (fill != NULL) {
12480Sstevel@tonic-gate 					pad += extra;
12490Sstevel@tonic-gate 					new_len += extra;
12500Sstevel@tonic-gate 					linkb(nmp, fill);
12510Sstevel@tonic-gate 				} else {
12520Sstevel@tonic-gate 					freemsg(nmp);
12530Sstevel@tonic-gate 					break;
12540Sstevel@tonic-gate 				}
12550Sstevel@tonic-gate 			}
12560Sstevel@tonic-gate 			seglen = new_len;
12570Sstevel@tonic-gate 			xtralen = new_xtralen;
12580Sstevel@tonic-gate 			SCTP_CHUNK_CLEAR_FLAGS(nmp);
12590Sstevel@tonic-gate 			SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
12600Sstevel@tonic-gate 			linkb(head, nmp);
12610Sstevel@tonic-gate 			mp = mp->b_next;
12620Sstevel@tonic-gate 		}
126313009SChandrasekar.Marimuthu@Sun.COM 		if ((seglen > fp->sf_pmss) && fp->sf_isv4) {
12640Sstevel@tonic-gate 			ipha_t *iph = (ipha_t *)head->b_rptr;
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 			/*
12670Sstevel@tonic-gate 			 * Path MTU is different from what we thought it would
12680Sstevel@tonic-gate 			 * be when we created chunks, or IP headers have grown.
12690Sstevel@tonic-gate 			 * Need to clear the DF bit.
12700Sstevel@tonic-gate 			 */
12710Sstevel@tonic-gate 			iph->ipha_fragment_offset_and_flags = 0;
12720Sstevel@tonic-gate 		}
12730Sstevel@tonic-gate 		/* xmit segment */
12740Sstevel@tonic-gate 		ASSERT(cansend >= seglen - pad - xtralen);
12750Sstevel@tonic-gate 		cansend -= (seglen - pad - xtralen);
12760Sstevel@tonic-gate 		dprint(2, ("sctp_output: Sending packet %d bytes, tsn %x "
12771676Sjpk 		    "ssn %d to %p (rwnd %d, cansend %d, lastack_rxd %x)\n",
12781676Sjpk 		    seglen - xtralen, ntohl(sdc->sdh_tsn),
12791676Sjpk 		    ntohs(sdc->sdh_ssn), (void *)fp, sctp->sctp_frwnd,
12801676Sjpk 		    cansend, sctp->sctp_lastack_rxd));
128113009SChandrasekar.Marimuthu@Sun.COM 		sctp_set_iplen(sctp, head, fp->sf_ixa);
128213009SChandrasekar.Marimuthu@Sun.COM 		(void) conn_ip_output(head, fp->sf_ixa);
128311042SErik.Nordmark@Sun.COM 		BUMP_LOCAL(sctp->sctp_opkts);
12840Sstevel@tonic-gate 		/* arm rto timer (if not set) */
128513009SChandrasekar.Marimuthu@Sun.COM 		if (!fp->sf_timer_running)
128613009SChandrasekar.Marimuthu@Sun.COM 			SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->sf_rto);
1287252Svi117747 		notsent = B_FALSE;
12880Sstevel@tonic-gate 	}
12890Sstevel@tonic-gate 	sctp->sctp_active = now;
12900Sstevel@tonic-gate 	return;
12910Sstevel@tonic-gate unsent_data:
12920Sstevel@tonic-gate 	/* arm persist timer (if rto timer not set) */
129313009SChandrasekar.Marimuthu@Sun.COM 	if (!fp->sf_timer_running)
129413009SChandrasekar.Marimuthu@Sun.COM 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->sf_rto);
12950Sstevel@tonic-gate 	if (head != NULL)
12960Sstevel@tonic-gate 		freemsg(head);
12970Sstevel@tonic-gate }
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate /*
13000Sstevel@tonic-gate  * The following two functions initialize and destroy the cache
13010Sstevel@tonic-gate  * associated with the sets used for PR-SCTP.
13020Sstevel@tonic-gate  */
13030Sstevel@tonic-gate void
sctp_ftsn_sets_init(void)13040Sstevel@tonic-gate sctp_ftsn_sets_init(void)
13050Sstevel@tonic-gate {
13060Sstevel@tonic-gate 	sctp_kmem_ftsn_set_cache = kmem_cache_create("sctp_ftsn_set_cache",
13070Sstevel@tonic-gate 	    sizeof (sctp_ftsn_set_t), 0, NULL, NULL, NULL, NULL,
13080Sstevel@tonic-gate 	    NULL, 0);
13090Sstevel@tonic-gate }
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate void
sctp_ftsn_sets_fini(void)13120Sstevel@tonic-gate sctp_ftsn_sets_fini(void)
13130Sstevel@tonic-gate {
13140Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_ftsn_set_cache);
13150Sstevel@tonic-gate }
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate /* Free PR-SCTP sets */
13190Sstevel@tonic-gate void
sctp_free_ftsn_set(sctp_ftsn_set_t * s)13200Sstevel@tonic-gate sctp_free_ftsn_set(sctp_ftsn_set_t *s)
13210Sstevel@tonic-gate {
13220Sstevel@tonic-gate 	sctp_ftsn_set_t *p;
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	while (s != NULL) {
13250Sstevel@tonic-gate 		p = s->next;
13260Sstevel@tonic-gate 		s->next = NULL;
13270Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_ftsn_set_cache, s);
13280Sstevel@tonic-gate 		s = p;
13290Sstevel@tonic-gate 	}
13300Sstevel@tonic-gate }
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate /*
13330Sstevel@tonic-gate  * Given a message meta block, meta, this routine creates or modifies
13340Sstevel@tonic-gate  * the set that will be used to generate a Forward TSN chunk. If the
13350Sstevel@tonic-gate  * entry for stream id, sid, for this message already exists, the
13360Sstevel@tonic-gate  * sequence number, ssn, is updated if it is greater than the existing
13370Sstevel@tonic-gate  * one. If an entry for this sid does not exist, one is created if
133813009SChandrasekar.Marimuthu@Sun.COM  * the size does not exceed fp->sf_pmss. We return false in case
13390Sstevel@tonic-gate  * or an error.
13400Sstevel@tonic-gate  */
13410Sstevel@tonic-gate boolean_t
sctp_add_ftsn_set(sctp_ftsn_set_t ** s,sctp_faddr_t * fp,mblk_t * meta,uint_t * nsets,uint32_t * slen)13420Sstevel@tonic-gate sctp_add_ftsn_set(sctp_ftsn_set_t **s, sctp_faddr_t *fp, mblk_t *meta,
13430Sstevel@tonic-gate     uint_t *nsets, uint32_t *slen)
13440Sstevel@tonic-gate {
13450Sstevel@tonic-gate 	sctp_ftsn_set_t		*p;
13460Sstevel@tonic-gate 	sctp_msg_hdr_t		*msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
13470Sstevel@tonic-gate 	uint16_t		sid = htons(msg_hdr->smh_sid);
13480Sstevel@tonic-gate 	/* msg_hdr->smh_ssn is already in NBO */
13490Sstevel@tonic-gate 	uint16_t		ssn = msg_hdr->smh_ssn;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	ASSERT(s != NULL && nsets != NULL);
13520Sstevel@tonic-gate 	ASSERT((*nsets == 0 && *s == NULL) || (*nsets > 0 && *s != NULL));
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	if (*s == NULL) {
135513009SChandrasekar.Marimuthu@Sun.COM 		ASSERT((*slen + sizeof (uint32_t)) <= fp->sf_pmss);
13560Sstevel@tonic-gate 		*s = kmem_cache_alloc(sctp_kmem_ftsn_set_cache, KM_NOSLEEP);
13570Sstevel@tonic-gate 		if (*s == NULL)
13580Sstevel@tonic-gate 			return (B_FALSE);
13590Sstevel@tonic-gate 		(*s)->ftsn_entries.ftsn_sid = sid;
13600Sstevel@tonic-gate 		(*s)->ftsn_entries.ftsn_ssn = ssn;
13610Sstevel@tonic-gate 		(*s)->next = NULL;
13620Sstevel@tonic-gate 		*nsets = 1;
13630Sstevel@tonic-gate 		*slen += sizeof (uint32_t);
13640Sstevel@tonic-gate 		return (B_TRUE);
13650Sstevel@tonic-gate 	}
13660Sstevel@tonic-gate 	for (p = *s; p->next != NULL; p = p->next) {
13670Sstevel@tonic-gate 		if (p->ftsn_entries.ftsn_sid == sid) {
13680Sstevel@tonic-gate 			if (SSN_GT(ssn, p->ftsn_entries.ftsn_ssn))
13690Sstevel@tonic-gate 				p->ftsn_entries.ftsn_ssn = ssn;
13700Sstevel@tonic-gate 			return (B_TRUE);
13710Sstevel@tonic-gate 		}
13720Sstevel@tonic-gate 	}
13730Sstevel@tonic-gate 	/* the last one */
13740Sstevel@tonic-gate 	if (p->ftsn_entries.ftsn_sid == sid) {
13750Sstevel@tonic-gate 		if (SSN_GT(ssn, p->ftsn_entries.ftsn_ssn))
13760Sstevel@tonic-gate 			p->ftsn_entries.ftsn_ssn = ssn;
13770Sstevel@tonic-gate 	} else {
137813009SChandrasekar.Marimuthu@Sun.COM 		if ((*slen + sizeof (uint32_t)) > fp->sf_pmss)
13790Sstevel@tonic-gate 			return (B_FALSE);
13800Sstevel@tonic-gate 		p->next = kmem_cache_alloc(sctp_kmem_ftsn_set_cache,
13810Sstevel@tonic-gate 		    KM_NOSLEEP);
13820Sstevel@tonic-gate 		if (p->next == NULL)
13830Sstevel@tonic-gate 			return (B_FALSE);
13840Sstevel@tonic-gate 		p = p->next;
13850Sstevel@tonic-gate 		p->ftsn_entries.ftsn_sid = sid;
13860Sstevel@tonic-gate 		p->ftsn_entries.ftsn_ssn = ssn;
13870Sstevel@tonic-gate 		p->next = NULL;
13880Sstevel@tonic-gate 		(*nsets)++;
13890Sstevel@tonic-gate 		*slen += sizeof (uint32_t);
13900Sstevel@tonic-gate 	}
13910Sstevel@tonic-gate 	return (B_TRUE);
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate /*
13950Sstevel@tonic-gate  * Given a set of stream id - sequence number pairs, this routing creates
13960Sstevel@tonic-gate  * a Forward TSN chunk. The cumulative TSN (advanced peer ack point)
13970Sstevel@tonic-gate  * for the chunk is obtained from sctp->sctp_adv_pap. The caller
13980Sstevel@tonic-gate  * will add the IP/SCTP header.
13990Sstevel@tonic-gate  */
14000Sstevel@tonic-gate mblk_t *
sctp_make_ftsn_chunk(sctp_t * sctp,sctp_faddr_t * fp,sctp_ftsn_set_t * sets,uint_t nsets,uint32_t seglen)14010Sstevel@tonic-gate sctp_make_ftsn_chunk(sctp_t *sctp, sctp_faddr_t *fp, sctp_ftsn_set_t *sets,
14020Sstevel@tonic-gate     uint_t nsets, uint32_t seglen)
14030Sstevel@tonic-gate {
14040Sstevel@tonic-gate 	mblk_t			*ftsn_mp;
14050Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ch_hdr;
14060Sstevel@tonic-gate 	uint32_t		*advtsn;
14070Sstevel@tonic-gate 	uint16_t		schlen;
14080Sstevel@tonic-gate 	size_t			xtralen;
14090Sstevel@tonic-gate 	ftsn_entry_t		*ftsn_entry;
14103448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	seglen += sizeof (sctp_chunk_hdr_t);
141313009SChandrasekar.Marimuthu@Sun.COM 	if (fp->sf_isv4)
14143448Sdh155122 		xtralen = sctp->sctp_hdr_len + sctps->sctps_wroff_xtra;
14150Sstevel@tonic-gate 	else
14163448Sdh155122 		xtralen = sctp->sctp_hdr6_len + sctps->sctps_wroff_xtra;
141711042SErik.Nordmark@Sun.COM 	ftsn_mp = allocb(xtralen + seglen, BPRI_MED);
14180Sstevel@tonic-gate 	if (ftsn_mp == NULL)
14190Sstevel@tonic-gate 		return (NULL);
14200Sstevel@tonic-gate 	ftsn_mp->b_rptr += xtralen;
14210Sstevel@tonic-gate 	ftsn_mp->b_wptr = ftsn_mp->b_rptr + seglen;
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	ch_hdr = (sctp_chunk_hdr_t *)ftsn_mp->b_rptr;
14240Sstevel@tonic-gate 	ch_hdr->sch_id = CHUNK_FORWARD_TSN;
14250Sstevel@tonic-gate 	ch_hdr->sch_flags = 0;
14260Sstevel@tonic-gate 	/*
14270Sstevel@tonic-gate 	 * The cast here should not be an issue since seglen is
14280Sstevel@tonic-gate 	 * the length of the Forward TSN chunk.
14290Sstevel@tonic-gate 	 */
14300Sstevel@tonic-gate 	schlen = (uint16_t)seglen;
14310Sstevel@tonic-gate 	U16_TO_ABE16(schlen, &(ch_hdr->sch_len));
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	advtsn = (uint32_t *)(ch_hdr + 1);
14340Sstevel@tonic-gate 	U32_TO_ABE32(sctp->sctp_adv_pap, advtsn);
14350Sstevel@tonic-gate 	ftsn_entry = (ftsn_entry_t *)(advtsn + 1);
14360Sstevel@tonic-gate 	while (nsets > 0) {
14370Sstevel@tonic-gate 		ASSERT((uchar_t *)&ftsn_entry[1] <= ftsn_mp->b_wptr);
14380Sstevel@tonic-gate 		ftsn_entry->ftsn_sid = sets->ftsn_entries.ftsn_sid;
14390Sstevel@tonic-gate 		ftsn_entry->ftsn_ssn = sets->ftsn_entries.ftsn_ssn;
14400Sstevel@tonic-gate 		ftsn_entry++;
14410Sstevel@tonic-gate 		sets = sets->next;
14420Sstevel@tonic-gate 		nsets--;
14430Sstevel@tonic-gate 	}
14440Sstevel@tonic-gate 	return (ftsn_mp);
14450Sstevel@tonic-gate }
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate /*
14480Sstevel@tonic-gate  * Given a starting message, the routine steps through all the
14490Sstevel@tonic-gate  * messages whose TSN is less than sctp->sctp_adv_pap and creates
14500Sstevel@tonic-gate  * ftsn sets. The ftsn sets is then used to create an Forward TSN
14510Sstevel@tonic-gate  * chunk. All the messages, that have chunks that are included in the
14520Sstevel@tonic-gate  * ftsn sets, are flagged abandonded. If a message is partially sent
14530Sstevel@tonic-gate  * and is deemed abandoned, all remaining unsent chunks are marked
14540Sstevel@tonic-gate  * abandoned and are deducted from sctp_unsent.
14550Sstevel@tonic-gate  */
14560Sstevel@tonic-gate void
sctp_make_ftsns(sctp_t * sctp,mblk_t * meta,mblk_t * mp,mblk_t ** nmp,sctp_faddr_t * fp,uint32_t * seglen)14570Sstevel@tonic-gate sctp_make_ftsns(sctp_t *sctp, mblk_t *meta, mblk_t *mp, mblk_t **nmp,
14580Sstevel@tonic-gate     sctp_faddr_t *fp, uint32_t *seglen)
14590Sstevel@tonic-gate {
14600Sstevel@tonic-gate 	mblk_t		*mp1 = mp;
14610Sstevel@tonic-gate 	mblk_t		*mp_head = mp;
14620Sstevel@tonic-gate 	mblk_t		*meta_head = meta;
14630Sstevel@tonic-gate 	mblk_t		*head;
14640Sstevel@tonic-gate 	sctp_ftsn_set_t	*sets = NULL;
14650Sstevel@tonic-gate 	uint_t		nsets = 0;
14660Sstevel@tonic-gate 	uint16_t	clen;
14670Sstevel@tonic-gate 	sctp_data_hdr_t	*sdc;
14680Sstevel@tonic-gate 	uint32_t	sacklen;
14690Sstevel@tonic-gate 	uint32_t	adv_pap = sctp->sctp_adv_pap;
14700Sstevel@tonic-gate 	uint32_t	unsent = 0;
14710Sstevel@tonic-gate 	boolean_t	ubit;
14723448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	*seglen = sizeof (uint32_t);
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
14770Sstevel@tonic-gate 	while (meta != NULL &&
14780Sstevel@tonic-gate 	    SEQ_GEQ(sctp->sctp_adv_pap, ntohl(sdc->sdh_tsn))) {
14790Sstevel@tonic-gate 		/*
14800Sstevel@tonic-gate 		 * Skip adding FTSN sets for un-ordered messages as they do
14810Sstevel@tonic-gate 		 * not have SSNs.
14820Sstevel@tonic-gate 		 */
14830Sstevel@tonic-gate 		ubit = SCTP_DATA_GET_UBIT(sdc);
14840Sstevel@tonic-gate 		if (!ubit &&
14850Sstevel@tonic-gate 		    !sctp_add_ftsn_set(&sets, fp, meta, &nsets, seglen)) {
14860Sstevel@tonic-gate 			meta = NULL;
14870Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
14880Sstevel@tonic-gate 			goto ftsn_done;
14890Sstevel@tonic-gate 		}
14900Sstevel@tonic-gate 		while (mp1 != NULL && SCTP_CHUNK_ISSENT(mp1)) {
14910Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
14920Sstevel@tonic-gate 			adv_pap = ntohl(sdc->sdh_tsn);
14930Sstevel@tonic-gate 			mp1 = mp1->b_next;
14940Sstevel@tonic-gate 		}
14950Sstevel@tonic-gate 		meta = meta->b_next;
14960Sstevel@tonic-gate 		if (meta != NULL) {
14970Sstevel@tonic-gate 			mp1 = meta->b_cont;
14980Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp1))
14990Sstevel@tonic-gate 				break;
15000Sstevel@tonic-gate 			sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
15010Sstevel@tonic-gate 		}
15020Sstevel@tonic-gate 	}
15030Sstevel@tonic-gate ftsn_done:
15040Sstevel@tonic-gate 	/*
15050Sstevel@tonic-gate 	 * Can't compare with sets == NULL, since we don't add any
15060Sstevel@tonic-gate 	 * sets for un-ordered messages.
15070Sstevel@tonic-gate 	 */
15080Sstevel@tonic-gate 	if (meta == meta_head)
15090Sstevel@tonic-gate 		return;
15100Sstevel@tonic-gate 	*nmp = sctp_make_ftsn_chunk(sctp, fp, sets, nsets, *seglen);
15110Sstevel@tonic-gate 	sctp_free_ftsn_set(sets);
15120Sstevel@tonic-gate 	if (*nmp == NULL)
15130Sstevel@tonic-gate 		return;
15140Sstevel@tonic-gate 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
15150Sstevel@tonic-gate 		sacklen = 0;
15160Sstevel@tonic-gate 	} else {
15170Sstevel@tonic-gate 		sacklen = sizeof (sctp_chunk_hdr_t) +
15180Sstevel@tonic-gate 		    sizeof (sctp_sack_chunk_t) +
15190Sstevel@tonic-gate 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
152013009SChandrasekar.Marimuthu@Sun.COM 		if (*seglen + sacklen > sctp->sctp_lastdata->sf_pmss) {
15210Sstevel@tonic-gate 			/* piggybacked SACK doesn't fit */
15220Sstevel@tonic-gate 			sacklen = 0;
15230Sstevel@tonic-gate 		} else {
15240Sstevel@tonic-gate 			fp = sctp->sctp_lastdata;
15250Sstevel@tonic-gate 		}
15260Sstevel@tonic-gate 	}
1527252Svi117747 	head = sctp_add_proto_hdr(sctp, fp, *nmp, sacklen, NULL);
15280Sstevel@tonic-gate 	if (head == NULL) {
15290Sstevel@tonic-gate 		freemsg(*nmp);
15300Sstevel@tonic-gate 		*nmp = NULL;
15313448Sdh155122 		SCTP_KSTAT(sctps, sctp_send_ftsn_failed);
15320Sstevel@tonic-gate 		return;
15330Sstevel@tonic-gate 	}
15340Sstevel@tonic-gate 	*seglen += sacklen;
15350Sstevel@tonic-gate 	*nmp = head;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	/*
15380Sstevel@tonic-gate 	 * XXXNeed to optimise this, the reason it is done here is so
15390Sstevel@tonic-gate 	 * that we don't have to undo in case of failure.
15400Sstevel@tonic-gate 	 */
15410Sstevel@tonic-gate 	mp1 = mp_head;
15420Sstevel@tonic-gate 	sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
15430Sstevel@tonic-gate 	while (meta_head != NULL &&
15440Sstevel@tonic-gate 	    SEQ_GEQ(sctp->sctp_adv_pap, ntohl(sdc->sdh_tsn))) {
15450Sstevel@tonic-gate 		if (!SCTP_IS_MSG_ABANDONED(meta_head))
15460Sstevel@tonic-gate 			SCTP_MSG_SET_ABANDONED(meta_head);
15470Sstevel@tonic-gate 		while (mp1 != NULL && SCTP_CHUNK_ISSENT(mp1)) {
15480Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
15490Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISACKED(mp1)) {
15500Sstevel@tonic-gate 				clen = ntohs(sdc->sdh_len) - sizeof (*sdc);
15510Sstevel@tonic-gate 				SCTP_CHUNK_SENT(sctp, mp1, sdc, fp, clen,
15520Sstevel@tonic-gate 				    meta_head);
15530Sstevel@tonic-gate 			}
15540Sstevel@tonic-gate 			mp1 = mp1->b_next;
15550Sstevel@tonic-gate 		}
15560Sstevel@tonic-gate 		while (mp1 != NULL) {
15570Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp1->b_rptr;
15580Sstevel@tonic-gate 			if (!SCTP_CHUNK_ABANDONED(mp1)) {
15590Sstevel@tonic-gate 				ASSERT(!SCTP_CHUNK_ISSENT(mp1));
15600Sstevel@tonic-gate 				unsent += ntohs(sdc->sdh_len) - sizeof (*sdc);
15610Sstevel@tonic-gate 				SCTP_ABANDON_CHUNK(mp1);
15620Sstevel@tonic-gate 			}
15630Sstevel@tonic-gate 			mp1 = mp1->b_next;
15640Sstevel@tonic-gate 		}
15650Sstevel@tonic-gate 		meta_head = meta_head->b_next;
15660Sstevel@tonic-gate 		if (meta_head != NULL) {
15670Sstevel@tonic-gate 			mp1 = meta_head->b_cont;
15680Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp1))
15690Sstevel@tonic-gate 				break;
15700Sstevel@tonic-gate 			sdc  = (sctp_data_hdr_t *)mp1->b_rptr;
15710Sstevel@tonic-gate 		}
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate 	if (unsent > 0) {
15740Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent >= unsent);
15750Sstevel@tonic-gate 		sctp->sctp_unsent -= unsent;
15760Sstevel@tonic-gate 		/*
15770Sstevel@tonic-gate 		 * Update ULP the amount of queued data, which is
15780Sstevel@tonic-gate 		 * sent-unack'ed + unsent.
15790Sstevel@tonic-gate 		 */
15808348SEric.Yu@Sun.COM 		if (!SCTP_IS_DETACHED(sctp))
15818348SEric.Yu@Sun.COM 			SCTP_TXQ_UPDATE(sctp);
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate }
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate /*
15860Sstevel@tonic-gate  * This function steps through messages starting at meta and checks if
15870Sstevel@tonic-gate  * the message is abandoned. It stops when it hits an unsent chunk or
15880Sstevel@tonic-gate  * a message that has all its chunk acked. This is the only place
15890Sstevel@tonic-gate  * where the sctp_adv_pap is moved forward to indicated abandoned
15900Sstevel@tonic-gate  * messages.
15910Sstevel@tonic-gate  */
15920Sstevel@tonic-gate void
sctp_check_adv_ack_pt(sctp_t * sctp,mblk_t * meta,mblk_t * mp)15930Sstevel@tonic-gate sctp_check_adv_ack_pt(sctp_t *sctp, mblk_t *meta, mblk_t *mp)
15940Sstevel@tonic-gate {
15950Sstevel@tonic-gate 	uint32_t	tsn = sctp->sctp_adv_pap;
15960Sstevel@tonic-gate 	sctp_data_hdr_t	*sdc;
15970Sstevel@tonic-gate 	sctp_msg_hdr_t	*msg_hdr;
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate 	ASSERT(mp != NULL);
16000Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
16010Sstevel@tonic-gate 	ASSERT(SEQ_GT(ntohl(sdc->sdh_tsn), sctp->sctp_lastack_rxd));
16020Sstevel@tonic-gate 	msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
16030Sstevel@tonic-gate 	if (!SCTP_IS_MSG_ABANDONED(meta) &&
16040Sstevel@tonic-gate 	    !SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
16050Sstevel@tonic-gate 		return;
16060Sstevel@tonic-gate 	}
16070Sstevel@tonic-gate 	while (meta != NULL) {
16080Sstevel@tonic-gate 		while (mp != NULL && SCTP_CHUNK_ISSENT(mp)) {
16090Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
16100Sstevel@tonic-gate 			tsn = ntohl(sdc->sdh_tsn);
16110Sstevel@tonic-gate 			mp = mp->b_next;
16120Sstevel@tonic-gate 		}
16130Sstevel@tonic-gate 		if (mp != NULL)
16140Sstevel@tonic-gate 			break;
16150Sstevel@tonic-gate 		/*
16160Sstevel@tonic-gate 		 * We continue checking for successive messages only if there
16170Sstevel@tonic-gate 		 * is a chunk marked for retransmission. Else, we might
16180Sstevel@tonic-gate 		 * end up sending FTSN prematurely for chunks that have been
16190Sstevel@tonic-gate 		 * sent, but not yet acked.
16200Sstevel@tonic-gate 		 */
16210Sstevel@tonic-gate 		if ((meta = meta->b_next) != NULL) {
16220Sstevel@tonic-gate 			msg_hdr = (sctp_msg_hdr_t *)meta->b_rptr;
16230Sstevel@tonic-gate 			if (!SCTP_IS_MSG_ABANDONED(meta) &&
16240Sstevel@tonic-gate 			    !SCTP_MSG_TO_BE_ABANDONED(meta, msg_hdr, sctp)) {
16250Sstevel@tonic-gate 				break;
16260Sstevel@tonic-gate 			}
16270Sstevel@tonic-gate 			for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
16280Sstevel@tonic-gate 				if (!SCTP_CHUNK_ISSENT(mp)) {
16290Sstevel@tonic-gate 					sctp->sctp_adv_pap = tsn;
16300Sstevel@tonic-gate 					return;
16310Sstevel@tonic-gate 				}
16320Sstevel@tonic-gate 				if (SCTP_CHUNK_WANT_REXMIT(mp))
16330Sstevel@tonic-gate 					break;
16340Sstevel@tonic-gate 			}
16350Sstevel@tonic-gate 			if (mp == NULL)
16360Sstevel@tonic-gate 				break;
16370Sstevel@tonic-gate 		}
16380Sstevel@tonic-gate 	}
16390Sstevel@tonic-gate 	sctp->sctp_adv_pap = tsn;
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate 
16421735Skcpoon 
16431735Skcpoon /*
16441735Skcpoon  * Determine if we should bundle a data chunk with the chunk being
16451735Skcpoon  * retransmitted.  We bundle if
16461735Skcpoon  *
16471735Skcpoon  * - the chunk is sent to the same destination and unack'ed.
16481735Skcpoon  *
16491735Skcpoon  * OR
16501735Skcpoon  *
16511735Skcpoon  * - the chunk is unsent, i.e. new data.
16521735Skcpoon  */
16531735Skcpoon #define	SCTP_CHUNK_RX_CANBUNDLE(mp, fp)					\
16541735Skcpoon 	(!SCTP_CHUNK_ABANDONED((mp)) && 				\
16551735Skcpoon 	((SCTP_CHUNK_ISSENT((mp)) && (SCTP_CHUNK_DEST(mp) == (fp) &&	\
16561735Skcpoon 	!SCTP_CHUNK_ISACKED(mp))) ||					\
16571735Skcpoon 	(((mp)->b_flag & (SCTP_CHUNK_FLAG_REXMIT|SCTP_CHUNK_FLAG_SENT)) != \
16581735Skcpoon 	SCTP_CHUNK_FLAG_SENT)))
16591735Skcpoon 
16600Sstevel@tonic-gate /*
16610Sstevel@tonic-gate  * Retransmit first segment which hasn't been acked with cumtsn or send
16620Sstevel@tonic-gate  * a Forward TSN chunk, if appropriate.
16630Sstevel@tonic-gate  */
16640Sstevel@tonic-gate void
sctp_rexmit(sctp_t * sctp,sctp_faddr_t * oldfp)16650Sstevel@tonic-gate sctp_rexmit(sctp_t *sctp, sctp_faddr_t *oldfp)
16660Sstevel@tonic-gate {
16670Sstevel@tonic-gate 	mblk_t		*mp;
16680Sstevel@tonic-gate 	mblk_t		*nmp = NULL;
16690Sstevel@tonic-gate 	mblk_t		*head;
16700Sstevel@tonic-gate 	mblk_t		*meta = sctp->sctp_xmit_head;
16710Sstevel@tonic-gate 	mblk_t		*fill;
16720Sstevel@tonic-gate 	uint32_t	seglen = 0;
16730Sstevel@tonic-gate 	uint32_t	sacklen;
16740Sstevel@tonic-gate 	uint16_t	chunklen;
16750Sstevel@tonic-gate 	int		extra;
16760Sstevel@tonic-gate 	sctp_data_hdr_t	*sdc;
16770Sstevel@tonic-gate 	sctp_faddr_t	*fp;
16780Sstevel@tonic-gate 	uint32_t	adv_pap = sctp->sctp_adv_pap;
16790Sstevel@tonic-gate 	boolean_t	do_ftsn = B_FALSE;
16800Sstevel@tonic-gate 	boolean_t	ftsn_check = B_TRUE;
16811735Skcpoon 	uint32_t	first_ua_tsn;
16821735Skcpoon 	sctp_msg_hdr_t	*mhdr;
16833448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
16848154SGeorge.Shepherd@Sun.COM 	int		error;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	while (meta != NULL) {
16870Sstevel@tonic-gate 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
16880Sstevel@tonic-gate 			uint32_t	tsn;
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp))
16910Sstevel@tonic-gate 				goto window_probe;
16920Sstevel@tonic-gate 			/*
16930Sstevel@tonic-gate 			 * We break in the following cases -
16940Sstevel@tonic-gate 			 *
16950Sstevel@tonic-gate 			 *	if the advanced peer ack point includes the next
16960Sstevel@tonic-gate 			 *	chunk to be retransmited - possibly the Forward
16970Sstevel@tonic-gate 			 * 	TSN was lost.
16980Sstevel@tonic-gate 			 *
16990Sstevel@tonic-gate 			 *	if we are PRSCTP aware and the next chunk to be
17000Sstevel@tonic-gate 			 *	retransmitted is now abandoned
17010Sstevel@tonic-gate 			 *
17020Sstevel@tonic-gate 			 *	if the next chunk to be retransmitted is for
17030Sstevel@tonic-gate 			 *	the dest on which the timer went off. (this
17040Sstevel@tonic-gate 			 *	message is not abandoned).
17050Sstevel@tonic-gate 			 *
17060Sstevel@tonic-gate 			 * We check for Forward TSN only for the first
17070Sstevel@tonic-gate 			 * eligible chunk to be retransmitted. The reason
17080Sstevel@tonic-gate 			 * being if the first eligible chunk is skipped (say
17090Sstevel@tonic-gate 			 * it was sent to a destination other than oldfp)
17100Sstevel@tonic-gate 			 * then we cannot advance the cum TSN via Forward
17110Sstevel@tonic-gate 			 * TSN chunk.
17120Sstevel@tonic-gate 			 *
17130Sstevel@tonic-gate 			 * Also, ftsn_check is B_TRUE only for the first
17140Sstevel@tonic-gate 			 * eligible chunk, it  will be B_FALSE for all
17150Sstevel@tonic-gate 			 * subsequent candidate messages for retransmission.
17160Sstevel@tonic-gate 			 */
17170Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
17180Sstevel@tonic-gate 			tsn = ntohl(sdc->sdh_tsn);
17190Sstevel@tonic-gate 			if (SEQ_GT(tsn, sctp->sctp_lastack_rxd)) {
17200Sstevel@tonic-gate 				if (sctp->sctp_prsctp_aware && ftsn_check) {
17210Sstevel@tonic-gate 					if (SEQ_GEQ(sctp->sctp_adv_pap, tsn)) {
17220Sstevel@tonic-gate 						ASSERT(sctp->sctp_prsctp_aware);
17230Sstevel@tonic-gate 						do_ftsn = B_TRUE;
17240Sstevel@tonic-gate 						goto out;
17250Sstevel@tonic-gate 					} else {
17260Sstevel@tonic-gate 						sctp_check_adv_ack_pt(sctp,
17270Sstevel@tonic-gate 						    meta, mp);
17280Sstevel@tonic-gate 						if (SEQ_GT(sctp->sctp_adv_pap,
17290Sstevel@tonic-gate 						    adv_pap)) {
17300Sstevel@tonic-gate 							do_ftsn = B_TRUE;
17310Sstevel@tonic-gate 							goto out;
17320Sstevel@tonic-gate 						}
17330Sstevel@tonic-gate 					}
17340Sstevel@tonic-gate 					ftsn_check = B_FALSE;
17350Sstevel@tonic-gate 				}
17360Sstevel@tonic-gate 				if (SCTP_CHUNK_DEST(mp) == oldfp)
17370Sstevel@tonic-gate 					goto out;
17380Sstevel@tonic-gate 			}
17390Sstevel@tonic-gate 		}
17400Sstevel@tonic-gate 		meta = meta->b_next;
17410Sstevel@tonic-gate 		if (meta != NULL && sctp->sctp_prsctp_aware) {
17421735Skcpoon 			mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 			while (meta != NULL && (SCTP_IS_MSG_ABANDONED(meta) ||
17450Sstevel@tonic-gate 			    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr, sctp))) {
17460Sstevel@tonic-gate 				meta = meta->b_next;
17470Sstevel@tonic-gate 			}
17480Sstevel@tonic-gate 		}
17490Sstevel@tonic-gate 	}
17500Sstevel@tonic-gate window_probe:
17510Sstevel@tonic-gate 	/*
17520Sstevel@tonic-gate 	 * Retransmit fired for a destination which didn't have
17530Sstevel@tonic-gate 	 * any unacked data pending.
17540Sstevel@tonic-gate 	 */
17551932Svi117747 	if (sctp->sctp_unacked == 0 && sctp->sctp_unsent != 0) {
17560Sstevel@tonic-gate 		/*
17570Sstevel@tonic-gate 		 * Send a window probe. Inflate frwnd to allow
17580Sstevel@tonic-gate 		 * sending one segment.
17590Sstevel@tonic-gate 		 */
176013009SChandrasekar.Marimuthu@Sun.COM 		if (sctp->sctp_frwnd < (oldfp->sf_pmss - sizeof (*sdc)))
176113009SChandrasekar.Marimuthu@Sun.COM 			sctp->sctp_frwnd = oldfp->sf_pmss - sizeof (*sdc);
17623795Skcpoon 
17631932Svi117747 		/* next TSN to send */
17641932Svi117747 		sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn;
17653795Skcpoon 
17663795Skcpoon 		/*
17673795Skcpoon 		 * The above sctp_frwnd adjustment is coarse.  The "changed"
17683795Skcpoon 		 * sctp_frwnd may allow us to send more than 1 packet.  So
17693795Skcpoon 		 * tell sctp_output() to send only 1 packet.
17703795Skcpoon 		 */
17713795Skcpoon 		sctp_output(sctp, 1);
17723795Skcpoon 
17731932Svi117747 		/* Last sent TSN */
17741932Svi117747 		sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn - 1;
17751932Svi117747 		ASSERT(sctp->sctp_rxt_maxtsn >= sctp->sctp_rxt_nxttsn);
17761932Svi117747 		sctp->sctp_zero_win_probe = B_TRUE;
177712869SKacheong.Poon@Sun.COM 		SCTPS_BUMP_MIB(sctps, sctpOutWinProbe);
17780Sstevel@tonic-gate 	}
17790Sstevel@tonic-gate 	return;
17800Sstevel@tonic-gate out:
17810Sstevel@tonic-gate 	/*
17823795Skcpoon 	 * After a time out, assume that everything has left the network.  So
17833795Skcpoon 	 * we can clear rxt_unacked for the original peer address.
17843795Skcpoon 	 */
178513009SChandrasekar.Marimuthu@Sun.COM 	oldfp->sf_rxt_unacked = 0;
17863795Skcpoon 
17873795Skcpoon 	/*
17883795Skcpoon 	 * If we were probing for zero window, don't adjust retransmission
17891932Svi117747 	 * variables, but the timer is still backed off.
17901932Svi117747 	 */
17911932Svi117747 	if (sctp->sctp_zero_win_probe) {
17921932Svi117747 		mblk_t	*pkt;
17931932Svi117747 		uint_t	pkt_len;
17941932Svi117747 
17951932Svi117747 		/*
17961932Svi117747 		 * Get the Zero Win Probe for retrasmission, sctp_rxt_nxttsn
17971932Svi117747 		 * and sctp_rxt_maxtsn will specify the ZWP packet.
17981932Svi117747 		 */
17991932Svi117747 		fp = oldfp;
180013009SChandrasekar.Marimuthu@Sun.COM 		if (oldfp->sf_state != SCTP_FADDRS_ALIVE)
18011932Svi117747 			fp = sctp_rotate_faddr(sctp, oldfp);
18021932Svi117747 		pkt = sctp_rexmit_packet(sctp, &meta, &mp, fp, &pkt_len);
18031932Svi117747 		if (pkt != NULL) {
180413009SChandrasekar.Marimuthu@Sun.COM 			ASSERT(pkt_len <= fp->sf_pmss);
180513009SChandrasekar.Marimuthu@Sun.COM 			sctp_set_iplen(sctp, pkt, fp->sf_ixa);
180613009SChandrasekar.Marimuthu@Sun.COM 			(void) conn_ip_output(pkt, fp->sf_ixa);
180711042SErik.Nordmark@Sun.COM 			BUMP_LOCAL(sctp->sctp_opkts);
18081932Svi117747 		} else {
18093448Sdh155122 			SCTP_KSTAT(sctps, sctp_ss_rexmit_failed);
18101932Svi117747 		}
18113795Skcpoon 
18123795Skcpoon 		/*
18133795Skcpoon 		 * The strikes will be clear by sctp_faddr_alive() when the
18143795Skcpoon 		 * other side sends us an ack.
18153795Skcpoon 		 */
181613009SChandrasekar.Marimuthu@Sun.COM 		oldfp->sf_strikes++;
18171932Svi117747 		sctp->sctp_strikes++;
18183795Skcpoon 
181912474SGeorge.Shepherd@Sun.COM 		SCTP_CALC_RXT(sctp, oldfp, sctp->sctp_rto_max);
182013009SChandrasekar.Marimuthu@Sun.COM 		if (oldfp != fp && oldfp->sf_suna != 0)
182113009SChandrasekar.Marimuthu@Sun.COM 			SCTP_FADDR_TIMER_RESTART(sctp, oldfp, fp->sf_rto);
182213009SChandrasekar.Marimuthu@Sun.COM 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->sf_rto);
182312869SKacheong.Poon@Sun.COM 		SCTPS_BUMP_MIB(sctps, sctpOutWinProbe);
18241932Svi117747 		return;
18251932Svi117747 	}
18261932Svi117747 
18271932Svi117747 	/*
18280Sstevel@tonic-gate 	 * Enter slowstart for this destination
18290Sstevel@tonic-gate 	 */
183013009SChandrasekar.Marimuthu@Sun.COM 	oldfp->sf_ssthresh = oldfp->sf_cwnd / 2;
183113009SChandrasekar.Marimuthu@Sun.COM 	if (oldfp->sf_ssthresh < 2 * oldfp->sf_pmss)
183213009SChandrasekar.Marimuthu@Sun.COM 		oldfp->sf_ssthresh = 2 * oldfp->sf_pmss;
183313009SChandrasekar.Marimuthu@Sun.COM 	oldfp->sf_cwnd = oldfp->sf_pmss;
183413009SChandrasekar.Marimuthu@Sun.COM 	oldfp->sf_pba = 0;
18350Sstevel@tonic-gate 	fp = sctp_rotate_faddr(sctp, oldfp);
18360Sstevel@tonic-gate 	ASSERT(fp != NULL);
18370Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
18380Sstevel@tonic-gate 
18391735Skcpoon 	first_ua_tsn = ntohl(sdc->sdh_tsn);
18400Sstevel@tonic-gate 	if (do_ftsn) {
18410Sstevel@tonic-gate 		sctp_make_ftsns(sctp, meta, mp, &nmp, fp, &seglen);
18420Sstevel@tonic-gate 		if (nmp == NULL) {
18430Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
18440Sstevel@tonic-gate 			goto restart_timer;
18450Sstevel@tonic-gate 		}
18460Sstevel@tonic-gate 		head = nmp;
18471735Skcpoon 		/*
18481735Skcpoon 		 * Move to the next unabandoned chunk. XXXCheck if meta will
18491735Skcpoon 		 * always be marked abandoned.
18501735Skcpoon 		 */
18511735Skcpoon 		while (meta != NULL && SCTP_IS_MSG_ABANDONED(meta))
18521735Skcpoon 			meta = meta->b_next;
18530Sstevel@tonic-gate 		if (meta != NULL)
18541735Skcpoon 			mp = mp->b_cont;
18551735Skcpoon 		else
18561735Skcpoon 			mp = NULL;
18570Sstevel@tonic-gate 		goto try_bundle;
18580Sstevel@tonic-gate 	}
18590Sstevel@tonic-gate 	seglen = ntohs(sdc->sdh_len);
18600Sstevel@tonic-gate 	chunklen = seglen - sizeof (*sdc);
18610Sstevel@tonic-gate 	if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
18620Sstevel@tonic-gate 		extra = SCTP_ALIGN - extra;
18630Sstevel@tonic-gate 
18641735Skcpoon 	/* Find out if we need to piggyback SACK. */
18651735Skcpoon 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
18661735Skcpoon 		sacklen = 0;
18671735Skcpoon 	} else {
18681735Skcpoon 		sacklen = sizeof (sctp_chunk_hdr_t) +
18691735Skcpoon 		    sizeof (sctp_sack_chunk_t) +
18701735Skcpoon 		    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
187113009SChandrasekar.Marimuthu@Sun.COM 		if (seglen + sacklen > sctp->sctp_lastdata->sf_pmss) {
18721735Skcpoon 			/* piggybacked SACK doesn't fit */
18731735Skcpoon 			sacklen = 0;
18741735Skcpoon 		} else {
18751735Skcpoon 			/*
18761735Skcpoon 			 * OK, we have room to send SACK back.  But we
18771735Skcpoon 			 * should send it back to the last fp where we
18781735Skcpoon 			 * receive data from, unless sctp_lastdata equals
18791735Skcpoon 			 * oldfp, then we should probably not send it
18801735Skcpoon 			 * back to that fp.  Also we should check that
18811735Skcpoon 			 * the fp is alive.
18821735Skcpoon 			 */
18831735Skcpoon 			if (sctp->sctp_lastdata != oldfp &&
188413009SChandrasekar.Marimuthu@Sun.COM 			    sctp->sctp_lastdata->sf_state ==
188513009SChandrasekar.Marimuthu@Sun.COM 			    SCTP_FADDRS_ALIVE) {
18861735Skcpoon 				fp = sctp->sctp_lastdata;
18871735Skcpoon 			}
18881735Skcpoon 		}
18891735Skcpoon 	}
18901735Skcpoon 
18910Sstevel@tonic-gate 	/*
18920Sstevel@tonic-gate 	 * Cancel RTT measurement if the retransmitted TSN is before the
18930Sstevel@tonic-gate 	 * TSN used for timimg.
18940Sstevel@tonic-gate 	 */
18950Sstevel@tonic-gate 	if (sctp->sctp_out_time != 0 &&
18960Sstevel@tonic-gate 	    SEQ_GEQ(sctp->sctp_rtt_tsn, sdc->sdh_tsn)) {
18970Sstevel@tonic-gate 		sctp->sctp_out_time = 0;
18980Sstevel@tonic-gate 	}
18990Sstevel@tonic-gate 	/* Clear the counter as the RTT calculation may be off. */
190013009SChandrasekar.Marimuthu@Sun.COM 	fp->sf_rtt_updates = 0;
190113009SChandrasekar.Marimuthu@Sun.COM 	oldfp->sf_rtt_updates = 0;
19020Sstevel@tonic-gate 
19031735Skcpoon 	/*
19041735Skcpoon 	 * After a timeout, we should change the current faddr so that
19051735Skcpoon 	 * new chunks will be sent to the alternate address.
19061735Skcpoon 	 */
19071735Skcpoon 	sctp_set_faddr_current(sctp, fp);
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	nmp = dupmsg(mp);
19100Sstevel@tonic-gate 	if (nmp == NULL)
19110Sstevel@tonic-gate 		goto restart_timer;
19120Sstevel@tonic-gate 	if (extra > 0) {
19134691Skcpoon 		fill = sctp_get_padding(sctp, extra);
19140Sstevel@tonic-gate 		if (fill != NULL) {
19150Sstevel@tonic-gate 			linkb(nmp, fill);
19160Sstevel@tonic-gate 			seglen += extra;
19170Sstevel@tonic-gate 		} else {
19180Sstevel@tonic-gate 			freemsg(nmp);
19190Sstevel@tonic-gate 			goto restart_timer;
19200Sstevel@tonic-gate 		}
19210Sstevel@tonic-gate 	}
19220Sstevel@tonic-gate 	SCTP_CHUNK_CLEAR_FLAGS(nmp);
1923252Svi117747 	head = sctp_add_proto_hdr(sctp, fp, nmp, sacklen, NULL);
19240Sstevel@tonic-gate 	if (head == NULL) {
19250Sstevel@tonic-gate 		freemsg(nmp);
19263448Sdh155122 		SCTP_KSTAT(sctps, sctp_rexmit_failed);
19270Sstevel@tonic-gate 		goto restart_timer;
19280Sstevel@tonic-gate 	}
19290Sstevel@tonic-gate 	seglen += sacklen;
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 	SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	mp = mp->b_next;
19341735Skcpoon 
19350Sstevel@tonic-gate try_bundle:
19363795Skcpoon 	/* We can at least and at most send 1 packet at timeout. */
193713009SChandrasekar.Marimuthu@Sun.COM 	while (seglen < fp->sf_pmss) {
19380Sstevel@tonic-gate 		int32_t new_len;
19390Sstevel@tonic-gate 
19401735Skcpoon 		/* Go through the list to find more chunks to be bundled. */
19410Sstevel@tonic-gate 		while (mp != NULL) {
19421735Skcpoon 			/* Check if the chunk can be bundled. */
19431735Skcpoon 			if (SCTP_CHUNK_RX_CANBUNDLE(mp, oldfp))
19440Sstevel@tonic-gate 				break;
19450Sstevel@tonic-gate 			mp = mp->b_next;
19460Sstevel@tonic-gate 		}
19471735Skcpoon 		/* Go to the next message. */
19480Sstevel@tonic-gate 		if (mp == NULL) {
19491735Skcpoon 			for (meta = meta->b_next; meta != NULL;
19501735Skcpoon 			    meta = meta->b_next) {
19511735Skcpoon 				mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
19521735Skcpoon 
19531735Skcpoon 				if (SCTP_IS_MSG_ABANDONED(meta) ||
19541735Skcpoon 				    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr,
19551735Skcpoon 				    sctp)) {
19561735Skcpoon 					continue;
19571735Skcpoon 				}
19581735Skcpoon 
19591735Skcpoon 				mp = meta->b_cont;
19601735Skcpoon 				goto try_bundle;
19611735Skcpoon 			}
19628154SGeorge.Shepherd@Sun.COM 			/*
19638154SGeorge.Shepherd@Sun.COM 			 * Check if there is a new message which potentially
19648154SGeorge.Shepherd@Sun.COM 			 * could be bundled with this retransmission.
19658154SGeorge.Shepherd@Sun.COM 			 */
19668154SGeorge.Shepherd@Sun.COM 			meta = sctp_get_msg_to_send(sctp, &mp, NULL, &error,
196713009SChandrasekar.Marimuthu@Sun.COM 			    seglen, fp->sf_pmss - seglen, NULL);
19688154SGeorge.Shepherd@Sun.COM 			if (error != 0 || meta == NULL) {
19698154SGeorge.Shepherd@Sun.COM 				/* No more chunk to be bundled. */
19708154SGeorge.Shepherd@Sun.COM 				break;
19718154SGeorge.Shepherd@Sun.COM 			} else {
19728154SGeorge.Shepherd@Sun.COM 				goto try_bundle;
19738154SGeorge.Shepherd@Sun.COM 			}
19740Sstevel@tonic-gate 		}
19751735Skcpoon 
19760Sstevel@tonic-gate 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
19771735Skcpoon 		new_len = ntohs(sdc->sdh_len);
19781735Skcpoon 		chunklen = new_len - sizeof (*sdc);
19790Sstevel@tonic-gate 
19801735Skcpoon 		if ((extra = new_len & (SCTP_ALIGN - 1)) != 0)
19811735Skcpoon 			extra = SCTP_ALIGN - extra;
198213009SChandrasekar.Marimuthu@Sun.COM 		if ((new_len = seglen + new_len + extra) > fp->sf_pmss)
19831735Skcpoon 			break;
19841735Skcpoon 		if ((nmp = dupmsg(mp)) == NULL)
19851735Skcpoon 			break;
19860Sstevel@tonic-gate 
19871735Skcpoon 		if (extra > 0) {
19884691Skcpoon 			fill = sctp_get_padding(sctp, extra);
19890Sstevel@tonic-gate 			if (fill != NULL) {
19901735Skcpoon 				linkb(nmp, fill);
19910Sstevel@tonic-gate 			} else {
19921735Skcpoon 				freemsg(nmp);
19930Sstevel@tonic-gate 				break;
19940Sstevel@tonic-gate 			}
19950Sstevel@tonic-gate 		}
19961735Skcpoon 		linkb(head, nmp);
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate 		SCTP_CHUNK_CLEAR_FLAGS(nmp);
19990Sstevel@tonic-gate 		SCTP_CHUNK_SENT(sctp, mp, sdc, fp, chunklen, meta);
20001735Skcpoon 
20011735Skcpoon 		seglen = new_len;
20020Sstevel@tonic-gate 		mp = mp->b_next;
20030Sstevel@tonic-gate 	}
20041735Skcpoon done_bundle:
200513009SChandrasekar.Marimuthu@Sun.COM 	if ((seglen > fp->sf_pmss) && fp->sf_isv4) {
20060Sstevel@tonic-gate 		ipha_t *iph = (ipha_t *)head->b_rptr;
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate 		/*
20090Sstevel@tonic-gate 		 * Path MTU is different from path we thought it would
20100Sstevel@tonic-gate 		 * be when we created chunks, or IP headers have grown.
20110Sstevel@tonic-gate 		 * Need to clear the DF bit.
20120Sstevel@tonic-gate 		 */
20130Sstevel@tonic-gate 		iph->ipha_fragment_offset_and_flags = 0;
20140Sstevel@tonic-gate 	}
201513009SChandrasekar.Marimuthu@Sun.COM 	fp->sf_rxt_unacked += seglen;
20163795Skcpoon 
20170Sstevel@tonic-gate 	dprint(2, ("sctp_rexmit: Sending packet %d bytes, tsn %x "
20180Sstevel@tonic-gate 	    "ssn %d to %p (rwnd %d, lastack_rxd %x)\n",
20191676Sjpk 	    seglen, ntohl(sdc->sdh_tsn), ntohs(sdc->sdh_ssn),
20201676Sjpk 	    (void *)fp, sctp->sctp_frwnd, sctp->sctp_lastack_rxd));
20210Sstevel@tonic-gate 
20221735Skcpoon 	sctp->sctp_rexmitting = B_TRUE;
20231735Skcpoon 	sctp->sctp_rxt_nxttsn = first_ua_tsn;
20241735Skcpoon 	sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn - 1;
202513009SChandrasekar.Marimuthu@Sun.COM 	sctp_set_iplen(sctp, head, fp->sf_ixa);
202613009SChandrasekar.Marimuthu@Sun.COM 	(void) conn_ip_output(head, fp->sf_ixa);
202711042SErik.Nordmark@Sun.COM 	BUMP_LOCAL(sctp->sctp_opkts);
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 	/*
20301735Skcpoon 	 * Restart the oldfp timer with exponential backoff and
20311735Skcpoon 	 * the new fp timer for the retransmitted chunks.
20320Sstevel@tonic-gate 	 */
20330Sstevel@tonic-gate restart_timer:
203413009SChandrasekar.Marimuthu@Sun.COM 	oldfp->sf_strikes++;
20350Sstevel@tonic-gate 	sctp->sctp_strikes++;
203612474SGeorge.Shepherd@Sun.COM 	SCTP_CALC_RXT(sctp, oldfp, sctp->sctp_rto_max);
20374818Skcpoon 	/*
20384818Skcpoon 	 * If there is still some data in the oldfp, restart the
20394818Skcpoon 	 * retransmission timer.  If there is no data, the heartbeat will
20404818Skcpoon 	 * continue to run so it will do its job in checking the reachability
20414818Skcpoon 	 * of the oldfp.
20424818Skcpoon 	 */
204313009SChandrasekar.Marimuthu@Sun.COM 	if (oldfp != fp && oldfp->sf_suna != 0)
204413009SChandrasekar.Marimuthu@Sun.COM 		SCTP_FADDR_TIMER_RESTART(sctp, oldfp, oldfp->sf_rto);
20451735Skcpoon 
20461735Skcpoon 	/*
20471735Skcpoon 	 * Should we restart the timer of the new fp?  If there is
20481735Skcpoon 	 * outstanding data to the new fp, the timer should be
20491735Skcpoon 	 * running already.  So restarting it means that the timer
20501735Skcpoon 	 * will fire later for those outstanding data.  But if
20511735Skcpoon 	 * we don't restart it, the timer will fire too early for the
20521735Skcpoon 	 * just retransmitted chunks to the new fp.  The reason is that we
20531735Skcpoon 	 * don't keep a timestamp on when a chunk is retransmitted.
20541735Skcpoon 	 * So when the timer fires, it will just search for the
20551735Skcpoon 	 * chunk with the earliest TSN sent to new fp.  This probably
20561735Skcpoon 	 * is the chunk we just retransmitted.  So for now, let's
20571735Skcpoon 	 * be conservative and restart the timer of the new fp.
20581735Skcpoon 	 */
205913009SChandrasekar.Marimuthu@Sun.COM 	SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->sf_rto);
20604818Skcpoon 
206111066Srafael.vanoni@sun.com 	sctp->sctp_active = ddi_get_lbolt64();
20620Sstevel@tonic-gate }
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate /*
20651735Skcpoon  * This function is called by sctp_ss_rexmit() to create a packet
20661735Skcpoon  * to be retransmitted to the given fp.  The given meta and mp
20671735Skcpoon  * parameters are respectively the sctp_msg_hdr_t and the mblk of the
20683795Skcpoon  * first chunk to be retransmitted.  This is also called when we want
20691932Svi117747  * to retransmit a zero window probe from sctp_rexmit() or when we
20701932Svi117747  * want to retransmit the zero window probe after the window has
20711932Svi117747  * opened from sctp_got_sack().
20721735Skcpoon  */
20731932Svi117747 mblk_t *
sctp_rexmit_packet(sctp_t * sctp,mblk_t ** meta,mblk_t ** mp,sctp_faddr_t * fp,uint_t * packet_len)20741735Skcpoon sctp_rexmit_packet(sctp_t *sctp, mblk_t **meta, mblk_t **mp, sctp_faddr_t *fp,
20751735Skcpoon     uint_t *packet_len)
20761735Skcpoon {
20771735Skcpoon 	uint32_t	seglen = 0;
20781735Skcpoon 	uint16_t	chunklen;
20791735Skcpoon 	int		extra;
20801735Skcpoon 	mblk_t		*nmp;
20811735Skcpoon 	mblk_t		*head;
20821735Skcpoon 	mblk_t		*fill;
20831735Skcpoon 	sctp_data_hdr_t	*sdc;
20841735Skcpoon 	sctp_msg_hdr_t	*mhdr;
20851735Skcpoon 
20861735Skcpoon 	sdc = (sctp_data_hdr_t *)(*mp)->b_rptr;
20871735Skcpoon 	seglen = ntohs(sdc->sdh_len);
20881735Skcpoon 	chunklen = seglen - sizeof (*sdc);
20891735Skcpoon 	if ((extra = seglen & (SCTP_ALIGN - 1)) != 0)
20901735Skcpoon 		extra = SCTP_ALIGN - extra;
20911735Skcpoon 
20921735Skcpoon 	nmp = dupmsg(*mp);
20931735Skcpoon 	if (nmp == NULL)
20941735Skcpoon 		return (NULL);
20951735Skcpoon 	if (extra > 0) {
20964691Skcpoon 		fill = sctp_get_padding(sctp, extra);
20971735Skcpoon 		if (fill != NULL) {
20981735Skcpoon 			linkb(nmp, fill);
20991735Skcpoon 			seglen += extra;
21001735Skcpoon 		} else {
21011735Skcpoon 			freemsg(nmp);
21021735Skcpoon 			return (NULL);
21031735Skcpoon 		}
21041735Skcpoon 	}
21051735Skcpoon 	SCTP_CHUNK_CLEAR_FLAGS(nmp);
21061735Skcpoon 	head = sctp_add_proto_hdr(sctp, fp, nmp, 0, NULL);
21071735Skcpoon 	if (head == NULL) {
21081735Skcpoon 		freemsg(nmp);
21091735Skcpoon 		return (NULL);
21101735Skcpoon 	}
21111735Skcpoon 	SCTP_CHUNK_SENT(sctp, *mp, sdc, fp, chunklen, *meta);
21121932Svi117747 	/*
21131932Svi117747 	 * Don't update the TSN if we are doing a Zero Win Probe.
21141932Svi117747 	 */
21151932Svi117747 	if (!sctp->sctp_zero_win_probe)
21161932Svi117747 		sctp->sctp_rxt_nxttsn = ntohl(sdc->sdh_tsn);
21171735Skcpoon 	*mp = (*mp)->b_next;
21181735Skcpoon 
21191735Skcpoon try_bundle:
212013009SChandrasekar.Marimuthu@Sun.COM 	while (seglen < fp->sf_pmss) {
21211735Skcpoon 		int32_t new_len;
21221735Skcpoon 
21231735Skcpoon 		/*
21241735Skcpoon 		 * Go through the list to find more chunks to be bundled.
21251735Skcpoon 		 * We should only retransmit sent by unack'ed chunks.  Since
21261735Skcpoon 		 * they were sent before, the peer's receive window should
21271735Skcpoon 		 * be able to receive them.
21281735Skcpoon 		 */
21291735Skcpoon 		while (*mp != NULL) {
21301735Skcpoon 			/* Check if the chunk can be bundled. */
21311735Skcpoon 			if (SCTP_CHUNK_ISSENT(*mp) && !SCTP_CHUNK_ISACKED(*mp))
21321735Skcpoon 				break;
21331735Skcpoon 			*mp = (*mp)->b_next;
21341735Skcpoon 		}
21351735Skcpoon 		/* Go to the next message. */
21361735Skcpoon 		if (*mp == NULL) {
21371735Skcpoon 			for (*meta = (*meta)->b_next; *meta != NULL;
21381735Skcpoon 			    *meta = (*meta)->b_next) {
21391735Skcpoon 				mhdr = (sctp_msg_hdr_t *)(*meta)->b_rptr;
21401735Skcpoon 
21411735Skcpoon 				if (SCTP_IS_MSG_ABANDONED(*meta) ||
21421735Skcpoon 				    SCTP_MSG_TO_BE_ABANDONED(*meta, mhdr,
21431735Skcpoon 				    sctp)) {
21441735Skcpoon 					continue;
21451735Skcpoon 				}
21461735Skcpoon 
21471735Skcpoon 				*mp = (*meta)->b_cont;
21481735Skcpoon 				goto try_bundle;
21491735Skcpoon 			}
21501735Skcpoon 			/* No more chunk to be bundled. */
21511735Skcpoon 			break;
21521735Skcpoon 		}
21531735Skcpoon 
21541735Skcpoon 		sdc = (sctp_data_hdr_t *)(*mp)->b_rptr;
21551735Skcpoon 		/* Don't bundle chunks beyond sctp_rxt_maxtsn. */
21561735Skcpoon 		if (SEQ_GT(ntohl(sdc->sdh_tsn), sctp->sctp_rxt_maxtsn))
21571735Skcpoon 			break;
21581735Skcpoon 		new_len = ntohs(sdc->sdh_len);
21591735Skcpoon 		chunklen = new_len - sizeof (*sdc);
21601735Skcpoon 
21611735Skcpoon 		if ((extra = new_len & (SCTP_ALIGN - 1)) != 0)
21621735Skcpoon 			extra = SCTP_ALIGN - extra;
216313009SChandrasekar.Marimuthu@Sun.COM 		if ((new_len = seglen + new_len + extra) > fp->sf_pmss)
21641735Skcpoon 			break;
21651735Skcpoon 		if ((nmp = dupmsg(*mp)) == NULL)
21661735Skcpoon 			break;
21671735Skcpoon 
21681735Skcpoon 		if (extra > 0) {
21694691Skcpoon 			fill = sctp_get_padding(sctp, extra);
21701735Skcpoon 			if (fill != NULL) {
21711735Skcpoon 				linkb(nmp, fill);
21721735Skcpoon 			} else {
21731735Skcpoon 				freemsg(nmp);
21741735Skcpoon 				break;
21751735Skcpoon 			}
21761735Skcpoon 		}
21771735Skcpoon 		linkb(head, nmp);
21781735Skcpoon 
21791735Skcpoon 		SCTP_CHUNK_CLEAR_FLAGS(nmp);
21801735Skcpoon 		SCTP_CHUNK_SENT(sctp, *mp, sdc, fp, chunklen, *meta);
21811932Svi117747 		/*
21821932Svi117747 		 * Don't update the TSN if we are doing a Zero Win Probe.
21831932Svi117747 		 */
21841932Svi117747 		if (!sctp->sctp_zero_win_probe)
21851932Svi117747 			sctp->sctp_rxt_nxttsn = ntohl(sdc->sdh_tsn);
21861735Skcpoon 
21871735Skcpoon 		seglen = new_len;
21881735Skcpoon 		*mp = (*mp)->b_next;
21891735Skcpoon 	}
21901735Skcpoon 	*packet_len = seglen;
219113009SChandrasekar.Marimuthu@Sun.COM 	fp->sf_rxt_unacked += seglen;
21921735Skcpoon 	return (head);
21931735Skcpoon }
21941735Skcpoon 
21951735Skcpoon /*
21961735Skcpoon  * sctp_ss_rexmit() is called when we get a SACK after a timeout which
21971735Skcpoon  * advances the cum_tsn but the cum_tsn is still less than what we have sent
21981735Skcpoon  * (sctp_rxt_maxtsn) at the time of the timeout.  This SACK is a "partial"
21991735Skcpoon  * SACK.  We retransmit unacked chunks without having to wait for another
22001735Skcpoon  * timeout.  The rationale is that the SACK should not be "partial" if all the
22011735Skcpoon  * lost chunks have been retransmitted.  Since the SACK is "partial,"
22021735Skcpoon  * the chunks between the cum_tsn and the sctp_rxt_maxtsn should still
22031735Skcpoon  * be missing.  It is better for us to retransmit them now instead
22041735Skcpoon  * of waiting for a timeout.
22051735Skcpoon  */
22061735Skcpoon void
sctp_ss_rexmit(sctp_t * sctp)22071735Skcpoon sctp_ss_rexmit(sctp_t *sctp)
22081735Skcpoon {
22091735Skcpoon 	mblk_t		*meta;
22101735Skcpoon 	mblk_t		*mp;
22111735Skcpoon 	mblk_t		*pkt;
22121735Skcpoon 	sctp_faddr_t	*fp;
22131735Skcpoon 	uint_t		pkt_len;
22141735Skcpoon 	uint32_t	tot_wnd;
22151735Skcpoon 	sctp_data_hdr_t	*sdc;
22161735Skcpoon 	int		burst;
22173448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
22181735Skcpoon 
22191932Svi117747 	ASSERT(!sctp->sctp_zero_win_probe);
22201932Svi117747 
22211735Skcpoon 	/*
22221735Skcpoon 	 * If the last cum ack is smaller than what we have just
22231735Skcpoon 	 * retransmitted, simply return.
22241735Skcpoon 	 */
22251735Skcpoon 	if (SEQ_GEQ(sctp->sctp_lastack_rxd, sctp->sctp_rxt_nxttsn))
22261735Skcpoon 		sctp->sctp_rxt_nxttsn = sctp->sctp_lastack_rxd + 1;
22271735Skcpoon 	else
22281735Skcpoon 		return;
22291735Skcpoon 	ASSERT(SEQ_LEQ(sctp->sctp_rxt_nxttsn, sctp->sctp_rxt_maxtsn));
22301735Skcpoon 
22311735Skcpoon 	/*
22321735Skcpoon 	 * After a timer fires, sctp_current should be set to the new
22331735Skcpoon 	 * fp where the retransmitted chunks are sent.
22341735Skcpoon 	 */
22351735Skcpoon 	fp = sctp->sctp_current;
22361735Skcpoon 
22371735Skcpoon 	/*
22383795Skcpoon 	 * Since we are retransmitting, we only need to use cwnd to determine
22393795Skcpoon 	 * how much we can send as we were allowed (by peer's receive window)
22403795Skcpoon 	 * to send those retransmitted chunks previously when they are first
22413795Skcpoon 	 * sent.  If we record how much we have retransmitted but
22423795Skcpoon 	 * unacknowledged using rxt_unacked, then the amount we can now send
22433795Skcpoon 	 * is equal to cwnd minus rxt_unacked.
22443795Skcpoon 	 *
22453795Skcpoon 	 * The field rxt_unacked is incremented when we retransmit a packet
22463795Skcpoon 	 * and decremented when we got a SACK acknowledging something.  And
22473795Skcpoon 	 * it is reset when the retransmission timer fires as we assume that
22483795Skcpoon 	 * all packets have left the network after a timeout.  If this
22493795Skcpoon 	 * assumption is not true, it means that after a timeout, we can
22503795Skcpoon 	 * get a SACK acknowledging more than rxt_unacked (its value only
22513795Skcpoon 	 * contains what is retransmitted when the timer fires).  So
22523795Skcpoon 	 * rxt_unacked will become very big (it is an unsiged int so going
22533795Skcpoon 	 * negative means that the value is huge).  This is the reason we
22543795Skcpoon 	 * always send at least 1 MSS bytes.
22553795Skcpoon 	 *
22563795Skcpoon 	 * The reason why we do not have an accurate count is that we
22573795Skcpoon 	 * only know how many packets are outstanding (using the TSN numbers).
22583795Skcpoon 	 * But we do not know how many bytes those packets contain.  To
22593795Skcpoon 	 * have an accurate count, we need to walk through the send list.
22603795Skcpoon 	 * As it is not really important to have an accurate count during
22613795Skcpoon 	 * retransmission, we skip this walk to save some time.  This should
22623795Skcpoon 	 * not make the retransmission too aggressive to cause congestion.
22631735Skcpoon 	 */
226413009SChandrasekar.Marimuthu@Sun.COM 	if (fp->sf_cwnd <= fp->sf_rxt_unacked)
226513009SChandrasekar.Marimuthu@Sun.COM 		tot_wnd = fp->sf_pmss;
22661735Skcpoon 	else
226713009SChandrasekar.Marimuthu@Sun.COM 		tot_wnd = fp->sf_cwnd - fp->sf_rxt_unacked;
22681735Skcpoon 
22691735Skcpoon 	/* Find the first unack'ed chunk */
22701735Skcpoon 	for (meta = sctp->sctp_xmit_head; meta != NULL; meta = meta->b_next) {
22711735Skcpoon 		sctp_msg_hdr_t	*mhdr = (sctp_msg_hdr_t *)meta->b_rptr;
22721735Skcpoon 
22731735Skcpoon 		if (SCTP_IS_MSG_ABANDONED(meta) ||
22741735Skcpoon 		    SCTP_MSG_TO_BE_ABANDONED(meta, mhdr, sctp)) {
22751735Skcpoon 			continue;
22761735Skcpoon 		}
22771735Skcpoon 
22781735Skcpoon 		for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
22791735Skcpoon 			/* Again, this may not be possible */
22801735Skcpoon 			if (!SCTP_CHUNK_ISSENT(mp))
22811735Skcpoon 				return;
22821735Skcpoon 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
22831735Skcpoon 			if (ntohl(sdc->sdh_tsn) == sctp->sctp_rxt_nxttsn)
22841735Skcpoon 				goto found_msg;
22851735Skcpoon 		}
22861735Skcpoon 	}
22871735Skcpoon 
22881735Skcpoon 	/* Everything is abandoned... */
22891735Skcpoon 	return;
22901735Skcpoon 
22911735Skcpoon found_msg:
229213009SChandrasekar.Marimuthu@Sun.COM 	if (!fp->sf_timer_running)
229313009SChandrasekar.Marimuthu@Sun.COM 		SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->sf_rto);
22941735Skcpoon 	pkt = sctp_rexmit_packet(sctp, &meta, &mp, fp, &pkt_len);
22951735Skcpoon 	if (pkt == NULL) {
22963448Sdh155122 		SCTP_KSTAT(sctps, sctp_ss_rexmit_failed);
22971735Skcpoon 		return;
22981735Skcpoon 	}
229913009SChandrasekar.Marimuthu@Sun.COM 	if ((pkt_len > fp->sf_pmss) && fp->sf_isv4) {
23001735Skcpoon 		ipha_t	*iph = (ipha_t *)pkt->b_rptr;
23011735Skcpoon 
23021735Skcpoon 		/*
23031735Skcpoon 		 * Path MTU is different from path we thought it would
23041735Skcpoon 		 * be when we created chunks, or IP headers have grown.
23051735Skcpoon 		 *  Need to clear the DF bit.
23061735Skcpoon 		 */
23071735Skcpoon 		iph->ipha_fragment_offset_and_flags = 0;
23081735Skcpoon 	}
230913009SChandrasekar.Marimuthu@Sun.COM 	sctp_set_iplen(sctp, pkt, fp->sf_ixa);
231013009SChandrasekar.Marimuthu@Sun.COM 	(void) conn_ip_output(pkt, fp->sf_ixa);
231111042SErik.Nordmark@Sun.COM 	BUMP_LOCAL(sctp->sctp_opkts);
23121735Skcpoon 
23131735Skcpoon 	/* Check and see if there is more chunk to be retransmitted. */
231413009SChandrasekar.Marimuthu@Sun.COM 	if (tot_wnd <= pkt_len || tot_wnd - pkt_len < fp->sf_pmss ||
23151735Skcpoon 	    meta == NULL)
23161735Skcpoon 		return;
23171735Skcpoon 	if (mp == NULL)
23181735Skcpoon 		meta = meta->b_next;
23191735Skcpoon 	if (meta == NULL)
23201735Skcpoon 		return;
23211735Skcpoon 
23221735Skcpoon 	/* Retransmit another packet if the window allows. */
23233448Sdh155122 	for (tot_wnd -= pkt_len, burst = sctps->sctps_maxburst - 1;
23241735Skcpoon 	    meta != NULL && burst > 0; meta = meta->b_next, burst--) {
23251735Skcpoon 		if (mp == NULL)
23261735Skcpoon 			mp = meta->b_cont;
23271735Skcpoon 		for (; mp != NULL; mp = mp->b_next) {
23281735Skcpoon 			/* Again, this may not be possible */
23291735Skcpoon 			if (!SCTP_CHUNK_ISSENT(mp))
23301735Skcpoon 				return;
23311735Skcpoon 			if (!SCTP_CHUNK_ISACKED(mp))
23321735Skcpoon 				goto found_msg;
23331735Skcpoon 		}
23341735Skcpoon 	}
23351735Skcpoon }
2336