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