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