sctp_indata.c revision 172396
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: head/sys/netinet/sctp_indata.c 172396 2007-10-01 03:22:29Z rrs $");
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
59void
60sctp_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 */
121uint32_t
122sctp_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 (asoc->my_rwnd &&
174	    (asoc->my_rwnd < 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 */
186struct sctp_queued_to_read *
187sctp_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;
222failed_build:
223	return (read_queue_e);
224}
225
226
227/*
228 * Build out our readq entry based on the incoming packet.
229 */
230static struct sctp_queued_to_read *
231sctp_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;
262failed_build:
263	return (read_queue_e);
264}
265
266
267struct mbuf *
268sctp_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
314char *
315sctp_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 */
363static void
364sctp_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.. */
381abandon:
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 */
538static void
539sctp_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 */
732static int
733sctp_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
763static void
764sctp_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
770doit_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 */
831static void
832sctp_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 */
1382static int
1383sctp_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
1438static int
1439sctp_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		return (0);
1481	}
1482	/* Calculate the number of TSN's between the base and this TSN */
1483	if (tsn >= asoc->mapping_array_base_tsn) {
1484		gap = tsn - asoc->mapping_array_base_tsn;
1485	} else {
1486		gap = (MAX_TSN - asoc->mapping_array_base_tsn) + tsn + 1;
1487	}
1488	if (gap >= (SCTP_MAPPING_ARRAY << 3)) {
1489		/* Can't hold the bit in the mapping at max array, toss it */
1490		return (0);
1491	}
1492	if (gap >= (uint32_t) (asoc->mapping_array_size << 3)) {
1493		SCTP_TCB_LOCK_ASSERT(stcb);
1494		if (sctp_expand_mapping_array(asoc, gap)) {
1495			/* Can't expand, drop it */
1496			return (0);
1497		}
1498	}
1499	if (compare_with_wrap(tsn, *high_tsn, MAX_TSN)) {
1500		*high_tsn = tsn;
1501	}
1502	/* See if we have received this one already */
1503	if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
1504		SCTP_STAT_INCR(sctps_recvdupdata);
1505		if (asoc->numduptsns < SCTP_MAX_DUP_TSNS) {
1506			/* Record a dup for the next outbound sack */
1507			asoc->dup_tsns[asoc->numduptsns] = tsn;
1508			asoc->numduptsns++;
1509		}
1510		asoc->send_sack = 1;
1511		return (0);
1512	}
1513	/*
1514	 * Check to see about the GONE flag, duplicates would cause a sack
1515	 * to be sent up above
1516	 */
1517	if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1518	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1519	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET))
1520	    ) {
1521		/*
1522		 * wait a minute, this guy is gone, there is no longer a
1523		 * receiver. Send peer an ABORT!
1524		 */
1525		struct mbuf *op_err;
1526
1527		op_err = sctp_generate_invmanparam(SCTP_CAUSE_OUT_OF_RESC);
1528		sctp_abort_an_association(stcb->sctp_ep, stcb, 0, op_err, SCTP_SO_NOT_LOCKED);
1529		*abort_flag = 1;
1530		return (0);
1531	}
1532	/*
1533	 * Now before going further we see if there is room. If NOT then we
1534	 * MAY let one through only IF this TSN is the one we are waiting
1535	 * for on a partial delivery API.
1536	 */
1537
1538	/* now do the tests */
1539	if (((asoc->cnt_on_all_streams +
1540	    asoc->cnt_on_reasm_queue +
1541	    asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) ||
1542	    (((int)asoc->my_rwnd) <= 0)) {
1543		/*
1544		 * When we have NO room in the rwnd we check to make sure
1545		 * the reader is doing its job...
1546		 */
1547		if (stcb->sctp_socket->so_rcv.sb_cc) {
1548			/* some to read, wake-up */
1549#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1550			struct socket *so;
1551
1552			so = SCTP_INP_SO(stcb->sctp_ep);
1553			atomic_add_int(&stcb->asoc.refcnt, 1);
1554			SCTP_TCB_UNLOCK(stcb);
1555			SCTP_SOCKET_LOCK(so, 1);
1556			SCTP_TCB_LOCK(stcb);
1557			atomic_subtract_int(&stcb->asoc.refcnt, 1);
1558			if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
1559				/* assoc was freed while we were unlocked */
1560				SCTP_SOCKET_UNLOCK(so, 1);
1561				return (0);
1562			}
1563#endif
1564			sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
1565#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
1566			SCTP_SOCKET_UNLOCK(so, 1);
1567#endif
1568		}
1569		/* now is it in the mapping array of what we have accepted? */
1570		if (compare_with_wrap(tsn,
1571		    asoc->highest_tsn_inside_map, MAX_TSN)) {
1572
1573			/* Nope not in the valid range dump it */
1574			SCTPDBG(SCTP_DEBUG_INDATA1, "My rwnd overrun1:tsn:%x rwnd %x sbspace:%x\n",
1575			    tsn, asoc->my_rwnd,
1576			    sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv));
1577			sctp_set_rwnd(stcb, asoc);
1578			if ((asoc->cnt_on_all_streams +
1579			    asoc->cnt_on_reasm_queue +
1580			    asoc->cnt_msg_on_sb) > sctp_max_chunks_on_queue) {
1581				SCTP_STAT_INCR(sctps_datadropchklmt);
1582			} else {
1583				SCTP_STAT_INCR(sctps_datadroprwnd);
1584			}
1585			indx = *break_flag;
1586			*break_flag = 1;
1587			return (0);
1588		}
1589	}
1590	strmno = ntohs(ch->dp.stream_id);
1591	if (strmno >= asoc->streamincnt) {
1592		struct sctp_paramhdr *phdr;
1593		struct mbuf *mb;
1594
1595		mb = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) * 2),
1596		    0, M_DONTWAIT, 1, MT_DATA);
1597		if (mb != NULL) {
1598			/* add some space up front so prepend will work well */
1599			SCTP_BUF_RESV_UF(mb, sizeof(struct sctp_chunkhdr));
1600			phdr = mtod(mb, struct sctp_paramhdr *);
1601			/*
1602			 * Error causes are just param's and this one has
1603			 * two back to back phdr, one with the error type
1604			 * and size, the other with the streamid and a rsvd
1605			 */
1606			SCTP_BUF_LEN(mb) = (sizeof(struct sctp_paramhdr) * 2);
1607			phdr->param_type = htons(SCTP_CAUSE_INVALID_STREAM);
1608			phdr->param_length =
1609			    htons(sizeof(struct sctp_paramhdr) * 2);
1610			phdr++;
1611			/* We insert the stream in the type field */
1612			phdr->param_type = ch->dp.stream_id;
1613			/* And set the length to 0 for the rsvd field */
1614			phdr->param_length = 0;
1615			sctp_queue_op_err(stcb, mb);
1616		}
1617		SCTP_STAT_INCR(sctps_badsid);
1618		SCTP_TCB_LOCK_ASSERT(stcb);
1619		SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
1620		if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
1621			/* we have a new high score */
1622			asoc->highest_tsn_inside_map = tsn;
1623			if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
1624				sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
1625			}
1626		}
1627		if (tsn == (asoc->cumulative_tsn + 1)) {
1628			/* Update cum-ack */
1629			asoc->cumulative_tsn = tsn;
1630		}
1631		return (0);
1632	}
1633	/*
1634	 * Before we continue lets validate that we are not being fooled by
1635	 * an evil attacker. We can only have 4k chunks based on our TSN
1636	 * spread allowed by the mapping array 512 * 8 bits, so there is no
1637	 * way our stream sequence numbers could have wrapped. We of course
1638	 * only validate the FIRST fragment so the bit must be set.
1639	 */
1640	strmseq = ntohs(ch->dp.stream_sequence);
1641#ifdef SCTP_ASOCLOG_OF_TSNS
1642	SCTP_TCB_LOCK_ASSERT(stcb);
1643	if (asoc->tsn_in_at >= SCTP_TSN_LOG_SIZE) {
1644		asoc->tsn_in_at = 0;
1645		asoc->tsn_in_wrapped = 1;
1646	}
1647	asoc->in_tsnlog[asoc->tsn_in_at].tsn = tsn;
1648	asoc->in_tsnlog[asoc->tsn_in_at].strm = strmno;
1649	asoc->in_tsnlog[asoc->tsn_in_at].seq = strmseq;
1650	asoc->in_tsnlog[asoc->tsn_in_at].sz = chk_length;
1651	asoc->in_tsnlog[asoc->tsn_in_at].flgs = chunk_flags;
1652	asoc->in_tsnlog[asoc->tsn_in_at].stcb = (void *)stcb;
1653	asoc->in_tsnlog[asoc->tsn_in_at].in_pos = asoc->tsn_in_at;
1654	asoc->in_tsnlog[asoc->tsn_in_at].in_out = 1;
1655	asoc->tsn_in_at++;
1656#endif
1657	if ((chunk_flags & SCTP_DATA_FIRST_FRAG) &&
1658	    (TAILQ_EMPTY(&asoc->resetHead)) &&
1659	    (chunk_flags & SCTP_DATA_UNORDERED) == 0 &&
1660	    (compare_with_wrap(asoc->strmin[strmno].last_sequence_delivered,
1661	    strmseq, MAX_SEQ) ||
1662	    asoc->strmin[strmno].last_sequence_delivered == strmseq)) {
1663		/* The incoming sseq is behind where we last delivered? */
1664		SCTPDBG(SCTP_DEBUG_INDATA1, "EVIL/Broken-Dup S-SEQ:%d delivered:%d from peer, Abort!\n",
1665		    strmseq, asoc->strmin[strmno].last_sequence_delivered);
1666		oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1667		    0, M_DONTWAIT, 1, MT_DATA);
1668		if (oper) {
1669			struct sctp_paramhdr *ph;
1670			uint32_t *ippp;
1671
1672			SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
1673			    (3 * sizeof(uint32_t));
1674			ph = mtod(oper, struct sctp_paramhdr *);
1675			ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1676			ph->param_length = htons(SCTP_BUF_LEN(oper));
1677			ippp = (uint32_t *) (ph + 1);
1678			*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_14);
1679			ippp++;
1680			*ippp = tsn;
1681			ippp++;
1682			*ippp = ((strmno << 16) | strmseq);
1683
1684		}
1685		stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_14;
1686		sctp_abort_an_association(stcb->sctp_ep, stcb,
1687		    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1688		*abort_flag = 1;
1689		return (0);
1690	}
1691	/************************************
1692	 * From here down we may find ch-> invalid
1693	 * so its a good idea NOT to use it.
1694	 *************************************/
1695
1696	the_len = (chk_length - sizeof(struct sctp_data_chunk));
1697	if (last_chunk == 0) {
1698		dmbuf = SCTP_M_COPYM(*m,
1699		    (offset + sizeof(struct sctp_data_chunk)),
1700		    the_len, M_DONTWAIT);
1701#ifdef SCTP_MBUF_LOGGING
1702		if (sctp_logging_level & SCTP_MBUF_LOGGING_ENABLE) {
1703			struct mbuf *mat;
1704
1705			mat = dmbuf;
1706			while (mat) {
1707				if (SCTP_BUF_IS_EXTENDED(mat)) {
1708					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
1709				}
1710				mat = SCTP_BUF_NEXT(mat);
1711			}
1712		}
1713#endif
1714	} else {
1715		/* We can steal the last chunk */
1716		int l_len;
1717
1718		dmbuf = *m;
1719		/* lop off the top part */
1720		m_adj(dmbuf, (offset + sizeof(struct sctp_data_chunk)));
1721		if (SCTP_BUF_NEXT(dmbuf) == NULL) {
1722			l_len = SCTP_BUF_LEN(dmbuf);
1723		} else {
1724			/*
1725			 * need to count up the size hopefully does not hit
1726			 * this to often :-0
1727			 */
1728			struct mbuf *lat;
1729
1730			l_len = 0;
1731			lat = dmbuf;
1732			while (lat) {
1733				l_len += SCTP_BUF_LEN(lat);
1734				lat = SCTP_BUF_NEXT(lat);
1735			}
1736		}
1737		if (l_len > the_len) {
1738			/* Trim the end round bytes off  too */
1739			m_adj(dmbuf, -(l_len - the_len));
1740		}
1741	}
1742	if (dmbuf == NULL) {
1743		SCTP_STAT_INCR(sctps_nomem);
1744		return (0);
1745	}
1746	if ((chunk_flags & SCTP_DATA_NOT_FRAG) == SCTP_DATA_NOT_FRAG &&
1747	    asoc->fragmented_delivery_inprogress == 0 &&
1748	    TAILQ_EMPTY(&asoc->resetHead) &&
1749	    ((ordered == 0) ||
1750	    ((asoc->strmin[strmno].last_sequence_delivered + 1) == strmseq &&
1751	    TAILQ_EMPTY(&asoc->strmin[strmno].inqueue)))) {
1752		/* Candidate for express delivery */
1753		/*
1754		 * Its not fragmented, No PD-API is up, Nothing in the
1755		 * delivery queue, Its un-ordered OR ordered and the next to
1756		 * deliver AND nothing else is stuck on the stream queue,
1757		 * And there is room for it in the socket buffer. Lets just
1758		 * stuff it up the buffer....
1759		 */
1760
1761		/* It would be nice to avoid this copy if we could :< */
1762		sctp_alloc_a_readq(stcb, control);
1763		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1764		    protocol_id,
1765		    stcb->asoc.context,
1766		    strmno, strmseq,
1767		    chunk_flags,
1768		    dmbuf);
1769		if (control == NULL) {
1770			goto failed_express_del;
1771		}
1772		sctp_add_to_readq(stcb->sctp_ep, stcb, control, &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
1773		if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1774			/* for ordered, bump what we delivered */
1775			asoc->strmin[strmno].last_sequence_delivered++;
1776		}
1777		SCTP_STAT_INCR(sctps_recvexpress);
1778		if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
1779			sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno,
1780			    SCTP_STR_LOG_FROM_EXPRS_DEL);
1781		}
1782		control = NULL;
1783		goto finish_express_del;
1784	}
1785failed_express_del:
1786	/* If we reach here this is a new chunk */
1787	chk = NULL;
1788	control = NULL;
1789	/* Express for fragmented delivery? */
1790	if ((asoc->fragmented_delivery_inprogress) &&
1791	    (stcb->asoc.control_pdapi) &&
1792	    (asoc->str_of_pdapi == strmno) &&
1793	    (asoc->ssn_of_pdapi == strmseq)
1794	    ) {
1795		control = stcb->asoc.control_pdapi;
1796		if ((chunk_flags & SCTP_DATA_FIRST_FRAG) == SCTP_DATA_FIRST_FRAG) {
1797			/* Can't be another first? */
1798			goto failed_pdapi_express_del;
1799		}
1800		if (tsn == (control->sinfo_tsn + 1)) {
1801			/* Yep, we can add it on */
1802			int end = 0;
1803			uint32_t cumack;
1804
1805			if (chunk_flags & SCTP_DATA_LAST_FRAG) {
1806				end = 1;
1807			}
1808			cumack = asoc->cumulative_tsn;
1809			if ((cumack + 1) == tsn)
1810				cumack = tsn;
1811
1812			if (sctp_append_to_readq(stcb->sctp_ep, stcb, control, dmbuf, end,
1813			    tsn,
1814			    &stcb->sctp_socket->so_rcv)) {
1815				SCTP_PRINTF("Append fails end:%d\n", end);
1816				goto failed_pdapi_express_del;
1817			}
1818			SCTP_STAT_INCR(sctps_recvexpressm);
1819			control->sinfo_tsn = tsn;
1820			asoc->tsn_last_delivered = tsn;
1821			asoc->fragment_flags = chunk_flags;
1822			asoc->tsn_of_pdapi_last_delivered = tsn;
1823			asoc->last_flags_delivered = chunk_flags;
1824			asoc->last_strm_seq_delivered = strmseq;
1825			asoc->last_strm_no_delivered = strmno;
1826			if (end) {
1827				/* clean up the flags and such */
1828				asoc->fragmented_delivery_inprogress = 0;
1829				if ((chunk_flags & SCTP_DATA_UNORDERED) == 0) {
1830					asoc->strmin[strmno].last_sequence_delivered++;
1831				}
1832				stcb->asoc.control_pdapi = NULL;
1833				if (TAILQ_EMPTY(&asoc->reasmqueue) == 0) {
1834					/*
1835					 * There could be another message
1836					 * ready
1837					 */
1838					need_reasm_check = 1;
1839				}
1840			}
1841			control = NULL;
1842			goto finish_express_del;
1843		}
1844	}
1845failed_pdapi_express_del:
1846	control = NULL;
1847	if ((chunk_flags & SCTP_DATA_NOT_FRAG) != SCTP_DATA_NOT_FRAG) {
1848		sctp_alloc_a_chunk(stcb, chk);
1849		if (chk == NULL) {
1850			/* No memory so we drop the chunk */
1851			SCTP_STAT_INCR(sctps_nomem);
1852			if (last_chunk == 0) {
1853				/* we copied it, free the copy */
1854				sctp_m_freem(dmbuf);
1855			}
1856			return (0);
1857		}
1858		chk->rec.data.TSN_seq = tsn;
1859		chk->no_fr_allowed = 0;
1860		chk->rec.data.stream_seq = strmseq;
1861		chk->rec.data.stream_number = strmno;
1862		chk->rec.data.payloadtype = protocol_id;
1863		chk->rec.data.context = stcb->asoc.context;
1864		chk->rec.data.doing_fast_retransmit = 0;
1865		chk->rec.data.rcv_flags = chunk_flags;
1866		chk->asoc = asoc;
1867		chk->send_size = the_len;
1868		chk->whoTo = net;
1869		atomic_add_int(&net->ref_count, 1);
1870		chk->data = dmbuf;
1871	} else {
1872		sctp_alloc_a_readq(stcb, control);
1873		sctp_build_readq_entry_mac(control, stcb, asoc->context, net, tsn,
1874		    protocol_id,
1875		    stcb->asoc.context,
1876		    strmno, strmseq,
1877		    chunk_flags,
1878		    dmbuf);
1879		if (control == NULL) {
1880			/* No memory so we drop the chunk */
1881			SCTP_STAT_INCR(sctps_nomem);
1882			if (last_chunk == 0) {
1883				/* we copied it, free the copy */
1884				sctp_m_freem(dmbuf);
1885			}
1886			return (0);
1887		}
1888		control->length = the_len;
1889	}
1890
1891	/* Mark it as received */
1892	/* Now queue it where it belongs */
1893	if (control != NULL) {
1894		/* First a sanity check */
1895		if (asoc->fragmented_delivery_inprogress) {
1896			/*
1897			 * Ok, we have a fragmented delivery in progress if
1898			 * this chunk is next to deliver OR belongs in our
1899			 * view to the reassembly, the peer is evil or
1900			 * broken.
1901			 */
1902			uint32_t estimate_tsn;
1903
1904			estimate_tsn = asoc->tsn_last_delivered + 1;
1905			if (TAILQ_EMPTY(&asoc->reasmqueue) &&
1906			    (estimate_tsn == control->sinfo_tsn)) {
1907				/* Evil/Broke peer */
1908				sctp_m_freem(control->data);
1909				control->data = NULL;
1910				if (control->whoFrom) {
1911					sctp_free_remote_addr(control->whoFrom);
1912					control->whoFrom = NULL;
1913				}
1914				sctp_free_a_readq(stcb, control);
1915				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1916				    0, M_DONTWAIT, 1, MT_DATA);
1917				if (oper) {
1918					struct sctp_paramhdr *ph;
1919					uint32_t *ippp;
1920
1921					SCTP_BUF_LEN(oper) =
1922					    sizeof(struct sctp_paramhdr) +
1923					    (3 * sizeof(uint32_t));
1924					ph = mtod(oper, struct sctp_paramhdr *);
1925					ph->param_type =
1926					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1927					ph->param_length = htons(SCTP_BUF_LEN(oper));
1928					ippp = (uint32_t *) (ph + 1);
1929					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_15);
1930					ippp++;
1931					*ippp = tsn;
1932					ippp++;
1933					*ippp = ((strmno << 16) | strmseq);
1934				}
1935				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_15;
1936				sctp_abort_an_association(stcb->sctp_ep, stcb,
1937				    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1938
1939				*abort_flag = 1;
1940				return (0);
1941			} else {
1942				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1943					sctp_m_freem(control->data);
1944					control->data = NULL;
1945					if (control->whoFrom) {
1946						sctp_free_remote_addr(control->whoFrom);
1947						control->whoFrom = NULL;
1948					}
1949					sctp_free_a_readq(stcb, control);
1950
1951					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1952					    0, M_DONTWAIT, 1, MT_DATA);
1953					if (oper) {
1954						struct sctp_paramhdr *ph;
1955						uint32_t *ippp;
1956
1957						SCTP_BUF_LEN(oper) =
1958						    sizeof(struct sctp_paramhdr) +
1959						    (3 * sizeof(uint32_t));
1960						ph = mtod(oper,
1961						    struct sctp_paramhdr *);
1962						ph->param_type =
1963						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
1964						ph->param_length =
1965						    htons(SCTP_BUF_LEN(oper));
1966						ippp = (uint32_t *) (ph + 1);
1967						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_16);
1968						ippp++;
1969						*ippp = tsn;
1970						ippp++;
1971						*ippp = ((strmno << 16) | strmseq);
1972					}
1973					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_16;
1974					sctp_abort_an_association(stcb->sctp_ep,
1975					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
1976
1977					*abort_flag = 1;
1978					return (0);
1979				}
1980			}
1981		} else {
1982			/* No PDAPI running */
1983			if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
1984				/*
1985				 * Reassembly queue is NOT empty validate
1986				 * that this tsn does not need to be in
1987				 * reasembly queue. If it does then our peer
1988				 * is broken or evil.
1989				 */
1990				if (sctp_does_tsn_belong_to_reasm(asoc, control->sinfo_tsn)) {
1991					sctp_m_freem(control->data);
1992					control->data = NULL;
1993					if (control->whoFrom) {
1994						sctp_free_remote_addr(control->whoFrom);
1995						control->whoFrom = NULL;
1996					}
1997					sctp_free_a_readq(stcb, control);
1998					oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
1999					    0, M_DONTWAIT, 1, MT_DATA);
2000					if (oper) {
2001						struct sctp_paramhdr *ph;
2002						uint32_t *ippp;
2003
2004						SCTP_BUF_LEN(oper) =
2005						    sizeof(struct sctp_paramhdr) +
2006						    (3 * sizeof(uint32_t));
2007						ph = mtod(oper,
2008						    struct sctp_paramhdr *);
2009						ph->param_type =
2010						    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2011						ph->param_length =
2012						    htons(SCTP_BUF_LEN(oper));
2013						ippp = (uint32_t *) (ph + 1);
2014						*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_17);
2015						ippp++;
2016						*ippp = tsn;
2017						ippp++;
2018						*ippp = ((strmno << 16) | strmseq);
2019					}
2020					stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_17;
2021					sctp_abort_an_association(stcb->sctp_ep,
2022					    stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
2023
2024					*abort_flag = 1;
2025					return (0);
2026				}
2027			}
2028		}
2029		/* ok, if we reach here we have passed the sanity checks */
2030		if (chunk_flags & SCTP_DATA_UNORDERED) {
2031			/* queue directly into socket buffer */
2032			sctp_add_to_readq(stcb->sctp_ep, stcb,
2033			    control,
2034			    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
2035		} else {
2036			/*
2037			 * Special check for when streams are resetting. We
2038			 * could be more smart about this and check the
2039			 * actual stream to see if it is not being reset..
2040			 * that way we would not create a HOLB when amongst
2041			 * streams being reset and those not being reset.
2042			 *
2043			 * We take complete messages that have a stream reset
2044			 * intervening (aka the TSN is after where our
2045			 * cum-ack needs to be) off and put them on a
2046			 * pending_reply_queue. The reassembly ones we do
2047			 * not have to worry about since they are all sorted
2048			 * and proceessed by TSN order. It is only the
2049			 * singletons I must worry about.
2050			 */
2051			if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2052			    ((compare_with_wrap(tsn, liste->tsn, MAX_TSN)))
2053			    ) {
2054				/*
2055				 * yep its past where we need to reset... go
2056				 * ahead and queue it.
2057				 */
2058				if (TAILQ_EMPTY(&asoc->pending_reply_queue)) {
2059					/* first one on */
2060					TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2061				} else {
2062					struct sctp_queued_to_read *ctlOn;
2063					unsigned char inserted = 0;
2064
2065					ctlOn = TAILQ_FIRST(&asoc->pending_reply_queue);
2066					while (ctlOn) {
2067						if (compare_with_wrap(control->sinfo_tsn,
2068						    ctlOn->sinfo_tsn, MAX_TSN)) {
2069							ctlOn = TAILQ_NEXT(ctlOn, next);
2070						} else {
2071							/* found it */
2072							TAILQ_INSERT_BEFORE(ctlOn, control, next);
2073							inserted = 1;
2074							break;
2075						}
2076					}
2077					if (inserted == 0) {
2078						/*
2079						 * must be put at end, use
2080						 * prevP (all setup from
2081						 * loop) to setup nextP.
2082						 */
2083						TAILQ_INSERT_TAIL(&asoc->pending_reply_queue, control, next);
2084					}
2085				}
2086			} else {
2087				sctp_queue_data_to_stream(stcb, asoc, control, abort_flag);
2088				if (*abort_flag) {
2089					return (0);
2090				}
2091			}
2092		}
2093	} else {
2094		/* Into the re-assembly queue */
2095		sctp_queue_data_for_reasm(stcb, asoc, chk, abort_flag);
2096		if (*abort_flag) {
2097			/*
2098			 * the assoc is now gone and chk was put onto the
2099			 * reasm queue, which has all been freed.
2100			 */
2101			*m = NULL;
2102			return (0);
2103		}
2104	}
2105finish_express_del:
2106	if (compare_with_wrap(tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
2107		/* we have a new high score */
2108		asoc->highest_tsn_inside_map = tsn;
2109		if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2110			sctp_log_map(0, 2, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
2111		}
2112	}
2113	if (tsn == (asoc->cumulative_tsn + 1)) {
2114		/* Update cum-ack */
2115		asoc->cumulative_tsn = tsn;
2116	}
2117	if (last_chunk) {
2118		*m = NULL;
2119	}
2120	if (ordered) {
2121		SCTP_STAT_INCR_COUNTER64(sctps_inorderchunks);
2122	} else {
2123		SCTP_STAT_INCR_COUNTER64(sctps_inunorderchunks);
2124	}
2125	SCTP_STAT_INCR(sctps_recvdata);
2126	/* Set it present please */
2127	if (sctp_logging_level & SCTP_STR_LOGGING_ENABLE) {
2128		sctp_log_strm_del_alt(stcb, tsn, strmseq, strmno, SCTP_STR_LOG_FROM_MARK_TSN);
2129	}
2130	if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2131		sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2132		    asoc->highest_tsn_inside_map, SCTP_MAP_PREPARE_SLIDE);
2133	}
2134	SCTP_TCB_LOCK_ASSERT(stcb);
2135	SCTP_SET_TSN_PRESENT(asoc->mapping_array, gap);
2136	/* check the special flag for stream resets */
2137	if (((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) &&
2138	    ((compare_with_wrap(asoc->cumulative_tsn, liste->tsn, MAX_TSN)) ||
2139	    (asoc->cumulative_tsn == liste->tsn))
2140	    ) {
2141		/*
2142		 * we have finished working through the backlogged TSN's now
2143		 * time to reset streams. 1: call reset function. 2: free
2144		 * pending_reply space 3: distribute any chunks in
2145		 * pending_reply_queue.
2146		 */
2147		struct sctp_queued_to_read *ctl;
2148
2149		sctp_reset_in_stream(stcb, liste->number_entries, liste->req.list_of_streams);
2150		TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
2151		SCTP_FREE(liste, SCTP_M_STRESET);
2152		/* sa_ignore FREED_MEMORY */
2153		liste = TAILQ_FIRST(&asoc->resetHead);
2154		ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2155		if (ctl && (liste == NULL)) {
2156			/* All can be removed */
2157			while (ctl) {
2158				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2159				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2160				if (*abort_flag) {
2161					return (0);
2162				}
2163				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2164			}
2165		} else if (ctl) {
2166			/* more than one in queue */
2167			while (!compare_with_wrap(ctl->sinfo_tsn, liste->tsn, MAX_TSN)) {
2168				/*
2169				 * if ctl->sinfo_tsn is <= liste->tsn we can
2170				 * process it which is the NOT of
2171				 * ctl->sinfo_tsn > liste->tsn
2172				 */
2173				TAILQ_REMOVE(&asoc->pending_reply_queue, ctl, next);
2174				sctp_queue_data_to_stream(stcb, asoc, ctl, abort_flag);
2175				if (*abort_flag) {
2176					return (0);
2177				}
2178				ctl = TAILQ_FIRST(&asoc->pending_reply_queue);
2179			}
2180		}
2181		/*
2182		 * Now service re-assembly to pick up anything that has been
2183		 * held on reassembly queue?
2184		 */
2185		sctp_deliver_reasm_check(stcb, asoc);
2186		need_reasm_check = 0;
2187	}
2188	if (need_reasm_check) {
2189		/* Another one waits ? */
2190		sctp_deliver_reasm_check(stcb, asoc);
2191	}
2192	return (1);
2193}
2194
2195int8_t sctp_map_lookup_tab[256] = {
2196	-1, 0, -1, 1, -1, 0, -1, 2,
2197	-1, 0, -1, 1, -1, 0, -1, 3,
2198	-1, 0, -1, 1, -1, 0, -1, 2,
2199	-1, 0, -1, 1, -1, 0, -1, 4,
2200	-1, 0, -1, 1, -1, 0, -1, 2,
2201	-1, 0, -1, 1, -1, 0, -1, 3,
2202	-1, 0, -1, 1, -1, 0, -1, 2,
2203	-1, 0, -1, 1, -1, 0, -1, 5,
2204	-1, 0, -1, 1, -1, 0, -1, 2,
2205	-1, 0, -1, 1, -1, 0, -1, 3,
2206	-1, 0, -1, 1, -1, 0, -1, 2,
2207	-1, 0, -1, 1, -1, 0, -1, 4,
2208	-1, 0, -1, 1, -1, 0, -1, 2,
2209	-1, 0, -1, 1, -1, 0, -1, 3,
2210	-1, 0, -1, 1, -1, 0, -1, 2,
2211	-1, 0, -1, 1, -1, 0, -1, 6,
2212	-1, 0, -1, 1, -1, 0, -1, 2,
2213	-1, 0, -1, 1, -1, 0, -1, 3,
2214	-1, 0, -1, 1, -1, 0, -1, 2,
2215	-1, 0, -1, 1, -1, 0, -1, 4,
2216	-1, 0, -1, 1, -1, 0, -1, 2,
2217	-1, 0, -1, 1, -1, 0, -1, 3,
2218	-1, 0, -1, 1, -1, 0, -1, 2,
2219	-1, 0, -1, 1, -1, 0, -1, 5,
2220	-1, 0, -1, 1, -1, 0, -1, 2,
2221	-1, 0, -1, 1, -1, 0, -1, 3,
2222	-1, 0, -1, 1, -1, 0, -1, 2,
2223	-1, 0, -1, 1, -1, 0, -1, 4,
2224	-1, 0, -1, 1, -1, 0, -1, 2,
2225	-1, 0, -1, 1, -1, 0, -1, 3,
2226	-1, 0, -1, 1, -1, 0, -1, 2,
2227	-1, 0, -1, 1, -1, 0, -1, 7,
2228};
2229
2230
2231void
2232sctp_sack_check(struct sctp_tcb *stcb, int ok_to_sack, int was_a_gap, int *abort_flag)
2233{
2234	/*
2235	 * Now we also need to check the mapping array in a couple of ways.
2236	 * 1) Did we move the cum-ack point?
2237	 */
2238	struct sctp_association *asoc;
2239	int i, at;
2240	int last_all_ones = 0;
2241	int slide_from, slide_end, lgap, distance;
2242	uint32_t old_cumack, old_base, old_highest;
2243	unsigned char aux_array[64];
2244
2245
2246	asoc = &stcb->asoc;
2247	at = 0;
2248
2249	old_cumack = asoc->cumulative_tsn;
2250	old_base = asoc->mapping_array_base_tsn;
2251	old_highest = asoc->highest_tsn_inside_map;
2252	if (asoc->mapping_array_size < 64)
2253		memcpy(aux_array, asoc->mapping_array,
2254		    asoc->mapping_array_size);
2255	else
2256		memcpy(aux_array, asoc->mapping_array, 64);
2257
2258	/*
2259	 * We could probably improve this a small bit by calculating the
2260	 * offset of the current cum-ack as the starting point.
2261	 */
2262	at = 0;
2263	for (i = 0; i < stcb->asoc.mapping_array_size; i++) {
2264
2265		if (asoc->mapping_array[i] == 0xff) {
2266			at += 8;
2267			last_all_ones = 1;
2268		} else {
2269			/* there is a 0 bit */
2270			at += sctp_map_lookup_tab[asoc->mapping_array[i]];
2271			last_all_ones = 0;
2272			break;
2273		}
2274	}
2275	asoc->cumulative_tsn = asoc->mapping_array_base_tsn + (at - last_all_ones);
2276	/* at is one off, since in the table a embedded -1 is present */
2277	at++;
2278
2279	if (compare_with_wrap(asoc->cumulative_tsn,
2280	    asoc->highest_tsn_inside_map,
2281	    MAX_TSN)) {
2282#ifdef INVARIANTS
2283		panic("huh, cumack 0x%x greater than high-tsn 0x%x in map",
2284		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2285#else
2286		SCTP_PRINTF("huh, cumack 0x%x greater than high-tsn 0x%x in map - should panic?\n",
2287		    asoc->cumulative_tsn, asoc->highest_tsn_inside_map);
2288		asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
2289#endif
2290	}
2291	if ((asoc->cumulative_tsn == asoc->highest_tsn_inside_map) && (at >= 8)) {
2292		/* The complete array was completed by a single FR */
2293		/* higest becomes the cum-ack */
2294		int clr;
2295
2296		asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
2297		/* clear the array */
2298		clr = (at >> 3) + 1;
2299		if (clr > asoc->mapping_array_size) {
2300			clr = asoc->mapping_array_size;
2301		}
2302		memset(asoc->mapping_array, 0, clr);
2303		/* base becomes one ahead of the cum-ack */
2304		asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
2305		if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2306			sctp_log_map(old_base, old_cumack, old_highest,
2307			    SCTP_MAP_PREPARE_SLIDE);
2308			sctp_log_map(asoc->mapping_array_base_tsn, asoc->cumulative_tsn,
2309			    asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_CLEARED);
2310		}
2311	} else if (at >= 8) {
2312		/* we can slide the mapping array down */
2313		/* Calculate the new byte postion we can move down */
2314		slide_from = at >> 3;
2315		/*
2316		 * now calculate the ceiling of the move using our highest
2317		 * TSN value
2318		 */
2319		if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
2320			lgap = asoc->highest_tsn_inside_map -
2321			    asoc->mapping_array_base_tsn;
2322		} else {
2323			lgap = (MAX_TSN - asoc->mapping_array_base_tsn) +
2324			    asoc->highest_tsn_inside_map + 1;
2325		}
2326		slide_end = lgap >> 3;
2327		if (slide_end < slide_from) {
2328#ifdef INVARIANTS
2329			panic("impossible slide");
2330#else
2331			printf("impossible slide?\n");
2332			return;
2333#endif
2334		}
2335		distance = (slide_end - slide_from) + 1;
2336		if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2337			sctp_log_map(old_base, old_cumack, old_highest,
2338			    SCTP_MAP_PREPARE_SLIDE);
2339			sctp_log_map((uint32_t) slide_from, (uint32_t) slide_end,
2340			    (uint32_t) lgap, SCTP_MAP_SLIDE_FROM);
2341		}
2342		if (distance + slide_from > asoc->mapping_array_size ||
2343		    distance < 0) {
2344			/*
2345			 * Here we do NOT slide forward the array so that
2346			 * hopefully when more data comes in to fill it up
2347			 * we will be able to slide it forward. Really I
2348			 * don't think this should happen :-0
2349			 */
2350
2351			if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2352				sctp_log_map((uint32_t) distance, (uint32_t) slide_from,
2353				    (uint32_t) asoc->mapping_array_size,
2354				    SCTP_MAP_SLIDE_NONE);
2355			}
2356		} else {
2357			int ii;
2358
2359			for (ii = 0; ii < distance; ii++) {
2360				asoc->mapping_array[ii] =
2361				    asoc->mapping_array[slide_from + ii];
2362			}
2363			for (ii = distance; ii <= slide_end; ii++) {
2364				asoc->mapping_array[ii] = 0;
2365			}
2366			asoc->mapping_array_base_tsn += (slide_from << 3);
2367			if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
2368				sctp_log_map(asoc->mapping_array_base_tsn,
2369				    asoc->cumulative_tsn, asoc->highest_tsn_inside_map,
2370				    SCTP_MAP_SLIDE_RESULT);
2371			}
2372		}
2373	}
2374	/*
2375	 * Now we need to see if we need to queue a sack or just start the
2376	 * timer (if allowed).
2377	 */
2378	if (ok_to_sack) {
2379		if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2380			/*
2381			 * Ok special case, in SHUTDOWN-SENT case. here we
2382			 * maker sure SACK timer is off and instead send a
2383			 * SHUTDOWN and a SACK
2384			 */
2385			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2386				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
2387				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_INDATA + SCTP_LOC_18);
2388			}
2389			sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
2390			sctp_send_sack(stcb);
2391		} else {
2392			int is_a_gap;
2393
2394			/* is there a gap now ? */
2395			is_a_gap = compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2396			    stcb->asoc.cumulative_tsn, MAX_TSN);
2397
2398			/*
2399			 * CMT DAC algorithm: increase number of packets
2400			 * received since last ack
2401			 */
2402			stcb->asoc.cmt_dac_pkts_rcvd++;
2403
2404			if ((stcb->asoc.send_sack == 1) ||	/* We need to send a
2405								 * SACK */
2406			    ((was_a_gap) && (is_a_gap == 0)) ||	/* was a gap, but no
2407								 * longer is one */
2408			    (stcb->asoc.numduptsns) ||	/* we have dup's */
2409			    (is_a_gap) ||	/* is still a gap */
2410			    (stcb->asoc.delayed_ack == 0) ||	/* Delayed sack disabled */
2411			    (stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq)	/* hit limit of pkts */
2412			    ) {
2413
2414				if ((sctp_cmt_on_off) && (sctp_cmt_use_dac) &&
2415				    (stcb->asoc.send_sack == 0) &&
2416				    (stcb->asoc.numduptsns == 0) &&
2417				    (stcb->asoc.delayed_ack) &&
2418				    (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer))) {
2419
2420					/*
2421					 * CMT DAC algorithm: With CMT,
2422					 * delay acks even in the face of
2423					 *
2424					 * reordering. Therefore, if acks that
2425					 * do not have to be sent because of
2426					 * the above reasons, will be
2427					 * delayed. That is, acks that would
2428					 * have been sent due to gap reports
2429					 * will be delayed with DAC. Start
2430					 * the delayed ack timer.
2431					 */
2432					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2433					    stcb->sctp_ep, stcb, NULL);
2434				} else {
2435					/*
2436					 * Ok we must build a SACK since the
2437					 * timer is pending, we got our
2438					 * first packet OR there are gaps or
2439					 * duplicates.
2440					 */
2441					(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2442					sctp_send_sack(stcb);
2443				}
2444			} else {
2445				if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2446					sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2447					    stcb->sctp_ep, stcb, NULL);
2448				}
2449			}
2450		}
2451	}
2452}
2453
2454void
2455sctp_service_queues(struct sctp_tcb *stcb, struct sctp_association *asoc)
2456{
2457	struct sctp_tmit_chunk *chk;
2458	uint32_t tsize;
2459	uint16_t nxt_todel;
2460
2461	if (asoc->fragmented_delivery_inprogress) {
2462		sctp_service_reassembly(stcb, asoc);
2463	}
2464	/* Can we proceed further, i.e. the PD-API is complete */
2465	if (asoc->fragmented_delivery_inprogress) {
2466		/* no */
2467		return;
2468	}
2469	/*
2470	 * Now is there some other chunk I can deliver from the reassembly
2471	 * queue.
2472	 */
2473doit_again:
2474	chk = TAILQ_FIRST(&asoc->reasmqueue);
2475	if (chk == NULL) {
2476		asoc->size_on_reasm_queue = 0;
2477		asoc->cnt_on_reasm_queue = 0;
2478		return;
2479	}
2480	nxt_todel = asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered + 1;
2481	if ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) &&
2482	    ((nxt_todel == chk->rec.data.stream_seq) ||
2483	    (chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED))) {
2484		/*
2485		 * Yep the first one is here. We setup to start reception,
2486		 * by backing down the TSN just in case we can't deliver.
2487		 */
2488
2489		/*
2490		 * Before we start though either all of the message should
2491		 * be here or 1/4 the socket buffer max or nothing on the
2492		 * delivery queue and something can be delivered.
2493		 */
2494		if ((sctp_is_all_msg_on_reasm(asoc, &tsize) ||
2495		    (tsize >= stcb->sctp_ep->partial_delivery_point))) {
2496			asoc->fragmented_delivery_inprogress = 1;
2497			asoc->tsn_last_delivered = chk->rec.data.TSN_seq - 1;
2498			asoc->str_of_pdapi = chk->rec.data.stream_number;
2499			asoc->ssn_of_pdapi = chk->rec.data.stream_seq;
2500			asoc->pdapi_ppid = chk->rec.data.payloadtype;
2501			asoc->fragment_flags = chk->rec.data.rcv_flags;
2502			sctp_service_reassembly(stcb, asoc);
2503			if (asoc->fragmented_delivery_inprogress == 0) {
2504				goto doit_again;
2505			}
2506		}
2507	}
2508}
2509
2510int
2511sctp_process_data(struct mbuf **mm, int iphlen, int *offset, int length,
2512    struct sctphdr *sh, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2513    struct sctp_nets *net, uint32_t * high_tsn)
2514{
2515	struct sctp_data_chunk *ch, chunk_buf;
2516	struct sctp_association *asoc;
2517	int num_chunks = 0;	/* number of control chunks processed */
2518	int stop_proc = 0;
2519	int chk_length, break_flag, last_chunk;
2520	int abort_flag = 0, was_a_gap = 0;
2521	struct mbuf *m;
2522
2523	/* set the rwnd */
2524	sctp_set_rwnd(stcb, &stcb->asoc);
2525
2526	m = *mm;
2527	SCTP_TCB_LOCK_ASSERT(stcb);
2528	asoc = &stcb->asoc;
2529	if (compare_with_wrap(stcb->asoc.highest_tsn_inside_map,
2530	    stcb->asoc.cumulative_tsn, MAX_TSN)) {
2531		/* there was a gap before this data was processed */
2532		was_a_gap = 1;
2533	}
2534	/*
2535	 * setup where we got the last DATA packet from for any SACK that
2536	 * may need to go out. Don't bump the net. This is done ONLY when a
2537	 * chunk is assigned.
2538	 */
2539	asoc->last_data_chunk_from = net;
2540
2541	/*-
2542	 * Now before we proceed we must figure out if this is a wasted
2543	 * cluster... i.e. it is a small packet sent in and yet the driver
2544	 * underneath allocated a full cluster for it. If so we must copy it
2545	 * to a smaller mbuf and free up the cluster mbuf. This will help
2546	 * with cluster starvation. Note for __Panda__ we don't do this
2547	 * since it has clusters all the way down to 64 bytes.
2548	 */
2549	if (SCTP_BUF_LEN(m) < (long)MLEN && SCTP_BUF_NEXT(m) == NULL) {
2550		/* we only handle mbufs that are singletons.. not chains */
2551		m = sctp_get_mbuf_for_msg(SCTP_BUF_LEN(m), 0, M_DONTWAIT, 1, MT_DATA);
2552		if (m) {
2553			/* ok lets see if we can copy the data up */
2554			caddr_t *from, *to;
2555
2556			/* get the pointers and copy */
2557			to = mtod(m, caddr_t *);
2558			from = mtod((*mm), caddr_t *);
2559			memcpy(to, from, SCTP_BUF_LEN((*mm)));
2560			/* copy the length and free up the old */
2561			SCTP_BUF_LEN(m) = SCTP_BUF_LEN((*mm));
2562			sctp_m_freem(*mm);
2563			/* sucess, back copy */
2564			*mm = m;
2565		} else {
2566			/* We are in trouble in the mbuf world .. yikes */
2567			m = *mm;
2568		}
2569	}
2570	/* get pointer to the first chunk header */
2571	ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2572	    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2573	if (ch == NULL) {
2574		return (1);
2575	}
2576	/*
2577	 * process all DATA chunks...
2578	 */
2579	*high_tsn = asoc->cumulative_tsn;
2580	break_flag = 0;
2581	asoc->data_pkts_seen++;
2582	while (stop_proc == 0) {
2583		/* validate chunk length */
2584		chk_length = ntohs(ch->ch.chunk_length);
2585		if (length - *offset < chk_length) {
2586			/* all done, mutulated chunk */
2587			stop_proc = 1;
2588			break;
2589		}
2590		if (ch->ch.chunk_type == SCTP_DATA) {
2591			if ((size_t)chk_length < sizeof(struct sctp_data_chunk) + 1) {
2592				/*
2593				 * Need to send an abort since we had a
2594				 * invalid data chunk.
2595				 */
2596				struct mbuf *op_err;
2597
2598				op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 2 * sizeof(uint32_t)),
2599				    0, M_DONTWAIT, 1, MT_DATA);
2600
2601				if (op_err) {
2602					struct sctp_paramhdr *ph;
2603					uint32_t *ippp;
2604
2605					SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr) +
2606					    (2 * sizeof(uint32_t));
2607					ph = mtod(op_err, struct sctp_paramhdr *);
2608					ph->param_type =
2609					    htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
2610					ph->param_length = htons(SCTP_BUF_LEN(op_err));
2611					ippp = (uint32_t *) (ph + 1);
2612					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_19);
2613					ippp++;
2614					*ippp = asoc->cumulative_tsn;
2615
2616				}
2617				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_19;
2618				sctp_abort_association(inp, stcb, m, iphlen, sh,
2619				    op_err, 0);
2620				return (2);
2621			}
2622#ifdef SCTP_AUDITING_ENABLED
2623			sctp_audit_log(0xB1, 0);
2624#endif
2625			if (SCTP_SIZE32(chk_length) == (length - *offset)) {
2626				last_chunk = 1;
2627			} else {
2628				last_chunk = 0;
2629			}
2630			if (sctp_process_a_data_chunk(stcb, asoc, mm, *offset, ch,
2631			    chk_length, net, high_tsn, &abort_flag, &break_flag,
2632			    last_chunk)) {
2633				num_chunks++;
2634			}
2635			if (abort_flag)
2636				return (2);
2637
2638			if (break_flag) {
2639				/*
2640				 * Set because of out of rwnd space and no
2641				 * drop rep space left.
2642				 */
2643				stop_proc = 1;
2644				break;
2645			}
2646		} else {
2647			/* not a data chunk in the data region */
2648			switch (ch->ch.chunk_type) {
2649			case SCTP_INITIATION:
2650			case SCTP_INITIATION_ACK:
2651			case SCTP_SELECTIVE_ACK:
2652			case SCTP_HEARTBEAT_REQUEST:
2653			case SCTP_HEARTBEAT_ACK:
2654			case SCTP_ABORT_ASSOCIATION:
2655			case SCTP_SHUTDOWN:
2656			case SCTP_SHUTDOWN_ACK:
2657			case SCTP_OPERATION_ERROR:
2658			case SCTP_COOKIE_ECHO:
2659			case SCTP_COOKIE_ACK:
2660			case SCTP_ECN_ECHO:
2661			case SCTP_ECN_CWR:
2662			case SCTP_SHUTDOWN_COMPLETE:
2663			case SCTP_AUTHENTICATION:
2664			case SCTP_ASCONF_ACK:
2665			case SCTP_PACKET_DROPPED:
2666			case SCTP_STREAM_RESET:
2667			case SCTP_FORWARD_CUM_TSN:
2668			case SCTP_ASCONF:
2669				/*
2670				 * Now, what do we do with KNOWN chunks that
2671				 * are NOT in the right place?
2672				 *
2673				 * For now, I do nothing but ignore them. We
2674				 * may later want to add sysctl stuff to
2675				 * switch out and do either an ABORT() or
2676				 * possibly process them.
2677				 */
2678				if (sctp_strict_data_order) {
2679					struct mbuf *op_err;
2680
2681					op_err = sctp_generate_invmanparam(SCTP_CAUSE_PROTOCOL_VIOLATION);
2682					sctp_abort_association(inp, stcb, m, iphlen, sh, op_err, 0);
2683					return (2);
2684				}
2685				break;
2686			default:
2687				/* unknown chunk type, use bit rules */
2688				if (ch->ch.chunk_type & 0x40) {
2689					/* Add a error report to the queue */
2690					struct mbuf *merr;
2691					struct sctp_paramhdr *phd;
2692
2693					merr = sctp_get_mbuf_for_msg(sizeof(*phd), 0, M_DONTWAIT, 1, MT_DATA);
2694					if (merr) {
2695						phd = mtod(merr, struct sctp_paramhdr *);
2696						/*
2697						 * We cheat and use param
2698						 * type since we did not
2699						 * bother to define a error
2700						 * cause struct. They are
2701						 * the same basic format
2702						 * with different names.
2703						 */
2704						phd->param_type =
2705						    htons(SCTP_CAUSE_UNRECOG_CHUNK);
2706						phd->param_length =
2707						    htons(chk_length + sizeof(*phd));
2708						SCTP_BUF_LEN(merr) = sizeof(*phd);
2709						SCTP_BUF_NEXT(merr) = SCTP_M_COPYM(m, *offset,
2710						    SCTP_SIZE32(chk_length),
2711						    M_DONTWAIT);
2712						if (SCTP_BUF_NEXT(merr)) {
2713							sctp_queue_op_err(stcb, merr);
2714						} else {
2715							sctp_m_freem(merr);
2716						}
2717					}
2718				}
2719				if ((ch->ch.chunk_type & 0x80) == 0) {
2720					/* discard the rest of this packet */
2721					stop_proc = 1;
2722				}	/* else skip this bad chunk and
2723					 * continue... */
2724				break;
2725			};	/* switch of chunk type */
2726		}
2727		*offset += SCTP_SIZE32(chk_length);
2728		if ((*offset >= length) || stop_proc) {
2729			/* no more data left in the mbuf chain */
2730			stop_proc = 1;
2731			continue;
2732		}
2733		ch = (struct sctp_data_chunk *)sctp_m_getptr(m, *offset,
2734		    sizeof(struct sctp_data_chunk), (uint8_t *) & chunk_buf);
2735		if (ch == NULL) {
2736			*offset = length;
2737			stop_proc = 1;
2738			break;
2739
2740		}
2741	}			/* while */
2742	if (break_flag) {
2743		/*
2744		 * we need to report rwnd overrun drops.
2745		 */
2746		sctp_send_packet_dropped(stcb, net, *mm, iphlen, 0);
2747	}
2748	if (num_chunks) {
2749		/*
2750		 * Did we get data, if so update the time for auto-close and
2751		 * give peer credit for being alive.
2752		 */
2753		SCTP_STAT_INCR(sctps_recvpktwithdata);
2754		if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) {
2755			sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
2756			    stcb->asoc.overall_error_count,
2757			    0,
2758			    SCTP_FROM_SCTP_INDATA,
2759			    __LINE__);
2760		}
2761		stcb->asoc.overall_error_count = 0;
2762		(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_last_rcvd);
2763	}
2764	/* now service all of the reassm queue if needed */
2765	if (!(TAILQ_EMPTY(&asoc->reasmqueue)))
2766		sctp_service_queues(stcb, asoc);
2767
2768	if (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) {
2769		/* Assure that we ack right away */
2770		stcb->asoc.send_sack = 1;
2771	}
2772	/* Start a sack timer or QUEUE a SACK for sending */
2773	if ((stcb->asoc.cumulative_tsn == stcb->asoc.highest_tsn_inside_map) &&
2774	    (stcb->asoc.mapping_array[0] != 0xff)) {
2775		if ((stcb->asoc.data_pkts_seen >= stcb->asoc.sack_freq) ||
2776		    (stcb->asoc.delayed_ack == 0) ||
2777		    (stcb->asoc.send_sack == 1)) {
2778			if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2779				(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
2780			}
2781			sctp_send_sack(stcb);
2782		} else {
2783			if (!SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
2784				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
2785				    stcb->sctp_ep, stcb, NULL);
2786			}
2787		}
2788	} else {
2789		sctp_sack_check(stcb, 1, was_a_gap, &abort_flag);
2790	}
2791	if (abort_flag)
2792		return (2);
2793
2794	return (0);
2795}
2796
2797static void
2798sctp_handle_segments(struct mbuf *m, int *offset, struct sctp_tcb *stcb, struct sctp_association *asoc,
2799    struct sctp_sack_chunk *ch, uint32_t last_tsn, uint32_t * biggest_tsn_acked,
2800    uint32_t * biggest_newly_acked_tsn, uint32_t * this_sack_lowest_newack,
2801    int num_seg, int *ecn_seg_sums)
2802{
2803	/************************************************/
2804	/* process fragments and update sendqueue        */
2805	/************************************************/
2806	struct sctp_sack *sack;
2807	struct sctp_gap_ack_block *frag, block;
2808	struct sctp_tmit_chunk *tp1;
2809	int i;
2810	unsigned int j;
2811	int num_frs = 0;
2812
2813	uint16_t frag_strt, frag_end, primary_flag_set;
2814	u_long last_frag_high;
2815
2816	/*
2817	 * @@@ JRI : TODO: This flag is not used anywhere .. remove?
2818	 */
2819	if (asoc->primary_destination->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
2820		primary_flag_set = 1;
2821	} else {
2822		primary_flag_set = 0;
2823	}
2824	sack = &ch->sack;
2825
2826	frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
2827	    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
2828	*offset += sizeof(block);
2829	if (frag == NULL) {
2830		return;
2831	}
2832	tp1 = NULL;
2833	last_frag_high = 0;
2834	for (i = 0; i < num_seg; i++) {
2835		frag_strt = ntohs(frag->start);
2836		frag_end = ntohs(frag->end);
2837		/* some sanity checks on the fargment offsets */
2838		if (frag_strt > frag_end) {
2839			/* this one is malformed, skip */
2840			frag++;
2841			continue;
2842		}
2843		if (compare_with_wrap((frag_end + last_tsn), *biggest_tsn_acked,
2844		    MAX_TSN))
2845			*biggest_tsn_acked = frag_end + last_tsn;
2846
2847		/* mark acked dgs and find out the highestTSN being acked */
2848		if (tp1 == NULL) {
2849			tp1 = TAILQ_FIRST(&asoc->sent_queue);
2850
2851			/* save the locations of the last frags */
2852			last_frag_high = frag_end + last_tsn;
2853		} else {
2854			/*
2855			 * now lets see if we need to reset the queue due to
2856			 * a out-of-order SACK fragment
2857			 */
2858			if (compare_with_wrap(frag_strt + last_tsn,
2859			    last_frag_high, MAX_TSN)) {
2860				/*
2861				 * if the new frag starts after the last TSN
2862				 * frag covered, we are ok and this one is
2863				 * beyond the last one
2864				 */
2865				;
2866			} else {
2867				/*
2868				 * ok, they have reset us, so we need to
2869				 * reset the queue this will cause extra
2870				 * hunting but hey, they chose the
2871				 * performance hit when they failed to order
2872				 * there gaps..
2873				 */
2874				tp1 = TAILQ_FIRST(&asoc->sent_queue);
2875			}
2876			last_frag_high = frag_end + last_tsn;
2877		}
2878		for (j = frag_strt + last_tsn; j <= frag_end + last_tsn; j++) {
2879			while (tp1) {
2880				if (tp1->rec.data.doing_fast_retransmit)
2881					num_frs++;
2882
2883				/*
2884				 * CMT: CUCv2 algorithm. For each TSN being
2885				 * processed from the sent queue, track the
2886				 * next expected pseudo-cumack, or
2887				 * rtx_pseudo_cumack, if required. Separate
2888				 * cumack trackers for first transmissions,
2889				 * and retransmissions.
2890				 */
2891				if ((tp1->whoTo->find_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2892				    (tp1->snd_count == 1)) {
2893					tp1->whoTo->pseudo_cumack = tp1->rec.data.TSN_seq;
2894					tp1->whoTo->find_pseudo_cumack = 0;
2895				}
2896				if ((tp1->whoTo->find_rtx_pseudo_cumack == 1) && (tp1->sent < SCTP_DATAGRAM_RESEND) &&
2897				    (tp1->snd_count > 1)) {
2898					tp1->whoTo->rtx_pseudo_cumack = tp1->rec.data.TSN_seq;
2899					tp1->whoTo->find_rtx_pseudo_cumack = 0;
2900				}
2901				if (tp1->rec.data.TSN_seq == j) {
2902					if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
2903						/*
2904						 * must be held until
2905						 * cum-ack passes
2906						 */
2907						/*
2908						 * ECN Nonce: Add the nonce
2909						 * value to the sender's
2910						 * nonce sum
2911						 */
2912						if (tp1->sent < SCTP_DATAGRAM_RESEND) {
2913							/*-
2914							 * If it is less than RESEND, it is
2915							 * now no-longer in flight.
2916							 * Higher values may already be set
2917							 * via previous Gap Ack Blocks...
2918							 * i.e. ACKED or RESEND.
2919							 */
2920							if (compare_with_wrap(tp1->rec.data.TSN_seq,
2921							    *biggest_newly_acked_tsn, MAX_TSN)) {
2922								*biggest_newly_acked_tsn = tp1->rec.data.TSN_seq;
2923							}
2924							/*
2925							 * CMT: SFR algo
2926							 * (and HTNA) - set
2927							 * saw_newack to 1
2928							 * for dest being
2929							 * newly acked.
2930							 * update
2931							 * this_sack_highest_
2932							 * newack if
2933							 * appropriate.
2934							 */
2935							if (tp1->rec.data.chunk_was_revoked == 0)
2936								tp1->whoTo->saw_newack = 1;
2937
2938							if (compare_with_wrap(tp1->rec.data.TSN_seq,
2939							    tp1->whoTo->this_sack_highest_newack,
2940							    MAX_TSN)) {
2941								tp1->whoTo->this_sack_highest_newack =
2942								    tp1->rec.data.TSN_seq;
2943							}
2944							/*
2945							 * CMT DAC algo:
2946							 * also update
2947							 * this_sack_lowest_n
2948							 * ewack
2949							 */
2950							if (*this_sack_lowest_newack == 0) {
2951								if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
2952									sctp_log_sack(*this_sack_lowest_newack,
2953									    last_tsn,
2954									    tp1->rec.data.TSN_seq,
2955									    0,
2956									    0,
2957									    SCTP_LOG_TSN_ACKED);
2958								}
2959								*this_sack_lowest_newack = tp1->rec.data.TSN_seq;
2960							}
2961							/*
2962							 * CMT: CUCv2
2963							 * algorithm. If
2964							 * (rtx-)pseudo-cumac
2965							 * k for corresp
2966							 * dest is being
2967							 * acked, then we
2968							 * have a new
2969							 * (rtx-)pseudo-cumac
2970							 * k. Set
2971							 * new_(rtx_)pseudo_c
2972							 * umack to TRUE so
2973							 * that the cwnd for
2974							 * this dest can be
2975							 * updated. Also
2976							 * trigger search
2977							 * for the next
2978							 * expected
2979							 * (rtx-)pseudo-cumac
2980							 * k. Separate
2981							 * pseudo_cumack
2982							 * trackers for
2983							 * first
2984							 * transmissions and
2985							 * retransmissions.
2986							 */
2987							if (tp1->rec.data.TSN_seq == tp1->whoTo->pseudo_cumack) {
2988								if (tp1->rec.data.chunk_was_revoked == 0) {
2989									tp1->whoTo->new_pseudo_cumack = 1;
2990								}
2991								tp1->whoTo->find_pseudo_cumack = 1;
2992							}
2993							if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
2994								sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
2995							}
2996							if (tp1->rec.data.TSN_seq == tp1->whoTo->rtx_pseudo_cumack) {
2997								if (tp1->rec.data.chunk_was_revoked == 0) {
2998									tp1->whoTo->new_pseudo_cumack = 1;
2999								}
3000								tp1->whoTo->find_rtx_pseudo_cumack = 1;
3001							}
3002							if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
3003								sctp_log_sack(*biggest_newly_acked_tsn,
3004								    last_tsn,
3005								    tp1->rec.data.TSN_seq,
3006								    frag_strt,
3007								    frag_end,
3008								    SCTP_LOG_TSN_ACKED);
3009							}
3010							if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3011								sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_GAP,
3012								    tp1->whoTo->flight_size,
3013								    tp1->book_size,
3014								    (uintptr_t) tp1->whoTo,
3015								    tp1->rec.data.TSN_seq);
3016							}
3017							sctp_flight_size_decrease(tp1);
3018							sctp_total_flight_decrease(stcb, tp1);
3019
3020							tp1->whoTo->net_ack += tp1->send_size;
3021							if (tp1->snd_count < 2) {
3022								/*
3023								 * True
3024								 * non-retran
3025								 * smited
3026								 * chunk */
3027								tp1->whoTo->net_ack2 += tp1->send_size;
3028
3029								/*
3030								 * update RTO
3031								 * too ? */
3032								if (tp1->do_rtt) {
3033									tp1->whoTo->RTO =
3034									    sctp_calculate_rto(stcb,
3035									    asoc,
3036									    tp1->whoTo,
3037									    &tp1->sent_rcv_time,
3038									    sctp_align_safe_nocopy);
3039									tp1->do_rtt = 0;
3040								}
3041							}
3042						}
3043						if (tp1->sent <= SCTP_DATAGRAM_RESEND) {
3044							(*ecn_seg_sums) += tp1->rec.data.ect_nonce;
3045							(*ecn_seg_sums) &= SCTP_SACK_NONCE_SUM;
3046							if (compare_with_wrap(tp1->rec.data.TSN_seq,
3047							    asoc->this_sack_highest_gap,
3048							    MAX_TSN)) {
3049								asoc->this_sack_highest_gap =
3050								    tp1->rec.data.TSN_seq;
3051							}
3052							if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3053								sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3054#ifdef SCTP_AUDITING_ENABLED
3055								sctp_audit_log(0xB2,
3056								    (asoc->sent_queue_retran_cnt & 0x000000ff));
3057#endif
3058							}
3059						}
3060						/*
3061						 * All chunks NOT UNSENT
3062						 * fall through here and are
3063						 * marked
3064						 */
3065						tp1->sent = SCTP_DATAGRAM_MARKED;
3066						if (tp1->rec.data.chunk_was_revoked) {
3067							/* deflate the cwnd */
3068							tp1->whoTo->cwnd -= tp1->book_size;
3069							tp1->rec.data.chunk_was_revoked = 0;
3070						}
3071					}
3072					break;
3073				}	/* if (tp1->TSN_seq == j) */
3074				if (compare_with_wrap(tp1->rec.data.TSN_seq, j,
3075				    MAX_TSN))
3076					break;
3077
3078				tp1 = TAILQ_NEXT(tp1, sctp_next);
3079			}	/* end while (tp1) */
3080		}		/* end for (j = fragStart */
3081		frag = (struct sctp_gap_ack_block *)sctp_m_getptr(m, *offset,
3082		    sizeof(struct sctp_gap_ack_block), (uint8_t *) & block);
3083		*offset += sizeof(block);
3084		if (frag == NULL) {
3085			break;
3086		}
3087	}
3088	if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3089		if (num_frs)
3090			sctp_log_fr(*biggest_tsn_acked,
3091			    *biggest_newly_acked_tsn,
3092			    last_tsn, SCTP_FR_LOG_BIGGEST_TSNS);
3093	}
3094}
3095
3096static void
3097sctp_check_for_revoked(struct sctp_tcb *stcb,
3098    struct sctp_association *asoc, uint32_t cumack,
3099    u_long biggest_tsn_acked)
3100{
3101	struct sctp_tmit_chunk *tp1;
3102	int tot_revoked = 0;
3103
3104	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3105	while (tp1) {
3106		if (compare_with_wrap(tp1->rec.data.TSN_seq, cumack,
3107		    MAX_TSN)) {
3108			/*
3109			 * ok this guy is either ACK or MARKED. If it is
3110			 * ACKED it has been previously acked but not this
3111			 * time i.e. revoked.  If it is MARKED it was ACK'ed
3112			 * again.
3113			 */
3114			if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3115			    MAX_TSN))
3116				break;
3117
3118
3119			if (tp1->sent == SCTP_DATAGRAM_ACKED) {
3120				/* it has been revoked */
3121				tp1->sent = SCTP_DATAGRAM_SENT;
3122				tp1->rec.data.chunk_was_revoked = 1;
3123				/*
3124				 * We must add this stuff back in to assure
3125				 * timers and such get started.
3126				 */
3127				if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3128					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
3129					    tp1->whoTo->flight_size,
3130					    tp1->book_size,
3131					    (uintptr_t) tp1->whoTo,
3132					    tp1->rec.data.TSN_seq);
3133				}
3134				sctp_flight_size_increase(tp1);
3135				sctp_total_flight_increase(stcb, tp1);
3136				/*
3137				 * We inflate the cwnd to compensate for our
3138				 * artificial inflation of the flight_size.
3139				 */
3140				tp1->whoTo->cwnd += tp1->book_size;
3141				tot_revoked++;
3142				if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
3143					sctp_log_sack(asoc->last_acked_seq,
3144					    cumack,
3145					    tp1->rec.data.TSN_seq,
3146					    0,
3147					    0,
3148					    SCTP_LOG_TSN_REVOKED);
3149				}
3150			} else if (tp1->sent == SCTP_DATAGRAM_MARKED) {
3151				/* it has been re-acked in this SACK */
3152				tp1->sent = SCTP_DATAGRAM_ACKED;
3153			}
3154		}
3155		if (tp1->sent == SCTP_DATAGRAM_UNSENT)
3156			break;
3157		tp1 = TAILQ_NEXT(tp1, sctp_next);
3158	}
3159	if (tot_revoked > 0) {
3160		/*
3161		 * Setup the ecn nonce re-sync point. We do this since once
3162		 * data is revoked we begin to retransmit things, which do
3163		 * NOT have the ECN bits set. This means we are now out of
3164		 * sync and must wait until we get back in sync with the
3165		 * peer to check ECN bits.
3166		 */
3167		tp1 = TAILQ_FIRST(&asoc->send_queue);
3168		if (tp1 == NULL) {
3169			asoc->nonce_resync_tsn = asoc->sending_seq;
3170		} else {
3171			asoc->nonce_resync_tsn = tp1->rec.data.TSN_seq;
3172		}
3173		asoc->nonce_wait_for_ecne = 0;
3174		asoc->nonce_sum_check = 0;
3175	}
3176}
3177
3178static void
3179sctp_strike_gap_ack_chunks(struct sctp_tcb *stcb, struct sctp_association *asoc,
3180    u_long biggest_tsn_acked, u_long biggest_tsn_newly_acked, u_long this_sack_lowest_newack, int accum_moved)
3181{
3182	struct sctp_tmit_chunk *tp1;
3183	int strike_flag = 0;
3184	struct timeval now;
3185	int tot_retrans = 0;
3186	uint32_t sending_seq;
3187	struct sctp_nets *net;
3188	int num_dests_sacked = 0;
3189
3190	/*
3191	 * select the sending_seq, this is either the next thing ready to be
3192	 * sent but not transmitted, OR, the next seq we assign.
3193	 */
3194	tp1 = TAILQ_FIRST(&stcb->asoc.send_queue);
3195	if (tp1 == NULL) {
3196		sending_seq = asoc->sending_seq;
3197	} else {
3198		sending_seq = tp1->rec.data.TSN_seq;
3199	}
3200
3201	/* CMT DAC algo: finding out if SACK is a mixed SACK */
3202	if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3203		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3204			if (net->saw_newack)
3205				num_dests_sacked++;
3206		}
3207	}
3208	if (stcb->asoc.peer_supports_prsctp) {
3209		(void)SCTP_GETTIME_TIMEVAL(&now);
3210	}
3211	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3212	while (tp1) {
3213		strike_flag = 0;
3214		if (tp1->no_fr_allowed) {
3215			/* this one had a timeout or something */
3216			tp1 = TAILQ_NEXT(tp1, sctp_next);
3217			continue;
3218		}
3219		if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3220			if (tp1->sent < SCTP_DATAGRAM_RESEND)
3221				sctp_log_fr(biggest_tsn_newly_acked,
3222				    tp1->rec.data.TSN_seq,
3223				    tp1->sent,
3224				    SCTP_FR_LOG_CHECK_STRIKE);
3225		}
3226		if (compare_with_wrap(tp1->rec.data.TSN_seq, biggest_tsn_acked,
3227		    MAX_TSN) ||
3228		    tp1->sent == SCTP_DATAGRAM_UNSENT) {
3229			/* done */
3230			break;
3231		}
3232		if (stcb->asoc.peer_supports_prsctp) {
3233			if ((PR_SCTP_TTL_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3234				/* Is it expired? */
3235				if (
3236				    (timevalcmp(&now, &tp1->rec.data.timetodrop, >))
3237				    ) {
3238					/* Yes so drop it */
3239					if (tp1->data != NULL) {
3240						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3241						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3242						    &asoc->sent_queue, SCTP_SO_NOT_LOCKED);
3243					}
3244					tp1 = TAILQ_NEXT(tp1, sctp_next);
3245					continue;
3246				}
3247			}
3248			if ((PR_SCTP_RTX_ENABLED(tp1->flags)) && tp1->sent < SCTP_DATAGRAM_ACKED) {
3249				/* Has it been retransmitted tv_sec times? */
3250				if (tp1->snd_count > tp1->rec.data.timetodrop.tv_sec) {
3251					/* Yes, so drop it */
3252					if (tp1->data != NULL) {
3253						(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3254						    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3255						    &asoc->sent_queue, SCTP_SO_NOT_LOCKED);
3256					}
3257					tp1 = TAILQ_NEXT(tp1, sctp_next);
3258					continue;
3259				}
3260			}
3261		}
3262		if (compare_with_wrap(tp1->rec.data.TSN_seq,
3263		    asoc->this_sack_highest_gap, MAX_TSN)) {
3264			/* we are beyond the tsn in the sack  */
3265			break;
3266		}
3267		if (tp1->sent >= SCTP_DATAGRAM_RESEND) {
3268			/* either a RESEND, ACKED, or MARKED */
3269			/* skip */
3270			tp1 = TAILQ_NEXT(tp1, sctp_next);
3271			continue;
3272		}
3273		/*
3274		 * CMT : SFR algo (covers part of DAC and HTNA as well)
3275		 */
3276		if (tp1->whoTo && tp1->whoTo->saw_newack == 0) {
3277			/*
3278			 * No new acks were receieved for data sent to this
3279			 * dest. Therefore, according to the SFR algo for
3280			 * CMT, no data sent to this dest can be marked for
3281			 * FR using this SACK.
3282			 */
3283			tp1 = TAILQ_NEXT(tp1, sctp_next);
3284			continue;
3285		} else if (tp1->whoTo && compare_with_wrap(tp1->rec.data.TSN_seq,
3286		    tp1->whoTo->this_sack_highest_newack, MAX_TSN)) {
3287			/*
3288			 * CMT: New acks were receieved for data sent to
3289			 * this dest. But no new acks were seen for data
3290			 * sent after tp1. Therefore, according to the SFR
3291			 * algo for CMT, tp1 cannot be marked for FR using
3292			 * this SACK. This step covers part of the DAC algo
3293			 * and the HTNA algo as well.
3294			 */
3295			tp1 = TAILQ_NEXT(tp1, sctp_next);
3296			continue;
3297		}
3298		/*
3299		 * Here we check to see if we were have already done a FR
3300		 * and if so we see if the biggest TSN we saw in the sack is
3301		 * smaller than the recovery point. If so we don't strike
3302		 * the tsn... otherwise we CAN strike the TSN.
3303		 */
3304		/*
3305		 * @@@ JRI: Check for CMT if (accum_moved &&
3306		 * asoc->fast_retran_loss_recovery && (sctp_cmt_on_off ==
3307		 * 0)) {
3308		 */
3309		if (accum_moved && asoc->fast_retran_loss_recovery) {
3310			/*
3311			 * Strike the TSN if in fast-recovery and cum-ack
3312			 * moved.
3313			 */
3314			if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3315				sctp_log_fr(biggest_tsn_newly_acked,
3316				    tp1->rec.data.TSN_seq,
3317				    tp1->sent,
3318				    SCTP_FR_LOG_STRIKE_CHUNK);
3319			}
3320			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3321				tp1->sent++;
3322			}
3323			if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3324				/*
3325				 * CMT DAC algorithm: If SACK flag is set to
3326				 * 0, then lowest_newack test will not pass
3327				 * because it would have been set to the
3328				 * cumack earlier. If not already to be
3329				 * rtx'd, If not a mixed sack and if tp1 is
3330				 * not between two sacked TSNs, then mark by
3331				 * one more. NOTE that we are marking by one
3332				 * additional time since the SACK DAC flag
3333				 * indicates that two packets have been
3334				 * received after this missing TSN.
3335				 */
3336				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3337				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3338					if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3339						sctp_log_fr(16 + num_dests_sacked,
3340						    tp1->rec.data.TSN_seq,
3341						    tp1->sent,
3342						    SCTP_FR_LOG_STRIKE_CHUNK);
3343					}
3344					tp1->sent++;
3345				}
3346			}
3347		} else if (tp1->rec.data.doing_fast_retransmit) {
3348			/*
3349			 * For those that have done a FR we must take
3350			 * special consideration if we strike. I.e the
3351			 * biggest_newly_acked must be higher than the
3352			 * sending_seq at the time we did the FR.
3353			 */
3354			if (
3355#ifdef SCTP_FR_TO_ALTERNATE
3356			/*
3357			 * If FR's go to new networks, then we must only do
3358			 * this for singly homed asoc's. However if the FR's
3359			 * go to the same network (Armando's work) then its
3360			 * ok to FR multiple times.
3361			 */
3362			    (asoc->numnets < 2)
3363#else
3364			    (1)
3365#endif
3366			    ) {
3367
3368				if ((compare_with_wrap(biggest_tsn_newly_acked,
3369				    tp1->rec.data.fast_retran_tsn, MAX_TSN)) ||
3370				    (biggest_tsn_newly_acked ==
3371				    tp1->rec.data.fast_retran_tsn)) {
3372					/*
3373					 * Strike the TSN, since this ack is
3374					 * beyond where things were when we
3375					 * did a FR.
3376					 */
3377					if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3378						sctp_log_fr(biggest_tsn_newly_acked,
3379						    tp1->rec.data.TSN_seq,
3380						    tp1->sent,
3381						    SCTP_FR_LOG_STRIKE_CHUNK);
3382					}
3383					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3384						tp1->sent++;
3385					}
3386					strike_flag = 1;
3387					if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3388						/*
3389						 * CMT DAC algorithm: If
3390						 * SACK flag is set to 0,
3391						 * then lowest_newack test
3392						 * will not pass because it
3393						 * would have been set to
3394						 * the cumack earlier. If
3395						 * not already to be rtx'd,
3396						 * If not a mixed sack and
3397						 * if tp1 is not between two
3398						 * sacked TSNs, then mark by
3399						 * one more. NOTE that we
3400						 * are marking by one
3401						 * additional time since the
3402						 * SACK DAC flag indicates
3403						 * that two packets have
3404						 * been received after this
3405						 * missing TSN.
3406						 */
3407						if ((tp1->sent < SCTP_DATAGRAM_RESEND) &&
3408						    (num_dests_sacked == 1) &&
3409						    compare_with_wrap(this_sack_lowest_newack,
3410						    tp1->rec.data.TSN_seq, MAX_TSN)) {
3411							if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3412								sctp_log_fr(32 + num_dests_sacked,
3413								    tp1->rec.data.TSN_seq,
3414								    tp1->sent,
3415								    SCTP_FR_LOG_STRIKE_CHUNK);
3416							}
3417							if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3418								tp1->sent++;
3419							}
3420						}
3421					}
3422				}
3423			}
3424			/*
3425			 * JRI: TODO: remove code for HTNA algo. CMT's SFR
3426			 * algo covers HTNA.
3427			 */
3428		} else if (compare_with_wrap(tp1->rec.data.TSN_seq,
3429		    biggest_tsn_newly_acked, MAX_TSN)) {
3430			/*
3431			 * We don't strike these: This is the  HTNA
3432			 * algorithm i.e. we don't strike If our TSN is
3433			 * larger than the Highest TSN Newly Acked.
3434			 */
3435			;
3436		} else {
3437			/* Strike the TSN */
3438			if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3439				sctp_log_fr(biggest_tsn_newly_acked,
3440				    tp1->rec.data.TSN_seq,
3441				    tp1->sent,
3442				    SCTP_FR_LOG_STRIKE_CHUNK);
3443			}
3444			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3445				tp1->sent++;
3446			}
3447			if (sctp_cmt_on_off && sctp_cmt_use_dac) {
3448				/*
3449				 * CMT DAC algorithm: If SACK flag is set to
3450				 * 0, then lowest_newack test will not pass
3451				 * because it would have been set to the
3452				 * cumack earlier. If not already to be
3453				 * rtx'd, If not a mixed sack and if tp1 is
3454				 * not between two sacked TSNs, then mark by
3455				 * one more. NOTE that we are marking by one
3456				 * additional time since the SACK DAC flag
3457				 * indicates that two packets have been
3458				 * received after this missing TSN.
3459				 */
3460				if ((tp1->sent < SCTP_DATAGRAM_RESEND) && (num_dests_sacked == 1) &&
3461				    compare_with_wrap(this_sack_lowest_newack, tp1->rec.data.TSN_seq, MAX_TSN)) {
3462					if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3463						sctp_log_fr(48 + num_dests_sacked,
3464						    tp1->rec.data.TSN_seq,
3465						    tp1->sent,
3466						    SCTP_FR_LOG_STRIKE_CHUNK);
3467					}
3468					tp1->sent++;
3469				}
3470			}
3471		}
3472		if (tp1->sent == SCTP_DATAGRAM_RESEND) {
3473			/* Increment the count to resend */
3474			struct sctp_nets *alt;
3475
3476			/* printf("OK, we are now ready to FR this guy\n"); */
3477			if (sctp_logging_level & SCTP_FR_LOGGING_ENABLE) {
3478				sctp_log_fr(tp1->rec.data.TSN_seq, tp1->snd_count,
3479				    0, SCTP_FR_MARKED);
3480			}
3481			if (strike_flag) {
3482				/* This is a subsequent FR */
3483				SCTP_STAT_INCR(sctps_sendmultfastretrans);
3484			}
3485			sctp_ucount_incr(stcb->asoc.sent_queue_retran_cnt);
3486			if (sctp_cmt_on_off) {
3487				/*
3488				 * CMT: Using RTX_SSTHRESH policy for CMT.
3489				 * If CMT is being used, then pick dest with
3490				 * largest ssthresh for any retransmission.
3491				 */
3492				tp1->no_fr_allowed = 1;
3493				alt = tp1->whoTo;
3494				/* sa_ignore NO_NULL_CHK */
3495				if (sctp_cmt_on_off && sctp_cmt_pf) {
3496					/*
3497					 * JRS 5/18/07 - If CMT PF is on,
3498					 * use the PF version of
3499					 * find_alt_net()
3500					 */
3501					alt = sctp_find_alternate_net(stcb, alt, 2);
3502				} else {
3503					/*
3504					 * JRS 5/18/07 - If only CMT is on,
3505					 * use the CMT version of
3506					 * find_alt_net()
3507					 */
3508					/* sa_ignore NO_NULL_CHK */
3509					alt = sctp_find_alternate_net(stcb, alt, 1);
3510				}
3511				if (alt == NULL) {
3512					alt = tp1->whoTo;
3513				}
3514				/*
3515				 * CUCv2: If a different dest is picked for
3516				 * the retransmission, then new
3517				 * (rtx-)pseudo_cumack needs to be tracked
3518				 * for orig dest. Let CUCv2 track new (rtx-)
3519				 * pseudo-cumack always.
3520				 */
3521				if (tp1->whoTo) {
3522					tp1->whoTo->find_pseudo_cumack = 1;
3523					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3524				}
3525			} else {/* CMT is OFF */
3526
3527#ifdef SCTP_FR_TO_ALTERNATE
3528				/* Can we find an alternate? */
3529				alt = sctp_find_alternate_net(stcb, tp1->whoTo, 0);
3530#else
3531				/*
3532				 * default behavior is to NOT retransmit
3533				 * FR's to an alternate. Armando Caro's
3534				 * paper details why.
3535				 */
3536				alt = tp1->whoTo;
3537#endif
3538			}
3539
3540			tp1->rec.data.doing_fast_retransmit = 1;
3541			tot_retrans++;
3542			/* mark the sending seq for possible subsequent FR's */
3543			/*
3544			 * printf("Marking TSN for FR new value %x\n",
3545			 * (uint32_t)tpi->rec.data.TSN_seq);
3546			 */
3547			if (TAILQ_EMPTY(&asoc->send_queue)) {
3548				/*
3549				 * If the queue of send is empty then its
3550				 * the next sequence number that will be
3551				 * assigned so we subtract one from this to
3552				 * get the one we last sent.
3553				 */
3554				tp1->rec.data.fast_retran_tsn = sending_seq;
3555			} else {
3556				/*
3557				 * If there are chunks on the send queue
3558				 * (unsent data that has made it from the
3559				 * stream queues but not out the door, we
3560				 * take the first one (which will have the
3561				 * lowest TSN) and subtract one to get the
3562				 * one we last sent.
3563				 */
3564				struct sctp_tmit_chunk *ttt;
3565
3566				ttt = TAILQ_FIRST(&asoc->send_queue);
3567				tp1->rec.data.fast_retran_tsn =
3568				    ttt->rec.data.TSN_seq;
3569			}
3570
3571			if (tp1->do_rtt) {
3572				/*
3573				 * this guy had a RTO calculation pending on
3574				 * it, cancel it
3575				 */
3576				tp1->do_rtt = 0;
3577			}
3578			/* fix counts and things */
3579			if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3580				sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_RSND,
3581				    (tp1->whoTo ? (tp1->whoTo->flight_size) : 0),
3582				    tp1->book_size,
3583				    (uintptr_t) tp1->whoTo,
3584				    tp1->rec.data.TSN_seq);
3585			}
3586			if (tp1->whoTo) {
3587				tp1->whoTo->net_ack++;
3588				sctp_flight_size_decrease(tp1);
3589			}
3590			if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
3591				sctp_log_rwnd(SCTP_INCREASE_PEER_RWND,
3592				    asoc->peers_rwnd, tp1->send_size, sctp_peer_chunk_oh);
3593			}
3594			/* add back to the rwnd */
3595			asoc->peers_rwnd += (tp1->send_size + sctp_peer_chunk_oh);
3596
3597			/* remove from the total flight */
3598			sctp_total_flight_decrease(stcb, tp1);
3599			if (alt != tp1->whoTo) {
3600				/* yes, there is an alternate. */
3601				sctp_free_remote_addr(tp1->whoTo);
3602				/* sa_ignore FREED_MEMORY */
3603				tp1->whoTo = alt;
3604				atomic_add_int(&alt->ref_count, 1);
3605			}
3606		}
3607		tp1 = TAILQ_NEXT(tp1, sctp_next);
3608	}			/* while (tp1) */
3609
3610	if (tot_retrans > 0) {
3611		/*
3612		 * Setup the ecn nonce re-sync point. We do this since once
3613		 * we go to FR something we introduce a Karn's rule scenario
3614		 * and won't know the totals for the ECN bits.
3615		 */
3616		asoc->nonce_resync_tsn = sending_seq;
3617		asoc->nonce_wait_for_ecne = 0;
3618		asoc->nonce_sum_check = 0;
3619	}
3620}
3621
3622struct sctp_tmit_chunk *
3623sctp_try_advance_peer_ack_point(struct sctp_tcb *stcb,
3624    struct sctp_association *asoc)
3625{
3626	struct sctp_tmit_chunk *tp1, *tp2, *a_adv = NULL;
3627	struct timeval now;
3628	int now_filled = 0;
3629
3630	if (asoc->peer_supports_prsctp == 0) {
3631		return (NULL);
3632	}
3633	tp1 = TAILQ_FIRST(&asoc->sent_queue);
3634	while (tp1) {
3635		if (tp1->sent != SCTP_FORWARD_TSN_SKIP &&
3636		    tp1->sent != SCTP_DATAGRAM_RESEND) {
3637			/* no chance to advance, out of here */
3638			break;
3639		}
3640		if (!PR_SCTP_ENABLED(tp1->flags)) {
3641			/*
3642			 * We can't fwd-tsn past any that are reliable aka
3643			 * retransmitted until the asoc fails.
3644			 */
3645			break;
3646		}
3647		if (!now_filled) {
3648			(void)SCTP_GETTIME_TIMEVAL(&now);
3649			now_filled = 1;
3650		}
3651		tp2 = TAILQ_NEXT(tp1, sctp_next);
3652		/*
3653		 * now we got a chunk which is marked for another
3654		 * retransmission to a PR-stream but has run out its chances
3655		 * already maybe OR has been marked to skip now. Can we skip
3656		 * it if its a resend?
3657		 */
3658		if (tp1->sent == SCTP_DATAGRAM_RESEND &&
3659		    (PR_SCTP_TTL_ENABLED(tp1->flags))) {
3660			/*
3661			 * Now is this one marked for resend and its time is
3662			 * now up?
3663			 */
3664			if (timevalcmp(&now, &tp1->rec.data.timetodrop, >)) {
3665				/* Yes so drop it */
3666				if (tp1->data) {
3667					(void)sctp_release_pr_sctp_chunk(stcb, tp1,
3668					    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3669					    &asoc->sent_queue, SCTP_SO_NOT_LOCKED);
3670				}
3671			} else {
3672				/*
3673				 * No, we are done when hit one for resend
3674				 * whos time as not expired.
3675				 */
3676				break;
3677			}
3678		}
3679		/*
3680		 * Ok now if this chunk is marked to drop it we can clean up
3681		 * the chunk, advance our peer ack point and we can check
3682		 * the next chunk.
3683		 */
3684		if (tp1->sent == SCTP_FORWARD_TSN_SKIP) {
3685			/* advance PeerAckPoint goes forward */
3686			asoc->advanced_peer_ack_point = tp1->rec.data.TSN_seq;
3687			a_adv = tp1;
3688			/*
3689			 * we don't want to de-queue it here. Just wait for
3690			 * the next peer SACK to come with a new cumTSN and
3691			 * then the chunk will be droped in the normal
3692			 * fashion.
3693			 */
3694			if (tp1->data) {
3695				sctp_free_bufspace(stcb, asoc, tp1, 1);
3696				/*
3697				 * Maybe there should be another
3698				 * notification type
3699				 */
3700				sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
3701				    (SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT),
3702				    tp1, SCTP_SO_NOT_LOCKED);
3703				sctp_m_freem(tp1->data);
3704				tp1->data = NULL;
3705				if (stcb->sctp_socket) {
3706#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3707					struct socket *so;
3708
3709					so = SCTP_INP_SO(stcb->sctp_ep);
3710					atomic_add_int(&stcb->asoc.refcnt, 1);
3711					SCTP_TCB_UNLOCK(stcb);
3712					SCTP_SOCKET_LOCK(so, 1);
3713					SCTP_TCB_LOCK(stcb);
3714					atomic_subtract_int(&stcb->asoc.refcnt, 1);
3715					if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3716						/*
3717						 * assoc was freed while we
3718						 * were unlocked
3719						 */
3720						SCTP_SOCKET_UNLOCK(so, 1);
3721						return (NULL);
3722					}
3723#endif
3724					sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
3725#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3726					SCTP_SOCKET_UNLOCK(so, 1);
3727#endif
3728					if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
3729						sctp_wakeup_log(stcb, tp1->rec.data.TSN_seq, 1, SCTP_WAKESND_FROM_FWDTSN);
3730					}
3731				}
3732			}
3733		} else {
3734			/*
3735			 * If it is still in RESEND we can advance no
3736			 * further
3737			 */
3738			break;
3739		}
3740		/*
3741		 * If we hit here we just dumped tp1, move to next tsn on
3742		 * sent queue.
3743		 */
3744		tp1 = tp2;
3745	}
3746	return (a_adv);
3747}
3748
3749static void
3750sctp_fs_audit(struct sctp_association *asoc)
3751{
3752	struct sctp_tmit_chunk *chk;
3753	int inflight = 0, resend = 0, inbetween = 0, acked = 0, above = 0;
3754
3755	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3756		if (chk->sent < SCTP_DATAGRAM_RESEND) {
3757			inflight++;
3758		} else if (chk->sent == SCTP_DATAGRAM_RESEND) {
3759			resend++;
3760		} else if (chk->sent < SCTP_DATAGRAM_ACKED) {
3761			inbetween++;
3762		} else if (chk->sent > SCTP_DATAGRAM_ACKED) {
3763			above++;
3764		} else {
3765			acked++;
3766		}
3767	}
3768
3769	if ((inflight > 0) || (inbetween > 0)) {
3770#ifdef INVARIANTS
3771		panic("Flight size-express incorrect? \n");
3772#else
3773		SCTP_PRINTF("Flight size-express incorrect inflight:%d inbetween:%d\n",
3774		    inflight, inbetween);
3775#endif
3776	}
3777}
3778
3779
3780static void
3781sctp_window_probe_recovery(struct sctp_tcb *stcb,
3782    struct sctp_association *asoc,
3783    struct sctp_nets *net,
3784    struct sctp_tmit_chunk *tp1)
3785{
3786	struct sctp_tmit_chunk *chk;
3787
3788	/* First setup this one and get it moved back */
3789	tp1->sent = SCTP_DATAGRAM_UNSENT;
3790	if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3791		sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_WP,
3792		    tp1->whoTo->flight_size,
3793		    tp1->book_size,
3794		    (uintptr_t) tp1->whoTo,
3795		    tp1->rec.data.TSN_seq);
3796	}
3797	sctp_flight_size_decrease(tp1);
3798	sctp_total_flight_decrease(stcb, tp1);
3799	TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
3800	TAILQ_INSERT_HEAD(&asoc->send_queue, tp1, sctp_next);
3801	asoc->sent_queue_cnt--;
3802	asoc->send_queue_cnt++;
3803	/*
3804	 * Now all guys marked for RESEND on the sent_queue must be moved
3805	 * back too.
3806	 */
3807	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3808		if (chk->sent == SCTP_DATAGRAM_RESEND) {
3809			/* Another chunk to move */
3810			chk->sent = SCTP_DATAGRAM_UNSENT;
3811			/* It should not be in flight */
3812			TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3813			TAILQ_INSERT_AFTER(&asoc->send_queue, tp1, chk, sctp_next);
3814			asoc->sent_queue_cnt--;
3815			asoc->send_queue_cnt++;
3816			sctp_ucount_decr(asoc->sent_queue_retran_cnt);
3817		}
3818	}
3819}
3820
3821void
3822sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
3823    uint32_t rwnd, int nonce_sum_flag, int *abort_now)
3824{
3825	struct sctp_nets *net;
3826	struct sctp_association *asoc;
3827	struct sctp_tmit_chunk *tp1, *tp2;
3828	uint32_t old_rwnd;
3829	int win_probe_recovery = 0;
3830	int win_probe_recovered = 0;
3831	int j, done_once = 0;
3832
3833	if (sctp_logging_level & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
3834		sctp_misc_ints(SCTP_SACK_LOG_EXPRESS, cumack,
3835		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
3836	}
3837	SCTP_TCB_LOCK_ASSERT(stcb);
3838#ifdef SCTP_ASOCLOG_OF_TSNS
3839	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cumack;
3840	stcb->asoc.cumack_log_at++;
3841	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
3842		stcb->asoc.cumack_log_at = 0;
3843	}
3844#endif
3845	asoc = &stcb->asoc;
3846	old_rwnd = asoc->peers_rwnd;
3847	if (compare_with_wrap(asoc->last_acked_seq, cumack, MAX_TSN)) {
3848		/* old ack */
3849		return;
3850	} else if (asoc->last_acked_seq == cumack) {
3851		/* Window update sack */
3852		asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
3853		    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
3854		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
3855			/* SWS sender side engages */
3856			asoc->peers_rwnd = 0;
3857		}
3858		if (asoc->peers_rwnd > old_rwnd) {
3859			goto again;
3860		}
3861		return;
3862	}
3863	/* First setup for CC stuff */
3864	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3865		net->prev_cwnd = net->cwnd;
3866		net->net_ack = 0;
3867		net->net_ack2 = 0;
3868
3869		/*
3870		 * CMT: Reset CUC and Fast recovery algo variables before
3871		 * SACK processing
3872		 */
3873		net->new_pseudo_cumack = 0;
3874		net->will_exit_fast_recovery = 0;
3875	}
3876	if (sctp_strict_sacks) {
3877		uint32_t send_s;
3878
3879		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3880			tp1 = TAILQ_LAST(&asoc->sent_queue,
3881			    sctpchunk_listhead);
3882			send_s = tp1->rec.data.TSN_seq + 1;
3883		} else {
3884			send_s = asoc->sending_seq;
3885		}
3886		if ((cumack == send_s) ||
3887		    compare_with_wrap(cumack, send_s, MAX_TSN)) {
3888#ifndef INVARIANTS
3889			struct mbuf *oper;
3890
3891#endif
3892#ifdef INVARIANTS
3893			panic("Impossible sack 1");
3894#else
3895			*abort_now = 1;
3896			/* XXX */
3897			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3898			    0, M_DONTWAIT, 1, MT_DATA);
3899			if (oper) {
3900				struct sctp_paramhdr *ph;
3901				uint32_t *ippp;
3902
3903				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
3904				    sizeof(uint32_t);
3905				ph = mtod(oper, struct sctp_paramhdr *);
3906				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
3907				ph->param_length = htons(SCTP_BUF_LEN(oper));
3908				ippp = (uint32_t *) (ph + 1);
3909				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
3910			}
3911			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
3912			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
3913			return;
3914#endif
3915		}
3916	}
3917	asoc->this_sack_highest_gap = cumack;
3918	if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) {
3919		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
3920		    stcb->asoc.overall_error_count,
3921		    0,
3922		    SCTP_FROM_SCTP_INDATA,
3923		    __LINE__);
3924	}
3925	stcb->asoc.overall_error_count = 0;
3926	if (compare_with_wrap(cumack, asoc->last_acked_seq, MAX_TSN)) {
3927		/* process the new consecutive TSN first */
3928		tp1 = TAILQ_FIRST(&asoc->sent_queue);
3929		while (tp1) {
3930			tp2 = TAILQ_NEXT(tp1, sctp_next);
3931			if (compare_with_wrap(cumack, tp1->rec.data.TSN_seq,
3932			    MAX_TSN) ||
3933			    cumack == tp1->rec.data.TSN_seq) {
3934				if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
3935					printf("Warning, an unsent is now acked?\n");
3936				}
3937				/*
3938				 * ECN Nonce: Add the nonce to the sender's
3939				 * nonce sum
3940				 */
3941				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
3942				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
3943					/*
3944					 * If it is less than ACKED, it is
3945					 * now no-longer in flight. Higher
3946					 * values may occur during marking
3947					 */
3948					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
3949						if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
3950							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
3951							    tp1->whoTo->flight_size,
3952							    tp1->book_size,
3953							    (uintptr_t) tp1->whoTo,
3954							    tp1->rec.data.TSN_seq);
3955						}
3956						sctp_flight_size_decrease(tp1);
3957						/* sa_ignore NO_NULL_CHK */
3958						sctp_total_flight_decrease(stcb, tp1);
3959					}
3960					tp1->whoTo->net_ack += tp1->send_size;
3961					if (tp1->snd_count < 2) {
3962						/*
3963						 * True non-retransmited
3964						 * chunk
3965						 */
3966						tp1->whoTo->net_ack2 +=
3967						    tp1->send_size;
3968
3969						/* update RTO too? */
3970						if (tp1->do_rtt) {
3971							tp1->whoTo->RTO =
3972							/*
3973							 * sa_ignore
3974							 * NO_NULL_CHK
3975							 */
3976							    sctp_calculate_rto(stcb,
3977							    asoc, tp1->whoTo,
3978							    &tp1->sent_rcv_time,
3979							    sctp_align_safe_nocopy);
3980							tp1->do_rtt = 0;
3981						}
3982					}
3983					/*
3984					 * CMT: CUCv2 algorithm. From the
3985					 * cumack'd TSNs, for each TSN being
3986					 * acked for the first time, set the
3987					 * following variables for the
3988					 * corresp destination.
3989					 * new_pseudo_cumack will trigger a
3990					 * cwnd update.
3991					 * find_(rtx_)pseudo_cumack will
3992					 * trigger search for the next
3993					 * expected (rtx-)pseudo-cumack.
3994					 */
3995					tp1->whoTo->new_pseudo_cumack = 1;
3996					tp1->whoTo->find_pseudo_cumack = 1;
3997					tp1->whoTo->find_rtx_pseudo_cumack = 1;
3998
3999					if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4000						/* sa_ignore NO_NULL_CHK */
4001						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4002					}
4003				}
4004				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4005					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4006				}
4007				if (tp1->rec.data.chunk_was_revoked) {
4008					/* deflate the cwnd */
4009					tp1->whoTo->cwnd -= tp1->book_size;
4010					tp1->rec.data.chunk_was_revoked = 0;
4011				}
4012				tp1->sent = SCTP_DATAGRAM_ACKED;
4013				TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4014				if (tp1->data) {
4015					/* sa_ignore NO_NULL_CHK */
4016					sctp_free_bufspace(stcb, asoc, tp1, 1);
4017					sctp_m_freem(tp1->data);
4018				}
4019				if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4020					sctp_log_sack(asoc->last_acked_seq,
4021					    cumack,
4022					    tp1->rec.data.TSN_seq,
4023					    0,
4024					    0,
4025					    SCTP_LOG_FREE_SENT);
4026				}
4027				tp1->data = NULL;
4028				asoc->sent_queue_cnt--;
4029				sctp_free_a_chunk(stcb, tp1);
4030				tp1 = tp2;
4031			} else {
4032				break;
4033			}
4034		}
4035
4036	}
4037	/* sa_ignore NO_NULL_CHK */
4038	if (stcb->sctp_socket) {
4039#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4040		struct socket *so;
4041
4042#endif
4043
4044		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4045		if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4046			/* sa_ignore NO_NULL_CHK */
4047			sctp_wakeup_log(stcb, cumack, 1, SCTP_WAKESND_FROM_SACK);
4048		}
4049#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4050		so = SCTP_INP_SO(stcb->sctp_ep);
4051		atomic_add_int(&stcb->asoc.refcnt, 1);
4052		SCTP_TCB_UNLOCK(stcb);
4053		SCTP_SOCKET_LOCK(so, 1);
4054		SCTP_TCB_LOCK(stcb);
4055		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4056		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4057			/* assoc was freed while we were unlocked */
4058			SCTP_SOCKET_UNLOCK(so, 1);
4059			return;
4060		}
4061#endif
4062		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4063#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4064		SCTP_SOCKET_UNLOCK(so, 1);
4065#endif
4066	} else {
4067		if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4068			sctp_wakeup_log(stcb, cumack, 1, SCTP_NOWAKE_FROM_SACK);
4069		}
4070	}
4071
4072	/* JRS - Use the congestion control given in the CC module */
4073	if (asoc->last_acked_seq != cumack)
4074		asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, 1, 0, 0);
4075
4076	asoc->last_acked_seq = cumack;
4077
4078	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4079		/* nothing left in-flight */
4080		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4081			net->flight_size = 0;
4082			net->partial_bytes_acked = 0;
4083		}
4084		asoc->total_flight = 0;
4085		asoc->total_flight_count = 0;
4086	}
4087	/* Fix up the a-p-a-p for future PR-SCTP sends */
4088	if (compare_with_wrap(cumack, asoc->advanced_peer_ack_point, MAX_TSN)) {
4089		asoc->advanced_peer_ack_point = cumack;
4090	}
4091	/* ECN Nonce updates */
4092	if (asoc->ecn_nonce_allowed) {
4093		if (asoc->nonce_sum_check) {
4094			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base) & SCTP_SACK_NONCE_SUM)) {
4095				if (asoc->nonce_wait_for_ecne == 0) {
4096					struct sctp_tmit_chunk *lchk;
4097
4098					lchk = TAILQ_FIRST(&asoc->send_queue);
4099					asoc->nonce_wait_for_ecne = 1;
4100					if (lchk) {
4101						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
4102					} else {
4103						asoc->nonce_wait_tsn = asoc->sending_seq;
4104					}
4105				} else {
4106					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
4107					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
4108						/*
4109						 * Misbehaving peer. We need
4110						 * to react to this guy
4111						 */
4112						asoc->ecn_allowed = 0;
4113						asoc->ecn_nonce_allowed = 0;
4114					}
4115				}
4116			}
4117		} else {
4118			/* See if Resynchronization Possible */
4119			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
4120				asoc->nonce_sum_check = 1;
4121				/*
4122				 * now we must calculate what the base is.
4123				 * We do this based on two things, we know
4124				 * the total's for all the segments
4125				 * gap-acked in the SACK (none), We also
4126				 * know the SACK's nonce sum, its in
4127				 * nonce_sum_flag. So we can build a truth
4128				 * table to back-calculate the new value of
4129				 * asoc->nonce_sum_expect_base:
4130				 *
4131				 * SACK-flag-Value         Seg-Sums Base 0 0 0
4132				 * 1                    0 1 0 1 1 1
4133				 * 1 0
4134				 */
4135				asoc->nonce_sum_expect_base = (0 ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
4136			}
4137		}
4138	}
4139	/* RWND update */
4140	asoc->peers_rwnd = sctp_sbspace_sub(rwnd,
4141	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
4142	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4143		/* SWS sender side engages */
4144		asoc->peers_rwnd = 0;
4145	}
4146	if (asoc->peers_rwnd > old_rwnd) {
4147		win_probe_recovery = 1;
4148	}
4149	/* Now assure a timer where data is queued at */
4150again:
4151	j = 0;
4152	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4153		if (win_probe_recovery && (net->window_probe)) {
4154			net->window_probe = 0;
4155			win_probe_recovered = 1;
4156			/*
4157			 * Find first chunk that was used with window probe
4158			 * and clear the sent
4159			 */
4160			/* sa_ignore FREED_MEMORY */
4161			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4162				if (tp1->window_probe) {
4163					/* move back to data send queue */
4164					sctp_window_probe_recovery(stcb, asoc, net, tp1);
4165					break;
4166				}
4167			}
4168		}
4169		if (net->flight_size) {
4170			int to_ticks;
4171
4172			if (net->RTO == 0) {
4173				to_ticks = MSEC_TO_TICKS(stcb->asoc.initial_rto);
4174			} else {
4175				to_ticks = MSEC_TO_TICKS(net->RTO);
4176			}
4177			j++;
4178			(void)SCTP_OS_TIMER_START(&net->rxt_timer.timer, to_ticks,
4179			    sctp_timeout_handler, &net->rxt_timer);
4180		} else {
4181			if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4182				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4183				    stcb, net,
4184				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
4185			}
4186			if (sctp_early_fr) {
4187				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4188					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4189					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4190					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
4191				}
4192			}
4193		}
4194	}
4195	if ((j == 0) &&
4196	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
4197	    (asoc->sent_queue_retran_cnt == 0) &&
4198	    (win_probe_recovered == 0) &&
4199	    (done_once == 0)) {
4200		/* huh, this should not happen */
4201		sctp_fs_audit(asoc);
4202		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4203			net->flight_size = 0;
4204		}
4205		asoc->total_flight = 0;
4206		asoc->total_flight_count = 0;
4207		asoc->sent_queue_retran_cnt = 0;
4208		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4209			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4210				sctp_flight_size_increase(tp1);
4211				sctp_total_flight_increase(stcb, tp1);
4212			} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4213				asoc->sent_queue_retran_cnt++;
4214			}
4215		}
4216		done_once = 1;
4217		goto again;
4218	}
4219	/**********************************/
4220	/* Now what about shutdown issues */
4221	/**********************************/
4222	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4223		/* nothing left on sendqueue.. consider done */
4224		/* clean up */
4225		if ((asoc->stream_queue_cnt == 1) &&
4226		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4227		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4228		    (asoc->locked_on_sending)
4229		    ) {
4230			struct sctp_stream_queue_pending *sp;
4231
4232			/*
4233			 * I may be in a state where we got all across.. but
4234			 * cannot write more due to a shutdown... we abort
4235			 * since the user did not indicate EOR in this case.
4236			 * The sp will be cleaned during free of the asoc.
4237			 */
4238			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4239			    sctp_streamhead);
4240			if ((sp) && (sp->length == 0)) {
4241				/* Let cleanup code purge it */
4242				if (sp->msg_is_complete) {
4243					asoc->stream_queue_cnt--;
4244				} else {
4245					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4246					asoc->locked_on_sending = NULL;
4247					asoc->stream_queue_cnt--;
4248				}
4249			}
4250		}
4251		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4252		    (asoc->stream_queue_cnt == 0)) {
4253			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4254				/* Need to abort here */
4255				struct mbuf *oper;
4256
4257		abort_out_now:
4258				*abort_now = 1;
4259				/* XXX */
4260				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4261				    0, M_DONTWAIT, 1, MT_DATA);
4262				if (oper) {
4263					struct sctp_paramhdr *ph;
4264					uint32_t *ippp;
4265
4266					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4267					    sizeof(uint32_t);
4268					ph = mtod(oper, struct sctp_paramhdr *);
4269					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4270					ph->param_length = htons(SCTP_BUF_LEN(oper));
4271					ippp = (uint32_t *) (ph + 1);
4272					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_24);
4273				}
4274				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_24;
4275				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4276			} else {
4277				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4278				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4279					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4280				}
4281				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
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_send_shutdown_ack(stcb,
4298			    stcb->asoc.primary_destination);
4299
4300			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4301			    stcb->sctp_ep, stcb, asoc->primary_destination);
4302		}
4303	}
4304	if (sctp_logging_level & SCTP_SACK_RWND_LOGGING_ENABLE) {
4305		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
4306		    rwnd,
4307		    stcb->asoc.peers_rwnd,
4308		    stcb->asoc.total_flight,
4309		    stcb->asoc.total_output_queue_size);
4310	}
4311}
4312
4313void
4314sctp_handle_sack(struct mbuf *m, int offset,
4315    struct sctp_sack_chunk *ch, struct sctp_tcb *stcb,
4316    struct sctp_nets *net_from, int *abort_now, int sack_len, uint32_t rwnd)
4317{
4318	struct sctp_association *asoc;
4319	struct sctp_sack *sack;
4320	struct sctp_tmit_chunk *tp1, *tp2;
4321	uint32_t cum_ack, last_tsn, biggest_tsn_acked, biggest_tsn_newly_acked,
4322	         this_sack_lowest_newack;
4323	uint32_t sav_cum_ack;
4324	uint16_t num_seg, num_dup;
4325	uint16_t wake_him = 0;
4326	unsigned int sack_length;
4327	uint32_t send_s = 0;
4328	long j;
4329	int accum_moved = 0;
4330	int will_exit_fast_recovery = 0;
4331	uint32_t a_rwnd, old_rwnd;
4332	int win_probe_recovery = 0;
4333	int win_probe_recovered = 0;
4334	struct sctp_nets *net = NULL;
4335	int nonce_sum_flag, ecn_seg_sums = 0;
4336	int done_once;
4337	uint8_t reneged_all = 0;
4338	uint8_t cmt_dac_flag;
4339
4340	/*
4341	 * we take any chance we can to service our queues since we cannot
4342	 * get awoken when the socket is read from :<
4343	 */
4344	/*
4345	 * Now perform the actual SACK handling: 1) Verify that it is not an
4346	 * old sack, if so discard. 2) If there is nothing left in the send
4347	 * queue (cum-ack is equal to last acked) then you have a duplicate
4348	 * too, update any rwnd change and verify no timers are running.
4349	 * then return. 3) Process any new consequtive data i.e. cum-ack
4350	 * moved process these first and note that it moved. 4) Process any
4351	 * sack blocks. 5) Drop any acked from the queue. 6) Check for any
4352	 * revoked blocks and mark. 7) Update the cwnd. 8) Nothing left,
4353	 * sync up flightsizes and things, stop all timers and also check
4354	 * for shutdown_pending state. If so then go ahead and send off the
4355	 * shutdown. If in shutdown recv, send off the shutdown-ack and
4356	 * start that timer, Ret. 9) Strike any non-acked things and do FR
4357	 * procedure if needed being sure to set the FR flag. 10) Do pr-sctp
4358	 * procedures. 11) Apply any FR penalties. 12) Assure we will SACK
4359	 * if in shutdown_recv state.
4360	 */
4361	SCTP_TCB_LOCK_ASSERT(stcb);
4362	sack = &ch->sack;
4363	/* CMT DAC algo */
4364	this_sack_lowest_newack = 0;
4365	j = 0;
4366	sack_length = (unsigned int)sack_len;
4367	/* ECN Nonce */
4368	SCTP_STAT_INCR(sctps_slowpath_sack);
4369	nonce_sum_flag = ch->ch.chunk_flags & SCTP_SACK_NONCE_SUM;
4370	cum_ack = last_tsn = ntohl(sack->cum_tsn_ack);
4371#ifdef SCTP_ASOCLOG_OF_TSNS
4372	stcb->asoc.cumack_log[stcb->asoc.cumack_log_at] = cum_ack;
4373	stcb->asoc.cumack_log_at++;
4374	if (stcb->asoc.cumack_log_at > SCTP_TSN_LOG_SIZE) {
4375		stcb->asoc.cumack_log_at = 0;
4376	}
4377#endif
4378	num_seg = ntohs(sack->num_gap_ack_blks);
4379	a_rwnd = rwnd;
4380
4381	if (sctp_logging_level & SCTP_LOG_SACK_ARRIVALS_ENABLE) {
4382		sctp_misc_ints(SCTP_SACK_LOG_NORMAL, cum_ack,
4383		    rwnd, stcb->asoc.last_acked_seq, stcb->asoc.peers_rwnd);
4384	}
4385	/* CMT DAC algo */
4386	cmt_dac_flag = ch->ch.chunk_flags & SCTP_SACK_CMT_DAC;
4387	num_dup = ntohs(sack->num_dup_tsns);
4388
4389	old_rwnd = stcb->asoc.peers_rwnd;
4390	if (sctp_logging_level & SCTP_THRESHOLD_LOGGING) {
4391		sctp_misc_ints(SCTP_THRESHOLD_CLEAR,
4392		    stcb->asoc.overall_error_count,
4393		    0,
4394		    SCTP_FROM_SCTP_INDATA,
4395		    __LINE__);
4396	}
4397	stcb->asoc.overall_error_count = 0;
4398	asoc = &stcb->asoc;
4399	if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4400		sctp_log_sack(asoc->last_acked_seq,
4401		    cum_ack,
4402		    0,
4403		    num_seg,
4404		    num_dup,
4405		    SCTP_LOG_NEW_SACK);
4406	}
4407	if ((num_dup) && (sctp_logging_level & (SCTP_FR_LOGGING_ENABLE | SCTP_EARLYFR_LOGGING_ENABLE))) {
4408		int off_to_dup, iii;
4409		uint32_t *dupdata, dblock;
4410
4411		off_to_dup = (num_seg * sizeof(struct sctp_gap_ack_block)) + sizeof(struct sctp_sack_chunk);
4412		if ((off_to_dup + (num_dup * sizeof(uint32_t))) <= sack_length) {
4413			dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4414			    sizeof(uint32_t), (uint8_t *) & dblock);
4415			off_to_dup += sizeof(uint32_t);
4416			if (dupdata) {
4417				for (iii = 0; iii < num_dup; iii++) {
4418					sctp_log_fr(*dupdata, 0, 0, SCTP_FR_DUPED);
4419					dupdata = (uint32_t *) sctp_m_getptr(m, off_to_dup,
4420					    sizeof(uint32_t), (uint8_t *) & dblock);
4421					if (dupdata == NULL)
4422						break;
4423					off_to_dup += sizeof(uint32_t);
4424				}
4425			}
4426		} else {
4427			SCTP_PRINTF("Size invalid offset to dups:%d number dups:%d sack_len:%d num gaps:%d\n",
4428			    off_to_dup, num_dup, sack_length, num_seg);
4429		}
4430	}
4431	if (sctp_strict_sacks) {
4432		/* reality check */
4433		if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4434			tp1 = TAILQ_LAST(&asoc->sent_queue,
4435			    sctpchunk_listhead);
4436			send_s = tp1->rec.data.TSN_seq + 1;
4437		} else {
4438			send_s = asoc->sending_seq;
4439		}
4440		if (cum_ack == send_s ||
4441		    compare_with_wrap(cum_ack, send_s, MAX_TSN)) {
4442#ifndef INVARIANTS
4443			struct mbuf *oper;
4444
4445#endif
4446#ifdef INVARIANTS
4447	hopeless_peer:
4448			panic("Impossible sack 1");
4449#else
4450
4451
4452			/*
4453			 * no way, we have not even sent this TSN out yet.
4454			 * Peer is hopelessly messed up with us.
4455			 */
4456	hopeless_peer:
4457			*abort_now = 1;
4458			/* XXX */
4459			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4460			    0, M_DONTWAIT, 1, MT_DATA);
4461			if (oper) {
4462				struct sctp_paramhdr *ph;
4463				uint32_t *ippp;
4464
4465				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4466				    sizeof(uint32_t);
4467				ph = mtod(oper, struct sctp_paramhdr *);
4468				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4469				ph->param_length = htons(SCTP_BUF_LEN(oper));
4470				ippp = (uint32_t *) (ph + 1);
4471				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_25);
4472			}
4473			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_25;
4474			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
4475			return;
4476#endif
4477		}
4478	}
4479	/**********************/
4480	/* 1) check the range */
4481	/**********************/
4482	if (compare_with_wrap(asoc->last_acked_seq, last_tsn, MAX_TSN)) {
4483		/* acking something behind */
4484		return;
4485	}
4486	sav_cum_ack = asoc->last_acked_seq;
4487
4488	/* update the Rwnd of the peer */
4489	if (TAILQ_EMPTY(&asoc->sent_queue) &&
4490	    TAILQ_EMPTY(&asoc->send_queue) &&
4491	    (asoc->stream_queue_cnt == 0)
4492	    ) {
4493		/* nothing left on send/sent and strmq */
4494		if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
4495			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4496			    asoc->peers_rwnd, 0, 0, a_rwnd);
4497		}
4498		asoc->peers_rwnd = a_rwnd;
4499		if (asoc->sent_queue_retran_cnt) {
4500			asoc->sent_queue_retran_cnt = 0;
4501		}
4502		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4503			/* SWS sender side engages */
4504			asoc->peers_rwnd = 0;
4505		}
4506		/* stop any timers */
4507		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4508			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4509			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4510			if (sctp_early_fr) {
4511				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4512					SCTP_STAT_INCR(sctps_earlyfrstpidsck1);
4513					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4514					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_26);
4515				}
4516			}
4517			net->partial_bytes_acked = 0;
4518			net->flight_size = 0;
4519		}
4520		asoc->total_flight = 0;
4521		asoc->total_flight_count = 0;
4522		return;
4523	}
4524	/*
4525	 * We init netAckSz and netAckSz2 to 0. These are used to track 2
4526	 * things. The total byte count acked is tracked in netAckSz AND
4527	 * netAck2 is used to track the total bytes acked that are un-
4528	 * amibguious and were never retransmitted. We track these on a per
4529	 * destination address basis.
4530	 */
4531	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4532		net->prev_cwnd = net->cwnd;
4533		net->net_ack = 0;
4534		net->net_ack2 = 0;
4535
4536		/*
4537		 * CMT: Reset CUC and Fast recovery algo variables before
4538		 * SACK processing
4539		 */
4540		net->new_pseudo_cumack = 0;
4541		net->will_exit_fast_recovery = 0;
4542	}
4543	/* process the new consecutive TSN first */
4544	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4545	while (tp1) {
4546		if (compare_with_wrap(last_tsn, tp1->rec.data.TSN_seq,
4547		    MAX_TSN) ||
4548		    last_tsn == tp1->rec.data.TSN_seq) {
4549			if (tp1->sent != SCTP_DATAGRAM_UNSENT) {
4550				/*
4551				 * ECN Nonce: Add the nonce to the sender's
4552				 * nonce sum
4553				 */
4554				asoc->nonce_sum_expect_base += tp1->rec.data.ect_nonce;
4555				accum_moved = 1;
4556				if (tp1->sent < SCTP_DATAGRAM_ACKED) {
4557					/*
4558					 * If it is less than ACKED, it is
4559					 * now no-longer in flight. Higher
4560					 * values may occur during marking
4561					 */
4562					if ((tp1->whoTo->dest_state &
4563					    SCTP_ADDR_UNCONFIRMED) &&
4564					    (tp1->snd_count < 2)) {
4565						/*
4566						 * If there was no retran
4567						 * and the address is
4568						 * un-confirmed and we sent
4569						 * there and are now
4570						 * sacked.. its confirmed,
4571						 * mark it so.
4572						 */
4573						tp1->whoTo->dest_state &=
4574						    ~SCTP_ADDR_UNCONFIRMED;
4575					}
4576					if (tp1->sent < SCTP_DATAGRAM_RESEND) {
4577						if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
4578							sctp_misc_ints(SCTP_FLIGHT_LOG_DOWN_CA,
4579							    tp1->whoTo->flight_size,
4580							    tp1->book_size,
4581							    (uintptr_t) tp1->whoTo,
4582							    tp1->rec.data.TSN_seq);
4583						}
4584						sctp_flight_size_decrease(tp1);
4585						sctp_total_flight_decrease(stcb, tp1);
4586					}
4587					tp1->whoTo->net_ack += tp1->send_size;
4588
4589					/* CMT SFR and DAC algos */
4590					this_sack_lowest_newack = tp1->rec.data.TSN_seq;
4591					tp1->whoTo->saw_newack = 1;
4592
4593					if (tp1->snd_count < 2) {
4594						/*
4595						 * True non-retransmited
4596						 * chunk
4597						 */
4598						tp1->whoTo->net_ack2 +=
4599						    tp1->send_size;
4600
4601						/* update RTO too? */
4602						if (tp1->do_rtt) {
4603							tp1->whoTo->RTO =
4604							    sctp_calculate_rto(stcb,
4605							    asoc, tp1->whoTo,
4606							    &tp1->sent_rcv_time,
4607							    sctp_align_safe_nocopy);
4608							tp1->do_rtt = 0;
4609						}
4610					}
4611					/*
4612					 * CMT: CUCv2 algorithm. From the
4613					 * cumack'd TSNs, for each TSN being
4614					 * acked for the first time, set the
4615					 * following variables for the
4616					 * corresp destination.
4617					 * new_pseudo_cumack will trigger a
4618					 * cwnd update.
4619					 * find_(rtx_)pseudo_cumack will
4620					 * trigger search for the next
4621					 * expected (rtx-)pseudo-cumack.
4622					 */
4623					tp1->whoTo->new_pseudo_cumack = 1;
4624					tp1->whoTo->find_pseudo_cumack = 1;
4625					tp1->whoTo->find_rtx_pseudo_cumack = 1;
4626
4627
4628					if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4629						sctp_log_sack(asoc->last_acked_seq,
4630						    cum_ack,
4631						    tp1->rec.data.TSN_seq,
4632						    0,
4633						    0,
4634						    SCTP_LOG_TSN_ACKED);
4635					}
4636					if (sctp_logging_level & SCTP_CWND_LOGGING_ENABLE) {
4637						sctp_log_cwnd(stcb, tp1->whoTo, tp1->rec.data.TSN_seq, SCTP_CWND_LOG_FROM_SACK);
4638					}
4639				}
4640				if (tp1->sent == SCTP_DATAGRAM_RESEND) {
4641					sctp_ucount_decr(asoc->sent_queue_retran_cnt);
4642#ifdef SCTP_AUDITING_ENABLED
4643					sctp_audit_log(0xB3,
4644					    (asoc->sent_queue_retran_cnt & 0x000000ff));
4645#endif
4646				}
4647				if (tp1->rec.data.chunk_was_revoked) {
4648					/* deflate the cwnd */
4649					tp1->whoTo->cwnd -= tp1->book_size;
4650					tp1->rec.data.chunk_was_revoked = 0;
4651				}
4652				tp1->sent = SCTP_DATAGRAM_ACKED;
4653			}
4654		} else {
4655			break;
4656		}
4657		tp1 = TAILQ_NEXT(tp1, sctp_next);
4658	}
4659	biggest_tsn_newly_acked = biggest_tsn_acked = last_tsn;
4660	/* always set this up to cum-ack */
4661	asoc->this_sack_highest_gap = last_tsn;
4662
4663	/* Move offset up to point to gaps/dups */
4664	offset += sizeof(struct sctp_sack_chunk);
4665	if (((num_seg * (sizeof(struct sctp_gap_ack_block))) + sizeof(struct sctp_sack_chunk)) > sack_length) {
4666
4667		/* skip corrupt segments */
4668		goto skip_segments;
4669	}
4670	if (num_seg > 0) {
4671
4672		/*
4673		 * CMT: SFR algo (and HTNA) - this_sack_highest_newack has
4674		 * to be greater than the cumack. Also reset saw_newack to 0
4675		 * for all dests.
4676		 */
4677		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4678			net->saw_newack = 0;
4679			net->this_sack_highest_newack = last_tsn;
4680		}
4681
4682		/*
4683		 * thisSackHighestGap will increase while handling NEW
4684		 * segments this_sack_highest_newack will increase while
4685		 * handling NEWLY ACKED chunks. this_sack_lowest_newack is
4686		 * used for CMT DAC algo. saw_newack will also change.
4687		 */
4688		sctp_handle_segments(m, &offset, stcb, asoc, ch, last_tsn,
4689		    &biggest_tsn_acked, &biggest_tsn_newly_acked, &this_sack_lowest_newack,
4690		    num_seg, &ecn_seg_sums);
4691
4692		if (sctp_strict_sacks) {
4693			/*
4694			 * validate the biggest_tsn_acked in the gap acks if
4695			 * strict adherence is wanted.
4696			 */
4697			if ((biggest_tsn_acked == send_s) ||
4698			    (compare_with_wrap(biggest_tsn_acked, send_s, MAX_TSN))) {
4699				/*
4700				 * peer is either confused or we are under
4701				 * attack. We must abort.
4702				 */
4703				goto hopeless_peer;
4704			}
4705		}
4706	}
4707skip_segments:
4708	/*******************************************/
4709	/* cancel ALL T3-send timer if accum moved */
4710	/*******************************************/
4711	if (sctp_cmt_on_off) {
4712		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4713			if (net->new_pseudo_cumack)
4714				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4715				    stcb, net,
4716				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_27);
4717
4718		}
4719	} else {
4720		if (accum_moved) {
4721			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4722				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4723				    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_28);
4724			}
4725		}
4726	}
4727	/********************************************/
4728	/* drop the acked chunks from the sendqueue */
4729	/********************************************/
4730	asoc->last_acked_seq = cum_ack;
4731
4732	tp1 = TAILQ_FIRST(&asoc->sent_queue);
4733	if (tp1 == NULL)
4734		goto done_with_it;
4735	do {
4736		if (compare_with_wrap(tp1->rec.data.TSN_seq, cum_ack,
4737		    MAX_TSN)) {
4738			break;
4739		}
4740		if (tp1->sent == SCTP_DATAGRAM_UNSENT) {
4741			/* no more sent on list */
4742			printf("Warning, tp1->sent == %d and its now acked?\n",
4743			    tp1->sent);
4744		}
4745		tp2 = TAILQ_NEXT(tp1, sctp_next);
4746		TAILQ_REMOVE(&asoc->sent_queue, tp1, sctp_next);
4747		if (tp1->pr_sctp_on) {
4748			if (asoc->pr_sctp_cnt != 0)
4749				asoc->pr_sctp_cnt--;
4750		}
4751		if ((TAILQ_FIRST(&asoc->sent_queue) == NULL) &&
4752		    (asoc->total_flight > 0)) {
4753#ifdef INVARIANTS
4754			panic("Warning flight size is postive and should be 0");
4755#else
4756			SCTP_PRINTF("Warning flight size incorrect should be 0 is %d\n",
4757			    asoc->total_flight);
4758#endif
4759			asoc->total_flight = 0;
4760		}
4761		if (tp1->data) {
4762			/* sa_ignore NO_NULL_CHK */
4763			sctp_free_bufspace(stcb, asoc, tp1, 1);
4764			sctp_m_freem(tp1->data);
4765			if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
4766				asoc->sent_queue_cnt_removeable--;
4767			}
4768		}
4769		if (sctp_logging_level & SCTP_SACK_LOGGING_ENABLE) {
4770			sctp_log_sack(asoc->last_acked_seq,
4771			    cum_ack,
4772			    tp1->rec.data.TSN_seq,
4773			    0,
4774			    0,
4775			    SCTP_LOG_FREE_SENT);
4776		}
4777		tp1->data = NULL;
4778		asoc->sent_queue_cnt--;
4779		sctp_free_a_chunk(stcb, tp1);
4780		wake_him++;
4781		tp1 = tp2;
4782	} while (tp1 != NULL);
4783
4784done_with_it:
4785	/* sa_ignore NO_NULL_CHK */
4786	if ((wake_him) && (stcb->sctp_socket)) {
4787#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4788		struct socket *so;
4789
4790#endif
4791		SOCKBUF_LOCK(&stcb->sctp_socket->so_snd);
4792		if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4793			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_WAKESND_FROM_SACK);
4794		}
4795#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4796		so = SCTP_INP_SO(stcb->sctp_ep);
4797		atomic_add_int(&stcb->asoc.refcnt, 1);
4798		SCTP_TCB_UNLOCK(stcb);
4799		SCTP_SOCKET_LOCK(so, 1);
4800		SCTP_TCB_LOCK(stcb);
4801		atomic_subtract_int(&stcb->asoc.refcnt, 1);
4802		if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4803			/* assoc was freed while we were unlocked */
4804			SCTP_SOCKET_UNLOCK(so, 1);
4805			return;
4806		}
4807#endif
4808		sctp_sowwakeup_locked(stcb->sctp_ep, stcb->sctp_socket);
4809#if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4810		SCTP_SOCKET_UNLOCK(so, 1);
4811#endif
4812	} else {
4813		if (sctp_logging_level & SCTP_WAKE_LOGGING_ENABLE) {
4814			sctp_wakeup_log(stcb, cum_ack, wake_him, SCTP_NOWAKE_FROM_SACK);
4815		}
4816	}
4817
4818	if (asoc->fast_retran_loss_recovery && accum_moved) {
4819		if (compare_with_wrap(asoc->last_acked_seq,
4820		    asoc->fast_recovery_tsn, MAX_TSN) ||
4821		    asoc->last_acked_seq == asoc->fast_recovery_tsn) {
4822			/* Setup so we will exit RFC2582 fast recovery */
4823			will_exit_fast_recovery = 1;
4824		}
4825	}
4826	/*
4827	 * Check for revoked fragments:
4828	 *
4829	 * if Previous sack - Had no frags then we can't have any revoked if
4830	 * Previous sack - Had frag's then - If we now have frags aka
4831	 * num_seg > 0 call sctp_check_for_revoked() to tell if peer revoked
4832	 * some of them. else - The peer revoked all ACKED fragments, since
4833	 * we had some before and now we have NONE.
4834	 */
4835
4836	if (num_seg)
4837		sctp_check_for_revoked(stcb, asoc, cum_ack, biggest_tsn_acked);
4838	else if (asoc->saw_sack_with_frags) {
4839		int cnt_revoked = 0;
4840
4841		tp1 = TAILQ_FIRST(&asoc->sent_queue);
4842		if (tp1 != NULL) {
4843			/* Peer revoked all dg's marked or acked */
4844			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
4845				if ((tp1->sent > SCTP_DATAGRAM_RESEND) &&
4846				    (tp1->sent < SCTP_FORWARD_TSN_SKIP)) {
4847					tp1->sent = SCTP_DATAGRAM_SENT;
4848					if (sctp_logging_level & SCTP_FLIGHT_LOGGING_ENABLE) {
4849						sctp_misc_ints(SCTP_FLIGHT_LOG_UP_REVOKE,
4850						    tp1->whoTo->flight_size,
4851						    tp1->book_size,
4852						    (uintptr_t) tp1->whoTo,
4853						    tp1->rec.data.TSN_seq);
4854					}
4855					sctp_flight_size_increase(tp1);
4856					sctp_total_flight_increase(stcb, tp1);
4857					tp1->rec.data.chunk_was_revoked = 1;
4858					/*
4859					 * To ensure that this increase in
4860					 * flightsize, which is artificial,
4861					 * does not throttle the sender, we
4862					 * also increase the cwnd
4863					 * artificially.
4864					 */
4865					tp1->whoTo->cwnd += tp1->book_size;
4866					cnt_revoked++;
4867				}
4868			}
4869			if (cnt_revoked) {
4870				reneged_all = 1;
4871			}
4872		}
4873		asoc->saw_sack_with_frags = 0;
4874	}
4875	if (num_seg)
4876		asoc->saw_sack_with_frags = 1;
4877	else
4878		asoc->saw_sack_with_frags = 0;
4879
4880	/* JRS - Use the congestion control given in the CC module */
4881	asoc->cc_functions.sctp_cwnd_update_after_sack(stcb, asoc, accum_moved, reneged_all, will_exit_fast_recovery);
4882
4883	if (TAILQ_EMPTY(&asoc->sent_queue)) {
4884		/* nothing left in-flight */
4885		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4886			/* stop all timers */
4887			if (sctp_early_fr) {
4888				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
4889					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
4890					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
4891					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_29);
4892				}
4893			}
4894			sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
4895			    stcb, net, SCTP_FROM_SCTP_INDATA + SCTP_LOC_30);
4896			net->flight_size = 0;
4897			net->partial_bytes_acked = 0;
4898		}
4899		asoc->total_flight = 0;
4900		asoc->total_flight_count = 0;
4901	}
4902	/**********************************/
4903	/* Now what about shutdown issues */
4904	/**********************************/
4905	if (TAILQ_EMPTY(&asoc->send_queue) && TAILQ_EMPTY(&asoc->sent_queue)) {
4906		/* nothing left on sendqueue.. consider done */
4907		if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
4908			sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
4909			    asoc->peers_rwnd, 0, 0, a_rwnd);
4910		}
4911		asoc->peers_rwnd = a_rwnd;
4912		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4913			/* SWS sender side engages */
4914			asoc->peers_rwnd = 0;
4915		}
4916		/* clean up */
4917		if ((asoc->stream_queue_cnt == 1) &&
4918		    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
4919		    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED)) &&
4920		    (asoc->locked_on_sending)
4921		    ) {
4922			struct sctp_stream_queue_pending *sp;
4923
4924			/*
4925			 * I may be in a state where we got all across.. but
4926			 * cannot write more due to a shutdown... we abort
4927			 * since the user did not indicate EOR in this case.
4928			 */
4929			sp = TAILQ_LAST(&((asoc->locked_on_sending)->outqueue),
4930			    sctp_streamhead);
4931			if ((sp) && (sp->length == 0)) {
4932				asoc->locked_on_sending = NULL;
4933				if (sp->msg_is_complete) {
4934					asoc->stream_queue_cnt--;
4935				} else {
4936					asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
4937					asoc->stream_queue_cnt--;
4938				}
4939			}
4940		}
4941		if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) &&
4942		    (asoc->stream_queue_cnt == 0)) {
4943			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4944				/* Need to abort here */
4945				struct mbuf *oper;
4946
4947		abort_out_now:
4948				*abort_now = 1;
4949				/* XXX */
4950				oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
4951				    0, M_DONTWAIT, 1, MT_DATA);
4952				if (oper) {
4953					struct sctp_paramhdr *ph;
4954					uint32_t *ippp;
4955
4956					SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
4957					    sizeof(uint32_t);
4958					ph = mtod(oper, struct sctp_paramhdr *);
4959					ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4960					ph->param_length = htons(SCTP_BUF_LEN(oper));
4961					ippp = (uint32_t *) (ph + 1);
4962					*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_31);
4963				}
4964				stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_31;
4965				sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, oper, SCTP_SO_NOT_LOCKED);
4966				return;
4967			} else {
4968				if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4969				    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4970					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4971				}
4972				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4973				sctp_stop_timers_for_shutdown(stcb);
4974				sctp_send_shutdown(stcb,
4975				    stcb->asoc.primary_destination);
4976				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
4977				    stcb->sctp_ep, stcb, asoc->primary_destination);
4978				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
4979				    stcb->sctp_ep, stcb, asoc->primary_destination);
4980			}
4981			return;
4982		} else if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) &&
4983		    (asoc->stream_queue_cnt == 0)) {
4984			if (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT) {
4985				goto abort_out_now;
4986			}
4987			SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4988			SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_ACK_SENT);
4989			sctp_send_shutdown_ack(stcb,
4990			    stcb->asoc.primary_destination);
4991
4992			sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNACK,
4993			    stcb->sctp_ep, stcb, asoc->primary_destination);
4994			return;
4995		}
4996	}
4997	/*
4998	 * Now here we are going to recycle net_ack for a different use...
4999	 * HEADS UP.
5000	 */
5001	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5002		net->net_ack = 0;
5003	}
5004
5005	/*
5006	 * CMT DAC algorithm: If SACK DAC flag was 0, then no extra marking
5007	 * to be done. Setting this_sack_lowest_newack to the cum_ack will
5008	 * automatically ensure that.
5009	 */
5010	if (sctp_cmt_on_off && sctp_cmt_use_dac && (cmt_dac_flag == 0)) {
5011		this_sack_lowest_newack = cum_ack;
5012	}
5013	if (num_seg > 0) {
5014		sctp_strike_gap_ack_chunks(stcb, asoc, biggest_tsn_acked,
5015		    biggest_tsn_newly_acked, this_sack_lowest_newack, accum_moved);
5016	}
5017	/*********************************************/
5018	/* Here we perform PR-SCTP procedures        */
5019	/* (section 4.2)                             */
5020	/*********************************************/
5021	/* C1. update advancedPeerAckPoint */
5022	if (compare_with_wrap(cum_ack, asoc->advanced_peer_ack_point, MAX_TSN)) {
5023		asoc->advanced_peer_ack_point = cum_ack;
5024	}
5025	/* C2. try to further move advancedPeerAckPoint ahead */
5026	if ((asoc->peer_supports_prsctp) && (asoc->pr_sctp_cnt > 0)) {
5027		struct sctp_tmit_chunk *lchk;
5028
5029		lchk = sctp_try_advance_peer_ack_point(stcb, asoc);
5030		/* C3. See if we need to send a Fwd-TSN */
5031		if (compare_with_wrap(asoc->advanced_peer_ack_point, cum_ack,
5032		    MAX_TSN)) {
5033			/*
5034			 * ISSUE with ECN, see FWD-TSN processing for notes
5035			 * on issues that will occur when the ECN NONCE
5036			 * stuff is put into SCTP for cross checking.
5037			 */
5038			send_forward_tsn(stcb, asoc);
5039
5040			/*
5041			 * ECN Nonce: Disable Nonce Sum check when FWD TSN
5042			 * is sent and store resync tsn
5043			 */
5044			asoc->nonce_sum_check = 0;
5045			asoc->nonce_resync_tsn = asoc->advanced_peer_ack_point;
5046			if (lchk) {
5047				/* Assure a timer is up */
5048				sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5049				    stcb->sctp_ep, stcb, lchk->whoTo);
5050			}
5051		}
5052	}
5053	/* JRS - Use the congestion control given in the CC module */
5054	asoc->cc_functions.sctp_cwnd_update_after_fr(stcb, asoc);
5055
5056	/******************************************************************
5057	 *  Here we do the stuff with ECN Nonce checking.
5058	 *  We basically check to see if the nonce sum flag was incorrect
5059	 *  or if resynchronization needs to be done. Also if we catch a
5060	 *  misbehaving receiver we give him the kick.
5061	 ******************************************************************/
5062
5063	if (asoc->ecn_nonce_allowed) {
5064		if (asoc->nonce_sum_check) {
5065			if (nonce_sum_flag != ((asoc->nonce_sum_expect_base + ecn_seg_sums) & SCTP_SACK_NONCE_SUM)) {
5066				if (asoc->nonce_wait_for_ecne == 0) {
5067					struct sctp_tmit_chunk *lchk;
5068
5069					lchk = TAILQ_FIRST(&asoc->send_queue);
5070					asoc->nonce_wait_for_ecne = 1;
5071					if (lchk) {
5072						asoc->nonce_wait_tsn = lchk->rec.data.TSN_seq;
5073					} else {
5074						asoc->nonce_wait_tsn = asoc->sending_seq;
5075					}
5076				} else {
5077					if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_wait_tsn, MAX_TSN) ||
5078					    (asoc->last_acked_seq == asoc->nonce_wait_tsn)) {
5079						/*
5080						 * Misbehaving peer. We need
5081						 * to react to this guy
5082						 */
5083						asoc->ecn_allowed = 0;
5084						asoc->ecn_nonce_allowed = 0;
5085					}
5086				}
5087			}
5088		} else {
5089			/* See if Resynchronization Possible */
5090			if (compare_with_wrap(asoc->last_acked_seq, asoc->nonce_resync_tsn, MAX_TSN)) {
5091				asoc->nonce_sum_check = 1;
5092				/*
5093				 * now we must calculate what the base is.
5094				 * We do this based on two things, we know
5095				 * the total's for all the segments
5096				 * gap-acked in the SACK, its stored in
5097				 * ecn_seg_sums. We also know the SACK's
5098				 * nonce sum, its in nonce_sum_flag. So we
5099				 * can build a truth table to back-calculate
5100				 * the new value of
5101				 * asoc->nonce_sum_expect_base:
5102				 *
5103				 * SACK-flag-Value         Seg-Sums Base 0 0 0
5104				 * 1                    0 1 0 1 1 1
5105				 * 1 0
5106				 */
5107				asoc->nonce_sum_expect_base = (ecn_seg_sums ^ nonce_sum_flag) & SCTP_SACK_NONCE_SUM;
5108			}
5109		}
5110	}
5111	/* Now are we exiting loss recovery ? */
5112	if (will_exit_fast_recovery) {
5113		/* Ok, we must exit fast recovery */
5114		asoc->fast_retran_loss_recovery = 0;
5115	}
5116	if ((asoc->sat_t3_loss_recovery) &&
5117	    ((compare_with_wrap(asoc->last_acked_seq, asoc->sat_t3_recovery_tsn,
5118	    MAX_TSN) ||
5119	    (asoc->last_acked_seq == asoc->sat_t3_recovery_tsn)))) {
5120		/* end satellite t3 loss recovery */
5121		asoc->sat_t3_loss_recovery = 0;
5122	}
5123	/*
5124	 * CMT Fast recovery
5125	 */
5126	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5127		if (net->will_exit_fast_recovery) {
5128			/* Ok, we must exit fast recovery */
5129			net->fast_retran_loss_recovery = 0;
5130		}
5131	}
5132
5133	/* Adjust and set the new rwnd value */
5134	if (sctp_logging_level & SCTP_LOG_RWND_ENABLE) {
5135		sctp_log_rwnd_set(SCTP_SET_PEER_RWND_VIA_SACK,
5136		    asoc->peers_rwnd, asoc->total_flight, (asoc->sent_queue_cnt * sctp_peer_chunk_oh), a_rwnd);
5137	}
5138	asoc->peers_rwnd = sctp_sbspace_sub(a_rwnd,
5139	    (uint32_t) (asoc->total_flight + (asoc->sent_queue_cnt * sctp_peer_chunk_oh)));
5140	if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5141		/* SWS sender side engages */
5142		asoc->peers_rwnd = 0;
5143	}
5144	if (asoc->peers_rwnd > old_rwnd) {
5145		win_probe_recovery = 1;
5146	}
5147	/*
5148	 * Now we must setup so we have a timer up for anyone with
5149	 * outstanding data.
5150	 */
5151	done_once = 0;
5152again:
5153	j = 0;
5154	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5155		if (win_probe_recovery && (net->window_probe)) {
5156			net->window_probe = 0;
5157			win_probe_recovered = 1;
5158			/*-
5159			 * Find first chunk that was used with
5160			 * window probe and clear the event. Put
5161			 * it back into the send queue as if has
5162			 * not been sent.
5163			 */
5164			TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5165				if (tp1->window_probe) {
5166					sctp_window_probe_recovery(stcb, asoc, net, tp1);
5167					break;
5168				}
5169			}
5170		}
5171		if (net->flight_size) {
5172			j++;
5173			sctp_timer_start(SCTP_TIMER_TYPE_SEND,
5174			    stcb->sctp_ep, stcb, net);
5175		} else {
5176			if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5177				sctp_timer_stop(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep,
5178				    stcb, net,
5179				    SCTP_FROM_SCTP_INDATA + SCTP_LOC_22);
5180			}
5181			if (sctp_early_fr) {
5182				if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
5183					SCTP_STAT_INCR(sctps_earlyfrstpidsck4);
5184					sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, stcb->sctp_ep, stcb, net,
5185					    SCTP_FROM_SCTP_INDATA + SCTP_LOC_23);
5186				}
5187			}
5188		}
5189	}
5190	if ((j == 0) &&
5191	    (!TAILQ_EMPTY(&asoc->sent_queue)) &&
5192	    (asoc->sent_queue_retran_cnt == 0) &&
5193	    (win_probe_recovered == 0) &&
5194	    (done_once == 0)) {
5195		/* huh, this should not happen */
5196		sctp_fs_audit(asoc);
5197		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5198			net->flight_size = 0;
5199		}
5200		asoc->total_flight = 0;
5201		asoc->total_flight_count = 0;
5202		asoc->sent_queue_retran_cnt = 0;
5203		TAILQ_FOREACH(tp1, &asoc->sent_queue, sctp_next) {
5204			if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5205				sctp_flight_size_increase(tp1);
5206				sctp_total_flight_increase(stcb, tp1);
5207			} else if (tp1->sent == SCTP_DATAGRAM_RESEND) {
5208				asoc->sent_queue_retran_cnt++;
5209			}
5210		}
5211		done_once = 1;
5212		goto again;
5213	}
5214	if (sctp_logging_level & SCTP_SACK_RWND_LOGGING_ENABLE) {
5215		sctp_misc_ints(SCTP_SACK_RWND_UPDATE,
5216		    a_rwnd,
5217		    stcb->asoc.peers_rwnd,
5218		    stcb->asoc.total_flight,
5219		    stcb->asoc.total_output_queue_size);
5220	}
5221}
5222
5223void
5224sctp_update_acked(struct sctp_tcb *stcb, struct sctp_shutdown_chunk *cp,
5225    struct sctp_nets *netp, int *abort_flag)
5226{
5227	/* Copy cum-ack */
5228	uint32_t cum_ack, a_rwnd;
5229
5230	cum_ack = ntohl(cp->cumulative_tsn_ack);
5231	/* Arrange so a_rwnd does NOT change */
5232	a_rwnd = stcb->asoc.peers_rwnd + stcb->asoc.total_flight;
5233
5234	/* Now call the express sack handling */
5235	sctp_express_handle_sack(stcb, cum_ack, a_rwnd, 0, abort_flag);
5236}
5237
5238static void
5239sctp_kick_prsctp_reorder_queue(struct sctp_tcb *stcb,
5240    struct sctp_stream_in *strmin)
5241{
5242	struct sctp_queued_to_read *ctl, *nctl;
5243	struct sctp_association *asoc;
5244	int tt;
5245
5246	asoc = &stcb->asoc;
5247	tt = strmin->last_sequence_delivered;
5248	/*
5249	 * First deliver anything prior to and including the stream no that
5250	 * came in
5251	 */
5252	ctl = TAILQ_FIRST(&strmin->inqueue);
5253	while (ctl) {
5254		nctl = TAILQ_NEXT(ctl, next);
5255		if (compare_with_wrap(tt, ctl->sinfo_ssn, MAX_SEQ) ||
5256		    (tt == ctl->sinfo_ssn)) {
5257			/* this is deliverable now */
5258			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5259			/* subtract pending on streams */
5260			asoc->size_on_all_streams -= ctl->length;
5261			sctp_ucount_decr(asoc->cnt_on_all_streams);
5262			/* deliver it to at least the delivery-q */
5263			if (stcb->sctp_socket) {
5264				sctp_add_to_readq(stcb->sctp_ep, stcb,
5265				    ctl,
5266				    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
5267			}
5268		} else {
5269			/* no more delivery now. */
5270			break;
5271		}
5272		ctl = nctl;
5273	}
5274	/*
5275	 * now we must deliver things in queue the normal way  if any are
5276	 * now ready.
5277	 */
5278	tt = strmin->last_sequence_delivered + 1;
5279	ctl = TAILQ_FIRST(&strmin->inqueue);
5280	while (ctl) {
5281		nctl = TAILQ_NEXT(ctl, next);
5282		if (tt == ctl->sinfo_ssn) {
5283			/* this is deliverable now */
5284			TAILQ_REMOVE(&strmin->inqueue, ctl, next);
5285			/* subtract pending on streams */
5286			asoc->size_on_all_streams -= ctl->length;
5287			sctp_ucount_decr(asoc->cnt_on_all_streams);
5288			/* deliver it to at least the delivery-q */
5289			strmin->last_sequence_delivered = ctl->sinfo_ssn;
5290			if (stcb->sctp_socket) {
5291				sctp_add_to_readq(stcb->sctp_ep, stcb,
5292				    ctl,
5293				    &stcb->sctp_socket->so_rcv, 1, SCTP_SO_NOT_LOCKED);
5294			}
5295			tt = strmin->last_sequence_delivered + 1;
5296		} else {
5297			break;
5298		}
5299		ctl = nctl;
5300	}
5301}
5302
5303void
5304sctp_handle_forward_tsn(struct sctp_tcb *stcb,
5305    struct sctp_forward_tsn_chunk *fwd, int *abort_flag, struct mbuf *m, int offset)
5306{
5307	/*
5308	 * ISSUES that MUST be fixed for ECN! When we are the sender of the
5309	 * forward TSN, when the SACK comes back that acknowledges the
5310	 * FWD-TSN we must reset the NONCE sum to match correctly. This will
5311	 * get quite tricky since we may have sent more data interveneing
5312	 * and must carefully account for what the SACK says on the nonce
5313	 * and any gaps that are reported. This work will NOT be done here,
5314	 * but I note it here since it is really related to PR-SCTP and
5315	 * FWD-TSN's
5316	 */
5317
5318	/* The pr-sctp fwd tsn */
5319	/*
5320	 * here we will perform all the data receiver side steps for
5321	 * processing FwdTSN, as required in by pr-sctp draft:
5322	 *
5323	 * Assume we get FwdTSN(x):
5324	 *
5325	 * 1) update local cumTSN to x 2) try to further advance cumTSN to x +
5326	 * others we have 3) examine and update re-ordering queue on
5327	 * pr-in-streams 4) clean up re-assembly queue 5) Send a sack to
5328	 * report where we are.
5329	 */
5330	struct sctp_association *asoc;
5331	uint32_t new_cum_tsn, gap;
5332	unsigned int i, cnt_gone, fwd_sz, cumack_set_flag, m_size;
5333	struct sctp_stream_in *strm;
5334	struct sctp_tmit_chunk *chk, *at;
5335
5336	cumack_set_flag = 0;
5337	asoc = &stcb->asoc;
5338	cnt_gone = 0;
5339	if ((fwd_sz = ntohs(fwd->ch.chunk_length)) < sizeof(struct sctp_forward_tsn_chunk)) {
5340		SCTPDBG(SCTP_DEBUG_INDATA1,
5341		    "Bad size too small/big fwd-tsn\n");
5342		return;
5343	}
5344	m_size = (stcb->asoc.mapping_array_size << 3);
5345	/*************************************************************/
5346	/* 1. Here we update local cumTSN and shift the bitmap array */
5347	/*************************************************************/
5348	new_cum_tsn = ntohl(fwd->new_cumulative_tsn);
5349
5350	if (compare_with_wrap(asoc->cumulative_tsn, new_cum_tsn, MAX_TSN) ||
5351	    asoc->cumulative_tsn == new_cum_tsn) {
5352		/* Already got there ... */
5353		return;
5354	}
5355	if (compare_with_wrap(new_cum_tsn, asoc->highest_tsn_inside_map,
5356	    MAX_TSN)) {
5357		asoc->highest_tsn_inside_map = new_cum_tsn;
5358		if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5359			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5360		}
5361	}
5362	/*
5363	 * now we know the new TSN is more advanced, let's find the actual
5364	 * gap
5365	 */
5366	if ((compare_with_wrap(new_cum_tsn, asoc->mapping_array_base_tsn,
5367	    MAX_TSN)) ||
5368	    (new_cum_tsn == asoc->mapping_array_base_tsn)) {
5369		gap = new_cum_tsn - asoc->mapping_array_base_tsn;
5370	} else {
5371		/* try to prevent underflow here */
5372		gap = new_cum_tsn + (MAX_TSN - asoc->mapping_array_base_tsn) + 1;
5373	}
5374
5375	if (gap >= m_size) {
5376		if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5377			sctp_log_map(0, 0, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5378		}
5379		if ((long)gap > sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv)) {
5380			struct mbuf *oper;
5381
5382			/*
5383			 * out of range (of single byte chunks in the rwnd I
5384			 * give out). This must be an attacker.
5385			 */
5386			*abort_flag = 1;
5387			oper = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + 3 * sizeof(uint32_t)),
5388			    0, M_DONTWAIT, 1, MT_DATA);
5389			if (oper) {
5390				struct sctp_paramhdr *ph;
5391				uint32_t *ippp;
5392
5393				SCTP_BUF_LEN(oper) = sizeof(struct sctp_paramhdr) +
5394				    (sizeof(uint32_t) * 3);
5395				ph = mtod(oper, struct sctp_paramhdr *);
5396				ph->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5397				ph->param_length = htons(SCTP_BUF_LEN(oper));
5398				ippp = (uint32_t *) (ph + 1);
5399				*ippp = htonl(SCTP_FROM_SCTP_INDATA + SCTP_LOC_33);
5400				ippp++;
5401				*ippp = asoc->highest_tsn_inside_map;
5402				ippp++;
5403				*ippp = new_cum_tsn;
5404			}
5405			stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_INDATA + SCTP_LOC_33;
5406			sctp_abort_an_association(stcb->sctp_ep, stcb,
5407			    SCTP_PEER_FAULTY, oper, SCTP_SO_NOT_LOCKED);
5408			return;
5409		}
5410		SCTP_STAT_INCR(sctps_fwdtsn_map_over);
5411slide_out:
5412		memset(stcb->asoc.mapping_array, 0, stcb->asoc.mapping_array_size);
5413		cumack_set_flag = 1;
5414		asoc->mapping_array_base_tsn = new_cum_tsn + 1;
5415		asoc->cumulative_tsn = asoc->highest_tsn_inside_map = new_cum_tsn;
5416
5417		if (sctp_logging_level & SCTP_MAP_LOGGING_ENABLE) {
5418			sctp_log_map(0, 3, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
5419		}
5420		asoc->last_echo_tsn = asoc->highest_tsn_inside_map;
5421	} else {
5422		SCTP_TCB_LOCK_ASSERT(stcb);
5423		if ((compare_with_wrap(((uint32_t) asoc->cumulative_tsn + gap), asoc->highest_tsn_inside_map, MAX_TSN)) ||
5424		    (((uint32_t) asoc->cumulative_tsn + gap) == asoc->highest_tsn_inside_map)) {
5425			goto slide_out;
5426		} else {
5427			for (i = 0; i <= gap; i++) {
5428				SCTP_SET_TSN_PRESENT(asoc->mapping_array, i);
5429			}
5430		}
5431		/*
5432		 * Now after marking all, slide thing forward but no sack
5433		 * please.
5434		 */
5435		sctp_sack_check(stcb, 0, 0, abort_flag);
5436		if (*abort_flag)
5437			return;
5438	}
5439
5440	/*************************************************************/
5441	/* 2. Clear up re-assembly queue                             */
5442	/*************************************************************/
5443	/*
5444	 * First service it if pd-api is up, just in case we can progress it
5445	 * forward
5446	 */
5447	if (asoc->fragmented_delivery_inprogress) {
5448		sctp_service_reassembly(stcb, asoc);
5449	}
5450	if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
5451		/* For each one on here see if we need to toss it */
5452		/*
5453		 * For now large messages held on the reasmqueue that are
5454		 * complete will be tossed too. We could in theory do more
5455		 * work to spin through and stop after dumping one msg aka
5456		 * seeing the start of a new msg at the head, and call the
5457		 * delivery function... to see if it can be delivered... But
5458		 * for now we just dump everything on the queue.
5459		 */
5460		chk = TAILQ_FIRST(&asoc->reasmqueue);
5461		while (chk) {
5462			at = TAILQ_NEXT(chk, sctp_next);
5463			if (compare_with_wrap(asoc->cumulative_tsn,
5464			    chk->rec.data.TSN_seq, MAX_TSN) ||
5465			    asoc->cumulative_tsn == chk->rec.data.TSN_seq) {
5466				/* It needs to be tossed */
5467				TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5468				if (compare_with_wrap(chk->rec.data.TSN_seq,
5469				    asoc->tsn_last_delivered, MAX_TSN)) {
5470					asoc->tsn_last_delivered =
5471					    chk->rec.data.TSN_seq;
5472					asoc->str_of_pdapi =
5473					    chk->rec.data.stream_number;
5474					asoc->ssn_of_pdapi =
5475					    chk->rec.data.stream_seq;
5476					asoc->fragment_flags =
5477					    chk->rec.data.rcv_flags;
5478				}
5479				asoc->size_on_reasm_queue -= chk->send_size;
5480				sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5481				cnt_gone++;
5482
5483				/* Clear up any stream problem */
5484				if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) !=
5485				    SCTP_DATA_UNORDERED &&
5486				    (compare_with_wrap(chk->rec.data.stream_seq,
5487				    asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered,
5488				    MAX_SEQ))) {
5489					/*
5490					 * We must dump forward this streams
5491					 * sequence number if the chunk is
5492					 * not unordered that is being
5493					 * skipped. There is a chance that
5494					 * if the peer does not include the
5495					 * last fragment in its FWD-TSN we
5496					 * WILL have a problem here since
5497					 * you would have a partial chunk in
5498					 * queue that may not be
5499					 * deliverable. Also if a Partial
5500					 * delivery API as started the user
5501					 * may get a partial chunk. The next
5502					 * read returning a new chunk...
5503					 * really ugly but I see no way
5504					 * around it! Maybe a notify??
5505					 */
5506					asoc->strmin[chk->rec.data.stream_number].last_sequence_delivered =
5507					    chk->rec.data.stream_seq;
5508				}
5509				if (chk->data) {
5510					sctp_m_freem(chk->data);
5511					chk->data = NULL;
5512				}
5513				sctp_free_a_chunk(stcb, chk);
5514			} else {
5515				/*
5516				 * Ok we have gone beyond the end of the
5517				 * fwd-tsn's mark. Some checks...
5518				 */
5519				if ((asoc->fragmented_delivery_inprogress) &&
5520				    (chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG)) {
5521					uint32_t str_seq;
5522
5523					/*
5524					 * Special case PD-API is up and
5525					 * what we fwd-tsn' over includes
5526					 * one that had the LAST_FRAG. We no
5527					 * longer need to do the PD-API.
5528					 */
5529					asoc->fragmented_delivery_inprogress = 0;
5530
5531					str_seq = (asoc->str_of_pdapi << 16) | asoc->ssn_of_pdapi;
5532					sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5533					    stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)&str_seq, SCTP_SO_NOT_LOCKED);
5534
5535				}
5536				break;
5537			}
5538			chk = at;
5539		}
5540	}
5541	if (asoc->fragmented_delivery_inprogress) {
5542		/*
5543		 * Ok we removed cnt_gone chunks in the PD-API queue that
5544		 * were being delivered. So now we must turn off the flag.
5545		 */
5546		uint32_t str_seq;
5547
5548		str_seq = (asoc->str_of_pdapi << 16) | asoc->ssn_of_pdapi;
5549		sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5550		    stcb, SCTP_PARTIAL_DELIVERY_ABORTED, (void *)&str_seq, SCTP_SO_NOT_LOCKED);
5551		asoc->fragmented_delivery_inprogress = 0;
5552	}
5553	/*************************************************************/
5554	/* 3. Update the PR-stream re-ordering queues                */
5555	/*************************************************************/
5556	fwd_sz -= sizeof(*fwd);
5557	if (m && fwd_sz) {
5558		/* New method. */
5559		unsigned int num_str;
5560		struct sctp_strseq *stseq, strseqbuf;
5561
5562		offset += sizeof(*fwd);
5563
5564		num_str = fwd_sz / sizeof(struct sctp_strseq);
5565		for (i = 0; i < num_str; i++) {
5566			uint16_t st;
5567
5568			stseq = (struct sctp_strseq *)sctp_m_getptr(m, offset,
5569			    sizeof(struct sctp_strseq),
5570			    (uint8_t *) & strseqbuf);
5571			offset += sizeof(struct sctp_strseq);
5572			if (stseq == NULL) {
5573				break;
5574			}
5575			/* Convert */
5576			st = ntohs(stseq->stream);
5577			stseq->stream = st;
5578			st = ntohs(stseq->sequence);
5579			stseq->sequence = st;
5580			/* now process */
5581			if (stseq->stream >= asoc->streamincnt) {
5582				/* screwed up streams, stop!  */
5583				break;
5584			}
5585			strm = &asoc->strmin[stseq->stream];
5586			if (compare_with_wrap(stseq->sequence,
5587			    strm->last_sequence_delivered, MAX_SEQ)) {
5588				/* Update the sequence number */
5589				strm->last_sequence_delivered =
5590				    stseq->sequence;
5591			}
5592			/* now kick the stream the new way */
5593			/* sa_ignore NO_NULL_CHK */
5594			sctp_kick_prsctp_reorder_queue(stcb, strm);
5595		}
5596	}
5597	if (TAILQ_FIRST(&asoc->reasmqueue)) {
5598		/* now lets kick out and check for more fragmented delivery */
5599		/* sa_ignore NO_NULL_CHK */
5600		sctp_deliver_reasm_check(stcb, &stcb->asoc);
5601	}
5602}
5603