xref: /titanic_50/usr/src/uts/common/inet/sctp/sctp_input.c (revision 24db46411fd54f70c35b94bb952eb7ba040e43b4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/systm.h>
31 #include <sys/stream.h>
32 #include <sys/cmn_err.h>
33 #include <sys/kmem.h>
34 #define	_SUN_TPI_VERSION 2
35 #include <sys/tihdr.h>
36 #include <sys/socket.h>
37 #include <sys/strsun.h>
38 #include <sys/strsubr.h>
39 
40 #include <netinet/in.h>
41 #include <netinet/ip6.h>
42 #include <netinet/tcp_seq.h>
43 #include <netinet/sctp.h>
44 
45 #include <inet/common.h>
46 #include <inet/ip.h>
47 #include <inet/ip6.h>
48 #include <inet/mib2.h>
49 #include <inet/ipclassifier.h>
50 #include <inet/ipp_common.h>
51 #include <inet/ipsec_impl.h>
52 #include <inet/sctp_ip.h>
53 
54 #include "sctp_impl.h"
55 #include "sctp_asconf.h"
56 #include "sctp_addr.h"
57 
58 static struct kmem_cache *sctp_kmem_set_cache;
59 
60 /*
61  * PR-SCTP comments.
62  *
63  * When we get a valid Forward TSN chunk, we check the fragment list for this
64  * SSN and preceeding SSNs free all them. Further, if this Forward TSN causes
65  * the next expected SSN to be present in the stream queue, we deliver any
66  * such stranded messages upstream. We also update the SACK info. appropriately.
67  * When checking for advancing the cumulative ack (in sctp_cumack()) we must
68  * check for abandoned chunks and messages. While traversing the tramsmit
69  * list if we come across an abandoned chunk, we can skip the message (i.e.
70  * take it out of the (re)transmit list) since this message, and hence this
71  * chunk, has been marked abandoned by sctp_rexmit(). If we come across an
72  * unsent chunk for a message this now abandoned we need to check if a
73  * Forward TSN needs to be sent, this could be a case where we deferred sending
74  * a Forward TSN in sctp_get_msg_to_send(). Further, after processing a
75  * SACK we check if the Advanced peer ack point can be moved ahead, i.e.
76  * if we can send a Forward TSN via sctp_check_abandoned_data().
77  */
78 void
79 sctp_free_set(sctp_set_t *s)
80 {
81 	sctp_set_t *p;
82 
83 	while (s) {
84 		p = s->next;
85 		kmem_cache_free(sctp_kmem_set_cache, s);
86 		s = p;
87 	}
88 }
89 
90 static void
91 sctp_ack_add(sctp_set_t **head, uint32_t tsn, int *num)
92 {
93 	sctp_set_t *p, *t;
94 
95 	if (head == NULL || num == NULL)
96 		return;
97 
98 	ASSERT(*num >= 0);
99 	ASSERT((*num == 0 && *head == NULL) || (*num > 0 && *head != NULL));
100 
101 	if (*head == NULL) {
102 		*head = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP);
103 		if (*head == NULL)
104 			return;
105 		(*head)->prev = (*head)->next = NULL;
106 		(*head)->begin = tsn;
107 		(*head)->end = tsn;
108 		*num = 1;
109 		return;
110 	}
111 
112 	ASSERT((*head)->prev == NULL);
113 
114 	/*
115 	 * Handle this special case here so we don't have to check
116 	 * for it each time in the loop.
117 	 */
118 	if (SEQ_LT(tsn + 1, (*head)->begin)) {
119 		/* add a new set, and move the head pointer */
120 		t = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP);
121 		if (t == NULL)
122 			return;
123 		t->next = *head;
124 		t->prev = NULL;
125 		(*head)->prev = t;
126 		t->begin = tsn;
127 		t->end = tsn;
128 		(*num)++;
129 		*head = t;
130 		return;
131 	}
132 
133 	/*
134 	 * We need to handle the following cases, where p points to
135 	 * the current set (as we walk through the loop):
136 	 *
137 	 * 1. tsn is entirely less than p; create a new set before p.
138 	 * 2. tsn borders p from less; coalesce p with tsn.
139 	 * 3. tsn is withing p; do nothing.
140 	 * 4. tsn borders p from greater; coalesce p with tsn.
141 	 * 4a. p may now border p->next from less; if so, coalesce those
142 	 *    two sets.
143 	 * 5. tsn is entirely greater then all sets; add a new set at
144 	 *    the end.
145 	 */
146 	for (p = *head; ; p = p->next) {
147 		if (SEQ_LT(tsn + 1, p->begin)) {
148 			/* 1: add a new set before p. */
149 			t = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP);
150 			if (t == NULL)
151 				return;
152 			t->next = p;
153 			t->prev = NULL;
154 			t->begin = tsn;
155 			t->end = tsn;
156 			if (p->prev) {
157 				t->prev = p->prev;
158 				p->prev->next = t;
159 			}
160 			p->prev = t;
161 			(*num)++;
162 			return;
163 		}
164 
165 		if ((tsn + 1) == p->begin) {
166 			/* 2: adjust p->begin */
167 			p->begin = tsn;
168 			return;
169 		}
170 
171 		if (SEQ_GEQ(tsn, p->begin) && SEQ_LEQ(tsn, p->end)) {
172 			/* 3; do nothing */
173 			return;
174 		}
175 
176 		if ((p->end + 1) == tsn) {
177 			/* 4; adjust p->end */
178 			p->end = tsn;
179 
180 			if (p->next != NULL && (tsn + 1) == p->next->begin) {
181 				/* 4a: coalesce p and p->next */
182 				t = p->next;
183 				p->end = t->end;
184 				p->next = t->next;
185 				if (t->next != NULL)
186 					t->next->prev = p;
187 				kmem_cache_free(sctp_kmem_set_cache, t);
188 				(*num)--;
189 			}
190 			return;
191 		}
192 
193 		if (p->next == NULL) {
194 			/* 5: add new set at the end */
195 			t = kmem_cache_alloc(sctp_kmem_set_cache, KM_NOSLEEP);
196 			if (t == NULL)
197 				return;
198 			t->next = NULL;
199 			t->prev = p;
200 			t->begin = tsn;
201 			t->end = tsn;
202 			p->next = t;
203 			(*num)++;
204 			return;
205 		}
206 
207 		if (SEQ_GT(tsn, p->end + 1))
208 			continue;
209 	}
210 }
211 
212 static void
213 sctp_ack_rem(sctp_set_t **head, uint32_t end, int *num)
214 {
215 	sctp_set_t *p, *t;
216 
217 	if (head == NULL || *head == NULL || num == NULL)
218 		return;
219 
220 	/* Nothing to remove */
221 	if (SEQ_LT(end, (*head)->begin))
222 		return;
223 
224 	/* Find out where to start removing sets */
225 	for (p = *head; p->next; p = p->next) {
226 		if (SEQ_LEQ(end, p->end))
227 			break;
228 	}
229 
230 	if (SEQ_LT(end, p->end) && SEQ_GEQ(end, p->begin)) {
231 		/* adjust p */
232 		p->begin = end + 1;
233 		/* all done */
234 		if (p == *head)
235 			return;
236 	} else if (SEQ_GEQ(end, p->end)) {
237 		/* remove this set too */
238 		p = p->next;
239 	}
240 
241 	/* unlink everything before this set */
242 	t = *head;
243 	*head = p;
244 	if (p != NULL && p->prev != NULL) {
245 		p->prev->next = NULL;
246 		p->prev = NULL;
247 	}
248 
249 	sctp_free_set(t);
250 
251 	/* recount the number of sets */
252 	*num = 0;
253 
254 	for (p = *head; p != NULL; p = p->next)
255 		(*num)++;
256 }
257 
258 void
259 sctp_sets_init()
260 {
261 	sctp_kmem_set_cache = kmem_cache_create("sctp_set_cache",
262 	    sizeof (sctp_set_t), 0, NULL, NULL, NULL, NULL,
263 	    NULL, 0);
264 }
265 
266 void
267 sctp_sets_fini()
268 {
269 	kmem_cache_destroy(sctp_kmem_set_cache);
270 }
271 
272 sctp_chunk_hdr_t *
273 sctp_first_chunk(uchar_t *rptr, ssize_t remaining)
274 {
275 	sctp_chunk_hdr_t *ch;
276 	uint16_t ch_len;
277 
278 	if (remaining < sizeof (*ch)) {
279 		return (NULL);
280 	}
281 
282 	ch = (sctp_chunk_hdr_t *)rptr;
283 	ch_len = ntohs(ch->sch_len);
284 
285 	if (ch_len < sizeof (*ch) || remaining < ch_len) {
286 		return (NULL);
287 	}
288 
289 	return (ch);
290 }
291 
292 sctp_chunk_hdr_t *
293 sctp_next_chunk(sctp_chunk_hdr_t *ch, ssize_t *remaining)
294 {
295 	int pad;
296 	uint16_t ch_len;
297 
298 	if (!ch) {
299 		return (NULL);
300 	}
301 
302 	ch_len = ntohs(ch->sch_len);
303 
304 	if ((pad = ch_len & (SCTP_ALIGN - 1)) != 0) {
305 		pad = SCTP_ALIGN - pad;
306 	}
307 
308 	*remaining -= (ch_len + pad);
309 	ch = (sctp_chunk_hdr_t *)((char *)ch + ch_len + pad);
310 
311 	return (sctp_first_chunk((uchar_t *)ch, *remaining));
312 }
313 
314 /*
315  * Attach ancillary data to a received SCTP segments.
316  * If the source address (fp) is not the primary, send up a
317  * unitdata_ind so recvfrom() can populate the msg_name field.
318  * If ancillary data is also requested, we append it to the
319  * unitdata_req. Otherwise, we just send up an optdata_ind.
320  */
321 static int
322 sctp_input_add_ancillary(sctp_t *sctp, mblk_t **mp, sctp_data_hdr_t *dcp,
323     sctp_faddr_t *fp, ip6_pkt_t *ipp)
324 {
325 	struct T_unitdata_ind	*tudi;
326 	int			optlen;
327 	int			hdrlen;
328 	uchar_t			*optptr;
329 	struct cmsghdr		*cmsg;
330 	mblk_t			*mp1;
331 	struct sockaddr_in6	sin_buf[1];
332 	struct sockaddr_in6	*sin6;
333 	struct sockaddr_in	*sin4;
334 	uint_t			addflag = 0;
335 
336 	sin4 = NULL;
337 	sin6 = NULL;
338 
339 	optlen = hdrlen = 0;
340 
341 	/* Figure out address size */
342 	if (sctp->sctp_ipversion == IPV4_VERSION) {
343 		sin4 = (struct sockaddr_in *)sin_buf;
344 		sin4->sin_family = AF_INET;
345 		sin4->sin_port = sctp->sctp_fport;
346 		IN6_V4MAPPED_TO_IPADDR(&fp->faddr, sin4->sin_addr.s_addr);
347 		hdrlen = sizeof (*tudi) + sizeof (*sin4);
348 	} else {
349 		sin6 = sin_buf;
350 		sin6->sin6_family = AF_INET6;
351 		sin6->sin6_port = sctp->sctp_fport;
352 		sin6->sin6_addr = fp->faddr;
353 		hdrlen = sizeof (*tudi) + sizeof (*sin6);
354 	}
355 
356 	/* If app asked to receive send / recv info */
357 	if (sctp->sctp_recvsndrcvinfo) {
358 		optlen += sizeof (*cmsg) + sizeof (struct sctp_sndrcvinfo);
359 		if (hdrlen == 0)
360 			hdrlen = sizeof (struct T_optdata_ind);
361 	}
362 
363 	if (sctp->sctp_ipv6_recvancillary == 0)
364 		goto noancillary;
365 
366 	if ((ipp->ipp_fields & IPPF_IFINDEX) &&
367 	    ipp->ipp_ifindex != sctp->sctp_recvifindex &&
368 	    (sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVPKTINFO)) {
369 		optlen += sizeof (*cmsg) + sizeof (struct in6_pktinfo);
370 		if (hdrlen == 0)
371 			hdrlen = sizeof (struct T_unitdata_ind);
372 		addflag |= SCTP_IPV6_RECVPKTINFO;
373 	}
374 	/* If app asked for hoplimit and it has changed ... */
375 	if ((ipp->ipp_fields & IPPF_HOPLIMIT) &&
376 	    ipp->ipp_hoplimit != sctp->sctp_recvhops &&
377 	    (sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVHOPLIMIT)) {
378 		optlen += sizeof (*cmsg) + sizeof (uint_t);
379 		if (hdrlen == 0)
380 			hdrlen = sizeof (struct T_unitdata_ind);
381 		addflag |= SCTP_IPV6_RECVHOPLIMIT;
382 	}
383 	/* If app asked for hopbyhop headers and it has changed ... */
384 	if ((sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVHOPOPTS) &&
385 	    ip_cmpbuf(sctp->sctp_hopopts, sctp->sctp_hopoptslen,
386 		(ipp->ipp_fields & IPPF_HOPOPTS),
387 		ipp->ipp_hopopts, ipp->ipp_hopoptslen)) {
388 		optlen += sizeof (*cmsg) + ipp->ipp_hopoptslen -
389 		    sctp->sctp_v6label_len;
390 		if (hdrlen == 0)
391 			hdrlen = sizeof (struct T_unitdata_ind);
392 		addflag |= SCTP_IPV6_RECVHOPOPTS;
393 		if (!ip_allocbuf((void **)&sctp->sctp_hopopts,
394 		    &sctp->sctp_hopoptslen,
395 		    (ipp->ipp_fields & IPPF_HOPOPTS),
396 		    ipp->ipp_hopopts, ipp->ipp_hopoptslen))
397 			return (-1);
398 	}
399 	/* If app asked for dst headers before routing headers ... */
400 	if ((sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVRTDSTOPTS) &&
401 	    ip_cmpbuf(sctp->sctp_rtdstopts, sctp->sctp_rtdstoptslen,
402 		(ipp->ipp_fields & IPPF_RTDSTOPTS),
403 		ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) {
404 		optlen += sizeof (*cmsg) + ipp->ipp_rtdstoptslen;
405 		if (hdrlen == 0)
406 			hdrlen = sizeof (struct T_unitdata_ind);
407 		addflag |= SCTP_IPV6_RECVRTDSTOPTS;
408 		if (!ip_allocbuf((void **)&sctp->sctp_rtdstopts,
409 		    &sctp->sctp_rtdstoptslen,
410 		    (ipp->ipp_fields & IPPF_RTDSTOPTS),
411 		    ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen))
412 			return (-1);
413 	}
414 	/* If app asked for routing headers and it has changed ... */
415 	if (sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVRTHDR) {
416 		if (ip_cmpbuf(sctp->sctp_rthdr, sctp->sctp_rthdrlen,
417 		    (ipp->ipp_fields & IPPF_RTHDR),
418 		    ipp->ipp_rthdr, ipp->ipp_rthdrlen)) {
419 			optlen += sizeof (*cmsg) + ipp->ipp_rthdrlen;
420 			if (hdrlen == 0)
421 				hdrlen = sizeof (struct T_unitdata_ind);
422 			addflag |= SCTP_IPV6_RECVRTHDR;
423 			if (!ip_allocbuf((void **)&sctp->sctp_rthdr,
424 			    &sctp->sctp_rthdrlen,
425 			    (ipp->ipp_fields & IPPF_RTHDR),
426 			    ipp->ipp_rthdr, ipp->ipp_rthdrlen))
427 				return (-1);
428 		}
429 	}
430 	/* If app asked for dest headers and it has changed ... */
431 	if ((sctp->sctp_ipv6_recvancillary & SCTP_IPV6_RECVDSTOPTS) &&
432 	    ip_cmpbuf(sctp->sctp_dstopts, sctp->sctp_dstoptslen,
433 		(ipp->ipp_fields & IPPF_DSTOPTS),
434 		ipp->ipp_dstopts, ipp->ipp_dstoptslen)) {
435 		optlen += sizeof (*cmsg) + ipp->ipp_dstoptslen;
436 		if (hdrlen == 0)
437 			hdrlen = sizeof (struct T_unitdata_ind);
438 		addflag |= SCTP_IPV6_RECVDSTOPTS;
439 		if (!ip_allocbuf((void **)&sctp->sctp_dstopts,
440 		    &sctp->sctp_dstoptslen,
441 		    (ipp->ipp_fields & IPPF_DSTOPTS),
442 		    ipp->ipp_dstopts, ipp->ipp_dstoptslen))
443 			return (-1);
444 	}
445 noancillary:
446 	/* Nothing to add */
447 	if (hdrlen == 0)
448 		return (-1);
449 
450 	mp1 = allocb(hdrlen + optlen + sizeof (void *), BPRI_MED);
451 	if (mp1 == NULL)
452 		return (-1);
453 	mp1->b_cont = *mp;
454 	*mp = mp1;
455 	mp1->b_rptr += sizeof (void *);  /* pointer worth of padding */
456 	mp1->b_wptr = mp1->b_rptr + hdrlen + optlen;
457 	DB_TYPE(mp1) = M_PROTO;
458 	tudi = (struct T_unitdata_ind *)mp1->b_rptr;
459 	tudi->PRIM_type = T_UNITDATA_IND;
460 	tudi->SRC_length = sin4 ? sizeof (*sin4) : sizeof (*sin6);
461 	tudi->SRC_offset = sizeof (*tudi);
462 	tudi->OPT_offset = sizeof (*tudi) + tudi->SRC_length;
463 	tudi->OPT_length = optlen;
464 	if (sin4) {
465 		bcopy(sin4, tudi + 1, sizeof (*sin4));
466 	} else {
467 		bcopy(sin6, tudi + 1, sizeof (*sin6));
468 	}
469 	optptr = (uchar_t *)tudi + tudi->OPT_offset;
470 
471 	if (sctp->sctp_recvsndrcvinfo) {
472 		/* XXX need backout method if memory allocation fails. */
473 		struct sctp_sndrcvinfo *sri;
474 
475 		cmsg = (struct cmsghdr *)optptr;
476 		cmsg->cmsg_level = IPPROTO_SCTP;
477 		cmsg->cmsg_type = SCTP_SNDRCV;
478 		cmsg->cmsg_len = sizeof (*cmsg) + sizeof (*sri);
479 		optptr += sizeof (*cmsg);
480 
481 		sri = (struct sctp_sndrcvinfo *)(cmsg + 1);
482 		ASSERT(OK_32PTR(sri));
483 		sri->sinfo_stream = ntohs(dcp->sdh_sid);
484 		sri->sinfo_ssn = ntohs(dcp->sdh_ssn);
485 		if (SCTP_DATA_GET_UBIT(dcp)) {
486 			sri->sinfo_flags = MSG_UNORDERED;
487 		} else {
488 			sri->sinfo_flags = 0;
489 		}
490 		sri->sinfo_ppid = dcp->sdh_payload_id;
491 		sri->sinfo_context = 0;
492 		sri->sinfo_timetolive = 0;
493 		sri->sinfo_tsn = ntohl(dcp->sdh_tsn);
494 		sri->sinfo_cumtsn = sctp->sctp_ftsn;
495 		sri->sinfo_assoc_id = 0;
496 
497 		optptr += sizeof (*sri);
498 	}
499 
500 	/*
501 	 * If app asked for pktinfo and the index has changed ...
502 	 * Note that the local address never changes for the connection.
503 	 */
504 	if (addflag & SCTP_IPV6_RECVPKTINFO) {
505 		struct in6_pktinfo *pkti;
506 
507 		cmsg = (struct cmsghdr *)optptr;
508 		cmsg->cmsg_level = IPPROTO_IPV6;
509 		cmsg->cmsg_type = IPV6_PKTINFO;
510 		cmsg->cmsg_len = sizeof (*cmsg) + sizeof (*pkti);
511 		optptr += sizeof (*cmsg);
512 
513 		pkti = (struct in6_pktinfo *)optptr;
514 		if (sctp->sctp_ipversion == IPV6_VERSION)
515 			pkti->ipi6_addr = sctp->sctp_ip6h->ip6_src;
516 		else
517 			IN6_IPADDR_TO_V4MAPPED(sctp->sctp_ipha->ipha_src,
518 			    &pkti->ipi6_addr);
519 		pkti->ipi6_ifindex = ipp->ipp_ifindex;
520 		optptr += sizeof (*pkti);
521 		ASSERT(OK_32PTR(optptr));
522 		/* Save as "last" value */
523 		sctp->sctp_recvifindex = ipp->ipp_ifindex;
524 	}
525 	/* If app asked for hoplimit and it has changed ... */
526 	if (addflag & SCTP_IPV6_RECVHOPLIMIT) {
527 		cmsg = (struct cmsghdr *)optptr;
528 		cmsg->cmsg_level = IPPROTO_IPV6;
529 		cmsg->cmsg_type = IPV6_HOPLIMIT;
530 		cmsg->cmsg_len = sizeof (*cmsg) + sizeof (uint_t);
531 		optptr += sizeof (*cmsg);
532 
533 		*(uint_t *)optptr = ipp->ipp_hoplimit;
534 		optptr += sizeof (uint_t);
535 		ASSERT(OK_32PTR(optptr));
536 		/* Save as "last" value */
537 		sctp->sctp_recvhops = ipp->ipp_hoplimit;
538 	}
539 	if (addflag & SCTP_IPV6_RECVHOPOPTS) {
540 		cmsg = (struct cmsghdr *)optptr;
541 		cmsg->cmsg_level = IPPROTO_IPV6;
542 		cmsg->cmsg_type = IPV6_HOPOPTS;
543 		cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_hopoptslen;
544 		optptr += sizeof (*cmsg);
545 
546 		bcopy(ipp->ipp_hopopts, optptr, ipp->ipp_hopoptslen);
547 		optptr += ipp->ipp_hopoptslen;
548 		ASSERT(OK_32PTR(optptr));
549 		/* Save as last value */
550 		ip_savebuf((void **)&sctp->sctp_hopopts,
551 		    &sctp->sctp_hopoptslen,
552 		    (ipp->ipp_fields & IPPF_HOPOPTS),
553 		    ipp->ipp_hopopts, ipp->ipp_hopoptslen);
554 	}
555 	if (addflag & SCTP_IPV6_RECVRTDSTOPTS) {
556 		cmsg = (struct cmsghdr *)optptr;
557 		cmsg->cmsg_level = IPPROTO_IPV6;
558 		cmsg->cmsg_type = IPV6_RTHDRDSTOPTS;
559 		cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_rtdstoptslen;
560 		optptr += sizeof (*cmsg);
561 
562 		bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen);
563 		optptr += ipp->ipp_rtdstoptslen;
564 		ASSERT(OK_32PTR(optptr));
565 		/* Save as last value */
566 		ip_savebuf((void **)&sctp->sctp_rtdstopts,
567 		    &sctp->sctp_rtdstoptslen,
568 		    (ipp->ipp_fields & IPPF_RTDSTOPTS),
569 		    ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen);
570 	}
571 	if (addflag & SCTP_IPV6_RECVRTHDR) {
572 		cmsg = (struct cmsghdr *)optptr;
573 		cmsg->cmsg_level = IPPROTO_IPV6;
574 		cmsg->cmsg_type = IPV6_RTHDR;
575 		cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_rthdrlen;
576 		optptr += sizeof (*cmsg);
577 
578 		bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen);
579 		optptr += ipp->ipp_rthdrlen;
580 		ASSERT(OK_32PTR(optptr));
581 		/* Save as last value */
582 		ip_savebuf((void **)&sctp->sctp_rthdr,
583 		    &sctp->sctp_rthdrlen,
584 		    (ipp->ipp_fields & IPPF_RTHDR),
585 		    ipp->ipp_rthdr, ipp->ipp_rthdrlen);
586 	}
587 	if (addflag & SCTP_IPV6_RECVDSTOPTS) {
588 		cmsg = (struct cmsghdr *)optptr;
589 		cmsg->cmsg_level = IPPROTO_IPV6;
590 		cmsg->cmsg_type = IPV6_DSTOPTS;
591 		cmsg->cmsg_len = sizeof (*cmsg) + ipp->ipp_dstoptslen;
592 		optptr += sizeof (*cmsg);
593 
594 		bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen);
595 		optptr += ipp->ipp_dstoptslen;
596 		ASSERT(OK_32PTR(optptr));
597 		/* Save as last value */
598 		ip_savebuf((void **)&sctp->sctp_dstopts,
599 		    &sctp->sctp_dstoptslen,
600 		    (ipp->ipp_fields & IPPF_DSTOPTS),
601 		    ipp->ipp_dstopts, ipp->ipp_dstoptslen);
602 	}
603 
604 	ASSERT(optptr == mp1->b_wptr);
605 
606 	return (0);
607 }
608 
609 void
610 sctp_free_reass(sctp_instr_t *sip)
611 {
612 	mblk_t *mp, *mpnext, *mctl;
613 
614 	for (mp = sip->istr_reass; mp != NULL; mp = mpnext) {
615 		mpnext = mp->b_next;
616 		mp->b_next = NULL;
617 		mp->b_prev = NULL;
618 		if (DB_TYPE(mp) == M_CTL) {
619 			mctl = mp;
620 			ASSERT(mp->b_cont != NULL);
621 			mp = mp->b_cont;
622 			mctl->b_cont = NULL;
623 			freeb(mctl);
624 		}
625 		freemsg(mp);
626 	}
627 }
628 
629 /*
630  * If the series of data fragments of which dmp is a part is successfully
631  * reassembled, the first mblk in the series is returned. dc is adjusted
632  * to point at the data chunk in the lead mblk, and b_rptr also points to
633  * the data chunk; the following mblk's b_rptr's point at the actual payload.
634  *
635  * If the series is not yet reassembled, NULL is returned. dc is not changed.
636  * XXX should probably move this up into the state machine.
637  */
638 
639 /* Fragment list for un-ordered messages. Partial delivery is not supported */
640 static mblk_t *
641 sctp_uodata_frag(sctp_t *sctp, mblk_t *dmp, sctp_data_hdr_t **dc)
642 {
643 	mblk_t		*hmp;
644 	mblk_t		*begin = NULL;
645 	mblk_t		*end = NULL;
646 	sctp_data_hdr_t	*qdc;
647 	uint32_t	ntsn;
648 	uint32_t	tsn = ntohl((*dc)->sdh_tsn);
649 #ifdef	DEBUG
650 	mblk_t		*mp1;
651 #endif
652 
653 	/* First frag. */
654 	if (sctp->sctp_uo_frags == NULL) {
655 		sctp->sctp_uo_frags = dmp;
656 		return (NULL);
657 	}
658 	hmp = sctp->sctp_uo_frags;
659 	/*
660 	 * Insert the segment according to the TSN, fragmented unordered
661 	 * chunks are sequenced by TSN.
662 	 */
663 	while (hmp != NULL) {
664 		qdc = (sctp_data_hdr_t *)hmp->b_rptr;
665 		ntsn = ntohl(qdc->sdh_tsn);
666 		if (SEQ_GT(ntsn, tsn)) {
667 			if (hmp->b_prev == NULL) {
668 				dmp->b_next = hmp;
669 				hmp->b_prev = dmp;
670 				sctp->sctp_uo_frags = dmp;
671 			} else {
672 				dmp->b_next = hmp;
673 				dmp->b_prev = hmp->b_prev;
674 				hmp->b_prev->b_next = dmp;
675 				hmp->b_prev = dmp;
676 			}
677 			break;
678 		}
679 		if (hmp->b_next == NULL) {
680 			hmp->b_next = dmp;
681 			dmp->b_prev = hmp;
682 			break;
683 		}
684 		hmp = hmp->b_next;
685 	}
686 	/* check if we completed a msg */
687 	if (SCTP_DATA_GET_BBIT(*dc)) {
688 		begin = dmp;
689 	} else if (SCTP_DATA_GET_EBIT(*dc)) {
690 		end = dmp;
691 	}
692 	/*
693 	 * We walk consecutive TSNs backwards till we get a seg. with
694 	 * the B bit
695 	 */
696 	if (begin == NULL) {
697 		for (hmp = dmp->b_prev; hmp != NULL; hmp = hmp->b_prev) {
698 			qdc = (sctp_data_hdr_t *)hmp->b_rptr;
699 			ntsn = ntohl(qdc->sdh_tsn);
700 			if ((int32_t)(tsn - ntsn) > 1) {
701 				return (NULL);
702 			}
703 			if (SCTP_DATA_GET_BBIT(qdc)) {
704 				begin = hmp;
705 				break;
706 			}
707 			tsn = ntsn;
708 		}
709 	}
710 	tsn = ntohl((*dc)->sdh_tsn);
711 	/*
712 	 * We walk consecutive TSNs till we get a seg. with the E bit
713 	 */
714 	if (end == NULL) {
715 		for (hmp = dmp->b_next; hmp != NULL; hmp = hmp->b_next) {
716 			qdc = (sctp_data_hdr_t *)hmp->b_rptr;
717 			ntsn = ntohl(qdc->sdh_tsn);
718 			if ((int32_t)(ntsn - tsn) > 1) {
719 				return (NULL);
720 			}
721 			if (SCTP_DATA_GET_EBIT(qdc)) {
722 				end = hmp;
723 				break;
724 			}
725 			tsn = ntsn;
726 		}
727 	}
728 	if (begin == NULL || end == NULL) {
729 		return (NULL);
730 	}
731 	/* Got one!, Remove the msg from the list */
732 	if (sctp->sctp_uo_frags == begin) {
733 		ASSERT(begin->b_prev == NULL);
734 		sctp->sctp_uo_frags = end->b_next;
735 		if (end->b_next != NULL)
736 			end->b_next->b_prev = NULL;
737 	} else {
738 		begin->b_prev->b_next = end->b_next;
739 		if (end->b_next != NULL)
740 			end->b_next->b_prev = begin->b_prev;
741 	}
742 	begin->b_prev = NULL;
743 	end->b_next = NULL;
744 
745 	/*
746 	 * Null out b_next and b_prev and chain using b_cont.
747 	 */
748 	dmp = end = begin;
749 	hmp = begin->b_next;
750 	*dc = (sctp_data_hdr_t *)begin->b_rptr;
751 	begin->b_next = NULL;
752 	while (hmp != NULL) {
753 		qdc = (sctp_data_hdr_t *)hmp->b_rptr;
754 		hmp->b_rptr = (uchar_t *)(qdc + 1);
755 		end = hmp->b_next;
756 		dmp->b_cont = hmp;
757 		dmp = hmp;
758 
759 		if (end != NULL)
760 			hmp->b_next = NULL;
761 		hmp->b_prev = NULL;
762 		hmp = end;
763 	}
764 	BUMP_LOCAL(sctp->sctp_reassmsgs);
765 #ifdef	DEBUG
766 	mp1 = begin;
767 	while (mp1 != NULL) {
768 		ASSERT(mp1->b_next == NULL);
769 		ASSERT(mp1->b_prev == NULL);
770 		mp1 = mp1->b_cont;
771 	}
772 #endif
773 	return (begin);
774 }
775 
776 /*
777  * Try partial delivery.
778  */
779 static mblk_t *
780 sctp_try_partial_delivery(sctp_t *sctp, mblk_t *hmp, sctp_reass_t *srp,
781     sctp_data_hdr_t **dc)
782 {
783 	mblk_t		*first_mp;
784 	mblk_t		*mp;
785 	mblk_t		*dmp;
786 	mblk_t		*qmp;
787 	mblk_t		*prev;
788 	sctp_data_hdr_t	*qdc;
789 	uint32_t	tsn;
790 
791 	ASSERT(DB_TYPE(hmp) == M_CTL);
792 
793 	dprint(4, ("trypartial: got=%d, needed=%d\n",
794 	    (int)(srp->got), (int)(srp->needed)));
795 
796 	first_mp = hmp->b_cont;
797 	mp = first_mp;
798 	qdc = (sctp_data_hdr_t *)mp->b_rptr;
799 
800 	ASSERT(SCTP_DATA_GET_BBIT(qdc) && srp->hasBchunk);
801 
802 	tsn = ntohl(qdc->sdh_tsn) + 1;
803 
804 	/*
805 	 * This loop has two exit conditions: the
806 	 * end of received chunks has been reached, or
807 	 * there is a break in the sequence. We want
808 	 * to chop the reassembly list as follows (the
809 	 * numbers are TSNs):
810 	 *   10 -> 11 -> 	(end of chunks)
811 	 *   10 -> 11 -> | 13   (break in sequence)
812 	 */
813 	prev = mp;
814 	mp = mp->b_cont;
815 	while (mp != NULL) {
816 		qdc = (sctp_data_hdr_t *)mp->b_rptr;
817 		if (ntohl(qdc->sdh_tsn) != tsn)
818 			break;
819 		prev = mp;
820 		mp = mp->b_cont;
821 		tsn++;
822 	}
823 	/*
824 	 * We are sending all the fragments upstream, we have to retain
825 	 * the srp info for further fragments.
826 	 */
827 	if (mp == NULL) {
828 		dmp = hmp->b_cont;
829 		hmp->b_cont = NULL;
830 		srp->nexttsn = tsn;
831 		srp->msglen = 0;
832 		srp->needed = 0;
833 		srp->got = 0;
834 		srp->partial_delivered = B_TRUE;
835 		srp->tail = NULL;
836 	} else {
837 		dmp = hmp->b_cont;
838 		hmp->b_cont = mp;
839 	}
840 	srp->hasBchunk = B_FALSE;
841 	/*
842 	 * mp now points at the last chunk in the sequence,
843 	 * and prev points to mp's previous in the list.
844 	 * We chop the list at prev, and convert mp into the
845 	 * new list head by setting the B bit. Subsequence
846 	 * fragment deliveries will follow the normal reassembly
847 	 * path.
848 	 */
849 	prev->b_cont = NULL;
850 	srp->partial_delivered = B_TRUE;
851 
852 	dprint(4, ("trypartial: got some, got=%d, needed=%d\n",
853 	    (int)(srp->got), (int)(srp->needed)));
854 
855 	/*
856 	 * Adjust all mblk's except the lead so their rptr's point to the
857 	 * payload. sctp_data_chunk() will need to process the lead's
858 	 * data chunk section, so leave it's rptr pointing at the data chunk.
859 	 */
860 	*dc = (sctp_data_hdr_t *)dmp->b_rptr;
861 	if (srp->tail != NULL) {
862 		srp->got--;
863 		ASSERT(srp->got != 0);
864 		if (srp->needed != 0) {
865 			srp->needed--;
866 			ASSERT(srp->needed != 0);
867 		}
868 		srp->msglen -= ntohs((*dc)->sdh_len);
869 	}
870 	for (qmp = dmp->b_cont; qmp != NULL; qmp = qmp->b_cont) {
871 		qdc = (sctp_data_hdr_t *)qmp->b_rptr;
872 		qmp->b_rptr = (uchar_t *)(qdc + 1);
873 
874 		/*
875 		 * Deduct the balance from got and needed here, now that
876 		 * we know we are actually delivering these data.
877 		 */
878 		if (srp->tail != NULL) {
879 			srp->got--;
880 			ASSERT(srp->got != 0);
881 			if (srp->needed != 0) {
882 				srp->needed--;
883 				ASSERT(srp->needed != 0);
884 			}
885 			srp->msglen -= ntohs(qdc->sdh_len);
886 		}
887 	}
888 	ASSERT(srp->msglen == 0);
889 	BUMP_LOCAL(sctp->sctp_reassmsgs);
890 
891 	return (dmp);
892 }
893 
894 /*
895  * Fragment list for ordered messages.
896  * If no error occures, error is set to 0. If we run out of memory, error
897  * is set to 1. If the peer commits a fatal error (like using different
898  * sequence numbers for the same data fragment series), the association is
899  * aborted and error is set to 2. tpfinished indicates whether we have
900  * assembled a complete message, this is used in sctp_data_chunk() to
901  * see if we can try to send any queued message for this stream.
902  */
903 static mblk_t *
904 sctp_data_frag(sctp_t *sctp, mblk_t *dmp, sctp_data_hdr_t **dc, int *error,
905     sctp_instr_t *sip, boolean_t *tpfinished)
906 {
907 	mblk_t		*hmp;
908 	mblk_t		*pmp;
909 	mblk_t		*qmp;
910 	mblk_t		*first_mp;
911 	sctp_reass_t	*srp;
912 	sctp_data_hdr_t	*qdc;
913 	sctp_data_hdr_t	*bdc;
914 	sctp_data_hdr_t	*edc;
915 	uint32_t	tsn;
916 	uint16_t	fraglen = 0;
917 
918 	*error = 0;
919 
920 	/* find the reassembly queue for this data chunk */
921 	hmp = qmp = sip->istr_reass;
922 	for (; hmp != NULL; hmp = hmp->b_next) {
923 		srp = (sctp_reass_t *)DB_BASE(hmp);
924 		if (ntohs((*dc)->sdh_ssn) == srp->ssn)
925 			goto foundit;
926 		else if (SSN_GT(srp->ssn, ntohs((*dc)->sdh_ssn)))
927 			break;
928 		qmp = hmp;
929 	}
930 
931 	/*
932 	 * Allocate a M_CTL that will contain information about this
933 	 * fragmented message.
934 	 */
935 	if ((pmp = allocb(sizeof (*srp), BPRI_MED)) == NULL) {
936 		*error = 1;
937 		return (NULL);
938 	}
939 	DB_TYPE(pmp) = M_CTL;
940 	srp = (sctp_reass_t *)DB_BASE(pmp);
941 	pmp->b_cont = dmp;
942 
943 	if (hmp != NULL) {
944 		if (sip->istr_reass == hmp) {
945 			sip->istr_reass = pmp;
946 			pmp->b_next = hmp;
947 			pmp->b_prev = NULL;
948 			hmp->b_prev = pmp;
949 		} else {
950 			qmp->b_next = pmp;
951 			pmp->b_prev = qmp;
952 			pmp->b_next = hmp;
953 			hmp->b_prev = pmp;
954 		}
955 	} else {
956 		/* make a new reass head and stick it on the end */
957 		if (sip->istr_reass == NULL) {
958 			sip->istr_reass = pmp;
959 			pmp->b_prev = NULL;
960 		} else {
961 			qmp->b_next = pmp;
962 			pmp->b_prev = qmp;
963 		}
964 		pmp->b_next = NULL;
965 	}
966 	srp->partial_delivered = B_FALSE;
967 	srp->ssn = ntohs((*dc)->sdh_ssn);
968 empty_srp:
969 	srp->needed = 0;
970 	srp->got = 1;
971 	srp->tail = dmp;
972 	if (SCTP_DATA_GET_BBIT(*dc)) {
973 		srp->msglen = ntohs((*dc)->sdh_len);
974 		srp->nexttsn = ntohl((*dc)->sdh_tsn) + 1;
975 		srp->hasBchunk = B_TRUE;
976 	} else if (srp->partial_delivered &&
977 	    srp->nexttsn == ntohl((*dc)->sdh_tsn)) {
978 		SCTP_DATA_SET_BBIT(*dc);
979 		/* Last fragment */
980 		if (SCTP_DATA_GET_EBIT(*dc)) {
981 			srp->needed = 1;
982 			goto frag_done;
983 		}
984 		srp->hasBchunk = B_TRUE;
985 		srp->msglen = ntohs((*dc)->sdh_len);
986 		srp->nexttsn++;
987 	}
988 	return (NULL);
989 foundit:
990 	/*
991 	 * else already have a reassembly queue. Insert the new data chunk
992 	 * in the reassemble queue. Try the tail first, on the assumption
993 	 * that the fragments are coming in in order.
994 	 */
995 	qmp = srp->tail;
996 
997 	/*
998 	 * This means the message was partially delivered.
999 	 */
1000 	if (qmp == NULL) {
1001 		ASSERT(srp->got == 0 && srp->needed == 0 &&
1002 		    srp->partial_delivered);
1003 		ASSERT(hmp->b_cont == NULL);
1004 		hmp->b_cont = dmp;
1005 		goto empty_srp;
1006 	}
1007 	qdc = (sctp_data_hdr_t *)qmp->b_rptr;
1008 	ASSERT(qmp->b_cont == NULL);
1009 
1010 	/* XXXIs it fine to do this just here? */
1011 	if ((*dc)->sdh_sid != qdc->sdh_sid) {
1012 		/* our peer is fatally confused; XXX abort the assc */
1013 		*error = 2;
1014 		return (NULL);
1015 	}
1016 	if (SEQ_GT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) {
1017 		qmp->b_cont = dmp;
1018 		srp->tail = dmp;
1019 		dmp->b_cont = NULL;
1020 		if (srp->hasBchunk && srp->nexttsn == ntohl((*dc)->sdh_tsn)) {
1021 			srp->msglen += ntohs((*dc)->sdh_len);
1022 			srp->nexttsn++;
1023 		}
1024 		goto inserted;
1025 	}
1026 
1027 	/* Next check for insertion at the beginning */
1028 	qmp = hmp->b_cont;
1029 	qdc = (sctp_data_hdr_t *)qmp->b_rptr;
1030 	if (SEQ_LT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) {
1031 		dmp->b_cont = qmp;
1032 		hmp->b_cont = dmp;
1033 		if (SCTP_DATA_GET_BBIT(*dc)) {
1034 			srp->hasBchunk = B_TRUE;
1035 			srp->nexttsn = ntohl((*dc)->sdh_tsn);
1036 		}
1037 		goto preinserted;
1038 	}
1039 
1040 	/* Insert somewhere in the middle */
1041 	for (;;) {
1042 		/* Tail check above should have caught this */
1043 		ASSERT(qmp->b_cont != NULL);
1044 
1045 		qdc = (sctp_data_hdr_t *)qmp->b_cont->b_rptr;
1046 		if (SEQ_LT(ntohl((*dc)->sdh_tsn), ntohl(qdc->sdh_tsn))) {
1047 			/* insert here */
1048 			dmp->b_cont = qmp->b_cont;
1049 			qmp->b_cont = dmp;
1050 			break;
1051 		}
1052 		qmp = qmp->b_cont;
1053 	}
1054 preinserted:
1055 	if (!srp->hasBchunk || ntohl((*dc)->sdh_tsn) != srp->nexttsn)
1056 		goto inserted;
1057 	/*
1058 	 * fraglen contains the length of consecutive chunks of fragments.
1059 	 * starting from the chunk inserted recently.
1060 	 */
1061 	tsn = srp->nexttsn;
1062 	for (qmp = dmp; qmp != NULL; qmp = qmp->b_cont) {
1063 		qdc = (sctp_data_hdr_t *)qmp->b_rptr;
1064 		if (tsn != ntohl(qdc->sdh_tsn))
1065 			break;
1066 		fraglen += ntohs(qdc->sdh_len);
1067 		tsn++;
1068 	}
1069 	srp->nexttsn = tsn;
1070 	srp->msglen += fraglen;
1071 inserted:
1072 	srp->got++;
1073 	first_mp = hmp->b_cont;
1074 	if (srp->needed == 0) {
1075 		/* check if we have the first and last fragments */
1076 		bdc = (sctp_data_hdr_t *)first_mp->b_rptr;
1077 		edc = (sctp_data_hdr_t *)srp->tail->b_rptr;
1078 
1079 		/* calculate how many fragments are needed, if possible  */
1080 		if (SCTP_DATA_GET_BBIT(bdc) && SCTP_DATA_GET_EBIT(edc)) {
1081 			srp->needed = ntohl(edc->sdh_tsn) -
1082 			    ntohl(bdc->sdh_tsn) + 1;
1083 		}
1084 	}
1085 
1086 	/*
1087 	 * Try partial delivery if the message length has exceeded the
1088 	 * partial delivery point. Only do this if we can immediately
1089 	 * deliver the partially assembled message, and only partially
1090 	 * deliver one message at a time (i.e. messages cannot be
1091 	 * intermixed arriving at the upper layer). A simple way to
1092 	 * enforce this is to only try partial delivery if this TSN is
1093 	 * the next expected TSN. Partial Delivery not supported
1094 	 * for un-ordered message.
1095 	 */
1096 	if (srp->needed != srp->got) {
1097 		dmp = NULL;
1098 		if (ntohl((*dc)->sdh_tsn) == sctp->sctp_ftsn &&
1099 		    srp->msglen >= sctp->sctp_pd_point) {
1100 			dmp = sctp_try_partial_delivery(sctp, hmp, srp, dc);
1101 			*tpfinished = B_FALSE;
1102 		}
1103 		return (dmp);
1104 	}
1105 frag_done:
1106 	/*
1107 	 * else reassembly done; prepare the data for delivery.
1108 	 * First unlink hmp from the ssn list.
1109 	 */
1110 	if (sip->istr_reass == hmp) {
1111 		sip->istr_reass = hmp->b_next;
1112 		if (hmp->b_next)
1113 			hmp->b_next->b_prev = NULL;
1114 	} else {
1115 		ASSERT(hmp->b_prev != NULL);
1116 		hmp->b_prev->b_next = hmp->b_next;
1117 		if (hmp->b_next)
1118 			hmp->b_next->b_prev = hmp->b_prev;
1119 	}
1120 
1121 	/*
1122 	 * Using b_prev and b_next was a little sinful, but OK since
1123 	 * this mblk is never put*'d. However, freeb() will still
1124 	 * ASSERT that they are unused, so we need to NULL them out now.
1125 	 */
1126 	hmp->b_next = NULL;
1127 	hmp->b_prev = NULL;
1128 	dmp = hmp;
1129 	dmp = dmp->b_cont;
1130 	hmp->b_cont = NULL;
1131 	freeb(hmp);
1132 	*tpfinished = B_TRUE;
1133 
1134 	/*
1135 	 * Adjust all mblk's except the lead so their rptr's point to the
1136 	 * payload. sctp_data_chunk() will need to process the lead's
1137 	 * data chunk section, so leave it's rptr pointing at the data chunk.
1138 	 */
1139 	*dc = (sctp_data_hdr_t *)dmp->b_rptr;
1140 	for (qmp = dmp->b_cont; qmp != NULL; qmp = qmp->b_cont) {
1141 		qdc = (sctp_data_hdr_t *)qmp->b_rptr;
1142 		qmp->b_rptr = (uchar_t *)(qdc + 1);
1143 	}
1144 	BUMP_LOCAL(sctp->sctp_reassmsgs);
1145 
1146 	return (dmp);
1147 }
1148 static void
1149 sctp_add_dup(uint32_t tsn, mblk_t **dups)
1150 {
1151 	mblk_t *mp;
1152 	size_t bsize = SCTP_DUP_MBLK_SZ * sizeof (tsn);
1153 
1154 	if (dups == NULL) {
1155 		return;
1156 	}
1157 
1158 	/* first time? */
1159 	if (*dups == NULL) {
1160 		*dups = allocb(bsize, BPRI_MED);
1161 		if (*dups == NULL) {
1162 			return;
1163 		}
1164 	}
1165 
1166 	mp = *dups;
1167 	if ((mp->b_wptr - mp->b_rptr) >= bsize) {
1168 		/* maximum reached */
1169 		return;
1170 	}
1171 
1172 	/* add the duplicate tsn */
1173 	bcopy(&tsn, mp->b_wptr, sizeof (tsn));
1174 	mp->b_wptr += sizeof (tsn);
1175 	ASSERT((mp->b_wptr - mp->b_rptr) <= bsize);
1176 }
1177 
1178 static void
1179 sctp_data_chunk(sctp_t *sctp, sctp_chunk_hdr_t *ch, mblk_t *mp, mblk_t **dups,
1180     sctp_faddr_t *fp, ip6_pkt_t *ipp)
1181 {
1182 	sctp_data_hdr_t *dc;
1183 	mblk_t *dmp, *pmp;
1184 	mblk_t *errmp;
1185 	sctp_instr_t *instr;
1186 	int ubit;
1187 	int isfrag;
1188 	uint16_t ssn;
1189 	uint32_t oftsn;
1190 	boolean_t can_deliver = B_TRUE;
1191 	uint32_t tsn;
1192 	int dlen;
1193 	boolean_t tpfinished = B_TRUE;
1194 	int32_t new_rwnd;
1195 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1196 
1197 	/* The following are used multiple times, so we inline them */
1198 #define	SCTP_ACK_IT(sctp, tsn)						\
1199 	if (tsn == sctp->sctp_ftsn) {					\
1200 		dprint(2, ("data_chunk: acking next %x\n", tsn));	\
1201 		(sctp)->sctp_ftsn++;					\
1202 		if ((sctp)->sctp_sack_gaps > 0)				\
1203 			(sctp)->sctp_force_sack = 1;			\
1204 	} else if (SEQ_GT(tsn, sctp->sctp_ftsn)) {			\
1205 		/* Got a gap; record it */				\
1206 		dprint(2, ("data_chunk: acking gap %x\n", tsn));	\
1207 		sctp_ack_add(&sctp->sctp_sack_info, tsn,		\
1208 		    &sctp->sctp_sack_gaps);				\
1209 		sctp->sctp_force_sack = 1;				\
1210 	}
1211 
1212 	errmp = NULL;
1213 	dmp = NULL;
1214 
1215 	dc = (sctp_data_hdr_t *)ch;
1216 	tsn = ntohl(dc->sdh_tsn);
1217 
1218 	dprint(3, ("sctp_data_chunk: mp=%p tsn=%x\n", (void *)mp, tsn));
1219 
1220 	/* Check for duplicates */
1221 	if (SEQ_LT(tsn, sctp->sctp_ftsn)) {
1222 		dprint(4, ("sctp_data_chunk: dropping duplicate\n"));
1223 		sctp->sctp_force_sack = 1;
1224 		sctp_add_dup(dc->sdh_tsn, dups);
1225 		return;
1226 	}
1227 
1228 	if (sctp->sctp_sack_info != NULL) {
1229 		sctp_set_t *sp;
1230 
1231 		for (sp = sctp->sctp_sack_info; sp; sp = sp->next) {
1232 			if (SEQ_GEQ(tsn, sp->begin) && SEQ_LEQ(tsn, sp->end)) {
1233 				dprint(4,
1234 				("sctp_data_chunk: dropping dup > cumtsn\n"));
1235 				sctp->sctp_force_sack = 1;
1236 				sctp_add_dup(dc->sdh_tsn, dups);
1237 				return;
1238 			}
1239 		}
1240 	}
1241 
1242 	/* We cannot deliver anything up now but we still need to handle it. */
1243 	if (SCTP_IS_DETACHED(sctp)) {
1244 		BUMP_MIB(&sctps->sctps_mib, sctpInClosed);
1245 		can_deliver = B_FALSE;
1246 	}
1247 
1248 	dlen = ntohs(dc->sdh_len) - sizeof (*dc);
1249 
1250 	/* Check for buffer space */
1251 	if (sctp->sctp_rwnd - sctp->sctp_rxqueued < dlen) {
1252 		/* Drop and SACK, but don't advance the cumulative TSN. */
1253 		sctp->sctp_force_sack = 1;
1254 		dprint(0, ("sctp_data_chunk: exceed rwnd %d rxqueued %d "
1255 		    "dlen %d ssn %d tsn %x\n", sctp->sctp_rwnd,
1256 		    sctp->sctp_rxqueued, dlen, ntohs(dc->sdh_ssn),
1257 		    ntohl(dc->sdh_tsn)));
1258 		return;
1259 	}
1260 
1261 	if (ntohs(dc->sdh_sid) >= sctp->sctp_num_istr) {
1262 		uint16_t	inval_parm[2];
1263 
1264 		inval_parm[0] = dc->sdh_sid;
1265 		/* RESERVED to be ignored at the receiving end */
1266 		inval_parm[1] = 0;
1267 		/* ack and drop it */
1268 		errmp = sctp_make_err(sctp, SCTP_ERR_BAD_SID,
1269 		    (char *)inval_parm, sizeof (inval_parm));
1270 		SCTP_ACK_IT(sctp, tsn);
1271 		if (errmp != NULL)
1272 			sctp_send_err(sctp, errmp, NULL);
1273 		return;
1274 	}
1275 
1276 	ubit = SCTP_DATA_GET_UBIT(dc);
1277 	ASSERT(sctp->sctp_instr != NULL);
1278 	instr = &sctp->sctp_instr[ntohs(dc->sdh_sid)];
1279 	/* Initialize the stream, if not yet used */
1280 	if (instr->sctp == NULL)
1281 		instr->sctp = sctp;
1282 
1283 	isfrag = !(SCTP_DATA_GET_BBIT(dc) && SCTP_DATA_GET_EBIT(dc));
1284 	ssn = ntohs(dc->sdh_ssn);
1285 
1286 	dmp = dupb(mp);
1287 	if (dmp == NULL) {
1288 		/* drop it and don't ack it, causing the peer to retransmit */
1289 		return;
1290 	}
1291 	dmp->b_wptr = (uchar_t *)ch + ntohs(ch->sch_len);
1292 
1293 	sctp->sctp_rxqueued += dlen;
1294 
1295 	oftsn = sctp->sctp_ftsn;
1296 
1297 	if (isfrag) {
1298 		int error = 0;
1299 
1300 		/* fragmented data chunk */
1301 		dmp->b_rptr = (uchar_t *)dc;
1302 		if (ubit) {
1303 			dmp = sctp_uodata_frag(sctp, dmp, &dc);
1304 #if	DEBUG
1305 			if (dmp != NULL) {
1306 				ASSERT(instr ==
1307 				    &sctp->sctp_instr[ntohs(dc->sdh_sid)]);
1308 			}
1309 #endif
1310 		} else {
1311 			dmp = sctp_data_frag(sctp, dmp, &dc, &error, instr,
1312 			    &tpfinished);
1313 		}
1314 		if (error != 0) {
1315 			sctp->sctp_rxqueued -= dlen;
1316 			if (error == 1) {
1317 				/*
1318 				 * out of memory; don't ack it so
1319 				 * the peer retransmits
1320 				 */
1321 				return;
1322 			} else if (error == 2) {
1323 				/*
1324 				 * fatal error (i.e. peer used different
1325 				 * ssn's for same fragmented data) --
1326 				 * the association has been aborted.
1327 				 * XXX need to return errval so state
1328 				 * machine can also abort processing.
1329 				 */
1330 				dprint(0, ("error 2: must not happen!\n"));
1331 				return;
1332 			}
1333 		}
1334 
1335 		if (dmp == NULL) {
1336 			/*
1337 			 * Can't process this data now, but the cumulative
1338 			 * TSN may be advanced, so do the checks at done.
1339 			 */
1340 			SCTP_ACK_IT(sctp, tsn);
1341 			goto done;
1342 		}
1343 	}
1344 
1345 	if (!ubit && !isfrag && ssn != instr->nextseq) {
1346 		/* Adjust rptr to point at the data chunk for compares */
1347 		dmp->b_rptr = (uchar_t *)dc;
1348 
1349 		dprint(2,
1350 		    ("data_chunk: inserted %x in pq (ssn %d expected %d)\n",
1351 		    ntohl(dc->sdh_tsn), (int)(ssn), (int)(instr->nextseq)));
1352 
1353 		if (instr->istr_msgs == NULL) {
1354 			instr->istr_msgs = dmp;
1355 			ASSERT(dmp->b_prev == NULL && dmp->b_next == NULL);
1356 		} else {
1357 			mblk_t			*imblk = instr->istr_msgs;
1358 			sctp_data_hdr_t		*idc;
1359 
1360 			/*
1361 			 * XXXNeed to take sequence wraps into account,
1362 			 * ... and a more efficient insertion algo.
1363 			 */
1364 			for (;;) {
1365 				idc = (sctp_data_hdr_t *)imblk->b_rptr;
1366 				if (SSN_GT(ntohs(idc->sdh_ssn),
1367 					ntohs(dc->sdh_ssn))) {
1368 					if (instr->istr_msgs == imblk) {
1369 						instr->istr_msgs = dmp;
1370 						dmp->b_next = imblk;
1371 						imblk->b_prev = dmp;
1372 					} else {
1373 						ASSERT(imblk->b_prev != NULL);
1374 						imblk->b_prev->b_next = dmp;
1375 						dmp->b_prev = imblk->b_prev;
1376 						imblk->b_prev = dmp;
1377 						dmp->b_next = imblk;
1378 					}
1379 					break;
1380 				}
1381 				if (imblk->b_next == NULL) {
1382 					imblk->b_next = dmp;
1383 					dmp->b_prev = imblk;
1384 					break;
1385 				}
1386 				imblk = imblk->b_next;
1387 			}
1388 		}
1389 		(instr->istr_nmsgs)++;
1390 		(sctp->sctp_istr_nmsgs)++;
1391 		SCTP_ACK_IT(sctp, tsn);
1392 		return;
1393 	}
1394 
1395 	/*
1396 	 * Else we can deliver the data directly. Recalculate
1397 	 * dlen now since we may have reassembled data.
1398 	 */
1399 	dlen = dmp->b_wptr - (uchar_t *)dc - sizeof (*dc);
1400 	for (pmp = dmp->b_cont; pmp != NULL; pmp = pmp->b_cont)
1401 		dlen += pmp->b_wptr - pmp->b_rptr;
1402 	ASSERT(sctp->sctp_rxqueued >= dlen);
1403 	ASSERT(sctp->sctp_rwnd >= dlen);
1404 
1405 	/* Deliver the message. */
1406 	sctp->sctp_rxqueued -= dlen;
1407 
1408 	if (can_deliver) {
1409 		dmp->b_rptr = (uchar_t *)(dc + 1);
1410 		if (sctp_input_add_ancillary(sctp, &dmp, dc, fp, ipp) == 0) {
1411 			dprint(1, ("sctp_data_chunk: delivering %lu bytes\n",
1412 			    msgdsize(dmp)));
1413 			sctp->sctp_rwnd -= dlen;
1414 			new_rwnd = sctp->sctp_ulp_recv(sctp->sctp_ulpd, dmp,
1415 			    tpfinished ? 0 : SCTP_PARTIAL_DATA);
1416 			if (new_rwnd > sctp->sctp_rwnd) {
1417 				sctp->sctp_rwnd = new_rwnd;
1418 			}
1419 			SCTP_ACK_IT(sctp, tsn);
1420 		} else {
1421 			/* Just free the message if we don't have memory. */
1422 			freemsg(dmp);
1423 			return;
1424 		}
1425 	} else {
1426 		/* About to free the data */
1427 		freemsg(dmp);
1428 		SCTP_ACK_IT(sctp, tsn);
1429 	}
1430 
1431 	/*
1432 	 * data, now enqueued, may already have been processed and free'd
1433 	 * by the ULP (or we may have just freed it above, if we could not
1434 	 * deliver it), so we must not reference it (this is why we kept
1435 	 * the ssn and ubit above).
1436 	 */
1437 	if (ubit != 0) {
1438 		BUMP_LOCAL(sctp->sctp_iudchunks);
1439 		goto done;
1440 	}
1441 	BUMP_LOCAL(sctp->sctp_idchunks);
1442 
1443 	/*
1444 	 * If there was a partial delivery and it has not finished,
1445 	 * don't pull anything from the pqueues.
1446 	 */
1447 	if (!tpfinished) {
1448 		goto done;
1449 	}
1450 
1451 	instr->nextseq = ssn + 1;
1452 	/* Deliver any successive data chunks in the instr queue */
1453 	while (instr->istr_nmsgs > 0) {
1454 		dmp = (mblk_t *)instr->istr_msgs;
1455 		dc = (sctp_data_hdr_t *)dmp->b_rptr;
1456 		ssn = ntohs(dc->sdh_ssn);
1457 		/* Gap in the sequence */
1458 		if (ssn != instr->nextseq)
1459 			break;
1460 
1461 		/* Else deliver the data */
1462 		(instr->istr_nmsgs)--;
1463 		(instr->nextseq)++;
1464 		(sctp->sctp_istr_nmsgs)--;
1465 
1466 		instr->istr_msgs = instr->istr_msgs->b_next;
1467 		if (instr->istr_msgs != NULL)
1468 			instr->istr_msgs->b_prev = NULL;
1469 		dmp->b_next = dmp->b_prev = NULL;
1470 
1471 		dprint(2, ("data_chunk: pulling %x from pq (ssn %d)\n",
1472 		    ntohl(dc->sdh_tsn), (int)ssn));
1473 
1474 		/*
1475 		 * If this chunk was reassembled, each b_cont represents
1476 		 * another TSN; advance ftsn now.
1477 		 */
1478 		dlen = dmp->b_wptr - dmp->b_rptr - sizeof (*dc);
1479 		for (pmp = dmp->b_cont; pmp; pmp = pmp->b_cont)
1480 			dlen += pmp->b_wptr - pmp->b_rptr;
1481 
1482 		ASSERT(sctp->sctp_rxqueued >= dlen);
1483 		ASSERT(sctp->sctp_rwnd >= dlen);
1484 
1485 		sctp->sctp_rxqueued -= dlen;
1486 		if (can_deliver) {
1487 			dmp->b_rptr = (uchar_t *)(dc + 1);
1488 			if (sctp_input_add_ancillary(sctp, &dmp, dc, fp,
1489 			    ipp) == 0) {
1490 				dprint(1, ("sctp_data_chunk: delivering %lu "
1491 				    "bytes\n", msgdsize(dmp)));
1492 				sctp->sctp_rwnd -= dlen;
1493 				new_rwnd = sctp->sctp_ulp_recv(sctp->sctp_ulpd,
1494 				    dmp, tpfinished ? 0 : SCTP_PARTIAL_DATA);
1495 				if (new_rwnd > sctp->sctp_rwnd) {
1496 					sctp->sctp_rwnd = new_rwnd;
1497 				}
1498 				SCTP_ACK_IT(sctp, tsn);
1499 			} else {
1500 				freemsg(dmp);
1501 				return;
1502 			}
1503 		} else {
1504 			/* About to free the data */
1505 			freemsg(dmp);
1506 			SCTP_ACK_IT(sctp, tsn);
1507 		}
1508 	}
1509 
1510 done:
1511 
1512 	/*
1513 	 * If there are gap reports pending, check if advancing
1514 	 * the ftsn here closes a gap. If so, we can advance
1515 	 * ftsn to the end of the set.
1516 	 */
1517 	if (sctp->sctp_sack_info != NULL &&
1518 	    sctp->sctp_ftsn == sctp->sctp_sack_info->begin) {
1519 		sctp->sctp_ftsn = sctp->sctp_sack_info->end + 1;
1520 	}
1521 	/*
1522 	 * If ftsn has moved forward, maybe we can remove gap reports.
1523 	 * NB: dmp may now be NULL, so don't dereference it here.
1524 	 */
1525 	if (oftsn != sctp->sctp_ftsn && sctp->sctp_sack_info != NULL) {
1526 		sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1,
1527 		    &sctp->sctp_sack_gaps);
1528 		dprint(2, ("data_chunk: removed acks before %x (num=%d)\n",
1529 		    sctp->sctp_ftsn - 1, sctp->sctp_sack_gaps));
1530 	}
1531 
1532 #ifdef	DEBUG
1533 	if (sctp->sctp_sack_info != NULL) {
1534 		ASSERT(sctp->sctp_ftsn != sctp->sctp_sack_info->begin);
1535 	}
1536 #endif
1537 
1538 #undef	SCTP_ACK_IT
1539 }
1540 
1541 void
1542 sctp_fill_sack(sctp_t *sctp, unsigned char *dst, int sacklen)
1543 {
1544 	sctp_chunk_hdr_t *sch;
1545 	sctp_sack_chunk_t *sc;
1546 	sctp_sack_frag_t *sf;
1547 	uint16_t num_gaps = sctp->sctp_sack_gaps;
1548 	sctp_set_t *sp;
1549 
1550 	/* Chunk hdr */
1551 	sch = (sctp_chunk_hdr_t *)dst;
1552 	sch->sch_id = CHUNK_SACK;
1553 	sch->sch_flags = 0;
1554 	sch->sch_len = htons(sacklen);
1555 
1556 	/* SACK chunk */
1557 	sctp->sctp_lastacked = sctp->sctp_ftsn - 1;
1558 
1559 	sc = (sctp_sack_chunk_t *)(sch + 1);
1560 	sc->ssc_cumtsn = htonl(sctp->sctp_lastacked);
1561 	if (sctp->sctp_rxqueued < sctp->sctp_rwnd) {
1562 		sc->ssc_a_rwnd = htonl(sctp->sctp_rwnd - sctp->sctp_rxqueued);
1563 	} else {
1564 		sc->ssc_a_rwnd = 0;
1565 	}
1566 	sc->ssc_numfrags = htons(num_gaps);
1567 	sc->ssc_numdups = 0;
1568 
1569 	/* lay in gap reports */
1570 	sf = (sctp_sack_frag_t *)(sc + 1);
1571 	for (sp = sctp->sctp_sack_info; sp; sp = sp->next) {
1572 		uint16_t offset;
1573 
1574 		/* start */
1575 		if (sp->begin > sctp->sctp_lastacked) {
1576 			offset = (uint16_t)(sp->begin - sctp->sctp_lastacked);
1577 		} else {
1578 			/* sequence number wrap */
1579 			offset = (uint16_t)(UINT32_MAX - sctp->sctp_lastacked +
1580 			    sp->begin);
1581 		}
1582 		sf->ssf_start = htons(offset);
1583 
1584 		/* end */
1585 		if (sp->end >= sp->begin) {
1586 			offset += (uint16_t)(sp->end - sp->begin);
1587 		} else {
1588 			/* sequence number wrap */
1589 			offset += (uint16_t)(UINT32_MAX - sp->begin + sp->end);
1590 		}
1591 		sf->ssf_end = htons(offset);
1592 
1593 		sf++;
1594 		/* This is just for debugging (a la the following assertion) */
1595 		num_gaps--;
1596 	}
1597 
1598 	ASSERT(num_gaps == 0);
1599 
1600 	/* If the SACK timer is running, stop it */
1601 	if (sctp->sctp_ack_timer_running) {
1602 		sctp_timer_stop(sctp->sctp_ack_mp);
1603 		sctp->sctp_ack_timer_running = B_FALSE;
1604 	}
1605 
1606 	BUMP_LOCAL(sctp->sctp_obchunks);
1607 }
1608 
1609 mblk_t *
1610 sctp_make_sack(sctp_t *sctp, sctp_faddr_t *sendto, mblk_t *dups)
1611 {
1612 	mblk_t *smp;
1613 	size_t slen;
1614 	sctp_chunk_hdr_t *sch;
1615 	sctp_sack_chunk_t *sc;
1616 	int32_t acks_max;
1617 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1618 
1619 	if (sctp->sctp_force_sack) {
1620 		sctp->sctp_force_sack = 0;
1621 		goto checks_done;
1622 	}
1623 
1624 	acks_max = sctps->sctps_deferred_acks_max;
1625 	if (sctp->sctp_state == SCTPS_ESTABLISHED) {
1626 		if (sctp->sctp_sack_toggle < acks_max) {
1627 			/* no need to SACK right now */
1628 			dprint(2, ("sctp_make_sack: %p no sack (toggle)\n",
1629 			    (void *)sctp));
1630 			return (NULL);
1631 		} else if (sctp->sctp_sack_toggle >= acks_max) {
1632 			sctp->sctp_sack_toggle = 0;
1633 		}
1634 	}
1635 
1636 	if (sctp->sctp_ftsn == sctp->sctp_lastacked + 1) {
1637 		dprint(2, ("sctp_make_sack: %p no sack (already)\n",
1638 		    (void *)sctp));
1639 		return (NULL);
1640 	}
1641 
1642 checks_done:
1643 	dprint(2, ("sctp_make_sack: acking %x\n", sctp->sctp_ftsn - 1));
1644 
1645 	slen = sizeof (*sch) + sizeof (*sc) +
1646 	    (sizeof (sctp_sack_frag_t) * sctp->sctp_sack_gaps);
1647 	smp = sctp_make_mp(sctp, sendto, slen);
1648 	if (smp == NULL) {
1649 		SCTP_KSTAT(sctps, sctp_send_sack_failed);
1650 		return (NULL);
1651 	}
1652 	sch = (sctp_chunk_hdr_t *)smp->b_wptr;
1653 
1654 	sctp_fill_sack(sctp, smp->b_wptr, slen);
1655 	smp->b_wptr += slen;
1656 	if (dups) {
1657 		sc = (sctp_sack_chunk_t *)(sch + 1);
1658 		sc->ssc_numdups = htons((dups->b_wptr - dups->b_rptr)
1659 		    / sizeof (uint32_t));
1660 		sch->sch_len = htons(slen + (dups->b_wptr - dups->b_rptr));
1661 		smp->b_cont = dups;
1662 	}
1663 
1664 	return (smp);
1665 }
1666 
1667 void
1668 sctp_sack(sctp_t *sctp, mblk_t *dups)
1669 {
1670 	mblk_t *smp;
1671 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1672 
1673 	/* If we are shutting down, let send_shutdown() bundle the SACK */
1674 	if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) {
1675 		sctp_send_shutdown(sctp, 0);
1676 	}
1677 
1678 	ASSERT(sctp->sctp_lastdata != NULL);
1679 
1680 	if ((smp = sctp_make_sack(sctp, sctp->sctp_lastdata, dups)) == NULL) {
1681 		/* The caller of sctp_sack() will not free the dups mblk. */
1682 		if (dups != NULL)
1683 			freeb(dups);
1684 		return;
1685 	}
1686 
1687 	sctp_set_iplen(sctp, smp);
1688 
1689 	dprint(2, ("sctp_sack: sending to %p %x:%x:%x:%x\n",
1690 	    (void *)sctp->sctp_lastdata,
1691 	    SCTP_PRINTADDR(sctp->sctp_lastdata->faddr)));
1692 
1693 	sctp->sctp_active = lbolt64;
1694 
1695 	BUMP_MIB(&sctps->sctps_mib, sctpOutAck);
1696 	sctp_add_sendq(sctp, smp);
1697 }
1698 
1699 /*
1700  * This is called if we have a message that was partially sent and is
1701  * abandoned. The cum TSN will be the last chunk sent for this message,
1702  * subsequent chunks will be marked ABANDONED. We send a Forward TSN
1703  * chunk in this case with the TSN of the last sent chunk so that the
1704  * peer can clean up its fragment list for this message. This message
1705  * will be removed from the transmit list when the peer sends a SACK
1706  * back.
1707  */
1708 int
1709 sctp_check_abandoned_msg(sctp_t *sctp, mblk_t *meta)
1710 {
1711 	sctp_data_hdr_t	*dh;
1712 	mblk_t		*nmp;
1713 	mblk_t		*head;
1714 	int32_t		unsent = 0;
1715 	mblk_t		*mp1 = meta->b_cont;
1716 	uint32_t	adv_pap = sctp->sctp_adv_pap;
1717 	sctp_faddr_t	*fp = sctp->sctp_current;
1718 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1719 
1720 	dh = (sctp_data_hdr_t *)mp1->b_rptr;
1721 	if (SEQ_GEQ(sctp->sctp_lastack_rxd, ntohl(dh->sdh_tsn))) {
1722 		sctp_ftsn_set_t	*sets = NULL;
1723 		uint_t		nsets = 0;
1724 		uint32_t	seglen = sizeof (uint32_t);
1725 		boolean_t	ubit = SCTP_DATA_GET_UBIT(dh);
1726 
1727 		while (mp1->b_next != NULL && SCTP_CHUNK_ISSENT(mp1->b_next))
1728 			mp1 = mp1->b_next;
1729 		dh = (sctp_data_hdr_t *)mp1->b_rptr;
1730 		sctp->sctp_adv_pap = ntohl(dh->sdh_tsn);
1731 		if (!ubit &&
1732 		    !sctp_add_ftsn_set(&sets, fp, meta, &nsets, &seglen)) {
1733 			sctp->sctp_adv_pap = adv_pap;
1734 			return (ENOMEM);
1735 		}
1736 		nmp = sctp_make_ftsn_chunk(sctp, fp, sets, nsets, seglen);
1737 		sctp_free_ftsn_set(sets);
1738 		if (nmp == NULL) {
1739 			sctp->sctp_adv_pap = adv_pap;
1740 			return (ENOMEM);
1741 		}
1742 		head = sctp_add_proto_hdr(sctp, fp, nmp, 0, NULL);
1743 		if (head == NULL) {
1744 			sctp->sctp_adv_pap = adv_pap;
1745 			freemsg(nmp);
1746 			SCTP_KSTAT(sctps, sctp_send_ftsn_failed);
1747 			return (ENOMEM);
1748 		}
1749 		SCTP_MSG_SET_ABANDONED(meta);
1750 		sctp_set_iplen(sctp, head);
1751 		sctp_add_sendq(sctp, head);
1752 		if (!fp->timer_running)
1753 			SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
1754 		mp1 = mp1->b_next;
1755 		while (mp1 != NULL) {
1756 			ASSERT(!SCTP_CHUNK_ISSENT(mp1));
1757 			ASSERT(!SCTP_CHUNK_ABANDONED(mp1));
1758 			SCTP_ABANDON_CHUNK(mp1);
1759 			dh = (sctp_data_hdr_t *)mp1->b_rptr;
1760 			unsent += ntohs(dh->sdh_len) - sizeof (*dh);
1761 			mp1 = mp1->b_next;
1762 		}
1763 		ASSERT(sctp->sctp_unsent >= unsent);
1764 		sctp->sctp_unsent -= unsent;
1765 		/*
1766 		 * Update ULP the amount of queued data, which is
1767 		 * sent-unack'ed + unsent.
1768 		 */
1769 		if (!SCTP_IS_DETACHED(sctp)) {
1770 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
1771 			    sctp->sctp_unacked + sctp->sctp_unsent);
1772 		}
1773 		return (0);
1774 	}
1775 	return (-1);
1776 }
1777 
1778 uint32_t
1779 sctp_cumack(sctp_t *sctp, uint32_t tsn, mblk_t **first_unacked)
1780 {
1781 	mblk_t *ump, *nump, *mp = NULL;
1782 	uint16_t chunklen;
1783 	uint32_t xtsn;
1784 	sctp_faddr_t *fp;
1785 	sctp_data_hdr_t *sdc;
1786 	uint32_t cumack_forward = 0;
1787 	sctp_msg_hdr_t	*mhdr;
1788 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1789 
1790 	ump = sctp->sctp_xmit_head;
1791 
1792 	/*
1793 	 * Free messages only when they're completely acked.
1794 	 */
1795 	while (ump != NULL) {
1796 		mhdr = (sctp_msg_hdr_t *)ump->b_rptr;
1797 		for (mp = ump->b_cont; mp != NULL; mp = mp->b_next) {
1798 			if (SCTP_CHUNK_ABANDONED(mp)) {
1799 				ASSERT(SCTP_IS_MSG_ABANDONED(ump));
1800 				mp = NULL;
1801 				break;
1802 			}
1803 			/*
1804 			 * We check for abandoned message if we are PR-SCTP
1805 			 * aware, if this is not the first chunk in the
1806 			 * message (b_cont) and if the message is marked
1807 			 * abandoned.
1808 			 */
1809 			if (!SCTP_CHUNK_ISSENT(mp)) {
1810 				if (sctp->sctp_prsctp_aware &&
1811 				    mp != ump->b_cont &&
1812 				    (SCTP_IS_MSG_ABANDONED(ump) ||
1813 				    SCTP_MSG_TO_BE_ABANDONED(ump, mhdr,
1814 				    sctp))) {
1815 					(void) sctp_check_abandoned_msg(sctp,
1816 					    ump);
1817 				}
1818 				goto cum_ack_done;
1819 			}
1820 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
1821 			xtsn = ntohl(sdc->sdh_tsn);
1822 			if (SEQ_GEQ(sctp->sctp_lastack_rxd, xtsn))
1823 				continue;
1824 			if (SEQ_GEQ(tsn, xtsn)) {
1825 				fp = SCTP_CHUNK_DEST(mp);
1826 				chunklen = ntohs(sdc->sdh_len);
1827 
1828 				if (sctp->sctp_out_time != 0 &&
1829 				    xtsn == sctp->sctp_rtt_tsn) {
1830 					/* Got a new RTT measurement */
1831 					sctp_update_rtt(sctp, fp,
1832 					    lbolt64 - sctp->sctp_out_time);
1833 					sctp->sctp_out_time = 0;
1834 				}
1835 				if (SCTP_CHUNK_ISACKED(mp))
1836 					continue;
1837 				SCTP_CHUNK_SET_SACKCNT(mp, 0);
1838 				SCTP_CHUNK_ACKED(mp);
1839 				ASSERT(fp->suna >= chunklen);
1840 				fp->suna -= chunklen;
1841 				fp->acked += chunklen;
1842 				cumack_forward += chunklen;
1843 				ASSERT(sctp->sctp_unacked >=
1844 				    (chunklen - sizeof (*sdc)));
1845 				sctp->sctp_unacked -=
1846 				    (chunklen - sizeof (*sdc));
1847 				if (fp->suna == 0) {
1848 					/* all outstanding data acked */
1849 					fp->pba = 0;
1850 					SCTP_FADDR_TIMER_STOP(fp);
1851 				} else {
1852 					SCTP_FADDR_TIMER_RESTART(sctp, fp,
1853 					    fp->rto);
1854 				}
1855 			} else {
1856 				goto cum_ack_done;
1857 			}
1858 		}
1859 		nump = ump->b_next;
1860 		if (nump != NULL)
1861 			nump->b_prev = NULL;
1862 		if (ump == sctp->sctp_xmit_tail)
1863 			sctp->sctp_xmit_tail = nump;
1864 		if (SCTP_IS_MSG_ABANDONED(ump)) {
1865 			BUMP_LOCAL(sctp->sctp_prsctpdrop);
1866 			ump->b_next = NULL;
1867 			sctp_sendfail_event(sctp, ump, 0, B_TRUE);
1868 		} else {
1869 			sctp_free_msg(ump);
1870 		}
1871 		sctp->sctp_xmit_head = ump = nump;
1872 	}
1873 cum_ack_done:
1874 	*first_unacked = mp;
1875 	if (cumack_forward > 0) {
1876 		BUMP_MIB(&sctps->sctps_mib, sctpInAck);
1877 		if (SEQ_GT(sctp->sctp_lastack_rxd, sctp->sctp_recovery_tsn)) {
1878 			sctp->sctp_recovery_tsn = sctp->sctp_lastack_rxd;
1879 		}
1880 
1881 		/*
1882 		 * Update ULP the amount of queued data, which is
1883 		 * sent-unack'ed + unsent.
1884 		 */
1885 		if (!SCTP_IS_DETACHED(sctp)) {
1886 			sctp->sctp_ulp_xmitted(sctp->sctp_ulpd,
1887 			    sctp->sctp_unacked + sctp->sctp_unsent);
1888 		}
1889 
1890 		/* Time to send a shutdown? */
1891 		if (sctp->sctp_state == SCTPS_SHUTDOWN_PENDING) {
1892 			sctp_send_shutdown(sctp, 0);
1893 		}
1894 		sctp->sctp_xmit_unacked = mp;
1895 	} else {
1896 		/* dup ack */
1897 		BUMP_MIB(&sctps->sctps_mib, sctpInDupAck);
1898 	}
1899 	sctp->sctp_lastack_rxd = tsn;
1900 	if (SEQ_LT(sctp->sctp_adv_pap, sctp->sctp_lastack_rxd))
1901 		sctp->sctp_adv_pap = sctp->sctp_lastack_rxd;
1902 	ASSERT(sctp->sctp_xmit_head || sctp->sctp_unacked == 0);
1903 
1904 	return (cumack_forward);
1905 }
1906 
1907 static int
1908 sctp_set_frwnd(sctp_t *sctp, uint32_t frwnd)
1909 {
1910 	uint32_t orwnd;
1911 
1912 	if (sctp->sctp_unacked > frwnd) {
1913 		sctp->sctp_frwnd = 0;
1914 		return (0);
1915 	}
1916 	orwnd = sctp->sctp_frwnd;
1917 	sctp->sctp_frwnd = frwnd - sctp->sctp_unacked;
1918 	if (orwnd < sctp->sctp_frwnd) {
1919 		return (1);
1920 	} else {
1921 		return (0);
1922 	}
1923 }
1924 
1925 /*
1926  * For un-ordered messages.
1927  * Walk the sctp->sctp_uo_frag list and remove any fragments with TSN
1928  * less than/equal to ftsn. Fragments for un-ordered messages are
1929  * strictly in sequence (w.r.t TSN).
1930  */
1931 static int
1932 sctp_ftsn_check_uo_frag(sctp_t *sctp, uint32_t ftsn)
1933 {
1934 	mblk_t		*hmp;
1935 	mblk_t		*hmp_next;
1936 	sctp_data_hdr_t	*dc;
1937 	int		dlen = 0;
1938 
1939 	hmp = sctp->sctp_uo_frags;
1940 	while (hmp != NULL) {
1941 		hmp_next = hmp->b_next;
1942 		dc = (sctp_data_hdr_t *)hmp->b_rptr;
1943 		if (SEQ_GT(ntohl(dc->sdh_tsn), ftsn))
1944 			return (dlen);
1945 		sctp->sctp_uo_frags = hmp_next;
1946 		if (hmp_next != NULL)
1947 			hmp_next->b_prev = NULL;
1948 		hmp->b_next = NULL;
1949 		dlen += ntohs(dc->sdh_len) - sizeof (*dc);
1950 		freeb(hmp);
1951 		hmp = hmp_next;
1952 	}
1953 	return (dlen);
1954 }
1955 
1956 /*
1957  * For ordered messages.
1958  * Check for existing fragments for an sid-ssn pair reported as abandoned,
1959  * hence will not receive, in the Forward TSN. If there are fragments, then
1960  * we just nuke them. If and when Partial Delivery API is supported, we
1961  * would need to send a notification to the upper layer about this.
1962  */
1963 static int
1964 sctp_ftsn_check_frag(sctp_t *sctp, uint16_t ssn, sctp_instr_t *sip)
1965 {
1966 	sctp_reass_t	*srp;
1967 	mblk_t		*hmp;
1968 	mblk_t		*dmp;
1969 	mblk_t		*hmp_next;
1970 	sctp_data_hdr_t	*dc;
1971 	int		dlen = 0;
1972 
1973 	hmp = sip->istr_reass;
1974 	while (hmp != NULL) {
1975 		hmp_next = hmp->b_next;
1976 		srp = (sctp_reass_t *)DB_BASE(hmp);
1977 		if (SSN_GT(srp->ssn, ssn))
1978 			return (dlen);
1979 		/*
1980 		 * If we had sent part of this message up, send a partial
1981 		 * delivery event. Since this is ordered delivery, we should
1982 		 * have sent partial message only for the next in sequence,
1983 		 * hence the ASSERT. See comments in sctp_data_chunk() for
1984 		 * trypartial.
1985 		 */
1986 		if (srp->partial_delivered) {
1987 			ASSERT(sip->nextseq == srp->ssn);
1988 			sctp_partial_delivery_event(sctp);
1989 		}
1990 		/* Take it out of the reass queue */
1991 		sip->istr_reass = hmp_next;
1992 		if (hmp_next != NULL)
1993 			hmp_next->b_prev = NULL;
1994 		hmp->b_next = NULL;
1995 		ASSERT(hmp->b_prev == NULL);
1996 		dmp = hmp;
1997 		ASSERT(DB_TYPE(hmp) == M_CTL);
1998 		dmp = hmp->b_cont;
1999 		hmp->b_cont = NULL;
2000 		freeb(hmp);
2001 		hmp = dmp;
2002 		while (dmp != NULL) {
2003 			dc = (sctp_data_hdr_t *)dmp->b_rptr;
2004 			dlen += ntohs(dc->sdh_len) - sizeof (*dc);
2005 			dmp = dmp->b_cont;
2006 		}
2007 		freemsg(hmp);
2008 		hmp = hmp_next;
2009 	}
2010 	return (dlen);
2011 }
2012 
2013 /*
2014  * Update sctp_ftsn to the cumulative TSN from the Forward TSN chunk. Remove
2015  * any SACK gaps less than the newly updated sctp_ftsn. Walk through the
2016  * sid-ssn pair in the Forward TSN and for each, clean the fragment list
2017  * for this pair, if needed, and check if we can deliver subsequent
2018  * messages, if any, from the instream queue (that were waiting for this
2019  * sid-ssn message to show up). Once we are done try to update the SACK
2020  * info. We could get a duplicate Forward TSN, in which case just send
2021  * a SACK. If any of the sid values in the the Forward TSN is invalid,
2022  * send back an "Invalid Stream Identifier" error and continue processing
2023  * the rest.
2024  */
2025 static void
2026 sctp_process_forward_tsn(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp,
2027     ip6_pkt_t *ipp)
2028 {
2029 	uint32_t	*ftsn = (uint32_t *)(ch + 1);
2030 	ftsn_entry_t	*ftsn_entry;
2031 	sctp_instr_t	*instr;
2032 	boolean_t	can_deliver = B_TRUE;
2033 	size_t		dlen;
2034 	int		flen;
2035 	mblk_t		*dmp;
2036 	mblk_t		*pmp;
2037 	sctp_data_hdr_t	*dc;
2038 	ssize_t		remaining;
2039 	sctp_stack_t	*sctps = sctp->sctp_sctps;
2040 
2041 	*ftsn = ntohl(*ftsn);
2042 	remaining =  ntohs(ch->sch_len) - sizeof (*ch) - sizeof (*ftsn);
2043 
2044 	if (SCTP_IS_DETACHED(sctp)) {
2045 		BUMP_MIB(&sctps->sctps_mib, sctpInClosed);
2046 		can_deliver = B_FALSE;
2047 	}
2048 	/*
2049 	 * un-ordered messages don't have SID-SSN pair entries, we check
2050 	 * for any fragments (for un-ordered message) to be discarded using
2051 	 * the cumulative FTSN.
2052 	 */
2053 	flen = sctp_ftsn_check_uo_frag(sctp, *ftsn);
2054 	if (flen > 0) {
2055 		ASSERT(sctp->sctp_rxqueued >= flen);
2056 		sctp->sctp_rxqueued -= flen;
2057 	}
2058 	ftsn_entry = (ftsn_entry_t *)(ftsn + 1);
2059 	while (remaining >= sizeof (*ftsn_entry)) {
2060 		ftsn_entry->ftsn_sid = ntohs(ftsn_entry->ftsn_sid);
2061 		ftsn_entry->ftsn_ssn = ntohs(ftsn_entry->ftsn_ssn);
2062 		if (ftsn_entry->ftsn_sid >= sctp->sctp_num_istr) {
2063 			uint16_t	inval_parm[2];
2064 			mblk_t		*errmp;
2065 
2066 			inval_parm[0] = htons(ftsn_entry->ftsn_sid);
2067 			/* RESERVED to be ignored at the receiving end */
2068 			inval_parm[1] = 0;
2069 			errmp = sctp_make_err(sctp, SCTP_ERR_BAD_SID,
2070 			    (char *)inval_parm, sizeof (inval_parm));
2071 			if (errmp != NULL)
2072 				sctp_send_err(sctp, errmp, NULL);
2073 			ftsn_entry++;
2074 			remaining -= sizeof (*ftsn_entry);
2075 			continue;
2076 		}
2077 		instr = &sctp->sctp_instr[ftsn_entry->ftsn_sid];
2078 		flen = sctp_ftsn_check_frag(sctp, ftsn_entry->ftsn_ssn, instr);
2079 		/* Indicates frags were nuked, update rxqueued */
2080 		if (flen > 0) {
2081 			ASSERT(sctp->sctp_rxqueued >= flen);
2082 			sctp->sctp_rxqueued -= flen;
2083 		}
2084 		/*
2085 		 * It is possible to receive an FTSN chunk with SSN smaller
2086 		 * than then nextseq if this chunk is a retransmission because
2087 		 * of incomplete processing when it was first processed.
2088 		 */
2089 		if (SSN_GE(ftsn_entry->ftsn_ssn, instr->nextseq))
2090 			instr->nextseq = ftsn_entry->ftsn_ssn + 1;
2091 		while (instr->istr_nmsgs > 0) {
2092 			mblk_t	*next;
2093 
2094 			dmp = (mblk_t *)instr->istr_msgs;
2095 			dc = (sctp_data_hdr_t *)dmp->b_rptr;
2096 			if (ntohs(dc->sdh_ssn) != instr->nextseq)
2097 				break;
2098 
2099 			next = dmp->b_next;
2100 			dlen = dmp->b_wptr - dmp->b_rptr - sizeof (*dc);
2101 			for (pmp = dmp->b_cont; pmp != NULL;
2102 			    pmp = pmp->b_cont) {
2103 				dlen += pmp->b_wptr - pmp->b_rptr;
2104 			}
2105 			if (can_deliver) {
2106 				int32_t	nrwnd;
2107 
2108 				dmp->b_rptr = (uchar_t *)(dc + 1);
2109 				dmp->b_next = NULL;
2110 				ASSERT(dmp->b_prev == NULL);
2111 				if (sctp_input_add_ancillary(sctp,
2112 				    &dmp, dc, fp, ipp) == 0) {
2113 					sctp->sctp_rxqueued -= dlen;
2114 					sctp->sctp_rwnd -= dlen;
2115 					nrwnd = sctp->sctp_ulp_recv(
2116 					    sctp->sctp_ulpd, dmp, 0);
2117 					if (nrwnd > sctp->sctp_rwnd)
2118 						sctp->sctp_rwnd = nrwnd;
2119 				} else {
2120 					/*
2121 					 * We will resume processing when
2122 					 * the FTSN chunk is re-xmitted.
2123 					 */
2124 					dmp->b_rptr = (uchar_t *)dc;
2125 					dmp->b_next = next;
2126 					dprint(0,
2127 					    ("FTSN dequeuing %u failed\n",
2128 					    ntohs(dc->sdh_ssn)));
2129 					return;
2130 				}
2131 			} else {
2132 				sctp->sctp_rxqueued -= dlen;
2133 				ASSERT(dmp->b_prev == NULL);
2134 				dmp->b_next = NULL;
2135 				freemsg(dmp);
2136 			}
2137 			instr->istr_nmsgs--;
2138 			instr->nextseq++;
2139 			sctp->sctp_istr_nmsgs--;
2140 			if (next != NULL)
2141 				next->b_prev = NULL;
2142 			instr->istr_msgs = next;
2143 		}
2144 		ftsn_entry++;
2145 		remaining -= sizeof (*ftsn_entry);
2146 	}
2147 	/* Duplicate FTSN */
2148 	if (*ftsn <= (sctp->sctp_ftsn - 1)) {
2149 		sctp->sctp_force_sack = 1;
2150 		return;
2151 	}
2152 	/* Advance cum TSN to that reported in the Forward TSN chunk */
2153 	sctp->sctp_ftsn = *ftsn + 1;
2154 
2155 	/* Remove all the SACK gaps before the new cum TSN */
2156 	if (sctp->sctp_sack_info != NULL) {
2157 		sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1,
2158 		    &sctp->sctp_sack_gaps);
2159 	}
2160 	/*
2161 	 * If there are gap reports pending, check if advancing
2162 	 * the ftsn here closes a gap. If so, we can advance
2163 	 * ftsn to the end of the set.
2164 	 * If ftsn has moved forward, maybe we can remove gap reports.
2165 	 */
2166 	if (sctp->sctp_sack_info != NULL &&
2167 	    sctp->sctp_ftsn == sctp->sctp_sack_info->begin) {
2168 		sctp->sctp_ftsn = sctp->sctp_sack_info->end + 1;
2169 		sctp_ack_rem(&sctp->sctp_sack_info, sctp->sctp_ftsn - 1,
2170 		    &sctp->sctp_sack_gaps);
2171 	}
2172 }
2173 
2174 /*
2175  * When we have processed a SACK we check to see if we can advance the
2176  * cumulative TSN if there are abandoned chunks immediately following
2177  * the updated cumulative TSN. If there are, we attempt to send a
2178  * Forward TSN chunk.
2179  */
2180 static void
2181 sctp_check_abandoned_data(sctp_t *sctp, sctp_faddr_t *fp)
2182 {
2183 	mblk_t		*meta = sctp->sctp_xmit_head;
2184 	mblk_t		*mp;
2185 	mblk_t		*nmp;
2186 	uint32_t	seglen;
2187 	uint32_t	adv_pap = sctp->sctp_adv_pap;
2188 
2189 	/*
2190 	 * We only check in the first meta since otherwise we can't
2191 	 * advance the cumulative ack point. We just look for chunks
2192 	 * marked for retransmission, else we might prematurely
2193 	 * send an FTSN for a sent, but unacked, chunk.
2194 	 */
2195 	for (mp = meta->b_cont; mp != NULL; mp = mp->b_next) {
2196 		if (!SCTP_CHUNK_ISSENT(mp))
2197 			return;
2198 		if (SCTP_CHUNK_WANT_REXMIT(mp))
2199 			break;
2200 	}
2201 	if (mp == NULL)
2202 		return;
2203 	sctp_check_adv_ack_pt(sctp, meta, mp);
2204 	if (SEQ_GT(sctp->sctp_adv_pap, adv_pap)) {
2205 		sctp_make_ftsns(sctp, meta, mp, &nmp, fp, &seglen);
2206 		if (nmp == NULL) {
2207 			sctp->sctp_adv_pap = adv_pap;
2208 			if (!fp->timer_running)
2209 				SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
2210 			return;
2211 		}
2212 		sctp_set_iplen(sctp, nmp);
2213 		sctp_add_sendq(sctp, nmp);
2214 		if (!fp->timer_running)
2215 			SCTP_FADDR_TIMER_RESTART(sctp, fp, fp->rto);
2216 	}
2217 }
2218 
2219 /*
2220  * The processing here follows the same logic in sctp_got_sack(), the reason
2221  * we do this separately is because, usually, gap blocks are ordered and
2222  * we can process it in sctp_got_sack(). However if they aren't we would
2223  * need to do some additional non-optimal stuff when we start processing the
2224  * unordered gaps. To that effect sctp_got_sack() does the processing in the
2225  * simple case and this does the same in the more involved case.
2226  */
2227 static uint32_t
2228 sctp_process_uo_gaps(sctp_t *sctp, uint32_t ctsn, sctp_sack_frag_t *ssf,
2229     int num_gaps, mblk_t *umphead, mblk_t *mphead, int *trysend,
2230     boolean_t *fast_recovery, uint32_t fr_xtsn)
2231 {
2232 	uint32_t		xtsn;
2233 	uint32_t		gapstart = 0;
2234 	uint32_t		gapend = 0;
2235 	int			gapcnt;
2236 	uint16_t		chunklen;
2237 	sctp_data_hdr_t		*sdc;
2238 	int			gstart;
2239 	mblk_t			*ump = umphead;
2240 	mblk_t			*mp = mphead;
2241 	sctp_faddr_t		*fp;
2242 	uint32_t		acked = 0;
2243 	sctp_stack_t		*sctps = sctp->sctp_sctps;
2244 
2245 	/*
2246 	 * gstart tracks the last (in the order of TSN) gapstart that
2247 	 * we process in this SACK gaps walk.
2248 	 */
2249 	gstart = ctsn;
2250 
2251 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
2252 	xtsn = ntohl(sdc->sdh_tsn);
2253 	for (gapcnt = 0; gapcnt < num_gaps; gapcnt++, ssf++) {
2254 		if (gapstart != 0) {
2255 			/*
2256 			 * If we have reached the end of the transmit list or
2257 			 * hit an unsent chunk or encountered an unordered gap
2258 			 * block start from the ctsn again.
2259 			 */
2260 			if (ump == NULL || !SCTP_CHUNK_ISSENT(mp) ||
2261 			    SEQ_LT(ctsn + ntohs(ssf->ssf_start), xtsn)) {
2262 				ump = umphead;
2263 				mp = mphead;
2264 				sdc = (sctp_data_hdr_t *)mp->b_rptr;
2265 				xtsn = ntohl(sdc->sdh_tsn);
2266 			}
2267 		}
2268 
2269 		gapstart = ctsn + ntohs(ssf->ssf_start);
2270 		gapend = ctsn + ntohs(ssf->ssf_end);
2271 
2272 		/* SACK for TSN we have not sent - ABORT */
2273 		if (SEQ_GT(gapstart, sctp->sctp_ltsn - 1) ||
2274 		    SEQ_GT(gapend, sctp->sctp_ltsn - 1)) {
2275 			BUMP_MIB(&sctps->sctps_mib, sctpInAckUnsent);
2276 			*trysend = -1;
2277 			return (acked);
2278 		} else if (SEQ_LT(gapend, gapstart)) {
2279 			break;
2280 		}
2281 		/*
2282 		 * The xtsn can be the TSN processed for the last gap
2283 		 * (gapend) or it could be the cumulative TSN. We continue
2284 		 * with the last xtsn as long as the gaps are ordered, when
2285 		 * we hit an unordered gap, we re-start from the cumulative
2286 		 * TSN. For the first gap it is always the cumulative TSN.
2287 		 */
2288 		while (xtsn != gapstart) {
2289 			/*
2290 			 * We can't reliably check for reneged chunks
2291 			 * when walking the unordered list, so we don't.
2292 			 * In case the peer reneges then we will end up
2293 			 * sending the reneged chunk via timeout.
2294 			 */
2295 			mp = mp->b_next;
2296 			if (mp == NULL) {
2297 				ump = ump->b_next;
2298 				/*
2299 				 * ump can't be NULL because of the sanity
2300 				 * check above.
2301 				 */
2302 				ASSERT(ump != NULL);
2303 				mp = ump->b_cont;
2304 			}
2305 			/*
2306 			 * mp can't be unsent because of the sanity check
2307 			 * above.
2308 			 */
2309 			ASSERT(SCTP_CHUNK_ISSENT(mp));
2310 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
2311 			xtsn = ntohl(sdc->sdh_tsn);
2312 		}
2313 		/*
2314 		 * Now that we have found the chunk with TSN == 'gapstart',
2315 		 * let's walk till we hit the chunk with TSN == 'gapend'.
2316 		 * All intermediate chunks will be marked ACKED, if they
2317 		 * haven't already been.
2318 		 */
2319 		while (SEQ_LEQ(xtsn, gapend)) {
2320 			/*
2321 			 * SACKed
2322 			 */
2323 			SCTP_CHUNK_SET_SACKCNT(mp, 0);
2324 			if (!SCTP_CHUNK_ISACKED(mp)) {
2325 				SCTP_CHUNK_ACKED(mp);
2326 
2327 				fp = SCTP_CHUNK_DEST(mp);
2328 				chunklen = ntohs(sdc->sdh_len);
2329 				ASSERT(fp->suna >= chunklen);
2330 				fp->suna -= chunklen;
2331 				if (fp->suna == 0) {
2332 					/* All outstanding data acked. */
2333 					fp->pba = 0;
2334 					SCTP_FADDR_TIMER_STOP(fp);
2335 				}
2336 				fp->acked += chunklen;
2337 				acked += chunklen;
2338 				sctp->sctp_unacked -= chunklen - sizeof (*sdc);
2339 				ASSERT(sctp->sctp_unacked >= 0);
2340 			}
2341 			/*
2342 			 * Move to the next message in the transmit list
2343 			 * if we are done with all the chunks from the current
2344 			 * message. Note, it is possible to hit the end of the
2345 			 * transmit list here, i.e. if we have already completed
2346 			 * processing the gap block.
2347 			 */
2348 			mp = mp->b_next;
2349 			if (mp == NULL) {
2350 				ump = ump->b_next;
2351 				if (ump == NULL) {
2352 					ASSERT(xtsn == gapend);
2353 					break;
2354 				}
2355 				mp = ump->b_cont;
2356 			}
2357 			/*
2358 			 * Likewise, we can hit an unsent chunk once we have
2359 			 * completed processing the gap block.
2360 			 */
2361 			if (!SCTP_CHUNK_ISSENT(mp)) {
2362 				ASSERT(xtsn == gapend);
2363 				break;
2364 			}
2365 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
2366 			xtsn = ntohl(sdc->sdh_tsn);
2367 		}
2368 		/*
2369 		 * We keep track of the last gap we successfully processed
2370 		 * so that we can terminate the walk below for incrementing
2371 		 * the SACK count.
2372 		 */
2373 		if (SEQ_LT(gstart, gapstart))
2374 			gstart = gapstart;
2375 	}
2376 	/*
2377 	 * Check if have incremented the SACK count for all unacked TSNs in
2378 	 * sctp_got_sack(), if so we are done.
2379 	 */
2380 	if (SEQ_LEQ(gstart, fr_xtsn))
2381 		return (acked);
2382 
2383 	ump = umphead;
2384 	mp = mphead;
2385 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
2386 	xtsn = ntohl(sdc->sdh_tsn);
2387 	while (SEQ_LT(xtsn, gstart)) {
2388 		/*
2389 		 * We have incremented SACK count for TSNs less than fr_tsn
2390 		 * in sctp_got_sack(), so don't increment them again here.
2391 		 */
2392 		if (SEQ_GT(xtsn, fr_xtsn) && !SCTP_CHUNK_ISACKED(mp)) {
2393 			SCTP_CHUNK_SET_SACKCNT(mp, SCTP_CHUNK_SACKCNT(mp) + 1);
2394 			if (SCTP_CHUNK_SACKCNT(mp) ==
2395 			    sctps->sctps_fast_rxt_thresh) {
2396 				SCTP_CHUNK_REXMIT(mp);
2397 				sctp->sctp_chk_fast_rexmit = B_TRUE;
2398 				*trysend = 1;
2399 				if (!*fast_recovery) {
2400 					/*
2401 					 * Entering fast recovery.
2402 					 */
2403 					fp = SCTP_CHUNK_DEST(mp);
2404 					fp->ssthresh = fp->cwnd / 2;
2405 					if (fp->ssthresh < 2 * fp->sfa_pmss) {
2406 						fp->ssthresh =
2407 						    2 * fp->sfa_pmss;
2408 					}
2409 					fp->cwnd = fp->ssthresh;
2410 					fp->pba = 0;
2411 					sctp->sctp_recovery_tsn =
2412 					    sctp->sctp_ltsn - 1;
2413 					*fast_recovery = B_TRUE;
2414 				}
2415 			}
2416 		}
2417 		mp = mp->b_next;
2418 		if (mp == NULL) {
2419 			ump = ump->b_next;
2420 			/* We can't get to the end of the transmit list here */
2421 			ASSERT(ump != NULL);
2422 			mp = ump->b_cont;
2423 		}
2424 		/* We can't hit an unsent chunk here */
2425 		ASSERT(SCTP_CHUNK_ISSENT(mp));
2426 		sdc = (sctp_data_hdr_t *)mp->b_rptr;
2427 		xtsn = ntohl(sdc->sdh_tsn);
2428 	}
2429 	return (acked);
2430 }
2431 
2432 static int
2433 sctp_got_sack(sctp_t *sctp, sctp_chunk_hdr_t *sch)
2434 {
2435 	sctp_sack_chunk_t	*sc;
2436 	sctp_data_hdr_t		*sdc;
2437 	sctp_sack_frag_t	*ssf;
2438 	mblk_t			*ump;
2439 	mblk_t			*mp;
2440 	mblk_t			*mp1;
2441 	uint32_t		cumtsn;
2442 	uint32_t		xtsn;
2443 	uint32_t		gapstart = 0;
2444 	uint32_t		gapend = 0;
2445 	uint32_t		acked = 0;
2446 	uint16_t		chunklen;
2447 	sctp_faddr_t		*fp;
2448 	int			num_gaps;
2449 	int			trysend = 0;
2450 	int			i;
2451 	boolean_t		fast_recovery = B_FALSE;
2452 	boolean_t		cumack_forward = B_FALSE;
2453 	boolean_t		fwd_tsn = B_FALSE;
2454 	sctp_stack_t		*sctps = sctp->sctp_sctps;
2455 
2456 	BUMP_LOCAL(sctp->sctp_ibchunks);
2457 	chunklen = ntohs(sch->sch_len);
2458 	if (chunklen < (sizeof (*sch) + sizeof (*sc)))
2459 		return (0);
2460 
2461 	sc = (sctp_sack_chunk_t *)(sch + 1);
2462 	cumtsn = ntohl(sc->ssc_cumtsn);
2463 
2464 	dprint(2, ("got sack cumtsn %x -> %x\n", sctp->sctp_lastack_rxd,
2465 	    cumtsn));
2466 
2467 	/* out of order */
2468 	if (SEQ_LT(cumtsn, sctp->sctp_lastack_rxd))
2469 		return (0);
2470 
2471 	if (SEQ_GT(cumtsn, sctp->sctp_ltsn - 1)) {
2472 		BUMP_MIB(&sctps->sctps_mib, sctpInAckUnsent);
2473 		/* Send an ABORT */
2474 		return (-1);
2475 	}
2476 
2477 	/*
2478 	 * Cwnd only done when not in fast recovery mode.
2479 	 */
2480 	if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_recovery_tsn))
2481 		fast_recovery = B_TRUE;
2482 
2483 	/*
2484 	 * .. and if the cum TSN is not moving ahead on account Forward TSN
2485 	 */
2486 	if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_adv_pap))
2487 		fwd_tsn = B_TRUE;
2488 
2489 	if (cumtsn == sctp->sctp_lastack_rxd &&
2490 	    (sctp->sctp_xmit_unacked == NULL ||
2491 	    !SCTP_CHUNK_ABANDONED(sctp->sctp_xmit_unacked))) {
2492 		if (sctp->sctp_xmit_unacked != NULL)
2493 			mp = sctp->sctp_xmit_unacked;
2494 		else if (sctp->sctp_xmit_head != NULL)
2495 			mp = sctp->sctp_xmit_head->b_cont;
2496 		else
2497 			mp = NULL;
2498 		BUMP_MIB(&sctps->sctps_mib, sctpInDupAck);
2499 		/*
2500 		 * If we were doing a zero win probe and the win
2501 		 * has now opened to at least MSS, re-transmit the
2502 		 * zero win probe via sctp_rexmit_packet().
2503 		 */
2504 		if (mp != NULL && sctp->sctp_zero_win_probe &&
2505 		    ntohl(sc->ssc_a_rwnd) >= sctp->sctp_current->sfa_pmss) {
2506 			mblk_t	*pkt;
2507 			uint_t	pkt_len;
2508 			mblk_t	*mp1 = mp;
2509 			mblk_t	*meta = sctp->sctp_xmit_head;
2510 
2511 			/*
2512 			 * Reset the RTO since we have been backing-off
2513 			 * to send the ZWP.
2514 			 */
2515 			fp = sctp->sctp_current;
2516 			fp->rto = fp->srtt + 4 * fp->rttvar;
2517 			/* Resend the ZWP */
2518 			pkt = sctp_rexmit_packet(sctp, &meta, &mp1, fp,
2519 			    &pkt_len);
2520 			if (pkt == NULL) {
2521 				SCTP_KSTAT(sctps, sctp_ss_rexmit_failed);
2522 				return (0);
2523 			}
2524 			ASSERT(pkt_len <= fp->sfa_pmss);
2525 			sctp->sctp_zero_win_probe = B_FALSE;
2526 			sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn;
2527 			sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn;
2528 			sctp_set_iplen(sctp, pkt);
2529 			sctp_add_sendq(sctp, pkt);
2530 		}
2531 	} else {
2532 		if (sctp->sctp_zero_win_probe) {
2533 			/*
2534 			 * Reset the RTO since we have been backing-off
2535 			 * to send the ZWP.
2536 			 */
2537 			fp = sctp->sctp_current;
2538 			fp->rto = fp->srtt + 4 * fp->rttvar;
2539 			sctp->sctp_zero_win_probe = B_FALSE;
2540 			/* This is probably not required */
2541 			if (!sctp->sctp_rexmitting) {
2542 				sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn;
2543 				sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn;
2544 			}
2545 		}
2546 		acked = sctp_cumack(sctp, cumtsn, &mp);
2547 		sctp->sctp_xmit_unacked = mp;
2548 		if (acked > 0) {
2549 			trysend = 1;
2550 			cumack_forward = B_TRUE;
2551 			if (fwd_tsn && SEQ_GEQ(sctp->sctp_lastack_rxd,
2552 			    sctp->sctp_adv_pap)) {
2553 				cumack_forward = B_FALSE;
2554 			}
2555 		}
2556 	}
2557 	num_gaps = ntohs(sc->ssc_numfrags);
2558 	if (num_gaps == 0 || mp == NULL || !SCTP_CHUNK_ISSENT(mp) ||
2559 	    chunklen < (sizeof (*sch) + sizeof (*sc) +
2560 	    num_gaps * sizeof (*ssf))) {
2561 		goto ret;
2562 	}
2563 #ifdef	DEBUG
2564 	/*
2565 	 * Since we delete any message that has been acked completely,
2566 	 * the unacked chunk must belong to sctp_xmit_head (as
2567 	 * we don't have a back pointer from the mp to the meta data
2568 	 * we do this).
2569 	 */
2570 	{
2571 		mblk_t	*mp2 = sctp->sctp_xmit_head->b_cont;
2572 
2573 		while (mp2 != NULL) {
2574 			if (mp2 == mp)
2575 				break;
2576 			mp2 = mp2->b_next;
2577 		}
2578 		ASSERT(mp2 != NULL);
2579 	}
2580 #endif
2581 	ump = sctp->sctp_xmit_head;
2582 
2583 	/*
2584 	 * Just remember where we started from, in case we need to call
2585 	 * sctp_process_uo_gaps() if the gap blocks are unordered.
2586 	 */
2587 	mp1 = mp;
2588 
2589 	sdc = (sctp_data_hdr_t *)mp->b_rptr;
2590 	xtsn = ntohl(sdc->sdh_tsn);
2591 	ASSERT(xtsn == cumtsn + 1);
2592 
2593 	/*
2594 	 * Go through SACK gaps. They are ordered based on start TSN.
2595 	 */
2596 	ssf = (sctp_sack_frag_t *)(sc + 1);
2597 	for (i = 0; i < num_gaps; i++, ssf++) {
2598 		if (gapstart != 0) {
2599 			/* check for unordered gap */
2600 			if (SEQ_LEQ(cumtsn + ntohs(ssf->ssf_start), gapstart)) {
2601 				acked += sctp_process_uo_gaps(sctp,
2602 				    cumtsn, ssf, num_gaps - i,
2603 				    sctp->sctp_xmit_head, mp1,
2604 				    &trysend, &fast_recovery, gapstart);
2605 				if (trysend < 0) {
2606 					BUMP_MIB(&sctps->sctps_mib,
2607 					    sctpInAckUnsent);
2608 					return (-1);
2609 				}
2610 				break;
2611 			}
2612 		}
2613 		gapstart = cumtsn + ntohs(ssf->ssf_start);
2614 		gapend = cumtsn + ntohs(ssf->ssf_end);
2615 
2616 		/* SACK for TSN we have not sent - ABORT */
2617 		if (SEQ_GT(gapstart, sctp->sctp_ltsn - 1) ||
2618 		    SEQ_GT(gapend, sctp->sctp_ltsn - 1)) {
2619 			BUMP_MIB(&sctps->sctps_mib, sctpInAckUnsent);
2620 			return (-1);
2621 		} else if (SEQ_LT(gapend, gapstart)) {
2622 			break;
2623 		}
2624 		/*
2625 		 * Let's start at the current TSN (for the 1st gap we start
2626 		 * from the cumulative TSN, for subsequent ones we start from
2627 		 * where the previous gapend was found - second while loop
2628 		 * below) and walk the transmit list till we find the TSN
2629 		 * corresponding to gapstart. All the unacked chunks till we
2630 		 * get to the chunk with TSN == gapstart will have their
2631 		 * SACKCNT incremented by 1. Note since the gap blocks are
2632 		 * ordered, we won't be incrementing the SACKCNT for an
2633 		 * unacked chunk by more than one while processing the gap
2634 		 * blocks. If the SACKCNT for any unacked chunk exceeds
2635 		 * the fast retransmit threshold, we will fast retransmit
2636 		 * after processing all the gap blocks.
2637 		 */
2638 		ASSERT(SEQ_LT(xtsn, gapstart));
2639 		while (xtsn != gapstart) {
2640 			SCTP_CHUNK_SET_SACKCNT(mp, SCTP_CHUNK_SACKCNT(mp) + 1);
2641 			if (SCTP_CHUNK_SACKCNT(mp) ==
2642 			    sctps->sctps_fast_rxt_thresh) {
2643 				SCTP_CHUNK_REXMIT(mp);
2644 				sctp->sctp_chk_fast_rexmit = B_TRUE;
2645 				trysend = 1;
2646 				if (!fast_recovery) {
2647 					/*
2648 					 * Entering fast recovery.
2649 					 */
2650 					fp = SCTP_CHUNK_DEST(mp);
2651 					fp->ssthresh = fp->cwnd / 2;
2652 					if (fp->ssthresh < 2 * fp->sfa_pmss) {
2653 						fp->ssthresh =
2654 						    2 * fp->sfa_pmss;
2655 					}
2656 					fp->cwnd = fp->ssthresh;
2657 					fp->pba = 0;
2658 					sctp->sctp_recovery_tsn =
2659 					    sctp->sctp_ltsn - 1;
2660 					fast_recovery = B_TRUE;
2661 				}
2662 			}
2663 
2664 			/*
2665 			 * Peer may have reneged on this chunk, so un-sack
2666 			 * it now. If the peer did renege, we need to
2667 			 * readjust unacked.
2668 			 */
2669 			if (SCTP_CHUNK_ISACKED(mp)) {
2670 				chunklen = ntohs(sdc->sdh_len);
2671 				fp = SCTP_CHUNK_DEST(mp);
2672 				fp->suna += chunklen;
2673 				sctp->sctp_unacked += chunklen - sizeof (*sdc);
2674 				SCTP_CHUNK_CLEAR_ACKED(mp);
2675 				if (!fp->timer_running) {
2676 					SCTP_FADDR_TIMER_RESTART(sctp, fp,
2677 					    fp->rto);
2678 				}
2679 			}
2680 
2681 			mp = mp->b_next;
2682 			if (mp == NULL) {
2683 				ump = ump->b_next;
2684 				/*
2685 				 * ump can't be NULL given the sanity check
2686 				 * above.
2687 				 */
2688 				ASSERT(ump != NULL);
2689 				mp = ump->b_cont;
2690 			}
2691 			/*
2692 			 * mp can't be unsent given the sanity check above.
2693 			 */
2694 			ASSERT(SCTP_CHUNK_ISSENT(mp));
2695 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
2696 			xtsn = ntohl(sdc->sdh_tsn);
2697 		}
2698 		/*
2699 		 * Now that we have found the chunk with TSN == 'gapstart',
2700 		 * let's walk till we hit the chunk with TSN == 'gapend'.
2701 		 * All intermediate chunks will be marked ACKED, if they
2702 		 * haven't already been.
2703 		 */
2704 		while (SEQ_LEQ(xtsn, gapend)) {
2705 			/*
2706 			 * SACKed
2707 			 */
2708 			SCTP_CHUNK_SET_SACKCNT(mp, 0);
2709 			if (!SCTP_CHUNK_ISACKED(mp)) {
2710 				SCTP_CHUNK_ACKED(mp);
2711 
2712 				fp = SCTP_CHUNK_DEST(mp);
2713 				chunklen = ntohs(sdc->sdh_len);
2714 				ASSERT(fp->suna >= chunklen);
2715 				fp->suna -= chunklen;
2716 				if (fp->suna == 0) {
2717 					/* All outstanding data acked. */
2718 					fp->pba = 0;
2719 					SCTP_FADDR_TIMER_STOP(fp);
2720 				}
2721 				fp->acked += chunklen;
2722 				acked += chunklen;
2723 				sctp->sctp_unacked -= chunklen - sizeof (*sdc);
2724 				ASSERT(sctp->sctp_unacked >= 0);
2725 			}
2726 			/* Go to the next chunk of the current message */
2727 			mp = mp->b_next;
2728 			/*
2729 			 * Move to the next message in the transmit list
2730 			 * if we are done with all the chunks from the current
2731 			 * message. Note, it is possible to hit the end of the
2732 			 * transmit list here, i.e. if we have already completed
2733 			 * processing the gap block.
2734 			 * Also, note that we break here, which means we
2735 			 * continue processing gap blocks, if any. In case of
2736 			 * ordered gap blocks there can't be any following
2737 			 * this (if there is it will fail the sanity check
2738 			 * above). In case of un-ordered gap blocks we will
2739 			 * switch to sctp_process_uo_gaps().  In either case
2740 			 * it should be fine to continue with NULL ump/mp,
2741 			 * but we just reset it to xmit_head.
2742 			 */
2743 			if (mp == NULL) {
2744 				ump = ump->b_next;
2745 				if (ump == NULL) {
2746 					ASSERT(xtsn == gapend);
2747 					ump = sctp->sctp_xmit_head;
2748 					mp = mp1;
2749 					sdc = (sctp_data_hdr_t *)mp->b_rptr;
2750 					xtsn = ntohl(sdc->sdh_tsn);
2751 					break;
2752 				}
2753 				mp = ump->b_cont;
2754 			}
2755 			/*
2756 			 * Likewise, we could hit an unsent chunk once we have
2757 			 * completed processing the gap block. Again, it is
2758 			 * fine to continue processing gap blocks with mp
2759 			 * pointing to the unsent chunk, because if there
2760 			 * are more ordered gap blocks, they will fail the
2761 			 * sanity check, and if there are un-ordered gap blocks,
2762 			 * we will continue processing in sctp_process_uo_gaps()
2763 			 * We just reset the mp to the one we started with.
2764 			 */
2765 			if (!SCTP_CHUNK_ISSENT(mp)) {
2766 				ASSERT(xtsn == gapend);
2767 				ump = sctp->sctp_xmit_head;
2768 				mp = mp1;
2769 				sdc = (sctp_data_hdr_t *)mp->b_rptr;
2770 				xtsn = ntohl(sdc->sdh_tsn);
2771 				break;
2772 			}
2773 			sdc = (sctp_data_hdr_t *)mp->b_rptr;
2774 			xtsn = ntohl(sdc->sdh_tsn);
2775 		}
2776 	}
2777 	if (sctp->sctp_prsctp_aware)
2778 		sctp_check_abandoned_data(sctp, sctp->sctp_current);
2779 	if (sctp->sctp_chk_fast_rexmit)
2780 		sctp_fast_rexmit(sctp);
2781 ret:
2782 	trysend += sctp_set_frwnd(sctp, ntohl(sc->ssc_a_rwnd));
2783 
2784 	/*
2785 	 * If receive window is closed while there is unsent data,
2786 	 * set a timer for doing zero window probes.
2787 	 */
2788 	if (sctp->sctp_frwnd == 0 && sctp->sctp_unacked == 0 &&
2789 	    sctp->sctp_unsent != 0) {
2790 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
2791 		    sctp->sctp_current->rto);
2792 	}
2793 
2794 	/*
2795 	 * Set cwnd for all destinations.
2796 	 * Congestion window gets increased only when cumulative
2797 	 * TSN moves forward, we're not in fast recovery, and
2798 	 * cwnd has been fully utilized (almost fully, need to allow
2799 	 * some leeway due to non-MSS sized messages).
2800 	 */
2801 	if (sctp->sctp_current->acked == acked) {
2802 		/*
2803 		 * Fast-path, only data sent to sctp_current got acked.
2804 		 */
2805 		fp = sctp->sctp_current;
2806 		if (cumack_forward && !fast_recovery &&
2807 		    (fp->acked + fp->suna > fp->cwnd - fp->sfa_pmss)) {
2808 			if (fp->cwnd < fp->ssthresh) {
2809 				/*
2810 				 * Slow start
2811 				 */
2812 				if (fp->acked > fp->sfa_pmss) {
2813 					fp->cwnd += fp->sfa_pmss;
2814 				} else {
2815 					fp->cwnd += fp->acked;
2816 				}
2817 				fp->cwnd = MIN(fp->cwnd, sctp->sctp_cwnd_max);
2818 			} else {
2819 				/*
2820 				 * Congestion avoidance
2821 				 */
2822 				fp->pba += fp->acked;
2823 				if (fp->pba >= fp->cwnd) {
2824 					fp->pba -= fp->cwnd;
2825 					fp->cwnd += fp->sfa_pmss;
2826 					fp->cwnd = MIN(fp->cwnd,
2827 					    sctp->sctp_cwnd_max);
2828 				}
2829 			}
2830 		}
2831 		/*
2832 		 * Limit the burst of transmitted data segments.
2833 		 */
2834 		if (fp->suna + sctps->sctps_maxburst * fp->sfa_pmss <
2835 		    fp->cwnd) {
2836 			fp->cwnd = fp->suna + sctps->sctps_maxburst *
2837 			    fp->sfa_pmss;
2838 		}
2839 		fp->acked = 0;
2840 		goto check_ss_rxmit;
2841 	}
2842 	for (fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
2843 		if (cumack_forward && fp->acked && !fast_recovery &&
2844 		    (fp->acked + fp->suna > fp->cwnd - fp->sfa_pmss)) {
2845 			if (fp->cwnd < fp->ssthresh) {
2846 				if (fp->acked > fp->sfa_pmss) {
2847 					fp->cwnd += fp->sfa_pmss;
2848 				} else {
2849 					fp->cwnd += fp->acked;
2850 				}
2851 				fp->cwnd = MIN(fp->cwnd, sctp->sctp_cwnd_max);
2852 			} else {
2853 				fp->pba += fp->acked;
2854 				if (fp->pba >= fp->cwnd) {
2855 					fp->pba -= fp->cwnd;
2856 					fp->cwnd += fp->sfa_pmss;
2857 					fp->cwnd = MIN(fp->cwnd,
2858 					    sctp->sctp_cwnd_max);
2859 				}
2860 			}
2861 		}
2862 		if (fp->suna + sctps->sctps_maxburst * fp->sfa_pmss <
2863 		    fp->cwnd) {
2864 			fp->cwnd = fp->suna + sctps->sctps_maxburst *
2865 			    fp->sfa_pmss;
2866 		}
2867 		fp->acked = 0;
2868 	}
2869 check_ss_rxmit:
2870 	/*
2871 	 * If this is a SACK following a timeout, check if there are
2872 	 * still unacked chunks (sent before the timeout) that we can
2873 	 * send.
2874 	 */
2875 	if (sctp->sctp_rexmitting) {
2876 		if (SEQ_LT(sctp->sctp_lastack_rxd, sctp->sctp_rxt_maxtsn)) {
2877 			/*
2878 			 * As we are in retransmission phase, we may get a
2879 			 * SACK which indicates some new chunks are received
2880 			 * but cum_tsn does not advance.  During this
2881 			 * phase, the other side advances cum_tsn only because
2882 			 * it receives our retransmitted chunks.  Only
2883 			 * this signals that some chunks are still
2884 			 * missing.
2885 			 */
2886 			if (cumack_forward) {
2887 				fp->rxt_unacked -= acked;
2888 				sctp_ss_rexmit(sctp);
2889 			}
2890 		} else {
2891 			sctp->sctp_rexmitting = B_FALSE;
2892 			sctp->sctp_rxt_nxttsn = sctp->sctp_ltsn;
2893 			sctp->sctp_rxt_maxtsn = sctp->sctp_ltsn;
2894 			fp->rxt_unacked = 0;
2895 		}
2896 	}
2897 	return (trysend);
2898 }
2899 
2900 /*
2901  * Returns 0 if the caller should stop processing any more chunks,
2902  * 1 if the caller should skip this chunk and continue processing.
2903  */
2904 static int
2905 sctp_strange_chunk(sctp_t *sctp, sctp_chunk_hdr_t *ch, sctp_faddr_t *fp)
2906 {
2907 	mblk_t *errmp;
2908 	size_t len;
2909 
2910 	BUMP_LOCAL(sctp->sctp_ibchunks);
2911 	/* check top two bits for action required */
2912 	if (ch->sch_id & 0x40) {	/* also matches 0xc0 */
2913 		len = ntohs(ch->sch_len);
2914 		errmp = sctp_make_err(sctp, SCTP_ERR_UNREC_CHUNK, ch, len);
2915 		if (errmp != NULL)
2916 			sctp_send_err(sctp, errmp, fp);
2917 		if ((ch->sch_id & 0xc0) == 0xc0) {
2918 			/* skip and continue */
2919 			return (1);
2920 		} else {
2921 			/* stop processing */
2922 			return (0);
2923 		}
2924 	}
2925 	if (ch->sch_id & 0x80) {
2926 		/* skip and continue, no error */
2927 		return (1);
2928 	}
2929 	/* top two bits are clear; stop processing and no error */
2930 	return (0);
2931 }
2932 
2933 /*
2934  * Basic sanity checks on all input chunks and parameters: they must
2935  * be of legitimate size for their purported type, and must follow
2936  * ordering conventions as defined in rfc2960.
2937  *
2938  * Returns 1 if the chunk and all encloded params are legitimate,
2939  * 0 otherwise.
2940  */
2941 /*ARGSUSED*/
2942 static int
2943 sctp_check_input(sctp_t *sctp, sctp_chunk_hdr_t *ch, ssize_t len, int first)
2944 {
2945 	sctp_parm_hdr_t	*ph;
2946 	void		*p = NULL;
2947 	ssize_t		clen;
2948 	uint16_t	ch_len;
2949 
2950 	ch_len = ntohs(ch->sch_len);
2951 	if (ch_len > len) {
2952 		return (0);
2953 	}
2954 
2955 	switch (ch->sch_id) {
2956 	case CHUNK_DATA:
2957 		if (ch_len < sizeof (sctp_data_hdr_t)) {
2958 			return (0);
2959 		}
2960 		return (1);
2961 	case CHUNK_INIT:
2962 	case CHUNK_INIT_ACK:
2963 		{
2964 			ssize_t	remlen = len;
2965 
2966 			/*
2967 			 * INIT and INIT-ACK chunks must not be bundled with
2968 			 * any other.
2969 			 */
2970 			if (!first || sctp_next_chunk(ch, &remlen) != NULL ||
2971 			    (ch_len < (sizeof (*ch) +
2972 			    sizeof (sctp_init_chunk_t)))) {
2973 				return (0);
2974 			}
2975 			/* may have params that need checking */
2976 			p = (char *)(ch + 1) + sizeof (sctp_init_chunk_t);
2977 			clen = ch_len - (sizeof (*ch) +
2978 			    sizeof (sctp_init_chunk_t));
2979 		}
2980 		break;
2981 	case CHUNK_SACK:
2982 		if (ch_len < (sizeof (*ch) + sizeof (sctp_sack_chunk_t))) {
2983 			return (0);
2984 		}
2985 		/* dup and gap reports checked by got_sack() */
2986 		return (1);
2987 	case CHUNK_SHUTDOWN:
2988 		if (ch_len < (sizeof (*ch) + sizeof (uint32_t))) {
2989 			return (0);
2990 		}
2991 		return (1);
2992 	case CHUNK_ABORT:
2993 	case CHUNK_ERROR:
2994 		if (ch_len < sizeof (*ch)) {
2995 			return (0);
2996 		}
2997 		/* may have params that need checking */
2998 		p = ch + 1;
2999 		clen = ch_len - sizeof (*ch);
3000 		break;
3001 	case CHUNK_ECNE:
3002 	case CHUNK_CWR:
3003 	case CHUNK_HEARTBEAT:
3004 	case CHUNK_HEARTBEAT_ACK:
3005 	/* Full ASCONF chunk and parameter checks are in asconf.c */
3006 	case CHUNK_ASCONF:
3007 	case CHUNK_ASCONF_ACK:
3008 		if (ch_len < sizeof (*ch)) {
3009 			return (0);
3010 		}
3011 		/* heartbeat data checked by process_heartbeat() */
3012 		return (1);
3013 	case CHUNK_SHUTDOWN_COMPLETE:
3014 		{
3015 			ssize_t remlen = len;
3016 
3017 			/*
3018 			 * SHUTDOWN-COMPLETE chunk must not be bundled with any
3019 			 * other
3020 			 */
3021 			if (!first || sctp_next_chunk(ch, &remlen) != NULL ||
3022 			    ch_len < sizeof (*ch)) {
3023 				return (0);
3024 			}
3025 		}
3026 		return (1);
3027 	case CHUNK_COOKIE:
3028 	case CHUNK_COOKIE_ACK:
3029 	case CHUNK_SHUTDOWN_ACK:
3030 		if (ch_len < sizeof (*ch) || !first) {
3031 			return (0);
3032 		}
3033 		return (1);
3034 	case CHUNK_FORWARD_TSN:
3035 		if (ch_len < (sizeof (*ch) + sizeof (uint32_t)))
3036 			return (0);
3037 		return (1);
3038 	default:
3039 		return (1);	/* handled by strange_chunk() */
3040 	}
3041 
3042 	/* check and byteorder parameters */
3043 	if (clen <= 0) {
3044 		return (1);
3045 	}
3046 	ASSERT(p != NULL);
3047 
3048 	ph = p;
3049 	while (ph != NULL && clen > 0) {
3050 		ch_len = ntohs(ph->sph_len);
3051 		if (ch_len > len || ch_len < sizeof (*ph)) {
3052 			return (0);
3053 		}
3054 		ph = sctp_next_parm(ph, &clen);
3055 	}
3056 
3057 	/* All OK */
3058 	return (1);
3059 }
3060 
3061 /* ARGSUSED */
3062 static sctp_hdr_t *
3063 find_sctp_hdrs(mblk_t *mp, in6_addr_t *src, in6_addr_t *dst,
3064     uint_t *ifindex, uint_t *ip_hdr_len, ip6_pkt_t *ipp, ip_pktinfo_t *pinfo)
3065 {
3066 	uchar_t	*rptr;
3067 	ipha_t	*ip4h;
3068 	ip6_t	*ip6h;
3069 	mblk_t	*mp1;
3070 
3071 	rptr = mp->b_rptr;
3072 	if (IPH_HDR_VERSION(rptr) == IPV4_VERSION) {
3073 		*ip_hdr_len = IPH_HDR_LENGTH(rptr);
3074 		ip4h = (ipha_t *)rptr;
3075 		IN6_IPADDR_TO_V4MAPPED(ip4h->ipha_src, src);
3076 		IN6_IPADDR_TO_V4MAPPED(ip4h->ipha_dst, dst);
3077 
3078 		ipp->ipp_fields |= IPPF_HOPLIMIT;
3079 		ipp->ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl;
3080 		if (pinfo != NULL && (pinfo->ip_pkt_flags & IPF_RECVIF)) {
3081 			ipp->ipp_fields |= IPPF_IFINDEX;
3082 			ipp->ipp_ifindex = pinfo->ip_pkt_ifindex;
3083 		}
3084 	} else {
3085 		ASSERT(IPH_HDR_VERSION(rptr) == IPV6_VERSION);
3086 		ip6h = (ip6_t *)rptr;
3087 		ipp->ipp_fields = IPPF_HOPLIMIT;
3088 		ipp->ipp_hoplimit = ip6h->ip6_hops;
3089 
3090 		if (ip6h->ip6_nxt != IPPROTO_SCTP) {
3091 			/* Look for ifindex information */
3092 			if (ip6h->ip6_nxt == IPPROTO_RAW) {
3093 				ip6i_t *ip6i = (ip6i_t *)ip6h;
3094 
3095 				if (ip6i->ip6i_flags & IP6I_IFINDEX) {
3096 					ASSERT(ip6i->ip6i_ifindex != 0);
3097 					ipp->ipp_fields |= IPPF_IFINDEX;
3098 					ipp->ipp_ifindex = ip6i->ip6i_ifindex;
3099 				}
3100 				rptr = (uchar_t *)&ip6i[1];
3101 				mp->b_rptr = rptr;
3102 				if (rptr == mp->b_wptr) {
3103 					mp1 = mp->b_cont;
3104 					freeb(mp);
3105 					mp = mp1;
3106 					rptr = mp->b_rptr;
3107 				}
3108 				ASSERT(mp->b_wptr - rptr >=
3109 				    IPV6_HDR_LEN + sizeof (sctp_hdr_t));
3110 				ip6h = (ip6_t *)rptr;
3111 			}
3112 			/*
3113 			 * Find any potentially interesting extension headers
3114 			 * as well as the length of the IPv6 + extension
3115 			 * headers.
3116 			 */
3117 			*ip_hdr_len = ip_find_hdr_v6(mp, ip6h, ipp, NULL);
3118 		} else {
3119 			*ip_hdr_len = IPV6_HDR_LEN;
3120 		}
3121 		*src = ip6h->ip6_src;
3122 		*dst = ip6h->ip6_dst;
3123 	}
3124 	ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX);
3125 	return ((sctp_hdr_t *)&rptr[*ip_hdr_len]);
3126 #undef IPVER
3127 }
3128 
3129 static mblk_t *
3130 sctp_check_in_policy(mblk_t *mp, mblk_t *ipsec_mp)
3131 {
3132 	ipsec_in_t *ii;
3133 	boolean_t check = B_TRUE;
3134 	boolean_t policy_present;
3135 	ipha_t *ipha;
3136 	ip6_t *ip6h;
3137 	netstack_t	*ns;
3138 	ipsec_stack_t	*ipss;
3139 
3140 	ii = (ipsec_in_t *)ipsec_mp->b_rptr;
3141 	ASSERT(ii->ipsec_in_type == IPSEC_IN);
3142 	ns = ii->ipsec_in_ns;
3143 	ipss = ns->netstack_ipsec;
3144 
3145 	if (ii->ipsec_in_dont_check) {
3146 		check = B_FALSE;
3147 		if (!ii->ipsec_in_secure) {
3148 			freeb(ipsec_mp);
3149 			ipsec_mp = NULL;
3150 		}
3151 	}
3152 	if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) {
3153 		policy_present = ipss->ipsec_inbound_v4_policy_present;
3154 		ipha = (ipha_t *)mp->b_rptr;
3155 		ip6h = NULL;
3156 	} else {
3157 		policy_present = ipss->ipsec_inbound_v6_policy_present;
3158 		ipha = NULL;
3159 		ip6h = (ip6_t *)mp->b_rptr;
3160 	}
3161 
3162 	if (check && policy_present) {
3163 		/*
3164 		 * The conn_t parameter is NULL because we already know
3165 		 * nobody's home.
3166 		 */
3167 		ipsec_mp = ipsec_check_global_policy(ipsec_mp, (conn_t *)NULL,
3168 		    ipha, ip6h, B_TRUE, ns);
3169 		if (ipsec_mp == NULL)
3170 			return (NULL);
3171 	}
3172 	if (ipsec_mp != NULL)
3173 		freeb(ipsec_mp);
3174 	return (mp);
3175 }
3176 
3177 /* Handle out-of-the-blue packets */
3178 void
3179 sctp_ootb_input(mblk_t *mp, ill_t *recv_ill, zoneid_t zoneid,
3180     boolean_t mctl_present)
3181 {
3182 	sctp_t			*sctp;
3183 	sctp_chunk_hdr_t	*ch;
3184 	sctp_hdr_t		*sctph;
3185 	in6_addr_t		src, dst;
3186 	uint_t			ip_hdr_len;
3187 	uint_t			ifindex;
3188 	ip6_pkt_t		ipp;
3189 	ssize_t			mlen;
3190 	ip_pktinfo_t		*pinfo = NULL;
3191 	mblk_t			*first_mp;
3192 	sctp_stack_t		*sctps;
3193 	ip_stack_t		*ipst;
3194 
3195 	ASSERT(recv_ill != NULL);
3196 	ipst = recv_ill->ill_ipst;
3197 	sctps = ipst->ips_netstack->netstack_sctp;
3198 
3199 	BUMP_MIB(&sctps->sctps_mib, sctpOutOfBlue);
3200 	BUMP_MIB(&sctps->sctps_mib, sctpInSCTPPkts);
3201 
3202 	if (sctps->sctps_gsctp == NULL) {
3203 		/*
3204 		 * For non-zero stackids the default queue isn't created
3205 		 * until the first open, thus there can be a need to send
3206 		 * an error before then. But we can't do that, hence we just
3207 		 * drop the packet. Later during boot, when the default queue
3208 		 * has been setup, a retransmitted packet from the peer
3209 		 * will result in a error.
3210 		 */
3211 		ASSERT(sctps->sctps_netstack->netstack_stackid !=
3212 		    GLOBAL_NETSTACKID);
3213 		freemsg(mp);
3214 		return;
3215 	}
3216 
3217 	first_mp = mp;
3218 	if (mctl_present)
3219 		mp = mp->b_cont;
3220 
3221 	/* Initiate IPPf processing, if needed. */
3222 	if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) {
3223 		ip_process(IPP_LOCAL_IN, &mp,
3224 		    recv_ill->ill_phyint->phyint_ifindex);
3225 		if (mp == NULL) {
3226 			if (mctl_present)
3227 				freeb(first_mp);
3228 			return;
3229 		}
3230 	}
3231 
3232 	if (mp->b_cont != NULL) {
3233 		/*
3234 		 * All subsequent code is vastly simplified if it can
3235 		 * assume a single contiguous chunk of data.
3236 		 */
3237 		if (pullupmsg(mp, -1) == 0) {
3238 			BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
3239 			freemsg(first_mp);
3240 			return;
3241 		}
3242 	}
3243 
3244 	/*
3245 	 * We don't really need to call this function...  Need to
3246 	 * optimize later.
3247 	 */
3248 	sctph = find_sctp_hdrs(mp, &src, &dst, &ifindex, &ip_hdr_len,
3249 	    &ipp, pinfo);
3250 	mlen = mp->b_wptr - (uchar_t *)(sctph + 1);
3251 	if ((ch = sctp_first_chunk((uchar_t *)(sctph + 1), mlen)) == NULL) {
3252 		dprint(3, ("sctp_ootb_input: invalid packet\n"));
3253 		BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
3254 		freemsg(first_mp);
3255 		return;
3256 	}
3257 
3258 	switch (ch->sch_id) {
3259 	case CHUNK_INIT:
3260 		/* no listener; send abort  */
3261 		if (mctl_present && sctp_check_in_policy(mp, first_mp) == NULL)
3262 			return;
3263 		sctp_send_abort(sctps->sctps_gsctp, sctp_init2vtag(ch), 0,
3264 		    NULL, 0, mp, 0, B_TRUE);
3265 		break;
3266 	case CHUNK_INIT_ACK:
3267 		/* check for changed src addr */
3268 		sctp = sctp_addrlist2sctp(mp, sctph, ch, zoneid, sctps);
3269 		if (sctp != NULL) {
3270 			/* success; proceed to normal path */
3271 			mutex_enter(&sctp->sctp_lock);
3272 			if (sctp->sctp_running) {
3273 				if (!sctp_add_recvq(sctp, mp, B_FALSE)) {
3274 					BUMP_MIB(recv_ill->ill_ip_mib,
3275 					    ipIfStatsInDiscards);
3276 					freemsg(mp);
3277 				}
3278 				mutex_exit(&sctp->sctp_lock);
3279 			} else {
3280 				/*
3281 				 * If the source address is changed, we
3282 				 * don't need to worry too much about
3283 				 * out of order processing.  So we don't
3284 				 * check if the recvq is empty or not here.
3285 				 */
3286 				sctp->sctp_running = B_TRUE;
3287 				mutex_exit(&sctp->sctp_lock);
3288 				sctp_input_data(sctp, mp, NULL);
3289 				WAKE_SCTP(sctp);
3290 				sctp_process_sendq(sctp);
3291 			}
3292 			SCTP_REFRELE(sctp);
3293 			return;
3294 		}
3295 		if (mctl_present)
3296 			freeb(first_mp);
3297 		/* else bogus init ack; drop it */
3298 		break;
3299 	case CHUNK_SHUTDOWN_ACK:
3300 		if (mctl_present && sctp_check_in_policy(mp, first_mp) == NULL)
3301 			return;
3302 		sctp_ootb_shutdown_ack(sctps->sctps_gsctp, mp, ip_hdr_len);
3303 		sctp_process_sendq(sctps->sctps_gsctp);
3304 		return;
3305 	case CHUNK_ERROR:
3306 	case CHUNK_ABORT:
3307 	case CHUNK_COOKIE_ACK:
3308 	case CHUNK_SHUTDOWN_COMPLETE:
3309 		if (mctl_present)
3310 			freeb(first_mp);
3311 		break;
3312 	default:
3313 		if (mctl_present && sctp_check_in_policy(mp, first_mp) == NULL)
3314 			return;
3315 		sctp_send_abort(sctps->sctps_gsctp, sctph->sh_verf, 0,
3316 		    NULL, 0, mp, 0, B_TRUE);
3317 		break;
3318 	}
3319 	sctp_process_sendq(sctps->sctps_gsctp);
3320 	freemsg(mp);
3321 }
3322 
3323 void
3324 sctp_input(conn_t *connp, ipha_t *ipha, mblk_t *mp, mblk_t *first_mp,
3325     ill_t *recv_ill, boolean_t isv4, boolean_t mctl_present)
3326 {
3327 	sctp_t *sctp = CONN2SCTP(connp);
3328 	ip_stack_t	*ipst = recv_ill->ill_ipst;
3329 	ipsec_stack_t	*ipss = ipst->ips_netstack->netstack_ipsec;
3330 
3331 	/*
3332 	 * We check some fields in conn_t without holding a lock.
3333 	 * This should be fine.
3334 	 */
3335 	if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || mctl_present) {
3336 		first_mp = ipsec_check_inbound_policy(first_mp, connp,
3337 		    ipha, NULL, mctl_present);
3338 		if (first_mp == NULL) {
3339 			BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
3340 			SCTP_REFRELE(sctp);
3341 			return;
3342 		}
3343 	}
3344 
3345 	/* Initiate IPPF processing for fastpath */
3346 	if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) {
3347 		ip_process(IPP_LOCAL_IN, &mp,
3348 		    recv_ill->ill_phyint->phyint_ifindex);
3349 		if (mp == NULL) {
3350 			SCTP_REFRELE(sctp);
3351 			if (mctl_present)
3352 				freeb(first_mp);
3353 			return;
3354 		} else if (mctl_present) {
3355 			/*
3356 			 * ip_process might return a new mp.
3357 			 */
3358 			ASSERT(first_mp != mp);
3359 			first_mp->b_cont = mp;
3360 		} else {
3361 			first_mp = mp;
3362 		}
3363 	}
3364 
3365 	if (connp->conn_recvif || connp->conn_recvslla ||
3366 	    connp->conn_ip_recvpktinfo) {
3367 		int in_flags = 0;
3368 
3369 		if (connp->conn_recvif || connp->conn_ip_recvpktinfo) {
3370 			in_flags = IPF_RECVIF;
3371 		}
3372 		if (connp->conn_recvslla) {
3373 			in_flags |= IPF_RECVSLLA;
3374 		}
3375 		if (isv4) {
3376 			mp = ip_add_info(mp, recv_ill, in_flags,
3377 			    IPCL_ZONEID(connp), ipst);
3378 		} else {
3379 			mp = ip_add_info_v6(mp, recv_ill,
3380 			    &(((ip6_t *)ipha)->ip6_dst));
3381 		}
3382 		if (mp == NULL) {
3383 			BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
3384 			SCTP_REFRELE(sctp);
3385 			if (mctl_present)
3386 				freeb(first_mp);
3387 			return;
3388 		} else if (mctl_present) {
3389 			/*
3390 			 * ip_add_info might return a new mp.
3391 			 */
3392 			ASSERT(first_mp != mp);
3393 			first_mp->b_cont = mp;
3394 		} else {
3395 			first_mp = mp;
3396 		}
3397 	}
3398 
3399 	mutex_enter(&sctp->sctp_lock);
3400 	if (sctp->sctp_running) {
3401 		if (mctl_present)
3402 			mp->b_prev = first_mp;
3403 		if (!sctp_add_recvq(sctp, mp, B_FALSE)) {
3404 			BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
3405 			freemsg(first_mp);
3406 		}
3407 		mutex_exit(&sctp->sctp_lock);
3408 		SCTP_REFRELE(sctp);
3409 		return;
3410 	} else {
3411 		sctp->sctp_running = B_TRUE;
3412 		mutex_exit(&sctp->sctp_lock);
3413 
3414 		mutex_enter(&sctp->sctp_recvq_lock);
3415 		if (sctp->sctp_recvq != NULL) {
3416 			if (mctl_present)
3417 				mp->b_prev = first_mp;
3418 			if (!sctp_add_recvq(sctp, mp, B_TRUE)) {
3419 				BUMP_MIB(recv_ill->ill_ip_mib,
3420 				    ipIfStatsInDiscards);
3421 				freemsg(first_mp);
3422 			}
3423 			mutex_exit(&sctp->sctp_recvq_lock);
3424 			WAKE_SCTP(sctp);
3425 			SCTP_REFRELE(sctp);
3426 			return;
3427 		}
3428 	}
3429 	mutex_exit(&sctp->sctp_recvq_lock);
3430 	sctp_input_data(sctp, mp, (mctl_present ? first_mp : NULL));
3431 	WAKE_SCTP(sctp);
3432 	sctp_process_sendq(sctp);
3433 	SCTP_REFRELE(sctp);
3434 }
3435 
3436 static void
3437 sctp_process_abort(sctp_t *sctp, sctp_chunk_hdr_t *ch, int err)
3438 {
3439 	sctp_stack_t	*sctps = sctp->sctp_sctps;
3440 
3441 	BUMP_MIB(&sctps->sctps_mib, sctpAborted);
3442 	BUMP_LOCAL(sctp->sctp_ibchunks);
3443 
3444 	sctp_assoc_event(sctp, SCTP_COMM_LOST,
3445 	    ntohs(((sctp_parm_hdr_t *)(ch + 1))->sph_type), ch);
3446 	sctp_clean_death(sctp, err);
3447 }
3448 
3449 void
3450 sctp_input_data(sctp_t *sctp, mblk_t *mp, mblk_t *ipsec_mp)
3451 {
3452 	sctp_chunk_hdr_t	*ch;
3453 	ssize_t			mlen;
3454 	int			gotdata;
3455 	int			trysend;
3456 	sctp_faddr_t		*fp;
3457 	sctp_init_chunk_t	*iack;
3458 	uint32_t		tsn;
3459 	sctp_data_hdr_t		*sdc;
3460 	ip6_pkt_t		ipp;
3461 	in6_addr_t		src;
3462 	in6_addr_t		dst;
3463 	uint_t			ifindex;
3464 	sctp_hdr_t		*sctph;
3465 	uint_t			ip_hdr_len;
3466 	mblk_t			*dups = NULL;
3467 	int			recv_adaption;
3468 	boolean_t		wake_eager = B_FALSE;
3469 	mblk_t			*pinfo_mp;
3470 	ip_pktinfo_t		*pinfo = NULL;
3471 	in6_addr_t		peer_src;
3472 	int64_t			now;
3473 	sctp_stack_t		*sctps = sctp->sctp_sctps;
3474 	ip_stack_t		*ipst = sctps->sctps_netstack->netstack_ip;
3475 
3476 	if (DB_TYPE(mp) != M_DATA) {
3477 		ASSERT(DB_TYPE(mp) == M_CTL);
3478 		if (MBLKL(mp) == sizeof (ip_pktinfo_t) &&
3479 		    ((ip_pktinfo_t *)mp->b_rptr)->ip_pkt_ulp_type ==
3480 		    IN_PKTINFO) {
3481 			pinfo = (ip_pktinfo_t *)mp->b_rptr;
3482 			pinfo_mp = mp;
3483 			mp = mp->b_cont;
3484 		} else {
3485 			if (ipsec_mp != NULL)
3486 				freeb(ipsec_mp);
3487 			sctp_icmp_error(sctp, mp);
3488 			return;
3489 		}
3490 	}
3491 	ASSERT(DB_TYPE(mp) == M_DATA);
3492 
3493 	if (mp->b_cont != NULL) {
3494 		/*
3495 		 * All subsequent code is vastly simplified if it can
3496 		 * assume a single contiguous chunk of data.
3497 		 */
3498 		if (pullupmsg(mp, -1) == 0) {
3499 			BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
3500 			if (ipsec_mp != NULL)
3501 				freeb(ipsec_mp);
3502 			if (pinfo != NULL)
3503 				freeb(pinfo_mp);
3504 			freemsg(mp);
3505 			return;
3506 		}
3507 	}
3508 
3509 	BUMP_LOCAL(sctp->sctp_ipkts);
3510 	sctph = find_sctp_hdrs(mp, &src, &dst, &ifindex, &ip_hdr_len,
3511 	    &ipp, pinfo);
3512 	if (pinfo != NULL)
3513 		freeb(pinfo_mp);
3514 	mlen = mp->b_wptr - (uchar_t *)(sctph + 1);
3515 	ch = sctp_first_chunk((uchar_t *)(sctph + 1), mlen);
3516 	if (ch == NULL) {
3517 		BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
3518 		if (ipsec_mp != NULL)
3519 			freeb(ipsec_mp);
3520 		freemsg(mp);
3521 		return;
3522 	}
3523 
3524 	if (!sctp_check_input(sctp, ch, mlen, 1)) {
3525 		BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
3526 		goto done;
3527 	}
3528 	/*
3529 	 * Check verfication tag (special handling for INIT,
3530 	 * COOKIE, SHUTDOWN_COMPLETE and SHUTDOWN_ACK chunks).
3531 	 * ABORTs are handled in the chunk processing loop, since
3532 	 * may not appear first. All other checked chunks must
3533 	 * appear first, or will have been dropped by check_input().
3534 	 */
3535 	switch (ch->sch_id) {
3536 	case CHUNK_INIT:
3537 		if (sctph->sh_verf != 0) {
3538 			/* drop it */
3539 			goto done;
3540 		}
3541 		break;
3542 	case CHUNK_SHUTDOWN_COMPLETE:
3543 		if (sctph->sh_verf == sctp->sctp_lvtag)
3544 			break;
3545 		if (sctph->sh_verf == sctp->sctp_fvtag &&
3546 		    SCTP_GET_TBIT(ch)) {
3547 			break;
3548 		}
3549 		/* else drop it */
3550 		goto done;
3551 	case CHUNK_ABORT:
3552 	case CHUNK_COOKIE:
3553 		/* handled below */
3554 		break;
3555 	case CHUNK_SHUTDOWN_ACK:
3556 		if (sctp->sctp_state > SCTPS_BOUND &&
3557 		    sctp->sctp_state < SCTPS_ESTABLISHED) {
3558 			/* treat as OOTB */
3559 			sctp_ootb_shutdown_ack(sctp, mp, ip_hdr_len);
3560 			if (ipsec_mp != NULL)
3561 				freeb(ipsec_mp);
3562 			return;
3563 		}
3564 		/* else fallthru */
3565 	default:
3566 		/*
3567 		 * All other packets must have a valid
3568 		 * verification tag, however if this is a
3569 		 * listener, we use a refined version of
3570 		 * out-of-the-blue logic.
3571 		 */
3572 		if (sctph->sh_verf != sctp->sctp_lvtag &&
3573 		    sctp->sctp_state != SCTPS_LISTEN) {
3574 			/* drop it */
3575 			goto done;
3576 		}
3577 		break;
3578 	}
3579 
3580 	/* Have a valid sctp for this packet */
3581 	fp = sctp_lookup_faddr(sctp, &src);
3582 	dprint(2, ("sctp_dispatch_rput: mp=%p fp=%p sctp=%p\n", (void *)mp,
3583 	    (void *)fp, (void *)sctp));
3584 
3585 	gotdata = 0;
3586 	trysend = 0;
3587 
3588 	now = lbolt64;
3589 	/* Process the chunks */
3590 	do {
3591 		dprint(3, ("sctp_dispatch_rput: state=%d, chunk id=%d\n",
3592 		    sctp->sctp_state, (int)(ch->sch_id)));
3593 
3594 		if (ch->sch_id == CHUNK_ABORT) {
3595 			if (sctph->sh_verf != sctp->sctp_lvtag &&
3596 			    sctph->sh_verf != sctp->sctp_fvtag) {
3597 				/* drop it */
3598 				goto done;
3599 			}
3600 		}
3601 
3602 		switch (sctp->sctp_state) {
3603 
3604 		case SCTPS_ESTABLISHED:
3605 		case SCTPS_SHUTDOWN_PENDING:
3606 		case SCTPS_SHUTDOWN_SENT:
3607 			switch (ch->sch_id) {
3608 			case CHUNK_DATA:
3609 				/* 0-length data chunks are not allowed */
3610 				if (ntohs(ch->sch_len) == sizeof (*sdc)) {
3611 					sdc = (sctp_data_hdr_t *)ch;
3612 					tsn = sdc->sdh_tsn;
3613 					sctp_send_abort(sctp, sctp->sctp_fvtag,
3614 					    SCTP_ERR_NO_USR_DATA, (char *)&tsn,
3615 					    sizeof (tsn), mp, 0, B_FALSE);
3616 					sctp_assoc_event(sctp, SCTP_COMM_LOST,
3617 					    0, NULL);
3618 					sctp_clean_death(sctp, ECONNABORTED);
3619 					goto done;
3620 				}
3621 
3622 				ASSERT(fp != NULL);
3623 				sctp->sctp_lastdata = fp;
3624 				sctp_data_chunk(sctp, ch, mp, &dups, fp, &ipp);
3625 				gotdata = 1;
3626 				/* Restart shutdown timer if shutting down */
3627 				if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) {
3628 					/*
3629 					 * If we have exceeded our max
3630 					 * wait bound for waiting for a
3631 					 * shutdown ack from the peer,
3632 					 * abort the association.
3633 					 */
3634 					if (sctps->sctps_shutack_wait_bound !=
3635 					    0 &&
3636 					    TICK_TO_MSEC(now -
3637 					    sctp->sctp_out_time) >
3638 					    sctps->sctps_shutack_wait_bound) {
3639 						sctp_send_abort(sctp,
3640 						    sctp->sctp_fvtag, 0, NULL,
3641 						    0, mp, 0, B_FALSE);
3642 						sctp_assoc_event(sctp,
3643 						    SCTP_COMM_LOST, 0, NULL);
3644 						sctp_clean_death(sctp,
3645 						    ECONNABORTED);
3646 						goto done;
3647 					}
3648 					SCTP_FADDR_TIMER_RESTART(sctp, fp,
3649 					    fp->rto);
3650 				}
3651 				break;
3652 			case CHUNK_SACK:
3653 				ASSERT(fp != NULL);
3654 				/*
3655 				 * Peer is real and alive if it can ack our
3656 				 * data.
3657 				 */
3658 				sctp_faddr_alive(sctp, fp);
3659 				trysend = sctp_got_sack(sctp, ch);
3660 				if (trysend < 0) {
3661 					sctp_send_abort(sctp, sctph->sh_verf,
3662 					    0, NULL, 0, mp, 0, B_FALSE);
3663 					sctp_assoc_event(sctp,
3664 					    SCTP_COMM_LOST, 0, NULL);
3665 					sctp_clean_death(sctp,
3666 					    ECONNABORTED);
3667 					goto done;
3668 				}
3669 				break;
3670 			case CHUNK_HEARTBEAT:
3671 				sctp_return_heartbeat(sctp, ch, mp);
3672 				break;
3673 			case CHUNK_HEARTBEAT_ACK:
3674 				sctp_process_heartbeat(sctp, ch);
3675 				break;
3676 			case CHUNK_SHUTDOWN:
3677 				sctp_shutdown_event(sctp);
3678 				trysend = sctp_shutdown_received(sctp, ch,
3679 				    B_FALSE, B_FALSE, fp);
3680 				BUMP_LOCAL(sctp->sctp_ibchunks);
3681 				break;
3682 			case CHUNK_SHUTDOWN_ACK:
3683 				BUMP_LOCAL(sctp->sctp_ibchunks);
3684 				if (sctp->sctp_state == SCTPS_SHUTDOWN_SENT) {
3685 					sctp_shutdown_complete(sctp);
3686 					BUMP_MIB(&sctps->sctps_mib,
3687 					    sctpShutdowns);
3688 					sctp_assoc_event(sctp,
3689 					    SCTP_SHUTDOWN_COMP, 0, NULL);
3690 					sctp_clean_death(sctp, 0);
3691 					goto done;
3692 				}
3693 				break;
3694 			case CHUNK_ABORT: {
3695 				sctp_saddr_ipif_t *sp;
3696 
3697 				/* Ignore if delete pending */
3698 				sp = sctp_saddr_lookup(sctp, &dst, 0);
3699 				ASSERT(sp != NULL);
3700 				if (sp->saddr_ipif_delete_pending) {
3701 					BUMP_LOCAL(sctp->sctp_ibchunks);
3702 					break;
3703 				}
3704 
3705 				sctp_process_abort(sctp, ch, ECONNRESET);
3706 				goto done;
3707 			}
3708 			case CHUNK_INIT:
3709 				sctp_send_initack(sctp, sctph, ch, mp);
3710 				break;
3711 			case CHUNK_COOKIE:
3712 				if (sctp_process_cookie(sctp, ch, mp, &iack,
3713 				    sctph, &recv_adaption, NULL) != -1) {
3714 					sctp_send_cookie_ack(sctp);
3715 					sctp_assoc_event(sctp, SCTP_RESTART,
3716 					    0, NULL);
3717 					if (recv_adaption) {
3718 						sctp->sctp_recv_adaption = 1;
3719 						sctp_adaption_event(sctp);
3720 					}
3721 				} else {
3722 					BUMP_MIB(&sctps->sctps_mib,
3723 					    sctpInInvalidCookie);
3724 				}
3725 				break;
3726 			case CHUNK_ERROR: {
3727 				int error;
3728 
3729 				BUMP_LOCAL(sctp->sctp_ibchunks);
3730 				error = sctp_handle_error(sctp, sctph, ch, mp);
3731 				if (error != 0) {
3732 					sctp_assoc_event(sctp, SCTP_COMM_LOST,
3733 					    0, NULL);
3734 					sctp_clean_death(sctp, error);
3735 					goto done;
3736 				}
3737 				break;
3738 			}
3739 			case CHUNK_ASCONF:
3740 				ASSERT(fp != NULL);
3741 				sctp_input_asconf(sctp, ch, fp);
3742 				BUMP_LOCAL(sctp->sctp_ibchunks);
3743 				break;
3744 			case CHUNK_ASCONF_ACK:
3745 				ASSERT(fp != NULL);
3746 				sctp_faddr_alive(sctp, fp);
3747 				sctp_input_asconf_ack(sctp, ch, fp);
3748 				BUMP_LOCAL(sctp->sctp_ibchunks);
3749 				break;
3750 			case CHUNK_FORWARD_TSN:
3751 				ASSERT(fp != NULL);
3752 				sctp->sctp_lastdata = fp;
3753 				sctp_process_forward_tsn(sctp, ch, fp, &ipp);
3754 				gotdata = 1;
3755 				BUMP_LOCAL(sctp->sctp_ibchunks);
3756 				break;
3757 			default:
3758 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
3759 					goto nomorechunks;
3760 				} /* else skip and continue processing */
3761 				break;
3762 			}
3763 			break;
3764 
3765 		case SCTPS_LISTEN:
3766 			switch (ch->sch_id) {
3767 			case CHUNK_INIT:
3768 				sctp_send_initack(sctp, sctph, ch, mp);
3769 				break;
3770 			case CHUNK_COOKIE: {
3771 				sctp_t *eager;
3772 
3773 				if (sctp_process_cookie(sctp, ch, mp, &iack,
3774 				    sctph, &recv_adaption, &peer_src) == -1) {
3775 					BUMP_MIB(&sctps->sctps_mib,
3776 					    sctpInInvalidCookie);
3777 					goto done;
3778 				}
3779 
3780 				/*
3781 				 * The cookie is good; ensure that
3782 				 * the peer used the verification
3783 				 * tag from the init ack in the header.
3784 				 */
3785 				if (iack->sic_inittag != sctph->sh_verf)
3786 					goto done;
3787 
3788 				eager = sctp_conn_request(sctp, mp, ifindex,
3789 				    ip_hdr_len, iack, ipsec_mp);
3790 				if (eager == NULL) {
3791 					sctp_send_abort(sctp, sctph->sh_verf,
3792 					    SCTP_ERR_NO_RESOURCES, NULL, 0, mp,
3793 					    0, B_FALSE);
3794 					goto done;
3795 				}
3796 
3797 				/*
3798 				 * If there were extra chunks
3799 				 * bundled with the cookie,
3800 				 * they must be processed
3801 				 * on the eager's queue. We
3802 				 * accomplish this by refeeding
3803 				 * the whole packet into the
3804 				 * state machine on the right
3805 				 * q. The packet (mp) gets
3806 				 * there via the eager's
3807 				 * cookie_mp field (overloaded
3808 				 * with the active open role).
3809 				 * This is picked up when
3810 				 * processing the null bind
3811 				 * request put on the eager's
3812 				 * q by sctp_accept(). We must
3813 				 * first revert the cookie
3814 				 * chunk's length field to network
3815 				 * byteorder so it can be
3816 				 * properly reprocessed on the
3817 				 * eager's queue.
3818 				 */
3819 				BUMP_MIB(&sctps->sctps_mib, sctpPassiveEstab);
3820 				if (mlen > ntohs(ch->sch_len)) {
3821 					eager->sctp_cookie_mp = dupb(mp);
3822 					mblk_setcred(eager->sctp_cookie_mp,
3823 					    CONN_CRED(eager->sctp_connp));
3824 					/*
3825 					 * If no mem, just let
3826 					 * the peer retransmit.
3827 					 */
3828 				}
3829 				sctp_assoc_event(eager, SCTP_COMM_UP, 0, NULL);
3830 				if (recv_adaption) {
3831 					eager->sctp_recv_adaption = 1;
3832 					eager->sctp_rx_adaption_code =
3833 					    sctp->sctp_rx_adaption_code;
3834 					sctp_adaption_event(eager);
3835 				}
3836 
3837 				eager->sctp_active = now;
3838 				sctp_send_cookie_ack(eager);
3839 
3840 				wake_eager = B_TRUE;
3841 
3842 				/*
3843 				 * Process rest of the chunks with eager.
3844 				 */
3845 				sctp = eager;
3846 				fp = sctp_lookup_faddr(sctp, &peer_src);
3847 				/*
3848 				 * Confirm peer's original source.  fp can
3849 				 * only be NULL if peer does not use the
3850 				 * original source as one of its addresses...
3851 				 */
3852 				if (fp == NULL)
3853 					fp = sctp_lookup_faddr(sctp, &src);
3854 				else
3855 					sctp_faddr_alive(sctp, fp);
3856 
3857 				/*
3858 				 * Validate the peer addresses.  It also starts
3859 				 * the heartbeat timer.
3860 				 */
3861 				sctp_validate_peer(sctp);
3862 				break;
3863 			}
3864 			/* Anything else is considered out-of-the-blue */
3865 			case CHUNK_ERROR:
3866 			case CHUNK_ABORT:
3867 			case CHUNK_COOKIE_ACK:
3868 			case CHUNK_SHUTDOWN_COMPLETE:
3869 				BUMP_LOCAL(sctp->sctp_ibchunks);
3870 				goto done;
3871 			default:
3872 				BUMP_LOCAL(sctp->sctp_ibchunks);
3873 				sctp_send_abort(sctp, sctph->sh_verf, 0, NULL,
3874 				    0, mp, 0, B_TRUE);
3875 				goto done;
3876 			}
3877 			break;
3878 
3879 		case SCTPS_COOKIE_WAIT:
3880 			switch (ch->sch_id) {
3881 			case CHUNK_INIT_ACK:
3882 				sctp_stop_faddr_timers(sctp);
3883 				sctp_faddr_alive(sctp, sctp->sctp_current);
3884 				sctp_send_cookie_echo(sctp, ch, mp);
3885 				BUMP_LOCAL(sctp->sctp_ibchunks);
3886 				break;
3887 			case CHUNK_ABORT:
3888 				sctp_process_abort(sctp, ch, ECONNREFUSED);
3889 				goto done;
3890 			case CHUNK_INIT:
3891 				sctp_send_initack(sctp, sctph, ch, mp);
3892 				break;
3893 			case CHUNK_COOKIE:
3894 				if (sctp_process_cookie(sctp, ch, mp, &iack,
3895 				    sctph, &recv_adaption, NULL) == -1) {
3896 					BUMP_MIB(&sctps->sctps_mib,
3897 					    sctpInInvalidCookie);
3898 					break;
3899 				}
3900 				sctp_send_cookie_ack(sctp);
3901 				sctp_stop_faddr_timers(sctp);
3902 				if (!SCTP_IS_DETACHED(sctp)) {
3903 				    sctp->sctp_ulp_connected(sctp->sctp_ulpd);
3904 				    sctp_set_ulp_prop(sctp);
3905 				}
3906 				sctp->sctp_state = SCTPS_ESTABLISHED;
3907 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
3908 				BUMP_MIB(&sctps->sctps_mib, sctpActiveEstab);
3909 				if (sctp->sctp_cookie_mp) {
3910 					freemsg(sctp->sctp_cookie_mp);
3911 					sctp->sctp_cookie_mp = NULL;
3912 				}
3913 
3914 				/* Validate the peer addresses. */
3915 				sctp->sctp_active = now;
3916 				sctp_validate_peer(sctp);
3917 
3918 				sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL);
3919 				if (recv_adaption) {
3920 					sctp->sctp_recv_adaption = 1;
3921 					sctp_adaption_event(sctp);
3922 				}
3923 				/* Try sending queued data, or ASCONFs */
3924 				trysend = 1;
3925 				break;
3926 			default:
3927 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
3928 					goto nomorechunks;
3929 				} /* else skip and continue processing */
3930 				break;
3931 			}
3932 			break;
3933 
3934 		case SCTPS_COOKIE_ECHOED:
3935 			switch (ch->sch_id) {
3936 			case CHUNK_COOKIE_ACK:
3937 				if (!SCTP_IS_DETACHED(sctp)) {
3938 				    sctp->sctp_ulp_connected(sctp->sctp_ulpd);
3939 				    sctp_set_ulp_prop(sctp);
3940 				}
3941 				if (sctp->sctp_unacked == 0)
3942 					sctp_stop_faddr_timers(sctp);
3943 				sctp->sctp_state = SCTPS_ESTABLISHED;
3944 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
3945 				BUMP_MIB(&sctps->sctps_mib, sctpActiveEstab);
3946 				BUMP_LOCAL(sctp->sctp_ibchunks);
3947 				if (sctp->sctp_cookie_mp) {
3948 					freemsg(sctp->sctp_cookie_mp);
3949 					sctp->sctp_cookie_mp = NULL;
3950 				}
3951 				sctp_faddr_alive(sctp, fp);
3952 				/* Validate the peer addresses. */
3953 				sctp->sctp_active = now;
3954 				sctp_validate_peer(sctp);
3955 
3956 				/* Try sending queued data, or ASCONFs */
3957 				trysend = 1;
3958 				sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL);
3959 				sctp_adaption_event(sctp);
3960 				break;
3961 			case CHUNK_ABORT:
3962 				sctp_process_abort(sctp, ch, ECONNREFUSED);
3963 				goto done;
3964 			case CHUNK_COOKIE:
3965 				if (sctp_process_cookie(sctp, ch, mp, &iack,
3966 				    sctph, &recv_adaption, NULL) == -1) {
3967 					BUMP_MIB(&sctps->sctps_mib,
3968 					    sctpInInvalidCookie);
3969 					break;
3970 				}
3971 				sctp_send_cookie_ack(sctp);
3972 
3973 				if (!SCTP_IS_DETACHED(sctp)) {
3974 				    sctp->sctp_ulp_connected(sctp->sctp_ulpd);
3975 				    sctp_set_ulp_prop(sctp);
3976 				}
3977 				if (sctp->sctp_unacked == 0)
3978 					sctp_stop_faddr_timers(sctp);
3979 				sctp->sctp_state = SCTPS_ESTABLISHED;
3980 				sctp->sctp_assoc_start_time = (uint32_t)lbolt;
3981 				BUMP_MIB(&sctps->sctps_mib, sctpActiveEstab);
3982 				if (sctp->sctp_cookie_mp) {
3983 					freemsg(sctp->sctp_cookie_mp);
3984 					sctp->sctp_cookie_mp = NULL;
3985 				}
3986 				/* Validate the peer addresses. */
3987 				sctp->sctp_active = now;
3988 				sctp_validate_peer(sctp);
3989 
3990 				sctp_assoc_event(sctp, SCTP_COMM_UP, 0, NULL);
3991 				if (recv_adaption) {
3992 					sctp->sctp_recv_adaption = 1;
3993 					sctp_adaption_event(sctp);
3994 				}
3995 				/* Try sending queued data, or ASCONFs */
3996 				trysend = 1;
3997 				break;
3998 			case CHUNK_INIT:
3999 				sctp_send_initack(sctp, sctph, ch, mp);
4000 				break;
4001 			case CHUNK_ERROR: {
4002 				sctp_parm_hdr_t *p;
4003 
4004 				BUMP_LOCAL(sctp->sctp_ibchunks);
4005 				/* check for a stale cookie */
4006 				if (ntohs(ch->sch_len) >=
4007 				    (sizeof (*p) + sizeof (*ch)) +
4008 				    sizeof (uint32_t)) {
4009 
4010 					p = (sctp_parm_hdr_t *)(ch + 1);
4011 					if (p->sph_type ==
4012 					    htons(SCTP_ERR_STALE_COOKIE)) {
4013 						BUMP_MIB(&sctps->sctps_mib,
4014 						    sctpAborted);
4015 						sctp_error_event(sctp, ch);
4016 						sctp_assoc_event(sctp,
4017 						    SCTP_COMM_LOST, 0, NULL);
4018 						sctp_clean_death(sctp,
4019 						    ECONNREFUSED);
4020 						goto done;
4021 					}
4022 				}
4023 				break;
4024 			}
4025 			case CHUNK_HEARTBEAT:
4026 				sctp_return_heartbeat(sctp, ch, mp);
4027 				break;
4028 			default:
4029 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
4030 					goto nomorechunks;
4031 				} /* else skip and continue processing */
4032 			} /* switch (ch->sch_id) */
4033 			break;
4034 
4035 		case SCTPS_SHUTDOWN_ACK_SENT:
4036 			switch (ch->sch_id) {
4037 			case CHUNK_ABORT:
4038 				/* Pass gathered wisdom to IP for keeping */
4039 				sctp_update_ire(sctp);
4040 				sctp_process_abort(sctp, ch, 0);
4041 				goto done;
4042 			case CHUNK_SHUTDOWN_COMPLETE:
4043 				BUMP_LOCAL(sctp->sctp_ibchunks);
4044 				BUMP_MIB(&sctps->sctps_mib, sctpShutdowns);
4045 				sctp_assoc_event(sctp, SCTP_SHUTDOWN_COMP, 0,
4046 				    NULL);
4047 
4048 				/* Pass gathered wisdom to IP for keeping */
4049 				sctp_update_ire(sctp);
4050 				sctp_clean_death(sctp, 0);
4051 				goto done;
4052 			case CHUNK_SHUTDOWN_ACK:
4053 				sctp_shutdown_complete(sctp);
4054 				BUMP_LOCAL(sctp->sctp_ibchunks);
4055 				BUMP_MIB(&sctps->sctps_mib, sctpShutdowns);
4056 				sctp_assoc_event(sctp, SCTP_SHUTDOWN_COMP, 0,
4057 				    NULL);
4058 				sctp_clean_death(sctp, 0);
4059 				goto done;
4060 			case CHUNK_COOKIE:
4061 				(void) sctp_shutdown_received(sctp, NULL,
4062 				    B_TRUE, B_FALSE, fp);
4063 				BUMP_LOCAL(sctp->sctp_ibchunks);
4064 				break;
4065 			case CHUNK_HEARTBEAT:
4066 				sctp_return_heartbeat(sctp, ch, mp);
4067 				break;
4068 			default:
4069 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
4070 					goto nomorechunks;
4071 				} /* else skip and continue processing */
4072 				break;
4073 			}
4074 			break;
4075 
4076 		case SCTPS_SHUTDOWN_RECEIVED:
4077 			switch (ch->sch_id) {
4078 			case CHUNK_SHUTDOWN:
4079 				trysend = sctp_shutdown_received(sctp, ch,
4080 				    B_FALSE, B_FALSE, fp);
4081 				break;
4082 			case CHUNK_SACK:
4083 				trysend = sctp_got_sack(sctp, ch);
4084 				if (trysend < 0) {
4085 					sctp_send_abort(sctp, sctph->sh_verf,
4086 					    0, NULL, 0, mp, 0, B_FALSE);
4087 					sctp_assoc_event(sctp,
4088 					    SCTP_COMM_LOST, 0, NULL);
4089 					sctp_clean_death(sctp,
4090 					    ECONNABORTED);
4091 					goto done;
4092 				}
4093 				break;
4094 			case CHUNK_ABORT:
4095 				sctp_process_abort(sctp, ch, ECONNRESET);
4096 				goto done;
4097 			case CHUNK_HEARTBEAT:
4098 				sctp_return_heartbeat(sctp, ch, mp);
4099 				break;
4100 			default:
4101 				if (sctp_strange_chunk(sctp, ch, fp) == 0) {
4102 					goto nomorechunks;
4103 				} /* else skip and continue processing */
4104 				break;
4105 			}
4106 			break;
4107 
4108 		default:
4109 			/*
4110 			 * The only remaining states are SCTPS_IDLE and
4111 			 * SCTPS_BOUND, and we should not be getting here
4112 			 * for these.
4113 			 */
4114 			ASSERT(0);
4115 		} /* switch (sctp->sctp_state) */
4116 
4117 		ch = sctp_next_chunk(ch, &mlen);
4118 		if (ch != NULL && !sctp_check_input(sctp, ch, mlen, 0))
4119 			goto done;
4120 	} while (ch != NULL);
4121 
4122 	/* Finished processing all chunks in packet */
4123 
4124 nomorechunks:
4125 	/* SACK if necessary */
4126 	if (gotdata) {
4127 		(sctp->sctp_sack_toggle)++;
4128 		sctp_sack(sctp, dups);
4129 		dups = NULL;
4130 
4131 		if (!sctp->sctp_ack_timer_running) {
4132 			sctp->sctp_ack_timer_running = B_TRUE;
4133 			sctp_timer(sctp, sctp->sctp_ack_mp,
4134 			    MSEC_TO_TICK(sctps->sctps_deferred_ack_interval));
4135 		}
4136 	}
4137 
4138 	if (trysend) {
4139 		sctp_output(sctp, UINT_MAX);
4140 		if (sctp->sctp_cxmit_list != NULL)
4141 			sctp_wput_asconf(sctp, NULL);
4142 	}
4143 	/* If there is unsent data, make sure a timer is running */
4144 	if (sctp->sctp_unsent > 0 && !sctp->sctp_current->timer_running) {
4145 		SCTP_FADDR_TIMER_RESTART(sctp, sctp->sctp_current,
4146 		    sctp->sctp_current->rto);
4147 	}
4148 
4149 done:
4150 	if (dups != NULL)
4151 		freeb(dups);
4152 	if (ipsec_mp != NULL)
4153 		freeb(ipsec_mp);
4154 	freemsg(mp);
4155 
4156 	if (wake_eager) {
4157 		/*
4158 		 * sctp points to newly created control block, need to
4159 		 * release it before exiting.  Before releasing it and
4160 		 * processing the sendq, need to grab a hold on it.
4161 		 * Otherwise, another thread can close it while processing
4162 		 * the sendq.
4163 		 */
4164 		SCTP_REFHOLD(sctp);
4165 		WAKE_SCTP(sctp);
4166 		sctp_process_sendq(sctp);
4167 		SCTP_REFRELE(sctp);
4168 	}
4169 }
4170 
4171 /*
4172  * Some amount of data got removed from rx q.
4173  * Check if we should send a window update.
4174  *
4175  * Due to way sctp_rwnd updates are made, ULP can give reports out-of-order.
4176  * To keep from dropping incoming data due to this, we only update
4177  * sctp_rwnd when if it's larger than what we've reported to peer earlier.
4178  */
4179 void
4180 sctp_recvd(sctp_t *sctp, int len)
4181 {
4182 	int32_t old, new;
4183 	sctp_stack_t	*sctps = sctp->sctp_sctps;
4184 
4185 	ASSERT(sctp != NULL);
4186 	RUN_SCTP(sctp);
4187 
4188 	if (len < sctp->sctp_rwnd) {
4189 		WAKE_SCTP(sctp);
4190 		return;
4191 	}
4192 	ASSERT(sctp->sctp_rwnd >= sctp->sctp_rxqueued);
4193 	old = sctp->sctp_rwnd - sctp->sctp_rxqueued;
4194 	new = len - sctp->sctp_rxqueued;
4195 	sctp->sctp_rwnd = len;
4196 
4197 	if (sctp->sctp_state >= SCTPS_ESTABLISHED &&
4198 	    ((old <= new >> 1) || (old < sctp->sctp_mss))) {
4199 		sctp->sctp_force_sack = 1;
4200 		BUMP_MIB(&sctps->sctps_mib, sctpOutWinUpdate);
4201 		sctp_sack(sctp, NULL);
4202 		old = 1;
4203 	} else {
4204 		old = 0;
4205 	}
4206 	WAKE_SCTP(sctp);
4207 	if (old > 0) {
4208 		sctp_process_sendq(sctp);
4209 	}
4210 }
4211