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