xref: /onnv-gate/usr/src/uts/common/inet/sctp/sctp_input.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 /*
2312604SGeorge.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 #include <sys/kmem.h>
310Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
320Sstevel@tonic-gate #include <sys/tihdr.h>
330Sstevel@tonic-gate #include <sys/socket.h>
340Sstevel@tonic-gate #include <sys/strsun.h>
350Sstevel@tonic-gate #include <sys/strsubr.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <netinet/in.h>
380Sstevel@tonic-gate #include <netinet/ip6.h>
390Sstevel@tonic-gate #include <netinet/tcp_seq.h>
400Sstevel@tonic-gate #include <netinet/sctp.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <inet/common.h>
430Sstevel@tonic-gate #include <inet/ip.h>
4411042SErik.Nordmark@Sun.COM #include <inet/ip_if.h>
450Sstevel@tonic-gate #include <inet/ip6.h>
460Sstevel@tonic-gate #include <inet/mib2.h>
470Sstevel@tonic-gate #include <inet/ipclassifier.h>
480Sstevel@tonic-gate #include <inet/ipp_common.h>
490Sstevel@tonic-gate #include <inet/ipsec_impl.h>
500Sstevel@tonic-gate #include <inet/sctp_ip.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include "sctp_impl.h"
530Sstevel@tonic-gate #include "sctp_asconf.h"
540Sstevel@tonic-gate #include "sctp_addr.h"
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static struct kmem_cache *sctp_kmem_set_cache;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * PR-SCTP comments.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * When we get a valid Forward TSN chunk, we check the fragment list for this
620Sstevel@tonic-gate  * SSN and preceeding SSNs free all them. Further, if this Forward TSN causes
630Sstevel@tonic-gate  * the next expected SSN to be present in the stream queue, we deliver any
640Sstevel@tonic-gate  * such stranded messages upstream. We also update the SACK info. appropriately.
650Sstevel@tonic-gate  * When checking for advancing the cumulative ack (in sctp_cumack()) we must
660Sstevel@tonic-gate  * check for abandoned chunks and messages. While traversing the tramsmit
670Sstevel@tonic-gate  * list if we come across an abandoned chunk, we can skip the message (i.e.
680Sstevel@tonic-gate  * take it out of the (re)transmit list) since this message, and hence this
690Sstevel@tonic-gate  * chunk, has been marked abandoned by sctp_rexmit(). If we come across an
700Sstevel@tonic-gate  * unsent chunk for a message this now abandoned we need to check if a
710Sstevel@tonic-gate  * Forward TSN needs to be sent, this could be a case where we deferred sending
720Sstevel@tonic-gate  * a Forward TSN in sctp_get_msg_to_send(). Further, after processing a
730Sstevel@tonic-gate  * SACK we check if the Advanced peer ack point can be moved ahead, i.e.
740Sstevel@tonic-gate  * if we can send a Forward TSN via sctp_check_abandoned_data().
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate void
sctp_free_set(sctp_set_t * s)770Sstevel@tonic-gate sctp_free_set(sctp_set_t *s)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	sctp_set_t *p;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	while (s) {
820Sstevel@tonic-gate 		p = s->next;
830Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_set_cache, s);
840Sstevel@tonic-gate 		s = p;
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static void
sctp_ack_add(sctp_set_t ** head,uint32_t tsn,int * num)890Sstevel@tonic-gate sctp_ack_add(sctp_set_t **head, uint32_t tsn, int *num)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	sctp_set_t *p, *t;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	if (head == NULL || num == NULL)
940Sstevel@tonic-gate 		return;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	ASSERT(*num >= 0);
970Sstevel@tonic-gate 	ASSERT((*num == 0 && *head == NULL) || (*num > 0 && *head != NULL));
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	if (*head == NULL) {
1000Sstevel@tonic-gate 		*head = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP);
1010Sstevel@tonic-gate 		if (*head == NULL)
1020Sstevel@tonic-gate 			return;
1030Sstevel@tonic-gate 		(*head)->prev = (*head)->next = NULL;
1040Sstevel@tonic-gate 		(*head)->begin = tsn;
1050Sstevel@tonic-gate 		(*head)->end = tsn;
1060Sstevel@tonic-gate 		*num = 1;
1070Sstevel@tonic-gate 		return;
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	ASSERT((*head)->prev == NULL);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	/*
1130Sstevel@tonic-gate 	 * Handle this special case here so we don't have to check
1140Sstevel@tonic-gate 	 * for it each time in the loop.
1150Sstevel@tonic-gate 	 */
1160Sstevel@tonic-gate 	if (SEQ_LT(tsn + 1, (*head)->begin)) {
1170Sstevel@tonic-gate 		/* add a new set, and move the head pointer */
1180Sstevel@tonic-gate 		t = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP);
1190Sstevel@tonic-gate 		if (t == NULL)
1200Sstevel@tonic-gate 			return;
1210Sstevel@tonic-gate 		t->next = *head;
1220Sstevel@tonic-gate 		t->prev = NULL;
1230Sstevel@tonic-gate 		(*head)->prev = t;
1240Sstevel@tonic-gate 		t->begin = tsn;
1250Sstevel@tonic-gate 		t->end = tsn;
1260Sstevel@tonic-gate 		(*num)++;
1270Sstevel@tonic-gate 		*head = t;
1280Sstevel@tonic-gate 		return;
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	/*
1320Sstevel@tonic-gate 	 * We need to handle the following cases, where p points to
1330Sstevel@tonic-gate 	 * the current set (as we walk through the loop):
1340Sstevel@tonic-gate 	 *
1350Sstevel@tonic-gate 	 * 1. tsn is entirely less than p; create a new set before p.
1360Sstevel@tonic-gate 	 * 2. tsn borders p from less; coalesce p with tsn.
1370Sstevel@tonic-gate 	 * 3. tsn is withing p; do nothing.
1380Sstevel@tonic-gate 	 * 4. tsn borders p from greater; coalesce p with tsn.
1390Sstevel@tonic-gate 	 * 4a. p may now border p->next from less; if so, coalesce those
1400Sstevel@tonic-gate 	 *    two sets.
1410Sstevel@tonic-gate 	 * 5. tsn is entirely greater then all sets; add a new set at
1420Sstevel@tonic-gate 	 *    the end.
1430Sstevel@tonic-gate 	 */
1440Sstevel@tonic-gate 	for (p = *head; ; p = p->next) {
1450Sstevel@tonic-gate 		if (SEQ_LT(tsn + 1, p->begin)) {
1460Sstevel@tonic-gate 			/* 1: add a new set before p. */
1470Sstevel@tonic-gate 			t = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP);
1480Sstevel@tonic-gate 			if (t == NULL)
1490Sstevel@tonic-gate 				return;
1500Sstevel@tonic-gate 			t->next = p;
1510Sstevel@tonic-gate 			t->prev = NULL;
1520Sstevel@tonic-gate 			t->begin = tsn;
1530Sstevel@tonic-gate 			t->end = tsn;
1540Sstevel@tonic-gate 			if (p->prev) {
1550Sstevel@tonic-gate 				t->prev = p->prev;
1560Sstevel@tonic-gate 				p->prev->next = t;
1570Sstevel@tonic-gate 			}
1580Sstevel@tonic-gate 			p->prev = t;
1590Sstevel@tonic-gate 			(*num)++;
1600Sstevel@tonic-gate 			return;
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		if ((tsn + 1) == p->begin) {
1640Sstevel@tonic-gate 			/* 2: adjust p->begin */
1650Sstevel@tonic-gate 			p->begin = tsn;
1660Sstevel@tonic-gate 			return;
1670Sstevel@tonic-gate 		}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 		if (SEQ_GEQ(tsn, p->begin) && SEQ_LEQ(tsn, p->end)) {
1700Sstevel@tonic-gate 			/* 3; do nothing */
1710Sstevel@tonic-gate 			return;
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 		if ((p->end + 1) == tsn) {
1750Sstevel@tonic-gate 			/* 4; adjust p->end */
1760Sstevel@tonic-gate 			p->end = tsn;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 			if (p->next != NULL && (tsn + 1) == p->next->begin) {
1790Sstevel@tonic-gate 				/* 4a: coalesce p and p->next */
1800Sstevel@tonic-gate 				t = p->next;
1810Sstevel@tonic-gate 				p->end = t->end;
1820Sstevel@tonic-gate 				p->next = t->next;
1830Sstevel@tonic-gate 				if (t->next != NULL)
1840Sstevel@tonic-gate 					t->next->prev = p;
1850Sstevel@tonic-gate 				kmem_cache_free(sctp_kmem_set_cache, t);
1860Sstevel@tonic-gate 				(*num)--;
1870Sstevel@tonic-gate 			}
1880Sstevel@tonic-gate 			return;
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 		if (p->next == NULL) {
1920Sstevel@tonic-gate 			/* 5: add new set at the end */
1930Sstevel@tonic-gate 			t = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP);
1940Sstevel@tonic-gate 			if (t == NULL)
1950Sstevel@tonic-gate 				return;
1960Sstevel@tonic-gate 			t->next = NULL;
1970Sstevel@tonic-gate 			t->prev = p;
1980Sstevel@tonic-gate 			t->begin = tsn;
1990Sstevel@tonic-gate 			t->end = tsn;
2000Sstevel@tonic-gate 			p->next = t;
2010Sstevel@tonic-gate 			(*num)++;
2020Sstevel@tonic-gate 			return;
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		if (SEQ_GT(tsn, p->end + 1))
2060Sstevel@tonic-gate 			continue;
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate static void
sctp_ack_rem(sctp_set_t ** head,uint32_t end,int * num)2110Sstevel@tonic-gate sctp_ack_rem(sctp_set_t **head, uint32_t end, int *num)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate 	sctp_set_t *p, *t;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	if (head == NULL || *head == NULL || num == NULL)
2160Sstevel@tonic-gate 		return;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/* Nothing to remove */
2190Sstevel@tonic-gate 	if (SEQ_LT(end, (*head)->begin))
2200Sstevel@tonic-gate 		return;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	/* Find out where to start removing sets */
2230Sstevel@tonic-gate 	for (p = *head; p->next; p = p->next) {
2240Sstevel@tonic-gate 		if (SEQ_LEQ(end, p->end))
2250Sstevel@tonic-gate 			break;
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (SEQ_LT(end, p->end) && SEQ_GEQ(end, p->begin)) {
2290Sstevel@tonic-gate 		/* adjust p */
2300Sstevel@tonic-gate 		p->begin = end + 1;
2310Sstevel@tonic-gate 		/* all done */
2320Sstevel@tonic-gate 		if (p == *head)
2330Sstevel@tonic-gate 			return;
2340Sstevel@tonic-gate 	} else if (SEQ_GEQ(end, p->end)) {
2350Sstevel@tonic-gate 		/* remove this set too */
2360Sstevel@tonic-gate 		p = p->next;
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/* unlink everything before this set */
2400Sstevel@tonic-gate 	t = *head;
2410Sstevel@tonic-gate 	*head = p;
2420Sstevel@tonic-gate 	if (p != NULL && p->prev != NULL) {
2430Sstevel@tonic-gate 		p->prev->next = NULL;
2440Sstevel@tonic-gate 		p->prev = NULL;
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	sctp_free_set(t);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	/* recount the number of sets */
2500Sstevel@tonic-gate 	*num = 0;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	for (p = *head; p != NULL; p = p->next)
2530Sstevel@tonic-gate 		(*num)++;
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate void
sctp_sets_init()2570Sstevel@tonic-gate sctp_sets_init()
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate 	sctp_kmem_set_cache = kmem_cache_create("sctp_set_cache",
2600Sstevel@tonic-gate 	    sizeof (sctp_set_t), 0, NULL, NULL, NULL, NULL,
2610Sstevel@tonic-gate 	    NULL, 0);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate void
sctp_sets_fini()2650Sstevel@tonic-gate sctp_sets_fini()
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_set_cache);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate sctp_chunk_hdr_t *
sctp_first_chunk(uchar_t * rptr,ssize_t remaining)2710Sstevel@tonic-gate sctp_first_chunk(uchar_t *rptr, ssize_t remaining)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	sctp_chunk_hdr_t *ch;
2740Sstevel@tonic-gate 	uint16_t ch_len;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	if (remaining < sizeof (*ch)) {
2770Sstevel@tonic-gate 		return (NULL);
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	ch = (sctp_chunk_hdr_t *)rptr;
2810Sstevel@tonic-gate 	ch_len = ntohs(ch->sch_len);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	if (ch_len < sizeof (*ch) || remaining < ch_len) {
2840Sstevel@tonic-gate 		return (NULL);
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	return (ch);
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate sctp_chunk_hdr_t *
sctp_next_chunk(sctp_chunk_hdr_t * ch,ssize_t * remaining)2910Sstevel@tonic-gate sctp_next_chunk(sctp_chunk_hdr_t *ch, ssize_t *remaining)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	int pad;
2940Sstevel@tonic-gate 	uint16_t ch_len;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	if (!ch) {
2970Sstevel@tonic-gate 		return (NULL);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	ch_len = ntohs(ch->sch_len);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	if ((pad = ch_len & (SCTP_ALIGN - 1)) != 0) {
3030Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	*remaining -= (ch_len + pad);
3070Sstevel@tonic-gate 	ch = (sctp_chunk_hdr_t *)((char *)ch + ch_len + pad);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	return (sctp_first_chunk((uchar_t *)ch, *remaining));
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * Attach ancillary data to a received SCTP segments.
3140Sstevel@tonic-gate  * If the source address (fp) is not the primary, send up a
3150Sstevel@tonic-gate  * unitdata_ind so recvfrom() can populate the msg_name field.
3160Sstevel@tonic-gate  * If ancillary data is also requested, we append it to the
3170Sstevel@tonic-gate  * unitdata_req. Otherwise, we just send up an optdata_ind.
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate static int
sctp_input_add_ancillary(sctp_t * sctp,mblk_t ** mp,sctp_data_hdr_t * dcp,sctp_faddr_t * fp,ip_pkt_t * ipp,ip_recv_attr_t * ira)3200Sstevel@tonic-gate sctp_input_add_ancillary(sctp_t *sctp, mblk_t **mp, sctp_data_hdr_t *dcp,
32111042SErik.Nordmark@Sun.COM     sctp_faddr_t *fp, ip_pkt_t *ipp, ip_recv_attr_t *ira)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	struct T_unitdata_ind	*tudi;
3240Sstevel@tonic-gate 	int			optlen;
3250Sstevel@tonic-gate 	int			hdrlen;
3260Sstevel@tonic-gate 	uchar_t			*optptr;
3270Sstevel@tonic-gate 	struct cmsghdr		*cmsg;
3280Sstevel@tonic-gate 	mblk_t			*mp1;
3290Sstevel@tonic-gate 	struct sockaddr_in6	sin_buf[1];
3300Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
3310Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
33211042SErik.Nordmark@Sun.COM 	crb_t			 addflag;	/* Which pieces to add */
33311042SErik.Nordmark@Sun.COM 	conn_t			*connp = sctp->sctp_connp;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	sin4 = NULL;
3360Sstevel@tonic-gate 	sin6 = NULL;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	optlen = hdrlen = 0;
33911042SErik.Nordmark@Sun.COM 	addflag.crb_all = 0;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/* Figure out address size */
34211042SErik.Nordmark@Sun.COM 	if (connp->conn_family == AF_INET) {
3430Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)sin_buf;
3440Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
34511042SErik.Nordmark@Sun.COM 		sin4->sin_port = connp->conn_fport;
34613009SChandrasekar.Marimuthu@Sun.COM 		IN6_V4MAPPED_TO_IPADDR(&fp->sf_faddr, sin4->sin_addr.s_addr);
3470Sstevel@tonic-gate 		hdrlen = sizeof (*tudi) + sizeof (*sin4);
3480Sstevel@tonic-gate 	} else {
3490Sstevel@tonic-gate 		sin6 = sin_buf;
3500Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
35111042SErik.Nordmark@Sun.COM 		sin6->sin6_port = connp->conn_fport;
35213009SChandrasekar.Marimuthu@Sun.COM 		sin6->sin6_addr = fp->sf_faddr;
3530Sstevel@tonic-gate 		hdrlen = sizeof (*tudi) + sizeof (*sin6);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 	/* If app asked to receive send / recv info */
35611042SErik.Nordmark@Sun.COM 	if (sctp->sctp_recvsndrcvinfo)
3570Sstevel@tonic-gate 		optlen += sizeof (*cmsg) + sizeof (struct sctp_sndrcvinfo);
35811042SErik.Nordmark@Sun.COM 
35911042SErik.Nordmark@Sun.COM 	if (connp->conn_recv_ancillary.crb_all == 0)
3600Sstevel@tonic-gate 		goto noancillary;
3610Sstevel@tonic-gate 
36211042SErik.Nordmark@Sun.COM 	if (connp->conn_recv_ancillary.crb_ip_recvpktinfo &&
36311042SErik.Nordmark@Sun.COM 	    ira->ira_ruifindex != sctp->sctp_recvifindex) {
3640Sstevel@tonic-gate 		optlen += sizeof (*cmsg) + sizeof (struct in6_pktinfo);
3650Sstevel@tonic-gate 		if (hdrlen == 0)
3660Sstevel@tonic-gate 			hdrlen = sizeof (struct T_unitdata_ind);
36711042SErik.Nordmark@Sun.COM 		addflag.crb_ip_recvpktinfo = 1;
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 	/* If app asked for hoplimit and it has changed ... */
37011042SErik.Nordmark@Sun.COM 	if (connp->conn_recv_ancillary.crb_ipv6_recvhoplimit &&
37111042SErik.Nordmark@Sun.COM 	    ipp->ipp_hoplimit != sctp->sctp_recvhops) {
3720Sstevel@tonic-gate 		optlen += sizeof (*cmsg) + sizeof (uint_t);
3730Sstevel@tonic-gate 		if (hdrlen == 0)
3740Sstevel@tonic-gate 			hdrlen = sizeof (struct T_unitdata_ind);
37511042SErik.Nordmark@Sun.COM 		addflag.crb_ipv6_recvhoplimit = 1;
37611042SErik.Nordmark@Sun.COM 	}
37711042SErik.Nordmark@Sun.COM 	/* If app asked for tclass and it has changed ... */
37811042SErik.Nordmark@Sun.COM 	if (connp->conn_recv_ancillary.crb_ipv6_recvtclass &&
37911042SErik.Nordmark@Sun.COM 	    ipp->ipp_tclass != sctp->sctp_recvtclass) {
38011042SErik.Nordmark@Sun.COM 		optlen += sizeof (struct T_opthdr) + sizeof (uint_t);
38111042SErik.Nordmark@Sun.COM 		if (hdrlen == 0)
38211042SErik.Nordmark@Sun.COM 			hdrlen = sizeof (struct T_unitdata_ind);
38311042SErik.Nordmark@Sun.COM 		addflag.crb_ipv6_recvtclass = 1;
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 	/* If app asked for hopbyhop headers and it has changed ... */
38611042SErik.Nordmark@Sun.COM 	if (connp->conn_recv_ancillary.crb_ipv6_recvhopopts &&
3871676Sjpk 	    ip_cmpbuf(sctp->sctp_hopopts, sctp->sctp_hopoptslen,
3884964Skcpoon 	    (ipp->ipp_fields & IPPF_HOPOPTS),
3894964Skcpoon 	    ipp->ipp_hopopts, ipp->ipp_hopoptslen)) {
3901676Sjpk 		optlen += sizeof (*cmsg) + ipp->ipp_hopoptslen -
3911676Sjpk 		    sctp->sctp_v6label_len;
3920Sstevel@tonic-gate 		if (hdrlen == 0)
3930Sstevel@tonic-gate 			hdrlen = sizeof (struct T_unitdata_ind);
39411042SErik.Nordmark@Sun.COM 		addflag.crb_ipv6_recvhopopts = 1;
3951676Sjpk 		if (!ip_allocbuf((void **)&sctp->sctp_hopopts,
3960Sstevel@tonic-gate 		    &sctp->sctp_hopoptslen,
3970Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_HOPOPTS),
3980Sstevel@tonic-gate 		    ipp->ipp_hopopts, ipp->ipp_hopoptslen))
3990Sstevel@tonic-gate 			return (-1);
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 	/* If app asked for dst headers before routing headers ... */
40211042SErik.Nordmark@Sun.COM 	if (connp->conn_recv_ancillary.crb_ipv6_recvrthdrdstopts &&
40311042SErik.Nordmark@Sun.COM 	    ip_cmpbuf(sctp->sctp_rthdrdstopts, sctp->sctp_rthdrdstoptslen,
40411042SErik.Nordmark@Sun.COM 	    (ipp->ipp_fields & IPPF_RTHDRDSTOPTS),
40511042SErik.Nordmark@Sun.COM 	    ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen)) {
40611042SErik.Nordmark@Sun.COM 		optlen += sizeof (*cmsg) + ipp->ipp_rthdrdstoptslen;
4070Sstevel@tonic-gate 		if (hdrlen == 0)
4080Sstevel@tonic-gate 			hdrlen = sizeof (struct T_unitdata_ind);
40911042SErik.Nordmark@Sun.COM 		addflag.crb_ipv6_recvrthdrdstopts = 1;
41011042SErik.Nordmark@Sun.COM 		if (!ip_allocbuf((void **)&sctp->sctp_rthdrdstopts,
41111042SErik.Nordmark@Sun.COM 		    &sctp->sctp_rthdrdstoptslen,
41211042SErik.Nordmark@Sun.COM 		    (ipp->ipp_fields & IPPF_RTHDRDSTOPTS),
41311042SErik.Nordmark@Sun.COM 		    ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen))
4140Sstevel@tonic-gate 			return (-1);
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 	/* If app asked for routing headers and it has changed ... */
41711042SErik.Nordmark@Sun.COM 	if (connp->conn_recv_ancillary.crb_ipv6_recvrthdr &&
41811042SErik.Nordmark@Sun.COM 	    ip_cmpbuf(sctp->sctp_rthdr, sctp->sctp_rthdrlen,
41911042SErik.Nordmark@Sun.COM 	    (ipp->ipp_fields & IPPF_RTHDR),
42011042SErik.Nordmark@Sun.COM 	    ipp->ipp_rthdr, ipp->ipp_rthdrlen)) {
42111042SErik.Nordmark@Sun.COM 		optlen += sizeof (*cmsg) + ipp->ipp_rthdrlen;
42211042SErik.Nordmark@Sun.COM 		if (hdrlen == 0)
42311042SErik.Nordmark@Sun.COM 			hdrlen = sizeof (struct T_unitdata_ind);
42411042SErik.Nordmark@Sun.COM 		addflag.crb_ipv6_recvrthdr = 1;
42511042SErik.Nordmark@Sun.COM 		if (!ip_allocbuf((void **)&sctp->sctp_rthdr,
42611042SErik.Nordmark@Sun.COM 		    &sctp->sctp_rthdrlen,
4270Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_RTHDR),
42811042SErik.Nordmark@Sun.COM 		    ipp->ipp_rthdr, ipp->ipp_rthdrlen))
42911042SErik.Nordmark@Sun.COM 			return (-1);
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 	/* If app asked for dest headers and it has changed ... */
43211042SErik.Nordmark@Sun.COM 	if (connp->conn_recv_ancillary.crb_ipv6_recvdstopts &&
4331676Sjpk 	    ip_cmpbuf(sctp->sctp_dstopts, sctp->sctp_dstoptslen,
4344964Skcpoon 	    (ipp->ipp_fields & IPPF_DSTOPTS),
4354964Skcpoon 	    ipp->ipp_dstopts, ipp->ipp_dstoptslen)) {
4360Sstevel@tonic-gate 		optlen += sizeof (*cmsg) + ipp->ipp_dstoptslen;
4370Sstevel@tonic-gate 		if (hdrlen == 0)
4380Sstevel@tonic-gate 			hdrlen = sizeof (struct T_unitdata_ind);
43911042SErik.Nordmark@Sun.COM 		addflag.crb_ipv6_recvdstopts = 1;
4401676Sjpk 		if (!ip_allocbuf((void **)&sctp->sctp_dstopts,
4410Sstevel@tonic-gate 		    &sctp->sctp_dstoptslen,
4420Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_DSTOPTS),
4430Sstevel@tonic-gate 		    ipp->ipp_dstopts, ipp->ipp_dstoptslen))
4440Sstevel@tonic-gate 			return (-1);
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate noancillary:
4470Sstevel@tonic-gate 	/* Nothing to add */
4480Sstevel@tonic-gate 	if (hdrlen == 0)
4490Sstevel@tonic-gate 		return (-1);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	mp1 = allocb(hdrlen + optlen + sizeof (void *), BPRI_MED);
4520Sstevel@tonic-gate 	if (mp1 == NULL)
4530Sstevel@tonic-gate 		return (-1);
4540Sstevel@tonic-gate 	mp1->b_cont = *mp;
4550Sstevel@tonic-gate 	*mp = mp1;
4560Sstevel@tonic-gate 	mp1->b_rptr += sizeof (void *);  /* pointer worth of padding */
4570Sstevel@tonic-gate 	mp1->b_wptr = mp1->b_rptr + hdrlen + optlen;
4580Sstevel@tonic-gate 	DB_TYPE(mp1) = M_PROTO;
4590Sstevel@tonic-gate 	tudi = (struct T_unitdata_ind *)mp1->b_rptr;
4600Sstevel@tonic-gate 	tudi->PRIM_type = T_UNITDATA_IND;
4610Sstevel@tonic-gate 	tudi->SRC_length = sin4 ? sizeof (*sin4) : sizeof (*sin6);
4620Sstevel@tonic-gate 	tudi->SRC_offset = sizeof (*tudi);
4630Sstevel@tonic-gate 	tudi->OPT_offset = sizeof (*tudi) + tudi->SRC_length;
4640Sstevel@tonic-gate 	tudi->OPT_length = optlen;
4650Sstevel@tonic-gate 	if (sin4) {
4660Sstevel@tonic-gate 		bcopy(sin4, tudi + 1, sizeof (*sin4));
4670Sstevel@tonic-gate 	} else {
4680Sstevel@tonic-gate 		bcopy(sin6, tudi + 1, sizeof (*sin6));
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 	optptr = (uchar_t *)tudi + tudi->OPT_offset;
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (sctp->sctp_recvsndrcvinfo) {
4730Sstevel@tonic-gate 		/* XXX need backout method if memory allocation fails. */
4740Sstevel@tonic-gate 		struct sctp_sndrcvinfo *sri;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
4770Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_SCTP;
4780Sstevel@tonic-gate 		cmsg->cmsg_type = SCTP_SNDRCV;
4790Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + sizeof (*sri);
4800Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 		sri = (struct sctp_sndrcvinfo *)(cmsg + 1);
4830Sstevel@tonic-gate 		ASSERT(OK_32PTR(sri));
4840Sstevel@tonic-gate 		sri->sinfo_stream = ntohs(dcp->sdh_sid);
4850Sstevel@tonic-gate 		sri->sinfo_ssn = ntohs(dcp->sdh_ssn);
4860Sstevel@tonic-gate 		if (SCTP_DATA_GET_UBIT(dcp)) {
4870Sstevel@tonic-gate 			sri->sinfo_flags = MSG_UNORDERED;
4880Sstevel@tonic-gate 		} else {
4890Sstevel@tonic-gate 			sri->sinfo_flags = 0;
4900Sstevel@tonic-gate 		}
4910Sstevel@tonic-gate 		sri->sinfo_ppid = dcp->sdh_payload_id;
4920Sstevel@tonic-gate 		sri->sinfo_context = 0;
4930Sstevel@tonic-gate 		sri->sinfo_timetolive = 0;
4940Sstevel@tonic-gate 		sri->sinfo_tsn = ntohl(dcp->sdh_tsn);
4950Sstevel@tonic-gate 		sri->sinfo_cumtsn = sctp->sctp_ftsn;
4960Sstevel@tonic-gate 		sri->sinfo_assoc_id = 0;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 		optptr += sizeof (*sri);
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	/*
5020Sstevel@tonic-gate 	 * If app asked for pktinfo and the index has changed ...
5030Sstevel@tonic-gate 	 * Note that the local address never changes for the connection.
5040Sstevel@tonic-gate 	 */
50511042SErik.Nordmark@Sun.COM 	if (addflag.crb_ip_recvpktinfo) {
5060Sstevel@tonic-gate 		struct in6_pktinfo *pkti;
50711042SErik.Nordmark@Sun.COM 		uint_t ifindex;
50811042SErik.Nordmark@Sun.COM 
50911042SErik.Nordmark@Sun.COM 		ifindex = ira->ira_ruifindex;
5100Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
5110Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_IPV6;
5120Sstevel@tonic-gate 		cmsg->cmsg_type = IPV6_PKTINFO;
5130Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + sizeof (*pkti);
5140Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 		pkti = (struct in6_pktinfo *)optptr;
51711042SErik.Nordmark@Sun.COM 		if (connp->conn_family == AF_INET6)
5180Sstevel@tonic-gate 			pkti->ipi6_addr = sctp->sctp_ip6h->ip6_src;
5190Sstevel@tonic-gate 		else
5200Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(sctp->sctp_ipha->ipha_src,
5210Sstevel@tonic-gate 			    &pkti->ipi6_addr);
52211042SErik.Nordmark@Sun.COM 
52311042SErik.Nordmark@Sun.COM 		pkti->ipi6_ifindex = ifindex;
5240Sstevel@tonic-gate 		optptr += sizeof (*pkti);
5250Sstevel@tonic-gate 		ASSERT(OK_32PTR(optptr));
5260Sstevel@tonic-gate 		/* Save as "last" value */
52711042SErik.Nordmark@Sun.COM 		sctp->sctp_recvifindex = ifindex;
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 	/* If app asked for hoplimit and it has changed ... */
53011042SErik.Nordmark@Sun.COM 	if (addflag.crb_ipv6_recvhoplimit) {
5310Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
5320Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_IPV6;
5330Sstevel@tonic-gate 		cmsg->cmsg_type = IPV6_HOPLIMIT;
5340Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + sizeof (uint_t);
5350Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 		*(uint_t *)optptr = ipp->ipp_hoplimit;
5380Sstevel@tonic-gate 		optptr += sizeof (uint_t);
5390Sstevel@tonic-gate 		ASSERT(OK_32PTR(optptr));
5400Sstevel@tonic-gate 		/* Save as "last" value */
5410Sstevel@tonic-gate 		sctp->sctp_recvhops = ipp->ipp_hoplimit;
5420Sstevel@tonic-gate 	}
54311042SErik.Nordmark@Sun.COM 	/* If app asked for tclass and it has changed ... */
54411042SErik.Nordmark@Sun.COM 	if (addflag.crb_ipv6_recvtclass) {
54511042SErik.Nordmark@Sun.COM 		cmsg = (struct cmsghdr *)optptr;
54611042SErik.Nordmark@Sun.COM 		cmsg->cmsg_level = IPPROTO_IPV6;
54711042SErik.Nordmark@Sun.COM 		cmsg->cmsg_type = IPV6_TCLASS;
54811042SErik.Nordmark@Sun.COM 		cmsg->cmsg_len = sizeof (*cmsg) + sizeof (uint_t);
54911042SErik.Nordmark@Sun.COM 		optptr += sizeof (*cmsg);
55011042SErik.Nordmark@Sun.COM 
55111042SErik.Nordmark@Sun.COM 		*(uint_t *)optptr = ipp->ipp_tclass;
55211042SErik.Nordmark@Sun.COM 		optptr += sizeof (uint_t);
55311042SErik.Nordmark@Sun.COM 		ASSERT(OK_32PTR(optptr));
55411042SErik.Nordmark@Sun.COM 		/* Save as "last" value */
55511042SErik.Nordmark@Sun.COM 		sctp->sctp_recvtclass = ipp->ipp_tclass;
55611042SErik.Nordmark@Sun.COM 	}
55711042SErik.Nordmark@Sun.COM 	if (addflag.crb_ipv6_recvhopopts) {
5580Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
5590Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_IPV6;
5600Sstevel@tonic-gate 		cmsg->cmsg_type = IPV6_HOPOPTS;
5610Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_hopoptslen;
5620Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 		bcopy(ipp->ipp_hopopts, optptr, ipp->ipp_hopoptslen);
5650Sstevel@tonic-gate 		optptr += ipp->ipp_hopoptslen;
5660Sstevel@tonic-gate 		ASSERT(OK_32PTR(optptr));
5670Sstevel@tonic-gate 		/* Save as last value */
5681676Sjpk 		ip_savebuf((void **)&sctp->sctp_hopopts,
5690Sstevel@tonic-gate 		    &sctp->sctp_hopoptslen,
5700Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_HOPOPTS),
5710Sstevel@tonic-gate 		    ipp->ipp_hopopts, ipp->ipp_hopoptslen);
5720Sstevel@tonic-gate 	}
57311042SErik.Nordmark@Sun.COM 	if (addflag.crb_ipv6_recvrthdrdstopts) {
5740Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
5750Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_IPV6;
5760Sstevel@tonic-gate 		cmsg->cmsg_type = IPV6_RTHDRDSTOPTS;
57711042SErik.Nordmark@Sun.COM 		cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_rthdrdstoptslen;
5780Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
5790Sstevel@tonic-gate 
58011042SErik.Nordmark@Sun.COM 		bcopy(ipp->ipp_rthdrdstopts, optptr, ipp->ipp_rthdrdstoptslen);
58111042SErik.Nordmark@Sun.COM 		optptr += ipp->ipp_rthdrdstoptslen;
5820Sstevel@tonic-gate 		ASSERT(OK_32PTR(optptr));
5830Sstevel@tonic-gate 		/* Save as last value */
58411042SErik.Nordmark@Sun.COM 		ip_savebuf((void **)&sctp->sctp_rthdrdstopts,
58511042SErik.Nordmark@Sun.COM 		    &sctp->sctp_rthdrdstoptslen,
58611042SErik.Nordmark@Sun.COM 		    (ipp->ipp_fields & IPPF_RTHDRDSTOPTS),
58711042SErik.Nordmark@Sun.COM 		    ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
5880Sstevel@tonic-gate 	}
58911042SErik.Nordmark@Sun.COM 	if (addflag.crb_ipv6_recvrthdr) {
5900Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
5910Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_IPV6;
5920Sstevel@tonic-gate 		cmsg->cmsg_type = IPV6_RTHDR;
5930Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_rthdrlen;
5940Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 		bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen);
5970Sstevel@tonic-gate 		optptr += ipp->ipp_rthdrlen;
5980Sstevel@tonic-gate 		ASSERT(OK_32PTR(optptr));
5990Sstevel@tonic-gate 		/* Save as last value */
6001676Sjpk 		ip_savebuf((void **)&sctp->sctp_rthdr,
6010Sstevel@tonic-gate 		    &sctp->sctp_rthdrlen,
6020Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_RTHDR),
6030Sstevel@tonic-gate 		    ipp->ipp_rthdr, ipp->ipp_rthdrlen);
6040Sstevel@tonic-gate 	}
60511042SErik.Nordmark@Sun.COM 	if (addflag.crb_ipv6_recvdstopts) {
6060Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
6070Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_IPV6;
6080Sstevel@tonic-gate 		cmsg->cmsg_type = IPV6_DSTOPTS;
6090Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_dstoptslen;
6100Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 		bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen);
6130Sstevel@tonic-gate 		optptr += ipp->ipp_dstoptslen;
6140Sstevel@tonic-gate 		ASSERT(OK_32PTR(optptr));
6150Sstevel@tonic-gate 		/* Save as last value */
6161676Sjpk 		ip_savebuf((void **)&sctp->sctp_dstopts,
6170Sstevel@tonic-gate 		    &sctp->sctp_dstoptslen,
6180Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_DSTOPTS),
6190Sstevel@tonic-gate 		    ipp->ipp_dstopts, ipp->ipp_dstoptslen);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	ASSERT(optptr == mp1->b_wptr);
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	return (0);
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate void
sctp_free_reass(sctp_instr_t * sip)6280Sstevel@tonic-gate sctp_free_reass(sctp_instr_t *sip)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate 	mblk_t *mp, *mpnext, *mctl;
63112604SGeorge.Shepherd@Sun.COM #ifdef	DEBUG
63212604SGeorge.Shepherd@Sun.COM 	sctp_reass_t	*srp;
63312604SGeorge.Shepherd@Sun.COM #endif
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	for (mp = sip->istr_reass; mp != NULL; mp = mpnext) {
6360Sstevel@tonic-gate 		mpnext = mp->b_next;
6370Sstevel@tonic-gate 		mp->b_next = NULL;
6380Sstevel@tonic-gate 		mp->b_prev = NULL;
6390Sstevel@tonic-gate 		if (DB_TYPE(mp) == M_CTL) {
6400Sstevel@tonic-gate 			mctl = mp;
64112604SGeorge.Shepherd@Sun.COM #ifdef	DEBUG
64212604SGeorge.Shepherd@Sun.COM 			srp = (sctp_reass_t *)DB_BASE(mctl);
64312604SGeorge.Shepherd@Sun.COM 			/* Partial delivery can leave empty srp */
64413009SChandrasekar.Marimuthu@Sun.COM 			ASSERT(mp->b_cont != NULL || srp->sr_got == 0);
64512604SGeorge.Shepherd@Sun.COM #endif
6460Sstevel@tonic-gate 			mp = mp->b_cont;
6470Sstevel@tonic-gate 			mctl->b_cont = NULL;
6480Sstevel@tonic-gate 			freeb(mctl);
6490Sstevel@tonic-gate 		}
6500Sstevel@tonic-gate 		freemsg(mp);
6510Sstevel@tonic-gate 	}
65211373SGeorge.Shepherd@Sun.COM 	sip->istr_reass = NULL;
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate /*
6560Sstevel@tonic-gate  * If the series of data fragments of which dmp is a part is successfully
6570Sstevel@tonic-gate  * reassembled, the first mblk in the series is returned. dc is adjusted
6580Sstevel@tonic-gate  * to point at the data chunk in the lead mblk, and b_rptr also points to
6590Sstevel@tonic-gate  * the data chunk; the following mblk's b_rptr's point at the actual payload.
6600Sstevel@tonic-gate  *
6610Sstevel@tonic-gate  * If the series is not yet reassembled, NULL is returned. dc is not changed.
6620Sstevel@tonic-gate  * XXX should probably move this up into the state machine.
6630Sstevel@tonic-gate  */
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate /* Fragment list for un-ordered messages. Partial delivery is not supported */
6660Sstevel@tonic-gate static mblk_t *
sctp_uodata_frag(sctp_t * sctp,mblk_t * dmp,sctp_data_hdr_t ** dc)6670Sstevel@tonic-gate sctp_uodata_frag(sctp_t *sctp, mblk_t *dmp, sctp_data_hdr_t **dc)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	mblk_t		*hmp;
6700Sstevel@tonic-gate 	mblk_t		*begin = NULL;
6710Sstevel@tonic-gate 	mblk_t		*end = NULL;
6720Sstevel@tonic-gate 	sctp_data_hdr_t	*qdc;
6730Sstevel@tonic-gate 	uint32_t	ntsn;
6740Sstevel@tonic-gate 	uint32_t	tsn = ntohl((*dc)->sdh_tsn);
6750Sstevel@tonic-gate #ifdef	DEBUG
6760Sstevel@tonic-gate 	mblk_t		*mp1;
6770Sstevel@tonic-gate #endif
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	/* First frag. */
6800Sstevel@tonic-gate 	if (sctp->sctp_uo_frags == NULL) {
6810Sstevel@tonic-gate 		sctp->sctp_uo_frags = dmp;
6820Sstevel@tonic-gate 		return (NULL);
6830Sstevel@tonic-gate 	}
6840Sstevel@tonic-gate 	hmp = sctp->sctp_uo_frags;
6850Sstevel@tonic-gate 	/*
6860Sstevel@tonic-gate 	 * Insert the segment according to the TSN, fragmented unordered
6870Sstevel@tonic-gate 	 * chunks are sequenced by TSN.
6880Sstevel@tonic-gate 	 */
6890Sstevel@tonic-gate 	while (hmp != NULL) {
6900Sstevel@tonic-gate 		qdc = (sctp_data_hdr_t *)hmp->b_rptr;
6910Sstevel@tonic-gate 		ntsn = ntohl(qdc->sdh_tsn);
6920Sstevel@tonic-gate 		if (SEQ_GT(ntsn, tsn)) {
6930Sstevel@tonic-gate 			if (hmp->b_prev == NULL) {
6940Sstevel@tonic-gate 				dmp->b_next = hmp;
6950Sstevel@tonic-gate 				hmp->b_prev = dmp;
6960Sstevel@tonic-gate 				sctp->sctp_uo_frags = dmp;
6970Sstevel@tonic-gate 			} else {
6980Sstevel@tonic-gate 				dmp->b_next = hmp;
6990Sstevel@tonic-gate 				dmp->b_prev = hmp->b_prev;
7000Sstevel@tonic-gate 				hmp->b_prev->b_next = dmp;
7010Sstevel@tonic-gate 				hmp->b_prev = dmp;
7020Sstevel@tonic-gate 			}
7030Sstevel@tonic-gate 			break;
7040Sstevel@tonic-gate 		}
7050Sstevel@tonic-gate 		if (hmp->b_next == NULL) {
7060Sstevel@tonic-gate 			hmp->b_next = dmp;
7070Sstevel@tonic-gate 			dmp->b_prev = hmp;
7080Sstevel@tonic-gate 			break;
7090Sstevel@tonic-gate 		}
7100Sstevel@tonic-gate 		hmp = hmp->b_next;
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 	/* check if we completed a msg */
7130Sstevel@tonic-gate 	if (SCTP_DATA_GET_BBIT(*dc)) {
7140Sstevel@tonic-gate 		begin = dmp;
7150Sstevel@tonic-gate 	} else if (SCTP_DATA_GET_EBIT(*dc)) {
7160Sstevel@tonic-gate 		end = dmp;
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 	/*
7190Sstevel@tonic-gate 	 * We walk consecutive TSNs backwards till we get a seg. with
7200Sstevel@tonic-gate 	 * the B bit
7210Sstevel@tonic-gate 	 */
7220Sstevel@tonic-gate 	if (begin == NULL) {
7230Sstevel@tonic-gate 		for (hmp = dmp->b_prev; hmp != NULL; hmp = hmp->b_prev) {
7240Sstevel@tonic-gate 			qdc = (sctp_data_hdr_t *)hmp->b_rptr;
7250Sstevel@tonic-gate 			ntsn = ntohl(qdc->sdh_tsn);
7260Sstevel@tonic-gate 			if ((int32_t)(tsn - ntsn) > 1) {
7270Sstevel@tonic-gate 				return (NULL);
7280Sstevel@tonic-gate 			}
7290Sstevel@tonic-gate 			if (SCTP_DATA_GET_BBIT(qdc)) {
7300Sstevel@tonic-gate 				begin = hmp;
7310Sstevel@tonic-gate 				break;
7320Sstevel@tonic-gate 			}
7330Sstevel@tonic-gate 			tsn = ntsn;
7340Sstevel@tonic-gate 		}
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 	tsn = ntohl((*dc)->sdh_tsn);
7370Sstevel@tonic-gate 	/*
7380Sstevel@tonic-gate 	 * We walk consecutive TSNs till we get a seg. with the E bit
7390Sstevel@tonic-gate 	 */
7400Sstevel@tonic-gate 	if (end == NULL) {
7410Sstevel@tonic-gate 		for (hmp = dmp->b_next; hmp != NULL; hmp = hmp->b_next) {
7420Sstevel@tonic-gate 			qdc = (sctp_data_hdr_t *)hmp->b_rptr;
7430Sstevel@tonic-gate 			ntsn = ntohl(qdc->sdh_tsn);
7440Sstevel@tonic-gate 			if ((int32_t)(ntsn - tsn) > 1) {
7450Sstevel@tonic-gate 				return (NULL);
7460Sstevel@tonic-gate 			}
7470Sstevel@tonic-gate 			if (SCTP_DATA_GET_EBIT(qdc)) {
7480Sstevel@tonic-gate 				end = hmp;
7490Sstevel@tonic-gate 				break;
7500Sstevel@tonic-gate 			}
7510Sstevel@tonic-gate 			tsn = ntsn;
7520Sstevel@tonic-gate 		}
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 	if (begin == NULL || end == NULL) {
7550Sstevel@tonic-gate 		return (NULL);
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 	/* Got one!, Remove the msg from the list */
7580Sstevel@tonic-gate 	if (sctp->sctp_uo_frags == begin) {
7590Sstevel@tonic-gate 		ASSERT(begin->b_prev == NULL);
7600Sstevel@tonic-gate 		sctp->sctp_uo_frags = end->b_next;
7610Sstevel@tonic-gate 		if (end->b_next != NULL)
7620Sstevel@tonic-gate 			end->b_next->b_prev = NULL;
7630Sstevel@tonic-gate 	} else {
7640Sstevel@tonic-gate 		begin->b_prev->b_next = end->b_next;
7650Sstevel@tonic-gate 		if (end->b_next != NULL)
7660Sstevel@tonic-gate 			end->b_next->b_prev = begin->b_prev;
7670Sstevel@tonic-gate 	}
7680Sstevel@tonic-gate 	begin->b_prev = NULL;
7690Sstevel@tonic-gate 	end->b_next = NULL;
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	/*
7720Sstevel@tonic-gate 	 * Null out b_next and b_prev and chain using b_cont.
7730Sstevel@tonic-gate 	 */
7740Sstevel@tonic-gate 	dmp = end = begin;
7750Sstevel@tonic-gate 	hmp = begin->b_next;
7760Sstevel@tonic-gate 	*dc = (sctp_data_hdr_t *)begin->b_rptr;
7770Sstevel@tonic-gate 	begin->b_next = NULL;
7780Sstevel@tonic-gate 	while (hmp != NULL) {
7790Sstevel@tonic-gate 		qdc = (sctp_data_hdr_t *)hmp->b_rptr;
7800Sstevel@tonic-gate 		hmp->b_rptr = (uchar_t *)(qdc + 1);
7810Sstevel@tonic-gate 		end = hmp->b_next;
7820Sstevel@tonic-gate 		dmp->b_cont = hmp;
7830Sstevel@tonic-gate 		dmp = hmp;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 		if (end != NULL)
7860Sstevel@tonic-gate 			hmp->b_next = NULL;
7870Sstevel@tonic-gate 		hmp->b_prev = NULL;
7880Sstevel@tonic-gate 		hmp = end;
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_reassmsgs);
7910Sstevel@tonic-gate #ifdef	DEBUG
7920Sstevel@tonic-gate 	mp1 = begin;
7930Sstevel@tonic-gate 	while (mp1 != NULL) {
7940Sstevel@tonic-gate 		ASSERT(mp1->b_next == NULL);
7950Sstevel@tonic-gate 		ASSERT(mp1->b_prev == NULL);
7960Sstevel@tonic-gate 		mp1 = mp1->b_cont;
7970Sstevel@tonic-gate 	}
7980Sstevel@tonic-gate #endif
7990Sstevel@tonic-gate 	return (begin);
8000Sstevel@tonic-gate }
8013845Svi117747 
8023845Svi117747 /*
8033845Svi117747  * Try partial delivery.
8043845Svi117747  */
8053845Svi117747 static mblk_t *
sctp_try_partial_delivery(sctp_t * sctp,mblk_t * hmp,sctp_reass_t * srp,sctp_data_hdr_t ** dc)8063845Svi117747 sctp_try_partial_delivery(sctp_t *sctp, mblk_t *hmp, sctp_reass_t *srp,
8073845Svi117747     sctp_data_hdr_t **dc)
8083845Svi117747 {
8093845Svi117747 	mblk_t		*mp;
8103845Svi117747 	mblk_t		*dmp;
8113845Svi117747 	mblk_t		*qmp;
8123845Svi117747 	mblk_t		*prev;
8133845Svi117747 	sctp_data_hdr_t	*qdc;
8143845Svi117747 	uint32_t	tsn;
8153845Svi117747 
8163845Svi117747 	ASSERT(DB_TYPE(hmp) == M_CTL);
8173845Svi117747 
8183845Svi117747 	dprint(4, ("trypartial: got=%d, needed=%d\n",
81913009SChandrasekar.Marimuthu@Sun.COM 	    (int)(srp->sr_got), (int)(srp->sr_needed)));
8203845Svi117747 
82111042SErik.Nordmark@Sun.COM 	mp = hmp->b_cont;
8223845Svi117747 	qdc = (sctp_data_hdr_t *)mp->b_rptr;
8233845Svi117747 
82413009SChandrasekar.Marimuthu@Sun.COM 	ASSERT(SCTP_DATA_GET_BBIT(qdc) && srp->sr_hasBchunk);
8253845Svi117747 
8263845Svi117747 	tsn = ntohl(qdc->sdh_tsn) + 1;
8273845Svi117747 
8283845Svi117747 	/*
8293845Svi117747 	 * This loop has two exit conditions: the
8303845Svi117747 	 * end of received chunks has been reached, or
8313845Svi117747 	 * there is a break in the sequence. We want
8323845Svi117747 	 * to chop the reassembly list as follows (the
8333845Svi117747 	 * numbers are TSNs):
8343845Svi117747 	 *   10 -> 11 -> 	(end of chunks)
8353845Svi117747 	 *   10 -> 11 -> | 13   (break in sequence)
8363845Svi117747 	 */
8373845Svi117747 	prev = mp;
8383845Svi117747 	mp = mp->b_cont;
8393845Svi117747 	while (mp != NULL) {
8403845Svi117747 		qdc = (sctp_data_hdr_t *)mp->b_rptr;
8413845Svi117747 		if (ntohl(qdc->sdh_tsn) != tsn)
8423845Svi117747 			break;
8433845Svi117747 		prev = mp;
8443845Svi117747 		mp = mp->b_cont;
8453845Svi117747 		tsn++;
8463845Svi117747 	}
8473845Svi117747 	/*
8483845Svi117747 	 * We are sending all the fragments upstream, we have to retain
8493845Svi117747 	 * the srp info for further fragments.
8503845Svi117747 	 */
8513845Svi117747 	if (mp == NULL) {
8523845Svi117747 		dmp = hmp->b_cont;
8533845Svi117747 		hmp->b_cont = NULL;
85413009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_nexttsn = tsn;
85513009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_msglen = 0;
85613009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_needed = 0;
85713009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_got = 0;
85813009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_tail = NULL;
8593845Svi117747 	} else {
86012604SGeorge.Shepherd@Sun.COM 		/*
86112604SGeorge.Shepherd@Sun.COM 		 * There is a gap then some ordered frags which are not
86212604SGeorge.Shepherd@Sun.COM 		 * the next deliverable tsn. When the next deliverable
86312604SGeorge.Shepherd@Sun.COM 		 * frag arrives it will be set as the new list head in
86412604SGeorge.Shepherd@Sun.COM 		 * sctp_data_frag() by setting the B bit.
86512604SGeorge.Shepherd@Sun.COM 		 */
8663845Svi117747 		dmp = hmp->b_cont;
8673845Svi117747 		hmp->b_cont = mp;
8683845Svi117747 	}
86913009SChandrasekar.Marimuthu@Sun.COM 	srp->sr_hasBchunk = B_FALSE;
8703845Svi117747 	/*
8713845Svi117747 	 * mp now points at the last chunk in the sequence,
8723845Svi117747 	 * and prev points to mp's previous in the list.
87312604SGeorge.Shepherd@Sun.COM 	 * We chop the list at prev. Subsequent fragment
87412604SGeorge.Shepherd@Sun.COM 	 * deliveries will follow the normal reassembly
87512604SGeorge.Shepherd@Sun.COM 	 * path unless they too exceed the sctp_pd_point.
8763845Svi117747 	 */
8773845Svi117747 	prev->b_cont = NULL;
87813009SChandrasekar.Marimuthu@Sun.COM 	srp->sr_partial_delivered = B_TRUE;
8793845Svi117747 
8803845Svi117747 	dprint(4, ("trypartial: got some, got=%d, needed=%d\n",
88113009SChandrasekar.Marimuthu@Sun.COM 	    (int)(srp->sr_got), (int)(srp->sr_needed)));
8823845Svi117747 
8833845Svi117747 	/*
8843845Svi117747 	 * Adjust all mblk's except the lead so their rptr's point to the
8853845Svi117747 	 * payload. sctp_data_chunk() will need to process the lead's
8863845Svi117747 	 * data chunk section, so leave it's rptr pointing at the data chunk.
8873845Svi117747 	 */
8883845Svi117747 	*dc = (sctp_data_hdr_t *)dmp->b_rptr;
88913009SChandrasekar.Marimuthu@Sun.COM 	if (srp->sr_tail != NULL) {
89013009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_got--;
89113009SChandrasekar.Marimuthu@Sun.COM 		ASSERT(srp->sr_got != 0);
89213009SChandrasekar.Marimuthu@Sun.COM 		if (srp->sr_needed != 0) {
89313009SChandrasekar.Marimuthu@Sun.COM 			srp->sr_needed--;
89413009SChandrasekar.Marimuthu@Sun.COM 			ASSERT(srp->sr_needed != 0);
8953845Svi117747 		}
89613009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_msglen -= ntohs((*dc)->sdh_len);
8973845Svi117747 	}
8983845Svi117747 	for (qmp = dmp->b_cont; qmp != NULL; qmp = qmp->b_cont) {
8993845Svi117747 		qdc = (sctp_data_hdr_t *)qmp->b_rptr;
9003845Svi117747 		qmp->b_rptr = (uchar_t *)(qdc + 1);
9013845Svi117747 
9023845Svi117747 		/*
9033845Svi117747 		 * Deduct the balance from got and needed here, now that
9043845Svi117747 		 * we know we are actually delivering these data.
9053845Svi117747 		 */
90613009SChandrasekar.Marimuthu@Sun.COM 		if (srp->sr_tail != NULL) {
90713009SChandrasekar.Marimuthu@Sun.COM 			srp->sr_got--;
90813009SChandrasekar.Marimuthu@Sun.COM 			ASSERT(srp->sr_got != 0);
90913009SChandrasekar.Marimuthu@Sun.COM 			if (srp->sr_needed != 0) {
91013009SChandrasekar.Marimuthu@Sun.COM 				srp->sr_needed--;
91113009SChandrasekar.Marimuthu@Sun.COM 				ASSERT(srp->sr_needed != 0);
9123845Svi117747 			}
91313009SChandrasekar.Marimuthu@Sun.COM 			srp->sr_msglen -= ntohs(qdc->sdh_len);
9143845Svi117747 		}
9153845Svi117747 	}
91613009SChandrasekar.Marimuthu@Sun.COM 	ASSERT(srp->sr_msglen == 0);
9173845Svi117747 	BUMP_LOCAL(sctp->sctp_reassmsgs);
9183845Svi117747 
9193845Svi117747 	return (dmp);
9203845Svi117747 }
9213845Svi117747 
9220Sstevel@tonic-gate /*
92312604SGeorge.Shepherd@Sun.COM  * Handle received fragments for ordered delivery to upper layer protocol.
92412604SGeorge.Shepherd@Sun.COM  * Manage the per message reassembly queue and if this fragment completes
92512604SGeorge.Shepherd@Sun.COM  * reassembly of the message, or qualifies the already reassembled data
92612604SGeorge.Shepherd@Sun.COM  * for partial delivery, prepare the message for delivery upstream.
92712604SGeorge.Shepherd@Sun.COM  *
92812604SGeorge.Shepherd@Sun.COM  * tpfinished in the caller remains set only when the incoming fragment
92912604SGeorge.Shepherd@Sun.COM  * has completed the reassembly of the message associated with its ssn.
9300Sstevel@tonic-gate  */
9310Sstevel@tonic-gate static mblk_t *
sctp_data_frag(sctp_t * sctp,mblk_t * dmp,sctp_data_hdr_t ** dc,int * error,sctp_instr_t * sip,boolean_t * tpfinished)9320Sstevel@tonic-gate sctp_data_frag(sctp_t *sctp, mblk_t *dmp, sctp_data_hdr_t **dc, int *error,
9333845Svi117747     sctp_instr_t *sip, boolean_t *tpfinished)
9340Sstevel@tonic-gate {
93512604SGeorge.Shepherd@Sun.COM 	mblk_t		*reassq_curr, *reassq_next, *reassq_prev;
93612604SGeorge.Shepherd@Sun.COM 	mblk_t		*new_reassq;
9370Sstevel@tonic-gate 	mblk_t		*qmp;
9380Sstevel@tonic-gate 	mblk_t		*first_mp;
9390Sstevel@tonic-gate 	sctp_reass_t	*srp;
9400Sstevel@tonic-gate 	sctp_data_hdr_t	*qdc;
9410Sstevel@tonic-gate 	sctp_data_hdr_t	*bdc;
9420Sstevel@tonic-gate 	sctp_data_hdr_t	*edc;
9430Sstevel@tonic-gate 	uint32_t	tsn;
9443845Svi117747 	uint16_t	fraglen = 0;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	*error = 0;
9470Sstevel@tonic-gate 
94812604SGeorge.Shepherd@Sun.COM 	/*
94912604SGeorge.Shepherd@Sun.COM 	 * Find the reassembly queue for this data chunk, if none
95012604SGeorge.Shepherd@Sun.COM 	 * yet exists, a new per message queue will be created and
95112604SGeorge.Shepherd@Sun.COM 	 * appended to the end of the list of per message queues.
95212604SGeorge.Shepherd@Sun.COM 	 *
95312604SGeorge.Shepherd@Sun.COM 	 * sip points on sctp_instr_t representing instream messages
95412604SGeorge.Shepherd@Sun.COM 	 * as yet undelivered for this stream (sid) of the association.
95512604SGeorge.Shepherd@Sun.COM 	 */
95612604SGeorge.Shepherd@Sun.COM 	reassq_next = reassq_prev = sip->istr_reass;
95712604SGeorge.Shepherd@Sun.COM 	for (; reassq_next != NULL; reassq_next = reassq_next->b_next) {
95812604SGeorge.Shepherd@Sun.COM 		srp = (sctp_reass_t *)DB_BASE(reassq_next);
95913009SChandrasekar.Marimuthu@Sun.COM 		if (ntohs((*dc)->sdh_ssn) == srp->sr_ssn) {
96012604SGeorge.Shepherd@Sun.COM 			reassq_curr = reassq_next;
9610Sstevel@tonic-gate 			goto foundit;
96213009SChandrasekar.Marimuthu@Sun.COM 		} else if (SSN_GT(srp->sr_ssn, ntohs((*dc)->sdh_ssn)))
9630Sstevel@tonic-gate 			break;
96412604SGeorge.Shepherd@Sun.COM 		reassq_prev = reassq_next;
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 
9673845Svi117747 	/*
96812604SGeorge.Shepherd@Sun.COM 	 * First fragment of this message received, allocate a M_CTL that
96912604SGeorge.Shepherd@Sun.COM 	 * will head the reassembly queue for this message. The message
97012604SGeorge.Shepherd@Sun.COM 	 * and all its fragments are identified by having the same ssn.
97112604SGeorge.Shepherd@Sun.COM 	 *
97212604SGeorge.Shepherd@Sun.COM 	 * Arriving fragments will be inserted in tsn order on the
97312604SGeorge.Shepherd@Sun.COM 	 * reassembly queue for this message (ssn), linked by b_cont.
9743845Svi117747 	 */
97512604SGeorge.Shepherd@Sun.COM 	if ((new_reassq = allocb(sizeof (*srp), BPRI_MED)) == NULL) {
97612604SGeorge.Shepherd@Sun.COM 		*error = ENOMEM;
9773845Svi117747 		return (NULL);
9783845Svi117747 	}
97912604SGeorge.Shepherd@Sun.COM 	DB_TYPE(new_reassq) = M_CTL;
98012604SGeorge.Shepherd@Sun.COM 	srp = (sctp_reass_t *)DB_BASE(new_reassq);
98112604SGeorge.Shepherd@Sun.COM 	new_reassq->b_cont = dmp;
98212604SGeorge.Shepherd@Sun.COM 
98312604SGeorge.Shepherd@Sun.COM 	/*
98412604SGeorge.Shepherd@Sun.COM 	 * All per ssn reassembly queues, (one for each message) on
98512604SGeorge.Shepherd@Sun.COM 	 * this stream are doubly linked by b_next/b_prev back to the
98612604SGeorge.Shepherd@Sun.COM 	 * instr_reass of the instream structure associated with this
98712604SGeorge.Shepherd@Sun.COM 	 * stream id, (sip is initialized as sctp->sctp_instr[sid]).
98812604SGeorge.Shepherd@Sun.COM 	 * Insert the new reassembly queue in the correct (ssn) order.
98912604SGeorge.Shepherd@Sun.COM 	 */
99012604SGeorge.Shepherd@Sun.COM 	if (reassq_next != NULL) {
99112604SGeorge.Shepherd@Sun.COM 		if (sip->istr_reass == reassq_next) {
99212604SGeorge.Shepherd@Sun.COM 			/* head insertion */
99312604SGeorge.Shepherd@Sun.COM 			sip->istr_reass = new_reassq;
99412604SGeorge.Shepherd@Sun.COM 			new_reassq->b_next = reassq_next;
99512604SGeorge.Shepherd@Sun.COM 			new_reassq->b_prev = NULL;
99612604SGeorge.Shepherd@Sun.COM 			reassq_next->b_prev = new_reassq;
9970Sstevel@tonic-gate 		} else {
99812604SGeorge.Shepherd@Sun.COM 			/* mid queue insertion */
99912604SGeorge.Shepherd@Sun.COM 			reassq_prev->b_next = new_reassq;
100012604SGeorge.Shepherd@Sun.COM 			new_reassq->b_prev = reassq_prev;
100112604SGeorge.Shepherd@Sun.COM 			new_reassq->b_next = reassq_next;
100212604SGeorge.Shepherd@Sun.COM 			reassq_next->b_prev = new_reassq;
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 	} else {
100512604SGeorge.Shepherd@Sun.COM 		/* place new reassembly queue at the end */
10060Sstevel@tonic-gate 		if (sip->istr_reass == NULL) {
100712604SGeorge.Shepherd@Sun.COM 			sip->istr_reass = new_reassq;
100812604SGeorge.Shepherd@Sun.COM 			new_reassq->b_prev = NULL;
10090Sstevel@tonic-gate 		} else {
101012604SGeorge.Shepherd@Sun.COM 			reassq_prev->b_next = new_reassq;
101112604SGeorge.Shepherd@Sun.COM 			new_reassq->b_prev = reassq_prev;
10120Sstevel@tonic-gate 		}
101312604SGeorge.Shepherd@Sun.COM 		new_reassq->b_next = NULL;
10140Sstevel@tonic-gate 	}
101513009SChandrasekar.Marimuthu@Sun.COM 	srp->sr_partial_delivered = B_FALSE;
101613009SChandrasekar.Marimuthu@Sun.COM 	srp->sr_ssn = ntohs((*dc)->sdh_ssn);
101713009SChandrasekar.Marimuthu@Sun.COM 	srp->sr_hasBchunk = B_FALSE;
10183845Svi117747 empty_srp:
101913009SChandrasekar.Marimuthu@Sun.COM 	srp->sr_needed = 0;
102013009SChandrasekar.Marimuthu@Sun.COM 	srp->sr_got = 1;
102112604SGeorge.Shepherd@Sun.COM 	/* tail always the highest tsn on the reassembly queue for this ssn */
102213009SChandrasekar.Marimuthu@Sun.COM 	srp->sr_tail = dmp;
10233845Svi117747 	if (SCTP_DATA_GET_BBIT(*dc)) {
102412604SGeorge.Shepherd@Sun.COM 		/* Incoming frag is flagged as the beginning of message */
102513009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_msglen = ntohs((*dc)->sdh_len);
102613009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_nexttsn = ntohl((*dc)->sdh_tsn) + 1;
102713009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_hasBchunk = B_TRUE;
102813009SChandrasekar.Marimuthu@Sun.COM 	} else if (srp->sr_partial_delivered &&
102913009SChandrasekar.Marimuthu@Sun.COM 	    srp->sr_nexttsn == ntohl((*dc)->sdh_tsn)) {
103012604SGeorge.Shepherd@Sun.COM 		/*
103112604SGeorge.Shepherd@Sun.COM 		 * The real beginning fragment of the message was already
103212604SGeorge.Shepherd@Sun.COM 		 * delivered upward, so this is the earliest frag expected.
103312604SGeorge.Shepherd@Sun.COM 		 * Fake the B-bit then see if this frag also completes the
103412604SGeorge.Shepherd@Sun.COM 		 * message.
103512604SGeorge.Shepherd@Sun.COM 		 */
10363845Svi117747 		SCTP_DATA_SET_BBIT(*dc);
103713009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_hasBchunk = B_TRUE;
103813009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_msglen = ntohs((*dc)->sdh_len);
10393845Svi117747 		if (SCTP_DATA_GET_EBIT(*dc)) {
104012604SGeorge.Shepherd@Sun.COM 			/* This frag is marked as the end of message */
104113009SChandrasekar.Marimuthu@Sun.COM 			srp->sr_needed = 1;
104212604SGeorge.Shepherd@Sun.COM 			/* Got all fragments of this message now */
10433845Svi117747 			goto frag_done;
10443845Svi117747 		}
104513009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_nexttsn++;
10463845Svi117747 	}
104712604SGeorge.Shepherd@Sun.COM 
104812604SGeorge.Shepherd@Sun.COM 	/* The only fragment of this message currently queued */
104912604SGeorge.Shepherd@Sun.COM 	*tpfinished = B_FALSE;
10500Sstevel@tonic-gate 	return (NULL);
10510Sstevel@tonic-gate foundit:
10520Sstevel@tonic-gate 	/*
105312604SGeorge.Shepherd@Sun.COM 	 * This message already has a reassembly queue. Insert the new frag
105412604SGeorge.Shepherd@Sun.COM 	 * in the reassembly queue. Try the tail first, on the assumption
105512604SGeorge.Shepherd@Sun.COM 	 * that the fragments are arriving in order.
10560Sstevel@tonic-gate 	 */
105713009SChandrasekar.Marimuthu@Sun.COM 	qmp = srp->sr_tail;
10583845Svi117747 
10593845Svi117747 	/*
106012604SGeorge.Shepherd@Sun.COM 	 * A NULL tail means all existing fragments of the message have
106112604SGeorge.Shepherd@Sun.COM 	 * been entirely consumed during a partially delivery.
10623845Svi117747 	 */
10633845Svi117747 	if (qmp == NULL) {
106413009SChandrasekar.Marimuthu@Sun.COM 		ASSERT(srp->sr_got == 0 && srp->sr_needed == 0 &&
106513009SChandrasekar.Marimuthu@Sun.COM 		    srp->sr_partial_delivered);
106612604SGeorge.Shepherd@Sun.COM 		ASSERT(reassq_curr->b_cont == NULL);
106712604SGeorge.Shepherd@Sun.COM 		reassq_curr->b_cont = dmp;
10683845Svi117747 		goto empty_srp;
106912604SGeorge.Shepherd@Sun.COM 	} else {
107012604SGeorge.Shepherd@Sun.COM 		/*
107112604SGeorge.Shepherd@Sun.COM 		 * If partial delivery did take place but the next arriving
107212604SGeorge.Shepherd@Sun.COM 		 * fragment was not the next to be delivered, or partial
107312604SGeorge.Shepherd@Sun.COM 		 * delivery broke off due to a gap, fragments remain on the
107412604SGeorge.Shepherd@Sun.COM 		 * tail. The next fragment due to be delivered still has to
107512604SGeorge.Shepherd@Sun.COM 		 * be set as the new head of list upon arrival. Fake B-bit
107612604SGeorge.Shepherd@Sun.COM 		 * on that frag then see if it also completes the message.
107712604SGeorge.Shepherd@Sun.COM 		 */
107813009SChandrasekar.Marimuthu@Sun.COM 		if (srp->sr_partial_delivered &&
107913009SChandrasekar.Marimuthu@Sun.COM 		    srp->sr_nexttsn == ntohl((*dc)->sdh_tsn)) {
108012604SGeorge.Shepherd@Sun.COM 			SCTP_DATA_SET_BBIT(*dc);
108113009SChandrasekar.Marimuthu@Sun.COM 			srp->sr_hasBchunk = B_TRUE;
108212604SGeorge.Shepherd@Sun.COM 			if (SCTP_DATA_GET_EBIT(*dc)) {
108312604SGeorge.Shepherd@Sun.COM 				/* Got all fragments of this message now */
108412604SGeorge.Shepherd@Sun.COM 				goto frag_done;
108512604SGeorge.Shepherd@Sun.COM 			}
108612604SGeorge.Shepherd@Sun.COM 		}
10873845Svi117747 	}
108812604SGeorge.Shepherd@Sun.COM 
108912604SGeorge.Shepherd@Sun.COM 	/* grab the frag header of already queued tail frag for comparison */
10900Sstevel@tonic-gate 	qdc = (sctp_data_hdr_t *)qmp->b_rptr;
10910Sstevel@tonic-gate 	ASSERT(qmp->b_cont == NULL);
10920Sstevel@tonic-gate 
109312604SGeorge.Shepherd@Sun.COM 	/* check if the frag goes on the tail in order */
10940Sstevel@tonic-gate 	if (SEQ_GT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) {
10950Sstevel@tonic-gate 		qmp->b_cont = dmp;
109613009SChandrasekar.Marimuthu@Sun.COM 		srp->sr_tail = dmp;
10970Sstevel@tonic-gate 		dmp->b_cont = NULL;
109813009SChandrasekar.Marimuthu@Sun.COM 		if (srp->sr_hasBchunk && srp->sr_nexttsn ==
109913009SChandrasekar.Marimuthu@Sun.COM 		    ntohl((*dc)->sdh_tsn)) {
110013009SChandrasekar.Marimuthu@Sun.COM 			srp->sr_msglen += ntohs((*dc)->sdh_len);
110113009SChandrasekar.Marimuthu@Sun.COM 			srp->sr_nexttsn++;
11023845Svi117747 		}
11030Sstevel@tonic-gate 		goto inserted;
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate 
110612604SGeorge.Shepherd@Sun.COM 	/* Next check if we should insert this frag at the beginning */
110712604SGeorge.Shepherd@Sun.COM 	qmp = reassq_curr->b_cont;
11080Sstevel@tonic-gate 	qdc = (sctp_data_hdr_t *)qmp->b_rptr;
11090Sstevel@tonic-gate 	if (SEQ_LT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) {
11103845Svi117747 		dmp->b_cont = qmp;
111112604SGeorge.Shepherd@Sun.COM 		reassq_curr->b_cont = dmp;
11123845Svi117747 		if (SCTP_DATA_GET_BBIT(*dc)) {
111313009SChandrasekar.Marimuthu@Sun.COM 			srp->sr_hasBchunk = B_TRUE;
111413009SChandrasekar.Marimuthu@Sun.COM 			srp->sr_nexttsn = ntohl((*dc)->sdh_tsn);
11150Sstevel@tonic-gate 		}
11163845Svi117747 		goto preinserted;
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 
111912604SGeorge.Shepherd@Sun.COM 	/* Insert this frag in it's correct order in the middle */
11200Sstevel@tonic-gate 	for (;;) {
11210Sstevel@tonic-gate 		/* Tail check above should have caught this */
11220Sstevel@tonic-gate 		ASSERT(qmp->b_cont != NULL);
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 		qdc = (sctp_data_hdr_t *)qmp->b_cont->b_rptr;
11250Sstevel@tonic-gate 		if (SEQ_LT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) {
11260Sstevel@tonic-gate 			/* insert here */
11270Sstevel@tonic-gate 			dmp->b_cont = qmp->b_cont;
11280Sstevel@tonic-gate 			qmp->b_cont = dmp;
11290Sstevel@tonic-gate 			break;
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 		qmp = qmp->b_cont;
11320Sstevel@tonic-gate 	}
11333845Svi117747 preinserted:
113412604SGeorge.Shepherd@Sun.COM 	/*
113512604SGeorge.Shepherd@Sun.COM 	 * Need head of message and to be due to deliver, otherwise skip
113612604SGeorge.Shepherd@Sun.COM 	 * the recalculation of the message length below.
113712604SGeorge.Shepherd@Sun.COM 	 */
113813009SChandrasekar.Marimuthu@Sun.COM 	if (!srp->sr_hasBchunk || ntohl((*dc)->sdh_tsn) != srp->sr_nexttsn)
11393845Svi117747 		goto inserted;
11403845Svi117747 	/*
11413845Svi117747 	 * fraglen contains the length of consecutive chunks of fragments.
114212604SGeorge.Shepherd@Sun.COM 	 * starting from the chunk we just inserted.
11433845Svi117747 	 */
114413009SChandrasekar.Marimuthu@Sun.COM 	tsn = srp->sr_nexttsn;
11453845Svi117747 	for (qmp = dmp; qmp != NULL; qmp = qmp->b_cont) {
11463845Svi117747 		qdc = (sctp_data_hdr_t *)qmp->b_rptr;
11473845Svi117747 		if (tsn != ntohl(qdc->sdh_tsn))
11483845Svi117747 			break;
11493845Svi117747 		fraglen += ntohs(qdc->sdh_len);
11503845Svi117747 		tsn++;
11513845Svi117747 	}
115213009SChandrasekar.Marimuthu@Sun.COM 	srp->sr_nexttsn = tsn;
115313009SChandrasekar.Marimuthu@Sun.COM 	srp->sr_msglen += fraglen;
11540Sstevel@tonic-gate inserted:
115513009SChandrasekar.Marimuthu@Sun.COM 	srp->sr_got++;
115612604SGeorge.Shepherd@Sun.COM 	first_mp = reassq_curr->b_cont;
115712604SGeorge.Shepherd@Sun.COM 	/* Prior to this frag either the beginning or end frag was missing */
115813009SChandrasekar.Marimuthu@Sun.COM 	if (srp->sr_needed == 0) {
115912604SGeorge.Shepherd@Sun.COM 		/* used to check if we have the first and last fragments */
11600Sstevel@tonic-gate 		bdc = (sctp_data_hdr_t *)first_mp->b_rptr;
116113009SChandrasekar.Marimuthu@Sun.COM 		edc = (sctp_data_hdr_t *)srp->sr_tail->b_rptr;
11620Sstevel@tonic-gate 
116312604SGeorge.Shepherd@Sun.COM 		/*
116412604SGeorge.Shepherd@Sun.COM 		 * If we now have both the beginning and the end of the message,
116512604SGeorge.Shepherd@Sun.COM 		 * calculate how many fragments in the complete message.
116612604SGeorge.Shepherd@Sun.COM 		 */
11673845Svi117747 		if (SCTP_DATA_GET_BBIT(bdc) && SCTP_DATA_GET_EBIT(edc)) {
116813009SChandrasekar.Marimuthu@Sun.COM 			srp->sr_needed = ntohl(edc->sdh_tsn) -
11690Sstevel@tonic-gate 			    ntohl(bdc->sdh_tsn) + 1;
11700Sstevel@tonic-gate 		}
11713845Svi117747 	}
11723845Svi117747 
11733845Svi117747 	/*
11743845Svi117747 	 * Try partial delivery if the message length has exceeded the
11753845Svi117747 	 * partial delivery point. Only do this if we can immediately
11763845Svi117747 	 * deliver the partially assembled message, and only partially
11773845Svi117747 	 * deliver one message at a time (i.e. messages cannot be
117812604SGeorge.Shepherd@Sun.COM 	 * intermixed arriving at the upper layer).
117912604SGeorge.Shepherd@Sun.COM 	 * sctp_try_partial_delivery() will return a message consisting
118012604SGeorge.Shepherd@Sun.COM 	 * of only consecutive fragments.
11813845Svi117747 	 */
118213009SChandrasekar.Marimuthu@Sun.COM 	if (srp->sr_needed != srp->sr_got) {
118312604SGeorge.Shepherd@Sun.COM 		/* we don't have the full message yet */
11843845Svi117747 		dmp = NULL;
118512604SGeorge.Shepherd@Sun.COM 		if (ntohl((*dc)->sdh_tsn) <= sctp->sctp_ftsn &&
118613009SChandrasekar.Marimuthu@Sun.COM 		    srp->sr_msglen >= sctp->sctp_pd_point &&
118713009SChandrasekar.Marimuthu@Sun.COM 		    srp->sr_ssn == sip->nextseq) {
118812604SGeorge.Shepherd@Sun.COM 			dmp = sctp_try_partial_delivery(sctp, reassq_curr,
118912604SGeorge.Shepherd@Sun.COM 			    srp, dc);
11900Sstevel@tonic-gate 		}
119112604SGeorge.Shepherd@Sun.COM 		*tpfinished = B_FALSE;
119212604SGeorge.Shepherd@Sun.COM 		/*
119312604SGeorge.Shepherd@Sun.COM 		 * NULL unless a segment of the message now qualified for
119412604SGeorge.Shepherd@Sun.COM 		 * partial_delivery and has been prepared for delivery by
119512604SGeorge.Shepherd@Sun.COM 		 * sctp_try_partial_delivery().
119612604SGeorge.Shepherd@Sun.COM 		 */
11973845Svi117747 		return (dmp);
11980Sstevel@tonic-gate 	}
11993845Svi117747 frag_done:
12000Sstevel@tonic-gate 	/*
120112604SGeorge.Shepherd@Sun.COM 	 * Reassembly complete for this message, prepare the data for delivery.
120212604SGeorge.Shepherd@Sun.COM 	 * First unlink the reassembly queue for this ssn from the list of
120312604SGeorge.Shepherd@Sun.COM 	 * messages in reassembly.
12040Sstevel@tonic-gate 	 */
120512604SGeorge.Shepherd@Sun.COM 	if (sip->istr_reass == reassq_curr) {
120612604SGeorge.Shepherd@Sun.COM 		sip->istr_reass = reassq_curr->b_next;
120712604SGeorge.Shepherd@Sun.COM 		if (reassq_curr->b_next)
120812604SGeorge.Shepherd@Sun.COM 			reassq_curr->b_next->b_prev = NULL;
12090Sstevel@tonic-gate 	} else {
121012604SGeorge.Shepherd@Sun.COM 		ASSERT(reassq_curr->b_prev != NULL);
121112604SGeorge.Shepherd@Sun.COM 		reassq_curr->b_prev->b_next = reassq_curr->b_next;
121212604SGeorge.Shepherd@Sun.COM 		if (reassq_curr->b_next)
121312604SGeorge.Shepherd@Sun.COM 			reassq_curr->b_next->b_prev = reassq_curr->b_prev;
12140Sstevel@tonic-gate 	}
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	/*
121712604SGeorge.Shepherd@Sun.COM 	 * Need to clean up b_prev and b_next as freeb() will
121812604SGeorge.Shepherd@Sun.COM 	 * ASSERT that they are unused.
12190Sstevel@tonic-gate 	 */
122012604SGeorge.Shepherd@Sun.COM 	reassq_curr->b_next = NULL;
122112604SGeorge.Shepherd@Sun.COM 	reassq_curr->b_prev = NULL;
122212604SGeorge.Shepherd@Sun.COM 
122312604SGeorge.Shepherd@Sun.COM 	dmp = reassq_curr;
122412604SGeorge.Shepherd@Sun.COM 	/* point to the head of the reassembled data message */
12253845Svi117747 	dmp = dmp->b_cont;
122612604SGeorge.Shepherd@Sun.COM 	reassq_curr->b_cont = NULL;
122712604SGeorge.Shepherd@Sun.COM 	freeb(reassq_curr);
122812604SGeorge.Shepherd@Sun.COM 	/* Tell our caller that we are returning a complete message. */
12293845Svi117747 	*tpfinished = B_TRUE;
12303845Svi117747 
12310Sstevel@tonic-gate 	/*
12320Sstevel@tonic-gate 	 * Adjust all mblk's except the lead so their rptr's point to the
123312604SGeorge.Shepherd@Sun.COM 	 * payload. sctp_data_chunk() will need to process the lead's data
123412604SGeorge.Shepherd@Sun.COM 	 * data chunk section, so leave its rptr pointing at the data chunk
123512604SGeorge.Shepherd@Sun.COM 	 * header.
12360Sstevel@tonic-gate 	 */
12370Sstevel@tonic-gate 	*dc = (sctp_data_hdr_t *)dmp->b_rptr;
12383845Svi117747 	for (qmp = dmp->b_cont; qmp != NULL; qmp = qmp->b_cont) {
12390Sstevel@tonic-gate 		qdc = (sctp_data_hdr_t *)qmp->b_rptr;
12400Sstevel@tonic-gate 		qmp->b_rptr = (uchar_t *)(qdc + 1);
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_reassmsgs);
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	return (dmp);
12450Sstevel@tonic-gate }
124612604SGeorge.Shepherd@Sun.COM 
12470Sstevel@tonic-gate static void
sctp_add_dup(uint32_t tsn,mblk_t ** dups)12480Sstevel@tonic-gate sctp_add_dup(uint32_t tsn, mblk_t **dups)
12490Sstevel@tonic-gate {
12500Sstevel@tonic-gate 	mblk_t *mp;
12510Sstevel@tonic-gate 	size_t bsize = SCTP_DUP_MBLK_SZ * sizeof (tsn);
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	if (dups == NULL) {
12540Sstevel@tonic-gate 		return;
12550Sstevel@tonic-gate 	}
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	/* first time? */
12580Sstevel@tonic-gate 	if (*dups == NULL) {
12590Sstevel@tonic-gate 		*dups = allocb(bsize, BPRI_MED);
12600Sstevel@tonic-gate 		if (*dups == NULL) {
12610Sstevel@tonic-gate 			return;
12620Sstevel@tonic-gate 		}
12630Sstevel@tonic-gate 	}
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	mp = *dups;
12660Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) >= bsize) {
12670Sstevel@tonic-gate 		/* maximum reached */
12680Sstevel@tonic-gate 		return;
12690Sstevel@tonic-gate 	}
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	/* add the duplicate tsn */
12720Sstevel@tonic-gate 	bcopy(&tsn, mp->b_wptr, sizeof (tsn));
12730Sstevel@tonic-gate 	mp->b_wptr += sizeof (tsn);
12740Sstevel@tonic-gate 	ASSERT((mp->b_wptr - mp->b_rptr) <= bsize);
12750Sstevel@tonic-gate }
12760Sstevel@tonic-gate 
127712604SGeorge.Shepherd@Sun.COM /*
127812604SGeorge.Shepherd@Sun.COM  * All incoming sctp data, complete messages and fragments are handled by
127912604SGeorge.Shepherd@Sun.COM  * this function. Unless the U-bit is set in the data chunk it will be
128012604SGeorge.Shepherd@Sun.COM  * delivered in order or queued until an in-order delivery can be made.
128112604SGeorge.Shepherd@Sun.COM  */
12820Sstevel@tonic-gate static void
sctp_data_chunk(sctp_t * sctp,sctp_chunk_hdr_t * ch,mblk_t * mp,mblk_t ** dups,sctp_faddr_t * fp,ip_pkt_t * ipp,ip_recv_attr_t * ira)12830Sstevel@tonic-gate sctp_data_chunk(sctp_t *sctp, sctp_chunk_hdr_t *ch, mblk_t *mp, mblk_t **dups,
128411042SErik.Nordmark@Sun.COM     sctp_faddr_t *fp, ip_pkt_t *ipp, ip_recv_attr_t *ira)
12850Sstevel@tonic-gate {
12860Sstevel@tonic-gate 	sctp_data_hdr_t *dc;
12870Sstevel@tonic-gate 	mblk_t *dmp, *pmp;
12880Sstevel@tonic-gate 	sctp_instr_t *instr;
12890Sstevel@tonic-gate 	int ubit;
129012604SGeorge.Shepherd@Sun.COM 	int sid;
12910Sstevel@tonic-gate 	int isfrag;
12920Sstevel@tonic-gate 	uint16_t ssn;
12930Sstevel@tonic-gate 	uint32_t oftsn;
12940Sstevel@tonic-gate 	boolean_t can_deliver = B_TRUE;
12950Sstevel@tonic-gate 	uint32_t tsn;
12960Sstevel@tonic-gate 	int dlen;
12973845Svi117747 	boolean_t tpfinished = B_TRUE;
12983448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
12998348SEric.Yu@Sun.COM 	int	error;
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 	/* The following are used multiple times, so we inline them */
13020Sstevel@tonic-gate #define	SCTP_ACK_IT(sctp, tsn)						\
13030Sstevel@tonic-gate 	if (tsn == sctp->sctp_ftsn) {					\
13040Sstevel@tonic-gate 		dprint(2, ("data_chunk: acking next %x\n", tsn));	\
13051932Svi117747 		(sctp)->sctp_ftsn++;					\
13061932Svi117747 		if ((sctp)->sctp_sack_gaps > 0)				\
13071932Svi117747 			(sctp)->sctp_force_sack = 1;			\
13080Sstevel@tonic-gate 	} else if (SEQ_GT(tsn, sctp->sctp_ftsn)) {			\
13090Sstevel@tonic-gate 		/* Got a gap; record it */				\
131010212SGeorge.Shepherd@Sun.COM 		BUMP_LOCAL(sctp->sctp_outseqtsns);			\
13110Sstevel@tonic-gate 		dprint(2, ("data_chunk: acking gap %x\n", tsn));	\
13121932Svi117747 		sctp_ack_add(&sctp->sctp_sack_info, tsn,		\
13131932Svi117747 		    &sctp->sctp_sack_gaps);				\
13140Sstevel@tonic-gate 		sctp->sctp_force_sack = 1;				\
13150Sstevel@tonic-gate 	}
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	dmp = NULL;
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 	dc = (sctp_data_hdr_t *)ch;
13200Sstevel@tonic-gate 	tsn = ntohl(dc->sdh_tsn);
13210Sstevel@tonic-gate 
13221676Sjpk 	dprint(3, ("sctp_data_chunk: mp=%p tsn=%x\n", (void *)mp, tsn));
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	/* Check for duplicates */
13250Sstevel@tonic-gate 	if (SEQ_LT(tsn, sctp->sctp_ftsn)) {
13260Sstevel@tonic-gate 		dprint(4, ("sctp_data_chunk: dropping duplicate\n"));
132710212SGeorge.Shepherd@Sun.COM 		BUMP_LOCAL(sctp->sctp_idupchunks);
13280Sstevel@tonic-gate 		sctp->sctp_force_sack = 1;
13290Sstevel@tonic-gate 		sctp_add_dup(dc->sdh_tsn, dups);
13300Sstevel@tonic-gate 		return;
13310Sstevel@tonic-gate 	}
13320Sstevel@tonic-gate 
133312604SGeorge.Shepherd@Sun.COM 	/* Check for dups of sack'ed data */
13340Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
13350Sstevel@tonic-gate 		sctp_set_t *sp;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 		for (sp = sctp->sctp_sack_info; sp; sp = sp->next) {
13380Sstevel@tonic-gate 			if (SEQ_GEQ(tsn, sp->begin) && SEQ_LEQ(tsn, sp->end)) {
13390Sstevel@tonic-gate 				dprint(4,
13404964Skcpoon 				    ("sctp_data_chunk: dropping dup > "
13414964Skcpoon 				    "cumtsn\n"));
134210212SGeorge.Shepherd@Sun.COM 				BUMP_LOCAL(sctp->sctp_idupchunks);
13430Sstevel@tonic-gate 				sctp->sctp_force_sack = 1;
13440Sstevel@tonic-gate 				sctp_add_dup(dc->sdh_tsn, dups);
13450Sstevel@tonic-gate 				return;
13460Sstevel@tonic-gate 			}
13470Sstevel@tonic-gate 		}
13480Sstevel@tonic-gate 	}
13490Sstevel@tonic-gate 
135012604SGeorge.Shepherd@Sun.COM 	/* We can no longer deliver anything up, but still need to handle it. */
13510Sstevel@tonic-gate 	if (SCTP_IS_DETACHED(sctp)) {
135212869SKacheong.Poon@Sun.COM 		SCTPS_BUMP_MIB(sctps, sctpInClosed);
13530Sstevel@tonic-gate 		can_deliver = B_FALSE;
13540Sstevel@tonic-gate 	}
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	dlen = ntohs(dc->sdh_len) - sizeof (*dc);
13570Sstevel@tonic-gate 
135810828SGeorge.Shepherd@Sun.COM 	/*
135910828SGeorge.Shepherd@Sun.COM 	 * Check for buffer space. Note if this is the next expected TSN
136010828SGeorge.Shepherd@Sun.COM 	 * we have to take it to avoid deadlock because we cannot deliver
136110828SGeorge.Shepherd@Sun.COM 	 * later queued TSNs and thus clear buffer space without it.
136210828SGeorge.Shepherd@Sun.COM 	 * We drop anything that is purely zero window probe data here.
136310828SGeorge.Shepherd@Sun.COM 	 */
136410828SGeorge.Shepherd@Sun.COM 	if ((sctp->sctp_rwnd - sctp->sctp_rxqueued < dlen) &&
136510828SGeorge.Shepherd@Sun.COM 	    (tsn != sctp->sctp_ftsn || sctp->sctp_rwnd == 0)) {
13660Sstevel@tonic-gate 		/* Drop and SACK, but don't advance the cumulative TSN. */
13670Sstevel@tonic-gate 		sctp->sctp_force_sack = 1;
13680Sstevel@tonic-gate 		dprint(0, ("sctp_data_chunk: exceed rwnd %d rxqueued %d "
13693795Skcpoon 		    "dlen %d ssn %d tsn %x\n", sctp->sctp_rwnd,
13703795Skcpoon 		    sctp->sctp_rxqueued, dlen, ntohs(dc->sdh_ssn),
13713795Skcpoon 		    ntohl(dc->sdh_tsn)));
13720Sstevel@tonic-gate 		return;
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 
137512604SGeorge.Shepherd@Sun.COM 	sid = ntohs(dc->sdh_sid);
137612604SGeorge.Shepherd@Sun.COM 
137712604SGeorge.Shepherd@Sun.COM 	/* Data received for a stream not negotiated for this association */
137812604SGeorge.Shepherd@Sun.COM 	if (sid >= sctp->sctp_num_istr) {
13799451SGeorge.Shepherd@Sun.COM 		sctp_bsc_t	inval_parm;
13809451SGeorge.Shepherd@Sun.COM 
13819451SGeorge.Shepherd@Sun.COM 		/* Will populate the CAUSE block in the ERROR chunk. */
13829451SGeorge.Shepherd@Sun.COM 		inval_parm.bsc_sid = dc->sdh_sid;
13839451SGeorge.Shepherd@Sun.COM 		/* RESERVED, ignored at the receiving end */
13849451SGeorge.Shepherd@Sun.COM 		inval_parm.bsc_pad = 0;
13859451SGeorge.Shepherd@Sun.COM 
13860Sstevel@tonic-gate 		/* ack and drop it */
13879451SGeorge.Shepherd@Sun.COM 		sctp_add_err(sctp, SCTP_ERR_BAD_SID, (void *)&inval_parm,
13889451SGeorge.Shepherd@Sun.COM 		    sizeof (sctp_bsc_t), fp);
13890Sstevel@tonic-gate 		SCTP_ACK_IT(sctp, tsn);
13900Sstevel@tonic-gate 		return;
13910Sstevel@tonic-gate 	}
13920Sstevel@tonic-gate 
139312604SGeorge.Shepherd@Sun.COM 	/* unordered delivery OK for this data if ubit set */
13940Sstevel@tonic-gate 	ubit = SCTP_DATA_GET_UBIT(dc);
13950Sstevel@tonic-gate 	ASSERT(sctp->sctp_instr != NULL);
139612604SGeorge.Shepherd@Sun.COM 
139712604SGeorge.Shepherd@Sun.COM 	/* select per stream structure for this stream from the array */
139812604SGeorge.Shepherd@Sun.COM 	instr = &sctp->sctp_instr[sid];
13990Sstevel@tonic-gate 	/* Initialize the stream, if not yet used */
14000Sstevel@tonic-gate 	if (instr->sctp == NULL)
14010Sstevel@tonic-gate 		instr->sctp = sctp;
14023845Svi117747 
140312604SGeorge.Shepherd@Sun.COM 	/* Begin and End bit set would mean a complete message */
14040Sstevel@tonic-gate 	isfrag = !(SCTP_DATA_GET_BBIT(dc) && SCTP_DATA_GET_EBIT(dc));
140512604SGeorge.Shepherd@Sun.COM 
140612604SGeorge.Shepherd@Sun.COM 	/* The ssn of this sctp message and of any fragments in it */
14070Sstevel@tonic-gate 	ssn = ntohs(dc->sdh_ssn);
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	dmp = dupb(mp);
14100Sstevel@tonic-gate 	if (dmp == NULL) {
141112604SGeorge.Shepherd@Sun.COM 		/* drop it and don't ack, let the peer retransmit */
14120Sstevel@tonic-gate 		return;
14130Sstevel@tonic-gate 	}
141412604SGeorge.Shepherd@Sun.COM 	/*
141512604SGeorge.Shepherd@Sun.COM 	 * Past header and payload, note: the underlying buffer may
141612604SGeorge.Shepherd@Sun.COM 	 * contain further chunks from the same incoming IP packet,
141712604SGeorge.Shepherd@Sun.COM 	 * if so db_ref will be greater than one.
141812604SGeorge.Shepherd@Sun.COM 	 */
14190Sstevel@tonic-gate 	dmp->b_wptr = (uchar_t *)ch + ntohs(ch->sch_len);
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	sctp->sctp_rxqueued += dlen;
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	oftsn = sctp->sctp_ftsn;
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	if (isfrag) {
14268348SEric.Yu@Sun.COM 
14278348SEric.Yu@Sun.COM 		error = 0;
14280Sstevel@tonic-gate 		/* fragmented data chunk */
14290Sstevel@tonic-gate 		dmp->b_rptr = (uchar_t *)dc;
14300Sstevel@tonic-gate 		if (ubit) {
143112604SGeorge.Shepherd@Sun.COM 			/* prepare data for unordered delivery */
14320Sstevel@tonic-gate 			dmp = sctp_uodata_frag(sctp, dmp, &dc);
14330Sstevel@tonic-gate #if	DEBUG
14340Sstevel@tonic-gate 			if (dmp != NULL) {
14350Sstevel@tonic-gate 				ASSERT(instr ==
143612604SGeorge.Shepherd@Sun.COM 				    &sctp->sctp_instr[sid]);
14370Sstevel@tonic-gate 			}
14380Sstevel@tonic-gate #endif
14390Sstevel@tonic-gate 		} else {
144012604SGeorge.Shepherd@Sun.COM 			/*
144112604SGeorge.Shepherd@Sun.COM 			 * Assemble fragments and queue for ordered delivery,
144212604SGeorge.Shepherd@Sun.COM 			 * dmp returned is NULL or the head of a complete or
144312604SGeorge.Shepherd@Sun.COM 			 * "partial delivery" message. Any returned message
144412604SGeorge.Shepherd@Sun.COM 			 * and all its fragments will have the same ssn as the
144512604SGeorge.Shepherd@Sun.COM 			 * input fragment currently being handled.
144612604SGeorge.Shepherd@Sun.COM 			 */
14470Sstevel@tonic-gate 			dmp = sctp_data_frag(sctp, dmp, &dc, &error, instr,
14483845Svi117747 			    &tpfinished);
14490Sstevel@tonic-gate 		}
145012604SGeorge.Shepherd@Sun.COM 		if (error == ENOMEM) {
145112604SGeorge.Shepherd@Sun.COM 			/* back out the adjustment made earlier */
14520Sstevel@tonic-gate 			sctp->sctp_rxqueued -= dlen;
145312604SGeorge.Shepherd@Sun.COM 			/*
145412604SGeorge.Shepherd@Sun.COM 			 * Don't ack the segment,
145512604SGeorge.Shepherd@Sun.COM 			 * the peer will retransmit.
145612604SGeorge.Shepherd@Sun.COM 			 */
145712604SGeorge.Shepherd@Sun.COM 			return;
14580Sstevel@tonic-gate 		}
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 		if (dmp == NULL) {
14610Sstevel@tonic-gate 			/*
146212604SGeorge.Shepherd@Sun.COM 			 * The frag has been queued for later in-order delivery,
146312604SGeorge.Shepherd@Sun.COM 			 * but the cumulative TSN may need to advance, so also
146412604SGeorge.Shepherd@Sun.COM 			 * need to perform the gap ack checks at the done label.
14650Sstevel@tonic-gate 			 */
14660Sstevel@tonic-gate 			SCTP_ACK_IT(sctp, tsn);
146712604SGeorge.Shepherd@Sun.COM 			DTRACE_PROBE4(sctp_data_frag_queued, sctp_t *, sctp,
146812604SGeorge.Shepherd@Sun.COM 			    int, sid, int, tsn, uint16_t, ssn);
14690Sstevel@tonic-gate 			goto done;
14700Sstevel@tonic-gate 		}
14710Sstevel@tonic-gate 	}
14720Sstevel@tonic-gate 
14736374Sgeorges 	/*
147412604SGeorge.Shepherd@Sun.COM 	 * Unless message is the next for delivery to the ulp, queue complete
147512604SGeorge.Shepherd@Sun.COM 	 * message in the correct order for ordered delivery.
147612604SGeorge.Shepherd@Sun.COM 	 * Note: tpfinished is true when the incoming chunk contains a complete
14776374Sgeorges 	 * message or is the final missing fragment which completed a message.
14786374Sgeorges 	 */
14796374Sgeorges 	if (!ubit && tpfinished && ssn != instr->nextseq) {
14800Sstevel@tonic-gate 		/* Adjust rptr to point at the data chunk for compares */
14810Sstevel@tonic-gate 		dmp->b_rptr = (uchar_t *)dc;
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 		dprint(2,
14840Sstevel@tonic-gate 		    ("data_chunk: inserted %x in pq (ssn %d expected %d)\n",
14850Sstevel@tonic-gate 		    ntohl(dc->sdh_tsn), (int)(ssn), (int)(instr->nextseq)));
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 		if (instr->istr_msgs == NULL) {
14880Sstevel@tonic-gate 			instr->istr_msgs = dmp;
14890Sstevel@tonic-gate 			ASSERT(dmp->b_prev == NULL && dmp->b_next == NULL);
14900Sstevel@tonic-gate 		} else {
14910Sstevel@tonic-gate 			mblk_t			*imblk = instr->istr_msgs;
14920Sstevel@tonic-gate 			sctp_data_hdr_t		*idc;
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 			/*
14950Sstevel@tonic-gate 			 * XXXNeed to take sequence wraps into account,
14960Sstevel@tonic-gate 			 * ... and a more efficient insertion algo.
14970Sstevel@tonic-gate 			 */
14980Sstevel@tonic-gate 			for (;;) {
14990Sstevel@tonic-gate 				idc = (sctp_data_hdr_t *)imblk->b_rptr;
15000Sstevel@tonic-gate 				if (SSN_GT(ntohs(idc->sdh_ssn),
15014964Skcpoon 				    ntohs(dc->sdh_ssn))) {
15020Sstevel@tonic-gate 					if (instr->istr_msgs == imblk) {
15030Sstevel@tonic-gate 						instr->istr_msgs = dmp;
15040Sstevel@tonic-gate 						dmp->b_next = imblk;
15050Sstevel@tonic-gate 						imblk->b_prev = dmp;
15060Sstevel@tonic-gate 					} else {
15070Sstevel@tonic-gate 						ASSERT(imblk->b_prev != NULL);
15080Sstevel@tonic-gate 						imblk->b_prev->b_next = dmp;
15090Sstevel@tonic-gate 						dmp->b_prev = imblk->b_prev;
15100Sstevel@tonic-gate 						imblk->b_prev = dmp;
15110Sstevel@tonic-gate 						dmp->b_next = imblk;
15120Sstevel@tonic-gate 					}
15130Sstevel@tonic-gate 					break;
15140Sstevel@tonic-gate 				}
15150Sstevel@tonic-gate 				if (imblk->b_next == NULL) {
15160Sstevel@tonic-gate 					imblk->b_next = dmp;
15170Sstevel@tonic-gate 					dmp->b_prev = imblk;
15180Sstevel@tonic-gate 					break;
15190Sstevel@tonic-gate 				}
15200Sstevel@tonic-gate 				imblk = imblk->b_next;
15210Sstevel@tonic-gate 			}
15220Sstevel@tonic-gate 		}
15230Sstevel@tonic-gate 		(instr->istr_nmsgs)++;
15240Sstevel@tonic-gate 		(sctp->sctp_istr_nmsgs)++;
15250Sstevel@tonic-gate 		SCTP_ACK_IT(sctp, tsn);
152612604SGeorge.Shepherd@Sun.COM 		DTRACE_PROBE4(sctp_pqueue_completemsg, sctp_t *, sctp,
152712604SGeorge.Shepherd@Sun.COM 		    int, sid, int, tsn, uint16_t, ssn);
15280Sstevel@tonic-gate 		return;
15290Sstevel@tonic-gate 	}
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 	/*
153212604SGeorge.Shepherd@Sun.COM 	 * Deliver the data directly. Recalculate dlen now since
153312604SGeorge.Shepherd@Sun.COM 	 * we may have just reassembled this data.
15340Sstevel@tonic-gate 	 */
15350Sstevel@tonic-gate 	dlen = dmp->b_wptr - (uchar_t *)dc - sizeof (*dc);
15360Sstevel@tonic-gate 	for (pmp = dmp->b_cont; pmp != NULL; pmp = pmp->b_cont)
153710828SGeorge.Shepherd@Sun.COM 		dlen += MBLKL(pmp);
15380Sstevel@tonic-gate 	ASSERT(sctp->sctp_rxqueued >= dlen);
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	/* Deliver the message. */
15410Sstevel@tonic-gate 	sctp->sctp_rxqueued -= dlen;
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	if (can_deliver) {
154412604SGeorge.Shepherd@Sun.COM 		/* step past header to the payload */
15450Sstevel@tonic-gate 		dmp->b_rptr = (uchar_t *)(dc + 1);
154611042SErik.Nordmark@Sun.COM 		if (sctp_input_add_ancillary(sctp, &dmp, dc, fp,
154711042SErik.Nordmark@Sun.COM 		    ipp, ira) == 0) {
15480Sstevel@tonic-gate 			dprint(1, ("sctp_data_chunk: delivering %lu bytes\n",
15490Sstevel@tonic-gate 			    msgdsize(dmp)));
15508348SEric.Yu@Sun.COM 			/*
155112604SGeorge.Shepherd@Sun.COM 			 * We overload the meaning of b_flag for SCTP sockfs
155212604SGeorge.Shepherd@Sun.COM 			 * internal use, to advise sockfs of partial delivery
155312604SGeorge.Shepherd@Sun.COM 			 * semantics.
15548348SEric.Yu@Sun.COM 			 */
15558348SEric.Yu@Sun.COM 			dmp->b_flag = tpfinished ? 0 : SCTP_PARTIAL_DATA;
1556*13054SKacheong.Poon@Sun.COM 			if (sctp->sctp_flowctrld) {
1557*13054SKacheong.Poon@Sun.COM 				sctp->sctp_rwnd -= dlen;
1558*13054SKacheong.Poon@Sun.COM 				if (sctp->sctp_rwnd < 0)
1559*13054SKacheong.Poon@Sun.COM 					sctp->sctp_rwnd = 0;
1560*13054SKacheong.Poon@Sun.COM 			}
1561*13054SKacheong.Poon@Sun.COM 			if (sctp->sctp_ulp_recv(sctp->sctp_ulpd, dmp,
1562*13054SKacheong.Poon@Sun.COM 			    msgdsize(dmp), 0, &error, NULL) <= 0) {
1563*13054SKacheong.Poon@Sun.COM 				sctp->sctp_flowctrld = B_TRUE;
1564*13054SKacheong.Poon@Sun.COM 			}
15650Sstevel@tonic-gate 			SCTP_ACK_IT(sctp, tsn);
15660Sstevel@tonic-gate 		} else {
156712604SGeorge.Shepherd@Sun.COM 			/* No memory don't ack, the peer will retransmit. */
15680Sstevel@tonic-gate 			freemsg(dmp);
15690Sstevel@tonic-gate 			return;
15700Sstevel@tonic-gate 		}
15710Sstevel@tonic-gate 	} else {
157212604SGeorge.Shepherd@Sun.COM 		/* Closed above, ack to peer and free the data */
15730Sstevel@tonic-gate 		freemsg(dmp);
15740Sstevel@tonic-gate 		SCTP_ACK_IT(sctp, tsn);
15750Sstevel@tonic-gate 	}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	/*
157812604SGeorge.Shepherd@Sun.COM 	 * Data now enqueued, may already have been processed and free'd
15790Sstevel@tonic-gate 	 * by the ULP (or we may have just freed it above, if we could not
158012604SGeorge.Shepherd@Sun.COM 	 * deliver), so we must not reference it (this is why we saved the
158112604SGeorge.Shepherd@Sun.COM 	 * ssn and ubit earlier).
15820Sstevel@tonic-gate 	 */
15830Sstevel@tonic-gate 	if (ubit != 0) {
15840Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_iudchunks);
15850Sstevel@tonic-gate 		goto done;
15860Sstevel@tonic-gate 	}
15870Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_idchunks);
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	/*
159012604SGeorge.Shepherd@Sun.COM 	 * There was a partial delivery and it has not finished,
159112604SGeorge.Shepherd@Sun.COM 	 * don't pull anything from the pqueues or increment the
159212604SGeorge.Shepherd@Sun.COM 	 * nextseq. This msg must complete before starting on
159312604SGeorge.Shepherd@Sun.COM 	 * the next ssn and the partial message must have the
159412604SGeorge.Shepherd@Sun.COM 	 * same ssn as the next expected message..
15950Sstevel@tonic-gate 	 */
15960Sstevel@tonic-gate 	if (!tpfinished) {
159712604SGeorge.Shepherd@Sun.COM 		DTRACE_PROBE4(sctp_partial_delivery, sctp_t *, sctp,
159812604SGeorge.Shepherd@Sun.COM 		    int, sid, int, tsn, uint16_t, ssn);
159912604SGeorge.Shepherd@Sun.COM 		/*
160012604SGeorge.Shepherd@Sun.COM 		 * Verify the partial delivery is part of the
160112604SGeorge.Shepherd@Sun.COM 		 * message expected for ordered delivery.
160212604SGeorge.Shepherd@Sun.COM 		 */
160312604SGeorge.Shepherd@Sun.COM 		if (ssn != instr->nextseq) {
160412604SGeorge.Shepherd@Sun.COM 			DTRACE_PROBE4(sctp_partial_delivery_error,
160512604SGeorge.Shepherd@Sun.COM 			    sctp_t *, sctp, int, sid, int, tsn,
160612604SGeorge.Shepherd@Sun.COM 			    uint16_t, ssn);
160712604SGeorge.Shepherd@Sun.COM 			cmn_err(CE_WARN, "sctp partial"
160812604SGeorge.Shepherd@Sun.COM 			    " delivery error, sctp 0x%p"
160912604SGeorge.Shepherd@Sun.COM 			    " sid = 0x%x ssn != nextseq"
161012604SGeorge.Shepherd@Sun.COM 			    " tsn 0x%x ftsn 0x%x"
161112604SGeorge.Shepherd@Sun.COM 			    " ssn 0x%x nextseq 0x%x",
161212604SGeorge.Shepherd@Sun.COM 			    (void *)sctp, sid,
161312604SGeorge.Shepherd@Sun.COM 			    tsn, sctp->sctp_ftsn, ssn,
161412604SGeorge.Shepherd@Sun.COM 			    instr->nextseq);
161512604SGeorge.Shepherd@Sun.COM 		}
161612604SGeorge.Shepherd@Sun.COM 
161712604SGeorge.Shepherd@Sun.COM 		ASSERT(ssn == instr->nextseq);
16180Sstevel@tonic-gate 		goto done;
16190Sstevel@tonic-gate 	}
16200Sstevel@tonic-gate 
162112604SGeorge.Shepherd@Sun.COM 	if (ssn != instr->nextseq) {
162212604SGeorge.Shepherd@Sun.COM 		DTRACE_PROBE4(sctp_inorder_delivery_error,
162312604SGeorge.Shepherd@Sun.COM 		    sctp_t *, sctp, int, sid, int, tsn,
162412604SGeorge.Shepherd@Sun.COM 		    uint16_t, ssn);
162512604SGeorge.Shepherd@Sun.COM 		cmn_err(CE_WARN, "sctp in-order delivery error, sctp 0x%p "
162612604SGeorge.Shepherd@Sun.COM 		    "sid = 0x%x ssn != nextseq ssn 0x%x nextseq 0x%x",
162712604SGeorge.Shepherd@Sun.COM 		    (void *)sctp, sid, ssn, instr->nextseq);
162812604SGeorge.Shepherd@Sun.COM 	}
162912604SGeorge.Shepherd@Sun.COM 
163012604SGeorge.Shepherd@Sun.COM 	ASSERT(ssn == instr->nextseq);
163112604SGeorge.Shepherd@Sun.COM 
163212604SGeorge.Shepherd@Sun.COM 	DTRACE_PROBE4(sctp_deliver_completemsg, sctp_t *, sctp, int, sid,
163312604SGeorge.Shepherd@Sun.COM 	    int, tsn, uint16_t, ssn);
163412604SGeorge.Shepherd@Sun.COM 
16350Sstevel@tonic-gate 	instr->nextseq = ssn + 1;
163612604SGeorge.Shepherd@Sun.COM 
163712604SGeorge.Shepherd@Sun.COM 	/*
163812604SGeorge.Shepherd@Sun.COM 	 * Deliver any successive data chunks waiting in the instr pqueue
163912604SGeorge.Shepherd@Sun.COM 	 * for the data just sent up.
164012604SGeorge.Shepherd@Sun.COM 	 */
16410Sstevel@tonic-gate 	while (instr->istr_nmsgs > 0) {
16420Sstevel@tonic-gate 		dmp = (mblk_t *)instr->istr_msgs;
16430Sstevel@tonic-gate 		dc = (sctp_data_hdr_t *)dmp->b_rptr;
16440Sstevel@tonic-gate 		ssn = ntohs(dc->sdh_ssn);
164512604SGeorge.Shepherd@Sun.COM 		tsn = ntohl(dc->sdh_tsn);
164612604SGeorge.Shepherd@Sun.COM 		/* Stop at the first gap in the sequence */
16470Sstevel@tonic-gate 		if (ssn != instr->nextseq)
16480Sstevel@tonic-gate 			break;
16490Sstevel@tonic-gate 
165012604SGeorge.Shepherd@Sun.COM 		DTRACE_PROBE4(sctp_deliver_pqueuedmsg, sctp_t *, sctp,
165112604SGeorge.Shepherd@Sun.COM 		    int, sid, int, tsn, uint16_t, ssn);
165212604SGeorge.Shepherd@Sun.COM 		/*
165312604SGeorge.Shepherd@Sun.COM 		 * Ready to deliver all data before the gap
165412604SGeorge.Shepherd@Sun.COM 		 * to the upper layer.
165512604SGeorge.Shepherd@Sun.COM 		 */
16560Sstevel@tonic-gate 		(instr->istr_nmsgs)--;
16570Sstevel@tonic-gate 		(instr->nextseq)++;
16580Sstevel@tonic-gate 		(sctp->sctp_istr_nmsgs)--;
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 		instr->istr_msgs = instr->istr_msgs->b_next;
16610Sstevel@tonic-gate 		if (instr->istr_msgs != NULL)
16620Sstevel@tonic-gate 			instr->istr_msgs->b_prev = NULL;
16630Sstevel@tonic-gate 		dmp->b_next = dmp->b_prev = NULL;
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 		dprint(2, ("data_chunk: pulling %x from pq (ssn %d)\n",
16660Sstevel@tonic-gate 		    ntohl(dc->sdh_tsn), (int)ssn));
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 		/*
166912604SGeorge.Shepherd@Sun.COM 		 * Composite messages indicate this chunk was reassembled,
167012604SGeorge.Shepherd@Sun.COM 		 * each b_cont represents another TSN; Follow the chain to
167112604SGeorge.Shepherd@Sun.COM 		 * reach the frag with the last tsn in order to advance ftsn
167212604SGeorge.Shepherd@Sun.COM 		 * shortly by calling SCTP_ACK_IT().
16730Sstevel@tonic-gate 		 */
16740Sstevel@tonic-gate 		dlen = dmp->b_wptr - dmp->b_rptr - sizeof (*dc);
16750Sstevel@tonic-gate 		for (pmp = dmp->b_cont; pmp; pmp = pmp->b_cont)
167610828SGeorge.Shepherd@Sun.COM 			dlen += MBLKL(pmp);
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 		ASSERT(sctp->sctp_rxqueued >= dlen);
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 		sctp->sctp_rxqueued -= dlen;
16810Sstevel@tonic-gate 		if (can_deliver) {
16820Sstevel@tonic-gate 			dmp->b_rptr = (uchar_t *)(dc + 1);
16830Sstevel@tonic-gate 			if (sctp_input_add_ancillary(sctp, &dmp, dc, fp,
168411042SErik.Nordmark@Sun.COM 			    ipp, ira) == 0) {
16850Sstevel@tonic-gate 				dprint(1, ("sctp_data_chunk: delivering %lu "
16860Sstevel@tonic-gate 				    "bytes\n", msgdsize(dmp)));
16878348SEric.Yu@Sun.COM 				/*
168812604SGeorge.Shepherd@Sun.COM 				 * Meaning of b_flag overloaded for SCTP sockfs
168912604SGeorge.Shepherd@Sun.COM 				 * internal use, advise sockfs of partial
169012604SGeorge.Shepherd@Sun.COM 				 * delivery semantics.
16918348SEric.Yu@Sun.COM 				 */
16928348SEric.Yu@Sun.COM 				dmp->b_flag = tpfinished ?
16938348SEric.Yu@Sun.COM 				    0 : SCTP_PARTIAL_DATA;
1694*13054SKacheong.Poon@Sun.COM 				if (sctp->sctp_flowctrld) {
1695*13054SKacheong.Poon@Sun.COM 					sctp->sctp_rwnd -= dlen;
1696*13054SKacheong.Poon@Sun.COM 					if (sctp->sctp_rwnd < 0)
1697*13054SKacheong.Poon@Sun.COM 						sctp->sctp_rwnd = 0;
1698*13054SKacheong.Poon@Sun.COM 				}
1699*13054SKacheong.Poon@Sun.COM 				if (sctp->sctp_ulp_recv(sctp->sctp_ulpd, dmp,
1700*13054SKacheong.Poon@Sun.COM 				    msgdsize(dmp), 0, &error, NULL) <= 0) {
1701*13054SKacheong.Poon@Sun.COM 					sctp->sctp_flowctrld = B_TRUE;
1702*13054SKacheong.Poon@Sun.COM 				}
17030Sstevel@tonic-gate 				SCTP_ACK_IT(sctp, tsn);
17040Sstevel@tonic-gate 			} else {
170512604SGeorge.Shepherd@Sun.COM 				/* don't ack, the peer will retransmit */
17060Sstevel@tonic-gate 				freemsg(dmp);
17070Sstevel@tonic-gate 				return;
17080Sstevel@tonic-gate 			}
17090Sstevel@tonic-gate 		} else {
171012604SGeorge.Shepherd@Sun.COM 			/* Closed above, ack and free the data */
17110Sstevel@tonic-gate 			freemsg(dmp);
17120Sstevel@tonic-gate 			SCTP_ACK_IT(sctp, tsn);
17130Sstevel@tonic-gate 		}
17140Sstevel@tonic-gate 	}
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate done:
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	/*
17190Sstevel@tonic-gate 	 * If there are gap reports pending, check if advancing
17200Sstevel@tonic-gate 	 * the ftsn here closes a gap. If so, we can advance
17210Sstevel@tonic-gate 	 * ftsn to the end of the set.
17220Sstevel@tonic-gate 	 */
17230Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL &&
17240Sstevel@tonic-gate 	    sctp->sctp_ftsn == sctp->sctp_sack_info->begin) {
17250Sstevel@tonic-gate 		sctp->sctp_ftsn = sctp->sctp_sack_info->end + 1;
17260Sstevel@tonic-gate 	}
17270Sstevel@tonic-gate 	/*
17280Sstevel@tonic-gate 	 * If ftsn has moved forward, maybe we can remove gap reports.
17290Sstevel@tonic-gate 	 * NB: dmp may now be NULL, so don't dereference it here.
17300Sstevel@tonic-gate 	 */
17310Sstevel@tonic-gate 	if (oftsn != sctp->sctp_ftsn && sctp->sctp_sack_info != NULL) {
17320Sstevel@tonic-gate 		sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1,
17330Sstevel@tonic-gate 		    &sctp->sctp_sack_gaps);
17340Sstevel@tonic-gate 		dprint(2, ("data_chunk: removed acks before %x (num=%d)\n",
17350Sstevel@tonic-gate 		    sctp->sctp_ftsn - 1, sctp->sctp_sack_gaps));
17360Sstevel@tonic-gate 	}
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate #ifdef	DEBUG
17390Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
17400Sstevel@tonic-gate 		ASSERT(sctp->sctp_ftsn != sctp->sctp_sack_info->begin);
17410Sstevel@tonic-gate 	}
17420Sstevel@tonic-gate #endif
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate #undef	SCTP_ACK_IT
17450Sstevel@tonic-gate }
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate void
sctp_fill_sack(sctp_t * sctp,unsigned char * dst,int sacklen)17480Sstevel@tonic-gate sctp_fill_sack(sctp_t *sctp, unsigned char *dst, int sacklen)
17490Sstevel@tonic-gate {
17500Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
17510Sstevel@tonic-gate 	sctp_sack_chunk_t *sc;
17520Sstevel@tonic-gate 	sctp_sack_frag_t *sf;
17530Sstevel@tonic-gate 	uint16_t num_gaps = sctp->sctp_sack_gaps;
17540Sstevel@tonic-gate 	sctp_set_t *sp;
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	/* Chunk hdr */
17570Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)dst;
17580Sstevel@tonic-gate 	sch->sch_id = CHUNK_SACK;
17590Sstevel@tonic-gate 	sch->sch_flags = 0;
17600Sstevel@tonic-gate 	sch->sch_len = htons(sacklen);
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	/* SACK chunk */
17630Sstevel@tonic-gate 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	sc = (sctp_sack_chunk_t *)(sch + 1);
17660Sstevel@tonic-gate 	sc->ssc_cumtsn = htonl(sctp->sctp_lastacked);
17670Sstevel@tonic-gate 	if (sctp->sctp_rxqueued < sctp->sctp_rwnd) {
17680Sstevel@tonic-gate 		sc->ssc_a_rwnd = htonl(sctp->sctp_rwnd - sctp->sctp_rxqueued);
17690Sstevel@tonic-gate 	} else {
17700Sstevel@tonic-gate 		sc->ssc_a_rwnd = 0;
17710Sstevel@tonic-gate 	}
1772*13054SKacheong.Poon@Sun.COM 	/* Remember the last window sent to peer. */
1773*13054SKacheong.Poon@Sun.COM 	sctp->sctp_arwnd = sc->ssc_a_rwnd;
17740Sstevel@tonic-gate 	sc->ssc_numfrags = htons(num_gaps);
17750Sstevel@tonic-gate 	sc->ssc_numdups = 0;
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	/* lay in gap reports */
17780Sstevel@tonic-gate 	sf = (sctp_sack_frag_t *)(sc + 1);
17790Sstevel@tonic-gate 	for (sp = sctp->sctp_sack_info; sp; sp = sp->next) {
17800Sstevel@tonic-gate 		uint16_t offset;
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 		/* start */
17830Sstevel@tonic-gate 		if (sp->begin > sctp->sctp_lastacked) {
17840Sstevel@tonic-gate 			offset = (uint16_t)(sp->begin - sctp->sctp_lastacked);
17850Sstevel@tonic-gate 		} else {
17860Sstevel@tonic-gate 			/* sequence number wrap */
17870Sstevel@tonic-gate 			offset = (uint16_t)(UINT32_MAX - sctp->sctp_lastacked +
17880Sstevel@tonic-gate 			    sp->begin);
17890Sstevel@tonic-gate 		}
17900Sstevel@tonic-gate 		sf->ssf_start = htons(offset);
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 		/* end */
17930Sstevel@tonic-gate 		if (sp->end >= sp->begin) {
17940Sstevel@tonic-gate 			offset += (uint16_t)(sp->end - sp->begin);
17950Sstevel@tonic-gate 		} else {
17960Sstevel@tonic-gate 			/* sequence number wrap */
17970Sstevel@tonic-gate 			offset += (uint16_t)(UINT32_MAX - sp->begin + sp->end);
17980Sstevel@tonic-gate 		}
17990Sstevel@tonic-gate 		sf->ssf_end = htons(offset);
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 		sf++;
18020Sstevel@tonic-gate 		/* This is just for debugging (a la the following assertion) */
18030Sstevel@tonic-gate 		num_gaps--;
18040Sstevel@tonic-gate 	}
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 	ASSERT(num_gaps == 0);
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 	/* If the SACK timer is running, stop it */
18090Sstevel@tonic-gate 	if (sctp->sctp_ack_timer_running) {
18100Sstevel@tonic-gate 		sctp_timer_stop(sctp->sctp_ack_mp);
18110Sstevel@tonic-gate 		sctp->sctp_ack_timer_running = B_FALSE;
18120Sstevel@tonic-gate 	}
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
181510212SGeorge.Shepherd@Sun.COM 	BUMP_LOCAL(sctp->sctp_osacks);
18160Sstevel@tonic-gate }
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate mblk_t *
sctp_make_sack(sctp_t * sctp,sctp_faddr_t * sendto,mblk_t * dups)18190Sstevel@tonic-gate sctp_make_sack(sctp_t *sctp, sctp_faddr_t *sendto, mblk_t *dups)
18200Sstevel@tonic-gate {
18210Sstevel@tonic-gate 	mblk_t *smp;
18220Sstevel@tonic-gate 	size_t slen;
18230Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
18240Sstevel@tonic-gate 	sctp_sack_chunk_t *sc;
18253430Skcpoon 	int32_t acks_max;
18263448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
18274964Skcpoon 	uint32_t	dups_len;
18284964Skcpoon 	sctp_faddr_t	*fp;
18290Sstevel@tonic-gate 
183011042SErik.Nordmark@Sun.COM 	ASSERT(sendto != NULL);
183111042SErik.Nordmark@Sun.COM 
18320Sstevel@tonic-gate 	if (sctp->sctp_force_sack) {
18330Sstevel@tonic-gate 		sctp->sctp_force_sack = 0;
18340Sstevel@tonic-gate 		goto checks_done;
18350Sstevel@tonic-gate 	}
18360Sstevel@tonic-gate 
18373448Sdh155122 	acks_max = sctps->sctps_deferred_acks_max;
18380Sstevel@tonic-gate 	if (sctp->sctp_state == SCTPS_ESTABLISHED) {
18393430Skcpoon 		if (sctp->sctp_sack_toggle < acks_max) {
18400Sstevel@tonic-gate 			/* no need to SACK right now */
18410Sstevel@tonic-gate 			dprint(2, ("sctp_make_sack: %p no sack (toggle)\n",
18421676Sjpk 			    (void *)sctp));
18430Sstevel@tonic-gate 			return (NULL);
18443430Skcpoon 		} else if (sctp->sctp_sack_toggle >= acks_max) {
18450Sstevel@tonic-gate 			sctp->sctp_sack_toggle = 0;
18460Sstevel@tonic-gate 		}
18470Sstevel@tonic-gate 	}
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
18501676Sjpk 		dprint(2, ("sctp_make_sack: %p no sack (already)\n",
18511676Sjpk 		    (void *)sctp));
18520Sstevel@tonic-gate 		return (NULL);
18530Sstevel@tonic-gate 	}
18540Sstevel@tonic-gate 
18550Sstevel@tonic-gate checks_done:
18560Sstevel@tonic-gate 	dprint(2, ("sctp_make_sack: acking %x\n", sctp->sctp_ftsn - 1));
18570Sstevel@tonic-gate 
18584964Skcpoon 	if (dups != NULL)
18594964Skcpoon 		dups_len = MBLKL(dups);
18604964Skcpoon 	else
18614964Skcpoon 		dups_len = 0;
18620Sstevel@tonic-gate 	slen = sizeof (*sch) + sizeof (*sc) +
18630Sstevel@tonic-gate 	    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
18644964Skcpoon 
18654964Skcpoon 	/*
18664964Skcpoon 	 * If there are error chunks, check and see if we can send the
18674964Skcpoon 	 * SACK chunk and error chunks together in one packet.  If not,
18684964Skcpoon 	 * send the error chunks out now.
18694964Skcpoon 	 */
18704964Skcpoon 	if (sctp->sctp_err_chunks != NULL) {
18714964Skcpoon 		fp = SCTP_CHUNK_DEST(sctp->sctp_err_chunks);
187213009SChandrasekar.Marimuthu@Sun.COM 		if (sctp->sctp_err_len + slen + dups_len > fp->sf_pmss) {
18734964Skcpoon 			if ((smp = sctp_make_mp(sctp, fp, 0)) == NULL) {
18744964Skcpoon 				SCTP_KSTAT(sctps, sctp_send_err_failed);
18754964Skcpoon 				SCTP_KSTAT(sctps, sctp_send_sack_failed);
18764964Skcpoon 				freemsg(sctp->sctp_err_chunks);
18774964Skcpoon 				sctp->sctp_err_chunks = NULL;
18784964Skcpoon 				sctp->sctp_err_len = 0;
18794964Skcpoon 				return (NULL);
18804964Skcpoon 			}
18814964Skcpoon 			smp->b_cont = sctp->sctp_err_chunks;
188213009SChandrasekar.Marimuthu@Sun.COM 			sctp_set_iplen(sctp, smp, fp->sf_ixa);
188313009SChandrasekar.Marimuthu@Sun.COM 			(void) conn_ip_output(smp, fp->sf_ixa);
188411042SErik.Nordmark@Sun.COM 			BUMP_LOCAL(sctp->sctp_opkts);
18854964Skcpoon 			sctp->sctp_err_chunks = NULL;
18864964Skcpoon 			sctp->sctp_err_len = 0;
18874964Skcpoon 		}
18884964Skcpoon 	}
18890Sstevel@tonic-gate 	smp = sctp_make_mp(sctp, sendto, slen);
18900Sstevel@tonic-gate 	if (smp == NULL) {
18913448Sdh155122 		SCTP_KSTAT(sctps, sctp_send_sack_failed);
18920Sstevel@tonic-gate 		return (NULL);
18930Sstevel@tonic-gate 	}
18940Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)smp->b_wptr;
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	sctp_fill_sack(sctp, smp->b_wptr, slen);
18970Sstevel@tonic-gate 	smp->b_wptr += slen;
18984964Skcpoon 	if (dups != NULL) {
18990Sstevel@tonic-gate 		sc = (sctp_sack_chunk_t *)(sch + 1);
19004964Skcpoon 		sc->ssc_numdups = htons(MBLKL(dups) / sizeof (uint32_t));
19014964Skcpoon 		sch->sch_len = htons(slen + dups_len);
19020Sstevel@tonic-gate 		smp->b_cont = dups;
19030Sstevel@tonic-gate 	}
19040Sstevel@tonic-gate 
19054964Skcpoon 	if (sctp->sctp_err_chunks != NULL) {
19064964Skcpoon 		linkb(smp, sctp->sctp_err_chunks);
19074964Skcpoon 		sctp->sctp_err_chunks = NULL;
19084964Skcpoon 		sctp->sctp_err_len = 0;
19094964Skcpoon 	}
19100Sstevel@tonic-gate 	return (smp);
19110Sstevel@tonic-gate }
19120Sstevel@tonic-gate 
19134964Skcpoon /*
19144964Skcpoon  * Check and see if we need to send a SACK chunk.  If it is needed,
19154964Skcpoon  * send it out.  Return true if a SACK chunk is sent, false otherwise.
19164964Skcpoon  */
19174964Skcpoon boolean_t
sctp_sack(sctp_t * sctp,mblk_t * dups)19180Sstevel@tonic-gate sctp_sack(sctp_t *sctp, mblk_t *dups)
19190Sstevel@tonic-gate {
19200Sstevel@tonic-gate 	mblk_t *smp;
19213448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	/* If we are shutting down, let send_shutdown() bundle the SACK */
19240Sstevel@tonic-gate 	if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) {
19250Sstevel@tonic-gate 		sctp_send_shutdown(sctp, 0);
19260Sstevel@tonic-gate 	}
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastdata != NULL);
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	if ((smp = sctp_make_sack(sctp, sctp->sctp_lastdata, dups)) == NULL) {
19310Sstevel@tonic-gate 		/* The caller of sctp_sack() will not free the dups mblk. */
19320Sstevel@tonic-gate 		if (dups != NULL)
19330Sstevel@tonic-gate 			freeb(dups);
19344964Skcpoon 		return (B_FALSE);
19350Sstevel@tonic-gate 	}
19360Sstevel@tonic-gate 	dprint(2, ("sctp_sack: sending to %p %x:%x:%x:%x\n",
19371676Sjpk 	    (void *)sctp->sctp_lastdata,
193813009SChandrasekar.Marimuthu@Sun.COM 	    SCTP_PRINTADDR(sctp->sctp_lastdata->sf_faddr)));
19390Sstevel@tonic-gate 
194012869SKacheong.Poon@Sun.COM 	sctp->sctp_active = LBOLT_FASTPATH64;
194112869SKacheong.Poon@Sun.COM 
194212869SKacheong.Poon@Sun.COM 	SCTPS_BUMP_MIB(sctps, sctpOutAck);
194311042SErik.Nordmark@Sun.COM 
194413009SChandrasekar.Marimuthu@Sun.COM 	sctp_set_iplen(sctp, smp, sctp->sctp_lastdata->sf_ixa);
194513009SChandrasekar.Marimuthu@Sun.COM 	(void) conn_ip_output(smp, sctp->sctp_lastdata->sf_ixa);
194611042SErik.Nordmark@Sun.COM 	BUMP_LOCAL(sctp->sctp_opkts);
19474964Skcpoon 	return (B_TRUE);
19480Sstevel@tonic-gate }
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate /*
19510Sstevel@tonic-gate  * This is called if we have a message that was partially sent and is
19520Sstevel@tonic-gate  * abandoned. The cum TSN will be the last chunk sent for this message,
19530Sstevel@tonic-gate  * subsequent chunks will be marked ABANDONED. We send a Forward TSN
19540Sstevel@tonic-gate  * chunk in this case with the TSN of the last sent chunk so that the
19550Sstevel@tonic-gate  * peer can clean up its fragment list for this message. This message
19560Sstevel@tonic-gate  * will be removed from the transmit list when the peer sends a SACK
19570Sstevel@tonic-gate  * back.
19580Sstevel@tonic-gate  */
19590Sstevel@tonic-gate int
sctp_check_abandoned_msg(sctp_t * sctp,mblk_t * meta)19600Sstevel@tonic-gate sctp_check_abandoned_msg(sctp_t *sctp, mblk_t *meta)
19610Sstevel@tonic-gate {
19620Sstevel@tonic-gate 	sctp_data_hdr_t	*dh;
19630Sstevel@tonic-gate 	mblk_t		*nmp;
19640Sstevel@tonic-gate 	mblk_t		*head;
19650Sstevel@tonic-gate 	int32_t		unsent = 0;
19660Sstevel@tonic-gate 	mblk_t		*mp1 = meta->b_cont;
19670Sstevel@tonic-gate 	uint32_t	adv_pap = sctp->sctp_adv_pap;
19680Sstevel@tonic-gate 	sctp_faddr_t	*fp = sctp->sctp_current;
19693448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate 	dh = (sctp_data_hdr_t *)mp1->b_rptr;
19720Sstevel@tonic-gate 	if (SEQ_GEQ(sctp->sctp_lastack_rxd, ntohl(dh->sdh_tsn))) {
19730Sstevel@tonic-gate 		sctp_ftsn_set_t	*sets = NULL;
19740Sstevel@tonic-gate 		uint_t		nsets = 0;
19750Sstevel@tonic-gate 		uint32_t	seglen = sizeof (uint32_t);
19760Sstevel@tonic-gate 		boolean_t	ubit = SCTP_DATA_GET_UBIT(dh);
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 		while (mp1->b_next != NULL && SCTP_CHUNK_ISSENT(mp1->b_next))
19790Sstevel@tonic-gate 			mp1 = mp1->b_next;
19800Sstevel@tonic-gate 		dh = (sctp_data_hdr_t *)mp1->b_rptr;
19810Sstevel@tonic-gate 		sctp->sctp_adv_pap = ntohl(dh->sdh_tsn);
19820Sstevel@tonic-gate 		if (!ubit &&
19830Sstevel@tonic-gate 		    !sctp_add_ftsn_set(&sets, fp, meta, &nsets, &seglen)) {
19840Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
19850Sstevel@tonic-gate 			return (ENOMEM);
19860Sstevel@tonic-gate 		}
19870Sstevel@tonic-gate 		nmp = sctp_make_ftsn_chunk(sctp, fp, sets, nsets, seglen);
19880Sstevel@tonic-gate 		sctp_free_ftsn_set(sets);
19890Sstevel@tonic-gate 		if (nmp == NULL) {
19900Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
19910Sstevel@tonic-gate 			return (ENOMEM);
19920Sstevel@tonic-gate 		}
1993252Svi117747 		head = sctp_add_proto_hdr(sctp, fp, nmp, 0, NULL);
19940Sstevel@tonic-gate 		if (head == NULL) {
19950Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
19960Sstevel@tonic-gate 			freemsg(nmp);
19973448Sdh155122 			SCTP_KSTAT(sctps, sctp_send_ftsn_failed);
19980Sstevel@tonic-gate 			return (ENOMEM);
19990Sstevel@tonic-gate 		}
20000Sstevel@tonic-gate 		SCTP_MSG_SET_ABANDONED(meta);
200113009SChandrasekar.Marimuthu@Sun.COM 		sctp_set_iplen(sctp, head, fp->sf_ixa);
200213009SChandrasekar.Marimuthu@Sun.COM 		(void) conn_ip_output(head, fp->sf_ixa);
200311042SErik.Nordmark@Sun.COM 		BUMP_LOCAL(sctp->sctp_opkts);
200413009SChandrasekar.Marimuthu@Sun.COM 		if (!fp->sf_timer_running)
200513009SChandrasekar.Marimuthu@Sun.COM 			SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->sf_rto);
20060Sstevel@tonic-gate 		mp1 = mp1->b_next;
20070Sstevel@tonic-gate 		while (mp1 != NULL) {
20080Sstevel@tonic-gate 			ASSERT(!SCTP_CHUNK_ISSENT(mp1));
20090Sstevel@tonic-gate 			ASSERT(!SCTP_CHUNK_ABANDONED(mp1));
20100Sstevel@tonic-gate 			SCTP_ABANDON_CHUNK(mp1);
20110Sstevel@tonic-gate 			dh = (sctp_data_hdr_t *)mp1->b_rptr;
20120Sstevel@tonic-gate 			unsent += ntohs(dh->sdh_len) - sizeof (*dh);
20130Sstevel@tonic-gate 			mp1 = mp1->b_next;
20140Sstevel@tonic-gate 		}
20150Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent >= unsent);
20160Sstevel@tonic-gate 		sctp->sctp_unsent -= unsent;
20170Sstevel@tonic-gate 		/*
20180Sstevel@tonic-gate 		 * Update ULP the amount of queued data, which is
20190Sstevel@tonic-gate 		 * sent-unack'ed + unsent.
20200Sstevel@tonic-gate 		 */
20218348SEric.Yu@Sun.COM 		if (!SCTP_IS_DETACHED(sctp))
20228348SEric.Yu@Sun.COM 			SCTP_TXQ_UPDATE(sctp);
20230Sstevel@tonic-gate 		return (0);
20240Sstevel@tonic-gate 	}
20250Sstevel@tonic-gate 	return (-1);
20260Sstevel@tonic-gate }
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate uint32_t
sctp_cumack(sctp_t * sctp,uint32_t tsn,mblk_t ** first_unacked)20290Sstevel@tonic-gate sctp_cumack(sctp_t *sctp, uint32_t tsn, mblk_t **first_unacked)
20300Sstevel@tonic-gate {
20310Sstevel@tonic-gate 	mblk_t *ump, *nump, *mp = NULL;
20320Sstevel@tonic-gate 	uint16_t chunklen;
20330Sstevel@tonic-gate 	uint32_t xtsn;
20340Sstevel@tonic-gate 	sctp_faddr_t *fp;
20350Sstevel@tonic-gate 	sctp_data_hdr_t *sdc;
20360Sstevel@tonic-gate 	uint32_t cumack_forward = 0;
20370Sstevel@tonic-gate 	sctp_msg_hdr_t	*mhdr;
20383448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 	ump = sctp->sctp_xmit_head;
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	/*
20430Sstevel@tonic-gate 	 * Free messages only when they're completely acked.
20440Sstevel@tonic-gate 	 */
20450Sstevel@tonic-gate 	while (ump != NULL) {
20460Sstevel@tonic-gate 		mhdr = (sctp_msg_hdr_t *)ump->b_rptr;
20470Sstevel@tonic-gate 		for (mp = ump->b_cont; mp != NULL; mp = mp->b_next) {
20480Sstevel@tonic-gate 			if (SCTP_CHUNK_ABANDONED(mp)) {
20490Sstevel@tonic-gate 				ASSERT(SCTP_IS_MSG_ABANDONED(ump));
20500Sstevel@tonic-gate 				mp = NULL;
20510Sstevel@tonic-gate 				break;
20520Sstevel@tonic-gate 			}
20530Sstevel@tonic-gate 			/*
20540Sstevel@tonic-gate 			 * We check for abandoned message if we are PR-SCTP
20550Sstevel@tonic-gate 			 * aware, if this is not the first chunk in the
20560Sstevel@tonic-gate 			 * message (b_cont) and if the message is marked
20570Sstevel@tonic-gate 			 * abandoned.
20580Sstevel@tonic-gate 			 */
20590Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp)) {
20600Sstevel@tonic-gate 				if (sctp->sctp_prsctp_aware &&
20610Sstevel@tonic-gate 				    mp != ump->b_cont &&
20620Sstevel@tonic-gate 				    (SCTP_IS_MSG_ABANDONED(ump) ||
20630Sstevel@tonic-gate 				    SCTP_MSG_TO_BE_ABANDONED(ump, mhdr,
20640Sstevel@tonic-gate 				    sctp))) {
20650Sstevel@tonic-gate 					(void) sctp_check_abandoned_msg(sctp,
20660Sstevel@tonic-gate 					    ump);
20670Sstevel@tonic-gate 				}
20680Sstevel@tonic-gate 				goto cum_ack_done;
20690Sstevel@tonic-gate 			}
20700Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
20710Sstevel@tonic-gate 			xtsn = ntohl(sdc->sdh_tsn);
20720Sstevel@tonic-gate 			if (SEQ_GEQ(sctp->sctp_lastack_rxd, xtsn))
20730Sstevel@tonic-gate 				continue;
20740Sstevel@tonic-gate 			if (SEQ_GEQ(tsn, xtsn)) {
20750Sstevel@tonic-gate 				fp = SCTP_CHUNK_DEST(mp);
20760Sstevel@tonic-gate 				chunklen = ntohs(sdc->sdh_len);
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 				if (sctp->sctp_out_time != 0 &&
20790Sstevel@tonic-gate 				    xtsn == sctp->sctp_rtt_tsn) {
20800Sstevel@tonic-gate 					/* Got a new RTT measurement */
20810Sstevel@tonic-gate 					sctp_update_rtt(sctp, fp,
208211066Srafael.vanoni@sun.com 					    ddi_get_lbolt64() -
208311066Srafael.vanoni@sun.com 					    sctp->sctp_out_time);
20840Sstevel@tonic-gate 					sctp->sctp_out_time = 0;
20850Sstevel@tonic-gate 				}
20860Sstevel@tonic-gate 				if (SCTP_CHUNK_ISACKED(mp))
20870Sstevel@tonic-gate 					continue;
20881735Skcpoon 				SCTP_CHUNK_SET_SACKCNT(mp, 0);
20890Sstevel@tonic-gate 				SCTP_CHUNK_ACKED(mp);
209013009SChandrasekar.Marimuthu@Sun.COM 				ASSERT(fp->sf_suna >= chunklen);
209113009SChandrasekar.Marimuthu@Sun.COM 				fp->sf_suna -= chunklen;
209213009SChandrasekar.Marimuthu@Sun.COM 				fp->sf_acked += chunklen;
20930Sstevel@tonic-gate 				cumack_forward += chunklen;
20940Sstevel@tonic-gate 				ASSERT(sctp->sctp_unacked >=
20950Sstevel@tonic-gate 				    (chunklen - sizeof (*sdc)));
20960Sstevel@tonic-gate 				sctp->sctp_unacked -=
20970Sstevel@tonic-gate 				    (chunklen - sizeof (*sdc));
209813009SChandrasekar.Marimuthu@Sun.COM 				if (fp->sf_suna == 0) {
20990Sstevel@tonic-gate 					/* all outstanding data acked */
210013009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_pba = 0;
21010Sstevel@tonic-gate 					SCTP_FADDR_TIMER_STOP(fp);
21020Sstevel@tonic-gate 				} else {
21030Sstevel@tonic-gate 					SCTP_FADDR_TIMER_RESTART(sctp, fp,
210413009SChandrasekar.Marimuthu@Sun.COM 					    fp->sf_rto);
21050Sstevel@tonic-gate 				}
21060Sstevel@tonic-gate 			} else {
21070Sstevel@tonic-gate 				goto cum_ack_done;
21080Sstevel@tonic-gate 			}
21090Sstevel@tonic-gate 		}
21100Sstevel@tonic-gate 		nump = ump->b_next;
21110Sstevel@tonic-gate 		if (nump != NULL)
21120Sstevel@tonic-gate 			nump->b_prev = NULL;
21130Sstevel@tonic-gate 		if (ump == sctp->sctp_xmit_tail)
21140Sstevel@tonic-gate 			sctp->sctp_xmit_tail = nump;
21150Sstevel@tonic-gate 		if (SCTP_IS_MSG_ABANDONED(ump)) {
21160Sstevel@tonic-gate 			BUMP_LOCAL(sctp->sctp_prsctpdrop);
21170Sstevel@tonic-gate 			ump->b_next = NULL;
21180Sstevel@tonic-gate 			sctp_sendfail_event(sctp, ump, 0, B_TRUE);
21190Sstevel@tonic-gate 		} else {
21200Sstevel@tonic-gate 			sctp_free_msg(ump);
21210Sstevel@tonic-gate 		}
21220Sstevel@tonic-gate 		sctp->sctp_xmit_head = ump = nump;
21230Sstevel@tonic-gate 	}
21240Sstevel@tonic-gate cum_ack_done:
21250Sstevel@tonic-gate 	*first_unacked = mp;
21260Sstevel@tonic-gate 	if (cumack_forward > 0) {
212712869SKacheong.Poon@Sun.COM 		SCTPS_BUMP_MIB(sctps, sctpInAck);
21280Sstevel@tonic-gate 		if (SEQ_GT(sctp->sctp_lastack_rxd, sctp->sctp_recovery_tsn)) {
21290Sstevel@tonic-gate 			sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd;
21300Sstevel@tonic-gate 		}
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 		/*
21330Sstevel@tonic-gate 		 * Update ULP the amount of queued data, which is
21340Sstevel@tonic-gate 		 * sent-unack'ed + unsent.
21350Sstevel@tonic-gate 		 */
21368348SEric.Yu@Sun.COM 		if (!SCTP_IS_DETACHED(sctp))
21378348SEric.Yu@Sun.COM 			SCTP_TXQ_UPDATE(sctp);
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 		/* Time to send a shutdown? */
21400Sstevel@tonic-gate 		if (sctp->sctp_state == SCTPS_SHUTDOWN_PENDING) {
21410Sstevel@tonic-gate 			sctp_send_shutdown(sctp, 0);
21420Sstevel@tonic-gate 		}
21430Sstevel@tonic-gate 		sctp->sctp_xmit_unacked = mp;
21440Sstevel@tonic-gate 	} else {
21450Sstevel@tonic-gate 		/* dup ack */
214612869SKacheong.Poon@Sun.COM 		SCTPS_BUMP_MIB(sctps, sctpInDupAck);
21470Sstevel@tonic-gate 	}
21480Sstevel@tonic-gate 	sctp->sctp_lastack_rxd = tsn;
21490Sstevel@tonic-gate 	if (SEQ_LT(sctp->sctp_adv_pap, sctp->sctp_lastack_rxd))
21500Sstevel@tonic-gate 		sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
21510Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head || sctp->sctp_unacked == 0);
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 	return (cumack_forward);
21540Sstevel@tonic-gate }
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate static int
sctp_set_frwnd(sctp_t * sctp,uint32_t frwnd)21570Sstevel@tonic-gate sctp_set_frwnd(sctp_t *sctp, uint32_t frwnd)
21580Sstevel@tonic-gate {
21590Sstevel@tonic-gate 	uint32_t orwnd;
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	if (sctp->sctp_unacked > frwnd) {
21620Sstevel@tonic-gate 		sctp->sctp_frwnd = 0;
21630Sstevel@tonic-gate 		return (0);
21640Sstevel@tonic-gate 	}
21650Sstevel@tonic-gate 	orwnd = sctp->sctp_frwnd;
21660Sstevel@tonic-gate 	sctp->sctp_frwnd = frwnd - sctp->sctp_unacked;
21670Sstevel@tonic-gate 	if (orwnd < sctp->sctp_frwnd) {
21680Sstevel@tonic-gate 		return (1);
21690Sstevel@tonic-gate 	} else {
21700Sstevel@tonic-gate 		return (0);
21710Sstevel@tonic-gate 	}
21720Sstevel@tonic-gate }
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate /*
21750Sstevel@tonic-gate  * For un-ordered messages.
21760Sstevel@tonic-gate  * Walk the sctp->sctp_uo_frag list and remove any fragments with TSN
21770Sstevel@tonic-gate  * less than/equal to ftsn. Fragments for un-ordered messages are
21780Sstevel@tonic-gate  * strictly in sequence (w.r.t TSN).
21790Sstevel@tonic-gate  */
21800Sstevel@tonic-gate static int
sctp_ftsn_check_uo_frag(sctp_t * sctp,uint32_t ftsn)21810Sstevel@tonic-gate sctp_ftsn_check_uo_frag(sctp_t *sctp, uint32_t ftsn)
21820Sstevel@tonic-gate {
21830Sstevel@tonic-gate 	mblk_t		*hmp;
21840Sstevel@tonic-gate 	mblk_t		*hmp_next;
21850Sstevel@tonic-gate 	sctp_data_hdr_t	*dc;
21860Sstevel@tonic-gate 	int		dlen = 0;
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 	hmp = sctp->sctp_uo_frags;
21890Sstevel@tonic-gate 	while (hmp != NULL) {
21900Sstevel@tonic-gate 		hmp_next = hmp->b_next;
21910Sstevel@tonic-gate 		dc = (sctp_data_hdr_t *)hmp->b_rptr;
21920Sstevel@tonic-gate 		if (SEQ_GT(ntohl(dc->sdh_tsn), ftsn))
21930Sstevel@tonic-gate 			return (dlen);
21940Sstevel@tonic-gate 		sctp->sctp_uo_frags = hmp_next;
21950Sstevel@tonic-gate 		if (hmp_next != NULL)
21960Sstevel@tonic-gate 			hmp_next->b_prev = NULL;
21970Sstevel@tonic-gate 		hmp->b_next = NULL;
21980Sstevel@tonic-gate 		dlen += ntohs(dc->sdh_len) - sizeof (*dc);
21990Sstevel@tonic-gate 		freeb(hmp);
22000Sstevel@tonic-gate 		hmp = hmp_next;
22010Sstevel@tonic-gate 	}
22020Sstevel@tonic-gate 	return (dlen);
22030Sstevel@tonic-gate }
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate /*
22060Sstevel@tonic-gate  * For ordered messages.
22070Sstevel@tonic-gate  * Check for existing fragments for an sid-ssn pair reported as abandoned,
22080Sstevel@tonic-gate  * hence will not receive, in the Forward TSN. If there are fragments, then
22090Sstevel@tonic-gate  * we just nuke them. If and when Partial Delivery API is supported, we
22100Sstevel@tonic-gate  * would need to send a notification to the upper layer about this.
22110Sstevel@tonic-gate  */
22120Sstevel@tonic-gate static int
sctp_ftsn_check_frag(sctp_t * sctp,uint16_t ssn,sctp_instr_t * sip)22130Sstevel@tonic-gate sctp_ftsn_check_frag(sctp_t *sctp, uint16_t ssn, sctp_instr_t *sip)
22140Sstevel@tonic-gate {
22150Sstevel@tonic-gate 	sctp_reass_t	*srp;
22160Sstevel@tonic-gate 	mblk_t		*hmp;
22170Sstevel@tonic-gate 	mblk_t		*dmp;
22180Sstevel@tonic-gate 	mblk_t		*hmp_next;
22190Sstevel@tonic-gate 	sctp_data_hdr_t	*dc;
22200Sstevel@tonic-gate 	int		dlen = 0;
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 	hmp = sip->istr_reass;
22230Sstevel@tonic-gate 	while (hmp != NULL) {
22240Sstevel@tonic-gate 		hmp_next = hmp->b_next;
22250Sstevel@tonic-gate 		srp = (sctp_reass_t *)DB_BASE(hmp);
222613009SChandrasekar.Marimuthu@Sun.COM 		if (SSN_GT(srp->sr_ssn, ssn))
22270Sstevel@tonic-gate 			return (dlen);
22280Sstevel@tonic-gate 		/*
22290Sstevel@tonic-gate 		 * If we had sent part of this message up, send a partial
22300Sstevel@tonic-gate 		 * delivery event. Since this is ordered delivery, we should
22310Sstevel@tonic-gate 		 * have sent partial message only for the next in sequence,
22320Sstevel@tonic-gate 		 * hence the ASSERT. See comments in sctp_data_chunk() for
22330Sstevel@tonic-gate 		 * trypartial.
22340Sstevel@tonic-gate 		 */
223513009SChandrasekar.Marimuthu@Sun.COM 		if (srp->sr_partial_delivered) {
223613009SChandrasekar.Marimuthu@Sun.COM 			if (srp->sr_ssn != sip->nextseq)
223712604SGeorge.Shepherd@Sun.COM 				cmn_err(CE_WARN, "sctp partial"
223812604SGeorge.Shepherd@Sun.COM 				    " delivery notify, sctp 0x%p"
223912604SGeorge.Shepherd@Sun.COM 				    " sip = 0x%p ssn != nextseq"
224012604SGeorge.Shepherd@Sun.COM 				    " ssn 0x%x nextseq 0x%x",
224112604SGeorge.Shepherd@Sun.COM 				    (void *)sctp, (void *)sip,
224213009SChandrasekar.Marimuthu@Sun.COM 				    srp->sr_ssn, sip->nextseq);
224313009SChandrasekar.Marimuthu@Sun.COM 			ASSERT(sip->nextseq == srp->sr_ssn);
22440Sstevel@tonic-gate 			sctp_partial_delivery_event(sctp);
22450Sstevel@tonic-gate 		}
22460Sstevel@tonic-gate 		/* Take it out of the reass queue */
22470Sstevel@tonic-gate 		sip->istr_reass = hmp_next;
22480Sstevel@tonic-gate 		if (hmp_next != NULL)
22490Sstevel@tonic-gate 			hmp_next->b_prev = NULL;
22500Sstevel@tonic-gate 		hmp->b_next = NULL;
22510Sstevel@tonic-gate 		ASSERT(hmp->b_prev == NULL);
22520Sstevel@tonic-gate 		dmp = hmp;
22533845Svi117747 		ASSERT(DB_TYPE(hmp) == M_CTL);
22543845Svi117747 		dmp = hmp->b_cont;
22553845Svi117747 		hmp->b_cont = NULL;
22563845Svi117747 		freeb(hmp);
22573845Svi117747 		hmp = dmp;
22580Sstevel@tonic-gate 		while (dmp != NULL) {
22590Sstevel@tonic-gate 			dc = (sctp_data_hdr_t *)dmp->b_rptr;
22600Sstevel@tonic-gate 			dlen += ntohs(dc->sdh_len) - sizeof (*dc);
22610Sstevel@tonic-gate 			dmp = dmp->b_cont;
22620Sstevel@tonic-gate 		}
22630Sstevel@tonic-gate 		freemsg(hmp);
22640Sstevel@tonic-gate 		hmp = hmp_next;
22650Sstevel@tonic-gate 	}
22660Sstevel@tonic-gate 	return (dlen);
22670Sstevel@tonic-gate }
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate /*
22700Sstevel@tonic-gate  * Update sctp_ftsn to the cumulative TSN from the Forward TSN chunk. Remove
22710Sstevel@tonic-gate  * any SACK gaps less than the newly updated sctp_ftsn. Walk through the
22720Sstevel@tonic-gate  * sid-ssn pair in the Forward TSN and for each, clean the fragment list
22730Sstevel@tonic-gate  * for this pair, if needed, and check if we can deliver subsequent
22740Sstevel@tonic-gate  * messages, if any, from the instream queue (that were waiting for this
22750Sstevel@tonic-gate  * sid-ssn message to show up). Once we are done try to update the SACK
22760Sstevel@tonic-gate  * info. We could get a duplicate Forward TSN, in which case just send
227711042SErik.Nordmark@Sun.COM  * a SACK. If any of the sid values in the Forward TSN is invalid,
22780Sstevel@tonic-gate  * send back an "Invalid Stream Identifier" error and continue processing
22790Sstevel@tonic-gate  * the rest.
22800Sstevel@tonic-gate  */
22810Sstevel@tonic-gate static void
sctp_process_forward_tsn(sctp_t * sctp,sctp_chunk_hdr_t * ch,sctp_faddr_t * fp,ip_pkt_t * ipp,ip_recv_attr_t * ira)22820Sstevel@tonic-gate sctp_process_forward_tsn(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp,
228311042SErik.Nordmark@Sun.COM     ip_pkt_t *ipp, ip_recv_attr_t *ira)
22840Sstevel@tonic-gate {
22850Sstevel@tonic-gate 	uint32_t	*ftsn = (uint32_t *)(ch + 1);
22860Sstevel@tonic-gate 	ftsn_entry_t	*ftsn_entry;
22870Sstevel@tonic-gate 	sctp_instr_t	*instr;
22880Sstevel@tonic-gate 	boolean_t	can_deliver = B_TRUE;
22890Sstevel@tonic-gate 	size_t		dlen;
22900Sstevel@tonic-gate 	int		flen;
22910Sstevel@tonic-gate 	mblk_t		*dmp;
22920Sstevel@tonic-gate 	mblk_t		*pmp;
22930Sstevel@tonic-gate 	sctp_data_hdr_t	*dc;
22940Sstevel@tonic-gate 	ssize_t		remaining;
22953448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 	*ftsn = ntohl(*ftsn);
22980Sstevel@tonic-gate 	remaining =  ntohs(ch->sch_len) - sizeof (*ch) - sizeof (*ftsn);
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate 	if (SCTP_IS_DETACHED(sctp)) {
230112869SKacheong.Poon@Sun.COM 		SCTPS_BUMP_MIB(sctps, sctpInClosed);
23020Sstevel@tonic-gate 		can_deliver = B_FALSE;
23030Sstevel@tonic-gate 	}
23040Sstevel@tonic-gate 	/*
23050Sstevel@tonic-gate 	 * un-ordered messages don't have SID-SSN pair entries, we check
23060Sstevel@tonic-gate 	 * for any fragments (for un-ordered message) to be discarded using
23070Sstevel@tonic-gate 	 * the cumulative FTSN.
23080Sstevel@tonic-gate 	 */
23090Sstevel@tonic-gate 	flen = sctp_ftsn_check_uo_frag(sctp, *ftsn);
23100Sstevel@tonic-gate 	if (flen > 0) {
23110Sstevel@tonic-gate 		ASSERT(sctp->sctp_rxqueued >= flen);
23120Sstevel@tonic-gate 		sctp->sctp_rxqueued -= flen;
23130Sstevel@tonic-gate 	}
23140Sstevel@tonic-gate 	ftsn_entry = (ftsn_entry_t *)(ftsn + 1);
23150Sstevel@tonic-gate 	while (remaining >= sizeof (*ftsn_entry)) {
23160Sstevel@tonic-gate 		ftsn_entry->ftsn_sid = ntohs(ftsn_entry->ftsn_sid);
23170Sstevel@tonic-gate 		ftsn_entry->ftsn_ssn = ntohs(ftsn_entry->ftsn_ssn);
23180Sstevel@tonic-gate 		if (ftsn_entry->ftsn_sid >= sctp->sctp_num_istr) {
23199451SGeorge.Shepherd@Sun.COM 			sctp_bsc_t	inval_parm;
23209451SGeorge.Shepherd@Sun.COM 
23219451SGeorge.Shepherd@Sun.COM 			/* Will populate the CAUSE block in the ERROR chunk. */
23229451SGeorge.Shepherd@Sun.COM 			inval_parm.bsc_sid = htons(ftsn_entry->ftsn_sid);
23239451SGeorge.Shepherd@Sun.COM 			/* RESERVED, ignored at the receiving end */
23249451SGeorge.Shepherd@Sun.COM 			inval_parm.bsc_pad = 0;
23259451SGeorge.Shepherd@Sun.COM 
23269451SGeorge.Shepherd@Sun.COM 			sctp_add_err(sctp, SCTP_ERR_BAD_SID,
23279451SGeorge.Shepherd@Sun.COM 			    (void *)&inval_parm, sizeof (sctp_bsc_t), fp);
23280Sstevel@tonic-gate 			ftsn_entry++;
23290Sstevel@tonic-gate 			remaining -= sizeof (*ftsn_entry);
23300Sstevel@tonic-gate 			continue;
23310Sstevel@tonic-gate 		}
23320Sstevel@tonic-gate 		instr = &sctp->sctp_instr[ftsn_entry->ftsn_sid];
23330Sstevel@tonic-gate 		flen = sctp_ftsn_check_frag(sctp, ftsn_entry->ftsn_ssn, instr);
23340Sstevel@tonic-gate 		/* Indicates frags were nuked, update rxqueued */
23350Sstevel@tonic-gate 		if (flen > 0) {
23360Sstevel@tonic-gate 			ASSERT(sctp->sctp_rxqueued >= flen);
23370Sstevel@tonic-gate 			sctp->sctp_rxqueued -= flen;
23380Sstevel@tonic-gate 		}
23390Sstevel@tonic-gate 		/*
23400Sstevel@tonic-gate 		 * It is possible to receive an FTSN chunk with SSN smaller
23410Sstevel@tonic-gate 		 * than then nextseq if this chunk is a retransmission because
23420Sstevel@tonic-gate 		 * of incomplete processing when it was first processed.
23430Sstevel@tonic-gate 		 */
23440Sstevel@tonic-gate 		if (SSN_GE(ftsn_entry->ftsn_ssn, instr->nextseq))
23450Sstevel@tonic-gate 			instr->nextseq = ftsn_entry->ftsn_ssn + 1;
23460Sstevel@tonic-gate 		while (instr->istr_nmsgs > 0) {
23470Sstevel@tonic-gate 			mblk_t	*next;
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate 			dmp = (mblk_t *)instr->istr_msgs;
23500Sstevel@tonic-gate 			dc = (sctp_data_hdr_t *)dmp->b_rptr;
23510Sstevel@tonic-gate 			if (ntohs(dc->sdh_ssn) != instr->nextseq)
23520Sstevel@tonic-gate 				break;
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate 			next = dmp->b_next;
23550Sstevel@tonic-gate 			dlen = dmp->b_wptr - dmp->b_rptr - sizeof (*dc);
23560Sstevel@tonic-gate 			for (pmp = dmp->b_cont; pmp != NULL;
23570Sstevel@tonic-gate 			    pmp = pmp->b_cont) {
235810828SGeorge.Shepherd@Sun.COM 				dlen += MBLKL(pmp);
23590Sstevel@tonic-gate 			}
23600Sstevel@tonic-gate 			if (can_deliver) {
23618348SEric.Yu@Sun.COM 				int error;
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate 				dmp->b_rptr = (uchar_t *)(dc + 1);
23640Sstevel@tonic-gate 				dmp->b_next = NULL;
23650Sstevel@tonic-gate 				ASSERT(dmp->b_prev == NULL);
23660Sstevel@tonic-gate 				if (sctp_input_add_ancillary(sctp,
236711042SErik.Nordmark@Sun.COM 				    &dmp, dc, fp, ipp, ira) == 0) {
23680Sstevel@tonic-gate 					sctp->sctp_rxqueued -= dlen;
23698348SEric.Yu@Sun.COM 					/*
23708348SEric.Yu@Sun.COM 					 * Override b_flag for SCTP sockfs
23718348SEric.Yu@Sun.COM 					 * internal use
23728348SEric.Yu@Sun.COM 					 */
23738348SEric.Yu@Sun.COM 
23748348SEric.Yu@Sun.COM 					dmp->b_flag = 0;
2375*13054SKacheong.Poon@Sun.COM 					if (sctp->sctp_flowctrld) {
2376*13054SKacheong.Poon@Sun.COM 						sctp->sctp_rwnd -= dlen;
2377*13054SKacheong.Poon@Sun.COM 						if (sctp->sctp_rwnd < 0)
2378*13054SKacheong.Poon@Sun.COM 							sctp->sctp_rwnd = 0;
2379*13054SKacheong.Poon@Sun.COM 					}
2380*13054SKacheong.Poon@Sun.COM 					if (sctp->sctp_ulp_recv(
23818348SEric.Yu@Sun.COM 					    sctp->sctp_ulpd, dmp, msgdsize(dmp),
2382*13054SKacheong.Poon@Sun.COM 					    0, &error, NULL) <= 0) {
2383*13054SKacheong.Poon@Sun.COM 						sctp->sctp_flowctrld = B_TRUE;
2384*13054SKacheong.Poon@Sun.COM 					}
23850Sstevel@tonic-gate 				} else {
23860Sstevel@tonic-gate 					/*
23870Sstevel@tonic-gate 					 * We will resume processing when
23880Sstevel@tonic-gate 					 * the FTSN chunk is re-xmitted.
23890Sstevel@tonic-gate 					 */
23900Sstevel@tonic-gate 					dmp->b_rptr = (uchar_t *)dc;
23910Sstevel@tonic-gate 					dmp->b_next = next;
23920Sstevel@tonic-gate 					dprint(0,
23930Sstevel@tonic-gate 					    ("FTSN dequeuing %u failed\n",
23940Sstevel@tonic-gate 					    ntohs(dc->sdh_ssn)));
23950Sstevel@tonic-gate 					return;
23960Sstevel@tonic-gate 				}
23970Sstevel@tonic-gate 			} else {
23980Sstevel@tonic-gate 				sctp->sctp_rxqueued -= dlen;
23990Sstevel@tonic-gate 				ASSERT(dmp->b_prev == NULL);
24000Sstevel@tonic-gate 				dmp->b_next = NULL;
24010Sstevel@tonic-gate 				freemsg(dmp);
24020Sstevel@tonic-gate 			}
24030Sstevel@tonic-gate 			instr->istr_nmsgs--;
24040Sstevel@tonic-gate 			instr->nextseq++;
24050Sstevel@tonic-gate 			sctp->sctp_istr_nmsgs--;
24060Sstevel@tonic-gate 			if (next != NULL)
24070Sstevel@tonic-gate 				next->b_prev = NULL;
24080Sstevel@tonic-gate 			instr->istr_msgs = next;
24090Sstevel@tonic-gate 		}
24100Sstevel@tonic-gate 		ftsn_entry++;
24110Sstevel@tonic-gate 		remaining -= sizeof (*ftsn_entry);
24120Sstevel@tonic-gate 	}
24130Sstevel@tonic-gate 	/* Duplicate FTSN */
24140Sstevel@tonic-gate 	if (*ftsn <= (sctp->sctp_ftsn - 1)) {
24150Sstevel@tonic-gate 		sctp->sctp_force_sack = 1;
24160Sstevel@tonic-gate 		return;
24170Sstevel@tonic-gate 	}
24180Sstevel@tonic-gate 	/* Advance cum TSN to that reported in the Forward TSN chunk */
24190Sstevel@tonic-gate 	sctp->sctp_ftsn = *ftsn + 1;
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	/* Remove all the SACK gaps before the new cum TSN */
24220Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
24230Sstevel@tonic-gate 		sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1,
24240Sstevel@tonic-gate 		    &sctp->sctp_sack_gaps);
24250Sstevel@tonic-gate 	}
24260Sstevel@tonic-gate 	/*
24270Sstevel@tonic-gate 	 * If there are gap reports pending, check if advancing
24280Sstevel@tonic-gate 	 * the ftsn here closes a gap. If so, we can advance
24290Sstevel@tonic-gate 	 * ftsn to the end of the set.
24300Sstevel@tonic-gate 	 * If ftsn has moved forward, maybe we can remove gap reports.
24310Sstevel@tonic-gate 	 */
24320Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL &&
24330Sstevel@tonic-gate 	    sctp->sctp_ftsn == sctp->sctp_sack_info->begin) {
24340Sstevel@tonic-gate 		sctp->sctp_ftsn = sctp->sctp_sack_info->end + 1;
24350Sstevel@tonic-gate 		sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1,
24360Sstevel@tonic-gate 		    &sctp->sctp_sack_gaps);
24370Sstevel@tonic-gate 	}
24380Sstevel@tonic-gate }
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate /*
24410Sstevel@tonic-gate  * When we have processed a SACK we check to see if we can advance the
24420Sstevel@tonic-gate  * cumulative TSN if there are abandoned chunks immediately following
24430Sstevel@tonic-gate  * the updated cumulative TSN. If there are, we attempt to send a
24440Sstevel@tonic-gate  * Forward TSN chunk.
24450Sstevel@tonic-gate  */
24460Sstevel@tonic-gate static void
sctp_check_abandoned_data(sctp_t * sctp,sctp_faddr_t * fp)24470Sstevel@tonic-gate sctp_check_abandoned_data(sctp_t *sctp, sctp_faddr_t *fp)
24480Sstevel@tonic-gate {
24490Sstevel@tonic-gate 	mblk_t		*meta = sctp->sctp_xmit_head;
24500Sstevel@tonic-gate 	mblk_t		*mp;
24510Sstevel@tonic-gate 	mblk_t		*nmp;
24520Sstevel@tonic-gate 	uint32_t	seglen;
24530Sstevel@tonic-gate 	uint32_t	adv_pap = sctp->sctp_adv_pap;
24540Sstevel@tonic-gate 
24550Sstevel@tonic-gate 	/*
24560Sstevel@tonic-gate 	 * We only check in the first meta since otherwise we can't
24570Sstevel@tonic-gate 	 * advance the cumulative ack point. We just look for chunks
24580Sstevel@tonic-gate 	 * marked for retransmission, else we might prematurely
24590Sstevel@tonic-gate 	 * send an FTSN for a sent, but unacked, chunk.
24600Sstevel@tonic-gate 	 */
24610Sstevel@tonic-gate 	for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
24620Sstevel@tonic-gate 		if (!SCTP_CHUNK_ISSENT(mp))
24630Sstevel@tonic-gate 			return;
24640Sstevel@tonic-gate 		if (SCTP_CHUNK_WANT_REXMIT(mp))
24650Sstevel@tonic-gate 			break;
24660Sstevel@tonic-gate 	}
24670Sstevel@tonic-gate 	if (mp == NULL)
24680Sstevel@tonic-gate 		return;
24690Sstevel@tonic-gate 	sctp_check_adv_ack_pt(sctp, meta, mp);
24700Sstevel@tonic-gate 	if (SEQ_GT(sctp->sctp_adv_pap, adv_pap)) {
24710Sstevel@tonic-gate 		sctp_make_ftsns(sctp, meta, mp, &nmp, fp, &seglen);
24720Sstevel@tonic-gate 		if (nmp == NULL) {
24730Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
247413009SChandrasekar.Marimuthu@Sun.COM 			if (!fp->sf_timer_running)
247513009SChandrasekar.Marimuthu@Sun.COM 				SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->sf_rto);
24760Sstevel@tonic-gate 			return;
24770Sstevel@tonic-gate 		}
247813009SChandrasekar.Marimuthu@Sun.COM 		sctp_set_iplen(sctp, nmp, fp->sf_ixa);
247913009SChandrasekar.Marimuthu@Sun.COM 		(void) conn_ip_output(nmp, fp->sf_ixa);
248011042SErik.Nordmark@Sun.COM 		BUMP_LOCAL(sctp->sctp_opkts);
248113009SChandrasekar.Marimuthu@Sun.COM 		if (!fp->sf_timer_running)
248213009SChandrasekar.Marimuthu@Sun.COM 			SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->sf_rto);
24830Sstevel@tonic-gate 	}
24840Sstevel@tonic-gate }
24850Sstevel@tonic-gate 
2486852Svi117747 /*
2487852Svi117747  * The processing here follows the same logic in sctp_got_sack(), the reason
2488852Svi117747  * we do this separately is because, usually, gap blocks are ordered and
2489852Svi117747  * we can process it in sctp_got_sack(). However if they aren't we would
2490852Svi117747  * need to do some additional non-optimal stuff when we start processing the
2491852Svi117747  * unordered gaps. To that effect sctp_got_sack() does the processing in the
2492852Svi117747  * simple case and this does the same in the more involved case.
2493852Svi117747  */
2494852Svi117747 static uint32_t
sctp_process_uo_gaps(sctp_t * sctp,uint32_t ctsn,sctp_sack_frag_t * ssf,int num_gaps,mblk_t * umphead,mblk_t * mphead,int * trysend,boolean_t * fast_recovery,uint32_t fr_xtsn)2495852Svi117747 sctp_process_uo_gaps(sctp_t *sctp, uint32_t ctsn, sctp_sack_frag_t *ssf,
2496852Svi117747     int num_gaps, mblk_t *umphead, mblk_t *mphead, int *trysend,
2497852Svi117747     boolean_t *fast_recovery, uint32_t fr_xtsn)
2498852Svi117747 {
2499852Svi117747 	uint32_t		xtsn;
2500852Svi117747 	uint32_t		gapstart = 0;
2501852Svi117747 	uint32_t		gapend = 0;
2502852Svi117747 	int			gapcnt;
2503852Svi117747 	uint16_t		chunklen;
2504852Svi117747 	sctp_data_hdr_t		*sdc;
2505852Svi117747 	int			gstart;
2506852Svi117747 	mblk_t			*ump = umphead;
2507852Svi117747 	mblk_t			*mp = mphead;
2508852Svi117747 	sctp_faddr_t		*fp;
2509852Svi117747 	uint32_t		acked = 0;
25103448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
2511852Svi117747 
2512852Svi117747 	/*
2513852Svi117747 	 * gstart tracks the last (in the order of TSN) gapstart that
2514852Svi117747 	 * we process in this SACK gaps walk.
2515852Svi117747 	 */
2516852Svi117747 	gstart = ctsn;
2517852Svi117747 
2518852Svi117747 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
2519852Svi117747 	xtsn = ntohl(sdc->sdh_tsn);
2520852Svi117747 	for (gapcnt = 0; gapcnt < num_gaps; gapcnt++, ssf++) {
2521852Svi117747 		if (gapstart != 0) {
2522852Svi117747 			/*
2523852Svi117747 			 * If we have reached the end of the transmit list or
2524852Svi117747 			 * hit an unsent chunk or encountered an unordered gap
2525852Svi117747 			 * block start from the ctsn again.
2526852Svi117747 			 */
2527852Svi117747 			if (ump == NULL || !SCTP_CHUNK_ISSENT(mp) ||
2528852Svi117747 			    SEQ_LT(ctsn + ntohs(ssf->ssf_start), xtsn)) {
2529852Svi117747 				ump = umphead;
2530852Svi117747 				mp = mphead;
2531852Svi117747 				sdc = (sctp_data_hdr_t *)mp->b_rptr;
2532852Svi117747 				xtsn = ntohl(sdc->sdh_tsn);
2533852Svi117747 			}
2534852Svi117747 		}
2535852Svi117747 
2536852Svi117747 		gapstart = ctsn + ntohs(ssf->ssf_start);
2537852Svi117747 		gapend = ctsn + ntohs(ssf->ssf_end);
2538852Svi117747 
253910068SChandrasekar.Marimuthu@Sun.COM 		/*
254010068SChandrasekar.Marimuthu@Sun.COM 		 * Sanity checks:
254110068SChandrasekar.Marimuthu@Sun.COM 		 *
254210068SChandrasekar.Marimuthu@Sun.COM 		 * 1. SACK for TSN we have not sent - ABORT
254310068SChandrasekar.Marimuthu@Sun.COM 		 * 2. Invalid or spurious gaps, ignore all gaps
254410068SChandrasekar.Marimuthu@Sun.COM 		 */
2545852Svi117747 		if (SEQ_GT(gapstart, sctp->sctp_ltsn - 1) ||
2546852Svi117747 		    SEQ_GT(gapend, sctp->sctp_ltsn - 1)) {
254712869SKacheong.Poon@Sun.COM 			SCTPS_BUMP_MIB(sctps, sctpInAckUnsent);
2548852Svi117747 			*trysend = -1;
2549852Svi117747 			return (acked);
255010068SChandrasekar.Marimuthu@Sun.COM 		} else if (SEQ_LT(gapend, gapstart) ||
255110068SChandrasekar.Marimuthu@Sun.COM 		    SEQ_LEQ(gapstart, ctsn)) {
2552852Svi117747 			break;
2553852Svi117747 		}
2554852Svi117747 		/*
2555852Svi117747 		 * The xtsn can be the TSN processed for the last gap
2556852Svi117747 		 * (gapend) or it could be the cumulative TSN. We continue
2557852Svi117747 		 * with the last xtsn as long as the gaps are ordered, when
2558852Svi117747 		 * we hit an unordered gap, we re-start from the cumulative
2559852Svi117747 		 * TSN. For the first gap it is always the cumulative TSN.
2560852Svi117747 		 */
2561852Svi117747 		while (xtsn != gapstart) {
2562852Svi117747 			/*
2563852Svi117747 			 * We can't reliably check for reneged chunks
2564852Svi117747 			 * when walking the unordered list, so we don't.
2565852Svi117747 			 * In case the peer reneges then we will end up
2566852Svi117747 			 * sending the reneged chunk via timeout.
2567852Svi117747 			 */
2568852Svi117747 			mp = mp->b_next;
2569852Svi117747 			if (mp == NULL) {
2570852Svi117747 				ump = ump->b_next;
2571852Svi117747 				/*
2572852Svi117747 				 * ump can't be NULL because of the sanity
2573852Svi117747 				 * check above.
2574852Svi117747 				 */
2575852Svi117747 				ASSERT(ump != NULL);
2576852Svi117747 				mp = ump->b_cont;
2577852Svi117747 			}
2578852Svi117747 			/*
2579852Svi117747 			 * mp can't be unsent because of the sanity check
2580852Svi117747 			 * above.
2581852Svi117747 			 */
2582852Svi117747 			ASSERT(SCTP_CHUNK_ISSENT(mp));
2583852Svi117747 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
2584852Svi117747 			xtsn = ntohl(sdc->sdh_tsn);
2585852Svi117747 		}
2586852Svi117747 		/*
2587852Svi117747 		 * Now that we have found the chunk with TSN == 'gapstart',
2588852Svi117747 		 * let's walk till we hit the chunk with TSN == 'gapend'.
2589852Svi117747 		 * All intermediate chunks will be marked ACKED, if they
2590852Svi117747 		 * haven't already been.
2591852Svi117747 		 */
2592852Svi117747 		while (SEQ_LEQ(xtsn, gapend)) {
2593852Svi117747 			/*
2594852Svi117747 			 * SACKed
2595852Svi117747 			 */
2596852Svi117747 			SCTP_CHUNK_SET_SACKCNT(mp, 0);
2597852Svi117747 			if (!SCTP_CHUNK_ISACKED(mp)) {
2598852Svi117747 				SCTP_CHUNK_ACKED(mp);
2599852Svi117747 
2600852Svi117747 				fp = SCTP_CHUNK_DEST(mp);
2601852Svi117747 				chunklen = ntohs(sdc->sdh_len);
260213009SChandrasekar.Marimuthu@Sun.COM 				ASSERT(fp->sf_suna >= chunklen);
260313009SChandrasekar.Marimuthu@Sun.COM 				fp->sf_suna -= chunklen;
260413009SChandrasekar.Marimuthu@Sun.COM 				if (fp->sf_suna == 0) {
2605852Svi117747 					/* All outstanding data acked. */
260613009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_pba = 0;
2607852Svi117747 					SCTP_FADDR_TIMER_STOP(fp);
2608852Svi117747 				}
260913009SChandrasekar.Marimuthu@Sun.COM 				fp->sf_acked += chunklen;
2610852Svi117747 				acked += chunklen;
2611852Svi117747 				sctp->sctp_unacked -= chunklen - sizeof (*sdc);
2612852Svi117747 				ASSERT(sctp->sctp_unacked >= 0);
2613852Svi117747 			}
2614852Svi117747 			/*
2615852Svi117747 			 * Move to the next message in the transmit list
2616852Svi117747 			 * if we are done with all the chunks from the current
2617852Svi117747 			 * message. Note, it is possible to hit the end of the
2618852Svi117747 			 * transmit list here, i.e. if we have already completed
2619852Svi117747 			 * processing the gap block.
2620852Svi117747 			 */
2621852Svi117747 			mp = mp->b_next;
2622852Svi117747 			if (mp == NULL) {
2623852Svi117747 				ump = ump->b_next;
2624852Svi117747 				if (ump == NULL) {
2625852Svi117747 					ASSERT(xtsn == gapend);
2626852Svi117747 					break;
2627852Svi117747 				}
2628852Svi117747 				mp = ump->b_cont;
2629852Svi117747 			}
2630852Svi117747 			/*
2631852Svi117747 			 * Likewise, we can hit an unsent chunk once we have
2632852Svi117747 			 * completed processing the gap block.
2633852Svi117747 			 */
2634852Svi117747 			if (!SCTP_CHUNK_ISSENT(mp)) {
2635852Svi117747 				ASSERT(xtsn == gapend);
2636852Svi117747 				break;
2637852Svi117747 			}
2638852Svi117747 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
2639852Svi117747 			xtsn = ntohl(sdc->sdh_tsn);
2640852Svi117747 		}
2641852Svi117747 		/*
2642852Svi117747 		 * We keep track of the last gap we successfully processed
2643852Svi117747 		 * so that we can terminate the walk below for incrementing
2644852Svi117747 		 * the SACK count.
2645852Svi117747 		 */
2646852Svi117747 		if (SEQ_LT(gstart, gapstart))
2647852Svi117747 			gstart = gapstart;
2648852Svi117747 	}
2649852Svi117747 	/*
2650852Svi117747 	 * Check if have incremented the SACK count for all unacked TSNs in
2651852Svi117747 	 * sctp_got_sack(), if so we are done.
2652852Svi117747 	 */
2653852Svi117747 	if (SEQ_LEQ(gstart, fr_xtsn))
2654852Svi117747 		return (acked);
2655852Svi117747 
2656852Svi117747 	ump = umphead;
2657852Svi117747 	mp = mphead;
2658852Svi117747 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
2659852Svi117747 	xtsn = ntohl(sdc->sdh_tsn);
2660852Svi117747 	while (SEQ_LT(xtsn, gstart)) {
2661852Svi117747 		/*
2662852Svi117747 		 * We have incremented SACK count for TSNs less than fr_tsn
2663852Svi117747 		 * in sctp_got_sack(), so don't increment them again here.
2664852Svi117747 		 */
2665852Svi117747 		if (SEQ_GT(xtsn, fr_xtsn) && !SCTP_CHUNK_ISACKED(mp)) {
2666852Svi117747 			SCTP_CHUNK_SET_SACKCNT(mp, SCTP_CHUNK_SACKCNT(mp) + 1);
26673448Sdh155122 			if (SCTP_CHUNK_SACKCNT(mp) ==
26683448Sdh155122 			    sctps->sctps_fast_rxt_thresh) {
266912604SGeorge.Shepherd@Sun.COM 				SCTP_CHUNK_REXMIT(sctp, mp);
2670852Svi117747 				sctp->sctp_chk_fast_rexmit = B_TRUE;
2671852Svi117747 				*trysend = 1;
2672852Svi117747 				if (!*fast_recovery) {
2673852Svi117747 					/*
2674852Svi117747 					 * Entering fast recovery.
2675852Svi117747 					 */
2676852Svi117747 					fp = SCTP_CHUNK_DEST(mp);
267713009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_ssthresh = fp->sf_cwnd / 2;
267813009SChandrasekar.Marimuthu@Sun.COM 					if (fp->sf_ssthresh < 2 * fp->sf_pmss) {
267913009SChandrasekar.Marimuthu@Sun.COM 						fp->sf_ssthresh =
268013009SChandrasekar.Marimuthu@Sun.COM 						    2 * fp->sf_pmss;
2681852Svi117747 					}
268213009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_cwnd = fp->sf_ssthresh;
268313009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_pba = 0;
2684852Svi117747 					sctp->sctp_recovery_tsn =
2685852Svi117747 					    sctp->sctp_ltsn - 1;
2686852Svi117747 					*fast_recovery = B_TRUE;
2687852Svi117747 				}
2688852Svi117747 			}
2689852Svi117747 		}
2690852Svi117747 		mp = mp->b_next;
2691852Svi117747 		if (mp == NULL) {
2692852Svi117747 			ump = ump->b_next;
2693852Svi117747 			/* We can't get to the end of the transmit list here */
2694852Svi117747 			ASSERT(ump != NULL);
2695852Svi117747 			mp = ump->b_cont;
2696852Svi117747 		}
2697852Svi117747 		/* We can't hit an unsent chunk here */
2698852Svi117747 		ASSERT(SCTP_CHUNK_ISSENT(mp));
2699852Svi117747 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
2700852Svi117747 		xtsn = ntohl(sdc->sdh_tsn);
2701852Svi117747 	}
2702852Svi117747 	return (acked);
2703852Svi117747 }
2704852Svi117747 
27050Sstevel@tonic-gate static int
sctp_got_sack(sctp_t * sctp,sctp_chunk_hdr_t * sch)27060Sstevel@tonic-gate sctp_got_sack(sctp_t *sctp, sctp_chunk_hdr_t *sch)
27070Sstevel@tonic-gate {
27080Sstevel@tonic-gate 	sctp_sack_chunk_t	*sc;
27090Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
27100Sstevel@tonic-gate 	sctp_sack_frag_t	*ssf;
27110Sstevel@tonic-gate 	mblk_t			*ump;
27120Sstevel@tonic-gate 	mblk_t			*mp;
2713852Svi117747 	mblk_t			*mp1;
2714852Svi117747 	uint32_t		cumtsn;
27150Sstevel@tonic-gate 	uint32_t		xtsn;
2716852Svi117747 	uint32_t		gapstart = 0;
2717852Svi117747 	uint32_t		gapend = 0;
27180Sstevel@tonic-gate 	uint32_t		acked = 0;
27190Sstevel@tonic-gate 	uint16_t		chunklen;
27200Sstevel@tonic-gate 	sctp_faddr_t		*fp;
27210Sstevel@tonic-gate 	int			num_gaps;
27220Sstevel@tonic-gate 	int			trysend = 0;
27230Sstevel@tonic-gate 	int			i;
27240Sstevel@tonic-gate 	boolean_t		fast_recovery = B_FALSE;
27250Sstevel@tonic-gate 	boolean_t		cumack_forward = B_FALSE;
27260Sstevel@tonic-gate 	boolean_t		fwd_tsn = B_FALSE;
27273448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
27280Sstevel@tonic-gate 
27290Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
273010212SGeorge.Shepherd@Sun.COM 	BUMP_LOCAL(sctp->sctp_isacks);
27310Sstevel@tonic-gate 	chunklen = ntohs(sch->sch_len);
27320Sstevel@tonic-gate 	if (chunklen < (sizeof (*sch) + sizeof (*sc)))
27330Sstevel@tonic-gate 		return (0);
27340Sstevel@tonic-gate 
27350Sstevel@tonic-gate 	sc = (sctp_sack_chunk_t *)(sch + 1);
2736852Svi117747 	cumtsn = ntohl(sc->ssc_cumtsn);
2737852Svi117747 
2738852Svi117747 	dprint(2, ("got sack cumtsn %x -> %x\n", sctp->sctp_lastack_rxd,
2739852Svi117747 	    cumtsn));
27400Sstevel@tonic-gate 
27410Sstevel@tonic-gate 	/* out of order */
2742852Svi117747 	if (SEQ_LT(cumtsn, sctp->sctp_lastack_rxd))
27430Sstevel@tonic-gate 		return (0);
27440Sstevel@tonic-gate 
2745852Svi117747 	if (SEQ_GT(cumtsn, sctp->sctp_ltsn - 1)) {
274612869SKacheong.Poon@Sun.COM 		SCTPS_BUMP_MIB(sctps, sctpInAckUnsent);
2747852Svi117747 		/* Send an ABORT */
2748852Svi117747 		return (-1);
27490Sstevel@tonic-gate 	}
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate 	/*
27520Sstevel@tonic-gate 	 * Cwnd only done when not in fast recovery mode.
27530Sstevel@tonic-gate 	 */
27540Sstevel@tonic-gate 	if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_recovery_tsn))
27550Sstevel@tonic-gate 		fast_recovery = B_TRUE;
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate 	/*
27580Sstevel@tonic-gate 	 * .. and if the cum TSN is not moving ahead on account Forward TSN
27590Sstevel@tonic-gate 	 */
27600Sstevel@tonic-gate 	if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_adv_pap))
27610Sstevel@tonic-gate 		fwd_tsn = B_TRUE;
27620Sstevel@tonic-gate 
2763852Svi117747 	if (cumtsn == sctp->sctp_lastack_rxd &&
27640Sstevel@tonic-gate 	    (sctp->sctp_xmit_unacked == NULL ||
27650Sstevel@tonic-gate 	    !SCTP_CHUNK_ABANDONED(sctp->sctp_xmit_unacked))) {
27660Sstevel@tonic-gate 		if (sctp->sctp_xmit_unacked != NULL)
27670Sstevel@tonic-gate 			mp = sctp->sctp_xmit_unacked;
27680Sstevel@tonic-gate 		else if (sctp->sctp_xmit_head != NULL)
27690Sstevel@tonic-gate 			mp = sctp->sctp_xmit_head->b_cont;
27700Sstevel@tonic-gate 		else
27710Sstevel@tonic-gate 			mp = NULL;
277212869SKacheong.Poon@Sun.COM 		SCTPS_BUMP_MIB(sctps, sctpInDupAck);
27731932Svi117747 		/*
27741932Svi117747 		 * If we were doing a zero win probe and the win
27751932Svi117747 		 * has now opened to at least MSS, re-transmit the
27761932Svi117747 		 * zero win probe via sctp_rexmit_packet().
27771932Svi117747 		 */
27781932Svi117747 		if (mp != NULL && sctp->sctp_zero_win_probe &&
277913009SChandrasekar.Marimuthu@Sun.COM 		    ntohl(sc->ssc_a_rwnd) >= sctp->sctp_current->sf_pmss) {
27801932Svi117747 			mblk_t	*pkt;
27811932Svi117747 			uint_t	pkt_len;
27821932Svi117747 			mblk_t	*mp1 = mp;
27831932Svi117747 			mblk_t	*meta = sctp->sctp_xmit_head;
27841932Svi117747 
27851932Svi117747 			/*
27861932Svi117747 			 * Reset the RTO since we have been backing-off
27871932Svi117747 			 * to send the ZWP.
27881932Svi117747 			 */
27891932Svi117747 			fp = sctp->sctp_current;
279013009SChandrasekar.Marimuthu@Sun.COM 			fp->sf_rto = fp->sf_srtt + 4 * fp->sf_rttvar;
279110212SGeorge.Shepherd@Sun.COM 			SCTP_MAX_RTO(sctp, fp);
27921932Svi117747 			/* Resend the ZWP */
27931932Svi117747 			pkt = sctp_rexmit_packet(sctp, &meta, &mp1, fp,
27941932Svi117747 			    &pkt_len);
27951932Svi117747 			if (pkt == NULL) {
27963448Sdh155122 				SCTP_KSTAT(sctps, sctp_ss_rexmit_failed);
27971932Svi117747 				return (0);
27981932Svi117747 			}
279913009SChandrasekar.Marimuthu@Sun.COM 			ASSERT(pkt_len <= fp->sf_pmss);
28001932Svi117747 			sctp->sctp_zero_win_probe = B_FALSE;
28011932Svi117747 			sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn;
28021932Svi117747 			sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn;
280313009SChandrasekar.Marimuthu@Sun.COM 			sctp_set_iplen(sctp, pkt, fp->sf_ixa);
280413009SChandrasekar.Marimuthu@Sun.COM 			(void) conn_ip_output(pkt, fp->sf_ixa);
280511042SErik.Nordmark@Sun.COM 			BUMP_LOCAL(sctp->sctp_opkts);
28061932Svi117747 		}
28070Sstevel@tonic-gate 	} else {
28081932Svi117747 		if (sctp->sctp_zero_win_probe) {
28091932Svi117747 			/*
28101932Svi117747 			 * Reset the RTO since we have been backing-off
28111932Svi117747 			 * to send the ZWP.
28121932Svi117747 			 */
28131932Svi117747 			fp = sctp->sctp_current;
281413009SChandrasekar.Marimuthu@Sun.COM 			fp->sf_rto = fp->sf_srtt + 4 * fp->sf_rttvar;
281510212SGeorge.Shepherd@Sun.COM 			SCTP_MAX_RTO(sctp, fp);
28161932Svi117747 			sctp->sctp_zero_win_probe = B_FALSE;
28171932Svi117747 			/* This is probably not required */
28181932Svi117747 			if (!sctp->sctp_rexmitting) {
28191932Svi117747 				sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn;
28201932Svi117747 				sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn;
28211932Svi117747 			}
28221932Svi117747 		}
2823852Svi117747 		acked = sctp_cumack(sctp, cumtsn, &mp);
28240Sstevel@tonic-gate 		sctp->sctp_xmit_unacked = mp;
28250Sstevel@tonic-gate 		if (acked > 0) {
28260Sstevel@tonic-gate 			trysend = 1;
28270Sstevel@tonic-gate 			cumack_forward = B_TRUE;
28280Sstevel@tonic-gate 			if (fwd_tsn && SEQ_GEQ(sctp->sctp_lastack_rxd,
28290Sstevel@tonic-gate 			    sctp->sctp_adv_pap)) {
28300Sstevel@tonic-gate 				cumack_forward = B_FALSE;
28310Sstevel@tonic-gate 			}
28320Sstevel@tonic-gate 		}
28330Sstevel@tonic-gate 	}
28340Sstevel@tonic-gate 	num_gaps = ntohs(sc->ssc_numfrags);
283510212SGeorge.Shepherd@Sun.COM 	UPDATE_LOCAL(sctp->sctp_gapcnt, num_gaps);
28360Sstevel@tonic-gate 	if (num_gaps == 0 || mp == NULL || !SCTP_CHUNK_ISSENT(mp) ||
28370Sstevel@tonic-gate 	    chunklen < (sizeof (*sch) + sizeof (*sc) +
28380Sstevel@tonic-gate 	    num_gaps * sizeof (*ssf))) {
28390Sstevel@tonic-gate 		goto ret;
28400Sstevel@tonic-gate 	}
2841852Svi117747 #ifdef	DEBUG
2842852Svi117747 	/*
2843852Svi117747 	 * Since we delete any message that has been acked completely,
2844852Svi117747 	 * the unacked chunk must belong to sctp_xmit_head (as
2845852Svi117747 	 * we don't have a back pointer from the mp to the meta data
2846852Svi117747 	 * we do this).
2847852Svi117747 	 */
2848852Svi117747 	{
2849852Svi117747 		mblk_t	*mp2 = sctp->sctp_xmit_head->b_cont;
2850852Svi117747 
2851852Svi117747 		while (mp2 != NULL) {
2852852Svi117747 			if (mp2 == mp)
2853852Svi117747 				break;
2854852Svi117747 			mp2 = mp2->b_next;
2855852Svi117747 		}
2856852Svi117747 		ASSERT(mp2 != NULL);
2857852Svi117747 	}
2858852Svi117747 #endif
28590Sstevel@tonic-gate 	ump = sctp->sctp_xmit_head;
28600Sstevel@tonic-gate 
28610Sstevel@tonic-gate 	/*
2862852Svi117747 	 * Just remember where we started from, in case we need to call
2863852Svi117747 	 * sctp_process_uo_gaps() if the gap blocks are unordered.
2864852Svi117747 	 */
2865852Svi117747 	mp1 = mp;
2866852Svi117747 
2867852Svi117747 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
2868852Svi117747 	xtsn = ntohl(sdc->sdh_tsn);
2869852Svi117747 	ASSERT(xtsn == cumtsn + 1);
2870852Svi117747 
2871852Svi117747 	/*
28720Sstevel@tonic-gate 	 * Go through SACK gaps. They are ordered based on start TSN.
28730Sstevel@tonic-gate 	 */
28740Sstevel@tonic-gate 	ssf = (sctp_sack_frag_t *)(sc + 1);
2875852Svi117747 	for (i = 0; i < num_gaps; i++, ssf++) {
2876852Svi117747 		if (gapstart != 0) {
2877852Svi117747 			/* check for unordered gap */
2878852Svi117747 			if (SEQ_LEQ(cumtsn + ntohs(ssf->ssf_start), gapstart)) {
2879852Svi117747 				acked += sctp_process_uo_gaps(sctp,
2880852Svi117747 				    cumtsn, ssf, num_gaps - i,
2881852Svi117747 				    sctp->sctp_xmit_head, mp1,
2882852Svi117747 				    &trysend, &fast_recovery, gapstart);
2883852Svi117747 				if (trysend < 0) {
288412869SKacheong.Poon@Sun.COM 					SCTPS_BUMP_MIB(sctps, sctpInAckUnsent);
2885852Svi117747 					return (-1);
2886852Svi117747 				}
2887852Svi117747 				break;
2888852Svi117747 			}
2889852Svi117747 		}
2890852Svi117747 		gapstart = cumtsn + ntohs(ssf->ssf_start);
2891852Svi117747 		gapend = cumtsn + ntohs(ssf->ssf_end);
2892852Svi117747 
289310068SChandrasekar.Marimuthu@Sun.COM 		/*
289410068SChandrasekar.Marimuthu@Sun.COM 		 * Sanity checks:
289510068SChandrasekar.Marimuthu@Sun.COM 		 *
289610068SChandrasekar.Marimuthu@Sun.COM 		 * 1. SACK for TSN we have not sent - ABORT
289710068SChandrasekar.Marimuthu@Sun.COM 		 * 2. Invalid or spurious gaps, ignore all gaps
289810068SChandrasekar.Marimuthu@Sun.COM 		 */
2899852Svi117747 		if (SEQ_GT(gapstart, sctp->sctp_ltsn - 1) ||
2900852Svi117747 		    SEQ_GT(gapend, sctp->sctp_ltsn - 1)) {
290112869SKacheong.Poon@Sun.COM 			SCTPS_BUMP_MIB(sctps, sctpInAckUnsent);
2902852Svi117747 			return (-1);
290310068SChandrasekar.Marimuthu@Sun.COM 		} else if (SEQ_LT(gapend, gapstart) ||
290410068SChandrasekar.Marimuthu@Sun.COM 		    SEQ_LEQ(gapstart, cumtsn)) {
2905852Svi117747 			break;
2906852Svi117747 		}
2907852Svi117747 		/*
2908852Svi117747 		 * Let's start at the current TSN (for the 1st gap we start
2909852Svi117747 		 * from the cumulative TSN, for subsequent ones we start from
2910852Svi117747 		 * where the previous gapend was found - second while loop
2911852Svi117747 		 * below) and walk the transmit list till we find the TSN
2912852Svi117747 		 * corresponding to gapstart. All the unacked chunks till we
2913852Svi117747 		 * get to the chunk with TSN == gapstart will have their
2914852Svi117747 		 * SACKCNT incremented by 1. Note since the gap blocks are
2915852Svi117747 		 * ordered, we won't be incrementing the SACKCNT for an
2916852Svi117747 		 * unacked chunk by more than one while processing the gap
2917852Svi117747 		 * blocks. If the SACKCNT for any unacked chunk exceeds
2918852Svi117747 		 * the fast retransmit threshold, we will fast retransmit
2919852Svi117747 		 * after processing all the gap blocks.
2920852Svi117747 		 */
292110068SChandrasekar.Marimuthu@Sun.COM 		ASSERT(SEQ_LEQ(xtsn, gapstart));
29220Sstevel@tonic-gate 		while (xtsn != gapstart) {
29230Sstevel@tonic-gate 			SCTP_CHUNK_SET_SACKCNT(mp, SCTP_CHUNK_SACKCNT(mp) + 1);
29243448Sdh155122 			if (SCTP_CHUNK_SACKCNT(mp) ==
29253448Sdh155122 			    sctps->sctps_fast_rxt_thresh) {
292612604SGeorge.Shepherd@Sun.COM 				SCTP_CHUNK_REXMIT(sctp, mp);
29270Sstevel@tonic-gate 				sctp->sctp_chk_fast_rexmit = B_TRUE;
29280Sstevel@tonic-gate 				trysend = 1;
29290Sstevel@tonic-gate 				if (!fast_recovery) {
29300Sstevel@tonic-gate 					/*
29310Sstevel@tonic-gate 					 * Entering fast recovery.
29320Sstevel@tonic-gate 					 */
29330Sstevel@tonic-gate 					fp = SCTP_CHUNK_DEST(mp);
293413009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_ssthresh = fp->sf_cwnd / 2;
293513009SChandrasekar.Marimuthu@Sun.COM 					if (fp->sf_ssthresh < 2 * fp->sf_pmss) {
293613009SChandrasekar.Marimuthu@Sun.COM 						fp->sf_ssthresh =
293713009SChandrasekar.Marimuthu@Sun.COM 						    2 * fp->sf_pmss;
29380Sstevel@tonic-gate 					}
293913009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_cwnd = fp->sf_ssthresh;
294013009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_pba = 0;
29410Sstevel@tonic-gate 					sctp->sctp_recovery_tsn =
29420Sstevel@tonic-gate 					    sctp->sctp_ltsn - 1;
29430Sstevel@tonic-gate 					fast_recovery = B_TRUE;
29440Sstevel@tonic-gate 				}
29450Sstevel@tonic-gate 			}
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 			/*
29480Sstevel@tonic-gate 			 * Peer may have reneged on this chunk, so un-sack
29490Sstevel@tonic-gate 			 * it now. If the peer did renege, we need to
29500Sstevel@tonic-gate 			 * readjust unacked.
29510Sstevel@tonic-gate 			 */
29520Sstevel@tonic-gate 			if (SCTP_CHUNK_ISACKED(mp)) {
29530Sstevel@tonic-gate 				chunklen = ntohs(sdc->sdh_len);
29540Sstevel@tonic-gate 				fp = SCTP_CHUNK_DEST(mp);
295513009SChandrasekar.Marimuthu@Sun.COM 				fp->sf_suna += chunklen;
29560Sstevel@tonic-gate 				sctp->sctp_unacked += chunklen - sizeof (*sdc);
295712604SGeorge.Shepherd@Sun.COM 				SCTP_CHUNK_CLEAR_ACKED(sctp, mp);
295813009SChandrasekar.Marimuthu@Sun.COM 				if (!fp->sf_timer_running) {
29590Sstevel@tonic-gate 					SCTP_FADDR_TIMER_RESTART(sctp, fp,
296013009SChandrasekar.Marimuthu@Sun.COM 					    fp->sf_rto);
29610Sstevel@tonic-gate 				}
29620Sstevel@tonic-gate 			}
29630Sstevel@tonic-gate 
29640Sstevel@tonic-gate 			mp = mp->b_next;
29650Sstevel@tonic-gate 			if (mp == NULL) {
29660Sstevel@tonic-gate 				ump = ump->b_next;
2967852Svi117747 				/*
2968852Svi117747 				 * ump can't be NULL given the sanity check
296910068SChandrasekar.Marimuthu@Sun.COM 				 * above.  But if it is NULL, it means that
297010068SChandrasekar.Marimuthu@Sun.COM 				 * there is a data corruption.  We'd better
297110068SChandrasekar.Marimuthu@Sun.COM 				 * panic.
2972852Svi117747 				 */
297310068SChandrasekar.Marimuthu@Sun.COM 				if (ump == NULL) {
297410068SChandrasekar.Marimuthu@Sun.COM 					panic("Memory corruption detected: gap "
297510068SChandrasekar.Marimuthu@Sun.COM 					    "start TSN 0x%x missing from the "
297610068SChandrasekar.Marimuthu@Sun.COM 					    "xmit list: %p", gapstart,
297710068SChandrasekar.Marimuthu@Sun.COM 					    (void *)sctp);
297810068SChandrasekar.Marimuthu@Sun.COM 				}
29790Sstevel@tonic-gate 				mp = ump->b_cont;
29800Sstevel@tonic-gate 			}
2981852Svi117747 			/*
2982852Svi117747 			 * mp can't be unsent given the sanity check above.
2983852Svi117747 			 */
2984852Svi117747 			ASSERT(SCTP_CHUNK_ISSENT(mp));
29850Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
29860Sstevel@tonic-gate 			xtsn = ntohl(sdc->sdh_tsn);
29870Sstevel@tonic-gate 		}
2988852Svi117747 		/*
2989852Svi117747 		 * Now that we have found the chunk with TSN == 'gapstart',
2990852Svi117747 		 * let's walk till we hit the chunk with TSN == 'gapend'.
2991852Svi117747 		 * All intermediate chunks will be marked ACKED, if they
2992852Svi117747 		 * haven't already been.
2993852Svi117747 		 */
29940Sstevel@tonic-gate 		while (SEQ_LEQ(xtsn, gapend)) {
29950Sstevel@tonic-gate 			/*
29960Sstevel@tonic-gate 			 * SACKed
29970Sstevel@tonic-gate 			 */
29980Sstevel@tonic-gate 			SCTP_CHUNK_SET_SACKCNT(mp, 0);
29990Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISACKED(mp)) {
30000Sstevel@tonic-gate 				SCTP_CHUNK_ACKED(mp);
30010Sstevel@tonic-gate 
30020Sstevel@tonic-gate 				fp = SCTP_CHUNK_DEST(mp);
30030Sstevel@tonic-gate 				chunklen = ntohs(sdc->sdh_len);
300413009SChandrasekar.Marimuthu@Sun.COM 				ASSERT(fp->sf_suna >= chunklen);
300513009SChandrasekar.Marimuthu@Sun.COM 				fp->sf_suna -= chunklen;
300613009SChandrasekar.Marimuthu@Sun.COM 				if (fp->sf_suna == 0) {
30070Sstevel@tonic-gate 					/* All outstanding data acked. */
300813009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_pba = 0;
30090Sstevel@tonic-gate 					SCTP_FADDR_TIMER_STOP(fp);
30100Sstevel@tonic-gate 				}
301113009SChandrasekar.Marimuthu@Sun.COM 				fp->sf_acked += chunklen;
30120Sstevel@tonic-gate 				acked += chunklen;
30130Sstevel@tonic-gate 				sctp->sctp_unacked -= chunklen - sizeof (*sdc);
30140Sstevel@tonic-gate 				ASSERT(sctp->sctp_unacked >= 0);
30150Sstevel@tonic-gate 			}
3016852Svi117747 			/* Go to the next chunk of the current message */
30170Sstevel@tonic-gate 			mp = mp->b_next;
3018852Svi117747 			/*
3019852Svi117747 			 * Move to the next message in the transmit list
3020852Svi117747 			 * if we are done with all the chunks from the current
3021852Svi117747 			 * message. Note, it is possible to hit the end of the
3022852Svi117747 			 * transmit list here, i.e. if we have already completed
302310068SChandrasekar.Marimuthu@Sun.COM 			 * processing the gap block.  But the TSN must be equal
302410068SChandrasekar.Marimuthu@Sun.COM 			 * to the gapend because of the above sanity check.
302510068SChandrasekar.Marimuthu@Sun.COM 			 * If it is not equal, it means that some data is
302610068SChandrasekar.Marimuthu@Sun.COM 			 * missing.
3027852Svi117747 			 * Also, note that we break here, which means we
3028852Svi117747 			 * continue processing gap blocks, if any. In case of
3029852Svi117747 			 * ordered gap blocks there can't be any following
3030852Svi117747 			 * this (if there is it will fail the sanity check
3031852Svi117747 			 * above). In case of un-ordered gap blocks we will
3032852Svi117747 			 * switch to sctp_process_uo_gaps().  In either case
3033852Svi117747 			 * it should be fine to continue with NULL ump/mp,
3034852Svi117747 			 * but we just reset it to xmit_head.
3035852Svi117747 			 */
30360Sstevel@tonic-gate 			if (mp == NULL) {
30370Sstevel@tonic-gate 				ump = ump->b_next;
30380Sstevel@tonic-gate 				if (ump == NULL) {
303910068SChandrasekar.Marimuthu@Sun.COM 					if (xtsn != gapend) {
304010068SChandrasekar.Marimuthu@Sun.COM 						panic("Memory corruption "
304110068SChandrasekar.Marimuthu@Sun.COM 						    "detected: gap end TSN "
304210068SChandrasekar.Marimuthu@Sun.COM 						    "0x%x missing from the "
304310068SChandrasekar.Marimuthu@Sun.COM 						    "xmit list: %p", gapend,
304410068SChandrasekar.Marimuthu@Sun.COM 						    (void *)sctp);
304510068SChandrasekar.Marimuthu@Sun.COM 					}
3046852Svi117747 					ump = sctp->sctp_xmit_head;
3047852Svi117747 					mp = mp1;
3048852Svi117747 					sdc = (sctp_data_hdr_t *)mp->b_rptr;
3049852Svi117747 					xtsn = ntohl(sdc->sdh_tsn);
3050852Svi117747 					break;
30510Sstevel@tonic-gate 				}
30520Sstevel@tonic-gate 				mp = ump->b_cont;
30530Sstevel@tonic-gate 			}
3054852Svi117747 			/*
3055852Svi117747 			 * Likewise, we could hit an unsent chunk once we have
3056852Svi117747 			 * completed processing the gap block. Again, it is
3057852Svi117747 			 * fine to continue processing gap blocks with mp
3058852Svi117747 			 * pointing to the unsent chunk, because if there
3059852Svi117747 			 * are more ordered gap blocks, they will fail the
3060852Svi117747 			 * sanity check, and if there are un-ordered gap blocks,
3061852Svi117747 			 * we will continue processing in sctp_process_uo_gaps()
3062852Svi117747 			 * We just reset the mp to the one we started with.
3063852Svi117747 			 */
30640Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp)) {
3065852Svi117747 				ASSERT(xtsn == gapend);
3066852Svi117747 				ump = sctp->sctp_xmit_head;
3067852Svi117747 				mp = mp1;
3068852Svi117747 				sdc = (sctp_data_hdr_t *)mp->b_rptr;
3069852Svi117747 				xtsn = ntohl(sdc->sdh_tsn);
3070852Svi117747 				break;
30710Sstevel@tonic-gate 			}
30720Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
30730Sstevel@tonic-gate 			xtsn = ntohl(sdc->sdh_tsn);
30740Sstevel@tonic-gate 		}
30750Sstevel@tonic-gate 	}
30760Sstevel@tonic-gate 	if (sctp->sctp_prsctp_aware)
30770Sstevel@tonic-gate 		sctp_check_abandoned_data(sctp, sctp->sctp_current);
30780Sstevel@tonic-gate 	if (sctp->sctp_chk_fast_rexmit)
30790Sstevel@tonic-gate 		sctp_fast_rexmit(sctp);
30800Sstevel@tonic-gate ret:
30810Sstevel@tonic-gate 	trysend += sctp_set_frwnd(sctp, ntohl(sc->ssc_a_rwnd));
30820Sstevel@tonic-gate 
30830Sstevel@tonic-gate 	/*
30840Sstevel@tonic-gate 	 * If receive window is closed while there is unsent data,
30850Sstevel@tonic-gate 	 * set a timer for doing zero window probes.
30860Sstevel@tonic-gate 	 */
30870Sstevel@tonic-gate 	if (sctp->sctp_frwnd == 0 && sctp->sctp_unacked == 0 &&
30880Sstevel@tonic-gate 	    sctp->sctp_unsent != 0) {
30890Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
309013009SChandrasekar.Marimuthu@Sun.COM 		    sctp->sctp_current->sf_rto);
30910Sstevel@tonic-gate 	}
30920Sstevel@tonic-gate 
30930Sstevel@tonic-gate 	/*
30940Sstevel@tonic-gate 	 * Set cwnd for all destinations.
30950Sstevel@tonic-gate 	 * Congestion window gets increased only when cumulative
30960Sstevel@tonic-gate 	 * TSN moves forward, we're not in fast recovery, and
30970Sstevel@tonic-gate 	 * cwnd has been fully utilized (almost fully, need to allow
30980Sstevel@tonic-gate 	 * some leeway due to non-MSS sized messages).
30990Sstevel@tonic-gate 	 */
310013009SChandrasekar.Marimuthu@Sun.COM 	if (sctp->sctp_current->sf_acked == acked) {
31010Sstevel@tonic-gate 		/*
31020Sstevel@tonic-gate 		 * Fast-path, only data sent to sctp_current got acked.
31030Sstevel@tonic-gate 		 */
31040Sstevel@tonic-gate 		fp = sctp->sctp_current;
31050Sstevel@tonic-gate 		if (cumack_forward && !fast_recovery &&
310613009SChandrasekar.Marimuthu@Sun.COM 		    (fp->sf_acked + fp->sf_suna > fp->sf_cwnd - fp->sf_pmss)) {
310713009SChandrasekar.Marimuthu@Sun.COM 			if (fp->sf_cwnd < fp->sf_ssthresh) {
31080Sstevel@tonic-gate 				/*
31090Sstevel@tonic-gate 				 * Slow start
31100Sstevel@tonic-gate 				 */
311113009SChandrasekar.Marimuthu@Sun.COM 				if (fp->sf_acked > fp->sf_pmss) {
311213009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_cwnd += fp->sf_pmss;
31130Sstevel@tonic-gate 				} else {
311413009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_cwnd += fp->sf_acked;
31150Sstevel@tonic-gate 				}
311613009SChandrasekar.Marimuthu@Sun.COM 				fp->sf_cwnd = MIN(fp->sf_cwnd,
311713009SChandrasekar.Marimuthu@Sun.COM 				    sctp->sctp_cwnd_max);
31180Sstevel@tonic-gate 			} else {
31190Sstevel@tonic-gate 				/*
31200Sstevel@tonic-gate 				 * Congestion avoidance
31210Sstevel@tonic-gate 				 */
312213009SChandrasekar.Marimuthu@Sun.COM 				fp->sf_pba += fp->sf_acked;
312313009SChandrasekar.Marimuthu@Sun.COM 				if (fp->sf_pba >= fp->sf_cwnd) {
312413009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_pba -= fp->sf_cwnd;
312513009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_cwnd += fp->sf_pmss;
312613009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_cwnd = MIN(fp->sf_cwnd,
31270Sstevel@tonic-gate 					    sctp->sctp_cwnd_max);
31280Sstevel@tonic-gate 				}
31290Sstevel@tonic-gate 			}
31300Sstevel@tonic-gate 		}
31310Sstevel@tonic-gate 		/*
31320Sstevel@tonic-gate 		 * Limit the burst of transmitted data segments.
31330Sstevel@tonic-gate 		 */
313413009SChandrasekar.Marimuthu@Sun.COM 		if (fp->sf_suna + sctps->sctps_maxburst * fp->sf_pmss <
313513009SChandrasekar.Marimuthu@Sun.COM 		    fp->sf_cwnd) {
313613009SChandrasekar.Marimuthu@Sun.COM 			fp->sf_cwnd = fp->sf_suna + sctps->sctps_maxburst *
313713009SChandrasekar.Marimuthu@Sun.COM 			    fp->sf_pmss;
31380Sstevel@tonic-gate 		}
313913009SChandrasekar.Marimuthu@Sun.COM 		fp->sf_acked = 0;
31401735Skcpoon 		goto check_ss_rxmit;
31410Sstevel@tonic-gate 	}
314213009SChandrasekar.Marimuthu@Sun.COM 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->sf_next) {
314313009SChandrasekar.Marimuthu@Sun.COM 		if (cumack_forward && fp->sf_acked && !fast_recovery &&
314413009SChandrasekar.Marimuthu@Sun.COM 		    (fp->sf_acked + fp->sf_suna > fp->sf_cwnd - fp->sf_pmss)) {
314513009SChandrasekar.Marimuthu@Sun.COM 			if (fp->sf_cwnd < fp->sf_ssthresh) {
314613009SChandrasekar.Marimuthu@Sun.COM 				if (fp->sf_acked > fp->sf_pmss) {
314713009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_cwnd += fp->sf_pmss;
31480Sstevel@tonic-gate 				} else {
314913009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_cwnd += fp->sf_acked;
31500Sstevel@tonic-gate 				}
315113009SChandrasekar.Marimuthu@Sun.COM 				fp->sf_cwnd = MIN(fp->sf_cwnd,
315213009SChandrasekar.Marimuthu@Sun.COM 				    sctp->sctp_cwnd_max);
31530Sstevel@tonic-gate 			} else {
315413009SChandrasekar.Marimuthu@Sun.COM 				fp->sf_pba += fp->sf_acked;
315513009SChandrasekar.Marimuthu@Sun.COM 				if (fp->sf_pba >= fp->sf_cwnd) {
315613009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_pba -= fp->sf_cwnd;
315713009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_cwnd += fp->sf_pmss;
315813009SChandrasekar.Marimuthu@Sun.COM 					fp->sf_cwnd = MIN(fp->sf_cwnd,
31590Sstevel@tonic-gate 					    sctp->sctp_cwnd_max);
31600Sstevel@tonic-gate 				}
31610Sstevel@tonic-gate 			}
31620Sstevel@tonic-gate 		}
316313009SChandrasekar.Marimuthu@Sun.COM 		if (fp->sf_suna + sctps->sctps_maxburst * fp->sf_pmss <
316413009SChandrasekar.Marimuthu@Sun.COM 		    fp->sf_cwnd) {
316513009SChandrasekar.Marimuthu@Sun.COM 			fp->sf_cwnd = fp->sf_suna + sctps->sctps_maxburst *
316613009SChandrasekar.Marimuthu@Sun.COM 			    fp->sf_pmss;
31670Sstevel@tonic-gate 		}
316813009SChandrasekar.Marimuthu@Sun.COM 		fp->sf_acked = 0;
31690Sstevel@tonic-gate 	}
31704311Svi117747 	fp = sctp->sctp_current;
31711735Skcpoon check_ss_rxmit:
31721735Skcpoon 	/*
31731735Skcpoon 	 * If this is a SACK following a timeout, check if there are
31741735Skcpoon 	 * still unacked chunks (sent before the timeout) that we can
31751735Skcpoon 	 * send.
31761735Skcpoon 	 */
31771735Skcpoon 	if (sctp->sctp_rexmitting) {
31781735Skcpoon 		if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_rxt_maxtsn)) {
31791735Skcpoon 			/*
31801735Skcpoon 			 * As we are in retransmission phase, we may get a
31811735Skcpoon 			 * SACK which indicates some new chunks are received
31821735Skcpoon 			 * but cum_tsn does not advance.  During this
31831735Skcpoon 			 * phase, the other side advances cum_tsn only because
31841735Skcpoon 			 * it receives our retransmitted chunks.  Only
31851735Skcpoon 			 * this signals that some chunks are still
31861735Skcpoon 			 * missing.
31871735Skcpoon 			 */
31883795Skcpoon 			if (cumack_forward) {
318913009SChandrasekar.Marimuthu@Sun.COM 				fp->sf_rxt_unacked -= acked;
31901735Skcpoon 				sctp_ss_rexmit(sctp);
31913795Skcpoon 			}
31921735Skcpoon 		} else {
31931735Skcpoon 			sctp->sctp_rexmitting = B_FALSE;
31941735Skcpoon 			sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn;
31951735Skcpoon 			sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn;
319613009SChandrasekar.Marimuthu@Sun.COM 			fp->sf_rxt_unacked = 0;
31971735Skcpoon 		}
31981735Skcpoon 	}
31990Sstevel@tonic-gate 	return (trysend);
32000Sstevel@tonic-gate }
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate /*
32030Sstevel@tonic-gate  * Returns 0 if the caller should stop processing any more chunks,
32040Sstevel@tonic-gate  * 1 if the caller should skip this chunk and continue processing.
32050Sstevel@tonic-gate  */
32060Sstevel@tonic-gate static int
sctp_strange_chunk(sctp_t * sctp,sctp_chunk_hdr_t * ch,sctp_faddr_t * fp)32070Sstevel@tonic-gate sctp_strange_chunk(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp)
32080Sstevel@tonic-gate {
32090Sstevel@tonic-gate 	size_t len;
32100Sstevel@tonic-gate 
32110Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
32120Sstevel@tonic-gate 	/* check top two bits for action required */
32130Sstevel@tonic-gate 	if (ch->sch_id & 0x40) {	/* also matches 0xc0 */
32140Sstevel@tonic-gate 		len = ntohs(ch->sch_len);
32154964Skcpoon 		sctp_add_err(sctp, SCTP_ERR_UNREC_CHUNK, ch, len, fp);
32164964Skcpoon 
32170Sstevel@tonic-gate 		if ((ch->sch_id & 0xc0) == 0xc0) {
32180Sstevel@tonic-gate 			/* skip and continue */
32190Sstevel@tonic-gate 			return (1);
32200Sstevel@tonic-gate 		} else {
32210Sstevel@tonic-gate 			/* stop processing */
32220Sstevel@tonic-gate 			return (0);
32230Sstevel@tonic-gate 		}
32240Sstevel@tonic-gate 	}
32250Sstevel@tonic-gate 	if (ch->sch_id & 0x80) {
32260Sstevel@tonic-gate 		/* skip and continue, no error */
32270Sstevel@tonic-gate 		return (1);
32280Sstevel@tonic-gate 	}
32290Sstevel@tonic-gate 	/* top two bits are clear; stop processing and no error */
32300Sstevel@tonic-gate 	return (0);
32310Sstevel@tonic-gate }
32320Sstevel@tonic-gate 
32330Sstevel@tonic-gate /*
32340Sstevel@tonic-gate  * Basic sanity checks on all input chunks and parameters: they must
32350Sstevel@tonic-gate  * be of legitimate size for their purported type, and must follow
32360Sstevel@tonic-gate  * ordering conventions as defined in rfc2960.
32370Sstevel@tonic-gate  *
32380Sstevel@tonic-gate  * Returns 1 if the chunk and all encloded params are legitimate,
32390Sstevel@tonic-gate  * 0 otherwise.
32400Sstevel@tonic-gate  */
32410Sstevel@tonic-gate /*ARGSUSED*/
32420Sstevel@tonic-gate static int
sctp_check_input(sctp_t * sctp,sctp_chunk_hdr_t * ch,ssize_t len,int first)32430Sstevel@tonic-gate sctp_check_input(sctp_t *sctp, sctp_chunk_hdr_t *ch, ssize_t len, int first)
32440Sstevel@tonic-gate {
32450Sstevel@tonic-gate 	sctp_parm_hdr_t	*ph;
32460Sstevel@tonic-gate 	void		*p = NULL;
32470Sstevel@tonic-gate 	ssize_t		clen;
32480Sstevel@tonic-gate 	uint16_t	ch_len;
32490Sstevel@tonic-gate 
32500Sstevel@tonic-gate 	ch_len = ntohs(ch->sch_len);
32510Sstevel@tonic-gate 	if (ch_len > len) {
32520Sstevel@tonic-gate 		return (0);
32530Sstevel@tonic-gate 	}
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 	switch (ch->sch_id) {
32560Sstevel@tonic-gate 	case CHUNK_DATA:
32570Sstevel@tonic-gate 		if (ch_len < sizeof (sctp_data_hdr_t)) {
32580Sstevel@tonic-gate 			return (0);
32590Sstevel@tonic-gate 		}
32600Sstevel@tonic-gate 		return (1);
32610Sstevel@tonic-gate 	case CHUNK_INIT:
32620Sstevel@tonic-gate 	case CHUNK_INIT_ACK:
32630Sstevel@tonic-gate 		{
32640Sstevel@tonic-gate 			ssize_t	remlen = len;
32650Sstevel@tonic-gate 
32660Sstevel@tonic-gate 			/*
32670Sstevel@tonic-gate 			 * INIT and INIT-ACK chunks must not be bundled with
32680Sstevel@tonic-gate 			 * any other.
32690Sstevel@tonic-gate 			 */
32700Sstevel@tonic-gate 			if (!first || sctp_next_chunk(ch, &remlen) != NULL ||
32710Sstevel@tonic-gate 			    (ch_len < (sizeof (*ch) +
32720Sstevel@tonic-gate 			    sizeof (sctp_init_chunk_t)))) {
32730Sstevel@tonic-gate 				return (0);
32740Sstevel@tonic-gate 			}
32750Sstevel@tonic-gate 			/* may have params that need checking */
32760Sstevel@tonic-gate 			p = (char *)(ch + 1) + sizeof (sctp_init_chunk_t);
32770Sstevel@tonic-gate 			clen = ch_len - (sizeof (*ch) +
32780Sstevel@tonic-gate 			    sizeof (sctp_init_chunk_t));
32790Sstevel@tonic-gate 		}
32800Sstevel@tonic-gate 		break;
32810Sstevel@tonic-gate 	case CHUNK_SACK:
32820Sstevel@tonic-gate 		if (ch_len < (sizeof (*ch) + sizeof (sctp_sack_chunk_t))) {
32830Sstevel@tonic-gate 			return (0);
32840Sstevel@tonic-gate 		}
32850Sstevel@tonic-gate 		/* dup and gap reports checked by got_sack() */
32860Sstevel@tonic-gate 		return (1);
32870Sstevel@tonic-gate 	case CHUNK_SHUTDOWN:
32880Sstevel@tonic-gate 		if (ch_len < (sizeof (*ch) + sizeof (uint32_t))) {
32890Sstevel@tonic-gate 			return (0);
32900Sstevel@tonic-gate 		}
32910Sstevel@tonic-gate 		return (1);
32920Sstevel@tonic-gate 	case CHUNK_ABORT:
32930Sstevel@tonic-gate 	case CHUNK_ERROR:
32940Sstevel@tonic-gate 		if (ch_len < sizeof (*ch)) {
32950Sstevel@tonic-gate 			return (0);
32960Sstevel@tonic-gate 		}
32970Sstevel@tonic-gate 		/* may have params that need checking */
32980Sstevel@tonic-gate 		p = ch + 1;
32990Sstevel@tonic-gate 		clen = ch_len - sizeof (*ch);
33000Sstevel@tonic-gate 		break;
33010Sstevel@tonic-gate 	case CHUNK_ECNE:
33020Sstevel@tonic-gate 	case CHUNK_CWR:
33030Sstevel@tonic-gate 	case CHUNK_HEARTBEAT:
33040Sstevel@tonic-gate 	case CHUNK_HEARTBEAT_ACK:
33050Sstevel@tonic-gate 	/* Full ASCONF chunk and parameter checks are in asconf.c */
33060Sstevel@tonic-gate 	case CHUNK_ASCONF:
33070Sstevel@tonic-gate 	case CHUNK_ASCONF_ACK:
33080Sstevel@tonic-gate 		if (ch_len < sizeof (*ch)) {
33090Sstevel@tonic-gate 			return (0);
33100Sstevel@tonic-gate 		}
33110Sstevel@tonic-gate 		/* heartbeat data checked by process_heartbeat() */
33120Sstevel@tonic-gate 		return (1);
33130Sstevel@tonic-gate 	case CHUNK_SHUTDOWN_COMPLETE:
33140Sstevel@tonic-gate 		{
33150Sstevel@tonic-gate 			ssize_t remlen = len;
33160Sstevel@tonic-gate 
33170Sstevel@tonic-gate 			/*
33180Sstevel@tonic-gate 			 * SHUTDOWN-COMPLETE chunk must not be bundled with any
33190Sstevel@tonic-gate 			 * other
33200Sstevel@tonic-gate 			 */
33210Sstevel@tonic-gate 			if (!first || sctp_next_chunk(ch, &remlen) != NULL ||
33220Sstevel@tonic-gate 			    ch_len < sizeof (*ch)) {
33230Sstevel@tonic-gate 				return (0);
33240Sstevel@tonic-gate 			}
33250Sstevel@tonic-gate 		}
33260Sstevel@tonic-gate 		return (1);
33270Sstevel@tonic-gate 	case CHUNK_COOKIE:
33280Sstevel@tonic-gate 	case CHUNK_COOKIE_ACK:
33290Sstevel@tonic-gate 	case CHUNK_SHUTDOWN_ACK:
33300Sstevel@tonic-gate 		if (ch_len < sizeof (*ch) || !first) {
33310Sstevel@tonic-gate 			return (0);
33320Sstevel@tonic-gate 		}
33330Sstevel@tonic-gate 		return (1);
33340Sstevel@tonic-gate 	case CHUNK_FORWARD_TSN:
33350Sstevel@tonic-gate 		if (ch_len < (sizeof (*ch) + sizeof (uint32_t)))
33360Sstevel@tonic-gate 			return (0);
33370Sstevel@tonic-gate 		return (1);
33380Sstevel@tonic-gate 	default:
33390Sstevel@tonic-gate 		return (1);	/* handled by strange_chunk() */
33400Sstevel@tonic-gate 	}
33410Sstevel@tonic-gate 
33420Sstevel@tonic-gate 	/* check and byteorder parameters */
33430Sstevel@tonic-gate 	if (clen <= 0) {
33440Sstevel@tonic-gate 		return (1);
33450Sstevel@tonic-gate 	}
33460Sstevel@tonic-gate 	ASSERT(p != NULL);
33470Sstevel@tonic-gate 
33480Sstevel@tonic-gate 	ph = p;
33490Sstevel@tonic-gate 	while (ph != NULL && clen > 0) {
33500Sstevel@tonic-gate 		ch_len = ntohs(ph->sph_len);
33510Sstevel@tonic-gate 		if (ch_len > len || ch_len < sizeof (*ph)) {
33520Sstevel@tonic-gate 			return (0);
33530Sstevel@tonic-gate 		}
33540Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &clen);
33550Sstevel@tonic-gate 	}
33560Sstevel@tonic-gate 
33570Sstevel@tonic-gate 	/* All OK */
33580Sstevel@tonic-gate 	return (1);
33590Sstevel@tonic-gate }
33600Sstevel@tonic-gate 
336111042SErik.Nordmark@Sun.COM static mblk_t *
sctp_check_in_policy(mblk_t * mp,ip_recv_attr_t * ira,ip_stack_t * ipst)336211042SErik.Nordmark@Sun.COM sctp_check_in_policy(mblk_t *mp, ip_recv_attr_t *ira, ip_stack_t *ipst)
33630Sstevel@tonic-gate {
33640Sstevel@tonic-gate 	boolean_t policy_present;
33650Sstevel@tonic-gate 	ipha_t *ipha;
33660Sstevel@tonic-gate 	ip6_t *ip6h;
336711042SErik.Nordmark@Sun.COM 	netstack_t	*ns = ipst->ips_netstack;
336811042SErik.Nordmark@Sun.COM 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
336911042SErik.Nordmark@Sun.COM 
33700Sstevel@tonic-gate 	if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
33713448Sdh155122 		policy_present = ipss->ipsec_inbound_v4_policy_present;
33720Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
33730Sstevel@tonic-gate 		ip6h = NULL;
33740Sstevel@tonic-gate 	} else {
33753448Sdh155122 		policy_present = ipss->ipsec_inbound_v6_policy_present;
33760Sstevel@tonic-gate 		ipha = NULL;
33770Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
33780Sstevel@tonic-gate 	}
33790Sstevel@tonic-gate 
338011042SErik.Nordmark@Sun.COM 	if (policy_present) {
33810Sstevel@tonic-gate 		/*
33820Sstevel@tonic-gate 		 * The conn_t parameter is NULL because we already know
33830Sstevel@tonic-gate 		 * nobody's home.
33840Sstevel@tonic-gate 		 */
338511042SErik.Nordmark@Sun.COM 		mp = ipsec_check_global_policy(mp, (conn_t *)NULL,
338611042SErik.Nordmark@Sun.COM 		    ipha, ip6h, ira, ns);
338711042SErik.Nordmark@Sun.COM 		if (mp == NULL)
33880Sstevel@tonic-gate 			return (NULL);
33890Sstevel@tonic-gate 	}
33900Sstevel@tonic-gate 	return (mp);
33910Sstevel@tonic-gate }
33920Sstevel@tonic-gate 
33930Sstevel@tonic-gate /* Handle out-of-the-blue packets */
33940Sstevel@tonic-gate void
sctp_ootb_input(mblk_t * mp,ip_recv_attr_t * ira,ip_stack_t * ipst)339511042SErik.Nordmark@Sun.COM sctp_ootb_input(mblk_t *mp, ip_recv_attr_t *ira, ip_stack_t *ipst)
33960Sstevel@tonic-gate {
33970Sstevel@tonic-gate 	sctp_t			*sctp;
33980Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ch;
33990Sstevel@tonic-gate 	sctp_hdr_t		*sctph;
34000Sstevel@tonic-gate 	in6_addr_t		src, dst;
340111042SErik.Nordmark@Sun.COM 	uint_t			ip_hdr_len = ira->ira_ip_hdr_length;
34020Sstevel@tonic-gate 	ssize_t			mlen;
34033448Sdh155122 	sctp_stack_t		*sctps;
340411042SErik.Nordmark@Sun.COM 	boolean_t		secure;
340511042SErik.Nordmark@Sun.COM 	zoneid_t		zoneid = ira->ira_zoneid;
340611042SErik.Nordmark@Sun.COM 	uchar_t			*rptr;
340711042SErik.Nordmark@Sun.COM 
340811042SErik.Nordmark@Sun.COM 	ASSERT(ira->ira_ill == NULL);
340911042SErik.Nordmark@Sun.COM 
341011042SErik.Nordmark@Sun.COM 	secure = ira->ira_flags & IRAF_IPSEC_SECURE;
341111042SErik.Nordmark@Sun.COM 
34123448Sdh155122 	sctps = ipst->ips_netstack->netstack_sctp;
34133448Sdh155122 
341412869SKacheong.Poon@Sun.COM 	SCTPS_BUMP_MIB(sctps, sctpOutOfBlue);
341512869SKacheong.Poon@Sun.COM 	SCTPS_BUMP_MIB(sctps, sctpInSCTPPkts);
34163448Sdh155122 
34170Sstevel@tonic-gate 	if (mp->b_cont != NULL) {
34180Sstevel@tonic-gate 		/*
34190Sstevel@tonic-gate 		 * All subsequent code is vastly simplified if it can
34200Sstevel@tonic-gate 		 * assume a single contiguous chunk of data.
34210Sstevel@tonic-gate 		 */
34220Sstevel@tonic-gate 		if (pullupmsg(mp, -1) == 0) {
342311042SErik.Nordmark@Sun.COM 			BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
342411042SErik.Nordmark@Sun.COM 			ip_drop_input("ipIfStatsInDiscards", mp, NULL);
342511042SErik.Nordmark@Sun.COM 			freemsg(mp);
34260Sstevel@tonic-gate 			return;
34270Sstevel@tonic-gate 		}
34280Sstevel@tonic-gate 	}
34290Sstevel@tonic-gate 
343011042SErik.Nordmark@Sun.COM 	rptr = mp->b_rptr;
343111042SErik.Nordmark@Sun.COM 	sctph = ((sctp_hdr_t *)&rptr[ip_hdr_len]);
343211042SErik.Nordmark@Sun.COM 	if (ira->ira_flags & IRAF_IS_IPV4) {
343311042SErik.Nordmark@Sun.COM 		ipha_t *ipha;
343411042SErik.Nordmark@Sun.COM 
343511042SErik.Nordmark@Sun.COM 		ipha = (ipha_t *)rptr;
343611042SErik.Nordmark@Sun.COM 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &src);
343711042SErik.Nordmark@Sun.COM 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
343811042SErik.Nordmark@Sun.COM 	} else {
343911042SErik.Nordmark@Sun.COM 		ip6_t *ip6h;
344011042SErik.Nordmark@Sun.COM 
344111042SErik.Nordmark@Sun.COM 		ip6h = (ip6_t *)rptr;
344211042SErik.Nordmark@Sun.COM 		src = ip6h->ip6_src;
344311042SErik.Nordmark@Sun.COM 		dst = ip6h->ip6_dst;
344411042SErik.Nordmark@Sun.COM 	}
344511042SErik.Nordmark@Sun.COM 
34460Sstevel@tonic-gate 	mlen = mp->b_wptr - (uchar_t *)(sctph + 1);
34470Sstevel@tonic-gate 	if ((ch = sctp_first_chunk((uchar_t *)(sctph + 1), mlen)) == NULL) {
34480Sstevel@tonic-gate 		dprint(3, ("sctp_ootb_input: invalid packet\n"));
344911042SErik.Nordmark@Sun.COM 		BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
345011042SErik.Nordmark@Sun.COM 		ip_drop_input("ipIfStatsInDiscards", mp, NULL);
345111042SErik.Nordmark@Sun.COM 		freemsg(mp);
34520Sstevel@tonic-gate 		return;
34530Sstevel@tonic-gate 	}
34540Sstevel@tonic-gate 
34550Sstevel@tonic-gate 	switch (ch->sch_id) {
34560Sstevel@tonic-gate 	case CHUNK_INIT:
34570Sstevel@tonic-gate 		/* no listener; send abort  */
345811042SErik.Nordmark@Sun.COM 		if (secure && sctp_check_in_policy(mp, ira, ipst) == NULL)
34590Sstevel@tonic-gate 			return;
346011042SErik.Nordmark@Sun.COM 		sctp_ootb_send_abort(sctp_init2vtag(ch), 0,
346111042SErik.Nordmark@Sun.COM 		    NULL, 0, mp, 0, B_TRUE, ira, ipst);
34620Sstevel@tonic-gate 		break;
34630Sstevel@tonic-gate 	case CHUNK_INIT_ACK:
34640Sstevel@tonic-gate 		/* check for changed src addr */
34653510Svi117747 		sctp = sctp_addrlist2sctp(mp, sctph, ch, zoneid, sctps);
34660Sstevel@tonic-gate 		if (sctp != NULL) {
34670Sstevel@tonic-gate 			/* success; proceed to normal path */
34680Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_lock);
34690Sstevel@tonic-gate 			if (sctp->sctp_running) {
347011042SErik.Nordmark@Sun.COM 				sctp_add_recvq(sctp, mp, B_FALSE, ira);
34710Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_lock);
34720Sstevel@tonic-gate 			} else {
34730Sstevel@tonic-gate 				/*
34740Sstevel@tonic-gate 				 * If the source address is changed, we
34750Sstevel@tonic-gate 				 * don't need to worry too much about
34760Sstevel@tonic-gate 				 * out of order processing.  So we don't
34770Sstevel@tonic-gate 				 * check if the recvq is empty or not here.
34780Sstevel@tonic-gate 				 */
34790Sstevel@tonic-gate 				sctp->sctp_running = B_TRUE;
34800Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_lock);
348111042SErik.Nordmark@Sun.COM 				sctp_input_data(sctp, mp, ira);
34820Sstevel@tonic-gate 				WAKE_SCTP(sctp);
34830Sstevel@tonic-gate 			}
34840Sstevel@tonic-gate 			SCTP_REFRELE(sctp);
34850Sstevel@tonic-gate 			return;
34860Sstevel@tonic-gate 		}
34870Sstevel@tonic-gate 		/* else bogus init ack; drop it */
34880Sstevel@tonic-gate 		break;
34890Sstevel@tonic-gate 	case CHUNK_SHUTDOWN_ACK:
349011042SErik.Nordmark@Sun.COM 		if (secure && sctp_check_in_policy(mp, ira, ipst) == NULL)
34910Sstevel@tonic-gate 			return;
349211042SErik.Nordmark@Sun.COM 		sctp_ootb_shutdown_ack(mp, ip_hdr_len, ira, ipst);
34930Sstevel@tonic-gate 		return;
34940Sstevel@tonic-gate 	case CHUNK_ERROR:
34950Sstevel@tonic-gate 	case CHUNK_ABORT:
34960Sstevel@tonic-gate 	case CHUNK_COOKIE_ACK:
34970Sstevel@tonic-gate 	case CHUNK_SHUTDOWN_COMPLETE:
34980Sstevel@tonic-gate 		break;
34990Sstevel@tonic-gate 	default:
350011042SErik.Nordmark@Sun.COM 		if (secure && sctp_check_in_policy(mp, ira, ipst) == NULL)
35010Sstevel@tonic-gate 			return;
350211042SErik.Nordmark@Sun.COM 		sctp_ootb_send_abort(sctph->sh_verf, 0,
350311042SErik.Nordmark@Sun.COM 		    NULL, 0, mp, 0, B_TRUE, ira, ipst);
35040Sstevel@tonic-gate 		break;
35050Sstevel@tonic-gate 	}
35060Sstevel@tonic-gate 	freemsg(mp);
35070Sstevel@tonic-gate }
35080Sstevel@tonic-gate 
350911042SErik.Nordmark@Sun.COM /*
351011042SErik.Nordmark@Sun.COM  * Handle sctp packets.
351111042SErik.Nordmark@Sun.COM  * Note that we rele the sctp_t (the caller got a reference on it).
351211042SErik.Nordmark@Sun.COM  */
35130Sstevel@tonic-gate void
sctp_input(conn_t * connp,ipha_t * ipha,ip6_t * ip6h,mblk_t * mp,ip_recv_attr_t * ira)351411042SErik.Nordmark@Sun.COM sctp_input(conn_t *connp, ipha_t *ipha, ip6_t *ip6h, mblk_t *mp,
351511042SErik.Nordmark@Sun.COM     ip_recv_attr_t *ira)
35160Sstevel@tonic-gate {
351711042SErik.Nordmark@Sun.COM 	sctp_t		*sctp = CONN2SCTP(connp);
351811042SErik.Nordmark@Sun.COM 	boolean_t	secure;
351911042SErik.Nordmark@Sun.COM 	ill_t		*ill = ira->ira_ill;
352011042SErik.Nordmark@Sun.COM 	ip_stack_t	*ipst = ill->ill_ipst;
35213448Sdh155122 	ipsec_stack_t	*ipss = ipst->ips_netstack->netstack_ipsec;
352211042SErik.Nordmark@Sun.COM 	iaflags_t	iraflags = ira->ira_flags;
352311042SErik.Nordmark@Sun.COM 	ill_t		*rill = ira->ira_rill;
352411042SErik.Nordmark@Sun.COM 
352511042SErik.Nordmark@Sun.COM 	secure = iraflags & IRAF_IPSEC_SECURE;
35260Sstevel@tonic-gate 
35270Sstevel@tonic-gate 	/*
35280Sstevel@tonic-gate 	 * We check some fields in conn_t without holding a lock.
35290Sstevel@tonic-gate 	 * This should be fine.
35300Sstevel@tonic-gate 	 */
353111042SErik.Nordmark@Sun.COM 	if (((iraflags & IRAF_IS_IPV4) ?
353211042SErik.Nordmark@Sun.COM 	    CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
353311042SErik.Nordmark@Sun.COM 	    CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
353411042SErik.Nordmark@Sun.COM 	    secure) {
353511042SErik.Nordmark@Sun.COM 		mp = ipsec_check_inbound_policy(mp, connp, ipha,
353611042SErik.Nordmark@Sun.COM 		    ip6h, ira);
353711042SErik.Nordmark@Sun.COM 		if (mp == NULL) {
353811042SErik.Nordmark@Sun.COM 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
353911042SErik.Nordmark@Sun.COM 			/* Note that mp is NULL */
354011042SErik.Nordmark@Sun.COM 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
35410Sstevel@tonic-gate 			SCTP_REFRELE(sctp);
35420Sstevel@tonic-gate 			return;
35430Sstevel@tonic-gate 		}
35440Sstevel@tonic-gate 	}
35450Sstevel@tonic-gate 
354611042SErik.Nordmark@Sun.COM 	ira->ira_ill = ira->ira_rill = NULL;
35470Sstevel@tonic-gate 
35480Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_lock);
35490Sstevel@tonic-gate 	if (sctp->sctp_running) {
355011042SErik.Nordmark@Sun.COM 		sctp_add_recvq(sctp, mp, B_FALSE, ira);
35510Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_lock);
355211042SErik.Nordmark@Sun.COM 		goto done;
35530Sstevel@tonic-gate 	} else {
35540Sstevel@tonic-gate 		sctp->sctp_running = B_TRUE;
35550Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_lock);
35560Sstevel@tonic-gate 
35570Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
35580Sstevel@tonic-gate 		if (sctp->sctp_recvq != NULL) {
355911042SErik.Nordmark@Sun.COM 			sctp_add_recvq(sctp, mp, B_TRUE, ira);
35600Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_recvq_lock);
35610Sstevel@tonic-gate 			WAKE_SCTP(sctp);
356211042SErik.Nordmark@Sun.COM 			goto done;
35630Sstevel@tonic-gate 		}
35640Sstevel@tonic-gate 	}
35650Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
356611042SErik.Nordmark@Sun.COM 	if (ira->ira_flags & IRAF_ICMP_ERROR)
356711042SErik.Nordmark@Sun.COM 		sctp_icmp_error(sctp, mp);
356811042SErik.Nordmark@Sun.COM 	else
356911042SErik.Nordmark@Sun.COM 		sctp_input_data(sctp, mp, ira);
35700Sstevel@tonic-gate 	WAKE_SCTP(sctp);
357111042SErik.Nordmark@Sun.COM 
357211042SErik.Nordmark@Sun.COM done:
35730Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
357411042SErik.Nordmark@Sun.COM 	ira->ira_ill = ill;
357511042SErik.Nordmark@Sun.COM 	ira->ira_rill = rill;
35760Sstevel@tonic-gate }
35770Sstevel@tonic-gate 
35780Sstevel@tonic-gate static void
sctp_process_abort(sctp_t * sctp,sctp_chunk_hdr_t * ch,int err)35790Sstevel@tonic-gate sctp_process_abort(sctp_t *sctp, sctp_chunk_hdr_t *ch, int err)
35800Sstevel@tonic-gate {
35813448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
35823448Sdh155122 
358312869SKacheong.Poon@Sun.COM 	SCTPS_BUMP_MIB(sctps, sctpAborted);
35840Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
35850Sstevel@tonic-gate 
358613009SChandrasekar.Marimuthu@Sun.COM 	/*
358713009SChandrasekar.Marimuthu@Sun.COM 	 * SCTP_COMM_LOST is only sent up if the association is
358813009SChandrasekar.Marimuthu@Sun.COM 	 * established (sctp_state >= SCTPS_ESTABLISHED).
358913009SChandrasekar.Marimuthu@Sun.COM 	 */
359013009SChandrasekar.Marimuthu@Sun.COM 	if (sctp->sctp_state >= SCTPS_ESTABLISHED) {
359113009SChandrasekar.Marimuthu@Sun.COM 		sctp_assoc_event(sctp, SCTP_COMM_LOST,
359213009SChandrasekar.Marimuthu@Sun.COM 		    ntohs(((sctp_parm_hdr_t *)(ch + 1))->sph_type), ch);
359313009SChandrasekar.Marimuthu@Sun.COM 	}
359413009SChandrasekar.Marimuthu@Sun.COM 
35950Sstevel@tonic-gate 	sctp_clean_death(sctp, err);
35960Sstevel@tonic-gate }
35970Sstevel@tonic-gate 
35980Sstevel@tonic-gate void
sctp_input_data(sctp_t * sctp,mblk_t * mp,ip_recv_attr_t * ira)359911042SErik.Nordmark@Sun.COM sctp_input_data(sctp_t *sctp, mblk_t *mp, ip_recv_attr_t *ira)
36000Sstevel@tonic-gate {
36010Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ch;
36020Sstevel@tonic-gate 	ssize_t			mlen;
36030Sstevel@tonic-gate 	int			gotdata;
36040Sstevel@tonic-gate 	int			trysend;
36050Sstevel@tonic-gate 	sctp_faddr_t		*fp;
36060Sstevel@tonic-gate 	sctp_init_chunk_t	*iack;
36070Sstevel@tonic-gate 	uint32_t		tsn;
36080Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
360911042SErik.Nordmark@Sun.COM 	ip_pkt_t		ipp;
36100Sstevel@tonic-gate 	in6_addr_t		src;
36110Sstevel@tonic-gate 	in6_addr_t		dst;
36120Sstevel@tonic-gate 	uint_t			ifindex;
36130Sstevel@tonic-gate 	sctp_hdr_t		*sctph;
361411042SErik.Nordmark@Sun.COM 	uint_t			ip_hdr_len = ira->ira_ip_hdr_length;
36150Sstevel@tonic-gate 	mblk_t			*dups = NULL;
36165586Skcpoon 	int			recv_adaptation;
36170Sstevel@tonic-gate 	boolean_t		wake_eager = B_FALSE;
36180Sstevel@tonic-gate 	in6_addr_t		peer_src;
36190Sstevel@tonic-gate 	int64_t			now;
36203448Sdh155122 	sctp_stack_t		*sctps = sctp->sctp_sctps;
36213448Sdh155122 	ip_stack_t		*ipst = sctps->sctps_netstack->netstack_ip;
36224964Skcpoon 	boolean_t		hb_already = B_FALSE;
36238778SErik.Nordmark@Sun.COM 	cred_t			*cr;
36248778SErik.Nordmark@Sun.COM 	pid_t			cpid;
362511042SErik.Nordmark@Sun.COM 	uchar_t			*rptr;
362611042SErik.Nordmark@Sun.COM 	conn_t			*connp = sctp->sctp_connp;
362712721Sanil.udupa@sun.com 	boolean_t		shutdown_ack_needed = B_FALSE;
362811042SErik.Nordmark@Sun.COM 
36290Sstevel@tonic-gate 	ASSERT(DB_TYPE(mp) == M_DATA);
363011042SErik.Nordmark@Sun.COM 	ASSERT(ira->ira_ill == NULL);
36310Sstevel@tonic-gate 
36320Sstevel@tonic-gate 	if (mp->b_cont != NULL) {
36330Sstevel@tonic-gate 		/*
36340Sstevel@tonic-gate 		 * All subsequent code is vastly simplified if it can
36350Sstevel@tonic-gate 		 * assume a single contiguous chunk of data.
36360Sstevel@tonic-gate 		 */
36370Sstevel@tonic-gate 		if (pullupmsg(mp, -1) == 0) {
36383448Sdh155122 			BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
363911042SErik.Nordmark@Sun.COM 			ip_drop_input("ipIfStatsInDiscards", mp, NULL);
36400Sstevel@tonic-gate 			freemsg(mp);
36410Sstevel@tonic-gate 			return;
36420Sstevel@tonic-gate 		}
36430Sstevel@tonic-gate 	}
36440Sstevel@tonic-gate 
36450Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ipkts);
364611042SErik.Nordmark@Sun.COM 	ifindex = ira->ira_ruifindex;
364711042SErik.Nordmark@Sun.COM 
364811042SErik.Nordmark@Sun.COM 	rptr = mp->b_rptr;
364911042SErik.Nordmark@Sun.COM 
365011042SErik.Nordmark@Sun.COM 	ipp.ipp_fields = 0;
365111042SErik.Nordmark@Sun.COM 	if (connp->conn_recv_ancillary.crb_all != 0) {
365211042SErik.Nordmark@Sun.COM 		/*
365311042SErik.Nordmark@Sun.COM 		 * Record packet information in the ip_pkt_t
365411042SErik.Nordmark@Sun.COM 		 */
365511042SErik.Nordmark@Sun.COM 		if (ira->ira_flags & IRAF_IS_IPV4) {
365611042SErik.Nordmark@Sun.COM 			(void) ip_find_hdr_v4((ipha_t *)rptr, &ipp,
365711042SErik.Nordmark@Sun.COM 			    B_FALSE);
365811042SErik.Nordmark@Sun.COM 		} else {
365911042SErik.Nordmark@Sun.COM 			uint8_t nexthdrp;
366011042SErik.Nordmark@Sun.COM 
366111042SErik.Nordmark@Sun.COM 			/*
366211042SErik.Nordmark@Sun.COM 			 * IPv6 packets can only be received by applications
366311042SErik.Nordmark@Sun.COM 			 * that are prepared to receive IPv6 addresses.
366411042SErik.Nordmark@Sun.COM 			 * The IP fanout must ensure this.
366511042SErik.Nordmark@Sun.COM 			 */
366611042SErik.Nordmark@Sun.COM 			ASSERT(connp->conn_family == AF_INET6);
366711042SErik.Nordmark@Sun.COM 
366811042SErik.Nordmark@Sun.COM 			(void) ip_find_hdr_v6(mp, (ip6_t *)rptr, B_TRUE, &ipp,
366911042SErik.Nordmark@Sun.COM 			    &nexthdrp);
367011042SErik.Nordmark@Sun.COM 			ASSERT(nexthdrp == IPPROTO_SCTP);
367111042SErik.Nordmark@Sun.COM 
367211042SErik.Nordmark@Sun.COM 			/* Could have caused a pullup? */
367311042SErik.Nordmark@Sun.COM 			rptr = mp->b_rptr;
367411042SErik.Nordmark@Sun.COM 		}
367511042SErik.Nordmark@Sun.COM 	}
367611042SErik.Nordmark@Sun.COM 
367711042SErik.Nordmark@Sun.COM 	sctph = ((sctp_hdr_t *)&rptr[ip_hdr_len]);
367811042SErik.Nordmark@Sun.COM 
367911042SErik.Nordmark@Sun.COM 	if (ira->ira_flags & IRAF_IS_IPV4) {
368011042SErik.Nordmark@Sun.COM 		ipha_t *ipha;
368111042SErik.Nordmark@Sun.COM 
368211042SErik.Nordmark@Sun.COM 		ipha = (ipha_t *)rptr;
368311042SErik.Nordmark@Sun.COM 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &src);
368411042SErik.Nordmark@Sun.COM 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &dst);
368511042SErik.Nordmark@Sun.COM 	} else {
368611042SErik.Nordmark@Sun.COM 		ip6_t *ip6h;
368711042SErik.Nordmark@Sun.COM 
368811042SErik.Nordmark@Sun.COM 		ip6h = (ip6_t *)rptr;
368911042SErik.Nordmark@Sun.COM 		src = ip6h->ip6_src;
369011042SErik.Nordmark@Sun.COM 		dst = ip6h->ip6_dst;
369111042SErik.Nordmark@Sun.COM 	}
369211042SErik.Nordmark@Sun.COM 
36930Sstevel@tonic-gate 	mlen = mp->b_wptr - (uchar_t *)(sctph + 1);
36940Sstevel@tonic-gate 	ch = sctp_first_chunk((uchar_t *)(sctph + 1), mlen);
36950Sstevel@tonic-gate 	if (ch == NULL) {
36963448Sdh155122 		BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
369711042SErik.Nordmark@Sun.COM 		ip_drop_input("ipIfStatsInDiscards", mp, NULL);
36980Sstevel@tonic-gate 		freemsg(mp);
36990Sstevel@tonic-gate 		return;
37000Sstevel@tonic-gate 	}
37010Sstevel@tonic-gate 
37020Sstevel@tonic-gate 	if (!sctp_check_input(sctp, ch, mlen, 1)) {
37033448Sdh155122 		BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
370411042SErik.Nordmark@Sun.COM 		ip_drop_input("ipIfStatsInDiscards", mp, NULL);
37050Sstevel@tonic-gate 		goto done;
37060Sstevel@tonic-gate 	}
37070Sstevel@tonic-gate 	/*
37080Sstevel@tonic-gate 	 * Check verfication tag (special handling for INIT,
37090Sstevel@tonic-gate 	 * COOKIE, SHUTDOWN_COMPLETE and SHUTDOWN_ACK chunks).
37100Sstevel@tonic-gate 	 * ABORTs are handled in the chunk processing loop, since
37110Sstevel@tonic-gate 	 * may not appear first. All other checked chunks must
37120Sstevel@tonic-gate 	 * appear first, or will have been dropped by check_input().
37130Sstevel@tonic-gate 	 */
37140Sstevel@tonic-gate 	switch (ch->sch_id) {
37150Sstevel@tonic-gate 	case CHUNK_INIT:
37160Sstevel@tonic-gate 		if (sctph->sh_verf != 0) {
37170Sstevel@tonic-gate 			/* drop it */
37180Sstevel@tonic-gate 			goto done;
37190Sstevel@tonic-gate 		}
37200Sstevel@tonic-gate 		break;
37210Sstevel@tonic-gate 	case CHUNK_SHUTDOWN_COMPLETE:
37220Sstevel@tonic-gate 		if (sctph->sh_verf == sctp->sctp_lvtag)
37230Sstevel@tonic-gate 			break;
37240Sstevel@tonic-gate 		if (sctph->sh_verf == sctp->sctp_fvtag &&
37250Sstevel@tonic-gate 		    SCTP_GET_TBIT(ch)) {
37260Sstevel@tonic-gate 			break;
37270Sstevel@tonic-gate 		}
37280Sstevel@tonic-gate 		/* else drop it */
37290Sstevel@tonic-gate 		goto done;
37300Sstevel@tonic-gate 	case CHUNK_ABORT:
37310Sstevel@tonic-gate 	case CHUNK_COOKIE:
37320Sstevel@tonic-gate 		/* handled below */
37330Sstevel@tonic-gate 		break;
37340Sstevel@tonic-gate 	case CHUNK_SHUTDOWN_ACK:
37350Sstevel@tonic-gate 		if (sctp->sctp_state > SCTPS_BOUND &&
37360Sstevel@tonic-gate 		    sctp->sctp_state < SCTPS_ESTABLISHED) {
37370Sstevel@tonic-gate 			/* treat as OOTB */
373811042SErik.Nordmark@Sun.COM 			sctp_ootb_shutdown_ack(mp, ip_hdr_len, ira, ipst);
37390Sstevel@tonic-gate 			return;
37400Sstevel@tonic-gate 		}
37410Sstevel@tonic-gate 		/* else fallthru */
37420Sstevel@tonic-gate 	default:
37430Sstevel@tonic-gate 		/*
37440Sstevel@tonic-gate 		 * All other packets must have a valid
37450Sstevel@tonic-gate 		 * verification tag, however if this is a
37460Sstevel@tonic-gate 		 * listener, we use a refined version of
37470Sstevel@tonic-gate 		 * out-of-the-blue logic.
37480Sstevel@tonic-gate 		 */
37490Sstevel@tonic-gate 		if (sctph->sh_verf != sctp->sctp_lvtag &&
37500Sstevel@tonic-gate 		    sctp->sctp_state != SCTPS_LISTEN) {
37510Sstevel@tonic-gate 			/* drop it */
37520Sstevel@tonic-gate 			goto done;
37530Sstevel@tonic-gate 		}
37540Sstevel@tonic-gate 		break;
37550Sstevel@tonic-gate 	}
37560Sstevel@tonic-gate 
37570Sstevel@tonic-gate 	/* Have a valid sctp for this packet */
37580Sstevel@tonic-gate 	fp = sctp_lookup_faddr(sctp, &src);
37591676Sjpk 	dprint(2, ("sctp_dispatch_rput: mp=%p fp=%p sctp=%p\n", (void *)mp,
37601676Sjpk 	    (void *)fp, (void *)sctp));
37610Sstevel@tonic-gate 
37620Sstevel@tonic-gate 	gotdata = 0;
37630Sstevel@tonic-gate 	trysend = 0;
37640Sstevel@tonic-gate 
376512869SKacheong.Poon@Sun.COM 	now = LBOLT_FASTPATH64;
37660Sstevel@tonic-gate 	/* Process the chunks */
37670Sstevel@tonic-gate 	do {
37680Sstevel@tonic-gate 		dprint(3, ("sctp_dispatch_rput: state=%d, chunk id=%d\n",
37690Sstevel@tonic-gate 		    sctp->sctp_state, (int)(ch->sch_id)));
37700Sstevel@tonic-gate 
37710Sstevel@tonic-gate 		if (ch->sch_id == CHUNK_ABORT) {
37720Sstevel@tonic-gate 			if (sctph->sh_verf != sctp->sctp_lvtag &&
37730Sstevel@tonic-gate 			    sctph->sh_verf != sctp->sctp_fvtag) {
37740Sstevel@tonic-gate 				/* drop it */
37750Sstevel@tonic-gate 				goto done;
37760Sstevel@tonic-gate 			}
37770Sstevel@tonic-gate 		}
37780Sstevel@tonic-gate 
37790Sstevel@tonic-gate 		switch (sctp->sctp_state) {
37800Sstevel@tonic-gate 
37810Sstevel@tonic-gate 		case SCTPS_ESTABLISHED:
37820Sstevel@tonic-gate 		case SCTPS_SHUTDOWN_PENDING:
37830Sstevel@tonic-gate 		case SCTPS_SHUTDOWN_SENT:
37840Sstevel@tonic-gate 			switch (ch->sch_id) {
37850Sstevel@tonic-gate 			case CHUNK_DATA:
37860Sstevel@tonic-gate 				/* 0-length data chunks are not allowed */
37870Sstevel@tonic-gate 				if (ntohs(ch->sch_len) == sizeof (*sdc)) {
37880Sstevel@tonic-gate 					sdc = (sctp_data_hdr_t *)ch;
37890Sstevel@tonic-gate 					tsn = sdc->sdh_tsn;
37900Sstevel@tonic-gate 					sctp_send_abort(sctp, sctp->sctp_fvtag,
37910Sstevel@tonic-gate 					    SCTP_ERR_NO_USR_DATA, (char *)&tsn,
379211042SErik.Nordmark@Sun.COM 					    sizeof (tsn), mp, 0, B_FALSE, ira);
37930Sstevel@tonic-gate 					sctp_assoc_event(sctp, SCTP_COMM_LOST,
37940Sstevel@tonic-gate 					    0, NULL);
37950Sstevel@tonic-gate 					sctp_clean_death(sctp, ECONNABORTED);
37960Sstevel@tonic-gate 					goto done;
37970Sstevel@tonic-gate 				}
37980Sstevel@tonic-gate 
37990Sstevel@tonic-gate 				ASSERT(fp != NULL);
38000Sstevel@tonic-gate 				sctp->sctp_lastdata = fp;
380111042SErik.Nordmark@Sun.COM 				sctp_data_chunk(sctp, ch, mp, &dups, fp,
380211042SErik.Nordmark@Sun.COM 				    &ipp, ira);
38030Sstevel@tonic-gate 				gotdata = 1;
38040Sstevel@tonic-gate 				/* Restart shutdown timer if shutting down */
38050Sstevel@tonic-gate 				if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) {
38060Sstevel@tonic-gate 					/*
38070Sstevel@tonic-gate 					 * If we have exceeded our max
38080Sstevel@tonic-gate 					 * wait bound for waiting for a
38090Sstevel@tonic-gate 					 * shutdown ack from the peer,
38100Sstevel@tonic-gate 					 * abort the association.
38110Sstevel@tonic-gate 					 */
38123448Sdh155122 					if (sctps->sctps_shutack_wait_bound !=
38133448Sdh155122 					    0 &&
38140Sstevel@tonic-gate 					    TICK_TO_MSEC(now -
38150Sstevel@tonic-gate 					    sctp->sctp_out_time) >
38163448Sdh155122 					    sctps->sctps_shutack_wait_bound) {
38170Sstevel@tonic-gate 						sctp_send_abort(sctp,
38180Sstevel@tonic-gate 						    sctp->sctp_fvtag, 0, NULL,
381911042SErik.Nordmark@Sun.COM 						    0, mp, 0, B_FALSE, ira);
38200Sstevel@tonic-gate 						sctp_assoc_event(sctp,
38210Sstevel@tonic-gate 						    SCTP_COMM_LOST, 0, NULL);
38220Sstevel@tonic-gate 						sctp_clean_death(sctp,
38230Sstevel@tonic-gate 						    ECONNABORTED);
38240Sstevel@tonic-gate 						goto done;
38250Sstevel@tonic-gate 					}
38260Sstevel@tonic-gate 					SCTP_FADDR_TIMER_RESTART(sctp, fp,
382713009SChandrasekar.Marimuthu@Sun.COM 					    fp->sf_rto);
38280Sstevel@tonic-gate 				}
38290Sstevel@tonic-gate 				break;
38300Sstevel@tonic-gate 			case CHUNK_SACK:
38310Sstevel@tonic-gate 				ASSERT(fp != NULL);
38320Sstevel@tonic-gate 				/*
38330Sstevel@tonic-gate 				 * Peer is real and alive if it can ack our
38340Sstevel@tonic-gate 				 * data.
38350Sstevel@tonic-gate 				 */
38360Sstevel@tonic-gate 				sctp_faddr_alive(sctp, fp);
38370Sstevel@tonic-gate 				trysend = sctp_got_sack(sctp, ch);
3838852Svi117747 				if (trysend < 0) {
3839852Svi117747 					sctp_send_abort(sctp, sctph->sh_verf,
384011042SErik.Nordmark@Sun.COM 					    0, NULL, 0, mp, 0, B_FALSE, ira);
3841852Svi117747 					sctp_assoc_event(sctp,
3842852Svi117747 					    SCTP_COMM_LOST, 0, NULL);
3843852Svi117747 					sctp_clean_death(sctp,
3844852Svi117747 					    ECONNABORTED);
3845852Svi117747 					goto done;
3846852Svi117747 				}
38470Sstevel@tonic-gate 				break;
38480Sstevel@tonic-gate 			case CHUNK_HEARTBEAT:
38494964Skcpoon 				if (!hb_already) {
38504964Skcpoon 					/*
38514964Skcpoon 					 * In any one packet, there should
38524964Skcpoon 					 * only be one heartbeat chunk.  So
38534964Skcpoon 					 * we should not process more than
38544964Skcpoon 					 * once.
38554964Skcpoon 					 */
38564964Skcpoon 					sctp_return_heartbeat(sctp, ch, mp);
38574964Skcpoon 					hb_already = B_TRUE;
38584964Skcpoon 				}
38590Sstevel@tonic-gate 				break;
38600Sstevel@tonic-gate 			case CHUNK_HEARTBEAT_ACK:
38610Sstevel@tonic-gate 				sctp_process_heartbeat(sctp, ch);
38620Sstevel@tonic-gate 				break;
38630Sstevel@tonic-gate 			case CHUNK_SHUTDOWN:
38640Sstevel@tonic-gate 				sctp_shutdown_event(sctp);
38650Sstevel@tonic-gate 				trysend = sctp_shutdown_received(sctp, ch,
38661735Skcpoon 				    B_FALSE, B_FALSE, fp);
38670Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
38680Sstevel@tonic-gate 				break;
38690Sstevel@tonic-gate 			case CHUNK_SHUTDOWN_ACK:
38700Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
38710Sstevel@tonic-gate 				if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) {
38720Sstevel@tonic-gate 					sctp_shutdown_complete(sctp);
387312869SKacheong.Poon@Sun.COM 					SCTPS_BUMP_MIB(sctps, sctpShutdowns);
38740Sstevel@tonic-gate 					sctp_assoc_event(sctp,
38750Sstevel@tonic-gate 					    SCTP_SHUTDOWN_COMP, 0, NULL);
38760Sstevel@tonic-gate 					sctp_clean_death(sctp, 0);
38770Sstevel@tonic-gate 					goto done;
38780Sstevel@tonic-gate 				}
38790Sstevel@tonic-gate 				break;
38800Sstevel@tonic-gate 			case CHUNK_ABORT: {
38810Sstevel@tonic-gate 				sctp_saddr_ipif_t *sp;
38820Sstevel@tonic-gate 
38830Sstevel@tonic-gate 				/* Ignore if delete pending */
3884852Svi117747 				sp = sctp_saddr_lookup(sctp, &dst, 0);
38850Sstevel@tonic-gate 				ASSERT(sp != NULL);
38860Sstevel@tonic-gate 				if (sp->saddr_ipif_delete_pending) {
38870Sstevel@tonic-gate 					BUMP_LOCAL(sctp->sctp_ibchunks);
38880Sstevel@tonic-gate 					break;
38890Sstevel@tonic-gate 				}
38900Sstevel@tonic-gate 
38910Sstevel@tonic-gate 				sctp_process_abort(sctp, ch, ECONNRESET);
38920Sstevel@tonic-gate 				goto done;
38930Sstevel@tonic-gate 			}
38940Sstevel@tonic-gate 			case CHUNK_INIT:
389511042SErik.Nordmark@Sun.COM 				sctp_send_initack(sctp, sctph, ch, mp, ira);
38960Sstevel@tonic-gate 				break;
38970Sstevel@tonic-gate 			case CHUNK_COOKIE:
38980Sstevel@tonic-gate 				if (sctp_process_cookie(sctp, ch, mp, &iack,
389911042SErik.Nordmark@Sun.COM 				    sctph, &recv_adaptation, NULL, ira) != -1) {
39000Sstevel@tonic-gate 					sctp_send_cookie_ack(sctp);
39010Sstevel@tonic-gate 					sctp_assoc_event(sctp, SCTP_RESTART,
39020Sstevel@tonic-gate 					    0, NULL);
39035586Skcpoon 					if (recv_adaptation) {
39045586Skcpoon 						sctp->sctp_recv_adaptation = 1;
39055586Skcpoon 						sctp_adaptation_event(sctp);
39060Sstevel@tonic-gate 					}
39070Sstevel@tonic-gate 				} else {
390812869SKacheong.Poon@Sun.COM 					SCTPS_BUMP_MIB(sctps,
39090Sstevel@tonic-gate 					    sctpInInvalidCookie);
39100Sstevel@tonic-gate 				}
39110Sstevel@tonic-gate 				break;
39120Sstevel@tonic-gate 			case CHUNK_ERROR: {
39130Sstevel@tonic-gate 				int error;
39140Sstevel@tonic-gate 
39150Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
391611042SErik.Nordmark@Sun.COM 				error = sctp_handle_error(sctp, sctph, ch, mp,
391711042SErik.Nordmark@Sun.COM 				    ira);
39180Sstevel@tonic-gate 				if (error != 0) {
39193314Skcpoon 					sctp_assoc_event(sctp, SCTP_COMM_LOST,
39203314Skcpoon 					    0, NULL);
39210Sstevel@tonic-gate 					sctp_clean_death(sctp, error);
39220Sstevel@tonic-gate 					goto done;
39230Sstevel@tonic-gate 				}
39240Sstevel@tonic-gate 				break;
39250Sstevel@tonic-gate 			}
39260Sstevel@tonic-gate 			case CHUNK_ASCONF:
39270Sstevel@tonic-gate 				ASSERT(fp != NULL);
39280Sstevel@tonic-gate 				sctp_input_asconf(sctp, ch, fp);
39290Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
39300Sstevel@tonic-gate 				break;
39310Sstevel@tonic-gate 			case CHUNK_ASCONF_ACK:
39320Sstevel@tonic-gate 				ASSERT(fp != NULL);
39330Sstevel@tonic-gate 				sctp_faddr_alive(sctp, fp);
39340Sstevel@tonic-gate 				sctp_input_asconf_ack(sctp, ch, fp);
39350Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
39360Sstevel@tonic-gate 				break;
39370Sstevel@tonic-gate 			case CHUNK_FORWARD_TSN:
39380Sstevel@tonic-gate 				ASSERT(fp != NULL);
39390Sstevel@tonic-gate 				sctp->sctp_lastdata = fp;
394011042SErik.Nordmark@Sun.COM 				sctp_process_forward_tsn(sctp, ch, fp,
394111042SErik.Nordmark@Sun.COM 				    &ipp, ira);
39420Sstevel@tonic-gate 				gotdata = 1;
39430Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
39440Sstevel@tonic-gate 				break;
39450Sstevel@tonic-gate 			default:
39460Sstevel@tonic-gate 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
39470Sstevel@tonic-gate 					goto nomorechunks;
39480Sstevel@tonic-gate 				} /* else skip and continue processing */
39490Sstevel@tonic-gate 				break;
39500Sstevel@tonic-gate 			}
39510Sstevel@tonic-gate 			break;
39520Sstevel@tonic-gate 
39530Sstevel@tonic-gate 		case SCTPS_LISTEN:
39540Sstevel@tonic-gate 			switch (ch->sch_id) {
39550Sstevel@tonic-gate 			case CHUNK_INIT:
395611042SErik.Nordmark@Sun.COM 				sctp_send_initack(sctp, sctph, ch, mp, ira);
39570Sstevel@tonic-gate 				break;
39580Sstevel@tonic-gate 			case CHUNK_COOKIE: {
39590Sstevel@tonic-gate 				sctp_t *eager;
39600Sstevel@tonic-gate 
39610Sstevel@tonic-gate 				if (sctp_process_cookie(sctp, ch, mp, &iack,
396211042SErik.Nordmark@Sun.COM 				    sctph, &recv_adaptation, &peer_src,
396311042SErik.Nordmark@Sun.COM 				    ira) == -1) {
396412869SKacheong.Poon@Sun.COM 					SCTPS_BUMP_MIB(sctps,
39650Sstevel@tonic-gate 					    sctpInInvalidCookie);
39660Sstevel@tonic-gate 					goto done;
39670Sstevel@tonic-gate 				}
39680Sstevel@tonic-gate 
39690Sstevel@tonic-gate 				/*
39700Sstevel@tonic-gate 				 * The cookie is good; ensure that
39710Sstevel@tonic-gate 				 * the peer used the verification
39720Sstevel@tonic-gate 				 * tag from the init ack in the header.
39730Sstevel@tonic-gate 				 */
39740Sstevel@tonic-gate 				if (iack->sic_inittag != sctph->sh_verf)
39750Sstevel@tonic-gate 					goto done;
39760Sstevel@tonic-gate 
39770Sstevel@tonic-gate 				eager = sctp_conn_request(sctp, mp, ifindex,
397811042SErik.Nordmark@Sun.COM 				    ip_hdr_len, iack, ira);
39790Sstevel@tonic-gate 				if (eager == NULL) {
39800Sstevel@tonic-gate 					sctp_send_abort(sctp, sctph->sh_verf,
39810Sstevel@tonic-gate 					    SCTP_ERR_NO_RESOURCES, NULL, 0, mp,
398211042SErik.Nordmark@Sun.COM 					    0, B_FALSE, ira);
39830Sstevel@tonic-gate 					goto done;
39840Sstevel@tonic-gate 				}
39850Sstevel@tonic-gate 
39860Sstevel@tonic-gate 				/*
39870Sstevel@tonic-gate 				 * If there were extra chunks
39880Sstevel@tonic-gate 				 * bundled with the cookie,
39890Sstevel@tonic-gate 				 * they must be processed
39900Sstevel@tonic-gate 				 * on the eager's queue. We
39910Sstevel@tonic-gate 				 * accomplish this by refeeding
39920Sstevel@tonic-gate 				 * the whole packet into the
39930Sstevel@tonic-gate 				 * state machine on the right
39940Sstevel@tonic-gate 				 * q. The packet (mp) gets
39950Sstevel@tonic-gate 				 * there via the eager's
39960Sstevel@tonic-gate 				 * cookie_mp field (overloaded
39970Sstevel@tonic-gate 				 * with the active open role).
39980Sstevel@tonic-gate 				 * This is picked up when
39990Sstevel@tonic-gate 				 * processing the null bind
40000Sstevel@tonic-gate 				 * request put on the eager's
40010Sstevel@tonic-gate 				 * q by sctp_accept(). We must
40020Sstevel@tonic-gate 				 * first revert the cookie
40030Sstevel@tonic-gate 				 * chunk's length field to network
40040Sstevel@tonic-gate 				 * byteorder so it can be
40050Sstevel@tonic-gate 				 * properly reprocessed on the
40060Sstevel@tonic-gate 				 * eager's queue.
40070Sstevel@tonic-gate 				 */
400812869SKacheong.Poon@Sun.COM 				SCTPS_BUMP_MIB(sctps, sctpPassiveEstab);
40090Sstevel@tonic-gate 				if (mlen > ntohs(ch->sch_len)) {
40100Sstevel@tonic-gate 					eager->sctp_cookie_mp = dupb(mp);
40110Sstevel@tonic-gate 					/*
40120Sstevel@tonic-gate 					 * If no mem, just let
40130Sstevel@tonic-gate 					 * the peer retransmit.
40140Sstevel@tonic-gate 					 */
40150Sstevel@tonic-gate 				}
40160Sstevel@tonic-gate 				sctp_assoc_event(eager, SCTP_COMM_UP, 0, NULL);
40175586Skcpoon 				if (recv_adaptation) {
40185586Skcpoon 					eager->sctp_recv_adaptation = 1;
40195586Skcpoon 					eager->sctp_rx_adaptation_code =
40205586Skcpoon 					    sctp->sctp_rx_adaptation_code;
40215586Skcpoon 					sctp_adaptation_event(eager);
40220Sstevel@tonic-gate 				}
40230Sstevel@tonic-gate 
40240Sstevel@tonic-gate 				eager->sctp_active = now;
40250Sstevel@tonic-gate 				sctp_send_cookie_ack(eager);
40260Sstevel@tonic-gate 
40270Sstevel@tonic-gate 				wake_eager = B_TRUE;
40280Sstevel@tonic-gate 
40290Sstevel@tonic-gate 				/*
40300Sstevel@tonic-gate 				 * Process rest of the chunks with eager.
40310Sstevel@tonic-gate 				 */
40320Sstevel@tonic-gate 				sctp = eager;
40330Sstevel@tonic-gate 				fp = sctp_lookup_faddr(sctp, &peer_src);
40340Sstevel@tonic-gate 				/*
40350Sstevel@tonic-gate 				 * Confirm peer's original source.  fp can
40360Sstevel@tonic-gate 				 * only be NULL if peer does not use the
40370Sstevel@tonic-gate 				 * original source as one of its addresses...
40380Sstevel@tonic-gate 				 */
40390Sstevel@tonic-gate 				if (fp == NULL)
40400Sstevel@tonic-gate 					fp = sctp_lookup_faddr(sctp, &src);
40410Sstevel@tonic-gate 				else
40420Sstevel@tonic-gate 					sctp_faddr_alive(sctp, fp);
40430Sstevel@tonic-gate 
40440Sstevel@tonic-gate 				/*
40450Sstevel@tonic-gate 				 * Validate the peer addresses.  It also starts
40460Sstevel@tonic-gate 				 * the heartbeat timer.
40470Sstevel@tonic-gate 				 */
40480Sstevel@tonic-gate 				sctp_validate_peer(sctp);
40490Sstevel@tonic-gate 				break;
40500Sstevel@tonic-gate 			}
40510Sstevel@tonic-gate 			/* Anything else is considered out-of-the-blue */
40520Sstevel@tonic-gate 			case CHUNK_ERROR:
40530Sstevel@tonic-gate 			case CHUNK_ABORT:
40540Sstevel@tonic-gate 			case CHUNK_COOKIE_ACK:
40550Sstevel@tonic-gate 			case CHUNK_SHUTDOWN_COMPLETE:
40560Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
40570Sstevel@tonic-gate 				goto done;
40580Sstevel@tonic-gate 			default:
40590Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
40600Sstevel@tonic-gate 				sctp_send_abort(sctp, sctph->sh_verf, 0, NULL,
406111042SErik.Nordmark@Sun.COM 				    0, mp, 0, B_TRUE, ira);
40620Sstevel@tonic-gate 				goto done;
40630Sstevel@tonic-gate 			}
40640Sstevel@tonic-gate 			break;
40650Sstevel@tonic-gate 
40660Sstevel@tonic-gate 		case SCTPS_COOKIE_WAIT:
40670Sstevel@tonic-gate 			switch (ch->sch_id) {
40680Sstevel@tonic-gate 			case CHUNK_INIT_ACK:
40690Sstevel@tonic-gate 				sctp_stop_faddr_timers(sctp);
40700Sstevel@tonic-gate 				sctp_faddr_alive(sctp, sctp->sctp_current);
407111042SErik.Nordmark@Sun.COM 				sctp_send_cookie_echo(sctp, ch, mp, ira);
40720Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
40730Sstevel@tonic-gate 				break;
40740Sstevel@tonic-gate 			case CHUNK_ABORT:
40750Sstevel@tonic-gate 				sctp_process_abort(sctp, ch, ECONNREFUSED);
40760Sstevel@tonic-gate 				goto done;
40770Sstevel@tonic-gate 			case CHUNK_INIT:
407811042SErik.Nordmark@Sun.COM 				sctp_send_initack(sctp, sctph, ch, mp, ira);
40790Sstevel@tonic-gate 				break;
40800Sstevel@tonic-gate 			case CHUNK_COOKIE:
408111042SErik.Nordmark@Sun.COM 				cr = ira->ira_cred;
408211042SErik.Nordmark@Sun.COM 				cpid = ira->ira_cpid;
40838778SErik.Nordmark@Sun.COM 
40840Sstevel@tonic-gate 				if (sctp_process_cookie(sctp, ch, mp, &iack,
408511042SErik.Nordmark@Sun.COM 				    sctph, &recv_adaptation, NULL, ira) == -1) {
408612869SKacheong.Poon@Sun.COM 					SCTPS_BUMP_MIB(sctps,
40870Sstevel@tonic-gate 					    sctpInInvalidCookie);
40880Sstevel@tonic-gate 					break;
40890Sstevel@tonic-gate 				}
40900Sstevel@tonic-gate 				sctp_send_cookie_ack(sctp);
40910Sstevel@tonic-gate 				sctp_stop_faddr_timers(sctp);
40920Sstevel@tonic-gate 				if (!SCTP_IS_DETACHED(sctp)) {
40934964Skcpoon 					sctp->sctp_ulp_connected(
40948778SErik.Nordmark@Sun.COM 					    sctp->sctp_ulpd, 0, cr, cpid);
40954964Skcpoon 					sctp_set_ulp_prop(sctp);
40968778SErik.Nordmark@Sun.COM 
40970Sstevel@tonic-gate 				}
409812869SKacheong.Poon@Sun.COM 				SCTP_ASSOC_EST(sctps, sctp);
409912869SKacheong.Poon@Sun.COM 				SCTPS_BUMP_MIB(sctps, sctpActiveEstab);
41000Sstevel@tonic-gate 				if (sctp->sctp_cookie_mp) {
41010Sstevel@tonic-gate 					freemsg(sctp->sctp_cookie_mp);
41020Sstevel@tonic-gate 					sctp->sctp_cookie_mp = NULL;
41030Sstevel@tonic-gate 				}
41040Sstevel@tonic-gate 
41050Sstevel@tonic-gate 				/* Validate the peer addresses. */
41060Sstevel@tonic-gate 				sctp->sctp_active = now;
41070Sstevel@tonic-gate 				sctp_validate_peer(sctp);
41080Sstevel@tonic-gate 
41090Sstevel@tonic-gate 				sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL);
41105586Skcpoon 				if (recv_adaptation) {
41115586Skcpoon 					sctp->sctp_recv_adaptation = 1;
41125586Skcpoon 					sctp_adaptation_event(sctp);
41130Sstevel@tonic-gate 				}
41140Sstevel@tonic-gate 				/* Try sending queued data, or ASCONFs */
41150Sstevel@tonic-gate 				trysend = 1;
41160Sstevel@tonic-gate 				break;
41170Sstevel@tonic-gate 			default:
41180Sstevel@tonic-gate 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
41190Sstevel@tonic-gate 					goto nomorechunks;
41200Sstevel@tonic-gate 				} /* else skip and continue processing */
41210Sstevel@tonic-gate 				break;
41220Sstevel@tonic-gate 			}
41230Sstevel@tonic-gate 			break;
41240Sstevel@tonic-gate 
41250Sstevel@tonic-gate 		case SCTPS_COOKIE_ECHOED:
41260Sstevel@tonic-gate 			switch (ch->sch_id) {
41270Sstevel@tonic-gate 			case CHUNK_COOKIE_ACK:
412811042SErik.Nordmark@Sun.COM 				cr = ira->ira_cred;
412911042SErik.Nordmark@Sun.COM 				cpid = ira->ira_cpid;
41308778SErik.Nordmark@Sun.COM 
41310Sstevel@tonic-gate 				if (!SCTP_IS_DETACHED(sctp)) {
41324964Skcpoon 					sctp->sctp_ulp_connected(
41338778SErik.Nordmark@Sun.COM 					    sctp->sctp_ulpd, 0, cr, cpid);
41344964Skcpoon 					sctp_set_ulp_prop(sctp);
41350Sstevel@tonic-gate 				}
41360Sstevel@tonic-gate 				if (sctp->sctp_unacked == 0)
41370Sstevel@tonic-gate 					sctp_stop_faddr_timers(sctp);
413812869SKacheong.Poon@Sun.COM 				SCTP_ASSOC_EST(sctps, sctp);
413912869SKacheong.Poon@Sun.COM 				SCTPS_BUMP_MIB(sctps, sctpActiveEstab);
41400Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
41410Sstevel@tonic-gate 				if (sctp->sctp_cookie_mp) {
41420Sstevel@tonic-gate 					freemsg(sctp->sctp_cookie_mp);
41430Sstevel@tonic-gate 					sctp->sctp_cookie_mp = NULL;
41440Sstevel@tonic-gate 				}
41450Sstevel@tonic-gate 				sctp_faddr_alive(sctp, fp);
41460Sstevel@tonic-gate 				/* Validate the peer addresses. */
41470Sstevel@tonic-gate 				sctp->sctp_active = now;
41480Sstevel@tonic-gate 				sctp_validate_peer(sctp);
41490Sstevel@tonic-gate 
41500Sstevel@tonic-gate 				/* Try sending queued data, or ASCONFs */
41510Sstevel@tonic-gate 				trysend = 1;
41520Sstevel@tonic-gate 				sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL);
41535586Skcpoon 				sctp_adaptation_event(sctp);
41540Sstevel@tonic-gate 				break;
41550Sstevel@tonic-gate 			case CHUNK_ABORT:
41560Sstevel@tonic-gate 				sctp_process_abort(sctp, ch, ECONNREFUSED);
41570Sstevel@tonic-gate 				goto done;
41580Sstevel@tonic-gate 			case CHUNK_COOKIE:
415911042SErik.Nordmark@Sun.COM 				cr = ira->ira_cred;
416011042SErik.Nordmark@Sun.COM 				cpid = ira->ira_cpid;
41618778SErik.Nordmark@Sun.COM 
41620Sstevel@tonic-gate 				if (sctp_process_cookie(sctp, ch, mp, &iack,
416311042SErik.Nordmark@Sun.COM 				    sctph, &recv_adaptation, NULL, ira) == -1) {
416412869SKacheong.Poon@Sun.COM 					SCTPS_BUMP_MIB(sctps,
41650Sstevel@tonic-gate 					    sctpInInvalidCookie);
41660Sstevel@tonic-gate 					break;
41670Sstevel@tonic-gate 				}
41680Sstevel@tonic-gate 				sctp_send_cookie_ack(sctp);
41690Sstevel@tonic-gate 
41700Sstevel@tonic-gate 				if (!SCTP_IS_DETACHED(sctp)) {
41714964Skcpoon 					sctp->sctp_ulp_connected(
41728778SErik.Nordmark@Sun.COM 					    sctp->sctp_ulpd, 0, cr, cpid);
41734964Skcpoon 					sctp_set_ulp_prop(sctp);
41748778SErik.Nordmark@Sun.COM 
41750Sstevel@tonic-gate 				}
41760Sstevel@tonic-gate 				if (sctp->sctp_unacked == 0)
41770Sstevel@tonic-gate 					sctp_stop_faddr_timers(sctp);
417812869SKacheong.Poon@Sun.COM 				SCTP_ASSOC_EST(sctps, sctp);
417912869SKacheong.Poon@Sun.COM 				SCTPS_BUMP_MIB(sctps, sctpActiveEstab);
41800Sstevel@tonic-gate 				if (sctp->sctp_cookie_mp) {
41810Sstevel@tonic-gate 					freemsg(sctp->sctp_cookie_mp);
41820Sstevel@tonic-gate 					sctp->sctp_cookie_mp = NULL;
41830Sstevel@tonic-gate 				}
41840Sstevel@tonic-gate 				/* Validate the peer addresses. */
41850Sstevel@tonic-gate 				sctp->sctp_active = now;
41860Sstevel@tonic-gate 				sctp_validate_peer(sctp);
41870Sstevel@tonic-gate 
41880Sstevel@tonic-gate 				sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL);
41895586Skcpoon 				if (recv_adaptation) {
41905586Skcpoon 					sctp->sctp_recv_adaptation = 1;
41915586Skcpoon 					sctp_adaptation_event(sctp);
41920Sstevel@tonic-gate 				}
41930Sstevel@tonic-gate 				/* Try sending queued data, or ASCONFs */
41940Sstevel@tonic-gate 				trysend = 1;
41950Sstevel@tonic-gate 				break;
41960Sstevel@tonic-gate 			case CHUNK_INIT:
419711042SErik.Nordmark@Sun.COM 				sctp_send_initack(sctp, sctph, ch, mp, ira);
41980Sstevel@tonic-gate 				break;
41990Sstevel@tonic-gate 			case CHUNK_ERROR: {
42000Sstevel@tonic-gate 				sctp_parm_hdr_t *p;
42010Sstevel@tonic-gate 
42020Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
42030Sstevel@tonic-gate 				/* check for a stale cookie */
42040Sstevel@tonic-gate 				if (ntohs(ch->sch_len) >=
42050Sstevel@tonic-gate 				    (sizeof (*p) + sizeof (*ch)) +
42060Sstevel@tonic-gate 				    sizeof (uint32_t)) {
42070Sstevel@tonic-gate 
42080Sstevel@tonic-gate 					p = (sctp_parm_hdr_t *)(ch + 1);
42090Sstevel@tonic-gate 					if (p->sph_type ==
42100Sstevel@tonic-gate 					    htons(SCTP_ERR_STALE_COOKIE)) {
421112869SKacheong.Poon@Sun.COM 						SCTPS_BUMP_MIB(sctps,
42120Sstevel@tonic-gate 						    sctpAborted);
421311953Sanil.udupa@sun.com 						sctp_error_event(sctp,
421411953Sanil.udupa@sun.com 						    ch, B_FALSE);
42153314Skcpoon 						sctp_assoc_event(sctp,
42163314Skcpoon 						    SCTP_COMM_LOST, 0, NULL);
42170Sstevel@tonic-gate 						sctp_clean_death(sctp,
42180Sstevel@tonic-gate 						    ECONNREFUSED);
42190Sstevel@tonic-gate 						goto done;
42200Sstevel@tonic-gate 					}
42210Sstevel@tonic-gate 				}
42220Sstevel@tonic-gate 				break;
42230Sstevel@tonic-gate 			}
42240Sstevel@tonic-gate 			case CHUNK_HEARTBEAT:
42254964Skcpoon 				if (!hb_already) {
42264964Skcpoon 					sctp_return_heartbeat(sctp, ch, mp);
42274964Skcpoon 					hb_already = B_TRUE;
42284964Skcpoon 				}
42290Sstevel@tonic-gate 				break;
42300Sstevel@tonic-gate 			default:
42310Sstevel@tonic-gate 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
42320Sstevel@tonic-gate 					goto nomorechunks;
42330Sstevel@tonic-gate 				} /* else skip and continue processing */
42340Sstevel@tonic-gate 			} /* switch (ch->sch_id) */
42350Sstevel@tonic-gate 			break;
42360Sstevel@tonic-gate 
42370Sstevel@tonic-gate 		case SCTPS_SHUTDOWN_ACK_SENT:
42380Sstevel@tonic-gate 			switch (ch->sch_id) {
42390Sstevel@tonic-gate 			case CHUNK_ABORT:
42400Sstevel@tonic-gate 				/* Pass gathered wisdom to IP for keeping */
424111042SErik.Nordmark@Sun.COM 				sctp_update_dce(sctp);
42420Sstevel@tonic-gate 				sctp_process_abort(sctp, ch, 0);
42430Sstevel@tonic-gate 				goto done;
42440Sstevel@tonic-gate 			case CHUNK_SHUTDOWN_COMPLETE:
42450Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
424612869SKacheong.Poon@Sun.COM 				SCTPS_BUMP_MIB(sctps, sctpShutdowns);
42470Sstevel@tonic-gate 				sctp_assoc_event(sctp, SCTP_SHUTDOWN_COMP, 0,
42480Sstevel@tonic-gate 				    NULL);
42490Sstevel@tonic-gate 
42500Sstevel@tonic-gate 				/* Pass gathered wisdom to IP for keeping */
425111042SErik.Nordmark@Sun.COM 				sctp_update_dce(sctp);
42520Sstevel@tonic-gate 				sctp_clean_death(sctp, 0);
42530Sstevel@tonic-gate 				goto done;
42540Sstevel@tonic-gate 			case CHUNK_SHUTDOWN_ACK:
42550Sstevel@tonic-gate 				sctp_shutdown_complete(sctp);
42560Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
425712869SKacheong.Poon@Sun.COM 				SCTPS_BUMP_MIB(sctps, sctpShutdowns);
42580Sstevel@tonic-gate 				sctp_assoc_event(sctp, SCTP_SHUTDOWN_COMP, 0,
42590Sstevel@tonic-gate 				    NULL);
42600Sstevel@tonic-gate 				sctp_clean_death(sctp, 0);
42610Sstevel@tonic-gate 				goto done;
42620Sstevel@tonic-gate 			case CHUNK_COOKIE:
42630Sstevel@tonic-gate 				(void) sctp_shutdown_received(sctp, NULL,
42641735Skcpoon 				    B_TRUE, B_FALSE, fp);
42650Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
42660Sstevel@tonic-gate 				break;
42670Sstevel@tonic-gate 			case CHUNK_HEARTBEAT:
42684964Skcpoon 				if (!hb_already) {
42694964Skcpoon 					sctp_return_heartbeat(sctp, ch, mp);
42704964Skcpoon 					hb_already = B_TRUE;
42714964Skcpoon 				}
42720Sstevel@tonic-gate 				break;
42730Sstevel@tonic-gate 			default:
42740Sstevel@tonic-gate 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
42750Sstevel@tonic-gate 					goto nomorechunks;
42760Sstevel@tonic-gate 				} /* else skip and continue processing */
42770Sstevel@tonic-gate 				break;
42780Sstevel@tonic-gate 			}
42790Sstevel@tonic-gate 			break;
42800Sstevel@tonic-gate 
42810Sstevel@tonic-gate 		case SCTPS_SHUTDOWN_RECEIVED:
42820Sstevel@tonic-gate 			switch (ch->sch_id) {
42830Sstevel@tonic-gate 			case CHUNK_SHUTDOWN:
42840Sstevel@tonic-gate 				trysend = sctp_shutdown_received(sctp, ch,
42851735Skcpoon 				    B_FALSE, B_FALSE, fp);
428612721Sanil.udupa@sun.com 				/*
428712721Sanil.udupa@sun.com 				 * shutdown_ack_needed may have been set as
428812721Sanil.udupa@sun.com 				 * mentioned in the case CHUNK_SACK below.
428912721Sanil.udupa@sun.com 				 * If sctp_shutdown_received() above found
429012721Sanil.udupa@sun.com 				 * the xmit queue empty the SHUTDOWN ACK chunk
429112721Sanil.udupa@sun.com 				 * has already been sent (or scheduled to be
429212721Sanil.udupa@sun.com 				 * sent on the timer) and the SCTP state
429312721Sanil.udupa@sun.com 				 * changed, so reset shutdown_ack_needed.
429412721Sanil.udupa@sun.com 				 */
429512721Sanil.udupa@sun.com 				if (shutdown_ack_needed && (sctp->sctp_state ==
429612721Sanil.udupa@sun.com 				    SCTPS_SHUTDOWN_ACK_SENT))
429712721Sanil.udupa@sun.com 					shutdown_ack_needed = B_FALSE;
42980Sstevel@tonic-gate 				break;
42990Sstevel@tonic-gate 			case CHUNK_SACK:
43000Sstevel@tonic-gate 				trysend = sctp_got_sack(sctp, ch);
4301852Svi117747 				if (trysend < 0) {
4302852Svi117747 					sctp_send_abort(sctp, sctph->sh_verf,
430311042SErik.Nordmark@Sun.COM 					    0, NULL, 0, mp, 0, B_FALSE, ira);
4304852Svi117747 					sctp_assoc_event(sctp,
4305852Svi117747 					    SCTP_COMM_LOST, 0, NULL);
4306852Svi117747 					sctp_clean_death(sctp,
4307852Svi117747 					    ECONNABORTED);
4308852Svi117747 					goto done;
4309852Svi117747 				}
431012721Sanil.udupa@sun.com 
431112721Sanil.udupa@sun.com 				/*
431212721Sanil.udupa@sun.com 				 * All data acknowledgement after a shutdown
431312721Sanil.udupa@sun.com 				 * should be done with SHUTDOWN chunk.
431412721Sanil.udupa@sun.com 				 * However some peer SCTP do not conform with
431512721Sanil.udupa@sun.com 				 * this and can unexpectedly send a SACK chunk.
431612721Sanil.udupa@sun.com 				 * If all data are acknowledged, set
431712721Sanil.udupa@sun.com 				 * shutdown_ack_needed here indicating that
431812721Sanil.udupa@sun.com 				 * SHUTDOWN ACK needs to be sent later by
431912721Sanil.udupa@sun.com 				 * sctp_send_shutdown_ack().
432012721Sanil.udupa@sun.com 				 */
432112721Sanil.udupa@sun.com 				if ((sctp->sctp_xmit_head == NULL) &&
432212721Sanil.udupa@sun.com 				    (sctp->sctp_xmit_unsent == NULL))
432312721Sanil.udupa@sun.com 					shutdown_ack_needed = B_TRUE;
43240Sstevel@tonic-gate 				break;
43250Sstevel@tonic-gate 			case CHUNK_ABORT:
43260Sstevel@tonic-gate 				sctp_process_abort(sctp, ch, ECONNRESET);
43270Sstevel@tonic-gate 				goto done;
43280Sstevel@tonic-gate 			case CHUNK_HEARTBEAT:
43294964Skcpoon 				if (!hb_already) {
43304964Skcpoon 					sctp_return_heartbeat(sctp, ch, mp);
43314964Skcpoon 					hb_already = B_TRUE;
43324964Skcpoon 				}
43330Sstevel@tonic-gate 				break;
43340Sstevel@tonic-gate 			default:
43350Sstevel@tonic-gate 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
43360Sstevel@tonic-gate 					goto nomorechunks;
43370Sstevel@tonic-gate 				} /* else skip and continue processing */
43380Sstevel@tonic-gate 				break;
43390Sstevel@tonic-gate 			}
43400Sstevel@tonic-gate 			break;
43410Sstevel@tonic-gate 
43420Sstevel@tonic-gate 		default:
43431932Svi117747 			/*
43441932Svi117747 			 * The only remaining states are SCTPS_IDLE and
43451932Svi117747 			 * SCTPS_BOUND, and we should not be getting here
43461932Svi117747 			 * for these.
43471932Svi117747 			 */
43481932Svi117747 			ASSERT(0);
43490Sstevel@tonic-gate 		} /* switch (sctp->sctp_state) */
43500Sstevel@tonic-gate 
43510Sstevel@tonic-gate 		ch = sctp_next_chunk(ch, &mlen);
43520Sstevel@tonic-gate 		if (ch != NULL && !sctp_check_input(sctp, ch, mlen, 0))
43530Sstevel@tonic-gate 			goto done;
43540Sstevel@tonic-gate 	} while (ch != NULL);
43550Sstevel@tonic-gate 
43560Sstevel@tonic-gate 	/* Finished processing all chunks in packet */
43570Sstevel@tonic-gate 
43580Sstevel@tonic-gate nomorechunks:
435912721Sanil.udupa@sun.com 
436012721Sanil.udupa@sun.com 	if (shutdown_ack_needed)
436112721Sanil.udupa@sun.com 		sctp_send_shutdown_ack(sctp, fp, B_FALSE);
436212721Sanil.udupa@sun.com 
43630Sstevel@tonic-gate 	/* SACK if necessary */
43640Sstevel@tonic-gate 	if (gotdata) {
43654964Skcpoon 		boolean_t sack_sent;
43664964Skcpoon 
43670Sstevel@tonic-gate 		(sctp->sctp_sack_toggle)++;
43684964Skcpoon 		sack_sent = sctp_sack(sctp, dups);
43690Sstevel@tonic-gate 		dups = NULL;
43700Sstevel@tonic-gate 
43714964Skcpoon 		/* If a SACK is sent, no need to restart the timer. */
43724964Skcpoon 		if (!sack_sent && !sctp->sctp_ack_timer_running) {
43730Sstevel@tonic-gate 			sctp->sctp_ack_timer_running = B_TRUE;
43740Sstevel@tonic-gate 			sctp_timer(sctp, sctp->sctp_ack_mp,
43753448Sdh155122 			    MSEC_TO_TICK(sctps->sctps_deferred_ack_interval));
43760Sstevel@tonic-gate 		}
43770Sstevel@tonic-gate 	}
43780Sstevel@tonic-gate 
43790Sstevel@tonic-gate 	if (trysend) {
43803795Skcpoon 		sctp_output(sctp, UINT_MAX);
43810Sstevel@tonic-gate 		if (sctp->sctp_cxmit_list != NULL)
43820Sstevel@tonic-gate 			sctp_wput_asconf(sctp, NULL);
43830Sstevel@tonic-gate 	}
438411373SGeorge.Shepherd@Sun.COM 	/*
438511373SGeorge.Shepherd@Sun.COM 	 * If there is unsent data, make sure a timer is running, check
438611373SGeorge.Shepherd@Sun.COM 	 * timer_mp, if sctp_closei_local() ran the timers may be free.
438711373SGeorge.Shepherd@Sun.COM 	 */
438813009SChandrasekar.Marimuthu@Sun.COM 	if (sctp->sctp_unsent > 0 && !sctp->sctp_current->sf_timer_running &&
438913009SChandrasekar.Marimuthu@Sun.COM 	    sctp->sctp_current->sf_timer_mp != NULL) {
43900Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
439113009SChandrasekar.Marimuthu@Sun.COM 		    sctp->sctp_current->sf_rto);
43920Sstevel@tonic-gate 	}
43930Sstevel@tonic-gate 
43940Sstevel@tonic-gate done:
43950Sstevel@tonic-gate 	if (dups != NULL)
43960Sstevel@tonic-gate 		freeb(dups);
43970Sstevel@tonic-gate 	freemsg(mp);
43980Sstevel@tonic-gate 
43994964Skcpoon 	if (sctp->sctp_err_chunks != NULL)
44004964Skcpoon 		sctp_process_err(sctp);
44014964Skcpoon 
44020Sstevel@tonic-gate 	if (wake_eager) {
44030Sstevel@tonic-gate 		/*
44040Sstevel@tonic-gate 		 * sctp points to newly created control block, need to
440511042SErik.Nordmark@Sun.COM 		 * release it before exiting.
44060Sstevel@tonic-gate 		 */
44070Sstevel@tonic-gate 		WAKE_SCTP(sctp);
44080Sstevel@tonic-gate 	}
44090Sstevel@tonic-gate }
44100Sstevel@tonic-gate 
44110Sstevel@tonic-gate /*
4412*13054SKacheong.Poon@Sun.COM  * Some amount of data got removed from ULP's receive queue and we can
4413*13054SKacheong.Poon@Sun.COM  * push messages up if we are flow controlled before.  Reset the receive
4414*13054SKacheong.Poon@Sun.COM  * window to full capacity (conn_rcvbuf) and check if we should send a
4415*13054SKacheong.Poon@Sun.COM  * window update.
44160Sstevel@tonic-gate  */
44170Sstevel@tonic-gate void
sctp_recvd(sctp_t * sctp,int len)44180Sstevel@tonic-gate sctp_recvd(sctp_t *sctp, int len)
44190Sstevel@tonic-gate {
44203448Sdh155122 	sctp_stack_t	*sctps = sctp->sctp_sctps;
4421*13054SKacheong.Poon@Sun.COM 	conn_t		*connp = sctp->sctp_connp;
4422*13054SKacheong.Poon@Sun.COM 	boolean_t	send_sack = B_FALSE;
44230Sstevel@tonic-gate 
44240Sstevel@tonic-gate 	ASSERT(sctp != NULL);
44250Sstevel@tonic-gate 	RUN_SCTP(sctp);
44260Sstevel@tonic-gate 
4427*13054SKacheong.Poon@Sun.COM 	sctp->sctp_flowctrld = B_FALSE;
4428*13054SKacheong.Poon@Sun.COM 	/* This is the amount of data queued in ULP. */
4429*13054SKacheong.Poon@Sun.COM 	sctp->sctp_ulp_rxqueued = connp->conn_rcvbuf - len;
4430*13054SKacheong.Poon@Sun.COM 
4431*13054SKacheong.Poon@Sun.COM 	if (connp->conn_rcvbuf - sctp->sctp_arwnd >= sctp->sctp_mss)
4432*13054SKacheong.Poon@Sun.COM 		send_sack = B_TRUE;
4433*13054SKacheong.Poon@Sun.COM 	sctp->sctp_rwnd = connp->conn_rcvbuf;
4434*13054SKacheong.Poon@Sun.COM 
4435*13054SKacheong.Poon@Sun.COM 	if (sctp->sctp_state >= SCTPS_ESTABLISHED && send_sack) {
44360Sstevel@tonic-gate 		sctp->sctp_force_sack = 1;
443712869SKacheong.Poon@Sun.COM 		SCTPS_BUMP_MIB(sctps, sctpOutWinUpdate);
44384964Skcpoon 		(void) sctp_sack(sctp, NULL);
44390Sstevel@tonic-gate 	}
44400Sstevel@tonic-gate 	WAKE_SCTP(sctp);
44410Sstevel@tonic-gate }
4442