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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/stream.h>
320Sstevel@tonic-gate #include <sys/cmn_err.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
350Sstevel@tonic-gate #include <sys/tihdr.h>
360Sstevel@tonic-gate #include <sys/socket.h>
370Sstevel@tonic-gate #include <sys/strsun.h>
380Sstevel@tonic-gate #include <sys/strsubr.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include <netinet/in.h>
410Sstevel@tonic-gate #include <netinet/ip6.h>
420Sstevel@tonic-gate #include <netinet/tcp_seq.h>
430Sstevel@tonic-gate #include <netinet/sctp.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <inet/common.h>
460Sstevel@tonic-gate #include <inet/ip.h>
470Sstevel@tonic-gate #include <inet/ip6.h>
480Sstevel@tonic-gate #include <inet/mib2.h>
490Sstevel@tonic-gate #include <inet/ipclassifier.h>
500Sstevel@tonic-gate #include <inet/ipp_common.h>
510Sstevel@tonic-gate #include <inet/ipsec_impl.h>
520Sstevel@tonic-gate #include <inet/sctp_ip.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate #include "sctp_impl.h"
550Sstevel@tonic-gate #include "sctp_asconf.h"
560Sstevel@tonic-gate #include "sctp_addr.h"
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static struct kmem_cache *sctp_kmem_set_cache;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * PR-SCTP comments.
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * When we get a valid Forward TSN chunk, we check the fragment list for this
640Sstevel@tonic-gate  * SSN and preceeding SSNs free all them. Further, if this Forward TSN causes
650Sstevel@tonic-gate  * the next expected SSN to be present in the stream queue, we deliver any
660Sstevel@tonic-gate  * such stranded messages upstream. We also update the SACK info. appropriately.
670Sstevel@tonic-gate  * When checking for advancing the cumulative ack (in sctp_cumack()) we must
680Sstevel@tonic-gate  * check for abandoned chunks and messages. While traversing the tramsmit
690Sstevel@tonic-gate  * list if we come across an abandoned chunk, we can skip the message (i.e.
700Sstevel@tonic-gate  * take it out of the (re)transmit list) since this message, and hence this
710Sstevel@tonic-gate  * chunk, has been marked abandoned by sctp_rexmit(). If we come across an
720Sstevel@tonic-gate  * unsent chunk for a message this now abandoned we need to check if a
730Sstevel@tonic-gate  * Forward TSN needs to be sent, this could be a case where we deferred sending
740Sstevel@tonic-gate  * a Forward TSN in sctp_get_msg_to_send(). Further, after processing a
750Sstevel@tonic-gate  * SACK we check if the Advanced peer ack point can be moved ahead, i.e.
760Sstevel@tonic-gate  * if we can send a Forward TSN via sctp_check_abandoned_data().
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate void
790Sstevel@tonic-gate sctp_free_set(sctp_set_t *s)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	sctp_set_t *p;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	while (s) {
840Sstevel@tonic-gate 		p = s->next;
850Sstevel@tonic-gate 		kmem_cache_free(sctp_kmem_set_cache, s);
860Sstevel@tonic-gate 		s = p;
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate static void
910Sstevel@tonic-gate sctp_ack_add(sctp_set_t **head, uint32_t tsn, int *num)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	sctp_set_t *p, *t;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if (head == NULL || num == NULL)
960Sstevel@tonic-gate 		return;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	ASSERT(*num >= 0);
990Sstevel@tonic-gate 	ASSERT((*num == 0 && *head == NULL) || (*num > 0 && *head != NULL));
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (*head == NULL) {
1020Sstevel@tonic-gate 		*head = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP);
1030Sstevel@tonic-gate 		if (*head == NULL)
1040Sstevel@tonic-gate 			return;
1050Sstevel@tonic-gate 		(*head)->prev = (*head)->next = NULL;
1060Sstevel@tonic-gate 		(*head)->begin = tsn;
1070Sstevel@tonic-gate 		(*head)->end = tsn;
1080Sstevel@tonic-gate 		*num = 1;
1090Sstevel@tonic-gate 		return;
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	ASSERT((*head)->prev == NULL);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	/*
1150Sstevel@tonic-gate 	 * Handle this special case here so we don't have to check
1160Sstevel@tonic-gate 	 * for it each time in the loop.
1170Sstevel@tonic-gate 	 */
1180Sstevel@tonic-gate 	if (SEQ_LT(tsn + 1, (*head)->begin)) {
1190Sstevel@tonic-gate 		/* add a new set, and move the head pointer */
1200Sstevel@tonic-gate 		t = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP);
1210Sstevel@tonic-gate 		if (t == NULL)
1220Sstevel@tonic-gate 			return;
1230Sstevel@tonic-gate 		t->next = *head;
1240Sstevel@tonic-gate 		t->prev = NULL;
1250Sstevel@tonic-gate 		(*head)->prev = t;
1260Sstevel@tonic-gate 		t->begin = tsn;
1270Sstevel@tonic-gate 		t->end = tsn;
1280Sstevel@tonic-gate 		(*num)++;
1290Sstevel@tonic-gate 		*head = t;
1300Sstevel@tonic-gate 		return;
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	/*
1340Sstevel@tonic-gate 	 * We need to handle the following cases, where p points to
1350Sstevel@tonic-gate 	 * the current set (as we walk through the loop):
1360Sstevel@tonic-gate 	 *
1370Sstevel@tonic-gate 	 * 1. tsn is entirely less than p; create a new set before p.
1380Sstevel@tonic-gate 	 * 2. tsn borders p from less; coalesce p with tsn.
1390Sstevel@tonic-gate 	 * 3. tsn is withing p; do nothing.
1400Sstevel@tonic-gate 	 * 4. tsn borders p from greater; coalesce p with tsn.
1410Sstevel@tonic-gate 	 * 4a. p may now border p->next from less; if so, coalesce those
1420Sstevel@tonic-gate 	 *    two sets.
1430Sstevel@tonic-gate 	 * 5. tsn is entirely greater then all sets; add a new set at
1440Sstevel@tonic-gate 	 *    the end.
1450Sstevel@tonic-gate 	 */
1460Sstevel@tonic-gate 	for (p = *head; ; p = p->next) {
1470Sstevel@tonic-gate 		if (SEQ_LT(tsn + 1, p->begin)) {
1480Sstevel@tonic-gate 			/* 1: add a new set before p. */
1490Sstevel@tonic-gate 			t = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP);
1500Sstevel@tonic-gate 			if (t == NULL)
1510Sstevel@tonic-gate 				return;
1520Sstevel@tonic-gate 			t->next = p;
1530Sstevel@tonic-gate 			t->prev = NULL;
1540Sstevel@tonic-gate 			t->begin = tsn;
1550Sstevel@tonic-gate 			t->end = tsn;
1560Sstevel@tonic-gate 			if (p->prev) {
1570Sstevel@tonic-gate 				t->prev = p->prev;
1580Sstevel@tonic-gate 				p->prev->next = t;
1590Sstevel@tonic-gate 			}
1600Sstevel@tonic-gate 			p->prev = t;
1610Sstevel@tonic-gate 			(*num)++;
1620Sstevel@tonic-gate 			return;
1630Sstevel@tonic-gate 		}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 		if ((tsn + 1) == p->begin) {
1660Sstevel@tonic-gate 			/* 2: adjust p->begin */
1670Sstevel@tonic-gate 			p->begin = tsn;
1680Sstevel@tonic-gate 			return;
1690Sstevel@tonic-gate 		}
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 		if (SEQ_GEQ(tsn, p->begin) && SEQ_LEQ(tsn, p->end)) {
1720Sstevel@tonic-gate 			/* 3; do nothing */
1730Sstevel@tonic-gate 			return;
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 		if ((p->end + 1) == tsn) {
1770Sstevel@tonic-gate 			/* 4; adjust p->end */
1780Sstevel@tonic-gate 			p->end = tsn;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 			if (p->next != NULL && (tsn + 1) == p->next->begin) {
1810Sstevel@tonic-gate 				/* 4a: coalesce p and p->next */
1820Sstevel@tonic-gate 				t = p->next;
1830Sstevel@tonic-gate 				p->end = t->end;
1840Sstevel@tonic-gate 				p->next = t->next;
1850Sstevel@tonic-gate 				if (t->next != NULL)
1860Sstevel@tonic-gate 					t->next->prev = p;
1870Sstevel@tonic-gate 				kmem_cache_free(sctp_kmem_set_cache, t);
1880Sstevel@tonic-gate 				(*num)--;
1890Sstevel@tonic-gate 			}
1900Sstevel@tonic-gate 			return;
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 		if (p->next == NULL) {
1940Sstevel@tonic-gate 			/* 5: add new set at the end */
1950Sstevel@tonic-gate 			t = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP);
1960Sstevel@tonic-gate 			if (t == NULL)
1970Sstevel@tonic-gate 				return;
1980Sstevel@tonic-gate 			t->next = NULL;
1990Sstevel@tonic-gate 			t->prev = p;
2000Sstevel@tonic-gate 			t->begin = tsn;
2010Sstevel@tonic-gate 			t->end = tsn;
2020Sstevel@tonic-gate 			p->next = t;
2030Sstevel@tonic-gate 			(*num)++;
2040Sstevel@tonic-gate 			return;
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 		if (SEQ_GT(tsn, p->end + 1))
2080Sstevel@tonic-gate 			continue;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate static void
2130Sstevel@tonic-gate sctp_ack_rem(sctp_set_t **head, uint32_t end, int *num)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	sctp_set_t *p, *t;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	if (head == NULL || *head == NULL || num == NULL)
2180Sstevel@tonic-gate 		return;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	/* Nothing to remove */
2210Sstevel@tonic-gate 	if (SEQ_LT(end, (*head)->begin))
2220Sstevel@tonic-gate 		return;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	/* Find out where to start removing sets */
2250Sstevel@tonic-gate 	for (p = *head; p->next; p = p->next) {
2260Sstevel@tonic-gate 		if (SEQ_LEQ(end, p->end))
2270Sstevel@tonic-gate 			break;
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	if (SEQ_LT(end, p->end) && SEQ_GEQ(end, p->begin)) {
2310Sstevel@tonic-gate 		/* adjust p */
2320Sstevel@tonic-gate 		p->begin = end + 1;
2330Sstevel@tonic-gate 		/* all done */
2340Sstevel@tonic-gate 		if (p == *head)
2350Sstevel@tonic-gate 			return;
2360Sstevel@tonic-gate 	} else if (SEQ_GEQ(end, p->end)) {
2370Sstevel@tonic-gate 		/* remove this set too */
2380Sstevel@tonic-gate 		p = p->next;
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/* unlink everything before this set */
2420Sstevel@tonic-gate 	t = *head;
2430Sstevel@tonic-gate 	*head = p;
2440Sstevel@tonic-gate 	if (p != NULL && p->prev != NULL) {
2450Sstevel@tonic-gate 		p->prev->next = NULL;
2460Sstevel@tonic-gate 		p->prev = NULL;
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	sctp_free_set(t);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	/* recount the number of sets */
2520Sstevel@tonic-gate 	*num = 0;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	for (p = *head; p != NULL; p = p->next)
2550Sstevel@tonic-gate 		(*num)++;
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate void
2590Sstevel@tonic-gate sctp_sets_init()
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	sctp_kmem_set_cache = kmem_cache_create("sctp_set_cache",
2620Sstevel@tonic-gate 	    sizeof (sctp_set_t), 0, NULL, NULL, NULL, NULL,
2630Sstevel@tonic-gate 	    NULL, 0);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate void
2670Sstevel@tonic-gate sctp_sets_fini()
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate 	kmem_cache_destroy(sctp_kmem_set_cache);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate sctp_chunk_hdr_t *
2730Sstevel@tonic-gate sctp_first_chunk(uchar_t *rptr, ssize_t remaining)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	sctp_chunk_hdr_t *ch;
2760Sstevel@tonic-gate 	uint16_t ch_len;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if (remaining < sizeof (*ch)) {
2790Sstevel@tonic-gate 		return (NULL);
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	ch = (sctp_chunk_hdr_t *)rptr;
2830Sstevel@tonic-gate 	ch_len = ntohs(ch->sch_len);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	if (ch_len < sizeof (*ch) || remaining < ch_len) {
2860Sstevel@tonic-gate 		return (NULL);
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	return (ch);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate sctp_chunk_hdr_t *
2930Sstevel@tonic-gate sctp_next_chunk(sctp_chunk_hdr_t *ch, ssize_t *remaining)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	int pad;
2960Sstevel@tonic-gate 	uint16_t ch_len;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	if (!ch) {
2990Sstevel@tonic-gate 		return (NULL);
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	ch_len = ntohs(ch->sch_len);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	if ((pad = ch_len & (SCTP_ALIGN - 1)) != 0) {
3050Sstevel@tonic-gate 		pad = SCTP_ALIGN - pad;
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	*remaining -= (ch_len + pad);
3090Sstevel@tonic-gate 	ch = (sctp_chunk_hdr_t *)((char *)ch + ch_len + pad);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	return (sctp_first_chunk((uchar_t *)ch, *remaining));
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate  * Attach ancillary data to a received SCTP segments.
3160Sstevel@tonic-gate  * If the source address (fp) is not the primary, send up a
3170Sstevel@tonic-gate  * unitdata_ind so recvfrom() can populate the msg_name field.
3180Sstevel@tonic-gate  * If ancillary data is also requested, we append it to the
3190Sstevel@tonic-gate  * unitdata_req. Otherwise, we just send up an optdata_ind.
3200Sstevel@tonic-gate  */
3210Sstevel@tonic-gate static int
3220Sstevel@tonic-gate sctp_input_add_ancillary(sctp_t *sctp, mblk_t **mp, sctp_data_hdr_t *dcp,
3230Sstevel@tonic-gate     sctp_faddr_t *fp, ip6_pkt_t *ipp)
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate 	struct T_unitdata_ind	*tudi;
3260Sstevel@tonic-gate 	int			optlen;
3270Sstevel@tonic-gate 	int			hdrlen;
3280Sstevel@tonic-gate 	uchar_t			*optptr;
3290Sstevel@tonic-gate 	struct cmsghdr		*cmsg;
3300Sstevel@tonic-gate 	mblk_t			*mp1;
3310Sstevel@tonic-gate 	struct sockaddr_in6	sin_buf[1];
3320Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
3330Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
3340Sstevel@tonic-gate 	uint_t			addflag = 0;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	sin4 = NULL;
3370Sstevel@tonic-gate 	sin6 = NULL;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	optlen = hdrlen = 0;
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/* Figure out address size */
3420Sstevel@tonic-gate 	if (sctp->sctp_ipversion == IPV4_VERSION) {
3430Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)sin_buf;
3440Sstevel@tonic-gate 		sin4->sin_family = AF_INET;
3450Sstevel@tonic-gate 		sin4->sin_port = sctp->sctp_fport;
3460Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&fp->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;
3510Sstevel@tonic-gate 		sin6->sin6_port = sctp->sctp_fport;
3520Sstevel@tonic-gate 		sin6->sin6_addr = fp->faddr;
3530Sstevel@tonic-gate 		hdrlen = sizeof (*tudi) + sizeof (*sin6);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/* If app asked to receive send / recv info */
3570Sstevel@tonic-gate 	if (sctp->sctp_recvsndrcvinfo) {
3580Sstevel@tonic-gate 		optlen += sizeof (*cmsg) + sizeof (struct sctp_sndrcvinfo);
3590Sstevel@tonic-gate 		if (hdrlen == 0)
3600Sstevel@tonic-gate 			hdrlen = sizeof (struct T_optdata_ind);
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	if (sctp->sctp_ipv6_recvancillary == 0)
3640Sstevel@tonic-gate 		goto noancillary;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	if ((ipp->ipp_fields & IPPF_IFINDEX) &&
3670Sstevel@tonic-gate 	    ipp->ipp_ifindex != sctp->sctp_recvifindex &&
3680Sstevel@tonic-gate 	    (sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVPKTINFO)) {
3690Sstevel@tonic-gate 		optlen += sizeof (*cmsg) + sizeof (struct in6_pktinfo);
3700Sstevel@tonic-gate 		if (hdrlen == 0)
3710Sstevel@tonic-gate 			hdrlen = sizeof (struct T_unitdata_ind);
3720Sstevel@tonic-gate 		addflag |= SCTP_IPV6_RECVPKTINFO;
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 	/* If app asked for hoplimit and it has changed ... */
3750Sstevel@tonic-gate 	if ((ipp->ipp_fields & IPPF_HOPLIMIT) &&
3760Sstevel@tonic-gate 	    ipp->ipp_hoplimit != sctp->sctp_recvhops &&
3770Sstevel@tonic-gate 	    (sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVHOPLIMIT)) {
3780Sstevel@tonic-gate 		optlen += sizeof (*cmsg) + sizeof (uint_t);
3790Sstevel@tonic-gate 		if (hdrlen == 0)
3800Sstevel@tonic-gate 			hdrlen = sizeof (struct T_unitdata_ind);
3810Sstevel@tonic-gate 		addflag |= SCTP_IPV6_RECVHOPLIMIT;
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 	/* If app asked for hopbyhop headers and it has changed ... */
3840Sstevel@tonic-gate 	if ((sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVHOPOPTS) &&
3850Sstevel@tonic-gate 	    sctp_cmpbuf(sctp->sctp_hopopts, sctp->sctp_hopoptslen,
3860Sstevel@tonic-gate 		(ipp->ipp_fields & IPPF_HOPOPTS),
3870Sstevel@tonic-gate 		ipp->ipp_hopopts, ipp->ipp_hopoptslen)) {
3880Sstevel@tonic-gate 		optlen += sizeof (*cmsg) + ipp->ipp_hopoptslen;
3890Sstevel@tonic-gate 		if (hdrlen == 0)
3900Sstevel@tonic-gate 			hdrlen = sizeof (struct T_unitdata_ind);
3910Sstevel@tonic-gate 		addflag |= SCTP_IPV6_RECVHOPOPTS;
3920Sstevel@tonic-gate 		if (!sctp_allocbuf((void **)&sctp->sctp_hopopts,
3930Sstevel@tonic-gate 		    &sctp->sctp_hopoptslen,
3940Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_HOPOPTS),
3950Sstevel@tonic-gate 		    ipp->ipp_hopopts, ipp->ipp_hopoptslen))
3960Sstevel@tonic-gate 			return (-1);
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 	/* If app asked for dst headers before routing headers ... */
3990Sstevel@tonic-gate 	if ((sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVRTDSTOPTS) &&
4000Sstevel@tonic-gate 	    sctp_cmpbuf(sctp->sctp_rtdstopts, sctp->sctp_rtdstoptslen,
4010Sstevel@tonic-gate 		(ipp->ipp_fields & IPPF_RTDSTOPTS),
4020Sstevel@tonic-gate 		ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) {
4030Sstevel@tonic-gate 		optlen += sizeof (*cmsg) + ipp->ipp_rtdstoptslen;
4040Sstevel@tonic-gate 		if (hdrlen == 0)
4050Sstevel@tonic-gate 			hdrlen = sizeof (struct T_unitdata_ind);
4060Sstevel@tonic-gate 		addflag |= SCTP_IPV6_RECVRTDSTOPTS;
4070Sstevel@tonic-gate 		if (!sctp_allocbuf((void **)&sctp->sctp_rtdstopts,
4080Sstevel@tonic-gate 		    &sctp->sctp_rtdstoptslen,
4090Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_RTDSTOPTS),
4100Sstevel@tonic-gate 		    ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen))
4110Sstevel@tonic-gate 			return (-1);
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 	/* If app asked for routing headers and it has changed ... */
4140Sstevel@tonic-gate 	if (sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVRTHDR) {
4150Sstevel@tonic-gate 		if (sctp_cmpbuf(sctp->sctp_rthdr, sctp->sctp_rthdrlen,
4160Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_RTHDR),
4170Sstevel@tonic-gate 		    ipp->ipp_rthdr, ipp->ipp_rthdrlen)) {
4180Sstevel@tonic-gate 			optlen += sizeof (*cmsg) + ipp->ipp_rthdrlen;
4190Sstevel@tonic-gate 			if (hdrlen == 0)
4200Sstevel@tonic-gate 				hdrlen = sizeof (struct T_unitdata_ind);
4210Sstevel@tonic-gate 			addflag |= SCTP_IPV6_RECVRTHDR;
4220Sstevel@tonic-gate 			if (!sctp_allocbuf((void **)&sctp->sctp_rthdr,
4230Sstevel@tonic-gate 			    &sctp->sctp_rthdrlen,
4240Sstevel@tonic-gate 			    (ipp->ipp_fields & IPPF_RTHDR),
4250Sstevel@tonic-gate 			    ipp->ipp_rthdr, ipp->ipp_rthdrlen))
4260Sstevel@tonic-gate 				return (-1);
4270Sstevel@tonic-gate 		}
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 	/* If app asked for dest headers and it has changed ... */
4300Sstevel@tonic-gate 	if ((sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVDSTOPTS) &&
4310Sstevel@tonic-gate 	    sctp_cmpbuf(sctp->sctp_dstopts, sctp->sctp_dstoptslen,
4320Sstevel@tonic-gate 		(ipp->ipp_fields & IPPF_DSTOPTS),
4330Sstevel@tonic-gate 		ipp->ipp_dstopts, ipp->ipp_dstoptslen)) {
4340Sstevel@tonic-gate 		optlen += sizeof (*cmsg) + ipp->ipp_dstoptslen;
4350Sstevel@tonic-gate 		if (hdrlen == 0)
4360Sstevel@tonic-gate 			hdrlen = sizeof (struct T_unitdata_ind);
4370Sstevel@tonic-gate 		addflag |= SCTP_IPV6_RECVDSTOPTS;
4380Sstevel@tonic-gate 		if (!sctp_allocbuf((void **)&sctp->sctp_dstopts,
4390Sstevel@tonic-gate 		    &sctp->sctp_dstoptslen,
4400Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_DSTOPTS),
4410Sstevel@tonic-gate 		    ipp->ipp_dstopts, ipp->ipp_dstoptslen))
4420Sstevel@tonic-gate 			return (-1);
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate noancillary:
4450Sstevel@tonic-gate 	/* Nothing to add */
4460Sstevel@tonic-gate 	if (hdrlen == 0)
4470Sstevel@tonic-gate 		return (-1);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	mp1 = allocb(hdrlen + optlen + sizeof (void *), BPRI_MED);
4500Sstevel@tonic-gate 	if (mp1 == NULL)
4510Sstevel@tonic-gate 		return (-1);
4520Sstevel@tonic-gate 	mp1->b_cont = *mp;
4530Sstevel@tonic-gate 	*mp = mp1;
4540Sstevel@tonic-gate 	mp1->b_rptr += sizeof (void *);  /* pointer worth of padding */
4550Sstevel@tonic-gate 	mp1->b_wptr = mp1->b_rptr + hdrlen + optlen;
4560Sstevel@tonic-gate 	DB_TYPE(mp1) = M_PROTO;
4570Sstevel@tonic-gate 	tudi = (struct T_unitdata_ind *)mp1->b_rptr;
4580Sstevel@tonic-gate 	tudi->PRIM_type = T_UNITDATA_IND;
4590Sstevel@tonic-gate 	tudi->SRC_length = sin4 ? sizeof (*sin4) : sizeof (*sin6);
4600Sstevel@tonic-gate 	tudi->SRC_offset = sizeof (*tudi);
4610Sstevel@tonic-gate 	tudi->OPT_offset = sizeof (*tudi) + tudi->SRC_length;
4620Sstevel@tonic-gate 	tudi->OPT_length = optlen;
4630Sstevel@tonic-gate 	if (sin4) {
4640Sstevel@tonic-gate 		bcopy(sin4, tudi + 1, sizeof (*sin4));
4650Sstevel@tonic-gate 	} else {
4660Sstevel@tonic-gate 		bcopy(sin6, tudi + 1, sizeof (*sin6));
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 	optptr = (uchar_t *)tudi + tudi->OPT_offset;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (sctp->sctp_recvsndrcvinfo) {
4710Sstevel@tonic-gate 		/* XXX need backout method if memory allocation fails. */
4720Sstevel@tonic-gate 		struct sctp_sndrcvinfo *sri;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
4750Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_SCTP;
4760Sstevel@tonic-gate 		cmsg->cmsg_type = SCTP_SNDRCV;
4770Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + sizeof (*sri);
4780Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		sri = (struct sctp_sndrcvinfo *)(cmsg + 1);
4810Sstevel@tonic-gate 		ASSERT(OK_32PTR(sri));
4820Sstevel@tonic-gate 		sri->sinfo_stream = ntohs(dcp->sdh_sid);
4830Sstevel@tonic-gate 		sri->sinfo_ssn = ntohs(dcp->sdh_ssn);
4840Sstevel@tonic-gate 		if (SCTP_DATA_GET_UBIT(dcp)) {
4850Sstevel@tonic-gate 			sri->sinfo_flags = MSG_UNORDERED;
4860Sstevel@tonic-gate 		} else {
4870Sstevel@tonic-gate 			sri->sinfo_flags = 0;
4880Sstevel@tonic-gate 		}
4890Sstevel@tonic-gate 		sri->sinfo_ppid = dcp->sdh_payload_id;
4900Sstevel@tonic-gate 		sri->sinfo_context = 0;
4910Sstevel@tonic-gate 		sri->sinfo_timetolive = 0;
4920Sstevel@tonic-gate 		sri->sinfo_tsn = ntohl(dcp->sdh_tsn);
4930Sstevel@tonic-gate 		sri->sinfo_cumtsn = sctp->sctp_ftsn;
4940Sstevel@tonic-gate 		sri->sinfo_assoc_id = 0;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		optptr += sizeof (*sri);
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	/*
5000Sstevel@tonic-gate 	 * If app asked for pktinfo and the index has changed ...
5010Sstevel@tonic-gate 	 * Note that the local address never changes for the connection.
5020Sstevel@tonic-gate 	 */
5030Sstevel@tonic-gate 	if (addflag & SCTP_IPV6_RECVPKTINFO) {
5040Sstevel@tonic-gate 		struct in6_pktinfo *pkti;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
5070Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_IPV6;
5080Sstevel@tonic-gate 		cmsg->cmsg_type = IPV6_PKTINFO;
5090Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + sizeof (*pkti);
5100Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 		pkti = (struct in6_pktinfo *)optptr;
5130Sstevel@tonic-gate 		if (sctp->sctp_ipversion == IPV6_VERSION)
5140Sstevel@tonic-gate 			pkti->ipi6_addr = sctp->sctp_ip6h->ip6_src;
5150Sstevel@tonic-gate 		else
5160Sstevel@tonic-gate 			IN6_IPADDR_TO_V4MAPPED(sctp->sctp_ipha->ipha_src,
5170Sstevel@tonic-gate 			    &pkti->ipi6_addr);
5180Sstevel@tonic-gate 		pkti->ipi6_ifindex = ipp->ipp_ifindex;
5190Sstevel@tonic-gate 		optptr += sizeof (*pkti);
5200Sstevel@tonic-gate 		ASSERT(OK_32PTR(optptr));
5210Sstevel@tonic-gate 		/* Save as "last" value */
5220Sstevel@tonic-gate 		sctp->sctp_recvifindex = ipp->ipp_ifindex;
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 	/* If app asked for hoplimit and it has changed ... */
5250Sstevel@tonic-gate 	if (addflag & SCTP_IPV6_RECVHOPLIMIT) {
5260Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
5270Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_IPV6;
5280Sstevel@tonic-gate 		cmsg->cmsg_type = IPV6_HOPLIMIT;
5290Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + sizeof (uint_t);
5300Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 		*(uint_t *)optptr = ipp->ipp_hoplimit;
5330Sstevel@tonic-gate 		optptr += sizeof (uint_t);
5340Sstevel@tonic-gate 		ASSERT(OK_32PTR(optptr));
5350Sstevel@tonic-gate 		/* Save as "last" value */
5360Sstevel@tonic-gate 		sctp->sctp_recvhops = ipp->ipp_hoplimit;
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 	if (addflag & SCTP_IPV6_RECVHOPOPTS) {
5390Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
5400Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_IPV6;
5410Sstevel@tonic-gate 		cmsg->cmsg_type = IPV6_HOPOPTS;
5420Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_hopoptslen;
5430Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 		bcopy(ipp->ipp_hopopts, optptr, ipp->ipp_hopoptslen);
5460Sstevel@tonic-gate 		optptr += ipp->ipp_hopoptslen;
5470Sstevel@tonic-gate 		ASSERT(OK_32PTR(optptr));
5480Sstevel@tonic-gate 		/* Save as last value */
5490Sstevel@tonic-gate 		sctp_savebuf((void **)&sctp->sctp_hopopts,
5500Sstevel@tonic-gate 		    &sctp->sctp_hopoptslen,
5510Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_HOPOPTS),
5520Sstevel@tonic-gate 		    ipp->ipp_hopopts, ipp->ipp_hopoptslen);
5530Sstevel@tonic-gate 	}
5540Sstevel@tonic-gate 	if (addflag & SCTP_IPV6_RECVRTDSTOPTS) {
5550Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
5560Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_IPV6;
5570Sstevel@tonic-gate 		cmsg->cmsg_type = IPV6_RTHDRDSTOPTS;
5580Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_rtdstoptslen;
5590Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen);
5620Sstevel@tonic-gate 		optptr += ipp->ipp_rtdstoptslen;
5630Sstevel@tonic-gate 		ASSERT(OK_32PTR(optptr));
5640Sstevel@tonic-gate 		/* Save as last value */
5650Sstevel@tonic-gate 		sctp_savebuf((void **)&sctp->sctp_rtdstopts,
5660Sstevel@tonic-gate 		    &sctp->sctp_rtdstoptslen,
5670Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_RTDSTOPTS),
5680Sstevel@tonic-gate 		    ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen);
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 	if (addflag & SCTP_IPV6_RECVRTHDR) {
5710Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
5720Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_IPV6;
5730Sstevel@tonic-gate 		cmsg->cmsg_type = IPV6_RTHDR;
5740Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_rthdrlen;
5750Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 		bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen);
5780Sstevel@tonic-gate 		optptr += ipp->ipp_rthdrlen;
5790Sstevel@tonic-gate 		ASSERT(OK_32PTR(optptr));
5800Sstevel@tonic-gate 		/* Save as last value */
5810Sstevel@tonic-gate 		sctp_savebuf((void **)&sctp->sctp_rthdr,
5820Sstevel@tonic-gate 		    &sctp->sctp_rthdrlen,
5830Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_RTHDR),
5840Sstevel@tonic-gate 		    ipp->ipp_rthdr, ipp->ipp_rthdrlen);
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 	if (addflag & SCTP_IPV6_RECVDSTOPTS) {
5870Sstevel@tonic-gate 		cmsg = (struct cmsghdr *)optptr;
5880Sstevel@tonic-gate 		cmsg->cmsg_level = IPPROTO_IPV6;
5890Sstevel@tonic-gate 		cmsg->cmsg_type = IPV6_DSTOPTS;
5900Sstevel@tonic-gate 		cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_dstoptslen;
5910Sstevel@tonic-gate 		optptr += sizeof (*cmsg);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen);
5940Sstevel@tonic-gate 		optptr += ipp->ipp_dstoptslen;
5950Sstevel@tonic-gate 		ASSERT(OK_32PTR(optptr));
5960Sstevel@tonic-gate 		/* Save as last value */
5970Sstevel@tonic-gate 		sctp_savebuf((void **)&sctp->sctp_dstopts,
5980Sstevel@tonic-gate 		    &sctp->sctp_dstoptslen,
5990Sstevel@tonic-gate 		    (ipp->ipp_fields & IPPF_DSTOPTS),
6000Sstevel@tonic-gate 		    ipp->ipp_dstopts, ipp->ipp_dstoptslen);
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	ASSERT(optptr == mp1->b_wptr);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	return (0);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate void
6090Sstevel@tonic-gate sctp_free_reass(sctp_instr_t *sip)
6100Sstevel@tonic-gate {
6110Sstevel@tonic-gate 	mblk_t *mp, *mpnext, *mctl;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	for (mp = sip->istr_reass; mp != NULL; mp = mpnext) {
6140Sstevel@tonic-gate 		mpnext = mp->b_next;
6150Sstevel@tonic-gate 		mp->b_next = NULL;
6160Sstevel@tonic-gate 		mp->b_prev = NULL;
6170Sstevel@tonic-gate 		if (DB_TYPE(mp) == M_CTL) {
6180Sstevel@tonic-gate 			mctl = mp;
6190Sstevel@tonic-gate 			ASSERT(mp->b_cont != NULL);
6200Sstevel@tonic-gate 			mp = mp->b_cont;
6210Sstevel@tonic-gate 			mctl->b_cont = NULL;
6220Sstevel@tonic-gate 			freeb(mctl);
6230Sstevel@tonic-gate 		}
6240Sstevel@tonic-gate 		freemsg(mp);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate /*
6290Sstevel@tonic-gate  * If the series of data fragments of which dmp is a part is successfully
6300Sstevel@tonic-gate  * reassembled, the first mblk in the series is returned. dc is adjusted
6310Sstevel@tonic-gate  * to point at the data chunk in the lead mblk, and b_rptr also points to
6320Sstevel@tonic-gate  * the data chunk; the following mblk's b_rptr's point at the actual payload.
6330Sstevel@tonic-gate  *
6340Sstevel@tonic-gate  * If the series is not yet reassembled, NULL is returned. dc is not changed.
6350Sstevel@tonic-gate  * XXX should probably move this up into the state machine.
6360Sstevel@tonic-gate  */
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate /* Fragment list for un-ordered messages. Partial delivery is not supported */
6390Sstevel@tonic-gate static mblk_t *
6400Sstevel@tonic-gate sctp_uodata_frag(sctp_t *sctp, mblk_t *dmp, sctp_data_hdr_t **dc)
6410Sstevel@tonic-gate {
6420Sstevel@tonic-gate 	mblk_t		*hmp;
6430Sstevel@tonic-gate 	mblk_t		*begin = NULL;
6440Sstevel@tonic-gate 	mblk_t		*end = NULL;
6450Sstevel@tonic-gate 	sctp_data_hdr_t	*qdc;
6460Sstevel@tonic-gate 	uint32_t	ntsn;
6470Sstevel@tonic-gate 	uint32_t	tsn = ntohl((*dc)->sdh_tsn);
6480Sstevel@tonic-gate #ifdef	DEBUG
6490Sstevel@tonic-gate 	mblk_t		*mp1;
6500Sstevel@tonic-gate #endif
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	/* First frag. */
6530Sstevel@tonic-gate 	if (sctp->sctp_uo_frags == NULL) {
6540Sstevel@tonic-gate 		sctp->sctp_uo_frags = dmp;
6550Sstevel@tonic-gate 		return (NULL);
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 	hmp = sctp->sctp_uo_frags;
6580Sstevel@tonic-gate 	/*
6590Sstevel@tonic-gate 	 * Insert the segment according to the TSN, fragmented unordered
6600Sstevel@tonic-gate 	 * chunks are sequenced by TSN.
6610Sstevel@tonic-gate 	 */
6620Sstevel@tonic-gate 	while (hmp != NULL) {
6630Sstevel@tonic-gate 		qdc = (sctp_data_hdr_t *)hmp->b_rptr;
6640Sstevel@tonic-gate 		ntsn = ntohl(qdc->sdh_tsn);
6650Sstevel@tonic-gate 		if (SEQ_GT(ntsn, tsn)) {
6660Sstevel@tonic-gate 			if (hmp->b_prev == NULL) {
6670Sstevel@tonic-gate 				dmp->b_next = hmp;
6680Sstevel@tonic-gate 				hmp->b_prev = dmp;
6690Sstevel@tonic-gate 				sctp->sctp_uo_frags = dmp;
6700Sstevel@tonic-gate 			} else {
6710Sstevel@tonic-gate 				dmp->b_next = hmp;
6720Sstevel@tonic-gate 				dmp->b_prev = hmp->b_prev;
6730Sstevel@tonic-gate 				hmp->b_prev->b_next = dmp;
6740Sstevel@tonic-gate 				hmp->b_prev = dmp;
6750Sstevel@tonic-gate 			}
6760Sstevel@tonic-gate 			break;
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 		if (hmp->b_next == NULL) {
6790Sstevel@tonic-gate 			hmp->b_next = dmp;
6800Sstevel@tonic-gate 			dmp->b_prev = hmp;
6810Sstevel@tonic-gate 			break;
6820Sstevel@tonic-gate 		}
6830Sstevel@tonic-gate 		hmp = hmp->b_next;
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate 	/* check if we completed a msg */
6860Sstevel@tonic-gate 	if (SCTP_DATA_GET_BBIT(*dc)) {
6870Sstevel@tonic-gate 		begin = dmp;
6880Sstevel@tonic-gate 	} else if (SCTP_DATA_GET_EBIT(*dc)) {
6890Sstevel@tonic-gate 		end = dmp;
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 	/*
6920Sstevel@tonic-gate 	 * We walk consecutive TSNs backwards till we get a seg. with
6930Sstevel@tonic-gate 	 * the B bit
6940Sstevel@tonic-gate 	 */
6950Sstevel@tonic-gate 	if (begin == NULL) {
6960Sstevel@tonic-gate 		for (hmp = dmp->b_prev; hmp != NULL; hmp = hmp->b_prev) {
6970Sstevel@tonic-gate 			qdc = (sctp_data_hdr_t *)hmp->b_rptr;
6980Sstevel@tonic-gate 			ntsn = ntohl(qdc->sdh_tsn);
6990Sstevel@tonic-gate 			if ((int32_t)(tsn - ntsn) > 1) {
7000Sstevel@tonic-gate 				return (NULL);
7010Sstevel@tonic-gate 			}
7020Sstevel@tonic-gate 			if (SCTP_DATA_GET_BBIT(qdc)) {
7030Sstevel@tonic-gate 				begin = hmp;
7040Sstevel@tonic-gate 				break;
7050Sstevel@tonic-gate 			}
7060Sstevel@tonic-gate 			tsn = ntsn;
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate 	tsn = ntohl((*dc)->sdh_tsn);
7100Sstevel@tonic-gate 	/*
7110Sstevel@tonic-gate 	 * We walk consecutive TSNs till we get a seg. with the E bit
7120Sstevel@tonic-gate 	 */
7130Sstevel@tonic-gate 	if (end == NULL) {
7140Sstevel@tonic-gate 		for (hmp = dmp->b_next; hmp != NULL; hmp = hmp->b_next) {
7150Sstevel@tonic-gate 			qdc = (sctp_data_hdr_t *)hmp->b_rptr;
7160Sstevel@tonic-gate 			ntsn = ntohl(qdc->sdh_tsn);
7170Sstevel@tonic-gate 			if ((int32_t)(ntsn - tsn) > 1) {
7180Sstevel@tonic-gate 				return (NULL);
7190Sstevel@tonic-gate 			}
7200Sstevel@tonic-gate 			if (SCTP_DATA_GET_EBIT(qdc)) {
7210Sstevel@tonic-gate 				end = hmp;
7220Sstevel@tonic-gate 				break;
7230Sstevel@tonic-gate 			}
7240Sstevel@tonic-gate 			tsn = ntsn;
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 	if (begin == NULL || end == NULL) {
7280Sstevel@tonic-gate 		return (NULL);
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate 	/* Got one!, Remove the msg from the list */
7310Sstevel@tonic-gate 	if (sctp->sctp_uo_frags == begin) {
7320Sstevel@tonic-gate 		ASSERT(begin->b_prev == NULL);
7330Sstevel@tonic-gate 		sctp->sctp_uo_frags = end->b_next;
7340Sstevel@tonic-gate 		if (end->b_next != NULL)
7350Sstevel@tonic-gate 			end->b_next->b_prev = NULL;
7360Sstevel@tonic-gate 	} else {
7370Sstevel@tonic-gate 		begin->b_prev->b_next = end->b_next;
7380Sstevel@tonic-gate 		if (end->b_next != NULL)
7390Sstevel@tonic-gate 			end->b_next->b_prev = begin->b_prev;
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 	begin->b_prev = NULL;
7420Sstevel@tonic-gate 	end->b_next = NULL;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/*
7450Sstevel@tonic-gate 	 * Null out b_next and b_prev and chain using b_cont.
7460Sstevel@tonic-gate 	 */
7470Sstevel@tonic-gate 	dmp = end = begin;
7480Sstevel@tonic-gate 	hmp = begin->b_next;
7490Sstevel@tonic-gate 	*dc = (sctp_data_hdr_t *)begin->b_rptr;
7500Sstevel@tonic-gate 	begin->b_next = NULL;
7510Sstevel@tonic-gate 	while (hmp != NULL) {
7520Sstevel@tonic-gate 		qdc = (sctp_data_hdr_t *)hmp->b_rptr;
7530Sstevel@tonic-gate 		hmp->b_rptr = (uchar_t *)(qdc + 1);
7540Sstevel@tonic-gate 		end = hmp->b_next;
7550Sstevel@tonic-gate 		dmp->b_cont = hmp;
7560Sstevel@tonic-gate 		dmp = hmp;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 		if (end != NULL)
7590Sstevel@tonic-gate 			hmp->b_next = NULL;
7600Sstevel@tonic-gate 		hmp->b_prev = NULL;
7610Sstevel@tonic-gate 		hmp = end;
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_reassmsgs);
7640Sstevel@tonic-gate #ifdef	DEBUG
7650Sstevel@tonic-gate 	mp1 = begin;
7660Sstevel@tonic-gate 	while (mp1 != NULL) {
7670Sstevel@tonic-gate 		ASSERT(mp1->b_next == NULL);
7680Sstevel@tonic-gate 		ASSERT(mp1->b_prev == NULL);
7690Sstevel@tonic-gate 		mp1 = mp1->b_cont;
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate #endif
7720Sstevel@tonic-gate 	return (begin);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate /*
7750Sstevel@tonic-gate  * Fragment list for ordered messages.
7760Sstevel@tonic-gate  * If no error occures, error is set to 0. If we run out of memory, error
7770Sstevel@tonic-gate  * is set to 1. If the peer commits a fatal error (like using different
7780Sstevel@tonic-gate  * sequence numbers for the same data fragment series), the association is
7790Sstevel@tonic-gate  * aborted and error is set to 2.
7800Sstevel@tonic-gate  */
7810Sstevel@tonic-gate static mblk_t *
7820Sstevel@tonic-gate sctp_data_frag(sctp_t *sctp, mblk_t *dmp, sctp_data_hdr_t **dc, int *error,
7830Sstevel@tonic-gate     sctp_instr_t *sip, int trypartial, int *tpfinished)
7840Sstevel@tonic-gate {
7850Sstevel@tonic-gate 	mblk_t		*hmp;
7860Sstevel@tonic-gate 	mblk_t		*pmp;
7870Sstevel@tonic-gate 	mblk_t		*qmp;
7880Sstevel@tonic-gate 	mblk_t		*mp;
7890Sstevel@tonic-gate 	mblk_t		*prev;
7900Sstevel@tonic-gate 	mblk_t		*prevprev;
7910Sstevel@tonic-gate 	mblk_t		*first_mp;
7920Sstevel@tonic-gate 	sctp_reass_t	*srp;
7930Sstevel@tonic-gate 	sctp_data_hdr_t	*qdc;
7940Sstevel@tonic-gate 	sctp_data_hdr_t	*bdc;
7950Sstevel@tonic-gate 	sctp_data_hdr_t	*edc;
7960Sstevel@tonic-gate 	uint32_t	tsn;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	/*
7990Sstevel@tonic-gate 	 * We can overwrite the Link Layer + IP header here, I suppose.
8000Sstevel@tonic-gate 	 * The M_CTL does not leave this function. We need to check
8010Sstevel@tonic-gate 	 * DB_REF(dmp) before using DB_BASE(dmp), since there could be
8020Sstevel@tonic-gate 	 * two fragments for different ssns in the same mblk.
8030Sstevel@tonic-gate 	 */
8040Sstevel@tonic-gate #define	SCTP_NEW_REASS(nmp, dmp, srp, seterror)				\
8050Sstevel@tonic-gate 	if ((DB_REF(dmp) == 2) && (MBLKHEAD(dmp) >= 			\
8060Sstevel@tonic-gate 	    (sizeof (*(srp)) + sizeof (sctp_hdr_t)))) {			\
8070Sstevel@tonic-gate 		(nmp) = (dmp);						\
8080Sstevel@tonic-gate 	} else {							\
8090Sstevel@tonic-gate 		(nmp) = allocb(sizeof (*(srp)), BPRI_MED); 		\
8100Sstevel@tonic-gate 		if ((nmp) == NULL) {					\
8110Sstevel@tonic-gate 			switch (seterror) {				\
8120Sstevel@tonic-gate 			case B_TRUE:					\
8130Sstevel@tonic-gate 				*error = 1;				\
8140Sstevel@tonic-gate 				break;					\
8150Sstevel@tonic-gate 			}						\
8160Sstevel@tonic-gate 			return (NULL);					\
8170Sstevel@tonic-gate 		}							\
8180Sstevel@tonic-gate 		DB_TYPE(nmp) = M_CTL;					\
8190Sstevel@tonic-gate 		(nmp)->b_cont = dmp;					\
8200Sstevel@tonic-gate 	}								\
8210Sstevel@tonic-gate 	(srp) = (sctp_reass_t *)DB_BASE(nmp);
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	*error = 0;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	/* find the reassembly queue for this data chunk */
8260Sstevel@tonic-gate 	hmp = qmp = sip->istr_reass;
8270Sstevel@tonic-gate 	for (; hmp != NULL; hmp = hmp->b_next) {
8280Sstevel@tonic-gate 		srp = (sctp_reass_t *)DB_BASE(hmp);
8290Sstevel@tonic-gate 		if (ntohs((*dc)->sdh_ssn) == srp->ssn)
8300Sstevel@tonic-gate 			goto foundit;
8310Sstevel@tonic-gate 		else if (SSN_GT(srp->ssn, ntohs((*dc)->sdh_ssn)))
8320Sstevel@tonic-gate 			break;
8330Sstevel@tonic-gate 		qmp = hmp;
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	SCTP_NEW_REASS(pmp, dmp, srp, B_TRUE);
8370Sstevel@tonic-gate 	srp->ssn = ntohs((*dc)->sdh_ssn);
8380Sstevel@tonic-gate 	srp->needed = 0;
8390Sstevel@tonic-gate 	srp->got = 1;
8400Sstevel@tonic-gate 	srp->tail = dmp;
8410Sstevel@tonic-gate 	srp->partial_delivered = B_FALSE;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	if (hmp != NULL) {
8440Sstevel@tonic-gate 		if (sip->istr_reass == hmp) {
8450Sstevel@tonic-gate 			sip->istr_reass = pmp;
8460Sstevel@tonic-gate 			pmp->b_next = hmp;
8470Sstevel@tonic-gate 			pmp->b_prev = NULL;
8480Sstevel@tonic-gate 			hmp->b_prev = pmp;
8490Sstevel@tonic-gate 		} else {
8500Sstevel@tonic-gate 			qmp->b_next = pmp;
8510Sstevel@tonic-gate 			pmp->b_prev = qmp;
8520Sstevel@tonic-gate 			pmp->b_next = hmp;
8530Sstevel@tonic-gate 			hmp->b_prev = pmp;
8540Sstevel@tonic-gate 		}
8550Sstevel@tonic-gate 	} else {
8560Sstevel@tonic-gate 		/* make a new reass head and stick it on the end */
8570Sstevel@tonic-gate 		if (sip->istr_reass == NULL) {
8580Sstevel@tonic-gate 			sip->istr_reass = pmp;
8590Sstevel@tonic-gate 			pmp->b_prev = NULL;
8600Sstevel@tonic-gate 		} else {
8610Sstevel@tonic-gate 			qmp->b_next = pmp;
8620Sstevel@tonic-gate 			pmp->b_prev = qmp;
8630Sstevel@tonic-gate 		}
8640Sstevel@tonic-gate 		pmp->b_next = NULL;
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 	return (NULL);
8670Sstevel@tonic-gate foundit:
8680Sstevel@tonic-gate 	/*
8690Sstevel@tonic-gate 	 * else already have a reassembly queue. Insert the new data chunk
8700Sstevel@tonic-gate 	 * in the reassemble queue. Try the tail first, on the assumption
8710Sstevel@tonic-gate 	 * that the fragments are coming in in order.
8720Sstevel@tonic-gate 	 */
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	qmp = srp->tail;
8750Sstevel@tonic-gate 	qdc = (sctp_data_hdr_t *)qmp->b_rptr;
8760Sstevel@tonic-gate 	ASSERT(qmp->b_cont == NULL);
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	/* XXXIs it fine to do this just here? */
8790Sstevel@tonic-gate 	if ((*dc)->sdh_sid != qdc->sdh_sid) {
8800Sstevel@tonic-gate 		/* our peer is fatally confused; XXX abort the assc */
8810Sstevel@tonic-gate 		*error = 2;
8820Sstevel@tonic-gate 		return (NULL);
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 	if (SEQ_GT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) {
8850Sstevel@tonic-gate 		qmp->b_cont = dmp;
8860Sstevel@tonic-gate 		srp->tail = dmp;
8870Sstevel@tonic-gate 		dmp->b_cont = NULL;
8880Sstevel@tonic-gate 		goto inserted;
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	/* Next check for insertion at the beginning */
8920Sstevel@tonic-gate 	qmp = (DB_TYPE(hmp) == M_DATA) ? hmp : hmp->b_cont;
8930Sstevel@tonic-gate 	qdc = (sctp_data_hdr_t *)qmp->b_rptr;
8940Sstevel@tonic-gate 	if (SEQ_LT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) {
8950Sstevel@tonic-gate 		if (DB_TYPE(hmp) == M_DATA) {
8960Sstevel@tonic-gate 			sctp_reass_t	*srp1 = srp;
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 			SCTP_NEW_REASS(pmp, dmp, srp, B_TRUE);
8990Sstevel@tonic-gate 			ASSERT(pmp->b_prev == NULL && pmp->b_next == NULL);
9000Sstevel@tonic-gate 			if (sip->istr_reass == hmp) {
9010Sstevel@tonic-gate 				sip->istr_reass = pmp;
9020Sstevel@tonic-gate 				if (hmp->b_next != NULL) {
9030Sstevel@tonic-gate 					hmp->b_next->b_prev = pmp;
9040Sstevel@tonic-gate 					pmp->b_next = hmp->b_next;
9050Sstevel@tonic-gate 				}
9060Sstevel@tonic-gate 			} else {
9070Sstevel@tonic-gate 				hmp->b_prev->b_next = pmp;
9080Sstevel@tonic-gate 				pmp->b_prev = hmp->b_prev;
9090Sstevel@tonic-gate 				if (hmp->b_next != NULL) {
9100Sstevel@tonic-gate 					hmp->b_next->b_prev = pmp;
9110Sstevel@tonic-gate 					pmp->b_next = hmp->b_next;
9120Sstevel@tonic-gate 				}
9130Sstevel@tonic-gate 			}
9140Sstevel@tonic-gate 			srp->ssn = srp1->ssn;
9150Sstevel@tonic-gate 			srp->needed = srp1->needed;
9160Sstevel@tonic-gate 			srp->got = srp1->got;
9170Sstevel@tonic-gate 			srp->tail = srp1->tail;
9180Sstevel@tonic-gate 			srp->partial_delivered = srp1->partial_delivered;
9190Sstevel@tonic-gate 			hmp->b_next = hmp->b_prev = NULL;
9200Sstevel@tonic-gate 			dmp->b_cont = hmp;
9210Sstevel@tonic-gate 			hmp = pmp;
9220Sstevel@tonic-gate 		} else {
9230Sstevel@tonic-gate 			ASSERT(DB_TYPE(hmp) == M_CTL);
9240Sstevel@tonic-gate 			dmp->b_cont = qmp;
9250Sstevel@tonic-gate 			hmp->b_cont = dmp;
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 		goto inserted;
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	/* Insert somewhere in the middle */
9310Sstevel@tonic-gate 	for (;;) {
9320Sstevel@tonic-gate 		/* Tail check above should have caught this */
9330Sstevel@tonic-gate 		ASSERT(qmp->b_cont != NULL);
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 		qdc = (sctp_data_hdr_t *)qmp->b_cont->b_rptr;
9360Sstevel@tonic-gate 		if (SEQ_LT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) {
9370Sstevel@tonic-gate 			/* insert here */
9380Sstevel@tonic-gate 			dmp->b_cont = qmp->b_cont;
9390Sstevel@tonic-gate 			qmp->b_cont = dmp;
9400Sstevel@tonic-gate 			break;
9410Sstevel@tonic-gate 		}
9420Sstevel@tonic-gate 		qmp = qmp->b_cont;
9430Sstevel@tonic-gate 	}
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate inserted:
9460Sstevel@tonic-gate 	(srp->got)++;
9470Sstevel@tonic-gate 	first_mp = (DB_TYPE(hmp) == M_DATA) ? hmp : hmp->b_cont;
9480Sstevel@tonic-gate 	if (srp->needed == 0) {
9490Sstevel@tonic-gate 		/* check if we have the first and last fragments */
9500Sstevel@tonic-gate 		bdc = (sctp_data_hdr_t *)first_mp->b_rptr;
9510Sstevel@tonic-gate 		edc = (sctp_data_hdr_t *)srp->tail->b_rptr;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 		/* calculate how many fragments are needed, if possible  */
9540Sstevel@tonic-gate 		if (SCTP_DATA_GET_BBIT(bdc) && SCTP_DATA_GET_EBIT(edc))
9550Sstevel@tonic-gate 			srp->needed = ntohl(edc->sdh_tsn) -
9560Sstevel@tonic-gate 			    ntohl(bdc->sdh_tsn) + 1;
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	if (srp->needed != srp->got) {
9600Sstevel@tonic-gate 		if (!trypartial)
9610Sstevel@tonic-gate 			return (NULL);
9620Sstevel@tonic-gate 		/*
9630Sstevel@tonic-gate 		 * Try partial delivery. We need a consecutive run of
9640Sstevel@tonic-gate 		 * at least two chunks, starting from the first chunk
9650Sstevel@tonic-gate 		 * (which may have been the last + 1 chunk from a
9660Sstevel@tonic-gate 		 * previous partial delivery).
9670Sstevel@tonic-gate 		 */
9680Sstevel@tonic-gate 		dprint(4, ("trypartial: got=%d, needed=%d\n",
9690Sstevel@tonic-gate 		    (int)(srp->got), (int)(srp->needed)));
9700Sstevel@tonic-gate 		mp = first_mp;
9710Sstevel@tonic-gate 		if (mp->b_cont == NULL) {
9720Sstevel@tonic-gate 			/* need at least two chunks */
9730Sstevel@tonic-gate 			dprint(4, ("trypartial: only 1 chunk\n"));
9740Sstevel@tonic-gate 			return (NULL);
9750Sstevel@tonic-gate 		}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 		qdc = (sctp_data_hdr_t *)mp->b_rptr;
9780Sstevel@tonic-gate 		if (!SCTP_DATA_GET_BBIT(qdc)) {
9790Sstevel@tonic-gate 			/* don't have first chunk; can't do it. */
9800Sstevel@tonic-gate 			dprint(4, ("trypartial: no beginning\n"));
9810Sstevel@tonic-gate 			return (NULL);
9820Sstevel@tonic-gate 		}
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 		tsn = ntohl(qdc->sdh_tsn) + 1;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 		/*
9870Sstevel@tonic-gate 		 * This loop has two exit conditions: the
9880Sstevel@tonic-gate 		 * end of received chunks has been reached, or
9890Sstevel@tonic-gate 		 * there is a break in the sequence. We want
9900Sstevel@tonic-gate 		 * to chop the reassembly list as follows (the
9910Sstevel@tonic-gate 		 * numbers are TSNs):
9920Sstevel@tonic-gate 		 *   10 -> 11 -> | 12	(end of chunks)
9930Sstevel@tonic-gate 		 *   10 -> 11 -> | 12 -> 14 (break in sequence)
9940Sstevel@tonic-gate 		 */
9950Sstevel@tonic-gate 		prevprev = prev = mp;
9960Sstevel@tonic-gate 		mp = mp->b_cont;
9970Sstevel@tonic-gate 		while (mp != NULL) {
9980Sstevel@tonic-gate 			qdc = (sctp_data_hdr_t *)mp->b_rptr;
9990Sstevel@tonic-gate 			if (ntohl(qdc->sdh_tsn) != tsn) {
10000Sstevel@tonic-gate 				/*
10010Sstevel@tonic-gate 				 * break in sequence.
10020Sstevel@tonic-gate 				 * 1st and 2nd chunks are not sequntial.
10030Sstevel@tonic-gate 				 */
10040Sstevel@tonic-gate 				if (mp == first_mp->b_cont)
10050Sstevel@tonic-gate 					return (NULL);
10060Sstevel@tonic-gate 				/* Back up mp and prev */
10070Sstevel@tonic-gate 				mp = prev;
10080Sstevel@tonic-gate 				prev = prevprev;
10090Sstevel@tonic-gate 				break;
10100Sstevel@tonic-gate 			}
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 			/* end of sequence */
10130Sstevel@tonic-gate 			if (mp->b_cont == NULL)
10140Sstevel@tonic-gate 				break;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 			prevprev = prev;
10170Sstevel@tonic-gate 			prev = mp;
10180Sstevel@tonic-gate 			mp = mp->b_cont;
10190Sstevel@tonic-gate 			tsn++;
10200Sstevel@tonic-gate 		}
10210Sstevel@tonic-gate 		if (DB_TYPE(hmp) == M_DATA) {
10220Sstevel@tonic-gate 			sctp_reass_t	*srp1 = srp;
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 			SCTP_NEW_REASS(pmp, mp, srp, B_FALSE);
10250Sstevel@tonic-gate 			ASSERT(pmp->b_prev == NULL && pmp->b_next == NULL);
10260Sstevel@tonic-gate 			if (sip->istr_reass == hmp) {
10270Sstevel@tonic-gate 				sip->istr_reass = pmp;
10280Sstevel@tonic-gate 				if (hmp->b_next != NULL) {
10290Sstevel@tonic-gate 					hmp->b_next->b_prev = pmp;
10300Sstevel@tonic-gate 					pmp->b_next = hmp->b_next;
10310Sstevel@tonic-gate 				}
10320Sstevel@tonic-gate 			} else {
10330Sstevel@tonic-gate 				hmp->b_prev->b_next = pmp;
10340Sstevel@tonic-gate 				pmp->b_prev = hmp->b_prev;
10350Sstevel@tonic-gate 				if (hmp->b_next != NULL) {
10360Sstevel@tonic-gate 					hmp->b_next->b_prev = pmp;
10370Sstevel@tonic-gate 					pmp->b_next = hmp->b_next;
10380Sstevel@tonic-gate 				}
10390Sstevel@tonic-gate 			}
10400Sstevel@tonic-gate 			srp->ssn = srp1->ssn;
10410Sstevel@tonic-gate 			srp->needed = srp1->needed;
10420Sstevel@tonic-gate 			srp->got = srp1->got;
10430Sstevel@tonic-gate 			srp->tail = srp1->tail;
10440Sstevel@tonic-gate 			hmp->b_next = hmp->b_prev = NULL;
10450Sstevel@tonic-gate 			dmp = hmp;
10460Sstevel@tonic-gate 			hmp = pmp;
10470Sstevel@tonic-gate 		} else {
10480Sstevel@tonic-gate 			ASSERT(DB_TYPE(hmp) == M_CTL);
10490Sstevel@tonic-gate 			dmp = hmp->b_cont;
10500Sstevel@tonic-gate 			hmp->b_cont = mp;
10510Sstevel@tonic-gate 		}
10520Sstevel@tonic-gate 		/*
10530Sstevel@tonic-gate 		 * mp now points at the last chunk in the sequence,
10540Sstevel@tonic-gate 		 * and prev points to mp's previous in the list.
10550Sstevel@tonic-gate 		 * We chop the list at prev, and convert mp into the
10560Sstevel@tonic-gate 		 * new list head by setting the B bit. Subsequence
10570Sstevel@tonic-gate 		 * fragment deliveries will follow the normal reassembly
10580Sstevel@tonic-gate 		 * path.
10590Sstevel@tonic-gate 		 */
10600Sstevel@tonic-gate 		prev->b_cont = NULL;
10610Sstevel@tonic-gate 		bdc = (sctp_data_hdr_t *)mp->b_rptr;
10620Sstevel@tonic-gate 		SCTP_DATA_SET_BBIT(bdc);
10630Sstevel@tonic-gate 		*tpfinished = 0;
10640Sstevel@tonic-gate 		srp->partial_delivered = B_TRUE;
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 		dprint(4, ("trypartial: got some, got=%d, needed=%d\n",
10670Sstevel@tonic-gate 		    (int)(srp->got), (int)(srp->needed)));
10680Sstevel@tonic-gate 		goto fixup;
10690Sstevel@tonic-gate 	}
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	/*
10720Sstevel@tonic-gate 	 * else reassembly done; prepare the data for delivery.
10730Sstevel@tonic-gate 	 * First unlink hmp from the ssn list.
10740Sstevel@tonic-gate 	 */
10750Sstevel@tonic-gate 	if (sip->istr_reass == hmp) {
10760Sstevel@tonic-gate 		sip->istr_reass = hmp->b_next;
10770Sstevel@tonic-gate 		if (hmp->b_next) {
10780Sstevel@tonic-gate 			hmp->b_next->b_prev = NULL;
10790Sstevel@tonic-gate 		}
10800Sstevel@tonic-gate 	} else {
10810Sstevel@tonic-gate 		ASSERT(hmp->b_prev != NULL);
10820Sstevel@tonic-gate 		hmp->b_prev->b_next = hmp->b_next;
10830Sstevel@tonic-gate 		if (hmp->b_next) {
10840Sstevel@tonic-gate 			hmp->b_next->b_prev = hmp->b_prev;
10850Sstevel@tonic-gate 		}
10860Sstevel@tonic-gate 	}
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	/*
10890Sstevel@tonic-gate 	 * Using b_prev and b_next was a little sinful, but OK since
10900Sstevel@tonic-gate 	 * this mblk is never put*'d. However, freeb() will still
10910Sstevel@tonic-gate 	 * ASSERT that they are unused, so we need to NULL them out now.
10920Sstevel@tonic-gate 	 */
10930Sstevel@tonic-gate 	hmp->b_next = NULL;
10940Sstevel@tonic-gate 	hmp->b_prev = NULL;
10950Sstevel@tonic-gate 	dmp = hmp;
10960Sstevel@tonic-gate 	if (DB_TYPE(hmp) == M_CTL) {
10970Sstevel@tonic-gate 		dmp = dmp->b_cont;
10980Sstevel@tonic-gate 		hmp->b_cont = NULL;
10990Sstevel@tonic-gate 		freeb(hmp);
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate 	*tpfinished = 1;
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate fixup:
11040Sstevel@tonic-gate 	/*
11050Sstevel@tonic-gate 	 * Adjust all mblk's except the lead so their rptr's point to the
11060Sstevel@tonic-gate 	 * payload. sctp_data_chunk() will need to process the lead's
11070Sstevel@tonic-gate 	 * data chunk section, so leave it's rptr pointing at the data chunk.
11080Sstevel@tonic-gate 	 */
11090Sstevel@tonic-gate 	*dc = (sctp_data_hdr_t *)dmp->b_rptr;
11100Sstevel@tonic-gate 	if (trypartial && !(*tpfinished)) {
11110Sstevel@tonic-gate 		(srp->got)--;
11120Sstevel@tonic-gate 		ASSERT(srp->got != 0);
11130Sstevel@tonic-gate 		if (srp->needed != 0) {
11140Sstevel@tonic-gate 			(srp->needed)--;
11150Sstevel@tonic-gate 			ASSERT(srp->needed != 0);
11160Sstevel@tonic-gate 		}
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 	for (qmp = dmp->b_cont; qmp; qmp = qmp->b_cont) {
11190Sstevel@tonic-gate 		qdc = (sctp_data_hdr_t *)qmp->b_rptr;
11200Sstevel@tonic-gate 		qmp->b_rptr = (uchar_t *)(qdc + 1);
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 		/*
11230Sstevel@tonic-gate 		 * If in partial delivery, deduct the balance from got
11240Sstevel@tonic-gate 		 * and needed here, now that we know we are actually
11250Sstevel@tonic-gate 		 * delivering these data.
11260Sstevel@tonic-gate 		 */
11270Sstevel@tonic-gate 		if (trypartial && !(*tpfinished)) {
11280Sstevel@tonic-gate 			(srp->got)--;
11290Sstevel@tonic-gate 			ASSERT(srp->got != 0);
11300Sstevel@tonic-gate 			if (srp->needed != 0) {
11310Sstevel@tonic-gate 				(srp->needed)--;
11320Sstevel@tonic-gate 				ASSERT(srp->needed != 0);
11330Sstevel@tonic-gate 			}
11340Sstevel@tonic-gate 		}
11350Sstevel@tonic-gate 	}
11360Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_reassmsgs);
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	return (dmp);
11390Sstevel@tonic-gate }
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate static void
11420Sstevel@tonic-gate sctp_add_dup(uint32_t tsn, mblk_t **dups)
11430Sstevel@tonic-gate {
11440Sstevel@tonic-gate 	mblk_t *mp;
11450Sstevel@tonic-gate 	size_t bsize = SCTP_DUP_MBLK_SZ * sizeof (tsn);
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	if (dups == NULL) {
11480Sstevel@tonic-gate 		return;
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	/* first time? */
11520Sstevel@tonic-gate 	if (*dups == NULL) {
11530Sstevel@tonic-gate 		*dups = allocb(bsize, BPRI_MED);
11540Sstevel@tonic-gate 		if (*dups == NULL) {
11550Sstevel@tonic-gate 			return;
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	mp = *dups;
11600Sstevel@tonic-gate 	if ((mp->b_wptr - mp->b_rptr) >= bsize) {
11610Sstevel@tonic-gate 		/* maximum reached */
11620Sstevel@tonic-gate 		return;
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	/* add the duplicate tsn */
11660Sstevel@tonic-gate 	bcopy(&tsn, mp->b_wptr, sizeof (tsn));
11670Sstevel@tonic-gate 	mp->b_wptr += sizeof (tsn);
11680Sstevel@tonic-gate 	ASSERT((mp->b_wptr - mp->b_rptr) <= bsize);
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate static void
11720Sstevel@tonic-gate sctp_data_chunk(sctp_t *sctp, sctp_chunk_hdr_t *ch, mblk_t *mp, mblk_t **dups,
11730Sstevel@tonic-gate     sctp_faddr_t *fp, ip6_pkt_t *ipp)
11740Sstevel@tonic-gate {
11750Sstevel@tonic-gate 	sctp_data_hdr_t *dc;
11760Sstevel@tonic-gate 	mblk_t *dmp, *pmp;
11770Sstevel@tonic-gate 	mblk_t *errmp;
11780Sstevel@tonic-gate 	sctp_instr_t *instr;
11790Sstevel@tonic-gate 	int ubit;
11800Sstevel@tonic-gate 	int isfrag;
11810Sstevel@tonic-gate 	uint16_t ssn;
11820Sstevel@tonic-gate 	uint32_t oftsn;
11830Sstevel@tonic-gate 	boolean_t can_deliver = B_TRUE;
11840Sstevel@tonic-gate 	uint32_t tsn;
11850Sstevel@tonic-gate 	int dlen;
11860Sstevel@tonic-gate 	int trypartial = 0;
11870Sstevel@tonic-gate 	int tpfinished = 1;
11880Sstevel@tonic-gate 	int32_t new_rwnd;
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	/* The following are used multiple times, so we inline them */
11910Sstevel@tonic-gate #define	SCTP_ACK_IT(sctp, tsn)						\
11920Sstevel@tonic-gate 	if (tsn == sctp->sctp_ftsn) {					\
11930Sstevel@tonic-gate 		dprint(2, ("data_chunk: acking next %x\n", tsn));	\
11940Sstevel@tonic-gate 		(sctp->sctp_ftsn)++;					\
11950Sstevel@tonic-gate 	} else if (SEQ_GT(tsn, sctp->sctp_ftsn)) {			\
11960Sstevel@tonic-gate 		/* Got a gap; record it */				\
11970Sstevel@tonic-gate 		dprint(2, ("data_chunk: acking gap %x\n", tsn));	\
11980Sstevel@tonic-gate 		sctp_ack_add(&sctp->sctp_sack_info,			\
11990Sstevel@tonic-gate 				tsn,					\
12000Sstevel@tonic-gate 				&sctp->sctp_sack_gaps);			\
12010Sstevel@tonic-gate 		sctp->sctp_force_sack = 1;				\
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	errmp = NULL;
12050Sstevel@tonic-gate 	dmp = NULL;
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	dc = (sctp_data_hdr_t *)ch;
12080Sstevel@tonic-gate 	tsn = ntohl(dc->sdh_tsn);
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	dprint(3, ("sctp_data_chunk: mp=%p tsn=%x\n", mp, tsn));
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	/* Check for duplicates */
12130Sstevel@tonic-gate 	if (SEQ_LT(tsn, sctp->sctp_ftsn)) {
12140Sstevel@tonic-gate 		dprint(4, ("sctp_data_chunk: dropping duplicate\n"));
12150Sstevel@tonic-gate 		sctp->sctp_force_sack = 1;
12160Sstevel@tonic-gate 		sctp_add_dup(dc->sdh_tsn, dups);
12170Sstevel@tonic-gate 		return;
12180Sstevel@tonic-gate 	}
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
12210Sstevel@tonic-gate 		sctp_set_t *sp;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 		for (sp = sctp->sctp_sack_info; sp; sp = sp->next) {
12240Sstevel@tonic-gate 			if (SEQ_GEQ(tsn, sp->begin) && SEQ_LEQ(tsn, sp->end)) {
12250Sstevel@tonic-gate 				dprint(4,
12260Sstevel@tonic-gate 				("sctp_data_chunk: dropping dup > cumtsn\n"));
12270Sstevel@tonic-gate 				sctp->sctp_force_sack = 1;
12280Sstevel@tonic-gate 				sctp_add_dup(dc->sdh_tsn, dups);
12290Sstevel@tonic-gate 				return;
12300Sstevel@tonic-gate 			}
12310Sstevel@tonic-gate 		}
12320Sstevel@tonic-gate 	}
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	/* We cannot deliver anything up now but we still need to handle it. */
12350Sstevel@tonic-gate 	if (SCTP_IS_DETACHED(sctp)) {
12360Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpInClosed);
12370Sstevel@tonic-gate 		can_deliver = B_FALSE;
12380Sstevel@tonic-gate 	}
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	dlen = ntohs(dc->sdh_len) - sizeof (*dc);
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	/* Check for buffer space */
12430Sstevel@tonic-gate 	if (sctp->sctp_rwnd - sctp->sctp_rxqueued < dlen) {
12440Sstevel@tonic-gate 		/* Drop and SACK, but don't advance the cumulative TSN. */
12450Sstevel@tonic-gate 		sctp->sctp_force_sack = 1;
12460Sstevel@tonic-gate 		dprint(0, ("sctp_data_chunk: exceed rwnd %d rxqueued %d "
12470Sstevel@tonic-gate 			"ssn %d tsn %x\n", sctp->sctp_rwnd,
12480Sstevel@tonic-gate 			sctp->sctp_rxqueued, dc->sdh_ssn, ntohl(dc->sdh_tsn)));
12490Sstevel@tonic-gate 		return;
12500Sstevel@tonic-gate 	}
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	if (ntohs(dc->sdh_sid) >= sctp->sctp_num_istr) {
12530Sstevel@tonic-gate 		uint16_t	inval_parm[2];
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 		inval_parm[0] = dc->sdh_sid;
12560Sstevel@tonic-gate 		/* RESERVED to be ignored at the receiving end */
12570Sstevel@tonic-gate 		inval_parm[1] = 0;
12580Sstevel@tonic-gate 		/* ack and drop it */
12590Sstevel@tonic-gate 		errmp = sctp_make_err(sctp, SCTP_ERR_BAD_SID,
12600Sstevel@tonic-gate 		    (char *)inval_parm, sizeof (inval_parm));
12610Sstevel@tonic-gate 		SCTP_ACK_IT(sctp, tsn);
12620Sstevel@tonic-gate 		if (errmp != NULL)
12630Sstevel@tonic-gate 			sctp_send_err(sctp, errmp, NULL);
12640Sstevel@tonic-gate 		return;
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	ubit = SCTP_DATA_GET_UBIT(dc);
12680Sstevel@tonic-gate 	ASSERT(sctp->sctp_instr != NULL);
12690Sstevel@tonic-gate 	instr = &sctp->sctp_instr[ntohs(dc->sdh_sid)];
12700Sstevel@tonic-gate 	/* Initialize the stream, if not yet used */
12710Sstevel@tonic-gate 	if (instr->sctp == NULL)
12720Sstevel@tonic-gate 		instr->sctp = sctp;
12730Sstevel@tonic-gate 	/*
12740Sstevel@tonic-gate 	 * If we are getting low on buffers set trypartial to try
12750Sstevel@tonic-gate 	 * a partial delivery if we are reassembling a fragmented
12760Sstevel@tonic-gate 	 * message. Only do this if we can immediately deliver the
12770Sstevel@tonic-gate 	 * partially assembled message, and only partially deliver
12780Sstevel@tonic-gate 	 * one message at a time (i.e. messages cannot be intermixed
12790Sstevel@tonic-gate 	 * arriving at the upper layer). A simple way to enforce
12800Sstevel@tonic-gate 	 * this is to only try partial delivery if this TSN is
12810Sstevel@tonic-gate 	 * the next expected TSN. Partial Delivery not supported
12820Sstevel@tonic-gate 	 * for un-ordered message.
12830Sstevel@tonic-gate 	 */
12840Sstevel@tonic-gate 	isfrag = !(SCTP_DATA_GET_BBIT(dc) && SCTP_DATA_GET_EBIT(dc));
12850Sstevel@tonic-gate 	ssn = ntohs(dc->sdh_ssn);
12860Sstevel@tonic-gate 	if ((sctp->sctp_rwnd - sctp->sctp_rxqueued < SCTP_RECV_LOWATER) &&
12870Sstevel@tonic-gate 	    !ubit && isfrag && (tsn == sctp->sctp_ftsn)) {
12880Sstevel@tonic-gate 		trypartial = 1;
12890Sstevel@tonic-gate 	}
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	dmp = dupb(mp);
12920Sstevel@tonic-gate 	if (dmp == NULL) {
12930Sstevel@tonic-gate 		/* drop it and don't ack it, causing the peer to retransmit */
12940Sstevel@tonic-gate 		return;
12950Sstevel@tonic-gate 	}
12960Sstevel@tonic-gate 	dmp->b_wptr = (uchar_t *)ch + ntohs(ch->sch_len);
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	sctp->sctp_rxqueued += dlen;
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	oftsn = sctp->sctp_ftsn;
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	if (isfrag) {
13030Sstevel@tonic-gate 		int error = 0;
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 		/* fragmented data chunk */
13060Sstevel@tonic-gate 		dmp->b_rptr = (uchar_t *)dc;
13070Sstevel@tonic-gate 		if (ubit) {
13080Sstevel@tonic-gate 			dmp = sctp_uodata_frag(sctp, dmp, &dc);
13090Sstevel@tonic-gate #if	DEBUG
13100Sstevel@tonic-gate 			if (dmp != NULL) {
13110Sstevel@tonic-gate 				ASSERT(instr ==
13120Sstevel@tonic-gate 				    &sctp->sctp_instr[ntohs(dc->sdh_sid)]);
13130Sstevel@tonic-gate 			}
13140Sstevel@tonic-gate #endif
13150Sstevel@tonic-gate 		} else {
13160Sstevel@tonic-gate 			dmp = sctp_data_frag(sctp, dmp, &dc, &error, instr,
13170Sstevel@tonic-gate 			    trypartial, &tpfinished);
13180Sstevel@tonic-gate 		}
13190Sstevel@tonic-gate 		if (error != 0) {
13200Sstevel@tonic-gate 			sctp->sctp_rxqueued -= dlen;
13210Sstevel@tonic-gate 			if (error == 1) {
13220Sstevel@tonic-gate 				/*
13230Sstevel@tonic-gate 				 * out of memory; don't ack it so
13240Sstevel@tonic-gate 				 * the peer retransmits
13250Sstevel@tonic-gate 				 */
13260Sstevel@tonic-gate 				return;
13270Sstevel@tonic-gate 			} else if (error == 2) {
13280Sstevel@tonic-gate 				/*
13290Sstevel@tonic-gate 				 * fatal error (i.e. peer used different
13300Sstevel@tonic-gate 				 * ssn's for same fragmented data) --
13310Sstevel@tonic-gate 				 * the association has been aborted.
13320Sstevel@tonic-gate 				 * XXX need to return errval so state
13330Sstevel@tonic-gate 				 * machine can also abort processing.
13340Sstevel@tonic-gate 				 */
13350Sstevel@tonic-gate 				dprint(0, ("error 2: must not happen!\n"));
13360Sstevel@tonic-gate 				return;
13370Sstevel@tonic-gate 			}
13380Sstevel@tonic-gate 		}
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 		if (dmp == NULL) {
13410Sstevel@tonic-gate 			/*
13420Sstevel@tonic-gate 			 * Can't process this data now, but the cumulative
13430Sstevel@tonic-gate 			 * TSN may be advanced, so do the checks at done.
13440Sstevel@tonic-gate 			 */
13450Sstevel@tonic-gate 			SCTP_ACK_IT(sctp, tsn);
13460Sstevel@tonic-gate 			goto done;
13470Sstevel@tonic-gate 		}
13480Sstevel@tonic-gate 	}
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	if (!ubit && !trypartial && ssn != instr->nextseq) {
13510Sstevel@tonic-gate 		/* Adjust rptr to point at the data chunk for compares */
13520Sstevel@tonic-gate 		dmp->b_rptr = (uchar_t *)dc;
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 		dprint(2,
13550Sstevel@tonic-gate 		    ("data_chunk: inserted %x in pq (ssn %d expected %d)\n",
13560Sstevel@tonic-gate 		    ntohl(dc->sdh_tsn), (int)(ssn), (int)(instr->nextseq)));
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 		if (instr->istr_msgs == NULL) {
13590Sstevel@tonic-gate 			instr->istr_msgs = dmp;
13600Sstevel@tonic-gate 			ASSERT(dmp->b_prev == NULL && dmp->b_next == NULL);
13610Sstevel@tonic-gate 		} else {
13620Sstevel@tonic-gate 			mblk_t			*imblk = instr->istr_msgs;
13630Sstevel@tonic-gate 			sctp_data_hdr_t		*idc;
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 			/*
13660Sstevel@tonic-gate 			 * XXXNeed to take sequence wraps into account,
13670Sstevel@tonic-gate 			 * ... and a more efficient insertion algo.
13680Sstevel@tonic-gate 			 */
13690Sstevel@tonic-gate 			for (;;) {
13700Sstevel@tonic-gate 				idc = (sctp_data_hdr_t *)imblk->b_rptr;
13710Sstevel@tonic-gate 				if (SSN_GT(ntohs(idc->sdh_ssn),
13720Sstevel@tonic-gate 					ntohs(dc->sdh_ssn))) {
13730Sstevel@tonic-gate 					if (instr->istr_msgs == imblk) {
13740Sstevel@tonic-gate 						instr->istr_msgs = dmp;
13750Sstevel@tonic-gate 						dmp->b_next = imblk;
13760Sstevel@tonic-gate 						imblk->b_prev = dmp;
13770Sstevel@tonic-gate 					} else {
13780Sstevel@tonic-gate 						ASSERT(imblk->b_prev != NULL);
13790Sstevel@tonic-gate 						imblk->b_prev->b_next = dmp;
13800Sstevel@tonic-gate 						dmp->b_prev = imblk->b_prev;
13810Sstevel@tonic-gate 						imblk->b_prev = dmp;
13820Sstevel@tonic-gate 						dmp->b_next = imblk;
13830Sstevel@tonic-gate 					}
13840Sstevel@tonic-gate 					break;
13850Sstevel@tonic-gate 				}
13860Sstevel@tonic-gate 				if (imblk->b_next == NULL) {
13870Sstevel@tonic-gate 					imblk->b_next = dmp;
13880Sstevel@tonic-gate 					dmp->b_prev = imblk;
13890Sstevel@tonic-gate 					break;
13900Sstevel@tonic-gate 				}
13910Sstevel@tonic-gate 				imblk = imblk->b_next;
13920Sstevel@tonic-gate 			}
13930Sstevel@tonic-gate 		}
13940Sstevel@tonic-gate 		(instr->istr_nmsgs)++;
13950Sstevel@tonic-gate 		(sctp->sctp_istr_nmsgs)++;
13960Sstevel@tonic-gate 		SCTP_ACK_IT(sctp, tsn);
13970Sstevel@tonic-gate 		return;
13980Sstevel@tonic-gate 	}
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 	/*
14010Sstevel@tonic-gate 	 * Else we can deliver the data directly. Recalculate
14020Sstevel@tonic-gate 	 * dlen now since we may have reassembled data.
14030Sstevel@tonic-gate 	 */
14040Sstevel@tonic-gate 	dlen = dmp->b_wptr - (uchar_t *)dc - sizeof (*dc);
14050Sstevel@tonic-gate 	for (pmp = dmp->b_cont; pmp != NULL; pmp = pmp->b_cont)
14060Sstevel@tonic-gate 		dlen += pmp->b_wptr - pmp->b_rptr;
14070Sstevel@tonic-gate 	ASSERT(sctp->sctp_rxqueued >= dlen);
14080Sstevel@tonic-gate 	ASSERT(sctp->sctp_rwnd >= dlen);
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	/* Deliver the message. */
14110Sstevel@tonic-gate 	sctp->sctp_rxqueued -= dlen;
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	if (can_deliver) {
14140Sstevel@tonic-gate 		dmp->b_rptr = (uchar_t *)(dc + 1);
14150Sstevel@tonic-gate 		if (sctp_input_add_ancillary(sctp, &dmp, dc, fp, ipp) == 0) {
14160Sstevel@tonic-gate 			dprint(1, ("sctp_data_chunk: delivering %lu bytes\n",
14170Sstevel@tonic-gate 			    msgdsize(dmp)));
14180Sstevel@tonic-gate 			sctp->sctp_rwnd -= dlen;
14190Sstevel@tonic-gate 			new_rwnd = sctp->sctp_ulp_recv(sctp->sctp_ulpd, dmp,
14200Sstevel@tonic-gate 			    tpfinished ? 0 : SCTP_PARTIAL_DATA);
14210Sstevel@tonic-gate 			if (new_rwnd > sctp->sctp_rwnd) {
14220Sstevel@tonic-gate 				sctp->sctp_rwnd = new_rwnd;
14230Sstevel@tonic-gate 			}
14240Sstevel@tonic-gate 			SCTP_ACK_IT(sctp, tsn);
14250Sstevel@tonic-gate 		} else {
14260Sstevel@tonic-gate 			/* Just free the message if we don't have memory. */
14270Sstevel@tonic-gate 			freemsg(dmp);
14280Sstevel@tonic-gate 			return;
14290Sstevel@tonic-gate 		}
14300Sstevel@tonic-gate 	} else {
14310Sstevel@tonic-gate 		/* About to free the data */
14320Sstevel@tonic-gate 		freemsg(dmp);
14330Sstevel@tonic-gate 		SCTP_ACK_IT(sctp, tsn);
14340Sstevel@tonic-gate 	}
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	/*
14370Sstevel@tonic-gate 	 * data, now enqueued, may already have been processed and free'd
14380Sstevel@tonic-gate 	 * by the ULP (or we may have just freed it above, if we could not
14390Sstevel@tonic-gate 	 * deliver it), so we must not reference it (this is why we kept
14400Sstevel@tonic-gate 	 * the ssn and ubit above).
14410Sstevel@tonic-gate 	 */
14420Sstevel@tonic-gate 	if (ubit != 0) {
14430Sstevel@tonic-gate 		BUMP_LOCAL(sctp->sctp_iudchunks);
14440Sstevel@tonic-gate 		goto done;
14450Sstevel@tonic-gate 	}
14460Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_idchunks);
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	/*
14490Sstevel@tonic-gate 	 * If there was a partial delivery and it has not finished,
14500Sstevel@tonic-gate 	 * don't pull anything from the pqueues.
14510Sstevel@tonic-gate 	 */
14520Sstevel@tonic-gate 	if (!tpfinished) {
14530Sstevel@tonic-gate 		goto done;
14540Sstevel@tonic-gate 	}
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	instr->nextseq = ssn + 1;
14570Sstevel@tonic-gate 	/* Deliver any successive data chunks in the instr queue */
14580Sstevel@tonic-gate 	while (instr->istr_nmsgs > 0) {
14590Sstevel@tonic-gate 		dmp = (mblk_t *)instr->istr_msgs;
14600Sstevel@tonic-gate 		dc = (sctp_data_hdr_t *)dmp->b_rptr;
14610Sstevel@tonic-gate 		ssn = ntohs(dc->sdh_ssn);
14620Sstevel@tonic-gate 		/* Gap in the sequence */
14630Sstevel@tonic-gate 		if (ssn != instr->nextseq)
14640Sstevel@tonic-gate 			break;
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 		/* Else deliver the data */
14670Sstevel@tonic-gate 		(instr->istr_nmsgs)--;
14680Sstevel@tonic-gate 		(instr->nextseq)++;
14690Sstevel@tonic-gate 		(sctp->sctp_istr_nmsgs)--;
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 		instr->istr_msgs = instr->istr_msgs->b_next;
14720Sstevel@tonic-gate 		if (instr->istr_msgs != NULL)
14730Sstevel@tonic-gate 			instr->istr_msgs->b_prev = NULL;
14740Sstevel@tonic-gate 		dmp->b_next = dmp->b_prev = NULL;
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 		dprint(2, ("data_chunk: pulling %x from pq (ssn %d)\n",
14770Sstevel@tonic-gate 		    ntohl(dc->sdh_tsn), (int)ssn));
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 		/*
14800Sstevel@tonic-gate 		 * If this chunk was reassembled, each b_cont represents
14810Sstevel@tonic-gate 		 * another TSN; advance ftsn now.
14820Sstevel@tonic-gate 		 */
14830Sstevel@tonic-gate 		dlen = dmp->b_wptr - dmp->b_rptr - sizeof (*dc);
14840Sstevel@tonic-gate 		for (pmp = dmp->b_cont; pmp; pmp = pmp->b_cont)
14850Sstevel@tonic-gate 			dlen += pmp->b_wptr - pmp->b_rptr;
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 		ASSERT(sctp->sctp_rxqueued >= dlen);
14880Sstevel@tonic-gate 		ASSERT(sctp->sctp_rwnd >= dlen);
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 		sctp->sctp_rxqueued -= dlen;
14910Sstevel@tonic-gate 		if (can_deliver) {
14920Sstevel@tonic-gate 			dmp->b_rptr = (uchar_t *)(dc + 1);
14930Sstevel@tonic-gate 			if (sctp_input_add_ancillary(sctp, &dmp, dc, fp,
14940Sstevel@tonic-gate 			    ipp) == 0) {
14950Sstevel@tonic-gate 				dprint(1, ("sctp_data_chunk: delivering %lu "
14960Sstevel@tonic-gate 				    "bytes\n", msgdsize(dmp)));
14970Sstevel@tonic-gate 				sctp->sctp_rwnd -= dlen;
14980Sstevel@tonic-gate 				new_rwnd = sctp->sctp_ulp_recv(sctp->sctp_ulpd,
14990Sstevel@tonic-gate 				    dmp, tpfinished ? 0 : SCTP_PARTIAL_DATA);
15000Sstevel@tonic-gate 				if (new_rwnd > sctp->sctp_rwnd) {
15010Sstevel@tonic-gate 					sctp->sctp_rwnd = new_rwnd;
15020Sstevel@tonic-gate 				}
15030Sstevel@tonic-gate 				SCTP_ACK_IT(sctp, tsn);
15040Sstevel@tonic-gate 			} else {
15050Sstevel@tonic-gate 				freemsg(dmp);
15060Sstevel@tonic-gate 				return;
15070Sstevel@tonic-gate 			}
15080Sstevel@tonic-gate 		} else {
15090Sstevel@tonic-gate 			/* About to free the data */
15100Sstevel@tonic-gate 			freemsg(dmp);
15110Sstevel@tonic-gate 			SCTP_ACK_IT(sctp, tsn);
15120Sstevel@tonic-gate 		}
15130Sstevel@tonic-gate 	}
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate done:
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	/*
15180Sstevel@tonic-gate 	 * If there are gap reports pending, check if advancing
15190Sstevel@tonic-gate 	 * the ftsn here closes a gap. If so, we can advance
15200Sstevel@tonic-gate 	 * ftsn to the end of the set.
15210Sstevel@tonic-gate 	 */
15220Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL &&
15230Sstevel@tonic-gate 	    sctp->sctp_ftsn == sctp->sctp_sack_info->begin) {
15240Sstevel@tonic-gate 		sctp->sctp_ftsn = sctp->sctp_sack_info->end + 1;
15250Sstevel@tonic-gate 	}
15260Sstevel@tonic-gate 	/*
15270Sstevel@tonic-gate 	 * If ftsn has moved forward, maybe we can remove gap reports.
15280Sstevel@tonic-gate 	 * NB: dmp may now be NULL, so don't dereference it here.
15290Sstevel@tonic-gate 	 */
15300Sstevel@tonic-gate 	if (oftsn != sctp->sctp_ftsn && sctp->sctp_sack_info != NULL) {
15310Sstevel@tonic-gate 		sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1,
15320Sstevel@tonic-gate 		    &sctp->sctp_sack_gaps);
15330Sstevel@tonic-gate 		dprint(2, ("data_chunk: removed acks before %x (num=%d)\n",
15340Sstevel@tonic-gate 		    sctp->sctp_ftsn - 1, sctp->sctp_sack_gaps));
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate #ifdef	DEBUG
15380Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
15390Sstevel@tonic-gate 		ASSERT(sctp->sctp_ftsn != sctp->sctp_sack_info->begin);
15400Sstevel@tonic-gate 	}
15410Sstevel@tonic-gate #endif
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate #undef	SCTP_ACK_IT
15440Sstevel@tonic-gate }
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate void
15470Sstevel@tonic-gate sctp_fill_sack(sctp_t *sctp, unsigned char *dst, int sacklen)
15480Sstevel@tonic-gate {
15490Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
15500Sstevel@tonic-gate 	sctp_sack_chunk_t *sc;
15510Sstevel@tonic-gate 	sctp_sack_frag_t *sf;
15520Sstevel@tonic-gate 	uint16_t num_gaps = sctp->sctp_sack_gaps;
15530Sstevel@tonic-gate 	sctp_set_t *sp;
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	/* Chunk hdr */
15560Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)dst;
15570Sstevel@tonic-gate 	sch->sch_id = CHUNK_SACK;
15580Sstevel@tonic-gate 	sch->sch_flags = 0;
15590Sstevel@tonic-gate 	sch->sch_len = htons(sacklen);
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	/* SACK chunk */
15620Sstevel@tonic-gate 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	sc = (sctp_sack_chunk_t *)(sch + 1);
15650Sstevel@tonic-gate 	sc->ssc_cumtsn = htonl(sctp->sctp_lastacked);
15660Sstevel@tonic-gate 	if (sctp->sctp_rxqueued < sctp->sctp_rwnd) {
15670Sstevel@tonic-gate 		sc->ssc_a_rwnd = htonl(sctp->sctp_rwnd - sctp->sctp_rxqueued);
15680Sstevel@tonic-gate 	} else {
15690Sstevel@tonic-gate 		sc->ssc_a_rwnd = 0;
15700Sstevel@tonic-gate 	}
15710Sstevel@tonic-gate 	sc->ssc_numfrags = htons(num_gaps);
15720Sstevel@tonic-gate 	sc->ssc_numdups = 0;
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	/* lay in gap reports */
15750Sstevel@tonic-gate 	sf = (sctp_sack_frag_t *)(sc + 1);
15760Sstevel@tonic-gate 	for (sp = sctp->sctp_sack_info; sp; sp = sp->next) {
15770Sstevel@tonic-gate 		uint16_t offset;
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 		/* start */
15800Sstevel@tonic-gate 		if (sp->begin > sctp->sctp_lastacked) {
15810Sstevel@tonic-gate 			offset = (uint16_t)(sp->begin - sctp->sctp_lastacked);
15820Sstevel@tonic-gate 		} else {
15830Sstevel@tonic-gate 			/* sequence number wrap */
15840Sstevel@tonic-gate 			offset = (uint16_t)(UINT32_MAX - sctp->sctp_lastacked +
15850Sstevel@tonic-gate 			    sp->begin);
15860Sstevel@tonic-gate 		}
15870Sstevel@tonic-gate 		sf->ssf_start = htons(offset);
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 		/* end */
15900Sstevel@tonic-gate 		if (sp->end >= sp->begin) {
15910Sstevel@tonic-gate 			offset += (uint16_t)(sp->end - sp->begin);
15920Sstevel@tonic-gate 		} else {
15930Sstevel@tonic-gate 			/* sequence number wrap */
15940Sstevel@tonic-gate 			offset += (uint16_t)(UINT32_MAX - sp->begin + sp->end);
15950Sstevel@tonic-gate 		}
15960Sstevel@tonic-gate 		sf->ssf_end = htons(offset);
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 		sf++;
15990Sstevel@tonic-gate 		/* This is just for debugging (a la the following assertion) */
16000Sstevel@tonic-gate 		num_gaps--;
16010Sstevel@tonic-gate 	}
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	ASSERT(num_gaps == 0);
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	/* If the SACK timer is running, stop it */
16060Sstevel@tonic-gate 	if (sctp->sctp_ack_timer_running) {
16070Sstevel@tonic-gate 		sctp_timer_stop(sctp->sctp_ack_mp);
16080Sstevel@tonic-gate 		sctp->sctp_ack_timer_running = B_FALSE;
16090Sstevel@tonic-gate 	}
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_obchunks);
16120Sstevel@tonic-gate }
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate mblk_t *
16150Sstevel@tonic-gate sctp_make_sack(sctp_t *sctp, sctp_faddr_t *sendto, mblk_t *dups)
16160Sstevel@tonic-gate {
16170Sstevel@tonic-gate 	mblk_t *smp;
16180Sstevel@tonic-gate 	size_t slen;
16190Sstevel@tonic-gate 	sctp_chunk_hdr_t *sch;
16200Sstevel@tonic-gate 	sctp_sack_chunk_t *sc;
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	if (sctp->sctp_force_sack) {
16230Sstevel@tonic-gate 		sctp->sctp_force_sack = 0;
16240Sstevel@tonic-gate 		goto checks_done;
16250Sstevel@tonic-gate 	}
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	if (sctp->sctp_state == SCTPS_ESTABLISHED) {
16280Sstevel@tonic-gate 		if (sctp->sctp_sack_toggle < 2) {
16290Sstevel@tonic-gate 			/* no need to SACK right now */
16300Sstevel@tonic-gate 			dprint(2, ("sctp_make_sack: %p no sack (toggle)\n",
16310Sstevel@tonic-gate 			    sctp));
16320Sstevel@tonic-gate 			return (NULL);
16330Sstevel@tonic-gate 		} else if (sctp->sctp_sack_toggle >= 2) {
16340Sstevel@tonic-gate 			sctp->sctp_sack_toggle = 0;
16350Sstevel@tonic-gate 		}
16360Sstevel@tonic-gate 	}
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
16390Sstevel@tonic-gate 		dprint(2, ("sctp_make_sack: %p no sack (already)\n", sctp));
16400Sstevel@tonic-gate 		return (NULL);
16410Sstevel@tonic-gate 	}
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate checks_done:
16440Sstevel@tonic-gate 	dprint(2, ("sctp_make_sack: acking %x\n", sctp->sctp_ftsn - 1));
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	slen = sizeof (*sch) + sizeof (*sc) +
16470Sstevel@tonic-gate 	    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
16480Sstevel@tonic-gate 	smp = sctp_make_mp(sctp, sendto, slen);
16490Sstevel@tonic-gate 	if (smp == NULL) {
16500Sstevel@tonic-gate 		return (NULL);
16510Sstevel@tonic-gate 	}
16520Sstevel@tonic-gate 	sch = (sctp_chunk_hdr_t *)smp->b_wptr;
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	sctp_fill_sack(sctp, smp->b_wptr, slen);
16550Sstevel@tonic-gate 	smp->b_wptr += slen;
16560Sstevel@tonic-gate 	if (dups) {
16570Sstevel@tonic-gate 		sc = (sctp_sack_chunk_t *)(sch + 1);
16580Sstevel@tonic-gate 		sc->ssc_numdups = htons((dups->b_wptr - dups->b_rptr)
16590Sstevel@tonic-gate 		    / sizeof (uint32_t));
16600Sstevel@tonic-gate 		sch->sch_len = htons(slen + (dups->b_wptr - dups->b_rptr));
16610Sstevel@tonic-gate 		smp->b_cont = dups;
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	return (smp);
16650Sstevel@tonic-gate }
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate void
16680Sstevel@tonic-gate sctp_sack(sctp_t *sctp, mblk_t *dups)
16690Sstevel@tonic-gate {
16700Sstevel@tonic-gate 	mblk_t *smp;
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 	/* If we are shutting down, let send_shutdown() bundle the SACK */
16730Sstevel@tonic-gate 	if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) {
16740Sstevel@tonic-gate 		sctp_send_shutdown(sctp, 0);
16750Sstevel@tonic-gate 	}
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	ASSERT(sctp->sctp_lastdata != NULL);
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	if ((smp = sctp_make_sack(sctp, sctp->sctp_lastdata, dups)) == NULL) {
16800Sstevel@tonic-gate 		/* The caller of sctp_sack() will not free the dups mblk. */
16810Sstevel@tonic-gate 		if (dups != NULL)
16820Sstevel@tonic-gate 			freeb(dups);
16830Sstevel@tonic-gate 		return;
16840Sstevel@tonic-gate 	}
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	sctp_set_iplen(sctp, smp);
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 	dprint(2, ("sctp_sack: sending to %p %x:%x:%x:%x\n",
16890Sstevel@tonic-gate 	    sctp->sctp_lastdata, SCTP_PRINTADDR(sctp->sctp_lastdata->faddr)));
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate 	sctp->sctp_active = lbolt64;
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	BUMP_MIB(&sctp_mib, sctpOutAck);
16940Sstevel@tonic-gate 	sctp_add_sendq(sctp, smp);
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate /*
16980Sstevel@tonic-gate  * This is called if we have a message that was partially sent and is
16990Sstevel@tonic-gate  * abandoned. The cum TSN will be the last chunk sent for this message,
17000Sstevel@tonic-gate  * subsequent chunks will be marked ABANDONED. We send a Forward TSN
17010Sstevel@tonic-gate  * chunk in this case with the TSN of the last sent chunk so that the
17020Sstevel@tonic-gate  * peer can clean up its fragment list for this message. This message
17030Sstevel@tonic-gate  * will be removed from the transmit list when the peer sends a SACK
17040Sstevel@tonic-gate  * back.
17050Sstevel@tonic-gate  */
17060Sstevel@tonic-gate int
17070Sstevel@tonic-gate sctp_check_abandoned_msg(sctp_t *sctp, mblk_t *meta)
17080Sstevel@tonic-gate {
17090Sstevel@tonic-gate 	sctp_data_hdr_t	*dh;
17100Sstevel@tonic-gate 	mblk_t		*nmp;
17110Sstevel@tonic-gate 	mblk_t		*head;
17120Sstevel@tonic-gate 	int32_t		unsent = 0;
17130Sstevel@tonic-gate 	mblk_t		*mp1 = meta->b_cont;
17140Sstevel@tonic-gate 	uint32_t	adv_pap = sctp->sctp_adv_pap;
17150Sstevel@tonic-gate 	sctp_faddr_t	*fp = sctp->sctp_current;
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 	dh = (sctp_data_hdr_t *)mp1->b_rptr;
17180Sstevel@tonic-gate 	if (SEQ_GEQ(sctp->sctp_lastack_rxd, ntohl(dh->sdh_tsn))) {
17190Sstevel@tonic-gate 		sctp_ftsn_set_t	*sets = NULL;
17200Sstevel@tonic-gate 		uint_t		nsets = 0;
17210Sstevel@tonic-gate 		uint32_t	seglen = sizeof (uint32_t);
17220Sstevel@tonic-gate 		boolean_t	ubit = SCTP_DATA_GET_UBIT(dh);
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 		while (mp1->b_next != NULL && SCTP_CHUNK_ISSENT(mp1->b_next))
17250Sstevel@tonic-gate 			mp1 = mp1->b_next;
17260Sstevel@tonic-gate 		dh = (sctp_data_hdr_t *)mp1->b_rptr;
17270Sstevel@tonic-gate 		sctp->sctp_adv_pap = ntohl(dh->sdh_tsn);
17280Sstevel@tonic-gate 		if (!ubit &&
17290Sstevel@tonic-gate 		    !sctp_add_ftsn_set(&sets, fp, meta, &nsets, &seglen)) {
17300Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
17310Sstevel@tonic-gate 			return (ENOMEM);
17320Sstevel@tonic-gate 		}
17330Sstevel@tonic-gate 		nmp = sctp_make_ftsn_chunk(sctp, fp, sets, nsets, seglen);
17340Sstevel@tonic-gate 		sctp_free_ftsn_set(sets);
17350Sstevel@tonic-gate 		if (nmp == NULL) {
17360Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
17370Sstevel@tonic-gate 			return (ENOMEM);
17380Sstevel@tonic-gate 		}
1739*252Svi117747 		head = sctp_add_proto_hdr(sctp, fp, nmp, 0, NULL);
17400Sstevel@tonic-gate 		if (head == NULL) {
17410Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
17420Sstevel@tonic-gate 			freemsg(nmp);
17430Sstevel@tonic-gate 			return (ENOMEM);
17440Sstevel@tonic-gate 		}
17450Sstevel@tonic-gate 		SCTP_MSG_SET_ABANDONED(meta);
17460Sstevel@tonic-gate 		sctp_set_iplen(sctp, head);
17470Sstevel@tonic-gate 		sctp_add_sendq(sctp, head);
17480Sstevel@tonic-gate 		if (!fp->timer_running)
17490Sstevel@tonic-gate 			SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
17500Sstevel@tonic-gate 		mp1 = mp1->b_next;
17510Sstevel@tonic-gate 		while (mp1 != NULL) {
17520Sstevel@tonic-gate 			ASSERT(!SCTP_CHUNK_ISSENT(mp1));
17530Sstevel@tonic-gate 			ASSERT(!SCTP_CHUNK_ABANDONED(mp1));
17540Sstevel@tonic-gate 			SCTP_ABANDON_CHUNK(mp1);
17550Sstevel@tonic-gate 			dh = (sctp_data_hdr_t *)mp1->b_rptr;
17560Sstevel@tonic-gate 			unsent += ntohs(dh->sdh_len) - sizeof (*dh);
17570Sstevel@tonic-gate 			mp1 = mp1->b_next;
17580Sstevel@tonic-gate 		}
17590Sstevel@tonic-gate 		ASSERT(sctp->sctp_unsent >= unsent);
17600Sstevel@tonic-gate 		sctp->sctp_unsent -= unsent;
17610Sstevel@tonic-gate 		/*
17620Sstevel@tonic-gate 		 * Update ULP the amount of queued data, which is
17630Sstevel@tonic-gate 		 * sent-unack'ed + unsent.
17640Sstevel@tonic-gate 		 */
17650Sstevel@tonic-gate 		if (!SCTP_IS_DETACHED(sctp)) {
17660Sstevel@tonic-gate 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
17670Sstevel@tonic-gate 			    sctp->sctp_unacked + sctp->sctp_unsent);
17680Sstevel@tonic-gate 		}
17690Sstevel@tonic-gate 		return (0);
17700Sstevel@tonic-gate 	}
17710Sstevel@tonic-gate 	return (-1);
17720Sstevel@tonic-gate }
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate uint32_t
17750Sstevel@tonic-gate sctp_cumack(sctp_t *sctp, uint32_t tsn, mblk_t **first_unacked)
17760Sstevel@tonic-gate {
17770Sstevel@tonic-gate 	mblk_t *ump, *nump, *mp = NULL;
17780Sstevel@tonic-gate 	uint16_t chunklen;
17790Sstevel@tonic-gate 	uint32_t xtsn;
17800Sstevel@tonic-gate 	sctp_faddr_t *fp;
17810Sstevel@tonic-gate 	sctp_data_hdr_t *sdc;
17820Sstevel@tonic-gate 	uint32_t cumack_forward = 0;
17830Sstevel@tonic-gate 	sctp_msg_hdr_t	*mhdr;
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	ump = sctp->sctp_xmit_head;
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	/*
17880Sstevel@tonic-gate 	 * Free messages only when they're completely acked.
17890Sstevel@tonic-gate 	 */
17900Sstevel@tonic-gate 	while (ump != NULL) {
17910Sstevel@tonic-gate 		mhdr = (sctp_msg_hdr_t *)ump->b_rptr;
17920Sstevel@tonic-gate 		for (mp = ump->b_cont; mp != NULL; mp = mp->b_next) {
17930Sstevel@tonic-gate 			if (SCTP_CHUNK_ABANDONED(mp)) {
17940Sstevel@tonic-gate 				ASSERT(SCTP_IS_MSG_ABANDONED(ump));
17950Sstevel@tonic-gate 				mp = NULL;
17960Sstevel@tonic-gate 				break;
17970Sstevel@tonic-gate 			}
17980Sstevel@tonic-gate 			/*
17990Sstevel@tonic-gate 			 * We check for abandoned message if we are PR-SCTP
18000Sstevel@tonic-gate 			 * aware, if this is not the first chunk in the
18010Sstevel@tonic-gate 			 * message (b_cont) and if the message is marked
18020Sstevel@tonic-gate 			 * abandoned.
18030Sstevel@tonic-gate 			 */
18040Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp)) {
18050Sstevel@tonic-gate 				if (sctp->sctp_prsctp_aware &&
18060Sstevel@tonic-gate 				    mp != ump->b_cont &&
18070Sstevel@tonic-gate 				    (SCTP_IS_MSG_ABANDONED(ump) ||
18080Sstevel@tonic-gate 				    SCTP_MSG_TO_BE_ABANDONED(ump, mhdr,
18090Sstevel@tonic-gate 				    sctp))) {
18100Sstevel@tonic-gate 					(void) sctp_check_abandoned_msg(sctp,
18110Sstevel@tonic-gate 					    ump);
18120Sstevel@tonic-gate 				}
18130Sstevel@tonic-gate 				goto cum_ack_done;
18140Sstevel@tonic-gate 			}
18150Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
18160Sstevel@tonic-gate 			xtsn = ntohl(sdc->sdh_tsn);
18170Sstevel@tonic-gate 			if (SEQ_GEQ(sctp->sctp_lastack_rxd, xtsn))
18180Sstevel@tonic-gate 				continue;
18190Sstevel@tonic-gate 			if (SEQ_GEQ(tsn, xtsn)) {
18200Sstevel@tonic-gate 				fp = SCTP_CHUNK_DEST(mp);
18210Sstevel@tonic-gate 				chunklen = ntohs(sdc->sdh_len);
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 				if (sctp->sctp_out_time != 0 &&
18240Sstevel@tonic-gate 				    xtsn == sctp->sctp_rtt_tsn) {
18250Sstevel@tonic-gate 					/* Got a new RTT measurement */
18260Sstevel@tonic-gate 					sctp_update_rtt(sctp, fp,
18270Sstevel@tonic-gate 					    lbolt64 - sctp->sctp_out_time);
18280Sstevel@tonic-gate 					sctp->sctp_out_time = 0;
18290Sstevel@tonic-gate 				}
18300Sstevel@tonic-gate 				if (SCTP_CHUNK_ISACKED(mp))
18310Sstevel@tonic-gate 					continue;
18320Sstevel@tonic-gate 				SCTP_CHUNK_ACKED(mp);
18330Sstevel@tonic-gate 				ASSERT(fp->suna >= chunklen);
18340Sstevel@tonic-gate 				fp->suna -= chunklen;
18350Sstevel@tonic-gate 				fp->acked += chunklen;
18360Sstevel@tonic-gate 				cumack_forward += chunklen;
18370Sstevel@tonic-gate 				ASSERT(sctp->sctp_unacked >=
18380Sstevel@tonic-gate 				    (chunklen - sizeof (*sdc)));
18390Sstevel@tonic-gate 				sctp->sctp_unacked -=
18400Sstevel@tonic-gate 				    (chunklen - sizeof (*sdc));
18410Sstevel@tonic-gate 				if (fp->suna == 0) {
18420Sstevel@tonic-gate 					/* all outstanding data acked */
18430Sstevel@tonic-gate 					fp->pba = 0;
18440Sstevel@tonic-gate 					SCTP_FADDR_TIMER_STOP(fp);
18450Sstevel@tonic-gate 				} else {
18460Sstevel@tonic-gate 					SCTP_FADDR_TIMER_RESTART(sctp, fp,
18470Sstevel@tonic-gate 					    fp->rto);
18480Sstevel@tonic-gate 				}
18490Sstevel@tonic-gate 			} else {
18500Sstevel@tonic-gate 				goto cum_ack_done;
18510Sstevel@tonic-gate 			}
18520Sstevel@tonic-gate 		}
18530Sstevel@tonic-gate 		nump = ump->b_next;
18540Sstevel@tonic-gate 		if (nump != NULL)
18550Sstevel@tonic-gate 			nump->b_prev = NULL;
18560Sstevel@tonic-gate 		if (ump == sctp->sctp_xmit_tail)
18570Sstevel@tonic-gate 			sctp->sctp_xmit_tail = nump;
18580Sstevel@tonic-gate 		if (SCTP_IS_MSG_ABANDONED(ump)) {
18590Sstevel@tonic-gate 			BUMP_LOCAL(sctp->sctp_prsctpdrop);
18600Sstevel@tonic-gate 			ump->b_next = NULL;
18610Sstevel@tonic-gate 			sctp_sendfail_event(sctp, ump, 0, B_TRUE);
18620Sstevel@tonic-gate 		} else {
18630Sstevel@tonic-gate 			sctp_free_msg(ump);
18640Sstevel@tonic-gate 		}
18650Sstevel@tonic-gate 		sctp->sctp_xmit_head = ump = nump;
18660Sstevel@tonic-gate 	}
18670Sstevel@tonic-gate cum_ack_done:
18680Sstevel@tonic-gate 	*first_unacked = mp;
18690Sstevel@tonic-gate 	if (cumack_forward > 0) {
18700Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpInAck);
18710Sstevel@tonic-gate 		if (SEQ_GT(sctp->sctp_lastack_rxd, sctp->sctp_recovery_tsn)) {
18720Sstevel@tonic-gate 			sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd;
18730Sstevel@tonic-gate 		}
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 		/*
18760Sstevel@tonic-gate 		 * Update ULP the amount of queued data, which is
18770Sstevel@tonic-gate 		 * sent-unack'ed + unsent.
18780Sstevel@tonic-gate 		 */
18790Sstevel@tonic-gate 		if (!SCTP_IS_DETACHED(sctp)) {
18800Sstevel@tonic-gate 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
18810Sstevel@tonic-gate 			    sctp->sctp_unacked + sctp->sctp_unsent);
18820Sstevel@tonic-gate 		}
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 		/* Time to send a shutdown? */
18850Sstevel@tonic-gate 		if (sctp->sctp_state == SCTPS_SHUTDOWN_PENDING) {
18860Sstevel@tonic-gate 			sctp_send_shutdown(sctp, 0);
18870Sstevel@tonic-gate 		}
18880Sstevel@tonic-gate 		sctp->sctp_xmit_unacked = mp;
18890Sstevel@tonic-gate 	} else {
18900Sstevel@tonic-gate 		/* dup ack */
18910Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpInDupAck);
18920Sstevel@tonic-gate 	}
18930Sstevel@tonic-gate 	sctp->sctp_lastack_rxd = tsn;
18940Sstevel@tonic-gate 	if (SEQ_LT(sctp->sctp_adv_pap, sctp->sctp_lastack_rxd))
18950Sstevel@tonic-gate 		sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
18960Sstevel@tonic-gate 	ASSERT(sctp->sctp_xmit_head || sctp->sctp_unacked == 0);
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	return (cumack_forward);
18990Sstevel@tonic-gate }
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate static int
19020Sstevel@tonic-gate sctp_set_frwnd(sctp_t *sctp, uint32_t frwnd)
19030Sstevel@tonic-gate {
19040Sstevel@tonic-gate 	uint32_t orwnd;
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	if (sctp->sctp_unacked > frwnd) {
19070Sstevel@tonic-gate 		sctp->sctp_frwnd = 0;
19080Sstevel@tonic-gate 		return (0);
19090Sstevel@tonic-gate 	}
19100Sstevel@tonic-gate 	orwnd = sctp->sctp_frwnd;
19110Sstevel@tonic-gate 	sctp->sctp_frwnd = frwnd - sctp->sctp_unacked;
19120Sstevel@tonic-gate 	if (orwnd < sctp->sctp_frwnd) {
19130Sstevel@tonic-gate 		return (1);
19140Sstevel@tonic-gate 	} else {
19150Sstevel@tonic-gate 		return (0);
19160Sstevel@tonic-gate 	}
19170Sstevel@tonic-gate }
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate /*
19200Sstevel@tonic-gate  * For un-ordered messages.
19210Sstevel@tonic-gate  * Walk the sctp->sctp_uo_frag list and remove any fragments with TSN
19220Sstevel@tonic-gate  * less than/equal to ftsn. Fragments for un-ordered messages are
19230Sstevel@tonic-gate  * strictly in sequence (w.r.t TSN).
19240Sstevel@tonic-gate  */
19250Sstevel@tonic-gate static int
19260Sstevel@tonic-gate sctp_ftsn_check_uo_frag(sctp_t *sctp, uint32_t ftsn)
19270Sstevel@tonic-gate {
19280Sstevel@tonic-gate 	mblk_t		*hmp;
19290Sstevel@tonic-gate 	mblk_t		*hmp_next;
19300Sstevel@tonic-gate 	sctp_data_hdr_t	*dc;
19310Sstevel@tonic-gate 	int		dlen = 0;
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	hmp = sctp->sctp_uo_frags;
19340Sstevel@tonic-gate 	while (hmp != NULL) {
19350Sstevel@tonic-gate 		hmp_next = hmp->b_next;
19360Sstevel@tonic-gate 		dc = (sctp_data_hdr_t *)hmp->b_rptr;
19370Sstevel@tonic-gate 		if (SEQ_GT(ntohl(dc->sdh_tsn), ftsn))
19380Sstevel@tonic-gate 			return (dlen);
19390Sstevel@tonic-gate 		sctp->sctp_uo_frags = hmp_next;
19400Sstevel@tonic-gate 		if (hmp_next != NULL)
19410Sstevel@tonic-gate 			hmp_next->b_prev = NULL;
19420Sstevel@tonic-gate 		hmp->b_next = NULL;
19430Sstevel@tonic-gate 		dlen += ntohs(dc->sdh_len) - sizeof (*dc);
19440Sstevel@tonic-gate 		freeb(hmp);
19450Sstevel@tonic-gate 		hmp = hmp_next;
19460Sstevel@tonic-gate 	}
19470Sstevel@tonic-gate 	return (dlen);
19480Sstevel@tonic-gate }
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate /*
19510Sstevel@tonic-gate  * For ordered messages.
19520Sstevel@tonic-gate  * Check for existing fragments for an sid-ssn pair reported as abandoned,
19530Sstevel@tonic-gate  * hence will not receive, in the Forward TSN. If there are fragments, then
19540Sstevel@tonic-gate  * we just nuke them. If and when Partial Delivery API is supported, we
19550Sstevel@tonic-gate  * would need to send a notification to the upper layer about this.
19560Sstevel@tonic-gate  */
19570Sstevel@tonic-gate static int
19580Sstevel@tonic-gate sctp_ftsn_check_frag(sctp_t *sctp, uint16_t ssn, sctp_instr_t *sip)
19590Sstevel@tonic-gate {
19600Sstevel@tonic-gate 	sctp_reass_t	*srp;
19610Sstevel@tonic-gate 	mblk_t		*hmp;
19620Sstevel@tonic-gate 	mblk_t		*dmp;
19630Sstevel@tonic-gate 	mblk_t		*hmp_next;
19640Sstevel@tonic-gate 	sctp_data_hdr_t	*dc;
19650Sstevel@tonic-gate 	int		dlen = 0;
19660Sstevel@tonic-gate 
19670Sstevel@tonic-gate 	hmp = sip->istr_reass;
19680Sstevel@tonic-gate 	while (hmp != NULL) {
19690Sstevel@tonic-gate 		hmp_next = hmp->b_next;
19700Sstevel@tonic-gate 		srp = (sctp_reass_t *)DB_BASE(hmp);
19710Sstevel@tonic-gate 		if (SSN_GT(srp->ssn, ssn))
19720Sstevel@tonic-gate 			return (dlen);
19730Sstevel@tonic-gate 		/*
19740Sstevel@tonic-gate 		 * If we had sent part of this message up, send a partial
19750Sstevel@tonic-gate 		 * delivery event. Since this is ordered delivery, we should
19760Sstevel@tonic-gate 		 * have sent partial message only for the next in sequence,
19770Sstevel@tonic-gate 		 * hence the ASSERT. See comments in sctp_data_chunk() for
19780Sstevel@tonic-gate 		 * trypartial.
19790Sstevel@tonic-gate 		 */
19800Sstevel@tonic-gate 		if (srp->partial_delivered) {
19810Sstevel@tonic-gate 			ASSERT(sip->nextseq == srp->ssn);
19820Sstevel@tonic-gate 			sctp_partial_delivery_event(sctp);
19830Sstevel@tonic-gate 		}
19840Sstevel@tonic-gate 		/* Take it out of the reass queue */
19850Sstevel@tonic-gate 		sip->istr_reass = hmp_next;
19860Sstevel@tonic-gate 		if (hmp_next != NULL)
19870Sstevel@tonic-gate 			hmp_next->b_prev = NULL;
19880Sstevel@tonic-gate 		hmp->b_next = NULL;
19890Sstevel@tonic-gate 		ASSERT(hmp->b_prev == NULL);
19900Sstevel@tonic-gate 		dmp = hmp;
19910Sstevel@tonic-gate 		if (DB_TYPE(hmp) == M_CTL) {
19920Sstevel@tonic-gate 			dmp = hmp->b_cont;
19930Sstevel@tonic-gate 			hmp->b_cont = NULL;
19940Sstevel@tonic-gate 			freeb(hmp);
19950Sstevel@tonic-gate 			hmp = dmp;
19960Sstevel@tonic-gate 		}
19970Sstevel@tonic-gate 		while (dmp != NULL) {
19980Sstevel@tonic-gate 			dc = (sctp_data_hdr_t *)dmp->b_rptr;
19990Sstevel@tonic-gate 			dlen += ntohs(dc->sdh_len) - sizeof (*dc);
20000Sstevel@tonic-gate 			dmp = dmp->b_cont;
20010Sstevel@tonic-gate 		}
20020Sstevel@tonic-gate 		freemsg(hmp);
20030Sstevel@tonic-gate 		hmp = hmp_next;
20040Sstevel@tonic-gate 	}
20050Sstevel@tonic-gate 	return (dlen);
20060Sstevel@tonic-gate }
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate /*
20090Sstevel@tonic-gate  * Update sctp_ftsn to the cumulative TSN from the Forward TSN chunk. Remove
20100Sstevel@tonic-gate  * any SACK gaps less than the newly updated sctp_ftsn. Walk through the
20110Sstevel@tonic-gate  * sid-ssn pair in the Forward TSN and for each, clean the fragment list
20120Sstevel@tonic-gate  * for this pair, if needed, and check if we can deliver subsequent
20130Sstevel@tonic-gate  * messages, if any, from the instream queue (that were waiting for this
20140Sstevel@tonic-gate  * sid-ssn message to show up). Once we are done try to update the SACK
20150Sstevel@tonic-gate  * info. We could get a duplicate Forward TSN, in which case just send
20160Sstevel@tonic-gate  * a SACK. If any of the sid values in the the Forward TSN is invalid,
20170Sstevel@tonic-gate  * send back an "Invalid Stream Identifier" error and continue processing
20180Sstevel@tonic-gate  * the rest.
20190Sstevel@tonic-gate  */
20200Sstevel@tonic-gate static void
20210Sstevel@tonic-gate sctp_process_forward_tsn(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp,
20220Sstevel@tonic-gate     ip6_pkt_t *ipp)
20230Sstevel@tonic-gate {
20240Sstevel@tonic-gate 	uint32_t	*ftsn = (uint32_t *)(ch + 1);
20250Sstevel@tonic-gate 	ftsn_entry_t	*ftsn_entry;
20260Sstevel@tonic-gate 	sctp_instr_t	*instr;
20270Sstevel@tonic-gate 	boolean_t	can_deliver = B_TRUE;
20280Sstevel@tonic-gate 	size_t		dlen;
20290Sstevel@tonic-gate 	int		flen;
20300Sstevel@tonic-gate 	mblk_t		*dmp;
20310Sstevel@tonic-gate 	mblk_t		*pmp;
20320Sstevel@tonic-gate 	sctp_data_hdr_t	*dc;
20330Sstevel@tonic-gate 	ssize_t		remaining;
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	*ftsn = ntohl(*ftsn);
20360Sstevel@tonic-gate 	remaining =  ntohs(ch->sch_len) - sizeof (*ch) - sizeof (*ftsn);
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 	if (SCTP_IS_DETACHED(sctp)) {
20390Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpInClosed);
20400Sstevel@tonic-gate 		can_deliver = B_FALSE;
20410Sstevel@tonic-gate 	}
20420Sstevel@tonic-gate 	/*
20430Sstevel@tonic-gate 	 * un-ordered messages don't have SID-SSN pair entries, we check
20440Sstevel@tonic-gate 	 * for any fragments (for un-ordered message) to be discarded using
20450Sstevel@tonic-gate 	 * the cumulative FTSN.
20460Sstevel@tonic-gate 	 */
20470Sstevel@tonic-gate 	flen = sctp_ftsn_check_uo_frag(sctp, *ftsn);
20480Sstevel@tonic-gate 	if (flen > 0) {
20490Sstevel@tonic-gate 		ASSERT(sctp->sctp_rxqueued >= flen);
20500Sstevel@tonic-gate 		sctp->sctp_rxqueued -= flen;
20510Sstevel@tonic-gate 	}
20520Sstevel@tonic-gate 	ftsn_entry = (ftsn_entry_t *)(ftsn + 1);
20530Sstevel@tonic-gate 	while (remaining >= sizeof (*ftsn_entry)) {
20540Sstevel@tonic-gate 		ftsn_entry->ftsn_sid = ntohs(ftsn_entry->ftsn_sid);
20550Sstevel@tonic-gate 		ftsn_entry->ftsn_ssn = ntohs(ftsn_entry->ftsn_ssn);
20560Sstevel@tonic-gate 		if (ftsn_entry->ftsn_sid >= sctp->sctp_num_istr) {
20570Sstevel@tonic-gate 			uint16_t	inval_parm[2];
20580Sstevel@tonic-gate 			mblk_t		*errmp;
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate 			inval_parm[0] = htons(ftsn_entry->ftsn_sid);
20610Sstevel@tonic-gate 			/* RESERVED to be ignored at the receiving end */
20620Sstevel@tonic-gate 			inval_parm[1] = 0;
20630Sstevel@tonic-gate 			errmp = sctp_make_err(sctp, SCTP_ERR_BAD_SID,
20640Sstevel@tonic-gate 			    (char *)inval_parm, sizeof (inval_parm));
20650Sstevel@tonic-gate 			if (errmp != NULL)
20660Sstevel@tonic-gate 				sctp_send_err(sctp, errmp, NULL);
20670Sstevel@tonic-gate 			ftsn_entry++;
20680Sstevel@tonic-gate 			remaining -= sizeof (*ftsn_entry);
20690Sstevel@tonic-gate 			continue;
20700Sstevel@tonic-gate 		}
20710Sstevel@tonic-gate 		instr = &sctp->sctp_instr[ftsn_entry->ftsn_sid];
20720Sstevel@tonic-gate 		flen = sctp_ftsn_check_frag(sctp, ftsn_entry->ftsn_ssn, instr);
20730Sstevel@tonic-gate 		/* Indicates frags were nuked, update rxqueued */
20740Sstevel@tonic-gate 		if (flen > 0) {
20750Sstevel@tonic-gate 			ASSERT(sctp->sctp_rxqueued >= flen);
20760Sstevel@tonic-gate 			sctp->sctp_rxqueued -= flen;
20770Sstevel@tonic-gate 		}
20780Sstevel@tonic-gate 		/*
20790Sstevel@tonic-gate 		 * It is possible to receive an FTSN chunk with SSN smaller
20800Sstevel@tonic-gate 		 * than then nextseq if this chunk is a retransmission because
20810Sstevel@tonic-gate 		 * of incomplete processing when it was first processed.
20820Sstevel@tonic-gate 		 */
20830Sstevel@tonic-gate 		if (SSN_GE(ftsn_entry->ftsn_ssn, instr->nextseq))
20840Sstevel@tonic-gate 			instr->nextseq = ftsn_entry->ftsn_ssn + 1;
20850Sstevel@tonic-gate 		while (instr->istr_nmsgs > 0) {
20860Sstevel@tonic-gate 			mblk_t	*next;
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 			dmp = (mblk_t *)instr->istr_msgs;
20890Sstevel@tonic-gate 			dc = (sctp_data_hdr_t *)dmp->b_rptr;
20900Sstevel@tonic-gate 			if (ntohs(dc->sdh_ssn) != instr->nextseq)
20910Sstevel@tonic-gate 				break;
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 			next = dmp->b_next;
20940Sstevel@tonic-gate 			dlen = dmp->b_wptr - dmp->b_rptr - sizeof (*dc);
20950Sstevel@tonic-gate 			for (pmp = dmp->b_cont; pmp != NULL;
20960Sstevel@tonic-gate 			    pmp = pmp->b_cont) {
20970Sstevel@tonic-gate 				dlen += pmp->b_wptr - pmp->b_rptr;
20980Sstevel@tonic-gate 			}
20990Sstevel@tonic-gate 			if (can_deliver) {
21000Sstevel@tonic-gate 				int32_t	nrwnd;
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 				dmp->b_rptr = (uchar_t *)(dc + 1);
21030Sstevel@tonic-gate 				dmp->b_next = NULL;
21040Sstevel@tonic-gate 				ASSERT(dmp->b_prev == NULL);
21050Sstevel@tonic-gate 				if (sctp_input_add_ancillary(sctp,
21060Sstevel@tonic-gate 				    &dmp, dc, fp, ipp) == 0) {
21070Sstevel@tonic-gate 					sctp->sctp_rxqueued -= dlen;
21080Sstevel@tonic-gate 					sctp->sctp_rwnd -= dlen;
21090Sstevel@tonic-gate 					nrwnd = sctp->sctp_ulp_recv(
21100Sstevel@tonic-gate 					    sctp->sctp_ulpd, dmp, 0);
21110Sstevel@tonic-gate 					if (nrwnd > sctp->sctp_rwnd)
21120Sstevel@tonic-gate 						sctp->sctp_rwnd = nrwnd;
21130Sstevel@tonic-gate 				} else {
21140Sstevel@tonic-gate 					/*
21150Sstevel@tonic-gate 					 * We will resume processing when
21160Sstevel@tonic-gate 					 * the FTSN chunk is re-xmitted.
21170Sstevel@tonic-gate 					 */
21180Sstevel@tonic-gate 					dmp->b_rptr = (uchar_t *)dc;
21190Sstevel@tonic-gate 					dmp->b_next = next;
21200Sstevel@tonic-gate 					dprint(0,
21210Sstevel@tonic-gate 					    ("FTSN dequeuing %u failed\n",
21220Sstevel@tonic-gate 					    ntohs(dc->sdh_ssn)));
21230Sstevel@tonic-gate 					return;
21240Sstevel@tonic-gate 				}
21250Sstevel@tonic-gate 			} else {
21260Sstevel@tonic-gate 				sctp->sctp_rxqueued -= dlen;
21270Sstevel@tonic-gate 				ASSERT(dmp->b_prev == NULL);
21280Sstevel@tonic-gate 				dmp->b_next = NULL;
21290Sstevel@tonic-gate 				freemsg(dmp);
21300Sstevel@tonic-gate 			}
21310Sstevel@tonic-gate 			instr->istr_nmsgs--;
21320Sstevel@tonic-gate 			instr->nextseq++;
21330Sstevel@tonic-gate 			sctp->sctp_istr_nmsgs--;
21340Sstevel@tonic-gate 			if (next != NULL)
21350Sstevel@tonic-gate 				next->b_prev = NULL;
21360Sstevel@tonic-gate 			instr->istr_msgs = next;
21370Sstevel@tonic-gate 		}
21380Sstevel@tonic-gate 		ftsn_entry++;
21390Sstevel@tonic-gate 		remaining -= sizeof (*ftsn_entry);
21400Sstevel@tonic-gate 	}
21410Sstevel@tonic-gate 	/* Duplicate FTSN */
21420Sstevel@tonic-gate 	if (*ftsn <= (sctp->sctp_ftsn - 1)) {
21430Sstevel@tonic-gate 		sctp->sctp_force_sack = 1;
21440Sstevel@tonic-gate 		return;
21450Sstevel@tonic-gate 	}
21460Sstevel@tonic-gate 	/* Advance cum TSN to that reported in the Forward TSN chunk */
21470Sstevel@tonic-gate 	sctp->sctp_ftsn = *ftsn + 1;
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 	/* Remove all the SACK gaps before the new cum TSN */
21500Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL) {
21510Sstevel@tonic-gate 		sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1,
21520Sstevel@tonic-gate 		    &sctp->sctp_sack_gaps);
21530Sstevel@tonic-gate 	}
21540Sstevel@tonic-gate 	/*
21550Sstevel@tonic-gate 	 * If there are gap reports pending, check if advancing
21560Sstevel@tonic-gate 	 * the ftsn here closes a gap. If so, we can advance
21570Sstevel@tonic-gate 	 * ftsn to the end of the set.
21580Sstevel@tonic-gate 	 * If ftsn has moved forward, maybe we can remove gap reports.
21590Sstevel@tonic-gate 	 */
21600Sstevel@tonic-gate 	if (sctp->sctp_sack_info != NULL &&
21610Sstevel@tonic-gate 	    sctp->sctp_ftsn == sctp->sctp_sack_info->begin) {
21620Sstevel@tonic-gate 		sctp->sctp_ftsn = sctp->sctp_sack_info->end + 1;
21630Sstevel@tonic-gate 		sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1,
21640Sstevel@tonic-gate 		    &sctp->sctp_sack_gaps);
21650Sstevel@tonic-gate 	}
21660Sstevel@tonic-gate }
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate /*
21690Sstevel@tonic-gate  * When we have processed a SACK we check to see if we can advance the
21700Sstevel@tonic-gate  * cumulative TSN if there are abandoned chunks immediately following
21710Sstevel@tonic-gate  * the updated cumulative TSN. If there are, we attempt to send a
21720Sstevel@tonic-gate  * Forward TSN chunk.
21730Sstevel@tonic-gate  */
21740Sstevel@tonic-gate static void
21750Sstevel@tonic-gate sctp_check_abandoned_data(sctp_t *sctp, sctp_faddr_t *fp)
21760Sstevel@tonic-gate {
21770Sstevel@tonic-gate 	mblk_t		*meta = sctp->sctp_xmit_head;
21780Sstevel@tonic-gate 	mblk_t		*mp;
21790Sstevel@tonic-gate 	mblk_t		*nmp;
21800Sstevel@tonic-gate 	uint32_t	seglen;
21810Sstevel@tonic-gate 	uint32_t	adv_pap = sctp->sctp_adv_pap;
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 	/*
21840Sstevel@tonic-gate 	 * We only check in the first meta since otherwise we can't
21850Sstevel@tonic-gate 	 * advance the cumulative ack point. We just look for chunks
21860Sstevel@tonic-gate 	 * marked for retransmission, else we might prematurely
21870Sstevel@tonic-gate 	 * send an FTSN for a sent, but unacked, chunk.
21880Sstevel@tonic-gate 	 */
21890Sstevel@tonic-gate 	for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
21900Sstevel@tonic-gate 		if (!SCTP_CHUNK_ISSENT(mp))
21910Sstevel@tonic-gate 			return;
21920Sstevel@tonic-gate 		if (SCTP_CHUNK_WANT_REXMIT(mp))
21930Sstevel@tonic-gate 			break;
21940Sstevel@tonic-gate 	}
21950Sstevel@tonic-gate 	if (mp == NULL)
21960Sstevel@tonic-gate 		return;
21970Sstevel@tonic-gate 	sctp_check_adv_ack_pt(sctp, meta, mp);
21980Sstevel@tonic-gate 	if (SEQ_GT(sctp->sctp_adv_pap, adv_pap)) {
21990Sstevel@tonic-gate 		sctp_make_ftsns(sctp, meta, mp, &nmp, fp, &seglen);
22000Sstevel@tonic-gate 		if (nmp == NULL) {
22010Sstevel@tonic-gate 			sctp->sctp_adv_pap = adv_pap;
22020Sstevel@tonic-gate 			if (!fp->timer_running)
22030Sstevel@tonic-gate 				SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
22040Sstevel@tonic-gate 			return;
22050Sstevel@tonic-gate 		}
22060Sstevel@tonic-gate 		sctp_set_iplen(sctp, nmp);
22070Sstevel@tonic-gate 		sctp_add_sendq(sctp, nmp);
22080Sstevel@tonic-gate 		if (!fp->timer_running)
22090Sstevel@tonic-gate 			SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
22100Sstevel@tonic-gate 	}
22110Sstevel@tonic-gate }
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate static int
22140Sstevel@tonic-gate sctp_got_sack(sctp_t *sctp, sctp_chunk_hdr_t *sch)
22150Sstevel@tonic-gate {
22160Sstevel@tonic-gate 	sctp_sack_chunk_t	*sc;
22170Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
22180Sstevel@tonic-gate 	sctp_sack_frag_t	*ssf;
22190Sstevel@tonic-gate 	mblk_t			*ump;
22200Sstevel@tonic-gate 	mblk_t			*mp;
22210Sstevel@tonic-gate 	uint32_t		tsn;
22220Sstevel@tonic-gate 	uint32_t		xtsn;
22230Sstevel@tonic-gate 	uint32_t		gapstart;
22240Sstevel@tonic-gate 	uint32_t		gapend;
22250Sstevel@tonic-gate 	uint32_t		acked = 0;
22260Sstevel@tonic-gate 	uint16_t		chunklen;
22270Sstevel@tonic-gate 	sctp_faddr_t		*fp;
22280Sstevel@tonic-gate 	int			num_gaps;
22290Sstevel@tonic-gate 	int			trysend = 0;
22300Sstevel@tonic-gate 	int			i;
22310Sstevel@tonic-gate 	boolean_t		fast_recovery = B_FALSE;
22320Sstevel@tonic-gate 	boolean_t		cumack_forward = B_FALSE;
22330Sstevel@tonic-gate 	boolean_t		fwd_tsn = B_FALSE;
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
22360Sstevel@tonic-gate 	chunklen = ntohs(sch->sch_len);
22370Sstevel@tonic-gate 	if (chunklen < (sizeof (*sch) + sizeof (*sc)))
22380Sstevel@tonic-gate 		return (0);
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate 	sc = (sctp_sack_chunk_t *)(sch + 1);
22410Sstevel@tonic-gate 	tsn = ntohl(sc->ssc_cumtsn);
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 	dprint(2, ("got sack tsn %x -> %x\n", sctp->sctp_lastack_rxd, tsn));
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	/* out of order */
22460Sstevel@tonic-gate 	if (SEQ_LT(tsn, sctp->sctp_lastack_rxd))
22470Sstevel@tonic-gate 		return (0);
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate 	if (SEQ_GT(tsn, sctp->sctp_ltsn - 1)) {
22500Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpInAckUnsent);
22510Sstevel@tonic-gate 		/* funky; don't go beyond our own last assigned TSN */
22520Sstevel@tonic-gate 		tsn = sctp->sctp_ltsn - 1;
22530Sstevel@tonic-gate 	}
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 	/*
22560Sstevel@tonic-gate 	 * Cwnd only done when not in fast recovery mode.
22570Sstevel@tonic-gate 	 */
22580Sstevel@tonic-gate 	if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_recovery_tsn))
22590Sstevel@tonic-gate 		fast_recovery = B_TRUE;
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 	/*
22620Sstevel@tonic-gate 	 * .. and if the cum TSN is not moving ahead on account Forward TSN
22630Sstevel@tonic-gate 	 */
22640Sstevel@tonic-gate 	if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_adv_pap))
22650Sstevel@tonic-gate 		fwd_tsn = B_TRUE;
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	if (tsn == sctp->sctp_lastack_rxd &&
22680Sstevel@tonic-gate 	    (sctp->sctp_xmit_unacked == NULL ||
22690Sstevel@tonic-gate 	    !SCTP_CHUNK_ABANDONED(sctp->sctp_xmit_unacked))) {
22700Sstevel@tonic-gate 		if (sctp->sctp_xmit_unacked != NULL)
22710Sstevel@tonic-gate 			mp = sctp->sctp_xmit_unacked;
22720Sstevel@tonic-gate 		else if (sctp->sctp_xmit_head != NULL)
22730Sstevel@tonic-gate 			mp = sctp->sctp_xmit_head->b_cont;
22740Sstevel@tonic-gate 		else
22750Sstevel@tonic-gate 			mp = NULL;
22760Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpInDupAck);
22770Sstevel@tonic-gate 	} else {
22780Sstevel@tonic-gate 		acked = sctp_cumack(sctp, tsn, &mp);
22790Sstevel@tonic-gate 		sctp->sctp_xmit_unacked = mp;
22800Sstevel@tonic-gate 		if (acked > 0) {
22810Sstevel@tonic-gate 			trysend = 1;
22820Sstevel@tonic-gate 			cumack_forward = B_TRUE;
22830Sstevel@tonic-gate 			if (fwd_tsn && SEQ_GEQ(sctp->sctp_lastack_rxd,
22840Sstevel@tonic-gate 			    sctp->sctp_adv_pap)) {
22850Sstevel@tonic-gate 				cumack_forward = B_FALSE;
22860Sstevel@tonic-gate 			}
22870Sstevel@tonic-gate 		}
22880Sstevel@tonic-gate 	}
22890Sstevel@tonic-gate 	num_gaps = ntohs(sc->ssc_numfrags);
22900Sstevel@tonic-gate 	if (num_gaps == 0 || mp == NULL || !SCTP_CHUNK_ISSENT(mp) ||
22910Sstevel@tonic-gate 	    chunklen < (sizeof (*sch) + sizeof (*sc) +
22920Sstevel@tonic-gate 	    num_gaps * sizeof (*ssf))) {
22930Sstevel@tonic-gate 		goto ret;
22940Sstevel@tonic-gate 	}
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate 	ump = sctp->sctp_xmit_head;
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 	/*
22990Sstevel@tonic-gate 	 * Go through SACK gaps. They are ordered based on start TSN.
23000Sstevel@tonic-gate 	 */
23010Sstevel@tonic-gate 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
23020Sstevel@tonic-gate 	xtsn = ntohl(sdc->sdh_tsn);
23030Sstevel@tonic-gate 	ASSERT(xtsn == tsn + 1);
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 	ssf = (sctp_sack_frag_t *)(sc + 1);
23060Sstevel@tonic-gate 	for (i = 0; i < num_gaps; i++) {
23070Sstevel@tonic-gate 		gapstart = tsn + ntohs(ssf->ssf_start);
23080Sstevel@tonic-gate 		gapend = tsn + ntohs(ssf->ssf_end);
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate 		while (xtsn != gapstart) {
23110Sstevel@tonic-gate 			SCTP_CHUNK_SET_SACKCNT(mp, SCTP_CHUNK_SACKCNT(mp) + 1);
23120Sstevel@tonic-gate 			if (SCTP_CHUNK_SACKCNT(mp) == sctp_fast_rxt_thresh) {
23130Sstevel@tonic-gate 				SCTP_CHUNK_REXMIT(mp);
23140Sstevel@tonic-gate 				sctp->sctp_chk_fast_rexmit = B_TRUE;
23150Sstevel@tonic-gate 				trysend = 1;
23160Sstevel@tonic-gate 				if (!fast_recovery) {
23170Sstevel@tonic-gate 					/*
23180Sstevel@tonic-gate 					 * Entering fast recovery.
23190Sstevel@tonic-gate 					 */
23200Sstevel@tonic-gate 					fp = SCTP_CHUNK_DEST(mp);
23210Sstevel@tonic-gate 					fp->ssthresh = fp->cwnd / 2;
23220Sstevel@tonic-gate 					if (fp->ssthresh < 2 * fp->sfa_pmss) {
23230Sstevel@tonic-gate 						fp->ssthresh =
23240Sstevel@tonic-gate 						    2 * fp->sfa_pmss;
23250Sstevel@tonic-gate 					}
23260Sstevel@tonic-gate 					fp->cwnd = fp->ssthresh;
23270Sstevel@tonic-gate 					fp->pba = 0;
23280Sstevel@tonic-gate 					sctp->sctp_recovery_tsn =
23290Sstevel@tonic-gate 					    sctp->sctp_ltsn - 1;
23300Sstevel@tonic-gate 					fast_recovery = B_TRUE;
23310Sstevel@tonic-gate 				}
23320Sstevel@tonic-gate 			}
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 			/*
23350Sstevel@tonic-gate 			 * Peer may have reneged on this chunk, so un-sack
23360Sstevel@tonic-gate 			 * it now. If the peer did renege, we need to
23370Sstevel@tonic-gate 			 * readjust unacked.
23380Sstevel@tonic-gate 			 */
23390Sstevel@tonic-gate 			if (SCTP_CHUNK_ISACKED(mp)) {
23400Sstevel@tonic-gate 				chunklen = ntohs(sdc->sdh_len);
23410Sstevel@tonic-gate 				fp = SCTP_CHUNK_DEST(mp);
23420Sstevel@tonic-gate 				fp->suna += chunklen;
23430Sstevel@tonic-gate 				sctp->sctp_unacked += chunklen - sizeof (*sdc);
23440Sstevel@tonic-gate 				SCTP_CHUNK_CLEAR_ACKED(mp);
23450Sstevel@tonic-gate 				if (!fp->timer_running) {
23460Sstevel@tonic-gate 					SCTP_FADDR_TIMER_RESTART(sctp, fp,
23470Sstevel@tonic-gate 					    fp->rto);
23480Sstevel@tonic-gate 				}
23490Sstevel@tonic-gate 			}
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 			mp = mp->b_next;
23520Sstevel@tonic-gate 			if (mp == NULL) {
23530Sstevel@tonic-gate 				ump = ump->b_next;
23540Sstevel@tonic-gate 				if (ump == NULL) {
23550Sstevel@tonic-gate 					goto ret;
23560Sstevel@tonic-gate 				}
23570Sstevel@tonic-gate 				mp = ump->b_cont;
23580Sstevel@tonic-gate 			}
23590Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp)) {
23600Sstevel@tonic-gate 				goto ret;
23610Sstevel@tonic-gate 			}
23620Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
23630Sstevel@tonic-gate 			xtsn = ntohl(sdc->sdh_tsn);
23640Sstevel@tonic-gate 		}
23650Sstevel@tonic-gate 		while (SEQ_LEQ(xtsn, gapend)) {
23660Sstevel@tonic-gate 			/*
23670Sstevel@tonic-gate 			 * SACKed
23680Sstevel@tonic-gate 			 */
23690Sstevel@tonic-gate 			SCTP_CHUNK_SET_SACKCNT(mp, 0);
23700Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISACKED(mp)) {
23710Sstevel@tonic-gate 				SCTP_CHUNK_ACKED(mp);
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 				fp = SCTP_CHUNK_DEST(mp);
23740Sstevel@tonic-gate 				chunklen = ntohs(sdc->sdh_len);
23750Sstevel@tonic-gate 				ASSERT(fp->suna >= chunklen);
23760Sstevel@tonic-gate 				fp->suna -= chunklen;
23770Sstevel@tonic-gate 				if (fp->suna == 0) {
23780Sstevel@tonic-gate 					/* All outstanding data acked. */
23790Sstevel@tonic-gate 					fp->pba = 0;
23800Sstevel@tonic-gate 					SCTP_FADDR_TIMER_STOP(fp);
23810Sstevel@tonic-gate 				}
23820Sstevel@tonic-gate 				fp->acked += chunklen;
23830Sstevel@tonic-gate 				acked += chunklen;
23840Sstevel@tonic-gate 				sctp->sctp_unacked -= chunklen - sizeof (*sdc);
23850Sstevel@tonic-gate 				ASSERT(sctp->sctp_unacked >= 0);
23860Sstevel@tonic-gate 			}
23870Sstevel@tonic-gate 			mp = mp->b_next;
23880Sstevel@tonic-gate 			if (mp == NULL) {
23890Sstevel@tonic-gate 				ump = ump->b_next;
23900Sstevel@tonic-gate 				if (ump == NULL) {
23910Sstevel@tonic-gate 					goto ret;
23920Sstevel@tonic-gate 				}
23930Sstevel@tonic-gate 				mp = ump->b_cont;
23940Sstevel@tonic-gate 			}
23950Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp)) {
23960Sstevel@tonic-gate 				goto ret;
23970Sstevel@tonic-gate 			}
23980Sstevel@tonic-gate 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
23990Sstevel@tonic-gate 			xtsn = ntohl(sdc->sdh_tsn);
24000Sstevel@tonic-gate 		}
24010Sstevel@tonic-gate 		ssf++;
24020Sstevel@tonic-gate 	}
24030Sstevel@tonic-gate 	if (sctp->sctp_prsctp_aware)
24040Sstevel@tonic-gate 		sctp_check_abandoned_data(sctp, sctp->sctp_current);
24050Sstevel@tonic-gate 	if (sctp->sctp_chk_fast_rexmit)
24060Sstevel@tonic-gate 		sctp_fast_rexmit(sctp);
24070Sstevel@tonic-gate ret:
24080Sstevel@tonic-gate 	trysend += sctp_set_frwnd(sctp, ntohl(sc->ssc_a_rwnd));
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate 	/*
24110Sstevel@tonic-gate 	 * If receive window is closed while there is unsent data,
24120Sstevel@tonic-gate 	 * set a timer for doing zero window probes.
24130Sstevel@tonic-gate 	 */
24140Sstevel@tonic-gate 	if (sctp->sctp_frwnd == 0 && sctp->sctp_unacked == 0 &&
24150Sstevel@tonic-gate 	    sctp->sctp_unsent != 0) {
24160Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
24170Sstevel@tonic-gate 		    sctp->sctp_current->rto);
24180Sstevel@tonic-gate 	}
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate 	/*
24210Sstevel@tonic-gate 	 * Set cwnd for all destinations.
24220Sstevel@tonic-gate 	 * Congestion window gets increased only when cumulative
24230Sstevel@tonic-gate 	 * TSN moves forward, we're not in fast recovery, and
24240Sstevel@tonic-gate 	 * cwnd has been fully utilized (almost fully, need to allow
24250Sstevel@tonic-gate 	 * some leeway due to non-MSS sized messages).
24260Sstevel@tonic-gate 	 */
24270Sstevel@tonic-gate 	if (sctp->sctp_current->acked == acked) {
24280Sstevel@tonic-gate 		/*
24290Sstevel@tonic-gate 		 * Fast-path, only data sent to sctp_current got acked.
24300Sstevel@tonic-gate 		 */
24310Sstevel@tonic-gate 		fp = sctp->sctp_current;
24320Sstevel@tonic-gate 		if (cumack_forward && !fast_recovery &&
24330Sstevel@tonic-gate 		    (fp->acked + fp->suna > fp->cwnd - fp->sfa_pmss)) {
24340Sstevel@tonic-gate 			if (fp->cwnd < fp->ssthresh) {
24350Sstevel@tonic-gate 				/*
24360Sstevel@tonic-gate 				 * Slow start
24370Sstevel@tonic-gate 				 */
24380Sstevel@tonic-gate 				if (fp->acked > fp->sfa_pmss) {
24390Sstevel@tonic-gate 					fp->cwnd += fp->sfa_pmss;
24400Sstevel@tonic-gate 				} else {
24410Sstevel@tonic-gate 					fp->cwnd += fp->acked;
24420Sstevel@tonic-gate 				}
24430Sstevel@tonic-gate 				fp->cwnd = MIN(fp->cwnd, sctp->sctp_cwnd_max);
24440Sstevel@tonic-gate 			} else {
24450Sstevel@tonic-gate 				/*
24460Sstevel@tonic-gate 				 * Congestion avoidance
24470Sstevel@tonic-gate 				 */
24480Sstevel@tonic-gate 				fp->pba += fp->acked;
24490Sstevel@tonic-gate 				if (fp->pba >= fp->cwnd) {
24500Sstevel@tonic-gate 					fp->pba -= fp->cwnd;
24510Sstevel@tonic-gate 					fp->cwnd += fp->sfa_pmss;
24520Sstevel@tonic-gate 					fp->cwnd = MIN(fp->cwnd,
24530Sstevel@tonic-gate 					    sctp->sctp_cwnd_max);
24540Sstevel@tonic-gate 				}
24550Sstevel@tonic-gate 			}
24560Sstevel@tonic-gate 		}
24570Sstevel@tonic-gate 		/*
24580Sstevel@tonic-gate 		 * Limit the burst of transmitted data segments.
24590Sstevel@tonic-gate 		 */
24600Sstevel@tonic-gate 		if (fp->suna + sctp_maxburst * fp->sfa_pmss < fp->cwnd) {
24610Sstevel@tonic-gate 			fp->cwnd = fp->suna + sctp_maxburst * fp->sfa_pmss;
24620Sstevel@tonic-gate 		}
24630Sstevel@tonic-gate 		fp->acked = 0;
24640Sstevel@tonic-gate 		return (trysend);
24650Sstevel@tonic-gate 	}
24660Sstevel@tonic-gate 	for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
24670Sstevel@tonic-gate 		if (cumack_forward && fp->acked && !fast_recovery &&
24680Sstevel@tonic-gate 		    (fp->acked + fp->suna > fp->cwnd - fp->sfa_pmss)) {
24690Sstevel@tonic-gate 			if (fp->cwnd < fp->ssthresh) {
24700Sstevel@tonic-gate 				if (fp->acked > fp->sfa_pmss) {
24710Sstevel@tonic-gate 					fp->cwnd += fp->sfa_pmss;
24720Sstevel@tonic-gate 				} else {
24730Sstevel@tonic-gate 					fp->cwnd += fp->acked;
24740Sstevel@tonic-gate 				}
24750Sstevel@tonic-gate 				fp->cwnd = MIN(fp->cwnd, sctp->sctp_cwnd_max);
24760Sstevel@tonic-gate 			} else {
24770Sstevel@tonic-gate 				fp->pba += fp->acked;
24780Sstevel@tonic-gate 				if (fp->pba >= fp->cwnd) {
24790Sstevel@tonic-gate 					fp->pba -= fp->cwnd;
24800Sstevel@tonic-gate 					fp->cwnd += fp->sfa_pmss;
24810Sstevel@tonic-gate 					fp->cwnd = MIN(fp->cwnd,
24820Sstevel@tonic-gate 					    sctp->sctp_cwnd_max);
24830Sstevel@tonic-gate 				}
24840Sstevel@tonic-gate 			}
24850Sstevel@tonic-gate 		}
24860Sstevel@tonic-gate 		if (fp->suna + sctp_maxburst * fp->sfa_pmss < fp->cwnd) {
24870Sstevel@tonic-gate 			fp->cwnd = fp->suna + sctp_maxburst * fp->sfa_pmss;
24880Sstevel@tonic-gate 		}
24890Sstevel@tonic-gate 		fp->acked = 0;
24900Sstevel@tonic-gate 	}
24910Sstevel@tonic-gate 	return (trysend);
24920Sstevel@tonic-gate }
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate /*
24950Sstevel@tonic-gate  * Returns 0 if the caller should stop processing any more chunks,
24960Sstevel@tonic-gate  * 1 if the caller should skip this chunk and continue processing.
24970Sstevel@tonic-gate  */
24980Sstevel@tonic-gate static int
24990Sstevel@tonic-gate sctp_strange_chunk(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp)
25000Sstevel@tonic-gate {
25010Sstevel@tonic-gate 	mblk_t *errmp;
25020Sstevel@tonic-gate 	size_t len;
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
25050Sstevel@tonic-gate 	/* check top two bits for action required */
25060Sstevel@tonic-gate 	if (ch->sch_id & 0x40) {	/* also matches 0xc0 */
25070Sstevel@tonic-gate 		len = ntohs(ch->sch_len);
25080Sstevel@tonic-gate 		errmp = sctp_make_err(sctp, SCTP_ERR_UNREC_CHUNK, ch, len);
25090Sstevel@tonic-gate 		if (errmp != NULL)
25100Sstevel@tonic-gate 			sctp_send_err(sctp, errmp, fp);
25110Sstevel@tonic-gate 		if ((ch->sch_id & 0xc0) == 0xc0) {
25120Sstevel@tonic-gate 			/* skip and continue */
25130Sstevel@tonic-gate 			return (1);
25140Sstevel@tonic-gate 		} else {
25150Sstevel@tonic-gate 			/* stop processing */
25160Sstevel@tonic-gate 			return (0);
25170Sstevel@tonic-gate 		}
25180Sstevel@tonic-gate 	}
25190Sstevel@tonic-gate 	if (ch->sch_id & 0x80) {
25200Sstevel@tonic-gate 		/* skip and continue, no error */
25210Sstevel@tonic-gate 		return (1);
25220Sstevel@tonic-gate 	}
25230Sstevel@tonic-gate 	/* top two bits are clear; stop processing and no error */
25240Sstevel@tonic-gate 	return (0);
25250Sstevel@tonic-gate }
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate /*
25280Sstevel@tonic-gate  * Basic sanity checks on all input chunks and parameters: they must
25290Sstevel@tonic-gate  * be of legitimate size for their purported type, and must follow
25300Sstevel@tonic-gate  * ordering conventions as defined in rfc2960.
25310Sstevel@tonic-gate  *
25320Sstevel@tonic-gate  * Returns 1 if the chunk and all encloded params are legitimate,
25330Sstevel@tonic-gate  * 0 otherwise.
25340Sstevel@tonic-gate  */
25350Sstevel@tonic-gate /*ARGSUSED*/
25360Sstevel@tonic-gate static int
25370Sstevel@tonic-gate sctp_check_input(sctp_t *sctp, sctp_chunk_hdr_t *ch, ssize_t len, int first)
25380Sstevel@tonic-gate {
25390Sstevel@tonic-gate 	sctp_parm_hdr_t	*ph;
25400Sstevel@tonic-gate 	void		*p = NULL;
25410Sstevel@tonic-gate 	ssize_t		clen;
25420Sstevel@tonic-gate 	uint16_t	ch_len;
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate 	ch_len = ntohs(ch->sch_len);
25450Sstevel@tonic-gate 	if (ch_len > len) {
25460Sstevel@tonic-gate 		return (0);
25470Sstevel@tonic-gate 	}
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 	switch (ch->sch_id) {
25500Sstevel@tonic-gate 	case CHUNK_DATA:
25510Sstevel@tonic-gate 		if (ch_len < sizeof (sctp_data_hdr_t)) {
25520Sstevel@tonic-gate 			return (0);
25530Sstevel@tonic-gate 		}
25540Sstevel@tonic-gate 		return (1);
25550Sstevel@tonic-gate 	case CHUNK_INIT:
25560Sstevel@tonic-gate 	case CHUNK_INIT_ACK:
25570Sstevel@tonic-gate 		{
25580Sstevel@tonic-gate 			ssize_t	remlen = len;
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate 			/*
25610Sstevel@tonic-gate 			 * INIT and INIT-ACK chunks must not be bundled with
25620Sstevel@tonic-gate 			 * any other.
25630Sstevel@tonic-gate 			 */
25640Sstevel@tonic-gate 			if (!first || sctp_next_chunk(ch, &remlen) != NULL ||
25650Sstevel@tonic-gate 			    (ch_len < (sizeof (*ch) +
25660Sstevel@tonic-gate 			    sizeof (sctp_init_chunk_t)))) {
25670Sstevel@tonic-gate 				return (0);
25680Sstevel@tonic-gate 			}
25690Sstevel@tonic-gate 			/* may have params that need checking */
25700Sstevel@tonic-gate 			p = (char *)(ch + 1) + sizeof (sctp_init_chunk_t);
25710Sstevel@tonic-gate 			clen = ch_len - (sizeof (*ch) +
25720Sstevel@tonic-gate 			    sizeof (sctp_init_chunk_t));
25730Sstevel@tonic-gate 		}
25740Sstevel@tonic-gate 		break;
25750Sstevel@tonic-gate 	case CHUNK_SACK:
25760Sstevel@tonic-gate 		if (ch_len < (sizeof (*ch) + sizeof (sctp_sack_chunk_t))) {
25770Sstevel@tonic-gate 			return (0);
25780Sstevel@tonic-gate 		}
25790Sstevel@tonic-gate 		/* dup and gap reports checked by got_sack() */
25800Sstevel@tonic-gate 		return (1);
25810Sstevel@tonic-gate 	case CHUNK_SHUTDOWN:
25820Sstevel@tonic-gate 		if (ch_len < (sizeof (*ch) + sizeof (uint32_t))) {
25830Sstevel@tonic-gate 			return (0);
25840Sstevel@tonic-gate 		}
25850Sstevel@tonic-gate 		return (1);
25860Sstevel@tonic-gate 	case CHUNK_ABORT:
25870Sstevel@tonic-gate 	case CHUNK_ERROR:
25880Sstevel@tonic-gate 		if (ch_len < sizeof (*ch)) {
25890Sstevel@tonic-gate 			return (0);
25900Sstevel@tonic-gate 		}
25910Sstevel@tonic-gate 		/* may have params that need checking */
25920Sstevel@tonic-gate 		p = ch + 1;
25930Sstevel@tonic-gate 		clen = ch_len - sizeof (*ch);
25940Sstevel@tonic-gate 		break;
25950Sstevel@tonic-gate 	case CHUNK_ECNE:
25960Sstevel@tonic-gate 	case CHUNK_CWR:
25970Sstevel@tonic-gate 	case CHUNK_HEARTBEAT:
25980Sstevel@tonic-gate 	case CHUNK_HEARTBEAT_ACK:
25990Sstevel@tonic-gate 	/* Full ASCONF chunk and parameter checks are in asconf.c */
26000Sstevel@tonic-gate 	case CHUNK_ASCONF:
26010Sstevel@tonic-gate 	case CHUNK_ASCONF_ACK:
26020Sstevel@tonic-gate 		if (ch_len < sizeof (*ch)) {
26030Sstevel@tonic-gate 			return (0);
26040Sstevel@tonic-gate 		}
26050Sstevel@tonic-gate 		/* heartbeat data checked by process_heartbeat() */
26060Sstevel@tonic-gate 		return (1);
26070Sstevel@tonic-gate 	case CHUNK_SHUTDOWN_COMPLETE:
26080Sstevel@tonic-gate 		{
26090Sstevel@tonic-gate 			ssize_t remlen = len;
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 			/*
26120Sstevel@tonic-gate 			 * SHUTDOWN-COMPLETE chunk must not be bundled with any
26130Sstevel@tonic-gate 			 * other
26140Sstevel@tonic-gate 			 */
26150Sstevel@tonic-gate 			if (!first || sctp_next_chunk(ch, &remlen) != NULL ||
26160Sstevel@tonic-gate 			    ch_len < sizeof (*ch)) {
26170Sstevel@tonic-gate 				return (0);
26180Sstevel@tonic-gate 			}
26190Sstevel@tonic-gate 		}
26200Sstevel@tonic-gate 		return (1);
26210Sstevel@tonic-gate 	case CHUNK_COOKIE:
26220Sstevel@tonic-gate 	case CHUNK_COOKIE_ACK:
26230Sstevel@tonic-gate 	case CHUNK_SHUTDOWN_ACK:
26240Sstevel@tonic-gate 		if (ch_len < sizeof (*ch) || !first) {
26250Sstevel@tonic-gate 			return (0);
26260Sstevel@tonic-gate 		}
26270Sstevel@tonic-gate 		return (1);
26280Sstevel@tonic-gate 	case CHUNK_FORWARD_TSN:
26290Sstevel@tonic-gate 		if (ch_len < (sizeof (*ch) + sizeof (uint32_t)))
26300Sstevel@tonic-gate 			return (0);
26310Sstevel@tonic-gate 		return (1);
26320Sstevel@tonic-gate 	default:
26330Sstevel@tonic-gate 		return (1);	/* handled by strange_chunk() */
26340Sstevel@tonic-gate 	}
26350Sstevel@tonic-gate 
26360Sstevel@tonic-gate 	/* check and byteorder parameters */
26370Sstevel@tonic-gate 	if (clen <= 0) {
26380Sstevel@tonic-gate 		return (1);
26390Sstevel@tonic-gate 	}
26400Sstevel@tonic-gate 	ASSERT(p != NULL);
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	ph = p;
26430Sstevel@tonic-gate 	while (ph != NULL && clen > 0) {
26440Sstevel@tonic-gate 		ch_len = ntohs(ph->sph_len);
26450Sstevel@tonic-gate 		if (ch_len > len || ch_len < sizeof (*ph)) {
26460Sstevel@tonic-gate 			return (0);
26470Sstevel@tonic-gate 		}
26480Sstevel@tonic-gate 		ph = sctp_next_parm(ph, &clen);
26490Sstevel@tonic-gate 	}
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate 	/* All OK */
26520Sstevel@tonic-gate 	return (1);
26530Sstevel@tonic-gate }
26540Sstevel@tonic-gate 
26550Sstevel@tonic-gate /* ARGSUSED */
26560Sstevel@tonic-gate static sctp_hdr_t *
26570Sstevel@tonic-gate find_sctp_hdrs(mblk_t *mp, in6_addr_t *src, in6_addr_t *dst,
26580Sstevel@tonic-gate     uint_t *ifindex, uint_t *ip_hdr_len, ip6_pkt_t *ipp, in_pktinfo_t *pinfo)
26590Sstevel@tonic-gate {
26600Sstevel@tonic-gate 	uchar_t	*rptr;
26610Sstevel@tonic-gate 	ipha_t	*ip4h;
26620Sstevel@tonic-gate 	ip6_t	*ip6h;
26630Sstevel@tonic-gate 	mblk_t	*mp1;
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 	rptr = mp->b_rptr;
26660Sstevel@tonic-gate 	if (IPH_HDR_VERSION(rptr) == IPV4_VERSION) {
26670Sstevel@tonic-gate 		*ip_hdr_len = IPH_HDR_LENGTH(rptr);
26680Sstevel@tonic-gate 		ip4h = (ipha_t *)rptr;
26690Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(ip4h->ipha_src, src);
26700Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(ip4h->ipha_dst, dst);
26710Sstevel@tonic-gate 
26720Sstevel@tonic-gate 		ipp->ipp_fields |= IPPF_HOPLIMIT;
26730Sstevel@tonic-gate 		ipp->ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl;
26740Sstevel@tonic-gate 		if (pinfo != NULL && (pinfo->in_pkt_flags & IPF_RECVIF)) {
26750Sstevel@tonic-gate 			ipp->ipp_fields |= IPPF_IFINDEX;
26760Sstevel@tonic-gate 			ipp->ipp_ifindex = pinfo->in_pkt_ifindex;
26770Sstevel@tonic-gate 		}
26780Sstevel@tonic-gate 	} else {
26790Sstevel@tonic-gate 		ASSERT(IPH_HDR_VERSION(rptr) == IPV6_VERSION);
26800Sstevel@tonic-gate 		ip6h = (ip6_t *)rptr;
26810Sstevel@tonic-gate 		ipp->ipp_fields = IPPF_HOPLIMIT;
26820Sstevel@tonic-gate 		ipp->ipp_hoplimit = ip6h->ip6_hops;
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate 		if (ip6h->ip6_nxt != IPPROTO_SCTP) {
26850Sstevel@tonic-gate 			/* Look for ifindex information */
26860Sstevel@tonic-gate 			if (ip6h->ip6_nxt == IPPROTO_RAW) {
26870Sstevel@tonic-gate 				ip6i_t *ip6i = (ip6i_t *)ip6h;
26880Sstevel@tonic-gate 
26890Sstevel@tonic-gate 				if (ip6i->ip6i_flags & IP6I_IFINDEX) {
26900Sstevel@tonic-gate 					ASSERT(ip6i->ip6i_ifindex != 0);
26910Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_IFINDEX;
26920Sstevel@tonic-gate 					ipp->ipp_ifindex = ip6i->ip6i_ifindex;
26930Sstevel@tonic-gate 				}
26940Sstevel@tonic-gate 				rptr = (uchar_t *)&ip6i[1];
26950Sstevel@tonic-gate 				mp->b_rptr = rptr;
26960Sstevel@tonic-gate 				if (rptr == mp->b_wptr) {
26970Sstevel@tonic-gate 					mp1 = mp->b_cont;
26980Sstevel@tonic-gate 					freeb(mp);
26990Sstevel@tonic-gate 					mp = mp1;
27000Sstevel@tonic-gate 					rptr = mp->b_rptr;
27010Sstevel@tonic-gate 				}
27020Sstevel@tonic-gate 				ASSERT(mp->b_wptr - rptr >=
27030Sstevel@tonic-gate 				    IPV6_HDR_LEN + sizeof (sctp_hdr_t));
27040Sstevel@tonic-gate 				ip6h = (ip6_t *)rptr;
27050Sstevel@tonic-gate 			}
27060Sstevel@tonic-gate 			/*
27070Sstevel@tonic-gate 			 * Find any potentially interesting extension headers
27080Sstevel@tonic-gate 			 * as well as the length of the IPv6 + extension
27090Sstevel@tonic-gate 			 * headers.
27100Sstevel@tonic-gate 			 */
27110Sstevel@tonic-gate 			*ip_hdr_len = ip_find_hdr_v6(mp, ip6h, ipp, NULL);
27120Sstevel@tonic-gate 		} else {
27130Sstevel@tonic-gate 			*ip_hdr_len = IPV6_HDR_LEN;
27140Sstevel@tonic-gate 		}
27150Sstevel@tonic-gate 		*src = ip6h->ip6_src;
27160Sstevel@tonic-gate 		*dst = ip6h->ip6_dst;
27170Sstevel@tonic-gate 	}
27180Sstevel@tonic-gate 	ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX);
27190Sstevel@tonic-gate 	return ((sctp_hdr_t *)&rptr[*ip_hdr_len]);
27200Sstevel@tonic-gate #undef IPVER
27210Sstevel@tonic-gate }
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate static mblk_t *
27240Sstevel@tonic-gate sctp_check_in_policy(mblk_t *mp, mblk_t *ipsec_mp)
27250Sstevel@tonic-gate {
27260Sstevel@tonic-gate 	ipsec_in_t *ii;
27270Sstevel@tonic-gate 	boolean_t check = B_TRUE;
27280Sstevel@tonic-gate 	boolean_t policy_present;
27290Sstevel@tonic-gate 	ipha_t *ipha;
27300Sstevel@tonic-gate 	ip6_t *ip6h;
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 	ii = (ipsec_in_t *)ipsec_mp->b_rptr;
27330Sstevel@tonic-gate 	ASSERT(ii->ipsec_in_type == IPSEC_IN);
27340Sstevel@tonic-gate 	if (ii->ipsec_in_dont_check) {
27350Sstevel@tonic-gate 		check = B_FALSE;
27360Sstevel@tonic-gate 		if (!ii->ipsec_in_secure) {
27370Sstevel@tonic-gate 			freeb(ipsec_mp);
27380Sstevel@tonic-gate 			ipsec_mp = NULL;
27390Sstevel@tonic-gate 		}
27400Sstevel@tonic-gate 	}
27410Sstevel@tonic-gate 	if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
27420Sstevel@tonic-gate 		policy_present = ipsec_inbound_v4_policy_present;
27430Sstevel@tonic-gate 		ipha = (ipha_t *)mp->b_rptr;
27440Sstevel@tonic-gate 		ip6h = NULL;
27450Sstevel@tonic-gate 	} else {
27460Sstevel@tonic-gate 		policy_present = ipsec_inbound_v6_policy_present;
27470Sstevel@tonic-gate 		ipha = NULL;
27480Sstevel@tonic-gate 		ip6h = (ip6_t *)mp->b_rptr;
27490Sstevel@tonic-gate 	}
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate 	if (check && policy_present) {
27520Sstevel@tonic-gate 		/*
27530Sstevel@tonic-gate 		 * The conn_t parameter is NULL because we already know
27540Sstevel@tonic-gate 		 * nobody's home.
27550Sstevel@tonic-gate 		 */
27560Sstevel@tonic-gate 		ipsec_mp = ipsec_check_global_policy(ipsec_mp, (conn_t *)NULL,
27570Sstevel@tonic-gate 		    ipha, ip6h, B_TRUE);
27580Sstevel@tonic-gate 		if (ipsec_mp == NULL)
27590Sstevel@tonic-gate 			return (NULL);
27600Sstevel@tonic-gate 	}
27610Sstevel@tonic-gate 	if (ipsec_mp != NULL)
27620Sstevel@tonic-gate 		freeb(ipsec_mp);
27630Sstevel@tonic-gate 	return (mp);
27640Sstevel@tonic-gate }
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate /* Handle out-of-the-blue packets */
27670Sstevel@tonic-gate void
27680Sstevel@tonic-gate sctp_ootb_input(mblk_t *mp, ill_t *recv_ill, uint_t ipif_seqid,
27690Sstevel@tonic-gate     zoneid_t zoneid, boolean_t mctl_present)
27700Sstevel@tonic-gate {
27710Sstevel@tonic-gate 	sctp_t			*sctp;
27720Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ch;
27730Sstevel@tonic-gate 	sctp_hdr_t		*sctph;
27740Sstevel@tonic-gate 	in6_addr_t		src, dst;
27750Sstevel@tonic-gate 	uint_t			ip_hdr_len;
27760Sstevel@tonic-gate 	uint_t			ifindex;
27770Sstevel@tonic-gate 	ip6_pkt_t		ipp;
27780Sstevel@tonic-gate 	ssize_t			mlen;
27790Sstevel@tonic-gate 	in_pktinfo_t		*pinfo = NULL;
27800Sstevel@tonic-gate 	mblk_t			*first_mp;
27810Sstevel@tonic-gate 
27820Sstevel@tonic-gate 	BUMP_MIB(&sctp_mib, sctpOutOfBlue);
27830Sstevel@tonic-gate 	BUMP_MIB(&sctp_mib, sctpInSCTPPkts);
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 	first_mp = mp;
27860Sstevel@tonic-gate 	if (mctl_present)
27870Sstevel@tonic-gate 		mp = mp->b_cont;
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate 	/* Initiate IPPf processing, if needed. */
27900Sstevel@tonic-gate 	if (IPP_ENABLED(IPP_LOCAL_IN)) {
27910Sstevel@tonic-gate 		ip_process(IPP_LOCAL_IN, &mp,
27920Sstevel@tonic-gate 		    recv_ill->ill_phyint->phyint_ifindex);
27930Sstevel@tonic-gate 		if (mp == NULL) {
27940Sstevel@tonic-gate 			if (mctl_present)
27950Sstevel@tonic-gate 				freeb(first_mp);
27960Sstevel@tonic-gate 			return;
27970Sstevel@tonic-gate 		}
27980Sstevel@tonic-gate 	}
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 	if (mp->b_cont != NULL) {
28010Sstevel@tonic-gate 		/*
28020Sstevel@tonic-gate 		 * All subsequent code is vastly simplified if it can
28030Sstevel@tonic-gate 		 * assume a single contiguous chunk of data.
28040Sstevel@tonic-gate 		 */
28050Sstevel@tonic-gate 		if (pullupmsg(mp, -1) == 0) {
28060Sstevel@tonic-gate 			BUMP_MIB(&ip_mib, ipInDiscards);
28070Sstevel@tonic-gate 			freemsg(first_mp);
28080Sstevel@tonic-gate 			return;
28090Sstevel@tonic-gate 		}
28100Sstevel@tonic-gate 	}
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate 	/*
28130Sstevel@tonic-gate 	 * We don't really need to call this function...  Need to
28140Sstevel@tonic-gate 	 * optimize later.
28150Sstevel@tonic-gate 	 */
28160Sstevel@tonic-gate 	sctph = find_sctp_hdrs(mp, &src, &dst, &ifindex, &ip_hdr_len,
28170Sstevel@tonic-gate 	    &ipp, pinfo);
28180Sstevel@tonic-gate 	mlen = mp->b_wptr - (uchar_t *)(sctph + 1);
28190Sstevel@tonic-gate 	if ((ch = sctp_first_chunk((uchar_t *)(sctph + 1), mlen)) == NULL) {
28200Sstevel@tonic-gate 		dprint(3, ("sctp_ootb_input: invalid packet\n"));
28210Sstevel@tonic-gate 		BUMP_MIB(&ip_mib, ipInDiscards);
28220Sstevel@tonic-gate 		freemsg(first_mp);
28230Sstevel@tonic-gate 		return;
28240Sstevel@tonic-gate 	}
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate 	switch (ch->sch_id) {
28270Sstevel@tonic-gate 	case CHUNK_INIT:
28280Sstevel@tonic-gate 		/* no listener; send abort  */
28290Sstevel@tonic-gate 		if (mctl_present && sctp_check_in_policy(mp, first_mp) == NULL)
28300Sstevel@tonic-gate 			return;
28310Sstevel@tonic-gate 		sctp_send_abort(gsctp, sctp_init2vtag(ch), 0,
28320Sstevel@tonic-gate 		    NULL, 0, mp, 0, B_TRUE);
28330Sstevel@tonic-gate 		break;
28340Sstevel@tonic-gate 	case CHUNK_INIT_ACK:
28350Sstevel@tonic-gate 		/* check for changed src addr */
28360Sstevel@tonic-gate 		sctp = sctp_addrlist2sctp(mp, sctph, ch, ipif_seqid, zoneid);
28370Sstevel@tonic-gate 		if (sctp != NULL) {
28380Sstevel@tonic-gate 			/* success; proceed to normal path */
28390Sstevel@tonic-gate 			mutex_enter(&sctp->sctp_lock);
28400Sstevel@tonic-gate 			if (sctp->sctp_running) {
28410Sstevel@tonic-gate 				if (!sctp_add_recvq(sctp, mp, B_FALSE)) {
28420Sstevel@tonic-gate 					BUMP_MIB(&ip_mib, ipInDiscards);
28430Sstevel@tonic-gate 					freemsg(mp);
28440Sstevel@tonic-gate 				}
28450Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_lock);
28460Sstevel@tonic-gate 			} else {
28470Sstevel@tonic-gate 				/*
28480Sstevel@tonic-gate 				 * If the source address is changed, we
28490Sstevel@tonic-gate 				 * don't need to worry too much about
28500Sstevel@tonic-gate 				 * out of order processing.  So we don't
28510Sstevel@tonic-gate 				 * check if the recvq is empty or not here.
28520Sstevel@tonic-gate 				 */
28530Sstevel@tonic-gate 				sctp->sctp_running = B_TRUE;
28540Sstevel@tonic-gate 				mutex_exit(&sctp->sctp_lock);
28550Sstevel@tonic-gate 				sctp_input_data(sctp, mp, NULL);
28560Sstevel@tonic-gate 				WAKE_SCTP(sctp);
28570Sstevel@tonic-gate 				sctp_process_sendq(sctp);
28580Sstevel@tonic-gate 			}
28590Sstevel@tonic-gate 			SCTP_REFRELE(sctp);
28600Sstevel@tonic-gate 			return;
28610Sstevel@tonic-gate 		}
28620Sstevel@tonic-gate 		if (mctl_present)
28630Sstevel@tonic-gate 			freeb(first_mp);
28640Sstevel@tonic-gate 		/* else bogus init ack; drop it */
28650Sstevel@tonic-gate 		break;
28660Sstevel@tonic-gate 	case CHUNK_SHUTDOWN_ACK:
28670Sstevel@tonic-gate 		if (mctl_present && sctp_check_in_policy(mp, first_mp) == NULL)
28680Sstevel@tonic-gate 			return;
28690Sstevel@tonic-gate 		sctp_ootb_shutdown_ack(gsctp, mp, ip_hdr_len);
28700Sstevel@tonic-gate 		sctp_process_sendq(gsctp);
28710Sstevel@tonic-gate 		return;
28720Sstevel@tonic-gate 	case CHUNK_ERROR:
28730Sstevel@tonic-gate 	case CHUNK_ABORT:
28740Sstevel@tonic-gate 	case CHUNK_COOKIE_ACK:
28750Sstevel@tonic-gate 	case CHUNK_SHUTDOWN_COMPLETE:
28760Sstevel@tonic-gate 		if (mctl_present)
28770Sstevel@tonic-gate 			freeb(first_mp);
28780Sstevel@tonic-gate 		break;
28790Sstevel@tonic-gate 	default:
28800Sstevel@tonic-gate 		if (mctl_present && sctp_check_in_policy(mp, first_mp) == NULL)
28810Sstevel@tonic-gate 			return;
28820Sstevel@tonic-gate 		sctp_send_abort(gsctp, sctph->sh_verf, 0, NULL, 0, mp, 0,
28830Sstevel@tonic-gate 		    B_TRUE);
28840Sstevel@tonic-gate 		break;
28850Sstevel@tonic-gate 	}
28860Sstevel@tonic-gate 	sctp_process_sendq(gsctp);
28870Sstevel@tonic-gate 	freemsg(mp);
28880Sstevel@tonic-gate }
28890Sstevel@tonic-gate 
28900Sstevel@tonic-gate void
28910Sstevel@tonic-gate sctp_input(conn_t *connp, ipha_t *ipha, mblk_t *mp, mblk_t *first_mp,
28920Sstevel@tonic-gate     ill_t *recv_ill, boolean_t isv4, boolean_t mctl_present)
28930Sstevel@tonic-gate {
28940Sstevel@tonic-gate 	sctp_t *sctp = CONN2SCTP(connp);
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate 	/*
28970Sstevel@tonic-gate 	 * We check some fields in conn_t without holding a lock.
28980Sstevel@tonic-gate 	 * This should be fine.
28990Sstevel@tonic-gate 	 */
29000Sstevel@tonic-gate 	if (CONN_INBOUND_POLICY_PRESENT(connp) || mctl_present) {
29010Sstevel@tonic-gate 		first_mp = ipsec_check_inbound_policy(first_mp, connp,
29020Sstevel@tonic-gate 		    ipha, NULL, mctl_present);
29030Sstevel@tonic-gate 		if (first_mp == NULL) {
29040Sstevel@tonic-gate 			SCTP_REFRELE(sctp);
29050Sstevel@tonic-gate 			return;
29060Sstevel@tonic-gate 		}
29070Sstevel@tonic-gate 	}
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 	/* Initiate IPPF processing for fastpath */
29100Sstevel@tonic-gate 	if (IPP_ENABLED(IPP_LOCAL_IN)) {
29110Sstevel@tonic-gate 		ip_process(IPP_LOCAL_IN, &mp,
29120Sstevel@tonic-gate 		    recv_ill->ill_phyint->phyint_ifindex);
29130Sstevel@tonic-gate 		if (mp == NULL) {
29140Sstevel@tonic-gate 			SCTP_REFRELE(sctp);
29150Sstevel@tonic-gate 			if (mctl_present)
29160Sstevel@tonic-gate 				freeb(first_mp);
29170Sstevel@tonic-gate 			return;
29180Sstevel@tonic-gate 		} else if (mctl_present) {
29190Sstevel@tonic-gate 			/*
29200Sstevel@tonic-gate 			 * ip_process might return a new mp.
29210Sstevel@tonic-gate 			 */
29220Sstevel@tonic-gate 			ASSERT(first_mp != mp);
29230Sstevel@tonic-gate 			first_mp->b_cont = mp;
29240Sstevel@tonic-gate 		} else {
29250Sstevel@tonic-gate 			first_mp = mp;
29260Sstevel@tonic-gate 		}
29270Sstevel@tonic-gate 	}
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 	if (connp->conn_recvif || connp->conn_recvslla ||
29300Sstevel@tonic-gate 	    connp->conn_ipv6_recvpktinfo) {
29310Sstevel@tonic-gate 		int in_flags = 0;
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate 		if (connp->conn_recvif || connp->conn_ipv6_recvpktinfo) {
29340Sstevel@tonic-gate 			in_flags = IPF_RECVIF;
29350Sstevel@tonic-gate 		}
29360Sstevel@tonic-gate 		if (connp->conn_recvslla) {
29370Sstevel@tonic-gate 			in_flags |= IPF_RECVSLLA;
29380Sstevel@tonic-gate 		}
29390Sstevel@tonic-gate 		if (isv4) {
29400Sstevel@tonic-gate 			mp = ip_add_info(mp, recv_ill, in_flags);
29410Sstevel@tonic-gate 		} else {
29420Sstevel@tonic-gate 			mp = ip_add_info_v6(mp, recv_ill,
29430Sstevel@tonic-gate 			    &(((ip6_t *)ipha)->ip6_dst));
29440Sstevel@tonic-gate 		}
29450Sstevel@tonic-gate 		if (mp == NULL) {
29460Sstevel@tonic-gate 			SCTP_REFRELE(sctp);
29470Sstevel@tonic-gate 			if (mctl_present)
29480Sstevel@tonic-gate 				freeb(first_mp);
29490Sstevel@tonic-gate 			return;
29500Sstevel@tonic-gate 		} else if (mctl_present) {
29510Sstevel@tonic-gate 			/*
29520Sstevel@tonic-gate 			 * ip_add_info might return a new mp.
29530Sstevel@tonic-gate 			 */
29540Sstevel@tonic-gate 			ASSERT(first_mp != mp);
29550Sstevel@tonic-gate 			first_mp->b_cont = mp;
29560Sstevel@tonic-gate 		} else {
29570Sstevel@tonic-gate 			first_mp = mp;
29580Sstevel@tonic-gate 		}
29590Sstevel@tonic-gate 	}
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate 	mutex_enter(&sctp->sctp_lock);
29620Sstevel@tonic-gate 	if (sctp->sctp_running) {
29630Sstevel@tonic-gate 		if (mctl_present)
29640Sstevel@tonic-gate 			mp->b_prev = first_mp;
29650Sstevel@tonic-gate 		if (!sctp_add_recvq(sctp, mp, B_FALSE)) {
29660Sstevel@tonic-gate 			BUMP_MIB(&ip_mib, ipInDiscards);
29670Sstevel@tonic-gate 			freemsg(first_mp);
29680Sstevel@tonic-gate 		}
29690Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_lock);
29700Sstevel@tonic-gate 		SCTP_REFRELE(sctp);
29710Sstevel@tonic-gate 		return;
29720Sstevel@tonic-gate 	} else {
29730Sstevel@tonic-gate 		sctp->sctp_running = B_TRUE;
29740Sstevel@tonic-gate 		mutex_exit(&sctp->sctp_lock);
29750Sstevel@tonic-gate 
29760Sstevel@tonic-gate 		mutex_enter(&sctp->sctp_recvq_lock);
29770Sstevel@tonic-gate 		if (sctp->sctp_recvq != NULL) {
29780Sstevel@tonic-gate 			if (mctl_present)
29790Sstevel@tonic-gate 				mp->b_prev = first_mp;
29800Sstevel@tonic-gate 			if (!sctp_add_recvq(sctp, mp, B_TRUE)) {
29810Sstevel@tonic-gate 				BUMP_MIB(&ip_mib, ipInDiscards);
29820Sstevel@tonic-gate 				freemsg(first_mp);
29830Sstevel@tonic-gate 			}
29840Sstevel@tonic-gate 			mutex_exit(&sctp->sctp_recvq_lock);
29850Sstevel@tonic-gate 			WAKE_SCTP(sctp);
29860Sstevel@tonic-gate 			SCTP_REFRELE(sctp);
29870Sstevel@tonic-gate 			return;
29880Sstevel@tonic-gate 		}
29890Sstevel@tonic-gate 	}
29900Sstevel@tonic-gate 	mutex_exit(&sctp->sctp_recvq_lock);
29910Sstevel@tonic-gate 	sctp_input_data(sctp, mp, (mctl_present ? first_mp : NULL));
29920Sstevel@tonic-gate 	WAKE_SCTP(sctp);
29930Sstevel@tonic-gate 	sctp_process_sendq(sctp);
29940Sstevel@tonic-gate 	SCTP_REFRELE(sctp);
29950Sstevel@tonic-gate }
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate static void
29980Sstevel@tonic-gate sctp_process_abort(sctp_t *sctp, sctp_chunk_hdr_t *ch, int err)
29990Sstevel@tonic-gate {
30000Sstevel@tonic-gate 	BUMP_MIB(&sctp_mib, sctpAborted);
30010Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ibchunks);
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate 	sctp_assoc_event(sctp, SCTP_COMM_LOST,
30040Sstevel@tonic-gate 	    ntohs(((sctp_parm_hdr_t *)(ch + 1))->sph_type), ch);
30050Sstevel@tonic-gate 	sctp_clean_death(sctp, err);
30060Sstevel@tonic-gate }
30070Sstevel@tonic-gate 
30080Sstevel@tonic-gate void
30090Sstevel@tonic-gate sctp_input_data(sctp_t *sctp, mblk_t *mp, mblk_t *ipsec_mp)
30100Sstevel@tonic-gate {
30110Sstevel@tonic-gate 	sctp_chunk_hdr_t	*ch;
30120Sstevel@tonic-gate 	ssize_t			mlen;
30130Sstevel@tonic-gate 	int			gotdata;
30140Sstevel@tonic-gate 	int			trysend;
30150Sstevel@tonic-gate 	sctp_faddr_t		*fp;
30160Sstevel@tonic-gate 	sctp_init_chunk_t	*iack;
30170Sstevel@tonic-gate 	uint32_t		tsn;
30180Sstevel@tonic-gate 	sctp_data_hdr_t		*sdc;
30190Sstevel@tonic-gate 	ip6_pkt_t		ipp;
30200Sstevel@tonic-gate 	in6_addr_t		src;
30210Sstevel@tonic-gate 	in6_addr_t		dst;
30220Sstevel@tonic-gate 	uint_t			ifindex;
30230Sstevel@tonic-gate 	sctp_hdr_t		*sctph;
30240Sstevel@tonic-gate 	uint_t			ip_hdr_len;
30250Sstevel@tonic-gate 	mblk_t			*dups = NULL;
30260Sstevel@tonic-gate 	int			recv_adaption;
30270Sstevel@tonic-gate 	boolean_t		wake_eager = B_FALSE;
30280Sstevel@tonic-gate 	mblk_t			*pinfo_mp;
30290Sstevel@tonic-gate 	in_pktinfo_t		*pinfo = NULL;
30300Sstevel@tonic-gate 	in6_addr_t		peer_src;
30310Sstevel@tonic-gate 	int64_t			now;
30320Sstevel@tonic-gate 
30330Sstevel@tonic-gate 	if (DB_TYPE(mp) != M_DATA) {
30340Sstevel@tonic-gate 		ASSERT(DB_TYPE(mp) == M_CTL);
30350Sstevel@tonic-gate 		if (MBLKL(mp) == sizeof (in_pktinfo_t) &&
30360Sstevel@tonic-gate 		    ((in_pktinfo_t *)mp->b_rptr)->in_pkt_ulp_type ==
30370Sstevel@tonic-gate 		    IN_PKTINFO) {
30380Sstevel@tonic-gate 			pinfo = (in_pktinfo_t *)mp->b_rptr;
30390Sstevel@tonic-gate 			pinfo_mp = mp;
30400Sstevel@tonic-gate 			mp = mp->b_cont;
30410Sstevel@tonic-gate 		} else {
30420Sstevel@tonic-gate 			if (ipsec_mp != NULL)
30430Sstevel@tonic-gate 				freeb(ipsec_mp);
30440Sstevel@tonic-gate 			sctp_icmp_error(sctp, mp);
30450Sstevel@tonic-gate 			return;
30460Sstevel@tonic-gate 		}
30470Sstevel@tonic-gate 	}
30480Sstevel@tonic-gate 	ASSERT(DB_TYPE(mp) == M_DATA);
30490Sstevel@tonic-gate 
30500Sstevel@tonic-gate 	if (mp->b_cont != NULL) {
30510Sstevel@tonic-gate 		/*
30520Sstevel@tonic-gate 		 * All subsequent code is vastly simplified if it can
30530Sstevel@tonic-gate 		 * assume a single contiguous chunk of data.
30540Sstevel@tonic-gate 		 */
30550Sstevel@tonic-gate 		if (pullupmsg(mp, -1) == 0) {
30560Sstevel@tonic-gate 			BUMP_MIB(&ip_mib, ipInDiscards);
30570Sstevel@tonic-gate 			if (ipsec_mp != NULL)
30580Sstevel@tonic-gate 				freeb(ipsec_mp);
30590Sstevel@tonic-gate 			if (pinfo != NULL)
30600Sstevel@tonic-gate 				freeb(pinfo_mp);
30610Sstevel@tonic-gate 			freemsg(mp);
30620Sstevel@tonic-gate 			return;
30630Sstevel@tonic-gate 		}
30640Sstevel@tonic-gate 	}
30650Sstevel@tonic-gate 
30660Sstevel@tonic-gate 	BUMP_LOCAL(sctp->sctp_ipkts);
30670Sstevel@tonic-gate 	sctph = find_sctp_hdrs(mp, &src, &dst, &ifindex, &ip_hdr_len,
30680Sstevel@tonic-gate 	    &ipp, pinfo);
30690Sstevel@tonic-gate 	if (pinfo != NULL)
30700Sstevel@tonic-gate 		freeb(pinfo_mp);
30710Sstevel@tonic-gate 	mlen = mp->b_wptr - (uchar_t *)(sctph + 1);
30720Sstevel@tonic-gate 	ch = sctp_first_chunk((uchar_t *)(sctph + 1), mlen);
30730Sstevel@tonic-gate 	if (ch == NULL) {
30740Sstevel@tonic-gate 		BUMP_MIB(&ip_mib, ipInDiscards);
30750Sstevel@tonic-gate 		if (ipsec_mp != NULL)
30760Sstevel@tonic-gate 			freeb(ipsec_mp);
30770Sstevel@tonic-gate 		freemsg(mp);
30780Sstevel@tonic-gate 		return;
30790Sstevel@tonic-gate 	}
30800Sstevel@tonic-gate 
30810Sstevel@tonic-gate 	if (!sctp_check_input(sctp, ch, mlen, 1)) {
30820Sstevel@tonic-gate 		BUMP_MIB(&ip_mib, ipInDiscards);
30830Sstevel@tonic-gate 		goto done;
30840Sstevel@tonic-gate 	}
30850Sstevel@tonic-gate 	/*
30860Sstevel@tonic-gate 	 * Check verfication tag (special handling for INIT,
30870Sstevel@tonic-gate 	 * COOKIE, SHUTDOWN_COMPLETE and SHUTDOWN_ACK chunks).
30880Sstevel@tonic-gate 	 * ABORTs are handled in the chunk processing loop, since
30890Sstevel@tonic-gate 	 * may not appear first. All other checked chunks must
30900Sstevel@tonic-gate 	 * appear first, or will have been dropped by check_input().
30910Sstevel@tonic-gate 	 */
30920Sstevel@tonic-gate 	switch (ch->sch_id) {
30930Sstevel@tonic-gate 	case CHUNK_INIT:
30940Sstevel@tonic-gate 		if (sctph->sh_verf != 0) {
30950Sstevel@tonic-gate 			/* drop it */
30960Sstevel@tonic-gate 			goto done;
30970Sstevel@tonic-gate 		}
30980Sstevel@tonic-gate 		break;
30990Sstevel@tonic-gate 	case CHUNK_SHUTDOWN_COMPLETE:
31000Sstevel@tonic-gate 		if (sctph->sh_verf == sctp->sctp_lvtag)
31010Sstevel@tonic-gate 			break;
31020Sstevel@tonic-gate 		if (sctph->sh_verf == sctp->sctp_fvtag &&
31030Sstevel@tonic-gate 		    SCTP_GET_TBIT(ch)) {
31040Sstevel@tonic-gate 			break;
31050Sstevel@tonic-gate 		}
31060Sstevel@tonic-gate 		/* else drop it */
31070Sstevel@tonic-gate 		goto done;
31080Sstevel@tonic-gate 	case CHUNK_ABORT:
31090Sstevel@tonic-gate 	case CHUNK_COOKIE:
31100Sstevel@tonic-gate 		/* handled below */
31110Sstevel@tonic-gate 		break;
31120Sstevel@tonic-gate 	case CHUNK_SHUTDOWN_ACK:
31130Sstevel@tonic-gate 		if (sctp->sctp_state > SCTPS_BOUND &&
31140Sstevel@tonic-gate 		    sctp->sctp_state < SCTPS_ESTABLISHED) {
31150Sstevel@tonic-gate 			/* treat as OOTB */
31160Sstevel@tonic-gate 			sctp_ootb_shutdown_ack(sctp, mp, ip_hdr_len);
31170Sstevel@tonic-gate 			if (ipsec_mp != NULL)
31180Sstevel@tonic-gate 				freeb(ipsec_mp);
31190Sstevel@tonic-gate 			return;
31200Sstevel@tonic-gate 		}
31210Sstevel@tonic-gate 		/* else fallthru */
31220Sstevel@tonic-gate 	default:
31230Sstevel@tonic-gate 		/*
31240Sstevel@tonic-gate 		 * All other packets must have a valid
31250Sstevel@tonic-gate 		 * verification tag, however if this is a
31260Sstevel@tonic-gate 		 * listener, we use a refined version of
31270Sstevel@tonic-gate 		 * out-of-the-blue logic.
31280Sstevel@tonic-gate 		 */
31290Sstevel@tonic-gate 		if (sctph->sh_verf != sctp->sctp_lvtag &&
31300Sstevel@tonic-gate 		    sctp->sctp_state != SCTPS_LISTEN) {
31310Sstevel@tonic-gate 			/* drop it */
31320Sstevel@tonic-gate 			goto done;
31330Sstevel@tonic-gate 		}
31340Sstevel@tonic-gate 		break;
31350Sstevel@tonic-gate 	}
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate 	/* Have a valid sctp for this packet */
31380Sstevel@tonic-gate 	fp = sctp_lookup_faddr(sctp, &src);
31390Sstevel@tonic-gate 	dprint(2, ("sctp_dispatch_rput: mp=%p fp=%p sctp=%p\n", mp, fp, sctp));
31400Sstevel@tonic-gate 
31410Sstevel@tonic-gate 	gotdata = 0;
31420Sstevel@tonic-gate 	trysend = 0;
31430Sstevel@tonic-gate 
31440Sstevel@tonic-gate 	now = lbolt64;
31450Sstevel@tonic-gate 	/* Process the chunks */
31460Sstevel@tonic-gate 	do {
31470Sstevel@tonic-gate 		dprint(3, ("sctp_dispatch_rput: state=%d, chunk id=%d\n",
31480Sstevel@tonic-gate 		    sctp->sctp_state, (int)(ch->sch_id)));
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate 		if (ch->sch_id == CHUNK_ABORT) {
31510Sstevel@tonic-gate 			if (sctph->sh_verf != sctp->sctp_lvtag &&
31520Sstevel@tonic-gate 			    sctph->sh_verf != sctp->sctp_fvtag) {
31530Sstevel@tonic-gate 				/* drop it */
31540Sstevel@tonic-gate 				goto done;
31550Sstevel@tonic-gate 			}
31560Sstevel@tonic-gate 		}
31570Sstevel@tonic-gate 
31580Sstevel@tonic-gate 		switch (sctp->sctp_state) {
31590Sstevel@tonic-gate 
31600Sstevel@tonic-gate 		case SCTPS_ESTABLISHED:
31610Sstevel@tonic-gate 		case SCTPS_SHUTDOWN_PENDING:
31620Sstevel@tonic-gate 		case SCTPS_SHUTDOWN_SENT:
31630Sstevel@tonic-gate 			switch (ch->sch_id) {
31640Sstevel@tonic-gate 			case CHUNK_DATA:
31650Sstevel@tonic-gate 				/* 0-length data chunks are not allowed */
31660Sstevel@tonic-gate 				if (ntohs(ch->sch_len) == sizeof (*sdc)) {
31670Sstevel@tonic-gate 					sdc = (sctp_data_hdr_t *)ch;
31680Sstevel@tonic-gate 					tsn = sdc->sdh_tsn;
31690Sstevel@tonic-gate 					sctp_send_abort(sctp, sctp->sctp_fvtag,
31700Sstevel@tonic-gate 					    SCTP_ERR_NO_USR_DATA, (char *)&tsn,
31710Sstevel@tonic-gate 					    sizeof (tsn), mp, 0, B_FALSE);
31720Sstevel@tonic-gate 					sctp_assoc_event(sctp, SCTP_COMM_LOST,
31730Sstevel@tonic-gate 					    0, NULL);
31740Sstevel@tonic-gate 					sctp_clean_death(sctp, ECONNABORTED);
31750Sstevel@tonic-gate 					goto done;
31760Sstevel@tonic-gate 				}
31770Sstevel@tonic-gate 
31780Sstevel@tonic-gate 				ASSERT(fp != NULL);
31790Sstevel@tonic-gate 				sctp->sctp_lastdata = fp;
31800Sstevel@tonic-gate 				sctp_data_chunk(sctp, ch, mp, &dups, fp, &ipp);
31810Sstevel@tonic-gate 				gotdata = 1;
31820Sstevel@tonic-gate 				/* Restart shutdown timer if shutting down */
31830Sstevel@tonic-gate 				if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) {
31840Sstevel@tonic-gate 					/*
31850Sstevel@tonic-gate 					 * If we have exceeded our max
31860Sstevel@tonic-gate 					 * wait bound for waiting for a
31870Sstevel@tonic-gate 					 * shutdown ack from the peer,
31880Sstevel@tonic-gate 					 * abort the association.
31890Sstevel@tonic-gate 					 */
31900Sstevel@tonic-gate 					if (sctp_shutack_wait_bound != 0 &&
31910Sstevel@tonic-gate 					    TICK_TO_MSEC(now -
31920Sstevel@tonic-gate 					    sctp->sctp_out_time) >
31930Sstevel@tonic-gate 					    sctp_shutack_wait_bound) {
31940Sstevel@tonic-gate 						sctp_send_abort(sctp,
31950Sstevel@tonic-gate 						    sctp->sctp_fvtag, 0, NULL,
31960Sstevel@tonic-gate 						    0, mp, 0, B_FALSE);
31970Sstevel@tonic-gate 						sctp_assoc_event(sctp,
31980Sstevel@tonic-gate 						    SCTP_COMM_LOST, 0, NULL);
31990Sstevel@tonic-gate 						sctp_clean_death(sctp,
32000Sstevel@tonic-gate 						    ECONNABORTED);
32010Sstevel@tonic-gate 						goto done;
32020Sstevel@tonic-gate 					}
32030Sstevel@tonic-gate 					SCTP_FADDR_TIMER_RESTART(sctp, fp,
32040Sstevel@tonic-gate 					    fp->rto);
32050Sstevel@tonic-gate 				}
32060Sstevel@tonic-gate 				break;
32070Sstevel@tonic-gate 			case CHUNK_SACK:
32080Sstevel@tonic-gate 				ASSERT(fp != NULL);
32090Sstevel@tonic-gate 				/*
32100Sstevel@tonic-gate 				 * Peer is real and alive if it can ack our
32110Sstevel@tonic-gate 				 * data.
32120Sstevel@tonic-gate 				 */
32130Sstevel@tonic-gate 				sctp_faddr_alive(sctp, fp);
32140Sstevel@tonic-gate 				trysend = sctp_got_sack(sctp, ch);
32150Sstevel@tonic-gate 				break;
32160Sstevel@tonic-gate 			case CHUNK_HEARTBEAT:
32170Sstevel@tonic-gate 				sctp_return_heartbeat(sctp, ch, mp);
32180Sstevel@tonic-gate 				break;
32190Sstevel@tonic-gate 			case CHUNK_HEARTBEAT_ACK:
32200Sstevel@tonic-gate 				sctp_process_heartbeat(sctp, ch);
32210Sstevel@tonic-gate 				break;
32220Sstevel@tonic-gate 			case CHUNK_SHUTDOWN:
32230Sstevel@tonic-gate 				sctp_shutdown_event(sctp);
32240Sstevel@tonic-gate 				trysend = sctp_shutdown_received(sctp, ch,
32250Sstevel@tonic-gate 				    0, 0);
32260Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
32270Sstevel@tonic-gate 				break;
32280Sstevel@tonic-gate 			case CHUNK_SHUTDOWN_ACK:
32290Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
32300Sstevel@tonic-gate 				if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) {
32310Sstevel@tonic-gate 					sctp_shutdown_complete(sctp);
32320Sstevel@tonic-gate 					BUMP_MIB(&sctp_mib, sctpShutdowns);
32330Sstevel@tonic-gate 					sctp_assoc_event(sctp,
32340Sstevel@tonic-gate 					    SCTP_SHUTDOWN_COMP, 0, NULL);
32350Sstevel@tonic-gate 					sctp_clean_death(sctp, 0);
32360Sstevel@tonic-gate 					goto done;
32370Sstevel@tonic-gate 				}
32380Sstevel@tonic-gate 				break;
32390Sstevel@tonic-gate 			case CHUNK_ABORT: {
32400Sstevel@tonic-gate 				sctp_saddr_ipif_t *sp;
32410Sstevel@tonic-gate 
32420Sstevel@tonic-gate 				/* Ignore if delete pending */
32430Sstevel@tonic-gate 				sp = sctp_saddr_lookup(sctp, &dst);
32440Sstevel@tonic-gate 				ASSERT(sp != NULL);
32450Sstevel@tonic-gate 				if (sp->saddr_ipif_delete_pending) {
32460Sstevel@tonic-gate 					BUMP_LOCAL(sctp->sctp_ibchunks);
32470Sstevel@tonic-gate 					break;
32480Sstevel@tonic-gate 				}
32490Sstevel@tonic-gate 
32500Sstevel@tonic-gate 				sctp_process_abort(sctp, ch, ECONNRESET);
32510Sstevel@tonic-gate 				goto done;
32520Sstevel@tonic-gate 			}
32530Sstevel@tonic-gate 			case CHUNK_INIT:
32540Sstevel@tonic-gate 				sctp_send_initack(sctp, ch, mp);
32550Sstevel@tonic-gate 				break;
32560Sstevel@tonic-gate 			case CHUNK_COOKIE:
32570Sstevel@tonic-gate 				if (sctp_process_cookie(sctp, ch, mp, &iack,
32580Sstevel@tonic-gate 				    sctph, &recv_adaption, NULL) != -1) {
32590Sstevel@tonic-gate 					sctp_send_cookie_ack(sctp);
32600Sstevel@tonic-gate 					sctp_assoc_event(sctp, SCTP_RESTART,
32610Sstevel@tonic-gate 					    0, NULL);
32620Sstevel@tonic-gate 					if (recv_adaption) {
32630Sstevel@tonic-gate 						sctp->sctp_recv_adaption = 1;
32640Sstevel@tonic-gate 						sctp_adaption_event(sctp);
32650Sstevel@tonic-gate 					}
32660Sstevel@tonic-gate 				} else {
32670Sstevel@tonic-gate 					BUMP_MIB(&sctp_mib,
32680Sstevel@tonic-gate 					    sctpInInvalidCookie);
32690Sstevel@tonic-gate 				}
32700Sstevel@tonic-gate 				break;
32710Sstevel@tonic-gate 			case CHUNK_ERROR: {
32720Sstevel@tonic-gate 				int error;
32730Sstevel@tonic-gate 
32740Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
32750Sstevel@tonic-gate 				error = sctp_handle_error(sctp, sctph, ch, mp);
32760Sstevel@tonic-gate 				if (error != 0) {
32770Sstevel@tonic-gate 					sctp_clean_death(sctp, error);
32780Sstevel@tonic-gate 					goto done;
32790Sstevel@tonic-gate 				}
32800Sstevel@tonic-gate 				break;
32810Sstevel@tonic-gate 			}
32820Sstevel@tonic-gate 			case CHUNK_ASCONF:
32830Sstevel@tonic-gate 				ASSERT(fp != NULL);
32840Sstevel@tonic-gate 				sctp_input_asconf(sctp, ch, fp);
32850Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
32860Sstevel@tonic-gate 				break;
32870Sstevel@tonic-gate 			case CHUNK_ASCONF_ACK:
32880Sstevel@tonic-gate 				ASSERT(fp != NULL);
32890Sstevel@tonic-gate 				sctp_faddr_alive(sctp, fp);
32900Sstevel@tonic-gate 				sctp_input_asconf_ack(sctp, ch, fp);
32910Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
32920Sstevel@tonic-gate 				break;
32930Sstevel@tonic-gate 			case CHUNK_FORWARD_TSN:
32940Sstevel@tonic-gate 				ASSERT(fp != NULL);
32950Sstevel@tonic-gate 				sctp->sctp_lastdata = fp;
32960Sstevel@tonic-gate 				sctp_process_forward_tsn(sctp, ch, fp, &ipp);
32970Sstevel@tonic-gate 				gotdata = 1;
32980Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
32990Sstevel@tonic-gate 				break;
33000Sstevel@tonic-gate 			default:
33010Sstevel@tonic-gate 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
33020Sstevel@tonic-gate 					goto nomorechunks;
33030Sstevel@tonic-gate 				} /* else skip and continue processing */
33040Sstevel@tonic-gate 				break;
33050Sstevel@tonic-gate 			}
33060Sstevel@tonic-gate 			break;
33070Sstevel@tonic-gate 
33080Sstevel@tonic-gate 		case SCTPS_LISTEN:
33090Sstevel@tonic-gate 			switch (ch->sch_id) {
33100Sstevel@tonic-gate 			case CHUNK_INIT:
33110Sstevel@tonic-gate 				sctp_send_initack(sctp, ch, mp);
33120Sstevel@tonic-gate 				break;
33130Sstevel@tonic-gate 			case CHUNK_COOKIE: {
33140Sstevel@tonic-gate 				sctp_t *eager;
33150Sstevel@tonic-gate 
33160Sstevel@tonic-gate 				if (sctp_process_cookie(sctp, ch, mp, &iack,
33170Sstevel@tonic-gate 				    sctph, &recv_adaption, &peer_src) == -1) {
33180Sstevel@tonic-gate 					BUMP_MIB(&sctp_mib,
33190Sstevel@tonic-gate 					    sctpInInvalidCookie);
33200Sstevel@tonic-gate 					goto done;
33210Sstevel@tonic-gate 				}
33220Sstevel@tonic-gate 
33230Sstevel@tonic-gate 				/*
33240Sstevel@tonic-gate 				 * The cookie is good; ensure that
33250Sstevel@tonic-gate 				 * the peer used the verification
33260Sstevel@tonic-gate 				 * tag from the init ack in the header.
33270Sstevel@tonic-gate 				 */
33280Sstevel@tonic-gate 				if (iack->sic_inittag != sctph->sh_verf)
33290Sstevel@tonic-gate 					goto done;
33300Sstevel@tonic-gate 
33310Sstevel@tonic-gate 				eager = sctp_conn_request(sctp, mp, ifindex,
33320Sstevel@tonic-gate 				    ip_hdr_len, iack, ipsec_mp);
33330Sstevel@tonic-gate 				if (eager == NULL) {
33340Sstevel@tonic-gate 					sctp_send_abort(sctp, sctph->sh_verf,
33350Sstevel@tonic-gate 					    SCTP_ERR_NO_RESOURCES, NULL, 0, mp,
33360Sstevel@tonic-gate 					    0, B_FALSE);
33370Sstevel@tonic-gate 					goto done;
33380Sstevel@tonic-gate 				}
33390Sstevel@tonic-gate 
33400Sstevel@tonic-gate 				/*
33410Sstevel@tonic-gate 				 * If there were extra chunks
33420Sstevel@tonic-gate 				 * bundled with the cookie,
33430Sstevel@tonic-gate 				 * they must be processed
33440Sstevel@tonic-gate 				 * on the eager's queue. We
33450Sstevel@tonic-gate 				 * accomplish this by refeeding
33460Sstevel@tonic-gate 				 * the whole packet into the
33470Sstevel@tonic-gate 				 * state machine on the right
33480Sstevel@tonic-gate 				 * q. The packet (mp) gets
33490Sstevel@tonic-gate 				 * there via the eager's
33500Sstevel@tonic-gate 				 * cookie_mp field (overloaded
33510Sstevel@tonic-gate 				 * with the active open role).
33520Sstevel@tonic-gate 				 * This is picked up when
33530Sstevel@tonic-gate 				 * processing the null bind
33540Sstevel@tonic-gate 				 * request put on the eager's
33550Sstevel@tonic-gate 				 * q by sctp_accept(). We must
33560Sstevel@tonic-gate 				 * first revert the cookie
33570Sstevel@tonic-gate 				 * chunk's length field to network
33580Sstevel@tonic-gate 				 * byteorder so it can be
33590Sstevel@tonic-gate 				 * properly reprocessed on the
33600Sstevel@tonic-gate 				 * eager's queue.
33610Sstevel@tonic-gate 				 */
33620Sstevel@tonic-gate 				BUMP_MIB(&sctp_mib, sctpPassiveEstab);
33630Sstevel@tonic-gate 				if (mlen > ntohs(ch->sch_len)) {
33640Sstevel@tonic-gate 					eager->sctp_cookie_mp = dupb(mp);
33650Sstevel@tonic-gate 					/*
33660Sstevel@tonic-gate 					 * If no mem, just let
33670Sstevel@tonic-gate 					 * the peer retransmit.
33680Sstevel@tonic-gate 					 */
33690Sstevel@tonic-gate 				}
33700Sstevel@tonic-gate 				sctp_assoc_event(eager, SCTP_COMM_UP, 0, NULL);
33710Sstevel@tonic-gate 				if (recv_adaption) {
33720Sstevel@tonic-gate 					eager->sctp_recv_adaption = 1;
33730Sstevel@tonic-gate 					eager->sctp_rx_adaption_code =
33740Sstevel@tonic-gate 					    sctp->sctp_rx_adaption_code;
33750Sstevel@tonic-gate 					sctp_adaption_event(eager);
33760Sstevel@tonic-gate 				}
33770Sstevel@tonic-gate 
33780Sstevel@tonic-gate 				eager->sctp_active = now;
33790Sstevel@tonic-gate 				sctp_send_cookie_ack(eager);
33800Sstevel@tonic-gate 
33810Sstevel@tonic-gate 				wake_eager = B_TRUE;
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate 				/*
33840Sstevel@tonic-gate 				 * Process rest of the chunks with eager.
33850Sstevel@tonic-gate 				 */
33860Sstevel@tonic-gate 				sctp = eager;
33870Sstevel@tonic-gate 				fp = sctp_lookup_faddr(sctp, &peer_src);
33880Sstevel@tonic-gate 				/*
33890Sstevel@tonic-gate 				 * Confirm peer's original source.  fp can
33900Sstevel@tonic-gate 				 * only be NULL if peer does not use the
33910Sstevel@tonic-gate 				 * original source as one of its addresses...
33920Sstevel@tonic-gate 				 */
33930Sstevel@tonic-gate 				if (fp == NULL)
33940Sstevel@tonic-gate 					fp = sctp_lookup_faddr(sctp, &src);
33950Sstevel@tonic-gate 				else
33960Sstevel@tonic-gate 					sctp_faddr_alive(sctp, fp);
33970Sstevel@tonic-gate 
33980Sstevel@tonic-gate 				/*
33990Sstevel@tonic-gate 				 * Validate the peer addresses.  It also starts
34000Sstevel@tonic-gate 				 * the heartbeat timer.
34010Sstevel@tonic-gate 				 */
34020Sstevel@tonic-gate 				sctp_validate_peer(sctp);
34030Sstevel@tonic-gate 				break;
34040Sstevel@tonic-gate 			}
34050Sstevel@tonic-gate 			/* Anything else is considered out-of-the-blue */
34060Sstevel@tonic-gate 			case CHUNK_ERROR:
34070Sstevel@tonic-gate 			case CHUNK_ABORT:
34080Sstevel@tonic-gate 			case CHUNK_COOKIE_ACK:
34090Sstevel@tonic-gate 			case CHUNK_SHUTDOWN_COMPLETE:
34100Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
34110Sstevel@tonic-gate 				goto done;
34120Sstevel@tonic-gate 			default:
34130Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
34140Sstevel@tonic-gate 				sctp_send_abort(sctp, sctph->sh_verf, 0, NULL,
34150Sstevel@tonic-gate 				    0, mp, 0, B_TRUE);
34160Sstevel@tonic-gate 				goto done;
34170Sstevel@tonic-gate 			}
34180Sstevel@tonic-gate 			break;
34190Sstevel@tonic-gate 
34200Sstevel@tonic-gate 		case SCTPS_COOKIE_WAIT:
34210Sstevel@tonic-gate 			switch (ch->sch_id) {
34220Sstevel@tonic-gate 			case CHUNK_INIT_ACK:
34230Sstevel@tonic-gate 				sctp_stop_faddr_timers(sctp);
34240Sstevel@tonic-gate 				sctp_faddr_alive(sctp, sctp->sctp_current);
34250Sstevel@tonic-gate 				sctp_send_cookie_echo(sctp, ch, mp);
34260Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
34270Sstevel@tonic-gate 				break;
34280Sstevel@tonic-gate 			case CHUNK_ABORT:
34290Sstevel@tonic-gate 				sctp_process_abort(sctp, ch, ECONNREFUSED);
34300Sstevel@tonic-gate 				goto done;
34310Sstevel@tonic-gate 			case CHUNK_INIT:
34320Sstevel@tonic-gate 				sctp_send_initack(sctp, ch, mp);
34330Sstevel@tonic-gate 				break;
34340Sstevel@tonic-gate 			case CHUNK_COOKIE:
34350Sstevel@tonic-gate 				if (sctp_process_cookie(sctp, ch, mp, &iack,
34360Sstevel@tonic-gate 				    sctph, &recv_adaption, NULL) == -1) {
34370Sstevel@tonic-gate 					BUMP_MIB(&sctp_mib,
34380Sstevel@tonic-gate 					    sctpInInvalidCookie);
34390Sstevel@tonic-gate 					break;
34400Sstevel@tonic-gate 				}
34410Sstevel@tonic-gate 				sctp_send_cookie_ack(sctp);
34420Sstevel@tonic-gate 				sctp_stop_faddr_timers(sctp);
34430Sstevel@tonic-gate 				if (!SCTP_IS_DETACHED(sctp)) {
34440Sstevel@tonic-gate 				    sctp->sctp_ulp_connected(sctp->sctp_ulpd);
34450Sstevel@tonic-gate 				    sctp_set_ulp_prop(sctp);
34460Sstevel@tonic-gate 				}
34470Sstevel@tonic-gate 				sctp->sctp_state = SCTPS_ESTABLISHED;
34480Sstevel@tonic-gate 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
34490Sstevel@tonic-gate 				BUMP_MIB(&sctp_mib, sctpActiveEstab);
34500Sstevel@tonic-gate 				if (sctp->sctp_cookie_mp) {
34510Sstevel@tonic-gate 					freemsg(sctp->sctp_cookie_mp);
34520Sstevel@tonic-gate 					sctp->sctp_cookie_mp = NULL;
34530Sstevel@tonic-gate 				}
34540Sstevel@tonic-gate 
34550Sstevel@tonic-gate 				/* Validate the peer addresses. */
34560Sstevel@tonic-gate 				sctp->sctp_active = now;
34570Sstevel@tonic-gate 				sctp_validate_peer(sctp);
34580Sstevel@tonic-gate 
34590Sstevel@tonic-gate 				sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL);
34600Sstevel@tonic-gate 				if (recv_adaption) {
34610Sstevel@tonic-gate 					sctp->sctp_recv_adaption = 1;
34620Sstevel@tonic-gate 					sctp_adaption_event(sctp);
34630Sstevel@tonic-gate 				}
34640Sstevel@tonic-gate 				/* Try sending queued data, or ASCONFs */
34650Sstevel@tonic-gate 				trysend = 1;
34660Sstevel@tonic-gate 				break;
34670Sstevel@tonic-gate 			default:
34680Sstevel@tonic-gate 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
34690Sstevel@tonic-gate 					goto nomorechunks;
34700Sstevel@tonic-gate 				} /* else skip and continue processing */
34710Sstevel@tonic-gate 				break;
34720Sstevel@tonic-gate 			}
34730Sstevel@tonic-gate 			break;
34740Sstevel@tonic-gate 
34750Sstevel@tonic-gate 		case SCTPS_COOKIE_ECHOED:
34760Sstevel@tonic-gate 			switch (ch->sch_id) {
34770Sstevel@tonic-gate 			case CHUNK_COOKIE_ACK:
34780Sstevel@tonic-gate 				if (!SCTP_IS_DETACHED(sctp)) {
34790Sstevel@tonic-gate 				    sctp->sctp_ulp_connected(sctp->sctp_ulpd);
34800Sstevel@tonic-gate 				    sctp_set_ulp_prop(sctp);
34810Sstevel@tonic-gate 				}
34820Sstevel@tonic-gate 				if (sctp->sctp_unacked == 0)
34830Sstevel@tonic-gate 					sctp_stop_faddr_timers(sctp);
34840Sstevel@tonic-gate 				sctp->sctp_state = SCTPS_ESTABLISHED;
34850Sstevel@tonic-gate 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
34860Sstevel@tonic-gate 				BUMP_MIB(&sctp_mib, sctpActiveEstab);
34870Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
34880Sstevel@tonic-gate 				if (sctp->sctp_cookie_mp) {
34890Sstevel@tonic-gate 					freemsg(sctp->sctp_cookie_mp);
34900Sstevel@tonic-gate 					sctp->sctp_cookie_mp = NULL;
34910Sstevel@tonic-gate 				}
34920Sstevel@tonic-gate 				sctp_faddr_alive(sctp, fp);
34930Sstevel@tonic-gate 				/* Validate the peer addresses. */
34940Sstevel@tonic-gate 				sctp->sctp_active = now;
34950Sstevel@tonic-gate 				sctp_validate_peer(sctp);
34960Sstevel@tonic-gate 
34970Sstevel@tonic-gate 				/* Try sending queued data, or ASCONFs */
34980Sstevel@tonic-gate 				trysend = 1;
34990Sstevel@tonic-gate 				sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL);
35000Sstevel@tonic-gate 				sctp_adaption_event(sctp);
35010Sstevel@tonic-gate 				break;
35020Sstevel@tonic-gate 			case CHUNK_ABORT:
35030Sstevel@tonic-gate 				sctp_process_abort(sctp, ch, ECONNREFUSED);
35040Sstevel@tonic-gate 				goto done;
35050Sstevel@tonic-gate 			case CHUNK_COOKIE:
35060Sstevel@tonic-gate 				if (sctp_process_cookie(sctp, ch, mp, &iack,
35070Sstevel@tonic-gate 				    sctph, &recv_adaption, NULL) == -1) {
35080Sstevel@tonic-gate 					BUMP_MIB(&sctp_mib,
35090Sstevel@tonic-gate 					    sctpInInvalidCookie);
35100Sstevel@tonic-gate 					break;
35110Sstevel@tonic-gate 				}
35120Sstevel@tonic-gate 				sctp_send_cookie_ack(sctp);
35130Sstevel@tonic-gate 
35140Sstevel@tonic-gate 				if (!SCTP_IS_DETACHED(sctp)) {
35150Sstevel@tonic-gate 				    sctp->sctp_ulp_connected(sctp->sctp_ulpd);
35160Sstevel@tonic-gate 				    sctp_set_ulp_prop(sctp);
35170Sstevel@tonic-gate 				}
35180Sstevel@tonic-gate 				if (sctp->sctp_unacked == 0)
35190Sstevel@tonic-gate 					sctp_stop_faddr_timers(sctp);
35200Sstevel@tonic-gate 				sctp->sctp_state = SCTPS_ESTABLISHED;
35210Sstevel@tonic-gate 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
35220Sstevel@tonic-gate 				BUMP_MIB(&sctp_mib, sctpActiveEstab);
35230Sstevel@tonic-gate 				if (sctp->sctp_cookie_mp) {
35240Sstevel@tonic-gate 					freemsg(sctp->sctp_cookie_mp);
35250Sstevel@tonic-gate 					sctp->sctp_cookie_mp = NULL;
35260Sstevel@tonic-gate 				}
35270Sstevel@tonic-gate 				/* Validate the peer addresses. */
35280Sstevel@tonic-gate 				sctp->sctp_active = now;
35290Sstevel@tonic-gate 				sctp_validate_peer(sctp);
35300Sstevel@tonic-gate 
35310Sstevel@tonic-gate 				sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL);
35320Sstevel@tonic-gate 				if (recv_adaption) {
35330Sstevel@tonic-gate 					sctp->sctp_recv_adaption = 1;
35340Sstevel@tonic-gate 					sctp_adaption_event(sctp);
35350Sstevel@tonic-gate 				}
35360Sstevel@tonic-gate 				/* Try sending queued data, or ASCONFs */
35370Sstevel@tonic-gate 				trysend = 1;
35380Sstevel@tonic-gate 				break;
35390Sstevel@tonic-gate 			case CHUNK_INIT:
35400Sstevel@tonic-gate 				sctp_send_initack(sctp, ch, mp);
35410Sstevel@tonic-gate 				break;
35420Sstevel@tonic-gate 			case CHUNK_ERROR: {
35430Sstevel@tonic-gate 				sctp_parm_hdr_t *p;
35440Sstevel@tonic-gate 
35450Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
35460Sstevel@tonic-gate 				/* check for a stale cookie */
35470Sstevel@tonic-gate 				if (ntohs(ch->sch_len) >=
35480Sstevel@tonic-gate 				    (sizeof (*p) + sizeof (*ch)) +
35490Sstevel@tonic-gate 				    sizeof (uint32_t)) {
35500Sstevel@tonic-gate 
35510Sstevel@tonic-gate 					p = (sctp_parm_hdr_t *)(ch + 1);
35520Sstevel@tonic-gate 					if (p->sph_type ==
35530Sstevel@tonic-gate 					    htons(SCTP_ERR_STALE_COOKIE)) {
35540Sstevel@tonic-gate 						BUMP_MIB(&sctp_mib,
35550Sstevel@tonic-gate 						    sctpAborted);
35560Sstevel@tonic-gate 						sctp_error_event(sctp, ch);
35570Sstevel@tonic-gate 						sctp_clean_death(sctp,
35580Sstevel@tonic-gate 						    ECONNREFUSED);
35590Sstevel@tonic-gate 						goto done;
35600Sstevel@tonic-gate 					}
35610Sstevel@tonic-gate 				}
35620Sstevel@tonic-gate 				break;
35630Sstevel@tonic-gate 			}
35640Sstevel@tonic-gate 			case CHUNK_HEARTBEAT:
35650Sstevel@tonic-gate 				sctp_return_heartbeat(sctp, ch, mp);
35660Sstevel@tonic-gate 				break;
35670Sstevel@tonic-gate 			default:
35680Sstevel@tonic-gate 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
35690Sstevel@tonic-gate 					goto nomorechunks;
35700Sstevel@tonic-gate 				} /* else skip and continue processing */
35710Sstevel@tonic-gate 			} /* switch (ch->sch_id) */
35720Sstevel@tonic-gate 			break;
35730Sstevel@tonic-gate 
35740Sstevel@tonic-gate 		case SCTPS_SHUTDOWN_ACK_SENT:
35750Sstevel@tonic-gate 			switch (ch->sch_id) {
35760Sstevel@tonic-gate 			case CHUNK_ABORT:
35770Sstevel@tonic-gate 				/* Pass gathered wisdom to IP for keeping */
35780Sstevel@tonic-gate 				for (fp = sctp->sctp_faddrs; fp != NULL;
35790Sstevel@tonic-gate 				    fp = fp->next) {
35800Sstevel@tonic-gate 					sctp_faddr2ire(sctp, fp);
35810Sstevel@tonic-gate 				}
35820Sstevel@tonic-gate 				sctp_process_abort(sctp, ch, 0);
35830Sstevel@tonic-gate 				goto done;
35840Sstevel@tonic-gate 			case CHUNK_SHUTDOWN_COMPLETE:
35850Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
35860Sstevel@tonic-gate 				BUMP_MIB(&sctp_mib, sctpShutdowns);
35870Sstevel@tonic-gate 				sctp_assoc_event(sctp, SCTP_SHUTDOWN_COMP, 0,
35880Sstevel@tonic-gate 				    NULL);
35890Sstevel@tonic-gate 
35900Sstevel@tonic-gate 				/* Pass gathered wisdom to IP for keeping */
35910Sstevel@tonic-gate 				for (fp = sctp->sctp_faddrs; fp != NULL;
35920Sstevel@tonic-gate 				    fp = fp->next) {
35930Sstevel@tonic-gate 					sctp_faddr2ire(sctp, fp);
35940Sstevel@tonic-gate 				}
35950Sstevel@tonic-gate 				sctp_clean_death(sctp, 0);
35960Sstevel@tonic-gate 				goto done;
35970Sstevel@tonic-gate 			case CHUNK_SHUTDOWN_ACK:
35980Sstevel@tonic-gate 				sctp_shutdown_complete(sctp);
35990Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
36000Sstevel@tonic-gate 				BUMP_MIB(&sctp_mib, sctpShutdowns);
36010Sstevel@tonic-gate 				sctp_assoc_event(sctp, SCTP_SHUTDOWN_COMP, 0,
36020Sstevel@tonic-gate 				    NULL);
36030Sstevel@tonic-gate 				sctp_clean_death(sctp, 0);
36040Sstevel@tonic-gate 				goto done;
36050Sstevel@tonic-gate 			case CHUNK_COOKIE:
36060Sstevel@tonic-gate 				(void) sctp_shutdown_received(sctp, NULL,
36070Sstevel@tonic-gate 				    1, 0);
36080Sstevel@tonic-gate 				BUMP_LOCAL(sctp->sctp_ibchunks);
36090Sstevel@tonic-gate 				break;
36100Sstevel@tonic-gate 			case CHUNK_HEARTBEAT:
36110Sstevel@tonic-gate 				sctp_return_heartbeat(sctp, ch, mp);
36120Sstevel@tonic-gate 				break;
36130Sstevel@tonic-gate 			default:
36140Sstevel@tonic-gate 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
36150Sstevel@tonic-gate 					goto nomorechunks;
36160Sstevel@tonic-gate 				} /* else skip and continue processing */
36170Sstevel@tonic-gate 				break;
36180Sstevel@tonic-gate 			}
36190Sstevel@tonic-gate 			break;
36200Sstevel@tonic-gate 
36210Sstevel@tonic-gate 		case SCTPS_SHUTDOWN_RECEIVED:
36220Sstevel@tonic-gate 			switch (ch->sch_id) {
36230Sstevel@tonic-gate 			case CHUNK_SHUTDOWN:
36240Sstevel@tonic-gate 				trysend = sctp_shutdown_received(sctp, ch,
36250Sstevel@tonic-gate 				    0, 0);
36260Sstevel@tonic-gate 				break;
36270Sstevel@tonic-gate 			case CHUNK_SACK:
36280Sstevel@tonic-gate 				trysend = sctp_got_sack(sctp, ch);
36290Sstevel@tonic-gate 				break;
36300Sstevel@tonic-gate 			case CHUNK_ABORT:
36310Sstevel@tonic-gate 				sctp_process_abort(sctp, ch, ECONNRESET);
36320Sstevel@tonic-gate 				goto done;
36330Sstevel@tonic-gate 			case CHUNK_HEARTBEAT:
36340Sstevel@tonic-gate 				sctp_return_heartbeat(sctp, ch, mp);
36350Sstevel@tonic-gate 				break;
36360Sstevel@tonic-gate 			default:
36370Sstevel@tonic-gate 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
36380Sstevel@tonic-gate 					goto nomorechunks;
36390Sstevel@tonic-gate 				} /* else skip and continue processing */
36400Sstevel@tonic-gate 				break;
36410Sstevel@tonic-gate 			}
36420Sstevel@tonic-gate 			break;
36430Sstevel@tonic-gate 
36440Sstevel@tonic-gate 		default:
36450Sstevel@tonic-gate 			BUMP_LOCAL(sctp->sctp_ibchunks);
36460Sstevel@tonic-gate 			cmn_err(CE_WARN, "XXXdefault in dispatch state %d",
36470Sstevel@tonic-gate 			    sctp->sctp_state);
36480Sstevel@tonic-gate 			break;
36490Sstevel@tonic-gate 		} /* switch (sctp->sctp_state) */
36500Sstevel@tonic-gate 
36510Sstevel@tonic-gate 		ch = sctp_next_chunk(ch, &mlen);
36520Sstevel@tonic-gate 		if (ch != NULL && !sctp_check_input(sctp, ch, mlen, 0))
36530Sstevel@tonic-gate 			goto done;
36540Sstevel@tonic-gate 	} while (ch != NULL);
36550Sstevel@tonic-gate 
36560Sstevel@tonic-gate 	/* Finished processing all chunks in packet */
36570Sstevel@tonic-gate 
36580Sstevel@tonic-gate nomorechunks:
36590Sstevel@tonic-gate 	/* SACK if necessary */
36600Sstevel@tonic-gate 	if (gotdata) {
36610Sstevel@tonic-gate 		(sctp->sctp_sack_toggle)++;
36620Sstevel@tonic-gate 		sctp_sack(sctp, dups);
36630Sstevel@tonic-gate 		dups = NULL;
36640Sstevel@tonic-gate 
36650Sstevel@tonic-gate 		if (!sctp->sctp_ack_timer_running) {
36660Sstevel@tonic-gate 			sctp->sctp_ack_timer_running = B_TRUE;
36670Sstevel@tonic-gate 			sctp_timer(sctp, sctp->sctp_ack_mp,
36680Sstevel@tonic-gate 			    MSEC_TO_TICK(sctp_deferred_ack_interval));
36690Sstevel@tonic-gate 		}
36700Sstevel@tonic-gate 	}
36710Sstevel@tonic-gate 
36720Sstevel@tonic-gate 	if (trysend) {
36730Sstevel@tonic-gate 		sctp_output(sctp);
36740Sstevel@tonic-gate 		if (sctp->sctp_cxmit_list != NULL)
36750Sstevel@tonic-gate 			sctp_wput_asconf(sctp, NULL);
36760Sstevel@tonic-gate 	}
36770Sstevel@tonic-gate 	/* If there is unsent data, make sure a timer is running */
36780Sstevel@tonic-gate 	if (sctp->sctp_unsent > 0 && !sctp->sctp_current->timer_running) {
36790Sstevel@tonic-gate 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
36800Sstevel@tonic-gate 		    sctp->sctp_current->rto);
36810Sstevel@tonic-gate 	}
36820Sstevel@tonic-gate 
36830Sstevel@tonic-gate done:
36840Sstevel@tonic-gate 	if (dups != NULL)
36850Sstevel@tonic-gate 		freeb(dups);
36860Sstevel@tonic-gate 	if (ipsec_mp != NULL)
36870Sstevel@tonic-gate 		freeb(ipsec_mp);
36880Sstevel@tonic-gate 	freemsg(mp);
36890Sstevel@tonic-gate 
36900Sstevel@tonic-gate 	if (wake_eager) {
36910Sstevel@tonic-gate 		/*
36920Sstevel@tonic-gate 		 * sctp points to newly created control block, need to
36930Sstevel@tonic-gate 		 * release it before exiting.  Before releasing it and
36940Sstevel@tonic-gate 		 * processing the sendq, need to grab a hold on it.
36950Sstevel@tonic-gate 		 * Otherwise, another thread can close it while processing
36960Sstevel@tonic-gate 		 * the sendq.
36970Sstevel@tonic-gate 		 */
36980Sstevel@tonic-gate 		SCTP_REFHOLD(sctp);
36990Sstevel@tonic-gate 		WAKE_SCTP(sctp);
37000Sstevel@tonic-gate 		sctp_process_sendq(sctp);
37010Sstevel@tonic-gate 		SCTP_REFRELE(sctp);
37020Sstevel@tonic-gate 	}
37030Sstevel@tonic-gate }
37040Sstevel@tonic-gate 
37050Sstevel@tonic-gate /*
37060Sstevel@tonic-gate  * Some amount of data got removed from rx q.
37070Sstevel@tonic-gate  * Check if we should send a window update.
37080Sstevel@tonic-gate  *
37090Sstevel@tonic-gate  * Due to way sctp_rwnd updates are made, ULP can give reports out-of-order.
37100Sstevel@tonic-gate  * To keep from dropping incoming data due to this, we only update
37110Sstevel@tonic-gate  * sctp_rwnd when if it's larger than what we've reported to peer earlier.
37120Sstevel@tonic-gate  */
37130Sstevel@tonic-gate void
37140Sstevel@tonic-gate sctp_recvd(sctp_t *sctp, int len)
37150Sstevel@tonic-gate {
37160Sstevel@tonic-gate 	int32_t old, new;
37170Sstevel@tonic-gate 
37180Sstevel@tonic-gate 	ASSERT(sctp != NULL);
37190Sstevel@tonic-gate 	RUN_SCTP(sctp);
37200Sstevel@tonic-gate 
37210Sstevel@tonic-gate 	if (len < sctp->sctp_rwnd) {
37220Sstevel@tonic-gate 		WAKE_SCTP(sctp);
37230Sstevel@tonic-gate 		return;
37240Sstevel@tonic-gate 	}
37250Sstevel@tonic-gate 	ASSERT(sctp->sctp_rwnd >= sctp->sctp_rxqueued);
37260Sstevel@tonic-gate 	old = sctp->sctp_rwnd - sctp->sctp_rxqueued;
37270Sstevel@tonic-gate 	new = len - sctp->sctp_rxqueued;
37280Sstevel@tonic-gate 	sctp->sctp_rwnd = len;
37290Sstevel@tonic-gate 
37300Sstevel@tonic-gate 	if (sctp->sctp_state >= SCTPS_ESTABLISHED &&
37310Sstevel@tonic-gate 	    ((old <= new >> 1) || (old < sctp->sctp_mss))) {
37320Sstevel@tonic-gate 		sctp->sctp_force_sack = 1;
37330Sstevel@tonic-gate 		BUMP_MIB(&sctp_mib, sctpOutWinUpdate);
37340Sstevel@tonic-gate 		sctp_sack(sctp, NULL);
37350Sstevel@tonic-gate 		old = 1;
37360Sstevel@tonic-gate 	} else {
37370Sstevel@tonic-gate 		old = 0;
37380Sstevel@tonic-gate 	}
37390Sstevel@tonic-gate 	WAKE_SCTP(sctp);
37400Sstevel@tonic-gate 	if (old > 0) {
37410Sstevel@tonic-gate 		sctp_process_sendq(sctp);
37420Sstevel@tonic-gate 	}
37430Sstevel@tonic-gate }
3744