xref: /freebsd/sys/netinet/sctp_indata.c (revision 830940567b49bb0c08dfaed40418999e76616909)
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 #define SCTP_CALC_TSN_TO_GAP(gap, tsn, mapping_tsn) do { \
49 					if ((compare_with_wrap(tsn, mapping_tsn, MAX_TSN)) || \
50                         (tsn == mapping_tsn)) { \
51 						gap = tsn - mapping_tsn; \
52 					} else { \
53 						gap = (MAX_TSN - mapping_tsn) + tsn + 1; \
54 					} \
55                   } while(0)
56 
57 #define SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc) do { \
58                     if (asoc->mapping_array_base_tsn == asoc->nr_mapping_array_base_tsn) { \
59                        SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, nr_gap); \
60                     } else {\
61                        int lgap; \
62                        SCTP_CALC_TSN_TO_GAP(lgap, tsn, asoc->mapping_array_base_tsn); \
63                        SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, lgap); \
64                     } \
65                   } while(0)
66 
67 /*
68  * NOTES: On the outbound side of things I need to check the sack timer to
69  * see if I should generate a sack into the chunk queue (if I have data to
70  * send that is and will be sending it .. for bundling.
71  *
72  * The callback in sctp_usrreq.c will get called when the socket is read from.
73  * This will cause sctp_service_queues() to get called on the top entry in
74  * the list.
75  */
76 
77 void
78 sctp_set_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
79 {
80 	asoc->my_rwnd = sctp_calc_rwnd(stcb, asoc);
81 }
82 
83 /* Calculate what the rwnd would be */
84 uint32_t
85 sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc)
86 {
87 	uint32_t calc = 0;
88 
89 	/*
90 	 * This is really set wrong with respect to a 1-2-m socket. Since
91 	 * the sb_cc is the count that everyone as put up. When we re-write
92 	 * sctp_soreceive then we will fix this so that ONLY this
93 	 * associations data is taken into account.
94 	 */
95 	if (stcb->sctp_socket == NULL)
96 		return (calc);
97 
98 	if (stcb->asoc.sb_cc == 0 &&
99 	    asoc->size_on_reasm_queue == 0 &&
100 	    asoc->size_on_all_streams == 0) {
101 		/* Full rwnd granted */
102 		calc = max(SCTP_SB_LIMIT_RCV(stcb->sctp_socket), SCTP_MINIMAL_RWND);
103 		return (calc);
104 	}
105 	/* get actual space */
106 	calc = (uint32_t) sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv);
107 
108 	/*
109 	 * take out what has NOT been put on socket queue and we yet hold
110 	 * for putting up.
111 	 */
112 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_reasm_queue);
113 	calc = sctp_sbspace_sub(calc, (uint32_t) asoc->size_on_all_streams);
114 
115 	if (calc == 0) {
116 		/* out of space */
117 		return (calc);
118 	}
119 	/* what is the overhead of all these rwnd's */
120 	calc = sctp_sbspace_sub(calc, stcb->asoc.my_rwnd_control_len);
121 	/*
122 	 * If the window gets too small due to ctrl-stuff, reduce it to 1,
123 	 * even it is 0. SWS engaged
124 	 */
125 	if (calc < stcb->asoc.my_rwnd_control_len) {
126 		calc = 1;
127 	}
128 	return (calc);
129 }
130 
131 
132 
133 /*
134  * Build out our readq entry based on the incoming packet.
135  */
136 struct sctp_queued_to_read *
137 sctp_build_readq_entry(struct sctp_tcb *stcb,
138     struct sctp_nets *net,
139     uint32_t tsn, uint32_t ppid,
140     uint32_t context, uint16_t stream_no,
141     uint16_t stream_seq, uint8_t flags,
142     struct mbuf *dm)
143 {
144 	struct sctp_queued_to_read *read_queue_e = NULL;
145 
146 	sctp_alloc_a_readq(stcb, read_queue_e);
147 	if (read_queue_e == NULL) {
148 		goto failed_build;
149 	}
150 	read_queue_e->sinfo_stream = stream_no;
151 	read_queue_e->sinfo_ssn = stream_seq;
152 	read_queue_e->sinfo_flags = (flags << 8);
153 	read_queue_e->sinfo_ppid = ppid;
154 	read_queue_e->sinfo_context = stcb->asoc.context;
155 	read_queue_e->sinfo_timetolive = 0;
156 	read_queue_e->sinfo_tsn = tsn;
157 	read_queue_e->sinfo_cumtsn = tsn;
158 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
159 	read_queue_e->whoFrom = net;
160 	read_queue_e->length = 0;
161 	atomic_add_int(&net->ref_count, 1);
162 	read_queue_e->data = dm;
163 	read_queue_e->spec_flags = 0;
164 	read_queue_e->tail_mbuf = NULL;
165 	read_queue_e->aux_data = NULL;
166 	read_queue_e->stcb = stcb;
167 	read_queue_e->port_from = stcb->rport;
168 	read_queue_e->do_not_ref_stcb = 0;
169 	read_queue_e->end_added = 0;
170 	read_queue_e->some_taken = 0;
171 	read_queue_e->pdapi_aborted = 0;
172 failed_build:
173 	return (read_queue_e);
174 }
175 
176 
177 /*
178  * Build out our readq entry based on the incoming packet.
179  */
180 static struct sctp_queued_to_read *
181 sctp_build_readq_entry_chk(struct sctp_tcb *stcb,
182     struct sctp_tmit_chunk *chk)
183 {
184 	struct sctp_queued_to_read *read_queue_e = NULL;
185 
186 	sctp_alloc_a_readq(stcb, read_queue_e);
187 	if (read_queue_e == NULL) {
188 		goto failed_build;
189 	}
190 	read_queue_e->sinfo_stream = chk->rec.data.stream_number;
191 	read_queue_e->sinfo_ssn = chk->rec.data.stream_seq;
192 	read_queue_e->sinfo_flags = (chk->rec.data.rcv_flags << 8);
193 	read_queue_e->sinfo_ppid = chk->rec.data.payloadtype;
194 	read_queue_e->sinfo_context = stcb->asoc.context;
195 	read_queue_e->sinfo_timetolive = 0;
196 	read_queue_e->sinfo_tsn = chk->rec.data.TSN_seq;
197 	read_queue_e->sinfo_cumtsn = chk->rec.data.TSN_seq;
198 	read_queue_e->sinfo_assoc_id = sctp_get_associd(stcb);
199 	read_queue_e->whoFrom = chk->whoTo;
200 	read_queue_e->aux_data = NULL;
201 	read_queue_e->length = 0;
202 	atomic_add_int(&chk->whoTo->ref_count, 1);
203 	read_queue_e->data = chk->data;
204 	read_queue_e->tail_mbuf = NULL;
205 	read_queue_e->stcb = stcb;
206 	read_queue_e->port_from = stcb->rport;
207 	read_queue_e->spec_flags = 0;
208 	read_queue_e->do_not_ref_stcb = 0;
209 	read_queue_e->end_added = 0;
210 	read_queue_e->some_taken = 0;
211 	read_queue_e->pdapi_aborted = 0;
212 failed_build:
213 	return (read_queue_e);
214 }
215 
216 
217 struct mbuf *
218 sctp_build_ctl_nchunk(struct sctp_inpcb *inp,
219     struct sctp_sndrcvinfo *sinfo)
220 {
221 	struct sctp_sndrcvinfo *outinfo;
222 	struct cmsghdr *cmh;
223 	struct mbuf *ret;
224 	int len;
225 	int use_extended = 0;
226 
227 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
228 		/* user does not want the sndrcv ctl */
229 		return (NULL);
230 	}
231 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
232 		use_extended = 1;
233 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
234 	} else {
235 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
236 	}
237 
238 
239 	ret = sctp_get_mbuf_for_msg(len,
240 	    0, M_DONTWAIT, 1, MT_DATA);
241 
242 	if (ret == NULL) {
243 		/* No space */
244 		return (ret);
245 	}
246 	/* We need a CMSG header followed by the struct  */
247 	cmh = mtod(ret, struct cmsghdr *);
248 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
249 	cmh->cmsg_level = IPPROTO_SCTP;
250 	if (use_extended) {
251 		cmh->cmsg_type = SCTP_EXTRCV;
252 		cmh->cmsg_len = len;
253 		memcpy(outinfo, sinfo, len);
254 	} else {
255 		cmh->cmsg_type = SCTP_SNDRCV;
256 		cmh->cmsg_len = len;
257 		*outinfo = *sinfo;
258 	}
259 	SCTP_BUF_LEN(ret) = cmh->cmsg_len;
260 	return (ret);
261 }
262 
263 
264 char *
265 sctp_build_ctl_cchunk(struct sctp_inpcb *inp,
266     int *control_len,
267     struct sctp_sndrcvinfo *sinfo)
268 {
269 	struct sctp_sndrcvinfo *outinfo;
270 	struct cmsghdr *cmh;
271 	char *buf;
272 	int len;
273 	int use_extended = 0;
274 
275 	if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT)) {
276 		/* user does not want the sndrcv ctl */
277 		return (NULL);
278 	}
279 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO)) {
280 		use_extended = 1;
281 		len = CMSG_LEN(sizeof(struct sctp_extrcvinfo));
282 	} else {
283 		len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
284 	}
285 	SCTP_MALLOC(buf, char *, len, SCTP_M_CMSG);
286 	if (buf == NULL) {
287 		/* No space */
288 		return (buf);
289 	}
290 	/* We need a CMSG header followed by the struct  */
291 	cmh = (struct cmsghdr *)buf;
292 	outinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmh);
293 	cmh->cmsg_level = IPPROTO_SCTP;
294 	if (use_extended) {
295 		cmh->cmsg_type = SCTP_EXTRCV;
296 		cmh->cmsg_len = len;
297 		memcpy(outinfo, sinfo, len);
298 	} else {
299 		cmh->cmsg_type = SCTP_SNDRCV;
300 		cmh->cmsg_len = len;
301 		*outinfo = *sinfo;
302 	}
303 	*control_len = len;
304 	return (buf);
305 }
306 
307 
308 /*
309  * We are delivering currently from the reassembly queue. We must continue to
310  * deliver until we either: 1) run out of space. 2) run out of sequential
311  * TSN's 3) hit the SCTP_DATA_LAST_FRAG flag.
312  */
313 static void
314 sctp_service_reassembly(struct sctp_tcb *stcb, struct sctp_association *asoc)
315 {
316 	struct sctp_tmit_chunk *chk;
317 	uint16_t nxt_todel;
318 	uint16_t stream_no;
319 	int end = 0;
320 	int cntDel;
321 
322 	/* EY if any out-of-order delivered, then tag it nr on nr_map */
323 	uint32_t nr_tsn, nr_gap;
324 
325 	struct sctp_queued_to_read *control, *ctl, *ctlat;
326 
327 	if (stcb == NULL)
328 		return;
329 
330 	cntDel = stream_no = 0;
331 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
332 	    (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
333 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
334 		/* socket above is long gone or going.. */
335 abandon:
336 		asoc->fragmented_delivery_inprogress = 0;
337 		chk = TAILQ_FIRST(&asoc->reasmqueue);
338 		while (chk) {
339 			TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
340 			asoc->size_on_reasm_queue -= chk->send_size;
341 			sctp_ucount_decr(asoc->cnt_on_reasm_queue);
342 			/*
343 			 * Lose the data pointer, since its in the socket
344 			 * buffer
345 			 */
346 			if (chk->data) {
347 				sctp_m_freem(chk->data);
348 				chk->data = NULL;
349 			}
350 			/* Now free the address and data */
351 			sctp_free_a_chunk(stcb, chk);
352 			/* sa_ignore FREED_MEMORY */
353 			chk = TAILQ_FIRST(&asoc->reasmqueue);
354 		}
355 		return;
356 	}
357 	SCTP_TCB_LOCK_ASSERT(stcb);
358 	do {
359 		chk = TAILQ_FIRST(&asoc->reasmqueue);
360 		if (chk == NULL) {
361 			return;
362 		}
363 		if (chk->rec.data.TSN_seq != (asoc->tsn_last_delivered + 1)) {
364 			/* Can't deliver more :< */
365 			return;
366 		}
367 		stream_no = chk->rec.data.stream_number;
368 		nxt_todel = asoc->strmin[stream_no].last_sequence_delivered + 1;
369 		if (nxt_todel != chk->rec.data.stream_seq &&
370 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
371 			/*
372 			 * Not the next sequence to deliver in its stream OR
373 			 * unordered
374 			 */
375 			return;
376 		}
377 		if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
378 
379 			control = sctp_build_readq_entry_chk(stcb, chk);
380 			if (control == NULL) {
381 				/* out of memory? */
382 				return;
383 			}
384 			/* save it off for our future deliveries */
385 			stcb->asoc.control_pdapi = control;
386 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
387 				end = 1;
388 			else
389 				end = 0;
390 			sctp_add_to_readq(stcb->sctp_ep,
391 			    stcb, control, &stcb->sctp_socket->so_rcv, end,
392 			    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
393 			cntDel++;
394 		} else {
395 			if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG)
396 				end = 1;
397 			else
398 				end = 0;
399 			if (sctp_append_to_readq(stcb->sctp_ep, stcb,
400 			    stcb->asoc.control_pdapi,
401 			    chk->data, end, chk->rec.data.TSN_seq,
402 			    &stcb->sctp_socket->so_rcv)) {
403 				/*
404 				 * something is very wrong, either
405 				 * control_pdapi is NULL, or the tail_mbuf
406 				 * is corrupt, or there is a EOM already on
407 				 * the mbuf chain.
408 				 */
409 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
410 					goto abandon;
411 				} else {
412 #ifdef INVARIANTS
413 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
414 						panic("This should not happen control_pdapi NULL?");
415 					}
416 					/* if we did not panic, it was a EOM */
417 					panic("Bad chunking ??");
418 #else
419 					if ((stcb->asoc.control_pdapi == NULL) || (stcb->asoc.control_pdapi->tail_mbuf == NULL)) {
420 						SCTP_PRINTF("This should not happen control_pdapi NULL?\n");
421 					}
422 					SCTP_PRINTF("Bad chunking ??\n");
423 					SCTP_PRINTF("Dumping re-assembly queue this will probably hose the association\n");
424 
425 #endif
426 					goto abandon;
427 				}
428 			}
429 			cntDel++;
430 		}
431 		/* pull it we did it */
432 		TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
433 		/*
434 		 * EY this is the chunk that should be tagged nr gapped
435 		 * calculate the gap and such then tag this TSN nr
436 		 * chk->rec.data.TSN_seq
437 		 */
438 		/*
439 		 * EY!-TODO- this tsn should be tagged nr only if it is
440 		 * out-of-order, the if statement should be modified
441 		 */
442 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
443 
444 			nr_tsn = chk->rec.data.TSN_seq;
445 			SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
446 			if ((nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3)) ||
447 			    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
448 				/*
449 				 * EY The 1st should never happen, as in
450 				 * process_a_data_chunk method this check
451 				 * should be done
452 				 */
453 				/*
454 				 * EY The 2nd should never happen, because
455 				 * nr_mapping_array is always expanded when
456 				 * mapping_array is expanded
457 				 */
458 				printf("Impossible nr_gap ack range failed\n");
459 			} else {
460 				SCTP_TCB_LOCK_ASSERT(stcb);
461 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
462 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
463 				if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
464 					asoc->highest_tsn_inside_nr_map = nr_tsn;
465 			}
466 		}
467 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
468 			asoc->fragmented_delivery_inprogress = 0;
469 			if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
470 				asoc->strmin[stream_no].last_sequence_delivered++;
471 			}
472 			if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
473 				SCTP_STAT_INCR_COUNTER64(sctps_reasmusrmsgs);
474 			}
475 		} else if (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
476 			/*
477 			 * turn the flag back on since we just  delivered
478 			 * yet another one.
479 			 */
480 			asoc->fragmented_delivery_inprogress = 1;
481 		}
482 		asoc->tsn_of_pdapi_last_delivered = chk->rec.data.TSN_seq;
483 		asoc->last_flags_delivered = chk->rec.data.rcv_flags;
484 		asoc->last_strm_seq_delivered = chk->rec.data.stream_seq;
485 		asoc->last_strm_no_delivered = chk->rec.data.stream_number;
486 
487 		asoc->tsn_last_delivered = chk->rec.data.TSN_seq;
488 		asoc->size_on_reasm_queue -= chk->send_size;
489 		sctp_ucount_decr(asoc->cnt_on_reasm_queue);
490 		/* free up the chk */
491 		chk->data = NULL;
492 		sctp_free_a_chunk(stcb, chk);
493 
494 		if (asoc->fragmented_delivery_inprogress == 0) {
495 			/*
496 			 * Now lets see if we can deliver the next one on
497 			 * the stream
498 			 */
499 			struct sctp_stream_in *strm;
500 
501 			strm = &asoc->strmin[stream_no];
502 			nxt_todel = strm->last_sequence_delivered + 1;
503 			ctl = TAILQ_FIRST(&strm->inqueue);
504 			if (ctl && (nxt_todel == ctl->sinfo_ssn)) {
505 				while (ctl != NULL) {
506 					/* Deliver more if we can. */
507 					if (nxt_todel == ctl->sinfo_ssn) {
508 						ctlat = TAILQ_NEXT(ctl, next);
509 						TAILQ_REMOVE(&strm->inqueue, ctl, next);
510 						asoc->size_on_all_streams -= ctl->length;
511 						sctp_ucount_decr(asoc->cnt_on_all_streams);
512 						strm->last_sequence_delivered++;
513 						/*
514 						 * EY will be used to
515 						 * calculate nr-gap
516 						 */
517 						nr_tsn = ctl->sinfo_tsn;
518 						sctp_add_to_readq(stcb->sctp_ep, stcb,
519 						    ctl,
520 						    &stcb->sctp_socket->so_rcv, 1,
521 						    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
522 						/*
523 						 * EY -now something is
524 						 * delivered, calculate
525 						 * nr_gap and tag this tsn
526 						 * NR
527 						 */
528 						if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
529 							SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
530 							if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
531 							    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
532 								printf("Impossible NR gap calculation?\n");
533 								/*
534 								 * EY The
535 								 * 1st
536 								 * should
537 								 * never
538 								 * happen,
539 								 * as in
540 								 * process_a_
541 								 * data_chunk
542 								 *  method
543 								 * this
544 								 * check
545 								 * should be
546 								 * done
547 								 */
548 								/*
549 								 * EY The
550 								 * 2nd
551 								 * should
552 								 * never
553 								 * happen,
554 								 * because
555 								 * nr_mapping
556 								 * _array is
557 								 * always
558 								 * expanded
559 								 * when
560 								 * mapping_ar
561 								 * ray is
562 								 * expanded
563 								 */
564 							} else {
565 								SCTP_TCB_LOCK_ASSERT(stcb);
566 								SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
567 								SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
568 								if (compare_with_wrap(nr_tsn,
569 								    asoc->highest_tsn_inside_nr_map,
570 								    MAX_TSN))
571 									asoc->highest_tsn_inside_nr_map = nr_tsn;
572 							}
573 						}
574 						ctl = ctlat;
575 					} else {
576 						break;
577 					}
578 					nxt_todel = strm->last_sequence_delivered + 1;
579 				}
580 			}
581 			break;
582 		}
583 		/* sa_ignore FREED_MEMORY */
584 		chk = TAILQ_FIRST(&asoc->reasmqueue);
585 	} while (chk);
586 }
587 
588 /*
589  * Queue the chunk either right into the socket buffer if it is the next one
590  * to go OR put it in the correct place in the delivery queue.  If we do
591  * append to the so_buf, keep doing so until we are out of order. One big
592  * question still remains, what to do when the socket buffer is FULL??
593  */
594 static void
595 sctp_queue_data_to_stream(struct sctp_tcb *stcb, struct sctp_association *asoc,
596     struct sctp_queued_to_read *control, int *abort_flag)
597 {
598 	/*
599 	 * FIX-ME maybe? What happens when the ssn wraps? If we are getting
600 	 * all the data in one stream this could happen quite rapidly. One
601 	 * could use the TSN to keep track of things, but this scheme breaks
602 	 * down in the other type of stream useage that could occur. Send a
603 	 * single msg to stream 0, send 4Billion messages to stream 1, now
604 	 * send a message to stream 0. You have a situation where the TSN
605 	 * has wrapped but not in the stream. Is this worth worrying about
606 	 * or should we just change our queue sort at the bottom to be by
607 	 * TSN.
608 	 *
609 	 * Could it also be legal for a peer to send ssn 1 with TSN 2 and ssn 2
610 	 * with TSN 1? If the peer is doing some sort of funky TSN/SSN
611 	 * assignment this could happen... and I don't see how this would be
612 	 * a violation. So for now I am undecided an will leave the sort by
613 	 * SSN alone. Maybe a hybred approach is the answer
614 	 *
615 	 */
616 	struct sctp_stream_in *strm;
617 	struct sctp_queued_to_read *at;
618 	int queue_needed;
619 	uint16_t nxt_todel;
620 	struct mbuf *oper;
621 
622 	/* EY- will be used to calculate nr-gap for a tsn */
623 	uint32_t nr_tsn, nr_gap;
624 
625 	queue_needed = 1;
626 	asoc->size_on_all_streams += control->length;
627 	sctp_ucount_incr(asoc->cnt_on_all_streams);
628 	strm = &asoc->strmin[control->sinfo_stream];
629 	nxt_todel = strm->last_sequence_delivered + 1;
630 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
631 		sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INTO_STRD);
632 	}
633 	SCTPDBG(SCTP_DEBUG_INDATA1,
634 	    "queue to stream called for ssn:%u lastdel:%u nxt:%u\n",
635 	    (uint32_t) control->sinfo_stream,
636 	    (uint32_t) strm->last_sequence_delivered,
637 	    (uint32_t) nxt_todel);
638 	if (compare_with_wrap(strm->last_sequence_delivered,
639 	    control->sinfo_ssn, MAX_SEQ) ||
640 	    (strm->last_sequence_delivered == control->sinfo_ssn)) {
641 		/* The incoming sseq is behind where we last delivered? */
642 		SCTPDBG(SCTP_DEBUG_INDATA1, "Duplicate S-SEQ:%d delivered:%d from peer, Abort  association\n",
643 		    control->sinfo_ssn, strm->last_sequence_delivered);
644 protocol_error:
645 		/*
646 		 * throw it in the stream so it gets cleaned up in
647 		 * association destruction
648 		 */
649 		TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
650 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
651 		    0, M_DONTWAIT, 1, MT_DATA);
652 		if (oper) {
653 			struct sctp_paramhdr *ph;
654 			uint32_t *ippp;
655 
656 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
657 			    (sizeof(uint32_t) * 3);
658 			ph = mtod(oper, struct sctp_paramhdr *);
659 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
660 			ph->param_length = htons(SCTP_BUF_LEN(oper));
661 			ippp = (uint32_t *) (ph + 1);
662 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_1);
663 			ippp++;
664 			*ippp = control->sinfo_tsn;
665 			ippp++;
666 			*ippp = ((control->sinfo_stream << 16) | control->sinfo_ssn);
667 		}
668 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_1;
669 		sctp_abort_an_association(stcb->sctp_ep, stcb,
670 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
671 
672 		*abort_flag = 1;
673 		return;
674 
675 	}
676 	if (nxt_todel == control->sinfo_ssn) {
677 		/* can be delivered right away? */
678 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
679 			sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_IMMED_DEL);
680 		}
681 		/* EY it wont be queued if it could be delivered directly */
682 		queue_needed = 0;
683 		asoc->size_on_all_streams -= control->length;
684 		sctp_ucount_decr(asoc->cnt_on_all_streams);
685 		strm->last_sequence_delivered++;
686 		/* EY will be used to calculate nr-gap */
687 		nr_tsn = control->sinfo_tsn;
688 		sctp_add_to_readq(stcb->sctp_ep, stcb,
689 		    control,
690 		    &stcb->sctp_socket->so_rcv, 1,
691 		    SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
692 		/*
693 		 * EY this is the chunk that should be tagged nr gapped
694 		 * calculate the gap and such then tag this TSN nr
695 		 * chk->rec.data.TSN_seq
696 		 */
697 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
698 			SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
699 			if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
700 			    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
701 				printf("Impossible nr_tsn set 2?\n");
702 				/*
703 				 * EY The 1st should never happen, as in
704 				 * process_a_data_chunk method this check
705 				 * should be done
706 				 */
707 				/*
708 				 * EY The 2nd should never happen, because
709 				 * nr_mapping_array is always expanded when
710 				 * mapping_array is expanded
711 				 */
712 			} else {
713 				SCTP_TCB_LOCK_ASSERT(stcb);
714 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
715 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
716 				if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
717 					asoc->highest_tsn_inside_nr_map = nr_tsn;
718 			}
719 		}
720 		control = TAILQ_FIRST(&strm->inqueue);
721 		while (control != NULL) {
722 			/* all delivered */
723 			nxt_todel = strm->last_sequence_delivered + 1;
724 			if (nxt_todel == control->sinfo_ssn) {
725 				at = TAILQ_NEXT(control, next);
726 				TAILQ_REMOVE(&strm->inqueue, control, next);
727 				asoc->size_on_all_streams -= control->length;
728 				sctp_ucount_decr(asoc->cnt_on_all_streams);
729 				strm->last_sequence_delivered++;
730 				/*
731 				 * We ignore the return of deliver_data here
732 				 * since we always can hold the chunk on the
733 				 * d-queue. And we have a finite number that
734 				 * can be delivered from the strq.
735 				 */
736 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
737 					sctp_log_strm_del(control, NULL,
738 					    SCTP_STR_LOG_FROM_IMMED_DEL);
739 				}
740 				/* EY will be used to calculate nr-gap */
741 				nr_tsn = control->sinfo_tsn;
742 				sctp_add_to_readq(stcb->sctp_ep, stcb,
743 				    control,
744 				    &stcb->sctp_socket->so_rcv, 1,
745 				    SCTP_READ_LOCK_NOT_HELD,
746 				    SCTP_SO_NOT_LOCKED);
747 				/*
748 				 * EY this is the chunk that should be
749 				 * tagged nr gapped calculate the gap and
750 				 * such then tag this TSN nr
751 				 * chk->rec.data.TSN_seq
752 				 */
753 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
754 					SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
755 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
756 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
757 						printf("Impossible nr TSN set 3?\n");
758 						/*
759 						 * EY The 1st should never
760 						 * happen, as in
761 						 * process_a_data_chunk
762 						 * method this check should
763 						 * be done
764 						 */
765 						/*
766 						 * EY The 2nd should never
767 						 * happen, because
768 						 * nr_mapping_array is
769 						 * always expanded when
770 						 * mapping_array is expanded
771 						 */
772 					} else {
773 						SCTP_TCB_LOCK_ASSERT(stcb);
774 						SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
775 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
776 						if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map,
777 						    MAX_TSN))
778 							asoc->highest_tsn_inside_nr_map = nr_tsn;
779 					}
780 				}
781 				control = at;
782 				continue;
783 			}
784 			break;
785 		}
786 	}
787 	if (queue_needed) {
788 		/*
789 		 * Ok, we did not deliver this guy, find the correct place
790 		 * to put it on the queue.
791 		 */
792 		if ((compare_with_wrap(asoc->cumulative_tsn,
793 		    control->sinfo_tsn, MAX_TSN)) ||
794 		    (control->sinfo_tsn == asoc->cumulative_tsn)) {
795 			goto protocol_error;
796 		}
797 		if (TAILQ_EMPTY(&strm->inqueue)) {
798 			/* Empty queue */
799 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
800 				sctp_log_strm_del(control, NULL, SCTP_STR_LOG_FROM_INSERT_HD);
801 			}
802 			TAILQ_INSERT_HEAD(&strm->inqueue, control, next);
803 		} else {
804 			TAILQ_FOREACH(at, &strm->inqueue, next) {
805 				if (compare_with_wrap(at->sinfo_ssn,
806 				    control->sinfo_ssn, MAX_SEQ)) {
807 					/*
808 					 * one in queue is bigger than the
809 					 * new one, insert before this one
810 					 */
811 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
812 						sctp_log_strm_del(control, at,
813 						    SCTP_STR_LOG_FROM_INSERT_MD);
814 					}
815 					TAILQ_INSERT_BEFORE(at, control, next);
816 					break;
817 				} else if (at->sinfo_ssn == control->sinfo_ssn) {
818 					/*
819 					 * Gak, He sent me a duplicate str
820 					 * seq number
821 					 */
822 					/*
823 					 * foo bar, I guess I will just free
824 					 * this new guy, should we abort
825 					 * too? FIX ME MAYBE? Or it COULD be
826 					 * that the SSN's have wrapped.
827 					 * Maybe I should compare to TSN
828 					 * somehow... sigh for now just blow
829 					 * away the chunk!
830 					 */
831 
832 					if (control->data)
833 						sctp_m_freem(control->data);
834 					control->data = NULL;
835 					asoc->size_on_all_streams -= control->length;
836 					sctp_ucount_decr(asoc->cnt_on_all_streams);
837 					if (control->whoFrom)
838 						sctp_free_remote_addr(control->whoFrom);
839 					control->whoFrom = NULL;
840 					sctp_free_a_readq(stcb, control);
841 					return;
842 				} else {
843 					if (TAILQ_NEXT(at, next) == NULL) {
844 						/*
845 						 * We are at the end, insert
846 						 * it after this one
847 						 */
848 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
849 							sctp_log_strm_del(control, at,
850 							    SCTP_STR_LOG_FROM_INSERT_TL);
851 						}
852 						TAILQ_INSERT_AFTER(&strm->inqueue,
853 						    at, control, next);
854 						break;
855 					}
856 				}
857 			}
858 		}
859 	}
860 }
861 
862 /*
863  * Returns two things: You get the total size of the deliverable parts of the
864  * first fragmented message on the reassembly queue. And you get a 1 back if
865  * all of the message is ready or a 0 back if the message is still incomplete
866  */
867 static int
868 sctp_is_all_msg_on_reasm(struct sctp_association *asoc, uint32_t * t_size)
869 {
870 	struct sctp_tmit_chunk *chk;
871 	uint32_t tsn;
872 
873 	*t_size = 0;
874 	chk = TAILQ_FIRST(&asoc->reasmqueue);
875 	if (chk == NULL) {
876 		/* nothing on the queue */
877 		return (0);
878 	}
879 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0) {
880 		/* Not a first on the queue */
881 		return (0);
882 	}
883 	tsn = chk->rec.data.TSN_seq;
884 	while (chk) {
885 		if (tsn != chk->rec.data.TSN_seq) {
886 			return (0);
887 		}
888 		*t_size += chk->send_size;
889 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
890 			return (1);
891 		}
892 		tsn++;
893 		chk = TAILQ_NEXT(chk, sctp_next);
894 	}
895 	return (0);
896 }
897 
898 static void
899 sctp_deliver_reasm_check(struct sctp_tcb *stcb, struct sctp_association *asoc)
900 {
901 	struct sctp_tmit_chunk *chk;
902 	uint16_t nxt_todel;
903 	uint32_t tsize, pd_point;
904 
905 doit_again:
906 	chk = TAILQ_FIRST(&asoc->reasmqueue);
907 	if (chk == NULL) {
908 		/* Huh? */
909 		asoc->size_on_reasm_queue = 0;
910 		asoc->cnt_on_reasm_queue = 0;
911 		return;
912 	}
913 	if (asoc->fragmented_delivery_inprogress == 0) {
914 		nxt_todel =
915 		    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
916 		if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
917 		    (nxt_todel == chk->rec.data.stream_seq ||
918 		    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
919 			/*
920 			 * Yep the first one is here and its ok to deliver
921 			 * but should we?
922 			 */
923 			if (stcb->sctp_socket) {
924 				pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
925 				    stcb->sctp_ep->partial_delivery_point);
926 			} else {
927 				pd_point = stcb->sctp_ep->partial_delivery_point;
928 			}
929 			if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
930 
931 				/*
932 				 * Yes, we setup to start reception, by
933 				 * backing down the TSN just in case we
934 				 * can't deliver. If we
935 				 */
936 				asoc->fragmented_delivery_inprogress = 1;
937 				asoc->tsn_last_delivered =
938 				    chk->rec.data.TSN_seq - 1;
939 				asoc->str_of_pdapi =
940 				    chk->rec.data.stream_number;
941 				asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
942 				asoc->pdapi_ppid = chk->rec.data.payloadtype;
943 				asoc->fragment_flags = chk->rec.data.rcv_flags;
944 				sctp_service_reassembly(stcb, asoc);
945 			}
946 		}
947 	} else {
948 		/*
949 		 * Service re-assembly will deliver stream data queued at
950 		 * the end of fragmented delivery.. but it wont know to go
951 		 * back and call itself again... we do that here with the
952 		 * got doit_again
953 		 */
954 		sctp_service_reassembly(stcb, asoc);
955 		if (asoc->fragmented_delivery_inprogress == 0) {
956 			/*
957 			 * finished our Fragmented delivery, could be more
958 			 * waiting?
959 			 */
960 			goto doit_again;
961 		}
962 	}
963 }
964 
965 /*
966  * Dump onto the re-assembly queue, in its proper place. After dumping on the
967  * queue, see if anthing can be delivered. If so pull it off (or as much as
968  * we can. If we run out of space then we must dump what we can and set the
969  * appropriate flag to say we queued what we could.
970  */
971 static void
972 sctp_queue_data_for_reasm(struct sctp_tcb *stcb, struct sctp_association *asoc,
973     struct sctp_tmit_chunk *chk, int *abort_flag)
974 {
975 	struct mbuf *oper;
976 	uint32_t cum_ackp1, last_tsn, prev_tsn, post_tsn;
977 	u_char last_flags;
978 	struct sctp_tmit_chunk *at, *prev, *next;
979 
980 	prev = next = NULL;
981 	cum_ackp1 = asoc->tsn_last_delivered + 1;
982 	if (TAILQ_EMPTY(&asoc->reasmqueue)) {
983 		/* This is the first one on the queue */
984 		TAILQ_INSERT_HEAD(&asoc->reasmqueue, chk, sctp_next);
985 		/*
986 		 * we do not check for delivery of anything when only one
987 		 * fragment is here
988 		 */
989 		asoc->size_on_reasm_queue = chk->send_size;
990 		sctp_ucount_incr(asoc->cnt_on_reasm_queue);
991 		if (chk->rec.data.TSN_seq == cum_ackp1) {
992 			if (asoc->fragmented_delivery_inprogress == 0 &&
993 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) !=
994 			    SCTP_DATA_FIRST_FRAG) {
995 				/*
996 				 * An empty queue, no delivery inprogress,
997 				 * we hit the next one and it does NOT have
998 				 * a FIRST fragment mark.
999 				 */
1000 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not first, no fragmented delivery in progress\n");
1001 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1002 				    0, M_DONTWAIT, 1, MT_DATA);
1003 
1004 				if (oper) {
1005 					struct sctp_paramhdr *ph;
1006 					uint32_t *ippp;
1007 
1008 					SCTP_BUF_LEN(oper) =
1009 					    sizeof(struct sctp_paramhdr) +
1010 					    (sizeof(uint32_t) * 3);
1011 					ph = mtod(oper, struct sctp_paramhdr *);
1012 					ph->param_type =
1013 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1014 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1015 					ippp = (uint32_t *) (ph + 1);
1016 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_2);
1017 					ippp++;
1018 					*ippp = chk->rec.data.TSN_seq;
1019 					ippp++;
1020 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1021 
1022 				}
1023 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_2;
1024 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1025 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1026 				*abort_flag = 1;
1027 			} else if (asoc->fragmented_delivery_inprogress &&
1028 			    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1029 				/*
1030 				 * We are doing a partial delivery and the
1031 				 * NEXT chunk MUST be either the LAST or
1032 				 * MIDDLE fragment NOT a FIRST
1033 				 */
1034 				SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS a first and fragmented delivery in progress\n");
1035 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1036 				    0, M_DONTWAIT, 1, MT_DATA);
1037 				if (oper) {
1038 					struct sctp_paramhdr *ph;
1039 					uint32_t *ippp;
1040 
1041 					SCTP_BUF_LEN(oper) =
1042 					    sizeof(struct sctp_paramhdr) +
1043 					    (3 * sizeof(uint32_t));
1044 					ph = mtod(oper, struct sctp_paramhdr *);
1045 					ph->param_type =
1046 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1047 					ph->param_length = htons(SCTP_BUF_LEN(oper));
1048 					ippp = (uint32_t *) (ph + 1);
1049 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_3);
1050 					ippp++;
1051 					*ippp = chk->rec.data.TSN_seq;
1052 					ippp++;
1053 					*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1054 				}
1055 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_3;
1056 				sctp_abort_an_association(stcb->sctp_ep, stcb,
1057 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1058 				*abort_flag = 1;
1059 			} else if (asoc->fragmented_delivery_inprogress) {
1060 				/*
1061 				 * Here we are ok with a MIDDLE or LAST
1062 				 * piece
1063 				 */
1064 				if (chk->rec.data.stream_number !=
1065 				    asoc->str_of_pdapi) {
1066 					/* Got to be the right STR No */
1067 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream number %d vs %d\n",
1068 					    chk->rec.data.stream_number,
1069 					    asoc->str_of_pdapi);
1070 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1071 					    0, M_DONTWAIT, 1, MT_DATA);
1072 					if (oper) {
1073 						struct sctp_paramhdr *ph;
1074 						uint32_t *ippp;
1075 
1076 						SCTP_BUF_LEN(oper) =
1077 						    sizeof(struct sctp_paramhdr) +
1078 						    (sizeof(uint32_t) * 3);
1079 						ph = mtod(oper,
1080 						    struct sctp_paramhdr *);
1081 						ph->param_type =
1082 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1083 						ph->param_length =
1084 						    htons(SCTP_BUF_LEN(oper));
1085 						ippp = (uint32_t *) (ph + 1);
1086 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_4);
1087 						ippp++;
1088 						*ippp = chk->rec.data.TSN_seq;
1089 						ippp++;
1090 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1091 					}
1092 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_4;
1093 					sctp_abort_an_association(stcb->sctp_ep,
1094 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1095 					*abort_flag = 1;
1096 				} else if ((asoc->fragment_flags & SCTP_DATA_UNORDERED) !=
1097 					    SCTP_DATA_UNORDERED &&
1098 					    chk->rec.data.stream_seq !=
1099 				    asoc->ssn_of_pdapi) {
1100 					/* Got to be the right STR Seq */
1101 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it IS not same stream seq %d vs %d\n",
1102 					    chk->rec.data.stream_seq,
1103 					    asoc->ssn_of_pdapi);
1104 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1105 					    0, M_DONTWAIT, 1, MT_DATA);
1106 					if (oper) {
1107 						struct sctp_paramhdr *ph;
1108 						uint32_t *ippp;
1109 
1110 						SCTP_BUF_LEN(oper) =
1111 						    sizeof(struct sctp_paramhdr) +
1112 						    (3 * sizeof(uint32_t));
1113 						ph = mtod(oper,
1114 						    struct sctp_paramhdr *);
1115 						ph->param_type =
1116 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1117 						ph->param_length =
1118 						    htons(SCTP_BUF_LEN(oper));
1119 						ippp = (uint32_t *) (ph + 1);
1120 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_5);
1121 						ippp++;
1122 						*ippp = chk->rec.data.TSN_seq;
1123 						ippp++;
1124 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1125 
1126 					}
1127 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_5;
1128 					sctp_abort_an_association(stcb->sctp_ep,
1129 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1130 					*abort_flag = 1;
1131 				}
1132 			}
1133 		}
1134 		return;
1135 	}
1136 	/* Find its place */
1137 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1138 		if (compare_with_wrap(at->rec.data.TSN_seq,
1139 		    chk->rec.data.TSN_seq, MAX_TSN)) {
1140 			/*
1141 			 * one in queue is bigger than the new one, insert
1142 			 * before this one
1143 			 */
1144 			/* A check */
1145 			asoc->size_on_reasm_queue += chk->send_size;
1146 			sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1147 			next = at;
1148 			TAILQ_INSERT_BEFORE(at, chk, sctp_next);
1149 			break;
1150 		} else if (at->rec.data.TSN_seq == chk->rec.data.TSN_seq) {
1151 			/* Gak, He sent me a duplicate str seq number */
1152 			/*
1153 			 * foo bar, I guess I will just free this new guy,
1154 			 * should we abort too? FIX ME MAYBE? Or it COULD be
1155 			 * that the SSN's have wrapped. Maybe I should
1156 			 * compare to TSN somehow... sigh for now just blow
1157 			 * away the chunk!
1158 			 */
1159 			if (chk->data) {
1160 				sctp_m_freem(chk->data);
1161 				chk->data = NULL;
1162 			}
1163 			sctp_free_a_chunk(stcb, chk);
1164 			return;
1165 		} else {
1166 			last_flags = at->rec.data.rcv_flags;
1167 			last_tsn = at->rec.data.TSN_seq;
1168 			prev = at;
1169 			if (TAILQ_NEXT(at, sctp_next) == NULL) {
1170 				/*
1171 				 * We are at the end, insert it after this
1172 				 * one
1173 				 */
1174 				/* check it first */
1175 				asoc->size_on_reasm_queue += chk->send_size;
1176 				sctp_ucount_incr(asoc->cnt_on_reasm_queue);
1177 				TAILQ_INSERT_AFTER(&asoc->reasmqueue, at, chk, sctp_next);
1178 				break;
1179 			}
1180 		}
1181 	}
1182 	/* Now the audits */
1183 	if (prev) {
1184 		prev_tsn = chk->rec.data.TSN_seq - 1;
1185 		if (prev_tsn == prev->rec.data.TSN_seq) {
1186 			/*
1187 			 * Ok the one I am dropping onto the end is the
1188 			 * NEXT. A bit of valdiation here.
1189 			 */
1190 			if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1191 			    SCTP_DATA_FIRST_FRAG ||
1192 			    (prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1193 			    SCTP_DATA_MIDDLE_FRAG) {
1194 				/*
1195 				 * Insert chk MUST be a MIDDLE or LAST
1196 				 * fragment
1197 				 */
1198 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1199 				    SCTP_DATA_FIRST_FRAG) {
1200 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - It can be a midlle or last but not a first\n");
1201 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, it's a FIRST!\n");
1202 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1203 					    0, M_DONTWAIT, 1, MT_DATA);
1204 					if (oper) {
1205 						struct sctp_paramhdr *ph;
1206 						uint32_t *ippp;
1207 
1208 						SCTP_BUF_LEN(oper) =
1209 						    sizeof(struct sctp_paramhdr) +
1210 						    (3 * sizeof(uint32_t));
1211 						ph = mtod(oper,
1212 						    struct sctp_paramhdr *);
1213 						ph->param_type =
1214 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1215 						ph->param_length =
1216 						    htons(SCTP_BUF_LEN(oper));
1217 						ippp = (uint32_t *) (ph + 1);
1218 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_6);
1219 						ippp++;
1220 						*ippp = chk->rec.data.TSN_seq;
1221 						ippp++;
1222 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1223 
1224 					}
1225 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_6;
1226 					sctp_abort_an_association(stcb->sctp_ep,
1227 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1228 					*abort_flag = 1;
1229 					return;
1230 				}
1231 				if (chk->rec.data.stream_number !=
1232 				    prev->rec.data.stream_number) {
1233 					/*
1234 					 * Huh, need the correct STR here,
1235 					 * they must be the same.
1236 					 */
1237 					SCTP_PRINTF("Prev check - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1238 					    chk->rec.data.stream_number,
1239 					    prev->rec.data.stream_number);
1240 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1241 					    0, M_DONTWAIT, 1, MT_DATA);
1242 					if (oper) {
1243 						struct sctp_paramhdr *ph;
1244 						uint32_t *ippp;
1245 
1246 						SCTP_BUF_LEN(oper) =
1247 						    sizeof(struct sctp_paramhdr) +
1248 						    (3 * sizeof(uint32_t));
1249 						ph = mtod(oper,
1250 						    struct sctp_paramhdr *);
1251 						ph->param_type =
1252 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1253 						ph->param_length =
1254 						    htons(SCTP_BUF_LEN(oper));
1255 						ippp = (uint32_t *) (ph + 1);
1256 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_7);
1257 						ippp++;
1258 						*ippp = chk->rec.data.TSN_seq;
1259 						ippp++;
1260 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1261 					}
1262 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_7;
1263 					sctp_abort_an_association(stcb->sctp_ep,
1264 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1265 
1266 					*abort_flag = 1;
1267 					return;
1268 				}
1269 				if ((prev->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1270 				    chk->rec.data.stream_seq !=
1271 				    prev->rec.data.stream_seq) {
1272 					/*
1273 					 * Huh, need the correct STR here,
1274 					 * they must be the same.
1275 					 */
1276 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1277 					    chk->rec.data.stream_seq,
1278 					    prev->rec.data.stream_seq);
1279 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1280 					    0, M_DONTWAIT, 1, MT_DATA);
1281 					if (oper) {
1282 						struct sctp_paramhdr *ph;
1283 						uint32_t *ippp;
1284 
1285 						SCTP_BUF_LEN(oper) =
1286 						    sizeof(struct sctp_paramhdr) +
1287 						    (3 * sizeof(uint32_t));
1288 						ph = mtod(oper,
1289 						    struct sctp_paramhdr *);
1290 						ph->param_type =
1291 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1292 						ph->param_length =
1293 						    htons(SCTP_BUF_LEN(oper));
1294 						ippp = (uint32_t *) (ph + 1);
1295 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_8);
1296 						ippp++;
1297 						*ippp = chk->rec.data.TSN_seq;
1298 						ippp++;
1299 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1300 					}
1301 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_8;
1302 					sctp_abort_an_association(stcb->sctp_ep,
1303 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1304 
1305 					*abort_flag = 1;
1306 					return;
1307 				}
1308 			} else if ((prev->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1309 			    SCTP_DATA_LAST_FRAG) {
1310 				/* Insert chk MUST be a FIRST */
1311 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1312 				    SCTP_DATA_FIRST_FRAG) {
1313 					SCTPDBG(SCTP_DEBUG_INDATA1, "Prev check - Gak, evil plot, its not FIRST and it must be!\n");
1314 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1315 					    0, M_DONTWAIT, 1, MT_DATA);
1316 					if (oper) {
1317 						struct sctp_paramhdr *ph;
1318 						uint32_t *ippp;
1319 
1320 						SCTP_BUF_LEN(oper) =
1321 						    sizeof(struct sctp_paramhdr) +
1322 						    (3 * sizeof(uint32_t));
1323 						ph = mtod(oper,
1324 						    struct sctp_paramhdr *);
1325 						ph->param_type =
1326 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1327 						ph->param_length =
1328 						    htons(SCTP_BUF_LEN(oper));
1329 						ippp = (uint32_t *) (ph + 1);
1330 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_9);
1331 						ippp++;
1332 						*ippp = chk->rec.data.TSN_seq;
1333 						ippp++;
1334 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1335 
1336 					}
1337 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_9;
1338 					sctp_abort_an_association(stcb->sctp_ep,
1339 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1340 
1341 					*abort_flag = 1;
1342 					return;
1343 				}
1344 			}
1345 		}
1346 	}
1347 	if (next) {
1348 		post_tsn = chk->rec.data.TSN_seq + 1;
1349 		if (post_tsn == next->rec.data.TSN_seq) {
1350 			/*
1351 			 * Ok the one I am inserting ahead of is my NEXT
1352 			 * one. A bit of valdiation here.
1353 			 */
1354 			if (next->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) {
1355 				/* Insert chk MUST be a last fragment */
1356 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK)
1357 				    != SCTP_DATA_LAST_FRAG) {
1358 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is FIRST, we must be LAST\n");
1359 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, its not a last!\n");
1360 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1361 					    0, M_DONTWAIT, 1, MT_DATA);
1362 					if (oper) {
1363 						struct sctp_paramhdr *ph;
1364 						uint32_t *ippp;
1365 
1366 						SCTP_BUF_LEN(oper) =
1367 						    sizeof(struct sctp_paramhdr) +
1368 						    (3 * sizeof(uint32_t));
1369 						ph = mtod(oper,
1370 						    struct sctp_paramhdr *);
1371 						ph->param_type =
1372 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1373 						ph->param_length =
1374 						    htons(SCTP_BUF_LEN(oper));
1375 						ippp = (uint32_t *) (ph + 1);
1376 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_10);
1377 						ippp++;
1378 						*ippp = chk->rec.data.TSN_seq;
1379 						ippp++;
1380 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1381 					}
1382 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_10;
1383 					sctp_abort_an_association(stcb->sctp_ep,
1384 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1385 
1386 					*abort_flag = 1;
1387 					return;
1388 				}
1389 			} else if ((next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1390 				    SCTP_DATA_MIDDLE_FRAG ||
1391 				    (next->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1392 			    SCTP_DATA_LAST_FRAG) {
1393 				/*
1394 				 * Insert chk CAN be MIDDLE or FIRST NOT
1395 				 * LAST
1396 				 */
1397 				if ((chk->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) ==
1398 				    SCTP_DATA_LAST_FRAG) {
1399 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Next is a MIDDLE/LAST\n");
1400 					SCTPDBG(SCTP_DEBUG_INDATA1, "Gak, Evil plot, new prev chunk is a LAST\n");
1401 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1402 					    0, M_DONTWAIT, 1, MT_DATA);
1403 					if (oper) {
1404 						struct sctp_paramhdr *ph;
1405 						uint32_t *ippp;
1406 
1407 						SCTP_BUF_LEN(oper) =
1408 						    sizeof(struct sctp_paramhdr) +
1409 						    (3 * sizeof(uint32_t));
1410 						ph = mtod(oper,
1411 						    struct sctp_paramhdr *);
1412 						ph->param_type =
1413 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1414 						ph->param_length =
1415 						    htons(SCTP_BUF_LEN(oper));
1416 						ippp = (uint32_t *) (ph + 1);
1417 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_11);
1418 						ippp++;
1419 						*ippp = chk->rec.data.TSN_seq;
1420 						ippp++;
1421 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1422 
1423 					}
1424 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_11;
1425 					sctp_abort_an_association(stcb->sctp_ep,
1426 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1427 
1428 					*abort_flag = 1;
1429 					return;
1430 				}
1431 				if (chk->rec.data.stream_number !=
1432 				    next->rec.data.stream_number) {
1433 					/*
1434 					 * Huh, need the correct STR here,
1435 					 * they must be the same.
1436 					 */
1437 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, ssn:%d not the same as at:%d\n",
1438 					    chk->rec.data.stream_number,
1439 					    next->rec.data.stream_number);
1440 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1441 					    0, M_DONTWAIT, 1, MT_DATA);
1442 					if (oper) {
1443 						struct sctp_paramhdr *ph;
1444 						uint32_t *ippp;
1445 
1446 						SCTP_BUF_LEN(oper) =
1447 						    sizeof(struct sctp_paramhdr) +
1448 						    (3 * sizeof(uint32_t));
1449 						ph = mtod(oper,
1450 						    struct sctp_paramhdr *);
1451 						ph->param_type =
1452 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1453 						ph->param_length =
1454 						    htons(SCTP_BUF_LEN(oper));
1455 						ippp = (uint32_t *) (ph + 1);
1456 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_12);
1457 						ippp++;
1458 						*ippp = chk->rec.data.TSN_seq;
1459 						ippp++;
1460 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1461 
1462 					}
1463 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_12;
1464 					sctp_abort_an_association(stcb->sctp_ep,
1465 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1466 
1467 					*abort_flag = 1;
1468 					return;
1469 				}
1470 				if ((next->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0 &&
1471 				    chk->rec.data.stream_seq !=
1472 				    next->rec.data.stream_seq) {
1473 					/*
1474 					 * Huh, need the correct STR here,
1475 					 * they must be the same.
1476 					 */
1477 					SCTPDBG(SCTP_DEBUG_INDATA1, "Next chk - Gak, Evil plot, sseq:%d not the same as at:%d\n",
1478 					    chk->rec.data.stream_seq,
1479 					    next->rec.data.stream_seq);
1480 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1481 					    0, M_DONTWAIT, 1, MT_DATA);
1482 					if (oper) {
1483 						struct sctp_paramhdr *ph;
1484 						uint32_t *ippp;
1485 
1486 						SCTP_BUF_LEN(oper) =
1487 						    sizeof(struct sctp_paramhdr) +
1488 						    (3 * sizeof(uint32_t));
1489 						ph = mtod(oper,
1490 						    struct sctp_paramhdr *);
1491 						ph->param_type =
1492 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1493 						ph->param_length =
1494 						    htons(SCTP_BUF_LEN(oper));
1495 						ippp = (uint32_t *) (ph + 1);
1496 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_13);
1497 						ippp++;
1498 						*ippp = chk->rec.data.TSN_seq;
1499 						ippp++;
1500 						*ippp = ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq);
1501 					}
1502 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_13;
1503 					sctp_abort_an_association(stcb->sctp_ep,
1504 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1505 
1506 					*abort_flag = 1;
1507 					return;
1508 				}
1509 			}
1510 		}
1511 	}
1512 	/* Do we need to do some delivery? check */
1513 	sctp_deliver_reasm_check(stcb, asoc);
1514 }
1515 
1516 /*
1517  * This is an unfortunate routine. It checks to make sure a evil guy is not
1518  * stuffing us full of bad packet fragments. A broken peer could also do this
1519  * but this is doubtful. It is to bad I must worry about evil crackers sigh
1520  * :< more cycles.
1521  */
1522 static int
1523 sctp_does_tsn_belong_to_reasm(struct sctp_association *asoc,
1524     uint32_t TSN_seq)
1525 {
1526 	struct sctp_tmit_chunk *at;
1527 	uint32_t tsn_est;
1528 
1529 	TAILQ_FOREACH(at, &asoc->reasmqueue, sctp_next) {
1530 		if (compare_with_wrap(TSN_seq,
1531 		    at->rec.data.TSN_seq, MAX_TSN)) {
1532 			/* is it one bigger? */
1533 			tsn_est = at->rec.data.TSN_seq + 1;
1534 			if (tsn_est == TSN_seq) {
1535 				/* yep. It better be a last then */
1536 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1537 				    SCTP_DATA_LAST_FRAG) {
1538 					/*
1539 					 * Ok this guy belongs next to a guy
1540 					 * that is NOT last, it should be a
1541 					 * middle/last, not a complete
1542 					 * chunk.
1543 					 */
1544 					return (1);
1545 				} else {
1546 					/*
1547 					 * This guy is ok since its a LAST
1548 					 * and the new chunk is a fully
1549 					 * self- contained one.
1550 					 */
1551 					return (0);
1552 				}
1553 			}
1554 		} else if (TSN_seq == at->rec.data.TSN_seq) {
1555 			/* Software error since I have a dup? */
1556 			return (1);
1557 		} else {
1558 			/*
1559 			 * Ok, 'at' is larger than new chunk but does it
1560 			 * need to be right before it.
1561 			 */
1562 			tsn_est = TSN_seq + 1;
1563 			if (tsn_est == at->rec.data.TSN_seq) {
1564 				/* Yep, It better be a first */
1565 				if ((at->rec.data.rcv_flags & SCTP_DATA_FRAG_MASK) !=
1566 				    SCTP_DATA_FIRST_FRAG) {
1567 					return (1);
1568 				} else {
1569 					return (0);
1570 				}
1571 			}
1572 		}
1573 	}
1574 	return (0);
1575 }
1576 
1577 
1578 static int
1579 sctp_process_a_data_chunk(struct sctp_tcb *stcb, struct sctp_association *asoc,
1580     struct mbuf **m, int offset, struct sctp_data_chunk *ch, int chk_length,
1581     struct sctp_nets *net, uint32_t * high_tsn, int *abort_flag,
1582     int *break_flag, int last_chunk)
1583 {
1584 	/* Process a data chunk */
1585 	/* struct sctp_tmit_chunk *chk; */
1586 	struct sctp_tmit_chunk *chk;
1587 	uint32_t tsn, gap;
1588 
1589 	/* EY - for nr_sack */
1590 	uint32_t nr_gap;
1591 	struct mbuf *dmbuf;
1592 	int indx, the_len;
1593 	int need_reasm_check = 0;
1594 	uint16_t strmno, strmseq;
1595 	struct mbuf *oper;
1596 	struct sctp_queued_to_read *control;
1597 	int ordered;
1598 	uint32_t protocol_id;
1599 	uint8_t chunk_flags;
1600 	struct sctp_stream_reset_list *liste;
1601 
1602 	chk = NULL;
1603 	tsn = ntohl(ch->dp.tsn);
1604 	chunk_flags = ch->ch.chunk_flags;
1605 	if ((chunk_flags & SCTP_DATA_SACK_IMMEDIATELY) == SCTP_DATA_SACK_IMMEDIATELY) {
1606 		asoc->send_sack = 1;
1607 	}
1608 	protocol_id = ch->dp.protocol_id;
1609 	ordered = ((ch->ch.chunk_flags & SCTP_DATA_UNORDERED) == 0);
1610 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1611 		sctp_log_map(tsn, asoc->cumulative_tsn, asoc->highest_tsn_inside_map, SCTP_MAP_TSN_ENTERS);
1612 	}
1613 	if (stcb == NULL) {
1614 		return (0);
1615 	}
1616 	SCTP_LTRACE_CHK(stcb->sctp_ep, stcb, ch->ch.chunk_type, tsn);
1617 	if (compare_with_wrap(asoc->cumulative_tsn, tsn, MAX_TSN) ||
1618 	    asoc->cumulative_tsn == tsn) {
1619 		/* It is a duplicate */
1620 		SCTP_STAT_INCR(sctps_recvdupdata);
1621 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1622 			/* Record a dup for the next outbound sack */
1623 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1624 			asoc->numduptsns++;
1625 		}
1626 		asoc->send_sack = 1;
1627 		return (0);
1628 	}
1629 	/* Calculate the number of TSN's between the base and this TSN */
1630 	SCTP_CALC_TSN_TO_GAP(gap, tsn, asoc->mapping_array_base_tsn);
1631 	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1632 		/* Can't hold the bit in the mapping at max array, toss it */
1633 		return (0);
1634 	}
1635 	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1636 		SCTP_TCB_LOCK_ASSERT(stcb);
1637 		if (sctp_expand_mapping_array(asoc, gap)) {
1638 			/* Can't expand, drop it */
1639 			return (0);
1640 		}
1641 	}
1642 	/* EY - for nr_sack */
1643 	nr_gap = gap;
1644 
1645 	if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1646 		*high_tsn = tsn;
1647 	}
1648 	/* See if we have received this one already */
1649 	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
1650 		SCTP_STAT_INCR(sctps_recvdupdata);
1651 		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1652 			/* Record a dup for the next outbound sack */
1653 			asoc->dup_tsns[asoc->numduptsns] = tsn;
1654 			asoc->numduptsns++;
1655 		}
1656 		asoc->send_sack = 1;
1657 		return (0);
1658 	}
1659 	/*
1660 	 * Check to see about the GONE flag, duplicates would cause a sack
1661 	 * to be sent up above
1662 	 */
1663 	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1664 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1665 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1666 	    ) {
1667 		/*
1668 		 * wait a minute, this guy is gone, there is no longer a
1669 		 * receiver. Send peer an ABORT!
1670 		 */
1671 		struct mbuf *op_err;
1672 
1673 		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1674 		sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1675 		*abort_flag = 1;
1676 		return (0);
1677 	}
1678 	/*
1679 	 * Now before going further we see if there is room. If NOT then we
1680 	 * MAY let one through only IF this TSN is the one we are waiting
1681 	 * for on a partial delivery API.
1682 	 */
1683 
1684 	/* now do the tests */
1685 	if (((asoc->cnt_on_all_streams +
1686 	    asoc->cnt_on_reasm_queue +
1687 	    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) ||
1688 	    (((int)asoc->my_rwnd) <= 0)) {
1689 		/*
1690 		 * When we have NO room in the rwnd we check to make sure
1691 		 * the reader is doing its job...
1692 		 */
1693 		if (stcb->sctp_socket->so_rcv.sb_cc) {
1694 			/* some to read, wake-up */
1695 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1696 			struct socket *so;
1697 
1698 			so = SCTP_INP_SO(stcb->sctp_ep);
1699 			atomic_add_int(&stcb->asoc.refcnt, 1);
1700 			SCTP_TCB_UNLOCK(stcb);
1701 			SCTP_SOCKET_LOCK(so, 1);
1702 			SCTP_TCB_LOCK(stcb);
1703 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1704 			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1705 				/* assoc was freed while we were unlocked */
1706 				SCTP_SOCKET_UNLOCK(so, 1);
1707 				return (0);
1708 			}
1709 #endif
1710 			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1711 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1712 			SCTP_SOCKET_UNLOCK(so, 1);
1713 #endif
1714 		}
1715 		/* now is it in the mapping array of what we have accepted? */
1716 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1717 			/* Nope not in the valid range dump it */
1718 			sctp_set_rwnd(stcb, asoc);
1719 			if ((asoc->cnt_on_all_streams +
1720 			    asoc->cnt_on_reasm_queue +
1721 			    asoc->cnt_msg_on_sb) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue)) {
1722 				SCTP_STAT_INCR(sctps_datadropchklmt);
1723 			} else {
1724 				SCTP_STAT_INCR(sctps_datadroprwnd);
1725 			}
1726 			indx = *break_flag;
1727 			*break_flag = 1;
1728 			return (0);
1729 		}
1730 	}
1731 	strmno = ntohs(ch->dp.stream_id);
1732 	if (strmno >= asoc->streamincnt) {
1733 		struct sctp_paramhdr *phdr;
1734 		struct mbuf *mb;
1735 
1736 		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1737 		    0, M_DONTWAIT, 1, MT_DATA);
1738 		if (mb != NULL) {
1739 			/* add some space up front so prepend will work well */
1740 			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1741 			phdr = mtod(mb, struct sctp_paramhdr *);
1742 			/*
1743 			 * Error causes are just param's and this one has
1744 			 * two back to back phdr, one with the error type
1745 			 * and size, the other with the streamid and a rsvd
1746 			 */
1747 			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1748 			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1749 			phdr->param_length =
1750 			    htons(sizeof(struct sctp_paramhdr) * 2);
1751 			phdr++;
1752 			/* We insert the stream in the type field */
1753 			phdr->param_type = ch->dp.stream_id;
1754 			/* And set the length to 0 for the rsvd field */
1755 			phdr->param_length = 0;
1756 			sctp_queue_op_err(stcb, mb);
1757 		}
1758 		SCTP_STAT_INCR(sctps_badsid);
1759 		SCTP_TCB_LOCK_ASSERT(stcb);
1760 		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1761 		/* EY set this tsn present in  nr_sack's nr_mapping_array */
1762 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
1763 			SCTP_TCB_LOCK_ASSERT(stcb);
1764 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
1765 			SCTP_REVERSE_OUT_TSN_PRES(gap, tsn, asoc);
1766 		}
1767 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1768 			/* we have a new high score */
1769 			asoc->highest_tsn_inside_map = tsn;
1770 			/* EY nr_sack version of the above */
1771 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
1772 				asoc->highest_tsn_inside_nr_map = tsn;
1773 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
1774 				sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
1775 			}
1776 		}
1777 		if (tsn == (asoc->cumulative_tsn + 1)) {
1778 			/* Update cum-ack */
1779 			asoc->cumulative_tsn = tsn;
1780 		}
1781 		return (0);
1782 	}
1783 	/*
1784 	 * Before we continue lets validate that we are not being fooled by
1785 	 * an evil attacker. We can only have 4k chunks based on our TSN
1786 	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1787 	 * way our stream sequence numbers could have wrapped. We of course
1788 	 * only validate the FIRST fragment so the bit must be set.
1789 	 */
1790 	strmseq = ntohs(ch->dp.stream_sequence);
1791 #ifdef SCTP_ASOCLOG_OF_TSNS
1792 	SCTP_TCB_LOCK_ASSERT(stcb);
1793 	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
1794 		asoc->tsn_in_at = 0;
1795 		asoc->tsn_in_wrapped = 1;
1796 	}
1797 	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1798 	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1799 	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1800 	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1801 	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
1802 	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
1803 	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
1804 	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1805 	asoc->tsn_in_at++;
1806 #endif
1807 	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1808 	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1809 	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1810 	    (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1811 	    strmseq, MAX_SEQ) ||
1812 	    asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1813 		/* The incoming sseq is behind where we last delivered? */
1814 		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1815 		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1816 		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1817 		    0, M_DONTWAIT, 1, MT_DATA);
1818 		if (oper) {
1819 			struct sctp_paramhdr *ph;
1820 			uint32_t *ippp;
1821 
1822 			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1823 			    (3 * sizeof(uint32_t));
1824 			ph = mtod(oper, struct sctp_paramhdr *);
1825 			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1826 			ph->param_length = htons(SCTP_BUF_LEN(oper));
1827 			ippp = (uint32_t *) (ph + 1);
1828 			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1829 			ippp++;
1830 			*ippp = tsn;
1831 			ippp++;
1832 			*ippp = ((strmno << 16) | strmseq);
1833 
1834 		}
1835 		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1836 		sctp_abort_an_association(stcb->sctp_ep, stcb,
1837 		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1838 		*abort_flag = 1;
1839 		return (0);
1840 	}
1841 	/************************************
1842 	 * From here down we may find ch-> invalid
1843 	 * so its a good idea NOT to use it.
1844 	 *************************************/
1845 
1846 	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1847 	if (last_chunk == 0) {
1848 		dmbuf = SCTP_M_COPYM(*m,
1849 		    (offset + sizeof(struct sctp_data_chunk)),
1850 		    the_len, M_DONTWAIT);
1851 #ifdef SCTP_MBUF_LOGGING
1852 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
1853 			struct mbuf *mat;
1854 
1855 			mat = dmbuf;
1856 			while (mat) {
1857 				if (SCTP_BUF_IS_EXTENDED(mat)) {
1858 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1859 				}
1860 				mat = SCTP_BUF_NEXT(mat);
1861 			}
1862 		}
1863 #endif
1864 	} else {
1865 		/* We can steal the last chunk */
1866 		int l_len;
1867 
1868 		dmbuf = *m;
1869 		/* lop off the top part */
1870 		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1871 		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1872 			l_len = SCTP_BUF_LEN(dmbuf);
1873 		} else {
1874 			/*
1875 			 * need to count up the size hopefully does not hit
1876 			 * this to often :-0
1877 			 */
1878 			struct mbuf *lat;
1879 
1880 			l_len = 0;
1881 			lat = dmbuf;
1882 			while (lat) {
1883 				l_len += SCTP_BUF_LEN(lat);
1884 				lat = SCTP_BUF_NEXT(lat);
1885 			}
1886 		}
1887 		if (l_len > the_len) {
1888 			/* Trim the end round bytes off  too */
1889 			m_adj(dmbuf, -(l_len - the_len));
1890 		}
1891 	}
1892 	if (dmbuf == NULL) {
1893 		SCTP_STAT_INCR(sctps_nomem);
1894 		return (0);
1895 	}
1896 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1897 	    asoc->fragmented_delivery_inprogress == 0 &&
1898 	    TAILQ_EMPTY(&asoc->resetHead) &&
1899 	    ((ordered == 0) ||
1900 	    ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1901 	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1902 		/* Candidate for express delivery */
1903 		/*
1904 		 * Its not fragmented, No PD-API is up, Nothing in the
1905 		 * delivery queue, Its un-ordered OR ordered and the next to
1906 		 * deliver AND nothing else is stuck on the stream queue,
1907 		 * And there is room for it in the socket buffer. Lets just
1908 		 * stuff it up the buffer....
1909 		 */
1910 
1911 		/* It would be nice to avoid this copy if we could :< */
1912 		sctp_alloc_a_readq(stcb, control);
1913 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1914 		    protocol_id,
1915 		    stcb->asoc.context,
1916 		    strmno, strmseq,
1917 		    chunk_flags,
1918 		    dmbuf);
1919 		if (control == NULL) {
1920 			goto failed_express_del;
1921 		}
1922 		sctp_add_to_readq(stcb->sctp_ep, stcb,
1923 		    control, &stcb->sctp_socket->so_rcv,
1924 		    1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
1925 
1926 		/*
1927 		 * EY here I should check if this delivered tsn is
1928 		 * out_of_order, if yes then update the nr_map
1929 		 */
1930 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
1931 			/*
1932 			 * EY check if the mapping_array and nr_mapping
1933 			 * array are consistent
1934 			 */
1935 			if (asoc->mapping_array_base_tsn != asoc->nr_mapping_array_base_tsn)
1936 				/*
1937 				 * printf("EY-IN
1938 				 * sctp_process_a_data_chunk(5): Something
1939 				 * is wrong the map base tsn" "\nEY-and
1940 				 * nr_map base tsn should be equal.");
1941 				 */
1942 				/* EY debugging block */
1943 			{
1944 				/*
1945 				 * printf("\nEY-Calculating an
1946 				 * nr_gap!!\nmapping_array_size = %d
1947 				 * nr_mapping_array_size = %d"
1948 				 * "\nEY-mapping_array_base = %d
1949 				 * nr_mapping_array_base =
1950 				 * %d\nEY-highest_tsn_inside_map = %d"
1951 				 * "highest_tsn_inside_nr_map = %d\nEY-TSN =
1952 				 * %d nr_gap = %d",asoc->mapping_array_size,
1953 				 * asoc->nr_mapping_array_size,
1954 				 * asoc->mapping_array_base_tsn,
1955 				 * asoc->nr_mapping_array_base_tsn,
1956 				 * asoc->highest_tsn_inside_map,
1957 				 * asoc->highest_tsn_inside_nr_map,tsn,nr_gap
1958 				 * );
1959 				 */
1960 			}
1961 			/* EY - not %100 sure about the lock thing */
1962 			SCTP_TCB_LOCK_ASSERT(stcb);
1963 			SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
1964 			SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
1965 			if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
1966 				asoc->highest_tsn_inside_nr_map = tsn;
1967 		}
1968 		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1969 			/* for ordered, bump what we delivered */
1970 			asoc->strmin[strmno].last_sequence_delivered++;
1971 		}
1972 		SCTP_STAT_INCR(sctps_recvexpress);
1973 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
1974 			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1975 			    SCTP_STR_LOG_FROM_EXPRS_DEL);
1976 		}
1977 		control = NULL;
1978 		goto finish_express_del;
1979 	}
1980 failed_express_del:
1981 	/* If we reach here this is a new chunk */
1982 	chk = NULL;
1983 	control = NULL;
1984 	/* Express for fragmented delivery? */
1985 	if ((asoc->fragmented_delivery_inprogress) &&
1986 	    (stcb->asoc.control_pdapi) &&
1987 	    (asoc->str_of_pdapi == strmno) &&
1988 	    (asoc->ssn_of_pdapi == strmseq)
1989 	    ) {
1990 		control = stcb->asoc.control_pdapi;
1991 		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1992 			/* Can't be another first? */
1993 			goto failed_pdapi_express_del;
1994 		}
1995 		if (tsn == (control->sinfo_tsn + 1)) {
1996 			/* Yep, we can add it on */
1997 			int end = 0;
1998 			uint32_t cumack;
1999 
2000 			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
2001 				end = 1;
2002 			}
2003 			cumack = asoc->cumulative_tsn;
2004 			if ((cumack + 1) == tsn)
2005 				cumack = tsn;
2006 
2007 			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
2008 			    tsn,
2009 			    &stcb->sctp_socket->so_rcv)) {
2010 				SCTP_PRINTF("Append fails end:%d\n", end);
2011 				goto failed_pdapi_express_del;
2012 			}
2013 			/*
2014 			 * EY It is appended to the read queue in prev if
2015 			 * block here I should check if this delivered tsn
2016 			 * is out_of_order, if yes then update the nr_map
2017 			 */
2018 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2019 				/* EY debugging block */
2020 				{
2021 					/*
2022 					 * printf("\nEY-Calculating an
2023 					 * nr_gap!!\nEY-mapping_array_size =
2024 					 * %d nr_mapping_array_size = %d"
2025 					 * "\nEY-mapping_array_base = %d
2026 					 * nr_mapping_array_base =
2027 					 * %d\nEY-highest_tsn_inside_map =
2028 					 * %d" "highest_tsn_inside_nr_map =
2029 					 * %d\nEY-TSN = %d nr_gap =
2030 					 * %d",asoc->mapping_array_size,
2031 					 * asoc->nr_mapping_array_size,
2032 					 * asoc->mapping_array_base_tsn,
2033 					 * asoc->nr_mapping_array_base_tsn,
2034 					 * asoc->highest_tsn_inside_map,
2035 					 * asoc->highest_tsn_inside_nr_map,ts
2036 					 * n,nr_gap);
2037 					 */
2038 				}
2039 				/* EY - not %100 sure about the lock thing */
2040 				SCTP_TCB_LOCK_ASSERT(stcb);
2041 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
2042 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
2043 				if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
2044 					asoc->highest_tsn_inside_nr_map = tsn;
2045 			}
2046 			SCTP_STAT_INCR(sctps_recvexpressm);
2047 			control->sinfo_tsn = tsn;
2048 			asoc->tsn_last_delivered = tsn;
2049 			asoc->fragment_flags = chunk_flags;
2050 			asoc->tsn_of_pdapi_last_delivered = tsn;
2051 			asoc->last_flags_delivered = chunk_flags;
2052 			asoc->last_strm_seq_delivered = strmseq;
2053 			asoc->last_strm_no_delivered = strmno;
2054 			if (end) {
2055 				/* clean up the flags and such */
2056 				asoc->fragmented_delivery_inprogress = 0;
2057 				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
2058 					asoc->strmin[strmno].last_sequence_delivered++;
2059 				}
2060 				stcb->asoc.control_pdapi = NULL;
2061 				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
2062 					/*
2063 					 * There could be another message
2064 					 * ready
2065 					 */
2066 					need_reasm_check = 1;
2067 				}
2068 			}
2069 			control = NULL;
2070 			goto finish_express_del;
2071 		}
2072 	}
2073 failed_pdapi_express_del:
2074 	control = NULL;
2075 	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
2076 		sctp_alloc_a_chunk(stcb, chk);
2077 		if (chk == NULL) {
2078 			/* No memory so we drop the chunk */
2079 			SCTP_STAT_INCR(sctps_nomem);
2080 			if (last_chunk == 0) {
2081 				/* we copied it, free the copy */
2082 				sctp_m_freem(dmbuf);
2083 			}
2084 			return (0);
2085 		}
2086 		chk->rec.data.TSN_seq = tsn;
2087 		chk->no_fr_allowed = 0;
2088 		chk->rec.data.stream_seq = strmseq;
2089 		chk->rec.data.stream_number = strmno;
2090 		chk->rec.data.payloadtype = protocol_id;
2091 		chk->rec.data.context = stcb->asoc.context;
2092 		chk->rec.data.doing_fast_retransmit = 0;
2093 		chk->rec.data.rcv_flags = chunk_flags;
2094 		chk->asoc = asoc;
2095 		chk->send_size = the_len;
2096 		chk->whoTo = net;
2097 		atomic_add_int(&net->ref_count, 1);
2098 		chk->data = dmbuf;
2099 	} else {
2100 		sctp_alloc_a_readq(stcb, control);
2101 		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
2102 		    protocol_id,
2103 		    stcb->asoc.context,
2104 		    strmno, strmseq,
2105 		    chunk_flags,
2106 		    dmbuf);
2107 		if (control == NULL) {
2108 			/* No memory so we drop the chunk */
2109 			SCTP_STAT_INCR(sctps_nomem);
2110 			if (last_chunk == 0) {
2111 				/* we copied it, free the copy */
2112 				sctp_m_freem(dmbuf);
2113 			}
2114 			return (0);
2115 		}
2116 		control->length = the_len;
2117 	}
2118 
2119 	/* Mark it as received */
2120 	/* Now queue it where it belongs */
2121 	if (control != NULL) {
2122 		/* First a sanity check */
2123 		if (asoc->fragmented_delivery_inprogress) {
2124 			/*
2125 			 * Ok, we have a fragmented delivery in progress if
2126 			 * this chunk is next to deliver OR belongs in our
2127 			 * view to the reassembly, the peer is evil or
2128 			 * broken.
2129 			 */
2130 			uint32_t estimate_tsn;
2131 
2132 			estimate_tsn = asoc->tsn_last_delivered + 1;
2133 			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
2134 			    (estimate_tsn == control->sinfo_tsn)) {
2135 				/* Evil/Broke peer */
2136 				sctp_m_freem(control->data);
2137 				control->data = NULL;
2138 				if (control->whoFrom) {
2139 					sctp_free_remote_addr(control->whoFrom);
2140 					control->whoFrom = NULL;
2141 				}
2142 				sctp_free_a_readq(stcb, control);
2143 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2144 				    0, M_DONTWAIT, 1, MT_DATA);
2145 				if (oper) {
2146 					struct sctp_paramhdr *ph;
2147 					uint32_t *ippp;
2148 
2149 					SCTP_BUF_LEN(oper) =
2150 					    sizeof(struct sctp_paramhdr) +
2151 					    (3 * sizeof(uint32_t));
2152 					ph = mtod(oper, struct sctp_paramhdr *);
2153 					ph->param_type =
2154 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2155 					ph->param_length = htons(SCTP_BUF_LEN(oper));
2156 					ippp = (uint32_t *) (ph + 1);
2157 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
2158 					ippp++;
2159 					*ippp = tsn;
2160 					ippp++;
2161 					*ippp = ((strmno << 16) | strmseq);
2162 				}
2163 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
2164 				sctp_abort_an_association(stcb->sctp_ep, stcb,
2165 				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2166 
2167 				*abort_flag = 1;
2168 				return (0);
2169 			} else {
2170 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2171 					sctp_m_freem(control->data);
2172 					control->data = NULL;
2173 					if (control->whoFrom) {
2174 						sctp_free_remote_addr(control->whoFrom);
2175 						control->whoFrom = NULL;
2176 					}
2177 					sctp_free_a_readq(stcb, control);
2178 
2179 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2180 					    0, M_DONTWAIT, 1, MT_DATA);
2181 					if (oper) {
2182 						struct sctp_paramhdr *ph;
2183 						uint32_t *ippp;
2184 
2185 						SCTP_BUF_LEN(oper) =
2186 						    sizeof(struct sctp_paramhdr) +
2187 						    (3 * sizeof(uint32_t));
2188 						ph = mtod(oper,
2189 						    struct sctp_paramhdr *);
2190 						ph->param_type =
2191 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2192 						ph->param_length =
2193 						    htons(SCTP_BUF_LEN(oper));
2194 						ippp = (uint32_t *) (ph + 1);
2195 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
2196 						ippp++;
2197 						*ippp = tsn;
2198 						ippp++;
2199 						*ippp = ((strmno << 16) | strmseq);
2200 					}
2201 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
2202 					sctp_abort_an_association(stcb->sctp_ep,
2203 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2204 
2205 					*abort_flag = 1;
2206 					return (0);
2207 				}
2208 			}
2209 		} else {
2210 			/* No PDAPI running */
2211 			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
2212 				/*
2213 				 * Reassembly queue is NOT empty validate
2214 				 * that this tsn does not need to be in
2215 				 * reasembly queue. If it does then our peer
2216 				 * is broken or evil.
2217 				 */
2218 				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
2219 					sctp_m_freem(control->data);
2220 					control->data = NULL;
2221 					if (control->whoFrom) {
2222 						sctp_free_remote_addr(control->whoFrom);
2223 						control->whoFrom = NULL;
2224 					}
2225 					sctp_free_a_readq(stcb, control);
2226 					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
2227 					    0, M_DONTWAIT, 1, MT_DATA);
2228 					if (oper) {
2229 						struct sctp_paramhdr *ph;
2230 						uint32_t *ippp;
2231 
2232 						SCTP_BUF_LEN(oper) =
2233 						    sizeof(struct sctp_paramhdr) +
2234 						    (3 * sizeof(uint32_t));
2235 						ph = mtod(oper,
2236 						    struct sctp_paramhdr *);
2237 						ph->param_type =
2238 						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2239 						ph->param_length =
2240 						    htons(SCTP_BUF_LEN(oper));
2241 						ippp = (uint32_t *) (ph + 1);
2242 						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2243 						ippp++;
2244 						*ippp = tsn;
2245 						ippp++;
2246 						*ippp = ((strmno << 16) | strmseq);
2247 					}
2248 					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2249 					sctp_abort_an_association(stcb->sctp_ep,
2250 					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2251 
2252 					*abort_flag = 1;
2253 					return (0);
2254 				}
2255 			}
2256 		}
2257 		/* ok, if we reach here we have passed the sanity checks */
2258 		if (chunk_flags & SCTP_DATA_UNORDERED) {
2259 			/* queue directly into socket buffer */
2260 			sctp_add_to_readq(stcb->sctp_ep, stcb,
2261 			    control,
2262 			    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, SCTP_SO_NOT_LOCKED);
2263 
2264 			/*
2265 			 * EY It is added to the read queue in prev if block
2266 			 * here I should check if this delivered tsn is
2267 			 * out_of_order, if yes then update the nr_map
2268 			 */
2269 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2270 				/*
2271 				 * EY check if the mapping_array and
2272 				 * nr_mapping array are consistent
2273 				 */
2274 				if (asoc->mapping_array_base_tsn != asoc->nr_mapping_array_base_tsn)
2275 					/*
2276 					 * printf("EY-IN
2277 					 * sctp_process_a_data_chunk(6):
2278 					 * Something is wrong the map base
2279 					 * tsn" "\nEY-and nr_map base tsn
2280 					 * should be equal.");
2281 					 */
2282 					/*
2283 					 * EY - not %100 sure about the lock
2284 					 * thing, i think we don't need the
2285 					 * below,
2286 					 */
2287 					/* SCTP_TCB_LOCK_ASSERT(stcb); */
2288 				{
2289 					/*
2290 					 * printf("\nEY-Calculating an
2291 					 * nr_gap!!\nEY-mapping_array_size =
2292 					 * %d nr_mapping_array_size = %d"
2293 					 * "\nEY-mapping_array_base = %d
2294 					 * nr_mapping_array_base =
2295 					 * %d\nEY-highest_tsn_inside_map =
2296 					 * %d" "highest_tsn_inside_nr_map =
2297 					 * %d\nEY-TSN = %d nr_gap =
2298 					 * %d",asoc->mapping_array_size,
2299 					 * asoc->nr_mapping_array_size,
2300 					 * asoc->mapping_array_base_tsn,
2301 					 * asoc->nr_mapping_array_base_tsn,
2302 					 * asoc->highest_tsn_inside_map,
2303 					 * asoc->highest_tsn_inside_nr_map,ts
2304 					 * n,nr_gap);
2305 					 */
2306 				}
2307 				SCTP_TCB_LOCK_ASSERT(stcb);
2308 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
2309 				SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
2310 				if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN))
2311 					asoc->highest_tsn_inside_nr_map = tsn;
2312 			}
2313 		} else {
2314 			/*
2315 			 * Special check for when streams are resetting. We
2316 			 * could be more smart about this and check the
2317 			 * actual stream to see if it is not being reset..
2318 			 * that way we would not create a HOLB when amongst
2319 			 * streams being reset and those not being reset.
2320 			 *
2321 			 * We take complete messages that have a stream reset
2322 			 * intervening (aka the TSN is after where our
2323 			 * cum-ack needs to be) off and put them on a
2324 			 * pending_reply_queue. The reassembly ones we do
2325 			 * not have to worry about since they are all sorted
2326 			 * and proceessed by TSN order. It is only the
2327 			 * singletons I must worry about.
2328 			 */
2329 			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2330 			    ((compare_with_wrap(tsn, liste->tsn, MAX_TSN)))
2331 			    ) {
2332 				/*
2333 				 * yep its past where we need to reset... go
2334 				 * ahead and queue it.
2335 				 */
2336 				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2337 					/* first one on */
2338 					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2339 				} else {
2340 					struct sctp_queued_to_read *ctlOn;
2341 					unsigned char inserted = 0;
2342 
2343 					ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2344 					while (ctlOn) {
2345 						if (compare_with_wrap(control->sinfo_tsn,
2346 						    ctlOn->sinfo_tsn, MAX_TSN)) {
2347 							ctlOn = TAILQ_NEXT(ctlOn, next);
2348 						} else {
2349 							/* found it */
2350 							TAILQ_INSERT_BEFORE(ctlOn, control, next);
2351 							inserted = 1;
2352 							break;
2353 						}
2354 					}
2355 					if (inserted == 0) {
2356 						/*
2357 						 * must be put at end, use
2358 						 * prevP (all setup from
2359 						 * loop) to setup nextP.
2360 						 */
2361 						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2362 					}
2363 				}
2364 			} else {
2365 				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2366 				if (*abort_flag) {
2367 					return (0);
2368 				}
2369 			}
2370 		}
2371 	} else {
2372 		/* Into the re-assembly queue */
2373 		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2374 		if (*abort_flag) {
2375 			/*
2376 			 * the assoc is now gone and chk was put onto the
2377 			 * reasm queue, which has all been freed.
2378 			 */
2379 			*m = NULL;
2380 			return (0);
2381 		}
2382 	}
2383 finish_express_del:
2384 	if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
2385 		/* we have a new high score */
2386 		asoc->highest_tsn_inside_map = tsn;
2387 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2388 			sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2389 		}
2390 	}
2391 	if (tsn == (asoc->cumulative_tsn + 1)) {
2392 		/* Update cum-ack */
2393 		asoc->cumulative_tsn = tsn;
2394 	}
2395 	if (last_chunk) {
2396 		*m = NULL;
2397 	}
2398 	if (ordered) {
2399 		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2400 	} else {
2401 		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2402 	}
2403 	SCTP_STAT_INCR(sctps_recvdata);
2404 	/* Set it present please */
2405 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_STR_LOGGING_ENABLE) {
2406 		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
2407 	}
2408 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2409 		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2410 		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
2411 	}
2412 	SCTP_TCB_LOCK_ASSERT(stcb);
2413 	SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
2414 
2415 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
2416 	    asoc->peer_supports_nr_sack &&
2417 	    (SCTP_BASE_SYSCTL(sctp_do_drain) == 0)) {
2418 		SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, gap);
2419 		SCTP_REVERSE_OUT_TSN_PRES(nr_gap, tsn, asoc);
2420 		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
2421 			asoc->highest_tsn_inside_nr_map = tsn;
2422 		}
2423 	}
2424 	/* check the special flag for stream resets */
2425 	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2426 	    ((compare_with_wrap(asoc->cumulative_tsn, liste->tsn, MAX_TSN)) ||
2427 	    (asoc->cumulative_tsn == liste->tsn))
2428 	    ) {
2429 		/*
2430 		 * we have finished working through the backlogged TSN's now
2431 		 * time to reset streams. 1: call reset function. 2: free
2432 		 * pending_reply space 3: distribute any chunks in
2433 		 * pending_reply_queue.
2434 		 */
2435 		struct sctp_queued_to_read *ctl;
2436 
2437 		sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
2438 		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2439 		SCTP_FREE(liste, SCTP_M_STRESET);
2440 		/* sa_ignore FREED_MEMORY */
2441 		liste = TAILQ_FIRST(&asoc->resetHead);
2442 		ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2443 		if (ctl && (liste == NULL)) {
2444 			/* All can be removed */
2445 			while (ctl) {
2446 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2447 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2448 				if (*abort_flag) {
2449 					return (0);
2450 				}
2451 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2452 			}
2453 		} else if (ctl) {
2454 			/* more than one in queue */
2455 			while (!compare_with_wrap(ctl->sinfo_tsn, liste->tsn, MAX_TSN)) {
2456 				/*
2457 				 * if ctl->sinfo_tsn is <= liste->tsn we can
2458 				 * process it which is the NOT of
2459 				 * ctl->sinfo_tsn > liste->tsn
2460 				 */
2461 				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2462 				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2463 				if (*abort_flag) {
2464 					return (0);
2465 				}
2466 				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2467 			}
2468 		}
2469 		/*
2470 		 * Now service re-assembly to pick up anything that has been
2471 		 * held on reassembly queue?
2472 		 */
2473 		sctp_deliver_reasm_check(stcb, asoc);
2474 		need_reasm_check = 0;
2475 	}
2476 	if (need_reasm_check) {
2477 		/* Another one waits ? */
2478 		sctp_deliver_reasm_check(stcb, asoc);
2479 	}
2480 	return (1);
2481 }
2482 
2483 int8_t sctp_map_lookup_tab[256] = {
2484 	-1, 0, -1, 1, -1, 0, -1, 2,
2485 	-1, 0, -1, 1, -1, 0, -1, 3,
2486 	-1, 0, -1, 1, -1, 0, -1, 2,
2487 	-1, 0, -1, 1, -1, 0, -1, 4,
2488 	-1, 0, -1, 1, -1, 0, -1, 2,
2489 	-1, 0, -1, 1, -1, 0, -1, 3,
2490 	-1, 0, -1, 1, -1, 0, -1, 2,
2491 	-1, 0, -1, 1, -1, 0, -1, 5,
2492 	-1, 0, -1, 1, -1, 0, -1, 2,
2493 	-1, 0, -1, 1, -1, 0, -1, 3,
2494 	-1, 0, -1, 1, -1, 0, -1, 2,
2495 	-1, 0, -1, 1, -1, 0, -1, 4,
2496 	-1, 0, -1, 1, -1, 0, -1, 2,
2497 	-1, 0, -1, 1, -1, 0, -1, 3,
2498 	-1, 0, -1, 1, -1, 0, -1, 2,
2499 	-1, 0, -1, 1, -1, 0, -1, 6,
2500 	-1, 0, -1, 1, -1, 0, -1, 2,
2501 	-1, 0, -1, 1, -1, 0, -1, 3,
2502 	-1, 0, -1, 1, -1, 0, -1, 2,
2503 	-1, 0, -1, 1, -1, 0, -1, 4,
2504 	-1, 0, -1, 1, -1, 0, -1, 2,
2505 	-1, 0, -1, 1, -1, 0, -1, 3,
2506 	-1, 0, -1, 1, -1, 0, -1, 2,
2507 	-1, 0, -1, 1, -1, 0, -1, 5,
2508 	-1, 0, -1, 1, -1, 0, -1, 2,
2509 	-1, 0, -1, 1, -1, 0, -1, 3,
2510 	-1, 0, -1, 1, -1, 0, -1, 2,
2511 	-1, 0, -1, 1, -1, 0, -1, 4,
2512 	-1, 0, -1, 1, -1, 0, -1, 2,
2513 	-1, 0, -1, 1, -1, 0, -1, 3,
2514 	-1, 0, -1, 1, -1, 0, -1, 2,
2515 	-1, 0, -1, 1, -1, 0, -1, 7,
2516 };
2517 
2518 
2519 void
2520 sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2521 {
2522 	/*
2523 	 * Now we also need to check the mapping array in a couple of ways.
2524 	 * 1) Did we move the cum-ack point?
2525 	 */
2526 	struct sctp_association *asoc;
2527 	int at;
2528 	int last_all_ones = 0;
2529 	int slide_from, slide_end, lgap, distance;
2530 
2531 	/* EY nr_mapping array variables */
2532 	/* int nr_at; */
2533 	/* int nr_last_all_ones = 0; */
2534 	/* int nr_slide_from, nr_slide_end, nr_lgap, nr_distance; */
2535 
2536 	uint32_t old_cumack, old_base, old_highest;
2537 	unsigned char aux_array[64];
2538 
2539 	/*
2540 	 * EY! Don't think this is required but I am immitating the code for
2541 	 * map just to make sure
2542 	 */
2543 	unsigned char nr_aux_array[64];
2544 
2545 	asoc = &stcb->asoc;
2546 	at = 0;
2547 
2548 	old_cumack = asoc->cumulative_tsn;
2549 	old_base = asoc->mapping_array_base_tsn;
2550 	old_highest = asoc->highest_tsn_inside_map;
2551 	if (asoc->mapping_array_size < 64)
2552 		memcpy(aux_array, asoc->mapping_array,
2553 		    asoc->mapping_array_size);
2554 	else
2555 		memcpy(aux_array, asoc->mapping_array, 64);
2556 	/* EY do the same for nr_mapping_array */
2557 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2558 
2559 		if (asoc->nr_mapping_array_size != asoc->mapping_array_size) {
2560 			/*
2561 			 * printf("\nEY-IN sack_check method: \nEY-" "The
2562 			 * size of map and nr_map are inconsitent")
2563 			 */ ;
2564 		}
2565 		if (asoc->nr_mapping_array_base_tsn != asoc->mapping_array_base_tsn) {
2566 			/*
2567 			 * printf("\nEY-IN sack_check method VERY CRUCIAL
2568 			 * error: \nEY-" "The base tsns of map and nr_map
2569 			 * are inconsitent")
2570 			 */ ;
2571 		}
2572 		/* EY! just immitating the above code */
2573 		if (asoc->nr_mapping_array_size < 64)
2574 			memcpy(nr_aux_array, asoc->nr_mapping_array,
2575 			    asoc->nr_mapping_array_size);
2576 		else
2577 			memcpy(aux_array, asoc->nr_mapping_array, 64);
2578 	}
2579 	/*
2580 	 * We could probably improve this a small bit by calculating the
2581 	 * offset of the current cum-ack as the starting point.
2582 	 */
2583 	at = 0;
2584 	for (slide_from = 0; slide_from < stcb->asoc.mapping_array_size; slide_from++) {
2585 
2586 		if (asoc->mapping_array[slide_from] == 0xff) {
2587 			at += 8;
2588 			last_all_ones = 1;
2589 		} else {
2590 			/* there is a 0 bit */
2591 			at += sctp_map_lookup_tab[asoc->mapping_array[slide_from]];
2592 			last_all_ones = 0;
2593 			break;
2594 		}
2595 	}
2596 	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - last_all_ones);
2597 	/* at is one off, since in the table a embedded -1 is present */
2598 	at++;
2599 
2600 	if (compare_with_wrap(asoc->cumulative_tsn,
2601 	    asoc->highest_tsn_inside_map,
2602 	    MAX_TSN)) {
2603 #ifdef INVARIANTS
2604 		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2605 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2606 #else
2607 		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2608 		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2609 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2610 			sctp_log_map(0, 6, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2611 		}
2612 		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2613 		asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2614 #endif
2615 	}
2616 	if ((asoc->cumulative_tsn == asoc->highest_tsn_inside_map) && (at >= 8)) {
2617 		/* The complete array was completed by a single FR */
2618 		/* higest becomes the cum-ack */
2619 		int clr;
2620 
2621 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
2622 		/* clear the array */
2623 		clr = (at >> 3) + 1;
2624 		if (clr > asoc->mapping_array_size) {
2625 			clr = asoc->mapping_array_size;
2626 		}
2627 		memset(asoc->mapping_array, 0, clr);
2628 		/* base becomes one ahead of the cum-ack */
2629 		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2630 
2631 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2632 
2633 			if (clr > asoc->nr_mapping_array_size)
2634 				clr = asoc->nr_mapping_array_size;
2635 
2636 			memset(asoc->nr_mapping_array, 0, clr);
2637 			/* base becomes one ahead of the cum-ack */
2638 			asoc->nr_mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2639 			asoc->highest_tsn_inside_nr_map = asoc->cumulative_tsn;
2640 		}
2641 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2642 			sctp_log_map(old_base, old_cumack, old_highest,
2643 			    SCTP_MAP_PREPARE_SLIDE);
2644 			sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2645 			    asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
2646 		}
2647 	} else if (at >= 8) {
2648 		/* we can slide the mapping array down */
2649 		/* slide_from holds where we hit the first NON 0xff byte */
2650 
2651 		/*
2652 		 * now calculate the ceiling of the move using our highest
2653 		 * TSN value
2654 		 */
2655 		if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
2656 			lgap = asoc->highest_tsn_inside_map -
2657 			    asoc->mapping_array_base_tsn;
2658 		} else {
2659 			lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
2660 			    asoc->highest_tsn_inside_map + 1;
2661 		}
2662 		slide_end = lgap >> 3;
2663 		if (slide_end < slide_from) {
2664 #ifdef INVARIANTS
2665 			panic("impossible slide");
2666 #else
2667 			printf("impossible slide?\n");
2668 			return;
2669 #endif
2670 		}
2671 		if (slide_end > asoc->mapping_array_size) {
2672 #ifdef INVARIANTS
2673 			panic("would overrun buffer");
2674 #else
2675 			printf("Gak, would have overrun map end:%d slide_end:%d\n",
2676 			    asoc->mapping_array_size, slide_end);
2677 			slide_end = asoc->mapping_array_size;
2678 #endif
2679 		}
2680 		distance = (slide_end - slide_from) + 1;
2681 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2682 			sctp_log_map(old_base, old_cumack, old_highest,
2683 			    SCTP_MAP_PREPARE_SLIDE);
2684 			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2685 			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
2686 		}
2687 		if (distance + slide_from > asoc->mapping_array_size ||
2688 		    distance < 0) {
2689 			/*
2690 			 * Here we do NOT slide forward the array so that
2691 			 * hopefully when more data comes in to fill it up
2692 			 * we will be able to slide it forward. Really I
2693 			 * don't think this should happen :-0
2694 			 */
2695 
2696 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2697 				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2698 				    (uint32_t) asoc->mapping_array_size,
2699 				    SCTP_MAP_SLIDE_NONE);
2700 			}
2701 		} else {
2702 			int ii;
2703 
2704 			for (ii = 0; ii < distance; ii++) {
2705 				asoc->mapping_array[ii] =
2706 				    asoc->mapping_array[slide_from + ii];
2707 			}
2708 			for (ii = distance; ii <= slide_end; ii++) {
2709 				asoc->mapping_array[ii] = 0;
2710 			}
2711 			asoc->mapping_array_base_tsn += (slide_from << 3);
2712 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
2713 				sctp_log_map(asoc->mapping_array_base_tsn,
2714 				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2715 				    SCTP_MAP_SLIDE_RESULT);
2716 			}
2717 			/*
2718 			 * EY if doing nr_sacks then slide the
2719 			 * nr_mapping_array accordingly please
2720 			 */
2721 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
2722 				for (ii = 0; ii < distance; ii++) {
2723 					asoc->nr_mapping_array[ii] =
2724 					    asoc->nr_mapping_array[slide_from + ii];
2725 				}
2726 				for (ii = distance; ii <= slide_end; ii++) {
2727 					asoc->nr_mapping_array[ii] = 0;
2728 				}
2729 				asoc->nr_mapping_array_base_tsn += (slide_from << 3);
2730 			}
2731 		}
2732 	}
2733 	/*
2734 	 * Now we need to see if we need to queue a sack or just start the
2735 	 * timer (if allowed).
2736 	 */
2737 	if (ok_to_sack) {
2738 		if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2739 			/*
2740 			 * Ok special case, in SHUTDOWN-SENT case. here we
2741 			 * maker sure SACK timer is off and instead send a
2742 			 * SHUTDOWN and a SACK
2743 			 */
2744 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2745 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2746 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2747 			}
2748 			sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2749 			/*
2750 			 * EY if nr_sacks used then send an nr-sack , a sack
2751 			 * otherwise
2752 			 */
2753 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2754 				sctp_send_nr_sack(stcb);
2755 			else
2756 				sctp_send_sack(stcb);
2757 		} else {
2758 			int is_a_gap;
2759 
2760 			/* is there a gap now ? */
2761 			is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2762 			    stcb->asoc.cumulative_tsn, MAX_TSN);
2763 
2764 			/*
2765 			 * CMT DAC algorithm: increase number of packets
2766 			 * received since last ack
2767 			 */
2768 			stcb->asoc.cmt_dac_pkts_rcvd++;
2769 
2770 			if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
2771 								 * SACK */
2772 			    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2773 								 * longer is one */
2774 			    (stcb->asoc.numduptsns) ||	/* we have dup's */
2775 			    (is_a_gap) ||	/* is still a gap */
2776 			    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
2777 			    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2778 			    ) {
2779 
2780 				if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off)) &&
2781 				    (SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) &&
2782 				    (stcb->asoc.send_sack == 0) &&
2783 				    (stcb->asoc.numduptsns == 0) &&
2784 				    (stcb->asoc.delayed_ack) &&
2785 				    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2786 
2787 					/*
2788 					 * CMT DAC algorithm: With CMT,
2789 					 * delay acks even in the face of
2790 					 *
2791 					 * reordering. Therefore, if acks that
2792 					 * do not have to be sent because of
2793 					 * the above reasons, will be
2794 					 * delayed. That is, acks that would
2795 					 * have been sent due to gap reports
2796 					 * will be delayed with DAC. Start
2797 					 * the delayed ack timer.
2798 					 */
2799 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2800 					    stcb->sctp_ep, stcb, NULL);
2801 				} else {
2802 					/*
2803 					 * Ok we must build a SACK since the
2804 					 * timer is pending, we got our
2805 					 * first packet OR there are gaps or
2806 					 * duplicates.
2807 					 */
2808 					(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2809 					/*
2810 					 * EY if nr_sacks used then send an
2811 					 * nr-sack , a sack otherwise
2812 					 */
2813 					if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
2814 						sctp_send_nr_sack(stcb);
2815 					else
2816 						sctp_send_sack(stcb);
2817 				}
2818 			} else {
2819 				if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2820 					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2821 					    stcb->sctp_ep, stcb, NULL);
2822 				}
2823 			}
2824 		}
2825 	}
2826 }
2827 
2828 void
2829 sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2830 {
2831 	struct sctp_tmit_chunk *chk;
2832 	uint32_t tsize, pd_point;
2833 	uint16_t nxt_todel;
2834 
2835 	if (asoc->fragmented_delivery_inprogress) {
2836 		sctp_service_reassembly(stcb, asoc);
2837 	}
2838 	/* Can we proceed further, i.e. the PD-API is complete */
2839 	if (asoc->fragmented_delivery_inprogress) {
2840 		/* no */
2841 		return;
2842 	}
2843 	/*
2844 	 * Now is there some other chunk I can deliver from the reassembly
2845 	 * queue.
2846 	 */
2847 doit_again:
2848 	chk = TAILQ_FIRST(&asoc->reasmqueue);
2849 	if (chk == NULL) {
2850 		asoc->size_on_reasm_queue = 0;
2851 		asoc->cnt_on_reasm_queue = 0;
2852 		return;
2853 	}
2854 	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2855 	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2856 	    ((nxt_todel == chk->rec.data.stream_seq) ||
2857 	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2858 		/*
2859 		 * Yep the first one is here. We setup to start reception,
2860 		 * by backing down the TSN just in case we can't deliver.
2861 		 */
2862 
2863 		/*
2864 		 * Before we start though either all of the message should
2865 		 * be here or 1/4 the socket buffer max or nothing on the
2866 		 * delivery queue and something can be delivered.
2867 		 */
2868 		if (stcb->sctp_socket) {
2869 			pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
2870 			    stcb->sctp_ep->partial_delivery_point);
2871 		} else {
2872 			pd_point = stcb->sctp_ep->partial_delivery_point;
2873 		}
2874 		if (sctp_is_all_msg_on_reasm(asoc, &tsize) || (tsize >= pd_point)) {
2875 			asoc->fragmented_delivery_inprogress = 1;
2876 			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2877 			asoc->str_of_pdapi = chk->rec.data.stream_number;
2878 			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2879 			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2880 			asoc->fragment_flags = chk->rec.data.rcv_flags;
2881 			sctp_service_reassembly(stcb, asoc);
2882 			if (asoc->fragmented_delivery_inprogress == 0) {
2883 				goto doit_again;
2884 			}
2885 		}
2886 	}
2887 }
2888 
2889 int
2890 sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2891     struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2892     struct sctp_nets *net, uint32_t * high_tsn)
2893 {
2894 	struct sctp_data_chunk *ch, chunk_buf;
2895 	struct sctp_association *asoc;
2896 	int num_chunks = 0;	/* number of control chunks processed */
2897 	int stop_proc = 0;
2898 	int chk_length, break_flag, last_chunk;
2899 	int abort_flag = 0, was_a_gap = 0;
2900 	struct mbuf *m;
2901 
2902 	/* set the rwnd */
2903 	sctp_set_rwnd(stcb, &stcb->asoc);
2904 
2905 	m = *mm;
2906 	SCTP_TCB_LOCK_ASSERT(stcb);
2907 	asoc = &stcb->asoc;
2908 	if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2909 	    stcb->asoc.cumulative_tsn, MAX_TSN)) {
2910 		/* there was a gap before this data was processed */
2911 		was_a_gap = 1;
2912 	}
2913 	/*
2914 	 * setup where we got the last DATA packet from for any SACK that
2915 	 * may need to go out. Don't bump the net. This is done ONLY when a
2916 	 * chunk is assigned.
2917 	 */
2918 	asoc->last_data_chunk_from = net;
2919 
2920 	/*-
2921 	 * Now before we proceed we must figure out if this is a wasted
2922 	 * cluster... i.e. it is a small packet sent in and yet the driver
2923 	 * underneath allocated a full cluster for it. If so we must copy it
2924 	 * to a smaller mbuf and free up the cluster mbuf. This will help
2925 	 * with cluster starvation. Note for __Panda__ we don't do this
2926 	 * since it has clusters all the way down to 64 bytes.
2927 	 */
2928 	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2929 		/* we only handle mbufs that are singletons.. not chains */
2930 		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2931 		if (m) {
2932 			/* ok lets see if we can copy the data up */
2933 			caddr_t *from, *to;
2934 
2935 			/* get the pointers and copy */
2936 			to = mtod(m, caddr_t *);
2937 			from = mtod((*mm), caddr_t *);
2938 			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2939 			/* copy the length and free up the old */
2940 			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2941 			sctp_m_freem(*mm);
2942 			/* sucess, back copy */
2943 			*mm = m;
2944 		} else {
2945 			/* We are in trouble in the mbuf world .. yikes */
2946 			m = *mm;
2947 		}
2948 	}
2949 	/* get pointer to the first chunk header */
2950 	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2951 	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2952 	if (ch == NULL) {
2953 		return (1);
2954 	}
2955 	/*
2956 	 * process all DATA chunks...
2957 	 */
2958 	*high_tsn = asoc->cumulative_tsn;
2959 	break_flag = 0;
2960 	asoc->data_pkts_seen++;
2961 	while (stop_proc == 0) {
2962 		/* validate chunk length */
2963 		chk_length = ntohs(ch->ch.chunk_length);
2964 		if (length - *offset < chk_length) {
2965 			/* all done, mutulated chunk */
2966 			stop_proc = 1;
2967 			break;
2968 		}
2969 		if (ch->ch.chunk_type == SCTP_DATA) {
2970 			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2971 				/*
2972 				 * Need to send an abort since we had a
2973 				 * invalid data chunk.
2974 				 */
2975 				struct mbuf *op_err;
2976 
2977 				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2978 				    0, M_DONTWAIT, 1, MT_DATA);
2979 
2980 				if (op_err) {
2981 					struct sctp_paramhdr *ph;
2982 					uint32_t *ippp;
2983 
2984 					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2985 					    (2 * sizeof(uint32_t));
2986 					ph = mtod(op_err, struct sctp_paramhdr *);
2987 					ph->param_type =
2988 					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2989 					ph->param_length = htons(SCTP_BUF_LEN(op_err));
2990 					ippp = (uint32_t *) (ph + 1);
2991 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2992 					ippp++;
2993 					*ippp = asoc->cumulative_tsn;
2994 
2995 				}
2996 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2997 				sctp_abort_association(inp, stcb, m, iphlen, sh,
2998 				    op_err, 0, net->port);
2999 				return (2);
3000 			}
3001 #ifdef SCTP_AUDITING_ENABLED
3002 			sctp_audit_log(0xB1, 0);
3003 #endif
3004 			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
3005 				last_chunk = 1;
3006 			} else {
3007 				last_chunk = 0;
3008 			}
3009 			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
3010 			    chk_length, net, high_tsn, &abort_flag, &break_flag,
3011 			    last_chunk)) {
3012 				num_chunks++;
3013 			}
3014 			if (abort_flag)
3015 				return (2);
3016 
3017 			if (break_flag) {
3018 				/*
3019 				 * Set because of out of rwnd space and no
3020 				 * drop rep space left.
3021 				 */
3022 				stop_proc = 1;
3023 				break;
3024 			}
3025 		} else {
3026 			/* not a data chunk in the data region */
3027 			switch (ch->ch.chunk_type) {
3028 			case SCTP_INITIATION:
3029 			case SCTP_INITIATION_ACK:
3030 			case SCTP_SELECTIVE_ACK:
3031 			case SCTP_NR_SELECTIVE_ACK:	/* EY */
3032 			case SCTP_HEARTBEAT_REQUEST:
3033 			case SCTP_HEARTBEAT_ACK:
3034 			case SCTP_ABORT_ASSOCIATION:
3035 			case SCTP_SHUTDOWN:
3036 			case SCTP_SHUTDOWN_ACK:
3037 			case SCTP_OPERATION_ERROR:
3038 			case SCTP_COOKIE_ECHO:
3039 			case SCTP_COOKIE_ACK:
3040 			case SCTP_ECN_ECHO:
3041 			case SCTP_ECN_CWR:
3042 			case SCTP_SHUTDOWN_COMPLETE:
3043 			case SCTP_AUTHENTICATION:
3044 			case SCTP_ASCONF_ACK:
3045 			case SCTP_PACKET_DROPPED:
3046 			case SCTP_STREAM_RESET:
3047 			case SCTP_FORWARD_CUM_TSN:
3048 			case SCTP_ASCONF:
3049 				/*
3050 				 * Now, what do we do with KNOWN chunks that
3051 				 * are NOT in the right place?
3052 				 *
3053 				 * For now, I do nothing but ignore them. We
3054 				 * may later want to add sysctl stuff to
3055 				 * switch out and do either an ABORT() or
3056 				 * possibly process them.
3057 				 */
3058 				if (SCTP_BASE_SYSCTL(sctp_strict_data_order)) {
3059 					struct mbuf *op_err;
3060 
3061 					op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
3062 					sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0, net->port);
3063 					return (2);
3064 				}
3065 				break;
3066 			default:
3067 				/* unknown chunk type, use bit rules */
3068 				if (ch->ch.chunk_type & 0x40) {
3069 					/* Add a error report to the queue */
3070 					struct mbuf *merr;
3071 					struct sctp_paramhdr *phd;
3072 
3073 					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
3074 					if (merr) {
3075 						phd = mtod(merr, struct sctp_paramhdr *);
3076 						/*
3077 						 * We cheat and use param
3078 						 * type since we did not
3079 						 * bother to define a error
3080 						 * cause struct. They are
3081 						 * the same basic format
3082 						 * with different names.
3083 						 */
3084 						phd->param_type =
3085 						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
3086 						phd->param_length =
3087 						    htons(chk_length + sizeof(*phd));
3088 						SCTP_BUF_LEN(merr) = sizeof(*phd);
3089 						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
3090 						    SCTP_SIZE32(chk_length),
3091 						    M_DONTWAIT);
3092 						if (SCTP_BUF_NEXT(merr)) {
3093 							sctp_queue_op_err(stcb, merr);
3094 						} else {
3095 							sctp_m_freem(merr);
3096 						}
3097 					}
3098 				}
3099 				if ((ch->ch.chunk_type & 0x80) == 0) {
3100 					/* discard the rest of this packet */
3101 					stop_proc = 1;
3102 				}	/* else skip this bad chunk and
3103 					 * continue... */
3104 				break;
3105 			};	/* switch of chunk type */
3106 		}
3107 		*offset += SCTP_SIZE32(chk_length);
3108 		if ((*offset >= length) || stop_proc) {
3109 			/* no more data left in the mbuf chain */
3110 			stop_proc = 1;
3111 			continue;
3112 		}
3113 		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
3114 		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
3115 		if (ch == NULL) {
3116 			*offset = length;
3117 			stop_proc = 1;
3118 			break;
3119 
3120 		}
3121 	}			/* while */
3122 	if (break_flag) {
3123 		/*
3124 		 * we need to report rwnd overrun drops.
3125 		 */
3126 		sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
3127 	}
3128 	if (num_chunks) {
3129 		/*
3130 		 * Did we get data, if so update the time for auto-close and
3131 		 * give peer credit for being alive.
3132 		 */
3133 		SCTP_STAT_INCR(sctps_recvpktwithdata);
3134 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
3135 			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3136 			    stcb->asoc.overall_error_count,
3137 			    0,
3138 			    SCTP_FROM_SCTP_INDATA,
3139 			    __LINE__);
3140 		}
3141 		stcb->asoc.overall_error_count = 0;
3142 		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
3143 	}
3144 	/* now service all of the reassm queue if needed */
3145 	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
3146 		sctp_service_queues(stcb, asoc);
3147 
3148 	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
3149 		/* Assure that we ack right away */
3150 		stcb->asoc.send_sack = 1;
3151 	}
3152 	/* Start a sack timer or QUEUE a SACK for sending */
3153 	if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
3154 	    (stcb->asoc.mapping_array[0] != 0xff)) {
3155 		if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
3156 		    (stcb->asoc.delayed_ack == 0) ||
3157 		    (stcb->asoc.numduptsns) ||
3158 		    (stcb->asoc.send_sack == 1)) {
3159 			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
3160 				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
3161 			}
3162 			/*
3163 			 * EY if nr_sacks used then send an nr-sack , a sack
3164 			 * otherwise
3165 			 */
3166 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && stcb->asoc.peer_supports_nr_sack)
3167 				sctp_send_nr_sack(stcb);
3168 			else
3169 				sctp_send_sack(stcb);
3170 		} else {
3171 			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
3172 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
3173 				    stcb->sctp_ep, stcb, NULL);
3174 			}
3175 		}
3176 	} else {
3177 		sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
3178 	}
3179 	if (abort_flag)
3180 		return (2);
3181 
3182 	return (0);
3183 }
3184 
3185 static void
3186 sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
3187     struct sctp_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
3188     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
3189     int num_seg, int *ecn_seg_sums)
3190 {
3191 	/************************************************/
3192 	/* process fragments and update sendqueue        */
3193 	/************************************************/
3194 	struct sctp_sack *sack;
3195 	struct sctp_gap_ack_block *frag, block;
3196 	struct sctp_tmit_chunk *tp1;
3197 	int i, j;
3198 	unsigned int theTSN;
3199 	int num_frs = 0;
3200 
3201 	uint16_t frag_strt, frag_end, primary_flag_set;
3202 	u_long last_frag_high;
3203 
3204 	/*
3205 	 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
3206 	 */
3207 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3208 		primary_flag_set = 1;
3209 	} else {
3210 		primary_flag_set = 0;
3211 	}
3212 	sack = &ch->sack;
3213 
3214 	frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3215 	    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3216 	*offset += sizeof(block);
3217 	if (frag == NULL) {
3218 		return;
3219 	}
3220 	tp1 = NULL;
3221 	last_frag_high = 0;
3222 	for (i = 0; i < num_seg; i++) {
3223 		frag_strt = ntohs(frag->start);
3224 		frag_end = ntohs(frag->end);
3225 		/* some sanity checks on the fragment offsets */
3226 		if (frag_strt > frag_end) {
3227 			/* this one is malformed, skip */
3228 			frag++;
3229 			continue;
3230 		}
3231 		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
3232 		    MAX_TSN))
3233 			*biggest_tsn_acked = frag_end + last_tsn;
3234 
3235 		/* mark acked dgs and find out the highestTSN being acked */
3236 		if (tp1 == NULL) {
3237 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
3238 
3239 			/* save the locations of the last frags */
3240 			last_frag_high = frag_end + last_tsn;
3241 		} else {
3242 			/*
3243 			 * now lets see if we need to reset the queue due to
3244 			 * a out-of-order SACK fragment
3245 			 */
3246 			if (compare_with_wrap(frag_strt + last_tsn,
3247 			    last_frag_high, MAX_TSN)) {
3248 				/*
3249 				 * if the new frag starts after the last TSN
3250 				 * frag covered, we are ok and this one is
3251 				 * beyond the last one
3252 				 */
3253 				;
3254 			} else {
3255 				/*
3256 				 * ok, they have reset us, so we need to
3257 				 * reset the queue this will cause extra
3258 				 * hunting but hey, they chose the
3259 				 * performance hit when they failed to order
3260 				 * their gaps
3261 				 */
3262 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
3263 			}
3264 			last_frag_high = frag_end + last_tsn;
3265 		}
3266 		for (j = frag_strt; j <= frag_end; j++) {
3267 			theTSN = j + last_tsn;
3268 			while (tp1) {
3269 				if (tp1->rec.data.doing_fast_retransmit)
3270 					num_frs++;
3271 
3272 				/*
3273 				 * CMT: CUCv2 algorithm. For each TSN being
3274 				 * processed from the sent queue, track the
3275 				 * next expected pseudo-cumack, or
3276 				 * rtx_pseudo_cumack, if required. Separate
3277 				 * cumack trackers for first transmissions,
3278 				 * and retransmissions.
3279 				 */
3280 				if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
3281 				    (tp1->snd_count == 1)) {
3282 					tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
3283 					tp1->whoTo->find_pseudo_cumack = 0;
3284 				}
3285 				if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
3286 				    (tp1->snd_count > 1)) {
3287 					tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
3288 					tp1->whoTo->find_rtx_pseudo_cumack = 0;
3289 				}
3290 				if (tp1->rec.data.TSN_seq == theTSN) {
3291 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
3292 						/*
3293 						 * must be held until
3294 						 * cum-ack passes
3295 						 */
3296 						/*
3297 						 * ECN Nonce: Add the nonce
3298 						 * value to the sender's
3299 						 * nonce sum
3300 						 */
3301 						if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3302 							/*-
3303 							 * If it is less than RESEND, it is
3304 							 * now no-longer in flight.
3305 							 * Higher values may already be set
3306 							 * via previous Gap Ack Blocks...
3307 							 * i.e. ACKED or RESEND.
3308 							 */
3309 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3310 							    *biggest_newly_acked_tsn, MAX_TSN)) {
3311 								*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
3312 							}
3313 							/*
3314 							 * CMT: SFR algo
3315 							 * (and HTNA) - set
3316 							 * saw_newack to 1
3317 							 * for dest being
3318 							 * newly acked.
3319 							 * update
3320 							 * this_sack_highest_
3321 							 * newack if
3322 							 * appropriate.
3323 							 */
3324 							if (tp1->rec.data.chunk_was_revoked == 0)
3325 								tp1->whoTo->saw_newack = 1;
3326 
3327 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3328 							    tp1->whoTo->this_sack_highest_newack,
3329 							    MAX_TSN)) {
3330 								tp1->whoTo->this_sack_highest_newack =
3331 								    tp1->rec.data.TSN_seq;
3332 							}
3333 							/*
3334 							 * CMT DAC algo:
3335 							 * also update
3336 							 * this_sack_lowest_n
3337 							 * ewack
3338 							 */
3339 							if (*this_sack_lowest_newack == 0) {
3340 								if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3341 									sctp_log_sack(*this_sack_lowest_newack,
3342 									    last_tsn,
3343 									    tp1->rec.data.TSN_seq,
3344 									    0,
3345 									    0,
3346 									    SCTP_LOG_TSN_ACKED);
3347 								}
3348 								*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
3349 							}
3350 							/*
3351 							 * CMT: CUCv2
3352 							 * algorithm. If
3353 							 * (rtx-)pseudo-cumac
3354 							 * k for corresp
3355 							 * dest is being
3356 							 * acked, then we
3357 							 * have a new
3358 							 * (rtx-)pseudo-cumac
3359 							 * k. Set
3360 							 * new_(rtx_)pseudo_c
3361 							 * umack to TRUE so
3362 							 * that the cwnd for
3363 							 * this dest can be
3364 							 * updated. Also
3365 							 * trigger search
3366 							 * for the next
3367 							 * expected
3368 							 * (rtx-)pseudo-cumac
3369 							 * k. Separate
3370 							 * pseudo_cumack
3371 							 * trackers for
3372 							 * first
3373 							 * transmissions and
3374 							 * retransmissions.
3375 							 */
3376 							if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
3377 								if (tp1->rec.data.chunk_was_revoked == 0) {
3378 									tp1->whoTo->new_pseudo_cumack = 1;
3379 								}
3380 								tp1->whoTo->find_pseudo_cumack = 1;
3381 							}
3382 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
3383 								sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
3384 							}
3385 							if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
3386 								if (tp1->rec.data.chunk_was_revoked == 0) {
3387 									tp1->whoTo->new_pseudo_cumack = 1;
3388 								}
3389 								tp1->whoTo->find_rtx_pseudo_cumack = 1;
3390 							}
3391 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3392 								sctp_log_sack(*biggest_newly_acked_tsn,
3393 								    last_tsn,
3394 								    tp1->rec.data.TSN_seq,
3395 								    frag_strt,
3396 								    frag_end,
3397 								    SCTP_LOG_TSN_ACKED);
3398 							}
3399 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3400 								sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
3401 								    tp1->whoTo->flight_size,
3402 								    tp1->book_size,
3403 								    (uintptr_t) tp1->whoTo,
3404 								    tp1->rec.data.TSN_seq);
3405 							}
3406 							sctp_flight_size_decrease(tp1);
3407 							sctp_total_flight_decrease(stcb, tp1);
3408 
3409 							tp1->whoTo->net_ack += tp1->send_size;
3410 							if (tp1->snd_count < 2) {
3411 								/*
3412 								 * True
3413 								 * non-retran
3414 								 * smited
3415 								 * chunk */
3416 								tp1->whoTo->net_ack2 += tp1->send_size;
3417 
3418 								/*
3419 								 * update RTO
3420 								 * too ? */
3421 								if (tp1->do_rtt) {
3422 									tp1->whoTo->RTO =
3423 									    sctp_calculate_rto(stcb,
3424 									    asoc,
3425 									    tp1->whoTo,
3426 									    &tp1->sent_rcv_time,
3427 									    sctp_align_safe_nocopy);
3428 									tp1->do_rtt = 0;
3429 								}
3430 							}
3431 						}
3432 						if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3433 							(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
3434 							(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
3435 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3436 							    asoc->this_sack_highest_gap,
3437 							    MAX_TSN)) {
3438 								asoc->this_sack_highest_gap =
3439 								    tp1->rec.data.TSN_seq;
3440 							}
3441 							if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3442 								sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3443 #ifdef SCTP_AUDITING_ENABLED
3444 								sctp_audit_log(0xB2,
3445 								    (asoc->sent_queue_retran_cnt & 0x000000ff));
3446 #endif
3447 							}
3448 						}
3449 						/*
3450 						 * All chunks NOT UNSENT
3451 						 * fall through here and are
3452 						 * marked (leave PR-SCTP
3453 						 * ones that are to skip
3454 						 * alone though)
3455 						 */
3456 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
3457 							tp1->sent = SCTP_DATAGRAM_MARKED;
3458 
3459 						if (tp1->rec.data.chunk_was_revoked) {
3460 							/* deflate the cwnd */
3461 							tp1->whoTo->cwnd -= tp1->book_size;
3462 							tp1->rec.data.chunk_was_revoked = 0;
3463 						}
3464 					}
3465 					break;
3466 				}	/* if (tp1->TSN_seq == theTSN) */
3467 				if (compare_with_wrap(tp1->rec.data.TSN_seq, theTSN,
3468 				    MAX_TSN))
3469 					break;
3470 
3471 				tp1 = TAILQ_NEXT(tp1, sctp_next);
3472 			}	/* end while (tp1) */
3473 		}		/* end for (j = fragStart */
3474 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3475 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3476 		*offset += sizeof(block);
3477 		if (frag == NULL) {
3478 			break;
3479 		}
3480 	}
3481 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3482 		if (num_frs)
3483 			sctp_log_fr(*biggest_tsn_acked,
3484 			    *biggest_newly_acked_tsn,
3485 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
3486 	}
3487 }
3488 
3489 static void
3490 sctp_check_for_revoked(struct sctp_tcb *stcb,
3491     struct sctp_association *asoc, uint32_t cumack,
3492     u_long biggest_tsn_acked)
3493 {
3494 	struct sctp_tmit_chunk *tp1;
3495 	int tot_revoked = 0;
3496 
3497 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3498 	while (tp1) {
3499 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3500 		    MAX_TSN)) {
3501 			/*
3502 			 * ok this guy is either ACK or MARKED. If it is
3503 			 * ACKED it has been previously acked but not this
3504 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3505 			 * again.
3506 			 */
3507 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3508 			    MAX_TSN))
3509 				break;
3510 
3511 
3512 			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3513 				/* it has been revoked */
3514 				tp1->sent = SCTP_DATAGRAM_SENT;
3515 				tp1->rec.data.chunk_was_revoked = 1;
3516 				/*
3517 				 * We must add this stuff back in to assure
3518 				 * timers and such get started.
3519 				 */
3520 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3521 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3522 					    tp1->whoTo->flight_size,
3523 					    tp1->book_size,
3524 					    (uintptr_t) tp1->whoTo,
3525 					    tp1->rec.data.TSN_seq);
3526 				}
3527 				sctp_flight_size_increase(tp1);
3528 				sctp_total_flight_increase(stcb, tp1);
3529 				/*
3530 				 * We inflate the cwnd to compensate for our
3531 				 * artificial inflation of the flight_size.
3532 				 */
3533 				tp1->whoTo->cwnd += tp1->book_size;
3534 				tot_revoked++;
3535 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
3536 					sctp_log_sack(asoc->last_acked_seq,
3537 					    cumack,
3538 					    tp1->rec.data.TSN_seq,
3539 					    0,
3540 					    0,
3541 					    SCTP_LOG_TSN_REVOKED);
3542 				}
3543 			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3544 				/* it has been re-acked in this SACK */
3545 				tp1->sent = SCTP_DATAGRAM_ACKED;
3546 			}
3547 		}
3548 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3549 			break;
3550 		tp1 = TAILQ_NEXT(tp1, sctp_next);
3551 	}
3552 	if (tot_revoked > 0) {
3553 		/*
3554 		 * Setup the ecn nonce re-sync point. We do this since once
3555 		 * data is revoked we begin to retransmit things, which do
3556 		 * NOT have the ECN bits set. This means we are now out of
3557 		 * sync and must wait until we get back in sync with the
3558 		 * peer to check ECN bits.
3559 		 */
3560 		tp1 = TAILQ_FIRST(&asoc->send_queue);
3561 		if (tp1 == NULL) {
3562 			asoc->nonce_resync_tsn = asoc->sending_seq;
3563 		} else {
3564 			asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3565 		}
3566 		asoc->nonce_wait_for_ecne = 0;
3567 		asoc->nonce_sum_check = 0;
3568 	}
3569 }
3570 
3571 
3572 static void
3573 sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3574     u_long biggest_tsn_acked, u_long biggest_tsn_newly_acked, u_long this_sack_lowest_newack, int accum_moved)
3575 {
3576 	struct sctp_tmit_chunk *tp1;
3577 	int strike_flag = 0;
3578 	struct timeval now;
3579 	int tot_retrans = 0;
3580 	uint32_t sending_seq;
3581 	struct sctp_nets *net;
3582 	int num_dests_sacked = 0;
3583 
3584 	/*
3585 	 * select the sending_seq, this is either the next thing ready to be
3586 	 * sent but not transmitted, OR, the next seq we assign.
3587 	 */
3588 	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3589 	if (tp1 == NULL) {
3590 		sending_seq = asoc->sending_seq;
3591 	} else {
3592 		sending_seq = tp1->rec.data.TSN_seq;
3593 	}
3594 
3595 	/* CMT DAC algo: finding out if SACK is a mixed SACK */
3596 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3597 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3598 			if (net->saw_newack)
3599 				num_dests_sacked++;
3600 		}
3601 	}
3602 	if (stcb->asoc.peer_supports_prsctp) {
3603 		(void)SCTP_GETTIME_TIMEVAL(&now);
3604 	}
3605 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3606 	while (tp1) {
3607 		strike_flag = 0;
3608 		if (tp1->no_fr_allowed) {
3609 			/* this one had a timeout or something */
3610 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3611 			continue;
3612 		}
3613 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3614 			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3615 				sctp_log_fr(biggest_tsn_newly_acked,
3616 				    tp1->rec.data.TSN_seq,
3617 				    tp1->sent,
3618 				    SCTP_FR_LOG_CHECK_STRIKE);
3619 		}
3620 		if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3621 		    MAX_TSN) ||
3622 		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3623 			/* done */
3624 			break;
3625 		}
3626 		if (stcb->asoc.peer_supports_prsctp) {
3627 			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3628 				/* Is it expired? */
3629 				if (
3630 				/*
3631 				 * TODO sctp_constants.h needs alternative
3632 				 * time macros when _KERNEL is undefined.
3633 				 */
3634 				    (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
3635 				    ) {
3636 					/* Yes so drop it */
3637 					if (tp1->data != NULL) {
3638 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3639 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3640 						    SCTP_SO_NOT_LOCKED);
3641 					}
3642 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3643 					continue;
3644 				}
3645 			}
3646 		}
3647 		if (compare_with_wrap(tp1->rec.data.TSN_seq,
3648 		    asoc->this_sack_highest_gap, MAX_TSN)) {
3649 			/* we are beyond the tsn in the sack  */
3650 			break;
3651 		}
3652 		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3653 			/* either a RESEND, ACKED, or MARKED */
3654 			/* skip */
3655 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3656 			continue;
3657 		}
3658 		/*
3659 		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3660 		 */
3661 		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3662 			/*
3663 			 * No new acks were receieved for data sent to this
3664 			 * dest. Therefore, according to the SFR algo for
3665 			 * CMT, no data sent to this dest can be marked for
3666 			 * FR using this SACK.
3667 			 */
3668 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3669 			continue;
3670 		} else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3671 		    tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3672 			/*
3673 			 * CMT: New acks were receieved for data sent to
3674 			 * this dest. But no new acks were seen for data
3675 			 * sent after tp1. Therefore, according to the SFR
3676 			 * algo for CMT, tp1 cannot be marked for FR using
3677 			 * this SACK. This step covers part of the DAC algo
3678 			 * and the HTNA algo as well.
3679 			 */
3680 			tp1 = TAILQ_NEXT(tp1, sctp_next);
3681 			continue;
3682 		}
3683 		/*
3684 		 * Here we check to see if we were have already done a FR
3685 		 * and if so we see if the biggest TSN we saw in the sack is
3686 		 * smaller than the recovery point. If so we don't strike
3687 		 * the tsn... otherwise we CAN strike the TSN.
3688 		 */
3689 		/*
3690 		 * @@@ JRI: Check for CMT if (accum_moved &&
3691 		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
3692 		 * 0)) {
3693 		 */
3694 		if (accum_moved && asoc->fast_retran_loss_recovery) {
3695 			/*
3696 			 * Strike the TSN if in fast-recovery and cum-ack
3697 			 * moved.
3698 			 */
3699 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3700 				sctp_log_fr(biggest_tsn_newly_acked,
3701 				    tp1->rec.data.TSN_seq,
3702 				    tp1->sent,
3703 				    SCTP_FR_LOG_STRIKE_CHUNK);
3704 			}
3705 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3706 				tp1->sent++;
3707 			}
3708 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3709 				/*
3710 				 * CMT DAC algorithm: If SACK flag is set to
3711 				 * 0, then lowest_newack test will not pass
3712 				 * because it would have been set to the
3713 				 * cumack earlier. If not already to be
3714 				 * rtx'd, If not a mixed sack and if tp1 is
3715 				 * not between two sacked TSNs, then mark by
3716 				 * one more. NOTE that we are marking by one
3717 				 * additional time since the SACK DAC flag
3718 				 * indicates that two packets have been
3719 				 * received after this missing TSN.
3720 				 */
3721 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3722 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3723 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3724 						sctp_log_fr(16 + num_dests_sacked,
3725 						    tp1->rec.data.TSN_seq,
3726 						    tp1->sent,
3727 						    SCTP_FR_LOG_STRIKE_CHUNK);
3728 					}
3729 					tp1->sent++;
3730 				}
3731 			}
3732 		} else if ((tp1->rec.data.doing_fast_retransmit) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
3733 			/*
3734 			 * For those that have done a FR we must take
3735 			 * special consideration if we strike. I.e the
3736 			 * biggest_newly_acked must be higher than the
3737 			 * sending_seq at the time we did the FR.
3738 			 */
3739 			if (
3740 #ifdef SCTP_FR_TO_ALTERNATE
3741 			/*
3742 			 * If FR's go to new networks, then we must only do
3743 			 * this for singly homed asoc's. However if the FR's
3744 			 * go to the same network (Armando's work) then its
3745 			 * ok to FR multiple times.
3746 			 */
3747 			    (asoc->numnets < 2)
3748 #else
3749 			    (1)
3750 #endif
3751 			    ) {
3752 
3753 				if ((compare_with_wrap(biggest_tsn_newly_acked,
3754 				    tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3755 				    (biggest_tsn_newly_acked ==
3756 				    tp1->rec.data.fast_retran_tsn)) {
3757 					/*
3758 					 * Strike the TSN, since this ack is
3759 					 * beyond where things were when we
3760 					 * did a FR.
3761 					 */
3762 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3763 						sctp_log_fr(biggest_tsn_newly_acked,
3764 						    tp1->rec.data.TSN_seq,
3765 						    tp1->sent,
3766 						    SCTP_FR_LOG_STRIKE_CHUNK);
3767 					}
3768 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3769 						tp1->sent++;
3770 					}
3771 					strike_flag = 1;
3772 					if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3773 						/*
3774 						 * CMT DAC algorithm: If
3775 						 * SACK flag is set to 0,
3776 						 * then lowest_newack test
3777 						 * will not pass because it
3778 						 * would have been set to
3779 						 * the cumack earlier. If
3780 						 * not already to be rtx'd,
3781 						 * If not a mixed sack and
3782 						 * if tp1 is not between two
3783 						 * sacked TSNs, then mark by
3784 						 * one more. NOTE that we
3785 						 * are marking by one
3786 						 * additional time since the
3787 						 * SACK DAC flag indicates
3788 						 * that two packets have
3789 						 * been received after this
3790 						 * missing TSN.
3791 						 */
3792 						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3793 						    (num_dests_sacked == 1) &&
3794 						    compare_with_wrap(this_sack_lowest_newack,
3795 						    tp1->rec.data.TSN_seq, MAX_TSN)) {
3796 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3797 								sctp_log_fr(32 + num_dests_sacked,
3798 								    tp1->rec.data.TSN_seq,
3799 								    tp1->sent,
3800 								    SCTP_FR_LOG_STRIKE_CHUNK);
3801 							}
3802 							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3803 								tp1->sent++;
3804 							}
3805 						}
3806 					}
3807 				}
3808 			}
3809 			/*
3810 			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
3811 			 * algo covers HTNA.
3812 			 */
3813 		} else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3814 		    biggest_tsn_newly_acked, MAX_TSN)) {
3815 			/*
3816 			 * We don't strike these: This is the  HTNA
3817 			 * algorithm i.e. we don't strike If our TSN is
3818 			 * larger than the Highest TSN Newly Acked.
3819 			 */
3820 			;
3821 		} else {
3822 			/* Strike the TSN */
3823 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3824 				sctp_log_fr(biggest_tsn_newly_acked,
3825 				    tp1->rec.data.TSN_seq,
3826 				    tp1->sent,
3827 				    SCTP_FR_LOG_STRIKE_CHUNK);
3828 			}
3829 			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3830 				tp1->sent++;
3831 			}
3832 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
3833 				/*
3834 				 * CMT DAC algorithm: If SACK flag is set to
3835 				 * 0, then lowest_newack test will not pass
3836 				 * because it would have been set to the
3837 				 * cumack earlier. If not already to be
3838 				 * rtx'd, If not a mixed sack and if tp1 is
3839 				 * not between two sacked TSNs, then mark by
3840 				 * one more. NOTE that we are marking by one
3841 				 * additional time since the SACK DAC flag
3842 				 * indicates that two packets have been
3843 				 * received after this missing TSN.
3844 				 */
3845 				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3846 				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3847 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3848 						sctp_log_fr(48 + num_dests_sacked,
3849 						    tp1->rec.data.TSN_seq,
3850 						    tp1->sent,
3851 						    SCTP_FR_LOG_STRIKE_CHUNK);
3852 					}
3853 					tp1->sent++;
3854 				}
3855 			}
3856 		}
3857 		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3858 			struct sctp_nets *alt;
3859 
3860 			/* fix counts and things */
3861 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
3862 				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3863 				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3864 				    tp1->book_size,
3865 				    (uintptr_t) tp1->whoTo,
3866 				    tp1->rec.data.TSN_seq);
3867 			}
3868 			if (tp1->whoTo) {
3869 				tp1->whoTo->net_ack++;
3870 				sctp_flight_size_decrease(tp1);
3871 			}
3872 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
3873 				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3874 				    asoc->peers_rwnd, tp1->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3875 			}
3876 			/* add back to the rwnd */
3877 			asoc->peers_rwnd += (tp1->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
3878 
3879 			/* remove from the total flight */
3880 			sctp_total_flight_decrease(stcb, tp1);
3881 
3882 			if ((stcb->asoc.peer_supports_prsctp) &&
3883 			    (PR_SCTP_RTX_ENABLED(tp1->flags))) {
3884 				/*
3885 				 * Has it been retransmitted tv_sec times? -
3886 				 * we store the retran count there.
3887 				 */
3888 				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3889 					/* Yes, so drop it */
3890 					if (tp1->data != NULL) {
3891 						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3892 						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3893 						    SCTP_SO_NOT_LOCKED);
3894 					}
3895 					/* Make sure to flag we had a FR */
3896 					tp1->whoTo->net_ack++;
3897 					tp1 = TAILQ_NEXT(tp1, sctp_next);
3898 					continue;
3899 				}
3900 			}
3901 			/* printf("OK, we are now ready to FR this guy\n"); */
3902 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
3903 				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3904 				    0, SCTP_FR_MARKED);
3905 			}
3906 			if (strike_flag) {
3907 				/* This is a subsequent FR */
3908 				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3909 			}
3910 			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3911 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
3912 				/*
3913 				 * CMT: Using RTX_SSTHRESH policy for CMT.
3914 				 * If CMT is being used, then pick dest with
3915 				 * largest ssthresh for any retransmission.
3916 				 */
3917 				tp1->no_fr_allowed = 1;
3918 				alt = tp1->whoTo;
3919 				/* sa_ignore NO_NULL_CHK */
3920 				if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
3921 					/*
3922 					 * JRS 5/18/07 - If CMT PF is on,
3923 					 * use the PF version of
3924 					 * find_alt_net()
3925 					 */
3926 					alt = sctp_find_alternate_net(stcb, alt, 2);
3927 				} else {
3928 					/*
3929 					 * JRS 5/18/07 - If only CMT is on,
3930 					 * use the CMT version of
3931 					 * find_alt_net()
3932 					 */
3933 					/* sa_ignore NO_NULL_CHK */
3934 					alt = sctp_find_alternate_net(stcb, alt, 1);
3935 				}
3936 				if (alt == NULL) {
3937 					alt = tp1->whoTo;
3938 				}
3939 				/*
3940 				 * CUCv2: If a different dest is picked for
3941 				 * the retransmission, then new
3942 				 * (rtx-)pseudo_cumack needs to be tracked
3943 				 * for orig dest. Let CUCv2 track new (rtx-)
3944 				 * pseudo-cumack always.
3945 				 */
3946 				if (tp1->whoTo) {
3947 					tp1->whoTo->find_pseudo_cumack = 1;
3948 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3949 				}
3950 			} else {/* CMT is OFF */
3951 
3952 #ifdef SCTP_FR_TO_ALTERNATE
3953 				/* Can we find an alternate? */
3954 				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3955 #else
3956 				/*
3957 				 * default behavior is to NOT retransmit
3958 				 * FR's to an alternate. Armando Caro's
3959 				 * paper details why.
3960 				 */
3961 				alt = tp1->whoTo;
3962 #endif
3963 			}
3964 
3965 			tp1->rec.data.doing_fast_retransmit = 1;
3966 			tot_retrans++;
3967 			/* mark the sending seq for possible subsequent FR's */
3968 			/*
3969 			 * printf("Marking TSN for FR new value %x\n",
3970 			 * (uint32_t)tpi->rec.data.TSN_seq);
3971 			 */
3972 			if (TAILQ_EMPTY(&asoc->send_queue)) {
3973 				/*
3974 				 * If the queue of send is empty then its
3975 				 * the next sequence number that will be
3976 				 * assigned so we subtract one from this to
3977 				 * get the one we last sent.
3978 				 */
3979 				tp1->rec.data.fast_retran_tsn = sending_seq;
3980 			} else {
3981 				/*
3982 				 * If there are chunks on the send queue
3983 				 * (unsent data that has made it from the
3984 				 * stream queues but not out the door, we
3985 				 * take the first one (which will have the
3986 				 * lowest TSN) and subtract one to get the
3987 				 * one we last sent.
3988 				 */
3989 				struct sctp_tmit_chunk *ttt;
3990 
3991 				ttt = TAILQ_FIRST(&asoc->send_queue);
3992 				tp1->rec.data.fast_retran_tsn =
3993 				    ttt->rec.data.TSN_seq;
3994 			}
3995 
3996 			if (tp1->do_rtt) {
3997 				/*
3998 				 * this guy had a RTO calculation pending on
3999 				 * it, cancel it
4000 				 */
4001 				tp1->do_rtt = 0;
4002 			}
4003 			if (alt != tp1->whoTo) {
4004 				/* yes, there is an alternate. */
4005 				sctp_free_remote_addr(tp1->whoTo);
4006 				/* sa_ignore FREED_MEMORY */
4007 				tp1->whoTo = alt;
4008 				atomic_add_int(&alt->ref_count, 1);
4009 			}
4010 		}
4011 		tp1 = TAILQ_NEXT(tp1, sctp_next);
4012 	}			/* while (tp1) */
4013 
4014 	if (tot_retrans > 0) {
4015 		/*
4016 		 * Setup the ecn nonce re-sync point. We do this since once
4017 		 * we go to FR something we introduce a Karn's rule scenario
4018 		 * and won't know the totals for the ECN bits.
4019 		 */
4020 		asoc->nonce_resync_tsn = sending_seq;
4021 		asoc->nonce_wait_for_ecne = 0;
4022 		asoc->nonce_sum_check = 0;
4023 	}
4024 }
4025 
4026 struct sctp_tmit_chunk *
4027 sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
4028     struct sctp_association *asoc)
4029 {
4030 	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
4031 	struct timeval now;
4032 	int now_filled = 0;
4033 
4034 	if (asoc->peer_supports_prsctp == 0) {
4035 		return (NULL);
4036 	}
4037 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4038 	while (tp1) {
4039 		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
4040 		    tp1->sent != SCTP_DATAGRAM_RESEND) {
4041 			/* no chance to advance, out of here */
4042 			break;
4043 		}
4044 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
4045 			if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
4046 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
4047 				    asoc->advanced_peer_ack_point,
4048 				    tp1->rec.data.TSN_seq, 0, 0);
4049 			}
4050 		}
4051 		if (!PR_SCTP_ENABLED(tp1->flags)) {
4052 			/*
4053 			 * We can't fwd-tsn past any that are reliable aka
4054 			 * retransmitted until the asoc fails.
4055 			 */
4056 			break;
4057 		}
4058 		if (!now_filled) {
4059 			(void)SCTP_GETTIME_TIMEVAL(&now);
4060 			now_filled = 1;
4061 		}
4062 		tp2 = TAILQ_NEXT(tp1, sctp_next);
4063 		/*
4064 		 * now we got a chunk which is marked for another
4065 		 * retransmission to a PR-stream but has run out its chances
4066 		 * already maybe OR has been marked to skip now. Can we skip
4067 		 * it if its a resend?
4068 		 */
4069 		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
4070 		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
4071 			/*
4072 			 * Now is this one marked for resend and its time is
4073 			 * now up?
4074 			 */
4075 			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
4076 				/* Yes so drop it */
4077 				if (tp1->data) {
4078 					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
4079 					    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
4080 					    SCTP_SO_NOT_LOCKED);
4081 				}
4082 			} else {
4083 				/*
4084 				 * No, we are done when hit one for resend
4085 				 * whos time as not expired.
4086 				 */
4087 				break;
4088 			}
4089 		}
4090 		/*
4091 		 * Ok now if this chunk is marked to drop it we can clean up
4092 		 * the chunk, advance our peer ack point and we can check
4093 		 * the next chunk.
4094 		 */
4095 		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
4096 			/* advance PeerAckPoint goes forward */
4097 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
4098 			    asoc->advanced_peer_ack_point,
4099 			    MAX_TSN)) {
4100 
4101 				asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
4102 				a_adv = tp1;
4103 			} else if (tp1->rec.data.TSN_seq == asoc->advanced_peer_ack_point) {
4104 				/* No update but we do save the chk */
4105 				a_adv = tp1;
4106 			}
4107 		} else {
4108 			/*
4109 			 * If it is still in RESEND we can advance no
4110 			 * further
4111 			 */
4112 			break;
4113 		}
4114 		/*
4115 		 * If we hit here we just dumped tp1, move to next tsn on
4116 		 * sent queue.
4117 		 */
4118 		tp1 = tp2;
4119 	}
4120 	return (a_adv);
4121 }
4122 
4123 static int
4124 sctp_fs_audit(struct sctp_association *asoc)
4125 {
4126 	struct sctp_tmit_chunk *chk;
4127 	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
4128 	int entry_flight, entry_cnt, ret;
4129 
4130 	entry_flight = asoc->total_flight;
4131 	entry_cnt = asoc->total_flight_count;
4132 	ret = 0;
4133 
4134 	if (asoc->pr_sctp_cnt >= asoc->sent_queue_cnt)
4135 		return (0);
4136 
4137 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4138 		if (chk->sent < SCTP_DATAGRAM_RESEND) {
4139 			printf("Chk TSN:%u size:%d inflight cnt:%d\n",
4140 			    chk->rec.data.TSN_seq,
4141 			    chk->send_size,
4142 			    chk->snd_count
4143 			    );
4144 			inflight++;
4145 		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
4146 			resend++;
4147 		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
4148 			inbetween++;
4149 		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
4150 			above++;
4151 		} else {
4152 			acked++;
4153 		}
4154 	}
4155 
4156 	if ((inflight > 0) || (inbetween > 0)) {
4157 #ifdef INVARIANTS
4158 		panic("Flight size-express incorrect? \n");
4159 #else
4160 		printf("asoc->total_flight:%d cnt:%d\n",
4161 		    entry_flight, entry_cnt);
4162 
4163 		SCTP_PRINTF("Flight size-express incorrect F:%d I:%d R:%d Ab:%d ACK:%d\n",
4164 		    inflight, inbetween, resend, above, acked);
4165 		ret = 1;
4166 #endif
4167 	}
4168 	return (ret);
4169 }
4170 
4171 
4172 static void
4173 sctp_window_probe_recovery(struct sctp_tcb *stcb,
4174     struct sctp_association *asoc,
4175     struct sctp_nets *net,
4176     struct sctp_tmit_chunk *tp1)
4177 {
4178 	tp1->window_probe = 0;
4179 	if ((tp1->sent >= SCTP_DATAGRAM_ACKED) || (tp1->data == NULL)) {
4180 		/* TSN's skipped we do NOT move back. */
4181 		sctp_misc_ints(SCTP_FLIGHT_LOG_DWN_WP_FWD,
4182 		    tp1->whoTo->flight_size,
4183 		    tp1->book_size,
4184 		    (uintptr_t) tp1->whoTo,
4185 		    tp1->rec.data.TSN_seq);
4186 		return;
4187 	}
4188 	/* First setup this by shrinking flight */
4189 	sctp_flight_size_decrease(tp1);
4190 	sctp_total_flight_decrease(stcb, tp1);
4191 	/* Now mark for resend */
4192 	tp1->sent = SCTP_DATAGRAM_RESEND;
4193 	asoc->sent_queue_retran_cnt++;
4194 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4195 		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
4196 		    tp1->whoTo->flight_size,
4197 		    tp1->book_size,
4198 		    (uintptr_t) tp1->whoTo,
4199 		    tp1->rec.data.TSN_seq);
4200 	}
4201 }
4202 
4203 void
4204 sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
4205     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
4206 {
4207 	struct sctp_nets *net;
4208 	struct sctp_association *asoc;
4209 	struct sctp_tmit_chunk *tp1, *tp2;
4210 	uint32_t old_rwnd;
4211 	int win_probe_recovery = 0;
4212 	int win_probe_recovered = 0;
4213 	int j, done_once = 0;
4214 
4215 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4216 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
4217 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4218 	}
4219 	SCTP_TCB_LOCK_ASSERT(stcb);
4220 #ifdef SCTP_ASOCLOG_OF_TSNS
4221 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
4222 	stcb->asoc.cumack_log_at++;
4223 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
4224 		stcb->asoc.cumack_log_at = 0;
4225 	}
4226 #endif
4227 	asoc = &stcb->asoc;
4228 	old_rwnd = asoc->peers_rwnd;
4229 	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
4230 		/* old ack */
4231 		return;
4232 	} else if (asoc->last_acked_seq == cumack) {
4233 		/* Window update sack */
4234 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4235 		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4236 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4237 			/* SWS sender side engages */
4238 			asoc->peers_rwnd = 0;
4239 		}
4240 		if (asoc->peers_rwnd > old_rwnd) {
4241 			goto again;
4242 		}
4243 		return;
4244 	}
4245 	/* First setup for CC stuff */
4246 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4247 		net->prev_cwnd = net->cwnd;
4248 		net->net_ack = 0;
4249 		net->net_ack2 = 0;
4250 
4251 		/*
4252 		 * CMT: Reset CUC and Fast recovery algo variables before
4253 		 * SACK processing
4254 		 */
4255 		net->new_pseudo_cumack = 0;
4256 		net->will_exit_fast_recovery = 0;
4257 	}
4258 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4259 		uint32_t send_s;
4260 
4261 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4262 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4263 			    sctpchunk_listhead);
4264 			send_s = tp1->rec.data.TSN_seq + 1;
4265 		} else {
4266 			send_s = asoc->sending_seq;
4267 		}
4268 		if ((cumack == send_s) ||
4269 		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
4270 #ifndef INVARIANTS
4271 			struct mbuf *oper;
4272 
4273 #endif
4274 #ifdef INVARIANTS
4275 			panic("Impossible sack 1");
4276 #else
4277 			*abort_now = 1;
4278 			/* XXX */
4279 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4280 			    0, M_DONTWAIT, 1, MT_DATA);
4281 			if (oper) {
4282 				struct sctp_paramhdr *ph;
4283 				uint32_t *ippp;
4284 
4285 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4286 				    sizeof(uint32_t);
4287 				ph = mtod(oper, struct sctp_paramhdr *);
4288 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4289 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4290 				ippp = (uint32_t *) (ph + 1);
4291 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4292 			}
4293 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4294 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4295 			return;
4296 #endif
4297 		}
4298 	}
4299 	asoc->this_sack_highest_gap = cumack;
4300 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4301 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4302 		    stcb->asoc.overall_error_count,
4303 		    0,
4304 		    SCTP_FROM_SCTP_INDATA,
4305 		    __LINE__);
4306 	}
4307 	stcb->asoc.overall_error_count = 0;
4308 	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
4309 		/* process the new consecutive TSN first */
4310 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
4311 		while (tp1) {
4312 			tp2 = TAILQ_NEXT(tp1, sctp_next);
4313 			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
4314 			    MAX_TSN) ||
4315 			    cumack == tp1->rec.data.TSN_seq) {
4316 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4317 					printf("Warning, an unsent is now acked?\n");
4318 				}
4319 				/*
4320 				 * ECN Nonce: Add the nonce to the sender's
4321 				 * nonce sum
4322 				 */
4323 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4324 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4325 					/*
4326 					 * If it is less than ACKED, it is
4327 					 * now no-longer in flight. Higher
4328 					 * values may occur during marking
4329 					 */
4330 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4331 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
4332 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4333 							    tp1->whoTo->flight_size,
4334 							    tp1->book_size,
4335 							    (uintptr_t) tp1->whoTo,
4336 							    tp1->rec.data.TSN_seq);
4337 						}
4338 						sctp_flight_size_decrease(tp1);
4339 						/* sa_ignore NO_NULL_CHK */
4340 						sctp_total_flight_decrease(stcb, tp1);
4341 					}
4342 					tp1->whoTo->net_ack += tp1->send_size;
4343 					if (tp1->snd_count < 2) {
4344 						/*
4345 						 * True non-retransmited
4346 						 * chunk
4347 						 */
4348 						tp1->whoTo->net_ack2 +=
4349 						    tp1->send_size;
4350 
4351 						/* update RTO too? */
4352 						if (tp1->do_rtt) {
4353 							tp1->whoTo->RTO =
4354 							/*
4355 							 * sa_ignore
4356 							 * NO_NULL_CHK
4357 							 */
4358 							    sctp_calculate_rto(stcb,
4359 							    asoc, tp1->whoTo,
4360 							    &tp1->sent_rcv_time,
4361 							    sctp_align_safe_nocopy);
4362 							tp1->do_rtt = 0;
4363 						}
4364 					}
4365 					/*
4366 					 * CMT: CUCv2 algorithm. From the
4367 					 * cumack'd TSNs, for each TSN being
4368 					 * acked for the first time, set the
4369 					 * following variables for the
4370 					 * corresp destination.
4371 					 * new_pseudo_cumack will trigger a
4372 					 * cwnd update.
4373 					 * find_(rtx_)pseudo_cumack will
4374 					 * trigger search for the next
4375 					 * expected (rtx-)pseudo-cumack.
4376 					 */
4377 					tp1->whoTo->new_pseudo_cumack = 1;
4378 					tp1->whoTo->find_pseudo_cumack = 1;
4379 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4380 
4381 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
4382 						/* sa_ignore NO_NULL_CHK */
4383 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4384 					}
4385 				}
4386 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4387 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4388 				}
4389 				if (tp1->rec.data.chunk_was_revoked) {
4390 					/* deflate the cwnd */
4391 					tp1->whoTo->cwnd -= tp1->book_size;
4392 					tp1->rec.data.chunk_was_revoked = 0;
4393 				}
4394 				tp1->sent = SCTP_DATAGRAM_ACKED;
4395 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4396 				if (tp1->data) {
4397 					/* sa_ignore NO_NULL_CHK */
4398 					sctp_free_bufspace(stcb, asoc, tp1, 1);
4399 					sctp_m_freem(tp1->data);
4400 				}
4401 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4402 					sctp_log_sack(asoc->last_acked_seq,
4403 					    cumack,
4404 					    tp1->rec.data.TSN_seq,
4405 					    0,
4406 					    0,
4407 					    SCTP_LOG_FREE_SENT);
4408 				}
4409 				tp1->data = NULL;
4410 				asoc->sent_queue_cnt--;
4411 				sctp_free_a_chunk(stcb, tp1);
4412 				tp1 = tp2;
4413 			} else {
4414 				break;
4415 			}
4416 		}
4417 
4418 	}
4419 	/* sa_ignore NO_NULL_CHK */
4420 	if (stcb->sctp_socket) {
4421 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4422 		struct socket *so;
4423 
4424 #endif
4425 
4426 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4427 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4428 			/* sa_ignore NO_NULL_CHK */
4429 			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
4430 		}
4431 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4432 		so = SCTP_INP_SO(stcb->sctp_ep);
4433 		atomic_add_int(&stcb->asoc.refcnt, 1);
4434 		SCTP_TCB_UNLOCK(stcb);
4435 		SCTP_SOCKET_LOCK(so, 1);
4436 		SCTP_TCB_LOCK(stcb);
4437 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4438 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4439 			/* assoc was freed while we were unlocked */
4440 			SCTP_SOCKET_UNLOCK(so, 1);
4441 			return;
4442 		}
4443 #endif
4444 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4445 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4446 		SCTP_SOCKET_UNLOCK(so, 1);
4447 #endif
4448 	} else {
4449 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
4450 			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
4451 		}
4452 	}
4453 
4454 	/* JRS - Use the congestion control given in the CC module */
4455 	if (asoc->last_acked_seq != cumack)
4456 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4457 
4458 	asoc->last_acked_seq = cumack;
4459 
4460 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4461 		/* nothing left in-flight */
4462 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4463 			net->flight_size = 0;
4464 			net->partial_bytes_acked = 0;
4465 		}
4466 		asoc->total_flight = 0;
4467 		asoc->total_flight_count = 0;
4468 	}
4469 	/* ECN Nonce updates */
4470 	if (asoc->ecn_nonce_allowed) {
4471 		if (asoc->nonce_sum_check) {
4472 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4473 				if (asoc->nonce_wait_for_ecne == 0) {
4474 					struct sctp_tmit_chunk *lchk;
4475 
4476 					lchk = TAILQ_FIRST(&asoc->send_queue);
4477 					asoc->nonce_wait_for_ecne = 1;
4478 					if (lchk) {
4479 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4480 					} else {
4481 						asoc->nonce_wait_tsn = asoc->sending_seq;
4482 					}
4483 				} else {
4484 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4485 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4486 						/*
4487 						 * Misbehaving peer. We need
4488 						 * to react to this guy
4489 						 */
4490 						asoc->ecn_allowed = 0;
4491 						asoc->ecn_nonce_allowed = 0;
4492 					}
4493 				}
4494 			}
4495 		} else {
4496 			/* See if Resynchronization Possible */
4497 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4498 				asoc->nonce_sum_check = 1;
4499 				/*
4500 				 * now we must calculate what the base is.
4501 				 * We do this based on two things, we know
4502 				 * the total's for all the segments
4503 				 * gap-acked in the SACK (none), We also
4504 				 * know the SACK's nonce sum, its in
4505 				 * nonce_sum_flag. So we can build a truth
4506 				 * table to back-calculate the new value of
4507 				 * asoc->nonce_sum_expect_base:
4508 				 *
4509 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
4510 				 * 1                    0 1 0 1 1 1
4511 				 * 1 0
4512 				 */
4513 				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4514 			}
4515 		}
4516 	}
4517 	/* RWND update */
4518 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4519 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
4520 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4521 		/* SWS sender side engages */
4522 		asoc->peers_rwnd = 0;
4523 	}
4524 	if (asoc->peers_rwnd > old_rwnd) {
4525 		win_probe_recovery = 1;
4526 	}
4527 	/* Now assure a timer where data is queued at */
4528 again:
4529 	j = 0;
4530 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4531 		int to_ticks;
4532 
4533 		if (win_probe_recovery && (net->window_probe)) {
4534 			win_probe_recovered = 1;
4535 			/*
4536 			 * Find first chunk that was used with window probe
4537 			 * and clear the sent
4538 			 */
4539 			/* sa_ignore FREED_MEMORY */
4540 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4541 				if (tp1->window_probe) {
4542 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
4543 					break;
4544 				}
4545 			}
4546 		}
4547 		if (net->RTO == 0) {
4548 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4549 		} else {
4550 			to_ticks = MSEC_TO_TICKS(net->RTO);
4551 		}
4552 		if (net->flight_size) {
4553 			j++;
4554 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4555 			    sctp_timeout_handler, &net->rxt_timer);
4556 			if (net->window_probe) {
4557 				net->window_probe = 0;
4558 			}
4559 		} else {
4560 			if (net->window_probe) {
4561 				/*
4562 				 * In window probes we must assure a timer
4563 				 * is still running there
4564 				 */
4565 				net->window_probe = 0;
4566 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4567 					SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4568 					    sctp_timeout_handler, &net->rxt_timer);
4569 				}
4570 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4571 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4572 				    stcb, net,
4573 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4574 			}
4575 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4576 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4577 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4578 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4579 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4580 				}
4581 			}
4582 		}
4583 	}
4584 	if ((j == 0) &&
4585 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4586 	    (asoc->sent_queue_retran_cnt == 0) &&
4587 	    (win_probe_recovered == 0) &&
4588 	    (done_once == 0)) {
4589 		/*
4590 		 * huh, this should not happen unless all packets are
4591 		 * PR-SCTP and marked to skip of course.
4592 		 */
4593 		if (sctp_fs_audit(asoc)) {
4594 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4595 				if (net->flight_size) {
4596 					net->flight_size = 0;
4597 				}
4598 			}
4599 			asoc->total_flight = 0;
4600 			asoc->total_flight_count = 0;
4601 			asoc->sent_queue_retran_cnt = 0;
4602 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4603 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4604 					sctp_flight_size_increase(tp1);
4605 					sctp_total_flight_increase(stcb, tp1);
4606 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4607 					asoc->sent_queue_retran_cnt++;
4608 				}
4609 			}
4610 		}
4611 		done_once = 1;
4612 		goto again;
4613 	}
4614 	/**********************************/
4615 	/* Now what about shutdown issues */
4616 	/**********************************/
4617 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4618 		/* nothing left on sendqueue.. consider done */
4619 		/* clean up */
4620 		if ((asoc->stream_queue_cnt == 1) &&
4621 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4622 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4623 		    (asoc->locked_on_sending)
4624 		    ) {
4625 			struct sctp_stream_queue_pending *sp;
4626 
4627 			/*
4628 			 * I may be in a state where we got all across.. but
4629 			 * cannot write more due to a shutdown... we abort
4630 			 * since the user did not indicate EOR in this case.
4631 			 * The sp will be cleaned during free of the asoc.
4632 			 */
4633 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4634 			    sctp_streamhead);
4635 			if ((sp) && (sp->length == 0)) {
4636 				/* Let cleanup code purge it */
4637 				if (sp->msg_is_complete) {
4638 					asoc->stream_queue_cnt--;
4639 				} else {
4640 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4641 					asoc->locked_on_sending = NULL;
4642 					asoc->stream_queue_cnt--;
4643 				}
4644 			}
4645 		}
4646 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4647 		    (asoc->stream_queue_cnt == 0)) {
4648 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4649 				/* Need to abort here */
4650 				struct mbuf *oper;
4651 
4652 		abort_out_now:
4653 				*abort_now = 1;
4654 				/* XXX */
4655 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4656 				    0, M_DONTWAIT, 1, MT_DATA);
4657 				if (oper) {
4658 					struct sctp_paramhdr *ph;
4659 					uint32_t *ippp;
4660 
4661 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4662 					    sizeof(uint32_t);
4663 					ph = mtod(oper, struct sctp_paramhdr *);
4664 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4665 					ph->param_length = htons(SCTP_BUF_LEN(oper));
4666 					ippp = (uint32_t *) (ph + 1);
4667 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4668 				}
4669 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4670 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4671 			} else {
4672 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4673 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4674 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4675 				}
4676 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4677 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4678 				sctp_stop_timers_for_shutdown(stcb);
4679 				sctp_send_shutdown(stcb,
4680 				    stcb->asoc.primary_destination);
4681 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4682 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4683 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4684 				    stcb->sctp_ep, stcb, asoc->primary_destination);
4685 			}
4686 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4687 		    (asoc->stream_queue_cnt == 0)) {
4688 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4689 				goto abort_out_now;
4690 			}
4691 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4692 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4693 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4694 			sctp_send_shutdown_ack(stcb,
4695 			    stcb->asoc.primary_destination);
4696 
4697 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4698 			    stcb->sctp_ep, stcb, asoc->primary_destination);
4699 		}
4700 	}
4701 	/*********************************************/
4702 	/* Here we perform PR-SCTP procedures        */
4703 	/* (section 4.2)                             */
4704 	/*********************************************/
4705 	/* C1. update advancedPeerAckPoint */
4706 	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4707 		asoc->advanced_peer_ack_point = cumack;
4708 	}
4709 	/* PR-Sctp issues need to be addressed too */
4710 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
4711 		struct sctp_tmit_chunk *lchk;
4712 		uint32_t old_adv_peer_ack_point;
4713 
4714 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
4715 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
4716 		/* C3. See if we need to send a Fwd-TSN */
4717 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cumack,
4718 		    MAX_TSN)) {
4719 			/*
4720 			 * ISSUE with ECN, see FWD-TSN processing for notes
4721 			 * on issues that will occur when the ECN NONCE
4722 			 * stuff is put into SCTP for cross checking.
4723 			 */
4724 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
4725 			    MAX_TSN)) {
4726 				send_forward_tsn(stcb, asoc);
4727 				/*
4728 				 * ECN Nonce: Disable Nonce Sum check when
4729 				 * FWD TSN is sent and store resync tsn
4730 				 */
4731 				asoc->nonce_sum_check = 0;
4732 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
4733 			} else if (lchk) {
4734 				/* try to FR fwd-tsn's that get lost too */
4735 				lchk->rec.data.fwd_tsn_cnt++;
4736 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
4737 					send_forward_tsn(stcb, asoc);
4738 					lchk->rec.data.fwd_tsn_cnt = 0;
4739 				}
4740 			}
4741 		}
4742 		if (lchk) {
4743 			/* Assure a timer is up */
4744 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
4745 			    stcb->sctp_ep, stcb, lchk->whoTo);
4746 		}
4747 	}
4748 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
4749 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4750 		    rwnd,
4751 		    stcb->asoc.peers_rwnd,
4752 		    stcb->asoc.total_flight,
4753 		    stcb->asoc.total_output_queue_size);
4754 	}
4755 }
4756 
4757 void
4758 sctp_handle_sack(struct mbuf *m, int offset,
4759     struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
4760     struct sctp_nets *net_from, int *abort_now, int sack_len, uint32_t rwnd)
4761 {
4762 	struct sctp_association *asoc;
4763 	struct sctp_sack *sack;
4764 	struct sctp_tmit_chunk *tp1, *tp2;
4765 	uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
4766 	         this_sack_lowest_newack;
4767 	uint32_t sav_cum_ack;
4768 	uint16_t num_seg, num_dup;
4769 	uint16_t wake_him = 0;
4770 	unsigned int sack_length;
4771 	uint32_t send_s = 0;
4772 	long j;
4773 	int accum_moved = 0;
4774 	int will_exit_fast_recovery = 0;
4775 	uint32_t a_rwnd, old_rwnd;
4776 	int win_probe_recovery = 0;
4777 	int win_probe_recovered = 0;
4778 	struct sctp_nets *net = NULL;
4779 	int nonce_sum_flag, ecn_seg_sums = 0;
4780 	int done_once;
4781 	uint8_t reneged_all = 0;
4782 	uint8_t cmt_dac_flag;
4783 
4784 	/*
4785 	 * we take any chance we can to service our queues since we cannot
4786 	 * get awoken when the socket is read from :<
4787 	 */
4788 	/*
4789 	 * Now perform the actual SACK handling: 1) Verify that it is not an
4790 	 * old sack, if so discard. 2) If there is nothing left in the send
4791 	 * queue (cum-ack is equal to last acked) then you have a duplicate
4792 	 * too, update any rwnd change and verify no timers are running.
4793 	 * then return. 3) Process any new consequtive data i.e. cum-ack
4794 	 * moved process these first and note that it moved. 4) Process any
4795 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4796 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4797 	 * sync up flightsizes and things, stop all timers and also check
4798 	 * for shutdown_pending state. If so then go ahead and send off the
4799 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4800 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4801 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4802 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4803 	 * if in shutdown_recv state.
4804 	 */
4805 	SCTP_TCB_LOCK_ASSERT(stcb);
4806 	sack = &ch->sack;
4807 	/* CMT DAC algo */
4808 	this_sack_lowest_newack = 0;
4809 	j = 0;
4810 	sack_length = (unsigned int)sack_len;
4811 	/* ECN Nonce */
4812 	SCTP_STAT_INCR(sctps_slowpath_sack);
4813 	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
4814 	cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
4815 #ifdef SCTP_ASOCLOG_OF_TSNS
4816 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
4817 	stcb->asoc.cumack_log_at++;
4818 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
4819 		stcb->asoc.cumack_log_at = 0;
4820 	}
4821 #endif
4822 	num_seg = ntohs(sack->num_gap_ack_blks);
4823 	a_rwnd = rwnd;
4824 
4825 	/* CMT DAC algo */
4826 	cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
4827 	num_dup = ntohs(sack->num_dup_tsns);
4828 
4829 	old_rwnd = stcb->asoc.peers_rwnd;
4830 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
4831 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4832 		    stcb->asoc.overall_error_count,
4833 		    0,
4834 		    SCTP_FROM_SCTP_INDATA,
4835 		    __LINE__);
4836 	}
4837 	stcb->asoc.overall_error_count = 0;
4838 	asoc = &stcb->asoc;
4839 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
4840 		sctp_log_sack(asoc->last_acked_seq,
4841 		    cum_ack,
4842 		    0,
4843 		    num_seg,
4844 		    num_dup,
4845 		    SCTP_LOG_NEW_SACK);
4846 	}
4847 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4848 		int off_to_dup, iii;
4849 		uint32_t *dupdata, dblock;
4850 
4851 		off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) + sizeof(struct sctp_sack_chunk);
4852 		if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= sack_length) {
4853 			dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4854 			    sizeof(uint32_t), (uint8_t *) & dblock);
4855 			off_to_dup += sizeof(uint32_t);
4856 			if (dupdata) {
4857 				for (iii = 0; iii < num_dup; iii++) {
4858 					sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4859 					dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4860 					    sizeof(uint32_t), (uint8_t *) & dblock);
4861 					if (dupdata == NULL)
4862 						break;
4863 					off_to_dup += sizeof(uint32_t);
4864 				}
4865 			}
4866 		} else {
4867 			SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d sack_len:%d num gaps:%d\n",
4868 			    off_to_dup, num_dup, sack_length, num_seg);
4869 		}
4870 	}
4871 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
4872 		/* reality check */
4873 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4874 			tp1 = TAILQ_LAST(&asoc->sent_queue,
4875 			    sctpchunk_listhead);
4876 			send_s = tp1->rec.data.TSN_seq + 1;
4877 		} else {
4878 			send_s = asoc->sending_seq;
4879 		}
4880 		if (cum_ack == send_s ||
4881 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4882 #ifndef INVARIANTS
4883 			struct mbuf *oper;
4884 
4885 #endif
4886 #ifdef INVARIANTS
4887 	hopeless_peer:
4888 			panic("Impossible sack 1");
4889 #else
4890 
4891 
4892 			/*
4893 			 * no way, we have not even sent this TSN out yet.
4894 			 * Peer is hopelessly messed up with us.
4895 			 */
4896 	hopeless_peer:
4897 			*abort_now = 1;
4898 			/* XXX */
4899 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4900 			    0, M_DONTWAIT, 1, MT_DATA);
4901 			if (oper) {
4902 				struct sctp_paramhdr *ph;
4903 				uint32_t *ippp;
4904 
4905 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4906 				    sizeof(uint32_t);
4907 				ph = mtod(oper, struct sctp_paramhdr *);
4908 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4909 				ph->param_length = htons(SCTP_BUF_LEN(oper));
4910 				ippp = (uint32_t *) (ph + 1);
4911 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4912 			}
4913 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4914 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4915 			return;
4916 #endif
4917 		}
4918 	}
4919 	/**********************/
4920 	/* 1) check the range */
4921 	/**********************/
4922 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4923 		/* acking something behind */
4924 		return;
4925 	}
4926 	sav_cum_ack = asoc->last_acked_seq;
4927 
4928 	/* update the Rwnd of the peer */
4929 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4930 	    TAILQ_EMPTY(&asoc->send_queue) &&
4931 	    (asoc->stream_queue_cnt == 0)
4932 	    ) {
4933 		/* nothing left on send/sent and strmq */
4934 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
4935 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4936 			    asoc->peers_rwnd, 0, 0, a_rwnd);
4937 		}
4938 		asoc->peers_rwnd = a_rwnd;
4939 		if (asoc->sent_queue_retran_cnt) {
4940 			asoc->sent_queue_retran_cnt = 0;
4941 		}
4942 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4943 			/* SWS sender side engages */
4944 			asoc->peers_rwnd = 0;
4945 		}
4946 		/* stop any timers */
4947 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4948 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4949 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4950 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
4951 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4952 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4953 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4954 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4955 				}
4956 			}
4957 			net->partial_bytes_acked = 0;
4958 			net->flight_size = 0;
4959 		}
4960 		asoc->total_flight = 0;
4961 		asoc->total_flight_count = 0;
4962 		return;
4963 	}
4964 	/*
4965 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4966 	 * things. The total byte count acked is tracked in netAckSz AND
4967 	 * netAck2 is used to track the total bytes acked that are un-
4968 	 * amibguious and were never retransmitted. We track these on a per
4969 	 * destination address basis.
4970 	 */
4971 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4972 		net->prev_cwnd = net->cwnd;
4973 		net->net_ack = 0;
4974 		net->net_ack2 = 0;
4975 
4976 		/*
4977 		 * CMT: Reset CUC and Fast recovery algo variables before
4978 		 * SACK processing
4979 		 */
4980 		net->new_pseudo_cumack = 0;
4981 		net->will_exit_fast_recovery = 0;
4982 	}
4983 	/* process the new consecutive TSN first */
4984 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4985 	while (tp1) {
4986 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
4987 		    MAX_TSN) ||
4988 		    last_tsn == tp1->rec.data.TSN_seq) {
4989 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4990 				/*
4991 				 * ECN Nonce: Add the nonce to the sender's
4992 				 * nonce sum
4993 				 */
4994 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4995 				accum_moved = 1;
4996 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4997 					/*
4998 					 * If it is less than ACKED, it is
4999 					 * now no-longer in flight. Higher
5000 					 * values may occur during marking
5001 					 */
5002 					if ((tp1->whoTo->dest_state &
5003 					    SCTP_ADDR_UNCONFIRMED) &&
5004 					    (tp1->snd_count < 2)) {
5005 						/*
5006 						 * If there was no retran
5007 						 * and the address is
5008 						 * un-confirmed and we sent
5009 						 * there and are now
5010 						 * sacked.. its confirmed,
5011 						 * mark it so.
5012 						 */
5013 						tp1->whoTo->dest_state &=
5014 						    ~SCTP_ADDR_UNCONFIRMED;
5015 					}
5016 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5017 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
5018 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
5019 							    tp1->whoTo->flight_size,
5020 							    tp1->book_size,
5021 							    (uintptr_t) tp1->whoTo,
5022 							    tp1->rec.data.TSN_seq);
5023 						}
5024 						sctp_flight_size_decrease(tp1);
5025 						sctp_total_flight_decrease(stcb, tp1);
5026 					}
5027 					tp1->whoTo->net_ack += tp1->send_size;
5028 
5029 					/* CMT SFR and DAC algos */
5030 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
5031 					tp1->whoTo->saw_newack = 1;
5032 
5033 					if (tp1->snd_count < 2) {
5034 						/*
5035 						 * True non-retransmited
5036 						 * chunk
5037 						 */
5038 						tp1->whoTo->net_ack2 +=
5039 						    tp1->send_size;
5040 
5041 						/* update RTO too? */
5042 						if (tp1->do_rtt) {
5043 							tp1->whoTo->RTO =
5044 							    sctp_calculate_rto(stcb,
5045 							    asoc, tp1->whoTo,
5046 							    &tp1->sent_rcv_time,
5047 							    sctp_align_safe_nocopy);
5048 							tp1->do_rtt = 0;
5049 						}
5050 					}
5051 					/*
5052 					 * CMT: CUCv2 algorithm. From the
5053 					 * cumack'd TSNs, for each TSN being
5054 					 * acked for the first time, set the
5055 					 * following variables for the
5056 					 * corresp destination.
5057 					 * new_pseudo_cumack will trigger a
5058 					 * cwnd update.
5059 					 * find_(rtx_)pseudo_cumack will
5060 					 * trigger search for the next
5061 					 * expected (rtx-)pseudo-cumack.
5062 					 */
5063 					tp1->whoTo->new_pseudo_cumack = 1;
5064 					tp1->whoTo->find_pseudo_cumack = 1;
5065 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
5066 
5067 
5068 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
5069 						sctp_log_sack(asoc->last_acked_seq,
5070 						    cum_ack,
5071 						    tp1->rec.data.TSN_seq,
5072 						    0,
5073 						    0,
5074 						    SCTP_LOG_TSN_ACKED);
5075 					}
5076 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
5077 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
5078 					}
5079 				}
5080 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5081 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
5082 #ifdef SCTP_AUDITING_ENABLED
5083 					sctp_audit_log(0xB3,
5084 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
5085 #endif
5086 				}
5087 				if (tp1->rec.data.chunk_was_revoked) {
5088 					/* deflate the cwnd */
5089 					tp1->whoTo->cwnd -= tp1->book_size;
5090 					tp1->rec.data.chunk_was_revoked = 0;
5091 				}
5092 				tp1->sent = SCTP_DATAGRAM_ACKED;
5093 			}
5094 		} else {
5095 			break;
5096 		}
5097 		tp1 = TAILQ_NEXT(tp1, sctp_next);
5098 	}
5099 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
5100 	/* always set this up to cum-ack */
5101 	asoc->this_sack_highest_gap = last_tsn;
5102 
5103 	/* Move offset up to point to gaps/dups */
5104 	offset += sizeof(struct sctp_sack_chunk);
5105 	if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_sack_chunk)) > sack_length) {
5106 
5107 		/* skip corrupt segments */
5108 		goto skip_segments;
5109 	}
5110 	if (num_seg > 0) {
5111 
5112 		/*
5113 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
5114 		 * to be greater than the cumack. Also reset saw_newack to 0
5115 		 * for all dests.
5116 		 */
5117 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5118 			net->saw_newack = 0;
5119 			net->this_sack_highest_newack = last_tsn;
5120 		}
5121 
5122 		/*
5123 		 * thisSackHighestGap will increase while handling NEW
5124 		 * segments this_sack_highest_newack will increase while
5125 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
5126 		 * used for CMT DAC algo. saw_newack will also change.
5127 		 */
5128 		sctp_handle_segments(m, &offset, stcb, asoc, ch, last_tsn,
5129 		    &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
5130 		    num_seg, &ecn_seg_sums);
5131 
5132 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
5133 			/*
5134 			 * validate the biggest_tsn_acked in the gap acks if
5135 			 * strict adherence is wanted.
5136 			 */
5137 			if ((biggest_tsn_acked == send_s) ||
5138 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
5139 				/*
5140 				 * peer is either confused or we are under
5141 				 * attack. We must abort.
5142 				 */
5143 				goto hopeless_peer;
5144 			}
5145 		}
5146 	}
5147 skip_segments:
5148 	/*******************************************/
5149 	/* cancel ALL T3-send timer if accum moved */
5150 	/*******************************************/
5151 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
5152 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5153 			if (net->new_pseudo_cumack)
5154 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5155 				    stcb, net,
5156 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
5157 
5158 		}
5159 	} else {
5160 		if (accum_moved) {
5161 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5162 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5163 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
5164 			}
5165 		}
5166 	}
5167 	/********************************************/
5168 	/* drop the acked chunks from the sendqueue */
5169 	/********************************************/
5170 	asoc->last_acked_seq = cum_ack;
5171 
5172 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
5173 	if (tp1 == NULL)
5174 		goto done_with_it;
5175 	do {
5176 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
5177 		    MAX_TSN)) {
5178 			break;
5179 		}
5180 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
5181 			/* no more sent on list */
5182 			printf("Warning, tp1->sent == %d and its now acked?\n",
5183 			    tp1->sent);
5184 		}
5185 		tp2 = TAILQ_NEXT(tp1, sctp_next);
5186 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
5187 		if (tp1->pr_sctp_on) {
5188 			if (asoc->pr_sctp_cnt != 0)
5189 				asoc->pr_sctp_cnt--;
5190 		}
5191 		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
5192 		    (asoc->total_flight > 0)) {
5193 #ifdef INVARIANTS
5194 			panic("Warning flight size is postive and should be 0");
5195 #else
5196 			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
5197 			    asoc->total_flight);
5198 #endif
5199 			asoc->total_flight = 0;
5200 		}
5201 		if (tp1->data) {
5202 			/* sa_ignore NO_NULL_CHK */
5203 			sctp_free_bufspace(stcb, asoc, tp1, 1);
5204 			sctp_m_freem(tp1->data);
5205 			if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
5206 				asoc->sent_queue_cnt_removeable--;
5207 			}
5208 		}
5209 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
5210 			sctp_log_sack(asoc->last_acked_seq,
5211 			    cum_ack,
5212 			    tp1->rec.data.TSN_seq,
5213 			    0,
5214 			    0,
5215 			    SCTP_LOG_FREE_SENT);
5216 		}
5217 		tp1->data = NULL;
5218 		asoc->sent_queue_cnt--;
5219 		sctp_free_a_chunk(stcb, tp1);
5220 		wake_him++;
5221 		tp1 = tp2;
5222 	} while (tp1 != NULL);
5223 
5224 done_with_it:
5225 	/* sa_ignore NO_NULL_CHK */
5226 	if ((wake_him) && (stcb->sctp_socket)) {
5227 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5228 		struct socket *so;
5229 
5230 #endif
5231 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
5232 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
5233 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
5234 		}
5235 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5236 		so = SCTP_INP_SO(stcb->sctp_ep);
5237 		atomic_add_int(&stcb->asoc.refcnt, 1);
5238 		SCTP_TCB_UNLOCK(stcb);
5239 		SCTP_SOCKET_LOCK(so, 1);
5240 		SCTP_TCB_LOCK(stcb);
5241 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
5242 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
5243 			/* assoc was freed while we were unlocked */
5244 			SCTP_SOCKET_UNLOCK(so, 1);
5245 			return;
5246 		}
5247 #endif
5248 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
5249 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5250 		SCTP_SOCKET_UNLOCK(so, 1);
5251 #endif
5252 	} else {
5253 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
5254 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
5255 		}
5256 	}
5257 
5258 	if (asoc->fast_retran_loss_recovery && accum_moved) {
5259 		if (compare_with_wrap(asoc->last_acked_seq,
5260 		    asoc->fast_recovery_tsn, MAX_TSN) ||
5261 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
5262 			/* Setup so we will exit RFC2582 fast recovery */
5263 			will_exit_fast_recovery = 1;
5264 		}
5265 	}
5266 	/*
5267 	 * Check for revoked fragments:
5268 	 *
5269 	 * if Previous sack - Had no frags then we can't have any revoked if
5270 	 * Previous sack - Had frag's then - If we now have frags aka
5271 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
5272 	 * some of them. else - The peer revoked all ACKED fragments, since
5273 	 * we had some before and now we have NONE.
5274 	 */
5275 
5276 	if (num_seg)
5277 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
5278 	else if (asoc->saw_sack_with_frags) {
5279 		int cnt_revoked = 0;
5280 
5281 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
5282 		if (tp1 != NULL) {
5283 			/* Peer revoked all dg's marked or acked */
5284 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5285 				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
5286 				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
5287 					tp1->sent = SCTP_DATAGRAM_SENT;
5288 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
5289 						sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
5290 						    tp1->whoTo->flight_size,
5291 						    tp1->book_size,
5292 						    (uintptr_t) tp1->whoTo,
5293 						    tp1->rec.data.TSN_seq);
5294 					}
5295 					sctp_flight_size_increase(tp1);
5296 					sctp_total_flight_increase(stcb, tp1);
5297 					tp1->rec.data.chunk_was_revoked = 1;
5298 					/*
5299 					 * To ensure that this increase in
5300 					 * flightsize, which is artificial,
5301 					 * does not throttle the sender, we
5302 					 * also increase the cwnd
5303 					 * artificially.
5304 					 */
5305 					tp1->whoTo->cwnd += tp1->book_size;
5306 					cnt_revoked++;
5307 				}
5308 			}
5309 			if (cnt_revoked) {
5310 				reneged_all = 1;
5311 			}
5312 		}
5313 		asoc->saw_sack_with_frags = 0;
5314 	}
5315 	if (num_seg)
5316 		asoc->saw_sack_with_frags = 1;
5317 	else
5318 		asoc->saw_sack_with_frags = 0;
5319 
5320 	/* JRS - Use the congestion control given in the CC module */
5321 	asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
5322 
5323 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
5324 		/* nothing left in-flight */
5325 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5326 			/* stop all timers */
5327 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5328 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5329 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5330 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5331 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
5332 				}
5333 			}
5334 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5335 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
5336 			net->flight_size = 0;
5337 			net->partial_bytes_acked = 0;
5338 		}
5339 		asoc->total_flight = 0;
5340 		asoc->total_flight_count = 0;
5341 	}
5342 	/**********************************/
5343 	/* Now what about shutdown issues */
5344 	/**********************************/
5345 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
5346 		/* nothing left on sendqueue.. consider done */
5347 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5348 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5349 			    asoc->peers_rwnd, 0, 0, a_rwnd);
5350 		}
5351 		asoc->peers_rwnd = a_rwnd;
5352 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5353 			/* SWS sender side engages */
5354 			asoc->peers_rwnd = 0;
5355 		}
5356 		/* clean up */
5357 		if ((asoc->stream_queue_cnt == 1) &&
5358 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
5359 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
5360 		    (asoc->locked_on_sending)
5361 		    ) {
5362 			struct sctp_stream_queue_pending *sp;
5363 
5364 			/*
5365 			 * I may be in a state where we got all across.. but
5366 			 * cannot write more due to a shutdown... we abort
5367 			 * since the user did not indicate EOR in this case.
5368 			 */
5369 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
5370 			    sctp_streamhead);
5371 			if ((sp) && (sp->length == 0)) {
5372 				asoc->locked_on_sending = NULL;
5373 				if (sp->msg_is_complete) {
5374 					asoc->stream_queue_cnt--;
5375 				} else {
5376 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
5377 					asoc->stream_queue_cnt--;
5378 				}
5379 			}
5380 		}
5381 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
5382 		    (asoc->stream_queue_cnt == 0)) {
5383 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5384 				/* Need to abort here */
5385 				struct mbuf *oper;
5386 
5387 		abort_out_now:
5388 				*abort_now = 1;
5389 				/* XXX */
5390 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
5391 				    0, M_DONTWAIT, 1, MT_DATA);
5392 				if (oper) {
5393 					struct sctp_paramhdr *ph;
5394 					uint32_t *ippp;
5395 
5396 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5397 					    sizeof(uint32_t);
5398 					ph = mtod(oper, struct sctp_paramhdr *);
5399 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
5400 					ph->param_length = htons(SCTP_BUF_LEN(oper));
5401 					ippp = (uint32_t *) (ph + 1);
5402 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
5403 				}
5404 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
5405 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
5406 				return;
5407 			} else {
5408 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
5409 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
5410 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5411 				}
5412 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
5413 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5414 				sctp_stop_timers_for_shutdown(stcb);
5415 				sctp_send_shutdown(stcb,
5416 				    stcb->asoc.primary_destination);
5417 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
5418 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5419 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
5420 				    stcb->sctp_ep, stcb, asoc->primary_destination);
5421 			}
5422 			return;
5423 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
5424 		    (asoc->stream_queue_cnt == 0)) {
5425 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
5426 				goto abort_out_now;
5427 			}
5428 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
5429 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
5430 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
5431 			sctp_send_shutdown_ack(stcb,
5432 			    stcb->asoc.primary_destination);
5433 
5434 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
5435 			    stcb->sctp_ep, stcb, asoc->primary_destination);
5436 			return;
5437 		}
5438 	}
5439 	/*
5440 	 * Now here we are going to recycle net_ack for a different use...
5441 	 * HEADS UP.
5442 	 */
5443 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5444 		net->net_ack = 0;
5445 	}
5446 
5447 	/*
5448 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5449 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5450 	 * automatically ensure that.
5451 	 */
5452 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
5453 		this_sack_lowest_newack = cum_ack;
5454 	}
5455 	if (num_seg > 0) {
5456 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5457 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5458 	}
5459 	/* JRS - Use the congestion control given in the CC module */
5460 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5461 
5462 	/******************************************************************
5463 	 *  Here we do the stuff with ECN Nonce checking.
5464 	 *  We basically check to see if the nonce sum flag was incorrect
5465 	 *  or if resynchronization needs to be done. Also if we catch a
5466 	 *  misbehaving receiver we give him the kick.
5467 	 ******************************************************************/
5468 
5469 	if (asoc->ecn_nonce_allowed) {
5470 		if (asoc->nonce_sum_check) {
5471 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5472 				if (asoc->nonce_wait_for_ecne == 0) {
5473 					struct sctp_tmit_chunk *lchk;
5474 
5475 					lchk = TAILQ_FIRST(&asoc->send_queue);
5476 					asoc->nonce_wait_for_ecne = 1;
5477 					if (lchk) {
5478 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5479 					} else {
5480 						asoc->nonce_wait_tsn = asoc->sending_seq;
5481 					}
5482 				} else {
5483 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5484 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5485 						/*
5486 						 * Misbehaving peer. We need
5487 						 * to react to this guy
5488 						 */
5489 						asoc->ecn_allowed = 0;
5490 						asoc->ecn_nonce_allowed = 0;
5491 					}
5492 				}
5493 			}
5494 		} else {
5495 			/* See if Resynchronization Possible */
5496 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5497 				asoc->nonce_sum_check = 1;
5498 				/*
5499 				 * now we must calculate what the base is.
5500 				 * We do this based on two things, we know
5501 				 * the total's for all the segments
5502 				 * gap-acked in the SACK, its stored in
5503 				 * ecn_seg_sums. We also know the SACK's
5504 				 * nonce sum, its in nonce_sum_flag. So we
5505 				 * can build a truth table to back-calculate
5506 				 * the new value of
5507 				 * asoc->nonce_sum_expect_base:
5508 				 *
5509 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
5510 				 * 1                    0 1 0 1 1 1
5511 				 * 1 0
5512 				 */
5513 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5514 			}
5515 		}
5516 	}
5517 	/* Now are we exiting loss recovery ? */
5518 	if (will_exit_fast_recovery) {
5519 		/* Ok, we must exit fast recovery */
5520 		asoc->fast_retran_loss_recovery = 0;
5521 	}
5522 	if ((asoc->sat_t3_loss_recovery) &&
5523 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5524 	    MAX_TSN) ||
5525 	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5526 		/* end satellite t3 loss recovery */
5527 		asoc->sat_t3_loss_recovery = 0;
5528 	}
5529 	/*
5530 	 * CMT Fast recovery
5531 	 */
5532 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5533 		if (net->will_exit_fast_recovery) {
5534 			/* Ok, we must exit fast recovery */
5535 			net->fast_retran_loss_recovery = 0;
5536 		}
5537 	}
5538 
5539 	/* Adjust and set the new rwnd value */
5540 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
5541 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5542 		    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
5543 	}
5544 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5545 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
5546 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5547 		/* SWS sender side engages */
5548 		asoc->peers_rwnd = 0;
5549 	}
5550 	if (asoc->peers_rwnd > old_rwnd) {
5551 		win_probe_recovery = 1;
5552 	}
5553 	/*
5554 	 * Now we must setup so we have a timer up for anyone with
5555 	 * outstanding data.
5556 	 */
5557 	done_once = 0;
5558 again:
5559 	j = 0;
5560 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5561 		if (win_probe_recovery && (net->window_probe)) {
5562 			win_probe_recovered = 1;
5563 			/*-
5564 			 * Find first chunk that was used with
5565 			 * window probe and clear the event. Put
5566 			 * it back into the send queue as if has
5567 			 * not been sent.
5568 			 */
5569 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5570 				if (tp1->window_probe) {
5571 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
5572 					break;
5573 				}
5574 			}
5575 		}
5576 		if (net->flight_size) {
5577 			j++;
5578 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5579 			    stcb->sctp_ep, stcb, net);
5580 			if (net->window_probe) {
5581 			}
5582 		} else {
5583 			if (net->window_probe) {
5584 				/*
5585 				 * In window probes we must assure a timer
5586 				 * is still running there
5587 				 */
5588 
5589 				if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5590 					sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5591 					    stcb->sctp_ep, stcb, net);
5592 
5593 				}
5594 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5595 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5596 				    stcb, net,
5597 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5598 			}
5599 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
5600 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5601 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5602 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5603 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5604 				}
5605 			}
5606 		}
5607 	}
5608 	if ((j == 0) &&
5609 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5610 	    (asoc->sent_queue_retran_cnt == 0) &&
5611 	    (win_probe_recovered == 0) &&
5612 	    (done_once == 0)) {
5613 		/*
5614 		 * huh, this should not happen unless all packets are
5615 		 * PR-SCTP and marked to skip of course.
5616 		 */
5617 		if (sctp_fs_audit(asoc)) {
5618 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5619 				net->flight_size = 0;
5620 			}
5621 			asoc->total_flight = 0;
5622 			asoc->total_flight_count = 0;
5623 			asoc->sent_queue_retran_cnt = 0;
5624 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5625 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5626 					sctp_flight_size_increase(tp1);
5627 					sctp_total_flight_increase(stcb, tp1);
5628 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5629 					asoc->sent_queue_retran_cnt++;
5630 				}
5631 			}
5632 		}
5633 		done_once = 1;
5634 		goto again;
5635 	}
5636 	/* Fix up the a-p-a-p for future PR-SCTP sends */
5637 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5638 		asoc->advanced_peer_ack_point = cum_ack;
5639 	}
5640 	/* C2. try to further move advancedPeerAckPoint ahead */
5641 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5642 		struct sctp_tmit_chunk *lchk;
5643 		uint32_t old_adv_peer_ack_point;
5644 
5645 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
5646 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5647 		/* C3. See if we need to send a Fwd-TSN */
5648 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5649 		    MAX_TSN)) {
5650 			/*
5651 			 * ISSUE with ECN, see FWD-TSN processing for notes
5652 			 * on issues that will occur when the ECN NONCE
5653 			 * stuff is put into SCTP for cross checking.
5654 			 */
5655 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
5656 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
5657 				    0xee, cum_ack, asoc->advanced_peer_ack_point,
5658 				    old_adv_peer_ack_point);
5659 			}
5660 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
5661 			    MAX_TSN)) {
5662 				send_forward_tsn(stcb, asoc);
5663 				/*
5664 				 * ECN Nonce: Disable Nonce Sum check when
5665 				 * FWD TSN is sent and store resync tsn
5666 				 */
5667 				asoc->nonce_sum_check = 0;
5668 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
5669 			} else if (lchk) {
5670 				/* try to FR fwd-tsn's that get lost too */
5671 				lchk->rec.data.fwd_tsn_cnt++;
5672 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
5673 					send_forward_tsn(stcb, asoc);
5674 					lchk->rec.data.fwd_tsn_cnt = 0;
5675 				}
5676 			}
5677 		}
5678 		if (lchk) {
5679 			/* Assure a timer is up */
5680 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5681 			    stcb->sctp_ep, stcb, lchk->whoTo);
5682 		}
5683 	}
5684 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
5685 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5686 		    a_rwnd,
5687 		    stcb->asoc.peers_rwnd,
5688 		    stcb->asoc.total_flight,
5689 		    stcb->asoc.total_output_queue_size);
5690 	}
5691 }
5692 
5693 void
5694 sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5695     struct sctp_nets *netp, int *abort_flag)
5696 {
5697 	/* Copy cum-ack */
5698 	uint32_t cum_ack, a_rwnd;
5699 
5700 	cum_ack = ntohl(cp->cumulative_tsn_ack);
5701 	/* Arrange so a_rwnd does NOT change */
5702 	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5703 
5704 	/* Now call the express sack handling */
5705 	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5706 }
5707 
5708 static void
5709 sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5710     struct sctp_stream_in *strmin)
5711 {
5712 	struct sctp_queued_to_read *ctl, *nctl;
5713 	struct sctp_association *asoc;
5714 	int tt;
5715 
5716 	/* EY -used to calculate nr_gap information */
5717 	uint32_t nr_tsn, nr_gap;
5718 
5719 	asoc = &stcb->asoc;
5720 	tt = strmin->last_sequence_delivered;
5721 	/*
5722 	 * First deliver anything prior to and including the stream no that
5723 	 * came in
5724 	 */
5725 	ctl = TAILQ_FIRST(&strmin->inqueue);
5726 	while (ctl) {
5727 		nctl = TAILQ_NEXT(ctl, next);
5728 		if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5729 		    (tt == ctl->sinfo_ssn)) {
5730 			/* this is deliverable now */
5731 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5732 			/* subtract pending on streams */
5733 			asoc->size_on_all_streams -= ctl->length;
5734 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5735 			/* deliver it to at least the delivery-q */
5736 			if (stcb->sctp_socket) {
5737 				/* EY need the tsn info for calculating nr */
5738 				nr_tsn = ctl->sinfo_tsn;
5739 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5740 				    ctl,
5741 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5742 				/*
5743 				 * EY this is the chunk that should be
5744 				 * tagged nr gapped calculate the gap and
5745 				 * such then tag this TSN nr
5746 				 * chk->rec.data.TSN_seq
5747 				 */
5748 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
5749 
5750 					SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
5751 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
5752 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
5753 						/*
5754 						 * EY These should never
5755 						 * happen- explained before
5756 						 */
5757 					} else {
5758 						SCTP_TCB_LOCK_ASSERT(stcb);
5759 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
5760 						SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
5761 						if (compare_with_wrap(nr_tsn,
5762 						    asoc->highest_tsn_inside_nr_map,
5763 						    MAX_TSN))
5764 							asoc->highest_tsn_inside_nr_map = nr_tsn;
5765 					}
5766 					if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, nr_gap))
5767 						/*
5768 						 * printf("In
5769 						 * sctp_kick_prsctp_reorder_q
5770 						 * ueue(7): Something wrong,
5771 						 * the TSN to be tagged"
5772 						 * "\nas NR is not even in
5773 						 * the mapping_array, or map
5774 						 * and nr_map are
5775 						 * inconsistent");
5776 						 */
5777 						/*
5778 						 * EY - not %100 sure about
5779 						 * the lock thing, don't
5780 						 * think its required
5781 						 */
5782 						/*
5783 						 * SCTP_TCB_LOCK_ASSERT(stcb)
5784 						 * ;
5785 						 */
5786 					{
5787 						/*
5788 						 * printf("\nCalculating an
5789 						 * nr_gap!!\nmapping_array_si
5790 						 * ze = %d
5791 						 * nr_mapping_array_size =
5792 						 * %d" "\nmapping_array_base
5793 						 * = %d
5794 						 * nr_mapping_array_base =
5795 						 * %d\nhighest_tsn_inside_map
5796 						 *  = %d"
5797 						 * "highest_tsn_inside_nr_map
5798 						 *  = %d\nTSN = %d nr_gap =
5799 						 * %d",asoc->mapping_array_si
5800 						 * ze,
5801 						 * asoc->nr_mapping_array_siz
5802 						 * e,
5803 						 * asoc->mapping_array_base_t
5804 						 * sn,
5805 						 * asoc->nr_mapping_array_bas
5806 						 * e_tsn,
5807 						 * asoc->highest_tsn_inside_m
5808 						 * ap,
5809 						 * asoc->highest_tsn_inside_n
5810 						 * r_map,tsn,nr_gap);
5811 						 */
5812 					}
5813 				}
5814 			}
5815 		} else {
5816 			/* no more delivery now. */
5817 			break;
5818 		}
5819 		ctl = nctl;
5820 	}
5821 	/*
5822 	 * now we must deliver things in queue the normal way  if any are
5823 	 * now ready.
5824 	 */
5825 	tt = strmin->last_sequence_delivered + 1;
5826 	ctl = TAILQ_FIRST(&strmin->inqueue);
5827 	while (ctl) {
5828 		nctl = TAILQ_NEXT(ctl, next);
5829 		if (tt == ctl->sinfo_ssn) {
5830 			/* this is deliverable now */
5831 			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5832 			/* subtract pending on streams */
5833 			asoc->size_on_all_streams -= ctl->length;
5834 			sctp_ucount_decr(asoc->cnt_on_all_streams);
5835 			/* deliver it to at least the delivery-q */
5836 			strmin->last_sequence_delivered = ctl->sinfo_ssn;
5837 			if (stcb->sctp_socket) {
5838 				/* EY */
5839 				nr_tsn = ctl->sinfo_tsn;
5840 				sctp_add_to_readq(stcb->sctp_ep, stcb,
5841 				    ctl,
5842 				    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_HELD, SCTP_SO_NOT_LOCKED);
5843 				/*
5844 				 * EY this is the chunk that should be
5845 				 * tagged nr gapped calculate the gap and
5846 				 * such then tag this TSN nr
5847 				 * chk->rec.data.TSN_seq
5848 				 */
5849 				if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
5850 					SCTP_CALC_TSN_TO_GAP(nr_gap, nr_tsn, asoc->nr_mapping_array_base_tsn);
5851 					if ((nr_gap >= (SCTP_NR_MAPPING_ARRAY << 3)) ||
5852 					    (nr_gap >= (uint32_t) (asoc->nr_mapping_array_size << 3))) {
5853 						/*
5854 						 * EY These should never
5855 						 * happen, explained before
5856 						 */
5857 					} else {
5858 						SCTP_TCB_LOCK_ASSERT(stcb);
5859 						SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, nr_gap);
5860 						SCTP_REVERSE_OUT_TSN_PRES(nr_gap, nr_tsn, asoc);
5861 						if (compare_with_wrap(nr_tsn, asoc->highest_tsn_inside_nr_map,
5862 						    MAX_TSN))
5863 							asoc->highest_tsn_inside_nr_map = nr_tsn;
5864 					}
5865 					if (!SCTP_IS_TSN_PRESENT(asoc->mapping_array, nr_gap))
5866 						/*
5867 						 * printf("In
5868 						 * sctp_kick_prsctp_reorder_q
5869 						 * ueue(8): Something wrong,
5870 						 * the TSN to be tagged"
5871 						 * "\nas NR is not even in
5872 						 * the mapping_array, or map
5873 						 * and nr_map are
5874 						 * inconsistent");
5875 						 */
5876 						/*
5877 						 * EY - not %100 sure about
5878 						 * the lock thing, don't
5879 						 * think its required
5880 						 */
5881 						/*
5882 						 * SCTP_TCB_LOCK_ASSERT(stcb)
5883 						 * ;
5884 						 */
5885 					{
5886 						/*
5887 						 * printf("\nCalculating an
5888 						 * nr_gap!!\nmapping_array_si
5889 						 * ze = %d
5890 						 * nr_mapping_array_size =
5891 						 * %d" "\nmapping_array_base
5892 						 * = %d
5893 						 * nr_mapping_array_base =
5894 						 * %d\nhighest_tsn_inside_map
5895 						 *  = %d"
5896 						 * "highest_tsn_inside_nr_map
5897 						 *  = %d\nTSN = %d nr_gap =
5898 						 * %d",asoc->mapping_array_si
5899 						 * ze,
5900 						 * asoc->nr_mapping_array_siz
5901 						 * e,
5902 						 * asoc->mapping_array_base_t
5903 						 * sn,
5904 						 * asoc->nr_mapping_array_bas
5905 						 * e_tsn,
5906 						 * asoc->highest_tsn_inside_m
5907 						 * ap,
5908 						 * asoc->highest_tsn_inside_n
5909 						 * r_map,tsn,nr_gap);
5910 						 */
5911 					}
5912 				}
5913 			}
5914 			tt = strmin->last_sequence_delivered + 1;
5915 		} else {
5916 			break;
5917 		}
5918 		ctl = nctl;
5919 	}
5920 }
5921 
5922 static void
5923 sctp_flush_reassm_for_str_seq(struct sctp_tcb *stcb,
5924     struct sctp_association *asoc,
5925     uint16_t stream, uint16_t seq)
5926 {
5927 	struct sctp_tmit_chunk *chk, *at;
5928 
5929 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5930 		/* For each one on here see if we need to toss it */
5931 		/*
5932 		 * For now large messages held on the reasmqueue that are
5933 		 * complete will be tossed too. We could in theory do more
5934 		 * work to spin through and stop after dumping one msg aka
5935 		 * seeing the start of a new msg at the head, and call the
5936 		 * delivery function... to see if it can be delivered... But
5937 		 * for now we just dump everything on the queue.
5938 		 */
5939 		chk = TAILQ_FIRST(&asoc->reasmqueue);
5940 		while (chk) {
5941 			at = TAILQ_NEXT(chk, sctp_next);
5942 			/*
5943 			 * Do not toss it if on a different stream or marked
5944 			 * for unordered delivery in which case the stream
5945 			 * sequence number has no meaning.
5946 			 */
5947 			if ((chk->rec.data.stream_number != stream) ||
5948 			    ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == SCTP_DATA_UNORDERED)) {
5949 				chk = at;
5950 				continue;
5951 			}
5952 			if (chk->rec.data.stream_seq == seq) {
5953 				/* It needs to be tossed */
5954 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5955 				if (compare_with_wrap(chk->rec.data.TSN_seq,
5956 				    asoc->tsn_last_delivered, MAX_TSN)) {
5957 					asoc->tsn_last_delivered =
5958 					    chk->rec.data.TSN_seq;
5959 					asoc->str_of_pdapi =
5960 					    chk->rec.data.stream_number;
5961 					asoc->ssn_of_pdapi =
5962 					    chk->rec.data.stream_seq;
5963 					asoc->fragment_flags =
5964 					    chk->rec.data.rcv_flags;
5965 				}
5966 				asoc->size_on_reasm_queue -= chk->send_size;
5967 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5968 
5969 				/* Clear up any stream problem */
5970 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
5971 				    SCTP_DATA_UNORDERED &&
5972 				    (compare_with_wrap(chk->rec.data.stream_seq,
5973 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
5974 				    MAX_SEQ))) {
5975 					/*
5976 					 * We must dump forward this streams
5977 					 * sequence number if the chunk is
5978 					 * not unordered that is being
5979 					 * skipped. There is a chance that
5980 					 * if the peer does not include the
5981 					 * last fragment in its FWD-TSN we
5982 					 * WILL have a problem here since
5983 					 * you would have a partial chunk in
5984 					 * queue that may not be
5985 					 * deliverable. Also if a Partial
5986 					 * delivery API as started the user
5987 					 * may get a partial chunk. The next
5988 					 * read returning a new chunk...
5989 					 * really ugly but I see no way
5990 					 * around it! Maybe a notify??
5991 					 */
5992 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
5993 					    chk->rec.data.stream_seq;
5994 				}
5995 				if (chk->data) {
5996 					sctp_m_freem(chk->data);
5997 					chk->data = NULL;
5998 				}
5999 				sctp_free_a_chunk(stcb, chk);
6000 			} else if (compare_with_wrap(chk->rec.data.stream_seq, seq, MAX_SEQ)) {
6001 				/*
6002 				 * If the stream_seq is > than the purging
6003 				 * one, we are done
6004 				 */
6005 				break;
6006 			}
6007 			chk = at;
6008 		}
6009 	}
6010 }
6011 
6012 
6013 void
6014 sctp_handle_forward_tsn(struct sctp_tcb *stcb,
6015     struct sctp_forward_tsn_chunk *fwd, int *abort_flag, struct mbuf *m, int offset)
6016 {
6017 	/*
6018 	 * ISSUES that MUST be fixed for ECN! When we are the sender of the
6019 	 * forward TSN, when the SACK comes back that acknowledges the
6020 	 * FWD-TSN we must reset the NONCE sum to match correctly. This will
6021 	 * get quite tricky since we may have sent more data interveneing
6022 	 * and must carefully account for what the SACK says on the nonce
6023 	 * and any gaps that are reported. This work will NOT be done here,
6024 	 * but I note it here since it is really related to PR-SCTP and
6025 	 * FWD-TSN's
6026 	 */
6027 
6028 	/* The pr-sctp fwd tsn */
6029 	/*
6030 	 * here we will perform all the data receiver side steps for
6031 	 * processing FwdTSN, as required in by pr-sctp draft:
6032 	 *
6033 	 * Assume we get FwdTSN(x):
6034 	 *
6035 	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
6036 	 * others we have 3) examine and update re-ordering queue on
6037 	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
6038 	 * report where we are.
6039 	 */
6040 	struct sctp_association *asoc;
6041 	uint32_t new_cum_tsn, gap;
6042 	unsigned int i, fwd_sz, cumack_set_flag, m_size;
6043 	uint32_t str_seq;
6044 	struct sctp_stream_in *strm;
6045 	struct sctp_tmit_chunk *chk, *at;
6046 	struct sctp_queued_to_read *ctl, *sv;
6047 
6048 	cumack_set_flag = 0;
6049 	asoc = &stcb->asoc;
6050 	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
6051 		SCTPDBG(SCTP_DEBUG_INDATA1,
6052 		    "Bad size too small/big fwd-tsn\n");
6053 		return;
6054 	}
6055 	m_size = (stcb->asoc.mapping_array_size << 3);
6056 	/*************************************************************/
6057 	/* 1. Here we update local cumTSN and shift the bitmap array */
6058 	/*************************************************************/
6059 	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
6060 
6061 	if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
6062 	    asoc->cumulative_tsn == new_cum_tsn) {
6063 		/* Already got there ... */
6064 		return;
6065 	}
6066 	if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
6067 	    MAX_TSN)) {
6068 		asoc->highest_tsn_inside_map = new_cum_tsn;
6069 		/* EY nr_mapping_array version of the above */
6070 		/*
6071 		 * if(SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) &&
6072 		 * asoc->peer_supports_nr_sack)
6073 		 */
6074 		asoc->highest_tsn_inside_nr_map = new_cum_tsn;
6075 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6076 			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6077 		}
6078 	}
6079 	/*
6080 	 * now we know the new TSN is more advanced, let's find the actual
6081 	 * gap
6082 	 */
6083 	SCTP_CALC_TSN_TO_GAP(gap, new_cum_tsn, asoc->mapping_array_base_tsn);
6084 	if (gap >= m_size) {
6085 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6086 			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6087 		}
6088 		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
6089 			struct mbuf *oper;
6090 
6091 			/*
6092 			 * out of range (of single byte chunks in the rwnd I
6093 			 * give out). This must be an attacker.
6094 			 */
6095 			*abort_flag = 1;
6096 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
6097 			    0, M_DONTWAIT, 1, MT_DATA);
6098 			if (oper) {
6099 				struct sctp_paramhdr *ph;
6100 				uint32_t *ippp;
6101 
6102 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6103 				    (sizeof(uint32_t) * 3);
6104 				ph = mtod(oper, struct sctp_paramhdr *);
6105 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
6106 				ph->param_length = htons(SCTP_BUF_LEN(oper));
6107 				ippp = (uint32_t *) (ph + 1);
6108 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
6109 				ippp++;
6110 				*ippp = asoc->highest_tsn_inside_map;
6111 				ippp++;
6112 				*ippp = new_cum_tsn;
6113 			}
6114 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
6115 			sctp_abort_an_association(stcb->sctp_ep, stcb,
6116 			    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
6117 			return;
6118 		}
6119 		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
6120 		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
6121 		cumack_set_flag = 1;
6122 		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
6123 		asoc->cumulative_tsn = asoc->highest_tsn_inside_map = new_cum_tsn;
6124 		/* EY - nr_sack: nr_mapping_array version of the above */
6125 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) {
6126 			memset(stcb->asoc.nr_mapping_array, 0, stcb->asoc.nr_mapping_array_size);
6127 			asoc->nr_mapping_array_base_tsn = new_cum_tsn + 1;
6128 			asoc->highest_tsn_inside_nr_map = new_cum_tsn;
6129 			if (asoc->nr_mapping_array_size != asoc->mapping_array_size) {
6130 				/*
6131 				 * printf("IN sctp_handle_forward_tsn:
6132 				 * Something is wrong the size of" "map and
6133 				 * nr_map should be equal!")
6134 				 */ ;
6135 			}
6136 		}
6137 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6138 			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6139 		}
6140 		asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
6141 	} else {
6142 		SCTP_TCB_LOCK_ASSERT(stcb);
6143 		for (i = 0; i <= gap; i++) {
6144 			if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack
6145 			    && SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
6146 				SCTP_SET_TSN_PRESENT(asoc->nr_mapping_array, i);
6147 			} else {
6148 				SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
6149 			}
6150 		}
6151 		/*
6152 		 * Now after marking all, slide thing forward but no sack
6153 		 * please.
6154 		 */
6155 		sctp_sack_check(stcb, 0, 0, abort_flag);
6156 		if (*abort_flag)
6157 			return;
6158 	}
6159 	/*************************************************************/
6160 	/* 2. Clear up re-assembly queue                             */
6161 	/*************************************************************/
6162 	/*
6163 	 * First service it if pd-api is up, just in case we can progress it
6164 	 * forward
6165 	 */
6166 	if (asoc->fragmented_delivery_inprogress) {
6167 		sctp_service_reassembly(stcb, asoc);
6168 	}
6169 	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
6170 		/* For each one on here see if we need to toss it */
6171 		/*
6172 		 * For now large messages held on the reasmqueue that are
6173 		 * complete will be tossed too. We could in theory do more
6174 		 * work to spin through and stop after dumping one msg aka
6175 		 * seeing the start of a new msg at the head, and call the
6176 		 * delivery function... to see if it can be delivered... But
6177 		 * for now we just dump everything on the queue.
6178 		 */
6179 		chk = TAILQ_FIRST(&asoc->reasmqueue);
6180 		while (chk) {
6181 			at = TAILQ_NEXT(chk, sctp_next);
6182 			if ((compare_with_wrap(new_cum_tsn,
6183 			    chk->rec.data.TSN_seq, MAX_TSN)) ||
6184 			    (new_cum_tsn == chk->rec.data.TSN_seq)) {
6185 				/* It needs to be tossed */
6186 				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6187 				if (compare_with_wrap(chk->rec.data.TSN_seq,
6188 				    asoc->tsn_last_delivered, MAX_TSN)) {
6189 					asoc->tsn_last_delivered =
6190 					    chk->rec.data.TSN_seq;
6191 					asoc->str_of_pdapi =
6192 					    chk->rec.data.stream_number;
6193 					asoc->ssn_of_pdapi =
6194 					    chk->rec.data.stream_seq;
6195 					asoc->fragment_flags =
6196 					    chk->rec.data.rcv_flags;
6197 				}
6198 				asoc->size_on_reasm_queue -= chk->send_size;
6199 				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6200 
6201 				/* Clear up any stream problem */
6202 				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
6203 				    SCTP_DATA_UNORDERED &&
6204 				    (compare_with_wrap(chk->rec.data.stream_seq,
6205 				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
6206 				    MAX_SEQ))) {
6207 					/*
6208 					 * We must dump forward this streams
6209 					 * sequence number if the chunk is
6210 					 * not unordered that is being
6211 					 * skipped. There is a chance that
6212 					 * if the peer does not include the
6213 					 * last fragment in its FWD-TSN we
6214 					 * WILL have a problem here since
6215 					 * you would have a partial chunk in
6216 					 * queue that may not be
6217 					 * deliverable. Also if a Partial
6218 					 * delivery API as started the user
6219 					 * may get a partial chunk. The next
6220 					 * read returning a new chunk...
6221 					 * really ugly but I see no way
6222 					 * around it! Maybe a notify??
6223 					 */
6224 					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
6225 					    chk->rec.data.stream_seq;
6226 				}
6227 				if (chk->data) {
6228 					sctp_m_freem(chk->data);
6229 					chk->data = NULL;
6230 				}
6231 				sctp_free_a_chunk(stcb, chk);
6232 			} else {
6233 				/*
6234 				 * Ok we have gone beyond the end of the
6235 				 * fwd-tsn's mark.
6236 				 */
6237 				break;
6238 			}
6239 			chk = at;
6240 		}
6241 	}
6242 	/*******************************************************/
6243 	/* 3. Update the PR-stream re-ordering queues and fix  */
6244 	/* delivery issues as needed.                       */
6245 	/*******************************************************/
6246 	fwd_sz -= sizeof(*fwd);
6247 	if (m && fwd_sz) {
6248 		/* New method. */
6249 		unsigned int num_str;
6250 		struct sctp_strseq *stseq, strseqbuf;
6251 
6252 		offset += sizeof(*fwd);
6253 
6254 		SCTP_INP_READ_LOCK(stcb->sctp_ep);
6255 		num_str = fwd_sz / sizeof(struct sctp_strseq);
6256 		for (i = 0; i < num_str; i++) {
6257 			uint16_t st;
6258 
6259 			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
6260 			    sizeof(struct sctp_strseq),
6261 			    (uint8_t *) & strseqbuf);
6262 			offset += sizeof(struct sctp_strseq);
6263 			if (stseq == NULL) {
6264 				break;
6265 			}
6266 			/* Convert */
6267 			st = ntohs(stseq->stream);
6268 			stseq->stream = st;
6269 			st = ntohs(stseq->sequence);
6270 			stseq->sequence = st;
6271 
6272 			/* now process */
6273 
6274 			/*
6275 			 * Ok we now look for the stream/seq on the read
6276 			 * queue where its not all delivered. If we find it
6277 			 * we transmute the read entry into a PDI_ABORTED.
6278 			 */
6279 			if (stseq->stream >= asoc->streamincnt) {
6280 				/* screwed up streams, stop!  */
6281 				break;
6282 			}
6283 			if ((asoc->str_of_pdapi == stseq->stream) &&
6284 			    (asoc->ssn_of_pdapi == stseq->sequence)) {
6285 				/*
6286 				 * If this is the one we were partially
6287 				 * delivering now then we no longer are.
6288 				 * Note this will change with the reassembly
6289 				 * re-write.
6290 				 */
6291 				asoc->fragmented_delivery_inprogress = 0;
6292 			}
6293 			sctp_flush_reassm_for_str_seq(stcb, asoc, stseq->stream, stseq->sequence);
6294 			TAILQ_FOREACH(ctl, &stcb->sctp_ep->read_queue, next) {
6295 				if ((ctl->sinfo_stream == stseq->stream) &&
6296 				    (ctl->sinfo_ssn == stseq->sequence)) {
6297 					str_seq = (stseq->stream << 16) | stseq->sequence;
6298 					ctl->end_added = 1;
6299 					ctl->pdapi_aborted = 1;
6300 					sv = stcb->asoc.control_pdapi;
6301 					stcb->asoc.control_pdapi = ctl;
6302 					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
6303 					    stcb,
6304 					    SCTP_PARTIAL_DELIVERY_ABORTED,
6305 					    (void *)&str_seq,
6306 					    SCTP_SO_NOT_LOCKED);
6307 					stcb->asoc.control_pdapi = sv;
6308 					break;
6309 				} else if ((ctl->sinfo_stream == stseq->stream) &&
6310 				    (compare_with_wrap(ctl->sinfo_ssn, stseq->sequence, MAX_SEQ))) {
6311 					/* We are past our victim SSN */
6312 					break;
6313 				}
6314 			}
6315 			strm = &asoc->strmin[stseq->stream];
6316 			if (compare_with_wrap(stseq->sequence,
6317 			    strm->last_sequence_delivered, MAX_SEQ)) {
6318 				/* Update the sequence number */
6319 				strm->last_sequence_delivered =
6320 				    stseq->sequence;
6321 			}
6322 			/* now kick the stream the new way */
6323 			/* sa_ignore NO_NULL_CHK */
6324 			sctp_kick_prsctp_reorder_queue(stcb, strm);
6325 		}
6326 		SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
6327 	}
6328 	if (TAILQ_FIRST(&asoc->reasmqueue)) {
6329 		/* now lets kick out and check for more fragmented delivery */
6330 		/* sa_ignore NO_NULL_CHK */
6331 		sctp_deliver_reasm_check(stcb, &stcb->asoc);
6332 	}
6333 }
6334 
6335 /* EY fully identical to sctp_express_handle_sack, duplicated for only naming convention */
6336 void
6337 sctp_express_handle_nr_sack(struct sctp_tcb *stcb, uint32_t cumack,
6338     uint32_t rwnd, int nonce_sum_flag, int *abort_now)
6339 {
6340 	struct sctp_nets *net;
6341 	struct sctp_association *asoc;
6342 	struct sctp_tmit_chunk *tp1, *tp2;
6343 	uint32_t old_rwnd;
6344 	int win_probe_recovery = 0;
6345 	int win_probe_recovered = 0;
6346 	int j, done_once = 0;
6347 
6348 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
6349 		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
6350 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
6351 	}
6352 	SCTP_TCB_LOCK_ASSERT(stcb);
6353 #ifdef SCTP_ASOCLOG_OF_TSNS
6354 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
6355 	stcb->asoc.cumack_log_at++;
6356 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
6357 		stcb->asoc.cumack_log_at = 0;
6358 	}
6359 #endif
6360 	asoc = &stcb->asoc;
6361 	old_rwnd = asoc->peers_rwnd;
6362 	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
6363 		/* old ack */
6364 		return;
6365 	} else if (asoc->last_acked_seq == cumack) {
6366 		/* Window update sack */
6367 		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
6368 		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
6369 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6370 			/* SWS sender side engages */
6371 			asoc->peers_rwnd = 0;
6372 		}
6373 		if (asoc->peers_rwnd > old_rwnd) {
6374 			goto again;
6375 		}
6376 		return;
6377 	}
6378 	/* First setup for CC stuff */
6379 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6380 		net->prev_cwnd = net->cwnd;
6381 		net->net_ack = 0;
6382 		net->net_ack2 = 0;
6383 
6384 		/*
6385 		 * CMT: Reset CUC and Fast recovery algo variables before
6386 		 * SACK processing
6387 		 */
6388 		net->new_pseudo_cumack = 0;
6389 		net->will_exit_fast_recovery = 0;
6390 	}
6391 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
6392 		uint32_t send_s;
6393 
6394 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
6395 			tp1 = TAILQ_LAST(&asoc->sent_queue,
6396 			    sctpchunk_listhead);
6397 			send_s = tp1->rec.data.TSN_seq + 1;
6398 		} else {
6399 			send_s = asoc->sending_seq;
6400 		}
6401 		if ((cumack == send_s) ||
6402 		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
6403 #ifndef INVARIANTS
6404 			struct mbuf *oper;
6405 
6406 #endif
6407 #ifdef INVARIANTS
6408 			panic("Impossible sack 1");
6409 #else
6410 			*abort_now = 1;
6411 			/* XXX */
6412 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
6413 			    0, M_DONTWAIT, 1, MT_DATA);
6414 			if (oper) {
6415 				struct sctp_paramhdr *ph;
6416 				uint32_t *ippp;
6417 
6418 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6419 				    sizeof(uint32_t);
6420 				ph = mtod(oper, struct sctp_paramhdr *);
6421 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
6422 				ph->param_length = htons(SCTP_BUF_LEN(oper));
6423 				ippp = (uint32_t *) (ph + 1);
6424 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
6425 			}
6426 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
6427 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
6428 			return;
6429 #endif
6430 		}
6431 	}
6432 	asoc->this_sack_highest_gap = cumack;
6433 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
6434 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
6435 		    stcb->asoc.overall_error_count,
6436 		    0,
6437 		    SCTP_FROM_SCTP_INDATA,
6438 		    __LINE__);
6439 	}
6440 	stcb->asoc.overall_error_count = 0;
6441 	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
6442 		/* process the new consecutive TSN first */
6443 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
6444 		while (tp1) {
6445 			tp2 = TAILQ_NEXT(tp1, sctp_next);
6446 			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
6447 			    MAX_TSN) ||
6448 			    cumack == tp1->rec.data.TSN_seq) {
6449 				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
6450 					printf("Warning, an unsent is now acked?\n");
6451 				}
6452 				/*
6453 				 * ECN Nonce: Add the nonce to the sender's
6454 				 * nonce sum
6455 				 */
6456 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
6457 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
6458 					/*
6459 					 * If it is less than ACKED, it is
6460 					 * now no-longer in flight. Higher
6461 					 * values may occur during marking
6462 					 */
6463 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6464 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6465 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
6466 							    tp1->whoTo->flight_size,
6467 							    tp1->book_size,
6468 							    (uintptr_t) tp1->whoTo,
6469 							    tp1->rec.data.TSN_seq);
6470 						}
6471 						sctp_flight_size_decrease(tp1);
6472 						/* sa_ignore NO_NULL_CHK */
6473 						sctp_total_flight_decrease(stcb, tp1);
6474 					}
6475 					tp1->whoTo->net_ack += tp1->send_size;
6476 					if (tp1->snd_count < 2) {
6477 						/*
6478 						 * True non-retransmited
6479 						 * chunk
6480 						 */
6481 						tp1->whoTo->net_ack2 +=
6482 						    tp1->send_size;
6483 
6484 						/* update RTO too? */
6485 						if (tp1->do_rtt) {
6486 							tp1->whoTo->RTO =
6487 							/*
6488 							 * sa_ignore
6489 							 * NO_NULL_CHK
6490 							 */
6491 							    sctp_calculate_rto(stcb,
6492 							    asoc, tp1->whoTo,
6493 							    &tp1->sent_rcv_time,
6494 							    sctp_align_safe_nocopy);
6495 							tp1->do_rtt = 0;
6496 						}
6497 					}
6498 					/*
6499 					 * CMT: CUCv2 algorithm. From the
6500 					 * cumack'd TSNs, for each TSN being
6501 					 * acked for the first time, set the
6502 					 * following variables for the
6503 					 * corresp destination.
6504 					 * new_pseudo_cumack will trigger a
6505 					 * cwnd update.
6506 					 * find_(rtx_)pseudo_cumack will
6507 					 * trigger search for the next
6508 					 * expected (rtx-)pseudo-cumack.
6509 					 */
6510 					tp1->whoTo->new_pseudo_cumack = 1;
6511 					tp1->whoTo->find_pseudo_cumack = 1;
6512 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
6513 
6514 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
6515 						/* sa_ignore NO_NULL_CHK */
6516 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
6517 					}
6518 				}
6519 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
6520 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
6521 				}
6522 				if (tp1->rec.data.chunk_was_revoked) {
6523 					/* deflate the cwnd */
6524 					tp1->whoTo->cwnd -= tp1->book_size;
6525 					tp1->rec.data.chunk_was_revoked = 0;
6526 				}
6527 				tp1->sent = SCTP_DATAGRAM_ACKED;
6528 				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
6529 				if (tp1->data) {
6530 					/* sa_ignore NO_NULL_CHK */
6531 					sctp_free_bufspace(stcb, asoc, tp1, 1);
6532 					sctp_m_freem(tp1->data);
6533 				}
6534 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
6535 					sctp_log_sack(asoc->last_acked_seq,
6536 					    cumack,
6537 					    tp1->rec.data.TSN_seq,
6538 					    0,
6539 					    0,
6540 					    SCTP_LOG_FREE_SENT);
6541 				}
6542 				tp1->data = NULL;
6543 				asoc->sent_queue_cnt--;
6544 				sctp_free_a_chunk(stcb, tp1);
6545 				tp1 = tp2;
6546 			} else {
6547 				break;
6548 			}
6549 		}
6550 
6551 	}
6552 	/* sa_ignore NO_NULL_CHK */
6553 	if (stcb->sctp_socket) {
6554 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6555 		struct socket *so;
6556 
6557 #endif
6558 
6559 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
6560 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
6561 			/* sa_ignore NO_NULL_CHK */
6562 			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
6563 		}
6564 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6565 		so = SCTP_INP_SO(stcb->sctp_ep);
6566 		atomic_add_int(&stcb->asoc.refcnt, 1);
6567 		SCTP_TCB_UNLOCK(stcb);
6568 		SCTP_SOCKET_LOCK(so, 1);
6569 		SCTP_TCB_LOCK(stcb);
6570 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
6571 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6572 			/* assoc was freed while we were unlocked */
6573 			SCTP_SOCKET_UNLOCK(so, 1);
6574 			return;
6575 		}
6576 #endif
6577 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
6578 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
6579 		SCTP_SOCKET_UNLOCK(so, 1);
6580 #endif
6581 	} else {
6582 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
6583 			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
6584 		}
6585 	}
6586 
6587 	/* JRS - Use the congestion control given in the CC module */
6588 	if (asoc->last_acked_seq != cumack)
6589 		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
6590 
6591 	asoc->last_acked_seq = cumack;
6592 
6593 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
6594 		/* nothing left in-flight */
6595 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6596 			net->flight_size = 0;
6597 			net->partial_bytes_acked = 0;
6598 		}
6599 		asoc->total_flight = 0;
6600 		asoc->total_flight_count = 0;
6601 	}
6602 	/* Fix up the a-p-a-p for future PR-SCTP sends */
6603 	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
6604 		asoc->advanced_peer_ack_point = cumack;
6605 	}
6606 	/* ECN Nonce updates */
6607 	if (asoc->ecn_nonce_allowed) {
6608 		if (asoc->nonce_sum_check) {
6609 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
6610 				if (asoc->nonce_wait_for_ecne == 0) {
6611 					struct sctp_tmit_chunk *lchk;
6612 
6613 					lchk = TAILQ_FIRST(&asoc->send_queue);
6614 					asoc->nonce_wait_for_ecne = 1;
6615 					if (lchk) {
6616 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
6617 					} else {
6618 						asoc->nonce_wait_tsn = asoc->sending_seq;
6619 					}
6620 				} else {
6621 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
6622 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
6623 						/*
6624 						 * Misbehaving peer. We need
6625 						 * to react to this guy
6626 						 */
6627 						asoc->ecn_allowed = 0;
6628 						asoc->ecn_nonce_allowed = 0;
6629 					}
6630 				}
6631 			}
6632 		} else {
6633 			/* See if Resynchronization Possible */
6634 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
6635 				asoc->nonce_sum_check = 1;
6636 				/*
6637 				 * now we must calculate what the base is.
6638 				 * We do this based on two things, we know
6639 				 * the total's for all the segments
6640 				 * gap-acked in the SACK (none), We also
6641 				 * know the SACK's nonce sum, its in
6642 				 * nonce_sum_flag. So we can build a truth
6643 				 * table to back-calculate the new value of
6644 				 * asoc->nonce_sum_expect_base:
6645 				 *
6646 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
6647 				 * 1                    0 1 0 1 1 1 1 0
6648 				 */
6649 				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
6650 			}
6651 		}
6652 	}
6653 	/* RWND update */
6654 	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
6655 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
6656 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6657 		/* SWS sender side engages */
6658 		asoc->peers_rwnd = 0;
6659 	}
6660 	if (asoc->peers_rwnd > old_rwnd) {
6661 		win_probe_recovery = 1;
6662 	}
6663 	/* Now assure a timer where data is queued at */
6664 again:
6665 	j = 0;
6666 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6667 		int to_ticks;
6668 
6669 		if (win_probe_recovery && (net->window_probe)) {
6670 			win_probe_recovered = 1;
6671 			/*
6672 			 * Find first chunk that was used with window probe
6673 			 * and clear the sent
6674 			 */
6675 			/* sa_ignore FREED_MEMORY */
6676 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
6677 				if (tp1->window_probe) {
6678 					/* move back to data send queue */
6679 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
6680 					break;
6681 				}
6682 			}
6683 		}
6684 		if (net->RTO == 0) {
6685 			to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
6686 		} else {
6687 			to_ticks = MSEC_TO_TICKS(net->RTO);
6688 		}
6689 		if (net->flight_size) {
6690 
6691 			j++;
6692 			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
6693 			    sctp_timeout_handler, &net->rxt_timer);
6694 			if (net->window_probe) {
6695 				net->window_probe = 0;
6696 			}
6697 		} else {
6698 			if (net->window_probe) {
6699 				/*
6700 				 * In window probes we must assure a timer
6701 				 * is still running there
6702 				 */
6703 				net->window_probe = 0;
6704 				(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
6705 				    sctp_timeout_handler, &net->rxt_timer);
6706 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
6707 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
6708 				    stcb, net,
6709 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
6710 			}
6711 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
6712 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
6713 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
6714 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
6715 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
6716 				}
6717 			}
6718 		}
6719 	}
6720 	if ((j == 0) &&
6721 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
6722 	    (asoc->sent_queue_retran_cnt == 0) &&
6723 	    (win_probe_recovered == 0) &&
6724 	    (done_once == 0)) {
6725 		/*
6726 		 * huh, this should not happen unless all packets are
6727 		 * PR-SCTP and marked to skip of course.
6728 		 */
6729 		if (sctp_fs_audit(asoc)) {
6730 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6731 				net->flight_size = 0;
6732 			}
6733 			asoc->total_flight = 0;
6734 			asoc->total_flight_count = 0;
6735 			asoc->sent_queue_retran_cnt = 0;
6736 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
6737 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6738 					sctp_flight_size_increase(tp1);
6739 					sctp_total_flight_increase(stcb, tp1);
6740 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
6741 					asoc->sent_queue_retran_cnt++;
6742 				}
6743 			}
6744 		}
6745 		done_once = 1;
6746 		goto again;
6747 	}
6748 	/**********************************/
6749 	/* Now what about shutdown issues */
6750 	/**********************************/
6751 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
6752 		/* nothing left on sendqueue.. consider done */
6753 		/* clean up */
6754 		if ((asoc->stream_queue_cnt == 1) &&
6755 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
6756 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
6757 		    (asoc->locked_on_sending)
6758 		    ) {
6759 			struct sctp_stream_queue_pending *sp;
6760 
6761 			/*
6762 			 * I may be in a state where we got all across.. but
6763 			 * cannot write more due to a shutdown... we abort
6764 			 * since the user did not indicate EOR in this case.
6765 			 * The sp will be cleaned during free of the asoc.
6766 			 */
6767 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
6768 			    sctp_streamhead);
6769 			if ((sp) && (sp->length == 0)) {
6770 				/* Let cleanup code purge it */
6771 				if (sp->msg_is_complete) {
6772 					asoc->stream_queue_cnt--;
6773 				} else {
6774 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6775 					asoc->locked_on_sending = NULL;
6776 					asoc->stream_queue_cnt--;
6777 				}
6778 			}
6779 		}
6780 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
6781 		    (asoc->stream_queue_cnt == 0)) {
6782 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
6783 				/* Need to abort here */
6784 				struct mbuf *oper;
6785 
6786 		abort_out_now:
6787 				*abort_now = 1;
6788 				/* XXX */
6789 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
6790 				    0, M_DONTWAIT, 1, MT_DATA);
6791 				if (oper) {
6792 					struct sctp_paramhdr *ph;
6793 					uint32_t *ippp;
6794 
6795 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
6796 					    sizeof(uint32_t);
6797 					ph = mtod(oper, struct sctp_paramhdr *);
6798 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6799 					ph->param_length = htons(SCTP_BUF_LEN(oper));
6800 					ippp = (uint32_t *) (ph + 1);
6801 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
6802 				}
6803 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
6804 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
6805 			} else {
6806 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
6807 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
6808 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6809 				}
6810 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6811 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6812 				sctp_stop_timers_for_shutdown(stcb);
6813 				sctp_send_shutdown(stcb,
6814 				    stcb->asoc.primary_destination);
6815 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
6816 				    stcb->sctp_ep, stcb, asoc->primary_destination);
6817 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
6818 				    stcb->sctp_ep, stcb, asoc->primary_destination);
6819 			}
6820 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
6821 		    (asoc->stream_queue_cnt == 0)) {
6822 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
6823 				goto abort_out_now;
6824 			}
6825 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6826 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
6827 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6828 			sctp_send_shutdown_ack(stcb,
6829 			    stcb->asoc.primary_destination);
6830 
6831 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
6832 			    stcb->sctp_ep, stcb, asoc->primary_destination);
6833 		}
6834 	}
6835 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
6836 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
6837 		    rwnd,
6838 		    stcb->asoc.peers_rwnd,
6839 		    stcb->asoc.total_flight,
6840 		    stcb->asoc.total_output_queue_size);
6841 	}
6842 }
6843 
6844 /* EY! nr_sack version of sctp_handle_segments, nr-gapped TSNs get removed from RtxQ in this method*/
6845 static void
6846 sctp_handle_nr_sack_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
6847     struct sctp_nr_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
6848     uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
6849     uint32_t num_seg, uint32_t num_nr_seg, int *ecn_seg_sums)
6850 {
6851 	/************************************************/
6852 	/* process fragments and update sendqueue        */
6853 	/************************************************/
6854 	struct sctp_nr_sack *nr_sack;
6855 	struct sctp_gap_ack_block *frag, block;
6856 	struct sctp_nr_gap_ack_block *nr_frag, nr_block;
6857 	struct sctp_tmit_chunk *tp1;
6858 	uint32_t i, j;
6859 	int wake_him = 0;
6860 	uint32_t theTSN;
6861 	int num_frs = 0;
6862 
6863 	uint16_t frag_strt, frag_end, primary_flag_set;
6864 	uint16_t nr_frag_strt, nr_frag_end;
6865 
6866 	uint32_t last_frag_high;
6867 	uint32_t last_nr_frag_high;
6868 
6869 	/*
6870 	 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
6871 	 */
6872 	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
6873 		primary_flag_set = 1;
6874 	} else {
6875 		primary_flag_set = 0;
6876 	}
6877 	nr_sack = &ch->nr_sack;
6878 
6879 	/*
6880 	 * EY! - I will process nr_gaps similarly,by going to this position
6881 	 * again if All bit is set
6882 	 */
6883 	frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
6884 	    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
6885 	*offset += sizeof(block);
6886 	if (frag == NULL) {
6887 		return;
6888 	}
6889 	tp1 = NULL;
6890 	last_frag_high = 0;
6891 	for (i = 0; i < num_seg; i++) {
6892 		frag_strt = ntohs(frag->start);
6893 		frag_end = ntohs(frag->end);
6894 		/* some sanity checks on the fargment offsets */
6895 		if (frag_strt > frag_end) {
6896 			/* this one is malformed, skip */
6897 			frag++;
6898 			continue;
6899 		}
6900 		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
6901 		    MAX_TSN))
6902 			*biggest_tsn_acked = frag_end + last_tsn;
6903 
6904 		/* mark acked dgs and find out the highestTSN being acked */
6905 		if (tp1 == NULL) {
6906 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
6907 
6908 			/* save the locations of the last frags */
6909 			last_frag_high = frag_end + last_tsn;
6910 		} else {
6911 			/*
6912 			 * now lets see if we need to reset the queue due to
6913 			 * a out-of-order SACK fragment
6914 			 */
6915 			if (compare_with_wrap(frag_strt + last_tsn,
6916 			    last_frag_high, MAX_TSN)) {
6917 				/*
6918 				 * if the new frag starts after the last TSN
6919 				 * frag covered, we are ok and this one is
6920 				 * beyond the last one
6921 				 */
6922 				;
6923 			} else {
6924 				/*
6925 				 * ok, they have reset us, so we need to
6926 				 * reset the queue this will cause extra
6927 				 * hunting but hey, they chose the
6928 				 * performance hit when they failed to order
6929 				 * there gaps..
6930 				 */
6931 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
6932 			}
6933 			last_frag_high = frag_end + last_tsn;
6934 		}
6935 		for (j = frag_strt; j <= frag_end; j++) {
6936 			theTSN = j + last_tsn;
6937 			while (tp1) {
6938 				if (tp1->rec.data.doing_fast_retransmit)
6939 					num_frs++;
6940 
6941 				/*
6942 				 * CMT: CUCv2 algorithm. For each TSN being
6943 				 * processed from the sent queue, track the
6944 				 * next expected pseudo-cumack, or
6945 				 * rtx_pseudo_cumack, if required. Separate
6946 				 * cumack trackers for first transmissions,
6947 				 * and retransmissions.
6948 				 */
6949 				if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
6950 				    (tp1->snd_count == 1)) {
6951 					tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
6952 					tp1->whoTo->find_pseudo_cumack = 0;
6953 				}
6954 				if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
6955 				    (tp1->snd_count > 1)) {
6956 					tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
6957 					tp1->whoTo->find_rtx_pseudo_cumack = 0;
6958 				}
6959 				if (tp1->rec.data.TSN_seq == theTSN) {
6960 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
6961 						/*
6962 						 * must be held until
6963 						 * cum-ack passes
6964 						 */
6965 						/*
6966 						 * ECN Nonce: Add the nonce
6967 						 * value to the sender's
6968 						 * nonce sum
6969 						 */
6970 						if (tp1->sent < SCTP_DATAGRAM_RESEND) {
6971 							/*-
6972 						         * If it is less than RESEND, it is
6973 						         * now no-longer in flight.
6974 						         * Higher values may already be set
6975 						         * via previous Gap Ack Blocks...
6976 						         * i.e. ACKED or RESEND.
6977 						         */
6978 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
6979 							    *biggest_newly_acked_tsn, MAX_TSN)) {
6980 								*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
6981 							}
6982 							/*
6983 							 * CMT: SFR algo
6984 							 * (and HTNA) - set
6985 							 * saw_newack to 1
6986 							 * for dest being
6987 							 * newly acked.
6988 							 * update
6989 							 * this_sack_highest_
6990 							 * newack if
6991 							 * appropriate.
6992 							 */
6993 							if (tp1->rec.data.chunk_was_revoked == 0)
6994 								tp1->whoTo->saw_newack = 1;
6995 
6996 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
6997 							    tp1->whoTo->this_sack_highest_newack,
6998 							    MAX_TSN)) {
6999 								tp1->whoTo->this_sack_highest_newack =
7000 								    tp1->rec.data.TSN_seq;
7001 							}
7002 							/*
7003 							 * CMT DAC algo:
7004 							 * also update
7005 							 * this_sack_lowest_n
7006 							 * ewack
7007 							 */
7008 							if (*this_sack_lowest_newack == 0) {
7009 								if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7010 									sctp_log_sack(*this_sack_lowest_newack,
7011 									    last_tsn,
7012 									    tp1->rec.data.TSN_seq,
7013 									    0,
7014 									    0,
7015 									    SCTP_LOG_TSN_ACKED);
7016 								}
7017 								*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
7018 							}
7019 							/*
7020 							 * CMT: CUCv2
7021 							 * algorithm. If
7022 							 * (rtx-)pseudo-cumac
7023 							 * k for corresp
7024 							 * dest is being
7025 							 * acked, then we
7026 							 * have a new
7027 							 * (rtx-)pseudo-cumac
7028 							 * k. Set
7029 							 * new_(rtx_)pseudo_c
7030 							 * umack to TRUE so
7031 							 * that the cwnd for
7032 							 * this dest can be
7033 							 * updated. Also
7034 							 * trigger search
7035 							 * for the next
7036 							 * expected
7037 							 * (rtx-)pseudo-cumac
7038 							 * k. Separate
7039 							 * pseudo_cumack
7040 							 * trackers for
7041 							 * first
7042 							 * transmissions and
7043 							 * retransmissions.
7044 							 */
7045 							if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
7046 								if (tp1->rec.data.chunk_was_revoked == 0) {
7047 									tp1->whoTo->new_pseudo_cumack = 1;
7048 								}
7049 								tp1->whoTo->find_pseudo_cumack = 1;
7050 							}
7051 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7052 								sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
7053 							}
7054 							if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
7055 								if (tp1->rec.data.chunk_was_revoked == 0) {
7056 									tp1->whoTo->new_pseudo_cumack = 1;
7057 								}
7058 								tp1->whoTo->find_rtx_pseudo_cumack = 1;
7059 							}
7060 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7061 								sctp_log_sack(*biggest_newly_acked_tsn,
7062 								    last_tsn,
7063 								    tp1->rec.data.TSN_seq,
7064 								    frag_strt,
7065 								    frag_end,
7066 								    SCTP_LOG_TSN_ACKED);
7067 							}
7068 							if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7069 								sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
7070 								    tp1->whoTo->flight_size,
7071 								    tp1->book_size,
7072 								    (uintptr_t) tp1->whoTo,
7073 								    tp1->rec.data.TSN_seq);
7074 							}
7075 							sctp_flight_size_decrease(tp1);
7076 							sctp_total_flight_decrease(stcb, tp1);
7077 
7078 							tp1->whoTo->net_ack += tp1->send_size;
7079 							if (tp1->snd_count < 2) {
7080 								/*
7081 								 * True
7082 								 * non-retran
7083 								 * smited
7084 								 * chunk
7085 								 */
7086 								tp1->whoTo->net_ack2 += tp1->send_size;
7087 
7088 								/*
7089 								 * update
7090 								 * RTO too ?
7091 								 */
7092 								if (tp1->do_rtt) {
7093 									tp1->whoTo->RTO =
7094 									    sctp_calculate_rto(stcb,
7095 									    asoc,
7096 									    tp1->whoTo,
7097 									    &tp1->sent_rcv_time,
7098 									    sctp_align_safe_nocopy);
7099 									tp1->do_rtt = 0;
7100 								}
7101 							}
7102 						}
7103 						if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
7104 							(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
7105 							(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
7106 							if (compare_with_wrap(tp1->rec.data.TSN_seq,
7107 							    asoc->this_sack_highest_gap,
7108 							    MAX_TSN)) {
7109 								asoc->this_sack_highest_gap =
7110 								    tp1->rec.data.TSN_seq;
7111 							}
7112 							if (tp1->sent == SCTP_DATAGRAM_RESEND) {
7113 								sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7114 #ifdef SCTP_AUDITING_ENABLED
7115 								sctp_audit_log(0xB2,
7116 								    (asoc->sent_queue_retran_cnt & 0x000000ff));
7117 #endif
7118 							}
7119 						}
7120 						/*
7121 						 * All chunks NOT UNSENT
7122 						 * fall through here and are
7123 						 * marked
7124 						 */
7125 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
7126 							tp1->sent = SCTP_DATAGRAM_NR_MARKED;
7127 						if (tp1->rec.data.chunk_was_revoked) {
7128 							/* deflate the cwnd */
7129 							tp1->whoTo->cwnd -= tp1->book_size;
7130 							tp1->rec.data.chunk_was_revoked = 0;
7131 						}
7132 					}
7133 					break;
7134 				}	/* if (tp1->TSN_seq == theTSN) */
7135 				if (compare_with_wrap(tp1->rec.data.TSN_seq, theTSN,
7136 				    MAX_TSN))
7137 					break;
7138 
7139 				tp1 = TAILQ_NEXT(tp1, sctp_next);
7140 			}	/* end while (tp1) */
7141 		}		/* end for (j = fragStart */
7142 		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
7143 		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
7144 		*offset += sizeof(block);
7145 		if (frag == NULL) {
7146 			break;
7147 		}
7148 	}
7149 
7150 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FR_LOGGING_ENABLE) {
7151 		if (num_frs)
7152 			sctp_log_fr(*biggest_tsn_acked,
7153 			    *biggest_newly_acked_tsn,
7154 			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
7155 	}
7156 	nr_frag = (struct sctp_nr_gap_ack_block *)sctp_m_getptr(m, *offset,
7157 	    sizeof(struct sctp_nr_gap_ack_block), (uint8_t *) & nr_block);
7158 	*offset += sizeof(nr_block);
7159 
7160 
7161 
7162 	if (nr_frag == NULL) {
7163 		return;
7164 	}
7165 	tp1 = NULL;
7166 	last_nr_frag_high = 0;
7167 
7168 	for (i = 0; i < num_nr_seg; i++) {
7169 
7170 		nr_frag_strt = ntohs(nr_frag->start);
7171 		nr_frag_end = ntohs(nr_frag->end);
7172 
7173 		/* some sanity checks on the nr fargment offsets */
7174 		if (nr_frag_strt > nr_frag_end) {
7175 			/* this one is malformed, skip */
7176 			nr_frag++;
7177 			continue;
7178 		}
7179 		/* mark acked dgs and find out the highestTSN being acked */
7180 		if (tp1 == NULL) {
7181 			tp1 = TAILQ_FIRST(&asoc->sent_queue);
7182 
7183 			/* save the locations of the last frags */
7184 			last_nr_frag_high = nr_frag_end + last_tsn;
7185 		} else {
7186 			/*
7187 			 * now lets see if we need to reset the queue due to
7188 			 * a out-of-order SACK fragment
7189 			 */
7190 			if (compare_with_wrap(nr_frag_strt + last_tsn,
7191 			    last_nr_frag_high, MAX_TSN)) {
7192 				/*
7193 				 * if the new frag starts after the last TSN
7194 				 * frag covered, we are ok and this one is
7195 				 * beyond the last one
7196 				 */
7197 				;
7198 			} else {
7199 				/*
7200 				 * ok, they have reset us, so we need to
7201 				 * reset the queue this will cause extra
7202 				 * hunting but hey, they chose the
7203 				 * performance hit when they failed to order
7204 				 * there gaps..
7205 				 */
7206 				tp1 = TAILQ_FIRST(&asoc->sent_queue);
7207 			}
7208 			last_nr_frag_high = nr_frag_end + last_tsn;
7209 		}
7210 
7211 		for (j = nr_frag_strt + last_tsn; (compare_with_wrap((nr_frag_end + last_tsn), j, MAX_TSN)); j++) {
7212 			while (tp1) {
7213 				if (tp1->rec.data.TSN_seq == j) {
7214 					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
7215 						if (tp1->sent != SCTP_FORWARD_TSN_SKIP)
7216 							tp1->sent = SCTP_DATAGRAM_NR_MARKED;
7217 						/*
7218 						 * TAILQ_REMOVE(&asoc->sent_q
7219 						 * ueue, tp1, sctp_next);
7220 						 */
7221 						if (tp1->data) {
7222 							/*
7223 							 * sa_ignore
7224 							 * NO_NULL_CHK
7225 							 */
7226 							sctp_free_bufspace(stcb, asoc, tp1, 1);
7227 							sctp_m_freem(tp1->data);
7228 						}
7229 						tp1->data = NULL;
7230 						/* asoc->sent_queue_cnt--; */
7231 						/*
7232 						 * sctp_free_a_chunk(stcb,
7233 						 * tp1);
7234 						 */
7235 						wake_him++;
7236 					}
7237 					break;
7238 				}	/* if (tp1->TSN_seq == j) */
7239 				if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
7240 				    MAX_TSN))
7241 					break;
7242 				tp1 = TAILQ_NEXT(tp1, sctp_next);
7243 			}	/* end while (tp1) */
7244 
7245 		}		/* end for (j = nrFragStart */
7246 
7247 		nr_frag = (struct sctp_nr_gap_ack_block *)sctp_m_getptr(m, *offset,
7248 		    sizeof(struct sctp_nr_gap_ack_block), (uint8_t *) & nr_block);
7249 		*offset += sizeof(nr_block);
7250 		if (nr_frag == NULL) {
7251 			break;
7252 		}
7253 	}
7254 
7255 	/*
7256 	 * EY- wake up the socket if things have been removed from the sent
7257 	 * queue
7258 	 */
7259 	if ((wake_him) && (stcb->sctp_socket)) {
7260 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7261 		struct socket *so;
7262 
7263 #endif
7264 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
7265 		/*
7266 		 * if (SCTP_BASE_SYSCTL(sctp_logging_level) &
7267 		 * SCTP_WAKE_LOGGING_ENABLE) { sctp_wakeup_log(stcb,
7268 		 * cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);}
7269 		 */
7270 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7271 		so = SCTP_INP_SO(stcb->sctp_ep);
7272 		atomic_add_int(&stcb->asoc.refcnt, 1);
7273 		SCTP_TCB_UNLOCK(stcb);
7274 		SCTP_SOCKET_LOCK(so, 1);
7275 		SCTP_TCB_LOCK(stcb);
7276 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
7277 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7278 			/* assoc was freed while we were unlocked */
7279 			SCTP_SOCKET_UNLOCK(so, 1);
7280 			return;
7281 		}
7282 #endif
7283 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
7284 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7285 		SCTP_SOCKET_UNLOCK(so, 1);
7286 #endif
7287 	}			/* else { if
7288 				 * (SCTP_BASE_SYSCTL(sctp_logging_level) &
7289 				 * SCTP_WAKE_LOGGING_ENABLE) {
7290 				 * sctp_wakeup_log(stcb, cum_ack, wake_him,
7291 				 * SCTP_NOWAKE_FROM_SACK); } } */
7292 }
7293 
7294 /* EY- nr_sack */
7295 /* Identifies the non-renegable tsns that are revoked*/
7296 static void
7297 sctp_check_for_nr_revoked(struct sctp_tcb *stcb,
7298     struct sctp_association *asoc, uint32_t cumack,
7299     u_long biggest_tsn_acked)
7300 {
7301 	struct sctp_tmit_chunk *tp1;
7302 
7303 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7304 	while (tp1) {
7305 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
7306 		    MAX_TSN)) {
7307 			/*
7308 			 * ok this guy is either ACK or MARKED. If it is
7309 			 * ACKED it has been previously acked but not this
7310 			 * time i.e. revoked.  If it is MARKED it was ACK'ed
7311 			 * again.
7312 			 */
7313 			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
7314 			    MAX_TSN))
7315 				break;
7316 
7317 
7318 			if (tp1->sent == SCTP_DATAGRAM_NR_ACKED) {
7319 				/*
7320 				 * EY! a non-renegable TSN is revoked, need
7321 				 * to abort the association
7322 				 */
7323 				/*
7324 				 * EY TODO: put in the code to abort the
7325 				 * assoc.
7326 				 */
7327 				return;
7328 			} else if (tp1->sent == SCTP_DATAGRAM_NR_MARKED) {
7329 				/* it has been re-acked in this SACK */
7330 				tp1->sent = SCTP_DATAGRAM_NR_ACKED;
7331 			}
7332 		}
7333 		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
7334 			break;
7335 		tp1 = TAILQ_NEXT(tp1, sctp_next);
7336 	}
7337 }
7338 
7339 /* EY! nr_sack version of sctp_handle_sack, nr_gap_ack processing should be added to this method*/
7340 void
7341 sctp_handle_nr_sack(struct mbuf *m, int offset,
7342     struct sctp_nr_sack_chunk *ch, struct sctp_tcb *stcb,
7343     struct sctp_nets *net_from, int *abort_now, int nr_sack_len, uint32_t rwnd)
7344 {
7345 	struct sctp_association *asoc;
7346 
7347 	/* EY sack */
7348 	struct sctp_nr_sack *nr_sack;
7349 	struct sctp_tmit_chunk *tp1, *tp2;
7350 	uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
7351 	         this_sack_lowest_newack;
7352 	uint32_t sav_cum_ack;
7353 
7354 	/* EY num_seg */
7355 	uint16_t num_seg, num_nr_seg, num_dup;
7356 	uint16_t wake_him = 0;
7357 	unsigned int nr_sack_length;
7358 	uint32_t send_s = 0;
7359 	long j;
7360 	int accum_moved = 0;
7361 	int will_exit_fast_recovery = 0;
7362 	uint32_t a_rwnd, old_rwnd;
7363 	int win_probe_recovery = 0;
7364 	int win_probe_recovered = 0;
7365 	struct sctp_nets *net = NULL;
7366 	int nonce_sum_flag, ecn_seg_sums = 0;
7367 	int done_once;
7368 	uint8_t reneged_all = 0;
7369 	uint8_t cmt_dac_flag;
7370 
7371 	/*
7372 	 * we take any chance we can to service our queues since we cannot
7373 	 * get awoken when the socket is read from :<
7374 	 */
7375 	/*
7376 	 * Now perform the actual SACK handling: 1) Verify that it is not an
7377 	 * old sack, if so discard. 2) If there is nothing left in the send
7378 	 * queue (cum-ack is equal to last acked) then you have a duplicate
7379 	 * too, update any rwnd change and verify no timers are running.
7380 	 * then return. 3) Process any new consequtive data i.e. cum-ack
7381 	 * moved process these first and note that it moved. 4) Process any
7382 	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
7383 	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
7384 	 * sync up flightsizes and things, stop all timers and also check
7385 	 * for shutdown_pending state. If so then go ahead and send off the
7386 	 * shutdown. If in shutdown recv, send off the shutdown-ack and
7387 	 * start that timer, Ret. 9) Strike any non-acked things and do FR
7388 	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
7389 	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
7390 	 * if in shutdown_recv state.
7391 	 */
7392 	SCTP_TCB_LOCK_ASSERT(stcb);
7393 	nr_sack = &ch->nr_sack;
7394 	/* CMT DAC algo */
7395 	this_sack_lowest_newack = 0;
7396 	j = 0;
7397 	nr_sack_length = (unsigned int)nr_sack_len;
7398 	/* ECN Nonce */
7399 	SCTP_STAT_INCR(sctps_slowpath_sack);
7400 	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
7401 	cum_ack = last_tsn = ntohl(nr_sack->cum_tsn_ack);
7402 #ifdef SCTP_ASOCLOG_OF_TSNS
7403 	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
7404 	stcb->asoc.cumack_log_at++;
7405 	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
7406 		stcb->asoc.cumack_log_at = 0;
7407 	}
7408 #endif
7409 	num_seg = ntohs(nr_sack->num_gap_ack_blks);
7410 	num_nr_seg = ntohs(nr_sack->num_nr_gap_ack_blks);
7411 	a_rwnd = rwnd;
7412 
7413 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
7414 		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
7415 		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
7416 	}
7417 	/* CMT DAC algo */
7418 	cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
7419 	num_dup = ntohs(nr_sack->num_dup_tsns);
7420 
7421 	old_rwnd = stcb->asoc.peers_rwnd;
7422 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_THRESHOLD_LOGGING) {
7423 		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
7424 		    stcb->asoc.overall_error_count,
7425 		    0,
7426 		    SCTP_FROM_SCTP_INDATA,
7427 		    __LINE__);
7428 	}
7429 	stcb->asoc.overall_error_count = 0;
7430 	asoc = &stcb->asoc;
7431 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7432 		sctp_log_sack(asoc->last_acked_seq,
7433 		    cum_ack,
7434 		    0,
7435 		    num_seg,
7436 		    num_dup,
7437 		    SCTP_LOG_NEW_SACK);
7438 	}
7439 	if ((num_dup) && (SCTP_BASE_SYSCTL(sctp_logging_level) & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
7440 		int off_to_dup, iii;
7441 		uint32_t *dupdata, dblock;
7442 
7443 		off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) +
7444 		    (num_nr_seg * sizeof(struct sctp_nr_gap_ack_block)) + sizeof(struct sctp_nr_sack_chunk);
7445 		if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= nr_sack_length) {
7446 			dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
7447 			    sizeof(uint32_t), (uint8_t *) & dblock);
7448 			off_to_dup += sizeof(uint32_t);
7449 			if (dupdata) {
7450 				for (iii = 0; iii < num_dup; iii++) {
7451 					sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
7452 					dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
7453 					    sizeof(uint32_t), (uint8_t *) & dblock);
7454 					if (dupdata == NULL)
7455 						break;
7456 					off_to_dup += sizeof(uint32_t);
7457 				}
7458 			}
7459 		} else {
7460 			SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d nr_sack_len:%d num gaps:%d num nr_gaps:%d\n",
7461 			    off_to_dup, num_dup, nr_sack_length, num_seg, num_nr_seg);
7462 		}
7463 	}
7464 	if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
7465 		/* reality check */
7466 		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
7467 			tp1 = TAILQ_LAST(&asoc->sent_queue,
7468 			    sctpchunk_listhead);
7469 			send_s = tp1->rec.data.TSN_seq + 1;
7470 		} else {
7471 			send_s = asoc->sending_seq;
7472 		}
7473 		if (cum_ack == send_s ||
7474 		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
7475 #ifndef INVARIANTS
7476 			struct mbuf *oper;
7477 
7478 #endif
7479 #ifdef INVARIANTS
7480 	hopeless_peer:
7481 			panic("Impossible sack 1");
7482 #else
7483 
7484 
7485 			/*
7486 			 * no way, we have not even sent this TSN out yet.
7487 			 * Peer is hopelessly messed up with us.
7488 			 */
7489 	hopeless_peer:
7490 			*abort_now = 1;
7491 			/* XXX */
7492 			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
7493 			    0, M_DONTWAIT, 1, MT_DATA);
7494 			if (oper) {
7495 				struct sctp_paramhdr *ph;
7496 				uint32_t *ippp;
7497 
7498 				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
7499 				    sizeof(uint32_t);
7500 				ph = mtod(oper, struct sctp_paramhdr *);
7501 				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
7502 				ph->param_length = htons(SCTP_BUF_LEN(oper));
7503 				ippp = (uint32_t *) (ph + 1);
7504 				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
7505 			}
7506 			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
7507 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
7508 			return;
7509 #endif
7510 		}
7511 	}
7512 	/**********************/
7513 	/* 1) check the range */
7514 	/**********************/
7515 	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
7516 		/* acking something behind */
7517 		return;
7518 	}
7519 	sav_cum_ack = asoc->last_acked_seq;
7520 
7521 	/* update the Rwnd of the peer */
7522 	if (TAILQ_EMPTY(&asoc->sent_queue) &&
7523 	    TAILQ_EMPTY(&asoc->send_queue) &&
7524 	    (asoc->stream_queue_cnt == 0)
7525 	    ) {
7526 		/* nothing left on send/sent and strmq */
7527 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7528 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
7529 			    asoc->peers_rwnd, 0, 0, a_rwnd);
7530 		}
7531 		asoc->peers_rwnd = a_rwnd;
7532 		if (asoc->sent_queue_retran_cnt) {
7533 			asoc->sent_queue_retran_cnt = 0;
7534 		}
7535 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7536 			/* SWS sender side engages */
7537 			asoc->peers_rwnd = 0;
7538 		}
7539 		/* stop any timers */
7540 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7541 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7542 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
7543 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
7544 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7545 					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
7546 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
7547 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
7548 				}
7549 			}
7550 			net->partial_bytes_acked = 0;
7551 			net->flight_size = 0;
7552 		}
7553 		asoc->total_flight = 0;
7554 		asoc->total_flight_count = 0;
7555 		return;
7556 	}
7557 	/*
7558 	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
7559 	 * things. The total byte count acked is tracked in netAckSz AND
7560 	 * netAck2 is used to track the total bytes acked that are un-
7561 	 * amibguious and were never retransmitted. We track these on a per
7562 	 * destination address basis.
7563 	 */
7564 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7565 		net->prev_cwnd = net->cwnd;
7566 		net->net_ack = 0;
7567 		net->net_ack2 = 0;
7568 
7569 		/*
7570 		 * CMT: Reset CUC and Fast recovery algo variables before
7571 		 * SACK processing
7572 		 */
7573 		net->new_pseudo_cumack = 0;
7574 		net->will_exit_fast_recovery = 0;
7575 	}
7576 	/* process the new consecutive TSN first */
7577 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7578 	while (tp1) {
7579 		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
7580 		    MAX_TSN) ||
7581 		    last_tsn == tp1->rec.data.TSN_seq) {
7582 			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
7583 				/*
7584 				 * ECN Nonce: Add the nonce to the sender's
7585 				 * nonce sum
7586 				 */
7587 				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
7588 				accum_moved = 1;
7589 				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
7590 					/*
7591 					 * If it is less than ACKED, it is
7592 					 * now no-longer in flight. Higher
7593 					 * values may occur during marking
7594 					 */
7595 					if ((tp1->whoTo->dest_state &
7596 					    SCTP_ADDR_UNCONFIRMED) &&
7597 					    (tp1->snd_count < 2)) {
7598 						/*
7599 						 * If there was no retran
7600 						 * and the address is
7601 						 * un-confirmed and we sent
7602 						 * there and are now
7603 						 * sacked.. its confirmed,
7604 						 * mark it so.
7605 						 */
7606 						tp1->whoTo->dest_state &=
7607 						    ~SCTP_ADDR_UNCONFIRMED;
7608 					}
7609 					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
7610 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7611 							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
7612 							    tp1->whoTo->flight_size,
7613 							    tp1->book_size,
7614 							    (uintptr_t) tp1->whoTo,
7615 							    tp1->rec.data.TSN_seq);
7616 						}
7617 						sctp_flight_size_decrease(tp1);
7618 						sctp_total_flight_decrease(stcb, tp1);
7619 					}
7620 					tp1->whoTo->net_ack += tp1->send_size;
7621 
7622 					/* CMT SFR and DAC algos */
7623 					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
7624 					tp1->whoTo->saw_newack = 1;
7625 
7626 					if (tp1->snd_count < 2) {
7627 						/*
7628 						 * True non-retransmited
7629 						 * chunk
7630 						 */
7631 						tp1->whoTo->net_ack2 +=
7632 						    tp1->send_size;
7633 
7634 						/* update RTO too? */
7635 						if (tp1->do_rtt) {
7636 							tp1->whoTo->RTO =
7637 							    sctp_calculate_rto(stcb,
7638 							    asoc, tp1->whoTo,
7639 							    &tp1->sent_rcv_time,
7640 							    sctp_align_safe_nocopy);
7641 							tp1->do_rtt = 0;
7642 						}
7643 					}
7644 					/*
7645 					 * CMT: CUCv2 algorithm. From the
7646 					 * cumack'd TSNs, for each TSN being
7647 					 * acked for the first time, set the
7648 					 * following variables for the
7649 					 * corresp destination.
7650 					 * new_pseudo_cumack will trigger a
7651 					 * cwnd update.
7652 					 * find_(rtx_)pseudo_cumack will
7653 					 * trigger search for the next
7654 					 * expected (rtx-)pseudo-cumack.
7655 					 */
7656 					tp1->whoTo->new_pseudo_cumack = 1;
7657 					tp1->whoTo->find_pseudo_cumack = 1;
7658 					tp1->whoTo->find_rtx_pseudo_cumack = 1;
7659 
7660 
7661 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7662 						sctp_log_sack(asoc->last_acked_seq,
7663 						    cum_ack,
7664 						    tp1->rec.data.TSN_seq,
7665 						    0,
7666 						    0,
7667 						    SCTP_LOG_TSN_ACKED);
7668 					}
7669 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7670 						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
7671 					}
7672 				}
7673 				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
7674 					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
7675 #ifdef SCTP_AUDITING_ENABLED
7676 					sctp_audit_log(0xB3,
7677 					    (asoc->sent_queue_retran_cnt & 0x000000ff));
7678 #endif
7679 				}
7680 				if (tp1->rec.data.chunk_was_revoked) {
7681 					/* deflate the cwnd */
7682 					tp1->whoTo->cwnd -= tp1->book_size;
7683 					tp1->rec.data.chunk_was_revoked = 0;
7684 				}
7685 				tp1->sent = SCTP_DATAGRAM_ACKED;
7686 			}
7687 		} else {
7688 			break;
7689 		}
7690 		tp1 = TAILQ_NEXT(tp1, sctp_next);
7691 	}
7692 	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
7693 	/* always set this up to cum-ack */
7694 	asoc->this_sack_highest_gap = last_tsn;
7695 
7696 	/* Move offset up to point to gaps/dups */
7697 	offset += sizeof(struct sctp_nr_sack_chunk);
7698 	if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_nr_sack_chunk)) > nr_sack_length) {
7699 
7700 		/* skip corrupt segments */
7701 		goto skip_segments;
7702 	}
7703 	if (num_seg > 0) {
7704 
7705 		/*
7706 		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
7707 		 * to be greater than the cumack. Also reset saw_newack to 0
7708 		 * for all dests.
7709 		 */
7710 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7711 			net->saw_newack = 0;
7712 			net->this_sack_highest_newack = last_tsn;
7713 		}
7714 
7715 		/*
7716 		 * thisSackHighestGap will increase while handling NEW
7717 		 * segments this_sack_highest_newack will increase while
7718 		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
7719 		 * used for CMT DAC algo. saw_newack will also change.
7720 		 */
7721 
7722 		sctp_handle_nr_sack_segments(m, &offset, stcb, asoc, ch, last_tsn,
7723 		    &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
7724 		    num_seg, num_nr_seg, &ecn_seg_sums);
7725 
7726 
7727 		if (SCTP_BASE_SYSCTL(sctp_strict_sacks)) {
7728 			/*
7729 			 * validate the biggest_tsn_acked in the gap acks if
7730 			 * strict adherence is wanted.
7731 			 */
7732 			if ((biggest_tsn_acked == send_s) ||
7733 			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
7734 				/*
7735 				 * peer is either confused or we are under
7736 				 * attack. We must abort.
7737 				 */
7738 				goto hopeless_peer;
7739 			}
7740 		}
7741 	}
7742 skip_segments:
7743 	/*******************************************/
7744 	/* cancel ALL T3-send timer if accum moved */
7745 	/*******************************************/
7746 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
7747 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7748 			if (net->new_pseudo_cumack)
7749 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7750 				    stcb, net,
7751 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
7752 
7753 		}
7754 	} else {
7755 		if (accum_moved) {
7756 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7757 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7758 				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
7759 			}
7760 		}
7761 	}
7762 	/********************************************/
7763 	/* drop the acked chunks from the sendqueue */
7764 	/********************************************/
7765 	asoc->last_acked_seq = cum_ack;
7766 
7767 	tp1 = TAILQ_FIRST(&asoc->sent_queue);
7768 	if (tp1 == NULL)
7769 		goto done_with_it;
7770 	do {
7771 		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
7772 		    MAX_TSN)) {
7773 			break;
7774 		}
7775 		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
7776 			/* no more sent on list */
7777 			printf("Warning, tp1->sent == %d and its now acked?\n",
7778 			    tp1->sent);
7779 		}
7780 		tp2 = TAILQ_NEXT(tp1, sctp_next);
7781 		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
7782 		if (tp1->pr_sctp_on) {
7783 			if (asoc->pr_sctp_cnt != 0)
7784 				asoc->pr_sctp_cnt--;
7785 		}
7786 		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
7787 		    (asoc->total_flight > 0)) {
7788 #ifdef INVARIANTS
7789 			panic("Warning flight size is postive and should be 0");
7790 #else
7791 			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
7792 			    asoc->total_flight);
7793 #endif
7794 			asoc->total_flight = 0;
7795 		}
7796 		if (tp1->data) {
7797 			/* sa_ignore NO_NULL_CHK */
7798 			sctp_free_bufspace(stcb, asoc, tp1, 1);
7799 			sctp_m_freem(tp1->data);
7800 			if (asoc->peer_supports_prsctp && PR_SCTP_BUF_ENABLED(tp1->flags)) {
7801 				asoc->sent_queue_cnt_removeable--;
7802 			}
7803 		}
7804 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_LOGGING_ENABLE) {
7805 			sctp_log_sack(asoc->last_acked_seq,
7806 			    cum_ack,
7807 			    tp1->rec.data.TSN_seq,
7808 			    0,
7809 			    0,
7810 			    SCTP_LOG_FREE_SENT);
7811 		}
7812 		tp1->data = NULL;
7813 		asoc->sent_queue_cnt--;
7814 		sctp_free_a_chunk(stcb, tp1);
7815 		wake_him++;
7816 		tp1 = tp2;
7817 	} while (tp1 != NULL);
7818 
7819 done_with_it:
7820 	/* sa_ignore NO_NULL_CHK */
7821 	if ((wake_him) && (stcb->sctp_socket)) {
7822 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7823 		struct socket *so;
7824 
7825 #endif
7826 		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
7827 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
7828 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
7829 		}
7830 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7831 		so = SCTP_INP_SO(stcb->sctp_ep);
7832 		atomic_add_int(&stcb->asoc.refcnt, 1);
7833 		SCTP_TCB_UNLOCK(stcb);
7834 		SCTP_SOCKET_LOCK(so, 1);
7835 		SCTP_TCB_LOCK(stcb);
7836 		atomic_subtract_int(&stcb->asoc.refcnt, 1);
7837 		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7838 			/* assoc was freed while we were unlocked */
7839 			SCTP_SOCKET_UNLOCK(so, 1);
7840 			return;
7841 		}
7842 #endif
7843 		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
7844 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
7845 		SCTP_SOCKET_UNLOCK(so, 1);
7846 #endif
7847 	} else {
7848 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_WAKE_LOGGING_ENABLE) {
7849 			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
7850 		}
7851 	}
7852 
7853 	if (asoc->fast_retran_loss_recovery && accum_moved) {
7854 		if (compare_with_wrap(asoc->last_acked_seq,
7855 		    asoc->fast_recovery_tsn, MAX_TSN) ||
7856 		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
7857 			/* Setup so we will exit RFC2582 fast recovery */
7858 			will_exit_fast_recovery = 1;
7859 		}
7860 	}
7861 	/*
7862 	 * Check for revoked fragments:
7863 	 *
7864 	 * if Previous sack - Had no frags then we can't have any revoked if
7865 	 * Previous sack - Had frag's then - If we now have frags aka
7866 	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
7867 	 * some of them. else - The peer revoked all ACKED fragments, since
7868 	 * we had some before and now we have NONE.
7869 	 */
7870 
7871 	if (num_seg)
7872 		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
7873 
7874 	else if (asoc->saw_sack_with_frags) {
7875 		int cnt_revoked = 0;
7876 
7877 		tp1 = TAILQ_FIRST(&asoc->sent_queue);
7878 		if (tp1 != NULL) {
7879 			/* Peer revoked all dg's marked or acked */
7880 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
7881 				/*
7882 				 * EY- maybe check only if it is nr_acked
7883 				 * nr_marked may not be possible
7884 				 */
7885 				if ((tp1->sent == SCTP_DATAGRAM_NR_ACKED) ||
7886 				    (tp1->sent == SCTP_DATAGRAM_NR_MARKED)) {
7887 					/*
7888 					 * EY! - TODO: Something previously
7889 					 * nr_gapped is reneged, abort the
7890 					 * association
7891 					 */
7892 					return;
7893 				}
7894 				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
7895 				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
7896 					tp1->sent = SCTP_DATAGRAM_SENT;
7897 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7898 						sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
7899 						    tp1->whoTo->flight_size,
7900 						    tp1->book_size,
7901 						    (uintptr_t) tp1->whoTo,
7902 						    tp1->rec.data.TSN_seq);
7903 					}
7904 					sctp_flight_size_increase(tp1);
7905 					sctp_total_flight_increase(stcb, tp1);
7906 					tp1->rec.data.chunk_was_revoked = 1;
7907 					/*
7908 					 * To ensure that this increase in
7909 					 * flightsize, which is artificial,
7910 					 * does not throttle the sender, we
7911 					 * also increase the cwnd
7912 					 * artificially.
7913 					 */
7914 					tp1->whoTo->cwnd += tp1->book_size;
7915 					cnt_revoked++;
7916 				}
7917 			}
7918 			if (cnt_revoked) {
7919 				reneged_all = 1;
7920 			}
7921 		}
7922 		asoc->saw_sack_with_frags = 0;
7923 	}
7924 	if (num_seg)
7925 		asoc->saw_sack_with_frags = 1;
7926 	else
7927 		asoc->saw_sack_with_frags = 0;
7928 
7929 	/* EY! - not sure about if there should be an IF */
7930 	if (num_nr_seg)
7931 		sctp_check_for_nr_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
7932 	else if (asoc->saw_sack_with_nr_frags) {
7933 		/*
7934 		 * EY!- TODO: all previously nr_gapped chunks have been
7935 		 * reneged abort the association
7936 		 */
7937 		asoc->saw_sack_with_nr_frags = 0;
7938 	}
7939 	if (num_nr_seg)
7940 		asoc->saw_sack_with_nr_frags = 1;
7941 	else
7942 		asoc->saw_sack_with_nr_frags = 0;
7943 	/* JRS - Use the congestion control given in the CC module */
7944 	asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
7945 
7946 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
7947 		/* nothing left in-flight */
7948 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7949 			/* stop all timers */
7950 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
7951 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
7952 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
7953 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
7954 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
7955 				}
7956 			}
7957 			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
7958 			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
7959 			net->flight_size = 0;
7960 			net->partial_bytes_acked = 0;
7961 		}
7962 		asoc->total_flight = 0;
7963 		asoc->total_flight_count = 0;
7964 	}
7965 	/**********************************/
7966 	/* Now what about shutdown issues */
7967 	/**********************************/
7968 	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
7969 		/* nothing left on sendqueue.. consider done */
7970 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7971 			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
7972 			    asoc->peers_rwnd, 0, 0, a_rwnd);
7973 		}
7974 		asoc->peers_rwnd = a_rwnd;
7975 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7976 			/* SWS sender side engages */
7977 			asoc->peers_rwnd = 0;
7978 		}
7979 		/* clean up */
7980 		if ((asoc->stream_queue_cnt == 1) &&
7981 		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7982 		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
7983 		    (asoc->locked_on_sending)
7984 		    ) {
7985 			struct sctp_stream_queue_pending *sp;
7986 
7987 			/*
7988 			 * I may be in a state where we got all across.. but
7989 			 * cannot write more due to a shutdown... we abort
7990 			 * since the user did not indicate EOR in this case.
7991 			 */
7992 			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
7993 			    sctp_streamhead);
7994 			if ((sp) && (sp->length == 0)) {
7995 				asoc->locked_on_sending = NULL;
7996 				if (sp->msg_is_complete) {
7997 					asoc->stream_queue_cnt--;
7998 				} else {
7999 					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
8000 					asoc->stream_queue_cnt--;
8001 				}
8002 			}
8003 		}
8004 		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
8005 		    (asoc->stream_queue_cnt == 0)) {
8006 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
8007 				/* Need to abort here */
8008 				struct mbuf *oper;
8009 
8010 		abort_out_now:
8011 				*abort_now = 1;
8012 				/* XXX */
8013 				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
8014 				    0, M_DONTWAIT, 1, MT_DATA);
8015 				if (oper) {
8016 					struct sctp_paramhdr *ph;
8017 					uint32_t *ippp;
8018 
8019 					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
8020 					    sizeof(uint32_t);
8021 					ph = mtod(oper, struct sctp_paramhdr *);
8022 					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
8023 					ph->param_length = htons(SCTP_BUF_LEN(oper));
8024 					ippp = (uint32_t *) (ph + 1);
8025 					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
8026 				}
8027 				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
8028 				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
8029 				return;
8030 			} else {
8031 				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
8032 				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
8033 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8034 				}
8035 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
8036 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8037 				sctp_stop_timers_for_shutdown(stcb);
8038 				sctp_send_shutdown(stcb,
8039 				    stcb->asoc.primary_destination);
8040 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
8041 				    stcb->sctp_ep, stcb, asoc->primary_destination);
8042 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
8043 				    stcb->sctp_ep, stcb, asoc->primary_destination);
8044 			}
8045 			return;
8046 		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
8047 		    (asoc->stream_queue_cnt == 0)) {
8048 			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
8049 				goto abort_out_now;
8050 			}
8051 			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8052 			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
8053 			SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8054 			sctp_send_shutdown_ack(stcb,
8055 			    stcb->asoc.primary_destination);
8056 
8057 			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
8058 			    stcb->sctp_ep, stcb, asoc->primary_destination);
8059 			return;
8060 		}
8061 	}
8062 	/*
8063 	 * Now here we are going to recycle net_ack for a different use...
8064 	 * HEADS UP.
8065 	 */
8066 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8067 		net->net_ack = 0;
8068 	}
8069 
8070 	/*
8071 	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
8072 	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
8073 	 * automatically ensure that.
8074 	 */
8075 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac) && (cmt_dac_flag == 0)) {
8076 		this_sack_lowest_newack = cum_ack;
8077 	}
8078 	if (num_seg > 0) {
8079 		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
8080 		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
8081 	}
8082 	/* JRS - Use the congestion control given in the CC module */
8083 	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
8084 
8085 	/******************************************************************
8086 	 *  Here we do the stuff with ECN Nonce checking.
8087 	 *  We basically check to see if the nonce sum flag was incorrect
8088 	 *  or if resynchronization needs to be done. Also if we catch a
8089 	 *  misbehaving receiver we give him the kick.
8090 	 ******************************************************************/
8091 
8092 	if (asoc->ecn_nonce_allowed) {
8093 		if (asoc->nonce_sum_check) {
8094 			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
8095 				if (asoc->nonce_wait_for_ecne == 0) {
8096 					struct sctp_tmit_chunk *lchk;
8097 
8098 					lchk = TAILQ_FIRST(&asoc->send_queue);
8099 					asoc->nonce_wait_for_ecne = 1;
8100 					if (lchk) {
8101 						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
8102 					} else {
8103 						asoc->nonce_wait_tsn = asoc->sending_seq;
8104 					}
8105 				} else {
8106 					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
8107 					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
8108 						/*
8109 						 * Misbehaving peer. We need
8110 						 * to react to this guy
8111 						 */
8112 						asoc->ecn_allowed = 0;
8113 						asoc->ecn_nonce_allowed = 0;
8114 					}
8115 				}
8116 			}
8117 		} else {
8118 			/* See if Resynchronization Possible */
8119 			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
8120 				asoc->nonce_sum_check = 1;
8121 				/*
8122 				 * now we must calculate what the base is.
8123 				 * We do this based on two things, we know
8124 				 * the total's for all the segments
8125 				 * gap-acked in the SACK, its stored in
8126 				 * ecn_seg_sums. We also know the SACK's
8127 				 * nonce sum, its in nonce_sum_flag. So we
8128 				 * can build a truth table to back-calculate
8129 				 * the new value of
8130 				 * asoc->nonce_sum_expect_base:
8131 				 *
8132 				 * SACK-flag-Value         Seg-Sums Base 0 0 0
8133 				 * 1                    0 1 0 1 1 1 1 0
8134 				 */
8135 				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
8136 			}
8137 		}
8138 	}
8139 	/* Now are we exiting loss recovery ? */
8140 	if (will_exit_fast_recovery) {
8141 		/* Ok, we must exit fast recovery */
8142 		asoc->fast_retran_loss_recovery = 0;
8143 	}
8144 	if ((asoc->sat_t3_loss_recovery) &&
8145 	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
8146 	    MAX_TSN) ||
8147 	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
8148 		/* end satellite t3 loss recovery */
8149 		asoc->sat_t3_loss_recovery = 0;
8150 	}
8151 	/*
8152 	 * CMT Fast recovery
8153 	 */
8154 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8155 		if (net->will_exit_fast_recovery) {
8156 			/* Ok, we must exit fast recovery */
8157 			net->fast_retran_loss_recovery = 0;
8158 		}
8159 	}
8160 
8161 	/* Adjust and set the new rwnd value */
8162 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
8163 		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
8164 		    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)), a_rwnd);
8165 	}
8166 	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
8167 	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * SCTP_BASE_SYSCTL(sctp_peer_chunk_oh))));
8168 	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8169 		/* SWS sender side engages */
8170 		asoc->peers_rwnd = 0;
8171 	}
8172 	if (asoc->peers_rwnd > old_rwnd) {
8173 		win_probe_recovery = 1;
8174 	}
8175 	/*
8176 	 * Now we must setup so we have a timer up for anyone with
8177 	 * outstanding data.
8178 	 */
8179 	done_once = 0;
8180 again:
8181 	j = 0;
8182 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8183 		if (win_probe_recovery && (net->window_probe)) {
8184 			win_probe_recovered = 1;
8185 			/*-
8186 			 * Find first chunk that was used with
8187 			 * window probe and clear the event. Put
8188 			 * it back into the send queue as if has
8189 			 * not been sent.
8190 			 */
8191 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
8192 				if (tp1->window_probe) {
8193 					sctp_window_probe_recovery(stcb, asoc, net, tp1);
8194 					break;
8195 				}
8196 			}
8197 		}
8198 		if (net->flight_size) {
8199 			j++;
8200 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8201 			    stcb->sctp_ep, stcb, net);
8202 			if (net->window_probe) {
8203 				net->window_probe = 0;
8204 			}
8205 		} else {
8206 			if (net->window_probe) {
8207 				net->window_probe = 0;
8208 				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8209 				    stcb->sctp_ep, stcb, net);
8210 			} else if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8211 				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
8212 				    stcb, net,
8213 				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
8214 			}
8215 			if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8216 				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8217 					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
8218 					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
8219 					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
8220 				}
8221 			}
8222 		}
8223 	}
8224 	if ((j == 0) &&
8225 	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
8226 	    (asoc->sent_queue_retran_cnt == 0) &&
8227 	    (win_probe_recovered == 0) &&
8228 	    (done_once == 0)) {
8229 		/*
8230 		 * huh, this should not happen unless all packets are
8231 		 * PR-SCTP and marked to skip of course.
8232 		 */
8233 		if (sctp_fs_audit(asoc)) {
8234 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8235 				net->flight_size = 0;
8236 			}
8237 			asoc->total_flight = 0;
8238 			asoc->total_flight_count = 0;
8239 			asoc->sent_queue_retran_cnt = 0;
8240 			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
8241 				if (tp1->sent < SCTP_DATAGRAM_RESEND) {
8242 					sctp_flight_size_increase(tp1);
8243 					sctp_total_flight_increase(stcb, tp1);
8244 				} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
8245 					asoc->sent_queue_retran_cnt++;
8246 				}
8247 			}
8248 		}
8249 		done_once = 1;
8250 		goto again;
8251 	}
8252 	/*********************************************/
8253 	/* Here we perform PR-SCTP procedures        */
8254 	/* (section 4.2)                             */
8255 	/*********************************************/
8256 	/* C1. update advancedPeerAckPoint */
8257 	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
8258 		asoc->advanced_peer_ack_point = cum_ack;
8259 	}
8260 	/* C2. try to further move advancedPeerAckPoint ahead */
8261 	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
8262 		struct sctp_tmit_chunk *lchk;
8263 		uint32_t old_adv_peer_ack_point;
8264 
8265 		old_adv_peer_ack_point = asoc->advanced_peer_ack_point;
8266 		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
8267 		/* C3. See if we need to send a Fwd-TSN */
8268 		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
8269 		    MAX_TSN)) {
8270 			/*
8271 			 * ISSUE with ECN, see FWD-TSN processing for notes
8272 			 * on issues that will occur when the ECN NONCE
8273 			 * stuff is put into SCTP for cross checking.
8274 			 */
8275 			if (compare_with_wrap(asoc->advanced_peer_ack_point, old_adv_peer_ack_point,
8276 			    MAX_TSN)) {
8277 				send_forward_tsn(stcb, asoc);
8278 				/*
8279 				 * ECN Nonce: Disable Nonce Sum check when
8280 				 * FWD TSN is sent and store resync tsn
8281 				 */
8282 				asoc->nonce_sum_check = 0;
8283 				asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
8284 			} else if (lchk) {
8285 				/* try to FR fwd-tsn's that get lost too */
8286 				lchk->rec.data.fwd_tsn_cnt++;
8287 				if (lchk->rec.data.fwd_tsn_cnt > 3) {
8288 					send_forward_tsn(stcb, asoc);
8289 					lchk->rec.data.fwd_tsn_cnt = 0;
8290 				}
8291 			}
8292 		}
8293 		if (lchk) {
8294 			/* Assure a timer is up */
8295 			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
8296 			    stcb->sctp_ep, stcb, lchk->whoTo);
8297 		}
8298 	}
8299 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SACK_RWND_LOGGING_ENABLE) {
8300 		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
8301 		    a_rwnd,
8302 		    stcb->asoc.peers_rwnd,
8303 		    stcb->asoc.total_flight,
8304 		    stcb->asoc.total_output_queue_size);
8305 	}
8306 }
8307