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