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