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