xref: /freebsd/sys/netinet/sctp_indata.c (revision 94942af266ac119ede0ca836f9aa5a5ac0582938)
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /* $KAME: sctp_indata.c,v 1.36 2005/03/06 16:04:17 itojun Exp $	 */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp_var.h>
38 #include <netinet/sctp_sysctl.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp_output.h>
43 #include <netinet/sctp_input.h>
44 #include <netinet/sctp_indata.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctp_timer.h>
47 
48 
49 /*
50  * NOTES: On the outbound side of things I need to check the sack timer to
51  * see if I should generate a sack into the chunk queue (if I have data to
52  * send that is and will be sending it .. for bundling.
53  *
54  * The callback in sctp_usrreq.c will get called when the socket is read from.
55  * This will cause sctp_service_queues() to get called on the top entry in
56  * the list.
57  */
58 
59 __inline void
60 sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
61 {
62 	uint32_t calc, calc_w_oh;
63 
64 	/*
65 	 * This is really set wrong with respect to a 1-2-m socket. Since
66 	 * the sb_cc is the count that everyone as put up. When we re-write
67 	 * sctp_soreceive then we will fix this so that ONLY this
68 	 * associations data is taken into account.
69 	 */
70 	if (stcb->sctp_socket == NULL)
71 		return;
72 
73 	if (stcb->asoc.sb_cc == 0 &&
74 	    asoc->size_on_reasm_queue == 0 &&
75 	    asoc->size_on_all_streams == 0) {
76 		/* Full rwnd granted */
77 		asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
78 		    SCTP_MINIMAL_RWND);
79 		return;
80 	}
81 	/* get actual space */
82 	calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
83 
84 	/*
85 	 * take out what has NOT been put on socket queue and we yet hold
86 	 * for putting up.
87 	 */
88 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
89 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
90 
91 	if (calc == 0) {
92 		/* out of space */
93 		asoc->my_rwnd = 0;
94 		return;
95 	}
96 	/* what is the overhead of all these rwnd's */
97 	calc_w_oh = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
98 	asoc->my_rwnd = calc;
99 	if (calc_w_oh == 0) {
100 		/*
101 		 * If our overhead is greater than the advertised rwnd, we
102 		 * clamp the rwnd to 1. This lets us still accept inbound
103 		 * segments, but hopefully will shut the sender down when he
104 		 * finally gets the message.
105 		 */
106 		asoc->my_rwnd = 1;
107 	} else {
108 		/* SWS threshold */
109 		if (asoc->my_rwnd &&
110 		    (asoc->my_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_receiver)) {
111 			/* SWS engaged, tell peer none left */
112 			asoc->my_rwnd = 1;
113 		}
114 	}
115 }
116 
117 /* Calculate what the rwnd would be */
118 
119 __inline uint32_t
120 sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
121 {
122 	uint32_t calc = 0, calc_w_oh;
123 
124 	/*
125 	 * This is really set wrong with respect to a 1-2-m socket. Since
126 	 * the sb_cc is the count that everyone as put up. When we re-write
127 	 * sctp_soreceive then we will fix this so that ONLY this
128 	 * associations data is taken into account.
129 	 */
130 	if (stcb->sctp_socket == NULL)
131 		return (calc);
132 
133 	if (stcb->asoc.sb_cc == 0 &&
134 	    asoc->size_on_reasm_queue == 0 &&
135 	    asoc->size_on_all_streams == 0) {
136 		/* Full rwnd granted */
137 		calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket),
138 		    SCTP_MINIMAL_RWND);
139 		return (calc);
140 	}
141 	/* get actual space */
142 	calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
143 
144 	/*
145 	 * take out what has NOT been put on socket queue and we yet hold
146 	 * for putting up.
147 	 */
148 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
149 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
150 
151 	if (calc == 0) {
152 		/* out of space */
153 		return (calc);
154 	}
155 	/* what is the overhead of all these rwnd's */
156 	calc_w_oh = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
157 	if (calc_w_oh == 0) {
158 		/*
159 		 * If our overhead is greater than the advertised rwnd, we
160 		 * clamp the rwnd to 1. This lets us still accept inbound
161 		 * segments, but hopefully will shut the sender down when he
162 		 * finally gets the message.
163 		 */
164 		calc = 1;
165 	} else {
166 		/* SWS threshold */
167 		if (calc &&
168 		    (calc < stcb->sctp_ep->sctp_ep.sctp_sws_receiver)) {
169 			/* SWS engaged, tell peer none left */
170 			calc = 1;
171 		}
172 	}
173 	return (calc);
174 }
175 
176 
177 
178 /*
179  * Build out our readq entry based on the incoming packet.
180  */
181 struct sctp_queued_to_read *
182 sctp_build_readq_entry(struct sctp_tcb *stcb,
183     struct sctp_nets *net,
184     uint32_t tsn, uint32_t ppid,
185     uint32_t context, uint16_t stream_no,
186     uint16_t stream_seq, uint8_t flags,
187     struct mbuf *dm)
188 {
189 	struct sctp_queued_to_read *read_queue_e = NULL;
190 
191 	sctp_alloc_a_readq(stcb, read_queue_e);
192 	if (read_queue_e == NULL) {
193 		goto failed_build;
194 	}
195 	read_queue_e->sinfo_stream = stream_no;
196 	read_queue_e->sinfo_ssn = stream_seq;
197 	read_queue_e->sinfo_flags = (flags << 8);
198 	read_queue_e->sinfo_ppid = ppid;
199 	read_queue_e->sinfo_context = stcb->asoc.context;
200 	read_queue_e->sinfo_timetolive = 0;
201 	read_queue_e->sinfo_tsn = tsn;
202 	read_queue_e->sinfo_cumtsn = tsn;
203 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
204 	read_queue_e->whoFrom = net;
205 	read_queue_e->length = 0;
206 	atomic_add_int(&net->ref_count, 1);
207 	read_queue_e->data = dm;
208 	read_queue_e->spec_flags = 0;
209 	read_queue_e->tail_mbuf = NULL;
210 	read_queue_e->aux_data = NULL;
211 	read_queue_e->stcb = stcb;
212 	read_queue_e->port_from = stcb->rport;
213 	read_queue_e->do_not_ref_stcb = 0;
214 	read_queue_e->end_added = 0;
215 	read_queue_e->some_taken = 0;
216 	read_queue_e->pdapi_aborted = 0;
217 failed_build:
218 	return (read_queue_e);
219 }
220 
221 
222 /*
223  * Build out our readq entry based on the incoming packet.
224  */
225 static struct sctp_queued_to_read *
226 sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
227     struct sctp_tmit_chunk *chk)
228 {
229 	struct sctp_queued_to_read *read_queue_e = NULL;
230 
231 	sctp_alloc_a_readq(stcb, read_queue_e);
232 	if (read_queue_e == NULL) {
233 		goto failed_build;
234 	}
235 	read_queue_e->sinfo_stream = chk->rec.data.stream_number;
236 	read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
237 	read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
238 	read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
239 	read_queue_e->sinfo_context = stcb->asoc.context;
240 	read_queue_e->sinfo_timetolive = 0;
241 	read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
242 	read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
243 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
244 	read_queue_e->whoFrom = chk->whoTo;
245 	read_queue_e->aux_data = NULL;
246 	read_queue_e->length = 0;
247 	atomic_add_int(&chk->whoTo->ref_count, 1);
248 	read_queue_e->data = chk->data;
249 	read_queue_e->tail_mbuf = NULL;
250 	read_queue_e->stcb = stcb;
251 	read_queue_e->port_from = stcb->rport;
252 	read_queue_e->spec_flags = 0;
253 	read_queue_e->do_not_ref_stcb = 0;
254 	read_queue_e->end_added = 0;
255 	read_queue_e->some_taken = 0;
256 	read_queue_e->pdapi_aborted = 0;
257 failed_build:
258 	return (read_queue_e);
259 }
260 
261 
262 struct mbuf *
263 sctp_build_ctl_nchunk(struct sctp_inpcb *inp,
264     struct sctp_sndrcvinfo *sinfo)
265 {
266 	struct sctp_sndrcvinfo *outinfo;
267 	struct cmsghdr *cmh;
268 	struct mbuf *ret;
269 	int len;
270 	int use_extended = 0;
271 
272 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
273 		/* user does not want the sndrcv ctl */
274 		return (NULL);
275 	}
276 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
277 		use_extended = 1;
278 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
279 	} else {
280 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
281 	}
282 
283 
284 	ret = sctp_get_mbuf_for_msg(len,
285 	    0, M_DONTWAIT, 1, MT_DATA);
286 
287 	if (ret == NULL) {
288 		/* No space */
289 		return (ret);
290 	}
291 	/* We need a CMSG header followed by the struct  */
292 	cmh = mtod(ret, struct cmsghdr *);
293 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
294 	cmh->cmsg_level = IPPROTO_SCTP;
295 	if (use_extended) {
296 		cmh->cmsg_type = SCTP_EXTRCV;
297 		cmh->cmsg_len = len;
298 		memcpy(outinfo, sinfo, len);
299 	} else {
300 		cmh->cmsg_type = SCTP_SNDRCV;
301 		cmh->cmsg_len = len;
302 		*outinfo = *sinfo;
303 	}
304 	SCTP_BUF_LEN(ret) = cmh->cmsg_len;
305 	return (ret);
306 }
307 
308 
309 char *
310 sctp_build_ctl_cchunk(struct sctp_inpcb *inp,
311     int *control_len,
312     struct sctp_sndrcvinfo *sinfo)
313 {
314 	struct sctp_sndrcvinfo *outinfo;
315 	struct cmsghdr *cmh;
316 	char *buf;
317 	int len;
318 	int use_extended = 0;
319 
320 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
321 		/* user does not want the sndrcv ctl */
322 		return (NULL);
323 	}
324 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
325 		use_extended = 1;
326 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
327 	} else {
328 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
329 	}
330 	SCTP_MALLOC(buf, char *, len, "SCTP_CMSG");
331 	if (buf == NULL) {
332 		/* No space */
333 		return (buf);
334 	}
335 	/* We need a CMSG header followed by the struct  */
336 	cmh = (struct cmsghdr *)buf;
337 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
338 	cmh->cmsg_level = IPPROTO_SCTP;
339 	if (use_extended) {
340 		cmh->cmsg_type = SCTP_EXTRCV;
341 		cmh->cmsg_len = len;
342 		memcpy(outinfo, sinfo, len);
343 	} else {
344 		cmh->cmsg_type = SCTP_SNDRCV;
345 		cmh->cmsg_len = len;
346 		*outinfo = *sinfo;
347 	}
348 	*control_len = len;
349 	return (buf);
350 }
351 
352 
353 /*
354  * We are delivering currently from the reassembly queue. We must continue to
355  * deliver until we either: 1) run out of space. 2) run out of sequential
356  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
357  */
358 static void
359 sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
360 {
361 	struct sctp_tmit_chunk *chk;
362 	uint16_t nxt_todel;
363 	uint16_t stream_no;
364 	int end = 0;
365 	int cntDel;
366 	struct sctp_queued_to_read *control, *ctl, *ctlat;
367 
368 	if (stcb == NULL)
369 		return;
370 
371 	cntDel = stream_no = 0;
372 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
373 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
374 		/* socket above is long gone */
375 		asoc->fragmented_delivery_inprogress = 0;
376 		chk = TAILQ_FIRST(&asoc->reasmqueue);
377 		while (chk) {
378 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
379 			asoc->size_on_reasm_queue -= chk->send_size;
380 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
381 			/*
382 			 * Lose the data pointer, since its in the socket
383 			 * buffer
384 			 */
385 			if (chk->data) {
386 				sctp_m_freem(chk->data);
387 				chk->data = NULL;
388 			}
389 			/* Now free the address and data */
390 			sctp_free_remote_addr(chk->whoTo);
391 			sctp_free_a_chunk(stcb, chk);
392 			/* sa_ignore FREED_MEMORY */
393 			chk = TAILQ_FIRST(&asoc->reasmqueue);
394 		}
395 		return;
396 	}
397 	SCTP_TCB_LOCK_ASSERT(stcb);
398 	do {
399 		chk = TAILQ_FIRST(&asoc->reasmqueue);
400 		if (chk == NULL) {
401 			return;
402 		}
403 		if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
404 			/* Can't deliver more :< */
405 			return;
406 		}
407 		stream_no = chk->rec.data.stream_number;
408 		nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
409 		if (nxt_todel != chk->rec.data.stream_seq &&
410 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
411 			/*
412 			 * Not the next sequence to deliver in its stream OR
413 			 * unordered
414 			 */
415 			return;
416 		}
417 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
418 
419 			control = sctp_build_readq_entry_chk(stcb, chk);
420 			if (control == NULL) {
421 				/* out of memory? */
422 				return;
423 			}
424 			/* save it off for our future deliveries */
425 			stcb->asoc.control_pdapi = control;
426 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
427 				end = 1;
428 			else
429 				end = 0;
430 			sctp_add_to_readq(stcb->sctp_ep,
431 			    stcb, control, &stcb->sctp_socket->so_rcv, end);
432 			cntDel++;
433 		} else {
434 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
435 				end = 1;
436 			else
437 				end = 0;
438 			if (sctp_append_to_readq(stcb->sctp_ep, stcb,
439 			    stcb->asoc.control_pdapi,
440 			    chk->data, end, chk->rec.data.TSN_seq,
441 			    &stcb->sctp_socket->so_rcv)) {
442 				/*
443 				 * something is very wrong, either
444 				 * control_pdapi is NULL, or the tail_mbuf
445 				 * is corrupt, or there is a EOM already on
446 				 * the mbuf chain.
447 				 */
448 				if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
449 					panic("This should not happen control_pdapi NULL?");
450 				}
451 				/* if we did not panic, it was a EOM */
452 				panic("Bad chunking ??");
453 				return;
454 			}
455 			cntDel++;
456 		}
457 		/* pull it we did it */
458 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
459 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
460 			asoc->fragmented_delivery_inprogress = 0;
461 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
462 				asoc->strmin[stream_no].last_sequence_delivered++;
463 			}
464 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
465 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
466 			}
467 		} else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
468 			/*
469 			 * turn the flag back on since we just  delivered
470 			 * yet another one.
471 			 */
472 			asoc->fragmented_delivery_inprogress = 1;
473 		}
474 		asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
475 		asoc->last_flags_delivered = chk->rec.data.rcv_flags;
476 		asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
477 		asoc->last_strm_no_delivered = chk->rec.data.stream_number;
478 
479 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
480 		asoc->size_on_reasm_queue -= chk->send_size;
481 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
482 		/* free up the chk */
483 		chk->data = NULL;
484 		sctp_free_remote_addr(chk->whoTo);
485 		sctp_free_a_chunk(stcb, chk);
486 
487 		if (asoc->fragmented_delivery_inprogress == 0) {
488 			/*
489 			 * Now lets see if we can deliver the next one on
490 			 * the stream
491 			 */
492 			uint16_t nxt_todel;
493 			struct sctp_stream_in *strm;
494 
495 			strm = &asoc->strmin[stream_no];
496 			nxt_todel = strm->last_sequence_delivered + 1;
497 			ctl = TAILQ_FIRST(&strm->inqueue);
498 			if (ctl && (nxt_todel == ctl->sinfo_ssn)) {
499 				while (ctl != NULL) {
500 					/* Deliver more if we can. */
501 					if (nxt_todel == ctl->sinfo_ssn) {
502 						ctlat = TAILQ_NEXT(ctl, next);
503 						TAILQ_REMOVE(&strm->inqueue, ctl, next);
504 						asoc->size_on_all_streams -= ctl->length;
505 						sctp_ucount_decr(asoc->cnt_on_all_streams);
506 						strm->last_sequence_delivered++;
507 						sctp_add_to_readq(stcb->sctp_ep, stcb,
508 						    ctl,
509 						    &stcb->sctp_socket->so_rcv, 1);
510 						ctl = ctlat;
511 					} else {
512 						break;
513 					}
514 					nxt_todel = strm->last_sequence_delivered + 1;
515 				}
516 			}
517 			break;
518 		}
519 		/* sa_ignore FREED_MEMORY */
520 		chk = TAILQ_FIRST(&asoc->reasmqueue);
521 	} while (chk);
522 }
523 
524 /*
525  * Queue the chunk either right into the socket buffer if it is the next one
526  * to go OR put it in the correct place in the delivery queue.  If we do
527  * append to the so_buf, keep doing so until we are out of order. One big
528  * question still remains, what to do when the socket buffer is FULL??
529  */
530 static void
531 sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
532     struct sctp_queued_to_read *control, int *abort_flag)
533 {
534 	/*
535 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
536 	 * all the data in one stream this could happen quite rapidly. One
537 	 * could use the TSN to keep track of things, but this scheme breaks
538 	 * down in the other type of stream useage that could occur. Send a
539 	 * single msg to stream 0, send 4Billion messages to stream 1, now
540 	 * send a message to stream 0. You have a situation where the TSN
541 	 * has wrapped but not in the stream. Is this worth worrying about
542 	 * or should we just change our queue sort at the bottom to be by
543 	 * TSN.
544 	 *
545 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
546 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
547 	 * assignment this could happen... and I don't see how this would be
548 	 * a violation. So for now I am undecided an will leave the sort by
549 	 * SSN alone. Maybe a hybred approach is the answer
550 	 *
551 	 */
552 	struct sctp_stream_in *strm;
553 	struct sctp_queued_to_read *at;
554 	int queue_needed;
555 	uint16_t nxt_todel;
556 	struct mbuf *oper;
557 
558 	queue_needed = 1;
559 	asoc->size_on_all_streams += control->length;
560 	sctp_ucount_incr(asoc->cnt_on_all_streams);
561 	strm = &asoc->strmin[control->sinfo_stream];
562 	nxt_todel = strm->last_sequence_delivered + 1;
563 #ifdef SCTP_STR_LOGGING
564 	sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
565 #endif
566 	SCTPDBG(SCTP_DEBUG_INDATA1,
567 	    "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
568 	    (uint32_t) control->sinfo_stream,
569 	    (uint32_t) strm->last_sequence_delivered,
570 	    (uint32_t) nxt_todel);
571 	if (compare_with_wrap(strm->last_sequence_delivered,
572 	    control->sinfo_ssn, MAX_SEQ) ||
573 	    (strm->last_sequence_delivered == control->sinfo_ssn)) {
574 		/* The incoming sseq is behind where we last delivered? */
575 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
576 		    control->sinfo_ssn, strm->last_sequence_delivered);
577 		/*
578 		 * throw it in the stream so it gets cleaned up in
579 		 * association destruction
580 		 */
581 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
582 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
583 		    0, M_DONTWAIT, 1, MT_DATA);
584 		if (oper) {
585 			struct sctp_paramhdr *ph;
586 			uint32_t *ippp;
587 
588 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
589 			    (sizeof(uint32_t) * 3);
590 			ph = mtod(oper, struct sctp_paramhdr *);
591 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
592 			ph->param_length = htons(SCTP_BUF_LEN(oper));
593 			ippp = (uint32_t *) (ph + 1);
594 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
595 			ippp++;
596 			*ippp = control->sinfo_tsn;
597 			ippp++;
598 			*ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
599 		}
600 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
601 		sctp_abort_an_association(stcb->sctp_ep, stcb,
602 		    SCTP_PEER_FAULTY, oper);
603 
604 		*abort_flag = 1;
605 		return;
606 
607 	}
608 	if (nxt_todel == control->sinfo_ssn) {
609 		/* can be delivered right away? */
610 #ifdef SCTP_STR_LOGGING
611 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
612 #endif
613 		queue_needed = 0;
614 		asoc->size_on_all_streams -= control->length;
615 		sctp_ucount_decr(asoc->cnt_on_all_streams);
616 		strm->last_sequence_delivered++;
617 		sctp_add_to_readq(stcb->sctp_ep, stcb,
618 		    control,
619 		    &stcb->sctp_socket->so_rcv, 1);
620 		control = TAILQ_FIRST(&strm->inqueue);
621 		while (control != NULL) {
622 			/* all delivered */
623 			nxt_todel = strm->last_sequence_delivered + 1;
624 			if (nxt_todel == control->sinfo_ssn) {
625 				at = TAILQ_NEXT(control, next);
626 				TAILQ_REMOVE(&strm->inqueue, control, next);
627 				asoc->size_on_all_streams -= control->length;
628 				sctp_ucount_decr(asoc->cnt_on_all_streams);
629 				strm->last_sequence_delivered++;
630 				/*
631 				 * We ignore the return of deliver_data here
632 				 * since we always can hold the chunk on the
633 				 * d-queue. And we have a finite number that
634 				 * can be delivered from the strq.
635 				 */
636 #ifdef SCTP_STR_LOGGING
637 				sctp_log_strm_del(control, NULL,
638 				    SCTP_STR_LOG_FROM_IMMED_DEL);
639 #endif
640 				sctp_add_to_readq(stcb->sctp_ep, stcb,
641 				    control,
642 				    &stcb->sctp_socket->so_rcv, 1);
643 				control = at;
644 				continue;
645 			}
646 			break;
647 		}
648 	}
649 	if (queue_needed) {
650 		/*
651 		 * Ok, we did not deliver this guy, find the correct place
652 		 * to put it on the queue.
653 		 */
654 		if (TAILQ_EMPTY(&strm->inqueue)) {
655 			/* Empty queue */
656 #ifdef SCTP_STR_LOGGING
657 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
658 #endif
659 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
660 		} else {
661 			TAILQ_FOREACH(at, &strm->inqueue, next) {
662 				if (compare_with_wrap(at->sinfo_ssn,
663 				    control->sinfo_ssn, MAX_SEQ)) {
664 					/*
665 					 * one in queue is bigger than the
666 					 * new one, insert before this one
667 					 */
668 #ifdef SCTP_STR_LOGGING
669 					sctp_log_strm_del(control, at,
670 					    SCTP_STR_LOG_FROM_INSERT_MD);
671 #endif
672 					TAILQ_INSERT_BEFORE(at, control, next);
673 					break;
674 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
675 					/*
676 					 * Gak, He sent me a duplicate str
677 					 * seq number
678 					 */
679 					/*
680 					 * foo bar, I guess I will just free
681 					 * this new guy, should we abort
682 					 * too? FIX ME MAYBE? Or it COULD be
683 					 * that the SSN's have wrapped.
684 					 * Maybe I should compare to TSN
685 					 * somehow... sigh for now just blow
686 					 * away the chunk!
687 					 */
688 
689 					if (control->data)
690 						sctp_m_freem(control->data);
691 					control->data = NULL;
692 					asoc->size_on_all_streams -= control->length;
693 					sctp_ucount_decr(asoc->cnt_on_all_streams);
694 					sctp_free_remote_addr(control->whoFrom);
695 					sctp_free_a_readq(stcb, control);
696 					return;
697 				} else {
698 					if (TAILQ_NEXT(at, next) == NULL) {
699 						/*
700 						 * We are at the end, insert
701 						 * it after this one
702 						 */
703 #ifdef SCTP_STR_LOGGING
704 						sctp_log_strm_del(control, at,
705 						    SCTP_STR_LOG_FROM_INSERT_TL);
706 #endif
707 						TAILQ_INSERT_AFTER(&strm->inqueue,
708 						    at, control, next);
709 						break;
710 					}
711 				}
712 			}
713 		}
714 	}
715 }
716 
717 /*
718  * Returns two things: You get the total size of the deliverable parts of the
719  * first fragmented message on the reassembly queue. And you get a 1 back if
720  * all of the message is ready or a 0 back if the message is still incomplete
721  */
722 static int
723 sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
724 {
725 	struct sctp_tmit_chunk *chk;
726 	uint32_t tsn;
727 
728 	*t_size = 0;
729 	chk = TAILQ_FIRST(&asoc->reasmqueue);
730 	if (chk == NULL) {
731 		/* nothing on the queue */
732 		return (0);
733 	}
734 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
735 		/* Not a first on the queue */
736 		return (0);
737 	}
738 	tsn = chk->rec.data.TSN_seq;
739 	while (chk) {
740 		if (tsn != chk->rec.data.TSN_seq) {
741 			return (0);
742 		}
743 		*t_size += chk->send_size;
744 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
745 			return (1);
746 		}
747 		tsn++;
748 		chk = TAILQ_NEXT(chk, sctp_next);
749 	}
750 	return (0);
751 }
752 
753 static void
754 sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
755 {
756 	struct sctp_tmit_chunk *chk;
757 	uint16_t nxt_todel;
758 	uint32_t tsize;
759 
760 doit_again:
761 	chk = TAILQ_FIRST(&asoc->reasmqueue);
762 	if (chk == NULL) {
763 		/* Huh? */
764 		asoc->size_on_reasm_queue = 0;
765 		asoc->cnt_on_reasm_queue = 0;
766 		return;
767 	}
768 	if (asoc->fragmented_delivery_inprogress == 0) {
769 		nxt_todel =
770 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
771 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
772 		    (nxt_todel == chk->rec.data.stream_seq ||
773 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
774 			/*
775 			 * Yep the first one is here and its ok to deliver
776 			 * but should we?
777 			 */
778 			if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
779 			    (tsize > stcb->sctp_ep->partial_delivery_point))) {
780 
781 				/*
782 				 * Yes, we setup to start reception, by
783 				 * backing down the TSN just in case we
784 				 * can't deliver. If we
785 				 */
786 				asoc->fragmented_delivery_inprogress = 1;
787 				asoc->tsn_last_delivered =
788 				    chk->rec.data.TSN_seq - 1;
789 				asoc->str_of_pdapi =
790 				    chk->rec.data.stream_number;
791 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
792 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
793 				asoc->fragment_flags = chk->rec.data.rcv_flags;
794 				sctp_service_reassembly(stcb, asoc);
795 			}
796 		}
797 	} else {
798 		/*
799 		 * Service re-assembly will deliver stream data queued at
800 		 * the end of fragmented delivery.. but it wont know to go
801 		 * back and call itself again... we do that here with the
802 		 * got doit_again
803 		 */
804 		sctp_service_reassembly(stcb, asoc);
805 		if (asoc->fragmented_delivery_inprogress == 0) {
806 			/*
807 			 * finished our Fragmented delivery, could be more
808 			 * waiting?
809 			 */
810 			goto doit_again;
811 		}
812 	}
813 }
814 
815 /*
816  * Dump onto the re-assembly queue, in its proper place. After dumping on the
817  * queue, see if anthing can be delivered. If so pull it off (or as much as
818  * we can. If we run out of space then we must dump what we can and set the
819  * appropriate flag to say we queued what we could.
820  */
821 static void
822 sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
823     struct sctp_tmit_chunk *chk, int *abort_flag)
824 {
825 	struct mbuf *oper;
826 	uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
827 	u_char last_flags;
828 	struct sctp_tmit_chunk *at, *prev, *next;
829 
830 	prev = next = NULL;
831 	cum_ackp1 = asoc->tsn_last_delivered + 1;
832 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
833 		/* This is the first one on the queue */
834 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
835 		/*
836 		 * we do not check for delivery of anything when only one
837 		 * fragment is here
838 		 */
839 		asoc->size_on_reasm_queue = chk->send_size;
840 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
841 		if (chk->rec.data.TSN_seq == cum_ackp1) {
842 			if (asoc->fragmented_delivery_inprogress == 0 &&
843 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
844 			    SCTP_DATA_FIRST_FRAG) {
845 				/*
846 				 * An empty queue, no delivery inprogress,
847 				 * we hit the next one and it does NOT have
848 				 * a FIRST fragment mark.
849 				 */
850 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
851 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
852 				    0, M_DONTWAIT, 1, MT_DATA);
853 
854 				if (oper) {
855 					struct sctp_paramhdr *ph;
856 					uint32_t *ippp;
857 
858 					SCTP_BUF_LEN(oper) =
859 					    sizeof(struct sctp_paramhdr) +
860 					    (sizeof(uint32_t) * 3);
861 					ph = mtod(oper, struct sctp_paramhdr *);
862 					ph->param_type =
863 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
864 					ph->param_length = htons(SCTP_BUF_LEN(oper));
865 					ippp = (uint32_t *) (ph + 1);
866 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
867 					ippp++;
868 					*ippp = chk->rec.data.TSN_seq;
869 					ippp++;
870 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
871 
872 				}
873 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
874 				sctp_abort_an_association(stcb->sctp_ep, stcb,
875 				    SCTP_PEER_FAULTY, oper);
876 				*abort_flag = 1;
877 			} else if (asoc->fragmented_delivery_inprogress &&
878 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
879 				/*
880 				 * We are doing a partial delivery and the
881 				 * NEXT chunk MUST be either the LAST or
882 				 * MIDDLE fragment NOT a FIRST
883 				 */
884 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
885 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
886 				    0, M_DONTWAIT, 1, MT_DATA);
887 				if (oper) {
888 					struct sctp_paramhdr *ph;
889 					uint32_t *ippp;
890 
891 					SCTP_BUF_LEN(oper) =
892 					    sizeof(struct sctp_paramhdr) +
893 					    (3 * sizeof(uint32_t));
894 					ph = mtod(oper, struct sctp_paramhdr *);
895 					ph->param_type =
896 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
897 					ph->param_length = htons(SCTP_BUF_LEN(oper));
898 					ippp = (uint32_t *) (ph + 1);
899 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
900 					ippp++;
901 					*ippp = chk->rec.data.TSN_seq;
902 					ippp++;
903 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
904 				}
905 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
906 				sctp_abort_an_association(stcb->sctp_ep, stcb,
907 				    SCTP_PEER_FAULTY, oper);
908 				*abort_flag = 1;
909 			} else if (asoc->fragmented_delivery_inprogress) {
910 				/*
911 				 * Here we are ok with a MIDDLE or LAST
912 				 * piece
913 				 */
914 				if (chk->rec.data.stream_number !=
915 				    asoc->str_of_pdapi) {
916 					/* Got to be the right STR No */
917 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
918 					    chk->rec.data.stream_number,
919 					    asoc->str_of_pdapi);
920 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
921 					    0, M_DONTWAIT, 1, MT_DATA);
922 					if (oper) {
923 						struct sctp_paramhdr *ph;
924 						uint32_t *ippp;
925 
926 						SCTP_BUF_LEN(oper) =
927 						    sizeof(struct sctp_paramhdr) +
928 						    (sizeof(uint32_t) * 3);
929 						ph = mtod(oper,
930 						    struct sctp_paramhdr *);
931 						ph->param_type =
932 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
933 						ph->param_length =
934 						    htons(SCTP_BUF_LEN(oper));
935 						ippp = (uint32_t *) (ph + 1);
936 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
937 						ippp++;
938 						*ippp = chk->rec.data.TSN_seq;
939 						ippp++;
940 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
941 					}
942 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
943 					sctp_abort_an_association(stcb->sctp_ep,
944 					    stcb, SCTP_PEER_FAULTY, oper);
945 					*abort_flag = 1;
946 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
947 					    SCTP_DATA_UNORDERED &&
948 					    chk->rec.data.stream_seq !=
949 				    asoc->ssn_of_pdapi) {
950 					/* Got to be the right STR Seq */
951 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
952 					    chk->rec.data.stream_seq,
953 					    asoc->ssn_of_pdapi);
954 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
955 					    0, M_DONTWAIT, 1, MT_DATA);
956 					if (oper) {
957 						struct sctp_paramhdr *ph;
958 						uint32_t *ippp;
959 
960 						SCTP_BUF_LEN(oper) =
961 						    sizeof(struct sctp_paramhdr) +
962 						    (3 * sizeof(uint32_t));
963 						ph = mtod(oper,
964 						    struct sctp_paramhdr *);
965 						ph->param_type =
966 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
967 						ph->param_length =
968 						    htons(SCTP_BUF_LEN(oper));
969 						ippp = (uint32_t *) (ph + 1);
970 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
971 						ippp++;
972 						*ippp = chk->rec.data.TSN_seq;
973 						ippp++;
974 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
975 
976 					}
977 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
978 					sctp_abort_an_association(stcb->sctp_ep,
979 					    stcb, SCTP_PEER_FAULTY, oper);
980 					*abort_flag = 1;
981 				}
982 			}
983 		}
984 		return;
985 	}
986 	/* Find its place */
987 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
988 		if (compare_with_wrap(at->rec.data.TSN_seq,
989 		    chk->rec.data.TSN_seq, MAX_TSN)) {
990 			/*
991 			 * one in queue is bigger than the new one, insert
992 			 * before this one
993 			 */
994 			/* A check */
995 			asoc->size_on_reasm_queue += chk->send_size;
996 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
997 			next = at;
998 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
999 			break;
1000 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1001 			/* Gak, He sent me a duplicate str seq number */
1002 			/*
1003 			 * foo bar, I guess I will just free this new guy,
1004 			 * should we abort too? FIX ME MAYBE? Or it COULD be
1005 			 * that the SSN's have wrapped. Maybe I should
1006 			 * compare to TSN somehow... sigh for now just blow
1007 			 * away the chunk!
1008 			 */
1009 			if (chk->data) {
1010 				sctp_m_freem(chk->data);
1011 				chk->data = NULL;
1012 			}
1013 			sctp_free_remote_addr(chk->whoTo);
1014 			sctp_free_a_chunk(stcb, chk);
1015 			return;
1016 		} else {
1017 			last_flags = at->rec.data.rcv_flags;
1018 			last_tsn = at->rec.data.TSN_seq;
1019 			prev = at;
1020 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
1021 				/*
1022 				 * We are at the end, insert it after this
1023 				 * one
1024 				 */
1025 				/* check it first */
1026 				asoc->size_on_reasm_queue += chk->send_size;
1027 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1028 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1029 				break;
1030 			}
1031 		}
1032 	}
1033 	/* Now the audits */
1034 	if (prev) {
1035 		prev_tsn = chk->rec.data.TSN_seq - 1;
1036 		if (prev_tsn == prev->rec.data.TSN_seq) {
1037 			/*
1038 			 * Ok the one I am dropping onto the end is the
1039 			 * NEXT. A bit of valdiation here.
1040 			 */
1041 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1042 			    SCTP_DATA_FIRST_FRAG ||
1043 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1044 			    SCTP_DATA_MIDDLE_FRAG) {
1045 				/*
1046 				 * Insert chk MUST be a MIDDLE or LAST
1047 				 * fragment
1048 				 */
1049 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1050 				    SCTP_DATA_FIRST_FRAG) {
1051 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1052 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1053 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1054 					    0, M_DONTWAIT, 1, MT_DATA);
1055 					if (oper) {
1056 						struct sctp_paramhdr *ph;
1057 						uint32_t *ippp;
1058 
1059 						SCTP_BUF_LEN(oper) =
1060 						    sizeof(struct sctp_paramhdr) +
1061 						    (3 * sizeof(uint32_t));
1062 						ph = mtod(oper,
1063 						    struct sctp_paramhdr *);
1064 						ph->param_type =
1065 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1066 						ph->param_length =
1067 						    htons(SCTP_BUF_LEN(oper));
1068 						ippp = (uint32_t *) (ph + 1);
1069 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1070 						ippp++;
1071 						*ippp = chk->rec.data.TSN_seq;
1072 						ippp++;
1073 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1074 
1075 					}
1076 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1077 					sctp_abort_an_association(stcb->sctp_ep,
1078 					    stcb, SCTP_PEER_FAULTY, oper);
1079 					*abort_flag = 1;
1080 					return;
1081 				}
1082 				if (chk->rec.data.stream_number !=
1083 				    prev->rec.data.stream_number) {
1084 					/*
1085 					 * Huh, need the correct STR here,
1086 					 * they must be the same.
1087 					 */
1088 					SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1089 					    chk->rec.data.stream_number,
1090 					    prev->rec.data.stream_number);
1091 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1092 					    0, M_DONTWAIT, 1, MT_DATA);
1093 					if (oper) {
1094 						struct sctp_paramhdr *ph;
1095 						uint32_t *ippp;
1096 
1097 						SCTP_BUF_LEN(oper) =
1098 						    sizeof(struct sctp_paramhdr) +
1099 						    (3 * sizeof(uint32_t));
1100 						ph = mtod(oper,
1101 						    struct sctp_paramhdr *);
1102 						ph->param_type =
1103 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1104 						ph->param_length =
1105 						    htons(SCTP_BUF_LEN(oper));
1106 						ippp = (uint32_t *) (ph + 1);
1107 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1108 						ippp++;
1109 						*ippp = chk->rec.data.TSN_seq;
1110 						ippp++;
1111 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1112 					}
1113 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1114 					sctp_abort_an_association(stcb->sctp_ep,
1115 					    stcb, SCTP_PEER_FAULTY, oper);
1116 
1117 					*abort_flag = 1;
1118 					return;
1119 				}
1120 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1121 				    chk->rec.data.stream_seq !=
1122 				    prev->rec.data.stream_seq) {
1123 					/*
1124 					 * Huh, need the correct STR here,
1125 					 * they must be the same.
1126 					 */
1127 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1128 					    chk->rec.data.stream_seq,
1129 					    prev->rec.data.stream_seq);
1130 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1131 					    0, M_DONTWAIT, 1, MT_DATA);
1132 					if (oper) {
1133 						struct sctp_paramhdr *ph;
1134 						uint32_t *ippp;
1135 
1136 						SCTP_BUF_LEN(oper) =
1137 						    sizeof(struct sctp_paramhdr) +
1138 						    (3 * sizeof(uint32_t));
1139 						ph = mtod(oper,
1140 						    struct sctp_paramhdr *);
1141 						ph->param_type =
1142 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1143 						ph->param_length =
1144 						    htons(SCTP_BUF_LEN(oper));
1145 						ippp = (uint32_t *) (ph + 1);
1146 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1147 						ippp++;
1148 						*ippp = chk->rec.data.TSN_seq;
1149 						ippp++;
1150 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1151 					}
1152 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1153 					sctp_abort_an_association(stcb->sctp_ep,
1154 					    stcb, SCTP_PEER_FAULTY, oper);
1155 
1156 					*abort_flag = 1;
1157 					return;
1158 				}
1159 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1160 			    SCTP_DATA_LAST_FRAG) {
1161 				/* Insert chk MUST be a FIRST */
1162 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1163 				    SCTP_DATA_FIRST_FRAG) {
1164 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1165 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1166 					    0, M_DONTWAIT, 1, MT_DATA);
1167 					if (oper) {
1168 						struct sctp_paramhdr *ph;
1169 						uint32_t *ippp;
1170 
1171 						SCTP_BUF_LEN(oper) =
1172 						    sizeof(struct sctp_paramhdr) +
1173 						    (3 * sizeof(uint32_t));
1174 						ph = mtod(oper,
1175 						    struct sctp_paramhdr *);
1176 						ph->param_type =
1177 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1178 						ph->param_length =
1179 						    htons(SCTP_BUF_LEN(oper));
1180 						ippp = (uint32_t *) (ph + 1);
1181 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1182 						ippp++;
1183 						*ippp = chk->rec.data.TSN_seq;
1184 						ippp++;
1185 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1186 
1187 					}
1188 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1189 					sctp_abort_an_association(stcb->sctp_ep,
1190 					    stcb, SCTP_PEER_FAULTY, oper);
1191 
1192 					*abort_flag = 1;
1193 					return;
1194 				}
1195 			}
1196 		}
1197 	}
1198 	if (next) {
1199 		post_tsn = chk->rec.data.TSN_seq + 1;
1200 		if (post_tsn == next->rec.data.TSN_seq) {
1201 			/*
1202 			 * Ok the one I am inserting ahead of is my NEXT
1203 			 * one. A bit of valdiation here.
1204 			 */
1205 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1206 				/* Insert chk MUST be a last fragment */
1207 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1208 				    != SCTP_DATA_LAST_FRAG) {
1209 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1210 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1211 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1212 					    0, M_DONTWAIT, 1, MT_DATA);
1213 					if (oper) {
1214 						struct sctp_paramhdr *ph;
1215 						uint32_t *ippp;
1216 
1217 						SCTP_BUF_LEN(oper) =
1218 						    sizeof(struct sctp_paramhdr) +
1219 						    (3 * sizeof(uint32_t));
1220 						ph = mtod(oper,
1221 						    struct sctp_paramhdr *);
1222 						ph->param_type =
1223 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1224 						ph->param_length =
1225 						    htons(SCTP_BUF_LEN(oper));
1226 						ippp = (uint32_t *) (ph + 1);
1227 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1228 						ippp++;
1229 						*ippp = chk->rec.data.TSN_seq;
1230 						ippp++;
1231 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1232 					}
1233 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1234 					sctp_abort_an_association(stcb->sctp_ep,
1235 					    stcb, SCTP_PEER_FAULTY, oper);
1236 
1237 					*abort_flag = 1;
1238 					return;
1239 				}
1240 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1241 				    SCTP_DATA_MIDDLE_FRAG ||
1242 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1243 			    SCTP_DATA_LAST_FRAG) {
1244 				/*
1245 				 * Insert chk CAN be MIDDLE or FIRST NOT
1246 				 * LAST
1247 				 */
1248 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1249 				    SCTP_DATA_LAST_FRAG) {
1250 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1251 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1252 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1253 					    0, M_DONTWAIT, 1, MT_DATA);
1254 					if (oper) {
1255 						struct sctp_paramhdr *ph;
1256 						uint32_t *ippp;
1257 
1258 						SCTP_BUF_LEN(oper) =
1259 						    sizeof(struct sctp_paramhdr) +
1260 						    (3 * sizeof(uint32_t));
1261 						ph = mtod(oper,
1262 						    struct sctp_paramhdr *);
1263 						ph->param_type =
1264 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1265 						ph->param_length =
1266 						    htons(SCTP_BUF_LEN(oper));
1267 						ippp = (uint32_t *) (ph + 1);
1268 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1269 						ippp++;
1270 						*ippp = chk->rec.data.TSN_seq;
1271 						ippp++;
1272 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1273 
1274 					}
1275 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1276 					sctp_abort_an_association(stcb->sctp_ep,
1277 					    stcb, SCTP_PEER_FAULTY, oper);
1278 
1279 					*abort_flag = 1;
1280 					return;
1281 				}
1282 				if (chk->rec.data.stream_number !=
1283 				    next->rec.data.stream_number) {
1284 					/*
1285 					 * Huh, need the correct STR here,
1286 					 * they must be the same.
1287 					 */
1288 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1289 					    chk->rec.data.stream_number,
1290 					    next->rec.data.stream_number);
1291 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1292 					    0, M_DONTWAIT, 1, MT_DATA);
1293 					if (oper) {
1294 						struct sctp_paramhdr *ph;
1295 						uint32_t *ippp;
1296 
1297 						SCTP_BUF_LEN(oper) =
1298 						    sizeof(struct sctp_paramhdr) +
1299 						    (3 * sizeof(uint32_t));
1300 						ph = mtod(oper,
1301 						    struct sctp_paramhdr *);
1302 						ph->param_type =
1303 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1304 						ph->param_length =
1305 						    htons(SCTP_BUF_LEN(oper));
1306 						ippp = (uint32_t *) (ph + 1);
1307 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1308 						ippp++;
1309 						*ippp = chk->rec.data.TSN_seq;
1310 						ippp++;
1311 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1312 
1313 					}
1314 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1315 					sctp_abort_an_association(stcb->sctp_ep,
1316 					    stcb, SCTP_PEER_FAULTY, oper);
1317 
1318 					*abort_flag = 1;
1319 					return;
1320 				}
1321 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1322 				    chk->rec.data.stream_seq !=
1323 				    next->rec.data.stream_seq) {
1324 					/*
1325 					 * Huh, need the correct STR here,
1326 					 * they must be the same.
1327 					 */
1328 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1329 					    chk->rec.data.stream_seq,
1330 					    next->rec.data.stream_seq);
1331 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1332 					    0, M_DONTWAIT, 1, MT_DATA);
1333 					if (oper) {
1334 						struct sctp_paramhdr *ph;
1335 						uint32_t *ippp;
1336 
1337 						SCTP_BUF_LEN(oper) =
1338 						    sizeof(struct sctp_paramhdr) +
1339 						    (3 * sizeof(uint32_t));
1340 						ph = mtod(oper,
1341 						    struct sctp_paramhdr *);
1342 						ph->param_type =
1343 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1344 						ph->param_length =
1345 						    htons(SCTP_BUF_LEN(oper));
1346 						ippp = (uint32_t *) (ph + 1);
1347 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1348 						ippp++;
1349 						*ippp = chk->rec.data.TSN_seq;
1350 						ippp++;
1351 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1352 					}
1353 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1354 					sctp_abort_an_association(stcb->sctp_ep,
1355 					    stcb, SCTP_PEER_FAULTY, oper);
1356 
1357 					*abort_flag = 1;
1358 					return;
1359 
1360 				}
1361 			}
1362 		}
1363 	}
1364 	/* Do we need to do some delivery? check */
1365 	sctp_deliver_reasm_check(stcb, asoc);
1366 }
1367 
1368 /*
1369  * This is an unfortunate routine. It checks to make sure a evil guy is not
1370  * stuffing us full of bad packet fragments. A broken peer could also do this
1371  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1372  * :< more cycles.
1373  */
1374 static int
1375 sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1376     uint32_t TSN_seq)
1377 {
1378 	struct sctp_tmit_chunk *at;
1379 	uint32_t tsn_est;
1380 
1381 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1382 		if (compare_with_wrap(TSN_seq,
1383 		    at->rec.data.TSN_seq, MAX_TSN)) {
1384 			/* is it one bigger? */
1385 			tsn_est = at->rec.data.TSN_seq + 1;
1386 			if (tsn_est == TSN_seq) {
1387 				/* yep. It better be a last then */
1388 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1389 				    SCTP_DATA_LAST_FRAG) {
1390 					/*
1391 					 * Ok this guy belongs next to a guy
1392 					 * that is NOT last, it should be a
1393 					 * middle/last, not a complete
1394 					 * chunk.
1395 					 */
1396 					return (1);
1397 				} else {
1398 					/*
1399 					 * This guy is ok since its a LAST
1400 					 * and the new chunk is a fully
1401 					 * self- contained one.
1402 					 */
1403 					return (0);
1404 				}
1405 			}
1406 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1407 			/* Software error since I have a dup? */
1408 			return (1);
1409 		} else {
1410 			/*
1411 			 * Ok, 'at' is larger than new chunk but does it
1412 			 * need to be right before it.
1413 			 */
1414 			tsn_est = TSN_seq + 1;
1415 			if (tsn_est == at->rec.data.TSN_seq) {
1416 				/* Yep, It better be a first */
1417 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1418 				    SCTP_DATA_FIRST_FRAG) {
1419 					return (1);
1420 				} else {
1421 					return (0);
1422 				}
1423 			}
1424 		}
1425 	}
1426 	return (0);
1427 }
1428 
1429 
1430 static int
1431 sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1432     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1433     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1434     int *break_flag, int last_chunk)
1435 {
1436 	/* Process a data chunk */
1437 	/* struct sctp_tmit_chunk *chk; */
1438 	struct sctp_tmit_chunk *chk;
1439 	uint32_t tsn, gap;
1440 	struct mbuf *dmbuf;
1441 	int indx, the_len;
1442 	int need_reasm_check = 0;
1443 	uint16_t strmno, strmseq;
1444 	struct mbuf *oper;
1445 	struct sctp_queued_to_read *control;
1446 	int ordered;
1447 	uint32_t protocol_id;
1448 	uint8_t chunk_flags;
1449 	struct sctp_stream_reset_list *liste;
1450 
1451 	chk = NULL;
1452 	tsn = ntohl(ch->dp.tsn);
1453 	chunk_flags = ch->ch.chunk_flags;
1454 	protocol_id = ch->dp.protocol_id;
1455 	ordered = ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0);
1456 #ifdef SCTP_MAP_LOGGING
1457 	sctp_log_map(0, tsn, asoc->cumulative_tsn, SCTP_MAP_PREPARE_SLIDE);
1458 #endif
1459 	if (stcb == NULL) {
1460 		return (0);
1461 	}
1462 	if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
1463 	    asoc->cumulative_tsn == tsn) {
1464 		/* It is a duplicate */
1465 		SCTP_STAT_INCR(sctps_recvdupdata);
1466 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1467 			/* Record a dup for the next outbound sack */
1468 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1469 			asoc->numduptsns++;
1470 		}
1471 		return (0);
1472 	}
1473 	/* Calculate the number of TSN's between the base and this TSN */
1474 	if (tsn >= asoc->mapping_array_base_tsn) {
1475 		gap = tsn - asoc->mapping_array_base_tsn;
1476 	} else {
1477 		gap = (MAX_TSN - asoc->mapping_array_base_tsn) + tsn + 1;
1478 	}
1479 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1480 		/* Can't hold the bit in the mapping at max array, toss it */
1481 		return (0);
1482 	}
1483 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1484 		if (sctp_expand_mapping_array(asoc)) {
1485 			/* Can't expand, drop it */
1486 			return (0);
1487 		}
1488 	}
1489 	if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1490 		*high_tsn = tsn;
1491 	}
1492 	/* See if we have received this one already */
1493 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
1494 		SCTP_STAT_INCR(sctps_recvdupdata);
1495 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1496 			/* Record a dup for the next outbound sack */
1497 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1498 			asoc->numduptsns++;
1499 		}
1500 		asoc->send_sack = 1;
1501 		return (0);
1502 	}
1503 	/*
1504 	 * Check to see about the GONE flag, duplicates would cause a sack
1505 	 * to be sent up above
1506 	 */
1507 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1508 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1509 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1510 	    ) {
1511 		/*
1512 		 * wait a minute, this guy is gone, there is no longer a
1513 		 * receiver. Send peer an ABORT!
1514 		 */
1515 		struct mbuf *op_err;
1516 
1517 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1518 		sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err);
1519 		*abort_flag = 1;
1520 		return (0);
1521 	}
1522 	/*
1523 	 * Now before going further we see if there is room. If NOT then we
1524 	 * MAY let one through only IF this TSN is the one we are waiting
1525 	 * for on a partial delivery API.
1526 	 */
1527 
1528 	/* now do the tests */
1529 	if (((asoc->cnt_on_all_streams +
1530 	    asoc->cnt_on_reasm_queue +
1531 	    asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) ||
1532 	    (((int)asoc->my_rwnd) <= 0)) {
1533 		/*
1534 		 * When we have NO room in the rwnd we check to make sure
1535 		 * the reader is doing its job...
1536 		 */
1537 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1538 			/* some to read, wake-up */
1539 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1540 		}
1541 		/* now is it in the mapping array of what we have accepted? */
1542 		if (compare_with_wrap(tsn,
1543 		    asoc->highest_tsn_inside_map, MAX_TSN)) {
1544 
1545 			/* Nope not in the valid range dump it */
1546 			SCTPDBG(SCTP_DEBUG_INDATA1, "My rwnd overrun1:tsn:%lx rwnd %lu sbspace:%ld\n",
1547 			    (u_long)tsn, (u_long)asoc->my_rwnd,
1548 			    sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv));
1549 			sctp_set_rwnd(stcb, asoc);
1550 			if ((asoc->cnt_on_all_streams +
1551 			    asoc->cnt_on_reasm_queue +
1552 			    asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) {
1553 				SCTP_STAT_INCR(sctps_datadropchklmt);
1554 			} else {
1555 				SCTP_STAT_INCR(sctps_datadroprwnd);
1556 			}
1557 			indx = *break_flag;
1558 			*break_flag = 1;
1559 			return (0);
1560 		}
1561 	}
1562 	strmno = ntohs(ch->dp.stream_id);
1563 	if (strmno >= asoc->streamincnt) {
1564 		struct sctp_paramhdr *phdr;
1565 		struct mbuf *mb;
1566 
1567 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1568 		    0, M_DONTWAIT, 1, MT_DATA);
1569 		if (mb != NULL) {
1570 			/* add some space up front so prepend will work well */
1571 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1572 			phdr = mtod(mb, struct sctp_paramhdr *);
1573 			/*
1574 			 * Error causes are just param's and this one has
1575 			 * two back to back phdr, one with the error type
1576 			 * and size, the other with the streamid and a rsvd
1577 			 */
1578 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1579 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1580 			phdr->param_length =
1581 			    htons(sizeof(struct sctp_paramhdr) * 2);
1582 			phdr++;
1583 			/* We insert the stream in the type field */
1584 			phdr->param_type = ch->dp.stream_id;
1585 			/* And set the length to 0 for the rsvd field */
1586 			phdr->param_length = 0;
1587 			sctp_queue_op_err(stcb, mb);
1588 		}
1589 		SCTP_STAT_INCR(sctps_badsid);
1590 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1591 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1592 			/* we have a new high score */
1593 			asoc->highest_tsn_inside_map = tsn;
1594 #ifdef SCTP_MAP_LOGGING
1595 			sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
1596 #endif
1597 		}
1598 		if (tsn == (asoc->cumulative_tsn + 1)) {
1599 			/* Update cum-ack */
1600 			asoc->cumulative_tsn = tsn;
1601 		}
1602 		return (0);
1603 	}
1604 	/*
1605 	 * Before we continue lets validate that we are not being fooled by
1606 	 * an evil attacker. We can only have 4k chunks based on our TSN
1607 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1608 	 * way our stream sequence numbers could have wrapped. We of course
1609 	 * only validate the FIRST fragment so the bit must be set.
1610 	 */
1611 	strmseq = ntohs(ch->dp.stream_sequence);
1612 
1613 #ifdef SCTP_ASOCLOG_OF_TSNS
1614 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1615 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1616 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1617 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1618 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
1619 	asoc->tsn_in_at++;
1620 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
1621 		asoc->tsn_in_at = 0;
1622 		asoc->tsn_in_wrapped = 1;
1623 	}
1624 #endif
1625 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1626 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1627 	    (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1628 	    strmseq, MAX_SEQ) ||
1629 	    asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1630 		/* The incoming sseq is behind where we last delivered? */
1631 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1632 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1633 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1634 		    0, M_DONTWAIT, 1, MT_DATA);
1635 		if (oper) {
1636 			struct sctp_paramhdr *ph;
1637 			uint32_t *ippp;
1638 
1639 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1640 			    (3 * sizeof(uint32_t));
1641 			ph = mtod(oper, struct sctp_paramhdr *);
1642 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1643 			ph->param_length = htons(SCTP_BUF_LEN(oper));
1644 			ippp = (uint32_t *) (ph + 1);
1645 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1646 			ippp++;
1647 			*ippp = tsn;
1648 			ippp++;
1649 			*ippp = ((strmno << 16) | strmseq);
1650 
1651 		}
1652 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1653 		sctp_abort_an_association(stcb->sctp_ep, stcb,
1654 		    SCTP_PEER_FAULTY, oper);
1655 		*abort_flag = 1;
1656 		return (0);
1657 	}
1658 	/************************************
1659 	 * From here down we may find ch-> invalid
1660 	 * so its a good idea NOT to use it.
1661 	 *************************************/
1662 
1663 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1664 	if (last_chunk == 0) {
1665 		dmbuf = SCTP_M_COPYM(*m,
1666 		    (offset + sizeof(struct sctp_data_chunk)),
1667 		    the_len, M_DONTWAIT);
1668 #ifdef SCTP_MBUF_LOGGING
1669 		{
1670 			struct mbuf *mat;
1671 
1672 			mat = dmbuf;
1673 			while (mat) {
1674 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1675 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1676 				}
1677 				mat = SCTP_BUF_NEXT(mat);
1678 			}
1679 		}
1680 #endif
1681 	} else {
1682 		/* We can steal the last chunk */
1683 		int l_len;
1684 
1685 		dmbuf = *m;
1686 		/* lop off the top part */
1687 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1688 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1689 			l_len = SCTP_BUF_LEN(dmbuf);
1690 		} else {
1691 			/*
1692 			 * need to count up the size hopefully does not hit
1693 			 * this to often :-0
1694 			 */
1695 			struct mbuf *lat;
1696 
1697 			l_len = 0;
1698 			lat = dmbuf;
1699 			while (lat) {
1700 				l_len += SCTP_BUF_LEN(lat);
1701 				lat = SCTP_BUF_NEXT(lat);
1702 			}
1703 		}
1704 		if (l_len > the_len) {
1705 			/* Trim the end round bytes off  too */
1706 			m_adj(dmbuf, -(l_len - the_len));
1707 		}
1708 	}
1709 	if (dmbuf == NULL) {
1710 		SCTP_STAT_INCR(sctps_nomem);
1711 		return (0);
1712 	}
1713 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1714 	    asoc->fragmented_delivery_inprogress == 0 &&
1715 	    TAILQ_EMPTY(&asoc->resetHead) &&
1716 	    ((ordered == 0) ||
1717 	    ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1718 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1719 		/* Candidate for express delivery */
1720 		/*
1721 		 * Its not fragmented, No PD-API is up, Nothing in the
1722 		 * delivery queue, Its un-ordered OR ordered and the next to
1723 		 * deliver AND nothing else is stuck on the stream queue,
1724 		 * And there is room for it in the socket buffer. Lets just
1725 		 * stuff it up the buffer....
1726 		 */
1727 
1728 		/* It would be nice to avoid this copy if we could :< */
1729 		sctp_alloc_a_readq(stcb, control);
1730 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1731 		    protocol_id,
1732 		    stcb->asoc.context,
1733 		    strmno, strmseq,
1734 		    chunk_flags,
1735 		    dmbuf);
1736 		if (control == NULL) {
1737 			goto failed_express_del;
1738 		}
1739 		sctp_add_to_readq(stcb->sctp_ep, stcb, control, &stcb->sctp_socket->so_rcv, 1);
1740 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1741 			/* for ordered, bump what we delivered */
1742 			asoc->strmin[strmno].last_sequence_delivered++;
1743 		}
1744 		SCTP_STAT_INCR(sctps_recvexpress);
1745 #ifdef SCTP_STR_LOGGING
1746 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1747 		    SCTP_STR_LOG_FROM_EXPRS_DEL);
1748 #endif
1749 		control = NULL;
1750 		goto finish_express_del;
1751 	}
1752 failed_express_del:
1753 	/* If we reach here this is a new chunk */
1754 	chk = NULL;
1755 	control = NULL;
1756 	/* Express for fragmented delivery? */
1757 	if ((asoc->fragmented_delivery_inprogress) &&
1758 	    (stcb->asoc.control_pdapi) &&
1759 	    (asoc->str_of_pdapi == strmno) &&
1760 	    (asoc->ssn_of_pdapi == strmseq)
1761 	    ) {
1762 		control = stcb->asoc.control_pdapi;
1763 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1764 			/* Can't be another first? */
1765 			goto failed_pdapi_express_del;
1766 		}
1767 		if (tsn == (control->sinfo_tsn + 1)) {
1768 			/* Yep, we can add it on */
1769 			int end = 0;
1770 			uint32_t cumack;
1771 
1772 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1773 				end = 1;
1774 			}
1775 			cumack = asoc->cumulative_tsn;
1776 			if ((cumack + 1) == tsn)
1777 				cumack = tsn;
1778 
1779 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1780 			    tsn,
1781 			    &stcb->sctp_socket->so_rcv)) {
1782 				SCTP_PRINTF("Append fails end:%d\n", end);
1783 				goto failed_pdapi_express_del;
1784 			}
1785 			SCTP_STAT_INCR(sctps_recvexpressm);
1786 			control->sinfo_tsn = tsn;
1787 			asoc->tsn_last_delivered = tsn;
1788 			asoc->fragment_flags = chunk_flags;
1789 			asoc->tsn_of_pdapi_last_delivered = tsn;
1790 			asoc->last_flags_delivered = chunk_flags;
1791 			asoc->last_strm_seq_delivered = strmseq;
1792 			asoc->last_strm_no_delivered = strmno;
1793 			if (end) {
1794 				/* clean up the flags and such */
1795 				asoc->fragmented_delivery_inprogress = 0;
1796 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1797 					asoc->strmin[strmno].last_sequence_delivered++;
1798 				}
1799 				stcb->asoc.control_pdapi = NULL;
1800 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1801 					/*
1802 					 * There could be another message
1803 					 * ready
1804 					 */
1805 					need_reasm_check = 1;
1806 				}
1807 			}
1808 			control = NULL;
1809 			goto finish_express_del;
1810 		}
1811 	}
1812 failed_pdapi_express_del:
1813 	control = NULL;
1814 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1815 		sctp_alloc_a_chunk(stcb, chk);
1816 		if (chk == NULL) {
1817 			/* No memory so we drop the chunk */
1818 			SCTP_STAT_INCR(sctps_nomem);
1819 			if (last_chunk == 0) {
1820 				/* we copied it, free the copy */
1821 				sctp_m_freem(dmbuf);
1822 			}
1823 			return (0);
1824 		}
1825 		chk->rec.data.TSN_seq = tsn;
1826 		chk->no_fr_allowed = 0;
1827 		chk->rec.data.stream_seq = strmseq;
1828 		chk->rec.data.stream_number = strmno;
1829 		chk->rec.data.payloadtype = protocol_id;
1830 		chk->rec.data.context = stcb->asoc.context;
1831 		chk->rec.data.doing_fast_retransmit = 0;
1832 		chk->rec.data.rcv_flags = chunk_flags;
1833 		chk->asoc = asoc;
1834 		chk->send_size = the_len;
1835 		chk->whoTo = net;
1836 		atomic_add_int(&net->ref_count, 1);
1837 		chk->data = dmbuf;
1838 	} else {
1839 		sctp_alloc_a_readq(stcb, control);
1840 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1841 		    protocol_id,
1842 		    stcb->asoc.context,
1843 		    strmno, strmseq,
1844 		    chunk_flags,
1845 		    dmbuf);
1846 		if (control == NULL) {
1847 			/* No memory so we drop the chunk */
1848 			SCTP_STAT_INCR(sctps_nomem);
1849 			if (last_chunk == 0) {
1850 				/* we copied it, free the copy */
1851 				sctp_m_freem(dmbuf);
1852 			}
1853 			return (0);
1854 		}
1855 		control->length = the_len;
1856 	}
1857 
1858 	/* Mark it as received */
1859 	/* Now queue it where it belongs */
1860 	if (control != NULL) {
1861 		/* First a sanity check */
1862 		if (asoc->fragmented_delivery_inprogress) {
1863 			/*
1864 			 * Ok, we have a fragmented delivery in progress if
1865 			 * this chunk is next to deliver OR belongs in our
1866 			 * view to the reassembly, the peer is evil or
1867 			 * broken.
1868 			 */
1869 			uint32_t estimate_tsn;
1870 
1871 			estimate_tsn = asoc->tsn_last_delivered + 1;
1872 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1873 			    (estimate_tsn == control->sinfo_tsn)) {
1874 				/* Evil/Broke peer */
1875 				sctp_m_freem(control->data);
1876 				control->data = NULL;
1877 				sctp_free_remote_addr(control->whoFrom);
1878 				sctp_free_a_readq(stcb, control);
1879 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1880 				    0, M_DONTWAIT, 1, MT_DATA);
1881 				if (oper) {
1882 					struct sctp_paramhdr *ph;
1883 					uint32_t *ippp;
1884 
1885 					SCTP_BUF_LEN(oper) =
1886 					    sizeof(struct sctp_paramhdr) +
1887 					    (3 * sizeof(uint32_t));
1888 					ph = mtod(oper, struct sctp_paramhdr *);
1889 					ph->param_type =
1890 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1891 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1892 					ippp = (uint32_t *) (ph + 1);
1893 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1894 					ippp++;
1895 					*ippp = tsn;
1896 					ippp++;
1897 					*ippp = ((strmno << 16) | strmseq);
1898 				}
1899 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1900 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1901 				    SCTP_PEER_FAULTY, oper);
1902 
1903 				*abort_flag = 1;
1904 				return (0);
1905 			} else {
1906 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1907 					sctp_m_freem(control->data);
1908 					control->data = NULL;
1909 					sctp_free_remote_addr(control->whoFrom);
1910 					sctp_free_a_readq(stcb, control);
1911 
1912 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1913 					    0, M_DONTWAIT, 1, MT_DATA);
1914 					if (oper) {
1915 						struct sctp_paramhdr *ph;
1916 						uint32_t *ippp;
1917 
1918 						SCTP_BUF_LEN(oper) =
1919 						    sizeof(struct sctp_paramhdr) +
1920 						    (3 * sizeof(uint32_t));
1921 						ph = mtod(oper,
1922 						    struct sctp_paramhdr *);
1923 						ph->param_type =
1924 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1925 						ph->param_length =
1926 						    htons(SCTP_BUF_LEN(oper));
1927 						ippp = (uint32_t *) (ph + 1);
1928 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
1929 						ippp++;
1930 						*ippp = tsn;
1931 						ippp++;
1932 						*ippp = ((strmno << 16) | strmseq);
1933 					}
1934 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1935 					sctp_abort_an_association(stcb->sctp_ep,
1936 					    stcb, SCTP_PEER_FAULTY, oper);
1937 
1938 					*abort_flag = 1;
1939 					return (0);
1940 				}
1941 			}
1942 		} else {
1943 			/* No PDAPI running */
1944 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1945 				/*
1946 				 * Reassembly queue is NOT empty validate
1947 				 * that this tsn does not need to be in
1948 				 * reasembly queue. If it does then our peer
1949 				 * is broken or evil.
1950 				 */
1951 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1952 					sctp_m_freem(control->data);
1953 					control->data = NULL;
1954 					sctp_free_remote_addr(control->whoFrom);
1955 					sctp_free_a_readq(stcb, control);
1956 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1957 					    0, M_DONTWAIT, 1, MT_DATA);
1958 					if (oper) {
1959 						struct sctp_paramhdr *ph;
1960 						uint32_t *ippp;
1961 
1962 						SCTP_BUF_LEN(oper) =
1963 						    sizeof(struct sctp_paramhdr) +
1964 						    (3 * sizeof(uint32_t));
1965 						ph = mtod(oper,
1966 						    struct sctp_paramhdr *);
1967 						ph->param_type =
1968 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1969 						ph->param_length =
1970 						    htons(SCTP_BUF_LEN(oper));
1971 						ippp = (uint32_t *) (ph + 1);
1972 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
1973 						ippp++;
1974 						*ippp = tsn;
1975 						ippp++;
1976 						*ippp = ((strmno << 16) | strmseq);
1977 					}
1978 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
1979 					sctp_abort_an_association(stcb->sctp_ep,
1980 					    stcb, SCTP_PEER_FAULTY, oper);
1981 
1982 					*abort_flag = 1;
1983 					return (0);
1984 				}
1985 			}
1986 		}
1987 		/* ok, if we reach here we have passed the sanity checks */
1988 		if (chunk_flags & SCTP_DATA_UNORDERED) {
1989 			/* queue directly into socket buffer */
1990 			sctp_add_to_readq(stcb->sctp_ep, stcb,
1991 			    control,
1992 			    &stcb->sctp_socket->so_rcv, 1);
1993 		} else {
1994 			/*
1995 			 * Special check for when streams are resetting. We
1996 			 * could be more smart about this and check the
1997 			 * actual stream to see if it is not being reset..
1998 			 * that way we would not create a HOLB when amongst
1999 			 * streams being reset and those not being reset.
2000 			 *
2001 			 * We take complete messages that have a stream reset
2002 			 * intervening (aka the TSN is after where our
2003 			 * cum-ack needs to be) off and put them on a
2004 			 * pending_reply_queue. The reassembly ones we do
2005 			 * not have to worry about since they are all sorted
2006 			 * and proceessed by TSN order. It is only the
2007 			 * singletons I must worry about.
2008 			 */
2009 			struct sctp_stream_reset_list *liste;
2010 
2011 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2012 			    ((compare_with_wrap(tsn, ntohl(liste->tsn), MAX_TSN)) ||
2013 			    (tsn == ntohl(liste->tsn)))
2014 			    ) {
2015 				/*
2016 				 * yep its past where we need to reset... go
2017 				 * ahead and queue it.
2018 				 */
2019 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2020 					/* first one on */
2021 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2022 				} else {
2023 					struct sctp_queued_to_read *ctlOn;
2024 					unsigned char inserted = 0;
2025 
2026 					ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2027 					while (ctlOn) {
2028 						if (compare_with_wrap(control->sinfo_tsn,
2029 						    ctlOn->sinfo_tsn, MAX_TSN)) {
2030 							ctlOn = TAILQ_NEXT(ctlOn, next);
2031 						} else {
2032 							/* found it */
2033 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
2034 							inserted = 1;
2035 							break;
2036 						}
2037 					}
2038 					if (inserted == 0) {
2039 						/*
2040 						 * must be put at end, use
2041 						 * prevP (all setup from
2042 						 * loop) to setup nextP.
2043 						 */
2044 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2045 					}
2046 				}
2047 			} else {
2048 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2049 				if (*abort_flag) {
2050 					return (0);
2051 				}
2052 			}
2053 		}
2054 	} else {
2055 		/* Into the re-assembly queue */
2056 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2057 		if (*abort_flag) {
2058 			/*
2059 			 * the assoc is now gone and chk was put onto the
2060 			 * reasm queue, which has all been freed.
2061 			 */
2062 			*m = NULL;
2063 			return (0);
2064 		}
2065 	}
2066 finish_express_del:
2067 	if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
2068 		/* we have a new high score */
2069 		asoc->highest_tsn_inside_map = tsn;
2070 #ifdef SCTP_MAP_LOGGING
2071 		sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2072 #endif
2073 	}
2074 	if (tsn == (asoc->cumulative_tsn + 1)) {
2075 		/* Update cum-ack */
2076 		asoc->cumulative_tsn = tsn;
2077 	}
2078 	if (last_chunk) {
2079 		*m = NULL;
2080 	}
2081 	if (ordered) {
2082 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2083 	} else {
2084 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2085 	}
2086 	SCTP_STAT_INCR(sctps_recvdata);
2087 	/* Set it present please */
2088 #ifdef SCTP_STR_LOGGING
2089 	sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
2090 #endif
2091 #ifdef SCTP_MAP_LOGGING
2092 	sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2093 	    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
2094 #endif
2095 	SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
2096 	/* check the special flag for stream resets */
2097 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2098 	    ((compare_with_wrap(asoc->cumulative_tsn, ntohl(liste->tsn), MAX_TSN)) ||
2099 	    (asoc->cumulative_tsn == ntohl(liste->tsn)))
2100 	    ) {
2101 		/*
2102 		 * we have finished working through the backlogged TSN's now
2103 		 * time to reset streams. 1: call reset function. 2: free
2104 		 * pending_reply space 3: distribute any chunks in
2105 		 * pending_reply_queue.
2106 		 */
2107 		struct sctp_queued_to_read *ctl;
2108 
2109 		sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
2110 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2111 		SCTP_FREE(liste);
2112 		/* sa_ignore FREED_MEMORY */
2113 		liste = TAILQ_FIRST(&asoc->resetHead);
2114 		ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2115 		if (ctl && (liste == NULL)) {
2116 			/* All can be removed */
2117 			while (ctl) {
2118 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2119 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2120 				if (*abort_flag) {
2121 					return (0);
2122 				}
2123 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2124 			}
2125 		} else if (ctl) {
2126 			/* more than one in queue */
2127 			while (!compare_with_wrap(ctl->sinfo_tsn, ntohl(liste->tsn), MAX_TSN)) {
2128 				/*
2129 				 * if ctl->sinfo_tsn is <= liste->tsn we can
2130 				 * process it which is the NOT of
2131 				 * ctl->sinfo_tsn > liste->tsn
2132 				 */
2133 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2134 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2135 				if (*abort_flag) {
2136 					return (0);
2137 				}
2138 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2139 			}
2140 		}
2141 		/*
2142 		 * Now service re-assembly to pick up anything that has been
2143 		 * held on reassembly queue?
2144 		 */
2145 		sctp_deliver_reasm_check(stcb, asoc);
2146 		need_reasm_check = 0;
2147 	}
2148 	if (need_reasm_check) {
2149 		/* Another one waits ? */
2150 		sctp_deliver_reasm_check(stcb, asoc);
2151 	}
2152 	return (1);
2153 }
2154 
2155 int8_t sctp_map_lookup_tab[256] = {
2156 	-1, 0, -1, 1, -1, 0, -1, 2,
2157 	-1, 0, -1, 1, -1, 0, -1, 3,
2158 	-1, 0, -1, 1, -1, 0, -1, 2,
2159 	-1, 0, -1, 1, -1, 0, -1, 4,
2160 	-1, 0, -1, 1, -1, 0, -1, 2,
2161 	-1, 0, -1, 1, -1, 0, -1, 3,
2162 	-1, 0, -1, 1, -1, 0, -1, 2,
2163 	-1, 0, -1, 1, -1, 0, -1, 5,
2164 	-1, 0, -1, 1, -1, 0, -1, 2,
2165 	-1, 0, -1, 1, -1, 0, -1, 3,
2166 	-1, 0, -1, 1, -1, 0, -1, 2,
2167 	-1, 0, -1, 1, -1, 0, -1, 4,
2168 	-1, 0, -1, 1, -1, 0, -1, 2,
2169 	-1, 0, -1, 1, -1, 0, -1, 3,
2170 	-1, 0, -1, 1, -1, 0, -1, 2,
2171 	-1, 0, -1, 1, -1, 0, -1, 6,
2172 	-1, 0, -1, 1, -1, 0, -1, 2,
2173 	-1, 0, -1, 1, -1, 0, -1, 3,
2174 	-1, 0, -1, 1, -1, 0, -1, 2,
2175 	-1, 0, -1, 1, -1, 0, -1, 4,
2176 	-1, 0, -1, 1, -1, 0, -1, 2,
2177 	-1, 0, -1, 1, -1, 0, -1, 3,
2178 	-1, 0, -1, 1, -1, 0, -1, 2,
2179 	-1, 0, -1, 1, -1, 0, -1, 5,
2180 	-1, 0, -1, 1, -1, 0, -1, 2,
2181 	-1, 0, -1, 1, -1, 0, -1, 3,
2182 	-1, 0, -1, 1, -1, 0, -1, 2,
2183 	-1, 0, -1, 1, -1, 0, -1, 4,
2184 	-1, 0, -1, 1, -1, 0, -1, 2,
2185 	-1, 0, -1, 1, -1, 0, -1, 3,
2186 	-1, 0, -1, 1, -1, 0, -1, 2,
2187 	-1, 0, -1, 1, -1, 0, -1, 7,
2188 };
2189 
2190 
2191 void
2192 sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2193 {
2194 	/*
2195 	 * Now we also need to check the mapping array in a couple of ways.
2196 	 * 1) Did we move the cum-ack point?
2197 	 */
2198 	struct sctp_association *asoc;
2199 	int i, at;
2200 	int all_ones, last_all_ones = 0;
2201 	int slide_from, slide_end, lgap, distance;
2202 
2203 #ifdef SCTP_MAP_LOGGING
2204 	uint32_t old_cumack, old_base, old_highest;
2205 	unsigned char aux_array[64];
2206 
2207 #endif
2208 
2209 	asoc = &stcb->asoc;
2210 	at = 0;
2211 
2212 #ifdef SCTP_MAP_LOGGING
2213 	old_cumack = asoc->cumulative_tsn;
2214 	old_base = asoc->mapping_array_base_tsn;
2215 	old_highest = asoc->highest_tsn_inside_map;
2216 	if (asoc->mapping_array_size < 64)
2217 		memcpy(aux_array, asoc->mapping_array,
2218 		    asoc->mapping_array_size);
2219 	else
2220 		memcpy(aux_array, asoc->mapping_array, 64);
2221 #endif
2222 
2223 	/*
2224 	 * We could probably improve this a small bit by calculating the
2225 	 * offset of the current cum-ack as the starting point.
2226 	 */
2227 	all_ones = 1;
2228 	at = 0;
2229 	for (i = 0; i < stcb->asoc.mapping_array_size; i++) {
2230 
2231 		if (asoc->mapping_array[i] == 0xff) {
2232 			at += 8;
2233 			last_all_ones = 1;
2234 		} else {
2235 			/* there is a 0 bit */
2236 			all_ones = 0;
2237 			at += sctp_map_lookup_tab[asoc->mapping_array[i]];
2238 			last_all_ones = 0;
2239 			break;
2240 		}
2241 	}
2242 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - last_all_ones);
2243 	/* at is one off, since in the table a embedded -1 is present */
2244 	at++;
2245 
2246 	if (compare_with_wrap(asoc->cumulative_tsn,
2247 	    asoc->highest_tsn_inside_map,
2248 	    MAX_TSN)) {
2249 #ifdef INVARIANTS
2250 		panic("huh, cumack greater than high-tsn in map");
2251 #else
2252 		SCTP_PRINTF("huh, cumack greater than high-tsn in map - should panic?\n");
2253 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2254 #endif
2255 	}
2256 	if (all_ones ||
2257 	    (asoc->cumulative_tsn == asoc->highest_tsn_inside_map && at >= 8)) {
2258 		/* The complete array was completed by a single FR */
2259 		/* higest becomes the cum-ack */
2260 		int clr;
2261 
2262 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
2263 		/* clear the array */
2264 		if (all_ones)
2265 			clr = asoc->mapping_array_size;
2266 		else {
2267 			clr = (at >> 3) + 1;
2268 			/*
2269 			 * this should be the allones case but just in case
2270 			 * :>
2271 			 */
2272 			if (clr > asoc->mapping_array_size)
2273 				clr = asoc->mapping_array_size;
2274 		}
2275 		memset(asoc->mapping_array, 0, clr);
2276 		/* base becomes one ahead of the cum-ack */
2277 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2278 #ifdef SCTP_MAP_LOGGING
2279 		sctp_log_map(old_base, old_cumack, old_highest,
2280 		    SCTP_MAP_PREPARE_SLIDE);
2281 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2282 		    asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
2283 #endif
2284 	} else if (at >= 8) {
2285 		/* we can slide the mapping array down */
2286 		/* Calculate the new byte postion we can move down */
2287 		slide_from = at >> 3;
2288 		/*
2289 		 * now calculate the ceiling of the move using our highest
2290 		 * TSN value
2291 		 */
2292 		if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
2293 			lgap = asoc->highest_tsn_inside_map -
2294 			    asoc->mapping_array_base_tsn;
2295 		} else {
2296 			lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
2297 			    asoc->highest_tsn_inside_map + 1;
2298 		}
2299 		slide_end = lgap >> 3;
2300 		if (slide_end < slide_from) {
2301 			panic("impossible slide");
2302 		}
2303 		distance = (slide_end - slide_from) + 1;
2304 #ifdef SCTP_MAP_LOGGING
2305 		sctp_log_map(old_base, old_cumack, old_highest,
2306 		    SCTP_MAP_PREPARE_SLIDE);
2307 		sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2308 		    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
2309 #endif
2310 		if (distance + slide_from > asoc->mapping_array_size ||
2311 		    distance < 0) {
2312 			/*
2313 			 * Here we do NOT slide forward the array so that
2314 			 * hopefully when more data comes in to fill it up
2315 			 * we will be able to slide it forward. Really I
2316 			 * don't think this should happen :-0
2317 			 */
2318 
2319 #ifdef SCTP_MAP_LOGGING
2320 			sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2321 			    (uint32_t) asoc->mapping_array_size,
2322 			    SCTP_MAP_SLIDE_NONE);
2323 #endif
2324 		} else {
2325 			int ii;
2326 
2327 			for (ii = 0; ii < distance; ii++) {
2328 				asoc->mapping_array[ii] =
2329 				    asoc->mapping_array[slide_from + ii];
2330 			}
2331 			for (ii = distance; ii <= slide_end; ii++) {
2332 				asoc->mapping_array[ii] = 0;
2333 			}
2334 			asoc->mapping_array_base_tsn += (slide_from << 3);
2335 #ifdef SCTP_MAP_LOGGING
2336 			sctp_log_map(asoc->mapping_array_base_tsn,
2337 			    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2338 			    SCTP_MAP_SLIDE_RESULT);
2339 #endif
2340 		}
2341 	}
2342 	/*
2343 	 * Now we need to see if we need to queue a sack or just start the
2344 	 * timer (if allowed).
2345 	 */
2346 	if (ok_to_sack) {
2347 		if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2348 			/*
2349 			 * Ok special case, in SHUTDOWN-SENT case. here we
2350 			 * maker sure SACK timer is off and instead send a
2351 			 * SHUTDOWN and a SACK
2352 			 */
2353 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2354 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2355 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2356 			}
2357 			sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2358 			sctp_send_sack(stcb);
2359 		} else {
2360 			int is_a_gap;
2361 
2362 			/* is there a gap now ? */
2363 			is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2364 			    stcb->asoc.cumulative_tsn, MAX_TSN);
2365 
2366 			/*
2367 			 * CMT DAC algorithm: increase number of packets
2368 			 * received since last ack
2369 			 */
2370 			stcb->asoc.cmt_dac_pkts_rcvd++;
2371 
2372 			if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
2373 								 * SACK */
2374 			    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2375 								 * longer is one */
2376 			    (stcb->asoc.numduptsns) ||	/* we have dup's */
2377 			    (is_a_gap) ||	/* is still a gap */
2378 			    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
2379 			    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2380 			    ) {
2381 
2382 				if ((sctp_cmt_on_off) && (sctp_cmt_use_dac) &&
2383 				    (stcb->asoc.send_sack == 0) &&
2384 				    (stcb->asoc.numduptsns == 0) &&
2385 				    (stcb->asoc.delayed_ack) &&
2386 				    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2387 
2388 					/*
2389 					 * CMT DAC algorithm: With CMT,
2390 					 * delay acks even in the face of
2391 					 *
2392 					 * reordering. Therefore, if acks that
2393 					 * do not have to be sent because of
2394 					 * the above reasons, will be
2395 					 * delayed. That is, acks that would
2396 					 * have been sent due to gap reports
2397 					 * will be delayed with DAC. Start
2398 					 * the delayed ack timer.
2399 					 */
2400 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2401 					    stcb->sctp_ep, stcb, NULL);
2402 				} else {
2403 					/*
2404 					 * Ok we must build a SACK since the
2405 					 * timer is pending, we got our
2406 					 * first packet OR there are gaps or
2407 					 * duplicates.
2408 					 */
2409 					(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2410 					sctp_send_sack(stcb);
2411 				}
2412 			} else {
2413 				if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2414 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2415 					    stcb->sctp_ep, stcb, NULL);
2416 				}
2417 			}
2418 		}
2419 	}
2420 }
2421 
2422 void
2423 sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2424 {
2425 	struct sctp_tmit_chunk *chk;
2426 	uint32_t tsize;
2427 	uint16_t nxt_todel;
2428 
2429 	if (asoc->fragmented_delivery_inprogress) {
2430 		sctp_service_reassembly(stcb, asoc);
2431 	}
2432 	/* Can we proceed further, i.e. the PD-API is complete */
2433 	if (asoc->fragmented_delivery_inprogress) {
2434 		/* no */
2435 		return;
2436 	}
2437 	/*
2438 	 * Now is there some other chunk I can deliver from the reassembly
2439 	 * queue.
2440 	 */
2441 doit_again:
2442 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2443 	if (chk == NULL) {
2444 		asoc->size_on_reasm_queue = 0;
2445 		asoc->cnt_on_reasm_queue = 0;
2446 		return;
2447 	}
2448 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2449 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2450 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2451 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2452 		/*
2453 		 * Yep the first one is here. We setup to start reception,
2454 		 * by backing down the TSN just in case we can't deliver.
2455 		 */
2456 
2457 		/*
2458 		 * Before we start though either all of the message should
2459 		 * be here or 1/4 the socket buffer max or nothing on the
2460 		 * delivery queue and something can be delivered.
2461 		 */
2462 		if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
2463 		    (tsize > stcb->sctp_ep->partial_delivery_point))) {
2464 			asoc->fragmented_delivery_inprogress = 1;
2465 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2466 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2467 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2468 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2469 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2470 			sctp_service_reassembly(stcb, asoc);
2471 			if (asoc->fragmented_delivery_inprogress == 0) {
2472 				goto doit_again;
2473 			}
2474 		}
2475 	}
2476 }
2477 
2478 int
2479 sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2480     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2481     struct sctp_nets *net, uint32_t * high_tsn)
2482 {
2483 	struct sctp_data_chunk *ch, chunk_buf;
2484 	struct sctp_association *asoc;
2485 	int num_chunks = 0;	/* number of control chunks processed */
2486 	int stop_proc = 0;
2487 	int chk_length, break_flag, last_chunk;
2488 	int abort_flag = 0, was_a_gap = 0;
2489 	struct mbuf *m;
2490 
2491 	/* set the rwnd */
2492 	sctp_set_rwnd(stcb, &stcb->asoc);
2493 
2494 	m = *mm;
2495 	SCTP_TCB_LOCK_ASSERT(stcb);
2496 	asoc = &stcb->asoc;
2497 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
2498 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
2499 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
2500 		/*
2501 		 * wait a minute, this guy is gone, there is no longer a
2502 		 * receiver. Send peer an ABORT!
2503 		 */
2504 		struct mbuf *op_err;
2505 
2506 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
2507 		sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err);
2508 		return (2);
2509 	}
2510 	if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2511 	    stcb->asoc.cumulative_tsn, MAX_TSN)) {
2512 		/* there was a gap before this data was processed */
2513 		was_a_gap = 1;
2514 	}
2515 	/*
2516 	 * setup where we got the last DATA packet from for any SACK that
2517 	 * may need to go out. Don't bump the net. This is done ONLY when a
2518 	 * chunk is assigned.
2519 	 */
2520 	asoc->last_data_chunk_from = net;
2521 
2522 	/*-
2523 	 * Now before we proceed we must figure out if this is a wasted
2524 	 * cluster... i.e. it is a small packet sent in and yet the driver
2525 	 * underneath allocated a full cluster for it. If so we must copy it
2526 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2527 	 * with cluster starvation. Note for __Panda__ we don't do this
2528 	 * since it has clusters all the way down to 64 bytes.
2529 	 */
2530 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2531 		/* we only handle mbufs that are singletons.. not chains */
2532 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2533 		if (m) {
2534 			/* ok lets see if we can copy the data up */
2535 			caddr_t *from, *to;
2536 
2537 			/* get the pointers and copy */
2538 			to = mtod(m, caddr_t *);
2539 			from = mtod((*mm), caddr_t *);
2540 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2541 			/* copy the length and free up the old */
2542 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2543 			sctp_m_freem(*mm);
2544 			/* sucess, back copy */
2545 			*mm = m;
2546 		} else {
2547 			/* We are in trouble in the mbuf world .. yikes */
2548 			m = *mm;
2549 		}
2550 	}
2551 	/* get pointer to the first chunk header */
2552 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2553 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2554 	if (ch == NULL) {
2555 		return (1);
2556 	}
2557 	/*
2558 	 * process all DATA chunks...
2559 	 */
2560 	*high_tsn = asoc->cumulative_tsn;
2561 	break_flag = 0;
2562 	asoc->data_pkts_seen++;
2563 	while (stop_proc == 0) {
2564 		/* validate chunk length */
2565 		chk_length = ntohs(ch->ch.chunk_length);
2566 		if (length - *offset < chk_length) {
2567 			/* all done, mutulated chunk */
2568 			stop_proc = 1;
2569 			break;
2570 		}
2571 		if (ch->ch.chunk_type == SCTP_DATA) {
2572 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2573 				/*
2574 				 * Need to send an abort since we had a
2575 				 * invalid data chunk.
2576 				 */
2577 				struct mbuf *op_err;
2578 
2579 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2580 				    0, M_DONTWAIT, 1, MT_DATA);
2581 
2582 				if (op_err) {
2583 					struct sctp_paramhdr *ph;
2584 					uint32_t *ippp;
2585 
2586 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2587 					    (2 * sizeof(uint32_t));
2588 					ph = mtod(op_err, struct sctp_paramhdr *);
2589 					ph->param_type =
2590 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2591 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
2592 					ippp = (uint32_t *) (ph + 1);
2593 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2594 					ippp++;
2595 					*ippp = asoc->cumulative_tsn;
2596 
2597 				}
2598 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2599 				sctp_abort_association(inp, stcb, m, iphlen, sh,
2600 				    op_err, 0, 0);
2601 				return (2);
2602 			}
2603 #ifdef SCTP_AUDITING_ENABLED
2604 			sctp_audit_log(0xB1, 0);
2605 #endif
2606 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2607 				last_chunk = 1;
2608 			} else {
2609 				last_chunk = 0;
2610 			}
2611 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2612 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
2613 			    last_chunk)) {
2614 				num_chunks++;
2615 			}
2616 			if (abort_flag)
2617 				return (2);
2618 
2619 			if (break_flag) {
2620 				/*
2621 				 * Set because of out of rwnd space and no
2622 				 * drop rep space left.
2623 				 */
2624 				stop_proc = 1;
2625 				break;
2626 			}
2627 		} else {
2628 			/* not a data chunk in the data region */
2629 			switch (ch->ch.chunk_type) {
2630 			case SCTP_INITIATION:
2631 			case SCTP_INITIATION_ACK:
2632 			case SCTP_SELECTIVE_ACK:
2633 			case SCTP_HEARTBEAT_REQUEST:
2634 			case SCTP_HEARTBEAT_ACK:
2635 			case SCTP_ABORT_ASSOCIATION:
2636 			case SCTP_SHUTDOWN:
2637 			case SCTP_SHUTDOWN_ACK:
2638 			case SCTP_OPERATION_ERROR:
2639 			case SCTP_COOKIE_ECHO:
2640 			case SCTP_COOKIE_ACK:
2641 			case SCTP_ECN_ECHO:
2642 			case SCTP_ECN_CWR:
2643 			case SCTP_SHUTDOWN_COMPLETE:
2644 			case SCTP_AUTHENTICATION:
2645 			case SCTP_ASCONF_ACK:
2646 			case SCTP_PACKET_DROPPED:
2647 			case SCTP_STREAM_RESET:
2648 			case SCTP_FORWARD_CUM_TSN:
2649 			case SCTP_ASCONF:
2650 				/*
2651 				 * Now, what do we do with KNOWN chunks that
2652 				 * are NOT in the right place?
2653 				 *
2654 				 * For now, I do nothing but ignore them. We
2655 				 * may later want to add sysctl stuff to
2656 				 * switch out and do either an ABORT() or
2657 				 * possibly process them.
2658 				 */
2659 				if (sctp_strict_data_order) {
2660 					struct mbuf *op_err;
2661 
2662 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
2663 					sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0, 0);
2664 					return (2);
2665 				}
2666 				break;
2667 			default:
2668 				/* unknown chunk type, use bit rules */
2669 				if (ch->ch.chunk_type & 0x40) {
2670 					/* Add a error report to the queue */
2671 					struct mbuf *mm;
2672 					struct sctp_paramhdr *phd;
2673 
2674 					mm = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
2675 					if (mm) {
2676 						phd = mtod(mm, struct sctp_paramhdr *);
2677 						/*
2678 						 * We cheat and use param
2679 						 * type since we did not
2680 						 * bother to define a error
2681 						 * cause struct. They are
2682 						 * the same basic format
2683 						 * with different names.
2684 						 */
2685 						phd->param_type =
2686 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
2687 						phd->param_length =
2688 						    htons(chk_length + sizeof(*phd));
2689 						SCTP_BUF_LEN(mm) = sizeof(*phd);
2690 						SCTP_BUF_NEXT(mm) = SCTP_M_COPYM(m, *offset,
2691 						    SCTP_SIZE32(chk_length),
2692 						    M_DONTWAIT);
2693 						if (SCTP_BUF_NEXT(mm)) {
2694 							sctp_queue_op_err(stcb, mm);
2695 						} else {
2696 							sctp_m_freem(mm);
2697 						}
2698 					}
2699 				}
2700 				if ((ch->ch.chunk_type & 0x80) == 0) {
2701 					/* discard the rest of this packet */
2702 					stop_proc = 1;
2703 				}	/* else skip this bad chunk and
2704 					 * continue... */
2705 				break;
2706 			};	/* switch of chunk type */
2707 		}
2708 		*offset += SCTP_SIZE32(chk_length);
2709 		if ((*offset >= length) || stop_proc) {
2710 			/* no more data left in the mbuf chain */
2711 			stop_proc = 1;
2712 			continue;
2713 		}
2714 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2715 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2716 		if (ch == NULL) {
2717 			*offset = length;
2718 			stop_proc = 1;
2719 			break;
2720 
2721 		}
2722 	}			/* while */
2723 	if (break_flag) {
2724 		/*
2725 		 * we need to report rwnd overrun drops.
2726 		 */
2727 		sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
2728 	}
2729 	if (num_chunks) {
2730 		/*
2731 		 * Did we get data, if so update the time for auto-close and
2732 		 * give peer credit for being alive.
2733 		 */
2734 		SCTP_STAT_INCR(sctps_recvpktwithdata);
2735 		stcb->asoc.overall_error_count = 0;
2736 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2737 	}
2738 	/* now service all of the reassm queue if needed */
2739 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2740 		sctp_service_queues(stcb, asoc);
2741 
2742 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2743 		/* Assure that we ack right away */
2744 		stcb->asoc.send_sack = 1;
2745 	}
2746 	/* Start a sack timer or QUEUE a SACK for sending */
2747 	if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
2748 	    (stcb->asoc.mapping_array[0] != 0xff)) {
2749 		if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
2750 		    (stcb->asoc.delayed_ack == 0) ||
2751 		    (stcb->asoc.send_sack == 1)) {
2752 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2753 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2754 			}
2755 			sctp_send_sack(stcb);
2756 		} else {
2757 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2758 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2759 				    stcb->sctp_ep, stcb, NULL);
2760 			}
2761 		}
2762 	} else {
2763 		sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
2764 	}
2765 	if (abort_flag)
2766 		return (2);
2767 
2768 	return (0);
2769 }
2770 
2771 static void
2772 sctp_handle_segments(struct sctp_tcb *stcb, struct sctp_association *asoc,
2773     struct sctp_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
2774     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
2775     int num_seg, int *ecn_seg_sums)
2776 {
2777 	/************************************************/
2778 	/* process fragments and update sendqueue        */
2779 	/************************************************/
2780 	struct sctp_sack *sack;
2781 	struct sctp_gap_ack_block *frag;
2782 	struct sctp_tmit_chunk *tp1;
2783 	int i;
2784 	unsigned int j;
2785 
2786 #ifdef SCTP_FR_LOGGING
2787 	int num_frs = 0;
2788 
2789 #endif
2790 	uint16_t frag_strt, frag_end, primary_flag_set;
2791 	u_long last_frag_high;
2792 
2793 	/*
2794 	 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
2795 	 */
2796 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
2797 		primary_flag_set = 1;
2798 	} else {
2799 		primary_flag_set = 0;
2800 	}
2801 
2802 	sack = &ch->sack;
2803 	frag = (struct sctp_gap_ack_block *)((caddr_t)sack +
2804 	    sizeof(struct sctp_sack));
2805 	tp1 = NULL;
2806 	last_frag_high = 0;
2807 	for (i = 0; i < num_seg; i++) {
2808 		frag_strt = ntohs(frag->start);
2809 		frag_end = ntohs(frag->end);
2810 		/* some sanity checks on the fargment offsets */
2811 		if (frag_strt > frag_end) {
2812 			/* this one is malformed, skip */
2813 			frag++;
2814 			continue;
2815 		}
2816 		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
2817 		    MAX_TSN))
2818 			*biggest_tsn_acked = frag_end + last_tsn;
2819 
2820 		/* mark acked dgs and find out the highestTSN being acked */
2821 		if (tp1 == NULL) {
2822 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
2823 
2824 			/* save the locations of the last frags */
2825 			last_frag_high = frag_end + last_tsn;
2826 		} else {
2827 			/*
2828 			 * now lets see if we need to reset the queue due to
2829 			 * a out-of-order SACK fragment
2830 			 */
2831 			if (compare_with_wrap(frag_strt + last_tsn,
2832 			    last_frag_high, MAX_TSN)) {
2833 				/*
2834 				 * if the new frag starts after the last TSN
2835 				 * frag covered, we are ok and this one is
2836 				 * beyond the last one
2837 				 */
2838 				;
2839 			} else {
2840 				/*
2841 				 * ok, they have reset us, so we need to
2842 				 * reset the queue this will cause extra
2843 				 * hunting but hey, they chose the
2844 				 * performance hit when they failed to order
2845 				 * there gaps..
2846 				 */
2847 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
2848 			}
2849 			last_frag_high = frag_end + last_tsn;
2850 		}
2851 		for (j = frag_strt + last_tsn; j <= frag_end + last_tsn; j++) {
2852 			while (tp1) {
2853 #ifdef SCTP_FR_LOGGING
2854 				if (tp1->rec.data.doing_fast_retransmit)
2855 					num_frs++;
2856 #endif
2857 
2858 				/*
2859 				 * CMT: CUCv2 algorithm. For each TSN being
2860 				 * processed from the sent queue, track the
2861 				 * next expected pseudo-cumack, or
2862 				 * rtx_pseudo_cumack, if required. Separate
2863 				 * cumack trackers for first transmissions,
2864 				 * and retransmissions.
2865 				 */
2866 				if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2867 				    (tp1->snd_count == 1)) {
2868 					tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
2869 					tp1->whoTo->find_pseudo_cumack = 0;
2870 				}
2871 				if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2872 				    (tp1->snd_count > 1)) {
2873 					tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
2874 					tp1->whoTo->find_rtx_pseudo_cumack = 0;
2875 				}
2876 				if (tp1->rec.data.TSN_seq == j) {
2877 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
2878 						/*
2879 						 * must be held until
2880 						 * cum-ack passes
2881 						 */
2882 						/*
2883 						 * ECN Nonce: Add the nonce
2884 						 * value to the sender's
2885 						 * nonce sum
2886 						 */
2887 						if (tp1->sent < SCTP_DATAGRAM_RESEND) {
2888 							/*-
2889 							 * If it is less than RESEND, it is
2890 							 * now no-longer in flight.
2891 							 * Higher values may already be set
2892 							 * via previous Gap Ack Blocks...
2893 							 * i.e. ACKED or RESEND.
2894 							 */
2895 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
2896 							    *biggest_newly_acked_tsn, MAX_TSN)) {
2897 								*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
2898 							}
2899 							/*
2900 							 * CMT: SFR algo
2901 							 * (and HTNA) - set
2902 							 * saw_newack to 1
2903 							 * for dest being
2904 							 * newly acked.
2905 							 * update
2906 							 * this_sack_highest_
2907 							 * newack if
2908 							 * appropriate.
2909 							 */
2910 							if (tp1->rec.data.chunk_was_revoked == 0)
2911 								tp1->whoTo->saw_newack = 1;
2912 
2913 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
2914 							    tp1->whoTo->this_sack_highest_newack,
2915 							    MAX_TSN)) {
2916 								tp1->whoTo->this_sack_highest_newack =
2917 								    tp1->rec.data.TSN_seq;
2918 							}
2919 							/*
2920 							 * CMT DAC algo:
2921 							 * also update
2922 							 * this_sack_lowest_n
2923 							 * ewack
2924 							 */
2925 							if (*this_sack_lowest_newack == 0) {
2926 #ifdef SCTP_SACK_LOGGING
2927 								sctp_log_sack(*this_sack_lowest_newack,
2928 								    last_tsn,
2929 								    tp1->rec.data.TSN_seq,
2930 								    0,
2931 								    0,
2932 								    SCTP_LOG_TSN_ACKED);
2933 #endif
2934 								*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
2935 							}
2936 							/*
2937 							 * CMT: CUCv2
2938 							 * algorithm. If
2939 							 * (rtx-)pseudo-cumac
2940 							 * k for corresp
2941 							 * dest is being
2942 							 * acked, then we
2943 							 * have a new
2944 							 * (rtx-)pseudo-cumac
2945 							 * k. Set
2946 							 * new_(rtx_)pseudo_c
2947 							 * umack to TRUE so
2948 							 * that the cwnd for
2949 							 * this dest can be
2950 							 * updated. Also
2951 							 * trigger search
2952 							 * for the next
2953 							 * expected
2954 							 * (rtx-)pseudo-cumac
2955 							 * k. Separate
2956 							 * pseudo_cumack
2957 							 * trackers for
2958 							 * first
2959 							 * transmissions and
2960 							 * retransmissions.
2961 							 */
2962 							if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
2963 								if (tp1->rec.data.chunk_was_revoked == 0) {
2964 									tp1->whoTo->new_pseudo_cumack = 1;
2965 								}
2966 								tp1->whoTo->find_pseudo_cumack = 1;
2967 							}
2968 #ifdef SCTP_CWND_LOGGING
2969 							sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
2970 #endif
2971 							if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
2972 								if (tp1->rec.data.chunk_was_revoked == 0) {
2973 									tp1->whoTo->new_pseudo_cumack = 1;
2974 								}
2975 								tp1->whoTo->find_rtx_pseudo_cumack = 1;
2976 							}
2977 #ifdef SCTP_SACK_LOGGING
2978 							sctp_log_sack(*biggest_newly_acked_tsn,
2979 							    last_tsn,
2980 							    tp1->rec.data.TSN_seq,
2981 							    frag_strt,
2982 							    frag_end,
2983 							    SCTP_LOG_TSN_ACKED);
2984 #endif
2985 #ifdef SCTP_FLIGHT_LOGGING
2986 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
2987 							    tp1->whoTo->flight_size,
2988 							    tp1->book_size,
2989 							    (uintptr_t) tp1->whoTo,
2990 							    tp1->rec.data.TSN_seq);
2991 #endif
2992 							sctp_flight_size_decrease(tp1);
2993 							sctp_total_flight_decrease(stcb, tp1);
2994 
2995 							tp1->whoTo->net_ack += tp1->send_size;
2996 							if (tp1->snd_count < 2) {
2997 								/*
2998 								 * True
2999 								 * non-retran
3000 								 * smited
3001 								 * chunk */
3002 								tp1->whoTo->net_ack2 += tp1->send_size;
3003 
3004 								/*
3005 								 * update RTO
3006 								 * too ? */
3007 								if (tp1->do_rtt) {
3008 									tp1->whoTo->RTO =
3009 									    sctp_calculate_rto(stcb,
3010 									    asoc,
3011 									    tp1->whoTo,
3012 									    &tp1->sent_rcv_time);
3013 									tp1->do_rtt = 0;
3014 								}
3015 							}
3016 						}
3017 						if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3018 							(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
3019 							(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
3020 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3021 							    asoc->this_sack_highest_gap,
3022 							    MAX_TSN)) {
3023 								asoc->this_sack_highest_gap =
3024 								    tp1->rec.data.TSN_seq;
3025 							}
3026 							if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3027 								sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3028 #ifdef SCTP_AUDITING_ENABLED
3029 								sctp_audit_log(0xB2,
3030 								    (asoc->sent_queue_retran_cnt & 0x000000ff));
3031 #endif
3032 							}
3033 						}
3034 						/*
3035 						 * All chunks NOT UNSENT
3036 						 * fall through here and are
3037 						 * marked
3038 						 */
3039 						tp1->sent = SCTP_DATAGRAM_MARKED;
3040 						if (tp1->rec.data.chunk_was_revoked) {
3041 							/* deflate the cwnd */
3042 							tp1->whoTo->cwnd -= tp1->book_size;
3043 							tp1->rec.data.chunk_was_revoked = 0;
3044 						}
3045 					}
3046 					break;
3047 				}	/* if (tp1->TSN_seq == j) */
3048 				if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
3049 				    MAX_TSN))
3050 					break;
3051 
3052 				tp1 = TAILQ_NEXT(tp1, sctp_next);
3053 			}	/* end while (tp1) */
3054 		}		/* end for (j = fragStart */
3055 		frag++;		/* next one */
3056 	}
3057 #ifdef SCTP_FR_LOGGING
3058 	/*
3059 	 * if (num_frs) sctp_log_fr(*biggest_tsn_acked,
3060 	 * *biggest_newly_acked_tsn, last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
3061 	 */
3062 #endif
3063 }
3064 
3065 static void
3066 sctp_check_for_revoked(struct sctp_tcb *stcb,
3067     struct sctp_association *asoc, uint32_t cumack,
3068     u_long biggest_tsn_acked)
3069 {
3070 	struct sctp_tmit_chunk *tp1;
3071 	int tot_revoked = 0;
3072 
3073 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3074 	while (tp1) {
3075 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3076 		    MAX_TSN)) {
3077 			/*
3078 			 * ok this guy is either ACK or MARKED. If it is
3079 			 * ACKED it has been previously acked but not this
3080 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3081 			 * again.
3082 			 */
3083 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3084 			    MAX_TSN))
3085 				break;
3086 
3087 
3088 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3089 				/* it has been revoked */
3090 				tp1->sent = SCTP_DATAGRAM_SENT;
3091 				tp1->rec.data.chunk_was_revoked = 1;
3092 				/*
3093 				 * We must add this stuff back in to assure
3094 				 * timers and such get started.
3095 				 */
3096 #ifdef SCTP_FLIGHT_LOGGING
3097 				sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3098 				    tp1->whoTo->flight_size,
3099 				    tp1->book_size,
3100 				    (uintptr_t) tp1->whoTo,
3101 				    tp1->rec.data.TSN_seq);
3102 #endif
3103 				sctp_flight_size_increase(tp1);
3104 				sctp_total_flight_increase(stcb, tp1);
3105 				/*
3106 				 * We inflate the cwnd to compensate for our
3107 				 * artificial inflation of the flight_size.
3108 				 */
3109 				tp1->whoTo->cwnd += tp1->book_size;
3110 				tot_revoked++;
3111 #ifdef SCTP_SACK_LOGGING
3112 				sctp_log_sack(asoc->last_acked_seq,
3113 				    cumack,
3114 				    tp1->rec.data.TSN_seq,
3115 				    0,
3116 				    0,
3117 				    SCTP_LOG_TSN_REVOKED);
3118 #endif
3119 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3120 				/* it has been re-acked in this SACK */
3121 				tp1->sent = SCTP_DATAGRAM_ACKED;
3122 			}
3123 		}
3124 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3125 			break;
3126 		tp1 = TAILQ_NEXT(tp1, sctp_next);
3127 	}
3128 	if (tot_revoked > 0) {
3129 		/*
3130 		 * Setup the ecn nonce re-sync point. We do this since once
3131 		 * data is revoked we begin to retransmit things, which do
3132 		 * NOT have the ECN bits set. This means we are now out of
3133 		 * sync and must wait until we get back in sync with the
3134 		 * peer to check ECN bits.
3135 		 */
3136 		tp1 = TAILQ_FIRST(&asoc->send_queue);
3137 		if (tp1 == NULL) {
3138 			asoc->nonce_resync_tsn = asoc->sending_seq;
3139 		} else {
3140 			asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3141 		}
3142 		asoc->nonce_wait_for_ecne = 0;
3143 		asoc->nonce_sum_check = 0;
3144 	}
3145 }
3146 
3147 static void
3148 sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3149     u_long biggest_tsn_acked, u_long biggest_tsn_newly_acked, u_long this_sack_lowest_newack, int accum_moved)
3150 {
3151 	struct sctp_tmit_chunk *tp1;
3152 	int strike_flag = 0;
3153 	struct timeval now;
3154 	int tot_retrans = 0;
3155 	uint32_t sending_seq;
3156 	struct sctp_nets *net;
3157 	int num_dests_sacked = 0;
3158 
3159 	/*
3160 	 * select the sending_seq, this is either the next thing ready to be
3161 	 * sent but not transmitted, OR, the next seq we assign.
3162 	 */
3163 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3164 	if (tp1 == NULL) {
3165 		sending_seq = asoc->sending_seq;
3166 	} else {
3167 		sending_seq = tp1->rec.data.TSN_seq;
3168 	}
3169 
3170 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
3171 	if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3172 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3173 			if (net->saw_newack)
3174 				num_dests_sacked++;
3175 		}
3176 	}
3177 	if (stcb->asoc.peer_supports_prsctp) {
3178 		(void)SCTP_GETTIME_TIMEVAL(&now);
3179 	}
3180 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3181 	while (tp1) {
3182 		strike_flag = 0;
3183 		if (tp1->no_fr_allowed) {
3184 			/* this one had a timeout or something */
3185 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3186 			continue;
3187 		}
3188 #ifdef SCTP_FR_LOGGING
3189 		if (tp1->sent < SCTP_DATAGRAM_RESEND)
3190 			sctp_log_fr(biggest_tsn_newly_acked,
3191 			    tp1->rec.data.TSN_seq,
3192 			    tp1->sent,
3193 			    SCTP_FR_LOG_CHECK_STRIKE);
3194 #endif
3195 		if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3196 		    MAX_TSN) ||
3197 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3198 			/* done */
3199 			break;
3200 		}
3201 		if (stcb->asoc.peer_supports_prsctp) {
3202 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3203 				/* Is it expired? */
3204 				if (
3205 				    (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
3206 				    ) {
3207 					/* Yes so drop it */
3208 					if (tp1->data != NULL) {
3209 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3210 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3211 						    &asoc->sent_queue);
3212 					}
3213 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3214 					continue;
3215 				}
3216 			}
3217 			if ((PR_SCTP_RTX_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3218 				/* Has it been retransmitted tv_sec times? */
3219 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3220 					/* Yes, so drop it */
3221 					if (tp1->data != NULL) {
3222 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3223 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3224 						    &asoc->sent_queue);
3225 					}
3226 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3227 					continue;
3228 				}
3229 			}
3230 		}
3231 		if (compare_with_wrap(tp1->rec.data.TSN_seq,
3232 		    asoc->this_sack_highest_gap, MAX_TSN)) {
3233 			/* we are beyond the tsn in the sack  */
3234 			break;
3235 		}
3236 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3237 			/* either a RESEND, ACKED, or MARKED */
3238 			/* skip */
3239 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3240 			continue;
3241 		}
3242 		/*
3243 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3244 		 */
3245 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3246 			/*
3247 			 * No new acks were receieved for data sent to this
3248 			 * dest. Therefore, according to the SFR algo for
3249 			 * CMT, no data sent to this dest can be marked for
3250 			 * FR using this SACK.
3251 			 */
3252 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3253 			continue;
3254 		} else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3255 		    tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3256 			/*
3257 			 * CMT: New acks were receieved for data sent to
3258 			 * this dest. But no new acks were seen for data
3259 			 * sent after tp1. Therefore, according to the SFR
3260 			 * algo for CMT, tp1 cannot be marked for FR using
3261 			 * this SACK. This step covers part of the DAC algo
3262 			 * and the HTNA algo as well.
3263 			 */
3264 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3265 			continue;
3266 		}
3267 		/*
3268 		 * Here we check to see if we were have already done a FR
3269 		 * and if so we see if the biggest TSN we saw in the sack is
3270 		 * smaller than the recovery point. If so we don't strike
3271 		 * the tsn... otherwise we CAN strike the TSN.
3272 		 */
3273 		/*
3274 		 * @@@ JRI: Check for CMT if (accum_moved &&
3275 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
3276 		 * 0)) {
3277 		 */
3278 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3279 			/*
3280 			 * Strike the TSN if in fast-recovery and cum-ack
3281 			 * moved.
3282 			 */
3283 #ifdef SCTP_FR_LOGGING
3284 			sctp_log_fr(biggest_tsn_newly_acked,
3285 			    tp1->rec.data.TSN_seq,
3286 			    tp1->sent,
3287 			    SCTP_FR_LOG_STRIKE_CHUNK);
3288 #endif
3289 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3290 				tp1->sent++;
3291 			}
3292 			if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3293 				/*
3294 				 * CMT DAC algorithm: If SACK flag is set to
3295 				 * 0, then lowest_newack test will not pass
3296 				 * because it would have been set to the
3297 				 * cumack earlier. If not already to be
3298 				 * rtx'd, If not a mixed sack and if tp1 is
3299 				 * not between two sacked TSNs, then mark by
3300 				 * one more. NOTE that we are marking by one
3301 				 * additional time since the SACK DAC flag
3302 				 * indicates that two packets have been
3303 				 * received after this missing TSN.
3304 				 */
3305 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3306 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3307 #ifdef SCTP_FR_LOGGING
3308 					sctp_log_fr(16 + num_dests_sacked,
3309 					    tp1->rec.data.TSN_seq,
3310 					    tp1->sent,
3311 					    SCTP_FR_LOG_STRIKE_CHUNK);
3312 #endif
3313 					tp1->sent++;
3314 				}
3315 			}
3316 		} else if (tp1->rec.data.doing_fast_retransmit) {
3317 			/*
3318 			 * For those that have done a FR we must take
3319 			 * special consideration if we strike. I.e the
3320 			 * biggest_newly_acked must be higher than the
3321 			 * sending_seq at the time we did the FR.
3322 			 */
3323 			if (
3324 #ifdef SCTP_FR_TO_ALTERNATE
3325 			/*
3326 			 * If FR's go to new networks, then we must only do
3327 			 * this for singly homed asoc's. However if the FR's
3328 			 * go to the same network (Armando's work) then its
3329 			 * ok to FR multiple times.
3330 			 */
3331 			    (asoc->numnets < 2)
3332 #else
3333 			    (1)
3334 #endif
3335 			    ) {
3336 
3337 				if ((compare_with_wrap(biggest_tsn_newly_acked,
3338 				    tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3339 				    (biggest_tsn_newly_acked ==
3340 				    tp1->rec.data.fast_retran_tsn)) {
3341 					/*
3342 					 * Strike the TSN, since this ack is
3343 					 * beyond where things were when we
3344 					 * did a FR.
3345 					 */
3346 #ifdef SCTP_FR_LOGGING
3347 					sctp_log_fr(biggest_tsn_newly_acked,
3348 					    tp1->rec.data.TSN_seq,
3349 					    tp1->sent,
3350 					    SCTP_FR_LOG_STRIKE_CHUNK);
3351 #endif
3352 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3353 						tp1->sent++;
3354 					}
3355 					strike_flag = 1;
3356 					if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3357 						/*
3358 						 * CMT DAC algorithm: If
3359 						 * SACK flag is set to 0,
3360 						 * then lowest_newack test
3361 						 * will not pass because it
3362 						 * would have been set to
3363 						 * the cumack earlier. If
3364 						 * not already to be rtx'd,
3365 						 * If not a mixed sack and
3366 						 * if tp1 is not between two
3367 						 * sacked TSNs, then mark by
3368 						 * one more. NOTE that we
3369 						 * are marking by one
3370 						 * additional time since the
3371 						 * SACK DAC flag indicates
3372 						 * that two packets have
3373 						 * been received after this
3374 						 * missing TSN.
3375 						 */
3376 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3377 						    (num_dests_sacked == 1) &&
3378 						    compare_with_wrap(this_sack_lowest_newack,
3379 						    tp1->rec.data.TSN_seq, MAX_TSN)) {
3380 #ifdef SCTP_FR_LOGGING
3381 							sctp_log_fr(32 + num_dests_sacked,
3382 							    tp1->rec.data.TSN_seq,
3383 							    tp1->sent,
3384 							    SCTP_FR_LOG_STRIKE_CHUNK);
3385 #endif
3386 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3387 								tp1->sent++;
3388 
3389 							}
3390 						}
3391 					}
3392 				}
3393 			}
3394 			/*
3395 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
3396 			 * algo covers HTNA.
3397 			 */
3398 		} else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3399 		    biggest_tsn_newly_acked, MAX_TSN)) {
3400 			/*
3401 			 * We don't strike these: This is the  HTNA
3402 			 * algorithm i.e. we don't strike If our TSN is
3403 			 * larger than the Highest TSN Newly Acked.
3404 			 */
3405 			;
3406 		} else {
3407 			/* Strike the TSN */
3408 #ifdef SCTP_FR_LOGGING
3409 			sctp_log_fr(biggest_tsn_newly_acked,
3410 			    tp1->rec.data.TSN_seq,
3411 			    tp1->sent,
3412 			    SCTP_FR_LOG_STRIKE_CHUNK);
3413 #endif
3414 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3415 				tp1->sent++;
3416 			}
3417 			if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3418 				/*
3419 				 * CMT DAC algorithm: If SACK flag is set to
3420 				 * 0, then lowest_newack test will not pass
3421 				 * because it would have been set to the
3422 				 * cumack earlier. If not already to be
3423 				 * rtx'd, If not a mixed sack and if tp1 is
3424 				 * not between two sacked TSNs, then mark by
3425 				 * one more. NOTE that we are marking by one
3426 				 * additional time since the SACK DAC flag
3427 				 * indicates that two packets have been
3428 				 * received after this missing TSN.
3429 				 */
3430 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3431 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3432 #ifdef SCTP_FR_LOGGING
3433 					sctp_log_fr(48 + num_dests_sacked,
3434 					    tp1->rec.data.TSN_seq,
3435 					    tp1->sent,
3436 					    SCTP_FR_LOG_STRIKE_CHUNK);
3437 #endif
3438 					tp1->sent++;
3439 				}
3440 			}
3441 		}
3442 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3443 			/* Increment the count to resend */
3444 			struct sctp_nets *alt;
3445 
3446 			/* printf("OK, we are now ready to FR this guy\n"); */
3447 #ifdef SCTP_FR_LOGGING
3448 			sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3449 			    0, SCTP_FR_MARKED);
3450 #endif
3451 			if (strike_flag) {
3452 				/* This is a subsequent FR */
3453 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3454 			}
3455 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3456 			if (sctp_cmt_on_off) {
3457 				/*
3458 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3459 				 * If CMT is being used, then pick dest with
3460 				 * largest ssthresh for any retransmission.
3461 				 */
3462 				tp1->no_fr_allowed = 1;
3463 				alt = tp1->whoTo;
3464 				/* sa_ignore NO_NULL_CHK */
3465 				alt = sctp_find_alternate_net(stcb, alt, 1);
3466 				if (alt == NULL) {
3467 					alt = tp1->whoTo;
3468 				}
3469 				/*
3470 				 * CUCv2: If a different dest is picked for
3471 				 * the retransmission, then new
3472 				 * (rtx-)pseudo_cumack needs to be tracked
3473 				 * for orig dest. Let CUCv2 track new (rtx-)
3474 				 * pseudo-cumack always.
3475 				 */
3476 				if (tp1->whoTo) {
3477 					tp1->whoTo->find_pseudo_cumack = 1;
3478 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3479 				}
3480 			} else {/* CMT is OFF */
3481 
3482 #ifdef SCTP_FR_TO_ALTERNATE
3483 				/* Can we find an alternate? */
3484 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3485 #else
3486 				/*
3487 				 * default behavior is to NOT retransmit
3488 				 * FR's to an alternate. Armando Caro's
3489 				 * paper details why.
3490 				 */
3491 				alt = tp1->whoTo;
3492 #endif
3493 			}
3494 
3495 			tp1->rec.data.doing_fast_retransmit = 1;
3496 			tot_retrans++;
3497 			/* mark the sending seq for possible subsequent FR's */
3498 			/*
3499 			 * printf("Marking TSN for FR new value %x\n",
3500 			 * (uint32_t)tpi->rec.data.TSN_seq);
3501 			 */
3502 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3503 				/*
3504 				 * If the queue of send is empty then its
3505 				 * the next sequence number that will be
3506 				 * assigned so we subtract one from this to
3507 				 * get the one we last sent.
3508 				 */
3509 				tp1->rec.data.fast_retran_tsn = sending_seq;
3510 			} else {
3511 				/*
3512 				 * If there are chunks on the send queue
3513 				 * (unsent data that has made it from the
3514 				 * stream queues but not out the door, we
3515 				 * take the first one (which will have the
3516 				 * lowest TSN) and subtract one to get the
3517 				 * one we last sent.
3518 				 */
3519 				struct sctp_tmit_chunk *ttt;
3520 
3521 				ttt = TAILQ_FIRST(&asoc->send_queue);
3522 				tp1->rec.data.fast_retran_tsn =
3523 				    ttt->rec.data.TSN_seq;
3524 			}
3525 
3526 			if (tp1->do_rtt) {
3527 				/*
3528 				 * this guy had a RTO calculation pending on
3529 				 * it, cancel it
3530 				 */
3531 				tp1->do_rtt = 0;
3532 			}
3533 			/* fix counts and things */
3534 #ifdef SCTP_FLIGHT_LOGGING
3535 			sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3536 			    tp1->whoTo->flight_size,
3537 			    tp1->book_size,
3538 			    (uintptr_t) tp1->whoTo,
3539 			    tp1->rec.data.TSN_seq);
3540 #endif
3541 			if (tp1->whoTo) {
3542 				tp1->whoTo->net_ack++;
3543 				sctp_flight_size_decrease(tp1);
3544 			}
3545 #ifdef SCTP_LOG_RWND
3546 			sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3547 			    asoc->peers_rwnd, tp1->send_size, sctp_peer_chunk_oh);
3548 #endif
3549 			/* add back to the rwnd */
3550 			asoc->peers_rwnd += (tp1->send_size + sctp_peer_chunk_oh);
3551 
3552 			/* remove from the total flight */
3553 			sctp_total_flight_decrease(stcb, tp1);
3554 			if (alt != tp1->whoTo) {
3555 				/* yes, there is an alternate. */
3556 				sctp_free_remote_addr(tp1->whoTo);
3557 				/* sa_ignore FREED_MEMORY */
3558 				tp1->whoTo = alt;
3559 				atomic_add_int(&alt->ref_count, 1);
3560 			}
3561 		}
3562 		tp1 = TAILQ_NEXT(tp1, sctp_next);
3563 	}			/* while (tp1) */
3564 
3565 	if (tot_retrans > 0) {
3566 		/*
3567 		 * Setup the ecn nonce re-sync point. We do this since once
3568 		 * we go to FR something we introduce a Karn's rule scenario
3569 		 * and won't know the totals for the ECN bits.
3570 		 */
3571 		asoc->nonce_resync_tsn = sending_seq;
3572 		asoc->nonce_wait_for_ecne = 0;
3573 		asoc->nonce_sum_check = 0;
3574 	}
3575 }
3576 
3577 struct sctp_tmit_chunk *
3578 sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3579     struct sctp_association *asoc)
3580 {
3581 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3582 	struct timeval now;
3583 	int now_filled = 0;
3584 
3585 	if (asoc->peer_supports_prsctp == 0) {
3586 		return (NULL);
3587 	}
3588 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3589 	while (tp1) {
3590 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3591 		    tp1->sent != SCTP_DATAGRAM_RESEND) {
3592 			/* no chance to advance, out of here */
3593 			break;
3594 		}
3595 		if (!PR_SCTP_ENABLED(tp1->flags)) {
3596 			/*
3597 			 * We can't fwd-tsn past any that are reliable aka
3598 			 * retransmitted until the asoc fails.
3599 			 */
3600 			break;
3601 		}
3602 		if (!now_filled) {
3603 			(void)SCTP_GETTIME_TIMEVAL(&now);
3604 			now_filled = 1;
3605 		}
3606 		tp2 = TAILQ_NEXT(tp1, sctp_next);
3607 		/*
3608 		 * now we got a chunk which is marked for another
3609 		 * retransmission to a PR-stream but has run out its chances
3610 		 * already maybe OR has been marked to skip now. Can we skip
3611 		 * it if its a resend?
3612 		 */
3613 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3614 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3615 			/*
3616 			 * Now is this one marked for resend and its time is
3617 			 * now up?
3618 			 */
3619 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3620 				/* Yes so drop it */
3621 				if (tp1->data) {
3622 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3623 					    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3624 					    &asoc->sent_queue);
3625 				}
3626 			} else {
3627 				/*
3628 				 * No, we are done when hit one for resend
3629 				 * whos time as not expired.
3630 				 */
3631 				break;
3632 			}
3633 		}
3634 		/*
3635 		 * Ok now if this chunk is marked to drop it we can clean up
3636 		 * the chunk, advance our peer ack point and we can check
3637 		 * the next chunk.
3638 		 */
3639 		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3640 			/* advance PeerAckPoint goes forward */
3641 			asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3642 			a_adv = tp1;
3643 			/*
3644 			 * we don't want to de-queue it here. Just wait for
3645 			 * the next peer SACK to come with a new cumTSN and
3646 			 * then the chunk will be droped in the normal
3647 			 * fashion.
3648 			 */
3649 			if (tp1->data) {
3650 				sctp_free_bufspace(stcb, asoc, tp1, 1);
3651 				/*
3652 				 * Maybe there should be another
3653 				 * notification type
3654 				 */
3655 				sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3656 				    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3657 				    tp1);
3658 				sctp_m_freem(tp1->data);
3659 				tp1->data = NULL;
3660 				if (stcb->sctp_socket) {
3661 					sctp_sowwakeup(stcb->sctp_ep,
3662 					    stcb->sctp_socket);
3663 #ifdef SCTP_WAKE_LOGGING
3664 					sctp_wakeup_log(stcb, tp1->rec.data.TSN_seq, 1, SCTP_WAKESND_FROM_FWDTSN);
3665 #endif
3666 				}
3667 			}
3668 		} else {
3669 			/*
3670 			 * If it is still in RESEND we can advance no
3671 			 * further
3672 			 */
3673 			break;
3674 		}
3675 		/*
3676 		 * If we hit here we just dumped tp1, move to next tsn on
3677 		 * sent queue.
3678 		 */
3679 		tp1 = tp2;
3680 	}
3681 	return (a_adv);
3682 }
3683 
3684 #ifdef SCTP_HIGH_SPEED
3685 struct sctp_hs_raise_drop {
3686 	int32_t cwnd;
3687 	int32_t increase;
3688 	int32_t drop_percent;
3689 };
3690 
3691 #define SCTP_HS_TABLE_SIZE 73
3692 
3693 struct sctp_hs_raise_drop sctp_cwnd_adjust[SCTP_HS_TABLE_SIZE] = {
3694 	{38, 1, 50},		/* 0   */
3695 	{118, 2, 44},		/* 1   */
3696 	{221, 3, 41},		/* 2   */
3697 	{347, 4, 38},		/* 3   */
3698 	{495, 5, 37},		/* 4   */
3699 	{663, 6, 35},		/* 5   */
3700 	{851, 7, 34},		/* 6   */
3701 	{1058, 8, 33},		/* 7   */
3702 	{1284, 9, 32},		/* 8   */
3703 	{1529, 10, 31},		/* 9   */
3704 	{1793, 11, 30},		/* 10  */
3705 	{2076, 12, 29},		/* 11  */
3706 	{2378, 13, 28},		/* 12  */
3707 	{2699, 14, 28},		/* 13  */
3708 	{3039, 15, 27},		/* 14  */
3709 	{3399, 16, 27},		/* 15  */
3710 	{3778, 17, 26},		/* 16  */
3711 	{4177, 18, 26},		/* 17  */
3712 	{4596, 19, 25},		/* 18  */
3713 	{5036, 20, 25},		/* 19  */
3714 	{5497, 21, 24},		/* 20  */
3715 	{5979, 22, 24},		/* 21  */
3716 	{6483, 23, 23},		/* 22  */
3717 	{7009, 24, 23},		/* 23  */
3718 	{7558, 25, 22},		/* 24  */
3719 	{8130, 26, 22},		/* 25  */
3720 	{8726, 27, 22},		/* 26  */
3721 	{9346, 28, 21},		/* 27  */
3722 	{9991, 29, 21},		/* 28  */
3723 	{10661, 30, 21},	/* 29  */
3724 	{11358, 31, 20},	/* 30  */
3725 	{12082, 32, 20},	/* 31  */
3726 	{12834, 33, 20},	/* 32  */
3727 	{13614, 34, 19},	/* 33  */
3728 	{14424, 35, 19},	/* 34  */
3729 	{15265, 36, 19},	/* 35  */
3730 	{16137, 37, 19},	/* 36  */
3731 	{17042, 38, 18},	/* 37  */
3732 	{17981, 39, 18},	/* 38  */
3733 	{18955, 40, 18},	/* 39  */
3734 	{19965, 41, 17},	/* 40  */
3735 	{21013, 42, 17},	/* 41  */
3736 	{22101, 43, 17},	/* 42  */
3737 	{23230, 44, 17},	/* 43  */
3738 	{24402, 45, 16},	/* 44  */
3739 	{25618, 46, 16},	/* 45  */
3740 	{26881, 47, 16},	/* 46  */
3741 	{28193, 48, 16},	/* 47  */
3742 	{29557, 49, 15},	/* 48  */
3743 	{30975, 50, 15},	/* 49  */
3744 	{32450, 51, 15},	/* 50  */
3745 	{33986, 52, 15},	/* 51  */
3746 	{35586, 53, 14},	/* 52  */
3747 	{37253, 54, 14},	/* 53  */
3748 	{38992, 55, 14},	/* 54  */
3749 	{40808, 56, 14},	/* 55  */
3750 	{42707, 57, 13},	/* 56  */
3751 	{44694, 58, 13},	/* 57  */
3752 	{46776, 59, 13},	/* 58  */
3753 	{48961, 60, 13},	/* 59  */
3754 	{51258, 61, 13},	/* 60  */
3755 	{53677, 62, 12},	/* 61  */
3756 	{56230, 63, 12},	/* 62  */
3757 	{58932, 64, 12},	/* 63  */
3758 	{61799, 65, 12},	/* 64  */
3759 	{64851, 66, 11},	/* 65  */
3760 	{68113, 67, 11},	/* 66  */
3761 	{71617, 68, 11},	/* 67  */
3762 	{75401, 69, 10},	/* 68  */
3763 	{79517, 70, 10},	/* 69  */
3764 	{84035, 71, 10},	/* 70  */
3765 	{89053, 72, 10},	/* 71  */
3766 	{94717, 73, 9}		/* 72  */
3767 };
3768 
3769 static void
3770 sctp_hs_cwnd_increase(struct sctp_tcb *stcb, struct sctp_nets *net)
3771 {
3772 	int cur_val, i, indx, incr;
3773 
3774 	cur_val = net->cwnd >> 10;
3775 	indx = SCTP_HS_TABLE_SIZE - 1;
3776 
3777 	if (cur_val < sctp_cwnd_adjust[0].cwnd) {
3778 		/* normal mode */
3779 		if (net->net_ack > net->mtu) {
3780 			net->cwnd += net->mtu;
3781 #ifdef SCTP_CWND_MONITOR
3782 			sctp_log_cwnd(stcb, net, net->mtu, SCTP_CWND_LOG_FROM_SS);
3783 #endif
3784 		} else {
3785 			net->cwnd += net->net_ack;
3786 #ifdef SCTP_CWND_MONITOR
3787 			sctp_log_cwnd(stcb, net, net->net_ack, SCTP_CWND_LOG_FROM_SS);
3788 #endif
3789 		}
3790 	} else {
3791 		for (i = net->last_hs_used; i < SCTP_HS_TABLE_SIZE; i++) {
3792 			if (cur_val < sctp_cwnd_adjust[i].cwnd) {
3793 				indx = i;
3794 				break;
3795 			}
3796 		}
3797 		net->last_hs_used = indx;
3798 		incr = ((sctp_cwnd_adjust[indx].increase) << 10);
3799 		net->cwnd += incr;
3800 #ifdef SCTP_CWND_MONITOR
3801 		sctp_log_cwnd(stcb, net, incr, SCTP_CWND_LOG_FROM_SS);
3802 #endif
3803 	}
3804 }
3805 
3806 static void
3807 sctp_hs_cwnd_decrease(struct sctp_tcb *stcb, struct sctp_nets *net)
3808 {
3809 	int cur_val, i, indx;
3810 
3811 #ifdef SCTP_CWND_MONITOR
3812 	int old_cwnd = net->cwnd;
3813 
3814 #endif
3815 
3816 	cur_val = net->cwnd >> 10;
3817 	indx = net->last_hs_used;
3818 	if (cur_val < sctp_cwnd_adjust[0].cwnd) {
3819 		/* normal mode */
3820 		net->ssthresh = net->cwnd / 2;
3821 		if (net->ssthresh < (net->mtu * 2)) {
3822 			net->ssthresh = 2 * net->mtu;
3823 		}
3824 		net->cwnd = net->ssthresh;
3825 	} else {
3826 		/* drop by the proper amount */
3827 		net->ssthresh = net->cwnd - (int)((net->cwnd / 100) *
3828 		    sctp_cwnd_adjust[net->last_hs_used].drop_percent);
3829 		net->cwnd = net->ssthresh;
3830 		/* now where are we */
3831 		indx = net->last_hs_used;
3832 		cur_val = net->cwnd >> 10;
3833 		/* reset where we are in the table */
3834 		if (cur_val < sctp_cwnd_adjust[0].cwnd) {
3835 			/* feel out of hs */
3836 			net->last_hs_used = 0;
3837 		} else {
3838 			for (i = indx; i >= 1; i--) {
3839 				if (cur_val > sctp_cwnd_adjust[i - 1].cwnd) {
3840 					break;
3841 				}
3842 			}
3843 			net->last_hs_used = indx;
3844 		}
3845 	}
3846 #ifdef SCTP_CWND_MONITOR
3847 	sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd), SCTP_CWND_LOG_FROM_FR);
3848 #endif
3849 
3850 }
3851 
3852 #endif
3853 
3854 
3855 static __inline void
3856 sctp_cwnd_update(struct sctp_tcb *stcb,
3857     struct sctp_association *asoc,
3858     int accum_moved, int reneged_all, int will_exit)
3859 {
3860 	struct sctp_nets *net;
3861 
3862 	/******************************/
3863 	/* update cwnd and Early FR   */
3864 	/******************************/
3865 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3866 
3867 #ifdef JANA_CMT_FAST_RECOVERY
3868 		/*
3869 		 * CMT fast recovery code. Need to debug.
3870 		 */
3871 		if (net->fast_retran_loss_recovery && net->new_pseudo_cumack) {
3872 			if (compare_with_wrap(asoc->last_acked_seq,
3873 			    net->fast_recovery_tsn, MAX_TSN) ||
3874 			    (asoc->last_acked_seq == net->fast_recovery_tsn) ||
3875 			    compare_with_wrap(net->pseudo_cumack, net->fast_recovery_tsn, MAX_TSN) ||
3876 			    (net->pseudo_cumack == net->fast_recovery_tsn)) {
3877 				net->will_exit_fast_recovery = 1;
3878 			}
3879 		}
3880 #endif
3881 		if (sctp_early_fr) {
3882 			/*
3883 			 * So, first of all do we need to have a Early FR
3884 			 * timer running?
3885 			 */
3886 			if (((TAILQ_FIRST(&asoc->sent_queue)) &&
3887 			    (net->ref_count > 1) &&
3888 			    (net->flight_size < net->cwnd)) ||
3889 			    (reneged_all)) {
3890 				/*
3891 				 * yes, so in this case stop it if its
3892 				 * running, and then restart it. Reneging
3893 				 * all is a special case where we want to
3894 				 * run the Early FR timer and then force the
3895 				 * last few unacked to be sent, causing us
3896 				 * to illicit a sack with gaps to force out
3897 				 * the others.
3898 				 */
3899 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
3900 					SCTP_STAT_INCR(sctps_earlyfrstpidsck2);
3901 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
3902 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_20);
3903 				}
3904 				SCTP_STAT_INCR(sctps_earlyfrstrid);
3905 				sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net);
3906 			} else {
3907 				/* No, stop it if its running */
3908 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
3909 					SCTP_STAT_INCR(sctps_earlyfrstpidsck3);
3910 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
3911 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_21);
3912 				}
3913 			}
3914 		}
3915 		/* if nothing was acked on this destination skip it */
3916 		if (net->net_ack == 0) {
3917 #ifdef SCTP_CWND_LOGGING
3918 			sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FROM_SACK);
3919 #endif
3920 			continue;
3921 		}
3922 		if (net->net_ack2 > 0) {
3923 			/*
3924 			 * Karn's rule applies to clearing error count, this
3925 			 * is optional.
3926 			 */
3927 			net->error_count = 0;
3928 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
3929 			    SCTP_ADDR_NOT_REACHABLE) {
3930 				/* addr came good */
3931 				net->dest_state &= ~SCTP_ADDR_NOT_REACHABLE;
3932 				net->dest_state |= SCTP_ADDR_REACHABLE;
3933 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_UP, stcb,
3934 				    SCTP_RECEIVED_SACK, (void *)net);
3935 				/* now was it the primary? if so restore */
3936 				if (net->dest_state & SCTP_ADDR_WAS_PRIMARY) {
3937 					(void)sctp_set_primary_addr(stcb, (struct sockaddr *)NULL, net);
3938 				}
3939 			}
3940 		}
3941 #ifdef JANA_CMT_FAST_RECOVERY
3942 		/*
3943 		 * CMT fast recovery code
3944 		 */
3945 		/*
3946 		 * if (sctp_cmt_on_off == 1 &&
3947 		 * net->fast_retran_loss_recovery &&
3948 		 * net->will_exit_fast_recovery == 0) { // @@@ Do something
3949 		 * }	   else if (sctp_cmt_on_off == 0 &&
3950 		 * asoc->fast_retran_loss_recovery && will_exit == 0) {
3951 		 */
3952 #endif
3953 
3954 		if (asoc->fast_retran_loss_recovery && will_exit == 0 && sctp_cmt_on_off == 0) {
3955 			/*
3956 			 * If we are in loss recovery we skip any cwnd
3957 			 * update
3958 			 */
3959 			goto skip_cwnd_update;
3960 		}
3961 		/*
3962 		 * CMT: CUC algorithm. Update cwnd if pseudo-cumack has
3963 		 * moved.
3964 		 */
3965 		if (accum_moved || (sctp_cmt_on_off && net->new_pseudo_cumack)) {
3966 			/* If the cumulative ack moved we can proceed */
3967 			if (net->cwnd <= net->ssthresh) {
3968 				/* We are in slow start */
3969 				if (net->flight_size + net->net_ack >=
3970 				    net->cwnd) {
3971 #ifdef SCTP_HIGH_SPEED
3972 					sctp_hs_cwnd_increase(stcb, net);
3973 #else
3974 					if (net->net_ack > (net->mtu * sctp_L2_abc_variable)) {
3975 						net->cwnd += (net->mtu * sctp_L2_abc_variable);
3976 #ifdef SCTP_CWND_MONITOR
3977 						sctp_log_cwnd(stcb, net, net->mtu,
3978 						    SCTP_CWND_LOG_FROM_SS);
3979 #endif
3980 
3981 					} else {
3982 						net->cwnd += net->net_ack;
3983 #ifdef SCTP_CWND_MONITOR
3984 						sctp_log_cwnd(stcb, net, net->net_ack,
3985 						    SCTP_CWND_LOG_FROM_SS);
3986 #endif
3987 
3988 					}
3989 #endif
3990 				} else {
3991 					unsigned int dif;
3992 
3993 					dif = net->cwnd - (net->flight_size +
3994 					    net->net_ack);
3995 #ifdef SCTP_CWND_LOGGING
3996 					sctp_log_cwnd(stcb, net, net->net_ack,
3997 					    SCTP_CWND_LOG_NOADV_SS);
3998 #endif
3999 				}
4000 			} else {
4001 				/* We are in congestion avoidance */
4002 				if (net->flight_size + net->net_ack >=
4003 				    net->cwnd) {
4004 					/*
4005 					 * add to pba only if we had a
4006 					 * cwnd's worth (or so) in flight OR
4007 					 * the burst limit was applied.
4008 					 */
4009 					net->partial_bytes_acked +=
4010 					    net->net_ack;
4011 
4012 					/*
4013 					 * Do we need to increase (if pba is
4014 					 * > cwnd)?
4015 					 */
4016 					if (net->partial_bytes_acked >=
4017 					    net->cwnd) {
4018 						if (net->cwnd <
4019 						    net->partial_bytes_acked) {
4020 							net->partial_bytes_acked -=
4021 							    net->cwnd;
4022 						} else {
4023 							net->partial_bytes_acked =
4024 							    0;
4025 						}
4026 						net->cwnd += net->mtu;
4027 #ifdef SCTP_CWND_MONITOR
4028 						sctp_log_cwnd(stcb, net, net->mtu,
4029 						    SCTP_CWND_LOG_FROM_CA);
4030 #endif
4031 					}
4032 #ifdef SCTP_CWND_LOGGING
4033 					else {
4034 						sctp_log_cwnd(stcb, net, net->net_ack,
4035 						    SCTP_CWND_LOG_NOADV_CA);
4036 					}
4037 #endif
4038 				} else {
4039 					unsigned int dif;
4040 
4041 #ifdef SCTP_CWND_LOGGING
4042 					sctp_log_cwnd(stcb, net, net->net_ack,
4043 					    SCTP_CWND_LOG_NOADV_CA);
4044 #endif
4045 					dif = net->cwnd - (net->flight_size +
4046 					    net->net_ack);
4047 				}
4048 			}
4049 		} else {
4050 #ifdef SCTP_CWND_LOGGING
4051 			sctp_log_cwnd(stcb, net, net->mtu,
4052 			    SCTP_CWND_LOG_NO_CUMACK);
4053 #endif
4054 		}
4055 skip_cwnd_update:
4056 		/*
4057 		 * NOW, according to Karn's rule do we need to restore the
4058 		 * RTO timer back? Check our net_ack2. If not set then we
4059 		 * have a ambiguity.. i.e. all data ack'd was sent to more
4060 		 * than one place.
4061 		 */
4062 		if (net->net_ack2) {
4063 			/* restore any doubled timers */
4064 			net->RTO = ((net->lastsa >> 2) + net->lastsv) >> 1;
4065 			if (net->RTO < stcb->asoc.minrto) {
4066 				net->RTO = stcb->asoc.minrto;
4067 			}
4068 			if (net->RTO > stcb->asoc.maxrto) {
4069 				net->RTO = stcb->asoc.maxrto;
4070 			}
4071 		}
4072 	}
4073 }
4074 
4075 static void
4076 sctp_fs_audit(struct sctp_association *asoc)
4077 {
4078 	struct sctp_tmit_chunk *chk;
4079 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
4080 
4081 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4082 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
4083 			inflight++;
4084 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
4085 			resend++;
4086 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
4087 			inbetween++;
4088 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
4089 			above++;
4090 		} else {
4091 			acked++;
4092 		}
4093 	}
4094 
4095 	if ((inflight > 0) || (inbetween > 0)) {
4096 #ifdef INVARIANTS
4097 		panic("Flight size-express incorrect? \n");
4098 #else
4099 		SCTP_PRINTF("Flight size-express incorrect inflight:%d inbetween:%d\n",
4100 		    inflight, inbetween);
4101 #endif
4102 	}
4103 }
4104 
4105 
4106 static void
4107 sctp_window_probe_recovery(struct sctp_tcb *stcb,
4108     struct sctp_association *asoc,
4109     struct sctp_nets *net,
4110     struct sctp_tmit_chunk *tp1)
4111 {
4112 	struct sctp_tmit_chunk *chk;
4113 
4114 	/* First setup this one and get it moved back */
4115 	tp1->sent = SCTP_DATAGRAM_UNSENT;
4116 	tp1->window_probe = 0;
4117 #ifdef SCTP_FLIGHT_LOGGING
4118 	sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
4119 	    tp1->whoTo->flight_size,
4120 	    tp1->book_size,
4121 	    (uintptr_t) tp1->whoTo,
4122 	    tp1->rec.data.TSN_seq);
4123 #endif
4124 	sctp_flight_size_decrease(tp1);
4125 	sctp_total_flight_decrease(stcb, tp1);
4126 	TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4127 	TAILQ_INSERT_HEAD(&asoc->send_queue, tp1, sctp_next);
4128 	asoc->sent_queue_cnt--;
4129 	asoc->send_queue_cnt++;
4130 	/*
4131 	 * Now all guys marked for RESEND on the sent_queue must be moved
4132 	 * back too.
4133 	 */
4134 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4135 		if (chk->sent == SCTP_DATAGRAM_RESEND) {
4136 			/* Another chunk to move */
4137 			chk->sent = SCTP_DATAGRAM_UNSENT;
4138 			chk->window_probe = 0;
4139 			/* It should not be in flight */
4140 			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4141 			TAILQ_INSERT_AFTER(&asoc->send_queue, tp1, chk, sctp_next);
4142 			asoc->sent_queue_cnt--;
4143 			asoc->send_queue_cnt++;
4144 			sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4145 		}
4146 	}
4147 }
4148 
4149 
4150 void
4151 sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
4152     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
4153 {
4154 	struct sctp_nets *net;
4155 	struct sctp_association *asoc;
4156 	struct sctp_tmit_chunk *tp1, *tp2;
4157 	uint32_t old_rwnd;
4158 	int win_probe_recovery = 0;
4159 	int win_probe_recovered = 0;
4160 	int j, done_once = 0;
4161 
4162 
4163 #ifdef SCTP_LOG_SACK_ARRIVALS
4164 	sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
4165 	    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4166 #endif
4167 	SCTP_TCB_LOCK_ASSERT(stcb);
4168 	asoc = &stcb->asoc;
4169 	old_rwnd = asoc->peers_rwnd;
4170 	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
4171 		/* old ack */
4172 		return;
4173 	} else if (asoc->last_acked_seq == cumack) {
4174 		/* Window update sack */
4175 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4176 		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
4177 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4178 			/* SWS sender side engages */
4179 			asoc->peers_rwnd = 0;
4180 		}
4181 		if (asoc->peers_rwnd > old_rwnd) {
4182 			goto again;
4183 		}
4184 		return;
4185 
4186 	}
4187 	/* First setup for CC stuff */
4188 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4189 		net->prev_cwnd = net->cwnd;
4190 		net->net_ack = 0;
4191 		net->net_ack2 = 0;
4192 
4193 		/*
4194 		 * CMT: Reset CUC and Fast recovery algo variables before
4195 		 * SACK processing
4196 		 */
4197 		net->new_pseudo_cumack = 0;
4198 		net->will_exit_fast_recovery = 0;
4199 	}
4200 	if (sctp_strict_sacks) {
4201 		uint32_t send_s;
4202 
4203 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4204 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4205 			    sctpchunk_listhead);
4206 			send_s = tp1->rec.data.TSN_seq + 1;
4207 		} else {
4208 			send_s = asoc->sending_seq;
4209 		}
4210 		if ((cumack == send_s) ||
4211 		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
4212 #ifndef INVARIANTS
4213 			struct mbuf *oper;
4214 
4215 #endif
4216 #ifdef INVARIANTS
4217 			panic("Impossible sack 1");
4218 #else
4219 			*abort_now = 1;
4220 			/* XXX */
4221 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4222 			    0, M_DONTWAIT, 1, MT_DATA);
4223 			if (oper) {
4224 				struct sctp_paramhdr *ph;
4225 				uint32_t *ippp;
4226 
4227 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4228 				    sizeof(uint32_t);
4229 				ph = mtod(oper, struct sctp_paramhdr *);
4230 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4231 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4232 				ippp = (uint32_t *) (ph + 1);
4233 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4234 			}
4235 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4236 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper);
4237 			return;
4238 #endif
4239 		}
4240 	}
4241 	asoc->this_sack_highest_gap = cumack;
4242 	stcb->asoc.overall_error_count = 0;
4243 	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
4244 		/* process the new consecutive TSN first */
4245 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
4246 		while (tp1) {
4247 			tp2 = TAILQ_NEXT(tp1, sctp_next);
4248 			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
4249 			    MAX_TSN) ||
4250 			    cumack == tp1->rec.data.TSN_seq) {
4251 				if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4252 					/*
4253 					 * ECN Nonce: Add the nonce to the
4254 					 * sender's nonce sum
4255 					 */
4256 					asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4257 					if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4258 						/*
4259 						 * If it is less than ACKED,
4260 						 * it is now no-longer in
4261 						 * flight. Higher values may
4262 						 * occur during marking
4263 						 */
4264 						if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4265 #ifdef SCTP_FLIGHT_LOGGING
4266 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4267 							    tp1->whoTo->flight_size,
4268 							    tp1->book_size,
4269 							    (uintptr_t) tp1->whoTo,
4270 							    tp1->rec.data.TSN_seq);
4271 #endif
4272 
4273 							sctp_flight_size_decrease(tp1);
4274 							sctp_total_flight_decrease(stcb, tp1);
4275 						}
4276 						tp1->whoTo->net_ack += tp1->send_size;
4277 						if (tp1->snd_count < 2) {
4278 							/*
4279 							 * True
4280 							 * non-retransmited
4281 							 * chunk
4282 							 */
4283 							tp1->whoTo->net_ack2 +=
4284 							    tp1->send_size;
4285 
4286 							/* update RTO too? */
4287 							if (tp1->do_rtt) {
4288 								tp1->whoTo->RTO =
4289 								    sctp_calculate_rto(stcb,
4290 								    asoc, tp1->whoTo,
4291 								    &tp1->sent_rcv_time);
4292 								tp1->do_rtt = 0;
4293 							}
4294 						}
4295 						/*
4296 						 * CMT: CUCv2 algorithm.
4297 						 * From the cumack'd TSNs,
4298 						 * for each TSN being acked
4299 						 * for the first time, set
4300 						 * the following variables
4301 						 * for the corresp
4302 						 * destination.
4303 						 * new_pseudo_cumack will
4304 						 * trigger a cwnd update.
4305 						 * find_(rtx_)pseudo_cumack
4306 						 * will trigger search for
4307 						 * the next expected
4308 						 * (rtx-)pseudo-cumack.
4309 						 */
4310 						tp1->whoTo->new_pseudo_cumack = 1;
4311 						tp1->whoTo->find_pseudo_cumack = 1;
4312 						tp1->whoTo->find_rtx_pseudo_cumack = 1;
4313 
4314 #ifdef SCTP_CWND_LOGGING
4315 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4316 #endif
4317 					}
4318 					if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4319 						sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4320 					}
4321 					if (tp1->rec.data.chunk_was_revoked) {
4322 						/* deflate the cwnd */
4323 						tp1->whoTo->cwnd -= tp1->book_size;
4324 						tp1->rec.data.chunk_was_revoked = 0;
4325 					}
4326 					tp1->sent = SCTP_DATAGRAM_ACKED;
4327 				}
4328 			} else {
4329 				break;
4330 			}
4331 			TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4332 			if (tp1->data) {
4333 				sctp_free_bufspace(stcb, asoc, tp1, 1);
4334 				sctp_m_freem(tp1->data);
4335 			}
4336 #ifdef SCTP_SACK_LOGGING
4337 			sctp_log_sack(asoc->last_acked_seq,
4338 			    cumack,
4339 			    tp1->rec.data.TSN_seq,
4340 			    0,
4341 			    0,
4342 			    SCTP_LOG_FREE_SENT);
4343 #endif
4344 			tp1->data = NULL;
4345 			asoc->sent_queue_cnt--;
4346 			sctp_free_remote_addr(tp1->whoTo);
4347 			sctp_free_a_chunk(stcb, tp1);
4348 			tp1 = tp2;
4349 		}
4350 	}
4351 	if (stcb->sctp_socket) {
4352 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4353 #ifdef SCTP_WAKE_LOGGING
4354 		sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
4355 #endif
4356 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4357 #ifdef SCTP_WAKE_LOGGING
4358 	} else {
4359 		sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
4360 #endif
4361 	}
4362 
4363 
4364 	if (asoc->last_acked_seq != cumack)
4365 		sctp_cwnd_update(stcb, asoc, 1, 0, 0);
4366 
4367 	asoc->last_acked_seq = cumack;
4368 
4369 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4370 		/* nothing left in-flight */
4371 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4372 			net->flight_size = 0;
4373 			net->partial_bytes_acked = 0;
4374 		}
4375 		asoc->total_flight = 0;
4376 		asoc->total_flight_count = 0;
4377 	}
4378 	/* Fix up the a-p-a-p for future PR-SCTP sends */
4379 	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4380 		asoc->advanced_peer_ack_point = cumack;
4381 	}
4382 	/* ECN Nonce updates */
4383 	if (asoc->ecn_nonce_allowed) {
4384 		if (asoc->nonce_sum_check) {
4385 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4386 				if (asoc->nonce_wait_for_ecne == 0) {
4387 					struct sctp_tmit_chunk *lchk;
4388 
4389 					lchk = TAILQ_FIRST(&asoc->send_queue);
4390 					asoc->nonce_wait_for_ecne = 1;
4391 					if (lchk) {
4392 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4393 					} else {
4394 						asoc->nonce_wait_tsn = asoc->sending_seq;
4395 					}
4396 				} else {
4397 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4398 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4399 						/*
4400 						 * Misbehaving peer. We need
4401 						 * to react to this guy
4402 						 */
4403 						asoc->ecn_allowed = 0;
4404 						asoc->ecn_nonce_allowed = 0;
4405 					}
4406 				}
4407 			}
4408 		} else {
4409 			/* See if Resynchronization Possible */
4410 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4411 				asoc->nonce_sum_check = 1;
4412 				/*
4413 				 * now we must calculate what the base is.
4414 				 * We do this based on two things, we know
4415 				 * the total's for all the segments
4416 				 * gap-acked in the SACK (none), We also
4417 				 * know the SACK's nonce sum, its in
4418 				 * nonce_sum_flag. So we can build a truth
4419 				 * table to back-calculate the new value of
4420 				 * asoc->nonce_sum_expect_base:
4421 				 *
4422 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
4423 				 * 1                    0 1 0 1 1 1
4424 				 * 1 0
4425 				 */
4426 				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4427 			}
4428 		}
4429 	}
4430 	/* RWND update */
4431 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4432 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
4433 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4434 		/* SWS sender side engages */
4435 		asoc->peers_rwnd = 0;
4436 	}
4437 	if (asoc->peers_rwnd > old_rwnd) {
4438 		win_probe_recovery = 1;
4439 	}
4440 	/* Now assure a timer where data is queued at */
4441 again:
4442 	j = 0;
4443 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4444 		if (win_probe_recovery && (net->window_probe)) {
4445 			net->window_probe = 0;
4446 			win_probe_recovered = 1;
4447 			/*
4448 			 * Find first chunk that was used with window probe
4449 			 * and clear the sent
4450 			 */
4451 			/* sa_ignore FREED_MEMORY */
4452 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4453 				if (tp1->window_probe) {
4454 					/* move back to data send queue */
4455 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
4456 					break;
4457 				}
4458 			}
4459 		}
4460 		if (net->flight_size) {
4461 			int to_ticks;
4462 
4463 			if (net->RTO == 0) {
4464 				to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4465 			} else {
4466 				to_ticks = MSEC_TO_TICKS(net->RTO);
4467 			}
4468 			j++;
4469 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4470 			    sctp_timeout_handler, &net->rxt_timer);
4471 		} else {
4472 			if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4473 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4474 				    stcb, net,
4475 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4476 			}
4477 			if (sctp_early_fr) {
4478 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4479 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4480 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4481 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4482 				}
4483 			}
4484 		}
4485 	}
4486 	if ((j == 0) &&
4487 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4488 	    (asoc->sent_queue_retran_cnt == 0) &&
4489 	    (win_probe_recovered == 0) &&
4490 	    (done_once == 0)) {
4491 		/* huh, this should not happen */
4492 		sctp_fs_audit(asoc);
4493 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4494 			net->flight_size = 0;
4495 		}
4496 		asoc->total_flight = 0;
4497 		asoc->total_flight_count = 0;
4498 		asoc->sent_queue_retran_cnt = 0;
4499 		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4500 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4501 				sctp_flight_size_increase(tp1);
4502 				sctp_total_flight_increase(stcb, tp1);
4503 			} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4504 				asoc->sent_queue_retran_cnt++;
4505 			}
4506 		}
4507 		done_once = 1;
4508 		goto again;
4509 	}
4510 	/**********************************/
4511 	/* Now what about shutdown issues */
4512 	/**********************************/
4513 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4514 		/* nothing left on sendqueue.. consider done */
4515 		/* clean up */
4516 		if ((asoc->stream_queue_cnt == 1) &&
4517 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4518 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4519 		    (asoc->locked_on_sending)
4520 		    ) {
4521 			struct sctp_stream_queue_pending *sp;
4522 
4523 			/*
4524 			 * I may be in a state where we got all across.. but
4525 			 * cannot write more due to a shutdown... we abort
4526 			 * since the user did not indicate EOR in this case.
4527 			 * The sp will be cleaned during free of the asoc.
4528 			 */
4529 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4530 			    sctp_streamhead);
4531 			if ((sp) && (sp->length == 0) && (sp->msg_is_complete == 0)) {
4532 				asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4533 				asoc->locked_on_sending = NULL;
4534 				asoc->stream_queue_cnt--;
4535 			}
4536 		}
4537 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4538 		    (asoc->stream_queue_cnt == 0)) {
4539 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4540 				/* Need to abort here */
4541 				struct mbuf *oper;
4542 
4543 		abort_out_now:
4544 				*abort_now = 1;
4545 				/* XXX */
4546 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4547 				    0, M_DONTWAIT, 1, MT_DATA);
4548 				if (oper) {
4549 					struct sctp_paramhdr *ph;
4550 					uint32_t *ippp;
4551 
4552 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4553 					    sizeof(uint32_t);
4554 					ph = mtod(oper, struct sctp_paramhdr *);
4555 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4556 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4557 					ippp = (uint32_t *) (ph + 1);
4558 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4559 				}
4560 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4561 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper);
4562 			} else {
4563 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4564 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4565 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4566 				}
4567 				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
4568 				sctp_stop_timers_for_shutdown(stcb);
4569 				sctp_send_shutdown(stcb,
4570 				    stcb->asoc.primary_destination);
4571 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4572 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4573 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4574 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4575 			}
4576 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4577 		    (asoc->stream_queue_cnt == 0)) {
4578 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4579 				goto abort_out_now;
4580 			}
4581 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4582 			asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
4583 			sctp_send_shutdown_ack(stcb,
4584 			    stcb->asoc.primary_destination);
4585 
4586 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4587 			    stcb->sctp_ep, stcb, asoc->primary_destination);
4588 		}
4589 	}
4590 #ifdef SCTP_SACK_RWND_LOGGING
4591 	sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4592 	    rwnd,
4593 	    stcb->asoc.peers_rwnd,
4594 	    stcb->asoc.total_flight,
4595 	    stcb->asoc.total_output_queue_size);
4596 
4597 #endif
4598 }
4599 
4600 
4601 
4602 void
4603 sctp_handle_sack(struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
4604     struct sctp_nets *net_from, int *abort_now, int sack_len, uint32_t rwnd)
4605 {
4606 	struct sctp_association *asoc;
4607 	struct sctp_sack *sack;
4608 	struct sctp_tmit_chunk *tp1, *tp2;
4609 	uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
4610 	         this_sack_lowest_newack;
4611 	uint32_t sav_cum_ack;
4612 	uint16_t num_seg, num_dup;
4613 	uint16_t wake_him = 0;
4614 	unsigned int sack_length;
4615 	uint32_t send_s = 0;
4616 	long j;
4617 	int accum_moved = 0;
4618 	int will_exit_fast_recovery = 0;
4619 	uint32_t a_rwnd, old_rwnd;
4620 	int win_probe_recovery = 0;
4621 	int win_probe_recovered = 0;
4622 	struct sctp_nets *net = NULL;
4623 	int nonce_sum_flag, ecn_seg_sums = 0;
4624 	int done_once;
4625 	uint8_t reneged_all = 0;
4626 	uint8_t cmt_dac_flag;
4627 
4628 	/*
4629 	 * we take any chance we can to service our queues since we cannot
4630 	 * get awoken when the socket is read from :<
4631 	 */
4632 	/*
4633 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4634 	 * old sack, if so discard. 2) If there is nothing left in the send
4635 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4636 	 * too, update any rwnd change and verify no timers are running.
4637 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4638 	 * moved process these first and note that it moved. 4) Process any
4639 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4640 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4641 	 * sync up flightsizes and things, stop all timers and also check
4642 	 * for shutdown_pending state. If so then go ahead and send off the
4643 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4644 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4645 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4646 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4647 	 * if in shutdown_recv state.
4648 	 */
4649 	SCTP_TCB_LOCK_ASSERT(stcb);
4650 	sack = &ch->sack;
4651 	/* CMT DAC algo */
4652 	this_sack_lowest_newack = 0;
4653 	j = 0;
4654 	sack_length = (unsigned int)sack_len;
4655 	/* ECN Nonce */
4656 	SCTP_STAT_INCR(sctps_slowpath_sack);
4657 	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
4658 	cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
4659 	num_seg = ntohs(sack->num_gap_ack_blks);
4660 	a_rwnd = rwnd;
4661 
4662 #ifdef SCTP_LOG_SACK_ARRIVALS
4663 	sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4664 	    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4665 #endif
4666 	/* CMT DAC algo */
4667 	cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
4668 	num_dup = ntohs(sack->num_dup_tsns);
4669 
4670 	old_rwnd = stcb->asoc.peers_rwnd;
4671 	stcb->asoc.overall_error_count = 0;
4672 	asoc = &stcb->asoc;
4673 #ifdef SCTP_SACK_LOGGING
4674 	sctp_log_sack(asoc->last_acked_seq,
4675 	    cum_ack,
4676 	    0,
4677 	    num_seg,
4678 	    num_dup,
4679 	    SCTP_LOG_NEW_SACK);
4680 #endif
4681 #if defined(SCTP_FR_LOGGING) || defined(SCTP_EARLYFR_LOGGING)
4682 	if (num_dup) {
4683 		int off_to_dup, iii;
4684 		uint32_t *dupdata;
4685 
4686 		off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) + sizeof(struct sctp_sack_chunk);
4687 		if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= sack_length) {
4688 			dupdata = (uint32_t *) ((caddr_t)ch + off_to_dup);
4689 			for (iii = 0; iii < num_dup; iii++) {
4690 				sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4691 				dupdata++;
4692 
4693 			}
4694 		} else {
4695 			SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d sack_len:%d num gaps:%d\n",
4696 			    off_to_dup, num_dup, sack_length, num_seg);
4697 		}
4698 	}
4699 #endif
4700 	if (sctp_strict_sacks) {
4701 		/* reality check */
4702 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4703 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4704 			    sctpchunk_listhead);
4705 			send_s = tp1->rec.data.TSN_seq + 1;
4706 		} else {
4707 			send_s = asoc->sending_seq;
4708 		}
4709 		if (cum_ack == send_s ||
4710 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4711 #ifndef INVARIANTS
4712 			struct mbuf *oper;
4713 
4714 #endif
4715 #ifdef INVARIANTS
4716 	hopeless_peer:
4717 			panic("Impossible sack 1");
4718 #else
4719 
4720 
4721 			/*
4722 			 * no way, we have not even sent this TSN out yet.
4723 			 * Peer is hopelessly messed up with us.
4724 			 */
4725 	hopeless_peer:
4726 			*abort_now = 1;
4727 			/* XXX */
4728 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4729 			    0, M_DONTWAIT, 1, MT_DATA);
4730 			if (oper) {
4731 				struct sctp_paramhdr *ph;
4732 				uint32_t *ippp;
4733 
4734 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4735 				    sizeof(uint32_t);
4736 				ph = mtod(oper, struct sctp_paramhdr *);
4737 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4738 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4739 				ippp = (uint32_t *) (ph + 1);
4740 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4741 			}
4742 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4743 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper);
4744 			return;
4745 #endif
4746 		}
4747 	}
4748 	/**********************/
4749 	/* 1) check the range */
4750 	/**********************/
4751 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4752 		/* acking something behind */
4753 		return;
4754 	}
4755 	sav_cum_ack = asoc->last_acked_seq;
4756 
4757 	/* update the Rwnd of the peer */
4758 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4759 	    TAILQ_EMPTY(&asoc->send_queue) &&
4760 	    (asoc->stream_queue_cnt == 0)
4761 	    ) {
4762 		/* nothing left on send/sent and strmq */
4763 #ifdef SCTP_LOG_RWND
4764 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4765 		    asoc->peers_rwnd, 0, 0, a_rwnd);
4766 #endif
4767 		asoc->peers_rwnd = a_rwnd;
4768 		if (asoc->sent_queue_retran_cnt) {
4769 			asoc->sent_queue_retran_cnt = 0;
4770 		}
4771 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4772 			/* SWS sender side engages */
4773 			asoc->peers_rwnd = 0;
4774 		}
4775 		/* stop any timers */
4776 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4777 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4778 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4779 			if (sctp_early_fr) {
4780 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4781 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4782 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4783 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4784 				}
4785 			}
4786 			net->partial_bytes_acked = 0;
4787 			net->flight_size = 0;
4788 		}
4789 		asoc->total_flight = 0;
4790 		asoc->total_flight_count = 0;
4791 		return;
4792 	}
4793 	/*
4794 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4795 	 * things. The total byte count acked is tracked in netAckSz AND
4796 	 * netAck2 is used to track the total bytes acked that are un-
4797 	 * amibguious and were never retransmitted. We track these on a per
4798 	 * destination address basis.
4799 	 */
4800 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4801 		net->prev_cwnd = net->cwnd;
4802 		net->net_ack = 0;
4803 		net->net_ack2 = 0;
4804 
4805 		/*
4806 		 * CMT: Reset CUC and Fast recovery algo variables before
4807 		 * SACK processing
4808 		 */
4809 		net->new_pseudo_cumack = 0;
4810 		net->will_exit_fast_recovery = 0;
4811 	}
4812 	/* process the new consecutive TSN first */
4813 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4814 	while (tp1) {
4815 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
4816 		    MAX_TSN) ||
4817 		    last_tsn == tp1->rec.data.TSN_seq) {
4818 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4819 				/*
4820 				 * ECN Nonce: Add the nonce to the sender's
4821 				 * nonce sum
4822 				 */
4823 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4824 				accum_moved = 1;
4825 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4826 					/*
4827 					 * If it is less than ACKED, it is
4828 					 * now no-longer in flight. Higher
4829 					 * values may occur during marking
4830 					 */
4831 					if ((tp1->whoTo->dest_state &
4832 					    SCTP_ADDR_UNCONFIRMED) &&
4833 					    (tp1->snd_count < 2)) {
4834 						/*
4835 						 * If there was no retran
4836 						 * and the address is
4837 						 * un-confirmed and we sent
4838 						 * there and are now
4839 						 * sacked.. its confirmed,
4840 						 * mark it so.
4841 						 */
4842 						tp1->whoTo->dest_state &=
4843 						    ~SCTP_ADDR_UNCONFIRMED;
4844 					}
4845 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4846 #ifdef SCTP_FLIGHT_LOGGING
4847 						sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4848 						    tp1->whoTo->flight_size,
4849 						    tp1->book_size,
4850 						    (uintptr_t) tp1->whoTo,
4851 						    tp1->rec.data.TSN_seq);
4852 #endif
4853 						sctp_flight_size_decrease(tp1);
4854 						sctp_total_flight_decrease(stcb, tp1);
4855 					}
4856 					tp1->whoTo->net_ack += tp1->send_size;
4857 
4858 					/* CMT SFR and DAC algos */
4859 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4860 					tp1->whoTo->saw_newack = 1;
4861 
4862 					if (tp1->snd_count < 2) {
4863 						/*
4864 						 * True non-retransmited
4865 						 * chunk
4866 						 */
4867 						tp1->whoTo->net_ack2 +=
4868 						    tp1->send_size;
4869 
4870 						/* update RTO too? */
4871 						if (tp1->do_rtt) {
4872 							tp1->whoTo->RTO =
4873 							    sctp_calculate_rto(stcb,
4874 							    asoc, tp1->whoTo,
4875 							    &tp1->sent_rcv_time);
4876 							tp1->do_rtt = 0;
4877 						}
4878 					}
4879 					/*
4880 					 * CMT: CUCv2 algorithm. From the
4881 					 * cumack'd TSNs, for each TSN being
4882 					 * acked for the first time, set the
4883 					 * following variables for the
4884 					 * corresp destination.
4885 					 * new_pseudo_cumack will trigger a
4886 					 * cwnd update.
4887 					 * find_(rtx_)pseudo_cumack will
4888 					 * trigger search for the next
4889 					 * expected (rtx-)pseudo-cumack.
4890 					 */
4891 					tp1->whoTo->new_pseudo_cumack = 1;
4892 					tp1->whoTo->find_pseudo_cumack = 1;
4893 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4894 
4895 
4896 #ifdef SCTP_SACK_LOGGING
4897 					sctp_log_sack(asoc->last_acked_seq,
4898 					    cum_ack,
4899 					    tp1->rec.data.TSN_seq,
4900 					    0,
4901 					    0,
4902 					    SCTP_LOG_TSN_ACKED);
4903 #endif
4904 #ifdef SCTP_CWND_LOGGING
4905 					sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4906 #endif
4907 				}
4908 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4909 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4910 #ifdef SCTP_AUDITING_ENABLED
4911 					sctp_audit_log(0xB3,
4912 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
4913 #endif
4914 				}
4915 				if (tp1->rec.data.chunk_was_revoked) {
4916 					/* deflate the cwnd */
4917 					tp1->whoTo->cwnd -= tp1->book_size;
4918 					tp1->rec.data.chunk_was_revoked = 0;
4919 				}
4920 				tp1->sent = SCTP_DATAGRAM_ACKED;
4921 			}
4922 		} else {
4923 			break;
4924 		}
4925 		tp1 = TAILQ_NEXT(tp1, sctp_next);
4926 	}
4927 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4928 	/* always set this up to cum-ack */
4929 	asoc->this_sack_highest_gap = last_tsn;
4930 
4931 	if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_sack_chunk)) > sack_length) {
4932 
4933 		/* skip corrupt segments */
4934 		goto skip_segments;
4935 	}
4936 	if (num_seg > 0) {
4937 
4938 		/*
4939 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4940 		 * to be greater than the cumack. Also reset saw_newack to 0
4941 		 * for all dests.
4942 		 */
4943 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4944 			net->saw_newack = 0;
4945 			net->this_sack_highest_newack = last_tsn;
4946 		}
4947 
4948 		/*
4949 		 * thisSackHighestGap will increase while handling NEW
4950 		 * segments this_sack_highest_newack will increase while
4951 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4952 		 * used for CMT DAC algo. saw_newack will also change.
4953 		 */
4954 		sctp_handle_segments(stcb, asoc, ch, last_tsn,
4955 		    &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
4956 		    num_seg, &ecn_seg_sums);
4957 
4958 		if (sctp_strict_sacks) {
4959 			/*
4960 			 * validate the biggest_tsn_acked in the gap acks if
4961 			 * strict adherence is wanted.
4962 			 */
4963 			if ((biggest_tsn_acked == send_s) ||
4964 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
4965 				/*
4966 				 * peer is either confused or we are under
4967 				 * attack. We must abort.
4968 				 */
4969 				goto hopeless_peer;
4970 			}
4971 		}
4972 	}
4973 skip_segments:
4974 	/*******************************************/
4975 	/* cancel ALL T3-send timer if accum moved */
4976 	/*******************************************/
4977 	if (sctp_cmt_on_off) {
4978 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4979 			if (net->new_pseudo_cumack)
4980 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4981 				    stcb, net,
4982 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4983 
4984 		}
4985 	} else {
4986 		if (accum_moved) {
4987 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4988 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4989 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4990 			}
4991 		}
4992 	}
4993 	/********************************************/
4994 	/* drop the acked chunks from the sendqueue */
4995 	/********************************************/
4996 	asoc->last_acked_seq = cum_ack;
4997 
4998 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4999 	if (tp1 == NULL)
5000 		goto done_with_it;
5001 	do {
5002 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
5003 		    MAX_TSN)) {
5004 			break;
5005 		}
5006 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
5007 			/* no more sent on list */
5008 			break;
5009 		}
5010 		tp2 = TAILQ_NEXT(tp1, sctp_next);
5011 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
5012 		/*
5013 		 * Friendlier printf in lieu of panic now that I think its
5014 		 * fixed
5015 		 */
5016 
5017 		if (tp1->pr_sctp_on) {
5018 			if (asoc->pr_sctp_cnt != 0)
5019 				asoc->pr_sctp_cnt--;
5020 		}
5021 		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
5022 		    (asoc->total_flight > 0)) {
5023 #ifdef INVARIANTS
5024 			panic("Warning flight size is postive and should be 0");
5025 #else
5026 			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
5027 			    asoc->total_flight);
5028 #endif
5029 			asoc->total_flight = 0;
5030 		}
5031 		if (tp1->data) {
5032 			sctp_free_bufspace(stcb, asoc, tp1, 1);
5033 			sctp_m_freem(tp1->data);
5034 			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5035 				asoc->sent_queue_cnt_removeable--;
5036 			}
5037 		}
5038 #ifdef SCTP_SACK_LOGGING
5039 		sctp_log_sack(asoc->last_acked_seq,
5040 		    cum_ack,
5041 		    tp1->rec.data.TSN_seq,
5042 		    0,
5043 		    0,
5044 		    SCTP_LOG_FREE_SENT);
5045 #endif
5046 		tp1->data = NULL;
5047 		asoc->sent_queue_cnt--;
5048 		sctp_free_remote_addr(tp1->whoTo);
5049 
5050 		sctp_free_a_chunk(stcb, tp1);
5051 		wake_him++;
5052 		tp1 = tp2;
5053 	} while (tp1 != NULL);
5054 
5055 done_with_it:
5056 	if ((wake_him) && (stcb->sctp_socket)) {
5057 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
5058 #ifdef SCTP_WAKE_LOGGING
5059 		sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
5060 #endif
5061 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
5062 #ifdef SCTP_WAKE_LOGGING
5063 	} else {
5064 		sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
5065 #endif
5066 	}
5067 
5068 	if (asoc->fast_retran_loss_recovery && accum_moved) {
5069 		if (compare_with_wrap(asoc->last_acked_seq,
5070 		    asoc->fast_recovery_tsn, MAX_TSN) ||
5071 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
5072 			/* Setup so we will exit RFC2582 fast recovery */
5073 			will_exit_fast_recovery = 1;
5074 		}
5075 	}
5076 	/*
5077 	 * Check for revoked fragments:
5078 	 *
5079 	 * if Previous sack - Had no frags then we can't have any revoked if
5080 	 * Previous sack - Had frag's then - If we now have frags aka
5081 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
5082 	 * some of them. else - The peer revoked all ACKED fragments, since
5083 	 * we had some before and now we have NONE.
5084 	 */
5085 
5086 	if (num_seg)
5087 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
5088 	else if (asoc->saw_sack_with_frags) {
5089 		int cnt_revoked = 0;
5090 
5091 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
5092 		if (tp1 != NULL) {
5093 			/* Peer revoked all dg's marked or acked */
5094 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5095 				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
5096 				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
5097 					tp1->sent = SCTP_DATAGRAM_SENT;
5098 #ifdef SCTP_FLIGHT_LOGGING
5099 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
5100 					    tp1->whoTo->flight_size,
5101 					    tp1->book_size,
5102 					    (uintptr_t) tp1->whoTo,
5103 					    tp1->rec.data.TSN_seq);
5104 #endif
5105 					sctp_flight_size_increase(tp1);
5106 					sctp_total_flight_increase(stcb, tp1);
5107 					tp1->rec.data.chunk_was_revoked = 1;
5108 					/*
5109 					 * To ensure that this increase in
5110 					 * flightsize, which is artificial,
5111 					 * does not throttle the sender, we
5112 					 * also increase the cwnd
5113 					 * artificially.
5114 					 */
5115 					tp1->whoTo->cwnd += tp1->book_size;
5116 					cnt_revoked++;
5117 				}
5118 			}
5119 			if (cnt_revoked) {
5120 				reneged_all = 1;
5121 			}
5122 		}
5123 		asoc->saw_sack_with_frags = 0;
5124 	}
5125 	if (num_seg)
5126 		asoc->saw_sack_with_frags = 1;
5127 	else
5128 		asoc->saw_sack_with_frags = 0;
5129 
5130 
5131 	sctp_cwnd_update(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
5132 
5133 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
5134 		/* nothing left in-flight */
5135 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5136 			/* stop all timers */
5137 			if (sctp_early_fr) {
5138 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5139 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5140 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5141 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
5142 				}
5143 			}
5144 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5145 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
5146 			net->flight_size = 0;
5147 			net->partial_bytes_acked = 0;
5148 		}
5149 		asoc->total_flight = 0;
5150 		asoc->total_flight_count = 0;
5151 	}
5152 	/**********************************/
5153 	/* Now what about shutdown issues */
5154 	/**********************************/
5155 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
5156 		/* nothing left on sendqueue.. consider done */
5157 #ifdef SCTP_LOG_RWND
5158 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5159 		    asoc->peers_rwnd, 0, 0, a_rwnd);
5160 #endif
5161 		asoc->peers_rwnd = a_rwnd;
5162 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5163 			/* SWS sender side engages */
5164 			asoc->peers_rwnd = 0;
5165 		}
5166 		/* clean up */
5167 		if ((asoc->stream_queue_cnt == 1) &&
5168 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5169 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
5170 		    (asoc->locked_on_sending)
5171 		    ) {
5172 			struct sctp_stream_queue_pending *sp;
5173 
5174 			/*
5175 			 * I may be in a state where we got all across.. but
5176 			 * cannot write more due to a shutdown... we abort
5177 			 * since the user did not indicate EOR in this case.
5178 			 */
5179 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
5180 			    sctp_streamhead);
5181 			if ((sp) && (sp->length == 0) && (sp->msg_is_complete == 0)) {
5182 				asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
5183 				asoc->locked_on_sending = NULL;
5184 				asoc->stream_queue_cnt--;
5185 			}
5186 		}
5187 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
5188 		    (asoc->stream_queue_cnt == 0)) {
5189 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5190 				/* Need to abort here */
5191 				struct mbuf *oper;
5192 
5193 		abort_out_now:
5194 				*abort_now = 1;
5195 				/* XXX */
5196 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
5197 				    0, M_DONTWAIT, 1, MT_DATA);
5198 				if (oper) {
5199 					struct sctp_paramhdr *ph;
5200 					uint32_t *ippp;
5201 
5202 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5203 					    sizeof(uint32_t);
5204 					ph = mtod(oper, struct sctp_paramhdr *);
5205 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5206 					ph->param_length = htons(SCTP_BUF_LEN(oper));
5207 					ippp = (uint32_t *) (ph + 1);
5208 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
5209 				}
5210 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
5211 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper);
5212 				return;
5213 			} else {
5214 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
5215 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
5216 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5217 				}
5218 				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
5219 				sctp_stop_timers_for_shutdown(stcb);
5220 				sctp_send_shutdown(stcb,
5221 				    stcb->asoc.primary_destination);
5222 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5223 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5224 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5225 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5226 			}
5227 			return;
5228 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5229 		    (asoc->stream_queue_cnt == 0)) {
5230 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5231 				goto abort_out_now;
5232 			}
5233 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5234 			asoc->state = SCTP_STATE_SHUTDOWN_ACK_SENT;
5235 			sctp_send_shutdown_ack(stcb,
5236 			    stcb->asoc.primary_destination);
5237 
5238 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5239 			    stcb->sctp_ep, stcb, asoc->primary_destination);
5240 			return;
5241 		}
5242 	}
5243 	/*
5244 	 * Now here we are going to recycle net_ack for a different use...
5245 	 * HEADS UP.
5246 	 */
5247 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5248 		net->net_ack = 0;
5249 	}
5250 
5251 	/*
5252 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5253 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5254 	 * automatically ensure that.
5255 	 */
5256 	if (sctp_cmt_on_off && sctp_cmt_use_dac && (cmt_dac_flag == 0)) {
5257 		this_sack_lowest_newack = cum_ack;
5258 	}
5259 	if (num_seg > 0) {
5260 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5261 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5262 	}
5263 	/*********************************************/
5264 	/* Here we perform PR-SCTP procedures        */
5265 	/* (section 4.2)                             */
5266 	/*********************************************/
5267 	/* C1. update advancedPeerAckPoint */
5268 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5269 		asoc->advanced_peer_ack_point = cum_ack;
5270 	}
5271 	/* C2. try to further move advancedPeerAckPoint ahead */
5272 
5273 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5274 		struct sctp_tmit_chunk *lchk;
5275 
5276 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5277 		/* C3. See if we need to send a Fwd-TSN */
5278 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5279 		    MAX_TSN)) {
5280 			/*
5281 			 * ISSUE with ECN, see FWD-TSN processing for notes
5282 			 * on issues that will occur when the ECN NONCE
5283 			 * stuff is put into SCTP for cross checking.
5284 			 */
5285 			send_forward_tsn(stcb, asoc);
5286 
5287 			/*
5288 			 * ECN Nonce: Disable Nonce Sum check when FWD TSN
5289 			 * is sent and store resync tsn
5290 			 */
5291 			asoc->nonce_sum_check = 0;
5292 			asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
5293 			if (lchk) {
5294 				/* Assure a timer is up */
5295 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5296 				    stcb->sctp_ep, stcb, lchk->whoTo);
5297 			}
5298 		}
5299 	}
5300 	/*
5301 	 * CMT fast recovery code. Need to debug. ((sctp_cmt_on_off == 1) &&
5302 	 * (net->fast_retran_loss_recovery == 0)))
5303 	 */
5304 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5305 		if ((asoc->fast_retran_loss_recovery == 0) || (sctp_cmt_on_off == 1)) {
5306 			/* out of a RFC2582 Fast recovery window? */
5307 			if (net->net_ack > 0) {
5308 				/*
5309 				 * per section 7.2.3, are there any
5310 				 * destinations that had a fast retransmit
5311 				 * to them. If so what we need to do is
5312 				 * adjust ssthresh and cwnd.
5313 				 */
5314 				struct sctp_tmit_chunk *lchk;
5315 
5316 #ifdef  SCTP_HIGH_SPEED
5317 				sctp_hs_cwnd_decrease(stcb, net);
5318 #else
5319 #ifdef SCTP_CWND_MONITOR
5320 				int old_cwnd = net->cwnd;
5321 
5322 #endif
5323 				net->ssthresh = net->cwnd / 2;
5324 				if (net->ssthresh < (net->mtu * 2)) {
5325 					net->ssthresh = 2 * net->mtu;
5326 				}
5327 				net->cwnd = net->ssthresh;
5328 #ifdef SCTP_CWND_MONITOR
5329 				sctp_log_cwnd(stcb, net, (net->cwnd - old_cwnd),
5330 				    SCTP_CWND_LOG_FROM_FR);
5331 #endif
5332 #endif
5333 
5334 				lchk = TAILQ_FIRST(&asoc->send_queue);
5335 
5336 				net->partial_bytes_acked = 0;
5337 				/* Turn on fast recovery window */
5338 				asoc->fast_retran_loss_recovery = 1;
5339 				if (lchk == NULL) {
5340 					/* Mark end of the window */
5341 					asoc->fast_recovery_tsn = asoc->sending_seq - 1;
5342 				} else {
5343 					asoc->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
5344 				}
5345 
5346 				/*
5347 				 * CMT fast recovery -- per destination
5348 				 * recovery variable.
5349 				 */
5350 				net->fast_retran_loss_recovery = 1;
5351 
5352 				if (lchk == NULL) {
5353 					/* Mark end of the window */
5354 					net->fast_recovery_tsn = asoc->sending_seq - 1;
5355 				} else {
5356 					net->fast_recovery_tsn = lchk->rec.data.TSN_seq - 1;
5357 				}
5358 
5359 
5360 
5361 				/*
5362 				 * Disable Nonce Sum Checking and store the
5363 				 * resync tsn
5364 				 */
5365 				asoc->nonce_sum_check = 0;
5366 				asoc->nonce_resync_tsn = asoc->fast_recovery_tsn + 1;
5367 
5368 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND,
5369 				    stcb->sctp_ep, stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_32);
5370 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5371 				    stcb->sctp_ep, stcb, net);
5372 			}
5373 		} else if (net->net_ack > 0) {
5374 			/*
5375 			 * Mark a peg that we WOULD have done a cwnd
5376 			 * reduction but RFC2582 prevented this action.
5377 			 */
5378 			SCTP_STAT_INCR(sctps_fastretransinrtt);
5379 		}
5380 	}
5381 
5382 
5383 	/******************************************************************
5384 	 *  Here we do the stuff with ECN Nonce checking.
5385 	 *  We basically check to see if the nonce sum flag was incorrect
5386 	 *  or if resynchronization needs to be done. Also if we catch a
5387 	 *  misbehaving receiver we give him the kick.
5388 	 ******************************************************************/
5389 
5390 	if (asoc->ecn_nonce_allowed) {
5391 		if (asoc->nonce_sum_check) {
5392 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5393 				if (asoc->nonce_wait_for_ecne == 0) {
5394 					struct sctp_tmit_chunk *lchk;
5395 
5396 					lchk = TAILQ_FIRST(&asoc->send_queue);
5397 					asoc->nonce_wait_for_ecne = 1;
5398 					if (lchk) {
5399 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5400 					} else {
5401 						asoc->nonce_wait_tsn = asoc->sending_seq;
5402 					}
5403 				} else {
5404 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5405 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5406 						/*
5407 						 * Misbehaving peer. We need
5408 						 * to react to this guy
5409 						 */
5410 						asoc->ecn_allowed = 0;
5411 						asoc->ecn_nonce_allowed = 0;
5412 					}
5413 				}
5414 			}
5415 		} else {
5416 			/* See if Resynchronization Possible */
5417 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5418 				asoc->nonce_sum_check = 1;
5419 				/*
5420 				 * now we must calculate what the base is.
5421 				 * We do this based on two things, we know
5422 				 * the total's for all the segments
5423 				 * gap-acked in the SACK, its stored in
5424 				 * ecn_seg_sums. We also know the SACK's
5425 				 * nonce sum, its in nonce_sum_flag. So we
5426 				 * can build a truth table to back-calculate
5427 				 * the new value of
5428 				 * asoc->nonce_sum_expect_base:
5429 				 *
5430 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
5431 				 * 1                    0 1 0 1 1 1
5432 				 * 1 0
5433 				 */
5434 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5435 			}
5436 		}
5437 	}
5438 	/* Now are we exiting loss recovery ? */
5439 	if (will_exit_fast_recovery) {
5440 		/* Ok, we must exit fast recovery */
5441 		asoc->fast_retran_loss_recovery = 0;
5442 	}
5443 	if ((asoc->sat_t3_loss_recovery) &&
5444 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5445 	    MAX_TSN) ||
5446 	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5447 		/* end satellite t3 loss recovery */
5448 		asoc->sat_t3_loss_recovery = 0;
5449 	}
5450 	/*
5451 	 * CMT Fast recovery
5452 	 */
5453 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5454 		if (net->will_exit_fast_recovery) {
5455 			/* Ok, we must exit fast recovery */
5456 			net->fast_retran_loss_recovery = 0;
5457 		}
5458 	}
5459 
5460 	/* Adjust and set the new rwnd value */
5461 #ifdef SCTP_LOG_RWND
5462 	sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5463 	    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * sctp_peer_chunk_oh), a_rwnd);
5464 #endif
5465 
5466 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5467 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
5468 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5469 		/* SWS sender side engages */
5470 		asoc->peers_rwnd = 0;
5471 	}
5472 	if (asoc->peers_rwnd > old_rwnd) {
5473 		win_probe_recovery = 1;
5474 	}
5475 	/*
5476 	 * Now we must setup so we have a timer up for anyone with
5477 	 * outstanding data.
5478 	 */
5479 	done_once = 0;
5480 again:
5481 	j = 0;
5482 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5483 		if (win_probe_recovery && (net->window_probe)) {
5484 			net->window_probe = 0;
5485 			win_probe_recovered = 1;
5486 			/*-
5487 			 * Find first chunk that was used with
5488 			 * window probe and clear the event. Put
5489 			 * it back into the send queue as if has
5490 			 * not been sent.
5491 			 */
5492 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5493 				if (tp1->window_probe) {
5494 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
5495 					break;
5496 				}
5497 			}
5498 		}
5499 		if (net->flight_size) {
5500 			j++;
5501 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5502 			    stcb->sctp_ep, stcb, net);
5503 		} else {
5504 			if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5505 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5506 				    stcb, net,
5507 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5508 			}
5509 			if (sctp_early_fr) {
5510 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5511 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5512 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5513 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5514 				}
5515 			}
5516 		}
5517 	}
5518 	if ((j == 0) &&
5519 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5520 	    (asoc->sent_queue_retran_cnt == 0) &&
5521 	    (win_probe_recovered == 0) &&
5522 	    (done_once == 0)) {
5523 		/* huh, this should not happen */
5524 		sctp_fs_audit(asoc);
5525 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5526 			net->flight_size = 0;
5527 		}
5528 		asoc->total_flight = 0;
5529 		asoc->total_flight_count = 0;
5530 		asoc->sent_queue_retran_cnt = 0;
5531 		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5532 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5533 				sctp_flight_size_increase(tp1);
5534 				sctp_total_flight_increase(stcb, tp1);
5535 			} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5536 				asoc->sent_queue_retran_cnt++;
5537 			}
5538 		}
5539 		done_once = 1;
5540 		goto again;
5541 	}
5542 #ifdef SCTP_SACK_RWND_LOGGING
5543 	sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5544 	    a_rwnd,
5545 	    stcb->asoc.peers_rwnd,
5546 	    stcb->asoc.total_flight,
5547 	    stcb->asoc.total_output_queue_size);
5548 
5549 #endif
5550 
5551 }
5552 
5553 void
5554 sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5555     struct sctp_nets *netp, int *abort_flag)
5556 {
5557 	/* Copy cum-ack */
5558 	uint32_t cum_ack, a_rwnd;
5559 
5560 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5561 	/* Arrange so a_rwnd does NOT change */
5562 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5563 
5564 	/* Now call the express sack handling */
5565 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5566 }
5567 
5568 static void
5569 sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5570     struct sctp_stream_in *strmin)
5571 {
5572 	struct sctp_queued_to_read *ctl, *nctl;
5573 	struct sctp_association *asoc;
5574 	int tt;
5575 
5576 	asoc = &stcb->asoc;
5577 	tt = strmin->last_sequence_delivered;
5578 	/*
5579 	 * First deliver anything prior to and including the stream no that
5580 	 * came in
5581 	 */
5582 	ctl = TAILQ_FIRST(&strmin->inqueue);
5583 	while (ctl) {
5584 		nctl = TAILQ_NEXT(ctl, next);
5585 		if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5586 		    (tt == ctl->sinfo_ssn)) {
5587 			/* this is deliverable now */
5588 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5589 			/* subtract pending on streams */
5590 			asoc->size_on_all_streams -= ctl->length;
5591 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5592 			/* deliver it to at least the delivery-q */
5593 			if (stcb->sctp_socket) {
5594 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5595 				    ctl,
5596 				    &stcb->sctp_socket->so_rcv, 1);
5597 			}
5598 		} else {
5599 			/* no more delivery now. */
5600 			break;
5601 		}
5602 		ctl = nctl;
5603 	}
5604 	/*
5605 	 * now we must deliver things in queue the normal way  if any are
5606 	 * now ready.
5607 	 */
5608 	tt = strmin->last_sequence_delivered + 1;
5609 	ctl = TAILQ_FIRST(&strmin->inqueue);
5610 	while (ctl) {
5611 		nctl = TAILQ_NEXT(ctl, next);
5612 		if (tt == ctl->sinfo_ssn) {
5613 			/* this is deliverable now */
5614 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5615 			/* subtract pending on streams */
5616 			asoc->size_on_all_streams -= ctl->length;
5617 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5618 			/* deliver it to at least the delivery-q */
5619 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
5620 			if (stcb->sctp_socket) {
5621 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5622 				    ctl,
5623 				    &stcb->sctp_socket->so_rcv, 1);
5624 			}
5625 			tt = strmin->last_sequence_delivered + 1;
5626 		} else {
5627 			break;
5628 		}
5629 		ctl = nctl;
5630 	}
5631 }
5632 
5633 void
5634 sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5635     struct sctp_forward_tsn_chunk *fwd, int *abort_flag)
5636 {
5637 	/*
5638 	 * ISSUES that MUST be fixed for ECN! When we are the sender of the
5639 	 * forward TSN, when the SACK comes back that acknowledges the
5640 	 * FWD-TSN we must reset the NONCE sum to match correctly. This will
5641 	 * get quite tricky since we may have sent more data interveneing
5642 	 * and must carefully account for what the SACK says on the nonce
5643 	 * and any gaps that are reported. This work will NOT be done here,
5644 	 * but I note it here since it is really related to PR-SCTP and
5645 	 * FWD-TSN's
5646 	 */
5647 
5648 	/* The pr-sctp fwd tsn */
5649 	/*
5650 	 * here we will perform all the data receiver side steps for
5651 	 * processing FwdTSN, as required in by pr-sctp draft:
5652 	 *
5653 	 * Assume we get FwdTSN(x):
5654 	 *
5655 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5656 	 * others we have 3) examine and update re-ordering queue on
5657 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5658 	 * report where we are.
5659 	 */
5660 	struct sctp_strseq *stseq;
5661 	struct sctp_association *asoc;
5662 	uint32_t new_cum_tsn, gap, back_out_htsn;
5663 	unsigned int i, cnt_gone, fwd_sz, cumack_set_flag, m_size;
5664 	struct sctp_stream_in *strm;
5665 	struct sctp_tmit_chunk *chk, *at;
5666 
5667 	cumack_set_flag = 0;
5668 	asoc = &stcb->asoc;
5669 	cnt_gone = 0;
5670 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5671 		SCTPDBG(SCTP_DEBUG_INDATA1,
5672 		    "Bad size too small/big fwd-tsn\n");
5673 		return;
5674 	}
5675 	m_size = (stcb->asoc.mapping_array_size << 3);
5676 	/*************************************************************/
5677 	/* 1. Here we update local cumTSN and shift the bitmap array */
5678 	/*************************************************************/
5679 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5680 
5681 	if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
5682 	    asoc->cumulative_tsn == new_cum_tsn) {
5683 		/* Already got there ... */
5684 		return;
5685 	}
5686 	back_out_htsn = asoc->highest_tsn_inside_map;
5687 	if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
5688 	    MAX_TSN)) {
5689 		asoc->highest_tsn_inside_map = new_cum_tsn;
5690 #ifdef SCTP_MAP_LOGGING
5691 		sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5692 #endif
5693 	}
5694 	/*
5695 	 * now we know the new TSN is more advanced, let's find the actual
5696 	 * gap
5697 	 */
5698 	if ((compare_with_wrap(new_cum_tsn, asoc->mapping_array_base_tsn,
5699 	    MAX_TSN)) ||
5700 	    (new_cum_tsn == asoc->mapping_array_base_tsn)) {
5701 		gap = new_cum_tsn - asoc->mapping_array_base_tsn;
5702 	} else {
5703 		/* try to prevent underflow here */
5704 		gap = new_cum_tsn + (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
5705 	}
5706 
5707 	if (gap > m_size) {
5708 		asoc->highest_tsn_inside_map = back_out_htsn;
5709 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5710 			struct mbuf *oper;
5711 
5712 			/*
5713 			 * out of range (of single byte chunks in the rwnd I
5714 			 * give out). This must be an attacker.
5715 			 */
5716 			*abort_flag = 1;
5717 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
5718 			    0, M_DONTWAIT, 1, MT_DATA);
5719 			if (oper) {
5720 				struct sctp_paramhdr *ph;
5721 				uint32_t *ippp;
5722 
5723 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5724 				    (sizeof(uint32_t) * 3);
5725 				ph = mtod(oper, struct sctp_paramhdr *);
5726 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5727 				ph->param_length = htons(SCTP_BUF_LEN(oper));
5728 				ippp = (uint32_t *) (ph + 1);
5729 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
5730 				ippp++;
5731 				*ippp = asoc->highest_tsn_inside_map;
5732 				ippp++;
5733 				*ippp = new_cum_tsn;
5734 			}
5735 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5736 			sctp_abort_an_association(stcb->sctp_ep, stcb,
5737 			    SCTP_PEER_FAULTY, oper);
5738 			return;
5739 		}
5740 		if (asoc->highest_tsn_inside_map >
5741 		    asoc->mapping_array_base_tsn) {
5742 			gap = asoc->highest_tsn_inside_map -
5743 			    asoc->mapping_array_base_tsn;
5744 		} else {
5745 			gap = asoc->highest_tsn_inside_map +
5746 			    (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
5747 		}
5748 		cumack_set_flag = 1;
5749 	}
5750 	for (i = 0; i <= gap; i++) {
5751 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
5752 	}
5753 	/*
5754 	 * Now after marking all, slide thing forward but no sack please.
5755 	 */
5756 	sctp_sack_check(stcb, 0, 0, abort_flag);
5757 	if (*abort_flag)
5758 		return;
5759 
5760 	if (cumack_set_flag) {
5761 		/*
5762 		 * fwd-tsn went outside my gap array - not a common
5763 		 * occurance. Do the same thing we do when a cookie-echo
5764 		 * arrives.
5765 		 */
5766 		asoc->highest_tsn_inside_map = new_cum_tsn - 1;
5767 		asoc->mapping_array_base_tsn = new_cum_tsn;
5768 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
5769 #ifdef SCTP_MAP_LOGGING
5770 		sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5771 #endif
5772 		asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
5773 	}
5774 	/*************************************************************/
5775 	/* 2. Clear up re-assembly queue                             */
5776 	/*************************************************************/
5777 
5778 	/*
5779 	 * First service it if pd-api is up, just in case we can progress it
5780 	 * forward
5781 	 */
5782 	if (asoc->fragmented_delivery_inprogress) {
5783 		sctp_service_reassembly(stcb, asoc);
5784 	}
5785 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5786 		/* For each one on here see if we need to toss it */
5787 		/*
5788 		 * For now large messages held on the reasmqueue that are
5789 		 * complete will be tossed too. We could in theory do more
5790 		 * work to spin through and stop after dumping one msg aka
5791 		 * seeing the start of a new msg at the head, and call the
5792 		 * delivery function... to see if it can be delivered... But
5793 		 * for now we just dump everything on the queue.
5794 		 */
5795 		chk = TAILQ_FIRST(&asoc->reasmqueue);
5796 		while (chk) {
5797 			at = TAILQ_NEXT(chk, sctp_next);
5798 			if (compare_with_wrap(asoc->cumulative_tsn,
5799 			    chk->rec.data.TSN_seq, MAX_TSN) ||
5800 			    asoc->cumulative_tsn == chk->rec.data.TSN_seq) {
5801 				/* It needs to be tossed */
5802 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5803 				if (compare_with_wrap(chk->rec.data.TSN_seq,
5804 				    asoc->tsn_last_delivered, MAX_TSN)) {
5805 					asoc->tsn_last_delivered =
5806 					    chk->rec.data.TSN_seq;
5807 					asoc->str_of_pdapi =
5808 					    chk->rec.data.stream_number;
5809 					asoc->ssn_of_pdapi =
5810 					    chk->rec.data.stream_seq;
5811 					asoc->fragment_flags =
5812 					    chk->rec.data.rcv_flags;
5813 				}
5814 				asoc->size_on_reasm_queue -= chk->send_size;
5815 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5816 				cnt_gone++;
5817 
5818 				/* Clear up any stream problem */
5819 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
5820 				    SCTP_DATA_UNORDERED &&
5821 				    (compare_with_wrap(chk->rec.data.stream_seq,
5822 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
5823 				    MAX_SEQ))) {
5824 					/*
5825 					 * We must dump forward this streams
5826 					 * sequence number if the chunk is
5827 					 * not unordered that is being
5828 					 * skipped. There is a chance that
5829 					 * if the peer does not include the
5830 					 * last fragment in its FWD-TSN we
5831 					 * WILL have a problem here since
5832 					 * you would have a partial chunk in
5833 					 * queue that may not be
5834 					 * deliverable. Also if a Partial
5835 					 * delivery API as started the user
5836 					 * may get a partial chunk. The next
5837 					 * read returning a new chunk...
5838 					 * really ugly but I see no way
5839 					 * around it! Maybe a notify??
5840 					 */
5841 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
5842 					    chk->rec.data.stream_seq;
5843 				}
5844 				if (chk->data) {
5845 					sctp_m_freem(chk->data);
5846 					chk->data = NULL;
5847 				}
5848 				sctp_free_remote_addr(chk->whoTo);
5849 				sctp_free_a_chunk(stcb, chk);
5850 			} else {
5851 				/*
5852 				 * Ok we have gone beyond the end of the
5853 				 * fwd-tsn's mark. Some checks...
5854 				 */
5855 				if ((asoc->fragmented_delivery_inprogress) &&
5856 				    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
5857 					uint32_t str_seq;
5858 
5859 					/*
5860 					 * Special case PD-API is up and
5861 					 * what we fwd-tsn' over includes
5862 					 * one that had the LAST_FRAG. We no
5863 					 * longer need to do the PD-API.
5864 					 */
5865 					asoc->fragmented_delivery_inprogress = 0;
5866 
5867 					str_seq = (asoc->str_of_pdapi << 16) | asoc->ssn_of_pdapi;
5868 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5869 					    stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)&str_seq);
5870 
5871 				}
5872 				break;
5873 			}
5874 			chk = at;
5875 		}
5876 	}
5877 	if (asoc->fragmented_delivery_inprogress) {
5878 		/*
5879 		 * Ok we removed cnt_gone chunks in the PD-API queue that
5880 		 * were being delivered. So now we must turn off the flag.
5881 		 */
5882 		uint32_t str_seq;
5883 
5884 		str_seq = (asoc->str_of_pdapi << 16) | asoc->ssn_of_pdapi;
5885 		sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5886 		    stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)&str_seq);
5887 		asoc->fragmented_delivery_inprogress = 0;
5888 	}
5889 	/*************************************************************/
5890 	/* 3. Update the PR-stream re-ordering queues                */
5891 	/*************************************************************/
5892 	stseq = (struct sctp_strseq *)((caddr_t)fwd + sizeof(*fwd));
5893 	fwd_sz -= sizeof(*fwd);
5894 	{
5895 		/* New method. */
5896 		int num_str, i;
5897 
5898 		num_str = fwd_sz / sizeof(struct sctp_strseq);
5899 		for (i = 0; i < num_str; i++) {
5900 			uint16_t st;
5901 			unsigned char *xx;
5902 
5903 			/* Convert */
5904 			xx = (unsigned char *)&stseq[i];
5905 			st = ntohs(stseq[i].stream);
5906 			stseq[i].stream = st;
5907 			st = ntohs(stseq[i].sequence);
5908 			stseq[i].sequence = st;
5909 			/* now process */
5910 			if (stseq[i].stream > asoc->streamincnt) {
5911 				/*
5912 				 * It is arguable if we should continue.
5913 				 * Since the peer sent bogus stream info we
5914 				 * may be in deep trouble.. a return may be
5915 				 * a better choice?
5916 				 */
5917 				continue;
5918 			}
5919 			strm = &asoc->strmin[stseq[i].stream];
5920 			if (compare_with_wrap(stseq[i].sequence,
5921 			    strm->last_sequence_delivered, MAX_SEQ)) {
5922 				/* Update the sequence number */
5923 				strm->last_sequence_delivered =
5924 				    stseq[i].sequence;
5925 			}
5926 			/* now kick the stream the new way */
5927 			sctp_kick_prsctp_reorder_queue(stcb, strm);
5928 		}
5929 	}
5930 	if (TAILQ_FIRST(&asoc->reasmqueue)) {
5931 		/* now lets kick out and check for more fragmented delivery */
5932 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
5933 	}
5934 }
5935