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