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